Skip to content

Commit a112710

Browse files
authored
Merge pull request #1244 from Shaikh-Ubaid/wasm_x86_support_functions_return_value
Wasm_x86: support functions return value
2 parents 6cad3e9 + 9b13170 commit a112710

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

src/libasr/codegen/wasm_to_x86.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class X86Generator : public WASMDecoder<X86Generator> {
3535
m_a.asm_push_r32(X86Reg::ebp);
3636
m_a.asm_mov_r32_r32(X86Reg::ebp, X86Reg::esp);
3737

38-
// Initialze space for local variables to zero
38+
// Initialze local variables to zero and thus allocate space
3939
m_a.asm_mov_r32_imm32(X86Reg::eax, 0U);
4040
for (uint32_t j = 0; j < codes.p[i].locals.size(); j++) {
4141
for (uint32_t k = 0; k < codes.p[i].locals.p[j].count;
@@ -106,7 +106,9 @@ Result<int> wasm_to_x86(Vec<uint8_t> &wasm_bytes, Allocator &al,
106106
auto t1 = std::chrono::high_resolution_clock::now();
107107
m_a.verify();
108108
auto t2 = std::chrono::high_resolution_clock::now();
109-
time_verify = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1).count();
109+
time_verify =
110+
std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1)
111+
.count();
110112
}
111113

112114
{
@@ -124,9 +126,11 @@ Result<int> wasm_to_x86(Vec<uint8_t> &wasm_bytes, Allocator &al,
124126
<< std::endl;
125127
std::cout << "Generate asm: " << std::setw(5) << time_gen_x86_bytes
126128
<< std::endl;
127-
std::cout << "Verify: " << std::setw(5) << time_verify << std::endl;
129+
std::cout << "Verify: " << std::setw(5) << time_verify
130+
<< std::endl;
128131
std::cout << "Save: " << std::setw(5) << time_save << std::endl;
129-
int total = time_decode_wasm + time_gen_x86_bytes + time_verify + time_save;
132+
int total =
133+
time_decode_wasm + time_gen_x86_bytes + time_verify + time_save;
130134
std::cout << "Total: " << std::setw(5) << total << std::endl;
131135
}
132136
return 0;

src/libasr/codegen/wasm_to_x86.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,23 @@ class X86Visitor : public BaseWASMVisitor<X86Visitor> {
4343
if (func_index == 0) {
4444
m_a.asm_call_label("print_i32");
4545
} else if (func_index == 5) {
46-
// currently ignoring print_buf
46+
// currently ignoring flush_buf
4747
} else if (func_index == 6) {
4848
m_a.asm_call_label("exit");
4949
} else {
5050
std::cerr << "Call to imported function with index " +
5151
std::to_string(func_index) +
5252
" not yet supported";
5353
}
54-
} else {
55-
m_a.asm_call_label(exports[func_index - 7].name);
54+
return;
5655
}
5756

57+
uint32_t imports_adjusted_func_index = func_index - 7U;
58+
m_a.asm_call_label(exports[imports_adjusted_func_index].name);
59+
60+
5861
// Pop the passed function arguments
59-
wasm::FuncType func_type = func_types[type_indices[func_index]];
62+
wasm::FuncType func_type = func_types[type_indices[imports_adjusted_func_index]];
6063
for (uint32_t i = 0; i < func_type.param_types.size(); i++) {
6164
m_a.asm_pop_r32(X86Reg::eax);
6265
}
@@ -68,7 +71,7 @@ class X86Visitor : public BaseWASMVisitor<X86Visitor> {
6871
m_a.asm_mov_r32_m32(
6972
X86Reg::eax, &base, nullptr, 1,
7073
-(4 * (func_type.param_types.size() + 2 +
71-
func_codes[func_index].locals.size() + 1)));
74+
func_codes[imports_adjusted_func_index].locals.size() + 1)));
7275

7376
// push eax value onto stack
7477
m_a.asm_push_r32(X86Reg::eax);

0 commit comments

Comments
 (0)