jacques_a_dit/wasm_runtime/
heap.rs1use wasm_gen;
2use wasm_gen::FuncCode;
3
4const OBJECT_TAG: isize = 1 << 7;
5
6pub enum Types {
7 Table = 1 | OBJECT_TAG,
8 String = 2 | OBJECT_TAG
9}
10
11pub const HEADER_SIZE: u8 = 1; pub fn generate_get_heap_ptr(heap_ptr_global: usize) -> wasm_gen::Func {
14 return wasm_gen::Func{
15 sig: wasm_gen::FuncType{
16 params: vec![],
17 results: vec![
18 wasm_gen::I32 ],
20 },
21 locals: vec![],
22 code: vec![
23 FuncCode::new1(wasm_gen::GLOBAL_GET, heap_ptr_global as u64),
25 ]
26 }
27}
28
29pub fn generate_malloc(heap_ptr_global: usize) -> wasm_gen::Func {
30 let ptr = 1;
31
32 return wasm_gen::Func{
33 sig: wasm_gen::FuncType{
34 params: vec![
35 wasm_gen::I32, ],
37 results: vec![
38 wasm_gen::I32 ],
40 },
41 locals: vec![
42 (1, wasm_gen::I32) ],
44 code: vec![
45 FuncCode::new1(wasm_gen::GLOBAL_GET, heap_ptr_global as u64),
47
48 FuncCode::new1(wasm_gen::LOCAL_TEE, ptr),
50
51 FuncCode::new1(wasm_gen::LOCAL_GET, 0x0), FuncCode::new0(wasm_gen::I32_ADD),
53
54 FuncCode::new1(wasm_gen::GLOBAL_SET, heap_ptr_global as u64),
56
57 FuncCode::new1(wasm_gen::LOCAL_GET, ptr),
59 ]
60 }
61}
62
63pub fn generate_alloc_string(malloc: usize) -> wasm_gen::Func {
64 assert!(HEADER_SIZE == HEADER_SIZE as u8);
65
66 let ptr = 1;
67
68 return wasm_gen::Func{
69 sig: wasm_gen::FuncType{
70 params: vec![
71 wasm_gen::I32, ],
73 results: vec![
74 wasm_gen::I32 ],
76 },
77 locals: vec![
78 (1, wasm_gen::I32) ],
80 code: vec![
81 FuncCode::new1(wasm_gen::LOCAL_GET, 0x0),
83 FuncCode::new1(wasm_gen::I32_CONST, HEADER_SIZE as u64), FuncCode::new0(wasm_gen::I32_ADD),
85 FuncCode::new1(wasm_gen::I32_CONST, 4), FuncCode::new0(wasm_gen::I32_ADD),
87 FuncCode::new1(wasm_gen::CALL, malloc as u64),
88
89 FuncCode::new1(wasm_gen::LOCAL_TEE, ptr),
91
92 FuncCode::new1(wasm_gen::I32_CONST, Types::String as u64),
94 FuncCode::new2(wasm_gen::I32_STORE8, 0x0, 0x0),
95
96 FuncCode::new1(wasm_gen::LOCAL_GET, ptr),
98 FuncCode::new1(wasm_gen::I32_CONST, HEADER_SIZE as u64),
99 FuncCode::new0(wasm_gen::I32_ADD),
100 FuncCode::new1(wasm_gen::LOCAL_GET, 0x0),
101 FuncCode::new2(wasm_gen::I32_STORE, 0x0, 0x0),
102
103 FuncCode::new1(wasm_gen::LOCAL_GET, ptr),
105 ]
106 }
107}