Skip to content

Commit

Permalink
Merge pull request lcompilers#1653 from harshsingh-24/dict-funcs
Browse files Browse the repository at this point in the history
Initial Support for Global Dictionaries
  • Loading branch information
certik committed Apr 1, 2023
2 parents f8b720c + ce1d4ea commit 071dd40
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -443,3 +443,4 @@ RUN(NAME global_syms_02 LABELS cpython llvm c)
RUN(NAME global_syms_03_b LABELS cpython llvm c)
RUN(NAME global_syms_03_c LABELS cpython llvm c)
RUN(NAME global_syms_04 LABELS cpython llvm c wasm wasm_x64)
RUN(NAME global_syms_05 LABELS cpython llvm c)
21 changes: 21 additions & 0 deletions integration_tests/global_syms_05.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from lpython import i32

# GLOBAL Dictionary
x: dict[i32, i32]
x = {0 : 0, 1: 1000, 2: 2000, 3 : 3000}

x[4] = 4000
assert len(x) == 5
x[5] = 5000
assert x[2] == 2000
i: i32
for i in range(len(x)):
assert x[i] == i * 1000

# Copy of Dictionary
tmp: dict[i32, i32]
tmp = x
tmp[6] = 6000
assert len(tmp) == 7
assert tmp[6] == 6000
assert tmp[1] == 1000
8 changes: 8 additions & 0 deletions src/libasr/codegen/asr_to_llvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2304,6 +2304,14 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
llvm::ConstantStruct::get(list_type,
llvm::Constant::getNullValue(list_type)));
llvm_symtab[h] = ptr;
} else if(x.m_type->type == ASR::ttypeType::Dict) {
llvm::StructType* dict_type = static_cast<llvm::StructType*>(
get_type_from_ttype_t_util(x.m_type));
llvm::Constant *ptr = module->getOrInsertGlobal(x.m_name, dict_type);
module->getNamedGlobal(x.m_name)->setInitializer(
llvm::ConstantStruct::get(dict_type,
llvm::Constant::getNullValue(dict_type)));
llvm_symtab[h] = ptr;
} else if (x.m_type->type == ASR::ttypeType::TypeParameter) {
// Ignore type variables
} else {
Expand Down

0 comments on commit 071dd40

Please sign in to comment.