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