Skip to content

Commit 5a6e2d2

Browse files
Merge pull request #1584 from Thirumalai-Shaktivel/global_syms_02
2 parents dd5a8f9 + e74ee9f commit 5a6e2d2

File tree

6 files changed

+70
-11
lines changed

6 files changed

+70
-11
lines changed

integration_tests/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -436,3 +436,4 @@ RUN(NAME bit_operations_i64 LABELS cpython llvm c wasm)
436436
RUN(NAME test_argv_01 LABELS llvm) # TODO: Test using CPython
437437
RUN(NAME global_syms_01 LABELS cpython llvm c)
438438
RUN(NAME global_syms_02 LABELS cpython llvm c)
439+
RUN(NAME global_syms_03_b LABELS cpython llvm c)

integration_tests/global_syms_03_a.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from ltypes import i32
2+
3+
l_1: list[str] = ['Monday', 'Tuesday', 'Wednesday']
4+
5+
def populate_lists() -> list[i32]:
6+
return [10, -20]
7+
l_2: list[i32] = populate_lists()

integration_tests/global_syms_03_b.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from global_syms_03_a import l_1, l_2
2+
3+
assert len(l_1) == 3
4+
assert l_1[1] == "Tuesday"
5+
assert l_2[1] == -20

src/libasr/asr_scopes.cpp

+47-2
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,12 @@ std::string SymbolTable::get_unique_name(const std::string &name) {
137137

138138
void SymbolTable::move_symbols_from_global_scope(Allocator &al,
139139
SymbolTable *module_scope, Vec<char *> &syms,
140-
Vec<char *> &mod_dependencies, Vec<ASR::stmt_t*> &var_init) {
140+
Vec<char *> &mod_dependencies, Vec<char *> &func_dependencies,
141+
Vec<ASR::stmt_t*> &var_init) {
141142
// TODO: This isn't scalable. We have write a visitor in asdl_cpp.py
142143
syms.reserve(al, 4);
143144
mod_dependencies.reserve(al, 4);
145+
func_dependencies.reserve(al, 4);
144146
var_init.reserve(al, 4);
145147
for (auto &a : scope) {
146148
switch (a.second->type) {
@@ -202,6 +204,49 @@ void SymbolTable::move_symbols_from_global_scope(Allocator &al,
202204
mod_dependencies.push_back(al, es->m_module_name);
203205
}
204206
es->m_parent_symtab = module_scope;
207+
ASR::symbol_t *s = ASRUtils::symbol_get_past_external(a.second);
208+
LCOMPILERS_ASSERT(s);
209+
if (ASR::is_a<ASR::Variable_t>(*s)) {
210+
ASR::Variable_t *v = ASR::down_cast<ASR::Variable_t>(s);
211+
if (v->m_symbolic_value && !ASR::is_a<ASR::Const_t>(*v->m_type)
212+
&& ASR::is_a<ASR::List_t>(*v->m_type)) {
213+
ASR::expr_t* target = ASRUtils::EXPR(ASR::make_Var_t(
214+
al, v->base.base.loc, (ASR::symbol_t *) es));
215+
ASR::expr_t *value = v->m_symbolic_value;
216+
v->m_symbolic_value = nullptr;
217+
v->m_value = nullptr;
218+
if (ASR::is_a<ASR::FunctionCall_t>(*value)) {
219+
ASR::FunctionCall_t *call =
220+
ASR::down_cast<ASR::FunctionCall_t>(value);
221+
ASR::Module_t *m = ASRUtils::get_sym_module(s);
222+
ASR::symbol_t *func = m->m_symtab->get_symbol(
223+
ASRUtils::symbol_name(call->m_name));
224+
ASR::Function_t *f = ASR::down_cast<ASR::Function_t>(func);
225+
std::string func_name = std::string(m->m_name) +
226+
"@" + f->m_name;
227+
ASR::symbol_t *es_func;
228+
if (!module_scope->get_symbol(func_name)) {
229+
es_func = ASR::down_cast<ASR::symbol_t>(
230+
ASR::make_ExternalSymbol_t(al, f->base.base.loc,
231+
module_scope, s2c(al, func_name), func, m->m_name,
232+
nullptr, 0, s2c(al, f->m_name), ASR::accessType::Public));
233+
module_scope->add_symbol(func_name, es_func);
234+
if (!present(func_dependencies, s2c(al, func_name))) {
235+
func_dependencies.push_back(al, s2c(al,func_name));
236+
}
237+
} else {
238+
es_func = module_scope->get_symbol(func_name);
239+
}
240+
value = ASRUtils::EXPR(ASR::make_FunctionCall_t(al,
241+
call->base.base.loc, es_func, call->m_original_name,
242+
call->m_args, call->n_args, call->m_type,
243+
call->m_value, call->m_dt));
244+
}
245+
ASR::asr_t* assign = ASR::make_Assignment_t(al,
246+
v->base.base.loc, target, value, nullptr);
247+
var_init.push_back(al, ASRUtils::STMT(assign));
248+
}
249+
}
205250
module_scope->add_symbol(a.first, (ASR::symbol_t *) es);
206251
syms.push_back(al, s2c(al, a.first));
207252
break;
@@ -250,7 +295,7 @@ void SymbolTable::move_symbols_from_global_scope(Allocator &al,
250295
} default : {
251296
throw LCompilersException("Moving the symbol:`" + a.first +
252297
"` from global scope is not implemented yet");
253-
};
298+
}
254299
}
255300
}
256301
}

src/libasr/asr_scopes.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ struct SymbolTable {
8585

8686
void move_symbols_from_global_scope(Allocator &al,
8787
SymbolTable *module_scope, Vec<char *> &syms,
88-
Vec<char *> &mod_dependencies, Vec<ASR::stmt_t*> &var_init);
88+
Vec<char *> &mod_dependencies, Vec<char *> &func_dependencies,
89+
Vec<ASR::stmt_t*> &var_init);
8990
};
9091

9192
} // namespace LCompilers

src/libasr/pass/global_symbols.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@ void pass_wrap_global_syms_into_module(Allocator &al,
2222
SymbolTable *module_scope = al.make_new<SymbolTable>(unit.m_global_scope);
2323
Vec<char *> moved_symbols;
2424
Vec<char *> mod_dependencies;
25+
Vec<char *> func_dependencies;
2526
Vec<ASR::stmt_t*> var_init;
2627

2728
// Move all the symbols from global into the module scope
2829
unit.m_global_scope->move_symbols_from_global_scope(al, module_scope,
29-
moved_symbols, mod_dependencies, var_init);
30+
moved_symbols, mod_dependencies, func_dependencies, var_init);
3031

3132
// Erase the symbols that are moved into the module
3233
for (auto &sym: moved_symbols) {
@@ -39,20 +40,19 @@ void pass_wrap_global_syms_into_module(Allocator &al,
3940
for (size_t i = 0; i < f->n_body; i++) {
4041
var_init.push_back(al, f->m_body[i]);
4142
}
43+
for (size_t i = 0; i < f->n_dependencies; i++) {
44+
func_dependencies.push_back(al, f->m_dependencies[i]);
45+
}
4246
f->m_body = var_init.p;
4347
f->n_body = var_init.n;
48+
f->m_dependencies = func_dependencies.p;
49+
f->n_dependencies = func_dependencies.n;
4450
// Overwrites the function: `_lpython_main_program`
4551
module_scope->add_symbol(f->m_name, (ASR::symbol_t *) f);
4652
}
4753

48-
Vec<char *> m_dependencies;
49-
m_dependencies.reserve(al, mod_dependencies.size());
50-
for( auto &dep: mod_dependencies) {
51-
m_dependencies.push_back(al, dep);
52-
}
53-
5454
ASR::symbol_t *module = (ASR::symbol_t *) ASR::make_Module_t(al, loc,
55-
module_scope, module_name, m_dependencies.p, m_dependencies.n,
55+
module_scope, module_name, mod_dependencies.p, mod_dependencies.n,
5656
false, false);
5757
unit.m_global_scope->add_symbol(module_name, module);
5858
}

0 commit comments

Comments
 (0)