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 if (diff
<= 0x7fffff && diff
>= -0x800000) {
471 /* Turn to branch. */
473 inst
[0] = (mov_pc
& COND_MASK
) | (B
- CONDITIONAL
) | (diff
& 0xffffff);
475 inst
= (sljit_uw
*)SLJIT_ADD_EXEC_OFFSET(inst
, executable_offset
);
476 SLJIT_CACHE_FLUSH(inst
, inst
+ 1);
479 inst
[0] = (mov_pc
& COND_MASK
) | (BL
- CONDITIONAL
) | (diff
& 0xffffff);
482 inst
= (sljit_uw
*)SLJIT_ADD_EXEC_OFFSET(inst
, executable_offset
);
483 SLJIT_CACHE_FLUSH(inst
, inst
+ 2);
487 /* Get the position of the constant. */
488 if (mov_pc
& (1 << 23))
489 ptr
= inst
+ ((mov_pc
& 0xfff) >> 2) + 2;
493 if (*inst
!= mov_pc
) {
497 inst
= (sljit_uw
*)SLJIT_ADD_EXEC_OFFSET(inst
, executable_offset
);
498 SLJIT_CACHE_FLUSH(inst
, inst
+ 1);
501 inst
[1] = BLX
| RM(TMP_REG1
);
503 inst
= (sljit_uw
*)SLJIT_ADD_EXEC_OFFSET(inst
, executable_offset
);
504 SLJIT_CACHE_FLUSH(inst
, inst
+ 2);
511 sljit_uw
*inst
= (sljit_uw
*)jump_ptr
;
512 SLJIT_ASSERT((inst
[0] & 0xfff00000) == MOVW
&& (inst
[1] & 0xfff00000) == MOVT
);
513 inst
[0] = MOVW
| (inst
[0] & 0xf000) | ((new_addr
<< 4) & 0xf0000) | (new_addr
& 0xfff);
514 inst
[1] = MOVT
| (inst
[1] & 0xf000) | ((new_addr
>> 12) & 0xf0000) | ((new_addr
>> 16) & 0xfff);
516 inst
= (sljit_uw
*)SLJIT_ADD_EXEC_OFFSET(inst
, executable_offset
);
517 SLJIT_CACHE_FLUSH(inst
, inst
+ 2);
522 static sljit_uw
get_imm(sljit_uw imm
);
524 static SLJIT_INLINE
void inline_set_const(sljit_uw addr
, sljit_sw executable_offset
, sljit_sw new_constant
, sljit_s32 flush_cache
)
526 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
527 sljit_uw
*ptr
= (sljit_uw
*)addr
;
528 sljit_uw
*inst
= (sljit_uw
*)ptr
[0];
529 sljit_uw ldr_literal
= ptr
[1];
532 src2
= get_imm(new_constant
);
534 *inst
= 0xe3a00000 | (ldr_literal
& 0xf000) | src2
;
536 inst
= (sljit_uw
*)SLJIT_ADD_EXEC_OFFSET(inst
, executable_offset
);
537 SLJIT_CACHE_FLUSH(inst
, inst
+ 1);
542 src2
= get_imm(~new_constant
);
544 *inst
= 0xe3e00000 | (ldr_literal
& 0xf000) | src2
;
546 inst
= (sljit_uw
*)SLJIT_ADD_EXEC_OFFSET(inst
, executable_offset
);
547 SLJIT_CACHE_FLUSH(inst
, inst
+ 1);
552 if (ldr_literal
& (1 << 23))
553 ptr
= inst
+ ((ldr_literal
& 0xfff) >> 2) + 2;
557 if (*inst
!= ldr_literal
) {
560 inst
= (sljit_uw
*)SLJIT_ADD_EXEC_OFFSET(inst
, executable_offset
);
561 SLJIT_CACHE_FLUSH(inst
, inst
+ 1);
566 sljit_uw
*inst
= (sljit_uw
*)addr
;
567 SLJIT_ASSERT((inst
[0] & 0xfff00000) == MOVW
&& (inst
[1] & 0xfff00000) == MOVT
);
568 inst
[0] = MOVW
| (inst
[0] & 0xf000) | ((new_constant
<< 4) & 0xf0000) | (new_constant
& 0xfff);
569 inst
[1] = MOVT
| (inst
[1] & 0xf000) | ((new_constant
>> 12) & 0xf0000) | ((new_constant
>> 16) & 0xfff);
571 inst
= (sljit_uw
*)SLJIT_ADD_EXEC_OFFSET(inst
, executable_offset
);
572 SLJIT_CACHE_FLUSH(inst
, inst
+ 2);
577 SLJIT_API_FUNC_ATTRIBUTE
void* sljit_generate_code(struct sljit_compiler
*compiler
)
579 struct sljit_memory_fragment
*buf
;
587 sljit_sw executable_offset
;
589 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
591 sljit_uw cpool_skip_alignment
;
592 sljit_uw cpool_current_index
;
593 sljit_uw
*cpool_start_address
;
594 sljit_uw
*last_pc_patch
;
595 struct future_patch
*first_patch
;
598 struct sljit_label
*label
;
599 struct sljit_jump
*jump
;
600 struct sljit_const
*const_
;
601 struct sljit_put_label
*put_label
;
604 CHECK_PTR(check_sljit_generate_code(compiler
));
605 reverse_buf(compiler
);
607 /* Second code generation pass. */
608 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
609 size
= compiler
->size
+ (compiler
->patches
<< 1);
610 if (compiler
->cpool_fill
> 0)
611 size
+= compiler
->cpool_fill
+ CONST_POOL_ALIGNMENT
- 1;
613 size
= compiler
->size
;
615 code
= (sljit_uw
*)SLJIT_MALLOC_EXEC(size
* sizeof(sljit_uw
));
616 PTR_FAIL_WITH_EXEC_IF(code
);
619 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
621 cpool_skip_alignment
= 0;
622 cpool_current_index
= 0;
623 cpool_start_address
= NULL
;
625 last_pc_patch
= code
;
631 executable_offset
= SLJIT_EXEC_OFFSET(code
);
633 label
= compiler
->labels
;
634 jump
= compiler
->jumps
;
635 const_
= compiler
->consts
;
636 put_label
= compiler
->put_labels
;
638 if (label
&& label
->size
== 0) {
639 label
->addr
= (sljit_uw
)SLJIT_ADD_EXEC_OFFSET(code
, executable_offset
);
644 buf_ptr
= (sljit_uw
*)buf
->memory
;
645 buf_end
= buf_ptr
+ (buf
->used_size
>> 2);
648 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
649 if (cpool_size
> 0) {
650 if (cpool_skip_alignment
> 0) {
652 cpool_skip_alignment
--;
655 if (SLJIT_UNLIKELY(resolve_const_pool_index(compiler
, &first_patch
, cpool_current_index
, cpool_start_address
, buf_ptr
))) {
656 SLJIT_FREE_EXEC(code
);
657 compiler
->error
= SLJIT_ERR_ALLOC_FAILED
;
661 if (++cpool_current_index
>= cpool_size
) {
662 SLJIT_ASSERT(!first_patch
);
664 if (label
&& label
->size
== word_count
) {
665 /* Points after the current instruction. */
666 label
->addr
= (sljit_uw
)SLJIT_ADD_EXEC_OFFSET(code_ptr
, executable_offset
);
667 label
->size
= code_ptr
- code
;
673 else if ((*buf_ptr
& 0xff000000) != PUSH_POOL
) {
675 *code_ptr
= *buf_ptr
++;
676 if (next_addr
== word_count
) {
677 SLJIT_ASSERT(!label
|| label
->size
>= word_count
);
678 SLJIT_ASSERT(!jump
|| jump
->addr
>= word_count
);
679 SLJIT_ASSERT(!const_
|| const_
->addr
>= word_count
);
680 SLJIT_ASSERT(!put_label
|| put_label
->addr
>= word_count
);
682 /* These structures are ordered by their address. */
683 if (jump
&& jump
->addr
== word_count
) {
684 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
685 if (detect_jump_type(jump
, code_ptr
, code
, executable_offset
))
687 jump
->addr
= (sljit_uw
)code_ptr
;
689 jump
->addr
= (sljit_uw
)(code_ptr
- 2);
690 if (detect_jump_type(jump
, code_ptr
, code
, executable_offset
))
695 if (label
&& label
->size
== word_count
) {
696 /* code_ptr can be affected above. */
697 label
->addr
= (sljit_uw
)SLJIT_ADD_EXEC_OFFSET(code_ptr
+ 1, executable_offset
);
698 label
->size
= (code_ptr
+ 1) - code
;
701 if (const_
&& const_
->addr
== word_count
) {
702 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
703 const_
->addr
= (sljit_uw
)code_ptr
;
705 const_
->addr
= (sljit_uw
)(code_ptr
- 1);
707 const_
= const_
->next
;
709 if (put_label
&& put_label
->addr
== word_count
) {
710 SLJIT_ASSERT(put_label
->label
);
711 put_label
->addr
= (sljit_uw
)code_ptr
;
712 put_label
= put_label
->next
;
714 next_addr
= compute_next_addr(label
, jump
, const_
, put_label
);
717 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
720 /* Fortunately, no need to shift. */
721 cpool_size
= *buf_ptr
++ & ~PUSH_POOL
;
722 SLJIT_ASSERT(cpool_size
> 0);
723 cpool_start_address
= ALIGN_INSTRUCTION(code_ptr
+ 1);
724 cpool_current_index
= patch_pc_relative_loads(last_pc_patch
, code_ptr
, cpool_start_address
, cpool_size
);
725 if (cpool_current_index
> 0) {
726 /* Unconditional branch. */
727 *code_ptr
= B
| (((cpool_start_address
- code_ptr
) + cpool_current_index
- 2) & ~PUSH_POOL
);
728 code_ptr
= cpool_start_address
+ cpool_current_index
;
730 cpool_skip_alignment
= CONST_POOL_ALIGNMENT
- 1;
731 cpool_current_index
= 0;
732 last_pc_patch
= code_ptr
;
735 } while (buf_ptr
< buf_end
);
739 SLJIT_ASSERT(!label
);
741 SLJIT_ASSERT(!const_
);
742 SLJIT_ASSERT(!put_label
);
744 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
745 SLJIT_ASSERT(cpool_size
== 0);
746 if (compiler
->cpool_fill
> 0) {
747 cpool_start_address
= ALIGN_INSTRUCTION(code_ptr
);
748 cpool_current_index
= patch_pc_relative_loads(last_pc_patch
, code_ptr
, cpool_start_address
, compiler
->cpool_fill
);
749 if (cpool_current_index
> 0)
750 code_ptr
= cpool_start_address
+ cpool_current_index
;
752 buf_ptr
= compiler
->cpool
;
753 buf_end
= buf_ptr
+ compiler
->cpool_fill
;
754 cpool_current_index
= 0;
755 while (buf_ptr
< buf_end
) {
756 if (SLJIT_UNLIKELY(resolve_const_pool_index(compiler
, &first_patch
, cpool_current_index
, cpool_start_address
, buf_ptr
))) {
757 SLJIT_FREE_EXEC(code
);
758 compiler
->error
= SLJIT_ERR_ALLOC_FAILED
;
762 cpool_current_index
++;
764 SLJIT_ASSERT(!first_patch
);
768 jump
= compiler
->jumps
;
770 buf_ptr
= (sljit_uw
*)jump
->addr
;
772 if (jump
->flags
& PATCH_B
) {
773 addr
= (sljit_sw
)SLJIT_ADD_EXEC_OFFSET(buf_ptr
+ 2, executable_offset
);
774 if (!(jump
->flags
& JUMP_ADDR
)) {
775 SLJIT_ASSERT(jump
->flags
& JUMP_LABEL
);
776 SLJIT_ASSERT(((sljit_sw
)jump
->u
.label
->addr
- addr
) <= 0x01ffffff && ((sljit_sw
)jump
->u
.label
->addr
- addr
) >= -0x02000000);
777 *buf_ptr
|= (((sljit_sw
)jump
->u
.label
->addr
- addr
) >> 2) & 0x00ffffff;
780 SLJIT_ASSERT(((sljit_sw
)jump
->u
.target
- addr
) <= 0x01ffffff && ((sljit_sw
)jump
->u
.target
- addr
) >= -0x02000000);
781 *buf_ptr
|= (((sljit_sw
)jump
->u
.target
- addr
) >> 2) & 0x00ffffff;
784 else if (jump
->flags
& SLJIT_REWRITABLE_JUMP
) {
785 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
786 jump
->addr
= (sljit_uw
)code_ptr
;
787 code_ptr
[0] = (sljit_uw
)buf_ptr
;
788 code_ptr
[1] = *buf_ptr
;
789 inline_set_jump_addr((sljit_uw
)code_ptr
, executable_offset
, (jump
->flags
& JUMP_LABEL
) ? jump
->u
.label
->addr
: jump
->u
.target
, 0);
792 inline_set_jump_addr((sljit_uw
)buf_ptr
, executable_offset
, (jump
->flags
& JUMP_LABEL
) ? jump
->u
.label
->addr
: jump
->u
.target
, 0);
796 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
797 if (jump
->flags
& IS_BL
)
799 if (*buf_ptr
& (1 << 23))
800 buf_ptr
+= ((*buf_ptr
& 0xfff) >> 2) + 2;
803 *buf_ptr
= (jump
->flags
& JUMP_LABEL
) ? jump
->u
.label
->addr
: jump
->u
.target
;
805 inline_set_jump_addr((sljit_uw
)buf_ptr
, executable_offset
, (jump
->flags
& JUMP_LABEL
) ? jump
->u
.label
->addr
: jump
->u
.target
, 0);
811 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
812 const_
= compiler
->consts
;
814 buf_ptr
= (sljit_uw
*)const_
->addr
;
815 const_
->addr
= (sljit_uw
)code_ptr
;
817 code_ptr
[0] = (sljit_uw
)buf_ptr
;
818 code_ptr
[1] = *buf_ptr
;
819 if (*buf_ptr
& (1 << 23))
820 buf_ptr
+= ((*buf_ptr
& 0xfff) >> 2) + 2;
823 /* Set the value again (can be a simple constant). */
824 inline_set_const((sljit_uw
)code_ptr
, executable_offset
, *buf_ptr
, 0);
827 const_
= const_
->next
;
831 put_label
= compiler
->put_labels
;
833 addr
= put_label
->label
->addr
;
834 buf_ptr
= (sljit_uw
*)put_label
->addr
;
836 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
837 SLJIT_ASSERT((buf_ptr
[0] & 0xffff0000) == 0xe59f0000);
838 buf_ptr
[((buf_ptr
[0] & 0xfff) >> 2) + 2] = addr
;
840 SLJIT_ASSERT((buf_ptr
[-1] & 0xfff00000) == MOVW
&& (buf_ptr
[0] & 0xfff00000) == MOVT
);
841 buf_ptr
[-1] |= ((addr
<< 4) & 0xf0000) | (addr
& 0xfff);
842 buf_ptr
[0] |= ((addr
>> 12) & 0xf0000) | ((addr
>> 16) & 0xfff);
844 put_label
= put_label
->next
;
847 SLJIT_ASSERT(code_ptr
- code
<= (sljit_s32
)size
);
849 compiler
->error
= SLJIT_ERR_COMPILED
;
850 compiler
->executable_offset
= executable_offset
;
851 compiler
->executable_size
= (code_ptr
- code
) * sizeof(sljit_uw
);
853 code
= (sljit_uw
*)SLJIT_ADD_EXEC_OFFSET(code
, executable_offset
);
854 code_ptr
= (sljit_uw
*)SLJIT_ADD_EXEC_OFFSET(code_ptr
, executable_offset
);
856 SLJIT_CACHE_FLUSH(code
, code_ptr
);
860 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_has_cpu_feature(sljit_s32 feature_type
)
862 switch (feature_type
) {
864 #ifdef SLJIT_IS_FPU_AVAILABLE
865 return SLJIT_IS_FPU_AVAILABLE
;
867 /* Available by default. */
880 /* --------------------------------------------------------------------- */
882 /* --------------------------------------------------------------------- */
884 /* Creates an index in data_transfer_insts array. */
885 #define WORD_SIZE 0x00
886 #define BYTE_SIZE 0x01
887 #define HALF_SIZE 0x02
890 #define LOAD_DATA 0x08
892 /* Flag bits for emit_op. */
893 #define ALLOW_IMM 0x10
894 #define ALLOW_INV_IMM 0x20
895 #define ALLOW_ANY_IMM (ALLOW_IMM | ALLOW_INV_IMM)
897 /* s/l - store/load (1 bit)
898 u/s - signed/unsigned (1 bit)
899 w/b/h/N - word/byte/half/NOT allowed (2 bit)
900 Storing signed and unsigned values are the same operations. */
902 static const sljit_uw data_transfer_insts
[16] = {
903 /* s u w */ 0xe5000000 /* str */,
904 /* s u b */ 0xe5400000 /* strb */,
905 /* s u h */ 0xe10000b0 /* strh */,
906 /* s u N */ 0x00000000 /* not allowed */,
907 /* s s w */ 0xe5000000 /* str */,
908 /* s s b */ 0xe5400000 /* strb */,
909 /* s s h */ 0xe10000b0 /* strh */,
910 /* s s N */ 0x00000000 /* not allowed */,
912 /* l u w */ 0xe5100000 /* ldr */,
913 /* l u b */ 0xe5500000 /* ldrb */,
914 /* l u h */ 0xe11000b0 /* ldrh */,
915 /* l u p */ 0xf5500000 /* preload */,
916 /* l s w */ 0xe5100000 /* ldr */,
917 /* l s b */ 0xe11000d0 /* ldrsb */,
918 /* l s h */ 0xe11000f0 /* ldrsh */,
919 /* l s N */ 0x00000000 /* not allowed */,
922 #define EMIT_DATA_TRANSFER(type, add, target_reg, base_reg, arg) \
923 (data_transfer_insts[(type) & 0xf] | ((add) << 23) | RD(target_reg) | RN(base_reg) | (arg))
925 /* Normal ldr/str instruction.
926 Type2: ldrsb, ldrh, ldrsh */
927 #define IS_TYPE1_TRANSFER(type) \
928 (data_transfer_insts[(type) & 0xf] & 0x04000000)
929 #define TYPE2_TRANSFER_IMM(imm) \
930 (((imm) & 0xf) | (((imm) & 0xf0) << 4) | (1 << 22))
932 static sljit_s32
emit_op(struct sljit_compiler
*compiler
, sljit_s32 op
, sljit_s32 inp_flags
,
933 sljit_s32 dst
, sljit_sw dstw
,
934 sljit_s32 src1
, sljit_sw src1w
,
935 sljit_s32 src2
, sljit_sw src2w
);
937 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_enter(struct sljit_compiler
*compiler
,
938 sljit_s32 options
, sljit_s32 arg_types
, sljit_s32 scratches
, sljit_s32 saveds
,
939 sljit_s32 fscratches
, sljit_s32 fsaveds
, sljit_s32 local_size
)
941 sljit_s32 args
, size
, i
, tmp
;
945 CHECK(check_sljit_emit_enter(compiler
, options
, arg_types
, scratches
, saveds
, fscratches
, fsaveds
, local_size
));
946 set_emit_enter(compiler
, options
, arg_types
, scratches
, saveds
, fscratches
, fsaveds
, local_size
);
948 /* Push saved registers, temporary registers
949 stmdb sp!, {..., lr} */
950 push
= PUSH
| (1 << 14);
952 tmp
= saveds
< SLJIT_NUMBER_OF_SAVED_REGISTERS
? (SLJIT_S0
+ 1 - saveds
) : SLJIT_FIRST_SAVED_REG
;
953 for (i
= SLJIT_S0
; i
>= tmp
; i
--)
954 push
|= 1 << reg_map
[i
];
956 for (i
= scratches
; i
>= SLJIT_FIRST_SAVED_REG
; i
--)
957 push
|= 1 << reg_map
[i
];
959 FAIL_IF(push_inst(compiler
, push
));
961 /* Stack must be aligned to 8 bytes: */
962 size
= GET_SAVED_REGISTERS_SIZE(scratches
, saveds
, 1);
963 local_size
= ((size
+ local_size
+ 7) & ~7) - size
;
964 compiler
->local_size
= local_size
;
966 FAIL_IF(emit_op(compiler
, SLJIT_SUB
, ALLOW_IMM
, SLJIT_SP
, 0, SLJIT_SP
, 0, SLJIT_IMM
, local_size
));
968 args
= get_arg_count(arg_types
);
971 FAIL_IF(push_inst(compiler
, MOV
| RD(SLJIT_S0
) | RM(SLJIT_R0
)));
973 FAIL_IF(push_inst(compiler
, MOV
| RD(SLJIT_S1
) | RM(SLJIT_R1
)));
975 FAIL_IF(push_inst(compiler
, MOV
| RD(SLJIT_S2
) | RM(SLJIT_R2
)));
977 return SLJIT_SUCCESS
;
980 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_set_context(struct sljit_compiler
*compiler
,
981 sljit_s32 options
, sljit_s32 arg_types
, sljit_s32 scratches
, sljit_s32 saveds
,
982 sljit_s32 fscratches
, sljit_s32 fsaveds
, sljit_s32 local_size
)
987 CHECK(check_sljit_set_context(compiler
, options
, arg_types
, scratches
, saveds
, fscratches
, fsaveds
, local_size
));
988 set_set_context(compiler
, options
, arg_types
, scratches
, saveds
, fscratches
, fsaveds
, local_size
);
990 size
= GET_SAVED_REGISTERS_SIZE(scratches
, saveds
, 1);
991 compiler
->local_size
= ((size
+ local_size
+ 7) & ~7) - size
;
992 return SLJIT_SUCCESS
;
995 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_return(struct sljit_compiler
*compiler
, sljit_s32 op
, sljit_s32 src
, sljit_sw srcw
)
1001 CHECK(check_sljit_emit_return(compiler
, op
, src
, srcw
));
1003 FAIL_IF(emit_mov_before_return(compiler
, op
, src
, srcw
));
1005 if (compiler
->local_size
> 0)
1006 FAIL_IF(emit_op(compiler
, SLJIT_ADD
, ALLOW_IMM
, SLJIT_SP
, 0, SLJIT_SP
, 0, SLJIT_IMM
, compiler
->local_size
));
1008 /* Push saved registers, temporary registers
1009 ldmia sp!, {..., pc} */
1010 pop
= POP
| (1 << 15);
1012 tmp
= compiler
->saveds
< SLJIT_NUMBER_OF_SAVED_REGISTERS
? (SLJIT_S0
+ 1 - compiler
->saveds
) : SLJIT_FIRST_SAVED_REG
;
1013 for (i
= SLJIT_S0
; i
>= tmp
; i
--)
1014 pop
|= 1 << reg_map
[i
];
1016 for (i
= compiler
->scratches
; i
>= SLJIT_FIRST_SAVED_REG
; i
--)
1017 pop
|= 1 << reg_map
[i
];
1019 return push_inst(compiler
, pop
);
1022 /* --------------------------------------------------------------------- */
1024 /* --------------------------------------------------------------------- */
1027 /* Arguments are swapped. */
1028 #define ARGS_SWAPPED 0x01
1029 /* Inverted immediate. */
1030 #define INV_IMM 0x02
1031 /* Source and destination is register. */
1032 #define MOVE_REG_CONV 0x04
1033 /* Unused return value. */
1034 #define UNUSED_RETURN 0x08
1035 /* SET_FLAGS must be (1 << 20) as it is also the value of S bit (can be used for optimization). */
1036 #define SET_FLAGS (1 << 20)
1039 src2: reg or imm (if allowed)
1040 SRC2_IMM must be (1 << 25) as it is also the value of I bit (can be used for optimization). */
1041 #define SRC2_IMM (1 << 25)
1043 #define EMIT_SHIFT_INS_AND_RETURN(opcode) \
1044 SLJIT_ASSERT(!(flags & INV_IMM) && !(src2 & SRC2_IMM)); \
1045 if (compiler->shift_imm != 0x20) { \
1046 SLJIT_ASSERT(src1 == TMP_REG1); \
1047 SLJIT_ASSERT(!(flags & ARGS_SWAPPED)); \
1049 if (compiler->shift_imm != 0) \
1050 return push_inst(compiler, MOV | (flags & SET_FLAGS) | \
1051 RD(dst) | (compiler->shift_imm << 7) | (opcode << 5) | RM(src2)); \
1052 return push_inst(compiler, MOV | (flags & SET_FLAGS) | RD(dst) | RM(src2)); \
1054 return push_inst(compiler, MOV | (flags & SET_FLAGS) | RD(dst) | \
1055 (reg_map[(flags & ARGS_SWAPPED) ? src1 : src2] << 8) | (opcode << 5) | 0x10 | RM((flags & ARGS_SWAPPED) ? src2 : src1));
1057 static SLJIT_INLINE sljit_s32
emit_single_op(struct sljit_compiler
*compiler
, sljit_s32 op
, sljit_s32 flags
,
1058 sljit_s32 dst
, sljit_s32 src1
, sljit_s32 src2
)
1060 switch (GET_OPCODE(op
)) {
1062 SLJIT_ASSERT(src1
== TMP_REG1
&& !(flags
& ARGS_SWAPPED
));
1064 if (src2
& SRC2_IMM
) {
1065 return push_inst(compiler
, ((flags
& INV_IMM
) ? MVN
: MOV
) | RD(dst
) | src2
);
1067 return push_inst(compiler
, MOV
| RD(dst
) | RM(src2
));
1069 return SLJIT_SUCCESS
;
1073 SLJIT_ASSERT(src1
== TMP_REG1
&& !(flags
& ARGS_SWAPPED
));
1074 if (flags
& MOVE_REG_CONV
) {
1075 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
1076 if (op
== SLJIT_MOV_U8
)
1077 return push_inst(compiler
, AND
| RD(dst
) | RN(src2
) | SRC2_IMM
| 0xff);
1078 FAIL_IF(push_inst(compiler
, MOV
| RD(dst
) | (24 << 7) | RM(src2
)));
1079 return push_inst(compiler
, MOV
| RD(dst
) | (24 << 7) | (op
== SLJIT_MOV_U8
? 0x20 : 0x40) | RM(dst
));
1081 return push_inst(compiler
, (op
== SLJIT_MOV_U8
? UXTB
: SXTB
) | RD(dst
) | RM(src2
));
1084 else if (dst
!= src2
) {
1085 SLJIT_ASSERT(src2
& SRC2_IMM
);
1086 return push_inst(compiler
, ((flags
& INV_IMM
) ? MVN
: MOV
) | RD(dst
) | src2
);
1088 return SLJIT_SUCCESS
;
1092 SLJIT_ASSERT(src1
== TMP_REG1
&& !(flags
& ARGS_SWAPPED
));
1093 if (flags
& MOVE_REG_CONV
) {
1094 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
1095 FAIL_IF(push_inst(compiler
, MOV
| RD(dst
) | (16 << 7) | RM(src2
)));
1096 return push_inst(compiler
, MOV
| RD(dst
) | (16 << 7) | (op
== SLJIT_MOV_U16
? 0x20 : 0x40) | RM(dst
));
1098 return push_inst(compiler
, (op
== SLJIT_MOV_U16
? UXTH
: SXTH
) | RD(dst
) | RM(src2
));
1101 else if (dst
!= src2
) {
1102 SLJIT_ASSERT(src2
& SRC2_IMM
);
1103 return push_inst(compiler
, ((flags
& INV_IMM
) ? MVN
: MOV
) | RD(dst
) | src2
);
1105 return SLJIT_SUCCESS
;
1108 if (src2
& SRC2_IMM
) {
1109 return push_inst(compiler
, ((flags
& INV_IMM
) ? MOV
: MVN
) | (flags
& SET_FLAGS
) | RD(dst
) | src2
);
1111 return push_inst(compiler
, MVN
| (flags
& SET_FLAGS
) | RD(dst
) | RM(src2
));
1114 SLJIT_ASSERT(!(flags
& INV_IMM
));
1115 SLJIT_ASSERT(!(src2
& SRC2_IMM
));
1116 FAIL_IF(push_inst(compiler
, CLZ
| RD(dst
) | RM(src2
)));
1117 return SLJIT_SUCCESS
;
1120 SLJIT_ASSERT(!(flags
& INV_IMM
));
1121 if ((flags
& (UNUSED_RETURN
| SET_FLAGS
)) == (UNUSED_RETURN
| SET_FLAGS
) && !(flags
& ARGS_SWAPPED
))
1122 return push_inst(compiler
, CMN
| SET_FLAGS
| RN(src1
) | ((src2
& SRC2_IMM
) ? src2
: RM(src2
)));
1123 return push_inst(compiler
, ADD
| (flags
& SET_FLAGS
) | RD(dst
) | RN(src1
) | ((src2
& SRC2_IMM
) ? src2
: RM(src2
)));
1126 SLJIT_ASSERT(!(flags
& INV_IMM
));
1127 return push_inst(compiler
, ADC
| (flags
& SET_FLAGS
) | RD(dst
) | RN(src1
) | ((src2
& SRC2_IMM
) ? src2
: RM(src2
)));
1130 SLJIT_ASSERT(!(flags
& INV_IMM
));
1131 if ((flags
& (UNUSED_RETURN
| SET_FLAGS
)) == (UNUSED_RETURN
| SET_FLAGS
) && !(flags
& ARGS_SWAPPED
))
1132 return push_inst(compiler
, CMP
| SET_FLAGS
| RN(src1
) | ((src2
& SRC2_IMM
) ? src2
: RM(src2
)));
1133 return push_inst(compiler
, (!(flags
& ARGS_SWAPPED
) ? SUB
: RSB
) | (flags
& SET_FLAGS
)
1134 | RD(dst
) | RN(src1
) | ((src2
& SRC2_IMM
) ? src2
: RM(src2
)));
1137 SLJIT_ASSERT(!(flags
& INV_IMM
));
1138 return push_inst(compiler
, (!(flags
& ARGS_SWAPPED
) ? SBC
: RSC
) | (flags
& SET_FLAGS
)
1139 | RD(dst
) | RN(src1
) | ((src2
& SRC2_IMM
) ? src2
: RM(src2
)));
1142 SLJIT_ASSERT(!(flags
& INV_IMM
));
1143 SLJIT_ASSERT(!(src2
& SRC2_IMM
));
1146 return push_inst(compiler
, MUL
| (reg_map
[dst
] << 16) | (reg_map
[src2
] << 8) | reg_map
[src1
]);
1148 FAIL_IF(push_inst(compiler
, SMULL
| (reg_map
[TMP_REG1
] << 16) | (reg_map
[dst
] << 12) | (reg_map
[src2
] << 8) | reg_map
[src1
]));
1150 /* cmp TMP_REG1, dst asr #31. */
1151 return push_inst(compiler
, CMP
| SET_FLAGS
| RN(TMP_REG1
) | RM(dst
) | 0xfc0);
1154 return push_inst(compiler
, (!(flags
& INV_IMM
) ? AND
: BIC
) | (flags
& SET_FLAGS
)
1155 | RD(dst
) | RN(src1
) | ((src2
& SRC2_IMM
) ? src2
: RM(src2
)));
1158 SLJIT_ASSERT(!(flags
& INV_IMM
));
1159 return push_inst(compiler
, ORR
| (flags
& SET_FLAGS
) | RD(dst
) | RN(src1
) | ((src2
& SRC2_IMM
) ? src2
: RM(src2
)));
1162 SLJIT_ASSERT(!(flags
& INV_IMM
));
1163 return push_inst(compiler
, EOR
| (flags
& SET_FLAGS
) | RD(dst
) | RN(src1
) | ((src2
& SRC2_IMM
) ? src2
: RM(src2
)));
1166 EMIT_SHIFT_INS_AND_RETURN(0);
1169 EMIT_SHIFT_INS_AND_RETURN(1);
1172 EMIT_SHIFT_INS_AND_RETURN(2);
1175 SLJIT_UNREACHABLE();
1176 return SLJIT_SUCCESS
;
1179 #undef EMIT_SHIFT_INS_AND_RETURN
1181 /* Tests whether the immediate can be stored in the 12 bit imm field.
1182 Returns with 0 if not possible. */
1183 static sljit_uw
get_imm(sljit_uw imm
)
1188 return SRC2_IMM
| imm
;
1190 if (!(imm
& 0xff000000)) {
1195 imm
= (imm
<< 24) | (imm
>> 8);
1199 if (!(imm
& 0xff000000)) {
1204 if (!(imm
& 0xf0000000)) {
1209 if (!(imm
& 0xc0000000)) {
1214 if (!(imm
& 0x00ffffff))
1215 return SRC2_IMM
| (imm
>> 24) | (rol
<< 8);
1220 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
1221 static sljit_s32
generate_int(struct sljit_compiler
*compiler
, sljit_s32 reg
, sljit_uw imm
, sljit_s32 positive
)
1228 /* Step1: Search a zero byte (8 continous zero bit). */
1232 if (!(imm
& mask
)) {
1233 /* Rol imm by rol. */
1234 imm
= (imm
<< rol
) | (imm
>> (32 - rol
));
1235 /* Calculate arm rol. */
1236 rol
= 4 + (rol
>> 1);
1243 imm
= (imm
<< 8) | (imm
>> 24);
1247 if (!(imm
& mask
)) {
1248 /* Rol imm by rol. */
1249 imm
= (imm
<< rol
) | (imm
>> (32 - rol
));
1250 /* Calculate arm rol. */
1251 rol
= (rol
>> 1) - 8;
1263 /* The low 8 bit must be zero. */
1264 SLJIT_ASSERT(!(imm
& 0xff));
1266 if (!(imm
& 0xff000000)) {
1267 imm1
= SRC2_IMM
| ((imm
>> 16) & 0xff) | (((rol
+ 4) & 0xf) << 8);
1268 imm2
= SRC2_IMM
| ((imm
>> 8) & 0xff) | (((rol
+ 8) & 0xf) << 8);
1270 else if (imm
& 0xc0000000) {
1271 imm1
= SRC2_IMM
| ((imm
>> 24) & 0xff) | ((rol
& 0xf) << 8);
1275 if (!(imm
& 0xff000000)) {
1280 if (!(imm
& 0xf0000000)) {
1285 if (!(imm
& 0xc0000000)) {
1290 if (!(imm
& 0x00ffffff))
1291 imm2
= SRC2_IMM
| (imm
>> 24) | ((rol
& 0xf) << 8);
1296 if (!(imm
& 0xf0000000)) {
1301 if (!(imm
& 0xc0000000)) {
1306 imm1
= SRC2_IMM
| ((imm
>> 24) & 0xff) | ((rol
& 0xf) << 8);
1310 if (!(imm
& 0xf0000000)) {
1315 if (!(imm
& 0xc0000000)) {
1320 if (!(imm
& 0x00ffffff))
1321 imm2
= SRC2_IMM
| (imm
>> 24) | ((rol
& 0xf) << 8);
1326 FAIL_IF(push_inst(compiler
, (positive
? MOV
: MVN
) | RD(reg
) | imm1
));
1327 FAIL_IF(push_inst(compiler
, (positive
? ORR
: BIC
) | RD(reg
) | RN(reg
) | imm2
));
1332 static sljit_s32
load_immediate(struct sljit_compiler
*compiler
, sljit_s32 reg
, sljit_uw imm
)
1336 #if (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
1337 if (!(imm
& ~0xffff))
1338 return push_inst(compiler
, MOVW
| RD(reg
) | ((imm
<< 4) & 0xf0000) | (imm
& 0xfff));
1341 /* Create imm by 1 inst. */
1344 return push_inst(compiler
, MOV
| RD(reg
) | tmp
);
1346 tmp
= get_imm(~imm
);
1348 return push_inst(compiler
, MVN
| RD(reg
) | tmp
);
1350 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
1351 /* Create imm by 2 inst. */
1352 FAIL_IF(generate_int(compiler
, reg
, imm
, 1));
1353 FAIL_IF(generate_int(compiler
, reg
, ~imm
, 0));
1356 return push_inst_with_literal(compiler
, EMIT_DATA_TRANSFER(WORD_SIZE
| LOAD_DATA
, 1, reg
, TMP_PC
, 0), imm
);
1358 FAIL_IF(push_inst(compiler
, MOVW
| RD(reg
) | ((imm
<< 4) & 0xf0000) | (imm
& 0xfff)));
1360 return SLJIT_SUCCESS
;
1361 return push_inst(compiler
, MOVT
| RD(reg
) | ((imm
>> 12) & 0xf0000) | ((imm
>> 16) & 0xfff));
1365 static SLJIT_INLINE sljit_s32
emit_op_mem(struct sljit_compiler
*compiler
, sljit_s32 flags
, sljit_s32 reg
,
1366 sljit_s32 arg
, sljit_sw argw
, sljit_s32 tmp_reg
)
1368 sljit_uw imm
, offset_reg
;
1369 sljit_uw is_type1_transfer
= IS_TYPE1_TRANSFER(flags
);
1371 SLJIT_ASSERT (arg
& SLJIT_MEM
);
1372 SLJIT_ASSERT((arg
& REG_MASK
) != tmp_reg
);
1374 if ((arg
& REG_MASK
) == SLJIT_UNUSED
) {
1375 if (is_type1_transfer
) {
1376 FAIL_IF(load_immediate(compiler
, tmp_reg
, argw
& ~0xfff));
1380 FAIL_IF(load_immediate(compiler
, tmp_reg
, argw
& ~0xff));
1384 return push_inst(compiler
, EMIT_DATA_TRANSFER(flags
, 1, reg
, tmp_reg
,
1385 is_type1_transfer
? argw
: TYPE2_TRANSFER_IMM(argw
)));
1388 if (arg
& OFFS_REG_MASK
) {
1389 offset_reg
= OFFS_REG(arg
);
1393 if (argw
!= 0 && !is_type1_transfer
) {
1394 FAIL_IF(push_inst(compiler
, ADD
| RD(tmp_reg
) | RN(arg
) | RM(offset_reg
) | (argw
<< 7)));
1395 return push_inst(compiler
, EMIT_DATA_TRANSFER(flags
, 1, reg
, tmp_reg
, TYPE2_TRANSFER_IMM(0)));
1398 /* Bit 25: RM is offset. */
1399 return push_inst(compiler
, EMIT_DATA_TRANSFER(flags
, 1, reg
, arg
,
1400 RM(offset_reg
) | (is_type1_transfer
? (1 << 25) : 0) | (argw
<< 7)));
1405 if (is_type1_transfer
) {
1407 imm
= get_imm(argw
& ~0xfff);
1409 FAIL_IF(push_inst(compiler
, ADD
| RD(tmp_reg
) | RN(arg
) | imm
));
1410 argw
= argw
& 0xfff;
1414 else if (argw
< -0xfff) {
1415 imm
= get_imm(-argw
& ~0xfff);
1417 FAIL_IF(push_inst(compiler
, SUB
| RD(tmp_reg
) | RN(arg
) | imm
));
1418 argw
= -(-argw
& 0xfff);
1423 if (argw
>= 0 && argw
<= 0xfff)
1424 return push_inst(compiler
, EMIT_DATA_TRANSFER(flags
, 1, reg
, arg
, argw
));
1426 if (argw
< 0 && argw
>= -0xfff)
1427 return push_inst(compiler
, EMIT_DATA_TRANSFER(flags
, 0, reg
, arg
, -argw
));
1431 imm
= get_imm(argw
& ~0xff);
1433 FAIL_IF(push_inst(compiler
, ADD
| RD(tmp_reg
) | RN(arg
) | imm
));
1438 else if (argw
< -0xff) {
1439 imm
= get_imm(-argw
& ~0xff);
1441 FAIL_IF(push_inst(compiler
, SUB
| RD(tmp_reg
) | RN(arg
) | imm
));
1442 argw
= -(-argw
& 0xff);
1447 if (argw
>= 0 && argw
<= 0xff)
1448 return push_inst(compiler
, EMIT_DATA_TRANSFER(flags
, 1, reg
, arg
, TYPE2_TRANSFER_IMM(argw
)));
1450 if (argw
< 0 && argw
>= -0xff) {
1452 return push_inst(compiler
, EMIT_DATA_TRANSFER(flags
, 0, reg
, arg
, TYPE2_TRANSFER_IMM(argw
)));
1456 FAIL_IF(load_immediate(compiler
, tmp_reg
, argw
));
1457 return push_inst(compiler
, EMIT_DATA_TRANSFER(flags
, 1, reg
, arg
,
1458 RM(tmp_reg
) | (is_type1_transfer
? (1 << 25) : 0)));
1461 static sljit_s32
emit_op(struct sljit_compiler
*compiler
, sljit_s32 op
, sljit_s32 inp_flags
,
1462 sljit_s32 dst
, sljit_sw dstw
,
1463 sljit_s32 src1
, sljit_sw src1w
,
1464 sljit_s32 src2
, sljit_sw src2w
)
1466 /* src1 is reg or TMP_REG1
1467 src2 is reg, TMP_REG2, or imm
1468 result goes to TMP_REG2, so put result can use TMP_REG1. */
1470 /* We prefers register and simple consts. */
1474 sljit_s32 flags
= HAS_FLAGS(op
) ? SET_FLAGS
: 0;
1476 /* Destination check. */
1477 if (SLJIT_UNLIKELY(dst
== SLJIT_UNUSED
))
1478 flags
|= UNUSED_RETURN
;
1480 SLJIT_ASSERT(!(inp_flags
& ALLOW_INV_IMM
) || (inp_flags
& ALLOW_IMM
));
1485 if (!(inp_flags
& ALLOW_IMM
))
1488 if (src2
& SLJIT_IMM
) {
1489 src2_reg
= get_imm(src2w
);
1492 if (inp_flags
& ALLOW_INV_IMM
) {
1493 src2_reg
= get_imm(~src2w
);
1499 if (GET_OPCODE(op
) == SLJIT_ADD
) {
1500 src2_reg
= get_imm(-src2w
);
1502 op
= SLJIT_SUB
| GET_ALL_FLAGS(op
);
1506 if (GET_OPCODE(op
) == SLJIT_SUB
) {
1507 src2_reg
= get_imm(-src2w
);
1509 op
= SLJIT_ADD
| GET_ALL_FLAGS(op
);
1515 if (src1
& SLJIT_IMM
) {
1516 src2_reg
= get_imm(src1w
);
1518 flags
|= ARGS_SWAPPED
;
1523 if (inp_flags
& ALLOW_INV_IMM
) {
1524 src2_reg
= get_imm(~src1w
);
1526 flags
|= ARGS_SWAPPED
| INV_IMM
;
1532 if (GET_OPCODE(op
) == SLJIT_ADD
) {
1533 src2_reg
= get_imm(-src1w
);
1535 /* Note: add is commutative operation. */
1538 op
= SLJIT_SUB
| GET_ALL_FLAGS(op
);
1546 if (FAST_IS_REG(src1
))
1548 else if (src1
& SLJIT_MEM
) {
1549 FAIL_IF(emit_op_mem(compiler
, inp_flags
| LOAD_DATA
, TMP_REG1
, src1
, src1w
, TMP_REG1
));
1550 src1_reg
= TMP_REG1
;
1553 FAIL_IF(load_immediate(compiler
, TMP_REG1
, src1w
));
1554 src1_reg
= TMP_REG1
;
1558 dst_reg
= SLOW_IS_REG(dst
) ? dst
: TMP_REG2
;
1560 if (op
<= SLJIT_MOV_P
) {
1561 if (dst
& SLJIT_MEM
) {
1562 if (inp_flags
& BYTE_SIZE
)
1563 inp_flags
&= ~SIGNED
;
1565 if (FAST_IS_REG(src2
))
1566 return emit_op_mem(compiler
, inp_flags
, src2
, dst
, dstw
, TMP_REG2
);
1569 if (FAST_IS_REG(src2
) && dst_reg
!= TMP_REG2
)
1570 flags
|= MOVE_REG_CONV
;
1574 if (src2_reg
== 0) {
1575 src2_reg
= (op
<= SLJIT_MOV_P
) ? dst_reg
: TMP_REG2
;
1577 if (FAST_IS_REG(src2
))
1579 else if (src2
& SLJIT_MEM
)
1580 FAIL_IF(emit_op_mem(compiler
, inp_flags
| LOAD_DATA
, src2_reg
, src2
, src2w
, TMP_REG2
));
1582 FAIL_IF(load_immediate(compiler
, src2_reg
, src2w
));
1585 FAIL_IF(emit_single_op(compiler
, op
, flags
, dst_reg
, src1_reg
, src2_reg
));
1587 if (!(dst
& SLJIT_MEM
))
1588 return SLJIT_SUCCESS
;
1590 return emit_op_mem(compiler
, inp_flags
, dst_reg
, dst
, dstw
, TMP_REG1
);
1597 #if defined(__GNUC__)
1598 extern unsigned int __aeabi_uidivmod(unsigned int numerator
, unsigned int denominator
);
1599 extern int __aeabi_idivmod(int numerator
, int denominator
);
1601 #error "Software divmod functions are needed"
1608 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op0(struct sljit_compiler
*compiler
, sljit_s32 op
)
1610 sljit_sw saved_reg_list
[3];
1611 sljit_sw saved_reg_count
;
1614 CHECK(check_sljit_emit_op0(compiler
, op
));
1616 op
= GET_OPCODE(op
);
1618 case SLJIT_BREAKPOINT
:
1619 FAIL_IF(push_inst(compiler
, BKPT
));
1622 FAIL_IF(push_inst(compiler
, NOP
));
1626 return push_inst(compiler
, (op
== SLJIT_LMUL_UW
? UMULL
: SMULL
)
1627 | (reg_map
[SLJIT_R1
] << 16)
1628 | (reg_map
[SLJIT_R0
] << 12)
1629 | (reg_map
[SLJIT_R0
] << 8)
1630 | reg_map
[SLJIT_R1
]);
1631 case SLJIT_DIVMOD_UW
:
1632 case SLJIT_DIVMOD_SW
:
1635 SLJIT_COMPILE_ASSERT((SLJIT_DIVMOD_UW
& 0x2) == 0 && SLJIT_DIV_UW
- 0x2 == SLJIT_DIVMOD_UW
, bad_div_opcode_assignments
);
1636 SLJIT_ASSERT(reg_map
[2] == 1 && reg_map
[3] == 2 && reg_map
[4] == 3);
1638 saved_reg_count
= 0;
1639 if (compiler
->scratches
>= 4)
1640 saved_reg_list
[saved_reg_count
++] = 3;
1641 if (compiler
->scratches
>= 3)
1642 saved_reg_list
[saved_reg_count
++] = 2;
1643 if (op
>= SLJIT_DIV_UW
)
1644 saved_reg_list
[saved_reg_count
++] = 1;
1646 if (saved_reg_count
> 0) {
1647 FAIL_IF(push_inst(compiler
, 0xe52d0000 | (saved_reg_count
>= 3 ? 16 : 8)
1648 | (saved_reg_list
[0] << 12) /* str rX, [sp, #-8/-16]! */));
1649 if (saved_reg_count
>= 2) {
1650 SLJIT_ASSERT(saved_reg_list
[1] < 8);
1651 FAIL_IF(push_inst(compiler
, 0xe58d0004 | (saved_reg_list
[1] << 12) /* str rX, [sp, #4] */));
1653 if (saved_reg_count
>= 3) {
1654 SLJIT_ASSERT(saved_reg_list
[2] < 8);
1655 FAIL_IF(push_inst(compiler
, 0xe58d0008 | (saved_reg_list
[2] << 12) /* str rX, [sp, #8] */));
1659 #if defined(__GNUC__)
1660 FAIL_IF(sljit_emit_ijump(compiler
, SLJIT_FAST_CALL
, SLJIT_IMM
,
1661 ((op
| 0x2) == SLJIT_DIV_UW
? SLJIT_FUNC_OFFSET(__aeabi_uidivmod
) : SLJIT_FUNC_OFFSET(__aeabi_idivmod
))));
1663 #error "Software divmod functions are needed"
1666 if (saved_reg_count
> 0) {
1667 if (saved_reg_count
>= 3) {
1668 SLJIT_ASSERT(saved_reg_list
[2] < 8);
1669 FAIL_IF(push_inst(compiler
, 0xe59d0008 | (saved_reg_list
[2] << 12) /* ldr rX, [sp, #8] */));
1671 if (saved_reg_count
>= 2) {
1672 SLJIT_ASSERT(saved_reg_list
[1] < 8);
1673 FAIL_IF(push_inst(compiler
, 0xe59d0004 | (saved_reg_list
[1] << 12) /* ldr rX, [sp, #4] */));
1675 return push_inst(compiler
, 0xe49d0000 | (saved_reg_count
>= 3 ? 16 : 8)
1676 | (saved_reg_list
[0] << 12) /* ldr rX, [sp], #8/16 */);
1678 return SLJIT_SUCCESS
;
1681 return SLJIT_SUCCESS
;
1684 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op1(struct sljit_compiler
*compiler
, sljit_s32 op
,
1685 sljit_s32 dst
, sljit_sw dstw
,
1686 sljit_s32 src
, sljit_sw srcw
)
1689 CHECK(check_sljit_emit_op1(compiler
, op
, dst
, dstw
, src
, srcw
));
1690 ADJUST_LOCAL_OFFSET(dst
, dstw
);
1691 ADJUST_LOCAL_OFFSET(src
, srcw
);
1693 if (dst
== SLJIT_UNUSED
&& !HAS_FLAGS(op
)) {
1694 #if (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
1695 if (op
<= SLJIT_MOV_P
&& (src
& SLJIT_MEM
))
1696 return emit_op_mem(compiler
, PRELOAD
| LOAD_DATA
, TMP_PC
, src
, srcw
, TMP_REG1
);
1698 return SLJIT_SUCCESS
;
1701 switch (GET_OPCODE(op
)) {
1706 return emit_op(compiler
, SLJIT_MOV
, ALLOW_ANY_IMM
, dst
, dstw
, TMP_REG1
, 0, src
, srcw
);
1709 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
);
1712 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
);
1715 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
);
1718 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
);
1721 return emit_op(compiler
, op
, ALLOW_ANY_IMM
, dst
, dstw
, TMP_REG1
, 0, src
, srcw
);
1724 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
1725 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
1726 compiler
->skip_checks
= 1;
1728 return sljit_emit_op2(compiler
, SLJIT_SUB
| GET_ALL_FLAGS(op
), dst
, dstw
, SLJIT_IMM
, 0, src
, srcw
);
1731 return emit_op(compiler
, op
, 0, dst
, dstw
, TMP_REG1
, 0, src
, srcw
);
1734 return SLJIT_SUCCESS
;
1737 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op2(struct sljit_compiler
*compiler
, sljit_s32 op
,
1738 sljit_s32 dst
, sljit_sw dstw
,
1739 sljit_s32 src1
, sljit_sw src1w
,
1740 sljit_s32 src2
, sljit_sw src2w
)
1743 CHECK(check_sljit_emit_op2(compiler
, op
, dst
, dstw
, src1
, src1w
, src2
, src2w
));
1744 ADJUST_LOCAL_OFFSET(dst
, dstw
);
1745 ADJUST_LOCAL_OFFSET(src1
, src1w
);
1746 ADJUST_LOCAL_OFFSET(src2
, src2w
);
1748 if (dst
== SLJIT_UNUSED
&& !HAS_FLAGS(op
))
1749 return SLJIT_SUCCESS
;
1751 switch (GET_OPCODE(op
)) {
1758 return emit_op(compiler
, op
, ALLOW_IMM
, dst
, dstw
, src1
, src1w
, src2
, src2w
);
1761 return emit_op(compiler
, op
, 0, dst
, dstw
, src1
, src1w
, src2
, src2w
);
1764 return emit_op(compiler
, op
, ALLOW_ANY_IMM
, dst
, dstw
, src1
, src1w
, src2
, src2w
);
1769 if (src2
& SLJIT_IMM
) {
1770 compiler
->shift_imm
= src2w
& 0x1f;
1771 return emit_op(compiler
, op
, 0, dst
, dstw
, TMP_REG1
, 0, src1
, src1w
);
1774 compiler
->shift_imm
= 0x20;
1775 return emit_op(compiler
, op
, 0, dst
, dstw
, src1
, src1w
, src2
, src2w
);
1779 return SLJIT_SUCCESS
;
1782 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_get_register_index(sljit_s32 reg
)
1784 CHECK_REG_INDEX(check_sljit_get_register_index(reg
));
1785 return reg_map
[reg
];
1788 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_get_float_register_index(sljit_s32 reg
)
1790 CHECK_REG_INDEX(check_sljit_get_float_register_index(reg
));
1791 return (freg_map
[reg
] << 1);
1794 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op_custom(struct sljit_compiler
*compiler
,
1795 void *instruction
, sljit_s32 size
)
1798 CHECK(check_sljit_emit_op_custom(compiler
, instruction
, size
));
1800 return push_inst(compiler
, *(sljit_uw
*)instruction
);
1803 /* --------------------------------------------------------------------- */
1804 /* Floating point operators */
1805 /* --------------------------------------------------------------------- */
1808 #define FPU_LOAD (1 << 20)
1809 #define EMIT_FPU_DATA_TRANSFER(inst, add, base, freg, offs) \
1810 ((inst) | ((add) << 23) | (reg_map[base] << 16) | (freg_map[freg] << 12) | (offs))
1811 #define EMIT_FPU_OPERATION(opcode, mode, dst, src1, src2) \
1812 ((opcode) | (mode) | (freg_map[dst] << 12) | freg_map[src1] | (freg_map[src2] << 16))
1814 static sljit_s32
emit_fop_mem(struct sljit_compiler
*compiler
, sljit_s32 flags
, sljit_s32 reg
, sljit_s32 arg
, sljit_sw argw
)
1817 sljit_sw inst
= VSTR_F32
| (flags
& (SLJIT_F32_OP
| FPU_LOAD
));
1819 SLJIT_ASSERT(arg
& SLJIT_MEM
);
1822 if (SLJIT_UNLIKELY(arg
& OFFS_REG_MASK
)) {
1823 FAIL_IF(push_inst(compiler
, ADD
| RD(TMP_REG2
) | RN(arg
& REG_MASK
) | RM(OFFS_REG(arg
)) | ((argw
& 0x3) << 7)));
1828 /* Fast loads and stores. */
1830 if (!(argw
& ~0x3fc))
1831 return push_inst(compiler
, EMIT_FPU_DATA_TRANSFER(inst
, 1, arg
& REG_MASK
, reg
, argw
>> 2));
1832 if (!(-argw
& ~0x3fc))
1833 return push_inst(compiler
, EMIT_FPU_DATA_TRANSFER(inst
, 0, arg
& REG_MASK
, reg
, (-argw
) >> 2));
1835 imm
= get_imm(argw
& ~0x3fc);
1837 FAIL_IF(push_inst(compiler
, ADD
| RD(TMP_REG2
) | RN(arg
& REG_MASK
) | imm
));
1838 return push_inst(compiler
, EMIT_FPU_DATA_TRANSFER(inst
, 1, TMP_REG2
, reg
, (argw
& 0x3fc) >> 2));
1840 imm
= get_imm(-argw
& ~0x3fc);
1843 FAIL_IF(push_inst(compiler
, SUB
| RD(TMP_REG2
) | RN(arg
& REG_MASK
) | imm
));
1844 return push_inst(compiler
, EMIT_FPU_DATA_TRANSFER(inst
, 0, TMP_REG2
, reg
, (argw
& 0x3fc) >> 2));
1849 FAIL_IF(load_immediate(compiler
, TMP_REG2
, argw
));
1850 FAIL_IF(push_inst(compiler
, ADD
| RD(TMP_REG2
) | RN(arg
& REG_MASK
) | RM(TMP_REG2
)));
1853 FAIL_IF(load_immediate(compiler
, TMP_REG2
, argw
));
1855 return push_inst(compiler
, EMIT_FPU_DATA_TRANSFER(inst
, 1, TMP_REG2
, reg
, 0));
1858 static SLJIT_INLINE sljit_s32
sljit_emit_fop1_conv_sw_from_f64(struct sljit_compiler
*compiler
, sljit_s32 op
,
1859 sljit_s32 dst
, sljit_sw dstw
,
1860 sljit_s32 src
, sljit_sw srcw
)
1864 if (src
& SLJIT_MEM
) {
1865 FAIL_IF(emit_fop_mem(compiler
, (op
& SLJIT_F32_OP
) | FPU_LOAD
, TMP_FREG1
, src
, srcw
));
1869 FAIL_IF(push_inst(compiler
, EMIT_FPU_OPERATION(VCVT_S32_F32
, op
& SLJIT_F32_OP
, TMP_FREG1
, src
, 0)));
1871 if (FAST_IS_REG(dst
))
1872 return push_inst(compiler
, VMOV
| (1 << 20) | RD(dst
) | (freg_map
[TMP_FREG1
] << 16));
1874 /* Store the integer value from a VFP register. */
1875 return emit_fop_mem(compiler
, 0, TMP_FREG1
, dst
, dstw
);
1878 static SLJIT_INLINE sljit_s32
sljit_emit_fop1_conv_f64_from_sw(struct sljit_compiler
*compiler
, sljit_s32 op
,
1879 sljit_s32 dst
, sljit_sw dstw
,
1880 sljit_s32 src
, sljit_sw srcw
)
1882 sljit_s32 dst_r
= FAST_IS_REG(dst
) ? dst
: TMP_FREG1
;
1886 if (FAST_IS_REG(src
))
1887 FAIL_IF(push_inst(compiler
, VMOV
| RD(src
) | (freg_map
[TMP_FREG1
] << 16)));
1888 else if (src
& SLJIT_MEM
) {
1889 /* Load the integer value into a VFP register. */
1890 FAIL_IF(emit_fop_mem(compiler
, FPU_LOAD
, TMP_FREG1
, src
, srcw
));
1893 FAIL_IF(load_immediate(compiler
, TMP_REG1
, srcw
));
1894 FAIL_IF(push_inst(compiler
, VMOV
| RD(TMP_REG1
) | (freg_map
[TMP_FREG1
] << 16)));
1897 FAIL_IF(push_inst(compiler
, EMIT_FPU_OPERATION(VCVT_F32_S32
, op
& SLJIT_F32_OP
, dst_r
, TMP_FREG1
, 0)));
1899 if (dst
& SLJIT_MEM
)
1900 return emit_fop_mem(compiler
, (op
& SLJIT_F32_OP
), TMP_FREG1
, dst
, dstw
);
1901 return SLJIT_SUCCESS
;
1904 static SLJIT_INLINE sljit_s32
sljit_emit_fop1_cmp(struct sljit_compiler
*compiler
, sljit_s32 op
,
1905 sljit_s32 src1
, sljit_sw src1w
,
1906 sljit_s32 src2
, sljit_sw src2w
)
1910 if (src1
& SLJIT_MEM
) {
1911 FAIL_IF(emit_fop_mem(compiler
, (op
& SLJIT_F32_OP
) | FPU_LOAD
, TMP_FREG1
, src1
, src1w
));
1915 if (src2
& SLJIT_MEM
) {
1916 FAIL_IF(emit_fop_mem(compiler
, (op
& SLJIT_F32_OP
) | FPU_LOAD
, TMP_FREG2
, src2
, src2w
));
1920 FAIL_IF(push_inst(compiler
, EMIT_FPU_OPERATION(VCMP_F32
, op
& SLJIT_F32_OP
, src1
, src2
, 0)));
1921 return push_inst(compiler
, VMRS
);
1924 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_fop1(struct sljit_compiler
*compiler
, sljit_s32 op
,
1925 sljit_s32 dst
, sljit_sw dstw
,
1926 sljit_s32 src
, sljit_sw srcw
)
1932 SLJIT_COMPILE_ASSERT((SLJIT_F32_OP
== 0x100), float_transfer_bit_error
);
1933 SELECT_FOP1_OPERATION_WITH_CHECKS(compiler
, op
, dst
, dstw
, src
, srcw
);
1935 dst_r
= FAST_IS_REG(dst
) ? dst
: TMP_FREG1
;
1937 if (GET_OPCODE(op
) != SLJIT_CONV_F64_FROM_F32
)
1940 if (src
& SLJIT_MEM
) {
1941 FAIL_IF(emit_fop_mem(compiler
, (op
& SLJIT_F32_OP
) | FPU_LOAD
, dst_r
, src
, srcw
));
1945 switch (GET_OPCODE(op
)) {
1948 if (dst_r
!= TMP_FREG1
)
1949 FAIL_IF(push_inst(compiler
, EMIT_FPU_OPERATION(VMOV_F32
, op
& SLJIT_F32_OP
, dst_r
, src
, 0)));
1955 FAIL_IF(push_inst(compiler
, EMIT_FPU_OPERATION(VNEG_F32
, op
& SLJIT_F32_OP
, dst_r
, src
, 0)));
1958 FAIL_IF(push_inst(compiler
, EMIT_FPU_OPERATION(VABS_F32
, op
& SLJIT_F32_OP
, dst_r
, src
, 0)));
1960 case SLJIT_CONV_F64_FROM_F32
:
1961 FAIL_IF(push_inst(compiler
, EMIT_FPU_OPERATION(VCVT_F64_F32
, op
& SLJIT_F32_OP
, dst_r
, src
, 0)));
1966 if (dst
& SLJIT_MEM
)
1967 return emit_fop_mem(compiler
, (op
& SLJIT_F32_OP
), dst_r
, dst
, dstw
);
1968 return SLJIT_SUCCESS
;
1971 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_fop2(struct sljit_compiler
*compiler
, sljit_s32 op
,
1972 sljit_s32 dst
, sljit_sw dstw
,
1973 sljit_s32 src1
, sljit_sw src1w
,
1974 sljit_s32 src2
, sljit_sw src2w
)
1979 CHECK(check_sljit_emit_fop2(compiler
, op
, dst
, dstw
, src1
, src1w
, src2
, src2w
));
1980 ADJUST_LOCAL_OFFSET(dst
, dstw
);
1981 ADJUST_LOCAL_OFFSET(src1
, src1w
);
1982 ADJUST_LOCAL_OFFSET(src2
, src2w
);
1986 dst_r
= FAST_IS_REG(dst
) ? dst
: TMP_FREG1
;
1988 if (src2
& SLJIT_MEM
) {
1989 FAIL_IF(emit_fop_mem(compiler
, (op
& SLJIT_F32_OP
) | FPU_LOAD
, TMP_FREG2
, src2
, src2w
));
1993 if (src1
& SLJIT_MEM
) {
1994 FAIL_IF(emit_fop_mem(compiler
, (op
& SLJIT_F32_OP
) | FPU_LOAD
, TMP_FREG1
, src1
, src1w
));
1998 switch (GET_OPCODE(op
)) {
2000 FAIL_IF(push_inst(compiler
, EMIT_FPU_OPERATION(VADD_F32
, op
& SLJIT_F32_OP
, dst_r
, src2
, src1
)));
2004 FAIL_IF(push_inst(compiler
, EMIT_FPU_OPERATION(VSUB_F32
, op
& SLJIT_F32_OP
, dst_r
, src2
, src1
)));
2008 FAIL_IF(push_inst(compiler
, EMIT_FPU_OPERATION(VMUL_F32
, op
& SLJIT_F32_OP
, dst_r
, src2
, src1
)));
2012 FAIL_IF(push_inst(compiler
, EMIT_FPU_OPERATION(VDIV_F32
, op
& SLJIT_F32_OP
, dst_r
, src2
, src1
)));
2016 if (dst_r
== TMP_FREG1
)
2017 FAIL_IF(emit_fop_mem(compiler
, (op
& SLJIT_F32_OP
), TMP_FREG1
, dst
, dstw
));
2019 return SLJIT_SUCCESS
;
2023 #undef EMIT_FPU_DATA_TRANSFER
2025 /* --------------------------------------------------------------------- */
2026 /* Other instructions */
2027 /* --------------------------------------------------------------------- */
2029 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_fast_enter(struct sljit_compiler
*compiler
, sljit_s32 dst
, sljit_sw dstw
)
2032 CHECK(check_sljit_emit_fast_enter(compiler
, dst
, dstw
));
2033 ADJUST_LOCAL_OFFSET(dst
, dstw
);
2035 SLJIT_ASSERT(reg_map
[TMP_REG2
] == 14);
2037 if (FAST_IS_REG(dst
))
2038 return push_inst(compiler
, MOV
| RD(dst
) | RM(TMP_REG2
));
2041 return emit_op_mem(compiler
, WORD_SIZE
, TMP_REG2
, dst
, dstw
, TMP_REG1
);
2044 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_fast_return(struct sljit_compiler
*compiler
, sljit_s32 src
, sljit_sw srcw
)
2047 CHECK(check_sljit_emit_fast_return(compiler
, src
, srcw
));
2048 ADJUST_LOCAL_OFFSET(src
, srcw
);
2050 SLJIT_ASSERT(reg_map
[TMP_REG2
] == 14);
2052 if (FAST_IS_REG(src
))
2053 FAIL_IF(push_inst(compiler
, MOV
| RD(TMP_REG2
) | RM(src
)));
2055 FAIL_IF(emit_op_mem(compiler
, WORD_SIZE
| LOAD_DATA
, TMP_REG2
, src
, srcw
, TMP_REG1
));
2057 return push_inst(compiler
, BX
| RM(TMP_REG2
));
2060 /* --------------------------------------------------------------------- */
2061 /* Conditional instructions */
2062 /* --------------------------------------------------------------------- */
2064 static sljit_uw
get_cc(sljit_s32 type
)
2068 case SLJIT_MUL_NOT_OVERFLOW
:
2069 case SLJIT_EQUAL_F64
:
2072 case SLJIT_NOT_EQUAL
:
2073 case SLJIT_MUL_OVERFLOW
:
2074 case SLJIT_NOT_EQUAL_F64
:
2078 case SLJIT_LESS_F64
:
2081 case SLJIT_GREATER_EQUAL
:
2082 case SLJIT_GREATER_EQUAL_F64
:
2086 case SLJIT_GREATER_F64
:
2089 case SLJIT_LESS_EQUAL
:
2090 case SLJIT_LESS_EQUAL_F64
:
2093 case SLJIT_SIG_LESS
:
2096 case SLJIT_SIG_GREATER_EQUAL
:
2099 case SLJIT_SIG_GREATER
:
2102 case SLJIT_SIG_LESS_EQUAL
:
2105 case SLJIT_OVERFLOW
:
2106 case SLJIT_UNORDERED_F64
:
2109 case SLJIT_NOT_OVERFLOW
:
2110 case SLJIT_ORDERED_F64
:
2114 SLJIT_ASSERT(type
>= SLJIT_JUMP
&& type
<= SLJIT_CALL_CDECL
);
2119 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_label
* sljit_emit_label(struct sljit_compiler
*compiler
)
2121 struct sljit_label
*label
;
2124 CHECK_PTR(check_sljit_emit_label(compiler
));
2126 if (compiler
->last_label
&& compiler
->last_label
->size
== compiler
->size
)
2127 return compiler
->last_label
;
2129 label
= (struct sljit_label
*)ensure_abuf(compiler
, sizeof(struct sljit_label
));
2130 PTR_FAIL_IF(!label
);
2131 set_label(label
, compiler
);
2135 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_jump
* sljit_emit_jump(struct sljit_compiler
*compiler
, sljit_s32 type
)
2137 struct sljit_jump
*jump
;
2140 CHECK_PTR(check_sljit_emit_jump(compiler
, type
));
2142 jump
= (struct sljit_jump
*)ensure_abuf(compiler
, sizeof(struct sljit_jump
));
2144 set_jump(jump
, compiler
, type
& SLJIT_REWRITABLE_JUMP
);
2147 SLJIT_ASSERT(reg_map
[TMP_REG1
] != 14);
2149 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
2150 if (type
>= SLJIT_FAST_CALL
)
2151 PTR_FAIL_IF(prepare_blx(compiler
));
2152 PTR_FAIL_IF(push_inst_with_unique_literal(compiler
, ((EMIT_DATA_TRANSFER(WORD_SIZE
| LOAD_DATA
, 1,
2153 type
<= SLJIT_JUMP
? TMP_PC
: TMP_REG1
, TMP_PC
, 0)) & ~COND_MASK
) | get_cc(type
), 0));
2155 if (jump
->flags
& SLJIT_REWRITABLE_JUMP
) {
2156 jump
->addr
= compiler
->size
;
2157 compiler
->patches
++;
2160 if (type
>= SLJIT_FAST_CALL
) {
2161 jump
->flags
|= IS_BL
;
2162 PTR_FAIL_IF(emit_blx(compiler
));
2165 if (!(jump
->flags
& SLJIT_REWRITABLE_JUMP
))
2166 jump
->addr
= compiler
->size
;
2168 if (type
>= SLJIT_FAST_CALL
)
2169 jump
->flags
|= IS_BL
;
2170 PTR_FAIL_IF(emit_imm(compiler
, TMP_REG1
, 0));
2171 PTR_FAIL_IF(push_inst(compiler
, (((type
<= SLJIT_JUMP
? BX
: BLX
) | RM(TMP_REG1
)) & ~COND_MASK
) | get_cc(type
)));
2172 jump
->addr
= compiler
->size
;
2179 static sljit_s32
softfloat_call_with_args(struct sljit_compiler
*compiler
, sljit_s32 arg_types
, sljit_s32
*src
)
2181 sljit_s32 stack_offset
= 0;
2182 sljit_s32 arg_count
= 0;
2183 sljit_s32 word_arg_offset
= 0;
2184 sljit_s32 float_arg_count
= 0;
2185 sljit_s32 types
= 0;
2186 sljit_s32 src_offset
= 4 * sizeof(sljit_sw
);
2187 sljit_u8 offsets
[4];
2189 if (src
&& FAST_IS_REG(*src
))
2190 src_offset
= reg_map
[*src
] * sizeof(sljit_sw
);
2192 arg_types
>>= SLJIT_DEF_SHIFT
;
2195 types
= (types
<< SLJIT_DEF_SHIFT
) | (arg_types
& SLJIT_DEF_MASK
);
2197 switch (arg_types
& SLJIT_DEF_MASK
) {
2198 case SLJIT_ARG_TYPE_F32
:
2199 offsets
[arg_count
] = (sljit_u8
)stack_offset
;
2200 stack_offset
+= sizeof(sljit_f32
);
2204 case SLJIT_ARG_TYPE_F64
:
2205 if (stack_offset
& 0x7)
2206 stack_offset
+= sizeof(sljit_sw
);
2207 offsets
[arg_count
] = (sljit_u8
)stack_offset
;
2208 stack_offset
+= sizeof(sljit_f64
);
2213 offsets
[arg_count
] = (sljit_u8
)stack_offset
;
2214 stack_offset
+= sizeof(sljit_sw
);
2216 word_arg_offset
+= sizeof(sljit_sw
);
2220 arg_types
>>= SLJIT_DEF_SHIFT
;
2223 if (stack_offset
> 16)
2224 FAIL_IF(push_inst(compiler
, SUB
| RD(SLJIT_SP
) | RN(SLJIT_SP
) | SRC2_IMM
| (((stack_offset
- 16) + 0x7) & ~0x7)));
2226 /* Process arguments in reversed direction. */
2228 switch (types
& SLJIT_DEF_MASK
) {
2229 case SLJIT_ARG_TYPE_F32
:
2232 stack_offset
= offsets
[arg_count
];
2234 if (stack_offset
< 16) {
2235 if (src_offset
== stack_offset
) {
2236 FAIL_IF(push_inst(compiler
, MOV
| RD(TMP_REG1
) | (src_offset
>> 2)));
2239 FAIL_IF(push_inst(compiler
, VMOV
| 0x100000 | (float_arg_count
<< 16) | (stack_offset
<< 10)));
2241 FAIL_IF(push_inst(compiler
, VSTR_F32
| 0x800000 | RN(SLJIT_SP
) | (float_arg_count
<< 12) | ((stack_offset
- 16) >> 2)));
2243 case SLJIT_ARG_TYPE_F64
:
2246 stack_offset
= offsets
[arg_count
];
2248 SLJIT_ASSERT((stack_offset
& 0x7) == 0);
2250 if (stack_offset
< 16) {
2251 if (src_offset
== stack_offset
|| src_offset
== stack_offset
+ sizeof(sljit_sw
)) {
2252 FAIL_IF(push_inst(compiler
, MOV
| RD(TMP_REG1
) | (src_offset
>> 2)));
2255 FAIL_IF(push_inst(compiler
, VMOV2
| 0x100000 | (stack_offset
<< 10) | ((stack_offset
+ sizeof(sljit_sw
)) << 14) | float_arg_count
));
2257 FAIL_IF(push_inst(compiler
, VSTR_F32
| 0x800100 | RN(SLJIT_SP
) | (float_arg_count
<< 12) | ((stack_offset
- 16) >> 2)));
2261 word_arg_offset
-= sizeof(sljit_sw
);
2262 stack_offset
= offsets
[arg_count
];
2264 SLJIT_ASSERT(stack_offset
>= word_arg_offset
);
2266 if (stack_offset
!= word_arg_offset
) {
2267 if (stack_offset
< 16) {
2268 if (src_offset
== stack_offset
) {
2269 FAIL_IF(push_inst(compiler
, MOV
| RD(TMP_REG1
) | (src_offset
>> 2)));
2272 else if (src_offset
== word_arg_offset
) {
2273 *src
= 1 + (stack_offset
>> 2);
2274 src_offset
= stack_offset
;
2276 FAIL_IF(push_inst(compiler
, MOV
| (stack_offset
<< 10) | (word_arg_offset
>> 2)));
2278 FAIL_IF(push_inst(compiler
, data_transfer_insts
[WORD_SIZE
] | 0x800000 | RN(SLJIT_SP
) | (word_arg_offset
<< 10) | (stack_offset
- 16)));
2283 types
>>= SLJIT_DEF_SHIFT
;
2286 return SLJIT_SUCCESS
;
2289 static sljit_s32
softfloat_post_call_with_args(struct sljit_compiler
*compiler
, sljit_s32 arg_types
)
2291 sljit_s32 stack_size
= 0;
2293 if ((arg_types
& SLJIT_DEF_MASK
) == SLJIT_ARG_TYPE_F32
)
2294 FAIL_IF(push_inst(compiler
, VMOV
| (0 << 16) | (0 << 12)));
2295 if ((arg_types
& SLJIT_DEF_MASK
) == SLJIT_ARG_TYPE_F64
)
2296 FAIL_IF(push_inst(compiler
, VMOV2
| (1 << 16) | (0 << 12) | 0));
2298 arg_types
>>= SLJIT_DEF_SHIFT
;
2301 switch (arg_types
& SLJIT_DEF_MASK
) {
2302 case SLJIT_ARG_TYPE_F32
:
2303 stack_size
+= sizeof(sljit_f32
);
2305 case SLJIT_ARG_TYPE_F64
:
2306 if (stack_size
& 0x7)
2307 stack_size
+= sizeof(sljit_sw
);
2308 stack_size
+= sizeof(sljit_f64
);
2311 stack_size
+= sizeof(sljit_sw
);
2315 arg_types
>>= SLJIT_DEF_SHIFT
;
2318 if (stack_size
<= 16)
2319 return SLJIT_SUCCESS
;
2321 return push_inst(compiler
, ADD
| RD(SLJIT_SP
) | RN(SLJIT_SP
) | SRC2_IMM
| (((stack_size
- 16) + 0x7) & ~0x7));
2324 #else /* !__SOFTFP__ */
2326 static sljit_s32
hardfloat_call_with_args(struct sljit_compiler
*compiler
, sljit_s32 arg_types
)
2328 sljit_u32 remap
= 0;
2329 sljit_u32 offset
= 0;
2330 sljit_u32 new_offset
, mask
;
2332 /* Remove return value. */
2333 arg_types
>>= SLJIT_DEF_SHIFT
;
2336 if ((arg_types
& SLJIT_DEF_MASK
) == SLJIT_ARG_TYPE_F32
) {
2340 while (remap
& mask
) {
2346 if (offset
!= new_offset
)
2347 FAIL_IF(push_inst(compiler
, EMIT_FPU_OPERATION(VMOV_F32
,
2348 0, (new_offset
>> 1) + 1, (offset
>> 1) + 1, 0) | ((new_offset
& 0x1) ? 0x400000 : 0)));
2352 else if ((arg_types
& SLJIT_DEF_MASK
) == SLJIT_ARG_TYPE_F64
) {
2356 while (remap
& mask
) {
2362 if (offset
!= new_offset
)
2363 FAIL_IF(push_inst(compiler
, EMIT_FPU_OPERATION(VMOV_F32
, SLJIT_F32_OP
, (new_offset
>> 1) + 1, (offset
>> 1) + 1, 0)));
2367 arg_types
>>= SLJIT_DEF_SHIFT
;
2370 return SLJIT_SUCCESS
;
2373 #endif /* __SOFTFP__ */
2375 #undef EMIT_FPU_OPERATION
2377 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_jump
* sljit_emit_call(struct sljit_compiler
*compiler
, sljit_s32 type
,
2378 sljit_s32 arg_types
)
2381 struct sljit_jump
*jump
;
2385 CHECK_PTR(check_sljit_emit_call(compiler
, type
, arg_types
));
2388 PTR_FAIL_IF(softfloat_call_with_args(compiler
, arg_types
, NULL
));
2390 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2391 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2392 compiler
->skip_checks
= 1;
2395 jump
= sljit_emit_jump(compiler
, type
);
2396 PTR_FAIL_IF(jump
== NULL
);
2398 PTR_FAIL_IF(softfloat_post_call_with_args(compiler
, arg_types
));
2400 #else /* !__SOFTFP__ */
2401 PTR_FAIL_IF(hardfloat_call_with_args(compiler
, arg_types
));
2403 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2404 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2405 compiler
->skip_checks
= 1;
2408 return sljit_emit_jump(compiler
, type
);
2409 #endif /* __SOFTFP__ */
2412 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_ijump(struct sljit_compiler
*compiler
, sljit_s32 type
, sljit_s32 src
, sljit_sw srcw
)
2414 struct sljit_jump
*jump
;
2417 CHECK(check_sljit_emit_ijump(compiler
, type
, src
, srcw
));
2418 ADJUST_LOCAL_OFFSET(src
, srcw
);
2420 SLJIT_ASSERT(reg_map
[TMP_REG1
] != 14);
2422 if (!(src
& SLJIT_IMM
)) {
2423 if (FAST_IS_REG(src
)) {
2424 SLJIT_ASSERT(reg_map
[src
] != 14);
2425 return push_inst(compiler
, (type
<= SLJIT_JUMP
? BX
: BLX
) | RM(src
));
2428 SLJIT_ASSERT(src
& SLJIT_MEM
);
2429 FAIL_IF(emit_op_mem(compiler
, WORD_SIZE
| LOAD_DATA
, TMP_REG1
, src
, srcw
, TMP_REG1
));
2430 return push_inst(compiler
, (type
<= SLJIT_JUMP
? BX
: BLX
) | RM(TMP_REG1
));
2433 /* These jumps are converted to jump/call instructions when possible. */
2434 jump
= (struct sljit_jump
*)ensure_abuf(compiler
, sizeof(struct sljit_jump
));
2436 set_jump(jump
, compiler
, JUMP_ADDR
| ((type
>= SLJIT_FAST_CALL
) ? IS_BL
: 0));
2437 jump
->u
.target
= srcw
;
2439 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
2440 if (type
>= SLJIT_FAST_CALL
)
2441 FAIL_IF(prepare_blx(compiler
));
2442 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));
2443 if (type
>= SLJIT_FAST_CALL
)
2444 FAIL_IF(emit_blx(compiler
));
2446 FAIL_IF(emit_imm(compiler
, TMP_REG1
, 0));
2447 FAIL_IF(push_inst(compiler
, (type
<= SLJIT_JUMP
? BX
: BLX
) | RM(TMP_REG1
)));
2449 jump
->addr
= compiler
->size
;
2450 return SLJIT_SUCCESS
;
2453 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_icall(struct sljit_compiler
*compiler
, sljit_s32 type
,
2454 sljit_s32 arg_types
,
2455 sljit_s32 src
, sljit_sw srcw
)
2458 CHECK(check_sljit_emit_icall(compiler
, type
, arg_types
, src
, srcw
));
2461 if (src
& SLJIT_MEM
) {
2462 FAIL_IF(emit_op_mem(compiler
, WORD_SIZE
| LOAD_DATA
, TMP_REG1
, src
, srcw
, TMP_REG1
));
2466 FAIL_IF(softfloat_call_with_args(compiler
, arg_types
, &src
));
2468 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2469 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2470 compiler
->skip_checks
= 1;
2473 FAIL_IF(sljit_emit_ijump(compiler
, type
, src
, srcw
));
2475 return softfloat_post_call_with_args(compiler
, arg_types
);
2476 #else /* !__SOFTFP__ */
2477 FAIL_IF(hardfloat_call_with_args(compiler
, arg_types
));
2479 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2480 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2481 compiler
->skip_checks
= 1;
2484 return sljit_emit_ijump(compiler
, type
, src
, srcw
);
2485 #endif /* __SOFTFP__ */
2488 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op_flags(struct sljit_compiler
*compiler
, sljit_s32 op
,
2489 sljit_s32 dst
, sljit_sw dstw
,
2492 sljit_s32 dst_reg
, flags
= GET_ALL_FLAGS(op
);
2496 CHECK(check_sljit_emit_op_flags(compiler
, op
, dst
, dstw
, type
));
2497 ADJUST_LOCAL_OFFSET(dst
, dstw
);
2499 op
= GET_OPCODE(op
);
2500 cc
= get_cc(type
& 0xff);
2501 dst_reg
= FAST_IS_REG(dst
) ? dst
: TMP_REG1
;
2503 if (op
< SLJIT_ADD
) {
2504 FAIL_IF(push_inst(compiler
, MOV
| RD(dst_reg
) | SRC2_IMM
| 0));
2505 FAIL_IF(push_inst(compiler
, ((MOV
| RD(dst_reg
) | SRC2_IMM
| 1) & ~COND_MASK
) | cc
));
2506 if (dst
& SLJIT_MEM
)
2507 return emit_op_mem(compiler
, WORD_SIZE
, TMP_REG1
, dst
, dstw
, TMP_REG2
);
2508 return SLJIT_SUCCESS
;
2511 ins
= (op
== SLJIT_AND
? AND
: (op
== SLJIT_OR
? ORR
: EOR
));
2513 if (dst
& SLJIT_MEM
)
2514 FAIL_IF(emit_op_mem(compiler
, WORD_SIZE
| LOAD_DATA
, TMP_REG1
, dst
, dstw
, TMP_REG2
));
2516 FAIL_IF(push_inst(compiler
, ((ins
| RD(dst_reg
) | RN(dst_reg
) | SRC2_IMM
| 1) & ~COND_MASK
) | cc
));
2518 if (op
== SLJIT_AND
)
2519 FAIL_IF(push_inst(compiler
, ((ins
| RD(dst_reg
) | RN(dst_reg
) | SRC2_IMM
| 0) & ~COND_MASK
) | (cc
^ 0x10000000)));
2521 if (dst
& SLJIT_MEM
)
2522 FAIL_IF(emit_op_mem(compiler
, WORD_SIZE
, TMP_REG1
, dst
, dstw
, TMP_REG2
));
2524 if (flags
& SLJIT_SET_Z
)
2525 return push_inst(compiler
, MOV
| SET_FLAGS
| RD(TMP_REG2
) | RM(dst_reg
));
2526 return SLJIT_SUCCESS
;
2529 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_cmov(struct sljit_compiler
*compiler
, sljit_s32 type
,
2531 sljit_s32 src
, sljit_sw srcw
)
2536 CHECK(check_sljit_emit_cmov(compiler
, type
, dst_reg
, src
, srcw
));
2538 dst_reg
&= ~SLJIT_I32_OP
;
2540 cc
= get_cc(type
& 0xff);
2542 if (SLJIT_UNLIKELY(src
& SLJIT_IMM
)) {
2543 tmp
= get_imm(srcw
);
2545 return push_inst(compiler
, ((MOV
| RD(dst_reg
) | tmp
) & ~COND_MASK
) | cc
);
2547 tmp
= get_imm(~srcw
);
2549 return push_inst(compiler
, ((MVN
| RD(dst_reg
) | tmp
) & ~COND_MASK
) | cc
);
2551 #if (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
2552 tmp
= (sljit_uw
) srcw
;
2553 FAIL_IF(push_inst(compiler
, (MOVW
& ~COND_MASK
) | cc
| RD(dst_reg
) | ((tmp
<< 4) & 0xf0000) | (tmp
& 0xfff)));
2555 return SLJIT_SUCCESS
;
2556 return push_inst(compiler
, (MOVT
& ~COND_MASK
) | cc
| RD(dst_reg
) | ((tmp
>> 12) & 0xf0000) | ((tmp
>> 16) & 0xfff));
2558 FAIL_IF(load_immediate(compiler
, TMP_REG1
, srcw
));
2563 return push_inst(compiler
, ((MOV
| RD(dst_reg
) | RM(src
)) & ~COND_MASK
) | cc
);
2566 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_mem(struct sljit_compiler
*compiler
, sljit_s32 type
,
2568 sljit_s32 mem
, sljit_sw memw
)
2571 sljit_uw is_type1_transfer
, inst
;
2574 CHECK(check_sljit_emit_mem(compiler
, type
, reg
, mem
, memw
));
2576 is_type1_transfer
= 1;
2578 switch (type
& 0xff) {
2589 if (!(type
& SLJIT_MEM_STORE
))
2590 is_type1_transfer
= 0;
2591 flags
= BYTE_SIZE
| SIGNED
;
2594 is_type1_transfer
= 0;
2598 is_type1_transfer
= 0;
2599 flags
= HALF_SIZE
| SIGNED
;
2602 SLJIT_UNREACHABLE();
2607 if (!(type
& SLJIT_MEM_STORE
))
2610 SLJIT_ASSERT(is_type1_transfer
== !!IS_TYPE1_TRANSFER(flags
));
2612 if (SLJIT_UNLIKELY(mem
& OFFS_REG_MASK
)) {
2613 if (!is_type1_transfer
&& memw
!= 0)
2614 return SLJIT_ERR_UNSUPPORTED
;
2617 if (is_type1_transfer
) {
2618 if (memw
> 4095 && memw
< -4095)
2619 return SLJIT_ERR_UNSUPPORTED
;
2622 if (memw
> 255 && memw
< -255)
2623 return SLJIT_ERR_UNSUPPORTED
;
2627 if (type
& SLJIT_MEM_SUPP
)
2628 return SLJIT_SUCCESS
;
2630 if (SLJIT_UNLIKELY(mem
& OFFS_REG_MASK
)) {
2633 inst
= EMIT_DATA_TRANSFER(flags
, 1, reg
, mem
& REG_MASK
, RM(OFFS_REG(mem
)) | (memw
<< 7));
2635 if (is_type1_transfer
)
2638 if (type
& SLJIT_MEM_PRE
)
2643 return push_inst(compiler
, inst
);
2646 inst
= EMIT_DATA_TRANSFER(flags
, 0, reg
, mem
& REG_MASK
, 0);
2648 if (type
& SLJIT_MEM_PRE
)
2653 if (is_type1_transfer
) {
2659 return push_inst(compiler
, inst
| memw
);
2667 return push_inst(compiler
, inst
| TYPE2_TRANSFER_IMM(memw
));
2670 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_const
* sljit_emit_const(struct sljit_compiler
*compiler
, sljit_s32 dst
, sljit_sw dstw
, sljit_sw init_value
)
2672 struct sljit_const
*const_
;
2676 CHECK_PTR(check_sljit_emit_const(compiler
, dst
, dstw
, init_value
));
2677 ADJUST_LOCAL_OFFSET(dst
, dstw
);
2679 dst_r
= SLOW_IS_REG(dst
) ? dst
: TMP_REG2
;
2681 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
2682 PTR_FAIL_IF(push_inst_with_unique_literal(compiler
, EMIT_DATA_TRANSFER(WORD_SIZE
| LOAD_DATA
, 1, dst_r
, TMP_PC
, 0), init_value
));
2683 compiler
->patches
++;
2685 PTR_FAIL_IF(emit_imm(compiler
, dst_r
, init_value
));
2688 const_
= (struct sljit_const
*)ensure_abuf(compiler
, sizeof(struct sljit_const
));
2689 PTR_FAIL_IF(!const_
);
2690 set_const(const_
, compiler
);
2692 if (dst
& SLJIT_MEM
)
2693 PTR_FAIL_IF(emit_op_mem(compiler
, WORD_SIZE
, TMP_REG2
, dst
, dstw
, TMP_REG1
));
2697 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_put_label
* sljit_emit_put_label(struct sljit_compiler
*compiler
, sljit_s32 dst
, sljit_sw dstw
)
2699 struct sljit_put_label
*put_label
;
2703 CHECK_PTR(check_sljit_emit_put_label(compiler
, dst
, dstw
));
2704 ADJUST_LOCAL_OFFSET(dst
, dstw
);
2706 dst_r
= SLOW_IS_REG(dst
) ? dst
: TMP_REG2
;
2708 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
2709 PTR_FAIL_IF(push_inst_with_unique_literal(compiler
, EMIT_DATA_TRANSFER(WORD_SIZE
| LOAD_DATA
, 1, dst_r
, TMP_PC
, 0), 0));
2710 compiler
->patches
++;
2712 PTR_FAIL_IF(emit_imm(compiler
, dst_r
, 0));
2715 put_label
= (struct sljit_put_label
*)ensure_abuf(compiler
, sizeof(struct sljit_put_label
));
2716 PTR_FAIL_IF(!put_label
);
2717 set_put_label(put_label
, compiler
, 0);
2719 if (dst
& SLJIT_MEM
)
2720 PTR_FAIL_IF(emit_op_mem(compiler
, WORD_SIZE
, TMP_REG2
, dst
, dstw
, TMP_REG1
));
2724 SLJIT_API_FUNC_ATTRIBUTE
void sljit_set_jump_addr(sljit_uw addr
, sljit_uw new_target
, sljit_sw executable_offset
)
2726 inline_set_jump_addr(addr
, executable_offset
, new_target
, 1);
2729 SLJIT_API_FUNC_ATTRIBUTE
void sljit_set_const(sljit_uw addr
, sljit_sw new_constant
, sljit_sw executable_offset
)
2731 inline_set_const(addr
, executable_offset
, new_constant
, 1);