Implement tail call on MIPS.
[sljit.git] / sljit_src / sljitNativeARM_T2_32.c
blobbb1d5422dd37dd9049d0603f76c973c03ad31e4a
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 SLJIT_API_FUNC_ATTRIBUTE const char* sljit_get_platform_name(void)
29 #ifdef __SOFTFP__
30 return "ARM-Thumb2" SLJIT_CPUINFO " ABI:softfp";
31 #else
32 return "ARM-Thumb2" SLJIT_CPUINFO " ABI:hardfp";
33 #endif
36 /* Length of an instruction word. */
37 typedef sljit_u32 sljit_ins;
39 /* Last register + 1. */
40 #define TMP_REG1 (SLJIT_NUMBER_OF_REGISTERS + 2)
41 #define TMP_REG2 (SLJIT_NUMBER_OF_REGISTERS + 3)
42 #define TMP_PC (SLJIT_NUMBER_OF_REGISTERS + 4)
44 #define TMP_FREG1 (SLJIT_NUMBER_OF_FLOAT_REGISTERS + 1)
45 #define TMP_FREG2 (SLJIT_NUMBER_OF_FLOAT_REGISTERS + 2)
47 /* See sljit_emit_enter and sljit_emit_op0 if you want to change them. */
48 static const sljit_u8 reg_map[SLJIT_NUMBER_OF_REGISTERS + 5] = {
49 0, 0, 1, 2, 3, 11, 10, 9, 8, 7, 6, 5, 4, 13, 12, 14, 15
52 static const sljit_u8 freg_map[SLJIT_NUMBER_OF_FLOAT_REGISTERS + 3] = {
53 0, 0, 1, 2, 3, 4, 5, 6, 7
56 #define COPY_BITS(src, from, to, bits) \
57 ((from >= to ? ((sljit_ins)(src) >> (from - to)) : ((sljit_ins)(src) << (to - from))) & (((1 << bits) - 1) << to))
59 #define NEGATE(uimm) ((sljit_uw)-(sljit_sw)(uimm))
61 /* Thumb16 encodings. */
62 #define RD3(rd) ((sljit_ins)reg_map[rd])
63 #define RN3(rn) ((sljit_ins)reg_map[rn] << 3)
64 #define RM3(rm) ((sljit_ins)reg_map[rm] << 6)
65 #define RDN3(rdn) ((sljit_ins)reg_map[rdn] << 8)
66 #define IMM3(imm) ((sljit_ins)imm << 6)
67 #define IMM8(imm) ((sljit_ins)imm)
69 /* Thumb16 helpers. */
70 #define SET_REGS44(rd, rn) \
71 (((sljit_ins)reg_map[rn] << 3) | ((sljit_ins)reg_map[rd] & 0x7) | (((sljit_ins)reg_map[rd] & 0x8) << 4))
72 #define IS_2_LO_REGS(reg1, reg2) \
73 (reg_map[reg1] <= 7 && reg_map[reg2] <= 7)
74 #define IS_3_LO_REGS(reg1, reg2, reg3) \
75 (reg_map[reg1] <= 7 && reg_map[reg2] <= 7 && reg_map[reg3] <= 7)
77 /* Thumb32 encodings. */
78 #define RD4(rd) ((sljit_ins)reg_map[rd] << 8)
79 #define RN4(rn) ((sljit_ins)reg_map[rn] << 16)
80 #define RM4(rm) ((sljit_ins)reg_map[rm])
81 #define RT4(rt) ((sljit_ins)reg_map[rt] << 12)
82 #define DD4(dd) ((sljit_ins)freg_map[dd] << 12)
83 #define DN4(dn) ((sljit_ins)freg_map[dn] << 16)
84 #define DM4(dm) ((sljit_ins)freg_map[dm])
85 #define IMM5(imm) \
86 (COPY_BITS(imm, 2, 12, 3) | (((sljit_ins)imm & 0x3) << 6))
87 #define IMM12(imm) \
88 (COPY_BITS(imm, 11, 26, 1) | COPY_BITS(imm, 8, 12, 3) | ((sljit_ins)imm & 0xff))
90 /* --------------------------------------------------------------------- */
91 /* Instrucion forms */
92 /* --------------------------------------------------------------------- */
94 /* dot '.' changed to _
95 I immediate form (possibly followed by number of immediate bits). */
96 #define ADCI 0xf1400000
97 #define ADCS 0x4140
98 #define ADC_W 0xeb400000
99 #define ADD 0x4400
100 #define ADDS 0x1800
101 #define ADDSI3 0x1c00
102 #define ADDSI8 0x3000
103 #define ADD_W 0xeb000000
104 #define ADDWI 0xf2000000
105 #define ADD_SP 0x4485
106 #define ADD_SP_I 0xb000
107 #define ADD_W 0xeb000000
108 #define ADD_WI 0xf1000000
109 #define ANDI 0xf0000000
110 #define ANDS 0x4000
111 #define AND_W 0xea000000
112 #define ASRS 0x4100
113 #define ASRSI 0x1000
114 #define ASR_W 0xfa40f000
115 #define ASR_WI 0xea4f0020
116 #define BCC 0xd000
117 #define BICI 0xf0200000
118 #define BKPT 0xbe00
119 #define BLX 0x4780
120 #define BX 0x4700
121 #define CLZ 0xfab0f080
122 #define CMNI_W 0xf1100f00
123 #define CMP 0x4280
124 #define CMPI 0x2800
125 #define CMPI_W 0xf1b00f00
126 #define CMP_X 0x4500
127 #define CMP_W 0xebb00f00
128 #define EORI 0xf0800000
129 #define EORS 0x4040
130 #define EOR_W 0xea800000
131 #define IT 0xbf00
132 #define LDR_SP 0x9800
133 #define LDR 0xf8d00000
134 #define LDRI 0xf8500800
135 #define LSLS 0x4080
136 #define LSLSI 0x0000
137 #define LSL_W 0xfa00f000
138 #define LSL_WI 0xea4f0000
139 #define LSRS 0x40c0
140 #define LSRSI 0x0800
141 #define LSR_W 0xfa20f000
142 #define LSR_WI 0xea4f0010
143 #define MOV 0x4600
144 #define MOVS 0x0000
145 #define MOVSI 0x2000
146 #define MOVT 0xf2c00000
147 #define MOVW 0xf2400000
148 #define MOV_W 0xea4f0000
149 #define MOV_WI 0xf04f0000
150 #define MUL 0xfb00f000
151 #define MVNS 0x43c0
152 #define MVN_W 0xea6f0000
153 #define MVN_WI 0xf06f0000
154 #define NOP 0xbf00
155 #define ORNI 0xf0600000
156 #define ORRI 0xf0400000
157 #define ORRS 0x4300
158 #define ORR_W 0xea400000
159 #define POP 0xbc00
160 #define POP_W 0xe8bd0000
161 #define PUSH 0xb400
162 #define PUSH_W 0xe92d0000
163 #define RSB_WI 0xf1c00000
164 #define RSBSI 0x4240
165 #define SBCI 0xf1600000
166 #define SBCS 0x4180
167 #define SBC_W 0xeb600000
168 #define SDIV 0xfb90f0f0
169 #define SMULL 0xfb800000
170 #define STR_SP 0x9000
171 #define SUBS 0x1a00
172 #define SUBSI3 0x1e00
173 #define SUBSI8 0x3800
174 #define SUB_W 0xeba00000
175 #define SUBWI 0xf2a00000
176 #define SUB_SP_I 0xb080
177 #define SUB_WI 0xf1a00000
178 #define SXTB 0xb240
179 #define SXTB_W 0xfa4ff080
180 #define SXTH 0xb200
181 #define SXTH_W 0xfa0ff080
182 #define TST 0x4200
183 #define TSTI 0xf0000f00
184 #define TST_W 0xea000f00
185 #define UDIV 0xfbb0f0f0
186 #define UMULL 0xfba00000
187 #define UXTB 0xb2c0
188 #define UXTB_W 0xfa5ff080
189 #define UXTH 0xb280
190 #define UXTH_W 0xfa1ff080
191 #define VABS_F32 0xeeb00ac0
192 #define VADD_F32 0xee300a00
193 #define VCMP_F32 0xeeb40a40
194 #define VCVT_F32_S32 0xeeb80ac0
195 #define VCVT_F64_F32 0xeeb70ac0
196 #define VCVT_S32_F32 0xeebd0ac0
197 #define VDIV_F32 0xee800a00
198 #define VLDR_F32 0xed100a00
199 #define VMOV_F32 0xeeb00a40
200 #define VMOV 0xee000a10
201 #define VMOV2 0xec400a10
202 #define VMRS 0xeef1fa10
203 #define VMUL_F32 0xee200a00
204 #define VNEG_F32 0xeeb10a40
205 #define VSTR_F32 0xed000a00
206 #define VSUB_F32 0xee300a40
208 static sljit_s32 push_inst16(struct sljit_compiler *compiler, sljit_ins inst)
210 sljit_u16 *ptr;
211 SLJIT_ASSERT(!(inst & 0xffff0000));
213 ptr = (sljit_u16*)ensure_buf(compiler, sizeof(sljit_u16));
214 FAIL_IF(!ptr);
215 *ptr = (sljit_u16)(inst);
216 compiler->size++;
217 return SLJIT_SUCCESS;
220 static sljit_s32 push_inst32(struct sljit_compiler *compiler, sljit_ins inst)
222 sljit_u16 *ptr = (sljit_u16*)ensure_buf(compiler, sizeof(sljit_ins));
223 FAIL_IF(!ptr);
224 *ptr++ = (sljit_u16)(inst >> 16);
225 *ptr = (sljit_u16)(inst);
226 compiler->size += 2;
227 return SLJIT_SUCCESS;
230 static SLJIT_INLINE sljit_s32 emit_imm32_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_uw imm)
232 FAIL_IF(push_inst32(compiler, MOVW | RD4(dst)
233 | COPY_BITS(imm, 12, 16, 4) | COPY_BITS(imm, 11, 26, 1) | COPY_BITS(imm, 8, 12, 3) | (imm & 0xff)));
234 return push_inst32(compiler, MOVT | RD4(dst)
235 | COPY_BITS(imm, 12 + 16, 16, 4) | COPY_BITS(imm, 11 + 16, 26, 1) | COPY_BITS(imm, 8 + 16, 12, 3) | ((imm & 0xff0000) >> 16));
238 static SLJIT_INLINE void modify_imm32_const(sljit_u16 *inst, sljit_uw new_imm)
240 sljit_ins dst = inst[1] & 0x0f00;
241 SLJIT_ASSERT(((inst[0] & 0xfbf0) == (MOVW >> 16)) && ((inst[2] & 0xfbf0) == (MOVT >> 16)) && dst == (inst[3] & 0x0f00));
242 inst[0] = (sljit_u16)((MOVW >> 16) | COPY_BITS(new_imm, 12, 0, 4) | COPY_BITS(new_imm, 11, 10, 1));
243 inst[1] = (sljit_u16)(dst | COPY_BITS(new_imm, 8, 12, 3) | (new_imm & 0xff));
244 inst[2] = (sljit_u16)((MOVT >> 16) | COPY_BITS(new_imm, 12 + 16, 0, 4) | COPY_BITS(new_imm, 11 + 16, 10, 1));
245 inst[3] = (sljit_u16)(dst | COPY_BITS(new_imm, 8 + 16, 12, 3) | ((new_imm & 0xff0000) >> 16));
248 static SLJIT_INLINE sljit_s32 detect_jump_type(struct sljit_jump *jump, sljit_u16 *code_ptr, sljit_u16 *code, sljit_sw executable_offset)
250 sljit_sw diff;
252 if (jump->flags & SLJIT_REWRITABLE_JUMP)
253 return 0;
255 if (jump->flags & JUMP_ADDR) {
256 /* Branch to ARM code is not optimized yet. */
257 if (!(jump->u.target & 0x1))
258 return 0;
259 diff = ((sljit_sw)jump->u.target - (sljit_sw)(code_ptr + 2) - executable_offset) >> 1;
261 else {
262 SLJIT_ASSERT(jump->flags & JUMP_LABEL);
263 diff = ((sljit_sw)(code + jump->u.label->size) - (sljit_sw)(code_ptr + 2)) >> 1;
266 if (jump->flags & IS_COND) {
267 SLJIT_ASSERT(!(jump->flags & IS_BL));
268 if (diff <= 127 && diff >= -128) {
269 jump->flags |= PATCH_TYPE1;
270 return 5;
272 if (diff <= 524287 && diff >= -524288) {
273 jump->flags |= PATCH_TYPE2;
274 return 4;
276 /* +1 comes from the prefix IT instruction. */
277 diff--;
278 if (diff <= 8388607 && diff >= -8388608) {
279 jump->flags |= PATCH_TYPE3;
280 return 3;
283 else if (jump->flags & IS_BL) {
284 if (diff <= 8388607 && diff >= -8388608) {
285 jump->flags |= PATCH_BL;
286 return 3;
289 else {
290 if (diff <= 1023 && diff >= -1024) {
291 jump->flags |= PATCH_TYPE4;
292 return 4;
294 if (diff <= 8388607 && diff >= -8388608) {
295 jump->flags |= PATCH_TYPE5;
296 return 3;
300 return 0;
303 static SLJIT_INLINE void set_jump_instruction(struct sljit_jump *jump, sljit_sw executable_offset)
305 sljit_s32 type = (jump->flags >> 4) & 0xf;
306 sljit_sw diff;
307 sljit_u16 *jump_inst;
308 sljit_s32 s, j1, j2;
310 if (SLJIT_UNLIKELY(type == 0)) {
311 modify_imm32_const((sljit_u16*)jump->addr, (jump->flags & JUMP_LABEL) ? jump->u.label->addr : jump->u.target);
312 return;
315 if (jump->flags & JUMP_ADDR) {
316 SLJIT_ASSERT(jump->u.target & 0x1);
317 diff = ((sljit_sw)jump->u.target - (sljit_sw)(jump->addr + sizeof(sljit_u32)) - executable_offset) >> 1;
319 else {
320 SLJIT_ASSERT(jump->u.label->addr & 0x1);
321 diff = ((sljit_sw)(jump->u.label->addr) - (sljit_sw)(jump->addr + sizeof(sljit_u32)) - executable_offset) >> 1;
323 jump_inst = (sljit_u16*)jump->addr;
325 switch (type) {
326 case 1:
327 /* Encoding T1 of 'B' instruction */
328 SLJIT_ASSERT(diff <= 127 && diff >= -128 && (jump->flags & IS_COND));
329 jump_inst[0] = (sljit_u16)(0xd000 | (jump->flags & 0xf00) | ((sljit_ins)diff & 0xff));
330 return;
331 case 2:
332 /* Encoding T3 of 'B' instruction */
333 SLJIT_ASSERT(diff <= 524287 && diff >= -524288 && (jump->flags & IS_COND));
334 jump_inst[0] = (sljit_u16)(0xf000 | COPY_BITS(jump->flags, 8, 6, 4) | COPY_BITS(diff, 11, 0, 6) | COPY_BITS(diff, 19, 10, 1));
335 jump_inst[1] = (sljit_u16)(0x8000 | COPY_BITS(diff, 17, 13, 1) | COPY_BITS(diff, 18, 11, 1) | ((sljit_ins)diff & 0x7ff));
336 return;
337 case 3:
338 SLJIT_ASSERT(jump->flags & IS_COND);
339 *jump_inst++ = (sljit_u16)(IT | ((jump->flags >> 4) & 0xf0) | 0x8);
340 diff--;
341 type = 5;
342 break;
343 case 4:
344 /* Encoding T2 of 'B' instruction */
345 SLJIT_ASSERT(diff <= 1023 && diff >= -1024 && !(jump->flags & IS_COND));
346 jump_inst[0] = (sljit_u16)(0xe000 | (diff & 0x7ff));
347 return;
350 SLJIT_ASSERT(diff <= 8388607 && diff >= -8388608);
352 /* Really complex instruction form for branches. */
353 s = (diff >> 23) & 0x1;
354 j1 = (~(diff >> 22) ^ s) & 0x1;
355 j2 = (~(diff >> 21) ^ s) & 0x1;
356 jump_inst[0] = (sljit_u16)(0xf000 | ((sljit_ins)s << 10) | COPY_BITS(diff, 11, 0, 10));
357 jump_inst[1] = (sljit_u16)((j1 << 13) | (j2 << 11) | (diff & 0x7ff));
359 /* The others have a common form. */
360 if (type == 5) /* Encoding T4 of 'B' instruction */
361 jump_inst[1] |= 0x9000;
362 else if (type == 6) /* Encoding T1 of 'BL' instruction */
363 jump_inst[1] |= 0xd000;
364 else
365 SLJIT_UNREACHABLE();
368 SLJIT_API_FUNC_ATTRIBUTE void* sljit_generate_code(struct sljit_compiler *compiler)
370 struct sljit_memory_fragment *buf;
371 sljit_u16 *code;
372 sljit_u16 *code_ptr;
373 sljit_u16 *buf_ptr;
374 sljit_u16 *buf_end;
375 sljit_uw half_count;
376 sljit_uw next_addr;
377 sljit_sw executable_offset;
379 struct sljit_label *label;
380 struct sljit_jump *jump;
381 struct sljit_const *const_;
382 struct sljit_put_label *put_label;
384 CHECK_ERROR_PTR();
385 CHECK_PTR(check_sljit_generate_code(compiler));
386 reverse_buf(compiler);
388 code = (sljit_u16*)SLJIT_MALLOC_EXEC(compiler->size * sizeof(sljit_u16), compiler->exec_allocator_data);
389 PTR_FAIL_WITH_EXEC_IF(code);
390 buf = compiler->buf;
392 code_ptr = code;
393 half_count = 0;
394 next_addr = 0;
395 executable_offset = SLJIT_EXEC_OFFSET(code);
397 label = compiler->labels;
398 jump = compiler->jumps;
399 const_ = compiler->consts;
400 put_label = compiler->put_labels;
402 do {
403 buf_ptr = (sljit_u16*)buf->memory;
404 buf_end = buf_ptr + (buf->used_size >> 1);
405 do {
406 *code_ptr = *buf_ptr++;
407 if (next_addr == half_count) {
408 SLJIT_ASSERT(!label || label->size >= half_count);
409 SLJIT_ASSERT(!jump || jump->addr >= half_count);
410 SLJIT_ASSERT(!const_ || const_->addr >= half_count);
411 SLJIT_ASSERT(!put_label || put_label->addr >= half_count);
413 /* These structures are ordered by their address. */
414 if (label && label->size == half_count) {
415 label->addr = ((sljit_uw)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset)) | 0x1;
416 label->size = (sljit_uw)(code_ptr - code);
417 label = label->next;
419 if (jump && jump->addr == half_count) {
420 jump->addr = (sljit_uw)code_ptr - ((jump->flags & IS_COND) ? 10 : 8);
421 code_ptr -= detect_jump_type(jump, code_ptr, code, executable_offset);
422 jump = jump->next;
424 if (const_ && const_->addr == half_count) {
425 const_->addr = (sljit_uw)code_ptr;
426 const_ = const_->next;
428 if (put_label && put_label->addr == half_count) {
429 SLJIT_ASSERT(put_label->label);
430 put_label->addr = (sljit_uw)code_ptr;
431 put_label = put_label->next;
433 next_addr = compute_next_addr(label, jump, const_, put_label);
435 code_ptr ++;
436 half_count ++;
437 } while (buf_ptr < buf_end);
439 buf = buf->next;
440 } while (buf);
442 if (label && label->size == half_count) {
443 label->addr = ((sljit_uw)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset)) | 0x1;
444 label->size = (sljit_uw)(code_ptr - code);
445 label = label->next;
448 SLJIT_ASSERT(!label);
449 SLJIT_ASSERT(!jump);
450 SLJIT_ASSERT(!const_);
451 SLJIT_ASSERT(!put_label);
452 SLJIT_ASSERT(code_ptr - code <= (sljit_sw)compiler->size);
454 jump = compiler->jumps;
455 while (jump) {
456 set_jump_instruction(jump, executable_offset);
457 jump = jump->next;
460 put_label = compiler->put_labels;
461 while (put_label) {
462 modify_imm32_const((sljit_u16 *)put_label->addr, put_label->label->addr);
463 put_label = put_label->next;
466 compiler->error = SLJIT_ERR_COMPILED;
467 compiler->executable_offset = executable_offset;
468 compiler->executable_size = (sljit_uw)(code_ptr - code) * sizeof(sljit_u16);
470 code = (sljit_u16 *)SLJIT_ADD_EXEC_OFFSET(code, executable_offset);
471 code_ptr = (sljit_u16 *)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset);
473 SLJIT_CACHE_FLUSH(code, code_ptr);
474 SLJIT_UPDATE_WX_FLAGS(code, code_ptr, 1);
476 /* Set thumb mode flag. */
477 return (void*)((sljit_uw)code | 0x1);
480 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_has_cpu_feature(sljit_s32 feature_type)
482 switch (feature_type) {
483 case SLJIT_HAS_FPU:
484 #ifdef SLJIT_IS_FPU_AVAILABLE
485 return SLJIT_IS_FPU_AVAILABLE;
486 #else
487 /* Available by default. */
488 return 1;
489 #endif
491 case SLJIT_HAS_CLZ:
492 case SLJIT_HAS_CMOV:
493 case SLJIT_HAS_PREFETCH:
494 return 1;
496 default:
497 return 0;
501 /* --------------------------------------------------------------------- */
502 /* Core code generator functions. */
503 /* --------------------------------------------------------------------- */
505 #define INVALID_IMM 0x80000000
506 static sljit_uw get_imm(sljit_uw imm)
508 /* Thumb immediate form. */
509 sljit_s32 counter;
511 if (imm <= 0xff)
512 return imm;
514 if ((imm & 0xffff) == (imm >> 16)) {
515 /* Some special cases. */
516 if (!(imm & 0xff00))
517 return (1 << 12) | (imm & 0xff);
518 if (!(imm & 0xff))
519 return (2 << 12) | ((imm >> 8) & 0xff);
520 if ((imm & 0xff00) == ((imm & 0xff) << 8))
521 return (3 << 12) | (imm & 0xff);
524 /* Assembly optimization: count leading zeroes? */
525 counter = 8;
526 if (!(imm & 0xffff0000)) {
527 counter += 16;
528 imm <<= 16;
530 if (!(imm & 0xff000000)) {
531 counter += 8;
532 imm <<= 8;
534 if (!(imm & 0xf0000000)) {
535 counter += 4;
536 imm <<= 4;
538 if (!(imm & 0xc0000000)) {
539 counter += 2;
540 imm <<= 2;
542 if (!(imm & 0x80000000)) {
543 counter += 1;
544 imm <<= 1;
546 /* Since imm >= 128, this must be true. */
547 SLJIT_ASSERT(counter <= 31);
549 if (imm & 0x00ffffff)
550 return INVALID_IMM; /* Cannot be encoded. */
552 return ((imm >> 24) & 0x7f) | COPY_BITS(counter, 4, 26, 1) | COPY_BITS(counter, 1, 12, 3) | COPY_BITS(counter, 0, 7, 1);
555 static sljit_s32 load_immediate(struct sljit_compiler *compiler, sljit_s32 dst, sljit_uw imm)
557 sljit_uw tmp;
559 /* MOVS cannot be used since it destroy flags. */
561 if (imm >= 0x10000) {
562 tmp = get_imm(imm);
563 if (tmp != INVALID_IMM)
564 return push_inst32(compiler, MOV_WI | RD4(dst) | tmp);
565 tmp = get_imm(~imm);
566 if (tmp != INVALID_IMM)
567 return push_inst32(compiler, MVN_WI | RD4(dst) | tmp);
570 /* set low 16 bits, set hi 16 bits to 0. */
571 FAIL_IF(push_inst32(compiler, MOVW | RD4(dst)
572 | COPY_BITS(imm, 12, 16, 4) | COPY_BITS(imm, 11, 26, 1) | COPY_BITS(imm, 8, 12, 3) | (imm & 0xff)));
574 /* set hi 16 bit if needed. */
575 if (imm >= 0x10000)
576 return push_inst32(compiler, MOVT | RD4(dst)
577 | COPY_BITS(imm, 12 + 16, 16, 4) | COPY_BITS(imm, 11 + 16, 26, 1) | COPY_BITS(imm, 8 + 16, 12, 3) | ((imm & 0xff0000) >> 16));
578 return SLJIT_SUCCESS;
581 #define ARG1_IMM 0x0010000
582 #define ARG2_IMM 0x0020000
583 /* SET_FLAGS must be 0x100000 as it is also the value of S bit (can be used for optimization). */
584 #define SET_FLAGS 0x0100000
585 #define UNUSED_RETURN 0x0200000
587 static sljit_s32 emit_op_imm(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 dst, sljit_uw arg1, sljit_uw arg2)
589 /* dst must be register, TMP_REG1
590 arg1 must be register, imm
591 arg2 must be register, imm */
592 sljit_s32 reg;
593 sljit_uw imm, nimm;
595 if (SLJIT_UNLIKELY((flags & (ARG1_IMM | ARG2_IMM)) == (ARG1_IMM | ARG2_IMM))) {
596 /* Both are immediates, no temporaries are used. */
597 flags &= ~ARG1_IMM;
598 FAIL_IF(load_immediate(compiler, TMP_REG1, arg1));
599 arg1 = TMP_REG1;
602 if (flags & (ARG1_IMM | ARG2_IMM)) {
603 reg = (sljit_s32)((flags & ARG2_IMM) ? arg1 : arg2);
604 imm = (flags & ARG2_IMM) ? arg2 : arg1;
606 switch (flags & 0xffff) {
607 case SLJIT_CLZ:
608 case SLJIT_MUL:
609 /* No form with immediate operand. */
610 break;
611 case SLJIT_MOV:
612 SLJIT_ASSERT(!(flags & SET_FLAGS) && (flags & ARG2_IMM) && arg1 == TMP_REG2);
613 return load_immediate(compiler, dst, imm);
614 case SLJIT_NOT:
615 if (!(flags & SET_FLAGS))
616 return load_immediate(compiler, dst, ~imm);
617 /* Since the flags should be set, we just fallback to the register mode.
618 Although some clever things could be done here, "NOT IMM" does not worth the efforts. */
619 break;
620 case SLJIT_ADD:
621 compiler->status_flags_state = SLJIT_CURRENT_FLAGS_ADD_SUB;
622 nimm = NEGATE(imm);
623 if (IS_2_LO_REGS(reg, dst)) {
624 if (imm <= 0x7)
625 return push_inst16(compiler, ADDSI3 | IMM3(imm) | RD3(dst) | RN3(reg));
626 if (nimm <= 0x7)
627 return push_inst16(compiler, SUBSI3 | IMM3(nimm) | RD3(dst) | RN3(reg));
628 if (reg == dst) {
629 if (imm <= 0xff)
630 return push_inst16(compiler, ADDSI8 | IMM8(imm) | RDN3(dst));
631 if (nimm <= 0xff)
632 return push_inst16(compiler, SUBSI8 | IMM8(nimm) | RDN3(dst));
635 if (!(flags & SET_FLAGS)) {
636 if (imm <= 0xfff)
637 return push_inst32(compiler, ADDWI | RD4(dst) | RN4(reg) | IMM12(imm));
638 if (nimm <= 0xfff)
639 return push_inst32(compiler, SUBWI | RD4(dst) | RN4(reg) | IMM12(nimm));
641 nimm = get_imm(imm);
642 if (nimm != INVALID_IMM)
643 return push_inst32(compiler, ADD_WI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | nimm);
644 nimm = get_imm(NEGATE(imm));
645 if (nimm != INVALID_IMM)
646 return push_inst32(compiler, SUB_WI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | nimm);
647 break;
648 case SLJIT_ADDC:
649 imm = get_imm(imm);
650 if (imm != INVALID_IMM)
651 return push_inst32(compiler, ADCI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
652 break;
653 case SLJIT_SUB:
654 /* SUB operation can be replaced by ADD because of the negative carry flag. */
655 compiler->status_flags_state = SLJIT_CURRENT_FLAGS_ADD_SUB;
656 if (flags & ARG1_IMM) {
657 if (imm == 0 && IS_2_LO_REGS(reg, dst))
658 return push_inst16(compiler, RSBSI | RD3(dst) | RN3(reg));
659 imm = get_imm(imm);
660 if (imm != INVALID_IMM)
661 return push_inst32(compiler, RSB_WI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
662 break;
664 if (flags & UNUSED_RETURN) {
665 if (imm <= 0xff && reg_map[reg] <= 7)
666 return push_inst16(compiler, CMPI | IMM8(imm) | RDN3(reg));
667 nimm = get_imm(imm);
668 if (nimm != INVALID_IMM)
669 return push_inst32(compiler, CMPI_W | RN4(reg) | nimm);
670 nimm = get_imm(NEGATE(imm));
671 if (nimm != INVALID_IMM)
672 return push_inst32(compiler, CMNI_W | RN4(reg) | nimm);
674 nimm = NEGATE(imm);
675 if (IS_2_LO_REGS(reg, dst)) {
676 if (imm <= 0x7)
677 return push_inst16(compiler, SUBSI3 | IMM3(imm) | RD3(dst) | RN3(reg));
678 if (nimm <= 0x7)
679 return push_inst16(compiler, ADDSI3 | IMM3(nimm) | RD3(dst) | RN3(reg));
680 if (reg == dst) {
681 if (imm <= 0xff)
682 return push_inst16(compiler, SUBSI8 | IMM8(imm) | RDN3(dst));
683 if (nimm <= 0xff)
684 return push_inst16(compiler, ADDSI8 | IMM8(nimm) | RDN3(dst));
687 if (!(flags & SET_FLAGS)) {
688 if (imm <= 0xfff)
689 return push_inst32(compiler, SUBWI | RD4(dst) | RN4(reg) | IMM12(imm));
690 if (nimm <= 0xfff)
691 return push_inst32(compiler, ADDWI | RD4(dst) | RN4(reg) | IMM12(nimm));
693 nimm = get_imm(imm);
694 if (nimm != INVALID_IMM)
695 return push_inst32(compiler, SUB_WI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | nimm);
696 nimm = get_imm(NEGATE(imm));
697 if (nimm != INVALID_IMM)
698 return push_inst32(compiler, ADD_WI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | nimm);
699 break;
700 case SLJIT_SUBC:
701 if (flags & ARG1_IMM)
702 break;
703 imm = get_imm(imm);
704 if (imm != INVALID_IMM)
705 return push_inst32(compiler, SBCI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
706 break;
707 case SLJIT_AND:
708 nimm = get_imm(imm);
709 if (nimm != INVALID_IMM)
710 return push_inst32(compiler, ((flags & UNUSED_RETURN) ? TSTI : ANDI) | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | nimm);
711 imm = get_imm(~imm);
712 if (imm != INVALID_IMM)
713 return push_inst32(compiler, BICI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
714 break;
715 case SLJIT_OR:
716 nimm = get_imm(imm);
717 if (nimm != INVALID_IMM)
718 return push_inst32(compiler, ORRI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | nimm);
719 imm = get_imm(~imm);
720 if (imm != INVALID_IMM)
721 return push_inst32(compiler, ORNI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
722 break;
723 case SLJIT_XOR:
724 imm = get_imm(imm);
725 if (imm != INVALID_IMM)
726 return push_inst32(compiler, EORI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
727 break;
728 case SLJIT_SHL:
729 case SLJIT_LSHR:
730 case SLJIT_ASHR:
731 if (flags & ARG1_IMM)
732 break;
733 imm &= 0x1f;
734 if (imm == 0) {
735 if (!(flags & SET_FLAGS))
736 return push_inst16(compiler, MOV | SET_REGS44(dst, reg));
737 if (IS_2_LO_REGS(dst, reg))
738 return push_inst16(compiler, MOVS | RD3(dst) | RN3(reg));
739 return push_inst32(compiler, MOV_W | SET_FLAGS | RD4(dst) | RM4(reg));
741 switch (flags & 0xffff) {
742 case SLJIT_SHL:
743 if (IS_2_LO_REGS(dst, reg))
744 return push_inst16(compiler, LSLSI | RD3(dst) | RN3(reg) | (imm << 6));
745 return push_inst32(compiler, LSL_WI | (flags & SET_FLAGS) | RD4(dst) | RM4(reg) | IMM5(imm));
746 case SLJIT_LSHR:
747 if (IS_2_LO_REGS(dst, reg))
748 return push_inst16(compiler, LSRSI | RD3(dst) | RN3(reg) | (imm << 6));
749 return push_inst32(compiler, LSR_WI | (flags & SET_FLAGS) | RD4(dst) | RM4(reg) | IMM5(imm));
750 default: /* SLJIT_ASHR */
751 if (IS_2_LO_REGS(dst, reg))
752 return push_inst16(compiler, ASRSI | RD3(dst) | RN3(reg) | (imm << 6));
753 return push_inst32(compiler, ASR_WI | (flags & SET_FLAGS) | RD4(dst) | RM4(reg) | IMM5(imm));
755 default:
756 SLJIT_UNREACHABLE();
757 break;
760 if (flags & ARG2_IMM) {
761 imm = arg2;
762 arg2 = (arg1 == TMP_REG1) ? TMP_REG2 : TMP_REG1;
763 FAIL_IF(load_immediate(compiler, (sljit_s32)arg2, imm));
765 else {
766 imm = arg1;
767 arg1 = (arg2 == TMP_REG1) ? TMP_REG2 : TMP_REG1;
768 FAIL_IF(load_immediate(compiler, (sljit_s32)arg1, imm));
771 SLJIT_ASSERT(arg1 != arg2);
774 /* Both arguments are registers. */
775 switch (flags & 0xffff) {
776 case SLJIT_MOV:
777 case SLJIT_MOV_U32:
778 case SLJIT_MOV_S32:
779 case SLJIT_MOV32:
780 case SLJIT_MOV_P:
781 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG2);
782 if (dst == (sljit_s32)arg2)
783 return SLJIT_SUCCESS;
784 return push_inst16(compiler, MOV | SET_REGS44(dst, arg2));
785 case SLJIT_MOV_U8:
786 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG2);
787 if (IS_2_LO_REGS(dst, arg2))
788 return push_inst16(compiler, UXTB | RD3(dst) | RN3(arg2));
789 return push_inst32(compiler, UXTB_W | RD4(dst) | RM4(arg2));
790 case SLJIT_MOV_S8:
791 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG2);
792 if (IS_2_LO_REGS(dst, arg2))
793 return push_inst16(compiler, SXTB | RD3(dst) | RN3(arg2));
794 return push_inst32(compiler, SXTB_W | RD4(dst) | RM4(arg2));
795 case SLJIT_MOV_U16:
796 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG2);
797 if (IS_2_LO_REGS(dst, arg2))
798 return push_inst16(compiler, UXTH | RD3(dst) | RN3(arg2));
799 return push_inst32(compiler, UXTH_W | RD4(dst) | RM4(arg2));
800 case SLJIT_MOV_S16:
801 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG2);
802 if (IS_2_LO_REGS(dst, arg2))
803 return push_inst16(compiler, SXTH | RD3(dst) | RN3(arg2));
804 return push_inst32(compiler, SXTH_W | RD4(dst) | RM4(arg2));
805 case SLJIT_NOT:
806 SLJIT_ASSERT(arg1 == TMP_REG2);
807 if (IS_2_LO_REGS(dst, arg2))
808 return push_inst16(compiler, MVNS | RD3(dst) | RN3(arg2));
809 return push_inst32(compiler, MVN_W | (flags & SET_FLAGS) | RD4(dst) | RM4(arg2));
810 case SLJIT_CLZ:
811 SLJIT_ASSERT(arg1 == TMP_REG2);
812 FAIL_IF(push_inst32(compiler, CLZ | RN4(arg2) | RD4(dst) | RM4(arg2)));
813 return SLJIT_SUCCESS;
814 case SLJIT_ADD:
815 compiler->status_flags_state = SLJIT_CURRENT_FLAGS_ADD_SUB;
816 if (IS_3_LO_REGS(dst, arg1, arg2))
817 return push_inst16(compiler, ADDS | RD3(dst) | RN3(arg1) | RM3(arg2));
818 if (dst == (sljit_s32)arg1 && !(flags & SET_FLAGS))
819 return push_inst16(compiler, ADD | SET_REGS44(dst, arg2));
820 return push_inst32(compiler, ADD_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
821 case SLJIT_ADDC:
822 if (dst == (sljit_s32)arg1 && IS_2_LO_REGS(dst, arg2))
823 return push_inst16(compiler, ADCS | RD3(dst) | RN3(arg2));
824 return push_inst32(compiler, ADC_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
825 case SLJIT_SUB:
826 compiler->status_flags_state = SLJIT_CURRENT_FLAGS_ADD_SUB;
827 if (flags & UNUSED_RETURN) {
828 if (IS_2_LO_REGS(arg1, arg2))
829 return push_inst16(compiler, CMP | RD3(arg1) | RN3(arg2));
830 return push_inst16(compiler, CMP_X | SET_REGS44(arg1, arg2));
832 if (IS_3_LO_REGS(dst, arg1, arg2))
833 return push_inst16(compiler, SUBS | RD3(dst) | RN3(arg1) | RM3(arg2));
834 return push_inst32(compiler, SUB_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
835 case SLJIT_SUBC:
836 if (dst == (sljit_s32)arg1 && IS_2_LO_REGS(dst, arg2))
837 return push_inst16(compiler, SBCS | RD3(dst) | RN3(arg2));
838 return push_inst32(compiler, SBC_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
839 case SLJIT_MUL:
840 compiler->status_flags_state = 0;
841 if (!(flags & SET_FLAGS))
842 return push_inst32(compiler, MUL | RD4(dst) | RN4(arg1) | RM4(arg2));
843 SLJIT_ASSERT(dst != TMP_REG2);
844 FAIL_IF(push_inst32(compiler, SMULL | RT4(dst) | RD4(TMP_REG2) | RN4(arg1) | RM4(arg2)));
845 /* cmp TMP_REG2, dst asr #31. */
846 return push_inst32(compiler, CMP_W | RN4(TMP_REG2) | 0x70e0 | RM4(dst));
847 case SLJIT_AND:
848 if (dst == (sljit_s32)arg1 && IS_2_LO_REGS(dst, arg2))
849 return push_inst16(compiler, ANDS | RD3(dst) | RN3(arg2));
850 if ((flags & UNUSED_RETURN) && IS_2_LO_REGS(arg1, arg2))
851 return push_inst16(compiler, TST | RD3(arg1) | RN3(arg2));
852 return push_inst32(compiler, ((flags & UNUSED_RETURN) ? TST_W : AND_W) | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
853 case SLJIT_OR:
854 if (dst == (sljit_s32)arg1 && IS_2_LO_REGS(dst, arg2))
855 return push_inst16(compiler, ORRS | RD3(dst) | RN3(arg2));
856 return push_inst32(compiler, ORR_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
857 case SLJIT_XOR:
858 if (dst == (sljit_s32)arg1 && IS_2_LO_REGS(dst, arg2))
859 return push_inst16(compiler, EORS | RD3(dst) | RN3(arg2));
860 return push_inst32(compiler, EOR_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
861 case SLJIT_SHL:
862 if (dst == (sljit_s32)arg1 && IS_2_LO_REGS(dst, arg2))
863 return push_inst16(compiler, LSLS | RD3(dst) | RN3(arg2));
864 return push_inst32(compiler, LSL_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
865 case SLJIT_LSHR:
866 if (dst == (sljit_s32)arg1 && IS_2_LO_REGS(dst, arg2))
867 return push_inst16(compiler, LSRS | RD3(dst) | RN3(arg2));
868 return push_inst32(compiler, LSR_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
869 case SLJIT_ASHR:
870 if (dst == (sljit_s32)arg1 && IS_2_LO_REGS(dst, arg2))
871 return push_inst16(compiler, ASRS | RD3(dst) | RN3(arg2));
872 return push_inst32(compiler, ASR_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
875 SLJIT_UNREACHABLE();
876 return SLJIT_SUCCESS;
879 #define STORE 0x01
880 #define SIGNED 0x02
882 #define WORD_SIZE 0x00
883 #define BYTE_SIZE 0x04
884 #define HALF_SIZE 0x08
885 #define PRELOAD 0x0c
887 #define IS_WORD_SIZE(flags) (!(flags & (BYTE_SIZE | HALF_SIZE)))
888 #define OFFSET_CHECK(imm, shift) (!(argw & ~(imm << shift)))
891 1st letter:
892 w = word
893 b = byte
894 h = half
896 2nd letter:
897 s = signed
898 u = unsigned
900 3rd letter:
901 l = load
902 s = store
905 static const sljit_ins sljit_mem16[12] = {
906 /* w u l */ 0x5800 /* ldr */,
907 /* w u s */ 0x5000 /* str */,
908 /* w s l */ 0x5800 /* ldr */,
909 /* w s s */ 0x5000 /* str */,
911 /* b u l */ 0x5c00 /* ldrb */,
912 /* b u s */ 0x5400 /* strb */,
913 /* b s l */ 0x5600 /* ldrsb */,
914 /* b s s */ 0x5400 /* strb */,
916 /* h u l */ 0x5a00 /* ldrh */,
917 /* h u s */ 0x5200 /* strh */,
918 /* h s l */ 0x5e00 /* ldrsh */,
919 /* h s s */ 0x5200 /* strh */,
922 static const sljit_ins sljit_mem16_imm5[12] = {
923 /* w u l */ 0x6800 /* ldr imm5 */,
924 /* w u s */ 0x6000 /* str imm5 */,
925 /* w s l */ 0x6800 /* ldr imm5 */,
926 /* w s s */ 0x6000 /* str imm5 */,
928 /* b u l */ 0x7800 /* ldrb imm5 */,
929 /* b u s */ 0x7000 /* strb imm5 */,
930 /* b s l */ 0x0000 /* not allowed */,
931 /* b s s */ 0x7000 /* strb imm5 */,
933 /* h u l */ 0x8800 /* ldrh imm5 */,
934 /* h u s */ 0x8000 /* strh imm5 */,
935 /* h s l */ 0x0000 /* not allowed */,
936 /* h s s */ 0x8000 /* strh imm5 */,
939 #define MEM_IMM8 0xc00
940 #define MEM_IMM12 0x800000
941 static const sljit_ins sljit_mem32[13] = {
942 /* w u l */ 0xf8500000 /* ldr.w */,
943 /* w u s */ 0xf8400000 /* str.w */,
944 /* w s l */ 0xf8500000 /* ldr.w */,
945 /* w s s */ 0xf8400000 /* str.w */,
947 /* b u l */ 0xf8100000 /* ldrb.w */,
948 /* b u s */ 0xf8000000 /* strb.w */,
949 /* b s l */ 0xf9100000 /* ldrsb.w */,
950 /* b s s */ 0xf8000000 /* strb.w */,
952 /* h u l */ 0xf8300000 /* ldrh.w */,
953 /* h u s */ 0xf8200000 /* strsh.w */,
954 /* h s l */ 0xf9300000 /* ldrsh.w */,
955 /* h s s */ 0xf8200000 /* strsh.w */,
957 /* p u l */ 0xf8100000 /* pld */,
960 /* Helper function. Dst should be reg + value, using at most 1 instruction, flags does not set. */
961 static sljit_s32 emit_set_delta(struct sljit_compiler *compiler, sljit_s32 dst, sljit_s32 reg, sljit_sw value)
963 sljit_uw imm;
965 if (value >= 0) {
966 if (value <= 0xfff)
967 return push_inst32(compiler, ADDWI | RD4(dst) | RN4(reg) | IMM12(value));
968 imm = get_imm((sljit_uw)value);
969 if (imm != INVALID_IMM)
970 return push_inst32(compiler, ADD_WI | RD4(dst) | RN4(reg) | imm);
972 else {
973 value = -value;
974 if (value <= 0xfff)
975 return push_inst32(compiler, SUBWI | RD4(dst) | RN4(reg) | IMM12(value));
976 imm = get_imm((sljit_uw)value);
977 if (imm != INVALID_IMM)
978 return push_inst32(compiler, SUB_WI | RD4(dst) | RN4(reg) | imm);
980 return SLJIT_ERR_UNSUPPORTED;
983 static SLJIT_INLINE sljit_s32 emit_op_mem(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg,
984 sljit_s32 arg, sljit_sw argw, sljit_s32 tmp_reg)
986 sljit_s32 other_r;
987 sljit_uw tmp;
989 SLJIT_ASSERT(arg & SLJIT_MEM);
990 SLJIT_ASSERT((arg & REG_MASK) != tmp_reg);
991 arg &= ~SLJIT_MEM;
993 if (SLJIT_UNLIKELY(!(arg & REG_MASK))) {
994 tmp = get_imm((sljit_uw)argw & ~(sljit_uw)0xfff);
995 if (tmp != INVALID_IMM) {
996 FAIL_IF(push_inst32(compiler, MOV_WI | RD4(tmp_reg) | tmp));
997 return push_inst32(compiler, sljit_mem32[flags] | MEM_IMM12 | RT4(reg) | RN4(tmp_reg) | (argw & 0xfff));
1000 FAIL_IF(load_immediate(compiler, tmp_reg, (sljit_uw)argw));
1001 if (IS_2_LO_REGS(reg, tmp_reg) && sljit_mem16_imm5[flags])
1002 return push_inst16(compiler, sljit_mem16_imm5[flags] | RD3(reg) | RN3(tmp_reg));
1003 return push_inst32(compiler, sljit_mem32[flags] | MEM_IMM12 | RT4(reg) | RN4(tmp_reg));
1006 if (SLJIT_UNLIKELY(arg & OFFS_REG_MASK)) {
1007 argw &= 0x3;
1008 other_r = OFFS_REG(arg);
1009 arg &= 0xf;
1011 if (!argw && IS_3_LO_REGS(reg, arg, other_r))
1012 return push_inst16(compiler, sljit_mem16[flags] | RD3(reg) | RN3(arg) | RM3(other_r));
1013 return push_inst32(compiler, sljit_mem32[flags] | RT4(reg) | RN4(arg) | RM4(other_r) | ((sljit_ins)argw << 4));
1016 if (argw > 0xfff) {
1017 tmp = get_imm((sljit_uw)argw & ~(sljit_uw)0xfff);
1018 if (tmp != INVALID_IMM) {
1019 push_inst32(compiler, ADD_WI | RD4(tmp_reg) | RN4(arg) | tmp);
1020 arg = tmp_reg;
1021 argw = argw & 0xfff;
1024 else if (argw < -0xff) {
1025 tmp = get_imm((sljit_uw)-argw & ~(sljit_uw)0xff);
1026 if (tmp != INVALID_IMM) {
1027 push_inst32(compiler, SUB_WI | RD4(tmp_reg) | RN4(arg) | tmp);
1028 arg = tmp_reg;
1029 argw = -(-argw & 0xff);
1033 if (IS_2_LO_REGS(reg, arg) && sljit_mem16_imm5[flags]) {
1034 tmp = 3;
1035 if (IS_WORD_SIZE(flags)) {
1036 if (OFFSET_CHECK(0x1f, 2))
1037 tmp = 2;
1039 else if (flags & BYTE_SIZE)
1041 if (OFFSET_CHECK(0x1f, 0))
1042 tmp = 0;
1044 else {
1045 SLJIT_ASSERT(flags & HALF_SIZE);
1046 if (OFFSET_CHECK(0x1f, 1))
1047 tmp = 1;
1050 if (tmp < 3)
1051 return push_inst16(compiler, sljit_mem16_imm5[flags] | RD3(reg) | RN3(arg) | ((sljit_ins)argw << (6 - tmp)));
1053 else if (SLJIT_UNLIKELY(arg == SLJIT_SP) && IS_WORD_SIZE(flags) && OFFSET_CHECK(0xff, 2) && reg_map[reg] <= 7) {
1054 /* SP based immediate. */
1055 return push_inst16(compiler, STR_SP | (sljit_ins)((flags & STORE) ? 0 : 0x800) | RDN3(reg) | ((sljit_ins)argw >> 2));
1058 if (argw >= 0 && argw <= 0xfff)
1059 return push_inst32(compiler, sljit_mem32[flags] | MEM_IMM12 | RT4(reg) | RN4(arg) | (sljit_ins)argw);
1060 else if (argw < 0 && argw >= -0xff)
1061 return push_inst32(compiler, sljit_mem32[flags] | MEM_IMM8 | RT4(reg) | RN4(arg) | (sljit_ins)-argw);
1063 SLJIT_ASSERT(arg != tmp_reg);
1065 FAIL_IF(load_immediate(compiler, tmp_reg, (sljit_uw)argw));
1066 if (IS_3_LO_REGS(reg, arg, tmp_reg))
1067 return push_inst16(compiler, sljit_mem16[flags] | RD3(reg) | RN3(arg) | RM3(tmp_reg));
1068 return push_inst32(compiler, sljit_mem32[flags] | RT4(reg) | RN4(arg) | RM4(tmp_reg));
1071 /* --------------------------------------------------------------------- */
1072 /* Entry, exit */
1073 /* --------------------------------------------------------------------- */
1075 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_enter(struct sljit_compiler *compiler,
1076 sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
1077 sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
1079 sljit_s32 size, i, tmp, word_arg_count;
1080 sljit_uw offset;
1081 sljit_uw imm = 0;
1082 #ifdef __SOFTFP__
1083 sljit_u32 float_arg_count;
1084 #else
1085 sljit_u32 old_offset, f32_offset;
1086 sljit_u32 remap[3];
1087 sljit_u32 *remap_ptr = remap;
1088 #endif
1089 #ifdef _WIN32
1090 sljit_uw imm;
1091 #endif
1093 CHECK_ERROR();
1094 CHECK(check_sljit_emit_enter(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size));
1095 set_emit_enter(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size);
1097 tmp = saveds < SLJIT_NUMBER_OF_SAVED_REGISTERS ? (SLJIT_S0 + 1 - saveds) : SLJIT_FIRST_SAVED_REG;
1098 for (i = SLJIT_S0; i >= tmp; i--)
1099 imm |= (sljit_uw)1 << reg_map[i];
1101 for (i = scratches; i >= SLJIT_FIRST_SAVED_REG; i--)
1102 imm |= (sljit_uw)1 << reg_map[i];
1104 /* At least two registers must be set for PUSH_W and one for PUSH instruction. */
1105 FAIL_IF((imm & 0xff00)
1106 ? push_inst32(compiler, PUSH_W | (1 << 14) | imm)
1107 : push_inst16(compiler, PUSH | (1 << 8) | imm));
1109 /* Stack must be aligned to 8 bytes: (LR, R4) */
1110 size = GET_SAVED_REGISTERS_SIZE(scratches, saveds, 1);
1111 local_size = ((size + local_size + 7) & ~7) - size;
1112 compiler->local_size = local_size;
1114 arg_types >>= SLJIT_ARG_SHIFT;
1115 word_arg_count = 0;
1116 #ifdef __SOFTFP__
1117 SLJIT_COMPILE_ASSERT(SLJIT_FR0 == 1, float_register_index_start);
1119 offset = 0;
1120 float_arg_count = 0;
1122 while (arg_types) {
1123 switch (arg_types & SLJIT_ARG_MASK) {
1124 case SLJIT_ARG_TYPE_F64:
1125 if (offset & 0x7)
1126 offset += sizeof(sljit_sw);
1128 if (offset < 4 * sizeof(sljit_sw))
1129 FAIL_IF(push_inst32(compiler, VMOV2 | (offset << 10) | ((offset + sizeof(sljit_sw)) << 14) | float_arg_count));
1130 else
1131 FAIL_IF(push_inst32(compiler, VLDR_F32 | 0x800100 | RN4(SLJIT_SP)
1132 | (float_arg_count << 12) | ((offset + (sljit_uw)size - 4 * sizeof(sljit_sw)) >> 2)));
1133 float_arg_count++;
1134 offset += sizeof(sljit_f64);
1135 break;
1136 case SLJIT_ARG_TYPE_F32:
1137 if (offset < 4 * sizeof(sljit_sw))
1138 FAIL_IF(push_inst32(compiler, VMOV | (float_arg_count << 16) | (offset << 10)));
1139 else
1140 FAIL_IF(push_inst32(compiler, VLDR_F32 | 0x800000 | RN4(SLJIT_SP)
1141 | (float_arg_count << 12) | ((offset + (sljit_uw)size - 4 * sizeof(sljit_sw)) >> 2)));
1142 float_arg_count++;
1143 offset += sizeof(sljit_f32);
1144 break;
1145 default:
1146 SLJIT_ASSERT(reg_map[SLJIT_S0 - word_arg_count] <= 7);
1148 if (offset < 4 * sizeof(sljit_sw))
1149 FAIL_IF(push_inst16(compiler, MOV | RD3(SLJIT_S0 - word_arg_count) | (offset << 1)));
1150 else
1151 FAIL_IF(push_inst16(compiler, LDR_SP | RDN3(SLJIT_S0 - word_arg_count)
1152 | ((offset + (sljit_uw)size - 4 * sizeof(sljit_sw)) >> 2)));
1154 word_arg_count++;
1155 offset += sizeof(sljit_sw);
1156 break;
1158 arg_types >>= SLJIT_ARG_SHIFT;
1161 compiler->args_size = offset;
1162 #else
1163 offset = SLJIT_FR0;
1164 old_offset = SLJIT_FR0;
1165 f32_offset = 0;
1167 while (arg_types) {
1168 switch (arg_types & SLJIT_ARG_MASK) {
1169 case SLJIT_ARG_TYPE_F64:
1170 if (offset != old_offset)
1171 *remap_ptr++ = VMOV_F32 | SLJIT_32 | DD4(offset) | DM4(old_offset);
1172 old_offset++;
1173 offset++;
1174 break;
1175 case SLJIT_ARG_TYPE_F32:
1176 if (f32_offset != 0) {
1177 *remap_ptr++ = VMOV_F32 | 0x20 | DD4(offset) | DM4(f32_offset);
1178 f32_offset = 0;
1179 } else {
1180 if (offset != old_offset)
1181 *remap_ptr++ = VMOV_F32 | DD4(offset) | DM4(old_offset);
1182 f32_offset = old_offset;
1183 old_offset++;
1185 offset++;
1186 break;
1187 default:
1188 FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(SLJIT_S0 - word_arg_count, SLJIT_R0 + word_arg_count)));
1189 word_arg_count++;
1190 break;
1192 arg_types >>= SLJIT_ARG_SHIFT;
1195 SLJIT_ASSERT((sljit_uw)(remap_ptr - remap) <= sizeof(remap));
1197 while (remap_ptr > remap)
1198 FAIL_IF(push_inst32(compiler, *(--remap_ptr)));
1199 #endif
1201 #ifdef _WIN32
1202 if (local_size >= 256) {
1203 if (local_size > 4096)
1204 imm = get_imm(4096);
1205 else
1206 imm = get_imm(local_size & ~0xff);
1208 SLJIT_ASSERT(imm != INVALID_IMM);
1209 FAIL_IF(push_inst32(compiler, SUB_WI | RD4(TMP_REG1) | RN4(SLJIT_SP) | imm));
1211 #else
1212 if (local_size > 0) {
1213 if (local_size <= (127 << 2))
1214 FAIL_IF(push_inst16(compiler, SUB_SP_I | ((sljit_uw)local_size >> 2)));
1215 else
1216 FAIL_IF(emit_op_imm(compiler, SLJIT_SUB | ARG2_IMM, SLJIT_SP, SLJIT_SP, (sljit_uw)local_size));
1218 #endif
1220 #ifdef _WIN32
1221 if (local_size >= 256) {
1222 if (local_size > 4096) {
1223 imm = get_imm(4096);
1224 SLJIT_ASSERT(imm != INVALID_IMM);
1226 if (local_size < 4 * 4096) {
1227 if (local_size > 2 * 4096) {
1228 FAIL_IF(push_inst32(compiler, LDRI | 0x400 | RT4(TMP_REG2) | RN4(TMP_REG1)));
1229 FAIL_IF(push_inst32(compiler, SUB_WI | RD4(TMP_REG1) | RN4(TMP_REG1) | imm));
1230 local_size -= 4096;
1233 if (local_size > 2 * 4096) {
1234 FAIL_IF(push_inst32(compiler, LDRI | 0x400 | RT4(TMP_REG2) | RN4(TMP_REG1)));
1235 FAIL_IF(push_inst32(compiler, SUB_WI | RD4(TMP_REG1) | RN4(TMP_REG1) | imm));
1236 local_size -= 4096;
1239 FAIL_IF(push_inst32(compiler, LDRI | 0x400 | RT4(TMP_REG2) | RN4(TMP_REG1)));
1240 local_size -= 4096;
1242 SLJIT_ASSERT(local_size > 0);
1244 else {
1245 FAIL_IF(load_immediate(compiler, SLJIT_R3, (local_size >> 12) - 1));
1246 FAIL_IF(push_inst32(compiler, LDRI | 0x400 | RT4(TMP_REG2) | RN4(TMP_REG1)));
1247 FAIL_IF(push_inst32(compiler, SUB_WI | RD4(TMP_REG1) | RN4(TMP_REG1) | imm));
1248 SLJIT_ASSERT(reg_map[SLJIT_R3] < 7);
1249 FAIL_IF(push_inst16(compiler, SUBSI8 | RDN3(SLJIT_R3) | 1));
1250 FAIL_IF(push_inst16(compiler, BCC | (0x1 << 8) /* not-equal */ | (-7 & 0xff)));
1252 local_size &= 0xfff;
1254 if (local_size != 0)
1255 FAIL_IF(push_inst32(compiler, LDRI | 0x400 | RT4(TMP_REG2) | RN4(TMP_REG1)));
1258 if (local_size >= 256) {
1259 imm = get_imm(local_size & ~0xff);
1260 SLJIT_ASSERT(imm != INVALID_IMM);
1262 FAIL_IF(push_inst32(compiler, SUB_WI | RD4(TMP_REG1) | RN4(TMP_REG1) | imm));
1266 local_size &= 0xff;
1267 FAIL_IF(push_inst32(compiler, LDRI | 0x400 | (local_size > 0 ? 0x100 : 0) | RT4(TMP_REG2) | RN4(TMP_REG1) | local_size));
1269 FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(SLJIT_SP, TMP_REG1)));
1271 else if (local_size > 0)
1272 FAIL_IF(push_inst32(compiler, LDRI | 0x500 | RT4(TMP_REG1) | RN4(SLJIT_SP) | local_size));
1273 #endif
1275 return SLJIT_SUCCESS;
1278 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_set_context(struct sljit_compiler *compiler,
1279 sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
1280 sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
1282 sljit_s32 size;
1284 CHECK_ERROR();
1285 CHECK(check_sljit_set_context(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size));
1286 set_set_context(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size);
1288 size = GET_SAVED_REGISTERS_SIZE(scratches, saveds, 1);
1289 compiler->local_size = ((size + local_size + 7) & ~7) - size;
1290 return SLJIT_SUCCESS;
1293 static sljit_s32 emit_add_sp(struct sljit_compiler *compiler, sljit_uw imm)
1295 sljit_uw imm2;
1297 /* The TMP_REG1 register must keep its value. */
1298 if (imm <= (127u << 2))
1299 return push_inst16(compiler, ADD_SP_I | (imm >> 2));
1301 if (imm <= 0xfff)
1302 return push_inst32(compiler, ADDWI | RD4(SLJIT_SP) | RN4(SLJIT_SP) | IMM12(imm));
1304 imm2 = get_imm(imm);
1306 if (imm2 != INVALID_IMM)
1307 return push_inst32(compiler, ADD_WI | RD4(SLJIT_SP) | RN4(SLJIT_SP) | imm2);
1309 FAIL_IF(load_immediate(compiler, TMP_REG2, imm));
1310 return push_inst16(compiler, ADD_SP | RN3(TMP_REG2));
1313 static sljit_s32 emit_stack_frame_release(struct sljit_compiler *compiler, sljit_s32 frame_size)
1315 sljit_s32 i, tmp;
1316 sljit_s32 lr_dst = TMP_PC;
1317 sljit_uw reg_list;
1319 SLJIT_ASSERT(reg_map[TMP_REG2] == 14 && frame_size <= 128);
1321 if (frame_size < 0) {
1322 lr_dst = TMP_REG2;
1323 frame_size = 0;
1324 } else if (frame_size > 0)
1325 lr_dst = 0;
1327 reg_list = 0;
1328 tmp = compiler->saveds < SLJIT_NUMBER_OF_SAVED_REGISTERS ? (SLJIT_S0 + 1 - compiler->saveds) : SLJIT_FIRST_SAVED_REG;
1329 for (i = SLJIT_S0; i >= tmp; i--)
1330 reg_list |= (sljit_uw)1 << reg_map[i];
1332 for (i = compiler->scratches; i >= SLJIT_FIRST_SAVED_REG; i--)
1333 reg_list |= (sljit_uw)1 << reg_map[i];
1335 tmp = compiler->local_size;
1336 if (lr_dst == 0 && (reg_list & (reg_list - 1)) == 0) {
1337 /* The local_size does not include the saved registers. */
1338 tmp += SSIZE_OF(sw);
1340 if (reg_list != 0)
1341 tmp += SSIZE_OF(sw);
1343 if (frame_size > tmp)
1344 FAIL_IF(push_inst16(compiler, SUB_SP_I | ((sljit_uw)(frame_size - tmp) >> 2)));
1345 else if (frame_size < tmp)
1346 FAIL_IF(emit_add_sp(compiler, (sljit_uw)(tmp - frame_size)));
1348 if (reg_list == 0)
1349 return SLJIT_SUCCESS;
1351 if (compiler->saveds > 0) {
1352 SLJIT_ASSERT(reg_list == ((sljit_uw)1 << reg_map[SLJIT_S0]));
1353 lr_dst = SLJIT_S0;
1354 } else {
1355 SLJIT_ASSERT(reg_list == ((sljit_uw)1 << reg_map[SLJIT_FIRST_SAVED_REG]));
1356 lr_dst = SLJIT_FIRST_SAVED_REG;
1359 frame_size -= 2 * SSIZE_OF(sw);
1361 if (reg_map[lr_dst] <= 7)
1362 return push_inst16(compiler, STR_SP | 0x800 | RDN3(lr_dst) | (sljit_uw)(frame_size >> 2));
1364 return push_inst32(compiler, LDR | RT4(lr_dst) | RN4(SLJIT_SP) | (sljit_uw)frame_size);
1367 if (tmp > 0)
1368 FAIL_IF(emit_add_sp(compiler, (sljit_uw)tmp));
1370 if (!(reg_list & 0xff00) && lr_dst != TMP_REG2) {
1371 if (lr_dst == TMP_PC)
1372 reg_list |= 1u << 8;
1374 if (reg_list == 0)
1375 return SLJIT_SUCCESS;
1377 /* At least one register must be set for POP instruction. */
1378 FAIL_IF(push_inst16(compiler, POP | reg_list));
1379 } else {
1380 if (lr_dst != 0) {
1381 if (reg_list == 0)
1382 return push_inst32(compiler, 0xf85d0b04 | RT4(lr_dst));
1384 reg_list |= (sljit_uw)1 << reg_map[lr_dst];
1387 SLJIT_ASSERT((reg_list & (reg_list - 1)) != 0);
1389 /* At least two registers must be set for POP_W instruction. */
1390 FAIL_IF(push_inst32(compiler, POP_W | reg_list));
1393 if (frame_size > 0)
1394 return push_inst16(compiler, SUB_SP_I | (((sljit_uw)frame_size - sizeof(sljit_sw)) >> 2));
1395 return SLJIT_SUCCESS;
1398 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_return_void(struct sljit_compiler *compiler)
1400 CHECK_ERROR();
1401 CHECK(check_sljit_emit_return_void(compiler));
1403 return emit_stack_frame_release(compiler, 0);
1406 /* --------------------------------------------------------------------- */
1407 /* Operators */
1408 /* --------------------------------------------------------------------- */
1410 #if !(defined __ARM_FEATURE_IDIV) && !(defined __ARM_ARCH_EXT_IDIV__)
1412 #ifdef __cplusplus
1413 extern "C" {
1414 #endif
1416 #ifdef _WIN32
1417 extern unsigned long long __rt_udiv(unsigned int denominator, unsigned int numerator);
1418 extern long long __rt_sdiv(int denominator, int numerator);
1419 #elif defined(__GNUC__)
1420 extern unsigned int __aeabi_uidivmod(unsigned int numerator, int unsigned denominator);
1421 extern int __aeabi_idivmod(int numerator, int denominator);
1422 #else
1423 #error "Software divmod functions are needed"
1424 #endif
1426 #ifdef __cplusplus
1428 #endif
1430 #endif /* !__ARM_FEATURE_IDIV && !__ARM_ARCH_EXT_IDIV__ */
1432 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op0(struct sljit_compiler *compiler, sljit_s32 op)
1434 #if !(defined __ARM_FEATURE_IDIV) && !(defined __ARM_ARCH_EXT_IDIV__)
1435 sljit_uw saved_reg_list[3];
1436 sljit_uw saved_reg_count;
1437 #endif
1439 CHECK_ERROR();
1440 CHECK(check_sljit_emit_op0(compiler, op));
1442 op = GET_OPCODE(op);
1443 switch (op) {
1444 case SLJIT_BREAKPOINT:
1445 return push_inst16(compiler, BKPT);
1446 case SLJIT_NOP:
1447 return push_inst16(compiler, NOP);
1448 case SLJIT_LMUL_UW:
1449 case SLJIT_LMUL_SW:
1450 return push_inst32(compiler, (op == SLJIT_LMUL_UW ? UMULL : SMULL)
1451 | RD4(SLJIT_R1) | RT4(SLJIT_R0) | RN4(SLJIT_R0) | RM4(SLJIT_R1));
1452 #if (defined __ARM_FEATURE_IDIV) || (defined __ARM_ARCH_EXT_IDIV__)
1453 case SLJIT_DIVMOD_UW:
1454 case SLJIT_DIVMOD_SW:
1455 FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(TMP_REG1, SLJIT_R0)));
1456 FAIL_IF(push_inst32(compiler, (op == SLJIT_DIVMOD_UW ? UDIV : SDIV) | RD4(SLJIT_R0) | RN4(SLJIT_R0) | RM4(SLJIT_R1)));
1457 FAIL_IF(push_inst32(compiler, MUL | RD4(SLJIT_R1) | RN4(SLJIT_R0) | RM4(SLJIT_R1)));
1458 return push_inst32(compiler, SUB_W | RD4(SLJIT_R1) | RN4(TMP_REG1) | RM4(SLJIT_R1));
1459 case SLJIT_DIV_UW:
1460 case SLJIT_DIV_SW:
1461 return push_inst32(compiler, (op == SLJIT_DIV_UW ? UDIV : SDIV) | RD4(SLJIT_R0) | RN4(SLJIT_R0) | RM4(SLJIT_R1));
1462 #else /* !__ARM_FEATURE_IDIV && !__ARM_ARCH_EXT_IDIV__ */
1463 case SLJIT_DIVMOD_UW:
1464 case SLJIT_DIVMOD_SW:
1465 case SLJIT_DIV_UW:
1466 case SLJIT_DIV_SW:
1467 SLJIT_COMPILE_ASSERT((SLJIT_DIVMOD_UW & 0x2) == 0 && SLJIT_DIV_UW - 0x2 == SLJIT_DIVMOD_UW, bad_div_opcode_assignments);
1468 SLJIT_ASSERT(reg_map[2] == 1 && reg_map[3] == 2 && reg_map[4] == 3);
1470 saved_reg_count = 0;
1471 if (compiler->scratches >= 4)
1472 saved_reg_list[saved_reg_count++] = 3;
1473 if (compiler->scratches >= 3)
1474 saved_reg_list[saved_reg_count++] = 2;
1475 if (op >= SLJIT_DIV_UW)
1476 saved_reg_list[saved_reg_count++] = 1;
1478 if (saved_reg_count > 0) {
1479 FAIL_IF(push_inst32(compiler, 0xf84d0d00 | (saved_reg_count >= 3 ? 16 : 8)
1480 | (saved_reg_list[0] << 12) /* str rX, [sp, #-8/-16]! */));
1481 if (saved_reg_count >= 2) {
1482 SLJIT_ASSERT(saved_reg_list[1] < 8);
1483 FAIL_IF(push_inst16(compiler, 0x9001 | (saved_reg_list[1] << 8) /* str rX, [sp, #4] */));
1485 if (saved_reg_count >= 3) {
1486 SLJIT_ASSERT(saved_reg_list[2] < 8);
1487 FAIL_IF(push_inst16(compiler, 0x9002 | (saved_reg_list[2] << 8) /* str rX, [sp, #8] */));
1491 #ifdef _WIN32
1492 FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(TMP_REG1, SLJIT_R0)));
1493 FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(SLJIT_R0, SLJIT_R1)));
1494 FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(SLJIT_R1, TMP_REG1)));
1495 FAIL_IF(sljit_emit_ijump(compiler, SLJIT_FAST_CALL, SLJIT_IMM,
1496 ((op | 0x2) == SLJIT_DIV_UW ? SLJIT_FUNC_ADDR(__rt_udiv) : SLJIT_FUNC_ADDR(__rt_sdiv))));
1497 #elif defined(__GNUC__)
1498 FAIL_IF(sljit_emit_ijump(compiler, SLJIT_FAST_CALL, SLJIT_IMM,
1499 ((op | 0x2) == SLJIT_DIV_UW ? SLJIT_FUNC_ADDR(__aeabi_uidivmod) : SLJIT_FUNC_ADDR(__aeabi_idivmod))));
1500 #else
1501 #error "Software divmod functions are needed"
1502 #endif
1504 if (saved_reg_count > 0) {
1505 if (saved_reg_count >= 3) {
1506 SLJIT_ASSERT(saved_reg_list[2] < 8);
1507 FAIL_IF(push_inst16(compiler, 0x9802 | (saved_reg_list[2] << 8) /* ldr rX, [sp, #8] */));
1509 if (saved_reg_count >= 2) {
1510 SLJIT_ASSERT(saved_reg_list[1] < 8);
1511 FAIL_IF(push_inst16(compiler, 0x9801 | (saved_reg_list[1] << 8) /* ldr rX, [sp, #4] */));
1513 return push_inst32(compiler, 0xf85d0b00 | (saved_reg_count >= 3 ? 16 : 8)
1514 | (saved_reg_list[0] << 12) /* ldr rX, [sp], #8/16 */);
1516 return SLJIT_SUCCESS;
1517 #endif /* __ARM_FEATURE_IDIV || __ARM_ARCH_EXT_IDIV__ */
1518 case SLJIT_ENDBR:
1519 case SLJIT_SKIP_FRAMES_BEFORE_RETURN:
1520 return SLJIT_SUCCESS;
1523 return SLJIT_SUCCESS;
1526 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op1(struct sljit_compiler *compiler, sljit_s32 op,
1527 sljit_s32 dst, sljit_sw dstw,
1528 sljit_s32 src, sljit_sw srcw)
1530 sljit_s32 dst_r, flags;
1531 sljit_s32 op_flags = GET_ALL_FLAGS(op);
1533 CHECK_ERROR();
1534 CHECK(check_sljit_emit_op1(compiler, op, dst, dstw, src, srcw));
1535 ADJUST_LOCAL_OFFSET(dst, dstw);
1536 ADJUST_LOCAL_OFFSET(src, srcw);
1538 dst_r = FAST_IS_REG(dst) ? dst : TMP_REG1;
1540 op = GET_OPCODE(op);
1541 if (op >= SLJIT_MOV && op <= SLJIT_MOV_P) {
1542 switch (op) {
1543 case SLJIT_MOV:
1544 case SLJIT_MOV_U32:
1545 case SLJIT_MOV_S32:
1546 case SLJIT_MOV32:
1547 case SLJIT_MOV_P:
1548 flags = WORD_SIZE;
1549 break;
1550 case SLJIT_MOV_U8:
1551 flags = BYTE_SIZE;
1552 if (src & SLJIT_IMM)
1553 srcw = (sljit_u8)srcw;
1554 break;
1555 case SLJIT_MOV_S8:
1556 flags = BYTE_SIZE | SIGNED;
1557 if (src & SLJIT_IMM)
1558 srcw = (sljit_s8)srcw;
1559 break;
1560 case SLJIT_MOV_U16:
1561 flags = HALF_SIZE;
1562 if (src & SLJIT_IMM)
1563 srcw = (sljit_u16)srcw;
1564 break;
1565 case SLJIT_MOV_S16:
1566 flags = HALF_SIZE | SIGNED;
1567 if (src & SLJIT_IMM)
1568 srcw = (sljit_s16)srcw;
1569 break;
1570 default:
1571 SLJIT_UNREACHABLE();
1572 flags = 0;
1573 break;
1576 if (src & SLJIT_IMM)
1577 FAIL_IF(emit_op_imm(compiler, SLJIT_MOV | ARG2_IMM, dst_r, TMP_REG2, (sljit_uw)srcw));
1578 else if (src & SLJIT_MEM) {
1579 FAIL_IF(emit_op_mem(compiler, flags, dst_r, src, srcw, TMP_REG1));
1580 } else {
1581 if (dst_r != TMP_REG1)
1582 return emit_op_imm(compiler, op, dst_r, TMP_REG2, (sljit_uw)src);
1583 dst_r = src;
1586 if (!(dst & SLJIT_MEM))
1587 return SLJIT_SUCCESS;
1589 return emit_op_mem(compiler, flags | STORE, dst_r, dst, dstw, TMP_REG2);
1592 if (op == SLJIT_NEG) {
1593 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
1594 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
1595 compiler->skip_checks = 1;
1596 #endif
1597 return sljit_emit_op2(compiler, SLJIT_SUB | op_flags, dst, dstw, SLJIT_IMM, 0, src, srcw);
1600 flags = HAS_FLAGS(op_flags) ? SET_FLAGS : 0;
1602 if (src & SLJIT_MEM) {
1603 FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG1, src, srcw, TMP_REG1));
1604 src = TMP_REG1;
1607 emit_op_imm(compiler, flags | op, dst_r, TMP_REG2, (sljit_uw)src);
1609 if (SLJIT_UNLIKELY(dst & SLJIT_MEM))
1610 return emit_op_mem(compiler, flags | STORE, dst_r, dst, dstw, TMP_REG2);
1611 return SLJIT_SUCCESS;
1614 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op2(struct sljit_compiler *compiler, sljit_s32 op,
1615 sljit_s32 dst, sljit_sw dstw,
1616 sljit_s32 src1, sljit_sw src1w,
1617 sljit_s32 src2, sljit_sw src2w)
1619 sljit_s32 dst_reg, flags, src2_reg;
1621 CHECK_ERROR();
1622 CHECK(check_sljit_emit_op2(compiler, op, 0, dst, dstw, src1, src1w, src2, src2w));
1623 ADJUST_LOCAL_OFFSET(dst, dstw);
1624 ADJUST_LOCAL_OFFSET(src1, src1w);
1625 ADJUST_LOCAL_OFFSET(src2, src2w);
1627 dst_reg = FAST_IS_REG(dst) ? dst : TMP_REG1;
1628 flags = HAS_FLAGS(op) ? SET_FLAGS : 0;
1630 if (dst == TMP_REG1)
1631 flags |= UNUSED_RETURN;
1633 if (src1 & SLJIT_IMM)
1634 flags |= ARG1_IMM;
1635 else if (src1 & SLJIT_MEM) {
1636 emit_op_mem(compiler, WORD_SIZE, TMP_REG1, src1, src1w, TMP_REG1);
1637 src1w = TMP_REG1;
1639 else
1640 src1w = src1;
1642 if (src2 & SLJIT_IMM)
1643 flags |= ARG2_IMM;
1644 else if (src2 & SLJIT_MEM) {
1645 src2_reg = (!(flags & ARG1_IMM) && (src1w == TMP_REG1)) ? TMP_REG2 : TMP_REG1;
1646 emit_op_mem(compiler, WORD_SIZE, src2_reg, src2, src2w, src2_reg);
1647 src2w = src2_reg;
1649 else
1650 src2w = src2;
1652 emit_op_imm(compiler, flags | GET_OPCODE(op), dst_reg, (sljit_uw)src1w, (sljit_uw)src2w);
1654 if (!(dst & SLJIT_MEM))
1655 return SLJIT_SUCCESS;
1656 return emit_op_mem(compiler, WORD_SIZE | STORE, dst_reg, dst, dstw, TMP_REG2);
1659 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op2u(struct sljit_compiler *compiler, sljit_s32 op,
1660 sljit_s32 src1, sljit_sw src1w,
1661 sljit_s32 src2, sljit_sw src2w)
1663 CHECK_ERROR();
1664 CHECK(check_sljit_emit_op2(compiler, op, 1, 0, 0, src1, src1w, src2, src2w));
1666 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
1667 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
1668 compiler->skip_checks = 1;
1669 #endif
1670 return sljit_emit_op2(compiler, op, TMP_REG1, 0, src1, src1w, src2, src2w);
1673 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_src(struct sljit_compiler *compiler, sljit_s32 op,
1674 sljit_s32 src, sljit_sw srcw)
1676 CHECK_ERROR();
1677 CHECK(check_sljit_emit_op_src(compiler, op, src, srcw));
1678 ADJUST_LOCAL_OFFSET(src, srcw);
1680 switch (op) {
1681 case SLJIT_FAST_RETURN:
1682 SLJIT_ASSERT(reg_map[TMP_REG2] == 14);
1684 if (FAST_IS_REG(src))
1685 FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(TMP_REG2, src)));
1686 else
1687 FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG2, src, srcw, TMP_REG2));
1689 return push_inst16(compiler, BX | RN3(TMP_REG2));
1690 case SLJIT_SKIP_FRAMES_BEFORE_FAST_RETURN:
1691 return SLJIT_SUCCESS;
1692 case SLJIT_PREFETCH_L1:
1693 case SLJIT_PREFETCH_L2:
1694 case SLJIT_PREFETCH_L3:
1695 case SLJIT_PREFETCH_ONCE:
1696 return emit_op_mem(compiler, PRELOAD, TMP_PC, src, srcw, TMP_REG1);
1699 return SLJIT_SUCCESS;
1702 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_register_index(sljit_s32 reg)
1704 CHECK_REG_INDEX(check_sljit_get_register_index(reg));
1705 return reg_map[reg];
1708 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_float_register_index(sljit_s32 reg)
1710 CHECK_REG_INDEX(check_sljit_get_float_register_index(reg));
1711 return (freg_map[reg] << 1);
1714 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_custom(struct sljit_compiler *compiler,
1715 void *instruction, sljit_u32 size)
1717 CHECK_ERROR();
1718 CHECK(check_sljit_emit_op_custom(compiler, instruction, size));
1720 if (size == 2)
1721 return push_inst16(compiler, *(sljit_u16*)instruction);
1722 return push_inst32(compiler, *(sljit_ins*)instruction);
1725 /* --------------------------------------------------------------------- */
1726 /* Floating point operators */
1727 /* --------------------------------------------------------------------- */
1729 #define FPU_LOAD (1 << 20)
1731 static sljit_s32 emit_fop_mem(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg, sljit_s32 arg, sljit_sw argw)
1733 sljit_uw imm;
1734 sljit_ins inst = VSTR_F32 | (flags & (SLJIT_32 | FPU_LOAD));
1736 SLJIT_ASSERT(arg & SLJIT_MEM);
1738 /* Fast loads and stores. */
1739 if (SLJIT_UNLIKELY(arg & OFFS_REG_MASK)) {
1740 FAIL_IF(push_inst32(compiler, ADD_W | RD4(TMP_REG1) | RN4(arg & REG_MASK) | RM4(OFFS_REG(arg)) | (((sljit_uw)argw & 0x3) << 6)));
1741 arg = SLJIT_MEM | TMP_REG1;
1742 argw = 0;
1745 if ((arg & REG_MASK) && (argw & 0x3) == 0) {
1746 if (!(argw & ~0x3fc))
1747 return push_inst32(compiler, inst | 0x800000 | RN4(arg & REG_MASK) | DD4(reg) | ((sljit_uw)argw >> 2));
1748 if (!(-argw & ~0x3fc))
1749 return push_inst32(compiler, inst | RN4(arg & REG_MASK) | DD4(reg) | ((sljit_uw)-argw >> 2));
1752 if (arg & REG_MASK) {
1753 if (emit_set_delta(compiler, TMP_REG1, arg & REG_MASK, argw) != SLJIT_ERR_UNSUPPORTED) {
1754 FAIL_IF(compiler->error);
1755 return push_inst32(compiler, inst | 0x800000 | RN4(TMP_REG1) | DD4(reg));
1758 imm = get_imm((sljit_uw)argw & ~(sljit_uw)0x3fc);
1759 if (imm != INVALID_IMM) {
1760 FAIL_IF(push_inst32(compiler, ADD_WI | RD4(TMP_REG1) | RN4(arg & REG_MASK) | imm));
1761 return push_inst32(compiler, inst | 0x800000 | RN4(TMP_REG1) | DD4(reg) | (((sljit_uw)argw & 0x3fc) >> 2));
1764 imm = get_imm((sljit_uw)-argw & ~(sljit_uw)0x3fc);
1765 if (imm != INVALID_IMM) {
1766 argw = -argw;
1767 FAIL_IF(push_inst32(compiler, SUB_WI | RD4(TMP_REG1) | RN4(arg & REG_MASK) | imm));
1768 return push_inst32(compiler, inst | RN4(TMP_REG1) | DD4(reg) | (((sljit_uw)argw & 0x3fc) >> 2));
1772 FAIL_IF(load_immediate(compiler, TMP_REG1, (sljit_uw)argw));
1773 if (arg & REG_MASK)
1774 FAIL_IF(push_inst16(compiler, ADD | SET_REGS44(TMP_REG1, (arg & REG_MASK))));
1775 return push_inst32(compiler, inst | 0x800000 | RN4(TMP_REG1) | DD4(reg));
1778 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_conv_sw_from_f64(struct sljit_compiler *compiler, sljit_s32 op,
1779 sljit_s32 dst, sljit_sw dstw,
1780 sljit_s32 src, sljit_sw srcw)
1782 op ^= SLJIT_32;
1784 if (src & SLJIT_MEM) {
1785 FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_32) | FPU_LOAD, TMP_FREG1, src, srcw));
1786 src = TMP_FREG1;
1789 FAIL_IF(push_inst32(compiler, VCVT_S32_F32 | (op & SLJIT_32) | DD4(TMP_FREG1) | DM4(src)));
1791 if (FAST_IS_REG(dst))
1792 return push_inst32(compiler, VMOV | (1 << 20) | RT4(dst) | DN4(TMP_FREG1));
1794 /* Store the integer value from a VFP register. */
1795 return emit_fop_mem(compiler, 0, TMP_FREG1, dst, dstw);
1798 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_conv_f64_from_sw(struct sljit_compiler *compiler, sljit_s32 op,
1799 sljit_s32 dst, sljit_sw dstw,
1800 sljit_s32 src, sljit_sw srcw)
1802 sljit_s32 dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
1804 op ^= SLJIT_32;
1806 if (FAST_IS_REG(src))
1807 FAIL_IF(push_inst32(compiler, VMOV | RT4(src) | DN4(TMP_FREG1)));
1808 else if (src & SLJIT_MEM) {
1809 /* Load the integer value into a VFP register. */
1810 FAIL_IF(emit_fop_mem(compiler, FPU_LOAD, TMP_FREG1, src, srcw));
1812 else {
1813 FAIL_IF(load_immediate(compiler, TMP_REG1, (sljit_uw)srcw));
1814 FAIL_IF(push_inst32(compiler, VMOV | RT4(TMP_REG1) | DN4(TMP_FREG1)));
1817 FAIL_IF(push_inst32(compiler, VCVT_F32_S32 | (op & SLJIT_32) | DD4(dst_r) | DM4(TMP_FREG1)));
1819 if (dst & SLJIT_MEM)
1820 return emit_fop_mem(compiler, (op & SLJIT_32), TMP_FREG1, dst, dstw);
1821 return SLJIT_SUCCESS;
1824 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_cmp(struct sljit_compiler *compiler, sljit_s32 op,
1825 sljit_s32 src1, sljit_sw src1w,
1826 sljit_s32 src2, sljit_sw src2w)
1828 op ^= SLJIT_32;
1830 if (src1 & SLJIT_MEM) {
1831 emit_fop_mem(compiler, (op & SLJIT_32) | FPU_LOAD, TMP_FREG1, src1, src1w);
1832 src1 = TMP_FREG1;
1835 if (src2 & SLJIT_MEM) {
1836 emit_fop_mem(compiler, (op & SLJIT_32) | FPU_LOAD, TMP_FREG2, src2, src2w);
1837 src2 = TMP_FREG2;
1840 FAIL_IF(push_inst32(compiler, VCMP_F32 | (op & SLJIT_32) | DD4(src1) | DM4(src2)));
1841 return push_inst32(compiler, VMRS);
1844 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop1(struct sljit_compiler *compiler, sljit_s32 op,
1845 sljit_s32 dst, sljit_sw dstw,
1846 sljit_s32 src, sljit_sw srcw)
1848 sljit_s32 dst_r;
1850 CHECK_ERROR();
1852 SLJIT_COMPILE_ASSERT((SLJIT_32 == 0x100), float_transfer_bit_error);
1853 SELECT_FOP1_OPERATION_WITH_CHECKS(compiler, op, dst, dstw, src, srcw);
1855 dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
1857 if (GET_OPCODE(op) != SLJIT_CONV_F64_FROM_F32)
1858 op ^= SLJIT_32;
1860 if (src & SLJIT_MEM) {
1861 emit_fop_mem(compiler, (op & SLJIT_32) | FPU_LOAD, dst_r, src, srcw);
1862 src = dst_r;
1865 switch (GET_OPCODE(op)) {
1866 case SLJIT_MOV_F64:
1867 if (src != dst_r) {
1868 if (dst_r != TMP_FREG1)
1869 FAIL_IF(push_inst32(compiler, VMOV_F32 | (op & SLJIT_32) | DD4(dst_r) | DM4(src)));
1870 else
1871 dst_r = src;
1873 break;
1874 case SLJIT_NEG_F64:
1875 FAIL_IF(push_inst32(compiler, VNEG_F32 | (op & SLJIT_32) | DD4(dst_r) | DM4(src)));
1876 break;
1877 case SLJIT_ABS_F64:
1878 FAIL_IF(push_inst32(compiler, VABS_F32 | (op & SLJIT_32) | DD4(dst_r) | DM4(src)));
1879 break;
1880 case SLJIT_CONV_F64_FROM_F32:
1881 FAIL_IF(push_inst32(compiler, VCVT_F64_F32 | (op & SLJIT_32) | DD4(dst_r) | DM4(src)));
1882 op ^= SLJIT_32;
1883 break;
1886 if (dst & SLJIT_MEM)
1887 return emit_fop_mem(compiler, (op & SLJIT_32), dst_r, dst, dstw);
1888 return SLJIT_SUCCESS;
1891 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop2(struct sljit_compiler *compiler, sljit_s32 op,
1892 sljit_s32 dst, sljit_sw dstw,
1893 sljit_s32 src1, sljit_sw src1w,
1894 sljit_s32 src2, sljit_sw src2w)
1896 sljit_s32 dst_r;
1898 CHECK_ERROR();
1899 CHECK(check_sljit_emit_fop2(compiler, op, dst, dstw, src1, src1w, src2, src2w));
1900 ADJUST_LOCAL_OFFSET(dst, dstw);
1901 ADJUST_LOCAL_OFFSET(src1, src1w);
1902 ADJUST_LOCAL_OFFSET(src2, src2w);
1904 op ^= SLJIT_32;
1906 dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
1907 if (src1 & SLJIT_MEM) {
1908 emit_fop_mem(compiler, (op & SLJIT_32) | FPU_LOAD, TMP_FREG1, src1, src1w);
1909 src1 = TMP_FREG1;
1911 if (src2 & SLJIT_MEM) {
1912 emit_fop_mem(compiler, (op & SLJIT_32) | FPU_LOAD, TMP_FREG2, src2, src2w);
1913 src2 = TMP_FREG2;
1916 switch (GET_OPCODE(op)) {
1917 case SLJIT_ADD_F64:
1918 FAIL_IF(push_inst32(compiler, VADD_F32 | (op & SLJIT_32) | DD4(dst_r) | DN4(src1) | DM4(src2)));
1919 break;
1920 case SLJIT_SUB_F64:
1921 FAIL_IF(push_inst32(compiler, VSUB_F32 | (op & SLJIT_32) | DD4(dst_r) | DN4(src1) | DM4(src2)));
1922 break;
1923 case SLJIT_MUL_F64:
1924 FAIL_IF(push_inst32(compiler, VMUL_F32 | (op & SLJIT_32) | DD4(dst_r) | DN4(src1) | DM4(src2)));
1925 break;
1926 case SLJIT_DIV_F64:
1927 FAIL_IF(push_inst32(compiler, VDIV_F32 | (op & SLJIT_32) | DD4(dst_r) | DN4(src1) | DM4(src2)));
1928 break;
1931 if (!(dst & SLJIT_MEM))
1932 return SLJIT_SUCCESS;
1933 return emit_fop_mem(compiler, (op & SLJIT_32), TMP_FREG1, dst, dstw);
1936 #undef FPU_LOAD
1938 /* --------------------------------------------------------------------- */
1939 /* Other instructions */
1940 /* --------------------------------------------------------------------- */
1942 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fast_enter(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw)
1944 CHECK_ERROR();
1945 CHECK(check_sljit_emit_fast_enter(compiler, dst, dstw));
1946 ADJUST_LOCAL_OFFSET(dst, dstw);
1948 SLJIT_ASSERT(reg_map[TMP_REG2] == 14);
1950 if (FAST_IS_REG(dst))
1951 return push_inst16(compiler, MOV | SET_REGS44(dst, TMP_REG2));
1953 /* Memory. */
1954 return emit_op_mem(compiler, WORD_SIZE | STORE, TMP_REG2, dst, dstw, TMP_REG1);
1957 /* --------------------------------------------------------------------- */
1958 /* Conditional instructions */
1959 /* --------------------------------------------------------------------- */
1961 static sljit_uw get_cc(struct sljit_compiler *compiler, sljit_s32 type)
1963 switch (type) {
1964 case SLJIT_EQUAL:
1965 case SLJIT_EQUAL_F64:
1966 return 0x0;
1968 case SLJIT_NOT_EQUAL:
1969 case SLJIT_NOT_EQUAL_F64:
1970 return 0x1;
1972 case SLJIT_LESS:
1973 case SLJIT_LESS_F64:
1974 return 0x3;
1976 case SLJIT_GREATER_EQUAL:
1977 case SLJIT_GREATER_EQUAL_F64:
1978 return 0x2;
1980 case SLJIT_GREATER:
1981 case SLJIT_GREATER_F64:
1982 return 0x8;
1984 case SLJIT_LESS_EQUAL:
1985 case SLJIT_LESS_EQUAL_F64:
1986 return 0x9;
1988 case SLJIT_SIG_LESS:
1989 return 0xb;
1991 case SLJIT_SIG_GREATER_EQUAL:
1992 return 0xa;
1994 case SLJIT_SIG_GREATER:
1995 return 0xc;
1997 case SLJIT_SIG_LESS_EQUAL:
1998 return 0xd;
2000 case SLJIT_OVERFLOW:
2001 if (!(compiler->status_flags_state & SLJIT_CURRENT_FLAGS_ADD_SUB))
2002 return 0x1;
2004 case SLJIT_UNORDERED_F64:
2005 return 0x6;
2007 case SLJIT_NOT_OVERFLOW:
2008 if (!(compiler->status_flags_state & SLJIT_CURRENT_FLAGS_ADD_SUB))
2009 return 0x0;
2011 case SLJIT_ORDERED_F64:
2012 return 0x7;
2014 default: /* SLJIT_JUMP */
2015 SLJIT_UNREACHABLE();
2016 return 0xe;
2020 SLJIT_API_FUNC_ATTRIBUTE struct sljit_label* sljit_emit_label(struct sljit_compiler *compiler)
2022 struct sljit_label *label;
2024 CHECK_ERROR_PTR();
2025 CHECK_PTR(check_sljit_emit_label(compiler));
2027 if (compiler->last_label && compiler->last_label->size == compiler->size)
2028 return compiler->last_label;
2030 label = (struct sljit_label*)ensure_abuf(compiler, sizeof(struct sljit_label));
2031 PTR_FAIL_IF(!label);
2032 set_label(label, compiler);
2033 return label;
2036 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_jump(struct sljit_compiler *compiler, sljit_s32 type)
2038 struct sljit_jump *jump;
2039 sljit_ins cc;
2041 CHECK_ERROR_PTR();
2042 CHECK_PTR(check_sljit_emit_jump(compiler, type));
2044 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
2045 PTR_FAIL_IF(!jump);
2046 set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
2047 type &= 0xff;
2049 PTR_FAIL_IF(emit_imm32_const(compiler, TMP_REG1, 0));
2050 if (type < SLJIT_JUMP) {
2051 jump->flags |= IS_COND;
2052 cc = get_cc(compiler, type);
2053 jump->flags |= cc << 8;
2054 PTR_FAIL_IF(push_inst16(compiler, IT | (cc << 4) | 0x8));
2057 jump->addr = compiler->size;
2058 if (type <= SLJIT_JUMP)
2059 PTR_FAIL_IF(push_inst16(compiler, BX | RN3(TMP_REG1)));
2060 else {
2061 jump->flags |= IS_BL;
2062 PTR_FAIL_IF(push_inst16(compiler, BLX | RN3(TMP_REG1)));
2065 return jump;
2068 #ifdef __SOFTFP__
2070 static sljit_s32 softfloat_call_with_args(struct sljit_compiler *compiler, sljit_s32 arg_types, sljit_s32 *src, sljit_u32 *extra_space)
2072 sljit_u32 is_tail_call = *extra_space & SLJIT_TAIL_CALL;
2073 sljit_u32 offset = 0;
2074 sljit_u32 word_arg_offset = 0;
2075 sljit_u32 float_arg_count = 0;
2076 sljit_s32 types = 0;
2077 sljit_u32 src_offset = 4 * sizeof(sljit_sw);
2078 sljit_u8 offsets[4];
2079 sljit_u8 *offset_ptr = offsets;
2081 if (src && FAST_IS_REG(*src))
2082 src_offset = (sljit_u32)reg_map[*src] * sizeof(sljit_sw);
2084 arg_types >>= SLJIT_ARG_SHIFT;
2086 while (arg_types) {
2087 types = (types << SLJIT_ARG_SHIFT) | (arg_types & SLJIT_ARG_MASK);
2089 switch (arg_types & SLJIT_ARG_MASK) {
2090 case SLJIT_ARG_TYPE_F64:
2091 if (offset & 0x7)
2092 offset += sizeof(sljit_sw);
2093 *offset_ptr++ = (sljit_u8)offset;
2094 offset += sizeof(sljit_f64);
2095 float_arg_count++;
2096 break;
2097 case SLJIT_ARG_TYPE_F32:
2098 *offset_ptr++ = (sljit_u8)offset;
2099 offset += sizeof(sljit_f32);
2100 float_arg_count++;
2101 break;
2102 default:
2103 *offset_ptr++ = (sljit_u8)offset;
2104 offset += sizeof(sljit_sw);
2105 word_arg_offset += sizeof(sljit_sw);
2106 break;
2109 arg_types >>= SLJIT_ARG_SHIFT;
2112 if (offset > 4 * sizeof(sljit_sw) && (!is_tail_call || offset > compiler->args_size)) {
2113 /* Keep lr register on the stack. */
2114 if (is_tail_call)
2115 offset += sizeof(sljit_sw);
2117 offset = ((offset - 4 * sizeof(sljit_sw)) + 0x7) & ~(sljit_uw)0x7;
2119 *extra_space = offset;
2121 if (is_tail_call)
2122 FAIL_IF(emit_stack_frame_release(compiler, (sljit_s32)offset));
2123 else
2124 FAIL_IF(push_inst16(compiler, SUB_SP_I | (offset >> 2)));
2125 } else {
2126 if (is_tail_call)
2127 FAIL_IF(emit_stack_frame_release(compiler, -1));
2128 *extra_space = 0;
2131 SLJIT_ASSERT(reg_map[TMP_REG1] == 12);
2133 /* Process arguments in reversed direction. */
2134 while (types) {
2135 switch (types & SLJIT_ARG_MASK) {
2136 case SLJIT_ARG_TYPE_F64:
2137 float_arg_count--;
2138 offset = *(--offset_ptr);
2140 SLJIT_ASSERT((offset & 0x7) == 0);
2142 if (offset < 4 * sizeof(sljit_sw)) {
2143 if (src_offset == offset || src_offset == offset + sizeof(sljit_sw)) {
2144 FAIL_IF(push_inst16(compiler, MOV | (src_offset << 1) | 4 | (1 << 7)));
2145 *src = TMP_REG1;
2147 FAIL_IF(push_inst32(compiler, VMOV2 | 0x100000 | (offset << 10) | ((offset + sizeof(sljit_sw)) << 14) | float_arg_count));
2148 } else
2149 FAIL_IF(push_inst32(compiler, VSTR_F32 | 0x800100 | RN4(SLJIT_SP)
2150 | (float_arg_count << 12) | ((offset - 4 * sizeof(sljit_sw)) >> 2)));
2151 break;
2152 case SLJIT_ARG_TYPE_F32:
2153 float_arg_count--;
2154 offset = *(--offset_ptr);
2156 if (offset < 4 * sizeof(sljit_sw)) {
2157 if (src_offset == offset) {
2158 FAIL_IF(push_inst16(compiler, MOV | (src_offset << 1) | 4 | (1 << 7)));
2159 *src = TMP_REG1;
2161 FAIL_IF(push_inst32(compiler, VMOV | 0x100000 | (float_arg_count << 16) | (offset << 10)));
2162 } else
2163 FAIL_IF(push_inst32(compiler, VSTR_F32 | 0x800000 | RN4(SLJIT_SP)
2164 | (float_arg_count << 12) | ((offset - 4 * sizeof(sljit_sw)) >> 2)));
2165 break;
2166 default:
2167 word_arg_offset -= sizeof(sljit_sw);
2168 offset = *(--offset_ptr);
2170 SLJIT_ASSERT(offset >= word_arg_offset);
2172 if (offset != word_arg_offset) {
2173 if (offset < 4 * sizeof(sljit_sw)) {
2174 if (src_offset == offset) {
2175 FAIL_IF(push_inst16(compiler, MOV | (src_offset << 1) | 4 | (1 << 7)));
2176 *src = TMP_REG1;
2178 else if (src_offset == word_arg_offset) {
2179 *src = (sljit_s32)(1 + (offset >> 2));
2180 src_offset = offset;
2182 FAIL_IF(push_inst16(compiler, MOV | (offset >> 2) | (word_arg_offset << 1)));
2183 } else
2184 FAIL_IF(push_inst16(compiler, STR_SP | (word_arg_offset << 6) | ((offset - 4 * sizeof(sljit_sw)) >> 2)));
2186 break;
2189 types >>= SLJIT_ARG_SHIFT;
2192 return SLJIT_SUCCESS;
2195 static sljit_s32 softfloat_post_call_with_args(struct sljit_compiler *compiler, sljit_s32 arg_types)
2197 if ((arg_types & SLJIT_ARG_MASK) == SLJIT_ARG_TYPE_F64)
2198 FAIL_IF(push_inst32(compiler, VMOV2 | (1 << 16) | (0 << 12) | 0));
2199 if ((arg_types & SLJIT_ARG_MASK) == SLJIT_ARG_TYPE_F32)
2200 FAIL_IF(push_inst32(compiler, VMOV | (0 << 16) | (0 << 12)));
2202 return SLJIT_SUCCESS;
2205 #else
2207 static sljit_s32 hardfloat_call_with_args(struct sljit_compiler *compiler, sljit_s32 arg_types)
2209 sljit_u32 offset = SLJIT_FR0;
2210 sljit_u32 new_offset = SLJIT_FR0;
2211 sljit_u32 f32_offset = 0;
2213 /* Remove return value. */
2214 arg_types >>= SLJIT_ARG_SHIFT;
2216 while (arg_types) {
2217 switch (arg_types & SLJIT_ARG_MASK) {
2218 case SLJIT_ARG_TYPE_F64:
2219 if (offset != new_offset)
2220 FAIL_IF(push_inst32(compiler, VMOV_F32 | SLJIT_32 | DD4(new_offset) | DM4(offset)));
2222 new_offset++;
2223 offset++;
2224 break;
2225 case SLJIT_ARG_TYPE_F32:
2226 if (f32_offset != 0) {
2227 FAIL_IF(push_inst32(compiler, VMOV_F32 | 0x400000 | DD4(f32_offset) | DM4(offset)));
2228 f32_offset = 0;
2229 } else {
2230 if (offset != new_offset)
2231 FAIL_IF(push_inst32(compiler, VMOV_F32 | 0x400000 | DD4(new_offset) | DM4(offset)));
2232 f32_offset = new_offset;
2233 new_offset++;
2235 offset++;
2236 break;
2238 arg_types >>= SLJIT_ARG_SHIFT;
2241 return SLJIT_SUCCESS;
2244 #endif
2246 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_call(struct sljit_compiler *compiler, sljit_s32 type,
2247 sljit_s32 arg_types)
2249 #ifdef __SOFTFP__
2250 struct sljit_jump *jump;
2251 sljit_u32 extra_space = (sljit_u32)type;
2252 #endif
2254 CHECK_ERROR_PTR();
2255 CHECK_PTR(check_sljit_emit_call(compiler, type, arg_types));
2257 #ifdef __SOFTFP__
2258 PTR_FAIL_IF(softfloat_call_with_args(compiler, arg_types, NULL, &extra_space));
2259 SLJIT_ASSERT((extra_space & 0x7) == 0);
2261 if ((type & SLJIT_TAIL_CALL) && extra_space == 0)
2262 type = SLJIT_JUMP | (type & SLJIT_REWRITABLE_JUMP);
2264 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2265 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2266 compiler->skip_checks = 1;
2267 #endif
2269 jump = sljit_emit_jump(compiler, type);
2270 PTR_FAIL_IF(jump == NULL);
2272 if (extra_space > 0) {
2273 if (type & SLJIT_TAIL_CALL)
2274 PTR_FAIL_IF(push_inst32(compiler, LDR | RT4(TMP_REG2)
2275 | RN4(SLJIT_SP) | (extra_space - sizeof(sljit_sw))));
2277 PTR_FAIL_IF(push_inst16(compiler, ADD_SP_I | (extra_space >> 2)));
2279 if (type & SLJIT_TAIL_CALL) {
2280 PTR_FAIL_IF(push_inst16(compiler, BX | RN3(TMP_REG2)));
2281 return jump;
2285 SLJIT_ASSERT(!(type & SLJIT_TAIL_CALL));
2286 PTR_FAIL_IF(softfloat_post_call_with_args(compiler, arg_types));
2287 return jump;
2288 #else
2289 if (type & SLJIT_TAIL_CALL) {
2290 /* ldmia sp!, {..., lr} */
2291 PTR_FAIL_IF(emit_stack_frame_release(compiler, -1));
2292 type = SLJIT_JUMP | (type & SLJIT_REWRITABLE_JUMP);
2295 PTR_FAIL_IF(hardfloat_call_with_args(compiler, arg_types));
2297 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2298 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2299 compiler->skip_checks = 1;
2300 #endif
2302 return sljit_emit_jump(compiler, type);
2303 #endif
2306 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_ijump(struct sljit_compiler *compiler, sljit_s32 type, sljit_s32 src, sljit_sw srcw)
2308 struct sljit_jump *jump;
2310 CHECK_ERROR();
2311 CHECK(check_sljit_emit_ijump(compiler, type, src, srcw));
2312 ADJUST_LOCAL_OFFSET(src, srcw);
2314 SLJIT_ASSERT(reg_map[TMP_REG1] != 14);
2316 if (!(src & SLJIT_IMM)) {
2317 if (FAST_IS_REG(src)) {
2318 SLJIT_ASSERT(reg_map[src] != 14);
2319 return push_inst16(compiler, (type <= SLJIT_JUMP ? BX : BLX) | RN3(src));
2322 FAIL_IF(emit_op_mem(compiler, WORD_SIZE, type <= SLJIT_JUMP ? TMP_PC : TMP_REG1, src, srcw, TMP_REG1));
2323 if (type >= SLJIT_FAST_CALL)
2324 return push_inst16(compiler, BLX | RN3(TMP_REG1));
2327 /* These jumps are converted to jump/call instructions when possible. */
2328 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
2329 FAIL_IF(!jump);
2330 set_jump(jump, compiler, JUMP_ADDR | ((type >= SLJIT_FAST_CALL) ? IS_BL : 0));
2331 jump->u.target = (sljit_uw)srcw;
2333 FAIL_IF(emit_imm32_const(compiler, TMP_REG1, 0));
2334 jump->addr = compiler->size;
2335 return push_inst16(compiler, (type <= SLJIT_JUMP ? BX : BLX) | RN3(TMP_REG1));
2338 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_icall(struct sljit_compiler *compiler, sljit_s32 type,
2339 sljit_s32 arg_types,
2340 sljit_s32 src, sljit_sw srcw)
2342 #ifdef __SOFTFP__
2343 sljit_u32 extra_space = (sljit_u32)type;
2344 #endif
2346 CHECK_ERROR();
2347 CHECK(check_sljit_emit_icall(compiler, type, arg_types, src, srcw));
2349 if (src & SLJIT_MEM) {
2350 FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG1, src, srcw, TMP_REG1));
2351 src = TMP_REG1;
2354 if ((type & SLJIT_TAIL_CALL) && (src >= SLJIT_FIRST_SAVED_REG && src <= SLJIT_S0)) {
2355 FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(TMP_REG1, src)));
2356 src = TMP_REG1;
2359 #ifdef __SOFTFP__
2360 FAIL_IF(softfloat_call_with_args(compiler, arg_types, &src, &extra_space));
2361 SLJIT_ASSERT((extra_space & 0x7) == 0);
2363 if ((type & SLJIT_TAIL_CALL) && extra_space == 0)
2364 type = SLJIT_JUMP;
2366 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2367 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2368 compiler->skip_checks = 1;
2369 #endif
2371 FAIL_IF(sljit_emit_ijump(compiler, type, src, srcw));
2373 if (extra_space > 0) {
2374 if (type & SLJIT_TAIL_CALL)
2375 FAIL_IF(push_inst32(compiler, LDR | RT4(TMP_REG2)
2376 | RN4(SLJIT_SP) | (extra_space - sizeof(sljit_sw))));
2378 FAIL_IF(push_inst16(compiler, ADD_SP_I | (extra_space >> 2)));
2380 if (type & SLJIT_TAIL_CALL)
2381 return push_inst16(compiler, BX | RN3(TMP_REG2));
2384 SLJIT_ASSERT(!(type & SLJIT_TAIL_CALL));
2385 return softfloat_post_call_with_args(compiler, arg_types);
2386 #else /* !__SOFTFP__ */
2387 if (type & SLJIT_TAIL_CALL) {
2388 /* ldmia sp!, {..., lr} */
2389 FAIL_IF(emit_stack_frame_release(compiler, -1));
2390 type = SLJIT_JUMP;
2393 FAIL_IF(hardfloat_call_with_args(compiler, arg_types));
2395 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2396 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2397 compiler->skip_checks = 1;
2398 #endif
2400 return sljit_emit_ijump(compiler, type, src, srcw);
2401 #endif /* __SOFTFP__ */
2404 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_flags(struct sljit_compiler *compiler, sljit_s32 op,
2405 sljit_s32 dst, sljit_sw dstw,
2406 sljit_s32 type)
2408 sljit_s32 dst_r, flags = GET_ALL_FLAGS(op);
2409 sljit_ins cc;
2411 CHECK_ERROR();
2412 CHECK(check_sljit_emit_op_flags(compiler, op, dst, dstw, type));
2413 ADJUST_LOCAL_OFFSET(dst, dstw);
2415 op = GET_OPCODE(op);
2416 cc = get_cc(compiler, type & 0xff);
2417 dst_r = FAST_IS_REG(dst) ? dst : TMP_REG1;
2419 if (op < SLJIT_ADD) {
2420 FAIL_IF(push_inst16(compiler, IT | (cc << 4) | (((cc & 0x1) ^ 0x1) << 3) | 0x4));
2421 if (reg_map[dst_r] > 7) {
2422 FAIL_IF(push_inst32(compiler, MOV_WI | RD4(dst_r) | 1));
2423 FAIL_IF(push_inst32(compiler, MOV_WI | RD4(dst_r) | 0));
2424 } else {
2425 /* The movsi (immediate) instruction does not set flags in IT block. */
2426 FAIL_IF(push_inst16(compiler, MOVSI | RDN3(dst_r) | 1));
2427 FAIL_IF(push_inst16(compiler, MOVSI | RDN3(dst_r) | 0));
2429 if (!(dst & SLJIT_MEM))
2430 return SLJIT_SUCCESS;
2431 return emit_op_mem(compiler, WORD_SIZE | STORE, TMP_REG1, dst, dstw, TMP_REG2);
2434 if (dst & SLJIT_MEM)
2435 FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG1, dst, dstw, TMP_REG2));
2437 if (op == SLJIT_AND) {
2438 FAIL_IF(push_inst16(compiler, IT | (cc << 4) | (((cc & 0x1) ^ 0x1) << 3) | 0x4));
2439 FAIL_IF(push_inst32(compiler, ANDI | RN4(dst_r) | RD4(dst_r) | 1));
2440 FAIL_IF(push_inst32(compiler, ANDI | RN4(dst_r) | RD4(dst_r) | 0));
2442 else {
2443 FAIL_IF(push_inst16(compiler, IT | (cc << 4) | 0x8));
2444 FAIL_IF(push_inst32(compiler, ((op == SLJIT_OR) ? ORRI : EORI) | RN4(dst_r) | RD4(dst_r) | 1));
2447 if (dst & SLJIT_MEM)
2448 FAIL_IF(emit_op_mem(compiler, WORD_SIZE | STORE, TMP_REG1, dst, dstw, TMP_REG2));
2450 if (!(flags & SLJIT_SET_Z))
2451 return SLJIT_SUCCESS;
2453 /* The condition must always be set, even if the ORR/EORI is not executed above. */
2454 return push_inst32(compiler, MOV_W | SET_FLAGS | RD4(TMP_REG1) | RM4(dst_r));
2457 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_cmov(struct sljit_compiler *compiler, sljit_s32 type,
2458 sljit_s32 dst_reg,
2459 sljit_s32 src, sljit_sw srcw)
2461 sljit_uw cc, tmp;
2463 CHECK_ERROR();
2464 CHECK(check_sljit_emit_cmov(compiler, type, dst_reg, src, srcw));
2466 dst_reg &= ~SLJIT_32;
2468 cc = get_cc(compiler, type & 0xff);
2470 if (!(src & SLJIT_IMM)) {
2471 FAIL_IF(push_inst16(compiler, IT | (cc << 4) | 0x8));
2472 return push_inst16(compiler, MOV | SET_REGS44(dst_reg, src));
2475 tmp = (sljit_uw) srcw;
2477 if (tmp < 0x10000) {
2478 /* set low 16 bits, set hi 16 bits to 0. */
2479 FAIL_IF(push_inst16(compiler, IT | (cc << 4) | 0x8));
2480 return push_inst32(compiler, MOVW | RD4(dst_reg)
2481 | COPY_BITS(tmp, 12, 16, 4) | COPY_BITS(tmp, 11, 26, 1) | COPY_BITS(tmp, 8, 12, 3) | (tmp & 0xff));
2484 tmp = get_imm((sljit_uw)srcw);
2485 if (tmp != INVALID_IMM) {
2486 FAIL_IF(push_inst16(compiler, IT | (cc << 4) | 0x8));
2487 return push_inst32(compiler, MOV_WI | RD4(dst_reg) | tmp);
2490 tmp = get_imm(~(sljit_uw)srcw);
2491 if (tmp != INVALID_IMM) {
2492 FAIL_IF(push_inst16(compiler, IT | (cc << 4) | 0x8));
2493 return push_inst32(compiler, MVN_WI | RD4(dst_reg) | tmp);
2496 FAIL_IF(push_inst16(compiler, IT | (cc << 4) | ((cc & 0x1) << 3) | 0x4));
2498 tmp = (sljit_uw) srcw;
2499 FAIL_IF(push_inst32(compiler, MOVW | RD4(dst_reg)
2500 | COPY_BITS(tmp, 12, 16, 4) | COPY_BITS(tmp, 11, 26, 1) | COPY_BITS(tmp, 8, 12, 3) | (tmp & 0xff)));
2501 return push_inst32(compiler, MOVT | RD4(dst_reg)
2502 | COPY_BITS(tmp, 12 + 16, 16, 4) | COPY_BITS(tmp, 11 + 16, 26, 1) | COPY_BITS(tmp, 8 + 16, 12, 3) | ((tmp & 0xff0000) >> 16));
2505 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_mem(struct sljit_compiler *compiler, sljit_s32 type,
2506 sljit_s32 reg,
2507 sljit_s32 mem, sljit_sw memw)
2509 sljit_s32 flags;
2510 sljit_ins inst;
2512 CHECK_ERROR();
2513 CHECK(check_sljit_emit_mem(compiler, type, reg, mem, memw));
2515 if ((mem & OFFS_REG_MASK) || (memw > 255 || memw < -255))
2516 return SLJIT_ERR_UNSUPPORTED;
2518 if (type & SLJIT_MEM_SUPP)
2519 return SLJIT_SUCCESS;
2521 switch (type & 0xff) {
2522 case SLJIT_MOV:
2523 case SLJIT_MOV_U32:
2524 case SLJIT_MOV_S32:
2525 case SLJIT_MOV32:
2526 case SLJIT_MOV_P:
2527 flags = WORD_SIZE;
2528 break;
2529 case SLJIT_MOV_U8:
2530 flags = BYTE_SIZE;
2531 break;
2532 case SLJIT_MOV_S8:
2533 flags = BYTE_SIZE | SIGNED;
2534 break;
2535 case SLJIT_MOV_U16:
2536 flags = HALF_SIZE;
2537 break;
2538 case SLJIT_MOV_S16:
2539 flags = HALF_SIZE | SIGNED;
2540 break;
2541 default:
2542 SLJIT_UNREACHABLE();
2543 flags = WORD_SIZE;
2544 break;
2547 if (type & SLJIT_MEM_STORE)
2548 flags |= STORE;
2550 inst = sljit_mem32[flags] | 0x900;
2552 if (type & SLJIT_MEM_PRE)
2553 inst |= 0x400;
2555 if (memw >= 0)
2556 inst |= 0x200;
2557 else
2558 memw = -memw;
2560 return push_inst32(compiler, inst | RT4(reg) | RN4(mem & REG_MASK) | (sljit_ins)memw);
2563 SLJIT_API_FUNC_ATTRIBUTE struct sljit_const* sljit_emit_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw, sljit_sw init_value)
2565 struct sljit_const *const_;
2566 sljit_s32 dst_r;
2568 CHECK_ERROR_PTR();
2569 CHECK_PTR(check_sljit_emit_const(compiler, dst, dstw, init_value));
2570 ADJUST_LOCAL_OFFSET(dst, dstw);
2572 const_ = (struct sljit_const*)ensure_abuf(compiler, sizeof(struct sljit_const));
2573 PTR_FAIL_IF(!const_);
2574 set_const(const_, compiler);
2576 dst_r = FAST_IS_REG(dst) ? dst : TMP_REG1;
2577 PTR_FAIL_IF(emit_imm32_const(compiler, dst_r, (sljit_uw)init_value));
2579 if (dst & SLJIT_MEM)
2580 PTR_FAIL_IF(emit_op_mem(compiler, WORD_SIZE | STORE, dst_r, dst, dstw, TMP_REG2));
2581 return const_;
2584 SLJIT_API_FUNC_ATTRIBUTE struct sljit_put_label* sljit_emit_put_label(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw)
2586 struct sljit_put_label *put_label;
2587 sljit_s32 dst_r;
2589 CHECK_ERROR_PTR();
2590 CHECK_PTR(check_sljit_emit_put_label(compiler, dst, dstw));
2591 ADJUST_LOCAL_OFFSET(dst, dstw);
2593 put_label = (struct sljit_put_label*)ensure_abuf(compiler, sizeof(struct sljit_put_label));
2594 PTR_FAIL_IF(!put_label);
2595 set_put_label(put_label, compiler, 0);
2597 dst_r = FAST_IS_REG(dst) ? dst : TMP_REG1;
2598 PTR_FAIL_IF(emit_imm32_const(compiler, dst_r, 0));
2600 if (dst & SLJIT_MEM)
2601 PTR_FAIL_IF(emit_op_mem(compiler, WORD_SIZE | STORE, dst_r, dst, dstw, TMP_REG2));
2602 return put_label;
2605 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_target, sljit_sw executable_offset)
2607 sljit_u16 *inst = (sljit_u16*)addr;
2608 SLJIT_UNUSED_ARG(executable_offset);
2610 SLJIT_UPDATE_WX_FLAGS(inst, inst + 4, 0);
2611 modify_imm32_const(inst, new_target);
2612 SLJIT_UPDATE_WX_FLAGS(inst, inst + 4, 1);
2613 inst = (sljit_u16 *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
2614 SLJIT_CACHE_FLUSH(inst, inst + 4);
2617 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_const(sljit_uw addr, sljit_sw new_constant, sljit_sw executable_offset)
2619 sljit_set_jump_addr(addr, (sljit_uw)new_constant, executable_offset);