Skip to content

Commit 35a4542

Browse files
authored
Merge pull request #1314 from certik/sync_libasr_1
Sync libasr from LFortran
2 parents 7c2a562 + f63c4cd commit 35a4542

File tree

83 files changed

+877
-618
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+877
-618
lines changed

src/bin/lpython.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ int emit_asr(const std::string &infile,
174174
LCompilers::PassOptions pass_options;
175175
pass_options.run_fun = "f";
176176
pass_options.always_run = true;
177-
pass_manager.apply_passes(al, asr, pass_options);
177+
pass_manager.apply_passes(al, asr, pass_options, diagnostics);
178178

179179
if (compiler_options.tree) {
180180
std::cout << LFortran::pickle_tree(*asr, compiler_options.use_colors,
@@ -1345,6 +1345,7 @@ int main(int argc, char *argv[])
13451345
case (LFortran::Platform::macOS_ARM) : std::cout << "macOS ARM"; break;
13461346
case (LFortran::Platform::Windows) : std::cout << "Windows"; break;
13471347
case (LFortran::Platform::FreeBSD) : std::cout << "FreeBSD"; break;
1348+
case (LFortran::Platform::OpenBSD) : std::cout << "OpenBSD"; break;
13481349
}
13491350
std::cout << std::endl;
13501351
#ifdef HAVE_LFORTRAN_LLVM

src/libasr/asdl_cpp.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1998,6 +1998,7 @@ def make_visitor(self, name, fields):
19981998
ASR::symbol_t *s = ((ASR::%s_t*)f)->m_v;
19991999
if (s->type == ASR::symbolType::ExternalSymbol) {
20002000
ASR::ExternalSymbol_t *e = ASR::down_cast<ASR::ExternalSymbol_t>(s);
2001+
LFORTRAN_ASSERT(e->m_external);
20012002
LFORTRAN_ASSERT(!ASR::is_a<ASR::ExternalSymbol_t>(*e->m_external));
20022003
s = e->m_external;
20032004
}

src/libasr/asr_utils.cpp

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <libasr/asr_verify.h>
88
#include <libasr/utils.h>
99
#include <libasr/modfile.h>
10+
#include <libasr/pass/pass_utils.h>
1011

1112
namespace LFortran {
1213

@@ -114,7 +115,7 @@ ASR::Module_t* extract_module(const ASR::TranslationUnit_t &m) {
114115
ASR::Module_t* load_module(Allocator &al, SymbolTable *symtab,
115116
const std::string &module_name,
116117
const Location &loc, bool intrinsic,
117-
const std::string &rl_path,
118+
LCompilers::PassOptions& pass_options,
118119
bool run_verify,
119120
const std::function<void (const std::string &, const Location &)> err) {
120121
LFORTRAN_ASSERT(symtab);
@@ -128,14 +129,14 @@ ASR::Module_t* load_module(Allocator &al, SymbolTable *symtab,
128129
}
129130
LFORTRAN_ASSERT(symtab->parent == nullptr);
130131
ASR::TranslationUnit_t *mod1 = find_and_load_module(al, module_name,
131-
*symtab, intrinsic, rl_path);
132+
*symtab, intrinsic, pass_options);
132133
if (mod1 == nullptr && !intrinsic) {
133134
// Module not found as a regular module. Try intrinsic module
134135
if (module_name == "iso_c_binding"
135136
||module_name == "iso_fortran_env"
136137
||module_name == "ieee_arithmetic") {
137138
mod1 = find_and_load_module(al, "lfortran_intrinsic_" + module_name,
138-
*symtab, true, rl_path);
139+
*symtab, true, pass_options);
139140
}
140141
}
141142
if (mod1 == nullptr) {
@@ -172,13 +173,13 @@ ASR::Module_t* load_module(Allocator &al, SymbolTable *symtab,
172173
bool is_intrinsic = startswith(item, "lfortran_intrinsic");
173174
ASR::TranslationUnit_t *mod1 = find_and_load_module(al,
174175
item,
175-
*symtab, is_intrinsic, rl_path);
176+
*symtab, is_intrinsic, pass_options);
176177
if (mod1 == nullptr && !is_intrinsic) {
177178
// Module not found as a regular module. Try intrinsic module
178179
if (item == "iso_c_binding"
179180
||item == "iso_fortran_env") {
180181
mod1 = find_and_load_module(al, "lfortran_intrinsic_" + item,
181-
*symtab, true, rl_path);
182+
*symtab, true, pass_options);
182183
}
183184
}
184185

@@ -205,8 +206,16 @@ ASR::Module_t* load_module(Allocator &al, SymbolTable *symtab,
205206

206207
// Fix all external symbols
207208
fix_external_symbols(*tu, *symtab);
209+
PassUtils::UpdateDependenciesVisitor v(al);
210+
v.visit_TranslationUnit(*tu);
208211
if (run_verify) {
209-
LFORTRAN_ASSERT(asr_verify(*tu));
212+
#if defined(WITH_LFORTRAN_ASSERT)
213+
diag::Diagnostics diagnostics;
214+
if (!asr_verify(*tu, true, diagnostics)) {
215+
std::cerr << diagnostics.render2();
216+
throw LCompilersException("Verify failed");
217+
};
218+
#endif
210219
}
211220
symtab->asr_owner = orig_asr_owner;
212221

@@ -252,20 +261,29 @@ void set_intrinsic(ASR::TranslationUnit_t* trans_unit) {
252261

253262
ASR::TranslationUnit_t* find_and_load_module(Allocator &al, const std::string &msym,
254263
SymbolTable &symtab, bool intrinsic,
255-
const std::string &rl_path) {
256-
std::string modfilename = msym + ".mod";
257-
if (intrinsic) {
258-
modfilename = rl_path + "/" + modfilename;
259-
}
264+
LCompilers::PassOptions& pass_options) {
265+
std::filesystem::path runtime_library_dir { pass_options.runtime_library_dir };
266+
std::filesystem::path filename {msym + ".mod"};
267+
std::vector<std::filesystem::path> mod_files_dirs;
268+
269+
mod_files_dirs.push_back( runtime_library_dir );
270+
mod_files_dirs.push_back( pass_options.mod_files_dir );
271+
mod_files_dirs.insert(mod_files_dirs.end(),
272+
pass_options.include_dirs.begin(),
273+
pass_options.include_dirs.end());
260274

261-
std::string modfile;
262-
if (!read_file(modfilename, modfile)) return nullptr;
263-
ASR::TranslationUnit_t *asr = load_modfile(al, modfile, false,
264-
symtab);
265-
if (intrinsic) {
266-
set_intrinsic(asr);
275+
for (auto path : mod_files_dirs) {
276+
std::string modfile;
277+
std::filesystem::path full_path = path / filename;
278+
if (read_file(full_path.string(), modfile)) {
279+
ASR::TranslationUnit_t *asr = load_modfile(al, modfile, false, symtab);
280+
if (intrinsic) {
281+
set_intrinsic(asr);
282+
}
283+
return asr;
284+
}
267285
}
268-
return asr;
286+
return nullptr;
269287
}
270288

271289
ASR::asr_t* getStructInstanceMember_t(Allocator& al, const Location& loc,

src/libasr/asr_utils.h

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,20 @@
99
#include <libasr/assert.h>
1010
#include <libasr/asr.h>
1111
#include <libasr/string_utils.h>
12+
#include <libasr/utils.h>
1213

1314
namespace LFortran {
1415

1516
namespace ASRUtils {
1617

1718
static inline double extract_real(const char *s) {
18-
return std::atof(s);
19-
}
19+
// TODO: this is inefficient. We should
20+
// convert this in the tokenizer where we know most information
21+
std::string x = s;
22+
x = replace(x, "d", "e");
23+
x = replace(x, "D", "E");
24+
return std::atof(x.c_str());
25+
}
2026

2127
static inline ASR::expr_t* EXPR(const ASR::asr_t *f)
2228
{
@@ -664,16 +670,16 @@ static inline bool extract_value(ASR::expr_t* value_expr, T& value) {
664670
value = (T) const_int->m_n;
665671
break;
666672
}
667-
case ASR::exprType::LogicalConstant: {
668-
ASR::LogicalConstant_t* const_logical = ASR::down_cast<ASR::LogicalConstant_t>(value_expr);
669-
value = (T) const_logical->m_value;
670-
break;
671-
}
672673
case ASR::exprType::RealConstant: {
673674
ASR::RealConstant_t* const_real = ASR::down_cast<ASR::RealConstant_t>(value_expr);
674675
value = (T) const_real->m_r;
675676
break;
676677
}
678+
case ASR::exprType::LogicalConstant: {
679+
ASR::LogicalConstant_t* const_logical = ASR::down_cast<ASR::LogicalConstant_t>(value_expr);
680+
value = (T) const_logical->m_value;
681+
break;
682+
}
677683
default:
678684
return false;
679685
}
@@ -768,7 +774,7 @@ static inline std::string get_type_code(const ASR::ttype_t *t, bool use_undersco
768774
}
769775
case ASR::ttypeType::Logical: {
770776
ASR::Logical_t* bool_ = ASR::down_cast<ASR::Logical_t>(t);
771-
std::string res = "bool";
777+
res = "bool";
772778
if( encode_dimensions_ ) {
773779
encode_dimensions(bool_->n_dims, res, use_underscore_sep);
774780
return res;
@@ -862,16 +868,20 @@ static inline std::string get_type_code(const ASR::ttype_t *t, bool use_undersco
862868
case ASR::ttypeType::Pointer: {
863869
ASR::Pointer_t* p = ASR::down_cast<ASR::Pointer_t>(t);
864870
if( use_underscore_sep ) {
865-
return "Pointer_" + get_type_code(p->m_type, use_underscore_sep, encode_dimensions_, set_dimensional_hint) + "_";
871+
return "Pointer_" + get_type_code(p->m_type, use_underscore_sep,
872+
encode_dimensions_, set_dimensional_hint) + "_";
866873
}
867-
return "Pointer[" + get_type_code(p->m_type, use_underscore_sep, encode_dimensions_, set_dimensional_hint) + "]";
874+
return "Pointer[" + get_type_code(p->m_type, use_underscore_sep,
875+
encode_dimensions_, set_dimensional_hint) + "]";
868876
}
869877
case ASR::ttypeType::Const: {
870878
ASR::Const_t* p = ASR::down_cast<ASR::Const_t>(t);
871879
if( use_underscore_sep ) {
872-
return "Const_" + get_type_code(p->m_type, use_underscore_sep, encode_dimensions_, set_dimensional_hint) + "_";
880+
return "Const_" + get_type_code(p->m_type, use_underscore_sep,
881+
encode_dimensions_, set_dimensional_hint) + "_";
873882
}
874-
return "Const[" + get_type_code(p->m_type, use_underscore_sep, encode_dimensions_, set_dimensional_hint) + "]";
883+
return "Const[" + get_type_code(p->m_type, use_underscore_sep,
884+
encode_dimensions_, set_dimensional_hint) + "]";
875885
}
876886
default: {
877887
throw LCompilersException("Type encoding not implemented for "
@@ -1115,7 +1125,7 @@ std::vector<std::string> determine_module_dependencies(
11151125
const ASR::TranslationUnit_t &unit);
11161126

11171127
std::vector<std::string> determine_function_definition_order(
1118-
SymbolTable* symtab);
1128+
SymbolTable* symtab);
11191129

11201130
void extract_module_python(const ASR::TranslationUnit_t &m,
11211131
std::vector<std::pair<std::string, ASR::Module_t*>>& children_modules,
@@ -1126,13 +1136,13 @@ ASR::Module_t* extract_module(const ASR::TranslationUnit_t &m);
11261136
ASR::Module_t* load_module(Allocator &al, SymbolTable *symtab,
11271137
const std::string &module_name,
11281138
const Location &loc, bool intrinsic,
1129-
const std::string &rl_path,
1139+
LCompilers::PassOptions& pass_options,
11301140
bool run_verify,
11311141
const std::function<void (const std::string &, const Location &)> err);
11321142

11331143
ASR::TranslationUnit_t* find_and_load_module(Allocator &al, const std::string &msym,
11341144
SymbolTable &symtab, bool intrinsic,
1135-
const std::string &rl_path);
1145+
LCompilers::PassOptions& pass_options);
11361146

11371147
void set_intrinsic(ASR::TranslationUnit_t* trans_unit);
11381148

@@ -1189,8 +1199,8 @@ static inline int extract_kind_from_ttype_t(const ASR::ttype_t* type) {
11891199
return extract_kind_from_ttype_t(ASR::down_cast<ASR::Pointer_t>(type)->m_type);
11901200
}
11911201
case ASR::ttypeType::Const: {
1192-
return extract_kind_from_ttype_t(ASR::down_cast<ASR::Const_t>(type)->m_type);
1193-
}
1202+
return extract_kind_from_ttype_t(ASR::down_cast<ASR::Const_t>(type)->m_type);
1203+
}
11941204
default : {
11951205
return -1;
11961206
}
@@ -1350,6 +1360,11 @@ inline int extract_dimensions_from_ttype(ASR::ttype_t *x,
13501360
return n_dims;
13511361
}
13521362

1363+
inline int extract_n_dims_from_ttype(ASR::ttype_t *x) {
1364+
ASR::dimension_t* m_dims_temp = nullptr;
1365+
return extract_dimensions_from_ttype(x, m_dims_temp);
1366+
}
1367+
13531368
// Sets the dimension member of `ttype_t`. Returns `true` if dimensions set.
13541369
// Returns `false` if the `ttype_t` does not have a dimension member.
13551370
inline bool ttype_set_dimensions(ASR::ttype_t *x,

0 commit comments

Comments
 (0)