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, 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])
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 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
)
213 SLJIT_ASSERT(!(inst
& 0xffff0000));
215 ptr
= (sljit_u16
*)ensure_buf(compiler
, sizeof(sljit_u16
));
217 *ptr
= (sljit_u16
)(inst
);
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
));
226 *ptr
++ = (sljit_u16
)(inst
>> 16);
227 *ptr
= (sljit_u16
)(inst
);
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
)
254 if (jump
->flags
& SLJIT_REWRITABLE_JUMP
)
257 if (jump
->flags
& JUMP_ADDR
) {
258 /* Branch to ARM code is not optimized yet. */
259 if (!(jump
->u
.target
& 0x1))
261 diff
= ((sljit_sw
)jump
->u
.target
- (sljit_sw
)(code_ptr
+ 2) - executable_offset
) >> 1;
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
;
274 if (diff
<= 524287 && diff
>= -524288) {
275 jump
->flags
|= PATCH_TYPE2
;
278 /* +1 comes from the prefix IT instruction. */
280 if (diff
<= 8388607 && diff
>= -8388608) {
281 jump
->flags
|= PATCH_TYPE3
;
285 else if (jump
->flags
& IS_BL
) {
286 if (diff
<= 8388607 && diff
>= -8388608) {
287 jump
->flags
|= PATCH_BL
;
292 if (diff
<= 1023 && diff
>= -1024) {
293 jump
->flags
|= PATCH_TYPE4
;
296 if (diff
<= 8388607 && diff
>= -8388608) {
297 jump
->flags
|= PATCH_TYPE5
;
305 static SLJIT_INLINE
void set_jump_instruction(struct sljit_jump
*jump
, sljit_sw executable_offset
)
307 sljit_s32 type
= (jump
->flags
>> 4) & 0xf;
309 sljit_u16
*jump_inst
;
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
);
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;
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
;
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));
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));
340 SLJIT_ASSERT(jump
->flags
& IS_COND
);
341 *jump_inst
++ = (sljit_u16
)(IT
| ((jump
->flags
>> 4) & 0xf0) | 0x8);
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));
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;
370 SLJIT_API_FUNC_ATTRIBUTE
void* sljit_generate_code(struct sljit_compiler
*compiler
)
372 struct sljit_memory_fragment
*buf
;
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
;
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
);
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
;
405 buf_ptr
= (sljit_u16
*)buf
->memory
;
406 buf_end
= buf_ptr
+ (buf
->used_size
>> 1);
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
);
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
);
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
);
439 } while (buf_ptr
< buf_end
);
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
);
450 SLJIT_ASSERT(!label
);
452 SLJIT_ASSERT(!const_
);
453 SLJIT_ASSERT(!put_label
);
454 SLJIT_ASSERT(code_ptr
- code
<= (sljit_sw
)compiler
->size
);
456 jump
= compiler
->jumps
;
458 set_jump_instruction(jump
, executable_offset
);
462 put_label
= compiler
->put_labels
;
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
) {
486 #ifdef SLJIT_IS_FPU_AVAILABLE
487 return SLJIT_IS_FPU_AVAILABLE
;
489 /* Available by default. */
495 case SLJIT_HAS_PREFETCH
:
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. */
516 if ((imm
& 0xffff) == (imm
>> 16)) {
517 /* Some special cases. */
519 return (1 << 12) | (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? */
528 if (!(imm
& 0xffff0000)) {
532 if (!(imm
& 0xff000000)) {
536 if (!(imm
& 0xf0000000)) {
540 if (!(imm
& 0xc0000000)) {
544 if (!(imm
& 0x80000000)) {
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
)
561 /* MOVS cannot be used since it destroy flags. */
563 if (imm
>= 0x10000) {
565 if (tmp
!= INVALID_IMM
)
566 return push_inst32(compiler
, MOV_WI
| RD4(dst
) | tmp
);
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. */
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 */
597 if (SLJIT_UNLIKELY((flags
& (ARG1_IMM
| ARG2_IMM
)) == (ARG1_IMM
| ARG2_IMM
))) {
598 /* Both are immediates, no temporaries are used. */
600 FAIL_IF(load_immediate(compiler
, TMP_REG1
, arg1
));
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) {
611 /* No form with immediate operand. */
614 SLJIT_ASSERT(!(flags
& SET_FLAGS
) && (flags
& ARG2_IMM
) && arg1
== TMP_REG2
);
615 return load_immediate(compiler
, dst
, imm
);
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. */
623 compiler
->status_flags_state
= SLJIT_CURRENT_FLAGS_ADD
;
625 if (IS_2_LO_REGS(reg
, dst
)) {
627 return push_inst16(compiler
, ADDSI3
| IMM3(imm
) | RD3(dst
) | RN3(reg
));
629 return push_inst16(compiler
, SUBSI3
| IMM3(nimm
) | RD3(dst
) | RN3(reg
));
632 return push_inst16(compiler
, ADDSI8
| IMM8(imm
) | RDN3(dst
));
634 return push_inst16(compiler
, SUBSI8
| IMM8(nimm
) | RDN3(dst
));
637 if (!(flags
& SET_FLAGS
)) {
639 return push_inst32(compiler
, ADDWI
| RD4(dst
) | RN4(reg
) | IMM12(imm
));
641 return push_inst32(compiler
, SUBWI
| RD4(dst
) | RN4(reg
) | IMM12(nimm
));
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
);
651 compiler
->status_flags_state
= SLJIT_CURRENT_FLAGS_ADD
;
653 if (imm
!= INVALID_IMM
)
654 return push_inst32(compiler
, ADCI
| (flags
& SET_FLAGS
) | RD4(dst
) | RN4(reg
) | imm
);
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
));
662 if (imm
!= INVALID_IMM
)
663 return push_inst32(compiler
, RSB_WI
| (flags
& SET_FLAGS
) | RD4(dst
) | RN4(reg
) | imm
);
666 if (flags
& UNUSED_RETURN
) {
667 if (imm
<= 0xff && reg_map
[reg
] <= 7)
668 return push_inst16(compiler
, CMPI
| IMM8(imm
) | RDN3(reg
));
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
);
678 if (IS_2_LO_REGS(reg
, dst
)) {
680 return push_inst16(compiler
, SUBSI3
| IMM3(imm
) | RD3(dst
) | RN3(reg
));
682 return push_inst16(compiler
, ADDSI3
| IMM3(nimm
) | RD3(dst
) | RN3(reg
));
685 return push_inst16(compiler
, SUBSI8
| IMM8(imm
) | RDN3(dst
));
687 return push_inst16(compiler
, ADDSI8
| IMM8(nimm
) | RDN3(dst
));
690 if (!(flags
& SET_FLAGS
)) {
692 return push_inst32(compiler
, SUBWI
| RD4(dst
) | RN4(reg
) | IMM12(imm
));
694 return push_inst32(compiler
, ADDWI
| RD4(dst
) | RN4(reg
) | IMM12(nimm
));
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
);
704 compiler
->status_flags_state
= SLJIT_CURRENT_FLAGS_SUB
;
705 if (flags
& ARG1_IMM
)
708 if (imm
!= INVALID_IMM
)
709 return push_inst32(compiler
, SBCI
| (flags
& SET_FLAGS
) | RD4(dst
) | RN4(reg
) | imm
);
713 if (nimm
!= INVALID_IMM
)
714 return push_inst32(compiler
, ((flags
& UNUSED_RETURN
) ? TSTI
: ANDI
) | (flags
& SET_FLAGS
) | RD4(dst
) | RN4(reg
) | nimm
);
716 if (imm
!= INVALID_IMM
)
717 return push_inst32(compiler
, BICI
| (flags
& SET_FLAGS
) | RD4(dst
) | RN4(reg
) | imm
);
721 if (nimm
!= INVALID_IMM
)
722 return push_inst32(compiler
, ORRI
| (flags
& SET_FLAGS
) | RD4(dst
) | RN4(reg
) | nimm
);
724 if (imm
!= INVALID_IMM
)
725 return push_inst32(compiler
, ORNI
| (flags
& SET_FLAGS
) | RD4(dst
) | RN4(reg
) | imm
);
729 if (imm
!= INVALID_IMM
)
730 return push_inst32(compiler
, EORI
| (flags
& SET_FLAGS
) | RD4(dst
) | RN4(reg
) | imm
);
735 if (flags
& ARG1_IMM
)
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) {
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
));
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
));
764 if (flags
& ARG2_IMM
) {
766 arg2
= (arg1
== TMP_REG1
) ? TMP_REG2
: TMP_REG1
;
767 FAIL_IF(load_immediate(compiler
, (sljit_s32
)arg2
, imm
));
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) {
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
));
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
));
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
));
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
));
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
));
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
));
815 SLJIT_ASSERT(arg1
== TMP_REG2
);
816 FAIL_IF(push_inst32(compiler
, CLZ
| RN4(arg2
) | RD4(dst
) | RM4(arg2
)));
817 return SLJIT_SUCCESS
;
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
));
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
));
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
));
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
));
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
));
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
));
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
));
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
));
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
));
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
));
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
));
882 return SLJIT_SUCCESS
;
888 #define WORD_SIZE 0x00
889 #define BYTE_SIZE 0x04
890 #define HALF_SIZE 0x08
893 #define IS_WORD_SIZE(flags) (!(flags & (BYTE_SIZE | HALF_SIZE)))
894 #define OFFSET_CHECK(imm, shift) (!(argw & ~(imm << shift)))
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
)
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
);
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
)
995 SLJIT_ASSERT(arg
& SLJIT_MEM
);
996 SLJIT_ASSERT((arg
& REG_MASK
) != tmp_reg
);
999 if (SLJIT_UNLIKELY(!(arg
& REG_MASK
))) {
1000 tmp
= get_imm((sljit_uw
)argw
& ~(sljit_uw
)0xfff);
1001 if (tmp
!= INVALID_IMM
) {
1002 FAIL_IF(push_inst32(compiler
, MOV_WI
| RD4(tmp_reg
) | tmp
));
1003 return push_inst32(compiler
, sljit_mem32
[flags
] | MEM_IMM12
| RT4(reg
) | RN4(tmp_reg
) | (argw
& 0xfff));
1006 FAIL_IF(load_immediate(compiler
, tmp_reg
, (sljit_uw
)argw
));
1007 if (IS_2_LO_REGS(reg
, tmp_reg
) && sljit_mem16_imm5
[flags
])
1008 return push_inst16(compiler
, sljit_mem16_imm5
[flags
] | RD3(reg
) | RN3(tmp_reg
));
1009 return push_inst32(compiler
, sljit_mem32
[flags
] | MEM_IMM12
| RT4(reg
) | RN4(tmp_reg
));
1012 if (SLJIT_UNLIKELY(arg
& OFFS_REG_MASK
)) {
1014 other_r
= OFFS_REG(arg
);
1017 if (!argw
&& IS_3_LO_REGS(reg
, arg
, other_r
))
1018 return push_inst16(compiler
, sljit_mem16
[flags
] | RD3(reg
) | RN3(arg
) | RM3(other_r
));
1019 return push_inst32(compiler
, sljit_mem32
[flags
] | RT4(reg
) | RN4(arg
) | RM4(other_r
) | ((sljit_ins
)argw
<< 4));
1023 tmp
= get_imm((sljit_uw
)argw
& ~(sljit_uw
)0xfff);
1024 if (tmp
!= INVALID_IMM
) {
1025 push_inst32(compiler
, ADD_WI
| RD4(tmp_reg
) | RN4(arg
) | tmp
);
1027 argw
= argw
& 0xfff;
1030 else if (argw
< -0xff) {
1031 tmp
= get_imm((sljit_uw
)-argw
& ~(sljit_uw
)0xff);
1032 if (tmp
!= INVALID_IMM
) {
1033 push_inst32(compiler
, SUB_WI
| RD4(tmp_reg
) | RN4(arg
) | tmp
);
1035 argw
= -(-argw
& 0xff);
1039 if (IS_2_LO_REGS(reg
, arg
) && sljit_mem16_imm5
[flags
]) {
1041 if (IS_WORD_SIZE(flags
)) {
1042 if (OFFSET_CHECK(0x1f, 2))
1045 else if (flags
& BYTE_SIZE
)
1047 if (OFFSET_CHECK(0x1f, 0))
1051 SLJIT_ASSERT(flags
& HALF_SIZE
);
1052 if (OFFSET_CHECK(0x1f, 1))
1057 return push_inst16(compiler
, sljit_mem16_imm5
[flags
] | RD3(reg
) | RN3(arg
) | ((sljit_ins
)argw
<< (6 - tmp
)));
1059 else if (SLJIT_UNLIKELY(arg
== SLJIT_SP
) && IS_WORD_SIZE(flags
) && OFFSET_CHECK(0xff, 2) && reg_map
[reg
] <= 7) {
1060 /* SP based immediate. */
1061 return push_inst16(compiler
, STR_SP
| (sljit_ins
)((flags
& STORE
) ? 0 : 0x800) | RDN3(reg
) | ((sljit_ins
)argw
>> 2));
1064 if (argw
>= 0 && argw
<= 0xfff)
1065 return push_inst32(compiler
, sljit_mem32
[flags
] | MEM_IMM12
| RT4(reg
) | RN4(arg
) | (sljit_ins
)argw
);
1066 else if (argw
< 0 && argw
>= -0xff)
1067 return push_inst32(compiler
, sljit_mem32
[flags
] | MEM_IMM8
| RT4(reg
) | RN4(arg
) | (sljit_ins
)-argw
);
1069 SLJIT_ASSERT(arg
!= tmp_reg
);
1071 FAIL_IF(load_immediate(compiler
, tmp_reg
, (sljit_uw
)argw
));
1072 if (IS_3_LO_REGS(reg
, arg
, tmp_reg
))
1073 return push_inst16(compiler
, sljit_mem16
[flags
] | RD3(reg
) | RN3(arg
) | RM3(tmp_reg
));
1074 return push_inst32(compiler
, sljit_mem32
[flags
] | RT4(reg
) | RN4(arg
) | RM4(tmp_reg
));
1077 /* --------------------------------------------------------------------- */
1079 /* --------------------------------------------------------------------- */
1081 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_enter(struct sljit_compiler
*compiler
,
1082 sljit_s32 options
, sljit_s32 arg_types
, sljit_s32 scratches
, sljit_s32 saveds
,
1083 sljit_s32 fscratches
, sljit_s32 fsaveds
, sljit_s32 local_size
)
1085 sljit_s32 size
, i
, tmp
, word_arg_count
, saved_arg_count
;
1089 sljit_u32 float_arg_count
;
1091 sljit_u32 old_offset
, f32_offset
;
1093 sljit_u32
*remap_ptr
= remap
;
1097 CHECK(check_sljit_emit_enter(compiler
, options
, arg_types
, scratches
, saveds
, fscratches
, fsaveds
, local_size
));
1098 set_emit_enter(compiler
, options
, arg_types
, scratches
, saveds
, fscratches
, fsaveds
, local_size
);
1100 tmp
= SLJIT_S0
- saveds
;
1101 for (i
= SLJIT_S0
; i
> tmp
; i
--)
1102 imm
|= (sljit_uw
)1 << reg_map
[i
];
1104 for (i
= scratches
; i
>= SLJIT_FIRST_SAVED_REG
; i
--)
1105 imm
|= (sljit_uw
)1 << reg_map
[i
];
1107 /* At least two registers must be set for PUSH_W and one for PUSH instruction. */
1108 FAIL_IF((imm
& 0xff00)
1109 ? push_inst32(compiler
, PUSH_W
| (1 << 14) | imm
)
1110 : push_inst16(compiler
, PUSH
| (1 << 8) | imm
));
1112 /* Stack must be aligned to 8 bytes: (LR, R4) */
1113 size
= GET_SAVED_REGISTERS_SIZE(scratches
, saveds
, 1);
1115 if (fsaveds
> 0 || fscratches
>= SLJIT_FIRST_SAVED_FLOAT_REG
) {
1116 if ((size
& SSIZE_OF(sw
)) != 0) {
1117 FAIL_IF(push_inst16(compiler
, SUB_SP_I
| (sizeof(sljit_sw
) >> 2)));
1118 size
+= SSIZE_OF(sw
);
1121 if (fsaveds
+ fscratches
>= SLJIT_NUMBER_OF_FLOAT_REGISTERS
) {
1122 FAIL_IF(push_inst32(compiler
, VPUSH
| DD4(SLJIT_FS0
) | ((sljit_uw
)SLJIT_NUMBER_OF_SAVED_FLOAT_REGISTERS
<< 1)));
1125 FAIL_IF(push_inst32(compiler
, VPUSH
| DD4(SLJIT_FS0
) | ((sljit_uw
)fsaveds
<< 1)));
1126 if (fscratches
>= SLJIT_FIRST_SAVED_FLOAT_REG
)
1127 FAIL_IF(push_inst32(compiler
, VPUSH
| DD4(fscratches
) | ((sljit_uw
)(fscratches
- (SLJIT_FIRST_SAVED_FLOAT_REG
- 1)) << 1)));
1131 local_size
= ((size
+ local_size
+ 0x7) & ~0x7) - size
;
1132 compiler
->local_size
= local_size
;
1134 arg_types
>>= SLJIT_ARG_SHIFT
;
1136 saved_arg_count
= 0;
1138 SLJIT_COMPILE_ASSERT(SLJIT_FR0
== 1, float_register_index_start
);
1141 float_arg_count
= 0;
1144 switch (arg_types
& SLJIT_ARG_MASK
) {
1145 case SLJIT_ARG_TYPE_F64
:
1147 offset
+= sizeof(sljit_sw
);
1149 if (offset
< 4 * sizeof(sljit_sw
))
1150 FAIL_IF(push_inst32(compiler
, VMOV2
| (offset
<< 10) | ((offset
+ sizeof(sljit_sw
)) << 14) | float_arg_count
));
1152 FAIL_IF(push_inst32(compiler
, VLDR_F32
| 0x800100 | RN4(SLJIT_SP
)
1153 | (float_arg_count
<< 12) | ((offset
+ (sljit_uw
)size
- 4 * sizeof(sljit_sw
)) >> 2)));
1155 offset
+= sizeof(sljit_f64
) - sizeof(sljit_sw
);
1157 case SLJIT_ARG_TYPE_F32
:
1158 if (offset
< 4 * sizeof(sljit_sw
))
1159 FAIL_IF(push_inst32(compiler
, VMOV
| (float_arg_count
<< 16) | (offset
<< 10)));
1161 FAIL_IF(push_inst32(compiler
, VLDR_F32
| 0x800000 | RN4(SLJIT_SP
)
1162 | (float_arg_count
<< 12) | ((offset
+ (sljit_uw
)size
- 4 * sizeof(sljit_sw
)) >> 2)));
1168 if (!(arg_types
& SLJIT_ARG_TYPE_SCRATCH_REG
)) {
1169 tmp
= SLJIT_S0
- saved_arg_count
;
1171 } else if (word_arg_count
- 1 != (sljit_s32
)(offset
>> 2))
1172 tmp
= word_arg_count
;
1176 SLJIT_ASSERT(reg_map
[tmp
] <= 7);
1178 if (offset
< 4 * sizeof(sljit_sw
))
1179 FAIL_IF(push_inst16(compiler
, MOV
| RD3(tmp
) | (offset
<< 1)));
1181 FAIL_IF(push_inst16(compiler
, LDR_SP
| RDN3(tmp
)
1182 | ((offset
+ (sljit_uw
)size
- 4 * sizeof(sljit_sw
)) >> 2)));
1186 offset
+= sizeof(sljit_sw
);
1187 arg_types
>>= SLJIT_ARG_SHIFT
;
1190 compiler
->args_size
= offset
;
1193 old_offset
= SLJIT_FR0
;
1197 switch (arg_types
& SLJIT_ARG_MASK
) {
1198 case SLJIT_ARG_TYPE_F64
:
1199 if (offset
!= old_offset
)
1200 *remap_ptr
++ = VMOV_F32
| SLJIT_32
| DD4(offset
) | DM4(old_offset
);
1204 case SLJIT_ARG_TYPE_F32
:
1205 if (f32_offset
!= 0) {
1206 *remap_ptr
++ = VMOV_F32
| 0x20 | DD4(offset
) | DM4(f32_offset
);
1209 if (offset
!= old_offset
)
1210 *remap_ptr
++ = VMOV_F32
| DD4(offset
) | DM4(old_offset
);
1211 f32_offset
= old_offset
;
1217 if (!(arg_types
& SLJIT_ARG_TYPE_SCRATCH_REG
)) {
1218 FAIL_IF(push_inst16(compiler
, MOV
| SET_REGS44(SLJIT_S0
- saved_arg_count
, SLJIT_R0
+ word_arg_count
)));
1225 arg_types
>>= SLJIT_ARG_SHIFT
;
1228 SLJIT_ASSERT((sljit_uw
)(remap_ptr
- remap
) <= sizeof(remap
));
1230 while (remap_ptr
> remap
)
1231 FAIL_IF(push_inst32(compiler
, *(--remap_ptr
)));
1235 if (local_size
>= 4096) {
1236 imm
= get_imm(4096);
1237 SLJIT_ASSERT(imm
!= INVALID_IMM
);
1239 FAIL_IF(push_inst32(compiler
, SUB_WI
| RD4(SLJIT_SP
) | RN4(SLJIT_SP
) | imm
));
1241 if (local_size
< 4 * 4096) {
1242 if (local_size
> 2 * 4096) {
1243 if (local_size
> 3 * 4096) {
1244 FAIL_IF(push_inst32(compiler
, LDRI
| 0x400 | RT4(TMP_REG1
) | RN4(SLJIT_SP
)));
1245 FAIL_IF(push_inst32(compiler
, SUB_WI
| RD4(SLJIT_SP
) | RN4(SLJIT_SP
) | imm
));
1248 FAIL_IF(push_inst32(compiler
, LDRI
| 0x400 | RT4(TMP_REG1
) | RN4(SLJIT_SP
)));
1249 FAIL_IF(push_inst32(compiler
, SUB_WI
| RD4(SLJIT_SP
) | RN4(SLJIT_SP
) | imm
));
1252 FAIL_IF(load_immediate(compiler
, TMP_REG2
, ((sljit_uw
)local_size
>> 12) - 1));
1253 FAIL_IF(push_inst32(compiler
, LDRI
| 0x400 | RT4(TMP_REG1
) | RN4(SLJIT_SP
)));
1254 FAIL_IF(push_inst32(compiler
, SUB_WI
| RD4(SLJIT_SP
) | RN4(SLJIT_SP
) | imm
));
1255 FAIL_IF(push_inst32(compiler
, SUB_WI
| SET_FLAGS
| RD4(TMP_REG2
) | RN4(TMP_REG2
) | 1));
1256 FAIL_IF(push_inst16(compiler
, BCC
| (0x1 << 8) /* not-equal */ | (-8 & 0xff)));
1259 FAIL_IF(push_inst32(compiler
, LDRI
| 0x400 | RT4(TMP_REG1
) | RN4(SLJIT_SP
)));
1260 local_size
&= 0xfff;
1263 if (local_size
>= 256) {
1264 SLJIT_ASSERT(local_size
< 4096);
1266 if (local_size
<= (127 << 2))
1267 FAIL_IF(push_inst16(compiler
, SUB_SP_I
| ((sljit_uw
)local_size
>> 2)));
1269 FAIL_IF(emit_op_imm(compiler
, SLJIT_SUB
| ARG2_IMM
, SLJIT_SP
, SLJIT_SP
, (sljit_uw
)local_size
));
1271 FAIL_IF(push_inst32(compiler
, LDRI
| 0x400 | RT4(TMP_REG1
) | RN4(SLJIT_SP
)));
1272 } else if (local_size
> 0)
1273 FAIL_IF(push_inst32(compiler
, LDRI
| 0x500 | RT4(TMP_REG1
) | RN4(SLJIT_SP
) | (sljit_uw
)local_size
));
1275 if (local_size
> 0) {
1276 if (local_size
<= (127 << 2))
1277 FAIL_IF(push_inst16(compiler
, SUB_SP_I
| ((sljit_uw
)local_size
>> 2)));
1279 FAIL_IF(emit_op_imm(compiler
, SLJIT_SUB
| ARG2_IMM
, SLJIT_SP
, SLJIT_SP
, (sljit_uw
)local_size
));
1283 return SLJIT_SUCCESS
;
1286 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_set_context(struct sljit_compiler
*compiler
,
1287 sljit_s32 options
, sljit_s32 arg_types
, sljit_s32 scratches
, sljit_s32 saveds
,
1288 sljit_s32 fscratches
, sljit_s32 fsaveds
, sljit_s32 local_size
)
1293 CHECK(check_sljit_set_context(compiler
, options
, arg_types
, scratches
, saveds
, fscratches
, fsaveds
, local_size
));
1294 set_set_context(compiler
, options
, arg_types
, scratches
, saveds
, fscratches
, fsaveds
, local_size
);
1296 size
= GET_SAVED_REGISTERS_SIZE(scratches
, saveds
, 1);
1298 if ((size
& SSIZE_OF(sw
)) != 0 && (fsaveds
> 0 || fscratches
>= SLJIT_FIRST_SAVED_FLOAT_REG
))
1299 size
+= SSIZE_OF(sw
);
1301 compiler
->local_size
= ((size
+ local_size
+ 0x7) & ~0x7) - size
;
1302 return SLJIT_SUCCESS
;
1305 static sljit_s32
emit_add_sp(struct sljit_compiler
*compiler
, sljit_uw imm
)
1309 /* The TMP_REG1 register must keep its value. */
1310 if (imm
<= (127u << 2))
1311 return push_inst16(compiler
, ADD_SP_I
| (imm
>> 2));
1314 return push_inst32(compiler
, ADDWI
| RD4(SLJIT_SP
) | RN4(SLJIT_SP
) | IMM12(imm
));
1316 imm2
= get_imm(imm
);
1318 if (imm2
!= INVALID_IMM
)
1319 return push_inst32(compiler
, ADD_WI
| RD4(SLJIT_SP
) | RN4(SLJIT_SP
) | imm2
);
1321 FAIL_IF(load_immediate(compiler
, TMP_REG2
, imm
));
1322 return push_inst16(compiler
, ADD_SP
| RN3(TMP_REG2
));
1325 static sljit_s32
emit_stack_frame_release(struct sljit_compiler
*compiler
, sljit_s32 frame_size
)
1327 sljit_s32 local_size
, fscratches
, fsaveds
, i
, tmp
;
1328 sljit_s32 lr_dst
= TMP_PC
;
1331 SLJIT_ASSERT(reg_map
[TMP_REG2
] == 14 && frame_size
<= 128);
1333 local_size
= compiler
->local_size
;
1334 fscratches
= compiler
->fscratches
;
1335 fsaveds
= compiler
->fsaveds
;
1337 if (fsaveds
> 0 || fscratches
>= SLJIT_FIRST_SAVED_FLOAT_REG
) {
1339 FAIL_IF(emit_add_sp(compiler
, (sljit_uw
)local_size
));
1341 if (fsaveds
+ fscratches
>= SLJIT_NUMBER_OF_FLOAT_REGISTERS
) {
1342 FAIL_IF(push_inst32(compiler
, VPOP
| DD4(SLJIT_FS0
) | ((sljit_uw
)SLJIT_NUMBER_OF_SAVED_FLOAT_REGISTERS
<< 1)));
1344 if (fscratches
>= SLJIT_FIRST_SAVED_FLOAT_REG
)
1345 FAIL_IF(push_inst32(compiler
, VPOP
| DD4(fscratches
) | ((sljit_uw
)(fscratches
- (SLJIT_FIRST_SAVED_FLOAT_REG
- 1)) << 1)));
1347 FAIL_IF(push_inst32(compiler
, VPOP
| DD4(SLJIT_FS0
) | ((sljit_uw
)fsaveds
<< 1)));
1350 local_size
= GET_SAVED_REGISTERS_SIZE(compiler
->scratches
, compiler
->saveds
, 1) & 0x7;
1353 if (frame_size
< 0) {
1356 } else if (frame_size
> 0)
1360 tmp
= SLJIT_S0
- compiler
->saveds
;
1361 for (i
= SLJIT_S0
; i
> tmp
; i
--)
1362 reg_list
|= (sljit_uw
)1 << reg_map
[i
];
1364 for (i
= compiler
->scratches
; i
>= SLJIT_FIRST_SAVED_REG
; i
--)
1365 reg_list
|= (sljit_uw
)1 << reg_map
[i
];
1367 if (lr_dst
== 0 && (reg_list
& (reg_list
- 1)) == 0) {
1368 /* The local_size does not include the saved registers. */
1369 local_size
+= SSIZE_OF(sw
);
1372 local_size
+= SSIZE_OF(sw
);
1374 if (frame_size
> local_size
)
1375 FAIL_IF(push_inst16(compiler
, SUB_SP_I
| ((sljit_uw
)(frame_size
- local_size
) >> 2)));
1376 else if (frame_size
< local_size
)
1377 FAIL_IF(emit_add_sp(compiler
, (sljit_uw
)(local_size
- frame_size
)));
1380 return SLJIT_SUCCESS
;
1382 if (compiler
->saveds
> 0) {
1383 SLJIT_ASSERT(reg_list
== ((sljit_uw
)1 << reg_map
[SLJIT_S0
]));
1386 SLJIT_ASSERT(reg_list
== ((sljit_uw
)1 << reg_map
[SLJIT_FIRST_SAVED_REG
]));
1387 lr_dst
= SLJIT_FIRST_SAVED_REG
;
1390 frame_size
-= 2 * SSIZE_OF(sw
);
1392 if (reg_map
[lr_dst
] <= 7)
1393 return push_inst16(compiler
, STR_SP
| 0x800 | RDN3(lr_dst
) | (sljit_uw
)(frame_size
>> 2));
1395 return push_inst32(compiler
, LDR
| RT4(lr_dst
) | RN4(SLJIT_SP
) | (sljit_uw
)frame_size
);
1399 FAIL_IF(emit_add_sp(compiler
, (sljit_uw
)local_size
));
1401 if (!(reg_list
& 0xff00) && lr_dst
!= TMP_REG2
) {
1402 if (lr_dst
== TMP_PC
)
1403 reg_list
|= 1u << 8;
1405 /* At least one register must be set for POP instruction. */
1406 SLJIT_ASSERT(reg_list
!= 0);
1408 FAIL_IF(push_inst16(compiler
, POP
| reg_list
));
1412 return push_inst32(compiler
, 0xf85d0b04 | RT4(lr_dst
));
1414 reg_list
|= (sljit_uw
)1 << reg_map
[lr_dst
];
1417 /* At least two registers must be set for POP_W instruction. */
1418 SLJIT_ASSERT((reg_list
& (reg_list
- 1)) != 0);
1420 FAIL_IF(push_inst32(compiler
, POP_W
| reg_list
));
1424 return push_inst16(compiler
, SUB_SP_I
| (((sljit_uw
)frame_size
- sizeof(sljit_sw
)) >> 2));
1425 return SLJIT_SUCCESS
;
1428 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_return_void(struct sljit_compiler
*compiler
)
1431 CHECK(check_sljit_emit_return_void(compiler
));
1433 return emit_stack_frame_release(compiler
, 0);
1436 /* --------------------------------------------------------------------- */
1438 /* --------------------------------------------------------------------- */
1440 #if !(defined __ARM_FEATURE_IDIV) && !(defined __ARM_ARCH_EXT_IDIV__)
1447 extern unsigned long long __rt_udiv(unsigned int denominator
, unsigned int numerator
);
1448 extern long long __rt_sdiv(int denominator
, int numerator
);
1449 #elif defined(__GNUC__)
1450 extern unsigned int __aeabi_uidivmod(unsigned int numerator
, int unsigned denominator
);
1451 extern int __aeabi_idivmod(int numerator
, int denominator
);
1453 #error "Software divmod functions are needed"
1460 #endif /* !__ARM_FEATURE_IDIV && !__ARM_ARCH_EXT_IDIV__ */
1462 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op0(struct sljit_compiler
*compiler
, sljit_s32 op
)
1464 #if !(defined __ARM_FEATURE_IDIV) && !(defined __ARM_ARCH_EXT_IDIV__)
1465 sljit_uw saved_reg_list
[3];
1466 sljit_uw saved_reg_count
;
1470 CHECK(check_sljit_emit_op0(compiler
, op
));
1472 op
= GET_OPCODE(op
);
1474 case SLJIT_BREAKPOINT
:
1475 return push_inst16(compiler
, BKPT
);
1477 return push_inst16(compiler
, NOP
);
1480 return push_inst32(compiler
, (op
== SLJIT_LMUL_UW
? UMULL
: SMULL
)
1481 | RD4(SLJIT_R1
) | RT4(SLJIT_R0
) | RN4(SLJIT_R0
) | RM4(SLJIT_R1
));
1482 #if (defined __ARM_FEATURE_IDIV) || (defined __ARM_ARCH_EXT_IDIV__)
1483 case SLJIT_DIVMOD_UW
:
1484 case SLJIT_DIVMOD_SW
:
1485 FAIL_IF(push_inst16(compiler
, MOV
| SET_REGS44(TMP_REG1
, SLJIT_R0
)));
1486 FAIL_IF(push_inst32(compiler
, (op
== SLJIT_DIVMOD_UW
? UDIV
: SDIV
) | RD4(SLJIT_R0
) | RN4(SLJIT_R0
) | RM4(SLJIT_R1
)));
1487 FAIL_IF(push_inst32(compiler
, MUL
| RD4(SLJIT_R1
) | RN4(SLJIT_R0
) | RM4(SLJIT_R1
)));
1488 return push_inst32(compiler
, SUB_W
| RD4(SLJIT_R1
) | RN4(TMP_REG1
) | RM4(SLJIT_R1
));
1491 return push_inst32(compiler
, (op
== SLJIT_DIV_UW
? UDIV
: SDIV
) | RD4(SLJIT_R0
) | RN4(SLJIT_R0
) | RM4(SLJIT_R1
));
1492 #else /* !__ARM_FEATURE_IDIV && !__ARM_ARCH_EXT_IDIV__ */
1493 case SLJIT_DIVMOD_UW
:
1494 case SLJIT_DIVMOD_SW
:
1497 SLJIT_COMPILE_ASSERT((SLJIT_DIVMOD_UW
& 0x2) == 0 && SLJIT_DIV_UW
- 0x2 == SLJIT_DIVMOD_UW
, bad_div_opcode_assignments
);
1498 SLJIT_ASSERT(reg_map
[2] == 1 && reg_map
[3] == 2 && reg_map
[4] == 3);
1500 saved_reg_count
= 0;
1501 if (compiler
->scratches
>= 4)
1502 saved_reg_list
[saved_reg_count
++] = 3;
1503 if (compiler
->scratches
>= 3)
1504 saved_reg_list
[saved_reg_count
++] = 2;
1505 if (op
>= SLJIT_DIV_UW
)
1506 saved_reg_list
[saved_reg_count
++] = 1;
1508 if (saved_reg_count
> 0) {
1509 FAIL_IF(push_inst32(compiler
, 0xf84d0d00 | (saved_reg_count
>= 3 ? 16 : 8)
1510 | (saved_reg_list
[0] << 12) /* str rX, [sp, #-8/-16]! */));
1511 if (saved_reg_count
>= 2) {
1512 SLJIT_ASSERT(saved_reg_list
[1] < 8);
1513 FAIL_IF(push_inst16(compiler
, 0x9001 | (saved_reg_list
[1] << 8) /* str rX, [sp, #4] */));
1515 if (saved_reg_count
>= 3) {
1516 SLJIT_ASSERT(saved_reg_list
[2] < 8);
1517 FAIL_IF(push_inst16(compiler
, 0x9002 | (saved_reg_list
[2] << 8) /* str rX, [sp, #8] */));
1522 FAIL_IF(push_inst16(compiler
, MOV
| SET_REGS44(TMP_REG1
, SLJIT_R0
)));
1523 FAIL_IF(push_inst16(compiler
, MOV
| SET_REGS44(SLJIT_R0
, SLJIT_R1
)));
1524 FAIL_IF(push_inst16(compiler
, MOV
| SET_REGS44(SLJIT_R1
, TMP_REG1
)));
1525 FAIL_IF(sljit_emit_ijump(compiler
, SLJIT_FAST_CALL
, SLJIT_IMM
,
1526 ((op
| 0x2) == SLJIT_DIV_UW
? SLJIT_FUNC_ADDR(__rt_udiv
) : SLJIT_FUNC_ADDR(__rt_sdiv
))));
1527 #elif defined(__GNUC__)
1528 FAIL_IF(sljit_emit_ijump(compiler
, SLJIT_FAST_CALL
, SLJIT_IMM
,
1529 ((op
| 0x2) == SLJIT_DIV_UW
? SLJIT_FUNC_ADDR(__aeabi_uidivmod
) : SLJIT_FUNC_ADDR(__aeabi_idivmod
))));
1531 #error "Software divmod functions are needed"
1534 if (saved_reg_count
> 0) {
1535 if (saved_reg_count
>= 3) {
1536 SLJIT_ASSERT(saved_reg_list
[2] < 8);
1537 FAIL_IF(push_inst16(compiler
, 0x9802 | (saved_reg_list
[2] << 8) /* ldr rX, [sp, #8] */));
1539 if (saved_reg_count
>= 2) {
1540 SLJIT_ASSERT(saved_reg_list
[1] < 8);
1541 FAIL_IF(push_inst16(compiler
, 0x9801 | (saved_reg_list
[1] << 8) /* ldr rX, [sp, #4] */));
1543 return push_inst32(compiler
, 0xf85d0b00 | (saved_reg_count
>= 3 ? 16 : 8)
1544 | (saved_reg_list
[0] << 12) /* ldr rX, [sp], #8/16 */);
1546 return SLJIT_SUCCESS
;
1547 #endif /* __ARM_FEATURE_IDIV || __ARM_ARCH_EXT_IDIV__ */
1549 case SLJIT_SKIP_FRAMES_BEFORE_RETURN
:
1550 return SLJIT_SUCCESS
;
1553 return SLJIT_SUCCESS
;
1556 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op1(struct sljit_compiler
*compiler
, sljit_s32 op
,
1557 sljit_s32 dst
, sljit_sw dstw
,
1558 sljit_s32 src
, sljit_sw srcw
)
1560 sljit_s32 dst_r
, flags
;
1561 sljit_s32 op_flags
= GET_ALL_FLAGS(op
);
1564 CHECK(check_sljit_emit_op1(compiler
, op
, dst
, dstw
, src
, srcw
));
1565 ADJUST_LOCAL_OFFSET(dst
, dstw
);
1566 ADJUST_LOCAL_OFFSET(src
, srcw
);
1568 dst_r
= FAST_IS_REG(dst
) ? dst
: TMP_REG1
;
1570 op
= GET_OPCODE(op
);
1571 if (op
>= SLJIT_MOV
&& op
<= SLJIT_MOV_P
) {
1582 if (src
& SLJIT_IMM
)
1583 srcw
= (sljit_u8
)srcw
;
1586 flags
= BYTE_SIZE
| SIGNED
;
1587 if (src
& SLJIT_IMM
)
1588 srcw
= (sljit_s8
)srcw
;
1592 if (src
& SLJIT_IMM
)
1593 srcw
= (sljit_u16
)srcw
;
1596 flags
= HALF_SIZE
| SIGNED
;
1597 if (src
& SLJIT_IMM
)
1598 srcw
= (sljit_s16
)srcw
;
1601 SLJIT_UNREACHABLE();
1606 if (src
& SLJIT_IMM
)
1607 FAIL_IF(emit_op_imm(compiler
, SLJIT_MOV
| ARG2_IMM
, dst_r
, TMP_REG2
, (sljit_uw
)srcw
));
1608 else if (src
& SLJIT_MEM
) {
1609 FAIL_IF(emit_op_mem(compiler
, flags
, dst_r
, src
, srcw
, TMP_REG1
));
1611 if (dst_r
!= TMP_REG1
)
1612 return emit_op_imm(compiler
, op
, dst_r
, TMP_REG2
, (sljit_uw
)src
);
1616 if (!(dst
& SLJIT_MEM
))
1617 return SLJIT_SUCCESS
;
1619 return emit_op_mem(compiler
, flags
| STORE
, dst_r
, dst
, dstw
, TMP_REG2
);
1622 if (op
== SLJIT_NEG
) {
1623 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
1624 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
1625 compiler
->skip_checks
= 1;
1627 return sljit_emit_op2(compiler
, SLJIT_SUB
| op_flags
, dst
, dstw
, SLJIT_IMM
, 0, src
, srcw
);
1630 flags
= HAS_FLAGS(op_flags
) ? SET_FLAGS
: 0;
1632 if (src
& SLJIT_MEM
) {
1633 FAIL_IF(emit_op_mem(compiler
, WORD_SIZE
, TMP_REG1
, src
, srcw
, TMP_REG1
));
1637 emit_op_imm(compiler
, flags
| op
, dst_r
, TMP_REG2
, (sljit_uw
)src
);
1639 if (SLJIT_UNLIKELY(dst
& SLJIT_MEM
))
1640 return emit_op_mem(compiler
, flags
| STORE
, dst_r
, dst
, dstw
, TMP_REG2
);
1641 return SLJIT_SUCCESS
;
1644 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op2(struct sljit_compiler
*compiler
, sljit_s32 op
,
1645 sljit_s32 dst
, sljit_sw dstw
,
1646 sljit_s32 src1
, sljit_sw src1w
,
1647 sljit_s32 src2
, sljit_sw src2w
)
1649 sljit_s32 dst_reg
, flags
, src2_reg
;
1652 CHECK(check_sljit_emit_op2(compiler
, op
, 0, dst
, dstw
, src1
, src1w
, src2
, src2w
));
1653 ADJUST_LOCAL_OFFSET(dst
, dstw
);
1654 ADJUST_LOCAL_OFFSET(src1
, src1w
);
1655 ADJUST_LOCAL_OFFSET(src2
, src2w
);
1657 dst_reg
= FAST_IS_REG(dst
) ? dst
: TMP_REG1
;
1658 flags
= HAS_FLAGS(op
) ? SET_FLAGS
: 0;
1660 if (dst
== TMP_REG1
)
1661 flags
|= UNUSED_RETURN
;
1663 if (src1
& SLJIT_IMM
)
1665 else if (src1
& SLJIT_MEM
) {
1666 emit_op_mem(compiler
, WORD_SIZE
, TMP_REG1
, src1
, src1w
, TMP_REG1
);
1672 if (src2
& SLJIT_IMM
)
1674 else if (src2
& SLJIT_MEM
) {
1675 src2_reg
= (!(flags
& ARG1_IMM
) && (src1w
== TMP_REG1
)) ? TMP_REG2
: TMP_REG1
;
1676 emit_op_mem(compiler
, WORD_SIZE
, src2_reg
, src2
, src2w
, src2_reg
);
1682 emit_op_imm(compiler
, flags
| GET_OPCODE(op
), dst_reg
, (sljit_uw
)src1w
, (sljit_uw
)src2w
);
1684 if (!(dst
& SLJIT_MEM
))
1685 return SLJIT_SUCCESS
;
1686 return emit_op_mem(compiler
, WORD_SIZE
| STORE
, dst_reg
, dst
, dstw
, TMP_REG2
);
1689 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op2u(struct sljit_compiler
*compiler
, sljit_s32 op
,
1690 sljit_s32 src1
, sljit_sw src1w
,
1691 sljit_s32 src2
, sljit_sw src2w
)
1694 CHECK(check_sljit_emit_op2(compiler
, op
, 1, 0, 0, src1
, src1w
, src2
, src2w
));
1696 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
1697 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
1698 compiler
->skip_checks
= 1;
1700 return sljit_emit_op2(compiler
, op
, TMP_REG1
, 0, src1
, src1w
, src2
, src2w
);
1703 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op_src(struct sljit_compiler
*compiler
, sljit_s32 op
,
1704 sljit_s32 src
, sljit_sw srcw
)
1707 CHECK(check_sljit_emit_op_src(compiler
, op
, src
, srcw
));
1708 ADJUST_LOCAL_OFFSET(src
, srcw
);
1711 case SLJIT_FAST_RETURN
:
1712 SLJIT_ASSERT(reg_map
[TMP_REG2
] == 14);
1714 if (FAST_IS_REG(src
))
1715 FAIL_IF(push_inst16(compiler
, MOV
| SET_REGS44(TMP_REG2
, src
)));
1717 FAIL_IF(emit_op_mem(compiler
, WORD_SIZE
, TMP_REG2
, src
, srcw
, TMP_REG2
));
1719 return push_inst16(compiler
, BX
| RN3(TMP_REG2
));
1720 case SLJIT_SKIP_FRAMES_BEFORE_FAST_RETURN
:
1721 return SLJIT_SUCCESS
;
1722 case SLJIT_PREFETCH_L1
:
1723 case SLJIT_PREFETCH_L2
:
1724 case SLJIT_PREFETCH_L3
:
1725 case SLJIT_PREFETCH_ONCE
:
1726 return emit_op_mem(compiler
, PRELOAD
, TMP_PC
, src
, srcw
, TMP_REG1
);
1729 return SLJIT_SUCCESS
;
1732 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_get_register_index(sljit_s32 reg
)
1734 CHECK_REG_INDEX(check_sljit_get_register_index(reg
));
1735 return reg_map
[reg
];
1738 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_get_float_register_index(sljit_s32 reg
)
1740 CHECK_REG_INDEX(check_sljit_get_float_register_index(reg
));
1741 return (freg_map
[reg
] << 1);
1744 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op_custom(struct sljit_compiler
*compiler
,
1745 void *instruction
, sljit_u32 size
)
1748 CHECK(check_sljit_emit_op_custom(compiler
, instruction
, size
));
1751 return push_inst16(compiler
, *(sljit_u16
*)instruction
);
1752 return push_inst32(compiler
, *(sljit_ins
*)instruction
);
1755 /* --------------------------------------------------------------------- */
1756 /* Floating point operators */
1757 /* --------------------------------------------------------------------- */
1759 #define FPU_LOAD (1 << 20)
1761 static sljit_s32
emit_fop_mem(struct sljit_compiler
*compiler
, sljit_s32 flags
, sljit_s32 reg
, sljit_s32 arg
, sljit_sw argw
)
1764 sljit_ins inst
= VSTR_F32
| (flags
& (SLJIT_32
| FPU_LOAD
));
1766 SLJIT_ASSERT(arg
& SLJIT_MEM
);
1768 /* Fast loads and stores. */
1769 if (SLJIT_UNLIKELY(arg
& OFFS_REG_MASK
)) {
1770 FAIL_IF(push_inst32(compiler
, ADD_W
| RD4(TMP_REG1
) | RN4(arg
& REG_MASK
) | RM4(OFFS_REG(arg
)) | (((sljit_uw
)argw
& 0x3) << 6)));
1771 arg
= SLJIT_MEM
| TMP_REG1
;
1775 if ((arg
& REG_MASK
) && (argw
& 0x3) == 0) {
1776 if (!(argw
& ~0x3fc))
1777 return push_inst32(compiler
, inst
| 0x800000 | RN4(arg
& REG_MASK
) | DD4(reg
) | ((sljit_uw
)argw
>> 2));
1778 if (!(-argw
& ~0x3fc))
1779 return push_inst32(compiler
, inst
| RN4(arg
& REG_MASK
) | DD4(reg
) | ((sljit_uw
)-argw
>> 2));
1782 if (arg
& REG_MASK
) {
1783 if (emit_set_delta(compiler
, TMP_REG1
, arg
& REG_MASK
, argw
) != SLJIT_ERR_UNSUPPORTED
) {
1784 FAIL_IF(compiler
->error
);
1785 return push_inst32(compiler
, inst
| 0x800000 | RN4(TMP_REG1
) | DD4(reg
));
1788 imm
= get_imm((sljit_uw
)argw
& ~(sljit_uw
)0x3fc);
1789 if (imm
!= INVALID_IMM
) {
1790 FAIL_IF(push_inst32(compiler
, ADD_WI
| RD4(TMP_REG1
) | RN4(arg
& REG_MASK
) | imm
));
1791 return push_inst32(compiler
, inst
| 0x800000 | RN4(TMP_REG1
) | DD4(reg
) | (((sljit_uw
)argw
& 0x3fc) >> 2));
1794 imm
= get_imm((sljit_uw
)-argw
& ~(sljit_uw
)0x3fc);
1795 if (imm
!= INVALID_IMM
) {
1797 FAIL_IF(push_inst32(compiler
, SUB_WI
| RD4(TMP_REG1
) | RN4(arg
& REG_MASK
) | imm
));
1798 return push_inst32(compiler
, inst
| RN4(TMP_REG1
) | DD4(reg
) | (((sljit_uw
)argw
& 0x3fc) >> 2));
1802 FAIL_IF(load_immediate(compiler
, TMP_REG1
, (sljit_uw
)argw
));
1804 FAIL_IF(push_inst16(compiler
, ADD
| SET_REGS44(TMP_REG1
, (arg
& REG_MASK
))));
1805 return push_inst32(compiler
, inst
| 0x800000 | RN4(TMP_REG1
) | DD4(reg
));
1808 static SLJIT_INLINE sljit_s32
sljit_emit_fop1_conv_sw_from_f64(struct sljit_compiler
*compiler
, sljit_s32 op
,
1809 sljit_s32 dst
, sljit_sw dstw
,
1810 sljit_s32 src
, sljit_sw srcw
)
1814 if (src
& SLJIT_MEM
) {
1815 FAIL_IF(emit_fop_mem(compiler
, (op
& SLJIT_32
) | FPU_LOAD
, TMP_FREG1
, src
, srcw
));
1819 FAIL_IF(push_inst32(compiler
, VCVT_S32_F32
| (op
& SLJIT_32
) | DD4(TMP_FREG1
) | DM4(src
)));
1821 if (FAST_IS_REG(dst
))
1822 return push_inst32(compiler
, VMOV
| (1 << 20) | RT4(dst
) | DN4(TMP_FREG1
));
1824 /* Store the integer value from a VFP register. */
1825 return emit_fop_mem(compiler
, 0, TMP_FREG1
, dst
, dstw
);
1828 static SLJIT_INLINE sljit_s32
sljit_emit_fop1_conv_f64_from_sw(struct sljit_compiler
*compiler
, sljit_s32 op
,
1829 sljit_s32 dst
, sljit_sw dstw
,
1830 sljit_s32 src
, sljit_sw srcw
)
1832 sljit_s32 dst_r
= FAST_IS_REG(dst
) ? dst
: TMP_FREG1
;
1836 if (FAST_IS_REG(src
))
1837 FAIL_IF(push_inst32(compiler
, VMOV
| RT4(src
) | DN4(TMP_FREG1
)));
1838 else if (src
& SLJIT_MEM
) {
1839 /* Load the integer value into a VFP register. */
1840 FAIL_IF(emit_fop_mem(compiler
, FPU_LOAD
, TMP_FREG1
, src
, srcw
));
1843 FAIL_IF(load_immediate(compiler
, TMP_REG1
, (sljit_uw
)srcw
));
1844 FAIL_IF(push_inst32(compiler
, VMOV
| RT4(TMP_REG1
) | DN4(TMP_FREG1
)));
1847 FAIL_IF(push_inst32(compiler
, VCVT_F32_S32
| (op
& SLJIT_32
) | DD4(dst_r
) | DM4(TMP_FREG1
)));
1849 if (dst
& SLJIT_MEM
)
1850 return emit_fop_mem(compiler
, (op
& SLJIT_32
), TMP_FREG1
, dst
, dstw
);
1851 return SLJIT_SUCCESS
;
1854 static SLJIT_INLINE sljit_s32
sljit_emit_fop1_cmp(struct sljit_compiler
*compiler
, sljit_s32 op
,
1855 sljit_s32 src1
, sljit_sw src1w
,
1856 sljit_s32 src2
, sljit_sw src2w
)
1860 if (src1
& SLJIT_MEM
) {
1861 emit_fop_mem(compiler
, (op
& SLJIT_32
) | FPU_LOAD
, TMP_FREG1
, src1
, src1w
);
1865 if (src2
& SLJIT_MEM
) {
1866 emit_fop_mem(compiler
, (op
& SLJIT_32
) | FPU_LOAD
, TMP_FREG2
, src2
, src2w
);
1870 FAIL_IF(push_inst32(compiler
, VCMP_F32
| (op
& SLJIT_32
) | DD4(src1
) | DM4(src2
)));
1871 return push_inst32(compiler
, VMRS
);
1874 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_fop1(struct sljit_compiler
*compiler
, sljit_s32 op
,
1875 sljit_s32 dst
, sljit_sw dstw
,
1876 sljit_s32 src
, sljit_sw srcw
)
1882 SLJIT_COMPILE_ASSERT((SLJIT_32
== 0x100), float_transfer_bit_error
);
1883 SELECT_FOP1_OPERATION_WITH_CHECKS(compiler
, op
, dst
, dstw
, src
, srcw
);
1885 dst_r
= FAST_IS_REG(dst
) ? dst
: TMP_FREG1
;
1887 if (GET_OPCODE(op
) != SLJIT_CONV_F64_FROM_F32
)
1890 if (src
& SLJIT_MEM
) {
1891 emit_fop_mem(compiler
, (op
& SLJIT_32
) | FPU_LOAD
, dst_r
, src
, srcw
);
1895 switch (GET_OPCODE(op
)) {
1898 if (dst_r
!= TMP_FREG1
)
1899 FAIL_IF(push_inst32(compiler
, VMOV_F32
| (op
& SLJIT_32
) | DD4(dst_r
) | DM4(src
)));
1905 FAIL_IF(push_inst32(compiler
, VNEG_F32
| (op
& SLJIT_32
) | DD4(dst_r
) | DM4(src
)));
1908 FAIL_IF(push_inst32(compiler
, VABS_F32
| (op
& SLJIT_32
) | DD4(dst_r
) | DM4(src
)));
1910 case SLJIT_CONV_F64_FROM_F32
:
1911 FAIL_IF(push_inst32(compiler
, VCVT_F64_F32
| (op
& SLJIT_32
) | DD4(dst_r
) | DM4(src
)));
1916 if (dst
& SLJIT_MEM
)
1917 return emit_fop_mem(compiler
, (op
& SLJIT_32
), dst_r
, dst
, dstw
);
1918 return SLJIT_SUCCESS
;
1921 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_fop2(struct sljit_compiler
*compiler
, sljit_s32 op
,
1922 sljit_s32 dst
, sljit_sw dstw
,
1923 sljit_s32 src1
, sljit_sw src1w
,
1924 sljit_s32 src2
, sljit_sw src2w
)
1929 CHECK(check_sljit_emit_fop2(compiler
, op
, dst
, dstw
, src1
, src1w
, src2
, src2w
));
1930 ADJUST_LOCAL_OFFSET(dst
, dstw
);
1931 ADJUST_LOCAL_OFFSET(src1
, src1w
);
1932 ADJUST_LOCAL_OFFSET(src2
, src2w
);
1936 dst_r
= FAST_IS_REG(dst
) ? dst
: TMP_FREG1
;
1937 if (src1
& SLJIT_MEM
) {
1938 emit_fop_mem(compiler
, (op
& SLJIT_32
) | FPU_LOAD
, TMP_FREG1
, src1
, src1w
);
1941 if (src2
& SLJIT_MEM
) {
1942 emit_fop_mem(compiler
, (op
& SLJIT_32
) | FPU_LOAD
, TMP_FREG2
, src2
, src2w
);
1946 switch (GET_OPCODE(op
)) {
1948 FAIL_IF(push_inst32(compiler
, VADD_F32
| (op
& SLJIT_32
) | DD4(dst_r
) | DN4(src1
) | DM4(src2
)));
1951 FAIL_IF(push_inst32(compiler
, VSUB_F32
| (op
& SLJIT_32
) | DD4(dst_r
) | DN4(src1
) | DM4(src2
)));
1954 FAIL_IF(push_inst32(compiler
, VMUL_F32
| (op
& SLJIT_32
) | DD4(dst_r
) | DN4(src1
) | DM4(src2
)));
1957 FAIL_IF(push_inst32(compiler
, VDIV_F32
| (op
& SLJIT_32
) | DD4(dst_r
) | DN4(src1
) | DM4(src2
)));
1961 if (!(dst
& SLJIT_MEM
))
1962 return SLJIT_SUCCESS
;
1963 return emit_fop_mem(compiler
, (op
& SLJIT_32
), TMP_FREG1
, dst
, dstw
);
1968 /* --------------------------------------------------------------------- */
1969 /* Other instructions */
1970 /* --------------------------------------------------------------------- */
1972 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_fast_enter(struct sljit_compiler
*compiler
, sljit_s32 dst
, sljit_sw dstw
)
1975 CHECK(check_sljit_emit_fast_enter(compiler
, dst
, dstw
));
1976 ADJUST_LOCAL_OFFSET(dst
, dstw
);
1978 SLJIT_ASSERT(reg_map
[TMP_REG2
] == 14);
1980 if (FAST_IS_REG(dst
))
1981 return push_inst16(compiler
, MOV
| SET_REGS44(dst
, TMP_REG2
));
1984 return emit_op_mem(compiler
, WORD_SIZE
| STORE
, TMP_REG2
, dst
, dstw
, TMP_REG1
);
1987 /* --------------------------------------------------------------------- */
1988 /* Conditional instructions */
1989 /* --------------------------------------------------------------------- */
1991 static sljit_uw
get_cc(struct sljit_compiler
*compiler
, sljit_s32 type
)
1995 case SLJIT_EQUAL_F64
:
1998 case SLJIT_NOT_EQUAL
:
1999 case SLJIT_NOT_EQUAL_F64
:
2003 if (compiler
->status_flags_state
& SLJIT_CURRENT_FLAGS_ADD
)
2008 case SLJIT_LESS_F64
:
2011 case SLJIT_NOT_CARRY
:
2012 if (compiler
->status_flags_state
& SLJIT_CURRENT_FLAGS_ADD
)
2016 case SLJIT_GREATER_EQUAL
:
2017 case SLJIT_GREATER_EQUAL_F64
:
2021 case SLJIT_GREATER_F64
:
2024 case SLJIT_LESS_EQUAL
:
2025 case SLJIT_LESS_EQUAL_F64
:
2028 case SLJIT_SIG_LESS
:
2031 case SLJIT_SIG_GREATER_EQUAL
:
2034 case SLJIT_SIG_GREATER
:
2037 case SLJIT_SIG_LESS_EQUAL
:
2040 case SLJIT_OVERFLOW
:
2041 if (!(compiler
->status_flags_state
& (SLJIT_CURRENT_FLAGS_ADD
| SLJIT_CURRENT_FLAGS_SUB
)))
2045 case SLJIT_UNORDERED_F64
:
2048 case SLJIT_NOT_OVERFLOW
:
2049 if (!(compiler
->status_flags_state
& (SLJIT_CURRENT_FLAGS_ADD
| SLJIT_CURRENT_FLAGS_SUB
)))
2053 case SLJIT_ORDERED_F64
:
2056 default: /* SLJIT_JUMP */
2057 SLJIT_UNREACHABLE();
2062 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_label
* sljit_emit_label(struct sljit_compiler
*compiler
)
2064 struct sljit_label
*label
;
2067 CHECK_PTR(check_sljit_emit_label(compiler
));
2069 if (compiler
->last_label
&& compiler
->last_label
->size
== compiler
->size
)
2070 return compiler
->last_label
;
2072 label
= (struct sljit_label
*)ensure_abuf(compiler
, sizeof(struct sljit_label
));
2073 PTR_FAIL_IF(!label
);
2074 set_label(label
, compiler
);
2078 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_jump
* sljit_emit_jump(struct sljit_compiler
*compiler
, sljit_s32 type
)
2080 struct sljit_jump
*jump
;
2084 CHECK_PTR(check_sljit_emit_jump(compiler
, type
));
2086 jump
= (struct sljit_jump
*)ensure_abuf(compiler
, sizeof(struct sljit_jump
));
2088 set_jump(jump
, compiler
, type
& SLJIT_REWRITABLE_JUMP
);
2091 PTR_FAIL_IF(emit_imm32_const(compiler
, TMP_REG1
, 0));
2092 if (type
< SLJIT_JUMP
) {
2093 jump
->flags
|= IS_COND
;
2094 cc
= get_cc(compiler
, type
);
2095 jump
->flags
|= cc
<< 8;
2096 PTR_FAIL_IF(push_inst16(compiler
, IT
| (cc
<< 4) | 0x8));
2099 jump
->addr
= compiler
->size
;
2100 if (type
<= SLJIT_JUMP
)
2101 PTR_FAIL_IF(push_inst16(compiler
, BX
| RN3(TMP_REG1
)));
2103 jump
->flags
|= IS_BL
;
2104 PTR_FAIL_IF(push_inst16(compiler
, BLX
| RN3(TMP_REG1
)));
2112 static sljit_s32
softfloat_call_with_args(struct sljit_compiler
*compiler
, sljit_s32 arg_types
, sljit_s32
*src
, sljit_u32
*extra_space
)
2114 sljit_u32 is_tail_call
= *extra_space
& SLJIT_CALL_RETURN
;
2115 sljit_u32 offset
= 0;
2116 sljit_u32 word_arg_offset
= 0;
2117 sljit_u32 float_arg_count
= 0;
2118 sljit_s32 types
= 0;
2119 sljit_u32 src_offset
= 4 * sizeof(sljit_sw
);
2120 sljit_u8 offsets
[4];
2121 sljit_u8
*offset_ptr
= offsets
;
2123 if (src
&& FAST_IS_REG(*src
))
2124 src_offset
= (sljit_u32
)reg_map
[*src
] * sizeof(sljit_sw
);
2126 arg_types
>>= SLJIT_ARG_SHIFT
;
2129 types
= (types
<< SLJIT_ARG_SHIFT
) | (arg_types
& SLJIT_ARG_MASK
);
2131 switch (arg_types
& SLJIT_ARG_MASK
) {
2132 case SLJIT_ARG_TYPE_F64
:
2134 offset
+= sizeof(sljit_sw
);
2135 *offset_ptr
++ = (sljit_u8
)offset
;
2136 offset
+= sizeof(sljit_f64
);
2139 case SLJIT_ARG_TYPE_F32
:
2140 *offset_ptr
++ = (sljit_u8
)offset
;
2141 offset
+= sizeof(sljit_f32
);
2145 *offset_ptr
++ = (sljit_u8
)offset
;
2146 offset
+= sizeof(sljit_sw
);
2147 word_arg_offset
+= sizeof(sljit_sw
);
2151 arg_types
>>= SLJIT_ARG_SHIFT
;
2154 if (offset
> 4 * sizeof(sljit_sw
) && (!is_tail_call
|| offset
> compiler
->args_size
)) {
2155 /* Keep lr register on the stack. */
2157 offset
+= sizeof(sljit_sw
);
2159 offset
= ((offset
- 4 * sizeof(sljit_sw
)) + 0x7) & ~(sljit_uw
)0x7;
2161 *extra_space
= offset
;
2164 FAIL_IF(emit_stack_frame_release(compiler
, (sljit_s32
)offset
));
2166 FAIL_IF(push_inst16(compiler
, SUB_SP_I
| (offset
>> 2)));
2169 FAIL_IF(emit_stack_frame_release(compiler
, -1));
2173 SLJIT_ASSERT(reg_map
[TMP_REG1
] == 12);
2175 /* Process arguments in reversed direction. */
2177 switch (types
& SLJIT_ARG_MASK
) {
2178 case SLJIT_ARG_TYPE_F64
:
2180 offset
= *(--offset_ptr
);
2182 SLJIT_ASSERT((offset
& 0x7) == 0);
2184 if (offset
< 4 * sizeof(sljit_sw
)) {
2185 if (src_offset
== offset
|| src_offset
== offset
+ sizeof(sljit_sw
)) {
2186 FAIL_IF(push_inst16(compiler
, MOV
| (src_offset
<< 1) | 4 | (1 << 7)));
2189 FAIL_IF(push_inst32(compiler
, VMOV2
| 0x100000 | (offset
<< 10) | ((offset
+ sizeof(sljit_sw
)) << 14) | float_arg_count
));
2191 FAIL_IF(push_inst32(compiler
, VSTR_F32
| 0x800100 | RN4(SLJIT_SP
)
2192 | (float_arg_count
<< 12) | ((offset
- 4 * sizeof(sljit_sw
)) >> 2)));
2194 case SLJIT_ARG_TYPE_F32
:
2196 offset
= *(--offset_ptr
);
2198 if (offset
< 4 * sizeof(sljit_sw
)) {
2199 if (src_offset
== offset
) {
2200 FAIL_IF(push_inst16(compiler
, MOV
| (src_offset
<< 1) | 4 | (1 << 7)));
2203 FAIL_IF(push_inst32(compiler
, VMOV
| 0x100000 | (float_arg_count
<< 16) | (offset
<< 10)));
2205 FAIL_IF(push_inst32(compiler
, VSTR_F32
| 0x800000 | RN4(SLJIT_SP
)
2206 | (float_arg_count
<< 12) | ((offset
- 4 * sizeof(sljit_sw
)) >> 2)));
2209 word_arg_offset
-= sizeof(sljit_sw
);
2210 offset
= *(--offset_ptr
);
2212 SLJIT_ASSERT(offset
>= word_arg_offset
);
2214 if (offset
!= word_arg_offset
) {
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)));
2220 else if (src_offset
== word_arg_offset
) {
2221 *src
= (sljit_s32
)(1 + (offset
>> 2));
2222 src_offset
= offset
;
2224 FAIL_IF(push_inst16(compiler
, MOV
| (offset
>> 2) | (word_arg_offset
<< 1)));
2226 FAIL_IF(push_inst16(compiler
, STR_SP
| (word_arg_offset
<< 6) | ((offset
- 4 * sizeof(sljit_sw
)) >> 2)));
2231 types
>>= SLJIT_ARG_SHIFT
;
2234 return SLJIT_SUCCESS
;
2237 static sljit_s32
softfloat_post_call_with_args(struct sljit_compiler
*compiler
, sljit_s32 arg_types
)
2239 if ((arg_types
& SLJIT_ARG_MASK
) == SLJIT_ARG_TYPE_F64
)
2240 FAIL_IF(push_inst32(compiler
, VMOV2
| (1 << 16) | (0 << 12) | 0));
2241 if ((arg_types
& SLJIT_ARG_MASK
) == SLJIT_ARG_TYPE_F32
)
2242 FAIL_IF(push_inst32(compiler
, VMOV
| (0 << 16) | (0 << 12)));
2244 return SLJIT_SUCCESS
;
2249 static sljit_s32
hardfloat_call_with_args(struct sljit_compiler
*compiler
, sljit_s32 arg_types
)
2251 sljit_u32 offset
= SLJIT_FR0
;
2252 sljit_u32 new_offset
= SLJIT_FR0
;
2253 sljit_u32 f32_offset
= 0;
2255 /* Remove return value. */
2256 arg_types
>>= SLJIT_ARG_SHIFT
;
2259 switch (arg_types
& SLJIT_ARG_MASK
) {
2260 case SLJIT_ARG_TYPE_F64
:
2261 if (offset
!= new_offset
)
2262 FAIL_IF(push_inst32(compiler
, VMOV_F32
| SLJIT_32
| DD4(new_offset
) | DM4(offset
)));
2267 case SLJIT_ARG_TYPE_F32
:
2268 if (f32_offset
!= 0) {
2269 FAIL_IF(push_inst32(compiler
, VMOV_F32
| 0x400000 | DD4(f32_offset
) | DM4(offset
)));
2272 if (offset
!= new_offset
)
2273 FAIL_IF(push_inst32(compiler
, VMOV_F32
| 0x400000 | DD4(new_offset
) | DM4(offset
)));
2274 f32_offset
= new_offset
;
2280 arg_types
>>= SLJIT_ARG_SHIFT
;
2283 return SLJIT_SUCCESS
;
2288 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_jump
* sljit_emit_call(struct sljit_compiler
*compiler
, sljit_s32 type
,
2289 sljit_s32 arg_types
)
2292 struct sljit_jump
*jump
;
2293 sljit_u32 extra_space
= (sljit_u32
)type
;
2297 CHECK_PTR(check_sljit_emit_call(compiler
, type
, arg_types
));
2300 PTR_FAIL_IF(softfloat_call_with_args(compiler
, arg_types
, NULL
, &extra_space
));
2301 SLJIT_ASSERT((extra_space
& 0x7) == 0);
2303 if ((type
& SLJIT_CALL_RETURN
) && extra_space
== 0)
2304 type
= SLJIT_JUMP
| (type
& SLJIT_REWRITABLE_JUMP
);
2306 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2307 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2308 compiler
->skip_checks
= 1;
2311 jump
= sljit_emit_jump(compiler
, type
);
2312 PTR_FAIL_IF(jump
== NULL
);
2314 if (extra_space
> 0) {
2315 if (type
& SLJIT_CALL_RETURN
)
2316 PTR_FAIL_IF(push_inst32(compiler
, LDR
| RT4(TMP_REG2
)
2317 | RN4(SLJIT_SP
) | (extra_space
- sizeof(sljit_sw
))));
2319 PTR_FAIL_IF(push_inst16(compiler
, ADD_SP_I
| (extra_space
>> 2)));
2321 if (type
& SLJIT_CALL_RETURN
) {
2322 PTR_FAIL_IF(push_inst16(compiler
, BX
| RN3(TMP_REG2
)));
2327 SLJIT_ASSERT(!(type
& SLJIT_CALL_RETURN
));
2328 PTR_FAIL_IF(softfloat_post_call_with_args(compiler
, arg_types
));
2331 if (type
& SLJIT_CALL_RETURN
) {
2332 /* ldmia sp!, {..., lr} */
2333 PTR_FAIL_IF(emit_stack_frame_release(compiler
, -1));
2334 type
= SLJIT_JUMP
| (type
& SLJIT_REWRITABLE_JUMP
);
2337 PTR_FAIL_IF(hardfloat_call_with_args(compiler
, arg_types
));
2339 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2340 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2341 compiler
->skip_checks
= 1;
2344 return sljit_emit_jump(compiler
, type
);
2348 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_ijump(struct sljit_compiler
*compiler
, sljit_s32 type
, sljit_s32 src
, sljit_sw srcw
)
2350 struct sljit_jump
*jump
;
2353 CHECK(check_sljit_emit_ijump(compiler
, type
, src
, srcw
));
2354 ADJUST_LOCAL_OFFSET(src
, srcw
);
2356 SLJIT_ASSERT(reg_map
[TMP_REG1
] != 14);
2358 if (!(src
& SLJIT_IMM
)) {
2359 if (FAST_IS_REG(src
)) {
2360 SLJIT_ASSERT(reg_map
[src
] != 14);
2361 return push_inst16(compiler
, (type
<= SLJIT_JUMP
? BX
: BLX
) | RN3(src
));
2364 FAIL_IF(emit_op_mem(compiler
, WORD_SIZE
, type
<= SLJIT_JUMP
? TMP_PC
: TMP_REG1
, src
, srcw
, TMP_REG1
));
2365 if (type
>= SLJIT_FAST_CALL
)
2366 return push_inst16(compiler
, BLX
| RN3(TMP_REG1
));
2369 /* These jumps are converted to jump/call instructions when possible. */
2370 jump
= (struct sljit_jump
*)ensure_abuf(compiler
, sizeof(struct sljit_jump
));
2372 set_jump(jump
, compiler
, JUMP_ADDR
| ((type
>= SLJIT_FAST_CALL
) ? IS_BL
: 0));
2373 jump
->u
.target
= (sljit_uw
)srcw
;
2375 FAIL_IF(emit_imm32_const(compiler
, TMP_REG1
, 0));
2376 jump
->addr
= compiler
->size
;
2377 return push_inst16(compiler
, (type
<= SLJIT_JUMP
? BX
: BLX
) | RN3(TMP_REG1
));
2380 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_icall(struct sljit_compiler
*compiler
, sljit_s32 type
,
2381 sljit_s32 arg_types
,
2382 sljit_s32 src
, sljit_sw srcw
)
2385 sljit_u32 extra_space
= (sljit_u32
)type
;
2389 CHECK(check_sljit_emit_icall(compiler
, type
, arg_types
, src
, srcw
));
2391 if (src
& SLJIT_MEM
) {
2392 FAIL_IF(emit_op_mem(compiler
, WORD_SIZE
, TMP_REG1
, src
, srcw
, TMP_REG1
));
2396 if ((type
& SLJIT_CALL_RETURN
) && (src
>= SLJIT_FIRST_SAVED_REG
&& src
<= SLJIT_S0
)) {
2397 FAIL_IF(push_inst16(compiler
, MOV
| SET_REGS44(TMP_REG1
, src
)));
2402 FAIL_IF(softfloat_call_with_args(compiler
, arg_types
, &src
, &extra_space
));
2403 SLJIT_ASSERT((extra_space
& 0x7) == 0);
2405 if ((type
& SLJIT_CALL_RETURN
) && extra_space
== 0)
2408 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2409 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2410 compiler
->skip_checks
= 1;
2413 FAIL_IF(sljit_emit_ijump(compiler
, type
, src
, srcw
));
2415 if (extra_space
> 0) {
2416 if (type
& SLJIT_CALL_RETURN
)
2417 FAIL_IF(push_inst32(compiler
, LDR
| RT4(TMP_REG2
)
2418 | RN4(SLJIT_SP
) | (extra_space
- sizeof(sljit_sw
))));
2420 FAIL_IF(push_inst16(compiler
, ADD_SP_I
| (extra_space
>> 2)));
2422 if (type
& SLJIT_CALL_RETURN
)
2423 return push_inst16(compiler
, BX
| RN3(TMP_REG2
));
2426 SLJIT_ASSERT(!(type
& SLJIT_CALL_RETURN
));
2427 return softfloat_post_call_with_args(compiler
, arg_types
);
2428 #else /* !__SOFTFP__ */
2429 if (type
& SLJIT_CALL_RETURN
) {
2430 /* ldmia sp!, {..., lr} */
2431 FAIL_IF(emit_stack_frame_release(compiler
, -1));
2435 FAIL_IF(hardfloat_call_with_args(compiler
, arg_types
));
2437 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2438 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2439 compiler
->skip_checks
= 1;
2442 return sljit_emit_ijump(compiler
, type
, src
, srcw
);
2443 #endif /* __SOFTFP__ */
2446 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op_flags(struct sljit_compiler
*compiler
, sljit_s32 op
,
2447 sljit_s32 dst
, sljit_sw dstw
,
2450 sljit_s32 dst_r
, flags
= GET_ALL_FLAGS(op
);
2454 CHECK(check_sljit_emit_op_flags(compiler
, op
, dst
, dstw
, type
));
2455 ADJUST_LOCAL_OFFSET(dst
, dstw
);
2457 op
= GET_OPCODE(op
);
2458 cc
= get_cc(compiler
, type
& 0xff);
2459 dst_r
= FAST_IS_REG(dst
) ? dst
: TMP_REG1
;
2461 if (op
< SLJIT_ADD
) {
2462 FAIL_IF(push_inst16(compiler
, IT
| (cc
<< 4) | (((cc
& 0x1) ^ 0x1) << 3) | 0x4));
2463 if (reg_map
[dst_r
] > 7) {
2464 FAIL_IF(push_inst32(compiler
, MOV_WI
| RD4(dst_r
) | 1));
2465 FAIL_IF(push_inst32(compiler
, MOV_WI
| RD4(dst_r
) | 0));
2467 /* The movsi (immediate) instruction does not set flags in IT block. */
2468 FAIL_IF(push_inst16(compiler
, MOVSI
| RDN3(dst_r
) | 1));
2469 FAIL_IF(push_inst16(compiler
, MOVSI
| RDN3(dst_r
) | 0));
2471 if (!(dst
& SLJIT_MEM
))
2472 return SLJIT_SUCCESS
;
2473 return emit_op_mem(compiler
, WORD_SIZE
| STORE
, TMP_REG1
, dst
, dstw
, TMP_REG2
);
2476 if (dst
& SLJIT_MEM
)
2477 FAIL_IF(emit_op_mem(compiler
, WORD_SIZE
, TMP_REG1
, dst
, dstw
, TMP_REG2
));
2479 if (op
== SLJIT_AND
) {
2480 FAIL_IF(push_inst16(compiler
, IT
| (cc
<< 4) | (((cc
& 0x1) ^ 0x1) << 3) | 0x4));
2481 FAIL_IF(push_inst32(compiler
, ANDI
| RN4(dst_r
) | RD4(dst_r
) | 1));
2482 FAIL_IF(push_inst32(compiler
, ANDI
| RN4(dst_r
) | RD4(dst_r
) | 0));
2485 FAIL_IF(push_inst16(compiler
, IT
| (cc
<< 4) | 0x8));
2486 FAIL_IF(push_inst32(compiler
, ((op
== SLJIT_OR
) ? ORRI
: EORI
) | RN4(dst_r
) | RD4(dst_r
) | 1));
2489 if (dst
& SLJIT_MEM
)
2490 FAIL_IF(emit_op_mem(compiler
, WORD_SIZE
| STORE
, TMP_REG1
, dst
, dstw
, TMP_REG2
));
2492 if (!(flags
& SLJIT_SET_Z
))
2493 return SLJIT_SUCCESS
;
2495 /* The condition must always be set, even if the ORR/EORI is not executed above. */
2496 return push_inst32(compiler
, MOV_W
| SET_FLAGS
| RD4(TMP_REG1
) | RM4(dst_r
));
2499 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_cmov(struct sljit_compiler
*compiler
, sljit_s32 type
,
2501 sljit_s32 src
, sljit_sw srcw
)
2506 CHECK(check_sljit_emit_cmov(compiler
, type
, dst_reg
, src
, srcw
));
2508 dst_reg
&= ~SLJIT_32
;
2510 cc
= get_cc(compiler
, type
& 0xff);
2512 if (!(src
& SLJIT_IMM
)) {
2513 FAIL_IF(push_inst16(compiler
, IT
| (cc
<< 4) | 0x8));
2514 return push_inst16(compiler
, MOV
| SET_REGS44(dst_reg
, src
));
2517 tmp
= (sljit_uw
) srcw
;
2519 if (tmp
< 0x10000) {
2520 /* set low 16 bits, set hi 16 bits to 0. */
2521 FAIL_IF(push_inst16(compiler
, IT
| (cc
<< 4) | 0x8));
2522 return push_inst32(compiler
, MOVW
| RD4(dst_reg
)
2523 | COPY_BITS(tmp
, 12, 16, 4) | COPY_BITS(tmp
, 11, 26, 1) | COPY_BITS(tmp
, 8, 12, 3) | (tmp
& 0xff));
2526 tmp
= get_imm((sljit_uw
)srcw
);
2527 if (tmp
!= INVALID_IMM
) {
2528 FAIL_IF(push_inst16(compiler
, IT
| (cc
<< 4) | 0x8));
2529 return push_inst32(compiler
, MOV_WI
| RD4(dst_reg
) | tmp
);
2532 tmp
= get_imm(~(sljit_uw
)srcw
);
2533 if (tmp
!= INVALID_IMM
) {
2534 FAIL_IF(push_inst16(compiler
, IT
| (cc
<< 4) | 0x8));
2535 return push_inst32(compiler
, MVN_WI
| RD4(dst_reg
) | tmp
);
2538 FAIL_IF(push_inst16(compiler
, IT
| (cc
<< 4) | ((cc
& 0x1) << 3) | 0x4));
2540 tmp
= (sljit_uw
) srcw
;
2541 FAIL_IF(push_inst32(compiler
, MOVW
| RD4(dst_reg
)
2542 | COPY_BITS(tmp
, 12, 16, 4) | COPY_BITS(tmp
, 11, 26, 1) | COPY_BITS(tmp
, 8, 12, 3) | (tmp
& 0xff)));
2543 return push_inst32(compiler
, MOVT
| RD4(dst_reg
)
2544 | COPY_BITS(tmp
, 12 + 16, 16, 4) | COPY_BITS(tmp
, 11 + 16, 26, 1) | COPY_BITS(tmp
, 8 + 16, 12, 3) | ((tmp
& 0xff0000) >> 16));
2547 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_mem(struct sljit_compiler
*compiler
, sljit_s32 type
,
2549 sljit_s32 mem
, sljit_sw memw
)
2555 CHECK(check_sljit_emit_mem(compiler
, type
, reg
, mem
, memw
));
2557 if ((mem
& OFFS_REG_MASK
) || (memw
> 255 || memw
< -255))
2558 return SLJIT_ERR_UNSUPPORTED
;
2560 if (type
& SLJIT_MEM_SUPP
)
2561 return SLJIT_SUCCESS
;
2563 switch (type
& 0xff) {
2575 flags
= BYTE_SIZE
| SIGNED
;
2581 flags
= HALF_SIZE
| SIGNED
;
2584 SLJIT_UNREACHABLE();
2589 if (type
& SLJIT_MEM_STORE
)
2592 inst
= sljit_mem32
[flags
] | 0x900;
2594 if (type
& SLJIT_MEM_PRE
)
2602 return push_inst32(compiler
, inst
| RT4(reg
) | RN4(mem
& REG_MASK
) | (sljit_ins
)memw
);
2605 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_const
* sljit_emit_const(struct sljit_compiler
*compiler
, sljit_s32 dst
, sljit_sw dstw
, sljit_sw init_value
)
2607 struct sljit_const
*const_
;
2611 CHECK_PTR(check_sljit_emit_const(compiler
, dst
, dstw
, init_value
));
2612 ADJUST_LOCAL_OFFSET(dst
, dstw
);
2614 const_
= (struct sljit_const
*)ensure_abuf(compiler
, sizeof(struct sljit_const
));
2615 PTR_FAIL_IF(!const_
);
2616 set_const(const_
, compiler
);
2618 dst_r
= FAST_IS_REG(dst
) ? dst
: TMP_REG1
;
2619 PTR_FAIL_IF(emit_imm32_const(compiler
, dst_r
, (sljit_uw
)init_value
));
2621 if (dst
& SLJIT_MEM
)
2622 PTR_FAIL_IF(emit_op_mem(compiler
, WORD_SIZE
| STORE
, dst_r
, dst
, dstw
, TMP_REG2
));
2626 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_put_label
* sljit_emit_put_label(struct sljit_compiler
*compiler
, sljit_s32 dst
, sljit_sw dstw
)
2628 struct sljit_put_label
*put_label
;
2632 CHECK_PTR(check_sljit_emit_put_label(compiler
, dst
, dstw
));
2633 ADJUST_LOCAL_OFFSET(dst
, dstw
);
2635 put_label
= (struct sljit_put_label
*)ensure_abuf(compiler
, sizeof(struct sljit_put_label
));
2636 PTR_FAIL_IF(!put_label
);
2637 set_put_label(put_label
, compiler
, 0);
2639 dst_r
= FAST_IS_REG(dst
) ? dst
: TMP_REG1
;
2640 PTR_FAIL_IF(emit_imm32_const(compiler
, dst_r
, 0));
2642 if (dst
& SLJIT_MEM
)
2643 PTR_FAIL_IF(emit_op_mem(compiler
, WORD_SIZE
| STORE
, dst_r
, dst
, dstw
, TMP_REG2
));
2647 SLJIT_API_FUNC_ATTRIBUTE
void sljit_set_jump_addr(sljit_uw addr
, sljit_uw new_target
, sljit_sw executable_offset
)
2649 sljit_u16
*inst
= (sljit_u16
*)addr
;
2650 SLJIT_UNUSED_ARG(executable_offset
);
2652 SLJIT_UPDATE_WX_FLAGS(inst
, inst
+ 4, 0);
2653 modify_imm32_const(inst
, new_target
);
2654 SLJIT_UPDATE_WX_FLAGS(inst
, inst
+ 4, 1);
2655 inst
= (sljit_u16
*)SLJIT_ADD_EXEC_OFFSET(inst
, executable_offset
);
2656 SLJIT_CACHE_FLUSH(inst
, inst
+ 4);
2659 SLJIT_API_FUNC_ATTRIBUTE
void sljit_set_const(sljit_uw addr
, sljit_sw new_constant
, sljit_sw executable_offset
)
2661 sljit_set_jump_addr(addr
, (sljit_uw
)new_constant
, executable_offset
);