Skip to content

Commit

Permalink
Merge pull request lcompilers#1244 from Shaikh-Ubaid/wasm_x86_support…
Browse files Browse the repository at this point in the history
…_functions_return_value

Wasm_x86: support functions return value
  • Loading branch information
certik committed Nov 2, 2022
2 parents 6cad3e9 + 9b13170 commit a112710
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
12 changes: 8 additions & 4 deletions src/libasr/codegen/wasm_to_x86.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class X86Generator : public WASMDecoder<X86Generator> {
m_a.asm_push_r32(X86Reg::ebp);
m_a.asm_mov_r32_r32(X86Reg::ebp, X86Reg::esp);

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

{
Expand All @@ -124,9 +126,11 @@ Result<int> wasm_to_x86(Vec<uint8_t> &wasm_bytes, Allocator &al,
<< std::endl;
std::cout << "Generate asm: " << std::setw(5) << time_gen_x86_bytes
<< std::endl;
std::cout << "Verify: " << std::setw(5) << time_verify << std::endl;
std::cout << "Verify: " << std::setw(5) << time_verify
<< std::endl;
std::cout << "Save: " << std::setw(5) << time_save << std::endl;
int total = time_decode_wasm + time_gen_x86_bytes + time_verify + time_save;
int total =
time_decode_wasm + time_gen_x86_bytes + time_verify + time_save;
std::cout << "Total: " << std::setw(5) << total << std::endl;
}
return 0;
Expand Down
13 changes: 8 additions & 5 deletions src/libasr/codegen/wasm_to_x86.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,23 @@ class X86Visitor : public BaseWASMVisitor<X86Visitor> {
if (func_index == 0) {
m_a.asm_call_label("print_i32");
} else if (func_index == 5) {
// currently ignoring print_buf
// currently ignoring flush_buf
} else if (func_index == 6) {
m_a.asm_call_label("exit");
} else {
std::cerr << "Call to imported function with index " +
std::to_string(func_index) +
" not yet supported";
}
} else {
m_a.asm_call_label(exports[func_index - 7].name);
return;
}

uint32_t imports_adjusted_func_index = func_index - 7U;
m_a.asm_call_label(exports[imports_adjusted_func_index].name);


// Pop the passed function arguments
wasm::FuncType func_type = func_types[type_indices[func_index]];
wasm::FuncType func_type = func_types[type_indices[imports_adjusted_func_index]];
for (uint32_t i = 0; i < func_type.param_types.size(); i++) {
m_a.asm_pop_r32(X86Reg::eax);
}
Expand All @@ -68,7 +71,7 @@ class X86Visitor : public BaseWASMVisitor<X86Visitor> {
m_a.asm_mov_r32_m32(
X86Reg::eax, &base, nullptr, 1,
-(4 * (func_type.param_types.size() + 2 +
func_codes[func_index].locals.size() + 1)));
func_codes[imports_adjusted_func_index].locals.size() + 1)));

// push eax value onto stack
m_a.asm_push_r32(X86Reg::eax);
Expand Down

0 comments on commit a112710

Please sign in to comment.