1 /* $NetBSD: sljitNativeARM_64.c,v 1.2 2014/06/17 19:33:20 alnsn Exp $ */ 2 3 /* 4 * Stack-less Just-In-Time compiler 5 * 6 * Copyright 2009-2012 Zoltan Herczeg (hzmester@freemail.hu). All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without modification, are 9 * permitted provided that the following conditions are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright notice, this list of 12 * conditions and the following disclaimer. 13 * 14 * 2. Redistributions in binary form must reproduce the above copyright notice, this list 15 * of conditions and the following disclaimer in the documentation and/or other materials 16 * provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) AND CONTRIBUTORS ``AS IS'' AND ANY 19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 21 * SHALL THE COPYRIGHT HOLDER(S) OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 23 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 26 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 SLJIT_API_FUNC_ATTRIBUTE SLJIT_CONST char* sljit_get_platform_name(void) 30 { 31 return "ARM-64" SLJIT_CPUINFO; 32 } 33 34 /* Length of an instruction word */ 35 typedef sljit_ui sljit_ins; 36 37 #define TMP_ZERO 0 38 39 #define TMP_REG1 (SLJIT_NO_REGISTERS + 1) 40 #define TMP_REG2 (SLJIT_NO_REGISTERS + 2) 41 #define TMP_REG3 (SLJIT_NO_REGISTERS + 3) 42 #define TMP_REG4 (SLJIT_NO_REGISTERS + 4) 43 #define TMP_LR (SLJIT_NO_REGISTERS + 5) 44 #define TMP_SP (SLJIT_NO_REGISTERS + 6) 45 46 #define TMP_FREG1 (0) 47 #define TMP_FREG2 (SLJIT_FLOAT_REG6 + 1) 48 49 static SLJIT_CONST sljit_ub reg_map[SLJIT_NO_REGISTERS + 7] = { 50 31, 0, 1, 2, 3, 4, 19, 20, 21, 22, 23, 29, 9, 10, 11, 12, 30, 31 51 }; 52 53 #define W_OP (1 << 31) 54 #define RD(rd) (reg_map[rd]) 55 #define RT(rt) (reg_map[rt]) 56 #define RN(rn) (reg_map[rn] << 5) 57 #define RT2(rt2) (reg_map[rt2] << 10) 58 #define RM(rm) (reg_map[rm] << 16) 59 #define VD(vd) (vd) 60 #define VT(vt) (vt) 61 #define VN(vn) ((vn) << 5) 62 #define VM(vm) ((vm) << 16) 63 64 /* --------------------------------------------------------------------- */ 65 /* Instrucion forms */ 66 /* --------------------------------------------------------------------- */ 67 68 #define ADC 0x9a000000 69 #define ADD 0x8b000000 70 #define ADDI 0x91000000 71 #define AND 0x8a000000 72 #define ANDI 0x92000000 73 #define ASRV 0x9ac02800 74 #define B 0x14000000 75 #define B_CC 0x54000000 76 #define BL 0x94000000 77 #define BLR 0xd63f0000 78 #define BR 0xd61f0000 79 #define BRK 0xd4200000 80 #define CBZ 0xb4000000 81 #define CLZ 0xdac01000 82 #define CSINC 0x9a800400 83 #define EOR 0xca000000 84 #define EORI 0xd2000000 85 #define FABS 0x1e60c000 86 #define FADD 0x1e602800 87 #define FCMP 0x1e602000 88 #define FDIV 0x1e601800 89 #define FMOV 0x1e604000 90 #define FMUL 0x1e600800 91 #define FNEG 0x1e614000 92 #define FSUB 0x1e603800 93 #define LDRI 0xf9400000 94 #define LDP 0xa9400000 95 #define LDP_PST 0xa8c00000 96 #define LSLV 0x9ac02000 97 #define LSRV 0x9ac02400 98 #define MADD 0x9b000000 99 #define MOVK 0xf2800000 100 #define MOVN 0x92800000 101 #define MOVZ 0xd2800000 102 #define NOP 0xd503201f 103 #define ORN 0xaa200000 104 #define ORR 0xaa000000 105 #define ORRI 0xb2000000 106 #define RET 0xd65f0000 107 #define SBC 0xda000000 108 #define SBFM 0x93000000 109 #define SDIV 0x9ac00c00 110 #define SMADDL 0x9b200000 111 #define SMULH 0x9b403c00 112 #define STP 0xa9000000 113 #define STP_PRE 0xa9800000 114 #define STRI 0xf9000000 115 #define STR_FI 0x3d000000 116 #define STR_FR 0x3c206800 117 #define STUR_FI 0x3c000000 118 #define SUB 0xcb000000 119 #define SUBI 0xd1000000 120 #define SUBS 0xeb000000 121 #define UBFM 0xd3000000 122 #define UDIV 0x9ac00800 123 #define UMULH 0x9bc03c00 124 125 /* dest_reg is the absolute name of the register 126 Useful for reordering instructions in the delay slot. */ 127 static sljit_si push_inst(struct sljit_compiler *compiler, sljit_ins ins) 128 { 129 sljit_ins *ptr = (sljit_ins*)ensure_buf(compiler, sizeof(sljit_ins)); 130 FAIL_IF(!ptr); 131 *ptr = ins; 132 compiler->size++; 133 return SLJIT_SUCCESS; 134 } 135 136 static SLJIT_INLINE sljit_si emit_imm64_const(struct sljit_compiler *compiler, sljit_si dst, sljit_uw imm) 137 { 138 FAIL_IF(push_inst(compiler, MOVZ | RD(dst) | ((imm & 0xffff) << 5))); 139 FAIL_IF(push_inst(compiler, MOVK | RD(dst) | (((imm >> 16) & 0xffff) << 5) | (1 << 21))); 140 FAIL_IF(push_inst(compiler, MOVK | RD(dst) | (((imm >> 32) & 0xffff) << 5) | (2 << 21))); 141 return push_inst(compiler, MOVK | RD(dst) | ((imm >> 48) << 5) | (3 << 21)); 142 } 143 144 static SLJIT_INLINE void modify_imm64_const(sljit_ins* inst, sljit_uw new_imm) 145 { 146 sljit_si dst = inst[0] & 0x1f; 147 SLJIT_ASSERT((inst[0] & 0xffe00000) == MOVZ && (inst[1] & 0xffe00000) == (MOVK | (1 << 21))); 148 inst[0] = MOVZ | dst | ((new_imm & 0xffff) << 5); 149 inst[1] = MOVK | dst | (((new_imm >> 16) & 0xffff) << 5) | (1 << 21); 150 inst[2] = MOVK | dst | (((new_imm >> 32) & 0xffff) << 5) | (2 << 21); 151 inst[3] = MOVK | dst | ((new_imm >> 48) << 5) | (3 << 21); 152 } 153 154 static SLJIT_INLINE sljit_si detect_jump_type(struct sljit_jump *jump, sljit_ins *code_ptr, sljit_ins *code) 155 { 156 sljit_sw diff; 157 sljit_uw target_addr; 158 159 if (jump->flags & SLJIT_REWRITABLE_JUMP) { 160 jump->flags |= PATCH_ABS64; 161 return 0; 162 } 163 164 if (jump->flags & JUMP_ADDR) 165 target_addr = jump->u.target; 166 else { 167 SLJIT_ASSERT(jump->flags & JUMP_LABEL); 168 target_addr = (sljit_uw)(code + jump->u.label->size); 169 } 170 diff = (sljit_sw)target_addr - (sljit_sw)(code_ptr + 4); 171 172 if (jump->flags & IS_COND) { 173 diff += sizeof(sljit_ins); 174 if (diff <= 0xfffff && diff >= -0x100000) { 175 code_ptr[-5] ^= (jump->flags & IS_CBZ) ? (0x1 << 24) : 0x1; 176 jump->addr -= sizeof(sljit_ins); 177 jump->flags |= PATCH_COND; 178 return 5; 179 } 180 diff -= sizeof(sljit_ins); 181 } 182 183 if (diff <= 0x7ffffff && diff >= -0x8000000) { 184 jump->flags |= PATCH_B; 185 return 4; 186 } 187 188 if (target_addr <= 0xffffffffl) { 189 if (jump->flags & IS_COND) 190 code_ptr[-5] -= (2 << 5); 191 code_ptr[-2] = code_ptr[0]; 192 return 2; 193 } 194 if (target_addr <= 0xffffffffffffl) { 195 if (jump->flags & IS_COND) 196 code_ptr[-5] -= (1 << 5); 197 jump->flags |= PATCH_ABS48; 198 code_ptr[-1] = code_ptr[0]; 199 return 1; 200 } 201 202 jump->flags |= PATCH_ABS64; 203 return 0; 204 } 205 206 SLJIT_API_FUNC_ATTRIBUTE void* sljit_generate_code(struct sljit_compiler *compiler) 207 { 208 struct sljit_memory_fragment *buf; 209 sljit_ins *code; 210 sljit_ins *code_ptr; 211 sljit_ins *buf_ptr; 212 sljit_ins *buf_end; 213 sljit_uw word_count; 214 sljit_uw addr; 215 sljit_si dst; 216 217 struct sljit_label *label; 218 struct sljit_jump *jump; 219 struct sljit_const *const_; 220 221 CHECK_ERROR_PTR(); 222 check_sljit_generate_code(compiler); 223 reverse_buf(compiler); 224 225 code = (sljit_ins*)SLJIT_MALLOC_EXEC(compiler->size * sizeof(sljit_ins)); 226 PTR_FAIL_WITH_EXEC_IF(code); 227 buf = compiler->buf; 228 229 code_ptr = code; 230 word_count = 0; 231 label = compiler->labels; 232 jump = compiler->jumps; 233 const_ = compiler->consts; 234 235 do { 236 buf_ptr = (sljit_ins*)buf->memory; 237 buf_end = buf_ptr + (buf->used_size >> 2); 238 do { 239 *code_ptr = *buf_ptr++; 240 /* These structures are ordered by their address. */ 241 SLJIT_ASSERT(!label || label->size >= word_count); 242 SLJIT_ASSERT(!jump || jump->addr >= word_count); 243 SLJIT_ASSERT(!const_ || const_->addr >= word_count); 244 if (label && label->size == word_count) { 245 label->addr = (sljit_uw)code_ptr; 246 label->size = code_ptr - code; 247 label = label->next; 248 } 249 if (jump && jump->addr == word_count) { 250 jump->addr = (sljit_uw)(code_ptr - 4); 251 code_ptr -= detect_jump_type(jump, code_ptr, code); 252 jump = jump->next; 253 } 254 if (const_ && const_->addr == word_count) { 255 const_->addr = (sljit_uw)code_ptr; 256 const_ = const_->next; 257 } 258 code_ptr ++; 259 word_count ++; 260 } while (buf_ptr < buf_end); 261 262 buf = buf->next; 263 } while (buf); 264 265 if (label && label->size == word_count) { 266 label->addr = (sljit_uw)code_ptr; 267 label->size = code_ptr - code; 268 label = label->next; 269 } 270 271 SLJIT_ASSERT(!label); 272 SLJIT_ASSERT(!jump); 273 SLJIT_ASSERT(!const_); 274 SLJIT_ASSERT(code_ptr - code <= (sljit_sw)compiler->size); 275 276 jump = compiler->jumps; 277 while (jump) { 278 do { 279 addr = (jump->flags & JUMP_LABEL) ? jump->u.label->addr : jump->u.target; 280 buf_ptr = (sljit_ins*)jump->addr; 281 if (jump->flags & PATCH_B) { 282 addr = (sljit_sw)(addr - jump->addr) >> 2; 283 SLJIT_ASSERT((sljit_sw)addr <= 0x1ffffff && (sljit_sw)addr >= -0x2000000); 284 buf_ptr[0] = ((jump->flags & IS_BL) ? BL : B) | (addr & 0x3ffffff); 285 if (jump->flags & IS_COND) 286 buf_ptr[-1] -= (4 << 5); 287 break; 288 } 289 if (jump->flags & PATCH_COND) { 290 addr = (sljit_sw)(addr - jump->addr) >> 2; 291 SLJIT_ASSERT((sljit_sw)addr <= 0x3ffff && (sljit_sw)addr >= -0x40000); 292 buf_ptr[0] = (buf_ptr[0] & ~0xffffe0) | ((addr & 0x7ffff) << 5); 293 break; 294 } 295 296 SLJIT_ASSERT((jump->flags & (PATCH_ABS48 | PATCH_ABS64)) || addr <= 0xffffffffl); 297 SLJIT_ASSERT((jump->flags & PATCH_ABS64) || addr <= 0xffffffffffffl); 298 299 dst = buf_ptr[0] & 0x1f; 300 buf_ptr[0] = MOVZ | dst | ((addr & 0xffff) << 5); 301 buf_ptr[1] = MOVK | dst | (((addr >> 16) & 0xffff) << 5) | (1 << 21); 302 if (jump->flags & (PATCH_ABS48 | PATCH_ABS64)) 303 buf_ptr[2] = MOVK | dst | (((addr >> 32) & 0xffff) << 5) | (2 << 21); 304 if (jump->flags & PATCH_ABS64) 305 buf_ptr[3] = MOVK | dst | (((addr >> 48) & 0xffff) << 5) | (3 << 21); 306 } while (0); 307 jump = jump->next; 308 } 309 310 compiler->error = SLJIT_ERR_COMPILED; 311 compiler->executable_size = (code_ptr - code) * sizeof(sljit_ins); 312 SLJIT_CACHE_FLUSH(code, code_ptr); 313 return code; 314 } 315 316 /* --------------------------------------------------------------------- */ 317 /* Core code generator functions. */ 318 /* --------------------------------------------------------------------- */ 319 320 #define COUNT_TRAILING_ZERO(value, result) \ 321 result = 0; \ 322 if (!(value & 0xffffffff)) { \ 323 result += 32; \ 324 value >>= 32; \ 325 } \ 326 if (!(value & 0xffff)) { \ 327 result += 16; \ 328 value >>= 16; \ 329 } \ 330 if (!(value & 0xff)) { \ 331 result += 8; \ 332 value >>= 8; \ 333 } \ 334 if (!(value & 0xf)) { \ 335 result += 4; \ 336 value >>= 4; \ 337 } \ 338 if (!(value & 0x3)) { \ 339 result += 2; \ 340 value >>= 2; \ 341 } \ 342 if (!(value & 0x1)) { \ 343 result += 1; \ 344 value >>= 1; \ 345 } 346 347 #define LOGICAL_IMM_CHECK 0x100 348 349 static sljit_ins logical_imm(sljit_sw imm, sljit_si len) 350 { 351 sljit_si negated, ones, right; 352 sljit_uw mask, uimm; 353 sljit_ins ins; 354 355 if (len & LOGICAL_IMM_CHECK) { 356 len &= ~LOGICAL_IMM_CHECK; 357 if (len == 32 && (imm == 0 || imm == -1)) 358 return 0; 359 if (len == 16 && ((sljit_si)imm == 0 || (sljit_si)imm == -1)) 360 return 0; 361 } 362 363 SLJIT_ASSERT((len == 32 && imm != 0 && imm != -1) 364 || (len == 16 && (sljit_si)imm != 0 && (sljit_si)imm != -1)); 365 uimm = (sljit_uw)imm; 366 while (1) { 367 if (len <= 0) { 368 SLJIT_ASSERT_STOP(); 369 return 0; 370 } 371 mask = ((sljit_uw)1 << len) - 1; 372 if ((uimm & mask) != ((uimm >> len) & mask)) 373 break; 374 len >>= 1; 375 } 376 377 len <<= 1; 378 379 negated = 0; 380 if (uimm & 0x1) { 381 negated = 1; 382 uimm = ~uimm; 383 } 384 385 if (len < 64) 386 uimm &= ((sljit_uw)1 << len) - 1; 387 388 /* Unsigned right shift. */ 389 COUNT_TRAILING_ZERO(uimm, right); 390 391 /* Signed shift. We also know that the highest bit is set. */ 392 imm = (sljit_sw)~uimm; 393 SLJIT_ASSERT(imm < 0); 394 395 COUNT_TRAILING_ZERO(imm, ones); 396 397 if (~imm) 398 return 0; 399 400 if (len == 64) 401 ins = 1 << 22; 402 else 403 ins = (0x3f - ((len << 1) - 1)) << 10; 404 405 if (negated) 406 return ins | ((len - ones - 1) << 10) | ((len - ones - right) << 16); 407 408 return ins | ((ones - 1) << 10) | ((len - right) << 16); 409 } 410 411 #undef COUNT_TRAILING_ZERO 412 413 static sljit_si load_immediate(struct sljit_compiler *compiler, sljit_si dst, sljit_sw simm) 414 { 415 sljit_uw imm = (sljit_uw)simm; 416 sljit_si i, zeros, ones, first; 417 sljit_ins bitmask; 418 419 if (imm <= 0xffff) 420 return push_inst(compiler, MOVZ | RD(dst) | (imm << 5)); 421 422 if (simm >= -0x10000 && simm < 0) 423 return push_inst(compiler, MOVN | RD(dst) | ((~imm & 0xffff) << 5)); 424 425 if (imm <= 0xffffffffl) { 426 if ((imm & 0xffff0000l) == 0xffff0000) 427 return push_inst(compiler, (MOVN ^ W_OP) | RD(dst) | ((~imm & 0xffff) << 5)); 428 if ((imm & 0xffff) == 0xffff) 429 return push_inst(compiler, (MOVN ^ W_OP) | RD(dst) | ((~imm & 0xffff0000l) >> (16 - 5)) | (1 << 21)); 430 bitmask = logical_imm(simm, 16); 431 if (bitmask != 0) 432 return push_inst(compiler, (ORRI ^ W_OP) | RD(dst) | RN(TMP_ZERO) | bitmask); 433 } 434 else { 435 bitmask = logical_imm(simm, 32); 436 if (bitmask != 0) 437 return push_inst(compiler, ORRI | RD(dst) | RN(TMP_ZERO) | bitmask); 438 } 439 440 if (imm <= 0xffffffffl) { 441 FAIL_IF(push_inst(compiler, MOVZ | RD(dst) | ((imm & 0xffff) << 5))); 442 return push_inst(compiler, MOVK | RD(dst) | ((imm & 0xffff0000l) >> (16 - 5)) | (1 << 21)); 443 } 444 445 if (simm >= -0x100000000l && simm < 0) { 446 FAIL_IF(push_inst(compiler, MOVN | RD(dst) | ((~imm & 0xffff) << 5))); 447 return push_inst(compiler, MOVK | RD(dst) | ((imm & 0xffff0000l) >> (16 - 5)) | (1 << 21)); 448 } 449 450 /* A large amount of number can be constructed from ORR and MOVx, 451 but computing them is costly. We don't */ 452 453 zeros = 0; 454 ones = 0; 455 for (i = 4; i > 0; i--) { 456 if ((simm & 0xffff) == 0) 457 zeros++; 458 if ((simm & 0xffff) == 0xffff) 459 ones++; 460 simm >>= 16; 461 } 462 463 simm = (sljit_sw)imm; 464 first = 1; 465 if (ones > zeros) { 466 simm = ~simm; 467 for (i = 0; i < 4; i++) { 468 if (!(simm & 0xffff)) { 469 simm >>= 16; 470 continue; 471 } 472 if (first) { 473 first = 0; 474 FAIL_IF(push_inst(compiler, MOVN | RD(dst) | ((simm & 0xffff) << 5) | (i << 21))); 475 } 476 else 477 FAIL_IF(push_inst(compiler, MOVK | RD(dst) | ((~simm & 0xffff) << 5) | (i << 21))); 478 simm >>= 16; 479 } 480 return SLJIT_SUCCESS; 481 } 482 483 for (i = 0; i < 4; i++) { 484 if (!(simm & 0xffff)) { 485 simm >>= 16; 486 continue; 487 } 488 if (first) { 489 first = 0; 490 FAIL_IF(push_inst(compiler, MOVZ | RD(dst) | ((simm & 0xffff) << 5) | (i << 21))); 491 } 492 else 493 FAIL_IF(push_inst(compiler, MOVK | RD(dst) | ((simm & 0xffff) << 5) | (i << 21))); 494 simm >>= 16; 495 } 496 return SLJIT_SUCCESS; 497 } 498 499 #define ARG1_IMM 0x0010000 500 #define ARG2_IMM 0x0020000 501 #define INT_OP 0x0040000 502 #define SET_FLAGS 0x0080000 503 #define UNUSED_RETURN 0x0100000 504 #define SLOW_DEST 0x0200000 505 #define SLOW_SRC1 0x0400000 506 #define SLOW_SRC2 0x0800000 507 508 #define CHECK_FLAGS(flag_bits) \ 509 if (flags & SET_FLAGS) { \ 510 inv_bits |= flag_bits; \ 511 if (flags & UNUSED_RETURN) \ 512 dst = TMP_ZERO; \ 513 } 514 515 static sljit_si emit_op_imm(struct sljit_compiler *compiler, sljit_si flags, sljit_si dst, sljit_sw arg1, sljit_sw arg2) 516 { 517 /* dst must be register, TMP_REG1 518 arg1 must be register, TMP_REG1, imm 519 arg2 must be register, TMP_REG2, imm */ 520 sljit_ins inv_bits = (flags & INT_OP) ? (1 << 31) : 0; 521 sljit_ins inst_bits; 522 sljit_si op = (flags & 0xffff); 523 sljit_si reg; 524 sljit_sw imm, nimm; 525 526 if (SLJIT_UNLIKELY((flags & (ARG1_IMM | ARG2_IMM)) == (ARG1_IMM | ARG2_IMM))) { 527 /* Both are immediates. */ 528 flags &= ~ARG1_IMM; 529 if (arg1 == 0 && op != SLJIT_ADD && op != SLJIT_SUB) 530 arg1 = TMP_ZERO; 531 else { 532 FAIL_IF(load_immediate(compiler, TMP_REG1, arg1)); 533 arg1 = TMP_REG1; 534 } 535 } 536 537 if (flags & (ARG1_IMM | ARG2_IMM)) { 538 reg = (flags & ARG2_IMM) ? arg1 : arg2; 539 imm = (flags & ARG2_IMM) ? arg2 : arg1; 540 541 switch (op) { 542 case SLJIT_MUL: 543 case SLJIT_NEG: 544 case SLJIT_CLZ: 545 case SLJIT_ADDC: 546 case SLJIT_SUBC: 547 /* No form with immediate operand (except imm 0, which 548 is represented by a ZERO register). */ 549 break; 550 case SLJIT_MOV: 551 SLJIT_ASSERT(!(flags & SET_FLAGS) && (flags & ARG2_IMM) && arg1 == TMP_REG1); 552 return load_immediate(compiler, dst, imm); 553 case SLJIT_NOT: 554 SLJIT_ASSERT(flags & ARG2_IMM); 555 FAIL_IF(load_immediate(compiler, dst, (flags & INT_OP) ? (~imm & 0xffffffff) : ~imm)); 556 goto set_flags; 557 case SLJIT_SUB: 558 if (flags & ARG1_IMM) 559 break; 560 imm = -imm; 561 /* Fall through. */ 562 case SLJIT_ADD: 563 if (imm == 0) { 564 CHECK_FLAGS(1 << 29); 565 return push_inst(compiler, ((op == SLJIT_ADD ? ADDI : SUBI) ^ inv_bits) | RD(dst) | RN(reg)); 566 } 567 if (imm > 0 && imm <= 0xfff) { 568 CHECK_FLAGS(1 << 29); 569 return push_inst(compiler, (ADDI ^ inv_bits) | RD(dst) | RN(reg) | (imm << 10)); 570 } 571 nimm = -imm; 572 if (nimm > 0 && nimm <= 0xfff) { 573 CHECK_FLAGS(1 << 29); 574 return push_inst(compiler, (SUBI ^ inv_bits) | RD(dst) | RN(reg) | (nimm << 10)); 575 } 576 if (imm > 0 && imm <= 0xffffff && !(imm & 0xfff)) { 577 CHECK_FLAGS(1 << 29); 578 return push_inst(compiler, (ADDI ^ inv_bits) | RD(dst) | RN(reg) | ((imm >> 12) << 10) | (1 << 22)); 579 } 580 if (nimm > 0 && nimm <= 0xffffff && !(nimm & 0xfff)) { 581 CHECK_FLAGS(1 << 29); 582 return push_inst(compiler, (SUBI ^ inv_bits) | RD(dst) | RN(reg) | ((nimm >> 12) << 10) | (1 << 22)); 583 } 584 if (imm > 0 && imm <= 0xffffff && !(flags & SET_FLAGS)) { 585 FAIL_IF(push_inst(compiler, (ADDI ^ inv_bits) | RD(dst) | RN(reg) | ((imm >> 12) << 10) | (1 << 22))); 586 return push_inst(compiler, (ADDI ^ inv_bits) | RD(dst) | RN(dst) | ((imm & 0xfff) << 10)); 587 } 588 if (nimm > 0 && nimm <= 0xffffff && !(flags & SET_FLAGS)) { 589 FAIL_IF(push_inst(compiler, (SUBI ^ inv_bits) | RD(dst) | RN(reg) | ((nimm >> 12) << 10) | (1 << 22))); 590 return push_inst(compiler, (SUBI ^ inv_bits) | RD(dst) | RN(dst) | ((nimm & 0xfff) << 10)); 591 } 592 break; 593 case SLJIT_AND: 594 inst_bits = logical_imm(imm, LOGICAL_IMM_CHECK | ((flags & INT_OP) ? 16 : 32)); 595 if (!inst_bits) 596 break; 597 CHECK_FLAGS(3 << 29); 598 return push_inst(compiler, (ANDI ^ inv_bits) | RD(dst) | RN(reg) | inst_bits); 599 case SLJIT_OR: 600 case SLJIT_XOR: 601 inst_bits = logical_imm(imm, LOGICAL_IMM_CHECK | ((flags & INT_OP) ? 16 : 32)); 602 if (!inst_bits) 603 break; 604 if (op == SLJIT_OR) 605 inst_bits |= ORRI; 606 else 607 inst_bits |= EORI; 608 FAIL_IF(push_inst(compiler, (inst_bits ^ inv_bits) | RD(dst) | RN(reg))); 609 goto set_flags; 610 case SLJIT_SHL: 611 if (flags & ARG1_IMM) 612 break; 613 if (flags & INT_OP) { 614 imm &= 0x1f; 615 FAIL_IF(push_inst(compiler, (UBFM ^ inv_bits) | RD(dst) | RN(arg1) | ((-imm & 0x1f) << 16) | ((31 - imm) << 10))); 616 } 617 else { 618 imm &= 0x3f; 619 FAIL_IF(push_inst(compiler, (UBFM ^ inv_bits) | RD(dst) | RN(arg1) | (1 << 22) | ((-imm & 0x3f) << 16) | ((63 - imm) << 10))); 620 } 621 goto set_flags; 622 case SLJIT_LSHR: 623 case SLJIT_ASHR: 624 if (flags & ARG1_IMM) 625 break; 626 if (op == SLJIT_ASHR) 627 inv_bits |= 1 << 30; 628 if (flags & INT_OP) { 629 imm &= 0x1f; 630 FAIL_IF(push_inst(compiler, (UBFM ^ inv_bits) | RD(dst) | RN(arg1) | (imm << 16) | (31 << 10))); 631 } 632 else { 633 imm &= 0x3f; 634 FAIL_IF(push_inst(compiler, (UBFM ^ inv_bits) | RD(dst) | RN(arg1) | (1 << 22) | (imm << 16) | (63 << 10))); 635 } 636 goto set_flags; 637 default: 638 SLJIT_ASSERT_STOP(); 639 break; 640 } 641 642 if (flags & ARG2_IMM) { 643 if (arg2 == 0) 644 arg2 = TMP_ZERO; 645 else { 646 FAIL_IF(load_immediate(compiler, TMP_REG2, arg2)); 647 arg2 = TMP_REG2; 648 } 649 } 650 else { 651 if (arg1 == 0) 652 arg1 = TMP_ZERO; 653 else { 654 FAIL_IF(load_immediate(compiler, TMP_REG1, arg1)); 655 arg1 = TMP_REG1; 656 } 657 } 658 } 659 660 /* Both arguments are registers. */ 661 switch (op) { 662 case SLJIT_MOV: 663 case SLJIT_MOV_P: 664 case SLJIT_MOVU: 665 case SLJIT_MOVU_P: 666 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1); 667 if (dst == arg2) 668 return SLJIT_SUCCESS; 669 return push_inst(compiler, ORR | RD(dst) | RN(TMP_ZERO) | RM(arg2)); 670 case SLJIT_MOV_UB: 671 case SLJIT_MOVU_UB: 672 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1); 673 return push_inst(compiler, (UBFM ^ (1 << 31)) | RD(dst) | RN(arg2) | (7 << 10)); 674 case SLJIT_MOV_SB: 675 case SLJIT_MOVU_SB: 676 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1); 677 if (!(flags & INT_OP)) 678 inv_bits |= 1 << 22; 679 return push_inst(compiler, (SBFM ^ inv_bits) | RD(dst) | RN(arg2) | (7 << 10)); 680 case SLJIT_MOV_UH: 681 case SLJIT_MOVU_UH: 682 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1); 683 return push_inst(compiler, (UBFM ^ (1 << 31)) | RD(dst) | RN(arg2) | (15 << 10)); 684 case SLJIT_MOV_SH: 685 case SLJIT_MOVU_SH: 686 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1); 687 if (!(flags & INT_OP)) 688 inv_bits |= 1 << 22; 689 return push_inst(compiler, (SBFM ^ inv_bits) | RD(dst) | RN(arg2) | (15 << 10)); 690 case SLJIT_MOV_UI: 691 case SLJIT_MOVU_UI: 692 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1); 693 if ((flags & INT_OP) && dst == arg2) 694 return SLJIT_SUCCESS; 695 return push_inst(compiler, (ORR ^ (1 << 31)) | RD(dst) | RN(TMP_ZERO) | RM(arg2)); 696 case SLJIT_MOV_SI: 697 case SLJIT_MOVU_SI: 698 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1); 699 if ((flags & INT_OP) && dst == arg2) 700 return SLJIT_SUCCESS; 701 return push_inst(compiler, SBFM | (1 << 22) | RD(dst) | RN(arg2) | (31 << 10)); 702 case SLJIT_NOT: 703 SLJIT_ASSERT(arg1 == TMP_REG1); 704 FAIL_IF(push_inst(compiler, (ORN ^ inv_bits) | RD(dst) | RN(TMP_ZERO) | RM(arg2))); 705 goto set_flags; 706 case SLJIT_NEG: 707 SLJIT_ASSERT(arg1 == TMP_REG1); 708 if (flags & SET_FLAGS) 709 inv_bits |= 1 << 29; 710 return push_inst(compiler, (SUB ^ inv_bits) | RD(dst) | RN(TMP_ZERO) | RM(arg2)); 711 case SLJIT_CLZ: 712 SLJIT_ASSERT(arg1 == TMP_REG1); 713 FAIL_IF(push_inst(compiler, (CLZ ^ inv_bits) | RD(dst) | RN(arg2))); 714 goto set_flags; 715 case SLJIT_ADD: 716 CHECK_FLAGS(1 << 29); 717 return push_inst(compiler, (ADD ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2)); 718 case SLJIT_ADDC: 719 CHECK_FLAGS(1 << 29); 720 return push_inst(compiler, (ADC ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2)); 721 case SLJIT_SUB: 722 CHECK_FLAGS(1 << 29); 723 return push_inst(compiler, (SUB ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2)); 724 case SLJIT_SUBC: 725 CHECK_FLAGS(1 << 29); 726 return push_inst(compiler, (SBC ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2)); 727 case SLJIT_MUL: 728 if (!(flags & SET_FLAGS)) 729 return push_inst(compiler, (MADD ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2) | RT2(TMP_ZERO)); 730 if (flags & INT_OP) { 731 FAIL_IF(push_inst(compiler, SMADDL | RD(dst) | RN(arg1) | RM(arg2) | (31 << 10))); 732 FAIL_IF(push_inst(compiler, ADD | RD(TMP_REG4) | RN(TMP_ZERO) | RM(dst) | (2 << 22) | (31 << 10))); 733 return push_inst(compiler, SUBS | RD(TMP_ZERO) | RN(TMP_REG4) | RM(dst) | (2 << 22) | (63 << 10)); 734 } 735 FAIL_IF(push_inst(compiler, SMULH | RD(TMP_REG4) | RN(arg1) | RM(arg2))); 736 FAIL_IF(push_inst(compiler, MADD | RD(dst) | RN(arg1) | RM(arg2) | RT2(TMP_ZERO))); 737 return push_inst(compiler, SUBS | RD(TMP_ZERO) | RN(TMP_REG4) | RM(dst) | (2 << 22) | (63 << 10)); 738 case SLJIT_AND: 739 CHECK_FLAGS(3 << 29); 740 return push_inst(compiler, (AND ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2)); 741 case SLJIT_OR: 742 FAIL_IF(push_inst(compiler, (ORR ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2))); 743 goto set_flags; 744 case SLJIT_XOR: 745 FAIL_IF(push_inst(compiler, (EOR ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2))); 746 goto set_flags; 747 case SLJIT_SHL: 748 FAIL_IF(push_inst(compiler, (LSLV ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2))); 749 goto set_flags; 750 case SLJIT_LSHR: 751 FAIL_IF(push_inst(compiler, (LSRV ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2))); 752 goto set_flags; 753 case SLJIT_ASHR: 754 FAIL_IF(push_inst(compiler, (ASRV ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2))); 755 goto set_flags; 756 } 757 758 SLJIT_ASSERT_STOP(); 759 return SLJIT_SUCCESS; 760 761 set_flags: 762 if (flags & SET_FLAGS) 763 return push_inst(compiler, (SUBS ^ inv_bits) | RD(TMP_ZERO) | RN(dst) | RM(TMP_ZERO)); 764 return SLJIT_SUCCESS; 765 } 766 767 #define STORE 0x01 768 #define SIGNED 0x02 769 770 #define UPDATE 0x04 771 #define ARG_TEST 0x08 772 773 #define BYTE_SIZE 0x000 774 #define HALF_SIZE 0x100 775 #define INT_SIZE 0x200 776 #define WORD_SIZE 0x300 777 778 #define MEM_SIZE_SHIFT(flags) ((flags) >> 8) 779 780 static SLJIT_CONST sljit_ins sljit_mem_imm[4] = { 781 /* u l */ 0x39400000 /* ldrb [reg,imm] */, 782 /* u s */ 0x39000000 /* strb [reg,imm] */, 783 /* s l */ 0x39800000 /* ldrsb [reg,imm] */, 784 /* s s */ 0x39000000 /* strb [reg,imm] */, 785 }; 786 787 static SLJIT_CONST sljit_ins sljit_mem_simm[4] = { 788 /* u l */ 0x38400000 /* ldurb [reg,imm] */, 789 /* u s */ 0x38000000 /* sturb [reg,imm] */, 790 /* s l */ 0x38800000 /* ldursb [reg,imm] */, 791 /* s s */ 0x38000000 /* sturb [reg,imm] */, 792 }; 793 794 static SLJIT_CONST sljit_ins sljit_mem_pre_simm[4] = { 795 /* u l */ 0x38400c00 /* ldrb [reg,imm]! */, 796 /* u s */ 0x38000c00 /* strb [reg,imm]! */, 797 /* s l */ 0x38800c00 /* ldrsb [reg,imm]! */, 798 /* s s */ 0x38000c00 /* strb [reg,imm]! */, 799 }; 800 801 static SLJIT_CONST sljit_ins sljit_mem_reg[4] = { 802 /* u l */ 0x38606800 /* ldrb [reg,reg] */, 803 /* u s */ 0x38206800 /* strb [reg,reg] */, 804 /* s l */ 0x38a06800 /* ldrsb [reg,reg] */, 805 /* s s */ 0x38206800 /* strb [reg,reg] */, 806 }; 807 808 /* Helper function. Dst should be reg + value, using at most 1 instruction, flags does not set. */ 809 static sljit_si emit_set_delta(struct sljit_compiler *compiler, sljit_si dst, sljit_si reg, sljit_sw value) 810 { 811 if (value >= 0) { 812 if (value <= 0xfff) 813 return push_inst(compiler, ADDI | RD(dst) | RN(reg) | (value << 10)); 814 if (value <= 0xffffff && !(value & 0xfff)) 815 return push_inst(compiler, ADDI | (1 << 22) | RD(dst) | RN(reg) | (value >> 2)); 816 } 817 else { 818 value = -value; 819 if (value <= 0xfff) 820 return push_inst(compiler, SUBI | RD(dst) | RN(reg) | (value << 10)); 821 if (value <= 0xffffff && !(value & 0xfff)) 822 return push_inst(compiler, SUBI | (1 << 22) | RD(dst) | RN(reg) | (value >> 2)); 823 } 824 return SLJIT_ERR_UNSUPPORTED; 825 } 826 827 /* Can perform an operation using at most 1 instruction. */ 828 static sljit_si getput_arg_fast(struct sljit_compiler *compiler, sljit_si flags, sljit_si reg, sljit_si arg, sljit_sw argw) 829 { 830 sljit_ui shift = MEM_SIZE_SHIFT(flags); 831 832 SLJIT_ASSERT(arg & SLJIT_MEM); 833 834 if (SLJIT_UNLIKELY(flags & UPDATE)) { 835 if ((arg & REG_MASK) && !(arg & OFFS_REG_MASK) && argw <= 255 && argw >= -256) { 836 if (SLJIT_UNLIKELY(flags & ARG_TEST)) 837 return 1; 838 839 arg &= REG_MASK; 840 argw &= 0x1ff; 841 FAIL_IF(push_inst(compiler, sljit_mem_pre_simm[flags & 0x3] 842 | (shift << 30) | RT(reg) | RN(arg) | (argw << 12))); 843 return -1; 844 } 845 return 0; 846 } 847 848 if (SLJIT_UNLIKELY(arg & OFFS_REG_MASK)) { 849 argw &= 0x3; 850 if (argw && argw != shift) 851 return 0; 852 853 if (SLJIT_UNLIKELY(flags & ARG_TEST)) 854 return 1; 855 856 FAIL_IF(push_inst(compiler, sljit_mem_reg[flags & 0x3] | (shift << 30) | RT(reg) 857 | RN(arg & REG_MASK) | RM(OFFS_REG(arg)) | (argw ? (1 << 12) : 0))); 858 return -1; 859 } 860 861 arg &= REG_MASK; 862 if (argw >= 0 && (argw >> shift) <= 0xfff && (argw & ((1 << shift) - 1)) == 0) { 863 if (SLJIT_UNLIKELY(flags & ARG_TEST)) 864 return 1; 865 866 FAIL_IF(push_inst(compiler, sljit_mem_imm[flags & 0x3] | (shift << 30) 867 | RT(reg) | RN(arg) | (argw << (10 - shift)))); 868 return -1; 869 } 870 871 if (argw > 255 || argw < -256) 872 return 0; 873 874 if (SLJIT_UNLIKELY(flags & ARG_TEST)) 875 return 1; 876 877 FAIL_IF(push_inst(compiler, sljit_mem_simm[flags & 0x3] | (shift << 30) 878 | RT(reg) | RN(arg) | ((argw & 0x1ff) << 12))); 879 return -1; 880 } 881 882 /* see getput_arg below. 883 Note: can_cache is called only for binary operators. Those 884 operators always uses word arguments without write back. */ 885 static sljit_si can_cache(sljit_si arg, sljit_sw argw, sljit_si next_arg, sljit_sw next_argw) 886 { 887 sljit_sw diff; 888 if ((arg & OFFS_REG_MASK) || !(next_arg & SLJIT_MEM)) 889 return 0; 890 891 if (!(arg & REG_MASK)) { 892 diff = argw - next_argw; 893 if (diff <= 0xfff && diff >= -0xfff) 894 return 1; 895 return 0; 896 } 897 898 if (argw == next_argw) 899 return 1; 900 901 diff = argw - next_argw; 902 if (arg == next_arg && diff <= 0xfff && diff >= -0xfff) 903 return 1; 904 905 return 0; 906 } 907 908 /* Emit the necessary instructions. See can_cache above. */ 909 static sljit_si getput_arg(struct sljit_compiler *compiler, sljit_si flags, sljit_si reg, 910 sljit_si arg, sljit_sw argw, sljit_si next_arg, sljit_sw next_argw) 911 { 912 sljit_ui shift = MEM_SIZE_SHIFT(flags); 913 sljit_si tmp_r, other_r; 914 sljit_sw diff; 915 916 SLJIT_ASSERT(arg & SLJIT_MEM); 917 if (!(next_arg & SLJIT_MEM)) { 918 next_arg = 0; 919 next_argw = 0; 920 } 921 922 tmp_r = (flags & STORE) ? TMP_REG3 : reg; 923 924 if (SLJIT_UNLIKELY((flags & UPDATE) && (arg & REG_MASK))) { 925 /* Update only applies if a base register exists. */ 926 other_r = OFFS_REG(arg); 927 if (!other_r) { 928 other_r = arg & REG_MASK; 929 if (other_r != reg && argw >= 0 && argw <= 0xffffff) { 930 if ((argw & 0xfff) != 0) 931 FAIL_IF(push_inst(compiler, ADDI | RD(other_r) | RN(other_r) | ((argw & 0xfff) << 10))); 932 if (argw >> 12) 933 FAIL_IF(push_inst(compiler, ADDI | (1 << 22) | RD(other_r) | RN(other_r) | ((argw >> 12) << 10))); 934 return push_inst(compiler, sljit_mem_imm[flags & 0x3] | (shift << 30) | RT(reg) | RN(other_r)); 935 } 936 else if (other_r != reg && argw < 0 && argw >= -0xffffff) { 937 argw = -argw; 938 if ((argw & 0xfff) != 0) 939 FAIL_IF(push_inst(compiler, SUBI | RD(other_r) | RN(other_r) | ((argw & 0xfff) << 10))); 940 if (argw >> 12) 941 FAIL_IF(push_inst(compiler, SUBI | (1 << 22) | RD(other_r) | RN(other_r) | ((argw >> 12) << 10))); 942 return push_inst(compiler, sljit_mem_imm[flags & 0x3] | (shift << 30) | RT(reg) | RN(other_r)); 943 } 944 945 if (compiler->cache_arg == SLJIT_MEM) { 946 if (argw == compiler->cache_argw) { 947 other_r = TMP_REG3; 948 argw = 0; 949 } 950 else if (emit_set_delta(compiler, TMP_REG3, TMP_REG3, argw - compiler->cache_argw) != SLJIT_ERR_UNSUPPORTED) { 951 FAIL_IF(compiler->error); 952 compiler->cache_argw = argw; 953 other_r = TMP_REG3; 954 argw = 0; 955 } 956 } 957 958 if (argw) { 959 FAIL_IF(load_immediate(compiler, TMP_REG3, argw)); 960 compiler->cache_arg = SLJIT_MEM; 961 compiler->cache_argw = argw; 962 other_r = TMP_REG3; 963 argw = 0; 964 } 965 } 966 967 /* No caching here. */ 968 arg &= REG_MASK; 969 argw &= 0x3; 970 if (!argw || argw == shift) { 971 FAIL_IF(push_inst(compiler, sljit_mem_reg[flags & 0x3] | (shift << 30) | RT(reg) | RN(arg) | RM(other_r) | (argw ? (1 << 12) : 0))); 972 return push_inst(compiler, ADD | RD(arg) | RN(arg) | RM(other_r) | (argw << 10)); 973 } 974 if (arg != reg) { 975 FAIL_IF(push_inst(compiler, ADD | RD(arg) | RN(arg) | RM(other_r) | (argw << 10))); 976 return push_inst(compiler, sljit_mem_imm[flags & 0x3] | (shift << 30) | RT(reg) | RN(arg)); 977 } 978 FAIL_IF(push_inst(compiler, ADD | RD(TMP_REG4) | RN(arg) | RM(other_r) | (argw << 10))); 979 FAIL_IF(push_inst(compiler, sljit_mem_imm[flags & 0x3] | (shift << 30) | RT(reg) | RN(TMP_REG4))); 980 return push_inst(compiler, ORR | RD(arg) | RN(TMP_ZERO) | RM(TMP_REG4)); 981 } 982 983 if (arg & OFFS_REG_MASK) { 984 other_r = OFFS_REG(arg); 985 arg &= REG_MASK; 986 FAIL_IF(push_inst(compiler, ADD | RD(tmp_r) | RN(arg) | RM(other_r) | ((argw & 0x3) << 10))); 987 return push_inst(compiler, sljit_mem_imm[flags & 0x3] | (shift << 30) | RT(reg) | RN(tmp_r)); 988 } 989 990 if (compiler->cache_arg == arg) { 991 diff = argw - compiler->cache_argw; 992 if (diff <= 255 && diff >= -256) 993 return push_inst(compiler, sljit_mem_simm[flags & 0x3] | (shift << 30) 994 | RT(reg) | RN(TMP_REG3) | ((diff & 0x1ff) << 12)); 995 if (emit_set_delta(compiler, TMP_REG3, TMP_REG3, diff) != SLJIT_ERR_UNSUPPORTED) { 996 FAIL_IF(compiler->error); 997 return push_inst(compiler, sljit_mem_imm[flags & 0x3] | (shift << 30) | RT(reg) | RN(arg)); 998 } 999 } 1000 1001 if (argw >= 0 && argw <= 0xffffff && (argw & ((1 << shift) - 1)) == 0) { 1002 FAIL_IF(push_inst(compiler, ADDI | (1 << 22) | RD(tmp_r) | RN(arg & REG_MASK) | ((argw >> 12) << 10))); 1003 return push_inst(compiler, sljit_mem_imm[flags & 0x3] | (shift << 30) 1004 | RT(reg) | RN(tmp_r) | ((argw & 0xfff) << (10 - shift))); 1005 } 1006 1007 diff = argw - next_argw; 1008 next_arg = (arg & REG_MASK) && (arg == next_arg) && diff <= 0xfff && diff >= -0xfff && diff != 0; 1009 arg &= REG_MASK; 1010 1011 if (arg && compiler->cache_arg == SLJIT_MEM) { 1012 if (compiler->cache_argw == argw) 1013 return push_inst(compiler, sljit_mem_reg[flags & 0x3] | (shift << 30) | RT(reg) | RN(arg) | RM(TMP_REG3)); 1014 if (emit_set_delta(compiler, TMP_REG3, TMP_REG3, argw - compiler->cache_argw) != SLJIT_ERR_UNSUPPORTED) { 1015 FAIL_IF(compiler->error); 1016 compiler->cache_argw = argw; 1017 return push_inst(compiler, sljit_mem_reg[flags & 0x3] | (shift << 30) | RT(reg) | RN(arg) | RM(TMP_REG3)); 1018 } 1019 } 1020 1021 compiler->cache_argw = argw; 1022 if (next_arg && emit_set_delta(compiler, TMP_REG3, arg, argw) != SLJIT_ERR_UNSUPPORTED) { 1023 FAIL_IF(compiler->error); 1024 compiler->cache_arg = SLJIT_MEM | arg; 1025 arg = 0; 1026 } 1027 else { 1028 FAIL_IF(load_immediate(compiler, TMP_REG3, argw)); 1029 compiler->cache_arg = SLJIT_MEM; 1030 1031 if (next_arg) { 1032 FAIL_IF(push_inst(compiler, ADD | RD(TMP_REG3) | RN(TMP_REG3) | RM(arg))); 1033 compiler->cache_arg = SLJIT_MEM | arg; 1034 arg = 0; 1035 } 1036 } 1037 1038 if (arg) 1039 return push_inst(compiler, sljit_mem_reg[flags & 0x3] | (shift << 30) | RT(reg) | RN(arg) | RM(TMP_REG3)); 1040 return push_inst(compiler, sljit_mem_imm[flags & 0x3] | (shift << 30) | RT(reg) | RN(TMP_REG3)); 1041 } 1042 1043 static SLJIT_INLINE sljit_si emit_op_mem(struct sljit_compiler *compiler, sljit_si flags, sljit_si reg, sljit_si arg, sljit_sw argw) 1044 { 1045 if (getput_arg_fast(compiler, flags, reg, arg, argw)) 1046 return compiler->error; 1047 compiler->cache_arg = 0; 1048 compiler->cache_argw = 0; 1049 return getput_arg(compiler, flags, reg, arg, argw, 0, 0); 1050 } 1051 1052 static SLJIT_INLINE sljit_si emit_op_mem2(struct sljit_compiler *compiler, sljit_si flags, sljit_si reg, sljit_si arg1, sljit_sw arg1w, sljit_si arg2, sljit_sw arg2w) 1053 { 1054 if (getput_arg_fast(compiler, flags, reg, arg1, arg1w)) 1055 return compiler->error; 1056 return getput_arg(compiler, flags, reg, arg1, arg1w, arg2, arg2w); 1057 } 1058 1059 /* --------------------------------------------------------------------- */ 1060 /* Entry, exit */ 1061 /* --------------------------------------------------------------------- */ 1062 1063 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_enter(struct sljit_compiler *compiler, sljit_si args, sljit_si scratches, sljit_si saveds, sljit_si local_size) 1064 { 1065 CHECK_ERROR(); 1066 check_sljit_emit_enter(compiler, args, scratches, saveds, local_size); 1067 1068 compiler->scratches = scratches; 1069 compiler->saveds = saveds; 1070 #if (defined SLJIT_DEBUG && SLJIT_DEBUG) 1071 compiler->logical_local_size = local_size; 1072 #endif 1073 compiler->locals_offset = (2 + saveds) * sizeof(sljit_sw); 1074 local_size = (compiler->locals_offset + local_size + 15) & ~15; 1075 compiler->local_size = local_size; 1076 1077 if (local_size <= (64 << 3)) 1078 FAIL_IF(push_inst(compiler, STP_PRE | 29 | RT2(TMP_LR) 1079 | RN(TMP_SP) | ((-(local_size >> 3) & 0x7f) << 15))); 1080 else { 1081 local_size -= (64 << 3); 1082 if (local_size > 0xfff) { 1083 FAIL_IF(push_inst(compiler, SUBI | RD(TMP_SP) | RN(TMP_SP) | ((local_size >> 12) << 10) | (1 << 22))); 1084 local_size &= 0xfff; 1085 } 1086 if (local_size) 1087 FAIL_IF(push_inst(compiler, SUBI | RD(TMP_SP) | RN(TMP_SP) | (local_size << 10))); 1088 FAIL_IF(push_inst(compiler, STP_PRE | 29 | RT2(TMP_LR) | RN(TMP_SP) | (0x40 << 15))); 1089 } 1090 1091 FAIL_IF(push_inst(compiler, ADDI | RD(SLJIT_LOCALS_REG) | RN(TMP_SP))); 1092 1093 if (saveds >= 2) 1094 FAIL_IF(push_inst(compiler, STP | RT(SLJIT_SAVED_REG1) | RT2(SLJIT_SAVED_REG2) | RN(TMP_SP) | (2 << 15))); 1095 if (saveds >= 4) 1096 FAIL_IF(push_inst(compiler, STP | RT(SLJIT_SAVED_REG3) | RT2(SLJIT_SAVED_EREG1) | RN(TMP_SP) | (4 << 15))); 1097 if (saveds == 1) 1098 FAIL_IF(push_inst(compiler, STRI | RT(SLJIT_SAVED_REG1) | RN(TMP_SP) | (2 << 10))); 1099 if (saveds == 3) 1100 FAIL_IF(push_inst(compiler, STRI | RT(SLJIT_SAVED_REG3) | RN(TMP_SP) | (4 << 10))); 1101 if (saveds == 5) 1102 FAIL_IF(push_inst(compiler, STRI | RT(SLJIT_SAVED_EREG2) | RN(TMP_SP) | (6 << 10))); 1103 1104 if (args >= 1) 1105 FAIL_IF(push_inst(compiler, ORR | RD(SLJIT_SAVED_REG1) | RN(TMP_ZERO) | RM(SLJIT_SCRATCH_REG1))); 1106 if (args >= 2) 1107 FAIL_IF(push_inst(compiler, ORR | RD(SLJIT_SAVED_REG2) | RN(TMP_ZERO) | RM(SLJIT_SCRATCH_REG2))); 1108 if (args >= 3) 1109 FAIL_IF(push_inst(compiler, ORR | RD(SLJIT_SAVED_REG3) | RN(TMP_ZERO) | RM(SLJIT_SCRATCH_REG3))); 1110 1111 return SLJIT_SUCCESS; 1112 } 1113 1114 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_context(struct sljit_compiler *compiler, sljit_si args, sljit_si scratches, sljit_si saveds, sljit_si local_size) 1115 { 1116 CHECK_ERROR_VOID(); 1117 check_sljit_set_context(compiler, args, scratches, saveds, local_size); 1118 1119 compiler->scratches = scratches; 1120 compiler->saveds = saveds; 1121 #if (defined SLJIT_DEBUG && SLJIT_DEBUG) 1122 compiler->logical_local_size = local_size; 1123 #endif 1124 compiler->locals_offset = (2 + saveds) * sizeof(sljit_sw); 1125 compiler->local_size = (compiler->locals_offset + local_size + 15) & ~15; 1126 } 1127 1128 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_return(struct sljit_compiler *compiler, sljit_si op, sljit_si src, sljit_sw srcw) 1129 { 1130 sljit_si saveds, local_size; 1131 1132 CHECK_ERROR(); 1133 check_sljit_emit_return(compiler, op, src, srcw); 1134 1135 FAIL_IF(emit_mov_before_return(compiler, op, src, srcw)); 1136 1137 saveds = compiler->saveds; 1138 1139 if (saveds >= 2) 1140 FAIL_IF(push_inst(compiler, LDP | RT(SLJIT_SAVED_REG1) | RT2(SLJIT_SAVED_REG2) | RN(TMP_SP) | (2 << 15))); 1141 if (saveds >= 4) 1142 FAIL_IF(push_inst(compiler, LDP | RT(SLJIT_SAVED_REG3) | RT2(SLJIT_SAVED_EREG1) | RN(TMP_SP) | (4 << 15))); 1143 if (saveds == 1) 1144 FAIL_IF(push_inst(compiler, LDRI | RT(SLJIT_SAVED_REG1) | RN(TMP_SP) | (2 << 10))); 1145 if (saveds == 3) 1146 FAIL_IF(push_inst(compiler, LDRI | RT(SLJIT_SAVED_REG3) | RN(TMP_SP) | (4 << 10))); 1147 if (saveds == 5) 1148 FAIL_IF(push_inst(compiler, LDRI | RT(SLJIT_SAVED_EREG2) | RN(TMP_SP) | (6 << 10))); 1149 1150 local_size = compiler->local_size; 1151 1152 if (local_size <= (62 << 3)) 1153 FAIL_IF(push_inst(compiler, LDP_PST | 29 | RT2(TMP_LR) 1154 | RN(TMP_SP) | (((local_size >> 3) & 0x7f) << 15))); 1155 else { 1156 FAIL_IF(push_inst(compiler, LDP_PST | 29 | RT2(TMP_LR) | RN(TMP_SP) | (0x3e << 15))); 1157 local_size -= (62 << 3); 1158 if (local_size > 0xfff) { 1159 FAIL_IF(push_inst(compiler, ADDI | RD(TMP_SP) | RN(TMP_SP) | ((local_size >> 12) << 10) | (1 << 22))); 1160 local_size &= 0xfff; 1161 } 1162 if (local_size) 1163 FAIL_IF(push_inst(compiler, ADDI | RD(TMP_SP) | RN(TMP_SP) | (local_size << 10))); 1164 } 1165 1166 FAIL_IF(push_inst(compiler, RET | RN(TMP_LR))); 1167 return SLJIT_SUCCESS; 1168 } 1169 1170 /* --------------------------------------------------------------------- */ 1171 /* Operators */ 1172 /* --------------------------------------------------------------------- */ 1173 1174 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op0(struct sljit_compiler *compiler, sljit_si op) 1175 { 1176 sljit_ins inv_bits = (op & SLJIT_INT_OP) ? (1 << 31) : 0; 1177 1178 CHECK_ERROR(); 1179 check_sljit_emit_op0(compiler, op); 1180 1181 op = GET_OPCODE(op); 1182 switch (op) { 1183 case SLJIT_BREAKPOINT: 1184 return push_inst(compiler, BRK); 1185 case SLJIT_NOP: 1186 return push_inst(compiler, NOP); 1187 case SLJIT_UMUL: 1188 case SLJIT_SMUL: 1189 FAIL_IF(push_inst(compiler, ORR | RD(TMP_REG1) | RN(TMP_ZERO) | RM(SLJIT_SCRATCH_REG1))); 1190 FAIL_IF(push_inst(compiler, MADD | RD(SLJIT_SCRATCH_REG1) | RN(SLJIT_SCRATCH_REG1) | RM(SLJIT_SCRATCH_REG2) | RT2(TMP_ZERO))); 1191 return push_inst(compiler, (op == SLJIT_SMUL ? SMULH : UMULH) | RD(SLJIT_SCRATCH_REG2) | RN(TMP_REG1) | RM(SLJIT_SCRATCH_REG2)); 1192 case SLJIT_UDIV: 1193 case SLJIT_SDIV: 1194 FAIL_IF(push_inst(compiler, (ORR ^ inv_bits) | RD(TMP_REG1) | RN(TMP_ZERO) | RM(SLJIT_SCRATCH_REG1))); 1195 FAIL_IF(push_inst(compiler, ((op == SLJIT_SDIV ? SDIV : UDIV) ^ inv_bits) | RD(SLJIT_SCRATCH_REG1) | RN(SLJIT_SCRATCH_REG1) | RM(SLJIT_SCRATCH_REG2))); 1196 FAIL_IF(push_inst(compiler, (MADD ^ inv_bits) | RD(SLJIT_SCRATCH_REG2) | RN(SLJIT_SCRATCH_REG1) | RM(SLJIT_SCRATCH_REG2) | RT2(TMP_ZERO))); 1197 return push_inst(compiler, (SUB ^ inv_bits) | RD(SLJIT_SCRATCH_REG2) | RN(TMP_REG1) | RM(SLJIT_SCRATCH_REG2)); 1198 } 1199 1200 return SLJIT_SUCCESS; 1201 } 1202 1203 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op1(struct sljit_compiler *compiler, sljit_si op, 1204 sljit_si dst, sljit_sw dstw, 1205 sljit_si src, sljit_sw srcw) 1206 { 1207 sljit_si dst_r, flags, mem_flags; 1208 sljit_si op_flags = GET_ALL_FLAGS(op); 1209 1210 CHECK_ERROR(); 1211 check_sljit_emit_op1(compiler, op, dst, dstw, src, srcw); 1212 ADJUST_LOCAL_OFFSET(dst, dstw); 1213 ADJUST_LOCAL_OFFSET(src, srcw); 1214 1215 compiler->cache_arg = 0; 1216 compiler->cache_argw = 0; 1217 1218 dst_r = SLOW_IS_REG(dst) ? dst : TMP_REG1; 1219 1220 op = GET_OPCODE(op); 1221 if (op >= SLJIT_MOV && op <= SLJIT_MOVU_P) { 1222 switch (op) { 1223 case SLJIT_MOV: 1224 case SLJIT_MOV_P: 1225 flags = WORD_SIZE; 1226 break; 1227 case SLJIT_MOV_UB: 1228 flags = BYTE_SIZE; 1229 if (src & SLJIT_IMM) 1230 srcw = (sljit_ub)srcw; 1231 break; 1232 case SLJIT_MOV_SB: 1233 flags = BYTE_SIZE | SIGNED; 1234 if (src & SLJIT_IMM) 1235 srcw = (sljit_sb)srcw; 1236 break; 1237 case SLJIT_MOV_UH: 1238 flags = HALF_SIZE; 1239 if (src & SLJIT_IMM) 1240 srcw = (sljit_uh)srcw; 1241 break; 1242 case SLJIT_MOV_SH: 1243 flags = HALF_SIZE | SIGNED; 1244 if (src & SLJIT_IMM) 1245 srcw = (sljit_sh)srcw; 1246 break; 1247 case SLJIT_MOV_UI: 1248 flags = INT_SIZE; 1249 if (src & SLJIT_IMM) 1250 srcw = (sljit_ui)srcw; 1251 break; 1252 case SLJIT_MOV_SI: 1253 flags = INT_SIZE | SIGNED; 1254 if (src & SLJIT_IMM) 1255 srcw = (sljit_si)srcw; 1256 break; 1257 case SLJIT_MOVU: 1258 case SLJIT_MOVU_P: 1259 flags = WORD_SIZE | UPDATE; 1260 break; 1261 case SLJIT_MOVU_UB: 1262 flags = BYTE_SIZE | UPDATE; 1263 if (src & SLJIT_IMM) 1264 srcw = (sljit_ub)srcw; 1265 break; 1266 case SLJIT_MOVU_SB: 1267 flags = BYTE_SIZE | SIGNED | UPDATE; 1268 if (src & SLJIT_IMM) 1269 srcw = (sljit_sb)srcw; 1270 break; 1271 case SLJIT_MOVU_UH: 1272 flags = HALF_SIZE | UPDATE; 1273 if (src & SLJIT_IMM) 1274 srcw = (sljit_uh)srcw; 1275 break; 1276 case SLJIT_MOVU_SH: 1277 flags = HALF_SIZE | SIGNED | UPDATE; 1278 if (src & SLJIT_IMM) 1279 srcw = (sljit_sh)srcw; 1280 break; 1281 case SLJIT_MOVU_UI: 1282 flags = INT_SIZE | UPDATE; 1283 if (src & SLJIT_IMM) 1284 srcw = (sljit_ui)srcw; 1285 break; 1286 case SLJIT_MOVU_SI: 1287 flags = INT_SIZE | SIGNED | UPDATE; 1288 if (src & SLJIT_IMM) 1289 srcw = (sljit_si)srcw; 1290 break; 1291 default: 1292 SLJIT_ASSERT_STOP(); 1293 flags = 0; 1294 break; 1295 } 1296 1297 if (src & SLJIT_IMM) 1298 FAIL_IF(emit_op_imm(compiler, SLJIT_MOV | ARG2_IMM, dst_r, TMP_REG1, srcw)); 1299 else if (src & SLJIT_MEM) { 1300 if (getput_arg_fast(compiler, flags, dst_r, src, srcw)) 1301 FAIL_IF(compiler->error); 1302 else 1303 FAIL_IF(getput_arg(compiler, flags, dst_r, src, srcw, dst, dstw)); 1304 } else { 1305 if (dst_r != TMP_REG1) 1306 return emit_op_imm(compiler, op | ((op_flags & SLJIT_INT_OP) ? INT_OP : 0), dst_r, TMP_REG1, src); 1307 dst_r = src; 1308 } 1309 1310 if (dst & SLJIT_MEM) { 1311 if (getput_arg_fast(compiler, flags | STORE, dst_r, dst, dstw)) 1312 return compiler->error; 1313 else 1314 return getput_arg(compiler, flags | STORE, dst_r, dst, dstw, 0, 0); 1315 } 1316 return SLJIT_SUCCESS; 1317 } 1318 1319 flags = GET_FLAGS(op_flags) ? SET_FLAGS : 0; 1320 mem_flags = WORD_SIZE; 1321 if (op_flags & SLJIT_INT_OP) { 1322 flags |= INT_OP; 1323 mem_flags = INT_SIZE; 1324 } 1325 1326 if (dst == SLJIT_UNUSED) 1327 flags |= UNUSED_RETURN; 1328 1329 if (src & SLJIT_MEM) { 1330 if (getput_arg_fast(compiler, mem_flags, TMP_REG2, src, srcw)) 1331 FAIL_IF(compiler->error); 1332 else 1333 FAIL_IF(getput_arg(compiler, mem_flags, TMP_REG2, src, srcw, dst, dstw)); 1334 src = TMP_REG2; 1335 } 1336 1337 if (src & SLJIT_IMM) { 1338 flags |= ARG2_IMM; 1339 if (op_flags & SLJIT_INT_OP) 1340 srcw = (sljit_si)srcw; 1341 } else 1342 srcw = src; 1343 1344 emit_op_imm(compiler, flags | op, dst_r, TMP_REG1, srcw); 1345 1346 if (dst & SLJIT_MEM) { 1347 if (getput_arg_fast(compiler, mem_flags | STORE, dst_r, dst, dstw)) 1348 return compiler->error; 1349 else 1350 return getput_arg(compiler, mem_flags | STORE, dst_r, dst, dstw, 0, 0); 1351 } 1352 return SLJIT_SUCCESS; 1353 } 1354 1355 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op2(struct sljit_compiler *compiler, sljit_si op, 1356 sljit_si dst, sljit_sw dstw, 1357 sljit_si src1, sljit_sw src1w, 1358 sljit_si src2, sljit_sw src2w) 1359 { 1360 sljit_si dst_r, flags, mem_flags; 1361 1362 CHECK_ERROR(); 1363 check_sljit_emit_op2(compiler, op, dst, dstw, src1, src1w, src2, src2w); 1364 ADJUST_LOCAL_OFFSET(dst, dstw); 1365 ADJUST_LOCAL_OFFSET(src1, src1w); 1366 ADJUST_LOCAL_OFFSET(src2, src2w); 1367 1368 compiler->cache_arg = 0; 1369 compiler->cache_argw = 0; 1370 1371 dst_r = SLOW_IS_REG(dst) ? dst : TMP_REG1; 1372 flags = GET_FLAGS(op) ? SET_FLAGS : 0; 1373 mem_flags = WORD_SIZE; 1374 if (op & SLJIT_INT_OP) { 1375 flags |= INT_OP; 1376 mem_flags = INT_SIZE; 1377 } 1378 1379 if (dst == SLJIT_UNUSED) 1380 flags |= UNUSED_RETURN; 1381 1382 if ((dst & SLJIT_MEM) && !getput_arg_fast(compiler, mem_flags | STORE | ARG_TEST, TMP_REG1, dst, dstw)) 1383 flags |= SLOW_DEST; 1384 1385 if (src1 & SLJIT_MEM) { 1386 if (getput_arg_fast(compiler, mem_flags, TMP_REG1, src1, src1w)) 1387 FAIL_IF(compiler->error); 1388 else 1389 flags |= SLOW_SRC1; 1390 } 1391 if (src2 & SLJIT_MEM) { 1392 if (getput_arg_fast(compiler, mem_flags, TMP_REG2, src2, src2w)) 1393 FAIL_IF(compiler->error); 1394 else 1395 flags |= SLOW_SRC2; 1396 } 1397 1398 if ((flags & (SLOW_SRC1 | SLOW_SRC2)) == (SLOW_SRC1 | SLOW_SRC2)) { 1399 if (!can_cache(src1, src1w, src2, src2w) && can_cache(src1, src1w, dst, dstw)) { 1400 FAIL_IF(getput_arg(compiler, mem_flags, TMP_REG2, src2, src2w, src1, src1w)); 1401 FAIL_IF(getput_arg(compiler, mem_flags, TMP_REG1, src1, src1w, dst, dstw)); 1402 } 1403 else { 1404 FAIL_IF(getput_arg(compiler, mem_flags, TMP_REG1, src1, src1w, src2, src2w)); 1405 FAIL_IF(getput_arg(compiler, mem_flags, TMP_REG2, src2, src2w, dst, dstw)); 1406 } 1407 } 1408 else if (flags & SLOW_SRC1) 1409 FAIL_IF(getput_arg(compiler, mem_flags, TMP_REG1, src1, src1w, dst, dstw)); 1410 else if (flags & SLOW_SRC2) 1411 FAIL_IF(getput_arg(compiler, mem_flags, TMP_REG2, src2, src2w, dst, dstw)); 1412 1413 if (src1 & SLJIT_MEM) 1414 src1 = TMP_REG1; 1415 if (src2 & SLJIT_MEM) 1416 src2 = TMP_REG2; 1417 1418 if (src1 & SLJIT_IMM) 1419 flags |= ARG1_IMM; 1420 else 1421 src1w = src1; 1422 if (src2 & SLJIT_IMM) 1423 flags |= ARG2_IMM; 1424 else 1425 src2w = src2; 1426 1427 emit_op_imm(compiler, flags | GET_OPCODE(op), dst_r, src1w, src2w); 1428 1429 if (dst & SLJIT_MEM) { 1430 if (!(flags & SLOW_DEST)) { 1431 getput_arg_fast(compiler, mem_flags | STORE, dst_r, dst, dstw); 1432 return compiler->error; 1433 } 1434 return getput_arg(compiler, mem_flags | STORE, TMP_REG1, dst, dstw, 0, 0); 1435 } 1436 1437 return SLJIT_SUCCESS; 1438 } 1439 1440 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_get_register_index(sljit_si reg) 1441 { 1442 check_sljit_get_register_index(reg); 1443 return reg_map[reg]; 1444 } 1445 1446 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_get_float_register_index(sljit_si reg) 1447 { 1448 check_sljit_get_float_register_index(reg); 1449 return reg; 1450 } 1451 1452 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op_custom(struct sljit_compiler *compiler, 1453 void *instruction, sljit_si size) 1454 { 1455 CHECK_ERROR(); 1456 check_sljit_emit_op_custom(compiler, instruction, size); 1457 SLJIT_ASSERT(size == 4); 1458 1459 return push_inst(compiler, *(sljit_ins*)instruction); 1460 } 1461 1462 /* --------------------------------------------------------------------- */ 1463 /* Floating point operators */ 1464 /* --------------------------------------------------------------------- */ 1465 1466 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_is_fpu_available(void) 1467 { 1468 #ifdef SLJIT_IS_FPU_AVAILABLE 1469 return SLJIT_IS_FPU_AVAILABLE; 1470 #else 1471 /* Available by default. */ 1472 return 1; 1473 #endif 1474 } 1475 1476 static sljit_si emit_fop_mem(struct sljit_compiler *compiler, sljit_si flags, sljit_si reg, sljit_si arg, sljit_sw argw) 1477 { 1478 sljit_ui shift = MEM_SIZE_SHIFT(flags); 1479 sljit_ins ins_bits = (shift << 30); 1480 sljit_si other_r; 1481 sljit_sw diff; 1482 1483 SLJIT_ASSERT(arg & SLJIT_MEM); 1484 1485 if (!(flags & STORE)) 1486 ins_bits |= 1 << 22; 1487 1488 if (arg & OFFS_REG_MASK) { 1489 argw &= 3; 1490 if (!argw || argw == shift) 1491 return push_inst(compiler, STR_FR | ins_bits | VT(reg) 1492 | RN(arg & REG_MASK) | RM(OFFS_REG(arg)) | (argw ? (1 << 12) : 0)); 1493 other_r = OFFS_REG(arg); 1494 arg &= REG_MASK; 1495 FAIL_IF(push_inst(compiler, ADD | RD(TMP_REG1) | RN(arg) | RM(other_r) | (argw << 10))); 1496 arg = TMP_REG1; 1497 argw = 0; 1498 } 1499 1500 arg &= REG_MASK; 1501 if (arg && argw >= 0 && ((argw >> shift) <= 0xfff) && (argw & ((1 << shift) - 1)) == 0) 1502 return push_inst(compiler, STR_FI | ins_bits | VT(reg) | RN(arg) | (argw << (10 - shift))); 1503 1504 if (arg && argw <= 255 && argw >= -256) 1505 return push_inst(compiler, STUR_FI | ins_bits | VT(reg) | RN(arg) | ((argw & 0x1ff) << 12)); 1506 1507 /* Slow cases */ 1508 if (compiler->cache_arg == SLJIT_MEM && argw != compiler->cache_argw) { 1509 diff = argw - compiler->cache_argw; 1510 if (!arg && diff <= 255 && diff >= -256) 1511 return push_inst(compiler, STUR_FI | ins_bits | VT(reg) | RN(TMP_REG3) | ((diff & 0x1ff) << 12)); 1512 if (emit_set_delta(compiler, TMP_REG3, TMP_REG3, argw - compiler->cache_argw) != SLJIT_ERR_UNSUPPORTED) { 1513 FAIL_IF(compiler->error); 1514 compiler->cache_argw = argw; 1515 } 1516 } 1517 1518 if (compiler->cache_arg != SLJIT_MEM || argw != compiler->cache_argw) { 1519 compiler->cache_arg = SLJIT_MEM; 1520 compiler->cache_argw = argw; 1521 FAIL_IF(load_immediate(compiler, TMP_REG3, argw)); 1522 } 1523 1524 if (arg & REG_MASK) 1525 return push_inst(compiler, STR_FR | ins_bits | VT(reg) | RN(arg) | RM(TMP_REG3)); 1526 return push_inst(compiler, STR_FI | ins_bits | VT(reg) | RN(TMP_REG3)); 1527 } 1528 1529 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fop1(struct sljit_compiler *compiler, sljit_si op, 1530 sljit_si dst, sljit_sw dstw, 1531 sljit_si src, sljit_sw srcw) 1532 { 1533 sljit_si dst_r, mem_flags = (op & SLJIT_SINGLE_OP) ? INT_SIZE : WORD_SIZE; 1534 sljit_ins inv_bits = (op & SLJIT_SINGLE_OP) ? (1 << 22) : 0; 1535 1536 CHECK_ERROR(); 1537 check_sljit_emit_fop1(compiler, op, dst, dstw, src, srcw); 1538 1539 compiler->cache_arg = 0; 1540 compiler->cache_argw = 0; 1541 1542 if (GET_OPCODE(op) == SLJIT_CMPD) { 1543 if (dst & SLJIT_MEM) { 1544 emit_fop_mem(compiler, mem_flags, TMP_FREG1, dst, dstw); 1545 dst = TMP_FREG1; 1546 } 1547 if (src & SLJIT_MEM) { 1548 emit_fop_mem(compiler, mem_flags, TMP_FREG2, src, srcw); 1549 src = TMP_FREG2; 1550 } 1551 return push_inst(compiler, (FCMP ^ inv_bits) | VN(dst) | VM(src)); 1552 } 1553 1554 dst_r = (dst <= REG_MASK) ? dst : TMP_FREG1; 1555 if (src & SLJIT_MEM) { 1556 emit_fop_mem(compiler, mem_flags, dst_r, src, srcw); 1557 src = dst_r; 1558 } 1559 1560 switch (GET_OPCODE(op)) { 1561 case SLJIT_MOVD: 1562 if (src != dst_r) 1563 FAIL_IF(push_inst(compiler, (FMOV ^ inv_bits) | VD(dst_r) | VN(src))); 1564 break; 1565 case SLJIT_NEGD: 1566 FAIL_IF(push_inst(compiler, (FNEG ^ inv_bits) | VD(dst_r) | VN(src))); 1567 break; 1568 case SLJIT_ABSD: 1569 FAIL_IF(push_inst(compiler, (FABS ^ inv_bits) | VD(dst_r) | VN(src))); 1570 break; 1571 } 1572 1573 if (!(dst & SLJIT_MEM)) 1574 return SLJIT_SUCCESS; 1575 return emit_fop_mem(compiler, mem_flags | STORE, TMP_FREG1, dst, dstw); 1576 } 1577 1578 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fop2(struct sljit_compiler *compiler, sljit_si op, 1579 sljit_si dst, sljit_sw dstw, 1580 sljit_si src1, sljit_sw src1w, 1581 sljit_si src2, sljit_sw src2w) 1582 { 1583 sljit_si dst_r, mem_flags = (op & SLJIT_SINGLE_OP) ? INT_SIZE : WORD_SIZE; 1584 sljit_ins inv_bits = (op & SLJIT_SINGLE_OP) ? (1 << 22) : 0; 1585 1586 CHECK_ERROR(); 1587 check_sljit_emit_fop2(compiler, op, dst, dstw, src1, src1w, src2, src2w); 1588 1589 compiler->cache_arg = 0; 1590 compiler->cache_argw = 0; 1591 1592 dst_r = (dst <= REG_MASK) ? dst : TMP_FREG1; 1593 if (src1 & SLJIT_MEM) { 1594 emit_fop_mem(compiler, mem_flags, TMP_FREG1, src1, src1w); 1595 src1 = TMP_FREG1; 1596 } 1597 if (src2 & SLJIT_MEM) { 1598 emit_fop_mem(compiler, mem_flags, TMP_FREG2, src2, src2w); 1599 src2 = TMP_FREG2; 1600 } 1601 1602 switch (GET_OPCODE(op)) { 1603 case SLJIT_ADDD: 1604 FAIL_IF(push_inst(compiler, (FADD ^ inv_bits) | VD(dst_r) | VN(src1) | VM(src2))); 1605 break; 1606 case SLJIT_SUBD: 1607 FAIL_IF(push_inst(compiler, (FSUB ^ inv_bits) | VD(dst_r) | VN(src1) | VM(src2))); 1608 break; 1609 case SLJIT_MULD: 1610 FAIL_IF(push_inst(compiler, (FMUL ^ inv_bits) | VD(dst_r) | VN(src1) | VM(src2))); 1611 break; 1612 case SLJIT_DIVD: 1613 FAIL_IF(push_inst(compiler, (FDIV ^ inv_bits) | VD(dst_r) | VN(src1) | VM(src2))); 1614 break; 1615 } 1616 1617 if (!(dst & SLJIT_MEM)) 1618 return SLJIT_SUCCESS; 1619 return emit_fop_mem(compiler, mem_flags | STORE, TMP_FREG1, dst, dstw); 1620 } 1621 1622 /* --------------------------------------------------------------------- */ 1623 /* Other instructions */ 1624 /* --------------------------------------------------------------------- */ 1625 1626 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fast_enter(struct sljit_compiler *compiler, sljit_si dst, sljit_sw dstw) 1627 { 1628 CHECK_ERROR(); 1629 check_sljit_emit_fast_enter(compiler, dst, dstw); 1630 ADJUST_LOCAL_OFFSET(dst, dstw); 1631 1632 /* For UNUSED dst. Uncommon, but possible. */ 1633 if (dst == SLJIT_UNUSED) 1634 return SLJIT_SUCCESS; 1635 1636 if (dst <= REG_MASK) 1637 return push_inst(compiler, ORR | RD(dst) | RN(TMP_ZERO) | RM(TMP_LR)); 1638 1639 /* Memory. */ 1640 return emit_op_mem(compiler, WORD_SIZE | STORE, TMP_LR, dst, dstw); 1641 } 1642 1643 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fast_return(struct sljit_compiler *compiler, sljit_si src, sljit_sw srcw) 1644 { 1645 CHECK_ERROR(); 1646 check_sljit_emit_fast_return(compiler, src, srcw); 1647 ADJUST_LOCAL_OFFSET(src, srcw); 1648 1649 if (src <= REG_MASK) 1650 FAIL_IF(push_inst(compiler, ORR | RD(TMP_LR) | RN(TMP_ZERO) | RM(src))); 1651 else if (src & SLJIT_MEM) 1652 FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_LR, src, srcw)); 1653 else if (src & SLJIT_IMM) 1654 FAIL_IF(load_immediate(compiler, TMP_LR, srcw)); 1655 1656 return push_inst(compiler, RET | RN(TMP_LR)); 1657 } 1658 1659 /* --------------------------------------------------------------------- */ 1660 /* Conditional instructions */ 1661 /* --------------------------------------------------------------------- */ 1662 1663 static sljit_uw get_cc(sljit_si type) 1664 { 1665 switch (type) { 1666 case SLJIT_C_EQUAL: 1667 case SLJIT_C_MUL_NOT_OVERFLOW: 1668 case SLJIT_C_FLOAT_EQUAL: 1669 return 0x1; 1670 1671 case SLJIT_C_NOT_EQUAL: 1672 case SLJIT_C_MUL_OVERFLOW: 1673 case SLJIT_C_FLOAT_NOT_EQUAL: 1674 return 0x0; 1675 1676 case SLJIT_C_LESS: 1677 case SLJIT_C_FLOAT_LESS: 1678 return 0x2; 1679 1680 case SLJIT_C_GREATER_EQUAL: 1681 case SLJIT_C_FLOAT_GREATER_EQUAL: 1682 return 0x3; 1683 1684 case SLJIT_C_GREATER: 1685 case SLJIT_C_FLOAT_GREATER: 1686 return 0x9; 1687 1688 case SLJIT_C_LESS_EQUAL: 1689 case SLJIT_C_FLOAT_LESS_EQUAL: 1690 return 0x8; 1691 1692 case SLJIT_C_SIG_LESS: 1693 return 0xa; 1694 1695 case SLJIT_C_SIG_GREATER_EQUAL: 1696 return 0xb; 1697 1698 case SLJIT_C_SIG_GREATER: 1699 return 0xd; 1700 1701 case SLJIT_C_SIG_LESS_EQUAL: 1702 return 0xc; 1703 1704 case SLJIT_C_OVERFLOW: 1705 case SLJIT_C_FLOAT_UNORDERED: 1706 return 0x7; 1707 1708 case SLJIT_C_NOT_OVERFLOW: 1709 case SLJIT_C_FLOAT_ORDERED: 1710 return 0x6; 1711 1712 default: 1713 SLJIT_ASSERT_STOP(); 1714 return 0xe; 1715 } 1716 } 1717 1718 SLJIT_API_FUNC_ATTRIBUTE struct sljit_label* sljit_emit_label(struct sljit_compiler *compiler) 1719 { 1720 struct sljit_label *label; 1721 1722 CHECK_ERROR_PTR(); 1723 check_sljit_emit_label(compiler); 1724 1725 if (compiler->last_label && compiler->last_label->size == compiler->size) 1726 return compiler->last_label; 1727 1728 label = (struct sljit_label*)ensure_abuf(compiler, sizeof(struct sljit_label)); 1729 PTR_FAIL_IF(!label); 1730 set_label(label, compiler); 1731 return label; 1732 } 1733 1734 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_jump(struct sljit_compiler *compiler, sljit_si type) 1735 { 1736 struct sljit_jump *jump; 1737 1738 CHECK_ERROR_PTR(); 1739 check_sljit_emit_jump(compiler, type); 1740 1741 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump)); 1742 PTR_FAIL_IF(!jump); 1743 set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP); 1744 type &= 0xff; 1745 1746 if (type < SLJIT_JUMP) { 1747 jump->flags |= IS_COND; 1748 PTR_FAIL_IF(push_inst(compiler, B_CC | (6 << 5) | get_cc(type))); 1749 } 1750 else if (type >= SLJIT_FAST_CALL) 1751 jump->flags |= IS_BL; 1752 1753 PTR_FAIL_IF(emit_imm64_const(compiler, TMP_REG1, 0)); 1754 jump->addr = compiler->size; 1755 PTR_FAIL_IF(push_inst(compiler, ((type >= SLJIT_FAST_CALL) ? BLR : BR) | RN(TMP_REG1))); 1756 1757 return jump; 1758 } 1759 1760 static SLJIT_INLINE struct sljit_jump* emit_cmp_to0(struct sljit_compiler *compiler, sljit_si type, 1761 sljit_si src, sljit_sw srcw) 1762 { 1763 struct sljit_jump *jump; 1764 sljit_ins inv_bits = (type & SLJIT_INT_OP) ? (1 << 31) : 0; 1765 1766 SLJIT_ASSERT((type & 0xff) == SLJIT_C_EQUAL || (type & 0xff) == SLJIT_C_NOT_EQUAL); 1767 ADJUST_LOCAL_OFFSET(src, srcw); 1768 1769 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump)); 1770 PTR_FAIL_IF(!jump); 1771 set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP); 1772 jump->flags |= IS_CBZ | IS_COND; 1773 1774 if (src & SLJIT_MEM) { 1775 PTR_FAIL_IF(emit_op_mem(compiler, inv_bits ? INT_SIZE : WORD_SIZE, TMP_REG1, src, srcw)); 1776 src = TMP_REG1; 1777 } 1778 else if (src & SLJIT_IMM) { 1779 PTR_FAIL_IF(load_immediate(compiler, TMP_REG1, srcw)); 1780 src = TMP_REG1; 1781 } 1782 SLJIT_ASSERT(FAST_IS_REG(src)); 1783 1784 if ((type & 0xff) == SLJIT_C_EQUAL) 1785 inv_bits |= 1 << 24; 1786 1787 PTR_FAIL_IF(push_inst(compiler, (CBZ ^ inv_bits) | (6 << 5) | RT(src))); 1788 PTR_FAIL_IF(emit_imm64_const(compiler, TMP_REG1, 0)); 1789 jump->addr = compiler->size; 1790 PTR_FAIL_IF(push_inst(compiler, BR | RN(TMP_REG1))); 1791 return jump; 1792 } 1793 1794 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_ijump(struct sljit_compiler *compiler, sljit_si type, sljit_si src, sljit_sw srcw) 1795 { 1796 struct sljit_jump *jump; 1797 1798 CHECK_ERROR(); 1799 check_sljit_emit_ijump(compiler, type, src, srcw); 1800 ADJUST_LOCAL_OFFSET(src, srcw); 1801 1802 /* In ARM, we don't need to touch the arguments. */ 1803 if (!(src & SLJIT_IMM)) { 1804 if (src & SLJIT_MEM) { 1805 FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG1, src, srcw)); 1806 src = TMP_REG1; 1807 } 1808 return push_inst(compiler, ((type >= SLJIT_FAST_CALL) ? BLR : BR) | RN(src)); 1809 } 1810 1811 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump)); 1812 FAIL_IF(!jump); 1813 set_jump(jump, compiler, JUMP_ADDR | ((type >= SLJIT_FAST_CALL) ? IS_BL : 0)); 1814 jump->u.target = srcw; 1815 1816 FAIL_IF(emit_imm64_const(compiler, TMP_REG1, 0)); 1817 jump->addr = compiler->size; 1818 return push_inst(compiler, ((type >= SLJIT_FAST_CALL) ? BLR : BR) | RN(TMP_REG1)); 1819 } 1820 1821 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op_flags(struct sljit_compiler *compiler, sljit_si op, 1822 sljit_si dst, sljit_sw dstw, 1823 sljit_si src, sljit_sw srcw, 1824 sljit_si type) 1825 { 1826 sljit_si dst_r, flags, mem_flags; 1827 sljit_ins cc; 1828 1829 CHECK_ERROR(); 1830 check_sljit_emit_op_flags(compiler, op, dst, dstw, src, srcw, type); 1831 ADJUST_LOCAL_OFFSET(dst, dstw); 1832 ADJUST_LOCAL_OFFSET(src, srcw); 1833 1834 if (dst == SLJIT_UNUSED) 1835 return SLJIT_SUCCESS; 1836 1837 cc = get_cc(type); 1838 dst_r = (dst <= REG_MASK) ? dst : TMP_REG1; 1839 1840 if (GET_OPCODE(op) < SLJIT_ADD) { 1841 FAIL_IF(push_inst(compiler, CSINC | (cc << 12) | RD(dst_r) | RN(TMP_ZERO) | RM(TMP_ZERO))); 1842 if (dst_r != TMP_REG1) 1843 return SLJIT_SUCCESS; 1844 return emit_op_mem(compiler, (GET_OPCODE(op) == SLJIT_MOV ? WORD_SIZE : INT_SIZE) | STORE, TMP_REG1, dst, dstw); 1845 } 1846 1847 compiler->cache_arg = 0; 1848 compiler->cache_argw = 0; 1849 flags = GET_FLAGS(op) ? SET_FLAGS : 0; 1850 mem_flags = WORD_SIZE; 1851 if (op & SLJIT_INT_OP) { 1852 flags |= INT_OP; 1853 mem_flags = INT_SIZE; 1854 } 1855 1856 if (src & SLJIT_MEM) { 1857 FAIL_IF(emit_op_mem2(compiler, mem_flags, TMP_REG1, src, srcw, dst, dstw)); 1858 src = TMP_REG1; 1859 srcw = 0; 1860 } else if (src & SLJIT_IMM) 1861 flags |= ARG1_IMM; 1862 1863 FAIL_IF(push_inst(compiler, CSINC | (cc << 12) | RD(TMP_REG2) | RN(TMP_ZERO) | RM(TMP_ZERO))); 1864 emit_op_imm(compiler, flags | GET_OPCODE(op), dst_r, src, TMP_REG2); 1865 1866 if (dst_r != TMP_REG1) 1867 return SLJIT_SUCCESS; 1868 return emit_op_mem2(compiler, mem_flags | STORE, TMP_REG1, dst, dstw, 0, 0); 1869 } 1870 1871 SLJIT_API_FUNC_ATTRIBUTE struct sljit_const* sljit_emit_const(struct sljit_compiler *compiler, sljit_si dst, sljit_sw dstw, sljit_sw init_value) 1872 { 1873 struct sljit_const *const_; 1874 sljit_si dst_r; 1875 1876 CHECK_ERROR_PTR(); 1877 check_sljit_emit_const(compiler, dst, dstw, init_value); 1878 ADJUST_LOCAL_OFFSET(dst, dstw); 1879 1880 const_ = (struct sljit_const*)ensure_abuf(compiler, sizeof(struct sljit_const)); 1881 PTR_FAIL_IF(!const_); 1882 set_const(const_, compiler); 1883 1884 dst_r = SLOW_IS_REG(dst) ? dst : TMP_REG1; 1885 PTR_FAIL_IF(emit_imm64_const(compiler, dst_r, init_value)); 1886 1887 if (dst & SLJIT_MEM) 1888 PTR_FAIL_IF(emit_op_mem(compiler, WORD_SIZE | STORE, dst_r, dst, dstw)); 1889 return const_; 1890 } 1891 1892 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_addr) 1893 { 1894 sljit_ins* inst = (sljit_ins*)addr; 1895 modify_imm64_const(inst, new_addr); 1896 SLJIT_CACHE_FLUSH(inst, inst + 4); 1897 } 1898 1899 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_const(sljit_uw addr, sljit_sw new_constant) 1900 { 1901 sljit_ins* inst = (sljit_ins*)addr; 1902 modify_imm64_const(inst, new_constant); 1903 SLJIT_CACHE_FLUSH(inst, inst + 4); 1904 } 1905