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