Skip to content

Commit 104f5f9

Browse files
Implement global List in module scope
1 parent 51473ae commit 104f5f9

File tree

3 files changed

+48
-8
lines changed

3 files changed

+48
-8
lines changed

examples/expr2.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
def main0():
2-
x: i32
3-
x = (2+3)*5
4-
print(x)
1+
from ltypes import i32
52

6-
main0()
3+
x: list[i32]
4+
x = [1, 2, 3]
5+
# i: i32 = x[0]
6+
print(x)
7+
8+
# def main0():
9+
# # This doesn't work yet
10+
# print(x[0])
11+
12+
# main0()
713

814
# Not implemented yet in LPython:
915
#if __name__ == "__main__":

src/libasr/codegen/asr_to_llvm.cpp

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2196,6 +2196,8 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
21962196
llvm_symtab[h] = ptr;
21972197
} else if (x.m_type->type == ASR::ttypeType::TypeParameter) {
21982198
// Ignore type variables
2199+
} else if (x.m_type->type == ASR::ttypeType::List) {
2200+
// Handled in ``_lpython_main_program``function_scope
21992201
} else {
22002202
throw CodeGenError("Variable type not supported", x.base.base.loc);
22012203
}
@@ -5309,7 +5311,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
53095311
}
53105312
}
53115313

5312-
inline void fetch_var(ASR::Variable_t* x) {
5314+
inline void fetch_var(ASR::Variable_t* x, bool is_external=false) {
53135315
if (x->m_value) {
53145316
this->visit_expr_wrapper(x->m_value, true);
53155317
return;
@@ -5369,6 +5371,28 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
53695371
}
53705372
break;
53715373
}
5374+
case ASR::ttypeType::List: {
5375+
if (is_external) {
5376+
uint32_t h = get_hash((ASR::asr_t*)x);
5377+
llvm::Type *type;
5378+
int n_dims = 0, a_kind = 4;
5379+
ASR::dimension_t* m_dims = nullptr;
5380+
bool is_array_type = false, is_malloc_array_type = false;
5381+
bool is_list = false;
5382+
type = get_type_from_ttype_t(x->m_type, x->m_storage, is_array_type,
5383+
is_malloc_array_type, is_list, m_dims, n_dims,
5384+
a_kind);
5385+
llvm::AllocaInst *ptr = builder->CreateAlloca(type, nullptr, x->m_name);
5386+
llvm_symtab[h] = ptr;
5387+
if( llvm_symtab.find(h) != llvm_symtab.end() ) {
5388+
tmp = llvm_symtab[h];
5389+
}
5390+
ASR::List_t* asr_list = ASR::down_cast<ASR::List_t>(x->m_type);
5391+
std::string type_code = ASRUtils::get_type_code(asr_list->m_type);
5392+
list_api->list_init(type_code, tmp, *module);
5393+
break;
5394+
}
5395+
}
53725396
default: {
53735397
fetch_val(x);
53745398
break;
@@ -5377,9 +5401,13 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
53775401
}
53785402

53795403
void visit_Var(const ASR::Var_t &x) {
5404+
bool is_external = false;
5405+
if(ASR::is_a<ASR::ExternalSymbol_t>(*x.m_v)) {
5406+
is_external = true;
5407+
}
53805408
ASR::Variable_t *v = ASR::down_cast<ASR::Variable_t>(
53815409
symbol_get_past_external(x.m_v));
5382-
fetch_var(v);
5410+
fetch_var(v, is_external);
53835411
}
53845412

53855413
inline ASR::ttype_t* extract_ttype_t_from_expr(ASR::expr_t* expr) {

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6236,7 +6236,13 @@ Result<ASR::TranslationUnit_t*> python_ast_to_asr(Allocator &al, LocationManager
62366236
pass_options.run_fun = "_lpython_main_program";
62376237
pass_options.runtime_library_dir = get_runtime_library_dir();
62386238
pass_wrap_global_stmts_into_program(al, *tu, pass_options);
6239-
LCOMPILERS_ASSERT(asr_verify(*tu, true, diagnostics));
6239+
#if defined(WITH_LFORTRAN_ASSERT)
6240+
diag::Diagnostics diagnostics;
6241+
if (!asr_verify(*tu, true, diagnostics)) {
6242+
std::cerr << diagnostics.render2();
6243+
throw LCompilersException("Verify failed");
6244+
};
6245+
#endif
62406246
}
62416247
}
62426248

0 commit comments

Comments
 (0)