Auto detect s390x and change const declarations to static.
[sljit.git] / sljit_src / sljitNativeARM_32.c
blobae8479f0314d95aa9b2c49af32ad987d7d67a734
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) (reg_map[rm])
72 #define RD(rd) (reg_map[rd] << 12)
73 #define RN(rn) (reg_map[rn] << 16)
75 /* --------------------------------------------------------------------- */
76 /* Instrucion forms */
77 /* --------------------------------------------------------------------- */
79 /* The instruction includes the AL condition.
80 INST_NAME - CONDITIONAL remove this flag. */
81 #define COND_MASK 0xf0000000
82 #define CONDITIONAL 0xe0000000
83 #define PUSH_POOL 0xff000000
85 #define ADC 0xe0a00000
86 #define ADD 0xe0800000
87 #define AND 0xe0000000
88 #define B 0xea000000
89 #define BIC 0xe1c00000
90 #define BL 0xeb000000
91 #define BLX 0xe12fff30
92 #define BX 0xe12fff10
93 #define CLZ 0xe16f0f10
94 #define CMN 0xe1600000
95 #define CMP 0xe1400000
96 #define BKPT 0xe1200070
97 #define EOR 0xe0200000
98 #define MOV 0xe1a00000
99 #define MUL 0xe0000090
100 #define MVN 0xe1e00000
101 #define NOP 0xe1a00000
102 #define ORR 0xe1800000
103 #define PUSH 0xe92d0000
104 #define POP 0xe8bd0000
105 #define RSB 0xe0600000
106 #define RSC 0xe0e00000
107 #define SBC 0xe0c00000
108 #define SMULL 0xe0c00090
109 #define SUB 0xe0400000
110 #define UMULL 0xe0800090
111 #define VABS_F32 0xeeb00ac0
112 #define VADD_F32 0xee300a00
113 #define VCMP_F32 0xeeb40a40
114 #define VCVT_F32_S32 0xeeb80ac0
115 #define VCVT_F64_F32 0xeeb70ac0
116 #define VCVT_S32_F32 0xeebd0ac0
117 #define VDIV_F32 0xee800a00
118 #define VMOV_F32 0xeeb00a40
119 #define VMOV 0xee000a10
120 #define VMOV2 0xec400a10
121 #define VMRS 0xeef1fa10
122 #define VMUL_F32 0xee200a00
123 #define VNEG_F32 0xeeb10a40
124 #define VSTR_F32 0xed000a00
125 #define VSUB_F32 0xee300a40
127 #if (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
128 /* Arm v7 specific instructions. */
129 #define MOVW 0xe3000000
130 #define MOVT 0xe3400000
131 #define SXTB 0xe6af0070
132 #define SXTH 0xe6bf0070
133 #define UXTB 0xe6ef0070
134 #define UXTH 0xe6ff0070
135 #endif
137 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
139 static sljit_s32 push_cpool(struct sljit_compiler *compiler)
141 /* Pushing the constant pool into the instruction stream. */
142 sljit_uw* inst;
143 sljit_uw* cpool_ptr;
144 sljit_uw* cpool_end;
145 sljit_s32 i;
147 /* The label could point the address after the constant pool. */
148 if (compiler->last_label && compiler->last_label->size == compiler->size)
149 compiler->last_label->size += compiler->cpool_fill + (CONST_POOL_ALIGNMENT - 1) + 1;
151 SLJIT_ASSERT(compiler->cpool_fill > 0 && compiler->cpool_fill <= CPOOL_SIZE);
152 inst = (sljit_uw*)ensure_buf(compiler, sizeof(sljit_uw));
153 FAIL_IF(!inst);
154 compiler->size++;
155 *inst = 0xff000000 | compiler->cpool_fill;
157 for (i = 0; i < CONST_POOL_ALIGNMENT - 1; i++) {
158 inst = (sljit_uw*)ensure_buf(compiler, sizeof(sljit_uw));
159 FAIL_IF(!inst);
160 compiler->size++;
161 *inst = 0;
164 cpool_ptr = compiler->cpool;
165 cpool_end = cpool_ptr + compiler->cpool_fill;
166 while (cpool_ptr < cpool_end) {
167 inst = (sljit_uw*)ensure_buf(compiler, sizeof(sljit_uw));
168 FAIL_IF(!inst);
169 compiler->size++;
170 *inst = *cpool_ptr++;
172 compiler->cpool_diff = CONST_POOL_EMPTY;
173 compiler->cpool_fill = 0;
174 return SLJIT_SUCCESS;
177 static sljit_s32 push_inst(struct sljit_compiler *compiler, sljit_uw inst)
179 sljit_uw* ptr;
181 if (SLJIT_UNLIKELY(compiler->cpool_diff != CONST_POOL_EMPTY && compiler->size - compiler->cpool_diff >= MAX_DIFFERENCE(4092)))
182 FAIL_IF(push_cpool(compiler));
184 ptr = (sljit_uw*)ensure_buf(compiler, sizeof(sljit_uw));
185 FAIL_IF(!ptr);
186 compiler->size++;
187 *ptr = inst;
188 return SLJIT_SUCCESS;
191 static sljit_s32 push_inst_with_literal(struct sljit_compiler *compiler, sljit_uw inst, sljit_uw literal)
193 sljit_uw* ptr;
194 sljit_uw cpool_index = CPOOL_SIZE;
195 sljit_uw* cpool_ptr;
196 sljit_uw* cpool_end;
197 sljit_u8* cpool_unique_ptr;
199 if (SLJIT_UNLIKELY(compiler->cpool_diff != CONST_POOL_EMPTY && compiler->size - compiler->cpool_diff >= MAX_DIFFERENCE(4092)))
200 FAIL_IF(push_cpool(compiler));
201 else if (compiler->cpool_fill > 0) {
202 cpool_ptr = compiler->cpool;
203 cpool_end = cpool_ptr + compiler->cpool_fill;
204 cpool_unique_ptr = compiler->cpool_unique;
205 do {
206 if ((*cpool_ptr == literal) && !(*cpool_unique_ptr)) {
207 cpool_index = cpool_ptr - compiler->cpool;
208 break;
210 cpool_ptr++;
211 cpool_unique_ptr++;
212 } while (cpool_ptr < cpool_end);
215 if (cpool_index == CPOOL_SIZE) {
216 /* Must allocate a new entry in the literal pool. */
217 if (compiler->cpool_fill < CPOOL_SIZE) {
218 cpool_index = compiler->cpool_fill;
219 compiler->cpool_fill++;
221 else {
222 FAIL_IF(push_cpool(compiler));
223 cpool_index = 0;
224 compiler->cpool_fill = 1;
228 SLJIT_ASSERT((inst & 0xfff) == 0);
229 ptr = (sljit_uw*)ensure_buf(compiler, sizeof(sljit_uw));
230 FAIL_IF(!ptr);
231 compiler->size++;
232 *ptr = inst | cpool_index;
234 compiler->cpool[cpool_index] = literal;
235 compiler->cpool_unique[cpool_index] = 0;
236 if (compiler->cpool_diff == CONST_POOL_EMPTY)
237 compiler->cpool_diff = compiler->size;
238 return SLJIT_SUCCESS;
241 static sljit_s32 push_inst_with_unique_literal(struct sljit_compiler *compiler, sljit_uw inst, sljit_uw literal)
243 sljit_uw* ptr;
244 if (SLJIT_UNLIKELY((compiler->cpool_diff != CONST_POOL_EMPTY && compiler->size - compiler->cpool_diff >= MAX_DIFFERENCE(4092)) || compiler->cpool_fill >= CPOOL_SIZE))
245 FAIL_IF(push_cpool(compiler));
247 SLJIT_ASSERT(compiler->cpool_fill < CPOOL_SIZE && (inst & 0xfff) == 0);
248 ptr = (sljit_uw*)ensure_buf(compiler, sizeof(sljit_uw));
249 FAIL_IF(!ptr);
250 compiler->size++;
251 *ptr = inst | compiler->cpool_fill;
253 compiler->cpool[compiler->cpool_fill] = literal;
254 compiler->cpool_unique[compiler->cpool_fill] = 1;
255 compiler->cpool_fill++;
256 if (compiler->cpool_diff == CONST_POOL_EMPTY)
257 compiler->cpool_diff = compiler->size;
258 return SLJIT_SUCCESS;
261 static SLJIT_INLINE sljit_s32 prepare_blx(struct sljit_compiler *compiler)
263 /* Place for at least two instruction (doesn't matter whether the first has a literal). */
264 if (SLJIT_UNLIKELY(compiler->cpool_diff != CONST_POOL_EMPTY && compiler->size - compiler->cpool_diff >= MAX_DIFFERENCE(4088)))
265 return push_cpool(compiler);
266 return SLJIT_SUCCESS;
269 static SLJIT_INLINE sljit_s32 emit_blx(struct sljit_compiler *compiler)
271 /* Must follow tightly the previous instruction (to be able to convert it to bl instruction). */
272 SLJIT_ASSERT(compiler->cpool_diff == CONST_POOL_EMPTY || compiler->size - compiler->cpool_diff < MAX_DIFFERENCE(4092));
273 SLJIT_ASSERT(reg_map[TMP_REG1] != 14);
275 return push_inst(compiler, BLX | RM(TMP_REG1));
278 static sljit_uw patch_pc_relative_loads(sljit_uw *last_pc_patch, sljit_uw *code_ptr, sljit_uw* const_pool, sljit_uw cpool_size)
280 sljit_uw diff;
281 sljit_uw ind;
282 sljit_uw counter = 0;
283 sljit_uw* clear_const_pool = const_pool;
284 sljit_uw* clear_const_pool_end = const_pool + cpool_size;
286 SLJIT_ASSERT(const_pool - code_ptr <= CONST_POOL_ALIGNMENT);
287 /* Set unused flag for all literals in the constant pool.
288 I.e.: unused literals can belong to branches, which can be encoded as B or BL.
289 We can "compress" the constant pool by discarding these literals. */
290 while (clear_const_pool < clear_const_pool_end)
291 *clear_const_pool++ = (sljit_uw)(-1);
293 while (last_pc_patch < code_ptr) {
294 /* Data transfer instruction with Rn == r15. */
295 if ((*last_pc_patch & 0x0c0f0000) == 0x040f0000) {
296 diff = const_pool - last_pc_patch;
297 ind = (*last_pc_patch) & 0xfff;
299 /* Must be a load instruction with immediate offset. */
300 SLJIT_ASSERT(ind < cpool_size && !(*last_pc_patch & (1 << 25)) && (*last_pc_patch & (1 << 20)));
301 if ((sljit_s32)const_pool[ind] < 0) {
302 const_pool[ind] = counter;
303 ind = counter;
304 counter++;
306 else
307 ind = const_pool[ind];
309 SLJIT_ASSERT(diff >= 1);
310 if (diff >= 2 || ind > 0) {
311 diff = (diff + ind - 2) << 2;
312 SLJIT_ASSERT(diff <= 0xfff);
313 *last_pc_patch = (*last_pc_patch & ~0xfff) | diff;
315 else
316 *last_pc_patch = (*last_pc_patch & ~(0xfff | (1 << 23))) | 0x004;
318 last_pc_patch++;
320 return counter;
323 /* In some rare ocasions we may need future patches. The probability is close to 0 in practice. */
324 struct future_patch {
325 struct future_patch* next;
326 sljit_s32 index;
327 sljit_s32 value;
330 static sljit_s32 resolve_const_pool_index(struct sljit_compiler *compiler, struct future_patch **first_patch, sljit_uw cpool_current_index, sljit_uw *cpool_start_address, sljit_uw *buf_ptr)
332 sljit_s32 value;
333 struct future_patch *curr_patch, *prev_patch;
335 SLJIT_UNUSED_ARG(compiler);
337 /* Using the values generated by patch_pc_relative_loads. */
338 if (!*first_patch)
339 value = (sljit_s32)cpool_start_address[cpool_current_index];
340 else {
341 curr_patch = *first_patch;
342 prev_patch = NULL;
343 while (1) {
344 if (!curr_patch) {
345 value = (sljit_s32)cpool_start_address[cpool_current_index];
346 break;
348 if ((sljit_uw)curr_patch->index == cpool_current_index) {
349 value = curr_patch->value;
350 if (prev_patch)
351 prev_patch->next = curr_patch->next;
352 else
353 *first_patch = curr_patch->next;
354 SLJIT_FREE(curr_patch, compiler->allocator_data);
355 break;
357 prev_patch = curr_patch;
358 curr_patch = curr_patch->next;
362 if (value >= 0) {
363 if ((sljit_uw)value > cpool_current_index) {
364 curr_patch = (struct future_patch*)SLJIT_MALLOC(sizeof(struct future_patch), compiler->allocator_data);
365 if (!curr_patch) {
366 while (*first_patch) {
367 curr_patch = *first_patch;
368 *first_patch = (*first_patch)->next;
369 SLJIT_FREE(curr_patch, compiler->allocator_data);
371 return SLJIT_ERR_ALLOC_FAILED;
373 curr_patch->next = *first_patch;
374 curr_patch->index = value;
375 curr_patch->value = cpool_start_address[value];
376 *first_patch = curr_patch;
378 cpool_start_address[value] = *buf_ptr;
380 return SLJIT_SUCCESS;
383 #else
385 static sljit_s32 push_inst(struct sljit_compiler *compiler, sljit_uw inst)
387 sljit_uw* ptr;
389 ptr = (sljit_uw*)ensure_buf(compiler, sizeof(sljit_uw));
390 FAIL_IF(!ptr);
391 compiler->size++;
392 *ptr = inst;
393 return SLJIT_SUCCESS;
396 static SLJIT_INLINE sljit_s32 emit_imm(struct sljit_compiler *compiler, sljit_s32 reg, sljit_sw imm)
398 FAIL_IF(push_inst(compiler, MOVW | RD(reg) | ((imm << 4) & 0xf0000) | (imm & 0xfff)));
399 return push_inst(compiler, MOVT | RD(reg) | ((imm >> 12) & 0xf0000) | ((imm >> 16) & 0xfff));
402 #endif
404 static SLJIT_INLINE sljit_s32 detect_jump_type(struct sljit_jump *jump, sljit_uw *code_ptr, sljit_uw *code, sljit_sw executable_offset)
406 sljit_sw diff;
408 if (jump->flags & SLJIT_REWRITABLE_JUMP)
409 return 0;
411 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
412 if (jump->flags & IS_BL)
413 code_ptr--;
415 if (jump->flags & JUMP_ADDR)
416 diff = ((sljit_sw)jump->u.target - (sljit_sw)(code_ptr + 2) - executable_offset);
417 else {
418 SLJIT_ASSERT(jump->flags & JUMP_LABEL);
419 diff = ((sljit_sw)(code + jump->u.label->size) - (sljit_sw)(code_ptr + 2));
422 /* Branch to Thumb code has not been optimized yet. */
423 if (diff & 0x3)
424 return 0;
426 if (jump->flags & IS_BL) {
427 if (diff <= 0x01ffffff && diff >= -0x02000000) {
428 *code_ptr = (BL - CONDITIONAL) | (*(code_ptr + 1) & COND_MASK);
429 jump->flags |= PATCH_B;
430 return 1;
433 else {
434 if (diff <= 0x01ffffff && diff >= -0x02000000) {
435 *code_ptr = (B - CONDITIONAL) | (*code_ptr & COND_MASK);
436 jump->flags |= PATCH_B;
439 #else
440 if (jump->flags & JUMP_ADDR)
441 diff = ((sljit_sw)jump->u.target - (sljit_sw)code_ptr - executable_offset);
442 else {
443 SLJIT_ASSERT(jump->flags & JUMP_LABEL);
444 diff = ((sljit_sw)(code + jump->u.label->size) - (sljit_sw)code_ptr);
447 /* Branch to Thumb code has not been optimized yet. */
448 if (diff & 0x3)
449 return 0;
451 if (diff <= 0x01ffffff && diff >= -0x02000000) {
452 code_ptr -= 2;
453 *code_ptr = ((jump->flags & IS_BL) ? (BL - CONDITIONAL) : (B - CONDITIONAL)) | (code_ptr[2] & COND_MASK);
454 jump->flags |= PATCH_B;
455 return 1;
457 #endif
458 return 0;
461 static SLJIT_INLINE void inline_set_jump_addr(sljit_uw jump_ptr, sljit_sw executable_offset, sljit_uw new_addr, sljit_s32 flush_cache)
463 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
464 sljit_uw *ptr = (sljit_uw *)jump_ptr;
465 sljit_uw *inst = (sljit_uw *)ptr[0];
466 sljit_uw mov_pc = ptr[1];
467 sljit_s32 bl = (mov_pc & 0x0000f000) != RD(TMP_PC);
468 sljit_sw diff = (sljit_sw)(((sljit_sw)new_addr - (sljit_sw)(inst + 2) - executable_offset) >> 2);
470 SLJIT_UNUSED_ARG(executable_offset);
472 if (diff <= 0x7fffff && diff >= -0x800000) {
473 /* Turn to branch. */
474 if (!bl) {
475 if (flush_cache) {
476 SLJIT_UPDATE_WX_FLAGS(inst, inst + 1, 0);
478 inst[0] = (mov_pc & COND_MASK) | (B - CONDITIONAL) | (diff & 0xffffff);
479 if (flush_cache) {
480 SLJIT_UPDATE_WX_FLAGS(inst, inst + 1, 1);
481 inst = (sljit_uw *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
482 SLJIT_CACHE_FLUSH(inst, inst + 1);
484 } else {
485 if (flush_cache) {
486 SLJIT_UPDATE_WX_FLAGS(inst, inst + 2, 0);
488 inst[0] = (mov_pc & COND_MASK) | (BL - CONDITIONAL) | (diff & 0xffffff);
489 inst[1] = NOP;
490 if (flush_cache) {
491 SLJIT_UPDATE_WX_FLAGS(inst, inst + 2, 1);
492 inst = (sljit_uw *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
493 SLJIT_CACHE_FLUSH(inst, inst + 2);
496 } else {
497 /* Get the position of the constant. */
498 if (mov_pc & (1 << 23))
499 ptr = inst + ((mov_pc & 0xfff) >> 2) + 2;
500 else
501 ptr = inst + 1;
503 if (*inst != mov_pc) {
504 if (flush_cache) {
505 SLJIT_UPDATE_WX_FLAGS(inst, inst + (!bl ? 1 : 2), 0);
507 inst[0] = mov_pc;
508 if (!bl) {
509 if (flush_cache) {
510 SLJIT_UPDATE_WX_FLAGS(inst, inst + 1, 1);
511 inst = (sljit_uw *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
512 SLJIT_CACHE_FLUSH(inst, inst + 1);
514 } else {
515 inst[1] = BLX | RM(TMP_REG1);
516 if (flush_cache) {
517 SLJIT_UPDATE_WX_FLAGS(inst, inst + 2, 1);
518 inst = (sljit_uw *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
519 SLJIT_CACHE_FLUSH(inst, inst + 2);
524 if (flush_cache) {
525 SLJIT_UPDATE_WX_FLAGS(ptr, ptr + 1, 0);
528 *ptr = new_addr;
530 if (flush_cache) {
531 SLJIT_UPDATE_WX_FLAGS(ptr, ptr + 1, 1);
534 #else
535 sljit_uw *inst = (sljit_uw*)jump_ptr;
537 SLJIT_UNUSED_ARG(executable_offset);
539 SLJIT_ASSERT((inst[0] & 0xfff00000) == MOVW && (inst[1] & 0xfff00000) == MOVT);
541 if (flush_cache) {
542 SLJIT_UPDATE_WX_FLAGS(inst, inst + 2, 0);
545 inst[0] = MOVW | (inst[0] & 0xf000) | ((new_addr << 4) & 0xf0000) | (new_addr & 0xfff);
546 inst[1] = MOVT | (inst[1] & 0xf000) | ((new_addr >> 12) & 0xf0000) | ((new_addr >> 16) & 0xfff);
548 if (flush_cache) {
549 SLJIT_UPDATE_WX_FLAGS(inst, inst + 2, 1);
550 inst = (sljit_uw *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
551 SLJIT_CACHE_FLUSH(inst, inst + 2);
553 #endif
556 static sljit_uw get_imm(sljit_uw imm);
558 static SLJIT_INLINE void inline_set_const(sljit_uw addr, sljit_sw executable_offset, sljit_sw new_constant, sljit_s32 flush_cache)
560 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
561 sljit_uw *ptr = (sljit_uw*)addr;
562 sljit_uw *inst = (sljit_uw*)ptr[0];
563 sljit_uw ldr_literal = ptr[1];
564 sljit_uw src2;
566 SLJIT_UNUSED_ARG(executable_offset);
568 src2 = get_imm(new_constant);
569 if (src2) {
570 if (flush_cache) {
571 SLJIT_UPDATE_WX_FLAGS(inst, inst + 1, 0);
574 *inst = 0xe3a00000 | (ldr_literal & 0xf000) | src2;
576 if (flush_cache) {
577 SLJIT_UPDATE_WX_FLAGS(inst, inst + 1, 1);
578 inst = (sljit_uw *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
579 SLJIT_CACHE_FLUSH(inst, inst + 1);
581 return;
584 src2 = get_imm(~new_constant);
585 if (src2) {
586 if (flush_cache) {
587 SLJIT_UPDATE_WX_FLAGS(inst, inst + 1, 0);
590 *inst = 0xe3e00000 | (ldr_literal & 0xf000) | src2;
592 if (flush_cache) {
593 SLJIT_UPDATE_WX_FLAGS(inst, inst + 1, 1);
594 inst = (sljit_uw *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
595 SLJIT_CACHE_FLUSH(inst, inst + 1);
597 return;
600 if (ldr_literal & (1 << 23))
601 ptr = inst + ((ldr_literal & 0xfff) >> 2) + 2;
602 else
603 ptr = inst + 1;
605 if (*inst != ldr_literal) {
606 if (flush_cache) {
607 SLJIT_UPDATE_WX_FLAGS(inst, inst + 1, 0);
610 *inst = ldr_literal;
612 if (flush_cache) {
613 SLJIT_UPDATE_WX_FLAGS(inst, inst + 1, 1);
614 inst = (sljit_uw *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
615 SLJIT_CACHE_FLUSH(inst, inst + 1);
619 if (flush_cache) {
620 SLJIT_UPDATE_WX_FLAGS(ptr, ptr + 1, 0);
623 *ptr = new_constant;
625 if (flush_cache) {
626 SLJIT_UPDATE_WX_FLAGS(ptr, ptr + 1, 1);
628 #else
629 sljit_uw *inst = (sljit_uw*)addr;
631 SLJIT_UNUSED_ARG(executable_offset);
633 SLJIT_ASSERT((inst[0] & 0xfff00000) == MOVW && (inst[1] & 0xfff00000) == MOVT);
635 if (flush_cache) {
636 SLJIT_UPDATE_WX_FLAGS(inst, inst + 2, 0);
639 inst[0] = MOVW | (inst[0] & 0xf000) | ((new_constant << 4) & 0xf0000) | (new_constant & 0xfff);
640 inst[1] = MOVT | (inst[1] & 0xf000) | ((new_constant >> 12) & 0xf0000) | ((new_constant >> 16) & 0xfff);
642 if (flush_cache) {
643 SLJIT_UPDATE_WX_FLAGS(inst, inst + 2, 1);
644 inst = (sljit_uw *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
645 SLJIT_CACHE_FLUSH(inst, inst + 2);
647 #endif
650 SLJIT_API_FUNC_ATTRIBUTE void* sljit_generate_code(struct sljit_compiler *compiler)
652 struct sljit_memory_fragment *buf;
653 sljit_uw *code;
654 sljit_uw *code_ptr;
655 sljit_uw *buf_ptr;
656 sljit_uw *buf_end;
657 sljit_uw size;
658 sljit_uw word_count;
659 sljit_uw next_addr;
660 sljit_sw executable_offset;
661 sljit_sw addr;
662 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
663 sljit_uw cpool_size;
664 sljit_uw cpool_skip_alignment;
665 sljit_uw cpool_current_index;
666 sljit_uw *cpool_start_address;
667 sljit_uw *last_pc_patch;
668 struct future_patch *first_patch;
669 #endif
671 struct sljit_label *label;
672 struct sljit_jump *jump;
673 struct sljit_const *const_;
674 struct sljit_put_label *put_label;
676 CHECK_ERROR_PTR();
677 CHECK_PTR(check_sljit_generate_code(compiler));
678 reverse_buf(compiler);
680 /* Second code generation pass. */
681 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
682 size = compiler->size + (compiler->patches << 1);
683 if (compiler->cpool_fill > 0)
684 size += compiler->cpool_fill + CONST_POOL_ALIGNMENT - 1;
685 #else
686 size = compiler->size;
687 #endif
688 code = (sljit_uw*)SLJIT_MALLOC_EXEC(size * sizeof(sljit_uw), compiler->exec_allocator_data);
689 PTR_FAIL_WITH_EXEC_IF(code);
690 buf = compiler->buf;
692 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
693 cpool_size = 0;
694 cpool_skip_alignment = 0;
695 cpool_current_index = 0;
696 cpool_start_address = NULL;
697 first_patch = NULL;
698 last_pc_patch = code;
699 #endif
701 code_ptr = code;
702 word_count = 0;
703 next_addr = 1;
704 executable_offset = SLJIT_EXEC_OFFSET(code);
706 label = compiler->labels;
707 jump = compiler->jumps;
708 const_ = compiler->consts;
709 put_label = compiler->put_labels;
711 if (label && label->size == 0) {
712 label->addr = (sljit_uw)SLJIT_ADD_EXEC_OFFSET(code, executable_offset);
713 label = label->next;
716 do {
717 buf_ptr = (sljit_uw*)buf->memory;
718 buf_end = buf_ptr + (buf->used_size >> 2);
719 do {
720 word_count++;
721 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
722 if (cpool_size > 0) {
723 if (cpool_skip_alignment > 0) {
724 buf_ptr++;
725 cpool_skip_alignment--;
727 else {
728 if (SLJIT_UNLIKELY(resolve_const_pool_index(compiler, &first_patch, cpool_current_index, cpool_start_address, buf_ptr))) {
729 SLJIT_FREE_EXEC(code, compiler->exec_allocator_data);
730 compiler->error = SLJIT_ERR_ALLOC_FAILED;
731 return NULL;
733 buf_ptr++;
734 if (++cpool_current_index >= cpool_size) {
735 SLJIT_ASSERT(!first_patch);
736 cpool_size = 0;
737 if (label && label->size == word_count) {
738 /* Points after the current instruction. */
739 label->addr = (sljit_uw)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset);
740 label->size = code_ptr - code;
741 label = label->next;
743 next_addr = compute_next_addr(label, jump, const_, put_label);
748 else if ((*buf_ptr & 0xff000000) != PUSH_POOL) {
749 #endif
750 *code_ptr = *buf_ptr++;
751 if (next_addr == word_count) {
752 SLJIT_ASSERT(!label || label->size >= word_count);
753 SLJIT_ASSERT(!jump || jump->addr >= word_count);
754 SLJIT_ASSERT(!const_ || const_->addr >= word_count);
755 SLJIT_ASSERT(!put_label || put_label->addr >= word_count);
757 /* These structures are ordered by their address. */
758 if (jump && jump->addr == word_count) {
759 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
760 if (detect_jump_type(jump, code_ptr, code, executable_offset))
761 code_ptr--;
762 jump->addr = (sljit_uw)code_ptr;
763 #else
764 jump->addr = (sljit_uw)(code_ptr - 2);
765 if (detect_jump_type(jump, code_ptr, code, executable_offset))
766 code_ptr -= 2;
767 #endif
768 jump = jump->next;
770 if (label && label->size == word_count) {
771 /* code_ptr can be affected above. */
772 label->addr = (sljit_uw)SLJIT_ADD_EXEC_OFFSET(code_ptr + 1, executable_offset);
773 label->size = (code_ptr + 1) - code;
774 label = label->next;
776 if (const_ && const_->addr == word_count) {
777 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
778 const_->addr = (sljit_uw)code_ptr;
779 #else
780 const_->addr = (sljit_uw)(code_ptr - 1);
781 #endif
782 const_ = const_->next;
784 if (put_label && put_label->addr == word_count) {
785 SLJIT_ASSERT(put_label->label);
786 put_label->addr = (sljit_uw)code_ptr;
787 put_label = put_label->next;
789 next_addr = compute_next_addr(label, jump, const_, put_label);
791 code_ptr++;
792 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
794 else {
795 /* Fortunately, no need to shift. */
796 cpool_size = *buf_ptr++ & ~PUSH_POOL;
797 SLJIT_ASSERT(cpool_size > 0);
798 cpool_start_address = ALIGN_INSTRUCTION(code_ptr + 1);
799 cpool_current_index = patch_pc_relative_loads(last_pc_patch, code_ptr, cpool_start_address, cpool_size);
800 if (cpool_current_index > 0) {
801 /* Unconditional branch. */
802 *code_ptr = B | (((cpool_start_address - code_ptr) + cpool_current_index - 2) & ~PUSH_POOL);
803 code_ptr = cpool_start_address + cpool_current_index;
805 cpool_skip_alignment = CONST_POOL_ALIGNMENT - 1;
806 cpool_current_index = 0;
807 last_pc_patch = code_ptr;
809 #endif
810 } while (buf_ptr < buf_end);
811 buf = buf->next;
812 } while (buf);
814 SLJIT_ASSERT(!label);
815 SLJIT_ASSERT(!jump);
816 SLJIT_ASSERT(!const_);
817 SLJIT_ASSERT(!put_label);
819 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
820 SLJIT_ASSERT(cpool_size == 0);
821 if (compiler->cpool_fill > 0) {
822 cpool_start_address = ALIGN_INSTRUCTION(code_ptr);
823 cpool_current_index = patch_pc_relative_loads(last_pc_patch, code_ptr, cpool_start_address, compiler->cpool_fill);
824 if (cpool_current_index > 0)
825 code_ptr = cpool_start_address + cpool_current_index;
827 buf_ptr = compiler->cpool;
828 buf_end = buf_ptr + compiler->cpool_fill;
829 cpool_current_index = 0;
830 while (buf_ptr < buf_end) {
831 if (SLJIT_UNLIKELY(resolve_const_pool_index(compiler, &first_patch, cpool_current_index, cpool_start_address, buf_ptr))) {
832 SLJIT_FREE_EXEC(code, compiler->exec_allocator_data);
833 compiler->error = SLJIT_ERR_ALLOC_FAILED;
834 return NULL;
836 buf_ptr++;
837 cpool_current_index++;
839 SLJIT_ASSERT(!first_patch);
841 #endif
843 jump = compiler->jumps;
844 while (jump) {
845 buf_ptr = (sljit_uw *)jump->addr;
847 if (jump->flags & PATCH_B) {
848 addr = (sljit_sw)SLJIT_ADD_EXEC_OFFSET(buf_ptr + 2, executable_offset);
849 if (!(jump->flags & JUMP_ADDR)) {
850 SLJIT_ASSERT(jump->flags & JUMP_LABEL);
851 SLJIT_ASSERT(((sljit_sw)jump->u.label->addr - addr) <= 0x01ffffff && ((sljit_sw)jump->u.label->addr - addr) >= -0x02000000);
852 *buf_ptr |= (((sljit_sw)jump->u.label->addr - addr) >> 2) & 0x00ffffff;
854 else {
855 SLJIT_ASSERT(((sljit_sw)jump->u.target - addr) <= 0x01ffffff && ((sljit_sw)jump->u.target - addr) >= -0x02000000);
856 *buf_ptr |= (((sljit_sw)jump->u.target - addr) >> 2) & 0x00ffffff;
859 else if (jump->flags & SLJIT_REWRITABLE_JUMP) {
860 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
861 jump->addr = (sljit_uw)code_ptr;
862 code_ptr[0] = (sljit_uw)buf_ptr;
863 code_ptr[1] = *buf_ptr;
864 inline_set_jump_addr((sljit_uw)code_ptr, executable_offset, (jump->flags & JUMP_LABEL) ? jump->u.label->addr : jump->u.target, 0);
865 code_ptr += 2;
866 #else
867 inline_set_jump_addr((sljit_uw)buf_ptr, executable_offset, (jump->flags & JUMP_LABEL) ? jump->u.label->addr : jump->u.target, 0);
868 #endif
870 else {
871 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
872 if (jump->flags & IS_BL)
873 buf_ptr--;
874 if (*buf_ptr & (1 << 23))
875 buf_ptr += ((*buf_ptr & 0xfff) >> 2) + 2;
876 else
877 buf_ptr += 1;
878 *buf_ptr = (jump->flags & JUMP_LABEL) ? jump->u.label->addr : jump->u.target;
879 #else
880 inline_set_jump_addr((sljit_uw)buf_ptr, executable_offset, (jump->flags & JUMP_LABEL) ? jump->u.label->addr : jump->u.target, 0);
881 #endif
883 jump = jump->next;
886 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
887 const_ = compiler->consts;
888 while (const_) {
889 buf_ptr = (sljit_uw*)const_->addr;
890 const_->addr = (sljit_uw)code_ptr;
892 code_ptr[0] = (sljit_uw)buf_ptr;
893 code_ptr[1] = *buf_ptr;
894 if (*buf_ptr & (1 << 23))
895 buf_ptr += ((*buf_ptr & 0xfff) >> 2) + 2;
896 else
897 buf_ptr += 1;
898 /* Set the value again (can be a simple constant). */
899 inline_set_const((sljit_uw)code_ptr, executable_offset, *buf_ptr, 0);
900 code_ptr += 2;
902 const_ = const_->next;
904 #endif
906 put_label = compiler->put_labels;
907 while (put_label) {
908 addr = put_label->label->addr;
909 buf_ptr = (sljit_uw*)put_label->addr;
911 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
912 SLJIT_ASSERT((buf_ptr[0] & 0xffff0000) == 0xe59f0000);
913 buf_ptr[((buf_ptr[0] & 0xfff) >> 2) + 2] = addr;
914 #else
915 SLJIT_ASSERT((buf_ptr[-1] & 0xfff00000) == MOVW && (buf_ptr[0] & 0xfff00000) == MOVT);
916 buf_ptr[-1] |= ((addr << 4) & 0xf0000) | (addr & 0xfff);
917 buf_ptr[0] |= ((addr >> 12) & 0xf0000) | ((addr >> 16) & 0xfff);
918 #endif
919 put_label = put_label->next;
922 SLJIT_ASSERT(code_ptr - code <= (sljit_s32)size);
924 compiler->error = SLJIT_ERR_COMPILED;
925 compiler->executable_offset = executable_offset;
926 compiler->executable_size = (code_ptr - code) * sizeof(sljit_uw);
928 code = (sljit_uw *)SLJIT_ADD_EXEC_OFFSET(code, executable_offset);
929 code_ptr = (sljit_uw *)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset);
931 SLJIT_CACHE_FLUSH(code, code_ptr);
932 SLJIT_UPDATE_WX_FLAGS(code, code_ptr, 1);
933 return code;
936 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_has_cpu_feature(sljit_s32 feature_type)
938 switch (feature_type) {
939 case SLJIT_HAS_FPU:
940 #ifdef SLJIT_IS_FPU_AVAILABLE
941 return SLJIT_IS_FPU_AVAILABLE;
942 #else
943 /* Available by default. */
944 return 1;
945 #endif
947 case SLJIT_HAS_CLZ:
948 case SLJIT_HAS_CMOV:
949 #if (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
950 case SLJIT_HAS_PREFETCH:
951 #endif
952 return 1;
954 default:
955 return 0;
959 /* --------------------------------------------------------------------- */
960 /* Entry, exit */
961 /* --------------------------------------------------------------------- */
963 /* Creates an index in data_transfer_insts array. */
964 #define WORD_SIZE 0x00
965 #define BYTE_SIZE 0x01
966 #define HALF_SIZE 0x02
967 #define PRELOAD 0x03
968 #define SIGNED 0x04
969 #define LOAD_DATA 0x08
971 /* Flag bits for emit_op. */
972 #define ALLOW_IMM 0x10
973 #define ALLOW_INV_IMM 0x20
974 #define ALLOW_ANY_IMM (ALLOW_IMM | ALLOW_INV_IMM)
976 /* s/l - store/load (1 bit)
977 u/s - signed/unsigned (1 bit)
978 w/b/h/N - word/byte/half/NOT allowed (2 bit)
979 Storing signed and unsigned values are the same operations. */
981 static const sljit_uw data_transfer_insts[16] = {
982 /* s u w */ 0xe5000000 /* str */,
983 /* s u b */ 0xe5400000 /* strb */,
984 /* s u h */ 0xe10000b0 /* strh */,
985 /* s u N */ 0x00000000 /* not allowed */,
986 /* s s w */ 0xe5000000 /* str */,
987 /* s s b */ 0xe5400000 /* strb */,
988 /* s s h */ 0xe10000b0 /* strh */,
989 /* s s N */ 0x00000000 /* not allowed */,
991 /* l u w */ 0xe5100000 /* ldr */,
992 /* l u b */ 0xe5500000 /* ldrb */,
993 /* l u h */ 0xe11000b0 /* ldrh */,
994 /* l u p */ 0xf5500000 /* preload */,
995 /* l s w */ 0xe5100000 /* ldr */,
996 /* l s b */ 0xe11000d0 /* ldrsb */,
997 /* l s h */ 0xe11000f0 /* ldrsh */,
998 /* l s N */ 0x00000000 /* not allowed */,
1001 #define EMIT_DATA_TRANSFER(type, add, target_reg, base_reg, arg) \
1002 (data_transfer_insts[(type) & 0xf] | ((add) << 23) | RD(target_reg) | RN(base_reg) | (arg))
1004 /* Normal ldr/str instruction.
1005 Type2: ldrsb, ldrh, ldrsh */
1006 #define IS_TYPE1_TRANSFER(type) \
1007 (data_transfer_insts[(type) & 0xf] & 0x04000000)
1008 #define TYPE2_TRANSFER_IMM(imm) \
1009 (((imm) & 0xf) | (((imm) & 0xf0) << 4) | (1 << 22))
1011 static sljit_s32 emit_op(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 inp_flags,
1012 sljit_s32 dst, sljit_sw dstw,
1013 sljit_s32 src1, sljit_sw src1w,
1014 sljit_s32 src2, sljit_sw src2w);
1016 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_enter(struct sljit_compiler *compiler,
1017 sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
1018 sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
1020 sljit_s32 args, size, i, tmp;
1021 sljit_uw push;
1023 CHECK_ERROR();
1024 CHECK(check_sljit_emit_enter(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size));
1025 set_emit_enter(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size);
1027 /* Push saved registers, temporary registers
1028 stmdb sp!, {..., lr} */
1029 push = PUSH | (1 << 14);
1031 tmp = saveds < SLJIT_NUMBER_OF_SAVED_REGISTERS ? (SLJIT_S0 + 1 - saveds) : SLJIT_FIRST_SAVED_REG;
1032 for (i = SLJIT_S0; i >= tmp; i--)
1033 push |= 1 << reg_map[i];
1035 for (i = scratches; i >= SLJIT_FIRST_SAVED_REG; i--)
1036 push |= 1 << reg_map[i];
1038 FAIL_IF(push_inst(compiler, push));
1040 /* Stack must be aligned to 8 bytes: */
1041 size = GET_SAVED_REGISTERS_SIZE(scratches, saveds, 1);
1042 local_size = ((size + local_size + 7) & ~7) - size;
1043 compiler->local_size = local_size;
1044 if (local_size > 0)
1045 FAIL_IF(emit_op(compiler, SLJIT_SUB, ALLOW_IMM, SLJIT_SP, 0, SLJIT_SP, 0, SLJIT_IMM, local_size));
1047 args = get_arg_count(arg_types);
1049 if (args >= 1)
1050 FAIL_IF(push_inst(compiler, MOV | RD(SLJIT_S0) | RM(SLJIT_R0)));
1051 if (args >= 2)
1052 FAIL_IF(push_inst(compiler, MOV | RD(SLJIT_S1) | RM(SLJIT_R1)));
1053 if (args >= 3)
1054 FAIL_IF(push_inst(compiler, MOV | RD(SLJIT_S2) | RM(SLJIT_R2)));
1056 return SLJIT_SUCCESS;
1059 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_set_context(struct sljit_compiler *compiler,
1060 sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
1061 sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
1063 sljit_s32 size;
1065 CHECK_ERROR();
1066 CHECK(check_sljit_set_context(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size));
1067 set_set_context(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size);
1069 size = GET_SAVED_REGISTERS_SIZE(scratches, saveds, 1);
1070 compiler->local_size = ((size + local_size + 7) & ~7) - size;
1071 return SLJIT_SUCCESS;
1074 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_return(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 src, sljit_sw srcw)
1076 sljit_s32 i, tmp;
1077 sljit_uw pop;
1079 CHECK_ERROR();
1080 CHECK(check_sljit_emit_return(compiler, op, src, srcw));
1082 FAIL_IF(emit_mov_before_return(compiler, op, src, srcw));
1084 if (compiler->local_size > 0)
1085 FAIL_IF(emit_op(compiler, SLJIT_ADD, ALLOW_IMM, SLJIT_SP, 0, SLJIT_SP, 0, SLJIT_IMM, compiler->local_size));
1087 /* Push saved registers, temporary registers
1088 ldmia sp!, {..., pc} */
1089 pop = POP | (1 << 15);
1091 tmp = compiler->saveds < SLJIT_NUMBER_OF_SAVED_REGISTERS ? (SLJIT_S0 + 1 - compiler->saveds) : SLJIT_FIRST_SAVED_REG;
1092 for (i = SLJIT_S0; i >= tmp; i--)
1093 pop |= 1 << reg_map[i];
1095 for (i = compiler->scratches; i >= SLJIT_FIRST_SAVED_REG; i--)
1096 pop |= 1 << reg_map[i];
1098 return push_inst(compiler, pop);
1101 /* --------------------------------------------------------------------- */
1102 /* Operators */
1103 /* --------------------------------------------------------------------- */
1105 /* flags: */
1106 /* Arguments are swapped. */
1107 #define ARGS_SWAPPED 0x01
1108 /* Inverted immediate. */
1109 #define INV_IMM 0x02
1110 /* Source and destination is register. */
1111 #define MOVE_REG_CONV 0x04
1112 /* Unused return value. */
1113 #define UNUSED_RETURN 0x08
1114 /* SET_FLAGS must be (1 << 20) as it is also the value of S bit (can be used for optimization). */
1115 #define SET_FLAGS (1 << 20)
1116 /* dst: reg
1117 src1: reg
1118 src2: reg or imm (if allowed)
1119 SRC2_IMM must be (1 << 25) as it is also the value of I bit (can be used for optimization). */
1120 #define SRC2_IMM (1 << 25)
1122 #define EMIT_SHIFT_INS_AND_RETURN(opcode) \
1123 SLJIT_ASSERT(!(flags & INV_IMM) && !(src2 & SRC2_IMM)); \
1124 if (compiler->shift_imm != 0x20) { \
1125 SLJIT_ASSERT(src1 == TMP_REG1); \
1126 SLJIT_ASSERT(!(flags & ARGS_SWAPPED)); \
1128 if (compiler->shift_imm != 0) \
1129 return push_inst(compiler, MOV | (flags & SET_FLAGS) | \
1130 RD(dst) | (compiler->shift_imm << 7) | (opcode << 5) | RM(src2)); \
1131 return push_inst(compiler, MOV | (flags & SET_FLAGS) | RD(dst) | RM(src2)); \
1133 return push_inst(compiler, MOV | (flags & SET_FLAGS) | RD(dst) | \
1134 (reg_map[(flags & ARGS_SWAPPED) ? src1 : src2] << 8) | (opcode << 5) | 0x10 | RM((flags & ARGS_SWAPPED) ? src2 : src1));
1136 static SLJIT_INLINE sljit_s32 emit_single_op(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 flags,
1137 sljit_s32 dst, sljit_s32 src1, sljit_s32 src2)
1139 switch (GET_OPCODE(op)) {
1140 case SLJIT_MOV:
1141 SLJIT_ASSERT(src1 == TMP_REG1 && !(flags & ARGS_SWAPPED));
1142 if (dst != src2) {
1143 if (src2 & SRC2_IMM) {
1144 return push_inst(compiler, ((flags & INV_IMM) ? MVN : MOV) | RD(dst) | src2);
1146 return push_inst(compiler, MOV | RD(dst) | RM(src2));
1148 return SLJIT_SUCCESS;
1150 case SLJIT_MOV_U8:
1151 case SLJIT_MOV_S8:
1152 SLJIT_ASSERT(src1 == TMP_REG1 && !(flags & ARGS_SWAPPED));
1153 if (flags & MOVE_REG_CONV) {
1154 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
1155 if (op == SLJIT_MOV_U8)
1156 return push_inst(compiler, AND | RD(dst) | RN(src2) | SRC2_IMM | 0xff);
1157 FAIL_IF(push_inst(compiler, MOV | RD(dst) | (24 << 7) | RM(src2)));
1158 return push_inst(compiler, MOV | RD(dst) | (24 << 7) | (op == SLJIT_MOV_U8 ? 0x20 : 0x40) | RM(dst));
1159 #else
1160 return push_inst(compiler, (op == SLJIT_MOV_U8 ? UXTB : SXTB) | RD(dst) | RM(src2));
1161 #endif
1163 else if (dst != src2) {
1164 SLJIT_ASSERT(src2 & SRC2_IMM);
1165 return push_inst(compiler, ((flags & INV_IMM) ? MVN : MOV) | RD(dst) | src2);
1167 return SLJIT_SUCCESS;
1169 case SLJIT_MOV_U16:
1170 case SLJIT_MOV_S16:
1171 SLJIT_ASSERT(src1 == TMP_REG1 && !(flags & ARGS_SWAPPED));
1172 if (flags & MOVE_REG_CONV) {
1173 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
1174 FAIL_IF(push_inst(compiler, MOV | RD(dst) | (16 << 7) | RM(src2)));
1175 return push_inst(compiler, MOV | RD(dst) | (16 << 7) | (op == SLJIT_MOV_U16 ? 0x20 : 0x40) | RM(dst));
1176 #else
1177 return push_inst(compiler, (op == SLJIT_MOV_U16 ? UXTH : SXTH) | RD(dst) | RM(src2));
1178 #endif
1180 else if (dst != src2) {
1181 SLJIT_ASSERT(src2 & SRC2_IMM);
1182 return push_inst(compiler, ((flags & INV_IMM) ? MVN : MOV) | RD(dst) | src2);
1184 return SLJIT_SUCCESS;
1186 case SLJIT_NOT:
1187 if (src2 & SRC2_IMM) {
1188 return push_inst(compiler, ((flags & INV_IMM) ? MOV : MVN) | (flags & SET_FLAGS) | RD(dst) | src2);
1190 return push_inst(compiler, MVN | (flags & SET_FLAGS) | RD(dst) | RM(src2));
1192 case SLJIT_CLZ:
1193 SLJIT_ASSERT(!(flags & INV_IMM));
1194 SLJIT_ASSERT(!(src2 & SRC2_IMM));
1195 FAIL_IF(push_inst(compiler, CLZ | RD(dst) | RM(src2)));
1196 return SLJIT_SUCCESS;
1198 case SLJIT_ADD:
1199 SLJIT_ASSERT(!(flags & INV_IMM));
1200 if ((flags & (UNUSED_RETURN | SET_FLAGS)) == (UNUSED_RETURN | SET_FLAGS) && !(flags & ARGS_SWAPPED))
1201 return push_inst(compiler, CMN | SET_FLAGS | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
1202 return push_inst(compiler, ADD | (flags & SET_FLAGS) | RD(dst) | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
1204 case SLJIT_ADDC:
1205 SLJIT_ASSERT(!(flags & INV_IMM));
1206 return push_inst(compiler, ADC | (flags & SET_FLAGS) | RD(dst) | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
1208 case SLJIT_SUB:
1209 SLJIT_ASSERT(!(flags & INV_IMM));
1210 if ((flags & (UNUSED_RETURN | SET_FLAGS)) == (UNUSED_RETURN | SET_FLAGS) && !(flags & ARGS_SWAPPED))
1211 return push_inst(compiler, CMP | SET_FLAGS | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
1212 return push_inst(compiler, (!(flags & ARGS_SWAPPED) ? SUB : RSB) | (flags & SET_FLAGS)
1213 | RD(dst) | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
1215 case SLJIT_SUBC:
1216 SLJIT_ASSERT(!(flags & INV_IMM));
1217 return push_inst(compiler, (!(flags & ARGS_SWAPPED) ? SBC : RSC) | (flags & SET_FLAGS)
1218 | RD(dst) | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
1220 case SLJIT_MUL:
1221 SLJIT_ASSERT(!(flags & INV_IMM));
1222 SLJIT_ASSERT(!(src2 & SRC2_IMM));
1224 if (!HAS_FLAGS(op))
1225 return push_inst(compiler, MUL | (reg_map[dst] << 16) | (reg_map[src2] << 8) | reg_map[src1]);
1227 FAIL_IF(push_inst(compiler, SMULL | (reg_map[TMP_REG1] << 16) | (reg_map[dst] << 12) | (reg_map[src2] << 8) | reg_map[src1]));
1229 /* cmp TMP_REG1, dst asr #31. */
1230 return push_inst(compiler, CMP | SET_FLAGS | RN(TMP_REG1) | RM(dst) | 0xfc0);
1232 case SLJIT_AND:
1233 return push_inst(compiler, (!(flags & INV_IMM) ? AND : BIC) | (flags & SET_FLAGS)
1234 | RD(dst) | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
1236 case SLJIT_OR:
1237 SLJIT_ASSERT(!(flags & INV_IMM));
1238 return push_inst(compiler, ORR | (flags & SET_FLAGS) | RD(dst) | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
1240 case SLJIT_XOR:
1241 SLJIT_ASSERT(!(flags & INV_IMM));
1242 return push_inst(compiler, EOR | (flags & SET_FLAGS) | RD(dst) | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
1244 case SLJIT_SHL:
1245 EMIT_SHIFT_INS_AND_RETURN(0);
1247 case SLJIT_LSHR:
1248 EMIT_SHIFT_INS_AND_RETURN(1);
1250 case SLJIT_ASHR:
1251 EMIT_SHIFT_INS_AND_RETURN(2);
1254 SLJIT_UNREACHABLE();
1255 return SLJIT_SUCCESS;
1258 #undef EMIT_SHIFT_INS_AND_RETURN
1260 /* Tests whether the immediate can be stored in the 12 bit imm field.
1261 Returns with 0 if not possible. */
1262 static sljit_uw get_imm(sljit_uw imm)
1264 sljit_s32 rol;
1266 if (imm <= 0xff)
1267 return SRC2_IMM | imm;
1269 if (!(imm & 0xff000000)) {
1270 imm <<= 8;
1271 rol = 8;
1273 else {
1274 imm = (imm << 24) | (imm >> 8);
1275 rol = 0;
1278 if (!(imm & 0xff000000)) {
1279 imm <<= 8;
1280 rol += 4;
1283 if (!(imm & 0xf0000000)) {
1284 imm <<= 4;
1285 rol += 2;
1288 if (!(imm & 0xc0000000)) {
1289 imm <<= 2;
1290 rol += 1;
1293 if (!(imm & 0x00ffffff))
1294 return SRC2_IMM | (imm >> 24) | (rol << 8);
1295 else
1296 return 0;
1299 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
1300 static sljit_s32 generate_int(struct sljit_compiler *compiler, sljit_s32 reg, sljit_uw imm, sljit_s32 positive)
1302 sljit_uw mask;
1303 sljit_uw imm1;
1304 sljit_uw imm2;
1305 sljit_s32 rol;
1307 /* Step1: Search a zero byte (8 continous zero bit). */
1308 mask = 0xff000000;
1309 rol = 8;
1310 while(1) {
1311 if (!(imm & mask)) {
1312 /* Rol imm by rol. */
1313 imm = (imm << rol) | (imm >> (32 - rol));
1314 /* Calculate arm rol. */
1315 rol = 4 + (rol >> 1);
1316 break;
1318 rol += 2;
1319 mask >>= 2;
1320 if (mask & 0x3) {
1321 /* rol by 8. */
1322 imm = (imm << 8) | (imm >> 24);
1323 mask = 0xff00;
1324 rol = 24;
1325 while (1) {
1326 if (!(imm & mask)) {
1327 /* Rol imm by rol. */
1328 imm = (imm << rol) | (imm >> (32 - rol));
1329 /* Calculate arm rol. */
1330 rol = (rol >> 1) - 8;
1331 break;
1333 rol += 2;
1334 mask >>= 2;
1335 if (mask & 0x3)
1336 return 0;
1338 break;
1342 /* The low 8 bit must be zero. */
1343 SLJIT_ASSERT(!(imm & 0xff));
1345 if (!(imm & 0xff000000)) {
1346 imm1 = SRC2_IMM | ((imm >> 16) & 0xff) | (((rol + 4) & 0xf) << 8);
1347 imm2 = SRC2_IMM | ((imm >> 8) & 0xff) | (((rol + 8) & 0xf) << 8);
1349 else if (imm & 0xc0000000) {
1350 imm1 = SRC2_IMM | ((imm >> 24) & 0xff) | ((rol & 0xf) << 8);
1351 imm <<= 8;
1352 rol += 4;
1354 if (!(imm & 0xff000000)) {
1355 imm <<= 8;
1356 rol += 4;
1359 if (!(imm & 0xf0000000)) {
1360 imm <<= 4;
1361 rol += 2;
1364 if (!(imm & 0xc0000000)) {
1365 imm <<= 2;
1366 rol += 1;
1369 if (!(imm & 0x00ffffff))
1370 imm2 = SRC2_IMM | (imm >> 24) | ((rol & 0xf) << 8);
1371 else
1372 return 0;
1374 else {
1375 if (!(imm & 0xf0000000)) {
1376 imm <<= 4;
1377 rol += 2;
1380 if (!(imm & 0xc0000000)) {
1381 imm <<= 2;
1382 rol += 1;
1385 imm1 = SRC2_IMM | ((imm >> 24) & 0xff) | ((rol & 0xf) << 8);
1386 imm <<= 8;
1387 rol += 4;
1389 if (!(imm & 0xf0000000)) {
1390 imm <<= 4;
1391 rol += 2;
1394 if (!(imm & 0xc0000000)) {
1395 imm <<= 2;
1396 rol += 1;
1399 if (!(imm & 0x00ffffff))
1400 imm2 = SRC2_IMM | (imm >> 24) | ((rol & 0xf) << 8);
1401 else
1402 return 0;
1405 FAIL_IF(push_inst(compiler, (positive ? MOV : MVN) | RD(reg) | imm1));
1406 FAIL_IF(push_inst(compiler, (positive ? ORR : BIC) | RD(reg) | RN(reg) | imm2));
1407 return 1;
1409 #endif
1411 static sljit_s32 load_immediate(struct sljit_compiler *compiler, sljit_s32 reg, sljit_uw imm)
1413 sljit_uw tmp;
1415 #if (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
1416 if (!(imm & ~0xffff))
1417 return push_inst(compiler, MOVW | RD(reg) | ((imm << 4) & 0xf0000) | (imm & 0xfff));
1418 #endif
1420 /* Create imm by 1 inst. */
1421 tmp = get_imm(imm);
1422 if (tmp)
1423 return push_inst(compiler, MOV | RD(reg) | tmp);
1425 tmp = get_imm(~imm);
1426 if (tmp)
1427 return push_inst(compiler, MVN | RD(reg) | tmp);
1429 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
1430 /* Create imm by 2 inst. */
1431 FAIL_IF(generate_int(compiler, reg, imm, 1));
1432 FAIL_IF(generate_int(compiler, reg, ~imm, 0));
1434 /* Load integer. */
1435 return push_inst_with_literal(compiler, EMIT_DATA_TRANSFER(WORD_SIZE | LOAD_DATA, 1, reg, TMP_PC, 0), imm);
1436 #else
1437 FAIL_IF(push_inst(compiler, MOVW | RD(reg) | ((imm << 4) & 0xf0000) | (imm & 0xfff)));
1438 if (imm <= 0xffff)
1439 return SLJIT_SUCCESS;
1440 return push_inst(compiler, MOVT | RD(reg) | ((imm >> 12) & 0xf0000) | ((imm >> 16) & 0xfff));
1441 #endif
1444 static SLJIT_INLINE sljit_s32 emit_op_mem(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg,
1445 sljit_s32 arg, sljit_sw argw, sljit_s32 tmp_reg)
1447 sljit_uw imm, offset_reg;
1448 sljit_uw is_type1_transfer = IS_TYPE1_TRANSFER(flags);
1450 SLJIT_ASSERT (arg & SLJIT_MEM);
1451 SLJIT_ASSERT((arg & REG_MASK) != tmp_reg);
1453 if ((arg & REG_MASK) == SLJIT_UNUSED) {
1454 if (is_type1_transfer) {
1455 FAIL_IF(load_immediate(compiler, tmp_reg, argw & ~0xfff));
1456 argw &= 0xfff;
1458 else {
1459 FAIL_IF(load_immediate(compiler, tmp_reg, argw & ~0xff));
1460 argw &= 0xff;
1463 return push_inst(compiler, EMIT_DATA_TRANSFER(flags, 1, reg, tmp_reg,
1464 is_type1_transfer ? argw : TYPE2_TRANSFER_IMM(argw)));
1467 if (arg & OFFS_REG_MASK) {
1468 offset_reg = OFFS_REG(arg);
1469 arg &= REG_MASK;
1470 argw &= 0x3;
1472 if (argw != 0 && !is_type1_transfer) {
1473 FAIL_IF(push_inst(compiler, ADD | RD(tmp_reg) | RN(arg) | RM(offset_reg) | (argw << 7)));
1474 return push_inst(compiler, EMIT_DATA_TRANSFER(flags, 1, reg, tmp_reg, TYPE2_TRANSFER_IMM(0)));
1477 /* Bit 25: RM is offset. */
1478 return push_inst(compiler, EMIT_DATA_TRANSFER(flags, 1, reg, arg,
1479 RM(offset_reg) | (is_type1_transfer ? (1 << 25) : 0) | (argw << 7)));
1482 arg &= REG_MASK;
1484 if (is_type1_transfer) {
1485 if (argw > 0xfff) {
1486 imm = get_imm(argw & ~0xfff);
1487 if (imm) {
1488 FAIL_IF(push_inst(compiler, ADD | RD(tmp_reg) | RN(arg) | imm));
1489 argw = argw & 0xfff;
1490 arg = tmp_reg;
1493 else if (argw < -0xfff) {
1494 imm = get_imm(-argw & ~0xfff);
1495 if (imm) {
1496 FAIL_IF(push_inst(compiler, SUB | RD(tmp_reg) | RN(arg) | imm));
1497 argw = -(-argw & 0xfff);
1498 arg = tmp_reg;
1502 if (argw >= 0 && argw <= 0xfff)
1503 return push_inst(compiler, EMIT_DATA_TRANSFER(flags, 1, reg, arg, argw));
1505 if (argw < 0 && argw >= -0xfff)
1506 return push_inst(compiler, EMIT_DATA_TRANSFER(flags, 0, reg, arg, -argw));
1508 else {
1509 if (argw > 0xff) {
1510 imm = get_imm(argw & ~0xff);
1511 if (imm) {
1512 FAIL_IF(push_inst(compiler, ADD | RD(tmp_reg) | RN(arg) | imm));
1513 argw = argw & 0xff;
1514 arg = tmp_reg;
1517 else if (argw < -0xff) {
1518 imm = get_imm(-argw & ~0xff);
1519 if (imm) {
1520 FAIL_IF(push_inst(compiler, SUB | RD(tmp_reg) | RN(arg) | imm));
1521 argw = -(-argw & 0xff);
1522 arg = tmp_reg;
1526 if (argw >= 0 && argw <= 0xff)
1527 return push_inst(compiler, EMIT_DATA_TRANSFER(flags, 1, reg, arg, TYPE2_TRANSFER_IMM(argw)));
1529 if (argw < 0 && argw >= -0xff) {
1530 argw = -argw;
1531 return push_inst(compiler, EMIT_DATA_TRANSFER(flags, 0, reg, arg, TYPE2_TRANSFER_IMM(argw)));
1535 FAIL_IF(load_immediate(compiler, tmp_reg, argw));
1536 return push_inst(compiler, EMIT_DATA_TRANSFER(flags, 1, reg, arg,
1537 RM(tmp_reg) | (is_type1_transfer ? (1 << 25) : 0)));
1540 static sljit_s32 emit_op(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 inp_flags,
1541 sljit_s32 dst, sljit_sw dstw,
1542 sljit_s32 src1, sljit_sw src1w,
1543 sljit_s32 src2, sljit_sw src2w)
1545 /* src1 is reg or TMP_REG1
1546 src2 is reg, TMP_REG2, or imm
1547 result goes to TMP_REG2, so put result can use TMP_REG1. */
1549 /* We prefers register and simple consts. */
1550 sljit_s32 dst_reg;
1551 sljit_s32 src1_reg;
1552 sljit_s32 src2_reg;
1553 sljit_s32 flags = HAS_FLAGS(op) ? SET_FLAGS : 0;
1555 /* Destination check. */
1556 if (SLJIT_UNLIKELY(dst == SLJIT_UNUSED))
1557 flags |= UNUSED_RETURN;
1559 SLJIT_ASSERT(!(inp_flags & ALLOW_INV_IMM) || (inp_flags & ALLOW_IMM));
1561 src2_reg = 0;
1563 do {
1564 if (!(inp_flags & ALLOW_IMM))
1565 break;
1567 if (src2 & SLJIT_IMM) {
1568 src2_reg = get_imm(src2w);
1569 if (src2_reg)
1570 break;
1571 if (inp_flags & ALLOW_INV_IMM) {
1572 src2_reg = get_imm(~src2w);
1573 if (src2_reg) {
1574 flags |= INV_IMM;
1575 break;
1578 if (GET_OPCODE(op) == SLJIT_ADD) {
1579 src2_reg = get_imm(-src2w);
1580 if (src2_reg) {
1581 op = SLJIT_SUB | GET_ALL_FLAGS(op);
1582 break;
1585 if (GET_OPCODE(op) == SLJIT_SUB) {
1586 src2_reg = get_imm(-src2w);
1587 if (src2_reg) {
1588 op = SLJIT_ADD | GET_ALL_FLAGS(op);
1589 break;
1594 if (src1 & SLJIT_IMM) {
1595 src2_reg = get_imm(src1w);
1596 if (src2_reg) {
1597 flags |= ARGS_SWAPPED;
1598 src1 = src2;
1599 src1w = src2w;
1600 break;
1602 if (inp_flags & ALLOW_INV_IMM) {
1603 src2_reg = get_imm(~src1w);
1604 if (src2_reg) {
1605 flags |= ARGS_SWAPPED | INV_IMM;
1606 src1 = src2;
1607 src1w = src2w;
1608 break;
1611 if (GET_OPCODE(op) == SLJIT_ADD) {
1612 src2_reg = get_imm(-src1w);
1613 if (src2_reg) {
1614 /* Note: add is commutative operation. */
1615 src1 = src2;
1616 src1w = src2w;
1617 op = SLJIT_SUB | GET_ALL_FLAGS(op);
1618 break;
1622 } while(0);
1624 /* Source 1. */
1625 if (FAST_IS_REG(src1))
1626 src1_reg = src1;
1627 else if (src1 & SLJIT_MEM) {
1628 FAIL_IF(emit_op_mem(compiler, inp_flags | LOAD_DATA, TMP_REG1, src1, src1w, TMP_REG1));
1629 src1_reg = TMP_REG1;
1631 else {
1632 FAIL_IF(load_immediate(compiler, TMP_REG1, src1w));
1633 src1_reg = TMP_REG1;
1636 /* Destination. */
1637 dst_reg = SLOW_IS_REG(dst) ? dst : TMP_REG2;
1639 if (op <= SLJIT_MOV_P) {
1640 if (dst & SLJIT_MEM) {
1641 if (inp_flags & BYTE_SIZE)
1642 inp_flags &= ~SIGNED;
1644 if (FAST_IS_REG(src2))
1645 return emit_op_mem(compiler, inp_flags, src2, dst, dstw, TMP_REG2);
1648 if (FAST_IS_REG(src2) && dst_reg != TMP_REG2)
1649 flags |= MOVE_REG_CONV;
1652 /* Source 2. */
1653 if (src2_reg == 0) {
1654 src2_reg = (op <= SLJIT_MOV_P) ? dst_reg : TMP_REG2;
1656 if (FAST_IS_REG(src2))
1657 src2_reg = src2;
1658 else if (src2 & SLJIT_MEM)
1659 FAIL_IF(emit_op_mem(compiler, inp_flags | LOAD_DATA, src2_reg, src2, src2w, TMP_REG2));
1660 else
1661 FAIL_IF(load_immediate(compiler, src2_reg, src2w));
1664 FAIL_IF(emit_single_op(compiler, op, flags, dst_reg, src1_reg, src2_reg));
1666 if (!(dst & SLJIT_MEM))
1667 return SLJIT_SUCCESS;
1669 return emit_op_mem(compiler, inp_flags, dst_reg, dst, dstw, TMP_REG1);
1672 #ifdef __cplusplus
1673 extern "C" {
1674 #endif
1676 #if defined(__GNUC__)
1677 extern unsigned int __aeabi_uidivmod(unsigned int numerator, unsigned int denominator);
1678 extern int __aeabi_idivmod(int numerator, int denominator);
1679 #else
1680 #error "Software divmod functions are needed"
1681 #endif
1683 #ifdef __cplusplus
1685 #endif
1687 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op0(struct sljit_compiler *compiler, sljit_s32 op)
1689 sljit_sw saved_reg_list[3];
1690 sljit_sw saved_reg_count;
1692 CHECK_ERROR();
1693 CHECK(check_sljit_emit_op0(compiler, op));
1695 op = GET_OPCODE(op);
1696 switch (op) {
1697 case SLJIT_BREAKPOINT:
1698 FAIL_IF(push_inst(compiler, BKPT));
1699 break;
1700 case SLJIT_NOP:
1701 FAIL_IF(push_inst(compiler, NOP));
1702 break;
1703 case SLJIT_LMUL_UW:
1704 case SLJIT_LMUL_SW:
1705 return push_inst(compiler, (op == SLJIT_LMUL_UW ? UMULL : SMULL)
1706 | (reg_map[SLJIT_R1] << 16)
1707 | (reg_map[SLJIT_R0] << 12)
1708 | (reg_map[SLJIT_R0] << 8)
1709 | reg_map[SLJIT_R1]);
1710 case SLJIT_DIVMOD_UW:
1711 case SLJIT_DIVMOD_SW:
1712 case SLJIT_DIV_UW:
1713 case SLJIT_DIV_SW:
1714 SLJIT_COMPILE_ASSERT((SLJIT_DIVMOD_UW & 0x2) == 0 && SLJIT_DIV_UW - 0x2 == SLJIT_DIVMOD_UW, bad_div_opcode_assignments);
1715 SLJIT_ASSERT(reg_map[2] == 1 && reg_map[3] == 2 && reg_map[4] == 3);
1717 saved_reg_count = 0;
1718 if (compiler->scratches >= 4)
1719 saved_reg_list[saved_reg_count++] = 3;
1720 if (compiler->scratches >= 3)
1721 saved_reg_list[saved_reg_count++] = 2;
1722 if (op >= SLJIT_DIV_UW)
1723 saved_reg_list[saved_reg_count++] = 1;
1725 if (saved_reg_count > 0) {
1726 FAIL_IF(push_inst(compiler, 0xe52d0000 | (saved_reg_count >= 3 ? 16 : 8)
1727 | (saved_reg_list[0] << 12) /* str rX, [sp, #-8/-16]! */));
1728 if (saved_reg_count >= 2) {
1729 SLJIT_ASSERT(saved_reg_list[1] < 8);
1730 FAIL_IF(push_inst(compiler, 0xe58d0004 | (saved_reg_list[1] << 12) /* str rX, [sp, #4] */));
1732 if (saved_reg_count >= 3) {
1733 SLJIT_ASSERT(saved_reg_list[2] < 8);
1734 FAIL_IF(push_inst(compiler, 0xe58d0008 | (saved_reg_list[2] << 12) /* str rX, [sp, #8] */));
1738 #if defined(__GNUC__)
1739 FAIL_IF(sljit_emit_ijump(compiler, SLJIT_FAST_CALL, SLJIT_IMM,
1740 ((op | 0x2) == SLJIT_DIV_UW ? SLJIT_FUNC_OFFSET(__aeabi_uidivmod) : SLJIT_FUNC_OFFSET(__aeabi_idivmod))));
1741 #else
1742 #error "Software divmod functions are needed"
1743 #endif
1745 if (saved_reg_count > 0) {
1746 if (saved_reg_count >= 3) {
1747 SLJIT_ASSERT(saved_reg_list[2] < 8);
1748 FAIL_IF(push_inst(compiler, 0xe59d0008 | (saved_reg_list[2] << 12) /* ldr rX, [sp, #8] */));
1750 if (saved_reg_count >= 2) {
1751 SLJIT_ASSERT(saved_reg_list[1] < 8);
1752 FAIL_IF(push_inst(compiler, 0xe59d0004 | (saved_reg_list[1] << 12) /* ldr rX, [sp, #4] */));
1754 return push_inst(compiler, 0xe49d0000 | (saved_reg_count >= 3 ? 16 : 8)
1755 | (saved_reg_list[0] << 12) /* ldr rX, [sp], #8/16 */);
1757 return SLJIT_SUCCESS;
1758 case SLJIT_ENDBR:
1759 case SLJIT_SKIP_FRAMES_BEFORE_RETURN:
1760 return SLJIT_SUCCESS;
1763 return SLJIT_SUCCESS;
1766 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op1(struct sljit_compiler *compiler, sljit_s32 op,
1767 sljit_s32 dst, sljit_sw dstw,
1768 sljit_s32 src, sljit_sw srcw)
1770 CHECK_ERROR();
1771 CHECK(check_sljit_emit_op1(compiler, op, dst, dstw, src, srcw));
1772 ADJUST_LOCAL_OFFSET(dst, dstw);
1773 ADJUST_LOCAL_OFFSET(src, srcw);
1775 switch (GET_OPCODE(op)) {
1776 case SLJIT_MOV:
1777 case SLJIT_MOV_U32:
1778 case SLJIT_MOV_S32:
1779 case SLJIT_MOV_P:
1780 return emit_op(compiler, SLJIT_MOV, ALLOW_ANY_IMM, dst, dstw, TMP_REG1, 0, src, srcw);
1782 case SLJIT_MOV_U8:
1783 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);
1785 case SLJIT_MOV_S8:
1786 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);
1788 case SLJIT_MOV_U16:
1789 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);
1791 case SLJIT_MOV_S16:
1792 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);
1794 case SLJIT_NOT:
1795 return emit_op(compiler, op, ALLOW_ANY_IMM, dst, dstw, TMP_REG1, 0, src, srcw);
1797 case SLJIT_NEG:
1798 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
1799 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
1800 compiler->skip_checks = 1;
1801 #endif
1802 return sljit_emit_op2(compiler, SLJIT_SUB | GET_ALL_FLAGS(op), dst, dstw, SLJIT_IMM, 0, src, srcw);
1804 case SLJIT_CLZ:
1805 return emit_op(compiler, op, 0, dst, dstw, TMP_REG1, 0, src, srcw);
1808 return SLJIT_SUCCESS;
1811 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op2(struct sljit_compiler *compiler, sljit_s32 op,
1812 sljit_s32 dst, sljit_sw dstw,
1813 sljit_s32 src1, sljit_sw src1w,
1814 sljit_s32 src2, sljit_sw src2w)
1816 CHECK_ERROR();
1817 CHECK(check_sljit_emit_op2(compiler, op, dst, dstw, src1, src1w, src2, src2w));
1818 ADJUST_LOCAL_OFFSET(dst, dstw);
1819 ADJUST_LOCAL_OFFSET(src1, src1w);
1820 ADJUST_LOCAL_OFFSET(src2, src2w);
1822 if (dst == SLJIT_UNUSED && !HAS_FLAGS(op))
1823 return SLJIT_SUCCESS;
1825 switch (GET_OPCODE(op)) {
1826 case SLJIT_ADD:
1827 case SLJIT_ADDC:
1828 case SLJIT_SUB:
1829 case SLJIT_SUBC:
1830 case SLJIT_OR:
1831 case SLJIT_XOR:
1832 return emit_op(compiler, op, ALLOW_IMM, dst, dstw, src1, src1w, src2, src2w);
1834 case SLJIT_MUL:
1835 return emit_op(compiler, op, 0, dst, dstw, src1, src1w, src2, src2w);
1837 case SLJIT_AND:
1838 return emit_op(compiler, op, ALLOW_ANY_IMM, dst, dstw, src1, src1w, src2, src2w);
1840 case SLJIT_SHL:
1841 case SLJIT_LSHR:
1842 case SLJIT_ASHR:
1843 if (src2 & SLJIT_IMM) {
1844 compiler->shift_imm = src2w & 0x1f;
1845 return emit_op(compiler, op, 0, dst, dstw, TMP_REG1, 0, src1, src1w);
1847 else {
1848 compiler->shift_imm = 0x20;
1849 return emit_op(compiler, op, 0, dst, dstw, src1, src1w, src2, src2w);
1853 return SLJIT_SUCCESS;
1856 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_src(struct sljit_compiler *compiler, sljit_s32 op,
1857 sljit_s32 src, sljit_sw srcw)
1859 CHECK_ERROR();
1860 CHECK(check_sljit_emit_op_src(compiler, op, src, srcw));
1861 ADJUST_LOCAL_OFFSET(src, srcw);
1863 switch (op) {
1864 case SLJIT_FAST_RETURN:
1865 SLJIT_ASSERT(reg_map[TMP_REG2] == 14);
1867 if (FAST_IS_REG(src))
1868 FAIL_IF(push_inst(compiler, MOV | RD(TMP_REG2) | RM(src)));
1869 else
1870 FAIL_IF(emit_op_mem(compiler, WORD_SIZE | LOAD_DATA, TMP_REG2, src, srcw, TMP_REG1));
1872 return push_inst(compiler, BX | RM(TMP_REG2));
1873 case SLJIT_SKIP_FRAMES_BEFORE_FAST_RETURN:
1874 return SLJIT_SUCCESS;
1875 case SLJIT_PREFETCH_L1:
1876 case SLJIT_PREFETCH_L2:
1877 case SLJIT_PREFETCH_L3:
1878 case SLJIT_PREFETCH_ONCE:
1879 #if (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
1880 SLJIT_ASSERT(src & SLJIT_MEM);
1881 return emit_op_mem(compiler, PRELOAD | LOAD_DATA, TMP_PC, src, srcw, TMP_REG1);
1882 #else /* !SLJIT_CONFIG_ARM_V7 */
1883 return SLJIT_SUCCESS;
1884 #endif /* SLJIT_CONFIG_ARM_V7 */
1887 return SLJIT_SUCCESS;
1890 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_register_index(sljit_s32 reg)
1892 CHECK_REG_INDEX(check_sljit_get_register_index(reg));
1893 return reg_map[reg];
1896 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_float_register_index(sljit_s32 reg)
1898 CHECK_REG_INDEX(check_sljit_get_float_register_index(reg));
1899 return (freg_map[reg] << 1);
1902 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_custom(struct sljit_compiler *compiler,
1903 void *instruction, sljit_s32 size)
1905 CHECK_ERROR();
1906 CHECK(check_sljit_emit_op_custom(compiler, instruction, size));
1908 return push_inst(compiler, *(sljit_uw*)instruction);
1911 /* --------------------------------------------------------------------- */
1912 /* Floating point operators */
1913 /* --------------------------------------------------------------------- */
1916 #define FPU_LOAD (1 << 20)
1917 #define EMIT_FPU_DATA_TRANSFER(inst, add, base, freg, offs) \
1918 ((inst) | ((add) << 23) | (reg_map[base] << 16) | (freg_map[freg] << 12) | (offs))
1919 #define EMIT_FPU_OPERATION(opcode, mode, dst, src1, src2) \
1920 ((opcode) | (mode) | (freg_map[dst] << 12) | freg_map[src1] | (freg_map[src2] << 16))
1922 static sljit_s32 emit_fop_mem(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg, sljit_s32 arg, sljit_sw argw)
1924 sljit_uw imm;
1925 sljit_sw inst = VSTR_F32 | (flags & (SLJIT_F32_OP | FPU_LOAD));
1927 SLJIT_ASSERT(arg & SLJIT_MEM);
1928 arg &= ~SLJIT_MEM;
1930 if (SLJIT_UNLIKELY(arg & OFFS_REG_MASK)) {
1931 FAIL_IF(push_inst(compiler, ADD | RD(TMP_REG2) | RN(arg & REG_MASK) | RM(OFFS_REG(arg)) | ((argw & 0x3) << 7)));
1932 arg = TMP_REG2;
1933 argw = 0;
1936 /* Fast loads and stores. */
1937 if (arg) {
1938 if (!(argw & ~0x3fc))
1939 return push_inst(compiler, EMIT_FPU_DATA_TRANSFER(inst, 1, arg & REG_MASK, reg, argw >> 2));
1940 if (!(-argw & ~0x3fc))
1941 return push_inst(compiler, EMIT_FPU_DATA_TRANSFER(inst, 0, arg & REG_MASK, reg, (-argw) >> 2));
1943 imm = get_imm(argw & ~0x3fc);
1944 if (imm) {
1945 FAIL_IF(push_inst(compiler, ADD | RD(TMP_REG2) | RN(arg & REG_MASK) | imm));
1946 return push_inst(compiler, EMIT_FPU_DATA_TRANSFER(inst, 1, TMP_REG2, reg, (argw & 0x3fc) >> 2));
1948 imm = get_imm(-argw & ~0x3fc);
1949 if (imm) {
1950 argw = -argw;
1951 FAIL_IF(push_inst(compiler, SUB | RD(TMP_REG2) | RN(arg & REG_MASK) | imm));
1952 return push_inst(compiler, EMIT_FPU_DATA_TRANSFER(inst, 0, TMP_REG2, reg, (argw & 0x3fc) >> 2));
1956 if (arg) {
1957 FAIL_IF(load_immediate(compiler, TMP_REG2, argw));
1958 FAIL_IF(push_inst(compiler, ADD | RD(TMP_REG2) | RN(arg & REG_MASK) | RM(TMP_REG2)));
1960 else
1961 FAIL_IF(load_immediate(compiler, TMP_REG2, argw));
1963 return push_inst(compiler, EMIT_FPU_DATA_TRANSFER(inst, 1, TMP_REG2, reg, 0));
1966 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_conv_sw_from_f64(struct sljit_compiler *compiler, sljit_s32 op,
1967 sljit_s32 dst, sljit_sw dstw,
1968 sljit_s32 src, sljit_sw srcw)
1970 op ^= SLJIT_F32_OP;
1972 if (src & SLJIT_MEM) {
1973 FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, TMP_FREG1, src, srcw));
1974 src = TMP_FREG1;
1977 FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VCVT_S32_F32, op & SLJIT_F32_OP, TMP_FREG1, src, 0)));
1979 if (FAST_IS_REG(dst))
1980 return push_inst(compiler, VMOV | (1 << 20) | RD(dst) | (freg_map[TMP_FREG1] << 16));
1982 /* Store the integer value from a VFP register. */
1983 return emit_fop_mem(compiler, 0, TMP_FREG1, dst, dstw);
1986 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_conv_f64_from_sw(struct sljit_compiler *compiler, sljit_s32 op,
1987 sljit_s32 dst, sljit_sw dstw,
1988 sljit_s32 src, sljit_sw srcw)
1990 sljit_s32 dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
1992 op ^= SLJIT_F32_OP;
1994 if (FAST_IS_REG(src))
1995 FAIL_IF(push_inst(compiler, VMOV | RD(src) | (freg_map[TMP_FREG1] << 16)));
1996 else if (src & SLJIT_MEM) {
1997 /* Load the integer value into a VFP register. */
1998 FAIL_IF(emit_fop_mem(compiler, FPU_LOAD, TMP_FREG1, src, srcw));
2000 else {
2001 FAIL_IF(load_immediate(compiler, TMP_REG1, srcw));
2002 FAIL_IF(push_inst(compiler, VMOV | RD(TMP_REG1) | (freg_map[TMP_FREG1] << 16)));
2005 FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VCVT_F32_S32, op & SLJIT_F32_OP, dst_r, TMP_FREG1, 0)));
2007 if (dst & SLJIT_MEM)
2008 return emit_fop_mem(compiler, (op & SLJIT_F32_OP), TMP_FREG1, dst, dstw);
2009 return SLJIT_SUCCESS;
2012 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_cmp(struct sljit_compiler *compiler, sljit_s32 op,
2013 sljit_s32 src1, sljit_sw src1w,
2014 sljit_s32 src2, sljit_sw src2w)
2016 op ^= SLJIT_F32_OP;
2018 if (src1 & SLJIT_MEM) {
2019 FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, TMP_FREG1, src1, src1w));
2020 src1 = TMP_FREG1;
2023 if (src2 & SLJIT_MEM) {
2024 FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, TMP_FREG2, src2, src2w));
2025 src2 = TMP_FREG2;
2028 FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VCMP_F32, op & SLJIT_F32_OP, src1, src2, 0)));
2029 return push_inst(compiler, VMRS);
2032 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop1(struct sljit_compiler *compiler, sljit_s32 op,
2033 sljit_s32 dst, sljit_sw dstw,
2034 sljit_s32 src, sljit_sw srcw)
2036 sljit_s32 dst_r;
2038 CHECK_ERROR();
2040 SLJIT_COMPILE_ASSERT((SLJIT_F32_OP == 0x100), float_transfer_bit_error);
2041 SELECT_FOP1_OPERATION_WITH_CHECKS(compiler, op, dst, dstw, src, srcw);
2043 dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
2045 if (GET_OPCODE(op) != SLJIT_CONV_F64_FROM_F32)
2046 op ^= SLJIT_F32_OP;
2048 if (src & SLJIT_MEM) {
2049 FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, dst_r, src, srcw));
2050 src = dst_r;
2053 switch (GET_OPCODE(op)) {
2054 case SLJIT_MOV_F64:
2055 if (src != dst_r) {
2056 if (dst_r != TMP_FREG1)
2057 FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VMOV_F32, op & SLJIT_F32_OP, dst_r, src, 0)));
2058 else
2059 dst_r = src;
2061 break;
2062 case SLJIT_NEG_F64:
2063 FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VNEG_F32, op & SLJIT_F32_OP, dst_r, src, 0)));
2064 break;
2065 case SLJIT_ABS_F64:
2066 FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VABS_F32, op & SLJIT_F32_OP, dst_r, src, 0)));
2067 break;
2068 case SLJIT_CONV_F64_FROM_F32:
2069 FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VCVT_F64_F32, op & SLJIT_F32_OP, dst_r, src, 0)));
2070 op ^= SLJIT_F32_OP;
2071 break;
2074 if (dst & SLJIT_MEM)
2075 return emit_fop_mem(compiler, (op & SLJIT_F32_OP), dst_r, dst, dstw);
2076 return SLJIT_SUCCESS;
2079 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop2(struct sljit_compiler *compiler, sljit_s32 op,
2080 sljit_s32 dst, sljit_sw dstw,
2081 sljit_s32 src1, sljit_sw src1w,
2082 sljit_s32 src2, sljit_sw src2w)
2084 sljit_s32 dst_r;
2086 CHECK_ERROR();
2087 CHECK(check_sljit_emit_fop2(compiler, op, dst, dstw, src1, src1w, src2, src2w));
2088 ADJUST_LOCAL_OFFSET(dst, dstw);
2089 ADJUST_LOCAL_OFFSET(src1, src1w);
2090 ADJUST_LOCAL_OFFSET(src2, src2w);
2092 op ^= SLJIT_F32_OP;
2094 dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
2096 if (src2 & SLJIT_MEM) {
2097 FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, TMP_FREG2, src2, src2w));
2098 src2 = TMP_FREG2;
2101 if (src1 & SLJIT_MEM) {
2102 FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, TMP_FREG1, src1, src1w));
2103 src1 = TMP_FREG1;
2106 switch (GET_OPCODE(op)) {
2107 case SLJIT_ADD_F64:
2108 FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VADD_F32, op & SLJIT_F32_OP, dst_r, src2, src1)));
2109 break;
2111 case SLJIT_SUB_F64:
2112 FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VSUB_F32, op & SLJIT_F32_OP, dst_r, src2, src1)));
2113 break;
2115 case SLJIT_MUL_F64:
2116 FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VMUL_F32, op & SLJIT_F32_OP, dst_r, src2, src1)));
2117 break;
2119 case SLJIT_DIV_F64:
2120 FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VDIV_F32, op & SLJIT_F32_OP, dst_r, src2, src1)));
2121 break;
2124 if (dst_r == TMP_FREG1)
2125 FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_F32_OP), TMP_FREG1, dst, dstw));
2127 return SLJIT_SUCCESS;
2130 #undef FPU_LOAD
2131 #undef EMIT_FPU_DATA_TRANSFER
2133 /* --------------------------------------------------------------------- */
2134 /* Other instructions */
2135 /* --------------------------------------------------------------------- */
2137 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fast_enter(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw)
2139 CHECK_ERROR();
2140 CHECK(check_sljit_emit_fast_enter(compiler, dst, dstw));
2141 ADJUST_LOCAL_OFFSET(dst, dstw);
2143 SLJIT_ASSERT(reg_map[TMP_REG2] == 14);
2145 if (FAST_IS_REG(dst))
2146 return push_inst(compiler, MOV | RD(dst) | RM(TMP_REG2));
2148 /* Memory. */
2149 return emit_op_mem(compiler, WORD_SIZE, TMP_REG2, dst, dstw, TMP_REG1);
2152 /* --------------------------------------------------------------------- */
2153 /* Conditional instructions */
2154 /* --------------------------------------------------------------------- */
2156 static sljit_uw get_cc(sljit_s32 type)
2158 switch (type) {
2159 case SLJIT_EQUAL:
2160 case SLJIT_MUL_NOT_OVERFLOW:
2161 case SLJIT_EQUAL_F64:
2162 return 0x00000000;
2164 case SLJIT_NOT_EQUAL:
2165 case SLJIT_MUL_OVERFLOW:
2166 case SLJIT_NOT_EQUAL_F64:
2167 return 0x10000000;
2169 case SLJIT_LESS:
2170 case SLJIT_LESS_F64:
2171 return 0x30000000;
2173 case SLJIT_GREATER_EQUAL:
2174 case SLJIT_GREATER_EQUAL_F64:
2175 return 0x20000000;
2177 case SLJIT_GREATER:
2178 case SLJIT_GREATER_F64:
2179 return 0x80000000;
2181 case SLJIT_LESS_EQUAL:
2182 case SLJIT_LESS_EQUAL_F64:
2183 return 0x90000000;
2185 case SLJIT_SIG_LESS:
2186 return 0xb0000000;
2188 case SLJIT_SIG_GREATER_EQUAL:
2189 return 0xa0000000;
2191 case SLJIT_SIG_GREATER:
2192 return 0xc0000000;
2194 case SLJIT_SIG_LESS_EQUAL:
2195 return 0xd0000000;
2197 case SLJIT_OVERFLOW:
2198 case SLJIT_UNORDERED_F64:
2199 return 0x60000000;
2201 case SLJIT_NOT_OVERFLOW:
2202 case SLJIT_ORDERED_F64:
2203 return 0x70000000;
2205 default:
2206 SLJIT_ASSERT(type >= SLJIT_JUMP && type <= SLJIT_CALL_CDECL);
2207 return 0xe0000000;
2211 SLJIT_API_FUNC_ATTRIBUTE struct sljit_label* sljit_emit_label(struct sljit_compiler *compiler)
2213 struct sljit_label *label;
2215 CHECK_ERROR_PTR();
2216 CHECK_PTR(check_sljit_emit_label(compiler));
2218 if (compiler->last_label && compiler->last_label->size == compiler->size)
2219 return compiler->last_label;
2221 label = (struct sljit_label*)ensure_abuf(compiler, sizeof(struct sljit_label));
2222 PTR_FAIL_IF(!label);
2223 set_label(label, compiler);
2224 return label;
2227 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_jump(struct sljit_compiler *compiler, sljit_s32 type)
2229 struct sljit_jump *jump;
2231 CHECK_ERROR_PTR();
2232 CHECK_PTR(check_sljit_emit_jump(compiler, type));
2234 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
2235 PTR_FAIL_IF(!jump);
2236 set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
2237 type &= 0xff;
2239 SLJIT_ASSERT(reg_map[TMP_REG1] != 14);
2241 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
2242 if (type >= SLJIT_FAST_CALL)
2243 PTR_FAIL_IF(prepare_blx(compiler));
2244 PTR_FAIL_IF(push_inst_with_unique_literal(compiler, ((EMIT_DATA_TRANSFER(WORD_SIZE | LOAD_DATA, 1,
2245 type <= SLJIT_JUMP ? TMP_PC : TMP_REG1, TMP_PC, 0)) & ~COND_MASK) | get_cc(type), 0));
2247 if (jump->flags & SLJIT_REWRITABLE_JUMP) {
2248 jump->addr = compiler->size;
2249 compiler->patches++;
2252 if (type >= SLJIT_FAST_CALL) {
2253 jump->flags |= IS_BL;
2254 PTR_FAIL_IF(emit_blx(compiler));
2257 if (!(jump->flags & SLJIT_REWRITABLE_JUMP))
2258 jump->addr = compiler->size;
2259 #else
2260 if (type >= SLJIT_FAST_CALL)
2261 jump->flags |= IS_BL;
2262 PTR_FAIL_IF(emit_imm(compiler, TMP_REG1, 0));
2263 PTR_FAIL_IF(push_inst(compiler, (((type <= SLJIT_JUMP ? BX : BLX) | RM(TMP_REG1)) & ~COND_MASK) | get_cc(type)));
2264 jump->addr = compiler->size;
2265 #endif
2266 return jump;
2269 #ifdef __SOFTFP__
2271 static sljit_s32 softfloat_call_with_args(struct sljit_compiler *compiler, sljit_s32 arg_types, sljit_s32 *src)
2273 sljit_s32 stack_offset = 0;
2274 sljit_s32 arg_count = 0;
2275 sljit_s32 word_arg_offset = 0;
2276 sljit_s32 float_arg_count = 0;
2277 sljit_s32 types = 0;
2278 sljit_s32 src_offset = 4 * sizeof(sljit_sw);
2279 sljit_u8 offsets[4];
2281 if (src && FAST_IS_REG(*src))
2282 src_offset = reg_map[*src] * sizeof(sljit_sw);
2284 arg_types >>= SLJIT_DEF_SHIFT;
2286 while (arg_types) {
2287 types = (types << SLJIT_DEF_SHIFT) | (arg_types & SLJIT_DEF_MASK);
2289 switch (arg_types & SLJIT_DEF_MASK) {
2290 case SLJIT_ARG_TYPE_F32:
2291 offsets[arg_count] = (sljit_u8)stack_offset;
2292 stack_offset += sizeof(sljit_f32);
2293 arg_count++;
2294 float_arg_count++;
2295 break;
2296 case SLJIT_ARG_TYPE_F64:
2297 if (stack_offset & 0x7)
2298 stack_offset += sizeof(sljit_sw);
2299 offsets[arg_count] = (sljit_u8)stack_offset;
2300 stack_offset += sizeof(sljit_f64);
2301 arg_count++;
2302 float_arg_count++;
2303 break;
2304 default:
2305 offsets[arg_count] = (sljit_u8)stack_offset;
2306 stack_offset += sizeof(sljit_sw);
2307 arg_count++;
2308 word_arg_offset += sizeof(sljit_sw);
2309 break;
2312 arg_types >>= SLJIT_DEF_SHIFT;
2315 if (stack_offset > 16)
2316 FAIL_IF(push_inst(compiler, SUB | RD(SLJIT_SP) | RN(SLJIT_SP) | SRC2_IMM | (((stack_offset - 16) + 0x7) & ~0x7)));
2318 /* Process arguments in reversed direction. */
2319 while (types) {
2320 switch (types & SLJIT_DEF_MASK) {
2321 case SLJIT_ARG_TYPE_F32:
2322 arg_count--;
2323 float_arg_count--;
2324 stack_offset = offsets[arg_count];
2326 if (stack_offset < 16) {
2327 if (src_offset == stack_offset) {
2328 FAIL_IF(push_inst(compiler, MOV | RD(TMP_REG1) | (src_offset >> 2)));
2329 *src = TMP_REG1;
2331 FAIL_IF(push_inst(compiler, VMOV | 0x100000 | (float_arg_count << 16) | (stack_offset << 10)));
2332 } else
2333 FAIL_IF(push_inst(compiler, VSTR_F32 | 0x800000 | RN(SLJIT_SP) | (float_arg_count << 12) | ((stack_offset - 16) >> 2)));
2334 break;
2335 case SLJIT_ARG_TYPE_F64:
2336 arg_count--;
2337 float_arg_count--;
2338 stack_offset = offsets[arg_count];
2340 SLJIT_ASSERT((stack_offset & 0x7) == 0);
2342 if (stack_offset < 16) {
2343 if (src_offset == stack_offset || src_offset == stack_offset + sizeof(sljit_sw)) {
2344 FAIL_IF(push_inst(compiler, MOV | RD(TMP_REG1) | (src_offset >> 2)));
2345 *src = TMP_REG1;
2347 FAIL_IF(push_inst(compiler, VMOV2 | 0x100000 | (stack_offset << 10) | ((stack_offset + sizeof(sljit_sw)) << 14) | float_arg_count));
2348 } else
2349 FAIL_IF(push_inst(compiler, VSTR_F32 | 0x800100 | RN(SLJIT_SP) | (float_arg_count << 12) | ((stack_offset - 16) >> 2)));
2350 break;
2351 default:
2352 arg_count--;
2353 word_arg_offset -= sizeof(sljit_sw);
2354 stack_offset = offsets[arg_count];
2356 SLJIT_ASSERT(stack_offset >= word_arg_offset);
2358 if (stack_offset != word_arg_offset) {
2359 if (stack_offset < 16) {
2360 if (src_offset == stack_offset) {
2361 FAIL_IF(push_inst(compiler, MOV | RD(TMP_REG1) | (src_offset >> 2)));
2362 *src = TMP_REG1;
2364 else if (src_offset == word_arg_offset) {
2365 *src = 1 + (stack_offset >> 2);
2366 src_offset = stack_offset;
2368 FAIL_IF(push_inst(compiler, MOV | (stack_offset << 10) | (word_arg_offset >> 2)));
2369 } else
2370 FAIL_IF(push_inst(compiler, data_transfer_insts[WORD_SIZE] | 0x800000 | RN(SLJIT_SP) | (word_arg_offset << 10) | (stack_offset - 16)));
2372 break;
2375 types >>= SLJIT_DEF_SHIFT;
2378 return SLJIT_SUCCESS;
2381 static sljit_s32 softfloat_post_call_with_args(struct sljit_compiler *compiler, sljit_s32 arg_types)
2383 sljit_s32 stack_size = 0;
2385 if ((arg_types & SLJIT_DEF_MASK) == SLJIT_ARG_TYPE_F32)
2386 FAIL_IF(push_inst(compiler, VMOV | (0 << 16) | (0 << 12)));
2387 if ((arg_types & SLJIT_DEF_MASK) == SLJIT_ARG_TYPE_F64)
2388 FAIL_IF(push_inst(compiler, VMOV2 | (1 << 16) | (0 << 12) | 0));
2390 arg_types >>= SLJIT_DEF_SHIFT;
2392 while (arg_types) {
2393 switch (arg_types & SLJIT_DEF_MASK) {
2394 case SLJIT_ARG_TYPE_F32:
2395 stack_size += sizeof(sljit_f32);
2396 break;
2397 case SLJIT_ARG_TYPE_F64:
2398 if (stack_size & 0x7)
2399 stack_size += sizeof(sljit_sw);
2400 stack_size += sizeof(sljit_f64);
2401 break;
2402 default:
2403 stack_size += sizeof(sljit_sw);
2404 break;
2407 arg_types >>= SLJIT_DEF_SHIFT;
2410 if (stack_size <= 16)
2411 return SLJIT_SUCCESS;
2413 return push_inst(compiler, ADD | RD(SLJIT_SP) | RN(SLJIT_SP) | SRC2_IMM | (((stack_size - 16) + 0x7) & ~0x7));
2416 #else /* !__SOFTFP__ */
2418 static sljit_s32 hardfloat_call_with_args(struct sljit_compiler *compiler, sljit_s32 arg_types)
2420 sljit_u32 remap = 0;
2421 sljit_u32 offset = 0;
2422 sljit_u32 new_offset, mask;
2424 /* Remove return value. */
2425 arg_types >>= SLJIT_DEF_SHIFT;
2427 while (arg_types) {
2428 if ((arg_types & SLJIT_DEF_MASK) == SLJIT_ARG_TYPE_F32) {
2429 new_offset = 0;
2430 mask = 1;
2432 while (remap & mask) {
2433 new_offset++;
2434 mask <<= 1;
2436 remap |= mask;
2438 if (offset != new_offset)
2439 FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VMOV_F32,
2440 0, (new_offset >> 1) + 1, (offset >> 1) + 1, 0) | ((new_offset & 0x1) ? 0x400000 : 0)));
2442 offset += 2;
2444 else if ((arg_types & SLJIT_DEF_MASK) == SLJIT_ARG_TYPE_F64) {
2445 new_offset = 0;
2446 mask = 3;
2448 while (remap & mask) {
2449 new_offset += 2;
2450 mask <<= 2;
2452 remap |= mask;
2454 if (offset != new_offset)
2455 FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VMOV_F32, SLJIT_F32_OP, (new_offset >> 1) + 1, (offset >> 1) + 1, 0)));
2457 offset += 2;
2459 arg_types >>= SLJIT_DEF_SHIFT;
2462 return SLJIT_SUCCESS;
2465 #endif /* __SOFTFP__ */
2467 #undef EMIT_FPU_OPERATION
2469 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_call(struct sljit_compiler *compiler, sljit_s32 type,
2470 sljit_s32 arg_types)
2472 #ifdef __SOFTFP__
2473 struct sljit_jump *jump;
2474 #endif
2476 CHECK_ERROR_PTR();
2477 CHECK_PTR(check_sljit_emit_call(compiler, type, arg_types));
2479 #ifdef __SOFTFP__
2480 PTR_FAIL_IF(softfloat_call_with_args(compiler, arg_types, NULL));
2482 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2483 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2484 compiler->skip_checks = 1;
2485 #endif
2487 jump = sljit_emit_jump(compiler, type);
2488 PTR_FAIL_IF(jump == NULL);
2490 PTR_FAIL_IF(softfloat_post_call_with_args(compiler, arg_types));
2491 return jump;
2492 #else /* !__SOFTFP__ */
2493 PTR_FAIL_IF(hardfloat_call_with_args(compiler, arg_types));
2495 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2496 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2497 compiler->skip_checks = 1;
2498 #endif
2500 return sljit_emit_jump(compiler, type);
2501 #endif /* __SOFTFP__ */
2504 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_ijump(struct sljit_compiler *compiler, sljit_s32 type, sljit_s32 src, sljit_sw srcw)
2506 struct sljit_jump *jump;
2508 CHECK_ERROR();
2509 CHECK(check_sljit_emit_ijump(compiler, type, src, srcw));
2510 ADJUST_LOCAL_OFFSET(src, srcw);
2512 SLJIT_ASSERT(reg_map[TMP_REG1] != 14);
2514 if (!(src & SLJIT_IMM)) {
2515 if (FAST_IS_REG(src)) {
2516 SLJIT_ASSERT(reg_map[src] != 14);
2517 return push_inst(compiler, (type <= SLJIT_JUMP ? BX : BLX) | RM(src));
2520 SLJIT_ASSERT(src & SLJIT_MEM);
2521 FAIL_IF(emit_op_mem(compiler, WORD_SIZE | LOAD_DATA, TMP_REG1, src, srcw, TMP_REG1));
2522 return push_inst(compiler, (type <= SLJIT_JUMP ? BX : BLX) | RM(TMP_REG1));
2525 /* These jumps are converted to jump/call instructions when possible. */
2526 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
2527 FAIL_IF(!jump);
2528 set_jump(jump, compiler, JUMP_ADDR | ((type >= SLJIT_FAST_CALL) ? IS_BL : 0));
2529 jump->u.target = srcw;
2531 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
2532 if (type >= SLJIT_FAST_CALL)
2533 FAIL_IF(prepare_blx(compiler));
2534 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));
2535 if (type >= SLJIT_FAST_CALL)
2536 FAIL_IF(emit_blx(compiler));
2537 #else
2538 FAIL_IF(emit_imm(compiler, TMP_REG1, 0));
2539 FAIL_IF(push_inst(compiler, (type <= SLJIT_JUMP ? BX : BLX) | RM(TMP_REG1)));
2540 #endif
2541 jump->addr = compiler->size;
2542 return SLJIT_SUCCESS;
2545 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_icall(struct sljit_compiler *compiler, sljit_s32 type,
2546 sljit_s32 arg_types,
2547 sljit_s32 src, sljit_sw srcw)
2549 CHECK_ERROR();
2550 CHECK(check_sljit_emit_icall(compiler, type, arg_types, src, srcw));
2552 #ifdef __SOFTFP__
2553 if (src & SLJIT_MEM) {
2554 FAIL_IF(emit_op_mem(compiler, WORD_SIZE | LOAD_DATA, TMP_REG1, src, srcw, TMP_REG1));
2555 src = TMP_REG1;
2558 FAIL_IF(softfloat_call_with_args(compiler, arg_types, &src));
2560 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2561 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2562 compiler->skip_checks = 1;
2563 #endif
2565 FAIL_IF(sljit_emit_ijump(compiler, type, src, srcw));
2567 return softfloat_post_call_with_args(compiler, arg_types);
2568 #else /* !__SOFTFP__ */
2569 FAIL_IF(hardfloat_call_with_args(compiler, arg_types));
2571 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2572 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2573 compiler->skip_checks = 1;
2574 #endif
2576 return sljit_emit_ijump(compiler, type, src, srcw);
2577 #endif /* __SOFTFP__ */
2580 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_flags(struct sljit_compiler *compiler, sljit_s32 op,
2581 sljit_s32 dst, sljit_sw dstw,
2582 sljit_s32 type)
2584 sljit_s32 dst_reg, flags = GET_ALL_FLAGS(op);
2585 sljit_uw cc, ins;
2587 CHECK_ERROR();
2588 CHECK(check_sljit_emit_op_flags(compiler, op, dst, dstw, type));
2589 ADJUST_LOCAL_OFFSET(dst, dstw);
2591 op = GET_OPCODE(op);
2592 cc = get_cc(type & 0xff);
2593 dst_reg = FAST_IS_REG(dst) ? dst : TMP_REG1;
2595 if (op < SLJIT_ADD) {
2596 FAIL_IF(push_inst(compiler, MOV | RD(dst_reg) | SRC2_IMM | 0));
2597 FAIL_IF(push_inst(compiler, ((MOV | RD(dst_reg) | SRC2_IMM | 1) & ~COND_MASK) | cc));
2598 if (dst & SLJIT_MEM)
2599 return emit_op_mem(compiler, WORD_SIZE, TMP_REG1, dst, dstw, TMP_REG2);
2600 return SLJIT_SUCCESS;
2603 ins = (op == SLJIT_AND ? AND : (op == SLJIT_OR ? ORR : EOR));
2605 if (dst & SLJIT_MEM)
2606 FAIL_IF(emit_op_mem(compiler, WORD_SIZE | LOAD_DATA, TMP_REG1, dst, dstw, TMP_REG2));
2608 FAIL_IF(push_inst(compiler, ((ins | RD(dst_reg) | RN(dst_reg) | SRC2_IMM | 1) & ~COND_MASK) | cc));
2610 if (op == SLJIT_AND)
2611 FAIL_IF(push_inst(compiler, ((ins | RD(dst_reg) | RN(dst_reg) | SRC2_IMM | 0) & ~COND_MASK) | (cc ^ 0x10000000)));
2613 if (dst & SLJIT_MEM)
2614 FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG1, dst, dstw, TMP_REG2));
2616 if (flags & SLJIT_SET_Z)
2617 return push_inst(compiler, MOV | SET_FLAGS | RD(TMP_REG2) | RM(dst_reg));
2618 return SLJIT_SUCCESS;
2621 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_cmov(struct sljit_compiler *compiler, sljit_s32 type,
2622 sljit_s32 dst_reg,
2623 sljit_s32 src, sljit_sw srcw)
2625 sljit_uw cc, tmp;
2627 CHECK_ERROR();
2628 CHECK(check_sljit_emit_cmov(compiler, type, dst_reg, src, srcw));
2630 dst_reg &= ~SLJIT_I32_OP;
2632 cc = get_cc(type & 0xff);
2634 if (SLJIT_UNLIKELY(src & SLJIT_IMM)) {
2635 tmp = get_imm(srcw);
2636 if (tmp)
2637 return push_inst(compiler, ((MOV | RD(dst_reg) | tmp) & ~COND_MASK) | cc);
2639 tmp = get_imm(~srcw);
2640 if (tmp)
2641 return push_inst(compiler, ((MVN | RD(dst_reg) | tmp) & ~COND_MASK) | cc);
2643 #if (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
2644 tmp = (sljit_uw) srcw;
2645 FAIL_IF(push_inst(compiler, (MOVW & ~COND_MASK) | cc | RD(dst_reg) | ((tmp << 4) & 0xf0000) | (tmp & 0xfff)));
2646 if (tmp <= 0xffff)
2647 return SLJIT_SUCCESS;
2648 return push_inst(compiler, (MOVT & ~COND_MASK) | cc | RD(dst_reg) | ((tmp >> 12) & 0xf0000) | ((tmp >> 16) & 0xfff));
2649 #else
2650 FAIL_IF(load_immediate(compiler, TMP_REG1, srcw));
2651 src = TMP_REG1;
2652 #endif
2655 return push_inst(compiler, ((MOV | RD(dst_reg) | RM(src)) & ~COND_MASK) | cc);
2658 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_mem(struct sljit_compiler *compiler, sljit_s32 type,
2659 sljit_s32 reg,
2660 sljit_s32 mem, sljit_sw memw)
2662 sljit_s32 flags;
2663 sljit_uw is_type1_transfer, inst;
2665 CHECK_ERROR();
2666 CHECK(check_sljit_emit_mem(compiler, type, reg, mem, memw));
2668 is_type1_transfer = 1;
2670 switch (type & 0xff) {
2671 case SLJIT_MOV:
2672 case SLJIT_MOV_U32:
2673 case SLJIT_MOV_S32:
2674 case SLJIT_MOV_P:
2675 flags = WORD_SIZE;
2676 break;
2677 case SLJIT_MOV_U8:
2678 flags = BYTE_SIZE;
2679 break;
2680 case SLJIT_MOV_S8:
2681 if (!(type & SLJIT_MEM_STORE))
2682 is_type1_transfer = 0;
2683 flags = BYTE_SIZE | SIGNED;
2684 break;
2685 case SLJIT_MOV_U16:
2686 is_type1_transfer = 0;
2687 flags = HALF_SIZE;
2688 break;
2689 case SLJIT_MOV_S16:
2690 is_type1_transfer = 0;
2691 flags = HALF_SIZE | SIGNED;
2692 break;
2693 default:
2694 SLJIT_UNREACHABLE();
2695 flags = WORD_SIZE;
2696 break;
2699 if (!(type & SLJIT_MEM_STORE))
2700 flags |= LOAD_DATA;
2702 SLJIT_ASSERT(is_type1_transfer == !!IS_TYPE1_TRANSFER(flags));
2704 if (SLJIT_UNLIKELY(mem & OFFS_REG_MASK)) {
2705 if (!is_type1_transfer && memw != 0)
2706 return SLJIT_ERR_UNSUPPORTED;
2708 else {
2709 if (is_type1_transfer) {
2710 if (memw > 4095 || memw < -4095)
2711 return SLJIT_ERR_UNSUPPORTED;
2713 else {
2714 if (memw > 255 || memw < -255)
2715 return SLJIT_ERR_UNSUPPORTED;
2719 if (type & SLJIT_MEM_SUPP)
2720 return SLJIT_SUCCESS;
2722 if (SLJIT_UNLIKELY(mem & OFFS_REG_MASK)) {
2723 memw &= 0x3;
2725 inst = EMIT_DATA_TRANSFER(flags, 1, reg, mem & REG_MASK, RM(OFFS_REG(mem)) | (memw << 7));
2727 if (is_type1_transfer)
2728 inst |= (1 << 25);
2730 if (type & SLJIT_MEM_PRE)
2731 inst |= (1 << 21);
2732 else
2733 inst ^= (1 << 24);
2735 return push_inst(compiler, inst);
2738 inst = EMIT_DATA_TRANSFER(flags, 0, reg, mem & REG_MASK, 0);
2740 if (type & SLJIT_MEM_PRE)
2741 inst |= (1 << 21);
2742 else
2743 inst ^= (1 << 24);
2745 if (is_type1_transfer) {
2746 if (memw >= 0)
2747 inst |= (1 << 23);
2748 else
2749 memw = -memw;
2751 return push_inst(compiler, inst | memw);
2754 if (memw >= 0)
2755 inst |= (1 << 23);
2756 else
2757 memw = -memw;
2759 return push_inst(compiler, inst | TYPE2_TRANSFER_IMM(memw));
2762 SLJIT_API_FUNC_ATTRIBUTE struct sljit_const* sljit_emit_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw, sljit_sw init_value)
2764 struct sljit_const *const_;
2765 sljit_s32 dst_r;
2767 CHECK_ERROR_PTR();
2768 CHECK_PTR(check_sljit_emit_const(compiler, dst, dstw, init_value));
2769 ADJUST_LOCAL_OFFSET(dst, dstw);
2771 dst_r = SLOW_IS_REG(dst) ? dst : TMP_REG2;
2773 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
2774 PTR_FAIL_IF(push_inst_with_unique_literal(compiler, EMIT_DATA_TRANSFER(WORD_SIZE | LOAD_DATA, 1, dst_r, TMP_PC, 0), init_value));
2775 compiler->patches++;
2776 #else
2777 PTR_FAIL_IF(emit_imm(compiler, dst_r, init_value));
2778 #endif
2780 const_ = (struct sljit_const*)ensure_abuf(compiler, sizeof(struct sljit_const));
2781 PTR_FAIL_IF(!const_);
2782 set_const(const_, compiler);
2784 if (dst & SLJIT_MEM)
2785 PTR_FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG2, dst, dstw, TMP_REG1));
2786 return const_;
2789 SLJIT_API_FUNC_ATTRIBUTE struct sljit_put_label* sljit_emit_put_label(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw)
2791 struct sljit_put_label *put_label;
2792 sljit_s32 dst_r;
2794 CHECK_ERROR_PTR();
2795 CHECK_PTR(check_sljit_emit_put_label(compiler, dst, dstw));
2796 ADJUST_LOCAL_OFFSET(dst, dstw);
2798 dst_r = SLOW_IS_REG(dst) ? dst : TMP_REG2;
2800 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
2801 PTR_FAIL_IF(push_inst_with_unique_literal(compiler, EMIT_DATA_TRANSFER(WORD_SIZE | LOAD_DATA, 1, dst_r, TMP_PC, 0), 0));
2802 compiler->patches++;
2803 #else
2804 PTR_FAIL_IF(emit_imm(compiler, dst_r, 0));
2805 #endif
2807 put_label = (struct sljit_put_label*)ensure_abuf(compiler, sizeof(struct sljit_put_label));
2808 PTR_FAIL_IF(!put_label);
2809 set_put_label(put_label, compiler, 0);
2811 if (dst & SLJIT_MEM)
2812 PTR_FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG2, dst, dstw, TMP_REG1));
2813 return put_label;
2816 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_target, sljit_sw executable_offset)
2818 inline_set_jump_addr(addr, executable_offset, new_target, 1);
2821 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_const(sljit_uw addr, sljit_sw new_constant, sljit_sw executable_offset)
2823 inline_set_const(addr, executable_offset, new_constant, 1);