Skip to content

Commit

Permalink
Respect --disable-main in global_stmts pass
Browse files Browse the repository at this point in the history
  • Loading branch information
czgdp1807 committed Apr 21, 2023
1 parent 5148356 commit c215565
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 11 deletions.
7 changes: 6 additions & 1 deletion run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def is_included(backend):
wat = is_included("wat")
run = is_included("run")
run_with_dbg = is_included("run_with_dbg")
disable_main = is_included("disable_main")
pass_ = test.get("pass", None)
optimization_passes = ["flip_sign", "div_to_mul", "fma", "sign_from_value",
"inline_function_calls", "loop_unroll",
Expand Down Expand Up @@ -111,7 +112,11 @@ def is_included(backend):
filename, update_reference, extra_args)

if c:
run_test(filename, "c", "lpython --no-color --show-c {infile}",
if disable_main:
run_test(filename, "c", "lpython --no-color --disable-main --show-c {infile}",
filename, update_reference, extra_args)
else:
run_test(filename, "c", "lpython --no-color --show-c {infile}",
filename, update_reference, extra_args)
if wat:
run_test(filename, "wat", "lpython --no-color --show-wat {infile}",
Expand Down
22 changes: 12 additions & 10 deletions src/libasr/pass/global_stmts_program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void pass_wrap_global_stmts_into_program(Allocator &al,
bool call_main_program = unit.n_items > 0;
pass_wrap_global_stmts_into_function(al, unit, pass_options);
pass_wrap_global_syms_into_module(al, unit, pass_options);
if( call_main_program ) {
if( call_main_program && !pass_options.disable_main ) {
// Call `_lpython_main_program` function
ASR::Module_t *mod = ASR::down_cast<ASR::Module_t>(
unit.m_global_scope->get_symbol("_global_symbols"));
Expand All @@ -55,15 +55,17 @@ void pass_wrap_global_stmts_into_program(Allocator &al,
prog_dep.push_back(al, s2c(al, "_global_symbols"));
}

ASR::asr_t *prog = ASR::make_Program_t(
al, unit.base.base.loc,
/* a_symtab */ current_scope,
/* a_name */ s2c(al, prog_name),
prog_dep.p,
prog_dep.n,
/* a_body */ prog_body.p,
/* n_body */ prog_body.n);
unit.m_global_scope->add_symbol(prog_name, ASR::down_cast<ASR::symbol_t>(prog));
if( !pass_options.disable_main ) {
ASR::asr_t *prog = ASR::make_Program_t(
al, unit.base.base.loc,
/* a_symtab */ current_scope,
/* a_name */ s2c(al, prog_name),
prog_dep.p,
prog_dep.n,
/* a_body */ prog_body.p,
/* n_body */ prog_body.n);
unit.m_global_scope->add_symbol(prog_name, ASR::down_cast<ASR::symbol_t>(prog));
}
}

} // namespace LCompilers
1 change: 1 addition & 0 deletions src/libasr/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ namespace LCompilers {
bool fast = false; // is fast flag enabled.
bool verbose = false; // For developer debugging
bool pass_cumulative = false; // Apply passes cumulatively
bool disable_main = false;
};

}
Expand Down
1 change: 1 addition & 0 deletions src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6728,6 +6728,7 @@ Result<ASR::TranslationUnit_t*> python_ast_to_asr(Allocator &al, LocationManager
// If it is a main module, turn it into a program
// Note: we can modify this behavior for interactive mode later
LCompilers::PassOptions pass_options;
pass_options.disable_main = compiler_options.disable_main;
if (compiler_options.disable_main) {
if (tu->n_items > 0) {
diagnostics.add(diag::Diagnostic(
Expand Down
13 changes: 13 additions & 0 deletions tests/reference/c-import_order_01-3ebf3c3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"basename": "c-import_order_01-3ebf3c3",
"cmd": "lpython --no-color --disable-main --show-c {infile}",
"infile": "tests/../integration_tests/import_order_01.py",
"infile_hash": "b6f67090973a5fce0778dbdd86fdb12272a8da5bbae2f50051df34e5",
"outfile": null,
"outfile_hash": null,
"stdout": "c-import_order_01-3ebf3c3.stdout",
"stdout_hash": "d844d0d693ba8a9867f29b17de6f66c99f2096b7a14efeb4c5d698d2",
"stderr": null,
"stderr_hash": null,
"returncode": 0
}
52 changes: 52 additions & 0 deletions tests/reference/c-import_order_01-3ebf3c3.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <inttypes.h>

#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <lfortran_intrinsics.h>

#define ASSERT(cond) \
{ \
if (!(cond)) { \
printf("%s%s", "ASSERT failed: ", __FILE__); \
printf("%s%s", "\nfunction ", __func__); \
printf("%s%d%s", "(), line number ", __LINE__, " at \n"); \
printf("%s%s", #cond, "\n"); \
exit(1); \
} \
}
#define ASSERT_MSG(cond, msg) \
{ \
if (!(cond)) { \
printf("%s%s", "ASSERT failed: ", __FILE__); \
printf("%s%s", "\nfunction ", __func__); \
printf("%s%d%s", "(), line number ", __LINE__, " at \n"); \
printf("%s%s", #cond, "\n"); \
printf("%s", "ERROR MESSAGE:\n"); \
printf("%s%s", msg, "\n"); \
exit(1); \
} \
}


struct dimension_descriptor
{
int32_t lower_bound, length;
};

// Implementations
int32_t f()
{
int32_t _lpython_return_variable;
_lpython_return_variable = 42;
return _lpython_return_variable;
}

void main1()
{
int32_t a;
a = f();
printf("%d\n", a);
}

5 changes: 5 additions & 0 deletions tests/tests.toml
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ asr = true
filename = "../integration_tests/test_import_02.py"
c = true

[[test]]
filename = "../integration_tests/import_order_01.py"
c = true
disable_main = true

[[test]]
filename = "../integration_tests/vec_01.py"
asr = true
Expand Down

0 comments on commit c215565

Please sign in to comment.