|
| 1 | +#ifndef LFORTRAN_WASM_TO_X86_H |
| 2 | +#define LFORTRAN_WASM_TO_X86_H |
| 3 | + |
| 4 | +#include <libasr/wasm_visitor.h> |
| 5 | +#include <libasr/codegen/x86_assembler.h> |
| 6 | + |
| 7 | +namespace LFortran { |
| 8 | + |
| 9 | +namespace WASM_INSTS_VISITOR { |
| 10 | + |
| 11 | +class X86Visitor : public BaseWASMVisitor<X86Visitor> { |
| 12 | + public: |
| 13 | + X86Assembler &m_a; |
| 14 | + Vec<wasm::FuncType> func_types; |
| 15 | + Vec<wasm::Import> imports; |
| 16 | + Vec<uint32_t> type_indices; |
| 17 | + Vec<wasm::Export> exports; |
| 18 | + Vec<wasm::Code> func_codes; |
| 19 | + Vec<wasm::Data> data_segments; |
| 20 | + uint32_t cur_func_idx; |
| 21 | + |
| 22 | + X86Visitor(Vec<uint8_t> &code, uint32_t offset, X86Assembler &m_a, |
| 23 | + Vec<wasm::FuncType> &func_types, Vec<wasm::Import> &imports, |
| 24 | + Vec<uint32_t> &type_indices, Vec<wasm::Export> &exports, |
| 25 | + Vec<wasm::Code> &func_codes, Vec<wasm::Data> &data_segments, |
| 26 | + uint32_t cur_func_idx) |
| 27 | + : BaseWASMVisitor(code, offset), m_a(m_a) { |
| 28 | + this->func_types.from_pointer_n(func_types.p, func_types.size()); |
| 29 | + this->imports.from_pointer_n(imports.p, imports.size()); |
| 30 | + this->type_indices.from_pointer_n(type_indices.p, type_indices.size()); |
| 31 | + this->exports.from_pointer_n(exports.p, exports.size()); |
| 32 | + this->func_codes.from_pointer_n(func_codes.p, func_codes.size()); |
| 33 | + this->data_segments.from_pointer_n(data_segments.p, |
| 34 | + data_segments.size()); |
| 35 | + this->cur_func_idx = cur_func_idx; |
| 36 | + } |
| 37 | + |
| 38 | + void visit_Return() {} |
| 39 | + |
| 40 | + void visit_Call(uint32_t func_index) { |
| 41 | + if (func_index <= 6U) { |
| 42 | + // call to imported functions |
| 43 | + if (func_index == 0) { |
| 44 | + m_a.asm_call_label("print_i32"); |
| 45 | + } else if (func_index == 5) { |
| 46 | + // currently ignoring print_buf |
| 47 | + } else if (func_index == 6) { |
| 48 | + m_a.asm_call_label("exit"); |
| 49 | + } else { |
| 50 | + std::cerr << "Call to imported function with index " + |
| 51 | + std::to_string(func_index) + |
| 52 | + " not yet supported"; |
| 53 | + } |
| 54 | + } else { |
| 55 | + m_a.asm_call_label(exports[func_index - 7].name); |
| 56 | + } |
| 57 | + |
| 58 | + // Pop the passed function arguments |
| 59 | + wasm::FuncType func_type = func_types[type_indices[func_index]]; |
| 60 | + for (uint32_t i = 0; i < func_type.param_types.size(); i++) { |
| 61 | + m_a.asm_pop_r32(X86Reg::eax); |
| 62 | + } |
| 63 | + |
| 64 | + // Adjust the return values of the called function |
| 65 | + X86Reg base = X86Reg::esp; |
| 66 | + for (uint32_t i = 0; i < func_type.result_types.size(); i++) { |
| 67 | + // take value into eax |
| 68 | + m_a.asm_mov_r32_m32( |
| 69 | + X86Reg::eax, &base, nullptr, 1, |
| 70 | + -(4 * (func_type.param_types.size() + 2 + |
| 71 | + func_codes[func_index].locals.size() + 1))); |
| 72 | + |
| 73 | + // push eax value onto stack |
| 74 | + m_a.asm_push_r32(X86Reg::eax); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + void visit_LocalGet(uint32_t localidx) { |
| 79 | + X86Reg base = X86Reg::ebp; |
| 80 | + int no_of_params = |
| 81 | + (int)func_types[type_indices[cur_func_idx]].param_types.size(); |
| 82 | + if ((int)localidx < no_of_params) { |
| 83 | + m_a.asm_mov_r32_m32(X86Reg::eax, &base, nullptr, 1, |
| 84 | + (4 * localidx + 8)); |
| 85 | + m_a.asm_push_r32(X86Reg::eax); |
| 86 | + } else { |
| 87 | + m_a.asm_mov_r32_m32(X86Reg::eax, &base, nullptr, 1, |
| 88 | + -(4 * ((int)localidx - no_of_params + 1))); |
| 89 | + m_a.asm_push_r32(X86Reg::eax); |
| 90 | + } |
| 91 | + } |
| 92 | + void visit_LocalSet(uint32_t localidx) { |
| 93 | + X86Reg base = X86Reg::ebp; |
| 94 | + int no_of_params = |
| 95 | + (int)func_types[type_indices[cur_func_idx]].param_types.size(); |
| 96 | + if ((int)localidx < no_of_params) { |
| 97 | + m_a.asm_pop_r32(X86Reg::eax); |
| 98 | + m_a.asm_mov_m32_r32(&base, nullptr, 1, (4 * localidx + 8), |
| 99 | + X86Reg::eax); |
| 100 | + } else { |
| 101 | + m_a.asm_pop_r32(X86Reg::eax); |
| 102 | + m_a.asm_mov_m32_r32(&base, nullptr, 1, |
| 103 | + -(4 * ((int)localidx - no_of_params + 1)), |
| 104 | + X86Reg::eax); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + void visit_I32Const(int32_t value) { |
| 109 | + m_a.asm_push_imm32(value); |
| 110 | + // if (value < 0) { |
| 111 | + // m_a.asm_pop_r32(X86Reg::eax); |
| 112 | + // m_a.asm_neg_r32(X86Reg::eax); |
| 113 | + // m_a.asm_push_r32(X86Reg::eax); |
| 114 | + // } |
| 115 | + } |
| 116 | + |
| 117 | + void visit_I32Add() { |
| 118 | + m_a.asm_pop_r32(X86Reg::ebx); |
| 119 | + m_a.asm_pop_r32(X86Reg::eax); |
| 120 | + m_a.asm_add_r32_r32(X86Reg::eax, X86Reg::ebx); |
| 121 | + m_a.asm_push_r32(X86Reg::eax); |
| 122 | + } |
| 123 | + void visit_I32Sub() { |
| 124 | + m_a.asm_pop_r32(X86Reg::ebx); |
| 125 | + m_a.asm_pop_r32(X86Reg::eax); |
| 126 | + m_a.asm_sub_r32_r32(X86Reg::eax, X86Reg::ebx); |
| 127 | + m_a.asm_push_r32(X86Reg::eax); |
| 128 | + } |
| 129 | + void visit_I32Mul() { |
| 130 | + m_a.asm_pop_r32(X86Reg::ebx); |
| 131 | + m_a.asm_pop_r32(X86Reg::eax); |
| 132 | + m_a.asm_mul_r32(X86Reg::ebx); |
| 133 | + m_a.asm_push_r32(X86Reg::eax); |
| 134 | + } |
| 135 | + void visit_I32DivS() { |
| 136 | + m_a.asm_pop_r32(X86Reg::ebx); |
| 137 | + m_a.asm_pop_r32(X86Reg::eax); |
| 138 | + m_a.asm_div_r32(X86Reg::ebx); |
| 139 | + m_a.asm_push_r32(X86Reg::eax); |
| 140 | + } |
| 141 | +}; |
| 142 | + |
| 143 | +} // namespace WASM_INSTS_VISITOR |
| 144 | + |
| 145 | +Result<int> wasm_to_x86(Vec<uint8_t> &wasm_bytes, Allocator &al, |
| 146 | + const std::string &filename, bool time_report, |
| 147 | + diag::Diagnostics &diagnostics); |
| 148 | + |
| 149 | +} // namespace LFortran |
| 150 | + |
| 151 | +#endif // LFORTRAN_WASM_TO_X86_H |
0 commit comments