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_LR (SLJIT_NUMBER_OF_REGISTERS + 4)
40 #define TMP_FP (SLJIT_NUMBER_OF_REGISTERS + 5)
42 #define TMP_FREG1 (SLJIT_NUMBER_OF_FLOAT_REGISTERS + 1)
43 #define TMP_FREG2 (SLJIT_NUMBER_OF_FLOAT_REGISTERS + 2)
45 /* r18 - platform register, currently not used */
46 static const sljit_u8 reg_map
[SLJIT_NUMBER_OF_REGISTERS
+ 8] = {
47 31, 0, 1, 2, 3, 4, 5, 6, 7, 11, 12, 13, 14, 15, 16, 17, 8, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 31, 9, 10, 30, 29
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 ((sljit_ins)1 << 31)
55 #define RD(rd) ((sljit_ins)reg_map[rd])
56 #define RT(rt) ((sljit_ins)reg_map[rt])
57 #define RN(rn) ((sljit_ins)reg_map[rn] << 5)
58 #define RT2(rt2) ((sljit_ins)reg_map[rt2] << 10)
59 #define RM(rm) ((sljit_ins)reg_map[rm] << 16)
60 #define VD(vd) ((sljit_ins)freg_map[vd])
61 #define VT(vt) ((sljit_ins)freg_map[vt])
62 #define VN(vn) ((sljit_ins)freg_map[vn] << 5)
63 #define VM(vm) ((sljit_ins)freg_map[vm] << 16)
65 /* --------------------------------------------------------------------- */
66 /* Instrucion forms */
67 /* --------------------------------------------------------------------- */
69 #define ADC 0x9a000000
70 #define ADD 0x8b000000
71 #define ADDE 0x8b200000
72 #define ADDI 0x91000000
73 #define AND 0x8a000000
74 #define ANDI 0x92000000
75 #define ASRV 0x9ac02800
77 #define B_CC 0x54000000
79 #define BLR 0xd63f0000
81 #define BRK 0xd4200000
82 #define CBZ 0xb4000000
83 #define CLZ 0xdac01000
84 #define CSEL 0x9a800000
85 #define CSINC 0x9a800400
86 #define EOR 0xca000000
87 #define EORI 0xd2000000
88 #define FABS 0x1e60c000
89 #define FADD 0x1e602800
90 #define FCMP 0x1e602000
91 #define FCVT 0x1e224000
92 #define FCVTZS 0x9e780000
93 #define FDIV 0x1e601800
94 #define FMOV 0x1e604000
95 #define FMUL 0x1e600800
96 #define FNEG 0x1e614000
97 #define FSUB 0x1e603800
98 #define LDRI 0xf9400000
99 #define LDP 0xa9400000
100 #define LDP_PRE 0xa9c00000
101 #define LDR_PRE 0xf8400c00
102 #define LSLV 0x9ac02000
103 #define LSRV 0x9ac02400
104 #define MADD 0x9b000000
105 #define MOVK 0xf2800000
106 #define MOVN 0x92800000
107 #define MOVZ 0xd2800000
108 #define NOP 0xd503201f
109 #define ORN 0xaa200000
110 #define ORR 0xaa000000
111 #define ORRI 0xb2000000
112 #define RET 0xd65f0000
113 #define SBC 0xda000000
114 #define SBFM 0x93000000
115 #define SCVTF 0x9e620000
116 #define SDIV 0x9ac00c00
117 #define SMADDL 0x9b200000
118 #define SMULH 0x9b403c00
119 #define STP 0xa9000000
120 #define STP_PRE 0xa9800000
121 #define STRB 0x38206800
122 #define STRBI 0x39000000
123 #define STRI 0xf9000000
124 #define STR_FI 0x3d000000
125 #define STR_FR 0x3c206800
126 #define STUR_FI 0x3c000000
127 #define STURBI 0x38000000
128 #define SUB 0xcb000000
129 #define SUBI 0xd1000000
130 #define SUBS 0xeb000000
131 #define UBFM 0xd3000000
132 #define UDIV 0x9ac00800
133 #define UMULH 0x9bc03c00
135 /* dest_reg is the absolute name of the register
136 Useful for reordering instructions in the delay slot. */
137 static sljit_s32
push_inst(struct sljit_compiler
*compiler
, sljit_ins ins
)
139 sljit_ins
*ptr
= (sljit_ins
*)ensure_buf(compiler
, sizeof(sljit_ins
));
143 return SLJIT_SUCCESS
;
146 static SLJIT_INLINE sljit_s32
emit_imm64_const(struct sljit_compiler
*compiler
, sljit_s32 dst
, sljit_uw imm
)
148 FAIL_IF(push_inst(compiler
, MOVZ
| RD(dst
) | ((sljit_ins
)(imm
& 0xffff) << 5)));
149 FAIL_IF(push_inst(compiler
, MOVK
| RD(dst
) | (((sljit_ins
)(imm
>> 16) & 0xffff) << 5) | (1 << 21)));
150 FAIL_IF(push_inst(compiler
, MOVK
| RD(dst
) | (((sljit_ins
)(imm
>> 32) & 0xffff) << 5) | (2 << 21)));
151 return push_inst(compiler
, MOVK
| RD(dst
) | ((sljit_ins
)(imm
>> 48) << 5) | (3 << 21));
154 static SLJIT_INLINE sljit_sw
detect_jump_type(struct sljit_jump
*jump
, sljit_ins
*code_ptr
, sljit_ins
*code
, sljit_sw executable_offset
)
157 sljit_uw target_addr
;
159 if (jump
->flags
& SLJIT_REWRITABLE_JUMP
) {
160 jump
->flags
|= PATCH_ABS64
;
164 if (jump
->flags
& JUMP_ADDR
)
165 target_addr
= jump
->u
.target
;
167 SLJIT_ASSERT(jump
->flags
& JUMP_LABEL
);
168 target_addr
= (sljit_uw
)(code
+ jump
->u
.label
->size
) + (sljit_uw
)executable_offset
;
171 diff
= (sljit_sw
)target_addr
- (sljit_sw
)(code_ptr
+ 4) - executable_offset
;
173 if (jump
->flags
& IS_COND
) {
174 diff
+= SSIZE_OF(ins
);
175 if (diff
<= 0xfffff && diff
>= -0x100000) {
176 code_ptr
[-5] ^= (jump
->flags
& IS_CBZ
) ? (0x1 << 24) : 0x1;
177 jump
->addr
-= sizeof(sljit_ins
);
178 jump
->flags
|= PATCH_COND
;
181 diff
-= SSIZE_OF(ins
);
184 if (diff
<= 0x7ffffff && diff
>= -0x8000000) {
185 jump
->flags
|= PATCH_B
;
189 if (target_addr
< 0x100000000l
) {
190 if (jump
->flags
& IS_COND
)
191 code_ptr
[-5] -= (2 << 5);
192 code_ptr
[-2] = code_ptr
[0];
196 if (target_addr
< 0x1000000000000l
) {
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 static SLJIT_INLINE sljit_sw
put_label_get_length(struct sljit_put_label
*put_label
, sljit_uw max_label
)
210 if (max_label
< 0x100000000l
) {
211 put_label
->flags
= 0;
215 if (max_label
< 0x1000000000000l
) {
216 put_label
->flags
= 1;
220 put_label
->flags
= 2;
224 SLJIT_API_FUNC_ATTRIBUTE
void* sljit_generate_code(struct sljit_compiler
*compiler
)
226 struct sljit_memory_fragment
*buf
;
233 sljit_sw executable_offset
;
237 struct sljit_label
*label
;
238 struct sljit_jump
*jump
;
239 struct sljit_const
*const_
;
240 struct sljit_put_label
*put_label
;
243 CHECK_PTR(check_sljit_generate_code(compiler
));
244 reverse_buf(compiler
);
246 code
= (sljit_ins
*)SLJIT_MALLOC_EXEC(compiler
->size
* sizeof(sljit_ins
), compiler
->exec_allocator_data
);
247 PTR_FAIL_WITH_EXEC_IF(code
);
253 executable_offset
= SLJIT_EXEC_OFFSET(code
);
255 label
= compiler
->labels
;
256 jump
= compiler
->jumps
;
257 const_
= compiler
->consts
;
258 put_label
= compiler
->put_labels
;
261 buf_ptr
= (sljit_ins
*)buf
->memory
;
262 buf_end
= buf_ptr
+ (buf
->used_size
>> 2);
264 *code_ptr
= *buf_ptr
++;
265 if (next_addr
== word_count
) {
266 SLJIT_ASSERT(!label
|| label
->size
>= word_count
);
267 SLJIT_ASSERT(!jump
|| jump
->addr
>= word_count
);
268 SLJIT_ASSERT(!const_
|| const_
->addr
>= word_count
);
269 SLJIT_ASSERT(!put_label
|| put_label
->addr
>= word_count
);
271 /* These structures are ordered by their address. */
272 if (label
&& label
->size
== word_count
) {
273 label
->addr
= (sljit_uw
)SLJIT_ADD_EXEC_OFFSET(code_ptr
, executable_offset
);
274 label
->size
= (sljit_uw
)(code_ptr
- code
);
277 if (jump
&& jump
->addr
== word_count
) {
278 jump
->addr
= (sljit_uw
)(code_ptr
- 4);
279 code_ptr
-= detect_jump_type(jump
, code_ptr
, code
, executable_offset
);
282 if (const_
&& const_
->addr
== word_count
) {
283 const_
->addr
= (sljit_uw
)code_ptr
;
284 const_
= const_
->next
;
286 if (put_label
&& put_label
->addr
== word_count
) {
287 SLJIT_ASSERT(put_label
->label
);
288 put_label
->addr
= (sljit_uw
)(code_ptr
- 3);
289 code_ptr
-= put_label_get_length(put_label
, (sljit_uw
)(SLJIT_ADD_EXEC_OFFSET(code
, executable_offset
) + put_label
->label
->size
));
290 put_label
= put_label
->next
;
292 next_addr
= compute_next_addr(label
, jump
, const_
, put_label
);
296 } while (buf_ptr
< buf_end
);
301 if (label
&& label
->size
== word_count
) {
302 label
->addr
= (sljit_uw
)SLJIT_ADD_EXEC_OFFSET(code_ptr
, executable_offset
);
303 label
->size
= (sljit_uw
)(code_ptr
- code
);
307 SLJIT_ASSERT(!label
);
309 SLJIT_ASSERT(!const_
);
310 SLJIT_ASSERT(!put_label
);
311 SLJIT_ASSERT(code_ptr
- code
<= (sljit_sw
)compiler
->size
);
313 jump
= compiler
->jumps
;
316 addr
= (sljit_sw
)((jump
->flags
& JUMP_LABEL
) ? jump
->u
.label
->addr
: jump
->u
.target
);
317 buf_ptr
= (sljit_ins
*)jump
->addr
;
319 if (jump
->flags
& PATCH_B
) {
320 addr
= (addr
- (sljit_sw
)SLJIT_ADD_EXEC_OFFSET(buf_ptr
, executable_offset
)) >> 2;
321 SLJIT_ASSERT(addr
<= 0x1ffffff && addr
>= -0x2000000);
322 buf_ptr
[0] = ((jump
->flags
& IS_BL
) ? BL
: B
) | (sljit_ins
)(addr
& 0x3ffffff);
323 if (jump
->flags
& IS_COND
)
324 buf_ptr
[-1] -= (4 << 5);
327 if (jump
->flags
& PATCH_COND
) {
328 addr
= (addr
- (sljit_sw
)SLJIT_ADD_EXEC_OFFSET(buf_ptr
, executable_offset
)) >> 2;
329 SLJIT_ASSERT(addr
<= 0x3ffff && addr
>= -0x40000);
330 buf_ptr
[0] = (buf_ptr
[0] & ~(sljit_ins
)0xffffe0) | (sljit_ins
)((addr
& 0x7ffff) << 5);
334 SLJIT_ASSERT((jump
->flags
& (PATCH_ABS48
| PATCH_ABS64
)) || (sljit_uw
)addr
<= (sljit_uw
)0xffffffff);
335 SLJIT_ASSERT((jump
->flags
& PATCH_ABS64
) || (sljit_uw
)addr
<= (sljit_uw
)0xffffffffffff);
337 dst
= buf_ptr
[0] & 0x1f;
338 buf_ptr
[0] = MOVZ
| dst
| (((sljit_ins
)addr
& 0xffff) << 5);
339 buf_ptr
[1] = MOVK
| dst
| (((sljit_ins
)(addr
>> 16) & 0xffff) << 5) | (1 << 21);
340 if (jump
->flags
& (PATCH_ABS48
| PATCH_ABS64
))
341 buf_ptr
[2] = MOVK
| dst
| (((sljit_ins
)(addr
>> 32) & 0xffff) << 5) | (2 << 21);
342 if (jump
->flags
& PATCH_ABS64
)
343 buf_ptr
[3] = MOVK
| dst
| ((sljit_ins
)(addr
>> 48) << 5) | (3 << 21);
348 put_label
= compiler
->put_labels
;
350 addr
= (sljit_sw
)put_label
->label
->addr
;
351 buf_ptr
= (sljit_ins
*)put_label
->addr
;
353 buf_ptr
[0] |= ((sljit_ins
)addr
& 0xffff) << 5;
354 buf_ptr
[1] |= ((sljit_ins
)(addr
>> 16) & 0xffff) << 5;
356 if (put_label
->flags
>= 1)
357 buf_ptr
[2] |= ((sljit_ins
)(addr
>> 32) & 0xffff) << 5;
359 if (put_label
->flags
>= 2)
360 buf_ptr
[3] |= (sljit_ins
)(addr
>> 48) << 5;
362 put_label
= put_label
->next
;
365 compiler
->error
= SLJIT_ERR_COMPILED
;
366 compiler
->executable_offset
= executable_offset
;
367 compiler
->executable_size
= (sljit_uw
)(code_ptr
- code
) * sizeof(sljit_ins
);
369 code
= (sljit_ins
*)SLJIT_ADD_EXEC_OFFSET(code
, executable_offset
);
370 code_ptr
= (sljit_ins
*)SLJIT_ADD_EXEC_OFFSET(code_ptr
, executable_offset
);
372 SLJIT_CACHE_FLUSH(code
, code_ptr
);
373 SLJIT_UPDATE_WX_FLAGS(code
, code_ptr
, 1);
377 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_has_cpu_feature(sljit_s32 feature_type
)
379 switch (feature_type
) {
381 #ifdef SLJIT_IS_FPU_AVAILABLE
382 return SLJIT_IS_FPU_AVAILABLE
;
384 /* Available by default. */
390 case SLJIT_HAS_PREFETCH
:
398 /* --------------------------------------------------------------------- */
399 /* Core code generator functions. */
400 /* --------------------------------------------------------------------- */
402 #define COUNT_TRAILING_ZERO(value, result) \
404 if (!(value & 0xffffffff)) { \
408 if (!(value & 0xffff)) { \
412 if (!(value & 0xff)) { \
416 if (!(value & 0xf)) { \
420 if (!(value & 0x3)) { \
424 if (!(value & 0x1)) { \
429 #define LOGICAL_IMM_CHECK (sljit_ins)0x100
431 static sljit_ins
logical_imm(sljit_sw imm
, sljit_u32 len
)
434 sljit_u32 ones
, right
;
438 if (len
& LOGICAL_IMM_CHECK
) {
439 len
&= ~LOGICAL_IMM_CHECK
;
440 if (len
== 32 && (imm
== 0 || imm
== -1))
442 if (len
== 16 && ((sljit_s32
)imm
== 0 || (sljit_s32
)imm
== -1))
446 SLJIT_ASSERT((len
== 32 && imm
!= 0 && imm
!= -1)
447 || (len
== 16 && (sljit_s32
)imm
!= 0 && (sljit_s32
)imm
!= -1));
449 uimm
= (sljit_uw
)imm
;
456 mask
= ((sljit_uw
)1 << len
) - 1;
457 if ((uimm
& mask
) != ((uimm
>> len
) & mask
))
471 uimm
&= ((sljit_uw
)1 << len
) - 1;
473 /* Unsigned right shift. */
474 COUNT_TRAILING_ZERO(uimm
, right
);
476 /* Signed shift. We also know that the highest bit is set. */
477 imm
= (sljit_sw
)~uimm
;
478 SLJIT_ASSERT(imm
< 0);
480 COUNT_TRAILING_ZERO(imm
, ones
);
488 ins
= (0x3f - ((len
<< 1) - 1)) << 10;
491 return ins
| ((len
- ones
- 1) << 10) | ((len
- ones
- right
) << 16);
493 return ins
| ((ones
- 1) << 10) | ((len
- right
) << 16);
496 #undef COUNT_TRAILING_ZERO
498 static sljit_s32
load_immediate(struct sljit_compiler
*compiler
, sljit_s32 dst
, sljit_sw simm
)
500 sljit_uw imm
= (sljit_uw
)simm
;
501 sljit_u32 i
, zeros
, ones
, first
;
504 /* Handling simple immediates first. */
506 return push_inst(compiler
, MOVZ
| RD(dst
) | ((sljit_ins
)imm
<< 5));
508 if (simm
< 0 && simm
>= -0x10000)
509 return push_inst(compiler
, MOVN
| RD(dst
) | (((sljit_ins
)~imm
& 0xffff) << 5));
511 if (imm
<= 0xffffffffl
) {
512 if ((imm
& 0xffff) == 0)
513 return push_inst(compiler
, MOVZ
| RD(dst
) | ((sljit_ins
)(imm
>> 16) << 5) | (1 << 21));
514 if ((imm
& 0xffff0000l
) == 0xffff0000)
515 return push_inst(compiler
, (MOVN
^ W_OP
) | RD(dst
) | (((sljit_ins
)~imm
& 0xffff) << 5));
516 if ((imm
& 0xffff) == 0xffff)
517 return push_inst(compiler
, (MOVN
^ W_OP
) | RD(dst
) | (((sljit_ins
)~imm
& 0xffff0000u
) >> (16 - 5)) | (1 << 21));
519 bitmask
= logical_imm(simm
, 16);
521 return push_inst(compiler
, (ORRI
^ W_OP
) | RD(dst
) | RN(TMP_ZERO
) | bitmask
);
523 FAIL_IF(push_inst(compiler
, MOVZ
| RD(dst
) | (((sljit_ins
)imm
& 0xffff) << 5)));
524 return push_inst(compiler
, MOVK
| RD(dst
) | (((sljit_ins
)imm
& 0xffff0000u
) >> (16 - 5)) | (1 << 21));
527 bitmask
= logical_imm(simm
, 32);
529 return push_inst(compiler
, ORRI
| RD(dst
) | RN(TMP_ZERO
) | bitmask
);
531 if (simm
< 0 && simm
>= -0x100000000l
) {
532 if ((imm
& 0xffff) == 0xffff)
533 return push_inst(compiler
, MOVN
| RD(dst
) | (((sljit_ins
)~imm
& 0xffff0000u
) >> (16 - 5)) | (1 << 21));
535 FAIL_IF(push_inst(compiler
, MOVN
| RD(dst
) | (((sljit_ins
)~imm
& 0xffff) << 5)));
536 return push_inst(compiler
, MOVK
| RD(dst
) | (((sljit_ins
)imm
& 0xffff0000u
) >> (16 - 5)) | (1 << 21));
539 /* A large amount of number can be constructed from ORR and MOVx, but computing them is costly. */
543 for (i
= 4; i
> 0; i
--) {
544 if ((simm
& 0xffff) == 0)
546 if ((simm
& 0xffff) == 0xffff)
551 simm
= (sljit_sw
)imm
;
555 for (i
= 0; i
< 4; i
++) {
556 if (!(simm
& 0xffff)) {
562 FAIL_IF(push_inst(compiler
, MOVN
| RD(dst
) | (((sljit_ins
)simm
& 0xffff) << 5) | (i
<< 21)));
565 FAIL_IF(push_inst(compiler
, MOVK
| RD(dst
) | (((sljit_ins
)~simm
& 0xffff) << 5) | (i
<< 21)));
568 return SLJIT_SUCCESS
;
571 for (i
= 0; i
< 4; i
++) {
572 if (!(simm
& 0xffff)) {
578 FAIL_IF(push_inst(compiler
, MOVZ
| RD(dst
) | (((sljit_ins
)simm
& 0xffff) << 5) | (i
<< 21)));
581 FAIL_IF(push_inst(compiler
, MOVK
| RD(dst
) | (((sljit_ins
)simm
& 0xffff) << 5) | (i
<< 21)));
584 return SLJIT_SUCCESS
;
587 #define ARG1_IMM 0x0010000
588 #define ARG2_IMM 0x0020000
589 #define INT_OP 0x0040000
590 #define SET_FLAGS 0x0080000
591 #define UNUSED_RETURN 0x0100000
593 #define CHECK_FLAGS(flag_bits) \
594 if (flags & SET_FLAGS) { \
595 inv_bits |= flag_bits; \
596 if (flags & UNUSED_RETURN) \
600 static sljit_s32
emit_op_imm(struct sljit_compiler
*compiler
, sljit_s32 flags
, sljit_s32 dst
, sljit_sw arg1
, sljit_sw arg2
)
602 /* dst must be register, TMP_REG1
603 arg1 must be register, TMP_REG1, imm
604 arg2 must be register, TMP_REG2, imm */
605 sljit_ins inv_bits
= (flags
& INT_OP
) ? W_OP
: 0;
607 sljit_s32 op
= (flags
& 0xffff);
611 if (SLJIT_UNLIKELY((flags
& (ARG1_IMM
| ARG2_IMM
)) == (ARG1_IMM
| ARG2_IMM
))) {
612 /* Both are immediates. */
614 if (arg1
== 0 && op
!= SLJIT_ADD
&& op
!= SLJIT_SUB
)
617 FAIL_IF(load_immediate(compiler
, TMP_REG1
, arg1
));
622 if (flags
& (ARG1_IMM
| ARG2_IMM
)) {
623 reg
= (sljit_s32
)((flags
& ARG2_IMM
) ? arg1
: arg2
);
624 imm
= (flags
& ARG2_IMM
) ? arg2
: arg1
;
632 /* No form with immediate operand (except imm 0, which
633 is represented by a ZERO register). */
636 SLJIT_ASSERT(!(flags
& SET_FLAGS
) && (flags
& ARG2_IMM
) && arg1
== TMP_REG1
);
637 return load_immediate(compiler
, dst
, imm
);
639 SLJIT_ASSERT(flags
& ARG2_IMM
);
640 FAIL_IF(load_immediate(compiler
, dst
, (flags
& INT_OP
) ? (~imm
& 0xffffffff) : ~imm
));
643 if (flags
& ARG1_IMM
)
648 compiler
->status_flags_state
= SLJIT_CURRENT_FLAGS_ADD_SUB
;
650 CHECK_FLAGS(1 << 29);
651 return push_inst(compiler
, ((op
== SLJIT_ADD
? ADDI
: SUBI
) ^ inv_bits
) | RD(dst
) | RN(reg
));
653 if (imm
> 0 && imm
<= 0xfff) {
654 CHECK_FLAGS(1 << 29);
655 return push_inst(compiler
, (ADDI
^ inv_bits
) | RD(dst
) | RN(reg
) | ((sljit_ins
)imm
<< 10));
658 if (nimm
> 0 && nimm
<= 0xfff) {
659 CHECK_FLAGS(1 << 29);
660 return push_inst(compiler
, (SUBI
^ inv_bits
) | RD(dst
) | RN(reg
) | ((sljit_ins
)nimm
<< 10));
662 if (imm
> 0 && imm
<= 0xffffff && !(imm
& 0xfff)) {
663 CHECK_FLAGS(1 << 29);
664 return push_inst(compiler
, (ADDI
^ inv_bits
) | RD(dst
) | RN(reg
) | (((sljit_ins
)imm
>> 12) << 10) | (1 << 22));
666 if (nimm
> 0 && nimm
<= 0xffffff && !(nimm
& 0xfff)) {
667 CHECK_FLAGS(1 << 29);
668 return push_inst(compiler
, (SUBI
^ inv_bits
) | RD(dst
) | RN(reg
) | (((sljit_ins
)nimm
>> 12) << 10) | (1 << 22));
670 if (imm
> 0 && imm
<= 0xffffff && !(flags
& SET_FLAGS
)) {
671 FAIL_IF(push_inst(compiler
, (ADDI
^ inv_bits
) | RD(dst
) | RN(reg
) | (((sljit_ins
)imm
>> 12) << 10) | (1 << 22)));
672 return push_inst(compiler
, (ADDI
^ inv_bits
) | RD(dst
) | RN(dst
) | (((sljit_ins
)imm
& 0xfff) << 10));
674 if (nimm
> 0 && nimm
<= 0xffffff && !(flags
& SET_FLAGS
)) {
675 FAIL_IF(push_inst(compiler
, (SUBI
^ inv_bits
) | RD(dst
) | RN(reg
) | (((sljit_ins
)nimm
>> 12) << 10) | (1 << 22)));
676 return push_inst(compiler
, (SUBI
^ inv_bits
) | RD(dst
) | RN(dst
) | (((sljit_ins
)nimm
& 0xfff) << 10));
680 inst_bits
= logical_imm(imm
, LOGICAL_IMM_CHECK
| ((flags
& INT_OP
) ? 16 : 32));
683 CHECK_FLAGS(3 << 29);
684 return push_inst(compiler
, (ANDI
^ inv_bits
) | RD(dst
) | RN(reg
) | inst_bits
);
687 inst_bits
= logical_imm(imm
, LOGICAL_IMM_CHECK
| ((flags
& INT_OP
) ? 16 : 32));
694 FAIL_IF(push_inst(compiler
, (inst_bits
^ inv_bits
) | RD(dst
) | RN(reg
)));
697 if (flags
& ARG1_IMM
)
699 if (flags
& INT_OP
) {
701 FAIL_IF(push_inst(compiler
, (UBFM
^ inv_bits
) | RD(dst
) | RN(arg1
)
702 | (((sljit_ins
)-imm
& 0x1f) << 16) | ((31 - (sljit_ins
)imm
) << 10)));
706 FAIL_IF(push_inst(compiler
, (UBFM
^ inv_bits
) | RD(dst
) | RN(arg1
) | (1 << 22)
707 | (((sljit_ins
)-imm
& 0x3f) << 16) | ((63 - (sljit_ins
)imm
) << 10)));
712 if (flags
& ARG1_IMM
)
714 if (op
== SLJIT_ASHR
)
716 if (flags
& INT_OP
) {
718 FAIL_IF(push_inst(compiler
, (UBFM
^ inv_bits
) | RD(dst
) | RN(arg1
)
719 | ((sljit_ins
)imm
<< 16) | (31 << 10)));
723 FAIL_IF(push_inst(compiler
, (UBFM
^ inv_bits
) | RD(dst
) | RN(arg1
)
724 | (1 << 22) | ((sljit_ins
)imm
<< 16) | (63 << 10)));
732 if (flags
& ARG2_IMM
) {
736 FAIL_IF(load_immediate(compiler
, TMP_REG2
, arg2
));
744 FAIL_IF(load_immediate(compiler
, TMP_REG1
, arg1
));
750 /* Both arguments are registers. */
754 SLJIT_ASSERT(!(flags
& SET_FLAGS
) && arg1
== TMP_REG1
);
756 return SLJIT_SUCCESS
;
757 return push_inst(compiler
, ORR
| RD(dst
) | RN(TMP_ZERO
) | RM(arg2
));
759 SLJIT_ASSERT(!(flags
& SET_FLAGS
) && arg1
== TMP_REG1
);
760 return push_inst(compiler
, (UBFM
^ W_OP
) | RD(dst
) | RN(arg2
) | (7 << 10));
762 SLJIT_ASSERT(!(flags
& SET_FLAGS
) && arg1
== TMP_REG1
);
763 if (!(flags
& INT_OP
))
765 return push_inst(compiler
, (SBFM
^ inv_bits
) | RD(dst
) | RN(arg2
) | (7 << 10));
767 SLJIT_ASSERT(!(flags
& SET_FLAGS
) && arg1
== TMP_REG1
);
768 return push_inst(compiler
, (UBFM
^ W_OP
) | RD(dst
) | RN(arg2
) | (15 << 10));
770 SLJIT_ASSERT(!(flags
& SET_FLAGS
) && arg1
== TMP_REG1
);
771 if (!(flags
& INT_OP
))
773 return push_inst(compiler
, (SBFM
^ inv_bits
) | RD(dst
) | RN(arg2
) | (15 << 10));
775 SLJIT_ASSERT(!(flags
& SET_FLAGS
) && arg1
== TMP_REG1
);
777 return SLJIT_SUCCESS
;
780 SLJIT_ASSERT(!(flags
& SET_FLAGS
) && arg1
== TMP_REG1
);
781 return push_inst(compiler
, (ORR
^ W_OP
) | RD(dst
) | RN(TMP_ZERO
) | RM(arg2
));
783 SLJIT_ASSERT(!(flags
& SET_FLAGS
) && arg1
== TMP_REG1
);
784 return push_inst(compiler
, SBFM
| (1 << 22) | RD(dst
) | RN(arg2
) | (31 << 10));
786 SLJIT_ASSERT(arg1
== TMP_REG1
);
787 FAIL_IF(push_inst(compiler
, (ORN
^ inv_bits
) | RD(dst
) | RN(TMP_ZERO
) | RM(arg2
)));
788 break; /* Set flags. */
790 SLJIT_ASSERT(arg1
== TMP_REG1
);
791 compiler
->status_flags_state
= SLJIT_CURRENT_FLAGS_ADD_SUB
;
792 if (flags
& SET_FLAGS
)
794 return push_inst(compiler
, (SUB
^ inv_bits
) | RD(dst
) | RN(TMP_ZERO
) | RM(arg2
));
796 SLJIT_ASSERT(arg1
== TMP_REG1
);
797 return push_inst(compiler
, (CLZ
^ inv_bits
) | RD(dst
) | RN(arg2
));
799 CHECK_FLAGS(1 << 29);
800 compiler
->status_flags_state
= SLJIT_CURRENT_FLAGS_ADD_SUB
;
801 return push_inst(compiler
, (ADD
^ inv_bits
) | RD(dst
) | RN(arg1
) | RM(arg2
));
803 CHECK_FLAGS(1 << 29);
804 return push_inst(compiler
, (ADC
^ inv_bits
) | RD(dst
) | RN(arg1
) | RM(arg2
));
806 CHECK_FLAGS(1 << 29);
807 compiler
->status_flags_state
= SLJIT_CURRENT_FLAGS_ADD_SUB
;
808 return push_inst(compiler
, (SUB
^ inv_bits
) | RD(dst
) | RN(arg1
) | RM(arg2
));
810 CHECK_FLAGS(1 << 29);
811 return push_inst(compiler
, (SBC
^ inv_bits
) | RD(dst
) | RN(arg1
) | RM(arg2
));
813 compiler
->status_flags_state
= 0;
814 if (!(flags
& SET_FLAGS
))
815 return push_inst(compiler
, (MADD
^ inv_bits
) | RD(dst
) | RN(arg1
) | RM(arg2
) | RT2(TMP_ZERO
));
816 if (flags
& INT_OP
) {
817 FAIL_IF(push_inst(compiler
, SMADDL
| RD(dst
) | RN(arg1
) | RM(arg2
) | (31 << 10)));
818 FAIL_IF(push_inst(compiler
, ADD
| RD(TMP_LR
) | RN(TMP_ZERO
) | RM(dst
) | (2 << 22) | (31 << 10)));
819 return push_inst(compiler
, SUBS
| RD(TMP_ZERO
) | RN(TMP_LR
) | RM(dst
) | (2 << 22) | (63 << 10));
821 FAIL_IF(push_inst(compiler
, SMULH
| RD(TMP_LR
) | RN(arg1
) | RM(arg2
)));
822 FAIL_IF(push_inst(compiler
, MADD
| RD(dst
) | RN(arg1
) | RM(arg2
) | RT2(TMP_ZERO
)));
823 return push_inst(compiler
, SUBS
| RD(TMP_ZERO
) | RN(TMP_LR
) | RM(dst
) | (2 << 22) | (63 << 10));
825 CHECK_FLAGS(3 << 29);
826 return push_inst(compiler
, (AND
^ inv_bits
) | RD(dst
) | RN(arg1
) | RM(arg2
));
828 FAIL_IF(push_inst(compiler
, (ORR
^ inv_bits
) | RD(dst
) | RN(arg1
) | RM(arg2
)));
829 break; /* Set flags. */
831 FAIL_IF(push_inst(compiler
, (EOR
^ inv_bits
) | RD(dst
) | RN(arg1
) | RM(arg2
)));
832 break; /* Set flags. */
834 FAIL_IF(push_inst(compiler
, (LSLV
^ inv_bits
) | RD(dst
) | RN(arg1
) | RM(arg2
)));
835 break; /* Set flags. */
837 FAIL_IF(push_inst(compiler
, (LSRV
^ inv_bits
) | RD(dst
) | RN(arg1
) | RM(arg2
)));
838 break; /* Set flags. */
840 FAIL_IF(push_inst(compiler
, (ASRV
^ inv_bits
) | RD(dst
) | RN(arg1
) | RM(arg2
)));
841 break; /* Set flags. */
844 return SLJIT_SUCCESS
;
848 if (flags
& SET_FLAGS
)
849 return push_inst(compiler
, (SUBS
^ inv_bits
) | RD(TMP_ZERO
) | RN(dst
) | RM(TMP_ZERO
));
850 return SLJIT_SUCCESS
;
856 #define BYTE_SIZE 0x0
857 #define HALF_SIZE 0x1
859 #define WORD_SIZE 0x3
861 #define MEM_SIZE_SHIFT(flags) ((sljit_ins)(flags) & 0x3)
863 static sljit_s32
emit_op_mem(struct sljit_compiler
*compiler
, sljit_s32 flags
, sljit_s32 reg
,
864 sljit_s32 arg
, sljit_sw argw
, sljit_s32 tmp_reg
)
866 sljit_u32 shift
= MEM_SIZE_SHIFT(flags
);
867 sljit_u32 type
= (shift
<< 30);
869 if (!(flags
& STORE
))
870 type
|= (flags
& SIGNED
) ? 0x00800000 : 0x00400000;
872 SLJIT_ASSERT(arg
& SLJIT_MEM
);
874 if (SLJIT_UNLIKELY(arg
& OFFS_REG_MASK
)) {
877 if (argw
== 0 || argw
== shift
)
878 return push_inst(compiler
, STRB
| type
| RT(reg
)
879 | RN(arg
& REG_MASK
) | RM(OFFS_REG(arg
)) | (argw
? (1 << 12) : 0));
881 FAIL_IF(push_inst(compiler
, ADD
| RD(tmp_reg
) | RN(arg
& REG_MASK
) | RM(OFFS_REG(arg
)) | ((sljit_ins
)argw
<< 10)));
882 return push_inst(compiler
, STRBI
| type
| RT(reg
) | RN(tmp_reg
));
888 FAIL_IF(load_immediate(compiler
, tmp_reg
, argw
& ~(0xfff << shift
)));
890 argw
= (argw
>> shift
) & 0xfff;
892 return push_inst(compiler
, STRBI
| type
| RT(reg
) | RN(tmp_reg
) | ((sljit_ins
)argw
<< 10));
895 if (argw
>= 0 && (argw
& ((1 << shift
) - 1)) == 0) {
896 if ((argw
>> shift
) <= 0xfff)
897 return push_inst(compiler
, STRBI
| type
| RT(reg
) | RN(arg
) | ((sljit_ins
)argw
<< (10 - shift
)));
899 if (argw
<= 0xffffff) {
900 FAIL_IF(push_inst(compiler
, ADDI
| (1 << 22) | RD(tmp_reg
) | RN(arg
) | (((sljit_ins
)argw
>> 12) << 10)));
902 argw
= ((argw
& 0xfff) >> shift
);
903 return push_inst(compiler
, STRBI
| type
| RT(reg
) | RN(tmp_reg
) | ((sljit_ins
)argw
<< 10));
907 if (argw
<= 255 && argw
>= -256)
908 return push_inst(compiler
, STURBI
| type
| RT(reg
) | RN(arg
) | (((sljit_ins
)argw
& 0x1ff) << 12));
910 FAIL_IF(load_immediate(compiler
, tmp_reg
, argw
));
912 return push_inst(compiler
, STRB
| type
| RT(reg
) | RN(arg
) | RM(tmp_reg
));
915 /* --------------------------------------------------------------------- */
917 /* --------------------------------------------------------------------- */
919 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_enter(struct sljit_compiler
*compiler
,
920 sljit_s32 options
, sljit_s32 arg_types
, sljit_s32 scratches
, sljit_s32 saveds
,
921 sljit_s32 fscratches
, sljit_s32 fsaveds
, sljit_s32 local_size
)
923 sljit_s32 prev
, saved_regs_size
, i
, tmp
;
924 sljit_s32 word_arg_count
= 0;
928 CHECK(check_sljit_emit_enter(compiler
, options
, arg_types
, scratches
, saveds
, fscratches
, fsaveds
, local_size
));
929 set_emit_enter(compiler
, options
, arg_types
, scratches
, saveds
, fscratches
, fsaveds
, local_size
);
931 saved_regs_size
= GET_SAVED_REGISTERS_SIZE(scratches
, saveds
, 2);
932 if (saved_regs_size
& 0x8)
933 saved_regs_size
+= SSIZE_OF(sw
);
935 local_size
= (local_size
+ 15) & ~0xf;
936 compiler
->local_size
= local_size
+ saved_regs_size
;
938 FAIL_IF(push_inst(compiler
, STP_PRE
| RT(TMP_FP
) | RT2(TMP_LR
)
939 | RN(SLJIT_SP
) | (sljit_ins
)((-(saved_regs_size
>> 3) & 0x7f) << 15)));
942 if (local_size
>= 4096)
943 FAIL_IF(push_inst(compiler
, SUBI
| RD(TMP_REG1
) | RN(SLJIT_SP
) | (1 << 10) | (1 << 22)));
944 else if (local_size
> 256)
945 FAIL_IF(push_inst(compiler
, SUBI
| RD(TMP_REG1
) | RN(SLJIT_SP
) | ((sljit_ins
)local_size
<< 10)));
948 tmp
= saveds
< SLJIT_NUMBER_OF_SAVED_REGISTERS
? (SLJIT_S0
+ 1 - saveds
) : SLJIT_FIRST_SAVED_REG
;
951 for (i
= SLJIT_S0
; i
>= tmp
; i
--) {
956 FAIL_IF(push_inst(compiler
, STP
| RT(prev
) | RT2(i
) | RN(SLJIT_SP
) | offs
));
961 for (i
= scratches
; i
>= SLJIT_FIRST_SAVED_REG
; i
--) {
966 FAIL_IF(push_inst(compiler
, STP
| RT(prev
) | RT2(i
) | RN(SLJIT_SP
) | offs
));
972 FAIL_IF(push_inst(compiler
, STRI
| RT(prev
) | RN(SLJIT_SP
) | (offs
>> 5)));
974 FAIL_IF(push_inst(compiler
, ADDI
| RD(TMP_FP
) | RN(SLJIT_SP
) | (0 << 10)));
976 arg_types
>>= SLJIT_ARG_SHIFT
;
978 while (arg_types
> 0) {
979 if ((arg_types
& SLJIT_ARG_MASK
) < SLJIT_ARG_TYPE_F64
) {
980 FAIL_IF(push_inst(compiler
, ORR
| RD(SLJIT_S0
- word_arg_count
) | RN(TMP_ZERO
) | RM(SLJIT_R0
+ word_arg_count
)));
983 arg_types
>>= SLJIT_ARG_SHIFT
;
987 if (local_size
>= 4096) {
988 if (local_size
< 4 * 4096) {
989 /* No need for a loop. */
990 if (local_size
>= 2 * 4096) {
991 FAIL_IF(push_inst(compiler
, LDRI
| RT(TMP_ZERO
) | RN(TMP_REG1
)));
992 FAIL_IF(push_inst(compiler
, SUBI
| RD(TMP_REG1
) | RN(TMP_REG1
) | (1 << 10) | (1 << 22)));
996 if (local_size
>= 2 * 4096) {
997 FAIL_IF(push_inst(compiler
, LDRI
| RT(TMP_ZERO
) | RN(TMP_REG1
)));
998 FAIL_IF(push_inst(compiler
, SUBI
| RD(TMP_REG1
) | RN(TMP_REG1
) | (1 << 10) | (1 << 22)));
1002 FAIL_IF(push_inst(compiler
, LDRI
| RT(TMP_ZERO
) | RN(TMP_REG1
)));
1006 FAIL_IF(push_inst(compiler
, MOVZ
| RD(TMP_REG2
) | ((((sljit_ins
)local_size
>> 12) - 1) << 5)));
1007 FAIL_IF(push_inst(compiler
, LDRI
| RT(TMP_ZERO
) | RN(TMP_REG1
)));
1008 FAIL_IF(push_inst(compiler
, SUBI
| RD(TMP_REG1
) | RN(TMP_REG1
) | (1 << 10) | (1 << 22)));
1009 FAIL_IF(push_inst(compiler
, SUBI
| (1 << 29) | RD(TMP_REG2
) | RN(TMP_REG2
) | (1 << 10)));
1010 FAIL_IF(push_inst(compiler
, B_CC
| ((((sljit_ins
) -3) & 0x7ffff) << 5) | 0x1 /* not-equal */));
1011 FAIL_IF(push_inst(compiler
, LDRI
| RT(TMP_ZERO
) | RN(TMP_REG1
)));
1013 local_size
&= 0xfff;
1016 if (local_size
> 256) {
1017 FAIL_IF(push_inst(compiler
, SUBI
| RD(TMP_REG1
) | RN(TMP_REG1
) | ((sljit_ins
)local_size
<< 10)));
1018 FAIL_IF(push_inst(compiler
, LDRI
| RT(TMP_ZERO
) | RN(TMP_REG1
)));
1020 else if (local_size
> 0)
1021 FAIL_IF(push_inst(compiler
, LDR_PRE
| RT(TMP_ZERO
) | RN(TMP_REG1
) | (((sljit_ins
)-local_size
& 0x1ff) << 12)));
1023 FAIL_IF(push_inst(compiler
, ADDI
| RD(SLJIT_SP
) | RN(TMP_REG1
) | (0 << 10)));
1025 else if (local_size
> 256) {
1026 FAIL_IF(push_inst(compiler
, LDRI
| RT(TMP_ZERO
) | RN(TMP_REG1
)));
1027 FAIL_IF(push_inst(compiler
, ADDI
| RD(SLJIT_SP
) | RN(TMP_REG1
) | (0 << 10)));
1029 else if (local_size
> 0)
1030 FAIL_IF(push_inst(compiler
, LDR_PRE
| RT(TMP_ZERO
) | RN(SLJIT_SP
) | (((sljit_ins
)-local_size
& 0x1ff) << 12)));
1034 /* The local_size does not include saved registers size. */
1035 if (local_size
> 0xfff) {
1036 FAIL_IF(push_inst(compiler
, SUBI
| RD(SLJIT_SP
) | RN(SLJIT_SP
) | (((sljit_ins
)local_size
>> 12) << 10) | (1 << 22)));
1037 local_size
&= 0xfff;
1039 if (local_size
!= 0)
1040 FAIL_IF(push_inst(compiler
, SUBI
| RD(SLJIT_SP
) | RN(SLJIT_SP
) | ((sljit_ins
)local_size
<< 10)));
1044 return SLJIT_SUCCESS
;
1047 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_set_context(struct sljit_compiler
*compiler
,
1048 sljit_s32 options
, sljit_s32 arg_types
, sljit_s32 scratches
, sljit_s32 saveds
,
1049 sljit_s32 fscratches
, sljit_s32 fsaveds
, sljit_s32 local_size
)
1051 sljit_s32 saved_regs_size
;
1054 CHECK(check_sljit_set_context(compiler
, options
, arg_types
, scratches
, saveds
, fscratches
, fsaveds
, local_size
));
1055 set_set_context(compiler
, options
, arg_types
, scratches
, saveds
, fscratches
, fsaveds
, local_size
);
1057 saved_regs_size
= GET_SAVED_REGISTERS_SIZE(scratches
, saveds
, 2);
1058 if (saved_regs_size
& 0x8)
1059 saved_regs_size
+= SSIZE_OF(sw
);
1061 compiler
->local_size
= saved_regs_size
+ ((local_size
+ 15) & ~0xf);
1062 return SLJIT_SUCCESS
;
1065 static sljit_s32
emit_stack_frame_release(struct sljit_compiler
*compiler
)
1067 sljit_s32 local_size
;
1068 sljit_s32 prev
, saved_regs_size
, i
, tmp
;
1071 saved_regs_size
= GET_SAVED_REGISTERS_SIZE(compiler
->scratches
, compiler
->saveds
, 2);
1072 if (saved_regs_size
& 0x8)
1073 saved_regs_size
+= SSIZE_OF(sw
);
1075 SLJIT_ASSERT((saved_regs_size
& 0xf) == 0);
1077 local_size
= compiler
->local_size
- saved_regs_size
;
1079 /* Load LR as early as possible. */
1080 if (local_size
> 0xfff) {
1081 FAIL_IF(push_inst(compiler
, ADDI
| RD(SLJIT_SP
) | RN(SLJIT_SP
) | (((sljit_ins
)local_size
>> 12) << 10) | (1 << 22)));
1082 local_size
&= 0xfff;
1085 if (local_size
== 0)
1086 FAIL_IF(push_inst(compiler
, LDP
| RT(TMP_FP
) | RT2(TMP_LR
) | RN(SLJIT_SP
)));
1087 else if (local_size
<= 63 * SSIZE_OF(sw
)) {
1088 FAIL_IF(push_inst(compiler
, LDP_PRE
| RT(TMP_FP
) | RT2(TMP_LR
)
1089 | RN(SLJIT_SP
) | ((sljit_ins
)local_size
<< (15 - 3))));
1092 FAIL_IF(push_inst(compiler
, ADDI
| RD(SLJIT_SP
) | RN(SLJIT_SP
) | ((sljit_ins
)local_size
<< 10)));
1093 FAIL_IF(push_inst(compiler
, LDP
| RT(TMP_FP
) | RT2(TMP_LR
) | RN(SLJIT_SP
)));
1096 tmp
= compiler
->saveds
< SLJIT_NUMBER_OF_SAVED_REGISTERS
? (SLJIT_S0
+ 1 - compiler
->saveds
) : SLJIT_FIRST_SAVED_REG
;
1099 for (i
= SLJIT_S0
; i
>= tmp
; i
--) {
1104 FAIL_IF(push_inst(compiler
, LDP
| RT(prev
) | RT2(i
) | RN(SLJIT_SP
) | offs
));
1109 for (i
= compiler
->scratches
; i
>= SLJIT_FIRST_SAVED_REG
; i
--) {
1114 FAIL_IF(push_inst(compiler
, LDP
| RT(prev
) | RT2(i
) | RN(SLJIT_SP
) | offs
));
1120 FAIL_IF(push_inst(compiler
, LDRI
| RT(prev
) | RN(SLJIT_SP
) | (offs
>> 5)));
1122 /* This and the next call/jump instruction can be executed parallel. */
1123 return push_inst(compiler
, ADDI
| RD(SLJIT_SP
) | RN(SLJIT_SP
) | (sljit_ins
)(saved_regs_size
<< 10));
1126 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_return_void(struct sljit_compiler
*compiler
)
1129 CHECK(check_sljit_emit_return_void(compiler
));
1131 FAIL_IF(emit_stack_frame_release(compiler
));
1133 return push_inst(compiler
, RET
| RN(TMP_LR
));
1136 /* --------------------------------------------------------------------- */
1138 /* --------------------------------------------------------------------- */
1140 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op0(struct sljit_compiler
*compiler
, sljit_s32 op
)
1142 sljit_ins inv_bits
= (op
& SLJIT_32
) ? W_OP
: 0;
1145 CHECK(check_sljit_emit_op0(compiler
, op
));
1147 op
= GET_OPCODE(op
);
1149 case SLJIT_BREAKPOINT
:
1150 return push_inst(compiler
, BRK
);
1152 return push_inst(compiler
, NOP
);
1155 FAIL_IF(push_inst(compiler
, ORR
| RD(TMP_REG1
) | RN(TMP_ZERO
) | RM(SLJIT_R0
)));
1156 FAIL_IF(push_inst(compiler
, MADD
| RD(SLJIT_R0
) | RN(SLJIT_R0
) | RM(SLJIT_R1
) | RT2(TMP_ZERO
)));
1157 return push_inst(compiler
, (op
== SLJIT_LMUL_UW
? UMULH
: SMULH
) | RD(SLJIT_R1
) | RN(TMP_REG1
) | RM(SLJIT_R1
));
1158 case SLJIT_DIVMOD_UW
:
1159 case SLJIT_DIVMOD_SW
:
1160 FAIL_IF(push_inst(compiler
, (ORR
^ inv_bits
) | RD(TMP_REG1
) | RN(TMP_ZERO
) | RM(SLJIT_R0
)));
1161 FAIL_IF(push_inst(compiler
, ((op
== SLJIT_DIVMOD_UW
? UDIV
: SDIV
) ^ inv_bits
) | RD(SLJIT_R0
) | RN(SLJIT_R0
) | RM(SLJIT_R1
)));
1162 FAIL_IF(push_inst(compiler
, (MADD
^ inv_bits
) | RD(SLJIT_R1
) | RN(SLJIT_R0
) | RM(SLJIT_R1
) | RT2(TMP_ZERO
)));
1163 return push_inst(compiler
, (SUB
^ inv_bits
) | RD(SLJIT_R1
) | RN(TMP_REG1
) | RM(SLJIT_R1
));
1166 return push_inst(compiler
, ((op
== SLJIT_DIV_UW
? UDIV
: SDIV
) ^ inv_bits
) | RD(SLJIT_R0
) | RN(SLJIT_R0
) | RM(SLJIT_R1
));
1168 case SLJIT_SKIP_FRAMES_BEFORE_RETURN
:
1169 return SLJIT_SUCCESS
;
1172 return SLJIT_SUCCESS
;
1175 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op1(struct sljit_compiler
*compiler
, sljit_s32 op
,
1176 sljit_s32 dst
, sljit_sw dstw
,
1177 sljit_s32 src
, sljit_sw srcw
)
1179 sljit_s32 dst_r
, flags
, mem_flags
;
1180 sljit_s32 op_flags
= GET_ALL_FLAGS(op
);
1183 CHECK(check_sljit_emit_op1(compiler
, op
, dst
, dstw
, src
, srcw
));
1184 ADJUST_LOCAL_OFFSET(dst
, dstw
);
1185 ADJUST_LOCAL_OFFSET(src
, srcw
);
1187 dst_r
= FAST_IS_REG(dst
) ? dst
: TMP_REG1
;
1189 op
= GET_OPCODE(op
);
1190 if (op
>= SLJIT_MOV
&& op
<= SLJIT_MOV_P
) {
1191 /* Both operands are registers. */
1192 if (dst_r
!= TMP_REG1
&& FAST_IS_REG(src
))
1193 return emit_op_imm(compiler
, op
| ((op_flags
& SLJIT_32
) ? INT_OP
: 0), dst_r
, TMP_REG1
, src
);
1198 mem_flags
= WORD_SIZE
;
1201 mem_flags
= BYTE_SIZE
;
1202 if (src
& SLJIT_IMM
)
1203 srcw
= (sljit_u8
)srcw
;
1206 mem_flags
= BYTE_SIZE
| SIGNED
;
1207 if (src
& SLJIT_IMM
)
1208 srcw
= (sljit_s8
)srcw
;
1211 mem_flags
= HALF_SIZE
;
1212 if (src
& SLJIT_IMM
)
1213 srcw
= (sljit_u16
)srcw
;
1216 mem_flags
= HALF_SIZE
| SIGNED
;
1217 if (src
& SLJIT_IMM
)
1218 srcw
= (sljit_s16
)srcw
;
1221 mem_flags
= INT_SIZE
;
1222 if (src
& SLJIT_IMM
)
1223 srcw
= (sljit_u32
)srcw
;
1227 mem_flags
= INT_SIZE
| SIGNED
;
1228 if (src
& SLJIT_IMM
)
1229 srcw
= (sljit_s32
)srcw
;
1232 SLJIT_UNREACHABLE();
1237 if (src
& SLJIT_IMM
)
1238 FAIL_IF(emit_op_imm(compiler
, SLJIT_MOV
| ARG2_IMM
, dst_r
, TMP_REG1
, srcw
));
1239 else if (!(src
& SLJIT_MEM
))
1242 FAIL_IF(emit_op_mem(compiler
, mem_flags
, dst_r
, src
, srcw
, TMP_REG1
));
1244 if (dst
& SLJIT_MEM
)
1245 return emit_op_mem(compiler
, mem_flags
| STORE
, dst_r
, dst
, dstw
, TMP_REG2
);
1246 return SLJIT_SUCCESS
;
1249 flags
= HAS_FLAGS(op_flags
) ? SET_FLAGS
: 0;
1250 mem_flags
= WORD_SIZE
;
1252 if (op_flags
& SLJIT_32
) {
1254 mem_flags
= INT_SIZE
;
1257 if (src
& SLJIT_MEM
) {
1258 FAIL_IF(emit_op_mem(compiler
, mem_flags
, TMP_REG2
, src
, srcw
, TMP_REG2
));
1262 emit_op_imm(compiler
, flags
| op
, dst_r
, TMP_REG1
, src
);
1264 if (SLJIT_UNLIKELY(dst
& SLJIT_MEM
))
1265 return emit_op_mem(compiler
, mem_flags
| STORE
, dst_r
, dst
, dstw
, TMP_REG2
);
1266 return SLJIT_SUCCESS
;
1269 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op2(struct sljit_compiler
*compiler
, sljit_s32 op
,
1270 sljit_s32 dst
, sljit_sw dstw
,
1271 sljit_s32 src1
, sljit_sw src1w
,
1272 sljit_s32 src2
, sljit_sw src2w
)
1274 sljit_s32 dst_r
, flags
, mem_flags
;
1277 CHECK(check_sljit_emit_op2(compiler
, op
, 0, dst
, dstw
, src1
, src1w
, src2
, src2w
));
1278 ADJUST_LOCAL_OFFSET(dst
, dstw
);
1279 ADJUST_LOCAL_OFFSET(src1
, src1w
);
1280 ADJUST_LOCAL_OFFSET(src2
, src2w
);
1282 dst_r
= FAST_IS_REG(dst
) ? dst
: TMP_REG1
;
1283 flags
= HAS_FLAGS(op
) ? SET_FLAGS
: 0;
1284 mem_flags
= WORD_SIZE
;
1286 if (op
& SLJIT_32
) {
1288 mem_flags
= INT_SIZE
;
1291 if (dst
== TMP_REG1
)
1292 flags
|= UNUSED_RETURN
;
1294 if (src1
& SLJIT_MEM
) {
1295 FAIL_IF(emit_op_mem(compiler
, mem_flags
, TMP_REG1
, src1
, src1w
, TMP_REG1
));
1299 if (src2
& SLJIT_MEM
) {
1300 FAIL_IF(emit_op_mem(compiler
, mem_flags
, TMP_REG2
, src2
, src2w
, TMP_REG2
));
1304 if (src1
& SLJIT_IMM
)
1309 if (src2
& SLJIT_IMM
)
1314 emit_op_imm(compiler
, flags
| GET_OPCODE(op
), dst_r
, src1w
, src2w
);
1316 if (dst
& SLJIT_MEM
)
1317 return emit_op_mem(compiler
, mem_flags
| STORE
, dst_r
, dst
, dstw
, TMP_REG2
);
1318 return SLJIT_SUCCESS
;
1321 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op2u(struct sljit_compiler
*compiler
, sljit_s32 op
,
1322 sljit_s32 src1
, sljit_sw src1w
,
1323 sljit_s32 src2
, sljit_sw src2w
)
1326 CHECK(check_sljit_emit_op2(compiler
, op
, 1, 0, 0, src1
, src1w
, src2
, src2w
));
1328 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
1329 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
1330 compiler
->skip_checks
= 1;
1332 return sljit_emit_op2(compiler
, op
, TMP_REG1
, 0, src1
, src1w
, src2
, src2w
);
1335 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op_src(struct sljit_compiler
*compiler
, sljit_s32 op
,
1336 sljit_s32 src
, sljit_sw srcw
)
1339 CHECK(check_sljit_emit_op_src(compiler
, op
, src
, srcw
));
1340 ADJUST_LOCAL_OFFSET(src
, srcw
);
1343 case SLJIT_FAST_RETURN
:
1344 if (FAST_IS_REG(src
))
1345 FAIL_IF(push_inst(compiler
, ORR
| RD(TMP_LR
) | RN(TMP_ZERO
) | RM(src
)));
1347 FAIL_IF(emit_op_mem(compiler
, WORD_SIZE
, TMP_LR
, src
, srcw
, TMP_REG1
));
1349 return push_inst(compiler
, RET
| RN(TMP_LR
));
1350 case SLJIT_SKIP_FRAMES_BEFORE_FAST_RETURN
:
1351 return SLJIT_SUCCESS
;
1352 case SLJIT_PREFETCH_L1
:
1353 case SLJIT_PREFETCH_L2
:
1354 case SLJIT_PREFETCH_L3
:
1355 case SLJIT_PREFETCH_ONCE
:
1356 SLJIT_ASSERT(reg_map
[1] == 0 && reg_map
[3] == 2 && reg_map
[5] == 4);
1358 /* The reg_map[op] should provide the appropriate constant. */
1359 if (op
== SLJIT_PREFETCH_L1
)
1361 else if (op
== SLJIT_PREFETCH_L2
)
1363 else if (op
== SLJIT_PREFETCH_L3
)
1368 /* Signed word sized load is the prefetch instruction. */
1369 return emit_op_mem(compiler
, WORD_SIZE
| SIGNED
, op
, src
, srcw
, TMP_REG1
);
1372 return SLJIT_SUCCESS
;
1375 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_get_register_index(sljit_s32 reg
)
1377 CHECK_REG_INDEX(check_sljit_get_register_index(reg
));
1378 return reg_map
[reg
];
1381 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_get_float_register_index(sljit_s32 reg
)
1383 CHECK_REG_INDEX(check_sljit_get_float_register_index(reg
));
1384 return freg_map
[reg
];
1387 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op_custom(struct sljit_compiler
*compiler
,
1388 void *instruction
, sljit_u32 size
)
1391 CHECK(check_sljit_emit_op_custom(compiler
, instruction
, size
));
1393 return push_inst(compiler
, *(sljit_ins
*)instruction
);
1396 /* --------------------------------------------------------------------- */
1397 /* Floating point operators */
1398 /* --------------------------------------------------------------------- */
1400 static sljit_s32
emit_fop_mem(struct sljit_compiler
*compiler
, sljit_s32 flags
, sljit_s32 reg
, sljit_s32 arg
, sljit_sw argw
)
1402 sljit_u32 shift
= MEM_SIZE_SHIFT(flags
);
1403 sljit_ins type
= (shift
<< 30);
1405 SLJIT_ASSERT(arg
& SLJIT_MEM
);
1407 if (!(flags
& STORE
))
1410 if (arg
& OFFS_REG_MASK
) {
1412 if (argw
== 0 || argw
== shift
)
1413 return push_inst(compiler
, STR_FR
| type
| VT(reg
)
1414 | RN(arg
& REG_MASK
) | RM(OFFS_REG(arg
)) | (argw
? (1 << 12) : 0));
1416 FAIL_IF(push_inst(compiler
, ADD
| RD(TMP_REG1
) | RN(arg
& REG_MASK
) | RM(OFFS_REG(arg
)) | ((sljit_ins
)argw
<< 10)));
1417 return push_inst(compiler
, STR_FI
| type
| VT(reg
) | RN(TMP_REG1
));
1423 FAIL_IF(load_immediate(compiler
, TMP_REG1
, argw
& ~(0xfff << shift
)));
1425 argw
= (argw
>> shift
) & 0xfff;
1427 return push_inst(compiler
, STR_FI
| type
| VT(reg
) | RN(TMP_REG1
) | ((sljit_ins
)argw
<< 10));
1430 if (argw
>= 0 && (argw
& ((1 << shift
) - 1)) == 0) {
1431 if ((argw
>> shift
) <= 0xfff)
1432 return push_inst(compiler
, STR_FI
| type
| VT(reg
) | RN(arg
) | ((sljit_ins
)argw
<< (10 - shift
)));
1434 if (argw
<= 0xffffff) {
1435 FAIL_IF(push_inst(compiler
, ADDI
| (1 << 22) | RD(TMP_REG1
) | RN(arg
) | (((sljit_ins
)argw
>> 12) << 10)));
1437 argw
= ((argw
& 0xfff) >> shift
);
1438 return push_inst(compiler
, STR_FI
| type
| VT(reg
) | RN(TMP_REG1
) | ((sljit_ins
)argw
<< 10));
1442 if (argw
<= 255 && argw
>= -256)
1443 return push_inst(compiler
, STUR_FI
| type
| VT(reg
) | RN(arg
) | (((sljit_ins
)argw
& 0x1ff) << 12));
1445 FAIL_IF(load_immediate(compiler
, TMP_REG1
, argw
));
1446 return push_inst(compiler
, STR_FR
| type
| VT(reg
) | RN(arg
) | RM(TMP_REG1
));
1449 static SLJIT_INLINE sljit_s32
sljit_emit_fop1_conv_sw_from_f64(struct sljit_compiler
*compiler
, sljit_s32 op
,
1450 sljit_s32 dst
, sljit_sw dstw
,
1451 sljit_s32 src
, sljit_sw srcw
)
1453 sljit_s32 dst_r
= FAST_IS_REG(dst
) ? dst
: TMP_REG1
;
1454 sljit_ins inv_bits
= (op
& SLJIT_32
) ? (1 << 22) : 0;
1456 if (GET_OPCODE(op
) == SLJIT_CONV_S32_FROM_F64
)
1459 if (src
& SLJIT_MEM
) {
1460 emit_fop_mem(compiler
, (op
& SLJIT_32
) ? INT_SIZE
: WORD_SIZE
, TMP_FREG1
, src
, srcw
);
1464 FAIL_IF(push_inst(compiler
, (FCVTZS
^ inv_bits
) | RD(dst_r
) | VN(src
)));
1466 if (dst
& SLJIT_MEM
)
1467 return emit_op_mem(compiler
, ((GET_OPCODE(op
) == SLJIT_CONV_S32_FROM_F64
) ? INT_SIZE
: WORD_SIZE
) | STORE
, TMP_REG1
, dst
, dstw
, TMP_REG2
);
1468 return SLJIT_SUCCESS
;
1471 static SLJIT_INLINE sljit_s32
sljit_emit_fop1_conv_f64_from_sw(struct sljit_compiler
*compiler
, sljit_s32 op
,
1472 sljit_s32 dst
, sljit_sw dstw
,
1473 sljit_s32 src
, sljit_sw srcw
)
1475 sljit_s32 dst_r
= FAST_IS_REG(dst
) ? dst
: TMP_FREG1
;
1476 sljit_ins inv_bits
= (op
& SLJIT_32
) ? (1 << 22) : 0;
1478 if (GET_OPCODE(op
) == SLJIT_CONV_F64_FROM_S32
)
1481 if (src
& SLJIT_MEM
) {
1482 emit_op_mem(compiler
, ((GET_OPCODE(op
) == SLJIT_CONV_F64_FROM_S32
) ? INT_SIZE
: WORD_SIZE
), TMP_REG1
, src
, srcw
, TMP_REG1
);
1484 } else if (src
& SLJIT_IMM
) {
1485 #if (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64)
1486 if (GET_OPCODE(op
) == SLJIT_CONV_F64_FROM_S32
)
1487 srcw
= (sljit_s32
)srcw
;
1489 FAIL_IF(load_immediate(compiler
, TMP_REG1
, srcw
));
1493 FAIL_IF(push_inst(compiler
, (SCVTF
^ inv_bits
) | VD(dst_r
) | RN(src
)));
1495 if (dst
& SLJIT_MEM
)
1496 return emit_fop_mem(compiler
, ((op
& SLJIT_32
) ? INT_SIZE
: WORD_SIZE
) | STORE
, TMP_FREG1
, dst
, dstw
);
1497 return SLJIT_SUCCESS
;
1500 static SLJIT_INLINE sljit_s32
sljit_emit_fop1_cmp(struct sljit_compiler
*compiler
, sljit_s32 op
,
1501 sljit_s32 src1
, sljit_sw src1w
,
1502 sljit_s32 src2
, sljit_sw src2w
)
1504 sljit_s32 mem_flags
= (op
& SLJIT_32
) ? INT_SIZE
: WORD_SIZE
;
1505 sljit_ins inv_bits
= (op
& SLJIT_32
) ? (1 << 22) : 0;
1507 if (src1
& SLJIT_MEM
) {
1508 emit_fop_mem(compiler
, mem_flags
, TMP_FREG1
, src1
, src1w
);
1512 if (src2
& SLJIT_MEM
) {
1513 emit_fop_mem(compiler
, mem_flags
, TMP_FREG2
, src2
, src2w
);
1517 return push_inst(compiler
, (FCMP
^ inv_bits
) | VN(src1
) | VM(src2
));
1520 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_fop1(struct sljit_compiler
*compiler
, sljit_s32 op
,
1521 sljit_s32 dst
, sljit_sw dstw
,
1522 sljit_s32 src
, sljit_sw srcw
)
1524 sljit_s32 dst_r
, mem_flags
= (op
& SLJIT_32
) ? INT_SIZE
: WORD_SIZE
;
1529 SLJIT_COMPILE_ASSERT((INT_SIZE
^ 0x1) == WORD_SIZE
, must_be_one_bit_difference
);
1530 SELECT_FOP1_OPERATION_WITH_CHECKS(compiler
, op
, dst
, dstw
, src
, srcw
);
1532 inv_bits
= (op
& SLJIT_32
) ? (1 << 22) : 0;
1533 dst_r
= FAST_IS_REG(dst
) ? dst
: TMP_FREG1
;
1535 if (src
& SLJIT_MEM
) {
1536 emit_fop_mem(compiler
, (GET_OPCODE(op
) == SLJIT_CONV_F64_FROM_F32
) ? (mem_flags
^ 0x1) : mem_flags
, dst_r
, src
, srcw
);
1540 switch (GET_OPCODE(op
)) {
1543 if (dst_r
!= TMP_FREG1
)
1544 FAIL_IF(push_inst(compiler
, (FMOV
^ inv_bits
) | VD(dst_r
) | VN(src
)));
1550 FAIL_IF(push_inst(compiler
, (FNEG
^ inv_bits
) | VD(dst_r
) | VN(src
)));
1553 FAIL_IF(push_inst(compiler
, (FABS
^ inv_bits
) | VD(dst_r
) | VN(src
)));
1555 case SLJIT_CONV_F64_FROM_F32
:
1556 FAIL_IF(push_inst(compiler
, FCVT
| (sljit_ins
)((op
& SLJIT_32
) ? (1 << 22) : (1 << 15)) | VD(dst_r
) | VN(src
)));
1560 if (dst
& SLJIT_MEM
)
1561 return emit_fop_mem(compiler
, mem_flags
| STORE
, dst_r
, dst
, dstw
);
1562 return SLJIT_SUCCESS
;
1565 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_fop2(struct sljit_compiler
*compiler
, sljit_s32 op
,
1566 sljit_s32 dst
, sljit_sw dstw
,
1567 sljit_s32 src1
, sljit_sw src1w
,
1568 sljit_s32 src2
, sljit_sw src2w
)
1570 sljit_s32 dst_r
, mem_flags
= (op
& SLJIT_32
) ? INT_SIZE
: WORD_SIZE
;
1571 sljit_ins inv_bits
= (op
& SLJIT_32
) ? (1 << 22) : 0;
1574 CHECK(check_sljit_emit_fop2(compiler
, op
, dst
, dstw
, src1
, src1w
, src2
, src2w
));
1575 ADJUST_LOCAL_OFFSET(dst
, dstw
);
1576 ADJUST_LOCAL_OFFSET(src1
, src1w
);
1577 ADJUST_LOCAL_OFFSET(src2
, src2w
);
1579 dst_r
= FAST_IS_REG(dst
) ? dst
: TMP_FREG1
;
1580 if (src1
& SLJIT_MEM
) {
1581 emit_fop_mem(compiler
, mem_flags
, TMP_FREG1
, src1
, src1w
);
1584 if (src2
& SLJIT_MEM
) {
1585 emit_fop_mem(compiler
, mem_flags
, TMP_FREG2
, src2
, src2w
);
1589 switch (GET_OPCODE(op
)) {
1591 FAIL_IF(push_inst(compiler
, (FADD
^ inv_bits
) | VD(dst_r
) | VN(src1
) | VM(src2
)));
1594 FAIL_IF(push_inst(compiler
, (FSUB
^ inv_bits
) | VD(dst_r
) | VN(src1
) | VM(src2
)));
1597 FAIL_IF(push_inst(compiler
, (FMUL
^ inv_bits
) | VD(dst_r
) | VN(src1
) | VM(src2
)));
1600 FAIL_IF(push_inst(compiler
, (FDIV
^ inv_bits
) | VD(dst_r
) | VN(src1
) | VM(src2
)));
1604 if (!(dst
& SLJIT_MEM
))
1605 return SLJIT_SUCCESS
;
1606 return emit_fop_mem(compiler
, mem_flags
| STORE
, TMP_FREG1
, dst
, dstw
);
1609 /* --------------------------------------------------------------------- */
1610 /* Other instructions */
1611 /* --------------------------------------------------------------------- */
1613 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_fast_enter(struct sljit_compiler
*compiler
, sljit_s32 dst
, sljit_sw dstw
)
1616 CHECK(check_sljit_emit_fast_enter(compiler
, dst
, dstw
));
1617 ADJUST_LOCAL_OFFSET(dst
, dstw
);
1619 if (FAST_IS_REG(dst
))
1620 return push_inst(compiler
, ORR
| RD(dst
) | RN(TMP_ZERO
) | RM(TMP_LR
));
1623 return emit_op_mem(compiler
, WORD_SIZE
| STORE
, TMP_LR
, dst
, dstw
, TMP_REG1
);
1626 /* --------------------------------------------------------------------- */
1627 /* Conditional instructions */
1628 /* --------------------------------------------------------------------- */
1630 static sljit_ins
get_cc(struct sljit_compiler
*compiler
, sljit_s32 type
)
1634 case SLJIT_EQUAL_F64
:
1637 case SLJIT_NOT_EQUAL
:
1638 case SLJIT_NOT_EQUAL_F64
:
1642 case SLJIT_LESS_F64
:
1645 case SLJIT_GREATER_EQUAL
:
1646 case SLJIT_GREATER_EQUAL_F64
:
1650 case SLJIT_GREATER_F64
:
1653 case SLJIT_LESS_EQUAL
:
1654 case SLJIT_LESS_EQUAL_F64
:
1657 case SLJIT_SIG_LESS
:
1660 case SLJIT_SIG_GREATER_EQUAL
:
1663 case SLJIT_SIG_GREATER
:
1666 case SLJIT_SIG_LESS_EQUAL
:
1669 case SLJIT_OVERFLOW
:
1670 if (!(compiler
->status_flags_state
& SLJIT_CURRENT_FLAGS_ADD_SUB
))
1673 case SLJIT_UNORDERED_F64
:
1676 case SLJIT_NOT_OVERFLOW
:
1677 if (!(compiler
->status_flags_state
& SLJIT_CURRENT_FLAGS_ADD_SUB
))
1680 case SLJIT_ORDERED_F64
:
1684 SLJIT_UNREACHABLE();
1689 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_label
* sljit_emit_label(struct sljit_compiler
*compiler
)
1691 struct sljit_label
*label
;
1694 CHECK_PTR(check_sljit_emit_label(compiler
));
1696 if (compiler
->last_label
&& compiler
->last_label
->size
== compiler
->size
)
1697 return compiler
->last_label
;
1699 label
= (struct sljit_label
*)ensure_abuf(compiler
, sizeof(struct sljit_label
));
1700 PTR_FAIL_IF(!label
);
1701 set_label(label
, compiler
);
1705 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_jump
* sljit_emit_jump(struct sljit_compiler
*compiler
, sljit_s32 type
)
1707 struct sljit_jump
*jump
;
1710 CHECK_PTR(check_sljit_emit_jump(compiler
, type
));
1712 jump
= (struct sljit_jump
*)ensure_abuf(compiler
, sizeof(struct sljit_jump
));
1714 set_jump(jump
, compiler
, type
& SLJIT_REWRITABLE_JUMP
);
1717 if (type
< SLJIT_JUMP
) {
1718 jump
->flags
|= IS_COND
;
1719 PTR_FAIL_IF(push_inst(compiler
, B_CC
| (6 << 5) | get_cc(compiler
, type
)));
1721 else if (type
>= SLJIT_FAST_CALL
)
1722 jump
->flags
|= IS_BL
;
1724 PTR_FAIL_IF(emit_imm64_const(compiler
, TMP_REG1
, 0));
1725 jump
->addr
= compiler
->size
;
1726 PTR_FAIL_IF(push_inst(compiler
, ((type
>= SLJIT_FAST_CALL
) ? BLR
: BR
) | RN(TMP_REG1
)));
1731 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_jump
* sljit_emit_call(struct sljit_compiler
*compiler
, sljit_s32 type
,
1732 sljit_s32 arg_types
)
1735 CHECK_PTR(check_sljit_emit_call(compiler
, type
, arg_types
));
1737 if (type
& SLJIT_TAIL_CALL
) {
1738 PTR_FAIL_IF(emit_stack_frame_release(compiler
));
1739 type
= SLJIT_JUMP
| (type
& SLJIT_REWRITABLE_JUMP
);
1742 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
1743 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
1744 compiler
->skip_checks
= 1;
1747 return sljit_emit_jump(compiler
, type
);
1750 static SLJIT_INLINE
struct sljit_jump
* emit_cmp_to0(struct sljit_compiler
*compiler
, sljit_s32 type
,
1751 sljit_s32 src
, sljit_sw srcw
)
1753 struct sljit_jump
*jump
;
1754 sljit_ins inv_bits
= (type
& SLJIT_32
) ? W_OP
: 0;
1756 SLJIT_ASSERT((type
& 0xff) == SLJIT_EQUAL
|| (type
& 0xff) == SLJIT_NOT_EQUAL
);
1757 ADJUST_LOCAL_OFFSET(src
, srcw
);
1759 jump
= (struct sljit_jump
*)ensure_abuf(compiler
, sizeof(struct sljit_jump
));
1761 set_jump(jump
, compiler
, type
& SLJIT_REWRITABLE_JUMP
);
1762 jump
->flags
|= IS_CBZ
| IS_COND
;
1764 if (src
& SLJIT_MEM
) {
1765 PTR_FAIL_IF(emit_op_mem(compiler
, inv_bits
? INT_SIZE
: WORD_SIZE
, TMP_REG1
, src
, srcw
, TMP_REG1
));
1768 else if (src
& SLJIT_IMM
) {
1769 PTR_FAIL_IF(load_immediate(compiler
, TMP_REG1
, srcw
));
1773 SLJIT_ASSERT(FAST_IS_REG(src
));
1775 if ((type
& 0xff) == SLJIT_EQUAL
)
1776 inv_bits
|= 1 << 24;
1778 PTR_FAIL_IF(push_inst(compiler
, (CBZ
^ inv_bits
) | (6 << 5) | RT(src
)));
1779 PTR_FAIL_IF(emit_imm64_const(compiler
, TMP_REG1
, 0));
1780 jump
->addr
= compiler
->size
;
1781 PTR_FAIL_IF(push_inst(compiler
, BR
| RN(TMP_REG1
)));
1785 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_ijump(struct sljit_compiler
*compiler
, sljit_s32 type
, sljit_s32 src
, sljit_sw srcw
)
1787 struct sljit_jump
*jump
;
1790 CHECK(check_sljit_emit_ijump(compiler
, type
, src
, srcw
));
1791 ADJUST_LOCAL_OFFSET(src
, srcw
);
1793 if (!(src
& SLJIT_IMM
)) {
1794 if (src
& SLJIT_MEM
) {
1795 FAIL_IF(emit_op_mem(compiler
, WORD_SIZE
, TMP_REG1
, src
, srcw
, TMP_REG1
));
1798 return push_inst(compiler
, ((type
>= SLJIT_FAST_CALL
) ? BLR
: BR
) | RN(src
));
1801 /* These jumps are converted to jump/call instructions when possible. */
1802 jump
= (struct sljit_jump
*)ensure_abuf(compiler
, sizeof(struct sljit_jump
));
1804 set_jump(jump
, compiler
, JUMP_ADDR
| ((type
>= SLJIT_FAST_CALL
) ? IS_BL
: 0));
1805 jump
->u
.target
= (sljit_uw
)srcw
;
1807 FAIL_IF(emit_imm64_const(compiler
, TMP_REG1
, 0));
1808 jump
->addr
= compiler
->size
;
1809 return push_inst(compiler
, ((type
>= SLJIT_FAST_CALL
) ? BLR
: BR
) | RN(TMP_REG1
));
1812 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_icall(struct sljit_compiler
*compiler
, sljit_s32 type
,
1813 sljit_s32 arg_types
,
1814 sljit_s32 src
, sljit_sw srcw
)
1817 CHECK(check_sljit_emit_icall(compiler
, type
, arg_types
, src
, srcw
));
1819 if (src
& SLJIT_MEM
) {
1820 FAIL_IF(emit_op_mem(compiler
, WORD_SIZE
, TMP_REG1
, src
, srcw
, TMP_REG1
));
1824 if (type
& SLJIT_TAIL_CALL
) {
1825 if (src
>= SLJIT_FIRST_SAVED_REG
&& src
<= SLJIT_S0
) {
1826 FAIL_IF(push_inst(compiler
, ORR
| RD(TMP_REG1
) | RN(TMP_ZERO
) | RM(src
)));
1830 FAIL_IF(emit_stack_frame_release(compiler
));
1834 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
1835 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
1836 compiler
->skip_checks
= 1;
1839 return sljit_emit_ijump(compiler
, type
, src
, srcw
);
1842 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op_flags(struct sljit_compiler
*compiler
, sljit_s32 op
,
1843 sljit_s32 dst
, sljit_sw dstw
,
1846 sljit_s32 dst_r
, src_r
, flags
, mem_flags
;
1850 CHECK(check_sljit_emit_op_flags(compiler
, op
, dst
, dstw
, type
));
1851 ADJUST_LOCAL_OFFSET(dst
, dstw
);
1853 cc
= get_cc(compiler
, type
& 0xff);
1854 dst_r
= FAST_IS_REG(dst
) ? dst
: TMP_REG1
;
1856 if (GET_OPCODE(op
) < SLJIT_ADD
) {
1857 FAIL_IF(push_inst(compiler
, CSINC
| (cc
<< 12) | RD(dst_r
) | RN(TMP_ZERO
) | RM(TMP_ZERO
)));
1859 if (dst_r
== TMP_REG1
) {
1860 mem_flags
= (GET_OPCODE(op
) == SLJIT_MOV
? WORD_SIZE
: INT_SIZE
) | STORE
;
1861 return emit_op_mem(compiler
, mem_flags
, TMP_REG1
, dst
, dstw
, TMP_REG2
);
1864 return SLJIT_SUCCESS
;
1867 flags
= HAS_FLAGS(op
) ? SET_FLAGS
: 0;
1868 mem_flags
= WORD_SIZE
;
1870 if (op
& SLJIT_32
) {
1872 mem_flags
= INT_SIZE
;
1877 if (dst
& SLJIT_MEM
) {
1878 FAIL_IF(emit_op_mem(compiler
, mem_flags
, TMP_REG1
, dst
, dstw
, TMP_REG1
));
1882 FAIL_IF(push_inst(compiler
, CSINC
| (cc
<< 12) | RD(TMP_REG2
) | RN(TMP_ZERO
) | RM(TMP_ZERO
)));
1883 emit_op_imm(compiler
, flags
| GET_OPCODE(op
), dst_r
, src_r
, TMP_REG2
);
1885 if (dst
& SLJIT_MEM
)
1886 return emit_op_mem(compiler
, mem_flags
| STORE
, TMP_REG1
, dst
, dstw
, TMP_REG2
);
1887 return SLJIT_SUCCESS
;
1890 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_cmov(struct sljit_compiler
*compiler
, sljit_s32 type
,
1892 sljit_s32 src
, sljit_sw srcw
)
1894 sljit_ins inv_bits
= (dst_reg
& SLJIT_32
) ? W_OP
: 0;
1898 CHECK(check_sljit_emit_cmov(compiler
, type
, dst_reg
, src
, srcw
));
1900 if (SLJIT_UNLIKELY(src
& SLJIT_IMM
)) {
1901 if (dst_reg
& SLJIT_32
)
1902 srcw
= (sljit_s32
)srcw
;
1903 FAIL_IF(load_immediate(compiler
, TMP_REG1
, srcw
));
1908 cc
= get_cc(compiler
, type
& 0xff);
1909 dst_reg
&= ~SLJIT_32
;
1911 return push_inst(compiler
, (CSEL
^ inv_bits
) | (cc
<< 12) | RD(dst_reg
) | RN(dst_reg
) | RM(src
));
1914 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_mem(struct sljit_compiler
*compiler
, sljit_s32 type
,
1916 sljit_s32 mem
, sljit_sw memw
)
1918 sljit_u32 sign
= 0, inst
;
1921 CHECK(check_sljit_emit_mem(compiler
, type
, reg
, mem
, memw
));
1923 if ((mem
& OFFS_REG_MASK
) || (memw
> 255 || memw
< -256))
1924 return SLJIT_ERR_UNSUPPORTED
;
1926 if (type
& SLJIT_MEM_SUPP
)
1927 return SLJIT_SUCCESS
;
1929 switch (type
& 0xff) {
1932 inst
= STURBI
| (MEM_SIZE_SHIFT(WORD_SIZE
) << 30) | 0x400;
1937 inst
= STURBI
| (MEM_SIZE_SHIFT(BYTE_SIZE
) << 30) | 0x400;
1942 inst
= STURBI
| (MEM_SIZE_SHIFT(HALF_SIZE
) << 30) | 0x400;
1948 inst
= STURBI
| (MEM_SIZE_SHIFT(INT_SIZE
) << 30) | 0x400;
1951 SLJIT_UNREACHABLE();
1952 inst
= STURBI
| (MEM_SIZE_SHIFT(WORD_SIZE
) << 30) | 0x400;
1956 if (!(type
& SLJIT_MEM_STORE
))
1957 inst
|= sign
? 0x00800000 : 0x00400000;
1959 if (type
& SLJIT_MEM_PRE
)
1962 return push_inst(compiler
, inst
| RT(reg
) | RN(mem
& REG_MASK
) | (sljit_ins
)((memw
& 0x1ff) << 12));
1965 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_fmem(struct sljit_compiler
*compiler
, sljit_s32 type
,
1967 sljit_s32 mem
, sljit_sw memw
)
1972 CHECK(check_sljit_emit_fmem(compiler
, type
, freg
, mem
, memw
));
1974 if ((mem
& OFFS_REG_MASK
) || (memw
> 255 || memw
< -256))
1975 return SLJIT_ERR_UNSUPPORTED
;
1977 if (type
& SLJIT_MEM_SUPP
)
1978 return SLJIT_SUCCESS
;
1980 inst
= STUR_FI
| 0x80000400;
1982 if (!(type
& SLJIT_32
))
1985 if (!(type
& SLJIT_MEM_STORE
))
1988 if (type
& SLJIT_MEM_PRE
)
1991 return push_inst(compiler
, inst
| VT(freg
) | RN(mem
& REG_MASK
) | (sljit_ins
)((memw
& 0x1ff) << 12));
1994 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_get_local_base(struct sljit_compiler
*compiler
, sljit_s32 dst
, sljit_sw dstw
, sljit_sw offset
)
2000 CHECK(check_sljit_get_local_base(compiler
, dst
, dstw
, offset
));
2002 SLJIT_ASSERT (SLJIT_LOCALS_OFFSET_BASE
== 0);
2004 dst_reg
= FAST_IS_REG(dst
) ? dst
: TMP_REG1
;
2006 if (offset
<= 0xffffff && offset
>= -0xffffff) {
2013 if (offset
<= 0xfff)
2014 FAIL_IF(push_inst(compiler
, ins
| RD(dst_reg
) | RN(SLJIT_SP
) | (sljit_ins
)(offset
<< 10)));
2016 FAIL_IF(push_inst(compiler
, ins
| RD(dst_reg
) | RN(SLJIT_SP
) | (sljit_ins
)((offset
& 0xfff000) >> (12 - 10)) | (1 << 22)));
2020 FAIL_IF(push_inst(compiler
, ins
| RD(dst_reg
) | RN(dst_reg
) | (sljit_ins
)(offset
<< 10)));
2024 FAIL_IF(load_immediate (compiler
, dst_reg
, offset
));
2025 /* Add extended register form. */
2026 FAIL_IF(push_inst(compiler
, ADDE
| (0x3 << 13) | RD(dst_reg
) | RN(SLJIT_SP
) | RM(dst_reg
)));
2029 if (SLJIT_UNLIKELY(dst
& SLJIT_MEM
))
2030 return emit_op_mem(compiler
, WORD_SIZE
| STORE
, dst_reg
, dst
, dstw
, TMP_REG1
);
2031 return SLJIT_SUCCESS
;
2034 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_const
* sljit_emit_const(struct sljit_compiler
*compiler
, sljit_s32 dst
, sljit_sw dstw
, sljit_sw init_value
)
2036 struct sljit_const
*const_
;
2040 CHECK_PTR(check_sljit_emit_const(compiler
, dst
, dstw
, init_value
));
2041 ADJUST_LOCAL_OFFSET(dst
, dstw
);
2043 const_
= (struct sljit_const
*)ensure_abuf(compiler
, sizeof(struct sljit_const
));
2044 PTR_FAIL_IF(!const_
);
2045 set_const(const_
, compiler
);
2047 dst_r
= FAST_IS_REG(dst
) ? dst
: TMP_REG1
;
2048 PTR_FAIL_IF(emit_imm64_const(compiler
, dst_r
, (sljit_uw
)init_value
));
2050 if (dst
& SLJIT_MEM
)
2051 PTR_FAIL_IF(emit_op_mem(compiler
, WORD_SIZE
| STORE
, dst_r
, dst
, dstw
, TMP_REG2
));
2055 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_put_label
* sljit_emit_put_label(struct sljit_compiler
*compiler
, sljit_s32 dst
, sljit_sw dstw
)
2057 struct sljit_put_label
*put_label
;
2061 CHECK_PTR(check_sljit_emit_put_label(compiler
, dst
, dstw
));
2062 ADJUST_LOCAL_OFFSET(dst
, dstw
);
2064 dst_r
= FAST_IS_REG(dst
) ? dst
: TMP_REG1
;
2065 PTR_FAIL_IF(emit_imm64_const(compiler
, dst_r
, 0));
2067 put_label
= (struct sljit_put_label
*)ensure_abuf(compiler
, sizeof(struct sljit_put_label
));
2068 PTR_FAIL_IF(!put_label
);
2069 set_put_label(put_label
, compiler
, 1);
2071 if (dst
& SLJIT_MEM
)
2072 PTR_FAIL_IF(emit_op_mem(compiler
, WORD_SIZE
| STORE
, dst_r
, dst
, dstw
, TMP_REG2
));
2077 SLJIT_API_FUNC_ATTRIBUTE
void sljit_set_jump_addr(sljit_uw addr
, sljit_uw new_target
, sljit_sw executable_offset
)
2079 sljit_ins
* inst
= (sljit_ins
*)addr
;
2081 SLJIT_UNUSED_ARG(executable_offset
);
2083 SLJIT_UPDATE_WX_FLAGS(inst
, inst
+ 4, 0);
2085 dst
= inst
[0] & 0x1f;
2086 SLJIT_ASSERT((inst
[0] & 0xffe00000) == MOVZ
&& (inst
[1] & 0xffe00000) == (MOVK
| (1 << 21)));
2087 inst
[0] = MOVZ
| dst
| (((sljit_u32
)new_target
& 0xffff) << 5);
2088 inst
[1] = MOVK
| dst
| (((sljit_u32
)(new_target
>> 16) & 0xffff) << 5) | (1 << 21);
2089 inst
[2] = MOVK
| dst
| (((sljit_u32
)(new_target
>> 32) & 0xffff) << 5) | (2 << 21);
2090 inst
[3] = MOVK
| dst
| ((sljit_u32
)(new_target
>> 48) << 5) | (3 << 21);
2092 SLJIT_UPDATE_WX_FLAGS(inst
, inst
+ 4, 1);
2093 inst
= (sljit_ins
*)SLJIT_ADD_EXEC_OFFSET(inst
, executable_offset
);
2094 SLJIT_CACHE_FLUSH(inst
, inst
+ 4);
2097 SLJIT_API_FUNC_ATTRIBUTE
void sljit_set_const(sljit_uw addr
, sljit_sw new_constant
, sljit_sw executable_offset
)
2099 sljit_set_jump_addr(addr
, (sljit_uw
)new_constant
, executable_offset
);