Remove unnecessary checks
[sljit.git] / sljit_src / sljitNativeARM_32.c
blob4e8070a3663432b3599f73ddf35538ea52b57182
1 /*
2 * Stack-less Just-In-Time compiler
4 * Copyright Zoltan Herczeg (hzmester@freemail.hu). All rights reserved.
6 * Redistribution and use in source and binary forms, with or without modification, are
7 * permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright notice, this list of
10 * conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
13 * of conditions and the following disclaimer in the documentation and/or other materials
14 * provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) AND CONTRIBUTORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19 * SHALL THE COPYRIGHT HOLDER(S) OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
22 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #ifdef __SOFTFP__
28 #define ARM_ABI_INFO " ABI:softfp"
29 #else
30 #define ARM_ABI_INFO " ABI:hardfp"
31 #endif
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;
39 #else
40 #error "Internal error: Unknown ARM architecture"
41 #endif
44 /* Last register + 1. */
45 #define TMP_REG1 (SLJIT_NUMBER_OF_REGISTERS + 2)
46 #define TMP_REG2 (SLJIT_NUMBER_OF_REGISTERS + 3)
47 #define TMP_PC (SLJIT_NUMBER_OF_REGISTERS + 4)
49 #define TMP_FREG1 (SLJIT_NUMBER_OF_FLOAT_REGISTERS + 1)
50 #define TMP_FREG2 (SLJIT_NUMBER_OF_FLOAT_REGISTERS + 2)
52 /* In ARM instruction words.
53 Cache lines are usually 32 byte aligned. */
54 #define CONST_POOL_ALIGNMENT 8
55 #define CONST_POOL_EMPTY 0xffffffff
57 #define ALIGN_INSTRUCTION(ptr) \
58 (sljit_uw*)(((sljit_uw)(ptr) + (CONST_POOL_ALIGNMENT * sizeof(sljit_uw)) - 1) & ~((CONST_POOL_ALIGNMENT * sizeof(sljit_uw)) - 1))
59 #define MAX_DIFFERENCE(max_diff) \
60 (((max_diff) / (sljit_s32)sizeof(sljit_uw)) - (CONST_POOL_ALIGNMENT - 1))
62 /* See sljit_emit_enter and sljit_emit_op0 if you want to change them. */
63 static const sljit_u8 reg_map[SLJIT_NUMBER_OF_REGISTERS + 5] = {
64 0, 0, 1, 2, 3, 11, 10, 9, 8, 7, 6, 5, 4, 13, 12, 14, 15
67 static const sljit_u8 freg_map[SLJIT_NUMBER_OF_FLOAT_REGISTERS + 3] = {
68 0, 0, 1, 2, 3, 4, 5, 15, 14, 13, 12, 11, 10, 9, 8, 6, 7
71 #define RM(rm) ((sljit_uw)reg_map[rm])
72 #define RM8(rm) ((sljit_uw)reg_map[rm] << 8)
73 #define RD(rd) ((sljit_uw)reg_map[rd] << 12)
74 #define RN(rn) ((sljit_uw)reg_map[rn] << 16)
76 #define VM(rm) ((sljit_uw)freg_map[rm])
77 #define VD(rd) ((sljit_uw)freg_map[rd] << 12)
78 #define VN(rn) ((sljit_uw)freg_map[rn] << 16)
80 /* --------------------------------------------------------------------- */
81 /* Instrucion forms */
82 /* --------------------------------------------------------------------- */
84 /* The instruction includes the AL condition.
85 INST_NAME - CONDITIONAL remove this flag. */
86 #define COND_MASK 0xf0000000
87 #define CONDITIONAL 0xe0000000
88 #define PUSH_POOL 0xff000000
90 #define ADC 0xe0a00000
91 #define ADD 0xe0800000
92 #define AND 0xe0000000
93 #define B 0xea000000
94 #define BIC 0xe1c00000
95 #define BL 0xeb000000
96 #define BLX 0xe12fff30
97 #define BX 0xe12fff10
98 #define CLZ 0xe16f0f10
99 #define CMN 0xe1600000
100 #define CMP 0xe1400000
101 #define BKPT 0xe1200070
102 #define EOR 0xe0200000
103 #define MOV 0xe1a00000
104 #define MUL 0xe0000090
105 #define MVN 0xe1e00000
106 #define NOP 0xe1a00000
107 #define ORR 0xe1800000
108 #define PUSH 0xe92d0000
109 #define POP 0xe8bd0000
110 #define RSB 0xe0600000
111 #define RSC 0xe0e00000
112 #define SBC 0xe0c00000
113 #define SMULL 0xe0c00090
114 #define SUB 0xe0400000
115 #define TST 0xe1000000
116 #define UMULL 0xe0800090
117 #define VABS_F32 0xeeb00ac0
118 #define VADD_F32 0xee300a00
119 #define VCMP_F32 0xeeb40a40
120 #define VCVT_F32_S32 0xeeb80ac0
121 #define VCVT_F64_F32 0xeeb70ac0
122 #define VCVT_S32_F32 0xeebd0ac0
123 #define VDIV_F32 0xee800a00
124 #define VLDR_F32 0xed100a00
125 #define VMOV_F32 0xeeb00a40
126 #define VMOV 0xee000a10
127 #define VMOV2 0xec400a10
128 #define VMRS 0xeef1fa10
129 #define VMUL_F32 0xee200a00
130 #define VNEG_F32 0xeeb10a40
131 #define VPOP 0xecbd0b00
132 #define VPUSH 0xed2d0b00
133 #define VSTR_F32 0xed000a00
134 #define VSUB_F32 0xee300a40
136 #if (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
137 /* Arm v7 specific instructions. */
138 #define MOVW 0xe3000000
139 #define MOVT 0xe3400000
140 #define SXTB 0xe6af0070
141 #define SXTH 0xe6bf0070
142 #define UXTB 0xe6ef0070
143 #define UXTH 0xe6ff0070
144 #endif
146 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
148 static sljit_s32 push_cpool(struct sljit_compiler *compiler)
150 /* Pushing the constant pool into the instruction stream. */
151 sljit_uw* inst;
152 sljit_uw* cpool_ptr;
153 sljit_uw* cpool_end;
154 sljit_s32 i;
156 /* The label could point the address after the constant pool. */
157 if (compiler->last_label && compiler->last_label->size == compiler->size)
158 compiler->last_label->size += compiler->cpool_fill + (CONST_POOL_ALIGNMENT - 1) + 1;
160 SLJIT_ASSERT(compiler->cpool_fill > 0 && compiler->cpool_fill <= CPOOL_SIZE);
161 inst = (sljit_uw*)ensure_buf(compiler, sizeof(sljit_uw));
162 FAIL_IF(!inst);
163 compiler->size++;
164 *inst = 0xff000000 | compiler->cpool_fill;
166 for (i = 0; i < CONST_POOL_ALIGNMENT - 1; i++) {
167 inst = (sljit_uw*)ensure_buf(compiler, sizeof(sljit_uw));
168 FAIL_IF(!inst);
169 compiler->size++;
170 *inst = 0;
173 cpool_ptr = compiler->cpool;
174 cpool_end = cpool_ptr + compiler->cpool_fill;
175 while (cpool_ptr < cpool_end) {
176 inst = (sljit_uw*)ensure_buf(compiler, sizeof(sljit_uw));
177 FAIL_IF(!inst);
178 compiler->size++;
179 *inst = *cpool_ptr++;
181 compiler->cpool_diff = CONST_POOL_EMPTY;
182 compiler->cpool_fill = 0;
183 return SLJIT_SUCCESS;
186 static sljit_s32 push_inst(struct sljit_compiler *compiler, sljit_uw inst)
188 sljit_uw* ptr;
190 if (SLJIT_UNLIKELY(compiler->cpool_diff != CONST_POOL_EMPTY && compiler->size - compiler->cpool_diff >= MAX_DIFFERENCE(4092)))
191 FAIL_IF(push_cpool(compiler));
193 ptr = (sljit_uw*)ensure_buf(compiler, sizeof(sljit_uw));
194 FAIL_IF(!ptr);
195 compiler->size++;
196 *ptr = inst;
197 return SLJIT_SUCCESS;
200 static sljit_s32 push_inst_with_literal(struct sljit_compiler *compiler, sljit_uw inst, sljit_uw literal)
202 sljit_uw* ptr;
203 sljit_uw cpool_index = CPOOL_SIZE;
204 sljit_uw* cpool_ptr;
205 sljit_uw* cpool_end;
206 sljit_u8* cpool_unique_ptr;
208 if (SLJIT_UNLIKELY(compiler->cpool_diff != CONST_POOL_EMPTY && compiler->size - compiler->cpool_diff >= MAX_DIFFERENCE(4092)))
209 FAIL_IF(push_cpool(compiler));
210 else if (compiler->cpool_fill > 0) {
211 cpool_ptr = compiler->cpool;
212 cpool_end = cpool_ptr + compiler->cpool_fill;
213 cpool_unique_ptr = compiler->cpool_unique;
214 do {
215 if ((*cpool_ptr == literal) && !(*cpool_unique_ptr)) {
216 cpool_index = (sljit_uw)(cpool_ptr - compiler->cpool);
217 break;
219 cpool_ptr++;
220 cpool_unique_ptr++;
221 } while (cpool_ptr < cpool_end);
224 if (cpool_index == CPOOL_SIZE) {
225 /* Must allocate a new entry in the literal pool. */
226 if (compiler->cpool_fill < CPOOL_SIZE) {
227 cpool_index = compiler->cpool_fill;
228 compiler->cpool_fill++;
230 else {
231 FAIL_IF(push_cpool(compiler));
232 cpool_index = 0;
233 compiler->cpool_fill = 1;
237 SLJIT_ASSERT((inst & 0xfff) == 0);
238 ptr = (sljit_uw*)ensure_buf(compiler, sizeof(sljit_uw));
239 FAIL_IF(!ptr);
240 compiler->size++;
241 *ptr = inst | cpool_index;
243 compiler->cpool[cpool_index] = literal;
244 compiler->cpool_unique[cpool_index] = 0;
245 if (compiler->cpool_diff == CONST_POOL_EMPTY)
246 compiler->cpool_diff = compiler->size;
247 return SLJIT_SUCCESS;
250 static sljit_s32 push_inst_with_unique_literal(struct sljit_compiler *compiler, sljit_uw inst, sljit_uw literal)
252 sljit_uw* ptr;
253 if (SLJIT_UNLIKELY((compiler->cpool_diff != CONST_POOL_EMPTY && compiler->size - compiler->cpool_diff >= MAX_DIFFERENCE(4092)) || compiler->cpool_fill >= CPOOL_SIZE))
254 FAIL_IF(push_cpool(compiler));
256 SLJIT_ASSERT(compiler->cpool_fill < CPOOL_SIZE && (inst & 0xfff) == 0);
257 ptr = (sljit_uw*)ensure_buf(compiler, sizeof(sljit_uw));
258 FAIL_IF(!ptr);
259 compiler->size++;
260 *ptr = inst | compiler->cpool_fill;
262 compiler->cpool[compiler->cpool_fill] = literal;
263 compiler->cpool_unique[compiler->cpool_fill] = 1;
264 compiler->cpool_fill++;
265 if (compiler->cpool_diff == CONST_POOL_EMPTY)
266 compiler->cpool_diff = compiler->size;
267 return SLJIT_SUCCESS;
270 static SLJIT_INLINE sljit_s32 prepare_blx(struct sljit_compiler *compiler)
272 /* Place for at least two instruction (doesn't matter whether the first has a literal). */
273 if (SLJIT_UNLIKELY(compiler->cpool_diff != CONST_POOL_EMPTY && compiler->size - compiler->cpool_diff >= MAX_DIFFERENCE(4088)))
274 return push_cpool(compiler);
275 return SLJIT_SUCCESS;
278 static SLJIT_INLINE sljit_s32 emit_blx(struct sljit_compiler *compiler)
280 /* Must follow tightly the previous instruction (to be able to convert it to bl instruction). */
281 SLJIT_ASSERT(compiler->cpool_diff == CONST_POOL_EMPTY || compiler->size - compiler->cpool_diff < MAX_DIFFERENCE(4092));
282 SLJIT_ASSERT(reg_map[TMP_REG1] != 14);
284 return push_inst(compiler, BLX | RM(TMP_REG1));
287 static sljit_uw patch_pc_relative_loads(sljit_uw *last_pc_patch, sljit_uw *code_ptr, sljit_uw* const_pool, sljit_uw cpool_size)
289 sljit_uw diff;
290 sljit_uw ind;
291 sljit_uw counter = 0;
292 sljit_uw* clear_const_pool = const_pool;
293 sljit_uw* clear_const_pool_end = const_pool + cpool_size;
295 SLJIT_ASSERT(const_pool - code_ptr <= CONST_POOL_ALIGNMENT);
296 /* Set unused flag for all literals in the constant pool.
297 I.e.: unused literals can belong to branches, which can be encoded as B or BL.
298 We can "compress" the constant pool by discarding these literals. */
299 while (clear_const_pool < clear_const_pool_end)
300 *clear_const_pool++ = (sljit_uw)(-1);
302 while (last_pc_patch < code_ptr) {
303 /* Data transfer instruction with Rn == r15. */
304 if ((*last_pc_patch & 0x0c0f0000) == 0x040f0000) {
305 diff = (sljit_uw)(const_pool - last_pc_patch);
306 ind = (*last_pc_patch) & 0xfff;
308 /* Must be a load instruction with immediate offset. */
309 SLJIT_ASSERT(ind < cpool_size && !(*last_pc_patch & (1 << 25)) && (*last_pc_patch & (1 << 20)));
310 if ((sljit_s32)const_pool[ind] < 0) {
311 const_pool[ind] = counter;
312 ind = counter;
313 counter++;
315 else
316 ind = const_pool[ind];
318 SLJIT_ASSERT(diff >= 1);
319 if (diff >= 2 || ind > 0) {
320 diff = (diff + (sljit_uw)ind - 2) << 2;
321 SLJIT_ASSERT(diff <= 0xfff);
322 *last_pc_patch = (*last_pc_patch & ~(sljit_uw)0xfff) | diff;
324 else
325 *last_pc_patch = (*last_pc_patch & ~(sljit_uw)(0xfff | (1 << 23))) | 0x004;
327 last_pc_patch++;
329 return counter;
332 /* In some rare ocasions we may need future patches. The probability is close to 0 in practice. */
333 struct future_patch {
334 struct future_patch* next;
335 sljit_s32 index;
336 sljit_s32 value;
339 static sljit_s32 resolve_const_pool_index(struct sljit_compiler *compiler, struct future_patch **first_patch, sljit_uw cpool_current_index, sljit_uw *cpool_start_address, sljit_uw *buf_ptr)
341 sljit_u32 value;
342 struct future_patch *curr_patch, *prev_patch;
344 SLJIT_UNUSED_ARG(compiler);
346 /* Using the values generated by patch_pc_relative_loads. */
347 if (!*first_patch)
348 value = cpool_start_address[cpool_current_index];
349 else {
350 curr_patch = *first_patch;
351 prev_patch = NULL;
352 while (1) {
353 if (!curr_patch) {
354 value = cpool_start_address[cpool_current_index];
355 break;
357 if ((sljit_uw)curr_patch->index == cpool_current_index) {
358 value = (sljit_uw)curr_patch->value;
359 if (prev_patch)
360 prev_patch->next = curr_patch->next;
361 else
362 *first_patch = curr_patch->next;
363 SLJIT_FREE(curr_patch, compiler->allocator_data);
364 break;
366 prev_patch = curr_patch;
367 curr_patch = curr_patch->next;
371 if ((sljit_sw)value >= 0) {
372 if (value > cpool_current_index) {
373 curr_patch = (struct future_patch*)SLJIT_MALLOC(sizeof(struct future_patch), compiler->allocator_data);
374 if (!curr_patch) {
375 while (*first_patch) {
376 curr_patch = *first_patch;
377 *first_patch = (*first_patch)->next;
378 SLJIT_FREE(curr_patch, compiler->allocator_data);
380 return SLJIT_ERR_ALLOC_FAILED;
382 curr_patch->next = *first_patch;
383 curr_patch->index = (sljit_sw)value;
384 curr_patch->value = (sljit_sw)cpool_start_address[value];
385 *first_patch = curr_patch;
387 cpool_start_address[value] = *buf_ptr;
389 return SLJIT_SUCCESS;
392 #else
394 static sljit_s32 push_inst(struct sljit_compiler *compiler, sljit_uw inst)
396 sljit_uw* ptr;
398 ptr = (sljit_uw*)ensure_buf(compiler, sizeof(sljit_uw));
399 FAIL_IF(!ptr);
400 compiler->size++;
401 *ptr = inst;
402 return SLJIT_SUCCESS;
405 static SLJIT_INLINE sljit_s32 emit_imm(struct sljit_compiler *compiler, sljit_s32 reg, sljit_sw imm)
407 FAIL_IF(push_inst(compiler, MOVW | RD(reg) | ((imm << 4) & 0xf0000) | ((sljit_u32)imm & 0xfff)));
408 return push_inst(compiler, MOVT | RD(reg) | ((imm >> 12) & 0xf0000) | (((sljit_u32)imm >> 16) & 0xfff));
411 #endif
413 static SLJIT_INLINE sljit_s32 detect_jump_type(struct sljit_jump *jump, sljit_uw *code_ptr, sljit_uw *code, sljit_sw executable_offset)
415 sljit_sw diff;
417 if (jump->flags & SLJIT_REWRITABLE_JUMP)
418 return 0;
420 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
421 if (jump->flags & IS_BL)
422 code_ptr--;
424 if (jump->flags & JUMP_ADDR)
425 diff = ((sljit_sw)jump->u.target - (sljit_sw)(code_ptr + 2) - executable_offset);
426 else {
427 SLJIT_ASSERT(jump->flags & JUMP_LABEL);
428 diff = ((sljit_sw)(code + jump->u.label->size) - (sljit_sw)(code_ptr + 2));
431 /* Branch to Thumb code has not been optimized yet. */
432 if (diff & 0x3)
433 return 0;
435 if (jump->flags & IS_BL) {
436 if (diff <= 0x01ffffff && diff >= -0x02000000) {
437 *code_ptr = (BL - CONDITIONAL) | (*(code_ptr + 1) & COND_MASK);
438 jump->flags |= PATCH_B;
439 return 1;
442 else {
443 if (diff <= 0x01ffffff && diff >= -0x02000000) {
444 *code_ptr = (B - CONDITIONAL) | (*code_ptr & COND_MASK);
445 jump->flags |= PATCH_B;
448 #else
449 if (jump->flags & JUMP_ADDR)
450 diff = ((sljit_sw)jump->u.target - (sljit_sw)code_ptr - executable_offset);
451 else {
452 SLJIT_ASSERT(jump->flags & JUMP_LABEL);
453 diff = ((sljit_sw)(code + jump->u.label->size) - (sljit_sw)code_ptr);
456 /* Branch to Thumb code has not been optimized yet. */
457 if (diff & 0x3)
458 return 0;
460 if (diff <= 0x01ffffff && diff >= -0x02000000) {
461 code_ptr -= 2;
462 *code_ptr = ((jump->flags & IS_BL) ? (BL - CONDITIONAL) : (B - CONDITIONAL)) | (code_ptr[2] & COND_MASK);
463 jump->flags |= PATCH_B;
464 return 1;
466 #endif
467 return 0;
470 static SLJIT_INLINE void inline_set_jump_addr(sljit_uw jump_ptr, sljit_sw executable_offset, sljit_uw new_addr, sljit_s32 flush_cache)
472 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
473 sljit_uw *ptr = (sljit_uw *)jump_ptr;
474 sljit_uw *inst = (sljit_uw *)ptr[0];
475 sljit_uw mov_pc = ptr[1];
476 sljit_s32 bl = (mov_pc & 0x0000f000) != RD(TMP_PC);
477 sljit_sw diff = (sljit_sw)(((sljit_sw)new_addr - (sljit_sw)(inst + 2) - executable_offset) >> 2);
479 SLJIT_UNUSED_ARG(executable_offset);
481 if (diff <= 0x7fffff && diff >= -0x800000) {
482 /* Turn to branch. */
483 if (!bl) {
484 if (flush_cache) {
485 SLJIT_UPDATE_WX_FLAGS(inst, inst + 1, 0);
487 inst[0] = (mov_pc & COND_MASK) | (B - CONDITIONAL) | (diff & 0xffffff);
488 if (flush_cache) {
489 SLJIT_UPDATE_WX_FLAGS(inst, inst + 1, 1);
490 inst = (sljit_uw *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
491 SLJIT_CACHE_FLUSH(inst, inst + 1);
493 } else {
494 if (flush_cache) {
495 SLJIT_UPDATE_WX_FLAGS(inst, inst + 2, 0);
497 inst[0] = (mov_pc & COND_MASK) | (BL - CONDITIONAL) | (diff & 0xffffff);
498 inst[1] = NOP;
499 if (flush_cache) {
500 SLJIT_UPDATE_WX_FLAGS(inst, inst + 2, 1);
501 inst = (sljit_uw *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
502 SLJIT_CACHE_FLUSH(inst, inst + 2);
505 } else {
506 /* Get the position of the constant. */
507 if (mov_pc & (1 << 23))
508 ptr = inst + ((mov_pc & 0xfff) >> 2) + 2;
509 else
510 ptr = inst + 1;
512 if (*inst != mov_pc) {
513 if (flush_cache) {
514 SLJIT_UPDATE_WX_FLAGS(inst, inst + (!bl ? 1 : 2), 0);
516 inst[0] = mov_pc;
517 if (!bl) {
518 if (flush_cache) {
519 SLJIT_UPDATE_WX_FLAGS(inst, inst + 1, 1);
520 inst = (sljit_uw *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
521 SLJIT_CACHE_FLUSH(inst, inst + 1);
523 } else {
524 inst[1] = BLX | RM(TMP_REG1);
525 if (flush_cache) {
526 SLJIT_UPDATE_WX_FLAGS(inst, inst + 2, 1);
527 inst = (sljit_uw *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
528 SLJIT_CACHE_FLUSH(inst, inst + 2);
533 if (flush_cache) {
534 SLJIT_UPDATE_WX_FLAGS(ptr, ptr + 1, 0);
537 *ptr = new_addr;
539 if (flush_cache) {
540 SLJIT_UPDATE_WX_FLAGS(ptr, ptr + 1, 1);
543 #else
544 sljit_uw *inst = (sljit_uw*)jump_ptr;
546 SLJIT_UNUSED_ARG(executable_offset);
548 SLJIT_ASSERT((inst[0] & 0xfff00000) == MOVW && (inst[1] & 0xfff00000) == MOVT);
550 if (flush_cache) {
551 SLJIT_UPDATE_WX_FLAGS(inst, inst + 2, 0);
554 inst[0] = MOVW | (inst[0] & 0xf000) | ((new_addr << 4) & 0xf0000) | (new_addr & 0xfff);
555 inst[1] = MOVT | (inst[1] & 0xf000) | ((new_addr >> 12) & 0xf0000) | ((new_addr >> 16) & 0xfff);
557 if (flush_cache) {
558 SLJIT_UPDATE_WX_FLAGS(inst, inst + 2, 1);
559 inst = (sljit_uw *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
560 SLJIT_CACHE_FLUSH(inst, inst + 2);
562 #endif
565 static sljit_uw get_imm(sljit_uw imm);
566 static sljit_s32 load_immediate(struct sljit_compiler *compiler, sljit_s32 reg, sljit_uw imm);
568 static SLJIT_INLINE void inline_set_const(sljit_uw addr, sljit_sw executable_offset, sljit_uw new_constant, sljit_s32 flush_cache)
570 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
571 sljit_uw *ptr = (sljit_uw*)addr;
572 sljit_uw *inst = (sljit_uw*)ptr[0];
573 sljit_uw ldr_literal = ptr[1];
574 sljit_uw src2;
576 SLJIT_UNUSED_ARG(executable_offset);
578 src2 = get_imm(new_constant);
579 if (src2) {
580 if (flush_cache) {
581 SLJIT_UPDATE_WX_FLAGS(inst, inst + 1, 0);
584 *inst = 0xe3a00000 | (ldr_literal & 0xf000) | src2;
586 if (flush_cache) {
587 SLJIT_UPDATE_WX_FLAGS(inst, inst + 1, 1);
588 inst = (sljit_uw *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
589 SLJIT_CACHE_FLUSH(inst, inst + 1);
591 return;
594 src2 = get_imm(~new_constant);
595 if (src2) {
596 if (flush_cache) {
597 SLJIT_UPDATE_WX_FLAGS(inst, inst + 1, 0);
600 *inst = 0xe3e00000 | (ldr_literal & 0xf000) | src2;
602 if (flush_cache) {
603 SLJIT_UPDATE_WX_FLAGS(inst, inst + 1, 1);
604 inst = (sljit_uw *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
605 SLJIT_CACHE_FLUSH(inst, inst + 1);
607 return;
610 if (ldr_literal & (1 << 23))
611 ptr = inst + ((ldr_literal & 0xfff) >> 2) + 2;
612 else
613 ptr = inst + 1;
615 if (*inst != ldr_literal) {
616 if (flush_cache) {
617 SLJIT_UPDATE_WX_FLAGS(inst, inst + 1, 0);
620 *inst = ldr_literal;
622 if (flush_cache) {
623 SLJIT_UPDATE_WX_FLAGS(inst, inst + 1, 1);
624 inst = (sljit_uw *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
625 SLJIT_CACHE_FLUSH(inst, inst + 1);
629 if (flush_cache) {
630 SLJIT_UPDATE_WX_FLAGS(ptr, ptr + 1, 0);
633 *ptr = new_constant;
635 if (flush_cache) {
636 SLJIT_UPDATE_WX_FLAGS(ptr, ptr + 1, 1);
638 #else
639 sljit_uw *inst = (sljit_uw*)addr;
641 SLJIT_UNUSED_ARG(executable_offset);
643 SLJIT_ASSERT((inst[0] & 0xfff00000) == MOVW && (inst[1] & 0xfff00000) == MOVT);
645 if (flush_cache) {
646 SLJIT_UPDATE_WX_FLAGS(inst, inst + 2, 0);
649 inst[0] = MOVW | (inst[0] & 0xf000) | ((new_constant << 4) & 0xf0000) | (new_constant & 0xfff);
650 inst[1] = MOVT | (inst[1] & 0xf000) | ((new_constant >> 12) & 0xf0000) | ((new_constant >> 16) & 0xfff);
652 if (flush_cache) {
653 SLJIT_UPDATE_WX_FLAGS(inst, inst + 2, 1);
654 inst = (sljit_uw *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
655 SLJIT_CACHE_FLUSH(inst, inst + 2);
657 #endif
660 SLJIT_API_FUNC_ATTRIBUTE void* sljit_generate_code(struct sljit_compiler *compiler)
662 struct sljit_memory_fragment *buf;
663 sljit_uw *code;
664 sljit_uw *code_ptr;
665 sljit_uw *buf_ptr;
666 sljit_uw *buf_end;
667 sljit_uw size;
668 sljit_uw word_count;
669 sljit_uw next_addr;
670 sljit_sw executable_offset;
671 sljit_uw addr;
672 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
673 sljit_uw cpool_size;
674 sljit_uw cpool_skip_alignment;
675 sljit_uw cpool_current_index;
676 sljit_uw *cpool_start_address;
677 sljit_uw *last_pc_patch;
678 struct future_patch *first_patch;
679 #endif
681 struct sljit_label *label;
682 struct sljit_jump *jump;
683 struct sljit_const *const_;
684 struct sljit_put_label *put_label;
686 CHECK_ERROR_PTR();
687 CHECK_PTR(check_sljit_generate_code(compiler));
688 reverse_buf(compiler);
690 /* Second code generation pass. */
691 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
692 size = compiler->size + (compiler->patches << 1);
693 if (compiler->cpool_fill > 0)
694 size += compiler->cpool_fill + CONST_POOL_ALIGNMENT - 1;
695 #else
696 size = compiler->size;
697 #endif
698 code = (sljit_uw*)SLJIT_MALLOC_EXEC(size * sizeof(sljit_uw), compiler->exec_allocator_data);
699 PTR_FAIL_WITH_EXEC_IF(code);
700 buf = compiler->buf;
702 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
703 cpool_size = 0;
704 cpool_skip_alignment = 0;
705 cpool_current_index = 0;
706 cpool_start_address = NULL;
707 first_patch = NULL;
708 last_pc_patch = code;
709 #endif
711 code_ptr = code;
712 word_count = 0;
713 next_addr = 1;
714 executable_offset = SLJIT_EXEC_OFFSET(code);
716 label = compiler->labels;
717 jump = compiler->jumps;
718 const_ = compiler->consts;
719 put_label = compiler->put_labels;
721 if (label && label->size == 0) {
722 label->addr = (sljit_uw)SLJIT_ADD_EXEC_OFFSET(code, executable_offset);
723 label = label->next;
726 do {
727 buf_ptr = (sljit_uw*)buf->memory;
728 buf_end = buf_ptr + (buf->used_size >> 2);
729 do {
730 word_count++;
731 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
732 if (cpool_size > 0) {
733 if (cpool_skip_alignment > 0) {
734 buf_ptr++;
735 cpool_skip_alignment--;
737 else {
738 if (SLJIT_UNLIKELY(resolve_const_pool_index(compiler, &first_patch, cpool_current_index, cpool_start_address, buf_ptr))) {
739 SLJIT_FREE_EXEC(code, compiler->exec_allocator_data);
740 compiler->error = SLJIT_ERR_ALLOC_FAILED;
741 return NULL;
743 buf_ptr++;
744 if (++cpool_current_index >= cpool_size) {
745 SLJIT_ASSERT(!first_patch);
746 cpool_size = 0;
747 if (label && label->size == word_count) {
748 /* Points after the current instruction. */
749 label->addr = (sljit_uw)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset);
750 label->size = (sljit_uw)(code_ptr - code);
751 label = label->next;
753 next_addr = compute_next_addr(label, jump, const_, put_label);
758 else if ((*buf_ptr & 0xff000000) != PUSH_POOL) {
759 #endif
760 *code_ptr = *buf_ptr++;
761 if (next_addr == word_count) {
762 SLJIT_ASSERT(!label || label->size >= word_count);
763 SLJIT_ASSERT(!jump || jump->addr >= word_count);
764 SLJIT_ASSERT(!const_ || const_->addr >= word_count);
765 SLJIT_ASSERT(!put_label || put_label->addr >= word_count);
767 /* These structures are ordered by their address. */
768 if (jump && jump->addr == word_count) {
769 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
770 if (detect_jump_type(jump, code_ptr, code, executable_offset))
771 code_ptr--;
772 jump->addr = (sljit_uw)code_ptr;
773 #else
774 jump->addr = (sljit_uw)(code_ptr - 2);
775 if (detect_jump_type(jump, code_ptr, code, executable_offset))
776 code_ptr -= 2;
777 #endif
778 jump = jump->next;
780 if (label && label->size == word_count) {
781 /* code_ptr can be affected above. */
782 label->addr = (sljit_uw)SLJIT_ADD_EXEC_OFFSET(code_ptr + 1, executable_offset);
783 label->size = (sljit_uw)((code_ptr + 1) - code);
784 label = label->next;
786 if (const_ && const_->addr == word_count) {
787 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
788 const_->addr = (sljit_uw)code_ptr;
789 #else
790 const_->addr = (sljit_uw)(code_ptr - 1);
791 #endif
792 const_ = const_->next;
794 if (put_label && put_label->addr == word_count) {
795 SLJIT_ASSERT(put_label->label);
796 put_label->addr = (sljit_uw)code_ptr;
797 put_label = put_label->next;
799 next_addr = compute_next_addr(label, jump, const_, put_label);
801 code_ptr++;
802 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
804 else {
805 /* Fortunately, no need to shift. */
806 cpool_size = *buf_ptr++ & ~PUSH_POOL;
807 SLJIT_ASSERT(cpool_size > 0);
808 cpool_start_address = ALIGN_INSTRUCTION(code_ptr + 1);
809 cpool_current_index = patch_pc_relative_loads(last_pc_patch, code_ptr, cpool_start_address, cpool_size);
810 if (cpool_current_index > 0) {
811 /* Unconditional branch. */
812 *code_ptr = B | (((sljit_uw)(cpool_start_address - code_ptr) + cpool_current_index - 2) & ~PUSH_POOL);
813 code_ptr = (sljit_uw*)(cpool_start_address + cpool_current_index);
815 cpool_skip_alignment = CONST_POOL_ALIGNMENT - 1;
816 cpool_current_index = 0;
817 last_pc_patch = code_ptr;
819 #endif
820 } while (buf_ptr < buf_end);
821 buf = buf->next;
822 } while (buf);
824 SLJIT_ASSERT(!label);
825 SLJIT_ASSERT(!jump);
826 SLJIT_ASSERT(!const_);
827 SLJIT_ASSERT(!put_label);
829 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
830 SLJIT_ASSERT(cpool_size == 0);
831 if (compiler->cpool_fill > 0) {
832 cpool_start_address = ALIGN_INSTRUCTION(code_ptr);
833 cpool_current_index = patch_pc_relative_loads(last_pc_patch, code_ptr, cpool_start_address, compiler->cpool_fill);
834 if (cpool_current_index > 0)
835 code_ptr = (sljit_uw*)(cpool_start_address + cpool_current_index);
837 buf_ptr = compiler->cpool;
838 buf_end = buf_ptr + compiler->cpool_fill;
839 cpool_current_index = 0;
840 while (buf_ptr < buf_end) {
841 if (SLJIT_UNLIKELY(resolve_const_pool_index(compiler, &first_patch, cpool_current_index, cpool_start_address, buf_ptr))) {
842 SLJIT_FREE_EXEC(code, compiler->exec_allocator_data);
843 compiler->error = SLJIT_ERR_ALLOC_FAILED;
844 return NULL;
846 buf_ptr++;
847 cpool_current_index++;
849 SLJIT_ASSERT(!first_patch);
851 #endif
853 jump = compiler->jumps;
854 while (jump) {
855 buf_ptr = (sljit_uw *)jump->addr;
857 if (jump->flags & PATCH_B) {
858 addr = (sljit_uw)SLJIT_ADD_EXEC_OFFSET(buf_ptr + 2, executable_offset);
859 if (!(jump->flags & JUMP_ADDR)) {
860 SLJIT_ASSERT(jump->flags & JUMP_LABEL);
861 SLJIT_ASSERT((sljit_sw)(jump->u.label->addr - addr) <= 0x01ffffff && (sljit_sw)(jump->u.label->addr - addr) >= -0x02000000);
862 *buf_ptr |= ((jump->u.label->addr - addr) >> 2) & 0x00ffffff;
864 else {
865 SLJIT_ASSERT((sljit_sw)(jump->u.target - addr) <= 0x01ffffff && (sljit_sw)(jump->u.target - addr) >= -0x02000000);
866 *buf_ptr |= ((jump->u.target - addr) >> 2) & 0x00ffffff;
869 else if (jump->flags & SLJIT_REWRITABLE_JUMP) {
870 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
871 jump->addr = (sljit_uw)code_ptr;
872 code_ptr[0] = (sljit_uw)buf_ptr;
873 code_ptr[1] = *buf_ptr;
874 inline_set_jump_addr((sljit_uw)code_ptr, executable_offset, (jump->flags & JUMP_LABEL) ? jump->u.label->addr : jump->u.target, 0);
875 code_ptr += 2;
876 #else
877 inline_set_jump_addr((sljit_uw)buf_ptr, executable_offset, (jump->flags & JUMP_LABEL) ? jump->u.label->addr : jump->u.target, 0);
878 #endif
880 else {
881 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
882 if (jump->flags & IS_BL)
883 buf_ptr--;
884 if (*buf_ptr & (1 << 23))
885 buf_ptr += ((*buf_ptr & 0xfff) >> 2) + 2;
886 else
887 buf_ptr += 1;
888 *buf_ptr = (jump->flags & JUMP_LABEL) ? jump->u.label->addr : jump->u.target;
889 #else
890 inline_set_jump_addr((sljit_uw)buf_ptr, executable_offset, (jump->flags & JUMP_LABEL) ? jump->u.label->addr : jump->u.target, 0);
891 #endif
893 jump = jump->next;
896 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
897 const_ = compiler->consts;
898 while (const_) {
899 buf_ptr = (sljit_uw*)const_->addr;
900 const_->addr = (sljit_uw)code_ptr;
902 code_ptr[0] = (sljit_uw)buf_ptr;
903 code_ptr[1] = *buf_ptr;
904 if (*buf_ptr & (1 << 23))
905 buf_ptr += ((*buf_ptr & 0xfff) >> 2) + 2;
906 else
907 buf_ptr += 1;
908 /* Set the value again (can be a simple constant). */
909 inline_set_const((sljit_uw)code_ptr, executable_offset, *buf_ptr, 0);
910 code_ptr += 2;
912 const_ = const_->next;
914 #endif
916 put_label = compiler->put_labels;
917 while (put_label) {
918 addr = put_label->label->addr;
919 buf_ptr = (sljit_uw*)put_label->addr;
921 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
922 SLJIT_ASSERT((buf_ptr[0] & 0xffff0000) == 0xe59f0000);
923 buf_ptr[((buf_ptr[0] & 0xfff) >> 2) + 2] = addr;
924 #else
925 SLJIT_ASSERT((buf_ptr[-1] & 0xfff00000) == MOVW && (buf_ptr[0] & 0xfff00000) == MOVT);
926 buf_ptr[-1] |= ((addr << 4) & 0xf0000) | (addr & 0xfff);
927 buf_ptr[0] |= ((addr >> 12) & 0xf0000) | ((addr >> 16) & 0xfff);
928 #endif
929 put_label = put_label->next;
932 SLJIT_ASSERT(code_ptr - code <= (sljit_s32)size);
934 compiler->error = SLJIT_ERR_COMPILED;
935 compiler->executable_offset = executable_offset;
936 compiler->executable_size = (sljit_uw)(code_ptr - code) * sizeof(sljit_uw);
938 code = (sljit_uw *)SLJIT_ADD_EXEC_OFFSET(code, executable_offset);
939 code_ptr = (sljit_uw *)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset);
941 SLJIT_CACHE_FLUSH(code, code_ptr);
942 SLJIT_UPDATE_WX_FLAGS(code, code_ptr, 1);
943 return code;
946 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_has_cpu_feature(sljit_s32 feature_type)
948 switch (feature_type) {
949 case SLJIT_HAS_FPU:
950 #ifdef SLJIT_IS_FPU_AVAILABLE
951 return SLJIT_IS_FPU_AVAILABLE;
952 #else
953 /* Available by default. */
954 return 1;
955 #endif
957 case SLJIT_HAS_CLZ:
958 case SLJIT_HAS_CMOV:
959 #if (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
960 case SLJIT_HAS_PREFETCH:
961 #endif
962 return 1;
964 default:
965 return 0;
969 /* --------------------------------------------------------------------- */
970 /* Entry, exit */
971 /* --------------------------------------------------------------------- */
973 /* Creates an index in data_transfer_insts array. */
974 #define WORD_SIZE 0x00
975 #define BYTE_SIZE 0x01
976 #define HALF_SIZE 0x02
977 #define PRELOAD 0x03
978 #define SIGNED 0x04
979 #define LOAD_DATA 0x08
981 /* Flag bits for emit_op. */
982 #define ALLOW_IMM 0x10
983 #define ALLOW_INV_IMM 0x20
984 #define ALLOW_ANY_IMM (ALLOW_IMM | ALLOW_INV_IMM)
985 #define ALLOW_NEG_IMM 0x40
987 /* s/l - store/load (1 bit)
988 u/s - signed/unsigned (1 bit)
989 w/b/h/N - word/byte/half/NOT allowed (2 bit)
990 Storing signed and unsigned values are the same operations. */
992 static const sljit_uw data_transfer_insts[16] = {
993 /* s u w */ 0xe5000000 /* str */,
994 /* s u b */ 0xe5400000 /* strb */,
995 /* s u h */ 0xe10000b0 /* strh */,
996 /* s u N */ 0x00000000 /* not allowed */,
997 /* s s w */ 0xe5000000 /* str */,
998 /* s s b */ 0xe5400000 /* strb */,
999 /* s s h */ 0xe10000b0 /* strh */,
1000 /* s s N */ 0x00000000 /* not allowed */,
1002 /* l u w */ 0xe5100000 /* ldr */,
1003 /* l u b */ 0xe5500000 /* ldrb */,
1004 /* l u h */ 0xe11000b0 /* ldrh */,
1005 /* l u p */ 0xf5500000 /* preload */,
1006 /* l s w */ 0xe5100000 /* ldr */,
1007 /* l s b */ 0xe11000d0 /* ldrsb */,
1008 /* l s h */ 0xe11000f0 /* ldrsh */,
1009 /* l s N */ 0x00000000 /* not allowed */,
1012 #define EMIT_DATA_TRANSFER(type, add, target_reg, base_reg, arg) \
1013 (data_transfer_insts[(type) & 0xf] | ((add) << 23) | RD(target_reg) | RN(base_reg) | (sljit_uw)(arg))
1015 /* Normal ldr/str instruction.
1016 Type2: ldrsb, ldrh, ldrsh */
1017 #define IS_TYPE1_TRANSFER(type) \
1018 (data_transfer_insts[(type) & 0xf] & 0x04000000)
1019 #define TYPE2_TRANSFER_IMM(imm) \
1020 (((imm) & 0xf) | (((imm) & 0xf0) << 4) | (1 << 22))
1022 #define EMIT_FPU_OPERATION(opcode, mode, dst, src1, src2) \
1023 ((sljit_uw)(opcode) | (sljit_uw)(mode) | VD(dst) | VM(src1) | VN(src2))
1025 /* Flags for emit_op: */
1026 /* Arguments are swapped. */
1027 #define ARGS_SWAPPED 0x01
1028 /* Inverted immediate. */
1029 #define INV_IMM 0x02
1030 /* Source and destination is register. */
1031 #define MOVE_REG_CONV 0x04
1032 /* Unused return value. */
1033 #define UNUSED_RETURN 0x08
1034 /* SET_FLAGS must be (1 << 20) as it is also the value of S bit (can be used for optimization). */
1035 #define SET_FLAGS (1 << 20)
1036 /* dst: reg
1037 src1: reg
1038 src2: reg or imm (if allowed)
1039 SRC2_IMM must be (1 << 25) as it is also the value of I bit (can be used for optimization). */
1040 #define SRC2_IMM (1 << 25)
1042 static sljit_s32 emit_op(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 inp_flags,
1043 sljit_s32 dst, sljit_sw dstw,
1044 sljit_s32 src1, sljit_sw src1w,
1045 sljit_s32 src2, sljit_sw src2w);
1047 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_enter(struct sljit_compiler *compiler,
1048 sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
1049 sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
1051 sljit_uw imm, offset;
1052 sljit_s32 i, tmp, size, word_arg_count, saved_arg_count;
1053 #ifdef __SOFTFP__
1054 sljit_u32 float_arg_count;
1055 #else
1056 sljit_u32 old_offset, f32_offset;
1057 sljit_u32 remap[3];
1058 sljit_u32 *remap_ptr = remap;
1059 #endif
1061 CHECK_ERROR();
1062 CHECK(check_sljit_emit_enter(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size));
1063 set_emit_enter(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size);
1065 imm = 0;
1067 tmp = SLJIT_S0 - saveds;
1068 for (i = SLJIT_S0; i > tmp; i--)
1069 imm |= (sljit_uw)1 << reg_map[i];
1071 for (i = scratches; i >= SLJIT_FIRST_SAVED_REG; i--)
1072 imm |= (sljit_uw)1 << reg_map[i];
1074 SLJIT_ASSERT(reg_map[TMP_REG2] == 14);
1076 /* Push saved and temporary registers
1077 multiple registers: stmdb sp!, {..., lr}
1078 single register: str reg, [sp, #-4]! */
1079 if (imm != 0)
1080 FAIL_IF(push_inst(compiler, PUSH | (1 << 14) | imm));
1081 else
1082 FAIL_IF(push_inst(compiler, 0xe52d0004 | RD(TMP_REG2)));
1084 /* Stack must be aligned to 8 bytes: */
1085 size = GET_SAVED_REGISTERS_SIZE(scratches, saveds, 1);
1087 if (fsaveds > 0 || fscratches >= SLJIT_FIRST_SAVED_FLOAT_REG) {
1088 if ((size & SSIZE_OF(sw)) != 0) {
1089 FAIL_IF(push_inst(compiler, SUB | RD(SLJIT_SP) | RN(SLJIT_SP) | SRC2_IMM | sizeof(sljit_sw)));
1090 size += SSIZE_OF(sw);
1093 if (fsaveds + fscratches >= SLJIT_NUMBER_OF_FLOAT_REGISTERS) {
1094 FAIL_IF(push_inst(compiler, VPUSH | VD(SLJIT_FS0) | ((sljit_uw)SLJIT_NUMBER_OF_SAVED_FLOAT_REGISTERS << 1)));
1095 } else {
1096 if (fsaveds > 0)
1097 FAIL_IF(push_inst(compiler, VPUSH | VD(SLJIT_FS0) | ((sljit_uw)fsaveds << 1)));
1098 if (fscratches >= SLJIT_FIRST_SAVED_FLOAT_REG)
1099 FAIL_IF(push_inst(compiler, VPUSH | VD(fscratches) | ((sljit_uw)(fscratches - (SLJIT_FIRST_SAVED_FLOAT_REG - 1)) << 1)));
1103 local_size = ((size + local_size + 0x7) & ~0x7) - size;
1104 compiler->local_size = local_size;
1106 arg_types >>= SLJIT_ARG_SHIFT;
1107 word_arg_count = 0;
1108 saved_arg_count = 0;
1109 #ifdef __SOFTFP__
1110 SLJIT_COMPILE_ASSERT(SLJIT_FR0 == 1, float_register_index_start);
1112 offset = 0;
1113 float_arg_count = 0;
1115 while (arg_types) {
1116 switch (arg_types & SLJIT_ARG_MASK) {
1117 case SLJIT_ARG_TYPE_F64:
1118 if (offset & 0x7)
1119 offset += sizeof(sljit_sw);
1121 if (offset < 4 * sizeof(sljit_sw))
1122 FAIL_IF(push_inst(compiler, VMOV2 | (offset << 10) | ((offset + sizeof(sljit_sw)) << 14) | float_arg_count));
1123 else
1124 FAIL_IF(push_inst(compiler, VLDR_F32 | 0x800100 | RN(SLJIT_SP)
1125 | (float_arg_count << 12) | ((offset + (sljit_uw)size - 4 * sizeof(sljit_sw)) >> 2)));
1126 float_arg_count++;
1127 offset += sizeof(sljit_f64) - sizeof(sljit_sw);
1128 break;
1129 case SLJIT_ARG_TYPE_F32:
1130 if (offset < 4 * sizeof(sljit_sw))
1131 FAIL_IF(push_inst(compiler, VMOV | (float_arg_count << 16) | (offset << 10)));
1132 else
1133 FAIL_IF(push_inst(compiler, VLDR_F32 | 0x800000 | RN(SLJIT_SP)
1134 | (float_arg_count << 12) | ((offset + (sljit_uw)size - 4 * sizeof(sljit_sw)) >> 2)));
1135 float_arg_count++;
1136 break;
1137 default:
1138 word_arg_count++;
1140 if (!(arg_types & SLJIT_ARG_TYPE_SCRATCH_REG)) {
1141 tmp = SLJIT_S0 - saved_arg_count;
1142 saved_arg_count++;
1143 } else if (word_arg_count - 1 != (sljit_s32)(offset >> 2))
1144 tmp = word_arg_count;
1145 else
1146 break;
1148 if (offset < 4 * sizeof(sljit_sw))
1149 FAIL_IF(push_inst(compiler, MOV | RD(tmp) | (offset >> 2)));
1150 else
1151 FAIL_IF(push_inst(compiler, data_transfer_insts[WORD_SIZE | LOAD_DATA] | 0x800000
1152 | RN(SLJIT_SP) | RD(tmp) | (offset + (sljit_uw)size - 4 * sizeof(sljit_sw))));
1153 break;
1156 offset += sizeof(sljit_sw);
1157 arg_types >>= SLJIT_ARG_SHIFT;
1160 compiler->args_size = offset;
1161 #else
1162 offset = SLJIT_FR0;
1163 old_offset = SLJIT_FR0;
1164 f32_offset = 0;
1166 while (arg_types) {
1167 switch (arg_types & SLJIT_ARG_MASK) {
1168 case SLJIT_ARG_TYPE_F64:
1169 if (offset != old_offset)
1170 *remap_ptr++ = EMIT_FPU_OPERATION(VMOV_F32, SLJIT_32, offset, old_offset, 0);
1171 old_offset++;
1172 offset++;
1173 break;
1174 case SLJIT_ARG_TYPE_F32:
1175 if (f32_offset != 0) {
1176 *remap_ptr++ = EMIT_FPU_OPERATION(VMOV_F32, 0x20, offset, f32_offset, 0);
1177 f32_offset = 0;
1178 } else {
1179 if (offset != old_offset)
1180 *remap_ptr++ = EMIT_FPU_OPERATION(VMOV_F32, 0, offset, old_offset, 0);
1181 f32_offset = old_offset;
1182 old_offset++;
1184 offset++;
1185 break;
1186 default:
1187 if (!(arg_types & SLJIT_ARG_TYPE_SCRATCH_REG)) {
1188 FAIL_IF(push_inst(compiler, MOV | RD(SLJIT_S0 - saved_arg_count) | RM(SLJIT_R0 + word_arg_count)));
1189 saved_arg_count++;
1192 word_arg_count++;
1193 break;
1195 arg_types >>= SLJIT_ARG_SHIFT;
1198 SLJIT_ASSERT((sljit_uw)(remap_ptr - remap) <= sizeof(remap));
1200 while (remap_ptr > remap)
1201 FAIL_IF(push_inst(compiler, *(--remap_ptr)));
1202 #endif
1204 if (local_size > 0)
1205 FAIL_IF(emit_op(compiler, SLJIT_SUB, ALLOW_IMM, SLJIT_SP, 0, SLJIT_SP, 0, SLJIT_IMM, local_size));
1207 return SLJIT_SUCCESS;
1210 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_set_context(struct sljit_compiler *compiler,
1211 sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
1212 sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
1214 sljit_s32 size;
1216 CHECK_ERROR();
1217 CHECK(check_sljit_set_context(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size));
1218 set_set_context(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size);
1220 size = GET_SAVED_REGISTERS_SIZE(scratches, saveds, 1);
1222 if ((size & SSIZE_OF(sw)) != 0 && (fsaveds > 0 || fscratches >= SLJIT_FIRST_SAVED_FLOAT_REG))
1223 size += SSIZE_OF(sw);
1225 compiler->local_size = ((size + local_size + 0x7) & ~0x7) - size;
1226 return SLJIT_SUCCESS;
1229 static sljit_s32 emit_add_sp(struct sljit_compiler *compiler, sljit_uw imm)
1231 sljit_uw imm2 = get_imm(imm);
1233 if (imm2 == 0) {
1234 FAIL_IF(load_immediate(compiler, TMP_REG2, imm));
1235 imm2 = RM(TMP_REG2);
1238 return push_inst(compiler, ADD | RD(SLJIT_SP) | RN(SLJIT_SP) | imm2);
1241 static sljit_s32 emit_stack_frame_release(struct sljit_compiler *compiler, sljit_s32 frame_size)
1243 sljit_s32 local_size, fscratches, fsaveds, i, tmp;
1244 sljit_s32 lr_dst = TMP_PC;
1245 sljit_uw reg_list;
1247 SLJIT_ASSERT(reg_map[TMP_REG2] == 14);
1249 local_size = compiler->local_size;
1250 fscratches = compiler->fscratches;
1251 fsaveds = compiler->fsaveds;
1253 if (fsaveds > 0 || fscratches >= SLJIT_FIRST_SAVED_FLOAT_REG) {
1254 if (local_size > 0)
1255 FAIL_IF(emit_add_sp(compiler, (sljit_uw)local_size));
1257 if (fsaveds + fscratches >= SLJIT_NUMBER_OF_FLOAT_REGISTERS) {
1258 FAIL_IF(push_inst(compiler, VPOP | VD(SLJIT_FS0) | ((sljit_uw)SLJIT_NUMBER_OF_SAVED_FLOAT_REGISTERS << 1)));
1259 } else {
1260 if (fscratches >= SLJIT_FIRST_SAVED_FLOAT_REG)
1261 FAIL_IF(push_inst(compiler, VPOP | VD(fscratches) | ((sljit_uw)(fscratches - (SLJIT_FIRST_SAVED_FLOAT_REG - 1)) << 1)));
1262 if (fsaveds > 0)
1263 FAIL_IF(push_inst(compiler, VPOP | VD(SLJIT_FS0) | ((sljit_uw)fsaveds << 1)));
1266 local_size = GET_SAVED_REGISTERS_SIZE(compiler->scratches, compiler->saveds, 1) & 0x7;
1269 if (frame_size < 0) {
1270 lr_dst = TMP_REG2;
1271 frame_size = 0;
1272 } else if (frame_size > 0)
1273 lr_dst = 0;
1275 reg_list = 0;
1276 if (lr_dst != 0)
1277 reg_list |= (sljit_uw)1 << reg_map[lr_dst];
1279 tmp = SLJIT_S0 - compiler->saveds;
1280 for (i = SLJIT_S0; i > tmp; i--)
1281 reg_list |= (sljit_uw)1 << reg_map[i];
1283 for (i = compiler->scratches; i >= SLJIT_FIRST_SAVED_REG; i--)
1284 reg_list |= (sljit_uw)1 << reg_map[i];
1286 if (lr_dst == 0 && (reg_list & (reg_list - 1)) == 0) {
1287 /* The local_size does not include the saved registers. */
1288 local_size += SSIZE_OF(sw);
1290 if (reg_list != 0)
1291 local_size += SSIZE_OF(sw);
1293 if (frame_size > local_size)
1294 FAIL_IF(push_inst(compiler, SUB | RD(SLJIT_SP) | RN(SLJIT_SP) | (1 << 25) | (sljit_uw)(frame_size - local_size)));
1295 else if (frame_size < local_size)
1296 FAIL_IF(emit_add_sp(compiler, (sljit_uw)(local_size - frame_size)));
1298 if (reg_list == 0)
1299 return SLJIT_SUCCESS;
1301 if (compiler->saveds > 0) {
1302 SLJIT_ASSERT(reg_list == ((sljit_uw)1 << reg_map[SLJIT_S0]));
1303 lr_dst = SLJIT_S0;
1304 } else {
1305 SLJIT_ASSERT(reg_list == ((sljit_uw)1 << reg_map[SLJIT_FIRST_SAVED_REG]));
1306 lr_dst = SLJIT_FIRST_SAVED_REG;
1309 return push_inst(compiler, data_transfer_insts[WORD_SIZE | LOAD_DATA] | 0x800000
1310 | RN(SLJIT_SP) | RD(lr_dst) | (sljit_uw)(frame_size - 2 * SSIZE_OF(sw)));
1313 if (local_size > 0)
1314 FAIL_IF(emit_add_sp(compiler, (sljit_uw)local_size));
1316 /* Pop saved and temporary registers
1317 multiple registers: ldmia sp!, {...}
1318 single register: ldr reg, [sp], #4 */
1319 if ((reg_list & (reg_list - 1)) == 0) {
1320 SLJIT_ASSERT(lr_dst != 0);
1321 SLJIT_ASSERT(reg_list == (sljit_uw)1 << reg_map[lr_dst]);
1323 return push_inst(compiler, 0xe49d0004 | RD(lr_dst));
1326 FAIL_IF(push_inst(compiler, POP | reg_list));
1327 if (frame_size > 0)
1328 return push_inst(compiler, SUB | RD(SLJIT_SP) | RN(SLJIT_SP) | (1 << 25) | ((sljit_uw)frame_size - sizeof(sljit_sw)));
1329 return SLJIT_SUCCESS;
1332 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_return_void(struct sljit_compiler *compiler)
1334 CHECK_ERROR();
1335 CHECK(check_sljit_emit_return_void(compiler));
1337 return emit_stack_frame_release(compiler, 0);
1340 /* --------------------------------------------------------------------- */
1341 /* Operators */
1342 /* --------------------------------------------------------------------- */
1344 #define EMIT_SHIFT_INS_AND_RETURN(opcode) \
1345 SLJIT_ASSERT(!(flags & INV_IMM) && !(src2 & SRC2_IMM)); \
1346 if (compiler->shift_imm != 0x20) { \
1347 SLJIT_ASSERT(src1 == TMP_REG1); \
1348 SLJIT_ASSERT(!(flags & ARGS_SWAPPED)); \
1350 if (compiler->shift_imm != 0) \
1351 return push_inst(compiler, MOV | (flags & SET_FLAGS) | \
1352 RD(dst) | (compiler->shift_imm << 7) | (opcode << 5) | RM(src2)); \
1353 return push_inst(compiler, MOV | (flags & SET_FLAGS) | RD(dst) | RM(src2)); \
1355 return push_inst(compiler, MOV | (flags & SET_FLAGS) | RD(dst) \
1356 | RM8((flags & ARGS_SWAPPED) ? src1 : src2) | (sljit_uw)(opcode << 5) \
1357 | 0x10 | RM((flags & ARGS_SWAPPED) ? src2 : src1));
1359 static SLJIT_INLINE sljit_s32 emit_single_op(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 flags,
1360 sljit_uw dst, sljit_uw src1, sljit_uw src2)
1362 switch (GET_OPCODE(op)) {
1363 case SLJIT_MOV:
1364 SLJIT_ASSERT(src1 == TMP_REG1 && !(flags & ARGS_SWAPPED));
1365 if (dst != src2) {
1366 if (src2 & SRC2_IMM) {
1367 return push_inst(compiler, ((flags & INV_IMM) ? MVN : MOV) | RD(dst) | src2);
1369 return push_inst(compiler, MOV | RD(dst) | RM(src2));
1371 return SLJIT_SUCCESS;
1373 case SLJIT_MOV_U8:
1374 case SLJIT_MOV_S8:
1375 SLJIT_ASSERT(src1 == TMP_REG1 && !(flags & ARGS_SWAPPED));
1376 if (flags & MOVE_REG_CONV) {
1377 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
1378 if (op == SLJIT_MOV_U8)
1379 return push_inst(compiler, AND | RD(dst) | RN(src2) | SRC2_IMM | 0xff);
1380 FAIL_IF(push_inst(compiler, MOV | RD(dst) | (24 << 7) | RM(src2)));
1381 return push_inst(compiler, MOV | RD(dst) | (24 << 7) | (op == SLJIT_MOV_U8 ? 0x20 : 0x40) | RM(dst));
1382 #else
1383 return push_inst(compiler, (op == SLJIT_MOV_U8 ? UXTB : SXTB) | RD(dst) | RM(src2));
1384 #endif
1386 else if (dst != src2) {
1387 SLJIT_ASSERT(src2 & SRC2_IMM);
1388 return push_inst(compiler, ((flags & INV_IMM) ? MVN : MOV) | RD(dst) | src2);
1390 return SLJIT_SUCCESS;
1392 case SLJIT_MOV_U16:
1393 case SLJIT_MOV_S16:
1394 SLJIT_ASSERT(src1 == TMP_REG1 && !(flags & ARGS_SWAPPED));
1395 if (flags & MOVE_REG_CONV) {
1396 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
1397 FAIL_IF(push_inst(compiler, MOV | RD(dst) | (16 << 7) | RM(src2)));
1398 return push_inst(compiler, MOV | RD(dst) | (16 << 7) | (op == SLJIT_MOV_U16 ? 0x20 : 0x40) | RM(dst));
1399 #else
1400 return push_inst(compiler, (op == SLJIT_MOV_U16 ? UXTH : SXTH) | RD(dst) | RM(src2));
1401 #endif
1403 else if (dst != src2) {
1404 SLJIT_ASSERT(src2 & SRC2_IMM);
1405 return push_inst(compiler, ((flags & INV_IMM) ? MVN : MOV) | RD(dst) | src2);
1407 return SLJIT_SUCCESS;
1409 case SLJIT_NOT:
1410 if (src2 & SRC2_IMM)
1411 return push_inst(compiler, ((flags & INV_IMM) ? MOV : MVN) | (flags & SET_FLAGS) | RD(dst) | src2);
1413 return push_inst(compiler, MVN | (flags & SET_FLAGS) | RD(dst) | RM(src2));
1415 case SLJIT_CLZ:
1416 SLJIT_ASSERT(!(flags & INV_IMM));
1417 SLJIT_ASSERT(!(src2 & SRC2_IMM));
1418 FAIL_IF(push_inst(compiler, CLZ | RD(dst) | RM(src2)));
1419 return SLJIT_SUCCESS;
1421 case SLJIT_ADD:
1422 SLJIT_ASSERT(!(flags & INV_IMM));
1424 if ((flags & (UNUSED_RETURN | ARGS_SWAPPED)) == UNUSED_RETURN)
1425 return push_inst(compiler, CMN | SET_FLAGS | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
1426 return push_inst(compiler, ADD | (flags & SET_FLAGS) | RD(dst) | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
1428 case SLJIT_ADDC:
1429 SLJIT_ASSERT(!(flags & INV_IMM));
1430 return push_inst(compiler, ADC | (flags & SET_FLAGS) | RD(dst) | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
1432 case SLJIT_SUB:
1433 SLJIT_ASSERT(!(flags & INV_IMM));
1435 if ((flags & (UNUSED_RETURN | ARGS_SWAPPED)) == UNUSED_RETURN)
1436 return push_inst(compiler, CMP | SET_FLAGS | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
1438 return push_inst(compiler, (!(flags & ARGS_SWAPPED) ? SUB : RSB) | (flags & SET_FLAGS)
1439 | RD(dst) | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
1441 case SLJIT_SUBC:
1442 SLJIT_ASSERT(!(flags & INV_IMM));
1443 return push_inst(compiler, (!(flags & ARGS_SWAPPED) ? SBC : RSC) | (flags & SET_FLAGS)
1444 | RD(dst) | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
1446 case SLJIT_MUL:
1447 SLJIT_ASSERT(!(flags & INV_IMM));
1448 SLJIT_ASSERT(!(src2 & SRC2_IMM));
1449 compiler->status_flags_state = 0;
1451 if (!HAS_FLAGS(op))
1452 return push_inst(compiler, MUL | RN(dst) | RM8(src2) | RM(src1));
1454 FAIL_IF(push_inst(compiler, SMULL | RN(TMP_REG1) | RD(dst) | RM8(src2) | RM(src1)));
1456 /* cmp TMP_REG1, dst asr #31. */
1457 return push_inst(compiler, CMP | SET_FLAGS | RN(TMP_REG1) | RM(dst) | 0xfc0);
1459 case SLJIT_AND:
1460 if ((flags & (UNUSED_RETURN | INV_IMM)) == UNUSED_RETURN)
1461 return push_inst(compiler, TST | SET_FLAGS | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
1462 return push_inst(compiler, (!(flags & INV_IMM) ? AND : BIC) | (flags & SET_FLAGS)
1463 | RD(dst) | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
1465 case SLJIT_OR:
1466 SLJIT_ASSERT(!(flags & INV_IMM));
1467 return push_inst(compiler, ORR | (flags & SET_FLAGS) | RD(dst) | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
1469 case SLJIT_XOR:
1470 SLJIT_ASSERT(!(flags & INV_IMM));
1471 return push_inst(compiler, EOR | (flags & SET_FLAGS) | RD(dst) | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
1473 case SLJIT_SHL:
1474 EMIT_SHIFT_INS_AND_RETURN(0);
1476 case SLJIT_LSHR:
1477 EMIT_SHIFT_INS_AND_RETURN(1);
1479 case SLJIT_ASHR:
1480 EMIT_SHIFT_INS_AND_RETURN(2);
1483 SLJIT_UNREACHABLE();
1484 return SLJIT_SUCCESS;
1487 #undef EMIT_SHIFT_INS_AND_RETURN
1489 /* Tests whether the immediate can be stored in the 12 bit imm field.
1490 Returns with 0 if not possible. */
1491 static sljit_uw get_imm(sljit_uw imm)
1493 sljit_u32 rol;
1495 if (imm <= 0xff)
1496 return SRC2_IMM | imm;
1498 if (!(imm & 0xff000000)) {
1499 imm <<= 8;
1500 rol = 8;
1502 else {
1503 imm = (imm << 24) | (imm >> 8);
1504 rol = 0;
1507 if (!(imm & 0xff000000)) {
1508 imm <<= 8;
1509 rol += 4;
1512 if (!(imm & 0xf0000000)) {
1513 imm <<= 4;
1514 rol += 2;
1517 if (!(imm & 0xc0000000)) {
1518 imm <<= 2;
1519 rol += 1;
1522 if (!(imm & 0x00ffffff))
1523 return SRC2_IMM | (imm >> 24) | (rol << 8);
1524 else
1525 return 0;
1528 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
1529 static sljit_s32 generate_int(struct sljit_compiler *compiler, sljit_s32 reg, sljit_uw imm, sljit_s32 positive)
1531 sljit_uw mask;
1532 sljit_uw imm1;
1533 sljit_uw imm2;
1534 sljit_uw rol;
1536 /* Step1: Search a zero byte (8 continous zero bit). */
1537 mask = 0xff000000;
1538 rol = 8;
1539 while(1) {
1540 if (!(imm & mask)) {
1541 /* Rol imm by rol. */
1542 imm = (imm << rol) | (imm >> (32 - rol));
1543 /* Calculate arm rol. */
1544 rol = 4 + (rol >> 1);
1545 break;
1547 rol += 2;
1548 mask >>= 2;
1549 if (mask & 0x3) {
1550 /* rol by 8. */
1551 imm = (imm << 8) | (imm >> 24);
1552 mask = 0xff00;
1553 rol = 24;
1554 while (1) {
1555 if (!(imm & mask)) {
1556 /* Rol imm by rol. */
1557 imm = (imm << rol) | (imm >> (32 - rol));
1558 /* Calculate arm rol. */
1559 rol = (rol >> 1) - 8;
1560 break;
1562 rol += 2;
1563 mask >>= 2;
1564 if (mask & 0x3)
1565 return 0;
1567 break;
1571 /* The low 8 bit must be zero. */
1572 SLJIT_ASSERT(!(imm & 0xff));
1574 if (!(imm & 0xff000000)) {
1575 imm1 = SRC2_IMM | ((imm >> 16) & 0xff) | (((rol + 4) & 0xf) << 8);
1576 imm2 = SRC2_IMM | ((imm >> 8) & 0xff) | (((rol + 8) & 0xf) << 8);
1578 else if (imm & 0xc0000000) {
1579 imm1 = SRC2_IMM | ((imm >> 24) & 0xff) | ((rol & 0xf) << 8);
1580 imm <<= 8;
1581 rol += 4;
1583 if (!(imm & 0xff000000)) {
1584 imm <<= 8;
1585 rol += 4;
1588 if (!(imm & 0xf0000000)) {
1589 imm <<= 4;
1590 rol += 2;
1593 if (!(imm & 0xc0000000)) {
1594 imm <<= 2;
1595 rol += 1;
1598 if (!(imm & 0x00ffffff))
1599 imm2 = SRC2_IMM | (imm >> 24) | ((rol & 0xf) << 8);
1600 else
1601 return 0;
1603 else {
1604 if (!(imm & 0xf0000000)) {
1605 imm <<= 4;
1606 rol += 2;
1609 if (!(imm & 0xc0000000)) {
1610 imm <<= 2;
1611 rol += 1;
1614 imm1 = SRC2_IMM | ((imm >> 24) & 0xff) | ((rol & 0xf) << 8);
1615 imm <<= 8;
1616 rol += 4;
1618 if (!(imm & 0xf0000000)) {
1619 imm <<= 4;
1620 rol += 2;
1623 if (!(imm & 0xc0000000)) {
1624 imm <<= 2;
1625 rol += 1;
1628 if (!(imm & 0x00ffffff))
1629 imm2 = SRC2_IMM | (imm >> 24) | ((rol & 0xf) << 8);
1630 else
1631 return 0;
1634 FAIL_IF(push_inst(compiler, (positive ? MOV : MVN) | RD(reg) | imm1));
1635 FAIL_IF(push_inst(compiler, (positive ? ORR : BIC) | RD(reg) | RN(reg) | imm2));
1636 return 1;
1638 #endif
1640 static sljit_s32 load_immediate(struct sljit_compiler *compiler, sljit_s32 reg, sljit_uw imm)
1642 sljit_uw tmp;
1644 #if (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
1645 if (!(imm & ~(sljit_uw)0xffff))
1646 return push_inst(compiler, MOVW | RD(reg) | ((imm << 4) & 0xf0000) | (imm & 0xfff));
1647 #endif
1649 /* Create imm by 1 inst. */
1650 tmp = get_imm(imm);
1651 if (tmp)
1652 return push_inst(compiler, MOV | RD(reg) | tmp);
1654 tmp = get_imm(~imm);
1655 if (tmp)
1656 return push_inst(compiler, MVN | RD(reg) | tmp);
1658 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
1659 /* Create imm by 2 inst. */
1660 FAIL_IF(generate_int(compiler, reg, imm, 1));
1661 FAIL_IF(generate_int(compiler, reg, ~imm, 0));
1663 /* Load integer. */
1664 return push_inst_with_literal(compiler, EMIT_DATA_TRANSFER(WORD_SIZE | LOAD_DATA, 1, reg, TMP_PC, 0), imm);
1665 #else
1666 FAIL_IF(push_inst(compiler, MOVW | RD(reg) | ((imm << 4) & 0xf0000) | (imm & 0xfff)));
1667 if (imm <= 0xffff)
1668 return SLJIT_SUCCESS;
1669 return push_inst(compiler, MOVT | RD(reg) | ((imm >> 12) & 0xf0000) | ((imm >> 16) & 0xfff));
1670 #endif
1673 static SLJIT_INLINE sljit_s32 emit_op_mem(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg,
1674 sljit_s32 arg, sljit_sw argw, sljit_s32 tmp_reg)
1676 sljit_uw imm, offset_reg;
1677 sljit_uw is_type1_transfer = IS_TYPE1_TRANSFER(flags);
1679 SLJIT_ASSERT (arg & SLJIT_MEM);
1680 SLJIT_ASSERT((arg & REG_MASK) != tmp_reg);
1682 if (!(arg & REG_MASK)) {
1683 if (is_type1_transfer) {
1684 FAIL_IF(load_immediate(compiler, tmp_reg, (sljit_uw)argw & ~(sljit_uw)0xfff));
1685 argw &= 0xfff;
1687 else {
1688 FAIL_IF(load_immediate(compiler, tmp_reg, (sljit_uw)argw & ~(sljit_uw)0xff));
1689 argw &= 0xff;
1692 return push_inst(compiler, EMIT_DATA_TRANSFER(flags, 1, reg, tmp_reg,
1693 is_type1_transfer ? argw : TYPE2_TRANSFER_IMM(argw)));
1696 if (arg & OFFS_REG_MASK) {
1697 offset_reg = OFFS_REG(arg);
1698 arg &= REG_MASK;
1699 argw &= 0x3;
1701 if (argw != 0 && !is_type1_transfer) {
1702 FAIL_IF(push_inst(compiler, ADD | RD(tmp_reg) | RN(arg) | RM(offset_reg) | ((sljit_uw)argw << 7)));
1703 return push_inst(compiler, EMIT_DATA_TRANSFER(flags, 1, reg, tmp_reg, TYPE2_TRANSFER_IMM(0)));
1706 /* Bit 25: RM is offset. */
1707 return push_inst(compiler, EMIT_DATA_TRANSFER(flags, 1, reg, arg,
1708 RM(offset_reg) | (is_type1_transfer ? (1 << 25) : 0) | ((sljit_uw)argw << 7)));
1711 arg &= REG_MASK;
1713 if (is_type1_transfer) {
1714 if (argw > 0xfff) {
1715 imm = get_imm((sljit_uw)argw & ~(sljit_uw)0xfff);
1716 if (imm) {
1717 FAIL_IF(push_inst(compiler, ADD | RD(tmp_reg) | RN(arg) | imm));
1718 argw = argw & 0xfff;
1719 arg = tmp_reg;
1722 else if (argw < -0xfff) {
1723 imm = get_imm((sljit_uw)-argw & ~(sljit_uw)0xfff);
1724 if (imm) {
1725 FAIL_IF(push_inst(compiler, SUB | RD(tmp_reg) | RN(arg) | imm));
1726 argw = -(-argw & 0xfff);
1727 arg = tmp_reg;
1731 if (argw >= 0 && argw <= 0xfff)
1732 return push_inst(compiler, EMIT_DATA_TRANSFER(flags, 1, reg, arg, argw));
1734 if (argw < 0 && argw >= -0xfff)
1735 return push_inst(compiler, EMIT_DATA_TRANSFER(flags, 0, reg, arg, -argw));
1737 else {
1738 if (argw > 0xff) {
1739 imm = get_imm((sljit_uw)argw & ~(sljit_uw)0xff);
1740 if (imm) {
1741 FAIL_IF(push_inst(compiler, ADD | RD(tmp_reg) | RN(arg) | imm));
1742 argw = argw & 0xff;
1743 arg = tmp_reg;
1746 else if (argw < -0xff) {
1747 imm = get_imm((sljit_uw)-argw & ~(sljit_uw)0xff);
1748 if (imm) {
1749 FAIL_IF(push_inst(compiler, SUB | RD(tmp_reg) | RN(arg) | imm));
1750 argw = -(-argw & 0xff);
1751 arg = tmp_reg;
1755 if (argw >= 0 && argw <= 0xff)
1756 return push_inst(compiler, EMIT_DATA_TRANSFER(flags, 1, reg, arg, TYPE2_TRANSFER_IMM(argw)));
1758 if (argw < 0 && argw >= -0xff) {
1759 argw = -argw;
1760 return push_inst(compiler, EMIT_DATA_TRANSFER(flags, 0, reg, arg, TYPE2_TRANSFER_IMM(argw)));
1764 FAIL_IF(load_immediate(compiler, tmp_reg, (sljit_uw)argw));
1765 return push_inst(compiler, EMIT_DATA_TRANSFER(flags, 1, reg, arg,
1766 RM(tmp_reg) | (is_type1_transfer ? (1 << 25) : 0)));
1769 static sljit_s32 emit_op(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 inp_flags,
1770 sljit_s32 dst, sljit_sw dstw,
1771 sljit_s32 src1, sljit_sw src1w,
1772 sljit_s32 src2, sljit_sw src2w)
1774 /* src1 is reg or TMP_REG1
1775 src2 is reg, TMP_REG2, or imm
1776 result goes to TMP_REG2, so put result can use TMP_REG1. */
1778 /* We prefers register and simple consts. */
1779 sljit_s32 dst_reg;
1780 sljit_s32 src1_reg;
1781 sljit_s32 src2_reg = 0;
1782 sljit_s32 flags = HAS_FLAGS(op) ? SET_FLAGS : 0;
1783 sljit_s32 neg_op = 0;
1785 if (dst == TMP_REG2)
1786 flags |= UNUSED_RETURN;
1788 SLJIT_ASSERT(!(inp_flags & ALLOW_INV_IMM) || (inp_flags & ALLOW_IMM));
1790 if (inp_flags & ALLOW_NEG_IMM) {
1791 switch (GET_OPCODE(op)) {
1792 case SLJIT_ADD:
1793 compiler->status_flags_state = SLJIT_CURRENT_FLAGS_ADD;
1794 neg_op = SLJIT_SUB;
1795 break;
1796 case SLJIT_ADDC:
1797 compiler->status_flags_state = SLJIT_CURRENT_FLAGS_ADD;
1798 neg_op = SLJIT_SUBC;
1799 break;
1800 case SLJIT_SUB:
1801 compiler->status_flags_state = SLJIT_CURRENT_FLAGS_SUB;
1802 neg_op = SLJIT_ADD;
1803 break;
1804 case SLJIT_SUBC:
1805 compiler->status_flags_state = SLJIT_CURRENT_FLAGS_SUB;
1806 neg_op = SLJIT_ADDC;
1807 break;
1811 do {
1812 if (!(inp_flags & ALLOW_IMM))
1813 break;
1815 if (src2 & SLJIT_IMM) {
1816 src2_reg = (sljit_s32)get_imm((sljit_uw)src2w);
1817 if (src2_reg)
1818 break;
1819 if (inp_flags & ALLOW_INV_IMM) {
1820 src2_reg = (sljit_s32)get_imm(~(sljit_uw)src2w);
1821 if (src2_reg) {
1822 flags |= INV_IMM;
1823 break;
1826 if (neg_op != 0) {
1827 src2_reg = (sljit_s32)get_imm((sljit_uw)-src2w);
1828 if (src2_reg) {
1829 op = neg_op | GET_ALL_FLAGS(op);
1830 break;
1835 if (src1 & SLJIT_IMM) {
1836 src2_reg = (sljit_s32)get_imm((sljit_uw)src1w);
1837 if (src2_reg) {
1838 flags |= ARGS_SWAPPED;
1839 src1 = src2;
1840 src1w = src2w;
1841 break;
1843 if (inp_flags & ALLOW_INV_IMM) {
1844 src2_reg = (sljit_s32)get_imm(~(sljit_uw)src1w);
1845 if (src2_reg) {
1846 flags |= ARGS_SWAPPED | INV_IMM;
1847 src1 = src2;
1848 src1w = src2w;
1849 break;
1852 if (neg_op >= SLJIT_SUB) {
1853 /* Note: additive operation (commutative). */
1854 src2_reg = (sljit_s32)get_imm((sljit_uw)-src1w);
1855 if (src2_reg) {
1856 src1 = src2;
1857 src1w = src2w;
1858 op = neg_op | GET_ALL_FLAGS(op);
1859 break;
1863 } while(0);
1865 /* Source 1. */
1866 if (FAST_IS_REG(src1))
1867 src1_reg = src1;
1868 else if (src1 & SLJIT_MEM) {
1869 FAIL_IF(emit_op_mem(compiler, inp_flags | LOAD_DATA, TMP_REG1, src1, src1w, TMP_REG1));
1870 src1_reg = TMP_REG1;
1872 else {
1873 FAIL_IF(load_immediate(compiler, TMP_REG1, (sljit_uw)src1w));
1874 src1_reg = TMP_REG1;
1877 /* Destination. */
1878 dst_reg = FAST_IS_REG(dst) ? dst : TMP_REG2;
1880 if (op <= SLJIT_MOV_P) {
1881 if (dst & SLJIT_MEM) {
1882 if (inp_flags & BYTE_SIZE)
1883 inp_flags &= ~SIGNED;
1885 if (FAST_IS_REG(src2))
1886 return emit_op_mem(compiler, inp_flags, src2, dst, dstw, TMP_REG2);
1889 if (FAST_IS_REG(src2) && dst_reg != TMP_REG2)
1890 flags |= MOVE_REG_CONV;
1893 /* Source 2. */
1894 if (src2_reg == 0) {
1895 src2_reg = (op <= SLJIT_MOV_P) ? dst_reg : TMP_REG2;
1897 if (FAST_IS_REG(src2))
1898 src2_reg = src2;
1899 else if (src2 & SLJIT_MEM)
1900 FAIL_IF(emit_op_mem(compiler, inp_flags | LOAD_DATA, src2_reg, src2, src2w, TMP_REG2));
1901 else
1902 FAIL_IF(load_immediate(compiler, src2_reg, (sljit_uw)src2w));
1905 FAIL_IF(emit_single_op(compiler, op, flags, (sljit_uw)dst_reg, (sljit_uw)src1_reg, (sljit_uw)src2_reg));
1907 if (!(dst & SLJIT_MEM))
1908 return SLJIT_SUCCESS;
1910 return emit_op_mem(compiler, inp_flags, dst_reg, dst, dstw, TMP_REG1);
1913 #ifdef __cplusplus
1914 extern "C" {
1915 #endif
1917 #if defined(__GNUC__)
1918 extern unsigned int __aeabi_uidivmod(unsigned int numerator, unsigned int denominator);
1919 extern int __aeabi_idivmod(int numerator, int denominator);
1920 #else
1921 #error "Software divmod functions are needed"
1922 #endif
1924 #ifdef __cplusplus
1926 #endif
1928 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op0(struct sljit_compiler *compiler, sljit_s32 op)
1930 sljit_uw saved_reg_list[3];
1931 sljit_sw saved_reg_count;
1933 CHECK_ERROR();
1934 CHECK(check_sljit_emit_op0(compiler, op));
1936 op = GET_OPCODE(op);
1937 switch (op) {
1938 case SLJIT_BREAKPOINT:
1939 FAIL_IF(push_inst(compiler, BKPT));
1940 break;
1941 case SLJIT_NOP:
1942 FAIL_IF(push_inst(compiler, NOP));
1943 break;
1944 case SLJIT_LMUL_UW:
1945 case SLJIT_LMUL_SW:
1946 return push_inst(compiler, (op == SLJIT_LMUL_UW ? UMULL : SMULL)
1947 | RN(SLJIT_R1) | RD(SLJIT_R0) | RM8(SLJIT_R0) | RM(SLJIT_R1));
1948 case SLJIT_DIVMOD_UW:
1949 case SLJIT_DIVMOD_SW:
1950 case SLJIT_DIV_UW:
1951 case SLJIT_DIV_SW:
1952 SLJIT_COMPILE_ASSERT((SLJIT_DIVMOD_UW & 0x2) == 0 && SLJIT_DIV_UW - 0x2 == SLJIT_DIVMOD_UW, bad_div_opcode_assignments);
1953 SLJIT_ASSERT(reg_map[2] == 1 && reg_map[3] == 2 && reg_map[4] == 3);
1955 saved_reg_count = 0;
1956 if (compiler->scratches >= 4)
1957 saved_reg_list[saved_reg_count++] = 3;
1958 if (compiler->scratches >= 3)
1959 saved_reg_list[saved_reg_count++] = 2;
1960 if (op >= SLJIT_DIV_UW)
1961 saved_reg_list[saved_reg_count++] = 1;
1963 if (saved_reg_count > 0) {
1964 FAIL_IF(push_inst(compiler, 0xe52d0000 | (saved_reg_count >= 3 ? 16 : 8)
1965 | (saved_reg_list[0] << 12) /* str rX, [sp, #-8/-16]! */));
1966 if (saved_reg_count >= 2) {
1967 SLJIT_ASSERT(saved_reg_list[1] < 8);
1968 FAIL_IF(push_inst(compiler, 0xe58d0004 | (saved_reg_list[1] << 12) /* str rX, [sp, #4] */));
1970 if (saved_reg_count >= 3) {
1971 SLJIT_ASSERT(saved_reg_list[2] < 8);
1972 FAIL_IF(push_inst(compiler, 0xe58d0008 | (saved_reg_list[2] << 12) /* str rX, [sp, #8] */));
1976 #if defined(__GNUC__)
1977 FAIL_IF(sljit_emit_ijump(compiler, SLJIT_FAST_CALL, SLJIT_IMM,
1978 ((op | 0x2) == SLJIT_DIV_UW ? SLJIT_FUNC_ADDR(__aeabi_uidivmod) : SLJIT_FUNC_ADDR(__aeabi_idivmod))));
1979 #else
1980 #error "Software divmod functions are needed"
1981 #endif
1983 if (saved_reg_count > 0) {
1984 if (saved_reg_count >= 3) {
1985 SLJIT_ASSERT(saved_reg_list[2] < 8);
1986 FAIL_IF(push_inst(compiler, 0xe59d0008 | (saved_reg_list[2] << 12) /* ldr rX, [sp, #8] */));
1988 if (saved_reg_count >= 2) {
1989 SLJIT_ASSERT(saved_reg_list[1] < 8);
1990 FAIL_IF(push_inst(compiler, 0xe59d0004 | (saved_reg_list[1] << 12) /* ldr rX, [sp, #4] */));
1992 return push_inst(compiler, 0xe49d0000 | (sljit_uw)(saved_reg_count >= 3 ? 16 : 8)
1993 | (saved_reg_list[0] << 12) /* ldr rX, [sp], #8/16 */);
1995 return SLJIT_SUCCESS;
1996 case SLJIT_ENDBR:
1997 case SLJIT_SKIP_FRAMES_BEFORE_RETURN:
1998 return SLJIT_SUCCESS;
2001 return SLJIT_SUCCESS;
2004 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op1(struct sljit_compiler *compiler, sljit_s32 op,
2005 sljit_s32 dst, sljit_sw dstw,
2006 sljit_s32 src, sljit_sw srcw)
2008 CHECK_ERROR();
2009 CHECK(check_sljit_emit_op1(compiler, op, dst, dstw, src, srcw));
2010 ADJUST_LOCAL_OFFSET(dst, dstw);
2011 ADJUST_LOCAL_OFFSET(src, srcw);
2013 switch (GET_OPCODE(op)) {
2014 case SLJIT_MOV:
2015 case SLJIT_MOV_U32:
2016 case SLJIT_MOV_S32:
2017 case SLJIT_MOV32:
2018 case SLJIT_MOV_P:
2019 return emit_op(compiler, SLJIT_MOV, ALLOW_ANY_IMM, dst, dstw, TMP_REG1, 0, src, srcw);
2021 case SLJIT_MOV_U8:
2022 return emit_op(compiler, SLJIT_MOV_U8, ALLOW_ANY_IMM | BYTE_SIZE, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_u8)srcw : srcw);
2024 case SLJIT_MOV_S8:
2025 return emit_op(compiler, SLJIT_MOV_S8, ALLOW_ANY_IMM | SIGNED | BYTE_SIZE, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_s8)srcw : srcw);
2027 case SLJIT_MOV_U16:
2028 return emit_op(compiler, SLJIT_MOV_U16, ALLOW_ANY_IMM | HALF_SIZE, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_u16)srcw : srcw);
2030 case SLJIT_MOV_S16:
2031 return emit_op(compiler, SLJIT_MOV_S16, ALLOW_ANY_IMM | SIGNED | HALF_SIZE, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_s16)srcw : srcw);
2033 case SLJIT_NOT:
2034 return emit_op(compiler, op, ALLOW_ANY_IMM, dst, dstw, TMP_REG1, 0, src, srcw);
2036 case SLJIT_CLZ:
2037 return emit_op(compiler, op, 0, dst, dstw, TMP_REG1, 0, src, srcw);
2040 return SLJIT_SUCCESS;
2043 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op2(struct sljit_compiler *compiler, sljit_s32 op,
2044 sljit_s32 dst, sljit_sw dstw,
2045 sljit_s32 src1, sljit_sw src1w,
2046 sljit_s32 src2, sljit_sw src2w)
2048 CHECK_ERROR();
2049 CHECK(check_sljit_emit_op2(compiler, op, 0, dst, dstw, src1, src1w, src2, src2w));
2050 ADJUST_LOCAL_OFFSET(dst, dstw);
2051 ADJUST_LOCAL_OFFSET(src1, src1w);
2052 ADJUST_LOCAL_OFFSET(src2, src2w);
2054 switch (GET_OPCODE(op)) {
2055 case SLJIT_ADD:
2056 case SLJIT_ADDC:
2057 case SLJIT_SUB:
2058 case SLJIT_SUBC:
2059 return emit_op(compiler, op, ALLOW_IMM | ALLOW_NEG_IMM, dst, dstw, src1, src1w, src2, src2w);
2061 case SLJIT_OR:
2062 case SLJIT_XOR:
2063 return emit_op(compiler, op, ALLOW_IMM, dst, dstw, src1, src1w, src2, src2w);
2065 case SLJIT_MUL:
2066 return emit_op(compiler, op, 0, dst, dstw, src1, src1w, src2, src2w);
2068 case SLJIT_AND:
2069 return emit_op(compiler, op, ALLOW_ANY_IMM, dst, dstw, src1, src1w, src2, src2w);
2071 case SLJIT_SHL:
2072 case SLJIT_LSHR:
2073 case SLJIT_ASHR:
2074 if (src2 & SLJIT_IMM) {
2075 compiler->shift_imm = src2w & 0x1f;
2076 return emit_op(compiler, op, 0, dst, dstw, TMP_REG1, 0, src1, src1w);
2078 else {
2079 compiler->shift_imm = 0x20;
2080 return emit_op(compiler, op, 0, dst, dstw, src1, src1w, src2, src2w);
2084 return SLJIT_SUCCESS;
2087 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op2u(struct sljit_compiler *compiler, sljit_s32 op,
2088 sljit_s32 src1, sljit_sw src1w,
2089 sljit_s32 src2, sljit_sw src2w)
2091 CHECK_ERROR();
2092 CHECK(check_sljit_emit_op2(compiler, op, 1, 0, 0, src1, src1w, src2, src2w));
2094 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2095 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2096 compiler->skip_checks = 1;
2097 #endif
2098 return sljit_emit_op2(compiler, op, TMP_REG2, 0, src1, src1w, src2, src2w);
2101 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_src(struct sljit_compiler *compiler, sljit_s32 op,
2102 sljit_s32 src, sljit_sw srcw)
2104 CHECK_ERROR();
2105 CHECK(check_sljit_emit_op_src(compiler, op, src, srcw));
2106 ADJUST_LOCAL_OFFSET(src, srcw);
2108 switch (op) {
2109 case SLJIT_FAST_RETURN:
2110 SLJIT_ASSERT(reg_map[TMP_REG2] == 14);
2112 if (FAST_IS_REG(src))
2113 FAIL_IF(push_inst(compiler, MOV | RD(TMP_REG2) | RM(src)));
2114 else
2115 FAIL_IF(emit_op_mem(compiler, WORD_SIZE | LOAD_DATA, TMP_REG2, src, srcw, TMP_REG1));
2117 return push_inst(compiler, BX | RM(TMP_REG2));
2118 case SLJIT_SKIP_FRAMES_BEFORE_FAST_RETURN:
2119 return SLJIT_SUCCESS;
2120 case SLJIT_PREFETCH_L1:
2121 case SLJIT_PREFETCH_L2:
2122 case SLJIT_PREFETCH_L3:
2123 case SLJIT_PREFETCH_ONCE:
2124 #if (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
2125 SLJIT_ASSERT(src & SLJIT_MEM);
2126 return emit_op_mem(compiler, PRELOAD | LOAD_DATA, TMP_PC, src, srcw, TMP_REG1);
2127 #else /* !SLJIT_CONFIG_ARM_V7 */
2128 return SLJIT_SUCCESS;
2129 #endif /* SLJIT_CONFIG_ARM_V7 */
2132 return SLJIT_SUCCESS;
2135 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_register_index(sljit_s32 reg)
2137 CHECK_REG_INDEX(check_sljit_get_register_index(reg));
2138 return reg_map[reg];
2141 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_float_register_index(sljit_s32 reg)
2143 CHECK_REG_INDEX(check_sljit_get_float_register_index(reg));
2144 return (freg_map[reg] << 1);
2147 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_custom(struct sljit_compiler *compiler,
2148 void *instruction, sljit_u32 size)
2150 SLJIT_UNUSED_ARG(size);
2151 CHECK_ERROR();
2152 CHECK(check_sljit_emit_op_custom(compiler, instruction, size));
2154 return push_inst(compiler, *(sljit_uw*)instruction);
2157 /* --------------------------------------------------------------------- */
2158 /* Floating point operators */
2159 /* --------------------------------------------------------------------- */
2161 #define FPU_LOAD (1 << 20)
2162 #define EMIT_FPU_DATA_TRANSFER(inst, add, base, freg, offs) \
2163 ((inst) | (sljit_uw)((add) << 23) | RN(base) | VD(freg) | (sljit_uw)(offs))
2165 static sljit_s32 emit_fop_mem(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg, sljit_s32 arg, sljit_sw argw)
2167 sljit_uw imm;
2168 sljit_uw inst = VSTR_F32 | (flags & (SLJIT_32 | FPU_LOAD));
2170 SLJIT_ASSERT(arg & SLJIT_MEM);
2171 arg &= ~SLJIT_MEM;
2173 if (SLJIT_UNLIKELY(arg & OFFS_REG_MASK)) {
2174 FAIL_IF(push_inst(compiler, ADD | RD(TMP_REG2) | RN(arg & REG_MASK) | RM(OFFS_REG(arg)) | (((sljit_uw)argw & 0x3) << 7)));
2175 arg = TMP_REG2;
2176 argw = 0;
2179 /* Fast loads and stores. */
2180 if (arg) {
2181 if (!(argw & ~0x3fc))
2182 return push_inst(compiler, EMIT_FPU_DATA_TRANSFER(inst, 1, arg & REG_MASK, reg, argw >> 2));
2183 if (!(-argw & ~0x3fc))
2184 return push_inst(compiler, EMIT_FPU_DATA_TRANSFER(inst, 0, arg & REG_MASK, reg, (-argw) >> 2));
2186 imm = get_imm((sljit_uw)argw & ~(sljit_uw)0x3fc);
2187 if (imm) {
2188 FAIL_IF(push_inst(compiler, ADD | RD(TMP_REG2) | RN(arg & REG_MASK) | imm));
2189 return push_inst(compiler, EMIT_FPU_DATA_TRANSFER(inst, 1, TMP_REG2, reg, (argw & 0x3fc) >> 2));
2191 imm = get_imm((sljit_uw)-argw & ~(sljit_uw)0x3fc);
2192 if (imm) {
2193 argw = -argw;
2194 FAIL_IF(push_inst(compiler, SUB | RD(TMP_REG2) | RN(arg & REG_MASK) | imm));
2195 return push_inst(compiler, EMIT_FPU_DATA_TRANSFER(inst, 0, TMP_REG2, reg, (argw & 0x3fc) >> 2));
2199 if (arg) {
2200 FAIL_IF(load_immediate(compiler, TMP_REG2, (sljit_uw)argw));
2201 FAIL_IF(push_inst(compiler, ADD | RD(TMP_REG2) | RN(arg & REG_MASK) | RM(TMP_REG2)));
2203 else
2204 FAIL_IF(load_immediate(compiler, TMP_REG2, (sljit_uw)argw));
2206 return push_inst(compiler, EMIT_FPU_DATA_TRANSFER(inst, 1, TMP_REG2, reg, 0));
2209 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_conv_sw_from_f64(struct sljit_compiler *compiler, sljit_s32 op,
2210 sljit_s32 dst, sljit_sw dstw,
2211 sljit_s32 src, sljit_sw srcw)
2213 op ^= SLJIT_32;
2215 if (src & SLJIT_MEM) {
2216 FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_32) | FPU_LOAD, TMP_FREG1, src, srcw));
2217 src = TMP_FREG1;
2220 FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VCVT_S32_F32, op & SLJIT_32, TMP_FREG1, src, 0)));
2222 if (FAST_IS_REG(dst))
2223 return push_inst(compiler, VMOV | (1 << 20) | RD(dst) | VN(TMP_FREG1));
2225 /* Store the integer value from a VFP register. */
2226 return emit_fop_mem(compiler, 0, TMP_FREG1, dst, dstw);
2229 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_conv_f64_from_sw(struct sljit_compiler *compiler, sljit_s32 op,
2230 sljit_s32 dst, sljit_sw dstw,
2231 sljit_s32 src, sljit_sw srcw)
2233 sljit_s32 dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
2235 op ^= SLJIT_32;
2237 if (FAST_IS_REG(src))
2238 FAIL_IF(push_inst(compiler, VMOV | RD(src) | VN(TMP_FREG1)));
2239 else if (src & SLJIT_MEM) {
2240 /* Load the integer value into a VFP register. */
2241 FAIL_IF(emit_fop_mem(compiler, FPU_LOAD, TMP_FREG1, src, srcw));
2243 else {
2244 FAIL_IF(load_immediate(compiler, TMP_REG1, (sljit_uw)srcw));
2245 FAIL_IF(push_inst(compiler, VMOV | RD(TMP_REG1) | VN(TMP_FREG1)));
2248 FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VCVT_F32_S32, op & SLJIT_32, dst_r, TMP_FREG1, 0)));
2250 if (dst & SLJIT_MEM)
2251 return emit_fop_mem(compiler, (op & SLJIT_32), TMP_FREG1, dst, dstw);
2252 return SLJIT_SUCCESS;
2255 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_cmp(struct sljit_compiler *compiler, sljit_s32 op,
2256 sljit_s32 src1, sljit_sw src1w,
2257 sljit_s32 src2, sljit_sw src2w)
2259 op ^= SLJIT_32;
2261 if (src1 & SLJIT_MEM) {
2262 FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_32) | FPU_LOAD, TMP_FREG1, src1, src1w));
2263 src1 = TMP_FREG1;
2266 if (src2 & SLJIT_MEM) {
2267 FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_32) | FPU_LOAD, TMP_FREG2, src2, src2w));
2268 src2 = TMP_FREG2;
2271 FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VCMP_F32, op & SLJIT_32, src1, src2, 0)));
2272 return push_inst(compiler, VMRS);
2275 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop1(struct sljit_compiler *compiler, sljit_s32 op,
2276 sljit_s32 dst, sljit_sw dstw,
2277 sljit_s32 src, sljit_sw srcw)
2279 sljit_s32 dst_r;
2281 CHECK_ERROR();
2283 SLJIT_COMPILE_ASSERT((SLJIT_32 == 0x100), float_transfer_bit_error);
2284 SELECT_FOP1_OPERATION_WITH_CHECKS(compiler, op, dst, dstw, src, srcw);
2286 dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
2288 if (GET_OPCODE(op) != SLJIT_CONV_F64_FROM_F32)
2289 op ^= SLJIT_32;
2291 if (src & SLJIT_MEM) {
2292 FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_32) | FPU_LOAD, dst_r, src, srcw));
2293 src = dst_r;
2296 switch (GET_OPCODE(op)) {
2297 case SLJIT_MOV_F64:
2298 if (src != dst_r) {
2299 if (dst_r != TMP_FREG1)
2300 FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VMOV_F32, op & SLJIT_32, dst_r, src, 0)));
2301 else
2302 dst_r = src;
2304 break;
2305 case SLJIT_NEG_F64:
2306 FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VNEG_F32, op & SLJIT_32, dst_r, src, 0)));
2307 break;
2308 case SLJIT_ABS_F64:
2309 FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VABS_F32, op & SLJIT_32, dst_r, src, 0)));
2310 break;
2311 case SLJIT_CONV_F64_FROM_F32:
2312 FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VCVT_F64_F32, op & SLJIT_32, dst_r, src, 0)));
2313 op ^= SLJIT_32;
2314 break;
2317 if (dst & SLJIT_MEM)
2318 return emit_fop_mem(compiler, (op & SLJIT_32), dst_r, dst, dstw);
2319 return SLJIT_SUCCESS;
2322 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop2(struct sljit_compiler *compiler, sljit_s32 op,
2323 sljit_s32 dst, sljit_sw dstw,
2324 sljit_s32 src1, sljit_sw src1w,
2325 sljit_s32 src2, sljit_sw src2w)
2327 sljit_s32 dst_r;
2329 CHECK_ERROR();
2330 CHECK(check_sljit_emit_fop2(compiler, op, dst, dstw, src1, src1w, src2, src2w));
2331 ADJUST_LOCAL_OFFSET(dst, dstw);
2332 ADJUST_LOCAL_OFFSET(src1, src1w);
2333 ADJUST_LOCAL_OFFSET(src2, src2w);
2335 op ^= SLJIT_32;
2337 dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
2339 if (src2 & SLJIT_MEM) {
2340 FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_32) | FPU_LOAD, TMP_FREG2, src2, src2w));
2341 src2 = TMP_FREG2;
2344 if (src1 & SLJIT_MEM) {
2345 FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_32) | FPU_LOAD, TMP_FREG1, src1, src1w));
2346 src1 = TMP_FREG1;
2349 switch (GET_OPCODE(op)) {
2350 case SLJIT_ADD_F64:
2351 FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VADD_F32, op & SLJIT_32, dst_r, src2, src1)));
2352 break;
2354 case SLJIT_SUB_F64:
2355 FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VSUB_F32, op & SLJIT_32, dst_r, src2, src1)));
2356 break;
2358 case SLJIT_MUL_F64:
2359 FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VMUL_F32, op & SLJIT_32, dst_r, src2, src1)));
2360 break;
2362 case SLJIT_DIV_F64:
2363 FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VDIV_F32, op & SLJIT_32, dst_r, src2, src1)));
2364 break;
2367 if (dst_r == TMP_FREG1)
2368 FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_32), TMP_FREG1, dst, dstw));
2370 return SLJIT_SUCCESS;
2373 #undef FPU_LOAD
2374 #undef EMIT_FPU_DATA_TRANSFER
2376 /* --------------------------------------------------------------------- */
2377 /* Other instructions */
2378 /* --------------------------------------------------------------------- */
2380 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fast_enter(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw)
2382 CHECK_ERROR();
2383 CHECK(check_sljit_emit_fast_enter(compiler, dst, dstw));
2384 ADJUST_LOCAL_OFFSET(dst, dstw);
2386 SLJIT_ASSERT(reg_map[TMP_REG2] == 14);
2388 if (FAST_IS_REG(dst))
2389 return push_inst(compiler, MOV | RD(dst) | RM(TMP_REG2));
2391 /* Memory. */
2392 return emit_op_mem(compiler, WORD_SIZE, TMP_REG2, dst, dstw, TMP_REG1);
2395 /* --------------------------------------------------------------------- */
2396 /* Conditional instructions */
2397 /* --------------------------------------------------------------------- */
2399 static sljit_uw get_cc(struct sljit_compiler *compiler, sljit_s32 type)
2401 switch (type) {
2402 case SLJIT_EQUAL:
2403 case SLJIT_F_EQUAL:
2404 case SLJIT_ORDERED_EQUAL:
2405 case SLJIT_UNORDERED_OR_EQUAL: /* Not supported. */
2406 return 0x00000000;
2408 case SLJIT_NOT_EQUAL:
2409 case SLJIT_F_NOT_EQUAL:
2410 case SLJIT_UNORDERED_OR_NOT_EQUAL:
2411 case SLJIT_ORDERED_NOT_EQUAL: /* Not supported. */
2412 return 0x10000000;
2414 case SLJIT_CARRY:
2415 if (compiler->status_flags_state & SLJIT_CURRENT_FLAGS_ADD)
2416 return 0x20000000;
2417 /* fallthrough */
2419 case SLJIT_LESS:
2420 return 0x30000000;
2422 case SLJIT_NOT_CARRY:
2423 if (compiler->status_flags_state & SLJIT_CURRENT_FLAGS_ADD)
2424 return 0x30000000;
2425 /* fallthrough */
2427 case SLJIT_GREATER_EQUAL:
2428 return 0x20000000;
2430 case SLJIT_GREATER:
2431 case SLJIT_UNORDERED_OR_GREATER:
2432 return 0x80000000;
2434 case SLJIT_LESS_EQUAL:
2435 case SLJIT_F_LESS_EQUAL:
2436 case SLJIT_ORDERED_LESS_EQUAL:
2437 return 0x90000000;
2439 case SLJIT_SIG_LESS:
2440 case SLJIT_UNORDERED_OR_LESS:
2441 return 0xb0000000;
2443 case SLJIT_SIG_GREATER_EQUAL:
2444 case SLJIT_F_GREATER_EQUAL:
2445 case SLJIT_ORDERED_GREATER_EQUAL:
2446 return 0xa0000000;
2448 case SLJIT_SIG_GREATER:
2449 case SLJIT_F_GREATER:
2450 case SLJIT_ORDERED_GREATER:
2451 return 0xc0000000;
2453 case SLJIT_SIG_LESS_EQUAL:
2454 case SLJIT_UNORDERED_OR_LESS_EQUAL:
2455 return 0xd0000000;
2457 case SLJIT_OVERFLOW:
2458 if (!(compiler->status_flags_state & (SLJIT_CURRENT_FLAGS_ADD | SLJIT_CURRENT_FLAGS_SUB)))
2459 return 0x10000000;
2460 /* fallthrough */
2462 case SLJIT_UNORDERED:
2463 return 0x60000000;
2465 case SLJIT_NOT_OVERFLOW:
2466 if (!(compiler->status_flags_state & (SLJIT_CURRENT_FLAGS_ADD | SLJIT_CURRENT_FLAGS_SUB)))
2467 return 0x00000000;
2468 /* fallthrough */
2470 case SLJIT_ORDERED:
2471 return 0x70000000;
2473 case SLJIT_F_LESS:
2474 case SLJIT_ORDERED_LESS:
2475 return 0x40000000;
2477 case SLJIT_UNORDERED_OR_GREATER_EQUAL:
2478 return 0x50000000;
2480 default:
2481 SLJIT_ASSERT(type >= SLJIT_JUMP && type <= SLJIT_CALL_CDECL);
2482 return 0xe0000000;
2486 SLJIT_API_FUNC_ATTRIBUTE struct sljit_label* sljit_emit_label(struct sljit_compiler *compiler)
2488 struct sljit_label *label;
2490 CHECK_ERROR_PTR();
2491 CHECK_PTR(check_sljit_emit_label(compiler));
2493 if (compiler->last_label && compiler->last_label->size == compiler->size)
2494 return compiler->last_label;
2496 label = (struct sljit_label*)ensure_abuf(compiler, sizeof(struct sljit_label));
2497 PTR_FAIL_IF(!label);
2498 set_label(label, compiler);
2499 return label;
2502 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_jump(struct sljit_compiler *compiler, sljit_s32 type)
2504 struct sljit_jump *jump;
2506 CHECK_ERROR_PTR();
2507 CHECK_PTR(check_sljit_emit_jump(compiler, type));
2509 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
2510 PTR_FAIL_IF(!jump);
2511 set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
2512 type &= 0xff;
2514 SLJIT_ASSERT(reg_map[TMP_REG1] != 14);
2516 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
2517 if (type >= SLJIT_FAST_CALL)
2518 PTR_FAIL_IF(prepare_blx(compiler));
2519 PTR_FAIL_IF(push_inst_with_unique_literal(compiler, ((EMIT_DATA_TRANSFER(WORD_SIZE | LOAD_DATA, 1,
2520 type <= SLJIT_JUMP ? TMP_PC : TMP_REG1, TMP_PC, 0)) & ~COND_MASK) | get_cc(compiler, type), 0));
2522 if (jump->flags & SLJIT_REWRITABLE_JUMP) {
2523 jump->addr = compiler->size;
2524 compiler->patches++;
2527 if (type >= SLJIT_FAST_CALL) {
2528 jump->flags |= IS_BL;
2529 PTR_FAIL_IF(emit_blx(compiler));
2532 if (!(jump->flags & SLJIT_REWRITABLE_JUMP))
2533 jump->addr = compiler->size;
2534 #else
2535 if (type >= SLJIT_FAST_CALL)
2536 jump->flags |= IS_BL;
2537 PTR_FAIL_IF(emit_imm(compiler, TMP_REG1, 0));
2538 PTR_FAIL_IF(push_inst(compiler, (((type <= SLJIT_JUMP ? BX : BLX) | RM(TMP_REG1)) & ~COND_MASK) | get_cc(compiler, type)));
2539 jump->addr = compiler->size;
2540 #endif
2541 return jump;
2544 #ifdef __SOFTFP__
2546 static sljit_s32 softfloat_call_with_args(struct sljit_compiler *compiler, sljit_s32 arg_types, sljit_s32 *src, sljit_u32 *extra_space)
2548 sljit_u32 is_tail_call = *extra_space & SLJIT_CALL_RETURN;
2549 sljit_u32 offset = 0;
2550 sljit_u32 word_arg_offset = 0;
2551 sljit_u32 src_offset = 4 * sizeof(sljit_sw);
2552 sljit_u32 float_arg_count = 0;
2553 sljit_s32 types = 0;
2554 sljit_u8 offsets[4];
2555 sljit_u8 *offset_ptr = offsets;
2557 if (src && FAST_IS_REG(*src))
2558 src_offset = (sljit_uw)reg_map[*src] * sizeof(sljit_sw);
2560 arg_types >>= SLJIT_ARG_SHIFT;
2562 while (arg_types) {
2563 types = (types << SLJIT_ARG_SHIFT) | (arg_types & SLJIT_ARG_MASK);
2565 switch (arg_types & SLJIT_ARG_MASK) {
2566 case SLJIT_ARG_TYPE_F64:
2567 if (offset & 0x7)
2568 offset += sizeof(sljit_sw);
2569 *offset_ptr++ = (sljit_u8)offset;
2570 offset += sizeof(sljit_f64);
2571 float_arg_count++;
2572 break;
2573 case SLJIT_ARG_TYPE_F32:
2574 *offset_ptr++ = (sljit_u8)offset;
2575 offset += sizeof(sljit_f32);
2576 float_arg_count++;
2577 break;
2578 default:
2579 *offset_ptr++ = (sljit_u8)offset;
2580 offset += sizeof(sljit_sw);
2581 word_arg_offset += sizeof(sljit_sw);
2582 break;
2585 arg_types >>= SLJIT_ARG_SHIFT;
2588 if (offset > 4 * sizeof(sljit_sw) && (!is_tail_call || offset > compiler->args_size)) {
2589 /* Keep lr register on the stack. */
2590 if (is_tail_call)
2591 offset += sizeof(sljit_sw);
2593 offset = ((offset - 4 * sizeof(sljit_sw)) + 0x7) & ~(sljit_uw)0x7;
2595 *extra_space = offset;
2597 if (is_tail_call)
2598 FAIL_IF(emit_stack_frame_release(compiler, (sljit_s32)offset));
2599 else
2600 FAIL_IF(push_inst(compiler, SUB | RD(SLJIT_SP) | RN(SLJIT_SP) | SRC2_IMM | offset));
2601 } else {
2602 if (is_tail_call)
2603 FAIL_IF(emit_stack_frame_release(compiler, -1));
2604 *extra_space = 0;
2607 /* Process arguments in reversed direction. */
2608 while (types) {
2609 switch (types & SLJIT_ARG_MASK) {
2610 case SLJIT_ARG_TYPE_F64:
2611 float_arg_count--;
2612 offset = *(--offset_ptr);
2614 SLJIT_ASSERT((offset & 0x7) == 0);
2616 if (offset < 4 * sizeof(sljit_sw)) {
2617 if (src_offset == offset || src_offset == offset + sizeof(sljit_sw)) {
2618 FAIL_IF(push_inst(compiler, MOV | RD(TMP_REG1) | (src_offset >> 2)));
2619 *src = TMP_REG1;
2621 FAIL_IF(push_inst(compiler, VMOV2 | 0x100000 | (offset << 10) | ((offset + sizeof(sljit_sw)) << 14) | float_arg_count));
2622 } else
2623 FAIL_IF(push_inst(compiler, VSTR_F32 | 0x800100 | RN(SLJIT_SP)
2624 | (float_arg_count << 12) | ((offset - 4 * sizeof(sljit_sw)) >> 2)));
2625 break;
2626 case SLJIT_ARG_TYPE_F32:
2627 float_arg_count--;
2628 offset = *(--offset_ptr);
2630 if (offset < 4 * sizeof(sljit_sw)) {
2631 if (src_offset == offset) {
2632 FAIL_IF(push_inst(compiler, MOV | RD(TMP_REG1) | (src_offset >> 2)));
2633 *src = TMP_REG1;
2635 FAIL_IF(push_inst(compiler, VMOV | 0x100000 | (float_arg_count << 16) | (offset << 10)));
2636 } else
2637 FAIL_IF(push_inst(compiler, VSTR_F32 | 0x800000 | RN(SLJIT_SP)
2638 | (float_arg_count << 12) | ((offset - 4 * sizeof(sljit_sw)) >> 2)));
2639 break;
2640 default:
2641 word_arg_offset -= sizeof(sljit_sw);
2642 offset = *(--offset_ptr);
2644 SLJIT_ASSERT(offset >= word_arg_offset);
2646 if (offset != word_arg_offset) {
2647 if (offset < 4 * sizeof(sljit_sw)) {
2648 if (src_offset == offset) {
2649 FAIL_IF(push_inst(compiler, MOV | RD(TMP_REG1) | (src_offset >> 2)));
2650 *src = TMP_REG1;
2652 else if (src_offset == word_arg_offset) {
2653 *src = (sljit_s32)(SLJIT_R0 + (offset >> 2));
2654 src_offset = offset;
2656 FAIL_IF(push_inst(compiler, MOV | (offset << 10) | (word_arg_offset >> 2)));
2657 } else
2658 FAIL_IF(push_inst(compiler, data_transfer_insts[WORD_SIZE] | 0x800000 | RN(SLJIT_SP) | (word_arg_offset << 10) | (offset - 4 * sizeof(sljit_sw))));
2660 break;
2663 types >>= SLJIT_ARG_SHIFT;
2666 return SLJIT_SUCCESS;
2669 static sljit_s32 softfloat_post_call_with_args(struct sljit_compiler *compiler, sljit_s32 arg_types)
2671 if ((arg_types & SLJIT_ARG_MASK) == SLJIT_ARG_TYPE_F64)
2672 FAIL_IF(push_inst(compiler, VMOV2 | (1 << 16) | (0 << 12) | 0));
2673 if ((arg_types & SLJIT_ARG_MASK) == SLJIT_ARG_TYPE_F32)
2674 FAIL_IF(push_inst(compiler, VMOV | (0 << 16) | (0 << 12)));
2676 return SLJIT_SUCCESS;
2679 #else /* !__SOFTFP__ */
2681 static sljit_s32 hardfloat_call_with_args(struct sljit_compiler *compiler, sljit_s32 arg_types)
2683 sljit_u32 offset = SLJIT_FR0;
2684 sljit_u32 new_offset = SLJIT_FR0;
2685 sljit_u32 f32_offset = 0;
2687 /* Remove return value. */
2688 arg_types >>= SLJIT_ARG_SHIFT;
2690 while (arg_types) {
2691 switch (arg_types & SLJIT_ARG_MASK) {
2692 case SLJIT_ARG_TYPE_F64:
2693 if (offset != new_offset)
2694 FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VMOV_F32,
2695 SLJIT_32, new_offset, offset, 0)));
2697 new_offset++;
2698 offset++;
2699 break;
2700 case SLJIT_ARG_TYPE_F32:
2701 if (f32_offset != 0) {
2702 FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VMOV_F32,
2703 0x400000, f32_offset, offset, 0)));
2704 f32_offset = 0;
2705 } else {
2706 if (offset != new_offset)
2707 FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VMOV_F32,
2708 0, new_offset, offset, 0)));
2709 f32_offset = new_offset;
2710 new_offset++;
2712 offset++;
2713 break;
2715 arg_types >>= SLJIT_ARG_SHIFT;
2718 return SLJIT_SUCCESS;
2721 #endif /* __SOFTFP__ */
2723 #undef EMIT_FPU_OPERATION
2725 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_call(struct sljit_compiler *compiler, sljit_s32 type,
2726 sljit_s32 arg_types)
2728 #ifdef __SOFTFP__
2729 struct sljit_jump *jump;
2730 sljit_u32 extra_space = (sljit_u32)type;
2731 #endif
2733 CHECK_ERROR_PTR();
2734 CHECK_PTR(check_sljit_emit_call(compiler, type, arg_types));
2736 #ifdef __SOFTFP__
2737 PTR_FAIL_IF(softfloat_call_with_args(compiler, arg_types, NULL, &extra_space));
2738 SLJIT_ASSERT((extra_space & 0x7) == 0);
2740 if ((type & SLJIT_CALL_RETURN) && extra_space == 0)
2741 type = SLJIT_JUMP | (type & SLJIT_REWRITABLE_JUMP);
2743 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2744 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2745 compiler->skip_checks = 1;
2746 #endif
2748 jump = sljit_emit_jump(compiler, type);
2749 PTR_FAIL_IF(jump == NULL);
2751 if (extra_space > 0) {
2752 if (type & SLJIT_CALL_RETURN)
2753 PTR_FAIL_IF(push_inst(compiler, EMIT_DATA_TRANSFER(WORD_SIZE | LOAD_DATA, 1,
2754 TMP_REG2, SLJIT_SP, extra_space - sizeof(sljit_sw))));
2756 PTR_FAIL_IF(push_inst(compiler, ADD | RD(SLJIT_SP) | RN(SLJIT_SP) | SRC2_IMM | extra_space));
2758 if (type & SLJIT_CALL_RETURN) {
2759 PTR_FAIL_IF(push_inst(compiler, BX | RM(TMP_REG2)));
2760 return jump;
2764 SLJIT_ASSERT(!(type & SLJIT_CALL_RETURN));
2765 PTR_FAIL_IF(softfloat_post_call_with_args(compiler, arg_types));
2766 return jump;
2767 #else /* !__SOFTFP__ */
2768 if (type & SLJIT_CALL_RETURN) {
2769 PTR_FAIL_IF(emit_stack_frame_release(compiler, -1));
2770 type = SLJIT_JUMP | (type & SLJIT_REWRITABLE_JUMP);
2773 PTR_FAIL_IF(hardfloat_call_with_args(compiler, arg_types));
2775 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2776 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2777 compiler->skip_checks = 1;
2778 #endif
2780 return sljit_emit_jump(compiler, type);
2781 #endif /* __SOFTFP__ */
2784 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_ijump(struct sljit_compiler *compiler, sljit_s32 type, sljit_s32 src, sljit_sw srcw)
2786 struct sljit_jump *jump;
2788 CHECK_ERROR();
2789 CHECK(check_sljit_emit_ijump(compiler, type, src, srcw));
2790 ADJUST_LOCAL_OFFSET(src, srcw);
2792 SLJIT_ASSERT(reg_map[TMP_REG1] != 14);
2794 if (!(src & SLJIT_IMM)) {
2795 if (FAST_IS_REG(src)) {
2796 SLJIT_ASSERT(reg_map[src] != 14);
2797 return push_inst(compiler, (type <= SLJIT_JUMP ? BX : BLX) | RM(src));
2800 SLJIT_ASSERT(src & SLJIT_MEM);
2801 FAIL_IF(emit_op_mem(compiler, WORD_SIZE | LOAD_DATA, TMP_REG1, src, srcw, TMP_REG1));
2802 return push_inst(compiler, (type <= SLJIT_JUMP ? BX : BLX) | RM(TMP_REG1));
2805 /* These jumps are converted to jump/call instructions when possible. */
2806 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
2807 FAIL_IF(!jump);
2808 set_jump(jump, compiler, JUMP_ADDR | ((type >= SLJIT_FAST_CALL) ? IS_BL : 0));
2809 jump->u.target = (sljit_uw)srcw;
2811 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
2812 if (type >= SLJIT_FAST_CALL)
2813 FAIL_IF(prepare_blx(compiler));
2814 FAIL_IF(push_inst_with_unique_literal(compiler, EMIT_DATA_TRANSFER(WORD_SIZE | LOAD_DATA, 1, type <= SLJIT_JUMP ? TMP_PC : TMP_REG1, TMP_PC, 0), 0));
2815 if (type >= SLJIT_FAST_CALL)
2816 FAIL_IF(emit_blx(compiler));
2817 #else
2818 FAIL_IF(emit_imm(compiler, TMP_REG1, 0));
2819 FAIL_IF(push_inst(compiler, (type <= SLJIT_JUMP ? BX : BLX) | RM(TMP_REG1)));
2820 #endif
2821 jump->addr = compiler->size;
2822 return SLJIT_SUCCESS;
2825 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_icall(struct sljit_compiler *compiler, sljit_s32 type,
2826 sljit_s32 arg_types,
2827 sljit_s32 src, sljit_sw srcw)
2829 #ifdef __SOFTFP__
2830 sljit_u32 extra_space = (sljit_u32)type;
2831 #endif
2833 CHECK_ERROR();
2834 CHECK(check_sljit_emit_icall(compiler, type, arg_types, src, srcw));
2836 if (src & SLJIT_MEM) {
2837 FAIL_IF(emit_op_mem(compiler, WORD_SIZE | LOAD_DATA, TMP_REG1, src, srcw, TMP_REG1));
2838 src = TMP_REG1;
2841 if ((type & SLJIT_CALL_RETURN) && (src >= SLJIT_FIRST_SAVED_REG && src <= SLJIT_S0)) {
2842 FAIL_IF(push_inst(compiler, MOV | RD(TMP_REG1) | RM(src)));
2843 src = TMP_REG1;
2846 #ifdef __SOFTFP__
2847 FAIL_IF(softfloat_call_with_args(compiler, arg_types, &src, &extra_space));
2848 SLJIT_ASSERT((extra_space & 0x7) == 0);
2850 if ((type & SLJIT_CALL_RETURN) && extra_space == 0)
2851 type = SLJIT_JUMP;
2853 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2854 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2855 compiler->skip_checks = 1;
2856 #endif
2858 FAIL_IF(sljit_emit_ijump(compiler, type, src, srcw));
2860 if (extra_space > 0) {
2861 if (type & SLJIT_CALL_RETURN)
2862 FAIL_IF(push_inst(compiler, EMIT_DATA_TRANSFER(WORD_SIZE | LOAD_DATA, 1,
2863 TMP_REG2, SLJIT_SP, extra_space - sizeof(sljit_sw))));
2865 FAIL_IF(push_inst(compiler, ADD | RD(SLJIT_SP) | RN(SLJIT_SP) | SRC2_IMM | extra_space));
2867 if (type & SLJIT_CALL_RETURN)
2868 return push_inst(compiler, BX | RM(TMP_REG2));
2871 SLJIT_ASSERT(!(type & SLJIT_CALL_RETURN));
2872 return softfloat_post_call_with_args(compiler, arg_types);
2873 #else /* !__SOFTFP__ */
2874 if (type & SLJIT_CALL_RETURN) {
2875 FAIL_IF(emit_stack_frame_release(compiler, -1));
2876 type = SLJIT_JUMP;
2879 FAIL_IF(hardfloat_call_with_args(compiler, arg_types));
2881 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2882 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2883 compiler->skip_checks = 1;
2884 #endif
2886 return sljit_emit_ijump(compiler, type, src, srcw);
2887 #endif /* __SOFTFP__ */
2890 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_flags(struct sljit_compiler *compiler, sljit_s32 op,
2891 sljit_s32 dst, sljit_sw dstw,
2892 sljit_s32 type)
2894 sljit_s32 dst_reg, flags = GET_ALL_FLAGS(op);
2895 sljit_uw cc, ins;
2897 CHECK_ERROR();
2898 CHECK(check_sljit_emit_op_flags(compiler, op, dst, dstw, type));
2899 ADJUST_LOCAL_OFFSET(dst, dstw);
2901 op = GET_OPCODE(op);
2902 cc = get_cc(compiler, type);
2903 dst_reg = FAST_IS_REG(dst) ? dst : TMP_REG1;
2905 if (op < SLJIT_ADD) {
2906 FAIL_IF(push_inst(compiler, MOV | RD(dst_reg) | SRC2_IMM | 0));
2907 FAIL_IF(push_inst(compiler, ((MOV | RD(dst_reg) | SRC2_IMM | 1) & ~COND_MASK) | cc));
2908 if (dst & SLJIT_MEM)
2909 return emit_op_mem(compiler, WORD_SIZE, TMP_REG1, dst, dstw, TMP_REG2);
2910 return SLJIT_SUCCESS;
2913 ins = (op == SLJIT_AND ? AND : (op == SLJIT_OR ? ORR : EOR));
2915 if (dst & SLJIT_MEM)
2916 FAIL_IF(emit_op_mem(compiler, WORD_SIZE | LOAD_DATA, TMP_REG1, dst, dstw, TMP_REG2));
2918 FAIL_IF(push_inst(compiler, ((ins | RD(dst_reg) | RN(dst_reg) | SRC2_IMM | 1) & ~COND_MASK) | cc));
2920 if (op == SLJIT_AND)
2921 FAIL_IF(push_inst(compiler, ((ins | RD(dst_reg) | RN(dst_reg) | SRC2_IMM | 0) & ~COND_MASK) | (cc ^ 0x10000000)));
2923 if (dst & SLJIT_MEM)
2924 FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG1, dst, dstw, TMP_REG2));
2926 if (flags & SLJIT_SET_Z)
2927 return push_inst(compiler, MOV | SET_FLAGS | RD(TMP_REG2) | RM(dst_reg));
2928 return SLJIT_SUCCESS;
2931 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_cmov(struct sljit_compiler *compiler, sljit_s32 type,
2932 sljit_s32 dst_reg,
2933 sljit_s32 src, sljit_sw srcw)
2935 sljit_uw cc, tmp;
2937 CHECK_ERROR();
2938 CHECK(check_sljit_emit_cmov(compiler, type, dst_reg, src, srcw));
2940 dst_reg &= ~SLJIT_32;
2942 cc = get_cc(compiler, type);
2944 if (SLJIT_UNLIKELY(src & SLJIT_IMM)) {
2945 tmp = get_imm((sljit_uw)srcw);
2946 if (tmp)
2947 return push_inst(compiler, ((MOV | RD(dst_reg) | tmp) & ~COND_MASK) | cc);
2949 tmp = get_imm(~(sljit_uw)srcw);
2950 if (tmp)
2951 return push_inst(compiler, ((MVN | RD(dst_reg) | tmp) & ~COND_MASK) | cc);
2953 #if (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
2954 tmp = (sljit_uw)srcw;
2955 FAIL_IF(push_inst(compiler, (MOVW & ~COND_MASK) | cc | RD(dst_reg) | ((tmp << 4) & 0xf0000) | (tmp & 0xfff)));
2956 if (tmp <= 0xffff)
2957 return SLJIT_SUCCESS;
2958 return push_inst(compiler, (MOVT & ~COND_MASK) | cc | RD(dst_reg) | ((tmp >> 12) & 0xf0000) | ((tmp >> 16) & 0xfff));
2959 #else
2960 FAIL_IF(load_immediate(compiler, TMP_REG1, (sljit_uw)srcw));
2961 src = TMP_REG1;
2962 #endif
2965 return push_inst(compiler, ((MOV | RD(dst_reg) | RM(src)) & ~COND_MASK) | cc);
2968 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_mem(struct sljit_compiler *compiler, sljit_s32 type,
2969 sljit_s32 reg,
2970 sljit_s32 mem, sljit_sw memw)
2972 sljit_s32 flags;
2973 sljit_uw is_type1_transfer, inst;
2975 CHECK_ERROR();
2976 CHECK(check_sljit_emit_mem(compiler, type, reg, mem, memw));
2978 is_type1_transfer = 1;
2980 switch (type & 0xff) {
2981 case SLJIT_MOV:
2982 case SLJIT_MOV_U32:
2983 case SLJIT_MOV_S32:
2984 case SLJIT_MOV32:
2985 case SLJIT_MOV_P:
2986 flags = WORD_SIZE;
2987 break;
2988 case SLJIT_MOV_U8:
2989 flags = BYTE_SIZE;
2990 break;
2991 case SLJIT_MOV_S8:
2992 if (!(type & SLJIT_MEM_STORE))
2993 is_type1_transfer = 0;
2994 flags = BYTE_SIZE | SIGNED;
2995 break;
2996 case SLJIT_MOV_U16:
2997 is_type1_transfer = 0;
2998 flags = HALF_SIZE;
2999 break;
3000 case SLJIT_MOV_S16:
3001 is_type1_transfer = 0;
3002 flags = HALF_SIZE | SIGNED;
3003 break;
3004 default:
3005 SLJIT_UNREACHABLE();
3006 flags = WORD_SIZE;
3007 break;
3010 if (!(type & SLJIT_MEM_STORE))
3011 flags |= LOAD_DATA;
3013 SLJIT_ASSERT(is_type1_transfer == !!IS_TYPE1_TRANSFER(flags));
3015 if (SLJIT_UNLIKELY(mem & OFFS_REG_MASK)) {
3016 if (!is_type1_transfer && memw != 0)
3017 return SLJIT_ERR_UNSUPPORTED;
3019 else {
3020 if (is_type1_transfer) {
3021 if (memw > 4095 || memw < -4095)
3022 return SLJIT_ERR_UNSUPPORTED;
3024 else {
3025 if (memw > 255 || memw < -255)
3026 return SLJIT_ERR_UNSUPPORTED;
3030 if (type & SLJIT_MEM_SUPP)
3031 return SLJIT_SUCCESS;
3033 if (SLJIT_UNLIKELY(mem & OFFS_REG_MASK)) {
3034 memw &= 0x3;
3036 inst = EMIT_DATA_TRANSFER(flags, 1, reg, mem & REG_MASK, RM(OFFS_REG(mem)) | ((sljit_uw)memw << 7));
3038 if (is_type1_transfer)
3039 inst |= (1 << 25);
3041 if (type & SLJIT_MEM_PRE)
3042 inst |= (1 << 21);
3043 else
3044 inst ^= (1 << 24);
3046 return push_inst(compiler, inst);
3049 inst = EMIT_DATA_TRANSFER(flags, 0, reg, mem & REG_MASK, 0);
3051 if (type & SLJIT_MEM_PRE)
3052 inst |= (1 << 21);
3053 else
3054 inst ^= (1 << 24);
3056 if (is_type1_transfer) {
3057 if (memw >= 0)
3058 inst |= (1 << 23);
3059 else
3060 memw = -memw;
3062 return push_inst(compiler, inst | (sljit_uw)memw);
3065 if (memw >= 0)
3066 inst |= (1 << 23);
3067 else
3068 memw = -memw;
3070 return push_inst(compiler, inst | TYPE2_TRANSFER_IMM((sljit_uw)memw));
3073 SLJIT_API_FUNC_ATTRIBUTE struct sljit_const* sljit_emit_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw, sljit_sw init_value)
3075 struct sljit_const *const_;
3076 sljit_s32 dst_r;
3078 CHECK_ERROR_PTR();
3079 CHECK_PTR(check_sljit_emit_const(compiler, dst, dstw, init_value));
3080 ADJUST_LOCAL_OFFSET(dst, dstw);
3082 dst_r = FAST_IS_REG(dst) ? dst : TMP_REG2;
3084 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
3085 PTR_FAIL_IF(push_inst_with_unique_literal(compiler,
3086 EMIT_DATA_TRANSFER(WORD_SIZE | LOAD_DATA, 1, dst_r, TMP_PC, 0), (sljit_uw)init_value));
3087 compiler->patches++;
3088 #else
3089 PTR_FAIL_IF(emit_imm(compiler, dst_r, init_value));
3090 #endif
3092 const_ = (struct sljit_const*)ensure_abuf(compiler, sizeof(struct sljit_const));
3093 PTR_FAIL_IF(!const_);
3094 set_const(const_, compiler);
3096 if (dst & SLJIT_MEM)
3097 PTR_FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG2, dst, dstw, TMP_REG1));
3098 return const_;
3101 SLJIT_API_FUNC_ATTRIBUTE struct sljit_put_label* sljit_emit_put_label(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw)
3103 struct sljit_put_label *put_label;
3104 sljit_s32 dst_r;
3106 CHECK_ERROR_PTR();
3107 CHECK_PTR(check_sljit_emit_put_label(compiler, dst, dstw));
3108 ADJUST_LOCAL_OFFSET(dst, dstw);
3110 dst_r = FAST_IS_REG(dst) ? dst : TMP_REG2;
3112 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
3113 PTR_FAIL_IF(push_inst_with_unique_literal(compiler, EMIT_DATA_TRANSFER(WORD_SIZE | LOAD_DATA, 1, dst_r, TMP_PC, 0), 0));
3114 compiler->patches++;
3115 #else
3116 PTR_FAIL_IF(emit_imm(compiler, dst_r, 0));
3117 #endif
3119 put_label = (struct sljit_put_label*)ensure_abuf(compiler, sizeof(struct sljit_put_label));
3120 PTR_FAIL_IF(!put_label);
3121 set_put_label(put_label, compiler, 0);
3123 if (dst & SLJIT_MEM)
3124 PTR_FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG2, dst, dstw, TMP_REG1));
3125 return put_label;
3128 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_target, sljit_sw executable_offset)
3130 inline_set_jump_addr(addr, executable_offset, new_target, 1);
3133 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_const(sljit_uw addr, sljit_sw new_constant, sljit_sw executable_offset)
3135 inline_set_const(addr, executable_offset, (sljit_uw)new_constant, 1);