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 ? ((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])
86 (COPY_BITS(imm, 2, 12, 3) | (((sljit_ins)imm & 0x3) << 6))
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
98 #define ADC_W 0xeb400000
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
111 #define AND_W 0xea000000
114 #define ASR_W 0xfa40f000
115 #define ASR_WI 0xea4f0020
117 #define BICI 0xf0200000
121 #define CLZ 0xfab0f080
122 #define CMNI_W 0xf1100f00
125 #define CMPI_W 0xf1b00f00
127 #define CMP_W 0xebb00f00
128 #define EORI 0xf0800000
130 #define EOR_W 0xea800000
132 #define LDR_SP 0x9800
133 #define LDR 0xf8d00000
134 #define LDRI 0xf8500800
137 #define LSL_W 0xfa00f000
138 #define LSL_WI 0xea4f0000
141 #define LSR_W 0xfa20f000
142 #define LSR_WI 0xea4f0010
146 #define MOVT 0xf2c00000
147 #define MOVW 0xf2400000
148 #define MOV_W 0xea4f0000
149 #define MOV_WI 0xf04f0000
150 #define MUL 0xfb00f000
152 #define MVN_W 0xea6f0000
153 #define MVN_WI 0xf06f0000
155 #define ORNI 0xf0600000
156 #define ORRI 0xf0400000
158 #define ORR_W 0xea400000
160 #define POP_W 0xe8bd0000
162 #define PUSH_W 0xe92d0000
163 #define RSB_WI 0xf1c00000
165 #define SBCI 0xf1600000
167 #define SBC_W 0xeb600000
168 #define SDIV 0xfb90f0f0
169 #define SMULL 0xfb800000
170 #define STR_SP 0x9000
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
179 #define SXTB_W 0xfa4ff080
181 #define SXTH_W 0xfa0ff080
183 #define TSTI 0xf0000f00
184 #define TST_W 0xea000f00
185 #define UDIV 0xfbb0f0f0
186 #define UMULL 0xfba00000
188 #define UXTB_W 0xfa5ff080
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 VSTR_F32 0xed000a00
206 #define VSUB_F32 0xee300a40
208 static sljit_s32
push_inst16(struct sljit_compiler
*compiler
, sljit_ins inst
)
211 SLJIT_ASSERT(!(inst
& 0xffff0000));
213 ptr
= (sljit_u16
*)ensure_buf(compiler
, sizeof(sljit_u16
));
215 *ptr
= (sljit_u16
)(inst
);
217 return SLJIT_SUCCESS
;
220 static sljit_s32
push_inst32(struct sljit_compiler
*compiler
, sljit_ins inst
)
222 sljit_u16
*ptr
= (sljit_u16
*)ensure_buf(compiler
, sizeof(sljit_ins
));
224 *ptr
++ = (sljit_u16
)(inst
>> 16);
225 *ptr
= (sljit_u16
)(inst
);
227 return SLJIT_SUCCESS
;
230 static SLJIT_INLINE sljit_s32
emit_imm32_const(struct sljit_compiler
*compiler
, sljit_s32 dst
, sljit_uw imm
)
232 FAIL_IF(push_inst32(compiler
, MOVW
| RD4(dst
)
233 | COPY_BITS(imm
, 12, 16, 4) | COPY_BITS(imm
, 11, 26, 1) | COPY_BITS(imm
, 8, 12, 3) | (imm
& 0xff)));
234 return push_inst32(compiler
, MOVT
| RD4(dst
)
235 | COPY_BITS(imm
, 12 + 16, 16, 4) | COPY_BITS(imm
, 11 + 16, 26, 1) | COPY_BITS(imm
, 8 + 16, 12, 3) | ((imm
& 0xff0000) >> 16));
238 static SLJIT_INLINE
void modify_imm32_const(sljit_u16
*inst
, sljit_uw new_imm
)
240 sljit_ins dst
= inst
[1] & 0x0f00;
241 SLJIT_ASSERT(((inst
[0] & 0xfbf0) == (MOVW
>> 16)) && ((inst
[2] & 0xfbf0) == (MOVT
>> 16)) && dst
== (inst
[3] & 0x0f00));
242 inst
[0] = (sljit_u16
)((MOVW
>> 16) | COPY_BITS(new_imm
, 12, 0, 4) | COPY_BITS(new_imm
, 11, 10, 1));
243 inst
[1] = (sljit_u16
)(dst
| COPY_BITS(new_imm
, 8, 12, 3) | (new_imm
& 0xff));
244 inst
[2] = (sljit_u16
)((MOVT
>> 16) | COPY_BITS(new_imm
, 12 + 16, 0, 4) | COPY_BITS(new_imm
, 11 + 16, 10, 1));
245 inst
[3] = (sljit_u16
)(dst
| COPY_BITS(new_imm
, 8 + 16, 12, 3) | ((new_imm
& 0xff0000) >> 16));
248 static SLJIT_INLINE sljit_s32
detect_jump_type(struct sljit_jump
*jump
, sljit_u16
*code_ptr
, sljit_u16
*code
, sljit_sw executable_offset
)
252 if (jump
->flags
& SLJIT_REWRITABLE_JUMP
)
255 if (jump
->flags
& JUMP_ADDR
) {
256 /* Branch to ARM code is not optimized yet. */
257 if (!(jump
->u
.target
& 0x1))
259 diff
= ((sljit_sw
)jump
->u
.target
- (sljit_sw
)(code_ptr
+ 2) - executable_offset
) >> 1;
262 SLJIT_ASSERT(jump
->flags
& JUMP_LABEL
);
263 diff
= ((sljit_sw
)(code
+ jump
->u
.label
->size
) - (sljit_sw
)(code_ptr
+ 2)) >> 1;
266 if (jump
->flags
& IS_COND
) {
267 SLJIT_ASSERT(!(jump
->flags
& IS_BL
));
268 if (diff
<= 127 && diff
>= -128) {
269 jump
->flags
|= PATCH_TYPE1
;
272 if (diff
<= 524287 && diff
>= -524288) {
273 jump
->flags
|= PATCH_TYPE2
;
276 /* +1 comes from the prefix IT instruction. */
278 if (diff
<= 8388607 && diff
>= -8388608) {
279 jump
->flags
|= PATCH_TYPE3
;
283 else if (jump
->flags
& IS_BL
) {
284 if (diff
<= 8388607 && diff
>= -8388608) {
285 jump
->flags
|= PATCH_BL
;
290 if (diff
<= 1023 && diff
>= -1024) {
291 jump
->flags
|= PATCH_TYPE4
;
294 if (diff
<= 8388607 && diff
>= -8388608) {
295 jump
->flags
|= PATCH_TYPE5
;
303 static SLJIT_INLINE
void set_jump_instruction(struct sljit_jump
*jump
, sljit_sw executable_offset
)
305 sljit_s32 type
= (jump
->flags
>> 4) & 0xf;
307 sljit_u16
*jump_inst
;
310 if (SLJIT_UNLIKELY(type
== 0)) {
311 modify_imm32_const((sljit_u16
*)jump
->addr
, (jump
->flags
& JUMP_LABEL
) ? jump
->u
.label
->addr
: jump
->u
.target
);
315 if (jump
->flags
& JUMP_ADDR
) {
316 SLJIT_ASSERT(jump
->u
.target
& 0x1);
317 diff
= ((sljit_sw
)jump
->u
.target
- (sljit_sw
)(jump
->addr
+ sizeof(sljit_u32
)) - executable_offset
) >> 1;
320 SLJIT_ASSERT(jump
->u
.label
->addr
& 0x1);
321 diff
= ((sljit_sw
)(jump
->u
.label
->addr
) - (sljit_sw
)(jump
->addr
+ sizeof(sljit_u32
)) - executable_offset
) >> 1;
323 jump_inst
= (sljit_u16
*)jump
->addr
;
327 /* Encoding T1 of 'B' instruction */
328 SLJIT_ASSERT(diff
<= 127 && diff
>= -128 && (jump
->flags
& IS_COND
));
329 jump_inst
[0] = (sljit_u16
)(0xd000 | (jump
->flags
& 0xf00) | ((sljit_ins
)diff
& 0xff));
332 /* Encoding T3 of 'B' instruction */
333 SLJIT_ASSERT(diff
<= 524287 && diff
>= -524288 && (jump
->flags
& IS_COND
));
334 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));
335 jump_inst
[1] = (sljit_u16
)(0x8000 | COPY_BITS(diff
, 17, 13, 1) | COPY_BITS(diff
, 18, 11, 1) | ((sljit_ins
)diff
& 0x7ff));
338 SLJIT_ASSERT(jump
->flags
& IS_COND
);
339 *jump_inst
++ = (sljit_u16
)(IT
| ((jump
->flags
>> 4) & 0xf0) | 0x8);
344 /* Encoding T2 of 'B' instruction */
345 SLJIT_ASSERT(diff
<= 1023 && diff
>= -1024 && !(jump
->flags
& IS_COND
));
346 jump_inst
[0] = (sljit_u16
)(0xe000 | (diff
& 0x7ff));
350 SLJIT_ASSERT(diff
<= 8388607 && diff
>= -8388608);
352 /* Really complex instruction form for branches. */
353 s
= (diff
>> 23) & 0x1;
354 j1
= (~(diff
>> 22) ^ s
) & 0x1;
355 j2
= (~(diff
>> 21) ^ s
) & 0x1;
356 jump_inst
[0] = (sljit_u16
)(0xf000 | ((sljit_ins
)s
<< 10) | COPY_BITS(diff
, 11, 0, 10));
357 jump_inst
[1] = (sljit_u16
)((j1
<< 13) | (j2
<< 11) | (diff
& 0x7ff));
359 /* The others have a common form. */
360 if (type
== 5) /* Encoding T4 of 'B' instruction */
361 jump_inst
[1] |= 0x9000;
362 else if (type
== 6) /* Encoding T1 of 'BL' instruction */
363 jump_inst
[1] |= 0xd000;
368 SLJIT_API_FUNC_ATTRIBUTE
void* sljit_generate_code(struct sljit_compiler
*compiler
)
370 struct sljit_memory_fragment
*buf
;
377 sljit_sw executable_offset
;
379 struct sljit_label
*label
;
380 struct sljit_jump
*jump
;
381 struct sljit_const
*const_
;
382 struct sljit_put_label
*put_label
;
385 CHECK_PTR(check_sljit_generate_code(compiler
));
386 reverse_buf(compiler
);
388 code
= (sljit_u16
*)SLJIT_MALLOC_EXEC(compiler
->size
* sizeof(sljit_u16
), compiler
->exec_allocator_data
);
389 PTR_FAIL_WITH_EXEC_IF(code
);
395 executable_offset
= SLJIT_EXEC_OFFSET(code
);
397 label
= compiler
->labels
;
398 jump
= compiler
->jumps
;
399 const_
= compiler
->consts
;
400 put_label
= compiler
->put_labels
;
403 buf_ptr
= (sljit_u16
*)buf
->memory
;
404 buf_end
= buf_ptr
+ (buf
->used_size
>> 1);
406 *code_ptr
= *buf_ptr
++;
407 if (next_addr
== half_count
) {
408 SLJIT_ASSERT(!label
|| label
->size
>= half_count
);
409 SLJIT_ASSERT(!jump
|| jump
->addr
>= half_count
);
410 SLJIT_ASSERT(!const_
|| const_
->addr
>= half_count
);
411 SLJIT_ASSERT(!put_label
|| put_label
->addr
>= half_count
);
413 /* These structures are ordered by their address. */
414 if (label
&& label
->size
== half_count
) {
415 label
->addr
= ((sljit_uw
)SLJIT_ADD_EXEC_OFFSET(code_ptr
, executable_offset
)) | 0x1;
416 label
->size
= (sljit_uw
)(code_ptr
- code
);
419 if (jump
&& jump
->addr
== half_count
) {
420 jump
->addr
= (sljit_uw
)code_ptr
- ((jump
->flags
& IS_COND
) ? 10 : 8);
421 code_ptr
-= detect_jump_type(jump
, code_ptr
, code
, executable_offset
);
424 if (const_
&& const_
->addr
== half_count
) {
425 const_
->addr
= (sljit_uw
)code_ptr
;
426 const_
= const_
->next
;
428 if (put_label
&& put_label
->addr
== half_count
) {
429 SLJIT_ASSERT(put_label
->label
);
430 put_label
->addr
= (sljit_uw
)code_ptr
;
431 put_label
= put_label
->next
;
433 next_addr
= compute_next_addr(label
, jump
, const_
, put_label
);
437 } while (buf_ptr
< buf_end
);
442 if (label
&& label
->size
== half_count
) {
443 label
->addr
= ((sljit_uw
)SLJIT_ADD_EXEC_OFFSET(code_ptr
, executable_offset
)) | 0x1;
444 label
->size
= (sljit_uw
)(code_ptr
- code
);
448 SLJIT_ASSERT(!label
);
450 SLJIT_ASSERT(!const_
);
451 SLJIT_ASSERT(!put_label
);
452 SLJIT_ASSERT(code_ptr
- code
<= (sljit_sw
)compiler
->size
);
454 jump
= compiler
->jumps
;
456 set_jump_instruction(jump
, executable_offset
);
460 put_label
= compiler
->put_labels
;
462 modify_imm32_const((sljit_u16
*)put_label
->addr
, put_label
->label
->addr
);
463 put_label
= put_label
->next
;
466 compiler
->error
= SLJIT_ERR_COMPILED
;
467 compiler
->executable_offset
= executable_offset
;
468 compiler
->executable_size
= (sljit_uw
)(code_ptr
- code
) * sizeof(sljit_u16
);
470 code
= (sljit_u16
*)SLJIT_ADD_EXEC_OFFSET(code
, executable_offset
);
471 code_ptr
= (sljit_u16
*)SLJIT_ADD_EXEC_OFFSET(code_ptr
, executable_offset
);
473 SLJIT_CACHE_FLUSH(code
, code_ptr
);
474 SLJIT_UPDATE_WX_FLAGS(code
, code_ptr
, 1);
476 /* Set thumb mode flag. */
477 return (void*)((sljit_uw
)code
| 0x1);
480 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_has_cpu_feature(sljit_s32 feature_type
)
482 switch (feature_type
) {
484 #ifdef SLJIT_IS_FPU_AVAILABLE
485 return SLJIT_IS_FPU_AVAILABLE
;
487 /* Available by default. */
493 case SLJIT_HAS_PREFETCH
:
501 /* --------------------------------------------------------------------- */
502 /* Core code generator functions. */
503 /* --------------------------------------------------------------------- */
505 #define INVALID_IMM 0x80000000
506 static sljit_uw
get_imm(sljit_uw imm
)
508 /* Thumb immediate form. */
514 if ((imm
& 0xffff) == (imm
>> 16)) {
515 /* Some special cases. */
517 return (1 << 12) | (imm
& 0xff);
519 return (2 << 12) | ((imm
>> 8) & 0xff);
520 if ((imm
& 0xff00) == ((imm
& 0xff) << 8))
521 return (3 << 12) | (imm
& 0xff);
524 /* Assembly optimization: count leading zeroes? */
526 if (!(imm
& 0xffff0000)) {
530 if (!(imm
& 0xff000000)) {
534 if (!(imm
& 0xf0000000)) {
538 if (!(imm
& 0xc0000000)) {
542 if (!(imm
& 0x80000000)) {
546 /* Since imm >= 128, this must be true. */
547 SLJIT_ASSERT(counter
<= 31);
549 if (imm
& 0x00ffffff)
550 return INVALID_IMM
; /* Cannot be encoded. */
552 return ((imm
>> 24) & 0x7f) | COPY_BITS(counter
, 4, 26, 1) | COPY_BITS(counter
, 1, 12, 3) | COPY_BITS(counter
, 0, 7, 1);
555 static sljit_s32
load_immediate(struct sljit_compiler
*compiler
, sljit_s32 dst
, sljit_uw imm
)
559 /* MOVS cannot be used since it destroy flags. */
561 if (imm
>= 0x10000) {
563 if (tmp
!= INVALID_IMM
)
564 return push_inst32(compiler
, MOV_WI
| RD4(dst
) | tmp
);
566 if (tmp
!= INVALID_IMM
)
567 return push_inst32(compiler
, MVN_WI
| RD4(dst
) | tmp
);
570 /* set low 16 bits, set hi 16 bits to 0. */
571 FAIL_IF(push_inst32(compiler
, MOVW
| RD4(dst
)
572 | COPY_BITS(imm
, 12, 16, 4) | COPY_BITS(imm
, 11, 26, 1) | COPY_BITS(imm
, 8, 12, 3) | (imm
& 0xff)));
574 /* set hi 16 bit if needed. */
576 return push_inst32(compiler
, MOVT
| RD4(dst
)
577 | COPY_BITS(imm
, 12 + 16, 16, 4) | COPY_BITS(imm
, 11 + 16, 26, 1) | COPY_BITS(imm
, 8 + 16, 12, 3) | ((imm
& 0xff0000) >> 16));
578 return SLJIT_SUCCESS
;
581 #define ARG1_IMM 0x0010000
582 #define ARG2_IMM 0x0020000
583 /* SET_FLAGS must be 0x100000 as it is also the value of S bit (can be used for optimization). */
584 #define SET_FLAGS 0x0100000
585 #define UNUSED_RETURN 0x0200000
587 static sljit_s32
emit_op_imm(struct sljit_compiler
*compiler
, sljit_s32 flags
, sljit_s32 dst
, sljit_uw arg1
, sljit_uw arg2
)
589 /* dst must be register, TMP_REG1
590 arg1 must be register, imm
591 arg2 must be register, imm */
595 if (SLJIT_UNLIKELY((flags
& (ARG1_IMM
| ARG2_IMM
)) == (ARG1_IMM
| ARG2_IMM
))) {
596 /* Both are immediates, no temporaries are used. */
598 FAIL_IF(load_immediate(compiler
, TMP_REG1
, arg1
));
602 if (flags
& (ARG1_IMM
| ARG2_IMM
)) {
603 reg
= (sljit_s32
)((flags
& ARG2_IMM
) ? arg1
: arg2
);
604 imm
= (flags
& ARG2_IMM
) ? arg2
: arg1
;
606 switch (flags
& 0xffff) {
609 /* No form with immediate operand. */
612 SLJIT_ASSERT(!(flags
& SET_FLAGS
) && (flags
& ARG2_IMM
) && arg1
== TMP_REG2
);
613 return load_immediate(compiler
, dst
, imm
);
615 if (!(flags
& SET_FLAGS
))
616 return load_immediate(compiler
, dst
, ~imm
);
617 /* Since the flags should be set, we just fallback to the register mode.
618 Although some clever things could be done here, "NOT IMM" does not worth the efforts. */
621 compiler
->status_flags_state
= SLJIT_CURRENT_FLAGS_ADD_SUB
;
623 if (IS_2_LO_REGS(reg
, dst
)) {
625 return push_inst16(compiler
, ADDSI3
| IMM3(imm
) | RD3(dst
) | RN3(reg
));
627 return push_inst16(compiler
, SUBSI3
| IMM3(nimm
) | RD3(dst
) | RN3(reg
));
630 return push_inst16(compiler
, ADDSI8
| IMM8(imm
) | RDN3(dst
));
632 return push_inst16(compiler
, SUBSI8
| IMM8(nimm
) | RDN3(dst
));
635 if (!(flags
& SET_FLAGS
)) {
637 return push_inst32(compiler
, ADDWI
| RD4(dst
) | RN4(reg
) | IMM12(imm
));
639 return push_inst32(compiler
, SUBWI
| RD4(dst
) | RN4(reg
) | IMM12(nimm
));
642 if (nimm
!= INVALID_IMM
)
643 return push_inst32(compiler
, ADD_WI
| (flags
& SET_FLAGS
) | RD4(dst
) | RN4(reg
) | nimm
);
644 nimm
= get_imm(NEGATE(imm
));
645 if (nimm
!= INVALID_IMM
)
646 return push_inst32(compiler
, SUB_WI
| (flags
& SET_FLAGS
) | RD4(dst
) | RN4(reg
) | nimm
);
650 if (imm
!= INVALID_IMM
)
651 return push_inst32(compiler
, ADCI
| (flags
& SET_FLAGS
) | RD4(dst
) | RN4(reg
) | imm
);
654 /* SUB operation can be replaced by ADD because of the negative carry flag. */
655 compiler
->status_flags_state
= SLJIT_CURRENT_FLAGS_ADD_SUB
;
656 if (flags
& ARG1_IMM
) {
657 if (imm
== 0 && IS_2_LO_REGS(reg
, dst
))
658 return push_inst16(compiler
, RSBSI
| RD3(dst
) | RN3(reg
));
660 if (imm
!= INVALID_IMM
)
661 return push_inst32(compiler
, RSB_WI
| (flags
& SET_FLAGS
) | RD4(dst
) | RN4(reg
) | imm
);
664 if (flags
& UNUSED_RETURN
) {
665 if (imm
<= 0xff && reg_map
[reg
] <= 7)
666 return push_inst16(compiler
, CMPI
| IMM8(imm
) | RDN3(reg
));
668 if (nimm
!= INVALID_IMM
)
669 return push_inst32(compiler
, CMPI_W
| RN4(reg
) | nimm
);
670 nimm
= get_imm(NEGATE(imm
));
671 if (nimm
!= INVALID_IMM
)
672 return push_inst32(compiler
, CMNI_W
| RN4(reg
) | nimm
);
675 if (IS_2_LO_REGS(reg
, dst
)) {
677 return push_inst16(compiler
, SUBSI3
| IMM3(imm
) | RD3(dst
) | RN3(reg
));
679 return push_inst16(compiler
, ADDSI3
| IMM3(nimm
) | RD3(dst
) | RN3(reg
));
682 return push_inst16(compiler
, SUBSI8
| IMM8(imm
) | RDN3(dst
));
684 return push_inst16(compiler
, ADDSI8
| IMM8(nimm
) | RDN3(dst
));
687 if (!(flags
& SET_FLAGS
)) {
689 return push_inst32(compiler
, SUBWI
| RD4(dst
) | RN4(reg
) | IMM12(imm
));
691 return push_inst32(compiler
, ADDWI
| RD4(dst
) | RN4(reg
) | IMM12(nimm
));
694 if (nimm
!= INVALID_IMM
)
695 return push_inst32(compiler
, SUB_WI
| (flags
& SET_FLAGS
) | RD4(dst
) | RN4(reg
) | nimm
);
696 nimm
= get_imm(NEGATE(imm
));
697 if (nimm
!= INVALID_IMM
)
698 return push_inst32(compiler
, ADD_WI
| (flags
& SET_FLAGS
) | RD4(dst
) | RN4(reg
) | nimm
);
701 if (flags
& ARG1_IMM
)
704 if (imm
!= INVALID_IMM
)
705 return push_inst32(compiler
, SBCI
| (flags
& SET_FLAGS
) | RD4(dst
) | RN4(reg
) | imm
);
709 if (nimm
!= INVALID_IMM
)
710 return push_inst32(compiler
, ((flags
& UNUSED_RETURN
) ? TSTI
: ANDI
) | (flags
& SET_FLAGS
) | RD4(dst
) | RN4(reg
) | nimm
);
712 if (imm
!= INVALID_IMM
)
713 return push_inst32(compiler
, BICI
| (flags
& SET_FLAGS
) | RD4(dst
) | RN4(reg
) | imm
);
717 if (nimm
!= INVALID_IMM
)
718 return push_inst32(compiler
, ORRI
| (flags
& SET_FLAGS
) | RD4(dst
) | RN4(reg
) | nimm
);
720 if (imm
!= INVALID_IMM
)
721 return push_inst32(compiler
, ORNI
| (flags
& SET_FLAGS
) | RD4(dst
) | RN4(reg
) | imm
);
725 if (imm
!= INVALID_IMM
)
726 return push_inst32(compiler
, EORI
| (flags
& SET_FLAGS
) | RD4(dst
) | RN4(reg
) | imm
);
731 if (flags
& ARG1_IMM
)
735 if (!(flags
& SET_FLAGS
))
736 return push_inst16(compiler
, MOV
| SET_REGS44(dst
, reg
));
737 if (IS_2_LO_REGS(dst
, reg
))
738 return push_inst16(compiler
, MOVS
| RD3(dst
) | RN3(reg
));
739 return push_inst32(compiler
, MOV_W
| SET_FLAGS
| RD4(dst
) | RM4(reg
));
741 switch (flags
& 0xffff) {
743 if (IS_2_LO_REGS(dst
, reg
))
744 return push_inst16(compiler
, LSLSI
| RD3(dst
) | RN3(reg
) | (imm
<< 6));
745 return push_inst32(compiler
, LSL_WI
| (flags
& SET_FLAGS
) | RD4(dst
) | RM4(reg
) | IMM5(imm
));
747 if (IS_2_LO_REGS(dst
, reg
))
748 return push_inst16(compiler
, LSRSI
| RD3(dst
) | RN3(reg
) | (imm
<< 6));
749 return push_inst32(compiler
, LSR_WI
| (flags
& SET_FLAGS
) | RD4(dst
) | RM4(reg
) | IMM5(imm
));
750 default: /* SLJIT_ASHR */
751 if (IS_2_LO_REGS(dst
, reg
))
752 return push_inst16(compiler
, ASRSI
| RD3(dst
) | RN3(reg
) | (imm
<< 6));
753 return push_inst32(compiler
, ASR_WI
| (flags
& SET_FLAGS
) | RD4(dst
) | RM4(reg
) | IMM5(imm
));
760 if (flags
& ARG2_IMM
) {
762 arg2
= (arg1
== TMP_REG1
) ? TMP_REG2
: TMP_REG1
;
763 FAIL_IF(load_immediate(compiler
, (sljit_s32
)arg2
, imm
));
767 arg1
= (arg2
== TMP_REG1
) ? TMP_REG2
: TMP_REG1
;
768 FAIL_IF(load_immediate(compiler
, (sljit_s32
)arg1
, imm
));
771 SLJIT_ASSERT(arg1
!= arg2
);
774 /* Both arguments are registers. */
775 switch (flags
& 0xffff) {
781 SLJIT_ASSERT(!(flags
& SET_FLAGS
) && arg1
== TMP_REG2
);
782 if (dst
== (sljit_s32
)arg2
)
783 return SLJIT_SUCCESS
;
784 return push_inst16(compiler
, MOV
| SET_REGS44(dst
, arg2
));
786 SLJIT_ASSERT(!(flags
& SET_FLAGS
) && arg1
== TMP_REG2
);
787 if (IS_2_LO_REGS(dst
, arg2
))
788 return push_inst16(compiler
, UXTB
| RD3(dst
) | RN3(arg2
));
789 return push_inst32(compiler
, UXTB_W
| RD4(dst
) | RM4(arg2
));
791 SLJIT_ASSERT(!(flags
& SET_FLAGS
) && arg1
== TMP_REG2
);
792 if (IS_2_LO_REGS(dst
, arg2
))
793 return push_inst16(compiler
, SXTB
| RD3(dst
) | RN3(arg2
));
794 return push_inst32(compiler
, SXTB_W
| RD4(dst
) | RM4(arg2
));
796 SLJIT_ASSERT(!(flags
& SET_FLAGS
) && arg1
== TMP_REG2
);
797 if (IS_2_LO_REGS(dst
, arg2
))
798 return push_inst16(compiler
, UXTH
| RD3(dst
) | RN3(arg2
));
799 return push_inst32(compiler
, UXTH_W
| RD4(dst
) | RM4(arg2
));
801 SLJIT_ASSERT(!(flags
& SET_FLAGS
) && arg1
== TMP_REG2
);
802 if (IS_2_LO_REGS(dst
, arg2
))
803 return push_inst16(compiler
, SXTH
| RD3(dst
) | RN3(arg2
));
804 return push_inst32(compiler
, SXTH_W
| RD4(dst
) | RM4(arg2
));
806 SLJIT_ASSERT(arg1
== TMP_REG2
);
807 if (IS_2_LO_REGS(dst
, arg2
))
808 return push_inst16(compiler
, MVNS
| RD3(dst
) | RN3(arg2
));
809 return push_inst32(compiler
, MVN_W
| (flags
& SET_FLAGS
) | RD4(dst
) | RM4(arg2
));
811 SLJIT_ASSERT(arg1
== TMP_REG2
);
812 FAIL_IF(push_inst32(compiler
, CLZ
| RN4(arg2
) | RD4(dst
) | RM4(arg2
)));
813 return SLJIT_SUCCESS
;
815 compiler
->status_flags_state
= SLJIT_CURRENT_FLAGS_ADD_SUB
;
816 if (IS_3_LO_REGS(dst
, arg1
, arg2
))
817 return push_inst16(compiler
, ADDS
| RD3(dst
) | RN3(arg1
) | RM3(arg2
));
818 if (dst
== (sljit_s32
)arg1
&& !(flags
& SET_FLAGS
))
819 return push_inst16(compiler
, ADD
| SET_REGS44(dst
, arg2
));
820 return push_inst32(compiler
, ADD_W
| (flags
& SET_FLAGS
) | RD4(dst
) | RN4(arg1
) | RM4(arg2
));
822 if (dst
== (sljit_s32
)arg1
&& IS_2_LO_REGS(dst
, arg2
))
823 return push_inst16(compiler
, ADCS
| RD3(dst
) | RN3(arg2
));
824 return push_inst32(compiler
, ADC_W
| (flags
& SET_FLAGS
) | RD4(dst
) | RN4(arg1
) | RM4(arg2
));
826 compiler
->status_flags_state
= SLJIT_CURRENT_FLAGS_ADD_SUB
;
827 if (flags
& UNUSED_RETURN
) {
828 if (IS_2_LO_REGS(arg1
, arg2
))
829 return push_inst16(compiler
, CMP
| RD3(arg1
) | RN3(arg2
));
830 return push_inst16(compiler
, CMP_X
| SET_REGS44(arg1
, arg2
));
832 if (IS_3_LO_REGS(dst
, arg1
, arg2
))
833 return push_inst16(compiler
, SUBS
| RD3(dst
) | RN3(arg1
) | RM3(arg2
));
834 return push_inst32(compiler
, SUB_W
| (flags
& SET_FLAGS
) | RD4(dst
) | RN4(arg1
) | RM4(arg2
));
836 if (dst
== (sljit_s32
)arg1
&& IS_2_LO_REGS(dst
, arg2
))
837 return push_inst16(compiler
, SBCS
| RD3(dst
) | RN3(arg2
));
838 return push_inst32(compiler
, SBC_W
| (flags
& SET_FLAGS
) | RD4(dst
) | RN4(arg1
) | RM4(arg2
));
840 compiler
->status_flags_state
= 0;
841 if (!(flags
& SET_FLAGS
))
842 return push_inst32(compiler
, MUL
| RD4(dst
) | RN4(arg1
) | RM4(arg2
));
843 SLJIT_ASSERT(dst
!= TMP_REG2
);
844 FAIL_IF(push_inst32(compiler
, SMULL
| RT4(dst
) | RD4(TMP_REG2
) | RN4(arg1
) | RM4(arg2
)));
845 /* cmp TMP_REG2, dst asr #31. */
846 return push_inst32(compiler
, CMP_W
| RN4(TMP_REG2
) | 0x70e0 | RM4(dst
));
848 if (dst
== (sljit_s32
)arg1
&& IS_2_LO_REGS(dst
, arg2
))
849 return push_inst16(compiler
, ANDS
| RD3(dst
) | RN3(arg2
));
850 if ((flags
& UNUSED_RETURN
) && IS_2_LO_REGS(arg1
, arg2
))
851 return push_inst16(compiler
, TST
| RD3(arg1
) | RN3(arg2
));
852 return push_inst32(compiler
, ((flags
& UNUSED_RETURN
) ? TST_W
: AND_W
) | (flags
& SET_FLAGS
) | RD4(dst
) | RN4(arg1
) | RM4(arg2
));
854 if (dst
== (sljit_s32
)arg1
&& IS_2_LO_REGS(dst
, arg2
))
855 return push_inst16(compiler
, ORRS
| RD3(dst
) | RN3(arg2
));
856 return push_inst32(compiler
, ORR_W
| (flags
& SET_FLAGS
) | RD4(dst
) | RN4(arg1
) | RM4(arg2
));
858 if (dst
== (sljit_s32
)arg1
&& IS_2_LO_REGS(dst
, arg2
))
859 return push_inst16(compiler
, EORS
| RD3(dst
) | RN3(arg2
));
860 return push_inst32(compiler
, EOR_W
| (flags
& SET_FLAGS
) | RD4(dst
) | RN4(arg1
) | RM4(arg2
));
862 if (dst
== (sljit_s32
)arg1
&& IS_2_LO_REGS(dst
, arg2
))
863 return push_inst16(compiler
, LSLS
| RD3(dst
) | RN3(arg2
));
864 return push_inst32(compiler
, LSL_W
| (flags
& SET_FLAGS
) | RD4(dst
) | RN4(arg1
) | RM4(arg2
));
866 if (dst
== (sljit_s32
)arg1
&& IS_2_LO_REGS(dst
, arg2
))
867 return push_inst16(compiler
, LSRS
| RD3(dst
) | RN3(arg2
));
868 return push_inst32(compiler
, LSR_W
| (flags
& SET_FLAGS
) | RD4(dst
) | RN4(arg1
) | RM4(arg2
));
870 if (dst
== (sljit_s32
)arg1
&& IS_2_LO_REGS(dst
, arg2
))
871 return push_inst16(compiler
, ASRS
| RD3(dst
) | RN3(arg2
));
872 return push_inst32(compiler
, ASR_W
| (flags
& SET_FLAGS
) | RD4(dst
) | RN4(arg1
) | RM4(arg2
));
876 return SLJIT_SUCCESS
;
882 #define WORD_SIZE 0x00
883 #define BYTE_SIZE 0x04
884 #define HALF_SIZE 0x08
887 #define IS_WORD_SIZE(flags) (!(flags & (BYTE_SIZE | HALF_SIZE)))
888 #define OFFSET_CHECK(imm, shift) (!(argw & ~(imm << shift)))
905 static const sljit_ins sljit_mem16
[12] = {
906 /* w u l */ 0x5800 /* ldr */,
907 /* w u s */ 0x5000 /* str */,
908 /* w s l */ 0x5800 /* ldr */,
909 /* w s s */ 0x5000 /* str */,
911 /* b u l */ 0x5c00 /* ldrb */,
912 /* b u s */ 0x5400 /* strb */,
913 /* b s l */ 0x5600 /* ldrsb */,
914 /* b s s */ 0x5400 /* strb */,
916 /* h u l */ 0x5a00 /* ldrh */,
917 /* h u s */ 0x5200 /* strh */,
918 /* h s l */ 0x5e00 /* ldrsh */,
919 /* h s s */ 0x5200 /* strh */,
922 static const sljit_ins sljit_mem16_imm5
[12] = {
923 /* w u l */ 0x6800 /* ldr imm5 */,
924 /* w u s */ 0x6000 /* str imm5 */,
925 /* w s l */ 0x6800 /* ldr imm5 */,
926 /* w s s */ 0x6000 /* str imm5 */,
928 /* b u l */ 0x7800 /* ldrb imm5 */,
929 /* b u s */ 0x7000 /* strb imm5 */,
930 /* b s l */ 0x0000 /* not allowed */,
931 /* b s s */ 0x7000 /* strb imm5 */,
933 /* h u l */ 0x8800 /* ldrh imm5 */,
934 /* h u s */ 0x8000 /* strh imm5 */,
935 /* h s l */ 0x0000 /* not allowed */,
936 /* h s s */ 0x8000 /* strh imm5 */,
939 #define MEM_IMM8 0xc00
940 #define MEM_IMM12 0x800000
941 static const sljit_ins sljit_mem32
[13] = {
942 /* w u l */ 0xf8500000 /* ldr.w */,
943 /* w u s */ 0xf8400000 /* str.w */,
944 /* w s l */ 0xf8500000 /* ldr.w */,
945 /* w s s */ 0xf8400000 /* str.w */,
947 /* b u l */ 0xf8100000 /* ldrb.w */,
948 /* b u s */ 0xf8000000 /* strb.w */,
949 /* b s l */ 0xf9100000 /* ldrsb.w */,
950 /* b s s */ 0xf8000000 /* strb.w */,
952 /* h u l */ 0xf8300000 /* ldrh.w */,
953 /* h u s */ 0xf8200000 /* strsh.w */,
954 /* h s l */ 0xf9300000 /* ldrsh.w */,
955 /* h s s */ 0xf8200000 /* strsh.w */,
957 /* p u l */ 0xf8100000 /* pld */,
960 /* Helper function. Dst should be reg + value, using at most 1 instruction, flags does not set. */
961 static sljit_s32
emit_set_delta(struct sljit_compiler
*compiler
, sljit_s32 dst
, sljit_s32 reg
, sljit_sw value
)
967 return push_inst32(compiler
, ADDWI
| RD4(dst
) | RN4(reg
) | IMM12(value
));
968 imm
= get_imm((sljit_uw
)value
);
969 if (imm
!= INVALID_IMM
)
970 return push_inst32(compiler
, ADD_WI
| RD4(dst
) | RN4(reg
) | imm
);
975 return push_inst32(compiler
, SUBWI
| RD4(dst
) | RN4(reg
) | IMM12(value
));
976 imm
= get_imm((sljit_uw
)value
);
977 if (imm
!= INVALID_IMM
)
978 return push_inst32(compiler
, SUB_WI
| RD4(dst
) | RN4(reg
) | imm
);
980 return SLJIT_ERR_UNSUPPORTED
;
983 static SLJIT_INLINE sljit_s32
emit_op_mem(struct sljit_compiler
*compiler
, sljit_s32 flags
, sljit_s32 reg
,
984 sljit_s32 arg
, sljit_sw argw
, sljit_s32 tmp_reg
)
989 SLJIT_ASSERT(arg
& SLJIT_MEM
);
990 SLJIT_ASSERT((arg
& REG_MASK
) != tmp_reg
);
993 if (SLJIT_UNLIKELY(!(arg
& REG_MASK
))) {
994 tmp
= get_imm((sljit_uw
)argw
& ~(sljit_uw
)0xfff);
995 if (tmp
!= INVALID_IMM
) {
996 FAIL_IF(push_inst32(compiler
, MOV_WI
| RD4(tmp_reg
) | tmp
));
997 return push_inst32(compiler
, sljit_mem32
[flags
] | MEM_IMM12
| RT4(reg
) | RN4(tmp_reg
) | (argw
& 0xfff));
1000 FAIL_IF(load_immediate(compiler
, tmp_reg
, (sljit_uw
)argw
));
1001 if (IS_2_LO_REGS(reg
, tmp_reg
) && sljit_mem16_imm5
[flags
])
1002 return push_inst16(compiler
, sljit_mem16_imm5
[flags
] | RD3(reg
) | RN3(tmp_reg
));
1003 return push_inst32(compiler
, sljit_mem32
[flags
] | MEM_IMM12
| RT4(reg
) | RN4(tmp_reg
));
1006 if (SLJIT_UNLIKELY(arg
& OFFS_REG_MASK
)) {
1008 other_r
= OFFS_REG(arg
);
1011 if (!argw
&& IS_3_LO_REGS(reg
, arg
, other_r
))
1012 return push_inst16(compiler
, sljit_mem16
[flags
] | RD3(reg
) | RN3(arg
) | RM3(other_r
));
1013 return push_inst32(compiler
, sljit_mem32
[flags
] | RT4(reg
) | RN4(arg
) | RM4(other_r
) | ((sljit_ins
)argw
<< 4));
1017 tmp
= get_imm((sljit_uw
)argw
& ~(sljit_uw
)0xfff);
1018 if (tmp
!= INVALID_IMM
) {
1019 push_inst32(compiler
, ADD_WI
| RD4(tmp_reg
) | RN4(arg
) | tmp
);
1021 argw
= argw
& 0xfff;
1024 else if (argw
< -0xff) {
1025 tmp
= get_imm((sljit_uw
)-argw
& ~(sljit_uw
)0xff);
1026 if (tmp
!= INVALID_IMM
) {
1027 push_inst32(compiler
, SUB_WI
| RD4(tmp_reg
) | RN4(arg
) | tmp
);
1029 argw
= -(-argw
& 0xff);
1033 if (IS_2_LO_REGS(reg
, arg
) && sljit_mem16_imm5
[flags
]) {
1035 if (IS_WORD_SIZE(flags
)) {
1036 if (OFFSET_CHECK(0x1f, 2))
1039 else if (flags
& BYTE_SIZE
)
1041 if (OFFSET_CHECK(0x1f, 0))
1045 SLJIT_ASSERT(flags
& HALF_SIZE
);
1046 if (OFFSET_CHECK(0x1f, 1))
1051 return push_inst16(compiler
, sljit_mem16_imm5
[flags
] | RD3(reg
) | RN3(arg
) | ((sljit_ins
)argw
<< (6 - tmp
)));
1053 else if (SLJIT_UNLIKELY(arg
== SLJIT_SP
) && IS_WORD_SIZE(flags
) && OFFSET_CHECK(0xff, 2) && reg_map
[reg
] <= 7) {
1054 /* SP based immediate. */
1055 return push_inst16(compiler
, STR_SP
| (sljit_ins
)((flags
& STORE
) ? 0 : 0x800) | RDN3(reg
) | ((sljit_ins
)argw
>> 2));
1058 if (argw
>= 0 && argw
<= 0xfff)
1059 return push_inst32(compiler
, sljit_mem32
[flags
] | MEM_IMM12
| RT4(reg
) | RN4(arg
) | (sljit_ins
)argw
);
1060 else if (argw
< 0 && argw
>= -0xff)
1061 return push_inst32(compiler
, sljit_mem32
[flags
] | MEM_IMM8
| RT4(reg
) | RN4(arg
) | (sljit_ins
)-argw
);
1063 SLJIT_ASSERT(arg
!= tmp_reg
);
1065 FAIL_IF(load_immediate(compiler
, tmp_reg
, (sljit_uw
)argw
));
1066 if (IS_3_LO_REGS(reg
, arg
, tmp_reg
))
1067 return push_inst16(compiler
, sljit_mem16
[flags
] | RD3(reg
) | RN3(arg
) | RM3(tmp_reg
));
1068 return push_inst32(compiler
, sljit_mem32
[flags
] | RT4(reg
) | RN4(arg
) | RM4(tmp_reg
));
1071 /* --------------------------------------------------------------------- */
1073 /* --------------------------------------------------------------------- */
1075 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_enter(struct sljit_compiler
*compiler
,
1076 sljit_s32 options
, sljit_s32 arg_types
, sljit_s32 scratches
, sljit_s32 saveds
,
1077 sljit_s32 fscratches
, sljit_s32 fsaveds
, sljit_s32 local_size
)
1079 sljit_s32 size
, i
, tmp
, word_arg_count
;
1083 sljit_u32 float_arg_count
;
1085 sljit_u32 old_offset
, f32_offset
;
1087 sljit_u32
*remap_ptr
= remap
;
1094 CHECK(check_sljit_emit_enter(compiler
, options
, arg_types
, scratches
, saveds
, fscratches
, fsaveds
, local_size
));
1095 set_emit_enter(compiler
, options
, arg_types
, scratches
, saveds
, fscratches
, fsaveds
, local_size
);
1097 tmp
= saveds
< SLJIT_NUMBER_OF_SAVED_REGISTERS
? (SLJIT_S0
+ 1 - saveds
) : SLJIT_FIRST_SAVED_REG
;
1098 for (i
= SLJIT_S0
; i
>= tmp
; i
--)
1099 imm
|= (sljit_uw
)1 << reg_map
[i
];
1101 for (i
= scratches
; i
>= SLJIT_FIRST_SAVED_REG
; i
--)
1102 imm
|= (sljit_uw
)1 << reg_map
[i
];
1104 /* At least two registers must be set for PUSH_W and one for PUSH instruction. */
1105 FAIL_IF((imm
& 0xff00)
1106 ? push_inst32(compiler
, PUSH_W
| (1 << 14) | imm
)
1107 : push_inst16(compiler
, PUSH
| (1 << 8) | imm
));
1109 /* Stack must be aligned to 8 bytes: (LR, R4) */
1110 size
= GET_SAVED_REGISTERS_SIZE(scratches
, saveds
, 1);
1111 local_size
= ((size
+ local_size
+ 7) & ~7) - size
;
1112 compiler
->local_size
= local_size
;
1114 arg_types
>>= SLJIT_ARG_SHIFT
;
1117 SLJIT_COMPILE_ASSERT(SLJIT_FR0
== 1, float_register_index_start
);
1120 float_arg_count
= 0;
1123 switch (arg_types
& SLJIT_ARG_MASK
) {
1124 case SLJIT_ARG_TYPE_F64
:
1126 offset
+= sizeof(sljit_sw
);
1128 if (offset
< 4 * sizeof(sljit_sw
))
1129 FAIL_IF(push_inst32(compiler
, VMOV2
| (offset
<< 10) | ((offset
+ sizeof(sljit_sw
)) << 14) | float_arg_count
));
1131 FAIL_IF(push_inst32(compiler
, VLDR_F32
| 0x800100 | RN4(SLJIT_SP
)
1132 | (float_arg_count
<< 12) | ((offset
+ (sljit_uw
)size
- 4 * sizeof(sljit_sw
)) >> 2)));
1134 offset
+= sizeof(sljit_f64
);
1136 case SLJIT_ARG_TYPE_F32
:
1137 if (offset
< 4 * sizeof(sljit_sw
))
1138 FAIL_IF(push_inst32(compiler
, VMOV
| (float_arg_count
<< 16) | (offset
<< 10)));
1140 FAIL_IF(push_inst32(compiler
, VLDR_F32
| 0x800000 | RN4(SLJIT_SP
)
1141 | (float_arg_count
<< 12) | ((offset
+ (sljit_uw
)size
- 4 * sizeof(sljit_sw
)) >> 2)));
1143 offset
+= sizeof(sljit_f32
);
1146 SLJIT_ASSERT(reg_map
[SLJIT_S0
- word_arg_count
] <= 7);
1148 if (offset
< 4 * sizeof(sljit_sw
))
1149 FAIL_IF(push_inst16(compiler
, MOV
| RD3(SLJIT_S0
- word_arg_count
) | (offset
<< 1)));
1151 FAIL_IF(push_inst16(compiler
, LDR_SP
| RDN3(SLJIT_S0
- word_arg_count
)
1152 | ((offset
+ (sljit_uw
)size
- 4 * sizeof(sljit_sw
)) >> 2)));
1155 offset
+= sizeof(sljit_sw
);
1158 arg_types
>>= SLJIT_ARG_SHIFT
;
1161 compiler
->args_size
= offset
;
1164 old_offset
= SLJIT_FR0
;
1168 switch (arg_types
& SLJIT_ARG_MASK
) {
1169 case SLJIT_ARG_TYPE_F64
:
1170 if (offset
!= old_offset
)
1171 *remap_ptr
++ = VMOV_F32
| SLJIT_32
| DD4(offset
) | DM4(old_offset
);
1175 case SLJIT_ARG_TYPE_F32
:
1176 if (f32_offset
!= 0) {
1177 *remap_ptr
++ = VMOV_F32
| 0x20 | DD4(offset
) | DM4(f32_offset
);
1180 if (offset
!= old_offset
)
1181 *remap_ptr
++ = VMOV_F32
| DD4(offset
) | DM4(old_offset
);
1182 f32_offset
= old_offset
;
1188 FAIL_IF(push_inst16(compiler
, MOV
| SET_REGS44(SLJIT_S0
- word_arg_count
, SLJIT_R0
+ word_arg_count
)));
1192 arg_types
>>= SLJIT_ARG_SHIFT
;
1195 SLJIT_ASSERT((sljit_uw
)(remap_ptr
- remap
) <= sizeof(remap
));
1197 while (remap_ptr
> remap
)
1198 FAIL_IF(push_inst32(compiler
, *(--remap_ptr
)));
1202 if (local_size
>= 256) {
1203 if (local_size
> 4096)
1204 imm
= get_imm(4096);
1206 imm
= get_imm(local_size
& ~0xff);
1208 SLJIT_ASSERT(imm
!= INVALID_IMM
);
1209 FAIL_IF(push_inst32(compiler
, SUB_WI
| RD4(TMP_REG1
) | RN4(SLJIT_SP
) | imm
));
1212 if (local_size
> 0) {
1213 if (local_size
<= (127 << 2))
1214 FAIL_IF(push_inst16(compiler
, SUB_SP_I
| ((sljit_uw
)local_size
>> 2)));
1216 FAIL_IF(emit_op_imm(compiler
, SLJIT_SUB
| ARG2_IMM
, SLJIT_SP
, SLJIT_SP
, (sljit_uw
)local_size
));
1221 if (local_size
>= 256) {
1222 if (local_size
> 4096) {
1223 imm
= get_imm(4096);
1224 SLJIT_ASSERT(imm
!= INVALID_IMM
);
1226 if (local_size
< 4 * 4096) {
1227 if (local_size
> 2 * 4096) {
1228 FAIL_IF(push_inst32(compiler
, LDRI
| 0x400 | RT4(TMP_REG2
) | RN4(TMP_REG1
)));
1229 FAIL_IF(push_inst32(compiler
, SUB_WI
| RD4(TMP_REG1
) | RN4(TMP_REG1
) | imm
));
1233 if (local_size
> 2 * 4096) {
1234 FAIL_IF(push_inst32(compiler
, LDRI
| 0x400 | RT4(TMP_REG2
) | RN4(TMP_REG1
)));
1235 FAIL_IF(push_inst32(compiler
, SUB_WI
| RD4(TMP_REG1
) | RN4(TMP_REG1
) | imm
));
1239 FAIL_IF(push_inst32(compiler
, LDRI
| 0x400 | RT4(TMP_REG2
) | RN4(TMP_REG1
)));
1242 SLJIT_ASSERT(local_size
> 0);
1245 FAIL_IF(load_immediate(compiler
, SLJIT_R3
, (local_size
>> 12) - 1));
1246 FAIL_IF(push_inst32(compiler
, LDRI
| 0x400 | RT4(TMP_REG2
) | RN4(TMP_REG1
)));
1247 FAIL_IF(push_inst32(compiler
, SUB_WI
| RD4(TMP_REG1
) | RN4(TMP_REG1
) | imm
));
1248 SLJIT_ASSERT(reg_map
[SLJIT_R3
] < 7);
1249 FAIL_IF(push_inst16(compiler
, SUBSI8
| RDN3(SLJIT_R3
) | 1));
1250 FAIL_IF(push_inst16(compiler
, BCC
| (0x1 << 8) /* not-equal */ | (-7 & 0xff)));
1252 local_size
&= 0xfff;
1254 if (local_size
!= 0)
1255 FAIL_IF(push_inst32(compiler
, LDRI
| 0x400 | RT4(TMP_REG2
) | RN4(TMP_REG1
)));
1258 if (local_size
>= 256) {
1259 imm
= get_imm(local_size
& ~0xff);
1260 SLJIT_ASSERT(imm
!= INVALID_IMM
);
1262 FAIL_IF(push_inst32(compiler
, SUB_WI
| RD4(TMP_REG1
) | RN4(TMP_REG1
) | imm
));
1267 FAIL_IF(push_inst32(compiler
, LDRI
| 0x400 | (local_size
> 0 ? 0x100 : 0) | RT4(TMP_REG2
) | RN4(TMP_REG1
) | local_size
));
1269 FAIL_IF(push_inst16(compiler
, MOV
| SET_REGS44(SLJIT_SP
, TMP_REG1
)));
1271 else if (local_size
> 0)
1272 FAIL_IF(push_inst32(compiler
, LDRI
| 0x500 | RT4(TMP_REG1
) | RN4(SLJIT_SP
) | local_size
));
1275 return SLJIT_SUCCESS
;
1278 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_set_context(struct sljit_compiler
*compiler
,
1279 sljit_s32 options
, sljit_s32 arg_types
, sljit_s32 scratches
, sljit_s32 saveds
,
1280 sljit_s32 fscratches
, sljit_s32 fsaveds
, sljit_s32 local_size
)
1285 CHECK(check_sljit_set_context(compiler
, options
, arg_types
, scratches
, saveds
, fscratches
, fsaveds
, local_size
));
1286 set_set_context(compiler
, options
, arg_types
, scratches
, saveds
, fscratches
, fsaveds
, local_size
);
1288 size
= GET_SAVED_REGISTERS_SIZE(scratches
, saveds
, 1);
1289 compiler
->local_size
= ((size
+ local_size
+ 7) & ~7) - size
;
1290 return SLJIT_SUCCESS
;
1293 static sljit_s32
emit_add_sp(struct sljit_compiler
*compiler
, sljit_uw imm
)
1297 /* The TMP_REG1 register must keep its value. */
1298 if (imm
<= (127u << 2))
1299 return push_inst16(compiler
, ADD_SP_I
| (imm
>> 2));
1302 return push_inst32(compiler
, ADDWI
| RD4(SLJIT_SP
) | RN4(SLJIT_SP
) | IMM12(imm
));
1304 imm2
= get_imm(imm
);
1306 if (imm2
!= INVALID_IMM
)
1307 return push_inst32(compiler
, ADD_WI
| RD4(SLJIT_SP
) | RN4(SLJIT_SP
) | imm2
);
1309 FAIL_IF(load_immediate(compiler
, TMP_REG2
, imm
));
1310 return push_inst16(compiler
, ADD_SP
| RN3(TMP_REG2
));
1313 static sljit_s32
emit_stack_frame_release(struct sljit_compiler
*compiler
, sljit_s32 frame_size
)
1316 sljit_s32 lr_dst
= TMP_PC
;
1319 SLJIT_ASSERT(reg_map
[TMP_REG2
] == 14 && frame_size
<= 128);
1321 if (frame_size
< 0) {
1324 } else if (frame_size
> 0)
1328 tmp
= compiler
->saveds
< SLJIT_NUMBER_OF_SAVED_REGISTERS
? (SLJIT_S0
+ 1 - compiler
->saveds
) : SLJIT_FIRST_SAVED_REG
;
1329 for (i
= SLJIT_S0
; i
>= tmp
; i
--)
1330 reg_list
|= (sljit_uw
)1 << reg_map
[i
];
1332 for (i
= compiler
->scratches
; i
>= SLJIT_FIRST_SAVED_REG
; i
--)
1333 reg_list
|= (sljit_uw
)1 << reg_map
[i
];
1335 tmp
= compiler
->local_size
;
1336 if (lr_dst
== 0 && (reg_list
& (reg_list
- 1)) == 0) {
1337 /* The local_size does not include the saved registers. */
1338 tmp
+= SSIZE_OF(sw
);
1341 tmp
+= SSIZE_OF(sw
);
1343 if (frame_size
> tmp
)
1344 FAIL_IF(push_inst16(compiler
, SUB_SP_I
| ((sljit_uw
)(frame_size
- tmp
) >> 2)));
1345 else if (frame_size
< tmp
)
1346 FAIL_IF(emit_add_sp(compiler
, (sljit_uw
)(tmp
- frame_size
)));
1349 return SLJIT_SUCCESS
;
1351 if (compiler
->saveds
> 0) {
1352 SLJIT_ASSERT(reg_list
== ((sljit_uw
)1 << reg_map
[SLJIT_S0
]));
1355 SLJIT_ASSERT(reg_list
== ((sljit_uw
)1 << reg_map
[SLJIT_FIRST_SAVED_REG
]));
1356 lr_dst
= SLJIT_FIRST_SAVED_REG
;
1359 frame_size
-= 2 * SSIZE_OF(sw
);
1361 if (reg_map
[lr_dst
] <= 7)
1362 return push_inst16(compiler
, STR_SP
| 0x800 | RDN3(lr_dst
) | (sljit_uw
)(frame_size
>> 2));
1364 return push_inst32(compiler
, LDR
| RT4(lr_dst
) | RN4(SLJIT_SP
) | (sljit_uw
)frame_size
);
1368 FAIL_IF(emit_add_sp(compiler
, (sljit_uw
)tmp
));
1370 if (!(reg_list
& 0xff00) && lr_dst
!= TMP_REG2
) {
1371 if (lr_dst
== TMP_PC
)
1372 reg_list
|= 1u << 8;
1375 return SLJIT_SUCCESS
;
1377 /* At least one register must be set for POP instruction. */
1378 FAIL_IF(push_inst16(compiler
, POP
| reg_list
));
1382 return push_inst32(compiler
, 0xf85d0b04 | RT4(lr_dst
));
1384 reg_list
|= (sljit_uw
)1 << reg_map
[lr_dst
];
1387 SLJIT_ASSERT((reg_list
& (reg_list
- 1)) != 0);
1389 /* At least two registers must be set for POP_W instruction. */
1390 FAIL_IF(push_inst32(compiler
, POP_W
| reg_list
));
1394 return push_inst16(compiler
, SUB_SP_I
| (((sljit_uw
)frame_size
- sizeof(sljit_sw
)) >> 2));
1395 return SLJIT_SUCCESS
;
1398 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_return_void(struct sljit_compiler
*compiler
)
1401 CHECK(check_sljit_emit_return_void(compiler
));
1403 return emit_stack_frame_release(compiler
, 0);
1406 /* --------------------------------------------------------------------- */
1408 /* --------------------------------------------------------------------- */
1410 #if !(defined __ARM_FEATURE_IDIV) && !(defined __ARM_ARCH_EXT_IDIV__)
1417 extern unsigned long long __rt_udiv(unsigned int denominator
, unsigned int numerator
);
1418 extern long long __rt_sdiv(int denominator
, int numerator
);
1419 #elif defined(__GNUC__)
1420 extern unsigned int __aeabi_uidivmod(unsigned int numerator
, int unsigned denominator
);
1421 extern int __aeabi_idivmod(int numerator
, int denominator
);
1423 #error "Software divmod functions are needed"
1430 #endif /* !__ARM_FEATURE_IDIV && !__ARM_ARCH_EXT_IDIV__ */
1432 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op0(struct sljit_compiler
*compiler
, sljit_s32 op
)
1434 #if !(defined __ARM_FEATURE_IDIV) && !(defined __ARM_ARCH_EXT_IDIV__)
1435 sljit_uw saved_reg_list
[3];
1436 sljit_uw saved_reg_count
;
1440 CHECK(check_sljit_emit_op0(compiler
, op
));
1442 op
= GET_OPCODE(op
);
1444 case SLJIT_BREAKPOINT
:
1445 return push_inst16(compiler
, BKPT
);
1447 return push_inst16(compiler
, NOP
);
1450 return push_inst32(compiler
, (op
== SLJIT_LMUL_UW
? UMULL
: SMULL
)
1451 | RD4(SLJIT_R1
) | RT4(SLJIT_R0
) | RN4(SLJIT_R0
) | RM4(SLJIT_R1
));
1452 #if (defined __ARM_FEATURE_IDIV) || (defined __ARM_ARCH_EXT_IDIV__)
1453 case SLJIT_DIVMOD_UW
:
1454 case SLJIT_DIVMOD_SW
:
1455 FAIL_IF(push_inst16(compiler
, MOV
| SET_REGS44(TMP_REG1
, SLJIT_R0
)));
1456 FAIL_IF(push_inst32(compiler
, (op
== SLJIT_DIVMOD_UW
? UDIV
: SDIV
) | RD4(SLJIT_R0
) | RN4(SLJIT_R0
) | RM4(SLJIT_R1
)));
1457 FAIL_IF(push_inst32(compiler
, MUL
| RD4(SLJIT_R1
) | RN4(SLJIT_R0
) | RM4(SLJIT_R1
)));
1458 return push_inst32(compiler
, SUB_W
| RD4(SLJIT_R1
) | RN4(TMP_REG1
) | RM4(SLJIT_R1
));
1461 return push_inst32(compiler
, (op
== SLJIT_DIV_UW
? UDIV
: SDIV
) | RD4(SLJIT_R0
) | RN4(SLJIT_R0
) | RM4(SLJIT_R1
));
1462 #else /* !__ARM_FEATURE_IDIV && !__ARM_ARCH_EXT_IDIV__ */
1463 case SLJIT_DIVMOD_UW
:
1464 case SLJIT_DIVMOD_SW
:
1467 SLJIT_COMPILE_ASSERT((SLJIT_DIVMOD_UW
& 0x2) == 0 && SLJIT_DIV_UW
- 0x2 == SLJIT_DIVMOD_UW
, bad_div_opcode_assignments
);
1468 SLJIT_ASSERT(reg_map
[2] == 1 && reg_map
[3] == 2 && reg_map
[4] == 3);
1470 saved_reg_count
= 0;
1471 if (compiler
->scratches
>= 4)
1472 saved_reg_list
[saved_reg_count
++] = 3;
1473 if (compiler
->scratches
>= 3)
1474 saved_reg_list
[saved_reg_count
++] = 2;
1475 if (op
>= SLJIT_DIV_UW
)
1476 saved_reg_list
[saved_reg_count
++] = 1;
1478 if (saved_reg_count
> 0) {
1479 FAIL_IF(push_inst32(compiler
, 0xf84d0d00 | (saved_reg_count
>= 3 ? 16 : 8)
1480 | (saved_reg_list
[0] << 12) /* str rX, [sp, #-8/-16]! */));
1481 if (saved_reg_count
>= 2) {
1482 SLJIT_ASSERT(saved_reg_list
[1] < 8);
1483 FAIL_IF(push_inst16(compiler
, 0x9001 | (saved_reg_list
[1] << 8) /* str rX, [sp, #4] */));
1485 if (saved_reg_count
>= 3) {
1486 SLJIT_ASSERT(saved_reg_list
[2] < 8);
1487 FAIL_IF(push_inst16(compiler
, 0x9002 | (saved_reg_list
[2] << 8) /* str rX, [sp, #8] */));
1492 FAIL_IF(push_inst16(compiler
, MOV
| SET_REGS44(TMP_REG1
, SLJIT_R0
)));
1493 FAIL_IF(push_inst16(compiler
, MOV
| SET_REGS44(SLJIT_R0
, SLJIT_R1
)));
1494 FAIL_IF(push_inst16(compiler
, MOV
| SET_REGS44(SLJIT_R1
, TMP_REG1
)));
1495 FAIL_IF(sljit_emit_ijump(compiler
, SLJIT_FAST_CALL
, SLJIT_IMM
,
1496 ((op
| 0x2) == SLJIT_DIV_UW
? SLJIT_FUNC_ADDR(__rt_udiv
) : SLJIT_FUNC_ADDR(__rt_sdiv
))));
1497 #elif defined(__GNUC__)
1498 FAIL_IF(sljit_emit_ijump(compiler
, SLJIT_FAST_CALL
, SLJIT_IMM
,
1499 ((op
| 0x2) == SLJIT_DIV_UW
? SLJIT_FUNC_ADDR(__aeabi_uidivmod
) : SLJIT_FUNC_ADDR(__aeabi_idivmod
))));
1501 #error "Software divmod functions are needed"
1504 if (saved_reg_count
> 0) {
1505 if (saved_reg_count
>= 3) {
1506 SLJIT_ASSERT(saved_reg_list
[2] < 8);
1507 FAIL_IF(push_inst16(compiler
, 0x9802 | (saved_reg_list
[2] << 8) /* ldr rX, [sp, #8] */));
1509 if (saved_reg_count
>= 2) {
1510 SLJIT_ASSERT(saved_reg_list
[1] < 8);
1511 FAIL_IF(push_inst16(compiler
, 0x9801 | (saved_reg_list
[1] << 8) /* ldr rX, [sp, #4] */));
1513 return push_inst32(compiler
, 0xf85d0b00 | (saved_reg_count
>= 3 ? 16 : 8)
1514 | (saved_reg_list
[0] << 12) /* ldr rX, [sp], #8/16 */);
1516 return SLJIT_SUCCESS
;
1517 #endif /* __ARM_FEATURE_IDIV || __ARM_ARCH_EXT_IDIV__ */
1519 case SLJIT_SKIP_FRAMES_BEFORE_RETURN
:
1520 return SLJIT_SUCCESS
;
1523 return SLJIT_SUCCESS
;
1526 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op1(struct sljit_compiler
*compiler
, sljit_s32 op
,
1527 sljit_s32 dst
, sljit_sw dstw
,
1528 sljit_s32 src
, sljit_sw srcw
)
1530 sljit_s32 dst_r
, flags
;
1531 sljit_s32 op_flags
= GET_ALL_FLAGS(op
);
1534 CHECK(check_sljit_emit_op1(compiler
, op
, dst
, dstw
, src
, srcw
));
1535 ADJUST_LOCAL_OFFSET(dst
, dstw
);
1536 ADJUST_LOCAL_OFFSET(src
, srcw
);
1538 dst_r
= FAST_IS_REG(dst
) ? dst
: TMP_REG1
;
1540 op
= GET_OPCODE(op
);
1541 if (op
>= SLJIT_MOV
&& op
<= SLJIT_MOV_P
) {
1552 if (src
& SLJIT_IMM
)
1553 srcw
= (sljit_u8
)srcw
;
1556 flags
= BYTE_SIZE
| SIGNED
;
1557 if (src
& SLJIT_IMM
)
1558 srcw
= (sljit_s8
)srcw
;
1562 if (src
& SLJIT_IMM
)
1563 srcw
= (sljit_u16
)srcw
;
1566 flags
= HALF_SIZE
| SIGNED
;
1567 if (src
& SLJIT_IMM
)
1568 srcw
= (sljit_s16
)srcw
;
1571 SLJIT_UNREACHABLE();
1576 if (src
& SLJIT_IMM
)
1577 FAIL_IF(emit_op_imm(compiler
, SLJIT_MOV
| ARG2_IMM
, dst_r
, TMP_REG2
, (sljit_uw
)srcw
));
1578 else if (src
& SLJIT_MEM
) {
1579 FAIL_IF(emit_op_mem(compiler
, flags
, dst_r
, src
, srcw
, TMP_REG1
));
1581 if (dst_r
!= TMP_REG1
)
1582 return emit_op_imm(compiler
, op
, dst_r
, TMP_REG2
, (sljit_uw
)src
);
1586 if (!(dst
& SLJIT_MEM
))
1587 return SLJIT_SUCCESS
;
1589 return emit_op_mem(compiler
, flags
| STORE
, dst_r
, dst
, dstw
, TMP_REG2
);
1592 if (op
== SLJIT_NEG
) {
1593 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
1594 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
1595 compiler
->skip_checks
= 1;
1597 return sljit_emit_op2(compiler
, SLJIT_SUB
| op_flags
, dst
, dstw
, SLJIT_IMM
, 0, src
, srcw
);
1600 flags
= HAS_FLAGS(op_flags
) ? SET_FLAGS
: 0;
1602 if (src
& SLJIT_MEM
) {
1603 FAIL_IF(emit_op_mem(compiler
, WORD_SIZE
, TMP_REG1
, src
, srcw
, TMP_REG1
));
1607 emit_op_imm(compiler
, flags
| op
, dst_r
, TMP_REG2
, (sljit_uw
)src
);
1609 if (SLJIT_UNLIKELY(dst
& SLJIT_MEM
))
1610 return emit_op_mem(compiler
, flags
| STORE
, dst_r
, dst
, dstw
, TMP_REG2
);
1611 return SLJIT_SUCCESS
;
1614 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op2(struct sljit_compiler
*compiler
, sljit_s32 op
,
1615 sljit_s32 dst
, sljit_sw dstw
,
1616 sljit_s32 src1
, sljit_sw src1w
,
1617 sljit_s32 src2
, sljit_sw src2w
)
1619 sljit_s32 dst_reg
, flags
, src2_reg
;
1622 CHECK(check_sljit_emit_op2(compiler
, op
, 0, dst
, dstw
, src1
, src1w
, src2
, src2w
));
1623 ADJUST_LOCAL_OFFSET(dst
, dstw
);
1624 ADJUST_LOCAL_OFFSET(src1
, src1w
);
1625 ADJUST_LOCAL_OFFSET(src2
, src2w
);
1627 dst_reg
= FAST_IS_REG(dst
) ? dst
: TMP_REG1
;
1628 flags
= HAS_FLAGS(op
) ? SET_FLAGS
: 0;
1630 if (dst
== TMP_REG1
)
1631 flags
|= UNUSED_RETURN
;
1633 if (src1
& SLJIT_IMM
)
1635 else if (src1
& SLJIT_MEM
) {
1636 emit_op_mem(compiler
, WORD_SIZE
, TMP_REG1
, src1
, src1w
, TMP_REG1
);
1642 if (src2
& SLJIT_IMM
)
1644 else if (src2
& SLJIT_MEM
) {
1645 src2_reg
= (!(flags
& ARG1_IMM
) && (src1w
== TMP_REG1
)) ? TMP_REG2
: TMP_REG1
;
1646 emit_op_mem(compiler
, WORD_SIZE
, src2_reg
, src2
, src2w
, src2_reg
);
1652 emit_op_imm(compiler
, flags
| GET_OPCODE(op
), dst_reg
, (sljit_uw
)src1w
, (sljit_uw
)src2w
);
1654 if (!(dst
& SLJIT_MEM
))
1655 return SLJIT_SUCCESS
;
1656 return emit_op_mem(compiler
, WORD_SIZE
| STORE
, dst_reg
, dst
, dstw
, TMP_REG2
);
1659 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op2u(struct sljit_compiler
*compiler
, sljit_s32 op
,
1660 sljit_s32 src1
, sljit_sw src1w
,
1661 sljit_s32 src2
, sljit_sw src2w
)
1664 CHECK(check_sljit_emit_op2(compiler
, op
, 1, 0, 0, src1
, src1w
, src2
, src2w
));
1666 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
1667 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
1668 compiler
->skip_checks
= 1;
1670 return sljit_emit_op2(compiler
, op
, TMP_REG1
, 0, src1
, src1w
, src2
, src2w
);
1673 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op_src(struct sljit_compiler
*compiler
, sljit_s32 op
,
1674 sljit_s32 src
, sljit_sw srcw
)
1677 CHECK(check_sljit_emit_op_src(compiler
, op
, src
, srcw
));
1678 ADJUST_LOCAL_OFFSET(src
, srcw
);
1681 case SLJIT_FAST_RETURN
:
1682 SLJIT_ASSERT(reg_map
[TMP_REG2
] == 14);
1684 if (FAST_IS_REG(src
))
1685 FAIL_IF(push_inst16(compiler
, MOV
| SET_REGS44(TMP_REG2
, src
)));
1687 FAIL_IF(emit_op_mem(compiler
, WORD_SIZE
, TMP_REG2
, src
, srcw
, TMP_REG2
));
1689 return push_inst16(compiler
, BX
| RN3(TMP_REG2
));
1690 case SLJIT_SKIP_FRAMES_BEFORE_FAST_RETURN
:
1691 return SLJIT_SUCCESS
;
1692 case SLJIT_PREFETCH_L1
:
1693 case SLJIT_PREFETCH_L2
:
1694 case SLJIT_PREFETCH_L3
:
1695 case SLJIT_PREFETCH_ONCE
:
1696 return emit_op_mem(compiler
, PRELOAD
, TMP_PC
, src
, srcw
, TMP_REG1
);
1699 return SLJIT_SUCCESS
;
1702 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_get_register_index(sljit_s32 reg
)
1704 CHECK_REG_INDEX(check_sljit_get_register_index(reg
));
1705 return reg_map
[reg
];
1708 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_get_float_register_index(sljit_s32 reg
)
1710 CHECK_REG_INDEX(check_sljit_get_float_register_index(reg
));
1711 return (freg_map
[reg
] << 1);
1714 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op_custom(struct sljit_compiler
*compiler
,
1715 void *instruction
, sljit_u32 size
)
1718 CHECK(check_sljit_emit_op_custom(compiler
, instruction
, size
));
1721 return push_inst16(compiler
, *(sljit_u16
*)instruction
);
1722 return push_inst32(compiler
, *(sljit_ins
*)instruction
);
1725 /* --------------------------------------------------------------------- */
1726 /* Floating point operators */
1727 /* --------------------------------------------------------------------- */
1729 #define FPU_LOAD (1 << 20)
1731 static sljit_s32
emit_fop_mem(struct sljit_compiler
*compiler
, sljit_s32 flags
, sljit_s32 reg
, sljit_s32 arg
, sljit_sw argw
)
1734 sljit_ins inst
= VSTR_F32
| (flags
& (SLJIT_32
| FPU_LOAD
));
1736 SLJIT_ASSERT(arg
& SLJIT_MEM
);
1738 /* Fast loads and stores. */
1739 if (SLJIT_UNLIKELY(arg
& OFFS_REG_MASK
)) {
1740 FAIL_IF(push_inst32(compiler
, ADD_W
| RD4(TMP_REG1
) | RN4(arg
& REG_MASK
) | RM4(OFFS_REG(arg
)) | (((sljit_uw
)argw
& 0x3) << 6)));
1741 arg
= SLJIT_MEM
| TMP_REG1
;
1745 if ((arg
& REG_MASK
) && (argw
& 0x3) == 0) {
1746 if (!(argw
& ~0x3fc))
1747 return push_inst32(compiler
, inst
| 0x800000 | RN4(arg
& REG_MASK
) | DD4(reg
) | ((sljit_uw
)argw
>> 2));
1748 if (!(-argw
& ~0x3fc))
1749 return push_inst32(compiler
, inst
| RN4(arg
& REG_MASK
) | DD4(reg
) | ((sljit_uw
)-argw
>> 2));
1752 if (arg
& REG_MASK
) {
1753 if (emit_set_delta(compiler
, TMP_REG1
, arg
& REG_MASK
, argw
) != SLJIT_ERR_UNSUPPORTED
) {
1754 FAIL_IF(compiler
->error
);
1755 return push_inst32(compiler
, inst
| 0x800000 | RN4(TMP_REG1
) | DD4(reg
));
1758 imm
= get_imm((sljit_uw
)argw
& ~(sljit_uw
)0x3fc);
1759 if (imm
!= INVALID_IMM
) {
1760 FAIL_IF(push_inst32(compiler
, ADD_WI
| RD4(TMP_REG1
) | RN4(arg
& REG_MASK
) | imm
));
1761 return push_inst32(compiler
, inst
| 0x800000 | RN4(TMP_REG1
) | DD4(reg
) | (((sljit_uw
)argw
& 0x3fc) >> 2));
1764 imm
= get_imm((sljit_uw
)-argw
& ~(sljit_uw
)0x3fc);
1765 if (imm
!= INVALID_IMM
) {
1767 FAIL_IF(push_inst32(compiler
, SUB_WI
| RD4(TMP_REG1
) | RN4(arg
& REG_MASK
) | imm
));
1768 return push_inst32(compiler
, inst
| RN4(TMP_REG1
) | DD4(reg
) | (((sljit_uw
)argw
& 0x3fc) >> 2));
1772 FAIL_IF(load_immediate(compiler
, TMP_REG1
, (sljit_uw
)argw
));
1774 FAIL_IF(push_inst16(compiler
, ADD
| SET_REGS44(TMP_REG1
, (arg
& REG_MASK
))));
1775 return push_inst32(compiler
, inst
| 0x800000 | RN4(TMP_REG1
) | DD4(reg
));
1778 static SLJIT_INLINE sljit_s32
sljit_emit_fop1_conv_sw_from_f64(struct sljit_compiler
*compiler
, sljit_s32 op
,
1779 sljit_s32 dst
, sljit_sw dstw
,
1780 sljit_s32 src
, sljit_sw srcw
)
1784 if (src
& SLJIT_MEM
) {
1785 FAIL_IF(emit_fop_mem(compiler
, (op
& SLJIT_32
) | FPU_LOAD
, TMP_FREG1
, src
, srcw
));
1789 FAIL_IF(push_inst32(compiler
, VCVT_S32_F32
| (op
& SLJIT_32
) | DD4(TMP_FREG1
) | DM4(src
)));
1791 if (FAST_IS_REG(dst
))
1792 return push_inst32(compiler
, VMOV
| (1 << 20) | RT4(dst
) | DN4(TMP_FREG1
));
1794 /* Store the integer value from a VFP register. */
1795 return emit_fop_mem(compiler
, 0, TMP_FREG1
, dst
, dstw
);
1798 static SLJIT_INLINE sljit_s32
sljit_emit_fop1_conv_f64_from_sw(struct sljit_compiler
*compiler
, sljit_s32 op
,
1799 sljit_s32 dst
, sljit_sw dstw
,
1800 sljit_s32 src
, sljit_sw srcw
)
1802 sljit_s32 dst_r
= FAST_IS_REG(dst
) ? dst
: TMP_FREG1
;
1806 if (FAST_IS_REG(src
))
1807 FAIL_IF(push_inst32(compiler
, VMOV
| RT4(src
) | DN4(TMP_FREG1
)));
1808 else if (src
& SLJIT_MEM
) {
1809 /* Load the integer value into a VFP register. */
1810 FAIL_IF(emit_fop_mem(compiler
, FPU_LOAD
, TMP_FREG1
, src
, srcw
));
1813 FAIL_IF(load_immediate(compiler
, TMP_REG1
, (sljit_uw
)srcw
));
1814 FAIL_IF(push_inst32(compiler
, VMOV
| RT4(TMP_REG1
) | DN4(TMP_FREG1
)));
1817 FAIL_IF(push_inst32(compiler
, VCVT_F32_S32
| (op
& SLJIT_32
) | DD4(dst_r
) | DM4(TMP_FREG1
)));
1819 if (dst
& SLJIT_MEM
)
1820 return emit_fop_mem(compiler
, (op
& SLJIT_32
), TMP_FREG1
, dst
, dstw
);
1821 return SLJIT_SUCCESS
;
1824 static SLJIT_INLINE sljit_s32
sljit_emit_fop1_cmp(struct sljit_compiler
*compiler
, sljit_s32 op
,
1825 sljit_s32 src1
, sljit_sw src1w
,
1826 sljit_s32 src2
, sljit_sw src2w
)
1830 if (src1
& SLJIT_MEM
) {
1831 emit_fop_mem(compiler
, (op
& SLJIT_32
) | FPU_LOAD
, TMP_FREG1
, src1
, src1w
);
1835 if (src2
& SLJIT_MEM
) {
1836 emit_fop_mem(compiler
, (op
& SLJIT_32
) | FPU_LOAD
, TMP_FREG2
, src2
, src2w
);
1840 FAIL_IF(push_inst32(compiler
, VCMP_F32
| (op
& SLJIT_32
) | DD4(src1
) | DM4(src2
)));
1841 return push_inst32(compiler
, VMRS
);
1844 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_fop1(struct sljit_compiler
*compiler
, sljit_s32 op
,
1845 sljit_s32 dst
, sljit_sw dstw
,
1846 sljit_s32 src
, sljit_sw srcw
)
1852 SLJIT_COMPILE_ASSERT((SLJIT_32
== 0x100), float_transfer_bit_error
);
1853 SELECT_FOP1_OPERATION_WITH_CHECKS(compiler
, op
, dst
, dstw
, src
, srcw
);
1855 dst_r
= FAST_IS_REG(dst
) ? dst
: TMP_FREG1
;
1857 if (GET_OPCODE(op
) != SLJIT_CONV_F64_FROM_F32
)
1860 if (src
& SLJIT_MEM
) {
1861 emit_fop_mem(compiler
, (op
& SLJIT_32
) | FPU_LOAD
, dst_r
, src
, srcw
);
1865 switch (GET_OPCODE(op
)) {
1868 if (dst_r
!= TMP_FREG1
)
1869 FAIL_IF(push_inst32(compiler
, VMOV_F32
| (op
& SLJIT_32
) | DD4(dst_r
) | DM4(src
)));
1875 FAIL_IF(push_inst32(compiler
, VNEG_F32
| (op
& SLJIT_32
) | DD4(dst_r
) | DM4(src
)));
1878 FAIL_IF(push_inst32(compiler
, VABS_F32
| (op
& SLJIT_32
) | DD4(dst_r
) | DM4(src
)));
1880 case SLJIT_CONV_F64_FROM_F32
:
1881 FAIL_IF(push_inst32(compiler
, VCVT_F64_F32
| (op
& SLJIT_32
) | DD4(dst_r
) | DM4(src
)));
1886 if (dst
& SLJIT_MEM
)
1887 return emit_fop_mem(compiler
, (op
& SLJIT_32
), dst_r
, dst
, dstw
);
1888 return SLJIT_SUCCESS
;
1891 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_fop2(struct sljit_compiler
*compiler
, sljit_s32 op
,
1892 sljit_s32 dst
, sljit_sw dstw
,
1893 sljit_s32 src1
, sljit_sw src1w
,
1894 sljit_s32 src2
, sljit_sw src2w
)
1899 CHECK(check_sljit_emit_fop2(compiler
, op
, dst
, dstw
, src1
, src1w
, src2
, src2w
));
1900 ADJUST_LOCAL_OFFSET(dst
, dstw
);
1901 ADJUST_LOCAL_OFFSET(src1
, src1w
);
1902 ADJUST_LOCAL_OFFSET(src2
, src2w
);
1906 dst_r
= FAST_IS_REG(dst
) ? dst
: TMP_FREG1
;
1907 if (src1
& SLJIT_MEM
) {
1908 emit_fop_mem(compiler
, (op
& SLJIT_32
) | FPU_LOAD
, TMP_FREG1
, src1
, src1w
);
1911 if (src2
& SLJIT_MEM
) {
1912 emit_fop_mem(compiler
, (op
& SLJIT_32
) | FPU_LOAD
, TMP_FREG2
, src2
, src2w
);
1916 switch (GET_OPCODE(op
)) {
1918 FAIL_IF(push_inst32(compiler
, VADD_F32
| (op
& SLJIT_32
) | DD4(dst_r
) | DN4(src1
) | DM4(src2
)));
1921 FAIL_IF(push_inst32(compiler
, VSUB_F32
| (op
& SLJIT_32
) | DD4(dst_r
) | DN4(src1
) | DM4(src2
)));
1924 FAIL_IF(push_inst32(compiler
, VMUL_F32
| (op
& SLJIT_32
) | DD4(dst_r
) | DN4(src1
) | DM4(src2
)));
1927 FAIL_IF(push_inst32(compiler
, VDIV_F32
| (op
& SLJIT_32
) | DD4(dst_r
) | DN4(src1
) | DM4(src2
)));
1931 if (!(dst
& SLJIT_MEM
))
1932 return SLJIT_SUCCESS
;
1933 return emit_fop_mem(compiler
, (op
& SLJIT_32
), TMP_FREG1
, dst
, dstw
);
1938 /* --------------------------------------------------------------------- */
1939 /* Other instructions */
1940 /* --------------------------------------------------------------------- */
1942 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_fast_enter(struct sljit_compiler
*compiler
, sljit_s32 dst
, sljit_sw dstw
)
1945 CHECK(check_sljit_emit_fast_enter(compiler
, dst
, dstw
));
1946 ADJUST_LOCAL_OFFSET(dst
, dstw
);
1948 SLJIT_ASSERT(reg_map
[TMP_REG2
] == 14);
1950 if (FAST_IS_REG(dst
))
1951 return push_inst16(compiler
, MOV
| SET_REGS44(dst
, TMP_REG2
));
1954 return emit_op_mem(compiler
, WORD_SIZE
| STORE
, TMP_REG2
, dst
, dstw
, TMP_REG1
);
1957 /* --------------------------------------------------------------------- */
1958 /* Conditional instructions */
1959 /* --------------------------------------------------------------------- */
1961 static sljit_uw
get_cc(struct sljit_compiler
*compiler
, sljit_s32 type
)
1965 case SLJIT_EQUAL_F64
:
1968 case SLJIT_NOT_EQUAL
:
1969 case SLJIT_NOT_EQUAL_F64
:
1973 case SLJIT_LESS_F64
:
1976 case SLJIT_GREATER_EQUAL
:
1977 case SLJIT_GREATER_EQUAL_F64
:
1981 case SLJIT_GREATER_F64
:
1984 case SLJIT_LESS_EQUAL
:
1985 case SLJIT_LESS_EQUAL_F64
:
1988 case SLJIT_SIG_LESS
:
1991 case SLJIT_SIG_GREATER_EQUAL
:
1994 case SLJIT_SIG_GREATER
:
1997 case SLJIT_SIG_LESS_EQUAL
:
2000 case SLJIT_OVERFLOW
:
2001 if (!(compiler
->status_flags_state
& SLJIT_CURRENT_FLAGS_ADD_SUB
))
2004 case SLJIT_UNORDERED_F64
:
2007 case SLJIT_NOT_OVERFLOW
:
2008 if (!(compiler
->status_flags_state
& SLJIT_CURRENT_FLAGS_ADD_SUB
))
2011 case SLJIT_ORDERED_F64
:
2014 default: /* SLJIT_JUMP */
2015 SLJIT_UNREACHABLE();
2020 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_label
* sljit_emit_label(struct sljit_compiler
*compiler
)
2022 struct sljit_label
*label
;
2025 CHECK_PTR(check_sljit_emit_label(compiler
));
2027 if (compiler
->last_label
&& compiler
->last_label
->size
== compiler
->size
)
2028 return compiler
->last_label
;
2030 label
= (struct sljit_label
*)ensure_abuf(compiler
, sizeof(struct sljit_label
));
2031 PTR_FAIL_IF(!label
);
2032 set_label(label
, compiler
);
2036 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_jump
* sljit_emit_jump(struct sljit_compiler
*compiler
, sljit_s32 type
)
2038 struct sljit_jump
*jump
;
2042 CHECK_PTR(check_sljit_emit_jump(compiler
, type
));
2044 jump
= (struct sljit_jump
*)ensure_abuf(compiler
, sizeof(struct sljit_jump
));
2046 set_jump(jump
, compiler
, type
& SLJIT_REWRITABLE_JUMP
);
2049 PTR_FAIL_IF(emit_imm32_const(compiler
, TMP_REG1
, 0));
2050 if (type
< SLJIT_JUMP
) {
2051 jump
->flags
|= IS_COND
;
2052 cc
= get_cc(compiler
, type
);
2053 jump
->flags
|= cc
<< 8;
2054 PTR_FAIL_IF(push_inst16(compiler
, IT
| (cc
<< 4) | 0x8));
2057 jump
->addr
= compiler
->size
;
2058 if (type
<= SLJIT_JUMP
)
2059 PTR_FAIL_IF(push_inst16(compiler
, BX
| RN3(TMP_REG1
)));
2061 jump
->flags
|= IS_BL
;
2062 PTR_FAIL_IF(push_inst16(compiler
, BLX
| RN3(TMP_REG1
)));
2070 static sljit_s32
softfloat_call_with_args(struct sljit_compiler
*compiler
, sljit_s32 arg_types
, sljit_s32
*src
, sljit_u32
*extra_space
)
2072 sljit_u32 is_tail_call
= *extra_space
& SLJIT_TAIL_CALL
;
2073 sljit_u32 offset
= 0;
2074 sljit_u32 word_arg_offset
= 0;
2075 sljit_u32 float_arg_count
= 0;
2076 sljit_s32 types
= 0;
2077 sljit_u32 src_offset
= 4 * sizeof(sljit_sw
);
2078 sljit_u8 offsets
[4];
2079 sljit_u8
*offset_ptr
= offsets
;
2081 if (src
&& FAST_IS_REG(*src
))
2082 src_offset
= (sljit_u32
)reg_map
[*src
] * sizeof(sljit_sw
);
2084 arg_types
>>= SLJIT_ARG_SHIFT
;
2087 types
= (types
<< SLJIT_ARG_SHIFT
) | (arg_types
& SLJIT_ARG_MASK
);
2089 switch (arg_types
& SLJIT_ARG_MASK
) {
2090 case SLJIT_ARG_TYPE_F64
:
2092 offset
+= sizeof(sljit_sw
);
2093 *offset_ptr
++ = (sljit_u8
)offset
;
2094 offset
+= sizeof(sljit_f64
);
2097 case SLJIT_ARG_TYPE_F32
:
2098 *offset_ptr
++ = (sljit_u8
)offset
;
2099 offset
+= sizeof(sljit_f32
);
2103 *offset_ptr
++ = (sljit_u8
)offset
;
2104 offset
+= sizeof(sljit_sw
);
2105 word_arg_offset
+= sizeof(sljit_sw
);
2109 arg_types
>>= SLJIT_ARG_SHIFT
;
2112 if (offset
> 4 * sizeof(sljit_sw
) && (!is_tail_call
|| offset
> compiler
->args_size
)) {
2113 /* Keep lr register on the stack. */
2115 offset
+= sizeof(sljit_sw
);
2117 offset
= ((offset
- 4 * sizeof(sljit_sw
)) + 0x7) & ~(sljit_uw
)0x7;
2119 *extra_space
= offset
;
2122 FAIL_IF(emit_stack_frame_release(compiler
, (sljit_s32
)offset
));
2124 FAIL_IF(push_inst16(compiler
, SUB_SP_I
| (offset
>> 2)));
2127 FAIL_IF(emit_stack_frame_release(compiler
, -1));
2131 SLJIT_ASSERT(reg_map
[TMP_REG1
] == 12);
2133 /* Process arguments in reversed direction. */
2135 switch (types
& SLJIT_ARG_MASK
) {
2136 case SLJIT_ARG_TYPE_F64
:
2138 offset
= *(--offset_ptr
);
2140 SLJIT_ASSERT((offset
& 0x7) == 0);
2142 if (offset
< 4 * sizeof(sljit_sw
)) {
2143 if (src_offset
== offset
|| src_offset
== offset
+ sizeof(sljit_sw
)) {
2144 FAIL_IF(push_inst16(compiler
, MOV
| (src_offset
<< 1) | 4 | (1 << 7)));
2147 FAIL_IF(push_inst32(compiler
, VMOV2
| 0x100000 | (offset
<< 10) | ((offset
+ sizeof(sljit_sw
)) << 14) | float_arg_count
));
2149 FAIL_IF(push_inst32(compiler
, VSTR_F32
| 0x800100 | RN4(SLJIT_SP
)
2150 | (float_arg_count
<< 12) | ((offset
- 4 * sizeof(sljit_sw
)) >> 2)));
2152 case SLJIT_ARG_TYPE_F32
:
2154 offset
= *(--offset_ptr
);
2156 if (offset
< 4 * sizeof(sljit_sw
)) {
2157 if (src_offset
== offset
) {
2158 FAIL_IF(push_inst16(compiler
, MOV
| (src_offset
<< 1) | 4 | (1 << 7)));
2161 FAIL_IF(push_inst32(compiler
, VMOV
| 0x100000 | (float_arg_count
<< 16) | (offset
<< 10)));
2163 FAIL_IF(push_inst32(compiler
, VSTR_F32
| 0x800000 | RN4(SLJIT_SP
)
2164 | (float_arg_count
<< 12) | ((offset
- 4 * sizeof(sljit_sw
)) >> 2)));
2167 word_arg_offset
-= sizeof(sljit_sw
);
2168 offset
= *(--offset_ptr
);
2170 SLJIT_ASSERT(offset
>= word_arg_offset
);
2172 if (offset
!= word_arg_offset
) {
2173 if (offset
< 4 * sizeof(sljit_sw
)) {
2174 if (src_offset
== offset
) {
2175 FAIL_IF(push_inst16(compiler
, MOV
| (src_offset
<< 1) | 4 | (1 << 7)));
2178 else if (src_offset
== word_arg_offset
) {
2179 *src
= (sljit_s32
)(1 + (offset
>> 2));
2180 src_offset
= offset
;
2182 FAIL_IF(push_inst16(compiler
, MOV
| (offset
>> 2) | (word_arg_offset
<< 1)));
2184 FAIL_IF(push_inst16(compiler
, STR_SP
| (word_arg_offset
<< 6) | ((offset
- 4 * sizeof(sljit_sw
)) >> 2)));
2189 types
>>= SLJIT_ARG_SHIFT
;
2192 return SLJIT_SUCCESS
;
2195 static sljit_s32
softfloat_post_call_with_args(struct sljit_compiler
*compiler
, sljit_s32 arg_types
)
2197 if ((arg_types
& SLJIT_ARG_MASK
) == SLJIT_ARG_TYPE_F64
)
2198 FAIL_IF(push_inst32(compiler
, VMOV2
| (1 << 16) | (0 << 12) | 0));
2199 if ((arg_types
& SLJIT_ARG_MASK
) == SLJIT_ARG_TYPE_F32
)
2200 FAIL_IF(push_inst32(compiler
, VMOV
| (0 << 16) | (0 << 12)));
2202 return SLJIT_SUCCESS
;
2207 static sljit_s32
hardfloat_call_with_args(struct sljit_compiler
*compiler
, sljit_s32 arg_types
)
2209 sljit_u32 offset
= SLJIT_FR0
;
2210 sljit_u32 new_offset
= SLJIT_FR0
;
2211 sljit_u32 f32_offset
= 0;
2213 /* Remove return value. */
2214 arg_types
>>= SLJIT_ARG_SHIFT
;
2217 switch (arg_types
& SLJIT_ARG_MASK
) {
2218 case SLJIT_ARG_TYPE_F64
:
2219 if (offset
!= new_offset
)
2220 FAIL_IF(push_inst32(compiler
, VMOV_F32
| SLJIT_32
| DD4(new_offset
) | DM4(offset
)));
2225 case SLJIT_ARG_TYPE_F32
:
2226 if (f32_offset
!= 0) {
2227 FAIL_IF(push_inst32(compiler
, VMOV_F32
| 0x400000 | DD4(f32_offset
) | DM4(offset
)));
2230 if (offset
!= new_offset
)
2231 FAIL_IF(push_inst32(compiler
, VMOV_F32
| 0x400000 | DD4(new_offset
) | DM4(offset
)));
2232 f32_offset
= new_offset
;
2238 arg_types
>>= SLJIT_ARG_SHIFT
;
2241 return SLJIT_SUCCESS
;
2246 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_jump
* sljit_emit_call(struct sljit_compiler
*compiler
, sljit_s32 type
,
2247 sljit_s32 arg_types
)
2250 struct sljit_jump
*jump
;
2251 sljit_u32 extra_space
= (sljit_u32
)type
;
2255 CHECK_PTR(check_sljit_emit_call(compiler
, type
, arg_types
));
2258 PTR_FAIL_IF(softfloat_call_with_args(compiler
, arg_types
, NULL
, &extra_space
));
2259 SLJIT_ASSERT((extra_space
& 0x7) == 0);
2261 if ((type
& SLJIT_TAIL_CALL
) && extra_space
== 0)
2262 type
= SLJIT_JUMP
| (type
& SLJIT_REWRITABLE_JUMP
);
2264 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2265 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2266 compiler
->skip_checks
= 1;
2269 jump
= sljit_emit_jump(compiler
, type
);
2270 PTR_FAIL_IF(jump
== NULL
);
2272 if (extra_space
> 0) {
2273 if (type
& SLJIT_TAIL_CALL
)
2274 PTR_FAIL_IF(push_inst32(compiler
, LDR
| RT4(TMP_REG2
)
2275 | RN4(SLJIT_SP
) | (extra_space
- sizeof(sljit_sw
))));
2277 PTR_FAIL_IF(push_inst16(compiler
, ADD_SP_I
| (extra_space
>> 2)));
2279 if (type
& SLJIT_TAIL_CALL
) {
2280 PTR_FAIL_IF(push_inst16(compiler
, BX
| RN3(TMP_REG2
)));
2285 SLJIT_ASSERT(!(type
& SLJIT_TAIL_CALL
));
2286 PTR_FAIL_IF(softfloat_post_call_with_args(compiler
, arg_types
));
2289 if (type
& SLJIT_TAIL_CALL
) {
2290 /* ldmia sp!, {..., lr} */
2291 PTR_FAIL_IF(emit_stack_frame_release(compiler
, -1));
2292 type
= SLJIT_JUMP
| (type
& SLJIT_REWRITABLE_JUMP
);
2295 PTR_FAIL_IF(hardfloat_call_with_args(compiler
, arg_types
));
2297 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2298 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2299 compiler
->skip_checks
= 1;
2302 return sljit_emit_jump(compiler
, type
);
2306 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_ijump(struct sljit_compiler
*compiler
, sljit_s32 type
, sljit_s32 src
, sljit_sw srcw
)
2308 struct sljit_jump
*jump
;
2311 CHECK(check_sljit_emit_ijump(compiler
, type
, src
, srcw
));
2312 ADJUST_LOCAL_OFFSET(src
, srcw
);
2314 SLJIT_ASSERT(reg_map
[TMP_REG1
] != 14);
2316 if (!(src
& SLJIT_IMM
)) {
2317 if (FAST_IS_REG(src
)) {
2318 SLJIT_ASSERT(reg_map
[src
] != 14);
2319 return push_inst16(compiler
, (type
<= SLJIT_JUMP
? BX
: BLX
) | RN3(src
));
2322 FAIL_IF(emit_op_mem(compiler
, WORD_SIZE
, type
<= SLJIT_JUMP
? TMP_PC
: TMP_REG1
, src
, srcw
, TMP_REG1
));
2323 if (type
>= SLJIT_FAST_CALL
)
2324 return push_inst16(compiler
, BLX
| RN3(TMP_REG1
));
2327 /* These jumps are converted to jump/call instructions when possible. */
2328 jump
= (struct sljit_jump
*)ensure_abuf(compiler
, sizeof(struct sljit_jump
));
2330 set_jump(jump
, compiler
, JUMP_ADDR
| ((type
>= SLJIT_FAST_CALL
) ? IS_BL
: 0));
2331 jump
->u
.target
= (sljit_uw
)srcw
;
2333 FAIL_IF(emit_imm32_const(compiler
, TMP_REG1
, 0));
2334 jump
->addr
= compiler
->size
;
2335 return push_inst16(compiler
, (type
<= SLJIT_JUMP
? BX
: BLX
) | RN3(TMP_REG1
));
2338 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_icall(struct sljit_compiler
*compiler
, sljit_s32 type
,
2339 sljit_s32 arg_types
,
2340 sljit_s32 src
, sljit_sw srcw
)
2343 sljit_u32 extra_space
= (sljit_u32
)type
;
2347 CHECK(check_sljit_emit_icall(compiler
, type
, arg_types
, src
, srcw
));
2349 if (src
& SLJIT_MEM
) {
2350 FAIL_IF(emit_op_mem(compiler
, WORD_SIZE
, TMP_REG1
, src
, srcw
, TMP_REG1
));
2354 if ((type
& SLJIT_TAIL_CALL
) && (src
>= SLJIT_FIRST_SAVED_REG
&& src
<= SLJIT_S0
)) {
2355 FAIL_IF(push_inst16(compiler
, MOV
| SET_REGS44(TMP_REG1
, src
)));
2360 FAIL_IF(softfloat_call_with_args(compiler
, arg_types
, &src
, &extra_space
));
2361 SLJIT_ASSERT((extra_space
& 0x7) == 0);
2363 if ((type
& SLJIT_TAIL_CALL
) && extra_space
== 0)
2366 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2367 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2368 compiler
->skip_checks
= 1;
2371 FAIL_IF(sljit_emit_ijump(compiler
, type
, src
, srcw
));
2373 if (extra_space
> 0) {
2374 if (type
& SLJIT_TAIL_CALL
)
2375 FAIL_IF(push_inst32(compiler
, LDR
| RT4(TMP_REG2
)
2376 | RN4(SLJIT_SP
) | (extra_space
- sizeof(sljit_sw
))));
2378 FAIL_IF(push_inst16(compiler
, ADD_SP_I
| (extra_space
>> 2)));
2380 if (type
& SLJIT_TAIL_CALL
)
2381 return push_inst16(compiler
, BX
| RN3(TMP_REG2
));
2384 SLJIT_ASSERT(!(type
& SLJIT_TAIL_CALL
));
2385 return softfloat_post_call_with_args(compiler
, arg_types
);
2386 #else /* !__SOFTFP__ */
2387 if (type
& SLJIT_TAIL_CALL
) {
2388 /* ldmia sp!, {..., lr} */
2389 FAIL_IF(emit_stack_frame_release(compiler
, -1));
2393 FAIL_IF(hardfloat_call_with_args(compiler
, arg_types
));
2395 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2396 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2397 compiler
->skip_checks
= 1;
2400 return sljit_emit_ijump(compiler
, type
, src
, srcw
);
2401 #endif /* __SOFTFP__ */
2404 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op_flags(struct sljit_compiler
*compiler
, sljit_s32 op
,
2405 sljit_s32 dst
, sljit_sw dstw
,
2408 sljit_s32 dst_r
, flags
= GET_ALL_FLAGS(op
);
2412 CHECK(check_sljit_emit_op_flags(compiler
, op
, dst
, dstw
, type
));
2413 ADJUST_LOCAL_OFFSET(dst
, dstw
);
2415 op
= GET_OPCODE(op
);
2416 cc
= get_cc(compiler
, type
& 0xff);
2417 dst_r
= FAST_IS_REG(dst
) ? dst
: TMP_REG1
;
2419 if (op
< SLJIT_ADD
) {
2420 FAIL_IF(push_inst16(compiler
, IT
| (cc
<< 4) | (((cc
& 0x1) ^ 0x1) << 3) | 0x4));
2421 if (reg_map
[dst_r
] > 7) {
2422 FAIL_IF(push_inst32(compiler
, MOV_WI
| RD4(dst_r
) | 1));
2423 FAIL_IF(push_inst32(compiler
, MOV_WI
| RD4(dst_r
) | 0));
2425 /* The movsi (immediate) instruction does not set flags in IT block. */
2426 FAIL_IF(push_inst16(compiler
, MOVSI
| RDN3(dst_r
) | 1));
2427 FAIL_IF(push_inst16(compiler
, MOVSI
| RDN3(dst_r
) | 0));
2429 if (!(dst
& SLJIT_MEM
))
2430 return SLJIT_SUCCESS
;
2431 return emit_op_mem(compiler
, WORD_SIZE
| STORE
, TMP_REG1
, dst
, dstw
, TMP_REG2
);
2434 if (dst
& SLJIT_MEM
)
2435 FAIL_IF(emit_op_mem(compiler
, WORD_SIZE
, TMP_REG1
, dst
, dstw
, TMP_REG2
));
2437 if (op
== SLJIT_AND
) {
2438 FAIL_IF(push_inst16(compiler
, IT
| (cc
<< 4) | (((cc
& 0x1) ^ 0x1) << 3) | 0x4));
2439 FAIL_IF(push_inst32(compiler
, ANDI
| RN4(dst_r
) | RD4(dst_r
) | 1));
2440 FAIL_IF(push_inst32(compiler
, ANDI
| RN4(dst_r
) | RD4(dst_r
) | 0));
2443 FAIL_IF(push_inst16(compiler
, IT
| (cc
<< 4) | 0x8));
2444 FAIL_IF(push_inst32(compiler
, ((op
== SLJIT_OR
) ? ORRI
: EORI
) | RN4(dst_r
) | RD4(dst_r
) | 1));
2447 if (dst
& SLJIT_MEM
)
2448 FAIL_IF(emit_op_mem(compiler
, WORD_SIZE
| STORE
, TMP_REG1
, dst
, dstw
, TMP_REG2
));
2450 if (!(flags
& SLJIT_SET_Z
))
2451 return SLJIT_SUCCESS
;
2453 /* The condition must always be set, even if the ORR/EORI is not executed above. */
2454 return push_inst32(compiler
, MOV_W
| SET_FLAGS
| RD4(TMP_REG1
) | RM4(dst_r
));
2457 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_cmov(struct sljit_compiler
*compiler
, sljit_s32 type
,
2459 sljit_s32 src
, sljit_sw srcw
)
2464 CHECK(check_sljit_emit_cmov(compiler
, type
, dst_reg
, src
, srcw
));
2466 dst_reg
&= ~SLJIT_32
;
2468 cc
= get_cc(compiler
, type
& 0xff);
2470 if (!(src
& SLJIT_IMM
)) {
2471 FAIL_IF(push_inst16(compiler
, IT
| (cc
<< 4) | 0x8));
2472 return push_inst16(compiler
, MOV
| SET_REGS44(dst_reg
, src
));
2475 tmp
= (sljit_uw
) srcw
;
2477 if (tmp
< 0x10000) {
2478 /* set low 16 bits, set hi 16 bits to 0. */
2479 FAIL_IF(push_inst16(compiler
, IT
| (cc
<< 4) | 0x8));
2480 return push_inst32(compiler
, MOVW
| RD4(dst_reg
)
2481 | COPY_BITS(tmp
, 12, 16, 4) | COPY_BITS(tmp
, 11, 26, 1) | COPY_BITS(tmp
, 8, 12, 3) | (tmp
& 0xff));
2484 tmp
= get_imm((sljit_uw
)srcw
);
2485 if (tmp
!= INVALID_IMM
) {
2486 FAIL_IF(push_inst16(compiler
, IT
| (cc
<< 4) | 0x8));
2487 return push_inst32(compiler
, MOV_WI
| RD4(dst_reg
) | tmp
);
2490 tmp
= get_imm(~(sljit_uw
)srcw
);
2491 if (tmp
!= INVALID_IMM
) {
2492 FAIL_IF(push_inst16(compiler
, IT
| (cc
<< 4) | 0x8));
2493 return push_inst32(compiler
, MVN_WI
| RD4(dst_reg
) | tmp
);
2496 FAIL_IF(push_inst16(compiler
, IT
| (cc
<< 4) | ((cc
& 0x1) << 3) | 0x4));
2498 tmp
= (sljit_uw
) srcw
;
2499 FAIL_IF(push_inst32(compiler
, MOVW
| RD4(dst_reg
)
2500 | COPY_BITS(tmp
, 12, 16, 4) | COPY_BITS(tmp
, 11, 26, 1) | COPY_BITS(tmp
, 8, 12, 3) | (tmp
& 0xff)));
2501 return push_inst32(compiler
, MOVT
| RD4(dst_reg
)
2502 | COPY_BITS(tmp
, 12 + 16, 16, 4) | COPY_BITS(tmp
, 11 + 16, 26, 1) | COPY_BITS(tmp
, 8 + 16, 12, 3) | ((tmp
& 0xff0000) >> 16));
2505 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_mem(struct sljit_compiler
*compiler
, sljit_s32 type
,
2507 sljit_s32 mem
, sljit_sw memw
)
2513 CHECK(check_sljit_emit_mem(compiler
, type
, reg
, mem
, memw
));
2515 if ((mem
& OFFS_REG_MASK
) || (memw
> 255 || memw
< -255))
2516 return SLJIT_ERR_UNSUPPORTED
;
2518 if (type
& SLJIT_MEM_SUPP
)
2519 return SLJIT_SUCCESS
;
2521 switch (type
& 0xff) {
2533 flags
= BYTE_SIZE
| SIGNED
;
2539 flags
= HALF_SIZE
| SIGNED
;
2542 SLJIT_UNREACHABLE();
2547 if (type
& SLJIT_MEM_STORE
)
2550 inst
= sljit_mem32
[flags
] | 0x900;
2552 if (type
& SLJIT_MEM_PRE
)
2560 return push_inst32(compiler
, inst
| RT4(reg
) | RN4(mem
& REG_MASK
) | (sljit_ins
)memw
);
2563 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_const
* sljit_emit_const(struct sljit_compiler
*compiler
, sljit_s32 dst
, sljit_sw dstw
, sljit_sw init_value
)
2565 struct sljit_const
*const_
;
2569 CHECK_PTR(check_sljit_emit_const(compiler
, dst
, dstw
, init_value
));
2570 ADJUST_LOCAL_OFFSET(dst
, dstw
);
2572 const_
= (struct sljit_const
*)ensure_abuf(compiler
, sizeof(struct sljit_const
));
2573 PTR_FAIL_IF(!const_
);
2574 set_const(const_
, compiler
);
2576 dst_r
= FAST_IS_REG(dst
) ? dst
: TMP_REG1
;
2577 PTR_FAIL_IF(emit_imm32_const(compiler
, dst_r
, (sljit_uw
)init_value
));
2579 if (dst
& SLJIT_MEM
)
2580 PTR_FAIL_IF(emit_op_mem(compiler
, WORD_SIZE
| STORE
, dst_r
, dst
, dstw
, TMP_REG2
));
2584 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_put_label
* sljit_emit_put_label(struct sljit_compiler
*compiler
, sljit_s32 dst
, sljit_sw dstw
)
2586 struct sljit_put_label
*put_label
;
2590 CHECK_PTR(check_sljit_emit_put_label(compiler
, dst
, dstw
));
2591 ADJUST_LOCAL_OFFSET(dst
, dstw
);
2593 put_label
= (struct sljit_put_label
*)ensure_abuf(compiler
, sizeof(struct sljit_put_label
));
2594 PTR_FAIL_IF(!put_label
);
2595 set_put_label(put_label
, compiler
, 0);
2597 dst_r
= FAST_IS_REG(dst
) ? dst
: TMP_REG1
;
2598 PTR_FAIL_IF(emit_imm32_const(compiler
, dst_r
, 0));
2600 if (dst
& SLJIT_MEM
)
2601 PTR_FAIL_IF(emit_op_mem(compiler
, WORD_SIZE
| STORE
, dst_r
, dst
, dstw
, TMP_REG2
));
2605 SLJIT_API_FUNC_ATTRIBUTE
void sljit_set_jump_addr(sljit_uw addr
, sljit_uw new_target
, sljit_sw executable_offset
)
2607 sljit_u16
*inst
= (sljit_u16
*)addr
;
2608 SLJIT_UNUSED_ARG(executable_offset
);
2610 SLJIT_UPDATE_WX_FLAGS(inst
, inst
+ 4, 0);
2611 modify_imm32_const(inst
, new_target
);
2612 SLJIT_UPDATE_WX_FLAGS(inst
, inst
+ 4, 1);
2613 inst
= (sljit_u16
*)SLJIT_ADD_EXEC_OFFSET(inst
, executable_offset
);
2614 SLJIT_CACHE_FLUSH(inst
, inst
+ 4);
2617 SLJIT_API_FUNC_ATTRIBUTE
void sljit_set_const(sljit_uw addr
, sljit_sw new_constant
, sljit_sw executable_offset
)
2619 sljit_set_jump_addr(addr
, (sljit_uw
)new_constant
, executable_offset
);