Debian Codegraph @

src/3rdparty/pcre2/deps/sljit/sljit_src/sljitNativeRISCV_32.c

raw
1 /*
2 * Stack-less Just-In-Time compiler
3 *
4 * Copyright Zoltan Herczeg ([email protected]). All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without modification, are
7 * permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice, this list of
10 * conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
13 * of conditions and the following disclaimer in the documentation and/or other materials
14 * provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) AND CONTRIBUTORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19 * SHALL THE COPYRIGHT HOLDER(S) OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
22 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 static sljit_s32 load_immediate(struct sljit_compiler *compiler, sljit_s32 dst_r, sljit_sw imm, sljit_s32 tmp_r)
28 {
29 SLJIT_UNUSED_ARG(tmp_r);
30
31 if (RISCV_HAS_COMPRESSED(200) && imm <= SIMM16_MAX && imm >= SIMM16_MIN)
32 return push_inst16(compiler, C_LI | C_RD(dst_r) | C_IMM_I(imm));
33
34 if (imm <= SIMM_MAX && imm >= SIMM_MIN)
35 return push_inst(compiler, ADDI | RD(dst_r) | RS1(TMP_ZERO) | IMM_I(imm));
36
37 if (imm & 0x800)
38 imm += 0x1000;
39
40 if (RISCV_HAS_COMPRESSED(200) && imm <= 0x1ffff && imm >= -0x20000)
41 FAIL_IF(push_inst16(compiler, C_LUI | C_RD(dst_r) | ((sljit_u16)(((imm) & 0x1f000) >> 10) | ((imm) & 0x20000) >> 5)));
42 else
43 FAIL_IF(push_inst(compiler, LUI | RD(dst_r) | (sljit_ins)(imm & ~(sljit_sw)0xfff)));
44
45 imm &= 0xfff;
46
47 if (imm == 0)
48 return SLJIT_SUCCESS;
49
50 if (RISCV_HAS_COMPRESSED(200) && (imm <= 0x1f || imm >= 0xfe0))
51 return push_inst16(compiler, C_ADDI | C_RD(dst_r) | C_IMM_I(imm));
52
53 return push_inst(compiler, ADDI | RD(dst_r) | RS1(dst_r) | IMM_I(imm));
54 }
55
56 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fset64(struct sljit_compiler *compiler,
57 sljit_s32 freg, sljit_f64 value)
58 {
59 union {
60 sljit_s32 imm[2];
61 sljit_f64 value;
62 } u;
63
64 CHECK_ERROR();
65 CHECK(check_sljit_emit_fset64(compiler, freg, value));
66
67 u.value = value;
68
69 if (u.imm[0] != 0)
70 FAIL_IF(load_immediate(compiler, TMP_REG1, u.imm[0], TMP_REG3));
71 if (u.imm[1] != 0)
72 FAIL_IF(load_immediate(compiler, TMP_REG2, u.imm[1], TMP_REG3));
73
74 FAIL_IF(push_inst(compiler, ADDI | RD(SLJIT_SP) | RS1(SLJIT_SP) | IMM_I(-16)));
75 FAIL_IF(push_inst(compiler, SW | RS1(SLJIT_SP) | RS2(u.imm[0] != 0 ? TMP_REG1 : TMP_ZERO) | (8 << 7)));
76 FAIL_IF(push_inst(compiler, SW | RS1(SLJIT_SP) | RS2(u.imm[1] != 0 ? TMP_REG2 : TMP_ZERO) | (12 << 7)));
77 FAIL_IF(push_inst(compiler, FLD | FRD(freg) | RS1(SLJIT_SP) | IMM_I(8)));
78 return push_inst(compiler, ADDI | RD(SLJIT_SP) | RS1(SLJIT_SP) | IMM_I(16));
79 }
80
81 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fcopy(struct sljit_compiler *compiler, sljit_s32 op,
82 sljit_s32 freg, sljit_s32 reg)
83 {
84 sljit_ins inst;
85 sljit_s32 reg2 = 0;
86
87 CHECK_ERROR();
88 CHECK(check_sljit_emit_fcopy(compiler, op, freg, reg));
89
90 if (op & SLJIT_32) {
91 if (op == SLJIT_COPY32_TO_F32)
92 inst = FMV_W_X | RS1(reg) | FRD(freg);
93 else
94 inst = FMV_X_W | FRS1(freg) | RD(reg);
95
96 return push_inst(compiler, inst);
97 }
98
99 FAIL_IF(push_inst(compiler, ADDI | RD(SLJIT_SP) | RS1(SLJIT_SP) | IMM_I(-16)));
100
101 if (reg & REG_PAIR_MASK) {
102 reg2 = REG_PAIR_SECOND(reg);
103 reg = REG_PAIR_FIRST(reg);
104 }
105
106 if (op == SLJIT_COPY_TO_F64) {
107 if (reg2 != 0)
108 FAIL_IF(push_inst(compiler, SW | RS1(SLJIT_SP) | RS2(reg2) | (8 << 7)));
109 else
110 FAIL_IF(push_inst(compiler, FSW | RS1(SLJIT_SP) | FRS2(freg) | (8 << 7)));
111
112 FAIL_IF(push_inst(compiler, SW | RS1(SLJIT_SP) | RS2(reg) | (12 << 7)));
113 FAIL_IF(push_inst(compiler, FLD | FRD(freg) | RS1(SLJIT_SP) | IMM_I(8)));
114 } else {
115 FAIL_IF(push_inst(compiler, FSD | RS1(SLJIT_SP) | FRS2(freg) | (8 << 7)));
116
117 if (reg2 != 0)
118 FAIL_IF(push_inst(compiler, FMV_X_W | FRS1(freg) | RD(reg2)));
119
120 FAIL_IF(push_inst(compiler, LW | RD(reg) | RS1(SLJIT_SP) | IMM_I(12)));
121 }
122
123 return push_inst(compiler, ADDI | RD(SLJIT_SP) | RS1(SLJIT_SP) | IMM_I(16));
124 }
125
126 static SLJIT_INLINE sljit_s32 emit_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw init_value, sljit_ins last_ins)
127 {
128 if ((init_value & 0x800) != 0)
129 init_value += 0x1000;
130
131 FAIL_IF(push_inst(compiler, LUI | RD(dst) | (sljit_ins)(init_value & ~0xfff)));
132 return push_inst(compiler, last_ins | RS1(dst) | IMM_I(init_value));
133 }
134
135 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_target, sljit_sw executable_offset)
136 {
137 sljit_u16 *inst = (sljit_u16*)addr;
138 SLJIT_UNUSED_ARG(executable_offset);
139
140 if ((new_target & 0x800) != 0)
141 new_target += 0x1000;
142
143 SLJIT_UPDATE_WX_FLAGS(inst, inst + 4, 0);
144
145 SLJIT_ASSERT((inst[0] & 0x7f) == LUI);
146 inst[0] = (sljit_u16)((inst[0] & 0xfff) | (new_target & 0xf000));
147 inst[1] = (sljit_u16)(new_target >> 16);
148 SLJIT_ASSERT((inst[2] & 0x707f) == ADDI || (inst[2] & 0x707f) == JALR);
149 inst[3] = (sljit_u16)((inst[3] & 0xf) | (new_target << 4));
150
151 SLJIT_UPDATE_WX_FLAGS(inst, inst + 4, 1);
152 inst = (sljit_u16 *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
153 SLJIT_CACHE_FLUSH(inst, inst + 4);
154 }