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.
28 #define ARM_ABI_INFO " ABI:softfp"
30 #define ARM_ABI_INFO " ABI:hardfp"
33 SLJIT_API_FUNC_ATTRIBUTE
const char* sljit_get_platform_name(void)
35 #if (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
36 return "ARMv7" SLJIT_CPUINFO ARM_ABI_INFO
;
37 #elif (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
38 return "ARMv5" SLJIT_CPUINFO ARM_ABI_INFO
;
40 #error "Internal error: Unknown ARM architecture"
44 /* Last register + 1. */
45 #define TMP_REG1 (SLJIT_NUMBER_OF_REGISTERS + 2)
46 #define TMP_REG2 (SLJIT_NUMBER_OF_REGISTERS + 3)
47 #define TMP_PC (SLJIT_NUMBER_OF_REGISTERS + 4)
49 #define TMP_FREG1 (SLJIT_NUMBER_OF_FLOAT_REGISTERS + 1)
50 #define TMP_FREG2 (SLJIT_NUMBER_OF_FLOAT_REGISTERS + 2)
52 /* In ARM instruction words.
53 Cache lines are usually 32 byte aligned. */
54 #define CONST_POOL_ALIGNMENT 8
55 #define CONST_POOL_EMPTY 0xffffffff
57 #define ALIGN_INSTRUCTION(ptr) \
58 (sljit_uw*)(((sljit_uw)(ptr) + (CONST_POOL_ALIGNMENT * sizeof(sljit_uw)) - 1) & ~((CONST_POOL_ALIGNMENT * sizeof(sljit_uw)) - 1))
59 #define MAX_DIFFERENCE(max_diff) \
60 (((max_diff) / (sljit_s32)sizeof(sljit_uw)) - (CONST_POOL_ALIGNMENT - 1))
62 /* See sljit_emit_enter and sljit_emit_op0 if you want to change them. */
63 static const sljit_u8 reg_map
[SLJIT_NUMBER_OF_REGISTERS
+ 5] = {
64 0, 0, 1, 2, 3, 11, 10, 9, 8, 7, 6, 5, 4, 13, 12, 14, 15
67 static const sljit_u8 freg_map
[SLJIT_NUMBER_OF_FLOAT_REGISTERS
+ 3] = {
68 0, 0, 1, 2, 3, 4, 5, 15, 14, 13, 12, 11, 10, 9, 8, 6, 7
71 #define RM(rm) ((sljit_uw)reg_map[rm])
72 #define RM8(rm) ((sljit_uw)reg_map[rm] << 8)
73 #define RD(rd) ((sljit_uw)reg_map[rd] << 12)
74 #define RN(rn) ((sljit_uw)reg_map[rn] << 16)
76 #define VM(rm) ((sljit_uw)freg_map[rm])
77 #define VD(rd) ((sljit_uw)freg_map[rd] << 12)
78 #define VN(rn) ((sljit_uw)freg_map[rn] << 16)
80 /* --------------------------------------------------------------------- */
81 /* Instrucion forms */
82 /* --------------------------------------------------------------------- */
84 /* The instruction includes the AL condition.
85 INST_NAME - CONDITIONAL remove this flag. */
86 #define COND_MASK 0xf0000000
87 #define CONDITIONAL 0xe0000000
88 #define PUSH_POOL 0xff000000
90 #define ADC 0xe0a00000
91 #define ADD 0xe0800000
92 #define AND 0xe0000000
94 #define BIC 0xe1c00000
96 #define BLX 0xe12fff30
98 #define CLZ 0xe16f0f10
99 #define CMN 0xe1600000
100 #define CMP 0xe1400000
101 #define BKPT 0xe1200070
102 #define EOR 0xe0200000
103 #define MOV 0xe1a00000
104 #define MUL 0xe0000090
105 #define MVN 0xe1e00000
106 #define NOP 0xe1a00000
107 #define ORR 0xe1800000
108 #define PUSH 0xe92d0000
109 #define POP 0xe8bd0000
110 #define RSB 0xe0600000
111 #define RSC 0xe0e00000
112 #define SBC 0xe0c00000
113 #define SMULL 0xe0c00090
114 #define SUB 0xe0400000
115 #define TST 0xe1000000
116 #define UMULL 0xe0800090
117 #define VABS_F32 0xeeb00ac0
118 #define VADD_F32 0xee300a00
119 #define VCMP_F32 0xeeb40a40
120 #define VCVT_F32_S32 0xeeb80ac0
121 #define VCVT_F64_F32 0xeeb70ac0
122 #define VCVT_S32_F32 0xeebd0ac0
123 #define VDIV_F32 0xee800a00
124 #define VLDR_F32 0xed100a00
125 #define VMOV_F32 0xeeb00a40
126 #define VMOV 0xee000a10
127 #define VMOV2 0xec400a10
128 #define VMRS 0xeef1fa10
129 #define VMUL_F32 0xee200a00
130 #define VNEG_F32 0xeeb10a40
131 #define VPOP 0xecbd0b00
132 #define VPUSH 0xed2d0b00
133 #define VSTR_F32 0xed000a00
134 #define VSUB_F32 0xee300a40
136 #if (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
137 /* Arm v7 specific instructions. */
138 #define MOVW 0xe3000000
139 #define MOVT 0xe3400000
140 #define SXTB 0xe6af0070
141 #define SXTH 0xe6bf0070
142 #define UXTB 0xe6ef0070
143 #define UXTH 0xe6ff0070
146 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
148 static sljit_s32
push_cpool(struct sljit_compiler
*compiler
)
150 /* Pushing the constant pool into the instruction stream. */
156 /* The label could point the address after the constant pool. */
157 if (compiler
->last_label
&& compiler
->last_label
->size
== compiler
->size
)
158 compiler
->last_label
->size
+= compiler
->cpool_fill
+ (CONST_POOL_ALIGNMENT
- 1) + 1;
160 SLJIT_ASSERT(compiler
->cpool_fill
> 0 && compiler
->cpool_fill
<= CPOOL_SIZE
);
161 inst
= (sljit_uw
*)ensure_buf(compiler
, sizeof(sljit_uw
));
164 *inst
= 0xff000000 | compiler
->cpool_fill
;
166 for (i
= 0; i
< CONST_POOL_ALIGNMENT
- 1; i
++) {
167 inst
= (sljit_uw
*)ensure_buf(compiler
, sizeof(sljit_uw
));
173 cpool_ptr
= compiler
->cpool
;
174 cpool_end
= cpool_ptr
+ compiler
->cpool_fill
;
175 while (cpool_ptr
< cpool_end
) {
176 inst
= (sljit_uw
*)ensure_buf(compiler
, sizeof(sljit_uw
));
179 *inst
= *cpool_ptr
++;
181 compiler
->cpool_diff
= CONST_POOL_EMPTY
;
182 compiler
->cpool_fill
= 0;
183 return SLJIT_SUCCESS
;
186 static sljit_s32
push_inst(struct sljit_compiler
*compiler
, sljit_uw inst
)
190 if (SLJIT_UNLIKELY(compiler
->cpool_diff
!= CONST_POOL_EMPTY
&& compiler
->size
- compiler
->cpool_diff
>= MAX_DIFFERENCE(4092)))
191 FAIL_IF(push_cpool(compiler
));
193 ptr
= (sljit_uw
*)ensure_buf(compiler
, sizeof(sljit_uw
));
197 return SLJIT_SUCCESS
;
200 static sljit_s32
push_inst_with_literal(struct sljit_compiler
*compiler
, sljit_uw inst
, sljit_uw literal
)
203 sljit_uw cpool_index
= CPOOL_SIZE
;
206 sljit_u8
* cpool_unique_ptr
;
208 if (SLJIT_UNLIKELY(compiler
->cpool_diff
!= CONST_POOL_EMPTY
&& compiler
->size
- compiler
->cpool_diff
>= MAX_DIFFERENCE(4092)))
209 FAIL_IF(push_cpool(compiler
));
210 else if (compiler
->cpool_fill
> 0) {
211 cpool_ptr
= compiler
->cpool
;
212 cpool_end
= cpool_ptr
+ compiler
->cpool_fill
;
213 cpool_unique_ptr
= compiler
->cpool_unique
;
215 if ((*cpool_ptr
== literal
) && !(*cpool_unique_ptr
)) {
216 cpool_index
= (sljit_uw
)(cpool_ptr
- compiler
->cpool
);
221 } while (cpool_ptr
< cpool_end
);
224 if (cpool_index
== CPOOL_SIZE
) {
225 /* Must allocate a new entry in the literal pool. */
226 if (compiler
->cpool_fill
< CPOOL_SIZE
) {
227 cpool_index
= compiler
->cpool_fill
;
228 compiler
->cpool_fill
++;
231 FAIL_IF(push_cpool(compiler
));
233 compiler
->cpool_fill
= 1;
237 SLJIT_ASSERT((inst
& 0xfff) == 0);
238 ptr
= (sljit_uw
*)ensure_buf(compiler
, sizeof(sljit_uw
));
241 *ptr
= inst
| cpool_index
;
243 compiler
->cpool
[cpool_index
] = literal
;
244 compiler
->cpool_unique
[cpool_index
] = 0;
245 if (compiler
->cpool_diff
== CONST_POOL_EMPTY
)
246 compiler
->cpool_diff
= compiler
->size
;
247 return SLJIT_SUCCESS
;
250 static sljit_s32
push_inst_with_unique_literal(struct sljit_compiler
*compiler
, sljit_uw inst
, sljit_uw literal
)
253 if (SLJIT_UNLIKELY((compiler
->cpool_diff
!= CONST_POOL_EMPTY
&& compiler
->size
- compiler
->cpool_diff
>= MAX_DIFFERENCE(4092)) || compiler
->cpool_fill
>= CPOOL_SIZE
))
254 FAIL_IF(push_cpool(compiler
));
256 SLJIT_ASSERT(compiler
->cpool_fill
< CPOOL_SIZE
&& (inst
& 0xfff) == 0);
257 ptr
= (sljit_uw
*)ensure_buf(compiler
, sizeof(sljit_uw
));
260 *ptr
= inst
| compiler
->cpool_fill
;
262 compiler
->cpool
[compiler
->cpool_fill
] = literal
;
263 compiler
->cpool_unique
[compiler
->cpool_fill
] = 1;
264 compiler
->cpool_fill
++;
265 if (compiler
->cpool_diff
== CONST_POOL_EMPTY
)
266 compiler
->cpool_diff
= compiler
->size
;
267 return SLJIT_SUCCESS
;
270 static SLJIT_INLINE sljit_s32
prepare_blx(struct sljit_compiler
*compiler
)
272 /* Place for at least two instruction (doesn't matter whether the first has a literal). */
273 if (SLJIT_UNLIKELY(compiler
->cpool_diff
!= CONST_POOL_EMPTY
&& compiler
->size
- compiler
->cpool_diff
>= MAX_DIFFERENCE(4088)))
274 return push_cpool(compiler
);
275 return SLJIT_SUCCESS
;
278 static SLJIT_INLINE sljit_s32
emit_blx(struct sljit_compiler
*compiler
)
280 /* Must follow tightly the previous instruction (to be able to convert it to bl instruction). */
281 SLJIT_ASSERT(compiler
->cpool_diff
== CONST_POOL_EMPTY
|| compiler
->size
- compiler
->cpool_diff
< MAX_DIFFERENCE(4092));
282 SLJIT_ASSERT(reg_map
[TMP_REG1
] != 14);
284 return push_inst(compiler
, BLX
| RM(TMP_REG1
));
287 static sljit_uw
patch_pc_relative_loads(sljit_uw
*last_pc_patch
, sljit_uw
*code_ptr
, sljit_uw
* const_pool
, sljit_uw cpool_size
)
291 sljit_uw counter
= 0;
292 sljit_uw
* clear_const_pool
= const_pool
;
293 sljit_uw
* clear_const_pool_end
= const_pool
+ cpool_size
;
295 SLJIT_ASSERT(const_pool
- code_ptr
<= CONST_POOL_ALIGNMENT
);
296 /* Set unused flag for all literals in the constant pool.
297 I.e.: unused literals can belong to branches, which can be encoded as B or BL.
298 We can "compress" the constant pool by discarding these literals. */
299 while (clear_const_pool
< clear_const_pool_end
)
300 *clear_const_pool
++ = (sljit_uw
)(-1);
302 while (last_pc_patch
< code_ptr
) {
303 /* Data transfer instruction with Rn == r15. */
304 if ((*last_pc_patch
& 0x0c0f0000) == 0x040f0000) {
305 diff
= (sljit_uw
)(const_pool
- last_pc_patch
);
306 ind
= (*last_pc_patch
) & 0xfff;
308 /* Must be a load instruction with immediate offset. */
309 SLJIT_ASSERT(ind
< cpool_size
&& !(*last_pc_patch
& (1 << 25)) && (*last_pc_patch
& (1 << 20)));
310 if ((sljit_s32
)const_pool
[ind
] < 0) {
311 const_pool
[ind
] = counter
;
316 ind
= const_pool
[ind
];
318 SLJIT_ASSERT(diff
>= 1);
319 if (diff
>= 2 || ind
> 0) {
320 diff
= (diff
+ (sljit_uw
)ind
- 2) << 2;
321 SLJIT_ASSERT(diff
<= 0xfff);
322 *last_pc_patch
= (*last_pc_patch
& ~(sljit_uw
)0xfff) | diff
;
325 *last_pc_patch
= (*last_pc_patch
& ~(sljit_uw
)(0xfff | (1 << 23))) | 0x004;
332 /* In some rare ocasions we may need future patches. The probability is close to 0 in practice. */
333 struct future_patch
{
334 struct future_patch
* next
;
339 static sljit_s32
resolve_const_pool_index(struct sljit_compiler
*compiler
, struct future_patch
**first_patch
, sljit_uw cpool_current_index
, sljit_uw
*cpool_start_address
, sljit_uw
*buf_ptr
)
342 struct future_patch
*curr_patch
, *prev_patch
;
344 SLJIT_UNUSED_ARG(compiler
);
346 /* Using the values generated by patch_pc_relative_loads. */
348 value
= cpool_start_address
[cpool_current_index
];
350 curr_patch
= *first_patch
;
354 value
= cpool_start_address
[cpool_current_index
];
357 if ((sljit_uw
)curr_patch
->index
== cpool_current_index
) {
358 value
= (sljit_uw
)curr_patch
->value
;
360 prev_patch
->next
= curr_patch
->next
;
362 *first_patch
= curr_patch
->next
;
363 SLJIT_FREE(curr_patch
, compiler
->allocator_data
);
366 prev_patch
= curr_patch
;
367 curr_patch
= curr_patch
->next
;
371 if ((sljit_sw
)value
>= 0) {
372 if (value
> cpool_current_index
) {
373 curr_patch
= (struct future_patch
*)SLJIT_MALLOC(sizeof(struct future_patch
), compiler
->allocator_data
);
375 while (*first_patch
) {
376 curr_patch
= *first_patch
;
377 *first_patch
= (*first_patch
)->next
;
378 SLJIT_FREE(curr_patch
, compiler
->allocator_data
);
380 return SLJIT_ERR_ALLOC_FAILED
;
382 curr_patch
->next
= *first_patch
;
383 curr_patch
->index
= (sljit_sw
)value
;
384 curr_patch
->value
= (sljit_sw
)cpool_start_address
[value
];
385 *first_patch
= curr_patch
;
387 cpool_start_address
[value
] = *buf_ptr
;
389 return SLJIT_SUCCESS
;
394 static sljit_s32
push_inst(struct sljit_compiler
*compiler
, sljit_uw inst
)
398 ptr
= (sljit_uw
*)ensure_buf(compiler
, sizeof(sljit_uw
));
402 return SLJIT_SUCCESS
;
405 static SLJIT_INLINE sljit_s32
emit_imm(struct sljit_compiler
*compiler
, sljit_s32 reg
, sljit_sw imm
)
407 FAIL_IF(push_inst(compiler
, MOVW
| RD(reg
) | ((imm
<< 4) & 0xf0000) | ((sljit_u32
)imm
& 0xfff)));
408 return push_inst(compiler
, MOVT
| RD(reg
) | ((imm
>> 12) & 0xf0000) | (((sljit_u32
)imm
>> 16) & 0xfff));
413 static SLJIT_INLINE sljit_s32
detect_jump_type(struct sljit_jump
*jump
, sljit_uw
*code_ptr
, sljit_uw
*code
, sljit_sw executable_offset
)
417 if (jump
->flags
& SLJIT_REWRITABLE_JUMP
)
420 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
421 if (jump
->flags
& IS_BL
)
424 if (jump
->flags
& JUMP_ADDR
)
425 diff
= ((sljit_sw
)jump
->u
.target
- (sljit_sw
)(code_ptr
+ 2) - executable_offset
);
427 SLJIT_ASSERT(jump
->flags
& JUMP_LABEL
);
428 diff
= ((sljit_sw
)(code
+ jump
->u
.label
->size
) - (sljit_sw
)(code_ptr
+ 2));
431 /* Branch to Thumb code has not been optimized yet. */
435 if (jump
->flags
& IS_BL
) {
436 if (diff
<= 0x01ffffff && diff
>= -0x02000000) {
437 *code_ptr
= (BL
- CONDITIONAL
) | (*(code_ptr
+ 1) & COND_MASK
);
438 jump
->flags
|= PATCH_B
;
443 if (diff
<= 0x01ffffff && diff
>= -0x02000000) {
444 *code_ptr
= (B
- CONDITIONAL
) | (*code_ptr
& COND_MASK
);
445 jump
->flags
|= PATCH_B
;
449 if (jump
->flags
& JUMP_ADDR
)
450 diff
= ((sljit_sw
)jump
->u
.target
- (sljit_sw
)code_ptr
- executable_offset
);
452 SLJIT_ASSERT(jump
->flags
& JUMP_LABEL
);
453 diff
= ((sljit_sw
)(code
+ jump
->u
.label
->size
) - (sljit_sw
)code_ptr
);
456 /* Branch to Thumb code has not been optimized yet. */
460 if (diff
<= 0x01ffffff && diff
>= -0x02000000) {
462 *code_ptr
= ((jump
->flags
& IS_BL
) ? (BL
- CONDITIONAL
) : (B
- CONDITIONAL
)) | (code_ptr
[2] & COND_MASK
);
463 jump
->flags
|= PATCH_B
;
470 static SLJIT_INLINE
void inline_set_jump_addr(sljit_uw jump_ptr
, sljit_sw executable_offset
, sljit_uw new_addr
, sljit_s32 flush_cache
)
472 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
473 sljit_uw
*ptr
= (sljit_uw
*)jump_ptr
;
474 sljit_uw
*inst
= (sljit_uw
*)ptr
[0];
475 sljit_uw mov_pc
= ptr
[1];
476 sljit_s32 bl
= (mov_pc
& 0x0000f000) != RD(TMP_PC
);
477 sljit_sw diff
= (sljit_sw
)(((sljit_sw
)new_addr
- (sljit_sw
)(inst
+ 2) - executable_offset
) >> 2);
479 SLJIT_UNUSED_ARG(executable_offset
);
481 if (diff
<= 0x7fffff && diff
>= -0x800000) {
482 /* Turn to branch. */
485 SLJIT_UPDATE_WX_FLAGS(inst
, inst
+ 1, 0);
487 inst
[0] = (mov_pc
& COND_MASK
) | (B
- CONDITIONAL
) | (diff
& 0xffffff);
489 SLJIT_UPDATE_WX_FLAGS(inst
, inst
+ 1, 1);
490 inst
= (sljit_uw
*)SLJIT_ADD_EXEC_OFFSET(inst
, executable_offset
);
491 SLJIT_CACHE_FLUSH(inst
, inst
+ 1);
495 SLJIT_UPDATE_WX_FLAGS(inst
, inst
+ 2, 0);
497 inst
[0] = (mov_pc
& COND_MASK
) | (BL
- CONDITIONAL
) | (diff
& 0xffffff);
500 SLJIT_UPDATE_WX_FLAGS(inst
, inst
+ 2, 1);
501 inst
= (sljit_uw
*)SLJIT_ADD_EXEC_OFFSET(inst
, executable_offset
);
502 SLJIT_CACHE_FLUSH(inst
, inst
+ 2);
506 /* Get the position of the constant. */
507 if (mov_pc
& (1 << 23))
508 ptr
= inst
+ ((mov_pc
& 0xfff) >> 2) + 2;
512 if (*inst
!= mov_pc
) {
514 SLJIT_UPDATE_WX_FLAGS(inst
, inst
+ (!bl
? 1 : 2), 0);
519 SLJIT_UPDATE_WX_FLAGS(inst
, inst
+ 1, 1);
520 inst
= (sljit_uw
*)SLJIT_ADD_EXEC_OFFSET(inst
, executable_offset
);
521 SLJIT_CACHE_FLUSH(inst
, inst
+ 1);
524 inst
[1] = BLX
| RM(TMP_REG1
);
526 SLJIT_UPDATE_WX_FLAGS(inst
, inst
+ 2, 1);
527 inst
= (sljit_uw
*)SLJIT_ADD_EXEC_OFFSET(inst
, executable_offset
);
528 SLJIT_CACHE_FLUSH(inst
, inst
+ 2);
534 SLJIT_UPDATE_WX_FLAGS(ptr
, ptr
+ 1, 0);
540 SLJIT_UPDATE_WX_FLAGS(ptr
, ptr
+ 1, 1);
544 sljit_uw
*inst
= (sljit_uw
*)jump_ptr
;
546 SLJIT_UNUSED_ARG(executable_offset
);
548 SLJIT_ASSERT((inst
[0] & 0xfff00000) == MOVW
&& (inst
[1] & 0xfff00000) == MOVT
);
551 SLJIT_UPDATE_WX_FLAGS(inst
, inst
+ 2, 0);
554 inst
[0] = MOVW
| (inst
[0] & 0xf000) | ((new_addr
<< 4) & 0xf0000) | (new_addr
& 0xfff);
555 inst
[1] = MOVT
| (inst
[1] & 0xf000) | ((new_addr
>> 12) & 0xf0000) | ((new_addr
>> 16) & 0xfff);
558 SLJIT_UPDATE_WX_FLAGS(inst
, inst
+ 2, 1);
559 inst
= (sljit_uw
*)SLJIT_ADD_EXEC_OFFSET(inst
, executable_offset
);
560 SLJIT_CACHE_FLUSH(inst
, inst
+ 2);
565 static sljit_uw
get_imm(sljit_uw imm
);
566 static sljit_s32
load_immediate(struct sljit_compiler
*compiler
, sljit_s32 reg
, sljit_uw imm
);
568 static SLJIT_INLINE
void inline_set_const(sljit_uw addr
, sljit_sw executable_offset
, sljit_uw new_constant
, sljit_s32 flush_cache
)
570 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
571 sljit_uw
*ptr
= (sljit_uw
*)addr
;
572 sljit_uw
*inst
= (sljit_uw
*)ptr
[0];
573 sljit_uw ldr_literal
= ptr
[1];
576 SLJIT_UNUSED_ARG(executable_offset
);
578 src2
= get_imm(new_constant
);
581 SLJIT_UPDATE_WX_FLAGS(inst
, inst
+ 1, 0);
584 *inst
= 0xe3a00000 | (ldr_literal
& 0xf000) | src2
;
587 SLJIT_UPDATE_WX_FLAGS(inst
, inst
+ 1, 1);
588 inst
= (sljit_uw
*)SLJIT_ADD_EXEC_OFFSET(inst
, executable_offset
);
589 SLJIT_CACHE_FLUSH(inst
, inst
+ 1);
594 src2
= get_imm(~new_constant
);
597 SLJIT_UPDATE_WX_FLAGS(inst
, inst
+ 1, 0);
600 *inst
= 0xe3e00000 | (ldr_literal
& 0xf000) | src2
;
603 SLJIT_UPDATE_WX_FLAGS(inst
, inst
+ 1, 1);
604 inst
= (sljit_uw
*)SLJIT_ADD_EXEC_OFFSET(inst
, executable_offset
);
605 SLJIT_CACHE_FLUSH(inst
, inst
+ 1);
610 if (ldr_literal
& (1 << 23))
611 ptr
= inst
+ ((ldr_literal
& 0xfff) >> 2) + 2;
615 if (*inst
!= ldr_literal
) {
617 SLJIT_UPDATE_WX_FLAGS(inst
, inst
+ 1, 0);
623 SLJIT_UPDATE_WX_FLAGS(inst
, inst
+ 1, 1);
624 inst
= (sljit_uw
*)SLJIT_ADD_EXEC_OFFSET(inst
, executable_offset
);
625 SLJIT_CACHE_FLUSH(inst
, inst
+ 1);
630 SLJIT_UPDATE_WX_FLAGS(ptr
, ptr
+ 1, 0);
636 SLJIT_UPDATE_WX_FLAGS(ptr
, ptr
+ 1, 1);
639 sljit_uw
*inst
= (sljit_uw
*)addr
;
641 SLJIT_UNUSED_ARG(executable_offset
);
643 SLJIT_ASSERT((inst
[0] & 0xfff00000) == MOVW
&& (inst
[1] & 0xfff00000) == MOVT
);
646 SLJIT_UPDATE_WX_FLAGS(inst
, inst
+ 2, 0);
649 inst
[0] = MOVW
| (inst
[0] & 0xf000) | ((new_constant
<< 4) & 0xf0000) | (new_constant
& 0xfff);
650 inst
[1] = MOVT
| (inst
[1] & 0xf000) | ((new_constant
>> 12) & 0xf0000) | ((new_constant
>> 16) & 0xfff);
653 SLJIT_UPDATE_WX_FLAGS(inst
, inst
+ 2, 1);
654 inst
= (sljit_uw
*)SLJIT_ADD_EXEC_OFFSET(inst
, executable_offset
);
655 SLJIT_CACHE_FLUSH(inst
, inst
+ 2);
660 SLJIT_API_FUNC_ATTRIBUTE
void* sljit_generate_code(struct sljit_compiler
*compiler
)
662 struct sljit_memory_fragment
*buf
;
670 sljit_sw executable_offset
;
672 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
674 sljit_uw cpool_skip_alignment
;
675 sljit_uw cpool_current_index
;
676 sljit_uw
*cpool_start_address
;
677 sljit_uw
*last_pc_patch
;
678 struct future_patch
*first_patch
;
681 struct sljit_label
*label
;
682 struct sljit_jump
*jump
;
683 struct sljit_const
*const_
;
684 struct sljit_put_label
*put_label
;
687 CHECK_PTR(check_sljit_generate_code(compiler
));
688 reverse_buf(compiler
);
690 /* Second code generation pass. */
691 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
692 size
= compiler
->size
+ (compiler
->patches
<< 1);
693 if (compiler
->cpool_fill
> 0)
694 size
+= compiler
->cpool_fill
+ CONST_POOL_ALIGNMENT
- 1;
696 size
= compiler
->size
;
698 code
= (sljit_uw
*)SLJIT_MALLOC_EXEC(size
* sizeof(sljit_uw
), compiler
->exec_allocator_data
);
699 PTR_FAIL_WITH_EXEC_IF(code
);
702 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
704 cpool_skip_alignment
= 0;
705 cpool_current_index
= 0;
706 cpool_start_address
= NULL
;
708 last_pc_patch
= code
;
714 executable_offset
= SLJIT_EXEC_OFFSET(code
);
716 label
= compiler
->labels
;
717 jump
= compiler
->jumps
;
718 const_
= compiler
->consts
;
719 put_label
= compiler
->put_labels
;
721 if (label
&& label
->size
== 0) {
722 label
->addr
= (sljit_uw
)SLJIT_ADD_EXEC_OFFSET(code
, executable_offset
);
727 buf_ptr
= (sljit_uw
*)buf
->memory
;
728 buf_end
= buf_ptr
+ (buf
->used_size
>> 2);
731 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
732 if (cpool_size
> 0) {
733 if (cpool_skip_alignment
> 0) {
735 cpool_skip_alignment
--;
738 if (SLJIT_UNLIKELY(resolve_const_pool_index(compiler
, &first_patch
, cpool_current_index
, cpool_start_address
, buf_ptr
))) {
739 SLJIT_FREE_EXEC(code
, compiler
->exec_allocator_data
);
740 compiler
->error
= SLJIT_ERR_ALLOC_FAILED
;
744 if (++cpool_current_index
>= cpool_size
) {
745 SLJIT_ASSERT(!first_patch
);
747 if (label
&& label
->size
== word_count
) {
748 /* Points after the current instruction. */
749 label
->addr
= (sljit_uw
)SLJIT_ADD_EXEC_OFFSET(code_ptr
, executable_offset
);
750 label
->size
= (sljit_uw
)(code_ptr
- code
);
753 next_addr
= compute_next_addr(label
, jump
, const_
, put_label
);
758 else if ((*buf_ptr
& 0xff000000) != PUSH_POOL
) {
760 *code_ptr
= *buf_ptr
++;
761 if (next_addr
== word_count
) {
762 SLJIT_ASSERT(!label
|| label
->size
>= word_count
);
763 SLJIT_ASSERT(!jump
|| jump
->addr
>= word_count
);
764 SLJIT_ASSERT(!const_
|| const_
->addr
>= word_count
);
765 SLJIT_ASSERT(!put_label
|| put_label
->addr
>= word_count
);
767 /* These structures are ordered by their address. */
768 if (jump
&& jump
->addr
== word_count
) {
769 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
770 if (detect_jump_type(jump
, code_ptr
, code
, executable_offset
))
772 jump
->addr
= (sljit_uw
)code_ptr
;
774 jump
->addr
= (sljit_uw
)(code_ptr
- 2);
775 if (detect_jump_type(jump
, code_ptr
, code
, executable_offset
))
780 if (label
&& label
->size
== word_count
) {
781 /* code_ptr can be affected above. */
782 label
->addr
= (sljit_uw
)SLJIT_ADD_EXEC_OFFSET(code_ptr
+ 1, executable_offset
);
783 label
->size
= (sljit_uw
)((code_ptr
+ 1) - code
);
786 if (const_
&& const_
->addr
== word_count
) {
787 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
788 const_
->addr
= (sljit_uw
)code_ptr
;
790 const_
->addr
= (sljit_uw
)(code_ptr
- 1);
792 const_
= const_
->next
;
794 if (put_label
&& put_label
->addr
== word_count
) {
795 SLJIT_ASSERT(put_label
->label
);
796 put_label
->addr
= (sljit_uw
)code_ptr
;
797 put_label
= put_label
->next
;
799 next_addr
= compute_next_addr(label
, jump
, const_
, put_label
);
802 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
805 /* Fortunately, no need to shift. */
806 cpool_size
= *buf_ptr
++ & ~PUSH_POOL
;
807 SLJIT_ASSERT(cpool_size
> 0);
808 cpool_start_address
= ALIGN_INSTRUCTION(code_ptr
+ 1);
809 cpool_current_index
= patch_pc_relative_loads(last_pc_patch
, code_ptr
, cpool_start_address
, cpool_size
);
810 if (cpool_current_index
> 0) {
811 /* Unconditional branch. */
812 *code_ptr
= B
| (((sljit_uw
)(cpool_start_address
- code_ptr
) + cpool_current_index
- 2) & ~PUSH_POOL
);
813 code_ptr
= (sljit_uw
*)(cpool_start_address
+ cpool_current_index
);
815 cpool_skip_alignment
= CONST_POOL_ALIGNMENT
- 1;
816 cpool_current_index
= 0;
817 last_pc_patch
= code_ptr
;
820 } while (buf_ptr
< buf_end
);
824 SLJIT_ASSERT(!label
);
826 SLJIT_ASSERT(!const_
);
827 SLJIT_ASSERT(!put_label
);
829 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
830 SLJIT_ASSERT(cpool_size
== 0);
831 if (compiler
->cpool_fill
> 0) {
832 cpool_start_address
= ALIGN_INSTRUCTION(code_ptr
);
833 cpool_current_index
= patch_pc_relative_loads(last_pc_patch
, code_ptr
, cpool_start_address
, compiler
->cpool_fill
);
834 if (cpool_current_index
> 0)
835 code_ptr
= (sljit_uw
*)(cpool_start_address
+ cpool_current_index
);
837 buf_ptr
= compiler
->cpool
;
838 buf_end
= buf_ptr
+ compiler
->cpool_fill
;
839 cpool_current_index
= 0;
840 while (buf_ptr
< buf_end
) {
841 if (SLJIT_UNLIKELY(resolve_const_pool_index(compiler
, &first_patch
, cpool_current_index
, cpool_start_address
, buf_ptr
))) {
842 SLJIT_FREE_EXEC(code
, compiler
->exec_allocator_data
);
843 compiler
->error
= SLJIT_ERR_ALLOC_FAILED
;
847 cpool_current_index
++;
849 SLJIT_ASSERT(!first_patch
);
853 jump
= compiler
->jumps
;
855 buf_ptr
= (sljit_uw
*)jump
->addr
;
857 if (jump
->flags
& PATCH_B
) {
858 addr
= (sljit_uw
)SLJIT_ADD_EXEC_OFFSET(buf_ptr
+ 2, executable_offset
);
859 if (!(jump
->flags
& JUMP_ADDR
)) {
860 SLJIT_ASSERT(jump
->flags
& JUMP_LABEL
);
861 SLJIT_ASSERT((sljit_sw
)(jump
->u
.label
->addr
- addr
) <= 0x01ffffff && (sljit_sw
)(jump
->u
.label
->addr
- addr
) >= -0x02000000);
862 *buf_ptr
|= ((jump
->u
.label
->addr
- addr
) >> 2) & 0x00ffffff;
865 SLJIT_ASSERT((sljit_sw
)(jump
->u
.target
- addr
) <= 0x01ffffff && (sljit_sw
)(jump
->u
.target
- addr
) >= -0x02000000);
866 *buf_ptr
|= ((jump
->u
.target
- addr
) >> 2) & 0x00ffffff;
869 else if (jump
->flags
& SLJIT_REWRITABLE_JUMP
) {
870 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
871 jump
->addr
= (sljit_uw
)code_ptr
;
872 code_ptr
[0] = (sljit_uw
)buf_ptr
;
873 code_ptr
[1] = *buf_ptr
;
874 inline_set_jump_addr((sljit_uw
)code_ptr
, executable_offset
, (jump
->flags
& JUMP_LABEL
) ? jump
->u
.label
->addr
: jump
->u
.target
, 0);
877 inline_set_jump_addr((sljit_uw
)buf_ptr
, executable_offset
, (jump
->flags
& JUMP_LABEL
) ? jump
->u
.label
->addr
: jump
->u
.target
, 0);
881 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
882 if (jump
->flags
& IS_BL
)
884 if (*buf_ptr
& (1 << 23))
885 buf_ptr
+= ((*buf_ptr
& 0xfff) >> 2) + 2;
888 *buf_ptr
= (jump
->flags
& JUMP_LABEL
) ? jump
->u
.label
->addr
: jump
->u
.target
;
890 inline_set_jump_addr((sljit_uw
)buf_ptr
, executable_offset
, (jump
->flags
& JUMP_LABEL
) ? jump
->u
.label
->addr
: jump
->u
.target
, 0);
896 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
897 const_
= compiler
->consts
;
899 buf_ptr
= (sljit_uw
*)const_
->addr
;
900 const_
->addr
= (sljit_uw
)code_ptr
;
902 code_ptr
[0] = (sljit_uw
)buf_ptr
;
903 code_ptr
[1] = *buf_ptr
;
904 if (*buf_ptr
& (1 << 23))
905 buf_ptr
+= ((*buf_ptr
& 0xfff) >> 2) + 2;
908 /* Set the value again (can be a simple constant). */
909 inline_set_const((sljit_uw
)code_ptr
, executable_offset
, *buf_ptr
, 0);
912 const_
= const_
->next
;
916 put_label
= compiler
->put_labels
;
918 addr
= put_label
->label
->addr
;
919 buf_ptr
= (sljit_uw
*)put_label
->addr
;
921 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
922 SLJIT_ASSERT((buf_ptr
[0] & 0xffff0000) == 0xe59f0000);
923 buf_ptr
[((buf_ptr
[0] & 0xfff) >> 2) + 2] = addr
;
925 SLJIT_ASSERT((buf_ptr
[-1] & 0xfff00000) == MOVW
&& (buf_ptr
[0] & 0xfff00000) == MOVT
);
926 buf_ptr
[-1] |= ((addr
<< 4) & 0xf0000) | (addr
& 0xfff);
927 buf_ptr
[0] |= ((addr
>> 12) & 0xf0000) | ((addr
>> 16) & 0xfff);
929 put_label
= put_label
->next
;
932 SLJIT_ASSERT(code_ptr
- code
<= (sljit_s32
)size
);
934 compiler
->error
= SLJIT_ERR_COMPILED
;
935 compiler
->executable_offset
= executable_offset
;
936 compiler
->executable_size
= (sljit_uw
)(code_ptr
- code
) * sizeof(sljit_uw
);
938 code
= (sljit_uw
*)SLJIT_ADD_EXEC_OFFSET(code
, executable_offset
);
939 code_ptr
= (sljit_uw
*)SLJIT_ADD_EXEC_OFFSET(code_ptr
, executable_offset
);
941 SLJIT_CACHE_FLUSH(code
, code_ptr
);
942 SLJIT_UPDATE_WX_FLAGS(code
, code_ptr
, 1);
946 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_has_cpu_feature(sljit_s32 feature_type
)
948 switch (feature_type
) {
950 #ifdef SLJIT_IS_FPU_AVAILABLE
951 return SLJIT_IS_FPU_AVAILABLE
;
953 /* Available by default. */
959 #if (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
960 case SLJIT_HAS_PREFETCH
:
969 /* --------------------------------------------------------------------- */
971 /* --------------------------------------------------------------------- */
973 /* Creates an index in data_transfer_insts array. */
974 #define WORD_SIZE 0x00
975 #define BYTE_SIZE 0x01
976 #define HALF_SIZE 0x02
979 #define LOAD_DATA 0x08
981 /* Flag bits for emit_op. */
982 #define ALLOW_IMM 0x10
983 #define ALLOW_INV_IMM 0x20
984 #define ALLOW_ANY_IMM (ALLOW_IMM | ALLOW_INV_IMM)
985 #define ALLOW_NEG_IMM 0x40
987 /* s/l - store/load (1 bit)
988 u/s - signed/unsigned (1 bit)
989 w/b/h/N - word/byte/half/NOT allowed (2 bit)
990 Storing signed and unsigned values are the same operations. */
992 static const sljit_uw data_transfer_insts
[16] = {
993 /* s u w */ 0xe5000000 /* str */,
994 /* s u b */ 0xe5400000 /* strb */,
995 /* s u h */ 0xe10000b0 /* strh */,
996 /* s u N */ 0x00000000 /* not allowed */,
997 /* s s w */ 0xe5000000 /* str */,
998 /* s s b */ 0xe5400000 /* strb */,
999 /* s s h */ 0xe10000b0 /* strh */,
1000 /* s s N */ 0x00000000 /* not allowed */,
1002 /* l u w */ 0xe5100000 /* ldr */,
1003 /* l u b */ 0xe5500000 /* ldrb */,
1004 /* l u h */ 0xe11000b0 /* ldrh */,
1005 /* l u p */ 0xf5500000 /* preload */,
1006 /* l s w */ 0xe5100000 /* ldr */,
1007 /* l s b */ 0xe11000d0 /* ldrsb */,
1008 /* l s h */ 0xe11000f0 /* ldrsh */,
1009 /* l s N */ 0x00000000 /* not allowed */,
1012 #define EMIT_DATA_TRANSFER(type, add, target_reg, base_reg, arg) \
1013 (data_transfer_insts[(type) & 0xf] | ((add) << 23) | RD(target_reg) | RN(base_reg) | (sljit_uw)(arg))
1015 /* Normal ldr/str instruction.
1016 Type2: ldrsb, ldrh, ldrsh */
1017 #define IS_TYPE1_TRANSFER(type) \
1018 (data_transfer_insts[(type) & 0xf] & 0x04000000)
1019 #define TYPE2_TRANSFER_IMM(imm) \
1020 (((imm) & 0xf) | (((imm) & 0xf0) << 4) | (1 << 22))
1022 #define EMIT_FPU_OPERATION(opcode, mode, dst, src1, src2) \
1023 ((sljit_uw)(opcode) | (sljit_uw)(mode) | VD(dst) | VM(src1) | VN(src2))
1025 /* Flags for emit_op: */
1026 /* Arguments are swapped. */
1027 #define ARGS_SWAPPED 0x01
1028 /* Inverted immediate. */
1029 #define INV_IMM 0x02
1030 /* Source and destination is register. */
1031 #define MOVE_REG_CONV 0x04
1032 /* Unused return value. */
1033 #define UNUSED_RETURN 0x08
1034 /* SET_FLAGS must be (1 << 20) as it is also the value of S bit (can be used for optimization). */
1035 #define SET_FLAGS (1 << 20)
1038 src2: reg or imm (if allowed)
1039 SRC2_IMM must be (1 << 25) as it is also the value of I bit (can be used for optimization). */
1040 #define SRC2_IMM (1 << 25)
1042 static sljit_s32
emit_op(struct sljit_compiler
*compiler
, sljit_s32 op
, sljit_s32 inp_flags
,
1043 sljit_s32 dst
, sljit_sw dstw
,
1044 sljit_s32 src1
, sljit_sw src1w
,
1045 sljit_s32 src2
, sljit_sw src2w
);
1047 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_enter(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_uw imm
, offset
;
1052 sljit_s32 i
, tmp
, size
, word_arg_count
, saved_arg_count
;
1054 sljit_u32 float_arg_count
;
1056 sljit_u32 old_offset
, f32_offset
;
1058 sljit_u32
*remap_ptr
= remap
;
1062 CHECK(check_sljit_emit_enter(compiler
, options
, arg_types
, scratches
, saveds
, fscratches
, fsaveds
, local_size
));
1063 set_emit_enter(compiler
, options
, arg_types
, scratches
, saveds
, fscratches
, fsaveds
, local_size
);
1067 tmp
= SLJIT_S0
- saveds
;
1068 for (i
= SLJIT_S0
; i
> tmp
; i
--)
1069 imm
|= (sljit_uw
)1 << reg_map
[i
];
1071 for (i
= scratches
; i
>= SLJIT_FIRST_SAVED_REG
; i
--)
1072 imm
|= (sljit_uw
)1 << reg_map
[i
];
1074 SLJIT_ASSERT(reg_map
[TMP_REG2
] == 14);
1076 /* Push saved and temporary registers
1077 multiple registers: stmdb sp!, {..., lr}
1078 single register: str reg, [sp, #-4]! */
1080 FAIL_IF(push_inst(compiler
, PUSH
| (1 << 14) | imm
));
1082 FAIL_IF(push_inst(compiler
, 0xe52d0004 | RD(TMP_REG2
)));
1084 /* Stack must be aligned to 8 bytes: */
1085 size
= GET_SAVED_REGISTERS_SIZE(scratches
, saveds
, 1);
1087 if (fsaveds
> 0 || fscratches
>= SLJIT_FIRST_SAVED_FLOAT_REG
) {
1088 if ((size
& SSIZE_OF(sw
)) != 0) {
1089 FAIL_IF(push_inst(compiler
, SUB
| RD(SLJIT_SP
) | RN(SLJIT_SP
) | SRC2_IMM
| sizeof(sljit_sw
)));
1090 size
+= SSIZE_OF(sw
);
1093 if (fsaveds
+ fscratches
>= SLJIT_NUMBER_OF_FLOAT_REGISTERS
) {
1094 FAIL_IF(push_inst(compiler
, VPUSH
| VD(SLJIT_FS0
) | ((sljit_uw
)SLJIT_NUMBER_OF_SAVED_FLOAT_REGISTERS
<< 1)));
1097 FAIL_IF(push_inst(compiler
, VPUSH
| VD(SLJIT_FS0
) | ((sljit_uw
)fsaveds
<< 1)));
1098 if (fscratches
>= SLJIT_FIRST_SAVED_FLOAT_REG
)
1099 FAIL_IF(push_inst(compiler
, VPUSH
| VD(fscratches
) | ((sljit_uw
)(fscratches
- (SLJIT_FIRST_SAVED_FLOAT_REG
- 1)) << 1)));
1103 local_size
= ((size
+ local_size
+ 0x7) & ~0x7) - size
;
1104 compiler
->local_size
= local_size
;
1106 arg_types
>>= SLJIT_ARG_SHIFT
;
1108 saved_arg_count
= 0;
1110 SLJIT_COMPILE_ASSERT(SLJIT_FR0
== 1, float_register_index_start
);
1113 float_arg_count
= 0;
1116 switch (arg_types
& SLJIT_ARG_MASK
) {
1117 case SLJIT_ARG_TYPE_F64
:
1119 offset
+= sizeof(sljit_sw
);
1121 if (offset
< 4 * sizeof(sljit_sw
))
1122 FAIL_IF(push_inst(compiler
, VMOV2
| (offset
<< 10) | ((offset
+ sizeof(sljit_sw
)) << 14) | float_arg_count
));
1124 FAIL_IF(push_inst(compiler
, VLDR_F32
| 0x800100 | RN(SLJIT_SP
)
1125 | (float_arg_count
<< 12) | ((offset
+ (sljit_uw
)size
- 4 * sizeof(sljit_sw
)) >> 2)));
1127 offset
+= sizeof(sljit_f64
) - sizeof(sljit_sw
);
1129 case SLJIT_ARG_TYPE_F32
:
1130 if (offset
< 4 * sizeof(sljit_sw
))
1131 FAIL_IF(push_inst(compiler
, VMOV
| (float_arg_count
<< 16) | (offset
<< 10)));
1133 FAIL_IF(push_inst(compiler
, VLDR_F32
| 0x800000 | RN(SLJIT_SP
)
1134 | (float_arg_count
<< 12) | ((offset
+ (sljit_uw
)size
- 4 * sizeof(sljit_sw
)) >> 2)));
1140 if (!(arg_types
& SLJIT_ARG_TYPE_SCRATCH_REG
)) {
1141 tmp
= SLJIT_S0
- saved_arg_count
;
1143 } else if (word_arg_count
- 1 != (sljit_s32
)(offset
>> 2))
1144 tmp
= word_arg_count
;
1148 if (offset
< 4 * sizeof(sljit_sw
))
1149 FAIL_IF(push_inst(compiler
, MOV
| RD(tmp
) | (offset
>> 2)));
1151 FAIL_IF(push_inst(compiler
, data_transfer_insts
[WORD_SIZE
| LOAD_DATA
] | 0x800000
1152 | RN(SLJIT_SP
) | RD(tmp
) | (offset
+ (sljit_uw
)size
- 4 * sizeof(sljit_sw
))));
1156 offset
+= sizeof(sljit_sw
);
1157 arg_types
>>= SLJIT_ARG_SHIFT
;
1160 compiler
->args_size
= offset
;
1163 old_offset
= SLJIT_FR0
;
1167 switch (arg_types
& SLJIT_ARG_MASK
) {
1168 case SLJIT_ARG_TYPE_F64
:
1169 if (offset
!= old_offset
)
1170 *remap_ptr
++ = EMIT_FPU_OPERATION(VMOV_F32
, SLJIT_32
, offset
, old_offset
, 0);
1174 case SLJIT_ARG_TYPE_F32
:
1175 if (f32_offset
!= 0) {
1176 *remap_ptr
++ = EMIT_FPU_OPERATION(VMOV_F32
, 0x20, offset
, f32_offset
, 0);
1179 if (offset
!= old_offset
)
1180 *remap_ptr
++ = EMIT_FPU_OPERATION(VMOV_F32
, 0, offset
, old_offset
, 0);
1181 f32_offset
= old_offset
;
1187 if (!(arg_types
& SLJIT_ARG_TYPE_SCRATCH_REG
)) {
1188 FAIL_IF(push_inst(compiler
, MOV
| RD(SLJIT_S0
- saved_arg_count
) | RM(SLJIT_R0
+ word_arg_count
)));
1195 arg_types
>>= SLJIT_ARG_SHIFT
;
1198 SLJIT_ASSERT((sljit_uw
)(remap_ptr
- remap
) <= sizeof(remap
));
1200 while (remap_ptr
> remap
)
1201 FAIL_IF(push_inst(compiler
, *(--remap_ptr
)));
1205 FAIL_IF(emit_op(compiler
, SLJIT_SUB
, ALLOW_IMM
, SLJIT_SP
, 0, SLJIT_SP
, 0, SLJIT_IMM
, local_size
));
1207 return SLJIT_SUCCESS
;
1210 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_set_context(struct sljit_compiler
*compiler
,
1211 sljit_s32 options
, sljit_s32 arg_types
, sljit_s32 scratches
, sljit_s32 saveds
,
1212 sljit_s32 fscratches
, sljit_s32 fsaveds
, sljit_s32 local_size
)
1217 CHECK(check_sljit_set_context(compiler
, options
, arg_types
, scratches
, saveds
, fscratches
, fsaveds
, local_size
));
1218 set_set_context(compiler
, options
, arg_types
, scratches
, saveds
, fscratches
, fsaveds
, local_size
);
1220 size
= GET_SAVED_REGISTERS_SIZE(scratches
, saveds
, 1);
1222 if ((size
& SSIZE_OF(sw
)) != 0 && (fsaveds
> 0 || fscratches
>= SLJIT_FIRST_SAVED_FLOAT_REG
))
1223 size
+= SSIZE_OF(sw
);
1225 compiler
->local_size
= ((size
+ local_size
+ 0x7) & ~0x7) - size
;
1226 return SLJIT_SUCCESS
;
1229 static sljit_s32
emit_add_sp(struct sljit_compiler
*compiler
, sljit_uw imm
)
1231 sljit_uw imm2
= get_imm(imm
);
1234 FAIL_IF(load_immediate(compiler
, TMP_REG2
, imm
));
1235 imm2
= RM(TMP_REG2
);
1238 return push_inst(compiler
, ADD
| RD(SLJIT_SP
) | RN(SLJIT_SP
) | imm2
);
1241 static sljit_s32
emit_stack_frame_release(struct sljit_compiler
*compiler
, sljit_s32 frame_size
)
1243 sljit_s32 local_size
, fscratches
, fsaveds
, i
, tmp
;
1244 sljit_s32 lr_dst
= TMP_PC
;
1247 SLJIT_ASSERT(reg_map
[TMP_REG2
] == 14);
1249 local_size
= compiler
->local_size
;
1250 fscratches
= compiler
->fscratches
;
1251 fsaveds
= compiler
->fsaveds
;
1253 if (fsaveds
> 0 || fscratches
>= SLJIT_FIRST_SAVED_FLOAT_REG
) {
1255 FAIL_IF(emit_add_sp(compiler
, (sljit_uw
)local_size
));
1257 if (fsaveds
+ fscratches
>= SLJIT_NUMBER_OF_FLOAT_REGISTERS
) {
1258 FAIL_IF(push_inst(compiler
, VPOP
| VD(SLJIT_FS0
) | ((sljit_uw
)SLJIT_NUMBER_OF_SAVED_FLOAT_REGISTERS
<< 1)));
1260 if (fscratches
>= SLJIT_FIRST_SAVED_FLOAT_REG
)
1261 FAIL_IF(push_inst(compiler
, VPOP
| VD(fscratches
) | ((sljit_uw
)(fscratches
- (SLJIT_FIRST_SAVED_FLOAT_REG
- 1)) << 1)));
1263 FAIL_IF(push_inst(compiler
, VPOP
| VD(SLJIT_FS0
) | ((sljit_uw
)fsaveds
<< 1)));
1266 local_size
= GET_SAVED_REGISTERS_SIZE(compiler
->scratches
, compiler
->saveds
, 1) & 0x7;
1269 if (frame_size
< 0) {
1272 } else if (frame_size
> 0)
1277 reg_list
|= (sljit_uw
)1 << reg_map
[lr_dst
];
1279 tmp
= SLJIT_S0
- compiler
->saveds
;
1280 for (i
= SLJIT_S0
; i
> tmp
; i
--)
1281 reg_list
|= (sljit_uw
)1 << reg_map
[i
];
1283 for (i
= compiler
->scratches
; i
>= SLJIT_FIRST_SAVED_REG
; i
--)
1284 reg_list
|= (sljit_uw
)1 << reg_map
[i
];
1286 if (lr_dst
== 0 && (reg_list
& (reg_list
- 1)) == 0) {
1287 /* The local_size does not include the saved registers. */
1288 local_size
+= SSIZE_OF(sw
);
1291 local_size
+= SSIZE_OF(sw
);
1293 if (frame_size
> local_size
)
1294 FAIL_IF(push_inst(compiler
, SUB
| RD(SLJIT_SP
) | RN(SLJIT_SP
) | (1 << 25) | (sljit_uw
)(frame_size
- local_size
)));
1295 else if (frame_size
< local_size
)
1296 FAIL_IF(emit_add_sp(compiler
, (sljit_uw
)(local_size
- frame_size
)));
1299 return SLJIT_SUCCESS
;
1301 if (compiler
->saveds
> 0) {
1302 SLJIT_ASSERT(reg_list
== ((sljit_uw
)1 << reg_map
[SLJIT_S0
]));
1305 SLJIT_ASSERT(reg_list
== ((sljit_uw
)1 << reg_map
[SLJIT_FIRST_SAVED_REG
]));
1306 lr_dst
= SLJIT_FIRST_SAVED_REG
;
1309 return push_inst(compiler
, data_transfer_insts
[WORD_SIZE
| LOAD_DATA
] | 0x800000
1310 | RN(SLJIT_SP
) | RD(lr_dst
) | (sljit_uw
)(frame_size
- 2 * SSIZE_OF(sw
)));
1314 FAIL_IF(emit_add_sp(compiler
, (sljit_uw
)local_size
));
1316 /* Pop saved and temporary registers
1317 multiple registers: ldmia sp!, {...}
1318 single register: ldr reg, [sp], #4 */
1319 if ((reg_list
& (reg_list
- 1)) == 0) {
1320 SLJIT_ASSERT(lr_dst
!= 0);
1321 SLJIT_ASSERT(reg_list
== (sljit_uw
)1 << reg_map
[lr_dst
]);
1323 return push_inst(compiler
, 0xe49d0004 | RD(lr_dst
));
1326 FAIL_IF(push_inst(compiler
, POP
| reg_list
));
1328 return push_inst(compiler
, SUB
| RD(SLJIT_SP
) | RN(SLJIT_SP
) | (1 << 25) | ((sljit_uw
)frame_size
- sizeof(sljit_sw
)));
1329 return SLJIT_SUCCESS
;
1332 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_return_void(struct sljit_compiler
*compiler
)
1335 CHECK(check_sljit_emit_return_void(compiler
));
1337 return emit_stack_frame_release(compiler
, 0);
1340 /* --------------------------------------------------------------------- */
1342 /* --------------------------------------------------------------------- */
1344 #define EMIT_SHIFT_INS_AND_RETURN(opcode) \
1345 SLJIT_ASSERT(!(flags & INV_IMM) && !(src2 & SRC2_IMM)); \
1346 if (compiler->shift_imm != 0x20) { \
1347 SLJIT_ASSERT(src1 == TMP_REG1); \
1348 SLJIT_ASSERT(!(flags & ARGS_SWAPPED)); \
1350 if (compiler->shift_imm != 0) \
1351 return push_inst(compiler, MOV | (flags & SET_FLAGS) | \
1352 RD(dst) | (compiler->shift_imm << 7) | (opcode << 5) | RM(src2)); \
1353 return push_inst(compiler, MOV | (flags & SET_FLAGS) | RD(dst) | RM(src2)); \
1355 return push_inst(compiler, MOV | (flags & SET_FLAGS) | RD(dst) \
1356 | RM8((flags & ARGS_SWAPPED) ? src1 : src2) | (sljit_uw)(opcode << 5) \
1357 | 0x10 | RM((flags & ARGS_SWAPPED) ? src2 : src1));
1359 static SLJIT_INLINE sljit_s32
emit_single_op(struct sljit_compiler
*compiler
, sljit_s32 op
, sljit_s32 flags
,
1360 sljit_uw dst
, sljit_uw src1
, sljit_uw src2
)
1362 switch (GET_OPCODE(op
)) {
1364 SLJIT_ASSERT(src1
== TMP_REG1
&& !(flags
& ARGS_SWAPPED
));
1366 if (src2
& SRC2_IMM
) {
1367 return push_inst(compiler
, ((flags
& INV_IMM
) ? MVN
: MOV
) | RD(dst
) | src2
);
1369 return push_inst(compiler
, MOV
| RD(dst
) | RM(src2
));
1371 return SLJIT_SUCCESS
;
1375 SLJIT_ASSERT(src1
== TMP_REG1
&& !(flags
& ARGS_SWAPPED
));
1376 if (flags
& MOVE_REG_CONV
) {
1377 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
1378 if (op
== SLJIT_MOV_U8
)
1379 return push_inst(compiler
, AND
| RD(dst
) | RN(src2
) | SRC2_IMM
| 0xff);
1380 FAIL_IF(push_inst(compiler
, MOV
| RD(dst
) | (24 << 7) | RM(src2
)));
1381 return push_inst(compiler
, MOV
| RD(dst
) | (24 << 7) | (op
== SLJIT_MOV_U8
? 0x20 : 0x40) | RM(dst
));
1383 return push_inst(compiler
, (op
== SLJIT_MOV_U8
? UXTB
: SXTB
) | RD(dst
) | RM(src2
));
1386 else if (dst
!= src2
) {
1387 SLJIT_ASSERT(src2
& SRC2_IMM
);
1388 return push_inst(compiler
, ((flags
& INV_IMM
) ? MVN
: MOV
) | RD(dst
) | src2
);
1390 return SLJIT_SUCCESS
;
1394 SLJIT_ASSERT(src1
== TMP_REG1
&& !(flags
& ARGS_SWAPPED
));
1395 if (flags
& MOVE_REG_CONV
) {
1396 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
1397 FAIL_IF(push_inst(compiler
, MOV
| RD(dst
) | (16 << 7) | RM(src2
)));
1398 return push_inst(compiler
, MOV
| RD(dst
) | (16 << 7) | (op
== SLJIT_MOV_U16
? 0x20 : 0x40) | RM(dst
));
1400 return push_inst(compiler
, (op
== SLJIT_MOV_U16
? UXTH
: SXTH
) | RD(dst
) | RM(src2
));
1403 else if (dst
!= src2
) {
1404 SLJIT_ASSERT(src2
& SRC2_IMM
);
1405 return push_inst(compiler
, ((flags
& INV_IMM
) ? MVN
: MOV
) | RD(dst
) | src2
);
1407 return SLJIT_SUCCESS
;
1410 if (src2
& SRC2_IMM
)
1411 return push_inst(compiler
, ((flags
& INV_IMM
) ? MOV
: MVN
) | (flags
& SET_FLAGS
) | RD(dst
) | src2
);
1413 return push_inst(compiler
, MVN
| (flags
& SET_FLAGS
) | RD(dst
) | RM(src2
));
1416 SLJIT_ASSERT(!(flags
& INV_IMM
));
1417 SLJIT_ASSERT(!(src2
& SRC2_IMM
));
1418 FAIL_IF(push_inst(compiler
, CLZ
| RD(dst
) | RM(src2
)));
1419 return SLJIT_SUCCESS
;
1422 SLJIT_ASSERT(!(flags
& INV_IMM
));
1424 if ((flags
& (UNUSED_RETURN
| ARGS_SWAPPED
)) == UNUSED_RETURN
)
1425 return push_inst(compiler
, CMN
| SET_FLAGS
| RN(src1
) | ((src2
& SRC2_IMM
) ? src2
: RM(src2
)));
1426 return push_inst(compiler
, ADD
| (flags
& SET_FLAGS
) | RD(dst
) | RN(src1
) | ((src2
& SRC2_IMM
) ? src2
: RM(src2
)));
1429 SLJIT_ASSERT(!(flags
& INV_IMM
));
1430 return push_inst(compiler
, ADC
| (flags
& SET_FLAGS
) | RD(dst
) | RN(src1
) | ((src2
& SRC2_IMM
) ? src2
: RM(src2
)));
1433 SLJIT_ASSERT(!(flags
& INV_IMM
));
1435 if ((flags
& (UNUSED_RETURN
| ARGS_SWAPPED
)) == UNUSED_RETURN
)
1436 return push_inst(compiler
, CMP
| SET_FLAGS
| RN(src1
) | ((src2
& SRC2_IMM
) ? src2
: RM(src2
)));
1438 return push_inst(compiler
, (!(flags
& ARGS_SWAPPED
) ? SUB
: RSB
) | (flags
& SET_FLAGS
)
1439 | RD(dst
) | RN(src1
) | ((src2
& SRC2_IMM
) ? src2
: RM(src2
)));
1442 SLJIT_ASSERT(!(flags
& INV_IMM
));
1443 return push_inst(compiler
, (!(flags
& ARGS_SWAPPED
) ? SBC
: RSC
) | (flags
& SET_FLAGS
)
1444 | RD(dst
) | RN(src1
) | ((src2
& SRC2_IMM
) ? src2
: RM(src2
)));
1447 SLJIT_ASSERT(!(flags
& INV_IMM
));
1448 SLJIT_ASSERT(!(src2
& SRC2_IMM
));
1449 compiler
->status_flags_state
= 0;
1452 return push_inst(compiler
, MUL
| RN(dst
) | RM8(src2
) | RM(src1
));
1454 FAIL_IF(push_inst(compiler
, SMULL
| RN(TMP_REG1
) | RD(dst
) | RM8(src2
) | RM(src1
)));
1456 /* cmp TMP_REG1, dst asr #31. */
1457 return push_inst(compiler
, CMP
| SET_FLAGS
| RN(TMP_REG1
) | RM(dst
) | 0xfc0);
1460 if ((flags
& (UNUSED_RETURN
| INV_IMM
)) == UNUSED_RETURN
)
1461 return push_inst(compiler
, TST
| SET_FLAGS
| RN(src1
) | ((src2
& SRC2_IMM
) ? src2
: RM(src2
)));
1462 return push_inst(compiler
, (!(flags
& INV_IMM
) ? AND
: BIC
) | (flags
& SET_FLAGS
)
1463 | RD(dst
) | RN(src1
) | ((src2
& SRC2_IMM
) ? src2
: RM(src2
)));
1466 SLJIT_ASSERT(!(flags
& INV_IMM
));
1467 return push_inst(compiler
, ORR
| (flags
& SET_FLAGS
) | RD(dst
) | RN(src1
) | ((src2
& SRC2_IMM
) ? src2
: RM(src2
)));
1470 SLJIT_ASSERT(!(flags
& INV_IMM
));
1471 return push_inst(compiler
, EOR
| (flags
& SET_FLAGS
) | RD(dst
) | RN(src1
) | ((src2
& SRC2_IMM
) ? src2
: RM(src2
)));
1474 EMIT_SHIFT_INS_AND_RETURN(0);
1477 EMIT_SHIFT_INS_AND_RETURN(1);
1480 EMIT_SHIFT_INS_AND_RETURN(2);
1483 SLJIT_UNREACHABLE();
1484 return SLJIT_SUCCESS
;
1487 #undef EMIT_SHIFT_INS_AND_RETURN
1489 /* Tests whether the immediate can be stored in the 12 bit imm field.
1490 Returns with 0 if not possible. */
1491 static sljit_uw
get_imm(sljit_uw imm
)
1496 return SRC2_IMM
| imm
;
1498 if (!(imm
& 0xff000000)) {
1503 imm
= (imm
<< 24) | (imm
>> 8);
1507 if (!(imm
& 0xff000000)) {
1512 if (!(imm
& 0xf0000000)) {
1517 if (!(imm
& 0xc0000000)) {
1522 if (!(imm
& 0x00ffffff))
1523 return SRC2_IMM
| (imm
>> 24) | (rol
<< 8);
1528 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
1529 static sljit_s32
generate_int(struct sljit_compiler
*compiler
, sljit_s32 reg
, sljit_uw imm
, sljit_s32 positive
)
1536 /* Step1: Search a zero byte (8 continous zero bit). */
1540 if (!(imm
& mask
)) {
1541 /* Rol imm by rol. */
1542 imm
= (imm
<< rol
) | (imm
>> (32 - rol
));
1543 /* Calculate arm rol. */
1544 rol
= 4 + (rol
>> 1);
1551 imm
= (imm
<< 8) | (imm
>> 24);
1555 if (!(imm
& mask
)) {
1556 /* Rol imm by rol. */
1557 imm
= (imm
<< rol
) | (imm
>> (32 - rol
));
1558 /* Calculate arm rol. */
1559 rol
= (rol
>> 1) - 8;
1571 /* The low 8 bit must be zero. */
1572 SLJIT_ASSERT(!(imm
& 0xff));
1574 if (!(imm
& 0xff000000)) {
1575 imm1
= SRC2_IMM
| ((imm
>> 16) & 0xff) | (((rol
+ 4) & 0xf) << 8);
1576 imm2
= SRC2_IMM
| ((imm
>> 8) & 0xff) | (((rol
+ 8) & 0xf) << 8);
1578 else if (imm
& 0xc0000000) {
1579 imm1
= SRC2_IMM
| ((imm
>> 24) & 0xff) | ((rol
& 0xf) << 8);
1583 if (!(imm
& 0xff000000)) {
1588 if (!(imm
& 0xf0000000)) {
1593 if (!(imm
& 0xc0000000)) {
1598 if (!(imm
& 0x00ffffff))
1599 imm2
= SRC2_IMM
| (imm
>> 24) | ((rol
& 0xf) << 8);
1604 if (!(imm
& 0xf0000000)) {
1609 if (!(imm
& 0xc0000000)) {
1614 imm1
= SRC2_IMM
| ((imm
>> 24) & 0xff) | ((rol
& 0xf) << 8);
1618 if (!(imm
& 0xf0000000)) {
1623 if (!(imm
& 0xc0000000)) {
1628 if (!(imm
& 0x00ffffff))
1629 imm2
= SRC2_IMM
| (imm
>> 24) | ((rol
& 0xf) << 8);
1634 FAIL_IF(push_inst(compiler
, (positive
? MOV
: MVN
) | RD(reg
) | imm1
));
1635 FAIL_IF(push_inst(compiler
, (positive
? ORR
: BIC
) | RD(reg
) | RN(reg
) | imm2
));
1640 static sljit_s32
load_immediate(struct sljit_compiler
*compiler
, sljit_s32 reg
, sljit_uw imm
)
1644 #if (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
1645 if (!(imm
& ~(sljit_uw
)0xffff))
1646 return push_inst(compiler
, MOVW
| RD(reg
) | ((imm
<< 4) & 0xf0000) | (imm
& 0xfff));
1649 /* Create imm by 1 inst. */
1652 return push_inst(compiler
, MOV
| RD(reg
) | tmp
);
1654 tmp
= get_imm(~imm
);
1656 return push_inst(compiler
, MVN
| RD(reg
) | tmp
);
1658 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
1659 /* Create imm by 2 inst. */
1660 FAIL_IF(generate_int(compiler
, reg
, imm
, 1));
1661 FAIL_IF(generate_int(compiler
, reg
, ~imm
, 0));
1664 return push_inst_with_literal(compiler
, EMIT_DATA_TRANSFER(WORD_SIZE
| LOAD_DATA
, 1, reg
, TMP_PC
, 0), imm
);
1666 FAIL_IF(push_inst(compiler
, MOVW
| RD(reg
) | ((imm
<< 4) & 0xf0000) | (imm
& 0xfff)));
1668 return SLJIT_SUCCESS
;
1669 return push_inst(compiler
, MOVT
| RD(reg
) | ((imm
>> 12) & 0xf0000) | ((imm
>> 16) & 0xfff));
1673 static SLJIT_INLINE sljit_s32
emit_op_mem(struct sljit_compiler
*compiler
, sljit_s32 flags
, sljit_s32 reg
,
1674 sljit_s32 arg
, sljit_sw argw
, sljit_s32 tmp_reg
)
1676 sljit_uw imm
, offset_reg
;
1677 sljit_uw is_type1_transfer
= IS_TYPE1_TRANSFER(flags
);
1679 SLJIT_ASSERT (arg
& SLJIT_MEM
);
1680 SLJIT_ASSERT((arg
& REG_MASK
) != tmp_reg
);
1682 if (!(arg
& REG_MASK
)) {
1683 if (is_type1_transfer
) {
1684 FAIL_IF(load_immediate(compiler
, tmp_reg
, (sljit_uw
)argw
& ~(sljit_uw
)0xfff));
1688 FAIL_IF(load_immediate(compiler
, tmp_reg
, (sljit_uw
)argw
& ~(sljit_uw
)0xff));
1692 return push_inst(compiler
, EMIT_DATA_TRANSFER(flags
, 1, reg
, tmp_reg
,
1693 is_type1_transfer
? argw
: TYPE2_TRANSFER_IMM(argw
)));
1696 if (arg
& OFFS_REG_MASK
) {
1697 offset_reg
= OFFS_REG(arg
);
1701 if (argw
!= 0 && !is_type1_transfer
) {
1702 FAIL_IF(push_inst(compiler
, ADD
| RD(tmp_reg
) | RN(arg
) | RM(offset_reg
) | ((sljit_uw
)argw
<< 7)));
1703 return push_inst(compiler
, EMIT_DATA_TRANSFER(flags
, 1, reg
, tmp_reg
, TYPE2_TRANSFER_IMM(0)));
1706 /* Bit 25: RM is offset. */
1707 return push_inst(compiler
, EMIT_DATA_TRANSFER(flags
, 1, reg
, arg
,
1708 RM(offset_reg
) | (is_type1_transfer
? (1 << 25) : 0) | ((sljit_uw
)argw
<< 7)));
1713 if (is_type1_transfer
) {
1715 imm
= get_imm((sljit_uw
)argw
& ~(sljit_uw
)0xfff);
1717 FAIL_IF(push_inst(compiler
, ADD
| RD(tmp_reg
) | RN(arg
) | imm
));
1718 argw
= argw
& 0xfff;
1722 else if (argw
< -0xfff) {
1723 imm
= get_imm((sljit_uw
)-argw
& ~(sljit_uw
)0xfff);
1725 FAIL_IF(push_inst(compiler
, SUB
| RD(tmp_reg
) | RN(arg
) | imm
));
1726 argw
= -(-argw
& 0xfff);
1731 if (argw
>= 0 && argw
<= 0xfff)
1732 return push_inst(compiler
, EMIT_DATA_TRANSFER(flags
, 1, reg
, arg
, argw
));
1734 if (argw
< 0 && argw
>= -0xfff)
1735 return push_inst(compiler
, EMIT_DATA_TRANSFER(flags
, 0, reg
, arg
, -argw
));
1739 imm
= get_imm((sljit_uw
)argw
& ~(sljit_uw
)0xff);
1741 FAIL_IF(push_inst(compiler
, ADD
| RD(tmp_reg
) | RN(arg
) | imm
));
1746 else if (argw
< -0xff) {
1747 imm
= get_imm((sljit_uw
)-argw
& ~(sljit_uw
)0xff);
1749 FAIL_IF(push_inst(compiler
, SUB
| RD(tmp_reg
) | RN(arg
) | imm
));
1750 argw
= -(-argw
& 0xff);
1755 if (argw
>= 0 && argw
<= 0xff)
1756 return push_inst(compiler
, EMIT_DATA_TRANSFER(flags
, 1, reg
, arg
, TYPE2_TRANSFER_IMM(argw
)));
1758 if (argw
< 0 && argw
>= -0xff) {
1760 return push_inst(compiler
, EMIT_DATA_TRANSFER(flags
, 0, reg
, arg
, TYPE2_TRANSFER_IMM(argw
)));
1764 FAIL_IF(load_immediate(compiler
, tmp_reg
, (sljit_uw
)argw
));
1765 return push_inst(compiler
, EMIT_DATA_TRANSFER(flags
, 1, reg
, arg
,
1766 RM(tmp_reg
) | (is_type1_transfer
? (1 << 25) : 0)));
1769 static sljit_s32
emit_op(struct sljit_compiler
*compiler
, sljit_s32 op
, sljit_s32 inp_flags
,
1770 sljit_s32 dst
, sljit_sw dstw
,
1771 sljit_s32 src1
, sljit_sw src1w
,
1772 sljit_s32 src2
, sljit_sw src2w
)
1774 /* src1 is reg or TMP_REG1
1775 src2 is reg, TMP_REG2, or imm
1776 result goes to TMP_REG2, so put result can use TMP_REG1. */
1778 /* We prefers register and simple consts. */
1781 sljit_s32 src2_reg
= 0;
1782 sljit_s32 flags
= HAS_FLAGS(op
) ? SET_FLAGS
: 0;
1783 sljit_s32 neg_op
= 0;
1785 if (dst
== TMP_REG2
)
1786 flags
|= UNUSED_RETURN
;
1788 SLJIT_ASSERT(!(inp_flags
& ALLOW_INV_IMM
) || (inp_flags
& ALLOW_IMM
));
1790 if (inp_flags
& ALLOW_NEG_IMM
) {
1791 switch (GET_OPCODE(op
)) {
1793 compiler
->status_flags_state
= SLJIT_CURRENT_FLAGS_ADD
;
1797 compiler
->status_flags_state
= SLJIT_CURRENT_FLAGS_ADD
;
1798 neg_op
= SLJIT_SUBC
;
1801 compiler
->status_flags_state
= SLJIT_CURRENT_FLAGS_SUB
;
1805 compiler
->status_flags_state
= SLJIT_CURRENT_FLAGS_SUB
;
1806 neg_op
= SLJIT_ADDC
;
1812 if (!(inp_flags
& ALLOW_IMM
))
1815 if (src2
& SLJIT_IMM
) {
1816 src2_reg
= (sljit_s32
)get_imm((sljit_uw
)src2w
);
1819 if (inp_flags
& ALLOW_INV_IMM
) {
1820 src2_reg
= (sljit_s32
)get_imm(~(sljit_uw
)src2w
);
1827 src2_reg
= (sljit_s32
)get_imm((sljit_uw
)-src2w
);
1829 op
= neg_op
| GET_ALL_FLAGS(op
);
1835 if (src1
& SLJIT_IMM
) {
1836 src2_reg
= (sljit_s32
)get_imm((sljit_uw
)src1w
);
1838 flags
|= ARGS_SWAPPED
;
1843 if (inp_flags
& ALLOW_INV_IMM
) {
1844 src2_reg
= (sljit_s32
)get_imm(~(sljit_uw
)src1w
);
1846 flags
|= ARGS_SWAPPED
| INV_IMM
;
1852 if (neg_op
>= SLJIT_SUB
) {
1853 /* Note: additive operation (commutative). */
1854 src2_reg
= (sljit_s32
)get_imm((sljit_uw
)-src1w
);
1858 op
= neg_op
| GET_ALL_FLAGS(op
);
1866 if (FAST_IS_REG(src1
))
1868 else if (src1
& SLJIT_MEM
) {
1869 FAIL_IF(emit_op_mem(compiler
, inp_flags
| LOAD_DATA
, TMP_REG1
, src1
, src1w
, TMP_REG1
));
1870 src1_reg
= TMP_REG1
;
1873 FAIL_IF(load_immediate(compiler
, TMP_REG1
, (sljit_uw
)src1w
));
1874 src1_reg
= TMP_REG1
;
1878 dst_reg
= FAST_IS_REG(dst
) ? dst
: TMP_REG2
;
1880 if (op
<= SLJIT_MOV_P
) {
1881 if (dst
& SLJIT_MEM
) {
1882 if (inp_flags
& BYTE_SIZE
)
1883 inp_flags
&= ~SIGNED
;
1885 if (FAST_IS_REG(src2
))
1886 return emit_op_mem(compiler
, inp_flags
, src2
, dst
, dstw
, TMP_REG2
);
1889 if (FAST_IS_REG(src2
) && dst_reg
!= TMP_REG2
)
1890 flags
|= MOVE_REG_CONV
;
1894 if (src2_reg
== 0) {
1895 src2_reg
= (op
<= SLJIT_MOV_P
) ? dst_reg
: TMP_REG2
;
1897 if (FAST_IS_REG(src2
))
1899 else if (src2
& SLJIT_MEM
)
1900 FAIL_IF(emit_op_mem(compiler
, inp_flags
| LOAD_DATA
, src2_reg
, src2
, src2w
, TMP_REG2
));
1902 FAIL_IF(load_immediate(compiler
, src2_reg
, (sljit_uw
)src2w
));
1905 FAIL_IF(emit_single_op(compiler
, op
, flags
, (sljit_uw
)dst_reg
, (sljit_uw
)src1_reg
, (sljit_uw
)src2_reg
));
1907 if (!(dst
& SLJIT_MEM
))
1908 return SLJIT_SUCCESS
;
1910 return emit_op_mem(compiler
, inp_flags
, dst_reg
, dst
, dstw
, TMP_REG1
);
1917 #if defined(__GNUC__)
1918 extern unsigned int __aeabi_uidivmod(unsigned int numerator
, unsigned int denominator
);
1919 extern int __aeabi_idivmod(int numerator
, int denominator
);
1921 #error "Software divmod functions are needed"
1928 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op0(struct sljit_compiler
*compiler
, sljit_s32 op
)
1930 sljit_uw saved_reg_list
[3];
1931 sljit_sw saved_reg_count
;
1934 CHECK(check_sljit_emit_op0(compiler
, op
));
1936 op
= GET_OPCODE(op
);
1938 case SLJIT_BREAKPOINT
:
1939 FAIL_IF(push_inst(compiler
, BKPT
));
1942 FAIL_IF(push_inst(compiler
, NOP
));
1946 return push_inst(compiler
, (op
== SLJIT_LMUL_UW
? UMULL
: SMULL
)
1947 | RN(SLJIT_R1
) | RD(SLJIT_R0
) | RM8(SLJIT_R0
) | RM(SLJIT_R1
));
1948 case SLJIT_DIVMOD_UW
:
1949 case SLJIT_DIVMOD_SW
:
1952 SLJIT_COMPILE_ASSERT((SLJIT_DIVMOD_UW
& 0x2) == 0 && SLJIT_DIV_UW
- 0x2 == SLJIT_DIVMOD_UW
, bad_div_opcode_assignments
);
1953 SLJIT_ASSERT(reg_map
[2] == 1 && reg_map
[3] == 2 && reg_map
[4] == 3);
1955 saved_reg_count
= 0;
1956 if (compiler
->scratches
>= 4)
1957 saved_reg_list
[saved_reg_count
++] = 3;
1958 if (compiler
->scratches
>= 3)
1959 saved_reg_list
[saved_reg_count
++] = 2;
1960 if (op
>= SLJIT_DIV_UW
)
1961 saved_reg_list
[saved_reg_count
++] = 1;
1963 if (saved_reg_count
> 0) {
1964 FAIL_IF(push_inst(compiler
, 0xe52d0000 | (saved_reg_count
>= 3 ? 16 : 8)
1965 | (saved_reg_list
[0] << 12) /* str rX, [sp, #-8/-16]! */));
1966 if (saved_reg_count
>= 2) {
1967 SLJIT_ASSERT(saved_reg_list
[1] < 8);
1968 FAIL_IF(push_inst(compiler
, 0xe58d0004 | (saved_reg_list
[1] << 12) /* str rX, [sp, #4] */));
1970 if (saved_reg_count
>= 3) {
1971 SLJIT_ASSERT(saved_reg_list
[2] < 8);
1972 FAIL_IF(push_inst(compiler
, 0xe58d0008 | (saved_reg_list
[2] << 12) /* str rX, [sp, #8] */));
1976 #if defined(__GNUC__)
1977 FAIL_IF(sljit_emit_ijump(compiler
, SLJIT_FAST_CALL
, SLJIT_IMM
,
1978 ((op
| 0x2) == SLJIT_DIV_UW
? SLJIT_FUNC_ADDR(__aeabi_uidivmod
) : SLJIT_FUNC_ADDR(__aeabi_idivmod
))));
1980 #error "Software divmod functions are needed"
1983 if (saved_reg_count
> 0) {
1984 if (saved_reg_count
>= 3) {
1985 SLJIT_ASSERT(saved_reg_list
[2] < 8);
1986 FAIL_IF(push_inst(compiler
, 0xe59d0008 | (saved_reg_list
[2] << 12) /* ldr rX, [sp, #8] */));
1988 if (saved_reg_count
>= 2) {
1989 SLJIT_ASSERT(saved_reg_list
[1] < 8);
1990 FAIL_IF(push_inst(compiler
, 0xe59d0004 | (saved_reg_list
[1] << 12) /* ldr rX, [sp, #4] */));
1992 return push_inst(compiler
, 0xe49d0000 | (sljit_uw
)(saved_reg_count
>= 3 ? 16 : 8)
1993 | (saved_reg_list
[0] << 12) /* ldr rX, [sp], #8/16 */);
1995 return SLJIT_SUCCESS
;
1997 case SLJIT_SKIP_FRAMES_BEFORE_RETURN
:
1998 return SLJIT_SUCCESS
;
2001 return SLJIT_SUCCESS
;
2004 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op1(struct sljit_compiler
*compiler
, sljit_s32 op
,
2005 sljit_s32 dst
, sljit_sw dstw
,
2006 sljit_s32 src
, sljit_sw srcw
)
2009 CHECK(check_sljit_emit_op1(compiler
, op
, dst
, dstw
, src
, srcw
));
2010 ADJUST_LOCAL_OFFSET(dst
, dstw
);
2011 ADJUST_LOCAL_OFFSET(src
, srcw
);
2013 switch (GET_OPCODE(op
)) {
2019 return emit_op(compiler
, SLJIT_MOV
, ALLOW_ANY_IMM
, dst
, dstw
, TMP_REG1
, 0, src
, srcw
);
2022 return emit_op(compiler
, SLJIT_MOV_U8
, ALLOW_ANY_IMM
| BYTE_SIZE
, dst
, dstw
, TMP_REG1
, 0, src
, (src
& SLJIT_IMM
) ? (sljit_u8
)srcw
: srcw
);
2025 return emit_op(compiler
, SLJIT_MOV_S8
, ALLOW_ANY_IMM
| SIGNED
| BYTE_SIZE
, dst
, dstw
, TMP_REG1
, 0, src
, (src
& SLJIT_IMM
) ? (sljit_s8
)srcw
: srcw
);
2028 return emit_op(compiler
, SLJIT_MOV_U16
, ALLOW_ANY_IMM
| HALF_SIZE
, dst
, dstw
, TMP_REG1
, 0, src
, (src
& SLJIT_IMM
) ? (sljit_u16
)srcw
: srcw
);
2031 return emit_op(compiler
, SLJIT_MOV_S16
, ALLOW_ANY_IMM
| SIGNED
| HALF_SIZE
, dst
, dstw
, TMP_REG1
, 0, src
, (src
& SLJIT_IMM
) ? (sljit_s16
)srcw
: srcw
);
2034 return emit_op(compiler
, op
, ALLOW_ANY_IMM
, dst
, dstw
, TMP_REG1
, 0, src
, srcw
);
2037 return emit_op(compiler
, op
, 0, dst
, dstw
, TMP_REG1
, 0, src
, srcw
);
2040 return SLJIT_SUCCESS
;
2043 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op2(struct sljit_compiler
*compiler
, sljit_s32 op
,
2044 sljit_s32 dst
, sljit_sw dstw
,
2045 sljit_s32 src1
, sljit_sw src1w
,
2046 sljit_s32 src2
, sljit_sw src2w
)
2049 CHECK(check_sljit_emit_op2(compiler
, op
, 0, dst
, dstw
, src1
, src1w
, src2
, src2w
));
2050 ADJUST_LOCAL_OFFSET(dst
, dstw
);
2051 ADJUST_LOCAL_OFFSET(src1
, src1w
);
2052 ADJUST_LOCAL_OFFSET(src2
, src2w
);
2054 switch (GET_OPCODE(op
)) {
2059 return emit_op(compiler
, op
, ALLOW_IMM
| ALLOW_NEG_IMM
, dst
, dstw
, src1
, src1w
, src2
, src2w
);
2063 return emit_op(compiler
, op
, ALLOW_IMM
, dst
, dstw
, src1
, src1w
, src2
, src2w
);
2066 return emit_op(compiler
, op
, 0, dst
, dstw
, src1
, src1w
, src2
, src2w
);
2069 return emit_op(compiler
, op
, ALLOW_ANY_IMM
, dst
, dstw
, src1
, src1w
, src2
, src2w
);
2074 if (src2
& SLJIT_IMM
) {
2075 compiler
->shift_imm
= src2w
& 0x1f;
2076 return emit_op(compiler
, op
, 0, dst
, dstw
, TMP_REG1
, 0, src1
, src1w
);
2079 compiler
->shift_imm
= 0x20;
2080 return emit_op(compiler
, op
, 0, dst
, dstw
, src1
, src1w
, src2
, src2w
);
2084 return SLJIT_SUCCESS
;
2087 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op2u(struct sljit_compiler
*compiler
, sljit_s32 op
,
2088 sljit_s32 src1
, sljit_sw src1w
,
2089 sljit_s32 src2
, sljit_sw src2w
)
2092 CHECK(check_sljit_emit_op2(compiler
, op
, 1, 0, 0, src1
, src1w
, src2
, src2w
));
2094 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2095 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2096 compiler
->skip_checks
= 1;
2098 return sljit_emit_op2(compiler
, op
, TMP_REG2
, 0, src1
, src1w
, src2
, src2w
);
2101 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op_src(struct sljit_compiler
*compiler
, sljit_s32 op
,
2102 sljit_s32 src
, sljit_sw srcw
)
2105 CHECK(check_sljit_emit_op_src(compiler
, op
, src
, srcw
));
2106 ADJUST_LOCAL_OFFSET(src
, srcw
);
2109 case SLJIT_FAST_RETURN
:
2110 SLJIT_ASSERT(reg_map
[TMP_REG2
] == 14);
2112 if (FAST_IS_REG(src
))
2113 FAIL_IF(push_inst(compiler
, MOV
| RD(TMP_REG2
) | RM(src
)));
2115 FAIL_IF(emit_op_mem(compiler
, WORD_SIZE
| LOAD_DATA
, TMP_REG2
, src
, srcw
, TMP_REG1
));
2117 return push_inst(compiler
, BX
| RM(TMP_REG2
));
2118 case SLJIT_SKIP_FRAMES_BEFORE_FAST_RETURN
:
2119 return SLJIT_SUCCESS
;
2120 case SLJIT_PREFETCH_L1
:
2121 case SLJIT_PREFETCH_L2
:
2122 case SLJIT_PREFETCH_L3
:
2123 case SLJIT_PREFETCH_ONCE
:
2124 #if (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
2125 SLJIT_ASSERT(src
& SLJIT_MEM
);
2126 return emit_op_mem(compiler
, PRELOAD
| LOAD_DATA
, TMP_PC
, src
, srcw
, TMP_REG1
);
2127 #else /* !SLJIT_CONFIG_ARM_V7 */
2128 return SLJIT_SUCCESS
;
2129 #endif /* SLJIT_CONFIG_ARM_V7 */
2132 return SLJIT_SUCCESS
;
2135 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_get_register_index(sljit_s32 reg
)
2137 CHECK_REG_INDEX(check_sljit_get_register_index(reg
));
2138 return reg_map
[reg
];
2141 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_get_float_register_index(sljit_s32 reg
)
2143 CHECK_REG_INDEX(check_sljit_get_float_register_index(reg
));
2144 return (freg_map
[reg
] << 1);
2147 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op_custom(struct sljit_compiler
*compiler
,
2148 void *instruction
, sljit_u32 size
)
2150 SLJIT_UNUSED_ARG(size
);
2152 CHECK(check_sljit_emit_op_custom(compiler
, instruction
, size
));
2154 return push_inst(compiler
, *(sljit_uw
*)instruction
);
2157 /* --------------------------------------------------------------------- */
2158 /* Floating point operators */
2159 /* --------------------------------------------------------------------- */
2161 #define FPU_LOAD (1 << 20)
2162 #define EMIT_FPU_DATA_TRANSFER(inst, add, base, freg, offs) \
2163 ((inst) | (sljit_uw)((add) << 23) | RN(base) | VD(freg) | (sljit_uw)(offs))
2165 static sljit_s32
emit_fop_mem(struct sljit_compiler
*compiler
, sljit_s32 flags
, sljit_s32 reg
, sljit_s32 arg
, sljit_sw argw
)
2168 sljit_uw inst
= VSTR_F32
| (flags
& (SLJIT_32
| FPU_LOAD
));
2170 SLJIT_ASSERT(arg
& SLJIT_MEM
);
2173 if (SLJIT_UNLIKELY(arg
& OFFS_REG_MASK
)) {
2174 FAIL_IF(push_inst(compiler
, ADD
| RD(TMP_REG2
) | RN(arg
& REG_MASK
) | RM(OFFS_REG(arg
)) | (((sljit_uw
)argw
& 0x3) << 7)));
2179 /* Fast loads and stores. */
2181 if (!(argw
& ~0x3fc))
2182 return push_inst(compiler
, EMIT_FPU_DATA_TRANSFER(inst
, 1, arg
& REG_MASK
, reg
, argw
>> 2));
2183 if (!(-argw
& ~0x3fc))
2184 return push_inst(compiler
, EMIT_FPU_DATA_TRANSFER(inst
, 0, arg
& REG_MASK
, reg
, (-argw
) >> 2));
2186 imm
= get_imm((sljit_uw
)argw
& ~(sljit_uw
)0x3fc);
2188 FAIL_IF(push_inst(compiler
, ADD
| RD(TMP_REG2
) | RN(arg
& REG_MASK
) | imm
));
2189 return push_inst(compiler
, EMIT_FPU_DATA_TRANSFER(inst
, 1, TMP_REG2
, reg
, (argw
& 0x3fc) >> 2));
2191 imm
= get_imm((sljit_uw
)-argw
& ~(sljit_uw
)0x3fc);
2194 FAIL_IF(push_inst(compiler
, SUB
| RD(TMP_REG2
) | RN(arg
& REG_MASK
) | imm
));
2195 return push_inst(compiler
, EMIT_FPU_DATA_TRANSFER(inst
, 0, TMP_REG2
, reg
, (argw
& 0x3fc) >> 2));
2200 FAIL_IF(load_immediate(compiler
, TMP_REG2
, (sljit_uw
)argw
));
2201 FAIL_IF(push_inst(compiler
, ADD
| RD(TMP_REG2
) | RN(arg
& REG_MASK
) | RM(TMP_REG2
)));
2204 FAIL_IF(load_immediate(compiler
, TMP_REG2
, (sljit_uw
)argw
));
2206 return push_inst(compiler
, EMIT_FPU_DATA_TRANSFER(inst
, 1, TMP_REG2
, reg
, 0));
2209 static SLJIT_INLINE sljit_s32
sljit_emit_fop1_conv_sw_from_f64(struct sljit_compiler
*compiler
, sljit_s32 op
,
2210 sljit_s32 dst
, sljit_sw dstw
,
2211 sljit_s32 src
, sljit_sw srcw
)
2215 if (src
& SLJIT_MEM
) {
2216 FAIL_IF(emit_fop_mem(compiler
, (op
& SLJIT_32
) | FPU_LOAD
, TMP_FREG1
, src
, srcw
));
2220 FAIL_IF(push_inst(compiler
, EMIT_FPU_OPERATION(VCVT_S32_F32
, op
& SLJIT_32
, TMP_FREG1
, src
, 0)));
2222 if (FAST_IS_REG(dst
))
2223 return push_inst(compiler
, VMOV
| (1 << 20) | RD(dst
) | VN(TMP_FREG1
));
2225 /* Store the integer value from a VFP register. */
2226 return emit_fop_mem(compiler
, 0, TMP_FREG1
, dst
, dstw
);
2229 static SLJIT_INLINE sljit_s32
sljit_emit_fop1_conv_f64_from_sw(struct sljit_compiler
*compiler
, sljit_s32 op
,
2230 sljit_s32 dst
, sljit_sw dstw
,
2231 sljit_s32 src
, sljit_sw srcw
)
2233 sljit_s32 dst_r
= FAST_IS_REG(dst
) ? dst
: TMP_FREG1
;
2237 if (FAST_IS_REG(src
))
2238 FAIL_IF(push_inst(compiler
, VMOV
| RD(src
) | VN(TMP_FREG1
)));
2239 else if (src
& SLJIT_MEM
) {
2240 /* Load the integer value into a VFP register. */
2241 FAIL_IF(emit_fop_mem(compiler
, FPU_LOAD
, TMP_FREG1
, src
, srcw
));
2244 FAIL_IF(load_immediate(compiler
, TMP_REG1
, (sljit_uw
)srcw
));
2245 FAIL_IF(push_inst(compiler
, VMOV
| RD(TMP_REG1
) | VN(TMP_FREG1
)));
2248 FAIL_IF(push_inst(compiler
, EMIT_FPU_OPERATION(VCVT_F32_S32
, op
& SLJIT_32
, dst_r
, TMP_FREG1
, 0)));
2250 if (dst
& SLJIT_MEM
)
2251 return emit_fop_mem(compiler
, (op
& SLJIT_32
), TMP_FREG1
, dst
, dstw
);
2252 return SLJIT_SUCCESS
;
2255 static SLJIT_INLINE sljit_s32
sljit_emit_fop1_cmp(struct sljit_compiler
*compiler
, sljit_s32 op
,
2256 sljit_s32 src1
, sljit_sw src1w
,
2257 sljit_s32 src2
, sljit_sw src2w
)
2261 if (src1
& SLJIT_MEM
) {
2262 FAIL_IF(emit_fop_mem(compiler
, (op
& SLJIT_32
) | FPU_LOAD
, TMP_FREG1
, src1
, src1w
));
2266 if (src2
& SLJIT_MEM
) {
2267 FAIL_IF(emit_fop_mem(compiler
, (op
& SLJIT_32
) | FPU_LOAD
, TMP_FREG2
, src2
, src2w
));
2271 FAIL_IF(push_inst(compiler
, EMIT_FPU_OPERATION(VCMP_F32
, op
& SLJIT_32
, src1
, src2
, 0)));
2272 return push_inst(compiler
, VMRS
);
2275 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_fop1(struct sljit_compiler
*compiler
, sljit_s32 op
,
2276 sljit_s32 dst
, sljit_sw dstw
,
2277 sljit_s32 src
, sljit_sw srcw
)
2283 SLJIT_COMPILE_ASSERT((SLJIT_32
== 0x100), float_transfer_bit_error
);
2284 SELECT_FOP1_OPERATION_WITH_CHECKS(compiler
, op
, dst
, dstw
, src
, srcw
);
2286 dst_r
= FAST_IS_REG(dst
) ? dst
: TMP_FREG1
;
2288 if (GET_OPCODE(op
) != SLJIT_CONV_F64_FROM_F32
)
2291 if (src
& SLJIT_MEM
) {
2292 FAIL_IF(emit_fop_mem(compiler
, (op
& SLJIT_32
) | FPU_LOAD
, dst_r
, src
, srcw
));
2296 switch (GET_OPCODE(op
)) {
2299 if (dst_r
!= TMP_FREG1
)
2300 FAIL_IF(push_inst(compiler
, EMIT_FPU_OPERATION(VMOV_F32
, op
& SLJIT_32
, dst_r
, src
, 0)));
2306 FAIL_IF(push_inst(compiler
, EMIT_FPU_OPERATION(VNEG_F32
, op
& SLJIT_32
, dst_r
, src
, 0)));
2309 FAIL_IF(push_inst(compiler
, EMIT_FPU_OPERATION(VABS_F32
, op
& SLJIT_32
, dst_r
, src
, 0)));
2311 case SLJIT_CONV_F64_FROM_F32
:
2312 FAIL_IF(push_inst(compiler
, EMIT_FPU_OPERATION(VCVT_F64_F32
, op
& SLJIT_32
, dst_r
, src
, 0)));
2317 if (dst
& SLJIT_MEM
)
2318 return emit_fop_mem(compiler
, (op
& SLJIT_32
), dst_r
, dst
, dstw
);
2319 return SLJIT_SUCCESS
;
2322 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_fop2(struct sljit_compiler
*compiler
, sljit_s32 op
,
2323 sljit_s32 dst
, sljit_sw dstw
,
2324 sljit_s32 src1
, sljit_sw src1w
,
2325 sljit_s32 src2
, sljit_sw src2w
)
2330 CHECK(check_sljit_emit_fop2(compiler
, op
, dst
, dstw
, src1
, src1w
, src2
, src2w
));
2331 ADJUST_LOCAL_OFFSET(dst
, dstw
);
2332 ADJUST_LOCAL_OFFSET(src1
, src1w
);
2333 ADJUST_LOCAL_OFFSET(src2
, src2w
);
2337 dst_r
= FAST_IS_REG(dst
) ? dst
: TMP_FREG1
;
2339 if (src2
& SLJIT_MEM
) {
2340 FAIL_IF(emit_fop_mem(compiler
, (op
& SLJIT_32
) | FPU_LOAD
, TMP_FREG2
, src2
, src2w
));
2344 if (src1
& SLJIT_MEM
) {
2345 FAIL_IF(emit_fop_mem(compiler
, (op
& SLJIT_32
) | FPU_LOAD
, TMP_FREG1
, src1
, src1w
));
2349 switch (GET_OPCODE(op
)) {
2351 FAIL_IF(push_inst(compiler
, EMIT_FPU_OPERATION(VADD_F32
, op
& SLJIT_32
, dst_r
, src2
, src1
)));
2355 FAIL_IF(push_inst(compiler
, EMIT_FPU_OPERATION(VSUB_F32
, op
& SLJIT_32
, dst_r
, src2
, src1
)));
2359 FAIL_IF(push_inst(compiler
, EMIT_FPU_OPERATION(VMUL_F32
, op
& SLJIT_32
, dst_r
, src2
, src1
)));
2363 FAIL_IF(push_inst(compiler
, EMIT_FPU_OPERATION(VDIV_F32
, op
& SLJIT_32
, dst_r
, src2
, src1
)));
2367 if (dst_r
== TMP_FREG1
)
2368 FAIL_IF(emit_fop_mem(compiler
, (op
& SLJIT_32
), TMP_FREG1
, dst
, dstw
));
2370 return SLJIT_SUCCESS
;
2374 #undef EMIT_FPU_DATA_TRANSFER
2376 /* --------------------------------------------------------------------- */
2377 /* Other instructions */
2378 /* --------------------------------------------------------------------- */
2380 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_fast_enter(struct sljit_compiler
*compiler
, sljit_s32 dst
, sljit_sw dstw
)
2383 CHECK(check_sljit_emit_fast_enter(compiler
, dst
, dstw
));
2384 ADJUST_LOCAL_OFFSET(dst
, dstw
);
2386 SLJIT_ASSERT(reg_map
[TMP_REG2
] == 14);
2388 if (FAST_IS_REG(dst
))
2389 return push_inst(compiler
, MOV
| RD(dst
) | RM(TMP_REG2
));
2392 return emit_op_mem(compiler
, WORD_SIZE
, TMP_REG2
, dst
, dstw
, TMP_REG1
);
2395 /* --------------------------------------------------------------------- */
2396 /* Conditional instructions */
2397 /* --------------------------------------------------------------------- */
2399 static sljit_uw
get_cc(struct sljit_compiler
*compiler
, sljit_s32 type
)
2404 case SLJIT_ORDERED_EQUAL
:
2405 case SLJIT_UNORDERED_OR_EQUAL
: /* Not supported. */
2408 case SLJIT_NOT_EQUAL
:
2409 case SLJIT_F_NOT_EQUAL
:
2410 case SLJIT_UNORDERED_OR_NOT_EQUAL
:
2411 case SLJIT_ORDERED_NOT_EQUAL
: /* Not supported. */
2415 if (compiler
->status_flags_state
& SLJIT_CURRENT_FLAGS_ADD
)
2422 case SLJIT_NOT_CARRY
:
2423 if (compiler
->status_flags_state
& SLJIT_CURRENT_FLAGS_ADD
)
2427 case SLJIT_GREATER_EQUAL
:
2431 case SLJIT_UNORDERED_OR_GREATER
:
2434 case SLJIT_LESS_EQUAL
:
2435 case SLJIT_F_LESS_EQUAL
:
2436 case SLJIT_ORDERED_LESS_EQUAL
:
2439 case SLJIT_SIG_LESS
:
2440 case SLJIT_UNORDERED_OR_LESS
:
2443 case SLJIT_SIG_GREATER_EQUAL
:
2444 case SLJIT_F_GREATER_EQUAL
:
2445 case SLJIT_ORDERED_GREATER_EQUAL
:
2448 case SLJIT_SIG_GREATER
:
2449 case SLJIT_F_GREATER
:
2450 case SLJIT_ORDERED_GREATER
:
2453 case SLJIT_SIG_LESS_EQUAL
:
2454 case SLJIT_UNORDERED_OR_LESS_EQUAL
:
2457 case SLJIT_OVERFLOW
:
2458 if (!(compiler
->status_flags_state
& (SLJIT_CURRENT_FLAGS_ADD
| SLJIT_CURRENT_FLAGS_SUB
)))
2462 case SLJIT_UNORDERED
:
2465 case SLJIT_NOT_OVERFLOW
:
2466 if (!(compiler
->status_flags_state
& (SLJIT_CURRENT_FLAGS_ADD
| SLJIT_CURRENT_FLAGS_SUB
)))
2474 case SLJIT_ORDERED_LESS
:
2477 case SLJIT_UNORDERED_OR_GREATER_EQUAL
:
2481 SLJIT_ASSERT(type
>= SLJIT_JUMP
&& type
<= SLJIT_CALL_CDECL
);
2486 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_label
* sljit_emit_label(struct sljit_compiler
*compiler
)
2488 struct sljit_label
*label
;
2491 CHECK_PTR(check_sljit_emit_label(compiler
));
2493 if (compiler
->last_label
&& compiler
->last_label
->size
== compiler
->size
)
2494 return compiler
->last_label
;
2496 label
= (struct sljit_label
*)ensure_abuf(compiler
, sizeof(struct sljit_label
));
2497 PTR_FAIL_IF(!label
);
2498 set_label(label
, compiler
);
2502 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_jump
* sljit_emit_jump(struct sljit_compiler
*compiler
, sljit_s32 type
)
2504 struct sljit_jump
*jump
;
2507 CHECK_PTR(check_sljit_emit_jump(compiler
, type
));
2509 jump
= (struct sljit_jump
*)ensure_abuf(compiler
, sizeof(struct sljit_jump
));
2511 set_jump(jump
, compiler
, type
& SLJIT_REWRITABLE_JUMP
);
2514 SLJIT_ASSERT(reg_map
[TMP_REG1
] != 14);
2516 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
2517 if (type
>= SLJIT_FAST_CALL
)
2518 PTR_FAIL_IF(prepare_blx(compiler
));
2519 PTR_FAIL_IF(push_inst_with_unique_literal(compiler
, ((EMIT_DATA_TRANSFER(WORD_SIZE
| LOAD_DATA
, 1,
2520 type
<= SLJIT_JUMP
? TMP_PC
: TMP_REG1
, TMP_PC
, 0)) & ~COND_MASK
) | get_cc(compiler
, type
), 0));
2522 if (jump
->flags
& SLJIT_REWRITABLE_JUMP
) {
2523 jump
->addr
= compiler
->size
;
2524 compiler
->patches
++;
2527 if (type
>= SLJIT_FAST_CALL
) {
2528 jump
->flags
|= IS_BL
;
2529 PTR_FAIL_IF(emit_blx(compiler
));
2532 if (!(jump
->flags
& SLJIT_REWRITABLE_JUMP
))
2533 jump
->addr
= compiler
->size
;
2535 if (type
>= SLJIT_FAST_CALL
)
2536 jump
->flags
|= IS_BL
;
2537 PTR_FAIL_IF(emit_imm(compiler
, TMP_REG1
, 0));
2538 PTR_FAIL_IF(push_inst(compiler
, (((type
<= SLJIT_JUMP
? BX
: BLX
) | RM(TMP_REG1
)) & ~COND_MASK
) | get_cc(compiler
, type
)));
2539 jump
->addr
= compiler
->size
;
2546 static sljit_s32
softfloat_call_with_args(struct sljit_compiler
*compiler
, sljit_s32 arg_types
, sljit_s32
*src
, sljit_u32
*extra_space
)
2548 sljit_u32 is_tail_call
= *extra_space
& SLJIT_CALL_RETURN
;
2549 sljit_u32 offset
= 0;
2550 sljit_u32 word_arg_offset
= 0;
2551 sljit_u32 src_offset
= 4 * sizeof(sljit_sw
);
2552 sljit_u32 float_arg_count
= 0;
2553 sljit_s32 types
= 0;
2554 sljit_u8 offsets
[4];
2555 sljit_u8
*offset_ptr
= offsets
;
2557 if (src
&& FAST_IS_REG(*src
))
2558 src_offset
= (sljit_uw
)reg_map
[*src
] * sizeof(sljit_sw
);
2560 arg_types
>>= SLJIT_ARG_SHIFT
;
2563 types
= (types
<< SLJIT_ARG_SHIFT
) | (arg_types
& SLJIT_ARG_MASK
);
2565 switch (arg_types
& SLJIT_ARG_MASK
) {
2566 case SLJIT_ARG_TYPE_F64
:
2568 offset
+= sizeof(sljit_sw
);
2569 *offset_ptr
++ = (sljit_u8
)offset
;
2570 offset
+= sizeof(sljit_f64
);
2573 case SLJIT_ARG_TYPE_F32
:
2574 *offset_ptr
++ = (sljit_u8
)offset
;
2575 offset
+= sizeof(sljit_f32
);
2579 *offset_ptr
++ = (sljit_u8
)offset
;
2580 offset
+= sizeof(sljit_sw
);
2581 word_arg_offset
+= sizeof(sljit_sw
);
2585 arg_types
>>= SLJIT_ARG_SHIFT
;
2588 if (offset
> 4 * sizeof(sljit_sw
) && (!is_tail_call
|| offset
> compiler
->args_size
)) {
2589 /* Keep lr register on the stack. */
2591 offset
+= sizeof(sljit_sw
);
2593 offset
= ((offset
- 4 * sizeof(sljit_sw
)) + 0x7) & ~(sljit_uw
)0x7;
2595 *extra_space
= offset
;
2598 FAIL_IF(emit_stack_frame_release(compiler
, (sljit_s32
)offset
));
2600 FAIL_IF(push_inst(compiler
, SUB
| RD(SLJIT_SP
) | RN(SLJIT_SP
) | SRC2_IMM
| offset
));
2603 FAIL_IF(emit_stack_frame_release(compiler
, -1));
2607 /* Process arguments in reversed direction. */
2609 switch (types
& SLJIT_ARG_MASK
) {
2610 case SLJIT_ARG_TYPE_F64
:
2612 offset
= *(--offset_ptr
);
2614 SLJIT_ASSERT((offset
& 0x7) == 0);
2616 if (offset
< 4 * sizeof(sljit_sw
)) {
2617 if (src_offset
== offset
|| src_offset
== offset
+ sizeof(sljit_sw
)) {
2618 FAIL_IF(push_inst(compiler
, MOV
| RD(TMP_REG1
) | (src_offset
>> 2)));
2621 FAIL_IF(push_inst(compiler
, VMOV2
| 0x100000 | (offset
<< 10) | ((offset
+ sizeof(sljit_sw
)) << 14) | float_arg_count
));
2623 FAIL_IF(push_inst(compiler
, VSTR_F32
| 0x800100 | RN(SLJIT_SP
)
2624 | (float_arg_count
<< 12) | ((offset
- 4 * sizeof(sljit_sw
)) >> 2)));
2626 case SLJIT_ARG_TYPE_F32
:
2628 offset
= *(--offset_ptr
);
2630 if (offset
< 4 * sizeof(sljit_sw
)) {
2631 if (src_offset
== offset
) {
2632 FAIL_IF(push_inst(compiler
, MOV
| RD(TMP_REG1
) | (src_offset
>> 2)));
2635 FAIL_IF(push_inst(compiler
, VMOV
| 0x100000 | (float_arg_count
<< 16) | (offset
<< 10)));
2637 FAIL_IF(push_inst(compiler
, VSTR_F32
| 0x800000 | RN(SLJIT_SP
)
2638 | (float_arg_count
<< 12) | ((offset
- 4 * sizeof(sljit_sw
)) >> 2)));
2641 word_arg_offset
-= sizeof(sljit_sw
);
2642 offset
= *(--offset_ptr
);
2644 SLJIT_ASSERT(offset
>= word_arg_offset
);
2646 if (offset
!= word_arg_offset
) {
2647 if (offset
< 4 * sizeof(sljit_sw
)) {
2648 if (src_offset
== offset
) {
2649 FAIL_IF(push_inst(compiler
, MOV
| RD(TMP_REG1
) | (src_offset
>> 2)));
2652 else if (src_offset
== word_arg_offset
) {
2653 *src
= (sljit_s32
)(SLJIT_R0
+ (offset
>> 2));
2654 src_offset
= offset
;
2656 FAIL_IF(push_inst(compiler
, MOV
| (offset
<< 10) | (word_arg_offset
>> 2)));
2658 FAIL_IF(push_inst(compiler
, data_transfer_insts
[WORD_SIZE
] | 0x800000 | RN(SLJIT_SP
) | (word_arg_offset
<< 10) | (offset
- 4 * sizeof(sljit_sw
))));
2663 types
>>= SLJIT_ARG_SHIFT
;
2666 return SLJIT_SUCCESS
;
2669 static sljit_s32
softfloat_post_call_with_args(struct sljit_compiler
*compiler
, sljit_s32 arg_types
)
2671 if ((arg_types
& SLJIT_ARG_MASK
) == SLJIT_ARG_TYPE_F64
)
2672 FAIL_IF(push_inst(compiler
, VMOV2
| (1 << 16) | (0 << 12) | 0));
2673 if ((arg_types
& SLJIT_ARG_MASK
) == SLJIT_ARG_TYPE_F32
)
2674 FAIL_IF(push_inst(compiler
, VMOV
| (0 << 16) | (0 << 12)));
2676 return SLJIT_SUCCESS
;
2679 #else /* !__SOFTFP__ */
2681 static sljit_s32
hardfloat_call_with_args(struct sljit_compiler
*compiler
, sljit_s32 arg_types
)
2683 sljit_u32 offset
= SLJIT_FR0
;
2684 sljit_u32 new_offset
= SLJIT_FR0
;
2685 sljit_u32 f32_offset
= 0;
2687 /* Remove return value. */
2688 arg_types
>>= SLJIT_ARG_SHIFT
;
2691 switch (arg_types
& SLJIT_ARG_MASK
) {
2692 case SLJIT_ARG_TYPE_F64
:
2693 if (offset
!= new_offset
)
2694 FAIL_IF(push_inst(compiler
, EMIT_FPU_OPERATION(VMOV_F32
,
2695 SLJIT_32
, new_offset
, offset
, 0)));
2700 case SLJIT_ARG_TYPE_F32
:
2701 if (f32_offset
!= 0) {
2702 FAIL_IF(push_inst(compiler
, EMIT_FPU_OPERATION(VMOV_F32
,
2703 0x400000, f32_offset
, offset
, 0)));
2706 if (offset
!= new_offset
)
2707 FAIL_IF(push_inst(compiler
, EMIT_FPU_OPERATION(VMOV_F32
,
2708 0, new_offset
, offset
, 0)));
2709 f32_offset
= new_offset
;
2715 arg_types
>>= SLJIT_ARG_SHIFT
;
2718 return SLJIT_SUCCESS
;
2721 #endif /* __SOFTFP__ */
2723 #undef EMIT_FPU_OPERATION
2725 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_jump
* sljit_emit_call(struct sljit_compiler
*compiler
, sljit_s32 type
,
2726 sljit_s32 arg_types
)
2729 struct sljit_jump
*jump
;
2730 sljit_u32 extra_space
= (sljit_u32
)type
;
2734 CHECK_PTR(check_sljit_emit_call(compiler
, type
, arg_types
));
2737 PTR_FAIL_IF(softfloat_call_with_args(compiler
, arg_types
, NULL
, &extra_space
));
2738 SLJIT_ASSERT((extra_space
& 0x7) == 0);
2740 if ((type
& SLJIT_CALL_RETURN
) && extra_space
== 0)
2741 type
= SLJIT_JUMP
| (type
& SLJIT_REWRITABLE_JUMP
);
2743 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2744 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2745 compiler
->skip_checks
= 1;
2748 jump
= sljit_emit_jump(compiler
, type
);
2749 PTR_FAIL_IF(jump
== NULL
);
2751 if (extra_space
> 0) {
2752 if (type
& SLJIT_CALL_RETURN
)
2753 PTR_FAIL_IF(push_inst(compiler
, EMIT_DATA_TRANSFER(WORD_SIZE
| LOAD_DATA
, 1,
2754 TMP_REG2
, SLJIT_SP
, extra_space
- sizeof(sljit_sw
))));
2756 PTR_FAIL_IF(push_inst(compiler
, ADD
| RD(SLJIT_SP
) | RN(SLJIT_SP
) | SRC2_IMM
| extra_space
));
2758 if (type
& SLJIT_CALL_RETURN
) {
2759 PTR_FAIL_IF(push_inst(compiler
, BX
| RM(TMP_REG2
)));
2764 SLJIT_ASSERT(!(type
& SLJIT_CALL_RETURN
));
2765 PTR_FAIL_IF(softfloat_post_call_with_args(compiler
, arg_types
));
2767 #else /* !__SOFTFP__ */
2768 if (type
& SLJIT_CALL_RETURN
) {
2769 PTR_FAIL_IF(emit_stack_frame_release(compiler
, -1));
2770 type
= SLJIT_JUMP
| (type
& SLJIT_REWRITABLE_JUMP
);
2773 PTR_FAIL_IF(hardfloat_call_with_args(compiler
, arg_types
));
2775 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2776 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2777 compiler
->skip_checks
= 1;
2780 return sljit_emit_jump(compiler
, type
);
2781 #endif /* __SOFTFP__ */
2784 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_ijump(struct sljit_compiler
*compiler
, sljit_s32 type
, sljit_s32 src
, sljit_sw srcw
)
2786 struct sljit_jump
*jump
;
2789 CHECK(check_sljit_emit_ijump(compiler
, type
, src
, srcw
));
2790 ADJUST_LOCAL_OFFSET(src
, srcw
);
2792 SLJIT_ASSERT(reg_map
[TMP_REG1
] != 14);
2794 if (!(src
& SLJIT_IMM
)) {
2795 if (FAST_IS_REG(src
)) {
2796 SLJIT_ASSERT(reg_map
[src
] != 14);
2797 return push_inst(compiler
, (type
<= SLJIT_JUMP
? BX
: BLX
) | RM(src
));
2800 SLJIT_ASSERT(src
& SLJIT_MEM
);
2801 FAIL_IF(emit_op_mem(compiler
, WORD_SIZE
| LOAD_DATA
, TMP_REG1
, src
, srcw
, TMP_REG1
));
2802 return push_inst(compiler
, (type
<= SLJIT_JUMP
? BX
: BLX
) | RM(TMP_REG1
));
2805 /* These jumps are converted to jump/call instructions when possible. */
2806 jump
= (struct sljit_jump
*)ensure_abuf(compiler
, sizeof(struct sljit_jump
));
2808 set_jump(jump
, compiler
, JUMP_ADDR
| ((type
>= SLJIT_FAST_CALL
) ? IS_BL
: 0));
2809 jump
->u
.target
= (sljit_uw
)srcw
;
2811 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
2812 if (type
>= SLJIT_FAST_CALL
)
2813 FAIL_IF(prepare_blx(compiler
));
2814 FAIL_IF(push_inst_with_unique_literal(compiler
, EMIT_DATA_TRANSFER(WORD_SIZE
| LOAD_DATA
, 1, type
<= SLJIT_JUMP
? TMP_PC
: TMP_REG1
, TMP_PC
, 0), 0));
2815 if (type
>= SLJIT_FAST_CALL
)
2816 FAIL_IF(emit_blx(compiler
));
2818 FAIL_IF(emit_imm(compiler
, TMP_REG1
, 0));
2819 FAIL_IF(push_inst(compiler
, (type
<= SLJIT_JUMP
? BX
: BLX
) | RM(TMP_REG1
)));
2821 jump
->addr
= compiler
->size
;
2822 return SLJIT_SUCCESS
;
2825 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_icall(struct sljit_compiler
*compiler
, sljit_s32 type
,
2826 sljit_s32 arg_types
,
2827 sljit_s32 src
, sljit_sw srcw
)
2830 sljit_u32 extra_space
= (sljit_u32
)type
;
2834 CHECK(check_sljit_emit_icall(compiler
, type
, arg_types
, src
, srcw
));
2836 if (src
& SLJIT_MEM
) {
2837 FAIL_IF(emit_op_mem(compiler
, WORD_SIZE
| LOAD_DATA
, TMP_REG1
, src
, srcw
, TMP_REG1
));
2841 if ((type
& SLJIT_CALL_RETURN
) && (src
>= SLJIT_FIRST_SAVED_REG
&& src
<= SLJIT_S0
)) {
2842 FAIL_IF(push_inst(compiler
, MOV
| RD(TMP_REG1
) | RM(src
)));
2847 FAIL_IF(softfloat_call_with_args(compiler
, arg_types
, &src
, &extra_space
));
2848 SLJIT_ASSERT((extra_space
& 0x7) == 0);
2850 if ((type
& SLJIT_CALL_RETURN
) && extra_space
== 0)
2853 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2854 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2855 compiler
->skip_checks
= 1;
2858 FAIL_IF(sljit_emit_ijump(compiler
, type
, src
, srcw
));
2860 if (extra_space
> 0) {
2861 if (type
& SLJIT_CALL_RETURN
)
2862 FAIL_IF(push_inst(compiler
, EMIT_DATA_TRANSFER(WORD_SIZE
| LOAD_DATA
, 1,
2863 TMP_REG2
, SLJIT_SP
, extra_space
- sizeof(sljit_sw
))));
2865 FAIL_IF(push_inst(compiler
, ADD
| RD(SLJIT_SP
) | RN(SLJIT_SP
) | SRC2_IMM
| extra_space
));
2867 if (type
& SLJIT_CALL_RETURN
)
2868 return push_inst(compiler
, BX
| RM(TMP_REG2
));
2871 SLJIT_ASSERT(!(type
& SLJIT_CALL_RETURN
));
2872 return softfloat_post_call_with_args(compiler
, arg_types
);
2873 #else /* !__SOFTFP__ */
2874 if (type
& SLJIT_CALL_RETURN
) {
2875 FAIL_IF(emit_stack_frame_release(compiler
, -1));
2879 FAIL_IF(hardfloat_call_with_args(compiler
, arg_types
));
2881 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2882 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2883 compiler
->skip_checks
= 1;
2886 return sljit_emit_ijump(compiler
, type
, src
, srcw
);
2887 #endif /* __SOFTFP__ */
2890 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op_flags(struct sljit_compiler
*compiler
, sljit_s32 op
,
2891 sljit_s32 dst
, sljit_sw dstw
,
2894 sljit_s32 dst_reg
, flags
= GET_ALL_FLAGS(op
);
2898 CHECK(check_sljit_emit_op_flags(compiler
, op
, dst
, dstw
, type
));
2899 ADJUST_LOCAL_OFFSET(dst
, dstw
);
2901 op
= GET_OPCODE(op
);
2902 cc
= get_cc(compiler
, type
);
2903 dst_reg
= FAST_IS_REG(dst
) ? dst
: TMP_REG1
;
2905 if (op
< SLJIT_ADD
) {
2906 FAIL_IF(push_inst(compiler
, MOV
| RD(dst_reg
) | SRC2_IMM
| 0));
2907 FAIL_IF(push_inst(compiler
, ((MOV
| RD(dst_reg
) | SRC2_IMM
| 1) & ~COND_MASK
) | cc
));
2908 if (dst
& SLJIT_MEM
)
2909 return emit_op_mem(compiler
, WORD_SIZE
, TMP_REG1
, dst
, dstw
, TMP_REG2
);
2910 return SLJIT_SUCCESS
;
2913 ins
= (op
== SLJIT_AND
? AND
: (op
== SLJIT_OR
? ORR
: EOR
));
2915 if (dst
& SLJIT_MEM
)
2916 FAIL_IF(emit_op_mem(compiler
, WORD_SIZE
| LOAD_DATA
, TMP_REG1
, dst
, dstw
, TMP_REG2
));
2918 FAIL_IF(push_inst(compiler
, ((ins
| RD(dst_reg
) | RN(dst_reg
) | SRC2_IMM
| 1) & ~COND_MASK
) | cc
));
2920 if (op
== SLJIT_AND
)
2921 FAIL_IF(push_inst(compiler
, ((ins
| RD(dst_reg
) | RN(dst_reg
) | SRC2_IMM
| 0) & ~COND_MASK
) | (cc
^ 0x10000000)));
2923 if (dst
& SLJIT_MEM
)
2924 FAIL_IF(emit_op_mem(compiler
, WORD_SIZE
, TMP_REG1
, dst
, dstw
, TMP_REG2
));
2926 if (flags
& SLJIT_SET_Z
)
2927 return push_inst(compiler
, MOV
| SET_FLAGS
| RD(TMP_REG2
) | RM(dst_reg
));
2928 return SLJIT_SUCCESS
;
2931 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_cmov(struct sljit_compiler
*compiler
, sljit_s32 type
,
2933 sljit_s32 src
, sljit_sw srcw
)
2938 CHECK(check_sljit_emit_cmov(compiler
, type
, dst_reg
, src
, srcw
));
2940 dst_reg
&= ~SLJIT_32
;
2942 cc
= get_cc(compiler
, type
);
2944 if (SLJIT_UNLIKELY(src
& SLJIT_IMM
)) {
2945 tmp
= get_imm((sljit_uw
)srcw
);
2947 return push_inst(compiler
, ((MOV
| RD(dst_reg
) | tmp
) & ~COND_MASK
) | cc
);
2949 tmp
= get_imm(~(sljit_uw
)srcw
);
2951 return push_inst(compiler
, ((MVN
| RD(dst_reg
) | tmp
) & ~COND_MASK
) | cc
);
2953 #if (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
2954 tmp
= (sljit_uw
)srcw
;
2955 FAIL_IF(push_inst(compiler
, (MOVW
& ~COND_MASK
) | cc
| RD(dst_reg
) | ((tmp
<< 4) & 0xf0000) | (tmp
& 0xfff)));
2957 return SLJIT_SUCCESS
;
2958 return push_inst(compiler
, (MOVT
& ~COND_MASK
) | cc
| RD(dst_reg
) | ((tmp
>> 12) & 0xf0000) | ((tmp
>> 16) & 0xfff));
2960 FAIL_IF(load_immediate(compiler
, TMP_REG1
, (sljit_uw
)srcw
));
2965 return push_inst(compiler
, ((MOV
| RD(dst_reg
) | RM(src
)) & ~COND_MASK
) | cc
);
2968 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_mem(struct sljit_compiler
*compiler
, sljit_s32 type
,
2970 sljit_s32 mem
, sljit_sw memw
)
2973 sljit_uw is_type1_transfer
, inst
;
2976 CHECK(check_sljit_emit_mem(compiler
, type
, reg
, mem
, memw
));
2978 is_type1_transfer
= 1;
2980 switch (type
& 0xff) {
2992 if (!(type
& SLJIT_MEM_STORE
))
2993 is_type1_transfer
= 0;
2994 flags
= BYTE_SIZE
| SIGNED
;
2997 is_type1_transfer
= 0;
3001 is_type1_transfer
= 0;
3002 flags
= HALF_SIZE
| SIGNED
;
3005 SLJIT_UNREACHABLE();
3010 if (!(type
& SLJIT_MEM_STORE
))
3013 SLJIT_ASSERT(is_type1_transfer
== !!IS_TYPE1_TRANSFER(flags
));
3015 if (SLJIT_UNLIKELY(mem
& OFFS_REG_MASK
)) {
3016 if (!is_type1_transfer
&& memw
!= 0)
3017 return SLJIT_ERR_UNSUPPORTED
;
3020 if (is_type1_transfer
) {
3021 if (memw
> 4095 || memw
< -4095)
3022 return SLJIT_ERR_UNSUPPORTED
;
3025 if (memw
> 255 || memw
< -255)
3026 return SLJIT_ERR_UNSUPPORTED
;
3030 if (type
& SLJIT_MEM_SUPP
)
3031 return SLJIT_SUCCESS
;
3033 if (SLJIT_UNLIKELY(mem
& OFFS_REG_MASK
)) {
3036 inst
= EMIT_DATA_TRANSFER(flags
, 1, reg
, mem
& REG_MASK
, RM(OFFS_REG(mem
)) | ((sljit_uw
)memw
<< 7));
3038 if (is_type1_transfer
)
3041 if (type
& SLJIT_MEM_PRE
)
3046 return push_inst(compiler
, inst
);
3049 inst
= EMIT_DATA_TRANSFER(flags
, 0, reg
, mem
& REG_MASK
, 0);
3051 if (type
& SLJIT_MEM_PRE
)
3056 if (is_type1_transfer
) {
3062 return push_inst(compiler
, inst
| (sljit_uw
)memw
);
3070 return push_inst(compiler
, inst
| TYPE2_TRANSFER_IMM((sljit_uw
)memw
));
3073 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_const
* sljit_emit_const(struct sljit_compiler
*compiler
, sljit_s32 dst
, sljit_sw dstw
, sljit_sw init_value
)
3075 struct sljit_const
*const_
;
3079 CHECK_PTR(check_sljit_emit_const(compiler
, dst
, dstw
, init_value
));
3080 ADJUST_LOCAL_OFFSET(dst
, dstw
);
3082 dst_r
= FAST_IS_REG(dst
) ? dst
: TMP_REG2
;
3084 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
3085 PTR_FAIL_IF(push_inst_with_unique_literal(compiler
,
3086 EMIT_DATA_TRANSFER(WORD_SIZE
| LOAD_DATA
, 1, dst_r
, TMP_PC
, 0), (sljit_uw
)init_value
));
3087 compiler
->patches
++;
3089 PTR_FAIL_IF(emit_imm(compiler
, dst_r
, init_value
));
3092 const_
= (struct sljit_const
*)ensure_abuf(compiler
, sizeof(struct sljit_const
));
3093 PTR_FAIL_IF(!const_
);
3094 set_const(const_
, compiler
);
3096 if (dst
& SLJIT_MEM
)
3097 PTR_FAIL_IF(emit_op_mem(compiler
, WORD_SIZE
, TMP_REG2
, dst
, dstw
, TMP_REG1
));
3101 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_put_label
* sljit_emit_put_label(struct sljit_compiler
*compiler
, sljit_s32 dst
, sljit_sw dstw
)
3103 struct sljit_put_label
*put_label
;
3107 CHECK_PTR(check_sljit_emit_put_label(compiler
, dst
, dstw
));
3108 ADJUST_LOCAL_OFFSET(dst
, dstw
);
3110 dst_r
= FAST_IS_REG(dst
) ? dst
: TMP_REG2
;
3112 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
3113 PTR_FAIL_IF(push_inst_with_unique_literal(compiler
, EMIT_DATA_TRANSFER(WORD_SIZE
| LOAD_DATA
, 1, dst_r
, TMP_PC
, 0), 0));
3114 compiler
->patches
++;
3116 PTR_FAIL_IF(emit_imm(compiler
, dst_r
, 0));
3119 put_label
= (struct sljit_put_label
*)ensure_abuf(compiler
, sizeof(struct sljit_put_label
));
3120 PTR_FAIL_IF(!put_label
);
3121 set_put_label(put_label
, compiler
, 0);
3123 if (dst
& SLJIT_MEM
)
3124 PTR_FAIL_IF(emit_op_mem(compiler
, WORD_SIZE
, TMP_REG2
, dst
, dstw
, TMP_REG1
));
3128 SLJIT_API_FUNC_ATTRIBUTE
void sljit_set_jump_addr(sljit_uw addr
, sljit_uw new_target
, sljit_sw executable_offset
)
3130 inline_set_jump_addr(addr
, executable_offset
, new_target
, 1);
3133 SLJIT_API_FUNC_ATTRIBUTE
void sljit_set_const(sljit_uw addr
, sljit_sw new_constant
, sljit_sw executable_offset
)
3135 inline_set_const(addr
, executable_offset
, (sljit_uw
)new_constant
, 1);