Rework x86-32 stack layout
[sljit.git] / sljit_src / sljitNativeARM_T2_32.c
blob3a38554a01fb44e7ac742ec927a3bed304c25ed6
1 /*
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)
29 #ifdef __SOFTFP__
30 return "ARM-Thumb2" SLJIT_CPUINFO " ABI:softfp";
31 #else
32 return "ARM-Thumb2" SLJIT_CPUINFO " ABI:hardfp";
33 #endif
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, 15, 14, 13, 12, 11, 10, 9, 8, 6, 7
56 #define COPY_BITS(src, from, to, bits) \
57 ((from >= to ? ((sljit_ins)(src) >> (from - to)) : ((sljit_ins)(src) << (to - from))) & (((1 << bits) - 1) << to))
59 #define NEGATE(uimm) ((sljit_uw)-(sljit_sw)(uimm))
61 /* Thumb16 encodings. */
62 #define RD3(rd) ((sljit_ins)reg_map[rd])
63 #define RN3(rn) ((sljit_ins)reg_map[rn] << 3)
64 #define RM3(rm) ((sljit_ins)reg_map[rm] << 6)
65 #define RDN3(rdn) ((sljit_ins)reg_map[rdn] << 8)
66 #define IMM3(imm) ((sljit_ins)imm << 6)
67 #define IMM8(imm) ((sljit_ins)imm)
69 /* Thumb16 helpers. */
70 #define SET_REGS44(rd, rn) \
71 (((sljit_ins)reg_map[rn] << 3) | ((sljit_ins)reg_map[rd] & 0x7) | (((sljit_ins)reg_map[rd] & 0x8) << 4))
72 #define IS_2_LO_REGS(reg1, reg2) \
73 (reg_map[reg1] <= 7 && reg_map[reg2] <= 7)
74 #define IS_3_LO_REGS(reg1, reg2, reg3) \
75 (reg_map[reg1] <= 7 && reg_map[reg2] <= 7 && reg_map[reg3] <= 7)
77 /* Thumb32 encodings. */
78 #define RD4(rd) ((sljit_ins)reg_map[rd] << 8)
79 #define RN4(rn) ((sljit_ins)reg_map[rn] << 16)
80 #define RM4(rm) ((sljit_ins)reg_map[rm])
81 #define RT4(rt) ((sljit_ins)reg_map[rt] << 12)
82 #define DD4(dd) ((sljit_ins)freg_map[dd] << 12)
83 #define DN4(dn) ((sljit_ins)freg_map[dn] << 16)
84 #define DM4(dm) ((sljit_ins)freg_map[dm])
85 #define IMM5(imm) \
86 (COPY_BITS(imm, 2, 12, 3) | (((sljit_ins)imm & 0x3) << 6))
87 #define IMM12(imm) \
88 (COPY_BITS(imm, 11, 26, 1) | COPY_BITS(imm, 8, 12, 3) | ((sljit_ins)imm & 0xff))
90 /* --------------------------------------------------------------------- */
91 /* Instrucion forms */
92 /* --------------------------------------------------------------------- */
94 /* dot '.' changed to _
95 I immediate form (possibly followed by number of immediate bits). */
96 #define ADCI 0xf1400000
97 #define ADCS 0x4140
98 #define ADC_W 0xeb400000
99 #define ADD 0x4400
100 #define ADDS 0x1800
101 #define ADDSI3 0x1c00
102 #define ADDSI8 0x3000
103 #define ADD_W 0xeb000000
104 #define ADDWI 0xf2000000
105 #define ADD_SP 0x4485
106 #define ADD_SP_I 0xb000
107 #define ADD_W 0xeb000000
108 #define ADD_WI 0xf1000000
109 #define ANDI 0xf0000000
110 #define ANDS 0x4000
111 #define AND_W 0xea000000
112 #define ASRS 0x4100
113 #define ASRSI 0x1000
114 #define ASR_W 0xfa40f000
115 #define ASR_WI 0xea4f0020
116 #define BCC 0xd000
117 #define BICI 0xf0200000
118 #define BKPT 0xbe00
119 #define BLX 0x4780
120 #define BX 0x4700
121 #define CLZ 0xfab0f080
122 #define CMNI_W 0xf1100f00
123 #define CMP 0x4280
124 #define CMPI 0x2800
125 #define CMPI_W 0xf1b00f00
126 #define CMP_X 0x4500
127 #define CMP_W 0xebb00f00
128 #define EORI 0xf0800000
129 #define EORS 0x4040
130 #define EOR_W 0xea800000
131 #define IT 0xbf00
132 #define LDR_SP 0x9800
133 #define LDR 0xf8d00000
134 #define LDRI 0xf8500800
135 #define LSLS 0x4080
136 #define LSLSI 0x0000
137 #define LSL_W 0xfa00f000
138 #define LSL_WI 0xea4f0000
139 #define LSRS 0x40c0
140 #define LSRSI 0x0800
141 #define LSR_W 0xfa20f000
142 #define LSR_WI 0xea4f0010
143 #define MOV 0x4600
144 #define MOVS 0x0000
145 #define MOVSI 0x2000
146 #define MOVT 0xf2c00000
147 #define MOVW 0xf2400000
148 #define MOV_W 0xea4f0000
149 #define MOV_WI 0xf04f0000
150 #define MUL 0xfb00f000
151 #define MVNS 0x43c0
152 #define MVN_W 0xea6f0000
153 #define MVN_WI 0xf06f0000
154 #define NOP 0xbf00
155 #define ORNI 0xf0600000
156 #define ORRI 0xf0400000
157 #define ORRS 0x4300
158 #define ORR_W 0xea400000
159 #define POP 0xbc00
160 #define POP_W 0xe8bd0000
161 #define PUSH 0xb400
162 #define PUSH_W 0xe92d0000
163 #define RSB_WI 0xf1c00000
164 #define RSBSI 0x4240
165 #define SBCI 0xf1600000
166 #define SBCS 0x4180
167 #define SBC_W 0xeb600000
168 #define SDIV 0xfb90f0f0
169 #define SMULL 0xfb800000
170 #define STR_SP 0x9000
171 #define SUBS 0x1a00
172 #define SUBSI3 0x1e00
173 #define SUBSI8 0x3800
174 #define SUB_W 0xeba00000
175 #define SUBWI 0xf2a00000
176 #define SUB_SP_I 0xb080
177 #define SUB_WI 0xf1a00000
178 #define SXTB 0xb240
179 #define SXTB_W 0xfa4ff080
180 #define SXTH 0xb200
181 #define SXTH_W 0xfa0ff080
182 #define TST 0x4200
183 #define TSTI 0xf0000f00
184 #define TST_W 0xea000f00
185 #define UDIV 0xfbb0f0f0
186 #define UMULL 0xfba00000
187 #define UXTB 0xb2c0
188 #define UXTB_W 0xfa5ff080
189 #define UXTH 0xb280
190 #define UXTH_W 0xfa1ff080
191 #define VABS_F32 0xeeb00ac0
192 #define VADD_F32 0xee300a00
193 #define VCMP_F32 0xeeb40a40
194 #define VCVT_F32_S32 0xeeb80ac0
195 #define VCVT_F64_F32 0xeeb70ac0
196 #define VCVT_S32_F32 0xeebd0ac0
197 #define VDIV_F32 0xee800a00
198 #define VLDR_F32 0xed100a00
199 #define VMOV_F32 0xeeb00a40
200 #define VMOV 0xee000a10
201 #define VMOV2 0xec400a10
202 #define VMRS 0xeef1fa10
203 #define VMUL_F32 0xee200a00
204 #define VNEG_F32 0xeeb10a40
205 #define VPOP 0xecbd0b00
206 #define VPUSH 0xed2d0b00
207 #define VSTR_F32 0xed000a00
208 #define VSUB_F32 0xee300a40
210 static sljit_s32 push_inst16(struct sljit_compiler *compiler, sljit_ins inst)
212 sljit_u16 *ptr;
213 SLJIT_ASSERT(!(inst & 0xffff0000));
215 ptr = (sljit_u16*)ensure_buf(compiler, sizeof(sljit_u16));
216 FAIL_IF(!ptr);
217 *ptr = (sljit_u16)(inst);
218 compiler->size++;
219 return SLJIT_SUCCESS;
222 static sljit_s32 push_inst32(struct sljit_compiler *compiler, sljit_ins inst)
224 sljit_u16 *ptr = (sljit_u16*)ensure_buf(compiler, sizeof(sljit_ins));
225 FAIL_IF(!ptr);
226 *ptr++ = (sljit_u16)(inst >> 16);
227 *ptr = (sljit_u16)(inst);
228 compiler->size += 2;
229 return SLJIT_SUCCESS;
232 static SLJIT_INLINE sljit_s32 emit_imm32_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_uw imm)
234 FAIL_IF(push_inst32(compiler, MOVW | RD4(dst)
235 | COPY_BITS(imm, 12, 16, 4) | COPY_BITS(imm, 11, 26, 1) | COPY_BITS(imm, 8, 12, 3) | (imm & 0xff)));
236 return push_inst32(compiler, MOVT | RD4(dst)
237 | COPY_BITS(imm, 12 + 16, 16, 4) | COPY_BITS(imm, 11 + 16, 26, 1) | COPY_BITS(imm, 8 + 16, 12, 3) | ((imm & 0xff0000) >> 16));
240 static SLJIT_INLINE void modify_imm32_const(sljit_u16 *inst, sljit_uw new_imm)
242 sljit_ins dst = inst[1] & 0x0f00;
243 SLJIT_ASSERT(((inst[0] & 0xfbf0) == (MOVW >> 16)) && ((inst[2] & 0xfbf0) == (MOVT >> 16)) && dst == (inst[3] & 0x0f00));
244 inst[0] = (sljit_u16)((MOVW >> 16) | COPY_BITS(new_imm, 12, 0, 4) | COPY_BITS(new_imm, 11, 10, 1));
245 inst[1] = (sljit_u16)(dst | COPY_BITS(new_imm, 8, 12, 3) | (new_imm & 0xff));
246 inst[2] = (sljit_u16)((MOVT >> 16) | COPY_BITS(new_imm, 12 + 16, 0, 4) | COPY_BITS(new_imm, 11 + 16, 10, 1));
247 inst[3] = (sljit_u16)(dst | COPY_BITS(new_imm, 8 + 16, 12, 3) | ((new_imm & 0xff0000) >> 16));
250 static SLJIT_INLINE sljit_s32 detect_jump_type(struct sljit_jump *jump, sljit_u16 *code_ptr, sljit_u16 *code, sljit_sw executable_offset)
252 sljit_sw diff;
254 if (jump->flags & SLJIT_REWRITABLE_JUMP)
255 return 0;
257 if (jump->flags & JUMP_ADDR) {
258 /* Branch to ARM code is not optimized yet. */
259 if (!(jump->u.target & 0x1))
260 return 0;
261 diff = ((sljit_sw)jump->u.target - (sljit_sw)(code_ptr + 2) - executable_offset) >> 1;
263 else {
264 SLJIT_ASSERT(jump->flags & JUMP_LABEL);
265 diff = ((sljit_sw)(code + jump->u.label->size) - (sljit_sw)(code_ptr + 2)) >> 1;
268 if (jump->flags & IS_COND) {
269 SLJIT_ASSERT(!(jump->flags & IS_BL));
270 if (diff <= 127 && diff >= -128) {
271 jump->flags |= PATCH_TYPE1;
272 return 5;
274 if (diff <= 524287 && diff >= -524288) {
275 jump->flags |= PATCH_TYPE2;
276 return 4;
278 /* +1 comes from the prefix IT instruction. */
279 diff--;
280 if (diff <= 8388607 && diff >= -8388608) {
281 jump->flags |= PATCH_TYPE3;
282 return 3;
285 else if (jump->flags & IS_BL) {
286 if (diff <= 8388607 && diff >= -8388608) {
287 jump->flags |= PATCH_BL;
288 return 3;
291 else {
292 if (diff <= 1023 && diff >= -1024) {
293 jump->flags |= PATCH_TYPE4;
294 return 4;
296 if (diff <= 8388607 && diff >= -8388608) {
297 jump->flags |= PATCH_TYPE5;
298 return 3;
302 return 0;
305 static SLJIT_INLINE void set_jump_instruction(struct sljit_jump *jump, sljit_sw executable_offset)
307 sljit_s32 type = (jump->flags >> 4) & 0xf;
308 sljit_sw diff;
309 sljit_u16 *jump_inst;
310 sljit_s32 s, j1, j2;
312 if (SLJIT_UNLIKELY(type == 0)) {
313 modify_imm32_const((sljit_u16*)jump->addr, (jump->flags & JUMP_LABEL) ? jump->u.label->addr : jump->u.target);
314 return;
317 if (jump->flags & JUMP_ADDR) {
318 SLJIT_ASSERT(jump->u.target & 0x1);
319 diff = ((sljit_sw)jump->u.target - (sljit_sw)(jump->addr + sizeof(sljit_u32)) - executable_offset) >> 1;
321 else {
322 SLJIT_ASSERT(jump->u.label->addr & 0x1);
323 diff = ((sljit_sw)(jump->u.label->addr) - (sljit_sw)(jump->addr + sizeof(sljit_u32)) - executable_offset) >> 1;
325 jump_inst = (sljit_u16*)jump->addr;
327 switch (type) {
328 case 1:
329 /* Encoding T1 of 'B' instruction */
330 SLJIT_ASSERT(diff <= 127 && diff >= -128 && (jump->flags & IS_COND));
331 jump_inst[0] = (sljit_u16)(0xd000 | (jump->flags & 0xf00) | ((sljit_ins)diff & 0xff));
332 return;
333 case 2:
334 /* Encoding T3 of 'B' instruction */
335 SLJIT_ASSERT(diff <= 524287 && diff >= -524288 && (jump->flags & IS_COND));
336 jump_inst[0] = (sljit_u16)(0xf000 | COPY_BITS(jump->flags, 8, 6, 4) | COPY_BITS(diff, 11, 0, 6) | COPY_BITS(diff, 19, 10, 1));
337 jump_inst[1] = (sljit_u16)(0x8000 | COPY_BITS(diff, 17, 13, 1) | COPY_BITS(diff, 18, 11, 1) | ((sljit_ins)diff & 0x7ff));
338 return;
339 case 3:
340 SLJIT_ASSERT(jump->flags & IS_COND);
341 *jump_inst++ = (sljit_u16)(IT | ((jump->flags >> 4) & 0xf0) | 0x8);
342 diff--;
343 type = 5;
344 break;
345 case 4:
346 /* Encoding T2 of 'B' instruction */
347 SLJIT_ASSERT(diff <= 1023 && diff >= -1024 && !(jump->flags & IS_COND));
348 jump_inst[0] = (sljit_u16)(0xe000 | (diff & 0x7ff));
349 return;
352 SLJIT_ASSERT(diff <= 8388607 && diff >= -8388608);
354 /* Really complex instruction form for branches. */
355 s = (diff >> 23) & 0x1;
356 j1 = (~(diff >> 22) ^ s) & 0x1;
357 j2 = (~(diff >> 21) ^ s) & 0x1;
358 jump_inst[0] = (sljit_u16)(0xf000 | ((sljit_ins)s << 10) | COPY_BITS(diff, 11, 0, 10));
359 jump_inst[1] = (sljit_u16)((j1 << 13) | (j2 << 11) | (diff & 0x7ff));
361 /* The others have a common form. */
362 if (type == 5) /* Encoding T4 of 'B' instruction */
363 jump_inst[1] |= 0x9000;
364 else if (type == 6) /* Encoding T1 of 'BL' instruction */
365 jump_inst[1] |= 0xd000;
366 else
367 SLJIT_UNREACHABLE();
370 SLJIT_API_FUNC_ATTRIBUTE void* sljit_generate_code(struct sljit_compiler *compiler)
372 struct sljit_memory_fragment *buf;
373 sljit_u16 *code;
374 sljit_u16 *code_ptr;
375 sljit_u16 *buf_ptr;
376 sljit_u16 *buf_end;
377 sljit_uw half_count;
378 sljit_uw next_addr;
379 sljit_sw executable_offset;
381 struct sljit_label *label;
382 struct sljit_jump *jump;
383 struct sljit_const *const_;
384 struct sljit_put_label *put_label;
386 CHECK_ERROR_PTR();
387 CHECK_PTR(check_sljit_generate_code(compiler));
388 reverse_buf(compiler);
390 code = (sljit_u16*)SLJIT_MALLOC_EXEC(compiler->size * sizeof(sljit_u16), compiler->exec_allocator_data);
391 PTR_FAIL_WITH_EXEC_IF(code);
392 buf = compiler->buf;
394 code_ptr = code;
395 half_count = 0;
396 next_addr = 0;
397 executable_offset = SLJIT_EXEC_OFFSET(code);
399 label = compiler->labels;
400 jump = compiler->jumps;
401 const_ = compiler->consts;
402 put_label = compiler->put_labels;
404 do {
405 buf_ptr = (sljit_u16*)buf->memory;
406 buf_end = buf_ptr + (buf->used_size >> 1);
407 do {
408 *code_ptr = *buf_ptr++;
409 if (next_addr == half_count) {
410 SLJIT_ASSERT(!label || label->size >= half_count);
411 SLJIT_ASSERT(!jump || jump->addr >= half_count);
412 SLJIT_ASSERT(!const_ || const_->addr >= half_count);
413 SLJIT_ASSERT(!put_label || put_label->addr >= half_count);
415 /* These structures are ordered by their address. */
416 if (label && label->size == half_count) {
417 label->addr = ((sljit_uw)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset)) | 0x1;
418 label->size = (sljit_uw)(code_ptr - code);
419 label = label->next;
421 if (jump && jump->addr == half_count) {
422 jump->addr = (sljit_uw)code_ptr - ((jump->flags & IS_COND) ? 10 : 8);
423 code_ptr -= detect_jump_type(jump, code_ptr, code, executable_offset);
424 jump = jump->next;
426 if (const_ && const_->addr == half_count) {
427 const_->addr = (sljit_uw)code_ptr;
428 const_ = const_->next;
430 if (put_label && put_label->addr == half_count) {
431 SLJIT_ASSERT(put_label->label);
432 put_label->addr = (sljit_uw)code_ptr;
433 put_label = put_label->next;
435 next_addr = compute_next_addr(label, jump, const_, put_label);
437 code_ptr++;
438 half_count++;
439 } while (buf_ptr < buf_end);
441 buf = buf->next;
442 } while (buf);
444 if (label && label->size == half_count) {
445 label->addr = ((sljit_uw)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset)) | 0x1;
446 label->size = (sljit_uw)(code_ptr - code);
447 label = label->next;
450 SLJIT_ASSERT(!label);
451 SLJIT_ASSERT(!jump);
452 SLJIT_ASSERT(!const_);
453 SLJIT_ASSERT(!put_label);
454 SLJIT_ASSERT(code_ptr - code <= (sljit_sw)compiler->size);
456 jump = compiler->jumps;
457 while (jump) {
458 set_jump_instruction(jump, executable_offset);
459 jump = jump->next;
462 put_label = compiler->put_labels;
463 while (put_label) {
464 modify_imm32_const((sljit_u16 *)put_label->addr, put_label->label->addr);
465 put_label = put_label->next;
468 compiler->error = SLJIT_ERR_COMPILED;
469 compiler->executable_offset = executable_offset;
470 compiler->executable_size = (sljit_uw)(code_ptr - code) * sizeof(sljit_u16);
472 code = (sljit_u16 *)SLJIT_ADD_EXEC_OFFSET(code, executable_offset);
473 code_ptr = (sljit_u16 *)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset);
475 SLJIT_CACHE_FLUSH(code, code_ptr);
476 SLJIT_UPDATE_WX_FLAGS(code, code_ptr, 1);
478 /* Set thumb mode flag. */
479 return (void*)((sljit_uw)code | 0x1);
482 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_has_cpu_feature(sljit_s32 feature_type)
484 switch (feature_type) {
485 case SLJIT_HAS_FPU:
486 #ifdef SLJIT_IS_FPU_AVAILABLE
487 return SLJIT_IS_FPU_AVAILABLE;
488 #else
489 /* Available by default. */
490 return 1;
491 #endif
493 case SLJIT_HAS_CLZ:
494 case SLJIT_HAS_CMOV:
495 case SLJIT_HAS_PREFETCH:
496 return 1;
498 default:
499 return 0;
503 /* --------------------------------------------------------------------- */
504 /* Core code generator functions. */
505 /* --------------------------------------------------------------------- */
507 #define INVALID_IMM 0x80000000
508 static sljit_uw get_imm(sljit_uw imm)
510 /* Thumb immediate form. */
511 sljit_s32 counter;
513 if (imm <= 0xff)
514 return imm;
516 if ((imm & 0xffff) == (imm >> 16)) {
517 /* Some special cases. */
518 if (!(imm & 0xff00))
519 return (1 << 12) | (imm & 0xff);
520 if (!(imm & 0xff))
521 return (2 << 12) | ((imm >> 8) & 0xff);
522 if ((imm & 0xff00) == ((imm & 0xff) << 8))
523 return (3 << 12) | (imm & 0xff);
526 /* Assembly optimization: count leading zeroes? */
527 counter = 8;
528 if (!(imm & 0xffff0000)) {
529 counter += 16;
530 imm <<= 16;
532 if (!(imm & 0xff000000)) {
533 counter += 8;
534 imm <<= 8;
536 if (!(imm & 0xf0000000)) {
537 counter += 4;
538 imm <<= 4;
540 if (!(imm & 0xc0000000)) {
541 counter += 2;
542 imm <<= 2;
544 if (!(imm & 0x80000000)) {
545 counter += 1;
546 imm <<= 1;
548 /* Since imm >= 128, this must be true. */
549 SLJIT_ASSERT(counter <= 31);
551 if (imm & 0x00ffffff)
552 return INVALID_IMM; /* Cannot be encoded. */
554 return ((imm >> 24) & 0x7f) | COPY_BITS(counter, 4, 26, 1) | COPY_BITS(counter, 1, 12, 3) | COPY_BITS(counter, 0, 7, 1);
557 static sljit_s32 load_immediate(struct sljit_compiler *compiler, sljit_s32 dst, sljit_uw imm)
559 sljit_uw tmp;
561 /* MOVS cannot be used since it destroy flags. */
563 if (imm >= 0x10000) {
564 tmp = get_imm(imm);
565 if (tmp != INVALID_IMM)
566 return push_inst32(compiler, MOV_WI | RD4(dst) | tmp);
567 tmp = get_imm(~imm);
568 if (tmp != INVALID_IMM)
569 return push_inst32(compiler, MVN_WI | RD4(dst) | tmp);
572 /* set low 16 bits, set hi 16 bits to 0. */
573 FAIL_IF(push_inst32(compiler, MOVW | RD4(dst)
574 | COPY_BITS(imm, 12, 16, 4) | COPY_BITS(imm, 11, 26, 1) | COPY_BITS(imm, 8, 12, 3) | (imm & 0xff)));
576 /* set hi 16 bit if needed. */
577 if (imm >= 0x10000)
578 return push_inst32(compiler, MOVT | RD4(dst)
579 | COPY_BITS(imm, 12 + 16, 16, 4) | COPY_BITS(imm, 11 + 16, 26, 1) | COPY_BITS(imm, 8 + 16, 12, 3) | ((imm & 0xff0000) >> 16));
580 return SLJIT_SUCCESS;
583 #define ARG1_IMM 0x0010000
584 #define ARG2_IMM 0x0020000
585 /* SET_FLAGS must be 0x100000 as it is also the value of S bit (can be used for optimization). */
586 #define SET_FLAGS 0x0100000
587 #define UNUSED_RETURN 0x0200000
589 static sljit_s32 emit_op_imm(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 dst, sljit_uw arg1, sljit_uw arg2)
591 /* dst must be register, TMP_REG1
592 arg1 must be register, imm
593 arg2 must be register, imm */
594 sljit_s32 reg;
595 sljit_uw imm, nimm;
597 if (SLJIT_UNLIKELY((flags & (ARG1_IMM | ARG2_IMM)) == (ARG1_IMM | ARG2_IMM))) {
598 /* Both are immediates, no temporaries are used. */
599 flags &= ~ARG1_IMM;
600 FAIL_IF(load_immediate(compiler, TMP_REG1, arg1));
601 arg1 = TMP_REG1;
604 if (flags & (ARG1_IMM | ARG2_IMM)) {
605 reg = (sljit_s32)((flags & ARG2_IMM) ? arg1 : arg2);
606 imm = (flags & ARG2_IMM) ? arg2 : arg1;
608 switch (flags & 0xffff) {
609 case SLJIT_CLZ:
610 case SLJIT_MUL:
611 /* No form with immediate operand. */
612 break;
613 case SLJIT_MOV:
614 SLJIT_ASSERT(!(flags & SET_FLAGS) && (flags & ARG2_IMM) && arg1 == TMP_REG2);
615 return load_immediate(compiler, dst, imm);
616 case SLJIT_NOT:
617 if (!(flags & SET_FLAGS))
618 return load_immediate(compiler, dst, ~imm);
619 /* Since the flags should be set, we just fallback to the register mode.
620 Although some clever things could be done here, "NOT IMM" does not worth the efforts. */
621 break;
622 case SLJIT_ADD:
623 compiler->status_flags_state = SLJIT_CURRENT_FLAGS_ADD;
624 nimm = NEGATE(imm);
625 if (IS_2_LO_REGS(reg, dst)) {
626 if (imm <= 0x7)
627 return push_inst16(compiler, ADDSI3 | IMM3(imm) | RD3(dst) | RN3(reg));
628 if (nimm <= 0x7)
629 return push_inst16(compiler, SUBSI3 | IMM3(nimm) | RD3(dst) | RN3(reg));
630 if (reg == dst) {
631 if (imm <= 0xff)
632 return push_inst16(compiler, ADDSI8 | IMM8(imm) | RDN3(dst));
633 if (nimm <= 0xff)
634 return push_inst16(compiler, SUBSI8 | IMM8(nimm) | RDN3(dst));
637 if (!(flags & SET_FLAGS)) {
638 if (imm <= 0xfff)
639 return push_inst32(compiler, ADDWI | RD4(dst) | RN4(reg) | IMM12(imm));
640 if (nimm <= 0xfff)
641 return push_inst32(compiler, SUBWI | RD4(dst) | RN4(reg) | IMM12(nimm));
643 nimm = get_imm(imm);
644 if (nimm != INVALID_IMM)
645 return push_inst32(compiler, ADD_WI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | nimm);
646 nimm = get_imm(NEGATE(imm));
647 if (nimm != INVALID_IMM)
648 return push_inst32(compiler, SUB_WI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | nimm);
649 break;
650 case SLJIT_ADDC:
651 compiler->status_flags_state = SLJIT_CURRENT_FLAGS_ADD;
652 imm = get_imm(imm);
653 if (imm != INVALID_IMM)
654 return push_inst32(compiler, ADCI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
655 break;
656 case SLJIT_SUB:
657 compiler->status_flags_state = SLJIT_CURRENT_FLAGS_SUB;
658 if (flags & ARG1_IMM) {
659 if (imm == 0 && IS_2_LO_REGS(reg, dst))
660 return push_inst16(compiler, RSBSI | RD3(dst) | RN3(reg));
661 imm = get_imm(imm);
662 if (imm != INVALID_IMM)
663 return push_inst32(compiler, RSB_WI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
664 break;
666 if (flags & UNUSED_RETURN) {
667 if (imm <= 0xff && reg_map[reg] <= 7)
668 return push_inst16(compiler, CMPI | IMM8(imm) | RDN3(reg));
669 nimm = get_imm(imm);
670 if (nimm != INVALID_IMM)
671 return push_inst32(compiler, CMPI_W | RN4(reg) | nimm);
672 nimm = get_imm(NEGATE(imm));
673 if (nimm != INVALID_IMM)
674 return push_inst32(compiler, CMNI_W | RN4(reg) | nimm);
675 break;
677 nimm = NEGATE(imm);
678 if (IS_2_LO_REGS(reg, dst)) {
679 if (imm <= 0x7)
680 return push_inst16(compiler, SUBSI3 | IMM3(imm) | RD3(dst) | RN3(reg));
681 if (nimm <= 0x7)
682 return push_inst16(compiler, ADDSI3 | IMM3(nimm) | RD3(dst) | RN3(reg));
683 if (reg == dst) {
684 if (imm <= 0xff)
685 return push_inst16(compiler, SUBSI8 | IMM8(imm) | RDN3(dst));
686 if (nimm <= 0xff)
687 return push_inst16(compiler, ADDSI8 | IMM8(nimm) | RDN3(dst));
690 if (!(flags & SET_FLAGS)) {
691 if (imm <= 0xfff)
692 return push_inst32(compiler, SUBWI | RD4(dst) | RN4(reg) | IMM12(imm));
693 if (nimm <= 0xfff)
694 return push_inst32(compiler, ADDWI | RD4(dst) | RN4(reg) | IMM12(nimm));
696 nimm = get_imm(imm);
697 if (nimm != INVALID_IMM)
698 return push_inst32(compiler, SUB_WI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | nimm);
699 nimm = get_imm(NEGATE(imm));
700 if (nimm != INVALID_IMM)
701 return push_inst32(compiler, ADD_WI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | nimm);
702 break;
703 case SLJIT_SUBC:
704 compiler->status_flags_state = SLJIT_CURRENT_FLAGS_SUB;
705 if (flags & ARG1_IMM)
706 break;
707 imm = get_imm(imm);
708 if (imm != INVALID_IMM)
709 return push_inst32(compiler, SBCI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
710 break;
711 case SLJIT_AND:
712 nimm = get_imm(imm);
713 if (nimm != INVALID_IMM)
714 return push_inst32(compiler, ((flags & UNUSED_RETURN) ? TSTI : ANDI) | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | nimm);
715 imm = get_imm(~imm);
716 if (imm != INVALID_IMM)
717 return push_inst32(compiler, BICI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
718 break;
719 case SLJIT_OR:
720 nimm = get_imm(imm);
721 if (nimm != INVALID_IMM)
722 return push_inst32(compiler, ORRI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | nimm);
723 imm = get_imm(~imm);
724 if (imm != INVALID_IMM)
725 return push_inst32(compiler, ORNI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
726 break;
727 case SLJIT_XOR:
728 imm = get_imm(imm);
729 if (imm != INVALID_IMM)
730 return push_inst32(compiler, EORI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
731 break;
732 case SLJIT_SHL:
733 case SLJIT_LSHR:
734 case SLJIT_ASHR:
735 if (flags & ARG1_IMM)
736 break;
737 imm &= 0x1f;
738 if (imm == 0) {
739 if (!(flags & SET_FLAGS))
740 return push_inst16(compiler, MOV | SET_REGS44(dst, reg));
741 if (IS_2_LO_REGS(dst, reg))
742 return push_inst16(compiler, MOVS | RD3(dst) | RN3(reg));
743 return push_inst32(compiler, MOV_W | SET_FLAGS | RD4(dst) | RM4(reg));
745 switch (flags & 0xffff) {
746 case SLJIT_SHL:
747 if (IS_2_LO_REGS(dst, reg))
748 return push_inst16(compiler, LSLSI | RD3(dst) | RN3(reg) | (imm << 6));
749 return push_inst32(compiler, LSL_WI | (flags & SET_FLAGS) | RD4(dst) | RM4(reg) | IMM5(imm));
750 case SLJIT_LSHR:
751 if (IS_2_LO_REGS(dst, reg))
752 return push_inst16(compiler, LSRSI | RD3(dst) | RN3(reg) | (imm << 6));
753 return push_inst32(compiler, LSR_WI | (flags & SET_FLAGS) | RD4(dst) | RM4(reg) | IMM5(imm));
754 default: /* SLJIT_ASHR */
755 if (IS_2_LO_REGS(dst, reg))
756 return push_inst16(compiler, ASRSI | RD3(dst) | RN3(reg) | (imm << 6));
757 return push_inst32(compiler, ASR_WI | (flags & SET_FLAGS) | RD4(dst) | RM4(reg) | IMM5(imm));
759 default:
760 SLJIT_UNREACHABLE();
761 break;
764 if (flags & ARG2_IMM) {
765 imm = arg2;
766 arg2 = (arg1 == TMP_REG1) ? TMP_REG2 : TMP_REG1;
767 FAIL_IF(load_immediate(compiler, (sljit_s32)arg2, imm));
769 else {
770 imm = arg1;
771 arg1 = (arg2 == TMP_REG1) ? TMP_REG2 : TMP_REG1;
772 FAIL_IF(load_immediate(compiler, (sljit_s32)arg1, imm));
775 SLJIT_ASSERT(arg1 != arg2);
778 /* Both arguments are registers. */
779 switch (flags & 0xffff) {
780 case SLJIT_MOV:
781 case SLJIT_MOV_U32:
782 case SLJIT_MOV_S32:
783 case SLJIT_MOV32:
784 case SLJIT_MOV_P:
785 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG2);
786 if (dst == (sljit_s32)arg2)
787 return SLJIT_SUCCESS;
788 return push_inst16(compiler, MOV | SET_REGS44(dst, arg2));
789 case SLJIT_MOV_U8:
790 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG2);
791 if (IS_2_LO_REGS(dst, arg2))
792 return push_inst16(compiler, UXTB | RD3(dst) | RN3(arg2));
793 return push_inst32(compiler, UXTB_W | RD4(dst) | RM4(arg2));
794 case SLJIT_MOV_S8:
795 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG2);
796 if (IS_2_LO_REGS(dst, arg2))
797 return push_inst16(compiler, SXTB | RD3(dst) | RN3(arg2));
798 return push_inst32(compiler, SXTB_W | RD4(dst) | RM4(arg2));
799 case SLJIT_MOV_U16:
800 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG2);
801 if (IS_2_LO_REGS(dst, arg2))
802 return push_inst16(compiler, UXTH | RD3(dst) | RN3(arg2));
803 return push_inst32(compiler, UXTH_W | RD4(dst) | RM4(arg2));
804 case SLJIT_MOV_S16:
805 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG2);
806 if (IS_2_LO_REGS(dst, arg2))
807 return push_inst16(compiler, SXTH | RD3(dst) | RN3(arg2));
808 return push_inst32(compiler, SXTH_W | RD4(dst) | RM4(arg2));
809 case SLJIT_NOT:
810 SLJIT_ASSERT(arg1 == TMP_REG2);
811 if (IS_2_LO_REGS(dst, arg2))
812 return push_inst16(compiler, MVNS | RD3(dst) | RN3(arg2));
813 return push_inst32(compiler, MVN_W | (flags & SET_FLAGS) | RD4(dst) | RM4(arg2));
814 case SLJIT_CLZ:
815 SLJIT_ASSERT(arg1 == TMP_REG2);
816 FAIL_IF(push_inst32(compiler, CLZ | RN4(arg2) | RD4(dst) | RM4(arg2)));
817 return SLJIT_SUCCESS;
818 case SLJIT_ADD:
819 compiler->status_flags_state = SLJIT_CURRENT_FLAGS_ADD;
820 if (IS_3_LO_REGS(dst, arg1, arg2))
821 return push_inst16(compiler, ADDS | RD3(dst) | RN3(arg1) | RM3(arg2));
822 if (dst == (sljit_s32)arg1 && !(flags & SET_FLAGS))
823 return push_inst16(compiler, ADD | SET_REGS44(dst, arg2));
824 return push_inst32(compiler, ADD_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
825 case SLJIT_ADDC:
826 compiler->status_flags_state = SLJIT_CURRENT_FLAGS_ADD;
827 if (dst == (sljit_s32)arg1 && IS_2_LO_REGS(dst, arg2))
828 return push_inst16(compiler, ADCS | RD3(dst) | RN3(arg2));
829 return push_inst32(compiler, ADC_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
830 case SLJIT_SUB:
831 compiler->status_flags_state = SLJIT_CURRENT_FLAGS_SUB;
832 if (flags & UNUSED_RETURN) {
833 if (IS_2_LO_REGS(arg1, arg2))
834 return push_inst16(compiler, CMP | RD3(arg1) | RN3(arg2));
835 return push_inst16(compiler, CMP_X | SET_REGS44(arg1, arg2));
837 if (IS_3_LO_REGS(dst, arg1, arg2))
838 return push_inst16(compiler, SUBS | RD3(dst) | RN3(arg1) | RM3(arg2));
839 return push_inst32(compiler, SUB_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
840 case SLJIT_SUBC:
841 compiler->status_flags_state = SLJIT_CURRENT_FLAGS_SUB;
842 if (dst == (sljit_s32)arg1 && IS_2_LO_REGS(dst, arg2))
843 return push_inst16(compiler, SBCS | RD3(dst) | RN3(arg2));
844 return push_inst32(compiler, SBC_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
845 case SLJIT_MUL:
846 compiler->status_flags_state = 0;
847 if (!(flags & SET_FLAGS))
848 return push_inst32(compiler, MUL | RD4(dst) | RN4(arg1) | RM4(arg2));
849 SLJIT_ASSERT(dst != TMP_REG2);
850 FAIL_IF(push_inst32(compiler, SMULL | RT4(dst) | RD4(TMP_REG2) | RN4(arg1) | RM4(arg2)));
851 /* cmp TMP_REG2, dst asr #31. */
852 return push_inst32(compiler, CMP_W | RN4(TMP_REG2) | 0x70e0 | RM4(dst));
853 case SLJIT_AND:
854 if (dst == (sljit_s32)arg1 && IS_2_LO_REGS(dst, arg2))
855 return push_inst16(compiler, ANDS | RD3(dst) | RN3(arg2));
856 if ((flags & UNUSED_RETURN) && IS_2_LO_REGS(arg1, arg2))
857 return push_inst16(compiler, TST | RD3(arg1) | RN3(arg2));
858 return push_inst32(compiler, ((flags & UNUSED_RETURN) ? TST_W : AND_W) | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
859 case SLJIT_OR:
860 if (dst == (sljit_s32)arg1 && IS_2_LO_REGS(dst, arg2))
861 return push_inst16(compiler, ORRS | RD3(dst) | RN3(arg2));
862 return push_inst32(compiler, ORR_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
863 case SLJIT_XOR:
864 if (dst == (sljit_s32)arg1 && IS_2_LO_REGS(dst, arg2))
865 return push_inst16(compiler, EORS | RD3(dst) | RN3(arg2));
866 return push_inst32(compiler, EOR_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
867 case SLJIT_SHL:
868 if (dst == (sljit_s32)arg1 && IS_2_LO_REGS(dst, arg2))
869 return push_inst16(compiler, LSLS | RD3(dst) | RN3(arg2));
870 return push_inst32(compiler, LSL_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
871 case SLJIT_LSHR:
872 if (dst == (sljit_s32)arg1 && IS_2_LO_REGS(dst, arg2))
873 return push_inst16(compiler, LSRS | RD3(dst) | RN3(arg2));
874 return push_inst32(compiler, LSR_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
875 case SLJIT_ASHR:
876 if (dst == (sljit_s32)arg1 && IS_2_LO_REGS(dst, arg2))
877 return push_inst16(compiler, ASRS | RD3(dst) | RN3(arg2));
878 return push_inst32(compiler, ASR_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
881 SLJIT_UNREACHABLE();
882 return SLJIT_SUCCESS;
885 #define STORE 0x01
886 #define SIGNED 0x02
888 #define WORD_SIZE 0x00
889 #define BYTE_SIZE 0x04
890 #define HALF_SIZE 0x08
891 #define PRELOAD 0x0c
893 #define IS_WORD_SIZE(flags) (!((flags) & (BYTE_SIZE | HALF_SIZE)))
894 #define ALIGN_CHECK(argw, imm, shift) (!((argw) & ~((imm) << (shift))))
897 1st letter:
898 w = word
899 b = byte
900 h = half
902 2nd letter:
903 s = signed
904 u = unsigned
906 3rd letter:
907 l = load
908 s = store
911 static const sljit_ins sljit_mem16[12] = {
912 /* w u l */ 0x5800 /* ldr */,
913 /* w u s */ 0x5000 /* str */,
914 /* w s l */ 0x5800 /* ldr */,
915 /* w s s */ 0x5000 /* str */,
917 /* b u l */ 0x5c00 /* ldrb */,
918 /* b u s */ 0x5400 /* strb */,
919 /* b s l */ 0x5600 /* ldrsb */,
920 /* b s s */ 0x5400 /* strb */,
922 /* h u l */ 0x5a00 /* ldrh */,
923 /* h u s */ 0x5200 /* strh */,
924 /* h s l */ 0x5e00 /* ldrsh */,
925 /* h s s */ 0x5200 /* strh */,
928 static const sljit_ins sljit_mem16_imm5[12] = {
929 /* w u l */ 0x6800 /* ldr imm5 */,
930 /* w u s */ 0x6000 /* str imm5 */,
931 /* w s l */ 0x6800 /* ldr imm5 */,
932 /* w s s */ 0x6000 /* str imm5 */,
934 /* b u l */ 0x7800 /* ldrb imm5 */,
935 /* b u s */ 0x7000 /* strb imm5 */,
936 /* b s l */ 0x0000 /* not allowed */,
937 /* b s s */ 0x7000 /* strb imm5 */,
939 /* h u l */ 0x8800 /* ldrh imm5 */,
940 /* h u s */ 0x8000 /* strh imm5 */,
941 /* h s l */ 0x0000 /* not allowed */,
942 /* h s s */ 0x8000 /* strh imm5 */,
945 #define MEM_IMM8 0xc00
946 #define MEM_IMM12 0x800000
947 static const sljit_ins sljit_mem32[13] = {
948 /* w u l */ 0xf8500000 /* ldr.w */,
949 /* w u s */ 0xf8400000 /* str.w */,
950 /* w s l */ 0xf8500000 /* ldr.w */,
951 /* w s s */ 0xf8400000 /* str.w */,
953 /* b u l */ 0xf8100000 /* ldrb.w */,
954 /* b u s */ 0xf8000000 /* strb.w */,
955 /* b s l */ 0xf9100000 /* ldrsb.w */,
956 /* b s s */ 0xf8000000 /* strb.w */,
958 /* h u l */ 0xf8300000 /* ldrh.w */,
959 /* h u s */ 0xf8200000 /* strsh.w */,
960 /* h s l */ 0xf9300000 /* ldrsh.w */,
961 /* h s s */ 0xf8200000 /* strsh.w */,
963 /* p u l */ 0xf8100000 /* pld */,
966 /* Helper function. Dst should be reg + value, using at most 1 instruction, flags does not set. */
967 static sljit_s32 emit_set_delta(struct sljit_compiler *compiler, sljit_s32 dst, sljit_s32 reg, sljit_sw value)
969 sljit_uw imm;
971 if (value >= 0) {
972 if (value <= 0xfff)
973 return push_inst32(compiler, ADDWI | RD4(dst) | RN4(reg) | IMM12(value));
974 imm = get_imm((sljit_uw)value);
975 if (imm != INVALID_IMM)
976 return push_inst32(compiler, ADD_WI | RD4(dst) | RN4(reg) | imm);
978 else {
979 value = -value;
980 if (value <= 0xfff)
981 return push_inst32(compiler, SUBWI | RD4(dst) | RN4(reg) | IMM12(value));
982 imm = get_imm((sljit_uw)value);
983 if (imm != INVALID_IMM)
984 return push_inst32(compiler, SUB_WI | RD4(dst) | RN4(reg) | imm);
986 return SLJIT_ERR_UNSUPPORTED;
989 static SLJIT_INLINE sljit_s32 emit_op_mem(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg,
990 sljit_s32 arg, sljit_sw argw, sljit_s32 tmp_reg)
992 sljit_s32 other_r;
993 sljit_uw tmp;
995 SLJIT_ASSERT(arg & SLJIT_MEM);
996 SLJIT_ASSERT((arg & REG_MASK) != tmp_reg || (arg == SLJIT_MEM1(tmp_reg) && argw >= -0xff && argw <= 0xfff));
998 if (SLJIT_UNLIKELY(!(arg & REG_MASK))) {
999 tmp = get_imm((sljit_uw)argw & ~(sljit_uw)0xfff);
1000 if (tmp != INVALID_IMM) {
1001 FAIL_IF(push_inst32(compiler, MOV_WI | RD4(tmp_reg) | tmp));
1002 return push_inst32(compiler, sljit_mem32[flags] | MEM_IMM12 | RT4(reg) | RN4(tmp_reg) | (argw & 0xfff));
1005 FAIL_IF(load_immediate(compiler, tmp_reg, (sljit_uw)argw));
1006 if (IS_2_LO_REGS(reg, tmp_reg) && sljit_mem16_imm5[flags])
1007 return push_inst16(compiler, sljit_mem16_imm5[flags] | RD3(reg) | RN3(tmp_reg));
1008 return push_inst32(compiler, sljit_mem32[flags] | MEM_IMM12 | RT4(reg) | RN4(tmp_reg));
1011 if (SLJIT_UNLIKELY(arg & OFFS_REG_MASK)) {
1012 argw &= 0x3;
1013 other_r = OFFS_REG(arg);
1014 arg &= REG_MASK;
1016 if (!argw && IS_3_LO_REGS(reg, arg, other_r))
1017 return push_inst16(compiler, sljit_mem16[flags] | RD3(reg) | RN3(arg) | RM3(other_r));
1018 return push_inst32(compiler, sljit_mem32[flags] | RT4(reg) | RN4(arg) | RM4(other_r) | ((sljit_ins)argw << 4));
1021 arg &= REG_MASK;
1023 if (argw > 0xfff) {
1024 tmp = get_imm((sljit_uw)(argw & ~0xfff));
1025 if (tmp != INVALID_IMM) {
1026 push_inst32(compiler, ADD_WI | RD4(tmp_reg) | RN4(arg) | tmp);
1027 arg = tmp_reg;
1028 argw = argw & 0xfff;
1031 else if (argw < -0xff) {
1032 tmp = get_imm((sljit_uw)(-argw & ~0xff));
1033 if (tmp != INVALID_IMM) {
1034 push_inst32(compiler, SUB_WI | RD4(tmp_reg) | RN4(arg) | tmp);
1035 arg = tmp_reg;
1036 argw = -(-argw & 0xff);
1040 /* 16 bit instruction forms. */
1041 if (IS_2_LO_REGS(reg, arg) && sljit_mem16_imm5[flags]) {
1042 tmp = 3;
1043 if (IS_WORD_SIZE(flags)) {
1044 if (ALIGN_CHECK(argw, 0x1f, 2))
1045 tmp = 2;
1047 else if (flags & BYTE_SIZE)
1049 if (ALIGN_CHECK(argw, 0x1f, 0))
1050 tmp = 0;
1052 else {
1053 SLJIT_ASSERT(flags & HALF_SIZE);
1054 if (ALIGN_CHECK(argw, 0x1f, 1))
1055 tmp = 1;
1058 if (tmp < 3)
1059 return push_inst16(compiler, sljit_mem16_imm5[flags] | RD3(reg) | RN3(arg) | ((sljit_ins)argw << (6 - tmp)));
1061 else if (SLJIT_UNLIKELY(arg == SLJIT_SP) && IS_WORD_SIZE(flags) && ALIGN_CHECK(argw, 0xff, 2) && reg_map[reg] <= 7) {
1062 /* SP based immediate. */
1063 return push_inst16(compiler, STR_SP | (sljit_ins)((flags & STORE) ? 0 : 0x800) | RDN3(reg) | ((sljit_ins)argw >> 2));
1066 if (argw >= 0 && argw <= 0xfff)
1067 return push_inst32(compiler, sljit_mem32[flags] | MEM_IMM12 | RT4(reg) | RN4(arg) | (sljit_ins)argw);
1068 else if (argw < 0 && argw >= -0xff)
1069 return push_inst32(compiler, sljit_mem32[flags] | MEM_IMM8 | RT4(reg) | RN4(arg) | (sljit_ins)-argw);
1071 SLJIT_ASSERT(arg != tmp_reg);
1073 FAIL_IF(load_immediate(compiler, tmp_reg, (sljit_uw)argw));
1074 if (IS_3_LO_REGS(reg, arg, tmp_reg))
1075 return push_inst16(compiler, sljit_mem16[flags] | RD3(reg) | RN3(arg) | RM3(tmp_reg));
1076 return push_inst32(compiler, sljit_mem32[flags] | RT4(reg) | RN4(arg) | RM4(tmp_reg));
1079 #undef ALIGN_CHECK
1080 #undef IS_WORD_SIZE
1082 /* --------------------------------------------------------------------- */
1083 /* Entry, exit */
1084 /* --------------------------------------------------------------------- */
1086 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_enter(struct sljit_compiler *compiler,
1087 sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
1088 sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
1090 sljit_s32 size, i, tmp, word_arg_count;
1091 sljit_s32 saved_arg_count = SLJIT_KEPT_SAVEDS_COUNT(options);
1092 sljit_uw offset;
1093 sljit_uw imm = 0;
1094 #ifdef __SOFTFP__
1095 sljit_u32 float_arg_count;
1096 #else
1097 sljit_u32 old_offset, f32_offset;
1098 sljit_u32 remap[3];
1099 sljit_u32 *remap_ptr = remap;
1100 #endif
1102 CHECK_ERROR();
1103 CHECK(check_sljit_emit_enter(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size));
1104 set_emit_enter(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size);
1106 tmp = SLJIT_S0 - saveds;
1107 for (i = SLJIT_S0 - saved_arg_count; i > tmp; i--)
1108 imm |= (sljit_uw)1 << reg_map[i];
1110 for (i = scratches; i >= SLJIT_FIRST_SAVED_REG; i--)
1111 imm |= (sljit_uw)1 << reg_map[i];
1113 /* At least two registers must be set for PUSH_W and one for PUSH instruction. */
1114 FAIL_IF((imm & 0xff00)
1115 ? push_inst32(compiler, PUSH_W | (1 << 14) | imm)
1116 : push_inst16(compiler, PUSH | (1 << 8) | imm));
1118 /* Stack must be aligned to 8 bytes: (LR, R4) */
1119 size = GET_SAVED_REGISTERS_SIZE(scratches, saveds - saved_arg_count, 1);
1121 if (fsaveds > 0 || fscratches >= SLJIT_FIRST_SAVED_FLOAT_REG) {
1122 if ((size & SSIZE_OF(sw)) != 0) {
1123 FAIL_IF(push_inst16(compiler, SUB_SP_I | (sizeof(sljit_sw) >> 2)));
1124 size += SSIZE_OF(sw);
1127 if (fsaveds + fscratches >= SLJIT_NUMBER_OF_FLOAT_REGISTERS) {
1128 FAIL_IF(push_inst32(compiler, VPUSH | DD4(SLJIT_FS0) | ((sljit_uw)SLJIT_NUMBER_OF_SAVED_FLOAT_REGISTERS << 1)));
1129 } else {
1130 if (fsaveds > 0)
1131 FAIL_IF(push_inst32(compiler, VPUSH | DD4(SLJIT_FS0) | ((sljit_uw)fsaveds << 1)));
1132 if (fscratches >= SLJIT_FIRST_SAVED_FLOAT_REG)
1133 FAIL_IF(push_inst32(compiler, VPUSH | DD4(fscratches) | ((sljit_uw)(fscratches - (SLJIT_FIRST_SAVED_FLOAT_REG - 1)) << 1)));
1137 local_size = ((size + local_size + 0x7) & ~0x7) - size;
1138 compiler->local_size = local_size;
1140 if (options & SLJIT_ENTER_REG_ARG)
1141 arg_types = 0;
1143 arg_types >>= SLJIT_ARG_SHIFT;
1144 word_arg_count = 0;
1145 saved_arg_count = 0;
1146 #ifdef __SOFTFP__
1147 SLJIT_COMPILE_ASSERT(SLJIT_FR0 == 1, float_register_index_start);
1149 offset = 0;
1150 float_arg_count = 0;
1152 while (arg_types) {
1153 switch (arg_types & SLJIT_ARG_MASK) {
1154 case SLJIT_ARG_TYPE_F64:
1155 if (offset & 0x7)
1156 offset += sizeof(sljit_sw);
1158 if (offset < 4 * sizeof(sljit_sw))
1159 FAIL_IF(push_inst32(compiler, VMOV2 | (offset << 10) | ((offset + sizeof(sljit_sw)) << 14) | float_arg_count));
1160 else
1161 FAIL_IF(push_inst32(compiler, VLDR_F32 | 0x800100 | RN4(SLJIT_SP)
1162 | (float_arg_count << 12) | ((offset + (sljit_uw)size - 4 * sizeof(sljit_sw)) >> 2)));
1163 float_arg_count++;
1164 offset += sizeof(sljit_f64) - sizeof(sljit_sw);
1165 break;
1166 case SLJIT_ARG_TYPE_F32:
1167 if (offset < 4 * sizeof(sljit_sw))
1168 FAIL_IF(push_inst32(compiler, VMOV | (float_arg_count << 16) | (offset << 10)));
1169 else
1170 FAIL_IF(push_inst32(compiler, VLDR_F32 | 0x800000 | RN4(SLJIT_SP)
1171 | (float_arg_count << 12) | ((offset + (sljit_uw)size - 4 * sizeof(sljit_sw)) >> 2)));
1172 float_arg_count++;
1173 break;
1174 default:
1175 word_arg_count++;
1177 if (!(arg_types & SLJIT_ARG_TYPE_SCRATCH_REG)) {
1178 tmp = SLJIT_S0 - saved_arg_count;
1179 saved_arg_count++;
1180 } else if (word_arg_count - 1 != (sljit_s32)(offset >> 2))
1181 tmp = word_arg_count;
1182 else
1183 break;
1185 if (offset < 4 * sizeof(sljit_sw))
1186 FAIL_IF(push_inst16(compiler, MOV | ((sljit_ins)reg_map[tmp] & 0x7) | (((sljit_ins)reg_map[tmp] & 0x8) << 4) | (offset << 1)));
1187 else if (reg_map[tmp] <= 7)
1188 FAIL_IF(push_inst16(compiler, LDR_SP | RDN3(tmp)
1189 | ((offset + (sljit_uw)size - 4 * sizeof(sljit_sw)) >> 2)));
1190 else
1191 FAIL_IF(push_inst32(compiler, LDR | RT4(tmp) | RN4(SLJIT_SP)
1192 | ((offset + (sljit_uw)size - 4 * sizeof(sljit_sw)))));
1193 break;
1196 offset += sizeof(sljit_sw);
1197 arg_types >>= SLJIT_ARG_SHIFT;
1200 compiler->args_size = offset;
1201 #else
1202 offset = SLJIT_FR0;
1203 old_offset = SLJIT_FR0;
1204 f32_offset = 0;
1206 while (arg_types) {
1207 switch (arg_types & SLJIT_ARG_MASK) {
1208 case SLJIT_ARG_TYPE_F64:
1209 if (offset != old_offset)
1210 *remap_ptr++ = VMOV_F32 | SLJIT_32 | DD4(offset) | DM4(old_offset);
1211 old_offset++;
1212 offset++;
1213 break;
1214 case SLJIT_ARG_TYPE_F32:
1215 if (f32_offset != 0) {
1216 *remap_ptr++ = VMOV_F32 | 0x20 | DD4(offset) | DM4(f32_offset);
1217 f32_offset = 0;
1218 } else {
1219 if (offset != old_offset)
1220 *remap_ptr++ = VMOV_F32 | DD4(offset) | DM4(old_offset);
1221 f32_offset = old_offset;
1222 old_offset++;
1224 offset++;
1225 break;
1226 default:
1227 if (!(arg_types & SLJIT_ARG_TYPE_SCRATCH_REG)) {
1228 FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(SLJIT_S0 - saved_arg_count, SLJIT_R0 + word_arg_count)));
1229 saved_arg_count++;
1232 word_arg_count++;
1233 break;
1235 arg_types >>= SLJIT_ARG_SHIFT;
1238 SLJIT_ASSERT((sljit_uw)(remap_ptr - remap) <= sizeof(remap));
1240 while (remap_ptr > remap)
1241 FAIL_IF(push_inst32(compiler, *(--remap_ptr)));
1242 #endif
1244 #ifdef _WIN32
1245 if (local_size >= 4096) {
1246 imm = get_imm(4096);
1247 SLJIT_ASSERT(imm != INVALID_IMM);
1249 FAIL_IF(push_inst32(compiler, SUB_WI | RD4(SLJIT_SP) | RN4(SLJIT_SP) | imm));
1251 if (local_size < 4 * 4096) {
1252 if (local_size > 2 * 4096) {
1253 if (local_size > 3 * 4096) {
1254 FAIL_IF(push_inst32(compiler, LDRI | 0x400 | RT4(TMP_REG1) | RN4(SLJIT_SP)));
1255 FAIL_IF(push_inst32(compiler, SUB_WI | RD4(SLJIT_SP) | RN4(SLJIT_SP) | imm));
1258 FAIL_IF(push_inst32(compiler, LDRI | 0x400 | RT4(TMP_REG1) | RN4(SLJIT_SP)));
1259 FAIL_IF(push_inst32(compiler, SUB_WI | RD4(SLJIT_SP) | RN4(SLJIT_SP) | imm));
1261 } else {
1262 FAIL_IF(load_immediate(compiler, TMP_REG2, ((sljit_uw)local_size >> 12) - 1));
1263 FAIL_IF(push_inst32(compiler, LDRI | 0x400 | RT4(TMP_REG1) | RN4(SLJIT_SP)));
1264 FAIL_IF(push_inst32(compiler, SUB_WI | RD4(SLJIT_SP) | RN4(SLJIT_SP) | imm));
1265 FAIL_IF(push_inst32(compiler, SUB_WI | SET_FLAGS | RD4(TMP_REG2) | RN4(TMP_REG2) | 1));
1266 FAIL_IF(push_inst16(compiler, BCC | (0x1 << 8) /* not-equal */ | (-8 & 0xff)));
1269 FAIL_IF(push_inst32(compiler, LDRI | 0x400 | RT4(TMP_REG1) | RN4(SLJIT_SP)));
1270 local_size &= 0xfff;
1273 if (local_size >= 256) {
1274 SLJIT_ASSERT(local_size < 4096);
1276 if (local_size <= (127 << 2))
1277 FAIL_IF(push_inst16(compiler, SUB_SP_I | ((sljit_uw)local_size >> 2)));
1278 else
1279 FAIL_IF(emit_op_imm(compiler, SLJIT_SUB | ARG2_IMM, SLJIT_SP, SLJIT_SP, (sljit_uw)local_size));
1281 FAIL_IF(push_inst32(compiler, LDRI | 0x400 | RT4(TMP_REG1) | RN4(SLJIT_SP)));
1282 } else if (local_size > 0)
1283 FAIL_IF(push_inst32(compiler, LDRI | 0x500 | RT4(TMP_REG1) | RN4(SLJIT_SP) | (sljit_uw)local_size));
1284 #else /* !_WIN32 */
1285 if (local_size > 0) {
1286 if (local_size <= (127 << 2))
1287 FAIL_IF(push_inst16(compiler, SUB_SP_I | ((sljit_uw)local_size >> 2)));
1288 else
1289 FAIL_IF(emit_op_imm(compiler, SLJIT_SUB | ARG2_IMM, SLJIT_SP, SLJIT_SP, (sljit_uw)local_size));
1291 #endif /* _WIN32 */
1293 return SLJIT_SUCCESS;
1296 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_set_context(struct sljit_compiler *compiler,
1297 sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
1298 sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
1300 sljit_s32 size;
1302 CHECK_ERROR();
1303 CHECK(check_sljit_set_context(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size));
1304 set_set_context(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size);
1306 size = GET_SAVED_REGISTERS_SIZE(scratches, saveds - SLJIT_KEPT_SAVEDS_COUNT(options), 1);
1308 if ((size & SSIZE_OF(sw)) != 0 && (fsaveds > 0 || fscratches >= SLJIT_FIRST_SAVED_FLOAT_REG))
1309 size += SSIZE_OF(sw);
1311 compiler->local_size = ((size + local_size + 0x7) & ~0x7) - size;
1312 return SLJIT_SUCCESS;
1315 static sljit_s32 emit_add_sp(struct sljit_compiler *compiler, sljit_uw imm)
1317 sljit_uw imm2;
1319 /* The TMP_REG1 register must keep its value. */
1320 if (imm <= (127u << 2))
1321 return push_inst16(compiler, ADD_SP_I | (imm >> 2));
1323 if (imm <= 0xfff)
1324 return push_inst32(compiler, ADDWI | RD4(SLJIT_SP) | RN4(SLJIT_SP) | IMM12(imm));
1326 imm2 = get_imm(imm);
1328 if (imm2 != INVALID_IMM)
1329 return push_inst32(compiler, ADD_WI | RD4(SLJIT_SP) | RN4(SLJIT_SP) | imm2);
1331 FAIL_IF(load_immediate(compiler, TMP_REG2, imm));
1332 return push_inst16(compiler, ADD_SP | RN3(TMP_REG2));
1335 static sljit_s32 emit_stack_frame_release(struct sljit_compiler *compiler, sljit_s32 frame_size)
1337 sljit_s32 local_size, fscratches, fsaveds, i, tmp;
1338 sljit_s32 saveds_restore_start = SLJIT_S0 - SLJIT_KEPT_SAVEDS_COUNT(compiler->options);
1339 sljit_s32 lr_dst = TMP_PC;
1340 sljit_uw reg_list;
1342 SLJIT_ASSERT(reg_map[TMP_REG2] == 14 && frame_size <= 128);
1344 local_size = compiler->local_size;
1345 fscratches = compiler->fscratches;
1346 fsaveds = compiler->fsaveds;
1348 if (fsaveds > 0 || fscratches >= SLJIT_FIRST_SAVED_FLOAT_REG) {
1349 if (local_size > 0)
1350 FAIL_IF(emit_add_sp(compiler, (sljit_uw)local_size));
1352 if (fsaveds + fscratches >= SLJIT_NUMBER_OF_FLOAT_REGISTERS) {
1353 FAIL_IF(push_inst32(compiler, VPOP | DD4(SLJIT_FS0) | ((sljit_uw)SLJIT_NUMBER_OF_SAVED_FLOAT_REGISTERS << 1)));
1354 } else {
1355 if (fscratches >= SLJIT_FIRST_SAVED_FLOAT_REG)
1356 FAIL_IF(push_inst32(compiler, VPOP | DD4(fscratches) | ((sljit_uw)(fscratches - (SLJIT_FIRST_SAVED_FLOAT_REG - 1)) << 1)));
1357 if (fsaveds > 0)
1358 FAIL_IF(push_inst32(compiler, VPOP | DD4(SLJIT_FS0) | ((sljit_uw)fsaveds << 1)));
1361 local_size = GET_SAVED_REGISTERS_SIZE(compiler->scratches, compiler->saveds, 1) & 0x7;
1364 if (frame_size < 0) {
1365 lr_dst = TMP_REG2;
1366 frame_size = 0;
1367 } else if (frame_size > 0)
1368 lr_dst = 0;
1370 reg_list = 0;
1371 tmp = SLJIT_S0 - compiler->saveds;
1372 if (saveds_restore_start != tmp) {
1373 for (i = saveds_restore_start; i > tmp; i--)
1374 reg_list |= (sljit_uw)1 << reg_map[i];
1375 } else
1376 saveds_restore_start = 0;
1378 for (i = compiler->scratches; i >= SLJIT_FIRST_SAVED_REG; i--)
1379 reg_list |= (sljit_uw)1 << reg_map[i];
1381 if (lr_dst == 0 && (reg_list & (reg_list - 1)) == 0) {
1382 /* The local_size does not include the saved registers. */
1383 local_size += SSIZE_OF(sw);
1385 if (reg_list != 0)
1386 local_size += SSIZE_OF(sw);
1388 if (frame_size > local_size)
1389 FAIL_IF(push_inst16(compiler, SUB_SP_I | ((sljit_uw)(frame_size - local_size) >> 2)));
1390 else if (frame_size < local_size)
1391 FAIL_IF(emit_add_sp(compiler, (sljit_uw)(local_size - frame_size)));
1393 if (reg_list == 0)
1394 return SLJIT_SUCCESS;
1396 if (saveds_restore_start != 0) {
1397 SLJIT_ASSERT(reg_list == ((sljit_uw)1 << reg_map[saveds_restore_start]));
1398 lr_dst = saveds_restore_start;
1399 } else {
1400 SLJIT_ASSERT(reg_list == ((sljit_uw)1 << reg_map[SLJIT_FIRST_SAVED_REG]));
1401 lr_dst = SLJIT_FIRST_SAVED_REG;
1404 frame_size -= 2 * SSIZE_OF(sw);
1406 if (reg_map[lr_dst] <= 7)
1407 return push_inst16(compiler, STR_SP | 0x800 | RDN3(lr_dst) | (sljit_uw)(frame_size >> 2));
1409 return push_inst32(compiler, LDR | RT4(lr_dst) | RN4(SLJIT_SP) | (sljit_uw)frame_size);
1412 if (local_size > 0)
1413 FAIL_IF(emit_add_sp(compiler, (sljit_uw)local_size));
1415 if (!(reg_list & 0xff00) && lr_dst != TMP_REG2) {
1416 if (lr_dst == TMP_PC)
1417 reg_list |= 1u << 8;
1419 /* At least one register must be set for POP instruction. */
1420 SLJIT_ASSERT(reg_list != 0);
1422 FAIL_IF(push_inst16(compiler, POP | reg_list));
1423 } else {
1424 if (lr_dst != 0) {
1425 if (reg_list == 0)
1426 return push_inst32(compiler, 0xf85d0b04 | RT4(lr_dst));
1428 reg_list |= (sljit_uw)1 << reg_map[lr_dst];
1431 /* At least two registers must be set for POP_W instruction. */
1432 SLJIT_ASSERT((reg_list & (reg_list - 1)) != 0);
1434 FAIL_IF(push_inst32(compiler, POP_W | reg_list));
1437 if (frame_size > 0)
1438 return push_inst16(compiler, SUB_SP_I | (((sljit_uw)frame_size - sizeof(sljit_sw)) >> 2));
1439 return SLJIT_SUCCESS;
1442 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_return_void(struct sljit_compiler *compiler)
1444 CHECK_ERROR();
1445 CHECK(check_sljit_emit_return_void(compiler));
1447 return emit_stack_frame_release(compiler, 0);
1450 /* --------------------------------------------------------------------- */
1451 /* Operators */
1452 /* --------------------------------------------------------------------- */
1454 #if !(defined __ARM_FEATURE_IDIV) && !(defined __ARM_ARCH_EXT_IDIV__)
1456 #ifdef __cplusplus
1457 extern "C" {
1458 #endif
1460 #ifdef _WIN32
1461 extern unsigned long long __rt_udiv(unsigned int denominator, unsigned int numerator);
1462 extern long long __rt_sdiv(int denominator, int numerator);
1463 #elif defined(__GNUC__)
1464 extern unsigned int __aeabi_uidivmod(unsigned int numerator, int unsigned denominator);
1465 extern int __aeabi_idivmod(int numerator, int denominator);
1466 #else
1467 #error "Software divmod functions are needed"
1468 #endif
1470 #ifdef __cplusplus
1472 #endif
1474 #endif /* !__ARM_FEATURE_IDIV && !__ARM_ARCH_EXT_IDIV__ */
1476 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op0(struct sljit_compiler *compiler, sljit_s32 op)
1478 #if !(defined __ARM_FEATURE_IDIV) && !(defined __ARM_ARCH_EXT_IDIV__)
1479 sljit_uw saved_reg_list[3];
1480 sljit_uw saved_reg_count;
1481 #endif
1483 CHECK_ERROR();
1484 CHECK(check_sljit_emit_op0(compiler, op));
1486 op = GET_OPCODE(op);
1487 switch (op) {
1488 case SLJIT_BREAKPOINT:
1489 return push_inst16(compiler, BKPT);
1490 case SLJIT_NOP:
1491 return push_inst16(compiler, NOP);
1492 case SLJIT_LMUL_UW:
1493 case SLJIT_LMUL_SW:
1494 return push_inst32(compiler, (op == SLJIT_LMUL_UW ? UMULL : SMULL)
1495 | RD4(SLJIT_R1) | RT4(SLJIT_R0) | RN4(SLJIT_R0) | RM4(SLJIT_R1));
1496 #if (defined __ARM_FEATURE_IDIV) || (defined __ARM_ARCH_EXT_IDIV__)
1497 case SLJIT_DIVMOD_UW:
1498 case SLJIT_DIVMOD_SW:
1499 FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(TMP_REG1, SLJIT_R0)));
1500 FAIL_IF(push_inst32(compiler, (op == SLJIT_DIVMOD_UW ? UDIV : SDIV) | RD4(SLJIT_R0) | RN4(SLJIT_R0) | RM4(SLJIT_R1)));
1501 FAIL_IF(push_inst32(compiler, MUL | RD4(SLJIT_R1) | RN4(SLJIT_R0) | RM4(SLJIT_R1)));
1502 return push_inst32(compiler, SUB_W | RD4(SLJIT_R1) | RN4(TMP_REG1) | RM4(SLJIT_R1));
1503 case SLJIT_DIV_UW:
1504 case SLJIT_DIV_SW:
1505 return push_inst32(compiler, (op == SLJIT_DIV_UW ? UDIV : SDIV) | RD4(SLJIT_R0) | RN4(SLJIT_R0) | RM4(SLJIT_R1));
1506 #else /* !__ARM_FEATURE_IDIV && !__ARM_ARCH_EXT_IDIV__ */
1507 case SLJIT_DIVMOD_UW:
1508 case SLJIT_DIVMOD_SW:
1509 case SLJIT_DIV_UW:
1510 case SLJIT_DIV_SW:
1511 SLJIT_COMPILE_ASSERT((SLJIT_DIVMOD_UW & 0x2) == 0 && SLJIT_DIV_UW - 0x2 == SLJIT_DIVMOD_UW, bad_div_opcode_assignments);
1512 SLJIT_ASSERT(reg_map[2] == 1 && reg_map[3] == 2 && reg_map[4] == 3);
1514 saved_reg_count = 0;
1515 if (compiler->scratches >= 4)
1516 saved_reg_list[saved_reg_count++] = 3;
1517 if (compiler->scratches >= 3)
1518 saved_reg_list[saved_reg_count++] = 2;
1519 if (op >= SLJIT_DIV_UW)
1520 saved_reg_list[saved_reg_count++] = 1;
1522 if (saved_reg_count > 0) {
1523 FAIL_IF(push_inst32(compiler, 0xf84d0d00 | (saved_reg_count >= 3 ? 16 : 8)
1524 | (saved_reg_list[0] << 12) /* str rX, [sp, #-8/-16]! */));
1525 if (saved_reg_count >= 2) {
1526 SLJIT_ASSERT(saved_reg_list[1] < 8);
1527 FAIL_IF(push_inst16(compiler, 0x9001 | (saved_reg_list[1] << 8) /* str rX, [sp, #4] */));
1529 if (saved_reg_count >= 3) {
1530 SLJIT_ASSERT(saved_reg_list[2] < 8);
1531 FAIL_IF(push_inst16(compiler, 0x9002 | (saved_reg_list[2] << 8) /* str rX, [sp, #8] */));
1535 #ifdef _WIN32
1536 FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(TMP_REG1, SLJIT_R0)));
1537 FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(SLJIT_R0, SLJIT_R1)));
1538 FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(SLJIT_R1, TMP_REG1)));
1539 FAIL_IF(sljit_emit_ijump(compiler, SLJIT_FAST_CALL, SLJIT_IMM,
1540 ((op | 0x2) == SLJIT_DIV_UW ? SLJIT_FUNC_ADDR(__rt_udiv) : SLJIT_FUNC_ADDR(__rt_sdiv))));
1541 #elif defined(__GNUC__)
1542 FAIL_IF(sljit_emit_ijump(compiler, SLJIT_FAST_CALL, SLJIT_IMM,
1543 ((op | 0x2) == SLJIT_DIV_UW ? SLJIT_FUNC_ADDR(__aeabi_uidivmod) : SLJIT_FUNC_ADDR(__aeabi_idivmod))));
1544 #else
1545 #error "Software divmod functions are needed"
1546 #endif
1548 if (saved_reg_count > 0) {
1549 if (saved_reg_count >= 3) {
1550 SLJIT_ASSERT(saved_reg_list[2] < 8);
1551 FAIL_IF(push_inst16(compiler, 0x9802 | (saved_reg_list[2] << 8) /* ldr rX, [sp, #8] */));
1553 if (saved_reg_count >= 2) {
1554 SLJIT_ASSERT(saved_reg_list[1] < 8);
1555 FAIL_IF(push_inst16(compiler, 0x9801 | (saved_reg_list[1] << 8) /* ldr rX, [sp, #4] */));
1557 return push_inst32(compiler, 0xf85d0b00 | (saved_reg_count >= 3 ? 16 : 8)
1558 | (saved_reg_list[0] << 12) /* ldr rX, [sp], #8/16 */);
1560 return SLJIT_SUCCESS;
1561 #endif /* __ARM_FEATURE_IDIV || __ARM_ARCH_EXT_IDIV__ */
1562 case SLJIT_ENDBR:
1563 case SLJIT_SKIP_FRAMES_BEFORE_RETURN:
1564 return SLJIT_SUCCESS;
1567 return SLJIT_SUCCESS;
1570 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op1(struct sljit_compiler *compiler, sljit_s32 op,
1571 sljit_s32 dst, sljit_sw dstw,
1572 sljit_s32 src, sljit_sw srcw)
1574 sljit_s32 dst_r, flags;
1575 sljit_s32 op_flags = GET_ALL_FLAGS(op);
1577 CHECK_ERROR();
1578 CHECK(check_sljit_emit_op1(compiler, op, dst, dstw, src, srcw));
1579 ADJUST_LOCAL_OFFSET(dst, dstw);
1580 ADJUST_LOCAL_OFFSET(src, srcw);
1582 dst_r = FAST_IS_REG(dst) ? dst : TMP_REG1;
1584 op = GET_OPCODE(op);
1585 if (op >= SLJIT_MOV && op <= SLJIT_MOV_P) {
1586 switch (op) {
1587 case SLJIT_MOV:
1588 case SLJIT_MOV_U32:
1589 case SLJIT_MOV_S32:
1590 case SLJIT_MOV32:
1591 case SLJIT_MOV_P:
1592 flags = WORD_SIZE;
1593 break;
1594 case SLJIT_MOV_U8:
1595 flags = BYTE_SIZE;
1596 if (src & SLJIT_IMM)
1597 srcw = (sljit_u8)srcw;
1598 break;
1599 case SLJIT_MOV_S8:
1600 flags = BYTE_SIZE | SIGNED;
1601 if (src & SLJIT_IMM)
1602 srcw = (sljit_s8)srcw;
1603 break;
1604 case SLJIT_MOV_U16:
1605 flags = HALF_SIZE;
1606 if (src & SLJIT_IMM)
1607 srcw = (sljit_u16)srcw;
1608 break;
1609 case SLJIT_MOV_S16:
1610 flags = HALF_SIZE | SIGNED;
1611 if (src & SLJIT_IMM)
1612 srcw = (sljit_s16)srcw;
1613 break;
1614 default:
1615 SLJIT_UNREACHABLE();
1616 flags = 0;
1617 break;
1620 if (src & SLJIT_IMM)
1621 FAIL_IF(emit_op_imm(compiler, SLJIT_MOV | ARG2_IMM, dst_r, TMP_REG2, (sljit_uw)srcw));
1622 else if (src & SLJIT_MEM) {
1623 FAIL_IF(emit_op_mem(compiler, flags, dst_r, src, srcw, TMP_REG1));
1624 } else {
1625 if (dst_r != TMP_REG1)
1626 return emit_op_imm(compiler, op, dst_r, TMP_REG2, (sljit_uw)src);
1627 dst_r = src;
1630 if (!(dst & SLJIT_MEM))
1631 return SLJIT_SUCCESS;
1633 return emit_op_mem(compiler, flags | STORE, dst_r, dst, dstw, TMP_REG2);
1636 flags = HAS_FLAGS(op_flags) ? SET_FLAGS : 0;
1638 if (src & SLJIT_MEM) {
1639 FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG1, src, srcw, TMP_REG1));
1640 src = TMP_REG1;
1643 emit_op_imm(compiler, flags | op, dst_r, TMP_REG2, (sljit_uw)src);
1645 if (SLJIT_UNLIKELY(dst & SLJIT_MEM))
1646 return emit_op_mem(compiler, flags | STORE, dst_r, dst, dstw, TMP_REG2);
1647 return SLJIT_SUCCESS;
1650 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op2(struct sljit_compiler *compiler, sljit_s32 op,
1651 sljit_s32 dst, sljit_sw dstw,
1652 sljit_s32 src1, sljit_sw src1w,
1653 sljit_s32 src2, sljit_sw src2w)
1655 sljit_s32 dst_reg, flags, src2_reg;
1657 CHECK_ERROR();
1658 CHECK(check_sljit_emit_op2(compiler, op, 0, dst, dstw, src1, src1w, src2, src2w));
1659 ADJUST_LOCAL_OFFSET(dst, dstw);
1660 ADJUST_LOCAL_OFFSET(src1, src1w);
1661 ADJUST_LOCAL_OFFSET(src2, src2w);
1663 dst_reg = FAST_IS_REG(dst) ? dst : TMP_REG1;
1664 flags = HAS_FLAGS(op) ? SET_FLAGS : 0;
1666 if (dst == TMP_REG1)
1667 flags |= UNUSED_RETURN;
1669 if (src1 & SLJIT_IMM)
1670 flags |= ARG1_IMM;
1671 else if (src1 & SLJIT_MEM) {
1672 emit_op_mem(compiler, WORD_SIZE, TMP_REG1, src1, src1w, TMP_REG1);
1673 src1w = TMP_REG1;
1675 else
1676 src1w = src1;
1678 if (src2 & SLJIT_IMM)
1679 flags |= ARG2_IMM;
1680 else if (src2 & SLJIT_MEM) {
1681 src2_reg = (!(flags & ARG1_IMM) && (src1w == TMP_REG1)) ? TMP_REG2 : TMP_REG1;
1682 emit_op_mem(compiler, WORD_SIZE, src2_reg, src2, src2w, src2_reg);
1683 src2w = src2_reg;
1685 else
1686 src2w = src2;
1688 emit_op_imm(compiler, flags | GET_OPCODE(op), dst_reg, (sljit_uw)src1w, (sljit_uw)src2w);
1690 if (!(dst & SLJIT_MEM))
1691 return SLJIT_SUCCESS;
1692 return emit_op_mem(compiler, WORD_SIZE | STORE, dst_reg, dst, dstw, TMP_REG2);
1695 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op2u(struct sljit_compiler *compiler, sljit_s32 op,
1696 sljit_s32 src1, sljit_sw src1w,
1697 sljit_s32 src2, sljit_sw src2w)
1699 CHECK_ERROR();
1700 CHECK(check_sljit_emit_op2(compiler, op, 1, 0, 0, src1, src1w, src2, src2w));
1702 SLJIT_SKIP_CHECKS(compiler);
1703 return sljit_emit_op2(compiler, op, TMP_REG1, 0, src1, src1w, src2, src2w);
1706 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_src(struct sljit_compiler *compiler, sljit_s32 op,
1707 sljit_s32 src, sljit_sw srcw)
1709 CHECK_ERROR();
1710 CHECK(check_sljit_emit_op_src(compiler, op, src, srcw));
1711 ADJUST_LOCAL_OFFSET(src, srcw);
1713 switch (op) {
1714 case SLJIT_FAST_RETURN:
1715 SLJIT_ASSERT(reg_map[TMP_REG2] == 14);
1717 if (FAST_IS_REG(src))
1718 FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(TMP_REG2, src)));
1719 else
1720 FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG2, src, srcw, TMP_REG2));
1722 return push_inst16(compiler, BX | RN3(TMP_REG2));
1723 case SLJIT_SKIP_FRAMES_BEFORE_FAST_RETURN:
1724 return SLJIT_SUCCESS;
1725 case SLJIT_PREFETCH_L1:
1726 case SLJIT_PREFETCH_L2:
1727 case SLJIT_PREFETCH_L3:
1728 case SLJIT_PREFETCH_ONCE:
1729 return emit_op_mem(compiler, PRELOAD, TMP_PC, src, srcw, TMP_REG1);
1732 return SLJIT_SUCCESS;
1735 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_register_index(sljit_s32 reg)
1737 CHECK_REG_INDEX(check_sljit_get_register_index(reg));
1738 return reg_map[reg];
1741 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_float_register_index(sljit_s32 reg)
1743 CHECK_REG_INDEX(check_sljit_get_float_register_index(reg));
1744 return (freg_map[reg] << 1);
1747 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_custom(struct sljit_compiler *compiler,
1748 void *instruction, sljit_u32 size)
1750 CHECK_ERROR();
1751 CHECK(check_sljit_emit_op_custom(compiler, instruction, size));
1753 if (size == 2)
1754 return push_inst16(compiler, *(sljit_u16*)instruction);
1755 return push_inst32(compiler, *(sljit_ins*)instruction);
1758 /* --------------------------------------------------------------------- */
1759 /* Floating point operators */
1760 /* --------------------------------------------------------------------- */
1762 #define FPU_LOAD (1 << 20)
1764 static sljit_s32 emit_fop_mem(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg, sljit_s32 arg, sljit_sw argw)
1766 sljit_uw imm;
1767 sljit_ins inst = VSTR_F32 | (flags & (SLJIT_32 | FPU_LOAD));
1769 SLJIT_ASSERT(arg & SLJIT_MEM);
1771 /* Fast loads and stores. */
1772 if (SLJIT_UNLIKELY(arg & OFFS_REG_MASK)) {
1773 FAIL_IF(push_inst32(compiler, ADD_W | RD4(TMP_REG1) | RN4(arg & REG_MASK) | RM4(OFFS_REG(arg)) | (((sljit_uw)argw & 0x3) << 6)));
1774 arg = SLJIT_MEM | TMP_REG1;
1775 argw = 0;
1778 if ((arg & REG_MASK) && (argw & 0x3) == 0) {
1779 if (!(argw & ~0x3fc))
1780 return push_inst32(compiler, inst | 0x800000 | RN4(arg & REG_MASK) | DD4(reg) | ((sljit_uw)argw >> 2));
1781 if (!(-argw & ~0x3fc))
1782 return push_inst32(compiler, inst | RN4(arg & REG_MASK) | DD4(reg) | ((sljit_uw)-argw >> 2));
1785 if (arg & REG_MASK) {
1786 if (emit_set_delta(compiler, TMP_REG1, arg & REG_MASK, argw) != SLJIT_ERR_UNSUPPORTED) {
1787 FAIL_IF(compiler->error);
1788 return push_inst32(compiler, inst | 0x800000 | RN4(TMP_REG1) | DD4(reg));
1791 imm = get_imm((sljit_uw)argw & ~(sljit_uw)0x3fc);
1792 if (imm != INVALID_IMM) {
1793 FAIL_IF(push_inst32(compiler, ADD_WI | RD4(TMP_REG1) | RN4(arg & REG_MASK) | imm));
1794 return push_inst32(compiler, inst | 0x800000 | RN4(TMP_REG1) | DD4(reg) | (((sljit_uw)argw & 0x3fc) >> 2));
1797 imm = get_imm((sljit_uw)-argw & ~(sljit_uw)0x3fc);
1798 if (imm != INVALID_IMM) {
1799 argw = -argw;
1800 FAIL_IF(push_inst32(compiler, SUB_WI | RD4(TMP_REG1) | RN4(arg & REG_MASK) | imm));
1801 return push_inst32(compiler, inst | RN4(TMP_REG1) | DD4(reg) | (((sljit_uw)argw & 0x3fc) >> 2));
1805 FAIL_IF(load_immediate(compiler, TMP_REG1, (sljit_uw)argw));
1806 if (arg & REG_MASK)
1807 FAIL_IF(push_inst16(compiler, ADD | SET_REGS44(TMP_REG1, (arg & REG_MASK))));
1808 return push_inst32(compiler, inst | 0x800000 | RN4(TMP_REG1) | DD4(reg));
1811 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_conv_sw_from_f64(struct sljit_compiler *compiler, sljit_s32 op,
1812 sljit_s32 dst, sljit_sw dstw,
1813 sljit_s32 src, sljit_sw srcw)
1815 op ^= SLJIT_32;
1817 if (src & SLJIT_MEM) {
1818 FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_32) | FPU_LOAD, TMP_FREG1, src, srcw));
1819 src = TMP_FREG1;
1822 FAIL_IF(push_inst32(compiler, VCVT_S32_F32 | (op & SLJIT_32) | DD4(TMP_FREG1) | DM4(src)));
1824 if (FAST_IS_REG(dst))
1825 return push_inst32(compiler, VMOV | (1 << 20) | RT4(dst) | DN4(TMP_FREG1));
1827 /* Store the integer value from a VFP register. */
1828 return emit_fop_mem(compiler, 0, TMP_FREG1, dst, dstw);
1831 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_conv_f64_from_sw(struct sljit_compiler *compiler, sljit_s32 op,
1832 sljit_s32 dst, sljit_sw dstw,
1833 sljit_s32 src, sljit_sw srcw)
1835 sljit_s32 dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
1837 op ^= SLJIT_32;
1839 if (FAST_IS_REG(src))
1840 FAIL_IF(push_inst32(compiler, VMOV | RT4(src) | DN4(TMP_FREG1)));
1841 else if (src & SLJIT_MEM) {
1842 /* Load the integer value into a VFP register. */
1843 FAIL_IF(emit_fop_mem(compiler, FPU_LOAD, TMP_FREG1, src, srcw));
1845 else {
1846 FAIL_IF(load_immediate(compiler, TMP_REG1, (sljit_uw)srcw));
1847 FAIL_IF(push_inst32(compiler, VMOV | RT4(TMP_REG1) | DN4(TMP_FREG1)));
1850 FAIL_IF(push_inst32(compiler, VCVT_F32_S32 | (op & SLJIT_32) | DD4(dst_r) | DM4(TMP_FREG1)));
1852 if (dst & SLJIT_MEM)
1853 return emit_fop_mem(compiler, (op & SLJIT_32), TMP_FREG1, dst, dstw);
1854 return SLJIT_SUCCESS;
1857 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_cmp(struct sljit_compiler *compiler, sljit_s32 op,
1858 sljit_s32 src1, sljit_sw src1w,
1859 sljit_s32 src2, sljit_sw src2w)
1861 op ^= SLJIT_32;
1863 if (src1 & SLJIT_MEM) {
1864 emit_fop_mem(compiler, (op & SLJIT_32) | FPU_LOAD, TMP_FREG1, src1, src1w);
1865 src1 = TMP_FREG1;
1868 if (src2 & SLJIT_MEM) {
1869 emit_fop_mem(compiler, (op & SLJIT_32) | FPU_LOAD, TMP_FREG2, src2, src2w);
1870 src2 = TMP_FREG2;
1873 FAIL_IF(push_inst32(compiler, VCMP_F32 | (op & SLJIT_32) | DD4(src1) | DM4(src2)));
1874 return push_inst32(compiler, VMRS);
1877 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop1(struct sljit_compiler *compiler, sljit_s32 op,
1878 sljit_s32 dst, sljit_sw dstw,
1879 sljit_s32 src, sljit_sw srcw)
1881 sljit_s32 dst_r;
1883 CHECK_ERROR();
1885 SLJIT_COMPILE_ASSERT((SLJIT_32 == 0x100), float_transfer_bit_error);
1886 SELECT_FOP1_OPERATION_WITH_CHECKS(compiler, op, dst, dstw, src, srcw);
1888 dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
1890 if (GET_OPCODE(op) != SLJIT_CONV_F64_FROM_F32)
1891 op ^= SLJIT_32;
1893 if (src & SLJIT_MEM) {
1894 emit_fop_mem(compiler, (op & SLJIT_32) | FPU_LOAD, dst_r, src, srcw);
1895 src = dst_r;
1898 switch (GET_OPCODE(op)) {
1899 case SLJIT_MOV_F64:
1900 if (src != dst_r) {
1901 if (dst_r != TMP_FREG1)
1902 FAIL_IF(push_inst32(compiler, VMOV_F32 | (op & SLJIT_32) | DD4(dst_r) | DM4(src)));
1903 else
1904 dst_r = src;
1906 break;
1907 case SLJIT_NEG_F64:
1908 FAIL_IF(push_inst32(compiler, VNEG_F32 | (op & SLJIT_32) | DD4(dst_r) | DM4(src)));
1909 break;
1910 case SLJIT_ABS_F64:
1911 FAIL_IF(push_inst32(compiler, VABS_F32 | (op & SLJIT_32) | DD4(dst_r) | DM4(src)));
1912 break;
1913 case SLJIT_CONV_F64_FROM_F32:
1914 FAIL_IF(push_inst32(compiler, VCVT_F64_F32 | (op & SLJIT_32) | DD4(dst_r) | DM4(src)));
1915 op ^= SLJIT_32;
1916 break;
1919 if (dst & SLJIT_MEM)
1920 return emit_fop_mem(compiler, (op & SLJIT_32), dst_r, dst, dstw);
1921 return SLJIT_SUCCESS;
1924 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop2(struct sljit_compiler *compiler, sljit_s32 op,
1925 sljit_s32 dst, sljit_sw dstw,
1926 sljit_s32 src1, sljit_sw src1w,
1927 sljit_s32 src2, sljit_sw src2w)
1929 sljit_s32 dst_r;
1931 CHECK_ERROR();
1932 CHECK(check_sljit_emit_fop2(compiler, op, dst, dstw, src1, src1w, src2, src2w));
1933 ADJUST_LOCAL_OFFSET(dst, dstw);
1934 ADJUST_LOCAL_OFFSET(src1, src1w);
1935 ADJUST_LOCAL_OFFSET(src2, src2w);
1937 op ^= SLJIT_32;
1939 dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
1940 if (src1 & SLJIT_MEM) {
1941 emit_fop_mem(compiler, (op & SLJIT_32) | FPU_LOAD, TMP_FREG1, src1, src1w);
1942 src1 = TMP_FREG1;
1944 if (src2 & SLJIT_MEM) {
1945 emit_fop_mem(compiler, (op & SLJIT_32) | FPU_LOAD, TMP_FREG2, src2, src2w);
1946 src2 = TMP_FREG2;
1949 switch (GET_OPCODE(op)) {
1950 case SLJIT_ADD_F64:
1951 FAIL_IF(push_inst32(compiler, VADD_F32 | (op & SLJIT_32) | DD4(dst_r) | DN4(src1) | DM4(src2)));
1952 break;
1953 case SLJIT_SUB_F64:
1954 FAIL_IF(push_inst32(compiler, VSUB_F32 | (op & SLJIT_32) | DD4(dst_r) | DN4(src1) | DM4(src2)));
1955 break;
1956 case SLJIT_MUL_F64:
1957 FAIL_IF(push_inst32(compiler, VMUL_F32 | (op & SLJIT_32) | DD4(dst_r) | DN4(src1) | DM4(src2)));
1958 break;
1959 case SLJIT_DIV_F64:
1960 FAIL_IF(push_inst32(compiler, VDIV_F32 | (op & SLJIT_32) | DD4(dst_r) | DN4(src1) | DM4(src2)));
1961 break;
1964 if (!(dst & SLJIT_MEM))
1965 return SLJIT_SUCCESS;
1966 return emit_fop_mem(compiler, (op & SLJIT_32), TMP_FREG1, dst, dstw);
1969 /* --------------------------------------------------------------------- */
1970 /* Other instructions */
1971 /* --------------------------------------------------------------------- */
1973 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fast_enter(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw)
1975 CHECK_ERROR();
1976 CHECK(check_sljit_emit_fast_enter(compiler, dst, dstw));
1977 ADJUST_LOCAL_OFFSET(dst, dstw);
1979 SLJIT_ASSERT(reg_map[TMP_REG2] == 14);
1981 if (FAST_IS_REG(dst))
1982 return push_inst16(compiler, MOV | SET_REGS44(dst, TMP_REG2));
1984 /* Memory. */
1985 return emit_op_mem(compiler, WORD_SIZE | STORE, TMP_REG2, dst, dstw, TMP_REG1);
1988 /* --------------------------------------------------------------------- */
1989 /* Conditional instructions */
1990 /* --------------------------------------------------------------------- */
1992 static sljit_uw get_cc(struct sljit_compiler *compiler, sljit_s32 type)
1994 switch (type) {
1995 case SLJIT_EQUAL:
1996 case SLJIT_F_EQUAL:
1997 case SLJIT_ORDERED_EQUAL:
1998 case SLJIT_UNORDERED_OR_EQUAL: /* Not supported. */
1999 return 0x0;
2001 case SLJIT_NOT_EQUAL:
2002 case SLJIT_F_NOT_EQUAL:
2003 case SLJIT_UNORDERED_OR_NOT_EQUAL:
2004 case SLJIT_ORDERED_NOT_EQUAL: /* Not supported. */
2005 return 0x1;
2007 case SLJIT_CARRY:
2008 if (compiler->status_flags_state & SLJIT_CURRENT_FLAGS_ADD)
2009 return 0x2;
2010 /* fallthrough */
2012 case SLJIT_LESS:
2013 return 0x3;
2015 case SLJIT_NOT_CARRY:
2016 if (compiler->status_flags_state & SLJIT_CURRENT_FLAGS_ADD)
2017 return 0x3;
2018 /* fallthrough */
2020 case SLJIT_GREATER_EQUAL:
2021 return 0x2;
2023 case SLJIT_GREATER:
2024 case SLJIT_UNORDERED_OR_GREATER:
2025 return 0x8;
2027 case SLJIT_LESS_EQUAL:
2028 case SLJIT_F_LESS_EQUAL:
2029 case SLJIT_ORDERED_LESS_EQUAL:
2030 return 0x9;
2032 case SLJIT_SIG_LESS:
2033 case SLJIT_UNORDERED_OR_LESS:
2034 return 0xb;
2036 case SLJIT_SIG_GREATER_EQUAL:
2037 case SLJIT_F_GREATER_EQUAL:
2038 case SLJIT_ORDERED_GREATER_EQUAL:
2039 return 0xa;
2041 case SLJIT_SIG_GREATER:
2042 case SLJIT_F_GREATER:
2043 case SLJIT_ORDERED_GREATER:
2044 return 0xc;
2046 case SLJIT_SIG_LESS_EQUAL:
2047 case SLJIT_UNORDERED_OR_LESS_EQUAL:
2048 return 0xd;
2050 case SLJIT_OVERFLOW:
2051 if (!(compiler->status_flags_state & (SLJIT_CURRENT_FLAGS_ADD | SLJIT_CURRENT_FLAGS_SUB)))
2052 return 0x1;
2053 /* fallthrough */
2055 case SLJIT_UNORDERED:
2056 return 0x6;
2058 case SLJIT_NOT_OVERFLOW:
2059 if (!(compiler->status_flags_state & (SLJIT_CURRENT_FLAGS_ADD | SLJIT_CURRENT_FLAGS_SUB)))
2060 return 0x0;
2061 /* fallthrough */
2063 case SLJIT_ORDERED:
2064 return 0x7;
2066 case SLJIT_F_LESS:
2067 case SLJIT_ORDERED_LESS:
2068 return 0x4;
2070 case SLJIT_UNORDERED_OR_GREATER_EQUAL:
2071 return 0x5;
2073 default: /* SLJIT_JUMP */
2074 SLJIT_UNREACHABLE();
2075 return 0xe;
2079 SLJIT_API_FUNC_ATTRIBUTE struct sljit_label* sljit_emit_label(struct sljit_compiler *compiler)
2081 struct sljit_label *label;
2083 CHECK_ERROR_PTR();
2084 CHECK_PTR(check_sljit_emit_label(compiler));
2086 if (compiler->last_label && compiler->last_label->size == compiler->size)
2087 return compiler->last_label;
2089 label = (struct sljit_label*)ensure_abuf(compiler, sizeof(struct sljit_label));
2090 PTR_FAIL_IF(!label);
2091 set_label(label, compiler);
2092 return label;
2095 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_jump(struct sljit_compiler *compiler, sljit_s32 type)
2097 struct sljit_jump *jump;
2098 sljit_ins cc;
2100 CHECK_ERROR_PTR();
2101 CHECK_PTR(check_sljit_emit_jump(compiler, type));
2103 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
2104 PTR_FAIL_IF(!jump);
2105 set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
2106 type &= 0xff;
2108 PTR_FAIL_IF(emit_imm32_const(compiler, TMP_REG1, 0));
2109 if (type < SLJIT_JUMP) {
2110 jump->flags |= IS_COND;
2111 cc = get_cc(compiler, type);
2112 jump->flags |= cc << 8;
2113 PTR_FAIL_IF(push_inst16(compiler, IT | (cc << 4) | 0x8));
2116 jump->addr = compiler->size;
2117 if (type <= SLJIT_JUMP)
2118 PTR_FAIL_IF(push_inst16(compiler, BX | RN3(TMP_REG1)));
2119 else {
2120 jump->flags |= IS_BL;
2121 PTR_FAIL_IF(push_inst16(compiler, BLX | RN3(TMP_REG1)));
2124 return jump;
2127 #ifdef __SOFTFP__
2129 static sljit_s32 softfloat_call_with_args(struct sljit_compiler *compiler, sljit_s32 arg_types, sljit_s32 *src, sljit_u32 *extra_space)
2131 sljit_u32 is_tail_call = *extra_space & SLJIT_CALL_RETURN;
2132 sljit_u32 offset = 0;
2133 sljit_u32 word_arg_offset = 0;
2134 sljit_u32 float_arg_count = 0;
2135 sljit_s32 types = 0;
2136 sljit_u32 src_offset = 4 * sizeof(sljit_sw);
2137 sljit_u8 offsets[4];
2138 sljit_u8 *offset_ptr = offsets;
2140 if (src && FAST_IS_REG(*src))
2141 src_offset = (sljit_u32)reg_map[*src] * sizeof(sljit_sw);
2143 arg_types >>= SLJIT_ARG_SHIFT;
2145 while (arg_types) {
2146 types = (types << SLJIT_ARG_SHIFT) | (arg_types & SLJIT_ARG_MASK);
2148 switch (arg_types & SLJIT_ARG_MASK) {
2149 case SLJIT_ARG_TYPE_F64:
2150 if (offset & 0x7)
2151 offset += sizeof(sljit_sw);
2152 *offset_ptr++ = (sljit_u8)offset;
2153 offset += sizeof(sljit_f64);
2154 float_arg_count++;
2155 break;
2156 case SLJIT_ARG_TYPE_F32:
2157 *offset_ptr++ = (sljit_u8)offset;
2158 offset += sizeof(sljit_f32);
2159 float_arg_count++;
2160 break;
2161 default:
2162 *offset_ptr++ = (sljit_u8)offset;
2163 offset += sizeof(sljit_sw);
2164 word_arg_offset += sizeof(sljit_sw);
2165 break;
2168 arg_types >>= SLJIT_ARG_SHIFT;
2171 if (offset > 4 * sizeof(sljit_sw) && (!is_tail_call || offset > compiler->args_size)) {
2172 /* Keep lr register on the stack. */
2173 if (is_tail_call)
2174 offset += sizeof(sljit_sw);
2176 offset = ((offset - 4 * sizeof(sljit_sw)) + 0x7) & ~(sljit_uw)0x7;
2178 *extra_space = offset;
2180 if (is_tail_call)
2181 FAIL_IF(emit_stack_frame_release(compiler, (sljit_s32)offset));
2182 else
2183 FAIL_IF(push_inst16(compiler, SUB_SP_I | (offset >> 2)));
2184 } else {
2185 if (is_tail_call)
2186 FAIL_IF(emit_stack_frame_release(compiler, -1));
2187 *extra_space = 0;
2190 SLJIT_ASSERT(reg_map[TMP_REG1] == 12);
2192 /* Process arguments in reversed direction. */
2193 while (types) {
2194 switch (types & SLJIT_ARG_MASK) {
2195 case SLJIT_ARG_TYPE_F64:
2196 float_arg_count--;
2197 offset = *(--offset_ptr);
2199 SLJIT_ASSERT((offset & 0x7) == 0);
2201 if (offset < 4 * sizeof(sljit_sw)) {
2202 if (src_offset == offset || src_offset == offset + sizeof(sljit_sw)) {
2203 FAIL_IF(push_inst16(compiler, MOV | (src_offset << 1) | 4 | (1 << 7)));
2204 *src = TMP_REG1;
2206 FAIL_IF(push_inst32(compiler, VMOV2 | 0x100000 | (offset << 10) | ((offset + sizeof(sljit_sw)) << 14) | float_arg_count));
2207 } else
2208 FAIL_IF(push_inst32(compiler, VSTR_F32 | 0x800100 | RN4(SLJIT_SP)
2209 | (float_arg_count << 12) | ((offset - 4 * sizeof(sljit_sw)) >> 2)));
2210 break;
2211 case SLJIT_ARG_TYPE_F32:
2212 float_arg_count--;
2213 offset = *(--offset_ptr);
2215 if (offset < 4 * sizeof(sljit_sw)) {
2216 if (src_offset == offset) {
2217 FAIL_IF(push_inst16(compiler, MOV | (src_offset << 1) | 4 | (1 << 7)));
2218 *src = TMP_REG1;
2220 FAIL_IF(push_inst32(compiler, VMOV | 0x100000 | (float_arg_count << 16) | (offset << 10)));
2221 } else
2222 FAIL_IF(push_inst32(compiler, VSTR_F32 | 0x800000 | RN4(SLJIT_SP)
2223 | (float_arg_count << 12) | ((offset - 4 * sizeof(sljit_sw)) >> 2)));
2224 break;
2225 default:
2226 word_arg_offset -= sizeof(sljit_sw);
2227 offset = *(--offset_ptr);
2229 SLJIT_ASSERT(offset >= word_arg_offset);
2231 if (offset != word_arg_offset) {
2232 if (offset < 4 * sizeof(sljit_sw)) {
2233 if (src_offset == offset) {
2234 FAIL_IF(push_inst16(compiler, MOV | (src_offset << 1) | 4 | (1 << 7)));
2235 *src = TMP_REG1;
2237 else if (src_offset == word_arg_offset) {
2238 *src = (sljit_s32)(1 + (offset >> 2));
2239 src_offset = offset;
2241 FAIL_IF(push_inst16(compiler, MOV | (offset >> 2) | (word_arg_offset << 1)));
2242 } else
2243 FAIL_IF(push_inst16(compiler, STR_SP | (word_arg_offset << 6) | ((offset - 4 * sizeof(sljit_sw)) >> 2)));
2245 break;
2248 types >>= SLJIT_ARG_SHIFT;
2251 return SLJIT_SUCCESS;
2254 static sljit_s32 softfloat_post_call_with_args(struct sljit_compiler *compiler, sljit_s32 arg_types)
2256 if ((arg_types & SLJIT_ARG_MASK) == SLJIT_ARG_TYPE_F64)
2257 FAIL_IF(push_inst32(compiler, VMOV2 | (1 << 16) | (0 << 12) | 0));
2258 if ((arg_types & SLJIT_ARG_MASK) == SLJIT_ARG_TYPE_F32)
2259 FAIL_IF(push_inst32(compiler, VMOV | (0 << 16) | (0 << 12)));
2261 return SLJIT_SUCCESS;
2264 #else
2266 static sljit_s32 hardfloat_call_with_args(struct sljit_compiler *compiler, sljit_s32 arg_types)
2268 sljit_u32 offset = SLJIT_FR0;
2269 sljit_u32 new_offset = SLJIT_FR0;
2270 sljit_u32 f32_offset = 0;
2272 /* Remove return value. */
2273 arg_types >>= SLJIT_ARG_SHIFT;
2275 while (arg_types) {
2276 switch (arg_types & SLJIT_ARG_MASK) {
2277 case SLJIT_ARG_TYPE_F64:
2278 if (offset != new_offset)
2279 FAIL_IF(push_inst32(compiler, VMOV_F32 | SLJIT_32 | DD4(new_offset) | DM4(offset)));
2281 new_offset++;
2282 offset++;
2283 break;
2284 case SLJIT_ARG_TYPE_F32:
2285 if (f32_offset != 0) {
2286 FAIL_IF(push_inst32(compiler, VMOV_F32 | 0x400000 | DD4(f32_offset) | DM4(offset)));
2287 f32_offset = 0;
2288 } else {
2289 if (offset != new_offset)
2290 FAIL_IF(push_inst32(compiler, VMOV_F32 | 0x400000 | DD4(new_offset) | DM4(offset)));
2291 f32_offset = new_offset;
2292 new_offset++;
2294 offset++;
2295 break;
2297 arg_types >>= SLJIT_ARG_SHIFT;
2300 return SLJIT_SUCCESS;
2303 #endif
2305 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_call(struct sljit_compiler *compiler, sljit_s32 type,
2306 sljit_s32 arg_types)
2308 #ifdef __SOFTFP__
2309 struct sljit_jump *jump;
2310 sljit_u32 extra_space = (sljit_u32)type;
2311 #endif
2313 CHECK_ERROR_PTR();
2314 CHECK_PTR(check_sljit_emit_call(compiler, type, arg_types));
2316 #ifdef __SOFTFP__
2317 if ((type & 0xff) != SLJIT_CALL_REG_ARG) {
2318 PTR_FAIL_IF(softfloat_call_with_args(compiler, arg_types, NULL, &extra_space));
2319 SLJIT_ASSERT((extra_space & 0x7) == 0);
2321 if ((type & SLJIT_CALL_RETURN) && extra_space == 0)
2322 type = SLJIT_JUMP | (type & SLJIT_REWRITABLE_JUMP);
2324 SLJIT_SKIP_CHECKS(compiler);
2325 jump = sljit_emit_jump(compiler, type);
2326 PTR_FAIL_IF(jump == NULL);
2328 if (extra_space > 0) {
2329 if (type & SLJIT_CALL_RETURN)
2330 PTR_FAIL_IF(push_inst32(compiler, LDR | RT4(TMP_REG2)
2331 | RN4(SLJIT_SP) | (extra_space - sizeof(sljit_sw))));
2333 PTR_FAIL_IF(push_inst16(compiler, ADD_SP_I | (extra_space >> 2)));
2335 if (type & SLJIT_CALL_RETURN) {
2336 PTR_FAIL_IF(push_inst16(compiler, BX | RN3(TMP_REG2)));
2337 return jump;
2341 SLJIT_ASSERT(!(type & SLJIT_CALL_RETURN));
2342 PTR_FAIL_IF(softfloat_post_call_with_args(compiler, arg_types));
2343 return jump;
2345 #endif /* __SOFTFP__ */
2347 if (type & SLJIT_CALL_RETURN) {
2348 /* ldmia sp!, {..., lr} */
2349 PTR_FAIL_IF(emit_stack_frame_release(compiler, -1));
2350 type = SLJIT_JUMP | (type & SLJIT_REWRITABLE_JUMP);
2353 #ifndef __SOFTFP__
2354 if ((type & 0xff) != SLJIT_CALL_REG_ARG)
2355 PTR_FAIL_IF(hardfloat_call_with_args(compiler, arg_types));
2356 #endif /* !__SOFTFP__ */
2358 SLJIT_SKIP_CHECKS(compiler);
2359 return sljit_emit_jump(compiler, type);
2362 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_ijump(struct sljit_compiler *compiler, sljit_s32 type, sljit_s32 src, sljit_sw srcw)
2364 struct sljit_jump *jump;
2366 CHECK_ERROR();
2367 CHECK(check_sljit_emit_ijump(compiler, type, src, srcw));
2368 ADJUST_LOCAL_OFFSET(src, srcw);
2370 SLJIT_ASSERT(reg_map[TMP_REG1] != 14);
2372 if (!(src & SLJIT_IMM)) {
2373 if (FAST_IS_REG(src)) {
2374 SLJIT_ASSERT(reg_map[src] != 14);
2375 return push_inst16(compiler, (type <= SLJIT_JUMP ? BX : BLX) | RN3(src));
2378 FAIL_IF(emit_op_mem(compiler, WORD_SIZE, type <= SLJIT_JUMP ? TMP_PC : TMP_REG1, src, srcw, TMP_REG1));
2379 if (type >= SLJIT_FAST_CALL)
2380 return push_inst16(compiler, BLX | RN3(TMP_REG1));
2383 /* These jumps are converted to jump/call instructions when possible. */
2384 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
2385 FAIL_IF(!jump);
2386 set_jump(jump, compiler, JUMP_ADDR | ((type >= SLJIT_FAST_CALL) ? IS_BL : 0));
2387 jump->u.target = (sljit_uw)srcw;
2389 FAIL_IF(emit_imm32_const(compiler, TMP_REG1, 0));
2390 jump->addr = compiler->size;
2391 return push_inst16(compiler, (type <= SLJIT_JUMP ? BX : BLX) | RN3(TMP_REG1));
2394 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_icall(struct sljit_compiler *compiler, sljit_s32 type,
2395 sljit_s32 arg_types,
2396 sljit_s32 src, sljit_sw srcw)
2398 #ifdef __SOFTFP__
2399 sljit_u32 extra_space = (sljit_u32)type;
2400 #endif
2402 CHECK_ERROR();
2403 CHECK(check_sljit_emit_icall(compiler, type, arg_types, src, srcw));
2405 if (src & SLJIT_MEM) {
2406 FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG1, src, srcw, TMP_REG1));
2407 src = TMP_REG1;
2410 if ((type & SLJIT_CALL_RETURN) && (src >= SLJIT_FIRST_SAVED_REG && src <= SLJIT_S0)) {
2411 FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(TMP_REG1, src)));
2412 src = TMP_REG1;
2415 #ifdef __SOFTFP__
2416 if ((type & 0xff) != SLJIT_CALL_REG_ARG) {
2417 FAIL_IF(softfloat_call_with_args(compiler, arg_types, &src, &extra_space));
2418 SLJIT_ASSERT((extra_space & 0x7) == 0);
2420 if ((type & SLJIT_CALL_RETURN) && extra_space == 0)
2421 type = SLJIT_JUMP;
2423 SLJIT_SKIP_CHECKS(compiler);
2424 FAIL_IF(sljit_emit_ijump(compiler, type, src, srcw));
2426 if (extra_space > 0) {
2427 if (type & SLJIT_CALL_RETURN)
2428 FAIL_IF(push_inst32(compiler, LDR | RT4(TMP_REG2)
2429 | RN4(SLJIT_SP) | (extra_space - sizeof(sljit_sw))));
2431 FAIL_IF(push_inst16(compiler, ADD_SP_I | (extra_space >> 2)));
2433 if (type & SLJIT_CALL_RETURN)
2434 return push_inst16(compiler, BX | RN3(TMP_REG2));
2437 SLJIT_ASSERT(!(type & SLJIT_CALL_RETURN));
2438 return softfloat_post_call_with_args(compiler, arg_types);
2440 #endif /* __SOFTFP__ */
2442 if (type & SLJIT_CALL_RETURN) {
2443 /* ldmia sp!, {..., lr} */
2444 FAIL_IF(emit_stack_frame_release(compiler, -1));
2445 type = SLJIT_JUMP;
2448 #ifndef __SOFTFP__
2449 if ((type & 0xff) != SLJIT_CALL_REG_ARG)
2450 FAIL_IF(hardfloat_call_with_args(compiler, arg_types));
2451 #endif /* !__SOFTFP__ */
2453 SLJIT_SKIP_CHECKS(compiler);
2454 return sljit_emit_ijump(compiler, type, src, srcw);
2457 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_flags(struct sljit_compiler *compiler, sljit_s32 op,
2458 sljit_s32 dst, sljit_sw dstw,
2459 sljit_s32 type)
2461 sljit_s32 dst_r, flags = GET_ALL_FLAGS(op);
2462 sljit_ins cc;
2464 CHECK_ERROR();
2465 CHECK(check_sljit_emit_op_flags(compiler, op, dst, dstw, type));
2466 ADJUST_LOCAL_OFFSET(dst, dstw);
2468 op = GET_OPCODE(op);
2469 cc = get_cc(compiler, type);
2470 dst_r = FAST_IS_REG(dst) ? dst : TMP_REG1;
2472 if (op < SLJIT_ADD) {
2473 FAIL_IF(push_inst16(compiler, IT | (cc << 4) | (((cc & 0x1) ^ 0x1) << 3) | 0x4));
2474 if (reg_map[dst_r] > 7) {
2475 FAIL_IF(push_inst32(compiler, MOV_WI | RD4(dst_r) | 1));
2476 FAIL_IF(push_inst32(compiler, MOV_WI | RD4(dst_r) | 0));
2477 } else {
2478 /* The movsi (immediate) instruction does not set flags in IT block. */
2479 FAIL_IF(push_inst16(compiler, MOVSI | RDN3(dst_r) | 1));
2480 FAIL_IF(push_inst16(compiler, MOVSI | RDN3(dst_r) | 0));
2482 if (!(dst & SLJIT_MEM))
2483 return SLJIT_SUCCESS;
2484 return emit_op_mem(compiler, WORD_SIZE | STORE, TMP_REG1, dst, dstw, TMP_REG2);
2487 if (dst & SLJIT_MEM)
2488 FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG1, dst, dstw, TMP_REG2));
2490 if (op == SLJIT_AND) {
2491 FAIL_IF(push_inst16(compiler, IT | (cc << 4) | (((cc & 0x1) ^ 0x1) << 3) | 0x4));
2492 FAIL_IF(push_inst32(compiler, ANDI | RN4(dst_r) | RD4(dst_r) | 1));
2493 FAIL_IF(push_inst32(compiler, ANDI | RN4(dst_r) | RD4(dst_r) | 0));
2495 else {
2496 FAIL_IF(push_inst16(compiler, IT | (cc << 4) | 0x8));
2497 FAIL_IF(push_inst32(compiler, ((op == SLJIT_OR) ? ORRI : EORI) | RN4(dst_r) | RD4(dst_r) | 1));
2500 if (dst & SLJIT_MEM)
2501 FAIL_IF(emit_op_mem(compiler, WORD_SIZE | STORE, TMP_REG1, dst, dstw, TMP_REG2));
2503 if (!(flags & SLJIT_SET_Z))
2504 return SLJIT_SUCCESS;
2506 /* The condition must always be set, even if the ORR/EORI is not executed above. */
2507 return push_inst32(compiler, MOV_W | SET_FLAGS | RD4(TMP_REG1) | RM4(dst_r));
2510 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_cmov(struct sljit_compiler *compiler, sljit_s32 type,
2511 sljit_s32 dst_reg,
2512 sljit_s32 src, sljit_sw srcw)
2514 sljit_uw cc, tmp;
2516 CHECK_ERROR();
2517 CHECK(check_sljit_emit_cmov(compiler, type, dst_reg, src, srcw));
2519 dst_reg &= ~SLJIT_32;
2521 cc = get_cc(compiler, type);
2523 if (!(src & SLJIT_IMM)) {
2524 FAIL_IF(push_inst16(compiler, IT | (cc << 4) | 0x8));
2525 return push_inst16(compiler, MOV | SET_REGS44(dst_reg, src));
2528 tmp = (sljit_uw) srcw;
2530 if (tmp < 0x10000) {
2531 /* set low 16 bits, set hi 16 bits to 0. */
2532 FAIL_IF(push_inst16(compiler, IT | (cc << 4) | 0x8));
2533 return push_inst32(compiler, MOVW | RD4(dst_reg)
2534 | COPY_BITS(tmp, 12, 16, 4) | COPY_BITS(tmp, 11, 26, 1) | COPY_BITS(tmp, 8, 12, 3) | (tmp & 0xff));
2537 tmp = get_imm((sljit_uw)srcw);
2538 if (tmp != INVALID_IMM) {
2539 FAIL_IF(push_inst16(compiler, IT | (cc << 4) | 0x8));
2540 return push_inst32(compiler, MOV_WI | RD4(dst_reg) | tmp);
2543 tmp = get_imm(~(sljit_uw)srcw);
2544 if (tmp != INVALID_IMM) {
2545 FAIL_IF(push_inst16(compiler, IT | (cc << 4) | 0x8));
2546 return push_inst32(compiler, MVN_WI | RD4(dst_reg) | tmp);
2549 FAIL_IF(push_inst16(compiler, IT | (cc << 4) | ((cc & 0x1) << 3) | 0x4));
2551 tmp = (sljit_uw) srcw;
2552 FAIL_IF(push_inst32(compiler, MOVW | RD4(dst_reg)
2553 | COPY_BITS(tmp, 12, 16, 4) | COPY_BITS(tmp, 11, 26, 1) | COPY_BITS(tmp, 8, 12, 3) | (tmp & 0xff)));
2554 return push_inst32(compiler, MOVT | RD4(dst_reg)
2555 | COPY_BITS(tmp, 12 + 16, 16, 4) | COPY_BITS(tmp, 11 + 16, 26, 1) | COPY_BITS(tmp, 8 + 16, 12, 3) | ((tmp & 0xff0000) >> 16));
2558 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_mem(struct sljit_compiler *compiler, sljit_s32 type,
2559 sljit_s32 reg,
2560 sljit_s32 mem, sljit_sw memw)
2562 sljit_s32 flags;
2563 sljit_ins inst;
2565 CHECK_ERROR();
2566 CHECK(check_sljit_emit_mem(compiler, type, reg, mem, memw));
2568 if (type & SLJIT_MEM_UNALIGNED)
2569 return sljit_emit_mem_unaligned(compiler, type, reg, mem, memw);
2571 if ((mem & OFFS_REG_MASK) || (memw > 255 || memw < -255))
2572 return SLJIT_ERR_UNSUPPORTED;
2574 if (type & SLJIT_MEM_SUPP)
2575 return SLJIT_SUCCESS;
2577 switch (type & 0xff) {
2578 case SLJIT_MOV:
2579 case SLJIT_MOV_U32:
2580 case SLJIT_MOV_S32:
2581 case SLJIT_MOV32:
2582 case SLJIT_MOV_P:
2583 flags = WORD_SIZE;
2584 break;
2585 case SLJIT_MOV_U8:
2586 flags = BYTE_SIZE;
2587 break;
2588 case SLJIT_MOV_S8:
2589 flags = BYTE_SIZE | SIGNED;
2590 break;
2591 case SLJIT_MOV_U16:
2592 flags = HALF_SIZE;
2593 break;
2594 case SLJIT_MOV_S16:
2595 flags = HALF_SIZE | SIGNED;
2596 break;
2597 default:
2598 SLJIT_UNREACHABLE();
2599 flags = WORD_SIZE;
2600 break;
2603 if (type & SLJIT_MEM_STORE)
2604 flags |= STORE;
2606 inst = sljit_mem32[flags] | 0x900;
2608 if (type & SLJIT_MEM_PRE)
2609 inst |= 0x400;
2611 if (memw >= 0)
2612 inst |= 0x200;
2613 else
2614 memw = -memw;
2616 return push_inst32(compiler, inst | RT4(reg) | RN4(mem & REG_MASK) | (sljit_ins)memw);
2619 static sljit_s32 update_mem_addr(struct sljit_compiler *compiler, sljit_s32 *mem, sljit_sw *memw, sljit_s32 max_offset)
2621 sljit_s32 arg = *mem;
2622 sljit_sw argw = *memw;
2623 sljit_uw imm;
2625 *mem = TMP_REG1;
2627 if (SLJIT_UNLIKELY(arg & OFFS_REG_MASK)) {
2628 *memw = 0;
2629 return push_inst32(compiler, ADD_W | RD4(TMP_REG1) | RN4(arg & REG_MASK) | RM4(OFFS_REG(arg)) | ((sljit_uw)(argw & 0x3) << 6));
2632 arg &= REG_MASK;
2634 if (arg) {
2635 if (argw <= max_offset && argw >= -0xff) {
2636 *mem = arg;
2637 return SLJIT_SUCCESS;
2640 if (argw < 0) {
2641 imm = get_imm((sljit_uw)(-argw & ~0xff));
2643 if (imm) {
2644 *memw = -(-argw & 0xff);
2645 return push_inst32(compiler, SUB_WI | RD4(TMP_REG1) | RN4(arg) | imm);
2647 } else if ((argw & 0xfff) <= max_offset) {
2648 imm = get_imm((sljit_uw)(argw & ~0xfff));
2650 if (imm) {
2651 *memw = argw & 0xfff;
2652 return push_inst32(compiler, ADD_WI | RD4(TMP_REG1) | RN4(arg) | imm);
2654 } else {
2655 imm = get_imm((sljit_uw)((argw | 0xfff) + 1));
2657 if (imm) {
2658 *memw = (argw & 0xfff) - 0x1000;
2659 return push_inst32(compiler, ADD_WI | RD4(TMP_REG1) | RN4(arg) | imm);
2664 imm = (sljit_uw)(argw & ~0xfff);
2666 if ((argw & 0xfff) > max_offset) {
2667 imm += 0x1000;
2668 *memw = (argw & 0xfff) - 0x1000;
2669 } else
2670 *memw = argw & 0xfff;
2672 FAIL_IF(load_immediate(compiler, TMP_REG1, imm));
2674 if (arg == 0)
2675 return SLJIT_SUCCESS;
2677 return push_inst16(compiler, ADD | SET_REGS44(TMP_REG1, arg));
2680 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fmem(struct sljit_compiler *compiler, sljit_s32 type,
2681 sljit_s32 freg,
2682 sljit_s32 mem, sljit_sw memw)
2684 CHECK_ERROR();
2685 CHECK(check_sljit_emit_fmem(compiler, type, freg, mem, memw));
2687 if (type & (SLJIT_MEM_PRE | SLJIT_MEM_POST))
2688 return SLJIT_ERR_UNSUPPORTED;
2690 if (type & SLJIT_MEM_ALIGNED_32)
2691 return emit_fop_mem(compiler, ((type ^ SLJIT_32) & SLJIT_32) | ((type & SLJIT_MEM_STORE) ? 0 : FPU_LOAD), freg, mem, memw);
2693 if (type & SLJIT_MEM_STORE) {
2694 FAIL_IF(push_inst32(compiler, VMOV | (1 << 20) | DN4(freg) | RT4(TMP_REG2)));
2696 if (type & SLJIT_32)
2697 return emit_op_mem(compiler, WORD_SIZE | STORE, TMP_REG2, mem, memw, TMP_REG1);
2699 FAIL_IF(update_mem_addr(compiler, &mem, &memw, 0xfff - 4));
2700 mem |= SLJIT_MEM;
2702 FAIL_IF(emit_op_mem(compiler, WORD_SIZE | STORE, TMP_REG2, mem, memw, TMP_REG1));
2703 FAIL_IF(push_inst32(compiler, VMOV | (1 << 20) | DN4(freg) | 0x80 | RT4(TMP_REG2)));
2704 return emit_op_mem(compiler, WORD_SIZE | STORE, TMP_REG2, mem, memw + 4, TMP_REG1);
2707 if (type & SLJIT_32) {
2708 FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG2, mem, memw, TMP_REG1));
2709 return push_inst32(compiler, VMOV | DN4(freg) | RT4(TMP_REG2));
2712 FAIL_IF(update_mem_addr(compiler, &mem, &memw, 0xfff - 4));
2713 mem |= SLJIT_MEM;
2715 FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG2, mem, memw, TMP_REG1));
2716 FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG1, mem, memw + 4, TMP_REG1));
2717 return push_inst32(compiler, VMOV2 | DM4(freg) | RT4(TMP_REG2) | RN4(TMP_REG1));
2720 #undef FPU_LOAD
2722 SLJIT_API_FUNC_ATTRIBUTE struct sljit_const* sljit_emit_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw, sljit_sw init_value)
2724 struct sljit_const *const_;
2725 sljit_s32 dst_r;
2727 CHECK_ERROR_PTR();
2728 CHECK_PTR(check_sljit_emit_const(compiler, dst, dstw, init_value));
2729 ADJUST_LOCAL_OFFSET(dst, dstw);
2731 const_ = (struct sljit_const*)ensure_abuf(compiler, sizeof(struct sljit_const));
2732 PTR_FAIL_IF(!const_);
2733 set_const(const_, compiler);
2735 dst_r = FAST_IS_REG(dst) ? dst : TMP_REG1;
2736 PTR_FAIL_IF(emit_imm32_const(compiler, dst_r, (sljit_uw)init_value));
2738 if (dst & SLJIT_MEM)
2739 PTR_FAIL_IF(emit_op_mem(compiler, WORD_SIZE | STORE, dst_r, dst, dstw, TMP_REG2));
2740 return const_;
2743 SLJIT_API_FUNC_ATTRIBUTE struct sljit_put_label* sljit_emit_put_label(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw)
2745 struct sljit_put_label *put_label;
2746 sljit_s32 dst_r;
2748 CHECK_ERROR_PTR();
2749 CHECK_PTR(check_sljit_emit_put_label(compiler, dst, dstw));
2750 ADJUST_LOCAL_OFFSET(dst, dstw);
2752 put_label = (struct sljit_put_label*)ensure_abuf(compiler, sizeof(struct sljit_put_label));
2753 PTR_FAIL_IF(!put_label);
2754 set_put_label(put_label, compiler, 0);
2756 dst_r = FAST_IS_REG(dst) ? dst : TMP_REG1;
2757 PTR_FAIL_IF(emit_imm32_const(compiler, dst_r, 0));
2759 if (dst & SLJIT_MEM)
2760 PTR_FAIL_IF(emit_op_mem(compiler, WORD_SIZE | STORE, dst_r, dst, dstw, TMP_REG2));
2761 return put_label;
2764 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_target, sljit_sw executable_offset)
2766 sljit_u16 *inst = (sljit_u16*)addr;
2767 SLJIT_UNUSED_ARG(executable_offset);
2769 SLJIT_UPDATE_WX_FLAGS(inst, inst + 4, 0);
2770 modify_imm32_const(inst, new_target);
2771 SLJIT_UPDATE_WX_FLAGS(inst, inst + 4, 1);
2772 inst = (sljit_u16 *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
2773 SLJIT_CACHE_FLUSH(inst, inst + 4);
2776 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_const(sljit_uw addr, sljit_sw new_constant, sljit_sw executable_offset)
2778 sljit_set_jump_addr(addr, (sljit_uw)new_constant, executable_offset);