Skip to content

Commit a8606b7

Browse files
combine global_init and global_stmts
1 parent e378b4e commit a8606b7

File tree

2 files changed

+14
-49
lines changed

2 files changed

+14
-49
lines changed

src/bin/lpython.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -900,19 +900,12 @@ int compile_python_using_llvm(
900900

901901
auto llvm_start = std::chrono::high_resolution_clock::now();
902902

903-
bool call_init = false;
904903
bool call_stmts = false;
905-
if (m->get_return_type("__module___main_____main__global_init") == "void") {
906-
call_init = true;
907-
}
908904
if (m->get_return_type("__module___main_____main__global_stmts") == "void") {
909905
call_stmts = true;
910906
}
911907

912908
e.add_module(std::move(m));
913-
if (call_init) {
914-
e.voidfn("__module___main_____main__global_init");
915-
}
916909
if (call_stmts) {
917910
e.voidfn("__module___main_____main__global_stmts");
918911
}

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 14 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -290,11 +290,11 @@ ASR::Module_t* load_module(Allocator &al, SymbolTable *symtab,
290290

291291
// Here, we call the global_initializer & global_statements to
292292
// initialize and execute the global symbols
293-
void get_calls_to_global_init_and_stmts(Allocator &al, const Location &loc, SymbolTable* scope,
293+
void get_calls_to_global_stmts(Allocator &al, const Location &loc, SymbolTable* scope,
294294
ASR::Module_t* mod, std::vector<ASR::asr_t *> &tmp_vec) {
295295

296296
std::string mod_name = mod->m_name;
297-
std::string g_func_name = mod_name + "global_init";
297+
std::string g_func_name = mod_name + "global_stmts";
298298
ASR::symbol_t *g_func = mod->m_symtab->get_symbol(g_func_name);
299299
if (g_func && !scope->get_symbol(g_func_name)) {
300300
ASR::symbol_t *es = ASR::down_cast<ASR::symbol_t>(
@@ -306,19 +306,6 @@ void get_calls_to_global_init_and_stmts(Allocator &al, const Location &loc, Symb
306306
tmp_vec.push_back(ASRUtils::make_SubroutineCall_t_util(al, loc,
307307
es, g_func, nullptr, 0, nullptr, nullptr, false, false));
308308
}
309-
310-
g_func_name = mod_name + "global_stmts";
311-
g_func = mod->m_symtab->get_symbol(g_func_name);
312-
if (g_func && !scope->get_symbol(g_func_name)) {
313-
ASR::symbol_t *es = ASR::down_cast<ASR::symbol_t>(
314-
ASR::make_ExternalSymbol_t(al, mod->base.base.loc,
315-
scope, s2c(al, g_func_name), g_func,
316-
s2c(al, mod_name), nullptr, 0, s2c(al, g_func_name),
317-
ASR::accessType::Public));
318-
scope->add_symbol(g_func_name, es);
319-
tmp_vec.push_back(ASRUtils::make_SubroutineCall_t_util(al, loc,
320-
es, g_func, nullptr, 0, nullptr, nullptr, false, false));
321-
}
322309
}
323310

324311
template <class Struct>
@@ -4888,6 +4875,14 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
48884875
tmp = nullptr;
48894876
tmp_vec.clear();
48904877
visit_stmt(*x.m_body[i]);
4878+
if (!global_init.empty()) {
4879+
for (auto t: global_init) {
4880+
if (t) {
4881+
items.push_back(al, t);
4882+
global_init.erase(t);
4883+
}
4884+
}
4885+
}
48914886
if (tmp) {
48924887
items.push_back(al, tmp);
48934888
} else if (!tmp_vec.empty()) {
@@ -4905,30 +4900,7 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
49054900
mod->m_dependencies = current_module_dependencies.p;
49064901
mod->n_dependencies = current_module_dependencies.n;
49074902

4908-
if (global_init.n > 0) {
4909-
// unit->m_items is used and set to nullptr in the
4910-
// `pass_wrap_global_stmts_into_function` pass
4911-
unit->m_items = global_init.p;
4912-
unit->n_items = global_init.size();
4913-
std::string func_name = module_name + "global_init";
4914-
LCompilers::PassOptions pass_options;
4915-
pass_options.run_fun = func_name;
4916-
pass_wrap_global_stmts(al, *unit, pass_options);
4917-
4918-
ASR::symbol_t *f_sym = unit->m_symtab->get_symbol(func_name);
4919-
if (f_sym) {
4920-
// Add the `global_initilaizer` function into the
4921-
// module and later call this function to initialize the
4922-
// global variables like list, ...
4923-
ASR::Function_t *f = ASR::down_cast<ASR::Function_t>(f_sym);
4924-
f->m_symtab->parent = mod->m_symtab;
4925-
mod->m_symtab->add_symbol(func_name, (ASR::symbol_t *) f);
4926-
// Erase the function in TranslationUnit
4927-
unit->m_symtab->erase_symbol(func_name);
4928-
}
4929-
global_init.p = nullptr;
4930-
global_init.n = 0;
4931-
}
4903+
LCOMPILERS_ASSERT(global_init.empty())
49324904

49334905
if (items.n > 0) {
49344906
unit->m_items = items.p;
@@ -5012,7 +4984,7 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
50124984
ASR::symbol_t *mod_sym = current_scope->resolve_symbol(mod_name);
50134985
if (mod_sym) {
50144986
ASR::Module_t *mod = ASR::down_cast<ASR::Module_t>(mod_sym);
5015-
get_calls_to_global_init_and_stmts(al, x.base.base.loc, current_scope, mod, tmp_vec);
4987+
get_calls_to_global_stmts(al, x.base.base.loc, current_scope, mod, tmp_vec);
50164988
}
50174989
}
50184990
}
@@ -5031,7 +5003,7 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
50315003
ASR::symbol_t *mod_sym = current_scope->resolve_symbol(mod_name);
50325004
if (mod_sym) {
50335005
ASR::Module_t *mod = ASR::down_cast<ASR::Module_t>(mod_sym);
5034-
get_calls_to_global_init_and_stmts(al, x.base.base.loc, current_scope, mod, tmp_vec);
5006+
get_calls_to_global_stmts(al, x.base.base.loc, current_scope, mod, tmp_vec);
50355007
}
50365008
tmp = nullptr;
50375009
}
@@ -8444,7 +8416,7 @@ Result<ASR::TranslationUnit_t*> python_ast_to_asr(Allocator &al, LocationManager
84448416
ASR::Module_t *mod = ASR::down_cast<ASR::Module_t>(mod_sym);
84458417
LCOMPILERS_ASSERT(mod);
84468418
std::vector<ASR::asr_t*> tmp_vec;
8447-
get_calls_to_global_init_and_stmts(al, tu->base.base.loc, program_scope, mod, tmp_vec);
8419+
get_calls_to_global_stmts(al, tu->base.base.loc, program_scope, mod, tmp_vec);
84488420

84498421
for (auto i:tmp_vec) {
84508422
prog_body.push_back(al, ASRUtils::STMT(i));

0 commit comments

Comments
 (0)