Skip to content

Commit 0b3de50

Browse files
committed
Support --dump-all-passes, --dump-all-passes-fortran
1 parent f0b2a49 commit 0b3de50

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

src/bin/lpython.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,45 @@ int emit_wat(const std::string &infile,
480480
return 0;
481481
}
482482

483+
int dump_all_passes(const std::string &infile,
484+
const std::string &runtime_library_dir,
485+
CompilerOptions &compiler_options) {
486+
std::string input = LCompilers::read_file(infile);
487+
488+
Allocator al(4*1024);
489+
LCompilers::LocationManager lm;
490+
LCompilers::diag::Diagnostics diagnostics;
491+
{
492+
LCompilers::LocationManager::FileLocations fl;
493+
fl.in_filename = infile;
494+
lm.files.push_back(fl);
495+
lm.file_ends.push_back(input.size());
496+
}
497+
498+
LCompilers::Result<LCompilers::LPython::AST::ast_t*> r = parse_python_file(
499+
al, runtime_library_dir, infile, diagnostics, 0, compiler_options.new_parser);
500+
std::cerr << diagnostics.render(lm, compiler_options);
501+
if (!r.ok) {
502+
return 1;
503+
}
504+
LCompilers::LPython::AST::ast_t* ast = r.result;
505+
diagnostics.diagnostics.clear();
506+
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
507+
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "__main__", infile);
508+
std::cerr << diagnostics.render(lm, compiler_options);
509+
if (r1.ok) {
510+
LCompilers::PassManager pass_manager;
511+
compiler_options.po.always_run = true;
512+
compiler_options.po.run_fun = "f";
513+
pass_manager.dump_all_passes(al, r1.result, compiler_options.po, diagnostics, lm);
514+
std::cerr << diagnostics.render(lm, compiler_options);
515+
} else {
516+
LCOMPILERS_ASSERT(diagnostics.has_error())
517+
return 1;
518+
}
519+
return 0;
520+
}
521+
483522
#ifdef HAVE_LFORTRAN_RAPIDJSON
484523

485524
int get_symbols (const std::string &infile,
@@ -1586,6 +1625,8 @@ int main(int argc, char *argv[])
15861625
app.add_flag("--get-rtl-header-dir", print_rtl_header_dir, "Print the path to the runtime library header file");
15871626
app.add_flag("--get-rtl-dir", print_rtl_dir, "Print the path to the runtime library file");
15881627
app.add_flag("--verbose", compiler_options.verbose, "Print debugging statements");
1628+
app.add_flag("--dump-all-passes", compiler_options.po.dump_all_passes, "Apply all the passes and dump the ASR into a file");
1629+
app.add_flag("--dump-all-passes-fortran", compiler_options.po.dump_fortran, "Apply all passes and dump the ASR after each pass into fortran file");
15891630
app.add_flag("--cumulative", compiler_options.pass_cumulative, "Apply all the passes cumulatively till the given pass");
15901631
app.add_flag("--enable-cpython", compiler_options.enable_cpython, "Enable CPython runtime");
15911632
app.add_flag("--enable-symengine", compiler_options.enable_symengine, "Enable Symengine runtime");
@@ -1763,6 +1804,10 @@ int main(int argc, char *argv[])
17631804
outfile = basename + ".out";
17641805
}
17651806

1807+
if (compiler_options.po.dump_fortran || compiler_options.po.dump_all_passes) {
1808+
dump_all_passes(arg_file, compiler_options);
1809+
}
1810+
17661811
// if (arg_E) {
17671812
// return emit_c_preprocessor(arg_file, compiler_options);
17681813
// }

0 commit comments

Comments
 (0)