1 /*
2 * Stack-less Just-In-Time compiler
3 *
4 * Copyright Zoltan Herczeg (hzmester@freemail.hu). All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without modification, are
7 * permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice, this list of
10 * conditions and the following disclaimer.
11 *
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.
15 *
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.
25 */
26
sljit_get_platform_name(void)27 SLJIT_API_FUNC_ATTRIBUTE const char* sljit_get_platform_name(void)
28 {
29 #ifdef __SOFTFP__
30 return "ARM-Thumb2" SLJIT_CPUINFO " ABI:softfp";
31 #else
32 return "ARM-Thumb2" SLJIT_CPUINFO " ABI:hardfp";
33 #endif
34 }
35
36 /* Length of an instruction word. */
37 typedef sljit_u32 sljit_ins;
38
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)
43
44 #define TMP_FREG1 (SLJIT_NUMBER_OF_FLOAT_REGISTERS + 1)
45 #define TMP_FREG2 (SLJIT_NUMBER_OF_FLOAT_REGISTERS + 2)
46
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
50 };
51
52 static const sljit_u8 freg_map[SLJIT_NUMBER_OF_FLOAT_REGISTERS + 3] = {
53 0, 0, 1, 2, 3, 4, 5, 6, 7
54 };
55
56 #define COPY_BITS(src, from, to, bits) \
57 ((from >= to ? (src >> (from - to)) : (src << (to - from))) & (((1 << bits) - 1) << to))
58
59 /* Thumb16 encodings. */
60 #define RD3(rd) (reg_map[rd])
61 #define RN3(rn) (reg_map[rn] << 3)
62 #define RM3(rm) (reg_map[rm] << 6)
63 #define RDN3(rdn) (reg_map[rdn] << 8)
64 #define IMM3(imm) (imm << 6)
65 #define IMM8(imm) (imm)
66
67 /* Thumb16 helpers. */
68 #define SET_REGS44(rd, rn) \
69 ((reg_map[rn] << 3) | (reg_map[rd] & 0x7) | ((reg_map[rd] & 0x8) << 4))
70 #define IS_2_LO_REGS(reg1, reg2) \
71 (reg_map[reg1] <= 7 && reg_map[reg2] <= 7)
72 #define IS_3_LO_REGS(reg1, reg2, reg3) \
73 (reg_map[reg1] <= 7 && reg_map[reg2] <= 7 && reg_map[reg3] <= 7)
74
75 /* Thumb32 encodings. */
76 #define RD4(rd) (reg_map[rd] << 8)
77 #define RN4(rn) (reg_map[rn] << 16)
78 #define RM4(rm) (reg_map[rm])
79 #define RT4(rt) (reg_map[rt] << 12)
80 #define DD4(dd) (freg_map[dd] << 12)
81 #define DN4(dn) (freg_map[dn] << 16)
82 #define DM4(dm) (freg_map[dm])
83 #define IMM5(imm) \
84 (COPY_BITS(imm, 2, 12, 3) | ((imm & 0x3) << 6))
85 #define IMM12(imm) \
86 (COPY_BITS(imm, 11, 26, 1) | COPY_BITS(imm, 8, 12, 3) | (imm & 0xff))
87
88 /* --------------------------------------------------------------------- */
89 /* Instrucion forms */
90 /* --------------------------------------------------------------------- */
91
92 /* dot '.' changed to _
93 I immediate form (possibly followed by number of immediate bits). */
94 #define ADCI 0xf1400000
95 #define ADCS 0x4140
96 #define ADC_W 0xeb400000
97 #define ADD 0x4400
98 #define ADDS 0x1800
99 #define ADDSI3 0x1c00
100 #define ADDSI8 0x3000
101 #define ADD_W 0xeb000000
102 #define ADDWI 0xf2000000
103 #define ADD_SP 0xb000
104 #define ADD_W 0xeb000000
105 #define ADD_WI 0xf1000000
106 #define ANDI 0xf0000000
107 #define ANDS 0x4000
108 #define AND_W 0xea000000
109 #define ASRS 0x4100
110 #define ASRSI 0x1000
111 #define ASR_W 0xfa40f000
112 #define ASR_WI 0xea4f0020
113 #define BCC 0xd000
114 #define BICI 0xf0200000
115 #define BKPT 0xbe00
116 #define BLX 0x4780
117 #define BX 0x4700
118 #define CLZ 0xfab0f080
119 #define CMNI_W 0xf1100f00
120 #define CMP 0x4280
121 #define CMPI 0x2800
122 #define CMPI_W 0xf1b00f00
123 #define CMP_X 0x4500
124 #define CMP_W 0xebb00f00
125 #define EORI 0xf0800000
126 #define EORS 0x4040
127 #define EOR_W 0xea800000
128 #define IT 0xbf00
129 #define LDRI 0xf8500800
130 #define LSLS 0x4080
131 #define LSLSI 0x0000
132 #define LSL_W 0xfa00f000
133 #define LSL_WI 0xea4f0000
134 #define LSRS 0x40c0
135 #define LSRSI 0x0800
136 #define LSR_W 0xfa20f000
137 #define LSR_WI 0xea4f0010
138 #define MOV 0x4600
139 #define MOVS 0x0000
140 #define MOVSI 0x2000
141 #define MOVT 0xf2c00000
142 #define MOVW 0xf2400000
143 #define MOV_W 0xea4f0000
144 #define MOV_WI 0xf04f0000
145 #define MUL 0xfb00f000
146 #define MVNS 0x43c0
147 #define MVN_W 0xea6f0000
148 #define MVN_WI 0xf06f0000
149 #define NOP 0xbf00
150 #define ORNI 0xf0600000
151 #define ORRI 0xf0400000
152 #define ORRS 0x4300
153 #define ORR_W 0xea400000
154 #define POP 0xbc00
155 #define POP_W 0xe8bd0000
156 #define PUSH 0xb400
157 #define PUSH_W 0xe92d0000
158 #define RSB_WI 0xf1c00000
159 #define RSBSI 0x4240
160 #define SBCI 0xf1600000
161 #define SBCS 0x4180
162 #define SBC_W 0xeb600000
163 #define SDIV 0xfb90f0f0
164 #define SMULL 0xfb800000
165 #define STR_SP 0x9000
166 #define SUBS 0x1a00
167 #define SUBSI3 0x1e00
168 #define SUBSI8 0x3800
169 #define SUB_W 0xeba00000
170 #define SUBWI 0xf2a00000
171 #define SUB_SP 0xb080
172 #define SUB_WI 0xf1a00000
173 #define SXTB 0xb240
174 #define SXTB_W 0xfa4ff080
175 #define SXTH 0xb200
176 #define SXTH_W 0xfa0ff080
177 #define TST 0x4200
178 #define UDIV 0xfbb0f0f0
179 #define UMULL 0xfba00000
180 #define UXTB 0xb2c0
181 #define UXTB_W 0xfa5ff080
182 #define UXTH 0xb280
183 #define UXTH_W 0xfa1ff080
184 #define VABS_F32 0xeeb00ac0
185 #define VADD_F32 0xee300a00
186 #define VCMP_F32 0xeeb40a40
187 #define VCVT_F32_S32 0xeeb80ac0
188 #define VCVT_F64_F32 0xeeb70ac0
189 #define VCVT_S32_F32 0xeebd0ac0
190 #define VDIV_F32 0xee800a00
191 #define VMOV_F32 0xeeb00a40
192 #define VMOV 0xee000a10
193 #define VMOV2 0xec400a10
194 #define VMRS 0xeef1fa10
195 #define VMUL_F32 0xee200a00
196 #define VNEG_F32 0xeeb10a40
197 #define VSTR_F32 0xed000a00
198 #define VSUB_F32 0xee300a40
199
push_inst16(struct sljit_compiler * compiler,sljit_ins inst)200 static sljit_s32 push_inst16(struct sljit_compiler *compiler, sljit_ins inst)
201 {
202 sljit_u16 *ptr;
203 SLJIT_ASSERT(!(inst & 0xffff0000));
204
205 ptr = (sljit_u16*)ensure_buf(compiler, sizeof(sljit_u16));
206 FAIL_IF(!ptr);
207 *ptr = inst;
208 compiler->size++;
209 return SLJIT_SUCCESS;
210 }
211
push_inst32(struct sljit_compiler * compiler,sljit_ins inst)212 static sljit_s32 push_inst32(struct sljit_compiler *compiler, sljit_ins inst)
213 {
214 sljit_u16 *ptr = (sljit_u16*)ensure_buf(compiler, sizeof(sljit_ins));
215 FAIL_IF(!ptr);
216 *ptr++ = inst >> 16;
217 *ptr = inst;
218 compiler->size += 2;
219 return SLJIT_SUCCESS;
220 }
221
emit_imm32_const(struct sljit_compiler * compiler,sljit_s32 dst,sljit_uw imm)222 static SLJIT_INLINE sljit_s32 emit_imm32_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_uw imm)
223 {
224 FAIL_IF(push_inst32(compiler, MOVW | RD4(dst)
225 | COPY_BITS(imm, 12, 16, 4) | COPY_BITS(imm, 11, 26, 1) | COPY_BITS(imm, 8, 12, 3) | (imm & 0xff)));
226 return push_inst32(compiler, MOVT | RD4(dst)
227 | COPY_BITS(imm, 12 + 16, 16, 4) | COPY_BITS(imm, 11 + 16, 26, 1) | COPY_BITS(imm, 8 + 16, 12, 3) | ((imm & 0xff0000) >> 16));
228 }
229
modify_imm32_const(sljit_u16 * inst,sljit_uw new_imm)230 static SLJIT_INLINE void modify_imm32_const(sljit_u16 *inst, sljit_uw new_imm)
231 {
232 sljit_s32 dst = inst[1] & 0x0f00;
233 SLJIT_ASSERT(((inst[0] & 0xfbf0) == (MOVW >> 16)) && ((inst[2] & 0xfbf0) == (MOVT >> 16)) && dst == (inst[3] & 0x0f00));
234 inst[0] = (MOVW >> 16) | COPY_BITS(new_imm, 12, 0, 4) | COPY_BITS(new_imm, 11, 10, 1);
235 inst[1] = dst | COPY_BITS(new_imm, 8, 12, 3) | (new_imm & 0xff);
236 inst[2] = (MOVT >> 16) | COPY_BITS(new_imm, 12 + 16, 0, 4) | COPY_BITS(new_imm, 11 + 16, 10, 1);
237 inst[3] = dst | COPY_BITS(new_imm, 8 + 16, 12, 3) | ((new_imm & 0xff0000) >> 16);
238 }
239
detect_jump_type(struct sljit_jump * jump,sljit_u16 * code_ptr,sljit_u16 * code,sljit_sw executable_offset)240 static SLJIT_INLINE sljit_s32 detect_jump_type(struct sljit_jump *jump, sljit_u16 *code_ptr, sljit_u16 *code, sljit_sw executable_offset)
241 {
242 sljit_sw diff;
243
244 if (jump->flags & SLJIT_REWRITABLE_JUMP)
245 return 0;
246
247 if (jump->flags & JUMP_ADDR) {
248 /* Branch to ARM code is not optimized yet. */
249 if (!(jump->u.target & 0x1))
250 return 0;
251 diff = ((sljit_sw)jump->u.target - (sljit_sw)(code_ptr + 2) - executable_offset) >> 1;
252 }
253 else {
254 SLJIT_ASSERT(jump->flags & JUMP_LABEL);
255 diff = ((sljit_sw)(code + jump->u.label->size) - (sljit_sw)(code_ptr + 2)) >> 1;
256 }
257
258 if (jump->flags & IS_COND) {
259 SLJIT_ASSERT(!(jump->flags & IS_BL));
260 if (diff <= 127 && diff >= -128) {
261 jump->flags |= PATCH_TYPE1;
262 return 5;
263 }
264 if (diff <= 524287 && diff >= -524288) {
265 jump->flags |= PATCH_TYPE2;
266 return 4;
267 }
268 /* +1 comes from the prefix IT instruction. */
269 diff--;
270 if (diff <= 8388607 && diff >= -8388608) {
271 jump->flags |= PATCH_TYPE3;
272 return 3;
273 }
274 }
275 else if (jump->flags & IS_BL) {
276 if (diff <= 8388607 && diff >= -8388608) {
277 jump->flags |= PATCH_BL;
278 return 3;
279 }
280 }
281 else {
282 if (diff <= 1023 && diff >= -1024) {
283 jump->flags |= PATCH_TYPE4;
284 return 4;
285 }
286 if (diff <= 8388607 && diff >= -8388608) {
287 jump->flags |= PATCH_TYPE5;
288 return 3;
289 }
290 }
291
292 return 0;
293 }
294
set_jump_instruction(struct sljit_jump * jump,sljit_sw executable_offset)295 static SLJIT_INLINE void set_jump_instruction(struct sljit_jump *jump, sljit_sw executable_offset)
296 {
297 sljit_s32 type = (jump->flags >> 4) & 0xf;
298 sljit_sw diff;
299 sljit_u16 *jump_inst;
300 sljit_s32 s, j1, j2;
301
302 if (SLJIT_UNLIKELY(type == 0)) {
303 modify_imm32_const((sljit_u16*)jump->addr, (jump->flags & JUMP_LABEL) ? jump->u.label->addr : jump->u.target);
304 return;
305 }
306
307 if (jump->flags & JUMP_ADDR) {
308 SLJIT_ASSERT(jump->u.target & 0x1);
309 diff = ((sljit_sw)jump->u.target - (sljit_sw)(jump->addr + sizeof(sljit_u32)) - executable_offset) >> 1;
310 }
311 else {
312 SLJIT_ASSERT(jump->u.label->addr & 0x1);
313 diff = ((sljit_sw)(jump->u.label->addr) - (sljit_sw)(jump->addr + sizeof(sljit_u32)) - executable_offset) >> 1;
314 }
315 jump_inst = (sljit_u16*)jump->addr;
316
317 switch (type) {
318 case 1:
319 /* Encoding T1 of 'B' instruction */
320 SLJIT_ASSERT(diff <= 127 && diff >= -128 && (jump->flags & IS_COND));
321 jump_inst[0] = 0xd000 | (jump->flags & 0xf00) | (diff & 0xff);
322 return;
323 case 2:
324 /* Encoding T3 of 'B' instruction */
325 SLJIT_ASSERT(diff <= 524287 && diff >= -524288 && (jump->flags & IS_COND));
326 jump_inst[0] = 0xf000 | COPY_BITS(jump->flags, 8, 6, 4) | COPY_BITS(diff, 11, 0, 6) | COPY_BITS(diff, 19, 10, 1);
327 jump_inst[1] = 0x8000 | COPY_BITS(diff, 17, 13, 1) | COPY_BITS(diff, 18, 11, 1) | (diff & 0x7ff);
328 return;
329 case 3:
330 SLJIT_ASSERT(jump->flags & IS_COND);
331 *jump_inst++ = IT | ((jump->flags >> 4) & 0xf0) | 0x8;
332 diff--;
333 type = 5;
334 break;
335 case 4:
336 /* Encoding T2 of 'B' instruction */
337 SLJIT_ASSERT(diff <= 1023 && diff >= -1024 && !(jump->flags & IS_COND));
338 jump_inst[0] = 0xe000 | (diff & 0x7ff);
339 return;
340 }
341
342 SLJIT_ASSERT(diff <= 8388607 && diff >= -8388608);
343
344 /* Really complex instruction form for branches. */
345 s = (diff >> 23) & 0x1;
346 j1 = (~(diff >> 22) ^ s) & 0x1;
347 j2 = (~(diff >> 21) ^ s) & 0x1;
348 jump_inst[0] = 0xf000 | (s << 10) | COPY_BITS(diff, 11, 0, 10);
349 jump_inst[1] = (j1 << 13) | (j2 << 11) | (diff & 0x7ff);
350
351 /* The others have a common form. */
352 if (type == 5) /* Encoding T4 of 'B' instruction */
353 jump_inst[1] |= 0x9000;
354 else if (type == 6) /* Encoding T1 of 'BL' instruction */
355 jump_inst[1] |= 0xd000;
356 else
357 SLJIT_UNREACHABLE();
358 }
359
sljit_generate_code(struct sljit_compiler * compiler)360 SLJIT_API_FUNC_ATTRIBUTE void* sljit_generate_code(struct sljit_compiler *compiler)
361 {
362 struct sljit_memory_fragment *buf;
363 sljit_u16 *code;
364 sljit_u16 *code_ptr;
365 sljit_u16 *buf_ptr;
366 sljit_u16 *buf_end;
367 sljit_uw half_count;
368 sljit_sw executable_offset;
369
370 struct sljit_label *label;
371 struct sljit_jump *jump;
372 struct sljit_const *const_;
373
374 CHECK_ERROR_PTR();
375 CHECK_PTR(check_sljit_generate_code(compiler));
376 reverse_buf(compiler);
377
378 code = (sljit_u16*)SLJIT_MALLOC_EXEC(compiler->size * sizeof(sljit_u16));
379 PTR_FAIL_WITH_EXEC_IF(code);
380 buf = compiler->buf;
381
382 code_ptr = code;
383 half_count = 0;
384 executable_offset = SLJIT_EXEC_OFFSET(code);
385
386 label = compiler->labels;
387 jump = compiler->jumps;
388 const_ = compiler->consts;
389
390 do {
391 buf_ptr = (sljit_u16*)buf->memory;
392 buf_end = buf_ptr + (buf->used_size >> 1);
393 do {
394 *code_ptr = *buf_ptr++;
395 /* These structures are ordered by their address. */
396 SLJIT_ASSERT(!label || label->size >= half_count);
397 SLJIT_ASSERT(!jump || jump->addr >= half_count);
398 SLJIT_ASSERT(!const_ || const_->addr >= half_count);
399 if (label && label->size == half_count) {
400 label->addr = ((sljit_uw)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset)) | 0x1;
401 label->size = code_ptr - code;
402 label = label->next;
403 }
404 if (jump && jump->addr == half_count) {
405 jump->addr = (sljit_uw)code_ptr - ((jump->flags & IS_COND) ? 10 : 8);
406 code_ptr -= detect_jump_type(jump, code_ptr, code, executable_offset);
407 jump = jump->next;
408 }
409 if (const_ && const_->addr == half_count) {
410 const_->addr = (sljit_uw)code_ptr;
411 const_ = const_->next;
412 }
413 code_ptr ++;
414 half_count ++;
415 } while (buf_ptr < buf_end);
416
417 buf = buf->next;
418 } while (buf);
419
420 if (label && label->size == half_count) {
421 label->addr = ((sljit_uw)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset)) | 0x1;
422 label->size = code_ptr - code;
423 label = label->next;
424 }
425
426 SLJIT_ASSERT(!label);
427 SLJIT_ASSERT(!jump);
428 SLJIT_ASSERT(!const_);
429 SLJIT_ASSERT(code_ptr - code <= (sljit_sw)compiler->size);
430
431 jump = compiler->jumps;
432 while (jump) {
433 set_jump_instruction(jump, executable_offset);
434 jump = jump->next;
435 }
436
437 compiler->error = SLJIT_ERR_COMPILED;
438 compiler->executable_offset = executable_offset;
439 compiler->executable_size = (code_ptr - code) * sizeof(sljit_u16);
440
441 code = (sljit_u16 *)SLJIT_ADD_EXEC_OFFSET(code, executable_offset);
442 code_ptr = (sljit_u16 *)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset);
443
444 SLJIT_CACHE_FLUSH(code, code_ptr);
445 /* Set thumb mode flag. */
446 return (void*)((sljit_uw)code | 0x1);
447 }
448
sljit_has_cpu_feature(sljit_s32 feature_type)449 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_has_cpu_feature(sljit_s32 feature_type)
450 {
451 switch (feature_type) {
452 case SLJIT_HAS_FPU:
453 #ifdef SLJIT_IS_FPU_AVAILABLE
454 return SLJIT_IS_FPU_AVAILABLE;
455 #else
456 /* Available by default. */
457 return 1;
458 #endif
459
460 case SLJIT_HAS_CLZ:
461 case SLJIT_HAS_CMOV:
462 return 1;
463
464 default:
465 return 0;
466 }
467 }
468
469 /* --------------------------------------------------------------------- */
470 /* Core code generator functions. */
471 /* --------------------------------------------------------------------- */
472
473 #define INVALID_IMM 0x80000000
get_imm(sljit_uw imm)474 static sljit_uw get_imm(sljit_uw imm)
475 {
476 /* Thumb immediate form. */
477 sljit_s32 counter;
478
479 if (imm <= 0xff)
480 return imm;
481
482 if ((imm & 0xffff) == (imm >> 16)) {
483 /* Some special cases. */
484 if (!(imm & 0xff00))
485 return (1 << 12) | (imm & 0xff);
486 if (!(imm & 0xff))
487 return (2 << 12) | ((imm >> 8) & 0xff);
488 if ((imm & 0xff00) == ((imm & 0xff) << 8))
489 return (3 << 12) | (imm & 0xff);
490 }
491
492 /* Assembly optimization: count leading zeroes? */
493 counter = 8;
494 if (!(imm & 0xffff0000)) {
495 counter += 16;
496 imm <<= 16;
497 }
498 if (!(imm & 0xff000000)) {
499 counter += 8;
500 imm <<= 8;
501 }
502 if (!(imm & 0xf0000000)) {
503 counter += 4;
504 imm <<= 4;
505 }
506 if (!(imm & 0xc0000000)) {
507 counter += 2;
508 imm <<= 2;
509 }
510 if (!(imm & 0x80000000)) {
511 counter += 1;
512 imm <<= 1;
513 }
514 /* Since imm >= 128, this must be true. */
515 SLJIT_ASSERT(counter <= 31);
516
517 if (imm & 0x00ffffff)
518 return INVALID_IMM; /* Cannot be encoded. */
519
520 return ((imm >> 24) & 0x7f) | COPY_BITS(counter, 4, 26, 1) | COPY_BITS(counter, 1, 12, 3) | COPY_BITS(counter, 0, 7, 1);
521 }
522
load_immediate(struct sljit_compiler * compiler,sljit_s32 dst,sljit_uw imm)523 static sljit_s32 load_immediate(struct sljit_compiler *compiler, sljit_s32 dst, sljit_uw imm)
524 {
525 sljit_uw tmp;
526
527 /* MOVS cannot be used since it destroy flags. */
528
529 if (imm >= 0x10000) {
530 tmp = get_imm(imm);
531 if (tmp != INVALID_IMM)
532 return push_inst32(compiler, MOV_WI | RD4(dst) | tmp);
533 tmp = get_imm(~imm);
534 if (tmp != INVALID_IMM)
535 return push_inst32(compiler, MVN_WI | RD4(dst) | tmp);
536 }
537
538 /* set low 16 bits, set hi 16 bits to 0. */
539 FAIL_IF(push_inst32(compiler, MOVW | RD4(dst)
540 | COPY_BITS(imm, 12, 16, 4) | COPY_BITS(imm, 11, 26, 1) | COPY_BITS(imm, 8, 12, 3) | (imm & 0xff)));
541
542 /* set hi 16 bit if needed. */
543 if (imm >= 0x10000)
544 return push_inst32(compiler, MOVT | RD4(dst)
545 | COPY_BITS(imm, 12 + 16, 16, 4) | COPY_BITS(imm, 11 + 16, 26, 1) | COPY_BITS(imm, 8 + 16, 12, 3) | ((imm & 0xff0000) >> 16));
546 return SLJIT_SUCCESS;
547 }
548
549 #define ARG1_IMM 0x0010000
550 #define ARG2_IMM 0x0020000
551 /* SET_FLAGS must be 0x100000 as it is also the value of S bit (can be used for optimization). */
552 #define SET_FLAGS 0x0100000
553 #define UNUSED_RETURN 0x0200000
554
emit_op_imm(struct sljit_compiler * compiler,sljit_s32 flags,sljit_s32 dst,sljit_uw arg1,sljit_uw arg2)555 static sljit_s32 emit_op_imm(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 dst, sljit_uw arg1, sljit_uw arg2)
556 {
557 /* dst must be register, TMP_REG1
558 arg1 must be register, imm
559 arg2 must be register, imm */
560 sljit_s32 reg;
561 sljit_uw imm, nimm;
562
563 if (SLJIT_UNLIKELY((flags & (ARG1_IMM | ARG2_IMM)) == (ARG1_IMM | ARG2_IMM))) {
564 /* Both are immediates, no temporaries are used. */
565 flags &= ~ARG1_IMM;
566 FAIL_IF(load_immediate(compiler, TMP_REG1, arg1));
567 arg1 = TMP_REG1;
568 }
569
570 if (flags & (ARG1_IMM | ARG2_IMM)) {
571 reg = (flags & ARG2_IMM) ? arg1 : arg2;
572 imm = (flags & ARG2_IMM) ? arg2 : arg1;
573
574 switch (flags & 0xffff) {
575 case SLJIT_CLZ:
576 case SLJIT_MUL:
577 /* No form with immediate operand. */
578 break;
579 case SLJIT_MOV:
580 SLJIT_ASSERT(!(flags & SET_FLAGS) && (flags & ARG2_IMM) && arg1 == TMP_REG2);
581 return load_immediate(compiler, dst, imm);
582 case SLJIT_NOT:
583 if (!(flags & SET_FLAGS))
584 return load_immediate(compiler, dst, ~imm);
585 /* Since the flags should be set, we just fallback to the register mode.
586 Although some clever things could be done here, "NOT IMM" does not worth the efforts. */
587 break;
588 case SLJIT_ADD:
589 nimm = -imm;
590 if (IS_2_LO_REGS(reg, dst)) {
591 if (imm <= 0x7)
592 return push_inst16(compiler, ADDSI3 | IMM3(imm) | RD3(dst) | RN3(reg));
593 if (nimm <= 0x7)
594 return push_inst16(compiler, SUBSI3 | IMM3(nimm) | RD3(dst) | RN3(reg));
595 if (reg == dst) {
596 if (imm <= 0xff)
597 return push_inst16(compiler, ADDSI8 | IMM8(imm) | RDN3(dst));
598 if (nimm <= 0xff)
599 return push_inst16(compiler, SUBSI8 | IMM8(nimm) | RDN3(dst));
600 }
601 }
602 if (!(flags & SET_FLAGS)) {
603 if (imm <= 0xfff)
604 return push_inst32(compiler, ADDWI | RD4(dst) | RN4(reg) | IMM12(imm));
605 if (nimm <= 0xfff)
606 return push_inst32(compiler, SUBWI | RD4(dst) | RN4(reg) | IMM12(nimm));
607 }
608 nimm = get_imm(imm);
609 if (nimm != INVALID_IMM)
610 return push_inst32(compiler, ADD_WI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | nimm);
611 nimm = get_imm(-imm);
612 if (nimm != INVALID_IMM)
613 return push_inst32(compiler, SUB_WI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | nimm);
614 break;
615 case SLJIT_ADDC:
616 imm = get_imm(imm);
617 if (imm != INVALID_IMM)
618 return push_inst32(compiler, ADCI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
619 break;
620 case SLJIT_SUB:
621 /* SUB operation can be replaced by ADD because of the negative carry flag. */
622 if (flags & ARG1_IMM) {
623 if (imm == 0 && IS_2_LO_REGS(reg, dst))
624 return push_inst16(compiler, RSBSI | RD3(dst) | RN3(reg));
625 imm = get_imm(imm);
626 if (imm != INVALID_IMM)
627 return push_inst32(compiler, RSB_WI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
628 break;
629 }
630 if (flags & UNUSED_RETURN) {
631 if (imm <= 0xff && reg_map[reg] <= 7)
632 return push_inst16(compiler, CMPI | IMM8(imm) | RDN3(reg));
633 nimm = get_imm(imm);
634 if (nimm != INVALID_IMM)
635 return push_inst32(compiler, CMPI_W | RN4(reg) | nimm);
636 nimm = get_imm(-imm);
637 if (nimm != INVALID_IMM)
638 return push_inst32(compiler, CMNI_W | RN4(reg) | nimm);
639 }
640 nimm = -imm;
641 if (IS_2_LO_REGS(reg, dst)) {
642 if (imm <= 0x7)
643 return push_inst16(compiler, SUBSI3 | IMM3(imm) | RD3(dst) | RN3(reg));
644 if (nimm <= 0x7)
645 return push_inst16(compiler, ADDSI3 | IMM3(nimm) | RD3(dst) | RN3(reg));
646 if (reg == dst) {
647 if (imm <= 0xff)
648 return push_inst16(compiler, SUBSI8 | IMM8(imm) | RDN3(dst));
649 if (nimm <= 0xff)
650 return push_inst16(compiler, ADDSI8 | IMM8(nimm) | RDN3(dst));
651 }
652 }
653 if (!(flags & SET_FLAGS)) {
654 if (imm <= 0xfff)
655 return push_inst32(compiler, SUBWI | RD4(dst) | RN4(reg) | IMM12(imm));
656 if (nimm <= 0xfff)
657 return push_inst32(compiler, ADDWI | RD4(dst) | RN4(reg) | IMM12(nimm));
658 }
659 nimm = get_imm(imm);
660 if (nimm != INVALID_IMM)
661 return push_inst32(compiler, SUB_WI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | nimm);
662 nimm = get_imm(-imm);
663 if (nimm != INVALID_IMM)
664 return push_inst32(compiler, ADD_WI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | nimm);
665 break;
666 case SLJIT_SUBC:
667 if (flags & ARG1_IMM)
668 break;
669 imm = get_imm(imm);
670 if (imm != INVALID_IMM)
671 return push_inst32(compiler, SBCI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
672 break;
673 case SLJIT_AND:
674 nimm = get_imm(imm);
675 if (nimm != INVALID_IMM)
676 return push_inst32(compiler, ANDI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | nimm);
677 imm = get_imm(imm);
678 if (imm != INVALID_IMM)
679 return push_inst32(compiler, BICI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
680 break;
681 case SLJIT_OR:
682 nimm = get_imm(imm);
683 if (nimm != INVALID_IMM)
684 return push_inst32(compiler, ORRI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | nimm);
685 imm = get_imm(imm);
686 if (imm != INVALID_IMM)
687 return push_inst32(compiler, ORNI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
688 break;
689 case SLJIT_XOR:
690 imm = get_imm(imm);
691 if (imm != INVALID_IMM)
692 return push_inst32(compiler, EORI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
693 break;
694 case SLJIT_SHL:
695 case SLJIT_LSHR:
696 case SLJIT_ASHR:
697 if (flags & ARG1_IMM)
698 break;
699 imm &= 0x1f;
700 if (imm == 0) {
701 if (!(flags & SET_FLAGS))
702 return push_inst16(compiler, MOV | SET_REGS44(dst, reg));
703 if (IS_2_LO_REGS(dst, reg))
704 return push_inst16(compiler, MOVS | RD3(dst) | RN3(reg));
705 return push_inst32(compiler, MOV_W | SET_FLAGS | RD4(dst) | RM4(reg));
706 }
707 switch (flags & 0xffff) {
708 case SLJIT_SHL:
709 if (IS_2_LO_REGS(dst, reg))
710 return push_inst16(compiler, LSLSI | RD3(dst) | RN3(reg) | (imm << 6));
711 return push_inst32(compiler, LSL_WI | (flags & SET_FLAGS) | RD4(dst) | RM4(reg) | IMM5(imm));
712 case SLJIT_LSHR:
713 if (IS_2_LO_REGS(dst, reg))
714 return push_inst16(compiler, LSRSI | RD3(dst) | RN3(reg) | (imm << 6));
715 return push_inst32(compiler, LSR_WI | (flags & SET_FLAGS) | RD4(dst) | RM4(reg) | IMM5(imm));
716 default: /* SLJIT_ASHR */
717 if (IS_2_LO_REGS(dst, reg))
718 return push_inst16(compiler, ASRSI | RD3(dst) | RN3(reg) | (imm << 6));
719 return push_inst32(compiler, ASR_WI | (flags & SET_FLAGS) | RD4(dst) | RM4(reg) | IMM5(imm));
720 }
721 default:
722 SLJIT_UNREACHABLE();
723 break;
724 }
725
726 if (flags & ARG2_IMM) {
727 imm = arg2;
728 arg2 = (arg1 == TMP_REG1) ? TMP_REG2 : TMP_REG1;
729 FAIL_IF(load_immediate(compiler, arg2, imm));
730 }
731 else {
732 imm = arg1;
733 arg1 = (arg2 == TMP_REG1) ? TMP_REG2 : TMP_REG1;
734 FAIL_IF(load_immediate(compiler, arg1, imm));
735 }
736
737 SLJIT_ASSERT(arg1 != arg2);
738 }
739
740 /* Both arguments are registers. */
741 switch (flags & 0xffff) {
742 case SLJIT_MOV:
743 case SLJIT_MOV_U32:
744 case SLJIT_MOV_S32:
745 case SLJIT_MOV_P:
746 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG2);
747 if (dst == arg2)
748 return SLJIT_SUCCESS;
749 return push_inst16(compiler, MOV | SET_REGS44(dst, arg2));
750 case SLJIT_MOV_U8:
751 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG2);
752 if (IS_2_LO_REGS(dst, arg2))
753 return push_inst16(compiler, UXTB | RD3(dst) | RN3(arg2));
754 return push_inst32(compiler, UXTB_W | RD4(dst) | RM4(arg2));
755 case SLJIT_MOV_S8:
756 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG2);
757 if (IS_2_LO_REGS(dst, arg2))
758 return push_inst16(compiler, SXTB | RD3(dst) | RN3(arg2));
759 return push_inst32(compiler, SXTB_W | RD4(dst) | RM4(arg2));
760 case SLJIT_MOV_U16:
761 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG2);
762 if (IS_2_LO_REGS(dst, arg2))
763 return push_inst16(compiler, UXTH | RD3(dst) | RN3(arg2));
764 return push_inst32(compiler, UXTH_W | RD4(dst) | RM4(arg2));
765 case SLJIT_MOV_S16:
766 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG2);
767 if (IS_2_LO_REGS(dst, arg2))
768 return push_inst16(compiler, SXTH | RD3(dst) | RN3(arg2));
769 return push_inst32(compiler, SXTH_W | RD4(dst) | RM4(arg2));
770 case SLJIT_NOT:
771 SLJIT_ASSERT(arg1 == TMP_REG2);
772 if (IS_2_LO_REGS(dst, arg2))
773 return push_inst16(compiler, MVNS | RD3(dst) | RN3(arg2));
774 return push_inst32(compiler, MVN_W | (flags & SET_FLAGS) | RD4(dst) | RM4(arg2));
775 case SLJIT_CLZ:
776 SLJIT_ASSERT(arg1 == TMP_REG2);
777 FAIL_IF(push_inst32(compiler, CLZ | RN4(arg2) | RD4(dst) | RM4(arg2)));
778 return SLJIT_SUCCESS;
779 case SLJIT_ADD:
780 if (IS_3_LO_REGS(dst, arg1, arg2))
781 return push_inst16(compiler, ADDS | RD3(dst) | RN3(arg1) | RM3(arg2));
782 if (dst == arg1 && !(flags & SET_FLAGS))
783 return push_inst16(compiler, ADD | SET_REGS44(dst, arg2));
784 return push_inst32(compiler, ADD_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
785 case SLJIT_ADDC:
786 if (dst == arg1 && IS_2_LO_REGS(dst, arg2))
787 return push_inst16(compiler, ADCS | RD3(dst) | RN3(arg2));
788 return push_inst32(compiler, ADC_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
789 case SLJIT_SUB:
790 if (flags & UNUSED_RETURN) {
791 if (IS_2_LO_REGS(arg1, arg2))
792 return push_inst16(compiler, CMP | RD3(arg1) | RN3(arg2));
793 return push_inst16(compiler, CMP_X | SET_REGS44(arg1, arg2));
794 }
795 if (IS_3_LO_REGS(dst, arg1, arg2))
796 return push_inst16(compiler, SUBS | RD3(dst) | RN3(arg1) | RM3(arg2));
797 return push_inst32(compiler, SUB_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
798 case SLJIT_SUBC:
799 if (dst == arg1 && IS_2_LO_REGS(dst, arg2))
800 return push_inst16(compiler, SBCS | RD3(dst) | RN3(arg2));
801 return push_inst32(compiler, SBC_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
802 case SLJIT_MUL:
803 if (!(flags & SET_FLAGS))
804 return push_inst32(compiler, MUL | RD4(dst) | RN4(arg1) | RM4(arg2));
805 SLJIT_ASSERT(dst != TMP_REG2);
806 FAIL_IF(push_inst32(compiler, SMULL | RT4(dst) | RD4(TMP_REG2) | RN4(arg1) | RM4(arg2)));
807 /* cmp TMP_REG2, dst asr #31. */
808 return push_inst32(compiler, CMP_W | RN4(TMP_REG2) | 0x70e0 | RM4(dst));
809 case SLJIT_AND:
810 if (dst == arg1 && IS_2_LO_REGS(dst, arg2))
811 return push_inst16(compiler, ANDS | RD3(dst) | RN3(arg2));
812 if ((flags & UNUSED_RETURN) && IS_2_LO_REGS(arg1, arg2))
813 return push_inst16(compiler, TST | RD3(arg1) | RN3(arg2));
814 return push_inst32(compiler, AND_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
815 case SLJIT_OR:
816 if (dst == arg1 && IS_2_LO_REGS(dst, arg2))
817 return push_inst16(compiler, ORRS | RD3(dst) | RN3(arg2));
818 return push_inst32(compiler, ORR_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
819 case SLJIT_XOR:
820 if (dst == arg1 && IS_2_LO_REGS(dst, arg2))
821 return push_inst16(compiler, EORS | RD3(dst) | RN3(arg2));
822 return push_inst32(compiler, EOR_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
823 case SLJIT_SHL:
824 if (dst == arg1 && IS_2_LO_REGS(dst, arg2))
825 return push_inst16(compiler, LSLS | RD3(dst) | RN3(arg2));
826 return push_inst32(compiler, LSL_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
827 case SLJIT_LSHR:
828 if (dst == arg1 && IS_2_LO_REGS(dst, arg2))
829 return push_inst16(compiler, LSRS | RD3(dst) | RN3(arg2));
830 return push_inst32(compiler, LSR_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
831 case SLJIT_ASHR:
832 if (dst == arg1 && IS_2_LO_REGS(dst, arg2))
833 return push_inst16(compiler, ASRS | RD3(dst) | RN3(arg2));
834 return push_inst32(compiler, ASR_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
835 }
836
837 SLJIT_UNREACHABLE();
838 return SLJIT_SUCCESS;
839 }
840
841 #define STORE 0x01
842 #define SIGNED 0x02
843
844 #define WORD_SIZE 0x00
845 #define BYTE_SIZE 0x04
846 #define HALF_SIZE 0x08
847 #define PRELOAD 0x0c
848
849 #define IS_WORD_SIZE(flags) (!(flags & (BYTE_SIZE | HALF_SIZE)))
850 #define OFFSET_CHECK(imm, shift) (!(argw & ~(imm << shift)))
851
852 /*
853 1st letter:
854 w = word
855 b = byte
856 h = half
857
858 2nd letter:
859 s = signed
860 u = unsigned
861
862 3rd letter:
863 l = load
864 s = store
865 */
866
867 static const sljit_ins sljit_mem16[12] = {
868 /* w u l */ 0x5800 /* ldr */,
869 /* w u s */ 0x5000 /* str */,
870 /* w s l */ 0x5800 /* ldr */,
871 /* w s s */ 0x5000 /* str */,
872
873 /* b u l */ 0x5c00 /* ldrb */,
874 /* b u s */ 0x5400 /* strb */,
875 /* b s l */ 0x5600 /* ldrsb */,
876 /* b s s */ 0x5400 /* strb */,
877
878 /* h u l */ 0x5a00 /* ldrh */,
879 /* h u s */ 0x5200 /* strh */,
880 /* h s l */ 0x5e00 /* ldrsh */,
881 /* h s s */ 0x5200 /* strh */,
882 };
883
884 static const sljit_ins sljit_mem16_imm5[12] = {
885 /* w u l */ 0x6800 /* ldr imm5 */,
886 /* w u s */ 0x6000 /* str imm5 */,
887 /* w s l */ 0x6800 /* ldr imm5 */,
888 /* w s s */ 0x6000 /* str imm5 */,
889
890 /* b u l */ 0x7800 /* ldrb imm5 */,
891 /* b u s */ 0x7000 /* strb imm5 */,
892 /* b s l */ 0x0000 /* not allowed */,
893 /* b s s */ 0x7000 /* strb imm5 */,
894
895 /* h u l */ 0x8800 /* ldrh imm5 */,
896 /* h u s */ 0x8000 /* strh imm5 */,
897 /* h s l */ 0x0000 /* not allowed */,
898 /* h s s */ 0x8000 /* strh imm5 */,
899 };
900
901 #define MEM_IMM8 0xc00
902 #define MEM_IMM12 0x800000
903 static const sljit_ins sljit_mem32[13] = {
904 /* w u l */ 0xf8500000 /* ldr.w */,
905 /* w u s */ 0xf8400000 /* str.w */,
906 /* w s l */ 0xf8500000 /* ldr.w */,
907 /* w s s */ 0xf8400000 /* str.w */,
908
909 /* b u l */ 0xf8100000 /* ldrb.w */,
910 /* b u s */ 0xf8000000 /* strb.w */,
911 /* b s l */ 0xf9100000 /* ldrsb.w */,
912 /* b s s */ 0xf8000000 /* strb.w */,
913
914 /* h u l */ 0xf8300000 /* ldrh.w */,
915 /* h u s */ 0xf8200000 /* strsh.w */,
916 /* h s l */ 0xf9300000 /* ldrsh.w */,
917 /* h s s */ 0xf8200000 /* strsh.w */,
918
919 /* p u l */ 0xf8100000 /* pld */,
920 };
921
922 /* Helper function. Dst should be reg + value, using at most 1 instruction, flags does not set. */
emit_set_delta(struct sljit_compiler * compiler,sljit_s32 dst,sljit_s32 reg,sljit_sw value)923 static sljit_s32 emit_set_delta(struct sljit_compiler *compiler, sljit_s32 dst, sljit_s32 reg, sljit_sw value)
924 {
925 if (value >= 0) {
926 if (value <= 0xfff)
927 return push_inst32(compiler, ADDWI | RD4(dst) | RN4(reg) | IMM12(value));
928 value = get_imm(value);
929 if (value != INVALID_IMM)
930 return push_inst32(compiler, ADD_WI | RD4(dst) | RN4(reg) | value);
931 }
932 else {
933 value = -value;
934 if (value <= 0xfff)
935 return push_inst32(compiler, SUBWI | RD4(dst) | RN4(reg) | IMM12(value));
936 value = get_imm(value);
937 if (value != INVALID_IMM)
938 return push_inst32(compiler, SUB_WI | RD4(dst) | RN4(reg) | value);
939 }
940 return SLJIT_ERR_UNSUPPORTED;
941 }
942
emit_op_mem(struct sljit_compiler * compiler,sljit_s32 flags,sljit_s32 reg,sljit_s32 arg,sljit_sw argw,sljit_s32 tmp_reg)943 static SLJIT_INLINE sljit_s32 emit_op_mem(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg,
944 sljit_s32 arg, sljit_sw argw, sljit_s32 tmp_reg)
945 {
946 sljit_s32 other_r;
947 sljit_uw tmp;
948
949 SLJIT_ASSERT(arg & SLJIT_MEM);
950 SLJIT_ASSERT((arg & REG_MASK) != tmp_reg);
951 arg &= ~SLJIT_MEM;
952
953 if (SLJIT_UNLIKELY(!(arg & REG_MASK))) {
954 tmp = get_imm(argw & ~0xfff);
955 if (tmp != INVALID_IMM) {
956 FAIL_IF(push_inst32(compiler, MOV_WI | RD4(tmp_reg) | tmp));
957 return push_inst32(compiler, sljit_mem32[flags] | MEM_IMM12 | RT4(reg) | RN4(tmp_reg) | (argw & 0xfff));
958 }
959
960 FAIL_IF(load_immediate(compiler, tmp_reg, argw));
961 if (IS_2_LO_REGS(reg, tmp_reg) && sljit_mem16_imm5[flags])
962 return push_inst16(compiler, sljit_mem16_imm5[flags] | RD3(reg) | RN3(tmp_reg));
963 return push_inst32(compiler, sljit_mem32[flags] | MEM_IMM12 | RT4(reg) | RN4(tmp_reg));
964 }
965
966 if (SLJIT_UNLIKELY(arg & OFFS_REG_MASK)) {
967 argw &= 0x3;
968 other_r = OFFS_REG(arg);
969 arg &= 0xf;
970
971 if (!argw && IS_3_LO_REGS(reg, arg, other_r))
972 return push_inst16(compiler, sljit_mem16[flags] | RD3(reg) | RN3(arg) | RM3(other_r));
973 return push_inst32(compiler, sljit_mem32[flags] | RT4(reg) | RN4(arg) | RM4(other_r) | (argw << 4));
974 }
975
976 if (argw > 0xfff) {
977 tmp = get_imm(argw & ~0xfff);
978 if (tmp != INVALID_IMM) {
979 push_inst32(compiler, ADD_WI | RD4(tmp_reg) | RN4(arg) | tmp);
980 arg = tmp_reg;
981 argw = argw & 0xfff;
982 }
983 }
984 else if (argw < -0xff) {
985 tmp = get_imm(-argw & ~0xff);
986 if (tmp != INVALID_IMM) {
987 push_inst32(compiler, SUB_WI | RD4(tmp_reg) | RN4(arg) | tmp);
988 arg = tmp_reg;
989 argw = -(-argw & 0xff);
990 }
991 }
992
993 if (IS_2_LO_REGS(reg, arg) && sljit_mem16_imm5[flags]) {
994 tmp = 3;
995 if (IS_WORD_SIZE(flags)) {
996 if (OFFSET_CHECK(0x1f, 2))
997 tmp = 2;
998 }
999 else if (flags & BYTE_SIZE)
1000 {
1001 if (OFFSET_CHECK(0x1f, 0))
1002 tmp = 0;
1003 }
1004 else {
1005 SLJIT_ASSERT(flags & HALF_SIZE);
1006 if (OFFSET_CHECK(0x1f, 1))
1007 tmp = 1;
1008 }
1009
1010 if (tmp < 3)
1011 return push_inst16(compiler, sljit_mem16_imm5[flags] | RD3(reg) | RN3(arg) | (argw << (6 - tmp)));
1012 }
1013 else if (SLJIT_UNLIKELY(arg == SLJIT_SP) && IS_WORD_SIZE(flags) && OFFSET_CHECK(0xff, 2) && reg_map[reg] <= 7) {
1014 /* SP based immediate. */
1015 return push_inst16(compiler, STR_SP | ((flags & STORE) ? 0 : 0x800) | RDN3(reg) | (argw >> 2));
1016 }
1017
1018 if (argw >= 0 && argw <= 0xfff)
1019 return push_inst32(compiler, sljit_mem32[flags] | MEM_IMM12 | RT4(reg) | RN4(arg) | argw);
1020 else if (argw < 0 && argw >= -0xff)
1021 return push_inst32(compiler, sljit_mem32[flags] | MEM_IMM8 | RT4(reg) | RN4(arg) | -argw);
1022
1023 SLJIT_ASSERT(arg != tmp_reg);
1024
1025 FAIL_IF(load_immediate(compiler, tmp_reg, argw));
1026 if (IS_3_LO_REGS(reg, arg, tmp_reg))
1027 return push_inst16(compiler, sljit_mem16[flags] | RD3(reg) | RN3(arg) | RM3(tmp_reg));
1028 return push_inst32(compiler, sljit_mem32[flags] | RT4(reg) | RN4(arg) | RM4(tmp_reg));
1029 }
1030
1031 /* --------------------------------------------------------------------- */
1032 /* Entry, exit */
1033 /* --------------------------------------------------------------------- */
1034
sljit_emit_enter(struct sljit_compiler * compiler,sljit_s32 options,sljit_s32 arg_types,sljit_s32 scratches,sljit_s32 saveds,sljit_s32 fscratches,sljit_s32 fsaveds,sljit_s32 local_size)1035 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_enter(struct sljit_compiler *compiler,
1036 sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
1037 sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
1038 {
1039 sljit_s32 args, size, i, tmp;
1040 sljit_ins push = 0;
1041 #ifdef _WIN32
1042 sljit_uw imm;
1043 #endif
1044
1045 CHECK_ERROR();
1046 CHECK(check_sljit_emit_enter(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size));
1047 set_emit_enter(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size);
1048
1049 tmp = saveds < SLJIT_NUMBER_OF_SAVED_REGISTERS ? (SLJIT_S0 + 1 - saveds) : SLJIT_FIRST_SAVED_REG;
1050 for (i = SLJIT_S0; i >= tmp; i--)
1051 push |= 1 << reg_map[i];
1052
1053 for (i = scratches; i >= SLJIT_FIRST_SAVED_REG; i--)
1054 push |= 1 << reg_map[i];
1055
1056 FAIL_IF((push & 0xff00)
1057 ? push_inst32(compiler, PUSH_W | (1 << 14) | push)
1058 : push_inst16(compiler, PUSH | (1 << 8) | push));
1059
1060 /* Stack must be aligned to 8 bytes: (LR, R4) */
1061 size = GET_SAVED_REGISTERS_SIZE(scratches, saveds, 1);
1062 local_size = ((size + local_size + 7) & ~7) - size;
1063 compiler->local_size = local_size;
1064
1065 #ifdef _WIN32
1066 if (local_size >= 256) {
1067 if (local_size > 4096)
1068 imm = get_imm(4096);
1069 else
1070 imm = get_imm(local_size & ~0xff);
1071
1072 SLJIT_ASSERT(imm != INVALID_IMM);
1073 FAIL_IF(push_inst32(compiler, SUB_WI | RD4(TMP_REG1) | RN4(SLJIT_SP) | imm));
1074 }
1075 #else
1076 if (local_size > 0) {
1077 if (local_size <= (127 << 2))
1078 FAIL_IF(push_inst16(compiler, SUB_SP | (local_size >> 2)));
1079 else
1080 FAIL_IF(emit_op_imm(compiler, SLJIT_SUB | ARG2_IMM, SLJIT_SP, SLJIT_SP, local_size));
1081 }
1082 #endif
1083
1084 args = get_arg_count(arg_types);
1085
1086 if (args >= 1)
1087 FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(SLJIT_S0, SLJIT_R0)));
1088 if (args >= 2)
1089 FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(SLJIT_S1, SLJIT_R1)));
1090 if (args >= 3)
1091 FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(SLJIT_S2, SLJIT_R2)));
1092
1093 #ifdef _WIN32
1094 if (local_size >= 256) {
1095 if (local_size > 4096) {
1096 imm = get_imm(4096);
1097 SLJIT_ASSERT(imm != INVALID_IMM);
1098
1099 if (local_size < 4 * 4096) {
1100 if (local_size > 2 * 4096) {
1101 FAIL_IF(push_inst32(compiler, LDRI | 0x400 | RT4(TMP_REG2) | RN4(TMP_REG1)));
1102 FAIL_IF(push_inst32(compiler, SUB_WI | RD4(TMP_REG1) | RN4(TMP_REG1) | imm));
1103 local_size -= 4096;
1104 }
1105
1106 if (local_size > 2 * 4096) {
1107 FAIL_IF(push_inst32(compiler, LDRI | 0x400 | RT4(TMP_REG2) | RN4(TMP_REG1)));
1108 FAIL_IF(push_inst32(compiler, SUB_WI | RD4(TMP_REG1) | RN4(TMP_REG1) | imm));
1109 local_size -= 4096;
1110 }
1111
1112 FAIL_IF(push_inst32(compiler, LDRI | 0x400 | RT4(TMP_REG2) | RN4(TMP_REG1)));
1113 local_size -= 4096;
1114
1115 SLJIT_ASSERT(local_size > 0);
1116 }
1117 else {
1118 FAIL_IF(load_immediate(compiler, SLJIT_R3, (local_size >> 12) - 1));
1119 FAIL_IF(push_inst32(compiler, LDRI | 0x400 | RT4(TMP_REG2) | RN4(TMP_REG1)));
1120 FAIL_IF(push_inst32(compiler, SUB_WI | RD4(TMP_REG1) | RN4(TMP_REG1) | imm));
1121 SLJIT_ASSERT(reg_map[SLJIT_R3] < 7);
1122 FAIL_IF(push_inst16(compiler, SUBSI8 | RDN3(SLJIT_R3) | 1));
1123 FAIL_IF(push_inst16(compiler, BCC | (0x1 << 8) /* not-equal */ | (-7 & 0xff)));
1124
1125 local_size &= 0xfff;
1126
1127 if (local_size != 0)
1128 FAIL_IF(push_inst32(compiler, LDRI | 0x400 | RT4(TMP_REG2) | RN4(TMP_REG1)));
1129 }
1130
1131 if (local_size >= 256) {
1132 imm = get_imm(local_size & ~0xff);
1133 SLJIT_ASSERT(imm != INVALID_IMM);
1134
1135 FAIL_IF(push_inst32(compiler, SUB_WI | RD4(TMP_REG1) | RN4(TMP_REG1) | imm));
1136 }
1137 }
1138
1139 local_size &= 0xff;
1140 FAIL_IF(push_inst32(compiler, LDRI | 0x400 | (local_size > 0 ? 0x100 : 0) | RT4(TMP_REG2) | RN4(TMP_REG1) | local_size));
1141
1142 FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(SLJIT_SP, TMP_REG1)));
1143 }
1144 else if (local_size > 0)
1145 FAIL_IF(push_inst32(compiler, LDRI | 0x500 | RT4(TMP_REG1) | RN4(SLJIT_SP) | local_size));
1146 #endif
1147
1148 return SLJIT_SUCCESS;
1149 }
1150
sljit_set_context(struct sljit_compiler * compiler,sljit_s32 options,sljit_s32 arg_types,sljit_s32 scratches,sljit_s32 saveds,sljit_s32 fscratches,sljit_s32 fsaveds,sljit_s32 local_size)1151 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_set_context(struct sljit_compiler *compiler,
1152 sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
1153 sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
1154 {
1155 sljit_s32 size;
1156
1157 CHECK_ERROR();
1158 CHECK(check_sljit_set_context(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size));
1159 set_set_context(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size);
1160
1161 size = GET_SAVED_REGISTERS_SIZE(scratches, saveds, 1);
1162 compiler->local_size = ((size + local_size + 7) & ~7) - size;
1163 return SLJIT_SUCCESS;
1164 }
1165
sljit_emit_return(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 src,sljit_sw srcw)1166 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_return(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 src, sljit_sw srcw)
1167 {
1168 sljit_s32 i, tmp;
1169 sljit_ins pop = 0;
1170
1171 CHECK_ERROR();
1172 CHECK(check_sljit_emit_return(compiler, op, src, srcw));
1173
1174 FAIL_IF(emit_mov_before_return(compiler, op, src, srcw));
1175
1176 if (compiler->local_size > 0) {
1177 if (compiler->local_size <= (127 << 2))
1178 FAIL_IF(push_inst16(compiler, ADD_SP | (compiler->local_size >> 2)));
1179 else
1180 FAIL_IF(emit_op_imm(compiler, SLJIT_ADD | ARG2_IMM, SLJIT_SP, SLJIT_SP, compiler->local_size));
1181 }
1182
1183 tmp = compiler->saveds < SLJIT_NUMBER_OF_SAVED_REGISTERS ? (SLJIT_S0 + 1 - compiler->saveds) : SLJIT_FIRST_SAVED_REG;
1184 for (i = SLJIT_S0; i >= tmp; i--)
1185 pop |= 1 << reg_map[i];
1186
1187 for (i = compiler->scratches; i >= SLJIT_FIRST_SAVED_REG; i--)
1188 pop |= 1 << reg_map[i];
1189
1190 return (pop & 0xff00)
1191 ? push_inst32(compiler, POP_W | (1 << 15) | pop)
1192 : push_inst16(compiler, POP | (1 << 8) | pop);
1193 }
1194
1195 /* --------------------------------------------------------------------- */
1196 /* Operators */
1197 /* --------------------------------------------------------------------- */
1198
1199 #if !(defined __ARM_FEATURE_IDIV) && !(defined __ARM_ARCH_EXT_IDIV__)
1200
1201 #ifdef __cplusplus
1202 extern "C" {
1203 #endif
1204
1205 #ifdef _WIN32
1206 extern unsigned long long __rt_udiv(unsigned int denominator, unsigned int numerator);
1207 extern long long __rt_sdiv(int denominator, int numerator);
1208 #elif defined(__GNUC__)
1209 extern unsigned int __aeabi_uidivmod(unsigned int numerator, int unsigned denominator);
1210 extern int __aeabi_idivmod(int numerator, int denominator);
1211 #else
1212 #error "Software divmod functions are needed"
1213 #endif
1214
1215 #ifdef __cplusplus
1216 }
1217 #endif
1218
1219 #endif /* !__ARM_FEATURE_IDIV && !__ARM_ARCH_EXT_IDIV__ */
1220
sljit_emit_op0(struct sljit_compiler * compiler,sljit_s32 op)1221 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op0(struct sljit_compiler *compiler, sljit_s32 op)
1222 {
1223 #if !(defined __ARM_FEATURE_IDIV) && !(defined __ARM_ARCH_EXT_IDIV__)
1224 sljit_sw saved_reg_list[3];
1225 sljit_sw saved_reg_count;
1226 #endif
1227
1228 CHECK_ERROR();
1229 CHECK(check_sljit_emit_op0(compiler, op));
1230
1231 op = GET_OPCODE(op);
1232 switch (op) {
1233 case SLJIT_BREAKPOINT:
1234 return push_inst16(compiler, BKPT);
1235 case SLJIT_NOP:
1236 return push_inst16(compiler, NOP);
1237 case SLJIT_LMUL_UW:
1238 case SLJIT_LMUL_SW:
1239 return push_inst32(compiler, (op == SLJIT_LMUL_UW ? UMULL : SMULL)
1240 | (reg_map[SLJIT_R1] << 8)
1241 | (reg_map[SLJIT_R0] << 12)
1242 | (reg_map[SLJIT_R0] << 16)
1243 | reg_map[SLJIT_R1]);
1244 #if (defined __ARM_FEATURE_IDIV) || (defined __ARM_ARCH_EXT_IDIV__)
1245 case SLJIT_DIVMOD_UW:
1246 case SLJIT_DIVMOD_SW:
1247 FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(TMP_REG1, SLJIT_R0)));
1248 FAIL_IF(push_inst32(compiler, (op == SLJIT_DIVMOD_UW ? UDIV : SDIV) | RD4(SLJIT_R0) | RN4(SLJIT_R0) | RM4(SLJIT_R1)));
1249 FAIL_IF(push_inst32(compiler, MUL | RD4(SLJIT_R1) | RN4(SLJIT_R0) | RM4(SLJIT_R1)));
1250 return push_inst32(compiler, SUB_W | RD4(SLJIT_R1) | RN4(TMP_REG1) | RM4(SLJIT_R1));
1251 case SLJIT_DIV_UW:
1252 case SLJIT_DIV_SW:
1253 return push_inst32(compiler, (op == SLJIT_DIV_UW ? UDIV : SDIV) | RD4(SLJIT_R0) | RN4(SLJIT_R0) | RM4(SLJIT_R1));
1254 #else /* !__ARM_FEATURE_IDIV && !__ARM_ARCH_EXT_IDIV__ */
1255 case SLJIT_DIVMOD_UW:
1256 case SLJIT_DIVMOD_SW:
1257 case SLJIT_DIV_UW:
1258 case SLJIT_DIV_SW:
1259 SLJIT_COMPILE_ASSERT((SLJIT_DIVMOD_UW & 0x2) == 0 && SLJIT_DIV_UW - 0x2 == SLJIT_DIVMOD_UW, bad_div_opcode_assignments);
1260 SLJIT_ASSERT(reg_map[2] == 1 && reg_map[3] == 2 && reg_map[4] == 3);
1261
1262 saved_reg_count = 0;
1263 if (compiler->scratches >= 4)
1264 saved_reg_list[saved_reg_count++] = 3;
1265 if (compiler->scratches >= 3)
1266 saved_reg_list[saved_reg_count++] = 2;
1267 if (op >= SLJIT_DIV_UW)
1268 saved_reg_list[saved_reg_count++] = 1;
1269
1270 if (saved_reg_count > 0) {
1271 FAIL_IF(push_inst32(compiler, 0xf84d0d00 | (saved_reg_count >= 3 ? 16 : 8)
1272 | (saved_reg_list[0] << 12) /* str rX, [sp, #-8/-16]! */));
1273 if (saved_reg_count >= 2) {
1274 SLJIT_ASSERT(saved_reg_list[1] < 8);
1275 FAIL_IF(push_inst16(compiler, 0x9001 | (saved_reg_list[1] << 8) /* str rX, [sp, #4] */));
1276 }
1277 if (saved_reg_count >= 3) {
1278 SLJIT_ASSERT(saved_reg_list[2] < 8);
1279 FAIL_IF(push_inst16(compiler, 0x9002 | (saved_reg_list[2] << 8) /* str rX, [sp, #8] */));
1280 }
1281 }
1282
1283 #ifdef _WIN32
1284 FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(TMP_REG1, SLJIT_R0)));
1285 FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(SLJIT_R0, SLJIT_R1)));
1286 FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(SLJIT_R1, TMP_REG1)));
1287 FAIL_IF(sljit_emit_ijump(compiler, SLJIT_FAST_CALL, SLJIT_IMM,
1288 ((op | 0x2) == SLJIT_DIV_UW ? SLJIT_FUNC_OFFSET(__rt_udiv) : SLJIT_FUNC_OFFSET(__rt_sdiv))));
1289 #elif defined(__GNUC__)
1290 FAIL_IF(sljit_emit_ijump(compiler, SLJIT_FAST_CALL, SLJIT_IMM,
1291 ((op | 0x2) == SLJIT_DIV_UW ? SLJIT_FUNC_OFFSET(__aeabi_uidivmod) : SLJIT_FUNC_OFFSET(__aeabi_idivmod))));
1292 #else
1293 #error "Software divmod functions are needed"
1294 #endif
1295
1296 if (saved_reg_count > 0) {
1297 if (saved_reg_count >= 3) {
1298 SLJIT_ASSERT(saved_reg_list[2] < 8);
1299 FAIL_IF(push_inst16(compiler, 0x9802 | (saved_reg_list[2] << 8) /* ldr rX, [sp, #8] */));
1300 }
1301 if (saved_reg_count >= 2) {
1302 SLJIT_ASSERT(saved_reg_list[1] < 8);
1303 FAIL_IF(push_inst16(compiler, 0x9801 | (saved_reg_list[1] << 8) /* ldr rX, [sp, #4] */));
1304 }
1305 return push_inst32(compiler, 0xf85d0b00 | (saved_reg_count >= 3 ? 16 : 8)
1306 | (saved_reg_list[0] << 12) /* ldr rX, [sp], #8/16 */);
1307 }
1308 return SLJIT_SUCCESS;
1309 #endif /* __ARM_FEATURE_IDIV || __ARM_ARCH_EXT_IDIV__ */
1310 }
1311
1312 return SLJIT_SUCCESS;
1313 }
1314
sljit_emit_op1(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 src,sljit_sw srcw)1315 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op1(struct sljit_compiler *compiler, sljit_s32 op,
1316 sljit_s32 dst, sljit_sw dstw,
1317 sljit_s32 src, sljit_sw srcw)
1318 {
1319 sljit_s32 dst_r, flags;
1320 sljit_s32 op_flags = GET_ALL_FLAGS(op);
1321
1322 CHECK_ERROR();
1323 CHECK(check_sljit_emit_op1(compiler, op, dst, dstw, src, srcw));
1324 ADJUST_LOCAL_OFFSET(dst, dstw);
1325 ADJUST_LOCAL_OFFSET(src, srcw);
1326
1327 if (dst == SLJIT_UNUSED && !HAS_FLAGS(op)) {
1328 /* Since TMP_PC has index 15, IS_2_LO_REGS and IS_3_LO_REGS checks always fail. */
1329 if (op <= SLJIT_MOV_P && (src & SLJIT_MEM))
1330 return emit_op_mem(compiler, PRELOAD, TMP_PC, src, srcw, TMP_REG1);
1331 return SLJIT_SUCCESS;
1332 }
1333
1334 dst_r = SLOW_IS_REG(dst) ? dst : TMP_REG1;
1335
1336 op = GET_OPCODE(op);
1337 if (op >= SLJIT_MOV && op <= SLJIT_MOV_P) {
1338 switch (op) {
1339 case SLJIT_MOV:
1340 case SLJIT_MOV_U32:
1341 case SLJIT_MOV_S32:
1342 case SLJIT_MOV_P:
1343 flags = WORD_SIZE;
1344 break;
1345 case SLJIT_MOV_U8:
1346 flags = BYTE_SIZE;
1347 if (src & SLJIT_IMM)
1348 srcw = (sljit_u8)srcw;
1349 break;
1350 case SLJIT_MOV_S8:
1351 flags = BYTE_SIZE | SIGNED;
1352 if (src & SLJIT_IMM)
1353 srcw = (sljit_s8)srcw;
1354 break;
1355 case SLJIT_MOV_U16:
1356 flags = HALF_SIZE;
1357 if (src & SLJIT_IMM)
1358 srcw = (sljit_u16)srcw;
1359 break;
1360 case SLJIT_MOV_S16:
1361 flags = HALF_SIZE | SIGNED;
1362 if (src & SLJIT_IMM)
1363 srcw = (sljit_s16)srcw;
1364 break;
1365 default:
1366 SLJIT_UNREACHABLE();
1367 flags = 0;
1368 break;
1369 }
1370
1371 if (src & SLJIT_IMM)
1372 FAIL_IF(emit_op_imm(compiler, SLJIT_MOV | ARG2_IMM, dst_r, TMP_REG2, srcw));
1373 else if (src & SLJIT_MEM) {
1374 FAIL_IF(emit_op_mem(compiler, flags, dst_r, src, srcw, TMP_REG1));
1375 } else {
1376 if (dst_r != TMP_REG1)
1377 return emit_op_imm(compiler, op, dst_r, TMP_REG2, src);
1378 dst_r = src;
1379 }
1380
1381 if (!(dst & SLJIT_MEM))
1382 return SLJIT_SUCCESS;
1383
1384 return emit_op_mem(compiler, flags | STORE, dst_r, dst, dstw, TMP_REG2);
1385 }
1386
1387 if (op == SLJIT_NEG) {
1388 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
1389 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
1390 compiler->skip_checks = 1;
1391 #endif
1392 return sljit_emit_op2(compiler, SLJIT_SUB | op_flags, dst, dstw, SLJIT_IMM, 0, src, srcw);
1393 }
1394
1395 flags = HAS_FLAGS(op_flags) ? SET_FLAGS : 0;
1396
1397 if (src & SLJIT_MEM) {
1398 FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG1, src, srcw, TMP_REG1));
1399 src = TMP_REG1;
1400 }
1401
1402 emit_op_imm(compiler, flags | op, dst_r, TMP_REG2, src);
1403
1404 if (SLJIT_UNLIKELY(dst & SLJIT_MEM))
1405 return emit_op_mem(compiler, flags | STORE, dst_r, dst, dstw, TMP_REG2);
1406 return SLJIT_SUCCESS;
1407 }
1408
sljit_emit_op2(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 src1,sljit_sw src1w,sljit_s32 src2,sljit_sw src2w)1409 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op2(struct sljit_compiler *compiler, sljit_s32 op,
1410 sljit_s32 dst, sljit_sw dstw,
1411 sljit_s32 src1, sljit_sw src1w,
1412 sljit_s32 src2, sljit_sw src2w)
1413 {
1414 sljit_s32 dst_reg, flags, src2_reg;
1415
1416 CHECK_ERROR();
1417 CHECK(check_sljit_emit_op2(compiler, op, dst, dstw, src1, src1w, src2, src2w));
1418 ADJUST_LOCAL_OFFSET(dst, dstw);
1419 ADJUST_LOCAL_OFFSET(src1, src1w);
1420 ADJUST_LOCAL_OFFSET(src2, src2w);
1421
1422 if (dst == SLJIT_UNUSED && !HAS_FLAGS(op))
1423 return SLJIT_SUCCESS;
1424
1425 dst_reg = SLOW_IS_REG(dst) ? dst : TMP_REG1;
1426 flags = HAS_FLAGS(op) ? SET_FLAGS : 0;
1427
1428 if (src1 & SLJIT_IMM)
1429 flags |= ARG1_IMM;
1430 else if (src1 & SLJIT_MEM) {
1431 emit_op_mem(compiler, WORD_SIZE, TMP_REG1, src1, src1w, TMP_REG1);
1432 src1w = TMP_REG1;
1433 }
1434 else
1435 src1w = src1;
1436
1437 if (src2 & SLJIT_IMM)
1438 flags |= ARG2_IMM;
1439 else if (src2 & SLJIT_MEM) {
1440 src2_reg = (!(flags & ARG1_IMM) && (src1w == TMP_REG1)) ? TMP_REG2 : TMP_REG1;
1441 emit_op_mem(compiler, WORD_SIZE, src2_reg, src2, src2w, src2_reg);
1442 src2w = src2_reg;
1443 }
1444 else
1445 src2w = src2;
1446
1447 if (dst == SLJIT_UNUSED)
1448 flags |= UNUSED_RETURN;
1449
1450 emit_op_imm(compiler, flags | GET_OPCODE(op), dst_reg, src1w, src2w);
1451
1452 if (!(dst & SLJIT_MEM))
1453 return SLJIT_SUCCESS;
1454 return emit_op_mem(compiler, WORD_SIZE | STORE, dst_reg, dst, dstw, TMP_REG2);
1455 }
1456
sljit_get_register_index(sljit_s32 reg)1457 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_register_index(sljit_s32 reg)
1458 {
1459 CHECK_REG_INDEX(check_sljit_get_register_index(reg));
1460 return reg_map[reg];
1461 }
1462
sljit_get_float_register_index(sljit_s32 reg)1463 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_float_register_index(sljit_s32 reg)
1464 {
1465 CHECK_REG_INDEX(check_sljit_get_float_register_index(reg));
1466 return (freg_map[reg] << 1);
1467 }
1468
sljit_emit_op_custom(struct sljit_compiler * compiler,void * instruction,sljit_s32 size)1469 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_custom(struct sljit_compiler *compiler,
1470 void *instruction, sljit_s32 size)
1471 {
1472 CHECK_ERROR();
1473 CHECK(check_sljit_emit_op_custom(compiler, instruction, size));
1474
1475 if (size == 2)
1476 return push_inst16(compiler, *(sljit_u16*)instruction);
1477 return push_inst32(compiler, *(sljit_ins*)instruction);
1478 }
1479
1480 /* --------------------------------------------------------------------- */
1481 /* Floating point operators */
1482 /* --------------------------------------------------------------------- */
1483
1484 #define FPU_LOAD (1 << 20)
1485
emit_fop_mem(struct sljit_compiler * compiler,sljit_s32 flags,sljit_s32 reg,sljit_s32 arg,sljit_sw argw)1486 static sljit_s32 emit_fop_mem(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg, sljit_s32 arg, sljit_sw argw)
1487 {
1488 sljit_uw imm;
1489 sljit_sw inst = VSTR_F32 | (flags & (SLJIT_F32_OP | FPU_LOAD));
1490
1491 SLJIT_ASSERT(arg & SLJIT_MEM);
1492
1493 /* Fast loads and stores. */
1494 if (SLJIT_UNLIKELY(arg & OFFS_REG_MASK)) {
1495 FAIL_IF(push_inst32(compiler, ADD_W | RD4(TMP_REG1) | RN4(arg & REG_MASK) | RM4(OFFS_REG(arg)) | ((argw & 0x3) << 6)));
1496 arg = SLJIT_MEM | TMP_REG1;
1497 argw = 0;
1498 }
1499
1500 if ((arg & REG_MASK) && (argw & 0x3) == 0) {
1501 if (!(argw & ~0x3fc))
1502 return push_inst32(compiler, inst | 0x800000 | RN4(arg & REG_MASK) | DD4(reg) | (argw >> 2));
1503 if (!(-argw & ~0x3fc))
1504 return push_inst32(compiler, inst | RN4(arg & REG_MASK) | DD4(reg) | (-argw >> 2));
1505 }
1506
1507 if (arg & REG_MASK) {
1508 if (emit_set_delta(compiler, TMP_REG1, arg & REG_MASK, argw) != SLJIT_ERR_UNSUPPORTED) {
1509 FAIL_IF(compiler->error);
1510 return push_inst32(compiler, inst | 0x800000 | RN4(TMP_REG1) | DD4(reg));
1511 }
1512 imm = get_imm(argw & ~0x3fc);
1513 if (imm != INVALID_IMM) {
1514 FAIL_IF(push_inst32(compiler, ADD_WI | RD4(TMP_REG1) | RN4(arg & REG_MASK) | imm));
1515 return push_inst32(compiler, inst | 0x800000 | RN4(TMP_REG1) | DD4(reg) | ((argw & 0x3fc) >> 2));
1516 }
1517 imm = get_imm(-argw & ~0x3fc);
1518 if (imm != INVALID_IMM) {
1519 argw = -argw;
1520 FAIL_IF(push_inst32(compiler, SUB_WI | RD4(TMP_REG1) | RN4(arg & REG_MASK) | imm));
1521 return push_inst32(compiler, inst | RN4(TMP_REG1) | DD4(reg) | ((argw & 0x3fc) >> 2));
1522 }
1523 }
1524
1525 FAIL_IF(load_immediate(compiler, TMP_REG1, argw));
1526 if (arg & REG_MASK)
1527 FAIL_IF(push_inst16(compiler, ADD | SET_REGS44(TMP_REG1, (arg & REG_MASK))));
1528 return push_inst32(compiler, inst | 0x800000 | RN4(TMP_REG1) | DD4(reg));
1529 }
1530
sljit_emit_fop1_conv_sw_from_f64(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 src,sljit_sw srcw)1531 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_conv_sw_from_f64(struct sljit_compiler *compiler, sljit_s32 op,
1532 sljit_s32 dst, sljit_sw dstw,
1533 sljit_s32 src, sljit_sw srcw)
1534 {
1535 op ^= SLJIT_F32_OP;
1536
1537 if (src & SLJIT_MEM) {
1538 FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, TMP_FREG1, src, srcw));
1539 src = TMP_FREG1;
1540 }
1541
1542 FAIL_IF(push_inst32(compiler, VCVT_S32_F32 | (op & SLJIT_F32_OP) | DD4(TMP_FREG1) | DM4(src)));
1543
1544 if (FAST_IS_REG(dst))
1545 return push_inst32(compiler, VMOV | (1 << 20) | RT4(dst) | DN4(TMP_FREG1));
1546
1547 /* Store the integer value from a VFP register. */
1548 return emit_fop_mem(compiler, 0, TMP_FREG1, dst, dstw);
1549 }
1550
sljit_emit_fop1_conv_f64_from_sw(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 src,sljit_sw srcw)1551 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_conv_f64_from_sw(struct sljit_compiler *compiler, sljit_s32 op,
1552 sljit_s32 dst, sljit_sw dstw,
1553 sljit_s32 src, sljit_sw srcw)
1554 {
1555 sljit_s32 dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
1556
1557 op ^= SLJIT_F32_OP;
1558
1559 if (FAST_IS_REG(src))
1560 FAIL_IF(push_inst32(compiler, VMOV | RT4(src) | DN4(TMP_FREG1)));
1561 else if (src & SLJIT_MEM) {
1562 /* Load the integer value into a VFP register. */
1563 FAIL_IF(emit_fop_mem(compiler, FPU_LOAD, TMP_FREG1, src, srcw));
1564 }
1565 else {
1566 FAIL_IF(load_immediate(compiler, TMP_REG1, srcw));
1567 FAIL_IF(push_inst32(compiler, VMOV | RT4(TMP_REG1) | DN4(TMP_FREG1)));
1568 }
1569
1570 FAIL_IF(push_inst32(compiler, VCVT_F32_S32 | (op & SLJIT_F32_OP) | DD4(dst_r) | DM4(TMP_FREG1)));
1571
1572 if (dst & SLJIT_MEM)
1573 return emit_fop_mem(compiler, (op & SLJIT_F32_OP), TMP_FREG1, dst, dstw);
1574 return SLJIT_SUCCESS;
1575 }
1576
sljit_emit_fop1_cmp(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 src1,sljit_sw src1w,sljit_s32 src2,sljit_sw src2w)1577 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_cmp(struct sljit_compiler *compiler, sljit_s32 op,
1578 sljit_s32 src1, sljit_sw src1w,
1579 sljit_s32 src2, sljit_sw src2w)
1580 {
1581 op ^= SLJIT_F32_OP;
1582
1583 if (src1 & SLJIT_MEM) {
1584 emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, TMP_FREG1, src1, src1w);
1585 src1 = TMP_FREG1;
1586 }
1587
1588 if (src2 & SLJIT_MEM) {
1589 emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, TMP_FREG2, src2, src2w);
1590 src2 = TMP_FREG2;
1591 }
1592
1593 FAIL_IF(push_inst32(compiler, VCMP_F32 | (op & SLJIT_F32_OP) | DD4(src1) | DM4(src2)));
1594 return push_inst32(compiler, VMRS);
1595 }
1596
sljit_emit_fop1(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 src,sljit_sw srcw)1597 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop1(struct sljit_compiler *compiler, sljit_s32 op,
1598 sljit_s32 dst, sljit_sw dstw,
1599 sljit_s32 src, sljit_sw srcw)
1600 {
1601 sljit_s32 dst_r;
1602
1603 CHECK_ERROR();
1604
1605 SLJIT_COMPILE_ASSERT((SLJIT_F32_OP == 0x100), float_transfer_bit_error);
1606 SELECT_FOP1_OPERATION_WITH_CHECKS(compiler, op, dst, dstw, src, srcw);
1607
1608 dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
1609
1610 if (GET_OPCODE(op) != SLJIT_CONV_F64_FROM_F32)
1611 op ^= SLJIT_F32_OP;
1612
1613 if (src & SLJIT_MEM) {
1614 emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, dst_r, src, srcw);
1615 src = dst_r;
1616 }
1617
1618 switch (GET_OPCODE(op)) {
1619 case SLJIT_MOV_F64:
1620 if (src != dst_r) {
1621 if (dst_r != TMP_FREG1)
1622 FAIL_IF(push_inst32(compiler, VMOV_F32 | (op & SLJIT_F32_OP) | DD4(dst_r) | DM4(src)));
1623 else
1624 dst_r = src;
1625 }
1626 break;
1627 case SLJIT_NEG_F64:
1628 FAIL_IF(push_inst32(compiler, VNEG_F32 | (op & SLJIT_F32_OP) | DD4(dst_r) | DM4(src)));
1629 break;
1630 case SLJIT_ABS_F64:
1631 FAIL_IF(push_inst32(compiler, VABS_F32 | (op & SLJIT_F32_OP) | DD4(dst_r) | DM4(src)));
1632 break;
1633 case SLJIT_CONV_F64_FROM_F32:
1634 FAIL_IF(push_inst32(compiler, VCVT_F64_F32 | (op & SLJIT_F32_OP) | DD4(dst_r) | DM4(src)));
1635 op ^= SLJIT_F32_OP;
1636 break;
1637 }
1638
1639 if (dst & SLJIT_MEM)
1640 return emit_fop_mem(compiler, (op & SLJIT_F32_OP), dst_r, dst, dstw);
1641 return SLJIT_SUCCESS;
1642 }
1643
sljit_emit_fop2(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 src1,sljit_sw src1w,sljit_s32 src2,sljit_sw src2w)1644 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop2(struct sljit_compiler *compiler, sljit_s32 op,
1645 sljit_s32 dst, sljit_sw dstw,
1646 sljit_s32 src1, sljit_sw src1w,
1647 sljit_s32 src2, sljit_sw src2w)
1648 {
1649 sljit_s32 dst_r;
1650
1651 CHECK_ERROR();
1652 CHECK(check_sljit_emit_fop2(compiler, op, dst, dstw, src1, src1w, src2, src2w));
1653 ADJUST_LOCAL_OFFSET(dst, dstw);
1654 ADJUST_LOCAL_OFFSET(src1, src1w);
1655 ADJUST_LOCAL_OFFSET(src2, src2w);
1656
1657 op ^= SLJIT_F32_OP;
1658
1659 dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
1660 if (src1 & SLJIT_MEM) {
1661 emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, TMP_FREG1, src1, src1w);
1662 src1 = TMP_FREG1;
1663 }
1664 if (src2 & SLJIT_MEM) {
1665 emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, TMP_FREG2, src2, src2w);
1666 src2 = TMP_FREG2;
1667 }
1668
1669 switch (GET_OPCODE(op)) {
1670 case SLJIT_ADD_F64:
1671 FAIL_IF(push_inst32(compiler, VADD_F32 | (op & SLJIT_F32_OP) | DD4(dst_r) | DN4(src1) | DM4(src2)));
1672 break;
1673 case SLJIT_SUB_F64:
1674 FAIL_IF(push_inst32(compiler, VSUB_F32 | (op & SLJIT_F32_OP) | DD4(dst_r) | DN4(src1) | DM4(src2)));
1675 break;
1676 case SLJIT_MUL_F64:
1677 FAIL_IF(push_inst32(compiler, VMUL_F32 | (op & SLJIT_F32_OP) | DD4(dst_r) | DN4(src1) | DM4(src2)));
1678 break;
1679 case SLJIT_DIV_F64:
1680 FAIL_IF(push_inst32(compiler, VDIV_F32 | (op & SLJIT_F32_OP) | DD4(dst_r) | DN4(src1) | DM4(src2)));
1681 break;
1682 }
1683
1684 if (!(dst & SLJIT_MEM))
1685 return SLJIT_SUCCESS;
1686 return emit_fop_mem(compiler, (op & SLJIT_F32_OP), TMP_FREG1, dst, dstw);
1687 }
1688
1689 #undef FPU_LOAD
1690
1691 /* --------------------------------------------------------------------- */
1692 /* Other instructions */
1693 /* --------------------------------------------------------------------- */
1694
sljit_emit_fast_enter(struct sljit_compiler * compiler,sljit_s32 dst,sljit_sw dstw)1695 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fast_enter(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw)
1696 {
1697 CHECK_ERROR();
1698 CHECK(check_sljit_emit_fast_enter(compiler, dst, dstw));
1699 ADJUST_LOCAL_OFFSET(dst, dstw);
1700
1701 SLJIT_ASSERT(reg_map[TMP_REG2] == 14);
1702
1703 if (FAST_IS_REG(dst))
1704 return push_inst16(compiler, MOV | SET_REGS44(dst, TMP_REG2));
1705
1706 /* Memory. */
1707 return emit_op_mem(compiler, WORD_SIZE | STORE, TMP_REG2, dst, dstw, TMP_REG1);
1708 }
1709
sljit_emit_fast_return(struct sljit_compiler * compiler,sljit_s32 src,sljit_sw srcw)1710 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fast_return(struct sljit_compiler *compiler, sljit_s32 src, sljit_sw srcw)
1711 {
1712 CHECK_ERROR();
1713 CHECK(check_sljit_emit_fast_return(compiler, src, srcw));
1714 ADJUST_LOCAL_OFFSET(src, srcw);
1715
1716 SLJIT_ASSERT(reg_map[TMP_REG2] == 14);
1717
1718 if (FAST_IS_REG(src))
1719 FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(TMP_REG2, src)));
1720 else
1721 FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG2, src, srcw, TMP_REG2));
1722
1723 return push_inst16(compiler, BX | RN3(TMP_REG2));
1724 }
1725
1726 /* --------------------------------------------------------------------- */
1727 /* Conditional instructions */
1728 /* --------------------------------------------------------------------- */
1729
get_cc(sljit_s32 type)1730 static sljit_uw get_cc(sljit_s32 type)
1731 {
1732 switch (type) {
1733 case SLJIT_EQUAL:
1734 case SLJIT_MUL_NOT_OVERFLOW:
1735 case SLJIT_EQUAL_F64:
1736 return 0x0;
1737
1738 case SLJIT_NOT_EQUAL:
1739 case SLJIT_MUL_OVERFLOW:
1740 case SLJIT_NOT_EQUAL_F64:
1741 return 0x1;
1742
1743 case SLJIT_LESS:
1744 case SLJIT_LESS_F64:
1745 return 0x3;
1746
1747 case SLJIT_GREATER_EQUAL:
1748 case SLJIT_GREATER_EQUAL_F64:
1749 return 0x2;
1750
1751 case SLJIT_GREATER:
1752 case SLJIT_GREATER_F64:
1753 return 0x8;
1754
1755 case SLJIT_LESS_EQUAL:
1756 case SLJIT_LESS_EQUAL_F64:
1757 return 0x9;
1758
1759 case SLJIT_SIG_LESS:
1760 return 0xb;
1761
1762 case SLJIT_SIG_GREATER_EQUAL:
1763 return 0xa;
1764
1765 case SLJIT_SIG_GREATER:
1766 return 0xc;
1767
1768 case SLJIT_SIG_LESS_EQUAL:
1769 return 0xd;
1770
1771 case SLJIT_OVERFLOW:
1772 case SLJIT_UNORDERED_F64:
1773 return 0x6;
1774
1775 case SLJIT_NOT_OVERFLOW:
1776 case SLJIT_ORDERED_F64:
1777 return 0x7;
1778
1779 default: /* SLJIT_JUMP */
1780 SLJIT_UNREACHABLE();
1781 return 0xe;
1782 }
1783 }
1784
sljit_emit_label(struct sljit_compiler * compiler)1785 SLJIT_API_FUNC_ATTRIBUTE struct sljit_label* sljit_emit_label(struct sljit_compiler *compiler)
1786 {
1787 struct sljit_label *label;
1788
1789 CHECK_ERROR_PTR();
1790 CHECK_PTR(check_sljit_emit_label(compiler));
1791
1792 if (compiler->last_label && compiler->last_label->size == compiler->size)
1793 return compiler->last_label;
1794
1795 label = (struct sljit_label*)ensure_abuf(compiler, sizeof(struct sljit_label));
1796 PTR_FAIL_IF(!label);
1797 set_label(label, compiler);
1798 return label;
1799 }
1800
sljit_emit_jump(struct sljit_compiler * compiler,sljit_s32 type)1801 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_jump(struct sljit_compiler *compiler, sljit_s32 type)
1802 {
1803 struct sljit_jump *jump;
1804 sljit_ins cc;
1805
1806 CHECK_ERROR_PTR();
1807 CHECK_PTR(check_sljit_emit_jump(compiler, type));
1808
1809 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1810 PTR_FAIL_IF(!jump);
1811 set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
1812 type &= 0xff;
1813
1814 PTR_FAIL_IF(emit_imm32_const(compiler, TMP_REG1, 0));
1815 if (type < SLJIT_JUMP) {
1816 jump->flags |= IS_COND;
1817 cc = get_cc(type);
1818 jump->flags |= cc << 8;
1819 PTR_FAIL_IF(push_inst16(compiler, IT | (cc << 4) | 0x8));
1820 }
1821
1822 jump->addr = compiler->size;
1823 if (type <= SLJIT_JUMP)
1824 PTR_FAIL_IF(push_inst16(compiler, BX | RN3(TMP_REG1)));
1825 else {
1826 jump->flags |= IS_BL;
1827 PTR_FAIL_IF(push_inst16(compiler, BLX | RN3(TMP_REG1)));
1828 }
1829
1830 return jump;
1831 }
1832
1833 #ifdef __SOFTFP__
1834
softfloat_call_with_args(struct sljit_compiler * compiler,sljit_s32 arg_types,sljit_s32 * src)1835 static sljit_s32 softfloat_call_with_args(struct sljit_compiler *compiler, sljit_s32 arg_types, sljit_s32 *src)
1836 {
1837 sljit_s32 stack_offset = 0;
1838 sljit_s32 arg_count = 0;
1839 sljit_s32 word_arg_offset = 0;
1840 sljit_s32 float_arg_count = 0;
1841 sljit_s32 types = 0;
1842 sljit_s32 src_offset = 4 * sizeof(sljit_sw);
1843 sljit_u8 offsets[4];
1844
1845 if (src && FAST_IS_REG(*src))
1846 src_offset = reg_map[*src] * sizeof(sljit_sw);
1847
1848 arg_types >>= SLJIT_DEF_SHIFT;
1849
1850 while (arg_types) {
1851 types = (types << SLJIT_DEF_SHIFT) | (arg_types & SLJIT_DEF_MASK);
1852
1853 switch (arg_types & SLJIT_DEF_MASK) {
1854 case SLJIT_ARG_TYPE_F32:
1855 offsets[arg_count] = (sljit_u8)stack_offset;
1856 stack_offset += sizeof(sljit_f32);
1857 arg_count++;
1858 float_arg_count++;
1859 break;
1860 case SLJIT_ARG_TYPE_F64:
1861 if (stack_offset & 0x7)
1862 stack_offset += sizeof(sljit_sw);
1863 offsets[arg_count] = (sljit_u8)stack_offset;
1864 stack_offset += sizeof(sljit_f64);
1865 arg_count++;
1866 float_arg_count++;
1867 break;
1868 default:
1869 offsets[arg_count] = (sljit_u8)stack_offset;
1870 stack_offset += sizeof(sljit_sw);
1871 arg_count++;
1872 word_arg_offset += sizeof(sljit_sw);
1873 break;
1874 }
1875
1876 arg_types >>= SLJIT_DEF_SHIFT;
1877 }
1878
1879 if (stack_offset > 16)
1880 FAIL_IF(push_inst16(compiler, SUB_SP | (((stack_offset - 16) + 0x7) & ~0x7) >> 2));
1881
1882 SLJIT_ASSERT(reg_map[TMP_REG1] == 12);
1883
1884 /* Process arguments in reversed direction. */
1885 while (types) {
1886 switch (types & SLJIT_DEF_MASK) {
1887 case SLJIT_ARG_TYPE_F32:
1888 arg_count--;
1889 float_arg_count--;
1890 stack_offset = offsets[arg_count];
1891
1892 if (stack_offset < 16) {
1893 if (src_offset == stack_offset) {
1894 FAIL_IF(push_inst16(compiler, MOV | (src_offset << 1) | 4 | (1 << 7)));
1895 *src = TMP_REG1;
1896 }
1897 FAIL_IF(push_inst32(compiler, VMOV | 0x100000 | (float_arg_count << 16) | (stack_offset << 10)));
1898 } else
1899 FAIL_IF(push_inst32(compiler, VSTR_F32 | 0x800000 | RN4(SLJIT_SP) | (float_arg_count << 12) | ((stack_offset - 16) >> 2)));
1900 break;
1901 case SLJIT_ARG_TYPE_F64:
1902 arg_count--;
1903 float_arg_count--;
1904 stack_offset = offsets[arg_count];
1905
1906 SLJIT_ASSERT((stack_offset & 0x7) == 0);
1907
1908 if (stack_offset < 16) {
1909 if (src_offset == stack_offset || src_offset == stack_offset + sizeof(sljit_sw)) {
1910 FAIL_IF(push_inst16(compiler, MOV | (src_offset << 1) | 4 | (1 << 7)));
1911 *src = TMP_REG1;
1912 }
1913 FAIL_IF(push_inst32(compiler, VMOV2 | 0x100000 | (stack_offset << 10) | ((stack_offset + sizeof(sljit_sw)) << 14) | float_arg_count));
1914 } else
1915 FAIL_IF(push_inst32(compiler, VSTR_F32 | 0x800100 | RN4(SLJIT_SP) | (float_arg_count << 12) | ((stack_offset - 16) >> 2)));
1916 break;
1917 default:
1918 arg_count--;
1919 word_arg_offset -= sizeof(sljit_sw);
1920 stack_offset = offsets[arg_count];
1921
1922 SLJIT_ASSERT(stack_offset >= word_arg_offset);
1923
1924 if (stack_offset != word_arg_offset) {
1925 if (stack_offset < 16) {
1926 if (src_offset == stack_offset) {
1927 FAIL_IF(push_inst16(compiler, MOV | (src_offset << 1) | 4 | (1 << 7)));
1928 *src = TMP_REG1;
1929 }
1930 else if (src_offset == word_arg_offset) {
1931 *src = 1 + (stack_offset >> 2);
1932 src_offset = stack_offset;
1933 }
1934 FAIL_IF(push_inst16(compiler, MOV | (stack_offset >> 2) | (word_arg_offset << 1)));
1935 } else
1936 FAIL_IF(push_inst16(compiler, STR_SP | (word_arg_offset << 6) | ((stack_offset - 16) >> 2)));
1937 }
1938 break;
1939 }
1940
1941 types >>= SLJIT_DEF_SHIFT;
1942 }
1943
1944 return SLJIT_SUCCESS;
1945 }
1946
softfloat_post_call_with_args(struct sljit_compiler * compiler,sljit_s32 arg_types)1947 static sljit_s32 softfloat_post_call_with_args(struct sljit_compiler *compiler, sljit_s32 arg_types)
1948 {
1949 sljit_s32 stack_size = 0;
1950
1951 if ((arg_types & SLJIT_DEF_MASK) == SLJIT_ARG_TYPE_F32)
1952 FAIL_IF(push_inst32(compiler, VMOV | (0 << 16) | (0 << 12)));
1953 if ((arg_types & SLJIT_DEF_MASK) == SLJIT_ARG_TYPE_F64)
1954 FAIL_IF(push_inst32(compiler, VMOV2 | (1 << 16) | (0 << 12) | 0));
1955
1956 arg_types >>= SLJIT_DEF_SHIFT;
1957
1958 while (arg_types) {
1959 switch (arg_types & SLJIT_DEF_MASK) {
1960 case SLJIT_ARG_TYPE_F32:
1961 stack_size += sizeof(sljit_f32);
1962 break;
1963 case SLJIT_ARG_TYPE_F64:
1964 if (stack_size & 0x7)
1965 stack_size += sizeof(sljit_sw);
1966 stack_size += sizeof(sljit_f64);
1967 break;
1968 default:
1969 stack_size += sizeof(sljit_sw);
1970 break;
1971 }
1972
1973 arg_types >>= SLJIT_DEF_SHIFT;
1974 }
1975
1976 if (stack_size <= 16)
1977 return SLJIT_SUCCESS;
1978
1979 return push_inst16(compiler, ADD_SP | ((((stack_size - 16) + 0x7) & ~0x7) >> 2));
1980 }
1981
1982 #else
1983
hardfloat_call_with_args(struct sljit_compiler * compiler,sljit_s32 arg_types)1984 static sljit_s32 hardfloat_call_with_args(struct sljit_compiler *compiler, sljit_s32 arg_types)
1985 {
1986 sljit_u32 remap = 0;
1987 sljit_u32 offset = 0;
1988 sljit_u32 new_offset, mask;
1989
1990 /* Remove return value. */
1991 arg_types >>= SLJIT_DEF_SHIFT;
1992
1993 while (arg_types) {
1994 if ((arg_types & SLJIT_DEF_MASK) == SLJIT_ARG_TYPE_F32) {
1995 new_offset = 0;
1996 mask = 1;
1997
1998 while (remap & mask) {
1999 new_offset++;
2000 mask <<= 1;
2001 }
2002 remap |= mask;
2003
2004 if (offset != new_offset)
2005 FAIL_IF(push_inst32(compiler, VMOV_F32 | DD4((new_offset >> 1) + 1)
2006 | ((new_offset & 0x1) ? 0x400000 : 0) | DM4((offset >> 1) + 1)));
2007
2008 offset += 2;
2009 }
2010 else if ((arg_types & SLJIT_DEF_MASK) == SLJIT_ARG_TYPE_F64) {
2011 new_offset = 0;
2012 mask = 3;
2013
2014 while (remap & mask) {
2015 new_offset += 2;
2016 mask <<= 2;
2017 }
2018 remap |= mask;
2019
2020 if (offset != new_offset)
2021 FAIL_IF(push_inst32(compiler, VMOV_F32 | SLJIT_F32_OP | DD4((new_offset >> 1) + 1) | DM4((offset >> 1) + 1)));
2022
2023 offset += 2;
2024 }
2025 arg_types >>= SLJIT_DEF_SHIFT;
2026 }
2027
2028 return SLJIT_SUCCESS;
2029 }
2030
2031 #endif
2032
sljit_emit_call(struct sljit_compiler * compiler,sljit_s32 type,sljit_s32 arg_types)2033 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_call(struct sljit_compiler *compiler, sljit_s32 type,
2034 sljit_s32 arg_types)
2035 {
2036 #ifdef __SOFTFP__
2037 struct sljit_jump *jump;
2038 #endif
2039
2040 CHECK_ERROR_PTR();
2041 CHECK_PTR(check_sljit_emit_call(compiler, type, arg_types));
2042
2043 #ifdef __SOFTFP__
2044 PTR_FAIL_IF(softfloat_call_with_args(compiler, arg_types, NULL));
2045
2046 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2047 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2048 compiler->skip_checks = 1;
2049 #endif
2050
2051 jump = sljit_emit_jump(compiler, type);
2052 PTR_FAIL_IF(jump == NULL);
2053
2054 PTR_FAIL_IF(softfloat_post_call_with_args(compiler, arg_types));
2055 return jump;
2056 #else
2057 PTR_FAIL_IF(hardfloat_call_with_args(compiler, arg_types));
2058
2059 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2060 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2061 compiler->skip_checks = 1;
2062 #endif
2063
2064 return sljit_emit_jump(compiler, type);
2065 #endif
2066 }
2067
sljit_emit_ijump(struct sljit_compiler * compiler,sljit_s32 type,sljit_s32 src,sljit_sw srcw)2068 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_ijump(struct sljit_compiler *compiler, sljit_s32 type, sljit_s32 src, sljit_sw srcw)
2069 {
2070 struct sljit_jump *jump;
2071
2072 CHECK_ERROR();
2073 CHECK(check_sljit_emit_ijump(compiler, type, src, srcw));
2074 ADJUST_LOCAL_OFFSET(src, srcw);
2075
2076 SLJIT_ASSERT(reg_map[TMP_REG1] != 14);
2077
2078 if (!(src & SLJIT_IMM)) {
2079 if (FAST_IS_REG(src)) {
2080 SLJIT_ASSERT(reg_map[src] != 14);
2081 return push_inst16(compiler, (type <= SLJIT_JUMP ? BX : BLX) | RN3(src));
2082 }
2083
2084 FAIL_IF(emit_op_mem(compiler, WORD_SIZE, type <= SLJIT_JUMP ? TMP_PC : TMP_REG1, src, srcw, TMP_REG1));
2085 if (type >= SLJIT_FAST_CALL)
2086 return push_inst16(compiler, BLX | RN3(TMP_REG1));
2087 }
2088
2089 /* These jumps are converted to jump/call instructions when possible. */
2090 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
2091 FAIL_IF(!jump);
2092 set_jump(jump, compiler, JUMP_ADDR | ((type >= SLJIT_FAST_CALL) ? IS_BL : 0));
2093 jump->u.target = srcw;
2094
2095 FAIL_IF(emit_imm32_const(compiler, TMP_REG1, 0));
2096 jump->addr = compiler->size;
2097 return push_inst16(compiler, (type <= SLJIT_JUMP ? BX : BLX) | RN3(TMP_REG1));
2098 }
2099
sljit_emit_icall(struct sljit_compiler * compiler,sljit_s32 type,sljit_s32 arg_types,sljit_s32 src,sljit_sw srcw)2100 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_icall(struct sljit_compiler *compiler, sljit_s32 type,
2101 sljit_s32 arg_types,
2102 sljit_s32 src, sljit_sw srcw)
2103 {
2104 CHECK_ERROR();
2105 CHECK(check_sljit_emit_icall(compiler, type, arg_types, src, srcw));
2106
2107 #ifdef __SOFTFP__
2108 if (src & SLJIT_MEM) {
2109 FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG1, src, srcw, TMP_REG1));
2110 src = TMP_REG1;
2111 }
2112
2113 FAIL_IF(softfloat_call_with_args(compiler, arg_types, &src));
2114
2115 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2116 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2117 compiler->skip_checks = 1;
2118 #endif
2119
2120 FAIL_IF(sljit_emit_ijump(compiler, type, src, srcw));
2121
2122 return softfloat_post_call_with_args(compiler, arg_types);
2123 #else /* !__SOFTFP__ */
2124 FAIL_IF(hardfloat_call_with_args(compiler, arg_types));
2125
2126 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2127 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2128 compiler->skip_checks = 1;
2129 #endif
2130
2131 return sljit_emit_ijump(compiler, type, src, srcw);
2132 #endif /* __SOFTFP__ */
2133 }
2134
sljit_emit_op_flags(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 type)2135 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_flags(struct sljit_compiler *compiler, sljit_s32 op,
2136 sljit_s32 dst, sljit_sw dstw,
2137 sljit_s32 type)
2138 {
2139 sljit_s32 dst_r, flags = GET_ALL_FLAGS(op);
2140 sljit_ins cc;
2141
2142 CHECK_ERROR();
2143 CHECK(check_sljit_emit_op_flags(compiler, op, dst, dstw, type));
2144 ADJUST_LOCAL_OFFSET(dst, dstw);
2145
2146 op = GET_OPCODE(op);
2147 cc = get_cc(type & 0xff);
2148 dst_r = FAST_IS_REG(dst) ? dst : TMP_REG1;
2149
2150 if (op < SLJIT_ADD) {
2151 FAIL_IF(push_inst16(compiler, IT | (cc << 4) | (((cc & 0x1) ^ 0x1) << 3) | 0x4));
2152 if (reg_map[dst_r] > 7) {
2153 FAIL_IF(push_inst32(compiler, MOV_WI | RD4(dst_r) | 1));
2154 FAIL_IF(push_inst32(compiler, MOV_WI | RD4(dst_r) | 0));
2155 } else {
2156 /* The movsi (immediate) instruction does not set flags in IT block. */
2157 FAIL_IF(push_inst16(compiler, MOVSI | RDN3(dst_r) | 1));
2158 FAIL_IF(push_inst16(compiler, MOVSI | RDN3(dst_r) | 0));
2159 }
2160 if (!(dst & SLJIT_MEM))
2161 return SLJIT_SUCCESS;
2162 return emit_op_mem(compiler, WORD_SIZE | STORE, TMP_REG1, dst, dstw, TMP_REG2);
2163 }
2164
2165 if (dst & SLJIT_MEM)
2166 FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG1, dst, dstw, TMP_REG2));
2167
2168 if (op == SLJIT_AND) {
2169 FAIL_IF(push_inst16(compiler, IT | (cc << 4) | (((cc & 0x1) ^ 0x1) << 3) | 0x4));
2170 FAIL_IF(push_inst32(compiler, ANDI | RN4(dst_r) | RD4(dst_r) | 1));
2171 FAIL_IF(push_inst32(compiler, ANDI | RN4(dst_r) | RD4(dst_r) | 0));
2172 }
2173 else {
2174 FAIL_IF(push_inst16(compiler, IT | (cc << 4) | 0x8));
2175 FAIL_IF(push_inst32(compiler, ((op == SLJIT_OR) ? ORRI : EORI) | RN4(dst_r) | RD4(dst_r) | 1));
2176 }
2177
2178 if (dst & SLJIT_MEM)
2179 FAIL_IF(emit_op_mem(compiler, WORD_SIZE | STORE, TMP_REG1, dst, dstw, TMP_REG2));
2180
2181 if (!(flags & SLJIT_SET_Z))
2182 return SLJIT_SUCCESS;
2183
2184 /* The condition must always be set, even if the ORR/EORI is not executed above. */
2185 return push_inst32(compiler, MOV_W | SET_FLAGS | RD4(TMP_REG1) | RM4(dst_r));
2186 }
2187
sljit_emit_cmov(struct sljit_compiler * compiler,sljit_s32 type,sljit_s32 dst_reg,sljit_s32 src,sljit_sw srcw)2188 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_cmov(struct sljit_compiler *compiler, sljit_s32 type,
2189 sljit_s32 dst_reg,
2190 sljit_s32 src, sljit_sw srcw)
2191 {
2192 sljit_uw cc, tmp;
2193
2194 CHECK_ERROR();
2195 CHECK(check_sljit_emit_cmov(compiler, type, dst_reg, src, srcw));
2196
2197 dst_reg &= ~SLJIT_I32_OP;
2198
2199 cc = get_cc(type & 0xff);
2200
2201 if (!(src & SLJIT_IMM)) {
2202 FAIL_IF(push_inst16(compiler, IT | (cc << 4) | 0x8));
2203 return push_inst16(compiler, MOV | SET_REGS44(dst_reg, src));
2204 }
2205
2206 tmp = (sljit_uw) srcw;
2207
2208 if (tmp < 0x10000) {
2209 /* set low 16 bits, set hi 16 bits to 0. */
2210 FAIL_IF(push_inst16(compiler, IT | (cc << 4) | 0x8));
2211 return push_inst32(compiler, MOVW | RD4(dst_reg)
2212 | COPY_BITS(tmp, 12, 16, 4) | COPY_BITS(tmp, 11, 26, 1) | COPY_BITS(tmp, 8, 12, 3) | (tmp & 0xff));
2213 }
2214
2215 tmp = get_imm(srcw);
2216 if (tmp != INVALID_IMM) {
2217 FAIL_IF(push_inst16(compiler, IT | (cc << 4) | 0x8));
2218 return push_inst32(compiler, MOV_WI | RD4(dst_reg) | tmp);
2219 }
2220
2221 tmp = get_imm(~srcw);
2222 if (tmp != INVALID_IMM) {
2223 FAIL_IF(push_inst16(compiler, IT | (cc << 4) | 0x8));
2224 return push_inst32(compiler, MVN_WI | RD4(dst_reg) | tmp);
2225 }
2226
2227 FAIL_IF(push_inst16(compiler, IT | (cc << 4) | ((cc & 0x1) << 3) | 0x4));
2228
2229 tmp = (sljit_uw) srcw;
2230 FAIL_IF(push_inst32(compiler, MOVW | RD4(dst_reg)
2231 | COPY_BITS(tmp, 12, 16, 4) | COPY_BITS(tmp, 11, 26, 1) | COPY_BITS(tmp, 8, 12, 3) | (tmp & 0xff)));
2232 return push_inst32(compiler, MOVT | RD4(dst_reg)
2233 | COPY_BITS(tmp, 12 + 16, 16, 4) | COPY_BITS(tmp, 11 + 16, 26, 1) | COPY_BITS(tmp, 8 + 16, 12, 3) | ((tmp & 0xff0000) >> 16));
2234 }
2235
sljit_emit_mem(struct sljit_compiler * compiler,sljit_s32 type,sljit_s32 reg,sljit_s32 mem,sljit_sw memw)2236 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_mem(struct sljit_compiler *compiler, sljit_s32 type,
2237 sljit_s32 reg,
2238 sljit_s32 mem, sljit_sw memw)
2239 {
2240 sljit_s32 flags;
2241 sljit_ins inst;
2242
2243 CHECK_ERROR();
2244 CHECK(check_sljit_emit_mem(compiler, type, reg, mem, memw));
2245
2246 if ((mem & OFFS_REG_MASK) || (memw > 255 && memw < -255))
2247 return SLJIT_ERR_UNSUPPORTED;
2248
2249 if (type & SLJIT_MEM_SUPP)
2250 return SLJIT_SUCCESS;
2251
2252 switch (type & 0xff) {
2253 case SLJIT_MOV:
2254 case SLJIT_MOV_U32:
2255 case SLJIT_MOV_S32:
2256 case SLJIT_MOV_P:
2257 flags = WORD_SIZE;
2258 break;
2259 case SLJIT_MOV_U8:
2260 flags = BYTE_SIZE;
2261 break;
2262 case SLJIT_MOV_S8:
2263 flags = BYTE_SIZE | SIGNED;
2264 break;
2265 case SLJIT_MOV_U16:
2266 flags = HALF_SIZE;
2267 break;
2268 case SLJIT_MOV_S16:
2269 flags = HALF_SIZE | SIGNED;
2270 break;
2271 default:
2272 SLJIT_UNREACHABLE();
2273 flags = WORD_SIZE;
2274 break;
2275 }
2276
2277 if (type & SLJIT_MEM_STORE)
2278 flags |= STORE;
2279
2280 inst = sljit_mem32[flags] | 0x900;
2281
2282 if (type & SLJIT_MEM_PRE)
2283 inst |= 0x400;
2284
2285 if (memw >= 0)
2286 inst |= 0x200;
2287 else
2288 memw = -memw;
2289
2290 return push_inst32(compiler, inst | RT4(reg) | RN4(mem & REG_MASK) | memw);
2291 }
2292
sljit_emit_const(struct sljit_compiler * compiler,sljit_s32 dst,sljit_sw dstw,sljit_sw init_value)2293 SLJIT_API_FUNC_ATTRIBUTE struct sljit_const* sljit_emit_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw, sljit_sw init_value)
2294 {
2295 struct sljit_const *const_;
2296 sljit_s32 dst_r;
2297
2298 CHECK_ERROR_PTR();
2299 CHECK_PTR(check_sljit_emit_const(compiler, dst, dstw, init_value));
2300 ADJUST_LOCAL_OFFSET(dst, dstw);
2301
2302 const_ = (struct sljit_const*)ensure_abuf(compiler, sizeof(struct sljit_const));
2303 PTR_FAIL_IF(!const_);
2304 set_const(const_, compiler);
2305
2306 dst_r = FAST_IS_REG(dst) ? dst : TMP_REG1;
2307 PTR_FAIL_IF(emit_imm32_const(compiler, dst_r, init_value));
2308
2309 if (dst & SLJIT_MEM)
2310 PTR_FAIL_IF(emit_op_mem(compiler, WORD_SIZE | STORE, dst_r, dst, dstw, TMP_REG2));
2311 return const_;
2312 }
2313
sljit_set_jump_addr(sljit_uw addr,sljit_uw new_target,sljit_sw executable_offset)2314 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_target, sljit_sw executable_offset)
2315 {
2316 sljit_u16 *inst = (sljit_u16*)addr;
2317 modify_imm32_const(inst, new_target);
2318 inst = (sljit_u16 *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
2319 SLJIT_CACHE_FLUSH(inst, inst + 4);
2320 }
2321
sljit_set_const(sljit_uw addr,sljit_sw new_constant,sljit_sw executable_offset)2322 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_const(sljit_uw addr, sljit_sw new_constant, sljit_sw executable_offset)
2323 {
2324 sljit_u16 *inst = (sljit_u16*)addr;
2325 modify_imm32_const(inst, new_constant);
2326 inst = (sljit_u16 *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
2327 SLJIT_CACHE_FLUSH(inst, inst + 4);
2328 }
2329