Skip to content

Commit 9993dcc

Browse files
committed
Insert changes from LFortran
1 parent 51e94cc commit 9993dcc

File tree

4 files changed

+51
-9
lines changed

4 files changed

+51
-9
lines changed

src/libasr/asr_verify.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,8 @@ class VerifyVisitor : public BaseWalkVisitor<VerifyVisitor>
268268
ASR::Var_t* target_Var = ASR::down_cast<ASR::Var_t>(target);
269269
ASR::ttype_t* target_type = nullptr;
270270
if( ASR::is_a<ASR::Variable_t>(*target_Var->m_v) ||
271-
ASR::is_a<ASR::ExternalSymbol_t>(*target_Var->m_v)) {
271+
(ASR::is_a<ASR::ExternalSymbol_t>(*target_Var->m_v) &&
272+
ASR::down_cast<ASR::ExternalSymbol_t>(target_Var->m_v)->m_external) ) {
272273
target_type = ASRUtils::expr_type(target);
273274
}
274275
if( target_type && ASR::is_a<ASR::Const_t>(*target_type) ) {

src/libasr/codegen/asr_to_wasm.cpp

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,40 @@ class ASRToWASMVisitor : public ASR::BaseVisitor<ASRToWASMVisitor> {
282282
for (auto wasi_func:wasi_imports_funcs) {
283283
import_function(wasi_func, "wasi_snapshot_preview1");
284284
}
285+
286+
// In WASM: The indices of the imports precede the indices of other
287+
// definitions in the same index space. Therefore, declare the import
288+
// functions before defined functions
289+
for (auto &item : global_scope->get_scope()) {
290+
if (ASR::is_a<ASR::Program_t>(*item.second)) {
291+
ASR::Program_t *p = ASR::down_cast<ASR::Program_t>(item.second);
292+
for (auto &item : p->m_symtab->get_scope()) {
293+
if (ASR::is_a<ASR::Function_t>(*item.second)) {
294+
ASR::Function_t *fn =
295+
ASR::down_cast<ASR::Function_t>(item.second);
296+
if (ASRUtils::get_FunctionType(fn)->m_abi == ASR::abiType::BindC &&
297+
ASRUtils::get_FunctionType(fn)->m_deftype == ASR::deftypeType::Interface &&
298+
!ASRUtils::is_intrinsic_function2(fn)) {
299+
wasm::emit_import_fn(m_import_section, m_al, "js",
300+
fn->m_name, no_of_types);
301+
no_of_imports++;
302+
emit_function_prototype(*fn);
303+
}
304+
}
305+
}
306+
} else if (ASR::is_a<ASR::Function_t>(*item.second)) {
307+
ASR::Function_t *fn =
308+
ASR::down_cast<ASR::Function_t>(item.second);
309+
if (ASRUtils::get_FunctionType(fn)->m_abi == ASR::abiType::BindC &&
310+
ASRUtils::get_FunctionType(fn)->m_deftype == ASR::deftypeType::Interface &&
311+
!ASRUtils::is_intrinsic_function2(fn)) {
312+
wasm::emit_import_fn(m_import_section, m_al, "js",
313+
fn->m_name, no_of_types);
314+
no_of_imports++;
315+
emit_function_prototype(*fn);
316+
}
317+
}
318+
}
285319
}
286320

287321
void emit_if_else(std::function<void()> test_cond, std::function<void()> if_block, std::function<void()> else_block) {
@@ -490,7 +524,7 @@ class ASRToWASMVisitor : public ASR::BaseVisitor<ASRToWASMVisitor> {
490524

491525
wasm::emit_get_local(m_code_section, m_al, 0);
492526
wasm::emit_i64_trunc_f64_s(m_code_section, m_al);
493-
wasm::emit_call(m_code_section, m_al, 2 /* print_i64 */);
527+
wasm::emit_call(m_code_section, m_al, no_of_imports /* print_i64 */);
494528
emit_call_fd_write(1, ".", 1, 0);
495529

496530
wasm::emit_get_local(m_code_section, m_al, 0);
@@ -541,7 +575,7 @@ class ASRToWASMVisitor : public ASR::BaseVisitor<ASRToWASMVisitor> {
541575
});
542576

543577
wasm::emit_get_local(m_code_section, m_al, 3);
544-
wasm::emit_call(m_code_section, m_al, 2 /* print_i64 */);
578+
wasm::emit_call(m_code_section, m_al, no_of_imports /* print_i64 */);
545579

546580
wasm::emit_b8(m_code_section, m_al, 0x0F); // emit wasm return instruction
547581
wasm::emit_expr_end(m_code_section, m_al);
@@ -2334,11 +2368,11 @@ class ASRToWASMVisitor : public ASR::BaseVisitor<ASRToWASMVisitor> {
23342368
switch (a_kind) {
23352369
case 4: {
23362370
wasm::emit_i64_extend_i32_s(m_code_section, m_al);
2337-
wasm::emit_call(m_code_section, m_al, 2 /* print_i64 */);
2371+
wasm::emit_call(m_code_section, m_al, no_of_imports /* print_i64 */);
23382372
break;
23392373
}
23402374
case 8: {
2341-
wasm::emit_call(m_code_section, m_al, 2 /* print_i64 */);
2375+
wasm::emit_call(m_code_section, m_al, no_of_imports /* print_i64 */);
23422376
break;
23432377
}
23442378
default: {
@@ -2352,11 +2386,11 @@ class ASRToWASMVisitor : public ASR::BaseVisitor<ASRToWASMVisitor> {
23522386
switch (a_kind) {
23532387
case 4: {
23542388
wasm::emit_f64_promote_f32(m_code_section, m_al);
2355-
wasm::emit_call(m_code_section, m_al, 3 /* print_f64 */);
2389+
wasm::emit_call(m_code_section, m_al, no_of_imports + 1 /* print_f64 */);
23562390
break;
23572391
}
23582392
case 8: {
2359-
wasm::emit_call(m_code_section, m_al, 3 /* print_f64 */);
2393+
wasm::emit_call(m_code_section, m_al, no_of_imports + 1 /* print_f64 */);
23602394
break;
23612395
}
23622396
default: {

src/libasr/codegen/wasm_assembler.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,12 @@ R"(async function main() {
879879
const fs = require("fs");
880880
const { WASI } = require("wasi");
881881
const wasi = new WASI();
882-
const importObject = { wasi_snapshot_preview1: wasi.wasiImport };
882+
const importObject = {
883+
wasi_snapshot_preview1: wasi.wasiImport,
884+
js: {
885+
cpu_time: (time) => (Date.now() / 1000) // Date.now() returns milliseconds, so divide by 1000
886+
}
887+
};
883888
const wasm = await WebAssembly.compile(fs.readFileSync(")" + filename + R"("));
884889
const instance = await WebAssembly.instantiate(wasm, importObject);
885890
wasi.start(instance);

src/libasr/compiler_tester/tester.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,9 @@ def tester_main(compiler, single_test):
409409
specific_backends=specific_backends,
410410
excluded_backends=excluded_backends,
411411
verbose=verbose,
412-
no_llvm=no_llvm)
412+
no_llvm=no_llvm,
413+
skip_run_with_dbg=True,
414+
no_color=True)
413415
filtered_tests = [test for test in filtered_tests if 'extrafiles' not in test]
414416

415417
if args.sequential:

0 commit comments

Comments
 (0)