Skip to content

Commit f8b720c

Browse files
authored
Merge pull request #1617 from Thirumalai-Shaktivel/global_syms_03
Import Global statements
2 parents 15ab2bf + 7befb71 commit f8b720c

File tree

8 files changed

+161
-111
lines changed

8 files changed

+161
-111
lines changed

integration_tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,4 +441,5 @@ RUN(NAME test_argv_01 LABELS llvm) # TODO: Test using CPython
441441
RUN(NAME global_syms_01 LABELS cpython llvm c)
442442
RUN(NAME global_syms_02 LABELS cpython llvm c)
443443
RUN(NAME global_syms_03_b LABELS cpython llvm c)
444+
RUN(NAME global_syms_03_c LABELS cpython llvm c)
444445
RUN(NAME global_syms_04 LABELS cpython llvm c wasm wasm_x64)

integration_tests/global_syms_03_a.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
from lpython import i32
1+
from lpython import i32, f64
2+
3+
print("Imported from global_syms_03_a")
24

35
l_1: list[str] = ['Monday', 'Tuesday', 'Wednesday']
6+
l_1.append('Thursday')
47

58
def populate_lists() -> list[i32]:
69
return [10, -20]
710
l_2: list[i32] = populate_lists()
11+
12+
l_3: list[f64]
13+
l_3 = [1.0, 2.0, 3.0]

integration_tests/global_syms_03_b.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from global_syms_03_a import l_1, l_2
22

3-
assert len(l_1) == 3
3+
assert len(l_1) == 4
44
assert l_1[1] == "Tuesday"
55
assert l_2[1] == -20

integration_tests/global_syms_03_c.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import global_syms_03_a
2+
3+
assert len(global_syms_03_a.l_1) == 4
4+
assert global_syms_03_a.l_1[3] == "Thursday"
5+
assert global_syms_03_a.l_3 == [1.0, 2.0, 3.0]

src/libasr/asr_scopes.cpp

Lines changed: 1 addition & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,10 @@ 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<char *> &func_dependencies,
141-
Vec<ASR::stmt_t*> &var_init) {
140+
Vec<char *> &mod_dependencies) {
142141
// TODO: This isn't scalable. We have write a visitor in asdl_cpp.py
143142
syms.reserve(al, 4);
144143
mod_dependencies.reserve(al, 4);
145-
func_dependencies.reserve(al, 4);
146-
var_init.reserve(al, 4);
147144
for (auto &a : scope) {
148145
switch (a.second->type) {
149146
case (ASR::symbolType::Module): {
@@ -206,47 +203,6 @@ void SymbolTable::move_symbols_from_global_scope(Allocator &al,
206203
es->m_parent_symtab = module_scope;
207204
ASR::symbol_t *s = ASRUtils::symbol_get_past_external(a.second);
208205
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-
}
250206
module_scope->add_symbol(a.first, (ASR::symbol_t *) es);
251207
syms.push_back(al, s2c(al, a.first));
252208
break;
@@ -271,24 +227,6 @@ void SymbolTable::move_symbols_from_global_scope(Allocator &al,
271227
} case (ASR::symbolType::Variable) : {
272228
ASR::Variable_t *v = ASR::down_cast<ASR::Variable_t>(a.second);
273229
v->m_parent_symtab = module_scope;
274-
// Make the Assignment statement only for the data-types (List,
275-
// Dict, ...), that cannot be handled in the LLVM global scope
276-
if (v->m_symbolic_value && !ASR::is_a<ASR::Const_t>(*v->m_type)
277-
&& ASR::is_a<ASR::List_t>(*v->m_type)) {
278-
ASR::expr_t* v_expr = ASRUtils::EXPR(ASR::make_Var_t(
279-
al, v->base.base.loc, (ASR::symbol_t *) v));
280-
ASR::asr_t* assign = ASR::make_Assignment_t(al,
281-
v->base.base.loc, v_expr, v->m_symbolic_value, nullptr);
282-
var_init.push_back(al, ASRUtils::STMT(assign));
283-
v->m_symbolic_value = nullptr;
284-
v->m_value = nullptr;
285-
Vec<char*> v_dependencies;
286-
v_dependencies.reserve(al, 1);
287-
ASRUtils::collect_variable_dependencies(al,
288-
v_dependencies, v->m_type);
289-
v->m_dependencies = v_dependencies.p;
290-
v->n_dependencies = v_dependencies.size();
291-
}
292230
module_scope->add_symbol(a.first, (ASR::symbol_t *) v);
293231
syms.push_back(al, s2c(al, a.first));
294232
break;

src/libasr/asr_scopes.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,7 @@ 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<char *> &func_dependencies,
89-
Vec<ASR::stmt_t*> &var_init);
88+
Vec<char *> &mod_dependencies);
9089
};
9190

9291
} // namespace LCompilers

src/libasr/pass/global_symbols.cpp

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,35 +22,16 @@ 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;
26-
Vec<ASR::stmt_t*> var_init;
2725

2826
// Move all the symbols from global into the module scope
2927
unit.m_global_scope->move_symbols_from_global_scope(al, module_scope,
30-
moved_symbols, mod_dependencies, func_dependencies, var_init);
28+
moved_symbols, mod_dependencies);
3129

3230
// Erase the symbols that are moved into the module
3331
for (auto &sym: moved_symbols) {
3432
unit.m_global_scope->erase_symbol(sym);
3533
}
3634

37-
if (module_scope->get_symbol(pass_options.run_fun) && var_init.n > 0) {
38-
ASR::Function_t *f = ASR::down_cast<ASR::Function_t>(
39-
module_scope->get_symbol(pass_options.run_fun));
40-
for (size_t i = 0; i < f->n_body; i++) {
41-
var_init.push_back(al, f->m_body[i]);
42-
}
43-
for (size_t i = 0; i < f->n_dependencies; i++) {
44-
func_dependencies.push_back(al, f->m_dependencies[i]);
45-
}
46-
f->m_body = var_init.p;
47-
f->n_body = var_init.n;
48-
f->m_dependencies = func_dependencies.p;
49-
f->n_dependencies = func_dependencies.n;
50-
// Overwrites the function: `_lpython_main_program`
51-
module_scope->add_symbol(f->m_name, (ASR::symbol_t *) f);
52-
}
53-
5435
ASR::symbol_t *module = (ASR::symbol_t *) ASR::make_Module_t(al, loc,
5536
module_scope, module_name, mod_dependencies.p, mod_dependencies.n,
5637
false, false);

0 commit comments

Comments
 (0)