From 3ad0c2cfff094cea8e708b687af69376b30dc892 Mon Sep 17 00:00:00 2001 From: Shaikh Ubaid Date: Mon, 2 Jan 2023 08:43:02 +0530 Subject: [PATCH 1/3] WASM: make Assert similar to LLVM --- src/libasr/codegen/asr_to_wasm.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/libasr/codegen/asr_to_wasm.cpp b/src/libasr/codegen/asr_to_wasm.cpp index 2ba10f0dbd..2010bcf69f 100644 --- a/src/libasr/codegen/asr_to_wasm.cpp +++ b/src/libasr/codegen/asr_to_wasm.cpp @@ -2102,9 +2102,9 @@ class ASRToWASMVisitor : public ASR::BaseVisitor { void visit_Assert(const ASR::Assert_t &x) { this->visit_expr(*x.m_test); - wasm::emit_i32_eqz(m_code_section, m_al); wasm::emit_b8(m_code_section, m_al, 0x04); // emit if start wasm::emit_b8(m_code_section, m_al, 0x40); // empty block type + wasm::emit_b8(m_code_section, m_al, 0x05); // starting of else if (x.m_msg) { std::string msg = ASR::down_cast(x.m_msg)->m_s; @@ -2114,7 +2114,6 @@ class ASRToWASMVisitor : public ASR::BaseVisitor { } wasm::emit_i32_const(m_code_section, m_al, 1); // non-zero exit code exit(); - wasm::emit_b8(m_code_section, m_al, 0x05); // starting of else wasm::emit_expr_end(m_code_section, m_al); // emit if end } }; From a427089ac62b1124ed41ba55cb35706df0ae5689 Mon Sep 17 00:00:00 2001 From: Shaikh Ubaid Date: Mon, 2 Jan 2023 08:43:26 +0530 Subject: [PATCH 2/3] WASM_X64: Support if-else --- src/libasr/codegen/wasm_to_x64.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/libasr/codegen/wasm_to_x64.cpp b/src/libasr/codegen/wasm_to_x64.cpp index 598088de3a..e08a412899 100644 --- a/src/libasr/codegen/wasm_to_x64.cpp +++ b/src/libasr/codegen/wasm_to_x64.cpp @@ -34,6 +34,8 @@ class X64Visitor : public WASMDecoder, uint32_t cur_func_idx; int32_t last_vis_i32_const, last_last_vis_i32_const; std::map label_to_str; + std::vector if_unique_id; + X64Visitor(X86Assembler &m_a, Allocator &al, diag::Diagnostics &diagonostics, Vec &code) @@ -47,6 +49,8 @@ class X64Visitor : public WASMDecoder, void visit_Unreachable() {} + void visit_EmtpyBlockType() {} + void call_imported_function(uint32_t func_idx) { switch (func_idx) { case 0: { // print_i32 @@ -122,6 +126,25 @@ class X64Visitor : public WASMDecoder, } } + void visit_If() { + if_unique_id.push_back(std::to_string(offset)); + m_a.asm_pop_r64(X64Reg::rax); // now `rax` contains the logical value (true = 1, false = 0) of the if condition + m_a.asm_cmp_r64_imm8(X64Reg::rax, 1); + m_a.asm_je_label(".then_" + if_unique_id.back()); + m_a.asm_jmp_label(".else_" + if_unique_id.back()); + m_a.add_label(".then_" + if_unique_id.back()); + { + decode_instructions(); + } + m_a.add_label(".endif_" + if_unique_id.back()); + if_unique_id.pop_back(); + } + + void visit_Else() { + m_a.asm_jmp_label(".endif_" + if_unique_id.back()); + m_a.add_label(".else_" + if_unique_id.back()); + } + void visit_LocalGet(uint32_t localidx) { X64Reg base = X64Reg::rbp; auto cur_func_param_type = func_types[type_indices[cur_func_idx]]; From 87fc74a3919d8e179fba3d4e2603a5a20d1af9b0 Mon Sep 17 00:00:00 2001 From: Shaikh Ubaid Date: Mon, 2 Jan 2023 08:44:27 +0530 Subject: [PATCH 3/3] TEST: WASM_X64: Enable support test for if --- integration_tests/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/integration_tests/CMakeLists.txt b/integration_tests/CMakeLists.txt index 6c5371220b..80dec07248 100644 --- a/integration_tests/CMakeLists.txt +++ b/integration_tests/CMakeLists.txt @@ -241,7 +241,7 @@ RUN(NAME expr_13 LABELS llvm c RUN(NAME expr_14 LABELS cpython llvm c) RUN(NAME loop_01 LABELS cpython llvm c) RUN(NAME loop_02 LABELS cpython llvm c wasm wasm_x86) -RUN(NAME if_01 LABELS cpython llvm c wasm wasm_x86) +RUN(NAME if_01 LABELS cpython llvm c wasm wasm_x86 wasm_x64) RUN(NAME if_02 LABELS cpython llvm c wasm wasm_x86) RUN(NAME print_02 LABELS cpython llvm) RUN(NAME test_types_01 LABELS cpython llvm c)