• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_ins *ptr = (sljit_ins*)ensure_buf(compiler, sizeof(sljit_ins));
294 	SLJIT_ASSERT(delay_slot == MOVABLE_INS || delay_slot >= UNMOVABLE_INS
295 		|| delay_slot == ((ins >> 11) & 0x1f) || delay_slot == ((ins >> 16) & 0x1f));
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), compiler->exec_allocator_data);
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 	SLJIT_UPDATE_WX_FLAGS(code, code_ptr, 1);
671 	return code;
672 }
673 
sljit_has_cpu_feature(sljit_s32 feature_type)674 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_has_cpu_feature(sljit_s32 feature_type)
675 {
676 	sljit_sw fir = 0;
677 
678 	switch (feature_type) {
679 	case SLJIT_HAS_FPU:
680 #ifdef SLJIT_IS_FPU_AVAILABLE
681 		return SLJIT_IS_FPU_AVAILABLE;
682 #elif defined(__GNUC__)
683 		__asm__ ("cfc1 %0, $0" : "=r"(fir));
684 		return (fir >> 22) & 0x1;
685 #else
686 #error "FIR check is not implemented for this architecture"
687 #endif
688 	case SLJIT_HAS_ZERO_REGISTER:
689 		return 1;
690 
691 #if (defined SLJIT_MIPS_REV && SLJIT_MIPS_REV >= 1)
692 	case SLJIT_HAS_CLZ:
693 	case SLJIT_HAS_CMOV:
694 	case SLJIT_HAS_PREFETCH:
695 		return 1;
696 #endif /* SLJIT_MIPS_REV >= 1 */
697 
698 	default:
699 		return fir;
700 	}
701 }
702 
703 /* --------------------------------------------------------------------- */
704 /*  Entry, exit                                                          */
705 /* --------------------------------------------------------------------- */
706 
707 /* Creates an index in data_transfer_insts array. */
708 #define LOAD_DATA	0x01
709 #define WORD_DATA	0x00
710 #define BYTE_DATA	0x02
711 #define HALF_DATA	0x04
712 #define INT_DATA	0x06
713 #define SIGNED_DATA	0x08
714 /* Separates integer and floating point registers */
715 #define GPR_REG		0x0f
716 #define DOUBLE_DATA	0x10
717 #define SINGLE_DATA	0x12
718 
719 #define MEM_MASK	0x1f
720 
721 #define ARG_TEST	0x00020
722 #define ALT_KEEP_CACHE	0x00040
723 #define CUMULATIVE_OP	0x00080
724 #define LOGICAL_OP	0x00100
725 #define IMM_OP		0x00200
726 #define SRC2_IMM	0x00400
727 
728 #define UNUSED_DEST	0x00800
729 #define REG_DEST	0x01000
730 #define REG1_SOURCE	0x02000
731 #define REG2_SOURCE	0x04000
732 #define SLOW_SRC1	0x08000
733 #define SLOW_SRC2	0x10000
734 #define SLOW_DEST	0x20000
735 
736 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
737 #define STACK_STORE	SW
738 #define STACK_LOAD	LW
739 #else
740 #define STACK_STORE	SD
741 #define STACK_LOAD	LD
742 #endif
743 
744 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);
745 
746 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
747 #include "sljitNativeMIPS_32.c"
748 #else
749 #include "sljitNativeMIPS_64.c"
750 #endif
751 
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)752 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_enter(struct sljit_compiler *compiler,
753 	sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
754 	sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
755 {
756 	sljit_ins base;
757 	sljit_s32 args, i, tmp, offs;
758 
759 	CHECK_ERROR();
760 	CHECK(check_sljit_emit_enter(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size));
761 	set_emit_enter(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size);
762 
763 	local_size += GET_SAVED_REGISTERS_SIZE(scratches, saveds, 1) + SLJIT_LOCALS_OFFSET;
764 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
765 	local_size = (local_size + 15) & ~0xf;
766 #else
767 	local_size = (local_size + 31) & ~0x1f;
768 #endif
769 	compiler->local_size = local_size;
770 
771 	if (local_size <= SIMM_MAX) {
772 		/* Frequent case. */
773 		FAIL_IF(push_inst(compiler, ADDIU_W | S(SLJIT_SP) | T(SLJIT_SP) | IMM(-local_size), DR(SLJIT_SP)));
774 		base = S(SLJIT_SP);
775 		offs = local_size - (sljit_sw)sizeof(sljit_sw);
776 	}
777 	else {
778 		FAIL_IF(load_immediate(compiler, DR(OTHER_FLAG), local_size));
779 		FAIL_IF(push_inst(compiler, ADDU_W | S(SLJIT_SP) | TA(0) | D(TMP_REG2), DR(TMP_REG2)));
780 		FAIL_IF(push_inst(compiler, SUBU_W | S(SLJIT_SP) | T(OTHER_FLAG) | D(SLJIT_SP), DR(SLJIT_SP)));
781 		base = S(TMP_REG2);
782 		local_size = 0;
783 		offs = -(sljit_sw)sizeof(sljit_sw);
784 	}
785 
786 	FAIL_IF(push_inst(compiler, STACK_STORE | base | TA(RETURN_ADDR_REG) | IMM(offs), MOVABLE_INS));
787 
788 	tmp = saveds < SLJIT_NUMBER_OF_SAVED_REGISTERS ? (SLJIT_S0 + 1 - saveds) : SLJIT_FIRST_SAVED_REG;
789 	for (i = SLJIT_S0; i >= tmp; i--) {
790 		offs -= (sljit_s32)(sizeof(sljit_sw));
791 		FAIL_IF(push_inst(compiler, STACK_STORE | base | T(i) | IMM(offs), MOVABLE_INS));
792 	}
793 
794 	for (i = scratches; i >= SLJIT_FIRST_SAVED_REG; i--) {
795 		offs -= (sljit_s32)(sizeof(sljit_sw));
796 		FAIL_IF(push_inst(compiler, STACK_STORE | base | T(i) | IMM(offs), MOVABLE_INS));
797 	}
798 
799 	args = get_arg_count(arg_types);
800 
801 	if (args >= 1)
802 		FAIL_IF(push_inst(compiler, ADDU_W | SA(4) | TA(0) | D(SLJIT_S0), DR(SLJIT_S0)));
803 	if (args >= 2)
804 		FAIL_IF(push_inst(compiler, ADDU_W | SA(5) | TA(0) | D(SLJIT_S1), DR(SLJIT_S1)));
805 	if (args >= 3)
806 		FAIL_IF(push_inst(compiler, ADDU_W | SA(6) | TA(0) | D(SLJIT_S2), DR(SLJIT_S2)));
807 
808 	return SLJIT_SUCCESS;
809 }
810 
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)811 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_set_context(struct sljit_compiler *compiler,
812 	sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
813 	sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
814 {
815 	CHECK_ERROR();
816 	CHECK(check_sljit_set_context(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size));
817 	set_set_context(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size);
818 
819 	local_size += GET_SAVED_REGISTERS_SIZE(scratches, saveds, 1) + SLJIT_LOCALS_OFFSET;
820 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
821 	compiler->local_size = (local_size + 15) & ~0xf;
822 #else
823 	compiler->local_size = (local_size + 31) & ~0x1f;
824 #endif
825 	return SLJIT_SUCCESS;
826 }
827 
sljit_emit_return(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 src,sljit_sw srcw)828 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_return(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 src, sljit_sw srcw)
829 {
830 	sljit_s32 local_size, i, tmp, offs;
831 	sljit_ins base;
832 
833 	CHECK_ERROR();
834 	CHECK(check_sljit_emit_return(compiler, op, src, srcw));
835 
836 	FAIL_IF(emit_mov_before_return(compiler, op, src, srcw));
837 
838 	local_size = compiler->local_size;
839 	if (local_size <= SIMM_MAX)
840 		base = S(SLJIT_SP);
841 	else {
842 		FAIL_IF(load_immediate(compiler, DR(TMP_REG1), local_size));
843 		FAIL_IF(push_inst(compiler, ADDU_W | S(SLJIT_SP) | T(TMP_REG1) | D(TMP_REG1), DR(TMP_REG1)));
844 		base = S(TMP_REG1);
845 		local_size = 0;
846 	}
847 
848 	FAIL_IF(push_inst(compiler, STACK_LOAD | base | TA(RETURN_ADDR_REG) | IMM(local_size - (sljit_s32)sizeof(sljit_sw)), RETURN_ADDR_REG));
849 	offs = local_size - (sljit_s32)GET_SAVED_REGISTERS_SIZE(compiler->scratches, compiler->saveds, 1);
850 
851 	tmp = compiler->scratches;
852 	for (i = SLJIT_FIRST_SAVED_REG; i <= tmp; i++) {
853 		FAIL_IF(push_inst(compiler, STACK_LOAD | base | T(i) | IMM(offs), DR(i)));
854 		offs += (sljit_s32)(sizeof(sljit_sw));
855 	}
856 
857 	tmp = compiler->saveds < SLJIT_NUMBER_OF_SAVED_REGISTERS ? (SLJIT_S0 + 1 - compiler->saveds) : SLJIT_FIRST_SAVED_REG;
858 	for (i = tmp; i <= SLJIT_S0; i++) {
859 		FAIL_IF(push_inst(compiler, STACK_LOAD | base | T(i) | IMM(offs), DR(i)));
860 		offs += (sljit_s32)(sizeof(sljit_sw));
861 	}
862 
863 	SLJIT_ASSERT(offs == local_size - (sljit_sw)(sizeof(sljit_sw)));
864 
865 	FAIL_IF(push_inst(compiler, JR | SA(RETURN_ADDR_REG), UNMOVABLE_INS));
866 	if (compiler->local_size <= SIMM_MAX)
867 		return push_inst(compiler, ADDIU_W | S(SLJIT_SP) | T(SLJIT_SP) | IMM(compiler->local_size), UNMOVABLE_INS);
868 	else
869 		return push_inst(compiler, ADDU_W | S(TMP_REG1) | TA(0) | D(SLJIT_SP), UNMOVABLE_INS);
870 }
871 
872 #undef STACK_STORE
873 #undef STACK_LOAD
874 
875 /* --------------------------------------------------------------------- */
876 /*  Operators                                                            */
877 /* --------------------------------------------------------------------- */
878 
879 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
880 #define ARCH_32_64(a, b)	a
881 #else
882 #define ARCH_32_64(a, b)	b
883 #endif
884 
885 static const sljit_ins data_transfer_insts[16 + 4] = {
886 /* u w s */ ARCH_32_64(HI(43) /* sw */, HI(63) /* sd */),
887 /* u w l */ ARCH_32_64(HI(35) /* lw */, HI(55) /* ld */),
888 /* u b s */ HI(40) /* sb */,
889 /* u b l */ HI(36) /* lbu */,
890 /* u h s */ HI(41) /* sh */,
891 /* u h l */ HI(37) /* lhu */,
892 /* u i s */ HI(43) /* sw */,
893 /* u i l */ ARCH_32_64(HI(35) /* lw */, HI(39) /* lwu */),
894 
895 /* s w s */ ARCH_32_64(HI(43) /* sw */, HI(63) /* sd */),
896 /* s w l */ ARCH_32_64(HI(35) /* lw */, HI(55) /* ld */),
897 /* s b s */ HI(40) /* sb */,
898 /* s b l */ HI(32) /* lb */,
899 /* s h s */ HI(41) /* sh */,
900 /* s h l */ HI(33) /* lh */,
901 /* s i s */ HI(43) /* sw */,
902 /* s i l */ HI(35) /* lw */,
903 
904 /* d   s */ HI(61) /* sdc1 */,
905 /* d   l */ HI(53) /* ldc1 */,
906 /* s   s */ HI(57) /* swc1 */,
907 /* s   l */ HI(49) /* lwc1 */,
908 };
909 
910 #undef ARCH_32_64
911 
912 /* reg_ar is an absoulute register! */
913 
914 /* 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)915 static sljit_s32 getput_arg_fast(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg_ar, sljit_s32 arg, sljit_sw argw)
916 {
917 	SLJIT_ASSERT(arg & SLJIT_MEM);
918 
919 	if (!(arg & OFFS_REG_MASK) && argw <= SIMM_MAX && argw >= SIMM_MIN) {
920 		/* Works for both absoulte and relative addresses. */
921 		if (SLJIT_UNLIKELY(flags & ARG_TEST))
922 			return 1;
923 		FAIL_IF(push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | S(arg & REG_MASK)
924 			| TA(reg_ar) | IMM(argw), ((flags & MEM_MASK) <= GPR_REG && (flags & LOAD_DATA)) ? reg_ar : MOVABLE_INS));
925 		return -1;
926 	}
927 	return 0;
928 }
929 
930 /* See getput_arg below.
931    Note: can_cache is called only for binary operators. Those
932    operators always uses word arguments without write back. */
can_cache(sljit_s32 arg,sljit_sw argw,sljit_s32 next_arg,sljit_sw next_argw)933 static sljit_s32 can_cache(sljit_s32 arg, sljit_sw argw, sljit_s32 next_arg, sljit_sw next_argw)
934 {
935 	SLJIT_ASSERT((arg & SLJIT_MEM) && (next_arg & SLJIT_MEM));
936 
937 	/* Simple operation except for updates. */
938 	if (arg & OFFS_REG_MASK) {
939 		argw &= 0x3;
940 		next_argw &= 0x3;
941 		if (argw && argw == next_argw && (arg == next_arg || (arg & OFFS_REG_MASK) == (next_arg & OFFS_REG_MASK)))
942 			return 1;
943 		return 0;
944 	}
945 
946 	if (arg == next_arg) {
947 		if (((next_argw - argw) <= SIMM_MAX && (next_argw - argw) >= SIMM_MIN))
948 			return 1;
949 		return 0;
950 	}
951 
952 	return 0;
953 }
954 
955 /* 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)956 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)
957 {
958 	sljit_s32 tmp_ar, base, delay_slot;
959 
960 	SLJIT_ASSERT(arg & SLJIT_MEM);
961 	if (!(next_arg & SLJIT_MEM)) {
962 		next_arg = 0;
963 		next_argw = 0;
964 	}
965 
966 	if ((flags & MEM_MASK) <= GPR_REG && (flags & LOAD_DATA)) {
967 		tmp_ar = reg_ar;
968 		delay_slot = reg_ar;
969 	}
970 	else {
971 		tmp_ar = DR(TMP_REG1);
972 		delay_slot = MOVABLE_INS;
973 	}
974 	base = arg & REG_MASK;
975 
976 	if (SLJIT_UNLIKELY(arg & OFFS_REG_MASK)) {
977 		argw &= 0x3;
978 
979 		/* Using the cache. */
980 		if (argw == compiler->cache_argw) {
981 			if (arg == compiler->cache_arg)
982 				return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | S(TMP_REG3) | TA(reg_ar), delay_slot);
983 
984 			if ((SLJIT_MEM | (arg & OFFS_REG_MASK)) == compiler->cache_arg) {
985 				if (arg == next_arg && argw == (next_argw & 0x3)) {
986 					compiler->cache_arg = arg;
987 					compiler->cache_argw = argw;
988 					FAIL_IF(push_inst(compiler, ADDU_W | S(base) | T(TMP_REG3) | D(TMP_REG3), DR(TMP_REG3)));
989 					return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | S(TMP_REG3) | TA(reg_ar), delay_slot);
990 				}
991 				FAIL_IF(push_inst(compiler, ADDU_W | S(base) | T(TMP_REG3) | DA(tmp_ar), tmp_ar));
992 				return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | SA(tmp_ar) | TA(reg_ar), delay_slot);
993 			}
994 		}
995 
996 		if (SLJIT_UNLIKELY(argw)) {
997 			compiler->cache_arg = SLJIT_MEM | (arg & OFFS_REG_MASK);
998 			compiler->cache_argw = argw;
999 			FAIL_IF(push_inst(compiler, SLL_W | T(OFFS_REG(arg)) | D(TMP_REG3) | SH_IMM(argw), DR(TMP_REG3)));
1000 		}
1001 
1002 		if (arg == next_arg && argw == (next_argw & 0x3)) {
1003 			compiler->cache_arg = arg;
1004 			compiler->cache_argw = argw;
1005 			FAIL_IF(push_inst(compiler, ADDU_W | S(base) | T(!argw ? OFFS_REG(arg) : TMP_REG3) | D(TMP_REG3), DR(TMP_REG3)));
1006 			tmp_ar = DR(TMP_REG3);
1007 		}
1008 		else
1009 			FAIL_IF(push_inst(compiler, ADDU_W | S(base) | T(!argw ? OFFS_REG(arg) : TMP_REG3) | DA(tmp_ar), tmp_ar));
1010 		return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | SA(tmp_ar) | TA(reg_ar), delay_slot);
1011 	}
1012 
1013 	if (compiler->cache_arg == arg && argw - compiler->cache_argw <= SIMM_MAX && argw - compiler->cache_argw >= SIMM_MIN) {
1014 		if (argw != compiler->cache_argw) {
1015 			FAIL_IF(push_inst(compiler, ADDIU_W | S(TMP_REG3) | T(TMP_REG3) | IMM(argw - compiler->cache_argw), DR(TMP_REG3)));
1016 			compiler->cache_argw = argw;
1017 		}
1018 		return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | S(TMP_REG3) | TA(reg_ar), delay_slot);
1019 	}
1020 
1021 	if (compiler->cache_arg == SLJIT_MEM && argw - compiler->cache_argw <= SIMM_MAX && argw - compiler->cache_argw >= SIMM_MIN) {
1022 		if (argw != compiler->cache_argw)
1023 			FAIL_IF(push_inst(compiler, ADDIU_W | S(TMP_REG3) | T(TMP_REG3) | IMM(argw - compiler->cache_argw), DR(TMP_REG3)));
1024 	}
1025 	else {
1026 		compiler->cache_arg = SLJIT_MEM;
1027 		FAIL_IF(load_immediate(compiler, DR(TMP_REG3), argw));
1028 	}
1029 	compiler->cache_argw = argw;
1030 
1031 	if (!base)
1032 		return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | S(TMP_REG3) | TA(reg_ar), delay_slot);
1033 
1034 	if (arg == next_arg && next_argw - argw <= SIMM_MAX && next_argw - argw >= SIMM_MIN) {
1035 		compiler->cache_arg = arg;
1036 		FAIL_IF(push_inst(compiler, ADDU_W | S(TMP_REG3) | T(base) | D(TMP_REG3), DR(TMP_REG3)));
1037 		return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | S(TMP_REG3) | TA(reg_ar), delay_slot);
1038 	}
1039 
1040 	FAIL_IF(push_inst(compiler, ADDU_W | S(TMP_REG3) | T(base) | DA(tmp_ar), tmp_ar));
1041 	return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | SA(tmp_ar) | TA(reg_ar), delay_slot);
1042 }
1043 
emit_op_mem(struct sljit_compiler * compiler,sljit_s32 flags,sljit_s32 reg_ar,sljit_s32 arg,sljit_sw argw)1044 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)
1045 {
1046 	sljit_s32 tmp_ar, base, delay_slot;
1047 
1048 	if (getput_arg_fast(compiler, flags, reg_ar, arg, argw))
1049 		return compiler->error;
1050 
1051 	if ((flags & MEM_MASK) <= GPR_REG && (flags & LOAD_DATA)) {
1052 		tmp_ar = reg_ar;
1053 		delay_slot = reg_ar;
1054 	}
1055 	else {
1056 		tmp_ar = DR(TMP_REG1);
1057 		delay_slot = MOVABLE_INS;
1058 	}
1059 	base = arg & REG_MASK;
1060 
1061 	if (SLJIT_UNLIKELY(arg & OFFS_REG_MASK)) {
1062 		argw &= 0x3;
1063 
1064 		if (SLJIT_UNLIKELY(argw)) {
1065 			FAIL_IF(push_inst(compiler, SLL_W | T(OFFS_REG(arg)) | DA(tmp_ar) | SH_IMM(argw), tmp_ar));
1066 			FAIL_IF(push_inst(compiler, ADDU_W | S(base) | TA(tmp_ar) | DA(tmp_ar), tmp_ar));
1067 		}
1068 		else
1069 			FAIL_IF(push_inst(compiler, ADDU_W | S(base) | T(OFFS_REG(arg)) | DA(tmp_ar), tmp_ar));
1070 		return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | SA(tmp_ar) | TA(reg_ar), delay_slot);
1071 	}
1072 
1073 	FAIL_IF(load_immediate(compiler, tmp_ar, argw));
1074 
1075 	if (base != 0)
1076 		FAIL_IF(push_inst(compiler, ADDU_W | S(base) | TA(tmp_ar) | DA(tmp_ar), tmp_ar));
1077 
1078 	return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | SA(tmp_ar) | TA(reg_ar), delay_slot);
1079 }
1080 
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 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)
1082 {
1083 	if (getput_arg_fast(compiler, flags, reg, arg1, arg1w))
1084 		return compiler->error;
1085 	return getput_arg(compiler, flags, reg, arg1, arg1w, arg2, arg2w);
1086 }
1087 
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)1088 static sljit_s32 emit_op(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 flags,
1089 	sljit_s32 dst, sljit_sw dstw,
1090 	sljit_s32 src1, sljit_sw src1w,
1091 	sljit_s32 src2, sljit_sw src2w)
1092 {
1093 	/* arg1 goes to TMP_REG1 or src reg
1094 	   arg2 goes to TMP_REG2, imm or src reg
1095 	   TMP_REG3 can be used for caching
1096 	   result goes to TMP_REG2, so put result can use TMP_REG1 and TMP_REG3. */
1097 	sljit_s32 dst_r = TMP_REG2;
1098 	sljit_s32 src1_r;
1099 	sljit_sw src2_r = 0;
1100 	sljit_s32 sugg_src2_r = TMP_REG2;
1101 
1102 	if (!(flags & ALT_KEEP_CACHE)) {
1103 		compiler->cache_arg = 0;
1104 		compiler->cache_argw = 0;
1105 	}
1106 
1107 	if (SLJIT_UNLIKELY(dst == SLJIT_UNUSED)) {
1108 		SLJIT_ASSERT(HAS_FLAGS(op));
1109 		flags |= UNUSED_DEST;
1110 	}
1111 	else if (FAST_IS_REG(dst)) {
1112 		dst_r = dst;
1113 		flags |= REG_DEST;
1114 		if (op >= SLJIT_MOV && op <= SLJIT_MOV_P)
1115 			sugg_src2_r = dst_r;
1116 	}
1117 	else if ((dst & SLJIT_MEM) && !getput_arg_fast(compiler, flags | ARG_TEST, DR(TMP_REG1), dst, dstw))
1118 		flags |= SLOW_DEST;
1119 
1120 	if (flags & IMM_OP) {
1121 		if ((src2 & SLJIT_IMM) && src2w) {
1122 			if ((!(flags & LOGICAL_OP) && (src2w <= SIMM_MAX && src2w >= SIMM_MIN))
1123 				|| ((flags & LOGICAL_OP) && !(src2w & ~UIMM_MAX))) {
1124 				flags |= SRC2_IMM;
1125 				src2_r = src2w;
1126 			}
1127 		}
1128 		if (!(flags & SRC2_IMM) && (flags & CUMULATIVE_OP) && (src1 & SLJIT_IMM) && src1w) {
1129 			if ((!(flags & LOGICAL_OP) && (src1w <= SIMM_MAX && src1w >= SIMM_MIN))
1130 				|| ((flags & LOGICAL_OP) && !(src1w & ~UIMM_MAX))) {
1131 				flags |= SRC2_IMM;
1132 				src2_r = src1w;
1133 
1134 				/* And swap arguments. */
1135 				src1 = src2;
1136 				src1w = src2w;
1137 				src2 = SLJIT_IMM;
1138 				/* src2w = src2_r unneeded. */
1139 			}
1140 		}
1141 	}
1142 
1143 	/* Source 1. */
1144 	if (FAST_IS_REG(src1)) {
1145 		src1_r = src1;
1146 		flags |= REG1_SOURCE;
1147 	}
1148 	else if (src1 & SLJIT_IMM) {
1149 		if (src1w) {
1150 			FAIL_IF(load_immediate(compiler, DR(TMP_REG1), src1w));
1151 			src1_r = TMP_REG1;
1152 		}
1153 		else
1154 			src1_r = 0;
1155 	}
1156 	else {
1157 		if (getput_arg_fast(compiler, flags | LOAD_DATA, DR(TMP_REG1), src1, src1w))
1158 			FAIL_IF(compiler->error);
1159 		else
1160 			flags |= SLOW_SRC1;
1161 		src1_r = TMP_REG1;
1162 	}
1163 
1164 	/* Source 2. */
1165 	if (FAST_IS_REG(src2)) {
1166 		src2_r = src2;
1167 		flags |= REG2_SOURCE;
1168 		if (!(flags & REG_DEST) && op >= SLJIT_MOV && op <= SLJIT_MOV_P)
1169 			dst_r = src2_r;
1170 	}
1171 	else if (src2 & SLJIT_IMM) {
1172 		if (!(flags & SRC2_IMM)) {
1173 			if (src2w) {
1174 				FAIL_IF(load_immediate(compiler, DR(sugg_src2_r), src2w));
1175 				src2_r = sugg_src2_r;
1176 			}
1177 			else {
1178 				src2_r = 0;
1179 				if ((op >= SLJIT_MOV && op <= SLJIT_MOV_P) && (dst & SLJIT_MEM))
1180 					dst_r = 0;
1181 			}
1182 		}
1183 	}
1184 	else {
1185 		if (getput_arg_fast(compiler, flags | LOAD_DATA, DR(sugg_src2_r), src2, src2w))
1186 			FAIL_IF(compiler->error);
1187 		else
1188 			flags |= SLOW_SRC2;
1189 		src2_r = sugg_src2_r;
1190 	}
1191 
1192 	if ((flags & (SLOW_SRC1 | SLOW_SRC2)) == (SLOW_SRC1 | SLOW_SRC2)) {
1193 		SLJIT_ASSERT(src2_r == TMP_REG2);
1194 		if (!can_cache(src1, src1w, src2, src2w) && can_cache(src1, src1w, dst, dstw)) {
1195 			FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, DR(TMP_REG2), src2, src2w, src1, src1w));
1196 			FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, DR(TMP_REG1), src1, src1w, dst, dstw));
1197 		}
1198 		else {
1199 			FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, DR(TMP_REG1), src1, src1w, src2, src2w));
1200 			FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, DR(TMP_REG2), src2, src2w, dst, dstw));
1201 		}
1202 	}
1203 	else if (flags & SLOW_SRC1)
1204 		FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, DR(TMP_REG1), src1, src1w, dst, dstw));
1205 	else if (flags & SLOW_SRC2)
1206 		FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, DR(sugg_src2_r), src2, src2w, dst, dstw));
1207 
1208 	FAIL_IF(emit_single_op(compiler, op, flags, dst_r, src1_r, src2_r));
1209 
1210 	if (dst & SLJIT_MEM) {
1211 		if (!(flags & SLOW_DEST)) {
1212 			getput_arg_fast(compiler, flags, DR(dst_r), dst, dstw);
1213 			return compiler->error;
1214 		}
1215 		return getput_arg(compiler, flags, DR(dst_r), dst, dstw, 0, 0);
1216 	}
1217 
1218 	return SLJIT_SUCCESS;
1219 }
1220 
sljit_emit_op0(struct sljit_compiler * compiler,sljit_s32 op)1221 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op0(struct sljit_compiler *compiler, sljit_s32 op)
1222 {
1223 #if (defined SLJIT_CONFIG_MIPS_64 && SLJIT_CONFIG_MIPS_64)
1224 	sljit_s32 int_op = op & SLJIT_I32_OP;
1225 #endif
1226 
1227 	CHECK_ERROR();
1228 	CHECK(check_sljit_emit_op0(compiler, op));
1229 
1230 	op = GET_OPCODE(op);
1231 	switch (op) {
1232 	case SLJIT_BREAKPOINT:
1233 		return push_inst(compiler, BREAK, UNMOVABLE_INS);
1234 	case SLJIT_NOP:
1235 		return push_inst(compiler, NOP, UNMOVABLE_INS);
1236 	case SLJIT_LMUL_UW:
1237 	case SLJIT_LMUL_SW:
1238 #if (defined SLJIT_MIPS_REV && SLJIT_MIPS_REV >= 6)
1239 #if (defined SLJIT_CONFIG_MIPS_64 && SLJIT_CONFIG_MIPS_64)
1240 		FAIL_IF(push_inst(compiler, (op == SLJIT_LMUL_UW ? DMULU : DMUL) | S(SLJIT_R0) | T(SLJIT_R1) | D(TMP_REG3), DR(TMP_REG3)));
1241 		FAIL_IF(push_inst(compiler, (op == SLJIT_LMUL_UW ? DMUHU : DMUH) | S(SLJIT_R0) | T(SLJIT_R1) | D(TMP_REG1), DR(TMP_REG1)));
1242 #else /* !SLJIT_CONFIG_MIPS_64 */
1243 		FAIL_IF(push_inst(compiler, (op == SLJIT_LMUL_UW ? MULU : MUL) | S(SLJIT_R0) | T(SLJIT_R1) | D(TMP_REG3), DR(TMP_REG3)));
1244 		FAIL_IF(push_inst(compiler, (op == SLJIT_LMUL_UW ? MUHU : MUH) | S(SLJIT_R0) | T(SLJIT_R1) | D(TMP_REG1), DR(TMP_REG1)));
1245 #endif /* SLJIT_CONFIG_MIPS_64 */
1246 		FAIL_IF(push_inst(compiler, ADDU_W | S(TMP_REG3) | TA(0) | D(SLJIT_R0), DR(SLJIT_R0)));
1247 		return push_inst(compiler, ADDU_W | S(TMP_REG1) | TA(0) | D(SLJIT_R1), DR(SLJIT_R1));
1248 #else /* SLJIT_MIPS_REV < 6 */
1249 #if (defined SLJIT_CONFIG_MIPS_64 && SLJIT_CONFIG_MIPS_64)
1250 		FAIL_IF(push_inst(compiler, (op == SLJIT_LMUL_UW ? DMULTU : DMULT) | S(SLJIT_R0) | T(SLJIT_R1), MOVABLE_INS));
1251 #else /* !SLJIT_CONFIG_MIPS_64 */
1252 		FAIL_IF(push_inst(compiler, (op == SLJIT_LMUL_UW ? MULTU : MULT) | S(SLJIT_R0) | T(SLJIT_R1), MOVABLE_INS));
1253 #endif /* SLJIT_CONFIG_MIPS_64 */
1254 		FAIL_IF(push_inst(compiler, MFLO | D(SLJIT_R0), DR(SLJIT_R0)));
1255 		return push_inst(compiler, MFHI | D(SLJIT_R1), DR(SLJIT_R1));
1256 #endif /* SLJIT_MIPS_REV >= 6 */
1257 	case SLJIT_DIVMOD_UW:
1258 	case SLJIT_DIVMOD_SW:
1259 	case SLJIT_DIV_UW:
1260 	case SLJIT_DIV_SW:
1261 		SLJIT_COMPILE_ASSERT((SLJIT_DIVMOD_UW & 0x2) == 0 && SLJIT_DIV_UW - 0x2 == SLJIT_DIVMOD_UW, bad_div_opcode_assignments);
1262 #if (defined SLJIT_MIPS_REV && SLJIT_MIPS_REV >= 6)
1263 #if (defined SLJIT_CONFIG_MIPS_64 && SLJIT_CONFIG_MIPS_64)
1264 		if (int_op) {
1265 			FAIL_IF(push_inst(compiler, ((op | 0x2) == SLJIT_DIV_UW ? DIVU : DIV) | S(SLJIT_R0) | T(SLJIT_R1) | D(TMP_REG3), DR(TMP_REG3)));
1266 			FAIL_IF(push_inst(compiler, ((op | 0x2) == SLJIT_DIV_UW ? MODU : MOD) | S(SLJIT_R0) | T(SLJIT_R1) | D(TMP_REG1), DR(TMP_REG1)));
1267 		}
1268 		else {
1269 			FAIL_IF(push_inst(compiler, ((op | 0x2) == SLJIT_DIV_UW ? DDIVU : DDIV) | S(SLJIT_R0) | T(SLJIT_R1) | D(TMP_REG3), DR(TMP_REG3)));
1270 			FAIL_IF(push_inst(compiler, ((op | 0x2) == SLJIT_DIV_UW ? DMODU : DMOD) | S(SLJIT_R0) | T(SLJIT_R1) | D(TMP_REG1), DR(TMP_REG1)));
1271 		}
1272 #else /* !SLJIT_CONFIG_MIPS_64 */
1273 		FAIL_IF(push_inst(compiler, ((op | 0x2) == SLJIT_DIV_UW ? DIVU : DIV) | S(SLJIT_R0) | T(SLJIT_R1) | D(TMP_REG3), DR(TMP_REG3)));
1274 		FAIL_IF(push_inst(compiler, ((op | 0x2) == SLJIT_DIV_UW ? MODU : MOD) | S(SLJIT_R0) | T(SLJIT_R1) | D(TMP_REG1), DR(TMP_REG1)));
1275 #endif /* SLJIT_CONFIG_MIPS_64 */
1276 		FAIL_IF(push_inst(compiler, ADDU_W | S(TMP_REG3) | TA(0) | D(SLJIT_R0), DR(SLJIT_R0)));
1277 		return (op >= SLJIT_DIV_UW) ? SLJIT_SUCCESS : push_inst(compiler, ADDU_W | S(TMP_REG1) | TA(0) | D(SLJIT_R1), DR(SLJIT_R1));
1278 #else /* SLJIT_MIPS_REV < 6 */
1279 #if !(defined SLJIT_MIPS_REV)
1280 		FAIL_IF(push_inst(compiler, NOP, UNMOVABLE_INS));
1281 		FAIL_IF(push_inst(compiler, NOP, UNMOVABLE_INS));
1282 #endif /* !SLJIT_MIPS_REV */
1283 #if (defined SLJIT_CONFIG_MIPS_64 && SLJIT_CONFIG_MIPS_64)
1284 		if (int_op)
1285 			FAIL_IF(push_inst(compiler, ((op | 0x2) == SLJIT_DIV_UW ? DIVU : DIV) | S(SLJIT_R0) | T(SLJIT_R1), MOVABLE_INS));
1286 		else
1287 			FAIL_IF(push_inst(compiler, ((op | 0x2) == SLJIT_DIV_UW ? DDIVU : DDIV) | S(SLJIT_R0) | T(SLJIT_R1), MOVABLE_INS));
1288 #else /* !SLJIT_CONFIG_MIPS_64 */
1289 		FAIL_IF(push_inst(compiler, ((op | 0x2) == SLJIT_DIV_UW ? DIVU : DIV) | S(SLJIT_R0) | T(SLJIT_R1), MOVABLE_INS));
1290 #endif /* SLJIT_CONFIG_MIPS_64 */
1291 		FAIL_IF(push_inst(compiler, MFLO | D(SLJIT_R0), DR(SLJIT_R0)));
1292 		return (op >= SLJIT_DIV_UW) ? SLJIT_SUCCESS : push_inst(compiler, MFHI | D(SLJIT_R1), DR(SLJIT_R1));
1293 #endif /* SLJIT_MIPS_REV >= 6 */
1294 	case SLJIT_ENDBR:
1295 	case SLJIT_SKIP_FRAMES_BEFORE_RETURN:
1296 		return SLJIT_SUCCESS;
1297 	}
1298 
1299 	return SLJIT_SUCCESS;
1300 }
1301 
1302 #if (defined SLJIT_MIPS_REV && SLJIT_MIPS_REV >= 1)
emit_prefetch(struct sljit_compiler * compiler,sljit_s32 src,sljit_sw srcw)1303 static sljit_s32 emit_prefetch(struct sljit_compiler *compiler,
1304         sljit_s32 src, sljit_sw srcw)
1305 {
1306 	if (!(src & OFFS_REG_MASK)) {
1307 		if (srcw <= SIMM_MAX && srcw >= SIMM_MIN)
1308 			return push_inst(compiler, PREF | S(src & REG_MASK) | IMM(srcw), MOVABLE_INS);
1309 
1310 		FAIL_IF(load_immediate(compiler, DR(TMP_REG1), srcw));
1311 		return push_inst(compiler, PREFX | S(src & REG_MASK) | T(TMP_REG1), MOVABLE_INS);
1312 	}
1313 
1314 	srcw &= 0x3;
1315 
1316 	if (SLJIT_UNLIKELY(srcw != 0)) {
1317 		FAIL_IF(push_inst(compiler, SLL_W | T(OFFS_REG(src)) | D(TMP_REG1) | SH_IMM(srcw), DR(TMP_REG1)));
1318 		return push_inst(compiler, PREFX | S(src & REG_MASK) | T(TMP_REG1), MOVABLE_INS);
1319 	}
1320 
1321 	return push_inst(compiler, PREFX | S(src & REG_MASK) | T(OFFS_REG(src)), MOVABLE_INS);
1322 }
1323 #endif /* SLJIT_MIPS_REV >= 1 */
1324 
sljit_emit_op1(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 src,sljit_sw srcw)1325 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op1(struct sljit_compiler *compiler, sljit_s32 op,
1326 	sljit_s32 dst, sljit_sw dstw,
1327 	sljit_s32 src, sljit_sw srcw)
1328 {
1329 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
1330 #	define flags 0
1331 #else
1332 	sljit_s32 flags = 0;
1333 #endif
1334 
1335 	CHECK_ERROR();
1336 	CHECK(check_sljit_emit_op1(compiler, op, dst, dstw, src, srcw));
1337 	ADJUST_LOCAL_OFFSET(dst, dstw);
1338 	ADJUST_LOCAL_OFFSET(src, srcw);
1339 
1340 #if (defined SLJIT_CONFIG_MIPS_64 && SLJIT_CONFIG_MIPS_64)
1341 	if ((op & SLJIT_I32_OP) && GET_OPCODE(op) >= SLJIT_NOT)
1342 		flags |= INT_DATA | SIGNED_DATA;
1343 #endif
1344 
1345 	switch (GET_OPCODE(op)) {
1346 	case SLJIT_MOV:
1347 	case SLJIT_MOV_P:
1348 		return emit_op(compiler, SLJIT_MOV, WORD_DATA, dst, dstw, TMP_REG1, 0, src, srcw);
1349 
1350 	case SLJIT_MOV_U32:
1351 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
1352 		return emit_op(compiler, SLJIT_MOV_U32, INT_DATA, dst, dstw, TMP_REG1, 0, src, srcw);
1353 #else
1354 		return emit_op(compiler, SLJIT_MOV_U32, INT_DATA, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_u32)srcw : srcw);
1355 #endif
1356 
1357 	case SLJIT_MOV_S32:
1358 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
1359 		return emit_op(compiler, SLJIT_MOV_S32, INT_DATA | SIGNED_DATA, dst, dstw, TMP_REG1, 0, src, srcw);
1360 #else
1361 		return emit_op(compiler, SLJIT_MOV_S32, INT_DATA | SIGNED_DATA, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_s32)srcw : srcw);
1362 #endif
1363 
1364 	case SLJIT_MOV_U8:
1365 		return emit_op(compiler, SLJIT_MOV_U8, BYTE_DATA, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_u8)srcw : srcw);
1366 
1367 	case SLJIT_MOV_S8:
1368 		return emit_op(compiler, SLJIT_MOV_S8, BYTE_DATA | SIGNED_DATA, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_s8)srcw : srcw);
1369 
1370 	case SLJIT_MOV_U16:
1371 		return emit_op(compiler, SLJIT_MOV_U16, HALF_DATA, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_u16)srcw : srcw);
1372 
1373 	case SLJIT_MOV_S16:
1374 		return emit_op(compiler, SLJIT_MOV_S16, HALF_DATA | SIGNED_DATA, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_s16)srcw : srcw);
1375 
1376 	case SLJIT_NOT:
1377 		return emit_op(compiler, op, flags, dst, dstw, TMP_REG1, 0, src, srcw);
1378 
1379 	case SLJIT_NEG:
1380 		return emit_op(compiler, SLJIT_SUB | GET_ALL_FLAGS(op), flags | IMM_OP, dst, dstw, SLJIT_IMM, 0, src, srcw);
1381 
1382 	case SLJIT_CLZ:
1383 		return emit_op(compiler, op, flags, dst, dstw, TMP_REG1, 0, src, srcw);
1384 	}
1385 
1386 	SLJIT_UNREACHABLE();
1387 	return SLJIT_SUCCESS;
1388 
1389 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
1390 #	undef flags
1391 #endif
1392 }
1393 
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)1394 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op2(struct sljit_compiler *compiler, sljit_s32 op,
1395 	sljit_s32 dst, sljit_sw dstw,
1396 	sljit_s32 src1, sljit_sw src1w,
1397 	sljit_s32 src2, sljit_sw src2w)
1398 {
1399 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
1400 #	define flags 0
1401 #else
1402 	sljit_s32 flags = 0;
1403 #endif
1404 
1405 	CHECK_ERROR();
1406 	CHECK(check_sljit_emit_op2(compiler, op, dst, dstw, src1, src1w, src2, src2w));
1407 	ADJUST_LOCAL_OFFSET(dst, dstw);
1408 	ADJUST_LOCAL_OFFSET(src1, src1w);
1409 	ADJUST_LOCAL_OFFSET(src2, src2w);
1410 
1411 	if (dst == SLJIT_UNUSED && !HAS_FLAGS(op))
1412 		return SLJIT_SUCCESS;
1413 
1414 #if (defined SLJIT_CONFIG_MIPS_64 && SLJIT_CONFIG_MIPS_64)
1415 	if (op & SLJIT_I32_OP) {
1416 		flags |= INT_DATA | SIGNED_DATA;
1417 		if (src1 & SLJIT_IMM)
1418 			src1w = (sljit_s32)src1w;
1419 		if (src2 & SLJIT_IMM)
1420 			src2w = (sljit_s32)src2w;
1421 	}
1422 #endif
1423 
1424 	switch (GET_OPCODE(op)) {
1425 	case SLJIT_ADD:
1426 	case SLJIT_ADDC:
1427 		return emit_op(compiler, op, flags | CUMULATIVE_OP | IMM_OP, dst, dstw, src1, src1w, src2, src2w);
1428 
1429 	case SLJIT_SUB:
1430 	case SLJIT_SUBC:
1431 		return emit_op(compiler, op, flags | IMM_OP, dst, dstw, src1, src1w, src2, src2w);
1432 
1433 	case SLJIT_MUL:
1434 		return emit_op(compiler, op, flags | CUMULATIVE_OP, dst, dstw, src1, src1w, src2, src2w);
1435 
1436 	case SLJIT_AND:
1437 	case SLJIT_OR:
1438 	case SLJIT_XOR:
1439 		return emit_op(compiler, op, flags | CUMULATIVE_OP | LOGICAL_OP | IMM_OP, dst, dstw, src1, src1w, src2, src2w);
1440 
1441 	case SLJIT_SHL:
1442 	case SLJIT_LSHR:
1443 	case SLJIT_ASHR:
1444 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
1445 		if (src2 & SLJIT_IMM)
1446 			src2w &= 0x1f;
1447 #else
1448 		if (src2 & SLJIT_IMM) {
1449 			if (op & SLJIT_I32_OP)
1450 				src2w &= 0x1f;
1451 			else
1452 				src2w &= 0x3f;
1453 		}
1454 #endif
1455 		return emit_op(compiler, op, flags | IMM_OP, dst, dstw, src1, src1w, src2, src2w);
1456 	}
1457 
1458 	SLJIT_UNREACHABLE();
1459 	return SLJIT_SUCCESS;
1460 
1461 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
1462 #	undef flags
1463 #endif
1464 }
1465 
sljit_emit_op_src(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 src,sljit_sw srcw)1466 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_src(struct sljit_compiler *compiler, sljit_s32 op,
1467 	sljit_s32 src, sljit_sw srcw)
1468 {
1469 	CHECK_ERROR();
1470 	CHECK(check_sljit_emit_op_src(compiler, op, src, srcw));
1471 	ADJUST_LOCAL_OFFSET(src, srcw);
1472 
1473 	switch (op) {
1474 	case SLJIT_FAST_RETURN:
1475 		if (FAST_IS_REG(src))
1476 			FAIL_IF(push_inst(compiler, ADDU_W | S(src) | TA(0) | DA(RETURN_ADDR_REG), RETURN_ADDR_REG));
1477 		else
1478 			FAIL_IF(emit_op_mem(compiler, WORD_DATA | LOAD_DATA, RETURN_ADDR_REG, src, srcw));
1479 
1480 		FAIL_IF(push_inst(compiler, JR | SA(RETURN_ADDR_REG), UNMOVABLE_INS));
1481 		return push_inst(compiler, NOP, UNMOVABLE_INS);
1482 	case SLJIT_SKIP_FRAMES_BEFORE_FAST_RETURN:
1483 		return SLJIT_SUCCESS;
1484 	case SLJIT_PREFETCH_L1:
1485 	case SLJIT_PREFETCH_L2:
1486 	case SLJIT_PREFETCH_L3:
1487 	case SLJIT_PREFETCH_ONCE:
1488 #if (defined SLJIT_MIPS_REV && SLJIT_MIPS_REV >= 1)
1489 		return emit_prefetch(compiler, src, srcw);
1490 #else /* SLJIT_MIPS_REV < 1 */
1491 		return SLJIT_SUCCESS;
1492 #endif /* SLJIT_MIPS_REV >= 1 */
1493 	}
1494 
1495 	return SLJIT_SUCCESS;
1496 }
1497 
sljit_get_register_index(sljit_s32 reg)1498 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_register_index(sljit_s32 reg)
1499 {
1500 	CHECK_REG_INDEX(check_sljit_get_register_index(reg));
1501 	return reg_map[reg];
1502 }
1503 
sljit_get_float_register_index(sljit_s32 reg)1504 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_float_register_index(sljit_s32 reg)
1505 {
1506 	CHECK_REG_INDEX(check_sljit_get_float_register_index(reg));
1507 	return FR(reg);
1508 }
1509 
sljit_emit_op_custom(struct sljit_compiler * compiler,void * instruction,sljit_s32 size)1510 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_custom(struct sljit_compiler *compiler,
1511 	void *instruction, sljit_s32 size)
1512 {
1513 	CHECK_ERROR();
1514 	CHECK(check_sljit_emit_op_custom(compiler, instruction, size));
1515 
1516 	return push_inst(compiler, *(sljit_ins*)instruction, UNMOVABLE_INS);
1517 }
1518 
1519 /* --------------------------------------------------------------------- */
1520 /*  Floating point operators                                             */
1521 /* --------------------------------------------------------------------- */
1522 
1523 #define FLOAT_DATA(op) (DOUBLE_DATA | ((op & SLJIT_F32_OP) >> 7))
1524 #define FMT(op) (((op & SLJIT_F32_OP) ^ SLJIT_F32_OP) << (21 - 8))
1525 
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)1526 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_conv_sw_from_f64(struct sljit_compiler *compiler, sljit_s32 op,
1527 	sljit_s32 dst, sljit_sw dstw,
1528 	sljit_s32 src, sljit_sw srcw)
1529 {
1530 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
1531 #	define flags 0
1532 #else
1533 	sljit_s32 flags = (GET_OPCODE(op) == SLJIT_CONV_SW_FROM_F64) << 21;
1534 #endif
1535 
1536 	if (src & SLJIT_MEM) {
1537 		FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op) | LOAD_DATA, FR(TMP_FREG1), src, srcw, dst, dstw));
1538 		src = TMP_FREG1;
1539 	}
1540 
1541 	FAIL_IF(push_inst(compiler, (TRUNC_W_S ^ (flags >> 19)) | FMT(op) | FS(src) | FD(TMP_FREG1), MOVABLE_INS));
1542 
1543 	if (FAST_IS_REG(dst))
1544 		return push_inst(compiler, MFC1 | flags | T(dst) | FS(TMP_FREG1), MOVABLE_INS);
1545 
1546 	/* Store the integer value from a VFP register. */
1547 	return emit_op_mem2(compiler, flags ? DOUBLE_DATA : SINGLE_DATA, FR(TMP_FREG1), dst, dstw, 0, 0);
1548 
1549 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
1550 #	undef is_long
1551 #endif
1552 }
1553 
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)1554 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_conv_f64_from_sw(struct sljit_compiler *compiler, sljit_s32 op,
1555 	sljit_s32 dst, sljit_sw dstw,
1556 	sljit_s32 src, sljit_sw srcw)
1557 {
1558 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
1559 #	define flags 0
1560 #else
1561 	sljit_s32 flags = (GET_OPCODE(op) == SLJIT_CONV_F64_FROM_SW) << 21;
1562 #endif
1563 
1564 	sljit_s32 dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
1565 
1566 	if (FAST_IS_REG(src))
1567 		FAIL_IF(push_inst(compiler, MTC1 | flags | T(src) | FS(TMP_FREG1), MOVABLE_INS));
1568 	else if (src & SLJIT_MEM) {
1569 		/* Load the integer value into a VFP register. */
1570 		FAIL_IF(emit_op_mem2(compiler, ((flags) ? DOUBLE_DATA : SINGLE_DATA) | LOAD_DATA, FR(TMP_FREG1), src, srcw, dst, dstw));
1571 	}
1572 	else {
1573 #if (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64)
1574 		if (GET_OPCODE(op) == SLJIT_CONV_F64_FROM_S32)
1575 			srcw = (sljit_s32)srcw;
1576 #endif
1577 		FAIL_IF(load_immediate(compiler, DR(TMP_REG1), srcw));
1578 		FAIL_IF(push_inst(compiler, MTC1 | flags | T(TMP_REG1) | FS(TMP_FREG1), MOVABLE_INS));
1579 	}
1580 
1581 	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));
1582 
1583 	if (dst & SLJIT_MEM)
1584 		return emit_op_mem2(compiler, FLOAT_DATA(op), FR(TMP_FREG1), dst, dstw, 0, 0);
1585 	return SLJIT_SUCCESS;
1586 
1587 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
1588 #	undef flags
1589 #endif
1590 }
1591 
sljit_emit_fop1_cmp(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 src1,sljit_sw src1w,sljit_s32 src2,sljit_sw src2w)1592 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_cmp(struct sljit_compiler *compiler, sljit_s32 op,
1593 	sljit_s32 src1, sljit_sw src1w,
1594 	sljit_s32 src2, sljit_sw src2w)
1595 {
1596 	sljit_ins inst;
1597 
1598 	if (src1 & SLJIT_MEM) {
1599 		FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op) | LOAD_DATA, FR(TMP_FREG1), src1, src1w, src2, src2w));
1600 		src1 = TMP_FREG1;
1601 	}
1602 
1603 	if (src2 & SLJIT_MEM) {
1604 		FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op) | LOAD_DATA, FR(TMP_FREG2), src2, src2w, 0, 0));
1605 		src2 = TMP_FREG2;
1606 	}
1607 
1608 	switch (GET_FLAG_TYPE(op)) {
1609 	case SLJIT_EQUAL_F64:
1610 	case SLJIT_NOT_EQUAL_F64:
1611 		inst = C_UEQ_S;
1612 		break;
1613 	case SLJIT_LESS_F64:
1614 	case SLJIT_GREATER_EQUAL_F64:
1615 		inst = C_ULT_S;
1616 		break;
1617 	case SLJIT_GREATER_F64:
1618 	case SLJIT_LESS_EQUAL_F64:
1619 		inst = C_ULE_S;
1620 		break;
1621 	default:
1622 		SLJIT_ASSERT(GET_FLAG_TYPE(op) == SLJIT_UNORDERED_F64 || GET_FLAG_TYPE(op) == SLJIT_ORDERED_F64);
1623 		inst = C_UN_S;
1624 		break;
1625 	}
1626 	return push_inst(compiler, inst | FMT(op) | FT(src2) | FS(src1) | C_FD, UNMOVABLE_INS);
1627 }
1628 
sljit_emit_fop1(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 src,sljit_sw srcw)1629 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop1(struct sljit_compiler *compiler, sljit_s32 op,
1630 	sljit_s32 dst, sljit_sw dstw,
1631 	sljit_s32 src, sljit_sw srcw)
1632 {
1633 	sljit_s32 dst_r;
1634 
1635 	CHECK_ERROR();
1636 	compiler->cache_arg = 0;
1637 	compiler->cache_argw = 0;
1638 
1639 	SLJIT_COMPILE_ASSERT((SLJIT_F32_OP == 0x100) && !(DOUBLE_DATA & 0x2), float_transfer_bit_error);
1640 	SELECT_FOP1_OPERATION_WITH_CHECKS(compiler, op, dst, dstw, src, srcw);
1641 
1642 	if (GET_OPCODE(op) == SLJIT_CONV_F64_FROM_F32)
1643 		op ^= SLJIT_F32_OP;
1644 
1645 	dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
1646 
1647 	if (src & SLJIT_MEM) {
1648 		FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op) | LOAD_DATA, FR(dst_r), src, srcw, dst, dstw));
1649 		src = dst_r;
1650 	}
1651 
1652 	switch (GET_OPCODE(op)) {
1653 	case SLJIT_MOV_F64:
1654 		if (src != dst_r) {
1655 			if (dst_r != TMP_FREG1)
1656 				FAIL_IF(push_inst(compiler, MOV_S | FMT(op) | FS(src) | FD(dst_r), MOVABLE_INS));
1657 			else
1658 				dst_r = src;
1659 		}
1660 		break;
1661 	case SLJIT_NEG_F64:
1662 		FAIL_IF(push_inst(compiler, NEG_S | FMT(op) | FS(src) | FD(dst_r), MOVABLE_INS));
1663 		break;
1664 	case SLJIT_ABS_F64:
1665 		FAIL_IF(push_inst(compiler, ABS_S | FMT(op) | FS(src) | FD(dst_r), MOVABLE_INS));
1666 		break;
1667 	case SLJIT_CONV_F64_FROM_F32:
1668 		FAIL_IF(push_inst(compiler, CVT_S_S | ((op & SLJIT_F32_OP) ? 1 : (1 << 21)) | FS(src) | FD(dst_r), MOVABLE_INS));
1669 		op ^= SLJIT_F32_OP;
1670 		break;
1671 	}
1672 
1673 	if (dst & SLJIT_MEM)
1674 		return emit_op_mem2(compiler, FLOAT_DATA(op), FR(dst_r), dst, dstw, 0, 0);
1675 	return SLJIT_SUCCESS;
1676 }
1677 
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)1678 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop2(struct sljit_compiler *compiler, sljit_s32 op,
1679 	sljit_s32 dst, sljit_sw dstw,
1680 	sljit_s32 src1, sljit_sw src1w,
1681 	sljit_s32 src2, sljit_sw src2w)
1682 {
1683 	sljit_s32 dst_r, flags = 0;
1684 
1685 	CHECK_ERROR();
1686 	CHECK(check_sljit_emit_fop2(compiler, op, dst, dstw, src1, src1w, src2, src2w));
1687 	ADJUST_LOCAL_OFFSET(dst, dstw);
1688 	ADJUST_LOCAL_OFFSET(src1, src1w);
1689 	ADJUST_LOCAL_OFFSET(src2, src2w);
1690 
1691 	compiler->cache_arg = 0;
1692 	compiler->cache_argw = 0;
1693 
1694 	dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG2;
1695 
1696 	if (src1 & SLJIT_MEM) {
1697 		if (getput_arg_fast(compiler, FLOAT_DATA(op) | LOAD_DATA, FR(TMP_FREG1), src1, src1w)) {
1698 			FAIL_IF(compiler->error);
1699 			src1 = TMP_FREG1;
1700 		} else
1701 			flags |= SLOW_SRC1;
1702 	}
1703 
1704 	if (src2 & SLJIT_MEM) {
1705 		if (getput_arg_fast(compiler, FLOAT_DATA(op) | LOAD_DATA, FR(TMP_FREG2), src2, src2w)) {
1706 			FAIL_IF(compiler->error);
1707 			src2 = TMP_FREG2;
1708 		} else
1709 			flags |= SLOW_SRC2;
1710 	}
1711 
1712 	if ((flags & (SLOW_SRC1 | SLOW_SRC2)) == (SLOW_SRC1 | SLOW_SRC2)) {
1713 		if (!can_cache(src1, src1w, src2, src2w) && can_cache(src1, src1w, dst, dstw)) {
1714 			FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, FR(TMP_FREG2), src2, src2w, src1, src1w));
1715 			FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, FR(TMP_FREG1), src1, src1w, dst, dstw));
1716 		}
1717 		else {
1718 			FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, FR(TMP_FREG1), src1, src1w, src2, src2w));
1719 			FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, FR(TMP_FREG2), src2, src2w, dst, dstw));
1720 		}
1721 	}
1722 	else if (flags & SLOW_SRC1)
1723 		FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, FR(TMP_FREG1), src1, src1w, dst, dstw));
1724 	else if (flags & SLOW_SRC2)
1725 		FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, FR(TMP_FREG2), src2, src2w, dst, dstw));
1726 
1727 	if (flags & SLOW_SRC1)
1728 		src1 = TMP_FREG1;
1729 	if (flags & SLOW_SRC2)
1730 		src2 = TMP_FREG2;
1731 
1732 	switch (GET_OPCODE(op)) {
1733 	case SLJIT_ADD_F64:
1734 		FAIL_IF(push_inst(compiler, ADD_S | FMT(op) | FT(src2) | FS(src1) | FD(dst_r), MOVABLE_INS));
1735 		break;
1736 
1737 	case SLJIT_SUB_F64:
1738 		FAIL_IF(push_inst(compiler, SUB_S | FMT(op) | FT(src2) | FS(src1) | FD(dst_r), MOVABLE_INS));
1739 		break;
1740 
1741 	case SLJIT_MUL_F64:
1742 		FAIL_IF(push_inst(compiler, MUL_S | FMT(op) | FT(src2) | FS(src1) | FD(dst_r), MOVABLE_INS));
1743 		break;
1744 
1745 	case SLJIT_DIV_F64:
1746 		FAIL_IF(push_inst(compiler, DIV_S | FMT(op) | FT(src2) | FS(src1) | FD(dst_r), MOVABLE_INS));
1747 		break;
1748 	}
1749 
1750 	if (dst_r == TMP_FREG2)
1751 		FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op), FR(TMP_FREG2), dst, dstw, 0, 0));
1752 
1753 	return SLJIT_SUCCESS;
1754 }
1755 
1756 /* --------------------------------------------------------------------- */
1757 /*  Other instructions                                                   */
1758 /* --------------------------------------------------------------------- */
1759 
sljit_emit_fast_enter(struct sljit_compiler * compiler,sljit_s32 dst,sljit_sw dstw)1760 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fast_enter(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw)
1761 {
1762 	CHECK_ERROR();
1763 	CHECK(check_sljit_emit_fast_enter(compiler, dst, dstw));
1764 	ADJUST_LOCAL_OFFSET(dst, dstw);
1765 
1766 	if (FAST_IS_REG(dst))
1767 		return push_inst(compiler, ADDU_W | SA(RETURN_ADDR_REG) | TA(0) | D(dst), UNMOVABLE_INS);
1768 
1769 	/* Memory. */
1770 	FAIL_IF(emit_op_mem(compiler, WORD_DATA, RETURN_ADDR_REG, dst, dstw));
1771 	compiler->delay_slot = UNMOVABLE_INS;
1772 	return SLJIT_SUCCESS;
1773 }
1774 
1775 /* --------------------------------------------------------------------- */
1776 /*  Conditional instructions                                             */
1777 /* --------------------------------------------------------------------- */
1778 
sljit_emit_label(struct sljit_compiler * compiler)1779 SLJIT_API_FUNC_ATTRIBUTE struct sljit_label* sljit_emit_label(struct sljit_compiler *compiler)
1780 {
1781 	struct sljit_label *label;
1782 
1783 	CHECK_ERROR_PTR();
1784 	CHECK_PTR(check_sljit_emit_label(compiler));
1785 
1786 	if (compiler->last_label && compiler->last_label->size == compiler->size)
1787 		return compiler->last_label;
1788 
1789 	label = (struct sljit_label*)ensure_abuf(compiler, sizeof(struct sljit_label));
1790 	PTR_FAIL_IF(!label);
1791 	set_label(label, compiler);
1792 	compiler->delay_slot = UNMOVABLE_INS;
1793 	return label;
1794 }
1795 
1796 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
1797 #define JUMP_LENGTH	4
1798 #else
1799 #define JUMP_LENGTH	8
1800 #endif
1801 
1802 #define BR_Z(src) \
1803 	inst = BEQ | SA(src) | TA(0) | JUMP_LENGTH; \
1804 	flags = IS_BIT26_COND; \
1805 	delay_check = src;
1806 
1807 #define BR_NZ(src) \
1808 	inst = BNE | SA(src) | TA(0) | JUMP_LENGTH; \
1809 	flags = IS_BIT26_COND; \
1810 	delay_check = src;
1811 
1812 #if (defined SLJIT_MIPS_REV && SLJIT_MIPS_REV >= 6)
1813 
1814 #define BR_T() \
1815 	inst = BC1NEZ; \
1816 	flags = IS_BIT23_COND; \
1817 	delay_check = FCSR_FCC;
1818 #define BR_F() \
1819 	inst = BC1EQZ; \
1820 	flags = IS_BIT23_COND; \
1821 	delay_check = FCSR_FCC;
1822 
1823 #else /* SLJIT_MIPS_REV < 6 */
1824 
1825 #define BR_T() \
1826 	inst = BC1T | JUMP_LENGTH; \
1827 	flags = IS_BIT16_COND; \
1828 	delay_check = FCSR_FCC;
1829 #define BR_F() \
1830 	inst = BC1F | JUMP_LENGTH; \
1831 	flags = IS_BIT16_COND; \
1832 	delay_check = FCSR_FCC;
1833 
1834 #endif /* SLJIT_MIPS_REV >= 6 */
1835 
sljit_emit_jump(struct sljit_compiler * compiler,sljit_s32 type)1836 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_jump(struct sljit_compiler *compiler, sljit_s32 type)
1837 {
1838 	struct sljit_jump *jump;
1839 	sljit_ins inst;
1840 	sljit_s32 flags = 0;
1841 	sljit_s32 delay_check = UNMOVABLE_INS;
1842 
1843 	CHECK_ERROR_PTR();
1844 	CHECK_PTR(check_sljit_emit_jump(compiler, type));
1845 
1846 	jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1847 	PTR_FAIL_IF(!jump);
1848 	set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
1849 	type &= 0xff;
1850 
1851 	switch (type) {
1852 	case SLJIT_EQUAL:
1853 		BR_NZ(EQUAL_FLAG);
1854 		break;
1855 	case SLJIT_NOT_EQUAL:
1856 		BR_Z(EQUAL_FLAG);
1857 		break;
1858 	case SLJIT_LESS:
1859 	case SLJIT_GREATER:
1860 	case SLJIT_SIG_LESS:
1861 	case SLJIT_SIG_GREATER:
1862 	case SLJIT_OVERFLOW:
1863 	case SLJIT_MUL_OVERFLOW:
1864 		BR_Z(OTHER_FLAG);
1865 		break;
1866 	case SLJIT_GREATER_EQUAL:
1867 	case SLJIT_LESS_EQUAL:
1868 	case SLJIT_SIG_GREATER_EQUAL:
1869 	case SLJIT_SIG_LESS_EQUAL:
1870 	case SLJIT_NOT_OVERFLOW:
1871 	case SLJIT_MUL_NOT_OVERFLOW:
1872 		BR_NZ(OTHER_FLAG);
1873 		break;
1874 	case SLJIT_NOT_EQUAL_F64:
1875 	case SLJIT_GREATER_EQUAL_F64:
1876 	case SLJIT_GREATER_F64:
1877 	case SLJIT_ORDERED_F64:
1878 		BR_T();
1879 		break;
1880 	case SLJIT_EQUAL_F64:
1881 	case SLJIT_LESS_F64:
1882 	case SLJIT_LESS_EQUAL_F64:
1883 	case SLJIT_UNORDERED_F64:
1884 		BR_F();
1885 		break;
1886 	default:
1887 		/* Not conditional branch. */
1888 		inst = 0;
1889 		break;
1890 	}
1891 
1892 	jump->flags |= flags;
1893 	if (compiler->delay_slot == MOVABLE_INS || (compiler->delay_slot != UNMOVABLE_INS && compiler->delay_slot != delay_check))
1894 		jump->flags |= IS_MOVABLE;
1895 
1896 	if (inst)
1897 		PTR_FAIL_IF(push_inst(compiler, inst, UNMOVABLE_INS));
1898 
1899 	PTR_FAIL_IF(emit_const(compiler, TMP_REG2, 0));
1900 
1901 	if (type <= SLJIT_JUMP)
1902 		PTR_FAIL_IF(push_inst(compiler, JR | S(TMP_REG2), UNMOVABLE_INS));
1903 	else {
1904 		jump->flags |= IS_JAL;
1905 		PTR_FAIL_IF(push_inst(compiler, JALR | S(TMP_REG2) | DA(RETURN_ADDR_REG), UNMOVABLE_INS));
1906 	}
1907 
1908 	jump->addr = compiler->size;
1909 	PTR_FAIL_IF(push_inst(compiler, NOP, UNMOVABLE_INS));
1910 	return jump;
1911 }
1912 
1913 #define RESOLVE_IMM1() \
1914 	if (src1 & SLJIT_IMM) { \
1915 		if (src1w) { \
1916 			PTR_FAIL_IF(load_immediate(compiler, DR(TMP_REG1), src1w)); \
1917 			src1 = TMP_REG1; \
1918 		} \
1919 		else \
1920 			src1 = 0; \
1921 	}
1922 
1923 #define RESOLVE_IMM2() \
1924 	if (src2 & SLJIT_IMM) { \
1925 		if (src2w) { \
1926 			PTR_FAIL_IF(load_immediate(compiler, DR(TMP_REG2), src2w)); \
1927 			src2 = TMP_REG2; \
1928 		} \
1929 		else \
1930 			src2 = 0; \
1931 	}
1932 
sljit_emit_cmp(struct sljit_compiler * compiler,sljit_s32 type,sljit_s32 src1,sljit_sw src1w,sljit_s32 src2,sljit_sw src2w)1933 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_cmp(struct sljit_compiler *compiler, sljit_s32 type,
1934 	sljit_s32 src1, sljit_sw src1w,
1935 	sljit_s32 src2, sljit_sw src2w)
1936 {
1937 	struct sljit_jump *jump;
1938 	sljit_s32 flags;
1939 	sljit_ins inst;
1940 
1941 	CHECK_ERROR_PTR();
1942 	CHECK_PTR(check_sljit_emit_cmp(compiler, type, src1, src1w, src2, src2w));
1943 	ADJUST_LOCAL_OFFSET(src1, src1w);
1944 	ADJUST_LOCAL_OFFSET(src2, src2w);
1945 
1946 	compiler->cache_arg = 0;
1947 	compiler->cache_argw = 0;
1948 	flags = ((type & SLJIT_I32_OP) ? INT_DATA : WORD_DATA) | LOAD_DATA;
1949 	if (src1 & SLJIT_MEM) {
1950 		PTR_FAIL_IF(emit_op_mem2(compiler, flags, DR(TMP_REG1), src1, src1w, src2, src2w));
1951 		src1 = TMP_REG1;
1952 	}
1953 	if (src2 & SLJIT_MEM) {
1954 		PTR_FAIL_IF(emit_op_mem2(compiler, flags, DR(TMP_REG2), src2, src2w, 0, 0));
1955 		src2 = TMP_REG2;
1956 	}
1957 
1958 	jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1959 	PTR_FAIL_IF(!jump);
1960 	set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
1961 	type &= 0xff;
1962 
1963 	if (type <= SLJIT_NOT_EQUAL) {
1964 		RESOLVE_IMM1();
1965 		RESOLVE_IMM2();
1966 		jump->flags |= IS_BIT26_COND;
1967 		if (compiler->delay_slot == MOVABLE_INS || (compiler->delay_slot != UNMOVABLE_INS && compiler->delay_slot != DR(src1) && compiler->delay_slot != DR(src2)))
1968 			jump->flags |= IS_MOVABLE;
1969 		PTR_FAIL_IF(push_inst(compiler, (type == SLJIT_EQUAL ? BNE : BEQ) | S(src1) | T(src2) | JUMP_LENGTH, UNMOVABLE_INS));
1970 	}
1971 	else if (type >= SLJIT_SIG_LESS && (((src1 & SLJIT_IMM) && (src1w == 0)) || ((src2 & SLJIT_IMM) && (src2w == 0)))) {
1972 		inst = NOP;
1973 		if ((src1 & SLJIT_IMM) && (src1w == 0)) {
1974 			RESOLVE_IMM2();
1975 			switch (type) {
1976 			case SLJIT_SIG_LESS:
1977 				inst = BLEZ;
1978 				jump->flags |= IS_BIT26_COND;
1979 				break;
1980 			case SLJIT_SIG_GREATER_EQUAL:
1981 				inst = BGTZ;
1982 				jump->flags |= IS_BIT26_COND;
1983 				break;
1984 			case SLJIT_SIG_GREATER:
1985 				inst = BGEZ;
1986 				jump->flags |= IS_BIT16_COND;
1987 				break;
1988 			case SLJIT_SIG_LESS_EQUAL:
1989 				inst = BLTZ;
1990 				jump->flags |= IS_BIT16_COND;
1991 				break;
1992 			}
1993 			src1 = src2;
1994 		}
1995 		else {
1996 			RESOLVE_IMM1();
1997 			switch (type) {
1998 			case SLJIT_SIG_LESS:
1999 				inst = BGEZ;
2000 				jump->flags |= IS_BIT16_COND;
2001 				break;
2002 			case SLJIT_SIG_GREATER_EQUAL:
2003 				inst = BLTZ;
2004 				jump->flags |= IS_BIT16_COND;
2005 				break;
2006 			case SLJIT_SIG_GREATER:
2007 				inst = BLEZ;
2008 				jump->flags |= IS_BIT26_COND;
2009 				break;
2010 			case SLJIT_SIG_LESS_EQUAL:
2011 				inst = BGTZ;
2012 				jump->flags |= IS_BIT26_COND;
2013 				break;
2014 			}
2015 		}
2016 		PTR_FAIL_IF(push_inst(compiler, inst | S(src1) | JUMP_LENGTH, UNMOVABLE_INS));
2017 	}
2018 	else {
2019 		if (type == SLJIT_LESS || type == SLJIT_GREATER_EQUAL || type == SLJIT_SIG_LESS || type == SLJIT_SIG_GREATER_EQUAL) {
2020 			RESOLVE_IMM1();
2021 			if ((src2 & SLJIT_IMM) && src2w <= SIMM_MAX && src2w >= SIMM_MIN)
2022 				PTR_FAIL_IF(push_inst(compiler, (type <= SLJIT_LESS_EQUAL ? SLTIU : SLTI) | S(src1) | T(TMP_REG1) | IMM(src2w), DR(TMP_REG1)));
2023 			else {
2024 				RESOLVE_IMM2();
2025 				PTR_FAIL_IF(push_inst(compiler, (type <= SLJIT_LESS_EQUAL ? SLTU : SLT) | S(src1) | T(src2) | D(TMP_REG1), DR(TMP_REG1)));
2026 			}
2027 			type = (type == SLJIT_LESS || type == SLJIT_SIG_LESS) ? SLJIT_NOT_EQUAL : SLJIT_EQUAL;
2028 		}
2029 		else {
2030 			RESOLVE_IMM2();
2031 			if ((src1 & SLJIT_IMM) && src1w <= SIMM_MAX && src1w >= SIMM_MIN)
2032 				PTR_FAIL_IF(push_inst(compiler, (type <= SLJIT_LESS_EQUAL ? SLTIU : SLTI) | S(src2) | T(TMP_REG1) | IMM(src1w), DR(TMP_REG1)));
2033 			else {
2034 				RESOLVE_IMM1();
2035 				PTR_FAIL_IF(push_inst(compiler, (type <= SLJIT_LESS_EQUAL ? SLTU : SLT) | S(src2) | T(src1) | D(TMP_REG1), DR(TMP_REG1)));
2036 			}
2037 			type = (type == SLJIT_GREATER || type == SLJIT_SIG_GREATER) ? SLJIT_NOT_EQUAL : SLJIT_EQUAL;
2038 		}
2039 
2040 		jump->flags |= IS_BIT26_COND;
2041 		PTR_FAIL_IF(push_inst(compiler, (type == SLJIT_EQUAL ? BNE : BEQ) | S(TMP_REG1) | TA(0) | JUMP_LENGTH, UNMOVABLE_INS));
2042 	}
2043 
2044 	PTR_FAIL_IF(emit_const(compiler, TMP_REG2, 0));
2045 	PTR_FAIL_IF(push_inst(compiler, JR | S(TMP_REG2), UNMOVABLE_INS));
2046 	jump->addr = compiler->size;
2047 	PTR_FAIL_IF(push_inst(compiler, NOP, UNMOVABLE_INS));
2048 	return jump;
2049 }
2050 
2051 #undef RESOLVE_IMM1
2052 #undef RESOLVE_IMM2
2053 
2054 #undef JUMP_LENGTH
2055 #undef BR_Z
2056 #undef BR_NZ
2057 #undef BR_T
2058 #undef BR_F
2059 
2060 #undef FLOAT_DATA
2061 #undef FMT
2062 
sljit_emit_ijump(struct sljit_compiler * compiler,sljit_s32 type,sljit_s32 src,sljit_sw srcw)2063 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_ijump(struct sljit_compiler *compiler, sljit_s32 type, sljit_s32 src, sljit_sw srcw)
2064 {
2065 	struct sljit_jump *jump = NULL;
2066 
2067 	CHECK_ERROR();
2068 	CHECK(check_sljit_emit_ijump(compiler, type, src, srcw));
2069 	ADJUST_LOCAL_OFFSET(src, srcw);
2070 
2071 	if (src & SLJIT_IMM) {
2072 		jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
2073 		FAIL_IF(!jump);
2074 		set_jump(jump, compiler, JUMP_ADDR | ((type >= SLJIT_FAST_CALL) ? IS_JAL : 0));
2075 		jump->u.target = srcw;
2076 
2077 		if (compiler->delay_slot != UNMOVABLE_INS)
2078 			jump->flags |= IS_MOVABLE;
2079 
2080 		FAIL_IF(emit_const(compiler, TMP_REG2, 0));
2081 		src = TMP_REG2;
2082 	}
2083 	else if (src & SLJIT_MEM) {
2084 		FAIL_IF(emit_op_mem(compiler, WORD_DATA | LOAD_DATA, DR(TMP_REG2), src, srcw));
2085 		src = TMP_REG2;
2086 	}
2087 
2088 	FAIL_IF(push_inst(compiler, JR | S(src), UNMOVABLE_INS));
2089 	if (jump)
2090 		jump->addr = compiler->size;
2091 	FAIL_IF(push_inst(compiler, NOP, UNMOVABLE_INS));
2092 	return SLJIT_SUCCESS;
2093 }
2094 
sljit_emit_op_flags(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 type)2095 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_flags(struct sljit_compiler *compiler, sljit_s32 op,
2096 	sljit_s32 dst, sljit_sw dstw,
2097 	sljit_s32 type)
2098 {
2099 	sljit_s32 src_ar, dst_ar;
2100 	sljit_s32 saved_op = op;
2101 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
2102 	sljit_s32 mem_type = WORD_DATA;
2103 #else
2104 	sljit_s32 mem_type = (op & SLJIT_I32_OP) ? (INT_DATA | SIGNED_DATA) : WORD_DATA;
2105 #endif
2106 
2107 	CHECK_ERROR();
2108 	CHECK(check_sljit_emit_op_flags(compiler, op, dst, dstw, type));
2109 	ADJUST_LOCAL_OFFSET(dst, dstw);
2110 
2111 	op = GET_OPCODE(op);
2112 #if (defined SLJIT_CONFIG_MIPS_64 && SLJIT_CONFIG_MIPS_64)
2113 	if (op == SLJIT_MOV_S32)
2114 		mem_type = INT_DATA | SIGNED_DATA;
2115 #endif
2116 	dst_ar = DR((op < SLJIT_ADD && FAST_IS_REG(dst)) ? dst : TMP_REG2);
2117 
2118 	compiler->cache_arg = 0;
2119 	compiler->cache_argw = 0;
2120 
2121 	if (op >= SLJIT_ADD && (dst & SLJIT_MEM))
2122 		FAIL_IF(emit_op_mem2(compiler, mem_type | LOAD_DATA, DR(TMP_REG1), dst, dstw, dst, dstw));
2123 
2124 	switch (type & 0xff) {
2125 	case SLJIT_EQUAL:
2126 	case SLJIT_NOT_EQUAL:
2127 		FAIL_IF(push_inst(compiler, SLTIU | SA(EQUAL_FLAG) | TA(dst_ar) | IMM(1), dst_ar));
2128 		src_ar = dst_ar;
2129 		break;
2130 	case SLJIT_MUL_OVERFLOW:
2131 	case SLJIT_MUL_NOT_OVERFLOW:
2132 		FAIL_IF(push_inst(compiler, SLTIU | SA(OTHER_FLAG) | TA(dst_ar) | IMM(1), dst_ar));
2133 		src_ar = dst_ar;
2134 		type ^= 0x1; /* Flip type bit for the XORI below. */
2135 		break;
2136 	case SLJIT_GREATER_F64:
2137 	case SLJIT_LESS_EQUAL_F64:
2138 		type ^= 0x1; /* Flip type bit for the XORI below. */
2139 	case SLJIT_EQUAL_F64:
2140 	case SLJIT_NOT_EQUAL_F64:
2141 	case SLJIT_LESS_F64:
2142 	case SLJIT_GREATER_EQUAL_F64:
2143 	case SLJIT_UNORDERED_F64:
2144 	case SLJIT_ORDERED_F64:
2145 #if (defined SLJIT_MIPS_REV && SLJIT_MIPS_REV >= 6)
2146 		FAIL_IF(push_inst(compiler, MFC1 | TA(dst_ar) | FS(TMP_FREG3), dst_ar));
2147 #else /* SLJIT_MIPS_REV < 6 */
2148 		FAIL_IF(push_inst(compiler, CFC1 | TA(dst_ar) | DA(FCSR_REG), dst_ar));
2149 #endif /* SLJIT_MIPS_REV >= 6 */
2150 		FAIL_IF(push_inst(compiler, SRL | TA(dst_ar) | DA(dst_ar) | SH_IMM(23), dst_ar));
2151 		FAIL_IF(push_inst(compiler, ANDI | SA(dst_ar) | TA(dst_ar) | IMM(1), dst_ar));
2152 		src_ar = dst_ar;
2153 		break;
2154 
2155 	default:
2156 		src_ar = OTHER_FLAG;
2157 		break;
2158 	}
2159 
2160 	if (type & 0x1) {
2161 		FAIL_IF(push_inst(compiler, XORI | SA(src_ar) | TA(dst_ar) | IMM(1), dst_ar));
2162 		src_ar = dst_ar;
2163 	}
2164 
2165 	if (op < SLJIT_ADD) {
2166 		if (dst & SLJIT_MEM)
2167 			return emit_op_mem(compiler, mem_type, src_ar, dst, dstw);
2168 
2169 		if (src_ar != dst_ar)
2170 			return push_inst(compiler, ADDU_W | SA(src_ar) | TA(0) | DA(dst_ar), dst_ar);
2171 		return SLJIT_SUCCESS;
2172 	}
2173 
2174 	/* OTHER_FLAG cannot be specified as src2 argument at the moment. */
2175 	if (DR(TMP_REG2) != src_ar)
2176 		FAIL_IF(push_inst(compiler, ADDU_W | SA(src_ar) | TA(0) | D(TMP_REG2), DR(TMP_REG2)));
2177 
2178 	mem_type |= CUMULATIVE_OP | LOGICAL_OP | IMM_OP | ALT_KEEP_CACHE;
2179 
2180 	if (dst & SLJIT_MEM)
2181 		return emit_op(compiler, saved_op, mem_type, dst, dstw, TMP_REG1, 0, TMP_REG2, 0);
2182 	return emit_op(compiler, saved_op, mem_type, dst, dstw, dst, dstw, TMP_REG2, 0);
2183 }
2184 
sljit_emit_cmov(struct sljit_compiler * compiler,sljit_s32 type,sljit_s32 dst_reg,sljit_s32 src,sljit_sw srcw)2185 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_cmov(struct sljit_compiler *compiler, sljit_s32 type,
2186 	sljit_s32 dst_reg,
2187 	sljit_s32 src, sljit_sw srcw)
2188 {
2189 #if (defined SLJIT_MIPS_REV && SLJIT_MIPS_REV >= 1 && SLJIT_MIPS_REV < 6)
2190 	sljit_ins ins;
2191 #endif /* SLJIT_MIPS_REV >= 1 && SLJIT_MIPS_REV < 6 */
2192 
2193 	CHECK_ERROR();
2194 	CHECK(check_sljit_emit_cmov(compiler, type, dst_reg, src, srcw));
2195 
2196 #if (defined SLJIT_MIPS_REV && SLJIT_MIPS_REV >= 1 && SLJIT_MIPS_REV < 6)
2197 
2198 	if (SLJIT_UNLIKELY(src & SLJIT_IMM)) {
2199 #if (defined SLJIT_CONFIG_MIPS_64 && SLJIT_CONFIG_MIPS_64)
2200 		if (dst_reg & SLJIT_I32_OP)
2201 			srcw = (sljit_s32)srcw;
2202 #endif
2203 		FAIL_IF(load_immediate(compiler, DR(TMP_REG1), srcw));
2204 		src = TMP_REG1;
2205 		srcw = 0;
2206 	}
2207 
2208 	dst_reg &= ~SLJIT_I32_OP;
2209 
2210 	switch (type & 0xff) {
2211 	case SLJIT_EQUAL:
2212 		ins = MOVZ | TA(EQUAL_FLAG);
2213 		break;
2214 	case SLJIT_NOT_EQUAL:
2215 		ins = MOVN | TA(EQUAL_FLAG);
2216 		break;
2217 	case SLJIT_LESS:
2218 	case SLJIT_GREATER:
2219 	case SLJIT_SIG_LESS:
2220 	case SLJIT_SIG_GREATER:
2221 	case SLJIT_OVERFLOW:
2222 	case SLJIT_MUL_OVERFLOW:
2223 		ins = MOVN | TA(OTHER_FLAG);
2224 		break;
2225 	case SLJIT_GREATER_EQUAL:
2226 	case SLJIT_LESS_EQUAL:
2227 	case SLJIT_SIG_GREATER_EQUAL:
2228 	case SLJIT_SIG_LESS_EQUAL:
2229 	case SLJIT_NOT_OVERFLOW:
2230 	case SLJIT_MUL_NOT_OVERFLOW:
2231 		ins = MOVZ | TA(OTHER_FLAG);
2232 		break;
2233 	case SLJIT_EQUAL_F64:
2234 	case SLJIT_LESS_F64:
2235 	case SLJIT_LESS_EQUAL_F64:
2236 	case SLJIT_UNORDERED_F64:
2237 		ins = MOVT;
2238 		break;
2239 	case SLJIT_NOT_EQUAL_F64:
2240 	case SLJIT_GREATER_EQUAL_F64:
2241 	case SLJIT_GREATER_F64:
2242 	case SLJIT_ORDERED_F64:
2243 		ins = MOVF;
2244 		break;
2245 	default:
2246 		ins = MOVZ | TA(OTHER_FLAG);
2247 		SLJIT_UNREACHABLE();
2248 		break;
2249 	}
2250 
2251 	return push_inst(compiler, ins | S(src) | D(dst_reg), DR(dst_reg));
2252 
2253 #else /* SLJIT_MIPS_REV < 1 || SLJIT_MIPS_REV >= 6 */
2254 	return sljit_emit_cmov_generic(compiler, type, dst_reg, src, srcw);
2255 #endif /* SLJIT_MIPS_REV >= 1 */
2256 }
2257 
sljit_emit_const(struct sljit_compiler * compiler,sljit_s32 dst,sljit_sw dstw,sljit_sw init_value)2258 SLJIT_API_FUNC_ATTRIBUTE struct sljit_const* sljit_emit_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw, sljit_sw init_value)
2259 {
2260 	struct sljit_const *const_;
2261 	sljit_s32 dst_r;
2262 
2263 	CHECK_ERROR_PTR();
2264 	CHECK_PTR(check_sljit_emit_const(compiler, dst, dstw, init_value));
2265 	ADJUST_LOCAL_OFFSET(dst, dstw);
2266 
2267 	const_ = (struct sljit_const*)ensure_abuf(compiler, sizeof(struct sljit_const));
2268 	PTR_FAIL_IF(!const_);
2269 	set_const(const_, compiler);
2270 
2271 	dst_r = FAST_IS_REG(dst) ? dst : TMP_REG2;
2272 	PTR_FAIL_IF(emit_const(compiler, dst_r, init_value));
2273 
2274 	if (dst & SLJIT_MEM)
2275 		PTR_FAIL_IF(emit_op(compiler, SLJIT_MOV, WORD_DATA, dst, dstw, TMP_REG1, 0, TMP_REG2, 0));
2276 
2277 	return const_;
2278 }
2279 
sljit_emit_put_label(struct sljit_compiler * compiler,sljit_s32 dst,sljit_sw dstw)2280 SLJIT_API_FUNC_ATTRIBUTE struct sljit_put_label* sljit_emit_put_label(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw)
2281 {
2282 	struct sljit_put_label *put_label;
2283 	sljit_s32 dst_r;
2284 
2285 	CHECK_ERROR_PTR();
2286 	CHECK_PTR(check_sljit_emit_put_label(compiler, dst, dstw));
2287 	ADJUST_LOCAL_OFFSET(dst, dstw);
2288 
2289 	put_label = (struct sljit_put_label*)ensure_abuf(compiler, sizeof(struct sljit_put_label));
2290 	PTR_FAIL_IF(!put_label);
2291 	set_put_label(put_label, compiler, 0);
2292 
2293 	dst_r = FAST_IS_REG(dst) ? dst : TMP_REG2;
2294 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
2295 	PTR_FAIL_IF(emit_const(compiler, dst_r, 0));
2296 #else
2297 	PTR_FAIL_IF(push_inst(compiler, dst_r, UNMOVABLE_INS));
2298 	compiler->size += 5;
2299 #endif
2300 
2301 	if (dst & SLJIT_MEM)
2302 		PTR_FAIL_IF(emit_op(compiler, SLJIT_MOV, WORD_DATA, dst, dstw, TMP_REG1, 0, TMP_REG2, 0));
2303 
2304 	return put_label;
2305 }
2306