Skip to content

Commit bf4ff47

Browse files
committed
WASM: Bring WASMAssembler outside of wasm namespace
1 parent 45e59e1 commit bf4ff47

File tree

2 files changed

+41
-41
lines changed

2 files changed

+41
-41
lines changed

src/libasr/codegen/asr_to_wasm.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ class ASRToWASMVisitor : public ASR::BaseVisitor<ASRToWASMVisitor> {
112112
bool is_prototype_only;
113113
bool is_local_vars_only;
114114
ASR::Function_t* main_func;
115-
wasm::WASMAssembler wa;
115+
WASMAssembler wa;
116116
std::vector<wasm::var_type> local_vars;
117117

118118
uint32_t avail_mem_loc;

src/libasr/codegen/wasm_assembler.h

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ void save_bin(Vec<uint8_t> &code, std::string filename) {
8484
save_js_glue_wasi(filename);
8585
}
8686

87+
} // namespace wasm
88+
8789
class WASMAssembler: public WASM_INSTS_VISITOR::WASMInstsAssembler<WASMAssembler> {
8890
private:
8991
Allocator &m_al;
@@ -226,57 +228,57 @@ class WASMAssembler: public WASM_INSTS_VISITOR::WASMInstsAssembler<WASMAssembler
226228

227229
void emit_import_fn(const std::string &mod_name, const std::string &fn_name,
228230
uint32_t type_idx) {
229-
emit_str(m_import_section, m_al, mod_name);
230-
emit_str(m_import_section, m_al, fn_name);
231-
emit_b8(m_import_section, m_al, 0x00); // for importing function
232-
emit_u32(m_import_section, m_al, type_idx);
231+
wasm::emit_str(m_import_section, m_al, mod_name);
232+
wasm::emit_str(m_import_section, m_al, fn_name);
233+
wasm::emit_b8(m_import_section, m_al, 0x00); // for importing function
234+
wasm::emit_u32(m_import_section, m_al, type_idx);
233235
no_of_imports++;
234236
}
235237

236238
void emit_export_fn(const std::string &name, uint32_t idx) {
237-
emit_str(m_export_section, m_al, name);
238-
emit_b8(m_export_section, m_al, 0x00); // for exporting function
239-
emit_u32(m_export_section, m_al, idx);
239+
wasm::emit_str(m_export_section, m_al, name);
240+
wasm::emit_b8(m_export_section, m_al, 0x00); // for exporting function
241+
wasm::emit_u32(m_export_section, m_al, idx);
240242
no_of_exports++;
241243
}
242244

243245
void emit_declare_mem(uint32_t min_no_pages, uint32_t max_no_pages = 0) {
244246
if (max_no_pages > 0) {
245-
emit_b8(m_memory_section, m_al,
247+
wasm::emit_b8(m_memory_section, m_al,
246248
0x01); // for specifying min and max page limits of memory
247-
emit_u32(m_memory_section, m_al, min_no_pages);
248-
emit_u32(m_memory_section, m_al, max_no_pages);
249+
wasm::emit_u32(m_memory_section, m_al, min_no_pages);
250+
wasm::emit_u32(m_memory_section, m_al, max_no_pages);
249251
} else {
250-
emit_b8(m_memory_section, m_al,
252+
wasm::emit_b8(m_memory_section, m_al,
251253
0x00); // for specifying only min page limit of memory
252-
emit_u32(m_memory_section, m_al, min_no_pages);
254+
wasm::emit_u32(m_memory_section, m_al, min_no_pages);
253255
}
254256
no_of_memories++;
255257
}
256258

257259
void emit_import_mem(const std::string &mod_name, const std::string &mem_name,
258260
uint32_t min_no_pages, uint32_t max_no_pages = 0) {
259-
emit_str(m_import_section, m_al, mod_name);
260-
emit_str(m_import_section, m_al, mem_name);
261-
emit_b8(m_import_section, m_al, 0x02); // for importing memory
261+
wasm::emit_str(m_import_section, m_al, mod_name);
262+
wasm::emit_str(m_import_section, m_al, mem_name);
263+
wasm::emit_b8(m_import_section, m_al, 0x02); // for importing memory
262264
if (max_no_pages > 0) {
263-
emit_b8(m_import_section, m_al,
265+
wasm::emit_b8(m_import_section, m_al,
264266
0x01); // for specifying min and max page limits of memory
265-
emit_u32(m_import_section, m_al, min_no_pages);
266-
emit_u32(m_import_section, m_al, max_no_pages);
267+
wasm::emit_u32(m_import_section, m_al, min_no_pages);
268+
wasm::emit_u32(m_import_section, m_al, max_no_pages);
267269
} else {
268-
emit_b8(m_import_section, m_al,
270+
wasm::emit_b8(m_import_section, m_al,
269271
0x00); // for specifying only min page limit of memory
270-
emit_u32(m_import_section, m_al, min_no_pages);
272+
wasm::emit_u32(m_import_section, m_al, min_no_pages);
271273
}
272274
no_of_imports++;
273275
}
274276

275277
void emit_export_mem(const std::string &name,
276278
uint32_t idx) {
277-
emit_str(m_export_section, m_al, name);
278-
emit_b8(m_export_section, m_al, 0x02); // for exporting memory
279-
emit_u32(m_export_section, m_al, idx);
279+
wasm::emit_str(m_export_section, m_al, name);
280+
wasm::emit_b8(m_export_section, m_al, 0x02); // for exporting memory
281+
wasm::emit_u32(m_export_section, m_al, idx);
280282
no_of_exports++;
281283
}
282284

@@ -285,10 +287,10 @@ class WASMAssembler: public WASM_INSTS_VISITOR::WASMInstsAssembler<WASMAssembler
285287
uint32_t no_of_elements) {
286288
// every section in WebAssembly is encoded by adding its section id,
287289
// followed by the content size and lastly the contents
288-
emit_u32(des, m_al, section_id);
289-
emit_u32(des, m_al, 4U /* size of no_of_elements */ + section_content.size());
290-
uint32_t len_idx = emit_len_placeholder(des, m_al);
291-
emit_u32_b32_idx(des, m_al, len_idx, no_of_elements);
290+
wasm::emit_u32(des, m_al, section_id);
291+
wasm::emit_u32(des, m_al, 4U /* size of no_of_elements */ + section_content.size());
292+
uint32_t len_idx = wasm::emit_len_placeholder(des, m_al);
293+
wasm::emit_u32_b32_idx(des, m_al, len_idx, no_of_elements);
292294
for (auto &byte : section_content) {
293295
des.push_back(m_al, byte);
294296
}
@@ -338,19 +340,19 @@ class WASMAssembler: public WASM_INSTS_VISITOR::WASMInstsAssembler<WASMAssembler
338340
m_global_section.push_back(m_al, true /* mutable */);
339341
switch (var_type)
340342
{
341-
case i32:
343+
case wasm::i32:
342344
wasm::emit_b8(m_global_section, m_al, 0x41); // emit instruction
343345
wasm::emit_i32(m_global_section, m_al, init_val); // emit val
344346
break;
345-
case i64:
347+
case wasm::i64:
346348
wasm::emit_b8(m_global_section, m_al, 0x42); // emit instruction
347349
wasm::emit_i64(m_global_section, m_al, init_val); // emit val
348350
break;
349-
case f32:
351+
case wasm::f32:
350352
wasm::emit_b8(m_global_section, m_al, 0x43); // emit instruction
351353
wasm::emit_f32(m_global_section, m_al, init_val); // emit val
352354
break;
353-
case f64:
355+
case wasm::f64:
354356
wasm::emit_b8(m_global_section, m_al, 0x44); // emit instruction
355357
wasm::emit_f64(m_global_section, m_al, init_val); // emit val
356358
break;
@@ -365,23 +367,21 @@ class WASMAssembler: public WASM_INSTS_VISITOR::WASMInstsAssembler<WASMAssembler
365367
uint32_t emit_local_vars(std::vector<wasm::var_type> locals) {
366368
uint32_t no_of_locals = 0;
367369
for (auto v:locals) {
368-
emit_u32(m_code_section, m_al, 1);
369-
emit_b8(m_code_section, m_al, v);
370+
wasm::emit_u32(m_code_section, m_al, 1);
371+
wasm::emit_b8(m_code_section, m_al, v);
370372
no_of_locals++;
371373
}
372374
return no_of_locals;
373375
}
374376

375377
void emit_data_str(uint32_t mem_idx, const std::string &text) {
376-
emit_u32(m_data_section, m_al, 0U); // for active mode of memory with default mem_idx of 0
377-
emit_b8(m_data_section, m_al, 0x41); // i32.const
378-
emit_i32(m_data_section, m_al, (int32_t)mem_idx); // specifying memory location
379-
emit_expr_end(m_data_section, m_al); // end instructions
380-
emit_str(m_data_section, m_al, text);
378+
wasm::emit_u32(m_data_section, m_al, 0U); // for active mode of memory with default mem_idx of 0
379+
wasm::emit_b8(m_data_section, m_al, 0x41); // i32.const
380+
wasm::emit_i32(m_data_section, m_al, (int32_t)mem_idx); // specifying memory location
381+
wasm::emit_expr_end(m_data_section, m_al); // end instructions
382+
wasm::emit_str(m_data_section, m_al, text);
381383
no_of_data_segs++;
382384
}
383385
};
384386

385-
} // namespace wasm
386-
387387
} // namespace LCompilers

0 commit comments

Comments
 (0)