2 * Stack-less Just-In-Time compiler
4 * Copyright Zoltan Herczeg (hzmester@freemail.hu). All rights reserved.
6 * Redistribution and use in source and binary forms, with or without modification, are
7 * permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright notice, this list of
10 * conditions and the following disclaimer.
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.
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.
27 SLJIT_API_FUNC_ATTRIBUTE const char* sljit_get_platform_name(void)
30 return "ARM-Thumb2" SLJIT_CPUINFO " ABI:softfp";
32 return "ARM-Thumb2" SLJIT_CPUINFO " ABI:hardfp";
36 /* Length of an instruction word. */
37 typedef sljit_u32 sljit_ins;
39 /* Last register + 1. */
40 #define TMP_REG1 (SLJIT_NUMBER_OF_REGISTERS + 2)
41 #define TMP_REG2 (SLJIT_NUMBER_OF_REGISTERS + 3)
42 #define TMP_PC (SLJIT_NUMBER_OF_REGISTERS + 4)
44 #define TMP_FREG1 (SLJIT_NUMBER_OF_FLOAT_REGISTERS + 1)
45 #define TMP_FREG2 (SLJIT_NUMBER_OF_FLOAT_REGISTERS + 2)
47 /* See sljit_emit_enter and sljit_emit_op0 if you want to change them. */
48 static const sljit_u8 reg_map[SLJIT_NUMBER_OF_REGISTERS + 5] = {
49 0, 0, 1, 2, 3, 11, 10, 9, 8, 7, 6, 5, 4, 13, 12, 14, 15
52 static const sljit_u8 freg_map[SLJIT_NUMBER_OF_FLOAT_REGISTERS + 3] = {
53 0, 0, 1, 2, 3, 4, 5, 6, 7
56 #define COPY_BITS(src, from, to, bits) \
57 ((from >= to ? (src >> (from - to)) : (src << (to - from))) & (((1 << bits) - 1) << to))
59 /* Thumb16 encodings. */
60 #define RD3(rd) (reg_map[rd])
61 #define RN3(rn) (reg_map[rn] << 3)
62 #define RM3(rm) (reg_map[rm] << 6)
63 #define RDN3(rdn) (reg_map[rdn] << 8)
64 #define IMM3(imm) (imm << 6)
65 #define IMM8(imm) (imm)
67 /* Thumb16 helpers. */
68 #define SET_REGS44(rd, rn) \
69 ((reg_map[rn] << 3) | (reg_map[rd] & 0x7) | ((reg_map[rd] & 0x8) << 4))
70 #define IS_2_LO_REGS(reg1, reg2) \
71 (reg_map[reg1] <= 7 && reg_map[reg2] <= 7)
72 #define IS_3_LO_REGS(reg1, reg2, reg3) \
73 (reg_map[reg1] <= 7 && reg_map[reg2] <= 7 && reg_map[reg3] <= 7)
75 /* Thumb32 encodings. */
76 #define RD4(rd) (reg_map[rd] << 8)
77 #define RN4(rn) (reg_map[rn] << 16)
78 #define RM4(rm) (reg_map[rm])
79 #define RT4(rt) (reg_map[rt] << 12)
80 #define DD4(dd) (freg_map[dd] << 12)
81 #define DN4(dn) (freg_map[dn] << 16)
82 #define DM4(dm) (freg_map[dm])
84 (COPY_BITS(imm, 2, 12, 3) | ((imm & 0x3) << 6))
86 (COPY_BITS(imm, 11, 26, 1) | COPY_BITS(imm, 8, 12, 3) | (imm & 0xff))
88 /* --------------------------------------------------------------------- */
89 /* Instrucion forms */
90 /* --------------------------------------------------------------------- */
92 /* dot '.' changed to _
93 I immediate form (possibly followed by number of immediate bits). */
94 #define ADCI 0xf1400000
96 #define ADC_W 0xeb400000
100 #define ADDSI8 0x3000
101 #define ADD_W 0xeb000000
102 #define ADDWI 0xf2000000
103 #define ADD_SP 0xb000
104 #define ADD_W 0xeb000000
105 #define ADD_WI 0xf1000000
106 #define ANDI 0xf0000000
108 #define AND_W 0xea000000
111 #define ASR_W 0xfa40f000
112 #define ASR_WI 0xea4f0020
114 #define BICI 0xf0200000
118 #define CLZ 0xfab0f080
119 #define CMNI_W 0xf1100f00
122 #define CMPI_W 0xf1b00f00
124 #define CMP_W 0xebb00f00
125 #define EORI 0xf0800000
127 #define EOR_W 0xea800000
129 #define LDRI 0xf8500800
132 #define LSL_W 0xfa00f000
133 #define LSL_WI 0xea4f0000
136 #define LSR_W 0xfa20f000
137 #define LSR_WI 0xea4f0010
141 #define MOVT 0xf2c00000
142 #define MOVW 0xf2400000
143 #define MOV_W 0xea4f0000
144 #define MOV_WI 0xf04f0000
145 #define MUL 0xfb00f000
147 #define MVN_W 0xea6f0000
148 #define MVN_WI 0xf06f0000
150 #define ORNI 0xf0600000
151 #define ORRI 0xf0400000
153 #define ORR_W 0xea400000
155 #define POP_W 0xe8bd0000
157 #define PUSH_W 0xe92d0000
158 #define RSB_WI 0xf1c00000
160 #define SBCI 0xf1600000
162 #define SBC_W 0xeb600000
163 #define SDIV 0xfb90f0f0
164 #define SMULL 0xfb800000
165 #define STR_SP 0x9000
167 #define SUBSI3 0x1e00
168 #define SUBSI8 0x3800
169 #define SUB_W 0xeba00000
170 #define SUBWI 0xf2a00000
171 #define SUB_SP 0xb080
172 #define SUB_WI 0xf1a00000
174 #define SXTB_W 0xfa4ff080
176 #define SXTH_W 0xfa0ff080
178 #define UDIV 0xfbb0f0f0
179 #define UMULL 0xfba00000
181 #define UXTB_W 0xfa5ff080
183 #define UXTH_W 0xfa1ff080
184 #define VABS_F32 0xeeb00ac0
185 #define VADD_F32 0xee300a00
186 #define VCMP_F32 0xeeb40a40
187 #define VCVT_F32_S32 0xeeb80ac0
188 #define VCVT_F64_F32 0xeeb70ac0
189 #define VCVT_S32_F32 0xeebd0ac0
190 #define VDIV_F32 0xee800a00
191 #define VMOV_F32 0xeeb00a40
192 #define VMOV 0xee000a10
193 #define VMOV2 0xec400a10
194 #define VMRS 0xeef1fa10
195 #define VMUL_F32 0xee200a00
196 #define VNEG_F32 0xeeb10a40
197 #define VSTR_F32 0xed000a00
198 #define VSUB_F32 0xee300a40
200 static sljit_s32 push_inst16(struct sljit_compiler *compiler, sljit_ins inst)
203 SLJIT_ASSERT(!(inst & 0xffff0000));
205 ptr = (sljit_u16*)ensure_buf(compiler, sizeof(sljit_u16));
209 return SLJIT_SUCCESS;
212 static sljit_s32 push_inst32(struct sljit_compiler *compiler, sljit_ins inst)
214 sljit_u16 *ptr = (sljit_u16*)ensure_buf(compiler, sizeof(sljit_ins));
219 return SLJIT_SUCCESS;
222 static SLJIT_INLINE sljit_s32 emit_imm32_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_uw imm)
224 FAIL_IF(push_inst32(compiler, MOVW | RD4(dst)
225 | COPY_BITS(imm, 12, 16, 4) | COPY_BITS(imm, 11, 26, 1) | COPY_BITS(imm, 8, 12, 3) | (imm & 0xff)));
226 return push_inst32(compiler, MOVT | RD4(dst)
227 | COPY_BITS(imm, 12 + 16, 16, 4) | COPY_BITS(imm, 11 + 16, 26, 1) | COPY_BITS(imm, 8 + 16, 12, 3) | ((imm & 0xff0000) >> 16));
230 static SLJIT_INLINE void modify_imm32_const(sljit_u16 *inst, sljit_uw new_imm)
232 sljit_s32 dst = inst[1] & 0x0f00;
233 SLJIT_ASSERT(((inst[0] & 0xfbf0) == (MOVW >> 16)) && ((inst[2] & 0xfbf0) == (MOVT >> 16)) && dst == (inst[3] & 0x0f00));
234 inst[0] = (MOVW >> 16) | COPY_BITS(new_imm, 12, 0, 4) | COPY_BITS(new_imm, 11, 10, 1);
235 inst[1] = dst | COPY_BITS(new_imm, 8, 12, 3) | (new_imm & 0xff);
236 inst[2] = (MOVT >> 16) | COPY_BITS(new_imm, 12 + 16, 0, 4) | COPY_BITS(new_imm, 11 + 16, 10, 1);
237 inst[3] = dst | COPY_BITS(new_imm, 8 + 16, 12, 3) | ((new_imm & 0xff0000) >> 16);
240 static SLJIT_INLINE sljit_s32 detect_jump_type(struct sljit_jump *jump, sljit_u16 *code_ptr, sljit_u16 *code, sljit_sw executable_offset)
244 if (jump->flags & SLJIT_REWRITABLE_JUMP)
247 if (jump->flags & JUMP_ADDR) {
248 /* Branch to ARM code is not optimized yet. */
249 if (!(jump->u.target & 0x1))
251 diff = ((sljit_sw)jump->u.target - (sljit_sw)(code_ptr + 2) - executable_offset) >> 1;
254 SLJIT_ASSERT(jump->flags & JUMP_LABEL);
255 diff = ((sljit_sw)(code + jump->u.label->size) - (sljit_sw)(code_ptr + 2)) >> 1;
258 if (jump->flags & IS_COND) {
259 SLJIT_ASSERT(!(jump->flags & IS_BL));
260 if (diff <= 127 && diff >= -128) {
261 jump->flags |= PATCH_TYPE1;
264 if (diff <= 524287 && diff >= -524288) {
265 jump->flags |= PATCH_TYPE2;
268 /* +1 comes from the prefix IT instruction. */
270 if (diff <= 8388607 && diff >= -8388608) {
271 jump->flags |= PATCH_TYPE3;
275 else if (jump->flags & IS_BL) {
276 if (diff <= 8388607 && diff >= -8388608) {
277 jump->flags |= PATCH_BL;
282 if (diff <= 1023 && diff >= -1024) {
283 jump->flags |= PATCH_TYPE4;
286 if (diff <= 8388607 && diff >= -8388608) {
287 jump->flags |= PATCH_TYPE5;
295 static SLJIT_INLINE void set_jump_instruction(struct sljit_jump *jump, sljit_sw executable_offset)
297 sljit_s32 type = (jump->flags >> 4) & 0xf;
299 sljit_u16 *jump_inst;
302 if (SLJIT_UNLIKELY(type == 0)) {
303 modify_imm32_const((sljit_u16*)jump->addr, (jump->flags & JUMP_LABEL) ? jump->u.label->addr : jump->u.target);
307 if (jump->flags & JUMP_ADDR) {
308 SLJIT_ASSERT(jump->u.target & 0x1);
309 diff = ((sljit_sw)jump->u.target - (sljit_sw)(jump->addr + sizeof(sljit_u32)) - executable_offset) >> 1;
312 SLJIT_ASSERT(jump->u.label->addr & 0x1);
313 diff = ((sljit_sw)(jump->u.label->addr) - (sljit_sw)(jump->addr + sizeof(sljit_u32)) - executable_offset) >> 1;
315 jump_inst = (sljit_u16*)jump->addr;
319 /* Encoding T1 of 'B' instruction */
320 SLJIT_ASSERT(diff <= 127 && diff >= -128 && (jump->flags & IS_COND));
321 jump_inst[0] = 0xd000 | (jump->flags & 0xf00) | (diff & 0xff);
324 /* Encoding T3 of 'B' instruction */
325 SLJIT_ASSERT(diff <= 524287 && diff >= -524288 && (jump->flags & IS_COND));
326 jump_inst[0] = 0xf000 | COPY_BITS(jump->flags, 8, 6, 4) | COPY_BITS(diff, 11, 0, 6) | COPY_BITS(diff, 19, 10, 1);
327 jump_inst[1] = 0x8000 | COPY_BITS(diff, 17, 13, 1) | COPY_BITS(diff, 18, 11, 1) | (diff & 0x7ff);
330 SLJIT_ASSERT(jump->flags & IS_COND);
331 *jump_inst++ = IT | ((jump->flags >> 4) & 0xf0) | 0x8;
336 /* Encoding T2 of 'B' instruction */
337 SLJIT_ASSERT(diff <= 1023 && diff >= -1024 && !(jump->flags & IS_COND));
338 jump_inst[0] = 0xe000 | (diff & 0x7ff);
342 SLJIT_ASSERT(diff <= 8388607 && diff >= -8388608);
344 /* Really complex instruction form for branches. */
345 s = (diff >> 23) & 0x1;
346 j1 = (~(diff >> 22) ^ s) & 0x1;
347 j2 = (~(diff >> 21) ^ s) & 0x1;
348 jump_inst[0] = 0xf000 | (s << 10) | COPY_BITS(diff, 11, 0, 10);
349 jump_inst[1] = (j1 << 13) | (j2 << 11) | (diff & 0x7ff);
351 /* The others have a common form. */
352 if (type == 5) /* Encoding T4 of 'B' instruction */
353 jump_inst[1] |= 0x9000;
354 else if (type == 6) /* Encoding T1 of 'BL' instruction */
355 jump_inst[1] |= 0xd000;
360 SLJIT_API_FUNC_ATTRIBUTE void* sljit_generate_code(struct sljit_compiler *compiler)
362 struct sljit_memory_fragment *buf;
368 sljit_sw executable_offset;
370 struct sljit_label *label;
371 struct sljit_jump *jump;
372 struct sljit_const *const_;
375 CHECK_PTR(check_sljit_generate_code(compiler));
376 reverse_buf(compiler);
378 code = (sljit_u16*)SLJIT_MALLOC_EXEC(compiler->size * sizeof(sljit_u16));
379 PTR_FAIL_WITH_EXEC_IF(code);
384 executable_offset = SLJIT_EXEC_OFFSET(code);
386 label = compiler->labels;
387 jump = compiler->jumps;
388 const_ = compiler->consts;
391 buf_ptr = (sljit_u16*)buf->memory;
392 buf_end = buf_ptr + (buf->used_size >> 1);
394 *code_ptr = *buf_ptr++;
395 /* These structures are ordered by their address. */
396 SLJIT_ASSERT(!label || label->size >= half_count);
397 SLJIT_ASSERT(!jump || jump->addr >= half_count);
398 SLJIT_ASSERT(!const_ || const_->addr >= half_count);
399 if (label && label->size == half_count) {
400 label->addr = ((sljit_uw)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset)) | 0x1;
401 label->size = code_ptr - code;
404 if (jump && jump->addr == half_count) {
405 jump->addr = (sljit_uw)code_ptr - ((jump->flags & IS_COND) ? 10 : 8);
406 code_ptr -= detect_jump_type(jump, code_ptr, code, executable_offset);
409 if (const_ && const_->addr == half_count) {
410 const_->addr = (sljit_uw)code_ptr;
411 const_ = const_->next;
415 } while (buf_ptr < buf_end);
420 if (label && label->size == half_count) {
421 label->addr = ((sljit_uw)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset)) | 0x1;
422 label->size = code_ptr - code;
426 SLJIT_ASSERT(!label);
428 SLJIT_ASSERT(!const_);
429 SLJIT_ASSERT(code_ptr - code <= (sljit_sw)compiler->size);
431 jump = compiler->jumps;
433 set_jump_instruction(jump, executable_offset);
437 compiler->error = SLJIT_ERR_COMPILED;
438 compiler->executable_offset = executable_offset;
439 compiler->executable_size = (code_ptr - code) * sizeof(sljit_u16);
441 code = (sljit_u16 *)SLJIT_ADD_EXEC_OFFSET(code, executable_offset);
442 code_ptr = (sljit_u16 *)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset);
444 SLJIT_CACHE_FLUSH(code, code_ptr);
445 /* Set thumb mode flag. */
446 return (void*)((sljit_uw)code | 0x1);
449 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_has_cpu_feature(sljit_s32 feature_type)
451 switch (feature_type) {
453 #ifdef SLJIT_IS_FPU_AVAILABLE
454 return SLJIT_IS_FPU_AVAILABLE;
456 /* Available by default. */
469 /* --------------------------------------------------------------------- */
470 /* Core code generator functions. */
471 /* --------------------------------------------------------------------- */
473 #define INVALID_IMM 0x80000000
474 static sljit_uw get_imm(sljit_uw imm)
476 /* Thumb immediate form. */
482 if ((imm & 0xffff) == (imm >> 16)) {
483 /* Some special cases. */
485 return (1 << 12) | (imm & 0xff);
487 return (2 << 12) | ((imm >> 8) & 0xff);
488 if ((imm & 0xff00) == ((imm & 0xff) << 8))
489 return (3 << 12) | (imm & 0xff);
492 /* Assembly optimization: count leading zeroes? */
494 if (!(imm & 0xffff0000)) {
498 if (!(imm & 0xff000000)) {
502 if (!(imm & 0xf0000000)) {
506 if (!(imm & 0xc0000000)) {
510 if (!(imm & 0x80000000)) {
514 /* Since imm >= 128, this must be true. */
515 SLJIT_ASSERT(counter <= 31);
517 if (imm & 0x00ffffff)
518 return INVALID_IMM; /* Cannot be encoded. */
520 return ((imm >> 24) & 0x7f) | COPY_BITS(counter, 4, 26, 1) | COPY_BITS(counter, 1, 12, 3) | COPY_BITS(counter, 0, 7, 1);
523 static sljit_s32 load_immediate(struct sljit_compiler *compiler, sljit_s32 dst, sljit_uw imm)
527 /* MOVS cannot be used since it destroy flags. */
529 if (imm >= 0x10000) {
531 if (tmp != INVALID_IMM)
532 return push_inst32(compiler, MOV_WI | RD4(dst) | tmp);
534 if (tmp != INVALID_IMM)
535 return push_inst32(compiler, MVN_WI | RD4(dst) | tmp);
538 /* set low 16 bits, set hi 16 bits to 0. */
539 FAIL_IF(push_inst32(compiler, MOVW | RD4(dst)
540 | COPY_BITS(imm, 12, 16, 4) | COPY_BITS(imm, 11, 26, 1) | COPY_BITS(imm, 8, 12, 3) | (imm & 0xff)));
542 /* set hi 16 bit if needed. */
544 return push_inst32(compiler, MOVT | RD4(dst)
545 | COPY_BITS(imm, 12 + 16, 16, 4) | COPY_BITS(imm, 11 + 16, 26, 1) | COPY_BITS(imm, 8 + 16, 12, 3) | ((imm & 0xff0000) >> 16));
546 return SLJIT_SUCCESS;
549 #define ARG1_IMM 0x0010000
550 #define ARG2_IMM 0x0020000
551 /* SET_FLAGS must be 0x100000 as it is also the value of S bit (can be used for optimization). */
552 #define SET_FLAGS 0x0100000
553 #define UNUSED_RETURN 0x0200000
555 static sljit_s32 emit_op_imm(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 dst, sljit_uw arg1, sljit_uw arg2)
557 /* dst must be register, TMP_REG1
558 arg1 must be register, imm
559 arg2 must be register, imm */
563 if (SLJIT_UNLIKELY((flags & (ARG1_IMM | ARG2_IMM)) == (ARG1_IMM | ARG2_IMM))) {
564 /* Both are immediates, no temporaries are used. */
566 FAIL_IF(load_immediate(compiler, TMP_REG1, arg1));
570 if (flags & (ARG1_IMM | ARG2_IMM)) {
571 reg = (flags & ARG2_IMM) ? arg1 : arg2;
572 imm = (flags & ARG2_IMM) ? arg2 : arg1;
574 switch (flags & 0xffff) {
577 /* No form with immediate operand. */
580 SLJIT_ASSERT(!(flags & SET_FLAGS) && (flags & ARG2_IMM) && arg1 == TMP_REG2);
581 return load_immediate(compiler, dst, imm);
583 if (!(flags & SET_FLAGS))
584 return load_immediate(compiler, dst, ~imm);
585 /* Since the flags should be set, we just fallback to the register mode.
586 Although some clever things could be done here, "NOT IMM" does not worth the efforts. */
590 if (IS_2_LO_REGS(reg, dst)) {
592 return push_inst16(compiler, ADDSI3 | IMM3(imm) | RD3(dst) | RN3(reg));
594 return push_inst16(compiler, SUBSI3 | IMM3(nimm) | RD3(dst) | RN3(reg));
597 return push_inst16(compiler, ADDSI8 | IMM8(imm) | RDN3(dst));
599 return push_inst16(compiler, SUBSI8 | IMM8(nimm) | RDN3(dst));
602 if (!(flags & SET_FLAGS)) {
604 return push_inst32(compiler, ADDWI | RD4(dst) | RN4(reg) | IMM12(imm));
606 return push_inst32(compiler, SUBWI | RD4(dst) | RN4(reg) | IMM12(nimm));
609 if (nimm != INVALID_IMM)
610 return push_inst32(compiler, ADD_WI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | nimm);
611 nimm = get_imm(-imm);
612 if (nimm != INVALID_IMM)
613 return push_inst32(compiler, SUB_WI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | nimm);
617 if (imm != INVALID_IMM)
618 return push_inst32(compiler, ADCI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
621 /* SUB operation can be replaced by ADD because of the negative carry flag. */
622 if (flags & ARG1_IMM) {
623 if (imm == 0 && IS_2_LO_REGS(reg, dst))
624 return push_inst16(compiler, RSBSI | RD3(dst) | RN3(reg));
626 if (imm != INVALID_IMM)
627 return push_inst32(compiler, RSB_WI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
630 if (flags & UNUSED_RETURN) {
631 if (imm <= 0xff && reg_map[reg] <= 7)
632 return push_inst16(compiler, CMPI | IMM8(imm) | RDN3(reg));
634 if (nimm != INVALID_IMM)
635 return push_inst32(compiler, CMPI_W | RN4(reg) | nimm);
636 nimm = get_imm(-imm);
637 if (nimm != INVALID_IMM)
638 return push_inst32(compiler, CMNI_W | RN4(reg) | nimm);
641 if (IS_2_LO_REGS(reg, dst)) {
643 return push_inst16(compiler, SUBSI3 | IMM3(imm) | RD3(dst) | RN3(reg));
645 return push_inst16(compiler, ADDSI3 | IMM3(nimm) | RD3(dst) | RN3(reg));
648 return push_inst16(compiler, SUBSI8 | IMM8(imm) | RDN3(dst));
650 return push_inst16(compiler, ADDSI8 | IMM8(nimm) | RDN3(dst));
653 if (!(flags & SET_FLAGS)) {
655 return push_inst32(compiler, SUBWI | RD4(dst) | RN4(reg) | IMM12(imm));
657 return push_inst32(compiler, ADDWI | RD4(dst) | RN4(reg) | IMM12(nimm));
660 if (nimm != INVALID_IMM)
661 return push_inst32(compiler, SUB_WI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | nimm);
662 nimm = get_imm(-imm);
663 if (nimm != INVALID_IMM)
664 return push_inst32(compiler, ADD_WI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | nimm);
667 if (flags & ARG1_IMM)
670 if (imm != INVALID_IMM)
671 return push_inst32(compiler, SBCI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
675 if (nimm != INVALID_IMM)
676 return push_inst32(compiler, ANDI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | nimm);
678 if (imm != INVALID_IMM)
679 return push_inst32(compiler, BICI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
683 if (nimm != INVALID_IMM)
684 return push_inst32(compiler, ORRI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | nimm);
686 if (imm != INVALID_IMM)
687 return push_inst32(compiler, ORNI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
691 if (imm != INVALID_IMM)
692 return push_inst32(compiler, EORI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
697 if (flags & ARG1_IMM)
701 if (!(flags & SET_FLAGS))
702 return push_inst16(compiler, MOV | SET_REGS44(dst, reg));
703 if (IS_2_LO_REGS(dst, reg))
704 return push_inst16(compiler, MOVS | RD3(dst) | RN3(reg));
705 return push_inst32(compiler, MOV_W | SET_FLAGS | RD4(dst) | RM4(reg));
707 switch (flags & 0xffff) {
709 if (IS_2_LO_REGS(dst, reg))
710 return push_inst16(compiler, LSLSI | RD3(dst) | RN3(reg) | (imm << 6));
711 return push_inst32(compiler, LSL_WI | (flags & SET_FLAGS) | RD4(dst) | RM4(reg) | IMM5(imm));
713 if (IS_2_LO_REGS(dst, reg))
714 return push_inst16(compiler, LSRSI | RD3(dst) | RN3(reg) | (imm << 6));
715 return push_inst32(compiler, LSR_WI | (flags & SET_FLAGS) | RD4(dst) | RM4(reg) | IMM5(imm));
716 default: /* SLJIT_ASHR */
717 if (IS_2_LO_REGS(dst, reg))
718 return push_inst16(compiler, ASRSI | RD3(dst) | RN3(reg) | (imm << 6));
719 return push_inst32(compiler, ASR_WI | (flags & SET_FLAGS) | RD4(dst) | RM4(reg) | IMM5(imm));
726 if (flags & ARG2_IMM) {
728 arg2 = (arg1 == TMP_REG1) ? TMP_REG2 : TMP_REG1;
729 FAIL_IF(load_immediate(compiler, arg2, imm));
733 arg1 = (arg2 == TMP_REG1) ? TMP_REG2 : TMP_REG1;
734 FAIL_IF(load_immediate(compiler, arg1, imm));
737 SLJIT_ASSERT(arg1 != arg2);
740 /* Both arguments are registers. */
741 switch (flags & 0xffff) {
746 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG2);
748 return SLJIT_SUCCESS;
749 return push_inst16(compiler, MOV | SET_REGS44(dst, arg2));
751 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG2);
752 if (IS_2_LO_REGS(dst, arg2))
753 return push_inst16(compiler, UXTB | RD3(dst) | RN3(arg2));
754 return push_inst32(compiler, UXTB_W | RD4(dst) | RM4(arg2));
756 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG2);
757 if (IS_2_LO_REGS(dst, arg2))
758 return push_inst16(compiler, SXTB | RD3(dst) | RN3(arg2));
759 return push_inst32(compiler, SXTB_W | RD4(dst) | RM4(arg2));
761 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG2);
762 if (IS_2_LO_REGS(dst, arg2))
763 return push_inst16(compiler, UXTH | RD3(dst) | RN3(arg2));
764 return push_inst32(compiler, UXTH_W | RD4(dst) | RM4(arg2));
766 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG2);
767 if (IS_2_LO_REGS(dst, arg2))
768 return push_inst16(compiler, SXTH | RD3(dst) | RN3(arg2));
769 return push_inst32(compiler, SXTH_W | RD4(dst) | RM4(arg2));
771 SLJIT_ASSERT(arg1 == TMP_REG2);
772 if (IS_2_LO_REGS(dst, arg2))
773 return push_inst16(compiler, MVNS | RD3(dst) | RN3(arg2));
774 return push_inst32(compiler, MVN_W | (flags & SET_FLAGS) | RD4(dst) | RM4(arg2));
776 SLJIT_ASSERT(arg1 == TMP_REG2);
777 FAIL_IF(push_inst32(compiler, CLZ | RN4(arg2) | RD4(dst) | RM4(arg2)));
778 return SLJIT_SUCCESS;
780 if (IS_3_LO_REGS(dst, arg1, arg2))
781 return push_inst16(compiler, ADDS | RD3(dst) | RN3(arg1) | RM3(arg2));
782 if (dst == arg1 && !(flags & SET_FLAGS))
783 return push_inst16(compiler, ADD | SET_REGS44(dst, arg2));
784 return push_inst32(compiler, ADD_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
786 if (dst == arg1 && IS_2_LO_REGS(dst, arg2))
787 return push_inst16(compiler, ADCS | RD3(dst) | RN3(arg2));
788 return push_inst32(compiler, ADC_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
790 if (flags & UNUSED_RETURN) {
791 if (IS_2_LO_REGS(arg1, arg2))
792 return push_inst16(compiler, CMP | RD3(arg1) | RN3(arg2));
793 return push_inst16(compiler, CMP_X | SET_REGS44(arg1, arg2));
795 if (IS_3_LO_REGS(dst, arg1, arg2))
796 return push_inst16(compiler, SUBS | RD3(dst) | RN3(arg1) | RM3(arg2));
797 return push_inst32(compiler, SUB_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
799 if (dst == arg1 && IS_2_LO_REGS(dst, arg2))
800 return push_inst16(compiler, SBCS | RD3(dst) | RN3(arg2));
801 return push_inst32(compiler, SBC_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
803 if (!(flags & SET_FLAGS))
804 return push_inst32(compiler, MUL | RD4(dst) | RN4(arg1) | RM4(arg2));
805 SLJIT_ASSERT(dst != TMP_REG2);
806 FAIL_IF(push_inst32(compiler, SMULL | RT4(dst) | RD4(TMP_REG2) | RN4(arg1) | RM4(arg2)));
807 /* cmp TMP_REG2, dst asr #31. */
808 return push_inst32(compiler, CMP_W | RN4(TMP_REG2) | 0x70e0 | RM4(dst));
810 if (dst == arg1 && IS_2_LO_REGS(dst, arg2))
811 return push_inst16(compiler, ANDS | RD3(dst) | RN3(arg2));
812 if ((flags & UNUSED_RETURN) && IS_2_LO_REGS(arg1, arg2))
813 return push_inst16(compiler, TST | RD3(arg1) | RN3(arg2));
814 return push_inst32(compiler, AND_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
816 if (dst == arg1 && IS_2_LO_REGS(dst, arg2))
817 return push_inst16(compiler, ORRS | RD3(dst) | RN3(arg2));
818 return push_inst32(compiler, ORR_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
820 if (dst == arg1 && IS_2_LO_REGS(dst, arg2))
821 return push_inst16(compiler, EORS | RD3(dst) | RN3(arg2));
822 return push_inst32(compiler, EOR_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
824 if (dst == arg1 && IS_2_LO_REGS(dst, arg2))
825 return push_inst16(compiler, LSLS | RD3(dst) | RN3(arg2));
826 return push_inst32(compiler, LSL_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
828 if (dst == arg1 && IS_2_LO_REGS(dst, arg2))
829 return push_inst16(compiler, LSRS | RD3(dst) | RN3(arg2));
830 return push_inst32(compiler, LSR_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
832 if (dst == arg1 && IS_2_LO_REGS(dst, arg2))
833 return push_inst16(compiler, ASRS | RD3(dst) | RN3(arg2));
834 return push_inst32(compiler, ASR_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
838 return SLJIT_SUCCESS;
844 #define WORD_SIZE 0x00
845 #define BYTE_SIZE 0x04
846 #define HALF_SIZE 0x08
849 #define IS_WORD_SIZE(flags) (!(flags & (BYTE_SIZE | HALF_SIZE)))
850 #define OFFSET_CHECK(imm, shift) (!(argw & ~(imm << shift)))
867 static const sljit_ins sljit_mem16[12] = {
868 /* w u l */ 0x5800 /* ldr */,
869 /* w u s */ 0x5000 /* str */,
870 /* w s l */ 0x5800 /* ldr */,
871 /* w s s */ 0x5000 /* str */,
873 /* b u l */ 0x5c00 /* ldrb */,
874 /* b u s */ 0x5400 /* strb */,
875 /* b s l */ 0x5600 /* ldrsb */,
876 /* b s s */ 0x5400 /* strb */,
878 /* h u l */ 0x5a00 /* ldrh */,
879 /* h u s */ 0x5200 /* strh */,
880 /* h s l */ 0x5e00 /* ldrsh */,
881 /* h s s */ 0x5200 /* strh */,
884 static const sljit_ins sljit_mem16_imm5[12] = {
885 /* w u l */ 0x6800 /* ldr imm5 */,
886 /* w u s */ 0x6000 /* str imm5 */,
887 /* w s l */ 0x6800 /* ldr imm5 */,
888 /* w s s */ 0x6000 /* str imm5 */,
890 /* b u l */ 0x7800 /* ldrb imm5 */,
891 /* b u s */ 0x7000 /* strb imm5 */,
892 /* b s l */ 0x0000 /* not allowed */,
893 /* b s s */ 0x7000 /* strb imm5 */,
895 /* h u l */ 0x8800 /* ldrh imm5 */,
896 /* h u s */ 0x8000 /* strh imm5 */,
897 /* h s l */ 0x0000 /* not allowed */,
898 /* h s s */ 0x8000 /* strh imm5 */,
901 #define MEM_IMM8 0xc00
902 #define MEM_IMM12 0x800000
903 static const sljit_ins sljit_mem32[13] = {
904 /* w u l */ 0xf8500000 /* ldr.w */,
905 /* w u s */ 0xf8400000 /* str.w */,
906 /* w s l */ 0xf8500000 /* ldr.w */,
907 /* w s s */ 0xf8400000 /* str.w */,
909 /* b u l */ 0xf8100000 /* ldrb.w */,
910 /* b u s */ 0xf8000000 /* strb.w */,
911 /* b s l */ 0xf9100000 /* ldrsb.w */,
912 /* b s s */ 0xf8000000 /* strb.w */,
914 /* h u l */ 0xf8300000 /* ldrh.w */,
915 /* h u s */ 0xf8200000 /* strsh.w */,
916 /* h s l */ 0xf9300000 /* ldrsh.w */,
917 /* h s s */ 0xf8200000 /* strsh.w */,
919 /* p u l */ 0xf8100000 /* pld */,
922 /* Helper function. Dst should be reg + value, using at most 1 instruction, flags does not set. */
923 static sljit_s32 emit_set_delta(struct sljit_compiler *compiler, sljit_s32 dst, sljit_s32 reg, sljit_sw value)
927 return push_inst32(compiler, ADDWI | RD4(dst) | RN4(reg) | IMM12(value));
928 value = get_imm(value);
929 if (value != INVALID_IMM)
930 return push_inst32(compiler, ADD_WI | RD4(dst) | RN4(reg) | value);
935 return push_inst32(compiler, SUBWI | RD4(dst) | RN4(reg) | IMM12(value));
936 value = get_imm(value);
937 if (value != INVALID_IMM)
938 return push_inst32(compiler, SUB_WI | RD4(dst) | RN4(reg) | value);
940 return SLJIT_ERR_UNSUPPORTED;
943 static SLJIT_INLINE sljit_s32 emit_op_mem(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg,
944 sljit_s32 arg, sljit_sw argw, sljit_s32 tmp_reg)
949 SLJIT_ASSERT(arg & SLJIT_MEM);
950 SLJIT_ASSERT((arg & REG_MASK) != tmp_reg);
953 if (SLJIT_UNLIKELY(!(arg & REG_MASK))) {
954 tmp = get_imm(argw & ~0xfff);
955 if (tmp != INVALID_IMM) {
956 FAIL_IF(push_inst32(compiler, MOV_WI | RD4(tmp_reg) | tmp));
957 return push_inst32(compiler, sljit_mem32[flags] | MEM_IMM12 | RT4(reg) | RN4(tmp_reg) | (argw & 0xfff));
960 FAIL_IF(load_immediate(compiler, tmp_reg, argw));
961 if (IS_2_LO_REGS(reg, tmp_reg) && sljit_mem16_imm5[flags])
962 return push_inst16(compiler, sljit_mem16_imm5[flags] | RD3(reg) | RN3(tmp_reg));
963 return push_inst32(compiler, sljit_mem32[flags] | MEM_IMM12 | RT4(reg) | RN4(tmp_reg));
966 if (SLJIT_UNLIKELY(arg & OFFS_REG_MASK)) {
968 other_r = OFFS_REG(arg);
971 if (!argw && IS_3_LO_REGS(reg, arg, other_r))
972 return push_inst16(compiler, sljit_mem16[flags] | RD3(reg) | RN3(arg) | RM3(other_r));
973 return push_inst32(compiler, sljit_mem32[flags] | RT4(reg) | RN4(arg) | RM4(other_r) | (argw << 4));
977 tmp = get_imm(argw & ~0xfff);
978 if (tmp != INVALID_IMM) {
979 push_inst32(compiler, ADD_WI | RD4(tmp_reg) | RN4(arg) | tmp);
984 else if (argw < -0xff) {
985 tmp = get_imm(-argw & ~0xff);
986 if (tmp != INVALID_IMM) {
987 push_inst32(compiler, SUB_WI | RD4(tmp_reg) | RN4(arg) | tmp);
989 argw = -(-argw & 0xff);
993 if (IS_2_LO_REGS(reg, arg) && sljit_mem16_imm5[flags]) {
995 if (IS_WORD_SIZE(flags)) {
996 if (OFFSET_CHECK(0x1f, 2))
999 else if (flags & BYTE_SIZE)
1001 if (OFFSET_CHECK(0x1f, 0))
1005 SLJIT_ASSERT(flags & HALF_SIZE);
1006 if (OFFSET_CHECK(0x1f, 1))
1011 return push_inst16(compiler, sljit_mem16_imm5[flags] | RD3(reg) | RN3(arg) | (argw << (6 - tmp)));
1013 else if (SLJIT_UNLIKELY(arg == SLJIT_SP) && IS_WORD_SIZE(flags) && OFFSET_CHECK(0xff, 2) && reg_map[reg] <= 7) {
1014 /* SP based immediate. */
1015 return push_inst16(compiler, STR_SP | ((flags & STORE) ? 0 : 0x800) | RDN3(reg) | (argw >> 2));
1018 if (argw >= 0 && argw <= 0xfff)
1019 return push_inst32(compiler, sljit_mem32[flags] | MEM_IMM12 | RT4(reg) | RN4(arg) | argw);
1020 else if (argw < 0 && argw >= -0xff)
1021 return push_inst32(compiler, sljit_mem32[flags] | MEM_IMM8 | RT4(reg) | RN4(arg) | -argw);
1023 SLJIT_ASSERT(arg != tmp_reg);
1025 FAIL_IF(load_immediate(compiler, tmp_reg, argw));
1026 if (IS_3_LO_REGS(reg, arg, tmp_reg))
1027 return push_inst16(compiler, sljit_mem16[flags] | RD3(reg) | RN3(arg) | RM3(tmp_reg));
1028 return push_inst32(compiler, sljit_mem32[flags] | RT4(reg) | RN4(arg) | RM4(tmp_reg));
1031 /* --------------------------------------------------------------------- */
1033 /* --------------------------------------------------------------------- */
1035 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_enter(struct sljit_compiler *compiler,
1036 sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
1037 sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
1039 sljit_s32 args, size, i, tmp;
1046 CHECK(check_sljit_emit_enter(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size));
1047 set_emit_enter(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size);
1049 tmp = saveds < SLJIT_NUMBER_OF_SAVED_REGISTERS ? (SLJIT_S0 + 1 - saveds) : SLJIT_FIRST_SAVED_REG;
1050 for (i = SLJIT_S0; i >= tmp; i--)
1051 push |= 1 << reg_map[i];
1053 for (i = scratches; i >= SLJIT_FIRST_SAVED_REG; i--)
1054 push |= 1 << reg_map[i];
1056 FAIL_IF((push & 0xff00)
1057 ? push_inst32(compiler, PUSH_W | (1 << 14) | push)
1058 : push_inst16(compiler, PUSH | (1 << 8) | push));
1060 /* Stack must be aligned to 8 bytes: (LR, R4) */
1061 size = GET_SAVED_REGISTERS_SIZE(scratches, saveds, 1);
1062 local_size = ((size + local_size + 7) & ~7) - size;
1063 compiler->local_size = local_size;
1066 if (local_size >= 256) {
1067 if (local_size > 4096)
1068 imm = get_imm(4096);
1070 imm = get_imm(local_size & ~0xff);
1072 SLJIT_ASSERT(imm != INVALID_IMM);
1073 FAIL_IF(push_inst32(compiler, SUB_WI | RD4(TMP_REG1) | RN4(SLJIT_SP) | imm));
1076 if (local_size > 0) {
1077 if (local_size <= (127 << 2))
1078 FAIL_IF(push_inst16(compiler, SUB_SP | (local_size >> 2)));
1080 FAIL_IF(emit_op_imm(compiler, SLJIT_SUB | ARG2_IMM, SLJIT_SP, SLJIT_SP, local_size));
1084 args = get_arg_count(arg_types);
1087 FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(SLJIT_S0, SLJIT_R0)));
1089 FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(SLJIT_S1, SLJIT_R1)));
1091 FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(SLJIT_S2, SLJIT_R2)));
1094 if (local_size >= 256) {
1095 if (local_size > 4096) {
1096 imm = get_imm(4096);
1097 SLJIT_ASSERT(imm != INVALID_IMM);
1099 if (local_size < 4 * 4096) {
1100 if (local_size > 2 * 4096) {
1101 FAIL_IF(push_inst32(compiler, LDRI | 0x400 | RT4(TMP_REG2) | RN4(TMP_REG1)));
1102 FAIL_IF(push_inst32(compiler, SUB_WI | RD4(TMP_REG1) | RN4(TMP_REG1) | imm));
1106 if (local_size > 2 * 4096) {
1107 FAIL_IF(push_inst32(compiler, LDRI | 0x400 | RT4(TMP_REG2) | RN4(TMP_REG1)));
1108 FAIL_IF(push_inst32(compiler, SUB_WI | RD4(TMP_REG1) | RN4(TMP_REG1) | imm));
1112 FAIL_IF(push_inst32(compiler, LDRI | 0x400 | RT4(TMP_REG2) | RN4(TMP_REG1)));
1115 SLJIT_ASSERT(local_size > 0);
1118 FAIL_IF(load_immediate(compiler, SLJIT_R3, (local_size >> 12) - 1));
1119 FAIL_IF(push_inst32(compiler, LDRI | 0x400 | RT4(TMP_REG2) | RN4(TMP_REG1)));
1120 FAIL_IF(push_inst32(compiler, SUB_WI | RD4(TMP_REG1) | RN4(TMP_REG1) | imm));
1121 SLJIT_ASSERT(reg_map[SLJIT_R3] < 7);
1122 FAIL_IF(push_inst16(compiler, SUBSI8 | RDN3(SLJIT_R3) | 1));
1123 FAIL_IF(push_inst16(compiler, BCC | (0x1 << 8) /* not-equal */ | (-7 & 0xff)));
1125 local_size &= 0xfff;
1127 if (local_size != 0)
1128 FAIL_IF(push_inst32(compiler, LDRI | 0x400 | RT4(TMP_REG2) | RN4(TMP_REG1)));
1131 if (local_size >= 256) {
1132 imm = get_imm(local_size & ~0xff);
1133 SLJIT_ASSERT(imm != INVALID_IMM);
1135 FAIL_IF(push_inst32(compiler, SUB_WI | RD4(TMP_REG1) | RN4(TMP_REG1) | imm));
1140 FAIL_IF(push_inst32(compiler, LDRI | 0x400 | (local_size > 0 ? 0x100 : 0) | RT4(TMP_REG2) | RN4(TMP_REG1) | local_size));
1142 FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(SLJIT_SP, TMP_REG1)));
1144 else if (local_size > 0)
1145 FAIL_IF(push_inst32(compiler, LDRI | 0x500 | RT4(TMP_REG1) | RN4(SLJIT_SP) | local_size));
1148 return SLJIT_SUCCESS;
1151 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_set_context(struct sljit_compiler *compiler,
1152 sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
1153 sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
1158 CHECK(check_sljit_set_context(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size));
1159 set_set_context(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size);
1161 size = GET_SAVED_REGISTERS_SIZE(scratches, saveds, 1);
1162 compiler->local_size = ((size + local_size + 7) & ~7) - size;
1163 return SLJIT_SUCCESS;
1166 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_return(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 src, sljit_sw srcw)
1172 CHECK(check_sljit_emit_return(compiler, op, src, srcw));
1174 FAIL_IF(emit_mov_before_return(compiler, op, src, srcw));
1176 if (compiler->local_size > 0) {
1177 if (compiler->local_size <= (127 << 2))
1178 FAIL_IF(push_inst16(compiler, ADD_SP | (compiler->local_size >> 2)));
1180 FAIL_IF(emit_op_imm(compiler, SLJIT_ADD | ARG2_IMM, SLJIT_SP, SLJIT_SP, compiler->local_size));
1183 tmp = compiler->saveds < SLJIT_NUMBER_OF_SAVED_REGISTERS ? (SLJIT_S0 + 1 - compiler->saveds) : SLJIT_FIRST_SAVED_REG;
1184 for (i = SLJIT_S0; i >= tmp; i--)
1185 pop |= 1 << reg_map[i];
1187 for (i = compiler->scratches; i >= SLJIT_FIRST_SAVED_REG; i--)
1188 pop |= 1 << reg_map[i];
1190 return (pop & 0xff00)
1191 ? push_inst32(compiler, POP_W | (1 << 15) | pop)
1192 : push_inst16(compiler, POP | (1 << 8) | pop);
1195 /* --------------------------------------------------------------------- */
1197 /* --------------------------------------------------------------------- */
1199 #if !(defined __ARM_FEATURE_IDIV) && !(defined __ARM_ARCH_EXT_IDIV__)
1206 extern unsigned long long __rt_udiv(unsigned int denominator, unsigned int numerator);
1207 extern long long __rt_sdiv(int denominator, int numerator);
1208 #elif defined(__GNUC__)
1209 extern unsigned int __aeabi_uidivmod(unsigned int numerator, int unsigned denominator);
1210 extern int __aeabi_idivmod(int numerator, int denominator);
1212 #error "Software divmod functions are needed"
1219 #endif /* !__ARM_FEATURE_IDIV && !__ARM_ARCH_EXT_IDIV__ */
1221 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op0(struct sljit_compiler *compiler, sljit_s32 op)
1223 #if !(defined __ARM_FEATURE_IDIV) && !(defined __ARM_ARCH_EXT_IDIV__)
1224 sljit_sw saved_reg_list[3];
1225 sljit_sw saved_reg_count;
1229 CHECK(check_sljit_emit_op0(compiler, op));
1231 op = GET_OPCODE(op);
1233 case SLJIT_BREAKPOINT:
1234 return push_inst16(compiler, BKPT);
1236 return push_inst16(compiler, NOP);
1239 return push_inst32(compiler, (op == SLJIT_LMUL_UW ? UMULL : SMULL)
1240 | (reg_map[SLJIT_R1] << 8)
1241 | (reg_map[SLJIT_R0] << 12)
1242 | (reg_map[SLJIT_R0] << 16)
1243 | reg_map[SLJIT_R1]);
1244 #if (defined __ARM_FEATURE_IDIV) || (defined __ARM_ARCH_EXT_IDIV__)
1245 case SLJIT_DIVMOD_UW:
1246 case SLJIT_DIVMOD_SW:
1247 FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(TMP_REG1, SLJIT_R0)));
1248 FAIL_IF(push_inst32(compiler, (op == SLJIT_DIVMOD_UW ? UDIV : SDIV) | RD4(SLJIT_R0) | RN4(SLJIT_R0) | RM4(SLJIT_R1)));
1249 FAIL_IF(push_inst32(compiler, MUL | RD4(SLJIT_R1) | RN4(SLJIT_R0) | RM4(SLJIT_R1)));
1250 return push_inst32(compiler, SUB_W | RD4(SLJIT_R1) | RN4(TMP_REG1) | RM4(SLJIT_R1));
1253 return push_inst32(compiler, (op == SLJIT_DIV_UW ? UDIV : SDIV) | RD4(SLJIT_R0) | RN4(SLJIT_R0) | RM4(SLJIT_R1));
1254 #else /* !__ARM_FEATURE_IDIV && !__ARM_ARCH_EXT_IDIV__ */
1255 case SLJIT_DIVMOD_UW:
1256 case SLJIT_DIVMOD_SW:
1259 SLJIT_COMPILE_ASSERT((SLJIT_DIVMOD_UW & 0x2) == 0 && SLJIT_DIV_UW - 0x2 == SLJIT_DIVMOD_UW, bad_div_opcode_assignments);
1260 SLJIT_ASSERT(reg_map[2] == 1 && reg_map[3] == 2 && reg_map[4] == 3);
1262 saved_reg_count = 0;
1263 if (compiler->scratches >= 4)
1264 saved_reg_list[saved_reg_count++] = 3;
1265 if (compiler->scratches >= 3)
1266 saved_reg_list[saved_reg_count++] = 2;
1267 if (op >= SLJIT_DIV_UW)
1268 saved_reg_list[saved_reg_count++] = 1;
1270 if (saved_reg_count > 0) {
1271 FAIL_IF(push_inst32(compiler, 0xf84d0d00 | (saved_reg_count >= 3 ? 16 : 8)
1272 | (saved_reg_list[0] << 12) /* str rX, [sp, #-8/-16]! */));
1273 if (saved_reg_count >= 2) {
1274 SLJIT_ASSERT(saved_reg_list[1] < 8);
1275 FAIL_IF(push_inst16(compiler, 0x9001 | (saved_reg_list[1] << 8) /* str rX, [sp, #4] */));
1277 if (saved_reg_count >= 3) {
1278 SLJIT_ASSERT(saved_reg_list[2] < 8);
1279 FAIL_IF(push_inst16(compiler, 0x9002 | (saved_reg_list[2] << 8) /* str rX, [sp, #8] */));
1284 FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(TMP_REG1, SLJIT_R0)));
1285 FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(SLJIT_R0, SLJIT_R1)));
1286 FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(SLJIT_R1, TMP_REG1)));
1287 FAIL_IF(sljit_emit_ijump(compiler, SLJIT_FAST_CALL, SLJIT_IMM,
1288 ((op | 0x2) == SLJIT_DIV_UW ? SLJIT_FUNC_OFFSET(__rt_udiv) : SLJIT_FUNC_OFFSET(__rt_sdiv))));
1289 #elif defined(__GNUC__)
1290 FAIL_IF(sljit_emit_ijump(compiler, SLJIT_FAST_CALL, SLJIT_IMM,
1291 ((op | 0x2) == SLJIT_DIV_UW ? SLJIT_FUNC_OFFSET(__aeabi_uidivmod) : SLJIT_FUNC_OFFSET(__aeabi_idivmod))));
1293 #error "Software divmod functions are needed"
1296 if (saved_reg_count > 0) {
1297 if (saved_reg_count >= 3) {
1298 SLJIT_ASSERT(saved_reg_list[2] < 8);
1299 FAIL_IF(push_inst16(compiler, 0x9802 | (saved_reg_list[2] << 8) /* ldr rX, [sp, #8] */));
1301 if (saved_reg_count >= 2) {
1302 SLJIT_ASSERT(saved_reg_list[1] < 8);
1303 FAIL_IF(push_inst16(compiler, 0x9801 | (saved_reg_list[1] << 8) /* ldr rX, [sp, #4] */));
1305 return push_inst32(compiler, 0xf85d0b00 | (saved_reg_count >= 3 ? 16 : 8)
1306 | (saved_reg_list[0] << 12) /* ldr rX, [sp], #8/16 */);
1308 return SLJIT_SUCCESS;
1309 #endif /* __ARM_FEATURE_IDIV || __ARM_ARCH_EXT_IDIV__ */
1312 return SLJIT_SUCCESS;
1315 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op1(struct sljit_compiler *compiler, sljit_s32 op,
1316 sljit_s32 dst, sljit_sw dstw,
1317 sljit_s32 src, sljit_sw srcw)
1319 sljit_s32 dst_r, flags;
1320 sljit_s32 op_flags = GET_ALL_FLAGS(op);
1323 CHECK(check_sljit_emit_op1(compiler, op, dst, dstw, src, srcw));
1324 ADJUST_LOCAL_OFFSET(dst, dstw);
1325 ADJUST_LOCAL_OFFSET(src, srcw);
1327 if (dst == SLJIT_UNUSED && !HAS_FLAGS(op)) {
1328 /* Since TMP_PC has index 15, IS_2_LO_REGS and IS_3_LO_REGS checks always fail. */
1329 if (op <= SLJIT_MOV_P && (src & SLJIT_MEM))
1330 return emit_op_mem(compiler, PRELOAD, TMP_PC, src, srcw, TMP_REG1);
1331 return SLJIT_SUCCESS;
1334 dst_r = SLOW_IS_REG(dst) ? dst : TMP_REG1;
1336 op = GET_OPCODE(op);
1337 if (op >= SLJIT_MOV && op <= SLJIT_MOV_P) {
1347 if (src & SLJIT_IMM)
1348 srcw = (sljit_u8)srcw;
1351 flags = BYTE_SIZE | SIGNED;
1352 if (src & SLJIT_IMM)
1353 srcw = (sljit_s8)srcw;
1357 if (src & SLJIT_IMM)
1358 srcw = (sljit_u16)srcw;
1361 flags = HALF_SIZE | SIGNED;
1362 if (src & SLJIT_IMM)
1363 srcw = (sljit_s16)srcw;
1366 SLJIT_UNREACHABLE();
1371 if (src & SLJIT_IMM)
1372 FAIL_IF(emit_op_imm(compiler, SLJIT_MOV | ARG2_IMM, dst_r, TMP_REG2, srcw));
1373 else if (src & SLJIT_MEM) {
1374 FAIL_IF(emit_op_mem(compiler, flags, dst_r, src, srcw, TMP_REG1));
1376 if (dst_r != TMP_REG1)
1377 return emit_op_imm(compiler, op, dst_r, TMP_REG2, src);
1381 if (!(dst & SLJIT_MEM))
1382 return SLJIT_SUCCESS;
1384 return emit_op_mem(compiler, flags | STORE, dst_r, dst, dstw, TMP_REG2);
1387 if (op == SLJIT_NEG) {
1388 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
1389 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
1390 compiler->skip_checks = 1;
1392 return sljit_emit_op2(compiler, SLJIT_SUB | op_flags, dst, dstw, SLJIT_IMM, 0, src, srcw);
1395 flags = HAS_FLAGS(op_flags) ? SET_FLAGS : 0;
1397 if (src & SLJIT_MEM) {
1398 FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG1, src, srcw, TMP_REG1));
1402 emit_op_imm(compiler, flags | op, dst_r, TMP_REG2, src);
1404 if (SLJIT_UNLIKELY(dst & SLJIT_MEM))
1405 return emit_op_mem(compiler, flags | STORE, dst_r, dst, dstw, TMP_REG2);
1406 return SLJIT_SUCCESS;
1409 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op2(struct sljit_compiler *compiler, sljit_s32 op,
1410 sljit_s32 dst, sljit_sw dstw,
1411 sljit_s32 src1, sljit_sw src1w,
1412 sljit_s32 src2, sljit_sw src2w)
1414 sljit_s32 dst_reg, flags, src2_reg;
1417 CHECK(check_sljit_emit_op2(compiler, op, dst, dstw, src1, src1w, src2, src2w));
1418 ADJUST_LOCAL_OFFSET(dst, dstw);
1419 ADJUST_LOCAL_OFFSET(src1, src1w);
1420 ADJUST_LOCAL_OFFSET(src2, src2w);
1422 if (dst == SLJIT_UNUSED && !HAS_FLAGS(op))
1423 return SLJIT_SUCCESS;
1425 dst_reg = SLOW_IS_REG(dst) ? dst : TMP_REG1;
1426 flags = HAS_FLAGS(op) ? SET_FLAGS : 0;
1428 if (src1 & SLJIT_IMM)
1430 else if (src1 & SLJIT_MEM) {
1431 emit_op_mem(compiler, WORD_SIZE, TMP_REG1, src1, src1w, TMP_REG1);
1437 if (src2 & SLJIT_IMM)
1439 else if (src2 & SLJIT_MEM) {
1440 src2_reg = (!(flags & ARG1_IMM) && (src1w == TMP_REG1)) ? TMP_REG2 : TMP_REG1;
1441 emit_op_mem(compiler, WORD_SIZE, src2_reg, src2, src2w, src2_reg);
1447 if (dst == SLJIT_UNUSED)
1448 flags |= UNUSED_RETURN;
1450 emit_op_imm(compiler, flags | GET_OPCODE(op), dst_reg, src1w, src2w);
1452 if (!(dst & SLJIT_MEM))
1453 return SLJIT_SUCCESS;
1454 return emit_op_mem(compiler, WORD_SIZE | STORE, dst_reg, dst, dstw, TMP_REG2);
1457 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_register_index(sljit_s32 reg)
1459 CHECK_REG_INDEX(check_sljit_get_register_index(reg));
1460 return reg_map[reg];
1463 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_float_register_index(sljit_s32 reg)
1465 CHECK_REG_INDEX(check_sljit_get_float_register_index(reg));
1466 return (freg_map[reg] << 1);
1469 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_custom(struct sljit_compiler *compiler,
1470 void *instruction, sljit_s32 size)
1473 CHECK(check_sljit_emit_op_custom(compiler, instruction, size));
1476 return push_inst16(compiler, *(sljit_u16*)instruction);
1477 return push_inst32(compiler, *(sljit_ins*)instruction);
1480 /* --------------------------------------------------------------------- */
1481 /* Floating point operators */
1482 /* --------------------------------------------------------------------- */
1484 #define FPU_LOAD (1 << 20)
1486 static sljit_s32 emit_fop_mem(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg, sljit_s32 arg, sljit_sw argw)
1489 sljit_sw inst = VSTR_F32 | (flags & (SLJIT_F32_OP | FPU_LOAD));
1491 SLJIT_ASSERT(arg & SLJIT_MEM);
1493 /* Fast loads and stores. */
1494 if (SLJIT_UNLIKELY(arg & OFFS_REG_MASK)) {
1495 FAIL_IF(push_inst32(compiler, ADD_W | RD4(TMP_REG1) | RN4(arg & REG_MASK) | RM4(OFFS_REG(arg)) | ((argw & 0x3) << 6)));
1496 arg = SLJIT_MEM | TMP_REG1;
1500 if ((arg & REG_MASK) && (argw & 0x3) == 0) {
1501 if (!(argw & ~0x3fc))
1502 return push_inst32(compiler, inst | 0x800000 | RN4(arg & REG_MASK) | DD4(reg) | (argw >> 2));
1503 if (!(-argw & ~0x3fc))
1504 return push_inst32(compiler, inst | RN4(arg & REG_MASK) | DD4(reg) | (-argw >> 2));
1507 if (arg & REG_MASK) {
1508 if (emit_set_delta(compiler, TMP_REG1, arg & REG_MASK, argw) != SLJIT_ERR_UNSUPPORTED) {
1509 FAIL_IF(compiler->error);
1510 return push_inst32(compiler, inst | 0x800000 | RN4(TMP_REG1) | DD4(reg));
1512 imm = get_imm(argw & ~0x3fc);
1513 if (imm != INVALID_IMM) {
1514 FAIL_IF(push_inst32(compiler, ADD_WI | RD4(TMP_REG1) | RN4(arg & REG_MASK) | imm));
1515 return push_inst32(compiler, inst | 0x800000 | RN4(TMP_REG1) | DD4(reg) | ((argw & 0x3fc) >> 2));
1517 imm = get_imm(-argw & ~0x3fc);
1518 if (imm != INVALID_IMM) {
1520 FAIL_IF(push_inst32(compiler, SUB_WI | RD4(TMP_REG1) | RN4(arg & REG_MASK) | imm));
1521 return push_inst32(compiler, inst | RN4(TMP_REG1) | DD4(reg) | ((argw & 0x3fc) >> 2));
1525 FAIL_IF(load_immediate(compiler, TMP_REG1, argw));
1527 FAIL_IF(push_inst16(compiler, ADD | SET_REGS44(TMP_REG1, (arg & REG_MASK))));
1528 return push_inst32(compiler, inst | 0x800000 | RN4(TMP_REG1) | DD4(reg));
1531 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_conv_sw_from_f64(struct sljit_compiler *compiler, sljit_s32 op,
1532 sljit_s32 dst, sljit_sw dstw,
1533 sljit_s32 src, sljit_sw srcw)
1537 if (src & SLJIT_MEM) {
1538 FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, TMP_FREG1, src, srcw));
1542 FAIL_IF(push_inst32(compiler, VCVT_S32_F32 | (op & SLJIT_F32_OP) | DD4(TMP_FREG1) | DM4(src)));
1544 if (FAST_IS_REG(dst))
1545 return push_inst32(compiler, VMOV | (1 << 20) | RT4(dst) | DN4(TMP_FREG1));
1547 /* Store the integer value from a VFP register. */
1548 return emit_fop_mem(compiler, 0, TMP_FREG1, dst, dstw);
1551 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_conv_f64_from_sw(struct sljit_compiler *compiler, sljit_s32 op,
1552 sljit_s32 dst, sljit_sw dstw,
1553 sljit_s32 src, sljit_sw srcw)
1555 sljit_s32 dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
1559 if (FAST_IS_REG(src))
1560 FAIL_IF(push_inst32(compiler, VMOV | RT4(src) | DN4(TMP_FREG1)));
1561 else if (src & SLJIT_MEM) {
1562 /* Load the integer value into a VFP register. */
1563 FAIL_IF(emit_fop_mem(compiler, FPU_LOAD, TMP_FREG1, src, srcw));
1566 FAIL_IF(load_immediate(compiler, TMP_REG1, srcw));
1567 FAIL_IF(push_inst32(compiler, VMOV | RT4(TMP_REG1) | DN4(TMP_FREG1)));
1570 FAIL_IF(push_inst32(compiler, VCVT_F32_S32 | (op & SLJIT_F32_OP) | DD4(dst_r) | DM4(TMP_FREG1)));
1572 if (dst & SLJIT_MEM)
1573 return emit_fop_mem(compiler, (op & SLJIT_F32_OP), TMP_FREG1, dst, dstw);
1574 return SLJIT_SUCCESS;
1577 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_cmp(struct sljit_compiler *compiler, sljit_s32 op,
1578 sljit_s32 src1, sljit_sw src1w,
1579 sljit_s32 src2, sljit_sw src2w)
1583 if (src1 & SLJIT_MEM) {
1584 emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, TMP_FREG1, src1, src1w);
1588 if (src2 & SLJIT_MEM) {
1589 emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, TMP_FREG2, src2, src2w);
1593 FAIL_IF(push_inst32(compiler, VCMP_F32 | (op & SLJIT_F32_OP) | DD4(src1) | DM4(src2)));
1594 return push_inst32(compiler, VMRS);
1597 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop1(struct sljit_compiler *compiler, sljit_s32 op,
1598 sljit_s32 dst, sljit_sw dstw,
1599 sljit_s32 src, sljit_sw srcw)
1605 SLJIT_COMPILE_ASSERT((SLJIT_F32_OP == 0x100), float_transfer_bit_error);
1606 SELECT_FOP1_OPERATION_WITH_CHECKS(compiler, op, dst, dstw, src, srcw);
1608 dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
1610 if (GET_OPCODE(op) != SLJIT_CONV_F64_FROM_F32)
1613 if (src & SLJIT_MEM) {
1614 emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, dst_r, src, srcw);
1618 switch (GET_OPCODE(op)) {
1621 if (dst_r != TMP_FREG1)
1622 FAIL_IF(push_inst32(compiler, VMOV_F32 | (op & SLJIT_F32_OP) | DD4(dst_r) | DM4(src)));
1628 FAIL_IF(push_inst32(compiler, VNEG_F32 | (op & SLJIT_F32_OP) | DD4(dst_r) | DM4(src)));
1631 FAIL_IF(push_inst32(compiler, VABS_F32 | (op & SLJIT_F32_OP) | DD4(dst_r) | DM4(src)));
1633 case SLJIT_CONV_F64_FROM_F32:
1634 FAIL_IF(push_inst32(compiler, VCVT_F64_F32 | (op & SLJIT_F32_OP) | DD4(dst_r) | DM4(src)));
1639 if (dst & SLJIT_MEM)
1640 return emit_fop_mem(compiler, (op & SLJIT_F32_OP), dst_r, dst, dstw);
1641 return SLJIT_SUCCESS;
1644 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop2(struct sljit_compiler *compiler, sljit_s32 op,
1645 sljit_s32 dst, sljit_sw dstw,
1646 sljit_s32 src1, sljit_sw src1w,
1647 sljit_s32 src2, sljit_sw src2w)
1652 CHECK(check_sljit_emit_fop2(compiler, op, dst, dstw, src1, src1w, src2, src2w));
1653 ADJUST_LOCAL_OFFSET(dst, dstw);
1654 ADJUST_LOCAL_OFFSET(src1, src1w);
1655 ADJUST_LOCAL_OFFSET(src2, src2w);
1659 dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
1660 if (src1 & SLJIT_MEM) {
1661 emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, TMP_FREG1, src1, src1w);
1664 if (src2 & SLJIT_MEM) {
1665 emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, TMP_FREG2, src2, src2w);
1669 switch (GET_OPCODE(op)) {
1671 FAIL_IF(push_inst32(compiler, VADD_F32 | (op & SLJIT_F32_OP) | DD4(dst_r) | DN4(src1) | DM4(src2)));
1674 FAIL_IF(push_inst32(compiler, VSUB_F32 | (op & SLJIT_F32_OP) | DD4(dst_r) | DN4(src1) | DM4(src2)));
1677 FAIL_IF(push_inst32(compiler, VMUL_F32 | (op & SLJIT_F32_OP) | DD4(dst_r) | DN4(src1) | DM4(src2)));
1680 FAIL_IF(push_inst32(compiler, VDIV_F32 | (op & SLJIT_F32_OP) | DD4(dst_r) | DN4(src1) | DM4(src2)));
1684 if (!(dst & SLJIT_MEM))
1685 return SLJIT_SUCCESS;
1686 return emit_fop_mem(compiler, (op & SLJIT_F32_OP), TMP_FREG1, dst, dstw);
1691 /* --------------------------------------------------------------------- */
1692 /* Other instructions */
1693 /* --------------------------------------------------------------------- */
1695 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fast_enter(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw)
1698 CHECK(check_sljit_emit_fast_enter(compiler, dst, dstw));
1699 ADJUST_LOCAL_OFFSET(dst, dstw);
1701 SLJIT_ASSERT(reg_map[TMP_REG2] == 14);
1703 if (FAST_IS_REG(dst))
1704 return push_inst16(compiler, MOV | SET_REGS44(dst, TMP_REG2));
1707 return emit_op_mem(compiler, WORD_SIZE | STORE, TMP_REG2, dst, dstw, TMP_REG1);
1710 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fast_return(struct sljit_compiler *compiler, sljit_s32 src, sljit_sw srcw)
1713 CHECK(check_sljit_emit_fast_return(compiler, src, srcw));
1714 ADJUST_LOCAL_OFFSET(src, srcw);
1716 SLJIT_ASSERT(reg_map[TMP_REG2] == 14);
1718 if (FAST_IS_REG(src))
1719 FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(TMP_REG2, src)));
1721 FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG2, src, srcw, TMP_REG2));
1723 return push_inst16(compiler, BX | RN3(TMP_REG2));
1726 /* --------------------------------------------------------------------- */
1727 /* Conditional instructions */
1728 /* --------------------------------------------------------------------- */
1730 static sljit_uw get_cc(sljit_s32 type)
1734 case SLJIT_MUL_NOT_OVERFLOW:
1735 case SLJIT_EQUAL_F64:
1738 case SLJIT_NOT_EQUAL:
1739 case SLJIT_MUL_OVERFLOW:
1740 case SLJIT_NOT_EQUAL_F64:
1744 case SLJIT_LESS_F64:
1747 case SLJIT_GREATER_EQUAL:
1748 case SLJIT_GREATER_EQUAL_F64:
1752 case SLJIT_GREATER_F64:
1755 case SLJIT_LESS_EQUAL:
1756 case SLJIT_LESS_EQUAL_F64:
1759 case SLJIT_SIG_LESS:
1762 case SLJIT_SIG_GREATER_EQUAL:
1765 case SLJIT_SIG_GREATER:
1768 case SLJIT_SIG_LESS_EQUAL:
1771 case SLJIT_OVERFLOW:
1772 case SLJIT_UNORDERED_F64:
1775 case SLJIT_NOT_OVERFLOW:
1776 case SLJIT_ORDERED_F64:
1779 default: /* SLJIT_JUMP */
1780 SLJIT_UNREACHABLE();
1785 SLJIT_API_FUNC_ATTRIBUTE struct sljit_label* sljit_emit_label(struct sljit_compiler *compiler)
1787 struct sljit_label *label;
1790 CHECK_PTR(check_sljit_emit_label(compiler));
1792 if (compiler->last_label && compiler->last_label->size == compiler->size)
1793 return compiler->last_label;
1795 label = (struct sljit_label*)ensure_abuf(compiler, sizeof(struct sljit_label));
1796 PTR_FAIL_IF(!label);
1797 set_label(label, compiler);
1801 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_jump(struct sljit_compiler *compiler, sljit_s32 type)
1803 struct sljit_jump *jump;
1807 CHECK_PTR(check_sljit_emit_jump(compiler, type));
1809 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1811 set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
1814 PTR_FAIL_IF(emit_imm32_const(compiler, TMP_REG1, 0));
1815 if (type < SLJIT_JUMP) {
1816 jump->flags |= IS_COND;
1818 jump->flags |= cc << 8;
1819 PTR_FAIL_IF(push_inst16(compiler, IT | (cc << 4) | 0x8));
1822 jump->addr = compiler->size;
1823 if (type <= SLJIT_JUMP)
1824 PTR_FAIL_IF(push_inst16(compiler, BX | RN3(TMP_REG1)));
1826 jump->flags |= IS_BL;
1827 PTR_FAIL_IF(push_inst16(compiler, BLX | RN3(TMP_REG1)));
1835 static sljit_s32 softfloat_call_with_args(struct sljit_compiler *compiler, sljit_s32 arg_types, sljit_s32 *src)
1837 sljit_s32 stack_offset = 0;
1838 sljit_s32 arg_count = 0;
1839 sljit_s32 word_arg_offset = 0;
1840 sljit_s32 float_arg_count = 0;
1841 sljit_s32 types = 0;
1842 sljit_s32 src_offset = 4 * sizeof(sljit_sw);
1843 sljit_u8 offsets[4];
1845 if (src && FAST_IS_REG(*src))
1846 src_offset = reg_map[*src] * sizeof(sljit_sw);
1848 arg_types >>= SLJIT_DEF_SHIFT;
1851 types = (types << SLJIT_DEF_SHIFT) | (arg_types & SLJIT_DEF_MASK);
1853 switch (arg_types & SLJIT_DEF_MASK) {
1854 case SLJIT_ARG_TYPE_F32:
1855 offsets[arg_count] = (sljit_u8)stack_offset;
1856 stack_offset += sizeof(sljit_f32);
1860 case SLJIT_ARG_TYPE_F64:
1861 if (stack_offset & 0x7)
1862 stack_offset += sizeof(sljit_sw);
1863 offsets[arg_count] = (sljit_u8)stack_offset;
1864 stack_offset += sizeof(sljit_f64);
1869 offsets[arg_count] = (sljit_u8)stack_offset;
1870 stack_offset += sizeof(sljit_sw);
1872 word_arg_offset += sizeof(sljit_sw);
1876 arg_types >>= SLJIT_DEF_SHIFT;
1879 if (stack_offset > 16)
1880 FAIL_IF(push_inst16(compiler, SUB_SP | (((stack_offset - 16) + 0x7) & ~0x7) >> 2));
1882 SLJIT_ASSERT(reg_map[TMP_REG1] == 12);
1884 /* Process arguments in reversed direction. */
1886 switch (types & SLJIT_DEF_MASK) {
1887 case SLJIT_ARG_TYPE_F32:
1890 stack_offset = offsets[arg_count];
1892 if (stack_offset < 16) {
1893 if (src_offset == stack_offset) {
1894 FAIL_IF(push_inst16(compiler, MOV | (src_offset << 1) | 4 | (1 << 7)));
1897 FAIL_IF(push_inst32(compiler, VMOV | 0x100000 | (float_arg_count << 16) | (stack_offset << 10)));
1899 FAIL_IF(push_inst32(compiler, VSTR_F32 | 0x800000 | RN4(SLJIT_SP) | (float_arg_count << 12) | ((stack_offset - 16) >> 2)));
1901 case SLJIT_ARG_TYPE_F64:
1904 stack_offset = offsets[arg_count];
1906 SLJIT_ASSERT((stack_offset & 0x7) == 0);
1908 if (stack_offset < 16) {
1909 if (src_offset == stack_offset || src_offset == stack_offset + sizeof(sljit_sw)) {
1910 FAIL_IF(push_inst16(compiler, MOV | (src_offset << 1) | 4 | (1 << 7)));
1913 FAIL_IF(push_inst32(compiler, VMOV2 | 0x100000 | (stack_offset << 10) | ((stack_offset + sizeof(sljit_sw)) << 14) | float_arg_count));
1915 FAIL_IF(push_inst32(compiler, VSTR_F32 | 0x800100 | RN4(SLJIT_SP) | (float_arg_count << 12) | ((stack_offset - 16) >> 2)));
1919 word_arg_offset -= sizeof(sljit_sw);
1920 stack_offset = offsets[arg_count];
1922 SLJIT_ASSERT(stack_offset >= word_arg_offset);
1924 if (stack_offset != word_arg_offset) {
1925 if (stack_offset < 16) {
1926 if (src_offset == stack_offset) {
1927 FAIL_IF(push_inst16(compiler, MOV | (src_offset << 1) | 4 | (1 << 7)));
1930 else if (src_offset == word_arg_offset) {
1931 *src = 1 + (stack_offset >> 2);
1932 src_offset = stack_offset;
1934 FAIL_IF(push_inst16(compiler, MOV | (stack_offset >> 2) | (word_arg_offset << 1)));
1936 FAIL_IF(push_inst16(compiler, STR_SP | (word_arg_offset << 6) | ((stack_offset - 16) >> 2)));
1941 types >>= SLJIT_DEF_SHIFT;
1944 return SLJIT_SUCCESS;
1947 static sljit_s32 softfloat_post_call_with_args(struct sljit_compiler *compiler, sljit_s32 arg_types)
1949 sljit_s32 stack_size = 0;
1951 if ((arg_types & SLJIT_DEF_MASK) == SLJIT_ARG_TYPE_F32)
1952 FAIL_IF(push_inst32(compiler, VMOV | (0 << 16) | (0 << 12)));
1953 if ((arg_types & SLJIT_DEF_MASK) == SLJIT_ARG_TYPE_F64)
1954 FAIL_IF(push_inst32(compiler, VMOV2 | (1 << 16) | (0 << 12) | 0));
1956 arg_types >>= SLJIT_DEF_SHIFT;
1959 switch (arg_types & SLJIT_DEF_MASK) {
1960 case SLJIT_ARG_TYPE_F32:
1961 stack_size += sizeof(sljit_f32);
1963 case SLJIT_ARG_TYPE_F64:
1964 if (stack_size & 0x7)
1965 stack_size += sizeof(sljit_sw);
1966 stack_size += sizeof(sljit_f64);
1969 stack_size += sizeof(sljit_sw);
1973 arg_types >>= SLJIT_DEF_SHIFT;
1976 if (stack_size <= 16)
1977 return SLJIT_SUCCESS;
1979 return push_inst16(compiler, ADD_SP | ((((stack_size - 16) + 0x7) & ~0x7) >> 2));
1984 static sljit_s32 hardfloat_call_with_args(struct sljit_compiler *compiler, sljit_s32 arg_types)
1986 sljit_u32 remap = 0;
1987 sljit_u32 offset = 0;
1988 sljit_u32 new_offset, mask;
1990 /* Remove return value. */
1991 arg_types >>= SLJIT_DEF_SHIFT;
1994 if ((arg_types & SLJIT_DEF_MASK) == SLJIT_ARG_TYPE_F32) {
1998 while (remap & mask) {
2004 if (offset != new_offset)
2005 FAIL_IF(push_inst32(compiler, VMOV_F32 | DD4((new_offset >> 1) + 1)
2006 | ((new_offset & 0x1) ? 0x400000 : 0) | DM4((offset >> 1) + 1)));
2010 else if ((arg_types & SLJIT_DEF_MASK) == SLJIT_ARG_TYPE_F64) {
2014 while (remap & mask) {
2020 if (offset != new_offset)
2021 FAIL_IF(push_inst32(compiler, VMOV_F32 | SLJIT_F32_OP | DD4((new_offset >> 1) + 1) | DM4((offset >> 1) + 1)));
2025 arg_types >>= SLJIT_DEF_SHIFT;
2028 return SLJIT_SUCCESS;
2033 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_call(struct sljit_compiler *compiler, sljit_s32 type,
2034 sljit_s32 arg_types)
2037 struct sljit_jump *jump;
2041 CHECK_PTR(check_sljit_emit_call(compiler, type, arg_types));
2044 PTR_FAIL_IF(softfloat_call_with_args(compiler, arg_types, NULL));
2046 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2047 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2048 compiler->skip_checks = 1;
2051 jump = sljit_emit_jump(compiler, type);
2052 PTR_FAIL_IF(jump == NULL);
2054 PTR_FAIL_IF(softfloat_post_call_with_args(compiler, arg_types));
2057 PTR_FAIL_IF(hardfloat_call_with_args(compiler, arg_types));
2059 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2060 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2061 compiler->skip_checks = 1;
2064 return sljit_emit_jump(compiler, type);
2068 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_ijump(struct sljit_compiler *compiler, sljit_s32 type, sljit_s32 src, sljit_sw srcw)
2070 struct sljit_jump *jump;
2073 CHECK(check_sljit_emit_ijump(compiler, type, src, srcw));
2074 ADJUST_LOCAL_OFFSET(src, srcw);
2076 SLJIT_ASSERT(reg_map[TMP_REG1] != 14);
2078 if (!(src & SLJIT_IMM)) {
2079 if (FAST_IS_REG(src)) {
2080 SLJIT_ASSERT(reg_map[src] != 14);
2081 return push_inst16(compiler, (type <= SLJIT_JUMP ? BX : BLX) | RN3(src));
2084 FAIL_IF(emit_op_mem(compiler, WORD_SIZE, type <= SLJIT_JUMP ? TMP_PC : TMP_REG1, src, srcw, TMP_REG1));
2085 if (type >= SLJIT_FAST_CALL)
2086 return push_inst16(compiler, BLX | RN3(TMP_REG1));
2089 /* These jumps are converted to jump/call instructions when possible. */
2090 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
2092 set_jump(jump, compiler, JUMP_ADDR | ((type >= SLJIT_FAST_CALL) ? IS_BL : 0));
2093 jump->u.target = srcw;
2095 FAIL_IF(emit_imm32_const(compiler, TMP_REG1, 0));
2096 jump->addr = compiler->size;
2097 return push_inst16(compiler, (type <= SLJIT_JUMP ? BX : BLX) | RN3(TMP_REG1));
2100 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_icall(struct sljit_compiler *compiler, sljit_s32 type,
2101 sljit_s32 arg_types,
2102 sljit_s32 src, sljit_sw srcw)
2105 CHECK(check_sljit_emit_icall(compiler, type, arg_types, src, srcw));
2108 if (src & SLJIT_MEM) {
2109 FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG1, src, srcw, TMP_REG1));
2113 FAIL_IF(softfloat_call_with_args(compiler, arg_types, &src));
2115 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2116 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2117 compiler->skip_checks = 1;
2120 FAIL_IF(sljit_emit_ijump(compiler, type, src, srcw));
2122 return softfloat_post_call_with_args(compiler, arg_types);
2123 #else /* !__SOFTFP__ */
2124 FAIL_IF(hardfloat_call_with_args(compiler, arg_types));
2126 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2127 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2128 compiler->skip_checks = 1;
2131 return sljit_emit_ijump(compiler, type, src, srcw);
2132 #endif /* __SOFTFP__ */
2135 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_flags(struct sljit_compiler *compiler, sljit_s32 op,
2136 sljit_s32 dst, sljit_sw dstw,
2139 sljit_s32 dst_r, flags = GET_ALL_FLAGS(op);
2143 CHECK(check_sljit_emit_op_flags(compiler, op, dst, dstw, type));
2144 ADJUST_LOCAL_OFFSET(dst, dstw);
2146 op = GET_OPCODE(op);
2147 cc = get_cc(type & 0xff);
2148 dst_r = FAST_IS_REG(dst) ? dst : TMP_REG1;
2150 if (op < SLJIT_ADD) {
2151 FAIL_IF(push_inst16(compiler, IT | (cc << 4) | (((cc & 0x1) ^ 0x1) << 3) | 0x4));
2152 if (reg_map[dst_r] > 7) {
2153 FAIL_IF(push_inst32(compiler, MOV_WI | RD4(dst_r) | 1));
2154 FAIL_IF(push_inst32(compiler, MOV_WI | RD4(dst_r) | 0));
2156 /* The movsi (immediate) instruction does not set flags in IT block. */
2157 FAIL_IF(push_inst16(compiler, MOVSI | RDN3(dst_r) | 1));
2158 FAIL_IF(push_inst16(compiler, MOVSI | RDN3(dst_r) | 0));
2160 if (!(dst & SLJIT_MEM))
2161 return SLJIT_SUCCESS;
2162 return emit_op_mem(compiler, WORD_SIZE | STORE, TMP_REG1, dst, dstw, TMP_REG2);
2165 if (dst & SLJIT_MEM)
2166 FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG1, dst, dstw, TMP_REG2));
2168 if (op == SLJIT_AND) {
2169 FAIL_IF(push_inst16(compiler, IT | (cc << 4) | (((cc & 0x1) ^ 0x1) << 3) | 0x4));
2170 FAIL_IF(push_inst32(compiler, ANDI | RN4(dst_r) | RD4(dst_r) | 1));
2171 FAIL_IF(push_inst32(compiler, ANDI | RN4(dst_r) | RD4(dst_r) | 0));
2174 FAIL_IF(push_inst16(compiler, IT | (cc << 4) | 0x8));
2175 FAIL_IF(push_inst32(compiler, ((op == SLJIT_OR) ? ORRI : EORI) | RN4(dst_r) | RD4(dst_r) | 1));
2178 if (dst & SLJIT_MEM)
2179 FAIL_IF(emit_op_mem(compiler, WORD_SIZE | STORE, TMP_REG1, dst, dstw, TMP_REG2));
2181 if (!(flags & SLJIT_SET_Z))
2182 return SLJIT_SUCCESS;
2184 /* The condition must always be set, even if the ORR/EORI is not executed above. */
2185 return push_inst32(compiler, MOV_W | SET_FLAGS | RD4(TMP_REG1) | RM4(dst_r));
2188 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_cmov(struct sljit_compiler *compiler, sljit_s32 type,
2190 sljit_s32 src, sljit_sw srcw)
2195 CHECK(check_sljit_emit_cmov(compiler, type, dst_reg, src, srcw));
2197 dst_reg &= ~SLJIT_I32_OP;
2199 cc = get_cc(type & 0xff);
2201 if (!(src & SLJIT_IMM)) {
2202 FAIL_IF(push_inst16(compiler, IT | (cc << 4) | 0x8));
2203 return push_inst16(compiler, MOV | SET_REGS44(dst_reg, src));
2206 tmp = (sljit_uw) srcw;
2208 if (tmp < 0x10000) {
2209 /* set low 16 bits, set hi 16 bits to 0. */
2210 FAIL_IF(push_inst16(compiler, IT | (cc << 4) | 0x8));
2211 return push_inst32(compiler, MOVW | RD4(dst_reg)
2212 | COPY_BITS(tmp, 12, 16, 4) | COPY_BITS(tmp, 11, 26, 1) | COPY_BITS(tmp, 8, 12, 3) | (tmp & 0xff));
2215 tmp = get_imm(srcw);
2216 if (tmp != INVALID_IMM) {
2217 FAIL_IF(push_inst16(compiler, IT | (cc << 4) | 0x8));
2218 return push_inst32(compiler, MOV_WI | RD4(dst_reg) | tmp);
2221 tmp = get_imm(~srcw);
2222 if (tmp != INVALID_IMM) {
2223 FAIL_IF(push_inst16(compiler, IT | (cc << 4) | 0x8));
2224 return push_inst32(compiler, MVN_WI | RD4(dst_reg) | tmp);
2227 FAIL_IF(push_inst16(compiler, IT | (cc << 4) | ((cc & 0x1) << 3) | 0x4));
2229 tmp = (sljit_uw) srcw;
2230 FAIL_IF(push_inst32(compiler, MOVW | RD4(dst_reg)
2231 | COPY_BITS(tmp, 12, 16, 4) | COPY_BITS(tmp, 11, 26, 1) | COPY_BITS(tmp, 8, 12, 3) | (tmp & 0xff)));
2232 return push_inst32(compiler, MOVT | RD4(dst_reg)
2233 | COPY_BITS(tmp, 12 + 16, 16, 4) | COPY_BITS(tmp, 11 + 16, 26, 1) | COPY_BITS(tmp, 8 + 16, 12, 3) | ((tmp & 0xff0000) >> 16));
2236 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_mem(struct sljit_compiler *compiler, sljit_s32 type,
2238 sljit_s32 mem, sljit_sw memw)
2244 CHECK(check_sljit_emit_mem(compiler, type, reg, mem, memw));
2246 if ((mem & OFFS_REG_MASK) || (memw > 255 && memw < -255))
2247 return SLJIT_ERR_UNSUPPORTED;
2249 if (type & SLJIT_MEM_SUPP)
2250 return SLJIT_SUCCESS;
2252 switch (type & 0xff) {
2263 flags = BYTE_SIZE | SIGNED;
2269 flags = HALF_SIZE | SIGNED;
2272 SLJIT_UNREACHABLE();
2277 if (type & SLJIT_MEM_STORE)
2280 inst = sljit_mem32[flags] | 0x900;
2282 if (type & SLJIT_MEM_PRE)
2290 return push_inst32(compiler, inst | RT4(reg) | RN4(mem & REG_MASK) | memw);
2293 SLJIT_API_FUNC_ATTRIBUTE struct sljit_const* sljit_emit_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw, sljit_sw init_value)
2295 struct sljit_const *const_;
2299 CHECK_PTR(check_sljit_emit_const(compiler, dst, dstw, init_value));
2300 ADJUST_LOCAL_OFFSET(dst, dstw);
2302 const_ = (struct sljit_const*)ensure_abuf(compiler, sizeof(struct sljit_const));
2303 PTR_FAIL_IF(!const_);
2304 set_const(const_, compiler);
2306 dst_r = FAST_IS_REG(dst) ? dst : TMP_REG1;
2307 PTR_FAIL_IF(emit_imm32_const(compiler, dst_r, init_value));
2309 if (dst & SLJIT_MEM)
2310 PTR_FAIL_IF(emit_op_mem(compiler, WORD_SIZE | STORE, dst_r, dst, dstw, TMP_REG2));
2314 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_target, sljit_sw executable_offset)
2316 sljit_u16 *inst = (sljit_u16*)addr;
2317 modify_imm32_const(inst, new_target);
2318 inst = (sljit_u16 *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
2319 SLJIT_CACHE_FLUSH(inst, inst + 4);
2322 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_const(sljit_uw addr, sljit_sw new_constant, sljit_sw executable_offset)
2324 sljit_u16 *inst = (sljit_u16*)addr;
2325 modify_imm32_const(inst, new_constant);
2326 inst = (sljit_u16 *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
2327 SLJIT_CACHE_FLUSH(inst, inst + 4);