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