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