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