Path: blob/master/thirdparty/pcre2/deps/sljit/sljit_src/sljitNativeRISCV_64.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_sw high;2930if (imm <= SIMM_MAX && imm >= SIMM_MIN)31return push_inst(compiler, ADDI | RD(dst_r) | RS1(TMP_ZERO) | IMM_I(imm));3233if (imm <= 0x7fffffffl && imm >= S32_MIN) {34if (imm > S32_MAX) {35SLJIT_ASSERT((imm & 0x800) != 0);36FAIL_IF(push_inst(compiler, LUI | RD(dst_r) | (sljit_ins)0x80000000u));37return push_inst(compiler, XORI | RD(dst_r) | RS1(dst_r) | IMM_I(imm));38}3940if ((imm & 0x800) != 0)41imm += 0x1000;4243FAIL_IF(push_inst(compiler, LUI | RD(dst_r) | (sljit_ins)(imm & ~0xfff)));4445if ((imm & 0xfff) == 0)46return SLJIT_SUCCESS;4748return push_inst(compiler, ADDI | RD(dst_r) | RS1(dst_r) | IMM_I(imm));49}5051/* Trailing zeroes could be used to produce shifted immediates. */5253if (imm <= 0x7ffffffffffl && imm >= -0x80000000000l) {54high = imm >> 12;5556if (imm & 0x800)57high = ~high;5859if (high > S32_MAX) {60SLJIT_ASSERT((high & 0x800) != 0);61FAIL_IF(push_inst(compiler, LUI | RD(dst_r) | (sljit_ins)0x80000000u));62FAIL_IF(push_inst(compiler, XORI | RD(dst_r) | RS1(dst_r) | IMM_I(high)));63} else {64if ((high & 0x800) != 0)65high += 0x1000;6667FAIL_IF(push_inst(compiler, LUI | RD(dst_r) | (sljit_ins)(high & ~0xfff)));6869if ((high & 0xfff) != 0)70FAIL_IF(push_inst(compiler, ADDI | RD(dst_r) | RS1(dst_r) | IMM_I(high)));71}7273FAIL_IF(push_inst(compiler, SLLI | RD(dst_r) | RS1(dst_r) | IMM_I(12)));7475if ((imm & 0xfff) != 0)76return push_inst(compiler, XORI | RD(dst_r) | RS1(dst_r) | IMM_I(imm));7778return SLJIT_SUCCESS;79}8081SLJIT_ASSERT(dst_r != tmp_r);8283high = imm >> 32;84imm = (sljit_s32)imm;8586if ((imm & 0x80000000l) != 0)87high = ~high;8889if (high <= 0x7ffff && high >= -0x80000) {90FAIL_IF(push_inst(compiler, LUI | RD(tmp_r) | (sljit_ins)(high << 12)));91high = 0x1000;92} else {93if ((high & 0x800) != 0)94high += 0x1000;9596FAIL_IF(push_inst(compiler, LUI | RD(tmp_r) | (sljit_ins)(high & ~0xfff)));97high &= 0xfff;98}99100if (imm <= SIMM_MAX && imm >= SIMM_MIN) {101FAIL_IF(push_inst(compiler, ADDI | RD(dst_r) | RS1(TMP_ZERO) | IMM_I(imm)));102imm = 0;103} else if (imm > S32_MAX) {104SLJIT_ASSERT((imm & 0x800) != 0);105106FAIL_IF(push_inst(compiler, LUI | RD(dst_r) | (sljit_ins)0x80000000u));107imm = 0x1000 | (imm & 0xfff);108} else {109if ((imm & 0x800) != 0)110imm += 0x1000;111112FAIL_IF(push_inst(compiler, LUI | RD(dst_r) | (sljit_ins)(imm & ~0xfff)));113imm &= 0xfff;114}115116if ((high & 0xfff) != 0)117FAIL_IF(push_inst(compiler, ADDI | RD(tmp_r) | RS1(tmp_r) | IMM_I(high)));118119if (imm & 0x1000)120FAIL_IF(push_inst(compiler, XORI | RD(dst_r) | RS1(dst_r) | IMM_I(imm)));121else if (imm != 0)122FAIL_IF(push_inst(compiler, ADDI | RD(dst_r) | RS1(dst_r) | IMM_I(imm)));123124FAIL_IF(push_inst(compiler, SLLI | RD(tmp_r) | RS1(tmp_r) | IMM_I((high & 0x1000) ? 20 : 32)));125return push_inst(compiler, XOR | RD(dst_r) | RS1(dst_r) | RS2(tmp_r));126}127128SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fset64(struct sljit_compiler *compiler,129sljit_s32 freg, sljit_f64 value)130{131union {132sljit_sw imm;133sljit_f64 value;134} u;135136CHECK_ERROR();137CHECK(check_sljit_emit_fset64(compiler, freg, value));138139u.value = value;140141if (u.imm == 0)142return push_inst(compiler, FMV_W_X | (1 << 25) | RS1(TMP_ZERO) | FRD(freg));143144FAIL_IF(load_immediate(compiler, TMP_REG1, u.imm, TMP_REG3));145return push_inst(compiler, FMV_W_X | (1 << 25) | RS1(TMP_REG1) | FRD(freg));146}147148SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fcopy(struct sljit_compiler *compiler, sljit_s32 op,149sljit_s32 freg, sljit_s32 reg)150{151sljit_ins inst;152153CHECK_ERROR();154CHECK(check_sljit_emit_fcopy(compiler, op, freg, reg));155156if (GET_OPCODE(op) == SLJIT_COPY_TO_F64)157inst = FMV_W_X | RS1(reg) | FRD(freg);158else159inst = FMV_X_W | FRS1(freg) | RD(reg);160161if (!(op & SLJIT_32))162inst |= (sljit_ins)1 << 25;163164return push_inst(compiler, inst);165}166167static SLJIT_INLINE sljit_s32 emit_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw init_value, sljit_ins last_ins)168{169sljit_sw high;170171if ((init_value & 0x800) != 0)172init_value += 0x1000;173174high = init_value >> 32;175176if ((init_value & 0x80000000l) != 0)177high = ~high;178179if ((high & 0x800) != 0)180high += 0x1000;181182FAIL_IF(push_inst(compiler, LUI | RD(TMP_REG3) | (sljit_ins)(high & ~0xfff)));183FAIL_IF(push_inst(compiler, ADDI | RD(TMP_REG3) | RS1(TMP_REG3) | IMM_I(high)));184FAIL_IF(push_inst(compiler, LUI | RD(dst) | (sljit_ins)(init_value & ~0xfff)));185FAIL_IF(push_inst(compiler, SLLI | RD(TMP_REG3) | RS1(TMP_REG3) | IMM_I(32)));186FAIL_IF(push_inst(compiler, XOR | RD(dst) | RS1(dst) | RS2(TMP_REG3)));187return push_inst(compiler, last_ins | RS1(dst) | IMM_I(init_value));188}189190SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_target, sljit_sw executable_offset)191{192sljit_ins *inst = (sljit_ins*)addr;193sljit_sw high;194SLJIT_UNUSED_ARG(executable_offset);195196if ((new_target & 0x800) != 0)197new_target += 0x1000;198199high = (sljit_sw)new_target >> 32;200201if ((new_target & 0x80000000l) != 0)202high = ~high;203204if ((high & 0x800) != 0)205high += 0x1000;206207SLJIT_UPDATE_WX_FLAGS(inst, inst + 5, 0);208209SLJIT_ASSERT((inst[0] & 0x7f) == LUI);210inst[0] = (inst[0] & 0xfff) | (sljit_ins)(high & ~0xfff);211SLJIT_ASSERT((inst[1] & 0x707f) == ADDI);212inst[1] = (inst[1] & 0xfffff) | IMM_I(high);213SLJIT_ASSERT((inst[2] & 0x7f) == LUI);214inst[2] = (inst[2] & 0xfff) | (sljit_ins)((sljit_sw)new_target & ~0xfff);215SLJIT_ASSERT((inst[5] & 0x707f) == ADDI || (inst[5] & 0x707f) == JALR);216inst[5] = (inst[5] & 0xfffff) | IMM_I(new_target);217SLJIT_UPDATE_WX_FLAGS(inst, inst + 5, 1);218219inst = (sljit_ins *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);220SLJIT_CACHE_FLUSH(inst, inst + 5);221}222223224