Implement tail call on ARM.
[sljit.git] / sljit_src / sljitNativeARM_32.c
blob56cf21bef1d4c180baeda399da3b4de0b225ac70
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, 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 VSTR_F32 0xed000a00
132 #define VSUB_F32 0xee300a40
134 #if (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
135 /* Arm v7 specific instructions. */
136 #define MOVW 0xe3000000
137 #define MOVT 0xe3400000
138 #define SXTB 0xe6af0070
139 #define SXTH 0xe6bf0070
140 #define UXTB 0xe6ef0070
141 #define UXTH 0xe6ff0070
142 #endif
144 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
146 static sljit_s32 push_cpool(struct sljit_compiler *compiler)
148 /* Pushing the constant pool into the instruction stream. */
149 sljit_uw* inst;
150 sljit_uw* cpool_ptr;
151 sljit_uw* cpool_end;
152 sljit_s32 i;
154 /* The label could point the address after the constant pool. */
155 if (compiler->last_label && compiler->last_label->size == compiler->size)
156 compiler->last_label->size += compiler->cpool_fill + (CONST_POOL_ALIGNMENT - 1) + 1;
158 SLJIT_ASSERT(compiler->cpool_fill > 0 && compiler->cpool_fill <= CPOOL_SIZE);
159 inst = (sljit_uw*)ensure_buf(compiler, sizeof(sljit_uw));
160 FAIL_IF(!inst);
161 compiler->size++;
162 *inst = 0xff000000 | compiler->cpool_fill;
164 for (i = 0; i < CONST_POOL_ALIGNMENT - 1; i++) {
165 inst = (sljit_uw*)ensure_buf(compiler, sizeof(sljit_uw));
166 FAIL_IF(!inst);
167 compiler->size++;
168 *inst = 0;
171 cpool_ptr = compiler->cpool;
172 cpool_end = cpool_ptr + compiler->cpool_fill;
173 while (cpool_ptr < cpool_end) {
174 inst = (sljit_uw*)ensure_buf(compiler, sizeof(sljit_uw));
175 FAIL_IF(!inst);
176 compiler->size++;
177 *inst = *cpool_ptr++;
179 compiler->cpool_diff = CONST_POOL_EMPTY;
180 compiler->cpool_fill = 0;
181 return SLJIT_SUCCESS;
184 static sljit_s32 push_inst(struct sljit_compiler *compiler, sljit_uw inst)
186 sljit_uw* ptr;
188 if (SLJIT_UNLIKELY(compiler->cpool_diff != CONST_POOL_EMPTY && compiler->size - compiler->cpool_diff >= MAX_DIFFERENCE(4092)))
189 FAIL_IF(push_cpool(compiler));
191 ptr = (sljit_uw*)ensure_buf(compiler, sizeof(sljit_uw));
192 FAIL_IF(!ptr);
193 compiler->size++;
194 *ptr = inst;
195 return SLJIT_SUCCESS;
198 static sljit_s32 push_inst_with_literal(struct sljit_compiler *compiler, sljit_uw inst, sljit_uw literal)
200 sljit_uw* ptr;
201 sljit_uw cpool_index = CPOOL_SIZE;
202 sljit_uw* cpool_ptr;
203 sljit_uw* cpool_end;
204 sljit_u8* cpool_unique_ptr;
206 if (SLJIT_UNLIKELY(compiler->cpool_diff != CONST_POOL_EMPTY && compiler->size - compiler->cpool_diff >= MAX_DIFFERENCE(4092)))
207 FAIL_IF(push_cpool(compiler));
208 else if (compiler->cpool_fill > 0) {
209 cpool_ptr = compiler->cpool;
210 cpool_end = cpool_ptr + compiler->cpool_fill;
211 cpool_unique_ptr = compiler->cpool_unique;
212 do {
213 if ((*cpool_ptr == literal) && !(*cpool_unique_ptr)) {
214 cpool_index = (sljit_uw)(cpool_ptr - compiler->cpool);
215 break;
217 cpool_ptr++;
218 cpool_unique_ptr++;
219 } while (cpool_ptr < cpool_end);
222 if (cpool_index == CPOOL_SIZE) {
223 /* Must allocate a new entry in the literal pool. */
224 if (compiler->cpool_fill < CPOOL_SIZE) {
225 cpool_index = compiler->cpool_fill;
226 compiler->cpool_fill++;
228 else {
229 FAIL_IF(push_cpool(compiler));
230 cpool_index = 0;
231 compiler->cpool_fill = 1;
235 SLJIT_ASSERT((inst & 0xfff) == 0);
236 ptr = (sljit_uw*)ensure_buf(compiler, sizeof(sljit_uw));
237 FAIL_IF(!ptr);
238 compiler->size++;
239 *ptr = inst | cpool_index;
241 compiler->cpool[cpool_index] = literal;
242 compiler->cpool_unique[cpool_index] = 0;
243 if (compiler->cpool_diff == CONST_POOL_EMPTY)
244 compiler->cpool_diff = compiler->size;
245 return SLJIT_SUCCESS;
248 static sljit_s32 push_inst_with_unique_literal(struct sljit_compiler *compiler, sljit_uw inst, sljit_uw literal)
250 sljit_uw* ptr;
251 if (SLJIT_UNLIKELY((compiler->cpool_diff != CONST_POOL_EMPTY && compiler->size - compiler->cpool_diff >= MAX_DIFFERENCE(4092)) || compiler->cpool_fill >= CPOOL_SIZE))
252 FAIL_IF(push_cpool(compiler));
254 SLJIT_ASSERT(compiler->cpool_fill < CPOOL_SIZE && (inst & 0xfff) == 0);
255 ptr = (sljit_uw*)ensure_buf(compiler, sizeof(sljit_uw));
256 FAIL_IF(!ptr);
257 compiler->size++;
258 *ptr = inst | compiler->cpool_fill;
260 compiler->cpool[compiler->cpool_fill] = literal;
261 compiler->cpool_unique[compiler->cpool_fill] = 1;
262 compiler->cpool_fill++;
263 if (compiler->cpool_diff == CONST_POOL_EMPTY)
264 compiler->cpool_diff = compiler->size;
265 return SLJIT_SUCCESS;
268 static SLJIT_INLINE sljit_s32 prepare_blx(struct sljit_compiler *compiler)
270 /* Place for at least two instruction (doesn't matter whether the first has a literal). */
271 if (SLJIT_UNLIKELY(compiler->cpool_diff != CONST_POOL_EMPTY && compiler->size - compiler->cpool_diff >= MAX_DIFFERENCE(4088)))
272 return push_cpool(compiler);
273 return SLJIT_SUCCESS;
276 static SLJIT_INLINE sljit_s32 emit_blx(struct sljit_compiler *compiler)
278 /* Must follow tightly the previous instruction (to be able to convert it to bl instruction). */
279 SLJIT_ASSERT(compiler->cpool_diff == CONST_POOL_EMPTY || compiler->size - compiler->cpool_diff < MAX_DIFFERENCE(4092));
280 SLJIT_ASSERT(reg_map[TMP_REG1] != 14);
282 return push_inst(compiler, BLX | RM(TMP_REG1));
285 static sljit_uw patch_pc_relative_loads(sljit_uw *last_pc_patch, sljit_uw *code_ptr, sljit_uw* const_pool, sljit_uw cpool_size)
287 sljit_uw diff;
288 sljit_uw ind;
289 sljit_uw counter = 0;
290 sljit_uw* clear_const_pool = const_pool;
291 sljit_uw* clear_const_pool_end = const_pool + cpool_size;
293 SLJIT_ASSERT(const_pool - code_ptr <= CONST_POOL_ALIGNMENT);
294 /* Set unused flag for all literals in the constant pool.
295 I.e.: unused literals can belong to branches, which can be encoded as B or BL.
296 We can "compress" the constant pool by discarding these literals. */
297 while (clear_const_pool < clear_const_pool_end)
298 *clear_const_pool++ = (sljit_uw)(-1);
300 while (last_pc_patch < code_ptr) {
301 /* Data transfer instruction with Rn == r15. */
302 if ((*last_pc_patch & 0x0c0f0000) == 0x040f0000) {
303 diff = (sljit_uw)(const_pool - last_pc_patch);
304 ind = (*last_pc_patch) & 0xfff;
306 /* Must be a load instruction with immediate offset. */
307 SLJIT_ASSERT(ind < cpool_size && !(*last_pc_patch & (1 << 25)) && (*last_pc_patch & (1 << 20)));
308 if ((sljit_s32)const_pool[ind] < 0) {
309 const_pool[ind] = counter;
310 ind = counter;
311 counter++;
313 else
314 ind = const_pool[ind];
316 SLJIT_ASSERT(diff >= 1);
317 if (diff >= 2 || ind > 0) {
318 diff = (diff + (sljit_uw)ind - 2) << 2;
319 SLJIT_ASSERT(diff <= 0xfff);
320 *last_pc_patch = (*last_pc_patch & ~(sljit_uw)0xfff) | diff;
322 else
323 *last_pc_patch = (*last_pc_patch & ~(sljit_uw)(0xfff | (1 << 23))) | 0x004;
325 last_pc_patch++;
327 return counter;
330 /* In some rare ocasions we may need future patches. The probability is close to 0 in practice. */
331 struct future_patch {
332 struct future_patch* next;
333 sljit_s32 index;
334 sljit_s32 value;
337 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)
339 sljit_u32 value;
340 struct future_patch *curr_patch, *prev_patch;
342 SLJIT_UNUSED_ARG(compiler);
344 /* Using the values generated by patch_pc_relative_loads. */
345 if (!*first_patch)
346 value = cpool_start_address[cpool_current_index];
347 else {
348 curr_patch = *first_patch;
349 prev_patch = NULL;
350 while (1) {
351 if (!curr_patch) {
352 value = cpool_start_address[cpool_current_index];
353 break;
355 if ((sljit_uw)curr_patch->index == cpool_current_index) {
356 value = (sljit_uw)curr_patch->value;
357 if (prev_patch)
358 prev_patch->next = curr_patch->next;
359 else
360 *first_patch = curr_patch->next;
361 SLJIT_FREE(curr_patch, compiler->allocator_data);
362 break;
364 prev_patch = curr_patch;
365 curr_patch = curr_patch->next;
369 if ((sljit_sw)value >= 0) {
370 if (value > cpool_current_index) {
371 curr_patch = (struct future_patch*)SLJIT_MALLOC(sizeof(struct future_patch), compiler->allocator_data);
372 if (!curr_patch) {
373 while (*first_patch) {
374 curr_patch = *first_patch;
375 *first_patch = (*first_patch)->next;
376 SLJIT_FREE(curr_patch, compiler->allocator_data);
378 return SLJIT_ERR_ALLOC_FAILED;
380 curr_patch->next = *first_patch;
381 curr_patch->index = (sljit_sw)value;
382 curr_patch->value = (sljit_sw)cpool_start_address[value];
383 *first_patch = curr_patch;
385 cpool_start_address[value] = *buf_ptr;
387 return SLJIT_SUCCESS;
390 #else
392 static sljit_s32 push_inst(struct sljit_compiler *compiler, sljit_uw inst)
394 sljit_uw* ptr;
396 ptr = (sljit_uw*)ensure_buf(compiler, sizeof(sljit_uw));
397 FAIL_IF(!ptr);
398 compiler->size++;
399 *ptr = inst;
400 return SLJIT_SUCCESS;
403 static SLJIT_INLINE sljit_s32 emit_imm(struct sljit_compiler *compiler, sljit_s32 reg, sljit_sw imm)
405 FAIL_IF(push_inst(compiler, MOVW | RD(reg) | ((imm << 4) & 0xf0000) | ((sljit_u32)imm & 0xfff)));
406 return push_inst(compiler, MOVT | RD(reg) | ((imm >> 12) & 0xf0000) | (((sljit_u32)imm >> 16) & 0xfff));
409 #endif
411 static SLJIT_INLINE sljit_s32 detect_jump_type(struct sljit_jump *jump, sljit_uw *code_ptr, sljit_uw *code, sljit_sw executable_offset)
413 sljit_sw diff;
415 if (jump->flags & SLJIT_REWRITABLE_JUMP)
416 return 0;
418 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
419 if (jump->flags & IS_BL)
420 code_ptr--;
422 if (jump->flags & JUMP_ADDR)
423 diff = ((sljit_sw)jump->u.target - (sljit_sw)(code_ptr + 2) - executable_offset);
424 else {
425 SLJIT_ASSERT(jump->flags & JUMP_LABEL);
426 diff = ((sljit_sw)(code + jump->u.label->size) - (sljit_sw)(code_ptr + 2));
429 /* Branch to Thumb code has not been optimized yet. */
430 if (diff & 0x3)
431 return 0;
433 if (jump->flags & IS_BL) {
434 if (diff <= 0x01ffffff && diff >= -0x02000000) {
435 *code_ptr = (BL - CONDITIONAL) | (*(code_ptr + 1) & COND_MASK);
436 jump->flags |= PATCH_B;
437 return 1;
440 else {
441 if (diff <= 0x01ffffff && diff >= -0x02000000) {
442 *code_ptr = (B - CONDITIONAL) | (*code_ptr & COND_MASK);
443 jump->flags |= PATCH_B;
446 #else
447 if (jump->flags & JUMP_ADDR)
448 diff = ((sljit_sw)jump->u.target - (sljit_sw)code_ptr - executable_offset);
449 else {
450 SLJIT_ASSERT(jump->flags & JUMP_LABEL);
451 diff = ((sljit_sw)(code + jump->u.label->size) - (sljit_sw)code_ptr);
454 /* Branch to Thumb code has not been optimized yet. */
455 if (diff & 0x3)
456 return 0;
458 if (diff <= 0x01ffffff && diff >= -0x02000000) {
459 code_ptr -= 2;
460 *code_ptr = ((jump->flags & IS_BL) ? (BL - CONDITIONAL) : (B - CONDITIONAL)) | (code_ptr[2] & COND_MASK);
461 jump->flags |= PATCH_B;
462 return 1;
464 #endif
465 return 0;
468 static SLJIT_INLINE void inline_set_jump_addr(sljit_uw jump_ptr, sljit_sw executable_offset, sljit_uw new_addr, sljit_s32 flush_cache)
470 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
471 sljit_uw *ptr = (sljit_uw *)jump_ptr;
472 sljit_uw *inst = (sljit_uw *)ptr[0];
473 sljit_uw mov_pc = ptr[1];
474 sljit_s32 bl = (mov_pc & 0x0000f000) != RD(TMP_PC);
475 sljit_sw diff = (sljit_sw)(((sljit_sw)new_addr - (sljit_sw)(inst + 2) - executable_offset) >> 2);
477 SLJIT_UNUSED_ARG(executable_offset);
479 if (diff <= 0x7fffff && diff >= -0x800000) {
480 /* Turn to branch. */
481 if (!bl) {
482 if (flush_cache) {
483 SLJIT_UPDATE_WX_FLAGS(inst, inst + 1, 0);
485 inst[0] = (mov_pc & COND_MASK) | (B - CONDITIONAL) | (diff & 0xffffff);
486 if (flush_cache) {
487 SLJIT_UPDATE_WX_FLAGS(inst, inst + 1, 1);
488 inst = (sljit_uw *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
489 SLJIT_CACHE_FLUSH(inst, inst + 1);
491 } else {
492 if (flush_cache) {
493 SLJIT_UPDATE_WX_FLAGS(inst, inst + 2, 0);
495 inst[0] = (mov_pc & COND_MASK) | (BL - CONDITIONAL) | (diff & 0xffffff);
496 inst[1] = NOP;
497 if (flush_cache) {
498 SLJIT_UPDATE_WX_FLAGS(inst, inst + 2, 1);
499 inst = (sljit_uw *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
500 SLJIT_CACHE_FLUSH(inst, inst + 2);
503 } else {
504 /* Get the position of the constant. */
505 if (mov_pc & (1 << 23))
506 ptr = inst + ((mov_pc & 0xfff) >> 2) + 2;
507 else
508 ptr = inst + 1;
510 if (*inst != mov_pc) {
511 if (flush_cache) {
512 SLJIT_UPDATE_WX_FLAGS(inst, inst + (!bl ? 1 : 2), 0);
514 inst[0] = mov_pc;
515 if (!bl) {
516 if (flush_cache) {
517 SLJIT_UPDATE_WX_FLAGS(inst, inst + 1, 1);
518 inst = (sljit_uw *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
519 SLJIT_CACHE_FLUSH(inst, inst + 1);
521 } else {
522 inst[1] = BLX | RM(TMP_REG1);
523 if (flush_cache) {
524 SLJIT_UPDATE_WX_FLAGS(inst, inst + 2, 1);
525 inst = (sljit_uw *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
526 SLJIT_CACHE_FLUSH(inst, inst + 2);
531 if (flush_cache) {
532 SLJIT_UPDATE_WX_FLAGS(ptr, ptr + 1, 0);
535 *ptr = new_addr;
537 if (flush_cache) {
538 SLJIT_UPDATE_WX_FLAGS(ptr, ptr + 1, 1);
541 #else
542 sljit_uw *inst = (sljit_uw*)jump_ptr;
544 SLJIT_UNUSED_ARG(executable_offset);
546 SLJIT_ASSERT((inst[0] & 0xfff00000) == MOVW && (inst[1] & 0xfff00000) == MOVT);
548 if (flush_cache) {
549 SLJIT_UPDATE_WX_FLAGS(inst, inst + 2, 0);
552 inst[0] = MOVW | (inst[0] & 0xf000) | ((new_addr << 4) & 0xf0000) | (new_addr & 0xfff);
553 inst[1] = MOVT | (inst[1] & 0xf000) | ((new_addr >> 12) & 0xf0000) | ((new_addr >> 16) & 0xfff);
555 if (flush_cache) {
556 SLJIT_UPDATE_WX_FLAGS(inst, inst + 2, 1);
557 inst = (sljit_uw *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
558 SLJIT_CACHE_FLUSH(inst, inst + 2);
560 #endif
563 static sljit_uw get_imm(sljit_uw imm);
564 static sljit_s32 load_immediate(struct sljit_compiler *compiler, sljit_s32 reg, sljit_uw imm);
566 static SLJIT_INLINE void inline_set_const(sljit_uw addr, sljit_sw executable_offset, sljit_uw new_constant, sljit_s32 flush_cache)
568 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
569 sljit_uw *ptr = (sljit_uw*)addr;
570 sljit_uw *inst = (sljit_uw*)ptr[0];
571 sljit_uw ldr_literal = ptr[1];
572 sljit_uw src2;
574 SLJIT_UNUSED_ARG(executable_offset);
576 src2 = get_imm(new_constant);
577 if (src2) {
578 if (flush_cache) {
579 SLJIT_UPDATE_WX_FLAGS(inst, inst + 1, 0);
582 *inst = 0xe3a00000 | (ldr_literal & 0xf000) | src2;
584 if (flush_cache) {
585 SLJIT_UPDATE_WX_FLAGS(inst, inst + 1, 1);
586 inst = (sljit_uw *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
587 SLJIT_CACHE_FLUSH(inst, inst + 1);
589 return;
592 src2 = get_imm(~new_constant);
593 if (src2) {
594 if (flush_cache) {
595 SLJIT_UPDATE_WX_FLAGS(inst, inst + 1, 0);
598 *inst = 0xe3e00000 | (ldr_literal & 0xf000) | src2;
600 if (flush_cache) {
601 SLJIT_UPDATE_WX_FLAGS(inst, inst + 1, 1);
602 inst = (sljit_uw *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
603 SLJIT_CACHE_FLUSH(inst, inst + 1);
605 return;
608 if (ldr_literal & (1 << 23))
609 ptr = inst + ((ldr_literal & 0xfff) >> 2) + 2;
610 else
611 ptr = inst + 1;
613 if (*inst != ldr_literal) {
614 if (flush_cache) {
615 SLJIT_UPDATE_WX_FLAGS(inst, inst + 1, 0);
618 *inst = ldr_literal;
620 if (flush_cache) {
621 SLJIT_UPDATE_WX_FLAGS(inst, inst + 1, 1);
622 inst = (sljit_uw *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
623 SLJIT_CACHE_FLUSH(inst, inst + 1);
627 if (flush_cache) {
628 SLJIT_UPDATE_WX_FLAGS(ptr, ptr + 1, 0);
631 *ptr = new_constant;
633 if (flush_cache) {
634 SLJIT_UPDATE_WX_FLAGS(ptr, ptr + 1, 1);
636 #else
637 sljit_uw *inst = (sljit_uw*)addr;
639 SLJIT_UNUSED_ARG(executable_offset);
641 SLJIT_ASSERT((inst[0] & 0xfff00000) == MOVW && (inst[1] & 0xfff00000) == MOVT);
643 if (flush_cache) {
644 SLJIT_UPDATE_WX_FLAGS(inst, inst + 2, 0);
647 inst[0] = MOVW | (inst[0] & 0xf000) | ((new_constant << 4) & 0xf0000) | (new_constant & 0xfff);
648 inst[1] = MOVT | (inst[1] & 0xf000) | ((new_constant >> 12) & 0xf0000) | ((new_constant >> 16) & 0xfff);
650 if (flush_cache) {
651 SLJIT_UPDATE_WX_FLAGS(inst, inst + 2, 1);
652 inst = (sljit_uw *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
653 SLJIT_CACHE_FLUSH(inst, inst + 2);
655 #endif
658 SLJIT_API_FUNC_ATTRIBUTE void* sljit_generate_code(struct sljit_compiler *compiler)
660 struct sljit_memory_fragment *buf;
661 sljit_uw *code;
662 sljit_uw *code_ptr;
663 sljit_uw *buf_ptr;
664 sljit_uw *buf_end;
665 sljit_uw size;
666 sljit_uw word_count;
667 sljit_uw next_addr;
668 sljit_sw executable_offset;
669 sljit_uw addr;
670 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
671 sljit_uw cpool_size;
672 sljit_uw cpool_skip_alignment;
673 sljit_uw cpool_current_index;
674 sljit_uw *cpool_start_address;
675 sljit_uw *last_pc_patch;
676 struct future_patch *first_patch;
677 #endif
679 struct sljit_label *label;
680 struct sljit_jump *jump;
681 struct sljit_const *const_;
682 struct sljit_put_label *put_label;
684 CHECK_ERROR_PTR();
685 CHECK_PTR(check_sljit_generate_code(compiler));
686 reverse_buf(compiler);
688 /* Second code generation pass. */
689 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
690 size = compiler->size + (compiler->patches << 1);
691 if (compiler->cpool_fill > 0)
692 size += compiler->cpool_fill + CONST_POOL_ALIGNMENT - 1;
693 #else
694 size = compiler->size;
695 #endif
696 code = (sljit_uw*)SLJIT_MALLOC_EXEC(size * sizeof(sljit_uw), compiler->exec_allocator_data);
697 PTR_FAIL_WITH_EXEC_IF(code);
698 buf = compiler->buf;
700 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
701 cpool_size = 0;
702 cpool_skip_alignment = 0;
703 cpool_current_index = 0;
704 cpool_start_address = NULL;
705 first_patch = NULL;
706 last_pc_patch = code;
707 #endif
709 code_ptr = code;
710 word_count = 0;
711 next_addr = 1;
712 executable_offset = SLJIT_EXEC_OFFSET(code);
714 label = compiler->labels;
715 jump = compiler->jumps;
716 const_ = compiler->consts;
717 put_label = compiler->put_labels;
719 if (label && label->size == 0) {
720 label->addr = (sljit_uw)SLJIT_ADD_EXEC_OFFSET(code, executable_offset);
721 label = label->next;
724 do {
725 buf_ptr = (sljit_uw*)buf->memory;
726 buf_end = buf_ptr + (buf->used_size >> 2);
727 do {
728 word_count++;
729 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
730 if (cpool_size > 0) {
731 if (cpool_skip_alignment > 0) {
732 buf_ptr++;
733 cpool_skip_alignment--;
735 else {
736 if (SLJIT_UNLIKELY(resolve_const_pool_index(compiler, &first_patch, cpool_current_index, cpool_start_address, buf_ptr))) {
737 SLJIT_FREE_EXEC(code, compiler->exec_allocator_data);
738 compiler->error = SLJIT_ERR_ALLOC_FAILED;
739 return NULL;
741 buf_ptr++;
742 if (++cpool_current_index >= cpool_size) {
743 SLJIT_ASSERT(!first_patch);
744 cpool_size = 0;
745 if (label && label->size == word_count) {
746 /* Points after the current instruction. */
747 label->addr = (sljit_uw)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset);
748 label->size = (sljit_uw)(code_ptr - code);
749 label = label->next;
751 next_addr = compute_next_addr(label, jump, const_, put_label);
756 else if ((*buf_ptr & 0xff000000) != PUSH_POOL) {
757 #endif
758 *code_ptr = *buf_ptr++;
759 if (next_addr == word_count) {
760 SLJIT_ASSERT(!label || label->size >= word_count);
761 SLJIT_ASSERT(!jump || jump->addr >= word_count);
762 SLJIT_ASSERT(!const_ || const_->addr >= word_count);
763 SLJIT_ASSERT(!put_label || put_label->addr >= word_count);
765 /* These structures are ordered by their address. */
766 if (jump && jump->addr == word_count) {
767 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
768 if (detect_jump_type(jump, code_ptr, code, executable_offset))
769 code_ptr--;
770 jump->addr = (sljit_uw)code_ptr;
771 #else
772 jump->addr = (sljit_uw)(code_ptr - 2);
773 if (detect_jump_type(jump, code_ptr, code, executable_offset))
774 code_ptr -= 2;
775 #endif
776 jump = jump->next;
778 if (label && label->size == word_count) {
779 /* code_ptr can be affected above. */
780 label->addr = (sljit_uw)SLJIT_ADD_EXEC_OFFSET(code_ptr + 1, executable_offset);
781 label->size = (sljit_uw)((code_ptr + 1) - code);
782 label = label->next;
784 if (const_ && const_->addr == word_count) {
785 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
786 const_->addr = (sljit_uw)code_ptr;
787 #else
788 const_->addr = (sljit_uw)(code_ptr - 1);
789 #endif
790 const_ = const_->next;
792 if (put_label && put_label->addr == word_count) {
793 SLJIT_ASSERT(put_label->label);
794 put_label->addr = (sljit_uw)code_ptr;
795 put_label = put_label->next;
797 next_addr = compute_next_addr(label, jump, const_, put_label);
799 code_ptr++;
800 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
802 else {
803 /* Fortunately, no need to shift. */
804 cpool_size = *buf_ptr++ & ~PUSH_POOL;
805 SLJIT_ASSERT(cpool_size > 0);
806 cpool_start_address = ALIGN_INSTRUCTION(code_ptr + 1);
807 cpool_current_index = patch_pc_relative_loads(last_pc_patch, code_ptr, cpool_start_address, cpool_size);
808 if (cpool_current_index > 0) {
809 /* Unconditional branch. */
810 *code_ptr = B | (((sljit_uw)(cpool_start_address - code_ptr) + cpool_current_index - 2) & ~PUSH_POOL);
811 code_ptr = (sljit_uw*)(cpool_start_address + cpool_current_index);
813 cpool_skip_alignment = CONST_POOL_ALIGNMENT - 1;
814 cpool_current_index = 0;
815 last_pc_patch = code_ptr;
817 #endif
818 } while (buf_ptr < buf_end);
819 buf = buf->next;
820 } while (buf);
822 SLJIT_ASSERT(!label);
823 SLJIT_ASSERT(!jump);
824 SLJIT_ASSERT(!const_);
825 SLJIT_ASSERT(!put_label);
827 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
828 SLJIT_ASSERT(cpool_size == 0);
829 if (compiler->cpool_fill > 0) {
830 cpool_start_address = ALIGN_INSTRUCTION(code_ptr);
831 cpool_current_index = patch_pc_relative_loads(last_pc_patch, code_ptr, cpool_start_address, compiler->cpool_fill);
832 if (cpool_current_index > 0)
833 code_ptr = (sljit_uw*)(cpool_start_address + cpool_current_index);
835 buf_ptr = compiler->cpool;
836 buf_end = buf_ptr + compiler->cpool_fill;
837 cpool_current_index = 0;
838 while (buf_ptr < buf_end) {
839 if (SLJIT_UNLIKELY(resolve_const_pool_index(compiler, &first_patch, cpool_current_index, cpool_start_address, buf_ptr))) {
840 SLJIT_FREE_EXEC(code, compiler->exec_allocator_data);
841 compiler->error = SLJIT_ERR_ALLOC_FAILED;
842 return NULL;
844 buf_ptr++;
845 cpool_current_index++;
847 SLJIT_ASSERT(!first_patch);
849 #endif
851 jump = compiler->jumps;
852 while (jump) {
853 buf_ptr = (sljit_uw *)jump->addr;
855 if (jump->flags & PATCH_B) {
856 addr = (sljit_uw)SLJIT_ADD_EXEC_OFFSET(buf_ptr + 2, executable_offset);
857 if (!(jump->flags & JUMP_ADDR)) {
858 SLJIT_ASSERT(jump->flags & JUMP_LABEL);
859 SLJIT_ASSERT((sljit_sw)(jump->u.label->addr - addr) <= 0x01ffffff && (sljit_sw)(jump->u.label->addr - addr) >= -0x02000000);
860 *buf_ptr |= ((jump->u.label->addr - addr) >> 2) & 0x00ffffff;
862 else {
863 SLJIT_ASSERT((sljit_sw)(jump->u.target - addr) <= 0x01ffffff && (sljit_sw)(jump->u.target - addr) >= -0x02000000);
864 *buf_ptr |= ((jump->u.target - addr) >> 2) & 0x00ffffff;
867 else if (jump->flags & SLJIT_REWRITABLE_JUMP) {
868 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
869 jump->addr = (sljit_uw)code_ptr;
870 code_ptr[0] = (sljit_uw)buf_ptr;
871 code_ptr[1] = *buf_ptr;
872 inline_set_jump_addr((sljit_uw)code_ptr, executable_offset, (jump->flags & JUMP_LABEL) ? jump->u.label->addr : jump->u.target, 0);
873 code_ptr += 2;
874 #else
875 inline_set_jump_addr((sljit_uw)buf_ptr, executable_offset, (jump->flags & JUMP_LABEL) ? jump->u.label->addr : jump->u.target, 0);
876 #endif
878 else {
879 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
880 if (jump->flags & IS_BL)
881 buf_ptr--;
882 if (*buf_ptr & (1 << 23))
883 buf_ptr += ((*buf_ptr & 0xfff) >> 2) + 2;
884 else
885 buf_ptr += 1;
886 *buf_ptr = (jump->flags & JUMP_LABEL) ? jump->u.label->addr : jump->u.target;
887 #else
888 inline_set_jump_addr((sljit_uw)buf_ptr, executable_offset, (jump->flags & JUMP_LABEL) ? jump->u.label->addr : jump->u.target, 0);
889 #endif
891 jump = jump->next;
894 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
895 const_ = compiler->consts;
896 while (const_) {
897 buf_ptr = (sljit_uw*)const_->addr;
898 const_->addr = (sljit_uw)code_ptr;
900 code_ptr[0] = (sljit_uw)buf_ptr;
901 code_ptr[1] = *buf_ptr;
902 if (*buf_ptr & (1 << 23))
903 buf_ptr += ((*buf_ptr & 0xfff) >> 2) + 2;
904 else
905 buf_ptr += 1;
906 /* Set the value again (can be a simple constant). */
907 inline_set_const((sljit_uw)code_ptr, executable_offset, *buf_ptr, 0);
908 code_ptr += 2;
910 const_ = const_->next;
912 #endif
914 put_label = compiler->put_labels;
915 while (put_label) {
916 addr = put_label->label->addr;
917 buf_ptr = (sljit_uw*)put_label->addr;
919 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
920 SLJIT_ASSERT((buf_ptr[0] & 0xffff0000) == 0xe59f0000);
921 buf_ptr[((buf_ptr[0] & 0xfff) >> 2) + 2] = addr;
922 #else
923 SLJIT_ASSERT((buf_ptr[-1] & 0xfff00000) == MOVW && (buf_ptr[0] & 0xfff00000) == MOVT);
924 buf_ptr[-1] |= ((addr << 4) & 0xf0000) | (addr & 0xfff);
925 buf_ptr[0] |= ((addr >> 12) & 0xf0000) | ((addr >> 16) & 0xfff);
926 #endif
927 put_label = put_label->next;
930 SLJIT_ASSERT(code_ptr - code <= (sljit_s32)size);
932 compiler->error = SLJIT_ERR_COMPILED;
933 compiler->executable_offset = executable_offset;
934 compiler->executable_size = (sljit_uw)(code_ptr - code) * sizeof(sljit_uw);
936 code = (sljit_uw *)SLJIT_ADD_EXEC_OFFSET(code, executable_offset);
937 code_ptr = (sljit_uw *)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset);
939 SLJIT_CACHE_FLUSH(code, code_ptr);
940 SLJIT_UPDATE_WX_FLAGS(code, code_ptr, 1);
941 return code;
944 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_has_cpu_feature(sljit_s32 feature_type)
946 switch (feature_type) {
947 case SLJIT_HAS_FPU:
948 #ifdef SLJIT_IS_FPU_AVAILABLE
949 return SLJIT_IS_FPU_AVAILABLE;
950 #else
951 /* Available by default. */
952 return 1;
953 #endif
955 case SLJIT_HAS_CLZ:
956 case SLJIT_HAS_CMOV:
957 #if (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
958 case SLJIT_HAS_PREFETCH:
959 #endif
960 return 1;
962 default:
963 return 0;
967 /* --------------------------------------------------------------------- */
968 /* Entry, exit */
969 /* --------------------------------------------------------------------- */
971 /* Creates an index in data_transfer_insts array. */
972 #define WORD_SIZE 0x00
973 #define BYTE_SIZE 0x01
974 #define HALF_SIZE 0x02
975 #define PRELOAD 0x03
976 #define SIGNED 0x04
977 #define LOAD_DATA 0x08
979 /* Flag bits for emit_op. */
980 #define ALLOW_IMM 0x10
981 #define ALLOW_INV_IMM 0x20
982 #define ALLOW_ANY_IMM (ALLOW_IMM | ALLOW_INV_IMM)
984 /* s/l - store/load (1 bit)
985 u/s - signed/unsigned (1 bit)
986 w/b/h/N - word/byte/half/NOT allowed (2 bit)
987 Storing signed and unsigned values are the same operations. */
989 static const sljit_uw data_transfer_insts[16] = {
990 /* s u w */ 0xe5000000 /* str */,
991 /* s u b */ 0xe5400000 /* strb */,
992 /* s u h */ 0xe10000b0 /* strh */,
993 /* s u N */ 0x00000000 /* not allowed */,
994 /* s s w */ 0xe5000000 /* str */,
995 /* s s b */ 0xe5400000 /* strb */,
996 /* s s h */ 0xe10000b0 /* strh */,
997 /* s s N */ 0x00000000 /* not allowed */,
999 /* l u w */ 0xe5100000 /* ldr */,
1000 /* l u b */ 0xe5500000 /* ldrb */,
1001 /* l u h */ 0xe11000b0 /* ldrh */,
1002 /* l u p */ 0xf5500000 /* preload */,
1003 /* l s w */ 0xe5100000 /* ldr */,
1004 /* l s b */ 0xe11000d0 /* ldrsb */,
1005 /* l s h */ 0xe11000f0 /* ldrsh */,
1006 /* l s N */ 0x00000000 /* not allowed */,
1009 #define EMIT_DATA_TRANSFER(type, add, target_reg, base_reg, arg) \
1010 (data_transfer_insts[(type) & 0xf] | ((add) << 23) | RD(target_reg) | RN(base_reg) | (sljit_uw)(arg))
1012 /* Normal ldr/str instruction.
1013 Type2: ldrsb, ldrh, ldrsh */
1014 #define IS_TYPE1_TRANSFER(type) \
1015 (data_transfer_insts[(type) & 0xf] & 0x04000000)
1016 #define TYPE2_TRANSFER_IMM(imm) \
1017 (((imm) & 0xf) | (((imm) & 0xf0) << 4) | (1 << 22))
1019 #define EMIT_FPU_OPERATION(opcode, mode, dst, src1, src2) \
1020 ((sljit_uw)(opcode) | (sljit_uw)(mode) | VD(dst) | VM(src1) | VN(src2))
1022 static sljit_s32 emit_op(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 inp_flags,
1023 sljit_s32 dst, sljit_sw dstw,
1024 sljit_s32 src1, sljit_sw src1w,
1025 sljit_s32 src2, sljit_sw src2w);
1027 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_enter(struct sljit_compiler *compiler,
1028 sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
1029 sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
1031 sljit_uw imm, offset;
1032 sljit_s32 i, tmp, size, word_arg_count;
1033 #ifdef __SOFTFP__
1034 sljit_u32 float_arg_count;
1035 #else
1036 sljit_u32 old_offset, f32_offset;
1037 sljit_u32 remap[3];
1038 sljit_u32 *remap_ptr = remap;
1039 #endif
1041 CHECK_ERROR();
1042 CHECK(check_sljit_emit_enter(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size));
1043 set_emit_enter(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size);
1045 imm = 0;
1047 tmp = saveds < SLJIT_NUMBER_OF_SAVED_REGISTERS ? (SLJIT_S0 + 1 - saveds) : SLJIT_FIRST_SAVED_REG;
1048 for (i = SLJIT_S0; i >= tmp; i--)
1049 imm |= (sljit_uw)1 << reg_map[i];
1051 for (i = scratches; i >= SLJIT_FIRST_SAVED_REG; i--)
1052 imm |= (sljit_uw)1 << reg_map[i];
1054 SLJIT_ASSERT(reg_map[TMP_REG2] == 14);
1056 /* Push saved and temporary registers
1057 multiple registers: stmdb sp!, {..., lr}
1058 single register: str reg, [sp, #-4]! */
1059 if (imm != 0)
1060 FAIL_IF(push_inst(compiler, PUSH | (1 << 14) | imm));
1061 else
1062 FAIL_IF(push_inst(compiler, 0xe52d0004 | RD(TMP_REG2)));
1064 /* Stack must be aligned to 8 bytes: */
1065 size = GET_SAVED_REGISTERS_SIZE(scratches, saveds, 1);
1066 local_size = ((size + local_size + 7) & ~7) - size;
1067 compiler->local_size = local_size;
1069 arg_types >>= SLJIT_ARG_SHIFT;
1070 word_arg_count = 0;
1071 #ifdef __SOFTFP__
1072 SLJIT_COMPILE_ASSERT(SLJIT_FR0 == 1, float_register_index_start);
1074 offset = 0;
1075 float_arg_count = 0;
1077 while (arg_types) {
1078 switch (arg_types & SLJIT_ARG_MASK) {
1079 case SLJIT_ARG_TYPE_F64:
1080 if (offset & 0x7)
1081 offset += sizeof(sljit_sw);
1083 if (offset < 4 * sizeof(sljit_sw))
1084 FAIL_IF(push_inst(compiler, VMOV2 | (offset << 10) | ((offset + sizeof(sljit_sw)) << 14) | float_arg_count));
1085 else
1086 FAIL_IF(push_inst(compiler, VLDR_F32 | 0x800100 | RN(SLJIT_SP)
1087 | (float_arg_count << 12) | ((offset + (sljit_uw)size - 4 * sizeof(sljit_sw)) >> 2)));
1088 float_arg_count++;
1089 offset += sizeof(sljit_f64);
1090 break;
1091 case SLJIT_ARG_TYPE_F32:
1092 if (offset < 4 * sizeof(sljit_sw))
1093 FAIL_IF(push_inst(compiler, VMOV | (float_arg_count << 16) | (offset << 10)));
1094 else
1095 FAIL_IF(push_inst(compiler, VLDR_F32 | 0x800000 | RN(SLJIT_SP)
1096 | (float_arg_count << 12) | ((offset + (sljit_uw)size - 4 * sizeof(sljit_sw)) >> 2)));
1097 float_arg_count++;
1098 offset += sizeof(sljit_f32);
1099 break;
1100 default:
1101 if (offset < 4 * sizeof(sljit_sw))
1102 FAIL_IF(push_inst(compiler, MOV | RD(SLJIT_S0 - word_arg_count) | (offset >> 2)));
1103 else
1104 FAIL_IF(push_inst(compiler, data_transfer_insts[WORD_SIZE | LOAD_DATA] | 0x800000
1105 | RN(SLJIT_SP) | RD(SLJIT_S0 - word_arg_count) | (offset + (sljit_uw)size - 4 * sizeof(sljit_sw))));
1106 word_arg_count++;
1107 offset += sizeof(sljit_sw);
1108 break;
1110 arg_types >>= SLJIT_ARG_SHIFT;
1113 compiler->args_size = offset;
1114 #else
1115 offset = SLJIT_FR0;
1116 old_offset = SLJIT_FR0;
1117 f32_offset = 0;
1119 while (arg_types) {
1120 switch (arg_types & SLJIT_ARG_MASK) {
1121 case SLJIT_ARG_TYPE_F64:
1122 if (offset != old_offset)
1123 *remap_ptr++ = EMIT_FPU_OPERATION(VMOV_F32, SLJIT_32, offset, old_offset, 0);
1124 old_offset++;
1125 offset++;
1126 break;
1127 case SLJIT_ARG_TYPE_F32:
1128 if (f32_offset != 0) {
1129 *remap_ptr++ = EMIT_FPU_OPERATION(VMOV_F32, 0x20, offset, f32_offset, 0);
1130 f32_offset = 0;
1131 } else {
1132 if (offset != old_offset)
1133 *remap_ptr++ = EMIT_FPU_OPERATION(VMOV_F32, 0, offset, old_offset, 0);
1134 f32_offset = old_offset;
1135 old_offset++;
1137 offset++;
1138 break;
1139 default:
1140 FAIL_IF(push_inst(compiler, MOV | RD(SLJIT_S0 - word_arg_count) | RM(SLJIT_R0 + word_arg_count)));
1141 word_arg_count++;
1142 break;
1144 arg_types >>= SLJIT_ARG_SHIFT;
1147 SLJIT_ASSERT((sljit_uw)(remap_ptr - remap) <= sizeof(remap));
1149 while (remap_ptr > remap)
1150 FAIL_IF(push_inst(compiler, *(--remap_ptr)));
1151 #endif
1153 if (local_size > 0)
1154 FAIL_IF(emit_op(compiler, SLJIT_SUB, ALLOW_IMM, SLJIT_SP, 0, SLJIT_SP, 0, SLJIT_IMM, local_size));
1156 return SLJIT_SUCCESS;
1159 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_set_context(struct sljit_compiler *compiler,
1160 sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
1161 sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
1163 sljit_s32 size;
1165 CHECK_ERROR();
1166 CHECK(check_sljit_set_context(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size));
1167 set_set_context(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size);
1169 size = GET_SAVED_REGISTERS_SIZE(scratches, saveds, 1);
1170 compiler->local_size = ((size + local_size + 7) & ~7) - size;
1171 return SLJIT_SUCCESS;
1174 static sljit_s32 emit_stack_frame_release(struct sljit_compiler *compiler, sljit_s32 lr_dst)
1176 sljit_s32 i, tmp;
1177 sljit_uw imm;
1179 SLJIT_ASSERT(lr_dst == TMP_PC || lr_dst == TMP_REG2 || lr_dst == 0);
1180 SLJIT_ASSERT(reg_map[TMP_REG2] == 14);
1182 if (compiler->local_size > 0) {
1183 tmp = compiler->local_size;
1184 imm = get_imm((sljit_uw)tmp);
1186 if (imm == 0) {
1187 FAIL_IF(load_immediate(compiler, TMP_REG2, (sljit_uw)tmp));
1188 imm = RM(TMP_REG2);
1191 FAIL_IF(push_inst(compiler, ADD | RD(SLJIT_SP) | RN(SLJIT_SP) | imm));
1194 imm = 0;
1195 if (lr_dst != 0)
1196 imm |= (sljit_uw)1 << reg_map[lr_dst];
1198 tmp = compiler->saveds < SLJIT_NUMBER_OF_SAVED_REGISTERS ? (SLJIT_S0 + 1 - compiler->saveds) : SLJIT_FIRST_SAVED_REG;
1199 for (i = SLJIT_S0; i >= tmp; i--)
1200 imm |= (sljit_uw)1 << reg_map[i];
1202 for (i = compiler->scratches; i >= SLJIT_FIRST_SAVED_REG; i--)
1203 imm |= (sljit_uw)1 << reg_map[i];
1205 /* Pop saved and temporary registers
1206 multiple registers: ldmia sp!, {...}
1207 single register: ldr reg, [sp], #4 */
1208 if ((imm & (imm - 1)) == 0) {
1209 if (imm == 0)
1210 return SLJIT_SUCCESS;
1212 if (lr_dst == 0) {
1213 if (compiler->saveds > 0) {
1214 SLJIT_ASSERT(imm == ((sljit_uw)1 << reg_map[SLJIT_S0]));
1215 lr_dst = SLJIT_S0;
1216 } else {
1217 SLJIT_ASSERT(imm == ((sljit_uw)1 << reg_map[SLJIT_FIRST_SAVED_REG]));
1218 lr_dst = SLJIT_FIRST_SAVED_REG;
1222 return push_inst(compiler, 0xe49d0004 | RD(lr_dst));
1225 return push_inst(compiler, POP | imm);
1228 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_return_void(struct sljit_compiler *compiler)
1230 CHECK_ERROR();
1231 CHECK(check_sljit_emit_return_void(compiler));
1233 return emit_stack_frame_release(compiler, TMP_PC);
1236 /* --------------------------------------------------------------------- */
1237 /* Operators */
1238 /* --------------------------------------------------------------------- */
1240 /* flags: */
1241 /* Arguments are swapped. */
1242 #define ARGS_SWAPPED 0x01
1243 /* Inverted immediate. */
1244 #define INV_IMM 0x02
1245 /* Source and destination is register. */
1246 #define MOVE_REG_CONV 0x04
1247 /* Unused return value. */
1248 #define UNUSED_RETURN 0x08
1249 /* SET_FLAGS must be (1 << 20) as it is also the value of S bit (can be used for optimization). */
1250 #define SET_FLAGS (1 << 20)
1251 /* dst: reg
1252 src1: reg
1253 src2: reg or imm (if allowed)
1254 SRC2_IMM must be (1 << 25) as it is also the value of I bit (can be used for optimization). */
1255 #define SRC2_IMM (1 << 25)
1257 #define EMIT_SHIFT_INS_AND_RETURN(opcode) \
1258 SLJIT_ASSERT(!(flags & INV_IMM) && !(src2 & SRC2_IMM)); \
1259 if (compiler->shift_imm != 0x20) { \
1260 SLJIT_ASSERT(src1 == TMP_REG1); \
1261 SLJIT_ASSERT(!(flags & ARGS_SWAPPED)); \
1263 if (compiler->shift_imm != 0) \
1264 return push_inst(compiler, MOV | (flags & SET_FLAGS) | \
1265 RD(dst) | (compiler->shift_imm << 7) | (opcode << 5) | RM(src2)); \
1266 return push_inst(compiler, MOV | (flags & SET_FLAGS) | RD(dst) | RM(src2)); \
1268 return push_inst(compiler, MOV | (flags & SET_FLAGS) | RD(dst) \
1269 | RM8((flags & ARGS_SWAPPED) ? src1 : src2) | (sljit_uw)(opcode << 5) \
1270 | 0x10 | RM((flags & ARGS_SWAPPED) ? src2 : src1));
1272 static SLJIT_INLINE sljit_s32 emit_single_op(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 flags,
1273 sljit_uw dst, sljit_uw src1, sljit_uw src2)
1275 switch (GET_OPCODE(op)) {
1276 case SLJIT_MOV:
1277 SLJIT_ASSERT(src1 == TMP_REG1 && !(flags & ARGS_SWAPPED));
1278 if (dst != src2) {
1279 if (src2 & SRC2_IMM) {
1280 return push_inst(compiler, ((flags & INV_IMM) ? MVN : MOV) | RD(dst) | src2);
1282 return push_inst(compiler, MOV | RD(dst) | RM(src2));
1284 return SLJIT_SUCCESS;
1286 case SLJIT_MOV_U8:
1287 case SLJIT_MOV_S8:
1288 SLJIT_ASSERT(src1 == TMP_REG1 && !(flags & ARGS_SWAPPED));
1289 if (flags & MOVE_REG_CONV) {
1290 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
1291 if (op == SLJIT_MOV_U8)
1292 return push_inst(compiler, AND | RD(dst) | RN(src2) | SRC2_IMM | 0xff);
1293 FAIL_IF(push_inst(compiler, MOV | RD(dst) | (24 << 7) | RM(src2)));
1294 return push_inst(compiler, MOV | RD(dst) | (24 << 7) | (op == SLJIT_MOV_U8 ? 0x20 : 0x40) | RM(dst));
1295 #else
1296 return push_inst(compiler, (op == SLJIT_MOV_U8 ? UXTB : SXTB) | RD(dst) | RM(src2));
1297 #endif
1299 else if (dst != src2) {
1300 SLJIT_ASSERT(src2 & SRC2_IMM);
1301 return push_inst(compiler, ((flags & INV_IMM) ? MVN : MOV) | RD(dst) | src2);
1303 return SLJIT_SUCCESS;
1305 case SLJIT_MOV_U16:
1306 case SLJIT_MOV_S16:
1307 SLJIT_ASSERT(src1 == TMP_REG1 && !(flags & ARGS_SWAPPED));
1308 if (flags & MOVE_REG_CONV) {
1309 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
1310 FAIL_IF(push_inst(compiler, MOV | RD(dst) | (16 << 7) | RM(src2)));
1311 return push_inst(compiler, MOV | RD(dst) | (16 << 7) | (op == SLJIT_MOV_U16 ? 0x20 : 0x40) | RM(dst));
1312 #else
1313 return push_inst(compiler, (op == SLJIT_MOV_U16 ? UXTH : SXTH) | RD(dst) | RM(src2));
1314 #endif
1316 else if (dst != src2) {
1317 SLJIT_ASSERT(src2 & SRC2_IMM);
1318 return push_inst(compiler, ((flags & INV_IMM) ? MVN : MOV) | RD(dst) | src2);
1320 return SLJIT_SUCCESS;
1322 case SLJIT_NOT:
1323 if (src2 & SRC2_IMM)
1324 return push_inst(compiler, ((flags & INV_IMM) ? MOV : MVN) | (flags & SET_FLAGS) | RD(dst) | src2);
1326 return push_inst(compiler, MVN | (flags & SET_FLAGS) | RD(dst) | RM(src2));
1328 case SLJIT_CLZ:
1329 SLJIT_ASSERT(!(flags & INV_IMM));
1330 SLJIT_ASSERT(!(src2 & SRC2_IMM));
1331 FAIL_IF(push_inst(compiler, CLZ | RD(dst) | RM(src2)));
1332 return SLJIT_SUCCESS;
1334 case SLJIT_ADD:
1335 SLJIT_ASSERT(!(flags & INV_IMM));
1336 compiler->status_flags_state = SLJIT_CURRENT_FLAGS_ADD_SUB;
1338 if ((flags & (UNUSED_RETURN | ARGS_SWAPPED)) == UNUSED_RETURN)
1339 return push_inst(compiler, CMN | SET_FLAGS | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
1340 return push_inst(compiler, ADD | (flags & SET_FLAGS) | RD(dst) | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
1342 case SLJIT_ADDC:
1343 SLJIT_ASSERT(!(flags & INV_IMM));
1344 return push_inst(compiler, ADC | (flags & SET_FLAGS) | RD(dst) | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
1346 case SLJIT_SUB:
1347 SLJIT_ASSERT(!(flags & INV_IMM));
1348 compiler->status_flags_state = SLJIT_CURRENT_FLAGS_ADD_SUB;
1350 if ((flags & (UNUSED_RETURN | ARGS_SWAPPED)) == UNUSED_RETURN)
1351 return push_inst(compiler, CMP | SET_FLAGS | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
1353 return push_inst(compiler, (!(flags & ARGS_SWAPPED) ? SUB : RSB) | (flags & SET_FLAGS)
1354 | RD(dst) | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
1356 case SLJIT_SUBC:
1357 SLJIT_ASSERT(!(flags & INV_IMM));
1358 return push_inst(compiler, (!(flags & ARGS_SWAPPED) ? SBC : RSC) | (flags & SET_FLAGS)
1359 | RD(dst) | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
1361 case SLJIT_MUL:
1362 SLJIT_ASSERT(!(flags & INV_IMM));
1363 SLJIT_ASSERT(!(src2 & SRC2_IMM));
1364 compiler->status_flags_state = 0;
1366 if (!HAS_FLAGS(op))
1367 return push_inst(compiler, MUL | RN(dst) | RM8(src2) | RM(src1));
1369 FAIL_IF(push_inst(compiler, SMULL | RN(TMP_REG1) | RD(dst) | RM8(src2) | RM(src1)));
1371 /* cmp TMP_REG1, dst asr #31. */
1372 return push_inst(compiler, CMP | SET_FLAGS | RN(TMP_REG1) | RM(dst) | 0xfc0);
1374 case SLJIT_AND:
1375 if ((flags & (UNUSED_RETURN | INV_IMM)) == UNUSED_RETURN)
1376 return push_inst(compiler, TST | SET_FLAGS | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
1377 return push_inst(compiler, (!(flags & INV_IMM) ? AND : BIC) | (flags & SET_FLAGS)
1378 | RD(dst) | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
1380 case SLJIT_OR:
1381 SLJIT_ASSERT(!(flags & INV_IMM));
1382 return push_inst(compiler, ORR | (flags & SET_FLAGS) | RD(dst) | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
1384 case SLJIT_XOR:
1385 SLJIT_ASSERT(!(flags & INV_IMM));
1386 return push_inst(compiler, EOR | (flags & SET_FLAGS) | RD(dst) | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
1388 case SLJIT_SHL:
1389 EMIT_SHIFT_INS_AND_RETURN(0);
1391 case SLJIT_LSHR:
1392 EMIT_SHIFT_INS_AND_RETURN(1);
1394 case SLJIT_ASHR:
1395 EMIT_SHIFT_INS_AND_RETURN(2);
1398 SLJIT_UNREACHABLE();
1399 return SLJIT_SUCCESS;
1402 #undef EMIT_SHIFT_INS_AND_RETURN
1404 /* Tests whether the immediate can be stored in the 12 bit imm field.
1405 Returns with 0 if not possible. */
1406 static sljit_uw get_imm(sljit_uw imm)
1408 sljit_u32 rol;
1410 if (imm <= 0xff)
1411 return SRC2_IMM | imm;
1413 if (!(imm & 0xff000000)) {
1414 imm <<= 8;
1415 rol = 8;
1417 else {
1418 imm = (imm << 24) | (imm >> 8);
1419 rol = 0;
1422 if (!(imm & 0xff000000)) {
1423 imm <<= 8;
1424 rol += 4;
1427 if (!(imm & 0xf0000000)) {
1428 imm <<= 4;
1429 rol += 2;
1432 if (!(imm & 0xc0000000)) {
1433 imm <<= 2;
1434 rol += 1;
1437 if (!(imm & 0x00ffffff))
1438 return SRC2_IMM | (imm >> 24) | (rol << 8);
1439 else
1440 return 0;
1443 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
1444 static sljit_s32 generate_int(struct sljit_compiler *compiler, sljit_s32 reg, sljit_uw imm, sljit_s32 positive)
1446 sljit_uw mask;
1447 sljit_uw imm1;
1448 sljit_uw imm2;
1449 sljit_uw rol;
1451 /* Step1: Search a zero byte (8 continous zero bit). */
1452 mask = 0xff000000;
1453 rol = 8;
1454 while(1) {
1455 if (!(imm & mask)) {
1456 /* Rol imm by rol. */
1457 imm = (imm << rol) | (imm >> (32 - rol));
1458 /* Calculate arm rol. */
1459 rol = 4 + (rol >> 1);
1460 break;
1462 rol += 2;
1463 mask >>= 2;
1464 if (mask & 0x3) {
1465 /* rol by 8. */
1466 imm = (imm << 8) | (imm >> 24);
1467 mask = 0xff00;
1468 rol = 24;
1469 while (1) {
1470 if (!(imm & mask)) {
1471 /* Rol imm by rol. */
1472 imm = (imm << rol) | (imm >> (32 - rol));
1473 /* Calculate arm rol. */
1474 rol = (rol >> 1) - 8;
1475 break;
1477 rol += 2;
1478 mask >>= 2;
1479 if (mask & 0x3)
1480 return 0;
1482 break;
1486 /* The low 8 bit must be zero. */
1487 SLJIT_ASSERT(!(imm & 0xff));
1489 if (!(imm & 0xff000000)) {
1490 imm1 = SRC2_IMM | ((imm >> 16) & 0xff) | (((rol + 4) & 0xf) << 8);
1491 imm2 = SRC2_IMM | ((imm >> 8) & 0xff) | (((rol + 8) & 0xf) << 8);
1493 else if (imm & 0xc0000000) {
1494 imm1 = SRC2_IMM | ((imm >> 24) & 0xff) | ((rol & 0xf) << 8);
1495 imm <<= 8;
1496 rol += 4;
1498 if (!(imm & 0xff000000)) {
1499 imm <<= 8;
1500 rol += 4;
1503 if (!(imm & 0xf0000000)) {
1504 imm <<= 4;
1505 rol += 2;
1508 if (!(imm & 0xc0000000)) {
1509 imm <<= 2;
1510 rol += 1;
1513 if (!(imm & 0x00ffffff))
1514 imm2 = SRC2_IMM | (imm >> 24) | ((rol & 0xf) << 8);
1515 else
1516 return 0;
1518 else {
1519 if (!(imm & 0xf0000000)) {
1520 imm <<= 4;
1521 rol += 2;
1524 if (!(imm & 0xc0000000)) {
1525 imm <<= 2;
1526 rol += 1;
1529 imm1 = SRC2_IMM | ((imm >> 24) & 0xff) | ((rol & 0xf) << 8);
1530 imm <<= 8;
1531 rol += 4;
1533 if (!(imm & 0xf0000000)) {
1534 imm <<= 4;
1535 rol += 2;
1538 if (!(imm & 0xc0000000)) {
1539 imm <<= 2;
1540 rol += 1;
1543 if (!(imm & 0x00ffffff))
1544 imm2 = SRC2_IMM | (imm >> 24) | ((rol & 0xf) << 8);
1545 else
1546 return 0;
1549 FAIL_IF(push_inst(compiler, (positive ? MOV : MVN) | RD(reg) | imm1));
1550 FAIL_IF(push_inst(compiler, (positive ? ORR : BIC) | RD(reg) | RN(reg) | imm2));
1551 return 1;
1553 #endif
1555 static sljit_s32 load_immediate(struct sljit_compiler *compiler, sljit_s32 reg, sljit_uw imm)
1557 sljit_uw tmp;
1559 #if (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
1560 if (!(imm & ~(sljit_uw)0xffff))
1561 return push_inst(compiler, MOVW | RD(reg) | ((imm << 4) & 0xf0000) | (imm & 0xfff));
1562 #endif
1564 /* Create imm by 1 inst. */
1565 tmp = get_imm(imm);
1566 if (tmp)
1567 return push_inst(compiler, MOV | RD(reg) | tmp);
1569 tmp = get_imm(~imm);
1570 if (tmp)
1571 return push_inst(compiler, MVN | RD(reg) | tmp);
1573 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
1574 /* Create imm by 2 inst. */
1575 FAIL_IF(generate_int(compiler, reg, imm, 1));
1576 FAIL_IF(generate_int(compiler, reg, ~imm, 0));
1578 /* Load integer. */
1579 return push_inst_with_literal(compiler, EMIT_DATA_TRANSFER(WORD_SIZE | LOAD_DATA, 1, reg, TMP_PC, 0), imm);
1580 #else
1581 FAIL_IF(push_inst(compiler, MOVW | RD(reg) | ((imm << 4) & 0xf0000) | (imm & 0xfff)));
1582 if (imm <= 0xffff)
1583 return SLJIT_SUCCESS;
1584 return push_inst(compiler, MOVT | RD(reg) | ((imm >> 12) & 0xf0000) | ((imm >> 16) & 0xfff));
1585 #endif
1588 static SLJIT_INLINE sljit_s32 emit_op_mem(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg,
1589 sljit_s32 arg, sljit_sw argw, sljit_s32 tmp_reg)
1591 sljit_uw imm, offset_reg;
1592 sljit_uw is_type1_transfer = IS_TYPE1_TRANSFER(flags);
1594 SLJIT_ASSERT (arg & SLJIT_MEM);
1595 SLJIT_ASSERT((arg & REG_MASK) != tmp_reg);
1597 if (!(arg & REG_MASK)) {
1598 if (is_type1_transfer) {
1599 FAIL_IF(load_immediate(compiler, tmp_reg, (sljit_uw)argw & ~(sljit_uw)0xfff));
1600 argw &= 0xfff;
1602 else {
1603 FAIL_IF(load_immediate(compiler, tmp_reg, (sljit_uw)argw & ~(sljit_uw)0xff));
1604 argw &= 0xff;
1607 return push_inst(compiler, EMIT_DATA_TRANSFER(flags, 1, reg, tmp_reg,
1608 is_type1_transfer ? argw : TYPE2_TRANSFER_IMM(argw)));
1611 if (arg & OFFS_REG_MASK) {
1612 offset_reg = OFFS_REG(arg);
1613 arg &= REG_MASK;
1614 argw &= 0x3;
1616 if (argw != 0 && !is_type1_transfer) {
1617 FAIL_IF(push_inst(compiler, ADD | RD(tmp_reg) | RN(arg) | RM(offset_reg) | ((sljit_uw)argw << 7)));
1618 return push_inst(compiler, EMIT_DATA_TRANSFER(flags, 1, reg, tmp_reg, TYPE2_TRANSFER_IMM(0)));
1621 /* Bit 25: RM is offset. */
1622 return push_inst(compiler, EMIT_DATA_TRANSFER(flags, 1, reg, arg,
1623 RM(offset_reg) | (is_type1_transfer ? (1 << 25) : 0) | ((sljit_uw)argw << 7)));
1626 arg &= REG_MASK;
1628 if (is_type1_transfer) {
1629 if (argw > 0xfff) {
1630 imm = get_imm((sljit_uw)argw & ~(sljit_uw)0xfff);
1631 if (imm) {
1632 FAIL_IF(push_inst(compiler, ADD | RD(tmp_reg) | RN(arg) | imm));
1633 argw = argw & 0xfff;
1634 arg = tmp_reg;
1637 else if (argw < -0xfff) {
1638 imm = get_imm((sljit_uw)-argw & ~(sljit_uw)0xfff);
1639 if (imm) {
1640 FAIL_IF(push_inst(compiler, SUB | RD(tmp_reg) | RN(arg) | imm));
1641 argw = -(-argw & 0xfff);
1642 arg = tmp_reg;
1646 if (argw >= 0 && argw <= 0xfff)
1647 return push_inst(compiler, EMIT_DATA_TRANSFER(flags, 1, reg, arg, argw));
1649 if (argw < 0 && argw >= -0xfff)
1650 return push_inst(compiler, EMIT_DATA_TRANSFER(flags, 0, reg, arg, -argw));
1652 else {
1653 if (argw > 0xff) {
1654 imm = get_imm((sljit_uw)argw & ~(sljit_uw)0xff);
1655 if (imm) {
1656 FAIL_IF(push_inst(compiler, ADD | RD(tmp_reg) | RN(arg) | imm));
1657 argw = argw & 0xff;
1658 arg = tmp_reg;
1661 else if (argw < -0xff) {
1662 imm = get_imm((sljit_uw)-argw & ~(sljit_uw)0xff);
1663 if (imm) {
1664 FAIL_IF(push_inst(compiler, SUB | RD(tmp_reg) | RN(arg) | imm));
1665 argw = -(-argw & 0xff);
1666 arg = tmp_reg;
1670 if (argw >= 0 && argw <= 0xff)
1671 return push_inst(compiler, EMIT_DATA_TRANSFER(flags, 1, reg, arg, TYPE2_TRANSFER_IMM(argw)));
1673 if (argw < 0 && argw >= -0xff) {
1674 argw = -argw;
1675 return push_inst(compiler, EMIT_DATA_TRANSFER(flags, 0, reg, arg, TYPE2_TRANSFER_IMM(argw)));
1679 FAIL_IF(load_immediate(compiler, tmp_reg, (sljit_uw)argw));
1680 return push_inst(compiler, EMIT_DATA_TRANSFER(flags, 1, reg, arg,
1681 RM(tmp_reg) | (is_type1_transfer ? (1 << 25) : 0)));
1684 static sljit_s32 emit_op(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 inp_flags,
1685 sljit_s32 dst, sljit_sw dstw,
1686 sljit_s32 src1, sljit_sw src1w,
1687 sljit_s32 src2, sljit_sw src2w)
1689 /* src1 is reg or TMP_REG1
1690 src2 is reg, TMP_REG2, or imm
1691 result goes to TMP_REG2, so put result can use TMP_REG1. */
1693 /* We prefers register and simple consts. */
1694 sljit_s32 dst_reg;
1695 sljit_s32 src1_reg;
1696 sljit_s32 src2_reg;
1697 sljit_s32 flags = HAS_FLAGS(op) ? SET_FLAGS : 0;
1699 if (dst == TMP_REG2)
1700 flags |= UNUSED_RETURN;
1702 SLJIT_ASSERT(!(inp_flags & ALLOW_INV_IMM) || (inp_flags & ALLOW_IMM));
1704 src2_reg = 0;
1706 do {
1707 if (!(inp_flags & ALLOW_IMM))
1708 break;
1710 if (src2 & SLJIT_IMM) {
1711 src2_reg = (sljit_s32)get_imm((sljit_uw)src2w);
1712 if (src2_reg)
1713 break;
1714 if (inp_flags & ALLOW_INV_IMM) {
1715 src2_reg = (sljit_s32)get_imm(~(sljit_uw)src2w);
1716 if (src2_reg) {
1717 flags |= INV_IMM;
1718 break;
1721 if (GET_OPCODE(op) == SLJIT_ADD) {
1722 src2_reg = (sljit_s32)get_imm((sljit_uw)-src2w);
1723 if (src2_reg) {
1724 op = SLJIT_SUB | GET_ALL_FLAGS(op);
1725 break;
1728 if (GET_OPCODE(op) == SLJIT_SUB) {
1729 src2_reg = (sljit_s32)get_imm((sljit_uw)-src2w);
1730 if (src2_reg) {
1731 op = SLJIT_ADD | GET_ALL_FLAGS(op);
1732 break;
1737 if (src1 & SLJIT_IMM) {
1738 src2_reg = (sljit_s32)get_imm((sljit_uw)src1w);
1739 if (src2_reg) {
1740 flags |= ARGS_SWAPPED;
1741 src1 = src2;
1742 src1w = src2w;
1743 break;
1745 if (inp_flags & ALLOW_INV_IMM) {
1746 src2_reg = (sljit_s32)get_imm(~(sljit_uw)src1w);
1747 if (src2_reg) {
1748 flags |= ARGS_SWAPPED | INV_IMM;
1749 src1 = src2;
1750 src1w = src2w;
1751 break;
1754 if (GET_OPCODE(op) == SLJIT_ADD) {
1755 src2_reg = (sljit_s32)get_imm((sljit_uw)-src1w);
1756 if (src2_reg) {
1757 /* Note: add is commutative operation. */
1758 src1 = src2;
1759 src1w = src2w;
1760 op = SLJIT_SUB | GET_ALL_FLAGS(op);
1761 break;
1765 } while(0);
1767 /* Source 1. */
1768 if (FAST_IS_REG(src1))
1769 src1_reg = src1;
1770 else if (src1 & SLJIT_MEM) {
1771 FAIL_IF(emit_op_mem(compiler, inp_flags | LOAD_DATA, TMP_REG1, src1, src1w, TMP_REG1));
1772 src1_reg = TMP_REG1;
1774 else {
1775 FAIL_IF(load_immediate(compiler, TMP_REG1, (sljit_uw)src1w));
1776 src1_reg = TMP_REG1;
1779 /* Destination. */
1780 dst_reg = FAST_IS_REG(dst) ? dst : TMP_REG2;
1782 if (op <= SLJIT_MOV_P) {
1783 if (dst & SLJIT_MEM) {
1784 if (inp_flags & BYTE_SIZE)
1785 inp_flags &= ~SIGNED;
1787 if (FAST_IS_REG(src2))
1788 return emit_op_mem(compiler, inp_flags, src2, dst, dstw, TMP_REG2);
1791 if (FAST_IS_REG(src2) && dst_reg != TMP_REG2)
1792 flags |= MOVE_REG_CONV;
1795 /* Source 2. */
1796 if (src2_reg == 0) {
1797 src2_reg = (op <= SLJIT_MOV_P) ? dst_reg : TMP_REG2;
1799 if (FAST_IS_REG(src2))
1800 src2_reg = src2;
1801 else if (src2 & SLJIT_MEM)
1802 FAIL_IF(emit_op_mem(compiler, inp_flags | LOAD_DATA, src2_reg, src2, src2w, TMP_REG2));
1803 else
1804 FAIL_IF(load_immediate(compiler, src2_reg, (sljit_uw)src2w));
1807 FAIL_IF(emit_single_op(compiler, op, flags, (sljit_uw)dst_reg, (sljit_uw)src1_reg, (sljit_uw)src2_reg));
1809 if (!(dst & SLJIT_MEM))
1810 return SLJIT_SUCCESS;
1812 return emit_op_mem(compiler, inp_flags, dst_reg, dst, dstw, TMP_REG1);
1815 #ifdef __cplusplus
1816 extern "C" {
1817 #endif
1819 #if defined(__GNUC__)
1820 extern unsigned int __aeabi_uidivmod(unsigned int numerator, unsigned int denominator);
1821 extern int __aeabi_idivmod(int numerator, int denominator);
1822 #else
1823 #error "Software divmod functions are needed"
1824 #endif
1826 #ifdef __cplusplus
1828 #endif
1830 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op0(struct sljit_compiler *compiler, sljit_s32 op)
1832 sljit_uw saved_reg_list[3];
1833 sljit_sw saved_reg_count;
1835 CHECK_ERROR();
1836 CHECK(check_sljit_emit_op0(compiler, op));
1838 op = GET_OPCODE(op);
1839 switch (op) {
1840 case SLJIT_BREAKPOINT:
1841 FAIL_IF(push_inst(compiler, BKPT));
1842 break;
1843 case SLJIT_NOP:
1844 FAIL_IF(push_inst(compiler, NOP));
1845 break;
1846 case SLJIT_LMUL_UW:
1847 case SLJIT_LMUL_SW:
1848 return push_inst(compiler, (op == SLJIT_LMUL_UW ? UMULL : SMULL)
1849 | RN(SLJIT_R1) | RD(SLJIT_R0) | RM8(SLJIT_R0) | RM(SLJIT_R1));
1850 case SLJIT_DIVMOD_UW:
1851 case SLJIT_DIVMOD_SW:
1852 case SLJIT_DIV_UW:
1853 case SLJIT_DIV_SW:
1854 SLJIT_COMPILE_ASSERT((SLJIT_DIVMOD_UW & 0x2) == 0 && SLJIT_DIV_UW - 0x2 == SLJIT_DIVMOD_UW, bad_div_opcode_assignments);
1855 SLJIT_ASSERT(reg_map[2] == 1 && reg_map[3] == 2 && reg_map[4] == 3);
1857 saved_reg_count = 0;
1858 if (compiler->scratches >= 4)
1859 saved_reg_list[saved_reg_count++] = 3;
1860 if (compiler->scratches >= 3)
1861 saved_reg_list[saved_reg_count++] = 2;
1862 if (op >= SLJIT_DIV_UW)
1863 saved_reg_list[saved_reg_count++] = 1;
1865 if (saved_reg_count > 0) {
1866 FAIL_IF(push_inst(compiler, 0xe52d0000 | (saved_reg_count >= 3 ? 16 : 8)
1867 | (saved_reg_list[0] << 12) /* str rX, [sp, #-8/-16]! */));
1868 if (saved_reg_count >= 2) {
1869 SLJIT_ASSERT(saved_reg_list[1] < 8);
1870 FAIL_IF(push_inst(compiler, 0xe58d0004 | (saved_reg_list[1] << 12) /* str rX, [sp, #4] */));
1872 if (saved_reg_count >= 3) {
1873 SLJIT_ASSERT(saved_reg_list[2] < 8);
1874 FAIL_IF(push_inst(compiler, 0xe58d0008 | (saved_reg_list[2] << 12) /* str rX, [sp, #8] */));
1878 #if defined(__GNUC__)
1879 FAIL_IF(sljit_emit_ijump(compiler, SLJIT_FAST_CALL, SLJIT_IMM,
1880 ((op | 0x2) == SLJIT_DIV_UW ? SLJIT_FUNC_ADDR(__aeabi_uidivmod) : SLJIT_FUNC_ADDR(__aeabi_idivmod))));
1881 #else
1882 #error "Software divmod functions are needed"
1883 #endif
1885 if (saved_reg_count > 0) {
1886 if (saved_reg_count >= 3) {
1887 SLJIT_ASSERT(saved_reg_list[2] < 8);
1888 FAIL_IF(push_inst(compiler, 0xe59d0008 | (saved_reg_list[2] << 12) /* ldr rX, [sp, #8] */));
1890 if (saved_reg_count >= 2) {
1891 SLJIT_ASSERT(saved_reg_list[1] < 8);
1892 FAIL_IF(push_inst(compiler, 0xe59d0004 | (saved_reg_list[1] << 12) /* ldr rX, [sp, #4] */));
1894 return push_inst(compiler, 0xe49d0000 | (sljit_uw)(saved_reg_count >= 3 ? 16 : 8)
1895 | (saved_reg_list[0] << 12) /* ldr rX, [sp], #8/16 */);
1897 return SLJIT_SUCCESS;
1898 case SLJIT_ENDBR:
1899 case SLJIT_SKIP_FRAMES_BEFORE_RETURN:
1900 return SLJIT_SUCCESS;
1903 return SLJIT_SUCCESS;
1906 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op1(struct sljit_compiler *compiler, sljit_s32 op,
1907 sljit_s32 dst, sljit_sw dstw,
1908 sljit_s32 src, sljit_sw srcw)
1910 CHECK_ERROR();
1911 CHECK(check_sljit_emit_op1(compiler, op, dst, dstw, src, srcw));
1912 ADJUST_LOCAL_OFFSET(dst, dstw);
1913 ADJUST_LOCAL_OFFSET(src, srcw);
1915 switch (GET_OPCODE(op)) {
1916 case SLJIT_MOV:
1917 case SLJIT_MOV_U32:
1918 case SLJIT_MOV_S32:
1919 case SLJIT_MOV32:
1920 case SLJIT_MOV_P:
1921 return emit_op(compiler, SLJIT_MOV, ALLOW_ANY_IMM, dst, dstw, TMP_REG1, 0, src, srcw);
1923 case SLJIT_MOV_U8:
1924 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);
1926 case SLJIT_MOV_S8:
1927 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);
1929 case SLJIT_MOV_U16:
1930 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);
1932 case SLJIT_MOV_S16:
1933 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);
1935 case SLJIT_NOT:
1936 return emit_op(compiler, op, ALLOW_ANY_IMM, dst, dstw, TMP_REG1, 0, src, srcw);
1938 case SLJIT_NEG:
1939 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
1940 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
1941 compiler->skip_checks = 1;
1942 #endif
1943 return sljit_emit_op2(compiler, SLJIT_SUB | GET_ALL_FLAGS(op), dst, dstw, SLJIT_IMM, 0, src, srcw);
1945 case SLJIT_CLZ:
1946 return emit_op(compiler, op, 0, dst, dstw, TMP_REG1, 0, src, srcw);
1949 return SLJIT_SUCCESS;
1952 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op2(struct sljit_compiler *compiler, sljit_s32 op,
1953 sljit_s32 dst, sljit_sw dstw,
1954 sljit_s32 src1, sljit_sw src1w,
1955 sljit_s32 src2, sljit_sw src2w)
1957 CHECK_ERROR();
1958 CHECK(check_sljit_emit_op2(compiler, op, 0, dst, dstw, src1, src1w, src2, src2w));
1959 ADJUST_LOCAL_OFFSET(dst, dstw);
1960 ADJUST_LOCAL_OFFSET(src1, src1w);
1961 ADJUST_LOCAL_OFFSET(src2, src2w);
1963 switch (GET_OPCODE(op)) {
1964 case SLJIT_ADD:
1965 case SLJIT_ADDC:
1966 case SLJIT_SUB:
1967 case SLJIT_SUBC:
1968 case SLJIT_OR:
1969 case SLJIT_XOR:
1970 return emit_op(compiler, op, ALLOW_IMM, dst, dstw, src1, src1w, src2, src2w);
1972 case SLJIT_MUL:
1973 return emit_op(compiler, op, 0, dst, dstw, src1, src1w, src2, src2w);
1975 case SLJIT_AND:
1976 return emit_op(compiler, op, ALLOW_ANY_IMM, dst, dstw, src1, src1w, src2, src2w);
1978 case SLJIT_SHL:
1979 case SLJIT_LSHR:
1980 case SLJIT_ASHR:
1981 if (src2 & SLJIT_IMM) {
1982 compiler->shift_imm = src2w & 0x1f;
1983 return emit_op(compiler, op, 0, dst, dstw, TMP_REG1, 0, src1, src1w);
1985 else {
1986 compiler->shift_imm = 0x20;
1987 return emit_op(compiler, op, 0, dst, dstw, src1, src1w, src2, src2w);
1991 return SLJIT_SUCCESS;
1994 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op2u(struct sljit_compiler *compiler, sljit_s32 op,
1995 sljit_s32 src1, sljit_sw src1w,
1996 sljit_s32 src2, sljit_sw src2w)
1998 CHECK_ERROR();
1999 CHECK(check_sljit_emit_op2(compiler, op, 1, 0, 0, src1, src1w, src2, src2w));
2001 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2002 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2003 compiler->skip_checks = 1;
2004 #endif
2005 return sljit_emit_op2(compiler, op, TMP_REG2, 0, src1, src1w, src2, src2w);
2008 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_src(struct sljit_compiler *compiler, sljit_s32 op,
2009 sljit_s32 src, sljit_sw srcw)
2011 CHECK_ERROR();
2012 CHECK(check_sljit_emit_op_src(compiler, op, src, srcw));
2013 ADJUST_LOCAL_OFFSET(src, srcw);
2015 switch (op) {
2016 case SLJIT_FAST_RETURN:
2017 SLJIT_ASSERT(reg_map[TMP_REG2] == 14);
2019 if (FAST_IS_REG(src))
2020 FAIL_IF(push_inst(compiler, MOV | RD(TMP_REG2) | RM(src)));
2021 else
2022 FAIL_IF(emit_op_mem(compiler, WORD_SIZE | LOAD_DATA, TMP_REG2, src, srcw, TMP_REG1));
2024 return push_inst(compiler, BX | RM(TMP_REG2));
2025 case SLJIT_SKIP_FRAMES_BEFORE_FAST_RETURN:
2026 return SLJIT_SUCCESS;
2027 case SLJIT_PREFETCH_L1:
2028 case SLJIT_PREFETCH_L2:
2029 case SLJIT_PREFETCH_L3:
2030 case SLJIT_PREFETCH_ONCE:
2031 #if (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
2032 SLJIT_ASSERT(src & SLJIT_MEM);
2033 return emit_op_mem(compiler, PRELOAD | LOAD_DATA, TMP_PC, src, srcw, TMP_REG1);
2034 #else /* !SLJIT_CONFIG_ARM_V7 */
2035 return SLJIT_SUCCESS;
2036 #endif /* SLJIT_CONFIG_ARM_V7 */
2039 return SLJIT_SUCCESS;
2042 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_register_index(sljit_s32 reg)
2044 CHECK_REG_INDEX(check_sljit_get_register_index(reg));
2045 return reg_map[reg];
2048 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_float_register_index(sljit_s32 reg)
2050 CHECK_REG_INDEX(check_sljit_get_float_register_index(reg));
2051 return (freg_map[reg] << 1);
2054 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_custom(struct sljit_compiler *compiler,
2055 void *instruction, sljit_u32 size)
2057 CHECK_ERROR();
2058 CHECK(check_sljit_emit_op_custom(compiler, instruction, size));
2060 return push_inst(compiler, *(sljit_uw*)instruction);
2063 /* --------------------------------------------------------------------- */
2064 /* Floating point operators */
2065 /* --------------------------------------------------------------------- */
2067 #define FPU_LOAD (1 << 20)
2068 #define EMIT_FPU_DATA_TRANSFER(inst, add, base, freg, offs) \
2069 ((inst) | (sljit_uw)((add) << 23) | RN(base) | VD(freg) | (sljit_uw)(offs))
2071 static sljit_s32 emit_fop_mem(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg, sljit_s32 arg, sljit_sw argw)
2073 sljit_uw imm;
2074 sljit_uw inst = VSTR_F32 | (flags & (SLJIT_32 | FPU_LOAD));
2076 SLJIT_ASSERT(arg & SLJIT_MEM);
2077 arg &= ~SLJIT_MEM;
2079 if (SLJIT_UNLIKELY(arg & OFFS_REG_MASK)) {
2080 FAIL_IF(push_inst(compiler, ADD | RD(TMP_REG2) | RN(arg & REG_MASK) | RM(OFFS_REG(arg)) | (((sljit_uw)argw & 0x3) << 7)));
2081 arg = TMP_REG2;
2082 argw = 0;
2085 /* Fast loads and stores. */
2086 if (arg) {
2087 if (!(argw & ~0x3fc))
2088 return push_inst(compiler, EMIT_FPU_DATA_TRANSFER(inst, 1, arg & REG_MASK, reg, argw >> 2));
2089 if (!(-argw & ~0x3fc))
2090 return push_inst(compiler, EMIT_FPU_DATA_TRANSFER(inst, 0, arg & REG_MASK, reg, (-argw) >> 2));
2092 imm = get_imm((sljit_uw)argw & ~(sljit_uw)0x3fc);
2093 if (imm) {
2094 FAIL_IF(push_inst(compiler, ADD | RD(TMP_REG2) | RN(arg & REG_MASK) | imm));
2095 return push_inst(compiler, EMIT_FPU_DATA_TRANSFER(inst, 1, TMP_REG2, reg, (argw & 0x3fc) >> 2));
2097 imm = get_imm((sljit_uw)-argw & ~(sljit_uw)0x3fc);
2098 if (imm) {
2099 argw = -argw;
2100 FAIL_IF(push_inst(compiler, SUB | RD(TMP_REG2) | RN(arg & REG_MASK) | imm));
2101 return push_inst(compiler, EMIT_FPU_DATA_TRANSFER(inst, 0, TMP_REG2, reg, (argw & 0x3fc) >> 2));
2105 if (arg) {
2106 FAIL_IF(load_immediate(compiler, TMP_REG2, (sljit_uw)argw));
2107 FAIL_IF(push_inst(compiler, ADD | RD(TMP_REG2) | RN(arg & REG_MASK) | RM(TMP_REG2)));
2109 else
2110 FAIL_IF(load_immediate(compiler, TMP_REG2, (sljit_uw)argw));
2112 return push_inst(compiler, EMIT_FPU_DATA_TRANSFER(inst, 1, TMP_REG2, reg, 0));
2115 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_conv_sw_from_f64(struct sljit_compiler *compiler, sljit_s32 op,
2116 sljit_s32 dst, sljit_sw dstw,
2117 sljit_s32 src, sljit_sw srcw)
2119 op ^= SLJIT_32;
2121 if (src & SLJIT_MEM) {
2122 FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_32) | FPU_LOAD, TMP_FREG1, src, srcw));
2123 src = TMP_FREG1;
2126 FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VCVT_S32_F32, op & SLJIT_32, TMP_FREG1, src, 0)));
2128 if (FAST_IS_REG(dst))
2129 return push_inst(compiler, VMOV | (1 << 20) | RD(dst) | VN(TMP_FREG1));
2131 /* Store the integer value from a VFP register. */
2132 return emit_fop_mem(compiler, 0, TMP_FREG1, dst, dstw);
2135 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_conv_f64_from_sw(struct sljit_compiler *compiler, sljit_s32 op,
2136 sljit_s32 dst, sljit_sw dstw,
2137 sljit_s32 src, sljit_sw srcw)
2139 sljit_s32 dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
2141 op ^= SLJIT_32;
2143 if (FAST_IS_REG(src))
2144 FAIL_IF(push_inst(compiler, VMOV | RD(src) | VN(TMP_FREG1)));
2145 else if (src & SLJIT_MEM) {
2146 /* Load the integer value into a VFP register. */
2147 FAIL_IF(emit_fop_mem(compiler, FPU_LOAD, TMP_FREG1, src, srcw));
2149 else {
2150 FAIL_IF(load_immediate(compiler, TMP_REG1, (sljit_uw)srcw));
2151 FAIL_IF(push_inst(compiler, VMOV | RD(TMP_REG1) | VN(TMP_FREG1)));
2154 FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VCVT_F32_S32, op & SLJIT_32, dst_r, TMP_FREG1, 0)));
2156 if (dst & SLJIT_MEM)
2157 return emit_fop_mem(compiler, (op & SLJIT_32), TMP_FREG1, dst, dstw);
2158 return SLJIT_SUCCESS;
2161 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_cmp(struct sljit_compiler *compiler, sljit_s32 op,
2162 sljit_s32 src1, sljit_sw src1w,
2163 sljit_s32 src2, sljit_sw src2w)
2165 op ^= SLJIT_32;
2167 if (src1 & SLJIT_MEM) {
2168 FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_32) | FPU_LOAD, TMP_FREG1, src1, src1w));
2169 src1 = TMP_FREG1;
2172 if (src2 & SLJIT_MEM) {
2173 FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_32) | FPU_LOAD, TMP_FREG2, src2, src2w));
2174 src2 = TMP_FREG2;
2177 FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VCMP_F32, op & SLJIT_32, src1, src2, 0)));
2178 return push_inst(compiler, VMRS);
2181 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop1(struct sljit_compiler *compiler, sljit_s32 op,
2182 sljit_s32 dst, sljit_sw dstw,
2183 sljit_s32 src, sljit_sw srcw)
2185 sljit_s32 dst_r;
2187 CHECK_ERROR();
2189 SLJIT_COMPILE_ASSERT((SLJIT_32 == 0x100), float_transfer_bit_error);
2190 SELECT_FOP1_OPERATION_WITH_CHECKS(compiler, op, dst, dstw, src, srcw);
2192 dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
2194 if (GET_OPCODE(op) != SLJIT_CONV_F64_FROM_F32)
2195 op ^= SLJIT_32;
2197 if (src & SLJIT_MEM) {
2198 FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_32) | FPU_LOAD, dst_r, src, srcw));
2199 src = dst_r;
2202 switch (GET_OPCODE(op)) {
2203 case SLJIT_MOV_F64:
2204 if (src != dst_r) {
2205 if (dst_r != TMP_FREG1)
2206 FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VMOV_F32, op & SLJIT_32, dst_r, src, 0)));
2207 else
2208 dst_r = src;
2210 break;
2211 case SLJIT_NEG_F64:
2212 FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VNEG_F32, op & SLJIT_32, dst_r, src, 0)));
2213 break;
2214 case SLJIT_ABS_F64:
2215 FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VABS_F32, op & SLJIT_32, dst_r, src, 0)));
2216 break;
2217 case SLJIT_CONV_F64_FROM_F32:
2218 FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VCVT_F64_F32, op & SLJIT_32, dst_r, src, 0)));
2219 op ^= SLJIT_32;
2220 break;
2223 if (dst & SLJIT_MEM)
2224 return emit_fop_mem(compiler, (op & SLJIT_32), dst_r, dst, dstw);
2225 return SLJIT_SUCCESS;
2228 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop2(struct sljit_compiler *compiler, sljit_s32 op,
2229 sljit_s32 dst, sljit_sw dstw,
2230 sljit_s32 src1, sljit_sw src1w,
2231 sljit_s32 src2, sljit_sw src2w)
2233 sljit_s32 dst_r;
2235 CHECK_ERROR();
2236 CHECK(check_sljit_emit_fop2(compiler, op, dst, dstw, src1, src1w, src2, src2w));
2237 ADJUST_LOCAL_OFFSET(dst, dstw);
2238 ADJUST_LOCAL_OFFSET(src1, src1w);
2239 ADJUST_LOCAL_OFFSET(src2, src2w);
2241 op ^= SLJIT_32;
2243 dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
2245 if (src2 & SLJIT_MEM) {
2246 FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_32) | FPU_LOAD, TMP_FREG2, src2, src2w));
2247 src2 = TMP_FREG2;
2250 if (src1 & SLJIT_MEM) {
2251 FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_32) | FPU_LOAD, TMP_FREG1, src1, src1w));
2252 src1 = TMP_FREG1;
2255 switch (GET_OPCODE(op)) {
2256 case SLJIT_ADD_F64:
2257 FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VADD_F32, op & SLJIT_32, dst_r, src2, src1)));
2258 break;
2260 case SLJIT_SUB_F64:
2261 FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VSUB_F32, op & SLJIT_32, dst_r, src2, src1)));
2262 break;
2264 case SLJIT_MUL_F64:
2265 FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VMUL_F32, op & SLJIT_32, dst_r, src2, src1)));
2266 break;
2268 case SLJIT_DIV_F64:
2269 FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VDIV_F32, op & SLJIT_32, dst_r, src2, src1)));
2270 break;
2273 if (dst_r == TMP_FREG1)
2274 FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_32), TMP_FREG1, dst, dstw));
2276 return SLJIT_SUCCESS;
2279 #undef FPU_LOAD
2280 #undef EMIT_FPU_DATA_TRANSFER
2282 /* --------------------------------------------------------------------- */
2283 /* Other instructions */
2284 /* --------------------------------------------------------------------- */
2286 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fast_enter(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw)
2288 CHECK_ERROR();
2289 CHECK(check_sljit_emit_fast_enter(compiler, dst, dstw));
2290 ADJUST_LOCAL_OFFSET(dst, dstw);
2292 SLJIT_ASSERT(reg_map[TMP_REG2] == 14);
2294 if (FAST_IS_REG(dst))
2295 return push_inst(compiler, MOV | RD(dst) | RM(TMP_REG2));
2297 /* Memory. */
2298 return emit_op_mem(compiler, WORD_SIZE, TMP_REG2, dst, dstw, TMP_REG1);
2301 /* --------------------------------------------------------------------- */
2302 /* Conditional instructions */
2303 /* --------------------------------------------------------------------- */
2305 static sljit_uw get_cc(struct sljit_compiler *compiler, sljit_s32 type)
2307 switch (type) {
2308 case SLJIT_EQUAL:
2309 case SLJIT_EQUAL_F64:
2310 return 0x00000000;
2312 case SLJIT_NOT_EQUAL:
2313 case SLJIT_NOT_EQUAL_F64:
2314 return 0x10000000;
2316 case SLJIT_LESS:
2317 case SLJIT_LESS_F64:
2318 return 0x30000000;
2320 case SLJIT_GREATER_EQUAL:
2321 case SLJIT_GREATER_EQUAL_F64:
2322 return 0x20000000;
2324 case SLJIT_GREATER:
2325 case SLJIT_GREATER_F64:
2326 return 0x80000000;
2328 case SLJIT_LESS_EQUAL:
2329 case SLJIT_LESS_EQUAL_F64:
2330 return 0x90000000;
2332 case SLJIT_SIG_LESS:
2333 return 0xb0000000;
2335 case SLJIT_SIG_GREATER_EQUAL:
2336 return 0xa0000000;
2338 case SLJIT_SIG_GREATER:
2339 return 0xc0000000;
2341 case SLJIT_SIG_LESS_EQUAL:
2342 return 0xd0000000;
2344 case SLJIT_OVERFLOW:
2345 if (!(compiler->status_flags_state & SLJIT_CURRENT_FLAGS_ADD_SUB))
2346 return 0x10000000;
2348 case SLJIT_UNORDERED_F64:
2349 return 0x60000000;
2351 case SLJIT_NOT_OVERFLOW:
2352 if (!(compiler->status_flags_state & SLJIT_CURRENT_FLAGS_ADD_SUB))
2353 return 0x00000000;
2355 case SLJIT_ORDERED_F64:
2356 return 0x70000000;
2358 default:
2359 SLJIT_ASSERT(type >= SLJIT_JUMP && type <= SLJIT_CALL_CDECL);
2360 return 0xe0000000;
2364 SLJIT_API_FUNC_ATTRIBUTE struct sljit_label* sljit_emit_label(struct sljit_compiler *compiler)
2366 struct sljit_label *label;
2368 CHECK_ERROR_PTR();
2369 CHECK_PTR(check_sljit_emit_label(compiler));
2371 if (compiler->last_label && compiler->last_label->size == compiler->size)
2372 return compiler->last_label;
2374 label = (struct sljit_label*)ensure_abuf(compiler, sizeof(struct sljit_label));
2375 PTR_FAIL_IF(!label);
2376 set_label(label, compiler);
2377 return label;
2380 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_jump(struct sljit_compiler *compiler, sljit_s32 type)
2382 struct sljit_jump *jump;
2384 CHECK_ERROR_PTR();
2385 CHECK_PTR(check_sljit_emit_jump(compiler, type));
2387 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
2388 PTR_FAIL_IF(!jump);
2389 set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
2390 type &= 0xff;
2392 SLJIT_ASSERT(reg_map[TMP_REG1] != 14);
2394 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
2395 if (type >= SLJIT_FAST_CALL)
2396 PTR_FAIL_IF(prepare_blx(compiler));
2397 PTR_FAIL_IF(push_inst_with_unique_literal(compiler, ((EMIT_DATA_TRANSFER(WORD_SIZE | LOAD_DATA, 1,
2398 type <= SLJIT_JUMP ? TMP_PC : TMP_REG1, TMP_PC, 0)) & ~COND_MASK) | get_cc(compiler, type), 0));
2400 if (jump->flags & SLJIT_REWRITABLE_JUMP) {
2401 jump->addr = compiler->size;
2402 compiler->patches++;
2405 if (type >= SLJIT_FAST_CALL) {
2406 jump->flags |= IS_BL;
2407 PTR_FAIL_IF(emit_blx(compiler));
2410 if (!(jump->flags & SLJIT_REWRITABLE_JUMP))
2411 jump->addr = compiler->size;
2412 #else
2413 if (type >= SLJIT_FAST_CALL)
2414 jump->flags |= IS_BL;
2415 PTR_FAIL_IF(emit_imm(compiler, TMP_REG1, 0));
2416 PTR_FAIL_IF(push_inst(compiler, (((type <= SLJIT_JUMP ? BX : BLX) | RM(TMP_REG1)) & ~COND_MASK) | get_cc(compiler, type)));
2417 jump->addr = compiler->size;
2418 #endif
2419 return jump;
2422 #ifdef __SOFTFP__
2424 static sljit_s32 softfloat_call_with_args(struct sljit_compiler *compiler, sljit_s32 arg_types, sljit_s32 *src, sljit_u32 *extra_space)
2426 sljit_u32 is_tail_call = *extra_space & SLJIT_TAIL_CALL;
2427 sljit_u32 offset = 0;
2428 sljit_u32 word_arg_offset = 0;
2429 sljit_u32 src_offset = 4 * sizeof(sljit_sw);
2430 sljit_u32 float_arg_count = 0;
2431 sljit_s32 types = 0;
2432 sljit_u8 offsets[4];
2433 sljit_u8 *offset_ptr = offsets;
2435 if (src && FAST_IS_REG(*src))
2436 src_offset = (sljit_uw)reg_map[*src] * sizeof(sljit_sw);
2438 arg_types >>= SLJIT_ARG_SHIFT;
2440 while (arg_types) {
2441 types = (types << SLJIT_ARG_SHIFT) | (arg_types & SLJIT_ARG_MASK);
2443 switch (arg_types & SLJIT_ARG_MASK) {
2444 case SLJIT_ARG_TYPE_F64:
2445 if (offset & 0x7)
2446 offset += sizeof(sljit_sw);
2447 *offset_ptr++ = (sljit_u8)offset;
2448 offset += sizeof(sljit_f64);
2449 float_arg_count++;
2450 break;
2451 case SLJIT_ARG_TYPE_F32:
2452 *offset_ptr++ = (sljit_u8)offset;
2453 offset += sizeof(sljit_f32);
2454 float_arg_count++;
2455 break;
2456 default:
2457 *offset_ptr++ = (sljit_u8)offset;
2458 offset += sizeof(sljit_sw);
2459 word_arg_offset += sizeof(sljit_sw);
2460 break;
2463 arg_types >>= SLJIT_ARG_SHIFT;
2466 if (offset > 4 * sizeof(sljit_sw) && (!is_tail_call || offset > compiler->args_size)) {
2467 /* Keep lr register on the stack. */
2468 if (is_tail_call) {
2469 offset += sizeof(sljit_sw);
2470 FAIL_IF(emit_stack_frame_release(compiler, 0));
2473 offset = ((offset - 4 * sizeof(sljit_sw)) + 0x7) & ~(sljit_uw)0x7;
2475 *extra_space = offset;
2477 if (is_tail_call)
2478 offset -= sizeof(sljit_sw);
2480 FAIL_IF(push_inst(compiler, SUB | RD(SLJIT_SP) | RN(SLJIT_SP) | SRC2_IMM | offset));
2481 } else {
2482 if (is_tail_call)
2483 FAIL_IF(emit_stack_frame_release(compiler, TMP_REG2));
2484 *extra_space = 0;
2487 /* Process arguments in reversed direction. */
2488 while (types) {
2489 switch (types & SLJIT_ARG_MASK) {
2490 case SLJIT_ARG_TYPE_F64:
2491 float_arg_count--;
2492 offset = *(--offset_ptr);
2494 SLJIT_ASSERT((offset & 0x7) == 0);
2496 if (offset < 4 * sizeof(sljit_sw)) {
2497 if (src_offset == offset || src_offset == offset + sizeof(sljit_sw)) {
2498 FAIL_IF(push_inst(compiler, MOV | RD(TMP_REG1) | (src_offset >> 2)));
2499 *src = TMP_REG1;
2501 FAIL_IF(push_inst(compiler, VMOV2 | 0x100000 | (offset << 10) | ((offset + sizeof(sljit_sw)) << 14) | float_arg_count));
2502 } else
2503 FAIL_IF(push_inst(compiler, VSTR_F32 | 0x800100 | RN(SLJIT_SP)
2504 | (float_arg_count << 12) | ((offset - 4 * sizeof(sljit_sw)) >> 2)));
2505 break;
2506 case SLJIT_ARG_TYPE_F32:
2507 float_arg_count--;
2508 offset = *(--offset_ptr);
2510 if (offset < 4 * sizeof(sljit_sw)) {
2511 if (src_offset == offset) {
2512 FAIL_IF(push_inst(compiler, MOV | RD(TMP_REG1) | (src_offset >> 2)));
2513 *src = TMP_REG1;
2515 FAIL_IF(push_inst(compiler, VMOV | 0x100000 | (float_arg_count << 16) | (offset << 10)));
2516 } else
2517 FAIL_IF(push_inst(compiler, VSTR_F32 | 0x800000 | RN(SLJIT_SP)
2518 | (float_arg_count << 12) | ((offset - 4 * sizeof(sljit_sw)) >> 2)));
2519 break;
2520 default:
2521 word_arg_offset -= sizeof(sljit_sw);
2522 offset = *(--offset_ptr);
2524 SLJIT_ASSERT(offset >= word_arg_offset);
2526 if (offset != word_arg_offset) {
2527 if (offset < 4 * sizeof(sljit_sw)) {
2528 if (src_offset == offset) {
2529 FAIL_IF(push_inst(compiler, MOV | RD(TMP_REG1) | (src_offset >> 2)));
2530 *src = TMP_REG1;
2532 else if (src_offset == word_arg_offset) {
2533 *src = (sljit_s32)(SLJIT_R0 + (offset >> 2));
2534 src_offset = offset;
2536 FAIL_IF(push_inst(compiler, MOV | (offset << 10) | (word_arg_offset >> 2)));
2537 } else
2538 FAIL_IF(push_inst(compiler, data_transfer_insts[WORD_SIZE] | 0x800000 | RN(SLJIT_SP) | (word_arg_offset << 10) | (offset - 4 * sizeof(sljit_sw))));
2540 break;
2543 types >>= SLJIT_ARG_SHIFT;
2546 return SLJIT_SUCCESS;
2549 static sljit_s32 softfloat_post_call_with_args(struct sljit_compiler *compiler, sljit_s32 arg_types)
2551 if ((arg_types & SLJIT_ARG_MASK) == SLJIT_ARG_TYPE_F64)
2552 FAIL_IF(push_inst(compiler, VMOV2 | (1 << 16) | (0 << 12) | 0));
2553 if ((arg_types & SLJIT_ARG_MASK) == SLJIT_ARG_TYPE_F32)
2554 FAIL_IF(push_inst(compiler, VMOV | (0 << 16) | (0 << 12)));
2556 return SLJIT_SUCCESS;
2559 #else /* !__SOFTFP__ */
2561 static sljit_s32 hardfloat_call_with_args(struct sljit_compiler *compiler, sljit_s32 arg_types)
2563 sljit_u32 offset = SLJIT_FR0;
2564 sljit_u32 new_offset = SLJIT_FR0;
2565 sljit_u32 f32_offset = 0;
2567 /* Remove return value. */
2568 arg_types >>= SLJIT_ARG_SHIFT;
2570 while (arg_types) {
2571 switch (arg_types & SLJIT_ARG_MASK) {
2572 case SLJIT_ARG_TYPE_F64:
2573 if (offset != new_offset)
2574 FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VMOV_F32,
2575 SLJIT_32, new_offset, offset, 0)));
2577 new_offset++;
2578 offset++;
2579 break;
2580 case SLJIT_ARG_TYPE_F32:
2581 if (f32_offset != 0) {
2582 FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VMOV_F32,
2583 0x400000, f32_offset, offset, 0)));
2584 f32_offset = 0;
2585 } else {
2586 if (offset != new_offset)
2587 FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VMOV_F32,
2588 0, new_offset, offset, 0)));
2589 f32_offset = new_offset;
2590 new_offset++;
2592 offset++;
2593 break;
2595 arg_types >>= SLJIT_ARG_SHIFT;
2598 return SLJIT_SUCCESS;
2601 #endif /* __SOFTFP__ */
2603 #undef EMIT_FPU_OPERATION
2605 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_call(struct sljit_compiler *compiler, sljit_s32 type,
2606 sljit_s32 arg_types)
2608 #ifdef __SOFTFP__
2609 struct sljit_jump *jump;
2610 sljit_u32 extra_space = (sljit_u32)type;
2611 #endif
2613 CHECK_ERROR_PTR();
2614 CHECK_PTR(check_sljit_emit_call(compiler, type, arg_types));
2616 #ifdef __SOFTFP__
2617 PTR_FAIL_IF(softfloat_call_with_args(compiler, arg_types, NULL, &extra_space));
2618 SLJIT_ASSERT((extra_space & 0x7) == 0);
2620 if ((type & SLJIT_TAIL_CALL) && extra_space == 0)
2621 type = SLJIT_JUMP | (type & SLJIT_REWRITABLE_JUMP);
2623 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2624 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2625 compiler->skip_checks = 1;
2626 #endif
2628 jump = sljit_emit_jump(compiler, type);
2629 PTR_FAIL_IF(jump == NULL);
2631 if (extra_space > 0) {
2632 if (type & SLJIT_TAIL_CALL)
2633 PTR_FAIL_IF(push_inst(compiler, EMIT_DATA_TRANSFER(WORD_SIZE | LOAD_DATA, 1,
2634 TMP_REG2, SLJIT_SP, extra_space - sizeof(sljit_sw))));
2636 PTR_FAIL_IF(push_inst(compiler, ADD | RD(SLJIT_SP) | RN(SLJIT_SP) | SRC2_IMM | extra_space));
2638 if (type & SLJIT_TAIL_CALL) {
2639 PTR_FAIL_IF(push_inst(compiler, BX | RM(TMP_REG2)));
2640 return jump;
2644 SLJIT_ASSERT(!(type & SLJIT_TAIL_CALL));
2645 PTR_FAIL_IF(softfloat_post_call_with_args(compiler, arg_types));
2646 return jump;
2647 #else /* !__SOFTFP__ */
2648 if (type & SLJIT_TAIL_CALL) {
2649 PTR_FAIL_IF(emit_stack_frame_release(compiler, TMP_REG2));
2650 type = SLJIT_JUMP | (type & SLJIT_REWRITABLE_JUMP);
2653 PTR_FAIL_IF(hardfloat_call_with_args(compiler, arg_types));
2655 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2656 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2657 compiler->skip_checks = 1;
2658 #endif
2660 return sljit_emit_jump(compiler, type);
2661 #endif /* __SOFTFP__ */
2664 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_ijump(struct sljit_compiler *compiler, sljit_s32 type, sljit_s32 src, sljit_sw srcw)
2666 struct sljit_jump *jump;
2668 CHECK_ERROR();
2669 CHECK(check_sljit_emit_ijump(compiler, type, src, srcw));
2670 ADJUST_LOCAL_OFFSET(src, srcw);
2672 SLJIT_ASSERT(reg_map[TMP_REG1] != 14);
2674 if (!(src & SLJIT_IMM)) {
2675 if (FAST_IS_REG(src)) {
2676 SLJIT_ASSERT(reg_map[src] != 14);
2677 return push_inst(compiler, (type <= SLJIT_JUMP ? BX : BLX) | RM(src));
2680 SLJIT_ASSERT(src & SLJIT_MEM);
2681 FAIL_IF(emit_op_mem(compiler, WORD_SIZE | LOAD_DATA, TMP_REG1, src, srcw, TMP_REG1));
2682 return push_inst(compiler, (type <= SLJIT_JUMP ? BX : BLX) | RM(TMP_REG1));
2685 /* These jumps are converted to jump/call instructions when possible. */
2686 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
2687 FAIL_IF(!jump);
2688 set_jump(jump, compiler, JUMP_ADDR | ((type >= SLJIT_FAST_CALL) ? IS_BL : 0));
2689 jump->u.target = (sljit_uw)srcw;
2691 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
2692 if (type >= SLJIT_FAST_CALL)
2693 FAIL_IF(prepare_blx(compiler));
2694 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));
2695 if (type >= SLJIT_FAST_CALL)
2696 FAIL_IF(emit_blx(compiler));
2697 #else
2698 FAIL_IF(emit_imm(compiler, TMP_REG1, 0));
2699 FAIL_IF(push_inst(compiler, (type <= SLJIT_JUMP ? BX : BLX) | RM(TMP_REG1)));
2700 #endif
2701 jump->addr = compiler->size;
2702 return SLJIT_SUCCESS;
2705 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_icall(struct sljit_compiler *compiler, sljit_s32 type,
2706 sljit_s32 arg_types,
2707 sljit_s32 src, sljit_sw srcw)
2709 #ifdef __SOFTFP__
2710 sljit_u32 extra_space = (sljit_u32)type;
2711 #endif
2713 CHECK_ERROR();
2714 CHECK(check_sljit_emit_icall(compiler, type, arg_types, src, srcw));
2716 if (src & SLJIT_MEM) {
2717 FAIL_IF(emit_op_mem(compiler, WORD_SIZE | LOAD_DATA, TMP_REG1, src, srcw, TMP_REG1));
2718 src = TMP_REG1;
2721 if ((type & SLJIT_TAIL_CALL) && (src >= SLJIT_FIRST_SAVED_REG && src <= SLJIT_S0)) {
2722 FAIL_IF(push_inst(compiler, MOV | RD(TMP_REG1) | RM(src)));
2723 src = TMP_REG1;
2726 #ifdef __SOFTFP__
2727 FAIL_IF(softfloat_call_with_args(compiler, arg_types, &src, &extra_space));
2728 SLJIT_ASSERT((extra_space & 0x7) == 0);
2730 if ((type & SLJIT_TAIL_CALL) && extra_space == 0)
2731 type = SLJIT_JUMP;
2733 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2734 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2735 compiler->skip_checks = 1;
2736 #endif
2738 FAIL_IF(sljit_emit_ijump(compiler, type, src, srcw));
2740 if (extra_space > 0) {
2741 if (type & SLJIT_TAIL_CALL)
2742 FAIL_IF(push_inst(compiler, EMIT_DATA_TRANSFER(WORD_SIZE | LOAD_DATA, 1,
2743 TMP_REG2, SLJIT_SP, extra_space - sizeof(sljit_sw))));
2745 FAIL_IF(push_inst(compiler, ADD | RD(SLJIT_SP) | RN(SLJIT_SP) | SRC2_IMM | extra_space));
2747 if (type & SLJIT_TAIL_CALL)
2748 return push_inst(compiler, BX | RM(TMP_REG2));
2751 SLJIT_ASSERT(!(type & SLJIT_TAIL_CALL));
2752 return softfloat_post_call_with_args(compiler, arg_types);
2753 #else /* !__SOFTFP__ */
2754 if (type & SLJIT_TAIL_CALL) {
2755 FAIL_IF(emit_stack_frame_release(compiler, TMP_REG2));
2756 type = SLJIT_JUMP;
2759 FAIL_IF(hardfloat_call_with_args(compiler, arg_types));
2761 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2762 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2763 compiler->skip_checks = 1;
2764 #endif
2766 return sljit_emit_ijump(compiler, type, src, srcw);
2767 #endif /* __SOFTFP__ */
2770 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_flags(struct sljit_compiler *compiler, sljit_s32 op,
2771 sljit_s32 dst, sljit_sw dstw,
2772 sljit_s32 type)
2774 sljit_s32 dst_reg, flags = GET_ALL_FLAGS(op);
2775 sljit_uw cc, ins;
2777 CHECK_ERROR();
2778 CHECK(check_sljit_emit_op_flags(compiler, op, dst, dstw, type));
2779 ADJUST_LOCAL_OFFSET(dst, dstw);
2781 op = GET_OPCODE(op);
2782 cc = get_cc(compiler, type & 0xff);
2783 dst_reg = FAST_IS_REG(dst) ? dst : TMP_REG1;
2785 if (op < SLJIT_ADD) {
2786 FAIL_IF(push_inst(compiler, MOV | RD(dst_reg) | SRC2_IMM | 0));
2787 FAIL_IF(push_inst(compiler, ((MOV | RD(dst_reg) | SRC2_IMM | 1) & ~COND_MASK) | cc));
2788 if (dst & SLJIT_MEM)
2789 return emit_op_mem(compiler, WORD_SIZE, TMP_REG1, dst, dstw, TMP_REG2);
2790 return SLJIT_SUCCESS;
2793 ins = (op == SLJIT_AND ? AND : (op == SLJIT_OR ? ORR : EOR));
2795 if (dst & SLJIT_MEM)
2796 FAIL_IF(emit_op_mem(compiler, WORD_SIZE | LOAD_DATA, TMP_REG1, dst, dstw, TMP_REG2));
2798 FAIL_IF(push_inst(compiler, ((ins | RD(dst_reg) | RN(dst_reg) | SRC2_IMM | 1) & ~COND_MASK) | cc));
2800 if (op == SLJIT_AND)
2801 FAIL_IF(push_inst(compiler, ((ins | RD(dst_reg) | RN(dst_reg) | SRC2_IMM | 0) & ~COND_MASK) | (cc ^ 0x10000000)));
2803 if (dst & SLJIT_MEM)
2804 FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG1, dst, dstw, TMP_REG2));
2806 if (flags & SLJIT_SET_Z)
2807 return push_inst(compiler, MOV | SET_FLAGS | RD(TMP_REG2) | RM(dst_reg));
2808 return SLJIT_SUCCESS;
2811 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_cmov(struct sljit_compiler *compiler, sljit_s32 type,
2812 sljit_s32 dst_reg,
2813 sljit_s32 src, sljit_sw srcw)
2815 sljit_uw cc, tmp;
2817 CHECK_ERROR();
2818 CHECK(check_sljit_emit_cmov(compiler, type, dst_reg, src, srcw));
2820 dst_reg &= ~SLJIT_32;
2822 cc = get_cc(compiler, type & 0xff);
2824 if (SLJIT_UNLIKELY(src & SLJIT_IMM)) {
2825 tmp = get_imm((sljit_uw)srcw);
2826 if (tmp)
2827 return push_inst(compiler, ((MOV | RD(dst_reg) | tmp) & ~COND_MASK) | cc);
2829 tmp = get_imm(~(sljit_uw)srcw);
2830 if (tmp)
2831 return push_inst(compiler, ((MVN | RD(dst_reg) | tmp) & ~COND_MASK) | cc);
2833 #if (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
2834 tmp = (sljit_uw)srcw;
2835 FAIL_IF(push_inst(compiler, (MOVW & ~COND_MASK) | cc | RD(dst_reg) | ((tmp << 4) & 0xf0000) | (tmp & 0xfff)));
2836 if (tmp <= 0xffff)
2837 return SLJIT_SUCCESS;
2838 return push_inst(compiler, (MOVT & ~COND_MASK) | cc | RD(dst_reg) | ((tmp >> 12) & 0xf0000) | ((tmp >> 16) & 0xfff));
2839 #else
2840 FAIL_IF(load_immediate(compiler, TMP_REG1, (sljit_uw)srcw));
2841 src = TMP_REG1;
2842 #endif
2845 return push_inst(compiler, ((MOV | RD(dst_reg) | RM(src)) & ~COND_MASK) | cc);
2848 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_mem(struct sljit_compiler *compiler, sljit_s32 type,
2849 sljit_s32 reg,
2850 sljit_s32 mem, sljit_sw memw)
2852 sljit_s32 flags;
2853 sljit_uw is_type1_transfer, inst;
2855 CHECK_ERROR();
2856 CHECK(check_sljit_emit_mem(compiler, type, reg, mem, memw));
2858 is_type1_transfer = 1;
2860 switch (type & 0xff) {
2861 case SLJIT_MOV:
2862 case SLJIT_MOV_U32:
2863 case SLJIT_MOV_S32:
2864 case SLJIT_MOV32:
2865 case SLJIT_MOV_P:
2866 flags = WORD_SIZE;
2867 break;
2868 case SLJIT_MOV_U8:
2869 flags = BYTE_SIZE;
2870 break;
2871 case SLJIT_MOV_S8:
2872 if (!(type & SLJIT_MEM_STORE))
2873 is_type1_transfer = 0;
2874 flags = BYTE_SIZE | SIGNED;
2875 break;
2876 case SLJIT_MOV_U16:
2877 is_type1_transfer = 0;
2878 flags = HALF_SIZE;
2879 break;
2880 case SLJIT_MOV_S16:
2881 is_type1_transfer = 0;
2882 flags = HALF_SIZE | SIGNED;
2883 break;
2884 default:
2885 SLJIT_UNREACHABLE();
2886 flags = WORD_SIZE;
2887 break;
2890 if (!(type & SLJIT_MEM_STORE))
2891 flags |= LOAD_DATA;
2893 SLJIT_ASSERT(is_type1_transfer == !!IS_TYPE1_TRANSFER(flags));
2895 if (SLJIT_UNLIKELY(mem & OFFS_REG_MASK)) {
2896 if (!is_type1_transfer && memw != 0)
2897 return SLJIT_ERR_UNSUPPORTED;
2899 else {
2900 if (is_type1_transfer) {
2901 if (memw > 4095 || memw < -4095)
2902 return SLJIT_ERR_UNSUPPORTED;
2904 else {
2905 if (memw > 255 || memw < -255)
2906 return SLJIT_ERR_UNSUPPORTED;
2910 if (type & SLJIT_MEM_SUPP)
2911 return SLJIT_SUCCESS;
2913 if (SLJIT_UNLIKELY(mem & OFFS_REG_MASK)) {
2914 memw &= 0x3;
2916 inst = EMIT_DATA_TRANSFER(flags, 1, reg, mem & REG_MASK, RM(OFFS_REG(mem)) | ((sljit_uw)memw << 7));
2918 if (is_type1_transfer)
2919 inst |= (1 << 25);
2921 if (type & SLJIT_MEM_PRE)
2922 inst |= (1 << 21);
2923 else
2924 inst ^= (1 << 24);
2926 return push_inst(compiler, inst);
2929 inst = EMIT_DATA_TRANSFER(flags, 0, reg, mem & REG_MASK, 0);
2931 if (type & SLJIT_MEM_PRE)
2932 inst |= (1 << 21);
2933 else
2934 inst ^= (1 << 24);
2936 if (is_type1_transfer) {
2937 if (memw >= 0)
2938 inst |= (1 << 23);
2939 else
2940 memw = -memw;
2942 return push_inst(compiler, inst | (sljit_uw)memw);
2945 if (memw >= 0)
2946 inst |= (1 << 23);
2947 else
2948 memw = -memw;
2950 return push_inst(compiler, inst | TYPE2_TRANSFER_IMM((sljit_uw)memw));
2953 SLJIT_API_FUNC_ATTRIBUTE struct sljit_const* sljit_emit_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw, sljit_sw init_value)
2955 struct sljit_const *const_;
2956 sljit_s32 dst_r;
2958 CHECK_ERROR_PTR();
2959 CHECK_PTR(check_sljit_emit_const(compiler, dst, dstw, init_value));
2960 ADJUST_LOCAL_OFFSET(dst, dstw);
2962 dst_r = FAST_IS_REG(dst) ? dst : TMP_REG2;
2964 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
2965 PTR_FAIL_IF(push_inst_with_unique_literal(compiler,
2966 EMIT_DATA_TRANSFER(WORD_SIZE | LOAD_DATA, 1, dst_r, TMP_PC, 0), (sljit_uw)init_value));
2967 compiler->patches++;
2968 #else
2969 PTR_FAIL_IF(emit_imm(compiler, dst_r, init_value));
2970 #endif
2972 const_ = (struct sljit_const*)ensure_abuf(compiler, sizeof(struct sljit_const));
2973 PTR_FAIL_IF(!const_);
2974 set_const(const_, compiler);
2976 if (dst & SLJIT_MEM)
2977 PTR_FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG2, dst, dstw, TMP_REG1));
2978 return const_;
2981 SLJIT_API_FUNC_ATTRIBUTE struct sljit_put_label* sljit_emit_put_label(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw)
2983 struct sljit_put_label *put_label;
2984 sljit_s32 dst_r;
2986 CHECK_ERROR_PTR();
2987 CHECK_PTR(check_sljit_emit_put_label(compiler, dst, dstw));
2988 ADJUST_LOCAL_OFFSET(dst, dstw);
2990 dst_r = FAST_IS_REG(dst) ? dst : TMP_REG2;
2992 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
2993 PTR_FAIL_IF(push_inst_with_unique_literal(compiler, EMIT_DATA_TRANSFER(WORD_SIZE | LOAD_DATA, 1, dst_r, TMP_PC, 0), 0));
2994 compiler->patches++;
2995 #else
2996 PTR_FAIL_IF(emit_imm(compiler, dst_r, 0));
2997 #endif
2999 put_label = (struct sljit_put_label*)ensure_abuf(compiler, sizeof(struct sljit_put_label));
3000 PTR_FAIL_IF(!put_label);
3001 set_put_label(put_label, compiler, 0);
3003 if (dst & SLJIT_MEM)
3004 PTR_FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG2, dst, dstw, TMP_REG1));
3005 return put_label;
3008 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_target, sljit_sw executable_offset)
3010 inline_set_jump_addr(addr, executable_offset, new_target, 1);
3013 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_const(sljit_uw addr, sljit_sw new_constant, sljit_sw executable_offset)
3015 inline_set_const(addr, executable_offset, (sljit_uw)new_constant, 1);