Skip to content

Commit 0331203

Browse files
authored
Merge pull request #1481 from Shaikh-Ubaid/wasm_x64_logical_bitwise_opts
WASM_X64: Support logical/bitwise opts
2 parents fa026e1 + 6dc77b9 commit 0331203

File tree

3 files changed

+63
-15
lines changed

3 files changed

+63
-15
lines changed

integration_tests/CMakeLists.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,10 @@ RUN(NAME const_02 LABELS cpython llvm c)
235235
RUN(NAME const_03 LABELS cpython llvm c
236236
EXTRAFILES const_03b.c)
237237
RUN(NAME const_04 LABELS cpython llvm c)
238-
RUN(NAME expr_01 LABELS cpython llvm c wasm)
239-
RUN(NAME expr_02 LABELS cpython llvm c wasm)
240-
RUN(NAME expr_03 LABELS cpython llvm c wasm)
241-
RUN(NAME expr_04 LABELS cpython llvm c)
238+
RUN(NAME expr_01 LABELS cpython llvm c wasm wasm_x64)
239+
RUN(NAME expr_02 LABELS cpython llvm c wasm wasm_x64)
240+
RUN(NAME expr_03 LABELS cpython llvm c wasm wasm_x64)
241+
RUN(NAME expr_04 LABELS cpython llvm c wasm)
242242
RUN(NAME expr_05 LABELS cpython llvm c)
243243
RUN(NAME expr_06 LABELS cpython llvm c)
244244
RUN(NAME expr_07 LABELS cpython llvm c)
@@ -252,7 +252,7 @@ RUN(NAME expr_13 LABELS llvm c
252252
RUN(NAME expr_14 LABELS cpython llvm c)
253253
RUN(NAME loop_01 LABELS cpython llvm c)
254254
RUN(NAME loop_02 LABELS cpython llvm c wasm wasm_x86 wasm_x64)
255-
RUN(NAME loop_03 LABELS cpython llvm c wasm)
255+
RUN(NAME loop_03 LABELS cpython llvm c wasm wasm_x64)
256256
RUN(NAME if_01 LABELS cpython llvm c wasm wasm_x86 wasm_x64)
257257
RUN(NAME if_02 LABELS cpython llvm c wasm wasm_x86 wasm_x64)
258258
RUN(NAME print_02 LABELS cpython llvm c)
@@ -417,7 +417,7 @@ RUN(NAME func_dep_04 LABELS cpython llvm c)
417417
RUN(NAME float_01 LABELS cpython llvm wasm wasm_x64)
418418
RUN(NAME recursive_01 LABELS cpython llvm wasm wasm_x64 wasm_x86)
419419
RUN(NAME comp_01 LABELS cpython llvm wasm wasm_x64)
420-
RUN(NAME bit_operations_i32 LABELS cpython llvm wasm)
420+
RUN(NAME bit_operations_i32 LABELS cpython llvm wasm wasm_x64)
421421
RUN(NAME bit_operations_i64 LABELS cpython llvm wasm)
422422

423423
RUN(NAME test_argv_01 LABELS llvm) # TODO: Test using CPython

src/libasr/codegen/wasm_to_x64.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,8 @@ class X64Visitor : public WASMDecoder<X64Visitor>,
173173
/*
174174
From WebAssembly Docs:
175175
The exact effect of branch depends on that control construct.
176-
In case of block or if it is a forward jump, resuming execution after the matching end.
177-
In case of loop it is a backward jump to the beginning of the loop.
176+
In case of block or if, it is a forward jump, resuming execution after the matching end.
177+
In case of loop, it is a backward jump to the beginning of the loop.
178178
*/
179179
case Block::LOOP: m_a.asm_jmp_label(".loop.head_" + label); break;
180180
case Block::IF: m_a.asm_jmp_label(".else_" + label); break;
@@ -297,6 +297,18 @@ class X64Visitor : public WASMDecoder<X64Visitor>,
297297
handleI32Opt([&](){ m_a.asm_div_r64(X64Reg::rbx);});
298298
}
299299

300+
void visit_I32And() {
301+
handleI32Opt([&](){ m_a.asm_and_r64_r64(X64Reg::rax, X64Reg::rbx);});
302+
}
303+
304+
void visit_I32Or() {
305+
handleI32Opt([&](){ m_a.asm_or_r64_r64(X64Reg::rax, X64Reg::rbx);});
306+
}
307+
308+
void visit_I32Xor() {
309+
handleI32Opt([&](){ m_a.asm_xor_r64_r64(X64Reg::rax, X64Reg::rbx);});
310+
}
311+
300312
void visit_I32Eqz() {
301313
m_a.asm_mov_r64_imm64(X64Reg::rax, 0);
302314
m_a.asm_push_r64(X64Reg::rax);

src/libasr/codegen/x86_assembler.h

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -775,13 +775,6 @@ class X86Assembler {
775775
EMIT("ret");
776776
}
777777

778-
void asm_xor_r32_r32(X86Reg r32, X86Reg s32) {
779-
m_code.push_back(m_al, 0x31);
780-
modrm_sib_disp(m_code, m_al,
781-
s32, &r32, nullptr, 1, 0, false);
782-
EMIT("xor " + r2s(r32) + ", " + r2s(s32));
783-
}
784-
785778
void asm_mov_r32_imm32(X86Reg r32, uint32_t imm32) {
786779
m_code.push_back(m_al, 0xb8 + r32);
787780
push_back_uint32(m_code, m_al, imm32);
@@ -1212,6 +1205,49 @@ class X86Assembler {
12121205
EMIT("and " + r2s(r32) + ", " + i2s(imm32));
12131206
}
12141207

1208+
void asm_and_r64_r64(X64Reg r64, X64Reg s64) {
1209+
X86Reg r32 = X86Reg(r64 & 7), s32 = X86Reg(s64 & 7);
1210+
m_code.push_back(m_al, rex(1, r64 >> 3, 0, s64 >> 3));
1211+
m_code.push_back(m_al, 0x23);
1212+
modrm_sib_disp(m_code, m_al, r32, &s32, nullptr, 1, 0, false);
1213+
EMIT("and " + r2s(r64) + ", " + r2s(s64));
1214+
}
1215+
1216+
void asm_and_r32_r32(X86Reg r32, X86Reg s32) {
1217+
m_code.push_back(m_al, 0x23);
1218+
modrm_sib_disp(m_code, m_al, r32, &s32, nullptr, 1, 0, false);
1219+
EMIT("and " + r2s(r32) + ", " + r2s(r32));
1220+
}
1221+
1222+
void asm_or_r64_r64(X64Reg r64, X64Reg s64) {
1223+
X86Reg r32 = X86Reg(r64 & 7), s32 = X86Reg(s64 & 7);
1224+
m_code.push_back(m_al, rex(1, r64 >> 3, 0, s64 >> 3));
1225+
m_code.push_back(m_al, 0x0B);
1226+
modrm_sib_disp(m_code, m_al, r32, &s32, nullptr, 1, 0, false);
1227+
EMIT("or " + r2s(r64) + ", " + r2s(s64));
1228+
}
1229+
1230+
void asm_or_r32_r32(X86Reg r32, X86Reg s32) {
1231+
m_code.push_back(m_al, 0x0B);
1232+
modrm_sib_disp(m_code, m_al, r32, &s32, nullptr, 1, 0, false);
1233+
EMIT("or " + r2s(r32) + ", " + r2s(r32));
1234+
}
1235+
1236+
void asm_xor_r64_r64(X64Reg r64, X64Reg s64) {
1237+
X86Reg r32 = X86Reg(r64 & 7), s32 = X86Reg(s64 & 7);
1238+
m_code.push_back(m_al, rex(1, r64 >> 3, 0, s64 >> 3));
1239+
m_code.push_back(m_al, 0x33);
1240+
modrm_sib_disp(m_code, m_al, r32, &s32, nullptr, 1, 0, false);
1241+
EMIT("xor " + r2s(r64) + ", " + r2s(s64));
1242+
}
1243+
1244+
void asm_xor_r32_r32(X86Reg r32, X86Reg s32) {
1245+
m_code.push_back(m_al, 0x31);
1246+
modrm_sib_disp(m_code, m_al,
1247+
s32, &r32, nullptr, 1, 0, false);
1248+
EMIT("xor " + r2s(r32) + ", " + r2s(s32));
1249+
}
1250+
12151251
void asm_syscall() {
12161252
m_code.push_back(m_al, 0x0F);
12171253
m_code.push_back(m_al, 0x05);

0 commit comments

Comments
 (0)