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