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