2 * Stack-less Just-In-Time compiler
4 * Copyright 2009-2012 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 SLJIT_API_FUNC_ATTRIBUTE const char* sljit_get_platform_name(void)
29 #if (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
30 return "ARMv7" SLJIT_CPUINFO;
31 #elif (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
32 return "ARMv5" SLJIT_CPUINFO;
34 #error "Internal error: Unknown ARM architecture"
38 /* Last register + 1. */
39 #define TMP_REG1 (SLJIT_NUMBER_OF_REGISTERS + 2)
40 #define TMP_REG2 (SLJIT_NUMBER_OF_REGISTERS + 3)
41 #define TMP_REG3 (SLJIT_NUMBER_OF_REGISTERS + 4)
42 #define TMP_PC (SLJIT_NUMBER_OF_REGISTERS + 5)
45 #define TMP_FREG2 (SLJIT_NUMBER_OF_FLOAT_REGISTERS + 1)
47 /* In ARM instruction words.
48 Cache lines are usually 32 byte aligned. */
49 #define CONST_POOL_ALIGNMENT 8
50 #define CONST_POOL_EMPTY 0xffffffff
52 #define ALIGN_INSTRUCTION(ptr) \
53 (sljit_uw*)(((sljit_uw)(ptr) + (CONST_POOL_ALIGNMENT * sizeof(sljit_uw)) - 1) & ~((CONST_POOL_ALIGNMENT * sizeof(sljit_uw)) - 1))
54 #define MAX_DIFFERENCE(max_diff) \
55 (((max_diff) / (sljit_s32)sizeof(sljit_uw)) - (CONST_POOL_ALIGNMENT - 1))
57 /* See sljit_emit_enter and sljit_emit_op0 if you want to change them. */
58 static const sljit_u8 reg_map[SLJIT_NUMBER_OF_REGISTERS + 6] = {
59 0, 0, 1, 2, 11, 10, 9, 8, 7, 6, 5, 4, 13, 3, 12, 14, 15
62 #define RM(rm) (reg_map[rm])
63 #define RD(rd) (reg_map[rd] << 12)
64 #define RN(rn) (reg_map[rn] << 16)
66 /* --------------------------------------------------------------------- */
67 /* Instrucion forms */
68 /* --------------------------------------------------------------------- */
70 /* The instruction includes the AL condition.
71 INST_NAME - CONDITIONAL remove this flag. */
72 #define COND_MASK 0xf0000000
73 #define CONDITIONAL 0xe0000000
74 #define PUSH_POOL 0xff000000
76 /* DP - Data Processing instruction (use with EMIT_DATA_PROCESS_INS). */
83 #define BLX 0xe12fff30
85 #define CLZ 0xe16f0f10
87 #define BKPT 0xe1200070
90 #define MUL 0xe0000090
92 #define NOP 0xe1a00000
94 #define PUSH 0xe92d0000
95 #define POP 0xe8bd0000
99 #define SMULL 0xe0c00090
101 #define UMULL 0xe0800090
102 #define VABS_F32 0xeeb00ac0
103 #define VADD_F32 0xee300a00
104 #define VCMP_F32 0xeeb40a40
105 #define VCVT_F32_S32 0xeeb80ac0
106 #define VCVT_F64_F32 0xeeb70ac0
107 #define VCVT_S32_F32 0xeebd0ac0
108 #define VDIV_F32 0xee800a00
109 #define VMOV_F32 0xeeb00a40
110 #define VMOV 0xee000a10
111 #define VMRS 0xeef1fa10
112 #define VMUL_F32 0xee200a00
113 #define VNEG_F32 0xeeb10a40
114 #define VSTR_F32 0xed000a00
115 #define VSUB_F32 0xee300a40
117 #if (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
118 /* Arm v7 specific instructions. */
119 #define MOVW 0xe3000000
120 #define MOVT 0xe3400000
121 #define SXTB 0xe6af0070
122 #define SXTH 0xe6bf0070
123 #define UXTB 0xe6ef0070
124 #define UXTH 0xe6ff0070
127 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
129 static sljit_s32 push_cpool(struct sljit_compiler *compiler)
131 /* Pushing the constant pool into the instruction stream. */
137 /* The label could point the address after the constant pool. */
138 if (compiler->last_label && compiler->last_label->size == compiler->size)
139 compiler->last_label->size += compiler->cpool_fill + (CONST_POOL_ALIGNMENT - 1) + 1;
141 SLJIT_ASSERT(compiler->cpool_fill > 0 && compiler->cpool_fill <= CPOOL_SIZE);
142 inst = (sljit_uw*)ensure_buf(compiler, sizeof(sljit_uw));
145 *inst = 0xff000000 | compiler->cpool_fill;
147 for (i = 0; i < CONST_POOL_ALIGNMENT - 1; i++) {
148 inst = (sljit_uw*)ensure_buf(compiler, sizeof(sljit_uw));
154 cpool_ptr = compiler->cpool;
155 cpool_end = cpool_ptr + compiler->cpool_fill;
156 while (cpool_ptr < cpool_end) {
157 inst = (sljit_uw*)ensure_buf(compiler, sizeof(sljit_uw));
160 *inst = *cpool_ptr++;
162 compiler->cpool_diff = CONST_POOL_EMPTY;
163 compiler->cpool_fill = 0;
164 return SLJIT_SUCCESS;
167 static sljit_s32 push_inst(struct sljit_compiler *compiler, sljit_uw inst)
171 if (SLJIT_UNLIKELY(compiler->cpool_diff != CONST_POOL_EMPTY && compiler->size - compiler->cpool_diff >= MAX_DIFFERENCE(4092)))
172 FAIL_IF(push_cpool(compiler));
174 ptr = (sljit_uw*)ensure_buf(compiler, sizeof(sljit_uw));
178 return SLJIT_SUCCESS;
181 static sljit_s32 push_inst_with_literal(struct sljit_compiler *compiler, sljit_uw inst, sljit_uw literal)
184 sljit_uw cpool_index = CPOOL_SIZE;
187 sljit_u8* cpool_unique_ptr;
189 if (SLJIT_UNLIKELY(compiler->cpool_diff != CONST_POOL_EMPTY && compiler->size - compiler->cpool_diff >= MAX_DIFFERENCE(4092)))
190 FAIL_IF(push_cpool(compiler));
191 else if (compiler->cpool_fill > 0) {
192 cpool_ptr = compiler->cpool;
193 cpool_end = cpool_ptr + compiler->cpool_fill;
194 cpool_unique_ptr = compiler->cpool_unique;
196 if ((*cpool_ptr == literal) && !(*cpool_unique_ptr)) {
197 cpool_index = cpool_ptr - compiler->cpool;
202 } while (cpool_ptr < cpool_end);
205 if (cpool_index == CPOOL_SIZE) {
206 /* Must allocate a new entry in the literal pool. */
207 if (compiler->cpool_fill < CPOOL_SIZE) {
208 cpool_index = compiler->cpool_fill;
209 compiler->cpool_fill++;
212 FAIL_IF(push_cpool(compiler));
214 compiler->cpool_fill = 1;
218 SLJIT_ASSERT((inst & 0xfff) == 0);
219 ptr = (sljit_uw*)ensure_buf(compiler, sizeof(sljit_uw));
222 *ptr = inst | cpool_index;
224 compiler->cpool[cpool_index] = literal;
225 compiler->cpool_unique[cpool_index] = 0;
226 if (compiler->cpool_diff == CONST_POOL_EMPTY)
227 compiler->cpool_diff = compiler->size;
228 return SLJIT_SUCCESS;
231 static sljit_s32 push_inst_with_unique_literal(struct sljit_compiler *compiler, sljit_uw inst, sljit_uw literal)
234 if (SLJIT_UNLIKELY((compiler->cpool_diff != CONST_POOL_EMPTY && compiler->size - compiler->cpool_diff >= MAX_DIFFERENCE(4092)) || compiler->cpool_fill >= CPOOL_SIZE))
235 FAIL_IF(push_cpool(compiler));
237 SLJIT_ASSERT(compiler->cpool_fill < CPOOL_SIZE && (inst & 0xfff) == 0);
238 ptr = (sljit_uw*)ensure_buf(compiler, sizeof(sljit_uw));
241 *ptr = inst | compiler->cpool_fill;
243 compiler->cpool[compiler->cpool_fill] = literal;
244 compiler->cpool_unique[compiler->cpool_fill] = 1;
245 compiler->cpool_fill++;
246 if (compiler->cpool_diff == CONST_POOL_EMPTY)
247 compiler->cpool_diff = compiler->size;
248 return SLJIT_SUCCESS;
251 static SLJIT_INLINE sljit_s32 prepare_blx(struct sljit_compiler *compiler)
253 /* Place for at least two instruction (doesn't matter whether the first has a literal). */
254 if (SLJIT_UNLIKELY(compiler->cpool_diff != CONST_POOL_EMPTY && compiler->size - compiler->cpool_diff >= MAX_DIFFERENCE(4088)))
255 return push_cpool(compiler);
256 return SLJIT_SUCCESS;
259 static SLJIT_INLINE sljit_s32 emit_blx(struct sljit_compiler *compiler)
261 /* Must follow tightly the previous instruction (to be able to convert it to bl instruction). */
262 SLJIT_ASSERT(compiler->cpool_diff == CONST_POOL_EMPTY || compiler->size - compiler->cpool_diff < MAX_DIFFERENCE(4092));
263 return push_inst(compiler, BLX | RM(TMP_REG1));
266 static sljit_uw patch_pc_relative_loads(sljit_uw *last_pc_patch, sljit_uw *code_ptr, sljit_uw* const_pool, sljit_uw cpool_size)
270 sljit_uw counter = 0;
271 sljit_uw* clear_const_pool = const_pool;
272 sljit_uw* clear_const_pool_end = const_pool + cpool_size;
274 SLJIT_ASSERT(const_pool - code_ptr <= CONST_POOL_ALIGNMENT);
275 /* Set unused flag for all literals in the constant pool.
276 I.e.: unused literals can belong to branches, which can be encoded as B or BL.
277 We can "compress" the constant pool by discarding these literals. */
278 while (clear_const_pool < clear_const_pool_end)
279 *clear_const_pool++ = (sljit_uw)(-1);
281 while (last_pc_patch < code_ptr) {
282 /* Data transfer instruction with Rn == r15. */
283 if ((*last_pc_patch & 0x0c0f0000) == 0x040f0000) {
284 diff = const_pool - last_pc_patch;
285 ind = (*last_pc_patch) & 0xfff;
287 /* Must be a load instruction with immediate offset. */
288 SLJIT_ASSERT(ind < cpool_size && !(*last_pc_patch & (1 << 25)) && (*last_pc_patch & (1 << 20)));
289 if ((sljit_s32)const_pool[ind] < 0) {
290 const_pool[ind] = counter;
295 ind = const_pool[ind];
297 SLJIT_ASSERT(diff >= 1);
298 if (diff >= 2 || ind > 0) {
299 diff = (diff + ind - 2) << 2;
300 SLJIT_ASSERT(diff <= 0xfff);
301 *last_pc_patch = (*last_pc_patch & ~0xfff) | diff;
304 *last_pc_patch = (*last_pc_patch & ~(0xfff | (1 << 23))) | 0x004;
311 /* In some rare ocasions we may need future patches. The probability is close to 0 in practice. */
312 struct future_patch {
313 struct future_patch* next;
318 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)
321 struct future_patch *curr_patch, *prev_patch;
323 SLJIT_UNUSED_ARG(compiler);
325 /* Using the values generated by patch_pc_relative_loads. */
327 value = (sljit_s32)cpool_start_address[cpool_current_index];
329 curr_patch = *first_patch;
333 value = (sljit_s32)cpool_start_address[cpool_current_index];
336 if ((sljit_uw)curr_patch->index == cpool_current_index) {
337 value = curr_patch->value;
339 prev_patch->next = curr_patch->next;
341 *first_patch = curr_patch->next;
342 SLJIT_FREE(curr_patch, compiler->allocator_data);
345 prev_patch = curr_patch;
346 curr_patch = curr_patch->next;
351 if ((sljit_uw)value > cpool_current_index) {
352 curr_patch = (struct future_patch*)SLJIT_MALLOC(sizeof(struct future_patch), compiler->allocator_data);
354 while (*first_patch) {
355 curr_patch = *first_patch;
356 *first_patch = (*first_patch)->next;
357 SLJIT_FREE(curr_patch, compiler->allocator_data);
359 return SLJIT_ERR_ALLOC_FAILED;
361 curr_patch->next = *first_patch;
362 curr_patch->index = value;
363 curr_patch->value = cpool_start_address[value];
364 *first_patch = curr_patch;
366 cpool_start_address[value] = *buf_ptr;
368 return SLJIT_SUCCESS;
373 static sljit_s32 push_inst(struct sljit_compiler *compiler, sljit_uw inst)
377 ptr = (sljit_uw*)ensure_buf(compiler, sizeof(sljit_uw));
381 return SLJIT_SUCCESS;
384 static SLJIT_INLINE sljit_s32 emit_imm(struct sljit_compiler *compiler, sljit_s32 reg, sljit_sw imm)
386 FAIL_IF(push_inst(compiler, MOVW | RD(reg) | ((imm << 4) & 0xf0000) | (imm & 0xfff)));
387 return push_inst(compiler, MOVT | RD(reg) | ((imm >> 12) & 0xf0000) | ((imm >> 16) & 0xfff));
392 static SLJIT_INLINE sljit_s32 detect_jump_type(struct sljit_jump *jump, sljit_uw *code_ptr, sljit_uw *code)
396 if (jump->flags & SLJIT_REWRITABLE_JUMP)
399 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
400 if (jump->flags & IS_BL)
403 if (jump->flags & JUMP_ADDR)
404 diff = ((sljit_sw)jump->u.target - (sljit_sw)(code_ptr + 2));
406 SLJIT_ASSERT(jump->flags & JUMP_LABEL);
407 diff = ((sljit_sw)(code + jump->u.label->size) - (sljit_sw)(code_ptr + 2));
410 /* Branch to Thumb code has not been optimized yet. */
414 if (jump->flags & IS_BL) {
415 if (diff <= 0x01ffffff && diff >= -0x02000000) {
416 *code_ptr = (BL - CONDITIONAL) | (*(code_ptr + 1) & COND_MASK);
417 jump->flags |= PATCH_B;
422 if (diff <= 0x01ffffff && diff >= -0x02000000) {
423 *code_ptr = (B - CONDITIONAL) | (*code_ptr & COND_MASK);
424 jump->flags |= PATCH_B;
428 if (jump->flags & JUMP_ADDR)
429 diff = ((sljit_sw)jump->u.target - (sljit_sw)code_ptr);
431 SLJIT_ASSERT(jump->flags & JUMP_LABEL);
432 diff = ((sljit_sw)(code + jump->u.label->size) - (sljit_sw)code_ptr);
435 /* Branch to Thumb code has not been optimized yet. */
439 if (diff <= 0x01ffffff && diff >= -0x02000000) {
441 *code_ptr = ((jump->flags & IS_BL) ? (BL - CONDITIONAL) : (B - CONDITIONAL)) | (code_ptr[2] & COND_MASK);
442 jump->flags |= PATCH_B;
449 static SLJIT_INLINE void inline_set_jump_addr(sljit_uw addr, sljit_uw new_addr, sljit_s32 flush)
451 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
452 sljit_uw *ptr = (sljit_uw*)addr;
453 sljit_uw *inst = (sljit_uw*)ptr[0];
454 sljit_uw mov_pc = ptr[1];
455 sljit_s32 bl = (mov_pc & 0x0000f000) != RD(TMP_PC);
456 sljit_sw diff = (sljit_sw)(((sljit_sw)new_addr - (sljit_sw)(inst + 2)) >> 2);
458 if (diff <= 0x7fffff && diff >= -0x800000) {
459 /* Turn to branch. */
461 inst[0] = (mov_pc & COND_MASK) | (B - CONDITIONAL) | (diff & 0xffffff);
463 SLJIT_CACHE_FLUSH(inst, inst + 1);
466 inst[0] = (mov_pc & COND_MASK) | (BL - CONDITIONAL) | (diff & 0xffffff);
469 SLJIT_CACHE_FLUSH(inst, inst + 2);
473 /* Get the position of the constant. */
474 if (mov_pc & (1 << 23))
475 ptr = inst + ((mov_pc & 0xfff) >> 2) + 2;
479 if (*inst != mov_pc) {
483 SLJIT_CACHE_FLUSH(inst, inst + 1);
486 inst[1] = BLX | RM(TMP_REG1);
488 SLJIT_CACHE_FLUSH(inst, inst + 2);
495 sljit_uw *inst = (sljit_uw*)addr;
496 SLJIT_ASSERT((inst[0] & 0xfff00000) == MOVW && (inst[1] & 0xfff00000) == MOVT);
497 inst[0] = MOVW | (inst[0] & 0xf000) | ((new_addr << 4) & 0xf0000) | (new_addr & 0xfff);
498 inst[1] = MOVT | (inst[1] & 0xf000) | ((new_addr >> 12) & 0xf0000) | ((new_addr >> 16) & 0xfff);
500 SLJIT_CACHE_FLUSH(inst, inst + 2);
505 static sljit_uw get_imm(sljit_uw imm);
507 static SLJIT_INLINE void inline_set_const(sljit_uw addr, sljit_sw new_constant, sljit_s32 flush)
509 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
510 sljit_uw *ptr = (sljit_uw*)addr;
511 sljit_uw *inst = (sljit_uw*)ptr[0];
512 sljit_uw ldr_literal = ptr[1];
515 src2 = get_imm(new_constant);
517 *inst = 0xe3a00000 | (ldr_literal & 0xf000) | src2;
519 SLJIT_CACHE_FLUSH(inst, inst + 1);
524 src2 = get_imm(~new_constant);
526 *inst = 0xe3e00000 | (ldr_literal & 0xf000) | src2;
528 SLJIT_CACHE_FLUSH(inst, inst + 1);
533 if (ldr_literal & (1 << 23))
534 ptr = inst + ((ldr_literal & 0xfff) >> 2) + 2;
538 if (*inst != ldr_literal) {
541 SLJIT_CACHE_FLUSH(inst, inst + 1);
546 sljit_uw *inst = (sljit_uw*)addr;
547 SLJIT_ASSERT((inst[0] & 0xfff00000) == MOVW && (inst[1] & 0xfff00000) == MOVT);
548 inst[0] = MOVW | (inst[0] & 0xf000) | ((new_constant << 4) & 0xf0000) | (new_constant & 0xfff);
549 inst[1] = MOVT | (inst[1] & 0xf000) | ((new_constant >> 12) & 0xf0000) | ((new_constant >> 16) & 0xfff);
551 SLJIT_CACHE_FLUSH(inst, inst + 2);
556 SLJIT_API_FUNC_ATTRIBUTE void* sljit_generate_code(struct sljit_compiler *compiler)
558 struct sljit_memory_fragment *buf;
565 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
567 sljit_uw cpool_skip_alignment;
568 sljit_uw cpool_current_index;
569 sljit_uw *cpool_start_address;
570 sljit_uw *last_pc_patch;
571 struct future_patch *first_patch;
574 struct sljit_label *label;
575 struct sljit_jump *jump;
576 struct sljit_const *const_;
579 CHECK_PTR(check_sljit_generate_code(compiler));
580 reverse_buf(compiler);
582 /* Second code generation pass. */
583 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
584 size = compiler->size + (compiler->patches << 1);
585 if (compiler->cpool_fill > 0)
586 size += compiler->cpool_fill + CONST_POOL_ALIGNMENT - 1;
588 size = compiler->size;
590 code = (sljit_uw*)SLJIT_MALLOC_EXEC(size * sizeof(sljit_uw));
591 PTR_FAIL_WITH_EXEC_IF(code);
594 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
596 cpool_skip_alignment = 0;
597 cpool_current_index = 0;
598 cpool_start_address = NULL;
600 last_pc_patch = code;
606 label = compiler->labels;
607 jump = compiler->jumps;
608 const_ = compiler->consts;
610 if (label && label->size == 0) {
611 label->addr = (sljit_uw)code;
617 buf_ptr = (sljit_uw*)buf->memory;
618 buf_end = buf_ptr + (buf->used_size >> 2);
621 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
622 if (cpool_size > 0) {
623 if (cpool_skip_alignment > 0) {
625 cpool_skip_alignment--;
628 if (SLJIT_UNLIKELY(resolve_const_pool_index(compiler, &first_patch, cpool_current_index, cpool_start_address, buf_ptr))) {
629 SLJIT_FREE_EXEC(code);
630 compiler->error = SLJIT_ERR_ALLOC_FAILED;
634 if (++cpool_current_index >= cpool_size) {
635 SLJIT_ASSERT(!first_patch);
637 if (label && label->size == word_count) {
638 /* Points after the current instruction. */
639 label->addr = (sljit_uw)code_ptr;
640 label->size = code_ptr - code;
646 else if ((*buf_ptr & 0xff000000) != PUSH_POOL) {
648 *code_ptr = *buf_ptr++;
649 /* These structures are ordered by their address. */
650 SLJIT_ASSERT(!label || label->size >= word_count);
651 SLJIT_ASSERT(!jump || jump->addr >= word_count);
652 SLJIT_ASSERT(!const_ || const_->addr >= word_count);
653 if (jump && jump->addr == word_count) {
654 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
655 if (detect_jump_type(jump, code_ptr, code))
657 jump->addr = (sljit_uw)code_ptr;
659 jump->addr = (sljit_uw)(code_ptr - 2);
660 if (detect_jump_type(jump, code_ptr, code))
665 if (label && label->size == word_count) {
666 /* code_ptr can be affected above. */
667 label->addr = (sljit_uw)(code_ptr + 1);
668 label->size = (code_ptr + 1) - code;
671 if (const_ && const_->addr == word_count) {
672 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
673 const_->addr = (sljit_uw)code_ptr;
675 const_->addr = (sljit_uw)(code_ptr - 1);
677 const_ = const_->next;
680 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
683 /* Fortunately, no need to shift. */
684 cpool_size = *buf_ptr++ & ~PUSH_POOL;
685 SLJIT_ASSERT(cpool_size > 0);
686 cpool_start_address = ALIGN_INSTRUCTION(code_ptr + 1);
687 cpool_current_index = patch_pc_relative_loads(last_pc_patch, code_ptr, cpool_start_address, cpool_size);
688 if (cpool_current_index > 0) {
689 /* Unconditional branch. */
690 *code_ptr = B | (((cpool_start_address - code_ptr) + cpool_current_index - 2) & ~PUSH_POOL);
691 code_ptr = cpool_start_address + cpool_current_index;
693 cpool_skip_alignment = CONST_POOL_ALIGNMENT - 1;
694 cpool_current_index = 0;
695 last_pc_patch = code_ptr;
698 } while (buf_ptr < buf_end);
702 SLJIT_ASSERT(!label);
704 SLJIT_ASSERT(!const_);
706 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
707 SLJIT_ASSERT(cpool_size == 0);
708 if (compiler->cpool_fill > 0) {
709 cpool_start_address = ALIGN_INSTRUCTION(code_ptr);
710 cpool_current_index = patch_pc_relative_loads(last_pc_patch, code_ptr, cpool_start_address, compiler->cpool_fill);
711 if (cpool_current_index > 0)
712 code_ptr = cpool_start_address + cpool_current_index;
714 buf_ptr = compiler->cpool;
715 buf_end = buf_ptr + compiler->cpool_fill;
716 cpool_current_index = 0;
717 while (buf_ptr < buf_end) {
718 if (SLJIT_UNLIKELY(resolve_const_pool_index(compiler, &first_patch, cpool_current_index, cpool_start_address, buf_ptr))) {
719 SLJIT_FREE_EXEC(code);
720 compiler->error = SLJIT_ERR_ALLOC_FAILED;
724 cpool_current_index++;
726 SLJIT_ASSERT(!first_patch);
730 jump = compiler->jumps;
732 buf_ptr = (sljit_uw*)jump->addr;
734 if (jump->flags & PATCH_B) {
735 if (!(jump->flags & JUMP_ADDR)) {
736 SLJIT_ASSERT(jump->flags & JUMP_LABEL);
737 SLJIT_ASSERT(((sljit_sw)jump->u.label->addr - (sljit_sw)(buf_ptr + 2)) <= 0x01ffffff && ((sljit_sw)jump->u.label->addr - (sljit_sw)(buf_ptr + 2)) >= -0x02000000);
738 *buf_ptr |= (((sljit_sw)jump->u.label->addr - (sljit_sw)(buf_ptr + 2)) >> 2) & 0x00ffffff;
741 SLJIT_ASSERT(((sljit_sw)jump->u.target - (sljit_sw)(buf_ptr + 2)) <= 0x01ffffff && ((sljit_sw)jump->u.target - (sljit_sw)(buf_ptr + 2)) >= -0x02000000);
742 *buf_ptr |= (((sljit_sw)jump->u.target - (sljit_sw)(buf_ptr + 2)) >> 2) & 0x00ffffff;
745 else if (jump->flags & SLJIT_REWRITABLE_JUMP) {
746 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
747 jump->addr = (sljit_uw)code_ptr;
748 code_ptr[0] = (sljit_uw)buf_ptr;
749 code_ptr[1] = *buf_ptr;
750 inline_set_jump_addr((sljit_uw)code_ptr, (jump->flags & JUMP_LABEL) ? jump->u.label->addr : jump->u.target, 0);
753 inline_set_jump_addr((sljit_uw)buf_ptr, (jump->flags & JUMP_LABEL) ? jump->u.label->addr : jump->u.target, 0);
757 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
758 if (jump->flags & IS_BL)
760 if (*buf_ptr & (1 << 23))
761 buf_ptr += ((*buf_ptr & 0xfff) >> 2) + 2;
764 *buf_ptr = (jump->flags & JUMP_LABEL) ? jump->u.label->addr : jump->u.target;
766 inline_set_jump_addr((sljit_uw)buf_ptr, (jump->flags & JUMP_LABEL) ? jump->u.label->addr : jump->u.target, 0);
772 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
773 const_ = compiler->consts;
775 buf_ptr = (sljit_uw*)const_->addr;
776 const_->addr = (sljit_uw)code_ptr;
778 code_ptr[0] = (sljit_uw)buf_ptr;
779 code_ptr[1] = *buf_ptr;
780 if (*buf_ptr & (1 << 23))
781 buf_ptr += ((*buf_ptr & 0xfff) >> 2) + 2;
784 /* Set the value again (can be a simple constant). */
785 inline_set_const((sljit_uw)code_ptr, *buf_ptr, 0);
788 const_ = const_->next;
792 SLJIT_ASSERT(code_ptr - code <= (sljit_s32)size);
794 compiler->error = SLJIT_ERR_COMPILED;
795 compiler->executable_size = (code_ptr - code) * sizeof(sljit_uw);
796 SLJIT_CACHE_FLUSH(code, code_ptr);
800 /* --------------------------------------------------------------------- */
802 /* --------------------------------------------------------------------- */
804 /* emit_op inp_flags.
805 WRITE_BACK must be the first, since it is a flag. */
806 #define WRITE_BACK 0x01
807 #define ALLOW_IMM 0x02
808 #define ALLOW_INV_IMM 0x04
809 #define ALLOW_ANY_IMM (ALLOW_IMM | ALLOW_INV_IMM)
810 #define ARG_TEST 0x08
812 /* Creates an index in data_transfer_insts array. */
813 #define WORD_DATA 0x00
814 #define BYTE_DATA 0x10
815 #define HALF_DATA 0x20
816 #define SIGNED_DATA 0x40
817 #define LOAD_DATA 0x80
820 #define EMIT_DATA_PROCESS_INS(opcode, set_flags, dst, src1, src2) \
821 (0xe0000000 | ((opcode) << 21) | (set_flags) | RD(dst) | RN(src1) | (src2))
823 static sljit_s32 emit_op(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 inp_flags,
824 sljit_s32 dst, sljit_sw dstw,
825 sljit_s32 src1, sljit_sw src1w,
826 sljit_s32 src2, sljit_sw src2w);
828 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_enter(struct sljit_compiler *compiler,
829 sljit_s32 options, sljit_s32 args, sljit_s32 scratches, sljit_s32 saveds,
830 sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
832 sljit_s32 size, i, tmp;
836 CHECK(check_sljit_emit_enter(compiler, options, args, scratches, saveds, fscratches, fsaveds, local_size));
837 set_emit_enter(compiler, options, args, scratches, saveds, fscratches, fsaveds, local_size);
839 /* Push saved registers, temporary registers
840 stmdb sp!, {..., lr} */
841 push = PUSH | (1 << 14);
843 tmp = saveds < SLJIT_NUMBER_OF_SAVED_REGISTERS ? (SLJIT_S0 + 1 - saveds) : SLJIT_FIRST_SAVED_REG;
844 for (i = SLJIT_S0; i >= tmp; i--)
845 push |= 1 << reg_map[i];
847 for (i = scratches; i >= SLJIT_FIRST_SAVED_REG; i--)
848 push |= 1 << reg_map[i];
850 FAIL_IF(push_inst(compiler, push));
852 /* Stack must be aligned to 8 bytes: */
853 size = GET_SAVED_REGISTERS_SIZE(scratches, saveds, 1);
854 local_size = ((size + local_size + 7) & ~7) - size;
855 compiler->local_size = local_size;
857 FAIL_IF(emit_op(compiler, SLJIT_SUB, ALLOW_IMM, SLJIT_SP, 0, SLJIT_SP, 0, SLJIT_IMM, local_size));
860 FAIL_IF(push_inst(compiler, EMIT_DATA_PROCESS_INS(MOV_DP, 0, SLJIT_S0, SLJIT_UNUSED, RM(SLJIT_R0))));
862 FAIL_IF(push_inst(compiler, EMIT_DATA_PROCESS_INS(MOV_DP, 0, SLJIT_S1, SLJIT_UNUSED, RM(SLJIT_R1))));
864 FAIL_IF(push_inst(compiler, EMIT_DATA_PROCESS_INS(MOV_DP, 0, SLJIT_S2, SLJIT_UNUSED, RM(SLJIT_R2))));
866 return SLJIT_SUCCESS;
869 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_set_context(struct sljit_compiler *compiler,
870 sljit_s32 options, sljit_s32 args, sljit_s32 scratches, sljit_s32 saveds,
871 sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
876 CHECK(check_sljit_set_context(compiler, options, args, scratches, saveds, fscratches, fsaveds, local_size));
877 set_set_context(compiler, options, args, scratches, saveds, fscratches, fsaveds, local_size);
879 size = GET_SAVED_REGISTERS_SIZE(scratches, saveds, 1);
880 compiler->local_size = ((size + local_size + 7) & ~7) - size;
881 return SLJIT_SUCCESS;
884 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_return(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 src, sljit_sw srcw)
890 CHECK(check_sljit_emit_return(compiler, op, src, srcw));
892 FAIL_IF(emit_mov_before_return(compiler, op, src, srcw));
894 if (compiler->local_size > 0)
895 FAIL_IF(emit_op(compiler, SLJIT_ADD, ALLOW_IMM, SLJIT_SP, 0, SLJIT_SP, 0, SLJIT_IMM, compiler->local_size));
897 /* Push saved registers, temporary registers
898 ldmia sp!, {..., pc} */
899 pop = POP | (1 << 15);
901 tmp = compiler->saveds < SLJIT_NUMBER_OF_SAVED_REGISTERS ? (SLJIT_S0 + 1 - compiler->saveds) : SLJIT_FIRST_SAVED_REG;
902 for (i = SLJIT_S0; i >= tmp; i--)
903 pop |= 1 << reg_map[i];
905 for (i = compiler->scratches; i >= SLJIT_FIRST_SAVED_REG; i--)
906 pop |= 1 << reg_map[i];
908 return push_inst(compiler, pop);
911 /* --------------------------------------------------------------------- */
913 /* --------------------------------------------------------------------- */
915 /* s/l - store/load (1 bit)
916 u/s - signed/unsigned (1 bit)
917 w/b/h/N - word/byte/half/NOT allowed (2 bit)
918 It contans 16 items, but not all are different. */
920 static sljit_sw data_transfer_insts[16] = {
921 /* s u w */ 0xe5000000 /* str */,
922 /* s u b */ 0xe5400000 /* strb */,
923 /* s u h */ 0xe10000b0 /* strh */,
924 /* s u N */ 0x00000000 /* not allowed */,
925 /* s s w */ 0xe5000000 /* str */,
926 /* s s b */ 0xe5400000 /* strb */,
927 /* s s h */ 0xe10000b0 /* strh */,
928 /* s s N */ 0x00000000 /* not allowed */,
930 /* l u w */ 0xe5100000 /* ldr */,
931 /* l u b */ 0xe5500000 /* ldrb */,
932 /* l u h */ 0xe11000b0 /* ldrh */,
933 /* l u N */ 0x00000000 /* not allowed */,
934 /* l s w */ 0xe5100000 /* ldr */,
935 /* l s b */ 0xe11000d0 /* ldrsb */,
936 /* l s h */ 0xe11000f0 /* ldrsh */,
937 /* l s N */ 0x00000000 /* not allowed */,
940 #define EMIT_DATA_TRANSFER(type, add, wb, target, base1, base2) \
941 (data_transfer_insts[(type) >> 4] | ((add) << 23) | ((wb) << 21) | (reg_map[target] << 12) | (reg_map[base1] << 16) | (base2))
942 /* Normal ldr/str instruction.
943 Type2: ldrsb, ldrh, ldrsh */
944 #define IS_TYPE1_TRANSFER(type) \
945 (data_transfer_insts[(type) >> 4] & 0x04000000)
946 #define TYPE2_TRANSFER_IMM(imm) \
947 (((imm) & 0xf) | (((imm) & 0xf0) << 4) | (1 << 22))
950 /* Arguments are swapped. */
951 #define ARGS_SWAPPED 0x01
952 /* Inverted immediate. */
954 /* Source and destination is register. */
955 #define REG_DEST 0x04
956 #define REG_SOURCE 0x08
957 /* One instruction is enough. */
958 #define FAST_DEST 0x10
959 /* Multiple instructions are required. */
960 #define SLOW_DEST 0x20
961 /* SET_FLAGS must be (1 << 20) as it is also the value of S bit (can be used for optimization). */
962 #define SET_FLAGS (1 << 20)
965 src2: reg or imm (if allowed)
966 SRC2_IMM must be (1 << 25) as it is also the value of I bit (can be used for optimization). */
967 #define SRC2_IMM (1 << 25)
969 #define EMIT_DATA_PROCESS_INS_AND_RETURN(opcode) \
970 return push_inst(compiler, EMIT_DATA_PROCESS_INS(opcode, flags & SET_FLAGS, dst, src1, (src2 & SRC2_IMM) ? src2 : RM(src2)))
972 #define EMIT_FULL_DATA_PROCESS_INS_AND_RETURN(opcode, dst, src1, src2) \
973 return push_inst(compiler, EMIT_DATA_PROCESS_INS(opcode, flags & SET_FLAGS, dst, src1, src2))
975 #define EMIT_SHIFT_INS_AND_RETURN(opcode) \
976 SLJIT_ASSERT(!(flags & INV_IMM) && !(src2 & SRC2_IMM)); \
977 if (compiler->shift_imm != 0x20) { \
978 SLJIT_ASSERT(src1 == TMP_REG1); \
979 SLJIT_ASSERT(!(flags & ARGS_SWAPPED)); \
980 if (compiler->shift_imm != 0) \
981 return push_inst(compiler, EMIT_DATA_PROCESS_INS(MOV_DP, flags & SET_FLAGS, dst, SLJIT_UNUSED, (compiler->shift_imm << 7) | (opcode << 5) | reg_map[src2])); \
982 return push_inst(compiler, EMIT_DATA_PROCESS_INS(MOV_DP, flags & SET_FLAGS, dst, SLJIT_UNUSED, reg_map[src2])); \
984 return push_inst(compiler, EMIT_DATA_PROCESS_INS(MOV_DP, flags & SET_FLAGS, dst, SLJIT_UNUSED, (reg_map[(flags & ARGS_SWAPPED) ? src1 : src2] << 8) | (opcode << 5) | 0x10 | ((flags & ARGS_SWAPPED) ? reg_map[src2] : reg_map[src1])));
986 static SLJIT_INLINE sljit_s32 emit_single_op(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 flags,
987 sljit_s32 dst, sljit_s32 src1, sljit_s32 src2)
991 switch (GET_OPCODE(op)) {
993 SLJIT_ASSERT(src1 == TMP_REG1 && !(flags & ARGS_SWAPPED));
995 if (src2 & SRC2_IMM) {
997 EMIT_FULL_DATA_PROCESS_INS_AND_RETURN(MVN_DP, dst, SLJIT_UNUSED, src2);
998 EMIT_FULL_DATA_PROCESS_INS_AND_RETURN(MOV_DP, dst, SLJIT_UNUSED, src2);
1000 EMIT_FULL_DATA_PROCESS_INS_AND_RETURN(MOV_DP, dst, SLJIT_UNUSED, reg_map[src2]);
1002 return SLJIT_SUCCESS;
1006 SLJIT_ASSERT(src1 == TMP_REG1 && !(flags & ARGS_SWAPPED));
1007 if ((flags & (REG_DEST | REG_SOURCE)) == (REG_DEST | REG_SOURCE)) {
1008 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
1009 if (op == SLJIT_MOV_U8)
1010 return push_inst(compiler, EMIT_DATA_PROCESS_INS(AND_DP, 0, dst, src2, SRC2_IMM | 0xff));
1011 FAIL_IF(push_inst(compiler, EMIT_DATA_PROCESS_INS(MOV_DP, 0, dst, SLJIT_UNUSED, (24 << 7) | reg_map[src2])));
1012 return push_inst(compiler, EMIT_DATA_PROCESS_INS(MOV_DP, 0, dst, SLJIT_UNUSED, (24 << 7) | (op == SLJIT_MOV_U8 ? 0x20 : 0x40) | reg_map[dst]));
1014 return push_inst(compiler, (op == SLJIT_MOV_U8 ? UXTB : SXTB) | RD(dst) | RM(src2));
1017 else if (dst != src2) {
1018 SLJIT_ASSERT(src2 & SRC2_IMM);
1019 if (flags & INV_IMM)
1020 EMIT_FULL_DATA_PROCESS_INS_AND_RETURN(MVN_DP, dst, SLJIT_UNUSED, src2);
1021 EMIT_FULL_DATA_PROCESS_INS_AND_RETURN(MOV_DP, dst, SLJIT_UNUSED, src2);
1023 return SLJIT_SUCCESS;
1027 SLJIT_ASSERT(src1 == TMP_REG1 && !(flags & ARGS_SWAPPED));
1028 if ((flags & (REG_DEST | REG_SOURCE)) == (REG_DEST | REG_SOURCE)) {
1029 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
1030 FAIL_IF(push_inst(compiler, EMIT_DATA_PROCESS_INS(MOV_DP, 0, dst, SLJIT_UNUSED, (16 << 7) | reg_map[src2])));
1031 return push_inst(compiler, EMIT_DATA_PROCESS_INS(MOV_DP, 0, dst, SLJIT_UNUSED, (16 << 7) | (op == SLJIT_MOV_U16 ? 0x20 : 0x40) | reg_map[dst]));
1033 return push_inst(compiler, (op == SLJIT_MOV_U16 ? UXTH : SXTH) | RD(dst) | RM(src2));
1036 else if (dst != src2) {
1037 SLJIT_ASSERT(src2 & SRC2_IMM);
1038 if (flags & INV_IMM)
1039 EMIT_FULL_DATA_PROCESS_INS_AND_RETURN(MVN_DP, dst, SLJIT_UNUSED, src2);
1040 EMIT_FULL_DATA_PROCESS_INS_AND_RETURN(MOV_DP, dst, SLJIT_UNUSED, src2);
1042 return SLJIT_SUCCESS;
1045 if (src2 & SRC2_IMM) {
1046 if (flags & INV_IMM)
1047 EMIT_FULL_DATA_PROCESS_INS_AND_RETURN(MOV_DP, dst, SLJIT_UNUSED, src2);
1048 EMIT_FULL_DATA_PROCESS_INS_AND_RETURN(MVN_DP, dst, SLJIT_UNUSED, src2);
1050 EMIT_FULL_DATA_PROCESS_INS_AND_RETURN(MVN_DP, dst, SLJIT_UNUSED, RM(src2));
1053 SLJIT_ASSERT(!(flags & INV_IMM));
1054 SLJIT_ASSERT(!(src2 & SRC2_IMM));
1055 FAIL_IF(push_inst(compiler, CLZ | RD(dst) | RM(src2)));
1056 if (flags & SET_FLAGS)
1057 EMIT_FULL_DATA_PROCESS_INS_AND_RETURN(CMP_DP, SLJIT_UNUSED, dst, SRC2_IMM);
1058 return SLJIT_SUCCESS;
1061 SLJIT_ASSERT(!(flags & INV_IMM));
1062 EMIT_DATA_PROCESS_INS_AND_RETURN(ADD_DP);
1065 SLJIT_ASSERT(!(flags & INV_IMM));
1066 EMIT_DATA_PROCESS_INS_AND_RETURN(ADC_DP);
1069 SLJIT_ASSERT(!(flags & INV_IMM));
1070 if (!(flags & ARGS_SWAPPED))
1071 EMIT_DATA_PROCESS_INS_AND_RETURN(SUB_DP);
1072 EMIT_DATA_PROCESS_INS_AND_RETURN(RSB_DP);
1075 SLJIT_ASSERT(!(flags & INV_IMM));
1076 if (!(flags & ARGS_SWAPPED))
1077 EMIT_DATA_PROCESS_INS_AND_RETURN(SBC_DP);
1078 EMIT_DATA_PROCESS_INS_AND_RETURN(RSC_DP);
1081 SLJIT_ASSERT(!(flags & INV_IMM));
1082 SLJIT_ASSERT(!(src2 & SRC2_IMM));
1083 if (SLJIT_UNLIKELY(op & SLJIT_SET_O))
1084 mul_inst = SMULL | (reg_map[TMP_REG3] << 16) | (reg_map[dst] << 12);
1086 mul_inst = MUL | (reg_map[dst] << 16);
1089 FAIL_IF(push_inst(compiler, mul_inst | (reg_map[src1] << 8) | reg_map[src2]));
1090 else if (dst != src1)
1091 FAIL_IF(push_inst(compiler, mul_inst | (reg_map[src2] << 8) | reg_map[src1]));
1093 /* Rm and Rd must not be the same register. */
1094 SLJIT_ASSERT(dst != TMP_REG1);
1095 FAIL_IF(push_inst(compiler, EMIT_DATA_PROCESS_INS(MOV_DP, 0, TMP_REG1, SLJIT_UNUSED, reg_map[src2])));
1096 FAIL_IF(push_inst(compiler, mul_inst | (reg_map[src2] << 8) | reg_map[TMP_REG1]));
1099 if (!(op & SLJIT_SET_O))
1100 return SLJIT_SUCCESS;
1102 /* We need to use TMP_REG3. */
1103 compiler->cache_arg = 0;
1104 compiler->cache_argw = 0;
1105 /* cmp TMP_REG2, dst asr #31. */
1106 return push_inst(compiler, EMIT_DATA_PROCESS_INS(CMP_DP, SET_FLAGS, SLJIT_UNUSED, TMP_REG3, RM(dst) | 0xfc0));
1109 if (!(flags & INV_IMM))
1110 EMIT_DATA_PROCESS_INS_AND_RETURN(AND_DP);
1111 EMIT_DATA_PROCESS_INS_AND_RETURN(BIC_DP);
1114 SLJIT_ASSERT(!(flags & INV_IMM));
1115 EMIT_DATA_PROCESS_INS_AND_RETURN(ORR_DP);
1118 SLJIT_ASSERT(!(flags & INV_IMM));
1119 EMIT_DATA_PROCESS_INS_AND_RETURN(EOR_DP);
1122 EMIT_SHIFT_INS_AND_RETURN(0);
1125 EMIT_SHIFT_INS_AND_RETURN(1);
1128 EMIT_SHIFT_INS_AND_RETURN(2);
1130 SLJIT_ASSERT_STOP();
1131 return SLJIT_SUCCESS;
1134 #undef EMIT_DATA_PROCESS_INS_AND_RETURN
1135 #undef EMIT_FULL_DATA_PROCESS_INS_AND_RETURN
1136 #undef EMIT_SHIFT_INS_AND_RETURN
1138 /* Tests whether the immediate can be stored in the 12 bit imm field.
1139 Returns with 0 if not possible. */
1140 static sljit_uw get_imm(sljit_uw imm)
1145 return SRC2_IMM | imm;
1147 if (!(imm & 0xff000000)) {
1152 imm = (imm << 24) | (imm >> 8);
1156 if (!(imm & 0xff000000)) {
1161 if (!(imm & 0xf0000000)) {
1166 if (!(imm & 0xc0000000)) {
1171 if (!(imm & 0x00ffffff))
1172 return SRC2_IMM | (imm >> 24) | (rol << 8);
1177 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
1178 static sljit_s32 generate_int(struct sljit_compiler *compiler, sljit_s32 reg, sljit_uw imm, sljit_s32 positive)
1185 /* Step1: Search a zero byte (8 continous zero bit). */
1189 if (!(imm & mask)) {
1190 /* Rol imm by rol. */
1191 imm = (imm << rol) | (imm >> (32 - rol));
1192 /* Calculate arm rol. */
1193 rol = 4 + (rol >> 1);
1200 imm = (imm << 8) | (imm >> 24);
1204 if (!(imm & mask)) {
1205 /* Rol imm by rol. */
1206 imm = (imm << rol) | (imm >> (32 - rol));
1207 /* Calculate arm rol. */
1208 rol = (rol >> 1) - 8;
1220 /* The low 8 bit must be zero. */
1221 SLJIT_ASSERT(!(imm & 0xff));
1223 if (!(imm & 0xff000000)) {
1224 imm1 = SRC2_IMM | ((imm >> 16) & 0xff) | (((rol + 4) & 0xf) << 8);
1225 imm2 = SRC2_IMM | ((imm >> 8) & 0xff) | (((rol + 8) & 0xf) << 8);
1227 else if (imm & 0xc0000000) {
1228 imm1 = SRC2_IMM | ((imm >> 24) & 0xff) | ((rol & 0xf) << 8);
1232 if (!(imm & 0xff000000)) {
1237 if (!(imm & 0xf0000000)) {
1242 if (!(imm & 0xc0000000)) {
1247 if (!(imm & 0x00ffffff))
1248 imm2 = SRC2_IMM | (imm >> 24) | ((rol & 0xf) << 8);
1253 if (!(imm & 0xf0000000)) {
1258 if (!(imm & 0xc0000000)) {
1263 imm1 = SRC2_IMM | ((imm >> 24) & 0xff) | ((rol & 0xf) << 8);
1267 if (!(imm & 0xf0000000)) {
1272 if (!(imm & 0xc0000000)) {
1277 if (!(imm & 0x00ffffff))
1278 imm2 = SRC2_IMM | (imm >> 24) | ((rol & 0xf) << 8);
1283 FAIL_IF(push_inst(compiler, EMIT_DATA_PROCESS_INS(positive ? MOV_DP : MVN_DP, 0, reg, SLJIT_UNUSED, imm1)));
1284 FAIL_IF(push_inst(compiler, EMIT_DATA_PROCESS_INS(positive ? ORR_DP : BIC_DP, 0, reg, reg, imm2)));
1289 static sljit_s32 load_immediate(struct sljit_compiler *compiler, sljit_s32 reg, sljit_uw imm)
1293 #if (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
1294 if (!(imm & ~0xffff))
1295 return push_inst(compiler, MOVW | RD(reg) | ((imm << 4) & 0xf0000) | (imm & 0xfff));
1298 /* Create imm by 1 inst. */
1301 return push_inst(compiler, EMIT_DATA_PROCESS_INS(MOV_DP, 0, reg, SLJIT_UNUSED, tmp));
1303 tmp = get_imm(~imm);
1305 return push_inst(compiler, EMIT_DATA_PROCESS_INS(MVN_DP, 0, reg, SLJIT_UNUSED, tmp));
1307 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
1308 /* Create imm by 2 inst. */
1309 FAIL_IF(generate_int(compiler, reg, imm, 1));
1310 FAIL_IF(generate_int(compiler, reg, ~imm, 0));
1313 return push_inst_with_literal(compiler, EMIT_DATA_TRANSFER(WORD_DATA | LOAD_DATA, 1, 0, reg, TMP_PC, 0), imm);
1315 return emit_imm(compiler, reg, imm);
1319 /* Helper function. Dst should be reg + value, using at most 1 instruction, flags does not set. */
1320 static sljit_s32 emit_set_delta(struct sljit_compiler *compiler, sljit_s32 dst, sljit_s32 reg, sljit_sw value)
1323 value = get_imm(value);
1325 return push_inst(compiler, EMIT_DATA_PROCESS_INS(ADD_DP, 0, dst, reg, value));
1328 value = get_imm(-value);
1330 return push_inst(compiler, EMIT_DATA_PROCESS_INS(SUB_DP, 0, dst, reg, value));
1332 return SLJIT_ERR_UNSUPPORTED;
1335 /* Can perform an operation using at most 1 instruction. */
1336 static sljit_s32 getput_arg_fast(struct sljit_compiler *compiler, sljit_s32 inp_flags, sljit_s32 reg, sljit_s32 arg, sljit_sw argw)
1340 if (arg & SLJIT_IMM) {
1341 imm = get_imm(argw);
1343 if (inp_flags & ARG_TEST)
1345 FAIL_IF(push_inst(compiler, EMIT_DATA_PROCESS_INS(MOV_DP, 0, reg, SLJIT_UNUSED, imm)));
1348 imm = get_imm(~argw);
1350 if (inp_flags & ARG_TEST)
1352 FAIL_IF(push_inst(compiler, EMIT_DATA_PROCESS_INS(MVN_DP, 0, reg, SLJIT_UNUSED, imm)));
1358 SLJIT_ASSERT(arg & SLJIT_MEM);
1360 /* Fast loads/stores. */
1361 if (!(arg & REG_MASK))
1364 if (arg & OFFS_REG_MASK) {
1365 if ((argw & 0x3) != 0 && !IS_TYPE1_TRANSFER(inp_flags))
1368 if (inp_flags & ARG_TEST)
1370 FAIL_IF(push_inst(compiler, EMIT_DATA_TRANSFER(inp_flags, 1, inp_flags & WRITE_BACK, reg, arg & REG_MASK,
1371 RM(OFFS_REG(arg)) | (IS_TYPE1_TRANSFER(inp_flags) ? SRC2_IMM : 0) | ((argw & 0x3) << 7))));
1375 if (IS_TYPE1_TRANSFER(inp_flags)) {
1376 if (argw >= 0 && argw <= 0xfff) {
1377 if (inp_flags & ARG_TEST)
1379 FAIL_IF(push_inst(compiler, EMIT_DATA_TRANSFER(inp_flags, 1, inp_flags & WRITE_BACK, reg, arg & REG_MASK, argw)));
1382 if (argw < 0 && argw >= -0xfff) {
1383 if (inp_flags & ARG_TEST)
1385 FAIL_IF(push_inst(compiler, EMIT_DATA_TRANSFER(inp_flags, 0, inp_flags & WRITE_BACK, reg, arg & REG_MASK, -argw)));
1390 if (argw >= 0 && argw <= 0xff) {
1391 if (inp_flags & ARG_TEST)
1393 FAIL_IF(push_inst(compiler, EMIT_DATA_TRANSFER(inp_flags, 1, inp_flags & WRITE_BACK, reg, arg & REG_MASK, TYPE2_TRANSFER_IMM(argw))));
1396 if (argw < 0 && argw >= -0xff) {
1397 if (inp_flags & ARG_TEST)
1400 FAIL_IF(push_inst(compiler, EMIT_DATA_TRANSFER(inp_flags, 0, inp_flags & WRITE_BACK, reg, arg & REG_MASK, TYPE2_TRANSFER_IMM(argw))));
1408 /* See getput_arg below.
1409 Note: can_cache is called only for binary operators. Those
1410 operators always uses word arguments without write back. */
1411 static sljit_s32 can_cache(sljit_s32 arg, sljit_sw argw, sljit_s32 next_arg, sljit_sw next_argw)
1413 /* Immediate caching is not supported as it would be an operation on constant arguments. */
1414 if (arg & SLJIT_IMM)
1417 /* Always a simple operation. */
1418 if (arg & OFFS_REG_MASK)
1421 if (!(arg & REG_MASK)) {
1422 /* Immediate access. */
1423 if ((next_arg & SLJIT_MEM) && ((sljit_uw)argw - (sljit_uw)next_argw <= 0xfff || (sljit_uw)next_argw - (sljit_uw)argw <= 0xfff))
1428 if (argw <= 0xfffff && argw >= -0xfffff)
1431 if (argw == next_argw && (next_arg & SLJIT_MEM))
1434 if (arg == next_arg && ((sljit_uw)argw - (sljit_uw)next_argw <= 0xfff || (sljit_uw)next_argw - (sljit_uw)argw <= 0xfff))
1440 #define GETPUT_ARG_DATA_TRANSFER(add, wb, target, base, imm) \
1441 if (max_delta & 0xf00) \
1442 FAIL_IF(push_inst(compiler, EMIT_DATA_TRANSFER(inp_flags, add, wb, target, base, imm))); \
1444 FAIL_IF(push_inst(compiler, EMIT_DATA_TRANSFER(inp_flags, add, wb, target, base, TYPE2_TRANSFER_IMM(imm))));
1446 #define TEST_WRITE_BACK() \
1447 if (inp_flags & WRITE_BACK) { \
1448 tmp_r = arg & REG_MASK; \
1449 if (reg == tmp_r) { \
1450 /* This can only happen for stores */ \
1451 /* since ldr reg, [reg, ...]! has no meaning */ \
1452 SLJIT_ASSERT(!(inp_flags & LOAD_DATA)); \
1453 FAIL_IF(push_inst(compiler, EMIT_DATA_PROCESS_INS(MOV_DP, 0, TMP_REG3, SLJIT_UNUSED, RM(reg)))); \
1458 /* Emit the necessary instructions. See can_cache above. */
1459 static sljit_s32 getput_arg(struct sljit_compiler *compiler, sljit_s32 inp_flags, sljit_s32 reg, sljit_s32 arg, sljit_sw argw, sljit_s32 next_arg, sljit_sw next_argw)
1466 if (arg & SLJIT_IMM) {
1467 SLJIT_ASSERT(inp_flags & LOAD_DATA);
1468 return load_immediate(compiler, reg, argw);
1471 SLJIT_ASSERT(arg & SLJIT_MEM);
1473 tmp_r = (inp_flags & LOAD_DATA) ? reg : TMP_REG3;
1474 max_delta = IS_TYPE1_TRANSFER(inp_flags) ? 0xfff : 0xff;
1476 if ((arg & REG_MASK) == SLJIT_UNUSED) {
1477 /* Write back is not used. */
1478 imm = (sljit_uw)(argw - compiler->cache_argw);
1479 if ((compiler->cache_arg & SLJIT_IMM) && (imm <= (sljit_uw)max_delta || imm >= (sljit_uw)-max_delta)) {
1480 if (imm <= (sljit_uw)max_delta) {
1482 argw = argw - compiler->cache_argw;
1486 argw = compiler->cache_argw - argw;
1489 GETPUT_ARG_DATA_TRANSFER(sign, 0, reg, TMP_REG3, argw);
1490 return SLJIT_SUCCESS;
1493 /* With write back, we can create some sophisticated loads, but
1494 it is hard to decide whether we should convert downward (0s) or upward (1s). */
1495 imm = (sljit_uw)(argw - next_argw);
1496 if ((next_arg & SLJIT_MEM) && (imm <= (sljit_uw)max_delta || imm >= (sljit_uw)-max_delta)) {
1497 SLJIT_ASSERT(inp_flags & LOAD_DATA);
1499 compiler->cache_arg = SLJIT_IMM;
1500 compiler->cache_argw = argw;
1504 FAIL_IF(load_immediate(compiler, tmp_r, argw));
1505 GETPUT_ARG_DATA_TRANSFER(1, 0, reg, tmp_r, 0);
1506 return SLJIT_SUCCESS;
1509 if (arg & OFFS_REG_MASK) {
1510 SLJIT_ASSERT((argw & 0x3) && !(max_delta & 0xf00));
1511 if (inp_flags & WRITE_BACK)
1512 tmp_r = arg & REG_MASK;
1513 FAIL_IF(push_inst(compiler, EMIT_DATA_PROCESS_INS(ADD_DP, 0, tmp_r, arg & REG_MASK, RM(OFFS_REG(arg)) | ((argw & 0x3) << 7))));
1514 return push_inst(compiler, EMIT_DATA_TRANSFER(inp_flags, 1, 0, reg, tmp_r, TYPE2_TRANSFER_IMM(0)));
1517 imm = (sljit_uw)(argw - compiler->cache_argw);
1518 if (compiler->cache_arg == arg && imm <= (sljit_uw)max_delta) {
1519 SLJIT_ASSERT(!(inp_flags & WRITE_BACK));
1520 GETPUT_ARG_DATA_TRANSFER(1, 0, reg, TMP_REG3, imm);
1521 return SLJIT_SUCCESS;
1523 if (compiler->cache_arg == arg && imm >= (sljit_uw)-max_delta) {
1524 SLJIT_ASSERT(!(inp_flags & WRITE_BACK));
1525 imm = (sljit_uw)-(sljit_sw)imm;
1526 GETPUT_ARG_DATA_TRANSFER(0, 0, reg, TMP_REG3, imm);
1527 return SLJIT_SUCCESS;
1530 imm = get_imm(argw & ~max_delta);
1533 FAIL_IF(push_inst(compiler, EMIT_DATA_PROCESS_INS(ADD_DP, 0, tmp_r, arg & REG_MASK, imm)));
1534 GETPUT_ARG_DATA_TRANSFER(1, inp_flags & WRITE_BACK, reg, tmp_r, argw & max_delta);
1535 return SLJIT_SUCCESS;
1538 imm = get_imm(-argw & ~max_delta);
1542 FAIL_IF(push_inst(compiler, EMIT_DATA_PROCESS_INS(SUB_DP, 0, tmp_r, arg & REG_MASK, imm)));
1543 GETPUT_ARG_DATA_TRANSFER(0, inp_flags & WRITE_BACK, reg, tmp_r, argw & max_delta);
1544 return SLJIT_SUCCESS;
1547 if ((compiler->cache_arg & SLJIT_IMM) && compiler->cache_argw == argw) {
1549 return push_inst(compiler, EMIT_DATA_TRANSFER(inp_flags, 1, inp_flags & WRITE_BACK, reg, arg & REG_MASK, RM(TMP_REG3) | (max_delta & 0xf00 ? SRC2_IMM : 0)));
1552 if (argw == next_argw && (next_arg & SLJIT_MEM)) {
1553 SLJIT_ASSERT(inp_flags & LOAD_DATA);
1554 FAIL_IF(load_immediate(compiler, TMP_REG3, argw));
1556 compiler->cache_arg = SLJIT_IMM;
1557 compiler->cache_argw = argw;
1560 return push_inst(compiler, EMIT_DATA_TRANSFER(inp_flags, 1, inp_flags & WRITE_BACK, reg, arg & REG_MASK, RM(TMP_REG3) | (max_delta & 0xf00 ? SRC2_IMM : 0)));
1563 imm = (sljit_uw)(argw - next_argw);
1564 if (arg == next_arg && !(inp_flags & WRITE_BACK) && (imm <= (sljit_uw)max_delta || imm >= (sljit_uw)-max_delta)) {
1565 SLJIT_ASSERT(inp_flags & LOAD_DATA);
1566 FAIL_IF(load_immediate(compiler, TMP_REG3, argw));
1567 FAIL_IF(push_inst(compiler, EMIT_DATA_PROCESS_INS(ADD_DP, 0, TMP_REG3, TMP_REG3, reg_map[arg & REG_MASK])));
1569 compiler->cache_arg = arg;
1570 compiler->cache_argw = argw;
1572 GETPUT_ARG_DATA_TRANSFER(1, 0, reg, TMP_REG3, 0);
1573 return SLJIT_SUCCESS;
1576 if ((arg & REG_MASK) == tmp_r) {
1577 compiler->cache_arg = SLJIT_IMM;
1578 compiler->cache_argw = argw;
1582 FAIL_IF(load_immediate(compiler, tmp_r, argw));
1583 return push_inst(compiler, EMIT_DATA_TRANSFER(inp_flags, 1, inp_flags & WRITE_BACK, reg, arg & REG_MASK, reg_map[tmp_r] | (max_delta & 0xf00 ? SRC2_IMM : 0)));
1586 static SLJIT_INLINE sljit_s32 emit_op_mem(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg, sljit_s32 arg, sljit_sw argw)
1588 if (getput_arg_fast(compiler, flags, reg, arg, argw))
1589 return compiler->error;
1590 compiler->cache_arg = 0;
1591 compiler->cache_argw = 0;
1592 return getput_arg(compiler, flags, reg, arg, argw, 0, 0);
1595 static SLJIT_INLINE sljit_s32 emit_op_mem2(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg, sljit_s32 arg1, sljit_sw arg1w, sljit_s32 arg2, sljit_sw arg2w)
1597 if (getput_arg_fast(compiler, flags, reg, arg1, arg1w))
1598 return compiler->error;
1599 return getput_arg(compiler, flags, reg, arg1, arg1w, arg2, arg2w);
1602 static sljit_s32 emit_op(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 inp_flags,
1603 sljit_s32 dst, sljit_sw dstw,
1604 sljit_s32 src1, sljit_sw src1w,
1605 sljit_s32 src2, sljit_sw src2w)
1607 /* arg1 goes to TMP_REG1 or src reg
1608 arg2 goes to TMP_REG2, imm or src reg
1609 TMP_REG3 can be used for caching
1610 result goes to TMP_REG2, so put result can use TMP_REG1 and TMP_REG3. */
1612 /* We prefers register and simple consts. */
1615 sljit_s32 src2_r = 0;
1616 sljit_s32 sugg_src2_r = TMP_REG2;
1617 sljit_s32 flags = GET_FLAGS(op) ? SET_FLAGS : 0;
1619 compiler->cache_arg = 0;
1620 compiler->cache_argw = 0;
1622 /* Destination check. */
1623 if (SLJIT_UNLIKELY(dst == SLJIT_UNUSED)) {
1624 if (op >= SLJIT_MOV && op <= SLJIT_MOVU_S32 && !(src2 & SLJIT_MEM))
1625 return SLJIT_SUCCESS;
1628 else if (FAST_IS_REG(dst)) {
1631 if (op >= SLJIT_MOV && op <= SLJIT_MOVU_S32)
1632 sugg_src2_r = dst_r;
1635 SLJIT_ASSERT(dst & SLJIT_MEM);
1636 if (getput_arg_fast(compiler, inp_flags | ARG_TEST, TMP_REG2, dst, dstw)) {
1647 if (FAST_IS_REG(src1))
1649 else if (FAST_IS_REG(src2)) {
1650 flags |= ARGS_SWAPPED;
1655 else do { /* do { } while(0) is used because of breaks. */
1657 if ((inp_flags & ALLOW_ANY_IMM) && (src1 & SLJIT_IMM)) {
1658 /* The second check will generate a hit. */
1659 src2_r = get_imm(src1w);
1661 flags |= ARGS_SWAPPED;
1666 if (inp_flags & ALLOW_INV_IMM) {
1667 src2_r = get_imm(~src1w);
1669 flags |= ARGS_SWAPPED | INV_IMM;
1675 if (GET_OPCODE(op) == SLJIT_ADD) {
1676 src2_r = get_imm(-src1w);
1678 /* Note: ARGS_SWAPPED is intentionally not applied! */
1681 op = SLJIT_SUB | GET_ALL_FLAGS(op);
1687 if (getput_arg_fast(compiler, inp_flags | LOAD_DATA, TMP_REG1, src1, src1w)) {
1688 FAIL_IF(compiler->error);
1695 if (FAST_IS_REG(src2)) {
1697 flags |= REG_SOURCE;
1698 if (!(flags & REG_DEST) && op >= SLJIT_MOV && op <= SLJIT_MOVU_S32)
1701 else do { /* do { } while(0) is used because of breaks. */
1702 if ((inp_flags & ALLOW_ANY_IMM) && (src2 & SLJIT_IMM)) {
1703 src2_r = get_imm(src2w);
1706 if (inp_flags & ALLOW_INV_IMM) {
1707 src2_r = get_imm(~src2w);
1713 if (GET_OPCODE(op) == SLJIT_ADD) {
1714 src2_r = get_imm(-src2w);
1716 op = SLJIT_SUB | GET_ALL_FLAGS(op);
1717 flags &= ~ARGS_SWAPPED;
1721 if (GET_OPCODE(op) == SLJIT_SUB && !(flags & ARGS_SWAPPED)) {
1722 src2_r = get_imm(-src2w);
1724 op = SLJIT_ADD | GET_ALL_FLAGS(op);
1725 flags &= ~ARGS_SWAPPED;
1732 if (getput_arg_fast(compiler, inp_flags | LOAD_DATA, sugg_src2_r, src2, src2w)) {
1733 FAIL_IF(compiler->error);
1734 src2_r = sugg_src2_r;
1739 /* src1_r, src2_r and dst_r can be zero (=unprocessed) or non-zero.
1740 If they are zero, they must not be registers. */
1741 if (src1_r == 0 && src2_r == 0 && dst_r == 0) {
1742 if (!can_cache(src1, src1w, src2, src2w) && can_cache(src1, src1w, dst, dstw)) {
1743 SLJIT_ASSERT(!(flags & ARGS_SWAPPED));
1744 flags |= ARGS_SWAPPED;
1745 FAIL_IF(getput_arg(compiler, inp_flags | LOAD_DATA, TMP_REG1, src2, src2w, src1, src1w));
1746 FAIL_IF(getput_arg(compiler, inp_flags | LOAD_DATA, TMP_REG2, src1, src1w, dst, dstw));
1749 FAIL_IF(getput_arg(compiler, inp_flags | LOAD_DATA, TMP_REG1, src1, src1w, src2, src2w));
1750 FAIL_IF(getput_arg(compiler, inp_flags | LOAD_DATA, TMP_REG2, src2, src2w, dst, dstw));
1755 else if (src1_r == 0 && src2_r == 0) {
1756 FAIL_IF(getput_arg(compiler, inp_flags | LOAD_DATA, TMP_REG1, src1, src1w, src2, src2w));
1759 else if (src1_r == 0 && dst_r == 0) {
1760 FAIL_IF(getput_arg(compiler, inp_flags | LOAD_DATA, TMP_REG1, src1, src1w, dst, dstw));
1763 else if (src2_r == 0 && dst_r == 0) {
1764 FAIL_IF(getput_arg(compiler, inp_flags | LOAD_DATA, sugg_src2_r, src2, src2w, dst, dstw));
1765 src2_r = sugg_src2_r;
1772 FAIL_IF(getput_arg(compiler, inp_flags | LOAD_DATA, TMP_REG1, src1, src1w, 0, 0));
1777 FAIL_IF(getput_arg(compiler, inp_flags | LOAD_DATA, sugg_src2_r, src2, src2w, 0, 0));
1778 src2_r = sugg_src2_r;
1781 FAIL_IF(emit_single_op(compiler, op, flags, dst_r, src1_r, src2_r));
1783 if (flags & (FAST_DEST | SLOW_DEST)) {
1784 if (flags & FAST_DEST)
1785 FAIL_IF(getput_arg_fast(compiler, inp_flags, dst_r, dst, dstw));
1787 FAIL_IF(getput_arg(compiler, inp_flags, dst_r, dst, dstw, 0, 0));
1789 return SLJIT_SUCCESS;
1796 #if defined(__GNUC__)
1797 extern unsigned int __aeabi_uidivmod(unsigned int numerator, unsigned int denominator);
1798 extern int __aeabi_idivmod(int numerator, int denominator);
1800 #error "Software divmod functions are needed"
1807 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op0(struct sljit_compiler *compiler, sljit_s32 op)
1810 CHECK(check_sljit_emit_op0(compiler, op));
1812 op = GET_OPCODE(op);
1814 case SLJIT_BREAKPOINT:
1815 FAIL_IF(push_inst(compiler, BKPT));
1818 FAIL_IF(push_inst(compiler, NOP));
1822 #if (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
1823 return push_inst(compiler, (op == SLJIT_LMUL_UW ? UMULL : SMULL)
1824 | (reg_map[SLJIT_R1] << 16)
1825 | (reg_map[SLJIT_R0] << 12)
1826 | (reg_map[SLJIT_R0] << 8)
1827 | reg_map[SLJIT_R1]);
1829 FAIL_IF(push_inst(compiler, EMIT_DATA_PROCESS_INS(MOV_DP, 0, TMP_REG1, SLJIT_UNUSED, RM(SLJIT_R1))));
1830 return push_inst(compiler, (op == SLJIT_LMUL_UW ? UMULL : SMULL)
1831 | (reg_map[SLJIT_R1] << 16)
1832 | (reg_map[SLJIT_R0] << 12)
1833 | (reg_map[SLJIT_R0] << 8)
1834 | reg_map[TMP_REG1]);
1836 case SLJIT_DIVMOD_UW:
1837 case SLJIT_DIVMOD_SW:
1840 SLJIT_COMPILE_ASSERT((SLJIT_DIVMOD_UW & 0x2) == 0 && SLJIT_DIV_UW - 0x2 == SLJIT_DIVMOD_UW, bad_div_opcode_assignments);
1841 SLJIT_COMPILE_ASSERT(reg_map[2] == 1 && reg_map[3] == 2, bad_register_mapping);
1843 if ((op >= SLJIT_DIV_UW) && (compiler->scratches >= 3)) {
1844 FAIL_IF(push_inst(compiler, 0xe52d2008 /* str r2, [sp, #-8]! */));
1845 FAIL_IF(push_inst(compiler, 0xe58d1004 /* str r1, [sp, #4] */));
1847 else if ((op >= SLJIT_DIV_UW) || (compiler->scratches >= 3))
1848 FAIL_IF(push_inst(compiler, 0xe52d0008 | (op >= SLJIT_DIV_UW ? 0x1000 : 0x2000) /* str r1/r2, [sp, #-8]! */));
1850 #if defined(__GNUC__)
1851 FAIL_IF(sljit_emit_ijump(compiler, SLJIT_FAST_CALL, SLJIT_IMM,
1852 ((op | 0x2) == SLJIT_DIV_UW ? SLJIT_FUNC_OFFSET(__aeabi_uidivmod) : SLJIT_FUNC_OFFSET(__aeabi_idivmod))));
1854 #error "Software divmod functions are needed"
1857 if ((op >= SLJIT_DIV_UW) && (compiler->scratches >= 3)) {
1858 FAIL_IF(push_inst(compiler, 0xe59d1004 /* ldr r1, [sp, #4] */));
1859 FAIL_IF(push_inst(compiler, 0xe49d2008 /* ldr r2, [sp], #8 */));
1861 else if ((op >= SLJIT_DIV_UW) || (compiler->scratches >= 3))
1862 return push_inst(compiler, 0xe49d0008 | (op >= SLJIT_DIV_UW ? 0x1000 : 0x2000) /* ldr r1/r2, [sp], #8 */);
1863 return SLJIT_SUCCESS;
1866 return SLJIT_SUCCESS;
1869 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op1(struct sljit_compiler *compiler, sljit_s32 op,
1870 sljit_s32 dst, sljit_sw dstw,
1871 sljit_s32 src, sljit_sw srcw)
1874 CHECK(check_sljit_emit_op1(compiler, op, dst, dstw, src, srcw));
1875 ADJUST_LOCAL_OFFSET(dst, dstw);
1876 ADJUST_LOCAL_OFFSET(src, srcw);
1878 switch (GET_OPCODE(op)) {
1883 return emit_op(compiler, SLJIT_MOV, ALLOW_ANY_IMM, dst, dstw, TMP_REG1, 0, src, srcw);
1886 return emit_op(compiler, SLJIT_MOV_U8, ALLOW_ANY_IMM | BYTE_DATA, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_u8)srcw : srcw);
1889 return emit_op(compiler, SLJIT_MOV_S8, ALLOW_ANY_IMM | SIGNED_DATA | BYTE_DATA, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_s8)srcw : srcw);
1892 return emit_op(compiler, SLJIT_MOV_U16, ALLOW_ANY_IMM | HALF_DATA, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_u16)srcw : srcw);
1895 return emit_op(compiler, SLJIT_MOV_S16, ALLOW_ANY_IMM | SIGNED_DATA | HALF_DATA, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_s16)srcw : srcw);
1898 case SLJIT_MOVU_U32:
1899 case SLJIT_MOVU_S32:
1901 return emit_op(compiler, SLJIT_MOV, ALLOW_ANY_IMM | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, srcw);
1904 return emit_op(compiler, SLJIT_MOV_U8, ALLOW_ANY_IMM | BYTE_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_u8)srcw : srcw);
1907 return emit_op(compiler, SLJIT_MOV_S8, ALLOW_ANY_IMM | SIGNED_DATA | BYTE_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_s8)srcw : srcw);
1909 case SLJIT_MOVU_U16:
1910 return emit_op(compiler, SLJIT_MOV_U16, ALLOW_ANY_IMM | HALF_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_u16)srcw : srcw);
1912 case SLJIT_MOVU_S16:
1913 return emit_op(compiler, SLJIT_MOV_S16, ALLOW_ANY_IMM | SIGNED_DATA | HALF_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_s16)srcw : srcw);
1916 return emit_op(compiler, op, ALLOW_ANY_IMM, dst, dstw, TMP_REG1, 0, src, srcw);
1919 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
1920 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
1921 compiler->skip_checks = 1;
1923 return sljit_emit_op2(compiler, SLJIT_SUB | GET_ALL_FLAGS(op), dst, dstw, SLJIT_IMM, 0, src, srcw);
1926 return emit_op(compiler, op, 0, dst, dstw, TMP_REG1, 0, src, srcw);
1929 return SLJIT_SUCCESS;
1932 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op2(struct sljit_compiler *compiler, sljit_s32 op,
1933 sljit_s32 dst, sljit_sw dstw,
1934 sljit_s32 src1, sljit_sw src1w,
1935 sljit_s32 src2, sljit_sw src2w)
1938 CHECK(check_sljit_emit_op2(compiler, op, dst, dstw, src1, src1w, src2, src2w));
1939 ADJUST_LOCAL_OFFSET(dst, dstw);
1940 ADJUST_LOCAL_OFFSET(src1, src1w);
1941 ADJUST_LOCAL_OFFSET(src2, src2w);
1943 switch (GET_OPCODE(op)) {
1950 return emit_op(compiler, op, ALLOW_IMM, dst, dstw, src1, src1w, src2, src2w);
1953 return emit_op(compiler, op, 0, dst, dstw, src1, src1w, src2, src2w);
1956 return emit_op(compiler, op, ALLOW_ANY_IMM, dst, dstw, src1, src1w, src2, src2w);
1961 if (src2 & SLJIT_IMM) {
1962 compiler->shift_imm = src2w & 0x1f;
1963 return emit_op(compiler, op, 0, dst, dstw, TMP_REG1, 0, src1, src1w);
1966 compiler->shift_imm = 0x20;
1967 return emit_op(compiler, op, 0, dst, dstw, src1, src1w, src2, src2w);
1971 return SLJIT_SUCCESS;
1974 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_register_index(sljit_s32 reg)
1976 CHECK_REG_INDEX(check_sljit_get_register_index(reg));
1977 return reg_map[reg];
1980 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_float_register_index(sljit_s32 reg)
1982 CHECK_REG_INDEX(check_sljit_get_float_register_index(reg));
1986 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_custom(struct sljit_compiler *compiler,
1987 void *instruction, sljit_s32 size)
1990 CHECK(check_sljit_emit_op_custom(compiler, instruction, size));
1992 return push_inst(compiler, *(sljit_uw*)instruction);
1995 /* --------------------------------------------------------------------- */
1996 /* Floating point operators */
1997 /* --------------------------------------------------------------------- */
1999 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
2003 static sljit_s32 arm_fpu_type = -1;
2005 static void init_compiler(void)
2007 if (arm_fpu_type != -1)
2010 /* TODO: Only the OS can help to determine the correct fpu type. */
2014 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_is_fpu_available(void)
2016 #ifdef SLJIT_IS_FPU_AVAILABLE
2017 return SLJIT_IS_FPU_AVAILABLE;
2019 if (arm_fpu_type == -1)
2021 return arm_fpu_type;
2027 #define arm_fpu_type 1
2029 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_is_fpu_available(void)
2031 /* Always available. */
2037 #define FPU_LOAD (1 << 20)
2038 #define EMIT_FPU_DATA_TRANSFER(inst, add, base, freg, offs) \
2039 ((inst) | ((add) << 23) | (reg_map[base] << 16) | (freg << 12) | (offs))
2040 #define EMIT_FPU_OPERATION(opcode, mode, dst, src1, src2) \
2041 ((opcode) | (mode) | ((dst) << 12) | (src1) | ((src2) << 16))
2043 static sljit_s32 emit_fop_mem(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg, sljit_s32 arg, sljit_sw argw)
2047 sljit_sw inst = VSTR_F32 | (flags & (SLJIT_F32_OP | FPU_LOAD));
2048 SLJIT_ASSERT(arg & SLJIT_MEM);
2050 if (SLJIT_UNLIKELY(arg & OFFS_REG_MASK)) {
2051 FAIL_IF(push_inst(compiler, EMIT_DATA_PROCESS_INS(ADD_DP, 0, TMP_REG1, arg & REG_MASK, RM(OFFS_REG(arg)) | ((argw & 0x3) << 7))));
2052 arg = SLJIT_MEM | TMP_REG1;
2056 /* Fast loads and stores. */
2057 if ((arg & REG_MASK)) {
2058 if (!(argw & ~0x3fc))
2059 return push_inst(compiler, EMIT_FPU_DATA_TRANSFER(inst, 1, arg & REG_MASK, reg, argw >> 2));
2060 if (!(-argw & ~0x3fc))
2061 return push_inst(compiler, EMIT_FPU_DATA_TRANSFER(inst, 0, arg & REG_MASK, reg, (-argw) >> 2));
2064 if (compiler->cache_arg == arg) {
2065 tmp = argw - compiler->cache_argw;
2066 if (!(tmp & ~0x3fc))
2067 return push_inst(compiler, EMIT_FPU_DATA_TRANSFER(inst, 1, TMP_REG3, reg, tmp >> 2));
2068 if (!(-tmp & ~0x3fc))
2069 return push_inst(compiler, EMIT_FPU_DATA_TRANSFER(inst, 0, TMP_REG3, reg, -tmp >> 2));
2070 if (emit_set_delta(compiler, TMP_REG3, TMP_REG3, tmp) != SLJIT_ERR_UNSUPPORTED) {
2071 FAIL_IF(compiler->error);
2072 compiler->cache_argw = argw;
2073 return push_inst(compiler, EMIT_FPU_DATA_TRANSFER(inst, 1, TMP_REG3, reg, 0));
2077 if (arg & REG_MASK) {
2078 if (emit_set_delta(compiler, TMP_REG1, arg & REG_MASK, argw) != SLJIT_ERR_UNSUPPORTED) {
2079 FAIL_IF(compiler->error);
2080 return push_inst(compiler, EMIT_FPU_DATA_TRANSFER(inst, 1, TMP_REG1, reg, 0));
2082 imm = get_imm(argw & ~0x3fc);
2084 FAIL_IF(push_inst(compiler, EMIT_DATA_PROCESS_INS(ADD_DP, 0, TMP_REG1, arg & REG_MASK, imm)));
2085 return push_inst(compiler, EMIT_FPU_DATA_TRANSFER(inst, 1, TMP_REG1, reg, (argw & 0x3fc) >> 2));
2087 imm = get_imm(-argw & ~0x3fc);
2090 FAIL_IF(push_inst(compiler, EMIT_DATA_PROCESS_INS(SUB_DP, 0, TMP_REG1, arg & REG_MASK, imm)));
2091 return push_inst(compiler, EMIT_FPU_DATA_TRANSFER(inst, 0, TMP_REG1, reg, (argw & 0x3fc) >> 2));
2095 compiler->cache_arg = arg;
2096 compiler->cache_argw = argw;
2097 if (arg & REG_MASK) {
2098 FAIL_IF(load_immediate(compiler, TMP_REG1, argw));
2099 FAIL_IF(push_inst(compiler, EMIT_DATA_PROCESS_INS(ADD_DP, 0, TMP_REG3, arg & REG_MASK, reg_map[TMP_REG1])));
2102 FAIL_IF(load_immediate(compiler, TMP_REG3, argw));
2104 return push_inst(compiler, EMIT_FPU_DATA_TRANSFER(inst, 1, TMP_REG3, reg, 0));
2107 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_conv_sw_from_f64(struct sljit_compiler *compiler, sljit_s32 op,
2108 sljit_s32 dst, sljit_sw dstw,
2109 sljit_s32 src, sljit_sw srcw)
2111 if (src & SLJIT_MEM) {
2112 FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, TMP_FREG1, src, srcw));
2116 FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VCVT_S32_F32, op & SLJIT_F32_OP, TMP_FREG1, src, 0)));
2118 if (dst == SLJIT_UNUSED)
2119 return SLJIT_SUCCESS;
2121 if (FAST_IS_REG(dst))
2122 return push_inst(compiler, VMOV | (1 << 20) | RD(dst) | (TMP_FREG1 << 16));
2124 /* Store the integer value from a VFP register. */
2125 return emit_fop_mem(compiler, 0, TMP_FREG1, dst, dstw);
2128 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_conv_f64_from_sw(struct sljit_compiler *compiler, sljit_s32 op,
2129 sljit_s32 dst, sljit_sw dstw,
2130 sljit_s32 src, sljit_sw srcw)
2132 sljit_s32 dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
2134 if (FAST_IS_REG(src))
2135 FAIL_IF(push_inst(compiler, VMOV | RD(src) | (TMP_FREG1 << 16)));
2136 else if (src & SLJIT_MEM) {
2137 /* Load the integer value into a VFP register. */
2138 FAIL_IF(emit_fop_mem(compiler, FPU_LOAD, TMP_FREG1, src, srcw));
2141 FAIL_IF(load_immediate(compiler, TMP_REG1, srcw));
2142 FAIL_IF(push_inst(compiler, VMOV | RD(TMP_REG1) | (TMP_FREG1 << 16)));
2145 FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VCVT_F32_S32, op & SLJIT_F32_OP, dst_r, TMP_FREG1, 0)));
2147 if (dst & SLJIT_MEM)
2148 return emit_fop_mem(compiler, (op & SLJIT_F32_OP), TMP_FREG1, dst, dstw);
2149 return SLJIT_SUCCESS;
2152 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_cmp(struct sljit_compiler *compiler, sljit_s32 op,
2153 sljit_s32 src1, sljit_sw src1w,
2154 sljit_s32 src2, sljit_sw src2w)
2156 if (src1 & SLJIT_MEM) {
2157 FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, TMP_FREG1, src1, src1w));
2161 if (src2 & SLJIT_MEM) {
2162 FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, TMP_FREG2, src2, src2w));
2166 FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VCMP_F32, op & SLJIT_F32_OP, src1, src2, 0)));
2167 return push_inst(compiler, VMRS);
2170 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop1(struct sljit_compiler *compiler, sljit_s32 op,
2171 sljit_s32 dst, sljit_sw dstw,
2172 sljit_s32 src, sljit_sw srcw)
2177 compiler->cache_arg = 0;
2178 compiler->cache_argw = 0;
2179 if (GET_OPCODE(op) != SLJIT_CONV_F64_FROM_F32)
2182 SLJIT_COMPILE_ASSERT((SLJIT_F32_OP == 0x100), float_transfer_bit_error);
2183 SELECT_FOP1_OPERATION_WITH_CHECKS(compiler, op, dst, dstw, src, srcw);
2185 dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
2187 if (src & SLJIT_MEM) {
2188 FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, dst_r, src, srcw));
2192 switch (GET_OPCODE(op)) {
2195 if (dst_r != TMP_FREG1)
2196 FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VMOV_F32, op & SLJIT_F32_OP, dst_r, src, 0)));
2202 FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VNEG_F32, op & SLJIT_F32_OP, dst_r, src, 0)));
2205 FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VABS_F32, op & SLJIT_F32_OP, dst_r, src, 0)));
2207 case SLJIT_CONV_F64_FROM_F32:
2208 FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VCVT_F64_F32, op & SLJIT_F32_OP, dst_r, src, 0)));
2213 if (dst & SLJIT_MEM)
2214 return emit_fop_mem(compiler, (op & SLJIT_F32_OP), dst_r, dst, dstw);
2215 return SLJIT_SUCCESS;
2218 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop2(struct sljit_compiler *compiler, sljit_s32 op,
2219 sljit_s32 dst, sljit_sw dstw,
2220 sljit_s32 src1, sljit_sw src1w,
2221 sljit_s32 src2, sljit_sw src2w)
2226 CHECK(check_sljit_emit_fop2(compiler, op, dst, dstw, src1, src1w, src2, src2w));
2227 ADJUST_LOCAL_OFFSET(dst, dstw);
2228 ADJUST_LOCAL_OFFSET(src1, src1w);
2229 ADJUST_LOCAL_OFFSET(src2, src2w);
2231 compiler->cache_arg = 0;
2232 compiler->cache_argw = 0;
2235 dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
2237 if (src2 & SLJIT_MEM) {
2238 FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, TMP_FREG2, src2, src2w));
2242 if (src1 & SLJIT_MEM) {
2243 FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, TMP_FREG1, src1, src1w));
2247 switch (GET_OPCODE(op)) {
2249 FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VADD_F32, op & SLJIT_F32_OP, dst_r, src2, src1)));
2253 FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VSUB_F32, op & SLJIT_F32_OP, dst_r, src2, src1)));
2257 FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VMUL_F32, op & SLJIT_F32_OP, dst_r, src2, src1)));
2261 FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VDIV_F32, op & SLJIT_F32_OP, dst_r, src2, src1)));
2265 if (dst_r == TMP_FREG1)
2266 FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_F32_OP), TMP_FREG1, dst, dstw));
2268 return SLJIT_SUCCESS;
2272 #undef EMIT_FPU_DATA_TRANSFER
2273 #undef EMIT_FPU_OPERATION
2275 /* --------------------------------------------------------------------- */
2276 /* Other instructions */
2277 /* --------------------------------------------------------------------- */
2279 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fast_enter(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw)
2282 CHECK(check_sljit_emit_fast_enter(compiler, dst, dstw));
2283 ADJUST_LOCAL_OFFSET(dst, dstw);
2285 /* For UNUSED dst. Uncommon, but possible. */
2286 if (dst == SLJIT_UNUSED)
2287 return SLJIT_SUCCESS;
2289 if (FAST_IS_REG(dst))
2290 return push_inst(compiler, EMIT_DATA_PROCESS_INS(MOV_DP, 0, dst, SLJIT_UNUSED, RM(TMP_REG3)));
2293 if (getput_arg_fast(compiler, WORD_DATA, TMP_REG3, dst, dstw))
2294 return compiler->error;
2295 /* TMP_REG3 is used for caching. */
2296 FAIL_IF(push_inst(compiler, EMIT_DATA_PROCESS_INS(MOV_DP, 0, TMP_REG2, SLJIT_UNUSED, RM(TMP_REG3))));
2297 compiler->cache_arg = 0;
2298 compiler->cache_argw = 0;
2299 return getput_arg(compiler, WORD_DATA, TMP_REG2, dst, dstw, 0, 0);
2302 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fast_return(struct sljit_compiler *compiler, sljit_s32 src, sljit_sw srcw)
2305 CHECK(check_sljit_emit_fast_return(compiler, src, srcw));
2306 ADJUST_LOCAL_OFFSET(src, srcw);
2308 if (FAST_IS_REG(src))
2309 FAIL_IF(push_inst(compiler, EMIT_DATA_PROCESS_INS(MOV_DP, 0, TMP_REG3, SLJIT_UNUSED, RM(src))));
2310 else if (src & SLJIT_MEM) {
2311 if (getput_arg_fast(compiler, WORD_DATA | LOAD_DATA, TMP_REG3, src, srcw))
2312 FAIL_IF(compiler->error);
2314 compiler->cache_arg = 0;
2315 compiler->cache_argw = 0;
2316 FAIL_IF(getput_arg(compiler, WORD_DATA | LOAD_DATA, TMP_REG2, src, srcw, 0, 0));
2317 FAIL_IF(push_inst(compiler, EMIT_DATA_PROCESS_INS(MOV_DP, 0, TMP_REG3, SLJIT_UNUSED, RM(TMP_REG2))));
2320 else if (src & SLJIT_IMM)
2321 FAIL_IF(load_immediate(compiler, TMP_REG3, srcw));
2322 return push_inst(compiler, BLX | RM(TMP_REG3));
2325 /* --------------------------------------------------------------------- */
2326 /* Conditional instructions */
2327 /* --------------------------------------------------------------------- */
2329 static sljit_uw get_cc(sljit_s32 type)
2333 case SLJIT_MUL_NOT_OVERFLOW:
2334 case SLJIT_EQUAL_F64:
2337 case SLJIT_NOT_EQUAL:
2338 case SLJIT_MUL_OVERFLOW:
2339 case SLJIT_NOT_EQUAL_F64:
2343 case SLJIT_LESS_F64:
2346 case SLJIT_GREATER_EQUAL:
2347 case SLJIT_GREATER_EQUAL_F64:
2351 case SLJIT_GREATER_F64:
2354 case SLJIT_LESS_EQUAL:
2355 case SLJIT_LESS_EQUAL_F64:
2358 case SLJIT_SIG_LESS:
2361 case SLJIT_SIG_GREATER_EQUAL:
2364 case SLJIT_SIG_GREATER:
2367 case SLJIT_SIG_LESS_EQUAL:
2370 case SLJIT_OVERFLOW:
2371 case SLJIT_UNORDERED_F64:
2374 case SLJIT_NOT_OVERFLOW:
2375 case SLJIT_ORDERED_F64:
2379 SLJIT_ASSERT(type >= SLJIT_JUMP && type <= SLJIT_CALL3);
2384 SLJIT_API_FUNC_ATTRIBUTE struct sljit_label* sljit_emit_label(struct sljit_compiler *compiler)
2386 struct sljit_label *label;
2389 CHECK_PTR(check_sljit_emit_label(compiler));
2391 if (compiler->last_label && compiler->last_label->size == compiler->size)
2392 return compiler->last_label;
2394 label = (struct sljit_label*)ensure_abuf(compiler, sizeof(struct sljit_label));
2395 PTR_FAIL_IF(!label);
2396 set_label(label, compiler);
2400 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_jump(struct sljit_compiler *compiler, sljit_s32 type)
2402 struct sljit_jump *jump;
2405 CHECK_PTR(check_sljit_emit_jump(compiler, type));
2407 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
2409 set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
2412 /* In ARM, we don't need to touch the arguments. */
2413 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
2414 if (type >= SLJIT_FAST_CALL)
2415 PTR_FAIL_IF(prepare_blx(compiler));
2416 PTR_FAIL_IF(push_inst_with_unique_literal(compiler, ((EMIT_DATA_TRANSFER(WORD_DATA | LOAD_DATA, 1, 0,
2417 type <= SLJIT_JUMP ? TMP_PC : TMP_REG1, TMP_PC, 0)) & ~COND_MASK) | get_cc(type), 0));
2419 if (jump->flags & SLJIT_REWRITABLE_JUMP) {
2420 jump->addr = compiler->size;
2421 compiler->patches++;
2424 if (type >= SLJIT_FAST_CALL) {
2425 jump->flags |= IS_BL;
2426 PTR_FAIL_IF(emit_blx(compiler));
2429 if (!(jump->flags & SLJIT_REWRITABLE_JUMP))
2430 jump->addr = compiler->size;
2432 if (type >= SLJIT_FAST_CALL)
2433 jump->flags |= IS_BL;
2434 PTR_FAIL_IF(emit_imm(compiler, TMP_REG1, 0));
2435 PTR_FAIL_IF(push_inst(compiler, (((type <= SLJIT_JUMP ? BX : BLX) | RM(TMP_REG1)) & ~COND_MASK) | get_cc(type)));
2436 jump->addr = compiler->size;
2441 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_ijump(struct sljit_compiler *compiler, sljit_s32 type, sljit_s32 src, sljit_sw srcw)
2443 struct sljit_jump *jump;
2446 CHECK(check_sljit_emit_ijump(compiler, type, src, srcw));
2447 ADJUST_LOCAL_OFFSET(src, srcw);
2449 /* In ARM, we don't need to touch the arguments. */
2450 if (!(src & SLJIT_IMM)) {
2451 if (FAST_IS_REG(src))
2452 return push_inst(compiler, (type <= SLJIT_JUMP ? BX : BLX) | RM(src));
2454 SLJIT_ASSERT(src & SLJIT_MEM);
2455 FAIL_IF(emit_op_mem(compiler, WORD_DATA | LOAD_DATA, TMP_REG2, src, srcw));
2456 return push_inst(compiler, (type <= SLJIT_JUMP ? BX : BLX) | RM(TMP_REG2));
2459 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
2461 set_jump(jump, compiler, JUMP_ADDR | ((type >= SLJIT_FAST_CALL) ? IS_BL : 0));
2462 jump->u.target = srcw;
2464 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
2465 if (type >= SLJIT_FAST_CALL)
2466 FAIL_IF(prepare_blx(compiler));
2467 FAIL_IF(push_inst_with_unique_literal(compiler, EMIT_DATA_TRANSFER(WORD_DATA | LOAD_DATA, 1, 0, type <= SLJIT_JUMP ? TMP_PC : TMP_REG1, TMP_PC, 0), 0));
2468 if (type >= SLJIT_FAST_CALL)
2469 FAIL_IF(emit_blx(compiler));
2471 FAIL_IF(emit_imm(compiler, TMP_REG1, 0));
2472 FAIL_IF(push_inst(compiler, (type <= SLJIT_JUMP ? BX : BLX) | RM(TMP_REG1)));
2474 jump->addr = compiler->size;
2475 return SLJIT_SUCCESS;
2478 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_flags(struct sljit_compiler *compiler, sljit_s32 op,
2479 sljit_s32 dst, sljit_sw dstw,
2480 sljit_s32 src, sljit_sw srcw,
2483 sljit_s32 dst_r, flags = GET_ALL_FLAGS(op);
2487 CHECK(check_sljit_emit_op_flags(compiler, op, dst, dstw, src, srcw, type));
2488 ADJUST_LOCAL_OFFSET(dst, dstw);
2489 ADJUST_LOCAL_OFFSET(src, srcw);
2491 if (dst == SLJIT_UNUSED)
2492 return SLJIT_SUCCESS;
2494 op = GET_OPCODE(op);
2495 cc = get_cc(type & 0xff);
2496 dst_r = FAST_IS_REG(dst) ? dst : TMP_REG2;
2498 if (op < SLJIT_ADD) {
2499 FAIL_IF(push_inst(compiler, EMIT_DATA_PROCESS_INS(MOV_DP, 0, dst_r, SLJIT_UNUSED, SRC2_IMM | 0)));
2500 FAIL_IF(push_inst(compiler, (EMIT_DATA_PROCESS_INS(MOV_DP, 0, dst_r, SLJIT_UNUSED, SRC2_IMM | 1) & ~COND_MASK) | cc));
2501 return (dst_r == TMP_REG2) ? emit_op_mem(compiler, WORD_DATA, TMP_REG2, dst, dstw) : SLJIT_SUCCESS;
2504 ins = (op == SLJIT_AND ? AND_DP : (op == SLJIT_OR ? ORR_DP : EOR_DP));
2505 if ((op == SLJIT_OR || op == SLJIT_XOR) && FAST_IS_REG(dst) && dst == src) {
2506 FAIL_IF(push_inst(compiler, (EMIT_DATA_PROCESS_INS(ins, 0, dst, dst, SRC2_IMM | 1) & ~COND_MASK) | cc));
2507 /* The condition must always be set, even if the ORR/EOR is not executed above. */
2508 return (flags & SLJIT_SET_E) ? push_inst(compiler, EMIT_DATA_PROCESS_INS(MOV_DP, SET_FLAGS, TMP_REG1, SLJIT_UNUSED, RM(dst))) : SLJIT_SUCCESS;
2511 compiler->cache_arg = 0;
2512 compiler->cache_argw = 0;
2513 if (src & SLJIT_MEM) {
2514 FAIL_IF(emit_op_mem2(compiler, WORD_DATA | LOAD_DATA, TMP_REG1, src, srcw, dst, dstw));
2517 } else if (src & SLJIT_IMM) {
2518 FAIL_IF(load_immediate(compiler, TMP_REG1, srcw));
2523 FAIL_IF(push_inst(compiler, (EMIT_DATA_PROCESS_INS(ins, 0, dst_r, src, SRC2_IMM | 1) & ~COND_MASK) | cc));
2524 FAIL_IF(push_inst(compiler, (EMIT_DATA_PROCESS_INS(ins, 0, dst_r, src, SRC2_IMM | 0) & ~COND_MASK) | (cc ^ 0x10000000)));
2525 if (dst_r == TMP_REG2)
2526 FAIL_IF(emit_op_mem2(compiler, WORD_DATA, TMP_REG2, dst, dstw, 0, 0));
2528 return (flags & SLJIT_SET_E) ? push_inst(compiler, EMIT_DATA_PROCESS_INS(MOV_DP, SET_FLAGS, TMP_REG1, SLJIT_UNUSED, RM(dst_r))) : SLJIT_SUCCESS;
2531 SLJIT_API_FUNC_ATTRIBUTE struct sljit_const* sljit_emit_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw, sljit_sw init_value)
2533 struct sljit_const *const_;
2537 CHECK_PTR(check_sljit_emit_const(compiler, dst, dstw, init_value));
2538 ADJUST_LOCAL_OFFSET(dst, dstw);
2540 const_ = (struct sljit_const*)ensure_abuf(compiler, sizeof(struct sljit_const));
2541 PTR_FAIL_IF(!const_);
2543 reg = SLOW_IS_REG(dst) ? dst : TMP_REG2;
2545 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
2546 PTR_FAIL_IF(push_inst_with_unique_literal(compiler, EMIT_DATA_TRANSFER(WORD_DATA | LOAD_DATA, 1, 0, reg, TMP_PC, 0), init_value));
2547 compiler->patches++;
2549 PTR_FAIL_IF(emit_imm(compiler, reg, init_value));
2551 set_const(const_, compiler);
2553 if (dst & SLJIT_MEM)
2554 PTR_FAIL_IF(emit_op_mem(compiler, WORD_DATA, TMP_REG2, dst, dstw));
2558 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_addr)
2560 inline_set_jump_addr(addr, new_addr, 1);
2563 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_const(sljit_uw addr, sljit_sw new_constant)
2565 inline_set_const(addr, new_constant, 1);