Path: blob/master/thirdparty/pcre2/deps/sljit/sljit_src/sljitNativeRISCV_32.c
9913 views
/*1* Stack-less Just-In-Time compiler2*3* Copyright Zoltan Herczeg ([email protected]). All rights reserved.4*5* Redistribution and use in source and binary forms, with or without modification, are6* permitted provided that the following conditions are met:7*8* 1. Redistributions of source code must retain the above copyright notice, this list of9* conditions and the following disclaimer.10*11* 2. Redistributions in binary form must reproduce the above copyright notice, this list12* of conditions and the following disclaimer in the documentation and/or other materials13* provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) AND CONTRIBUTORS ``AS IS'' AND ANY16* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES17* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT18* SHALL THE COPYRIGHT HOLDER(S) OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,19* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED20* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR21* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN22* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN23* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.24*/2526static sljit_s32 load_immediate(struct sljit_compiler *compiler, sljit_s32 dst_r, sljit_sw imm, sljit_s32 tmp_r)27{28SLJIT_UNUSED_ARG(tmp_r);2930if (imm <= SIMM_MAX && imm >= SIMM_MIN)31return push_inst(compiler, ADDI | RD(dst_r) | RS1(TMP_ZERO) | IMM_I(imm));3233if (imm & 0x800)34imm += 0x1000;3536FAIL_IF(push_inst(compiler, LUI | RD(dst_r) | (sljit_ins)(imm & ~0xfff)));3738if ((imm & 0xfff) == 0)39return SLJIT_SUCCESS;4041return push_inst(compiler, ADDI | RD(dst_r) | RS1(dst_r) | IMM_I(imm));42}4344SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fset64(struct sljit_compiler *compiler,45sljit_s32 freg, sljit_f64 value)46{47union {48sljit_s32 imm[2];49sljit_f64 value;50} u;5152CHECK_ERROR();53CHECK(check_sljit_emit_fset64(compiler, freg, value));5455u.value = value;5657if (u.imm[0] != 0)58FAIL_IF(load_immediate(compiler, TMP_REG1, u.imm[0], TMP_REG3));59if (u.imm[1] != 0)60FAIL_IF(load_immediate(compiler, TMP_REG2, u.imm[1], TMP_REG3));6162FAIL_IF(push_inst(compiler, ADDI | RD(SLJIT_SP) | RS1(SLJIT_SP) | IMM_I(-16)));63FAIL_IF(push_inst(compiler, SW | RS1(SLJIT_SP) | RS2(u.imm[0] != 0 ? TMP_REG1 : TMP_ZERO) | (8 << 7)));64FAIL_IF(push_inst(compiler, SW | RS1(SLJIT_SP) | RS2(u.imm[1] != 0 ? TMP_REG2 : TMP_ZERO) | (12 << 7)));65FAIL_IF(push_inst(compiler, FLD | FRD(freg) | RS1(SLJIT_SP) | IMM_I(8)));66return push_inst(compiler, ADDI | RD(SLJIT_SP) | RS1(SLJIT_SP) | IMM_I(16));67}6869SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fcopy(struct sljit_compiler *compiler, sljit_s32 op,70sljit_s32 freg, sljit_s32 reg)71{72sljit_ins inst;73sljit_s32 reg2 = 0;7475CHECK_ERROR();76CHECK(check_sljit_emit_fcopy(compiler, op, freg, reg));7778if (op & SLJIT_32) {79if (op == SLJIT_COPY32_TO_F32)80inst = FMV_W_X | RS1(reg) | FRD(freg);81else82inst = FMV_X_W | FRS1(freg) | RD(reg);8384return push_inst(compiler, inst);85}8687FAIL_IF(push_inst(compiler, ADDI | RD(SLJIT_SP) | RS1(SLJIT_SP) | IMM_I(-16)));8889if (reg & REG_PAIR_MASK) {90reg2 = REG_PAIR_SECOND(reg);91reg = REG_PAIR_FIRST(reg);92}9394if (op == SLJIT_COPY_TO_F64) {95if (reg2 != 0)96FAIL_IF(push_inst(compiler, SW | RS1(SLJIT_SP) | RS2(reg2) | (8 << 7)));97else98FAIL_IF(push_inst(compiler, FSW | RS1(SLJIT_SP) | FRS2(freg) | (8 << 7)));99100FAIL_IF(push_inst(compiler, SW | RS1(SLJIT_SP) | RS2(reg) | (12 << 7)));101FAIL_IF(push_inst(compiler, FLD | FRD(freg) | RS1(SLJIT_SP) | IMM_I(8)));102} else {103FAIL_IF(push_inst(compiler, FSD | RS1(SLJIT_SP) | FRS2(freg) | (8 << 7)));104105if (reg2 != 0)106FAIL_IF(push_inst(compiler, FMV_X_W | FRS1(freg) | RD(reg2)));107108FAIL_IF(push_inst(compiler, LW | RD(reg) | RS1(SLJIT_SP) | IMM_I(12)));109}110111return push_inst(compiler, ADDI | RD(SLJIT_SP) | RS1(SLJIT_SP) | IMM_I(16));112}113114static SLJIT_INLINE sljit_s32 emit_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw init_value, sljit_ins last_ins)115{116if ((init_value & 0x800) != 0)117init_value += 0x1000;118119FAIL_IF(push_inst(compiler, LUI | RD(dst) | (sljit_ins)(init_value & ~0xfff)));120return push_inst(compiler, last_ins | RS1(dst) | IMM_I(init_value));121}122123SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_target, sljit_sw executable_offset)124{125sljit_ins *inst = (sljit_ins*)addr;126SLJIT_UNUSED_ARG(executable_offset);127128if ((new_target & 0x800) != 0)129new_target += 0x1000;130131SLJIT_UPDATE_WX_FLAGS(inst, inst + 5, 0);132133SLJIT_ASSERT((inst[0] & 0x7f) == LUI);134inst[0] = (inst[0] & 0xfff) | (sljit_ins)((sljit_sw)new_target & ~0xfff);135SLJIT_ASSERT((inst[1] & 0x707f) == ADDI || (inst[1] & 0x707f) == JALR);136inst[1] = (inst[1] & 0xfffff) | IMM_I(new_target);137138SLJIT_UPDATE_WX_FLAGS(inst, inst + 5, 1);139inst = (sljit_ins *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);140SLJIT_CACHE_FLUSH(inst, inst + 5);141}142143144