Skip to content

WASM_X64: Support if else #1390

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 3 commits into from
Jan 2, 2023
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
2 changes: 1 addition & 1 deletion integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions src/libasr/codegen/asr_to_wasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2102,9 +2102,9 @@ class ASRToWASMVisitor : public ASR::BaseVisitor<ASRToWASMVisitor> {

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<ASR::StringConstant_t>(x.m_msg)->m_s;
Expand All @@ -2114,7 +2114,6 @@ class ASRToWASMVisitor : public ASR::BaseVisitor<ASRToWASMVisitor> {
}
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
}
};
Expand Down
23 changes: 23 additions & 0 deletions src/libasr/codegen/wasm_to_x64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class X64Visitor : public WASMDecoder<X64Visitor>,
uint32_t cur_func_idx;
int32_t last_vis_i32_const, last_last_vis_i32_const;
std::map<std::string, std::string> label_to_str;
std::vector<std::string> if_unique_id;


X64Visitor(X86Assembler &m_a, Allocator &al,
diag::Diagnostics &diagonostics, Vec<uint8_t> &code)
Expand All @@ -47,6 +49,8 @@ class X64Visitor : public WASMDecoder<X64Visitor>,

void visit_Unreachable() {}

void visit_EmtpyBlockType() {}

void call_imported_function(uint32_t func_idx) {
switch (func_idx) {
case 0: { // print_i32
Expand Down Expand Up @@ -122,6 +126,25 @@ class X64Visitor : public WASMDecoder<X64Visitor>,
}
}

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]];
Expand Down