1 /*
2  *    Stack-less Just-In-Time compiler
3  *
4  *    Copyright Zoltan Herczeg (hzmester@freemail.hu). All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without modification, are
7  * permitted provided that the following conditions are met:
8  *
9  *   1. Redistributions of source code must retain the above copyright notice, this list of
10  *      conditions and the following disclaimer.
11  *
12  *   2. Redistributions in binary form must reproduce the above copyright notice, this list
13  *      of conditions and the following disclaimer in the documentation and/or other materials
14  *      provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) AND CONTRIBUTORS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19  * SHALL THE COPYRIGHT HOLDER(S) OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
22  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
sljit_get_platform_name(void)27 SLJIT_API_FUNC_ATTRIBUTE const char* sljit_get_platform_name(void)
28 {
29 	return "ARM-64" SLJIT_CPUINFO;
30 }
31 
32 /* Length of an instruction word */
33 typedef sljit_u32 sljit_ins;
34 
35 #define TMP_ZERO	(0)
36 
37 #define TMP_REG1	(SLJIT_NUMBER_OF_REGISTERS + 2)
38 #define TMP_REG2	(SLJIT_NUMBER_OF_REGISTERS + 3)
39 #define TMP_REG3	(SLJIT_NUMBER_OF_REGISTERS + 4)
40 #define TMP_LR		(SLJIT_NUMBER_OF_REGISTERS + 5)
41 #define TMP_SP		(SLJIT_NUMBER_OF_REGISTERS + 6)
42 
43 #define TMP_FREG1	(0)
44 #define TMP_FREG2	(SLJIT_NUMBER_OF_FLOAT_REGISTERS + 1)
45 
46 static const sljit_u8 reg_map[SLJIT_NUMBER_OF_REGISTERS + 8] = {
47   31, 0, 1, 2, 3, 4, 5, 6, 7, 12, 13, 14, 15, 16, 17, 8, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 29, 9, 10, 11, 30, 31
48 };
49 
50 #define W_OP (1 << 31)
51 #define RD(rd) (reg_map[rd])
52 #define RT(rt) (reg_map[rt])
53 #define RN(rn) (reg_map[rn] << 5)
54 #define RT2(rt2) (reg_map[rt2] << 10)
55 #define RM(rm) (reg_map[rm] << 16)
56 #define VD(vd) (vd)
57 #define VT(vt) (vt)
58 #define VN(vn) ((vn) << 5)
59 #define VM(vm) ((vm) << 16)
60 
61 /* --------------------------------------------------------------------- */
62 /*  Instrucion forms                                                     */
63 /* --------------------------------------------------------------------- */
64 
65 #define ADC 0x9a000000
66 #define ADD 0x8b000000
67 #define ADDI 0x91000000
68 #define AND 0x8a000000
69 #define ANDI 0x92000000
70 #define ASRV 0x9ac02800
71 #define B 0x14000000
72 #define B_CC 0x54000000
73 #define BL 0x94000000
74 #define BLR 0xd63f0000
75 #define BR 0xd61f0000
76 #define BRK 0xd4200000
77 #define CBZ 0xb4000000
78 #define CLZ 0xdac01000
79 #define CSEL 0x9a800000
80 #define CSINC 0x9a800400
81 #define EOR 0xca000000
82 #define EORI 0xd2000000
83 #define FABS 0x1e60c000
84 #define FADD 0x1e602800
85 #define FCMP 0x1e602000
86 #define FCVT 0x1e224000
87 #define FCVTZS 0x9e780000
88 #define FDIV 0x1e601800
89 #define FMOV 0x1e604000
90 #define FMUL 0x1e600800
91 #define FNEG 0x1e614000
92 #define FSUB 0x1e603800
93 #define LDRI 0xf9400000
94 #define LDP 0xa9400000
95 #define LDP_PST 0xa8c00000
96 #define LSLV 0x9ac02000
97 #define LSRV 0x9ac02400
98 #define MADD 0x9b000000
99 #define MOVK 0xf2800000
100 #define MOVN 0x92800000
101 #define MOVZ 0xd2800000
102 #define NOP 0xd503201f
103 #define ORN 0xaa200000
104 #define ORR 0xaa000000
105 #define ORRI 0xb2000000
106 #define RET 0xd65f0000
107 #define SBC 0xda000000
108 #define SBFM 0x93000000
109 #define SCVTF 0x9e620000
110 #define SDIV 0x9ac00c00
111 #define SMADDL 0x9b200000
112 #define SMULH 0x9b403c00
113 #define STP 0xa9000000
114 #define STP_PRE 0xa9800000
115 #define STRI 0xf9000000
116 #define STR_FI 0x3d000000
117 #define STR_FR 0x3c206800
118 #define STUR_FI 0x3c000000
119 #define SUB 0xcb000000
120 #define SUBI 0xd1000000
121 #define SUBS 0xeb000000
122 #define UBFM 0xd3000000
123 #define UDIV 0x9ac00800
124 #define UMULH 0x9bc03c00
125 
126 /* dest_reg is the absolute name of the register
127    Useful for reordering instructions in the delay slot. */
push_inst(struct sljit_compiler * compiler,sljit_ins ins)128 static sljit_s32 push_inst(struct sljit_compiler *compiler, sljit_ins ins)
129 {
130 	sljit_ins *ptr = (sljit_ins*)ensure_buf(compiler, sizeof(sljit_ins));
131 	FAIL_IF(!ptr);
132 	*ptr = ins;
133 	compiler->size++;
134 	return SLJIT_SUCCESS;
135 }
136 
emit_imm64_const(struct sljit_compiler * compiler,sljit_s32 dst,sljit_uw imm)137 static SLJIT_INLINE sljit_s32 emit_imm64_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_uw imm)
138 {
139 	FAIL_IF(push_inst(compiler, MOVZ | RD(dst) | ((imm & 0xffff) << 5)));
140 	FAIL_IF(push_inst(compiler, MOVK | RD(dst) | (((imm >> 16) & 0xffff) << 5) | (1 << 21)));
141 	FAIL_IF(push_inst(compiler, MOVK | RD(dst) | (((imm >> 32) & 0xffff) << 5) | (2 << 21)));
142 	return push_inst(compiler, MOVK | RD(dst) | ((imm >> 48) << 5) | (3 << 21));
143 }
144 
modify_imm64_const(sljit_ins * inst,sljit_uw new_imm)145 static SLJIT_INLINE void modify_imm64_const(sljit_ins* inst, sljit_uw new_imm)
146 {
147 	sljit_s32 dst = inst[0] & 0x1f;
148 	SLJIT_ASSERT((inst[0] & 0xffe00000) == MOVZ && (inst[1] & 0xffe00000) == (MOVK | (1 << 21)));
149 	inst[0] = MOVZ | dst | ((new_imm & 0xffff) << 5);
150 	inst[1] = MOVK | dst | (((new_imm >> 16) & 0xffff) << 5) | (1 << 21);
151 	inst[2] = MOVK | dst | (((new_imm >> 32) & 0xffff) << 5) | (2 << 21);
152 	inst[3] = MOVK | dst | ((new_imm >> 48) << 5) | (3 << 21);
153 }
154 
detect_jump_type(struct sljit_jump * jump,sljit_ins * code_ptr,sljit_ins * code,sljit_sw executable_offset)155 static SLJIT_INLINE sljit_s32 detect_jump_type(struct sljit_jump *jump, sljit_ins *code_ptr, sljit_ins *code, sljit_sw executable_offset)
156 {
157 	sljit_sw diff;
158 	sljit_uw target_addr;
159 
160 	if (jump->flags & SLJIT_REWRITABLE_JUMP) {
161 		jump->flags |= PATCH_ABS64;
162 		return 0;
163 	}
164 
165 	if (jump->flags & JUMP_ADDR)
166 		target_addr = jump->u.target;
167 	else {
168 		SLJIT_ASSERT(jump->flags & JUMP_LABEL);
169 		target_addr = (sljit_uw)(code + jump->u.label->size) + (sljit_uw)executable_offset;
170 	}
171 
172 	diff = (sljit_sw)target_addr - (sljit_sw)(code_ptr + 4) - executable_offset;
173 
174 	if (jump->flags & IS_COND) {
175 		diff += sizeof(sljit_ins);
176 		if (diff <= 0xfffff && diff >= -0x100000) {
177 			code_ptr[-5] ^= (jump->flags & IS_CBZ) ? (0x1 << 24) : 0x1;
178 			jump->addr -= sizeof(sljit_ins);
179 			jump->flags |= PATCH_COND;
180 			return 5;
181 		}
182 		diff -= sizeof(sljit_ins);
183 	}
184 
185 	if (diff <= 0x7ffffff && diff >= -0x8000000) {
186 		jump->flags |= PATCH_B;
187 		return 4;
188 	}
189 
190 	if (target_addr <= 0xffffffffl) {
191 		if (jump->flags & IS_COND)
192 			code_ptr[-5] -= (2 << 5);
193 		code_ptr[-2] = code_ptr[0];
194 		return 2;
195 	}
196 	if (target_addr <= 0xffffffffffffl) {
197 		if (jump->flags & IS_COND)
198 			code_ptr[-5] -= (1 << 5);
199 		jump->flags |= PATCH_ABS48;
200 		code_ptr[-1] = code_ptr[0];
201 		return 1;
202 	}
203 
204 	jump->flags |= PATCH_ABS64;
205 	return 0;
206 }
207 
sljit_generate_code(struct sljit_compiler * compiler)208 SLJIT_API_FUNC_ATTRIBUTE void* sljit_generate_code(struct sljit_compiler *compiler)
209 {
210 	struct sljit_memory_fragment *buf;
211 	sljit_ins *code;
212 	sljit_ins *code_ptr;
213 	sljit_ins *buf_ptr;
214 	sljit_ins *buf_end;
215 	sljit_uw word_count;
216 	sljit_sw executable_offset;
217 	sljit_uw addr;
218 	sljit_s32 dst;
219 
220 	struct sljit_label *label;
221 	struct sljit_jump *jump;
222 	struct sljit_const *const_;
223 
224 	CHECK_ERROR_PTR();
225 	CHECK_PTR(check_sljit_generate_code(compiler));
226 	reverse_buf(compiler);
227 
228 	code = (sljit_ins*)SLJIT_MALLOC_EXEC(compiler->size * sizeof(sljit_ins));
229 	PTR_FAIL_WITH_EXEC_IF(code);
230 	buf = compiler->buf;
231 
232 	code_ptr = code;
233 	word_count = 0;
234 	executable_offset = SLJIT_EXEC_OFFSET(code);
235 
236 	label = compiler->labels;
237 	jump = compiler->jumps;
238 	const_ = compiler->consts;
239 
240 	do {
241 		buf_ptr = (sljit_ins*)buf->memory;
242 		buf_end = buf_ptr + (buf->used_size >> 2);
243 		do {
244 			*code_ptr = *buf_ptr++;
245 			/* These structures are ordered by their address. */
246 			SLJIT_ASSERT(!label || label->size >= word_count);
247 			SLJIT_ASSERT(!jump || jump->addr >= word_count);
248 			SLJIT_ASSERT(!const_ || const_->addr >= word_count);
249 			if (label && label->size == word_count) {
250 				label->addr = (sljit_uw)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset);
251 				label->size = code_ptr - code;
252 				label = label->next;
253 			}
254 			if (jump && jump->addr == word_count) {
255 					jump->addr = (sljit_uw)(code_ptr - 4);
256 					code_ptr -= detect_jump_type(jump, code_ptr, code, executable_offset);
257 					jump = jump->next;
258 			}
259 			if (const_ && const_->addr == word_count) {
260 				const_->addr = (sljit_uw)code_ptr;
261 				const_ = const_->next;
262 			}
263 			code_ptr ++;
264 			word_count ++;
265 		} while (buf_ptr < buf_end);
266 
267 		buf = buf->next;
268 	} while (buf);
269 
270 	if (label && label->size == word_count) {
271 		label->addr = (sljit_uw)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset);
272 		label->size = code_ptr - code;
273 		label = label->next;
274 	}
275 
276 	SLJIT_ASSERT(!label);
277 	SLJIT_ASSERT(!jump);
278 	SLJIT_ASSERT(!const_);
279 	SLJIT_ASSERT(code_ptr - code <= (sljit_sw)compiler->size);
280 
281 	jump = compiler->jumps;
282 	while (jump) {
283 		do {
284 			addr = (jump->flags & JUMP_LABEL) ? jump->u.label->addr : jump->u.target;
285 			buf_ptr = (sljit_ins *)jump->addr;
286 
287 			if (jump->flags & PATCH_B) {
288 				addr = (sljit_sw)(addr - (sljit_uw)SLJIT_ADD_EXEC_OFFSET(buf_ptr, executable_offset)) >> 2;
289 				SLJIT_ASSERT((sljit_sw)addr <= 0x1ffffff && (sljit_sw)addr >= -0x2000000);
290 				buf_ptr[0] = ((jump->flags & IS_BL) ? BL : B) | (addr & 0x3ffffff);
291 				if (jump->flags & IS_COND)
292 					buf_ptr[-1] -= (4 << 5);
293 				break;
294 			}
295 			if (jump->flags & PATCH_COND) {
296 				addr = (sljit_sw)(addr - (sljit_uw)SLJIT_ADD_EXEC_OFFSET(buf_ptr, executable_offset)) >> 2;
297 				SLJIT_ASSERT((sljit_sw)addr <= 0x3ffff && (sljit_sw)addr >= -0x40000);
298 				buf_ptr[0] = (buf_ptr[0] & ~0xffffe0) | ((addr & 0x7ffff) << 5);
299 				break;
300 			}
301 
302 			SLJIT_ASSERT((jump->flags & (PATCH_ABS48 | PATCH_ABS64)) || addr <= 0xffffffffl);
303 			SLJIT_ASSERT((jump->flags & PATCH_ABS64) || addr <= 0xffffffffffffl);
304 
305 			dst = buf_ptr[0] & 0x1f;
306 			buf_ptr[0] = MOVZ | dst | ((addr & 0xffff) << 5);
307 			buf_ptr[1] = MOVK | dst | (((addr >> 16) & 0xffff) << 5) | (1 << 21);
308 			if (jump->flags & (PATCH_ABS48 | PATCH_ABS64))
309 				buf_ptr[2] = MOVK | dst | (((addr >> 32) & 0xffff) << 5) | (2 << 21);
310 			if (jump->flags & PATCH_ABS64)
311 				buf_ptr[3] = MOVK | dst | (((addr >> 48) & 0xffff) << 5) | (3 << 21);
312 		} while (0);
313 		jump = jump->next;
314 	}
315 
316 	compiler->error = SLJIT_ERR_COMPILED;
317 	compiler->executable_offset = executable_offset;
318 	compiler->executable_size = (code_ptr - code) * sizeof(sljit_ins);
319 
320 	code = (sljit_ins *)SLJIT_ADD_EXEC_OFFSET(code, executable_offset);
321 	code_ptr = (sljit_ins *)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset);
322 
323 	SLJIT_CACHE_FLUSH(code, code_ptr);
324 	return code;
325 }
326 
sljit_has_cpu_feature(sljit_s32 feature_type)327 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_has_cpu_feature(sljit_s32 feature_type)
328 {
329 	switch (feature_type) {
330 	case SLJIT_HAS_FPU:
331 #ifdef SLJIT_IS_FPU_AVAILABLE
332 		return SLJIT_IS_FPU_AVAILABLE;
333 #else
334 		/* Available by default. */
335 		return 1;
336 #endif
337 
338 	case SLJIT_HAS_PRE_UPDATE:
339 	case SLJIT_HAS_CLZ:
340 	case SLJIT_HAS_CMOV:
341 		return 1;
342 
343 	default:
344 		return 0;
345 	}
346 }
347 
348 /* --------------------------------------------------------------------- */
349 /*  Core code generator functions.                                       */
350 /* --------------------------------------------------------------------- */
351 
352 #define COUNT_TRAILING_ZERO(value, result) \
353 	result = 0; \
354 	if (!(value & 0xffffffff)) { \
355 		result += 32; \
356 		value >>= 32; \
357 	} \
358 	if (!(value & 0xffff)) { \
359 		result += 16; \
360 		value >>= 16; \
361 	} \
362 	if (!(value & 0xff)) { \
363 		result += 8; \
364 		value >>= 8; \
365 	} \
366 	if (!(value & 0xf)) { \
367 		result += 4; \
368 		value >>= 4; \
369 	} \
370 	if (!(value & 0x3)) { \
371 		result += 2; \
372 		value >>= 2; \
373 	} \
374 	if (!(value & 0x1)) { \
375 		result += 1; \
376 		value >>= 1; \
377 	}
378 
379 #define LOGICAL_IMM_CHECK 0x100
380 
logical_imm(sljit_sw imm,sljit_s32 len)381 static sljit_ins logical_imm(sljit_sw imm, sljit_s32 len)
382 {
383 	sljit_s32 negated, ones, right;
384 	sljit_uw mask, uimm;
385 	sljit_ins ins;
386 
387 	if (len & LOGICAL_IMM_CHECK) {
388 		len &= ~LOGICAL_IMM_CHECK;
389 		if (len == 32 && (imm == 0 || imm == -1))
390 			return 0;
391 		if (len == 16 && ((sljit_s32)imm == 0 || (sljit_s32)imm == -1))
392 			return 0;
393 	}
394 
395 	SLJIT_ASSERT((len == 32 && imm != 0 && imm != -1)
396 		|| (len == 16 && (sljit_s32)imm != 0 && (sljit_s32)imm != -1));
397 	uimm = (sljit_uw)imm;
398 	while (1) {
399 		if (len <= 0) {
400 			SLJIT_UNREACHABLE();
401 			return 0;
402 		}
403 		mask = ((sljit_uw)1 << len) - 1;
404 		if ((uimm & mask) != ((uimm >> len) & mask))
405 			break;
406 		len >>= 1;
407 	}
408 
409 	len <<= 1;
410 
411 	negated = 0;
412 	if (uimm & 0x1) {
413 		negated = 1;
414 		uimm = ~uimm;
415 	}
416 
417 	if (len < 64)
418 		uimm &= ((sljit_uw)1 << len) - 1;
419 
420 	/* Unsigned right shift. */
421 	COUNT_TRAILING_ZERO(uimm, right);
422 
423 	/* Signed shift. We also know that the highest bit is set. */
424 	imm = (sljit_sw)~uimm;
425 	SLJIT_ASSERT(imm < 0);
426 
427 	COUNT_TRAILING_ZERO(imm, ones);
428 
429 	if (~imm)
430 		return 0;
431 
432 	if (len == 64)
433 		ins = 1 << 22;
434 	else
435 		ins = (0x3f - ((len << 1) - 1)) << 10;
436 
437 	if (negated)
438 		return ins | ((len - ones - 1) << 10) | ((len - ones - right) << 16);
439 
440 	return ins | ((ones - 1) << 10) | ((len - right) << 16);
441 }
442 
443 #undef COUNT_TRAILING_ZERO
444 
load_immediate(struct sljit_compiler * compiler,sljit_s32 dst,sljit_sw simm)445 static sljit_s32 load_immediate(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw simm)
446 {
447 	sljit_uw imm = (sljit_uw)simm;
448 	sljit_s32 i, zeros, ones, first;
449 	sljit_ins bitmask;
450 
451 	if (imm <= 0xffff)
452 		return push_inst(compiler, MOVZ | RD(dst) | (imm << 5));
453 
454 	if (simm >= -0x10000 && simm < 0)
455 		return push_inst(compiler, MOVN | RD(dst) | ((~imm & 0xffff) << 5));
456 
457 	if (imm <= 0xffffffffl) {
458 		if ((imm & 0xffff0000l) == 0xffff0000)
459 			return push_inst(compiler, (MOVN ^ W_OP) | RD(dst) | ((~imm & 0xffff) << 5));
460 		if ((imm & 0xffff) == 0xffff)
461 			return push_inst(compiler, (MOVN ^ W_OP) | RD(dst) | ((~imm & 0xffff0000l) >> (16 - 5)) | (1 << 21));
462 		bitmask = logical_imm(simm, 16);
463 		if (bitmask != 0)
464 			return push_inst(compiler, (ORRI ^ W_OP) | RD(dst) | RN(TMP_ZERO) | bitmask);
465 	}
466 	else {
467 		bitmask = logical_imm(simm, 32);
468 		if (bitmask != 0)
469 			return push_inst(compiler, ORRI | RD(dst) | RN(TMP_ZERO) | bitmask);
470 	}
471 
472 	if (imm <= 0xffffffffl) {
473 		FAIL_IF(push_inst(compiler, MOVZ | RD(dst) | ((imm & 0xffff) << 5)));
474 		return push_inst(compiler, MOVK | RD(dst) | ((imm & 0xffff0000l) >> (16 - 5)) | (1 << 21));
475 	}
476 
477 	if (simm >= -0x100000000l && simm < 0) {
478 		FAIL_IF(push_inst(compiler, MOVN | RD(dst) | ((~imm & 0xffff) << 5)));
479 		return push_inst(compiler, MOVK | RD(dst) | ((imm & 0xffff0000l) >> (16 - 5)) | (1 << 21));
480 	}
481 
482 	/* A large amount of number can be constructed from ORR and MOVx,
483 	but computing them is costly. We don't  */
484 
485 	zeros = 0;
486 	ones = 0;
487 	for (i = 4; i > 0; i--) {
488 		if ((simm & 0xffff) == 0)
489 			zeros++;
490 		if ((simm & 0xffff) == 0xffff)
491 			ones++;
492 		simm >>= 16;
493 	}
494 
495 	simm = (sljit_sw)imm;
496 	first = 1;
497 	if (ones > zeros) {
498 		simm = ~simm;
499 		for (i = 0; i < 4; i++) {
500 			if (!(simm & 0xffff)) {
501 				simm >>= 16;
502 				continue;
503 			}
504 			if (first) {
505 				first = 0;
506 				FAIL_IF(push_inst(compiler, MOVN | RD(dst) | ((simm & 0xffff) << 5) | (i << 21)));
507 			}
508 			else
509 				FAIL_IF(push_inst(compiler, MOVK | RD(dst) | ((~simm & 0xffff) << 5) | (i << 21)));
510 			simm >>= 16;
511 		}
512 		return SLJIT_SUCCESS;
513 	}
514 
515 	for (i = 0; i < 4; i++) {
516 		if (!(simm & 0xffff)) {
517 			simm >>= 16;
518 			continue;
519 		}
520 		if (first) {
521 			first = 0;
522 			FAIL_IF(push_inst(compiler, MOVZ | RD(dst) | ((simm & 0xffff) << 5) | (i << 21)));
523 		}
524 		else
525 			FAIL_IF(push_inst(compiler, MOVK | RD(dst) | ((simm & 0xffff) << 5) | (i << 21)));
526 		simm >>= 16;
527 	}
528 	return SLJIT_SUCCESS;
529 }
530 
531 #define ARG1_IMM	0x0010000
532 #define ARG2_IMM	0x0020000
533 #define INT_OP		0x0040000
534 #define SET_FLAGS	0x0080000
535 #define UNUSED_RETURN	0x0100000
536 #define SLOW_DEST	0x0200000
537 #define SLOW_SRC1	0x0400000
538 #define SLOW_SRC2	0x0800000
539 
540 #define CHECK_FLAGS(flag_bits) \
541 	if (flags & SET_FLAGS) { \
542 		inv_bits |= flag_bits; \
543 		if (flags & UNUSED_RETURN) \
544 			dst = TMP_ZERO; \
545 	}
546 
emit_op_imm(struct sljit_compiler * compiler,sljit_s32 flags,sljit_s32 dst,sljit_sw arg1,sljit_sw arg2)547 static sljit_s32 emit_op_imm(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 dst, sljit_sw arg1, sljit_sw arg2)
548 {
549 	/* dst must be register, TMP_REG1
550 	   arg1 must be register, TMP_REG1, imm
551 	   arg2 must be register, TMP_REG2, imm */
552 	sljit_ins inv_bits = (flags & INT_OP) ? (1 << 31) : 0;
553 	sljit_ins inst_bits;
554 	sljit_s32 op = (flags & 0xffff);
555 	sljit_s32 reg;
556 	sljit_sw imm, nimm;
557 
558 	if (SLJIT_UNLIKELY((flags & (ARG1_IMM | ARG2_IMM)) == (ARG1_IMM | ARG2_IMM))) {
559 		/* Both are immediates. */
560 		flags &= ~ARG1_IMM;
561 		if (arg1 == 0 && op != SLJIT_ADD && op != SLJIT_SUB)
562 			arg1 = TMP_ZERO;
563 		else {
564 			FAIL_IF(load_immediate(compiler, TMP_REG1, arg1));
565 			arg1 = TMP_REG1;
566 		}
567 	}
568 
569 	if (flags & (ARG1_IMM | ARG2_IMM)) {
570 		reg = (flags & ARG2_IMM) ? arg1 : arg2;
571 		imm = (flags & ARG2_IMM) ? arg2 : arg1;
572 
573 		switch (op) {
574 		case SLJIT_MUL:
575 		case SLJIT_NEG:
576 		case SLJIT_CLZ:
577 		case SLJIT_ADDC:
578 		case SLJIT_SUBC:
579 			/* No form with immediate operand (except imm 0, which
580 			is represented by a ZERO register). */
581 			break;
582 		case SLJIT_MOV:
583 			SLJIT_ASSERT(!(flags & SET_FLAGS) && (flags & ARG2_IMM) && arg1 == TMP_REG1);
584 			return load_immediate(compiler, dst, imm);
585 		case SLJIT_NOT:
586 			SLJIT_ASSERT(flags & ARG2_IMM);
587 			FAIL_IF(load_immediate(compiler, dst, (flags & INT_OP) ? (~imm & 0xffffffff) : ~imm));
588 			goto set_flags;
589 		case SLJIT_SUB:
590 			if (flags & ARG1_IMM)
591 				break;
592 			imm = -imm;
593 			/* Fall through. */
594 		case SLJIT_ADD:
595 			if (imm == 0) {
596 				CHECK_FLAGS(1 << 29);
597 				return push_inst(compiler, ((op == SLJIT_ADD ? ADDI : SUBI) ^ inv_bits) | RD(dst) | RN(reg));
598 			}
599 			if (imm > 0 && imm <= 0xfff) {
600 				CHECK_FLAGS(1 << 29);
601 				return push_inst(compiler, (ADDI ^ inv_bits) | RD(dst) | RN(reg) | (imm << 10));
602 			}
603 			nimm = -imm;
604 			if (nimm > 0 && nimm <= 0xfff) {
605 				CHECK_FLAGS(1 << 29);
606 				return push_inst(compiler, (SUBI ^ inv_bits) | RD(dst) | RN(reg) | (nimm << 10));
607 			}
608 			if (imm > 0 && imm <= 0xffffff && !(imm & 0xfff)) {
609 				CHECK_FLAGS(1 << 29);
610 				return push_inst(compiler, (ADDI ^ inv_bits) | RD(dst) | RN(reg) | ((imm >> 12) << 10) | (1 << 22));
611 			}
612 			if (nimm > 0 && nimm <= 0xffffff && !(nimm & 0xfff)) {
613 				CHECK_FLAGS(1 << 29);
614 				return push_inst(compiler, (SUBI ^ inv_bits) | RD(dst) | RN(reg) | ((nimm >> 12) << 10) | (1 << 22));
615 			}
616 			if (imm > 0 && imm <= 0xffffff && !(flags & SET_FLAGS)) {
617 				FAIL_IF(push_inst(compiler, (ADDI ^ inv_bits) | RD(dst) | RN(reg) | ((imm >> 12) << 10) | (1 << 22)));
618 				return push_inst(compiler, (ADDI ^ inv_bits) | RD(dst) | RN(dst) | ((imm & 0xfff) << 10));
619 			}
620 			if (nimm > 0 && nimm <= 0xffffff && !(flags & SET_FLAGS)) {
621 				FAIL_IF(push_inst(compiler, (SUBI ^ inv_bits) | RD(dst) | RN(reg) | ((nimm >> 12) << 10) | (1 << 22)));
622 				return push_inst(compiler, (SUBI ^ inv_bits) | RD(dst) | RN(dst) | ((nimm & 0xfff) << 10));
623 			}
624 			break;
625 		case SLJIT_AND:
626 			inst_bits = logical_imm(imm, LOGICAL_IMM_CHECK | ((flags & INT_OP) ? 16 : 32));
627 			if (!inst_bits)
628 				break;
629 			CHECK_FLAGS(3 << 29);
630 			return push_inst(compiler, (ANDI ^ inv_bits) | RD(dst) | RN(reg) | inst_bits);
631 		case SLJIT_OR:
632 		case SLJIT_XOR:
633 			inst_bits = logical_imm(imm, LOGICAL_IMM_CHECK | ((flags & INT_OP) ? 16 : 32));
634 			if (!inst_bits)
635 				break;
636 			if (op == SLJIT_OR)
637 				inst_bits |= ORRI;
638 			else
639 				inst_bits |= EORI;
640 			FAIL_IF(push_inst(compiler, (inst_bits ^ inv_bits) | RD(dst) | RN(reg)));
641 			goto set_flags;
642 		case SLJIT_SHL:
643 			if (flags & ARG1_IMM)
644 				break;
645 			if (flags & INT_OP) {
646 				imm &= 0x1f;
647 				FAIL_IF(push_inst(compiler, (UBFM ^ inv_bits) | RD(dst) | RN(arg1) | ((-imm & 0x1f) << 16) | ((31 - imm) << 10)));
648 			}
649 			else {
650 				imm &= 0x3f;
651 				FAIL_IF(push_inst(compiler, (UBFM ^ inv_bits) | RD(dst) | RN(arg1) | (1 << 22) | ((-imm & 0x3f) << 16) | ((63 - imm) << 10)));
652 			}
653 			goto set_flags;
654 		case SLJIT_LSHR:
655 		case SLJIT_ASHR:
656 			if (flags & ARG1_IMM)
657 				break;
658 			if (op == SLJIT_ASHR)
659 				inv_bits |= 1 << 30;
660 			if (flags & INT_OP) {
661 				imm &= 0x1f;
662 				FAIL_IF(push_inst(compiler, (UBFM ^ inv_bits) | RD(dst) | RN(arg1) | (imm << 16) | (31 << 10)));
663 			}
664 			else {
665 				imm &= 0x3f;
666 				FAIL_IF(push_inst(compiler, (UBFM ^ inv_bits) | RD(dst) | RN(arg1) | (1 << 22) | (imm << 16) | (63 << 10)));
667 			}
668 			goto set_flags;
669 		default:
670 			SLJIT_UNREACHABLE();
671 			break;
672 		}
673 
674 		if (flags & ARG2_IMM) {
675 			if (arg2 == 0)
676 				arg2 = TMP_ZERO;
677 			else {
678 				FAIL_IF(load_immediate(compiler, TMP_REG2, arg2));
679 				arg2 = TMP_REG2;
680 			}
681 		}
682 		else {
683 			if (arg1 == 0)
684 				arg1 = TMP_ZERO;
685 			else {
686 				FAIL_IF(load_immediate(compiler, TMP_REG1, arg1));
687 				arg1 = TMP_REG1;
688 			}
689 		}
690 	}
691 
692 	/* Both arguments are registers. */
693 	switch (op) {
694 	case SLJIT_MOV:
695 	case SLJIT_MOV_P:
696 	case SLJIT_MOVU:
697 	case SLJIT_MOVU_P:
698 		SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
699 		if (dst == arg2)
700 			return SLJIT_SUCCESS;
701 		return push_inst(compiler, ORR | RD(dst) | RN(TMP_ZERO) | RM(arg2));
702 	case SLJIT_MOV_U8:
703 	case SLJIT_MOVU_U8:
704 		SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
705 		return push_inst(compiler, (UBFM ^ (1 << 31)) | RD(dst) | RN(arg2) | (7 << 10));
706 	case SLJIT_MOV_S8:
707 	case SLJIT_MOVU_S8:
708 		SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
709 		if (!(flags & INT_OP))
710 			inv_bits |= 1 << 22;
711 		return push_inst(compiler, (SBFM ^ inv_bits) | RD(dst) | RN(arg2) | (7 << 10));
712 	case SLJIT_MOV_U16:
713 	case SLJIT_MOVU_U16:
714 		SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
715 		return push_inst(compiler, (UBFM ^ (1 << 31)) | RD(dst) | RN(arg2) | (15 << 10));
716 	case SLJIT_MOV_S16:
717 	case SLJIT_MOVU_S16:
718 		SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
719 		if (!(flags & INT_OP))
720 			inv_bits |= 1 << 22;
721 		return push_inst(compiler, (SBFM ^ inv_bits) | RD(dst) | RN(arg2) | (15 << 10));
722 	case SLJIT_MOV_U32:
723 	case SLJIT_MOVU_U32:
724 		SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
725 		if ((flags & INT_OP) && dst == arg2)
726 			return SLJIT_SUCCESS;
727 		return push_inst(compiler, (ORR ^ (1 << 31)) | RD(dst) | RN(TMP_ZERO) | RM(arg2));
728 	case SLJIT_MOV_S32:
729 	case SLJIT_MOVU_S32:
730 		SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
731 		if ((flags & INT_OP) && dst == arg2)
732 			return SLJIT_SUCCESS;
733 		return push_inst(compiler, SBFM | (1 << 22) | RD(dst) | RN(arg2) | (31 << 10));
734 	case SLJIT_NOT:
735 		SLJIT_ASSERT(arg1 == TMP_REG1);
736 		FAIL_IF(push_inst(compiler, (ORN ^ inv_bits) | RD(dst) | RN(TMP_ZERO) | RM(arg2)));
737 		break; /* Set flags. */
738 	case SLJIT_NEG:
739 		SLJIT_ASSERT(arg1 == TMP_REG1);
740 		if (flags & SET_FLAGS)
741 			inv_bits |= 1 << 29;
742 		return push_inst(compiler, (SUB ^ inv_bits) | RD(dst) | RN(TMP_ZERO) | RM(arg2));
743 	case SLJIT_CLZ:
744 		SLJIT_ASSERT(arg1 == TMP_REG1);
745 		return push_inst(compiler, (CLZ ^ inv_bits) | RD(dst) | RN(arg2));
746 	case SLJIT_ADD:
747 		CHECK_FLAGS(1 << 29);
748 		return push_inst(compiler, (ADD ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2));
749 	case SLJIT_ADDC:
750 		CHECK_FLAGS(1 << 29);
751 		return push_inst(compiler, (ADC ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2));
752 	case SLJIT_SUB:
753 		CHECK_FLAGS(1 << 29);
754 		return push_inst(compiler, (SUB ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2));
755 	case SLJIT_SUBC:
756 		CHECK_FLAGS(1 << 29);
757 		return push_inst(compiler, (SBC ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2));
758 	case SLJIT_MUL:
759 		if (!(flags & SET_FLAGS))
760 			return push_inst(compiler, (MADD ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2) | RT2(TMP_ZERO));
761 		if (flags & INT_OP) {
762 			FAIL_IF(push_inst(compiler, SMADDL | RD(dst) | RN(arg1) | RM(arg2) | (31 << 10)));
763 			FAIL_IF(push_inst(compiler, ADD | RD(TMP_LR) | RN(TMP_ZERO) | RM(dst) | (2 << 22) | (31 << 10)));
764 			return push_inst(compiler, SUBS | RD(TMP_ZERO) | RN(TMP_LR) | RM(dst) | (2 << 22) | (63 << 10));
765 		}
766 		FAIL_IF(push_inst(compiler, SMULH | RD(TMP_LR) | RN(arg1) | RM(arg2)));
767 		FAIL_IF(push_inst(compiler, MADD | RD(dst) | RN(arg1) | RM(arg2) | RT2(TMP_ZERO)));
768 		return push_inst(compiler, SUBS | RD(TMP_ZERO) | RN(TMP_LR) | RM(dst) | (2 << 22) | (63 << 10));
769 	case SLJIT_AND:
770 		CHECK_FLAGS(3 << 29);
771 		return push_inst(compiler, (AND ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2));
772 	case SLJIT_OR:
773 		FAIL_IF(push_inst(compiler, (ORR ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2)));
774 		break; /* Set flags. */
775 	case SLJIT_XOR:
776 		FAIL_IF(push_inst(compiler, (EOR ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2)));
777 		break; /* Set flags. */
778 	case SLJIT_SHL:
779 		FAIL_IF(push_inst(compiler, (LSLV ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2)));
780 		break; /* Set flags. */
781 	case SLJIT_LSHR:
782 		FAIL_IF(push_inst(compiler, (LSRV ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2)));
783 		break; /* Set flags. */
784 	case SLJIT_ASHR:
785 		FAIL_IF(push_inst(compiler, (ASRV ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2)));
786 		break; /* Set flags. */
787 	default:
788 		SLJIT_UNREACHABLE();
789 		return SLJIT_SUCCESS;
790 	}
791 
792 set_flags:
793 	if (flags & SET_FLAGS)
794 		return push_inst(compiler, (SUBS ^ inv_bits) | RD(TMP_ZERO) | RN(dst) | RM(TMP_ZERO));
795 	return SLJIT_SUCCESS;
796 }
797 
798 #define STORE		0x01
799 #define SIGNED		0x02
800 
801 #define UPDATE		0x04
802 #define ARG_TEST	0x08
803 
804 #define BYTE_SIZE	0x000
805 #define HALF_SIZE	0x100
806 #define INT_SIZE	0x200
807 #define WORD_SIZE	0x300
808 
809 #define MEM_SIZE_SHIFT(flags) ((flags) >> 8)
810 
811 static const sljit_ins sljit_mem_imm[4] = {
812 /* u l */ 0x39400000 /* ldrb [reg,imm] */,
813 /* u s */ 0x39000000 /* strb [reg,imm] */,
814 /* s l */ 0x39800000 /* ldrsb [reg,imm] */,
815 /* s s */ 0x39000000 /* strb [reg,imm] */,
816 };
817 
818 static const sljit_ins sljit_mem_simm[4] = {
819 /* u l */ 0x38400000 /* ldurb [reg,imm] */,
820 /* u s */ 0x38000000 /* sturb [reg,imm] */,
821 /* s l */ 0x38800000 /* ldursb [reg,imm] */,
822 /* s s */ 0x38000000 /* sturb [reg,imm] */,
823 };
824 
825 static const sljit_ins sljit_mem_pre_simm[4] = {
826 /* u l */ 0x38400c00 /* ldrb [reg,imm]! */,
827 /* u s */ 0x38000c00 /* strb [reg,imm]! */,
828 /* s l */ 0x38800c00 /* ldrsb [reg,imm]! */,
829 /* s s */ 0x38000c00 /* strb [reg,imm]! */,
830 };
831 
832 static const sljit_ins sljit_mem_reg[4] = {
833 /* u l */ 0x38606800 /* ldrb [reg,reg] */,
834 /* u s */ 0x38206800 /* strb [reg,reg] */,
835 /* s l */ 0x38a06800 /* ldrsb [reg,reg] */,
836 /* s s */ 0x38206800 /* strb [reg,reg] */,
837 };
838 
839 /* 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)840 static sljit_s32 emit_set_delta(struct sljit_compiler *compiler, sljit_s32 dst, sljit_s32 reg, sljit_sw value)
841 {
842 	if (value >= 0) {
843 		if (value <= 0xfff)
844 			return push_inst(compiler, ADDI | RD(dst) | RN(reg) | (value << 10));
845 		if (value <= 0xffffff && !(value & 0xfff))
846 			return push_inst(compiler, ADDI | (1 << 22) | RD(dst) | RN(reg) | (value >> 2));
847 	}
848 	else {
849 		value = -value;
850 		if (value <= 0xfff)
851 			return push_inst(compiler, SUBI | RD(dst) | RN(reg) | (value << 10));
852 		if (value <= 0xffffff && !(value & 0xfff))
853 			return push_inst(compiler, SUBI | (1 << 22) | RD(dst) | RN(reg) | (value >> 2));
854 	}
855 	return SLJIT_ERR_UNSUPPORTED;
856 }
857 
858 /* Can perform an operation using at most 1 instruction. */
getput_arg_fast(struct sljit_compiler * compiler,sljit_s32 flags,sljit_s32 reg,sljit_s32 arg,sljit_sw argw)859 static sljit_s32 getput_arg_fast(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg, sljit_s32 arg, sljit_sw argw)
860 {
861 	sljit_u32 shift = MEM_SIZE_SHIFT(flags);
862 
863 	SLJIT_ASSERT(arg & SLJIT_MEM);
864 
865 	if (SLJIT_UNLIKELY(flags & UPDATE)) {
866 		if ((arg & REG_MASK) && !(arg & OFFS_REG_MASK) && argw <= 255 && argw >= -256) {
867 			if (SLJIT_UNLIKELY(flags & ARG_TEST))
868 				return 1;
869 
870 			arg &= REG_MASK;
871 			argw &= 0x1ff;
872 			FAIL_IF(push_inst(compiler, sljit_mem_pre_simm[flags & 0x3]
873 				| (shift << 30) | RT(reg) | RN(arg) | (argw << 12)));
874 			return -1;
875 		}
876 		return 0;
877 	}
878 
879 	if (SLJIT_UNLIKELY(arg & OFFS_REG_MASK)) {
880 		argw &= 0x3;
881 		if (argw && argw != shift)
882 			return 0;
883 
884 		if (SLJIT_UNLIKELY(flags & ARG_TEST))
885 			return 1;
886 
887 		FAIL_IF(push_inst(compiler, sljit_mem_reg[flags & 0x3] | (shift << 30) | RT(reg)
888 			| RN(arg & REG_MASK) | RM(OFFS_REG(arg)) | (argw ? (1 << 12) : 0)));
889 		return -1;
890 	}
891 
892 	arg &= REG_MASK;
893 
894 	if (arg == SLJIT_UNUSED)
895 		return 0;
896 
897 	if (argw >= 0 && (argw >> shift) <= 0xfff && (argw & ((1 << shift) - 1)) == 0) {
898 		if (SLJIT_UNLIKELY(flags & ARG_TEST))
899 			return 1;
900 
901 		FAIL_IF(push_inst(compiler, sljit_mem_imm[flags & 0x3] | (shift << 30)
902 			| RT(reg) | RN(arg) | (argw << (10 - shift))));
903 		return -1;
904 	}
905 
906 	if (argw > 255 || argw < -256)
907 		return 0;
908 
909 	if (SLJIT_UNLIKELY(flags & ARG_TEST))
910 		return 1;
911 
912 	FAIL_IF(push_inst(compiler, sljit_mem_simm[flags & 0x3] | (shift << 30)
913 		| RT(reg) | RN(arg) | ((argw & 0x1ff) << 12)));
914 	return -1;
915 }
916 
917 /* see getput_arg below.
918    Note: can_cache is called only for binary operators. Those
919    operators always uses word arguments without write back. */
can_cache(sljit_s32 arg,sljit_sw argw,sljit_s32 next_arg,sljit_sw next_argw)920 static sljit_s32 can_cache(sljit_s32 arg, sljit_sw argw, sljit_s32 next_arg, sljit_sw next_argw)
921 {
922 	sljit_sw diff;
923 	if ((arg & OFFS_REG_MASK) || !(next_arg & SLJIT_MEM))
924 		return 0;
925 
926 	if (!(arg & REG_MASK)) {
927 		diff = argw - next_argw;
928 		if (diff <= 0xfff && diff >= -0xfff)
929 			return 1;
930 		return 0;
931 	}
932 
933 	if (argw == next_argw)
934 		return 1;
935 
936 	diff = argw - next_argw;
937 	if (arg == next_arg && diff <= 0xfff && diff >= -0xfff)
938 		return 1;
939 
940 	return 0;
941 }
942 
943 /* Emit the necessary instructions. See can_cache above. */
getput_arg(struct sljit_compiler * compiler,sljit_s32 flags,sljit_s32 reg,sljit_s32 arg,sljit_sw argw,sljit_s32 next_arg,sljit_sw next_argw)944 static sljit_s32 getput_arg(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg,
945 	sljit_s32 arg, sljit_sw argw, sljit_s32 next_arg, sljit_sw next_argw)
946 {
947 	sljit_u32 shift = MEM_SIZE_SHIFT(flags);
948 	sljit_s32 tmp_r, other_r;
949 	sljit_sw diff;
950 
951 	SLJIT_ASSERT(arg & SLJIT_MEM);
952 	if (!(next_arg & SLJIT_MEM)) {
953 		next_arg = 0;
954 		next_argw = 0;
955 	}
956 
957 	tmp_r = ((flags & STORE) || (flags == (WORD_SIZE | SIGNED))) ? TMP_REG3 : reg;
958 
959 	if (SLJIT_UNLIKELY((flags & UPDATE) && (arg & REG_MASK))) {
960 		/* Update only applies if a base register exists. */
961 		other_r = OFFS_REG(arg);
962 		if (!other_r) {
963 			other_r = arg & REG_MASK;
964 			SLJIT_ASSERT(other_r != reg);
965 
966 			if (argw >= 0 && argw <= 0xffffff) {
967 				if ((argw & 0xfff) != 0)
968 					FAIL_IF(push_inst(compiler, ADDI | RD(other_r) | RN(other_r) | ((argw & 0xfff) << 10)));
969 				if (argw >> 12)
970 					FAIL_IF(push_inst(compiler, ADDI | (1 << 22) | RD(other_r) | RN(other_r) | ((argw >> 12) << 10)));
971 				return push_inst(compiler, sljit_mem_imm[flags & 0x3] | (shift << 30) | RT(reg) | RN(other_r));
972 			}
973 			else if (argw < 0 && argw >= -0xffffff) {
974 				argw = -argw;
975 				if ((argw & 0xfff) != 0)
976 					FAIL_IF(push_inst(compiler, SUBI | RD(other_r) | RN(other_r) | ((argw & 0xfff) << 10)));
977 				if (argw >> 12)
978 					FAIL_IF(push_inst(compiler, SUBI | (1 << 22) | RD(other_r) | RN(other_r) | ((argw >> 12) << 10)));
979 				return push_inst(compiler, sljit_mem_imm[flags & 0x3] | (shift << 30) | RT(reg) | RN(other_r));
980 			}
981 
982 			if (compiler->cache_arg == SLJIT_MEM) {
983 				if (argw == compiler->cache_argw) {
984 					other_r = TMP_REG3;
985 					argw = 0;
986 				}
987 				else if (emit_set_delta(compiler, TMP_REG3, TMP_REG3, argw - compiler->cache_argw) != SLJIT_ERR_UNSUPPORTED) {
988 					FAIL_IF(compiler->error);
989 					compiler->cache_argw = argw;
990 					other_r = TMP_REG3;
991 					argw = 0;
992 				}
993 			}
994 
995 			if (argw) {
996 				FAIL_IF(load_immediate(compiler, TMP_REG3, argw));
997 				compiler->cache_arg = SLJIT_MEM;
998 				compiler->cache_argw = argw;
999 				other_r = TMP_REG3;
1000 				argw = 0;
1001 			}
1002 		}
1003 
1004 		/* No caching here. */
1005 		arg &= REG_MASK;
1006 		FAIL_IF(push_inst(compiler, sljit_mem_reg[flags & 0x3] | (shift << 30) | RT(reg) | RN(arg) | RM(other_r)));
1007 		return push_inst(compiler, ADD | RD(arg) | RN(arg) | RM(other_r));
1008 	}
1009 
1010 	if (arg & OFFS_REG_MASK) {
1011 		other_r = OFFS_REG(arg);
1012 		arg &= REG_MASK;
1013 		FAIL_IF(push_inst(compiler, ADD | RD(tmp_r) | RN(arg) | RM(other_r) | ((argw & 0x3) << 10)));
1014 		return push_inst(compiler, sljit_mem_imm[flags & 0x3] | (shift << 30) | RT(reg) | RN(tmp_r));
1015 	}
1016 
1017 	if (compiler->cache_arg == arg) {
1018 		diff = argw - compiler->cache_argw;
1019 		if (diff <= 255 && diff >= -256)
1020 			return push_inst(compiler, sljit_mem_simm[flags & 0x3] | (shift << 30)
1021 				| RT(reg) | RN(TMP_REG3) | ((diff & 0x1ff) << 12));
1022 		if (emit_set_delta(compiler, TMP_REG3, TMP_REG3, diff) != SLJIT_ERR_UNSUPPORTED) {
1023 			FAIL_IF(compiler->error);
1024 			return push_inst(compiler, sljit_mem_imm[flags & 0x3] | (shift << 30) | RT(reg) | RN(arg));
1025 		}
1026 	}
1027 
1028 	diff = argw - next_argw;
1029 	next_arg = (arg & REG_MASK) && (arg == next_arg) && diff <= 0xfff && diff >= -0xfff && diff != 0;
1030 	arg &= REG_MASK;
1031 
1032 	if (arg != SLJIT_UNUSED && argw >= 0 && argw <= 0xffffff && (argw & ((1 << shift) - 1)) == 0) {
1033 		FAIL_IF(push_inst(compiler, ADDI | (1 << 22) | RD(tmp_r) | RN(arg) | ((argw >> 12) << 10)));
1034 		return push_inst(compiler, sljit_mem_imm[flags & 0x3] | (shift << 30)
1035 			| RT(reg) | RN(tmp_r) | ((argw & 0xfff) << (10 - shift)));
1036 	}
1037 
1038 	if (arg && compiler->cache_arg == SLJIT_MEM) {
1039 		if (compiler->cache_argw == argw)
1040 			return push_inst(compiler, sljit_mem_reg[flags & 0x3] | (shift << 30) | RT(reg) | RN(arg) | RM(TMP_REG3));
1041 		if (emit_set_delta(compiler, TMP_REG3, TMP_REG3, argw - compiler->cache_argw) != SLJIT_ERR_UNSUPPORTED) {
1042 			FAIL_IF(compiler->error);
1043 			compiler->cache_argw = argw;
1044 			return push_inst(compiler, sljit_mem_reg[flags & 0x3] | (shift << 30) | RT(reg) | RN(arg) | RM(TMP_REG3));
1045 		}
1046 	}
1047 
1048 	compiler->cache_argw = argw;
1049 	if (next_arg && emit_set_delta(compiler, TMP_REG3, arg, argw) != SLJIT_ERR_UNSUPPORTED) {
1050 		FAIL_IF(compiler->error);
1051 		compiler->cache_arg = SLJIT_MEM | arg;
1052 		arg = 0;
1053 	}
1054 	else {
1055 		FAIL_IF(load_immediate(compiler, TMP_REG3, argw));
1056 		compiler->cache_arg = SLJIT_MEM;
1057 
1058 		if (next_arg) {
1059 			FAIL_IF(push_inst(compiler, ADD | RD(TMP_REG3) | RN(TMP_REG3) | RM(arg)));
1060 			compiler->cache_arg = SLJIT_MEM | arg;
1061 			arg = 0;
1062 		}
1063 	}
1064 
1065 	if (arg)
1066 		return push_inst(compiler, sljit_mem_reg[flags & 0x3] | (shift << 30) | RT(reg) | RN(arg) | RM(TMP_REG3));
1067 	return push_inst(compiler, sljit_mem_imm[flags & 0x3] | (shift << 30) | RT(reg) | RN(TMP_REG3));
1068 }
1069 
emit_op_mem(struct sljit_compiler * compiler,sljit_s32 flags,sljit_s32 reg,sljit_s32 arg,sljit_sw argw)1070 static SLJIT_INLINE sljit_s32 emit_op_mem(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg, sljit_s32 arg, sljit_sw argw)
1071 {
1072 	if (getput_arg_fast(compiler, flags, reg, arg, argw))
1073 		return compiler->error;
1074 	compiler->cache_arg = 0;
1075 	compiler->cache_argw = 0;
1076 	return getput_arg(compiler, flags, reg, arg, argw, 0, 0);
1077 }
1078 
emit_op_mem2(struct sljit_compiler * compiler,sljit_s32 flags,sljit_s32 reg,sljit_s32 arg1,sljit_sw arg1w,sljit_s32 arg2,sljit_sw arg2w)1079 static SLJIT_INLINE sljit_s32 emit_op_mem2(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg, sljit_s32 arg1, sljit_sw arg1w, sljit_s32 arg2, sljit_sw arg2w)
1080 {
1081 	if (getput_arg_fast(compiler, flags, reg, arg1, arg1w))
1082 		return compiler->error;
1083 	return getput_arg(compiler, flags, reg, arg1, arg1w, arg2, arg2w);
1084 }
1085 
1086 /* --------------------------------------------------------------------- */
1087 /*  Entry, exit                                                          */
1088 /* --------------------------------------------------------------------- */
1089 
sljit_emit_enter(struct sljit_compiler * compiler,sljit_s32 options,sljit_s32 args,sljit_s32 scratches,sljit_s32 saveds,sljit_s32 fscratches,sljit_s32 fsaveds,sljit_s32 local_size)1090 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_enter(struct sljit_compiler *compiler,
1091 	sljit_s32 options, sljit_s32 args, sljit_s32 scratches, sljit_s32 saveds,
1092 	sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
1093 {
1094 	sljit_s32 i, tmp, offs, prev, saved_regs_size;
1095 
1096 	CHECK_ERROR();
1097 	CHECK(check_sljit_emit_enter(compiler, options, args, scratches, saveds, fscratches, fsaveds, local_size));
1098 	set_emit_enter(compiler, options, args, scratches, saveds, fscratches, fsaveds, local_size);
1099 
1100 	saved_regs_size = GET_SAVED_REGISTERS_SIZE(scratches, saveds, 0);
1101 	local_size += saved_regs_size + SLJIT_LOCALS_OFFSET;
1102 	local_size = (local_size + 15) & ~0xf;
1103 	compiler->local_size = local_size;
1104 
1105 	if (local_size <= (63 * sizeof(sljit_sw))) {
1106 		FAIL_IF(push_inst(compiler, STP_PRE | 29 | RT2(TMP_LR)
1107 			| RN(TMP_SP) | ((-(local_size >> 3) & 0x7f) << 15)));
1108 		FAIL_IF(push_inst(compiler, ADDI | RD(SLJIT_SP) | RN(TMP_SP) | (0 << 10)));
1109 		offs = (local_size - saved_regs_size) << (15 - 3);
1110 	} else {
1111 		offs = 0 << 15;
1112 		if (saved_regs_size & 0x8) {
1113 			offs = 1 << 15;
1114 			saved_regs_size += sizeof(sljit_sw);
1115 		}
1116 		local_size -= saved_regs_size + SLJIT_LOCALS_OFFSET;
1117 		if (saved_regs_size > 0)
1118 			FAIL_IF(push_inst(compiler, SUBI | RD(TMP_SP) | RN(TMP_SP) | (saved_regs_size << 10)));
1119 	}
1120 
1121 	tmp = saveds < SLJIT_NUMBER_OF_SAVED_REGISTERS ? (SLJIT_S0 + 1 - saveds) : SLJIT_FIRST_SAVED_REG;
1122 	prev = -1;
1123 	for (i = SLJIT_S0; i >= tmp; i--) {
1124 		if (prev == -1) {
1125 			if (!(offs & (1 << 15))) {
1126 				prev = i;
1127 				continue;
1128 			}
1129 			FAIL_IF(push_inst(compiler, STRI | RT(i) | RN(TMP_SP) | (offs >> 5)));
1130 			offs += 1 << 15;
1131 			continue;
1132 		}
1133 		FAIL_IF(push_inst(compiler, STP | RT(prev) | RT2(i) | RN(TMP_SP) | offs));
1134 		offs += 2 << 15;
1135 		prev = -1;
1136 	}
1137 
1138 	for (i = scratches; i >= SLJIT_FIRST_SAVED_REG; i--) {
1139 		if (prev == -1) {
1140 			if (!(offs & (1 << 15))) {
1141 				prev = i;
1142 				continue;
1143 			}
1144 			FAIL_IF(push_inst(compiler, STRI | RT(i) | RN(TMP_SP) | (offs >> 5)));
1145 			offs += 1 << 15;
1146 			continue;
1147 		}
1148 		FAIL_IF(push_inst(compiler, STP | RT(prev) | RT2(i) | RN(TMP_SP) | offs));
1149 		offs += 2 << 15;
1150 		prev = -1;
1151 	}
1152 
1153 	SLJIT_ASSERT(prev == -1);
1154 
1155 	if (compiler->local_size > (63 * sizeof(sljit_sw))) {
1156 		/* The local_size is already adjusted by the saved registers. */
1157 		if (local_size > 0xfff) {
1158 			FAIL_IF(push_inst(compiler, SUBI | RD(TMP_SP) | RN(TMP_SP) | ((local_size >> 12) << 10) | (1 << 22)));
1159 			local_size &= 0xfff;
1160 		}
1161 		if (local_size)
1162 			FAIL_IF(push_inst(compiler, SUBI | RD(TMP_SP) | RN(TMP_SP) | (local_size << 10)));
1163 		FAIL_IF(push_inst(compiler, STP_PRE | 29 | RT2(TMP_LR)
1164 			| RN(TMP_SP) | ((-(16 >> 3) & 0x7f) << 15)));
1165 		FAIL_IF(push_inst(compiler, ADDI | RD(SLJIT_SP) | RN(TMP_SP) | (0 << 10)));
1166 	}
1167 
1168 	if (args >= 1)
1169 		FAIL_IF(push_inst(compiler, ORR | RD(SLJIT_S0) | RN(TMP_ZERO) | RM(SLJIT_R0)));
1170 	if (args >= 2)
1171 		FAIL_IF(push_inst(compiler, ORR | RD(SLJIT_S1) | RN(TMP_ZERO) | RM(SLJIT_R1)));
1172 	if (args >= 3)
1173 		FAIL_IF(push_inst(compiler, ORR | RD(SLJIT_S2) | RN(TMP_ZERO) | RM(SLJIT_R2)));
1174 
1175 	return SLJIT_SUCCESS;
1176 }
1177 
sljit_set_context(struct sljit_compiler * compiler,sljit_s32 options,sljit_s32 args,sljit_s32 scratches,sljit_s32 saveds,sljit_s32 fscratches,sljit_s32 fsaveds,sljit_s32 local_size)1178 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_set_context(struct sljit_compiler *compiler,
1179 	sljit_s32 options, sljit_s32 args, sljit_s32 scratches, sljit_s32 saveds,
1180 	sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
1181 {
1182 	CHECK_ERROR();
1183 	CHECK(check_sljit_set_context(compiler, options, args, scratches, saveds, fscratches, fsaveds, local_size));
1184 	set_set_context(compiler, options, args, scratches, saveds, fscratches, fsaveds, local_size);
1185 
1186 	local_size += GET_SAVED_REGISTERS_SIZE(scratches, saveds, 0) + SLJIT_LOCALS_OFFSET;
1187 	local_size = (local_size + 15) & ~0xf;
1188 	compiler->local_size = local_size;
1189 	return SLJIT_SUCCESS;
1190 }
1191 
sljit_emit_return(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 src,sljit_sw srcw)1192 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_return(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 src, sljit_sw srcw)
1193 {
1194 	sljit_s32 local_size;
1195 	sljit_s32 i, tmp, offs, prev, saved_regs_size;
1196 
1197 	CHECK_ERROR();
1198 	CHECK(check_sljit_emit_return(compiler, op, src, srcw));
1199 
1200 	FAIL_IF(emit_mov_before_return(compiler, op, src, srcw));
1201 
1202 	local_size = compiler->local_size;
1203 
1204 	saved_regs_size = GET_SAVED_REGISTERS_SIZE(compiler->scratches, compiler->saveds, 0);
1205 	if (local_size <= (63 * sizeof(sljit_sw)))
1206 		offs = (local_size - saved_regs_size) << (15 - 3);
1207 	else {
1208 		FAIL_IF(push_inst(compiler, LDP_PST | 29 | RT2(TMP_LR)
1209 			| RN(TMP_SP) | (((16 >> 3) & 0x7f) << 15)));
1210 		offs = 0 << 15;
1211 		if (saved_regs_size & 0x8) {
1212 			offs = 1 << 15;
1213 			saved_regs_size += sizeof(sljit_sw);
1214 		}
1215 		local_size -= saved_regs_size + SLJIT_LOCALS_OFFSET;
1216 		if (local_size > 0xfff) {
1217 			FAIL_IF(push_inst(compiler, ADDI | RD(TMP_SP) | RN(TMP_SP) | ((local_size >> 12) << 10) | (1 << 22)));
1218 			local_size &= 0xfff;
1219 		}
1220 		if (local_size)
1221 			FAIL_IF(push_inst(compiler, ADDI | RD(TMP_SP) | RN(TMP_SP) | (local_size << 10)));
1222 	}
1223 
1224 	tmp = compiler->saveds < SLJIT_NUMBER_OF_SAVED_REGISTERS ? (SLJIT_S0 + 1 - compiler->saveds) : SLJIT_FIRST_SAVED_REG;
1225 	prev = -1;
1226 	for (i = SLJIT_S0; i >= tmp; i--) {
1227 		if (prev == -1) {
1228 			if (!(offs & (1 << 15))) {
1229 				prev = i;
1230 				continue;
1231 			}
1232 			FAIL_IF(push_inst(compiler, LDRI | RT(i) | RN(TMP_SP) | (offs >> 5)));
1233 			offs += 1 << 15;
1234 			continue;
1235 		}
1236 		FAIL_IF(push_inst(compiler, LDP | RT(prev) | RT2(i) | RN(TMP_SP) | offs));
1237 		offs += 2 << 15;
1238 		prev = -1;
1239 	}
1240 
1241 	for (i = compiler->scratches; i >= SLJIT_FIRST_SAVED_REG; i--) {
1242 		if (prev == -1) {
1243 			if (!(offs & (1 << 15))) {
1244 				prev = i;
1245 				continue;
1246 			}
1247 			FAIL_IF(push_inst(compiler, LDRI | RT(i) | RN(TMP_SP) | (offs >> 5)));
1248 			offs += 1 << 15;
1249 			continue;
1250 		}
1251 		FAIL_IF(push_inst(compiler, LDP | RT(prev) | RT2(i) | RN(TMP_SP) | offs));
1252 		offs += 2 << 15;
1253 		prev = -1;
1254 	}
1255 
1256 	SLJIT_ASSERT(prev == -1);
1257 
1258 	if (compiler->local_size <= (63 * sizeof(sljit_sw))) {
1259 		FAIL_IF(push_inst(compiler, LDP_PST | 29 | RT2(TMP_LR)
1260 			| RN(TMP_SP) | (((local_size >> 3) & 0x7f) << 15)));
1261 	} else if (saved_regs_size > 0) {
1262 		FAIL_IF(push_inst(compiler, ADDI | RD(TMP_SP) | RN(TMP_SP) | (saved_regs_size << 10)));
1263 	}
1264 
1265 	FAIL_IF(push_inst(compiler, RET | RN(TMP_LR)));
1266 	return SLJIT_SUCCESS;
1267 }
1268 
1269 /* --------------------------------------------------------------------- */
1270 /*  Operators                                                            */
1271 /* --------------------------------------------------------------------- */
1272 
sljit_emit_op0(struct sljit_compiler * compiler,sljit_s32 op)1273 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op0(struct sljit_compiler *compiler, sljit_s32 op)
1274 {
1275 	sljit_ins inv_bits = (op & SLJIT_I32_OP) ? (1 << 31) : 0;
1276 
1277 	CHECK_ERROR();
1278 	CHECK(check_sljit_emit_op0(compiler, op));
1279 
1280 	op = GET_OPCODE(op);
1281 	switch (op) {
1282 	case SLJIT_BREAKPOINT:
1283 		return push_inst(compiler, BRK);
1284 	case SLJIT_NOP:
1285 		return push_inst(compiler, NOP);
1286 	case SLJIT_LMUL_UW:
1287 	case SLJIT_LMUL_SW:
1288 		FAIL_IF(push_inst(compiler, ORR | RD(TMP_REG1) | RN(TMP_ZERO) | RM(SLJIT_R0)));
1289 		FAIL_IF(push_inst(compiler, MADD | RD(SLJIT_R0) | RN(SLJIT_R0) | RM(SLJIT_R1) | RT2(TMP_ZERO)));
1290 		return push_inst(compiler, (op == SLJIT_LMUL_UW ? UMULH : SMULH) | RD(SLJIT_R1) | RN(TMP_REG1) | RM(SLJIT_R1));
1291 	case SLJIT_DIVMOD_UW:
1292 	case SLJIT_DIVMOD_SW:
1293 		FAIL_IF(push_inst(compiler, (ORR ^ inv_bits) | RD(TMP_REG1) | RN(TMP_ZERO) | RM(SLJIT_R0)));
1294 		FAIL_IF(push_inst(compiler, ((op == SLJIT_DIVMOD_UW ? UDIV : SDIV) ^ inv_bits) | RD(SLJIT_R0) | RN(SLJIT_R0) | RM(SLJIT_R1)));
1295 		FAIL_IF(push_inst(compiler, (MADD ^ inv_bits) | RD(SLJIT_R1) | RN(SLJIT_R0) | RM(SLJIT_R1) | RT2(TMP_ZERO)));
1296 		return push_inst(compiler, (SUB ^ inv_bits) | RD(SLJIT_R1) | RN(TMP_REG1) | RM(SLJIT_R1));
1297 	case SLJIT_DIV_UW:
1298 	case SLJIT_DIV_SW:
1299 		return push_inst(compiler, ((op == SLJIT_DIV_UW ? UDIV : SDIV) ^ inv_bits) | RD(SLJIT_R0) | RN(SLJIT_R0) | RM(SLJIT_R1));
1300 	}
1301 
1302 	return SLJIT_SUCCESS;
1303 }
1304 
sljit_emit_op1(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 src,sljit_sw srcw)1305 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op1(struct sljit_compiler *compiler, sljit_s32 op,
1306 	sljit_s32 dst, sljit_sw dstw,
1307 	sljit_s32 src, sljit_sw srcw)
1308 {
1309 	sljit_s32 dst_r, flags, mem_flags;
1310 	sljit_s32 op_flags = GET_ALL_FLAGS(op);
1311 
1312 	CHECK_ERROR();
1313 	CHECK(check_sljit_emit_op1(compiler, op, dst, dstw, src, srcw));
1314 	ADJUST_LOCAL_OFFSET(dst, dstw);
1315 	ADJUST_LOCAL_OFFSET(src, srcw);
1316 
1317 	compiler->cache_arg = 0;
1318 	compiler->cache_argw = 0;
1319 
1320 	if (dst == SLJIT_UNUSED && !HAS_FLAGS(op)) {
1321 		if (op <= SLJIT_MOV_P && (src & SLJIT_MEM)) {
1322 			SLJIT_ASSERT(reg_map[1] == 0 && reg_map[3] == 2 && reg_map[5] == 4);
1323 
1324 			if (op >= SLJIT_MOV_U8 && op <= SLJIT_MOV_S8)
1325 				dst = 5;
1326 			else if (op >= SLJIT_MOV_U16 && op <= SLJIT_MOV_S16)
1327 				dst = 3;
1328 			else
1329 				dst = 1;
1330 
1331 			/* Signed word sized load is the prefetch instruction. */
1332 			return emit_op_mem(compiler, WORD_SIZE | SIGNED, dst, src, srcw);
1333 		}
1334 		return SLJIT_SUCCESS;
1335 	}
1336 
1337 	dst_r = SLOW_IS_REG(dst) ? dst : TMP_REG1;
1338 
1339 	op = GET_OPCODE(op);
1340 	if (op >= SLJIT_MOV && op <= SLJIT_MOVU_P) {
1341 		switch (op) {
1342 		case SLJIT_MOV:
1343 		case SLJIT_MOV_P:
1344 			flags = WORD_SIZE;
1345 			break;
1346 		case SLJIT_MOV_U8:
1347 			flags = BYTE_SIZE;
1348 			if (src & SLJIT_IMM)
1349 				srcw = (sljit_u8)srcw;
1350 			break;
1351 		case SLJIT_MOV_S8:
1352 			flags = BYTE_SIZE | SIGNED;
1353 			if (src & SLJIT_IMM)
1354 				srcw = (sljit_s8)srcw;
1355 			break;
1356 		case SLJIT_MOV_U16:
1357 			flags = HALF_SIZE;
1358 			if (src & SLJIT_IMM)
1359 				srcw = (sljit_u16)srcw;
1360 			break;
1361 		case SLJIT_MOV_S16:
1362 			flags = HALF_SIZE | SIGNED;
1363 			if (src & SLJIT_IMM)
1364 				srcw = (sljit_s16)srcw;
1365 			break;
1366 		case SLJIT_MOV_U32:
1367 			flags = INT_SIZE;
1368 			if (src & SLJIT_IMM)
1369 				srcw = (sljit_u32)srcw;
1370 			break;
1371 		case SLJIT_MOV_S32:
1372 			flags = INT_SIZE | SIGNED;
1373 			if (src & SLJIT_IMM)
1374 				srcw = (sljit_s32)srcw;
1375 			break;
1376 		case SLJIT_MOVU:
1377 		case SLJIT_MOVU_P:
1378 			flags = WORD_SIZE | UPDATE;
1379 			break;
1380 		case SLJIT_MOVU_U8:
1381 			flags = BYTE_SIZE | UPDATE;
1382 			if (src & SLJIT_IMM)
1383 				srcw = (sljit_u8)srcw;
1384 			break;
1385 		case SLJIT_MOVU_S8:
1386 			flags = BYTE_SIZE | SIGNED | UPDATE;
1387 			if (src & SLJIT_IMM)
1388 				srcw = (sljit_s8)srcw;
1389 			break;
1390 		case SLJIT_MOVU_U16:
1391 			flags = HALF_SIZE | UPDATE;
1392 			if (src & SLJIT_IMM)
1393 				srcw = (sljit_u16)srcw;
1394 			break;
1395 		case SLJIT_MOVU_S16:
1396 			flags = HALF_SIZE | SIGNED | UPDATE;
1397 			if (src & SLJIT_IMM)
1398 				srcw = (sljit_s16)srcw;
1399 			break;
1400 		case SLJIT_MOVU_U32:
1401 			flags = INT_SIZE | UPDATE;
1402 			if (src & SLJIT_IMM)
1403 				srcw = (sljit_u32)srcw;
1404 			break;
1405 		case SLJIT_MOVU_S32:
1406 			flags = INT_SIZE | SIGNED | UPDATE;
1407 			if (src & SLJIT_IMM)
1408 				srcw = (sljit_s32)srcw;
1409 			break;
1410 		default:
1411 			SLJIT_UNREACHABLE();
1412 			flags = 0;
1413 			break;
1414 		}
1415 
1416 		if (src & SLJIT_IMM)
1417 			FAIL_IF(emit_op_imm(compiler, SLJIT_MOV | ARG2_IMM, dst_r, TMP_REG1, srcw));
1418 		else if (src & SLJIT_MEM) {
1419 			if (getput_arg_fast(compiler, flags, dst_r, src, srcw))
1420 				FAIL_IF(compiler->error);
1421 			else
1422 				FAIL_IF(getput_arg(compiler, flags, dst_r, src, srcw, dst, dstw));
1423 		} else {
1424 			if (dst_r != TMP_REG1)
1425 				return emit_op_imm(compiler, op | ((op_flags & SLJIT_I32_OP) ? INT_OP : 0), dst_r, TMP_REG1, src);
1426 			dst_r = src;
1427 		}
1428 
1429 		if (dst & SLJIT_MEM) {
1430 			if (getput_arg_fast(compiler, flags | STORE, dst_r, dst, dstw))
1431 				return compiler->error;
1432 			else
1433 				return getput_arg(compiler, flags | STORE, dst_r, dst, dstw, 0, 0);
1434 		}
1435 		return SLJIT_SUCCESS;
1436 	}
1437 
1438 	flags = HAS_FLAGS(op_flags) ? SET_FLAGS : 0;
1439 	mem_flags = WORD_SIZE;
1440 	if (op_flags & SLJIT_I32_OP) {
1441 		flags |= INT_OP;
1442 		mem_flags = INT_SIZE;
1443 	}
1444 
1445 	if (dst == SLJIT_UNUSED)
1446 		flags |= UNUSED_RETURN;
1447 
1448 	if (src & SLJIT_MEM) {
1449 		if (getput_arg_fast(compiler, mem_flags, TMP_REG2, src, srcw))
1450 			FAIL_IF(compiler->error);
1451 		else
1452 			FAIL_IF(getput_arg(compiler, mem_flags, TMP_REG2, src, srcw, dst, dstw));
1453 		src = TMP_REG2;
1454 	}
1455 
1456 	if (src & SLJIT_IMM) {
1457 		flags |= ARG2_IMM;
1458 		if (op_flags & SLJIT_I32_OP)
1459 			srcw = (sljit_s32)srcw;
1460 	} else
1461 		srcw = src;
1462 
1463 	emit_op_imm(compiler, flags | op, dst_r, TMP_REG1, srcw);
1464 
1465 	if (dst & SLJIT_MEM) {
1466 		if (getput_arg_fast(compiler, mem_flags | STORE, dst_r, dst, dstw))
1467 			return compiler->error;
1468 		else
1469 			return getput_arg(compiler, mem_flags | STORE, dst_r, dst, dstw, 0, 0);
1470 	}
1471 	return SLJIT_SUCCESS;
1472 }
1473 
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)1474 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op2(struct sljit_compiler *compiler, sljit_s32 op,
1475 	sljit_s32 dst, sljit_sw dstw,
1476 	sljit_s32 src1, sljit_sw src1w,
1477 	sljit_s32 src2, sljit_sw src2w)
1478 {
1479 	sljit_s32 dst_r, flags, mem_flags;
1480 
1481 	CHECK_ERROR();
1482 	CHECK(check_sljit_emit_op2(compiler, op, dst, dstw, src1, src1w, src2, src2w));
1483 	ADJUST_LOCAL_OFFSET(dst, dstw);
1484 	ADJUST_LOCAL_OFFSET(src1, src1w);
1485 	ADJUST_LOCAL_OFFSET(src2, src2w);
1486 
1487 	compiler->cache_arg = 0;
1488 	compiler->cache_argw = 0;
1489 
1490 	if (dst == SLJIT_UNUSED && !HAS_FLAGS(op))
1491 		return SLJIT_SUCCESS;
1492 
1493 	dst_r = SLOW_IS_REG(dst) ? dst : TMP_REG1;
1494 	flags = HAS_FLAGS(op) ? SET_FLAGS : 0;
1495 	mem_flags = WORD_SIZE;
1496 	if (op & SLJIT_I32_OP) {
1497 		flags |= INT_OP;
1498 		mem_flags = INT_SIZE;
1499 	}
1500 
1501 	if (dst == SLJIT_UNUSED)
1502 		flags |= UNUSED_RETURN;
1503 
1504 	if ((dst & SLJIT_MEM) && !getput_arg_fast(compiler, mem_flags | STORE | ARG_TEST, TMP_REG1, dst, dstw))
1505 		flags |= SLOW_DEST;
1506 
1507 	if (src1 & SLJIT_MEM) {
1508 		if (getput_arg_fast(compiler, mem_flags, TMP_REG1, src1, src1w))
1509 			FAIL_IF(compiler->error);
1510 		else
1511 			flags |= SLOW_SRC1;
1512 	}
1513 	if (src2 & SLJIT_MEM) {
1514 		if (getput_arg_fast(compiler, mem_flags, TMP_REG2, src2, src2w))
1515 			FAIL_IF(compiler->error);
1516 		else
1517 			flags |= SLOW_SRC2;
1518 	}
1519 
1520 	if ((flags & (SLOW_SRC1 | SLOW_SRC2)) == (SLOW_SRC1 | SLOW_SRC2)) {
1521 		if (!can_cache(src1, src1w, src2, src2w) && can_cache(src1, src1w, dst, dstw)) {
1522 			FAIL_IF(getput_arg(compiler, mem_flags, TMP_REG2, src2, src2w, src1, src1w));
1523 			FAIL_IF(getput_arg(compiler, mem_flags, TMP_REG1, src1, src1w, dst, dstw));
1524 		}
1525 		else {
1526 			FAIL_IF(getput_arg(compiler, mem_flags, TMP_REG1, src1, src1w, src2, src2w));
1527 			FAIL_IF(getput_arg(compiler, mem_flags, TMP_REG2, src2, src2w, dst, dstw));
1528 		}
1529 	}
1530 	else if (flags & SLOW_SRC1)
1531 		FAIL_IF(getput_arg(compiler, mem_flags, TMP_REG1, src1, src1w, dst, dstw));
1532 	else if (flags & SLOW_SRC2)
1533 		FAIL_IF(getput_arg(compiler, mem_flags, TMP_REG2, src2, src2w, dst, dstw));
1534 
1535 	if (src1 & SLJIT_MEM)
1536 		src1 = TMP_REG1;
1537 	if (src2 & SLJIT_MEM)
1538 		src2 = TMP_REG2;
1539 
1540 	if (src1 & SLJIT_IMM)
1541 		flags |= ARG1_IMM;
1542 	else
1543 		src1w = src1;
1544 	if (src2 & SLJIT_IMM)
1545 		flags |= ARG2_IMM;
1546 	else
1547 		src2w = src2;
1548 
1549 	emit_op_imm(compiler, flags | GET_OPCODE(op), dst_r, src1w, src2w);
1550 
1551 	if (dst & SLJIT_MEM) {
1552 		if (!(flags & SLOW_DEST)) {
1553 			getput_arg_fast(compiler, mem_flags | STORE, dst_r, dst, dstw);
1554 			return compiler->error;
1555 		}
1556 		return getput_arg(compiler, mem_flags | STORE, TMP_REG1, dst, dstw, 0, 0);
1557 	}
1558 
1559 	return SLJIT_SUCCESS;
1560 }
1561 
sljit_get_register_index(sljit_s32 reg)1562 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_register_index(sljit_s32 reg)
1563 {
1564 	CHECK_REG_INDEX(check_sljit_get_register_index(reg));
1565 	return reg_map[reg];
1566 }
1567 
sljit_get_float_register_index(sljit_s32 reg)1568 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_float_register_index(sljit_s32 reg)
1569 {
1570 	CHECK_REG_INDEX(check_sljit_get_float_register_index(reg));
1571 	return reg;
1572 }
1573 
sljit_emit_op_custom(struct sljit_compiler * compiler,void * instruction,sljit_s32 size)1574 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_custom(struct sljit_compiler *compiler,
1575 	void *instruction, sljit_s32 size)
1576 {
1577 	CHECK_ERROR();
1578 	CHECK(check_sljit_emit_op_custom(compiler, instruction, size));
1579 
1580 	return push_inst(compiler, *(sljit_ins*)instruction);
1581 }
1582 
1583 /* --------------------------------------------------------------------- */
1584 /*  Floating point operators                                             */
1585 /* --------------------------------------------------------------------- */
1586 
emit_fop_mem(struct sljit_compiler * compiler,sljit_s32 flags,sljit_s32 reg,sljit_s32 arg,sljit_sw argw)1587 static sljit_s32 emit_fop_mem(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg, sljit_s32 arg, sljit_sw argw)
1588 {
1589 	sljit_u32 shift = MEM_SIZE_SHIFT(flags);
1590 	sljit_ins ins_bits = (shift << 30);
1591 	sljit_s32 other_r;
1592 	sljit_sw diff;
1593 
1594 	SLJIT_ASSERT(arg & SLJIT_MEM);
1595 
1596 	if (!(flags & STORE))
1597 		ins_bits |= 1 << 22;
1598 
1599 	if (arg & OFFS_REG_MASK) {
1600 		argw &= 3;
1601 		if (!argw || argw == shift)
1602 			return push_inst(compiler, STR_FR | ins_bits | VT(reg)
1603 				| RN(arg & REG_MASK) | RM(OFFS_REG(arg)) | (argw ? (1 << 12) : 0));
1604 		other_r = OFFS_REG(arg);
1605 		arg &= REG_MASK;
1606 		FAIL_IF(push_inst(compiler, ADD | RD(TMP_REG1) | RN(arg) | RM(other_r) | (argw << 10)));
1607 		arg = TMP_REG1;
1608 		argw = 0;
1609 	}
1610 
1611 	arg &= REG_MASK;
1612 	if (arg && argw >= 0 && ((argw >> shift) <= 0xfff) && (argw & ((1 << shift) - 1)) == 0)
1613 		return push_inst(compiler, STR_FI | ins_bits | VT(reg) | RN(arg) | (argw << (10 - shift)));
1614 
1615 	if (arg && argw <= 255 && argw >= -256)
1616 		return push_inst(compiler, STUR_FI | ins_bits | VT(reg) | RN(arg) | ((argw & 0x1ff) << 12));
1617 
1618 	/* Slow cases */
1619 	if (compiler->cache_arg == SLJIT_MEM && argw != compiler->cache_argw) {
1620 		diff = argw - compiler->cache_argw;
1621 		if (!arg && diff <= 255 && diff >= -256)
1622 			return push_inst(compiler, STUR_FI | ins_bits | VT(reg) | RN(TMP_REG3) | ((diff & 0x1ff) << 12));
1623 		if (emit_set_delta(compiler, TMP_REG3, TMP_REG3, argw - compiler->cache_argw) != SLJIT_ERR_UNSUPPORTED) {
1624 			FAIL_IF(compiler->error);
1625 			compiler->cache_argw = argw;
1626 		}
1627 	}
1628 
1629 	if (compiler->cache_arg != SLJIT_MEM || argw != compiler->cache_argw) {
1630 		compiler->cache_arg = SLJIT_MEM;
1631 		compiler->cache_argw = argw;
1632 		FAIL_IF(load_immediate(compiler, TMP_REG3, argw));
1633 	}
1634 
1635 	if (arg & REG_MASK)
1636 		return push_inst(compiler, STR_FR | ins_bits | VT(reg) | RN(arg) | RM(TMP_REG3));
1637 	return push_inst(compiler, STR_FI | ins_bits | VT(reg) | RN(TMP_REG3));
1638 }
1639 
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)1640 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_conv_sw_from_f64(struct sljit_compiler *compiler, sljit_s32 op,
1641 	sljit_s32 dst, sljit_sw dstw,
1642 	sljit_s32 src, sljit_sw srcw)
1643 {
1644 	sljit_s32 dst_r = FAST_IS_REG(dst) ? dst : TMP_REG1;
1645 	sljit_ins inv_bits = (op & SLJIT_F32_OP) ? (1 << 22) : 0;
1646 
1647 	if (GET_OPCODE(op) == SLJIT_CONV_S32_FROM_F64)
1648 		inv_bits |= (1 << 31);
1649 
1650 	if (src & SLJIT_MEM) {
1651 		emit_fop_mem(compiler, (op & SLJIT_F32_OP) ? INT_SIZE : WORD_SIZE, TMP_FREG1, src, srcw);
1652 		src = TMP_FREG1;
1653 	}
1654 
1655 	FAIL_IF(push_inst(compiler, (FCVTZS ^ inv_bits) | RD(dst_r) | VN(src)));
1656 
1657 	if (dst & SLJIT_MEM)
1658 		return emit_op_mem(compiler, ((GET_OPCODE(op) == SLJIT_CONV_S32_FROM_F64) ? INT_SIZE : WORD_SIZE) | STORE, TMP_REG1, dst, dstw);
1659 	return SLJIT_SUCCESS;
1660 }
1661 
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)1662 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_conv_f64_from_sw(struct sljit_compiler *compiler, sljit_s32 op,
1663 	sljit_s32 dst, sljit_sw dstw,
1664 	sljit_s32 src, sljit_sw srcw)
1665 {
1666 	sljit_s32 dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
1667 	sljit_ins inv_bits = (op & SLJIT_F32_OP) ? (1 << 22) : 0;
1668 
1669 	if (GET_OPCODE(op) == SLJIT_CONV_F64_FROM_S32)
1670 		inv_bits |= (1 << 31);
1671 
1672 	if (src & SLJIT_MEM) {
1673 		emit_op_mem(compiler, ((GET_OPCODE(op) == SLJIT_CONV_F64_FROM_S32) ? INT_SIZE : WORD_SIZE), TMP_REG1, src, srcw);
1674 		src = TMP_REG1;
1675 	} else if (src & SLJIT_IMM) {
1676 #if (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64)
1677 		if (GET_OPCODE(op) == SLJIT_CONV_F64_FROM_S32)
1678 			srcw = (sljit_s32)srcw;
1679 #endif
1680 		FAIL_IF(load_immediate(compiler, TMP_REG1, srcw));
1681 		src = TMP_REG1;
1682 	}
1683 
1684 	FAIL_IF(push_inst(compiler, (SCVTF ^ inv_bits) | VD(dst_r) | RN(src)));
1685 
1686 	if (dst & SLJIT_MEM)
1687 		return emit_fop_mem(compiler, ((op & SLJIT_F32_OP) ? INT_SIZE : WORD_SIZE) | STORE, TMP_FREG1, dst, dstw);
1688 	return SLJIT_SUCCESS;
1689 }
1690 
sljit_emit_fop1_cmp(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 src1,sljit_sw src1w,sljit_s32 src2,sljit_sw src2w)1691 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_cmp(struct sljit_compiler *compiler, sljit_s32 op,
1692 	sljit_s32 src1, sljit_sw src1w,
1693 	sljit_s32 src2, sljit_sw src2w)
1694 {
1695 	sljit_s32 mem_flags = (op & SLJIT_F32_OP) ? INT_SIZE : WORD_SIZE;
1696 	sljit_ins inv_bits = (op & SLJIT_F32_OP) ? (1 << 22) : 0;
1697 
1698 	if (src1 & SLJIT_MEM) {
1699 		emit_fop_mem(compiler, mem_flags, TMP_FREG1, src1, src1w);
1700 		src1 = TMP_FREG1;
1701 	}
1702 
1703 	if (src2 & SLJIT_MEM) {
1704 		emit_fop_mem(compiler, mem_flags, TMP_FREG2, src2, src2w);
1705 		src2 = TMP_FREG2;
1706 	}
1707 
1708 	return push_inst(compiler, (FCMP ^ inv_bits) | VN(src1) | VM(src2));
1709 }
1710 
sljit_emit_fop1(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 src,sljit_sw srcw)1711 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop1(struct sljit_compiler *compiler, sljit_s32 op,
1712 	sljit_s32 dst, sljit_sw dstw,
1713 	sljit_s32 src, sljit_sw srcw)
1714 {
1715 	sljit_s32 dst_r, mem_flags = (op & SLJIT_F32_OP) ? INT_SIZE : WORD_SIZE;
1716 	sljit_ins inv_bits;
1717 
1718 	CHECK_ERROR();
1719 	compiler->cache_arg = 0;
1720 	compiler->cache_argw = 0;
1721 
1722 	SLJIT_COMPILE_ASSERT((INT_SIZE ^ 0x100) == WORD_SIZE, must_be_one_bit_difference);
1723 	SELECT_FOP1_OPERATION_WITH_CHECKS(compiler, op, dst, dstw, src, srcw);
1724 
1725 	inv_bits = (op & SLJIT_F32_OP) ? (1 << 22) : 0;
1726 	dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
1727 
1728 	if (src & SLJIT_MEM) {
1729 		emit_fop_mem(compiler, (GET_OPCODE(op) == SLJIT_CONV_F64_FROM_F32) ? (mem_flags ^ 0x100) : mem_flags, dst_r, src, srcw);
1730 		src = dst_r;
1731 	}
1732 
1733 	switch (GET_OPCODE(op)) {
1734 	case SLJIT_MOV_F64:
1735 		if (src != dst_r) {
1736 			if (dst_r != TMP_FREG1)
1737 				FAIL_IF(push_inst(compiler, (FMOV ^ inv_bits) | VD(dst_r) | VN(src)));
1738 			else
1739 				dst_r = src;
1740 		}
1741 		break;
1742 	case SLJIT_NEG_F64:
1743 		FAIL_IF(push_inst(compiler, (FNEG ^ inv_bits) | VD(dst_r) | VN(src)));
1744 		break;
1745 	case SLJIT_ABS_F64:
1746 		FAIL_IF(push_inst(compiler, (FABS ^ inv_bits) | VD(dst_r) | VN(src)));
1747 		break;
1748 	case SLJIT_CONV_F64_FROM_F32:
1749 		FAIL_IF(push_inst(compiler, FCVT | ((op & SLJIT_F32_OP) ? (1 << 22) : (1 << 15)) | VD(dst_r) | VN(src)));
1750 		break;
1751 	}
1752 
1753 	if (dst & SLJIT_MEM)
1754 		return emit_fop_mem(compiler, mem_flags | STORE, dst_r, dst, dstw);
1755 	return SLJIT_SUCCESS;
1756 }
1757 
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)1758 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop2(struct sljit_compiler *compiler, sljit_s32 op,
1759 	sljit_s32 dst, sljit_sw dstw,
1760 	sljit_s32 src1, sljit_sw src1w,
1761 	sljit_s32 src2, sljit_sw src2w)
1762 {
1763 	sljit_s32 dst_r, mem_flags = (op & SLJIT_F32_OP) ? INT_SIZE : WORD_SIZE;
1764 	sljit_ins inv_bits = (op & SLJIT_F32_OP) ? (1 << 22) : 0;
1765 
1766 	CHECK_ERROR();
1767 	CHECK(check_sljit_emit_fop2(compiler, op, dst, dstw, src1, src1w, src2, src2w));
1768 	ADJUST_LOCAL_OFFSET(dst, dstw);
1769 	ADJUST_LOCAL_OFFSET(src1, src1w);
1770 	ADJUST_LOCAL_OFFSET(src2, src2w);
1771 
1772 	compiler->cache_arg = 0;
1773 	compiler->cache_argw = 0;
1774 
1775 	dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
1776 	if (src1 & SLJIT_MEM) {
1777 		emit_fop_mem(compiler, mem_flags, TMP_FREG1, src1, src1w);
1778 		src1 = TMP_FREG1;
1779 	}
1780 	if (src2 & SLJIT_MEM) {
1781 		emit_fop_mem(compiler, mem_flags, TMP_FREG2, src2, src2w);
1782 		src2 = TMP_FREG2;
1783 	}
1784 
1785 	switch (GET_OPCODE(op)) {
1786 	case SLJIT_ADD_F64:
1787 		FAIL_IF(push_inst(compiler, (FADD ^ inv_bits) | VD(dst_r) | VN(src1) | VM(src2)));
1788 		break;
1789 	case SLJIT_SUB_F64:
1790 		FAIL_IF(push_inst(compiler, (FSUB ^ inv_bits) | VD(dst_r) | VN(src1) | VM(src2)));
1791 		break;
1792 	case SLJIT_MUL_F64:
1793 		FAIL_IF(push_inst(compiler, (FMUL ^ inv_bits) | VD(dst_r) | VN(src1) | VM(src2)));
1794 		break;
1795 	case SLJIT_DIV_F64:
1796 		FAIL_IF(push_inst(compiler, (FDIV ^ inv_bits) | VD(dst_r) | VN(src1) | VM(src2)));
1797 		break;
1798 	}
1799 
1800 	if (!(dst & SLJIT_MEM))
1801 		return SLJIT_SUCCESS;
1802 	return emit_fop_mem(compiler, mem_flags | STORE, TMP_FREG1, dst, dstw);
1803 }
1804 
1805 /* --------------------------------------------------------------------- */
1806 /*  Other instructions                                                   */
1807 /* --------------------------------------------------------------------- */
1808 
sljit_emit_fast_enter(struct sljit_compiler * compiler,sljit_s32 dst,sljit_sw dstw)1809 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fast_enter(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw)
1810 {
1811 	CHECK_ERROR();
1812 	CHECK(check_sljit_emit_fast_enter(compiler, dst, dstw));
1813 	ADJUST_LOCAL_OFFSET(dst, dstw);
1814 
1815 	if (FAST_IS_REG(dst))
1816 		return push_inst(compiler, ORR | RD(dst) | RN(TMP_ZERO) | RM(TMP_LR));
1817 
1818 	/* Memory. */
1819 	return emit_op_mem(compiler, WORD_SIZE | STORE, TMP_LR, dst, dstw);
1820 }
1821 
sljit_emit_fast_return(struct sljit_compiler * compiler,sljit_s32 src,sljit_sw srcw)1822 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fast_return(struct sljit_compiler *compiler, sljit_s32 src, sljit_sw srcw)
1823 {
1824 	CHECK_ERROR();
1825 	CHECK(check_sljit_emit_fast_return(compiler, src, srcw));
1826 	ADJUST_LOCAL_OFFSET(src, srcw);
1827 
1828 	if (FAST_IS_REG(src))
1829 		FAIL_IF(push_inst(compiler, ORR | RD(TMP_LR) | RN(TMP_ZERO) | RM(src)));
1830 	else if (src & SLJIT_MEM)
1831 		FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_LR, src, srcw));
1832 	else if (src & SLJIT_IMM)
1833 		FAIL_IF(load_immediate(compiler, TMP_LR, srcw));
1834 
1835 	return push_inst(compiler, RET | RN(TMP_LR));
1836 }
1837 
1838 /* --------------------------------------------------------------------- */
1839 /*  Conditional instructions                                             */
1840 /* --------------------------------------------------------------------- */
1841 
get_cc(sljit_s32 type)1842 static sljit_uw get_cc(sljit_s32 type)
1843 {
1844 	switch (type) {
1845 	case SLJIT_EQUAL:
1846 	case SLJIT_MUL_NOT_OVERFLOW:
1847 	case SLJIT_EQUAL_F64:
1848 		return 0x1;
1849 
1850 	case SLJIT_NOT_EQUAL:
1851 	case SLJIT_MUL_OVERFLOW:
1852 	case SLJIT_NOT_EQUAL_F64:
1853 		return 0x0;
1854 
1855 	case SLJIT_LESS:
1856 	case SLJIT_LESS_F64:
1857 		return 0x2;
1858 
1859 	case SLJIT_GREATER_EQUAL:
1860 	case SLJIT_GREATER_EQUAL_F64:
1861 		return 0x3;
1862 
1863 	case SLJIT_GREATER:
1864 	case SLJIT_GREATER_F64:
1865 		return 0x9;
1866 
1867 	case SLJIT_LESS_EQUAL:
1868 	case SLJIT_LESS_EQUAL_F64:
1869 		return 0x8;
1870 
1871 	case SLJIT_SIG_LESS:
1872 		return 0xa;
1873 
1874 	case SLJIT_SIG_GREATER_EQUAL:
1875 		return 0xb;
1876 
1877 	case SLJIT_SIG_GREATER:
1878 		return 0xd;
1879 
1880 	case SLJIT_SIG_LESS_EQUAL:
1881 		return 0xc;
1882 
1883 	case SLJIT_OVERFLOW:
1884 	case SLJIT_UNORDERED_F64:
1885 		return 0x7;
1886 
1887 	case SLJIT_NOT_OVERFLOW:
1888 	case SLJIT_ORDERED_F64:
1889 		return 0x6;
1890 
1891 	default:
1892 		SLJIT_UNREACHABLE();
1893 		return 0xe;
1894 	}
1895 }
1896 
sljit_emit_label(struct sljit_compiler * compiler)1897 SLJIT_API_FUNC_ATTRIBUTE struct sljit_label* sljit_emit_label(struct sljit_compiler *compiler)
1898 {
1899 	struct sljit_label *label;
1900 
1901 	CHECK_ERROR_PTR();
1902 	CHECK_PTR(check_sljit_emit_label(compiler));
1903 
1904 	if (compiler->last_label && compiler->last_label->size == compiler->size)
1905 		return compiler->last_label;
1906 
1907 	label = (struct sljit_label*)ensure_abuf(compiler, sizeof(struct sljit_label));
1908 	PTR_FAIL_IF(!label);
1909 	set_label(label, compiler);
1910 	return label;
1911 }
1912 
sljit_emit_jump(struct sljit_compiler * compiler,sljit_s32 type)1913 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_jump(struct sljit_compiler *compiler, sljit_s32 type)
1914 {
1915 	struct sljit_jump *jump;
1916 
1917 	CHECK_ERROR_PTR();
1918 	CHECK_PTR(check_sljit_emit_jump(compiler, type));
1919 
1920 	jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1921 	PTR_FAIL_IF(!jump);
1922 	set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
1923 	type &= 0xff;
1924 
1925 	if (type < SLJIT_JUMP) {
1926 		jump->flags |= IS_COND;
1927 		PTR_FAIL_IF(push_inst(compiler, B_CC | (6 << 5) | get_cc(type)));
1928 	}
1929 	else if (type >= SLJIT_FAST_CALL)
1930 		jump->flags |= IS_BL;
1931 
1932 	PTR_FAIL_IF(emit_imm64_const(compiler, TMP_REG1, 0));
1933 	jump->addr = compiler->size;
1934 	PTR_FAIL_IF(push_inst(compiler, ((type >= SLJIT_FAST_CALL) ? BLR : BR) | RN(TMP_REG1)));
1935 
1936 	return jump;
1937 }
1938 
emit_cmp_to0(struct sljit_compiler * compiler,sljit_s32 type,sljit_s32 src,sljit_sw srcw)1939 static SLJIT_INLINE struct sljit_jump* emit_cmp_to0(struct sljit_compiler *compiler, sljit_s32 type,
1940 	sljit_s32 src, sljit_sw srcw)
1941 {
1942 	struct sljit_jump *jump;
1943 	sljit_ins inv_bits = (type & SLJIT_I32_OP) ? (1 << 31) : 0;
1944 
1945 	SLJIT_ASSERT((type & 0xff) == SLJIT_EQUAL || (type & 0xff) == SLJIT_NOT_EQUAL);
1946 	ADJUST_LOCAL_OFFSET(src, srcw);
1947 
1948 	jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1949 	PTR_FAIL_IF(!jump);
1950 	set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
1951 	jump->flags |= IS_CBZ | IS_COND;
1952 
1953 	if (src & SLJIT_MEM) {
1954 		PTR_FAIL_IF(emit_op_mem(compiler, inv_bits ? INT_SIZE : WORD_SIZE, TMP_REG1, src, srcw));
1955 		src = TMP_REG1;
1956 	}
1957 	else if (src & SLJIT_IMM) {
1958 		PTR_FAIL_IF(load_immediate(compiler, TMP_REG1, srcw));
1959 		src = TMP_REG1;
1960 	}
1961 	SLJIT_ASSERT(FAST_IS_REG(src));
1962 
1963 	if ((type & 0xff) == SLJIT_EQUAL)
1964 		inv_bits |= 1 << 24;
1965 
1966 	PTR_FAIL_IF(push_inst(compiler, (CBZ ^ inv_bits) | (6 << 5) | RT(src)));
1967 	PTR_FAIL_IF(emit_imm64_const(compiler, TMP_REG1, 0));
1968 	jump->addr = compiler->size;
1969 	PTR_FAIL_IF(push_inst(compiler, BR | RN(TMP_REG1)));
1970 	return jump;
1971 }
1972 
sljit_emit_ijump(struct sljit_compiler * compiler,sljit_s32 type,sljit_s32 src,sljit_sw srcw)1973 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_ijump(struct sljit_compiler *compiler, sljit_s32 type, sljit_s32 src, sljit_sw srcw)
1974 {
1975 	struct sljit_jump *jump;
1976 
1977 	CHECK_ERROR();
1978 	CHECK(check_sljit_emit_ijump(compiler, type, src, srcw));
1979 	ADJUST_LOCAL_OFFSET(src, srcw);
1980 
1981 	/* In ARM, we don't need to touch the arguments. */
1982 	if (!(src & SLJIT_IMM)) {
1983 		if (src & SLJIT_MEM) {
1984 			FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG1, src, srcw));
1985 			src = TMP_REG1;
1986 		}
1987 		return push_inst(compiler, ((type >= SLJIT_FAST_CALL) ? BLR : BR) | RN(src));
1988 	}
1989 
1990 	jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1991 	FAIL_IF(!jump);
1992 	set_jump(jump, compiler, JUMP_ADDR | ((type >= SLJIT_FAST_CALL) ? IS_BL : 0));
1993 	jump->u.target = srcw;
1994 
1995 	FAIL_IF(emit_imm64_const(compiler, TMP_REG1, 0));
1996 	jump->addr = compiler->size;
1997 	return push_inst(compiler, ((type >= SLJIT_FAST_CALL) ? BLR : BR) | RN(TMP_REG1));
1998 }
1999 
sljit_emit_op_flags(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 type)2000 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_flags(struct sljit_compiler *compiler, sljit_s32 op,
2001 	sljit_s32 dst, sljit_sw dstw,
2002 	sljit_s32 type)
2003 {
2004 	sljit_s32 dst_r, src_r, flags, mem_flags;
2005 	sljit_ins cc;
2006 
2007 	CHECK_ERROR();
2008 	CHECK(check_sljit_emit_op_flags(compiler, op, dst, dstw, type));
2009 	ADJUST_LOCAL_OFFSET(dst, dstw);
2010 
2011 	cc = get_cc(type & 0xff);
2012 	dst_r = FAST_IS_REG(dst) ? dst : TMP_REG1;
2013 
2014 	if (GET_OPCODE(op) < SLJIT_ADD) {
2015 		FAIL_IF(push_inst(compiler, CSINC | (cc << 12) | RD(dst_r) | RN(TMP_ZERO) | RM(TMP_ZERO)));
2016 		if (dst_r != TMP_REG1)
2017 			return SLJIT_SUCCESS;
2018 		return emit_op_mem(compiler, (GET_OPCODE(op) == SLJIT_MOV ? WORD_SIZE : INT_SIZE) | STORE, TMP_REG1, dst, dstw);
2019 	}
2020 
2021 	compiler->cache_arg = 0;
2022 	compiler->cache_argw = 0;
2023 	flags = HAS_FLAGS(op) ? SET_FLAGS : 0;
2024 	mem_flags = WORD_SIZE;
2025 	if (op & SLJIT_I32_OP) {
2026 		flags |= INT_OP;
2027 		mem_flags = INT_SIZE;
2028 	}
2029 
2030 	src_r = dst;
2031 
2032 	if (dst & SLJIT_MEM) {
2033 		FAIL_IF(emit_op_mem2(compiler, mem_flags, TMP_REG1, dst, dstw, dst, dstw));
2034 		src_r = TMP_REG1;
2035 	}
2036 
2037 	FAIL_IF(push_inst(compiler, CSINC | (cc << 12) | RD(TMP_REG2) | RN(TMP_ZERO) | RM(TMP_ZERO)));
2038 	emit_op_imm(compiler, flags | GET_OPCODE(op), dst_r, src_r, TMP_REG2);
2039 
2040 	if (dst & SLJIT_MEM)
2041 		return emit_op_mem2(compiler, mem_flags | STORE, TMP_REG1, dst, dstw, 0, 0);
2042 	return SLJIT_SUCCESS;
2043 }
2044 
sljit_emit_cmov(struct sljit_compiler * compiler,sljit_s32 type,sljit_s32 dst_reg,sljit_s32 src,sljit_sw srcw)2045 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_cmov(struct sljit_compiler *compiler, sljit_s32 type,
2046 	sljit_s32 dst_reg,
2047 	sljit_s32 src, sljit_sw srcw)
2048 {
2049 	sljit_ins inv_bits = (dst_reg & SLJIT_I32_OP) ? (1 << 31) : 0;
2050 	sljit_ins cc;
2051 
2052 	CHECK_ERROR();
2053 	CHECK(check_sljit_emit_cmov(compiler, type, dst_reg, src, srcw));
2054 
2055 	if (SLJIT_UNLIKELY(src & SLJIT_IMM)) {
2056 		if (dst_reg & SLJIT_I32_OP)
2057 			srcw = (sljit_s32)srcw;
2058 		FAIL_IF(load_immediate(compiler, TMP_REG1, srcw));
2059 		src = TMP_REG1;
2060 		srcw = 0;
2061 	}
2062 
2063 	cc = get_cc(type & 0xff);
2064 	dst_reg &= ~SLJIT_I32_OP;
2065 
2066 	return push_inst(compiler, (CSEL ^ inv_bits) | (cc << 12) | RD(dst_reg) | RN(dst_reg) | RM(src));
2067 }
2068 
sljit_emit_const(struct sljit_compiler * compiler,sljit_s32 dst,sljit_sw dstw,sljit_sw init_value)2069 SLJIT_API_FUNC_ATTRIBUTE struct sljit_const* sljit_emit_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw, sljit_sw init_value)
2070 {
2071 	struct sljit_const *const_;
2072 	sljit_s32 dst_r;
2073 
2074 	CHECK_ERROR_PTR();
2075 	CHECK_PTR(check_sljit_emit_const(compiler, dst, dstw, init_value));
2076 	ADJUST_LOCAL_OFFSET(dst, dstw);
2077 
2078 	const_ = (struct sljit_const*)ensure_abuf(compiler, sizeof(struct sljit_const));
2079 	PTR_FAIL_IF(!const_);
2080 	set_const(const_, compiler);
2081 
2082 	dst_r = FAST_IS_REG(dst) ? dst : TMP_REG1;
2083 	PTR_FAIL_IF(emit_imm64_const(compiler, dst_r, init_value));
2084 
2085 	if (dst & SLJIT_MEM)
2086 		PTR_FAIL_IF(emit_op_mem(compiler, WORD_SIZE | STORE, dst_r, dst, dstw));
2087 	return const_;
2088 }
2089 
sljit_set_jump_addr(sljit_uw addr,sljit_uw new_target,sljit_sw executable_offset)2090 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_target, sljit_sw executable_offset)
2091 {
2092 	sljit_ins* inst = (sljit_ins*)addr;
2093 	modify_imm64_const(inst, new_target);
2094 	inst = (sljit_ins *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
2095 	SLJIT_CACHE_FLUSH(inst, inst + 4);
2096 }
2097 
sljit_set_const(sljit_uw addr,sljit_sw new_constant,sljit_sw executable_offset)2098 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_const(sljit_uw addr, sljit_sw new_constant, sljit_sw executable_offset)
2099 {
2100 	sljit_ins* inst = (sljit_ins*)addr;
2101 	modify_imm64_const(inst, new_constant);
2102 	inst = (sljit_ins *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
2103 	SLJIT_CACHE_FLUSH(inst, inst + 4);
2104 }
2105