@@ -85,6 +85,8 @@ void save_bin(Vec<uint8_t> &code, std::string filename) {
85
85
save_js_glue_wasi (filename);
86
86
}
87
87
88
+ } // namespace wasm
89
+
88
90
class WASMAssembler : public WASM_INSTS_VISITOR ::WASMInstsAssembler<WASMAssembler> {
89
91
private:
90
92
Allocator &m_al;
@@ -227,57 +229,57 @@ class WASMAssembler: public WASM_INSTS_VISITOR::WASMInstsAssembler<WASMAssembler
227
229
228
230
void emit_import_fn (const std::string &mod_name, const std::string &fn_name,
229
231
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);
234
236
no_of_imports++;
235
237
}
236
238
237
239
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);
241
243
no_of_exports++;
242
244
}
243
245
244
246
void emit_declare_mem (uint32_t min_no_pages, uint32_t max_no_pages = 0 ) {
245
247
if (max_no_pages > 0 ) {
246
- emit_b8 (m_memory_section, m_al,
248
+ wasm:: emit_b8 (m_memory_section, m_al,
247
249
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);
250
252
} else {
251
- emit_b8 (m_memory_section, m_al,
253
+ wasm:: emit_b8 (m_memory_section, m_al,
252
254
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);
254
256
}
255
257
no_of_memories++;
256
258
}
257
259
258
260
void emit_import_mem (const std::string &mod_name, const std::string &mem_name,
259
261
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
263
265
if (max_no_pages > 0 ) {
264
- emit_b8 (m_import_section, m_al,
266
+ wasm:: emit_b8 (m_import_section, m_al,
265
267
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);
268
270
} else {
269
- emit_b8 (m_import_section, m_al,
271
+ wasm:: emit_b8 (m_import_section, m_al,
270
272
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);
272
274
}
273
275
no_of_imports++;
274
276
}
275
277
276
278
void emit_export_mem (const std::string &name,
277
279
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);
281
283
no_of_exports++;
282
284
}
283
285
@@ -286,10 +288,10 @@ class WASMAssembler: public WASM_INSTS_VISITOR::WASMInstsAssembler<WASMAssembler
286
288
uint32_t no_of_elements) {
287
289
// every section in WebAssembly is encoded by adding its section id,
288
290
// 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);
293
295
for (auto &byte : section_content) {
294
296
des.push_back (m_al, byte);
295
297
}
@@ -339,19 +341,19 @@ class WASMAssembler: public WASM_INSTS_VISITOR::WASMInstsAssembler<WASMAssembler
339
341
m_global_section.push_back (m_al, true /* mutable */ );
340
342
switch (var_type)
341
343
{
342
- case i32:
344
+ case wasm:: i32:
343
345
wasm::emit_b8 (m_global_section, m_al, 0x41 ); // emit instruction
344
346
wasm::emit_i32 (m_global_section, m_al, init_val); // emit val
345
347
break ;
346
- case i64:
348
+ case wasm:: i64:
347
349
wasm::emit_b8 (m_global_section, m_al, 0x42 ); // emit instruction
348
350
wasm::emit_i64 (m_global_section, m_al, init_val); // emit val
349
351
break ;
350
- case f32:
352
+ case wasm:: f32:
351
353
wasm::emit_b8 (m_global_section, m_al, 0x43 ); // emit instruction
352
354
wasm::emit_f32 (m_global_section, m_al, init_val); // emit val
353
355
break ;
354
- case f64:
356
+ case wasm:: f64:
355
357
wasm::emit_b8 (m_global_section, m_al, 0x44 ); // emit instruction
356
358
wasm::emit_f64 (m_global_section, m_al, init_val); // emit val
357
359
break ;
@@ -366,23 +368,21 @@ class WASMAssembler: public WASM_INSTS_VISITOR::WASMInstsAssembler<WASMAssembler
366
368
uint32_t emit_local_vars (std::vector<wasm::var_type> locals) {
367
369
uint32_t no_of_locals = 0 ;
368
370
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);
371
373
no_of_locals++;
372
374
}
373
375
return no_of_locals;
374
376
}
375
377
376
378
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);
382
384
no_of_data_segs++;
383
385
}
384
386
};
385
387
386
- } // namespace wasm
387
-
388
388
} // namespace LCompilers
0 commit comments