Skip to content

Commit 071dd40

Browse files
authored
Merge pull request #1653 from harshsingh-24/dict-funcs
Initial Support for Global Dictionaries
2 parents f8b720c + ce1d4ea commit 071dd40

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

integration_tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,3 +443,4 @@ RUN(NAME global_syms_02 LABELS cpython llvm c)
443443
RUN(NAME global_syms_03_b LABELS cpython llvm c)
444444
RUN(NAME global_syms_03_c LABELS cpython llvm c)
445445
RUN(NAME global_syms_04 LABELS cpython llvm c wasm wasm_x64)
446+
RUN(NAME global_syms_05 LABELS cpython llvm c)

integration_tests/global_syms_05.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from lpython import i32
2+
3+
# GLOBAL Dictionary
4+
x: dict[i32, i32]
5+
x = {0 : 0, 1: 1000, 2: 2000, 3 : 3000}
6+
7+
x[4] = 4000
8+
assert len(x) == 5
9+
x[5] = 5000
10+
assert x[2] == 2000
11+
i: i32
12+
for i in range(len(x)):
13+
assert x[i] == i * 1000
14+
15+
# Copy of Dictionary
16+
tmp: dict[i32, i32]
17+
tmp = x
18+
tmp[6] = 6000
19+
assert len(tmp) == 7
20+
assert tmp[6] == 6000
21+
assert tmp[1] == 1000

src/libasr/codegen/asr_to_llvm.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2304,6 +2304,14 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
23042304
llvm::ConstantStruct::get(list_type,
23052305
llvm::Constant::getNullValue(list_type)));
23062306
llvm_symtab[h] = ptr;
2307+
} else if(x.m_type->type == ASR::ttypeType::Dict) {
2308+
llvm::StructType* dict_type = static_cast<llvm::StructType*>(
2309+
get_type_from_ttype_t_util(x.m_type));
2310+
llvm::Constant *ptr = module->getOrInsertGlobal(x.m_name, dict_type);
2311+
module->getNamedGlobal(x.m_name)->setInitializer(
2312+
llvm::ConstantStruct::get(dict_type,
2313+
llvm::Constant::getNullValue(dict_type)));
2314+
llvm_symtab[h] = ptr;
23072315
} else if (x.m_type->type == ASR::ttypeType::TypeParameter) {
23082316
// Ignore type variables
23092317
} else {

0 commit comments

Comments
 (0)