Skip to content

Commit 5fcf60f

Browse files
committed
WASM: Bring WASMAssembler outside of wasm namespace
1 parent 2bcb992 commit 5fcf60f

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
@@ -107,7 +107,7 @@ class ASRToWASMVisitor : public ASR::BaseVisitor<ASRToWASMVisitor> {
107107
bool is_prototype_only;
108108
bool is_local_vars_only;
109109
ASR::Function_t* main_func;
110-
wasm::WASMAssembler wa;
110+
WASMAssembler wa;
111111
std::vector<wasm::var_type> local_vars;
112112

113113
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
@@ -85,6 +85,8 @@ void save_bin(Vec<uint8_t> &code, std::string filename) {
8585
save_js_glue_wasi(filename);
8686
}
8787

88+
} // namespace wasm
89+
8890
class WASMAssembler: public WASM_INSTS_VISITOR::WASMInstsAssembler<WASMAssembler> {
8991
private:
9092
Allocator &m_al;
@@ -227,57 +229,57 @@ class WASMAssembler: public WASM_INSTS_VISITOR::WASMInstsAssembler<WASMAssembler
227229

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

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

244246
void emit_declare_mem(uint32_t min_no_pages, uint32_t max_no_pages = 0) {
245247
if (max_no_pages > 0) {
246-
emit_b8(m_memory_section, m_al,
248+
wasm::emit_b8(m_memory_section, m_al,
247249
0x01); // for specifying min and max page limits of memory
248-
emit_u32(m_memory_section, m_al, min_no_pages);
249-
emit_u32(m_memory_section, m_al, max_no_pages);
250+
wasm::emit_u32(m_memory_section, m_al, min_no_pages);
251+
wasm::emit_u32(m_memory_section, m_al, max_no_pages);
250252
} else {
251-
emit_b8(m_memory_section, m_al,
253+
wasm::emit_b8(m_memory_section, m_al,
252254
0x00); // for specifying only min page limit of memory
253-
emit_u32(m_memory_section, m_al, min_no_pages);
255+
wasm::emit_u32(m_memory_section, m_al, min_no_pages);
254256
}
255257
no_of_memories++;
256258
}
257259

258260
void emit_import_mem(const std::string &mod_name, const std::string &mem_name,
259261
uint32_t min_no_pages, uint32_t max_no_pages = 0) {
260-
emit_str(m_import_section, m_al, mod_name);
261-
emit_str(m_import_section, m_al, mem_name);
262-
emit_b8(m_import_section, m_al, 0x02); // for importing memory
262+
wasm::emit_str(m_import_section, m_al, mod_name);
263+
wasm::emit_str(m_import_section, m_al, mem_name);
264+
wasm::emit_b8(m_import_section, m_al, 0x02); // for importing memory
263265
if (max_no_pages > 0) {
264-
emit_b8(m_import_section, m_al,
266+
wasm::emit_b8(m_import_section, m_al,
265267
0x01); // for specifying min and max page limits of memory
266-
emit_u32(m_import_section, m_al, min_no_pages);
267-
emit_u32(m_import_section, m_al, max_no_pages);
268+
wasm::emit_u32(m_import_section, m_al, min_no_pages);
269+
wasm::emit_u32(m_import_section, m_al, max_no_pages);
268270
} else {
269-
emit_b8(m_import_section, m_al,
271+
wasm::emit_b8(m_import_section, m_al,
270272
0x00); // for specifying only min page limit of memory
271-
emit_u32(m_import_section, m_al, min_no_pages);
273+
wasm::emit_u32(m_import_section, m_al, min_no_pages);
272274
}
273275
no_of_imports++;
274276
}
275277

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

@@ -286,10 +288,10 @@ class WASMAssembler: public WASM_INSTS_VISITOR::WASMInstsAssembler<WASMAssembler
286288
uint32_t no_of_elements) {
287289
// every section in WebAssembly is encoded by adding its section id,
288290
// followed by the content size and lastly the contents
289-
emit_u32(des, m_al, section_id);
290-
emit_u32(des, m_al, 4U /* size of no_of_elements */ + section_content.size());
291-
uint32_t len_idx = emit_len_placeholder(des, m_al);
292-
emit_u32_b32_idx(des, m_al, len_idx, no_of_elements);
291+
wasm::emit_u32(des, m_al, section_id);
292+
wasm::emit_u32(des, m_al, 4U /* size of no_of_elements */ + section_content.size());
293+
uint32_t len_idx = wasm::emit_len_placeholder(des, m_al);
294+
wasm::emit_u32_b32_idx(des, m_al, len_idx, no_of_elements);
293295
for (auto &byte : section_content) {
294296
des.push_back(m_al, byte);
295297
}
@@ -339,19 +341,19 @@ class WASMAssembler: public WASM_INSTS_VISITOR::WASMInstsAssembler<WASMAssembler
339341
m_global_section.push_back(m_al, true /* mutable */);
340342
switch (var_type)
341343
{
342-
case i32:
344+
case wasm::i32:
343345
wasm::emit_b8(m_global_section, m_al, 0x41); // emit instruction
344346
wasm::emit_i32(m_global_section, m_al, init_val); // emit val
345347
break;
346-
case i64:
348+
case wasm::i64:
347349
wasm::emit_b8(m_global_section, m_al, 0x42); // emit instruction
348350
wasm::emit_i64(m_global_section, m_al, init_val); // emit val
349351
break;
350-
case f32:
352+
case wasm::f32:
351353
wasm::emit_b8(m_global_section, m_al, 0x43); // emit instruction
352354
wasm::emit_f32(m_global_section, m_al, init_val); // emit val
353355
break;
354-
case f64:
356+
case wasm::f64:
355357
wasm::emit_b8(m_global_section, m_al, 0x44); // emit instruction
356358
wasm::emit_f64(m_global_section, m_al, init_val); // emit val
357359
break;
@@ -366,23 +368,21 @@ class WASMAssembler: public WASM_INSTS_VISITOR::WASMInstsAssembler<WASMAssembler
366368
uint32_t emit_local_vars(std::vector<wasm::var_type> locals) {
367369
uint32_t no_of_locals = 0;
368370
for (auto v:locals) {
369-
emit_u32(m_code_section, m_al, 1);
370-
emit_b8(m_code_section, m_al, v);
371+
wasm::emit_u32(m_code_section, m_al, 1);
372+
wasm::emit_b8(m_code_section, m_al, v);
371373
no_of_locals++;
372374
}
373375
return no_of_locals;
374376
}
375377

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

386-
} // namespace wasm
387-
388388
} // namespace LCompilers

0 commit comments

Comments
 (0)