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)
44 #define TMP_FREG2 (SLJIT_NUMBER_OF_FLOAT_REGISTERS + 1)
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 #define W_OP (1 << 31)
51 #define RD(rd) (reg_map[rd])
52 #define RT(rt) (reg_map[rt])
53 #define RN(rn) (reg_map[rn] << 5)
54 #define RT2(rt2) (reg_map[rt2] << 10)
55 #define RM(rm) (reg_map[rm] << 16)
58 #define VN(vn) ((vn) << 5)
59 #define VM(vm) ((vm) << 16)
61 /* --------------------------------------------------------------------- */
62 /* Instrucion forms */
63 /* --------------------------------------------------------------------- */
65 #define ADC 0x9a000000
66 #define ADD 0x8b000000
67 #define ADDI 0x91000000
68 #define AND 0x8a000000
69 #define ANDI 0x92000000
70 #define ASRV 0x9ac02800
72 #define B_CC 0x54000000
74 #define BLR 0xd63f0000
76 #define BRK 0xd4200000
77 #define CBZ 0xb4000000
78 #define CLZ 0xdac01000
79 #define CSEL 0x9a800000
80 #define CSINC 0x9a800400
81 #define EOR 0xca000000
82 #define EORI 0xd2000000
83 #define FABS 0x1e60c000
84 #define FADD 0x1e602800
85 #define FCMP 0x1e602000
86 #define FCVT 0x1e224000
87 #define FCVTZS 0x9e780000
88 #define FDIV 0x1e601800
89 #define FMOV 0x1e604000
90 #define FMUL 0x1e600800
91 #define FNEG 0x1e614000
92 #define FSUB 0x1e603800
93 #define LDRI 0xf9400000
94 #define LDP 0xa9400000
95 #define LDP_PST 0xa8c00000
96 #define LSLV 0x9ac02000
97 #define LSRV 0x9ac02400
98 #define MADD 0x9b000000
99 #define MOVK 0xf2800000
100 #define MOVN 0x92800000
101 #define MOVZ 0xd2800000
102 #define NOP 0xd503201f
103 #define ORN 0xaa200000
104 #define ORR 0xaa000000
105 #define ORRI 0xb2000000
106 #define RET 0xd65f0000
107 #define SBC 0xda000000
108 #define SBFM 0x93000000
109 #define SCVTF 0x9e620000
110 #define SDIV 0x9ac00c00
111 #define SMADDL 0x9b200000
112 #define SMULH 0x9b403c00
113 #define STP 0xa9000000
114 #define STP_PRE 0xa9800000
115 #define STRI 0xf9000000
116 #define STR_FI 0x3d000000
117 #define STR_FR 0x3c206800
118 #define STUR_FI 0x3c000000
119 #define SUB 0xcb000000
120 #define SUBI 0xd1000000
121 #define SUBS 0xeb000000
122 #define UBFM 0xd3000000
123 #define UDIV 0x9ac00800
124 #define UMULH 0x9bc03c00
126 /* dest_reg is the absolute name of the register
127 Useful for reordering instructions in the delay slot. */
128 static sljit_s32
push_inst(struct sljit_compiler
*compiler
, sljit_ins ins
)
130 sljit_ins
*ptr
= (sljit_ins
*)ensure_buf(compiler
, sizeof(sljit_ins
));
134 return SLJIT_SUCCESS
;
137 static SLJIT_INLINE sljit_s32
emit_imm64_const(struct sljit_compiler
*compiler
, sljit_s32 dst
, sljit_uw imm
)
139 FAIL_IF(push_inst(compiler
, MOVZ
| RD(dst
) | ((imm
& 0xffff) << 5)));
140 FAIL_IF(push_inst(compiler
, MOVK
| RD(dst
) | (((imm
>> 16) & 0xffff) << 5) | (1 << 21)));
141 FAIL_IF(push_inst(compiler
, MOVK
| RD(dst
) | (((imm
>> 32) & 0xffff) << 5) | (2 << 21)));
142 return push_inst(compiler
, MOVK
| RD(dst
) | ((imm
>> 48) << 5) | (3 << 21));
145 static SLJIT_INLINE
void modify_imm64_const(sljit_ins
* inst
, sljit_uw new_imm
)
147 sljit_s32 dst
= inst
[0] & 0x1f;
148 SLJIT_ASSERT((inst
[0] & 0xffe00000) == MOVZ
&& (inst
[1] & 0xffe00000) == (MOVK
| (1 << 21)));
149 inst
[0] = MOVZ
| dst
| ((new_imm
& 0xffff) << 5);
150 inst
[1] = MOVK
| dst
| (((new_imm
>> 16) & 0xffff) << 5) | (1 << 21);
151 inst
[2] = MOVK
| dst
| (((new_imm
>> 32) & 0xffff) << 5) | (2 << 21);
152 inst
[3] = MOVK
| dst
| ((new_imm
>> 48) << 5) | (3 << 21);
155 static SLJIT_INLINE sljit_s32
detect_jump_type(struct sljit_jump
*jump
, sljit_ins
*code_ptr
, sljit_ins
*code
, sljit_sw executable_offset
)
158 sljit_uw target_addr
;
160 if (jump
->flags
& SLJIT_REWRITABLE_JUMP
) {
161 jump
->flags
|= PATCH_ABS64
;
165 if (jump
->flags
& JUMP_ADDR
)
166 target_addr
= jump
->u
.target
;
168 SLJIT_ASSERT(jump
->flags
& JUMP_LABEL
);
169 target_addr
= (sljit_uw
)(code
+ jump
->u
.label
->size
) + (sljit_uw
)executable_offset
;
172 diff
= (sljit_sw
)target_addr
- (sljit_sw
)(code_ptr
+ 4) - executable_offset
;
174 if (jump
->flags
& IS_COND
) {
175 diff
+= sizeof(sljit_ins
);
176 if (diff
<= 0xfffff && diff
>= -0x100000) {
177 code_ptr
[-5] ^= (jump
->flags
& IS_CBZ
) ? (0x1 << 24) : 0x1;
178 jump
->addr
-= sizeof(sljit_ins
);
179 jump
->flags
|= PATCH_COND
;
182 diff
-= sizeof(sljit_ins
);
185 if (diff
<= 0x7ffffff && diff
>= -0x8000000) {
186 jump
->flags
|= PATCH_B
;
190 if (target_addr
<= 0xffffffffl
) {
191 if (jump
->flags
& IS_COND
)
192 code_ptr
[-5] -= (2 << 5);
193 code_ptr
[-2] = code_ptr
[0];
196 if (target_addr
<= 0xffffffffffffl
) {
197 if (jump
->flags
& IS_COND
)
198 code_ptr
[-5] -= (1 << 5);
199 jump
->flags
|= PATCH_ABS48
;
200 code_ptr
[-1] = code_ptr
[0];
204 jump
->flags
|= PATCH_ABS64
;
208 SLJIT_API_FUNC_ATTRIBUTE
void* sljit_generate_code(struct sljit_compiler
*compiler
)
210 struct sljit_memory_fragment
*buf
;
216 sljit_sw executable_offset
;
220 struct sljit_label
*label
;
221 struct sljit_jump
*jump
;
222 struct sljit_const
*const_
;
225 CHECK_PTR(check_sljit_generate_code(compiler
));
226 reverse_buf(compiler
);
228 code
= (sljit_ins
*)SLJIT_MALLOC_EXEC(compiler
->size
* sizeof(sljit_ins
));
229 PTR_FAIL_WITH_EXEC_IF(code
);
234 executable_offset
= SLJIT_EXEC_OFFSET(code
);
236 label
= compiler
->labels
;
237 jump
= compiler
->jumps
;
238 const_
= compiler
->consts
;
241 buf_ptr
= (sljit_ins
*)buf
->memory
;
242 buf_end
= buf_ptr
+ (buf
->used_size
>> 2);
244 *code_ptr
= *buf_ptr
++;
245 /* These structures are ordered by their address. */
246 SLJIT_ASSERT(!label
|| label
->size
>= word_count
);
247 SLJIT_ASSERT(!jump
|| jump
->addr
>= word_count
);
248 SLJIT_ASSERT(!const_
|| const_
->addr
>= word_count
);
249 if (label
&& label
->size
== word_count
) {
250 label
->addr
= (sljit_uw
)SLJIT_ADD_EXEC_OFFSET(code_ptr
, executable_offset
);
251 label
->size
= code_ptr
- code
;
254 if (jump
&& jump
->addr
== word_count
) {
255 jump
->addr
= (sljit_uw
)(code_ptr
- 4);
256 code_ptr
-= detect_jump_type(jump
, code_ptr
, code
, executable_offset
);
259 if (const_
&& const_
->addr
== word_count
) {
260 const_
->addr
= (sljit_uw
)code_ptr
;
261 const_
= const_
->next
;
265 } while (buf_ptr
< buf_end
);
270 if (label
&& label
->size
== word_count
) {
271 label
->addr
= (sljit_uw
)SLJIT_ADD_EXEC_OFFSET(code_ptr
, executable_offset
);
272 label
->size
= code_ptr
- code
;
276 SLJIT_ASSERT(!label
);
278 SLJIT_ASSERT(!const_
);
279 SLJIT_ASSERT(code_ptr
- code
<= (sljit_sw
)compiler
->size
);
281 jump
= compiler
->jumps
;
284 addr
= (jump
->flags
& JUMP_LABEL
) ? jump
->u
.label
->addr
: jump
->u
.target
;
285 buf_ptr
= (sljit_ins
*)jump
->addr
;
287 if (jump
->flags
& PATCH_B
) {
288 addr
= (sljit_sw
)(addr
- (sljit_uw
)SLJIT_ADD_EXEC_OFFSET(buf_ptr
, executable_offset
)) >> 2;
289 SLJIT_ASSERT((sljit_sw
)addr
<= 0x1ffffff && (sljit_sw
)addr
>= -0x2000000);
290 buf_ptr
[0] = ((jump
->flags
& IS_BL
) ? BL
: B
) | (addr
& 0x3ffffff);
291 if (jump
->flags
& IS_COND
)
292 buf_ptr
[-1] -= (4 << 5);
295 if (jump
->flags
& PATCH_COND
) {
296 addr
= (sljit_sw
)(addr
- (sljit_uw
)SLJIT_ADD_EXEC_OFFSET(buf_ptr
, executable_offset
)) >> 2;
297 SLJIT_ASSERT((sljit_sw
)addr
<= 0x3ffff && (sljit_sw
)addr
>= -0x40000);
298 buf_ptr
[0] = (buf_ptr
[0] & ~0xffffe0) | ((addr
& 0x7ffff) << 5);
302 SLJIT_ASSERT((jump
->flags
& (PATCH_ABS48
| PATCH_ABS64
)) || addr
<= 0xffffffffl
);
303 SLJIT_ASSERT((jump
->flags
& PATCH_ABS64
) || addr
<= 0xffffffffffffl
);
305 dst
= buf_ptr
[0] & 0x1f;
306 buf_ptr
[0] = MOVZ
| dst
| ((addr
& 0xffff) << 5);
307 buf_ptr
[1] = MOVK
| dst
| (((addr
>> 16) & 0xffff) << 5) | (1 << 21);
308 if (jump
->flags
& (PATCH_ABS48
| PATCH_ABS64
))
309 buf_ptr
[2] = MOVK
| dst
| (((addr
>> 32) & 0xffff) << 5) | (2 << 21);
310 if (jump
->flags
& PATCH_ABS64
)
311 buf_ptr
[3] = MOVK
| dst
| (((addr
>> 48) & 0xffff) << 5) | (3 << 21);
316 compiler
->error
= SLJIT_ERR_COMPILED
;
317 compiler
->executable_offset
= executable_offset
;
318 compiler
->executable_size
= (code_ptr
- code
) * sizeof(sljit_ins
);
320 code
= (sljit_ins
*)SLJIT_ADD_EXEC_OFFSET(code
, executable_offset
);
321 code_ptr
= (sljit_ins
*)SLJIT_ADD_EXEC_OFFSET(code_ptr
, executable_offset
);
323 SLJIT_CACHE_FLUSH(code
, code_ptr
);
327 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_has_cpu_feature(sljit_s32 feature_type
)
329 switch (feature_type
) {
331 #ifdef SLJIT_IS_FPU_AVAILABLE
332 return SLJIT_IS_FPU_AVAILABLE
;
334 /* Available by default. */
338 case SLJIT_HAS_PRE_UPDATE
:
348 /* --------------------------------------------------------------------- */
349 /* Core code generator functions. */
350 /* --------------------------------------------------------------------- */
352 #define COUNT_TRAILING_ZERO(value, result) \
354 if (!(value & 0xffffffff)) { \
358 if (!(value & 0xffff)) { \
362 if (!(value & 0xff)) { \
366 if (!(value & 0xf)) { \
370 if (!(value & 0x3)) { \
374 if (!(value & 0x1)) { \
379 #define LOGICAL_IMM_CHECK 0x100
381 static sljit_ins
logical_imm(sljit_sw imm
, sljit_s32 len
)
383 sljit_s32 negated
, ones
, right
;
387 if (len
& LOGICAL_IMM_CHECK
) {
388 len
&= ~LOGICAL_IMM_CHECK
;
389 if (len
== 32 && (imm
== 0 || imm
== -1))
391 if (len
== 16 && ((sljit_s32
)imm
== 0 || (sljit_s32
)imm
== -1))
395 SLJIT_ASSERT((len
== 32 && imm
!= 0 && imm
!= -1)
396 || (len
== 16 && (sljit_s32
)imm
!= 0 && (sljit_s32
)imm
!= -1));
397 uimm
= (sljit_uw
)imm
;
403 mask
= ((sljit_uw
)1 << len
) - 1;
404 if ((uimm
& mask
) != ((uimm
>> len
) & mask
))
418 uimm
&= ((sljit_uw
)1 << len
) - 1;
420 /* Unsigned right shift. */
421 COUNT_TRAILING_ZERO(uimm
, right
);
423 /* Signed shift. We also know that the highest bit is set. */
424 imm
= (sljit_sw
)~uimm
;
425 SLJIT_ASSERT(imm
< 0);
427 COUNT_TRAILING_ZERO(imm
, ones
);
435 ins
= (0x3f - ((len
<< 1) - 1)) << 10;
438 return ins
| ((len
- ones
- 1) << 10) | ((len
- ones
- right
) << 16);
440 return ins
| ((ones
- 1) << 10) | ((len
- right
) << 16);
443 #undef COUNT_TRAILING_ZERO
445 static sljit_s32
load_immediate(struct sljit_compiler
*compiler
, sljit_s32 dst
, sljit_sw simm
)
447 sljit_uw imm
= (sljit_uw
)simm
;
448 sljit_s32 i
, zeros
, ones
, first
;
452 return push_inst(compiler
, MOVZ
| RD(dst
) | (imm
<< 5));
454 if (simm
>= -0x10000 && simm
< 0)
455 return push_inst(compiler
, MOVN
| RD(dst
) | ((~imm
& 0xffff) << 5));
457 if (imm
<= 0xffffffffl
) {
458 if ((imm
& 0xffff0000l
) == 0xffff0000)
459 return push_inst(compiler
, (MOVN
^ W_OP
) | RD(dst
) | ((~imm
& 0xffff) << 5));
460 if ((imm
& 0xffff) == 0xffff)
461 return push_inst(compiler
, (MOVN
^ W_OP
) | RD(dst
) | ((~imm
& 0xffff0000l
) >> (16 - 5)) | (1 << 21));
462 bitmask
= logical_imm(simm
, 16);
464 return push_inst(compiler
, (ORRI
^ W_OP
) | RD(dst
) | RN(TMP_ZERO
) | bitmask
);
467 bitmask
= logical_imm(simm
, 32);
469 return push_inst(compiler
, ORRI
| RD(dst
) | RN(TMP_ZERO
) | bitmask
);
472 if (imm
<= 0xffffffffl
) {
473 FAIL_IF(push_inst(compiler
, MOVZ
| RD(dst
) | ((imm
& 0xffff) << 5)));
474 return push_inst(compiler
, MOVK
| RD(dst
) | ((imm
& 0xffff0000l
) >> (16 - 5)) | (1 << 21));
477 if (simm
>= -0x100000000l
&& simm
< 0) {
478 FAIL_IF(push_inst(compiler
, MOVN
| RD(dst
) | ((~imm
& 0xffff) << 5)));
479 return push_inst(compiler
, MOVK
| RD(dst
) | ((imm
& 0xffff0000l
) >> (16 - 5)) | (1 << 21));
482 /* A large amount of number can be constructed from ORR and MOVx,
483 but computing them is costly. We don't */
487 for (i
= 4; i
> 0; i
--) {
488 if ((simm
& 0xffff) == 0)
490 if ((simm
& 0xffff) == 0xffff)
495 simm
= (sljit_sw
)imm
;
499 for (i
= 0; i
< 4; i
++) {
500 if (!(simm
& 0xffff)) {
506 FAIL_IF(push_inst(compiler
, MOVN
| RD(dst
) | ((simm
& 0xffff) << 5) | (i
<< 21)));
509 FAIL_IF(push_inst(compiler
, MOVK
| RD(dst
) | ((~simm
& 0xffff) << 5) | (i
<< 21)));
512 return SLJIT_SUCCESS
;
515 for (i
= 0; i
< 4; i
++) {
516 if (!(simm
& 0xffff)) {
522 FAIL_IF(push_inst(compiler
, MOVZ
| RD(dst
) | ((simm
& 0xffff) << 5) | (i
<< 21)));
525 FAIL_IF(push_inst(compiler
, MOVK
| RD(dst
) | ((simm
& 0xffff) << 5) | (i
<< 21)));
528 return SLJIT_SUCCESS
;
531 #define ARG1_IMM 0x0010000
532 #define ARG2_IMM 0x0020000
533 #define INT_OP 0x0040000
534 #define SET_FLAGS 0x0080000
535 #define UNUSED_RETURN 0x0100000
536 #define SLOW_DEST 0x0200000
537 #define SLOW_SRC1 0x0400000
538 #define SLOW_SRC2 0x0800000
540 #define CHECK_FLAGS(flag_bits) \
541 if (flags & SET_FLAGS) { \
542 inv_bits |= flag_bits; \
543 if (flags & UNUSED_RETURN) \
547 static sljit_s32
emit_op_imm(struct sljit_compiler
*compiler
, sljit_s32 flags
, sljit_s32 dst
, sljit_sw arg1
, sljit_sw arg2
)
549 /* dst must be register, TMP_REG1
550 arg1 must be register, TMP_REG1, imm
551 arg2 must be register, TMP_REG2, imm */
552 sljit_ins inv_bits
= (flags
& INT_OP
) ? (1 << 31) : 0;
554 sljit_s32 op
= (flags
& 0xffff);
558 if (SLJIT_UNLIKELY((flags
& (ARG1_IMM
| ARG2_IMM
)) == (ARG1_IMM
| ARG2_IMM
))) {
559 /* Both are immediates. */
561 if (arg1
== 0 && op
!= SLJIT_ADD
&& op
!= SLJIT_SUB
)
564 FAIL_IF(load_immediate(compiler
, TMP_REG1
, arg1
));
569 if (flags
& (ARG1_IMM
| ARG2_IMM
)) {
570 reg
= (flags
& ARG2_IMM
) ? arg1
: arg2
;
571 imm
= (flags
& ARG2_IMM
) ? arg2
: arg1
;
579 /* No form with immediate operand (except imm 0, which
580 is represented by a ZERO register). */
583 SLJIT_ASSERT(!(flags
& SET_FLAGS
) && (flags
& ARG2_IMM
) && arg1
== TMP_REG1
);
584 return load_immediate(compiler
, dst
, imm
);
586 SLJIT_ASSERT(flags
& ARG2_IMM
);
587 FAIL_IF(load_immediate(compiler
, dst
, (flags
& INT_OP
) ? (~imm
& 0xffffffff) : ~imm
));
590 if (flags
& ARG1_IMM
)
596 CHECK_FLAGS(1 << 29);
597 return push_inst(compiler
, ((op
== SLJIT_ADD
? ADDI
: SUBI
) ^ inv_bits
) | RD(dst
) | RN(reg
));
599 if (imm
> 0 && imm
<= 0xfff) {
600 CHECK_FLAGS(1 << 29);
601 return push_inst(compiler
, (ADDI
^ inv_bits
) | RD(dst
) | RN(reg
) | (imm
<< 10));
604 if (nimm
> 0 && nimm
<= 0xfff) {
605 CHECK_FLAGS(1 << 29);
606 return push_inst(compiler
, (SUBI
^ inv_bits
) | RD(dst
) | RN(reg
) | (nimm
<< 10));
608 if (imm
> 0 && imm
<= 0xffffff && !(imm
& 0xfff)) {
609 CHECK_FLAGS(1 << 29);
610 return push_inst(compiler
, (ADDI
^ inv_bits
) | RD(dst
) | RN(reg
) | ((imm
>> 12) << 10) | (1 << 22));
612 if (nimm
> 0 && nimm
<= 0xffffff && !(nimm
& 0xfff)) {
613 CHECK_FLAGS(1 << 29);
614 return push_inst(compiler
, (SUBI
^ inv_bits
) | RD(dst
) | RN(reg
) | ((nimm
>> 12) << 10) | (1 << 22));
616 if (imm
> 0 && imm
<= 0xffffff && !(flags
& SET_FLAGS
)) {
617 FAIL_IF(push_inst(compiler
, (ADDI
^ inv_bits
) | RD(dst
) | RN(reg
) | ((imm
>> 12) << 10) | (1 << 22)));
618 return push_inst(compiler
, (ADDI
^ inv_bits
) | RD(dst
) | RN(dst
) | ((imm
& 0xfff) << 10));
620 if (nimm
> 0 && nimm
<= 0xffffff && !(flags
& SET_FLAGS
)) {
621 FAIL_IF(push_inst(compiler
, (SUBI
^ inv_bits
) | RD(dst
) | RN(reg
) | ((nimm
>> 12) << 10) | (1 << 22)));
622 return push_inst(compiler
, (SUBI
^ inv_bits
) | RD(dst
) | RN(dst
) | ((nimm
& 0xfff) << 10));
626 inst_bits
= logical_imm(imm
, LOGICAL_IMM_CHECK
| ((flags
& INT_OP
) ? 16 : 32));
629 CHECK_FLAGS(3 << 29);
630 return push_inst(compiler
, (ANDI
^ inv_bits
) | RD(dst
) | RN(reg
) | inst_bits
);
633 inst_bits
= logical_imm(imm
, LOGICAL_IMM_CHECK
| ((flags
& INT_OP
) ? 16 : 32));
640 FAIL_IF(push_inst(compiler
, (inst_bits
^ inv_bits
) | RD(dst
) | RN(reg
)));
643 if (flags
& ARG1_IMM
)
645 if (flags
& INT_OP
) {
647 FAIL_IF(push_inst(compiler
, (UBFM
^ inv_bits
) | RD(dst
) | RN(arg1
) | ((-imm
& 0x1f) << 16) | ((31 - imm
) << 10)));
651 FAIL_IF(push_inst(compiler
, (UBFM
^ inv_bits
) | RD(dst
) | RN(arg1
) | (1 << 22) | ((-imm
& 0x3f) << 16) | ((63 - imm
) << 10)));
656 if (flags
& ARG1_IMM
)
658 if (op
== SLJIT_ASHR
)
660 if (flags
& INT_OP
) {
662 FAIL_IF(push_inst(compiler
, (UBFM
^ inv_bits
) | RD(dst
) | RN(arg1
) | (imm
<< 16) | (31 << 10)));
666 FAIL_IF(push_inst(compiler
, (UBFM
^ inv_bits
) | RD(dst
) | RN(arg1
) | (1 << 22) | (imm
<< 16) | (63 << 10)));
674 if (flags
& ARG2_IMM
) {
678 FAIL_IF(load_immediate(compiler
, TMP_REG2
, arg2
));
686 FAIL_IF(load_immediate(compiler
, TMP_REG1
, arg1
));
692 /* Both arguments are registers. */
698 SLJIT_ASSERT(!(flags
& SET_FLAGS
) && arg1
== TMP_REG1
);
700 return SLJIT_SUCCESS
;
701 return push_inst(compiler
, ORR
| RD(dst
) | RN(TMP_ZERO
) | RM(arg2
));
704 SLJIT_ASSERT(!(flags
& SET_FLAGS
) && arg1
== TMP_REG1
);
705 return push_inst(compiler
, (UBFM
^ (1 << 31)) | RD(dst
) | RN(arg2
) | (7 << 10));
708 SLJIT_ASSERT(!(flags
& SET_FLAGS
) && arg1
== TMP_REG1
);
709 if (!(flags
& INT_OP
))
711 return push_inst(compiler
, (SBFM
^ inv_bits
) | RD(dst
) | RN(arg2
) | (7 << 10));
714 SLJIT_ASSERT(!(flags
& SET_FLAGS
) && arg1
== TMP_REG1
);
715 return push_inst(compiler
, (UBFM
^ (1 << 31)) | RD(dst
) | RN(arg2
) | (15 << 10));
718 SLJIT_ASSERT(!(flags
& SET_FLAGS
) && arg1
== TMP_REG1
);
719 if (!(flags
& INT_OP
))
721 return push_inst(compiler
, (SBFM
^ inv_bits
) | RD(dst
) | RN(arg2
) | (15 << 10));
724 SLJIT_ASSERT(!(flags
& SET_FLAGS
) && arg1
== TMP_REG1
);
725 if ((flags
& INT_OP
) && dst
== arg2
)
726 return SLJIT_SUCCESS
;
727 return push_inst(compiler
, (ORR
^ (1 << 31)) | RD(dst
) | RN(TMP_ZERO
) | RM(arg2
));
730 SLJIT_ASSERT(!(flags
& SET_FLAGS
) && arg1
== TMP_REG1
);
731 if ((flags
& INT_OP
) && dst
== arg2
)
732 return SLJIT_SUCCESS
;
733 return push_inst(compiler
, SBFM
| (1 << 22) | RD(dst
) | RN(arg2
) | (31 << 10));
735 SLJIT_ASSERT(arg1
== TMP_REG1
);
736 FAIL_IF(push_inst(compiler
, (ORN
^ inv_bits
) | RD(dst
) | RN(TMP_ZERO
) | RM(arg2
)));
737 break; /* Set flags. */
739 SLJIT_ASSERT(arg1
== TMP_REG1
);
740 if (flags
& SET_FLAGS
)
742 return push_inst(compiler
, (SUB
^ inv_bits
) | RD(dst
) | RN(TMP_ZERO
) | RM(arg2
));
744 SLJIT_ASSERT(arg1
== TMP_REG1
);
745 return push_inst(compiler
, (CLZ
^ inv_bits
) | RD(dst
) | RN(arg2
));
747 CHECK_FLAGS(1 << 29);
748 return push_inst(compiler
, (ADD
^ inv_bits
) | RD(dst
) | RN(arg1
) | RM(arg2
));
750 CHECK_FLAGS(1 << 29);
751 return push_inst(compiler
, (ADC
^ inv_bits
) | RD(dst
) | RN(arg1
) | RM(arg2
));
753 CHECK_FLAGS(1 << 29);
754 return push_inst(compiler
, (SUB
^ inv_bits
) | RD(dst
) | RN(arg1
) | RM(arg2
));
756 CHECK_FLAGS(1 << 29);
757 return push_inst(compiler
, (SBC
^ inv_bits
) | RD(dst
) | RN(arg1
) | RM(arg2
));
759 if (!(flags
& SET_FLAGS
))
760 return push_inst(compiler
, (MADD
^ inv_bits
) | RD(dst
) | RN(arg1
) | RM(arg2
) | RT2(TMP_ZERO
));
761 if (flags
& INT_OP
) {
762 FAIL_IF(push_inst(compiler
, SMADDL
| RD(dst
) | RN(arg1
) | RM(arg2
) | (31 << 10)));
763 FAIL_IF(push_inst(compiler
, ADD
| RD(TMP_LR
) | RN(TMP_ZERO
) | RM(dst
) | (2 << 22) | (31 << 10)));
764 return push_inst(compiler
, SUBS
| RD(TMP_ZERO
) | RN(TMP_LR
) | RM(dst
) | (2 << 22) | (63 << 10));
766 FAIL_IF(push_inst(compiler
, SMULH
| RD(TMP_LR
) | RN(arg1
) | RM(arg2
)));
767 FAIL_IF(push_inst(compiler
, MADD
| RD(dst
) | RN(arg1
) | RM(arg2
) | RT2(TMP_ZERO
)));
768 return push_inst(compiler
, SUBS
| RD(TMP_ZERO
) | RN(TMP_LR
) | RM(dst
) | (2 << 22) | (63 << 10));
770 CHECK_FLAGS(3 << 29);
771 return push_inst(compiler
, (AND
^ inv_bits
) | RD(dst
) | RN(arg1
) | RM(arg2
));
773 FAIL_IF(push_inst(compiler
, (ORR
^ inv_bits
) | RD(dst
) | RN(arg1
) | RM(arg2
)));
774 break; /* Set flags. */
776 FAIL_IF(push_inst(compiler
, (EOR
^ inv_bits
) | RD(dst
) | RN(arg1
) | RM(arg2
)));
777 break; /* Set flags. */
779 FAIL_IF(push_inst(compiler
, (LSLV
^ inv_bits
) | RD(dst
) | RN(arg1
) | RM(arg2
)));
780 break; /* Set flags. */
782 FAIL_IF(push_inst(compiler
, (LSRV
^ inv_bits
) | RD(dst
) | RN(arg1
) | RM(arg2
)));
783 break; /* Set flags. */
785 FAIL_IF(push_inst(compiler
, (ASRV
^ inv_bits
) | RD(dst
) | RN(arg1
) | RM(arg2
)));
786 break; /* Set flags. */
789 return SLJIT_SUCCESS
;
793 if (flags
& SET_FLAGS
)
794 return push_inst(compiler
, (SUBS
^ inv_bits
) | RD(TMP_ZERO
) | RN(dst
) | RM(TMP_ZERO
));
795 return SLJIT_SUCCESS
;
802 #define ARG_TEST 0x08
804 #define BYTE_SIZE 0x000
805 #define HALF_SIZE 0x100
806 #define INT_SIZE 0x200
807 #define WORD_SIZE 0x300
809 #define MEM_SIZE_SHIFT(flags) ((flags) >> 8)
811 static const sljit_ins sljit_mem_imm
[4] = {
812 /* u l */ 0x39400000 /* ldrb [reg,imm] */,
813 /* u s */ 0x39000000 /* strb [reg,imm] */,
814 /* s l */ 0x39800000 /* ldrsb [reg,imm] */,
815 /* s s */ 0x39000000 /* strb [reg,imm] */,
818 static const sljit_ins sljit_mem_simm
[4] = {
819 /* u l */ 0x38400000 /* ldurb [reg,imm] */,
820 /* u s */ 0x38000000 /* sturb [reg,imm] */,
821 /* s l */ 0x38800000 /* ldursb [reg,imm] */,
822 /* s s */ 0x38000000 /* sturb [reg,imm] */,
825 static const sljit_ins sljit_mem_pre_simm
[4] = {
826 /* u l */ 0x38400c00 /* ldrb [reg,imm]! */,
827 /* u s */ 0x38000c00 /* strb [reg,imm]! */,
828 /* s l */ 0x38800c00 /* ldrsb [reg,imm]! */,
829 /* s s */ 0x38000c00 /* strb [reg,imm]! */,
832 static const sljit_ins sljit_mem_reg
[4] = {
833 /* u l */ 0x38606800 /* ldrb [reg,reg] */,
834 /* u s */ 0x38206800 /* strb [reg,reg] */,
835 /* s l */ 0x38a06800 /* ldrsb [reg,reg] */,
836 /* s s */ 0x38206800 /* strb [reg,reg] */,
839 /* Helper function. Dst should be reg + value, using at most 1 instruction, flags does not set. */
840 static sljit_s32
emit_set_delta(struct sljit_compiler
*compiler
, sljit_s32 dst
, sljit_s32 reg
, sljit_sw value
)
844 return push_inst(compiler
, ADDI
| RD(dst
) | RN(reg
) | (value
<< 10));
845 if (value
<= 0xffffff && !(value
& 0xfff))
846 return push_inst(compiler
, ADDI
| (1 << 22) | RD(dst
) | RN(reg
) | (value
>> 2));
851 return push_inst(compiler
, SUBI
| RD(dst
) | RN(reg
) | (value
<< 10));
852 if (value
<= 0xffffff && !(value
& 0xfff))
853 return push_inst(compiler
, SUBI
| (1 << 22) | RD(dst
) | RN(reg
) | (value
>> 2));
855 return SLJIT_ERR_UNSUPPORTED
;
858 /* Can perform an operation using at most 1 instruction. */
859 static sljit_s32
getput_arg_fast(struct sljit_compiler
*compiler
, sljit_s32 flags
, sljit_s32 reg
, sljit_s32 arg
, sljit_sw argw
)
861 sljit_u32 shift
= MEM_SIZE_SHIFT(flags
);
863 SLJIT_ASSERT(arg
& SLJIT_MEM
);
865 if (SLJIT_UNLIKELY(flags
& UPDATE
)) {
866 if ((arg
& REG_MASK
) && !(arg
& OFFS_REG_MASK
) && argw
<= 255 && argw
>= -256) {
867 if (SLJIT_UNLIKELY(flags
& ARG_TEST
))
872 FAIL_IF(push_inst(compiler
, sljit_mem_pre_simm
[flags
& 0x3]
873 | (shift
<< 30) | RT(reg
) | RN(arg
) | (argw
<< 12)));
879 if (SLJIT_UNLIKELY(arg
& OFFS_REG_MASK
)) {
881 if (argw
&& argw
!= shift
)
884 if (SLJIT_UNLIKELY(flags
& ARG_TEST
))
887 FAIL_IF(push_inst(compiler
, sljit_mem_reg
[flags
& 0x3] | (shift
<< 30) | RT(reg
)
888 | RN(arg
& REG_MASK
) | RM(OFFS_REG(arg
)) | (argw
? (1 << 12) : 0)));
894 if (arg
== SLJIT_UNUSED
)
897 if (argw
>= 0 && (argw
>> shift
) <= 0xfff && (argw
& ((1 << shift
) - 1)) == 0) {
898 if (SLJIT_UNLIKELY(flags
& ARG_TEST
))
901 FAIL_IF(push_inst(compiler
, sljit_mem_imm
[flags
& 0x3] | (shift
<< 30)
902 | RT(reg
) | RN(arg
) | (argw
<< (10 - shift
))));
906 if (argw
> 255 || argw
< -256)
909 if (SLJIT_UNLIKELY(flags
& ARG_TEST
))
912 FAIL_IF(push_inst(compiler
, sljit_mem_simm
[flags
& 0x3] | (shift
<< 30)
913 | RT(reg
) | RN(arg
) | ((argw
& 0x1ff) << 12)));
917 /* see getput_arg below.
918 Note: can_cache is called only for binary operators. Those
919 operators always uses word arguments without write back. */
920 static sljit_s32
can_cache(sljit_s32 arg
, sljit_sw argw
, sljit_s32 next_arg
, sljit_sw next_argw
)
923 if ((arg
& OFFS_REG_MASK
) || !(next_arg
& SLJIT_MEM
))
926 if (!(arg
& REG_MASK
)) {
927 diff
= argw
- next_argw
;
928 if (diff
<= 0xfff && diff
>= -0xfff)
933 if (argw
== next_argw
)
936 diff
= argw
- next_argw
;
937 if (arg
== next_arg
&& diff
<= 0xfff && diff
>= -0xfff)
943 /* Emit the necessary instructions. See can_cache above. */
944 static sljit_s32
getput_arg(struct sljit_compiler
*compiler
, sljit_s32 flags
, sljit_s32 reg
,
945 sljit_s32 arg
, sljit_sw argw
, sljit_s32 next_arg
, sljit_sw next_argw
)
947 sljit_u32 shift
= MEM_SIZE_SHIFT(flags
);
948 sljit_s32 tmp_r
, other_r
;
951 SLJIT_ASSERT(arg
& SLJIT_MEM
);
952 if (!(next_arg
& SLJIT_MEM
)) {
957 tmp_r
= ((flags
& STORE
) || (flags
== (WORD_SIZE
| SIGNED
))) ? TMP_REG3
: reg
;
959 if (SLJIT_UNLIKELY((flags
& UPDATE
) && (arg
& REG_MASK
))) {
960 /* Update only applies if a base register exists. */
961 other_r
= OFFS_REG(arg
);
963 other_r
= arg
& REG_MASK
;
964 SLJIT_ASSERT(other_r
!= reg
);
966 if (argw
>= 0 && argw
<= 0xffffff) {
967 if ((argw
& 0xfff) != 0)
968 FAIL_IF(push_inst(compiler
, ADDI
| RD(other_r
) | RN(other_r
) | ((argw
& 0xfff) << 10)));
970 FAIL_IF(push_inst(compiler
, ADDI
| (1 << 22) | RD(other_r
) | RN(other_r
) | ((argw
>> 12) << 10)));
971 return push_inst(compiler
, sljit_mem_imm
[flags
& 0x3] | (shift
<< 30) | RT(reg
) | RN(other_r
));
973 else if (argw
< 0 && argw
>= -0xffffff) {
975 if ((argw
& 0xfff) != 0)
976 FAIL_IF(push_inst(compiler
, SUBI
| RD(other_r
) | RN(other_r
) | ((argw
& 0xfff) << 10)));
978 FAIL_IF(push_inst(compiler
, SUBI
| (1 << 22) | RD(other_r
) | RN(other_r
) | ((argw
>> 12) << 10)));
979 return push_inst(compiler
, sljit_mem_imm
[flags
& 0x3] | (shift
<< 30) | RT(reg
) | RN(other_r
));
982 if (compiler
->cache_arg
== SLJIT_MEM
) {
983 if (argw
== compiler
->cache_argw
) {
987 else if (emit_set_delta(compiler
, TMP_REG3
, TMP_REG3
, argw
- compiler
->cache_argw
) != SLJIT_ERR_UNSUPPORTED
) {
988 FAIL_IF(compiler
->error
);
989 compiler
->cache_argw
= argw
;
996 FAIL_IF(load_immediate(compiler
, TMP_REG3
, argw
));
997 compiler
->cache_arg
= SLJIT_MEM
;
998 compiler
->cache_argw
= argw
;
1004 /* No caching here. */
1006 FAIL_IF(push_inst(compiler
, sljit_mem_reg
[flags
& 0x3] | (shift
<< 30) | RT(reg
) | RN(arg
) | RM(other_r
)));
1007 return push_inst(compiler
, ADD
| RD(arg
) | RN(arg
) | RM(other_r
));
1010 if (arg
& OFFS_REG_MASK
) {
1011 other_r
= OFFS_REG(arg
);
1013 FAIL_IF(push_inst(compiler
, ADD
| RD(tmp_r
) | RN(arg
) | RM(other_r
) | ((argw
& 0x3) << 10)));
1014 return push_inst(compiler
, sljit_mem_imm
[flags
& 0x3] | (shift
<< 30) | RT(reg
) | RN(tmp_r
));
1017 if (compiler
->cache_arg
== arg
) {
1018 diff
= argw
- compiler
->cache_argw
;
1019 if (diff
<= 255 && diff
>= -256)
1020 return push_inst(compiler
, sljit_mem_simm
[flags
& 0x3] | (shift
<< 30)
1021 | RT(reg
) | RN(TMP_REG3
) | ((diff
& 0x1ff) << 12));
1022 if (emit_set_delta(compiler
, TMP_REG3
, TMP_REG3
, diff
) != SLJIT_ERR_UNSUPPORTED
) {
1023 FAIL_IF(compiler
->error
);
1024 return push_inst(compiler
, sljit_mem_imm
[flags
& 0x3] | (shift
<< 30) | RT(reg
) | RN(arg
));
1028 diff
= argw
- next_argw
;
1029 next_arg
= (arg
& REG_MASK
) && (arg
== next_arg
) && diff
<= 0xfff && diff
>= -0xfff && diff
!= 0;
1032 if (arg
!= SLJIT_UNUSED
&& argw
>= 0 && argw
<= 0xffffff && (argw
& ((1 << shift
) - 1)) == 0) {
1033 FAIL_IF(push_inst(compiler
, ADDI
| (1 << 22) | RD(tmp_r
) | RN(arg
) | ((argw
>> 12) << 10)));
1034 return push_inst(compiler
, sljit_mem_imm
[flags
& 0x3] | (shift
<< 30)
1035 | RT(reg
) | RN(tmp_r
) | ((argw
& 0xfff) << (10 - shift
)));
1038 if (arg
&& compiler
->cache_arg
== SLJIT_MEM
) {
1039 if (compiler
->cache_argw
== argw
)
1040 return push_inst(compiler
, sljit_mem_reg
[flags
& 0x3] | (shift
<< 30) | RT(reg
) | RN(arg
) | RM(TMP_REG3
));
1041 if (emit_set_delta(compiler
, TMP_REG3
, TMP_REG3
, argw
- compiler
->cache_argw
) != SLJIT_ERR_UNSUPPORTED
) {
1042 FAIL_IF(compiler
->error
);
1043 compiler
->cache_argw
= argw
;
1044 return push_inst(compiler
, sljit_mem_reg
[flags
& 0x3] | (shift
<< 30) | RT(reg
) | RN(arg
) | RM(TMP_REG3
));
1048 compiler
->cache_argw
= argw
;
1049 if (next_arg
&& emit_set_delta(compiler
, TMP_REG3
, arg
, argw
) != SLJIT_ERR_UNSUPPORTED
) {
1050 FAIL_IF(compiler
->error
);
1051 compiler
->cache_arg
= SLJIT_MEM
| arg
;
1055 FAIL_IF(load_immediate(compiler
, TMP_REG3
, argw
));
1056 compiler
->cache_arg
= SLJIT_MEM
;
1059 FAIL_IF(push_inst(compiler
, ADD
| RD(TMP_REG3
) | RN(TMP_REG3
) | RM(arg
)));
1060 compiler
->cache_arg
= SLJIT_MEM
| arg
;
1066 return push_inst(compiler
, sljit_mem_reg
[flags
& 0x3] | (shift
<< 30) | RT(reg
) | RN(arg
) | RM(TMP_REG3
));
1067 return push_inst(compiler
, sljit_mem_imm
[flags
& 0x3] | (shift
<< 30) | RT(reg
) | RN(TMP_REG3
));
1070 static SLJIT_INLINE sljit_s32
emit_op_mem(struct sljit_compiler
*compiler
, sljit_s32 flags
, sljit_s32 reg
, sljit_s32 arg
, sljit_sw argw
)
1072 if (getput_arg_fast(compiler
, flags
, reg
, arg
, argw
))
1073 return compiler
->error
;
1074 compiler
->cache_arg
= 0;
1075 compiler
->cache_argw
= 0;
1076 return getput_arg(compiler
, flags
, reg
, arg
, argw
, 0, 0);
1079 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
)
1081 if (getput_arg_fast(compiler
, flags
, reg
, arg1
, arg1w
))
1082 return compiler
->error
;
1083 return getput_arg(compiler
, flags
, reg
, arg1
, arg1w
, arg2
, arg2w
);
1086 /* --------------------------------------------------------------------- */
1088 /* --------------------------------------------------------------------- */
1090 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_enter(struct sljit_compiler
*compiler
,
1091 sljit_s32 options
, sljit_s32 args
, sljit_s32 scratches
, sljit_s32 saveds
,
1092 sljit_s32 fscratches
, sljit_s32 fsaveds
, sljit_s32 local_size
)
1094 sljit_s32 i
, tmp
, offs
, prev
, saved_regs_size
;
1097 CHECK(check_sljit_emit_enter(compiler
, options
, args
, scratches
, saveds
, fscratches
, fsaveds
, local_size
));
1098 set_emit_enter(compiler
, options
, args
, scratches
, saveds
, fscratches
, fsaveds
, local_size
);
1100 saved_regs_size
= GET_SAVED_REGISTERS_SIZE(scratches
, saveds
, 0);
1101 local_size
+= saved_regs_size
+ SLJIT_LOCALS_OFFSET
;
1102 local_size
= (local_size
+ 15) & ~0xf;
1103 compiler
->local_size
= local_size
;
1105 if (local_size
<= (63 * sizeof(sljit_sw
))) {
1106 FAIL_IF(push_inst(compiler
, STP_PRE
| 29 | RT2(TMP_LR
)
1107 | RN(TMP_SP
) | ((-(local_size
>> 3) & 0x7f) << 15)));
1108 FAIL_IF(push_inst(compiler
, ADDI
| RD(SLJIT_SP
) | RN(TMP_SP
) | (0 << 10)));
1109 offs
= (local_size
- saved_regs_size
) << (15 - 3);
1112 if (saved_regs_size
& 0x8) {
1114 saved_regs_size
+= sizeof(sljit_sw
);
1116 local_size
-= saved_regs_size
+ SLJIT_LOCALS_OFFSET
;
1117 if (saved_regs_size
> 0)
1118 FAIL_IF(push_inst(compiler
, SUBI
| RD(TMP_SP
) | RN(TMP_SP
) | (saved_regs_size
<< 10)));
1121 tmp
= saveds
< SLJIT_NUMBER_OF_SAVED_REGISTERS
? (SLJIT_S0
+ 1 - saveds
) : SLJIT_FIRST_SAVED_REG
;
1123 for (i
= SLJIT_S0
; i
>= tmp
; i
--) {
1125 if (!(offs
& (1 << 15))) {
1129 FAIL_IF(push_inst(compiler
, STRI
| RT(i
) | RN(TMP_SP
) | (offs
>> 5)));
1133 FAIL_IF(push_inst(compiler
, STP
| RT(prev
) | RT2(i
) | RN(TMP_SP
) | offs
));
1138 for (i
= scratches
; i
>= SLJIT_FIRST_SAVED_REG
; i
--) {
1140 if (!(offs
& (1 << 15))) {
1144 FAIL_IF(push_inst(compiler
, STRI
| RT(i
) | RN(TMP_SP
) | (offs
>> 5)));
1148 FAIL_IF(push_inst(compiler
, STP
| RT(prev
) | RT2(i
) | RN(TMP_SP
) | offs
));
1153 SLJIT_ASSERT(prev
== -1);
1155 if (compiler
->local_size
> (63 * sizeof(sljit_sw
))) {
1156 /* The local_size is already adjusted by the saved registers. */
1157 if (local_size
> 0xfff) {
1158 FAIL_IF(push_inst(compiler
, SUBI
| RD(TMP_SP
) | RN(TMP_SP
) | ((local_size
>> 12) << 10) | (1 << 22)));
1159 local_size
&= 0xfff;
1162 FAIL_IF(push_inst(compiler
, SUBI
| RD(TMP_SP
) | RN(TMP_SP
) | (local_size
<< 10)));
1163 FAIL_IF(push_inst(compiler
, STP_PRE
| 29 | RT2(TMP_LR
)
1164 | RN(TMP_SP
) | ((-(16 >> 3) & 0x7f) << 15)));
1165 FAIL_IF(push_inst(compiler
, ADDI
| RD(SLJIT_SP
) | RN(TMP_SP
) | (0 << 10)));
1169 FAIL_IF(push_inst(compiler
, ORR
| RD(SLJIT_S0
) | RN(TMP_ZERO
) | RM(SLJIT_R0
)));
1171 FAIL_IF(push_inst(compiler
, ORR
| RD(SLJIT_S1
) | RN(TMP_ZERO
) | RM(SLJIT_R1
)));
1173 FAIL_IF(push_inst(compiler
, ORR
| RD(SLJIT_S2
) | RN(TMP_ZERO
) | RM(SLJIT_R2
)));
1175 return SLJIT_SUCCESS
;
1178 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_set_context(struct sljit_compiler
*compiler
,
1179 sljit_s32 options
, sljit_s32 args
, sljit_s32 scratches
, sljit_s32 saveds
,
1180 sljit_s32 fscratches
, sljit_s32 fsaveds
, sljit_s32 local_size
)
1183 CHECK(check_sljit_set_context(compiler
, options
, args
, scratches
, saveds
, fscratches
, fsaveds
, local_size
));
1184 set_set_context(compiler
, options
, args
, scratches
, saveds
, fscratches
, fsaveds
, local_size
);
1186 local_size
+= GET_SAVED_REGISTERS_SIZE(scratches
, saveds
, 0) + SLJIT_LOCALS_OFFSET
;
1187 local_size
= (local_size
+ 15) & ~0xf;
1188 compiler
->local_size
= local_size
;
1189 return SLJIT_SUCCESS
;
1192 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_return(struct sljit_compiler
*compiler
, sljit_s32 op
, sljit_s32 src
, sljit_sw srcw
)
1194 sljit_s32 local_size
;
1195 sljit_s32 i
, tmp
, offs
, prev
, saved_regs_size
;
1198 CHECK(check_sljit_emit_return(compiler
, op
, src
, srcw
));
1200 FAIL_IF(emit_mov_before_return(compiler
, op
, src
, srcw
));
1202 local_size
= compiler
->local_size
;
1204 saved_regs_size
= GET_SAVED_REGISTERS_SIZE(compiler
->scratches
, compiler
->saveds
, 0);
1205 if (local_size
<= (63 * sizeof(sljit_sw
)))
1206 offs
= (local_size
- saved_regs_size
) << (15 - 3);
1208 FAIL_IF(push_inst(compiler
, LDP_PST
| 29 | RT2(TMP_LR
)
1209 | RN(TMP_SP
) | (((16 >> 3) & 0x7f) << 15)));
1211 if (saved_regs_size
& 0x8) {
1213 saved_regs_size
+= sizeof(sljit_sw
);
1215 local_size
-= saved_regs_size
+ SLJIT_LOCALS_OFFSET
;
1216 if (local_size
> 0xfff) {
1217 FAIL_IF(push_inst(compiler
, ADDI
| RD(TMP_SP
) | RN(TMP_SP
) | ((local_size
>> 12) << 10) | (1 << 22)));
1218 local_size
&= 0xfff;
1221 FAIL_IF(push_inst(compiler
, ADDI
| RD(TMP_SP
) | RN(TMP_SP
) | (local_size
<< 10)));
1224 tmp
= compiler
->saveds
< SLJIT_NUMBER_OF_SAVED_REGISTERS
? (SLJIT_S0
+ 1 - compiler
->saveds
) : SLJIT_FIRST_SAVED_REG
;
1226 for (i
= SLJIT_S0
; i
>= tmp
; i
--) {
1228 if (!(offs
& (1 << 15))) {
1232 FAIL_IF(push_inst(compiler
, LDRI
| RT(i
) | RN(TMP_SP
) | (offs
>> 5)));
1236 FAIL_IF(push_inst(compiler
, LDP
| RT(prev
) | RT2(i
) | RN(TMP_SP
) | offs
));
1241 for (i
= compiler
->scratches
; i
>= SLJIT_FIRST_SAVED_REG
; i
--) {
1243 if (!(offs
& (1 << 15))) {
1247 FAIL_IF(push_inst(compiler
, LDRI
| RT(i
) | RN(TMP_SP
) | (offs
>> 5)));
1251 FAIL_IF(push_inst(compiler
, LDP
| RT(prev
) | RT2(i
) | RN(TMP_SP
) | offs
));
1256 SLJIT_ASSERT(prev
== -1);
1258 if (compiler
->local_size
<= (63 * sizeof(sljit_sw
))) {
1259 FAIL_IF(push_inst(compiler
, LDP_PST
| 29 | RT2(TMP_LR
)
1260 | RN(TMP_SP
) | (((local_size
>> 3) & 0x7f) << 15)));
1261 } else if (saved_regs_size
> 0) {
1262 FAIL_IF(push_inst(compiler
, ADDI
| RD(TMP_SP
) | RN(TMP_SP
) | (saved_regs_size
<< 10)));
1265 FAIL_IF(push_inst(compiler
, RET
| RN(TMP_LR
)));
1266 return SLJIT_SUCCESS
;
1269 /* --------------------------------------------------------------------- */
1271 /* --------------------------------------------------------------------- */
1273 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op0(struct sljit_compiler
*compiler
, sljit_s32 op
)
1275 sljit_ins inv_bits
= (op
& SLJIT_I32_OP
) ? (1 << 31) : 0;
1278 CHECK(check_sljit_emit_op0(compiler
, op
));
1280 op
= GET_OPCODE(op
);
1282 case SLJIT_BREAKPOINT
:
1283 return push_inst(compiler
, BRK
);
1285 return push_inst(compiler
, NOP
);
1288 FAIL_IF(push_inst(compiler
, ORR
| RD(TMP_REG1
) | RN(TMP_ZERO
) | RM(SLJIT_R0
)));
1289 FAIL_IF(push_inst(compiler
, MADD
| RD(SLJIT_R0
) | RN(SLJIT_R0
) | RM(SLJIT_R1
) | RT2(TMP_ZERO
)));
1290 return push_inst(compiler
, (op
== SLJIT_LMUL_UW
? UMULH
: SMULH
) | RD(SLJIT_R1
) | RN(TMP_REG1
) | RM(SLJIT_R1
));
1291 case SLJIT_DIVMOD_UW
:
1292 case SLJIT_DIVMOD_SW
:
1293 FAIL_IF(push_inst(compiler
, (ORR
^ inv_bits
) | RD(TMP_REG1
) | RN(TMP_ZERO
) | RM(SLJIT_R0
)));
1294 FAIL_IF(push_inst(compiler
, ((op
== SLJIT_DIVMOD_UW
? UDIV
: SDIV
) ^ inv_bits
) | RD(SLJIT_R0
) | RN(SLJIT_R0
) | RM(SLJIT_R1
)));
1295 FAIL_IF(push_inst(compiler
, (MADD
^ inv_bits
) | RD(SLJIT_R1
) | RN(SLJIT_R0
) | RM(SLJIT_R1
) | RT2(TMP_ZERO
)));
1296 return push_inst(compiler
, (SUB
^ inv_bits
) | RD(SLJIT_R1
) | RN(TMP_REG1
) | RM(SLJIT_R1
));
1299 return push_inst(compiler
, ((op
== SLJIT_DIV_UW
? UDIV
: SDIV
) ^ inv_bits
) | RD(SLJIT_R0
) | RN(SLJIT_R0
) | RM(SLJIT_R1
));
1302 return SLJIT_SUCCESS
;
1305 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op1(struct sljit_compiler
*compiler
, sljit_s32 op
,
1306 sljit_s32 dst
, sljit_sw dstw
,
1307 sljit_s32 src
, sljit_sw srcw
)
1309 sljit_s32 dst_r
, flags
, mem_flags
;
1310 sljit_s32 op_flags
= GET_ALL_FLAGS(op
);
1313 CHECK(check_sljit_emit_op1(compiler
, op
, dst
, dstw
, src
, srcw
));
1314 ADJUST_LOCAL_OFFSET(dst
, dstw
);
1315 ADJUST_LOCAL_OFFSET(src
, srcw
);
1317 compiler
->cache_arg
= 0;
1318 compiler
->cache_argw
= 0;
1320 if (dst
== SLJIT_UNUSED
&& !HAS_FLAGS(op
)) {
1321 if (op
<= SLJIT_MOV_P
&& (src
& SLJIT_MEM
)) {
1322 SLJIT_ASSERT(reg_map
[1] == 0 && reg_map
[3] == 2 && reg_map
[5] == 4);
1324 if (op
>= SLJIT_MOV_U8
&& op
<= SLJIT_MOV_S8
)
1326 else if (op
>= SLJIT_MOV_U16
&& op
<= SLJIT_MOV_S16
)
1331 /* Signed word sized load is the prefetch instruction. */
1332 return emit_op_mem(compiler
, WORD_SIZE
| SIGNED
, dst
, src
, srcw
);
1334 return SLJIT_SUCCESS
;
1337 dst_r
= SLOW_IS_REG(dst
) ? dst
: TMP_REG1
;
1339 op
= GET_OPCODE(op
);
1340 if (op
>= SLJIT_MOV
&& op
<= SLJIT_MOVU_P
) {
1348 if (src
& SLJIT_IMM
)
1349 srcw
= (sljit_u8
)srcw
;
1352 flags
= BYTE_SIZE
| SIGNED
;
1353 if (src
& SLJIT_IMM
)
1354 srcw
= (sljit_s8
)srcw
;
1358 if (src
& SLJIT_IMM
)
1359 srcw
= (sljit_u16
)srcw
;
1362 flags
= HALF_SIZE
| SIGNED
;
1363 if (src
& SLJIT_IMM
)
1364 srcw
= (sljit_s16
)srcw
;
1368 if (src
& SLJIT_IMM
)
1369 srcw
= (sljit_u32
)srcw
;
1372 flags
= INT_SIZE
| SIGNED
;
1373 if (src
& SLJIT_IMM
)
1374 srcw
= (sljit_s32
)srcw
;
1378 flags
= WORD_SIZE
| UPDATE
;
1381 flags
= BYTE_SIZE
| UPDATE
;
1382 if (src
& SLJIT_IMM
)
1383 srcw
= (sljit_u8
)srcw
;
1386 flags
= BYTE_SIZE
| SIGNED
| UPDATE
;
1387 if (src
& SLJIT_IMM
)
1388 srcw
= (sljit_s8
)srcw
;
1390 case SLJIT_MOVU_U16
:
1391 flags
= HALF_SIZE
| UPDATE
;
1392 if (src
& SLJIT_IMM
)
1393 srcw
= (sljit_u16
)srcw
;
1395 case SLJIT_MOVU_S16
:
1396 flags
= HALF_SIZE
| SIGNED
| UPDATE
;
1397 if (src
& SLJIT_IMM
)
1398 srcw
= (sljit_s16
)srcw
;
1400 case SLJIT_MOVU_U32
:
1401 flags
= INT_SIZE
| UPDATE
;
1402 if (src
& SLJIT_IMM
)
1403 srcw
= (sljit_u32
)srcw
;
1405 case SLJIT_MOVU_S32
:
1406 flags
= INT_SIZE
| SIGNED
| UPDATE
;
1407 if (src
& SLJIT_IMM
)
1408 srcw
= (sljit_s32
)srcw
;
1411 SLJIT_UNREACHABLE();
1416 if (src
& SLJIT_IMM
)
1417 FAIL_IF(emit_op_imm(compiler
, SLJIT_MOV
| ARG2_IMM
, dst_r
, TMP_REG1
, srcw
));
1418 else if (src
& SLJIT_MEM
) {
1419 if (getput_arg_fast(compiler
, flags
, dst_r
, src
, srcw
))
1420 FAIL_IF(compiler
->error
);
1422 FAIL_IF(getput_arg(compiler
, flags
, dst_r
, src
, srcw
, dst
, dstw
));
1424 if (dst_r
!= TMP_REG1
)
1425 return emit_op_imm(compiler
, op
| ((op_flags
& SLJIT_I32_OP
) ? INT_OP
: 0), dst_r
, TMP_REG1
, src
);
1429 if (dst
& SLJIT_MEM
) {
1430 if (getput_arg_fast(compiler
, flags
| STORE
, dst_r
, dst
, dstw
))
1431 return compiler
->error
;
1433 return getput_arg(compiler
, flags
| STORE
, dst_r
, dst
, dstw
, 0, 0);
1435 return SLJIT_SUCCESS
;
1438 flags
= HAS_FLAGS(op_flags
) ? SET_FLAGS
: 0;
1439 mem_flags
= WORD_SIZE
;
1440 if (op_flags
& SLJIT_I32_OP
) {
1442 mem_flags
= INT_SIZE
;
1445 if (dst
== SLJIT_UNUSED
)
1446 flags
|= UNUSED_RETURN
;
1448 if (src
& SLJIT_MEM
) {
1449 if (getput_arg_fast(compiler
, mem_flags
, TMP_REG2
, src
, srcw
))
1450 FAIL_IF(compiler
->error
);
1452 FAIL_IF(getput_arg(compiler
, mem_flags
, TMP_REG2
, src
, srcw
, dst
, dstw
));
1456 if (src
& SLJIT_IMM
) {
1458 if (op_flags
& SLJIT_I32_OP
)
1459 srcw
= (sljit_s32
)srcw
;
1463 emit_op_imm(compiler
, flags
| op
, dst_r
, TMP_REG1
, srcw
);
1465 if (dst
& SLJIT_MEM
) {
1466 if (getput_arg_fast(compiler
, mem_flags
| STORE
, dst_r
, dst
, dstw
))
1467 return compiler
->error
;
1469 return getput_arg(compiler
, mem_flags
| STORE
, dst_r
, dst
, dstw
, 0, 0);
1471 return SLJIT_SUCCESS
;
1474 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op2(struct sljit_compiler
*compiler
, sljit_s32 op
,
1475 sljit_s32 dst
, sljit_sw dstw
,
1476 sljit_s32 src1
, sljit_sw src1w
,
1477 sljit_s32 src2
, sljit_sw src2w
)
1479 sljit_s32 dst_r
, flags
, mem_flags
;
1482 CHECK(check_sljit_emit_op2(compiler
, op
, dst
, dstw
, src1
, src1w
, src2
, src2w
));
1483 ADJUST_LOCAL_OFFSET(dst
, dstw
);
1484 ADJUST_LOCAL_OFFSET(src1
, src1w
);
1485 ADJUST_LOCAL_OFFSET(src2
, src2w
);
1487 compiler
->cache_arg
= 0;
1488 compiler
->cache_argw
= 0;
1490 if (dst
== SLJIT_UNUSED
&& !HAS_FLAGS(op
))
1491 return SLJIT_SUCCESS
;
1493 dst_r
= SLOW_IS_REG(dst
) ? dst
: TMP_REG1
;
1494 flags
= HAS_FLAGS(op
) ? SET_FLAGS
: 0;
1495 mem_flags
= WORD_SIZE
;
1496 if (op
& SLJIT_I32_OP
) {
1498 mem_flags
= INT_SIZE
;
1501 if (dst
== SLJIT_UNUSED
)
1502 flags
|= UNUSED_RETURN
;
1504 if ((dst
& SLJIT_MEM
) && !getput_arg_fast(compiler
, mem_flags
| STORE
| ARG_TEST
, TMP_REG1
, dst
, dstw
))
1507 if (src1
& SLJIT_MEM
) {
1508 if (getput_arg_fast(compiler
, mem_flags
, TMP_REG1
, src1
, src1w
))
1509 FAIL_IF(compiler
->error
);
1513 if (src2
& SLJIT_MEM
) {
1514 if (getput_arg_fast(compiler
, mem_flags
, TMP_REG2
, src2
, src2w
))
1515 FAIL_IF(compiler
->error
);
1520 if ((flags
& (SLOW_SRC1
| SLOW_SRC2
)) == (SLOW_SRC1
| SLOW_SRC2
)) {
1521 if (!can_cache(src1
, src1w
, src2
, src2w
) && can_cache(src1
, src1w
, dst
, dstw
)) {
1522 FAIL_IF(getput_arg(compiler
, mem_flags
, TMP_REG2
, src2
, src2w
, src1
, src1w
));
1523 FAIL_IF(getput_arg(compiler
, mem_flags
, TMP_REG1
, src1
, src1w
, dst
, dstw
));
1526 FAIL_IF(getput_arg(compiler
, mem_flags
, TMP_REG1
, src1
, src1w
, src2
, src2w
));
1527 FAIL_IF(getput_arg(compiler
, mem_flags
, TMP_REG2
, src2
, src2w
, dst
, dstw
));
1530 else if (flags
& SLOW_SRC1
)
1531 FAIL_IF(getput_arg(compiler
, mem_flags
, TMP_REG1
, src1
, src1w
, dst
, dstw
));
1532 else if (flags
& SLOW_SRC2
)
1533 FAIL_IF(getput_arg(compiler
, mem_flags
, TMP_REG2
, src2
, src2w
, dst
, dstw
));
1535 if (src1
& SLJIT_MEM
)
1537 if (src2
& SLJIT_MEM
)
1540 if (src1
& SLJIT_IMM
)
1544 if (src2
& SLJIT_IMM
)
1549 emit_op_imm(compiler
, flags
| GET_OPCODE(op
), dst_r
, src1w
, src2w
);
1551 if (dst
& SLJIT_MEM
) {
1552 if (!(flags
& SLOW_DEST
)) {
1553 getput_arg_fast(compiler
, mem_flags
| STORE
, dst_r
, dst
, dstw
);
1554 return compiler
->error
;
1556 return getput_arg(compiler
, mem_flags
| STORE
, TMP_REG1
, dst
, dstw
, 0, 0);
1559 return SLJIT_SUCCESS
;
1562 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_get_register_index(sljit_s32 reg
)
1564 CHECK_REG_INDEX(check_sljit_get_register_index(reg
));
1565 return reg_map
[reg
];
1568 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_get_float_register_index(sljit_s32 reg
)
1570 CHECK_REG_INDEX(check_sljit_get_float_register_index(reg
));
1574 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op_custom(struct sljit_compiler
*compiler
,
1575 void *instruction
, sljit_s32 size
)
1578 CHECK(check_sljit_emit_op_custom(compiler
, instruction
, size
));
1580 return push_inst(compiler
, *(sljit_ins
*)instruction
);
1583 /* --------------------------------------------------------------------- */
1584 /* Floating point operators */
1585 /* --------------------------------------------------------------------- */
1587 static sljit_s32
emit_fop_mem(struct sljit_compiler
*compiler
, sljit_s32 flags
, sljit_s32 reg
, sljit_s32 arg
, sljit_sw argw
)
1589 sljit_u32 shift
= MEM_SIZE_SHIFT(flags
);
1590 sljit_ins ins_bits
= (shift
<< 30);
1594 SLJIT_ASSERT(arg
& SLJIT_MEM
);
1596 if (!(flags
& STORE
))
1597 ins_bits
|= 1 << 22;
1599 if (arg
& OFFS_REG_MASK
) {
1601 if (!argw
|| argw
== shift
)
1602 return push_inst(compiler
, STR_FR
| ins_bits
| VT(reg
)
1603 | RN(arg
& REG_MASK
) | RM(OFFS_REG(arg
)) | (argw
? (1 << 12) : 0));
1604 other_r
= OFFS_REG(arg
);
1606 FAIL_IF(push_inst(compiler
, ADD
| RD(TMP_REG1
) | RN(arg
) | RM(other_r
) | (argw
<< 10)));
1612 if (arg
&& argw
>= 0 && ((argw
>> shift
) <= 0xfff) && (argw
& ((1 << shift
) - 1)) == 0)
1613 return push_inst(compiler
, STR_FI
| ins_bits
| VT(reg
) | RN(arg
) | (argw
<< (10 - shift
)));
1615 if (arg
&& argw
<= 255 && argw
>= -256)
1616 return push_inst(compiler
, STUR_FI
| ins_bits
| VT(reg
) | RN(arg
) | ((argw
& 0x1ff) << 12));
1619 if (compiler
->cache_arg
== SLJIT_MEM
&& argw
!= compiler
->cache_argw
) {
1620 diff
= argw
- compiler
->cache_argw
;
1621 if (!arg
&& diff
<= 255 && diff
>= -256)
1622 return push_inst(compiler
, STUR_FI
| ins_bits
| VT(reg
) | RN(TMP_REG3
) | ((diff
& 0x1ff) << 12));
1623 if (emit_set_delta(compiler
, TMP_REG3
, TMP_REG3
, argw
- compiler
->cache_argw
) != SLJIT_ERR_UNSUPPORTED
) {
1624 FAIL_IF(compiler
->error
);
1625 compiler
->cache_argw
= argw
;
1629 if (compiler
->cache_arg
!= SLJIT_MEM
|| argw
!= compiler
->cache_argw
) {
1630 compiler
->cache_arg
= SLJIT_MEM
;
1631 compiler
->cache_argw
= argw
;
1632 FAIL_IF(load_immediate(compiler
, TMP_REG3
, argw
));
1636 return push_inst(compiler
, STR_FR
| ins_bits
| VT(reg
) | RN(arg
) | RM(TMP_REG3
));
1637 return push_inst(compiler
, STR_FI
| ins_bits
| VT(reg
) | RN(TMP_REG3
));
1640 static SLJIT_INLINE sljit_s32
sljit_emit_fop1_conv_sw_from_f64(struct sljit_compiler
*compiler
, sljit_s32 op
,
1641 sljit_s32 dst
, sljit_sw dstw
,
1642 sljit_s32 src
, sljit_sw srcw
)
1644 sljit_s32 dst_r
= FAST_IS_REG(dst
) ? dst
: TMP_REG1
;
1645 sljit_ins inv_bits
= (op
& SLJIT_F32_OP
) ? (1 << 22) : 0;
1647 if (GET_OPCODE(op
) == SLJIT_CONV_S32_FROM_F64
)
1648 inv_bits
|= (1 << 31);
1650 if (src
& SLJIT_MEM
) {
1651 emit_fop_mem(compiler
, (op
& SLJIT_F32_OP
) ? INT_SIZE
: WORD_SIZE
, TMP_FREG1
, src
, srcw
);
1655 FAIL_IF(push_inst(compiler
, (FCVTZS
^ inv_bits
) | RD(dst_r
) | VN(src
)));
1657 if (dst
& SLJIT_MEM
)
1658 return emit_op_mem(compiler
, ((GET_OPCODE(op
) == SLJIT_CONV_S32_FROM_F64
) ? INT_SIZE
: WORD_SIZE
) | STORE
, TMP_REG1
, dst
, dstw
);
1659 return SLJIT_SUCCESS
;
1662 static SLJIT_INLINE sljit_s32
sljit_emit_fop1_conv_f64_from_sw(struct sljit_compiler
*compiler
, sljit_s32 op
,
1663 sljit_s32 dst
, sljit_sw dstw
,
1664 sljit_s32 src
, sljit_sw srcw
)
1666 sljit_s32 dst_r
= FAST_IS_REG(dst
) ? dst
: TMP_FREG1
;
1667 sljit_ins inv_bits
= (op
& SLJIT_F32_OP
) ? (1 << 22) : 0;
1669 if (GET_OPCODE(op
) == SLJIT_CONV_F64_FROM_S32
)
1670 inv_bits
|= (1 << 31);
1672 if (src
& SLJIT_MEM
) {
1673 emit_op_mem(compiler
, ((GET_OPCODE(op
) == SLJIT_CONV_F64_FROM_S32
) ? INT_SIZE
: WORD_SIZE
), TMP_REG1
, src
, srcw
);
1675 } else if (src
& SLJIT_IMM
) {
1676 #if (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64)
1677 if (GET_OPCODE(op
) == SLJIT_CONV_F64_FROM_S32
)
1678 srcw
= (sljit_s32
)srcw
;
1680 FAIL_IF(load_immediate(compiler
, TMP_REG1
, srcw
));
1684 FAIL_IF(push_inst(compiler
, (SCVTF
^ inv_bits
) | VD(dst_r
) | RN(src
)));
1686 if (dst
& SLJIT_MEM
)
1687 return emit_fop_mem(compiler
, ((op
& SLJIT_F32_OP
) ? INT_SIZE
: WORD_SIZE
) | STORE
, TMP_FREG1
, dst
, dstw
);
1688 return SLJIT_SUCCESS
;
1691 static SLJIT_INLINE sljit_s32
sljit_emit_fop1_cmp(struct sljit_compiler
*compiler
, sljit_s32 op
,
1692 sljit_s32 src1
, sljit_sw src1w
,
1693 sljit_s32 src2
, sljit_sw src2w
)
1695 sljit_s32 mem_flags
= (op
& SLJIT_F32_OP
) ? INT_SIZE
: WORD_SIZE
;
1696 sljit_ins inv_bits
= (op
& SLJIT_F32_OP
) ? (1 << 22) : 0;
1698 if (src1
& SLJIT_MEM
) {
1699 emit_fop_mem(compiler
, mem_flags
, TMP_FREG1
, src1
, src1w
);
1703 if (src2
& SLJIT_MEM
) {
1704 emit_fop_mem(compiler
, mem_flags
, TMP_FREG2
, src2
, src2w
);
1708 return push_inst(compiler
, (FCMP
^ inv_bits
) | VN(src1
) | VM(src2
));
1711 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_fop1(struct sljit_compiler
*compiler
, sljit_s32 op
,
1712 sljit_s32 dst
, sljit_sw dstw
,
1713 sljit_s32 src
, sljit_sw srcw
)
1715 sljit_s32 dst_r
, mem_flags
= (op
& SLJIT_F32_OP
) ? INT_SIZE
: WORD_SIZE
;
1719 compiler
->cache_arg
= 0;
1720 compiler
->cache_argw
= 0;
1722 SLJIT_COMPILE_ASSERT((INT_SIZE
^ 0x100) == WORD_SIZE
, must_be_one_bit_difference
);
1723 SELECT_FOP1_OPERATION_WITH_CHECKS(compiler
, op
, dst
, dstw
, src
, srcw
);
1725 inv_bits
= (op
& SLJIT_F32_OP
) ? (1 << 22) : 0;
1726 dst_r
= FAST_IS_REG(dst
) ? dst
: TMP_FREG1
;
1728 if (src
& SLJIT_MEM
) {
1729 emit_fop_mem(compiler
, (GET_OPCODE(op
) == SLJIT_CONV_F64_FROM_F32
) ? (mem_flags
^ 0x100) : mem_flags
, dst_r
, src
, srcw
);
1733 switch (GET_OPCODE(op
)) {
1736 if (dst_r
!= TMP_FREG1
)
1737 FAIL_IF(push_inst(compiler
, (FMOV
^ inv_bits
) | VD(dst_r
) | VN(src
)));
1743 FAIL_IF(push_inst(compiler
, (FNEG
^ inv_bits
) | VD(dst_r
) | VN(src
)));
1746 FAIL_IF(push_inst(compiler
, (FABS
^ inv_bits
) | VD(dst_r
) | VN(src
)));
1748 case SLJIT_CONV_F64_FROM_F32
:
1749 FAIL_IF(push_inst(compiler
, FCVT
| ((op
& SLJIT_F32_OP
) ? (1 << 22) : (1 << 15)) | VD(dst_r
) | VN(src
)));
1753 if (dst
& SLJIT_MEM
)
1754 return emit_fop_mem(compiler
, mem_flags
| STORE
, dst_r
, dst
, dstw
);
1755 return SLJIT_SUCCESS
;
1758 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_fop2(struct sljit_compiler
*compiler
, sljit_s32 op
,
1759 sljit_s32 dst
, sljit_sw dstw
,
1760 sljit_s32 src1
, sljit_sw src1w
,
1761 sljit_s32 src2
, sljit_sw src2w
)
1763 sljit_s32 dst_r
, mem_flags
= (op
& SLJIT_F32_OP
) ? INT_SIZE
: WORD_SIZE
;
1764 sljit_ins inv_bits
= (op
& SLJIT_F32_OP
) ? (1 << 22) : 0;
1767 CHECK(check_sljit_emit_fop2(compiler
, op
, dst
, dstw
, src1
, src1w
, src2
, src2w
));
1768 ADJUST_LOCAL_OFFSET(dst
, dstw
);
1769 ADJUST_LOCAL_OFFSET(src1
, src1w
);
1770 ADJUST_LOCAL_OFFSET(src2
, src2w
);
1772 compiler
->cache_arg
= 0;
1773 compiler
->cache_argw
= 0;
1775 dst_r
= FAST_IS_REG(dst
) ? dst
: TMP_FREG1
;
1776 if (src1
& SLJIT_MEM
) {
1777 emit_fop_mem(compiler
, mem_flags
, TMP_FREG1
, src1
, src1w
);
1780 if (src2
& SLJIT_MEM
) {
1781 emit_fop_mem(compiler
, mem_flags
, TMP_FREG2
, src2
, src2w
);
1785 switch (GET_OPCODE(op
)) {
1787 FAIL_IF(push_inst(compiler
, (FADD
^ inv_bits
) | VD(dst_r
) | VN(src1
) | VM(src2
)));
1790 FAIL_IF(push_inst(compiler
, (FSUB
^ inv_bits
) | VD(dst_r
) | VN(src1
) | VM(src2
)));
1793 FAIL_IF(push_inst(compiler
, (FMUL
^ inv_bits
) | VD(dst_r
) | VN(src1
) | VM(src2
)));
1796 FAIL_IF(push_inst(compiler
, (FDIV
^ inv_bits
) | VD(dst_r
) | VN(src1
) | VM(src2
)));
1800 if (!(dst
& SLJIT_MEM
))
1801 return SLJIT_SUCCESS
;
1802 return emit_fop_mem(compiler
, mem_flags
| STORE
, TMP_FREG1
, dst
, dstw
);
1805 /* --------------------------------------------------------------------- */
1806 /* Other instructions */
1807 /* --------------------------------------------------------------------- */
1809 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_fast_enter(struct sljit_compiler
*compiler
, sljit_s32 dst
, sljit_sw dstw
)
1812 CHECK(check_sljit_emit_fast_enter(compiler
, dst
, dstw
));
1813 ADJUST_LOCAL_OFFSET(dst
, dstw
);
1815 if (FAST_IS_REG(dst
))
1816 return push_inst(compiler
, ORR
| RD(dst
) | RN(TMP_ZERO
) | RM(TMP_LR
));
1819 return emit_op_mem(compiler
, WORD_SIZE
| STORE
, TMP_LR
, dst
, dstw
);
1822 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_fast_return(struct sljit_compiler
*compiler
, sljit_s32 src
, sljit_sw srcw
)
1825 CHECK(check_sljit_emit_fast_return(compiler
, src
, srcw
));
1826 ADJUST_LOCAL_OFFSET(src
, srcw
);
1828 if (FAST_IS_REG(src
))
1829 FAIL_IF(push_inst(compiler
, ORR
| RD(TMP_LR
) | RN(TMP_ZERO
) | RM(src
)));
1830 else if (src
& SLJIT_MEM
)
1831 FAIL_IF(emit_op_mem(compiler
, WORD_SIZE
, TMP_LR
, src
, srcw
));
1832 else if (src
& SLJIT_IMM
)
1833 FAIL_IF(load_immediate(compiler
, TMP_LR
, srcw
));
1835 return push_inst(compiler
, RET
| RN(TMP_LR
));
1838 /* --------------------------------------------------------------------- */
1839 /* Conditional instructions */
1840 /* --------------------------------------------------------------------- */
1842 static sljit_uw
get_cc(sljit_s32 type
)
1846 case SLJIT_MUL_NOT_OVERFLOW
:
1847 case SLJIT_EQUAL_F64
:
1850 case SLJIT_NOT_EQUAL
:
1851 case SLJIT_MUL_OVERFLOW
:
1852 case SLJIT_NOT_EQUAL_F64
:
1856 case SLJIT_LESS_F64
:
1859 case SLJIT_GREATER_EQUAL
:
1860 case SLJIT_GREATER_EQUAL_F64
:
1864 case SLJIT_GREATER_F64
:
1867 case SLJIT_LESS_EQUAL
:
1868 case SLJIT_LESS_EQUAL_F64
:
1871 case SLJIT_SIG_LESS
:
1874 case SLJIT_SIG_GREATER_EQUAL
:
1877 case SLJIT_SIG_GREATER
:
1880 case SLJIT_SIG_LESS_EQUAL
:
1883 case SLJIT_OVERFLOW
:
1884 case SLJIT_UNORDERED_F64
:
1887 case SLJIT_NOT_OVERFLOW
:
1888 case SLJIT_ORDERED_F64
:
1892 SLJIT_UNREACHABLE();
1897 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_label
* sljit_emit_label(struct sljit_compiler
*compiler
)
1899 struct sljit_label
*label
;
1902 CHECK_PTR(check_sljit_emit_label(compiler
));
1904 if (compiler
->last_label
&& compiler
->last_label
->size
== compiler
->size
)
1905 return compiler
->last_label
;
1907 label
= (struct sljit_label
*)ensure_abuf(compiler
, sizeof(struct sljit_label
));
1908 PTR_FAIL_IF(!label
);
1909 set_label(label
, compiler
);
1913 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_jump
* sljit_emit_jump(struct sljit_compiler
*compiler
, sljit_s32 type
)
1915 struct sljit_jump
*jump
;
1918 CHECK_PTR(check_sljit_emit_jump(compiler
, type
));
1920 jump
= (struct sljit_jump
*)ensure_abuf(compiler
, sizeof(struct sljit_jump
));
1922 set_jump(jump
, compiler
, type
& SLJIT_REWRITABLE_JUMP
);
1925 if (type
< SLJIT_JUMP
) {
1926 jump
->flags
|= IS_COND
;
1927 PTR_FAIL_IF(push_inst(compiler
, B_CC
| (6 << 5) | get_cc(type
)));
1929 else if (type
>= SLJIT_FAST_CALL
)
1930 jump
->flags
|= IS_BL
;
1932 PTR_FAIL_IF(emit_imm64_const(compiler
, TMP_REG1
, 0));
1933 jump
->addr
= compiler
->size
;
1934 PTR_FAIL_IF(push_inst(compiler
, ((type
>= SLJIT_FAST_CALL
) ? BLR
: BR
) | RN(TMP_REG1
)));
1939 static SLJIT_INLINE
struct sljit_jump
* emit_cmp_to0(struct sljit_compiler
*compiler
, sljit_s32 type
,
1940 sljit_s32 src
, sljit_sw srcw
)
1942 struct sljit_jump
*jump
;
1943 sljit_ins inv_bits
= (type
& SLJIT_I32_OP
) ? (1 << 31) : 0;
1945 SLJIT_ASSERT((type
& 0xff) == SLJIT_EQUAL
|| (type
& 0xff) == SLJIT_NOT_EQUAL
);
1946 ADJUST_LOCAL_OFFSET(src
, srcw
);
1948 jump
= (struct sljit_jump
*)ensure_abuf(compiler
, sizeof(struct sljit_jump
));
1950 set_jump(jump
, compiler
, type
& SLJIT_REWRITABLE_JUMP
);
1951 jump
->flags
|= IS_CBZ
| IS_COND
;
1953 if (src
& SLJIT_MEM
) {
1954 PTR_FAIL_IF(emit_op_mem(compiler
, inv_bits
? INT_SIZE
: WORD_SIZE
, TMP_REG1
, src
, srcw
));
1957 else if (src
& SLJIT_IMM
) {
1958 PTR_FAIL_IF(load_immediate(compiler
, TMP_REG1
, srcw
));
1961 SLJIT_ASSERT(FAST_IS_REG(src
));
1963 if ((type
& 0xff) == SLJIT_EQUAL
)
1964 inv_bits
|= 1 << 24;
1966 PTR_FAIL_IF(push_inst(compiler
, (CBZ
^ inv_bits
) | (6 << 5) | RT(src
)));
1967 PTR_FAIL_IF(emit_imm64_const(compiler
, TMP_REG1
, 0));
1968 jump
->addr
= compiler
->size
;
1969 PTR_FAIL_IF(push_inst(compiler
, BR
| RN(TMP_REG1
)));
1973 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_ijump(struct sljit_compiler
*compiler
, sljit_s32 type
, sljit_s32 src
, sljit_sw srcw
)
1975 struct sljit_jump
*jump
;
1978 CHECK(check_sljit_emit_ijump(compiler
, type
, src
, srcw
));
1979 ADJUST_LOCAL_OFFSET(src
, srcw
);
1981 /* In ARM, we don't need to touch the arguments. */
1982 if (!(src
& SLJIT_IMM
)) {
1983 if (src
& SLJIT_MEM
) {
1984 FAIL_IF(emit_op_mem(compiler
, WORD_SIZE
, TMP_REG1
, src
, srcw
));
1987 return push_inst(compiler
, ((type
>= SLJIT_FAST_CALL
) ? BLR
: BR
) | RN(src
));
1990 jump
= (struct sljit_jump
*)ensure_abuf(compiler
, sizeof(struct sljit_jump
));
1992 set_jump(jump
, compiler
, JUMP_ADDR
| ((type
>= SLJIT_FAST_CALL
) ? IS_BL
: 0));
1993 jump
->u
.target
= srcw
;
1995 FAIL_IF(emit_imm64_const(compiler
, TMP_REG1
, 0));
1996 jump
->addr
= compiler
->size
;
1997 return push_inst(compiler
, ((type
>= SLJIT_FAST_CALL
) ? BLR
: BR
) | RN(TMP_REG1
));
2000 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op_flags(struct sljit_compiler
*compiler
, sljit_s32 op
,
2001 sljit_s32 dst
, sljit_sw dstw
,
2004 sljit_s32 dst_r
, src_r
, flags
, mem_flags
;
2008 CHECK(check_sljit_emit_op_flags(compiler
, op
, dst
, dstw
, type
));
2009 ADJUST_LOCAL_OFFSET(dst
, dstw
);
2011 cc
= get_cc(type
& 0xff);
2012 dst_r
= FAST_IS_REG(dst
) ? dst
: TMP_REG1
;
2014 if (GET_OPCODE(op
) < SLJIT_ADD
) {
2015 FAIL_IF(push_inst(compiler
, CSINC
| (cc
<< 12) | RD(dst_r
) | RN(TMP_ZERO
) | RM(TMP_ZERO
)));
2016 if (dst_r
!= TMP_REG1
)
2017 return SLJIT_SUCCESS
;
2018 return emit_op_mem(compiler
, (GET_OPCODE(op
) == SLJIT_MOV
? WORD_SIZE
: INT_SIZE
) | STORE
, TMP_REG1
, dst
, dstw
);
2021 compiler
->cache_arg
= 0;
2022 compiler
->cache_argw
= 0;
2023 flags
= HAS_FLAGS(op
) ? SET_FLAGS
: 0;
2024 mem_flags
= WORD_SIZE
;
2025 if (op
& SLJIT_I32_OP
) {
2027 mem_flags
= INT_SIZE
;
2032 if (dst
& SLJIT_MEM
) {
2033 FAIL_IF(emit_op_mem2(compiler
, mem_flags
, TMP_REG1
, dst
, dstw
, dst
, dstw
));
2037 FAIL_IF(push_inst(compiler
, CSINC
| (cc
<< 12) | RD(TMP_REG2
) | RN(TMP_ZERO
) | RM(TMP_ZERO
)));
2038 emit_op_imm(compiler
, flags
| GET_OPCODE(op
), dst_r
, src_r
, TMP_REG2
);
2040 if (dst
& SLJIT_MEM
)
2041 return emit_op_mem2(compiler
, mem_flags
| STORE
, TMP_REG1
, dst
, dstw
, 0, 0);
2042 return SLJIT_SUCCESS
;
2045 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_cmov(struct sljit_compiler
*compiler
, sljit_s32 type
,
2047 sljit_s32 src
, sljit_sw srcw
)
2049 sljit_ins inv_bits
= (dst_reg
& SLJIT_I32_OP
) ? (1 << 31) : 0;
2053 CHECK(check_sljit_emit_cmov(compiler
, type
, dst_reg
, src
, srcw
));
2055 if (SLJIT_UNLIKELY(src
& SLJIT_IMM
)) {
2056 if (dst_reg
& SLJIT_I32_OP
)
2057 srcw
= (sljit_s32
)srcw
;
2058 FAIL_IF(load_immediate(compiler
, TMP_REG1
, srcw
));
2063 cc
= get_cc(type
& 0xff);
2064 dst_reg
&= ~SLJIT_I32_OP
;
2066 return push_inst(compiler
, (CSEL
^ inv_bits
) | (cc
<< 12) | RD(dst_reg
) | RN(dst_reg
) | RM(src
));
2069 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_const
* sljit_emit_const(struct sljit_compiler
*compiler
, sljit_s32 dst
, sljit_sw dstw
, sljit_sw init_value
)
2071 struct sljit_const
*const_
;
2075 CHECK_PTR(check_sljit_emit_const(compiler
, dst
, dstw
, init_value
));
2076 ADJUST_LOCAL_OFFSET(dst
, dstw
);
2078 const_
= (struct sljit_const
*)ensure_abuf(compiler
, sizeof(struct sljit_const
));
2079 PTR_FAIL_IF(!const_
);
2080 set_const(const_
, compiler
);
2082 dst_r
= FAST_IS_REG(dst
) ? dst
: TMP_REG1
;
2083 PTR_FAIL_IF(emit_imm64_const(compiler
, dst_r
, init_value
));
2085 if (dst
& SLJIT_MEM
)
2086 PTR_FAIL_IF(emit_op_mem(compiler
, WORD_SIZE
| STORE
, dst_r
, dst
, dstw
));
2090 SLJIT_API_FUNC_ATTRIBUTE
void sljit_set_jump_addr(sljit_uw addr
, sljit_uw new_target
, sljit_sw executable_offset
)
2092 sljit_ins
* inst
= (sljit_ins
*)addr
;
2093 modify_imm64_const(inst
, new_target
);
2094 inst
= (sljit_ins
*)SLJIT_ADD_EXEC_OFFSET(inst
, executable_offset
);
2095 SLJIT_CACHE_FLUSH(inst
, inst
+ 4);
2098 SLJIT_API_FUNC_ATTRIBUTE
void sljit_set_const(sljit_uw addr
, sljit_sw new_constant
, sljit_sw executable_offset
)
2100 sljit_ins
* inst
= (sljit_ins
*)addr
;
2101 modify_imm64_const(inst
, new_constant
);
2102 inst
= (sljit_ins
*)SLJIT_ADD_EXEC_OFFSET(inst
, executable_offset
);
2103 SLJIT_CACHE_FLUSH(inst
, inst
+ 4);