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, 6, 7
71 #define RM(rm) (reg_map[rm])
72 #define RD(rd) (reg_map[rd] << 12)
73 #define RN(rn) (reg_map[rn] << 16)
75 /* --------------------------------------------------------------------- */
76 /* Instrucion forms */
77 /* --------------------------------------------------------------------- */
79 /* The instruction includes the AL condition.
80 INST_NAME - CONDITIONAL remove this flag. */
81 #define COND_MASK 0xf0000000
82 #define CONDITIONAL 0xe0000000
83 #define PUSH_POOL 0xff000000
85 #define ADC 0xe0a00000
86 #define ADD 0xe0800000
87 #define AND 0xe0000000
89 #define BIC 0xe1c00000
91 #define BLX 0xe12fff30
93 #define CLZ 0xe16f0f10
94 #define CMN 0xe1600000
95 #define CMP 0xe1400000
96 #define BKPT 0xe1200070
97 #define EOR 0xe0200000
98 #define MOV 0xe1a00000
99 #define MUL 0xe0000090
100 #define MVN 0xe1e00000
101 #define NOP 0xe1a00000
102 #define ORR 0xe1800000
103 #define PUSH 0xe92d0000
104 #define POP 0xe8bd0000
105 #define RSB 0xe0600000
106 #define RSC 0xe0e00000
107 #define SBC 0xe0c00000
108 #define SMULL 0xe0c00090
109 #define SUB 0xe0400000
110 #define UMULL 0xe0800090
111 #define VABS_F32 0xeeb00ac0
112 #define VADD_F32 0xee300a00
113 #define VCMP_F32 0xeeb40a40
114 #define VCVT_F32_S32 0xeeb80ac0
115 #define VCVT_F64_F32 0xeeb70ac0
116 #define VCVT_S32_F32 0xeebd0ac0
117 #define VDIV_F32 0xee800a00
118 #define VMOV_F32 0xeeb00a40
119 #define VMOV 0xee000a10
120 #define VMOV2 0xec400a10
121 #define VMRS 0xeef1fa10
122 #define VMUL_F32 0xee200a00
123 #define VNEG_F32 0xeeb10a40
124 #define VSTR_F32 0xed000a00
125 #define VSUB_F32 0xee300a40
127 #if (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
128 /* Arm v7 specific instructions. */
129 #define MOVW 0xe3000000
130 #define MOVT 0xe3400000
131 #define SXTB 0xe6af0070
132 #define SXTH 0xe6bf0070
133 #define UXTB 0xe6ef0070
134 #define UXTH 0xe6ff0070
137 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
139 static sljit_s32
push_cpool(struct sljit_compiler
*compiler
)
141 /* Pushing the constant pool into the instruction stream. */
147 /* The label could point the address after the constant pool. */
148 if (compiler
->last_label
&& compiler
->last_label
->size
== compiler
->size
)
149 compiler
->last_label
->size
+= compiler
->cpool_fill
+ (CONST_POOL_ALIGNMENT
- 1) + 1;
151 SLJIT_ASSERT(compiler
->cpool_fill
> 0 && compiler
->cpool_fill
<= CPOOL_SIZE
);
152 inst
= (sljit_uw
*)ensure_buf(compiler
, sizeof(sljit_uw
));
155 *inst
= 0xff000000 | compiler
->cpool_fill
;
157 for (i
= 0; i
< CONST_POOL_ALIGNMENT
- 1; i
++) {
158 inst
= (sljit_uw
*)ensure_buf(compiler
, sizeof(sljit_uw
));
164 cpool_ptr
= compiler
->cpool
;
165 cpool_end
= cpool_ptr
+ compiler
->cpool_fill
;
166 while (cpool_ptr
< cpool_end
) {
167 inst
= (sljit_uw
*)ensure_buf(compiler
, sizeof(sljit_uw
));
170 *inst
= *cpool_ptr
++;
172 compiler
->cpool_diff
= CONST_POOL_EMPTY
;
173 compiler
->cpool_fill
= 0;
174 return SLJIT_SUCCESS
;
177 static sljit_s32
push_inst(struct sljit_compiler
*compiler
, sljit_uw inst
)
181 if (SLJIT_UNLIKELY(compiler
->cpool_diff
!= CONST_POOL_EMPTY
&& compiler
->size
- compiler
->cpool_diff
>= MAX_DIFFERENCE(4092)))
182 FAIL_IF(push_cpool(compiler
));
184 ptr
= (sljit_uw
*)ensure_buf(compiler
, sizeof(sljit_uw
));
188 return SLJIT_SUCCESS
;
191 static sljit_s32
push_inst_with_literal(struct sljit_compiler
*compiler
, sljit_uw inst
, sljit_uw literal
)
194 sljit_uw cpool_index
= CPOOL_SIZE
;
197 sljit_u8
* cpool_unique_ptr
;
199 if (SLJIT_UNLIKELY(compiler
->cpool_diff
!= CONST_POOL_EMPTY
&& compiler
->size
- compiler
->cpool_diff
>= MAX_DIFFERENCE(4092)))
200 FAIL_IF(push_cpool(compiler
));
201 else if (compiler
->cpool_fill
> 0) {
202 cpool_ptr
= compiler
->cpool
;
203 cpool_end
= cpool_ptr
+ compiler
->cpool_fill
;
204 cpool_unique_ptr
= compiler
->cpool_unique
;
206 if ((*cpool_ptr
== literal
) && !(*cpool_unique_ptr
)) {
207 cpool_index
= cpool_ptr
- compiler
->cpool
;
212 } while (cpool_ptr
< cpool_end
);
215 if (cpool_index
== CPOOL_SIZE
) {
216 /* Must allocate a new entry in the literal pool. */
217 if (compiler
->cpool_fill
< CPOOL_SIZE
) {
218 cpool_index
= compiler
->cpool_fill
;
219 compiler
->cpool_fill
++;
222 FAIL_IF(push_cpool(compiler
));
224 compiler
->cpool_fill
= 1;
228 SLJIT_ASSERT((inst
& 0xfff) == 0);
229 ptr
= (sljit_uw
*)ensure_buf(compiler
, sizeof(sljit_uw
));
232 *ptr
= inst
| cpool_index
;
234 compiler
->cpool
[cpool_index
] = literal
;
235 compiler
->cpool_unique
[cpool_index
] = 0;
236 if (compiler
->cpool_diff
== CONST_POOL_EMPTY
)
237 compiler
->cpool_diff
= compiler
->size
;
238 return SLJIT_SUCCESS
;
241 static sljit_s32
push_inst_with_unique_literal(struct sljit_compiler
*compiler
, sljit_uw inst
, sljit_uw literal
)
244 if (SLJIT_UNLIKELY((compiler
->cpool_diff
!= CONST_POOL_EMPTY
&& compiler
->size
- compiler
->cpool_diff
>= MAX_DIFFERENCE(4092)) || compiler
->cpool_fill
>= CPOOL_SIZE
))
245 FAIL_IF(push_cpool(compiler
));
247 SLJIT_ASSERT(compiler
->cpool_fill
< CPOOL_SIZE
&& (inst
& 0xfff) == 0);
248 ptr
= (sljit_uw
*)ensure_buf(compiler
, sizeof(sljit_uw
));
251 *ptr
= inst
| compiler
->cpool_fill
;
253 compiler
->cpool
[compiler
->cpool_fill
] = literal
;
254 compiler
->cpool_unique
[compiler
->cpool_fill
] = 1;
255 compiler
->cpool_fill
++;
256 if (compiler
->cpool_diff
== CONST_POOL_EMPTY
)
257 compiler
->cpool_diff
= compiler
->size
;
258 return SLJIT_SUCCESS
;
261 static SLJIT_INLINE sljit_s32
prepare_blx(struct sljit_compiler
*compiler
)
263 /* Place for at least two instruction (doesn't matter whether the first has a literal). */
264 if (SLJIT_UNLIKELY(compiler
->cpool_diff
!= CONST_POOL_EMPTY
&& compiler
->size
- compiler
->cpool_diff
>= MAX_DIFFERENCE(4088)))
265 return push_cpool(compiler
);
266 return SLJIT_SUCCESS
;
269 static SLJIT_INLINE sljit_s32
emit_blx(struct sljit_compiler
*compiler
)
271 /* Must follow tightly the previous instruction (to be able to convert it to bl instruction). */
272 SLJIT_ASSERT(compiler
->cpool_diff
== CONST_POOL_EMPTY
|| compiler
->size
- compiler
->cpool_diff
< MAX_DIFFERENCE(4092));
273 SLJIT_ASSERT(reg_map
[TMP_REG1
] != 14);
275 return push_inst(compiler
, BLX
| RM(TMP_REG1
));
278 static sljit_uw
patch_pc_relative_loads(sljit_uw
*last_pc_patch
, sljit_uw
*code_ptr
, sljit_uw
* const_pool
, sljit_uw cpool_size
)
282 sljit_uw counter
= 0;
283 sljit_uw
* clear_const_pool
= const_pool
;
284 sljit_uw
* clear_const_pool_end
= const_pool
+ cpool_size
;
286 SLJIT_ASSERT(const_pool
- code_ptr
<= CONST_POOL_ALIGNMENT
);
287 /* Set unused flag for all literals in the constant pool.
288 I.e.: unused literals can belong to branches, which can be encoded as B or BL.
289 We can "compress" the constant pool by discarding these literals. */
290 while (clear_const_pool
< clear_const_pool_end
)
291 *clear_const_pool
++ = (sljit_uw
)(-1);
293 while (last_pc_patch
< code_ptr
) {
294 /* Data transfer instruction with Rn == r15. */
295 if ((*last_pc_patch
& 0x0c0f0000) == 0x040f0000) {
296 diff
= const_pool
- last_pc_patch
;
297 ind
= (*last_pc_patch
) & 0xfff;
299 /* Must be a load instruction with immediate offset. */
300 SLJIT_ASSERT(ind
< cpool_size
&& !(*last_pc_patch
& (1 << 25)) && (*last_pc_patch
& (1 << 20)));
301 if ((sljit_s32
)const_pool
[ind
] < 0) {
302 const_pool
[ind
] = counter
;
307 ind
= const_pool
[ind
];
309 SLJIT_ASSERT(diff
>= 1);
310 if (diff
>= 2 || ind
> 0) {
311 diff
= (diff
+ ind
- 2) << 2;
312 SLJIT_ASSERT(diff
<= 0xfff);
313 *last_pc_patch
= (*last_pc_patch
& ~0xfff) | diff
;
316 *last_pc_patch
= (*last_pc_patch
& ~(0xfff | (1 << 23))) | 0x004;
323 /* In some rare ocasions we may need future patches. The probability is close to 0 in practice. */
324 struct future_patch
{
325 struct future_patch
* next
;
330 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
)
333 struct future_patch
*curr_patch
, *prev_patch
;
335 SLJIT_UNUSED_ARG(compiler
);
337 /* Using the values generated by patch_pc_relative_loads. */
339 value
= (sljit_s32
)cpool_start_address
[cpool_current_index
];
341 curr_patch
= *first_patch
;
345 value
= (sljit_s32
)cpool_start_address
[cpool_current_index
];
348 if ((sljit_uw
)curr_patch
->index
== cpool_current_index
) {
349 value
= curr_patch
->value
;
351 prev_patch
->next
= curr_patch
->next
;
353 *first_patch
= curr_patch
->next
;
354 SLJIT_FREE(curr_patch
, compiler
->allocator_data
);
357 prev_patch
= curr_patch
;
358 curr_patch
= curr_patch
->next
;
363 if ((sljit_uw
)value
> cpool_current_index
) {
364 curr_patch
= (struct future_patch
*)SLJIT_MALLOC(sizeof(struct future_patch
), compiler
->allocator_data
);
366 while (*first_patch
) {
367 curr_patch
= *first_patch
;
368 *first_patch
= (*first_patch
)->next
;
369 SLJIT_FREE(curr_patch
, compiler
->allocator_data
);
371 return SLJIT_ERR_ALLOC_FAILED
;
373 curr_patch
->next
= *first_patch
;
374 curr_patch
->index
= value
;
375 curr_patch
->value
= cpool_start_address
[value
];
376 *first_patch
= curr_patch
;
378 cpool_start_address
[value
] = *buf_ptr
;
380 return SLJIT_SUCCESS
;
385 static sljit_s32
push_inst(struct sljit_compiler
*compiler
, sljit_uw inst
)
389 ptr
= (sljit_uw
*)ensure_buf(compiler
, sizeof(sljit_uw
));
393 return SLJIT_SUCCESS
;
396 static SLJIT_INLINE sljit_s32
emit_imm(struct sljit_compiler
*compiler
, sljit_s32 reg
, sljit_sw imm
)
398 FAIL_IF(push_inst(compiler
, MOVW
| RD(reg
) | ((imm
<< 4) & 0xf0000) | (imm
& 0xfff)));
399 return push_inst(compiler
, MOVT
| RD(reg
) | ((imm
>> 12) & 0xf0000) | ((imm
>> 16) & 0xfff));
404 static SLJIT_INLINE sljit_s32
detect_jump_type(struct sljit_jump
*jump
, sljit_uw
*code_ptr
, sljit_uw
*code
, sljit_sw executable_offset
)
408 if (jump
->flags
& SLJIT_REWRITABLE_JUMP
)
411 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
412 if (jump
->flags
& IS_BL
)
415 if (jump
->flags
& JUMP_ADDR
)
416 diff
= ((sljit_sw
)jump
->u
.target
- (sljit_sw
)(code_ptr
+ 2) - executable_offset
);
418 SLJIT_ASSERT(jump
->flags
& JUMP_LABEL
);
419 diff
= ((sljit_sw
)(code
+ jump
->u
.label
->size
) - (sljit_sw
)(code_ptr
+ 2));
422 /* Branch to Thumb code has not been optimized yet. */
426 if (jump
->flags
& IS_BL
) {
427 if (diff
<= 0x01ffffff && diff
>= -0x02000000) {
428 *code_ptr
= (BL
- CONDITIONAL
) | (*(code_ptr
+ 1) & COND_MASK
);
429 jump
->flags
|= PATCH_B
;
434 if (diff
<= 0x01ffffff && diff
>= -0x02000000) {
435 *code_ptr
= (B
- CONDITIONAL
) | (*code_ptr
& COND_MASK
);
436 jump
->flags
|= PATCH_B
;
440 if (jump
->flags
& JUMP_ADDR
)
441 diff
= ((sljit_sw
)jump
->u
.target
- (sljit_sw
)code_ptr
- executable_offset
);
443 SLJIT_ASSERT(jump
->flags
& JUMP_LABEL
);
444 diff
= ((sljit_sw
)(code
+ jump
->u
.label
->size
) - (sljit_sw
)code_ptr
);
447 /* Branch to Thumb code has not been optimized yet. */
451 if (diff
<= 0x01ffffff && diff
>= -0x02000000) {
453 *code_ptr
= ((jump
->flags
& IS_BL
) ? (BL
- CONDITIONAL
) : (B
- CONDITIONAL
)) | (code_ptr
[2] & COND_MASK
);
454 jump
->flags
|= PATCH_B
;
461 static SLJIT_INLINE
void inline_set_jump_addr(sljit_uw jump_ptr
, sljit_sw executable_offset
, sljit_uw new_addr
, sljit_s32 flush_cache
)
463 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
464 sljit_uw
*ptr
= (sljit_uw
*)jump_ptr
;
465 sljit_uw
*inst
= (sljit_uw
*)ptr
[0];
466 sljit_uw mov_pc
= ptr
[1];
467 sljit_s32 bl
= (mov_pc
& 0x0000f000) != RD(TMP_PC
);
468 sljit_sw diff
= (sljit_sw
)(((sljit_sw
)new_addr
- (sljit_sw
)(inst
+ 2) - executable_offset
) >> 2);
470 SLJIT_UNUSED_ARG(executable_offset
);
472 if (diff
<= 0x7fffff && diff
>= -0x800000) {
473 /* Turn to branch. */
476 SLJIT_UPDATE_WX_FLAGS(inst
, inst
+ 1, 0);
478 inst
[0] = (mov_pc
& COND_MASK
) | (B
- CONDITIONAL
) | (diff
& 0xffffff);
480 SLJIT_UPDATE_WX_FLAGS(inst
, inst
+ 1, 1);
481 inst
= (sljit_uw
*)SLJIT_ADD_EXEC_OFFSET(inst
, executable_offset
);
482 SLJIT_CACHE_FLUSH(inst
, inst
+ 1);
486 SLJIT_UPDATE_WX_FLAGS(inst
, inst
+ 2, 0);
488 inst
[0] = (mov_pc
& COND_MASK
) | (BL
- CONDITIONAL
) | (diff
& 0xffffff);
491 SLJIT_UPDATE_WX_FLAGS(inst
, inst
+ 2, 1);
492 inst
= (sljit_uw
*)SLJIT_ADD_EXEC_OFFSET(inst
, executable_offset
);
493 SLJIT_CACHE_FLUSH(inst
, inst
+ 2);
497 /* Get the position of the constant. */
498 if (mov_pc
& (1 << 23))
499 ptr
= inst
+ ((mov_pc
& 0xfff) >> 2) + 2;
503 if (*inst
!= mov_pc
) {
505 SLJIT_UPDATE_WX_FLAGS(inst
, inst
+ (!bl
? 1 : 2), 0);
510 SLJIT_UPDATE_WX_FLAGS(inst
, inst
+ 1, 1);
511 inst
= (sljit_uw
*)SLJIT_ADD_EXEC_OFFSET(inst
, executable_offset
);
512 SLJIT_CACHE_FLUSH(inst
, inst
+ 1);
515 inst
[1] = BLX
| RM(TMP_REG1
);
517 SLJIT_UPDATE_WX_FLAGS(inst
, inst
+ 2, 1);
518 inst
= (sljit_uw
*)SLJIT_ADD_EXEC_OFFSET(inst
, executable_offset
);
519 SLJIT_CACHE_FLUSH(inst
, inst
+ 2);
525 SLJIT_UPDATE_WX_FLAGS(ptr
, ptr
+ 1, 0);
531 SLJIT_UPDATE_WX_FLAGS(ptr
, ptr
+ 1, 1);
535 sljit_uw
*inst
= (sljit_uw
*)jump_ptr
;
537 SLJIT_UNUSED_ARG(executable_offset
);
539 SLJIT_ASSERT((inst
[0] & 0xfff00000) == MOVW
&& (inst
[1] & 0xfff00000) == MOVT
);
542 SLJIT_UPDATE_WX_FLAGS(inst
, inst
+ 2, 0);
545 inst
[0] = MOVW
| (inst
[0] & 0xf000) | ((new_addr
<< 4) & 0xf0000) | (new_addr
& 0xfff);
546 inst
[1] = MOVT
| (inst
[1] & 0xf000) | ((new_addr
>> 12) & 0xf0000) | ((new_addr
>> 16) & 0xfff);
549 SLJIT_UPDATE_WX_FLAGS(inst
, inst
+ 2, 1);
550 inst
= (sljit_uw
*)SLJIT_ADD_EXEC_OFFSET(inst
, executable_offset
);
551 SLJIT_CACHE_FLUSH(inst
, inst
+ 2);
556 static sljit_uw
get_imm(sljit_uw imm
);
558 static SLJIT_INLINE
void inline_set_const(sljit_uw addr
, sljit_sw executable_offset
, sljit_sw new_constant
, sljit_s32 flush_cache
)
560 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
561 sljit_uw
*ptr
= (sljit_uw
*)addr
;
562 sljit_uw
*inst
= (sljit_uw
*)ptr
[0];
563 sljit_uw ldr_literal
= ptr
[1];
566 SLJIT_UNUSED_ARG(executable_offset
);
568 src2
= get_imm(new_constant
);
571 SLJIT_UPDATE_WX_FLAGS(inst
, inst
+ 1, 0);
574 *inst
= 0xe3a00000 | (ldr_literal
& 0xf000) | src2
;
577 SLJIT_UPDATE_WX_FLAGS(inst
, inst
+ 1, 1);
578 inst
= (sljit_uw
*)SLJIT_ADD_EXEC_OFFSET(inst
, executable_offset
);
579 SLJIT_CACHE_FLUSH(inst
, inst
+ 1);
584 src2
= get_imm(~new_constant
);
587 SLJIT_UPDATE_WX_FLAGS(inst
, inst
+ 1, 0);
590 *inst
= 0xe3e00000 | (ldr_literal
& 0xf000) | src2
;
593 SLJIT_UPDATE_WX_FLAGS(inst
, inst
+ 1, 1);
594 inst
= (sljit_uw
*)SLJIT_ADD_EXEC_OFFSET(inst
, executable_offset
);
595 SLJIT_CACHE_FLUSH(inst
, inst
+ 1);
600 if (ldr_literal
& (1 << 23))
601 ptr
= inst
+ ((ldr_literal
& 0xfff) >> 2) + 2;
605 if (*inst
!= ldr_literal
) {
607 SLJIT_UPDATE_WX_FLAGS(inst
, inst
+ 1, 0);
613 SLJIT_UPDATE_WX_FLAGS(inst
, inst
+ 1, 1);
614 inst
= (sljit_uw
*)SLJIT_ADD_EXEC_OFFSET(inst
, executable_offset
);
615 SLJIT_CACHE_FLUSH(inst
, inst
+ 1);
620 SLJIT_UPDATE_WX_FLAGS(ptr
, ptr
+ 1, 0);
626 SLJIT_UPDATE_WX_FLAGS(ptr
, ptr
+ 1, 1);
629 sljit_uw
*inst
= (sljit_uw
*)addr
;
631 SLJIT_UNUSED_ARG(executable_offset
);
633 SLJIT_ASSERT((inst
[0] & 0xfff00000) == MOVW
&& (inst
[1] & 0xfff00000) == MOVT
);
636 SLJIT_UPDATE_WX_FLAGS(inst
, inst
+ 2, 0);
639 inst
[0] = MOVW
| (inst
[0] & 0xf000) | ((new_constant
<< 4) & 0xf0000) | (new_constant
& 0xfff);
640 inst
[1] = MOVT
| (inst
[1] & 0xf000) | ((new_constant
>> 12) & 0xf0000) | ((new_constant
>> 16) & 0xfff);
643 SLJIT_UPDATE_WX_FLAGS(inst
, inst
+ 2, 1);
644 inst
= (sljit_uw
*)SLJIT_ADD_EXEC_OFFSET(inst
, executable_offset
);
645 SLJIT_CACHE_FLUSH(inst
, inst
+ 2);
650 SLJIT_API_FUNC_ATTRIBUTE
void* sljit_generate_code(struct sljit_compiler
*compiler
)
652 struct sljit_memory_fragment
*buf
;
660 sljit_sw executable_offset
;
662 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
664 sljit_uw cpool_skip_alignment
;
665 sljit_uw cpool_current_index
;
666 sljit_uw
*cpool_start_address
;
667 sljit_uw
*last_pc_patch
;
668 struct future_patch
*first_patch
;
671 struct sljit_label
*label
;
672 struct sljit_jump
*jump
;
673 struct sljit_const
*const_
;
674 struct sljit_put_label
*put_label
;
677 CHECK_PTR(check_sljit_generate_code(compiler
));
678 reverse_buf(compiler
);
680 /* Second code generation pass. */
681 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
682 size
= compiler
->size
+ (compiler
->patches
<< 1);
683 if (compiler
->cpool_fill
> 0)
684 size
+= compiler
->cpool_fill
+ CONST_POOL_ALIGNMENT
- 1;
686 size
= compiler
->size
;
688 code
= (sljit_uw
*)SLJIT_MALLOC_EXEC(size
* sizeof(sljit_uw
), compiler
->exec_allocator_data
);
689 PTR_FAIL_WITH_EXEC_IF(code
);
692 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
694 cpool_skip_alignment
= 0;
695 cpool_current_index
= 0;
696 cpool_start_address
= NULL
;
698 last_pc_patch
= code
;
704 executable_offset
= SLJIT_EXEC_OFFSET(code
);
706 label
= compiler
->labels
;
707 jump
= compiler
->jumps
;
708 const_
= compiler
->consts
;
709 put_label
= compiler
->put_labels
;
711 if (label
&& label
->size
== 0) {
712 label
->addr
= (sljit_uw
)SLJIT_ADD_EXEC_OFFSET(code
, executable_offset
);
717 buf_ptr
= (sljit_uw
*)buf
->memory
;
718 buf_end
= buf_ptr
+ (buf
->used_size
>> 2);
721 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
722 if (cpool_size
> 0) {
723 if (cpool_skip_alignment
> 0) {
725 cpool_skip_alignment
--;
728 if (SLJIT_UNLIKELY(resolve_const_pool_index(compiler
, &first_patch
, cpool_current_index
, cpool_start_address
, buf_ptr
))) {
729 SLJIT_FREE_EXEC(code
, compiler
->exec_allocator_data
);
730 compiler
->error
= SLJIT_ERR_ALLOC_FAILED
;
734 if (++cpool_current_index
>= cpool_size
) {
735 SLJIT_ASSERT(!first_patch
);
737 if (label
&& label
->size
== word_count
) {
738 /* Points after the current instruction. */
739 label
->addr
= (sljit_uw
)SLJIT_ADD_EXEC_OFFSET(code_ptr
, executable_offset
);
740 label
->size
= code_ptr
- code
;
743 next_addr
= compute_next_addr(label
, jump
, const_
, put_label
);
748 else if ((*buf_ptr
& 0xff000000) != PUSH_POOL
) {
750 *code_ptr
= *buf_ptr
++;
751 if (next_addr
== word_count
) {
752 SLJIT_ASSERT(!label
|| label
->size
>= word_count
);
753 SLJIT_ASSERT(!jump
|| jump
->addr
>= word_count
);
754 SLJIT_ASSERT(!const_
|| const_
->addr
>= word_count
);
755 SLJIT_ASSERT(!put_label
|| put_label
->addr
>= word_count
);
757 /* These structures are ordered by their address. */
758 if (jump
&& jump
->addr
== word_count
) {
759 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
760 if (detect_jump_type(jump
, code_ptr
, code
, executable_offset
))
762 jump
->addr
= (sljit_uw
)code_ptr
;
764 jump
->addr
= (sljit_uw
)(code_ptr
- 2);
765 if (detect_jump_type(jump
, code_ptr
, code
, executable_offset
))
770 if (label
&& label
->size
== word_count
) {
771 /* code_ptr can be affected above. */
772 label
->addr
= (sljit_uw
)SLJIT_ADD_EXEC_OFFSET(code_ptr
+ 1, executable_offset
);
773 label
->size
= (code_ptr
+ 1) - code
;
776 if (const_
&& const_
->addr
== word_count
) {
777 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
778 const_
->addr
= (sljit_uw
)code_ptr
;
780 const_
->addr
= (sljit_uw
)(code_ptr
- 1);
782 const_
= const_
->next
;
784 if (put_label
&& put_label
->addr
== word_count
) {
785 SLJIT_ASSERT(put_label
->label
);
786 put_label
->addr
= (sljit_uw
)code_ptr
;
787 put_label
= put_label
->next
;
789 next_addr
= compute_next_addr(label
, jump
, const_
, put_label
);
792 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
795 /* Fortunately, no need to shift. */
796 cpool_size
= *buf_ptr
++ & ~PUSH_POOL
;
797 SLJIT_ASSERT(cpool_size
> 0);
798 cpool_start_address
= ALIGN_INSTRUCTION(code_ptr
+ 1);
799 cpool_current_index
= patch_pc_relative_loads(last_pc_patch
, code_ptr
, cpool_start_address
, cpool_size
);
800 if (cpool_current_index
> 0) {
801 /* Unconditional branch. */
802 *code_ptr
= B
| (((cpool_start_address
- code_ptr
) + cpool_current_index
- 2) & ~PUSH_POOL
);
803 code_ptr
= cpool_start_address
+ cpool_current_index
;
805 cpool_skip_alignment
= CONST_POOL_ALIGNMENT
- 1;
806 cpool_current_index
= 0;
807 last_pc_patch
= code_ptr
;
810 } while (buf_ptr
< buf_end
);
814 SLJIT_ASSERT(!label
);
816 SLJIT_ASSERT(!const_
);
817 SLJIT_ASSERT(!put_label
);
819 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
820 SLJIT_ASSERT(cpool_size
== 0);
821 if (compiler
->cpool_fill
> 0) {
822 cpool_start_address
= ALIGN_INSTRUCTION(code_ptr
);
823 cpool_current_index
= patch_pc_relative_loads(last_pc_patch
, code_ptr
, cpool_start_address
, compiler
->cpool_fill
);
824 if (cpool_current_index
> 0)
825 code_ptr
= cpool_start_address
+ cpool_current_index
;
827 buf_ptr
= compiler
->cpool
;
828 buf_end
= buf_ptr
+ compiler
->cpool_fill
;
829 cpool_current_index
= 0;
830 while (buf_ptr
< buf_end
) {
831 if (SLJIT_UNLIKELY(resolve_const_pool_index(compiler
, &first_patch
, cpool_current_index
, cpool_start_address
, buf_ptr
))) {
832 SLJIT_FREE_EXEC(code
, compiler
->exec_allocator_data
);
833 compiler
->error
= SLJIT_ERR_ALLOC_FAILED
;
837 cpool_current_index
++;
839 SLJIT_ASSERT(!first_patch
);
843 jump
= compiler
->jumps
;
845 buf_ptr
= (sljit_uw
*)jump
->addr
;
847 if (jump
->flags
& PATCH_B
) {
848 addr
= (sljit_sw
)SLJIT_ADD_EXEC_OFFSET(buf_ptr
+ 2, executable_offset
);
849 if (!(jump
->flags
& JUMP_ADDR
)) {
850 SLJIT_ASSERT(jump
->flags
& JUMP_LABEL
);
851 SLJIT_ASSERT(((sljit_sw
)jump
->u
.label
->addr
- addr
) <= 0x01ffffff && ((sljit_sw
)jump
->u
.label
->addr
- addr
) >= -0x02000000);
852 *buf_ptr
|= (((sljit_sw
)jump
->u
.label
->addr
- addr
) >> 2) & 0x00ffffff;
855 SLJIT_ASSERT(((sljit_sw
)jump
->u
.target
- addr
) <= 0x01ffffff && ((sljit_sw
)jump
->u
.target
- addr
) >= -0x02000000);
856 *buf_ptr
|= (((sljit_sw
)jump
->u
.target
- addr
) >> 2) & 0x00ffffff;
859 else if (jump
->flags
& SLJIT_REWRITABLE_JUMP
) {
860 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
861 jump
->addr
= (sljit_uw
)code_ptr
;
862 code_ptr
[0] = (sljit_uw
)buf_ptr
;
863 code_ptr
[1] = *buf_ptr
;
864 inline_set_jump_addr((sljit_uw
)code_ptr
, executable_offset
, (jump
->flags
& JUMP_LABEL
) ? jump
->u
.label
->addr
: jump
->u
.target
, 0);
867 inline_set_jump_addr((sljit_uw
)buf_ptr
, executable_offset
, (jump
->flags
& JUMP_LABEL
) ? jump
->u
.label
->addr
: jump
->u
.target
, 0);
871 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
872 if (jump
->flags
& IS_BL
)
874 if (*buf_ptr
& (1 << 23))
875 buf_ptr
+= ((*buf_ptr
& 0xfff) >> 2) + 2;
878 *buf_ptr
= (jump
->flags
& JUMP_LABEL
) ? jump
->u
.label
->addr
: jump
->u
.target
;
880 inline_set_jump_addr((sljit_uw
)buf_ptr
, executable_offset
, (jump
->flags
& JUMP_LABEL
) ? jump
->u
.label
->addr
: jump
->u
.target
, 0);
886 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
887 const_
= compiler
->consts
;
889 buf_ptr
= (sljit_uw
*)const_
->addr
;
890 const_
->addr
= (sljit_uw
)code_ptr
;
892 code_ptr
[0] = (sljit_uw
)buf_ptr
;
893 code_ptr
[1] = *buf_ptr
;
894 if (*buf_ptr
& (1 << 23))
895 buf_ptr
+= ((*buf_ptr
& 0xfff) >> 2) + 2;
898 /* Set the value again (can be a simple constant). */
899 inline_set_const((sljit_uw
)code_ptr
, executable_offset
, *buf_ptr
, 0);
902 const_
= const_
->next
;
906 put_label
= compiler
->put_labels
;
908 addr
= put_label
->label
->addr
;
909 buf_ptr
= (sljit_uw
*)put_label
->addr
;
911 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
912 SLJIT_ASSERT((buf_ptr
[0] & 0xffff0000) == 0xe59f0000);
913 buf_ptr
[((buf_ptr
[0] & 0xfff) >> 2) + 2] = addr
;
915 SLJIT_ASSERT((buf_ptr
[-1] & 0xfff00000) == MOVW
&& (buf_ptr
[0] & 0xfff00000) == MOVT
);
916 buf_ptr
[-1] |= ((addr
<< 4) & 0xf0000) | (addr
& 0xfff);
917 buf_ptr
[0] |= ((addr
>> 12) & 0xf0000) | ((addr
>> 16) & 0xfff);
919 put_label
= put_label
->next
;
922 SLJIT_ASSERT(code_ptr
- code
<= (sljit_s32
)size
);
924 compiler
->error
= SLJIT_ERR_COMPILED
;
925 compiler
->executable_offset
= executable_offset
;
926 compiler
->executable_size
= (code_ptr
- code
) * sizeof(sljit_uw
);
928 code
= (sljit_uw
*)SLJIT_ADD_EXEC_OFFSET(code
, executable_offset
);
929 code_ptr
= (sljit_uw
*)SLJIT_ADD_EXEC_OFFSET(code_ptr
, executable_offset
);
931 SLJIT_CACHE_FLUSH(code
, code_ptr
);
932 SLJIT_UPDATE_WX_FLAGS(code
, code_ptr
, 1);
936 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_has_cpu_feature(sljit_s32 feature_type
)
938 switch (feature_type
) {
940 #ifdef SLJIT_IS_FPU_AVAILABLE
941 return SLJIT_IS_FPU_AVAILABLE
;
943 /* Available by default. */
949 #if (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
950 case SLJIT_HAS_PREFETCH
:
959 /* --------------------------------------------------------------------- */
961 /* --------------------------------------------------------------------- */
963 /* Creates an index in data_transfer_insts array. */
964 #define WORD_SIZE 0x00
965 #define BYTE_SIZE 0x01
966 #define HALF_SIZE 0x02
969 #define LOAD_DATA 0x08
971 /* Flag bits for emit_op. */
972 #define ALLOW_IMM 0x10
973 #define ALLOW_INV_IMM 0x20
974 #define ALLOW_ANY_IMM (ALLOW_IMM | ALLOW_INV_IMM)
976 /* s/l - store/load (1 bit)
977 u/s - signed/unsigned (1 bit)
978 w/b/h/N - word/byte/half/NOT allowed (2 bit)
979 Storing signed and unsigned values are the same operations. */
981 static const sljit_uw data_transfer_insts
[16] = {
982 /* s u w */ 0xe5000000 /* str */,
983 /* s u b */ 0xe5400000 /* strb */,
984 /* s u h */ 0xe10000b0 /* strh */,
985 /* s u N */ 0x00000000 /* not allowed */,
986 /* s s w */ 0xe5000000 /* str */,
987 /* s s b */ 0xe5400000 /* strb */,
988 /* s s h */ 0xe10000b0 /* strh */,
989 /* s s N */ 0x00000000 /* not allowed */,
991 /* l u w */ 0xe5100000 /* ldr */,
992 /* l u b */ 0xe5500000 /* ldrb */,
993 /* l u h */ 0xe11000b0 /* ldrh */,
994 /* l u p */ 0xf5500000 /* preload */,
995 /* l s w */ 0xe5100000 /* ldr */,
996 /* l s b */ 0xe11000d0 /* ldrsb */,
997 /* l s h */ 0xe11000f0 /* ldrsh */,
998 /* l s N */ 0x00000000 /* not allowed */,
1001 #define EMIT_DATA_TRANSFER(type, add, target_reg, base_reg, arg) \
1002 (data_transfer_insts[(type) & 0xf] | ((add) << 23) | RD(target_reg) | RN(base_reg) | (arg))
1004 /* Normal ldr/str instruction.
1005 Type2: ldrsb, ldrh, ldrsh */
1006 #define IS_TYPE1_TRANSFER(type) \
1007 (data_transfer_insts[(type) & 0xf] & 0x04000000)
1008 #define TYPE2_TRANSFER_IMM(imm) \
1009 (((imm) & 0xf) | (((imm) & 0xf0) << 4) | (1 << 22))
1011 static sljit_s32
emit_op(struct sljit_compiler
*compiler
, sljit_s32 op
, sljit_s32 inp_flags
,
1012 sljit_s32 dst
, sljit_sw dstw
,
1013 sljit_s32 src1
, sljit_sw src1w
,
1014 sljit_s32 src2
, sljit_sw src2w
);
1016 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_enter(struct sljit_compiler
*compiler
,
1017 sljit_s32 options
, sljit_s32 arg_types
, sljit_s32 scratches
, sljit_s32 saveds
,
1018 sljit_s32 fscratches
, sljit_s32 fsaveds
, sljit_s32 local_size
)
1020 sljit_s32 args
, size
, i
, tmp
;
1024 CHECK(check_sljit_emit_enter(compiler
, options
, arg_types
, scratches
, saveds
, fscratches
, fsaveds
, local_size
));
1025 set_emit_enter(compiler
, options
, arg_types
, scratches
, saveds
, fscratches
, fsaveds
, local_size
);
1027 /* Push saved registers, temporary registers
1028 stmdb sp!, {..., lr} */
1029 push
= PUSH
| (1 << 14);
1031 tmp
= saveds
< SLJIT_NUMBER_OF_SAVED_REGISTERS
? (SLJIT_S0
+ 1 - saveds
) : SLJIT_FIRST_SAVED_REG
;
1032 for (i
= SLJIT_S0
; i
>= tmp
; i
--)
1033 push
|= 1 << reg_map
[i
];
1035 for (i
= scratches
; i
>= SLJIT_FIRST_SAVED_REG
; i
--)
1036 push
|= 1 << reg_map
[i
];
1038 FAIL_IF(push_inst(compiler
, push
));
1040 /* Stack must be aligned to 8 bytes: */
1041 size
= GET_SAVED_REGISTERS_SIZE(scratches
, saveds
, 1);
1042 local_size
= ((size
+ local_size
+ 7) & ~7) - size
;
1043 compiler
->local_size
= local_size
;
1045 FAIL_IF(emit_op(compiler
, SLJIT_SUB
, ALLOW_IMM
, SLJIT_SP
, 0, SLJIT_SP
, 0, SLJIT_IMM
, local_size
));
1047 args
= get_arg_count(arg_types
);
1050 FAIL_IF(push_inst(compiler
, MOV
| RD(SLJIT_S0
) | RM(SLJIT_R0
)));
1052 FAIL_IF(push_inst(compiler
, MOV
| RD(SLJIT_S1
) | RM(SLJIT_R1
)));
1054 FAIL_IF(push_inst(compiler
, MOV
| RD(SLJIT_S2
) | RM(SLJIT_R2
)));
1056 return SLJIT_SUCCESS
;
1059 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_set_context(struct sljit_compiler
*compiler
,
1060 sljit_s32 options
, sljit_s32 arg_types
, sljit_s32 scratches
, sljit_s32 saveds
,
1061 sljit_s32 fscratches
, sljit_s32 fsaveds
, sljit_s32 local_size
)
1066 CHECK(check_sljit_set_context(compiler
, options
, arg_types
, scratches
, saveds
, fscratches
, fsaveds
, local_size
));
1067 set_set_context(compiler
, options
, arg_types
, scratches
, saveds
, fscratches
, fsaveds
, local_size
);
1069 size
= GET_SAVED_REGISTERS_SIZE(scratches
, saveds
, 1);
1070 compiler
->local_size
= ((size
+ local_size
+ 7) & ~7) - size
;
1071 return SLJIT_SUCCESS
;
1074 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_return(struct sljit_compiler
*compiler
, sljit_s32 op
, sljit_s32 src
, sljit_sw srcw
)
1080 CHECK(check_sljit_emit_return(compiler
, op
, src
, srcw
));
1082 FAIL_IF(emit_mov_before_return(compiler
, op
, src
, srcw
));
1084 if (compiler
->local_size
> 0)
1085 FAIL_IF(emit_op(compiler
, SLJIT_ADD
, ALLOW_IMM
, SLJIT_SP
, 0, SLJIT_SP
, 0, SLJIT_IMM
, compiler
->local_size
));
1087 /* Push saved registers, temporary registers
1088 ldmia sp!, {..., pc} */
1089 pop
= POP
| (1 << 15);
1091 tmp
= compiler
->saveds
< SLJIT_NUMBER_OF_SAVED_REGISTERS
? (SLJIT_S0
+ 1 - compiler
->saveds
) : SLJIT_FIRST_SAVED_REG
;
1092 for (i
= SLJIT_S0
; i
>= tmp
; i
--)
1093 pop
|= 1 << reg_map
[i
];
1095 for (i
= compiler
->scratches
; i
>= SLJIT_FIRST_SAVED_REG
; i
--)
1096 pop
|= 1 << reg_map
[i
];
1098 return push_inst(compiler
, pop
);
1101 /* --------------------------------------------------------------------- */
1103 /* --------------------------------------------------------------------- */
1106 /* Arguments are swapped. */
1107 #define ARGS_SWAPPED 0x01
1108 /* Inverted immediate. */
1109 #define INV_IMM 0x02
1110 /* Source and destination is register. */
1111 #define MOVE_REG_CONV 0x04
1112 /* Unused return value. */
1113 #define UNUSED_RETURN 0x08
1114 /* SET_FLAGS must be (1 << 20) as it is also the value of S bit (can be used for optimization). */
1115 #define SET_FLAGS (1 << 20)
1118 src2: reg or imm (if allowed)
1119 SRC2_IMM must be (1 << 25) as it is also the value of I bit (can be used for optimization). */
1120 #define SRC2_IMM (1 << 25)
1122 #define EMIT_SHIFT_INS_AND_RETURN(opcode) \
1123 SLJIT_ASSERT(!(flags & INV_IMM) && !(src2 & SRC2_IMM)); \
1124 if (compiler->shift_imm != 0x20) { \
1125 SLJIT_ASSERT(src1 == TMP_REG1); \
1126 SLJIT_ASSERT(!(flags & ARGS_SWAPPED)); \
1128 if (compiler->shift_imm != 0) \
1129 return push_inst(compiler, MOV | (flags & SET_FLAGS) | \
1130 RD(dst) | (compiler->shift_imm << 7) | (opcode << 5) | RM(src2)); \
1131 return push_inst(compiler, MOV | (flags & SET_FLAGS) | RD(dst) | RM(src2)); \
1133 return push_inst(compiler, MOV | (flags & SET_FLAGS) | RD(dst) | \
1134 (reg_map[(flags & ARGS_SWAPPED) ? src1 : src2] << 8) | (opcode << 5) | 0x10 | RM((flags & ARGS_SWAPPED) ? src2 : src1));
1136 static SLJIT_INLINE sljit_s32
emit_single_op(struct sljit_compiler
*compiler
, sljit_s32 op
, sljit_s32 flags
,
1137 sljit_s32 dst
, sljit_s32 src1
, sljit_s32 src2
)
1139 switch (GET_OPCODE(op
)) {
1141 SLJIT_ASSERT(src1
== TMP_REG1
&& !(flags
& ARGS_SWAPPED
));
1143 if (src2
& SRC2_IMM
) {
1144 return push_inst(compiler
, ((flags
& INV_IMM
) ? MVN
: MOV
) | RD(dst
) | src2
);
1146 return push_inst(compiler
, MOV
| RD(dst
) | RM(src2
));
1148 return SLJIT_SUCCESS
;
1152 SLJIT_ASSERT(src1
== TMP_REG1
&& !(flags
& ARGS_SWAPPED
));
1153 if (flags
& MOVE_REG_CONV
) {
1154 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
1155 if (op
== SLJIT_MOV_U8
)
1156 return push_inst(compiler
, AND
| RD(dst
) | RN(src2
) | SRC2_IMM
| 0xff);
1157 FAIL_IF(push_inst(compiler
, MOV
| RD(dst
) | (24 << 7) | RM(src2
)));
1158 return push_inst(compiler
, MOV
| RD(dst
) | (24 << 7) | (op
== SLJIT_MOV_U8
? 0x20 : 0x40) | RM(dst
));
1160 return push_inst(compiler
, (op
== SLJIT_MOV_U8
? UXTB
: SXTB
) | RD(dst
) | RM(src2
));
1163 else if (dst
!= src2
) {
1164 SLJIT_ASSERT(src2
& SRC2_IMM
);
1165 return push_inst(compiler
, ((flags
& INV_IMM
) ? MVN
: MOV
) | RD(dst
) | src2
);
1167 return SLJIT_SUCCESS
;
1171 SLJIT_ASSERT(src1
== TMP_REG1
&& !(flags
& ARGS_SWAPPED
));
1172 if (flags
& MOVE_REG_CONV
) {
1173 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
1174 FAIL_IF(push_inst(compiler
, MOV
| RD(dst
) | (16 << 7) | RM(src2
)));
1175 return push_inst(compiler
, MOV
| RD(dst
) | (16 << 7) | (op
== SLJIT_MOV_U16
? 0x20 : 0x40) | RM(dst
));
1177 return push_inst(compiler
, (op
== SLJIT_MOV_U16
? UXTH
: SXTH
) | RD(dst
) | RM(src2
));
1180 else if (dst
!= src2
) {
1181 SLJIT_ASSERT(src2
& SRC2_IMM
);
1182 return push_inst(compiler
, ((flags
& INV_IMM
) ? MVN
: MOV
) | RD(dst
) | src2
);
1184 return SLJIT_SUCCESS
;
1187 if (src2
& SRC2_IMM
) {
1188 return push_inst(compiler
, ((flags
& INV_IMM
) ? MOV
: MVN
) | (flags
& SET_FLAGS
) | RD(dst
) | src2
);
1190 return push_inst(compiler
, MVN
| (flags
& SET_FLAGS
) | RD(dst
) | RM(src2
));
1193 SLJIT_ASSERT(!(flags
& INV_IMM
));
1194 SLJIT_ASSERT(!(src2
& SRC2_IMM
));
1195 FAIL_IF(push_inst(compiler
, CLZ
| RD(dst
) | RM(src2
)));
1196 return SLJIT_SUCCESS
;
1199 SLJIT_ASSERT(!(flags
& INV_IMM
));
1200 compiler
->status_flags_state
= SLJIT_CURRENT_FLAGS_ADD_SUB
;
1202 if ((flags
& (UNUSED_RETURN
| SET_FLAGS
)) == (UNUSED_RETURN
| SET_FLAGS
) && !(flags
& ARGS_SWAPPED
))
1203 return push_inst(compiler
, CMN
| SET_FLAGS
| RN(src1
) | ((src2
& SRC2_IMM
) ? src2
: RM(src2
)));
1204 return push_inst(compiler
, ADD
| (flags
& SET_FLAGS
) | RD(dst
) | RN(src1
) | ((src2
& SRC2_IMM
) ? src2
: RM(src2
)));
1207 SLJIT_ASSERT(!(flags
& INV_IMM
));
1208 return push_inst(compiler
, ADC
| (flags
& SET_FLAGS
) | RD(dst
) | RN(src1
) | ((src2
& SRC2_IMM
) ? src2
: RM(src2
)));
1211 SLJIT_ASSERT(!(flags
& INV_IMM
));
1212 compiler
->status_flags_state
= SLJIT_CURRENT_FLAGS_ADD_SUB
;
1214 if ((flags
& (UNUSED_RETURN
| SET_FLAGS
)) == (UNUSED_RETURN
| SET_FLAGS
) && !(flags
& ARGS_SWAPPED
))
1215 return push_inst(compiler
, CMP
| SET_FLAGS
| RN(src1
) | ((src2
& SRC2_IMM
) ? src2
: RM(src2
)));
1216 return push_inst(compiler
, (!(flags
& ARGS_SWAPPED
) ? SUB
: RSB
) | (flags
& SET_FLAGS
)
1217 | RD(dst
) | RN(src1
) | ((src2
& SRC2_IMM
) ? src2
: RM(src2
)));
1220 SLJIT_ASSERT(!(flags
& INV_IMM
));
1221 return push_inst(compiler
, (!(flags
& ARGS_SWAPPED
) ? SBC
: RSC
) | (flags
& SET_FLAGS
)
1222 | RD(dst
) | RN(src1
) | ((src2
& SRC2_IMM
) ? src2
: RM(src2
)));
1225 SLJIT_ASSERT(!(flags
& INV_IMM
));
1226 SLJIT_ASSERT(!(src2
& SRC2_IMM
));
1227 compiler
->status_flags_state
= 0;
1230 return push_inst(compiler
, MUL
| (reg_map
[dst
] << 16) | (reg_map
[src2
] << 8) | reg_map
[src1
]);
1232 FAIL_IF(push_inst(compiler
, SMULL
| (reg_map
[TMP_REG1
] << 16) | (reg_map
[dst
] << 12) | (reg_map
[src2
] << 8) | reg_map
[src1
]));
1234 /* cmp TMP_REG1, dst asr #31. */
1235 return push_inst(compiler
, CMP
| SET_FLAGS
| RN(TMP_REG1
) | RM(dst
) | 0xfc0);
1238 return push_inst(compiler
, (!(flags
& INV_IMM
) ? AND
: BIC
) | (flags
& SET_FLAGS
)
1239 | RD(dst
) | RN(src1
) | ((src2
& SRC2_IMM
) ? src2
: RM(src2
)));
1242 SLJIT_ASSERT(!(flags
& INV_IMM
));
1243 return push_inst(compiler
, ORR
| (flags
& SET_FLAGS
) | RD(dst
) | RN(src1
) | ((src2
& SRC2_IMM
) ? src2
: RM(src2
)));
1246 SLJIT_ASSERT(!(flags
& INV_IMM
));
1247 return push_inst(compiler
, EOR
| (flags
& SET_FLAGS
) | RD(dst
) | RN(src1
) | ((src2
& SRC2_IMM
) ? src2
: RM(src2
)));
1250 EMIT_SHIFT_INS_AND_RETURN(0);
1253 EMIT_SHIFT_INS_AND_RETURN(1);
1256 EMIT_SHIFT_INS_AND_RETURN(2);
1259 SLJIT_UNREACHABLE();
1260 return SLJIT_SUCCESS
;
1263 #undef EMIT_SHIFT_INS_AND_RETURN
1265 /* Tests whether the immediate can be stored in the 12 bit imm field.
1266 Returns with 0 if not possible. */
1267 static sljit_uw
get_imm(sljit_uw imm
)
1272 return SRC2_IMM
| imm
;
1274 if (!(imm
& 0xff000000)) {
1279 imm
= (imm
<< 24) | (imm
>> 8);
1283 if (!(imm
& 0xff000000)) {
1288 if (!(imm
& 0xf0000000)) {
1293 if (!(imm
& 0xc0000000)) {
1298 if (!(imm
& 0x00ffffff))
1299 return SRC2_IMM
| (imm
>> 24) | (rol
<< 8);
1304 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
1305 static sljit_s32
generate_int(struct sljit_compiler
*compiler
, sljit_s32 reg
, sljit_uw imm
, sljit_s32 positive
)
1312 /* Step1: Search a zero byte (8 continous zero bit). */
1316 if (!(imm
& mask
)) {
1317 /* Rol imm by rol. */
1318 imm
= (imm
<< rol
) | (imm
>> (32 - rol
));
1319 /* Calculate arm rol. */
1320 rol
= 4 + (rol
>> 1);
1327 imm
= (imm
<< 8) | (imm
>> 24);
1331 if (!(imm
& mask
)) {
1332 /* Rol imm by rol. */
1333 imm
= (imm
<< rol
) | (imm
>> (32 - rol
));
1334 /* Calculate arm rol. */
1335 rol
= (rol
>> 1) - 8;
1347 /* The low 8 bit must be zero. */
1348 SLJIT_ASSERT(!(imm
& 0xff));
1350 if (!(imm
& 0xff000000)) {
1351 imm1
= SRC2_IMM
| ((imm
>> 16) & 0xff) | (((rol
+ 4) & 0xf) << 8);
1352 imm2
= SRC2_IMM
| ((imm
>> 8) & 0xff) | (((rol
+ 8) & 0xf) << 8);
1354 else if (imm
& 0xc0000000) {
1355 imm1
= SRC2_IMM
| ((imm
>> 24) & 0xff) | ((rol
& 0xf) << 8);
1359 if (!(imm
& 0xff000000)) {
1364 if (!(imm
& 0xf0000000)) {
1369 if (!(imm
& 0xc0000000)) {
1374 if (!(imm
& 0x00ffffff))
1375 imm2
= SRC2_IMM
| (imm
>> 24) | ((rol
& 0xf) << 8);
1380 if (!(imm
& 0xf0000000)) {
1385 if (!(imm
& 0xc0000000)) {
1390 imm1
= SRC2_IMM
| ((imm
>> 24) & 0xff) | ((rol
& 0xf) << 8);
1394 if (!(imm
& 0xf0000000)) {
1399 if (!(imm
& 0xc0000000)) {
1404 if (!(imm
& 0x00ffffff))
1405 imm2
= SRC2_IMM
| (imm
>> 24) | ((rol
& 0xf) << 8);
1410 FAIL_IF(push_inst(compiler
, (positive
? MOV
: MVN
) | RD(reg
) | imm1
));
1411 FAIL_IF(push_inst(compiler
, (positive
? ORR
: BIC
) | RD(reg
) | RN(reg
) | imm2
));
1416 static sljit_s32
load_immediate(struct sljit_compiler
*compiler
, sljit_s32 reg
, sljit_uw imm
)
1420 #if (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
1421 if (!(imm
& ~0xffff))
1422 return push_inst(compiler
, MOVW
| RD(reg
) | ((imm
<< 4) & 0xf0000) | (imm
& 0xfff));
1425 /* Create imm by 1 inst. */
1428 return push_inst(compiler
, MOV
| RD(reg
) | tmp
);
1430 tmp
= get_imm(~imm
);
1432 return push_inst(compiler
, MVN
| RD(reg
) | tmp
);
1434 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
1435 /* Create imm by 2 inst. */
1436 FAIL_IF(generate_int(compiler
, reg
, imm
, 1));
1437 FAIL_IF(generate_int(compiler
, reg
, ~imm
, 0));
1440 return push_inst_with_literal(compiler
, EMIT_DATA_TRANSFER(WORD_SIZE
| LOAD_DATA
, 1, reg
, TMP_PC
, 0), imm
);
1442 FAIL_IF(push_inst(compiler
, MOVW
| RD(reg
) | ((imm
<< 4) & 0xf0000) | (imm
& 0xfff)));
1444 return SLJIT_SUCCESS
;
1445 return push_inst(compiler
, MOVT
| RD(reg
) | ((imm
>> 12) & 0xf0000) | ((imm
>> 16) & 0xfff));
1449 static SLJIT_INLINE sljit_s32
emit_op_mem(struct sljit_compiler
*compiler
, sljit_s32 flags
, sljit_s32 reg
,
1450 sljit_s32 arg
, sljit_sw argw
, sljit_s32 tmp_reg
)
1452 sljit_uw imm
, offset_reg
;
1453 sljit_uw is_type1_transfer
= IS_TYPE1_TRANSFER(flags
);
1455 SLJIT_ASSERT (arg
& SLJIT_MEM
);
1456 SLJIT_ASSERT((arg
& REG_MASK
) != tmp_reg
);
1458 if ((arg
& REG_MASK
) == SLJIT_UNUSED
) {
1459 if (is_type1_transfer
) {
1460 FAIL_IF(load_immediate(compiler
, tmp_reg
, argw
& ~0xfff));
1464 FAIL_IF(load_immediate(compiler
, tmp_reg
, argw
& ~0xff));
1468 return push_inst(compiler
, EMIT_DATA_TRANSFER(flags
, 1, reg
, tmp_reg
,
1469 is_type1_transfer
? argw
: TYPE2_TRANSFER_IMM(argw
)));
1472 if (arg
& OFFS_REG_MASK
) {
1473 offset_reg
= OFFS_REG(arg
);
1477 if (argw
!= 0 && !is_type1_transfer
) {
1478 FAIL_IF(push_inst(compiler
, ADD
| RD(tmp_reg
) | RN(arg
) | RM(offset_reg
) | (argw
<< 7)));
1479 return push_inst(compiler
, EMIT_DATA_TRANSFER(flags
, 1, reg
, tmp_reg
, TYPE2_TRANSFER_IMM(0)));
1482 /* Bit 25: RM is offset. */
1483 return push_inst(compiler
, EMIT_DATA_TRANSFER(flags
, 1, reg
, arg
,
1484 RM(offset_reg
) | (is_type1_transfer
? (1 << 25) : 0) | (argw
<< 7)));
1489 if (is_type1_transfer
) {
1491 imm
= get_imm(argw
& ~0xfff);
1493 FAIL_IF(push_inst(compiler
, ADD
| RD(tmp_reg
) | RN(arg
) | imm
));
1494 argw
= argw
& 0xfff;
1498 else if (argw
< -0xfff) {
1499 imm
= get_imm(-argw
& ~0xfff);
1501 FAIL_IF(push_inst(compiler
, SUB
| RD(tmp_reg
) | RN(arg
) | imm
));
1502 argw
= -(-argw
& 0xfff);
1507 if (argw
>= 0 && argw
<= 0xfff)
1508 return push_inst(compiler
, EMIT_DATA_TRANSFER(flags
, 1, reg
, arg
, argw
));
1510 if (argw
< 0 && argw
>= -0xfff)
1511 return push_inst(compiler
, EMIT_DATA_TRANSFER(flags
, 0, reg
, arg
, -argw
));
1515 imm
= get_imm(argw
& ~0xff);
1517 FAIL_IF(push_inst(compiler
, ADD
| RD(tmp_reg
) | RN(arg
) | imm
));
1522 else if (argw
< -0xff) {
1523 imm
= get_imm(-argw
& ~0xff);
1525 FAIL_IF(push_inst(compiler
, SUB
| RD(tmp_reg
) | RN(arg
) | imm
));
1526 argw
= -(-argw
& 0xff);
1531 if (argw
>= 0 && argw
<= 0xff)
1532 return push_inst(compiler
, EMIT_DATA_TRANSFER(flags
, 1, reg
, arg
, TYPE2_TRANSFER_IMM(argw
)));
1534 if (argw
< 0 && argw
>= -0xff) {
1536 return push_inst(compiler
, EMIT_DATA_TRANSFER(flags
, 0, reg
, arg
, TYPE2_TRANSFER_IMM(argw
)));
1540 FAIL_IF(load_immediate(compiler
, tmp_reg
, argw
));
1541 return push_inst(compiler
, EMIT_DATA_TRANSFER(flags
, 1, reg
, arg
,
1542 RM(tmp_reg
) | (is_type1_transfer
? (1 << 25) : 0)));
1545 static sljit_s32
emit_op(struct sljit_compiler
*compiler
, sljit_s32 op
, sljit_s32 inp_flags
,
1546 sljit_s32 dst
, sljit_sw dstw
,
1547 sljit_s32 src1
, sljit_sw src1w
,
1548 sljit_s32 src2
, sljit_sw src2w
)
1550 /* src1 is reg or TMP_REG1
1551 src2 is reg, TMP_REG2, or imm
1552 result goes to TMP_REG2, so put result can use TMP_REG1. */
1554 /* We prefers register and simple consts. */
1558 sljit_s32 flags
= HAS_FLAGS(op
) ? SET_FLAGS
: 0;
1560 /* Destination check. */
1561 if (SLJIT_UNLIKELY(dst
== SLJIT_UNUSED
))
1562 flags
|= UNUSED_RETURN
;
1564 SLJIT_ASSERT(!(inp_flags
& ALLOW_INV_IMM
) || (inp_flags
& ALLOW_IMM
));
1569 if (!(inp_flags
& ALLOW_IMM
))
1572 if (src2
& SLJIT_IMM
) {
1573 src2_reg
= get_imm(src2w
);
1576 if (inp_flags
& ALLOW_INV_IMM
) {
1577 src2_reg
= get_imm(~src2w
);
1583 if (GET_OPCODE(op
) == SLJIT_ADD
) {
1584 src2_reg
= get_imm(-src2w
);
1586 op
= SLJIT_SUB
| GET_ALL_FLAGS(op
);
1590 if (GET_OPCODE(op
) == SLJIT_SUB
) {
1591 src2_reg
= get_imm(-src2w
);
1593 op
= SLJIT_ADD
| GET_ALL_FLAGS(op
);
1599 if (src1
& SLJIT_IMM
) {
1600 src2_reg
= get_imm(src1w
);
1602 flags
|= ARGS_SWAPPED
;
1607 if (inp_flags
& ALLOW_INV_IMM
) {
1608 src2_reg
= get_imm(~src1w
);
1610 flags
|= ARGS_SWAPPED
| INV_IMM
;
1616 if (GET_OPCODE(op
) == SLJIT_ADD
) {
1617 src2_reg
= get_imm(-src1w
);
1619 /* Note: add is commutative operation. */
1622 op
= SLJIT_SUB
| GET_ALL_FLAGS(op
);
1630 if (FAST_IS_REG(src1
))
1632 else if (src1
& SLJIT_MEM
) {
1633 FAIL_IF(emit_op_mem(compiler
, inp_flags
| LOAD_DATA
, TMP_REG1
, src1
, src1w
, TMP_REG1
));
1634 src1_reg
= TMP_REG1
;
1637 FAIL_IF(load_immediate(compiler
, TMP_REG1
, src1w
));
1638 src1_reg
= TMP_REG1
;
1642 dst_reg
= SLOW_IS_REG(dst
) ? dst
: TMP_REG2
;
1644 if (op
<= SLJIT_MOV_P
) {
1645 if (dst
& SLJIT_MEM
) {
1646 if (inp_flags
& BYTE_SIZE
)
1647 inp_flags
&= ~SIGNED
;
1649 if (FAST_IS_REG(src2
))
1650 return emit_op_mem(compiler
, inp_flags
, src2
, dst
, dstw
, TMP_REG2
);
1653 if (FAST_IS_REG(src2
) && dst_reg
!= TMP_REG2
)
1654 flags
|= MOVE_REG_CONV
;
1658 if (src2_reg
== 0) {
1659 src2_reg
= (op
<= SLJIT_MOV_P
) ? dst_reg
: TMP_REG2
;
1661 if (FAST_IS_REG(src2
))
1663 else if (src2
& SLJIT_MEM
)
1664 FAIL_IF(emit_op_mem(compiler
, inp_flags
| LOAD_DATA
, src2_reg
, src2
, src2w
, TMP_REG2
));
1666 FAIL_IF(load_immediate(compiler
, src2_reg
, src2w
));
1669 FAIL_IF(emit_single_op(compiler
, op
, flags
, dst_reg
, src1_reg
, src2_reg
));
1671 if (!(dst
& SLJIT_MEM
))
1672 return SLJIT_SUCCESS
;
1674 return emit_op_mem(compiler
, inp_flags
, dst_reg
, dst
, dstw
, TMP_REG1
);
1681 #if defined(__GNUC__)
1682 extern unsigned int __aeabi_uidivmod(unsigned int numerator
, unsigned int denominator
);
1683 extern int __aeabi_idivmod(int numerator
, int denominator
);
1685 #error "Software divmod functions are needed"
1692 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op0(struct sljit_compiler
*compiler
, sljit_s32 op
)
1694 sljit_sw saved_reg_list
[3];
1695 sljit_sw saved_reg_count
;
1698 CHECK(check_sljit_emit_op0(compiler
, op
));
1700 op
= GET_OPCODE(op
);
1702 case SLJIT_BREAKPOINT
:
1703 FAIL_IF(push_inst(compiler
, BKPT
));
1706 FAIL_IF(push_inst(compiler
, NOP
));
1710 return push_inst(compiler
, (op
== SLJIT_LMUL_UW
? UMULL
: SMULL
)
1711 | (reg_map
[SLJIT_R1
] << 16)
1712 | (reg_map
[SLJIT_R0
] << 12)
1713 | (reg_map
[SLJIT_R0
] << 8)
1714 | reg_map
[SLJIT_R1
]);
1715 case SLJIT_DIVMOD_UW
:
1716 case SLJIT_DIVMOD_SW
:
1719 SLJIT_COMPILE_ASSERT((SLJIT_DIVMOD_UW
& 0x2) == 0 && SLJIT_DIV_UW
- 0x2 == SLJIT_DIVMOD_UW
, bad_div_opcode_assignments
);
1720 SLJIT_ASSERT(reg_map
[2] == 1 && reg_map
[3] == 2 && reg_map
[4] == 3);
1722 saved_reg_count
= 0;
1723 if (compiler
->scratches
>= 4)
1724 saved_reg_list
[saved_reg_count
++] = 3;
1725 if (compiler
->scratches
>= 3)
1726 saved_reg_list
[saved_reg_count
++] = 2;
1727 if (op
>= SLJIT_DIV_UW
)
1728 saved_reg_list
[saved_reg_count
++] = 1;
1730 if (saved_reg_count
> 0) {
1731 FAIL_IF(push_inst(compiler
, 0xe52d0000 | (saved_reg_count
>= 3 ? 16 : 8)
1732 | (saved_reg_list
[0] << 12) /* str rX, [sp, #-8/-16]! */));
1733 if (saved_reg_count
>= 2) {
1734 SLJIT_ASSERT(saved_reg_list
[1] < 8);
1735 FAIL_IF(push_inst(compiler
, 0xe58d0004 | (saved_reg_list
[1] << 12) /* str rX, [sp, #4] */));
1737 if (saved_reg_count
>= 3) {
1738 SLJIT_ASSERT(saved_reg_list
[2] < 8);
1739 FAIL_IF(push_inst(compiler
, 0xe58d0008 | (saved_reg_list
[2] << 12) /* str rX, [sp, #8] */));
1743 #if defined(__GNUC__)
1744 FAIL_IF(sljit_emit_ijump(compiler
, SLJIT_FAST_CALL
, SLJIT_IMM
,
1745 ((op
| 0x2) == SLJIT_DIV_UW
? SLJIT_FUNC_OFFSET(__aeabi_uidivmod
) : SLJIT_FUNC_OFFSET(__aeabi_idivmod
))));
1747 #error "Software divmod functions are needed"
1750 if (saved_reg_count
> 0) {
1751 if (saved_reg_count
>= 3) {
1752 SLJIT_ASSERT(saved_reg_list
[2] < 8);
1753 FAIL_IF(push_inst(compiler
, 0xe59d0008 | (saved_reg_list
[2] << 12) /* ldr rX, [sp, #8] */));
1755 if (saved_reg_count
>= 2) {
1756 SLJIT_ASSERT(saved_reg_list
[1] < 8);
1757 FAIL_IF(push_inst(compiler
, 0xe59d0004 | (saved_reg_list
[1] << 12) /* ldr rX, [sp, #4] */));
1759 return push_inst(compiler
, 0xe49d0000 | (saved_reg_count
>= 3 ? 16 : 8)
1760 | (saved_reg_list
[0] << 12) /* ldr rX, [sp], #8/16 */);
1762 return SLJIT_SUCCESS
;
1764 case SLJIT_SKIP_FRAMES_BEFORE_RETURN
:
1765 return SLJIT_SUCCESS
;
1768 return SLJIT_SUCCESS
;
1771 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op1(struct sljit_compiler
*compiler
, sljit_s32 op
,
1772 sljit_s32 dst
, sljit_sw dstw
,
1773 sljit_s32 src
, sljit_sw srcw
)
1776 CHECK(check_sljit_emit_op1(compiler
, op
, dst
, dstw
, src
, srcw
));
1777 ADJUST_LOCAL_OFFSET(dst
, dstw
);
1778 ADJUST_LOCAL_OFFSET(src
, srcw
);
1780 switch (GET_OPCODE(op
)) {
1785 return emit_op(compiler
, SLJIT_MOV
, ALLOW_ANY_IMM
, dst
, dstw
, TMP_REG1
, 0, src
, srcw
);
1788 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
);
1791 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
);
1794 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
);
1797 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
);
1800 return emit_op(compiler
, op
, ALLOW_ANY_IMM
, dst
, dstw
, TMP_REG1
, 0, src
, srcw
);
1803 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
1804 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
1805 compiler
->skip_checks
= 1;
1807 return sljit_emit_op2(compiler
, SLJIT_SUB
| GET_ALL_FLAGS(op
), dst
, dstw
, SLJIT_IMM
, 0, src
, srcw
);
1810 return emit_op(compiler
, op
, 0, dst
, dstw
, TMP_REG1
, 0, src
, srcw
);
1813 return SLJIT_SUCCESS
;
1816 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op2(struct sljit_compiler
*compiler
, sljit_s32 op
,
1817 sljit_s32 dst
, sljit_sw dstw
,
1818 sljit_s32 src1
, sljit_sw src1w
,
1819 sljit_s32 src2
, sljit_sw src2w
)
1822 CHECK(check_sljit_emit_op2(compiler
, op
, dst
, dstw
, src1
, src1w
, src2
, src2w
));
1823 ADJUST_LOCAL_OFFSET(dst
, dstw
);
1824 ADJUST_LOCAL_OFFSET(src1
, src1w
);
1825 ADJUST_LOCAL_OFFSET(src2
, src2w
);
1827 if (dst
== SLJIT_UNUSED
&& !HAS_FLAGS(op
))
1828 return SLJIT_SUCCESS
;
1830 switch (GET_OPCODE(op
)) {
1837 return emit_op(compiler
, op
, ALLOW_IMM
, dst
, dstw
, src1
, src1w
, src2
, src2w
);
1840 return emit_op(compiler
, op
, 0, dst
, dstw
, src1
, src1w
, src2
, src2w
);
1843 return emit_op(compiler
, op
, ALLOW_ANY_IMM
, dst
, dstw
, src1
, src1w
, src2
, src2w
);
1848 if (src2
& SLJIT_IMM
) {
1849 compiler
->shift_imm
= src2w
& 0x1f;
1850 return emit_op(compiler
, op
, 0, dst
, dstw
, TMP_REG1
, 0, src1
, src1w
);
1853 compiler
->shift_imm
= 0x20;
1854 return emit_op(compiler
, op
, 0, dst
, dstw
, src1
, src1w
, src2
, src2w
);
1858 return SLJIT_SUCCESS
;
1861 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op_src(struct sljit_compiler
*compiler
, sljit_s32 op
,
1862 sljit_s32 src
, sljit_sw srcw
)
1865 CHECK(check_sljit_emit_op_src(compiler
, op
, src
, srcw
));
1866 ADJUST_LOCAL_OFFSET(src
, srcw
);
1869 case SLJIT_FAST_RETURN
:
1870 SLJIT_ASSERT(reg_map
[TMP_REG2
] == 14);
1872 if (FAST_IS_REG(src
))
1873 FAIL_IF(push_inst(compiler
, MOV
| RD(TMP_REG2
) | RM(src
)));
1875 FAIL_IF(emit_op_mem(compiler
, WORD_SIZE
| LOAD_DATA
, TMP_REG2
, src
, srcw
, TMP_REG1
));
1877 return push_inst(compiler
, BX
| RM(TMP_REG2
));
1878 case SLJIT_SKIP_FRAMES_BEFORE_FAST_RETURN
:
1879 return SLJIT_SUCCESS
;
1880 case SLJIT_PREFETCH_L1
:
1881 case SLJIT_PREFETCH_L2
:
1882 case SLJIT_PREFETCH_L3
:
1883 case SLJIT_PREFETCH_ONCE
:
1884 #if (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
1885 SLJIT_ASSERT(src
& SLJIT_MEM
);
1886 return emit_op_mem(compiler
, PRELOAD
| LOAD_DATA
, TMP_PC
, src
, srcw
, TMP_REG1
);
1887 #else /* !SLJIT_CONFIG_ARM_V7 */
1888 return SLJIT_SUCCESS
;
1889 #endif /* SLJIT_CONFIG_ARM_V7 */
1892 return SLJIT_SUCCESS
;
1895 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_get_register_index(sljit_s32 reg
)
1897 CHECK_REG_INDEX(check_sljit_get_register_index(reg
));
1898 return reg_map
[reg
];
1901 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_get_float_register_index(sljit_s32 reg
)
1903 CHECK_REG_INDEX(check_sljit_get_float_register_index(reg
));
1904 return (freg_map
[reg
] << 1);
1907 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op_custom(struct sljit_compiler
*compiler
,
1908 void *instruction
, sljit_s32 size
)
1911 CHECK(check_sljit_emit_op_custom(compiler
, instruction
, size
));
1913 return push_inst(compiler
, *(sljit_uw
*)instruction
);
1916 /* --------------------------------------------------------------------- */
1917 /* Floating point operators */
1918 /* --------------------------------------------------------------------- */
1921 #define FPU_LOAD (1 << 20)
1922 #define EMIT_FPU_DATA_TRANSFER(inst, add, base, freg, offs) \
1923 ((inst) | ((add) << 23) | (reg_map[base] << 16) | (freg_map[freg] << 12) | (offs))
1924 #define EMIT_FPU_OPERATION(opcode, mode, dst, src1, src2) \
1925 ((opcode) | (mode) | (freg_map[dst] << 12) | freg_map[src1] | (freg_map[src2] << 16))
1927 static sljit_s32
emit_fop_mem(struct sljit_compiler
*compiler
, sljit_s32 flags
, sljit_s32 reg
, sljit_s32 arg
, sljit_sw argw
)
1930 sljit_sw inst
= VSTR_F32
| (flags
& (SLJIT_F32_OP
| FPU_LOAD
));
1932 SLJIT_ASSERT(arg
& SLJIT_MEM
);
1935 if (SLJIT_UNLIKELY(arg
& OFFS_REG_MASK
)) {
1936 FAIL_IF(push_inst(compiler
, ADD
| RD(TMP_REG2
) | RN(arg
& REG_MASK
) | RM(OFFS_REG(arg
)) | ((argw
& 0x3) << 7)));
1941 /* Fast loads and stores. */
1943 if (!(argw
& ~0x3fc))
1944 return push_inst(compiler
, EMIT_FPU_DATA_TRANSFER(inst
, 1, arg
& REG_MASK
, reg
, argw
>> 2));
1945 if (!(-argw
& ~0x3fc))
1946 return push_inst(compiler
, EMIT_FPU_DATA_TRANSFER(inst
, 0, arg
& REG_MASK
, reg
, (-argw
) >> 2));
1948 imm
= get_imm(argw
& ~0x3fc);
1950 FAIL_IF(push_inst(compiler
, ADD
| RD(TMP_REG2
) | RN(arg
& REG_MASK
) | imm
));
1951 return push_inst(compiler
, EMIT_FPU_DATA_TRANSFER(inst
, 1, TMP_REG2
, reg
, (argw
& 0x3fc) >> 2));
1953 imm
= get_imm(-argw
& ~0x3fc);
1956 FAIL_IF(push_inst(compiler
, SUB
| RD(TMP_REG2
) | RN(arg
& REG_MASK
) | imm
));
1957 return push_inst(compiler
, EMIT_FPU_DATA_TRANSFER(inst
, 0, TMP_REG2
, reg
, (argw
& 0x3fc) >> 2));
1962 FAIL_IF(load_immediate(compiler
, TMP_REG2
, argw
));
1963 FAIL_IF(push_inst(compiler
, ADD
| RD(TMP_REG2
) | RN(arg
& REG_MASK
) | RM(TMP_REG2
)));
1966 FAIL_IF(load_immediate(compiler
, TMP_REG2
, argw
));
1968 return push_inst(compiler
, EMIT_FPU_DATA_TRANSFER(inst
, 1, TMP_REG2
, reg
, 0));
1971 static SLJIT_INLINE sljit_s32
sljit_emit_fop1_conv_sw_from_f64(struct sljit_compiler
*compiler
, sljit_s32 op
,
1972 sljit_s32 dst
, sljit_sw dstw
,
1973 sljit_s32 src
, sljit_sw srcw
)
1977 if (src
& SLJIT_MEM
) {
1978 FAIL_IF(emit_fop_mem(compiler
, (op
& SLJIT_F32_OP
) | FPU_LOAD
, TMP_FREG1
, src
, srcw
));
1982 FAIL_IF(push_inst(compiler
, EMIT_FPU_OPERATION(VCVT_S32_F32
, op
& SLJIT_F32_OP
, TMP_FREG1
, src
, 0)));
1984 if (FAST_IS_REG(dst
))
1985 return push_inst(compiler
, VMOV
| (1 << 20) | RD(dst
) | (freg_map
[TMP_FREG1
] << 16));
1987 /* Store the integer value from a VFP register. */
1988 return emit_fop_mem(compiler
, 0, TMP_FREG1
, dst
, dstw
);
1991 static SLJIT_INLINE sljit_s32
sljit_emit_fop1_conv_f64_from_sw(struct sljit_compiler
*compiler
, sljit_s32 op
,
1992 sljit_s32 dst
, sljit_sw dstw
,
1993 sljit_s32 src
, sljit_sw srcw
)
1995 sljit_s32 dst_r
= FAST_IS_REG(dst
) ? dst
: TMP_FREG1
;
1999 if (FAST_IS_REG(src
))
2000 FAIL_IF(push_inst(compiler
, VMOV
| RD(src
) | (freg_map
[TMP_FREG1
] << 16)));
2001 else if (src
& SLJIT_MEM
) {
2002 /* Load the integer value into a VFP register. */
2003 FAIL_IF(emit_fop_mem(compiler
, FPU_LOAD
, TMP_FREG1
, src
, srcw
));
2006 FAIL_IF(load_immediate(compiler
, TMP_REG1
, srcw
));
2007 FAIL_IF(push_inst(compiler
, VMOV
| RD(TMP_REG1
) | (freg_map
[TMP_FREG1
] << 16)));
2010 FAIL_IF(push_inst(compiler
, EMIT_FPU_OPERATION(VCVT_F32_S32
, op
& SLJIT_F32_OP
, dst_r
, TMP_FREG1
, 0)));
2012 if (dst
& SLJIT_MEM
)
2013 return emit_fop_mem(compiler
, (op
& SLJIT_F32_OP
), TMP_FREG1
, dst
, dstw
);
2014 return SLJIT_SUCCESS
;
2017 static SLJIT_INLINE sljit_s32
sljit_emit_fop1_cmp(struct sljit_compiler
*compiler
, sljit_s32 op
,
2018 sljit_s32 src1
, sljit_sw src1w
,
2019 sljit_s32 src2
, sljit_sw src2w
)
2023 if (src1
& SLJIT_MEM
) {
2024 FAIL_IF(emit_fop_mem(compiler
, (op
& SLJIT_F32_OP
) | FPU_LOAD
, TMP_FREG1
, src1
, src1w
));
2028 if (src2
& SLJIT_MEM
) {
2029 FAIL_IF(emit_fop_mem(compiler
, (op
& SLJIT_F32_OP
) | FPU_LOAD
, TMP_FREG2
, src2
, src2w
));
2033 FAIL_IF(push_inst(compiler
, EMIT_FPU_OPERATION(VCMP_F32
, op
& SLJIT_F32_OP
, src1
, src2
, 0)));
2034 return push_inst(compiler
, VMRS
);
2037 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_fop1(struct sljit_compiler
*compiler
, sljit_s32 op
,
2038 sljit_s32 dst
, sljit_sw dstw
,
2039 sljit_s32 src
, sljit_sw srcw
)
2045 SLJIT_COMPILE_ASSERT((SLJIT_F32_OP
== 0x100), float_transfer_bit_error
);
2046 SELECT_FOP1_OPERATION_WITH_CHECKS(compiler
, op
, dst
, dstw
, src
, srcw
);
2048 dst_r
= FAST_IS_REG(dst
) ? dst
: TMP_FREG1
;
2050 if (GET_OPCODE(op
) != SLJIT_CONV_F64_FROM_F32
)
2053 if (src
& SLJIT_MEM
) {
2054 FAIL_IF(emit_fop_mem(compiler
, (op
& SLJIT_F32_OP
) | FPU_LOAD
, dst_r
, src
, srcw
));
2058 switch (GET_OPCODE(op
)) {
2061 if (dst_r
!= TMP_FREG1
)
2062 FAIL_IF(push_inst(compiler
, EMIT_FPU_OPERATION(VMOV_F32
, op
& SLJIT_F32_OP
, dst_r
, src
, 0)));
2068 FAIL_IF(push_inst(compiler
, EMIT_FPU_OPERATION(VNEG_F32
, op
& SLJIT_F32_OP
, dst_r
, src
, 0)));
2071 FAIL_IF(push_inst(compiler
, EMIT_FPU_OPERATION(VABS_F32
, op
& SLJIT_F32_OP
, dst_r
, src
, 0)));
2073 case SLJIT_CONV_F64_FROM_F32
:
2074 FAIL_IF(push_inst(compiler
, EMIT_FPU_OPERATION(VCVT_F64_F32
, op
& SLJIT_F32_OP
, dst_r
, src
, 0)));
2079 if (dst
& SLJIT_MEM
)
2080 return emit_fop_mem(compiler
, (op
& SLJIT_F32_OP
), dst_r
, dst
, dstw
);
2081 return SLJIT_SUCCESS
;
2084 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_fop2(struct sljit_compiler
*compiler
, sljit_s32 op
,
2085 sljit_s32 dst
, sljit_sw dstw
,
2086 sljit_s32 src1
, sljit_sw src1w
,
2087 sljit_s32 src2
, sljit_sw src2w
)
2092 CHECK(check_sljit_emit_fop2(compiler
, op
, dst
, dstw
, src1
, src1w
, src2
, src2w
));
2093 ADJUST_LOCAL_OFFSET(dst
, dstw
);
2094 ADJUST_LOCAL_OFFSET(src1
, src1w
);
2095 ADJUST_LOCAL_OFFSET(src2
, src2w
);
2099 dst_r
= FAST_IS_REG(dst
) ? dst
: TMP_FREG1
;
2101 if (src2
& SLJIT_MEM
) {
2102 FAIL_IF(emit_fop_mem(compiler
, (op
& SLJIT_F32_OP
) | FPU_LOAD
, TMP_FREG2
, src2
, src2w
));
2106 if (src1
& SLJIT_MEM
) {
2107 FAIL_IF(emit_fop_mem(compiler
, (op
& SLJIT_F32_OP
) | FPU_LOAD
, TMP_FREG1
, src1
, src1w
));
2111 switch (GET_OPCODE(op
)) {
2113 FAIL_IF(push_inst(compiler
, EMIT_FPU_OPERATION(VADD_F32
, op
& SLJIT_F32_OP
, dst_r
, src2
, src1
)));
2117 FAIL_IF(push_inst(compiler
, EMIT_FPU_OPERATION(VSUB_F32
, op
& SLJIT_F32_OP
, dst_r
, src2
, src1
)));
2121 FAIL_IF(push_inst(compiler
, EMIT_FPU_OPERATION(VMUL_F32
, op
& SLJIT_F32_OP
, dst_r
, src2
, src1
)));
2125 FAIL_IF(push_inst(compiler
, EMIT_FPU_OPERATION(VDIV_F32
, op
& SLJIT_F32_OP
, dst_r
, src2
, src1
)));
2129 if (dst_r
== TMP_FREG1
)
2130 FAIL_IF(emit_fop_mem(compiler
, (op
& SLJIT_F32_OP
), TMP_FREG1
, dst
, dstw
));
2132 return SLJIT_SUCCESS
;
2136 #undef EMIT_FPU_DATA_TRANSFER
2138 /* --------------------------------------------------------------------- */
2139 /* Other instructions */
2140 /* --------------------------------------------------------------------- */
2142 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_fast_enter(struct sljit_compiler
*compiler
, sljit_s32 dst
, sljit_sw dstw
)
2145 CHECK(check_sljit_emit_fast_enter(compiler
, dst
, dstw
));
2146 ADJUST_LOCAL_OFFSET(dst
, dstw
);
2148 SLJIT_ASSERT(reg_map
[TMP_REG2
] == 14);
2150 if (FAST_IS_REG(dst
))
2151 return push_inst(compiler
, MOV
| RD(dst
) | RM(TMP_REG2
));
2154 return emit_op_mem(compiler
, WORD_SIZE
, TMP_REG2
, dst
, dstw
, TMP_REG1
);
2157 /* --------------------------------------------------------------------- */
2158 /* Conditional instructions */
2159 /* --------------------------------------------------------------------- */
2161 static sljit_uw
get_cc(struct sljit_compiler
*compiler
, sljit_s32 type
)
2165 case SLJIT_EQUAL_F64
:
2168 case SLJIT_NOT_EQUAL
:
2169 case SLJIT_NOT_EQUAL_F64
:
2173 case SLJIT_LESS_F64
:
2176 case SLJIT_GREATER_EQUAL
:
2177 case SLJIT_GREATER_EQUAL_F64
:
2181 case SLJIT_GREATER_F64
:
2184 case SLJIT_LESS_EQUAL
:
2185 case SLJIT_LESS_EQUAL_F64
:
2188 case SLJIT_SIG_LESS
:
2191 case SLJIT_SIG_GREATER_EQUAL
:
2194 case SLJIT_SIG_GREATER
:
2197 case SLJIT_SIG_LESS_EQUAL
:
2200 case SLJIT_OVERFLOW
:
2201 if (!(compiler
->status_flags_state
& SLJIT_CURRENT_FLAGS_ADD_SUB
))
2204 case SLJIT_UNORDERED_F64
:
2207 case SLJIT_NOT_OVERFLOW
:
2208 if (!(compiler
->status_flags_state
& SLJIT_CURRENT_FLAGS_ADD_SUB
))
2211 case SLJIT_ORDERED_F64
:
2215 SLJIT_ASSERT(type
>= SLJIT_JUMP
&& type
<= SLJIT_CALL_CDECL
);
2220 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_label
* sljit_emit_label(struct sljit_compiler
*compiler
)
2222 struct sljit_label
*label
;
2225 CHECK_PTR(check_sljit_emit_label(compiler
));
2227 if (compiler
->last_label
&& compiler
->last_label
->size
== compiler
->size
)
2228 return compiler
->last_label
;
2230 label
= (struct sljit_label
*)ensure_abuf(compiler
, sizeof(struct sljit_label
));
2231 PTR_FAIL_IF(!label
);
2232 set_label(label
, compiler
);
2236 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_jump
* sljit_emit_jump(struct sljit_compiler
*compiler
, sljit_s32 type
)
2238 struct sljit_jump
*jump
;
2241 CHECK_PTR(check_sljit_emit_jump(compiler
, type
));
2243 jump
= (struct sljit_jump
*)ensure_abuf(compiler
, sizeof(struct sljit_jump
));
2245 set_jump(jump
, compiler
, type
& SLJIT_REWRITABLE_JUMP
);
2248 SLJIT_ASSERT(reg_map
[TMP_REG1
] != 14);
2250 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
2251 if (type
>= SLJIT_FAST_CALL
)
2252 PTR_FAIL_IF(prepare_blx(compiler
));
2253 PTR_FAIL_IF(push_inst_with_unique_literal(compiler
, ((EMIT_DATA_TRANSFER(WORD_SIZE
| LOAD_DATA
, 1,
2254 type
<= SLJIT_JUMP
? TMP_PC
: TMP_REG1
, TMP_PC
, 0)) & ~COND_MASK
) | get_cc(compiler
, type
), 0));
2256 if (jump
->flags
& SLJIT_REWRITABLE_JUMP
) {
2257 jump
->addr
= compiler
->size
;
2258 compiler
->patches
++;
2261 if (type
>= SLJIT_FAST_CALL
) {
2262 jump
->flags
|= IS_BL
;
2263 PTR_FAIL_IF(emit_blx(compiler
));
2266 if (!(jump
->flags
& SLJIT_REWRITABLE_JUMP
))
2267 jump
->addr
= compiler
->size
;
2269 if (type
>= SLJIT_FAST_CALL
)
2270 jump
->flags
|= IS_BL
;
2271 PTR_FAIL_IF(emit_imm(compiler
, TMP_REG1
, 0));
2272 PTR_FAIL_IF(push_inst(compiler
, (((type
<= SLJIT_JUMP
? BX
: BLX
) | RM(TMP_REG1
)) & ~COND_MASK
) | get_cc(compiler
, type
)));
2273 jump
->addr
= compiler
->size
;
2280 static sljit_s32
softfloat_call_with_args(struct sljit_compiler
*compiler
, sljit_s32 arg_types
, sljit_s32
*src
)
2282 sljit_s32 stack_offset
= 0;
2283 sljit_s32 arg_count
= 0;
2284 sljit_s32 word_arg_offset
= 0;
2285 sljit_s32 float_arg_count
= 0;
2286 sljit_s32 types
= 0;
2287 sljit_s32 src_offset
= 4 * sizeof(sljit_sw
);
2288 sljit_u8 offsets
[4];
2290 if (src
&& FAST_IS_REG(*src
))
2291 src_offset
= reg_map
[*src
] * sizeof(sljit_sw
);
2293 arg_types
>>= SLJIT_DEF_SHIFT
;
2296 types
= (types
<< SLJIT_DEF_SHIFT
) | (arg_types
& SLJIT_DEF_MASK
);
2298 switch (arg_types
& SLJIT_DEF_MASK
) {
2299 case SLJIT_ARG_TYPE_F32
:
2300 offsets
[arg_count
] = (sljit_u8
)stack_offset
;
2301 stack_offset
+= sizeof(sljit_f32
);
2305 case SLJIT_ARG_TYPE_F64
:
2306 if (stack_offset
& 0x7)
2307 stack_offset
+= sizeof(sljit_sw
);
2308 offsets
[arg_count
] = (sljit_u8
)stack_offset
;
2309 stack_offset
+= sizeof(sljit_f64
);
2314 offsets
[arg_count
] = (sljit_u8
)stack_offset
;
2315 stack_offset
+= sizeof(sljit_sw
);
2317 word_arg_offset
+= sizeof(sljit_sw
);
2321 arg_types
>>= SLJIT_DEF_SHIFT
;
2324 if (stack_offset
> 16)
2325 FAIL_IF(push_inst(compiler
, SUB
| RD(SLJIT_SP
) | RN(SLJIT_SP
) | SRC2_IMM
| (((stack_offset
- 16) + 0x7) & ~0x7)));
2327 /* Process arguments in reversed direction. */
2329 switch (types
& SLJIT_DEF_MASK
) {
2330 case SLJIT_ARG_TYPE_F32
:
2333 stack_offset
= offsets
[arg_count
];
2335 if (stack_offset
< 16) {
2336 if (src_offset
== stack_offset
) {
2337 FAIL_IF(push_inst(compiler
, MOV
| RD(TMP_REG1
) | (src_offset
>> 2)));
2340 FAIL_IF(push_inst(compiler
, VMOV
| 0x100000 | (float_arg_count
<< 16) | (stack_offset
<< 10)));
2342 FAIL_IF(push_inst(compiler
, VSTR_F32
| 0x800000 | RN(SLJIT_SP
) | (float_arg_count
<< 12) | ((stack_offset
- 16) >> 2)));
2344 case SLJIT_ARG_TYPE_F64
:
2347 stack_offset
= offsets
[arg_count
];
2349 SLJIT_ASSERT((stack_offset
& 0x7) == 0);
2351 if (stack_offset
< 16) {
2352 if (src_offset
== stack_offset
|| src_offset
== stack_offset
+ sizeof(sljit_sw
)) {
2353 FAIL_IF(push_inst(compiler
, MOV
| RD(TMP_REG1
) | (src_offset
>> 2)));
2356 FAIL_IF(push_inst(compiler
, VMOV2
| 0x100000 | (stack_offset
<< 10) | ((stack_offset
+ sizeof(sljit_sw
)) << 14) | float_arg_count
));
2358 FAIL_IF(push_inst(compiler
, VSTR_F32
| 0x800100 | RN(SLJIT_SP
) | (float_arg_count
<< 12) | ((stack_offset
- 16) >> 2)));
2362 word_arg_offset
-= sizeof(sljit_sw
);
2363 stack_offset
= offsets
[arg_count
];
2365 SLJIT_ASSERT(stack_offset
>= word_arg_offset
);
2367 if (stack_offset
!= word_arg_offset
) {
2368 if (stack_offset
< 16) {
2369 if (src_offset
== stack_offset
) {
2370 FAIL_IF(push_inst(compiler
, MOV
| RD(TMP_REG1
) | (src_offset
>> 2)));
2373 else if (src_offset
== word_arg_offset
) {
2374 *src
= 1 + (stack_offset
>> 2);
2375 src_offset
= stack_offset
;
2377 FAIL_IF(push_inst(compiler
, MOV
| (stack_offset
<< 10) | (word_arg_offset
>> 2)));
2379 FAIL_IF(push_inst(compiler
, data_transfer_insts
[WORD_SIZE
] | 0x800000 | RN(SLJIT_SP
) | (word_arg_offset
<< 10) | (stack_offset
- 16)));
2384 types
>>= SLJIT_DEF_SHIFT
;
2387 return SLJIT_SUCCESS
;
2390 static sljit_s32
softfloat_post_call_with_args(struct sljit_compiler
*compiler
, sljit_s32 arg_types
)
2392 sljit_s32 stack_size
= 0;
2394 if ((arg_types
& SLJIT_DEF_MASK
) == SLJIT_ARG_TYPE_F32
)
2395 FAIL_IF(push_inst(compiler
, VMOV
| (0 << 16) | (0 << 12)));
2396 if ((arg_types
& SLJIT_DEF_MASK
) == SLJIT_ARG_TYPE_F64
)
2397 FAIL_IF(push_inst(compiler
, VMOV2
| (1 << 16) | (0 << 12) | 0));
2399 arg_types
>>= SLJIT_DEF_SHIFT
;
2402 switch (arg_types
& SLJIT_DEF_MASK
) {
2403 case SLJIT_ARG_TYPE_F32
:
2404 stack_size
+= sizeof(sljit_f32
);
2406 case SLJIT_ARG_TYPE_F64
:
2407 if (stack_size
& 0x7)
2408 stack_size
+= sizeof(sljit_sw
);
2409 stack_size
+= sizeof(sljit_f64
);
2412 stack_size
+= sizeof(sljit_sw
);
2416 arg_types
>>= SLJIT_DEF_SHIFT
;
2419 if (stack_size
<= 16)
2420 return SLJIT_SUCCESS
;
2422 return push_inst(compiler
, ADD
| RD(SLJIT_SP
) | RN(SLJIT_SP
) | SRC2_IMM
| (((stack_size
- 16) + 0x7) & ~0x7));
2425 #else /* !__SOFTFP__ */
2427 static sljit_s32
hardfloat_call_with_args(struct sljit_compiler
*compiler
, sljit_s32 arg_types
)
2429 sljit_u32 remap
= 0;
2430 sljit_u32 offset
= 0;
2431 sljit_u32 new_offset
, mask
;
2433 /* Remove return value. */
2434 arg_types
>>= SLJIT_DEF_SHIFT
;
2437 if ((arg_types
& SLJIT_DEF_MASK
) == SLJIT_ARG_TYPE_F32
) {
2441 while (remap
& mask
) {
2447 if (offset
!= new_offset
)
2448 FAIL_IF(push_inst(compiler
, EMIT_FPU_OPERATION(VMOV_F32
,
2449 0, (new_offset
>> 1) + 1, (offset
>> 1) + 1, 0) | ((new_offset
& 0x1) ? 0x400000 : 0)));
2453 else if ((arg_types
& SLJIT_DEF_MASK
) == SLJIT_ARG_TYPE_F64
) {
2457 while (remap
& mask
) {
2463 if (offset
!= new_offset
)
2464 FAIL_IF(push_inst(compiler
, EMIT_FPU_OPERATION(VMOV_F32
, SLJIT_F32_OP
, (new_offset
>> 1) + 1, (offset
>> 1) + 1, 0)));
2468 arg_types
>>= SLJIT_DEF_SHIFT
;
2471 return SLJIT_SUCCESS
;
2474 #endif /* __SOFTFP__ */
2476 #undef EMIT_FPU_OPERATION
2478 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_jump
* sljit_emit_call(struct sljit_compiler
*compiler
, sljit_s32 type
,
2479 sljit_s32 arg_types
)
2482 struct sljit_jump
*jump
;
2486 CHECK_PTR(check_sljit_emit_call(compiler
, type
, arg_types
));
2489 PTR_FAIL_IF(softfloat_call_with_args(compiler
, arg_types
, NULL
));
2491 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2492 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2493 compiler
->skip_checks
= 1;
2496 jump
= sljit_emit_jump(compiler
, type
);
2497 PTR_FAIL_IF(jump
== NULL
);
2499 PTR_FAIL_IF(softfloat_post_call_with_args(compiler
, arg_types
));
2501 #else /* !__SOFTFP__ */
2502 PTR_FAIL_IF(hardfloat_call_with_args(compiler
, arg_types
));
2504 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2505 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2506 compiler
->skip_checks
= 1;
2509 return sljit_emit_jump(compiler
, type
);
2510 #endif /* __SOFTFP__ */
2513 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_ijump(struct sljit_compiler
*compiler
, sljit_s32 type
, sljit_s32 src
, sljit_sw srcw
)
2515 struct sljit_jump
*jump
;
2518 CHECK(check_sljit_emit_ijump(compiler
, type
, src
, srcw
));
2519 ADJUST_LOCAL_OFFSET(src
, srcw
);
2521 SLJIT_ASSERT(reg_map
[TMP_REG1
] != 14);
2523 if (!(src
& SLJIT_IMM
)) {
2524 if (FAST_IS_REG(src
)) {
2525 SLJIT_ASSERT(reg_map
[src
] != 14);
2526 return push_inst(compiler
, (type
<= SLJIT_JUMP
? BX
: BLX
) | RM(src
));
2529 SLJIT_ASSERT(src
& SLJIT_MEM
);
2530 FAIL_IF(emit_op_mem(compiler
, WORD_SIZE
| LOAD_DATA
, TMP_REG1
, src
, srcw
, TMP_REG1
));
2531 return push_inst(compiler
, (type
<= SLJIT_JUMP
? BX
: BLX
) | RM(TMP_REG1
));
2534 /* These jumps are converted to jump/call instructions when possible. */
2535 jump
= (struct sljit_jump
*)ensure_abuf(compiler
, sizeof(struct sljit_jump
));
2537 set_jump(jump
, compiler
, JUMP_ADDR
| ((type
>= SLJIT_FAST_CALL
) ? IS_BL
: 0));
2538 jump
->u
.target
= srcw
;
2540 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
2541 if (type
>= SLJIT_FAST_CALL
)
2542 FAIL_IF(prepare_blx(compiler
));
2543 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));
2544 if (type
>= SLJIT_FAST_CALL
)
2545 FAIL_IF(emit_blx(compiler
));
2547 FAIL_IF(emit_imm(compiler
, TMP_REG1
, 0));
2548 FAIL_IF(push_inst(compiler
, (type
<= SLJIT_JUMP
? BX
: BLX
) | RM(TMP_REG1
)));
2550 jump
->addr
= compiler
->size
;
2551 return SLJIT_SUCCESS
;
2554 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_icall(struct sljit_compiler
*compiler
, sljit_s32 type
,
2555 sljit_s32 arg_types
,
2556 sljit_s32 src
, sljit_sw srcw
)
2559 CHECK(check_sljit_emit_icall(compiler
, type
, arg_types
, src
, srcw
));
2562 if (src
& SLJIT_MEM
) {
2563 FAIL_IF(emit_op_mem(compiler
, WORD_SIZE
| LOAD_DATA
, TMP_REG1
, src
, srcw
, TMP_REG1
));
2567 FAIL_IF(softfloat_call_with_args(compiler
, arg_types
, &src
));
2569 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2570 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2571 compiler
->skip_checks
= 1;
2574 FAIL_IF(sljit_emit_ijump(compiler
, type
, src
, srcw
));
2576 return softfloat_post_call_with_args(compiler
, arg_types
);
2577 #else /* !__SOFTFP__ */
2578 FAIL_IF(hardfloat_call_with_args(compiler
, arg_types
));
2580 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2581 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2582 compiler
->skip_checks
= 1;
2585 return sljit_emit_ijump(compiler
, type
, src
, srcw
);
2586 #endif /* __SOFTFP__ */
2589 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op_flags(struct sljit_compiler
*compiler
, sljit_s32 op
,
2590 sljit_s32 dst
, sljit_sw dstw
,
2593 sljit_s32 dst_reg
, flags
= GET_ALL_FLAGS(op
);
2597 CHECK(check_sljit_emit_op_flags(compiler
, op
, dst
, dstw
, type
));
2598 ADJUST_LOCAL_OFFSET(dst
, dstw
);
2600 op
= GET_OPCODE(op
);
2601 cc
= get_cc(compiler
, type
& 0xff);
2602 dst_reg
= FAST_IS_REG(dst
) ? dst
: TMP_REG1
;
2604 if (op
< SLJIT_ADD
) {
2605 FAIL_IF(push_inst(compiler
, MOV
| RD(dst_reg
) | SRC2_IMM
| 0));
2606 FAIL_IF(push_inst(compiler
, ((MOV
| RD(dst_reg
) | SRC2_IMM
| 1) & ~COND_MASK
) | cc
));
2607 if (dst
& SLJIT_MEM
)
2608 return emit_op_mem(compiler
, WORD_SIZE
, TMP_REG1
, dst
, dstw
, TMP_REG2
);
2609 return SLJIT_SUCCESS
;
2612 ins
= (op
== SLJIT_AND
? AND
: (op
== SLJIT_OR
? ORR
: EOR
));
2614 if (dst
& SLJIT_MEM
)
2615 FAIL_IF(emit_op_mem(compiler
, WORD_SIZE
| LOAD_DATA
, TMP_REG1
, dst
, dstw
, TMP_REG2
));
2617 FAIL_IF(push_inst(compiler
, ((ins
| RD(dst_reg
) | RN(dst_reg
) | SRC2_IMM
| 1) & ~COND_MASK
) | cc
));
2619 if (op
== SLJIT_AND
)
2620 FAIL_IF(push_inst(compiler
, ((ins
| RD(dst_reg
) | RN(dst_reg
) | SRC2_IMM
| 0) & ~COND_MASK
) | (cc
^ 0x10000000)));
2622 if (dst
& SLJIT_MEM
)
2623 FAIL_IF(emit_op_mem(compiler
, WORD_SIZE
, TMP_REG1
, dst
, dstw
, TMP_REG2
));
2625 if (flags
& SLJIT_SET_Z
)
2626 return push_inst(compiler
, MOV
| SET_FLAGS
| RD(TMP_REG2
) | RM(dst_reg
));
2627 return SLJIT_SUCCESS
;
2630 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_cmov(struct sljit_compiler
*compiler
, sljit_s32 type
,
2632 sljit_s32 src
, sljit_sw srcw
)
2637 CHECK(check_sljit_emit_cmov(compiler
, type
, dst_reg
, src
, srcw
));
2639 dst_reg
&= ~SLJIT_I32_OP
;
2641 cc
= get_cc(compiler
, type
& 0xff);
2643 if (SLJIT_UNLIKELY(src
& SLJIT_IMM
)) {
2644 tmp
= get_imm(srcw
);
2646 return push_inst(compiler
, ((MOV
| RD(dst_reg
) | tmp
) & ~COND_MASK
) | cc
);
2648 tmp
= get_imm(~srcw
);
2650 return push_inst(compiler
, ((MVN
| RD(dst_reg
) | tmp
) & ~COND_MASK
) | cc
);
2652 #if (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
2653 tmp
= (sljit_uw
) srcw
;
2654 FAIL_IF(push_inst(compiler
, (MOVW
& ~COND_MASK
) | cc
| RD(dst_reg
) | ((tmp
<< 4) & 0xf0000) | (tmp
& 0xfff)));
2656 return SLJIT_SUCCESS
;
2657 return push_inst(compiler
, (MOVT
& ~COND_MASK
) | cc
| RD(dst_reg
) | ((tmp
>> 12) & 0xf0000) | ((tmp
>> 16) & 0xfff));
2659 FAIL_IF(load_immediate(compiler
, TMP_REG1
, srcw
));
2664 return push_inst(compiler
, ((MOV
| RD(dst_reg
) | RM(src
)) & ~COND_MASK
) | cc
);
2667 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_mem(struct sljit_compiler
*compiler
, sljit_s32 type
,
2669 sljit_s32 mem
, sljit_sw memw
)
2672 sljit_uw is_type1_transfer
, inst
;
2675 CHECK(check_sljit_emit_mem(compiler
, type
, reg
, mem
, memw
));
2677 is_type1_transfer
= 1;
2679 switch (type
& 0xff) {
2690 if (!(type
& SLJIT_MEM_STORE
))
2691 is_type1_transfer
= 0;
2692 flags
= BYTE_SIZE
| SIGNED
;
2695 is_type1_transfer
= 0;
2699 is_type1_transfer
= 0;
2700 flags
= HALF_SIZE
| SIGNED
;
2703 SLJIT_UNREACHABLE();
2708 if (!(type
& SLJIT_MEM_STORE
))
2711 SLJIT_ASSERT(is_type1_transfer
== !!IS_TYPE1_TRANSFER(flags
));
2713 if (SLJIT_UNLIKELY(mem
& OFFS_REG_MASK
)) {
2714 if (!is_type1_transfer
&& memw
!= 0)
2715 return SLJIT_ERR_UNSUPPORTED
;
2718 if (is_type1_transfer
) {
2719 if (memw
> 4095 || memw
< -4095)
2720 return SLJIT_ERR_UNSUPPORTED
;
2723 if (memw
> 255 || memw
< -255)
2724 return SLJIT_ERR_UNSUPPORTED
;
2728 if (type
& SLJIT_MEM_SUPP
)
2729 return SLJIT_SUCCESS
;
2731 if (SLJIT_UNLIKELY(mem
& OFFS_REG_MASK
)) {
2734 inst
= EMIT_DATA_TRANSFER(flags
, 1, reg
, mem
& REG_MASK
, RM(OFFS_REG(mem
)) | (memw
<< 7));
2736 if (is_type1_transfer
)
2739 if (type
& SLJIT_MEM_PRE
)
2744 return push_inst(compiler
, inst
);
2747 inst
= EMIT_DATA_TRANSFER(flags
, 0, reg
, mem
& REG_MASK
, 0);
2749 if (type
& SLJIT_MEM_PRE
)
2754 if (is_type1_transfer
) {
2760 return push_inst(compiler
, inst
| memw
);
2768 return push_inst(compiler
, inst
| TYPE2_TRANSFER_IMM(memw
));
2771 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_const
* sljit_emit_const(struct sljit_compiler
*compiler
, sljit_s32 dst
, sljit_sw dstw
, sljit_sw init_value
)
2773 struct sljit_const
*const_
;
2777 CHECK_PTR(check_sljit_emit_const(compiler
, dst
, dstw
, init_value
));
2778 ADJUST_LOCAL_OFFSET(dst
, dstw
);
2780 dst_r
= SLOW_IS_REG(dst
) ? dst
: TMP_REG2
;
2782 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
2783 PTR_FAIL_IF(push_inst_with_unique_literal(compiler
, EMIT_DATA_TRANSFER(WORD_SIZE
| LOAD_DATA
, 1, dst_r
, TMP_PC
, 0), init_value
));
2784 compiler
->patches
++;
2786 PTR_FAIL_IF(emit_imm(compiler
, dst_r
, init_value
));
2789 const_
= (struct sljit_const
*)ensure_abuf(compiler
, sizeof(struct sljit_const
));
2790 PTR_FAIL_IF(!const_
);
2791 set_const(const_
, compiler
);
2793 if (dst
& SLJIT_MEM
)
2794 PTR_FAIL_IF(emit_op_mem(compiler
, WORD_SIZE
, TMP_REG2
, dst
, dstw
, TMP_REG1
));
2798 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_put_label
* sljit_emit_put_label(struct sljit_compiler
*compiler
, sljit_s32 dst
, sljit_sw dstw
)
2800 struct sljit_put_label
*put_label
;
2804 CHECK_PTR(check_sljit_emit_put_label(compiler
, dst
, dstw
));
2805 ADJUST_LOCAL_OFFSET(dst
, dstw
);
2807 dst_r
= SLOW_IS_REG(dst
) ? dst
: TMP_REG2
;
2809 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
2810 PTR_FAIL_IF(push_inst_with_unique_literal(compiler
, EMIT_DATA_TRANSFER(WORD_SIZE
| LOAD_DATA
, 1, dst_r
, TMP_PC
, 0), 0));
2811 compiler
->patches
++;
2813 PTR_FAIL_IF(emit_imm(compiler
, dst_r
, 0));
2816 put_label
= (struct sljit_put_label
*)ensure_abuf(compiler
, sizeof(struct sljit_put_label
));
2817 PTR_FAIL_IF(!put_label
);
2818 set_put_label(put_label
, compiler
, 0);
2820 if (dst
& SLJIT_MEM
)
2821 PTR_FAIL_IF(emit_op_mem(compiler
, WORD_SIZE
, TMP_REG2
, dst
, dstw
, TMP_REG1
));
2825 SLJIT_API_FUNC_ATTRIBUTE
void sljit_set_jump_addr(sljit_uw addr
, sljit_uw new_target
, sljit_sw executable_offset
)
2827 inline_set_jump_addr(addr
, executable_offset
, new_target
, 1);
2830 SLJIT_API_FUNC_ATTRIBUTE
void sljit_set_const(sljit_uw addr
, sljit_sw new_constant
, sljit_sw executable_offset
)
2832 inline_set_const(addr
, executable_offset
, new_constant
, 1);