jacques_a_dit/wasm_runtime/
heap.rs

1use 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; /* byte */
12
13pub 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 // ptr
19            ],
20        },
21        locals: vec![],
22        code: vec![
23            // load heap ptr
24            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, // size
36            ],
37            results: vec![
38                wasm_gen::I32 // ptr
39            ],
40        },
41        locals: vec![
42            (1, wasm_gen::I32) // ptr
43        ],
44        code: vec![
45            // load heap ptr
46            FuncCode::new1(wasm_gen::GLOBAL_GET, heap_ptr_global as u64),
47
48            // backup ptr
49            FuncCode::new1(wasm_gen::LOCAL_TEE, ptr),
50
51            FuncCode::new1(wasm_gen::LOCAL_GET, 0x0), // size
52            FuncCode::new0(wasm_gen::I32_ADD),
53
54            // bump heap ptr
55            FuncCode::new1(wasm_gen::GLOBAL_SET, heap_ptr_global as u64),
56
57            // return ptr
58            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, // size
72            ],
73            results: vec![
74                wasm_gen::I32 // ptr
75            ],
76        },
77        locals: vec![
78            (1, wasm_gen::I32) // ptr
79        ],
80        code: vec![
81            // alloc memory
82            FuncCode::new1(wasm_gen::LOCAL_GET, 0x0),
83            FuncCode::new1(wasm_gen::I32_CONST, HEADER_SIZE as u64), // tag
84            FuncCode::new0(wasm_gen::I32_ADD),
85            FuncCode::new1(wasm_gen::I32_CONST, 4), // string size
86            FuncCode::new0(wasm_gen::I32_ADD),
87            FuncCode::new1(wasm_gen::CALL, malloc as u64),
88
89            // backup ptr
90            FuncCode::new1(wasm_gen::LOCAL_TEE, ptr),
91
92            // tag ptr
93            FuncCode::new1(wasm_gen::I32_CONST, Types::String as u64),
94            FuncCode::new2(wasm_gen::I32_STORE8, 0x0, 0x0),
95
96            // set length
97            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            // return ptr
104            FuncCode::new1(wasm_gen::LOCAL_GET, ptr),
105        ]
106    }
107}