From 8ef253a19c0d67fd527570257484e32e05c72e8e Mon Sep 17 00:00:00 2001 From: "liang.he" Date: Wed, 13 Apr 2022 13:43:45 +0800 Subject: [PATCH] Emit JIT IR for integer comparison (#1086) --- core/iwasm/fast-jit/fe/jit_emit_compare.c | 104 +++++++++++++++++++++- 1 file changed, 102 insertions(+), 2 deletions(-) diff --git a/core/iwasm/fast-jit/fe/jit_emit_compare.c b/core/iwasm/fast-jit/fe/jit_emit_compare.c index ec54e4e9b..41001fc06 100644 --- a/core/iwasm/fast-jit/fe/jit_emit_compare.c +++ b/core/iwasm/fast-jit/fe/jit_emit_compare.c @@ -6,26 +6,126 @@ #include "jit_emit_compare.h" #include "../jit_frontend.h" +static bool +jit_compile_op_compare(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; + } + case INT_GE_U: + { + GEN_INSN(SELECTGEU, res, cc->cmp_reg, const_one, const_zero); + break; + } + default: + { + goto fail; + } + } + + PUSH_I32(res); + return true; +fail: + return false; +} + bool jit_compile_op_i32_compare(JitCompContext *cc, IntCond cond) { - return false; + return jit_compile_op_compare(cc, cond, false); } bool jit_compile_op_i64_compare(JitCompContext *cc, IntCond cond) { - return false; + return jit_compile_op_compare(cc, cond, true); } bool jit_compile_op_f32_compare(JitCompContext *cc, FloatCond cond) { + jit_set_last_error(cc, "unimplement f32 comparison"); return false; } bool jit_compile_op_f64_compare(JitCompContext *cc, FloatCond cond) { + jit_set_last_error(cc, "unimplement f64 comparison"); return false; }