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