Skip to content

Sync libasr with LCompilers #8

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

Merged
merged 1 commit into from
Nov 19, 2022
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
25 changes: 16 additions & 9 deletions src/libasr/ASR.asdl
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ symbol
stmt* body)
| Module(symbol_table symtab, identifier name, identifier* dependencies,
bool loaded_from_mod, bool intrinsic)
| Function(symbol_table symtab, identifier name, expr* args, stmt* body,
| Function(symbol_table symtab, identifier name, identifier* dependencies, expr* args, stmt* body,
expr? return_var, abi abi, access access, deftype deftype,
string? bindc_name, bool elemental, bool pure, bool module, bool inline,
bool static, ttype* type_params, symbol* restrictions, bool is_restriction)
Expand All @@ -95,12 +95,14 @@ symbol
| ExternalSymbol(symbol_table parent_symtab, identifier name,
symbol external, identifier module_name, identifier* scope_names,
identifier original_name, access access)
| StructType(symbol_table symtab, identifier name, identifier* members,
abi abi, access access, symbol? parent)
| EnumType(symbol_table symtab, identifier name, identifier* members,
abi abi, access access, enumtype enum_value_type, ttype type, symbol? parent)
| UnionType(symbol_table symtab, identifier name, identifier* members,
abi abi, access access, symbol? parent)
| StructType(symbol_table symtab, identifier name, identifier* dependencies,
identifier* members, abi abi, access access, bool is_packed,
expr? alignment, symbol? parent)
| EnumType(symbol_table symtab, identifier name, identifier* dependencies,
identifier* members, abi abi, access access, enumtype enum_value_type,
ttype type, symbol? parent)
| UnionType(symbol_table symtab, identifier name, identifier* dependencies,
identifier* members, abi abi, access access, symbol? parent)
| Variable(symbol_table parent_symtab, identifier name, intent intent,
expr? symbolic_value, expr? value, storage_type storage, ttype type,
abi abi, access access, presence presence, bool value_attr)
Expand Down Expand Up @@ -271,8 +273,8 @@ expr

| Var(symbol v)

| ArrayConstant(expr* args, ttype type)
| ArrayItem(expr v, array_index* args, ttype type, expr? value)
| ArrayConstant(expr* args, ttype type, arraystorage storage_format)
| ArrayItem(expr v, array_index* args, ttype type, arraystorage storage_format, expr? value)
| ArraySection(expr v, array_index* args, ttype type, expr? value)
| ArraySize(expr v, expr? dim, ttype type, expr? value)
| ArrayBound(expr v, expr? dim, ttype type, arraybound bound,
Expand Down Expand Up @@ -306,6 +308,8 @@ expr
| IntegerBitLen(expr a, ttype type, expr? value)
| Ichar(expr arg, ttype type, expr? value)

| SizeOfType(ttype arg, ttype type, expr? value)


-- `len` in Character:
-- >=0 ... the length of the string, known at compile time
Expand Down Expand Up @@ -341,6 +345,7 @@ ttype
| Class(symbol class_type, dimension* dims)
| Dict(ttype key_type, ttype value_type)
| Pointer(ttype type)
| Const(ttype type)
| CPtr()
| TypeParameter(identifier param, dimension* dims)

Expand All @@ -356,6 +361,8 @@ integerboz = Binary | Hex | Octal

arraybound = LBound | UBound

arraystorage = RowMajor | ColMajor

cast_kind
= RealToInteger
| IntegerToReal
Expand Down
4 changes: 3 additions & 1 deletion src/libasr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ set(SRC
codegen/asr_to_x86.cpp
codegen/asr_to_wasm.cpp
codegen/wasm_to_wat.cpp
codegen/wasm_to_x86.cpp
codegen/wasm_utils.cpp

pass/param_to_const.cpp
Expand All @@ -33,6 +34,7 @@ set(SRC
pass/select_case.cpp
pass/implied_do_loops.cpp
pass/array_op.cpp
pass/subroutine_from_function.cpp
pass/class_constructor.cpp
pass/arr_slice.cpp
pass/print_arr.cpp
Expand All @@ -50,7 +52,7 @@ set(SRC
pass/instantiate_template.cpp
pass/update_array_dim_intrinsic_calls.cpp
pass/pass_array_by_data.cpp
pass/pass_list_concat.cpp
pass/pass_list_expr.cpp

asr_verify.cpp
asr_utils.cpp
Expand Down
1 change: 1 addition & 0 deletions src/libasr/asdl_cpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -1998,6 +1998,7 @@ def make_visitor(self, name, fields):
ASR::symbol_t *s = ((ASR::%s_t*)f)->m_v;
if (s->type == ASR::symbolType::ExternalSymbol) {
ASR::ExternalSymbol_t *e = ASR::down_cast<ASR::ExternalSymbol_t>(s);
LFORTRAN_ASSERT(e->m_external);
LFORTRAN_ASSERT(!ASR::is_a<ASR::ExternalSymbol_t>(*e->m_external));
s = e->m_external;
}
Expand Down
1 change: 1 addition & 0 deletions src/libasr/asr_scopes.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ struct SymbolTable {

void erase_symbol(const std::string &name) {
//auto it = scope.find(to_lower(name));
LFORTRAN_ASSERT(scope.find(name) != scope.end())
scope.erase(name);
}

Expand Down
71 changes: 53 additions & 18 deletions src/libasr/asr_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <libasr/asr_verify.h>
#include <libasr/utils.h>
#include <libasr/modfile.h>
#include <libasr/pass/pass_utils.h>

namespace LFortran {

Expand Down Expand Up @@ -64,6 +65,23 @@ std::vector<std::string> determine_module_dependencies(
return order_deps(deps);
}

std::vector<std::string> determine_function_definition_order(
SymbolTable* symtab) {
std::map<std::string, std::vector<std::string>> func_dep_graph;
for( auto itr: symtab->get_scope() ) {
if( ASR::is_a<ASR::Function_t>(*itr.second) ) {
std::vector<std::string> deps;
ASR::Function_t* func = ASR::down_cast<ASR::Function_t>(itr.second);
for( size_t i = 0; i < func->n_dependencies; i++ ) {
std::string dep = func->m_dependencies[i];
deps.push_back(dep);
}
func_dep_graph[itr.first] = deps;
}
}
return ASRUtils::order_deps(func_dep_graph);
}

void extract_module_python(const ASR::TranslationUnit_t &m,
std::vector<std::pair<std::string, ASR::Module_t*>>& children_modules,
std::string module_name) {
Expand Down Expand Up @@ -97,7 +115,7 @@ ASR::Module_t* extract_module(const ASR::TranslationUnit_t &m) {
ASR::Module_t* load_module(Allocator &al, SymbolTable *symtab,
const std::string &module_name,
const Location &loc, bool intrinsic,
const std::string &rl_path,
LCompilers::PassOptions& pass_options,
bool run_verify,
const std::function<void (const std::string &, const Location &)> err) {
LFORTRAN_ASSERT(symtab);
Expand All @@ -111,14 +129,14 @@ ASR::Module_t* load_module(Allocator &al, SymbolTable *symtab,
}
LFORTRAN_ASSERT(symtab->parent == nullptr);
ASR::TranslationUnit_t *mod1 = find_and_load_module(al, module_name,
*symtab, intrinsic, rl_path);
*symtab, intrinsic, pass_options);
if (mod1 == nullptr && !intrinsic) {
// Module not found as a regular module. Try intrinsic module
if (module_name == "iso_c_binding"
||module_name == "iso_fortran_env"
||module_name == "ieee_arithmetic") {
mod1 = find_and_load_module(al, "lfortran_intrinsic_" + module_name,
*symtab, true, rl_path);
*symtab, true, pass_options);
}
}
if (mod1 == nullptr) {
Expand Down Expand Up @@ -155,13 +173,13 @@ ASR::Module_t* load_module(Allocator &al, SymbolTable *symtab,
bool is_intrinsic = startswith(item, "lfortran_intrinsic");
ASR::TranslationUnit_t *mod1 = find_and_load_module(al,
item,
*symtab, is_intrinsic, rl_path);
*symtab, is_intrinsic, pass_options);
if (mod1 == nullptr && !is_intrinsic) {
// Module not found as a regular module. Try intrinsic module
if (item == "iso_c_binding"
||item == "iso_fortran_env") {
mod1 = find_and_load_module(al, "lfortran_intrinsic_" + item,
*symtab, true, rl_path);
*symtab, true, pass_options);
}
}

Expand All @@ -188,8 +206,16 @@ ASR::Module_t* load_module(Allocator &al, SymbolTable *symtab,

// Fix all external symbols
fix_external_symbols(*tu, *symtab);
PassUtils::UpdateDependenciesVisitor v(al);
v.visit_TranslationUnit(*tu);
if (run_verify) {
LFORTRAN_ASSERT(asr_verify(*tu));
#if defined(WITH_LFORTRAN_ASSERT)
diag::Diagnostics diagnostics;
if (!asr_verify(*tu, true, diagnostics)) {
std::cerr << diagnostics.render2();
throw LCompilersException("Verify failed");
};
#endif
}
symtab->asr_owner = orig_asr_owner;

Expand Down Expand Up @@ -235,20 +261,29 @@ void set_intrinsic(ASR::TranslationUnit_t* trans_unit) {

ASR::TranslationUnit_t* find_and_load_module(Allocator &al, const std::string &msym,
SymbolTable &symtab, bool intrinsic,
const std::string &rl_path) {
std::string modfilename = msym + ".mod";
if (intrinsic) {
modfilename = rl_path + "/" + modfilename;
}
LCompilers::PassOptions& pass_options) {
std::filesystem::path runtime_library_dir { pass_options.runtime_library_dir };
std::filesystem::path filename {msym + ".mod"};
std::vector<std::filesystem::path> mod_files_dirs;

mod_files_dirs.push_back( runtime_library_dir );
mod_files_dirs.push_back( pass_options.mod_files_dir );
mod_files_dirs.insert(mod_files_dirs.end(),
pass_options.include_dirs.begin(),
pass_options.include_dirs.end());

std::string modfile;
if (!read_file(modfilename, modfile)) return nullptr;
ASR::TranslationUnit_t *asr = load_modfile(al, modfile, false,
symtab);
if (intrinsic) {
set_intrinsic(asr);
for (auto path : mod_files_dirs) {
std::string modfile;
std::filesystem::path full_path = path / filename;
if (read_file(full_path.string(), modfile)) {
ASR::TranslationUnit_t *asr = load_modfile(al, modfile, false, symtab);
if (intrinsic) {
set_intrinsic(asr);
}
return asr;
}
}
return asr;
return nullptr;
}

ASR::asr_t* getStructInstanceMember_t(Allocator& al, const Location& loc,
Expand Down
Loading