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, 15, 14, 13, 12, 11, 10, 9, 8, 6, 7
54 };
55
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))
58
59 #define NEGATE(uimm) ((sljit_uw)-(sljit_sw)(uimm))
60
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)
68
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)
76
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))
89
90 /* --------------------------------------------------------------------- */
91 /* Instrucion forms */
92 /* --------------------------------------------------------------------- */
93
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 VPOP 0xecbd0b00
206 #define VPUSH 0xed2d0b00
207 #define VSTR_F32 0xed000a00
208 #define VSUB_F32 0xee300a40
209
push_inst16(struct sljit_compiler * compiler,sljit_ins inst)210 static sljit_s32 push_inst16(struct sljit_compiler *compiler, sljit_ins inst)
211 {
212 sljit_u16 *ptr;
213 SLJIT_ASSERT(!(inst & 0xffff0000));
214
215 ptr = (sljit_u16*)ensure_buf(compiler, sizeof(sljit_u16));
216 FAIL_IF(!ptr);
217 *ptr = (sljit_u16)(inst);
218 compiler->size++;
219 return SLJIT_SUCCESS;
220 }
221
push_inst32(struct sljit_compiler * compiler,sljit_ins inst)222 static sljit_s32 push_inst32(struct sljit_compiler *compiler, sljit_ins inst)
223 {
224 sljit_u16 *ptr = (sljit_u16*)ensure_buf(compiler, sizeof(sljit_ins));
225 FAIL_IF(!ptr);
226 *ptr++ = (sljit_u16)(inst >> 16);
227 *ptr = (sljit_u16)(inst);
228 compiler->size += 2;
229 return SLJIT_SUCCESS;
230 }
231
emit_imm32_const(struct sljit_compiler * compiler,sljit_s32 dst,sljit_uw imm)232 static SLJIT_INLINE sljit_s32 emit_imm32_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_uw imm)
233 {
234 FAIL_IF(push_inst32(compiler, MOVW | RD4(dst)
235 | COPY_BITS(imm, 12, 16, 4) | COPY_BITS(imm, 11, 26, 1) | COPY_BITS(imm, 8, 12, 3) | (imm & 0xff)));
236 return push_inst32(compiler, MOVT | RD4(dst)
237 | 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 }
239
modify_imm32_const(sljit_u16 * inst,sljit_uw new_imm)240 static SLJIT_INLINE void modify_imm32_const(sljit_u16 *inst, sljit_uw new_imm)
241 {
242 sljit_ins dst = inst[1] & 0x0f00;
243 SLJIT_ASSERT(((inst[0] & 0xfbf0) == (MOVW >> 16)) && ((inst[2] & 0xfbf0) == (MOVT >> 16)) && dst == (inst[3] & 0x0f00));
244 inst[0] = (sljit_u16)((MOVW >> 16) | COPY_BITS(new_imm, 12, 0, 4) | COPY_BITS(new_imm, 11, 10, 1));
245 inst[1] = (sljit_u16)(dst | COPY_BITS(new_imm, 8, 12, 3) | (new_imm & 0xff));
246 inst[2] = (sljit_u16)((MOVT >> 16) | COPY_BITS(new_imm, 12 + 16, 0, 4) | COPY_BITS(new_imm, 11 + 16, 10, 1));
247 inst[3] = (sljit_u16)(dst | COPY_BITS(new_imm, 8 + 16, 12, 3) | ((new_imm & 0xff0000) >> 16));
248 }
249
detect_jump_type(struct sljit_jump * jump,sljit_u16 * code_ptr,sljit_u16 * code,sljit_sw executable_offset)250 static SLJIT_INLINE sljit_s32 detect_jump_type(struct sljit_jump *jump, sljit_u16 *code_ptr, sljit_u16 *code, sljit_sw executable_offset)
251 {
252 sljit_sw diff;
253
254 if (jump->flags & SLJIT_REWRITABLE_JUMP)
255 return 0;
256
257 if (jump->flags & JUMP_ADDR) {
258 /* Branch to ARM code is not optimized yet. */
259 if (!(jump->u.target & 0x1))
260 return 0;
261 diff = ((sljit_sw)jump->u.target - (sljit_sw)(code_ptr + 2) - executable_offset) >> 1;
262 }
263 else {
264 SLJIT_ASSERT(jump->flags & JUMP_LABEL);
265 diff = ((sljit_sw)(code + jump->u.label->size) - (sljit_sw)(code_ptr + 2)) >> 1;
266 }
267
268 if (jump->flags & IS_COND) {
269 SLJIT_ASSERT(!(jump->flags & IS_BL));
270 if (diff <= 127 && diff >= -128) {
271 jump->flags |= PATCH_TYPE1;
272 return 5;
273 }
274 if (diff <= 524287 && diff >= -524288) {
275 jump->flags |= PATCH_TYPE2;
276 return 4;
277 }
278 /* +1 comes from the prefix IT instruction. */
279 diff--;
280 if (diff <= 8388607 && diff >= -8388608) {
281 jump->flags |= PATCH_TYPE3;
282 return 3;
283 }
284 }
285 else if (jump->flags & IS_BL) {
286 if (diff <= 8388607 && diff >= -8388608) {
287 jump->flags |= PATCH_BL;
288 return 3;
289 }
290 }
291 else {
292 if (diff <= 1023 && diff >= -1024) {
293 jump->flags |= PATCH_TYPE4;
294 return 4;
295 }
296 if (diff <= 8388607 && diff >= -8388608) {
297 jump->flags |= PATCH_TYPE5;
298 return 3;
299 }
300 }
301
302 return 0;
303 }
304
set_jump_instruction(struct sljit_jump * jump,sljit_sw executable_offset)305 static SLJIT_INLINE void set_jump_instruction(struct sljit_jump *jump, sljit_sw executable_offset)
306 {
307 sljit_s32 type = (jump->flags >> 4) & 0xf;
308 sljit_sw diff;
309 sljit_u16 *jump_inst;
310 sljit_s32 s, j1, j2;
311
312 if (SLJIT_UNLIKELY(type == 0)) {
313 modify_imm32_const((sljit_u16*)jump->addr, (jump->flags & JUMP_LABEL) ? jump->u.label->addr : jump->u.target);
314 return;
315 }
316
317 if (jump->flags & JUMP_ADDR) {
318 SLJIT_ASSERT(jump->u.target & 0x1);
319 diff = ((sljit_sw)jump->u.target - (sljit_sw)(jump->addr + sizeof(sljit_u32)) - executable_offset) >> 1;
320 }
321 else {
322 SLJIT_ASSERT(jump->u.label->addr & 0x1);
323 diff = ((sljit_sw)(jump->u.label->addr) - (sljit_sw)(jump->addr + sizeof(sljit_u32)) - executable_offset) >> 1;
324 }
325 jump_inst = (sljit_u16*)jump->addr;
326
327 switch (type) {
328 case 1:
329 /* Encoding T1 of 'B' instruction */
330 SLJIT_ASSERT(diff <= 127 && diff >= -128 && (jump->flags & IS_COND));
331 jump_inst[0] = (sljit_u16)(0xd000 | (jump->flags & 0xf00) | ((sljit_ins)diff & 0xff));
332 return;
333 case 2:
334 /* Encoding T3 of 'B' instruction */
335 SLJIT_ASSERT(diff <= 524287 && diff >= -524288 && (jump->flags & IS_COND));
336 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));
337 jump_inst[1] = (sljit_u16)(0x8000 | COPY_BITS(diff, 17, 13, 1) | COPY_BITS(diff, 18, 11, 1) | ((sljit_ins)diff & 0x7ff));
338 return;
339 case 3:
340 SLJIT_ASSERT(jump->flags & IS_COND);
341 *jump_inst++ = (sljit_u16)(IT | ((jump->flags >> 4) & 0xf0) | 0x8);
342 diff--;
343 type = 5;
344 break;
345 case 4:
346 /* Encoding T2 of 'B' instruction */
347 SLJIT_ASSERT(diff <= 1023 && diff >= -1024 && !(jump->flags & IS_COND));
348 jump_inst[0] = (sljit_u16)(0xe000 | (diff & 0x7ff));
349 return;
350 }
351
352 SLJIT_ASSERT(diff <= 8388607 && diff >= -8388608);
353
354 /* Really complex instruction form for branches. */
355 s = (diff >> 23) & 0x1;
356 j1 = (~(diff >> 22) ^ s) & 0x1;
357 j2 = (~(diff >> 21) ^ s) & 0x1;
358 jump_inst[0] = (sljit_u16)(0xf000 | ((sljit_ins)s << 10) | COPY_BITS(diff, 11, 0, 10));
359 jump_inst[1] = (sljit_u16)((j1 << 13) | (j2 << 11) | (diff & 0x7ff));
360
361 /* The others have a common form. */
362 if (type == 5) /* Encoding T4 of 'B' instruction */
363 jump_inst[1] |= 0x9000;
364 else if (type == 6) /* Encoding T1 of 'BL' instruction */
365 jump_inst[1] |= 0xd000;
366 else
367 SLJIT_UNREACHABLE();
368 }
369
sljit_generate_code(struct sljit_compiler * compiler)370 SLJIT_API_FUNC_ATTRIBUTE void* sljit_generate_code(struct sljit_compiler *compiler)
371 {
372 struct sljit_memory_fragment *buf;
373 sljit_u16 *code;
374 sljit_u16 *code_ptr;
375 sljit_u16 *buf_ptr;
376 sljit_u16 *buf_end;
377 sljit_uw half_count;
378 sljit_uw next_addr;
379 sljit_sw executable_offset;
380
381 struct sljit_label *label;
382 struct sljit_jump *jump;
383 struct sljit_const *const_;
384 struct sljit_put_label *put_label;
385
386 CHECK_ERROR_PTR();
387 CHECK_PTR(check_sljit_generate_code(compiler));
388 reverse_buf(compiler);
389
390 code = (sljit_u16*)SLJIT_MALLOC_EXEC(compiler->size * sizeof(sljit_u16), compiler->exec_allocator_data);
391 PTR_FAIL_WITH_EXEC_IF(code);
392 buf = compiler->buf;
393
394 code_ptr = code;
395 half_count = 0;
396 next_addr = 0;
397 executable_offset = SLJIT_EXEC_OFFSET(code);
398
399 label = compiler->labels;
400 jump = compiler->jumps;
401 const_ = compiler->consts;
402 put_label = compiler->put_labels;
403
404 do {
405 buf_ptr = (sljit_u16*)buf->memory;
406 buf_end = buf_ptr + (buf->used_size >> 1);
407 do {
408 *code_ptr = *buf_ptr++;
409 if (next_addr == half_count) {
410 SLJIT_ASSERT(!label || label->size >= half_count);
411 SLJIT_ASSERT(!jump || jump->addr >= half_count);
412 SLJIT_ASSERT(!const_ || const_->addr >= half_count);
413 SLJIT_ASSERT(!put_label || put_label->addr >= half_count);
414
415 /* These structures are ordered by their address. */
416 if (label && label->size == half_count) {
417 label->addr = ((sljit_uw)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset)) | 0x1;
418 label->size = (sljit_uw)(code_ptr - code);
419 label = label->next;
420 }
421 if (jump && jump->addr == half_count) {
422 jump->addr = (sljit_uw)code_ptr - ((jump->flags & IS_COND) ? 10 : 8);
423 code_ptr -= detect_jump_type(jump, code_ptr, code, executable_offset);
424 jump = jump->next;
425 }
426 if (const_ && const_->addr == half_count) {
427 const_->addr = (sljit_uw)code_ptr;
428 const_ = const_->next;
429 }
430 if (put_label && put_label->addr == half_count) {
431 SLJIT_ASSERT(put_label->label);
432 put_label->addr = (sljit_uw)code_ptr;
433 put_label = put_label->next;
434 }
435 next_addr = compute_next_addr(label, jump, const_, put_label);
436 }
437 code_ptr ++;
438 half_count ++;
439 } while (buf_ptr < buf_end);
440
441 buf = buf->next;
442 } while (buf);
443
444 if (label && label->size == half_count) {
445 label->addr = ((sljit_uw)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset)) | 0x1;
446 label->size = (sljit_uw)(code_ptr - code);
447 label = label->next;
448 }
449
450 SLJIT_ASSERT(!label);
451 SLJIT_ASSERT(!jump);
452 SLJIT_ASSERT(!const_);
453 SLJIT_ASSERT(!put_label);
454 SLJIT_ASSERT(code_ptr - code <= (sljit_sw)compiler->size);
455
456 jump = compiler->jumps;
457 while (jump) {
458 set_jump_instruction(jump, executable_offset);
459 jump = jump->next;
460 }
461
462 put_label = compiler->put_labels;
463 while (put_label) {
464 modify_imm32_const((sljit_u16 *)put_label->addr, put_label->label->addr);
465 put_label = put_label->next;
466 }
467
468 compiler->error = SLJIT_ERR_COMPILED;
469 compiler->executable_offset = executable_offset;
470 compiler->executable_size = (sljit_uw)(code_ptr - code) * sizeof(sljit_u16);
471
472 code = (sljit_u16 *)SLJIT_ADD_EXEC_OFFSET(code, executable_offset);
473 code_ptr = (sljit_u16 *)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset);
474
475 SLJIT_CACHE_FLUSH(code, code_ptr);
476 SLJIT_UPDATE_WX_FLAGS(code, code_ptr, 1);
477
478 /* Set thumb mode flag. */
479 return (void*)((sljit_uw)code | 0x1);
480 }
481
sljit_has_cpu_feature(sljit_s32 feature_type)482 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_has_cpu_feature(sljit_s32 feature_type)
483 {
484 switch (feature_type) {
485 case SLJIT_HAS_FPU:
486 #ifdef SLJIT_IS_FPU_AVAILABLE
487 return SLJIT_IS_FPU_AVAILABLE;
488 #else
489 /* Available by default. */
490 return 1;
491 #endif
492
493 case SLJIT_HAS_CLZ:
494 case SLJIT_HAS_CMOV:
495 case SLJIT_HAS_PREFETCH:
496 return 1;
497
498 default:
499 return 0;
500 }
501 }
502
503 /* --------------------------------------------------------------------- */
504 /* Core code generator functions. */
505 /* --------------------------------------------------------------------- */
506
507 #define INVALID_IMM 0x80000000
get_imm(sljit_uw imm)508 static sljit_uw get_imm(sljit_uw imm)
509 {
510 /* Thumb immediate form. */
511 sljit_s32 counter;
512
513 if (imm <= 0xff)
514 return imm;
515
516 if ((imm & 0xffff) == (imm >> 16)) {
517 /* Some special cases. */
518 if (!(imm & 0xff00))
519 return (1 << 12) | (imm & 0xff);
520 if (!(imm & 0xff))
521 return (2 << 12) | ((imm >> 8) & 0xff);
522 if ((imm & 0xff00) == ((imm & 0xff) << 8))
523 return (3 << 12) | (imm & 0xff);
524 }
525
526 /* Assembly optimization: count leading zeroes? */
527 counter = 8;
528 if (!(imm & 0xffff0000)) {
529 counter += 16;
530 imm <<= 16;
531 }
532 if (!(imm & 0xff000000)) {
533 counter += 8;
534 imm <<= 8;
535 }
536 if (!(imm & 0xf0000000)) {
537 counter += 4;
538 imm <<= 4;
539 }
540 if (!(imm & 0xc0000000)) {
541 counter += 2;
542 imm <<= 2;
543 }
544 if (!(imm & 0x80000000)) {
545 counter += 1;
546 imm <<= 1;
547 }
548 /* Since imm >= 128, this must be true. */
549 SLJIT_ASSERT(counter <= 31);
550
551 if (imm & 0x00ffffff)
552 return INVALID_IMM; /* Cannot be encoded. */
553
554 return ((imm >> 24) & 0x7f) | COPY_BITS(counter, 4, 26, 1) | COPY_BITS(counter, 1, 12, 3) | COPY_BITS(counter, 0, 7, 1);
555 }
556
load_immediate(struct sljit_compiler * compiler,sljit_s32 dst,sljit_uw imm)557 static sljit_s32 load_immediate(struct sljit_compiler *compiler, sljit_s32 dst, sljit_uw imm)
558 {
559 sljit_uw tmp;
560
561 /* MOVS cannot be used since it destroy flags. */
562
563 if (imm >= 0x10000) {
564 tmp = get_imm(imm);
565 if (tmp != INVALID_IMM)
566 return push_inst32(compiler, MOV_WI | RD4(dst) | tmp);
567 tmp = get_imm(~imm);
568 if (tmp != INVALID_IMM)
569 return push_inst32(compiler, MVN_WI | RD4(dst) | tmp);
570 }
571
572 /* set low 16 bits, set hi 16 bits to 0. */
573 FAIL_IF(push_inst32(compiler, MOVW | RD4(dst)
574 | COPY_BITS(imm, 12, 16, 4) | COPY_BITS(imm, 11, 26, 1) | COPY_BITS(imm, 8, 12, 3) | (imm & 0xff)));
575
576 /* set hi 16 bit if needed. */
577 if (imm >= 0x10000)
578 return push_inst32(compiler, MOVT | RD4(dst)
579 | COPY_BITS(imm, 12 + 16, 16, 4) | COPY_BITS(imm, 11 + 16, 26, 1) | COPY_BITS(imm, 8 + 16, 12, 3) | ((imm & 0xff0000) >> 16));
580 return SLJIT_SUCCESS;
581 }
582
583 #define ARG1_IMM 0x0010000
584 #define ARG2_IMM 0x0020000
585 /* SET_FLAGS must be 0x100000 as it is also the value of S bit (can be used for optimization). */
586 #define SET_FLAGS 0x0100000
587 #define UNUSED_RETURN 0x0200000
588
emit_op_imm(struct sljit_compiler * compiler,sljit_s32 flags,sljit_s32 dst,sljit_uw arg1,sljit_uw arg2)589 static sljit_s32 emit_op_imm(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 dst, sljit_uw arg1, sljit_uw arg2)
590 {
591 /* dst must be register, TMP_REG1
592 arg1 must be register, imm
593 arg2 must be register, imm */
594 sljit_s32 reg;
595 sljit_uw imm, nimm;
596
597 if (SLJIT_UNLIKELY((flags & (ARG1_IMM | ARG2_IMM)) == (ARG1_IMM | ARG2_IMM))) {
598 /* Both are immediates, no temporaries are used. */
599 flags &= ~ARG1_IMM;
600 FAIL_IF(load_immediate(compiler, TMP_REG1, arg1));
601 arg1 = TMP_REG1;
602 }
603
604 if (flags & (ARG1_IMM | ARG2_IMM)) {
605 reg = (sljit_s32)((flags & ARG2_IMM) ? arg1 : arg2);
606 imm = (flags & ARG2_IMM) ? arg2 : arg1;
607
608 switch (flags & 0xffff) {
609 case SLJIT_CLZ:
610 case SLJIT_MUL:
611 /* No form with immediate operand. */
612 break;
613 case SLJIT_MOV:
614 SLJIT_ASSERT(!(flags & SET_FLAGS) && (flags & ARG2_IMM) && arg1 == TMP_REG2);
615 return load_immediate(compiler, dst, imm);
616 case SLJIT_NOT:
617 if (!(flags & SET_FLAGS))
618 return load_immediate(compiler, dst, ~imm);
619 /* Since the flags should be set, we just fallback to the register mode.
620 Although some clever things could be done here, "NOT IMM" does not worth the efforts. */
621 break;
622 case SLJIT_ADD:
623 compiler->status_flags_state = SLJIT_CURRENT_FLAGS_ADD;
624 nimm = NEGATE(imm);
625 if (IS_2_LO_REGS(reg, dst)) {
626 if (imm <= 0x7)
627 return push_inst16(compiler, ADDSI3 | IMM3(imm) | RD3(dst) | RN3(reg));
628 if (nimm <= 0x7)
629 return push_inst16(compiler, SUBSI3 | IMM3(nimm) | RD3(dst) | RN3(reg));
630 if (reg == dst) {
631 if (imm <= 0xff)
632 return push_inst16(compiler, ADDSI8 | IMM8(imm) | RDN3(dst));
633 if (nimm <= 0xff)
634 return push_inst16(compiler, SUBSI8 | IMM8(nimm) | RDN3(dst));
635 }
636 }
637 if (!(flags & SET_FLAGS)) {
638 if (imm <= 0xfff)
639 return push_inst32(compiler, ADDWI | RD4(dst) | RN4(reg) | IMM12(imm));
640 if (nimm <= 0xfff)
641 return push_inst32(compiler, SUBWI | RD4(dst) | RN4(reg) | IMM12(nimm));
642 }
643 nimm = get_imm(imm);
644 if (nimm != INVALID_IMM)
645 return push_inst32(compiler, ADD_WI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | nimm);
646 nimm = get_imm(NEGATE(imm));
647 if (nimm != INVALID_IMM)
648 return push_inst32(compiler, SUB_WI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | nimm);
649 break;
650 case SLJIT_ADDC:
651 compiler->status_flags_state = SLJIT_CURRENT_FLAGS_ADD;
652 imm = get_imm(imm);
653 if (imm != INVALID_IMM)
654 return push_inst32(compiler, ADCI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
655 break;
656 case SLJIT_SUB:
657 compiler->status_flags_state = SLJIT_CURRENT_FLAGS_SUB;
658 if (flags & ARG1_IMM) {
659 if (imm == 0 && IS_2_LO_REGS(reg, dst))
660 return push_inst16(compiler, RSBSI | RD3(dst) | RN3(reg));
661 imm = get_imm(imm);
662 if (imm != INVALID_IMM)
663 return push_inst32(compiler, RSB_WI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
664 break;
665 }
666 if (flags & UNUSED_RETURN) {
667 if (imm <= 0xff && reg_map[reg] <= 7)
668 return push_inst16(compiler, CMPI | IMM8(imm) | RDN3(reg));
669 nimm = get_imm(imm);
670 if (nimm != INVALID_IMM)
671 return push_inst32(compiler, CMPI_W | RN4(reg) | nimm);
672 nimm = get_imm(NEGATE(imm));
673 if (nimm != INVALID_IMM)
674 return push_inst32(compiler, CMNI_W | RN4(reg) | nimm);
675 break;
676 }
677 nimm = NEGATE(imm);
678 if (IS_2_LO_REGS(reg, dst)) {
679 if (imm <= 0x7)
680 return push_inst16(compiler, SUBSI3 | IMM3(imm) | RD3(dst) | RN3(reg));
681 if (nimm <= 0x7)
682 return push_inst16(compiler, ADDSI3 | IMM3(nimm) | RD3(dst) | RN3(reg));
683 if (reg == dst) {
684 if (imm <= 0xff)
685 return push_inst16(compiler, SUBSI8 | IMM8(imm) | RDN3(dst));
686 if (nimm <= 0xff)
687 return push_inst16(compiler, ADDSI8 | IMM8(nimm) | RDN3(dst));
688 }
689 }
690 if (!(flags & SET_FLAGS)) {
691 if (imm <= 0xfff)
692 return push_inst32(compiler, SUBWI | RD4(dst) | RN4(reg) | IMM12(imm));
693 if (nimm <= 0xfff)
694 return push_inst32(compiler, ADDWI | RD4(dst) | RN4(reg) | IMM12(nimm));
695 }
696 nimm = get_imm(imm);
697 if (nimm != INVALID_IMM)
698 return push_inst32(compiler, SUB_WI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | nimm);
699 nimm = get_imm(NEGATE(imm));
700 if (nimm != INVALID_IMM)
701 return push_inst32(compiler, ADD_WI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | nimm);
702 break;
703 case SLJIT_SUBC:
704 compiler->status_flags_state = SLJIT_CURRENT_FLAGS_SUB;
705 if (flags & ARG1_IMM)
706 break;
707 imm = get_imm(imm);
708 if (imm != INVALID_IMM)
709 return push_inst32(compiler, SBCI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
710 break;
711 case SLJIT_AND:
712 nimm = get_imm(imm);
713 if (nimm != INVALID_IMM)
714 return push_inst32(compiler, ((flags & UNUSED_RETURN) ? TSTI : ANDI) | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | nimm);
715 imm = get_imm(~imm);
716 if (imm != INVALID_IMM)
717 return push_inst32(compiler, BICI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
718 break;
719 case SLJIT_OR:
720 nimm = get_imm(imm);
721 if (nimm != INVALID_IMM)
722 return push_inst32(compiler, ORRI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | nimm);
723 imm = get_imm(~imm);
724 if (imm != INVALID_IMM)
725 return push_inst32(compiler, ORNI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
726 break;
727 case SLJIT_XOR:
728 imm = get_imm(imm);
729 if (imm != INVALID_IMM)
730 return push_inst32(compiler, EORI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
731 break;
732 case SLJIT_SHL:
733 case SLJIT_LSHR:
734 case SLJIT_ASHR:
735 if (flags & ARG1_IMM)
736 break;
737 imm &= 0x1f;
738 if (imm == 0) {
739 if (!(flags & SET_FLAGS))
740 return push_inst16(compiler, MOV | SET_REGS44(dst, reg));
741 if (IS_2_LO_REGS(dst, reg))
742 return push_inst16(compiler, MOVS | RD3(dst) | RN3(reg));
743 return push_inst32(compiler, MOV_W | SET_FLAGS | RD4(dst) | RM4(reg));
744 }
745 switch (flags & 0xffff) {
746 case SLJIT_SHL:
747 if (IS_2_LO_REGS(dst, reg))
748 return push_inst16(compiler, LSLSI | RD3(dst) | RN3(reg) | (imm << 6));
749 return push_inst32(compiler, LSL_WI | (flags & SET_FLAGS) | RD4(dst) | RM4(reg) | IMM5(imm));
750 case SLJIT_LSHR:
751 if (IS_2_LO_REGS(dst, reg))
752 return push_inst16(compiler, LSRSI | RD3(dst) | RN3(reg) | (imm << 6));
753 return push_inst32(compiler, LSR_WI | (flags & SET_FLAGS) | RD4(dst) | RM4(reg) | IMM5(imm));
754 default: /* SLJIT_ASHR */
755 if (IS_2_LO_REGS(dst, reg))
756 return push_inst16(compiler, ASRSI | RD3(dst) | RN3(reg) | (imm << 6));
757 return push_inst32(compiler, ASR_WI | (flags & SET_FLAGS) | RD4(dst) | RM4(reg) | IMM5(imm));
758 }
759 default:
760 SLJIT_UNREACHABLE();
761 break;
762 }
763
764 if (flags & ARG2_IMM) {
765 imm = arg2;
766 arg2 = (arg1 == TMP_REG1) ? TMP_REG2 : TMP_REG1;
767 FAIL_IF(load_immediate(compiler, (sljit_s32)arg2, imm));
768 }
769 else {
770 imm = arg1;
771 arg1 = (arg2 == TMP_REG1) ? TMP_REG2 : TMP_REG1;
772 FAIL_IF(load_immediate(compiler, (sljit_s32)arg1, imm));
773 }
774
775 SLJIT_ASSERT(arg1 != arg2);
776 }
777
778 /* Both arguments are registers. */
779 switch (flags & 0xffff) {
780 case SLJIT_MOV:
781 case SLJIT_MOV_U32:
782 case SLJIT_MOV_S32:
783 case SLJIT_MOV32:
784 case SLJIT_MOV_P:
785 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG2);
786 if (dst == (sljit_s32)arg2)
787 return SLJIT_SUCCESS;
788 return push_inst16(compiler, MOV | SET_REGS44(dst, arg2));
789 case SLJIT_MOV_U8:
790 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG2);
791 if (IS_2_LO_REGS(dst, arg2))
792 return push_inst16(compiler, UXTB | RD3(dst) | RN3(arg2));
793 return push_inst32(compiler, UXTB_W | RD4(dst) | RM4(arg2));
794 case SLJIT_MOV_S8:
795 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG2);
796 if (IS_2_LO_REGS(dst, arg2))
797 return push_inst16(compiler, SXTB | RD3(dst) | RN3(arg2));
798 return push_inst32(compiler, SXTB_W | RD4(dst) | RM4(arg2));
799 case SLJIT_MOV_U16:
800 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG2);
801 if (IS_2_LO_REGS(dst, arg2))
802 return push_inst16(compiler, UXTH | RD3(dst) | RN3(arg2));
803 return push_inst32(compiler, UXTH_W | RD4(dst) | RM4(arg2));
804 case SLJIT_MOV_S16:
805 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG2);
806 if (IS_2_LO_REGS(dst, arg2))
807 return push_inst16(compiler, SXTH | RD3(dst) | RN3(arg2));
808 return push_inst32(compiler, SXTH_W | RD4(dst) | RM4(arg2));
809 case SLJIT_NOT:
810 SLJIT_ASSERT(arg1 == TMP_REG2);
811 if (IS_2_LO_REGS(dst, arg2))
812 return push_inst16(compiler, MVNS | RD3(dst) | RN3(arg2));
813 return push_inst32(compiler, MVN_W | (flags & SET_FLAGS) | RD4(dst) | RM4(arg2));
814 case SLJIT_CLZ:
815 SLJIT_ASSERT(arg1 == TMP_REG2);
816 FAIL_IF(push_inst32(compiler, CLZ | RN4(arg2) | RD4(dst) | RM4(arg2)));
817 return SLJIT_SUCCESS;
818 case SLJIT_ADD:
819 compiler->status_flags_state = SLJIT_CURRENT_FLAGS_ADD;
820 if (IS_3_LO_REGS(dst, arg1, arg2))
821 return push_inst16(compiler, ADDS | RD3(dst) | RN3(arg1) | RM3(arg2));
822 if (dst == (sljit_s32)arg1 && !(flags & SET_FLAGS))
823 return push_inst16(compiler, ADD | SET_REGS44(dst, arg2));
824 return push_inst32(compiler, ADD_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
825 case SLJIT_ADDC:
826 compiler->status_flags_state = SLJIT_CURRENT_FLAGS_ADD;
827 if (dst == (sljit_s32)arg1 && IS_2_LO_REGS(dst, arg2))
828 return push_inst16(compiler, ADCS | RD3(dst) | RN3(arg2));
829 return push_inst32(compiler, ADC_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
830 case SLJIT_SUB:
831 compiler->status_flags_state = SLJIT_CURRENT_FLAGS_SUB;
832 if (flags & UNUSED_RETURN) {
833 if (IS_2_LO_REGS(arg1, arg2))
834 return push_inst16(compiler, CMP | RD3(arg1) | RN3(arg2));
835 return push_inst16(compiler, CMP_X | SET_REGS44(arg1, arg2));
836 }
837 if (IS_3_LO_REGS(dst, arg1, arg2))
838 return push_inst16(compiler, SUBS | RD3(dst) | RN3(arg1) | RM3(arg2));
839 return push_inst32(compiler, SUB_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
840 case SLJIT_SUBC:
841 compiler->status_flags_state = SLJIT_CURRENT_FLAGS_SUB;
842 if (dst == (sljit_s32)arg1 && IS_2_LO_REGS(dst, arg2))
843 return push_inst16(compiler, SBCS | RD3(dst) | RN3(arg2));
844 return push_inst32(compiler, SBC_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
845 case SLJIT_MUL:
846 compiler->status_flags_state = 0;
847 if (!(flags & SET_FLAGS))
848 return push_inst32(compiler, MUL | RD4(dst) | RN4(arg1) | RM4(arg2));
849 SLJIT_ASSERT(dst != TMP_REG2);
850 FAIL_IF(push_inst32(compiler, SMULL | RT4(dst) | RD4(TMP_REG2) | RN4(arg1) | RM4(arg2)));
851 /* cmp TMP_REG2, dst asr #31. */
852 return push_inst32(compiler, CMP_W | RN4(TMP_REG2) | 0x70e0 | RM4(dst));
853 case SLJIT_AND:
854 if (dst == (sljit_s32)arg1 && IS_2_LO_REGS(dst, arg2))
855 return push_inst16(compiler, ANDS | RD3(dst) | RN3(arg2));
856 if ((flags & UNUSED_RETURN) && IS_2_LO_REGS(arg1, arg2))
857 return push_inst16(compiler, TST | RD3(arg1) | RN3(arg2));
858 return push_inst32(compiler, ((flags & UNUSED_RETURN) ? TST_W : AND_W) | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
859 case SLJIT_OR:
860 if (dst == (sljit_s32)arg1 && IS_2_LO_REGS(dst, arg2))
861 return push_inst16(compiler, ORRS | RD3(dst) | RN3(arg2));
862 return push_inst32(compiler, ORR_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
863 case SLJIT_XOR:
864 if (dst == (sljit_s32)arg1 && IS_2_LO_REGS(dst, arg2))
865 return push_inst16(compiler, EORS | RD3(dst) | RN3(arg2));
866 return push_inst32(compiler, EOR_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
867 case SLJIT_SHL:
868 if (dst == (sljit_s32)arg1 && IS_2_LO_REGS(dst, arg2))
869 return push_inst16(compiler, LSLS | RD3(dst) | RN3(arg2));
870 return push_inst32(compiler, LSL_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
871 case SLJIT_LSHR:
872 if (dst == (sljit_s32)arg1 && IS_2_LO_REGS(dst, arg2))
873 return push_inst16(compiler, LSRS | RD3(dst) | RN3(arg2));
874 return push_inst32(compiler, LSR_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
875 case SLJIT_ASHR:
876 if (dst == (sljit_s32)arg1 && IS_2_LO_REGS(dst, arg2))
877 return push_inst16(compiler, ASRS | RD3(dst) | RN3(arg2));
878 return push_inst32(compiler, ASR_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
879 }
880
881 SLJIT_UNREACHABLE();
882 return SLJIT_SUCCESS;
883 }
884
885 #define STORE 0x01
886 #define SIGNED 0x02
887
888 #define WORD_SIZE 0x00
889 #define BYTE_SIZE 0x04
890 #define HALF_SIZE 0x08
891 #define PRELOAD 0x0c
892
893 #define IS_WORD_SIZE(flags) (!(flags & (BYTE_SIZE | HALF_SIZE)))
894 #define OFFSET_CHECK(imm, shift) (!(argw & ~(imm << shift)))
895
896 /*
897 1st letter:
898 w = word
899 b = byte
900 h = half
901
902 2nd letter:
903 s = signed
904 u = unsigned
905
906 3rd letter:
907 l = load
908 s = store
909 */
910
911 static const sljit_ins sljit_mem16[12] = {
912 /* w u l */ 0x5800 /* ldr */,
913 /* w u s */ 0x5000 /* str */,
914 /* w s l */ 0x5800 /* ldr */,
915 /* w s s */ 0x5000 /* str */,
916
917 /* b u l */ 0x5c00 /* ldrb */,
918 /* b u s */ 0x5400 /* strb */,
919 /* b s l */ 0x5600 /* ldrsb */,
920 /* b s s */ 0x5400 /* strb */,
921
922 /* h u l */ 0x5a00 /* ldrh */,
923 /* h u s */ 0x5200 /* strh */,
924 /* h s l */ 0x5e00 /* ldrsh */,
925 /* h s s */ 0x5200 /* strh */,
926 };
927
928 static const sljit_ins sljit_mem16_imm5[12] = {
929 /* w u l */ 0x6800 /* ldr imm5 */,
930 /* w u s */ 0x6000 /* str imm5 */,
931 /* w s l */ 0x6800 /* ldr imm5 */,
932 /* w s s */ 0x6000 /* str imm5 */,
933
934 /* b u l */ 0x7800 /* ldrb imm5 */,
935 /* b u s */ 0x7000 /* strb imm5 */,
936 /* b s l */ 0x0000 /* not allowed */,
937 /* b s s */ 0x7000 /* strb imm5 */,
938
939 /* h u l */ 0x8800 /* ldrh imm5 */,
940 /* h u s */ 0x8000 /* strh imm5 */,
941 /* h s l */ 0x0000 /* not allowed */,
942 /* h s s */ 0x8000 /* strh imm5 */,
943 };
944
945 #define MEM_IMM8 0xc00
946 #define MEM_IMM12 0x800000
947 static const sljit_ins sljit_mem32[13] = {
948 /* w u l */ 0xf8500000 /* ldr.w */,
949 /* w u s */ 0xf8400000 /* str.w */,
950 /* w s l */ 0xf8500000 /* ldr.w */,
951 /* w s s */ 0xf8400000 /* str.w */,
952
953 /* b u l */ 0xf8100000 /* ldrb.w */,
954 /* b u s */ 0xf8000000 /* strb.w */,
955 /* b s l */ 0xf9100000 /* ldrsb.w */,
956 /* b s s */ 0xf8000000 /* strb.w */,
957
958 /* h u l */ 0xf8300000 /* ldrh.w */,
959 /* h u s */ 0xf8200000 /* strsh.w */,
960 /* h s l */ 0xf9300000 /* ldrsh.w */,
961 /* h s s */ 0xf8200000 /* strsh.w */,
962
963 /* p u l */ 0xf8100000 /* pld */,
964 };
965
966 /* 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)967 static sljit_s32 emit_set_delta(struct sljit_compiler *compiler, sljit_s32 dst, sljit_s32 reg, sljit_sw value)
968 {
969 sljit_uw imm;
970
971 if (value >= 0) {
972 if (value <= 0xfff)
973 return push_inst32(compiler, ADDWI | RD4(dst) | RN4(reg) | IMM12(value));
974 imm = get_imm((sljit_uw)value);
975 if (imm != INVALID_IMM)
976 return push_inst32(compiler, ADD_WI | RD4(dst) | RN4(reg) | imm);
977 }
978 else {
979 value = -value;
980 if (value <= 0xfff)
981 return push_inst32(compiler, SUBWI | RD4(dst) | RN4(reg) | IMM12(value));
982 imm = get_imm((sljit_uw)value);
983 if (imm != INVALID_IMM)
984 return push_inst32(compiler, SUB_WI | RD4(dst) | RN4(reg) | imm);
985 }
986 return SLJIT_ERR_UNSUPPORTED;
987 }
988
emit_op_mem(struct sljit_compiler * compiler,sljit_s32 flags,sljit_s32 reg,sljit_s32 arg,sljit_sw argw,sljit_s32 tmp_reg)989 static SLJIT_INLINE sljit_s32 emit_op_mem(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg,
990 sljit_s32 arg, sljit_sw argw, sljit_s32 tmp_reg)
991 {
992 sljit_s32 other_r;
993 sljit_uw tmp;
994
995 SLJIT_ASSERT(arg & SLJIT_MEM);
996 SLJIT_ASSERT((arg & REG_MASK) != tmp_reg);
997 arg &= ~SLJIT_MEM;
998
999 if (SLJIT_UNLIKELY(!(arg & REG_MASK))) {
1000 tmp = get_imm((sljit_uw)argw & ~(sljit_uw)0xfff);
1001 if (tmp != INVALID_IMM) {
1002 FAIL_IF(push_inst32(compiler, MOV_WI | RD4(tmp_reg) | tmp));
1003 return push_inst32(compiler, sljit_mem32[flags] | MEM_IMM12 | RT4(reg) | RN4(tmp_reg) | (argw & 0xfff));
1004 }
1005
1006 FAIL_IF(load_immediate(compiler, tmp_reg, (sljit_uw)argw));
1007 if (IS_2_LO_REGS(reg, tmp_reg) && sljit_mem16_imm5[flags])
1008 return push_inst16(compiler, sljit_mem16_imm5[flags] | RD3(reg) | RN3(tmp_reg));
1009 return push_inst32(compiler, sljit_mem32[flags] | MEM_IMM12 | RT4(reg) | RN4(tmp_reg));
1010 }
1011
1012 if (SLJIT_UNLIKELY(arg & OFFS_REG_MASK)) {
1013 argw &= 0x3;
1014 other_r = OFFS_REG(arg);
1015 arg &= 0xf;
1016
1017 if (!argw && IS_3_LO_REGS(reg, arg, other_r))
1018 return push_inst16(compiler, sljit_mem16[flags] | RD3(reg) | RN3(arg) | RM3(other_r));
1019 return push_inst32(compiler, sljit_mem32[flags] | RT4(reg) | RN4(arg) | RM4(other_r) | ((sljit_ins)argw << 4));
1020 }
1021
1022 if (argw > 0xfff) {
1023 tmp = get_imm((sljit_uw)argw & ~(sljit_uw)0xfff);
1024 if (tmp != INVALID_IMM) {
1025 push_inst32(compiler, ADD_WI | RD4(tmp_reg) | RN4(arg) | tmp);
1026 arg = tmp_reg;
1027 argw = argw & 0xfff;
1028 }
1029 }
1030 else if (argw < -0xff) {
1031 tmp = get_imm((sljit_uw)-argw & ~(sljit_uw)0xff);
1032 if (tmp != INVALID_IMM) {
1033 push_inst32(compiler, SUB_WI | RD4(tmp_reg) | RN4(arg) | tmp);
1034 arg = tmp_reg;
1035 argw = -(-argw & 0xff);
1036 }
1037 }
1038
1039 if (IS_2_LO_REGS(reg, arg) && sljit_mem16_imm5[flags]) {
1040 tmp = 3;
1041 if (IS_WORD_SIZE(flags)) {
1042 if (OFFSET_CHECK(0x1f, 2))
1043 tmp = 2;
1044 }
1045 else if (flags & BYTE_SIZE)
1046 {
1047 if (OFFSET_CHECK(0x1f, 0))
1048 tmp = 0;
1049 }
1050 else {
1051 SLJIT_ASSERT(flags & HALF_SIZE);
1052 if (OFFSET_CHECK(0x1f, 1))
1053 tmp = 1;
1054 }
1055
1056 if (tmp < 3)
1057 return push_inst16(compiler, sljit_mem16_imm5[flags] | RD3(reg) | RN3(arg) | ((sljit_ins)argw << (6 - tmp)));
1058 }
1059 else if (SLJIT_UNLIKELY(arg == SLJIT_SP) && IS_WORD_SIZE(flags) && OFFSET_CHECK(0xff, 2) && reg_map[reg] <= 7) {
1060 /* SP based immediate. */
1061 return push_inst16(compiler, STR_SP | (sljit_ins)((flags & STORE) ? 0 : 0x800) | RDN3(reg) | ((sljit_ins)argw >> 2));
1062 }
1063
1064 if (argw >= 0 && argw <= 0xfff)
1065 return push_inst32(compiler, sljit_mem32[flags] | MEM_IMM12 | RT4(reg) | RN4(arg) | (sljit_ins)argw);
1066 else if (argw < 0 && argw >= -0xff)
1067 return push_inst32(compiler, sljit_mem32[flags] | MEM_IMM8 | RT4(reg) | RN4(arg) | (sljit_ins)-argw);
1068
1069 SLJIT_ASSERT(arg != tmp_reg);
1070
1071 FAIL_IF(load_immediate(compiler, tmp_reg, (sljit_uw)argw));
1072 if (IS_3_LO_REGS(reg, arg, tmp_reg))
1073 return push_inst16(compiler, sljit_mem16[flags] | RD3(reg) | RN3(arg) | RM3(tmp_reg));
1074 return push_inst32(compiler, sljit_mem32[flags] | RT4(reg) | RN4(arg) | RM4(tmp_reg));
1075 }
1076
1077 /* --------------------------------------------------------------------- */
1078 /* Entry, exit */
1079 /* --------------------------------------------------------------------- */
1080
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)1081 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_enter(struct sljit_compiler *compiler,
1082 sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
1083 sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
1084 {
1085 sljit_s32 size, i, tmp, word_arg_count, saved_arg_count;
1086 sljit_uw offset;
1087 sljit_uw imm = 0;
1088 #ifdef __SOFTFP__
1089 sljit_u32 float_arg_count;
1090 #else
1091 sljit_u32 old_offset, f32_offset;
1092 sljit_u32 remap[3];
1093 sljit_u32 *remap_ptr = remap;
1094 #endif
1095
1096 CHECK_ERROR();
1097 CHECK(check_sljit_emit_enter(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size));
1098 set_emit_enter(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size);
1099
1100 tmp = SLJIT_S0 - saveds;
1101 for (i = SLJIT_S0; i > tmp; i--)
1102 imm |= (sljit_uw)1 << reg_map[i];
1103
1104 for (i = scratches; i >= SLJIT_FIRST_SAVED_REG; i--)
1105 imm |= (sljit_uw)1 << reg_map[i];
1106
1107 /* At least two registers must be set for PUSH_W and one for PUSH instruction. */
1108 FAIL_IF((imm & 0xff00)
1109 ? push_inst32(compiler, PUSH_W | (1 << 14) | imm)
1110 : push_inst16(compiler, PUSH | (1 << 8) | imm));
1111
1112 /* Stack must be aligned to 8 bytes: (LR, R4) */
1113 size = GET_SAVED_REGISTERS_SIZE(scratches, saveds, 1);
1114
1115 if (fsaveds > 0 || fscratches >= SLJIT_FIRST_SAVED_FLOAT_REG) {
1116 if ((size & SSIZE_OF(sw)) != 0) {
1117 FAIL_IF(push_inst16(compiler, SUB_SP_I | (sizeof(sljit_sw) >> 2)));
1118 size += SSIZE_OF(sw);
1119 }
1120
1121 if (fsaveds + fscratches >= SLJIT_NUMBER_OF_FLOAT_REGISTERS) {
1122 FAIL_IF(push_inst32(compiler, VPUSH | DD4(SLJIT_FS0) | ((sljit_uw)SLJIT_NUMBER_OF_SAVED_FLOAT_REGISTERS << 1)));
1123 } else {
1124 if (fsaveds > 0)
1125 FAIL_IF(push_inst32(compiler, VPUSH | DD4(SLJIT_FS0) | ((sljit_uw)fsaveds << 1)));
1126 if (fscratches >= SLJIT_FIRST_SAVED_FLOAT_REG)
1127 FAIL_IF(push_inst32(compiler, VPUSH | DD4(fscratches) | ((sljit_uw)(fscratches - (SLJIT_FIRST_SAVED_FLOAT_REG - 1)) << 1)));
1128 }
1129 }
1130
1131 local_size = ((size + local_size + 0x7) & ~0x7) - size;
1132 compiler->local_size = local_size;
1133
1134 arg_types >>= SLJIT_ARG_SHIFT;
1135 word_arg_count = 0;
1136 saved_arg_count = 0;
1137 #ifdef __SOFTFP__
1138 SLJIT_COMPILE_ASSERT(SLJIT_FR0 == 1, float_register_index_start);
1139
1140 offset = 0;
1141 float_arg_count = 0;
1142
1143 while (arg_types) {
1144 switch (arg_types & SLJIT_ARG_MASK) {
1145 case SLJIT_ARG_TYPE_F64:
1146 if (offset & 0x7)
1147 offset += sizeof(sljit_sw);
1148
1149 if (offset < 4 * sizeof(sljit_sw))
1150 FAIL_IF(push_inst32(compiler, VMOV2 | (offset << 10) | ((offset + sizeof(sljit_sw)) << 14) | float_arg_count));
1151 else
1152 FAIL_IF(push_inst32(compiler, VLDR_F32 | 0x800100 | RN4(SLJIT_SP)
1153 | (float_arg_count << 12) | ((offset + (sljit_uw)size - 4 * sizeof(sljit_sw)) >> 2)));
1154 float_arg_count++;
1155 offset += sizeof(sljit_f64) - sizeof(sljit_sw);
1156 break;
1157 case SLJIT_ARG_TYPE_F32:
1158 if (offset < 4 * sizeof(sljit_sw))
1159 FAIL_IF(push_inst32(compiler, VMOV | (float_arg_count << 16) | (offset << 10)));
1160 else
1161 FAIL_IF(push_inst32(compiler, VLDR_F32 | 0x800000 | RN4(SLJIT_SP)
1162 | (float_arg_count << 12) | ((offset + (sljit_uw)size - 4 * sizeof(sljit_sw)) >> 2)));
1163 float_arg_count++;
1164 break;
1165 default:
1166 word_arg_count++;
1167
1168 if (!(arg_types & SLJIT_ARG_TYPE_SCRATCH_REG)) {
1169 tmp = SLJIT_S0 - saved_arg_count;
1170 saved_arg_count++;
1171 } else if (word_arg_count - 1 != (sljit_s32)(offset >> 2))
1172 tmp = word_arg_count;
1173 else
1174 break;
1175
1176 SLJIT_ASSERT(reg_map[tmp] <= 7);
1177
1178 if (offset < 4 * sizeof(sljit_sw))
1179 FAIL_IF(push_inst16(compiler, MOV | RD3(tmp) | (offset << 1)));
1180 else
1181 FAIL_IF(push_inst16(compiler, LDR_SP | RDN3(tmp)
1182 | ((offset + (sljit_uw)size - 4 * sizeof(sljit_sw)) >> 2)));
1183 break;
1184 }
1185
1186 offset += sizeof(sljit_sw);
1187 arg_types >>= SLJIT_ARG_SHIFT;
1188 }
1189
1190 compiler->args_size = offset;
1191 #else
1192 offset = SLJIT_FR0;
1193 old_offset = SLJIT_FR0;
1194 f32_offset = 0;
1195
1196 while (arg_types) {
1197 switch (arg_types & SLJIT_ARG_MASK) {
1198 case SLJIT_ARG_TYPE_F64:
1199 if (offset != old_offset)
1200 *remap_ptr++ = VMOV_F32 | SLJIT_32 | DD4(offset) | DM4(old_offset);
1201 old_offset++;
1202 offset++;
1203 break;
1204 case SLJIT_ARG_TYPE_F32:
1205 if (f32_offset != 0) {
1206 *remap_ptr++ = VMOV_F32 | 0x20 | DD4(offset) | DM4(f32_offset);
1207 f32_offset = 0;
1208 } else {
1209 if (offset != old_offset)
1210 *remap_ptr++ = VMOV_F32 | DD4(offset) | DM4(old_offset);
1211 f32_offset = old_offset;
1212 old_offset++;
1213 }
1214 offset++;
1215 break;
1216 default:
1217 if (!(arg_types & SLJIT_ARG_TYPE_SCRATCH_REG)) {
1218 FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(SLJIT_S0 - saved_arg_count, SLJIT_R0 + word_arg_count)));
1219 saved_arg_count++;
1220 }
1221
1222 word_arg_count++;
1223 break;
1224 }
1225 arg_types >>= SLJIT_ARG_SHIFT;
1226 }
1227
1228 SLJIT_ASSERT((sljit_uw)(remap_ptr - remap) <= sizeof(remap));
1229
1230 while (remap_ptr > remap)
1231 FAIL_IF(push_inst32(compiler, *(--remap_ptr)));
1232 #endif
1233
1234 #ifdef _WIN32
1235 if (local_size >= 4096) {
1236 imm = get_imm(4096);
1237 SLJIT_ASSERT(imm != INVALID_IMM);
1238
1239 FAIL_IF(push_inst32(compiler, SUB_WI | RD4(SLJIT_SP) | RN4(SLJIT_SP) | imm));
1240
1241 if (local_size < 4 * 4096) {
1242 if (local_size > 2 * 4096) {
1243 if (local_size > 3 * 4096) {
1244 FAIL_IF(push_inst32(compiler, LDRI | 0x400 | RT4(TMP_REG1) | RN4(SLJIT_SP)));
1245 FAIL_IF(push_inst32(compiler, SUB_WI | RD4(SLJIT_SP) | RN4(SLJIT_SP) | imm));
1246 }
1247
1248 FAIL_IF(push_inst32(compiler, LDRI | 0x400 | RT4(TMP_REG1) | RN4(SLJIT_SP)));
1249 FAIL_IF(push_inst32(compiler, SUB_WI | RD4(SLJIT_SP) | RN4(SLJIT_SP) | imm));
1250 }
1251 } else {
1252 FAIL_IF(load_immediate(compiler, TMP_REG2, ((sljit_uw)local_size >> 12) - 1));
1253 FAIL_IF(push_inst32(compiler, LDRI | 0x400 | RT4(TMP_REG1) | RN4(SLJIT_SP)));
1254 FAIL_IF(push_inst32(compiler, SUB_WI | RD4(SLJIT_SP) | RN4(SLJIT_SP) | imm));
1255 FAIL_IF(push_inst32(compiler, SUB_WI | SET_FLAGS | RD4(TMP_REG2) | RN4(TMP_REG2) | 1));
1256 FAIL_IF(push_inst16(compiler, BCC | (0x1 << 8) /* not-equal */ | (-8 & 0xff)));
1257 }
1258
1259 FAIL_IF(push_inst32(compiler, LDRI | 0x400 | RT4(TMP_REG1) | RN4(SLJIT_SP)));
1260 local_size &= 0xfff;
1261 }
1262
1263 if (local_size >= 256) {
1264 SLJIT_ASSERT(local_size < 4096);
1265
1266 if (local_size <= (127 << 2))
1267 FAIL_IF(push_inst16(compiler, SUB_SP_I | ((sljit_uw)local_size >> 2)));
1268 else
1269 FAIL_IF(emit_op_imm(compiler, SLJIT_SUB | ARG2_IMM, SLJIT_SP, SLJIT_SP, (sljit_uw)local_size));
1270
1271 FAIL_IF(push_inst32(compiler, LDRI | 0x400 | RT4(TMP_REG1) | RN4(SLJIT_SP)));
1272 } else if (local_size > 0)
1273 FAIL_IF(push_inst32(compiler, LDRI | 0x500 | RT4(TMP_REG1) | RN4(SLJIT_SP) | (sljit_uw)local_size));
1274 #else /* !_WIN32 */
1275 if (local_size > 0) {
1276 if (local_size <= (127 << 2))
1277 FAIL_IF(push_inst16(compiler, SUB_SP_I | ((sljit_uw)local_size >> 2)));
1278 else
1279 FAIL_IF(emit_op_imm(compiler, SLJIT_SUB | ARG2_IMM, SLJIT_SP, SLJIT_SP, (sljit_uw)local_size));
1280 }
1281 #endif /* _WIN32 */
1282
1283 return SLJIT_SUCCESS;
1284 }
1285
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)1286 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_set_context(struct sljit_compiler *compiler,
1287 sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
1288 sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
1289 {
1290 sljit_s32 size;
1291
1292 CHECK_ERROR();
1293 CHECK(check_sljit_set_context(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size));
1294 set_set_context(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size);
1295
1296 size = GET_SAVED_REGISTERS_SIZE(scratches, saveds, 1);
1297
1298 if ((size & SSIZE_OF(sw)) != 0 && (fsaveds > 0 || fscratches >= SLJIT_FIRST_SAVED_FLOAT_REG))
1299 size += SSIZE_OF(sw);
1300
1301 compiler->local_size = ((size + local_size + 0x7) & ~0x7) - size;
1302 return SLJIT_SUCCESS;
1303 }
1304
emit_add_sp(struct sljit_compiler * compiler,sljit_uw imm)1305 static sljit_s32 emit_add_sp(struct sljit_compiler *compiler, sljit_uw imm)
1306 {
1307 sljit_uw imm2;
1308
1309 /* The TMP_REG1 register must keep its value. */
1310 if (imm <= (127u << 2))
1311 return push_inst16(compiler, ADD_SP_I | (imm >> 2));
1312
1313 if (imm <= 0xfff)
1314 return push_inst32(compiler, ADDWI | RD4(SLJIT_SP) | RN4(SLJIT_SP) | IMM12(imm));
1315
1316 imm2 = get_imm(imm);
1317
1318 if (imm2 != INVALID_IMM)
1319 return push_inst32(compiler, ADD_WI | RD4(SLJIT_SP) | RN4(SLJIT_SP) | imm2);
1320
1321 FAIL_IF(load_immediate(compiler, TMP_REG2, imm));
1322 return push_inst16(compiler, ADD_SP | RN3(TMP_REG2));
1323 }
1324
emit_stack_frame_release(struct sljit_compiler * compiler,sljit_s32 frame_size)1325 static sljit_s32 emit_stack_frame_release(struct sljit_compiler *compiler, sljit_s32 frame_size)
1326 {
1327 sljit_s32 local_size, fscratches, fsaveds, i, tmp;
1328 sljit_s32 lr_dst = TMP_PC;
1329 sljit_uw reg_list;
1330
1331 SLJIT_ASSERT(reg_map[TMP_REG2] == 14 && frame_size <= 128);
1332
1333 local_size = compiler->local_size;
1334 fscratches = compiler->fscratches;
1335 fsaveds = compiler->fsaveds;
1336
1337 if (fsaveds > 0 || fscratches >= SLJIT_FIRST_SAVED_FLOAT_REG) {
1338 if (local_size > 0)
1339 FAIL_IF(emit_add_sp(compiler, (sljit_uw)local_size));
1340
1341 if (fsaveds + fscratches >= SLJIT_NUMBER_OF_FLOAT_REGISTERS) {
1342 FAIL_IF(push_inst32(compiler, VPOP | DD4(SLJIT_FS0) | ((sljit_uw)SLJIT_NUMBER_OF_SAVED_FLOAT_REGISTERS << 1)));
1343 } else {
1344 if (fscratches >= SLJIT_FIRST_SAVED_FLOAT_REG)
1345 FAIL_IF(push_inst32(compiler, VPOP | DD4(fscratches) | ((sljit_uw)(fscratches - (SLJIT_FIRST_SAVED_FLOAT_REG - 1)) << 1)));
1346 if (fsaveds > 0)
1347 FAIL_IF(push_inst32(compiler, VPOP | DD4(SLJIT_FS0) | ((sljit_uw)fsaveds << 1)));
1348 }
1349
1350 local_size = GET_SAVED_REGISTERS_SIZE(compiler->scratches, compiler->saveds, 1) & 0x7;
1351 }
1352
1353 if (frame_size < 0) {
1354 lr_dst = TMP_REG2;
1355 frame_size = 0;
1356 } else if (frame_size > 0)
1357 lr_dst = 0;
1358
1359 reg_list = 0;
1360 tmp = SLJIT_S0 - compiler->saveds;
1361 for (i = SLJIT_S0; i > tmp; i--)
1362 reg_list |= (sljit_uw)1 << reg_map[i];
1363
1364 for (i = compiler->scratches; i >= SLJIT_FIRST_SAVED_REG; i--)
1365 reg_list |= (sljit_uw)1 << reg_map[i];
1366
1367 if (lr_dst == 0 && (reg_list & (reg_list - 1)) == 0) {
1368 /* The local_size does not include the saved registers. */
1369 local_size += SSIZE_OF(sw);
1370
1371 if (reg_list != 0)
1372 local_size += SSIZE_OF(sw);
1373
1374 if (frame_size > local_size)
1375 FAIL_IF(push_inst16(compiler, SUB_SP_I | ((sljit_uw)(frame_size - local_size) >> 2)));
1376 else if (frame_size < local_size)
1377 FAIL_IF(emit_add_sp(compiler, (sljit_uw)(local_size - frame_size)));
1378
1379 if (reg_list == 0)
1380 return SLJIT_SUCCESS;
1381
1382 if (compiler->saveds > 0) {
1383 SLJIT_ASSERT(reg_list == ((sljit_uw)1 << reg_map[SLJIT_S0]));
1384 lr_dst = SLJIT_S0;
1385 } else {
1386 SLJIT_ASSERT(reg_list == ((sljit_uw)1 << reg_map[SLJIT_FIRST_SAVED_REG]));
1387 lr_dst = SLJIT_FIRST_SAVED_REG;
1388 }
1389
1390 frame_size -= 2 * SSIZE_OF(sw);
1391
1392 if (reg_map[lr_dst] <= 7)
1393 return push_inst16(compiler, STR_SP | 0x800 | RDN3(lr_dst) | (sljit_uw)(frame_size >> 2));
1394
1395 return push_inst32(compiler, LDR | RT4(lr_dst) | RN4(SLJIT_SP) | (sljit_uw)frame_size);
1396 }
1397
1398 if (local_size > 0)
1399 FAIL_IF(emit_add_sp(compiler, (sljit_uw)local_size));
1400
1401 if (!(reg_list & 0xff00) && lr_dst != TMP_REG2) {
1402 if (lr_dst == TMP_PC)
1403 reg_list |= 1u << 8;
1404
1405 /* At least one register must be set for POP instruction. */
1406 SLJIT_ASSERT(reg_list != 0);
1407
1408 FAIL_IF(push_inst16(compiler, POP | reg_list));
1409 } else {
1410 if (lr_dst != 0) {
1411 if (reg_list == 0)
1412 return push_inst32(compiler, 0xf85d0b04 | RT4(lr_dst));
1413
1414 reg_list |= (sljit_uw)1 << reg_map[lr_dst];
1415 }
1416
1417 /* At least two registers must be set for POP_W instruction. */
1418 SLJIT_ASSERT((reg_list & (reg_list - 1)) != 0);
1419
1420 FAIL_IF(push_inst32(compiler, POP_W | reg_list));
1421 }
1422
1423 if (frame_size > 0)
1424 return push_inst16(compiler, SUB_SP_I | (((sljit_uw)frame_size - sizeof(sljit_sw)) >> 2));
1425 return SLJIT_SUCCESS;
1426 }
1427
sljit_emit_return_void(struct sljit_compiler * compiler)1428 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_return_void(struct sljit_compiler *compiler)
1429 {
1430 CHECK_ERROR();
1431 CHECK(check_sljit_emit_return_void(compiler));
1432
1433 return emit_stack_frame_release(compiler, 0);
1434 }
1435
1436 /* --------------------------------------------------------------------- */
1437 /* Operators */
1438 /* --------------------------------------------------------------------- */
1439
1440 #if !(defined __ARM_FEATURE_IDIV) && !(defined __ARM_ARCH_EXT_IDIV__)
1441
1442 #ifdef __cplusplus
1443 extern "C" {
1444 #endif
1445
1446 #ifdef _WIN32
1447 extern unsigned long long __rt_udiv(unsigned int denominator, unsigned int numerator);
1448 extern long long __rt_sdiv(int denominator, int numerator);
1449 #elif defined(__GNUC__)
1450 extern unsigned int __aeabi_uidivmod(unsigned int numerator, int unsigned denominator);
1451 extern int __aeabi_idivmod(int numerator, int denominator);
1452 #else
1453 #error "Software divmod functions are needed"
1454 #endif
1455
1456 #ifdef __cplusplus
1457 }
1458 #endif
1459
1460 #endif /* !__ARM_FEATURE_IDIV && !__ARM_ARCH_EXT_IDIV__ */
1461
sljit_emit_op0(struct sljit_compiler * compiler,sljit_s32 op)1462 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op0(struct sljit_compiler *compiler, sljit_s32 op)
1463 {
1464 #if !(defined __ARM_FEATURE_IDIV) && !(defined __ARM_ARCH_EXT_IDIV__)
1465 sljit_uw saved_reg_list[3];
1466 sljit_uw saved_reg_count;
1467 #endif
1468
1469 CHECK_ERROR();
1470 CHECK(check_sljit_emit_op0(compiler, op));
1471
1472 op = GET_OPCODE(op);
1473 switch (op) {
1474 case SLJIT_BREAKPOINT:
1475 return push_inst16(compiler, BKPT);
1476 case SLJIT_NOP:
1477 return push_inst16(compiler, NOP);
1478 case SLJIT_LMUL_UW:
1479 case SLJIT_LMUL_SW:
1480 return push_inst32(compiler, (op == SLJIT_LMUL_UW ? UMULL : SMULL)
1481 | RD4(SLJIT_R1) | RT4(SLJIT_R0) | RN4(SLJIT_R0) | RM4(SLJIT_R1));
1482 #if (defined __ARM_FEATURE_IDIV) || (defined __ARM_ARCH_EXT_IDIV__)
1483 case SLJIT_DIVMOD_UW:
1484 case SLJIT_DIVMOD_SW:
1485 FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(TMP_REG1, SLJIT_R0)));
1486 FAIL_IF(push_inst32(compiler, (op == SLJIT_DIVMOD_UW ? UDIV : SDIV) | RD4(SLJIT_R0) | RN4(SLJIT_R0) | RM4(SLJIT_R1)));
1487 FAIL_IF(push_inst32(compiler, MUL | RD4(SLJIT_R1) | RN4(SLJIT_R0) | RM4(SLJIT_R1)));
1488 return push_inst32(compiler, SUB_W | RD4(SLJIT_R1) | RN4(TMP_REG1) | RM4(SLJIT_R1));
1489 case SLJIT_DIV_UW:
1490 case SLJIT_DIV_SW:
1491 return push_inst32(compiler, (op == SLJIT_DIV_UW ? UDIV : SDIV) | RD4(SLJIT_R0) | RN4(SLJIT_R0) | RM4(SLJIT_R1));
1492 #else /* !__ARM_FEATURE_IDIV && !__ARM_ARCH_EXT_IDIV__ */
1493 case SLJIT_DIVMOD_UW:
1494 case SLJIT_DIVMOD_SW:
1495 case SLJIT_DIV_UW:
1496 case SLJIT_DIV_SW:
1497 SLJIT_COMPILE_ASSERT((SLJIT_DIVMOD_UW & 0x2) == 0 && SLJIT_DIV_UW - 0x2 == SLJIT_DIVMOD_UW, bad_div_opcode_assignments);
1498 SLJIT_ASSERT(reg_map[2] == 1 && reg_map[3] == 2 && reg_map[4] == 3);
1499
1500 saved_reg_count = 0;
1501 if (compiler->scratches >= 4)
1502 saved_reg_list[saved_reg_count++] = 3;
1503 if (compiler->scratches >= 3)
1504 saved_reg_list[saved_reg_count++] = 2;
1505 if (op >= SLJIT_DIV_UW)
1506 saved_reg_list[saved_reg_count++] = 1;
1507
1508 if (saved_reg_count > 0) {
1509 FAIL_IF(push_inst32(compiler, 0xf84d0d00 | (saved_reg_count >= 3 ? 16 : 8)
1510 | (saved_reg_list[0] << 12) /* str rX, [sp, #-8/-16]! */));
1511 if (saved_reg_count >= 2) {
1512 SLJIT_ASSERT(saved_reg_list[1] < 8);
1513 FAIL_IF(push_inst16(compiler, 0x9001 | (saved_reg_list[1] << 8) /* str rX, [sp, #4] */));
1514 }
1515 if (saved_reg_count >= 3) {
1516 SLJIT_ASSERT(saved_reg_list[2] < 8);
1517 FAIL_IF(push_inst16(compiler, 0x9002 | (saved_reg_list[2] << 8) /* str rX, [sp, #8] */));
1518 }
1519 }
1520
1521 #ifdef _WIN32
1522 FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(TMP_REG1, SLJIT_R0)));
1523 FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(SLJIT_R0, SLJIT_R1)));
1524 FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(SLJIT_R1, TMP_REG1)));
1525 FAIL_IF(sljit_emit_ijump(compiler, SLJIT_FAST_CALL, SLJIT_IMM,
1526 ((op | 0x2) == SLJIT_DIV_UW ? SLJIT_FUNC_ADDR(__rt_udiv) : SLJIT_FUNC_ADDR(__rt_sdiv))));
1527 #elif defined(__GNUC__)
1528 FAIL_IF(sljit_emit_ijump(compiler, SLJIT_FAST_CALL, SLJIT_IMM,
1529 ((op | 0x2) == SLJIT_DIV_UW ? SLJIT_FUNC_ADDR(__aeabi_uidivmod) : SLJIT_FUNC_ADDR(__aeabi_idivmod))));
1530 #else
1531 #error "Software divmod functions are needed"
1532 #endif
1533
1534 if (saved_reg_count > 0) {
1535 if (saved_reg_count >= 3) {
1536 SLJIT_ASSERT(saved_reg_list[2] < 8);
1537 FAIL_IF(push_inst16(compiler, 0x9802 | (saved_reg_list[2] << 8) /* ldr rX, [sp, #8] */));
1538 }
1539 if (saved_reg_count >= 2) {
1540 SLJIT_ASSERT(saved_reg_list[1] < 8);
1541 FAIL_IF(push_inst16(compiler, 0x9801 | (saved_reg_list[1] << 8) /* ldr rX, [sp, #4] */));
1542 }
1543 return push_inst32(compiler, 0xf85d0b00 | (saved_reg_count >= 3 ? 16 : 8)
1544 | (saved_reg_list[0] << 12) /* ldr rX, [sp], #8/16 */);
1545 }
1546 return SLJIT_SUCCESS;
1547 #endif /* __ARM_FEATURE_IDIV || __ARM_ARCH_EXT_IDIV__ */
1548 case SLJIT_ENDBR:
1549 case SLJIT_SKIP_FRAMES_BEFORE_RETURN:
1550 return SLJIT_SUCCESS;
1551 }
1552
1553 return SLJIT_SUCCESS;
1554 }
1555
sljit_emit_op1(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 src,sljit_sw srcw)1556 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op1(struct sljit_compiler *compiler, sljit_s32 op,
1557 sljit_s32 dst, sljit_sw dstw,
1558 sljit_s32 src, sljit_sw srcw)
1559 {
1560 sljit_s32 dst_r, flags;
1561 sljit_s32 op_flags = GET_ALL_FLAGS(op);
1562
1563 CHECK_ERROR();
1564 CHECK(check_sljit_emit_op1(compiler, op, dst, dstw, src, srcw));
1565 ADJUST_LOCAL_OFFSET(dst, dstw);
1566 ADJUST_LOCAL_OFFSET(src, srcw);
1567
1568 dst_r = FAST_IS_REG(dst) ? dst : TMP_REG1;
1569
1570 op = GET_OPCODE(op);
1571 if (op >= SLJIT_MOV && op <= SLJIT_MOV_P) {
1572 switch (op) {
1573 case SLJIT_MOV:
1574 case SLJIT_MOV_U32:
1575 case SLJIT_MOV_S32:
1576 case SLJIT_MOV32:
1577 case SLJIT_MOV_P:
1578 flags = WORD_SIZE;
1579 break;
1580 case SLJIT_MOV_U8:
1581 flags = BYTE_SIZE;
1582 if (src & SLJIT_IMM)
1583 srcw = (sljit_u8)srcw;
1584 break;
1585 case SLJIT_MOV_S8:
1586 flags = BYTE_SIZE | SIGNED;
1587 if (src & SLJIT_IMM)
1588 srcw = (sljit_s8)srcw;
1589 break;
1590 case SLJIT_MOV_U16:
1591 flags = HALF_SIZE;
1592 if (src & SLJIT_IMM)
1593 srcw = (sljit_u16)srcw;
1594 break;
1595 case SLJIT_MOV_S16:
1596 flags = HALF_SIZE | SIGNED;
1597 if (src & SLJIT_IMM)
1598 srcw = (sljit_s16)srcw;
1599 break;
1600 default:
1601 SLJIT_UNREACHABLE();
1602 flags = 0;
1603 break;
1604 }
1605
1606 if (src & SLJIT_IMM)
1607 FAIL_IF(emit_op_imm(compiler, SLJIT_MOV | ARG2_IMM, dst_r, TMP_REG2, (sljit_uw)srcw));
1608 else if (src & SLJIT_MEM) {
1609 FAIL_IF(emit_op_mem(compiler, flags, dst_r, src, srcw, TMP_REG1));
1610 } else {
1611 if (dst_r != TMP_REG1)
1612 return emit_op_imm(compiler, op, dst_r, TMP_REG2, (sljit_uw)src);
1613 dst_r = src;
1614 }
1615
1616 if (!(dst & SLJIT_MEM))
1617 return SLJIT_SUCCESS;
1618
1619 return emit_op_mem(compiler, flags | STORE, dst_r, dst, dstw, TMP_REG2);
1620 }
1621
1622 flags = HAS_FLAGS(op_flags) ? SET_FLAGS : 0;
1623
1624 if (src & SLJIT_MEM) {
1625 FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG1, src, srcw, TMP_REG1));
1626 src = TMP_REG1;
1627 }
1628
1629 emit_op_imm(compiler, flags | op, dst_r, TMP_REG2, (sljit_uw)src);
1630
1631 if (SLJIT_UNLIKELY(dst & SLJIT_MEM))
1632 return emit_op_mem(compiler, flags | STORE, dst_r, dst, dstw, TMP_REG2);
1633 return SLJIT_SUCCESS;
1634 }
1635
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)1636 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op2(struct sljit_compiler *compiler, sljit_s32 op,
1637 sljit_s32 dst, sljit_sw dstw,
1638 sljit_s32 src1, sljit_sw src1w,
1639 sljit_s32 src2, sljit_sw src2w)
1640 {
1641 sljit_s32 dst_reg, flags, src2_reg;
1642
1643 CHECK_ERROR();
1644 CHECK(check_sljit_emit_op2(compiler, op, 0, dst, dstw, src1, src1w, src2, src2w));
1645 ADJUST_LOCAL_OFFSET(dst, dstw);
1646 ADJUST_LOCAL_OFFSET(src1, src1w);
1647 ADJUST_LOCAL_OFFSET(src2, src2w);
1648
1649 dst_reg = FAST_IS_REG(dst) ? dst : TMP_REG1;
1650 flags = HAS_FLAGS(op) ? SET_FLAGS : 0;
1651
1652 if (dst == TMP_REG1)
1653 flags |= UNUSED_RETURN;
1654
1655 if (src1 & SLJIT_IMM)
1656 flags |= ARG1_IMM;
1657 else if (src1 & SLJIT_MEM) {
1658 emit_op_mem(compiler, WORD_SIZE, TMP_REG1, src1, src1w, TMP_REG1);
1659 src1w = TMP_REG1;
1660 }
1661 else
1662 src1w = src1;
1663
1664 if (src2 & SLJIT_IMM)
1665 flags |= ARG2_IMM;
1666 else if (src2 & SLJIT_MEM) {
1667 src2_reg = (!(flags & ARG1_IMM) && (src1w == TMP_REG1)) ? TMP_REG2 : TMP_REG1;
1668 emit_op_mem(compiler, WORD_SIZE, src2_reg, src2, src2w, src2_reg);
1669 src2w = src2_reg;
1670 }
1671 else
1672 src2w = src2;
1673
1674 emit_op_imm(compiler, flags | GET_OPCODE(op), dst_reg, (sljit_uw)src1w, (sljit_uw)src2w);
1675
1676 if (!(dst & SLJIT_MEM))
1677 return SLJIT_SUCCESS;
1678 return emit_op_mem(compiler, WORD_SIZE | STORE, dst_reg, dst, dstw, TMP_REG2);
1679 }
1680
sljit_emit_op2u(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 src1,sljit_sw src1w,sljit_s32 src2,sljit_sw src2w)1681 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op2u(struct sljit_compiler *compiler, sljit_s32 op,
1682 sljit_s32 src1, sljit_sw src1w,
1683 sljit_s32 src2, sljit_sw src2w)
1684 {
1685 CHECK_ERROR();
1686 CHECK(check_sljit_emit_op2(compiler, op, 1, 0, 0, src1, src1w, src2, src2w));
1687
1688 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
1689 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
1690 compiler->skip_checks = 1;
1691 #endif
1692 return sljit_emit_op2(compiler, op, TMP_REG1, 0, src1, src1w, src2, src2w);
1693 }
1694
sljit_emit_op_src(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 src,sljit_sw srcw)1695 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_src(struct sljit_compiler *compiler, sljit_s32 op,
1696 sljit_s32 src, sljit_sw srcw)
1697 {
1698 CHECK_ERROR();
1699 CHECK(check_sljit_emit_op_src(compiler, op, src, srcw));
1700 ADJUST_LOCAL_OFFSET(src, srcw);
1701
1702 switch (op) {
1703 case SLJIT_FAST_RETURN:
1704 SLJIT_ASSERT(reg_map[TMP_REG2] == 14);
1705
1706 if (FAST_IS_REG(src))
1707 FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(TMP_REG2, src)));
1708 else
1709 FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG2, src, srcw, TMP_REG2));
1710
1711 return push_inst16(compiler, BX | RN3(TMP_REG2));
1712 case SLJIT_SKIP_FRAMES_BEFORE_FAST_RETURN:
1713 return SLJIT_SUCCESS;
1714 case SLJIT_PREFETCH_L1:
1715 case SLJIT_PREFETCH_L2:
1716 case SLJIT_PREFETCH_L3:
1717 case SLJIT_PREFETCH_ONCE:
1718 return emit_op_mem(compiler, PRELOAD, TMP_PC, src, srcw, TMP_REG1);
1719 }
1720
1721 return SLJIT_SUCCESS;
1722 }
1723
sljit_get_register_index(sljit_s32 reg)1724 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_register_index(sljit_s32 reg)
1725 {
1726 CHECK_REG_INDEX(check_sljit_get_register_index(reg));
1727 return reg_map[reg];
1728 }
1729
sljit_get_float_register_index(sljit_s32 reg)1730 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_float_register_index(sljit_s32 reg)
1731 {
1732 CHECK_REG_INDEX(check_sljit_get_float_register_index(reg));
1733 return (freg_map[reg] << 1);
1734 }
1735
sljit_emit_op_custom(struct sljit_compiler * compiler,void * instruction,sljit_u32 size)1736 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_custom(struct sljit_compiler *compiler,
1737 void *instruction, sljit_u32 size)
1738 {
1739 CHECK_ERROR();
1740 CHECK(check_sljit_emit_op_custom(compiler, instruction, size));
1741
1742 if (size == 2)
1743 return push_inst16(compiler, *(sljit_u16*)instruction);
1744 return push_inst32(compiler, *(sljit_ins*)instruction);
1745 }
1746
1747 /* --------------------------------------------------------------------- */
1748 /* Floating point operators */
1749 /* --------------------------------------------------------------------- */
1750
1751 #define FPU_LOAD (1 << 20)
1752
emit_fop_mem(struct sljit_compiler * compiler,sljit_s32 flags,sljit_s32 reg,sljit_s32 arg,sljit_sw argw)1753 static sljit_s32 emit_fop_mem(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg, sljit_s32 arg, sljit_sw argw)
1754 {
1755 sljit_uw imm;
1756 sljit_ins inst = VSTR_F32 | (flags & (SLJIT_32 | FPU_LOAD));
1757
1758 SLJIT_ASSERT(arg & SLJIT_MEM);
1759
1760 /* Fast loads and stores. */
1761 if (SLJIT_UNLIKELY(arg & OFFS_REG_MASK)) {
1762 FAIL_IF(push_inst32(compiler, ADD_W | RD4(TMP_REG1) | RN4(arg & REG_MASK) | RM4(OFFS_REG(arg)) | (((sljit_uw)argw & 0x3) << 6)));
1763 arg = SLJIT_MEM | TMP_REG1;
1764 argw = 0;
1765 }
1766
1767 if ((arg & REG_MASK) && (argw & 0x3) == 0) {
1768 if (!(argw & ~0x3fc))
1769 return push_inst32(compiler, inst | 0x800000 | RN4(arg & REG_MASK) | DD4(reg) | ((sljit_uw)argw >> 2));
1770 if (!(-argw & ~0x3fc))
1771 return push_inst32(compiler, inst | RN4(arg & REG_MASK) | DD4(reg) | ((sljit_uw)-argw >> 2));
1772 }
1773
1774 if (arg & REG_MASK) {
1775 if (emit_set_delta(compiler, TMP_REG1, arg & REG_MASK, argw) != SLJIT_ERR_UNSUPPORTED) {
1776 FAIL_IF(compiler->error);
1777 return push_inst32(compiler, inst | 0x800000 | RN4(TMP_REG1) | DD4(reg));
1778 }
1779
1780 imm = get_imm((sljit_uw)argw & ~(sljit_uw)0x3fc);
1781 if (imm != INVALID_IMM) {
1782 FAIL_IF(push_inst32(compiler, ADD_WI | RD4(TMP_REG1) | RN4(arg & REG_MASK) | imm));
1783 return push_inst32(compiler, inst | 0x800000 | RN4(TMP_REG1) | DD4(reg) | (((sljit_uw)argw & 0x3fc) >> 2));
1784 }
1785
1786 imm = get_imm((sljit_uw)-argw & ~(sljit_uw)0x3fc);
1787 if (imm != INVALID_IMM) {
1788 argw = -argw;
1789 FAIL_IF(push_inst32(compiler, SUB_WI | RD4(TMP_REG1) | RN4(arg & REG_MASK) | imm));
1790 return push_inst32(compiler, inst | RN4(TMP_REG1) | DD4(reg) | (((sljit_uw)argw & 0x3fc) >> 2));
1791 }
1792 }
1793
1794 FAIL_IF(load_immediate(compiler, TMP_REG1, (sljit_uw)argw));
1795 if (arg & REG_MASK)
1796 FAIL_IF(push_inst16(compiler, ADD | SET_REGS44(TMP_REG1, (arg & REG_MASK))));
1797 return push_inst32(compiler, inst | 0x800000 | RN4(TMP_REG1) | DD4(reg));
1798 }
1799
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)1800 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_conv_sw_from_f64(struct sljit_compiler *compiler, sljit_s32 op,
1801 sljit_s32 dst, sljit_sw dstw,
1802 sljit_s32 src, sljit_sw srcw)
1803 {
1804 op ^= SLJIT_32;
1805
1806 if (src & SLJIT_MEM) {
1807 FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_32) | FPU_LOAD, TMP_FREG1, src, srcw));
1808 src = TMP_FREG1;
1809 }
1810
1811 FAIL_IF(push_inst32(compiler, VCVT_S32_F32 | (op & SLJIT_32) | DD4(TMP_FREG1) | DM4(src)));
1812
1813 if (FAST_IS_REG(dst))
1814 return push_inst32(compiler, VMOV | (1 << 20) | RT4(dst) | DN4(TMP_FREG1));
1815
1816 /* Store the integer value from a VFP register. */
1817 return emit_fop_mem(compiler, 0, TMP_FREG1, dst, dstw);
1818 }
1819
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)1820 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_conv_f64_from_sw(struct sljit_compiler *compiler, sljit_s32 op,
1821 sljit_s32 dst, sljit_sw dstw,
1822 sljit_s32 src, sljit_sw srcw)
1823 {
1824 sljit_s32 dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
1825
1826 op ^= SLJIT_32;
1827
1828 if (FAST_IS_REG(src))
1829 FAIL_IF(push_inst32(compiler, VMOV | RT4(src) | DN4(TMP_FREG1)));
1830 else if (src & SLJIT_MEM) {
1831 /* Load the integer value into a VFP register. */
1832 FAIL_IF(emit_fop_mem(compiler, FPU_LOAD, TMP_FREG1, src, srcw));
1833 }
1834 else {
1835 FAIL_IF(load_immediate(compiler, TMP_REG1, (sljit_uw)srcw));
1836 FAIL_IF(push_inst32(compiler, VMOV | RT4(TMP_REG1) | DN4(TMP_FREG1)));
1837 }
1838
1839 FAIL_IF(push_inst32(compiler, VCVT_F32_S32 | (op & SLJIT_32) | DD4(dst_r) | DM4(TMP_FREG1)));
1840
1841 if (dst & SLJIT_MEM)
1842 return emit_fop_mem(compiler, (op & SLJIT_32), TMP_FREG1, dst, dstw);
1843 return SLJIT_SUCCESS;
1844 }
1845
sljit_emit_fop1_cmp(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 src1,sljit_sw src1w,sljit_s32 src2,sljit_sw src2w)1846 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_cmp(struct sljit_compiler *compiler, sljit_s32 op,
1847 sljit_s32 src1, sljit_sw src1w,
1848 sljit_s32 src2, sljit_sw src2w)
1849 {
1850 op ^= SLJIT_32;
1851
1852 if (src1 & SLJIT_MEM) {
1853 emit_fop_mem(compiler, (op & SLJIT_32) | FPU_LOAD, TMP_FREG1, src1, src1w);
1854 src1 = TMP_FREG1;
1855 }
1856
1857 if (src2 & SLJIT_MEM) {
1858 emit_fop_mem(compiler, (op & SLJIT_32) | FPU_LOAD, TMP_FREG2, src2, src2w);
1859 src2 = TMP_FREG2;
1860 }
1861
1862 FAIL_IF(push_inst32(compiler, VCMP_F32 | (op & SLJIT_32) | DD4(src1) | DM4(src2)));
1863 return push_inst32(compiler, VMRS);
1864 }
1865
sljit_emit_fop1(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 src,sljit_sw srcw)1866 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop1(struct sljit_compiler *compiler, sljit_s32 op,
1867 sljit_s32 dst, sljit_sw dstw,
1868 sljit_s32 src, sljit_sw srcw)
1869 {
1870 sljit_s32 dst_r;
1871
1872 CHECK_ERROR();
1873
1874 SLJIT_COMPILE_ASSERT((SLJIT_32 == 0x100), float_transfer_bit_error);
1875 SELECT_FOP1_OPERATION_WITH_CHECKS(compiler, op, dst, dstw, src, srcw);
1876
1877 dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
1878
1879 if (GET_OPCODE(op) != SLJIT_CONV_F64_FROM_F32)
1880 op ^= SLJIT_32;
1881
1882 if (src & SLJIT_MEM) {
1883 emit_fop_mem(compiler, (op & SLJIT_32) | FPU_LOAD, dst_r, src, srcw);
1884 src = dst_r;
1885 }
1886
1887 switch (GET_OPCODE(op)) {
1888 case SLJIT_MOV_F64:
1889 if (src != dst_r) {
1890 if (dst_r != TMP_FREG1)
1891 FAIL_IF(push_inst32(compiler, VMOV_F32 | (op & SLJIT_32) | DD4(dst_r) | DM4(src)));
1892 else
1893 dst_r = src;
1894 }
1895 break;
1896 case SLJIT_NEG_F64:
1897 FAIL_IF(push_inst32(compiler, VNEG_F32 | (op & SLJIT_32) | DD4(dst_r) | DM4(src)));
1898 break;
1899 case SLJIT_ABS_F64:
1900 FAIL_IF(push_inst32(compiler, VABS_F32 | (op & SLJIT_32) | DD4(dst_r) | DM4(src)));
1901 break;
1902 case SLJIT_CONV_F64_FROM_F32:
1903 FAIL_IF(push_inst32(compiler, VCVT_F64_F32 | (op & SLJIT_32) | DD4(dst_r) | DM4(src)));
1904 op ^= SLJIT_32;
1905 break;
1906 }
1907
1908 if (dst & SLJIT_MEM)
1909 return emit_fop_mem(compiler, (op & SLJIT_32), dst_r, dst, dstw);
1910 return SLJIT_SUCCESS;
1911 }
1912
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)1913 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop2(struct sljit_compiler *compiler, sljit_s32 op,
1914 sljit_s32 dst, sljit_sw dstw,
1915 sljit_s32 src1, sljit_sw src1w,
1916 sljit_s32 src2, sljit_sw src2w)
1917 {
1918 sljit_s32 dst_r;
1919
1920 CHECK_ERROR();
1921 CHECK(check_sljit_emit_fop2(compiler, op, dst, dstw, src1, src1w, src2, src2w));
1922 ADJUST_LOCAL_OFFSET(dst, dstw);
1923 ADJUST_LOCAL_OFFSET(src1, src1w);
1924 ADJUST_LOCAL_OFFSET(src2, src2w);
1925
1926 op ^= SLJIT_32;
1927
1928 dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
1929 if (src1 & SLJIT_MEM) {
1930 emit_fop_mem(compiler, (op & SLJIT_32) | FPU_LOAD, TMP_FREG1, src1, src1w);
1931 src1 = TMP_FREG1;
1932 }
1933 if (src2 & SLJIT_MEM) {
1934 emit_fop_mem(compiler, (op & SLJIT_32) | FPU_LOAD, TMP_FREG2, src2, src2w);
1935 src2 = TMP_FREG2;
1936 }
1937
1938 switch (GET_OPCODE(op)) {
1939 case SLJIT_ADD_F64:
1940 FAIL_IF(push_inst32(compiler, VADD_F32 | (op & SLJIT_32) | DD4(dst_r) | DN4(src1) | DM4(src2)));
1941 break;
1942 case SLJIT_SUB_F64:
1943 FAIL_IF(push_inst32(compiler, VSUB_F32 | (op & SLJIT_32) | DD4(dst_r) | DN4(src1) | DM4(src2)));
1944 break;
1945 case SLJIT_MUL_F64:
1946 FAIL_IF(push_inst32(compiler, VMUL_F32 | (op & SLJIT_32) | DD4(dst_r) | DN4(src1) | DM4(src2)));
1947 break;
1948 case SLJIT_DIV_F64:
1949 FAIL_IF(push_inst32(compiler, VDIV_F32 | (op & SLJIT_32) | DD4(dst_r) | DN4(src1) | DM4(src2)));
1950 break;
1951 }
1952
1953 if (!(dst & SLJIT_MEM))
1954 return SLJIT_SUCCESS;
1955 return emit_fop_mem(compiler, (op & SLJIT_32), TMP_FREG1, dst, dstw);
1956 }
1957
1958 #undef FPU_LOAD
1959
1960 /* --------------------------------------------------------------------- */
1961 /* Other instructions */
1962 /* --------------------------------------------------------------------- */
1963
sljit_emit_fast_enter(struct sljit_compiler * compiler,sljit_s32 dst,sljit_sw dstw)1964 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fast_enter(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw)
1965 {
1966 CHECK_ERROR();
1967 CHECK(check_sljit_emit_fast_enter(compiler, dst, dstw));
1968 ADJUST_LOCAL_OFFSET(dst, dstw);
1969
1970 SLJIT_ASSERT(reg_map[TMP_REG2] == 14);
1971
1972 if (FAST_IS_REG(dst))
1973 return push_inst16(compiler, MOV | SET_REGS44(dst, TMP_REG2));
1974
1975 /* Memory. */
1976 return emit_op_mem(compiler, WORD_SIZE | STORE, TMP_REG2, dst, dstw, TMP_REG1);
1977 }
1978
1979 /* --------------------------------------------------------------------- */
1980 /* Conditional instructions */
1981 /* --------------------------------------------------------------------- */
1982
get_cc(struct sljit_compiler * compiler,sljit_s32 type)1983 static sljit_uw get_cc(struct sljit_compiler *compiler, sljit_s32 type)
1984 {
1985 switch (type) {
1986 case SLJIT_EQUAL:
1987 case SLJIT_EQUAL_F64:
1988 return 0x0;
1989
1990 case SLJIT_NOT_EQUAL:
1991 case SLJIT_NOT_EQUAL_F64:
1992 return 0x1;
1993
1994 case SLJIT_CARRY:
1995 if (compiler->status_flags_state & SLJIT_CURRENT_FLAGS_ADD)
1996 return 0x2;
1997 /* fallthrough */
1998
1999 case SLJIT_LESS:
2000 case SLJIT_LESS_F64:
2001 return 0x3;
2002
2003 case SLJIT_NOT_CARRY:
2004 if (compiler->status_flags_state & SLJIT_CURRENT_FLAGS_ADD)
2005 return 0x3;
2006 /* fallthrough */
2007
2008 case SLJIT_GREATER_EQUAL:
2009 case SLJIT_GREATER_EQUAL_F64:
2010 return 0x2;
2011
2012 case SLJIT_GREATER:
2013 case SLJIT_GREATER_F64:
2014 return 0x8;
2015
2016 case SLJIT_LESS_EQUAL:
2017 case SLJIT_LESS_EQUAL_F64:
2018 return 0x9;
2019
2020 case SLJIT_SIG_LESS:
2021 return 0xb;
2022
2023 case SLJIT_SIG_GREATER_EQUAL:
2024 return 0xa;
2025
2026 case SLJIT_SIG_GREATER:
2027 return 0xc;
2028
2029 case SLJIT_SIG_LESS_EQUAL:
2030 return 0xd;
2031
2032 case SLJIT_OVERFLOW:
2033 if (!(compiler->status_flags_state & (SLJIT_CURRENT_FLAGS_ADD | SLJIT_CURRENT_FLAGS_SUB)))
2034 return 0x1;
2035 /* fallthrough */
2036
2037 case SLJIT_UNORDERED_F64:
2038 return 0x6;
2039
2040 case SLJIT_NOT_OVERFLOW:
2041 if (!(compiler->status_flags_state & (SLJIT_CURRENT_FLAGS_ADD | SLJIT_CURRENT_FLAGS_SUB)))
2042 return 0x0;
2043 /* fallthrough */
2044
2045 case SLJIT_ORDERED_F64:
2046 return 0x7;
2047
2048 default: /* SLJIT_JUMP */
2049 SLJIT_UNREACHABLE();
2050 return 0xe;
2051 }
2052 }
2053
sljit_emit_label(struct sljit_compiler * compiler)2054 SLJIT_API_FUNC_ATTRIBUTE struct sljit_label* sljit_emit_label(struct sljit_compiler *compiler)
2055 {
2056 struct sljit_label *label;
2057
2058 CHECK_ERROR_PTR();
2059 CHECK_PTR(check_sljit_emit_label(compiler));
2060
2061 if (compiler->last_label && compiler->last_label->size == compiler->size)
2062 return compiler->last_label;
2063
2064 label = (struct sljit_label*)ensure_abuf(compiler, sizeof(struct sljit_label));
2065 PTR_FAIL_IF(!label);
2066 set_label(label, compiler);
2067 return label;
2068 }
2069
sljit_emit_jump(struct sljit_compiler * compiler,sljit_s32 type)2070 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_jump(struct sljit_compiler *compiler, sljit_s32 type)
2071 {
2072 struct sljit_jump *jump;
2073 sljit_ins cc;
2074
2075 CHECK_ERROR_PTR();
2076 CHECK_PTR(check_sljit_emit_jump(compiler, type));
2077
2078 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
2079 PTR_FAIL_IF(!jump);
2080 set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
2081 type &= 0xff;
2082
2083 PTR_FAIL_IF(emit_imm32_const(compiler, TMP_REG1, 0));
2084 if (type < SLJIT_JUMP) {
2085 jump->flags |= IS_COND;
2086 cc = get_cc(compiler, type);
2087 jump->flags |= cc << 8;
2088 PTR_FAIL_IF(push_inst16(compiler, IT | (cc << 4) | 0x8));
2089 }
2090
2091 jump->addr = compiler->size;
2092 if (type <= SLJIT_JUMP)
2093 PTR_FAIL_IF(push_inst16(compiler, BX | RN3(TMP_REG1)));
2094 else {
2095 jump->flags |= IS_BL;
2096 PTR_FAIL_IF(push_inst16(compiler, BLX | RN3(TMP_REG1)));
2097 }
2098
2099 return jump;
2100 }
2101
2102 #ifdef __SOFTFP__
2103
softfloat_call_with_args(struct sljit_compiler * compiler,sljit_s32 arg_types,sljit_s32 * src,sljit_u32 * extra_space)2104 static sljit_s32 softfloat_call_with_args(struct sljit_compiler *compiler, sljit_s32 arg_types, sljit_s32 *src, sljit_u32 *extra_space)
2105 {
2106 sljit_u32 is_tail_call = *extra_space & SLJIT_CALL_RETURN;
2107 sljit_u32 offset = 0;
2108 sljit_u32 word_arg_offset = 0;
2109 sljit_u32 float_arg_count = 0;
2110 sljit_s32 types = 0;
2111 sljit_u32 src_offset = 4 * sizeof(sljit_sw);
2112 sljit_u8 offsets[4];
2113 sljit_u8 *offset_ptr = offsets;
2114
2115 if (src && FAST_IS_REG(*src))
2116 src_offset = (sljit_u32)reg_map[*src] * sizeof(sljit_sw);
2117
2118 arg_types >>= SLJIT_ARG_SHIFT;
2119
2120 while (arg_types) {
2121 types = (types << SLJIT_ARG_SHIFT) | (arg_types & SLJIT_ARG_MASK);
2122
2123 switch (arg_types & SLJIT_ARG_MASK) {
2124 case SLJIT_ARG_TYPE_F64:
2125 if (offset & 0x7)
2126 offset += sizeof(sljit_sw);
2127 *offset_ptr++ = (sljit_u8)offset;
2128 offset += sizeof(sljit_f64);
2129 float_arg_count++;
2130 break;
2131 case SLJIT_ARG_TYPE_F32:
2132 *offset_ptr++ = (sljit_u8)offset;
2133 offset += sizeof(sljit_f32);
2134 float_arg_count++;
2135 break;
2136 default:
2137 *offset_ptr++ = (sljit_u8)offset;
2138 offset += sizeof(sljit_sw);
2139 word_arg_offset += sizeof(sljit_sw);
2140 break;
2141 }
2142
2143 arg_types >>= SLJIT_ARG_SHIFT;
2144 }
2145
2146 if (offset > 4 * sizeof(sljit_sw) && (!is_tail_call || offset > compiler->args_size)) {
2147 /* Keep lr register on the stack. */
2148 if (is_tail_call)
2149 offset += sizeof(sljit_sw);
2150
2151 offset = ((offset - 4 * sizeof(sljit_sw)) + 0x7) & ~(sljit_uw)0x7;
2152
2153 *extra_space = offset;
2154
2155 if (is_tail_call)
2156 FAIL_IF(emit_stack_frame_release(compiler, (sljit_s32)offset));
2157 else
2158 FAIL_IF(push_inst16(compiler, SUB_SP_I | (offset >> 2)));
2159 } else {
2160 if (is_tail_call)
2161 FAIL_IF(emit_stack_frame_release(compiler, -1));
2162 *extra_space = 0;
2163 }
2164
2165 SLJIT_ASSERT(reg_map[TMP_REG1] == 12);
2166
2167 /* Process arguments in reversed direction. */
2168 while (types) {
2169 switch (types & SLJIT_ARG_MASK) {
2170 case SLJIT_ARG_TYPE_F64:
2171 float_arg_count--;
2172 offset = *(--offset_ptr);
2173
2174 SLJIT_ASSERT((offset & 0x7) == 0);
2175
2176 if (offset < 4 * sizeof(sljit_sw)) {
2177 if (src_offset == offset || src_offset == offset + sizeof(sljit_sw)) {
2178 FAIL_IF(push_inst16(compiler, MOV | (src_offset << 1) | 4 | (1 << 7)));
2179 *src = TMP_REG1;
2180 }
2181 FAIL_IF(push_inst32(compiler, VMOV2 | 0x100000 | (offset << 10) | ((offset + sizeof(sljit_sw)) << 14) | float_arg_count));
2182 } else
2183 FAIL_IF(push_inst32(compiler, VSTR_F32 | 0x800100 | RN4(SLJIT_SP)
2184 | (float_arg_count << 12) | ((offset - 4 * sizeof(sljit_sw)) >> 2)));
2185 break;
2186 case SLJIT_ARG_TYPE_F32:
2187 float_arg_count--;
2188 offset = *(--offset_ptr);
2189
2190 if (offset < 4 * sizeof(sljit_sw)) {
2191 if (src_offset == offset) {
2192 FAIL_IF(push_inst16(compiler, MOV | (src_offset << 1) | 4 | (1 << 7)));
2193 *src = TMP_REG1;
2194 }
2195 FAIL_IF(push_inst32(compiler, VMOV | 0x100000 | (float_arg_count << 16) | (offset << 10)));
2196 } else
2197 FAIL_IF(push_inst32(compiler, VSTR_F32 | 0x800000 | RN4(SLJIT_SP)
2198 | (float_arg_count << 12) | ((offset - 4 * sizeof(sljit_sw)) >> 2)));
2199 break;
2200 default:
2201 word_arg_offset -= sizeof(sljit_sw);
2202 offset = *(--offset_ptr);
2203
2204 SLJIT_ASSERT(offset >= word_arg_offset);
2205
2206 if (offset != word_arg_offset) {
2207 if (offset < 4 * sizeof(sljit_sw)) {
2208 if (src_offset == offset) {
2209 FAIL_IF(push_inst16(compiler, MOV | (src_offset << 1) | 4 | (1 << 7)));
2210 *src = TMP_REG1;
2211 }
2212 else if (src_offset == word_arg_offset) {
2213 *src = (sljit_s32)(1 + (offset >> 2));
2214 src_offset = offset;
2215 }
2216 FAIL_IF(push_inst16(compiler, MOV | (offset >> 2) | (word_arg_offset << 1)));
2217 } else
2218 FAIL_IF(push_inst16(compiler, STR_SP | (word_arg_offset << 6) | ((offset - 4 * sizeof(sljit_sw)) >> 2)));
2219 }
2220 break;
2221 }
2222
2223 types >>= SLJIT_ARG_SHIFT;
2224 }
2225
2226 return SLJIT_SUCCESS;
2227 }
2228
softfloat_post_call_with_args(struct sljit_compiler * compiler,sljit_s32 arg_types)2229 static sljit_s32 softfloat_post_call_with_args(struct sljit_compiler *compiler, sljit_s32 arg_types)
2230 {
2231 if ((arg_types & SLJIT_ARG_MASK) == SLJIT_ARG_TYPE_F64)
2232 FAIL_IF(push_inst32(compiler, VMOV2 | (1 << 16) | (0 << 12) | 0));
2233 if ((arg_types & SLJIT_ARG_MASK) == SLJIT_ARG_TYPE_F32)
2234 FAIL_IF(push_inst32(compiler, VMOV | (0 << 16) | (0 << 12)));
2235
2236 return SLJIT_SUCCESS;
2237 }
2238
2239 #else
2240
hardfloat_call_with_args(struct sljit_compiler * compiler,sljit_s32 arg_types)2241 static sljit_s32 hardfloat_call_with_args(struct sljit_compiler *compiler, sljit_s32 arg_types)
2242 {
2243 sljit_u32 offset = SLJIT_FR0;
2244 sljit_u32 new_offset = SLJIT_FR0;
2245 sljit_u32 f32_offset = 0;
2246
2247 /* Remove return value. */
2248 arg_types >>= SLJIT_ARG_SHIFT;
2249
2250 while (arg_types) {
2251 switch (arg_types & SLJIT_ARG_MASK) {
2252 case SLJIT_ARG_TYPE_F64:
2253 if (offset != new_offset)
2254 FAIL_IF(push_inst32(compiler, VMOV_F32 | SLJIT_32 | DD4(new_offset) | DM4(offset)));
2255
2256 new_offset++;
2257 offset++;
2258 break;
2259 case SLJIT_ARG_TYPE_F32:
2260 if (f32_offset != 0) {
2261 FAIL_IF(push_inst32(compiler, VMOV_F32 | 0x400000 | DD4(f32_offset) | DM4(offset)));
2262 f32_offset = 0;
2263 } else {
2264 if (offset != new_offset)
2265 FAIL_IF(push_inst32(compiler, VMOV_F32 | 0x400000 | DD4(new_offset) | DM4(offset)));
2266 f32_offset = new_offset;
2267 new_offset++;
2268 }
2269 offset++;
2270 break;
2271 }
2272 arg_types >>= SLJIT_ARG_SHIFT;
2273 }
2274
2275 return SLJIT_SUCCESS;
2276 }
2277
2278 #endif
2279
sljit_emit_call(struct sljit_compiler * compiler,sljit_s32 type,sljit_s32 arg_types)2280 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_call(struct sljit_compiler *compiler, sljit_s32 type,
2281 sljit_s32 arg_types)
2282 {
2283 #ifdef __SOFTFP__
2284 struct sljit_jump *jump;
2285 sljit_u32 extra_space = (sljit_u32)type;
2286 #endif
2287
2288 CHECK_ERROR_PTR();
2289 CHECK_PTR(check_sljit_emit_call(compiler, type, arg_types));
2290
2291 #ifdef __SOFTFP__
2292 PTR_FAIL_IF(softfloat_call_with_args(compiler, arg_types, NULL, &extra_space));
2293 SLJIT_ASSERT((extra_space & 0x7) == 0);
2294
2295 if ((type & SLJIT_CALL_RETURN) && extra_space == 0)
2296 type = SLJIT_JUMP | (type & SLJIT_REWRITABLE_JUMP);
2297
2298 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2299 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2300 compiler->skip_checks = 1;
2301 #endif
2302
2303 jump = sljit_emit_jump(compiler, type);
2304 PTR_FAIL_IF(jump == NULL);
2305
2306 if (extra_space > 0) {
2307 if (type & SLJIT_CALL_RETURN)
2308 PTR_FAIL_IF(push_inst32(compiler, LDR | RT4(TMP_REG2)
2309 | RN4(SLJIT_SP) | (extra_space - sizeof(sljit_sw))));
2310
2311 PTR_FAIL_IF(push_inst16(compiler, ADD_SP_I | (extra_space >> 2)));
2312
2313 if (type & SLJIT_CALL_RETURN) {
2314 PTR_FAIL_IF(push_inst16(compiler, BX | RN3(TMP_REG2)));
2315 return jump;
2316 }
2317 }
2318
2319 SLJIT_ASSERT(!(type & SLJIT_CALL_RETURN));
2320 PTR_FAIL_IF(softfloat_post_call_with_args(compiler, arg_types));
2321 return jump;
2322 #else
2323 if (type & SLJIT_CALL_RETURN) {
2324 /* ldmia sp!, {..., lr} */
2325 PTR_FAIL_IF(emit_stack_frame_release(compiler, -1));
2326 type = SLJIT_JUMP | (type & SLJIT_REWRITABLE_JUMP);
2327 }
2328
2329 PTR_FAIL_IF(hardfloat_call_with_args(compiler, arg_types));
2330
2331 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2332 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2333 compiler->skip_checks = 1;
2334 #endif
2335
2336 return sljit_emit_jump(compiler, type);
2337 #endif
2338 }
2339
sljit_emit_ijump(struct sljit_compiler * compiler,sljit_s32 type,sljit_s32 src,sljit_sw srcw)2340 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_ijump(struct sljit_compiler *compiler, sljit_s32 type, sljit_s32 src, sljit_sw srcw)
2341 {
2342 struct sljit_jump *jump;
2343
2344 CHECK_ERROR();
2345 CHECK(check_sljit_emit_ijump(compiler, type, src, srcw));
2346 ADJUST_LOCAL_OFFSET(src, srcw);
2347
2348 SLJIT_ASSERT(reg_map[TMP_REG1] != 14);
2349
2350 if (!(src & SLJIT_IMM)) {
2351 if (FAST_IS_REG(src)) {
2352 SLJIT_ASSERT(reg_map[src] != 14);
2353 return push_inst16(compiler, (type <= SLJIT_JUMP ? BX : BLX) | RN3(src));
2354 }
2355
2356 FAIL_IF(emit_op_mem(compiler, WORD_SIZE, type <= SLJIT_JUMP ? TMP_PC : TMP_REG1, src, srcw, TMP_REG1));
2357 if (type >= SLJIT_FAST_CALL)
2358 return push_inst16(compiler, BLX | RN3(TMP_REG1));
2359 }
2360
2361 /* These jumps are converted to jump/call instructions when possible. */
2362 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
2363 FAIL_IF(!jump);
2364 set_jump(jump, compiler, JUMP_ADDR | ((type >= SLJIT_FAST_CALL) ? IS_BL : 0));
2365 jump->u.target = (sljit_uw)srcw;
2366
2367 FAIL_IF(emit_imm32_const(compiler, TMP_REG1, 0));
2368 jump->addr = compiler->size;
2369 return push_inst16(compiler, (type <= SLJIT_JUMP ? BX : BLX) | RN3(TMP_REG1));
2370 }
2371
sljit_emit_icall(struct sljit_compiler * compiler,sljit_s32 type,sljit_s32 arg_types,sljit_s32 src,sljit_sw srcw)2372 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_icall(struct sljit_compiler *compiler, sljit_s32 type,
2373 sljit_s32 arg_types,
2374 sljit_s32 src, sljit_sw srcw)
2375 {
2376 #ifdef __SOFTFP__
2377 sljit_u32 extra_space = (sljit_u32)type;
2378 #endif
2379
2380 CHECK_ERROR();
2381 CHECK(check_sljit_emit_icall(compiler, type, arg_types, src, srcw));
2382
2383 if (src & SLJIT_MEM) {
2384 FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG1, src, srcw, TMP_REG1));
2385 src = TMP_REG1;
2386 }
2387
2388 if ((type & SLJIT_CALL_RETURN) && (src >= SLJIT_FIRST_SAVED_REG && src <= SLJIT_S0)) {
2389 FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(TMP_REG1, src)));
2390 src = TMP_REG1;
2391 }
2392
2393 #ifdef __SOFTFP__
2394 FAIL_IF(softfloat_call_with_args(compiler, arg_types, &src, &extra_space));
2395 SLJIT_ASSERT((extra_space & 0x7) == 0);
2396
2397 if ((type & SLJIT_CALL_RETURN) && extra_space == 0)
2398 type = SLJIT_JUMP;
2399
2400 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2401 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2402 compiler->skip_checks = 1;
2403 #endif
2404
2405 FAIL_IF(sljit_emit_ijump(compiler, type, src, srcw));
2406
2407 if (extra_space > 0) {
2408 if (type & SLJIT_CALL_RETURN)
2409 FAIL_IF(push_inst32(compiler, LDR | RT4(TMP_REG2)
2410 | RN4(SLJIT_SP) | (extra_space - sizeof(sljit_sw))));
2411
2412 FAIL_IF(push_inst16(compiler, ADD_SP_I | (extra_space >> 2)));
2413
2414 if (type & SLJIT_CALL_RETURN)
2415 return push_inst16(compiler, BX | RN3(TMP_REG2));
2416 }
2417
2418 SLJIT_ASSERT(!(type & SLJIT_CALL_RETURN));
2419 return softfloat_post_call_with_args(compiler, arg_types);
2420 #else /* !__SOFTFP__ */
2421 if (type & SLJIT_CALL_RETURN) {
2422 /* ldmia sp!, {..., lr} */
2423 FAIL_IF(emit_stack_frame_release(compiler, -1));
2424 type = SLJIT_JUMP;
2425 }
2426
2427 FAIL_IF(hardfloat_call_with_args(compiler, arg_types));
2428
2429 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2430 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2431 compiler->skip_checks = 1;
2432 #endif
2433
2434 return sljit_emit_ijump(compiler, type, src, srcw);
2435 #endif /* __SOFTFP__ */
2436 }
2437
sljit_emit_op_flags(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 type)2438 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_flags(struct sljit_compiler *compiler, sljit_s32 op,
2439 sljit_s32 dst, sljit_sw dstw,
2440 sljit_s32 type)
2441 {
2442 sljit_s32 dst_r, flags = GET_ALL_FLAGS(op);
2443 sljit_ins cc;
2444
2445 CHECK_ERROR();
2446 CHECK(check_sljit_emit_op_flags(compiler, op, dst, dstw, type));
2447 ADJUST_LOCAL_OFFSET(dst, dstw);
2448
2449 op = GET_OPCODE(op);
2450 cc = get_cc(compiler, type & 0xff);
2451 dst_r = FAST_IS_REG(dst) ? dst : TMP_REG1;
2452
2453 if (op < SLJIT_ADD) {
2454 FAIL_IF(push_inst16(compiler, IT | (cc << 4) | (((cc & 0x1) ^ 0x1) << 3) | 0x4));
2455 if (reg_map[dst_r] > 7) {
2456 FAIL_IF(push_inst32(compiler, MOV_WI | RD4(dst_r) | 1));
2457 FAIL_IF(push_inst32(compiler, MOV_WI | RD4(dst_r) | 0));
2458 } else {
2459 /* The movsi (immediate) instruction does not set flags in IT block. */
2460 FAIL_IF(push_inst16(compiler, MOVSI | RDN3(dst_r) | 1));
2461 FAIL_IF(push_inst16(compiler, MOVSI | RDN3(dst_r) | 0));
2462 }
2463 if (!(dst & SLJIT_MEM))
2464 return SLJIT_SUCCESS;
2465 return emit_op_mem(compiler, WORD_SIZE | STORE, TMP_REG1, dst, dstw, TMP_REG2);
2466 }
2467
2468 if (dst & SLJIT_MEM)
2469 FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG1, dst, dstw, TMP_REG2));
2470
2471 if (op == SLJIT_AND) {
2472 FAIL_IF(push_inst16(compiler, IT | (cc << 4) | (((cc & 0x1) ^ 0x1) << 3) | 0x4));
2473 FAIL_IF(push_inst32(compiler, ANDI | RN4(dst_r) | RD4(dst_r) | 1));
2474 FAIL_IF(push_inst32(compiler, ANDI | RN4(dst_r) | RD4(dst_r) | 0));
2475 }
2476 else {
2477 FAIL_IF(push_inst16(compiler, IT | (cc << 4) | 0x8));
2478 FAIL_IF(push_inst32(compiler, ((op == SLJIT_OR) ? ORRI : EORI) | RN4(dst_r) | RD4(dst_r) | 1));
2479 }
2480
2481 if (dst & SLJIT_MEM)
2482 FAIL_IF(emit_op_mem(compiler, WORD_SIZE | STORE, TMP_REG1, dst, dstw, TMP_REG2));
2483
2484 if (!(flags & SLJIT_SET_Z))
2485 return SLJIT_SUCCESS;
2486
2487 /* The condition must always be set, even if the ORR/EORI is not executed above. */
2488 return push_inst32(compiler, MOV_W | SET_FLAGS | RD4(TMP_REG1) | RM4(dst_r));
2489 }
2490
sljit_emit_cmov(struct sljit_compiler * compiler,sljit_s32 type,sljit_s32 dst_reg,sljit_s32 src,sljit_sw srcw)2491 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_cmov(struct sljit_compiler *compiler, sljit_s32 type,
2492 sljit_s32 dst_reg,
2493 sljit_s32 src, sljit_sw srcw)
2494 {
2495 sljit_uw cc, tmp;
2496
2497 CHECK_ERROR();
2498 CHECK(check_sljit_emit_cmov(compiler, type, dst_reg, src, srcw));
2499
2500 dst_reg &= ~SLJIT_32;
2501
2502 cc = get_cc(compiler, type & 0xff);
2503
2504 if (!(src & SLJIT_IMM)) {
2505 FAIL_IF(push_inst16(compiler, IT | (cc << 4) | 0x8));
2506 return push_inst16(compiler, MOV | SET_REGS44(dst_reg, src));
2507 }
2508
2509 tmp = (sljit_uw) srcw;
2510
2511 if (tmp < 0x10000) {
2512 /* set low 16 bits, set hi 16 bits to 0. */
2513 FAIL_IF(push_inst16(compiler, IT | (cc << 4) | 0x8));
2514 return push_inst32(compiler, MOVW | RD4(dst_reg)
2515 | COPY_BITS(tmp, 12, 16, 4) | COPY_BITS(tmp, 11, 26, 1) | COPY_BITS(tmp, 8, 12, 3) | (tmp & 0xff));
2516 }
2517
2518 tmp = get_imm((sljit_uw)srcw);
2519 if (tmp != INVALID_IMM) {
2520 FAIL_IF(push_inst16(compiler, IT | (cc << 4) | 0x8));
2521 return push_inst32(compiler, MOV_WI | RD4(dst_reg) | tmp);
2522 }
2523
2524 tmp = get_imm(~(sljit_uw)srcw);
2525 if (tmp != INVALID_IMM) {
2526 FAIL_IF(push_inst16(compiler, IT | (cc << 4) | 0x8));
2527 return push_inst32(compiler, MVN_WI | RD4(dst_reg) | tmp);
2528 }
2529
2530 FAIL_IF(push_inst16(compiler, IT | (cc << 4) | ((cc & 0x1) << 3) | 0x4));
2531
2532 tmp = (sljit_uw) srcw;
2533 FAIL_IF(push_inst32(compiler, MOVW | RD4(dst_reg)
2534 | COPY_BITS(tmp, 12, 16, 4) | COPY_BITS(tmp, 11, 26, 1) | COPY_BITS(tmp, 8, 12, 3) | (tmp & 0xff)));
2535 return push_inst32(compiler, MOVT | RD4(dst_reg)
2536 | COPY_BITS(tmp, 12 + 16, 16, 4) | COPY_BITS(tmp, 11 + 16, 26, 1) | COPY_BITS(tmp, 8 + 16, 12, 3) | ((tmp & 0xff0000) >> 16));
2537 }
2538
sljit_emit_mem(struct sljit_compiler * compiler,sljit_s32 type,sljit_s32 reg,sljit_s32 mem,sljit_sw memw)2539 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_mem(struct sljit_compiler *compiler, sljit_s32 type,
2540 sljit_s32 reg,
2541 sljit_s32 mem, sljit_sw memw)
2542 {
2543 sljit_s32 flags;
2544 sljit_ins inst;
2545
2546 CHECK_ERROR();
2547 CHECK(check_sljit_emit_mem(compiler, type, reg, mem, memw));
2548
2549 if ((mem & OFFS_REG_MASK) || (memw > 255 || memw < -255))
2550 return SLJIT_ERR_UNSUPPORTED;
2551
2552 if (type & SLJIT_MEM_SUPP)
2553 return SLJIT_SUCCESS;
2554
2555 switch (type & 0xff) {
2556 case SLJIT_MOV:
2557 case SLJIT_MOV_U32:
2558 case SLJIT_MOV_S32:
2559 case SLJIT_MOV32:
2560 case SLJIT_MOV_P:
2561 flags = WORD_SIZE;
2562 break;
2563 case SLJIT_MOV_U8:
2564 flags = BYTE_SIZE;
2565 break;
2566 case SLJIT_MOV_S8:
2567 flags = BYTE_SIZE | SIGNED;
2568 break;
2569 case SLJIT_MOV_U16:
2570 flags = HALF_SIZE;
2571 break;
2572 case SLJIT_MOV_S16:
2573 flags = HALF_SIZE | SIGNED;
2574 break;
2575 default:
2576 SLJIT_UNREACHABLE();
2577 flags = WORD_SIZE;
2578 break;
2579 }
2580
2581 if (type & SLJIT_MEM_STORE)
2582 flags |= STORE;
2583
2584 inst = sljit_mem32[flags] | 0x900;
2585
2586 if (type & SLJIT_MEM_PRE)
2587 inst |= 0x400;
2588
2589 if (memw >= 0)
2590 inst |= 0x200;
2591 else
2592 memw = -memw;
2593
2594 return push_inst32(compiler, inst | RT4(reg) | RN4(mem & REG_MASK) | (sljit_ins)memw);
2595 }
2596
sljit_emit_const(struct sljit_compiler * compiler,sljit_s32 dst,sljit_sw dstw,sljit_sw init_value)2597 SLJIT_API_FUNC_ATTRIBUTE struct sljit_const* sljit_emit_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw, sljit_sw init_value)
2598 {
2599 struct sljit_const *const_;
2600 sljit_s32 dst_r;
2601
2602 CHECK_ERROR_PTR();
2603 CHECK_PTR(check_sljit_emit_const(compiler, dst, dstw, init_value));
2604 ADJUST_LOCAL_OFFSET(dst, dstw);
2605
2606 const_ = (struct sljit_const*)ensure_abuf(compiler, sizeof(struct sljit_const));
2607 PTR_FAIL_IF(!const_);
2608 set_const(const_, compiler);
2609
2610 dst_r = FAST_IS_REG(dst) ? dst : TMP_REG1;
2611 PTR_FAIL_IF(emit_imm32_const(compiler, dst_r, (sljit_uw)init_value));
2612
2613 if (dst & SLJIT_MEM)
2614 PTR_FAIL_IF(emit_op_mem(compiler, WORD_SIZE | STORE, dst_r, dst, dstw, TMP_REG2));
2615 return const_;
2616 }
2617
sljit_emit_put_label(struct sljit_compiler * compiler,sljit_s32 dst,sljit_sw dstw)2618 SLJIT_API_FUNC_ATTRIBUTE struct sljit_put_label* sljit_emit_put_label(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw)
2619 {
2620 struct sljit_put_label *put_label;
2621 sljit_s32 dst_r;
2622
2623 CHECK_ERROR_PTR();
2624 CHECK_PTR(check_sljit_emit_put_label(compiler, dst, dstw));
2625 ADJUST_LOCAL_OFFSET(dst, dstw);
2626
2627 put_label = (struct sljit_put_label*)ensure_abuf(compiler, sizeof(struct sljit_put_label));
2628 PTR_FAIL_IF(!put_label);
2629 set_put_label(put_label, compiler, 0);
2630
2631 dst_r = FAST_IS_REG(dst) ? dst : TMP_REG1;
2632 PTR_FAIL_IF(emit_imm32_const(compiler, dst_r, 0));
2633
2634 if (dst & SLJIT_MEM)
2635 PTR_FAIL_IF(emit_op_mem(compiler, WORD_SIZE | STORE, dst_r, dst, dstw, TMP_REG2));
2636 return put_label;
2637 }
2638
sljit_set_jump_addr(sljit_uw addr,sljit_uw new_target,sljit_sw executable_offset)2639 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_target, sljit_sw executable_offset)
2640 {
2641 sljit_u16 *inst = (sljit_u16*)addr;
2642 SLJIT_UNUSED_ARG(executable_offset);
2643
2644 SLJIT_UPDATE_WX_FLAGS(inst, inst + 4, 0);
2645 modify_imm32_const(inst, new_target);
2646 SLJIT_UPDATE_WX_FLAGS(inst, inst + 4, 1);
2647 inst = (sljit_u16 *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
2648 SLJIT_CACHE_FLUSH(inst, inst + 4);
2649 }
2650
sljit_set_const(sljit_uw addr,sljit_sw new_constant,sljit_sw executable_offset)2651 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_const(sljit_uw addr, sljit_sw new_constant, sljit_sw executable_offset)
2652 {
2653 sljit_set_jump_addr(addr, (sljit_uw)new_constant, executable_offset);
2654 }
2655