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