Thanks to visit codestin.com
Credit goes to github.com

Skip to content

WASM_X64: Support logical/bitwise opts #1481

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 5 commits into from
Jan 31, 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
12 changes: 6 additions & 6 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,10 @@ RUN(NAME const_02 LABELS cpython llvm c)
RUN(NAME const_03 LABELS cpython llvm c
EXTRAFILES const_03b.c)
RUN(NAME const_04 LABELS cpython llvm c)
RUN(NAME expr_01 LABELS cpython llvm c wasm)
RUN(NAME expr_02 LABELS cpython llvm c wasm)
RUN(NAME expr_03 LABELS cpython llvm c wasm)
RUN(NAME expr_04 LABELS cpython llvm c)
RUN(NAME expr_01 LABELS cpython llvm c wasm wasm_x64)
RUN(NAME expr_02 LABELS cpython llvm c wasm wasm_x64)
RUN(NAME expr_03 LABELS cpython llvm c wasm wasm_x64)
RUN(NAME expr_04 LABELS cpython llvm c wasm)
RUN(NAME expr_05 LABELS cpython llvm c)
RUN(NAME expr_06 LABELS cpython llvm c)
RUN(NAME expr_07 LABELS cpython llvm c)
Expand All @@ -252,7 +252,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 wasm_x64)
RUN(NAME loop_03 LABELS cpython llvm c wasm)
RUN(NAME loop_03 LABELS cpython llvm c wasm wasm_x64)
RUN(NAME if_01 LABELS cpython llvm c wasm wasm_x86 wasm_x64)
RUN(NAME if_02 LABELS cpython llvm c wasm wasm_x86 wasm_x64)
RUN(NAME print_02 LABELS cpython llvm c)
Expand Down Expand Up @@ -417,7 +417,7 @@ RUN(NAME func_dep_04 LABELS cpython llvm c)
RUN(NAME float_01 LABELS cpython llvm wasm wasm_x64)
RUN(NAME recursive_01 LABELS cpython llvm wasm wasm_x64 wasm_x86)
RUN(NAME comp_01 LABELS cpython llvm wasm wasm_x64)
RUN(NAME bit_operations_i32 LABELS cpython llvm wasm)
RUN(NAME bit_operations_i32 LABELS cpython llvm wasm wasm_x64)
RUN(NAME bit_operations_i64 LABELS cpython llvm wasm)

RUN(NAME test_argv_01 LABELS llvm) # TODO: Test using CPython
16 changes: 14 additions & 2 deletions src/libasr/codegen/wasm_to_x64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ class X64Visitor : public WASMDecoder<X64Visitor>,
/*
From WebAssembly Docs:
The exact effect of branch depends on that control construct.
In case of block or if it is a forward jump, resuming execution after the matching end.
In case of loop it is a backward jump to the beginning of the loop.
In case of block or if, it is a forward jump, resuming execution after the matching end.
In case of loop, it is a backward jump to the beginning of the loop.
*/
case Block::LOOP: m_a.asm_jmp_label(".loop.head_" + label); break;
case Block::IF: m_a.asm_jmp_label(".else_" + label); break;
Expand Down Expand Up @@ -297,6 +297,18 @@ class X64Visitor : public WASMDecoder<X64Visitor>,
handleI32Opt([&](){ m_a.asm_div_r64(X64Reg::rbx);});
}

void visit_I32And() {
handleI32Opt([&](){ m_a.asm_and_r64_r64(X64Reg::rax, X64Reg::rbx);});
}

void visit_I32Or() {
handleI32Opt([&](){ m_a.asm_or_r64_r64(X64Reg::rax, X64Reg::rbx);});
}

void visit_I32Xor() {
handleI32Opt([&](){ m_a.asm_xor_r64_r64(X64Reg::rax, X64Reg::rbx);});
}

void visit_I32Eqz() {
m_a.asm_mov_r64_imm64(X64Reg::rax, 0);
m_a.asm_push_r64(X64Reg::rax);
Expand Down
50 changes: 43 additions & 7 deletions src/libasr/codegen/x86_assembler.h
Original file line number Diff line number Diff line change
Expand Up @@ -775,13 +775,6 @@ class X86Assembler {
EMIT("ret");
}

void asm_xor_r32_r32(X86Reg r32, X86Reg s32) {
m_code.push_back(m_al, 0x31);
modrm_sib_disp(m_code, m_al,
s32, &r32, nullptr, 1, 0, false);
EMIT("xor " + r2s(r32) + ", " + r2s(s32));
}

void asm_mov_r32_imm32(X86Reg r32, uint32_t imm32) {
m_code.push_back(m_al, 0xb8 + r32);
push_back_uint32(m_code, m_al, imm32);
Expand Down Expand Up @@ -1212,6 +1205,49 @@ class X86Assembler {
EMIT("and " + r2s(r32) + ", " + i2s(imm32));
}

void asm_and_r64_r64(X64Reg r64, X64Reg s64) {
X86Reg r32 = X86Reg(r64 & 7), s32 = X86Reg(s64 & 7);
m_code.push_back(m_al, rex(1, r64 >> 3, 0, s64 >> 3));
m_code.push_back(m_al, 0x23);
modrm_sib_disp(m_code, m_al, r32, &s32, nullptr, 1, 0, false);
EMIT("and " + r2s(r64) + ", " + r2s(s64));
}

void asm_and_r32_r32(X86Reg r32, X86Reg s32) {
m_code.push_back(m_al, 0x23);
modrm_sib_disp(m_code, m_al, r32, &s32, nullptr, 1, 0, false);
EMIT("and " + r2s(r32) + ", " + r2s(r32));
}

void asm_or_r64_r64(X64Reg r64, X64Reg s64) {
X86Reg r32 = X86Reg(r64 & 7), s32 = X86Reg(s64 & 7);
m_code.push_back(m_al, rex(1, r64 >> 3, 0, s64 >> 3));
m_code.push_back(m_al, 0x0B);
modrm_sib_disp(m_code, m_al, r32, &s32, nullptr, 1, 0, false);
EMIT("or " + r2s(r64) + ", " + r2s(s64));
}

void asm_or_r32_r32(X86Reg r32, X86Reg s32) {
m_code.push_back(m_al, 0x0B);
modrm_sib_disp(m_code, m_al, r32, &s32, nullptr, 1, 0, false);
EMIT("or " + r2s(r32) + ", " + r2s(r32));
}

void asm_xor_r64_r64(X64Reg r64, X64Reg s64) {
X86Reg r32 = X86Reg(r64 & 7), s32 = X86Reg(s64 & 7);
m_code.push_back(m_al, rex(1, r64 >> 3, 0, s64 >> 3));
m_code.push_back(m_al, 0x33);
modrm_sib_disp(m_code, m_al, r32, &s32, nullptr, 1, 0, false);
EMIT("xor " + r2s(r64) + ", " + r2s(s64));
}

void asm_xor_r32_r32(X86Reg r32, X86Reg s32) {
m_code.push_back(m_al, 0x31);
modrm_sib_disp(m_code, m_al,
s32, &r32, nullptr, 1, 0, false);
EMIT("xor " + r2s(r32) + ", " + r2s(s32));
}

void asm_syscall() {
m_code.push_back(m_al, 0x0F);
m_code.push_back(m_al, 0x05);
Expand Down