Skip to content

Commit eb7b62e

Browse files
authored
Merge pull request #1732 from czgdp1807/disable_main
Always insert global symbols to`` _global_symbols`` module
2 parents b28a440 + 1798e93 commit eb7b62e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+7671
-7351
lines changed

integration_tests/CMakeLists.txt

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,72 @@ macro(RUN)
203203
endif()
204204
endmacro(RUN)
205205

206+
# only compiles till object file
207+
# to make sure that the generated code is syntactically correct
208+
# but we cannot generate an executable due to --disable-main option enabled.
209+
macro(COMPILE)
210+
set(options FAIL)
211+
set(oneValueArgs NAME IMPORT_PATH)
212+
set(multiValueArgs LABELS EXTRAFILES)
213+
cmake_parse_arguments(COMPILE "${options}" "${oneValueArgs}"
214+
"${multiValueArgs}" ${ARGN} )
215+
set(name ${COMPILE_NAME})
216+
set(import_path ${COMPILE_IMPORT_PATH})
217+
if (NOT name)
218+
message(FATAL_ERROR "Must specify the NAME argument")
219+
endif()
220+
221+
if (${KIND} IN_LIST COMPILE_LABELS)
222+
if (KIND STREQUAL "llvm")
223+
if (import_path)
224+
add_custom_command(
225+
OUTPUT ${name}.o
226+
COMMAND lpython --disable-main -c -I ${CMAKE_CURRENT_SOURCE_DIR}/${import_path} ${CMAKE_CURRENT_SOURCE_DIR}/${name}.py -o ${name}.o
227+
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${name}.py
228+
VERBATIM)
229+
else ()
230+
add_custom_command(
231+
OUTPUT ${name}.o
232+
COMMAND lpython --disable-main -c ${CMAKE_CURRENT_SOURCE_DIR}/${name}.py -o ${name}.o
233+
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${name}.py
234+
VERBATIM)
235+
endif()
236+
237+
add_library(${name} OBJECT ${name}.o)
238+
elseif(KIND STREQUAL "c")
239+
if (import_path)
240+
add_custom_command(
241+
OUTPUT ${name}.c
242+
COMMAND lpython --disable-main -I ${CMAKE_CURRENT_SOURCE_DIR}/${import_path} --show-c ${CMAKE_CURRENT_SOURCE_DIR}/${name}.py > ${name}.c
243+
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${name}.py
244+
VERBATIM)
245+
else ()
246+
add_custom_command(
247+
OUTPUT ${name}.c
248+
COMMAND lpython --disable-main --show-c ${CMAKE_CURRENT_SOURCE_DIR}/${name}.py > ${name}.c
249+
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${name}.py
250+
VERBATIM)
251+
endif()
252+
253+
add_library(${name} OBJECT ${name}.c)
254+
target_link_libraries(${name} lpython_rtlib)
255+
elseif(KIND STREQUAL "cpython")
256+
# CPython test
257+
set(PY_MOD "")
258+
259+
add_test(${name} python ${CMAKE_CURRENT_SOURCE_DIR}/${name}.py)
260+
set_tests_properties(${name} PROPERTIES
261+
ENVIRONMENT "PYTHONPATH=${CMAKE_SOURCE_DIR}/../src/runtime/lpython:${CMAKE_SOURCE_DIR}/..;LPYTHON_PY_MOD_NAME=${PY_MOD};LPYTHON_PY_MOD_PATH=${CMAKE_CURRENT_BINARY_DIR}")
262+
if (RUN_LABELS)
263+
set_tests_properties(${name} PROPERTIES LABELS "${RUN_LABELS}")
264+
endif()
265+
if (${RUN_FAIL})
266+
set_tests_properties(${name} PROPERTIES WILL_FAIL TRUE)
267+
endif()
268+
endif()
269+
endif()
270+
endmacro(COMPILE)
271+
206272

207273
# Test zero and non-zero exit code and assert statements
208274
RUN(NAME array_01_decl LABELS cpython llvm c)
@@ -456,3 +522,5 @@ RUN(NAME global_syms_06 LABELS cpython llvm c)
456522

457523
# Intrinsic Functions
458524
RUN(NAME intrinsics_01 LABELS cpython llvm) # any
525+
526+
COMPILE(NAME import_order_01 LABELS cpython llvm c) # any

integration_tests/import_order_01.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from import_order_01b import f
2+
from lpython import i32, ccallback
3+
4+
@ccallback
5+
def main1():
6+
a: i32 = f()
7+
print(a)

integration_tests/import_order_01b.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from lpython import i32
2+
3+
def f() -> i32:
4+
return 42

integration_tests/variable_decl_03.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from lpython import f64
22

33
def f() -> f64:
4-
return 5.5
4+
return abs(-5.5)
55

66
def main():
77
t1: f64 = f() * 1e6

src/libasr/pass/global_stmts.cpp

Lines changed: 128 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -20,146 +20,148 @@ using ASRUtils::EXPR;
2020
*/
2121
void pass_wrap_global_stmts_into_function(Allocator &al,
2222
ASR::TranslationUnit_t &unit, const LCompilers::PassOptions& pass_options) {
23+
if (unit.n_items == 0) {
24+
return ;
25+
}
26+
2327
std::string fn_name_s = pass_options.run_fun;
24-
if (unit.n_items > 0) {
25-
// Add an anonymous function
26-
Str s;
27-
s.from_str_view(fn_name_s);
28-
char *fn_name = s.c_str(al);
29-
SymbolTable *fn_scope = al.make_new<SymbolTable>(unit.m_global_scope);
28+
// Add an anonymous function
29+
Str s;
30+
s.from_str_view(fn_name_s);
31+
char *fn_name = s.c_str(al);
32+
SymbolTable *fn_scope = al.make_new<SymbolTable>(unit.m_global_scope);
3033

31-
ASR::ttype_t *type;
32-
Location loc = unit.base.base.loc;
33-
ASR::asr_t *return_var=nullptr;
34-
ASR::expr_t *return_var_ref=nullptr;
35-
char *var_name;
36-
int idx = 1;
34+
ASR::ttype_t *type;
35+
Location loc = unit.base.base.loc;
36+
ASR::asr_t *return_var=nullptr;
37+
ASR::expr_t *return_var_ref=nullptr;
38+
char *var_name;
39+
int idx = 1;
3740

38-
Vec<ASR::stmt_t*> body;
39-
body.reserve(al, unit.n_items);
40-
for (size_t i=0; i<unit.n_items; i++) {
41-
if (unit.m_items[i]->type == ASR::asrType::expr) {
42-
ASR::expr_t *target;
43-
ASR::expr_t *value = EXPR(unit.m_items[i]);
44-
// Create a new variable with the right type
45-
if (ASRUtils::expr_type(value)->type == ASR::ttypeType::Integer) {
46-
s.from_str(al, fn_name_s + std::to_string(idx));
47-
var_name = s.c_str(al);
41+
Vec<ASR::stmt_t*> body;
42+
body.reserve(al, unit.n_items);
43+
for (size_t i=0; i<unit.n_items; i++) {
44+
if (unit.m_items[i]->type == ASR::asrType::expr) {
45+
ASR::expr_t *target;
46+
ASR::expr_t *value = EXPR(unit.m_items[i]);
47+
// Create a new variable with the right type
48+
if (ASRUtils::expr_type(value)->type == ASR::ttypeType::Integer) {
49+
s.from_str(al, fn_name_s + std::to_string(idx));
50+
var_name = s.c_str(al);
4851

49-
int a_kind = down_cast<ASR::Integer_t>(ASRUtils::expr_type(value))->m_kind;
52+
int a_kind = down_cast<ASR::Integer_t>(ASRUtils::expr_type(value))->m_kind;
5053

51-
type = ASRUtils::TYPE(ASR::make_Integer_t(al, loc, a_kind, nullptr, 0));
52-
return_var = ASR::make_Variable_t(al, loc,
53-
fn_scope, var_name, nullptr, 0, ASRUtils::intent_local, nullptr, nullptr,
54-
ASR::storage_typeType::Default, type,
55-
ASR::abiType::BindC,
56-
ASR::Public, ASR::presenceType::Required, false);
57-
return_var_ref = EXPR(ASR::make_Var_t(al, loc,
58-
down_cast<ASR::symbol_t>(return_var)));
59-
fn_scope->add_symbol(std::string(var_name), down_cast<ASR::symbol_t>(return_var));
60-
target = return_var_ref;
61-
idx++;
62-
} else if (ASRUtils::expr_type(value)->type == ASR::ttypeType::Real) {
63-
s.from_str(al, fn_name_s + std::to_string(idx));
64-
var_name = s.c_str(al);
65-
type = ASRUtils::expr_type(value);
66-
return_var = ASR::make_Variable_t(al, loc,
67-
fn_scope, var_name, nullptr, 0, ASRUtils::intent_local, nullptr, nullptr,
68-
ASR::storage_typeType::Default, type,
69-
ASR::abiType::BindC,
70-
ASR::Public, ASR::presenceType::Required, false);
71-
return_var_ref = EXPR(ASR::make_Var_t(al, loc,
72-
down_cast<ASR::symbol_t>(return_var)));
73-
fn_scope->add_symbol(std::string(var_name), down_cast<ASR::symbol_t>(return_var));
74-
target = return_var_ref;
75-
idx++;
76-
} else if (ASRUtils::expr_type(value)->type == ASR::ttypeType::Complex) {
77-
s.from_str(al, fn_name_s + std::to_string(idx));
78-
var_name = s.c_str(al);
79-
type = ASRUtils::expr_type(value);
80-
return_var = ASR::make_Variable_t(al, loc,
81-
fn_scope, var_name, nullptr, 0, ASRUtils::intent_local, nullptr, nullptr,
82-
ASR::storage_typeType::Default, type,
83-
ASR::abiType::BindC,
84-
ASR::Public, ASR::presenceType::Required, false);
85-
return_var_ref = EXPR(ASR::make_Var_t(al, loc,
86-
down_cast<ASR::symbol_t>(return_var)));
87-
fn_scope->add_symbol(std::string(var_name), down_cast<ASR::symbol_t>(return_var));
88-
target = return_var_ref;
89-
idx++;
90-
} else {
91-
throw LCompilersException("Return type not supported in interactive mode");
92-
}
93-
ASR::stmt_t* asr_stmt = ASRUtils::STMT(ASR::make_Assignment_t(al, loc, target, value, nullptr));
94-
body.push_back(al, asr_stmt);
95-
} else if (unit.m_items[i]->type == ASR::asrType::stmt) {
96-
ASR::stmt_t* asr_stmt = ASRUtils::STMT(unit.m_items[i]);
97-
body.push_back(al, asr_stmt);
98-
return_var = nullptr;
54+
type = ASRUtils::TYPE(ASR::make_Integer_t(al, loc, a_kind, nullptr, 0));
55+
return_var = ASR::make_Variable_t(al, loc,
56+
fn_scope, var_name, nullptr, 0, ASRUtils::intent_local, nullptr, nullptr,
57+
ASR::storage_typeType::Default, type,
58+
ASR::abiType::BindC,
59+
ASR::Public, ASR::presenceType::Required, false);
60+
return_var_ref = EXPR(ASR::make_Var_t(al, loc,
61+
down_cast<ASR::symbol_t>(return_var)));
62+
fn_scope->add_symbol(std::string(var_name), down_cast<ASR::symbol_t>(return_var));
63+
target = return_var_ref;
64+
idx++;
65+
} else if (ASRUtils::expr_type(value)->type == ASR::ttypeType::Real) {
66+
s.from_str(al, fn_name_s + std::to_string(idx));
67+
var_name = s.c_str(al);
68+
type = ASRUtils::expr_type(value);
69+
return_var = ASR::make_Variable_t(al, loc,
70+
fn_scope, var_name, nullptr, 0, ASRUtils::intent_local, nullptr, nullptr,
71+
ASR::storage_typeType::Default, type,
72+
ASR::abiType::BindC,
73+
ASR::Public, ASR::presenceType::Required, false);
74+
return_var_ref = EXPR(ASR::make_Var_t(al, loc,
75+
down_cast<ASR::symbol_t>(return_var)));
76+
fn_scope->add_symbol(std::string(var_name), down_cast<ASR::symbol_t>(return_var));
77+
target = return_var_ref;
78+
idx++;
79+
} else if (ASRUtils::expr_type(value)->type == ASR::ttypeType::Complex) {
80+
s.from_str(al, fn_name_s + std::to_string(idx));
81+
var_name = s.c_str(al);
82+
type = ASRUtils::expr_type(value);
83+
return_var = ASR::make_Variable_t(al, loc,
84+
fn_scope, var_name, nullptr, 0, ASRUtils::intent_local, nullptr, nullptr,
85+
ASR::storage_typeType::Default, type,
86+
ASR::abiType::BindC,
87+
ASR::Public, ASR::presenceType::Required, false);
88+
return_var_ref = EXPR(ASR::make_Var_t(al, loc,
89+
down_cast<ASR::symbol_t>(return_var)));
90+
fn_scope->add_symbol(std::string(var_name), down_cast<ASR::symbol_t>(return_var));
91+
target = return_var_ref;
92+
idx++;
9993
} else {
100-
throw LCompilersException("Unsupported type of global scope node");
94+
throw LCompilersException("Return type not supported in interactive mode");
10195
}
96+
ASR::stmt_t* asr_stmt = ASRUtils::STMT(ASR::make_Assignment_t(al, loc, target, value, nullptr));
97+
body.push_back(al, asr_stmt);
98+
} else if (unit.m_items[i]->type == ASR::asrType::stmt) {
99+
ASR::stmt_t* asr_stmt = ASRUtils::STMT(unit.m_items[i]);
100+
body.push_back(al, asr_stmt);
101+
return_var = nullptr;
102+
} else {
103+
throw LCompilersException("Unsupported type of global scope node");
102104
}
105+
}
103106

104-
if (return_var) {
105-
// The last item was an expression, create a function returning it
107+
if (return_var) {
108+
// The last item was an expression, create a function returning it
106109

107-
// The last defined `return_var` is the actual return value
108-
ASR::down_cast2<ASR::Variable_t>(return_var)->m_intent = ASRUtils::intent_return_var;
110+
// The last defined `return_var` is the actual return value
111+
ASR::down_cast2<ASR::Variable_t>(return_var)->m_intent = ASRUtils::intent_return_var;
109112

110113

111-
ASR::asr_t *fn = ASRUtils::make_Function_t_util(
112-
al, loc,
113-
/* a_symtab */ fn_scope,
114-
/* a_name */ fn_name,
115-
nullptr, 0,
116-
/* a_args */ nullptr,
117-
/* n_args */ 0,
118-
/* a_body */ body.p,
119-
/* n_body */ body.size(),
120-
/* a_return_var */ return_var_ref,
121-
ASR::abiType::BindC,
122-
ASR::Public, ASR::Implementation,
123-
nullptr,
124-
false, false, false, false, false,
125-
nullptr, 0,
126-
nullptr, 0,
127-
false, false, false);
128-
std::string sym_name = fn_name;
129-
if (unit.m_global_scope->get_symbol(sym_name) != nullptr) {
130-
throw LCompilersException("Function already defined");
131-
}
132-
unit.m_global_scope->add_symbol(sym_name, down_cast<ASR::symbol_t>(fn));
133-
} else {
134-
// The last item was a statement, create a subroutine (returning
135-
// nothing)
136-
ASR::asr_t *fn = ASRUtils::make_Function_t_util(
137-
al, loc,
138-
/* a_symtab */ fn_scope,
139-
/* a_name */ fn_name,
140-
nullptr, 0,
141-
/* a_args */ nullptr,
142-
/* n_args */ 0,
143-
/* a_body */ body.p,
144-
/* n_body */ body.size(),
145-
nullptr,
146-
ASR::abiType::Source,
147-
ASR::Public, ASR::Implementation, nullptr,
148-
false, false, false, false, false,
149-
nullptr, 0,
150-
nullptr, 0,
151-
false, false, false);
152-
std::string sym_name = fn_name;
153-
if (unit.m_global_scope->get_symbol(sym_name) != nullptr) {
154-
throw LCompilersException("Function already defined");
155-
}
156-
unit.m_global_scope->add_symbol(sym_name, down_cast<ASR::symbol_t>(fn));
114+
ASR::asr_t *fn = ASRUtils::make_Function_t_util(
115+
al, loc,
116+
/* a_symtab */ fn_scope,
117+
/* a_name */ fn_name,
118+
nullptr, 0,
119+
/* a_args */ nullptr,
120+
/* n_args */ 0,
121+
/* a_body */ body.p,
122+
/* n_body */ body.size(),
123+
/* a_return_var */ return_var_ref,
124+
ASR::abiType::BindC,
125+
ASR::Public, ASR::Implementation,
126+
nullptr,
127+
false, false, false, false, false,
128+
nullptr, 0,
129+
nullptr, 0,
130+
false, false, false);
131+
std::string sym_name = fn_name;
132+
if (unit.m_global_scope->get_symbol(sym_name) != nullptr) {
133+
throw LCompilersException("Function already defined");
134+
}
135+
unit.m_global_scope->add_symbol(sym_name, down_cast<ASR::symbol_t>(fn));
136+
} else {
137+
// The last item was a statement, create a subroutine (returning
138+
// nothing)
139+
ASR::asr_t *fn = ASRUtils::make_Function_t_util(
140+
al, loc,
141+
/* a_symtab */ fn_scope,
142+
/* a_name */ fn_name,
143+
nullptr, 0,
144+
/* a_args */ nullptr,
145+
/* n_args */ 0,
146+
/* a_body */ body.p,
147+
/* n_body */ body.size(),
148+
nullptr,
149+
ASR::abiType::Source,
150+
ASR::Public, ASR::Implementation, nullptr,
151+
false, false, false, false, false,
152+
nullptr, 0,
153+
nullptr, 0,
154+
false, false, false);
155+
std::string sym_name = fn_name;
156+
if (unit.m_global_scope->get_symbol(sym_name) != nullptr) {
157+
throw LCompilersException("Function already defined");
157158
}
158-
unit.m_items = nullptr;
159-
unit.n_items = 0;
160-
PassUtils::UpdateDependenciesVisitor v(al);
161-
v.visit_TranslationUnit(unit);
159+
unit.m_global_scope->add_symbol(sym_name, down_cast<ASR::symbol_t>(fn));
162160
}
161+
unit.m_items = nullptr;
162+
unit.n_items = 0;
163+
PassUtils::UpdateDependenciesVisitor v(al);
164+
v.visit_TranslationUnit(unit);
163165
}
164166

165167
} // namespace LCompilers

0 commit comments

Comments
 (0)