summaryrefslogtreecommitdiffstats
path: root/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/fast-jit/fe/jit_emit_compare.c
blob: d41249346bc80e12a197c6a9db23ae61bff72276 (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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
 * Copyright (C) 2019 Intel Corporation. All rights reserved.
 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 */

#include "jit_emit_compare.h"
#include "jit_emit_function.h"
#include "../jit_frontend.h"
#include "../jit_codegen.h"

static bool
jit_compile_op_compare_integer(JitCompContext *cc, IntCond cond, bool is64Bit)
{
    JitReg lhs, rhs, res, const_zero, const_one;

    if (cond < INT_EQZ || cond > INT_GE_U) {
        jit_set_last_error(cc, "unsupported comparation operation");
        goto fail;
    }

    res = jit_cc_new_reg_I32(cc);
    const_zero = NEW_CONST(I32, 0);
    const_one = NEW_CONST(I32, 1);

    if (is64Bit) {
        if (INT_EQZ == cond) {
            rhs = NEW_CONST(I64, 0);
        }
        else {
            POP_I64(rhs);
        }
        POP_I64(lhs);
    }
    else {
        if (INT_EQZ == cond) {
            rhs = NEW_CONST(I32, 0);
        }
        else {
            POP_I32(rhs);
        }
        POP_I32(lhs);
    }

    GEN_INSN(CMP, cc->cmp_reg, lhs, rhs);
    switch (cond) {
        case INT_EQ:
        case INT_EQZ:
        {
            GEN_INSN(SELECTEQ, res, cc->cmp_reg, const_one, const_zero);
            break;
        }
        case INT_NE:
        {
            GEN_INSN(SELECTNE, res, cc->cmp_reg, const_one, const_zero);
            break;
        }
        case INT_LT_S:
        {
            GEN_INSN(SELECTLTS, res, cc->cmp_reg, const_one, const_zero);
            break;
        }
        case INT_LT_U:
        {
            GEN_INSN(SELECTLTU, res, cc->cmp_reg, const_one, const_zero);
            break;
        }
        case INT_GT_S:
        {
            GEN_INSN(SELECTGTS, res, cc->cmp_reg, const_one, const_zero);
            break;
        }
        case INT_GT_U:
        {
            GEN_INSN(SELECTGTU, res, cc->cmp_reg, const_one, const_zero);
            break;
        }
        case INT_LE_S:
        {
            GEN_INSN(SELECTLES, res, cc->cmp_reg, const_one, const_zero);
            break;
        }
        case INT_LE_U:
        {
            GEN_INSN(SELECTLEU, res, cc->cmp_reg, const_one, const_zero);
            break;
        }
        case INT_GE_S:
        {
            GEN_INSN(SELECTGES, res, cc->cmp_reg, const_one, const_zero);
            break;
        }
        default: /* INT_GE_U */
        {
            GEN_INSN(SELECTGEU, res, cc->cmp_reg, const_one, const_zero);
            break;
        }
    }

    PUSH_I32(res);
    return true;
fail:
    return false;
}

bool
jit_compile_op_i32_compare(JitCompContext *cc, IntCond cond)
{
    return jit_compile_op_compare_integer(cc, cond, false);
}

bool
jit_compile_op_i64_compare(JitCompContext *cc, IntCond cond)
{
    return jit_compile_op_compare_integer(cc, cond, true);
}

static int32
float_cmp_eq(float f1, float f2)
{
    if (isnan(f1) || isnan(f2))
        return 0;

    return f1 == f2;
}

static int32
float_cmp_ne(float f1, float f2)
{
    if (isnan(f1) || isnan(f2))
        return 1;

    return f1 != f2;
}

static int32
double_cmp_eq(double d1, double d2)
{
    if (isnan(d1) || isnan(d2))
        return 0;

    return d1 == d2;
}

static int32
double_cmp_ne(double d1, double d2)
{
    if (isnan(d1) || isnan(d2))
        return 1;

    return d1 != d2;
}

static bool
jit_compile_op_compare_float_point(JitCompContext *cc, FloatCond cond,
                                   JitReg lhs, JitReg rhs)
{
    JitReg res, args[2], const_zero, const_one;
    JitRegKind kind;
    void *func;

    if (cond == FLOAT_EQ || cond == FLOAT_NE) {
        kind = jit_reg_kind(lhs);
        if (cond == FLOAT_EQ)
            func = (kind == JIT_REG_KIND_F32) ? (void *)float_cmp_eq
                                              : (void *)double_cmp_eq;
        else
            func = (kind == JIT_REG_KIND_F32) ? (void *)float_cmp_ne
                                              : (void *)double_cmp_ne;

        res = jit_cc_new_reg_I32(cc);
        args[0] = lhs;
        args[1] = rhs;

        if (!jit_emit_callnative(cc, func, res, args, 2)) {
            goto fail;
        }
    }
    else {
        res = jit_cc_new_reg_I32(cc);
        const_zero = NEW_CONST(I32, 0);
        const_one = NEW_CONST(I32, 1);
        switch (cond) {
            case FLOAT_LT:
            {
                GEN_INSN(CMP, cc->cmp_reg, rhs, lhs);
                GEN_INSN(SELECTGTS, res, cc->cmp_reg, const_one, const_zero);
                break;
            }
            case FLOAT_GT:
            {
                GEN_INSN(CMP, cc->cmp_reg, lhs, rhs);
                GEN_INSN(SELECTGTS, res, cc->cmp_reg, const_one, const_zero);
                break;
            }
            case FLOAT_LE:
            {
                GEN_INSN(CMP, cc->cmp_reg, rhs, lhs);
                GEN_INSN(SELECTGES, res, cc->cmp_reg, const_one, const_zero);
                break;
            }
            case FLOAT_GE:
            {
                GEN_INSN(CMP, cc->cmp_reg, lhs, rhs);
                GEN_INSN(SELECTGES, res, cc->cmp_reg, const_one, const_zero);
                break;
            }
            default:
            {
                bh_assert(!"unknown FloatCond");
                goto fail;
            }
        }
    }
    PUSH_I32(res);

    return true;
fail:
    return false;
}

bool
jit_compile_op_f32_compare(JitCompContext *cc, FloatCond cond)
{
    JitReg res, const_zero, const_one;
    JitReg lhs, rhs;

    POP_F32(rhs);
    POP_F32(lhs);

    if (jit_reg_is_const_val(lhs) && jit_reg_is_const_val(rhs)) {
        float32 lvalue = jit_cc_get_const_F32(cc, lhs);
        float32 rvalue = jit_cc_get_const_F32(cc, rhs);

        const_zero = NEW_CONST(I32, 0);
        const_one = NEW_CONST(I32, 1);

        switch (cond) {
            case FLOAT_EQ:
            {
                res = (lvalue == rvalue) ? const_one : const_zero;
                break;
            }
            case FLOAT_NE:
            {
                res = (lvalue != rvalue) ? const_one : const_zero;
                break;
            }
            case FLOAT_LT:
            {
                res = (lvalue < rvalue) ? const_one : const_zero;
                break;
            }
            case FLOAT_GT:
            {
                res = (lvalue > rvalue) ? const_one : const_zero;
                break;
            }
            case FLOAT_LE:
            {
                res = (lvalue <= rvalue) ? const_one : const_zero;
                break;
            }
            case FLOAT_GE:
            {
                res = (lvalue >= rvalue) ? const_one : const_zero;
                break;
            }
            default:
            {
                bh_assert(!"unknown FloatCond");
                goto fail;
            }
        }

        PUSH_I32(res);
        return true;
    }

    return jit_compile_op_compare_float_point(cc, cond, lhs, rhs);
fail:
    return false;
}

bool
jit_compile_op_f64_compare(JitCompContext *cc, FloatCond cond)
{
    JitReg res, const_zero, const_one;
    JitReg lhs, rhs;

    POP_F64(rhs);
    POP_F64(lhs);

    if (jit_reg_is_const_val(lhs) && jit_reg_is_const_val(rhs)) {
        float64 lvalue = jit_cc_get_const_F64(cc, lhs);
        float64 rvalue = jit_cc_get_const_F64(cc, rhs);

        const_zero = NEW_CONST(I32, 0);
        const_one = NEW_CONST(I32, 1);

        switch (cond) {
            case FLOAT_EQ:
            {
                res = (lvalue == rvalue) ? const_one : const_zero;
                break;
            }
            case FLOAT_NE:
            {
                res = (lvalue != rvalue) ? const_one : const_zero;
                break;
            }
            case FLOAT_LT:
            {
                res = (lvalue < rvalue) ? const_one : const_zero;
                break;
            }
            case FLOAT_GT:
            {
                res = (lvalue > rvalue) ? const_one : const_zero;
                break;
            }
            case FLOAT_LE:
            {
                res = (lvalue <= rvalue) ? const_one : const_zero;
                break;
            }
            case FLOAT_GE:
            {
                res = (lvalue >= rvalue) ? const_one : const_zero;
                break;
            }
            default:
            {
                bh_assert(!"unknown FloatCond");
                goto fail;
            }
        }

        PUSH_I32(res);
        return true;
    }

    return jit_compile_op_compare_float_point(cc, cond, lhs, rhs);
fail:
    return false;
}