2 * Stack-less Just-In-Time compiler
4 * Copyright Zoltan Herczeg (hzmester@freemail.hu). All rights reserved.
6 * Redistribution and use in source and binary forms, with or without modification, are
7 * permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright notice, this list of
10 * conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
13 * of conditions and the following disclaimer in the documentation and/or other materials
14 * provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) AND CONTRIBUTORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19 * SHALL THE COPYRIGHT HOLDER(S) OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
22 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 SLJIT_API_FUNC_ATTRIBUTE
const char* sljit_get_platform_name(void)
29 return "ARM-64" SLJIT_CPUINFO
;
32 /* Length of an instruction word */
33 typedef sljit_u32 sljit_ins
;
37 #define TMP_REG1 (SLJIT_NUMBER_OF_REGISTERS + 2)
38 #define TMP_REG2 (SLJIT_NUMBER_OF_REGISTERS + 3)
39 #define TMP_REG3 (SLJIT_NUMBER_OF_REGISTERS + 4)
40 #define TMP_LR (SLJIT_NUMBER_OF_REGISTERS + 5)
41 #define TMP_SP (SLJIT_NUMBER_OF_REGISTERS + 6)
43 #define TMP_FREG1 (SLJIT_NUMBER_OF_FLOAT_REGISTERS + 1)
44 #define TMP_FREG2 (SLJIT_NUMBER_OF_FLOAT_REGISTERS + 2)
46 static const sljit_u8 reg_map
[SLJIT_NUMBER_OF_REGISTERS
+ 8] = {
47 31, 0, 1, 2, 3, 4, 5, 6, 7, 12, 13, 14, 15, 16, 17, 8, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 29, 9, 10, 11, 30, 31
50 static const sljit_u8 freg_map
[SLJIT_NUMBER_OF_FLOAT_REGISTERS
+ 3] = {
51 0, 0, 1, 2, 3, 4, 5, 6, 7
54 #define W_OP (1 << 31)
55 #define RD(rd) (reg_map[rd])
56 #define RT(rt) (reg_map[rt])
57 #define RN(rn) (reg_map[rn] << 5)
58 #define RT2(rt2) (reg_map[rt2] << 10)
59 #define RM(rm) (reg_map[rm] << 16)
60 #define VD(vd) (freg_map[vd])
61 #define VT(vt) (freg_map[vt])
62 #define VN(vn) (freg_map[vn] << 5)
63 #define VM(vm) (freg_map[vm] << 16)
65 /* --------------------------------------------------------------------- */
66 /* Instrucion forms */
67 /* --------------------------------------------------------------------- */
69 #define ADC 0x9a000000
70 #define ADD 0x8b000000
71 #define ADDI 0x91000000
72 #define AND 0x8a000000
73 #define ANDI 0x92000000
74 #define ASRV 0x9ac02800
76 #define B_CC 0x54000000
78 #define BLR 0xd63f0000
80 #define BRK 0xd4200000
81 #define CBZ 0xb4000000
82 #define CLZ 0xdac01000
83 #define CSEL 0x9a800000
84 #define CSINC 0x9a800400
85 #define EOR 0xca000000
86 #define EORI 0xd2000000
87 #define FABS 0x1e60c000
88 #define FADD 0x1e602800
89 #define FCMP 0x1e602000
90 #define FCVT 0x1e224000
91 #define FCVTZS 0x9e780000
92 #define FDIV 0x1e601800
93 #define FMOV 0x1e604000
94 #define FMUL 0x1e600800
95 #define FNEG 0x1e614000
96 #define FSUB 0x1e603800
97 #define LDRI 0xf9400000
98 #define LDP 0xa9400000
99 #define LDP_PST 0xa8c00000
100 #define LSLV 0x9ac02000
101 #define LSRV 0x9ac02400
102 #define MADD 0x9b000000
103 #define MOVK 0xf2800000
104 #define MOVN 0x92800000
105 #define MOVZ 0xd2800000
106 #define NOP 0xd503201f
107 #define ORN 0xaa200000
108 #define ORR 0xaa000000
109 #define ORRI 0xb2000000
110 #define RET 0xd65f0000
111 #define SBC 0xda000000
112 #define SBFM 0x93000000
113 #define SCVTF 0x9e620000
114 #define SDIV 0x9ac00c00
115 #define SMADDL 0x9b200000
116 #define SMULH 0x9b403c00
117 #define STP 0xa9000000
118 #define STP_PRE 0xa9800000
119 #define STRI 0xf9000000
120 #define STR_FI 0x3d000000
121 #define STR_FR 0x3c206800
122 #define STUR_FI 0x3c000000
123 #define SUB 0xcb000000
124 #define SUBI 0xd1000000
125 #define SUBS 0xeb000000
126 #define UBFM 0xd3000000
127 #define UDIV 0x9ac00800
128 #define UMULH 0x9bc03c00
130 /* dest_reg is the absolute name of the register
131 Useful for reordering instructions in the delay slot. */
132 static sljit_s32
push_inst(struct sljit_compiler
*compiler
, sljit_ins ins
)
134 sljit_ins
*ptr
= (sljit_ins
*)ensure_buf(compiler
, sizeof(sljit_ins
));
138 return SLJIT_SUCCESS
;
141 static SLJIT_INLINE sljit_s32
emit_imm64_const(struct sljit_compiler
*compiler
, sljit_s32 dst
, sljit_uw imm
)
143 FAIL_IF(push_inst(compiler
, MOVZ
| RD(dst
) | ((imm
& 0xffff) << 5)));
144 FAIL_IF(push_inst(compiler
, MOVK
| RD(dst
) | (((imm
>> 16) & 0xffff) << 5) | (1 << 21)));
145 FAIL_IF(push_inst(compiler
, MOVK
| RD(dst
) | (((imm
>> 32) & 0xffff) << 5) | (2 << 21)));
146 return push_inst(compiler
, MOVK
| RD(dst
) | ((imm
>> 48) << 5) | (3 << 21));
149 static SLJIT_INLINE
void modify_imm64_const(sljit_ins
* inst
, sljit_uw new_imm
)
151 sljit_s32 dst
= inst
[0] & 0x1f;
152 SLJIT_ASSERT((inst
[0] & 0xffe00000) == MOVZ
&& (inst
[1] & 0xffe00000) == (MOVK
| (1 << 21)));
153 inst
[0] = MOVZ
| dst
| ((new_imm
& 0xffff) << 5);
154 inst
[1] = MOVK
| dst
| (((new_imm
>> 16) & 0xffff) << 5) | (1 << 21);
155 inst
[2] = MOVK
| dst
| (((new_imm
>> 32) & 0xffff) << 5) | (2 << 21);
156 inst
[3] = MOVK
| dst
| ((new_imm
>> 48) << 5) | (3 << 21);
159 static SLJIT_INLINE sljit_s32
detect_jump_type(struct sljit_jump
*jump
, sljit_ins
*code_ptr
, sljit_ins
*code
, sljit_sw executable_offset
)
162 sljit_uw target_addr
;
164 if (jump
->flags
& SLJIT_REWRITABLE_JUMP
) {
165 jump
->flags
|= PATCH_ABS64
;
169 if (jump
->flags
& JUMP_ADDR
)
170 target_addr
= jump
->u
.target
;
172 SLJIT_ASSERT(jump
->flags
& JUMP_LABEL
);
173 target_addr
= (sljit_uw
)(code
+ jump
->u
.label
->size
) + (sljit_uw
)executable_offset
;
176 diff
= (sljit_sw
)target_addr
- (sljit_sw
)(code_ptr
+ 4) - executable_offset
;
178 if (jump
->flags
& IS_COND
) {
179 diff
+= sizeof(sljit_ins
);
180 if (diff
<= 0xfffff && diff
>= -0x100000) {
181 code_ptr
[-5] ^= (jump
->flags
& IS_CBZ
) ? (0x1 << 24) : 0x1;
182 jump
->addr
-= sizeof(sljit_ins
);
183 jump
->flags
|= PATCH_COND
;
186 diff
-= sizeof(sljit_ins
);
189 if (diff
<= 0x7ffffff && diff
>= -0x8000000) {
190 jump
->flags
|= PATCH_B
;
194 if (target_addr
<= 0xffffffffl
) {
195 if (jump
->flags
& IS_COND
)
196 code_ptr
[-5] -= (2 << 5);
197 code_ptr
[-2] = code_ptr
[0];
200 if (target_addr
<= 0xffffffffffffl
) {
201 if (jump
->flags
& IS_COND
)
202 code_ptr
[-5] -= (1 << 5);
203 jump
->flags
|= PATCH_ABS48
;
204 code_ptr
[-1] = code_ptr
[0];
208 jump
->flags
|= PATCH_ABS64
;
212 SLJIT_API_FUNC_ATTRIBUTE
void* sljit_generate_code(struct sljit_compiler
*compiler
)
214 struct sljit_memory_fragment
*buf
;
220 sljit_sw executable_offset
;
224 struct sljit_label
*label
;
225 struct sljit_jump
*jump
;
226 struct sljit_const
*const_
;
229 CHECK_PTR(check_sljit_generate_code(compiler
));
230 reverse_buf(compiler
);
232 code
= (sljit_ins
*)SLJIT_MALLOC_EXEC(compiler
->size
* sizeof(sljit_ins
));
233 PTR_FAIL_WITH_EXEC_IF(code
);
238 executable_offset
= SLJIT_EXEC_OFFSET(code
);
240 label
= compiler
->labels
;
241 jump
= compiler
->jumps
;
242 const_
= compiler
->consts
;
245 buf_ptr
= (sljit_ins
*)buf
->memory
;
246 buf_end
= buf_ptr
+ (buf
->used_size
>> 2);
248 *code_ptr
= *buf_ptr
++;
249 /* These structures are ordered by their address. */
250 SLJIT_ASSERT(!label
|| label
->size
>= word_count
);
251 SLJIT_ASSERT(!jump
|| jump
->addr
>= word_count
);
252 SLJIT_ASSERT(!const_
|| const_
->addr
>= word_count
);
253 if (label
&& label
->size
== word_count
) {
254 label
->addr
= (sljit_uw
)SLJIT_ADD_EXEC_OFFSET(code_ptr
, executable_offset
);
255 label
->size
= code_ptr
- code
;
258 if (jump
&& jump
->addr
== word_count
) {
259 jump
->addr
= (sljit_uw
)(code_ptr
- 4);
260 code_ptr
-= detect_jump_type(jump
, code_ptr
, code
, executable_offset
);
263 if (const_
&& const_
->addr
== word_count
) {
264 const_
->addr
= (sljit_uw
)code_ptr
;
265 const_
= const_
->next
;
269 } while (buf_ptr
< buf_end
);
274 if (label
&& label
->size
== word_count
) {
275 label
->addr
= (sljit_uw
)SLJIT_ADD_EXEC_OFFSET(code_ptr
, executable_offset
);
276 label
->size
= code_ptr
- code
;
280 SLJIT_ASSERT(!label
);
282 SLJIT_ASSERT(!const_
);
283 SLJIT_ASSERT(code_ptr
- code
<= (sljit_sw
)compiler
->size
);
285 jump
= compiler
->jumps
;
288 addr
= (jump
->flags
& JUMP_LABEL
) ? jump
->u
.label
->addr
: jump
->u
.target
;
289 buf_ptr
= (sljit_ins
*)jump
->addr
;
291 if (jump
->flags
& PATCH_B
) {
292 addr
= (sljit_sw
)(addr
- (sljit_uw
)SLJIT_ADD_EXEC_OFFSET(buf_ptr
, executable_offset
)) >> 2;
293 SLJIT_ASSERT((sljit_sw
)addr
<= 0x1ffffff && (sljit_sw
)addr
>= -0x2000000);
294 buf_ptr
[0] = ((jump
->flags
& IS_BL
) ? BL
: B
) | (addr
& 0x3ffffff);
295 if (jump
->flags
& IS_COND
)
296 buf_ptr
[-1] -= (4 << 5);
299 if (jump
->flags
& PATCH_COND
) {
300 addr
= (sljit_sw
)(addr
- (sljit_uw
)SLJIT_ADD_EXEC_OFFSET(buf_ptr
, executable_offset
)) >> 2;
301 SLJIT_ASSERT((sljit_sw
)addr
<= 0x3ffff && (sljit_sw
)addr
>= -0x40000);
302 buf_ptr
[0] = (buf_ptr
[0] & ~0xffffe0) | ((addr
& 0x7ffff) << 5);
306 SLJIT_ASSERT((jump
->flags
& (PATCH_ABS48
| PATCH_ABS64
)) || addr
<= 0xffffffffl
);
307 SLJIT_ASSERT((jump
->flags
& PATCH_ABS64
) || addr
<= 0xffffffffffffl
);
309 dst
= buf_ptr
[0] & 0x1f;
310 buf_ptr
[0] = MOVZ
| dst
| ((addr
& 0xffff) << 5);
311 buf_ptr
[1] = MOVK
| dst
| (((addr
>> 16) & 0xffff) << 5) | (1 << 21);
312 if (jump
->flags
& (PATCH_ABS48
| PATCH_ABS64
))
313 buf_ptr
[2] = MOVK
| dst
| (((addr
>> 32) & 0xffff) << 5) | (2 << 21);
314 if (jump
->flags
& PATCH_ABS64
)
315 buf_ptr
[3] = MOVK
| dst
| (((addr
>> 48) & 0xffff) << 5) | (3 << 21);
320 compiler
->error
= SLJIT_ERR_COMPILED
;
321 compiler
->executable_offset
= executable_offset
;
322 compiler
->executable_size
= (code_ptr
- code
) * sizeof(sljit_ins
);
324 code
= (sljit_ins
*)SLJIT_ADD_EXEC_OFFSET(code
, executable_offset
);
325 code_ptr
= (sljit_ins
*)SLJIT_ADD_EXEC_OFFSET(code_ptr
, executable_offset
);
327 SLJIT_CACHE_FLUSH(code
, code_ptr
);
331 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_has_cpu_feature(sljit_s32 feature_type
)
333 switch (feature_type
) {
335 #ifdef SLJIT_IS_FPU_AVAILABLE
336 return SLJIT_IS_FPU_AVAILABLE
;
338 /* Available by default. */
342 case SLJIT_HAS_PRE_UPDATE
:
352 /* --------------------------------------------------------------------- */
353 /* Core code generator functions. */
354 /* --------------------------------------------------------------------- */
356 #define COUNT_TRAILING_ZERO(value, result) \
358 if (!(value & 0xffffffff)) { \
362 if (!(value & 0xffff)) { \
366 if (!(value & 0xff)) { \
370 if (!(value & 0xf)) { \
374 if (!(value & 0x3)) { \
378 if (!(value & 0x1)) { \
383 #define LOGICAL_IMM_CHECK 0x100
385 static sljit_ins
logical_imm(sljit_sw imm
, sljit_s32 len
)
387 sljit_s32 negated
, ones
, right
;
391 if (len
& LOGICAL_IMM_CHECK
) {
392 len
&= ~LOGICAL_IMM_CHECK
;
393 if (len
== 32 && (imm
== 0 || imm
== -1))
395 if (len
== 16 && ((sljit_s32
)imm
== 0 || (sljit_s32
)imm
== -1))
399 SLJIT_ASSERT((len
== 32 && imm
!= 0 && imm
!= -1)
400 || (len
== 16 && (sljit_s32
)imm
!= 0 && (sljit_s32
)imm
!= -1));
401 uimm
= (sljit_uw
)imm
;
407 mask
= ((sljit_uw
)1 << len
) - 1;
408 if ((uimm
& mask
) != ((uimm
>> len
) & mask
))
422 uimm
&= ((sljit_uw
)1 << len
) - 1;
424 /* Unsigned right shift. */
425 COUNT_TRAILING_ZERO(uimm
, right
);
427 /* Signed shift. We also know that the highest bit is set. */
428 imm
= (sljit_sw
)~uimm
;
429 SLJIT_ASSERT(imm
< 0);
431 COUNT_TRAILING_ZERO(imm
, ones
);
439 ins
= (0x3f - ((len
<< 1) - 1)) << 10;
442 return ins
| ((len
- ones
- 1) << 10) | ((len
- ones
- right
) << 16);
444 return ins
| ((ones
- 1) << 10) | ((len
- right
) << 16);
447 #undef COUNT_TRAILING_ZERO
449 static sljit_s32
load_immediate(struct sljit_compiler
*compiler
, sljit_s32 dst
, sljit_sw simm
)
451 sljit_uw imm
= (sljit_uw
)simm
;
452 sljit_s32 i
, zeros
, ones
, first
;
456 return push_inst(compiler
, MOVZ
| RD(dst
) | (imm
<< 5));
458 if (simm
>= -0x10000 && simm
< 0)
459 return push_inst(compiler
, MOVN
| RD(dst
) | ((~imm
& 0xffff) << 5));
461 if (imm
<= 0xffffffffl
) {
462 if ((imm
& 0xffff0000l
) == 0xffff0000)
463 return push_inst(compiler
, (MOVN
^ W_OP
) | RD(dst
) | ((~imm
& 0xffff) << 5));
464 if ((imm
& 0xffff) == 0xffff)
465 return push_inst(compiler
, (MOVN
^ W_OP
) | RD(dst
) | ((~imm
& 0xffff0000l
) >> (16 - 5)) | (1 << 21));
466 bitmask
= logical_imm(simm
, 16);
468 return push_inst(compiler
, (ORRI
^ W_OP
) | RD(dst
) | RN(TMP_ZERO
) | bitmask
);
471 bitmask
= logical_imm(simm
, 32);
473 return push_inst(compiler
, ORRI
| RD(dst
) | RN(TMP_ZERO
) | bitmask
);
476 if (imm
<= 0xffffffffl
) {
477 FAIL_IF(push_inst(compiler
, MOVZ
| RD(dst
) | ((imm
& 0xffff) << 5)));
478 return push_inst(compiler
, MOVK
| RD(dst
) | ((imm
& 0xffff0000l
) >> (16 - 5)) | (1 << 21));
481 if (simm
>= -0x100000000l
&& simm
< 0) {
482 FAIL_IF(push_inst(compiler
, MOVN
| RD(dst
) | ((~imm
& 0xffff) << 5)));
483 return push_inst(compiler
, MOVK
| RD(dst
) | ((imm
& 0xffff0000l
) >> (16 - 5)) | (1 << 21));
486 /* A large amount of number can be constructed from ORR and MOVx,
487 but computing them is costly. We don't */
491 for (i
= 4; i
> 0; i
--) {
492 if ((simm
& 0xffff) == 0)
494 if ((simm
& 0xffff) == 0xffff)
499 simm
= (sljit_sw
)imm
;
503 for (i
= 0; i
< 4; i
++) {
504 if (!(simm
& 0xffff)) {
510 FAIL_IF(push_inst(compiler
, MOVN
| RD(dst
) | ((simm
& 0xffff) << 5) | (i
<< 21)));
513 FAIL_IF(push_inst(compiler
, MOVK
| RD(dst
) | ((~simm
& 0xffff) << 5) | (i
<< 21)));
516 return SLJIT_SUCCESS
;
519 for (i
= 0; i
< 4; i
++) {
520 if (!(simm
& 0xffff)) {
526 FAIL_IF(push_inst(compiler
, MOVZ
| RD(dst
) | ((simm
& 0xffff) << 5) | (i
<< 21)));
529 FAIL_IF(push_inst(compiler
, MOVK
| RD(dst
) | ((simm
& 0xffff) << 5) | (i
<< 21)));
532 return SLJIT_SUCCESS
;
535 #define ARG1_IMM 0x0010000
536 #define ARG2_IMM 0x0020000
537 #define INT_OP 0x0040000
538 #define SET_FLAGS 0x0080000
539 #define UNUSED_RETURN 0x0100000
540 #define SLOW_DEST 0x0200000
541 #define SLOW_SRC1 0x0400000
542 #define SLOW_SRC2 0x0800000
544 #define CHECK_FLAGS(flag_bits) \
545 if (flags & SET_FLAGS) { \
546 inv_bits |= flag_bits; \
547 if (flags & UNUSED_RETURN) \
551 static sljit_s32
emit_op_imm(struct sljit_compiler
*compiler
, sljit_s32 flags
, sljit_s32 dst
, sljit_sw arg1
, sljit_sw arg2
)
553 /* dst must be register, TMP_REG1
554 arg1 must be register, TMP_REG1, imm
555 arg2 must be register, TMP_REG2, imm */
556 sljit_ins inv_bits
= (flags
& INT_OP
) ? (1 << 31) : 0;
558 sljit_s32 op
= (flags
& 0xffff);
562 if (SLJIT_UNLIKELY((flags
& (ARG1_IMM
| ARG2_IMM
)) == (ARG1_IMM
| ARG2_IMM
))) {
563 /* Both are immediates. */
565 if (arg1
== 0 && op
!= SLJIT_ADD
&& op
!= SLJIT_SUB
)
568 FAIL_IF(load_immediate(compiler
, TMP_REG1
, arg1
));
573 if (flags
& (ARG1_IMM
| ARG2_IMM
)) {
574 reg
= (flags
& ARG2_IMM
) ? arg1
: arg2
;
575 imm
= (flags
& ARG2_IMM
) ? arg2
: arg1
;
583 /* No form with immediate operand (except imm 0, which
584 is represented by a ZERO register). */
587 SLJIT_ASSERT(!(flags
& SET_FLAGS
) && (flags
& ARG2_IMM
) && arg1
== TMP_REG1
);
588 return load_immediate(compiler
, dst
, imm
);
590 SLJIT_ASSERT(flags
& ARG2_IMM
);
591 FAIL_IF(load_immediate(compiler
, dst
, (flags
& INT_OP
) ? (~imm
& 0xffffffff) : ~imm
));
594 if (flags
& ARG1_IMM
)
600 CHECK_FLAGS(1 << 29);
601 return push_inst(compiler
, ((op
== SLJIT_ADD
? ADDI
: SUBI
) ^ inv_bits
) | RD(dst
) | RN(reg
));
603 if (imm
> 0 && imm
<= 0xfff) {
604 CHECK_FLAGS(1 << 29);
605 return push_inst(compiler
, (ADDI
^ inv_bits
) | RD(dst
) | RN(reg
) | (imm
<< 10));
608 if (nimm
> 0 && nimm
<= 0xfff) {
609 CHECK_FLAGS(1 << 29);
610 return push_inst(compiler
, (SUBI
^ inv_bits
) | RD(dst
) | RN(reg
) | (nimm
<< 10));
612 if (imm
> 0 && imm
<= 0xffffff && !(imm
& 0xfff)) {
613 CHECK_FLAGS(1 << 29);
614 return push_inst(compiler
, (ADDI
^ inv_bits
) | RD(dst
) | RN(reg
) | ((imm
>> 12) << 10) | (1 << 22));
616 if (nimm
> 0 && nimm
<= 0xffffff && !(nimm
& 0xfff)) {
617 CHECK_FLAGS(1 << 29);
618 return push_inst(compiler
, (SUBI
^ inv_bits
) | RD(dst
) | RN(reg
) | ((nimm
>> 12) << 10) | (1 << 22));
620 if (imm
> 0 && imm
<= 0xffffff && !(flags
& SET_FLAGS
)) {
621 FAIL_IF(push_inst(compiler
, (ADDI
^ inv_bits
) | RD(dst
) | RN(reg
) | ((imm
>> 12) << 10) | (1 << 22)));
622 return push_inst(compiler
, (ADDI
^ inv_bits
) | RD(dst
) | RN(dst
) | ((imm
& 0xfff) << 10));
624 if (nimm
> 0 && nimm
<= 0xffffff && !(flags
& SET_FLAGS
)) {
625 FAIL_IF(push_inst(compiler
, (SUBI
^ inv_bits
) | RD(dst
) | RN(reg
) | ((nimm
>> 12) << 10) | (1 << 22)));
626 return push_inst(compiler
, (SUBI
^ inv_bits
) | RD(dst
) | RN(dst
) | ((nimm
& 0xfff) << 10));
630 inst_bits
= logical_imm(imm
, LOGICAL_IMM_CHECK
| ((flags
& INT_OP
) ? 16 : 32));
633 CHECK_FLAGS(3 << 29);
634 return push_inst(compiler
, (ANDI
^ inv_bits
) | RD(dst
) | RN(reg
) | inst_bits
);
637 inst_bits
= logical_imm(imm
, LOGICAL_IMM_CHECK
| ((flags
& INT_OP
) ? 16 : 32));
644 FAIL_IF(push_inst(compiler
, (inst_bits
^ inv_bits
) | RD(dst
) | RN(reg
)));
647 if (flags
& ARG1_IMM
)
649 if (flags
& INT_OP
) {
651 FAIL_IF(push_inst(compiler
, (UBFM
^ inv_bits
) | RD(dst
) | RN(arg1
) | ((-imm
& 0x1f) << 16) | ((31 - imm
) << 10)));
655 FAIL_IF(push_inst(compiler
, (UBFM
^ inv_bits
) | RD(dst
) | RN(arg1
) | (1 << 22) | ((-imm
& 0x3f) << 16) | ((63 - imm
) << 10)));
660 if (flags
& ARG1_IMM
)
662 if (op
== SLJIT_ASHR
)
664 if (flags
& INT_OP
) {
666 FAIL_IF(push_inst(compiler
, (UBFM
^ inv_bits
) | RD(dst
) | RN(arg1
) | (imm
<< 16) | (31 << 10)));
670 FAIL_IF(push_inst(compiler
, (UBFM
^ inv_bits
) | RD(dst
) | RN(arg1
) | (1 << 22) | (imm
<< 16) | (63 << 10)));
678 if (flags
& ARG2_IMM
) {
682 FAIL_IF(load_immediate(compiler
, TMP_REG2
, arg2
));
690 FAIL_IF(load_immediate(compiler
, TMP_REG1
, arg1
));
696 /* Both arguments are registers. */
702 SLJIT_ASSERT(!(flags
& SET_FLAGS
) && arg1
== TMP_REG1
);
704 return SLJIT_SUCCESS
;
705 return push_inst(compiler
, ORR
| RD(dst
) | RN(TMP_ZERO
) | RM(arg2
));
708 SLJIT_ASSERT(!(flags
& SET_FLAGS
) && arg1
== TMP_REG1
);
709 return push_inst(compiler
, (UBFM
^ (1 << 31)) | RD(dst
) | RN(arg2
) | (7 << 10));
712 SLJIT_ASSERT(!(flags
& SET_FLAGS
) && arg1
== TMP_REG1
);
713 if (!(flags
& INT_OP
))
715 return push_inst(compiler
, (SBFM
^ inv_bits
) | RD(dst
) | RN(arg2
) | (7 << 10));
718 SLJIT_ASSERT(!(flags
& SET_FLAGS
) && arg1
== TMP_REG1
);
719 return push_inst(compiler
, (UBFM
^ (1 << 31)) | RD(dst
) | RN(arg2
) | (15 << 10));
722 SLJIT_ASSERT(!(flags
& SET_FLAGS
) && arg1
== TMP_REG1
);
723 if (!(flags
& INT_OP
))
725 return push_inst(compiler
, (SBFM
^ inv_bits
) | RD(dst
) | RN(arg2
) | (15 << 10));
728 SLJIT_ASSERT(!(flags
& SET_FLAGS
) && arg1
== TMP_REG1
);
729 if ((flags
& INT_OP
) && dst
== arg2
)
730 return SLJIT_SUCCESS
;
731 return push_inst(compiler
, (ORR
^ (1 << 31)) | RD(dst
) | RN(TMP_ZERO
) | RM(arg2
));
734 SLJIT_ASSERT(!(flags
& SET_FLAGS
) && arg1
== TMP_REG1
);
735 if ((flags
& INT_OP
) && dst
== arg2
)
736 return SLJIT_SUCCESS
;
737 return push_inst(compiler
, SBFM
| (1 << 22) | RD(dst
) | RN(arg2
) | (31 << 10));
739 SLJIT_ASSERT(arg1
== TMP_REG1
);
740 FAIL_IF(push_inst(compiler
, (ORN
^ inv_bits
) | RD(dst
) | RN(TMP_ZERO
) | RM(arg2
)));
741 break; /* Set flags. */
743 SLJIT_ASSERT(arg1
== TMP_REG1
);
744 if (flags
& SET_FLAGS
)
746 return push_inst(compiler
, (SUB
^ inv_bits
) | RD(dst
) | RN(TMP_ZERO
) | RM(arg2
));
748 SLJIT_ASSERT(arg1
== TMP_REG1
);
749 return push_inst(compiler
, (CLZ
^ inv_bits
) | RD(dst
) | RN(arg2
));
751 CHECK_FLAGS(1 << 29);
752 return push_inst(compiler
, (ADD
^ inv_bits
) | RD(dst
) | RN(arg1
) | RM(arg2
));
754 CHECK_FLAGS(1 << 29);
755 return push_inst(compiler
, (ADC
^ inv_bits
) | RD(dst
) | RN(arg1
) | RM(arg2
));
757 CHECK_FLAGS(1 << 29);
758 return push_inst(compiler
, (SUB
^ inv_bits
) | RD(dst
) | RN(arg1
) | RM(arg2
));
760 CHECK_FLAGS(1 << 29);
761 return push_inst(compiler
, (SBC
^ inv_bits
) | RD(dst
) | RN(arg1
) | RM(arg2
));
763 if (!(flags
& SET_FLAGS
))
764 return push_inst(compiler
, (MADD
^ inv_bits
) | RD(dst
) | RN(arg1
) | RM(arg2
) | RT2(TMP_ZERO
));
765 if (flags
& INT_OP
) {
766 FAIL_IF(push_inst(compiler
, SMADDL
| RD(dst
) | RN(arg1
) | RM(arg2
) | (31 << 10)));
767 FAIL_IF(push_inst(compiler
, ADD
| RD(TMP_LR
) | RN(TMP_ZERO
) | RM(dst
) | (2 << 22) | (31 << 10)));
768 return push_inst(compiler
, SUBS
| RD(TMP_ZERO
) | RN(TMP_LR
) | RM(dst
) | (2 << 22) | (63 << 10));
770 FAIL_IF(push_inst(compiler
, SMULH
| RD(TMP_LR
) | RN(arg1
) | RM(arg2
)));
771 FAIL_IF(push_inst(compiler
, MADD
| RD(dst
) | RN(arg1
) | RM(arg2
) | RT2(TMP_ZERO
)));
772 return push_inst(compiler
, SUBS
| RD(TMP_ZERO
) | RN(TMP_LR
) | RM(dst
) | (2 << 22) | (63 << 10));
774 CHECK_FLAGS(3 << 29);
775 return push_inst(compiler
, (AND
^ inv_bits
) | RD(dst
) | RN(arg1
) | RM(arg2
));
777 FAIL_IF(push_inst(compiler
, (ORR
^ inv_bits
) | RD(dst
) | RN(arg1
) | RM(arg2
)));
778 break; /* Set flags. */
780 FAIL_IF(push_inst(compiler
, (EOR
^ inv_bits
) | RD(dst
) | RN(arg1
) | RM(arg2
)));
781 break; /* Set flags. */
783 FAIL_IF(push_inst(compiler
, (LSLV
^ inv_bits
) | RD(dst
) | RN(arg1
) | RM(arg2
)));
784 break; /* Set flags. */
786 FAIL_IF(push_inst(compiler
, (LSRV
^ inv_bits
) | RD(dst
) | RN(arg1
) | RM(arg2
)));
787 break; /* Set flags. */
789 FAIL_IF(push_inst(compiler
, (ASRV
^ inv_bits
) | RD(dst
) | RN(arg1
) | RM(arg2
)));
790 break; /* Set flags. */
793 return SLJIT_SUCCESS
;
797 if (flags
& SET_FLAGS
)
798 return push_inst(compiler
, (SUBS
^ inv_bits
) | RD(TMP_ZERO
) | RN(dst
) | RM(TMP_ZERO
));
799 return SLJIT_SUCCESS
;
806 #define ARG_TEST 0x08
808 #define BYTE_SIZE 0x000
809 #define HALF_SIZE 0x100
810 #define INT_SIZE 0x200
811 #define WORD_SIZE 0x300
813 #define MEM_SIZE_SHIFT(flags) ((flags) >> 8)
815 static const sljit_ins sljit_mem_imm
[4] = {
816 /* u l */ 0x39400000 /* ldrb [reg,imm] */,
817 /* u s */ 0x39000000 /* strb [reg,imm] */,
818 /* s l */ 0x39800000 /* ldrsb [reg,imm] */,
819 /* s s */ 0x39000000 /* strb [reg,imm] */,
822 static const sljit_ins sljit_mem_simm
[4] = {
823 /* u l */ 0x38400000 /* ldurb [reg,imm] */,
824 /* u s */ 0x38000000 /* sturb [reg,imm] */,
825 /* s l */ 0x38800000 /* ldursb [reg,imm] */,
826 /* s s */ 0x38000000 /* sturb [reg,imm] */,
829 static const sljit_ins sljit_mem_pre_simm
[4] = {
830 /* u l */ 0x38400c00 /* ldrb [reg,imm]! */,
831 /* u s */ 0x38000c00 /* strb [reg,imm]! */,
832 /* s l */ 0x38800c00 /* ldrsb [reg,imm]! */,
833 /* s s */ 0x38000c00 /* strb [reg,imm]! */,
836 static const sljit_ins sljit_mem_reg
[4] = {
837 /* u l */ 0x38606800 /* ldrb [reg,reg] */,
838 /* u s */ 0x38206800 /* strb [reg,reg] */,
839 /* s l */ 0x38a06800 /* ldrsb [reg,reg] */,
840 /* s s */ 0x38206800 /* strb [reg,reg] */,
843 /* Helper function. Dst should be reg + value, using at most 1 instruction, flags does not set. */
844 static sljit_s32
emit_set_delta(struct sljit_compiler
*compiler
, sljit_s32 dst
, sljit_s32 reg
, sljit_sw value
)
848 return push_inst(compiler
, ADDI
| RD(dst
) | RN(reg
) | (value
<< 10));
849 if (value
<= 0xffffff && !(value
& 0xfff))
850 return push_inst(compiler
, ADDI
| (1 << 22) | RD(dst
) | RN(reg
) | (value
>> 2));
855 return push_inst(compiler
, SUBI
| RD(dst
) | RN(reg
) | (value
<< 10));
856 if (value
<= 0xffffff && !(value
& 0xfff))
857 return push_inst(compiler
, SUBI
| (1 << 22) | RD(dst
) | RN(reg
) | (value
>> 2));
859 return SLJIT_ERR_UNSUPPORTED
;
862 /* Can perform an operation using at most 1 instruction. */
863 static sljit_s32
getput_arg_fast(struct sljit_compiler
*compiler
, sljit_s32 flags
, sljit_s32 reg
, sljit_s32 arg
, sljit_sw argw
)
865 sljit_u32 shift
= MEM_SIZE_SHIFT(flags
);
867 SLJIT_ASSERT(arg
& SLJIT_MEM
);
869 if (SLJIT_UNLIKELY(flags
& UPDATE
)) {
870 if ((arg
& REG_MASK
) && !(arg
& OFFS_REG_MASK
) && argw
<= 255 && argw
>= -256) {
871 if (SLJIT_UNLIKELY(flags
& ARG_TEST
))
876 FAIL_IF(push_inst(compiler
, sljit_mem_pre_simm
[flags
& 0x3]
877 | (shift
<< 30) | RT(reg
) | RN(arg
) | (argw
<< 12)));
883 if (SLJIT_UNLIKELY(arg
& OFFS_REG_MASK
)) {
885 if (argw
&& argw
!= shift
)
888 if (SLJIT_UNLIKELY(flags
& ARG_TEST
))
891 FAIL_IF(push_inst(compiler
, sljit_mem_reg
[flags
& 0x3] | (shift
<< 30) | RT(reg
)
892 | RN(arg
& REG_MASK
) | RM(OFFS_REG(arg
)) | (argw
? (1 << 12) : 0)));
898 if (arg
== SLJIT_UNUSED
)
901 if (argw
>= 0 && (argw
>> shift
) <= 0xfff && (argw
& ((1 << shift
) - 1)) == 0) {
902 if (SLJIT_UNLIKELY(flags
& ARG_TEST
))
905 FAIL_IF(push_inst(compiler
, sljit_mem_imm
[flags
& 0x3] | (shift
<< 30)
906 | RT(reg
) | RN(arg
) | (argw
<< (10 - shift
))));
910 if (argw
> 255 || argw
< -256)
913 if (SLJIT_UNLIKELY(flags
& ARG_TEST
))
916 FAIL_IF(push_inst(compiler
, sljit_mem_simm
[flags
& 0x3] | (shift
<< 30)
917 | RT(reg
) | RN(arg
) | ((argw
& 0x1ff) << 12)));
921 /* see getput_arg below.
922 Note: can_cache is called only for binary operators. Those
923 operators always uses word arguments without write back. */
924 static sljit_s32
can_cache(sljit_s32 arg
, sljit_sw argw
, sljit_s32 next_arg
, sljit_sw next_argw
)
927 if ((arg
& OFFS_REG_MASK
) || !(next_arg
& SLJIT_MEM
))
930 if (!(arg
& REG_MASK
)) {
931 diff
= argw
- next_argw
;
932 if (diff
<= 0xfff && diff
>= -0xfff)
937 if (argw
== next_argw
)
940 diff
= argw
- next_argw
;
941 if (arg
== next_arg
&& diff
<= 0xfff && diff
>= -0xfff)
947 /* Emit the necessary instructions. See can_cache above. */
948 static sljit_s32
getput_arg(struct sljit_compiler
*compiler
, sljit_s32 flags
, sljit_s32 reg
,
949 sljit_s32 arg
, sljit_sw argw
, sljit_s32 next_arg
, sljit_sw next_argw
)
951 sljit_u32 shift
= MEM_SIZE_SHIFT(flags
);
952 sljit_s32 tmp_r
, other_r
;
955 SLJIT_ASSERT(arg
& SLJIT_MEM
);
956 if (!(next_arg
& SLJIT_MEM
)) {
961 tmp_r
= ((flags
& STORE
) || (flags
== (WORD_SIZE
| SIGNED
))) ? TMP_REG3
: reg
;
963 if (SLJIT_UNLIKELY((flags
& UPDATE
) && (arg
& REG_MASK
))) {
964 /* Update only applies if a base register exists. */
965 other_r
= OFFS_REG(arg
);
967 other_r
= arg
& REG_MASK
;
968 SLJIT_ASSERT(other_r
!= reg
);
970 if (argw
>= 0 && argw
<= 0xffffff) {
971 if ((argw
& 0xfff) != 0)
972 FAIL_IF(push_inst(compiler
, ADDI
| RD(other_r
) | RN(other_r
) | ((argw
& 0xfff) << 10)));
974 FAIL_IF(push_inst(compiler
, ADDI
| (1 << 22) | RD(other_r
) | RN(other_r
) | ((argw
>> 12) << 10)));
975 return push_inst(compiler
, sljit_mem_imm
[flags
& 0x3] | (shift
<< 30) | RT(reg
) | RN(other_r
));
977 else if (argw
< 0 && argw
>= -0xffffff) {
979 if ((argw
& 0xfff) != 0)
980 FAIL_IF(push_inst(compiler
, SUBI
| RD(other_r
) | RN(other_r
) | ((argw
& 0xfff) << 10)));
982 FAIL_IF(push_inst(compiler
, SUBI
| (1 << 22) | RD(other_r
) | RN(other_r
) | ((argw
>> 12) << 10)));
983 return push_inst(compiler
, sljit_mem_imm
[flags
& 0x3] | (shift
<< 30) | RT(reg
) | RN(other_r
));
986 if (compiler
->cache_arg
== SLJIT_MEM
) {
987 if (argw
== compiler
->cache_argw
) {
991 else if (emit_set_delta(compiler
, TMP_REG3
, TMP_REG3
, argw
- compiler
->cache_argw
) != SLJIT_ERR_UNSUPPORTED
) {
992 FAIL_IF(compiler
->error
);
993 compiler
->cache_argw
= argw
;
1000 FAIL_IF(load_immediate(compiler
, TMP_REG3
, argw
));
1001 compiler
->cache_arg
= SLJIT_MEM
;
1002 compiler
->cache_argw
= argw
;
1008 /* No caching here. */
1010 FAIL_IF(push_inst(compiler
, sljit_mem_reg
[flags
& 0x3] | (shift
<< 30) | RT(reg
) | RN(arg
) | RM(other_r
)));
1011 return push_inst(compiler
, ADD
| RD(arg
) | RN(arg
) | RM(other_r
));
1014 if (arg
& OFFS_REG_MASK
) {
1015 other_r
= OFFS_REG(arg
);
1017 FAIL_IF(push_inst(compiler
, ADD
| RD(tmp_r
) | RN(arg
) | RM(other_r
) | ((argw
& 0x3) << 10)));
1018 return push_inst(compiler
, sljit_mem_imm
[flags
& 0x3] | (shift
<< 30) | RT(reg
) | RN(tmp_r
));
1021 if (compiler
->cache_arg
== arg
) {
1022 diff
= argw
- compiler
->cache_argw
;
1023 if (diff
<= 255 && diff
>= -256)
1024 return push_inst(compiler
, sljit_mem_simm
[flags
& 0x3] | (shift
<< 30)
1025 | RT(reg
) | RN(TMP_REG3
) | ((diff
& 0x1ff) << 12));
1026 if (emit_set_delta(compiler
, TMP_REG3
, TMP_REG3
, diff
) != SLJIT_ERR_UNSUPPORTED
) {
1027 FAIL_IF(compiler
->error
);
1028 return push_inst(compiler
, sljit_mem_imm
[flags
& 0x3] | (shift
<< 30) | RT(reg
) | RN(arg
));
1032 diff
= argw
- next_argw
;
1033 next_arg
= (arg
& REG_MASK
) && (arg
== next_arg
) && diff
<= 0xfff && diff
>= -0xfff && diff
!= 0;
1036 if (arg
!= SLJIT_UNUSED
&& argw
>= 0 && argw
<= 0xffffff && (argw
& ((1 << shift
) - 1)) == 0) {
1037 FAIL_IF(push_inst(compiler
, ADDI
| (1 << 22) | RD(tmp_r
) | RN(arg
) | ((argw
>> 12) << 10)));
1038 return push_inst(compiler
, sljit_mem_imm
[flags
& 0x3] | (shift
<< 30)
1039 | RT(reg
) | RN(tmp_r
) | ((argw
& 0xfff) << (10 - shift
)));
1042 if (arg
&& compiler
->cache_arg
== SLJIT_MEM
) {
1043 if (compiler
->cache_argw
== argw
)
1044 return push_inst(compiler
, sljit_mem_reg
[flags
& 0x3] | (shift
<< 30) | RT(reg
) | RN(arg
) | RM(TMP_REG3
));
1045 if (emit_set_delta(compiler
, TMP_REG3
, TMP_REG3
, argw
- compiler
->cache_argw
) != SLJIT_ERR_UNSUPPORTED
) {
1046 FAIL_IF(compiler
->error
);
1047 compiler
->cache_argw
= argw
;
1048 return push_inst(compiler
, sljit_mem_reg
[flags
& 0x3] | (shift
<< 30) | RT(reg
) | RN(arg
) | RM(TMP_REG3
));
1052 compiler
->cache_argw
= argw
;
1053 if (next_arg
&& emit_set_delta(compiler
, TMP_REG3
, arg
, argw
) != SLJIT_ERR_UNSUPPORTED
) {
1054 FAIL_IF(compiler
->error
);
1055 compiler
->cache_arg
= SLJIT_MEM
| arg
;
1059 FAIL_IF(load_immediate(compiler
, TMP_REG3
, argw
));
1060 compiler
->cache_arg
= SLJIT_MEM
;
1063 FAIL_IF(push_inst(compiler
, ADD
| RD(TMP_REG3
) | RN(TMP_REG3
) | RM(arg
)));
1064 compiler
->cache_arg
= SLJIT_MEM
| arg
;
1070 return push_inst(compiler
, sljit_mem_reg
[flags
& 0x3] | (shift
<< 30) | RT(reg
) | RN(arg
) | RM(TMP_REG3
));
1071 return push_inst(compiler
, sljit_mem_imm
[flags
& 0x3] | (shift
<< 30) | RT(reg
) | RN(TMP_REG3
));
1074 static SLJIT_INLINE sljit_s32
emit_op_mem(struct sljit_compiler
*compiler
, sljit_s32 flags
, sljit_s32 reg
, sljit_s32 arg
, sljit_sw argw
)
1076 if (getput_arg_fast(compiler
, flags
, reg
, arg
, argw
))
1077 return compiler
->error
;
1078 compiler
->cache_arg
= 0;
1079 compiler
->cache_argw
= 0;
1080 return getput_arg(compiler
, flags
, reg
, arg
, argw
, 0, 0);
1083 static SLJIT_INLINE sljit_s32
emit_op_mem2(struct sljit_compiler
*compiler
, sljit_s32 flags
, sljit_s32 reg
, sljit_s32 arg1
, sljit_sw arg1w
, sljit_s32 arg2
, sljit_sw arg2w
)
1085 if (getput_arg_fast(compiler
, flags
, reg
, arg1
, arg1w
))
1086 return compiler
->error
;
1087 return getput_arg(compiler
, flags
, reg
, arg1
, arg1w
, arg2
, arg2w
);
1090 /* --------------------------------------------------------------------- */
1092 /* --------------------------------------------------------------------- */
1094 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_enter(struct sljit_compiler
*compiler
,
1095 sljit_s32 options
, sljit_s32 args
, sljit_s32 scratches
, sljit_s32 saveds
,
1096 sljit_s32 fscratches
, sljit_s32 fsaveds
, sljit_s32 local_size
)
1098 sljit_s32 i
, tmp
, offs
, prev
, saved_regs_size
;
1101 CHECK(check_sljit_emit_enter(compiler
, options
, args
, scratches
, saveds
, fscratches
, fsaveds
, local_size
));
1102 set_emit_enter(compiler
, options
, args
, scratches
, saveds
, fscratches
, fsaveds
, local_size
);
1104 saved_regs_size
= GET_SAVED_REGISTERS_SIZE(scratches
, saveds
, 0);
1105 local_size
+= saved_regs_size
+ SLJIT_LOCALS_OFFSET
;
1106 local_size
= (local_size
+ 15) & ~0xf;
1107 compiler
->local_size
= local_size
;
1109 if (local_size
<= (63 * sizeof(sljit_sw
))) {
1110 FAIL_IF(push_inst(compiler
, STP_PRE
| 29 | RT2(TMP_LR
)
1111 | RN(TMP_SP
) | ((-(local_size
>> 3) & 0x7f) << 15)));
1112 FAIL_IF(push_inst(compiler
, ADDI
| RD(SLJIT_SP
) | RN(TMP_SP
) | (0 << 10)));
1113 offs
= (local_size
- saved_regs_size
) << (15 - 3);
1116 if (saved_regs_size
& 0x8) {
1118 saved_regs_size
+= sizeof(sljit_sw
);
1120 local_size
-= saved_regs_size
+ SLJIT_LOCALS_OFFSET
;
1121 if (saved_regs_size
> 0)
1122 FAIL_IF(push_inst(compiler
, SUBI
| RD(TMP_SP
) | RN(TMP_SP
) | (saved_regs_size
<< 10)));
1125 tmp
= saveds
< SLJIT_NUMBER_OF_SAVED_REGISTERS
? (SLJIT_S0
+ 1 - saveds
) : SLJIT_FIRST_SAVED_REG
;
1127 for (i
= SLJIT_S0
; i
>= tmp
; i
--) {
1129 if (!(offs
& (1 << 15))) {
1133 FAIL_IF(push_inst(compiler
, STRI
| RT(i
) | RN(TMP_SP
) | (offs
>> 5)));
1137 FAIL_IF(push_inst(compiler
, STP
| RT(prev
) | RT2(i
) | RN(TMP_SP
) | offs
));
1142 for (i
= scratches
; i
>= SLJIT_FIRST_SAVED_REG
; i
--) {
1144 if (!(offs
& (1 << 15))) {
1148 FAIL_IF(push_inst(compiler
, STRI
| RT(i
) | RN(TMP_SP
) | (offs
>> 5)));
1152 FAIL_IF(push_inst(compiler
, STP
| RT(prev
) | RT2(i
) | RN(TMP_SP
) | offs
));
1157 SLJIT_ASSERT(prev
== -1);
1159 if (compiler
->local_size
> (63 * sizeof(sljit_sw
))) {
1160 /* The local_size is already adjusted by the saved registers. */
1161 if (local_size
> 0xfff) {
1162 FAIL_IF(push_inst(compiler
, SUBI
| RD(TMP_SP
) | RN(TMP_SP
) | ((local_size
>> 12) << 10) | (1 << 22)));
1163 local_size
&= 0xfff;
1166 FAIL_IF(push_inst(compiler
, SUBI
| RD(TMP_SP
) | RN(TMP_SP
) | (local_size
<< 10)));
1167 FAIL_IF(push_inst(compiler
, STP_PRE
| 29 | RT2(TMP_LR
)
1168 | RN(TMP_SP
) | ((-(16 >> 3) & 0x7f) << 15)));
1169 FAIL_IF(push_inst(compiler
, ADDI
| RD(SLJIT_SP
) | RN(TMP_SP
) | (0 << 10)));
1173 FAIL_IF(push_inst(compiler
, ORR
| RD(SLJIT_S0
) | RN(TMP_ZERO
) | RM(SLJIT_R0
)));
1175 FAIL_IF(push_inst(compiler
, ORR
| RD(SLJIT_S1
) | RN(TMP_ZERO
) | RM(SLJIT_R1
)));
1177 FAIL_IF(push_inst(compiler
, ORR
| RD(SLJIT_S2
) | RN(TMP_ZERO
) | RM(SLJIT_R2
)));
1179 return SLJIT_SUCCESS
;
1182 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_set_context(struct sljit_compiler
*compiler
,
1183 sljit_s32 options
, sljit_s32 args
, sljit_s32 scratches
, sljit_s32 saveds
,
1184 sljit_s32 fscratches
, sljit_s32 fsaveds
, sljit_s32 local_size
)
1187 CHECK(check_sljit_set_context(compiler
, options
, args
, scratches
, saveds
, fscratches
, fsaveds
, local_size
));
1188 set_set_context(compiler
, options
, args
, scratches
, saveds
, fscratches
, fsaveds
, local_size
);
1190 local_size
+= GET_SAVED_REGISTERS_SIZE(scratches
, saveds
, 0) + SLJIT_LOCALS_OFFSET
;
1191 local_size
= (local_size
+ 15) & ~0xf;
1192 compiler
->local_size
= local_size
;
1193 return SLJIT_SUCCESS
;
1196 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_return(struct sljit_compiler
*compiler
, sljit_s32 op
, sljit_s32 src
, sljit_sw srcw
)
1198 sljit_s32 local_size
;
1199 sljit_s32 i
, tmp
, offs
, prev
, saved_regs_size
;
1202 CHECK(check_sljit_emit_return(compiler
, op
, src
, srcw
));
1204 FAIL_IF(emit_mov_before_return(compiler
, op
, src
, srcw
));
1206 local_size
= compiler
->local_size
;
1208 saved_regs_size
= GET_SAVED_REGISTERS_SIZE(compiler
->scratches
, compiler
->saveds
, 0);
1209 if (local_size
<= (63 * sizeof(sljit_sw
)))
1210 offs
= (local_size
- saved_regs_size
) << (15 - 3);
1212 FAIL_IF(push_inst(compiler
, LDP_PST
| 29 | RT2(TMP_LR
)
1213 | RN(TMP_SP
) | (((16 >> 3) & 0x7f) << 15)));
1215 if (saved_regs_size
& 0x8) {
1217 saved_regs_size
+= sizeof(sljit_sw
);
1219 local_size
-= saved_regs_size
+ SLJIT_LOCALS_OFFSET
;
1220 if (local_size
> 0xfff) {
1221 FAIL_IF(push_inst(compiler
, ADDI
| RD(TMP_SP
) | RN(TMP_SP
) | ((local_size
>> 12) << 10) | (1 << 22)));
1222 local_size
&= 0xfff;
1225 FAIL_IF(push_inst(compiler
, ADDI
| RD(TMP_SP
) | RN(TMP_SP
) | (local_size
<< 10)));
1228 tmp
= compiler
->saveds
< SLJIT_NUMBER_OF_SAVED_REGISTERS
? (SLJIT_S0
+ 1 - compiler
->saveds
) : SLJIT_FIRST_SAVED_REG
;
1230 for (i
= SLJIT_S0
; i
>= tmp
; i
--) {
1232 if (!(offs
& (1 << 15))) {
1236 FAIL_IF(push_inst(compiler
, LDRI
| RT(i
) | RN(TMP_SP
) | (offs
>> 5)));
1240 FAIL_IF(push_inst(compiler
, LDP
| RT(prev
) | RT2(i
) | RN(TMP_SP
) | offs
));
1245 for (i
= compiler
->scratches
; i
>= SLJIT_FIRST_SAVED_REG
; i
--) {
1247 if (!(offs
& (1 << 15))) {
1251 FAIL_IF(push_inst(compiler
, LDRI
| RT(i
) | RN(TMP_SP
) | (offs
>> 5)));
1255 FAIL_IF(push_inst(compiler
, LDP
| RT(prev
) | RT2(i
) | RN(TMP_SP
) | offs
));
1260 SLJIT_ASSERT(prev
== -1);
1262 if (compiler
->local_size
<= (63 * sizeof(sljit_sw
))) {
1263 FAIL_IF(push_inst(compiler
, LDP_PST
| 29 | RT2(TMP_LR
)
1264 | RN(TMP_SP
) | (((local_size
>> 3) & 0x7f) << 15)));
1265 } else if (saved_regs_size
> 0) {
1266 FAIL_IF(push_inst(compiler
, ADDI
| RD(TMP_SP
) | RN(TMP_SP
) | (saved_regs_size
<< 10)));
1269 FAIL_IF(push_inst(compiler
, RET
| RN(TMP_LR
)));
1270 return SLJIT_SUCCESS
;
1273 /* --------------------------------------------------------------------- */
1275 /* --------------------------------------------------------------------- */
1277 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op0(struct sljit_compiler
*compiler
, sljit_s32 op
)
1279 sljit_ins inv_bits
= (op
& SLJIT_I32_OP
) ? (1 << 31) : 0;
1282 CHECK(check_sljit_emit_op0(compiler
, op
));
1284 op
= GET_OPCODE(op
);
1286 case SLJIT_BREAKPOINT
:
1287 return push_inst(compiler
, BRK
);
1289 return push_inst(compiler
, NOP
);
1292 FAIL_IF(push_inst(compiler
, ORR
| RD(TMP_REG1
) | RN(TMP_ZERO
) | RM(SLJIT_R0
)));
1293 FAIL_IF(push_inst(compiler
, MADD
| RD(SLJIT_R0
) | RN(SLJIT_R0
) | RM(SLJIT_R1
) | RT2(TMP_ZERO
)));
1294 return push_inst(compiler
, (op
== SLJIT_LMUL_UW
? UMULH
: SMULH
) | RD(SLJIT_R1
) | RN(TMP_REG1
) | RM(SLJIT_R1
));
1295 case SLJIT_DIVMOD_UW
:
1296 case SLJIT_DIVMOD_SW
:
1297 FAIL_IF(push_inst(compiler
, (ORR
^ inv_bits
) | RD(TMP_REG1
) | RN(TMP_ZERO
) | RM(SLJIT_R0
)));
1298 FAIL_IF(push_inst(compiler
, ((op
== SLJIT_DIVMOD_UW
? UDIV
: SDIV
) ^ inv_bits
) | RD(SLJIT_R0
) | RN(SLJIT_R0
) | RM(SLJIT_R1
)));
1299 FAIL_IF(push_inst(compiler
, (MADD
^ inv_bits
) | RD(SLJIT_R1
) | RN(SLJIT_R0
) | RM(SLJIT_R1
) | RT2(TMP_ZERO
)));
1300 return push_inst(compiler
, (SUB
^ inv_bits
) | RD(SLJIT_R1
) | RN(TMP_REG1
) | RM(SLJIT_R1
));
1303 return push_inst(compiler
, ((op
== SLJIT_DIV_UW
? UDIV
: SDIV
) ^ inv_bits
) | RD(SLJIT_R0
) | RN(SLJIT_R0
) | RM(SLJIT_R1
));
1306 return SLJIT_SUCCESS
;
1309 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op1(struct sljit_compiler
*compiler
, sljit_s32 op
,
1310 sljit_s32 dst
, sljit_sw dstw
,
1311 sljit_s32 src
, sljit_sw srcw
)
1313 sljit_s32 dst_r
, flags
, mem_flags
;
1314 sljit_s32 op_flags
= GET_ALL_FLAGS(op
);
1317 CHECK(check_sljit_emit_op1(compiler
, op
, dst
, dstw
, src
, srcw
));
1318 ADJUST_LOCAL_OFFSET(dst
, dstw
);
1319 ADJUST_LOCAL_OFFSET(src
, srcw
);
1321 compiler
->cache_arg
= 0;
1322 compiler
->cache_argw
= 0;
1324 if (dst
== SLJIT_UNUSED
&& !HAS_FLAGS(op
)) {
1325 if (op
<= SLJIT_MOV_P
&& (src
& SLJIT_MEM
)) {
1326 SLJIT_ASSERT(reg_map
[1] == 0 && reg_map
[3] == 2 && reg_map
[5] == 4);
1328 if (op
>= SLJIT_MOV_U8
&& op
<= SLJIT_MOV_S8
)
1330 else if (op
>= SLJIT_MOV_U16
&& op
<= SLJIT_MOV_S16
)
1335 /* Signed word sized load is the prefetch instruction. */
1336 return emit_op_mem(compiler
, WORD_SIZE
| SIGNED
, dst
, src
, srcw
);
1338 return SLJIT_SUCCESS
;
1341 dst_r
= SLOW_IS_REG(dst
) ? dst
: TMP_REG1
;
1343 op
= GET_OPCODE(op
);
1344 if (op
>= SLJIT_MOV
&& op
<= SLJIT_MOVU_P
) {
1352 if (src
& SLJIT_IMM
)
1353 srcw
= (sljit_u8
)srcw
;
1356 flags
= BYTE_SIZE
| SIGNED
;
1357 if (src
& SLJIT_IMM
)
1358 srcw
= (sljit_s8
)srcw
;
1362 if (src
& SLJIT_IMM
)
1363 srcw
= (sljit_u16
)srcw
;
1366 flags
= HALF_SIZE
| SIGNED
;
1367 if (src
& SLJIT_IMM
)
1368 srcw
= (sljit_s16
)srcw
;
1372 if (src
& SLJIT_IMM
)
1373 srcw
= (sljit_u32
)srcw
;
1376 flags
= INT_SIZE
| SIGNED
;
1377 if (src
& SLJIT_IMM
)
1378 srcw
= (sljit_s32
)srcw
;
1382 flags
= WORD_SIZE
| UPDATE
;
1385 flags
= BYTE_SIZE
| UPDATE
;
1386 if (src
& SLJIT_IMM
)
1387 srcw
= (sljit_u8
)srcw
;
1390 flags
= BYTE_SIZE
| SIGNED
| UPDATE
;
1391 if (src
& SLJIT_IMM
)
1392 srcw
= (sljit_s8
)srcw
;
1394 case SLJIT_MOVU_U16
:
1395 flags
= HALF_SIZE
| UPDATE
;
1396 if (src
& SLJIT_IMM
)
1397 srcw
= (sljit_u16
)srcw
;
1399 case SLJIT_MOVU_S16
:
1400 flags
= HALF_SIZE
| SIGNED
| UPDATE
;
1401 if (src
& SLJIT_IMM
)
1402 srcw
= (sljit_s16
)srcw
;
1404 case SLJIT_MOVU_U32
:
1405 flags
= INT_SIZE
| UPDATE
;
1406 if (src
& SLJIT_IMM
)
1407 srcw
= (sljit_u32
)srcw
;
1409 case SLJIT_MOVU_S32
:
1410 flags
= INT_SIZE
| SIGNED
| UPDATE
;
1411 if (src
& SLJIT_IMM
)
1412 srcw
= (sljit_s32
)srcw
;
1415 SLJIT_UNREACHABLE();
1420 if (src
& SLJIT_IMM
)
1421 FAIL_IF(emit_op_imm(compiler
, SLJIT_MOV
| ARG2_IMM
, dst_r
, TMP_REG1
, srcw
));
1422 else if (src
& SLJIT_MEM
) {
1423 if (getput_arg_fast(compiler
, flags
, dst_r
, src
, srcw
))
1424 FAIL_IF(compiler
->error
);
1426 FAIL_IF(getput_arg(compiler
, flags
, dst_r
, src
, srcw
, dst
, dstw
));
1428 if (dst_r
!= TMP_REG1
)
1429 return emit_op_imm(compiler
, op
| ((op_flags
& SLJIT_I32_OP
) ? INT_OP
: 0), dst_r
, TMP_REG1
, src
);
1433 if (dst
& SLJIT_MEM
) {
1434 if (getput_arg_fast(compiler
, flags
| STORE
, dst_r
, dst
, dstw
))
1435 return compiler
->error
;
1437 return getput_arg(compiler
, flags
| STORE
, dst_r
, dst
, dstw
, 0, 0);
1439 return SLJIT_SUCCESS
;
1442 flags
= HAS_FLAGS(op_flags
) ? SET_FLAGS
: 0;
1443 mem_flags
= WORD_SIZE
;
1444 if (op_flags
& SLJIT_I32_OP
) {
1446 mem_flags
= INT_SIZE
;
1449 if (dst
== SLJIT_UNUSED
)
1450 flags
|= UNUSED_RETURN
;
1452 if (src
& SLJIT_MEM
) {
1453 if (getput_arg_fast(compiler
, mem_flags
, TMP_REG2
, src
, srcw
))
1454 FAIL_IF(compiler
->error
);
1456 FAIL_IF(getput_arg(compiler
, mem_flags
, TMP_REG2
, src
, srcw
, dst
, dstw
));
1460 if (src
& SLJIT_IMM
) {
1462 if (op_flags
& SLJIT_I32_OP
)
1463 srcw
= (sljit_s32
)srcw
;
1467 emit_op_imm(compiler
, flags
| op
, dst_r
, TMP_REG1
, srcw
);
1469 if (dst
& SLJIT_MEM
) {
1470 if (getput_arg_fast(compiler
, mem_flags
| STORE
, dst_r
, dst
, dstw
))
1471 return compiler
->error
;
1473 return getput_arg(compiler
, mem_flags
| STORE
, dst_r
, dst
, dstw
, 0, 0);
1475 return SLJIT_SUCCESS
;
1478 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op2(struct sljit_compiler
*compiler
, sljit_s32 op
,
1479 sljit_s32 dst
, sljit_sw dstw
,
1480 sljit_s32 src1
, sljit_sw src1w
,
1481 sljit_s32 src2
, sljit_sw src2w
)
1483 sljit_s32 dst_r
, flags
, mem_flags
;
1486 CHECK(check_sljit_emit_op2(compiler
, op
, dst
, dstw
, src1
, src1w
, src2
, src2w
));
1487 ADJUST_LOCAL_OFFSET(dst
, dstw
);
1488 ADJUST_LOCAL_OFFSET(src1
, src1w
);
1489 ADJUST_LOCAL_OFFSET(src2
, src2w
);
1491 compiler
->cache_arg
= 0;
1492 compiler
->cache_argw
= 0;
1494 if (dst
== SLJIT_UNUSED
&& !HAS_FLAGS(op
))
1495 return SLJIT_SUCCESS
;
1497 dst_r
= SLOW_IS_REG(dst
) ? dst
: TMP_REG1
;
1498 flags
= HAS_FLAGS(op
) ? SET_FLAGS
: 0;
1499 mem_flags
= WORD_SIZE
;
1500 if (op
& SLJIT_I32_OP
) {
1502 mem_flags
= INT_SIZE
;
1505 if (dst
== SLJIT_UNUSED
)
1506 flags
|= UNUSED_RETURN
;
1508 if ((dst
& SLJIT_MEM
) && !getput_arg_fast(compiler
, mem_flags
| STORE
| ARG_TEST
, TMP_REG1
, dst
, dstw
))
1511 if (src1
& SLJIT_MEM
) {
1512 if (getput_arg_fast(compiler
, mem_flags
, TMP_REG1
, src1
, src1w
))
1513 FAIL_IF(compiler
->error
);
1517 if (src2
& SLJIT_MEM
) {
1518 if (getput_arg_fast(compiler
, mem_flags
, TMP_REG2
, src2
, src2w
))
1519 FAIL_IF(compiler
->error
);
1524 if ((flags
& (SLOW_SRC1
| SLOW_SRC2
)) == (SLOW_SRC1
| SLOW_SRC2
)) {
1525 if (!can_cache(src1
, src1w
, src2
, src2w
) && can_cache(src1
, src1w
, dst
, dstw
)) {
1526 FAIL_IF(getput_arg(compiler
, mem_flags
, TMP_REG2
, src2
, src2w
, src1
, src1w
));
1527 FAIL_IF(getput_arg(compiler
, mem_flags
, TMP_REG1
, src1
, src1w
, dst
, dstw
));
1530 FAIL_IF(getput_arg(compiler
, mem_flags
, TMP_REG1
, src1
, src1w
, src2
, src2w
));
1531 FAIL_IF(getput_arg(compiler
, mem_flags
, TMP_REG2
, src2
, src2w
, dst
, dstw
));
1534 else if (flags
& SLOW_SRC1
)
1535 FAIL_IF(getput_arg(compiler
, mem_flags
, TMP_REG1
, src1
, src1w
, dst
, dstw
));
1536 else if (flags
& SLOW_SRC2
)
1537 FAIL_IF(getput_arg(compiler
, mem_flags
, TMP_REG2
, src2
, src2w
, dst
, dstw
));
1539 if (src1
& SLJIT_MEM
)
1541 if (src2
& SLJIT_MEM
)
1544 if (src1
& SLJIT_IMM
)
1548 if (src2
& SLJIT_IMM
)
1553 emit_op_imm(compiler
, flags
| GET_OPCODE(op
), dst_r
, src1w
, src2w
);
1555 if (dst
& SLJIT_MEM
) {
1556 if (!(flags
& SLOW_DEST
)) {
1557 getput_arg_fast(compiler
, mem_flags
| STORE
, dst_r
, dst
, dstw
);
1558 return compiler
->error
;
1560 return getput_arg(compiler
, mem_flags
| STORE
, TMP_REG1
, dst
, dstw
, 0, 0);
1563 return SLJIT_SUCCESS
;
1566 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_get_register_index(sljit_s32 reg
)
1568 CHECK_REG_INDEX(check_sljit_get_register_index(reg
));
1569 return reg_map
[reg
];
1572 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_get_float_register_index(sljit_s32 reg
)
1574 CHECK_REG_INDEX(check_sljit_get_float_register_index(reg
));
1575 return freg_map
[reg
];
1578 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op_custom(struct sljit_compiler
*compiler
,
1579 void *instruction
, sljit_s32 size
)
1582 CHECK(check_sljit_emit_op_custom(compiler
, instruction
, size
));
1584 return push_inst(compiler
, *(sljit_ins
*)instruction
);
1587 /* --------------------------------------------------------------------- */
1588 /* Floating point operators */
1589 /* --------------------------------------------------------------------- */
1591 static sljit_s32
emit_fop_mem(struct sljit_compiler
*compiler
, sljit_s32 flags
, sljit_s32 reg
, sljit_s32 arg
, sljit_sw argw
)
1593 sljit_u32 shift
= MEM_SIZE_SHIFT(flags
);
1594 sljit_ins ins_bits
= (shift
<< 30);
1598 SLJIT_ASSERT(arg
& SLJIT_MEM
);
1600 if (!(flags
& STORE
))
1601 ins_bits
|= 1 << 22;
1603 if (arg
& OFFS_REG_MASK
) {
1605 if (!argw
|| argw
== shift
)
1606 return push_inst(compiler
, STR_FR
| ins_bits
| VT(reg
)
1607 | RN(arg
& REG_MASK
) | RM(OFFS_REG(arg
)) | (argw
? (1 << 12) : 0));
1608 other_r
= OFFS_REG(arg
);
1610 FAIL_IF(push_inst(compiler
, ADD
| RD(TMP_REG1
) | RN(arg
) | RM(other_r
) | (argw
<< 10)));
1616 if (arg
&& argw
>= 0 && ((argw
>> shift
) <= 0xfff) && (argw
& ((1 << shift
) - 1)) == 0)
1617 return push_inst(compiler
, STR_FI
| ins_bits
| VT(reg
) | RN(arg
) | (argw
<< (10 - shift
)));
1619 if (arg
&& argw
<= 255 && argw
>= -256)
1620 return push_inst(compiler
, STUR_FI
| ins_bits
| VT(reg
) | RN(arg
) | ((argw
& 0x1ff) << 12));
1623 if (compiler
->cache_arg
== SLJIT_MEM
&& argw
!= compiler
->cache_argw
) {
1624 diff
= argw
- compiler
->cache_argw
;
1625 if (!arg
&& diff
<= 255 && diff
>= -256)
1626 return push_inst(compiler
, STUR_FI
| ins_bits
| VT(reg
) | RN(TMP_REG3
) | ((diff
& 0x1ff) << 12));
1627 if (emit_set_delta(compiler
, TMP_REG3
, TMP_REG3
, argw
- compiler
->cache_argw
) != SLJIT_ERR_UNSUPPORTED
) {
1628 FAIL_IF(compiler
->error
);
1629 compiler
->cache_argw
= argw
;
1633 if (compiler
->cache_arg
!= SLJIT_MEM
|| argw
!= compiler
->cache_argw
) {
1634 compiler
->cache_arg
= SLJIT_MEM
;
1635 compiler
->cache_argw
= argw
;
1636 FAIL_IF(load_immediate(compiler
, TMP_REG3
, argw
));
1640 return push_inst(compiler
, STR_FR
| ins_bits
| VT(reg
) | RN(arg
) | RM(TMP_REG3
));
1641 return push_inst(compiler
, STR_FI
| ins_bits
| VT(reg
) | RN(TMP_REG3
));
1644 static SLJIT_INLINE sljit_s32
sljit_emit_fop1_conv_sw_from_f64(struct sljit_compiler
*compiler
, sljit_s32 op
,
1645 sljit_s32 dst
, sljit_sw dstw
,
1646 sljit_s32 src
, sljit_sw srcw
)
1648 sljit_s32 dst_r
= FAST_IS_REG(dst
) ? dst
: TMP_REG1
;
1649 sljit_ins inv_bits
= (op
& SLJIT_F32_OP
) ? (1 << 22) : 0;
1651 if (GET_OPCODE(op
) == SLJIT_CONV_S32_FROM_F64
)
1652 inv_bits
|= (1 << 31);
1654 if (src
& SLJIT_MEM
) {
1655 emit_fop_mem(compiler
, (op
& SLJIT_F32_OP
) ? INT_SIZE
: WORD_SIZE
, TMP_FREG1
, src
, srcw
);
1659 FAIL_IF(push_inst(compiler
, (FCVTZS
^ inv_bits
) | RD(dst_r
) | VN(src
)));
1661 if (dst
& SLJIT_MEM
)
1662 return emit_op_mem(compiler
, ((GET_OPCODE(op
) == SLJIT_CONV_S32_FROM_F64
) ? INT_SIZE
: WORD_SIZE
) | STORE
, TMP_REG1
, dst
, dstw
);
1663 return SLJIT_SUCCESS
;
1666 static SLJIT_INLINE sljit_s32
sljit_emit_fop1_conv_f64_from_sw(struct sljit_compiler
*compiler
, sljit_s32 op
,
1667 sljit_s32 dst
, sljit_sw dstw
,
1668 sljit_s32 src
, sljit_sw srcw
)
1670 sljit_s32 dst_r
= FAST_IS_REG(dst
) ? dst
: TMP_FREG1
;
1671 sljit_ins inv_bits
= (op
& SLJIT_F32_OP
) ? (1 << 22) : 0;
1673 if (GET_OPCODE(op
) == SLJIT_CONV_F64_FROM_S32
)
1674 inv_bits
|= (1 << 31);
1676 if (src
& SLJIT_MEM
) {
1677 emit_op_mem(compiler
, ((GET_OPCODE(op
) == SLJIT_CONV_F64_FROM_S32
) ? INT_SIZE
: WORD_SIZE
), TMP_REG1
, src
, srcw
);
1679 } else if (src
& SLJIT_IMM
) {
1680 #if (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64)
1681 if (GET_OPCODE(op
) == SLJIT_CONV_F64_FROM_S32
)
1682 srcw
= (sljit_s32
)srcw
;
1684 FAIL_IF(load_immediate(compiler
, TMP_REG1
, srcw
));
1688 FAIL_IF(push_inst(compiler
, (SCVTF
^ inv_bits
) | VD(dst_r
) | RN(src
)));
1690 if (dst
& SLJIT_MEM
)
1691 return emit_fop_mem(compiler
, ((op
& SLJIT_F32_OP
) ? INT_SIZE
: WORD_SIZE
) | STORE
, TMP_FREG1
, dst
, dstw
);
1692 return SLJIT_SUCCESS
;
1695 static SLJIT_INLINE sljit_s32
sljit_emit_fop1_cmp(struct sljit_compiler
*compiler
, sljit_s32 op
,
1696 sljit_s32 src1
, sljit_sw src1w
,
1697 sljit_s32 src2
, sljit_sw src2w
)
1699 sljit_s32 mem_flags
= (op
& SLJIT_F32_OP
) ? INT_SIZE
: WORD_SIZE
;
1700 sljit_ins inv_bits
= (op
& SLJIT_F32_OP
) ? (1 << 22) : 0;
1702 if (src1
& SLJIT_MEM
) {
1703 emit_fop_mem(compiler
, mem_flags
, TMP_FREG1
, src1
, src1w
);
1707 if (src2
& SLJIT_MEM
) {
1708 emit_fop_mem(compiler
, mem_flags
, TMP_FREG2
, src2
, src2w
);
1712 return push_inst(compiler
, (FCMP
^ inv_bits
) | VN(src1
) | VM(src2
));
1715 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_fop1(struct sljit_compiler
*compiler
, sljit_s32 op
,
1716 sljit_s32 dst
, sljit_sw dstw
,
1717 sljit_s32 src
, sljit_sw srcw
)
1719 sljit_s32 dst_r
, mem_flags
= (op
& SLJIT_F32_OP
) ? INT_SIZE
: WORD_SIZE
;
1723 compiler
->cache_arg
= 0;
1724 compiler
->cache_argw
= 0;
1726 SLJIT_COMPILE_ASSERT((INT_SIZE
^ 0x100) == WORD_SIZE
, must_be_one_bit_difference
);
1727 SELECT_FOP1_OPERATION_WITH_CHECKS(compiler
, op
, dst
, dstw
, src
, srcw
);
1729 inv_bits
= (op
& SLJIT_F32_OP
) ? (1 << 22) : 0;
1730 dst_r
= FAST_IS_REG(dst
) ? dst
: TMP_FREG1
;
1732 if (src
& SLJIT_MEM
) {
1733 emit_fop_mem(compiler
, (GET_OPCODE(op
) == SLJIT_CONV_F64_FROM_F32
) ? (mem_flags
^ 0x100) : mem_flags
, dst_r
, src
, srcw
);
1737 switch (GET_OPCODE(op
)) {
1740 if (dst_r
!= TMP_FREG1
)
1741 FAIL_IF(push_inst(compiler
, (FMOV
^ inv_bits
) | VD(dst_r
) | VN(src
)));
1747 FAIL_IF(push_inst(compiler
, (FNEG
^ inv_bits
) | VD(dst_r
) | VN(src
)));
1750 FAIL_IF(push_inst(compiler
, (FABS
^ inv_bits
) | VD(dst_r
) | VN(src
)));
1752 case SLJIT_CONV_F64_FROM_F32
:
1753 FAIL_IF(push_inst(compiler
, FCVT
| ((op
& SLJIT_F32_OP
) ? (1 << 22) : (1 << 15)) | VD(dst_r
) | VN(src
)));
1757 if (dst
& SLJIT_MEM
)
1758 return emit_fop_mem(compiler
, mem_flags
| STORE
, dst_r
, dst
, dstw
);
1759 return SLJIT_SUCCESS
;
1762 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_fop2(struct sljit_compiler
*compiler
, sljit_s32 op
,
1763 sljit_s32 dst
, sljit_sw dstw
,
1764 sljit_s32 src1
, sljit_sw src1w
,
1765 sljit_s32 src2
, sljit_sw src2w
)
1767 sljit_s32 dst_r
, mem_flags
= (op
& SLJIT_F32_OP
) ? INT_SIZE
: WORD_SIZE
;
1768 sljit_ins inv_bits
= (op
& SLJIT_F32_OP
) ? (1 << 22) : 0;
1771 CHECK(check_sljit_emit_fop2(compiler
, op
, dst
, dstw
, src1
, src1w
, src2
, src2w
));
1772 ADJUST_LOCAL_OFFSET(dst
, dstw
);
1773 ADJUST_LOCAL_OFFSET(src1
, src1w
);
1774 ADJUST_LOCAL_OFFSET(src2
, src2w
);
1776 compiler
->cache_arg
= 0;
1777 compiler
->cache_argw
= 0;
1779 dst_r
= FAST_IS_REG(dst
) ? dst
: TMP_FREG1
;
1780 if (src1
& SLJIT_MEM
) {
1781 emit_fop_mem(compiler
, mem_flags
, TMP_FREG1
, src1
, src1w
);
1784 if (src2
& SLJIT_MEM
) {
1785 emit_fop_mem(compiler
, mem_flags
, TMP_FREG2
, src2
, src2w
);
1789 switch (GET_OPCODE(op
)) {
1791 FAIL_IF(push_inst(compiler
, (FADD
^ inv_bits
) | VD(dst_r
) | VN(src1
) | VM(src2
)));
1794 FAIL_IF(push_inst(compiler
, (FSUB
^ inv_bits
) | VD(dst_r
) | VN(src1
) | VM(src2
)));
1797 FAIL_IF(push_inst(compiler
, (FMUL
^ inv_bits
) | VD(dst_r
) | VN(src1
) | VM(src2
)));
1800 FAIL_IF(push_inst(compiler
, (FDIV
^ inv_bits
) | VD(dst_r
) | VN(src1
) | VM(src2
)));
1804 if (!(dst
& SLJIT_MEM
))
1805 return SLJIT_SUCCESS
;
1806 return emit_fop_mem(compiler
, mem_flags
| STORE
, TMP_FREG1
, dst
, dstw
);
1809 /* --------------------------------------------------------------------- */
1810 /* Other instructions */
1811 /* --------------------------------------------------------------------- */
1813 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_fast_enter(struct sljit_compiler
*compiler
, sljit_s32 dst
, sljit_sw dstw
)
1816 CHECK(check_sljit_emit_fast_enter(compiler
, dst
, dstw
));
1817 ADJUST_LOCAL_OFFSET(dst
, dstw
);
1819 if (FAST_IS_REG(dst
))
1820 return push_inst(compiler
, ORR
| RD(dst
) | RN(TMP_ZERO
) | RM(TMP_LR
));
1823 return emit_op_mem(compiler
, WORD_SIZE
| STORE
, TMP_LR
, dst
, dstw
);
1826 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_fast_return(struct sljit_compiler
*compiler
, sljit_s32 src
, sljit_sw srcw
)
1829 CHECK(check_sljit_emit_fast_return(compiler
, src
, srcw
));
1830 ADJUST_LOCAL_OFFSET(src
, srcw
);
1832 if (FAST_IS_REG(src
))
1833 FAIL_IF(push_inst(compiler
, ORR
| RD(TMP_LR
) | RN(TMP_ZERO
) | RM(src
)));
1834 else if (src
& SLJIT_MEM
)
1835 FAIL_IF(emit_op_mem(compiler
, WORD_SIZE
, TMP_LR
, src
, srcw
));
1836 else if (src
& SLJIT_IMM
)
1837 FAIL_IF(load_immediate(compiler
, TMP_LR
, srcw
));
1839 return push_inst(compiler
, RET
| RN(TMP_LR
));
1842 /* --------------------------------------------------------------------- */
1843 /* Conditional instructions */
1844 /* --------------------------------------------------------------------- */
1846 static sljit_uw
get_cc(sljit_s32 type
)
1850 case SLJIT_MUL_NOT_OVERFLOW
:
1851 case SLJIT_EQUAL_F64
:
1854 case SLJIT_NOT_EQUAL
:
1855 case SLJIT_MUL_OVERFLOW
:
1856 case SLJIT_NOT_EQUAL_F64
:
1860 case SLJIT_LESS_F64
:
1863 case SLJIT_GREATER_EQUAL
:
1864 case SLJIT_GREATER_EQUAL_F64
:
1868 case SLJIT_GREATER_F64
:
1871 case SLJIT_LESS_EQUAL
:
1872 case SLJIT_LESS_EQUAL_F64
:
1875 case SLJIT_SIG_LESS
:
1878 case SLJIT_SIG_GREATER_EQUAL
:
1881 case SLJIT_SIG_GREATER
:
1884 case SLJIT_SIG_LESS_EQUAL
:
1887 case SLJIT_OVERFLOW
:
1888 case SLJIT_UNORDERED_F64
:
1891 case SLJIT_NOT_OVERFLOW
:
1892 case SLJIT_ORDERED_F64
:
1896 SLJIT_UNREACHABLE();
1901 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_label
* sljit_emit_label(struct sljit_compiler
*compiler
)
1903 struct sljit_label
*label
;
1906 CHECK_PTR(check_sljit_emit_label(compiler
));
1908 if (compiler
->last_label
&& compiler
->last_label
->size
== compiler
->size
)
1909 return compiler
->last_label
;
1911 label
= (struct sljit_label
*)ensure_abuf(compiler
, sizeof(struct sljit_label
));
1912 PTR_FAIL_IF(!label
);
1913 set_label(label
, compiler
);
1917 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_jump
* sljit_emit_jump(struct sljit_compiler
*compiler
, sljit_s32 type
)
1919 struct sljit_jump
*jump
;
1922 CHECK_PTR(check_sljit_emit_jump(compiler
, type
));
1924 jump
= (struct sljit_jump
*)ensure_abuf(compiler
, sizeof(struct sljit_jump
));
1926 set_jump(jump
, compiler
, type
& SLJIT_REWRITABLE_JUMP
);
1929 if (type
< SLJIT_JUMP
) {
1930 jump
->flags
|= IS_COND
;
1931 PTR_FAIL_IF(push_inst(compiler
, B_CC
| (6 << 5) | get_cc(type
)));
1933 else if (type
>= SLJIT_FAST_CALL
)
1934 jump
->flags
|= IS_BL
;
1936 PTR_FAIL_IF(emit_imm64_const(compiler
, TMP_REG1
, 0));
1937 jump
->addr
= compiler
->size
;
1938 PTR_FAIL_IF(push_inst(compiler
, ((type
>= SLJIT_FAST_CALL
) ? BLR
: BR
) | RN(TMP_REG1
)));
1943 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_jump
* sljit_emit_call(struct sljit_compiler
*compiler
, sljit_s32 type
,
1944 sljit_s32 arg_types
)
1947 CHECK_PTR(check_sljit_emit_call(compiler
, type
, arg_types
));
1949 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
1950 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
1951 compiler
->skip_checks
= 1;
1954 return sljit_emit_jump(compiler
, type
);
1957 static SLJIT_INLINE
struct sljit_jump
* emit_cmp_to0(struct sljit_compiler
*compiler
, sljit_s32 type
,
1958 sljit_s32 src
, sljit_sw srcw
)
1960 struct sljit_jump
*jump
;
1961 sljit_ins inv_bits
= (type
& SLJIT_I32_OP
) ? (1 << 31) : 0;
1963 SLJIT_ASSERT((type
& 0xff) == SLJIT_EQUAL
|| (type
& 0xff) == SLJIT_NOT_EQUAL
);
1964 ADJUST_LOCAL_OFFSET(src
, srcw
);
1966 jump
= (struct sljit_jump
*)ensure_abuf(compiler
, sizeof(struct sljit_jump
));
1968 set_jump(jump
, compiler
, type
& SLJIT_REWRITABLE_JUMP
);
1969 jump
->flags
|= IS_CBZ
| IS_COND
;
1971 if (src
& SLJIT_MEM
) {
1972 PTR_FAIL_IF(emit_op_mem(compiler
, inv_bits
? INT_SIZE
: WORD_SIZE
, TMP_REG1
, src
, srcw
));
1975 else if (src
& SLJIT_IMM
) {
1976 PTR_FAIL_IF(load_immediate(compiler
, TMP_REG1
, srcw
));
1979 SLJIT_ASSERT(FAST_IS_REG(src
));
1981 if ((type
& 0xff) == SLJIT_EQUAL
)
1982 inv_bits
|= 1 << 24;
1984 PTR_FAIL_IF(push_inst(compiler
, (CBZ
^ inv_bits
) | (6 << 5) | RT(src
)));
1985 PTR_FAIL_IF(emit_imm64_const(compiler
, TMP_REG1
, 0));
1986 jump
->addr
= compiler
->size
;
1987 PTR_FAIL_IF(push_inst(compiler
, BR
| RN(TMP_REG1
)));
1991 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_ijump(struct sljit_compiler
*compiler
, sljit_s32 type
, sljit_s32 src
, sljit_sw srcw
)
1993 struct sljit_jump
*jump
;
1996 CHECK(check_sljit_emit_ijump(compiler
, type
, src
, srcw
));
1997 ADJUST_LOCAL_OFFSET(src
, srcw
);
1999 if (!(src
& SLJIT_IMM
)) {
2000 if (src
& SLJIT_MEM
) {
2001 FAIL_IF(emit_op_mem(compiler
, WORD_SIZE
, TMP_REG1
, src
, srcw
));
2004 return push_inst(compiler
, ((type
>= SLJIT_FAST_CALL
) ? BLR
: BR
) | RN(src
));
2007 /* These jumps are converted to jump/call instructions when possible. */
2008 jump
= (struct sljit_jump
*)ensure_abuf(compiler
, sizeof(struct sljit_jump
));
2010 set_jump(jump
, compiler
, JUMP_ADDR
| ((type
>= SLJIT_FAST_CALL
) ? IS_BL
: 0));
2011 jump
->u
.target
= srcw
;
2013 FAIL_IF(emit_imm64_const(compiler
, TMP_REG1
, 0));
2014 jump
->addr
= compiler
->size
;
2015 return push_inst(compiler
, ((type
>= SLJIT_FAST_CALL
) ? BLR
: BR
) | RN(TMP_REG1
));
2018 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_icall(struct sljit_compiler
*compiler
, sljit_s32 type
,
2019 sljit_s32 arg_types
,
2020 sljit_s32 src
, sljit_sw srcw
)
2023 CHECK(check_sljit_emit_icall(compiler
, type
, arg_types
, src
, srcw
));
2025 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2026 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2027 compiler
->skip_checks
= 1;
2030 return sljit_emit_ijump(compiler
, type
, src
, srcw
);
2033 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op_flags(struct sljit_compiler
*compiler
, sljit_s32 op
,
2034 sljit_s32 dst
, sljit_sw dstw
,
2037 sljit_s32 dst_r
, src_r
, flags
, mem_flags
;
2041 CHECK(check_sljit_emit_op_flags(compiler
, op
, dst
, dstw
, type
));
2042 ADJUST_LOCAL_OFFSET(dst
, dstw
);
2044 cc
= get_cc(type
& 0xff);
2045 dst_r
= FAST_IS_REG(dst
) ? dst
: TMP_REG1
;
2047 if (GET_OPCODE(op
) < SLJIT_ADD
) {
2048 FAIL_IF(push_inst(compiler
, CSINC
| (cc
<< 12) | RD(dst_r
) | RN(TMP_ZERO
) | RM(TMP_ZERO
)));
2049 if (dst_r
!= TMP_REG1
)
2050 return SLJIT_SUCCESS
;
2051 return emit_op_mem(compiler
, (GET_OPCODE(op
) == SLJIT_MOV
? WORD_SIZE
: INT_SIZE
) | STORE
, TMP_REG1
, dst
, dstw
);
2054 compiler
->cache_arg
= 0;
2055 compiler
->cache_argw
= 0;
2056 flags
= HAS_FLAGS(op
) ? SET_FLAGS
: 0;
2057 mem_flags
= WORD_SIZE
;
2058 if (op
& SLJIT_I32_OP
) {
2060 mem_flags
= INT_SIZE
;
2065 if (dst
& SLJIT_MEM
) {
2066 FAIL_IF(emit_op_mem2(compiler
, mem_flags
, TMP_REG1
, dst
, dstw
, dst
, dstw
));
2070 FAIL_IF(push_inst(compiler
, CSINC
| (cc
<< 12) | RD(TMP_REG2
) | RN(TMP_ZERO
) | RM(TMP_ZERO
)));
2071 emit_op_imm(compiler
, flags
| GET_OPCODE(op
), dst_r
, src_r
, TMP_REG2
);
2073 if (dst
& SLJIT_MEM
)
2074 return emit_op_mem2(compiler
, mem_flags
| STORE
, TMP_REG1
, dst
, dstw
, 0, 0);
2075 return SLJIT_SUCCESS
;
2078 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_cmov(struct sljit_compiler
*compiler
, sljit_s32 type
,
2080 sljit_s32 src
, sljit_sw srcw
)
2082 sljit_ins inv_bits
= (dst_reg
& SLJIT_I32_OP
) ? (1 << 31) : 0;
2086 CHECK(check_sljit_emit_cmov(compiler
, type
, dst_reg
, src
, srcw
));
2088 if (SLJIT_UNLIKELY(src
& SLJIT_IMM
)) {
2089 if (dst_reg
& SLJIT_I32_OP
)
2090 srcw
= (sljit_s32
)srcw
;
2091 FAIL_IF(load_immediate(compiler
, TMP_REG1
, srcw
));
2096 cc
= get_cc(type
& 0xff);
2097 dst_reg
&= ~SLJIT_I32_OP
;
2099 return push_inst(compiler
, (CSEL
^ inv_bits
) | (cc
<< 12) | RD(dst_reg
) | RN(dst_reg
) | RM(src
));
2102 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_const
* sljit_emit_const(struct sljit_compiler
*compiler
, sljit_s32 dst
, sljit_sw dstw
, sljit_sw init_value
)
2104 struct sljit_const
*const_
;
2108 CHECK_PTR(check_sljit_emit_const(compiler
, dst
, dstw
, init_value
));
2109 ADJUST_LOCAL_OFFSET(dst
, dstw
);
2111 const_
= (struct sljit_const
*)ensure_abuf(compiler
, sizeof(struct sljit_const
));
2112 PTR_FAIL_IF(!const_
);
2113 set_const(const_
, compiler
);
2115 dst_r
= FAST_IS_REG(dst
) ? dst
: TMP_REG1
;
2116 PTR_FAIL_IF(emit_imm64_const(compiler
, dst_r
, init_value
));
2118 if (dst
& SLJIT_MEM
)
2119 PTR_FAIL_IF(emit_op_mem(compiler
, WORD_SIZE
| STORE
, dst_r
, dst
, dstw
));
2123 SLJIT_API_FUNC_ATTRIBUTE
void sljit_set_jump_addr(sljit_uw addr
, sljit_uw new_target
, sljit_sw executable_offset
)
2125 sljit_ins
* inst
= (sljit_ins
*)addr
;
2126 modify_imm64_const(inst
, new_target
);
2127 inst
= (sljit_ins
*)SLJIT_ADD_EXEC_OFFSET(inst
, executable_offset
);
2128 SLJIT_CACHE_FLUSH(inst
, inst
+ 4);
2131 SLJIT_API_FUNC_ATTRIBUTE
void sljit_set_const(sljit_uw addr
, sljit_sw new_constant
, sljit_sw executable_offset
)
2133 sljit_ins
* inst
= (sljit_ins
*)addr
;
2134 modify_imm64_const(inst
, new_constant
);
2135 inst
= (sljit_ins
*)SLJIT_ADD_EXEC_OFFSET(inst
, executable_offset
);
2136 SLJIT_CACHE_FLUSH(inst
, inst
+ 4);