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