| 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_immediate32(struct sljit_compiler *compiler, sljit_s32 dst_r, sljit_sw imm) |
| 28 |
{ |
| 29 |
SLJIT_ASSERT((imm <= 0x7fffffffl && imm > SIMM_MAX) || (imm >= S32_MIN && imm < SIMM_MIN)); |
| 30 |
|
| 31 |
if (imm > S32_MAX) { |
| 32 |
SLJIT_ASSERT((imm & 0x800) != 0); |
| 33 |
FAIL_IF(push_inst(compiler, LUI | RD(dst_r) | (sljit_ins)0x80000000u)); |
| 34 |
return push_inst(compiler, XORI | RD(dst_r) | RS1(dst_r) | IMM_I(imm)); |
| 35 |
} |
| 36 |
|
| 37 |
if (RISCV_HAS_COMPRESSED(200) && imm <= 0x1ffff && imm >= -0x20000) { |
| 38 |
if (imm > 0x1f7ff) { |
| 39 |
SLJIT_ASSERT((imm & 0x800) != 0); |
| 40 |
FAIL_IF(push_inst16(compiler, C_LUI | C_RD(dst_r) | (sljit_u16)0x1000)); |
| 41 |
return push_inst(compiler, XORI | RD(dst_r) | RS1(dst_r) | IMM_I(imm)); |
| 42 |
} |
| 43 |
|
| 44 |
if ((imm & 0x800) != 0) |
| 45 |
imm += 0x1000; |
| 46 |
|
| 47 |
FAIL_IF(push_inst16(compiler, C_LUI | C_RD(dst_r) | ((sljit_u16)(((imm) & 0x1f000) >> 10) | ((imm) & 0x20000) >> 5))); |
| 48 |
} else { |
| 49 |
if ((imm & 0x800) != 0) |
| 50 |
imm += 0x1000; |
| 51 |
|
| 52 |
FAIL_IF(push_inst(compiler, LUI | RD(dst_r) | (sljit_ins)(imm & ~(sljit_sw)0xfff))); |
| 53 |
} |
| 54 |
|
| 55 |
imm &= 0xfff; |
| 56 |
|
| 57 |
if (imm == 0) |
| 58 |
return SLJIT_SUCCESS; |
| 59 |
|
| 60 |
if (RISCV_HAS_COMPRESSED(200) && (imm <= 0x1f || imm >= 0xfe0)) |
| 61 |
return push_inst16(compiler, C_ADDI | C_RD(dst_r) | C_IMM_I(imm)); |
| 62 |
|
| 63 |
return push_inst(compiler, ADDI | RD(dst_r) | RS1(dst_r) | IMM_I(imm)); |
| 64 |
} |
| 65 |
|
| 66 |
static sljit_s32 load_immediate(struct sljit_compiler *compiler, sljit_s32 dst_r, sljit_sw imm, sljit_s32 tmp_r) |
| 67 |
{ |
| 68 |
sljit_sw high, shift; |
| 69 |
|
| 70 |
if (RISCV_HAS_COMPRESSED(200) && imm <= SIMM16_MAX && imm >= SIMM16_MIN) |
| 71 |
return push_inst16(compiler, C_LI | C_RD(dst_r) | C_IMM_I(imm)); |
| 72 |
|
| 73 |
if (imm <= SIMM_MAX && imm >= SIMM_MIN) |
| 74 |
return push_inst(compiler, ADDI | RD(dst_r) | RS1(TMP_ZERO) | IMM_I(imm)); |
| 75 |
|
| 76 |
if (imm <= 0x7fffffffl && imm >= S32_MIN) |
| 77 |
return load_immediate32(compiler, dst_r, imm); |
| 78 |
|
| 79 |
/* Shifted small immediates. */ |
| 80 |
|
| 81 |
high = imm; |
| 82 |
shift = 0; |
| 83 |
while ((high & 0xff) == 0) { |
| 84 |
high >>= 8; |
| 85 |
shift += 8; |
| 86 |
} |
| 87 |
|
| 88 |
if ((high & 0xf) == 0) { |
| 89 |
high >>= 4; |
| 90 |
shift += 4; |
| 91 |
} |
| 92 |
|
| 93 |
if ((high & 0x3) == 0) { |
| 94 |
high >>= 2; |
| 95 |
shift += 2; |
| 96 |
} |
| 97 |
|
| 98 |
if ((high & 0x1) == 0) { |
| 99 |
high >>= 1; |
| 100 |
shift += 1; |
| 101 |
} |
| 102 |
|
| 103 |
if (high <= 0x7fffffffl && high >= S32_MIN) { |
| 104 |
load_immediate(compiler, dst_r, high, tmp_r); |
| 105 |
|
| 106 |
if (RISCV_HAS_COMPRESSED(200)) |
| 107 |
return push_inst16(compiler, C_SLLI | C_RD(dst_r) | C_IMM_I(shift)); |
| 108 |
return push_inst(compiler, SLLI | RD(dst_r) | RS1(dst_r) | IMM_I(shift)); |
| 109 |
} |
| 110 |
|
| 111 |
/* Trailing zeroes could be used to produce shifted immediates. */ |
| 112 |
|
| 113 |
if (imm <= 0x7ffffffffffl && imm >= -0x80000000000l) { |
| 114 |
high = imm >> 12; |
| 115 |
|
| 116 |
if (imm & 0x800) |
| 117 |
high = ~high; |
| 118 |
|
| 119 |
FAIL_IF(load_immediate32(compiler, dst_r, high)); |
| 120 |
|
| 121 |
if (RISCV_HAS_COMPRESSED(200)) |
| 122 |
FAIL_IF(push_inst16(compiler, C_SLLI | C_RD(dst_r) | (sljit_u16)(12 << 2))); |
| 123 |
else |
| 124 |
FAIL_IF(push_inst(compiler, SLLI | RD(dst_r) | RS1(dst_r) | IMM_I(12))); |
| 125 |
|
| 126 |
SLJIT_ASSERT((imm & 0xfff) != 0); |
| 127 |
return push_inst(compiler, XORI | RD(dst_r) | RS1(dst_r) | IMM_I(imm)); |
| 128 |
} |
| 129 |
|
| 130 |
SLJIT_ASSERT(dst_r != tmp_r); |
| 131 |
|
| 132 |
high = imm >> 32; |
| 133 |
imm = (sljit_s32)imm; |
| 134 |
|
| 135 |
if ((imm & 0x80000000l) != 0) |
| 136 |
high = ~high; |
| 137 |
|
| 138 |
if (high <= 0x7ffff && high >= -0x80000) { |
| 139 |
FAIL_IF(push_inst(compiler, LUI | RD(tmp_r) | (sljit_ins)(high << 12))); |
| 140 |
high = 0x1000; |
| 141 |
} else { |
| 142 |
if ((high & 0x800) != 0) |
| 143 |
high += 0x1000; |
| 144 |
|
| 145 |
FAIL_IF(push_inst(compiler, LUI | RD(tmp_r) | (sljit_ins)(high & ~0xfff))); |
| 146 |
high &= 0xfff; |
| 147 |
} |
| 148 |
|
| 149 |
if (imm <= SIMM_MAX && imm >= SIMM_MIN) { |
| 150 |
if (RISCV_HAS_COMPRESSED(200) && imm <= 0x1f && imm >= -0x20) |
| 151 |
FAIL_IF(push_inst16(compiler, C_LI | C_RD(dst_r) | C_IMM_I(imm))); |
| 152 |
else |
| 153 |
FAIL_IF(push_inst(compiler, ADDI | RD(dst_r) | RS1(TMP_ZERO) | IMM_I(imm))); |
| 154 |
imm = 0; |
| 155 |
} else if (imm > S32_MAX) { |
| 156 |
SLJIT_ASSERT((imm & 0x800) != 0); |
| 157 |
|
| 158 |
FAIL_IF(push_inst(compiler, LUI | RD(dst_r) | (sljit_ins)0x80000000u)); |
| 159 |
imm = 0x1000 | (imm & 0xfff); |
| 160 |
} else { |
| 161 |
if ((imm & 0x800) != 0) |
| 162 |
imm += 0x1000; |
| 163 |
|
| 164 |
if (RISCV_HAS_COMPRESSED(200) && imm <= 0x1ffff && imm >= -0x20000) |
| 165 |
FAIL_IF(push_inst16(compiler, C_LUI | C_RD(dst_r) | ((sljit_u16)(((imm) & 0x1f000) >> 10) | ((imm) & 0x20000) >> 5))); |
| 166 |
else |
| 167 |
FAIL_IF(push_inst(compiler, LUI | RD(dst_r) | (sljit_ins)(imm & ~0xfff))); |
| 168 |
imm &= 0xfff; |
| 169 |
} |
| 170 |
|
| 171 |
if ((high & 0xfff) != 0) { |
| 172 |
SLJIT_ASSERT(high <= 0xfff); |
| 173 |
if (RISCV_HAS_COMPRESSED(200) && (high <= 0x1f || high >= 0xfe0)) |
| 174 |
FAIL_IF(push_inst16(compiler, C_ADDI | C_RD(tmp_r) | C_IMM_I(high))); |
| 175 |
else |
| 176 |
FAIL_IF(push_inst(compiler, ADDI | RD(tmp_r) | RS1(tmp_r) | IMM_I(high))); |
| 177 |
} |
| 178 |
|
| 179 |
if (imm & 0x1000) |
| 180 |
FAIL_IF(push_inst(compiler, XORI | RD(dst_r) | RS1(dst_r) | IMM_I(imm))); |
| 181 |
else if (imm != 0) { |
| 182 |
SLJIT_ASSERT(imm <= 0xfff); |
| 183 |
if (RISCV_HAS_COMPRESSED(200) && (imm <= 0x1f || imm >= 0xfe0)) |
| 184 |
FAIL_IF(push_inst16(compiler, C_ADDI | C_RD(dst_r) | C_IMM_I(imm))); |
| 185 |
else |
| 186 |
FAIL_IF(push_inst(compiler, ADDI | RD(dst_r) | RS1(dst_r) | IMM_I(imm))); |
| 187 |
} |
| 188 |
|
| 189 |
if (RISCV_HAS_COMPRESSED(200)) |
| 190 |
FAIL_IF(push_inst16(compiler, C_SLLI | C_RD(tmp_r) | (sljit_u16)((high & 0x1000) ? (20 << 2) : (1 << 12)))); |
| 191 |
else |
| 192 |
FAIL_IF(push_inst(compiler, SLLI | RD(tmp_r) | RS1(tmp_r) | IMM_I((high & 0x1000) ? 20 : 32))); |
| 193 |
return push_inst(compiler, XOR | RD(dst_r) | RS1(dst_r) | RS2(tmp_r)); |
| 194 |
} |
| 195 |
|
| 196 |
SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fset64(struct sljit_compiler *compiler, |
| 197 |
sljit_s32 freg, sljit_f64 value) |
| 198 |
{ |
| 199 |
union { |
| 200 |
sljit_sw imm; |
| 201 |
sljit_f64 value; |
| 202 |
} u; |
| 203 |
|
| 204 |
CHECK_ERROR(); |
| 205 |
CHECK(check_sljit_emit_fset64(compiler, freg, value)); |
| 206 |
|
| 207 |
u.value = value; |
| 208 |
|
| 209 |
if (u.imm == 0) |
| 210 |
return push_inst(compiler, FMV_W_X | (1 << 25) | RS1(TMP_ZERO) | FRD(freg)); |
| 211 |
|
| 212 |
FAIL_IF(load_immediate(compiler, TMP_REG1, u.imm, TMP_REG3)); |
| 213 |
return push_inst(compiler, FMV_W_X | (1 << 25) | RS1(TMP_REG1) | FRD(freg)); |
| 214 |
} |
| 215 |
|
| 216 |
SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fcopy(struct sljit_compiler *compiler, sljit_s32 op, |
| 217 |
sljit_s32 freg, sljit_s32 reg) |
| 218 |
{ |
| 219 |
sljit_ins inst; |
| 220 |
|
| 221 |
CHECK_ERROR(); |
| 222 |
CHECK(check_sljit_emit_fcopy(compiler, op, freg, reg)); |
| 223 |
|
| 224 |
if (GET_OPCODE(op) == SLJIT_COPY_TO_F64) |
| 225 |
inst = FMV_W_X | RS1(reg) | FRD(freg); |
| 226 |
else |
| 227 |
inst = FMV_X_W | FRS1(freg) | RD(reg); |
| 228 |
|
| 229 |
if (!(op & SLJIT_32)) |
| 230 |
inst |= (sljit_ins)1 << 25; |
| 231 |
|
| 232 |
return push_inst(compiler, inst); |
| 233 |
} |
| 234 |
|
| 235 |
static SLJIT_INLINE sljit_s32 emit_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw init_value, sljit_ins last_ins) |
| 236 |
{ |
| 237 |
sljit_sw high; |
| 238 |
|
| 239 |
if ((init_value & 0x800) != 0) |
| 240 |
init_value += 0x1000; |
| 241 |
|
| 242 |
high = init_value >> 32; |
| 243 |
|
| 244 |
if ((init_value & 0x80000000l) != 0) |
| 245 |
high = ~high; |
| 246 |
|
| 247 |
if ((high & 0x800) != 0) |
| 248 |
high += 0x1000; |
| 249 |
|
| 250 |
FAIL_IF(push_inst(compiler, LUI | RD(TMP_REG3) | (sljit_ins)(high & ~0xfff))); |
| 251 |
FAIL_IF(push_inst(compiler, ADDI | RD(TMP_REG3) | RS1(TMP_REG3) | IMM_I(high))); |
| 252 |
FAIL_IF(push_inst(compiler, LUI | RD(dst) | (sljit_ins)(init_value & ~0xfff))); |
| 253 |
FAIL_IF(push_inst(compiler, SLLI | RD(TMP_REG3) | RS1(TMP_REG3) | IMM_I(32))); |
| 254 |
FAIL_IF(push_inst(compiler, XOR | RD(dst) | RS1(dst) | RS2(TMP_REG3))); |
| 255 |
return push_inst(compiler, last_ins | RS1(dst) | IMM_I(init_value)); |
| 256 |
} |
| 257 |
|
| 258 |
SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_target, sljit_sw executable_offset) |
| 259 |
{ |
| 260 |
sljit_u16 *inst = (sljit_u16*)addr; |
| 261 |
sljit_sw high; |
| 262 |
SLJIT_UNUSED_ARG(executable_offset); |
| 263 |
|
| 264 |
if ((new_target & 0x800) != 0) |
| 265 |
new_target += 0x1000; |
| 266 |
|
| 267 |
high = (sljit_sw)new_target >> 32; |
| 268 |
|
| 269 |
if ((new_target & 0x80000000l) != 0) |
| 270 |
high = ~high; |
| 271 |
|
| 272 |
if ((high & 0x800) != 0) |
| 273 |
high += 0x1000; |
| 274 |
|
| 275 |
SLJIT_UPDATE_WX_FLAGS(inst, inst + 12, 0); |
| 276 |
|
| 277 |
SLJIT_ASSERT((inst[0] & 0x7f) == LUI); |
| 278 |
inst[0] = (sljit_u16)((inst[0] & 0xfff) | (high & 0xf000)); |
| 279 |
inst[1] = (sljit_u16)(high >> 16); |
| 280 |
SLJIT_ASSERT((inst[2] & 0x707f) == ADDI); |
| 281 |
inst[3] = (sljit_u16)((inst[3] & 0xf) | (high << 4)); |
| 282 |
SLJIT_ASSERT((inst[4] & 0x7f) == LUI); |
| 283 |
inst[4] = (sljit_u16)((inst[4] & 0xfff) | (new_target & 0xf000)); |
| 284 |
inst[5] = (sljit_u16)(new_target >> 16); |
| 285 |
SLJIT_ASSERT((inst[10] & 0x707f) == ADDI || (inst[10] & 0x707f) == JALR); |
| 286 |
inst[11] = (sljit_u16)((inst[11] & 0xf) | (new_target << 4)); |
| 287 |
SLJIT_UPDATE_WX_FLAGS(inst, inst + 12, 1); |
| 288 |
|
| 289 |
inst = (sljit_u16 *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset); |
| 290 |
SLJIT_CACHE_FLUSH(inst, inst + 12); |
| 291 |
} |