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

Skip to content

Commit aa90263

Browse files
authored
Merge pull request #1360 from Shaikh-Ubaid/wasm_x64_int_operations
WASM_X64: Support Integer Operations
2 parents 0a0d710 + 409a773 commit aa90263

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

src/libasr/codegen/wasm_to_x64.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,31 @@ class X64Visitor : public WASMDecoder<X64Visitor>,
106106
m_a.asm_push_r64(X64Reg::rax);
107107
}
108108

109+
void visit_I32Add() {
110+
m_a.asm_pop_r64(X64Reg::rbx);
111+
m_a.asm_pop_r64(X64Reg::rax);
112+
m_a.asm_add_r64_r64(X64Reg::rax, X64Reg::rbx);
113+
m_a.asm_push_r64(X64Reg::rax);
114+
}
115+
void visit_I32Sub() {
116+
m_a.asm_pop_r64(X64Reg::rbx);
117+
m_a.asm_pop_r64(X64Reg::rax);
118+
m_a.asm_sub_r64_r64(X64Reg::rax, X64Reg::rbx);
119+
m_a.asm_push_r64(X64Reg::rax);
120+
}
121+
void visit_I32Mul() {
122+
m_a.asm_pop_r64(X64Reg::rbx);
123+
m_a.asm_pop_r64(X64Reg::rax);
124+
m_a.asm_mul_r64(X64Reg::rbx);
125+
m_a.asm_push_r64(X64Reg::rax);
126+
}
127+
void visit_I32DivS() {
128+
m_a.asm_pop_r64(X64Reg::rbx);
129+
m_a.asm_pop_r64(X64Reg::rax);
130+
m_a.asm_div_r64(X64Reg::rbx);
131+
m_a.asm_push_r64(X64Reg::rax);
132+
}
133+
109134
void gen_x64_bytes() {
110135
{ // Initialize/Modify values of entities for code simplicity later
111136

src/libasr/codegen/x86_assembler.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,15 @@ class X86Assembler {
736736
EMIT("sub " + r2s(r32) + ", " + i2s(imm8));
737737
}
738738

739+
void asm_sub_r64_r64(X64Reg r64, X64Reg s64) {
740+
X86Reg r32 = X86Reg(r64 & 7), s32 = X86Reg(s64 & 7);
741+
m_code.push_back(m_al, rex(1, r64 >> 3, 0, s64 >> 3));
742+
m_code.push_back(m_al, 0x29);
743+
modrm_sib_disp(m_code, m_al,
744+
s32, &r32, nullptr, 1, 0, false);
745+
EMIT("sub " + r2s(r64) + ", " + r2s(s64));
746+
}
747+
739748
void asm_sub_r32_r32(X86Reg r32, X86Reg s32) {
740749
m_code.push_back(m_al, 0x29);
741750
modrm_sib_disp(m_code, m_al,
@@ -865,6 +874,15 @@ class X86Assembler {
865874
EMIT("add " + m2s(base, index, scale, disp) + ", " + r2s(r32));
866875
}
867876

877+
void asm_add_r64_r64(X64Reg s64, X64Reg r64) {
878+
X86Reg r32 = X86Reg(r64 & 7), s32 = X86Reg(s64 & 7);
879+
m_code.push_back(m_al, rex(1, s64 >> 3, 0, r64 >> 3));
880+
m_code.push_back(m_al, 0x01);
881+
modrm_sib_disp(m_code, m_al,
882+
r32, &s32, nullptr, 1, 0, false);
883+
EMIT("add " + r2s(s64) + ", " + r2s(r64));
884+
}
885+
868886
void asm_add_r32_r32(X86Reg s32, X86Reg r32) {
869887
m_code.push_back(m_al, 0x01);
870888
modrm_sib_disp(m_code, m_al,
@@ -888,13 +906,31 @@ class X86Assembler {
888906
EMIT("add " + r2s(r32) + ", " + i2s(imm32));
889907
}
890908

909+
void asm_mul_r64(X64Reg r64) {
910+
X86Reg r32 = X86Reg(r64 & 7);
911+
m_code.push_back(m_al, rex(1, 0, 0, r64 >> 3));
912+
m_code.push_back(m_al, 0xF7);
913+
modrm_sib_disp(m_code, m_al,
914+
X86Reg::esp, &r32, nullptr, 1, 0, false);
915+
EMIT("mul " + r2s(r64));
916+
}
917+
891918
void asm_mul_r32(X86Reg r32) {
892919
m_code.push_back(m_al, 0xF7);
893920
modrm_sib_disp(m_code, m_al,
894921
X86Reg::esp, &r32, nullptr, 1, 0, false);
895922
EMIT("mul " + r2s(r32));
896923
}
897924

925+
void asm_div_r64(X64Reg r64) {
926+
X86Reg r32 = X86Reg(r64 & 7);
927+
m_code.push_back(m_al, rex(1, 0, 0, r64 >> 3));
928+
m_code.push_back(m_al, 0xF7);
929+
modrm_sib_disp(m_code, m_al,
930+
X86Reg::esi, &r32, nullptr, 1, 0, false);
931+
EMIT("div " + r2s(r64));
932+
}
933+
898934
void asm_div_r32(X86Reg r32) {
899935
m_code.push_back(m_al, 0xF7);
900936
modrm_sib_disp(m_code, m_al,

0 commit comments

Comments
 (0)