Skip to content

Commit 2aacbb6

Browse files
authored
Merge pull request #1847 from czgdp1807/libasr_sync_03
Sync ``libasr`` with LFortran
2 parents d1990b4 + 510b416 commit 2aacbb6

File tree

226 files changed

+5991
-677
lines changed

Some content is hidden

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

226 files changed

+5991
-677
lines changed

src/bin/lpython.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ int emit_wat(const std::string &infile,
444444
LCompilers::ASR::TranslationUnit_t* asr = r1.result;
445445

446446
diagnostics.diagnostics.clear();
447-
LCompilers::Result<LCompilers::Vec<uint8_t>> r2 = LCompilers::asr_to_wasm_bytes_stream(*asr, al, diagnostics);
447+
LCompilers::Result<LCompilers::Vec<uint8_t>> r2 = LCompilers::asr_to_wasm_bytes_stream(*asr, al, diagnostics, compiler_options);
448448
std::cerr << diagnostics.render(lm, compiler_options);
449449
if (!r2.ok) {
450450
LCOMPILERS_ASSERT(diagnostics.has_error())
@@ -918,7 +918,7 @@ int compile_to_binary_wasm(
918918

919919
// ASR -> WASM
920920
auto asr_to_wasm_start = std::chrono::high_resolution_clock::now();
921-
LCompilers::Result<int> res = LCompilers::asr_to_wasm(*asr, al, outfile, time_report, diagnostics);
921+
LCompilers::Result<int> res = LCompilers::asr_to_wasm(*asr, al, outfile, time_report, diagnostics, compiler_options);
922922
auto asr_to_wasm_end = std::chrono::high_resolution_clock::now();
923923
times.push_back(std::make_pair("ASR to WASM", std::chrono::duration<double, std::milli>(asr_to_wasm_end - asr_to_wasm_start).count()));
924924
std::cerr << diagnostics.render(lm, compiler_options);
@@ -1065,7 +1065,7 @@ int compile_to_binary_wasm_to_x86(
10651065

10661066
// ASR -> WASM
10671067
auto asr_to_wasm_start = std::chrono::high_resolution_clock::now();
1068-
LCompilers::Result<LCompilers::Vec<uint8_t>> r3 = LCompilers::asr_to_wasm_bytes_stream(*asr, al, diagnostics);
1068+
LCompilers::Result<LCompilers::Vec<uint8_t>> r3 = LCompilers::asr_to_wasm_bytes_stream(*asr, al, diagnostics, compiler_options);
10691069
auto asr_to_wasm_end = std::chrono::high_resolution_clock::now();
10701070
times.push_back(std::make_pair("ASR to WASM", std::chrono::duration<double, std::milli>(asr_to_wasm_end - asr_to_wasm_start).count()));
10711071
std::cerr << diagnostics.render(lm, compiler_options);
@@ -1349,7 +1349,7 @@ EMSCRIPTEN_KEEPALIVE char* emit_wat_from_source(char *input) {
13491349
out = diagnostics.render(lm, compiler_options);
13501350
if (asr.ok) {
13511351
LCompilers::Result<LCompilers::Vec<uint8_t>>
1352-
wasm = LCompilers::asr_to_wasm_bytes_stream(*asr.result, al, diagnostics);
1352+
wasm = LCompilers::asr_to_wasm_bytes_stream(*asr.result, al, diagnostics, compiler_options);
13531353
out = diagnostics.render(lm, compiler_options);
13541354
if (wasm.ok) {
13551355
LCompilers::Result<std::string>
@@ -1414,7 +1414,7 @@ EMSCRIPTEN_KEEPALIVE char* emit_wasm_from_source(char *input) {
14141414
out = diagnostics.render(lm, compiler_options);
14151415
if (asr.ok) {
14161416
LCompilers::Result<LCompilers::Vec<uint8_t>>
1417-
wasm = LCompilers::asr_to_wasm_bytes_stream(*asr.result, al, diagnostics);
1417+
wasm = LCompilers::asr_to_wasm_bytes_stream(*asr.result, al, diagnostics, compiler_options);
14181418
out = diagnostics.render(lm, compiler_options);
14191419
if (wasm.ok) {
14201420
out = "0"; // exit code

src/libasr/ASR.asdl

+3-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ symbol
103103
| UnionType(symbol_table symtab, identifier name, identifier* dependencies,
104104
identifier* members, abi abi, access access, symbol? parent)
105105
| Variable(symbol_table parent_symtab, identifier name, identifier* dependencies,
106-
intent intent, expr? symbolic_value, expr? value, storage_type storage, ttype type,
106+
intent intent, expr? symbolic_value, expr? value, storage_type storage,
107+
ttype type, symbol? type_declaration,
107108
abi abi, access access, presence presence, bool value_attr)
108109
| ClassType(symbol_table symtab, identifier name, abi abi, access access)
109110
| ClassProcedure(symbol_table parent_symtab, identifier name, identifier? self_argument,
@@ -284,6 +285,7 @@ expr
284285
| DictLen(expr arg, ttype type, expr? value)
285286

286287
| Var(symbol v)
288+
| FunctionParam(int param_number, ttype type, expr? value) --- used in types
287289

288290
| ArrayConstant(expr* args, ttype type, arraystorage storage_format)
289291
| ArrayItem(expr v, array_index* args, ttype type, arraystorage storage_format, expr? value)

src/libasr/CMakeLists.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ set(SRC
2828
codegen/wasm_utils.cpp
2929

3030
pass/nested_vars.cpp
31+
pass/where.cpp
3132
pass/param_to_const.cpp
3233
pass/do_loops.cpp
3334
pass/for_all.cpp
@@ -90,7 +91,7 @@ if (WITH_LLVM)
9091
COMPILE_FLAGS -Wno-deprecated-declarations)
9192
endif()
9293
endif()
93-
add_library(asr ${SRC})
94+
add_library(asr STATIC ${SRC})
9495
target_include_directories(asr BEFORE PUBLIC ${libasr_SOURCE_DIR}/..)
9596
target_include_directories(asr BEFORE PUBLIC ${libasr_BINARY_DIR}/..)
9697
if (WITH_BFD)

src/libasr/asdl_cpp.py

+50-8
Original file line numberDiff line numberDiff line change
@@ -1169,7 +1169,9 @@ class ExprBaseReplacerVisitor(ASDLVisitor):
11691169

11701170
def __init__(self, stream, data):
11711171
self.replace_expr = []
1172+
self.replace_ttype = []
11721173
self.is_expr = False
1174+
self.is_ttype = False
11731175
self.is_product = False
11741176
self.current_expr_copy_variable_count = 0
11751177
super(ExprBaseReplacerVisitor, self).__init__(stream, data)
@@ -1195,16 +1197,32 @@ def visitModule(self, mod):
11951197
self.replace_expr.append(("", 0))
11961198
self.replace_expr.append((" switch(x->type) {", 1))
11971199

1200+
self.replace_ttype.append((" void replace_ttype(ASR::ttype_t* x) {", 0))
1201+
self.replace_ttype.append((" if( !x ) {", 1))
1202+
self.replace_ttype.append((" return ;", 2))
1203+
self.replace_ttype.append((" }", 1))
1204+
self.replace_ttype.append(("", 0))
1205+
self.replace_ttype.append((" switch(x->type) {", 1))
1206+
11981207
super(ExprBaseReplacerVisitor, self).visitModule(mod)
11991208

12001209
self.replace_expr.append((" default: {", 2))
1201-
self.replace_expr.append((' LCOMPILERS_ASSERT_MSG(false, "Duplication of " + std::to_string(x->type) + " expression is not supported yet.");', 3))
1210+
self.replace_expr.append((' LCOMPILERS_ASSERT_MSG(false, "Replacement in " + std::to_string(x->type) + " expression is not supported yet.");', 3))
12021211
self.replace_expr.append((" }", 2))
12031212
self.replace_expr.append((" }", 1))
12041213
self.replace_expr.append(("", 0))
12051214
self.replace_expr.append((" }", 0))
1215+
1216+
self.replace_ttype.append((" default: {", 2))
1217+
self.replace_ttype.append((' LCOMPILERS_ASSERT_MSG(false, "Replacement in " + std::to_string(x->type) + " type is not supported yet.");', 3))
1218+
self.replace_ttype.append((" }", 2))
1219+
self.replace_ttype.append((" }", 1))
1220+
self.replace_ttype.append(("", 0))
1221+
self.replace_ttype.append((" }", 0))
12061222
for line, level in self.replace_expr:
12071223
self.emit(line, level=level)
1224+
for line, level in self.replace_ttype:
1225+
self.emit(line, level=level)
12081226
self.emit("")
12091227
self.emit("};")
12101228

@@ -1215,7 +1233,8 @@ def visitType(self, tp):
12151233

12161234
def visitSum(self, sum, *args):
12171235
self.is_expr = args[0] == 'expr'
1218-
if self.is_expr:
1236+
self.is_ttype = args[0] == 'ttype'
1237+
if self.is_expr or self.is_ttype:
12191238
for tp in sum.types:
12201239
self.visit(tp, *args)
12211240

@@ -1239,12 +1258,21 @@ def make_visitor(self, name, fields):
12391258
self.replace_expr.append((" self().replace_%s(down_cast<ASR::%s_t>(x));" % (name, name), 3))
12401259
self.replace_expr.append((" break;", 3))
12411260
self.replace_expr.append((" }", 2))
1261+
elif self.is_ttype:
1262+
self.replace_ttype.append((" case ASR::ttypeType::%s: {" % name, 2))
1263+
self.replace_ttype.append((" self().replace_%s(down_cast<ASR::%s_t>(x));" % (name, name), 3))
1264+
self.replace_ttype.append((" break;", 3))
1265+
self.replace_ttype.append((" }", 2))
12421266
self.emit("}", 1)
12431267
self.emit("")
12441268

12451269
def visitField(self, field):
12461270
arguments = None
1247-
if field.type == "expr" or field.type == "symbol" or field.type == "call_arg":
1271+
if (field.type == "expr" or
1272+
field.type == "symbol" or
1273+
field.type == "call_arg" or
1274+
field.type == "ttype" or
1275+
field.type == "dimension"):
12481276
level = 2
12491277
if field.seq:
12501278
self.used = True
@@ -1257,15 +1285,29 @@ def visitField(self, field):
12571285
self.emit(" current_expr = current_expr_copy_%d;" % (self.current_expr_copy_variable_count), level + 1)
12581286
self.emit(" }", level)
12591287
self.current_expr_copy_variable_count += 1
1288+
elif field.type == "dimension":
1289+
self.emit(" ASR::expr_t** current_expr_copy_%d = current_expr;" % (self.current_expr_copy_variable_count), level)
1290+
self.emit(" current_expr = &(x->m_%s[i].m_length);" % (field.name), level)
1291+
self.emit(" self().replace_expr(x->m_%s[i].m_length);"%(field.name), level)
1292+
self.emit(" current_expr = current_expr_copy_%d;" % (self.current_expr_copy_variable_count), level)
1293+
self.current_expr_copy_variable_count += 1
1294+
self.emit(" ASR::expr_t** current_expr_copy_%d = current_expr;" % (self.current_expr_copy_variable_count), level)
1295+
self.emit(" current_expr = &(x->m_%s[i].m_start);" % (field.name), level)
1296+
self.emit(" self().replace_expr(x->m_%s[i].m_start);"%(field.name), level)
1297+
self.emit(" current_expr = current_expr_copy_%d;" % (self.current_expr_copy_variable_count), level)
1298+
self.current_expr_copy_variable_count += 1
12601299
self.emit("}", level)
12611300
else:
12621301
if field.type != "symbol":
12631302
self.used = True
1264-
self.emit("ASR::expr_t** current_expr_copy_%d = current_expr;" % (self.current_expr_copy_variable_count), level)
1265-
self.emit("current_expr = &(x->m_%s);" % (field.name), level)
1266-
self.emit("self().replace_%s(x->m_%s);" % (field.type, field.name), level)
1267-
self.emit("current_expr = current_expr_copy_%d;" % (self.current_expr_copy_variable_count), level)
1268-
self.current_expr_copy_variable_count += 1
1303+
if field.type == "ttype":
1304+
self.emit("self().replace_%s(x->m_%s);" % (field.type, field.name), level)
1305+
else:
1306+
self.emit("ASR::expr_t** current_expr_copy_%d = current_expr;" % (self.current_expr_copy_variable_count), level)
1307+
self.emit("current_expr = &(x->m_%s);" % (field.name), level)
1308+
self.emit("self().replace_%s(x->m_%s);" % (field.type, field.name), level)
1309+
self.emit("current_expr = current_expr_copy_%d;" % (self.current_expr_copy_variable_count), level)
1310+
self.current_expr_copy_variable_count += 1
12691311

12701312
class StmtBaseReplacerVisitor(ASDLVisitor):
12711313

src/libasr/asr_scopes.cpp

+5-50
Original file line numberDiff line numberDiff line change
@@ -6,55 +6,6 @@
66

77
namespace LCompilers {
88

9-
// This function is taken from:
10-
// https://github.com/aappleby/smhasher/blob/61a0530f28277f2e850bfc39600ce61d02b518de/src/MurmurHash2.cpp#L37
11-
uint32_t murmur_hash(const void * key, int len, uint32_t seed)
12-
{
13-
// 'm' and 'r' are mixing constants generated offline.
14-
// They're not really 'magic', they just happen to work well.
15-
const uint32_t m = 0x5bd1e995;
16-
const int r = 24;
17-
// Initialize the hash to a 'random' value
18-
uint32_t h = seed ^ len;
19-
// Mix 4 bytes at a time into the hash
20-
const unsigned char * data = (const unsigned char *)key;
21-
while(len >= 4)
22-
{
23-
uint32_t k = *(uint32_t*)data;
24-
k *= m;
25-
k ^= k >> r;
26-
k *= m;
27-
h *= m;
28-
h ^= k;
29-
data += 4;
30-
len -= 4;
31-
}
32-
// Handle the last few bytes of the input array
33-
switch(len)
34-
{
35-
case 3: h ^= data[2] << 16; // fall through
36-
case 2: h ^= data[1] << 8; // fall through
37-
case 1: h ^= data[0];
38-
h *= m;
39-
};
40-
// Do a few final mixes of the hash to ensure the last few
41-
// bytes are well-incorporated.
42-
h ^= h >> 13;
43-
h *= m;
44-
h ^= h >> 15;
45-
return h;
46-
}
47-
48-
uint32_t murmur_hash_str(const std::string &s, uint32_t seed)
49-
{
50-
return murmur_hash(&s[0], s.length(), seed);
51-
}
52-
53-
uint32_t murmur_hash_int(uint64_t i, uint32_t seed)
54-
{
55-
return murmur_hash(&i, 8, seed);
56-
}
57-
589
template< typename T >
5910
std::string hexify(T i)
6011
{
@@ -75,7 +26,7 @@ void SymbolTable::reset_global_counter() {
7526
symbol_table_counter = 0;
7627
}
7728

78-
void SymbolTable::mark_all_variables_external(Allocator &/*al*/) {
29+
void SymbolTable::mark_all_variables_external(Allocator &al) {
7930
for (auto &a : scope) {
8031
switch (a.second->type) {
8132
case (ASR::symbolType::Variable) : {
@@ -91,6 +42,10 @@ void SymbolTable::mark_all_variables_external(Allocator &/*al*/) {
9142
v->n_body = 0;
9243
break;
9344
}
45+
case (ASR::symbolType::Module) : {
46+
ASR::Module_t *v = ASR::down_cast<ASR::Module_t>(a.second);
47+
v->m_symtab->mark_all_variables_external(al);
48+
}
9449
default : {};
9550
}
9651
}

0 commit comments

Comments
 (0)