Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync libasr with LFortran #2410

Merged
merged 4 commits into from
Nov 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from compiler_tester.tester import color, fg, log, run_test, style, tester_main


def single_test(test, verbose, no_llvm, skip_run_with_dbg, update_reference,
def single_test(test, verbose, no_llvm, skip_run_with_dbg, skip_cpptranslate, update_reference,
no_color, specific_backends=None, excluded_backends=None):
filename = test["filename"]
def is_included(backend):
Expand Down
77 changes: 20 additions & 57 deletions src/bin/lpython.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,29 +80,6 @@ std::string get_kokkos_dir()
throw LCompilers::LCompilersException("LFORTRAN_KOKKOS_DIR is not defined");
}

int visualize_json(std::string &astr_data_json, LCompilers::Platform os) {
using namespace LCompilers;
std::string file_loc = LCompilers::LPython::generate_visualize_html(astr_data_json);
std::string open_cmd = "";
switch (os) {
case Linux: open_cmd = "xdg-open"; break;
case Windows: open_cmd = "start"; break;
case macOS_Intel:
case macOS_ARM: open_cmd = "open"; break;
default:
std::cerr << "Unsupported Platform " << pf2s(os) <<std::endl;
std::cerr << "Please open file " << file_loc << " manually" <<std::endl;
return 11;
}
std::string cmd = open_cmd + " " + file_loc;
int err = system(cmd.data());
if (err) {
std::cout << "The command '" + cmd + "' failed." << std::endl;
return 11;
}
return 0;
}

#ifdef HAVE_LFORTRAN_LLVM

#endif
Expand Down Expand Up @@ -171,10 +148,10 @@ int emit_ast(const std::string &infile,
}
LCompilers::LPython::AST::ast_t* ast = r.result;

if (compiler_options.tree) {
if (compiler_options.po.tree) {
std::cout << LCompilers::LPython::pickle_tree_python(*ast,
compiler_options.use_colors) << std::endl;
} else if (compiler_options.json) {
} else if (compiler_options.po.json) {
LCompilers::LocationManager lm;
{
LCompilers::LocationManager::FileLocations fl;
Expand All @@ -185,7 +162,7 @@ int emit_ast(const std::string &infile,
lm.file_ends.push_back(input.size());
}
std::cout << LCompilers::LPython::pickle_json(*ast, lm) << std::endl;
} else if (compiler_options.visualize) {
} else if (compiler_options.po.visualize) {
LCompilers::LocationManager lm;
{
LCompilers::LocationManager::FileLocations fl;
Expand Down Expand Up @@ -238,18 +215,10 @@ int emit_asr(const std::string &infile,
return 2;
}
LCompilers::ASR::TranslationUnit_t* asr = r.result;
LCompilers::PassOptions pass_options;
pass_options.run_fun = "f";
pass_options.always_run = true;
pass_options.verbose = compiler_options.verbose;
pass_options.pass_cumulative = compiler_options.pass_cumulative;
pass_options.all_symbols_mangling = compiler_options.all_symbols_mangling;
pass_options.module_name_mangling = compiler_options.module_name_mangling;
pass_options.global_symbols_mangling = compiler_options.global_symbols_mangling;
pass_options.intrinsic_symbols_mangling = compiler_options.intrinsic_symbols_mangling;
compiler_options.po.always_run = true;
compiler_options.po.run_fun = "f";


pass_manager.apply_passes(al, asr, pass_options, diagnostics);
pass_manager.apply_passes(al, asr, compiler_options.po, diagnostics);

if (compiler_options.tree) {
std::cout << LCompilers::LPython::pickle_tree(*asr,
Expand Down Expand Up @@ -345,17 +314,11 @@ int emit_c(const std::string &infile,
LCompilers::ASR::TranslationUnit_t* asr = r1.result;

// Apply ASR passes
LCompilers::PassOptions pass_options;
pass_manager.use_default_passes(true);
pass_options.run_fun = "f";
pass_options.always_run = true;
pass_options.verbose = compiler_options.verbose;
pass_options.all_symbols_mangling = compiler_options.all_symbols_mangling;
pass_options.module_name_mangling = compiler_options.module_name_mangling;
pass_options.global_symbols_mangling = compiler_options.global_symbols_mangling;
pass_options.intrinsic_symbols_mangling = compiler_options.intrinsic_symbols_mangling;
compiler_options.po.always_run = true;
compiler_options.po.run_fun = "f";

pass_manager.apply_passes(al, asr, pass_options, diagnostics);
pass_manager.apply_passes(al, asr, compiler_options.po, diagnostics);

diagnostics.diagnostics.clear();
auto res = LCompilers::asr_to_c(al, *asr, diagnostics, compiler_options, 0);
Expand Down Expand Up @@ -406,11 +369,11 @@ int emit_c_to_file(const std::string &infile, const std::string &outfile,
pass_manager.use_default_passes(true);
pass_options.run_fun = "f";
pass_options.always_run = true;
pass_options.verbose = compiler_options.verbose;
pass_options.all_symbols_mangling = compiler_options.all_symbols_mangling;
pass_options.module_name_mangling = compiler_options.module_name_mangling;
pass_options.global_symbols_mangling = compiler_options.global_symbols_mangling;
pass_options.intrinsic_symbols_mangling = compiler_options.intrinsic_symbols_mangling;
pass_options.verbose = compiler_options.po.verbose;
pass_options.all_symbols_mangling = compiler_options.po.all_symbols_mangling;
pass_options.module_name_mangling = compiler_options.po.module_name_mangling;
pass_options.global_symbols_mangling = compiler_options.po.global_symbols_mangling;
pass_options.intrinsic_symbols_mangling = compiler_options.po.intrinsic_symbols_mangling;
ubaidsk marked this conversation as resolved.
Show resolved Hide resolved

pass_manager.apply_passes(al, asr, pass_options, diagnostics);

Expand Down Expand Up @@ -1585,16 +1548,16 @@ int main(int argc, char *argv[])
app.add_flag("--print-targets", print_targets, "Print the registered targets");
app.add_flag("--get-rtl-header-dir", print_rtl_header_dir, "Print the path to the runtime library header file");
app.add_flag("--get-rtl-dir", print_rtl_dir, "Print the path to the runtime library file");
app.add_flag("--verbose", compiler_options.verbose, "Print debugging statements");
app.add_flag("--cumulative", compiler_options.pass_cumulative, "Apply all the passes cumulatively till the given pass");
app.add_flag("--verbose", compiler_options.po.verbose, "Print debugging statements");
app.add_flag("--cumulative", compiler_options.po.pass_cumulative, "Apply all the passes cumulatively till the given pass");
app.add_flag("--enable-cpython", compiler_options.enable_cpython, "Enable CPython runtime");
app.add_flag("--enable-symengine", compiler_options.enable_symengine, "Enable Symengine runtime");
app.add_flag("--link-numpy", compiler_options.link_numpy, "Enable NumPy runtime (implies --enable-cpython)");
app.add_flag("--separate-compilation", separate_compilation, "Generates unique names for all the symbols");
app.add_flag("--module-mangling", compiler_options.module_name_mangling, "Mangles the module name");
app.add_flag("--global-mangling", compiler_options.global_symbols_mangling, "Mangles all the global symbols");
app.add_flag("--intrinsic-mangling", compiler_options.intrinsic_symbols_mangling, "Mangles all the intrinsic symbols");
app.add_flag("--all-mangling", compiler_options.all_symbols_mangling, "Mangles all possible symbols");
app.add_flag("--module-mangling", compiler_options.po.module_name_mangling, "Mangles the module name");
app.add_flag("--global-mangling", compiler_options.po.global_symbols_mangling, "Mangles all the global symbols");
app.add_flag("--intrinsic-mangling", compiler_options.po.intrinsic_symbols_mangling, "Mangles all the intrinsic symbols");
app.add_flag("--all-mangling", compiler_options.po.all_symbols_mangling, "Mangles all possible symbols");

// LSP specific options
app.add_flag("--show-errors", show_errors, "Show errors when LSP is running in the background");
Expand Down
8 changes: 5 additions & 3 deletions src/libasr/ASR.asdl
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ symbol
bool loaded_from_mod, bool intrinsic)
| Function(symbol_table symtab, identifier name, ttype function_signature,
identifier* dependencies, expr* args, stmt* body, expr? return_var,
access access, bool deterministic, bool side_effect_free, string? module_file)
access access, bool deterministic, bool side_effect_free, string? module_file)
| GenericProcedure(symbol_table parent_symtab, identifier name,
symbol* procs, access access)
| CustomOperator(symbol_table parent_symtab, identifier name,
Expand Down Expand Up @@ -198,7 +198,7 @@ stmt
| GoToTarget(int id, identifier name)
| If(expr test, stmt* body, stmt* orelse)
| IfArithmetic(expr test, int lt_label, int eq_label, int gt_label)
| Print(expr? fmt, expr* values, expr? separator, expr? end)
| Print(expr* values, expr? separator, expr? end)
| FileOpen(int label, expr? newunit, expr? filename, expr? status, expr? form)
| FileClose(int label, expr? unit, expr? iostat, expr? iomsg, expr? err, expr? status)
| FileRead(int label, expr? unit, expr? fmt, expr? iomsg, expr? iostat, expr? id, expr* values)
Expand All @@ -212,7 +212,7 @@ stmt
expr? read, expr? write, expr? readwrite, expr? delim,
expr? pad, expr? flen, expr? blocksize, expr? convert,
expr? carriagecontrol, expr? iolength)
| FileWrite(int label, expr? unit, expr? fmt, expr? iomsg, expr? iostat, expr? id, expr* values, expr? separator, expr? end)
| FileWrite(int label, expr? unit, expr? iomsg, expr? iostat, expr? id, expr* values, expr? separator, expr? end)
| Return()
| Select(expr test, case_stmt* body, stmt* default)
| Stop(expr? code)
Expand Down Expand Up @@ -323,6 +323,7 @@ expr
| ArrayPack(expr array, expr mask, expr? vector, ttype type, expr? value)
| ArrayReshape(expr array, expr shape, ttype type, expr? value)
| ArrayAll(expr mask, expr? dim, ttype type, expr? value)
| ArrayBroadcast(expr array, expr shape, ttype type, expr? value)

| BitCast(expr source, expr mold, expr? size, ttype type, expr? value)
| StructInstanceMember(expr v, symbol m, ttype type, expr? value)
Expand Down Expand Up @@ -428,6 +429,7 @@ array_physical_type
| FixedSizeArray
| NumPyArray
| ISODescriptorArray
| SIMDArray

binop = Add | Sub | Mul | Div | Pow | BitAnd | BitOr | BitXor | BitLShift | BitRShift

Expand Down
2 changes: 2 additions & 0 deletions src/libasr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ set(SRC
codegen/asr_to_cpp.cpp
codegen/asr_to_c.cpp
codegen/asr_to_julia.cpp
codegen/asr_to_fortran.cpp
codegen/asr_to_py.cpp
codegen/x86_assembler.cpp
codegen/asr_to_x86.cpp
Expand Down Expand Up @@ -62,6 +63,7 @@ set(SRC
pass/pass_list_expr.cpp
pass/pass_compare.cpp
pass/unique_symbols.cpp
pass/insert_deallocate.cpp

asr_verify.cpp
asr_utils.cpp
Expand Down
6 changes: 4 additions & 2 deletions src/libasr/asdl_cpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -1718,6 +1718,7 @@ def visitModule(self, mod):
self.emit( "Struct& self() { return static_cast<Struct&>(*this); }", 1)
self.emit("public:")
self.emit( "std::string s, indtd = \"\";", 1)
self.emit( "bool no_loc = false;", 1)
self.emit( "int indent_level = 0, indent_spaces = 4;", 1)
# Storing a reference to LocationManager like this isn't ideal.
# One must make sure JsonBaseVisitor isn't reused in a case where AST/ASR has changed
Expand All @@ -1739,7 +1740,9 @@ def visitModule(self, mod):
self.emit( "indtd = std::string(indent_level*indent_spaces, ' ');",2)
self.emit( "}",1)
self.emit( "void append_location(std::string &s, uint32_t first, uint32_t last) {", 1)
self.emit( 's.append("\\"loc\\": {");', 2);
self.emit( 'if (no_loc) return;', 2)
self.emit( 's.append(",\\n" + indtd);', 2)
self.emit( 's.append("\\"loc\\": {");', 2)
self.emit( 'inc_indent();', 2)
self.emit( 's.append("\\n" + indtd);', 2)
self.emit( 's.append("\\"first\\": " + std::to_string(first));', 2)
Expand Down Expand Up @@ -1809,7 +1812,6 @@ def make_visitor(self, name, fields, cons):
self.emit('s.append(",\\n" + indtd);', 2)
self.emit('dec_indent(); s.append("\\n" + indtd);', 2)
self.emit( 's.append("}");', 2)
self.emit( 's.append(",\\n" + indtd);', 2)
if name in products:
self.emit( 'append_location(s, x.loc.first, x.loc.last);', 2)
else:
Expand Down
Loading
Loading