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