Skip to content

Commit 3635e32

Browse files
XX Test global lists using void type
1 parent cc02ba9 commit 3635e32

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

examples/expr2.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
1+
from ltypes import i32
2+
3+
x: list[i32]
4+
x.append(100)
5+
x = [100, 200, 300]
6+
i: i32
7+
i = 1
8+
i = x[0]
9+
110
def main0():
2-
x: i32
3-
x = (2+3)*5
11+
# This doesn't work yet
12+
# x: list[i32] = [1,3,4]
13+
# print(i)
414
print(x)
15+
x.append(400)
16+
517

618
main0()
19+
print(x, i)
720

821
# Not implemented yet in LPython:
922
#if __name__ == "__main__":

src/libasr/codegen/asr_to_llvm.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1613,7 +1613,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
16131613
int64_t ptr_loads_copy = ptr_loads;
16141614
ptr_loads = 0;
16151615
this->visit_expr(*x.m_a);
1616-
llvm::Value* plist = tmp;
1616+
llvm::Value* plist = builder->CreateBitCast(tmp, list_type);
16171617

16181618
ptr_loads = !LLVM::is_llvm_struct(asr_list->m_type);
16191619
this->visit_expr_wrapper(x.m_ele, true);
@@ -1652,7 +1652,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
16521652
int64_t ptr_loads_copy = ptr_loads;
16531653
ptr_loads = 0;
16541654
this->visit_expr(*x.m_a);
1655-
llvm::Value* plist = tmp;
1655+
llvm::Value* plist = builder->CreateBitCast(tmp, list_type);
16561656

16571657
ptr_loads = 1;
16581658
this->visit_expr_wrapper(x.m_pos, true);
@@ -1707,7 +1707,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
17071707
ptr_loads = 0;
17081708
this->visit_expr(*x.m_arg);
17091709
ptr_loads = ptr_loads_copy;
1710-
llvm::Value* plist = tmp;
1710+
llvm::Value* plist = builder->CreateBitCast(tmp, list_type);
17111711
tmp = list_api->len(plist);
17121712
}
17131713
}
@@ -2295,6 +2295,21 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
22952295
}
22962296
}
22972297
llvm_symtab[h] = ptr;
2298+
} else if (x.m_type->type == ASR::ttypeType::List) {
2299+
ASR::storage_typeType m_storage = ASR::storage_typeType::Default;
2300+
bool is_array_type = false, is_malloc_array_type = false, is_list = false;
2301+
ASR::dimension_t* m_dims = nullptr;
2302+
int n_dims = -1, a_kind = -1;
2303+
list_type = get_type_from_ttype_t(x.m_type, m_storage, is_array_type,
2304+
is_malloc_array_type, is_list, m_dims, n_dims, a_kind)->getPointerTo();
2305+
llvm::Type* void_ptr = llvm::Type::getVoidTy(context)->getPointerTo();
2306+
llvm::Constant *ptr = module->getOrInsertGlobal(x.m_name,
2307+
void_ptr);
2308+
module->getNamedGlobal(x.m_name)->setInitializer(
2309+
llvm::ConstantPointerNull::get(
2310+
static_cast<llvm::PointerType*>(void_ptr))
2311+
);
2312+
llvm_symtab[h] = ptr;
22982313
} else if (x.m_type->type == ASR::ttypeType::TypeParameter) {
22992314
// Ignore type variables
23002315
} else {

src/libasr/codegen/llvm_utils.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,7 @@ namespace LCompilers {
657657
void LLVMList::list_deepcopy(llvm::Value* src, llvm::Value* dest,
658658
ASR::ttype_t* element_type, llvm::Module* module,
659659
std::map<std::string, std::map<std::string, int>>& name2memidx) {
660+
dest = builder->CreateBitCast(dest, src->getType());
660661
LCOMPILERS_ASSERT(src->getType() == dest->getType());
661662
std::string src_type_code = ASRUtils::get_type_code(element_type);
662663
llvm::Value* src_end_point = LLVM::CreateLoad(*builder, get_pointer_to_current_end_point(src));

0 commit comments

Comments
 (0)