summaryrefslogtreecommitdiffstats
path: root/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/fast-jit/fe/jit_emit_variable.c
blob: ffbf06ab13d056185cda113097c1f8d1559dff46 (plain)
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
 * Copyright (C) 2019 Intel Corporation. All rights reserved.
 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 */

#include "jit_emit_variable.h"
#include "jit_emit_exception.h"
#include "../jit_frontend.h"

#define CHECK_LOCAL(idx)                                                     \
    do {                                                                     \
        if (idx                                                              \
            >= wasm_func->func_type->param_count + wasm_func->local_count) { \
            jit_set_last_error(cc, "local index out of range");              \
            goto fail;                                                       \
        }                                                                    \
    } while (0)

static uint8
get_local_type(const WASMFunction *wasm_func, uint32 local_idx)
{
    uint32 param_count = wasm_func->func_type->param_count;
    return local_idx < param_count
               ? wasm_func->func_type->types[local_idx]
               : wasm_func->local_types[local_idx - param_count];
}

bool
jit_compile_op_get_local(JitCompContext *cc, uint32 local_idx)
{
    WASMFunction *wasm_func = cc->cur_wasm_func;
    uint16 *local_offsets = wasm_func->local_offsets;
    uint16 local_offset;
    uint8 local_type;
    JitReg value = 0;

    CHECK_LOCAL(local_idx);

    local_offset = local_offsets[local_idx];
    local_type = get_local_type(wasm_func, local_idx);

    switch (local_type) {
        case VALUE_TYPE_I32:
#if WASM_ENABLE_REF_TYPES != 0
        case VALUE_TYPE_EXTERNREF:
        case VALUE_TYPE_FUNCREF:
#endif
            value = local_i32(cc->jit_frame, local_offset);

            break;
        case VALUE_TYPE_I64:
            value = local_i64(cc->jit_frame, local_offset);
            break;
        case VALUE_TYPE_F32:
            value = local_f32(cc->jit_frame, local_offset);
            break;
        case VALUE_TYPE_F64:
            value = local_f64(cc->jit_frame, local_offset);
            break;
        default:
            bh_assert(0);
            break;
    }

    PUSH(value, local_type);
    return true;
fail:
    return false;
}

bool
jit_compile_op_set_local(JitCompContext *cc, uint32 local_idx)
{
    WASMFunction *wasm_func = cc->cur_wasm_func;
    uint16 *local_offsets = wasm_func->local_offsets;
    uint16 local_offset;
    uint8 local_type;
    JitReg value;

    CHECK_LOCAL(local_idx);

    local_offset = local_offsets[local_idx];
    local_type = get_local_type(wasm_func, local_idx);

    switch (local_type) {
        case VALUE_TYPE_I32:
#if WASM_ENABLE_REF_TYPES != 0
        case VALUE_TYPE_EXTERNREF:
        case VALUE_TYPE_FUNCREF:
#endif
            POP_I32(value);
            set_local_i32(cc->jit_frame, local_offset, value);
            break;
        case VALUE_TYPE_I64:
            POP_I64(value);
            set_local_i64(cc->jit_frame, local_offset, value);
            break;
        case VALUE_TYPE_F32:
            POP_F32(value);
            set_local_f32(cc->jit_frame, local_offset, value);
            break;
        case VALUE_TYPE_F64:
            POP_F64(value);
            set_local_f64(cc->jit_frame, local_offset, value);
            break;
        default:
            bh_assert(0);
            break;
    }

    return true;
fail:
    return false;
}

bool
jit_compile_op_tee_local(JitCompContext *cc, uint32 local_idx)
{
    WASMFunction *wasm_func = cc->cur_wasm_func;
    uint16 *local_offsets = wasm_func->local_offsets;
    uint16 local_offset;
    uint8 local_type;
    JitReg value = 0;

    CHECK_LOCAL(local_idx);

    local_offset = local_offsets[local_idx];
    local_type = get_local_type(wasm_func, local_idx);

    switch (local_type) {
        case VALUE_TYPE_I32:
#if WASM_ENABLE_REF_TYPES != 0
        case VALUE_TYPE_EXTERNREF:
        case VALUE_TYPE_FUNCREF:
#endif
            POP_I32(value);
            set_local_i32(cc->jit_frame, local_offset, value);
            PUSH_I32(value);
            break;
        case VALUE_TYPE_I64:
            POP_I64(value);
            set_local_i64(cc->jit_frame, local_offset, value);
            PUSH_I64(value);
            break;
        case VALUE_TYPE_F32:
            POP_F32(value);
            set_local_f32(cc->jit_frame, local_offset, value);
            PUSH_F32(value);
            break;
        case VALUE_TYPE_F64:
            POP_F64(value);
            set_local_f64(cc->jit_frame, local_offset, value);
            PUSH_F64(value);
            break;
        default:
            bh_assert(0);
            goto fail;
    }

    return true;
fail:
    return false;
}

static uint8
get_global_type(const WASMModule *module, uint32 global_idx)
{
    if (global_idx < module->import_global_count) {
        const WASMGlobalImport *import_global =
            &((module->import_globals + global_idx)->u.global);
        return import_global->type;
    }
    else {
        const WASMGlobal *global =
            module->globals + (global_idx - module->import_global_count);
        return global->type;
    }
}

bool
jit_compile_op_get_global(JitCompContext *cc, uint32 global_idx)
{
    uint32 data_offset;
    uint8 global_type = 0;
    JitReg value = 0;

    bh_assert(global_idx < cc->cur_wasm_module->import_global_count
                               + cc->cur_wasm_module->global_count);

    data_offset =
        jit_frontend_get_global_data_offset(cc->cur_wasm_module, global_idx);
    global_type = get_global_type(cc->cur_wasm_module, global_idx);

    switch (global_type) {
        case VALUE_TYPE_I32:
#if WASM_ENABLE_REF_TYPES != 0
        case VALUE_TYPE_EXTERNREF:
        case VALUE_TYPE_FUNCREF:
#endif
        {
            value = jit_cc_new_reg_I32(cc);
            GEN_INSN(LDI32, value, get_module_inst_reg(cc->jit_frame),
                     NEW_CONST(I32, data_offset));
            break;
        }
        case VALUE_TYPE_I64:
        {
            value = jit_cc_new_reg_I64(cc);
            GEN_INSN(LDI64, value, get_module_inst_reg(cc->jit_frame),
                     NEW_CONST(I32, data_offset));
            break;
        }
        case VALUE_TYPE_F32:
        {
            value = jit_cc_new_reg_F32(cc);
            GEN_INSN(LDF32, value, get_module_inst_reg(cc->jit_frame),
                     NEW_CONST(I32, data_offset));
            break;
        }
        case VALUE_TYPE_F64:
        {
            value = jit_cc_new_reg_F64(cc);
            GEN_INSN(LDF64, value, get_module_inst_reg(cc->jit_frame),
                     NEW_CONST(I32, data_offset));
            break;
        }
        default:
        {
            jit_set_last_error(cc, "unexpected global type");
            goto fail;
        }
    }

    PUSH(value, global_type);

    return true;
fail:
    return false;
}

bool
jit_compile_op_set_global(JitCompContext *cc, uint32 global_idx,
                          bool is_aux_stack)
{
    uint32 data_offset;
    uint8 global_type = 0;
    JitReg value = 0;

    bh_assert(global_idx < cc->cur_wasm_module->import_global_count
                               + cc->cur_wasm_module->global_count);

    data_offset =
        jit_frontend_get_global_data_offset(cc->cur_wasm_module, global_idx);
    global_type = get_global_type(cc->cur_wasm_module, global_idx);

    switch (global_type) {
        case VALUE_TYPE_I32:
#if WASM_ENABLE_REF_TYPES != 0
        case VALUE_TYPE_EXTERNREF:
        case VALUE_TYPE_FUNCREF:
#endif
        {
            POP_I32(value);
            if (is_aux_stack) {
                JitReg aux_stack_bound = get_aux_stack_bound_reg(cc->jit_frame);
                JitReg aux_stack_bottom =
                    get_aux_stack_bottom_reg(cc->jit_frame);
                GEN_INSN(CMP, cc->cmp_reg, value, aux_stack_bound);
                if (!(jit_emit_exception(cc, EXCE_AUX_STACK_OVERFLOW,
                                         JIT_OP_BLEU, cc->cmp_reg, NULL)))
                    goto fail;
                GEN_INSN(CMP, cc->cmp_reg, value, aux_stack_bottom);
                if (!(jit_emit_exception(cc, EXCE_AUX_STACK_UNDERFLOW,
                                         JIT_OP_BGTU, cc->cmp_reg, NULL)))
                    goto fail;
            }
            GEN_INSN(STI32, value, get_module_inst_reg(cc->jit_frame),
                     NEW_CONST(I32, data_offset));
            break;
        }
        case VALUE_TYPE_I64:
        {
            POP_I64(value);
            GEN_INSN(STI64, value, get_module_inst_reg(cc->jit_frame),
                     NEW_CONST(I32, data_offset));
            break;
        }
        case VALUE_TYPE_F32:
        {
            POP_F32(value);
            GEN_INSN(STF32, value, get_module_inst_reg(cc->jit_frame),
                     NEW_CONST(I32, data_offset));
            break;
        }
        case VALUE_TYPE_F64:
        {
            POP_F64(value);
            GEN_INSN(STF64, value, get_module_inst_reg(cc->jit_frame),
                     NEW_CONST(I32, data_offset));
            break;
        }
        default:
        {
            jit_set_last_error(cc, "unexpected global type");
            goto fail;
        }
    }

    return true;
fail:
    return false;
}