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), compiler->exec_allocator_data);
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 	SLJIT_UPDATE_WX_FLAGS(code, code_ptr, 1);
467 
468 	/* Set thumb mode flag. */
469 	return (void*)((sljit_uw)code | 0x1);
470 }
471 
sljit_has_cpu_feature(sljit_s32 feature_type)472 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_has_cpu_feature(sljit_s32 feature_type)
473 {
474 	switch (feature_type) {
475 	case SLJIT_HAS_FPU:
476 #ifdef SLJIT_IS_FPU_AVAILABLE
477 		return SLJIT_IS_FPU_AVAILABLE;
478 #else
479 		/* Available by default. */
480 		return 1;
481 #endif
482 
483 	case SLJIT_HAS_CLZ:
484 	case SLJIT_HAS_CMOV:
485 	case SLJIT_HAS_PREFETCH:
486 		return 1;
487 
488 	default:
489 		return 0;
490 	}
491 }
492 
493 /* --------------------------------------------------------------------- */
494 /*  Core code generator functions.                                       */
495 /* --------------------------------------------------------------------- */
496 
497 #define INVALID_IMM	0x80000000
get_imm(sljit_uw imm)498 static sljit_uw get_imm(sljit_uw imm)
499 {
500 	/* Thumb immediate form. */
501 	sljit_s32 counter;
502 
503 	if (imm <= 0xff)
504 		return imm;
505 
506 	if ((imm & 0xffff) == (imm >> 16)) {
507 		/* Some special cases. */
508 		if (!(imm & 0xff00))
509 			return (1 << 12) | (imm & 0xff);
510 		if (!(imm & 0xff))
511 			return (2 << 12) | ((imm >> 8) & 0xff);
512 		if ((imm & 0xff00) == ((imm & 0xff) << 8))
513 			return (3 << 12) | (imm & 0xff);
514 	}
515 
516 	/* Assembly optimization: count leading zeroes? */
517 	counter = 8;
518 	if (!(imm & 0xffff0000)) {
519 		counter += 16;
520 		imm <<= 16;
521 	}
522 	if (!(imm & 0xff000000)) {
523 		counter += 8;
524 		imm <<= 8;
525 	}
526 	if (!(imm & 0xf0000000)) {
527 		counter += 4;
528 		imm <<= 4;
529 	}
530 	if (!(imm & 0xc0000000)) {
531 		counter += 2;
532 		imm <<= 2;
533 	}
534 	if (!(imm & 0x80000000)) {
535 		counter += 1;
536 		imm <<= 1;
537 	}
538 	/* Since imm >= 128, this must be true. */
539 	SLJIT_ASSERT(counter <= 31);
540 
541 	if (imm & 0x00ffffff)
542 		return INVALID_IMM; /* Cannot be encoded. */
543 
544 	return ((imm >> 24) & 0x7f) | COPY_BITS(counter, 4, 26, 1) | COPY_BITS(counter, 1, 12, 3) | COPY_BITS(counter, 0, 7, 1);
545 }
546 
load_immediate(struct sljit_compiler * compiler,sljit_s32 dst,sljit_uw imm)547 static sljit_s32 load_immediate(struct sljit_compiler *compiler, sljit_s32 dst, sljit_uw imm)
548 {
549 	sljit_uw tmp;
550 
551 	/* MOVS cannot be used since it destroy flags. */
552 
553 	if (imm >= 0x10000) {
554 		tmp = get_imm(imm);
555 		if (tmp != INVALID_IMM)
556 			return push_inst32(compiler, MOV_WI | RD4(dst) | tmp);
557 		tmp = get_imm(~imm);
558 		if (tmp != INVALID_IMM)
559 			return push_inst32(compiler, MVN_WI | RD4(dst) | tmp);
560 	}
561 
562 	/* set low 16 bits, set hi 16 bits to 0. */
563 	FAIL_IF(push_inst32(compiler, MOVW | RD4(dst)
564 		| COPY_BITS(imm, 12, 16, 4) | COPY_BITS(imm, 11, 26, 1) | COPY_BITS(imm, 8, 12, 3) | (imm & 0xff)));
565 
566 	/* set hi 16 bit if needed. */
567 	if (imm >= 0x10000)
568 		return push_inst32(compiler, MOVT | RD4(dst)
569 			| COPY_BITS(imm, 12 + 16, 16, 4) | COPY_BITS(imm, 11 + 16, 26, 1) | COPY_BITS(imm, 8 + 16, 12, 3) | ((imm & 0xff0000) >> 16));
570 	return SLJIT_SUCCESS;
571 }
572 
573 #define ARG1_IMM	0x0010000
574 #define ARG2_IMM	0x0020000
575 /* SET_FLAGS must be 0x100000 as it is also the value of S bit (can be used for optimization). */
576 #define SET_FLAGS	0x0100000
577 #define UNUSED_RETURN	0x0200000
578 
emit_op_imm(struct sljit_compiler * compiler,sljit_s32 flags,sljit_s32 dst,sljit_uw arg1,sljit_uw arg2)579 static sljit_s32 emit_op_imm(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 dst, sljit_uw arg1, sljit_uw arg2)
580 {
581 	/* dst must be register, TMP_REG1
582 	   arg1 must be register, imm
583 	   arg2 must be register, imm */
584 	sljit_s32 reg;
585 	sljit_uw imm, nimm;
586 
587 	if (SLJIT_UNLIKELY((flags & (ARG1_IMM | ARG2_IMM)) == (ARG1_IMM | ARG2_IMM))) {
588 		/* Both are immediates, no temporaries are used. */
589 		flags &= ~ARG1_IMM;
590 		FAIL_IF(load_immediate(compiler, TMP_REG1, arg1));
591 		arg1 = TMP_REG1;
592 	}
593 
594 	if (flags & (ARG1_IMM | ARG2_IMM)) {
595 		reg = (flags & ARG2_IMM) ? arg1 : arg2;
596 		imm = (flags & ARG2_IMM) ? arg2 : arg1;
597 
598 		switch (flags & 0xffff) {
599 		case SLJIT_CLZ:
600 		case SLJIT_MUL:
601 			/* No form with immediate operand. */
602 			break;
603 		case SLJIT_MOV:
604 			SLJIT_ASSERT(!(flags & SET_FLAGS) && (flags & ARG2_IMM) && arg1 == TMP_REG2);
605 			return load_immediate(compiler, dst, imm);
606 		case SLJIT_NOT:
607 			if (!(flags & SET_FLAGS))
608 				return load_immediate(compiler, dst, ~imm);
609 			/* Since the flags should be set, we just fallback to the register mode.
610 			   Although some clever things could be done here, "NOT IMM" does not worth the efforts. */
611 			break;
612 		case SLJIT_ADD:
613 			nimm = -(sljit_sw)imm;
614 			if (IS_2_LO_REGS(reg, dst)) {
615 				if (imm <= 0x7)
616 					return push_inst16(compiler, ADDSI3 | IMM3(imm) | RD3(dst) | RN3(reg));
617 				if (nimm <= 0x7)
618 					return push_inst16(compiler, SUBSI3 | IMM3(nimm) | RD3(dst) | RN3(reg));
619 				if (reg == dst) {
620 					if (imm <= 0xff)
621 						return push_inst16(compiler, ADDSI8 | IMM8(imm) | RDN3(dst));
622 					if (nimm <= 0xff)
623 						return push_inst16(compiler, SUBSI8 | IMM8(nimm) | RDN3(dst));
624 				}
625 			}
626 			if (!(flags & SET_FLAGS)) {
627 				if (imm <= 0xfff)
628 					return push_inst32(compiler, ADDWI | RD4(dst) | RN4(reg) | IMM12(imm));
629 				if (nimm <= 0xfff)
630 					return push_inst32(compiler, SUBWI | RD4(dst) | RN4(reg) | IMM12(nimm));
631 			}
632 			nimm = get_imm(imm);
633 			if (nimm != INVALID_IMM)
634 				return push_inst32(compiler, ADD_WI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | nimm);
635 			nimm = get_imm(-(sljit_sw)imm);
636 			if (nimm != INVALID_IMM)
637 				return push_inst32(compiler, SUB_WI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | nimm);
638 			break;
639 		case SLJIT_ADDC:
640 			imm = get_imm(imm);
641 			if (imm != INVALID_IMM)
642 				return push_inst32(compiler, ADCI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
643 			break;
644 		case SLJIT_SUB:
645 			/* SUB operation can be replaced by ADD because of the negative carry flag. */
646 			if (flags & ARG1_IMM) {
647 				if (imm == 0 && IS_2_LO_REGS(reg, dst))
648 					return push_inst16(compiler, RSBSI | RD3(dst) | RN3(reg));
649 				imm = get_imm(imm);
650 				if (imm != INVALID_IMM)
651 					return push_inst32(compiler, RSB_WI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
652 				break;
653 			}
654 			if (flags & UNUSED_RETURN) {
655 				if (imm <= 0xff && reg_map[reg] <= 7)
656 					return push_inst16(compiler, CMPI | IMM8(imm) | RDN3(reg));
657 				nimm = get_imm(imm);
658 				if (nimm != INVALID_IMM)
659 					return push_inst32(compiler, CMPI_W | RN4(reg) | nimm);
660 				nimm = get_imm(-(sljit_sw)imm);
661 				if (nimm != INVALID_IMM)
662 					return push_inst32(compiler, CMNI_W | RN4(reg) | nimm);
663 			}
664 			nimm = -(sljit_sw)imm;
665 			if (IS_2_LO_REGS(reg, dst)) {
666 				if (imm <= 0x7)
667 					return push_inst16(compiler, SUBSI3 | IMM3(imm) | RD3(dst) | RN3(reg));
668 				if (nimm <= 0x7)
669 					return push_inst16(compiler, ADDSI3 | IMM3(nimm) | RD3(dst) | RN3(reg));
670 				if (reg == dst) {
671 					if (imm <= 0xff)
672 						return push_inst16(compiler, SUBSI8 | IMM8(imm) | RDN3(dst));
673 					if (nimm <= 0xff)
674 						return push_inst16(compiler, ADDSI8 | IMM8(nimm) | RDN3(dst));
675 				}
676 			}
677 			if (!(flags & SET_FLAGS)) {
678 				if (imm <= 0xfff)
679 					return push_inst32(compiler, SUBWI | RD4(dst) | RN4(reg) | IMM12(imm));
680 				if (nimm <= 0xfff)
681 					return push_inst32(compiler, ADDWI | RD4(dst) | RN4(reg) | IMM12(nimm));
682 			}
683 			nimm = get_imm(imm);
684 			if (nimm != INVALID_IMM)
685 				return push_inst32(compiler, SUB_WI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | nimm);
686 			nimm = get_imm(-(sljit_sw)imm);
687 			if (nimm != INVALID_IMM)
688 				return push_inst32(compiler, ADD_WI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | nimm);
689 			break;
690 		case SLJIT_SUBC:
691 			if (flags & ARG1_IMM)
692 				break;
693 			imm = get_imm(imm);
694 			if (imm != INVALID_IMM)
695 				return push_inst32(compiler, SBCI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
696 			break;
697 		case SLJIT_AND:
698 			nimm = get_imm(imm);
699 			if (nimm != INVALID_IMM)
700 				return push_inst32(compiler, ANDI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | nimm);
701 			imm = get_imm(imm);
702 			if (imm != INVALID_IMM)
703 				return push_inst32(compiler, BICI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
704 			break;
705 		case SLJIT_OR:
706 			nimm = get_imm(imm);
707 			if (nimm != INVALID_IMM)
708 				return push_inst32(compiler, ORRI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | nimm);
709 			imm = get_imm(imm);
710 			if (imm != INVALID_IMM)
711 				return push_inst32(compiler, ORNI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
712 			break;
713 		case SLJIT_XOR:
714 			imm = get_imm(imm);
715 			if (imm != INVALID_IMM)
716 				return push_inst32(compiler, EORI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
717 			break;
718 		case SLJIT_SHL:
719 		case SLJIT_LSHR:
720 		case SLJIT_ASHR:
721 			if (flags & ARG1_IMM)
722 				break;
723 			imm &= 0x1f;
724 			if (imm == 0) {
725 				if (!(flags & SET_FLAGS))
726 					return push_inst16(compiler, MOV | SET_REGS44(dst, reg));
727 				if (IS_2_LO_REGS(dst, reg))
728 					return push_inst16(compiler, MOVS | RD3(dst) | RN3(reg));
729 				return push_inst32(compiler, MOV_W | SET_FLAGS | RD4(dst) | RM4(reg));
730 			}
731 			switch (flags & 0xffff) {
732 			case SLJIT_SHL:
733 				if (IS_2_LO_REGS(dst, reg))
734 					return push_inst16(compiler, LSLSI | RD3(dst) | RN3(reg) | (imm << 6));
735 				return push_inst32(compiler, LSL_WI | (flags & SET_FLAGS) | RD4(dst) | RM4(reg) | IMM5(imm));
736 			case SLJIT_LSHR:
737 				if (IS_2_LO_REGS(dst, reg))
738 					return push_inst16(compiler, LSRSI | RD3(dst) | RN3(reg) | (imm << 6));
739 				return push_inst32(compiler, LSR_WI | (flags & SET_FLAGS) | RD4(dst) | RM4(reg) | IMM5(imm));
740 			default: /* SLJIT_ASHR */
741 				if (IS_2_LO_REGS(dst, reg))
742 					return push_inst16(compiler, ASRSI | RD3(dst) | RN3(reg) | (imm << 6));
743 				return push_inst32(compiler, ASR_WI | (flags & SET_FLAGS) | RD4(dst) | RM4(reg) | IMM5(imm));
744 			}
745 		default:
746 			SLJIT_UNREACHABLE();
747 			break;
748 		}
749 
750 		if (flags & ARG2_IMM) {
751 			imm = arg2;
752 			arg2 = (arg1 == TMP_REG1) ? TMP_REG2 : TMP_REG1;
753 			FAIL_IF(load_immediate(compiler, arg2, imm));
754 		}
755 		else {
756 			imm = arg1;
757 			arg1 = (arg2 == TMP_REG1) ? TMP_REG2 : TMP_REG1;
758 			FAIL_IF(load_immediate(compiler, arg1, imm));
759 		}
760 
761 		SLJIT_ASSERT(arg1 != arg2);
762 	}
763 
764 	/* Both arguments are registers. */
765 	switch (flags & 0xffff) {
766 	case SLJIT_MOV:
767 	case SLJIT_MOV_U32:
768 	case SLJIT_MOV_S32:
769 	case SLJIT_MOV_P:
770 		SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG2);
771 		if (dst == arg2)
772 			return SLJIT_SUCCESS;
773 		return push_inst16(compiler, MOV | SET_REGS44(dst, arg2));
774 	case SLJIT_MOV_U8:
775 		SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG2);
776 		if (IS_2_LO_REGS(dst, arg2))
777 			return push_inst16(compiler, UXTB | RD3(dst) | RN3(arg2));
778 		return push_inst32(compiler, UXTB_W | RD4(dst) | RM4(arg2));
779 	case SLJIT_MOV_S8:
780 		SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG2);
781 		if (IS_2_LO_REGS(dst, arg2))
782 			return push_inst16(compiler, SXTB | RD3(dst) | RN3(arg2));
783 		return push_inst32(compiler, SXTB_W | RD4(dst) | RM4(arg2));
784 	case SLJIT_MOV_U16:
785 		SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG2);
786 		if (IS_2_LO_REGS(dst, arg2))
787 			return push_inst16(compiler, UXTH | RD3(dst) | RN3(arg2));
788 		return push_inst32(compiler, UXTH_W | RD4(dst) | RM4(arg2));
789 	case SLJIT_MOV_S16:
790 		SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG2);
791 		if (IS_2_LO_REGS(dst, arg2))
792 			return push_inst16(compiler, SXTH | RD3(dst) | RN3(arg2));
793 		return push_inst32(compiler, SXTH_W | RD4(dst) | RM4(arg2));
794 	case SLJIT_NOT:
795 		SLJIT_ASSERT(arg1 == TMP_REG2);
796 		if (IS_2_LO_REGS(dst, arg2))
797 			return push_inst16(compiler, MVNS | RD3(dst) | RN3(arg2));
798 		return push_inst32(compiler, MVN_W | (flags & SET_FLAGS) | RD4(dst) | RM4(arg2));
799 	case SLJIT_CLZ:
800 		SLJIT_ASSERT(arg1 == TMP_REG2);
801 		FAIL_IF(push_inst32(compiler, CLZ | RN4(arg2) | RD4(dst) | RM4(arg2)));
802 		return SLJIT_SUCCESS;
803 	case SLJIT_ADD:
804 		if (IS_3_LO_REGS(dst, arg1, arg2))
805 			return push_inst16(compiler, ADDS | RD3(dst) | RN3(arg1) | RM3(arg2));
806 		if (dst == arg1 && !(flags & SET_FLAGS))
807 			return push_inst16(compiler, ADD | SET_REGS44(dst, arg2));
808 		return push_inst32(compiler, ADD_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
809 	case SLJIT_ADDC:
810 		if (dst == arg1 && IS_2_LO_REGS(dst, arg2))
811 			return push_inst16(compiler, ADCS | RD3(dst) | RN3(arg2));
812 		return push_inst32(compiler, ADC_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
813 	case SLJIT_SUB:
814 		if (flags & UNUSED_RETURN) {
815 			if (IS_2_LO_REGS(arg1, arg2))
816 				return push_inst16(compiler, CMP | RD3(arg1) | RN3(arg2));
817 			return push_inst16(compiler, CMP_X | SET_REGS44(arg1, arg2));
818 		}
819 		if (IS_3_LO_REGS(dst, arg1, arg2))
820 			return push_inst16(compiler, SUBS | RD3(dst) | RN3(arg1) | RM3(arg2));
821 		return push_inst32(compiler, SUB_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
822 	case SLJIT_SUBC:
823 		if (dst == arg1 && IS_2_LO_REGS(dst, arg2))
824 			return push_inst16(compiler, SBCS | RD3(dst) | RN3(arg2));
825 		return push_inst32(compiler, SBC_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
826 	case SLJIT_MUL:
827 		if (!(flags & SET_FLAGS))
828 			return push_inst32(compiler, MUL | RD4(dst) | RN4(arg1) | RM4(arg2));
829 		SLJIT_ASSERT(dst != TMP_REG2);
830 		FAIL_IF(push_inst32(compiler, SMULL | RT4(dst) | RD4(TMP_REG2) | RN4(arg1) | RM4(arg2)));
831 		/* cmp TMP_REG2, dst asr #31. */
832 		return push_inst32(compiler, CMP_W | RN4(TMP_REG2) | 0x70e0 | RM4(dst));
833 	case SLJIT_AND:
834 		if (dst == arg1 && IS_2_LO_REGS(dst, arg2))
835 			return push_inst16(compiler, ANDS | RD3(dst) | RN3(arg2));
836 		if ((flags & UNUSED_RETURN) && IS_2_LO_REGS(arg1, arg2))
837 			return push_inst16(compiler, TST | RD3(arg1) | RN3(arg2));
838 		return push_inst32(compiler, AND_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
839 	case SLJIT_OR:
840 		if (dst == arg1 && IS_2_LO_REGS(dst, arg2))
841 			return push_inst16(compiler, ORRS | RD3(dst) | RN3(arg2));
842 		return push_inst32(compiler, ORR_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
843 	case SLJIT_XOR:
844 		if (dst == arg1 && IS_2_LO_REGS(dst, arg2))
845 			return push_inst16(compiler, EORS | RD3(dst) | RN3(arg2));
846 		return push_inst32(compiler, EOR_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
847 	case SLJIT_SHL:
848 		if (dst == arg1 && IS_2_LO_REGS(dst, arg2))
849 			return push_inst16(compiler, LSLS | RD3(dst) | RN3(arg2));
850 		return push_inst32(compiler, LSL_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
851 	case SLJIT_LSHR:
852 		if (dst == arg1 && IS_2_LO_REGS(dst, arg2))
853 			return push_inst16(compiler, LSRS | RD3(dst) | RN3(arg2));
854 		return push_inst32(compiler, LSR_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
855 	case SLJIT_ASHR:
856 		if (dst == arg1 && IS_2_LO_REGS(dst, arg2))
857 			return push_inst16(compiler, ASRS | RD3(dst) | RN3(arg2));
858 		return push_inst32(compiler, ASR_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
859 	}
860 
861 	SLJIT_UNREACHABLE();
862 	return SLJIT_SUCCESS;
863 }
864 
865 #define STORE		0x01
866 #define SIGNED		0x02
867 
868 #define WORD_SIZE	0x00
869 #define BYTE_SIZE	0x04
870 #define HALF_SIZE	0x08
871 #define PRELOAD		0x0c
872 
873 #define IS_WORD_SIZE(flags)		(!(flags & (BYTE_SIZE | HALF_SIZE)))
874 #define OFFSET_CHECK(imm, shift)	(!(argw & ~(imm << shift)))
875 
876 /*
877   1st letter:
878   w = word
879   b = byte
880   h = half
881 
882   2nd letter:
883   s = signed
884   u = unsigned
885 
886   3rd letter:
887   l = load
888   s = store
889 */
890 
891 static const sljit_ins sljit_mem16[12] = {
892 /* w u l */ 0x5800 /* ldr */,
893 /* w u s */ 0x5000 /* str */,
894 /* w s l */ 0x5800 /* ldr */,
895 /* w s s */ 0x5000 /* str */,
896 
897 /* b u l */ 0x5c00 /* ldrb */,
898 /* b u s */ 0x5400 /* strb */,
899 /* b s l */ 0x5600 /* ldrsb */,
900 /* b s s */ 0x5400 /* strb */,
901 
902 /* h u l */ 0x5a00 /* ldrh */,
903 /* h u s */ 0x5200 /* strh */,
904 /* h s l */ 0x5e00 /* ldrsh */,
905 /* h s s */ 0x5200 /* strh */,
906 };
907 
908 static const sljit_ins sljit_mem16_imm5[12] = {
909 /* w u l */ 0x6800 /* ldr imm5 */,
910 /* w u s */ 0x6000 /* str imm5 */,
911 /* w s l */ 0x6800 /* ldr imm5 */,
912 /* w s s */ 0x6000 /* str imm5 */,
913 
914 /* b u l */ 0x7800 /* ldrb imm5 */,
915 /* b u s */ 0x7000 /* strb imm5 */,
916 /* b s l */ 0x0000 /* not allowed */,
917 /* b s s */ 0x7000 /* strb imm5 */,
918 
919 /* h u l */ 0x8800 /* ldrh imm5 */,
920 /* h u s */ 0x8000 /* strh imm5 */,
921 /* h s l */ 0x0000 /* not allowed */,
922 /* h s s */ 0x8000 /* strh imm5 */,
923 };
924 
925 #define MEM_IMM8	0xc00
926 #define MEM_IMM12	0x800000
927 static const sljit_ins sljit_mem32[13] = {
928 /* w u l */ 0xf8500000 /* ldr.w */,
929 /* w u s */ 0xf8400000 /* str.w */,
930 /* w s l */ 0xf8500000 /* ldr.w */,
931 /* w s s */ 0xf8400000 /* str.w */,
932 
933 /* b u l */ 0xf8100000 /* ldrb.w */,
934 /* b u s */ 0xf8000000 /* strb.w */,
935 /* b s l */ 0xf9100000 /* ldrsb.w */,
936 /* b s s */ 0xf8000000 /* strb.w */,
937 
938 /* h u l */ 0xf8300000 /* ldrh.w */,
939 /* h u s */ 0xf8200000 /* strsh.w */,
940 /* h s l */ 0xf9300000 /* ldrsh.w */,
941 /* h s s */ 0xf8200000 /* strsh.w */,
942 
943 /* p u l */ 0xf8100000 /* pld */,
944 };
945 
946 /* 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)947 static sljit_s32 emit_set_delta(struct sljit_compiler *compiler, sljit_s32 dst, sljit_s32 reg, sljit_sw value)
948 {
949 	if (value >= 0) {
950 		if (value <= 0xfff)
951 			return push_inst32(compiler, ADDWI | RD4(dst) | RN4(reg) | IMM12(value));
952 		value = get_imm(value);
953 		if (value != INVALID_IMM)
954 			return push_inst32(compiler, ADD_WI | RD4(dst) | RN4(reg) | value);
955 	}
956 	else {
957 		value = -value;
958 		if (value <= 0xfff)
959 			return push_inst32(compiler, SUBWI | RD4(dst) | RN4(reg) | IMM12(value));
960 		value = get_imm(value);
961 		if (value != INVALID_IMM)
962 			return push_inst32(compiler, SUB_WI | RD4(dst) | RN4(reg) | value);
963 	}
964 	return SLJIT_ERR_UNSUPPORTED;
965 }
966 
emit_op_mem(struct sljit_compiler * compiler,sljit_s32 flags,sljit_s32 reg,sljit_s32 arg,sljit_sw argw,sljit_s32 tmp_reg)967 static SLJIT_INLINE sljit_s32 emit_op_mem(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg,
968 	sljit_s32 arg, sljit_sw argw, sljit_s32 tmp_reg)
969 {
970 	sljit_s32 other_r;
971 	sljit_uw tmp;
972 
973 	SLJIT_ASSERT(arg & SLJIT_MEM);
974 	SLJIT_ASSERT((arg & REG_MASK) != tmp_reg);
975 	arg &= ~SLJIT_MEM;
976 
977 	if (SLJIT_UNLIKELY(!(arg & REG_MASK))) {
978 		tmp = get_imm(argw & ~0xfff);
979 		if (tmp != INVALID_IMM) {
980 			FAIL_IF(push_inst32(compiler, MOV_WI | RD4(tmp_reg) | tmp));
981 			return push_inst32(compiler, sljit_mem32[flags] | MEM_IMM12 | RT4(reg) | RN4(tmp_reg) | (argw & 0xfff));
982 		}
983 
984 		FAIL_IF(load_immediate(compiler, tmp_reg, argw));
985 		if (IS_2_LO_REGS(reg, tmp_reg) && sljit_mem16_imm5[flags])
986 			return push_inst16(compiler, sljit_mem16_imm5[flags] | RD3(reg) | RN3(tmp_reg));
987 		return push_inst32(compiler, sljit_mem32[flags] | MEM_IMM12 | RT4(reg) | RN4(tmp_reg));
988 	}
989 
990 	if (SLJIT_UNLIKELY(arg & OFFS_REG_MASK)) {
991 		argw &= 0x3;
992 		other_r = OFFS_REG(arg);
993 		arg &= 0xf;
994 
995 		if (!argw && IS_3_LO_REGS(reg, arg, other_r))
996 			return push_inst16(compiler, sljit_mem16[flags] | RD3(reg) | RN3(arg) | RM3(other_r));
997 		return push_inst32(compiler, sljit_mem32[flags] | RT4(reg) | RN4(arg) | RM4(other_r) | (argw << 4));
998 	}
999 
1000 	if (argw > 0xfff) {
1001 		tmp = get_imm(argw & ~0xfff);
1002 		if (tmp != INVALID_IMM) {
1003 			push_inst32(compiler, ADD_WI | RD4(tmp_reg) | RN4(arg) | tmp);
1004 			arg = tmp_reg;
1005 			argw = argw & 0xfff;
1006 		}
1007 	}
1008 	else if (argw < -0xff) {
1009 		tmp = get_imm(-argw & ~0xff);
1010 		if (tmp != INVALID_IMM) {
1011 			push_inst32(compiler, SUB_WI | RD4(tmp_reg) | RN4(arg) | tmp);
1012 			arg = tmp_reg;
1013 			argw = -(-argw & 0xff);
1014 		}
1015 	}
1016 
1017 	if (IS_2_LO_REGS(reg, arg) && sljit_mem16_imm5[flags]) {
1018 		tmp = 3;
1019 		if (IS_WORD_SIZE(flags)) {
1020 			if (OFFSET_CHECK(0x1f, 2))
1021 				tmp = 2;
1022 		}
1023 		else if (flags & BYTE_SIZE)
1024 		{
1025 			if (OFFSET_CHECK(0x1f, 0))
1026 				tmp = 0;
1027 		}
1028 		else {
1029 			SLJIT_ASSERT(flags & HALF_SIZE);
1030 			if (OFFSET_CHECK(0x1f, 1))
1031 				tmp = 1;
1032 		}
1033 
1034 		if (tmp < 3)
1035 			return push_inst16(compiler, sljit_mem16_imm5[flags] | RD3(reg) | RN3(arg) | (argw << (6 - tmp)));
1036 	}
1037 	else if (SLJIT_UNLIKELY(arg == SLJIT_SP) && IS_WORD_SIZE(flags) && OFFSET_CHECK(0xff, 2) && reg_map[reg] <= 7) {
1038 		/* SP based immediate. */
1039 		return push_inst16(compiler, STR_SP | ((flags & STORE) ? 0 : 0x800) | RDN3(reg) | (argw >> 2));
1040 	}
1041 
1042 	if (argw >= 0 && argw <= 0xfff)
1043 		return push_inst32(compiler, sljit_mem32[flags] | MEM_IMM12 | RT4(reg) | RN4(arg) | argw);
1044 	else if (argw < 0 && argw >= -0xff)
1045 		return push_inst32(compiler, sljit_mem32[flags] | MEM_IMM8 | RT4(reg) | RN4(arg) | -argw);
1046 
1047 	SLJIT_ASSERT(arg != tmp_reg);
1048 
1049 	FAIL_IF(load_immediate(compiler, tmp_reg, argw));
1050 	if (IS_3_LO_REGS(reg, arg, tmp_reg))
1051 		return push_inst16(compiler, sljit_mem16[flags] | RD3(reg) | RN3(arg) | RM3(tmp_reg));
1052 	return push_inst32(compiler, sljit_mem32[flags] | RT4(reg) | RN4(arg) | RM4(tmp_reg));
1053 }
1054 
1055 /* --------------------------------------------------------------------- */
1056 /*  Entry, exit                                                          */
1057 /* --------------------------------------------------------------------- */
1058 
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)1059 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_enter(struct sljit_compiler *compiler,
1060 	sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
1061 	sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
1062 {
1063 	sljit_s32 args, size, i, tmp;
1064 	sljit_ins push = 0;
1065 #ifdef _WIN32
1066 	sljit_uw imm;
1067 #endif
1068 
1069 	CHECK_ERROR();
1070 	CHECK(check_sljit_emit_enter(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size));
1071 	set_emit_enter(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size);
1072 
1073 	tmp = saveds < SLJIT_NUMBER_OF_SAVED_REGISTERS ? (SLJIT_S0 + 1 - saveds) : SLJIT_FIRST_SAVED_REG;
1074 	for (i = SLJIT_S0; i >= tmp; i--)
1075 		push |= 1 << reg_map[i];
1076 
1077 	for (i = scratches; i >= SLJIT_FIRST_SAVED_REG; i--)
1078 		push |= 1 << reg_map[i];
1079 
1080 	FAIL_IF((push & 0xff00)
1081 		? push_inst32(compiler, PUSH_W | (1 << 14) | push)
1082 		: push_inst16(compiler, PUSH | (1 << 8) | push));
1083 
1084 	/* Stack must be aligned to 8 bytes: (LR, R4) */
1085 	size = GET_SAVED_REGISTERS_SIZE(scratches, saveds, 1);
1086 	local_size = ((size + local_size + 7) & ~7) - size;
1087 	compiler->local_size = local_size;
1088 
1089 #ifdef _WIN32
1090 	if (local_size >= 256) {
1091 		if (local_size > 4096)
1092 			imm = get_imm(4096);
1093 		else
1094 			imm = get_imm(local_size & ~0xff);
1095 
1096 		SLJIT_ASSERT(imm != INVALID_IMM);
1097 		FAIL_IF(push_inst32(compiler, SUB_WI | RD4(TMP_REG1) | RN4(SLJIT_SP) | imm));
1098 	}
1099 #else
1100 	if (local_size > 0) {
1101 		if (local_size <= (127 << 2))
1102 			FAIL_IF(push_inst16(compiler, SUB_SP | (local_size >> 2)));
1103 		else
1104 			FAIL_IF(emit_op_imm(compiler, SLJIT_SUB | ARG2_IMM, SLJIT_SP, SLJIT_SP, local_size));
1105 	}
1106 #endif
1107 
1108 	args = get_arg_count(arg_types);
1109 
1110 	if (args >= 1)
1111 		FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(SLJIT_S0, SLJIT_R0)));
1112 	if (args >= 2)
1113 		FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(SLJIT_S1, SLJIT_R1)));
1114 	if (args >= 3)
1115 		FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(SLJIT_S2, SLJIT_R2)));
1116 
1117 #ifdef _WIN32
1118 	if (local_size >= 256) {
1119 		if (local_size > 4096) {
1120 			imm = get_imm(4096);
1121 			SLJIT_ASSERT(imm != INVALID_IMM);
1122 
1123 			if (local_size < 4 * 4096) {
1124 				if (local_size > 2 * 4096) {
1125 					FAIL_IF(push_inst32(compiler, LDRI | 0x400 | RT4(TMP_REG2) | RN4(TMP_REG1)));
1126 					FAIL_IF(push_inst32(compiler, SUB_WI | RD4(TMP_REG1) | RN4(TMP_REG1) | imm));
1127 					local_size -= 4096;
1128 				}
1129 
1130 				if (local_size > 2 * 4096) {
1131 					FAIL_IF(push_inst32(compiler, LDRI | 0x400 | RT4(TMP_REG2) | RN4(TMP_REG1)));
1132 					FAIL_IF(push_inst32(compiler, SUB_WI | RD4(TMP_REG1) | RN4(TMP_REG1) | imm));
1133 					local_size -= 4096;
1134 				}
1135 
1136 				FAIL_IF(push_inst32(compiler, LDRI | 0x400 | RT4(TMP_REG2) | RN4(TMP_REG1)));
1137 				local_size -= 4096;
1138 
1139 				SLJIT_ASSERT(local_size > 0);
1140 			}
1141 			else {
1142 				FAIL_IF(load_immediate(compiler, SLJIT_R3, (local_size >> 12) - 1));
1143 				FAIL_IF(push_inst32(compiler, LDRI | 0x400 | RT4(TMP_REG2) | RN4(TMP_REG1)));
1144 				FAIL_IF(push_inst32(compiler, SUB_WI | RD4(TMP_REG1) | RN4(TMP_REG1) | imm));
1145 				SLJIT_ASSERT(reg_map[SLJIT_R3] < 7);
1146 				FAIL_IF(push_inst16(compiler, SUBSI8 | RDN3(SLJIT_R3) | 1));
1147 				FAIL_IF(push_inst16(compiler, BCC | (0x1 << 8) /* not-equal */ | (-7 & 0xff)));
1148 
1149 				local_size &= 0xfff;
1150 
1151 				if (local_size != 0)
1152 					FAIL_IF(push_inst32(compiler, LDRI | 0x400 | RT4(TMP_REG2) | RN4(TMP_REG1)));
1153 			}
1154 
1155 			if (local_size >= 256) {
1156 				imm = get_imm(local_size & ~0xff);
1157 				SLJIT_ASSERT(imm != INVALID_IMM);
1158 
1159 				FAIL_IF(push_inst32(compiler, SUB_WI | RD4(TMP_REG1) | RN4(TMP_REG1) | imm));
1160 			}
1161 		}
1162 
1163 		local_size &= 0xff;
1164 		FAIL_IF(push_inst32(compiler, LDRI | 0x400 | (local_size > 0 ? 0x100 : 0) | RT4(TMP_REG2) | RN4(TMP_REG1) | local_size));
1165 
1166 		FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(SLJIT_SP, TMP_REG1)));
1167 	}
1168 	else if (local_size > 0)
1169 		FAIL_IF(push_inst32(compiler, LDRI | 0x500 | RT4(TMP_REG1) | RN4(SLJIT_SP) | local_size));
1170 #endif
1171 
1172 	return SLJIT_SUCCESS;
1173 }
1174 
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)1175 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_set_context(struct sljit_compiler *compiler,
1176 	sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
1177 	sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
1178 {
1179 	sljit_s32 size;
1180 
1181 	CHECK_ERROR();
1182 	CHECK(check_sljit_set_context(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size));
1183 	set_set_context(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size);
1184 
1185 	size = GET_SAVED_REGISTERS_SIZE(scratches, saveds, 1);
1186 	compiler->local_size = ((size + local_size + 7) & ~7) - size;
1187 	return SLJIT_SUCCESS;
1188 }
1189 
sljit_emit_return(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 src,sljit_sw srcw)1190 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_return(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 src, sljit_sw srcw)
1191 {
1192 	sljit_s32 i, tmp;
1193 	sljit_ins pop = 0;
1194 
1195 	CHECK_ERROR();
1196 	CHECK(check_sljit_emit_return(compiler, op, src, srcw));
1197 
1198 	FAIL_IF(emit_mov_before_return(compiler, op, src, srcw));
1199 
1200 	if (compiler->local_size > 0) {
1201 		if (compiler->local_size <= (127 << 2))
1202 			FAIL_IF(push_inst16(compiler, ADD_SP | (compiler->local_size >> 2)));
1203 		else
1204 			FAIL_IF(emit_op_imm(compiler, SLJIT_ADD | ARG2_IMM, SLJIT_SP, SLJIT_SP, compiler->local_size));
1205 	}
1206 
1207 	tmp = compiler->saveds < SLJIT_NUMBER_OF_SAVED_REGISTERS ? (SLJIT_S0 + 1 - compiler->saveds) : SLJIT_FIRST_SAVED_REG;
1208 	for (i = SLJIT_S0; i >= tmp; i--)
1209 		pop |= 1 << reg_map[i];
1210 
1211 	for (i = compiler->scratches; i >= SLJIT_FIRST_SAVED_REG; i--)
1212 		pop |= 1 << reg_map[i];
1213 
1214 	return (pop & 0xff00)
1215 		? push_inst32(compiler, POP_W | (1 << 15) | pop)
1216 		: push_inst16(compiler, POP | (1 << 8) | pop);
1217 }
1218 
1219 /* --------------------------------------------------------------------- */
1220 /*  Operators                                                            */
1221 /* --------------------------------------------------------------------- */
1222 
1223 #if !(defined __ARM_FEATURE_IDIV) && !(defined __ARM_ARCH_EXT_IDIV__)
1224 
1225 #ifdef __cplusplus
1226 extern "C" {
1227 #endif
1228 
1229 #ifdef _WIN32
1230 extern unsigned long long __rt_udiv(unsigned int denominator, unsigned int numerator);
1231 extern long long __rt_sdiv(int denominator, int numerator);
1232 #elif defined(__GNUC__)
1233 extern unsigned int __aeabi_uidivmod(unsigned int numerator, int unsigned denominator);
1234 extern int __aeabi_idivmod(int numerator, int denominator);
1235 #else
1236 #error "Software divmod functions are needed"
1237 #endif
1238 
1239 #ifdef __cplusplus
1240 }
1241 #endif
1242 
1243 #endif /* !__ARM_FEATURE_IDIV && !__ARM_ARCH_EXT_IDIV__ */
1244 
sljit_emit_op0(struct sljit_compiler * compiler,sljit_s32 op)1245 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op0(struct sljit_compiler *compiler, sljit_s32 op)
1246 {
1247 #if !(defined __ARM_FEATURE_IDIV) && !(defined __ARM_ARCH_EXT_IDIV__)
1248 	sljit_sw saved_reg_list[3];
1249 	sljit_sw saved_reg_count;
1250 #endif
1251 
1252 	CHECK_ERROR();
1253 	CHECK(check_sljit_emit_op0(compiler, op));
1254 
1255 	op = GET_OPCODE(op);
1256 	switch (op) {
1257 	case SLJIT_BREAKPOINT:
1258 		return push_inst16(compiler, BKPT);
1259 	case SLJIT_NOP:
1260 		return push_inst16(compiler, NOP);
1261 	case SLJIT_LMUL_UW:
1262 	case SLJIT_LMUL_SW:
1263 		return push_inst32(compiler, (op == SLJIT_LMUL_UW ? UMULL : SMULL)
1264 			| (reg_map[SLJIT_R1] << 8)
1265 			| (reg_map[SLJIT_R0] << 12)
1266 			| (reg_map[SLJIT_R0] << 16)
1267 			| reg_map[SLJIT_R1]);
1268 #if (defined __ARM_FEATURE_IDIV) || (defined __ARM_ARCH_EXT_IDIV__)
1269 	case SLJIT_DIVMOD_UW:
1270 	case SLJIT_DIVMOD_SW:
1271 		FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(TMP_REG1, SLJIT_R0)));
1272 		FAIL_IF(push_inst32(compiler, (op == SLJIT_DIVMOD_UW ? UDIV : SDIV) | RD4(SLJIT_R0) | RN4(SLJIT_R0) | RM4(SLJIT_R1)));
1273 		FAIL_IF(push_inst32(compiler, MUL | RD4(SLJIT_R1) | RN4(SLJIT_R0) | RM4(SLJIT_R1)));
1274 		return push_inst32(compiler, SUB_W | RD4(SLJIT_R1) | RN4(TMP_REG1) | RM4(SLJIT_R1));
1275 	case SLJIT_DIV_UW:
1276 	case SLJIT_DIV_SW:
1277 		return push_inst32(compiler, (op == SLJIT_DIV_UW ? UDIV : SDIV) | RD4(SLJIT_R0) | RN4(SLJIT_R0) | RM4(SLJIT_R1));
1278 #else /* !__ARM_FEATURE_IDIV && !__ARM_ARCH_EXT_IDIV__ */
1279 	case SLJIT_DIVMOD_UW:
1280 	case SLJIT_DIVMOD_SW:
1281 	case SLJIT_DIV_UW:
1282 	case SLJIT_DIV_SW:
1283 		SLJIT_COMPILE_ASSERT((SLJIT_DIVMOD_UW & 0x2) == 0 && SLJIT_DIV_UW - 0x2 == SLJIT_DIVMOD_UW, bad_div_opcode_assignments);
1284 		SLJIT_ASSERT(reg_map[2] == 1 && reg_map[3] == 2 && reg_map[4] == 3);
1285 
1286 		saved_reg_count = 0;
1287 		if (compiler->scratches >= 4)
1288 			saved_reg_list[saved_reg_count++] = 3;
1289 		if (compiler->scratches >= 3)
1290 			saved_reg_list[saved_reg_count++] = 2;
1291 		if (op >= SLJIT_DIV_UW)
1292 			saved_reg_list[saved_reg_count++] = 1;
1293 
1294 		if (saved_reg_count > 0) {
1295 			FAIL_IF(push_inst32(compiler, 0xf84d0d00 | (saved_reg_count >= 3 ? 16 : 8)
1296 						| (saved_reg_list[0] << 12) /* str rX, [sp, #-8/-16]! */));
1297 			if (saved_reg_count >= 2) {
1298 				SLJIT_ASSERT(saved_reg_list[1] < 8);
1299 				FAIL_IF(push_inst16(compiler, 0x9001 | (saved_reg_list[1] << 8) /* str rX, [sp, #4] */));
1300 			}
1301 			if (saved_reg_count >= 3) {
1302 				SLJIT_ASSERT(saved_reg_list[2] < 8);
1303 				FAIL_IF(push_inst16(compiler, 0x9002 | (saved_reg_list[2] << 8) /* str rX, [sp, #8] */));
1304 			}
1305 		}
1306 
1307 #ifdef _WIN32
1308 		FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(TMP_REG1, SLJIT_R0)));
1309 		FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(SLJIT_R0, SLJIT_R1)));
1310 		FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(SLJIT_R1, TMP_REG1)));
1311 		FAIL_IF(sljit_emit_ijump(compiler, SLJIT_FAST_CALL, SLJIT_IMM,
1312 			((op | 0x2) == SLJIT_DIV_UW ? SLJIT_FUNC_OFFSET(__rt_udiv) : SLJIT_FUNC_OFFSET(__rt_sdiv))));
1313 #elif defined(__GNUC__)
1314 		FAIL_IF(sljit_emit_ijump(compiler, SLJIT_FAST_CALL, SLJIT_IMM,
1315 			((op | 0x2) == SLJIT_DIV_UW ? SLJIT_FUNC_OFFSET(__aeabi_uidivmod) : SLJIT_FUNC_OFFSET(__aeabi_idivmod))));
1316 #else
1317 #error "Software divmod functions are needed"
1318 #endif
1319 
1320 		if (saved_reg_count > 0) {
1321 			if (saved_reg_count >= 3) {
1322 				SLJIT_ASSERT(saved_reg_list[2] < 8);
1323 				FAIL_IF(push_inst16(compiler, 0x9802 | (saved_reg_list[2] << 8) /* ldr rX, [sp, #8] */));
1324 			}
1325 			if (saved_reg_count >= 2) {
1326 				SLJIT_ASSERT(saved_reg_list[1] < 8);
1327 				FAIL_IF(push_inst16(compiler, 0x9801 | (saved_reg_list[1] << 8) /* ldr rX, [sp, #4] */));
1328 			}
1329 			return push_inst32(compiler, 0xf85d0b00 | (saved_reg_count >= 3 ? 16 : 8)
1330 						| (saved_reg_list[0] << 12) /* ldr rX, [sp], #8/16 */);
1331 		}
1332 		return SLJIT_SUCCESS;
1333 #endif /* __ARM_FEATURE_IDIV || __ARM_ARCH_EXT_IDIV__ */
1334 	case SLJIT_ENDBR:
1335 	case SLJIT_SKIP_FRAMES_BEFORE_RETURN:
1336 		return SLJIT_SUCCESS;
1337 	}
1338 
1339 	return SLJIT_SUCCESS;
1340 }
1341 
sljit_emit_op1(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 src,sljit_sw srcw)1342 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op1(struct sljit_compiler *compiler, sljit_s32 op,
1343 	sljit_s32 dst, sljit_sw dstw,
1344 	sljit_s32 src, sljit_sw srcw)
1345 {
1346 	sljit_s32 dst_r, flags;
1347 	sljit_s32 op_flags = GET_ALL_FLAGS(op);
1348 
1349 	CHECK_ERROR();
1350 	CHECK(check_sljit_emit_op1(compiler, op, dst, dstw, src, srcw));
1351 	ADJUST_LOCAL_OFFSET(dst, dstw);
1352 	ADJUST_LOCAL_OFFSET(src, srcw);
1353 
1354 	dst_r = SLOW_IS_REG(dst) ? dst : TMP_REG1;
1355 
1356 	op = GET_OPCODE(op);
1357 	if (op >= SLJIT_MOV && op <= SLJIT_MOV_P) {
1358 		switch (op) {
1359 		case SLJIT_MOV:
1360 		case SLJIT_MOV_U32:
1361 		case SLJIT_MOV_S32:
1362 		case SLJIT_MOV_P:
1363 			flags = WORD_SIZE;
1364 			break;
1365 		case SLJIT_MOV_U8:
1366 			flags = BYTE_SIZE;
1367 			if (src & SLJIT_IMM)
1368 				srcw = (sljit_u8)srcw;
1369 			break;
1370 		case SLJIT_MOV_S8:
1371 			flags = BYTE_SIZE | SIGNED;
1372 			if (src & SLJIT_IMM)
1373 				srcw = (sljit_s8)srcw;
1374 			break;
1375 		case SLJIT_MOV_U16:
1376 			flags = HALF_SIZE;
1377 			if (src & SLJIT_IMM)
1378 				srcw = (sljit_u16)srcw;
1379 			break;
1380 		case SLJIT_MOV_S16:
1381 			flags = HALF_SIZE | SIGNED;
1382 			if (src & SLJIT_IMM)
1383 				srcw = (sljit_s16)srcw;
1384 			break;
1385 		default:
1386 			SLJIT_UNREACHABLE();
1387 			flags = 0;
1388 			break;
1389 		}
1390 
1391 		if (src & SLJIT_IMM)
1392 			FAIL_IF(emit_op_imm(compiler, SLJIT_MOV | ARG2_IMM, dst_r, TMP_REG2, srcw));
1393 		else if (src & SLJIT_MEM) {
1394 			FAIL_IF(emit_op_mem(compiler, flags, dst_r, src, srcw, TMP_REG1));
1395 		} else {
1396 			if (dst_r != TMP_REG1)
1397 				return emit_op_imm(compiler, op, dst_r, TMP_REG2, src);
1398 			dst_r = src;
1399 		}
1400 
1401 		if (!(dst & SLJIT_MEM))
1402 			return SLJIT_SUCCESS;
1403 
1404 		return emit_op_mem(compiler, flags | STORE, dst_r, dst, dstw, TMP_REG2);
1405 	}
1406 
1407 	if (op == SLJIT_NEG) {
1408 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
1409 			|| (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
1410 		compiler->skip_checks = 1;
1411 #endif
1412 		return sljit_emit_op2(compiler, SLJIT_SUB | op_flags, dst, dstw, SLJIT_IMM, 0, src, srcw);
1413 	}
1414 
1415 	flags = HAS_FLAGS(op_flags) ? SET_FLAGS : 0;
1416 
1417 	if (src & SLJIT_MEM) {
1418 		FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG1, src, srcw, TMP_REG1));
1419 		src = TMP_REG1;
1420 	}
1421 
1422 	emit_op_imm(compiler, flags | op, dst_r, TMP_REG2, src);
1423 
1424 	if (SLJIT_UNLIKELY(dst & SLJIT_MEM))
1425 		return emit_op_mem(compiler, flags | STORE, dst_r, dst, dstw, TMP_REG2);
1426 	return SLJIT_SUCCESS;
1427 }
1428 
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)1429 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op2(struct sljit_compiler *compiler, sljit_s32 op,
1430 	sljit_s32 dst, sljit_sw dstw,
1431 	sljit_s32 src1, sljit_sw src1w,
1432 	sljit_s32 src2, sljit_sw src2w)
1433 {
1434 	sljit_s32 dst_reg, flags, src2_reg;
1435 
1436 	CHECK_ERROR();
1437 	CHECK(check_sljit_emit_op2(compiler, op, dst, dstw, src1, src1w, src2, src2w));
1438 	ADJUST_LOCAL_OFFSET(dst, dstw);
1439 	ADJUST_LOCAL_OFFSET(src1, src1w);
1440 	ADJUST_LOCAL_OFFSET(src2, src2w);
1441 
1442 	if (dst == SLJIT_UNUSED && !HAS_FLAGS(op))
1443 		return SLJIT_SUCCESS;
1444 
1445 	dst_reg = SLOW_IS_REG(dst) ? dst : TMP_REG1;
1446 	flags = HAS_FLAGS(op) ? SET_FLAGS : 0;
1447 
1448 	if (src1 & SLJIT_IMM)
1449 		flags |= ARG1_IMM;
1450 	else if (src1 & SLJIT_MEM) {
1451 		emit_op_mem(compiler, WORD_SIZE, TMP_REG1, src1, src1w, TMP_REG1);
1452 		src1w = TMP_REG1;
1453 	}
1454 	else
1455 		src1w = src1;
1456 
1457 	if (src2 & SLJIT_IMM)
1458 		flags |= ARG2_IMM;
1459 	else if (src2 & SLJIT_MEM) {
1460 		src2_reg = (!(flags & ARG1_IMM) && (src1w == TMP_REG1)) ? TMP_REG2 : TMP_REG1;
1461 		emit_op_mem(compiler, WORD_SIZE, src2_reg, src2, src2w, src2_reg);
1462 		src2w = src2_reg;
1463 	}
1464 	else
1465 		src2w = src2;
1466 
1467 	if (dst == SLJIT_UNUSED)
1468 		flags |= UNUSED_RETURN;
1469 
1470 	emit_op_imm(compiler, flags | GET_OPCODE(op), dst_reg, src1w, src2w);
1471 
1472 	if (!(dst & SLJIT_MEM))
1473 		return SLJIT_SUCCESS;
1474 	return emit_op_mem(compiler, WORD_SIZE | STORE, dst_reg, dst, dstw, TMP_REG2);
1475 }
1476 
sljit_emit_op_src(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 src,sljit_sw srcw)1477 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_src(struct sljit_compiler *compiler, sljit_s32 op,
1478 	sljit_s32 src, sljit_sw srcw)
1479 {
1480 	CHECK_ERROR();
1481 	CHECK(check_sljit_emit_op_src(compiler, op, src, srcw));
1482 	ADJUST_LOCAL_OFFSET(src, srcw);
1483 
1484 	switch (op) {
1485 	case SLJIT_FAST_RETURN:
1486 		SLJIT_ASSERT(reg_map[TMP_REG2] == 14);
1487 
1488 		if (FAST_IS_REG(src))
1489 			FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(TMP_REG2, src)));
1490 		else
1491 			FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG2, src, srcw, TMP_REG2));
1492 
1493 		return push_inst16(compiler, BX | RN3(TMP_REG2));
1494 	case SLJIT_SKIP_FRAMES_BEFORE_FAST_RETURN:
1495 		return SLJIT_SUCCESS;
1496 	case SLJIT_PREFETCH_L1:
1497 	case SLJIT_PREFETCH_L2:
1498 	case SLJIT_PREFETCH_L3:
1499 	case SLJIT_PREFETCH_ONCE:
1500 		return emit_op_mem(compiler, PRELOAD, TMP_PC, src, srcw, TMP_REG1);
1501 	}
1502 
1503 	return SLJIT_SUCCESS;
1504 }
1505 
sljit_get_register_index(sljit_s32 reg)1506 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_register_index(sljit_s32 reg)
1507 {
1508 	CHECK_REG_INDEX(check_sljit_get_register_index(reg));
1509 	return reg_map[reg];
1510 }
1511 
sljit_get_float_register_index(sljit_s32 reg)1512 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_float_register_index(sljit_s32 reg)
1513 {
1514 	CHECK_REG_INDEX(check_sljit_get_float_register_index(reg));
1515 	return (freg_map[reg] << 1);
1516 }
1517 
sljit_emit_op_custom(struct sljit_compiler * compiler,void * instruction,sljit_s32 size)1518 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_custom(struct sljit_compiler *compiler,
1519 	void *instruction, sljit_s32 size)
1520 {
1521 	CHECK_ERROR();
1522 	CHECK(check_sljit_emit_op_custom(compiler, instruction, size));
1523 
1524 	if (size == 2)
1525 		return push_inst16(compiler, *(sljit_u16*)instruction);
1526 	return push_inst32(compiler, *(sljit_ins*)instruction);
1527 }
1528 
1529 /* --------------------------------------------------------------------- */
1530 /*  Floating point operators                                             */
1531 /* --------------------------------------------------------------------- */
1532 
1533 #define FPU_LOAD (1 << 20)
1534 
emit_fop_mem(struct sljit_compiler * compiler,sljit_s32 flags,sljit_s32 reg,sljit_s32 arg,sljit_sw argw)1535 static sljit_s32 emit_fop_mem(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg, sljit_s32 arg, sljit_sw argw)
1536 {
1537 	sljit_uw imm;
1538 	sljit_sw inst = VSTR_F32 | (flags & (SLJIT_F32_OP | FPU_LOAD));
1539 
1540 	SLJIT_ASSERT(arg & SLJIT_MEM);
1541 
1542 	/* Fast loads and stores. */
1543 	if (SLJIT_UNLIKELY(arg & OFFS_REG_MASK)) {
1544 		FAIL_IF(push_inst32(compiler, ADD_W | RD4(TMP_REG1) | RN4(arg & REG_MASK) | RM4(OFFS_REG(arg)) | ((argw & 0x3) << 6)));
1545 		arg = SLJIT_MEM | TMP_REG1;
1546 		argw = 0;
1547 	}
1548 
1549 	if ((arg & REG_MASK) && (argw & 0x3) == 0) {
1550 		if (!(argw & ~0x3fc))
1551 			return push_inst32(compiler, inst | 0x800000 | RN4(arg & REG_MASK) | DD4(reg) | (argw >> 2));
1552 		if (!(-argw & ~0x3fc))
1553 			return push_inst32(compiler, inst | RN4(arg & REG_MASK) | DD4(reg) | (-argw >> 2));
1554 	}
1555 
1556 	if (arg & REG_MASK) {
1557 		if (emit_set_delta(compiler, TMP_REG1, arg & REG_MASK, argw) != SLJIT_ERR_UNSUPPORTED) {
1558 			FAIL_IF(compiler->error);
1559 			return push_inst32(compiler, inst | 0x800000 | RN4(TMP_REG1) | DD4(reg));
1560 		}
1561 		imm = get_imm(argw & ~0x3fc);
1562 		if (imm != INVALID_IMM) {
1563 			FAIL_IF(push_inst32(compiler, ADD_WI | RD4(TMP_REG1) | RN4(arg & REG_MASK) | imm));
1564 			return push_inst32(compiler, inst | 0x800000 | RN4(TMP_REG1) | DD4(reg) | ((argw & 0x3fc) >> 2));
1565 		}
1566 		imm = get_imm(-argw & ~0x3fc);
1567 		if (imm != INVALID_IMM) {
1568 			argw = -argw;
1569 			FAIL_IF(push_inst32(compiler, SUB_WI | RD4(TMP_REG1) | RN4(arg & REG_MASK) | imm));
1570 			return push_inst32(compiler, inst | RN4(TMP_REG1) | DD4(reg) | ((argw & 0x3fc) >> 2));
1571 		}
1572 	}
1573 
1574 	FAIL_IF(load_immediate(compiler, TMP_REG1, argw));
1575 	if (arg & REG_MASK)
1576 		FAIL_IF(push_inst16(compiler, ADD | SET_REGS44(TMP_REG1, (arg & REG_MASK))));
1577 	return push_inst32(compiler, inst | 0x800000 | RN4(TMP_REG1) | DD4(reg));
1578 }
1579 
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)1580 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_conv_sw_from_f64(struct sljit_compiler *compiler, sljit_s32 op,
1581 	sljit_s32 dst, sljit_sw dstw,
1582 	sljit_s32 src, sljit_sw srcw)
1583 {
1584 	op ^= SLJIT_F32_OP;
1585 
1586 	if (src & SLJIT_MEM) {
1587 		FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, TMP_FREG1, src, srcw));
1588 		src = TMP_FREG1;
1589 	}
1590 
1591 	FAIL_IF(push_inst32(compiler, VCVT_S32_F32 | (op & SLJIT_F32_OP) | DD4(TMP_FREG1) | DM4(src)));
1592 
1593 	if (FAST_IS_REG(dst))
1594 		return push_inst32(compiler, VMOV | (1 << 20) | RT4(dst) | DN4(TMP_FREG1));
1595 
1596 	/* Store the integer value from a VFP register. */
1597 	return emit_fop_mem(compiler, 0, TMP_FREG1, dst, dstw);
1598 }
1599 
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)1600 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_conv_f64_from_sw(struct sljit_compiler *compiler, sljit_s32 op,
1601 	sljit_s32 dst, sljit_sw dstw,
1602 	sljit_s32 src, sljit_sw srcw)
1603 {
1604 	sljit_s32 dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
1605 
1606 	op ^= SLJIT_F32_OP;
1607 
1608 	if (FAST_IS_REG(src))
1609 		FAIL_IF(push_inst32(compiler, VMOV | RT4(src) | DN4(TMP_FREG1)));
1610 	else if (src & SLJIT_MEM) {
1611 		/* Load the integer value into a VFP register. */
1612 		FAIL_IF(emit_fop_mem(compiler, FPU_LOAD, TMP_FREG1, src, srcw));
1613 	}
1614 	else {
1615 		FAIL_IF(load_immediate(compiler, TMP_REG1, srcw));
1616 		FAIL_IF(push_inst32(compiler, VMOV | RT4(TMP_REG1) | DN4(TMP_FREG1)));
1617 	}
1618 
1619 	FAIL_IF(push_inst32(compiler, VCVT_F32_S32 | (op & SLJIT_F32_OP) | DD4(dst_r) | DM4(TMP_FREG1)));
1620 
1621 	if (dst & SLJIT_MEM)
1622 		return emit_fop_mem(compiler, (op & SLJIT_F32_OP), TMP_FREG1, dst, dstw);
1623 	return SLJIT_SUCCESS;
1624 }
1625 
sljit_emit_fop1_cmp(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 src1,sljit_sw src1w,sljit_s32 src2,sljit_sw src2w)1626 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_cmp(struct sljit_compiler *compiler, sljit_s32 op,
1627 	sljit_s32 src1, sljit_sw src1w,
1628 	sljit_s32 src2, sljit_sw src2w)
1629 {
1630 	op ^= SLJIT_F32_OP;
1631 
1632 	if (src1 & SLJIT_MEM) {
1633 		emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, TMP_FREG1, src1, src1w);
1634 		src1 = TMP_FREG1;
1635 	}
1636 
1637 	if (src2 & SLJIT_MEM) {
1638 		emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, TMP_FREG2, src2, src2w);
1639 		src2 = TMP_FREG2;
1640 	}
1641 
1642 	FAIL_IF(push_inst32(compiler, VCMP_F32 | (op & SLJIT_F32_OP) | DD4(src1) | DM4(src2)));
1643 	return push_inst32(compiler, VMRS);
1644 }
1645 
sljit_emit_fop1(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 src,sljit_sw srcw)1646 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop1(struct sljit_compiler *compiler, sljit_s32 op,
1647 	sljit_s32 dst, sljit_sw dstw,
1648 	sljit_s32 src, sljit_sw srcw)
1649 {
1650 	sljit_s32 dst_r;
1651 
1652 	CHECK_ERROR();
1653 
1654 	SLJIT_COMPILE_ASSERT((SLJIT_F32_OP == 0x100), float_transfer_bit_error);
1655 	SELECT_FOP1_OPERATION_WITH_CHECKS(compiler, op, dst, dstw, src, srcw);
1656 
1657 	dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
1658 
1659 	if (GET_OPCODE(op) != SLJIT_CONV_F64_FROM_F32)
1660 		op ^= SLJIT_F32_OP;
1661 
1662 	if (src & SLJIT_MEM) {
1663 		emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, dst_r, src, srcw);
1664 		src = dst_r;
1665 	}
1666 
1667 	switch (GET_OPCODE(op)) {
1668 	case SLJIT_MOV_F64:
1669 		if (src != dst_r) {
1670 			if (dst_r != TMP_FREG1)
1671 				FAIL_IF(push_inst32(compiler, VMOV_F32 | (op & SLJIT_F32_OP) | DD4(dst_r) | DM4(src)));
1672 			else
1673 				dst_r = src;
1674 		}
1675 		break;
1676 	case SLJIT_NEG_F64:
1677 		FAIL_IF(push_inst32(compiler, VNEG_F32 | (op & SLJIT_F32_OP) | DD4(dst_r) | DM4(src)));
1678 		break;
1679 	case SLJIT_ABS_F64:
1680 		FAIL_IF(push_inst32(compiler, VABS_F32 | (op & SLJIT_F32_OP) | DD4(dst_r) | DM4(src)));
1681 		break;
1682 	case SLJIT_CONV_F64_FROM_F32:
1683 		FAIL_IF(push_inst32(compiler, VCVT_F64_F32 | (op & SLJIT_F32_OP) | DD4(dst_r) | DM4(src)));
1684 		op ^= SLJIT_F32_OP;
1685 		break;
1686 	}
1687 
1688 	if (dst & SLJIT_MEM)
1689 		return emit_fop_mem(compiler, (op & SLJIT_F32_OP), dst_r, dst, dstw);
1690 	return SLJIT_SUCCESS;
1691 }
1692 
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)1693 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop2(struct sljit_compiler *compiler, sljit_s32 op,
1694 	sljit_s32 dst, sljit_sw dstw,
1695 	sljit_s32 src1, sljit_sw src1w,
1696 	sljit_s32 src2, sljit_sw src2w)
1697 {
1698 	sljit_s32 dst_r;
1699 
1700 	CHECK_ERROR();
1701 	CHECK(check_sljit_emit_fop2(compiler, op, dst, dstw, src1, src1w, src2, src2w));
1702 	ADJUST_LOCAL_OFFSET(dst, dstw);
1703 	ADJUST_LOCAL_OFFSET(src1, src1w);
1704 	ADJUST_LOCAL_OFFSET(src2, src2w);
1705 
1706 	op ^= SLJIT_F32_OP;
1707 
1708 	dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
1709 	if (src1 & SLJIT_MEM) {
1710 		emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, TMP_FREG1, src1, src1w);
1711 		src1 = TMP_FREG1;
1712 	}
1713 	if (src2 & SLJIT_MEM) {
1714 		emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, TMP_FREG2, src2, src2w);
1715 		src2 = TMP_FREG2;
1716 	}
1717 
1718 	switch (GET_OPCODE(op)) {
1719 	case SLJIT_ADD_F64:
1720 		FAIL_IF(push_inst32(compiler, VADD_F32 | (op & SLJIT_F32_OP) | DD4(dst_r) | DN4(src1) | DM4(src2)));
1721 		break;
1722 	case SLJIT_SUB_F64:
1723 		FAIL_IF(push_inst32(compiler, VSUB_F32 | (op & SLJIT_F32_OP) | DD4(dst_r) | DN4(src1) | DM4(src2)));
1724 		break;
1725 	case SLJIT_MUL_F64:
1726 		FAIL_IF(push_inst32(compiler, VMUL_F32 | (op & SLJIT_F32_OP) | DD4(dst_r) | DN4(src1) | DM4(src2)));
1727 		break;
1728 	case SLJIT_DIV_F64:
1729 		FAIL_IF(push_inst32(compiler, VDIV_F32 | (op & SLJIT_F32_OP) | DD4(dst_r) | DN4(src1) | DM4(src2)));
1730 		break;
1731 	}
1732 
1733 	if (!(dst & SLJIT_MEM))
1734 		return SLJIT_SUCCESS;
1735 	return emit_fop_mem(compiler, (op & SLJIT_F32_OP), TMP_FREG1, dst, dstw);
1736 }
1737 
1738 #undef FPU_LOAD
1739 
1740 /* --------------------------------------------------------------------- */
1741 /*  Other instructions                                                   */
1742 /* --------------------------------------------------------------------- */
1743 
sljit_emit_fast_enter(struct sljit_compiler * compiler,sljit_s32 dst,sljit_sw dstw)1744 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fast_enter(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw)
1745 {
1746 	CHECK_ERROR();
1747 	CHECK(check_sljit_emit_fast_enter(compiler, dst, dstw));
1748 	ADJUST_LOCAL_OFFSET(dst, dstw);
1749 
1750 	SLJIT_ASSERT(reg_map[TMP_REG2] == 14);
1751 
1752 	if (FAST_IS_REG(dst))
1753 		return push_inst16(compiler, MOV | SET_REGS44(dst, TMP_REG2));
1754 
1755 	/* Memory. */
1756 	return emit_op_mem(compiler, WORD_SIZE | STORE, TMP_REG2, dst, dstw, TMP_REG1);
1757 }
1758 
1759 /* --------------------------------------------------------------------- */
1760 /*  Conditional instructions                                             */
1761 /* --------------------------------------------------------------------- */
1762 
get_cc(sljit_s32 type)1763 static sljit_uw get_cc(sljit_s32 type)
1764 {
1765 	switch (type) {
1766 	case SLJIT_EQUAL:
1767 	case SLJIT_MUL_NOT_OVERFLOW:
1768 	case SLJIT_EQUAL_F64:
1769 		return 0x0;
1770 
1771 	case SLJIT_NOT_EQUAL:
1772 	case SLJIT_MUL_OVERFLOW:
1773 	case SLJIT_NOT_EQUAL_F64:
1774 		return 0x1;
1775 
1776 	case SLJIT_LESS:
1777 	case SLJIT_LESS_F64:
1778 		return 0x3;
1779 
1780 	case SLJIT_GREATER_EQUAL:
1781 	case SLJIT_GREATER_EQUAL_F64:
1782 		return 0x2;
1783 
1784 	case SLJIT_GREATER:
1785 	case SLJIT_GREATER_F64:
1786 		return 0x8;
1787 
1788 	case SLJIT_LESS_EQUAL:
1789 	case SLJIT_LESS_EQUAL_F64:
1790 		return 0x9;
1791 
1792 	case SLJIT_SIG_LESS:
1793 		return 0xb;
1794 
1795 	case SLJIT_SIG_GREATER_EQUAL:
1796 		return 0xa;
1797 
1798 	case SLJIT_SIG_GREATER:
1799 		return 0xc;
1800 
1801 	case SLJIT_SIG_LESS_EQUAL:
1802 		return 0xd;
1803 
1804 	case SLJIT_OVERFLOW:
1805 	case SLJIT_UNORDERED_F64:
1806 		return 0x6;
1807 
1808 	case SLJIT_NOT_OVERFLOW:
1809 	case SLJIT_ORDERED_F64:
1810 		return 0x7;
1811 
1812 	default: /* SLJIT_JUMP */
1813 		SLJIT_UNREACHABLE();
1814 		return 0xe;
1815 	}
1816 }
1817 
sljit_emit_label(struct sljit_compiler * compiler)1818 SLJIT_API_FUNC_ATTRIBUTE struct sljit_label* sljit_emit_label(struct sljit_compiler *compiler)
1819 {
1820 	struct sljit_label *label;
1821 
1822 	CHECK_ERROR_PTR();
1823 	CHECK_PTR(check_sljit_emit_label(compiler));
1824 
1825 	if (compiler->last_label && compiler->last_label->size == compiler->size)
1826 		return compiler->last_label;
1827 
1828 	label = (struct sljit_label*)ensure_abuf(compiler, sizeof(struct sljit_label));
1829 	PTR_FAIL_IF(!label);
1830 	set_label(label, compiler);
1831 	return label;
1832 }
1833 
sljit_emit_jump(struct sljit_compiler * compiler,sljit_s32 type)1834 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_jump(struct sljit_compiler *compiler, sljit_s32 type)
1835 {
1836 	struct sljit_jump *jump;
1837 	sljit_ins cc;
1838 
1839 	CHECK_ERROR_PTR();
1840 	CHECK_PTR(check_sljit_emit_jump(compiler, type));
1841 
1842 	jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1843 	PTR_FAIL_IF(!jump);
1844 	set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
1845 	type &= 0xff;
1846 
1847 	PTR_FAIL_IF(emit_imm32_const(compiler, TMP_REG1, 0));
1848 	if (type < SLJIT_JUMP) {
1849 		jump->flags |= IS_COND;
1850 		cc = get_cc(type);
1851 		jump->flags |= cc << 8;
1852 		PTR_FAIL_IF(push_inst16(compiler, IT | (cc << 4) | 0x8));
1853 	}
1854 
1855 	jump->addr = compiler->size;
1856 	if (type <= SLJIT_JUMP)
1857 		PTR_FAIL_IF(push_inst16(compiler, BX | RN3(TMP_REG1)));
1858 	else {
1859 		jump->flags |= IS_BL;
1860 		PTR_FAIL_IF(push_inst16(compiler, BLX | RN3(TMP_REG1)));
1861 	}
1862 
1863 	return jump;
1864 }
1865 
1866 #ifdef __SOFTFP__
1867 
softfloat_call_with_args(struct sljit_compiler * compiler,sljit_s32 arg_types,sljit_s32 * src)1868 static sljit_s32 softfloat_call_with_args(struct sljit_compiler *compiler, sljit_s32 arg_types, sljit_s32 *src)
1869 {
1870 	sljit_s32 stack_offset = 0;
1871 	sljit_s32 arg_count = 0;
1872 	sljit_s32 word_arg_offset = 0;
1873 	sljit_s32 float_arg_count = 0;
1874 	sljit_s32 types = 0;
1875 	sljit_s32 src_offset = 4 * sizeof(sljit_sw);
1876 	sljit_u8 offsets[4];
1877 
1878 	if (src && FAST_IS_REG(*src))
1879 		src_offset = reg_map[*src] * sizeof(sljit_sw);
1880 
1881 	arg_types >>= SLJIT_DEF_SHIFT;
1882 
1883 	while (arg_types) {
1884 		types = (types << SLJIT_DEF_SHIFT) | (arg_types & SLJIT_DEF_MASK);
1885 
1886 		switch (arg_types & SLJIT_DEF_MASK) {
1887 		case SLJIT_ARG_TYPE_F32:
1888 			offsets[arg_count] = (sljit_u8)stack_offset;
1889 			stack_offset += sizeof(sljit_f32);
1890 			arg_count++;
1891 			float_arg_count++;
1892 			break;
1893 		case SLJIT_ARG_TYPE_F64:
1894 			if (stack_offset & 0x7)
1895 				stack_offset += sizeof(sljit_sw);
1896 			offsets[arg_count] = (sljit_u8)stack_offset;
1897 			stack_offset += sizeof(sljit_f64);
1898 			arg_count++;
1899 			float_arg_count++;
1900 			break;
1901 		default:
1902 			offsets[arg_count] = (sljit_u8)stack_offset;
1903 			stack_offset += sizeof(sljit_sw);
1904 			arg_count++;
1905 			word_arg_offset += sizeof(sljit_sw);
1906 			break;
1907 		}
1908 
1909 		arg_types >>= SLJIT_DEF_SHIFT;
1910 	}
1911 
1912 	if (stack_offset > 16)
1913 		FAIL_IF(push_inst16(compiler, SUB_SP | (((stack_offset - 16) + 0x7) & ~0x7) >> 2));
1914 
1915 	SLJIT_ASSERT(reg_map[TMP_REG1] == 12);
1916 
1917 	/* Process arguments in reversed direction. */
1918 	while (types) {
1919 		switch (types & SLJIT_DEF_MASK) {
1920 		case SLJIT_ARG_TYPE_F32:
1921 			arg_count--;
1922 			float_arg_count--;
1923 			stack_offset = offsets[arg_count];
1924 
1925 			if (stack_offset < 16) {
1926 				if (src_offset == stack_offset) {
1927 					FAIL_IF(push_inst16(compiler, MOV | (src_offset << 1) | 4 | (1 << 7)));
1928 					*src = TMP_REG1;
1929 				}
1930 				FAIL_IF(push_inst32(compiler, VMOV | 0x100000 | (float_arg_count << 16) | (stack_offset << 10)));
1931 			} else
1932 				FAIL_IF(push_inst32(compiler, VSTR_F32 | 0x800000 | RN4(SLJIT_SP) | (float_arg_count << 12) | ((stack_offset - 16) >> 2)));
1933 			break;
1934 		case SLJIT_ARG_TYPE_F64:
1935 			arg_count--;
1936 			float_arg_count--;
1937 			stack_offset = offsets[arg_count];
1938 
1939 			SLJIT_ASSERT((stack_offset & 0x7) == 0);
1940 
1941 			if (stack_offset < 16) {
1942 				if (src_offset == stack_offset || src_offset == stack_offset + sizeof(sljit_sw)) {
1943 					FAIL_IF(push_inst16(compiler, MOV | (src_offset << 1) | 4 | (1 << 7)));
1944 					*src = TMP_REG1;
1945 				}
1946 				FAIL_IF(push_inst32(compiler, VMOV2 | 0x100000 | (stack_offset << 10) | ((stack_offset + sizeof(sljit_sw)) << 14) | float_arg_count));
1947 			} else
1948 				FAIL_IF(push_inst32(compiler, VSTR_F32 | 0x800100 | RN4(SLJIT_SP) | (float_arg_count << 12) | ((stack_offset - 16) >> 2)));
1949 			break;
1950 		default:
1951 			arg_count--;
1952 			word_arg_offset -= sizeof(sljit_sw);
1953 			stack_offset = offsets[arg_count];
1954 
1955 			SLJIT_ASSERT(stack_offset >= word_arg_offset);
1956 
1957 			if (stack_offset != word_arg_offset) {
1958 				if (stack_offset < 16) {
1959 					if (src_offset == stack_offset) {
1960 						FAIL_IF(push_inst16(compiler, MOV | (src_offset << 1) | 4 | (1 << 7)));
1961 						*src = TMP_REG1;
1962 					}
1963 					else if (src_offset == word_arg_offset) {
1964 						*src = 1 + (stack_offset >> 2);
1965 						src_offset = stack_offset;
1966 					}
1967 					FAIL_IF(push_inst16(compiler, MOV | (stack_offset >> 2) | (word_arg_offset << 1)));
1968 				} else
1969 					FAIL_IF(push_inst16(compiler, STR_SP | (word_arg_offset << 6) | ((stack_offset - 16) >> 2)));
1970 			}
1971 			break;
1972 		}
1973 
1974 		types >>= SLJIT_DEF_SHIFT;
1975 	}
1976 
1977 	return SLJIT_SUCCESS;
1978 }
1979 
softfloat_post_call_with_args(struct sljit_compiler * compiler,sljit_s32 arg_types)1980 static sljit_s32 softfloat_post_call_with_args(struct sljit_compiler *compiler, sljit_s32 arg_types)
1981 {
1982 	sljit_s32 stack_size = 0;
1983 
1984 	if ((arg_types & SLJIT_DEF_MASK) == SLJIT_ARG_TYPE_F32)
1985 		FAIL_IF(push_inst32(compiler, VMOV | (0 << 16) | (0 << 12)));
1986 	if ((arg_types & SLJIT_DEF_MASK) == SLJIT_ARG_TYPE_F64)
1987 		FAIL_IF(push_inst32(compiler, VMOV2 | (1 << 16) | (0 << 12) | 0));
1988 
1989 	arg_types >>= SLJIT_DEF_SHIFT;
1990 
1991 	while (arg_types) {
1992 		switch (arg_types & SLJIT_DEF_MASK) {
1993 		case SLJIT_ARG_TYPE_F32:
1994 			stack_size += sizeof(sljit_f32);
1995 			break;
1996 		case SLJIT_ARG_TYPE_F64:
1997 			if (stack_size & 0x7)
1998 				stack_size += sizeof(sljit_sw);
1999 			stack_size += sizeof(sljit_f64);
2000 			break;
2001 		default:
2002 			stack_size += sizeof(sljit_sw);
2003 			break;
2004 		}
2005 
2006 		arg_types >>= SLJIT_DEF_SHIFT;
2007 	}
2008 
2009 	if (stack_size <= 16)
2010 		return SLJIT_SUCCESS;
2011 
2012 	return push_inst16(compiler, ADD_SP | ((((stack_size - 16) + 0x7) & ~0x7) >> 2));
2013 }
2014 
2015 #else
2016 
hardfloat_call_with_args(struct sljit_compiler * compiler,sljit_s32 arg_types)2017 static sljit_s32 hardfloat_call_with_args(struct sljit_compiler *compiler, sljit_s32 arg_types)
2018 {
2019 	sljit_u32 remap = 0;
2020 	sljit_u32 offset = 0;
2021 	sljit_u32 new_offset, mask;
2022 
2023 	/* Remove return value. */
2024 	arg_types >>= SLJIT_DEF_SHIFT;
2025 
2026 	while (arg_types) {
2027 		if ((arg_types & SLJIT_DEF_MASK) == SLJIT_ARG_TYPE_F32) {
2028 			new_offset = 0;
2029 			mask = 1;
2030 
2031 			while (remap & mask) {
2032 				new_offset++;
2033 				mask <<= 1;
2034 			}
2035 			remap |= mask;
2036 
2037 			if (offset != new_offset)
2038 				FAIL_IF(push_inst32(compiler, VMOV_F32 | DD4((new_offset >> 1) + 1)
2039 					| ((new_offset & 0x1) ? 0x400000 : 0) | DM4((offset >> 1) + 1)));
2040 
2041 			offset += 2;
2042 		}
2043 		else if ((arg_types & SLJIT_DEF_MASK) == SLJIT_ARG_TYPE_F64) {
2044 			new_offset = 0;
2045 			mask = 3;
2046 
2047 			while (remap & mask) {
2048 				new_offset += 2;
2049 				mask <<= 2;
2050 			}
2051 			remap |= mask;
2052 
2053 			if (offset != new_offset)
2054 				FAIL_IF(push_inst32(compiler, VMOV_F32 | SLJIT_F32_OP | DD4((new_offset >> 1) + 1) | DM4((offset >> 1) + 1)));
2055 
2056 			offset += 2;
2057 		}
2058 		arg_types >>= SLJIT_DEF_SHIFT;
2059 	}
2060 
2061 	return SLJIT_SUCCESS;
2062 }
2063 
2064 #endif
2065 
sljit_emit_call(struct sljit_compiler * compiler,sljit_s32 type,sljit_s32 arg_types)2066 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_call(struct sljit_compiler *compiler, sljit_s32 type,
2067 	sljit_s32 arg_types)
2068 {
2069 #ifdef __SOFTFP__
2070 	struct sljit_jump *jump;
2071 #endif
2072 
2073 	CHECK_ERROR_PTR();
2074 	CHECK_PTR(check_sljit_emit_call(compiler, type, arg_types));
2075 
2076 #ifdef __SOFTFP__
2077 	PTR_FAIL_IF(softfloat_call_with_args(compiler, arg_types, NULL));
2078 
2079 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2080 		|| (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2081 	compiler->skip_checks = 1;
2082 #endif
2083 
2084 	jump = sljit_emit_jump(compiler, type);
2085 	PTR_FAIL_IF(jump == NULL);
2086 
2087 	PTR_FAIL_IF(softfloat_post_call_with_args(compiler, arg_types));
2088 	return jump;
2089 #else
2090 	PTR_FAIL_IF(hardfloat_call_with_args(compiler, arg_types));
2091 
2092 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2093 		|| (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2094 	compiler->skip_checks = 1;
2095 #endif
2096 
2097 	return sljit_emit_jump(compiler, type);
2098 #endif
2099 }
2100 
sljit_emit_ijump(struct sljit_compiler * compiler,sljit_s32 type,sljit_s32 src,sljit_sw srcw)2101 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_ijump(struct sljit_compiler *compiler, sljit_s32 type, sljit_s32 src, sljit_sw srcw)
2102 {
2103 	struct sljit_jump *jump;
2104 
2105 	CHECK_ERROR();
2106 	CHECK(check_sljit_emit_ijump(compiler, type, src, srcw));
2107 	ADJUST_LOCAL_OFFSET(src, srcw);
2108 
2109 	SLJIT_ASSERT(reg_map[TMP_REG1] != 14);
2110 
2111 	if (!(src & SLJIT_IMM)) {
2112 		if (FAST_IS_REG(src)) {
2113 			SLJIT_ASSERT(reg_map[src] != 14);
2114 			return push_inst16(compiler, (type <= SLJIT_JUMP ? BX : BLX) | RN3(src));
2115 		}
2116 
2117 		FAIL_IF(emit_op_mem(compiler, WORD_SIZE, type <= SLJIT_JUMP ? TMP_PC : TMP_REG1, src, srcw, TMP_REG1));
2118 		if (type >= SLJIT_FAST_CALL)
2119 			return push_inst16(compiler, BLX | RN3(TMP_REG1));
2120 	}
2121 
2122 	/* These jumps are converted to jump/call instructions when possible. */
2123 	jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
2124 	FAIL_IF(!jump);
2125 	set_jump(jump, compiler, JUMP_ADDR | ((type >= SLJIT_FAST_CALL) ? IS_BL : 0));
2126 	jump->u.target = srcw;
2127 
2128 	FAIL_IF(emit_imm32_const(compiler, TMP_REG1, 0));
2129 	jump->addr = compiler->size;
2130 	return push_inst16(compiler, (type <= SLJIT_JUMP ? BX : BLX) | RN3(TMP_REG1));
2131 }
2132 
sljit_emit_icall(struct sljit_compiler * compiler,sljit_s32 type,sljit_s32 arg_types,sljit_s32 src,sljit_sw srcw)2133 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_icall(struct sljit_compiler *compiler, sljit_s32 type,
2134 	sljit_s32 arg_types,
2135 	sljit_s32 src, sljit_sw srcw)
2136 {
2137 	CHECK_ERROR();
2138 	CHECK(check_sljit_emit_icall(compiler, type, arg_types, src, srcw));
2139 
2140 #ifdef __SOFTFP__
2141 	if (src & SLJIT_MEM) {
2142 		FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG1, src, srcw, TMP_REG1));
2143 		src = TMP_REG1;
2144 	}
2145 
2146 	FAIL_IF(softfloat_call_with_args(compiler, arg_types, &src));
2147 
2148 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2149 		|| (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2150 	compiler->skip_checks = 1;
2151 #endif
2152 
2153 	FAIL_IF(sljit_emit_ijump(compiler, type, src, srcw));
2154 
2155 	return softfloat_post_call_with_args(compiler, arg_types);
2156 #else /* !__SOFTFP__ */
2157 	FAIL_IF(hardfloat_call_with_args(compiler, arg_types));
2158 
2159 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2160 		|| (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2161 	compiler->skip_checks = 1;
2162 #endif
2163 
2164 	return sljit_emit_ijump(compiler, type, src, srcw);
2165 #endif /* __SOFTFP__ */
2166 }
2167 
sljit_emit_op_flags(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 type)2168 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_flags(struct sljit_compiler *compiler, sljit_s32 op,
2169 	sljit_s32 dst, sljit_sw dstw,
2170 	sljit_s32 type)
2171 {
2172 	sljit_s32 dst_r, flags = GET_ALL_FLAGS(op);
2173 	sljit_ins cc;
2174 
2175 	CHECK_ERROR();
2176 	CHECK(check_sljit_emit_op_flags(compiler, op, dst, dstw, type));
2177 	ADJUST_LOCAL_OFFSET(dst, dstw);
2178 
2179 	op = GET_OPCODE(op);
2180 	cc = get_cc(type & 0xff);
2181 	dst_r = FAST_IS_REG(dst) ? dst : TMP_REG1;
2182 
2183 	if (op < SLJIT_ADD) {
2184 		FAIL_IF(push_inst16(compiler, IT | (cc << 4) | (((cc & 0x1) ^ 0x1) << 3) | 0x4));
2185 		if (reg_map[dst_r] > 7) {
2186 			FAIL_IF(push_inst32(compiler, MOV_WI | RD4(dst_r) | 1));
2187 			FAIL_IF(push_inst32(compiler, MOV_WI | RD4(dst_r) | 0));
2188 		} else {
2189 			/* The movsi (immediate) instruction does not set flags in IT block. */
2190 			FAIL_IF(push_inst16(compiler, MOVSI | RDN3(dst_r) | 1));
2191 			FAIL_IF(push_inst16(compiler, MOVSI | RDN3(dst_r) | 0));
2192 		}
2193 		if (!(dst & SLJIT_MEM))
2194 			return SLJIT_SUCCESS;
2195 		return emit_op_mem(compiler, WORD_SIZE | STORE, TMP_REG1, dst, dstw, TMP_REG2);
2196 	}
2197 
2198 	if (dst & SLJIT_MEM)
2199 		FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG1, dst, dstw, TMP_REG2));
2200 
2201 	if (op == SLJIT_AND) {
2202 		FAIL_IF(push_inst16(compiler, IT | (cc << 4) | (((cc & 0x1) ^ 0x1) << 3) | 0x4));
2203 		FAIL_IF(push_inst32(compiler, ANDI | RN4(dst_r) | RD4(dst_r) | 1));
2204 		FAIL_IF(push_inst32(compiler, ANDI | RN4(dst_r) | RD4(dst_r) | 0));
2205 	}
2206 	else {
2207 		FAIL_IF(push_inst16(compiler, IT | (cc << 4) | 0x8));
2208 		FAIL_IF(push_inst32(compiler, ((op == SLJIT_OR) ? ORRI : EORI) | RN4(dst_r) | RD4(dst_r) | 1));
2209 	}
2210 
2211 	if (dst & SLJIT_MEM)
2212 		FAIL_IF(emit_op_mem(compiler, WORD_SIZE | STORE, TMP_REG1, dst, dstw, TMP_REG2));
2213 
2214 	if (!(flags & SLJIT_SET_Z))
2215 		return SLJIT_SUCCESS;
2216 
2217 	/* The condition must always be set, even if the ORR/EORI is not executed above. */
2218 	return push_inst32(compiler, MOV_W | SET_FLAGS | RD4(TMP_REG1) | RM4(dst_r));
2219 }
2220 
sljit_emit_cmov(struct sljit_compiler * compiler,sljit_s32 type,sljit_s32 dst_reg,sljit_s32 src,sljit_sw srcw)2221 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_cmov(struct sljit_compiler *compiler, sljit_s32 type,
2222 	sljit_s32 dst_reg,
2223 	sljit_s32 src, sljit_sw srcw)
2224 {
2225 	sljit_uw cc, tmp;
2226 
2227 	CHECK_ERROR();
2228 	CHECK(check_sljit_emit_cmov(compiler, type, dst_reg, src, srcw));
2229 
2230 	dst_reg &= ~SLJIT_I32_OP;
2231 
2232 	cc = get_cc(type & 0xff);
2233 
2234 	if (!(src & SLJIT_IMM)) {
2235 		FAIL_IF(push_inst16(compiler, IT | (cc << 4) | 0x8));
2236 		return push_inst16(compiler, MOV | SET_REGS44(dst_reg, src));
2237 	}
2238 
2239 	tmp = (sljit_uw) srcw;
2240 
2241 	if (tmp < 0x10000) {
2242 		/* set low 16 bits, set hi 16 bits to 0. */
2243 		FAIL_IF(push_inst16(compiler, IT | (cc << 4) | 0x8));
2244 		return push_inst32(compiler, MOVW | RD4(dst_reg)
2245 			| COPY_BITS(tmp, 12, 16, 4) | COPY_BITS(tmp, 11, 26, 1) | COPY_BITS(tmp, 8, 12, 3) | (tmp & 0xff));
2246 	}
2247 
2248 	tmp = get_imm(srcw);
2249 	if (tmp != INVALID_IMM) {
2250 		FAIL_IF(push_inst16(compiler, IT | (cc << 4) | 0x8));
2251 		return push_inst32(compiler, MOV_WI | RD4(dst_reg) | tmp);
2252 	}
2253 
2254 	tmp = get_imm(~srcw);
2255 	if (tmp != INVALID_IMM) {
2256 		FAIL_IF(push_inst16(compiler, IT | (cc << 4) | 0x8));
2257 		return push_inst32(compiler, MVN_WI | RD4(dst_reg) | tmp);
2258 	}
2259 
2260 	FAIL_IF(push_inst16(compiler, IT | (cc << 4) | ((cc & 0x1) << 3) | 0x4));
2261 
2262 	tmp = (sljit_uw) srcw;
2263 	FAIL_IF(push_inst32(compiler, MOVW | RD4(dst_reg)
2264 		| COPY_BITS(tmp, 12, 16, 4) | COPY_BITS(tmp, 11, 26, 1) | COPY_BITS(tmp, 8, 12, 3) | (tmp & 0xff)));
2265 	return push_inst32(compiler, MOVT | RD4(dst_reg)
2266 		| COPY_BITS(tmp, 12 + 16, 16, 4) | COPY_BITS(tmp, 11 + 16, 26, 1) | COPY_BITS(tmp, 8 + 16, 12, 3) | ((tmp & 0xff0000) >> 16));
2267 }
2268 
sljit_emit_mem(struct sljit_compiler * compiler,sljit_s32 type,sljit_s32 reg,sljit_s32 mem,sljit_sw memw)2269 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_mem(struct sljit_compiler *compiler, sljit_s32 type,
2270 	sljit_s32 reg,
2271 	sljit_s32 mem, sljit_sw memw)
2272 {
2273 	sljit_s32 flags;
2274 	sljit_ins inst;
2275 
2276 	CHECK_ERROR();
2277 	CHECK(check_sljit_emit_mem(compiler, type, reg, mem, memw));
2278 
2279 	if ((mem & OFFS_REG_MASK) || (memw > 255 || memw < -255))
2280 		return SLJIT_ERR_UNSUPPORTED;
2281 
2282 	if (type & SLJIT_MEM_SUPP)
2283 		return SLJIT_SUCCESS;
2284 
2285 	switch (type & 0xff) {
2286 	case SLJIT_MOV:
2287 	case SLJIT_MOV_U32:
2288 	case SLJIT_MOV_S32:
2289 	case SLJIT_MOV_P:
2290 		flags = WORD_SIZE;
2291 		break;
2292 	case SLJIT_MOV_U8:
2293 		flags = BYTE_SIZE;
2294 		break;
2295 	case SLJIT_MOV_S8:
2296 		flags = BYTE_SIZE | SIGNED;
2297 		break;
2298 	case SLJIT_MOV_U16:
2299 		flags = HALF_SIZE;
2300 		break;
2301 	case SLJIT_MOV_S16:
2302 		flags = HALF_SIZE | SIGNED;
2303 		break;
2304 	default:
2305 		SLJIT_UNREACHABLE();
2306 		flags = WORD_SIZE;
2307 		break;
2308 	}
2309 
2310 	if (type & SLJIT_MEM_STORE)
2311 		flags |= STORE;
2312 
2313 	inst = sljit_mem32[flags] | 0x900;
2314 
2315 	if (type & SLJIT_MEM_PRE)
2316 		inst |= 0x400;
2317 
2318 	if (memw >= 0)
2319 		inst |= 0x200;
2320 	else
2321 		memw = -memw;
2322 
2323 	return push_inst32(compiler, inst | RT4(reg) | RN4(mem & REG_MASK) | memw);
2324 }
2325 
sljit_emit_const(struct sljit_compiler * compiler,sljit_s32 dst,sljit_sw dstw,sljit_sw init_value)2326 SLJIT_API_FUNC_ATTRIBUTE struct sljit_const* sljit_emit_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw, sljit_sw init_value)
2327 {
2328 	struct sljit_const *const_;
2329 	sljit_s32 dst_r;
2330 
2331 	CHECK_ERROR_PTR();
2332 	CHECK_PTR(check_sljit_emit_const(compiler, dst, dstw, init_value));
2333 	ADJUST_LOCAL_OFFSET(dst, dstw);
2334 
2335 	const_ = (struct sljit_const*)ensure_abuf(compiler, sizeof(struct sljit_const));
2336 	PTR_FAIL_IF(!const_);
2337 	set_const(const_, compiler);
2338 
2339 	dst_r = FAST_IS_REG(dst) ? dst : TMP_REG1;
2340 	PTR_FAIL_IF(emit_imm32_const(compiler, dst_r, init_value));
2341 
2342 	if (dst & SLJIT_MEM)
2343 		PTR_FAIL_IF(emit_op_mem(compiler, WORD_SIZE | STORE, dst_r, dst, dstw, TMP_REG2));
2344 	return const_;
2345 }
2346 
sljit_emit_put_label(struct sljit_compiler * compiler,sljit_s32 dst,sljit_sw dstw)2347 SLJIT_API_FUNC_ATTRIBUTE struct sljit_put_label* sljit_emit_put_label(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw)
2348 {
2349 	struct sljit_put_label *put_label;
2350 	sljit_s32 dst_r;
2351 
2352 	CHECK_ERROR_PTR();
2353 	CHECK_PTR(check_sljit_emit_put_label(compiler, dst, dstw));
2354 	ADJUST_LOCAL_OFFSET(dst, dstw);
2355 
2356 	put_label = (struct sljit_put_label*)ensure_abuf(compiler, sizeof(struct sljit_put_label));
2357 	PTR_FAIL_IF(!put_label);
2358 	set_put_label(put_label, compiler, 0);
2359 
2360 	dst_r = FAST_IS_REG(dst) ? dst : TMP_REG1;
2361 	PTR_FAIL_IF(emit_imm32_const(compiler, dst_r, 0));
2362 
2363 	if (dst & SLJIT_MEM)
2364 		PTR_FAIL_IF(emit_op_mem(compiler, WORD_SIZE | STORE, dst_r, dst, dstw, TMP_REG2));
2365 	return put_label;
2366 }
2367 
sljit_set_jump_addr(sljit_uw addr,sljit_uw new_target,sljit_sw executable_offset)2368 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_target, sljit_sw executable_offset)
2369 {
2370 	sljit_u16 *inst = (sljit_u16*)addr;
2371 	SLJIT_UNUSED_ARG(executable_offset);
2372 
2373 	SLJIT_UPDATE_WX_FLAGS(inst, inst + 4, 0);
2374 	modify_imm32_const(inst, new_target);
2375 	SLJIT_UPDATE_WX_FLAGS(inst, inst + 4, 1);
2376 	inst = (sljit_u16 *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
2377 	SLJIT_CACHE_FLUSH(inst, inst + 4);
2378 }
2379 
sljit_set_const(sljit_uw addr,sljit_sw new_constant,sljit_sw executable_offset)2380 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_const(sljit_uw addr, sljit_sw new_constant, sljit_sw executable_offset)
2381 {
2382 	sljit_set_jump_addr(addr, new_constant, executable_offset);
2383 }
2384