1 /*
2  *    Stack-less Just-In-Time compiler
3  *
4  *    Copyright Zoltan Herczeg (hzmester@freemail.hu). All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without modification, are
7  * permitted provided that the following conditions are met:
8  *
9  *   1. Redistributions of source code must retain the above copyright notice, this list of
10  *      conditions and the following disclaimer.
11  *
12  *   2. Redistributions in binary form must reproduce the above copyright notice, this list
13  *      of conditions and the following disclaimer in the documentation and/or other materials
14  *      provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) AND CONTRIBUTORS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19  * SHALL THE COPYRIGHT HOLDER(S) OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
22  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 /* Latest MIPS architecture. */
28 /* Automatically detect SLJIT_MIPS_R1 */
29 
sljit_get_platform_name(void)30 SLJIT_API_FUNC_ATTRIBUTE const char* sljit_get_platform_name(void)
31 {
32 #if (defined SLJIT_MIPS_R1 && SLJIT_MIPS_R1)
33 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
34 	return "MIPS32-R1" SLJIT_CPUINFO;
35 #else
36 	return "MIPS64-R1" SLJIT_CPUINFO;
37 #endif
38 #else /* SLJIT_MIPS_R1 */
39 	return "MIPS III" SLJIT_CPUINFO;
40 #endif
41 }
42 
43 /* Length of an instruction word
44    Both for mips-32 and mips-64 */
45 typedef sljit_u32 sljit_ins;
46 
47 #define TMP_REG1	(SLJIT_NUMBER_OF_REGISTERS + 2)
48 #define TMP_REG2	(SLJIT_NUMBER_OF_REGISTERS + 3)
49 #define TMP_REG3	(SLJIT_NUMBER_OF_REGISTERS + 4)
50 
51 /* For position independent code, t9 must contain the function address. */
52 #define PIC_ADDR_REG	TMP_REG2
53 
54 /* Floating point status register. */
55 #define FCSR_REG	31
56 /* Return address register. */
57 #define RETURN_ADDR_REG	31
58 
59 /* Flags are kept in volatile registers. */
60 #define EQUAL_FLAG	31
61 #define OTHER_FLAG	1
62 
63 #define TMP_FREG1	(0)
64 #define TMP_FREG2	((SLJIT_NUMBER_OF_FLOAT_REGISTERS + 1) << 1)
65 
66 static const sljit_u8 reg_map[SLJIT_NUMBER_OF_REGISTERS + 5] = {
67 	0, 2, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 24, 23, 22, 21, 20, 19, 18, 17, 16, 29, 3, 25, 4
68 };
69 
70 /* --------------------------------------------------------------------- */
71 /*  Instrucion forms                                                     */
72 /* --------------------------------------------------------------------- */
73 
74 #define S(s)		(reg_map[s] << 21)
75 #define T(t)		(reg_map[t] << 16)
76 #define D(d)		(reg_map[d] << 11)
77 /* Absolute registers. */
78 #define SA(s)		((s) << 21)
79 #define TA(t)		((t) << 16)
80 #define DA(d)		((d) << 11)
81 #define FT(t)		((t) << 16)
82 #define FS(s)		((s) << 11)
83 #define FD(d)		((d) << 6)
84 #define IMM(imm)	((imm) & 0xffff)
85 #define SH_IMM(imm)	((imm) << 6)
86 
87 #define DR(dr)		(reg_map[dr])
88 #define HI(opcode)	((opcode) << 26)
89 #define LO(opcode)	(opcode)
90 /* S = (16 << 21) D = (17 << 21) */
91 #define FMT_S		(16 << 21)
92 
93 #define ABS_S		(HI(17) | FMT_S | LO(5))
94 #define ADD_S		(HI(17) | FMT_S | LO(0))
95 #define ADDIU		(HI(9))
96 #define ADDU		(HI(0) | LO(33))
97 #define AND		(HI(0) | LO(36))
98 #define ANDI		(HI(12))
99 #define B		(HI(4))
100 #define BAL		(HI(1) | (17 << 16))
101 #define BC1F		(HI(17) | (8 << 21))
102 #define BC1T		(HI(17) | (8 << 21) | (1 << 16))
103 #define BEQ		(HI(4))
104 #define BGEZ		(HI(1) | (1 << 16))
105 #define BGTZ		(HI(7))
106 #define BLEZ		(HI(6))
107 #define BLTZ		(HI(1) | (0 << 16))
108 #define BNE		(HI(5))
109 #define BREAK		(HI(0) | LO(13))
110 #define CFC1		(HI(17) | (2 << 21))
111 #define C_UN_S		(HI(17) | FMT_S | LO(49))
112 #define C_UEQ_S		(HI(17) | FMT_S | LO(51))
113 #define C_ULE_S		(HI(17) | FMT_S | LO(55))
114 #define C_ULT_S		(HI(17) | FMT_S | LO(53))
115 #define CVT_S_S		(HI(17) | FMT_S | LO(32))
116 #define DADDIU		(HI(25))
117 #define DADDU		(HI(0) | LO(45))
118 #define DDIV		(HI(0) | LO(30))
119 #define DDIVU		(HI(0) | LO(31))
120 #define DIV		(HI(0) | LO(26))
121 #define DIVU		(HI(0) | LO(27))
122 #define DIV_S		(HI(17) | FMT_S | LO(3))
123 #define DMULT		(HI(0) | LO(28))
124 #define DMULTU		(HI(0) | LO(29))
125 #define DSLL		(HI(0) | LO(56))
126 #define DSLL32		(HI(0) | LO(60))
127 #define DSLLV		(HI(0) | LO(20))
128 #define DSRA		(HI(0) | LO(59))
129 #define DSRA32		(HI(0) | LO(63))
130 #define DSRAV		(HI(0) | LO(23))
131 #define DSRL		(HI(0) | LO(58))
132 #define DSRL32		(HI(0) | LO(62))
133 #define DSRLV		(HI(0) | LO(22))
134 #define DSUBU		(HI(0) | LO(47))
135 #define J		(HI(2))
136 #define JAL		(HI(3))
137 #define JALR		(HI(0) | LO(9))
138 #define JR		(HI(0) | LO(8))
139 #define LD		(HI(55))
140 #define LUI		(HI(15))
141 #define LW		(HI(35))
142 #define MFC1		(HI(17))
143 #define MFHI		(HI(0) | LO(16))
144 #define MFLO		(HI(0) | LO(18))
145 #define MOV_S		(HI(17) | FMT_S | LO(6))
146 #define MTC1		(HI(17) | (4 << 21))
147 #define MUL_S		(HI(17) | FMT_S | LO(2))
148 #define MULT		(HI(0) | LO(24))
149 #define MULTU		(HI(0) | LO(25))
150 #define NEG_S		(HI(17) | FMT_S | LO(7))
151 #define NOP		(HI(0) | LO(0))
152 #define NOR		(HI(0) | LO(39))
153 #define OR		(HI(0) | LO(37))
154 #define ORI		(HI(13))
155 #define SD		(HI(63))
156 #define SLT		(HI(0) | LO(42))
157 #define SLTI		(HI(10))
158 #define SLTIU		(HI(11))
159 #define SLTU		(HI(0) | LO(43))
160 #define SLL		(HI(0) | LO(0))
161 #define SLLV		(HI(0) | LO(4))
162 #define SRL		(HI(0) | LO(2))
163 #define SRLV		(HI(0) | LO(6))
164 #define SRA		(HI(0) | LO(3))
165 #define SRAV		(HI(0) | LO(7))
166 #define SUB_S		(HI(17) | FMT_S | LO(1))
167 #define SUBU		(HI(0) | LO(35))
168 #define SW		(HI(43))
169 #define TRUNC_W_S	(HI(17) | FMT_S | LO(13))
170 #define XOR		(HI(0) | LO(38))
171 #define XORI		(HI(14))
172 
173 #if (defined SLJIT_MIPS_R1 && SLJIT_MIPS_R1)
174 #define CLZ		(HI(28) | LO(32))
175 #define DCLZ		(HI(28) | LO(36))
176 #define MOVF		(HI(0) | (0 << 16) | LO(1))
177 #define MOVN		(HI(0) | LO(11))
178 #define MOVT		(HI(0) | (1 << 16) | LO(1))
179 #define MOVZ		(HI(0) | LO(10))
180 #define MUL		(HI(28) | LO(2))
181 #define PREF		(HI(51))
182 #define PREFX		(HI(19) | LO(15))
183 #define SEB		(HI(31) | (16 << 6) | LO(32))
184 #define SEH		(HI(31) | (24 << 6) | LO(32))
185 #endif
186 
187 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
188 #define ADDU_W		ADDU
189 #define ADDIU_W		ADDIU
190 #define SLL_W		SLL
191 #define SUBU_W		SUBU
192 #else
193 #define ADDU_W		DADDU
194 #define ADDIU_W		DADDIU
195 #define SLL_W		DSLL
196 #define SUBU_W		DSUBU
197 #endif
198 
199 #define SIMM_MAX	(0x7fff)
200 #define SIMM_MIN	(-0x8000)
201 #define UIMM_MAX	(0xffff)
202 
203 /* dest_reg is the absolute name of the register
204    Useful for reordering instructions in the delay slot. */
push_inst(struct sljit_compiler * compiler,sljit_ins ins,sljit_s32 delay_slot)205 static sljit_s32 push_inst(struct sljit_compiler *compiler, sljit_ins ins, sljit_s32 delay_slot)
206 {
207 	SLJIT_ASSERT(delay_slot == MOVABLE_INS || delay_slot >= UNMOVABLE_INS
208 		|| delay_slot == ((ins >> 11) & 0x1f) || delay_slot == ((ins >> 16) & 0x1f));
209 	sljit_ins *ptr = (sljit_ins*)ensure_buf(compiler, sizeof(sljit_ins));
210 	FAIL_IF(!ptr);
211 	*ptr = ins;
212 	compiler->size++;
213 	compiler->delay_slot = delay_slot;
214 	return SLJIT_SUCCESS;
215 }
216 
invert_branch(sljit_s32 flags)217 static SLJIT_INLINE sljit_ins invert_branch(sljit_s32 flags)
218 {
219 	return (flags & IS_BIT26_COND) ? (1 << 26) : (1 << 16);
220 }
221 
detect_jump_type(struct sljit_jump * jump,sljit_ins * code_ptr,sljit_ins * code,sljit_sw executable_offset)222 static SLJIT_INLINE sljit_ins* detect_jump_type(struct sljit_jump *jump, sljit_ins *code_ptr, sljit_ins *code, sljit_sw executable_offset)
223 {
224 	sljit_sw diff;
225 	sljit_uw target_addr;
226 	sljit_ins *inst;
227 	sljit_ins saved_inst;
228 
229 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
230 	if (jump->flags & (SLJIT_REWRITABLE_JUMP | IS_CALL))
231 		return code_ptr;
232 #else
233 	if (jump->flags & SLJIT_REWRITABLE_JUMP)
234 		return code_ptr;
235 #endif
236 
237 	if (jump->flags & JUMP_ADDR)
238 		target_addr = jump->u.target;
239 	else {
240 		SLJIT_ASSERT(jump->flags & JUMP_LABEL);
241 		target_addr = (sljit_uw)(code + jump->u.label->size) + (sljit_uw)executable_offset;
242 	}
243 
244 	inst = (sljit_ins *)jump->addr;
245 	if (jump->flags & IS_COND)
246 		inst--;
247 
248 #if (defined SLJIT_CONFIG_MIPS_64 && SLJIT_CONFIG_MIPS_64)
249 	if (jump->flags & IS_CALL)
250 		goto keep_address;
251 #endif
252 
253 	/* B instructions. */
254 	if (jump->flags & IS_MOVABLE) {
255 		diff = ((sljit_sw)target_addr - (sljit_sw)inst - executable_offset) >> 2;
256 		if (diff <= SIMM_MAX && diff >= SIMM_MIN) {
257 			jump->flags |= PATCH_B;
258 
259 			if (!(jump->flags & IS_COND)) {
260 				inst[0] = inst[-1];
261 				inst[-1] = (jump->flags & IS_JAL) ? BAL : B;
262 				jump->addr -= sizeof(sljit_ins);
263 				return inst;
264 			}
265 			saved_inst = inst[0];
266 			inst[0] = inst[-1];
267 			inst[-1] = saved_inst ^ invert_branch(jump->flags);
268 			jump->addr -= 2 * sizeof(sljit_ins);
269 			return inst;
270 		}
271 	}
272 	else {
273 		diff = ((sljit_sw)target_addr - (sljit_sw)(inst + 1) - executable_offset) >> 2;
274 		if (diff <= SIMM_MAX && diff >= SIMM_MIN) {
275 			jump->flags |= PATCH_B;
276 
277 			if (!(jump->flags & IS_COND)) {
278 				inst[0] = (jump->flags & IS_JAL) ? BAL : B;
279 				inst[1] = NOP;
280 				return inst + 1;
281 			}
282 			inst[0] = inst[0] ^ invert_branch(jump->flags);
283 			inst[1] = NOP;
284 			jump->addr -= sizeof(sljit_ins);
285 			return inst + 1;
286 		}
287 	}
288 
289 	if (jump->flags & IS_COND) {
290 		if ((jump->flags & IS_MOVABLE) && (target_addr & ~0xfffffff) == ((jump->addr + 2 * sizeof(sljit_ins)) & ~0xfffffff)) {
291 			jump->flags |= PATCH_J;
292 			saved_inst = inst[0];
293 			inst[0] = inst[-1];
294 			inst[-1] = (saved_inst & 0xffff0000) | 3;
295 			inst[1] = J;
296 			inst[2] = NOP;
297 			return inst + 2;
298 		}
299 		else if ((target_addr & ~0xfffffff) == ((jump->addr + 3 * sizeof(sljit_ins)) & ~0xfffffff)) {
300 			jump->flags |= PATCH_J;
301 			inst[0] = (inst[0] & 0xffff0000) | 3;
302 			inst[1] = NOP;
303 			inst[2] = J;
304 			inst[3] = NOP;
305 			jump->addr += sizeof(sljit_ins);
306 			return inst + 3;
307 		}
308 	}
309 	else {
310 		/* J instuctions. */
311 		if ((jump->flags & IS_MOVABLE) && (target_addr & ~0xfffffff) == (jump->addr & ~0xfffffff)) {
312 			jump->flags |= PATCH_J;
313 			inst[0] = inst[-1];
314 			inst[-1] = (jump->flags & IS_JAL) ? JAL : J;
315 			jump->addr -= sizeof(sljit_ins);
316 			return inst;
317 		}
318 
319 		if ((target_addr & ~0xfffffff) == ((jump->addr + sizeof(sljit_ins)) & ~0xfffffff)) {
320 			jump->flags |= PATCH_J;
321 			inst[0] = (jump->flags & IS_JAL) ? JAL : J;
322 			inst[1] = NOP;
323 			return inst + 1;
324 		}
325 	}
326 
327 #if (defined SLJIT_CONFIG_MIPS_64 && SLJIT_CONFIG_MIPS_64)
328 keep_address:
329 	if (target_addr <= 0x7fffffff) {
330 		jump->flags |= PATCH_ABS32;
331 		if (jump->flags & IS_COND) {
332 			inst[0] -= 4;
333 			inst++;
334 		}
335 		inst[2] = inst[6];
336 		inst[3] = inst[7];
337 		return inst + 3;
338 	}
339 	if (target_addr <= 0x7fffffffffffl) {
340 		jump->flags |= PATCH_ABS48;
341 		if (jump->flags & IS_COND) {
342 			inst[0] -= 2;
343 			inst++;
344 		}
345 		inst[4] = inst[6];
346 		inst[5] = inst[7];
347 		return inst + 5;
348 	}
349 #endif
350 
351 	return code_ptr;
352 }
353 
354 #ifdef __GNUC__
sljit_cache_flush(void * code,void * code_ptr)355 static __attribute__ ((noinline)) void sljit_cache_flush(void* code, void* code_ptr)
356 {
357 	SLJIT_CACHE_FLUSH(code, code_ptr);
358 }
359 #endif
360 
sljit_generate_code(struct sljit_compiler * compiler)361 SLJIT_API_FUNC_ATTRIBUTE void* sljit_generate_code(struct sljit_compiler *compiler)
362 {
363 	struct sljit_memory_fragment *buf;
364 	sljit_ins *code;
365 	sljit_ins *code_ptr;
366 	sljit_ins *buf_ptr;
367 	sljit_ins *buf_end;
368 	sljit_uw word_count;
369 	sljit_sw executable_offset;
370 	sljit_uw addr;
371 
372 	struct sljit_label *label;
373 	struct sljit_jump *jump;
374 	struct sljit_const *const_;
375 
376 	CHECK_ERROR_PTR();
377 	CHECK_PTR(check_sljit_generate_code(compiler));
378 	reverse_buf(compiler);
379 
380 	code = (sljit_ins*)SLJIT_MALLOC_EXEC(compiler->size * sizeof(sljit_ins));
381 	PTR_FAIL_WITH_EXEC_IF(code);
382 	buf = compiler->buf;
383 
384 	code_ptr = code;
385 	word_count = 0;
386 	executable_offset = SLJIT_EXEC_OFFSET(code);
387 
388 	label = compiler->labels;
389 	jump = compiler->jumps;
390 	const_ = compiler->consts;
391 
392 	do {
393 		buf_ptr = (sljit_ins*)buf->memory;
394 		buf_end = buf_ptr + (buf->used_size >> 2);
395 		do {
396 			*code_ptr = *buf_ptr++;
397 			SLJIT_ASSERT(!label || label->size >= word_count);
398 			SLJIT_ASSERT(!jump || jump->addr >= word_count);
399 			SLJIT_ASSERT(!const_ || const_->addr >= word_count);
400 			/* These structures are ordered by their address. */
401 			if (label && label->size == word_count) {
402 				label->addr = (sljit_uw)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset);
403 				label->size = code_ptr - code;
404 				label = label->next;
405 			}
406 			if (jump && jump->addr == word_count) {
407 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
408 				jump->addr = (sljit_uw)(code_ptr - 3);
409 #else
410 				jump->addr = (sljit_uw)(code_ptr - 7);
411 #endif
412 				code_ptr = detect_jump_type(jump, code_ptr, code, executable_offset);
413 				jump = jump->next;
414 			}
415 			if (const_ && const_->addr == word_count) {
416 				/* Just recording the address. */
417 				const_->addr = (sljit_uw)code_ptr;
418 				const_ = const_->next;
419 			}
420 			code_ptr ++;
421 			word_count ++;
422 		} while (buf_ptr < buf_end);
423 
424 		buf = buf->next;
425 	} while (buf);
426 
427 	if (label && label->size == word_count) {
428 		label->addr = (sljit_uw)code_ptr;
429 		label->size = code_ptr - code;
430 		label = label->next;
431 	}
432 
433 	SLJIT_ASSERT(!label);
434 	SLJIT_ASSERT(!jump);
435 	SLJIT_ASSERT(!const_);
436 	SLJIT_ASSERT(code_ptr - code <= (sljit_sw)compiler->size);
437 
438 	jump = compiler->jumps;
439 	while (jump) {
440 		do {
441 			addr = (jump->flags & JUMP_LABEL) ? jump->u.label->addr : jump->u.target;
442 			buf_ptr = (sljit_ins *)jump->addr;
443 
444 			if (jump->flags & PATCH_B) {
445 				addr = (sljit_sw)(addr - ((sljit_uw)SLJIT_ADD_EXEC_OFFSET(buf_ptr, executable_offset) + sizeof(sljit_ins))) >> 2;
446 				SLJIT_ASSERT((sljit_sw)addr <= SIMM_MAX && (sljit_sw)addr >= SIMM_MIN);
447 				buf_ptr[0] = (buf_ptr[0] & 0xffff0000) | (addr & 0xffff);
448 				break;
449 			}
450 			if (jump->flags & PATCH_J) {
451 				SLJIT_ASSERT((addr & ~0xfffffff) == (((sljit_uw)SLJIT_ADD_EXEC_OFFSET(buf_ptr, executable_offset) + sizeof(sljit_ins)) & ~0xfffffff));
452 				buf_ptr[0] |= (addr >> 2) & 0x03ffffff;
453 				break;
454 			}
455 
456 			/* Set the fields of immediate loads. */
457 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
458 			buf_ptr[0] = (buf_ptr[0] & 0xffff0000) | ((addr >> 16) & 0xffff);
459 			buf_ptr[1] = (buf_ptr[1] & 0xffff0000) | (addr & 0xffff);
460 #else
461 			if (jump->flags & PATCH_ABS32) {
462 				SLJIT_ASSERT(addr <= 0x7fffffff);
463 				buf_ptr[0] = (buf_ptr[0] & 0xffff0000) | ((addr >> 16) & 0xffff);
464 				buf_ptr[1] = (buf_ptr[1] & 0xffff0000) | (addr & 0xffff);
465 			}
466 			else if (jump->flags & PATCH_ABS48) {
467 				SLJIT_ASSERT(addr <= 0x7fffffffffffl);
468 				buf_ptr[0] = (buf_ptr[0] & 0xffff0000) | ((addr >> 32) & 0xffff);
469 				buf_ptr[1] = (buf_ptr[1] & 0xffff0000) | ((addr >> 16) & 0xffff);
470 				buf_ptr[3] = (buf_ptr[3] & 0xffff0000) | (addr & 0xffff);
471 			}
472 			else {
473 				buf_ptr[0] = (buf_ptr[0] & 0xffff0000) | ((addr >> 48) & 0xffff);
474 				buf_ptr[1] = (buf_ptr[1] & 0xffff0000) | ((addr >> 32) & 0xffff);
475 				buf_ptr[3] = (buf_ptr[3] & 0xffff0000) | ((addr >> 16) & 0xffff);
476 				buf_ptr[5] = (buf_ptr[5] & 0xffff0000) | (addr & 0xffff);
477 			}
478 #endif
479 		} while (0);
480 		jump = jump->next;
481 	}
482 
483 	compiler->error = SLJIT_ERR_COMPILED;
484 	compiler->executable_offset = executable_offset;
485 	compiler->executable_size = (code_ptr - code) * sizeof(sljit_ins);
486 
487 	code = (sljit_ins *)SLJIT_ADD_EXEC_OFFSET(code, executable_offset);
488 	code_ptr = (sljit_ins *)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset);
489 
490 #ifndef __GNUC__
491 	SLJIT_CACHE_FLUSH(code, code_ptr);
492 #else
493 	/* GCC workaround for invalid code generation with -O2. */
494 	sljit_cache_flush(code, code_ptr);
495 #endif
496 	return code;
497 }
498 
sljit_has_cpu_feature(sljit_s32 feature_type)499 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_has_cpu_feature(sljit_s32 feature_type)
500 {
501 	switch (feature_type) {
502 	case SLJIT_HAS_FPU:
503 #ifdef SLJIT_IS_FPU_AVAILABLE
504 		return SLJIT_IS_FPU_AVAILABLE;
505 #elif defined(__GNUC__)
506 		sljit_sw fir;
507 		asm ("cfc1 %0, $0" : "=r"(fir));
508 		return (fir >> 22) & 0x1;
509 #else
510 #error "FIR check is not implemented for this architecture"
511 #endif
512 
513 #if (defined SLJIT_MIPS_R1 && SLJIT_MIPS_R1)
514 	case SLJIT_HAS_CLZ:
515 	case SLJIT_HAS_CMOV:
516 		return 1;
517 #endif
518 
519 	default:
520 		return 0;
521 	}
522 }
523 
524 /* --------------------------------------------------------------------- */
525 /*  Entry, exit                                                          */
526 /* --------------------------------------------------------------------- */
527 
528 /* Creates an index in data_transfer_insts array. */
529 #define LOAD_DATA	0x01
530 #define WORD_DATA	0x00
531 #define BYTE_DATA	0x02
532 #define HALF_DATA	0x04
533 #define INT_DATA	0x06
534 #define SIGNED_DATA	0x08
535 /* Separates integer and floating point registers */
536 #define GPR_REG		0x0f
537 #define DOUBLE_DATA	0x10
538 #define SINGLE_DATA	0x12
539 
540 #define MEM_MASK	0x1f
541 
542 #define WRITE_BACK	0x00020
543 #define ARG_TEST	0x00040
544 #define ALT_KEEP_CACHE	0x00080
545 #define CUMULATIVE_OP	0x00100
546 #define LOGICAL_OP	0x00200
547 #define IMM_OP		0x00400
548 #define SRC2_IMM	0x00800
549 
550 #define UNUSED_DEST	0x01000
551 #define REG_DEST	0x02000
552 #define REG1_SOURCE	0x04000
553 #define REG2_SOURCE	0x08000
554 #define SLOW_SRC1	0x10000
555 #define SLOW_SRC2	0x20000
556 #define SLOW_DEST	0x40000
557 
558 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
559 #define STACK_STORE	SW
560 #define STACK_LOAD	LW
561 #else
562 #define STACK_STORE	SD
563 #define STACK_LOAD	LD
564 #endif
565 
566 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
567 #include "sljitNativeMIPS_32.c"
568 #else
569 #include "sljitNativeMIPS_64.c"
570 #endif
571 
sljit_emit_enter(struct sljit_compiler * compiler,sljit_s32 options,sljit_s32 args,sljit_s32 scratches,sljit_s32 saveds,sljit_s32 fscratches,sljit_s32 fsaveds,sljit_s32 local_size)572 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_enter(struct sljit_compiler *compiler,
573 	sljit_s32 options, sljit_s32 args, sljit_s32 scratches, sljit_s32 saveds,
574 	sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
575 {
576 	sljit_ins base;
577 	sljit_s32 i, tmp, offs;
578 
579 	CHECK_ERROR();
580 	CHECK(check_sljit_emit_enter(compiler, options, args, scratches, saveds, fscratches, fsaveds, local_size));
581 	set_emit_enter(compiler, options, args, scratches, saveds, fscratches, fsaveds, local_size);
582 
583 	local_size += GET_SAVED_REGISTERS_SIZE(scratches, saveds, 1) + SLJIT_LOCALS_OFFSET;
584 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
585 	local_size = (local_size + 15) & ~0xf;
586 #else
587 	local_size = (local_size + 31) & ~0x1f;
588 #endif
589 	compiler->local_size = local_size;
590 
591 	if (local_size <= SIMM_MAX) {
592 		/* Frequent case. */
593 		FAIL_IF(push_inst(compiler, ADDIU_W | S(SLJIT_SP) | T(SLJIT_SP) | IMM(-local_size), DR(SLJIT_SP)));
594 		base = S(SLJIT_SP);
595 	}
596 	else {
597 		FAIL_IF(load_immediate(compiler, DR(TMP_REG1), local_size));
598 		FAIL_IF(push_inst(compiler, ADDU_W | S(SLJIT_SP) | TA(0) | D(TMP_REG2), DR(TMP_REG2)));
599 		FAIL_IF(push_inst(compiler, SUBU_W | S(SLJIT_SP) | T(TMP_REG1) | D(SLJIT_SP), DR(SLJIT_SP)));
600 		base = S(TMP_REG2);
601 		local_size = 0;
602 	}
603 
604 	offs = local_size - (sljit_sw)(sizeof(sljit_sw));
605 	FAIL_IF(push_inst(compiler, STACK_STORE | base | TA(RETURN_ADDR_REG) | IMM(offs), MOVABLE_INS));
606 
607 	tmp = saveds < SLJIT_NUMBER_OF_SAVED_REGISTERS ? (SLJIT_S0 + 1 - saveds) : SLJIT_FIRST_SAVED_REG;
608 	for (i = SLJIT_S0; i >= tmp; i--) {
609 		offs -= (sljit_s32)(sizeof(sljit_sw));
610 		FAIL_IF(push_inst(compiler, STACK_STORE | base | T(i) | IMM(offs), MOVABLE_INS));
611 	}
612 
613 	for (i = scratches; i >= SLJIT_FIRST_SAVED_REG; i--) {
614 		offs -= (sljit_s32)(sizeof(sljit_sw));
615 		FAIL_IF(push_inst(compiler, STACK_STORE | base | T(i) | IMM(offs), MOVABLE_INS));
616 	}
617 
618 	if (args >= 1)
619 		FAIL_IF(push_inst(compiler, ADDU_W | SA(4) | TA(0) | D(SLJIT_S0), DR(SLJIT_S0)));
620 	if (args >= 2)
621 		FAIL_IF(push_inst(compiler, ADDU_W | SA(5) | TA(0) | D(SLJIT_S1), DR(SLJIT_S1)));
622 	if (args >= 3)
623 		FAIL_IF(push_inst(compiler, ADDU_W | SA(6) | TA(0) | D(SLJIT_S2), DR(SLJIT_S2)));
624 
625 	return SLJIT_SUCCESS;
626 }
627 
sljit_set_context(struct sljit_compiler * compiler,sljit_s32 options,sljit_s32 args,sljit_s32 scratches,sljit_s32 saveds,sljit_s32 fscratches,sljit_s32 fsaveds,sljit_s32 local_size)628 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_set_context(struct sljit_compiler *compiler,
629 	sljit_s32 options, sljit_s32 args, sljit_s32 scratches, sljit_s32 saveds,
630 	sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
631 {
632 	CHECK_ERROR();
633 	CHECK(check_sljit_set_context(compiler, options, args, scratches, saveds, fscratches, fsaveds, local_size));
634 	set_set_context(compiler, options, args, scratches, saveds, fscratches, fsaveds, local_size);
635 
636 	local_size += GET_SAVED_REGISTERS_SIZE(scratches, saveds, 1) + SLJIT_LOCALS_OFFSET;
637 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
638 	compiler->local_size = (local_size + 15) & ~0xf;
639 #else
640 	compiler->local_size = (local_size + 31) & ~0x1f;
641 #endif
642 	return SLJIT_SUCCESS;
643 }
644 
sljit_emit_return(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 src,sljit_sw srcw)645 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_return(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 src, sljit_sw srcw)
646 {
647 	sljit_s32 local_size, i, tmp, offs;
648 	sljit_ins base;
649 
650 	CHECK_ERROR();
651 	CHECK(check_sljit_emit_return(compiler, op, src, srcw));
652 
653 	FAIL_IF(emit_mov_before_return(compiler, op, src, srcw));
654 
655 	local_size = compiler->local_size;
656 	if (local_size <= SIMM_MAX)
657 		base = S(SLJIT_SP);
658 	else {
659 		FAIL_IF(load_immediate(compiler, DR(TMP_REG1), local_size));
660 		FAIL_IF(push_inst(compiler, ADDU_W | S(SLJIT_SP) | T(TMP_REG1) | D(TMP_REG1), DR(TMP_REG1)));
661 		base = S(TMP_REG1);
662 		local_size = 0;
663 	}
664 
665 	FAIL_IF(push_inst(compiler, STACK_LOAD | base | TA(RETURN_ADDR_REG) | IMM(local_size - (sljit_s32)sizeof(sljit_sw)), RETURN_ADDR_REG));
666 	offs = local_size - (sljit_s32)GET_SAVED_REGISTERS_SIZE(compiler->scratches, compiler->saveds, 1);
667 
668 	tmp = compiler->scratches;
669 	for (i = SLJIT_FIRST_SAVED_REG; i <= tmp; i++) {
670 		FAIL_IF(push_inst(compiler, STACK_LOAD | base | T(i) | IMM(offs), DR(i)));
671 		offs += (sljit_s32)(sizeof(sljit_sw));
672 	}
673 
674 	tmp = compiler->saveds < SLJIT_NUMBER_OF_SAVED_REGISTERS ? (SLJIT_S0 + 1 - compiler->saveds) : SLJIT_FIRST_SAVED_REG;
675 	for (i = tmp; i <= SLJIT_S0; i++) {
676 		FAIL_IF(push_inst(compiler, STACK_LOAD | base | T(i) | IMM(offs), DR(i)));
677 		offs += (sljit_s32)(sizeof(sljit_sw));
678 	}
679 
680 	SLJIT_ASSERT(offs == local_size - (sljit_sw)(sizeof(sljit_sw)));
681 
682 	FAIL_IF(push_inst(compiler, JR | SA(RETURN_ADDR_REG), UNMOVABLE_INS));
683 	if (compiler->local_size <= SIMM_MAX)
684 		return push_inst(compiler, ADDIU_W | S(SLJIT_SP) | T(SLJIT_SP) | IMM(compiler->local_size), UNMOVABLE_INS);
685 	else
686 		return push_inst(compiler, ADDU_W | S(TMP_REG1) | TA(0) | D(SLJIT_SP), UNMOVABLE_INS);
687 }
688 
689 #undef STACK_STORE
690 #undef STACK_LOAD
691 
692 /* --------------------------------------------------------------------- */
693 /*  Operators                                                            */
694 /* --------------------------------------------------------------------- */
695 
696 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
697 #define ARCH_32_64(a, b)	a
698 #else
699 #define ARCH_32_64(a, b)	b
700 #endif
701 
702 static const sljit_ins data_transfer_insts[16 + 4] = {
703 /* u w s */ ARCH_32_64(HI(43) /* sw */, HI(63) /* sd */),
704 /* u w l */ ARCH_32_64(HI(35) /* lw */, HI(55) /* ld */),
705 /* u b s */ HI(40) /* sb */,
706 /* u b l */ HI(36) /* lbu */,
707 /* u h s */ HI(41) /* sh */,
708 /* u h l */ HI(37) /* lhu */,
709 /* u i s */ HI(43) /* sw */,
710 /* u i l */ ARCH_32_64(HI(35) /* lw */, HI(39) /* lwu */),
711 
712 /* s w s */ ARCH_32_64(HI(43) /* sw */, HI(63) /* sd */),
713 /* s w l */ ARCH_32_64(HI(35) /* lw */, HI(55) /* ld */),
714 /* s b s */ HI(40) /* sb */,
715 /* s b l */ HI(32) /* lb */,
716 /* s h s */ HI(41) /* sh */,
717 /* s h l */ HI(33) /* lh */,
718 /* s i s */ HI(43) /* sw */,
719 /* s i l */ HI(35) /* lw */,
720 
721 /* d   s */ HI(61) /* sdc1 */,
722 /* d   l */ HI(53) /* ldc1 */,
723 /* s   s */ HI(57) /* swc1 */,
724 /* s   l */ HI(49) /* lwc1 */,
725 };
726 
727 #undef ARCH_32_64
728 
729 /* reg_ar is an absoulute register! */
730 
731 /* Can perform an operation using at most 1 instruction. */
getput_arg_fast(struct sljit_compiler * compiler,sljit_s32 flags,sljit_s32 reg_ar,sljit_s32 arg,sljit_sw argw)732 static sljit_s32 getput_arg_fast(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg_ar, sljit_s32 arg, sljit_sw argw)
733 {
734 	SLJIT_ASSERT(arg & SLJIT_MEM);
735 
736 	if ((!(flags & WRITE_BACK) || !(arg & REG_MASK)) && !(arg & OFFS_REG_MASK) && argw <= SIMM_MAX && argw >= SIMM_MIN) {
737 		/* Works for both absoulte and relative addresses. */
738 		if (SLJIT_UNLIKELY(flags & ARG_TEST))
739 			return 1;
740 		FAIL_IF(push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | S(arg & REG_MASK)
741 			| TA(reg_ar) | IMM(argw), ((flags & MEM_MASK) <= GPR_REG && (flags & LOAD_DATA)) ? reg_ar : MOVABLE_INS));
742 		return -1;
743 	}
744 	return 0;
745 }
746 
747 /* See getput_arg below.
748    Note: can_cache is called only for binary operators. Those
749    operators always uses word arguments without write back. */
can_cache(sljit_s32 arg,sljit_sw argw,sljit_s32 next_arg,sljit_sw next_argw)750 static sljit_s32 can_cache(sljit_s32 arg, sljit_sw argw, sljit_s32 next_arg, sljit_sw next_argw)
751 {
752 	SLJIT_ASSERT((arg & SLJIT_MEM) && (next_arg & SLJIT_MEM));
753 
754 	/* Simple operation except for updates. */
755 	if (arg & OFFS_REG_MASK) {
756 		argw &= 0x3;
757 		next_argw &= 0x3;
758 		if (argw && argw == next_argw && (arg == next_arg || (arg & OFFS_REG_MASK) == (next_arg & OFFS_REG_MASK)))
759 			return 1;
760 		return 0;
761 	}
762 
763 	if (arg == next_arg) {
764 		if (((next_argw - argw) <= SIMM_MAX && (next_argw - argw) >= SIMM_MIN))
765 			return 1;
766 		return 0;
767 	}
768 
769 	return 0;
770 }
771 
772 /* Emit the necessary instructions. See can_cache above. */
getput_arg(struct sljit_compiler * compiler,sljit_s32 flags,sljit_s32 reg_ar,sljit_s32 arg,sljit_sw argw,sljit_s32 next_arg,sljit_sw next_argw)773 static sljit_s32 getput_arg(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg_ar, sljit_s32 arg, sljit_sw argw, sljit_s32 next_arg, sljit_sw next_argw)
774 {
775 	sljit_s32 tmp_ar, base, delay_slot;
776 
777 	SLJIT_ASSERT(arg & SLJIT_MEM);
778 	if (!(next_arg & SLJIT_MEM)) {
779 		next_arg = 0;
780 		next_argw = 0;
781 	}
782 
783 	if ((flags & MEM_MASK) <= GPR_REG && (flags & LOAD_DATA)) {
784 		tmp_ar = reg_ar;
785 		delay_slot = reg_ar;
786 	} else {
787 		tmp_ar = DR(TMP_REG1);
788 		delay_slot = MOVABLE_INS;
789 	}
790 	base = arg & REG_MASK;
791 
792 	if (SLJIT_UNLIKELY(arg & OFFS_REG_MASK)) {
793 		if (SLJIT_UNLIKELY(flags & WRITE_BACK)) {
794 			SLJIT_ASSERT(argw == 0);
795 			FAIL_IF(push_inst(compiler, ADDU_W | S(base) | T(OFFS_REG(arg)) | D(base), DR(base)));
796 			return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | S(base) | TA(reg_ar), delay_slot);
797 		}
798 
799 		argw &= 0x3;
800 
801 		/* Using the cache. */
802 		if (argw == compiler->cache_argw) {
803 			if (arg == compiler->cache_arg)
804 				return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | S(TMP_REG3) | TA(reg_ar), delay_slot);
805 
806 			if ((SLJIT_MEM | (arg & OFFS_REG_MASK)) == compiler->cache_arg) {
807 				if (arg == next_arg && argw == (next_argw & 0x3)) {
808 					compiler->cache_arg = arg;
809 					compiler->cache_argw = argw;
810 					FAIL_IF(push_inst(compiler, ADDU_W | S(base) | T(TMP_REG3) | D(TMP_REG3), DR(TMP_REG3)));
811 					return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | S(TMP_REG3) | TA(reg_ar), delay_slot);
812 				}
813 				FAIL_IF(push_inst(compiler, ADDU_W | S(base) | T(TMP_REG3) | DA(tmp_ar), tmp_ar));
814 				return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | SA(tmp_ar) | TA(reg_ar), delay_slot);
815 			}
816 		}
817 
818 		if (SLJIT_UNLIKELY(argw)) {
819 			compiler->cache_arg = SLJIT_MEM | (arg & OFFS_REG_MASK);
820 			compiler->cache_argw = argw;
821 			FAIL_IF(push_inst(compiler, SLL_W | T(OFFS_REG(arg)) | D(TMP_REG3) | SH_IMM(argw), DR(TMP_REG3)));
822 		}
823 
824 		if (arg == next_arg && argw == (next_argw & 0x3)) {
825 			compiler->cache_arg = arg;
826 			compiler->cache_argw = argw;
827 			FAIL_IF(push_inst(compiler, ADDU_W | S(base) | T(!argw ? OFFS_REG(arg) : TMP_REG3) | D(TMP_REG3), DR(TMP_REG3)));
828 			tmp_ar = DR(TMP_REG3);
829 		}
830 		else
831 			FAIL_IF(push_inst(compiler, ADDU_W | S(base) | T(!argw ? OFFS_REG(arg) : TMP_REG3) | DA(tmp_ar), tmp_ar));
832 		return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | SA(tmp_ar) | TA(reg_ar), delay_slot);
833 	}
834 
835 	if (SLJIT_UNLIKELY(flags & WRITE_BACK) && base) {
836 		if (argw <= SIMM_MAX && argw >= SIMM_MIN) {
837 			if (argw)
838 				FAIL_IF(push_inst(compiler, ADDIU_W | S(base) | T(base) | IMM(argw), DR(base)));
839 		}
840 		else {
841 			if (compiler->cache_arg == SLJIT_MEM && argw - compiler->cache_argw <= SIMM_MAX && argw - compiler->cache_argw >= SIMM_MIN) {
842 				if (argw != compiler->cache_argw) {
843 					FAIL_IF(push_inst(compiler, ADDIU_W | S(TMP_REG3) | T(TMP_REG3) | IMM(argw - compiler->cache_argw), DR(TMP_REG3)));
844 					compiler->cache_argw = argw;
845 				}
846 				FAIL_IF(push_inst(compiler, ADDU_W | S(base) | T(TMP_REG3) | D(base), DR(base)));
847 			}
848 			else {
849 				compiler->cache_arg = SLJIT_MEM;
850 				compiler->cache_argw = argw;
851 				FAIL_IF(load_immediate(compiler, DR(TMP_REG3), argw));
852 				FAIL_IF(push_inst(compiler, ADDU_W | S(base) | T(TMP_REG3) | D(base), DR(base)));
853 			}
854 		}
855 		return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | S(base) | TA(reg_ar), delay_slot);
856 	}
857 
858 	if (compiler->cache_arg == arg && argw - compiler->cache_argw <= SIMM_MAX && argw - compiler->cache_argw >= SIMM_MIN) {
859 		if (argw != compiler->cache_argw) {
860 			FAIL_IF(push_inst(compiler, ADDIU_W | S(TMP_REG3) | T(TMP_REG3) | IMM(argw - compiler->cache_argw), DR(TMP_REG3)));
861 			compiler->cache_argw = argw;
862 		}
863 		return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | S(TMP_REG3) | TA(reg_ar), delay_slot);
864 	}
865 
866 	if (compiler->cache_arg == SLJIT_MEM && argw - compiler->cache_argw <= SIMM_MAX && argw - compiler->cache_argw >= SIMM_MIN) {
867 		if (argw != compiler->cache_argw)
868 			FAIL_IF(push_inst(compiler, ADDIU_W | S(TMP_REG3) | T(TMP_REG3) | IMM(argw - compiler->cache_argw), DR(TMP_REG3)));
869 	}
870 	else {
871 		compiler->cache_arg = SLJIT_MEM;
872 		FAIL_IF(load_immediate(compiler, DR(TMP_REG3), argw));
873 	}
874 	compiler->cache_argw = argw;
875 
876 	if (!base)
877 		return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | S(TMP_REG3) | TA(reg_ar), delay_slot);
878 
879 	if (arg == next_arg && next_argw - argw <= SIMM_MAX && next_argw - argw >= SIMM_MIN) {
880 		compiler->cache_arg = arg;
881 		FAIL_IF(push_inst(compiler, ADDU_W | S(TMP_REG3) | T(base) | D(TMP_REG3), DR(TMP_REG3)));
882 		return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | S(TMP_REG3) | TA(reg_ar), delay_slot);
883 	}
884 
885 	FAIL_IF(push_inst(compiler, ADDU_W | S(TMP_REG3) | T(base) | DA(tmp_ar), tmp_ar));
886 	return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | SA(tmp_ar) | TA(reg_ar), delay_slot);
887 }
888 
emit_op_mem(struct sljit_compiler * compiler,sljit_s32 flags,sljit_s32 reg_ar,sljit_s32 arg,sljit_sw argw)889 static SLJIT_INLINE sljit_s32 emit_op_mem(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg_ar, sljit_s32 arg, sljit_sw argw)
890 {
891 	if (getput_arg_fast(compiler, flags, reg_ar, arg, argw))
892 		return compiler->error;
893 	compiler->cache_arg = 0;
894 	compiler->cache_argw = 0;
895 	return getput_arg(compiler, flags, reg_ar, arg, argw, 0, 0);
896 }
897 
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)898 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)
899 {
900 	if (getput_arg_fast(compiler, flags, reg, arg1, arg1w))
901 		return compiler->error;
902 	return getput_arg(compiler, flags, reg, arg1, arg1w, arg2, arg2w);
903 }
904 
emit_op(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 flags,sljit_s32 dst,sljit_sw dstw,sljit_s32 src1,sljit_sw src1w,sljit_s32 src2,sljit_sw src2w)905 static sljit_s32 emit_op(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 flags,
906 	sljit_s32 dst, sljit_sw dstw,
907 	sljit_s32 src1, sljit_sw src1w,
908 	sljit_s32 src2, sljit_sw src2w)
909 {
910 	/* arg1 goes to TMP_REG1 or src reg
911 	   arg2 goes to TMP_REG2, imm or src reg
912 	   TMP_REG3 can be used for caching
913 	   result goes to TMP_REG2, so put result can use TMP_REG1 and TMP_REG3. */
914 	sljit_s32 dst_r = TMP_REG2;
915 	sljit_s32 src1_r;
916 	sljit_sw src2_r = 0;
917 	sljit_s32 sugg_src2_r = TMP_REG2;
918 
919 	if (!(flags & ALT_KEEP_CACHE)) {
920 		compiler->cache_arg = 0;
921 		compiler->cache_argw = 0;
922 	}
923 
924 	if (SLJIT_UNLIKELY(dst == SLJIT_UNUSED)) {
925 		SLJIT_ASSERT(HAS_FLAGS(op));
926 		flags |= UNUSED_DEST;
927 	}
928 	else if (FAST_IS_REG(dst)) {
929 		dst_r = dst;
930 		flags |= REG_DEST;
931 		if (op >= SLJIT_MOV && op <= SLJIT_MOVU_S32)
932 			sugg_src2_r = dst_r;
933 	}
934 	else if ((dst & SLJIT_MEM) && !getput_arg_fast(compiler, flags | ARG_TEST, DR(TMP_REG1), dst, dstw))
935 		flags |= SLOW_DEST;
936 
937 	if (flags & IMM_OP) {
938 		if ((src2 & SLJIT_IMM) && src2w) {
939 			if ((!(flags & LOGICAL_OP) && (src2w <= SIMM_MAX && src2w >= SIMM_MIN))
940 				|| ((flags & LOGICAL_OP) && !(src2w & ~UIMM_MAX))) {
941 				flags |= SRC2_IMM;
942 				src2_r = src2w;
943 			}
944 		}
945 		if (!(flags & SRC2_IMM) && (flags & CUMULATIVE_OP) && (src1 & SLJIT_IMM) && src1w) {
946 			if ((!(flags & LOGICAL_OP) && (src1w <= SIMM_MAX && src1w >= SIMM_MIN))
947 				|| ((flags & LOGICAL_OP) && !(src1w & ~UIMM_MAX))) {
948 				flags |= SRC2_IMM;
949 				src2_r = src1w;
950 
951 				/* And swap arguments. */
952 				src1 = src2;
953 				src1w = src2w;
954 				src2 = SLJIT_IMM;
955 				/* src2w = src2_r unneeded. */
956 			}
957 		}
958 	}
959 
960 	/* Source 1. */
961 	if (FAST_IS_REG(src1)) {
962 		src1_r = src1;
963 		flags |= REG1_SOURCE;
964 	}
965 	else if (src1 & SLJIT_IMM) {
966 		if (src1w) {
967 			FAIL_IF(load_immediate(compiler, DR(TMP_REG1), src1w));
968 			src1_r = TMP_REG1;
969 		}
970 		else
971 			src1_r = 0;
972 	}
973 	else {
974 		if (getput_arg_fast(compiler, flags | LOAD_DATA, DR(TMP_REG1), src1, src1w))
975 			FAIL_IF(compiler->error);
976 		else
977 			flags |= SLOW_SRC1;
978 		src1_r = TMP_REG1;
979 	}
980 
981 	/* Source 2. */
982 	if (FAST_IS_REG(src2)) {
983 		src2_r = src2;
984 		flags |= REG2_SOURCE;
985 		if (!(flags & REG_DEST) && op >= SLJIT_MOV && op <= SLJIT_MOVU_S32)
986 			dst_r = src2_r;
987 	}
988 	else if (src2 & SLJIT_IMM) {
989 		if (!(flags & SRC2_IMM)) {
990 			if (src2w) {
991 				FAIL_IF(load_immediate(compiler, DR(sugg_src2_r), src2w));
992 				src2_r = sugg_src2_r;
993 			}
994 			else {
995 				src2_r = 0;
996 				if ((op >= SLJIT_MOV && op <= SLJIT_MOVU_S32) && (dst & SLJIT_MEM))
997 					dst_r = 0;
998 			}
999 		}
1000 	}
1001 	else {
1002 		if (getput_arg_fast(compiler, flags | LOAD_DATA, DR(sugg_src2_r), src2, src2w))
1003 			FAIL_IF(compiler->error);
1004 		else
1005 			flags |= SLOW_SRC2;
1006 		src2_r = sugg_src2_r;
1007 	}
1008 
1009 	if ((flags & (SLOW_SRC1 | SLOW_SRC2)) == (SLOW_SRC1 | SLOW_SRC2)) {
1010 		SLJIT_ASSERT(src2_r == TMP_REG2);
1011 		if (!can_cache(src1, src1w, src2, src2w) && can_cache(src1, src1w, dst, dstw)) {
1012 			FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, DR(TMP_REG2), src2, src2w, src1, src1w));
1013 			FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, DR(TMP_REG1), src1, src1w, dst, dstw));
1014 		}
1015 		else {
1016 			FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, DR(TMP_REG1), src1, src1w, src2, src2w));
1017 			FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, DR(TMP_REG2), src2, src2w, dst, dstw));
1018 		}
1019 	}
1020 	else if (flags & SLOW_SRC1)
1021 		FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, DR(TMP_REG1), src1, src1w, dst, dstw));
1022 	else if (flags & SLOW_SRC2)
1023 		FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, DR(sugg_src2_r), src2, src2w, dst, dstw));
1024 
1025 	FAIL_IF(emit_single_op(compiler, op, flags, dst_r, src1_r, src2_r));
1026 
1027 	if (dst & SLJIT_MEM) {
1028 		if (!(flags & SLOW_DEST)) {
1029 			getput_arg_fast(compiler, flags, DR(dst_r), dst, dstw);
1030 			return compiler->error;
1031 		}
1032 		return getput_arg(compiler, flags, DR(dst_r), dst, dstw, 0, 0);
1033 	}
1034 
1035 	return SLJIT_SUCCESS;
1036 }
1037 
sljit_emit_op0(struct sljit_compiler * compiler,sljit_s32 op)1038 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op0(struct sljit_compiler *compiler, sljit_s32 op)
1039 {
1040 #if (defined SLJIT_CONFIG_MIPS_64 && SLJIT_CONFIG_MIPS_64)
1041 	sljit_s32 int_op = op & SLJIT_I32_OP;
1042 #endif
1043 
1044 	CHECK_ERROR();
1045 	CHECK(check_sljit_emit_op0(compiler, op));
1046 
1047 	op = GET_OPCODE(op);
1048 	switch (op) {
1049 	case SLJIT_BREAKPOINT:
1050 		return push_inst(compiler, BREAK, UNMOVABLE_INS);
1051 	case SLJIT_NOP:
1052 		return push_inst(compiler, NOP, UNMOVABLE_INS);
1053 	case SLJIT_LMUL_UW:
1054 	case SLJIT_LMUL_SW:
1055 #if (defined SLJIT_CONFIG_MIPS_64 && SLJIT_CONFIG_MIPS_64)
1056 		FAIL_IF(push_inst(compiler, (op == SLJIT_LMUL_UW ? DMULTU : DMULT) | S(SLJIT_R0) | T(SLJIT_R1), MOVABLE_INS));
1057 #else
1058 		FAIL_IF(push_inst(compiler, (op == SLJIT_LMUL_UW ? MULTU : MULT) | S(SLJIT_R0) | T(SLJIT_R1), MOVABLE_INS));
1059 #endif
1060 		FAIL_IF(push_inst(compiler, MFLO | D(SLJIT_R0), DR(SLJIT_R0)));
1061 		return push_inst(compiler, MFHI | D(SLJIT_R1), DR(SLJIT_R1));
1062 	case SLJIT_DIVMOD_UW:
1063 	case SLJIT_DIVMOD_SW:
1064 	case SLJIT_DIV_UW:
1065 	case SLJIT_DIV_SW:
1066 		SLJIT_COMPILE_ASSERT((SLJIT_DIVMOD_UW & 0x2) == 0 && SLJIT_DIV_UW - 0x2 == SLJIT_DIVMOD_UW, bad_div_opcode_assignments);
1067 #if !(defined SLJIT_MIPS_R1 && SLJIT_MIPS_R1)
1068 		FAIL_IF(push_inst(compiler, NOP, UNMOVABLE_INS));
1069 		FAIL_IF(push_inst(compiler, NOP, UNMOVABLE_INS));
1070 #endif
1071 
1072 #if (defined SLJIT_CONFIG_MIPS_64 && SLJIT_CONFIG_MIPS_64)
1073 		if (int_op)
1074 			FAIL_IF(push_inst(compiler, ((op | 0x2) == SLJIT_DIV_UW ? DIVU : DIV) | S(SLJIT_R0) | T(SLJIT_R1), MOVABLE_INS));
1075 		else
1076 			FAIL_IF(push_inst(compiler, ((op | 0x2) == SLJIT_DIV_UW ? DDIVU : DDIV) | S(SLJIT_R0) | T(SLJIT_R1), MOVABLE_INS));
1077 #else
1078 		FAIL_IF(push_inst(compiler, ((op | 0x2) == SLJIT_DIV_UW ? DIVU : DIV) | S(SLJIT_R0) | T(SLJIT_R1), MOVABLE_INS));
1079 #endif
1080 
1081 		FAIL_IF(push_inst(compiler, MFLO | D(SLJIT_R0), DR(SLJIT_R0)));
1082 		return (op >= SLJIT_DIV_UW) ? SLJIT_SUCCESS : push_inst(compiler, MFHI | D(SLJIT_R1), DR(SLJIT_R1));
1083 	}
1084 
1085 	return SLJIT_SUCCESS;
1086 }
1087 
1088 #if (defined SLJIT_MIPS_R1 && SLJIT_MIPS_R1)
emit_prefetch(struct sljit_compiler * compiler,sljit_s32 src,sljit_sw srcw)1089 static sljit_s32 emit_prefetch(struct sljit_compiler *compiler,
1090         sljit_s32 src, sljit_sw srcw)
1091 {
1092 	if (!(src & OFFS_REG_MASK)) {
1093 		if (srcw <= SIMM_MAX && srcw >= SIMM_MIN)
1094 			return push_inst(compiler, PREF | S(src & REG_MASK) | IMM(srcw), MOVABLE_INS);
1095 
1096 		FAIL_IF(load_immediate(compiler, DR(TMP_REG1), srcw));
1097 		return push_inst(compiler, PREFX | S(src & REG_MASK) | T(TMP_REG1), MOVABLE_INS);
1098 	}
1099 
1100 	srcw &= 0x3;
1101 
1102 	if (SLJIT_UNLIKELY(srcw != 0)) {
1103 		FAIL_IF(push_inst(compiler, SLL_W | T(OFFS_REG(src)) | D(TMP_REG1) | SH_IMM(srcw), DR(TMP_REG1)));
1104 		return push_inst(compiler, PREFX | S(src & REG_MASK) | T(TMP_REG1), MOVABLE_INS);
1105 	}
1106 
1107 	return push_inst(compiler, PREFX | S(src & REG_MASK) | T(OFFS_REG(src)), MOVABLE_INS);
1108 }
1109 #endif
1110 
sljit_emit_op1(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 src,sljit_sw srcw)1111 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op1(struct sljit_compiler *compiler, sljit_s32 op,
1112 	sljit_s32 dst, sljit_sw dstw,
1113 	sljit_s32 src, sljit_sw srcw)
1114 {
1115 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
1116 #	define flags 0
1117 #else
1118 	sljit_s32 flags = 0;
1119 #endif
1120 
1121 	CHECK_ERROR();
1122 	CHECK(check_sljit_emit_op1(compiler, op, dst, dstw, src, srcw));
1123 	ADJUST_LOCAL_OFFSET(dst, dstw);
1124 	ADJUST_LOCAL_OFFSET(src, srcw);
1125 
1126 	if (dst == SLJIT_UNUSED && !HAS_FLAGS(op)) {
1127 #if (defined SLJIT_MIPS_R1 && SLJIT_MIPS_R1)
1128 		if (op <= SLJIT_MOV_P && (src & SLJIT_MEM))
1129 			return emit_prefetch(compiler, src, srcw);
1130 #endif
1131 		return SLJIT_SUCCESS;
1132 	}
1133 
1134 #if (defined SLJIT_CONFIG_MIPS_64 && SLJIT_CONFIG_MIPS_64)
1135 	if ((op & SLJIT_I32_OP) && GET_OPCODE(op) >= SLJIT_NOT) {
1136 		flags |= INT_DATA | SIGNED_DATA;
1137 		if (src & SLJIT_IMM)
1138 			srcw = (sljit_s32)srcw;
1139 	}
1140 #endif
1141 
1142 	switch (GET_OPCODE(op)) {
1143 	case SLJIT_MOV:
1144 	case SLJIT_MOV_P:
1145 		return emit_op(compiler, SLJIT_MOV, WORD_DATA, dst, dstw, TMP_REG1, 0, src, srcw);
1146 
1147 	case SLJIT_MOV_U32:
1148 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
1149 		return emit_op(compiler, SLJIT_MOV_U32, INT_DATA, dst, dstw, TMP_REG1, 0, src, srcw);
1150 #else
1151 		return emit_op(compiler, SLJIT_MOV_U32, INT_DATA, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_u32)srcw : srcw);
1152 #endif
1153 
1154 	case SLJIT_MOV_S32:
1155 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
1156 		return emit_op(compiler, SLJIT_MOV_S32, INT_DATA | SIGNED_DATA, dst, dstw, TMP_REG1, 0, src, srcw);
1157 #else
1158 		return emit_op(compiler, SLJIT_MOV_S32, INT_DATA | SIGNED_DATA, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_s32)srcw : srcw);
1159 #endif
1160 
1161 	case SLJIT_MOV_U8:
1162 		return emit_op(compiler, SLJIT_MOV_U8, BYTE_DATA, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_u8)srcw : srcw);
1163 
1164 	case SLJIT_MOV_S8:
1165 		return emit_op(compiler, SLJIT_MOV_S8, BYTE_DATA | SIGNED_DATA, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_s8)srcw : srcw);
1166 
1167 	case SLJIT_MOV_U16:
1168 		return emit_op(compiler, SLJIT_MOV_U16, HALF_DATA, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_u16)srcw : srcw);
1169 
1170 	case SLJIT_MOV_S16:
1171 		return emit_op(compiler, SLJIT_MOV_S16, HALF_DATA | SIGNED_DATA, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_s16)srcw : srcw);
1172 
1173 	case SLJIT_MOVU:
1174 	case SLJIT_MOVU_P:
1175 		return emit_op(compiler, SLJIT_MOV, WORD_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, srcw);
1176 
1177 	case SLJIT_MOVU_U32:
1178 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
1179 		return emit_op(compiler, SLJIT_MOV_U32, INT_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, srcw);
1180 #else
1181 		return emit_op(compiler, SLJIT_MOV_U32, INT_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_u32)srcw : srcw);
1182 #endif
1183 
1184 	case SLJIT_MOVU_S32:
1185 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
1186 		return emit_op(compiler, SLJIT_MOV_S32, INT_DATA | SIGNED_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, srcw);
1187 #else
1188 		return emit_op(compiler, SLJIT_MOV_S32, INT_DATA | SIGNED_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_s32)srcw : srcw);
1189 #endif
1190 
1191 	case SLJIT_MOVU_U8:
1192 		return emit_op(compiler, SLJIT_MOV_U8, BYTE_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_u8)srcw : srcw);
1193 
1194 	case SLJIT_MOVU_S8:
1195 		return emit_op(compiler, SLJIT_MOV_S8, BYTE_DATA | SIGNED_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_s8)srcw : srcw);
1196 
1197 	case SLJIT_MOVU_U16:
1198 		return emit_op(compiler, SLJIT_MOV_U16, HALF_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_u16)srcw : srcw);
1199 
1200 	case SLJIT_MOVU_S16:
1201 		return emit_op(compiler, SLJIT_MOV_S16, HALF_DATA | SIGNED_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_s16)srcw : srcw);
1202 
1203 	case SLJIT_NOT:
1204 		return emit_op(compiler, op, flags, dst, dstw, TMP_REG1, 0, src, srcw);
1205 
1206 	case SLJIT_NEG:
1207 		return emit_op(compiler, SLJIT_SUB | GET_ALL_FLAGS(op), flags | IMM_OP, dst, dstw, SLJIT_IMM, 0, src, srcw);
1208 
1209 	case SLJIT_CLZ:
1210 		return emit_op(compiler, op, flags, dst, dstw, TMP_REG1, 0, src, srcw);
1211 	}
1212 
1213 	return SLJIT_SUCCESS;
1214 
1215 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
1216 #	undef flags
1217 #endif
1218 }
1219 
sljit_emit_op2(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 src1,sljit_sw src1w,sljit_s32 src2,sljit_sw src2w)1220 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op2(struct sljit_compiler *compiler, sljit_s32 op,
1221 	sljit_s32 dst, sljit_sw dstw,
1222 	sljit_s32 src1, sljit_sw src1w,
1223 	sljit_s32 src2, sljit_sw src2w)
1224 {
1225 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
1226 #	define flags 0
1227 #else
1228 	sljit_s32 flags = 0;
1229 #endif
1230 
1231 	CHECK_ERROR();
1232 	CHECK(check_sljit_emit_op2(compiler, op, dst, dstw, src1, src1w, src2, src2w));
1233 	ADJUST_LOCAL_OFFSET(dst, dstw);
1234 	ADJUST_LOCAL_OFFSET(src1, src1w);
1235 	ADJUST_LOCAL_OFFSET(src2, src2w);
1236 
1237 	if (dst == SLJIT_UNUSED && !HAS_FLAGS(op))
1238 		return SLJIT_SUCCESS;
1239 
1240 #if (defined SLJIT_CONFIG_MIPS_64 && SLJIT_CONFIG_MIPS_64)
1241 	if (op & SLJIT_I32_OP) {
1242 		flags |= INT_DATA | SIGNED_DATA;
1243 		if (src1 & SLJIT_IMM)
1244 			src1w = (sljit_s32)src1w;
1245 		if (src2 & SLJIT_IMM)
1246 			src2w = (sljit_s32)src2w;
1247 	}
1248 #endif
1249 
1250 	switch (GET_OPCODE(op)) {
1251 	case SLJIT_ADD:
1252 	case SLJIT_ADDC:
1253 		return emit_op(compiler, op, flags | CUMULATIVE_OP | IMM_OP, dst, dstw, src1, src1w, src2, src2w);
1254 
1255 	case SLJIT_SUB:
1256 	case SLJIT_SUBC:
1257 		return emit_op(compiler, op, flags | IMM_OP, dst, dstw, src1, src1w, src2, src2w);
1258 
1259 	case SLJIT_MUL:
1260 		return emit_op(compiler, op, flags | CUMULATIVE_OP, dst, dstw, src1, src1w, src2, src2w);
1261 
1262 	case SLJIT_AND:
1263 	case SLJIT_OR:
1264 	case SLJIT_XOR:
1265 		return emit_op(compiler, op, flags | CUMULATIVE_OP | LOGICAL_OP | IMM_OP, dst, dstw, src1, src1w, src2, src2w);
1266 
1267 	case SLJIT_SHL:
1268 	case SLJIT_LSHR:
1269 	case SLJIT_ASHR:
1270 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
1271 		if (src2 & SLJIT_IMM)
1272 			src2w &= 0x1f;
1273 #else
1274 		if (src2 & SLJIT_IMM) {
1275 			if (op & SLJIT_I32_OP)
1276 				src2w &= 0x1f;
1277 			else
1278 				src2w &= 0x3f;
1279 		}
1280 #endif
1281 		return emit_op(compiler, op, flags | IMM_OP, dst, dstw, src1, src1w, src2, src2w);
1282 	}
1283 
1284 	return SLJIT_SUCCESS;
1285 
1286 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
1287 #	undef flags
1288 #endif
1289 }
1290 
sljit_get_register_index(sljit_s32 reg)1291 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_register_index(sljit_s32 reg)
1292 {
1293 	CHECK_REG_INDEX(check_sljit_get_register_index(reg));
1294 	return reg_map[reg];
1295 }
1296 
sljit_get_float_register_index(sljit_s32 reg)1297 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_float_register_index(sljit_s32 reg)
1298 {
1299 	CHECK_REG_INDEX(check_sljit_get_float_register_index(reg));
1300 	return reg << 1;
1301 }
1302 
sljit_emit_op_custom(struct sljit_compiler * compiler,void * instruction,sljit_s32 size)1303 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_custom(struct sljit_compiler *compiler,
1304 	void *instruction, sljit_s32 size)
1305 {
1306 	CHECK_ERROR();
1307 	CHECK(check_sljit_emit_op_custom(compiler, instruction, size));
1308 
1309 	return push_inst(compiler, *(sljit_ins*)instruction, UNMOVABLE_INS);
1310 }
1311 
1312 /* --------------------------------------------------------------------- */
1313 /*  Floating point operators                                             */
1314 /* --------------------------------------------------------------------- */
1315 
1316 #define FLOAT_DATA(op) (DOUBLE_DATA | ((op & SLJIT_F32_OP) >> 7))
1317 #define FMT(op) (((op & SLJIT_F32_OP) ^ SLJIT_F32_OP) << (21 - 8))
1318 
sljit_emit_fop1_conv_sw_from_f64(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 src,sljit_sw srcw)1319 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_conv_sw_from_f64(struct sljit_compiler *compiler, sljit_s32 op,
1320 	sljit_s32 dst, sljit_sw dstw,
1321 	sljit_s32 src, sljit_sw srcw)
1322 {
1323 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
1324 #	define flags 0
1325 #else
1326 	sljit_s32 flags = (GET_OPCODE(op) == SLJIT_CONV_SW_FROM_F64) << 21;
1327 #endif
1328 
1329 	if (src & SLJIT_MEM) {
1330 		FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG1, src, srcw, dst, dstw));
1331 		src = TMP_FREG1;
1332 	}
1333 	else
1334 		src <<= 1;
1335 
1336 	FAIL_IF(push_inst(compiler, (TRUNC_W_S ^ (flags >> 19)) | FMT(op) | FS(src) | FD(TMP_FREG1), MOVABLE_INS));
1337 
1338 	if (FAST_IS_REG(dst))
1339 		return push_inst(compiler, MFC1 | flags | T(dst) | FS(TMP_FREG1), MOVABLE_INS);
1340 
1341 	/* Store the integer value from a VFP register. */
1342 	return emit_op_mem2(compiler, flags ? DOUBLE_DATA : SINGLE_DATA, TMP_FREG1, dst, dstw, 0, 0);
1343 
1344 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
1345 #	undef is_long
1346 #endif
1347 }
1348 
sljit_emit_fop1_conv_f64_from_sw(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 src,sljit_sw srcw)1349 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_conv_f64_from_sw(struct sljit_compiler *compiler, sljit_s32 op,
1350 	sljit_s32 dst, sljit_sw dstw,
1351 	sljit_s32 src, sljit_sw srcw)
1352 {
1353 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
1354 #	define flags 0
1355 #else
1356 	sljit_s32 flags = (GET_OPCODE(op) == SLJIT_CONV_F64_FROM_SW) << 21;
1357 #endif
1358 
1359 	sljit_s32 dst_r = FAST_IS_REG(dst) ? (dst << 1) : TMP_FREG1;
1360 
1361 	if (FAST_IS_REG(src))
1362 		FAIL_IF(push_inst(compiler, MTC1 | flags | T(src) | FS(TMP_FREG1), MOVABLE_INS));
1363 	else if (src & SLJIT_MEM) {
1364 		/* Load the integer value into a VFP register. */
1365 		FAIL_IF(emit_op_mem2(compiler, ((flags) ? DOUBLE_DATA : SINGLE_DATA) | LOAD_DATA, TMP_FREG1, src, srcw, dst, dstw));
1366 	}
1367 	else {
1368 #if (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64)
1369 		if (GET_OPCODE(op) == SLJIT_CONV_F64_FROM_S32)
1370 			srcw = (sljit_s32)srcw;
1371 #endif
1372 		FAIL_IF(load_immediate(compiler, DR(TMP_REG1), srcw));
1373 		FAIL_IF(push_inst(compiler, MTC1 | flags | T(TMP_REG1) | FS(TMP_FREG1), MOVABLE_INS));
1374 	}
1375 
1376 	FAIL_IF(push_inst(compiler, CVT_S_S | flags | (4 << 21) | (((op & SLJIT_F32_OP) ^ SLJIT_F32_OP) >> 8) | FS(TMP_FREG1) | FD(dst_r), MOVABLE_INS));
1377 
1378 	if (dst & SLJIT_MEM)
1379 		return emit_op_mem2(compiler, FLOAT_DATA(op), TMP_FREG1, dst, dstw, 0, 0);
1380 	return SLJIT_SUCCESS;
1381 
1382 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
1383 #	undef flags
1384 #endif
1385 }
1386 
sljit_emit_fop1_cmp(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 src1,sljit_sw src1w,sljit_s32 src2,sljit_sw src2w)1387 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_cmp(struct sljit_compiler *compiler, sljit_s32 op,
1388 	sljit_s32 src1, sljit_sw src1w,
1389 	sljit_s32 src2, sljit_sw src2w)
1390 {
1391 	sljit_ins inst;
1392 
1393 	if (src1 & SLJIT_MEM) {
1394 		FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG1, src1, src1w, src2, src2w));
1395 		src1 = TMP_FREG1;
1396 	}
1397 	else
1398 		src1 <<= 1;
1399 
1400 	if (src2 & SLJIT_MEM) {
1401 		FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG2, src2, src2w, 0, 0));
1402 		src2 = TMP_FREG2;
1403 	}
1404 	else
1405 		src2 <<= 1;
1406 
1407 	switch (GET_FLAG_TYPE(op)) {
1408 	case SLJIT_EQUAL_F64:
1409 	case SLJIT_NOT_EQUAL_F64:
1410 		inst = C_UEQ_S;
1411 		break;
1412 	case SLJIT_LESS_F64:
1413 	case SLJIT_GREATER_EQUAL_F64:
1414 		inst = C_ULT_S;
1415 		break;
1416 	case SLJIT_GREATER_F64:
1417 	case SLJIT_LESS_EQUAL_F64:
1418 		inst = C_ULE_S;
1419 		break;
1420 	default:
1421 		SLJIT_ASSERT(GET_FLAG_TYPE(op) == SLJIT_UNORDERED_F64 || GET_FLAG_TYPE(op) == SLJIT_ORDERED_F64);
1422 		inst = C_UN_S;
1423 		break;
1424 	}
1425 
1426 	return push_inst(compiler, inst | FMT(op) | FT(src2) | FS(src1), UNMOVABLE_INS);
1427 }
1428 
sljit_emit_fop1(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 src,sljit_sw srcw)1429 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop1(struct sljit_compiler *compiler, sljit_s32 op,
1430 	sljit_s32 dst, sljit_sw dstw,
1431 	sljit_s32 src, sljit_sw srcw)
1432 {
1433 	sljit_s32 dst_r;
1434 
1435 	CHECK_ERROR();
1436 	compiler->cache_arg = 0;
1437 	compiler->cache_argw = 0;
1438 
1439 	SLJIT_COMPILE_ASSERT((SLJIT_F32_OP == 0x100) && !(DOUBLE_DATA & 0x2), float_transfer_bit_error);
1440 	SELECT_FOP1_OPERATION_WITH_CHECKS(compiler, op, dst, dstw, src, srcw);
1441 
1442 	if (GET_OPCODE(op) == SLJIT_CONV_F64_FROM_F32)
1443 		op ^= SLJIT_F32_OP;
1444 
1445 	dst_r = FAST_IS_REG(dst) ? (dst << 1) : TMP_FREG1;
1446 
1447 	if (src & SLJIT_MEM) {
1448 		FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op) | LOAD_DATA, dst_r, src, srcw, dst, dstw));
1449 		src = dst_r;
1450 	}
1451 	else
1452 		src <<= 1;
1453 
1454 	switch (GET_OPCODE(op)) {
1455 	case SLJIT_MOV_F64:
1456 		if (src != dst_r) {
1457 			if (dst_r != TMP_FREG1)
1458 				FAIL_IF(push_inst(compiler, MOV_S | FMT(op) | FS(src) | FD(dst_r), MOVABLE_INS));
1459 			else
1460 				dst_r = src;
1461 		}
1462 		break;
1463 	case SLJIT_NEG_F64:
1464 		FAIL_IF(push_inst(compiler, NEG_S | FMT(op) | FS(src) | FD(dst_r), MOVABLE_INS));
1465 		break;
1466 	case SLJIT_ABS_F64:
1467 		FAIL_IF(push_inst(compiler, ABS_S | FMT(op) | FS(src) | FD(dst_r), MOVABLE_INS));
1468 		break;
1469 	case SLJIT_CONV_F64_FROM_F32:
1470 		FAIL_IF(push_inst(compiler, CVT_S_S | ((op & SLJIT_F32_OP) ? 1 : (1 << 21)) | FS(src) | FD(dst_r), MOVABLE_INS));
1471 		op ^= SLJIT_F32_OP;
1472 		break;
1473 	}
1474 
1475 	if (dst & SLJIT_MEM)
1476 		return emit_op_mem2(compiler, FLOAT_DATA(op), dst_r, dst, dstw, 0, 0);
1477 	return SLJIT_SUCCESS;
1478 }
1479 
sljit_emit_fop2(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 src1,sljit_sw src1w,sljit_s32 src2,sljit_sw src2w)1480 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop2(struct sljit_compiler *compiler, sljit_s32 op,
1481 	sljit_s32 dst, sljit_sw dstw,
1482 	sljit_s32 src1, sljit_sw src1w,
1483 	sljit_s32 src2, sljit_sw src2w)
1484 {
1485 	sljit_s32 dst_r, flags = 0;
1486 
1487 	CHECK_ERROR();
1488 	CHECK(check_sljit_emit_fop2(compiler, op, dst, dstw, src1, src1w, src2, src2w));
1489 	ADJUST_LOCAL_OFFSET(dst, dstw);
1490 	ADJUST_LOCAL_OFFSET(src1, src1w);
1491 	ADJUST_LOCAL_OFFSET(src2, src2w);
1492 
1493 	compiler->cache_arg = 0;
1494 	compiler->cache_argw = 0;
1495 
1496 	dst_r = FAST_IS_REG(dst) ? (dst << 1) : TMP_FREG2;
1497 
1498 	if (src1 & SLJIT_MEM) {
1499 		if (getput_arg_fast(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG1, src1, src1w)) {
1500 			FAIL_IF(compiler->error);
1501 			src1 = TMP_FREG1;
1502 		} else
1503 			flags |= SLOW_SRC1;
1504 	}
1505 	else
1506 		src1 <<= 1;
1507 
1508 	if (src2 & SLJIT_MEM) {
1509 		if (getput_arg_fast(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG2, src2, src2w)) {
1510 			FAIL_IF(compiler->error);
1511 			src2 = TMP_FREG2;
1512 		} else
1513 			flags |= SLOW_SRC2;
1514 	}
1515 	else
1516 		src2 <<= 1;
1517 
1518 	if ((flags & (SLOW_SRC1 | SLOW_SRC2)) == (SLOW_SRC1 | SLOW_SRC2)) {
1519 		if (!can_cache(src1, src1w, src2, src2w) && can_cache(src1, src1w, dst, dstw)) {
1520 			FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG2, src2, src2w, src1, src1w));
1521 			FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG1, src1, src1w, dst, dstw));
1522 		}
1523 		else {
1524 			FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG1, src1, src1w, src2, src2w));
1525 			FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG2, src2, src2w, dst, dstw));
1526 		}
1527 	}
1528 	else if (flags & SLOW_SRC1)
1529 		FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG1, src1, src1w, dst, dstw));
1530 	else if (flags & SLOW_SRC2)
1531 		FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG2, src2, src2w, dst, dstw));
1532 
1533 	if (flags & SLOW_SRC1)
1534 		src1 = TMP_FREG1;
1535 	if (flags & SLOW_SRC2)
1536 		src2 = TMP_FREG2;
1537 
1538 	switch (GET_OPCODE(op)) {
1539 	case SLJIT_ADD_F64:
1540 		FAIL_IF(push_inst(compiler, ADD_S | FMT(op) | FT(src2) | FS(src1) | FD(dst_r), MOVABLE_INS));
1541 		break;
1542 
1543 	case SLJIT_SUB_F64:
1544 		FAIL_IF(push_inst(compiler, SUB_S | FMT(op) | FT(src2) | FS(src1) | FD(dst_r), MOVABLE_INS));
1545 		break;
1546 
1547 	case SLJIT_MUL_F64:
1548 		FAIL_IF(push_inst(compiler, MUL_S | FMT(op) | FT(src2) | FS(src1) | FD(dst_r), MOVABLE_INS));
1549 		break;
1550 
1551 	case SLJIT_DIV_F64:
1552 		FAIL_IF(push_inst(compiler, DIV_S | FMT(op) | FT(src2) | FS(src1) | FD(dst_r), MOVABLE_INS));
1553 		break;
1554 	}
1555 
1556 	if (dst_r == TMP_FREG2)
1557 		FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op), TMP_FREG2, dst, dstw, 0, 0));
1558 
1559 	return SLJIT_SUCCESS;
1560 }
1561 
1562 /* --------------------------------------------------------------------- */
1563 /*  Other instructions                                                   */
1564 /* --------------------------------------------------------------------- */
1565 
sljit_emit_fast_enter(struct sljit_compiler * compiler,sljit_s32 dst,sljit_sw dstw)1566 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fast_enter(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw)
1567 {
1568 	CHECK_ERROR();
1569 	CHECK(check_sljit_emit_fast_enter(compiler, dst, dstw));
1570 	ADJUST_LOCAL_OFFSET(dst, dstw);
1571 
1572 	if (FAST_IS_REG(dst))
1573 		return push_inst(compiler, ADDU_W | SA(RETURN_ADDR_REG) | TA(0) | D(dst), DR(dst));
1574 
1575 	/* Memory. */
1576 	return emit_op_mem(compiler, WORD_DATA, RETURN_ADDR_REG, dst, dstw);
1577 }
1578 
sljit_emit_fast_return(struct sljit_compiler * compiler,sljit_s32 src,sljit_sw srcw)1579 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fast_return(struct sljit_compiler *compiler, sljit_s32 src, sljit_sw srcw)
1580 {
1581 	CHECK_ERROR();
1582 	CHECK(check_sljit_emit_fast_return(compiler, src, srcw));
1583 	ADJUST_LOCAL_OFFSET(src, srcw);
1584 
1585 	if (FAST_IS_REG(src))
1586 		FAIL_IF(push_inst(compiler, ADDU_W | S(src) | TA(0) | DA(RETURN_ADDR_REG), RETURN_ADDR_REG));
1587 	else if (src & SLJIT_MEM)
1588 		FAIL_IF(emit_op_mem(compiler, WORD_DATA | LOAD_DATA, RETURN_ADDR_REG, src, srcw));
1589 	else if (src & SLJIT_IMM)
1590 		FAIL_IF(load_immediate(compiler, RETURN_ADDR_REG, srcw));
1591 
1592 	FAIL_IF(push_inst(compiler, JR | SA(RETURN_ADDR_REG), UNMOVABLE_INS));
1593 	return push_inst(compiler, NOP, UNMOVABLE_INS);
1594 }
1595 
1596 /* --------------------------------------------------------------------- */
1597 /*  Conditional instructions                                             */
1598 /* --------------------------------------------------------------------- */
1599 
sljit_emit_label(struct sljit_compiler * compiler)1600 SLJIT_API_FUNC_ATTRIBUTE struct sljit_label* sljit_emit_label(struct sljit_compiler *compiler)
1601 {
1602 	struct sljit_label *label;
1603 
1604 	CHECK_ERROR_PTR();
1605 	CHECK_PTR(check_sljit_emit_label(compiler));
1606 
1607 	if (compiler->last_label && compiler->last_label->size == compiler->size)
1608 		return compiler->last_label;
1609 
1610 	label = (struct sljit_label*)ensure_abuf(compiler, sizeof(struct sljit_label));
1611 	PTR_FAIL_IF(!label);
1612 	set_label(label, compiler);
1613 	compiler->delay_slot = UNMOVABLE_INS;
1614 	return label;
1615 }
1616 
1617 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
1618 #define JUMP_LENGTH	4
1619 #else
1620 #define JUMP_LENGTH	8
1621 #endif
1622 
1623 #define BR_Z(src) \
1624 	inst = BEQ | SA(src) | TA(0) | JUMP_LENGTH; \
1625 	flags = IS_BIT26_COND; \
1626 	delay_check = src;
1627 
1628 #define BR_NZ(src) \
1629 	inst = BNE | SA(src) | TA(0) | JUMP_LENGTH; \
1630 	flags = IS_BIT26_COND; \
1631 	delay_check = src;
1632 
1633 #define BR_T() \
1634 	inst = BC1T | JUMP_LENGTH; \
1635 	flags = IS_BIT16_COND; \
1636 	delay_check = FCSR_FCC;
1637 
1638 #define BR_F() \
1639 	inst = BC1F | JUMP_LENGTH; \
1640 	flags = IS_BIT16_COND; \
1641 	delay_check = FCSR_FCC;
1642 
sljit_emit_jump(struct sljit_compiler * compiler,sljit_s32 type)1643 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_jump(struct sljit_compiler *compiler, sljit_s32 type)
1644 {
1645 	struct sljit_jump *jump;
1646 	sljit_ins inst;
1647 	sljit_s32 flags = 0;
1648 	sljit_s32 delay_check = UNMOVABLE_INS;
1649 
1650 	CHECK_ERROR_PTR();
1651 	CHECK_PTR(check_sljit_emit_jump(compiler, type));
1652 
1653 	jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1654 	PTR_FAIL_IF(!jump);
1655 	set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
1656 	type &= 0xff;
1657 
1658 	switch (type) {
1659 	case SLJIT_EQUAL:
1660 		BR_NZ(EQUAL_FLAG);
1661 		break;
1662 	case SLJIT_NOT_EQUAL:
1663 		BR_Z(EQUAL_FLAG);
1664 		break;
1665 	case SLJIT_LESS:
1666 	case SLJIT_GREATER:
1667 	case SLJIT_SIG_LESS:
1668 	case SLJIT_SIG_GREATER:
1669 	case SLJIT_OVERFLOW:
1670 	case SLJIT_MUL_OVERFLOW:
1671 		BR_Z(OTHER_FLAG);
1672 		break;
1673 	case SLJIT_GREATER_EQUAL:
1674 	case SLJIT_LESS_EQUAL:
1675 	case SLJIT_SIG_GREATER_EQUAL:
1676 	case SLJIT_SIG_LESS_EQUAL:
1677 	case SLJIT_NOT_OVERFLOW:
1678 	case SLJIT_MUL_NOT_OVERFLOW:
1679 		BR_NZ(OTHER_FLAG);
1680 		break;
1681 	case SLJIT_NOT_EQUAL_F64:
1682 	case SLJIT_GREATER_EQUAL_F64:
1683 	case SLJIT_GREATER_F64:
1684 	case SLJIT_ORDERED_F64:
1685 		BR_T();
1686 		break;
1687 	case SLJIT_EQUAL_F64:
1688 	case SLJIT_LESS_F64:
1689 	case SLJIT_LESS_EQUAL_F64:
1690 	case SLJIT_UNORDERED_F64:
1691 		BR_F();
1692 		break;
1693 	default:
1694 		/* Not conditional branch. */
1695 		inst = 0;
1696 		break;
1697 	}
1698 
1699 	jump->flags |= flags;
1700 	if (compiler->delay_slot == MOVABLE_INS || (compiler->delay_slot != UNMOVABLE_INS && compiler->delay_slot != delay_check))
1701 		jump->flags |= IS_MOVABLE;
1702 
1703 	if (inst)
1704 		PTR_FAIL_IF(push_inst(compiler, inst, UNMOVABLE_INS));
1705 
1706 	PTR_FAIL_IF(emit_const(compiler, TMP_REG2, 0));
1707 	if (type <= SLJIT_JUMP) {
1708 		PTR_FAIL_IF(push_inst(compiler, JR | S(TMP_REG2), UNMOVABLE_INS));
1709 		jump->addr = compiler->size;
1710 		PTR_FAIL_IF(push_inst(compiler, NOP, UNMOVABLE_INS));
1711 	} else {
1712 		SLJIT_ASSERT(DR(PIC_ADDR_REG) == 25 && PIC_ADDR_REG == TMP_REG2);
1713 		/* Cannot be optimized out if type is >= CALL0. */
1714 		jump->flags |= IS_JAL | (type >= SLJIT_CALL0 ? IS_CALL : 0);
1715 		PTR_FAIL_IF(push_inst(compiler, JALR | S(TMP_REG2) | DA(RETURN_ADDR_REG), UNMOVABLE_INS));
1716 		jump->addr = compiler->size;
1717 		/* A NOP if type < CALL1. */
1718 		PTR_FAIL_IF(push_inst(compiler, ADDU_W | S(SLJIT_R0) | TA(0) | DA(4), UNMOVABLE_INS));
1719 	}
1720 	return jump;
1721 }
1722 
1723 #define RESOLVE_IMM1() \
1724 	if (src1 & SLJIT_IMM) { \
1725 		if (src1w) { \
1726 			PTR_FAIL_IF(load_immediate(compiler, DR(TMP_REG1), src1w)); \
1727 			src1 = TMP_REG1; \
1728 		} \
1729 		else \
1730 			src1 = 0; \
1731 	}
1732 
1733 #define RESOLVE_IMM2() \
1734 	if (src2 & SLJIT_IMM) { \
1735 		if (src2w) { \
1736 			PTR_FAIL_IF(load_immediate(compiler, DR(TMP_REG2), src2w)); \
1737 			src2 = TMP_REG2; \
1738 		} \
1739 		else \
1740 			src2 = 0; \
1741 	}
1742 
sljit_emit_cmp(struct sljit_compiler * compiler,sljit_s32 type,sljit_s32 src1,sljit_sw src1w,sljit_s32 src2,sljit_sw src2w)1743 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_cmp(struct sljit_compiler *compiler, sljit_s32 type,
1744 	sljit_s32 src1, sljit_sw src1w,
1745 	sljit_s32 src2, sljit_sw src2w)
1746 {
1747 	struct sljit_jump *jump;
1748 	sljit_s32 flags;
1749 	sljit_ins inst;
1750 
1751 	CHECK_ERROR_PTR();
1752 	CHECK_PTR(check_sljit_emit_cmp(compiler, type, src1, src1w, src2, src2w));
1753 	ADJUST_LOCAL_OFFSET(src1, src1w);
1754 	ADJUST_LOCAL_OFFSET(src2, src2w);
1755 
1756 	compiler->cache_arg = 0;
1757 	compiler->cache_argw = 0;
1758 	flags = ((type & SLJIT_I32_OP) ? INT_DATA : WORD_DATA) | LOAD_DATA;
1759 	if (src1 & SLJIT_MEM) {
1760 		PTR_FAIL_IF(emit_op_mem2(compiler, flags, DR(TMP_REG1), src1, src1w, src2, src2w));
1761 		src1 = TMP_REG1;
1762 	}
1763 	if (src2 & SLJIT_MEM) {
1764 		PTR_FAIL_IF(emit_op_mem2(compiler, flags, DR(TMP_REG2), src2, src2w, 0, 0));
1765 		src2 = TMP_REG2;
1766 	}
1767 
1768 	jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1769 	PTR_FAIL_IF(!jump);
1770 	set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
1771 	type &= 0xff;
1772 
1773 	if (type <= SLJIT_NOT_EQUAL) {
1774 		RESOLVE_IMM1();
1775 		RESOLVE_IMM2();
1776 		jump->flags |= IS_BIT26_COND;
1777 		if (compiler->delay_slot == MOVABLE_INS || (compiler->delay_slot != UNMOVABLE_INS && compiler->delay_slot != DR(src1) && compiler->delay_slot != DR(src2)))
1778 			jump->flags |= IS_MOVABLE;
1779 		PTR_FAIL_IF(push_inst(compiler, (type == SLJIT_EQUAL ? BNE : BEQ) | S(src1) | T(src2) | JUMP_LENGTH, UNMOVABLE_INS));
1780 	}
1781 	else if (type >= SLJIT_SIG_LESS && (((src1 & SLJIT_IMM) && (src1w == 0)) || ((src2 & SLJIT_IMM) && (src2w == 0)))) {
1782 		inst = NOP;
1783 		if ((src1 & SLJIT_IMM) && (src1w == 0)) {
1784 			RESOLVE_IMM2();
1785 			switch (type) {
1786 			case SLJIT_SIG_LESS:
1787 				inst = BLEZ;
1788 				jump->flags |= IS_BIT26_COND;
1789 				break;
1790 			case SLJIT_SIG_GREATER_EQUAL:
1791 				inst = BGTZ;
1792 				jump->flags |= IS_BIT26_COND;
1793 				break;
1794 			case SLJIT_SIG_GREATER:
1795 				inst = BGEZ;
1796 				jump->flags |= IS_BIT16_COND;
1797 				break;
1798 			case SLJIT_SIG_LESS_EQUAL:
1799 				inst = BLTZ;
1800 				jump->flags |= IS_BIT16_COND;
1801 				break;
1802 			}
1803 			src1 = src2;
1804 		}
1805 		else {
1806 			RESOLVE_IMM1();
1807 			switch (type) {
1808 			case SLJIT_SIG_LESS:
1809 				inst = BGEZ;
1810 				jump->flags |= IS_BIT16_COND;
1811 				break;
1812 			case SLJIT_SIG_GREATER_EQUAL:
1813 				inst = BLTZ;
1814 				jump->flags |= IS_BIT16_COND;
1815 				break;
1816 			case SLJIT_SIG_GREATER:
1817 				inst = BLEZ;
1818 				jump->flags |= IS_BIT26_COND;
1819 				break;
1820 			case SLJIT_SIG_LESS_EQUAL:
1821 				inst = BGTZ;
1822 				jump->flags |= IS_BIT26_COND;
1823 				break;
1824 			}
1825 		}
1826 		PTR_FAIL_IF(push_inst(compiler, inst | S(src1) | JUMP_LENGTH, UNMOVABLE_INS));
1827 	}
1828 	else {
1829 		if (type == SLJIT_LESS || type == SLJIT_GREATER_EQUAL || type == SLJIT_SIG_LESS || type == SLJIT_SIG_GREATER_EQUAL) {
1830 			RESOLVE_IMM1();
1831 			if ((src2 & SLJIT_IMM) && src2w <= SIMM_MAX && src2w >= SIMM_MIN)
1832 				PTR_FAIL_IF(push_inst(compiler, (type <= SLJIT_LESS_EQUAL ? SLTIU : SLTI) | S(src1) | T(TMP_REG1) | IMM(src2w), DR(TMP_REG1)));
1833 			else {
1834 				RESOLVE_IMM2();
1835 				PTR_FAIL_IF(push_inst(compiler, (type <= SLJIT_LESS_EQUAL ? SLTU : SLT) | S(src1) | T(src2) | D(TMP_REG1), DR(TMP_REG1)));
1836 			}
1837 			type = (type == SLJIT_LESS || type == SLJIT_SIG_LESS) ? SLJIT_NOT_EQUAL : SLJIT_EQUAL;
1838 		}
1839 		else {
1840 			RESOLVE_IMM2();
1841 			if ((src1 & SLJIT_IMM) && src1w <= SIMM_MAX && src1w >= SIMM_MIN)
1842 				PTR_FAIL_IF(push_inst(compiler, (type <= SLJIT_LESS_EQUAL ? SLTIU : SLTI) | S(src2) | T(TMP_REG1) | IMM(src1w), DR(TMP_REG1)));
1843 			else {
1844 				RESOLVE_IMM1();
1845 				PTR_FAIL_IF(push_inst(compiler, (type <= SLJIT_LESS_EQUAL ? SLTU : SLT) | S(src2) | T(src1) | D(TMP_REG1), DR(TMP_REG1)));
1846 			}
1847 			type = (type == SLJIT_GREATER || type == SLJIT_SIG_GREATER) ? SLJIT_NOT_EQUAL : SLJIT_EQUAL;
1848 		}
1849 
1850 		jump->flags |= IS_BIT26_COND;
1851 		PTR_FAIL_IF(push_inst(compiler, (type == SLJIT_EQUAL ? BNE : BEQ) | S(TMP_REG1) | TA(0) | JUMP_LENGTH, UNMOVABLE_INS));
1852 	}
1853 
1854 	PTR_FAIL_IF(emit_const(compiler, TMP_REG2, 0));
1855 	PTR_FAIL_IF(push_inst(compiler, JR | S(TMP_REG2), UNMOVABLE_INS));
1856 	jump->addr = compiler->size;
1857 	PTR_FAIL_IF(push_inst(compiler, NOP, UNMOVABLE_INS));
1858 	return jump;
1859 }
1860 
1861 #undef RESOLVE_IMM1
1862 #undef RESOLVE_IMM2
1863 
1864 #undef JUMP_LENGTH
1865 #undef BR_Z
1866 #undef BR_NZ
1867 #undef BR_T
1868 #undef BR_F
1869 
1870 #undef FLOAT_DATA
1871 #undef FMT
1872 
sljit_emit_ijump(struct sljit_compiler * compiler,sljit_s32 type,sljit_s32 src,sljit_sw srcw)1873 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_ijump(struct sljit_compiler *compiler, sljit_s32 type, sljit_s32 src, sljit_sw srcw)
1874 {
1875 	sljit_s32 src_r = TMP_REG2;
1876 	struct sljit_jump *jump = NULL;
1877 
1878 	CHECK_ERROR();
1879 	CHECK(check_sljit_emit_ijump(compiler, type, src, srcw));
1880 	ADJUST_LOCAL_OFFSET(src, srcw);
1881 
1882 	if (FAST_IS_REG(src)) {
1883 		if (DR(src) != 4)
1884 			src_r = src;
1885 		else
1886 			FAIL_IF(push_inst(compiler, ADDU_W | S(src) | TA(0) | D(TMP_REG2), DR(TMP_REG2)));
1887 	}
1888 
1889 	if (type >= SLJIT_CALL0) {
1890 		SLJIT_ASSERT(DR(PIC_ADDR_REG) == 25 && PIC_ADDR_REG == TMP_REG2);
1891 		if (src & (SLJIT_IMM | SLJIT_MEM)) {
1892 			if (src & SLJIT_IMM)
1893 				FAIL_IF(load_immediate(compiler, DR(PIC_ADDR_REG), srcw));
1894 			else {
1895 				SLJIT_ASSERT(src_r == TMP_REG2 && (src & SLJIT_MEM));
1896 				FAIL_IF(emit_op(compiler, SLJIT_MOV, WORD_DATA, TMP_REG2, 0, TMP_REG1, 0, src, srcw));
1897 			}
1898 			FAIL_IF(push_inst(compiler, JALR | S(PIC_ADDR_REG) | DA(RETURN_ADDR_REG), UNMOVABLE_INS));
1899 			/* We need an extra instruction in any case. */
1900 			return push_inst(compiler, ADDU_W | S(SLJIT_R0) | TA(0) | DA(4), UNMOVABLE_INS);
1901 		}
1902 
1903 		/* Register input. */
1904 		if (type >= SLJIT_CALL1)
1905 			FAIL_IF(push_inst(compiler, ADDU_W | S(SLJIT_R0) | TA(0) | DA(4), 4));
1906 		FAIL_IF(push_inst(compiler, JALR | S(src_r) | DA(RETURN_ADDR_REG), UNMOVABLE_INS));
1907 		return push_inst(compiler, ADDU_W | S(src_r) | TA(0) | D(PIC_ADDR_REG), UNMOVABLE_INS);
1908 	}
1909 
1910 	if (src & SLJIT_IMM) {
1911 		jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1912 		FAIL_IF(!jump);
1913 		set_jump(jump, compiler, JUMP_ADDR | ((type >= SLJIT_FAST_CALL) ? IS_JAL : 0));
1914 		jump->u.target = srcw;
1915 
1916 		if (compiler->delay_slot != UNMOVABLE_INS)
1917 			jump->flags |= IS_MOVABLE;
1918 
1919 		FAIL_IF(emit_const(compiler, TMP_REG2, 0));
1920 	}
1921 	else if (src & SLJIT_MEM)
1922 		FAIL_IF(emit_op(compiler, SLJIT_MOV, WORD_DATA, TMP_REG2, 0, TMP_REG1, 0, src, srcw));
1923 
1924 	FAIL_IF(push_inst(compiler, JR | S(src_r), UNMOVABLE_INS));
1925 	if (jump)
1926 		jump->addr = compiler->size;
1927 	FAIL_IF(push_inst(compiler, NOP, UNMOVABLE_INS));
1928 	return SLJIT_SUCCESS;
1929 }
1930 
sljit_emit_op_flags(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 type)1931 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_flags(struct sljit_compiler *compiler, sljit_s32 op,
1932 	sljit_s32 dst, sljit_sw dstw,
1933 	sljit_s32 type)
1934 {
1935 	sljit_s32 src_ar, dst_ar;
1936 	sljit_s32 saved_op = op;
1937 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
1938 	sljit_s32 mem_type = WORD_DATA;
1939 #else
1940 	sljit_s32 mem_type = (op & SLJIT_I32_OP) ? (INT_DATA | SIGNED_DATA) : WORD_DATA;
1941 #endif
1942 
1943 	CHECK_ERROR();
1944 	CHECK(check_sljit_emit_op_flags(compiler, op, dst, dstw, type));
1945 	ADJUST_LOCAL_OFFSET(dst, dstw);
1946 
1947 	op = GET_OPCODE(op);
1948 #if (defined SLJIT_CONFIG_MIPS_64 && SLJIT_CONFIG_MIPS_64)
1949 	if (op == SLJIT_MOV_S32)
1950 		mem_type = INT_DATA | SIGNED_DATA;
1951 #endif
1952 	dst_ar = DR((op < SLJIT_ADD && FAST_IS_REG(dst)) ? dst : TMP_REG2);
1953 
1954 	compiler->cache_arg = 0;
1955 	compiler->cache_argw = 0;
1956 
1957 	if (op >= SLJIT_ADD && (dst & SLJIT_MEM))
1958 		FAIL_IF(emit_op_mem2(compiler, mem_type | LOAD_DATA, DR(TMP_REG1), dst, dstw, dst, dstw));
1959 
1960 	switch (type & 0xff) {
1961 	case SLJIT_EQUAL:
1962 	case SLJIT_NOT_EQUAL:
1963 		FAIL_IF(push_inst(compiler, SLTIU | SA(EQUAL_FLAG) | TA(dst_ar) | IMM(1), dst_ar));
1964 		src_ar = dst_ar;
1965 		break;
1966 	case SLJIT_MUL_OVERFLOW:
1967 	case SLJIT_MUL_NOT_OVERFLOW:
1968 		FAIL_IF(push_inst(compiler, SLTIU | SA(OTHER_FLAG) | TA(dst_ar) | IMM(1), dst_ar));
1969 		src_ar = dst_ar;
1970 		type ^= 0x1; /* Flip type bit for the XORI below. */
1971 		break;
1972 	case SLJIT_GREATER_F64:
1973 	case SLJIT_LESS_EQUAL_F64:
1974 		type ^= 0x1; /* Flip type bit for the XORI below. */
1975 	case SLJIT_EQUAL_F64:
1976 	case SLJIT_NOT_EQUAL_F64:
1977 	case SLJIT_LESS_F64:
1978 	case SLJIT_GREATER_EQUAL_F64:
1979 	case SLJIT_UNORDERED_F64:
1980 	case SLJIT_ORDERED_F64:
1981 		FAIL_IF(push_inst(compiler, CFC1 | TA(dst_ar) | DA(FCSR_REG), dst_ar));
1982 		FAIL_IF(push_inst(compiler, SRL | TA(dst_ar) | DA(dst_ar) | SH_IMM(23), dst_ar));
1983 		FAIL_IF(push_inst(compiler, ANDI | SA(dst_ar) | TA(dst_ar) | IMM(1), dst_ar));
1984 		src_ar = dst_ar;
1985 		break;
1986 
1987 	default:
1988 		src_ar = OTHER_FLAG;
1989 		break;
1990 	}
1991 
1992 	if (type & 0x1) {
1993 		FAIL_IF(push_inst(compiler, XORI | SA(src_ar) | TA(dst_ar) | IMM(1), dst_ar));
1994 		src_ar = dst_ar;
1995 	}
1996 
1997 	if (op < SLJIT_ADD) {
1998 		if (dst & SLJIT_MEM)
1999 			return emit_op_mem(compiler, mem_type, src_ar, dst, dstw);
2000 
2001 		if (src_ar != dst_ar)
2002 			return push_inst(compiler, ADDU_W | SA(src_ar) | TA(0) | DA(dst_ar), dst_ar);
2003 		return SLJIT_SUCCESS;
2004 	}
2005 
2006 	/* OTHER_FLAG cannot be specified as src2 argument at the moment. */
2007 	if (DR(TMP_REG2) != src_ar)
2008 		FAIL_IF(push_inst(compiler, ADDU_W | SA(src_ar) | TA(0) | D(TMP_REG2), DR(TMP_REG2)));
2009 
2010 	mem_type |= CUMULATIVE_OP | LOGICAL_OP | IMM_OP | ALT_KEEP_CACHE;
2011 
2012 	if (dst & SLJIT_MEM)
2013 		return emit_op(compiler, saved_op, mem_type, dst, dstw, TMP_REG1, 0, TMP_REG2, 0);
2014 	return emit_op(compiler, saved_op, mem_type, dst, dstw, dst, dstw, TMP_REG2, 0);
2015 }
2016 
sljit_emit_cmov(struct sljit_compiler * compiler,sljit_s32 type,sljit_s32 dst_reg,sljit_s32 src,sljit_sw srcw)2017 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_cmov(struct sljit_compiler *compiler, sljit_s32 type,
2018 	sljit_s32 dst_reg,
2019 	sljit_s32 src, sljit_sw srcw)
2020 {
2021 #if (defined SLJIT_MIPS_R1 && SLJIT_MIPS_R1)
2022 	sljit_ins ins;
2023 #endif
2024 
2025 	CHECK_ERROR();
2026 	CHECK(check_sljit_emit_cmov(compiler, type, dst_reg, src, srcw));
2027 
2028 #if (defined SLJIT_MIPS_R1 && SLJIT_MIPS_R1)
2029 
2030 	if (SLJIT_UNLIKELY(src & SLJIT_IMM)) {
2031 #if (defined SLJIT_CONFIG_MIPS_64 && SLJIT_CONFIG_MIPS_64)
2032 		if (dst_reg & SLJIT_I32_OP)
2033 			srcw = (sljit_s32)srcw;
2034 #endif
2035 		FAIL_IF(load_immediate(compiler, DR(TMP_REG1), srcw));
2036 		src = TMP_REG1;
2037 		srcw = 0;
2038 	}
2039 
2040 	dst_reg &= ~SLJIT_I32_OP;
2041 
2042 	switch (type & 0xff) {
2043 	case SLJIT_EQUAL:
2044 		ins = MOVZ | TA(EQUAL_FLAG);
2045 		break;
2046 	case SLJIT_NOT_EQUAL:
2047 		ins = MOVN | TA(EQUAL_FLAG);
2048 		break;
2049 	case SLJIT_LESS:
2050 	case SLJIT_GREATER:
2051 	case SLJIT_SIG_LESS:
2052 	case SLJIT_SIG_GREATER:
2053 	case SLJIT_OVERFLOW:
2054 	case SLJIT_MUL_OVERFLOW:
2055 		ins = MOVN | TA(OTHER_FLAG);
2056 		break;
2057 	case SLJIT_GREATER_EQUAL:
2058 	case SLJIT_LESS_EQUAL:
2059 	case SLJIT_SIG_GREATER_EQUAL:
2060 	case SLJIT_SIG_LESS_EQUAL:
2061 	case SLJIT_NOT_OVERFLOW:
2062 	case SLJIT_MUL_NOT_OVERFLOW:
2063 		ins = MOVZ | TA(OTHER_FLAG);
2064 		break;
2065 	case SLJIT_EQUAL_F64:
2066 	case SLJIT_LESS_F64:
2067 	case SLJIT_LESS_EQUAL_F64:
2068 	case SLJIT_UNORDERED_F64:
2069 		ins = MOVT;
2070 		break;
2071 	case SLJIT_NOT_EQUAL_F64:
2072 	case SLJIT_GREATER_EQUAL_F64:
2073 	case SLJIT_GREATER_F64:
2074 	case SLJIT_ORDERED_F64:
2075 		ins = MOVF;
2076 		break;
2077 	default:
2078 		ins = MOVZ | TA(OTHER_FLAG);
2079 		SLJIT_UNREACHABLE();
2080 		break;
2081 	}
2082 
2083 	return push_inst(compiler, ins | S(src) | D(dst_reg), DR(dst_reg));
2084 
2085 #else
2086 	return sljit_emit_cmov_generic(compiler, type, dst_reg, src, srcw);
2087 #endif
2088 }
2089 
sljit_emit_const(struct sljit_compiler * compiler,sljit_s32 dst,sljit_sw dstw,sljit_sw init_value)2090 SLJIT_API_FUNC_ATTRIBUTE struct sljit_const* sljit_emit_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw, sljit_sw init_value)
2091 {
2092 	struct sljit_const *const_;
2093 	sljit_s32 reg;
2094 
2095 	CHECK_ERROR_PTR();
2096 	CHECK_PTR(check_sljit_emit_const(compiler, dst, dstw, init_value));
2097 	ADJUST_LOCAL_OFFSET(dst, dstw);
2098 
2099 	const_ = (struct sljit_const*)ensure_abuf(compiler, sizeof(struct sljit_const));
2100 	PTR_FAIL_IF(!const_);
2101 	set_const(const_, compiler);
2102 
2103 	reg = FAST_IS_REG(dst) ? dst : TMP_REG2;
2104 
2105 	PTR_FAIL_IF(emit_const(compiler, reg, init_value));
2106 
2107 	if (dst & SLJIT_MEM)
2108 		PTR_FAIL_IF(emit_op(compiler, SLJIT_MOV, WORD_DATA, dst, dstw, TMP_REG1, 0, TMP_REG2, 0));
2109 	return const_;
2110 }
2111