Skip to content

Commit

Permalink
Merge pull request lcompilers#638 from certik/no_main2
Browse files Browse the repository at this point in the history
Implement --disable-main
  • Loading branch information
certik authored Jun 19, 2022
2 parents 34d3004 + 7fedc31 commit ecead95
Show file tree
Hide file tree
Showing 14 changed files with 55 additions and 24 deletions.
11 changes: 6 additions & 5 deletions src/bin/lpython.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ int emit_asr(const std::string &infile,
diagnostics.diagnostics.clear();
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
r = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, true,
compiler_options.symtab_only);
compiler_options.disable_main, compiler_options.symtab_only);
std::cerr << diagnostics.render(input, lm, compiler_options);
if (!r.ok) {
LFORTRAN_ASSERT(diagnostics.has_error())
Expand Down Expand Up @@ -205,7 +205,7 @@ int emit_cpp(const std::string &infile,
diagnostics.diagnostics.clear();
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, true,
compiler_options.symtab_only);
compiler_options.disable_main, compiler_options.symtab_only);
std::cerr << diagnostics.render(input, lm, compiler_options);
if (!r1.ok) {
LFORTRAN_ASSERT(diagnostics.has_error())
Expand Down Expand Up @@ -245,7 +245,7 @@ int emit_c(const std::string &infile,
diagnostics.diagnostics.clear();
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, true,
compiler_options.symtab_only);
compiler_options.disable_main, compiler_options.symtab_only);
std::cerr << diagnostics.render(input, lm, compiler_options);
if (!r1.ok) {
LFORTRAN_ASSERT(diagnostics.has_error())
Expand Down Expand Up @@ -288,7 +288,7 @@ int emit_llvm(const std::string &infile,
diagnostics.diagnostics.clear();
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, true,
compiler_options.symtab_only);
compiler_options.disable_main, compiler_options.symtab_only);
std::cerr << diagnostics.render(input, lm, compiler_options);
if (!r1.ok) {
LFORTRAN_ASSERT(diagnostics.has_error())
Expand Down Expand Up @@ -352,7 +352,7 @@ int compile_python_to_object_file(
auto ast_to_asr_start = std::chrono::high_resolution_clock::now();
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, true,
compiler_options.symtab_only);
compiler_options.disable_main, compiler_options.symtab_only);
auto ast_to_asr_end = std::chrono::high_resolution_clock::now();
times.push_back(std::make_pair("AST to ASR", std::chrono::duration<double, std::milli>(ast_to_asr_end - ast_to_asr_start).count()));
std::cerr << diagnostics.render(input, lm, compiler_options);
Expand Down Expand Up @@ -645,6 +645,7 @@ int main(int argc, char *argv[])
app.add_flag("--indent", compiler_options.indent, "Indented print ASR/AST");
app.add_flag("--tree", compiler_options.tree, "Tree structure print ASR/AST");
app.add_option("--pass", arg_pass, "Apply the ASR pass and show ASR (implies --show-asr)");
app.add_flag("--disable-main", compiler_options.disable_main, "Do not generate any code for the `main` function");
app.add_flag("--symtab-only", compiler_options.symtab_only, "Only create symbol tables in ASR (skip executable stmt)");
app.add_flag("--time-report", time_report, "Show compilation time report");
app.add_flag("--static", static_link, "Create a static executable");
Expand Down
2 changes: 1 addition & 1 deletion src/libasr/codegen/asr_to_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ R"(
Result<std::string> asr_to_c(Allocator &al, ASR::TranslationUnit_t &asr,
diag::Diagnostics &diagnostics)
{
pass_unused_functions(al, asr);
pass_unused_functions(al, asr, true);
pass_replace_class_constructor(al, asr);
ASRToCVisitor v(diagnostics);
try {
Expand Down
16 changes: 14 additions & 2 deletions src/libasr/codegen/asr_to_c_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,9 @@ R"(#include <stdio.h>
} else if (ASR::is_a<ASR::CPtr_t>(*return_var->m_type)) {
sub = "void* ";
} else {
throw CodeGenError("Return type not supported", return_var->base.base.loc);
throw CodeGenError("Return type not supported in function '" +
std::string(x.m_name) +
+ "'", return_var->base.base.loc);
}
std::string sym_name = x.m_name;
if (sym_name == "main") {
Expand Down Expand Up @@ -654,11 +656,21 @@ R"(#include <stdio.h>
}
break;
}
case (ASR::cast_kindType::RealToComplex) : {
if (is_c) {
headers.insert("complex");
src = "CMPLX(" + src + ", 0.0)";
} else {
src = "std::complex<double>(" + src + ")";
}
break;
}
case (ASR::cast_kindType::LogicalToInteger) : {
src = "(int)(" + src + ")";
break;
}
default : throw CodeGenError("Cast kind " + std::to_string(x.m_kind) + " not implemented");
default : throw CodeGenError("Cast kind " + std::to_string(x.m_kind) + " not implemented",
x.base.base.loc);
}
last_expr_precedence = 2;
}
Expand Down
2 changes: 1 addition & 1 deletion src/libasr/codegen/asr_to_cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ Kokkos::View<T*> from_std_vector(const std::vector<T> &v)
Result<std::string> asr_to_cpp(Allocator &al, ASR::TranslationUnit_t &asr,
diag::Diagnostics &diagnostics)
{
pass_unused_functions(al, asr);
pass_unused_functions(al, asr, true);
ASRToCPPVisitor v(diagnostics);
try {
v.visit_asr((ASR::asr_t &)asr);
Expand Down
2 changes: 1 addition & 1 deletion src/libasr/codegen/asr_to_llvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4953,7 +4953,7 @@ Result<std::unique_ptr<LLVMModule>> asr_to_llvm(ASR::TranslationUnit_t &asr,
}

pass_replace_select_case(al, asr);
pass_unused_functions(al, asr);
pass_unused_functions(al, asr, false);

if( fast ) {
pass_replace_flip_sign(al, asr, rl_path);
Expand Down
5 changes: 3 additions & 2 deletions src/libasr/pass/unused_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,9 @@ class UnusedFunctionsVisitor : public ASR::BaseWalkVisitor<UnusedFunctionsVisito

};

void pass_unused_functions(Allocator &al, ASR::TranslationUnit_t &unit) {
if (is_program_present(unit)) {
void pass_unused_functions(Allocator &al, ASR::TranslationUnit_t &unit,
bool always_run) {
if (is_program_present(unit) || always_run) {
for (int i=0; i < 4; i++)
{
std::map<uint64_t, std::string> fn_unused;
Expand Down
3 changes: 2 additions & 1 deletion src/libasr/pass/unused_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

namespace LFortran {

void pass_unused_functions(Allocator &al, ASR::TranslationUnit_t &unit);
void pass_unused_functions(Allocator &al, ASR::TranslationUnit_t &unit,
bool always_run);

} // namespace LFortran

Expand Down
1 change: 1 addition & 0 deletions src/libasr/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ struct CompilerOptions {
bool c_preprocessor = false;
std::vector<std::string> c_preprocessor_defines;
bool prescan = true;
bool disable_main = false;
bool symtab_only = false;
bool show_stacktrace = false;
bool use_colors = true;
Expand Down
25 changes: 20 additions & 5 deletions src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ ASR::Module_t* load_module(Allocator &al, SymbolTable *symtab,
// Convert the module from AST to ASR
LFortran::LocationManager lm;
lm.in_filename = infile;
Result<ASR::TranslationUnit_t*> r2 = python_ast_to_asr(al, *ast, diagnostics, false, false);
Result<ASR::TranslationUnit_t*> r2 = python_ast_to_asr(al, *ast, diagnostics, false, false, false);
std::string input;
read_file(infile, input);
CompilerOptions compiler_options;
Expand Down Expand Up @@ -3567,7 +3567,7 @@ std::string pickle_tree_python(AST::ast_t &ast, bool colors) {

Result<ASR::TranslationUnit_t*> python_ast_to_asr(Allocator &al,
AST::ast_t &ast, diag::Diagnostics &diagnostics, bool main_module,
bool symtab_only)
bool disable_main, bool symtab_only)
{
std::map<int, ASR::symbol_t*> ast_overload;

Expand Down Expand Up @@ -3596,10 +3596,25 @@ Result<ASR::TranslationUnit_t*> python_ast_to_asr(Allocator &al,
}

if (main_module) {
// If it is a main module, turn it into a program.
// If it is a main module, turn it into a program
// Note: we can modify this behavior for interactive mode later
pass_wrap_global_stmts_into_program(al, *tu, "_lpython_main_program");
LFORTRAN_ASSERT(asr_verify(*tu));
if (disable_main) {
if (tu->n_items > 0) {
diagnostics.add(diag::Diagnostic(
"The script is invoked as the main module and it has code to execute,\n"
"but `--disable-main` was passed so no code was generated for `main`.\n"
"We are removing all global executable code from ASR.",
diag::Level::Warning, diag::Stage::Semantic, {})
);
// We have to remove the code
tu->m_items=nullptr;
tu->n_items=0;
LFORTRAN_ASSERT(asr_verify(*tu));
}
} else {
pass_wrap_global_stmts_into_program(al, *tu, "_lpython_main_program");
LFORTRAN_ASSERT(asr_verify(*tu));
}
}

return tu;
Expand Down
4 changes: 2 additions & 2 deletions src/lpython/semantics/python_ast_to_asr.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ namespace LFortran::LPython {
std::string pickle_python(AST::ast_t &ast, bool colors=false, bool indent=false);
std::string pickle_tree_python(AST::ast_t &ast, bool colors=true);
Result<ASR::TranslationUnit_t*> python_ast_to_asr(Allocator &al,
LPython::AST::ast_t &ast, diag::Diagnostics &diagnostics, bool main_module,
bool symtab_only);
LPython::AST::ast_t &ast, diag::Diagnostics &diagnostics,
bool main_module, bool disable_main, bool symtab_only);

} // namespace LFortran

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"basename": "llvm-ltypes-1402bca",
"basename": "llvm-ltypes1-dacf939",
"cmd": "lpython --no-color --show-llvm {infile} -o {outfile}",
"infile": "tests/ltypes.py",
"infile": "tests/ltypes1.py",
"infile_hash": "daac081f5e376233adabe24304126763178cfd3a6286c361b7955446",
"outfile": null,
"outfile_hash": null,
"stdout": "llvm-ltypes-1402bca.stdout",
"stdout": "llvm-ltypes1-dacf939.stdout",
"stdout_hash": "cafb6dc418ad5ddc1ef709741df5c2ee0a45fcdee41b49c50f121ffb",
"stderr": null,
"stderr_hash": null,
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion tests/tests.toml
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ c = true
# integration_tests

[[test]]
filename = "ltypes.py"
filename = "ltypes1.py"
llvm = true

[[test]]
Expand Down

0 comments on commit ecead95

Please sign in to comment.