1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
pub mod string;
pub mod table;
pub mod heap;

use wasm_gen;
use wasm_gen::FuncCode;

// TODO(sven): move in string
pub fn generate_eq_bytes(get_length: usize,
                         char_code_at: usize) -> wasm_gen::Func {
    let params = vec![
        wasm_gen::I32, // ptr string left
        wasm_gen::I32  // ptr string right
    ];

    let string_left_ptr = 0;
    let string_right_ptr = 1;

    let cursor = params.len() as u64 + 0;
    let result = params.len() as u64 + 1;
    let string_length = params.len() as u64 + 2;

    return wasm_gen::Func{
        sig: wasm_gen::FuncType{
            params,
            results: vec![
                wasm_gen::I32 // boolean
            ],
        },
        locals: vec![
            (3, wasm_gen::I32) // cursor, result, string length
        ],
        code: vec![
            // get length string left
            FuncCode::new1(wasm_gen::LOCAL_GET, string_left_ptr), // pointer
            FuncCode::new1(wasm_gen::CALL, get_length as u64),

            // get length string right
            FuncCode::new1(wasm_gen::LOCAL_GET, string_right_ptr), // pointer
            FuncCode::new1(wasm_gen::CALL, get_length as u64),
            FuncCode::new1(wasm_gen::LOCAL_TEE, string_length),

            FuncCode::new0(wasm_gen::I32_NE),
            FuncCode::new1(wasm_gen::LOCAL_SET, result),

            FuncCode::new1(wasm_gen::BLOCK, wasm_gen::NONE),
            FuncCode::new1(wasm_gen::LOOP, wasm_gen::NONE),
            // {
                FuncCode::new1(wasm_gen::LOCAL_GET, result),
                FuncCode::new1(wasm_gen::BR_IF, 0x1),

                // test if cursor is over the length
                FuncCode::new1(wasm_gen::LOCAL_GET, cursor),
                FuncCode::new1(wasm_gen::I32_CONST, 0x1),
                FuncCode::new0(wasm_gen::I32_ADD),
                FuncCode::new1(wasm_gen::LOCAL_GET, string_length),
                FuncCode::new0(wasm_gen::I32_GT_U),
                FuncCode::new1(wasm_gen::BR_IF, 0x1),

                // load left byte at cursor
                FuncCode::new1(wasm_gen::LOCAL_GET, string_left_ptr), // pointer
                FuncCode::new1(wasm_gen::LOCAL_GET, cursor),
                FuncCode::new1(wasm_gen::CALL, char_code_at as u64),

                // load right byte at cursor
                FuncCode::new1(wasm_gen::LOCAL_GET, string_right_ptr), // pointer
                FuncCode::new1(wasm_gen::LOCAL_GET, cursor),
                FuncCode::new1(wasm_gen::CALL, char_code_at as u64),

                // test if bytes are equal
                FuncCode::new0(wasm_gen::I32_NE),
                FuncCode::new1(wasm_gen::LOCAL_SET, result),

                // test if result is true and break
                FuncCode::new1(wasm_gen::LOCAL_GET, result),
                FuncCode::new1(wasm_gen::BR_IF, 0x1),

                // move cursor forward + 1 bytes
                FuncCode::new1(wasm_gen::LOCAL_GET, cursor),
                FuncCode::new1(wasm_gen::I32_CONST, 0x1),
                FuncCode::new0(wasm_gen::I32_ADD),
                FuncCode::new1(wasm_gen::LOCAL_SET, cursor),

                FuncCode::new1(wasm_gen::BR, 0x0),
                FuncCode::new0(wasm_gen::END),
            //}
            FuncCode::new0(wasm_gen::END),

            FuncCode::new1(wasm_gen::LOCAL_GET, result),
            FuncCode::new0(wasm_gen::I32_EQZ), // flip
        ],
    };
}

pub fn generate_get_value_table_ptr(offset: u32) -> wasm_gen::Func {
    return wasm_gen::Func{
        sig: wasm_gen::FuncType{
            params: vec![],
            results: vec![
                wasm_gen::I32 // offset
            ],
        },
        locals: vec![],
        code: vec![
            FuncCode::new1(wasm_gen::I32_CONST, offset as u64),
        ],
    };
}