Skip to content

Commit 2bcb992

Browse files
committed
WASM: Move type and mem_align to wasm_utils
Rename type to var_type Also define and use vt2s() and k2s()
1 parent e376d60 commit 2bcb992

File tree

7 files changed

+87
-51
lines changed

7 files changed

+87
-51
lines changed

src/libasr/codegen/asr_to_wasm.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ class ASRToWASMVisitor : public ASR::BaseVisitor<ASRToWASMVisitor> {
108108
bool is_local_vars_only;
109109
ASR::Function_t* main_func;
110110
wasm::WASMAssembler wa;
111-
std::vector<wasm::type> local_vars;
111+
std::vector<wasm::var_type> local_vars;
112112

113113
uint32_t avail_mem_loc;
114114
uint32_t digits_mem_loc;
@@ -144,8 +144,8 @@ class ASRToWASMVisitor : public ASR::BaseVisitor<ASRToWASMVisitor> {
144144
}
145145

146146
void import_function(IMPORT_FUNC fn,
147-
std::vector<wasm::type> param_types,
148-
std::vector<wasm::type> result_types) {
147+
std::vector<wasm::var_type> param_types,
148+
std::vector<wasm::var_type> result_types) {
149149
int func_idx = wa.emit_func_type(param_types, result_types);
150150
m_import_func_idx_map[fn] = func_idx;
151151
wa.emit_import_fn( "wasi_snapshot_preview1", import_fn_to_str(fn), func_idx);
@@ -616,11 +616,11 @@ class ASRToWASMVisitor : public ASR::BaseVisitor<ASRToWASMVisitor> {
616616
wa.emit_declare_mem(min_no_pages, max_no_pages);
617617
wa.emit_export_mem("memory", 0 /* mem_idx */);
618618

619-
m_compiler_globals[cur_mem_loc] = wa.declare_global_var(wasm::type::i32, 0);
620-
m_compiler_globals[tmp_reg_i32] = wa.declare_global_var(wasm::type::i32, 0);
621-
m_compiler_globals[tmp_reg_i64] = wa.declare_global_var(wasm::type::i64, 0);
622-
m_compiler_globals[tmp_reg_f32] = wa.declare_global_var(wasm::type::f32, 0);
623-
m_compiler_globals[tmp_reg_f64] = wa.declare_global_var(wasm::type::f64, 0);
619+
m_compiler_globals[cur_mem_loc] = wa.declare_global_var(wasm::var_type::i32, 0);
620+
m_compiler_globals[tmp_reg_i32] = wa.declare_global_var(wasm::var_type::i32, 0);
621+
m_compiler_globals[tmp_reg_i64] = wa.declare_global_var(wasm::var_type::i64, 0);
622+
m_compiler_globals[tmp_reg_f32] = wa.declare_global_var(wasm::var_type::f32, 0);
623+
m_compiler_globals[tmp_reg_f64] = wa.declare_global_var(wasm::var_type::f64, 0);
624624

625625
emit_string(" ");
626626
emit_string("\n");
@@ -717,7 +717,7 @@ class ASRToWASMVisitor : public ASR::BaseVisitor<ASRToWASMVisitor> {
717717
this->visit_Function(*main_func);
718718
}
719719

720-
void get_var_type(ASR::Variable_t *v, std::vector<wasm::type> &type_vec) {
720+
void get_var_type(ASR::Variable_t *v, std::vector<wasm::var_type> &type_vec) {
721721
using namespace wasm;
722722

723723
bool is_array = ASRUtils::is_array(v->m_type);
@@ -934,7 +934,7 @@ class ASRToWASMVisitor : public ASR::BaseVisitor<ASRToWASMVisitor> {
934934
void emit_function_prototype(const ASR::Function_t &x) {
935935
SymbolFuncInfo *s = new SymbolFuncInfo;
936936

937-
std::vector<wasm::type> params, results;
937+
std::vector<wasm::var_type> params, results;
938938

939939
s->referenced_vars.reserve(m_al, x.n_args);
940940
for (size_t i = 0; i < x.n_args; i++) {
@@ -961,7 +961,7 @@ class ASRToWASMVisitor : public ASR::BaseVisitor<ASRToWASMVisitor> {
961961
}
962962
}
963963

964-
s->index = wa.emit_func_type(params, results);;
964+
s->index = wa.emit_func_type(params, results);
965965
m_func_name_idx_map[get_hash((ASR::asr_t *)&x)] = s;
966966
}
967967

src/libasr/codegen/wasm_assembler.h

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ namespace LCompilers {
77

88
namespace wasm {
99

10-
enum type { i32 = 0x7F, i64 = 0x7E, f32 = 0x7D, f64 = 0x7C};
11-
12-
enum mem_align { b8 = 0, b16 = 1, b32 = 2, b64 = 3 };
13-
1410
void emit_expr_end(Vec<uint8_t> &code, Allocator &al) {
1511
code.push_back(al, 0x0B);
1612
}
@@ -213,7 +209,7 @@ class WASMAssembler: public WASM_INSTS_VISITOR::WASMInstsAssembler<WASMAssembler
213209
cur_loop_nest_lvl = prev_cur_loop_nest_lvl;
214210
}
215211

216-
uint32_t emit_func_type(std::vector<wasm::type> &params, std::vector<wasm::type> &results) {
212+
uint32_t emit_func_type(std::vector<wasm::var_type> &params, std::vector<wasm::var_type> &results) {
217213
wasm::emit_b8(m_type_section, m_al, 0x60);
218214

219215
wasm::emit_u32(m_type_section, m_al, params.size());
@@ -299,7 +295,7 @@ class WASMAssembler: public WASM_INSTS_VISITOR::WASMInstsAssembler<WASMAssembler
299295
}
300296
}
301297

302-
void emit_func_body(uint32_t func_idx, std::string func_name, std::vector<wasm::type> &locals, std::function<void()> func_body) {
298+
void emit_func_body(uint32_t func_idx, std::string func_name, std::vector<wasm::var_type> &locals, std::function<void()> func_body) {
303299
/*** Reference Function Prototype ***/
304300
wasm::emit_u32(m_func_section, m_al, func_idx);
305301

@@ -327,9 +323,9 @@ class WASMAssembler: public WASM_INSTS_VISITOR::WASMInstsAssembler<WASMAssembler
327323
}
328324

329325
void define_func(
330-
std::vector<wasm::type> params,
331-
std::vector<wasm::type> results,
332-
std::vector<wasm::type> locals,
326+
std::vector<wasm::var_type> params,
327+
std::vector<wasm::var_type> results,
328+
std::vector<wasm::var_type> locals,
333329
std::string func_name,
334330
std::function<void()> func_body) {
335331

@@ -338,7 +334,7 @@ class WASMAssembler: public WASM_INSTS_VISITOR::WASMInstsAssembler<WASMAssembler
338334
}
339335

340336
template <typename T>
341-
uint32_t declare_global_var(wasm::type var_type, T init_val) {
337+
uint32_t declare_global_var(wasm::var_type var_type, T init_val) {
342338
m_global_section.push_back(m_al, var_type);
343339
m_global_section.push_back(m_al, true /* mutable */);
344340
switch (var_type)
@@ -367,7 +363,7 @@ class WASMAssembler: public WASM_INSTS_VISITOR::WASMInstsAssembler<WASMAssembler
367363
return no_of_globals++;
368364
}
369365

370-
uint32_t emit_local_vars(std::vector<type> locals) {
366+
uint32_t emit_local_vars(std::vector<wasm::var_type> locals) {
371367
uint32_t no_of_locals = 0;
372368
for (auto v:locals) {
373369
emit_u32(m_code_section, m_al, 1);

src/libasr/codegen/wasm_decoder.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
#include <fstream>
55

66
#include <libasr/assert.h>
7-
#include <libasr/alloc.h>
8-
#include <libasr/containers.h>
97
#include <libasr/codegen/wasm_utils.h>
108

119
// #define WAT_DEBUG
@@ -48,9 +46,6 @@ class WASMDecoder {
4846
Struct &self() { return static_cast<Struct &>(*this); }
4947

5048
public:
51-
std::unordered_map<uint8_t, std::string> var_type_to_string;
52-
std::unordered_map<uint8_t, std::string> kind_to_string;
53-
5449
Allocator &al;
5550
diag::Diagnostics &diag;
5651
Vec<uint8_t> wasm_bytes;
@@ -67,10 +62,6 @@ class WASMDecoder {
6762

6863
WASMDecoder(Allocator &al, diag::Diagnostics &diagonostics)
6964
: al(al), diag(diagonostics) {
70-
var_type_to_string = {
71-
{0x7F, "i32"}, {0x7E, "i64"}, {0x7D, "f32"}, {0x7C, "f64"}};
72-
kind_to_string = {
73-
{0x00, "func"}, {0x01, "table"}, {0x02, "memory"}, {0x03, "global"}};
7465

7566
PREAMBLE_SIZE = 8 /* BYTES */;
7667
// wasm_bytes.reserve(al, 1024 * 128);

src/libasr/codegen/wasm_to_wat.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -313,13 +313,12 @@ class WATVisitor : public WASMDecoder<WATVisitor>,
313313
result +=
314314
indent + "(type (;" + std::to_string(i) + ";) (func (param";
315315
for (uint32_t j = 0; j < func_types[i].param_types.size(); j++) {
316-
result +=
317-
" " + var_type_to_string[func_types[i].param_types.p[j]];
316+
result += " " + vt2s(func_types[i].param_types.p[j]);
318317
}
319318
result += ") (result";
320319
for (uint32_t j = 0; j < func_types[i].result_types.size(); j++) {
321320
result +=
322-
" " + var_type_to_string[func_types[i].result_types.p[j]];
321+
" " + vt2s(func_types[i].result_types.p[j]);
323322
}
324323
result += ")))";
325324
}
@@ -350,8 +349,8 @@ class WATVisitor : public WASMDecoder<WATVisitor>,
350349
decode_instructions();
351350
global_initialization_insts = this->src;
352351
}
353-
std::string global_type = ((globals[i].mut == 0x00) ? var_type_to_string[globals[i].type]:
354-
"(mut " + var_type_to_string[globals[i].type] + ")" );
352+
std::string global_type = ((globals[i].mut == 0x00) ? vt2s(globals[i].type):
353+
"(mut " + vt2s(globals[i].type) + ")" );
355354
result += indent + "(global $" + std::to_string(i);
356355
result += " " + global_type;
357356
result += " (" + global_initialization_insts + "))";
@@ -365,20 +364,20 @@ class WATVisitor : public WASMDecoder<WATVisitor>,
365364
j++) {
366365
result +=
367366
" " +
368-
var_type_to_string[func_types[func_index].param_types.p[j]];
367+
vt2s(func_types[func_index].param_types.p[j]);
369368
}
370369
result += ") (result";
371370
for (uint32_t j = 0; j < func_types[func_index].result_types.size();
372371
j++) {
373-
result += " " + var_type_to_string[func_types[func_index]
374-
.result_types.p[j]];
372+
result += " " + vt2s(func_types[func_index]
373+
.result_types.p[j]);
375374
}
376375
result += ")";
377376
result += indent + " (local";
378377
for (uint32_t j = 0; j < codes.p[i].locals.size(); j++) {
379378
for (uint32_t k = 0; k < codes.p[i].locals.p[j].count; k++) {
380379
result +=
381-
" " + var_type_to_string[codes.p[i].locals.p[j].type];
380+
" " + vt2s(codes.p[i].locals.p[j].type);
382381
}
383382
}
384383
result += ")";
@@ -403,7 +402,7 @@ class WATVisitor : public WASMDecoder<WATVisitor>,
403402

404403
for (uint32_t i = 0; i < exports.size(); i++) {
405404
result += indent + "(export \"" + exports.p[i].name + "\" (" +
406-
kind_to_string[exports.p[i].kind] + " " +
405+
k2s(exports.p[i].kind) + " " +
407406
std::to_string(exports.p[i].index) + "))";
408407
}
409408

src/libasr/codegen/wasm_to_x64.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ class X64Visitor : public WASMDecoder<X64Visitor>,
212212

213213
void visit_GlobalGet(uint32_t globalidx) {
214214
std::string loc = "global_" + std::to_string(globalidx);
215-
std::string var_type = var_type_to_string[globals[globalidx].type];
215+
std::string var_type = vt2s(globals[globalidx].type);
216216

217217
X64Reg base = X64Reg::rbx;
218218
m_a.asm_mov_r64_label(X64Reg::rbx, loc);
@@ -235,7 +235,7 @@ class X64Visitor : public WASMDecoder<X64Visitor>,
235235
}
236236

237237
std::string loc = "global_" + std::to_string(globalidx);
238-
std::string var_type = var_type_to_string[globals[globalidx].type];
238+
std::string var_type = vt2s(globals[globalidx].type);
239239

240240
X64Reg base = X64Reg::rbx;
241241
m_a.asm_mov_r64_label(X64Reg::rbx, loc);
@@ -257,7 +257,7 @@ class X64Visitor : public WASMDecoder<X64Visitor>,
257257
auto cur_func_param_type = func_types[type_indices[cur_func_idx]];
258258
int no_of_params = (int)cur_func_param_type.param_types.size();
259259
if ((int)localidx < no_of_params) {
260-
std::string var_type = var_type_to_string[cur_func_param_type.param_types[localidx]];
260+
std::string var_type = vt2s(cur_func_param_type.param_types[localidx]);
261261
if (var_type == "i32" || var_type == "i64") {
262262
m_a.asm_mov_r64_m64(X64Reg::rax, &base, nullptr, 1, 8 * (2 + no_of_params - (int)localidx - 1));
263263
m_a.asm_push_r64(X64Reg::rax);
@@ -271,7 +271,7 @@ class X64Visitor : public WASMDecoder<X64Visitor>,
271271
}
272272
} else {
273273
localidx -= no_of_params;
274-
std::string var_type = var_type_to_string[codes[cur_func_idx].locals[localidx].type];
274+
std::string var_type = vt2s(codes[cur_func_idx].locals[localidx].type);
275275
if (var_type == "i32" || var_type == "i64") {
276276
m_a.asm_mov_r64_m64(X64Reg::rax, &base, nullptr, 1, -8 * (1 + (int)localidx));
277277
m_a.asm_push_r64(X64Reg::rax);
@@ -291,7 +291,7 @@ class X64Visitor : public WASMDecoder<X64Visitor>,
291291
auto cur_func_param_type = func_types[type_indices[cur_func_idx]];
292292
int no_of_params = (int)cur_func_param_type.param_types.size();
293293
if ((int)localidx < no_of_params) {
294-
std::string var_type = var_type_to_string[cur_func_param_type.param_types[localidx]];
294+
std::string var_type = vt2s(cur_func_param_type.param_types[localidx]);
295295
if (var_type == "i32" || var_type == "i64") {
296296
m_a.asm_pop_r64(X64Reg::rax);
297297
m_a.asm_mov_m64_r64(&base, nullptr, 1, 8 * (2 + no_of_params - (int)localidx - 1), X64Reg::rax);
@@ -305,7 +305,7 @@ class X64Visitor : public WASMDecoder<X64Visitor>,
305305
}
306306
} else {
307307
localidx -= no_of_params;
308-
std::string var_type = var_type_to_string[codes[cur_func_idx].locals[localidx].type];
308+
std::string var_type = vt2s(codes[cur_func_idx].locals[localidx].type);
309309
if (var_type == "i32" || var_type == "i64") {
310310
m_a.asm_pop_r64(X64Reg::rax);
311311
m_a.asm_mov_m64_r64(&base, nullptr, 1, -8 * (1 + (int)localidx), X64Reg::rax);

src/libasr/codegen/wasm_to_x86.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ class X86Visitor : public WASMDecoder<X86Visitor>,
209209
auto cur_func_param_type = func_types[type_indices[cur_func_idx]];
210210
int no_of_params = (int)cur_func_param_type.param_types.size();
211211
if ((int)localidx < no_of_params) {
212-
std::string var_type = var_type_to_string[cur_func_param_type.param_types[localidx]];
212+
std::string var_type = vt2s(cur_func_param_type.param_types[localidx]);
213213
if (var_type == "i32") {
214214
m_a.asm_mov_r32_m32(X86Reg::eax, &base, nullptr, 1, 4 * (2 + no_of_params - (int)localidx - 1));
215215
m_a.asm_push_r32(X86Reg::eax);
@@ -227,7 +227,7 @@ class X86Visitor : public WASMDecoder<X86Visitor>,
227227

228228
} else {
229229
localidx -= no_of_params;
230-
std::string var_type = var_type_to_string[codes[cur_func_idx].locals[localidx].type];
230+
std::string var_type = vt2s(codes[cur_func_idx].locals[localidx].type);
231231
if (var_type == "i32") {
232232
m_a.asm_mov_r32_m32(X86Reg::eax, &base, nullptr, 1, -4 * (1 + localidx));
233233
m_a.asm_push_r32(X86Reg::eax);
@@ -249,7 +249,7 @@ class X86Visitor : public WASMDecoder<X86Visitor>,
249249
auto cur_func_param_type = func_types[type_indices[cur_func_idx]];
250250
int no_of_params = (int)cur_func_param_type.param_types.size();
251251
if ((int)localidx < no_of_params) {
252-
std::string var_type = var_type_to_string[cur_func_param_type.param_types[localidx]];
252+
std::string var_type = vt2s(cur_func_param_type.param_types[localidx]);
253253
if (var_type == "i32") {
254254
m_a.asm_pop_r32(X86Reg::eax);
255255
m_a.asm_mov_m32_r32(&base, nullptr, 1, 4 * (2 + no_of_params - (int)localidx - 1), X86Reg::eax);
@@ -267,7 +267,7 @@ class X86Visitor : public WASMDecoder<X86Visitor>,
267267

268268
} else {
269269
localidx -= no_of_params;
270-
std::string var_type = var_type_to_string[codes[cur_func_idx].locals[localidx].type];
270+
std::string var_type = vt2s(codes[cur_func_idx].locals[localidx].type);
271271
if (var_type == "i32") {
272272
m_a.asm_pop_r32(X86Reg::eax);
273273
m_a.asm_mov_m32_r32(&base, nullptr, 1, -4 * (1 + (int)localidx), X86Reg::eax);

src/libasr/codegen/wasm_utils.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,56 @@ namespace LCompilers {
1111

1212
namespace wasm {
1313

14+
enum var_type: uint8_t {
15+
i32 = 0x7F,
16+
i64 = 0x7E,
17+
f32 = 0x7D,
18+
f64 = 0x7C
19+
};
20+
21+
enum mem_align : uint8_t {
22+
b8 = 0,
23+
b16 = 1,
24+
b32 = 2,
25+
b64 = 3
26+
};
27+
28+
enum wasm_kind: uint8_t {
29+
func = 0x00,
30+
table = 0x01,
31+
memory = 0x02,
32+
global = 0x03
33+
};
34+
35+
template <typename T>
36+
std::string vt2s(T vt) {
37+
switch(vt) {
38+
case var_type::i32: return "i32";
39+
case var_type::i64: return "i64";
40+
case var_type::f32: return "f32";
41+
case var_type::f64: return "f64";
42+
default:
43+
std::cerr << "Unsupported wasm var_type" << std::endl;
44+
LCOMPILERS_ASSERT(false);
45+
return "";
46+
47+
}
48+
}
49+
50+
template <typename T>
51+
std::string k2s(T k) {
52+
switch(k) {
53+
case wasm_kind::func: return "func";
54+
case wasm_kind::table: return "table";
55+
case wasm_kind::memory: return "memory";
56+
case wasm_kind::global: return "global";
57+
default:
58+
std::cerr << "Unsupported wasm kind" << std::endl;
59+
LCOMPILERS_ASSERT(false);
60+
return "";
61+
}
62+
}
63+
1464
struct FuncType {
1565
Vec<uint8_t> param_types;
1666
Vec<uint8_t> result_types;

0 commit comments

Comments
 (0)