Home | History | Annotate | Line # | Download | only in sljit_src
sljitNativeARM_64.c revision 1.1.1.3
      1 /*
      2  *    Stack-less Just-In-Time compiler
      3  *
      4  *    Copyright Zoltan Herczeg (hzmester (at) 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 
     27 SLJIT_API_FUNC_ATTRIBUTE const char* sljit_get_platform_name(void)
     28 {
     29 	return "ARM-64" SLJIT_CPUINFO;
     30 }
     31 
     32 /* Length of an instruction word */
     33 typedef sljit_u32 sljit_ins;
     34 
     35 #define TMP_ZERO	(0)
     36 
     37 #define TMP_REG1	(SLJIT_NUMBER_OF_REGISTERS + 2)
     38 #define TMP_REG2	(SLJIT_NUMBER_OF_REGISTERS + 3)
     39 #define TMP_REG3	(SLJIT_NUMBER_OF_REGISTERS + 4)
     40 #define TMP_LR		(SLJIT_NUMBER_OF_REGISTERS + 5)
     41 #define TMP_SP		(SLJIT_NUMBER_OF_REGISTERS + 6)
     42 
     43 #define TMP_FREG1	(0)
     44 #define TMP_FREG2	(SLJIT_NUMBER_OF_FLOAT_REGISTERS + 1)
     45 
     46 static const sljit_u8 reg_map[SLJIT_NUMBER_OF_REGISTERS + 8] = {
     47   31, 0, 1, 2, 3, 4, 5, 6, 7, 12, 13, 14, 15, 16, 17, 8, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 29, 9, 10, 11, 30, 31
     48 };
     49 
     50 #define W_OP (1 << 31)
     51 #define RD(rd) (reg_map[rd])
     52 #define RT(rt) (reg_map[rt])
     53 #define RN(rn) (reg_map[rn] << 5)
     54 #define RT2(rt2) (reg_map[rt2] << 10)
     55 #define RM(rm) (reg_map[rm] << 16)
     56 #define VD(vd) (vd)
     57 #define VT(vt) (vt)
     58 #define VN(vn) ((vn) << 5)
     59 #define VM(vm) ((vm) << 16)
     60 
     61 /* --------------------------------------------------------------------- */
     62 /*  Instrucion forms                                                     */
     63 /* --------------------------------------------------------------------- */
     64 
     65 #define ADC 0x9a000000
     66 #define ADD 0x8b000000
     67 #define ADDI 0x91000000
     68 #define AND 0x8a000000
     69 #define ANDI 0x92000000
     70 #define ASRV 0x9ac02800
     71 #define B 0x14000000
     72 #define B_CC 0x54000000
     73 #define BL 0x94000000
     74 #define BLR 0xd63f0000
     75 #define BR 0xd61f0000
     76 #define BRK 0xd4200000
     77 #define CBZ 0xb4000000
     78 #define CLZ 0xdac01000
     79 #define CSINC 0x9a800400
     80 #define EOR 0xca000000
     81 #define EORI 0xd2000000
     82 #define FABS 0x1e60c000
     83 #define FADD 0x1e602800
     84 #define FCMP 0x1e602000
     85 #define FCVT 0x1e224000
     86 #define FCVTZS 0x9e780000
     87 #define FDIV 0x1e601800
     88 #define FMOV 0x1e604000
     89 #define FMUL 0x1e600800
     90 #define FNEG 0x1e614000
     91 #define FSUB 0x1e603800
     92 #define LDRI 0xf9400000
     93 #define LDP 0xa9400000
     94 #define LDP_PST 0xa8c00000
     95 #define LSLV 0x9ac02000
     96 #define LSRV 0x9ac02400
     97 #define MADD 0x9b000000
     98 #define MOVK 0xf2800000
     99 #define MOVN 0x92800000
    100 #define MOVZ 0xd2800000
    101 #define NOP 0xd503201f
    102 #define ORN 0xaa200000
    103 #define ORR 0xaa000000
    104 #define ORRI 0xb2000000
    105 #define RET 0xd65f0000
    106 #define SBC 0xda000000
    107 #define SBFM 0x93000000
    108 #define SCVTF 0x9e620000
    109 #define SDIV 0x9ac00c00
    110 #define SMADDL 0x9b200000
    111 #define SMULH 0x9b403c00
    112 #define STP 0xa9000000
    113 #define STP_PRE 0xa9800000
    114 #define STRI 0xf9000000
    115 #define STR_FI 0x3d000000
    116 #define STR_FR 0x3c206800
    117 #define STUR_FI 0x3c000000
    118 #define SUB 0xcb000000
    119 #define SUBI 0xd1000000
    120 #define SUBS 0xeb000000
    121 #define UBFM 0xd3000000
    122 #define UDIV 0x9ac00800
    123 #define UMULH 0x9bc03c00
    124 
    125 /* dest_reg is the absolute name of the register
    126    Useful for reordering instructions in the delay slot. */
    127 static sljit_s32 push_inst(struct sljit_compiler *compiler, sljit_ins ins)
    128 {
    129 	sljit_ins *ptr = (sljit_ins*)ensure_buf(compiler, sizeof(sljit_ins));
    130 	FAIL_IF(!ptr);
    131 	*ptr = ins;
    132 	compiler->size++;
    133 	return SLJIT_SUCCESS;
    134 }
    135 
    136 static SLJIT_INLINE sljit_s32 emit_imm64_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_uw imm)
    137 {
    138 	FAIL_IF(push_inst(compiler, MOVZ | RD(dst) | ((imm & 0xffff) << 5)));
    139 	FAIL_IF(push_inst(compiler, MOVK | RD(dst) | (((imm >> 16) & 0xffff) << 5) | (1 << 21)));
    140 	FAIL_IF(push_inst(compiler, MOVK | RD(dst) | (((imm >> 32) & 0xffff) << 5) | (2 << 21)));
    141 	return push_inst(compiler, MOVK | RD(dst) | ((imm >> 48) << 5) | (3 << 21));
    142 }
    143 
    144 static SLJIT_INLINE void modify_imm64_const(sljit_ins* inst, sljit_uw new_imm)
    145 {
    146 	sljit_s32 dst = inst[0] & 0x1f;
    147 	SLJIT_ASSERT((inst[0] & 0xffe00000) == MOVZ && (inst[1] & 0xffe00000) == (MOVK | (1 << 21)));
    148 	inst[0] = MOVZ | dst | ((new_imm & 0xffff) << 5);
    149 	inst[1] = MOVK | dst | (((new_imm >> 16) & 0xffff) << 5) | (1 << 21);
    150 	inst[2] = MOVK | dst | (((new_imm >> 32) & 0xffff) << 5) | (2 << 21);
    151 	inst[3] = MOVK | dst | ((new_imm >> 48) << 5) | (3 << 21);
    152 }
    153 
    154 static SLJIT_INLINE sljit_s32 detect_jump_type(struct sljit_jump *jump, sljit_ins *code_ptr, sljit_ins *code, sljit_sw executable_offset)
    155 {
    156 	sljit_sw diff;
    157 	sljit_uw target_addr;
    158 
    159 	if (jump->flags & SLJIT_REWRITABLE_JUMP) {
    160 		jump->flags |= PATCH_ABS64;
    161 		return 0;
    162 	}
    163 
    164 	if (jump->flags & JUMP_ADDR)
    165 		target_addr = jump->u.target;
    166 	else {
    167 		SLJIT_ASSERT(jump->flags & JUMP_LABEL);
    168 		target_addr = (sljit_uw)(code + jump->u.label->size) + (sljit_uw)executable_offset;
    169 	}
    170 
    171 	diff = (sljit_sw)target_addr - (sljit_sw)(code_ptr + 4) - executable_offset;
    172 
    173 	if (jump->flags & IS_COND) {
    174 		diff += sizeof(sljit_ins);
    175 		if (diff <= 0xfffff && diff >= -0x100000) {
    176 			code_ptr[-5] ^= (jump->flags & IS_CBZ) ? (0x1 << 24) : 0x1;
    177 			jump->addr -= sizeof(sljit_ins);
    178 			jump->flags |= PATCH_COND;
    179 			return 5;
    180 		}
    181 		diff -= sizeof(sljit_ins);
    182 	}
    183 
    184 	if (diff <= 0x7ffffff && diff >= -0x8000000) {
    185 		jump->flags |= PATCH_B;
    186 		return 4;
    187 	}
    188 
    189 	if (target_addr <= 0xffffffffl) {
    190 		if (jump->flags & IS_COND)
    191 			code_ptr[-5] -= (2 << 5);
    192 		code_ptr[-2] = code_ptr[0];
    193 		return 2;
    194 	}
    195 	if (target_addr <= 0xffffffffffffl) {
    196 		if (jump->flags & IS_COND)
    197 			code_ptr[-5] -= (1 << 5);
    198 		jump->flags |= PATCH_ABS48;
    199 		code_ptr[-1] = code_ptr[0];
    200 		return 1;
    201 	}
    202 
    203 	jump->flags |= PATCH_ABS64;
    204 	return 0;
    205 }
    206 
    207 SLJIT_API_FUNC_ATTRIBUTE void* sljit_generate_code(struct sljit_compiler *compiler)
    208 {
    209 	struct sljit_memory_fragment *buf;
    210 	sljit_ins *code;
    211 	sljit_ins *code_ptr;
    212 	sljit_ins *buf_ptr;
    213 	sljit_ins *buf_end;
    214 	sljit_uw word_count;
    215 	sljit_sw executable_offset;
    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 	executable_offset = SLJIT_EXEC_OFFSET(code);
    234 
    235 	label = compiler->labels;
    236 	jump = compiler->jumps;
    237 	const_ = compiler->consts;
    238 
    239 	do {
    240 		buf_ptr = (sljit_ins*)buf->memory;
    241 		buf_end = buf_ptr + (buf->used_size >> 2);
    242 		do {
    243 			*code_ptr = *buf_ptr++;
    244 			/* These structures are ordered by their address. */
    245 			SLJIT_ASSERT(!label || label->size >= word_count);
    246 			SLJIT_ASSERT(!jump || jump->addr >= word_count);
    247 			SLJIT_ASSERT(!const_ || const_->addr >= word_count);
    248 			if (label && label->size == word_count) {
    249 				label->addr = (sljit_uw)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset);
    250 				label->size = code_ptr - code;
    251 				label = label->next;
    252 			}
    253 			if (jump && jump->addr == word_count) {
    254 					jump->addr = (sljit_uw)(code_ptr - 4);
    255 					code_ptr -= detect_jump_type(jump, code_ptr, code, executable_offset);
    256 					jump = jump->next;
    257 			}
    258 			if (const_ && const_->addr == word_count) {
    259 				const_->addr = (sljit_uw)code_ptr;
    260 				const_ = const_->next;
    261 			}
    262 			code_ptr ++;
    263 			word_count ++;
    264 		} while (buf_ptr < buf_end);
    265 
    266 		buf = buf->next;
    267 	} while (buf);
    268 
    269 	if (label && label->size == word_count) {
    270 		label->addr = (sljit_uw)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset);
    271 		label->size = code_ptr - code;
    272 		label = label->next;
    273 	}
    274 
    275 	SLJIT_ASSERT(!label);
    276 	SLJIT_ASSERT(!jump);
    277 	SLJIT_ASSERT(!const_);
    278 	SLJIT_ASSERT(code_ptr - code <= (sljit_sw)compiler->size);
    279 
    280 	jump = compiler->jumps;
    281 	while (jump) {
    282 		do {
    283 			addr = (jump->flags & JUMP_LABEL) ? jump->u.label->addr : jump->u.target;
    284 			buf_ptr = (sljit_ins *)jump->addr;
    285 
    286 			if (jump->flags & PATCH_B) {
    287 				addr = (sljit_sw)(addr - (sljit_uw)SLJIT_ADD_EXEC_OFFSET(buf_ptr, executable_offset)) >> 2;
    288 				SLJIT_ASSERT((sljit_sw)addr <= 0x1ffffff && (sljit_sw)addr >= -0x2000000);
    289 				buf_ptr[0] = ((jump->flags & IS_BL) ? BL : B) | (addr & 0x3ffffff);
    290 				if (jump->flags & IS_COND)
    291 					buf_ptr[-1] -= (4 << 5);
    292 				break;
    293 			}
    294 			if (jump->flags & PATCH_COND) {
    295 				addr = (sljit_sw)(addr - (sljit_uw)SLJIT_ADD_EXEC_OFFSET(buf_ptr, executable_offset)) >> 2;
    296 				SLJIT_ASSERT((sljit_sw)addr <= 0x3ffff && (sljit_sw)addr >= -0x40000);
    297 				buf_ptr[0] = (buf_ptr[0] & ~0xffffe0) | ((addr & 0x7ffff) << 5);
    298 				break;
    299 			}
    300 
    301 			SLJIT_ASSERT((jump->flags & (PATCH_ABS48 | PATCH_ABS64)) || addr <= 0xffffffffl);
    302 			SLJIT_ASSERT((jump->flags & PATCH_ABS64) || addr <= 0xffffffffffffl);
    303 
    304 			dst = buf_ptr[0] & 0x1f;
    305 			buf_ptr[0] = MOVZ | dst | ((addr & 0xffff) << 5);
    306 			buf_ptr[1] = MOVK | dst | (((addr >> 16) & 0xffff) << 5) | (1 << 21);
    307 			if (jump->flags & (PATCH_ABS48 | PATCH_ABS64))
    308 				buf_ptr[2] = MOVK | dst | (((addr >> 32) & 0xffff) << 5) | (2 << 21);
    309 			if (jump->flags & PATCH_ABS64)
    310 				buf_ptr[3] = MOVK | dst | (((addr >> 48) & 0xffff) << 5) | (3 << 21);
    311 		} while (0);
    312 		jump = jump->next;
    313 	}
    314 
    315 	compiler->error = SLJIT_ERR_COMPILED;
    316 	compiler->executable_offset = executable_offset;
    317 	compiler->executable_size = (code_ptr - code) * sizeof(sljit_ins);
    318 
    319 	code = (sljit_ins *)SLJIT_ADD_EXEC_OFFSET(code, executable_offset);
    320 	code_ptr = (sljit_ins *)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset);
    321 
    322 	SLJIT_CACHE_FLUSH(code, code_ptr);
    323 	return code;
    324 }
    325 
    326 /* --------------------------------------------------------------------- */
    327 /*  Core code generator functions.                                       */
    328 /* --------------------------------------------------------------------- */
    329 
    330 #define COUNT_TRAILING_ZERO(value, result) \
    331 	result = 0; \
    332 	if (!(value & 0xffffffff)) { \
    333 		result += 32; \
    334 		value >>= 32; \
    335 	} \
    336 	if (!(value & 0xffff)) { \
    337 		result += 16; \
    338 		value >>= 16; \
    339 	} \
    340 	if (!(value & 0xff)) { \
    341 		result += 8; \
    342 		value >>= 8; \
    343 	} \
    344 	if (!(value & 0xf)) { \
    345 		result += 4; \
    346 		value >>= 4; \
    347 	} \
    348 	if (!(value & 0x3)) { \
    349 		result += 2; \
    350 		value >>= 2; \
    351 	} \
    352 	if (!(value & 0x1)) { \
    353 		result += 1; \
    354 		value >>= 1; \
    355 	}
    356 
    357 #define LOGICAL_IMM_CHECK 0x100
    358 
    359 static sljit_ins logical_imm(sljit_sw imm, sljit_s32 len)
    360 {
    361 	sljit_s32 negated, ones, right;
    362 	sljit_uw mask, uimm;
    363 	sljit_ins ins;
    364 
    365 	if (len & LOGICAL_IMM_CHECK) {
    366 		len &= ~LOGICAL_IMM_CHECK;
    367 		if (len == 32 && (imm == 0 || imm == -1))
    368 			return 0;
    369 		if (len == 16 && ((sljit_s32)imm == 0 || (sljit_s32)imm == -1))
    370 			return 0;
    371 	}
    372 
    373 	SLJIT_ASSERT((len == 32 && imm != 0 && imm != -1)
    374 		|| (len == 16 && (sljit_s32)imm != 0 && (sljit_s32)imm != -1));
    375 	uimm = (sljit_uw)imm;
    376 	while (1) {
    377 		if (len <= 0) {
    378 			SLJIT_UNREACHABLE();
    379 			return 0;
    380 		}
    381 		mask = ((sljit_uw)1 << len) - 1;
    382 		if ((uimm & mask) != ((uimm >> len) & mask))
    383 			break;
    384 		len >>= 1;
    385 	}
    386 
    387 	len <<= 1;
    388 
    389 	negated = 0;
    390 	if (uimm & 0x1) {
    391 		negated = 1;
    392 		uimm = ~uimm;
    393 	}
    394 
    395 	if (len < 64)
    396 		uimm &= ((sljit_uw)1 << len) - 1;
    397 
    398 	/* Unsigned right shift. */
    399 	COUNT_TRAILING_ZERO(uimm, right);
    400 
    401 	/* Signed shift. We also know that the highest bit is set. */
    402 	imm = (sljit_sw)~uimm;
    403 	SLJIT_ASSERT(imm < 0);
    404 
    405 	COUNT_TRAILING_ZERO(imm, ones);
    406 
    407 	if (~imm)
    408 		return 0;
    409 
    410 	if (len == 64)
    411 		ins = 1 << 22;
    412 	else
    413 		ins = (0x3f - ((len << 1) - 1)) << 10;
    414 
    415 	if (negated)
    416 		return ins | ((len - ones - 1) << 10) | ((len - ones - right) << 16);
    417 
    418 	return ins | ((ones - 1) << 10) | ((len - right) << 16);
    419 }
    420 
    421 #undef COUNT_TRAILING_ZERO
    422 
    423 static sljit_s32 load_immediate(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw simm)
    424 {
    425 	sljit_uw imm = (sljit_uw)simm;
    426 	sljit_s32 i, zeros, ones, first;
    427 	sljit_ins bitmask;
    428 
    429 	if (imm <= 0xffff)
    430 		return push_inst(compiler, MOVZ | RD(dst) | (imm << 5));
    431 
    432 	if (simm >= -0x10000 && simm < 0)
    433 		return push_inst(compiler, MOVN | RD(dst) | ((~imm & 0xffff) << 5));
    434 
    435 	if (imm <= 0xffffffffl) {
    436 		if ((imm & 0xffff0000l) == 0xffff0000)
    437 			return push_inst(compiler, (MOVN ^ W_OP) | RD(dst) | ((~imm & 0xffff) << 5));
    438 		if ((imm & 0xffff) == 0xffff)
    439 			return push_inst(compiler, (MOVN ^ W_OP) | RD(dst) | ((~imm & 0xffff0000l) >> (16 - 5)) | (1 << 21));
    440 		bitmask = logical_imm(simm, 16);
    441 		if (bitmask != 0)
    442 			return push_inst(compiler, (ORRI ^ W_OP) | RD(dst) | RN(TMP_ZERO) | bitmask);
    443 	}
    444 	else {
    445 		bitmask = logical_imm(simm, 32);
    446 		if (bitmask != 0)
    447 			return push_inst(compiler, ORRI | RD(dst) | RN(TMP_ZERO) | bitmask);
    448 	}
    449 
    450 	if (imm <= 0xffffffffl) {
    451 		FAIL_IF(push_inst(compiler, MOVZ | RD(dst) | ((imm & 0xffff) << 5)));
    452 		return push_inst(compiler, MOVK | RD(dst) | ((imm & 0xffff0000l) >> (16 - 5)) | (1 << 21));
    453 	}
    454 
    455 	if (simm >= -0x100000000l && simm < 0) {
    456 		FAIL_IF(push_inst(compiler, MOVN | RD(dst) | ((~imm & 0xffff) << 5)));
    457 		return push_inst(compiler, MOVK | RD(dst) | ((imm & 0xffff0000l) >> (16 - 5)) | (1 << 21));
    458 	}
    459 
    460 	/* A large amount of number can be constructed from ORR and MOVx,
    461 	but computing them is costly. We don't  */
    462 
    463 	zeros = 0;
    464 	ones = 0;
    465 	for (i = 4; i > 0; i--) {
    466 		if ((simm & 0xffff) == 0)
    467 			zeros++;
    468 		if ((simm & 0xffff) == 0xffff)
    469 			ones++;
    470 		simm >>= 16;
    471 	}
    472 
    473 	simm = (sljit_sw)imm;
    474 	first = 1;
    475 	if (ones > zeros) {
    476 		simm = ~simm;
    477 		for (i = 0; i < 4; i++) {
    478 			if (!(simm & 0xffff)) {
    479 				simm >>= 16;
    480 				continue;
    481 			}
    482 			if (first) {
    483 				first = 0;
    484 				FAIL_IF(push_inst(compiler, MOVN | RD(dst) | ((simm & 0xffff) << 5) | (i << 21)));
    485 			}
    486 			else
    487 				FAIL_IF(push_inst(compiler, MOVK | RD(dst) | ((~simm & 0xffff) << 5) | (i << 21)));
    488 			simm >>= 16;
    489 		}
    490 		return SLJIT_SUCCESS;
    491 	}
    492 
    493 	for (i = 0; i < 4; i++) {
    494 		if (!(simm & 0xffff)) {
    495 			simm >>= 16;
    496 			continue;
    497 		}
    498 		if (first) {
    499 			first = 0;
    500 			FAIL_IF(push_inst(compiler, MOVZ | RD(dst) | ((simm & 0xffff) << 5) | (i << 21)));
    501 		}
    502 		else
    503 			FAIL_IF(push_inst(compiler, MOVK | RD(dst) | ((simm & 0xffff) << 5) | (i << 21)));
    504 		simm >>= 16;
    505 	}
    506 	return SLJIT_SUCCESS;
    507 }
    508 
    509 #define ARG1_IMM	0x0010000
    510 #define ARG2_IMM	0x0020000
    511 #define INT_OP		0x0040000
    512 #define SET_FLAGS	0x0080000
    513 #define UNUSED_RETURN	0x0100000
    514 #define SLOW_DEST	0x0200000
    515 #define SLOW_SRC1	0x0400000
    516 #define SLOW_SRC2	0x0800000
    517 
    518 #define CHECK_FLAGS(flag_bits) \
    519 	if (flags & SET_FLAGS) { \
    520 		inv_bits |= flag_bits; \
    521 		if (flags & UNUSED_RETURN) \
    522 			dst = TMP_ZERO; \
    523 	}
    524 
    525 static sljit_s32 emit_op_imm(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 dst, sljit_sw arg1, sljit_sw arg2)
    526 {
    527 	/* dst must be register, TMP_REG1
    528 	   arg1 must be register, TMP_REG1, imm
    529 	   arg2 must be register, TMP_REG2, imm */
    530 	sljit_ins inv_bits = (flags & INT_OP) ? (1 << 31) : 0;
    531 	sljit_ins inst_bits;
    532 	sljit_s32 op = (flags & 0xffff);
    533 	sljit_s32 reg;
    534 	sljit_sw imm, nimm;
    535 
    536 	if (SLJIT_UNLIKELY((flags & (ARG1_IMM | ARG2_IMM)) == (ARG1_IMM | ARG2_IMM))) {
    537 		/* Both are immediates. */
    538 		flags &= ~ARG1_IMM;
    539 		if (arg1 == 0 && op != SLJIT_ADD && op != SLJIT_SUB)
    540 			arg1 = TMP_ZERO;
    541 		else {
    542 			FAIL_IF(load_immediate(compiler, TMP_REG1, arg1));
    543 			arg1 = TMP_REG1;
    544 		}
    545 	}
    546 
    547 	if (flags & (ARG1_IMM | ARG2_IMM)) {
    548 		reg = (flags & ARG2_IMM) ? arg1 : arg2;
    549 		imm = (flags & ARG2_IMM) ? arg2 : arg1;
    550 
    551 		switch (op) {
    552 		case SLJIT_MUL:
    553 		case SLJIT_NEG:
    554 		case SLJIT_CLZ:
    555 		case SLJIT_ADDC:
    556 		case SLJIT_SUBC:
    557 			/* No form with immediate operand (except imm 0, which
    558 			is represented by a ZERO register). */
    559 			break;
    560 		case SLJIT_MOV:
    561 			SLJIT_ASSERT(!(flags & SET_FLAGS) && (flags & ARG2_IMM) && arg1 == TMP_REG1);
    562 			return load_immediate(compiler, dst, imm);
    563 		case SLJIT_NOT:
    564 			SLJIT_ASSERT(flags & ARG2_IMM);
    565 			FAIL_IF(load_immediate(compiler, dst, (flags & INT_OP) ? (~imm & 0xffffffff) : ~imm));
    566 			goto set_flags;
    567 		case SLJIT_SUB:
    568 			if (flags & ARG1_IMM)
    569 				break;
    570 			imm = -imm;
    571 			/* Fall through. */
    572 		case SLJIT_ADD:
    573 			if (imm == 0) {
    574 				CHECK_FLAGS(1 << 29);
    575 				return push_inst(compiler, ((op == SLJIT_ADD ? ADDI : SUBI) ^ inv_bits) | RD(dst) | RN(reg));
    576 			}
    577 			if (imm > 0 && imm <= 0xfff) {
    578 				CHECK_FLAGS(1 << 29);
    579 				return push_inst(compiler, (ADDI ^ inv_bits) | RD(dst) | RN(reg) | (imm << 10));
    580 			}
    581 			nimm = -imm;
    582 			if (nimm > 0 && nimm <= 0xfff) {
    583 				CHECK_FLAGS(1 << 29);
    584 				return push_inst(compiler, (SUBI ^ inv_bits) | RD(dst) | RN(reg) | (nimm << 10));
    585 			}
    586 			if (imm > 0 && imm <= 0xffffff && !(imm & 0xfff)) {
    587 				CHECK_FLAGS(1 << 29);
    588 				return push_inst(compiler, (ADDI ^ inv_bits) | RD(dst) | RN(reg) | ((imm >> 12) << 10) | (1 << 22));
    589 			}
    590 			if (nimm > 0 && nimm <= 0xffffff && !(nimm & 0xfff)) {
    591 				CHECK_FLAGS(1 << 29);
    592 				return push_inst(compiler, (SUBI ^ inv_bits) | RD(dst) | RN(reg) | ((nimm >> 12) << 10) | (1 << 22));
    593 			}
    594 			if (imm > 0 && imm <= 0xffffff && !(flags & SET_FLAGS)) {
    595 				FAIL_IF(push_inst(compiler, (ADDI ^ inv_bits) | RD(dst) | RN(reg) | ((imm >> 12) << 10) | (1 << 22)));
    596 				return push_inst(compiler, (ADDI ^ inv_bits) | RD(dst) | RN(dst) | ((imm & 0xfff) << 10));
    597 			}
    598 			if (nimm > 0 && nimm <= 0xffffff && !(flags & SET_FLAGS)) {
    599 				FAIL_IF(push_inst(compiler, (SUBI ^ inv_bits) | RD(dst) | RN(reg) | ((nimm >> 12) << 10) | (1 << 22)));
    600 				return push_inst(compiler, (SUBI ^ inv_bits) | RD(dst) | RN(dst) | ((nimm & 0xfff) << 10));
    601 			}
    602 			break;
    603 		case SLJIT_AND:
    604 			inst_bits = logical_imm(imm, LOGICAL_IMM_CHECK | ((flags & INT_OP) ? 16 : 32));
    605 			if (!inst_bits)
    606 				break;
    607 			CHECK_FLAGS(3 << 29);
    608 			return push_inst(compiler, (ANDI ^ inv_bits) | RD(dst) | RN(reg) | inst_bits);
    609 		case SLJIT_OR:
    610 		case SLJIT_XOR:
    611 			inst_bits = logical_imm(imm, LOGICAL_IMM_CHECK | ((flags & INT_OP) ? 16 : 32));
    612 			if (!inst_bits)
    613 				break;
    614 			if (op == SLJIT_OR)
    615 				inst_bits |= ORRI;
    616 			else
    617 				inst_bits |= EORI;
    618 			FAIL_IF(push_inst(compiler, (inst_bits ^ inv_bits) | RD(dst) | RN(reg)));
    619 			goto set_flags;
    620 		case SLJIT_SHL:
    621 			if (flags & ARG1_IMM)
    622 				break;
    623 			if (flags & INT_OP) {
    624 				imm &= 0x1f;
    625 				FAIL_IF(push_inst(compiler, (UBFM ^ inv_bits) | RD(dst) | RN(arg1) | ((-imm & 0x1f) << 16) | ((31 - imm) << 10)));
    626 			}
    627 			else {
    628 				imm &= 0x3f;
    629 				FAIL_IF(push_inst(compiler, (UBFM ^ inv_bits) | RD(dst) | RN(arg1) | (1 << 22) | ((-imm & 0x3f) << 16) | ((63 - imm) << 10)));
    630 			}
    631 			goto set_flags;
    632 		case SLJIT_LSHR:
    633 		case SLJIT_ASHR:
    634 			if (flags & ARG1_IMM)
    635 				break;
    636 			if (op == SLJIT_ASHR)
    637 				inv_bits |= 1 << 30;
    638 			if (flags & INT_OP) {
    639 				imm &= 0x1f;
    640 				FAIL_IF(push_inst(compiler, (UBFM ^ inv_bits) | RD(dst) | RN(arg1) | (imm << 16) | (31 << 10)));
    641 			}
    642 			else {
    643 				imm &= 0x3f;
    644 				FAIL_IF(push_inst(compiler, (UBFM ^ inv_bits) | RD(dst) | RN(arg1) | (1 << 22) | (imm << 16) | (63 << 10)));
    645 			}
    646 			goto set_flags;
    647 		default:
    648 			SLJIT_UNREACHABLE();
    649 			break;
    650 		}
    651 
    652 		if (flags & ARG2_IMM) {
    653 			if (arg2 == 0)
    654 				arg2 = TMP_ZERO;
    655 			else {
    656 				FAIL_IF(load_immediate(compiler, TMP_REG2, arg2));
    657 				arg2 = TMP_REG2;
    658 			}
    659 		}
    660 		else {
    661 			if (arg1 == 0)
    662 				arg1 = TMP_ZERO;
    663 			else {
    664 				FAIL_IF(load_immediate(compiler, TMP_REG1, arg1));
    665 				arg1 = TMP_REG1;
    666 			}
    667 		}
    668 	}
    669 
    670 	/* Both arguments are registers. */
    671 	switch (op) {
    672 	case SLJIT_MOV:
    673 	case SLJIT_MOV_P:
    674 	case SLJIT_MOVU:
    675 	case SLJIT_MOVU_P:
    676 		SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
    677 		if (dst == arg2)
    678 			return SLJIT_SUCCESS;
    679 		return push_inst(compiler, ORR | RD(dst) | RN(TMP_ZERO) | RM(arg2));
    680 	case SLJIT_MOV_U8:
    681 	case SLJIT_MOVU_U8:
    682 		SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
    683 		return push_inst(compiler, (UBFM ^ (1 << 31)) | RD(dst) | RN(arg2) | (7 << 10));
    684 	case SLJIT_MOV_S8:
    685 	case SLJIT_MOVU_S8:
    686 		SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
    687 		if (!(flags & INT_OP))
    688 			inv_bits |= 1 << 22;
    689 		return push_inst(compiler, (SBFM ^ inv_bits) | RD(dst) | RN(arg2) | (7 << 10));
    690 	case SLJIT_MOV_U16:
    691 	case SLJIT_MOVU_U16:
    692 		SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
    693 		return push_inst(compiler, (UBFM ^ (1 << 31)) | RD(dst) | RN(arg2) | (15 << 10));
    694 	case SLJIT_MOV_S16:
    695 	case SLJIT_MOVU_S16:
    696 		SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
    697 		if (!(flags & INT_OP))
    698 			inv_bits |= 1 << 22;
    699 		return push_inst(compiler, (SBFM ^ inv_bits) | RD(dst) | RN(arg2) | (15 << 10));
    700 	case SLJIT_MOV_U32:
    701 	case SLJIT_MOVU_U32:
    702 		SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
    703 		if ((flags & INT_OP) && dst == arg2)
    704 			return SLJIT_SUCCESS;
    705 		return push_inst(compiler, (ORR ^ (1 << 31)) | RD(dst) | RN(TMP_ZERO) | RM(arg2));
    706 	case SLJIT_MOV_S32:
    707 	case SLJIT_MOVU_S32:
    708 		SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
    709 		if ((flags & INT_OP) && dst == arg2)
    710 			return SLJIT_SUCCESS;
    711 		return push_inst(compiler, SBFM | (1 << 22) | RD(dst) | RN(arg2) | (31 << 10));
    712 	case SLJIT_NOT:
    713 		SLJIT_ASSERT(arg1 == TMP_REG1);
    714 		FAIL_IF(push_inst(compiler, (ORN ^ inv_bits) | RD(dst) | RN(TMP_ZERO) | RM(arg2)));
    715 		goto set_flags;
    716 	case SLJIT_NEG:
    717 		SLJIT_ASSERT(arg1 == TMP_REG1);
    718 		if (flags & SET_FLAGS)
    719 			inv_bits |= 1 << 29;
    720 		return push_inst(compiler, (SUB ^ inv_bits) | RD(dst) | RN(TMP_ZERO) | RM(arg2));
    721 	case SLJIT_CLZ:
    722 		SLJIT_ASSERT(arg1 == TMP_REG1);
    723 		FAIL_IF(push_inst(compiler, (CLZ ^ inv_bits) | RD(dst) | RN(arg2)));
    724 		goto set_flags;
    725 	case SLJIT_ADD:
    726 		CHECK_FLAGS(1 << 29);
    727 		return push_inst(compiler, (ADD ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2));
    728 	case SLJIT_ADDC:
    729 		CHECK_FLAGS(1 << 29);
    730 		return push_inst(compiler, (ADC ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2));
    731 	case SLJIT_SUB:
    732 		CHECK_FLAGS(1 << 29);
    733 		return push_inst(compiler, (SUB ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2));
    734 	case SLJIT_SUBC:
    735 		CHECK_FLAGS(1 << 29);
    736 		return push_inst(compiler, (SBC ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2));
    737 	case SLJIT_MUL:
    738 		if (!(flags & SET_FLAGS))
    739 			return push_inst(compiler, (MADD ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2) | RT2(TMP_ZERO));
    740 		if (flags & INT_OP) {
    741 			FAIL_IF(push_inst(compiler, SMADDL | RD(dst) | RN(arg1) | RM(arg2) | (31 << 10)));
    742 			FAIL_IF(push_inst(compiler, ADD | RD(TMP_LR) | RN(TMP_ZERO) | RM(dst) | (2 << 22) | (31 << 10)));
    743 			return push_inst(compiler, SUBS | RD(TMP_ZERO) | RN(TMP_LR) | RM(dst) | (2 << 22) | (63 << 10));
    744 		}
    745 		FAIL_IF(push_inst(compiler, SMULH | RD(TMP_LR) | RN(arg1) | RM(arg2)));
    746 		FAIL_IF(push_inst(compiler, MADD | RD(dst) | RN(arg1) | RM(arg2) | RT2(TMP_ZERO)));
    747 		return push_inst(compiler, SUBS | RD(TMP_ZERO) | RN(TMP_LR) | RM(dst) | (2 << 22) | (63 << 10));
    748 	case SLJIT_AND:
    749 		CHECK_FLAGS(3 << 29);
    750 		return push_inst(compiler, (AND ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2));
    751 	case SLJIT_OR:
    752 		FAIL_IF(push_inst(compiler, (ORR ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2)));
    753 		goto set_flags;
    754 	case SLJIT_XOR:
    755 		FAIL_IF(push_inst(compiler, (EOR ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2)));
    756 		goto set_flags;
    757 	case SLJIT_SHL:
    758 		FAIL_IF(push_inst(compiler, (LSLV ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2)));
    759 		goto set_flags;
    760 	case SLJIT_LSHR:
    761 		FAIL_IF(push_inst(compiler, (LSRV ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2)));
    762 		goto set_flags;
    763 	case SLJIT_ASHR:
    764 		FAIL_IF(push_inst(compiler, (ASRV ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2)));
    765 		goto set_flags;
    766 	}
    767 
    768 	SLJIT_UNREACHABLE();
    769 	return SLJIT_SUCCESS;
    770 
    771 set_flags:
    772 	if (flags & SET_FLAGS)
    773 		return push_inst(compiler, (SUBS ^ inv_bits) | RD(TMP_ZERO) | RN(dst) | RM(TMP_ZERO));
    774 	return SLJIT_SUCCESS;
    775 }
    776 
    777 #define STORE		0x01
    778 #define SIGNED		0x02
    779 
    780 #define UPDATE		0x04
    781 #define ARG_TEST	0x08
    782 
    783 #define BYTE_SIZE	0x000
    784 #define HALF_SIZE	0x100
    785 #define INT_SIZE	0x200
    786 #define WORD_SIZE	0x300
    787 
    788 #define MEM_SIZE_SHIFT(flags) ((flags) >> 8)
    789 
    790 static const sljit_ins sljit_mem_imm[4] = {
    791 /* u l */ 0x39400000 /* ldrb [reg,imm] */,
    792 /* u s */ 0x39000000 /* strb [reg,imm] */,
    793 /* s l */ 0x39800000 /* ldrsb [reg,imm] */,
    794 /* s s */ 0x39000000 /* strb [reg,imm] */,
    795 };
    796 
    797 static const sljit_ins sljit_mem_simm[4] = {
    798 /* u l */ 0x38400000 /* ldurb [reg,imm] */,
    799 /* u s */ 0x38000000 /* sturb [reg,imm] */,
    800 /* s l */ 0x38800000 /* ldursb [reg,imm] */,
    801 /* s s */ 0x38000000 /* sturb [reg,imm] */,
    802 };
    803 
    804 static const sljit_ins sljit_mem_pre_simm[4] = {
    805 /* u l */ 0x38400c00 /* ldrb [reg,imm]! */,
    806 /* u s */ 0x38000c00 /* strb [reg,imm]! */,
    807 /* s l */ 0x38800c00 /* ldrsb [reg,imm]! */,
    808 /* s s */ 0x38000c00 /* strb [reg,imm]! */,
    809 };
    810 
    811 static const sljit_ins sljit_mem_reg[4] = {
    812 /* u l */ 0x38606800 /* ldrb [reg,reg] */,
    813 /* u s */ 0x38206800 /* strb [reg,reg] */,
    814 /* s l */ 0x38a06800 /* ldrsb [reg,reg] */,
    815 /* s s */ 0x38206800 /* strb [reg,reg] */,
    816 };
    817 
    818 /* Helper function. Dst should be reg + value, using at most 1 instruction, flags does not set. */
    819 static sljit_s32 emit_set_delta(struct sljit_compiler *compiler, sljit_s32 dst, sljit_s32 reg, sljit_sw value)
    820 {
    821 	if (value >= 0) {
    822 		if (value <= 0xfff)
    823 			return push_inst(compiler, ADDI | RD(dst) | RN(reg) | (value << 10));
    824 		if (value <= 0xffffff && !(value & 0xfff))
    825 			return push_inst(compiler, ADDI | (1 << 22) | RD(dst) | RN(reg) | (value >> 2));
    826 	}
    827 	else {
    828 		value = -value;
    829 		if (value <= 0xfff)
    830 			return push_inst(compiler, SUBI | RD(dst) | RN(reg) | (value << 10));
    831 		if (value <= 0xffffff && !(value & 0xfff))
    832 			return push_inst(compiler, SUBI | (1 << 22) | RD(dst) | RN(reg) | (value >> 2));
    833 	}
    834 	return SLJIT_ERR_UNSUPPORTED;
    835 }
    836 
    837 /* Can perform an operation using at most 1 instruction. */
    838 static sljit_s32 getput_arg_fast(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg, sljit_s32 arg, sljit_sw argw)
    839 {
    840 	sljit_u32 shift = MEM_SIZE_SHIFT(flags);
    841 
    842 	SLJIT_ASSERT(arg & SLJIT_MEM);
    843 
    844 	if (SLJIT_UNLIKELY(flags & UPDATE)) {
    845 		if ((arg & REG_MASK) && !(arg & OFFS_REG_MASK) && argw <= 255 && argw >= -256) {
    846 			if (SLJIT_UNLIKELY(flags & ARG_TEST))
    847 				return 1;
    848 
    849 			arg &= REG_MASK;
    850 			argw &= 0x1ff;
    851 			FAIL_IF(push_inst(compiler, sljit_mem_pre_simm[flags & 0x3]
    852 				| (shift << 30) | RT(reg) | RN(arg) | (argw << 12)));
    853 			return -1;
    854 		}
    855 		return 0;
    856 	}
    857 
    858 	if (SLJIT_UNLIKELY(arg & OFFS_REG_MASK)) {
    859 		argw &= 0x3;
    860 		if (argw && argw != shift)
    861 			return 0;
    862 
    863 		if (SLJIT_UNLIKELY(flags & ARG_TEST))
    864 			return 1;
    865 
    866 		FAIL_IF(push_inst(compiler, sljit_mem_reg[flags & 0x3] | (shift << 30) | RT(reg)
    867 			| RN(arg & REG_MASK) | RM(OFFS_REG(arg)) | (argw ? (1 << 12) : 0)));
    868 		return -1;
    869 	}
    870 
    871 	arg &= REG_MASK;
    872 	if (argw >= 0 && (argw >> shift) <= 0xfff && (argw & ((1 << shift) - 1)) == 0) {
    873 		if (SLJIT_UNLIKELY(flags & ARG_TEST))
    874 			return 1;
    875 
    876 		FAIL_IF(push_inst(compiler, sljit_mem_imm[flags & 0x3] | (shift << 30)
    877 			| RT(reg) | RN(arg) | (argw << (10 - shift))));
    878 		return -1;
    879 	}
    880 
    881 	if (argw > 255 || argw < -256)
    882 		return 0;
    883 
    884 	if (SLJIT_UNLIKELY(flags & ARG_TEST))
    885 		return 1;
    886 
    887 	FAIL_IF(push_inst(compiler, sljit_mem_simm[flags & 0x3] | (shift << 30)
    888 		| RT(reg) | RN(arg) | ((argw & 0x1ff) << 12)));
    889 	return -1;
    890 }
    891 
    892 /* see getput_arg below.
    893    Note: can_cache is called only for binary operators. Those
    894    operators always uses word arguments without write back. */
    895 static sljit_s32 can_cache(sljit_s32 arg, sljit_sw argw, sljit_s32 next_arg, sljit_sw next_argw)
    896 {
    897 	sljit_sw diff;
    898 	if ((arg & OFFS_REG_MASK) || !(next_arg & SLJIT_MEM))
    899 		return 0;
    900 
    901 	if (!(arg & REG_MASK)) {
    902 		diff = argw - next_argw;
    903 		if (diff <= 0xfff && diff >= -0xfff)
    904 			return 1;
    905 		return 0;
    906 	}
    907 
    908 	if (argw == next_argw)
    909 		return 1;
    910 
    911 	diff = argw - next_argw;
    912 	if (arg == next_arg && diff <= 0xfff && diff >= -0xfff)
    913 		return 1;
    914 
    915 	return 0;
    916 }
    917 
    918 /* Emit the necessary instructions. See can_cache above. */
    919 static sljit_s32 getput_arg(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg,
    920 	sljit_s32 arg, sljit_sw argw, sljit_s32 next_arg, sljit_sw next_argw)
    921 {
    922 	sljit_u32 shift = MEM_SIZE_SHIFT(flags);
    923 	sljit_s32 tmp_r, other_r;
    924 	sljit_sw diff;
    925 
    926 	SLJIT_ASSERT(arg & SLJIT_MEM);
    927 	if (!(next_arg & SLJIT_MEM)) {
    928 		next_arg = 0;
    929 		next_argw = 0;
    930 	}
    931 
    932 	tmp_r = (flags & STORE) ? TMP_REG3 : reg;
    933 
    934 	if (SLJIT_UNLIKELY((flags & UPDATE) && (arg & REG_MASK))) {
    935 		/* Update only applies if a base register exists. */
    936 		other_r = OFFS_REG(arg);
    937 		if (!other_r) {
    938 			other_r = arg & REG_MASK;
    939 			SLJIT_ASSERT(other_r != reg);
    940 
    941 			if (argw >= 0 && argw <= 0xffffff) {
    942 				if ((argw & 0xfff) != 0)
    943 					FAIL_IF(push_inst(compiler, ADDI | RD(other_r) | RN(other_r) | ((argw & 0xfff) << 10)));
    944 				if (argw >> 12)
    945 					FAIL_IF(push_inst(compiler, ADDI | (1 << 22) | RD(other_r) | RN(other_r) | ((argw >> 12) << 10)));
    946 				return push_inst(compiler, sljit_mem_imm[flags & 0x3] | (shift << 30) | RT(reg) | RN(other_r));
    947 			}
    948 			else if (argw < 0 && argw >= -0xffffff) {
    949 				argw = -argw;
    950 				if ((argw & 0xfff) != 0)
    951 					FAIL_IF(push_inst(compiler, SUBI | RD(other_r) | RN(other_r) | ((argw & 0xfff) << 10)));
    952 				if (argw >> 12)
    953 					FAIL_IF(push_inst(compiler, SUBI | (1 << 22) | RD(other_r) | RN(other_r) | ((argw >> 12) << 10)));
    954 				return push_inst(compiler, sljit_mem_imm[flags & 0x3] | (shift << 30) | RT(reg) | RN(other_r));
    955 			}
    956 
    957 			if (compiler->cache_arg == SLJIT_MEM) {
    958 				if (argw == compiler->cache_argw) {
    959 					other_r = TMP_REG3;
    960 					argw = 0;
    961 				}
    962 				else if (emit_set_delta(compiler, TMP_REG3, TMP_REG3, argw - compiler->cache_argw) != SLJIT_ERR_UNSUPPORTED) {
    963 					FAIL_IF(compiler->error);
    964 					compiler->cache_argw = argw;
    965 					other_r = TMP_REG3;
    966 					argw = 0;
    967 				}
    968 			}
    969 
    970 			if (argw) {
    971 				FAIL_IF(load_immediate(compiler, TMP_REG3, argw));
    972 				compiler->cache_arg = SLJIT_MEM;
    973 				compiler->cache_argw = argw;
    974 				other_r = TMP_REG3;
    975 				argw = 0;
    976 			}
    977 		}
    978 
    979 		/* No caching here. */
    980 		arg &= REG_MASK;
    981 		FAIL_IF(push_inst(compiler, sljit_mem_reg[flags & 0x3] | (shift << 30) | RT(reg) | RN(arg) | RM(other_r)));
    982 		return push_inst(compiler, ADD | RD(arg) | RN(arg) | RM(other_r));
    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_UNREACHABLE();
   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 = HAS_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 = HAS_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_UNREACHABLE();
   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 = HAS_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_target, sljit_sw executable_offset)
   2041 {
   2042 	sljit_ins* inst = (sljit_ins*)addr;
   2043 	modify_imm64_const(inst, new_target);
   2044 	inst = (sljit_ins *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
   2045 	SLJIT_CACHE_FLUSH(inst, inst + 4);
   2046 }
   2047 
   2048 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_const(sljit_uw addr, sljit_sw new_constant, sljit_sw executable_offset)
   2049 {
   2050 	sljit_ins* inst = (sljit_ins*)addr;
   2051 	modify_imm64_const(inst, new_constant);
   2052 	inst = (sljit_ins *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
   2053 	SLJIT_CACHE_FLUSH(inst, inst + 4);
   2054 }
   2055