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 /* mips 64-bit arch dependent functions. */
28 
load_immediate(struct sljit_compiler * compiler,sljit_s32 dst_ar,sljit_sw imm)29 static sljit_s32 load_immediate(struct sljit_compiler *compiler, sljit_s32 dst_ar, sljit_sw imm)
30 {
31 	sljit_s32 shift = 32;
32 	sljit_s32 shift2;
33 	sljit_s32 inv = 0;
34 	sljit_ins ins;
35 	sljit_uw uimm;
36 
37 	if (!(imm & ~0xffff))
38 		return push_inst(compiler, ORI | SA(0) | TA(dst_ar) | IMM(imm), dst_ar);
39 
40 	if (imm < 0 && imm >= SIMM_MIN)
41 		return push_inst(compiler, ADDIU | SA(0) | TA(dst_ar) | IMM(imm), dst_ar);
42 
43 	if (imm <= 0x7fffffffl && imm >= -0x80000000l) {
44 		FAIL_IF(push_inst(compiler, LUI | TA(dst_ar) | IMM(imm >> 16), dst_ar));
45 		return (imm & 0xffff) ? push_inst(compiler, ORI | SA(dst_ar) | TA(dst_ar) | IMM(imm), dst_ar) : SLJIT_SUCCESS;
46 	}
47 
48 	/* Zero extended number. */
49 	uimm = (sljit_uw)imm;
50 	if (imm < 0) {
51 		uimm = ~(sljit_uw)imm;
52 		inv = 1;
53 	}
54 
55 	while (!(uimm & 0xff00000000000000l)) {
56 		shift -= 8;
57 		uimm <<= 8;
58 	}
59 
60 	if (!(uimm & 0xf000000000000000l)) {
61 		shift -= 4;
62 		uimm <<= 4;
63 	}
64 
65 	if (!(uimm & 0xc000000000000000l)) {
66 		shift -= 2;
67 		uimm <<= 2;
68 	}
69 
70 	if ((sljit_sw)uimm < 0) {
71 		uimm >>= 1;
72 		shift += 1;
73 	}
74 	SLJIT_ASSERT(((uimm & 0xc000000000000000l) == 0x4000000000000000l) && (shift > 0) && (shift <= 32));
75 
76 	if (inv)
77 		uimm = ~uimm;
78 
79 	FAIL_IF(push_inst(compiler, LUI | TA(dst_ar) | IMM(uimm >> 48), dst_ar));
80 	if (uimm & 0x0000ffff00000000l)
81 		FAIL_IF(push_inst(compiler, ORI | SA(dst_ar) | TA(dst_ar) | IMM(uimm >> 32), dst_ar));
82 
83 	imm &= (1l << shift) - 1;
84 	if (!(imm & ~0xffff)) {
85 		ins = (shift == 32) ? DSLL32 : DSLL;
86 		if (shift < 32)
87 			ins |= SH_IMM(shift);
88 		FAIL_IF(push_inst(compiler, ins | TA(dst_ar) | DA(dst_ar), dst_ar));
89 		return !(imm & 0xffff) ? SLJIT_SUCCESS : push_inst(compiler, ORI | SA(dst_ar) | TA(dst_ar) | IMM(imm), dst_ar);
90 	}
91 
92 	/* Double shifts needs to be performed. */
93 	uimm <<= 32;
94 	shift2 = shift - 16;
95 
96 	while (!(uimm & 0xf000000000000000l)) {
97 		shift2 -= 4;
98 		uimm <<= 4;
99 	}
100 
101 	if (!(uimm & 0xc000000000000000l)) {
102 		shift2 -= 2;
103 		uimm <<= 2;
104 	}
105 
106 	if (!(uimm & 0x8000000000000000l)) {
107 		shift2--;
108 		uimm <<= 1;
109 	}
110 
111 	SLJIT_ASSERT((uimm & 0x8000000000000000l) && (shift2 > 0) && (shift2 <= 16));
112 
113 	FAIL_IF(push_inst(compiler, DSLL | TA(dst_ar) | DA(dst_ar) | SH_IMM(shift - shift2), dst_ar));
114 	FAIL_IF(push_inst(compiler, ORI | SA(dst_ar) | TA(dst_ar) | IMM(uimm >> 48), dst_ar));
115 	FAIL_IF(push_inst(compiler, DSLL | TA(dst_ar) | DA(dst_ar) | SH_IMM(shift2), dst_ar));
116 
117 	imm &= (1l << shift2) - 1;
118 	return !(imm & 0xffff) ? SLJIT_SUCCESS : push_inst(compiler, ORI | SA(dst_ar) | TA(dst_ar) | IMM(imm), dst_ar);
119 }
120 
121 #define SELECT_OP(a, b) \
122 	(!(op & SLJIT_32) ? a : b)
123 
124 #define EMIT_LOGICAL(op_imm, op_norm) \
125 	if (flags & SRC2_IMM) { \
126 		if (op & SLJIT_SET_Z) \
127 			FAIL_IF(push_inst(compiler, op_imm | S(src1) | TA(EQUAL_FLAG) | IMM(src2), EQUAL_FLAG)); \
128 		if (!(flags & UNUSED_DEST)) \
129 			FAIL_IF(push_inst(compiler, op_imm | S(src1) | T(dst) | IMM(src2), DR(dst))); \
130 	} \
131 	else { \
132 		if (op & SLJIT_SET_Z) \
133 			FAIL_IF(push_inst(compiler, op_norm | S(src1) | T(src2) | DA(EQUAL_FLAG), EQUAL_FLAG)); \
134 		if (!(flags & UNUSED_DEST)) \
135 			FAIL_IF(push_inst(compiler, op_norm | S(src1) | T(src2) | D(dst), DR(dst))); \
136 	}
137 
138 #define EMIT_SHIFT(op_dimm, op_dimm32, op_imm, op_dv, op_v) \
139 	if (flags & SRC2_IMM) { \
140 		if (src2 >= 32) { \
141 			SLJIT_ASSERT(!(op & SLJIT_32)); \
142 			ins = op_dimm32; \
143 			src2 -= 32; \
144 		} \
145 		else \
146 			ins = (op & SLJIT_32) ? op_imm : op_dimm; \
147 		if (op & SLJIT_SET_Z) \
148 			FAIL_IF(push_inst(compiler, ins | T(src1) | DA(EQUAL_FLAG) | SH_IMM(src2), EQUAL_FLAG)); \
149 		if (!(flags & UNUSED_DEST)) \
150 			FAIL_IF(push_inst(compiler, ins | T(src1) | D(dst) | SH_IMM(src2), DR(dst))); \
151 	} \
152 	else { \
153 		ins = (op & SLJIT_32) ? op_v : op_dv; \
154 		if (op & SLJIT_SET_Z) \
155 			FAIL_IF(push_inst(compiler, ins | S(src2) | T(src1) | DA(EQUAL_FLAG), EQUAL_FLAG)); \
156 		if (!(flags & UNUSED_DEST)) \
157 			FAIL_IF(push_inst(compiler, ins | S(src2) | T(src1) | D(dst), DR(dst))); \
158 	}
159 
emit_single_op(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 flags,sljit_s32 dst,sljit_s32 src1,sljit_sw src2)160 static SLJIT_INLINE sljit_s32 emit_single_op(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 flags,
161 	sljit_s32 dst, sljit_s32 src1, sljit_sw src2)
162 {
163 	sljit_ins ins;
164 	sljit_s32 is_overflow, is_carry, is_handled;
165 
166 	switch (GET_OPCODE(op)) {
167 	case SLJIT_MOV:
168 		SLJIT_ASSERT(src1 == TMP_REG1 && !(flags & SRC2_IMM));
169 		if (dst != src2)
170 			return push_inst(compiler, SELECT_OP(DADDU, ADDU) | S(src2) | TA(0) | D(dst), DR(dst));
171 		return SLJIT_SUCCESS;
172 
173 	case SLJIT_MOV_U8:
174 		SLJIT_ASSERT(src1 == TMP_REG1 && !(flags & SRC2_IMM));
175 		if ((flags & (REG_DEST | REG2_SOURCE)) == (REG_DEST | REG2_SOURCE))
176 			return push_inst(compiler, ANDI | S(src2) | T(dst) | IMM(0xff), DR(dst));
177 		SLJIT_ASSERT(dst == src2);
178 		return SLJIT_SUCCESS;
179 
180 	case SLJIT_MOV_S8:
181 		SLJIT_ASSERT(src1 == TMP_REG1 && !(flags & SRC2_IMM));
182 		if ((flags & (REG_DEST | REG2_SOURCE)) == (REG_DEST | REG2_SOURCE)) {
183 #if (defined SLJIT_MIPS_REV && SLJIT_MIPS_REV >= 1)
184 			if (op & SLJIT_32)
185 				return push_inst(compiler, SEB | T(src2) | D(dst), DR(dst));
186 #endif /* SLJIT_MIPS_REV >= 1 */
187 			FAIL_IF(push_inst(compiler, DSLL32 | T(src2) | D(dst) | SH_IMM(24), DR(dst)));
188 			return push_inst(compiler, DSRA32 | T(dst) | D(dst) | SH_IMM(24), DR(dst));
189 		}
190 		SLJIT_ASSERT(dst == src2);
191 		return SLJIT_SUCCESS;
192 
193 	case SLJIT_MOV_U16:
194 		SLJIT_ASSERT(src1 == TMP_REG1 && !(flags & SRC2_IMM));
195 		if ((flags & (REG_DEST | REG2_SOURCE)) == (REG_DEST | REG2_SOURCE))
196 			return push_inst(compiler, ANDI | S(src2) | T(dst) | IMM(0xffff), DR(dst));
197 		SLJIT_ASSERT(dst == src2);
198 		return SLJIT_SUCCESS;
199 
200 	case SLJIT_MOV_S16:
201 		SLJIT_ASSERT(src1 == TMP_REG1 && !(flags & SRC2_IMM));
202 		if ((flags & (REG_DEST | REG2_SOURCE)) == (REG_DEST | REG2_SOURCE)) {
203 #if (defined SLJIT_MIPS_REV && SLJIT_MIPS_REV >= 1)
204 			if (op & SLJIT_32)
205 				return push_inst(compiler, SEH | T(src2) | D(dst), DR(dst));
206 #endif /* SLJIT_MIPS_REV >= 1 */
207 			FAIL_IF(push_inst(compiler, DSLL32 | T(src2) | D(dst) | SH_IMM(16), DR(dst)));
208 			return push_inst(compiler, DSRA32 | T(dst) | D(dst) | SH_IMM(16), DR(dst));
209 		}
210 		SLJIT_ASSERT(dst == src2);
211 		return SLJIT_SUCCESS;
212 
213 	case SLJIT_MOV_U32:
214 		SLJIT_ASSERT(src1 == TMP_REG1 && !(flags & SRC2_IMM) && !(op & SLJIT_32));
215 		if ((flags & (REG_DEST | REG2_SOURCE)) == (REG_DEST | REG2_SOURCE)) {
216 #if (defined SLJIT_MIPS_REV && SLJIT_MIPS_REV >= 2)
217 			if (dst == src2)
218 				return push_inst(compiler, DINSU | T(src2) | SA(0) | (31 << 11) | (0 << 11), DR(dst));
219 #endif /* SLJIT_MIPS_REV >= 2 */
220 			FAIL_IF(push_inst(compiler, DSLL32 | T(src2) | D(dst) | SH_IMM(0), DR(dst)));
221 			return push_inst(compiler, DSRL32 | T(dst) | D(dst) | SH_IMM(0), DR(dst));
222 		}
223 		SLJIT_ASSERT(dst == src2);
224 		return SLJIT_SUCCESS;
225 
226 	case SLJIT_MOV_S32:
227 		SLJIT_ASSERT(src1 == TMP_REG1 && !(flags & SRC2_IMM) && !(op & SLJIT_32));
228 		if ((flags & (REG_DEST | REG2_SOURCE)) == (REG_DEST | REG2_SOURCE)) {
229 			return push_inst(compiler, SLL | T(src2) | D(dst) | SH_IMM(0), DR(dst));
230 		}
231 		SLJIT_ASSERT(dst == src2);
232 		return SLJIT_SUCCESS;
233 
234 	case SLJIT_NOT:
235 		SLJIT_ASSERT(src1 == TMP_REG1 && !(flags & SRC2_IMM));
236 		if (op & SLJIT_SET_Z)
237 			FAIL_IF(push_inst(compiler, NOR | S(src2) | T(src2) | DA(EQUAL_FLAG), EQUAL_FLAG));
238 		if (!(flags & UNUSED_DEST))
239 			FAIL_IF(push_inst(compiler, NOR | S(src2) | T(src2) | D(dst), DR(dst)));
240 		return SLJIT_SUCCESS;
241 
242 	case SLJIT_CLZ:
243 		SLJIT_ASSERT(src1 == TMP_REG1 && !(flags & SRC2_IMM));
244 #if (defined SLJIT_MIPS_REV && SLJIT_MIPS_REV >= 1)
245 		if (op & SLJIT_SET_Z)
246 			FAIL_IF(push_inst(compiler, SELECT_OP(DCLZ, CLZ) | S(src2) | TA(EQUAL_FLAG) | DA(EQUAL_FLAG), EQUAL_FLAG));
247 		if (!(flags & UNUSED_DEST))
248 			FAIL_IF(push_inst(compiler, SELECT_OP(DCLZ, CLZ) | S(src2) | T(dst) | D(dst), DR(dst)));
249 #else /* SLJIT_MIPS_REV < 1 */
250 		if (SLJIT_UNLIKELY(flags & UNUSED_DEST)) {
251 			FAIL_IF(push_inst(compiler, SELECT_OP(DSRL32, SRL) | T(src2) | DA(EQUAL_FLAG) | SH_IMM(31), EQUAL_FLAG));
252 			return push_inst(compiler, XORI | SA(EQUAL_FLAG) | TA(EQUAL_FLAG) | IMM(1), EQUAL_FLAG);
253 		}
254 		/* Nearly all instructions are unmovable in the following sequence. */
255 		FAIL_IF(push_inst(compiler, SELECT_OP(DADDU, ADDU) | S(src2) | TA(0) | D(TMP_REG1), DR(TMP_REG1)));
256 		/* Check zero. */
257 		FAIL_IF(push_inst(compiler, BEQ | S(TMP_REG1) | TA(0) | IMM(5), UNMOVABLE_INS));
258 		FAIL_IF(push_inst(compiler, ORI | SA(0) | T(dst) | IMM((op & SLJIT_32) ? 32 : 64), UNMOVABLE_INS));
259 		FAIL_IF(push_inst(compiler, SELECT_OP(DADDIU, ADDIU) | SA(0) | T(dst) | IMM(-1), DR(dst)));
260 		/* Loop for searching the highest bit. */
261 		FAIL_IF(push_inst(compiler, SELECT_OP(DADDIU, ADDIU) | S(dst) | T(dst) | IMM(1), DR(dst)));
262 		FAIL_IF(push_inst(compiler, BGEZ | S(TMP_REG1) | IMM(-2), UNMOVABLE_INS));
263 		FAIL_IF(push_inst(compiler, SELECT_OP(DSLL, SLL) | T(TMP_REG1) | D(TMP_REG1) | SH_IMM(1), UNMOVABLE_INS));
264 #endif /* SLJIT_MIPS_REV >= 1 */
265 		return SLJIT_SUCCESS;
266 
267 	case SLJIT_ADD:
268 		is_overflow = GET_FLAG_TYPE(op) == SLJIT_OVERFLOW;
269 		is_carry = GET_FLAG_TYPE(op) == GET_FLAG_TYPE(SLJIT_SET_CARRY);
270 
271 		if (flags & SRC2_IMM) {
272 			if (is_overflow) {
273 				if (src2 >= 0)
274 					FAIL_IF(push_inst(compiler, OR | S(src1) | T(src1) | DA(EQUAL_FLAG), EQUAL_FLAG));
275 				else
276 					FAIL_IF(push_inst(compiler, NOR | S(src1) | T(src1) | DA(EQUAL_FLAG), EQUAL_FLAG));
277 			}
278 			else if (op & SLJIT_SET_Z)
279 				FAIL_IF(push_inst(compiler, SELECT_OP(DADDIU, ADDIU) | S(src1) | TA(EQUAL_FLAG) | IMM(src2), EQUAL_FLAG));
280 
281 			if (is_overflow || is_carry) {
282 				if (src2 >= 0)
283 					FAIL_IF(push_inst(compiler, ORI | S(src1) | TA(OTHER_FLAG) | IMM(src2), OTHER_FLAG));
284 				else {
285 					FAIL_IF(push_inst(compiler, SELECT_OP(DADDIU, ADDIU) | SA(0) | TA(OTHER_FLAG) | IMM(src2), OTHER_FLAG));
286 					FAIL_IF(push_inst(compiler, OR | S(src1) | TA(OTHER_FLAG) | DA(OTHER_FLAG), OTHER_FLAG));
287 				}
288 			}
289 			/* dst may be the same as src1 or src2. */
290 			if (!(flags & UNUSED_DEST) || (op & VARIABLE_FLAG_MASK))
291 				FAIL_IF(push_inst(compiler, SELECT_OP(DADDIU, ADDIU) | S(src1) | T(dst) | IMM(src2), DR(dst)));
292 		}
293 		else {
294 			if (is_overflow)
295 				FAIL_IF(push_inst(compiler, XOR | S(src1) | T(src2) | DA(EQUAL_FLAG), EQUAL_FLAG));
296 			else if (op & SLJIT_SET_Z)
297 				FAIL_IF(push_inst(compiler, SELECT_OP(DADDU, ADDU) | S(src1) | T(src2) | DA(EQUAL_FLAG), EQUAL_FLAG));
298 
299 			if (is_overflow || is_carry)
300 				FAIL_IF(push_inst(compiler, OR | S(src1) | T(src2) | DA(OTHER_FLAG), OTHER_FLAG));
301 			/* dst may be the same as src1 or src2. */
302 			if (!(flags & UNUSED_DEST) || (op & VARIABLE_FLAG_MASK))
303 				FAIL_IF(push_inst(compiler, SELECT_OP(DADDU, ADDU) | S(src1) | T(src2) | D(dst), DR(dst)));
304 		}
305 
306 		/* a + b >= a | b (otherwise, the carry should be set to 1). */
307 		if (is_overflow || is_carry)
308 			FAIL_IF(push_inst(compiler, SLTU | S(dst) | TA(OTHER_FLAG) | DA(OTHER_FLAG), OTHER_FLAG));
309 		if (!is_overflow)
310 			return SLJIT_SUCCESS;
311 		FAIL_IF(push_inst(compiler, SELECT_OP(DSLL32, SLL) | TA(OTHER_FLAG) | D(TMP_REG1) | SH_IMM(31), DR(TMP_REG1)));
312 		FAIL_IF(push_inst(compiler, XOR | S(TMP_REG1) | TA(EQUAL_FLAG) | DA(EQUAL_FLAG), EQUAL_FLAG));
313 		FAIL_IF(push_inst(compiler, XOR | S(dst) | TA(EQUAL_FLAG) | DA(OTHER_FLAG), OTHER_FLAG));
314 		if (op & SLJIT_SET_Z)
315 			FAIL_IF(push_inst(compiler, SELECT_OP(DADDU, ADDU) | S(dst) | TA(0) | DA(EQUAL_FLAG), EQUAL_FLAG));
316 		return push_inst(compiler, SELECT_OP(DSRL32, SRL) | TA(OTHER_FLAG) | DA(OTHER_FLAG) | SH_IMM(31), OTHER_FLAG);
317 
318 	case SLJIT_ADDC:
319 		is_carry = GET_FLAG_TYPE(op) == GET_FLAG_TYPE(SLJIT_SET_CARRY);
320 
321 		if (flags & SRC2_IMM) {
322 			if (is_carry) {
323 				if (src2 >= 0)
324 					FAIL_IF(push_inst(compiler, ORI | S(src1) | TA(EQUAL_FLAG) | IMM(src2), EQUAL_FLAG));
325 				else {
326 					FAIL_IF(push_inst(compiler, SELECT_OP(DADDIU, ADDIU) | SA(0) | TA(EQUAL_FLAG) | IMM(src2), EQUAL_FLAG));
327 					FAIL_IF(push_inst(compiler, OR | S(src1) | TA(EQUAL_FLAG) | DA(EQUAL_FLAG), EQUAL_FLAG));
328 				}
329 			}
330 			FAIL_IF(push_inst(compiler, SELECT_OP(DADDIU, ADDIU) | S(src1) | T(dst) | IMM(src2), DR(dst)));
331 		} else {
332 			if (is_carry)
333 				FAIL_IF(push_inst(compiler, OR | S(src1) | T(src2) | DA(EQUAL_FLAG), EQUAL_FLAG));
334 			/* dst may be the same as src1 or src2. */
335 			FAIL_IF(push_inst(compiler, SELECT_OP(DADDU, ADDU) | S(src1) | T(src2) | D(dst), DR(dst)));
336 		}
337 		if (is_carry)
338 			FAIL_IF(push_inst(compiler, SLTU | S(dst) | TA(EQUAL_FLAG) | DA(EQUAL_FLAG), EQUAL_FLAG));
339 
340 		FAIL_IF(push_inst(compiler, SELECT_OP(DADDU, ADDU) | S(dst) | TA(OTHER_FLAG) | D(dst), DR(dst)));
341 		if (!is_carry)
342 			return SLJIT_SUCCESS;
343 
344 		/* Set ULESS_FLAG (dst == 0) && (OTHER_FLAG == 1). */
345 		FAIL_IF(push_inst(compiler, SLTU | S(dst) | TA(OTHER_FLAG) | DA(OTHER_FLAG), OTHER_FLAG));
346 		/* Set carry flag. */
347 		return push_inst(compiler, OR | SA(OTHER_FLAG) | TA(EQUAL_FLAG) | DA(OTHER_FLAG), OTHER_FLAG);
348 
349 	case SLJIT_SUB:
350 		if ((flags & SRC2_IMM) && src2 == SIMM_MIN) {
351 			FAIL_IF(push_inst(compiler, ADDIU | SA(0) | T(TMP_REG2) | IMM(src2), DR(TMP_REG2)));
352 			src2 = TMP_REG2;
353 			flags &= ~SRC2_IMM;
354 		}
355 
356 		is_handled = 0;
357 
358 		if (flags & SRC2_IMM) {
359 			if (GET_FLAG_TYPE(op) == SLJIT_LESS || GET_FLAG_TYPE(op) == SLJIT_GREATER_EQUAL) {
360 				FAIL_IF(push_inst(compiler, SLTIU | S(src1) | TA(OTHER_FLAG) | IMM(src2), OTHER_FLAG));
361 				is_handled = 1;
362 			}
363 			else if (GET_FLAG_TYPE(op) == SLJIT_SIG_LESS || GET_FLAG_TYPE(op) == SLJIT_SIG_GREATER_EQUAL) {
364 				FAIL_IF(push_inst(compiler, SLTI | S(src1) | TA(OTHER_FLAG) | IMM(src2), OTHER_FLAG));
365 				is_handled = 1;
366 			}
367 		}
368 
369 		if (!is_handled && GET_FLAG_TYPE(op) >= SLJIT_LESS && GET_FLAG_TYPE(op) <= SLJIT_SIG_LESS_EQUAL) {
370 			is_handled = 1;
371 
372 			if (flags & SRC2_IMM) {
373 				FAIL_IF(push_inst(compiler, ADDIU | SA(0) | T(TMP_REG2) | IMM(src2), DR(TMP_REG2)));
374 				src2 = TMP_REG2;
375 				flags &= ~SRC2_IMM;
376 			}
377 
378 			if (GET_FLAG_TYPE(op) == SLJIT_LESS || GET_FLAG_TYPE(op) == SLJIT_GREATER_EQUAL) {
379 				FAIL_IF(push_inst(compiler, SLTU | S(src1) | T(src2) | DA(OTHER_FLAG), OTHER_FLAG));
380 			}
381 			else if (GET_FLAG_TYPE(op) == SLJIT_GREATER || GET_FLAG_TYPE(op) == SLJIT_LESS_EQUAL)
382 			{
383 				FAIL_IF(push_inst(compiler, SLTU | S(src2) | T(src1) | DA(OTHER_FLAG), OTHER_FLAG));
384 			}
385 			else if (GET_FLAG_TYPE(op) == SLJIT_SIG_LESS || GET_FLAG_TYPE(op) == SLJIT_SIG_GREATER_EQUAL) {
386 				FAIL_IF(push_inst(compiler, SLT | S(src1) | T(src2) | DA(OTHER_FLAG), OTHER_FLAG));
387 			}
388 			else if (GET_FLAG_TYPE(op) == SLJIT_SIG_GREATER || GET_FLAG_TYPE(op) == SLJIT_SIG_LESS_EQUAL)
389 			{
390 				FAIL_IF(push_inst(compiler, SLT | S(src2) | T(src1) | DA(OTHER_FLAG), OTHER_FLAG));
391 			}
392 		}
393 
394 		if (is_handled) {
395 			if (flags & SRC2_IMM) {
396 				if (op & SLJIT_SET_Z)
397 					FAIL_IF(push_inst(compiler, SELECT_OP(DADDIU, ADDIU) | S(src1) | TA(EQUAL_FLAG) | IMM(-src2), EQUAL_FLAG));
398 				if (!(flags & UNUSED_DEST))
399 					return push_inst(compiler, SELECT_OP(DADDIU, ADDIU) | S(src1) | T(dst) | IMM(-src2), DR(dst));
400 			}
401 			else {
402 				if (op & SLJIT_SET_Z)
403 					FAIL_IF(push_inst(compiler, SELECT_OP(DSUBU, SUBU) | S(src1) | T(src2) | DA(EQUAL_FLAG), EQUAL_FLAG));
404 				if (!(flags & UNUSED_DEST))
405 					return push_inst(compiler, SELECT_OP(DSUBU, SUBU) | S(src1) | T(src2) | D(dst), DR(dst));
406 			}
407 			return SLJIT_SUCCESS;
408 		}
409 
410 		is_overflow = GET_FLAG_TYPE(op) == SLJIT_OVERFLOW;
411 		is_carry = GET_FLAG_TYPE(op) == GET_FLAG_TYPE(SLJIT_SET_CARRY);
412 
413 		if (flags & SRC2_IMM) {
414 			if (is_overflow) {
415 				if (src2 >= 0)
416 					FAIL_IF(push_inst(compiler, OR | S(src1) | T(src1) | DA(EQUAL_FLAG), EQUAL_FLAG));
417 				else
418 					FAIL_IF(push_inst(compiler, NOR | S(src1) | T(src1) | DA(EQUAL_FLAG), EQUAL_FLAG));
419 			}
420 			else if (op & SLJIT_SET_Z)
421 				FAIL_IF(push_inst(compiler, SELECT_OP(DADDIU, ADDIU) | S(src1) | TA(EQUAL_FLAG) | IMM(-src2), EQUAL_FLAG));
422 
423 			if (is_overflow || is_carry)
424 				FAIL_IF(push_inst(compiler, SLTIU | S(src1) | TA(OTHER_FLAG) | IMM(src2), OTHER_FLAG));
425 			/* dst may be the same as src1 or src2. */
426 			if (!(flags & UNUSED_DEST) || (op & VARIABLE_FLAG_MASK))
427 				FAIL_IF(push_inst(compiler, SELECT_OP(DADDIU, ADDIU) | S(src1) | T(dst) | IMM(-src2), DR(dst)));
428 		}
429 		else {
430 			if (is_overflow)
431 				FAIL_IF(push_inst(compiler, XOR | S(src1) | T(src2) | DA(EQUAL_FLAG), EQUAL_FLAG));
432 			else if (op & SLJIT_SET_Z)
433 				FAIL_IF(push_inst(compiler, SELECT_OP(DSUBU, SUBU) | S(src1) | T(src2) | DA(EQUAL_FLAG), EQUAL_FLAG));
434 
435 			if (is_overflow || is_carry)
436 				FAIL_IF(push_inst(compiler, SLTU | S(src1) | T(src2) | DA(OTHER_FLAG), OTHER_FLAG));
437 			/* dst may be the same as src1 or src2. */
438 			if (!(flags & UNUSED_DEST) || (op & VARIABLE_FLAG_MASK))
439 				FAIL_IF(push_inst(compiler, SELECT_OP(DSUBU, SUBU) | S(src1) | T(src2) | D(dst), DR(dst)));
440 		}
441 
442 		if (!is_overflow)
443 			return SLJIT_SUCCESS;
444 		FAIL_IF(push_inst(compiler, SELECT_OP(DSLL32, SLL) | TA(OTHER_FLAG) | D(TMP_REG1) | SH_IMM(31), DR(TMP_REG1)));
445 		FAIL_IF(push_inst(compiler, XOR | S(TMP_REG1) | TA(EQUAL_FLAG) | DA(EQUAL_FLAG), EQUAL_FLAG));
446 		FAIL_IF(push_inst(compiler, XOR | S(dst) | TA(EQUAL_FLAG) | DA(OTHER_FLAG), OTHER_FLAG));
447 		if (op & SLJIT_SET_Z)
448 			FAIL_IF(push_inst(compiler, SELECT_OP(DADDU, ADDU) | S(dst) | TA(0) | DA(EQUAL_FLAG), EQUAL_FLAG));
449 		return push_inst(compiler, SELECT_OP(DSRL32, SRL) | TA(OTHER_FLAG) | DA(OTHER_FLAG) | SH_IMM(31), OTHER_FLAG);
450 
451 	case SLJIT_SUBC:
452 		if ((flags & SRC2_IMM) && src2 == SIMM_MIN) {
453 			FAIL_IF(push_inst(compiler, ADDIU | SA(0) | T(TMP_REG2) | IMM(src2), DR(TMP_REG2)));
454 			src2 = TMP_REG2;
455 			flags &= ~SRC2_IMM;
456 		}
457 
458 		is_carry = GET_FLAG_TYPE(op) == GET_FLAG_TYPE(SLJIT_SET_CARRY);
459 
460 		if (flags & SRC2_IMM) {
461 			if (is_carry)
462 				FAIL_IF(push_inst(compiler, SLTIU | S(src1) | TA(EQUAL_FLAG) | IMM(src2), EQUAL_FLAG));
463 			/* dst may be the same as src1 or src2. */
464 			FAIL_IF(push_inst(compiler, SELECT_OP(DADDIU, ADDIU) | S(src1) | T(dst) | IMM(-src2), DR(dst)));
465 		}
466 		else {
467 			if (is_carry)
468 				FAIL_IF(push_inst(compiler, SLTU | S(src1) | T(src2) | DA(EQUAL_FLAG), EQUAL_FLAG));
469 			/* dst may be the same as src1 or src2. */
470 			FAIL_IF(push_inst(compiler, SELECT_OP(DSUBU, SUBU) | S(src1) | T(src2) | D(dst), DR(dst)));
471 		}
472 
473 		if (is_carry)
474 			FAIL_IF(push_inst(compiler, SLTU | S(dst) | TA(OTHER_FLAG) | D(TMP_REG1), DR(TMP_REG1)));
475 
476 		FAIL_IF(push_inst(compiler, SELECT_OP(DSUBU, SUBU) | S(dst) | TA(OTHER_FLAG) | D(dst), DR(dst)));
477 		return (is_carry) ? push_inst(compiler, OR | SA(EQUAL_FLAG) | T(TMP_REG1) | DA(OTHER_FLAG), OTHER_FLAG) : SLJIT_SUCCESS;
478 
479 	case SLJIT_MUL:
480 		SLJIT_ASSERT(!(flags & SRC2_IMM));
481 
482 		if (GET_FLAG_TYPE(op) != SLJIT_OVERFLOW) {
483 #if (defined SLJIT_MIPS_REV && SLJIT_MIPS_REV >= 6)
484 			return push_inst(compiler, SELECT_OP(DMUL, MUL) | S(src1) | T(src2) | D(dst), DR(dst));
485 #elif (defined SLJIT_MIPS_REV && SLJIT_MIPS_REV >= 1)
486 			if (op & SLJIT_32)
487 				return push_inst(compiler, MUL | S(src1) | T(src2) | D(dst), DR(dst));
488 			FAIL_IF(push_inst(compiler, DMULT | S(src1) | T(src2), MOVABLE_INS));
489 			return push_inst(compiler, MFLO | D(dst), DR(dst));
490 #else /* SLJIT_MIPS_REV < 1 */
491 			FAIL_IF(push_inst(compiler, SELECT_OP(DMULT, MULT) | S(src1) | T(src2), MOVABLE_INS));
492 			return push_inst(compiler, MFLO | D(dst), DR(dst));
493 #endif /* SLJIT_MIPS_REV >= 6 */
494 		}
495 
496 #if (defined SLJIT_MIPS_REV && SLJIT_MIPS_REV >= 6)
497 		FAIL_IF(push_inst(compiler, SELECT_OP(DMUL, MUL) | S(src1) | T(src2) | D(dst), DR(dst)));
498 		FAIL_IF(push_inst(compiler, SELECT_OP(DMUH, MUH) | S(src1) | T(src2) | DA(EQUAL_FLAG), EQUAL_FLAG));
499 #else /* SLJIT_MIPS_REV < 6 */
500 		FAIL_IF(push_inst(compiler, SELECT_OP(DMULT, MULT) | S(src1) | T(src2), MOVABLE_INS));
501 		FAIL_IF(push_inst(compiler, MFHI | DA(EQUAL_FLAG), EQUAL_FLAG));
502 		FAIL_IF(push_inst(compiler, MFLO | D(dst), DR(dst)));
503 #endif /* SLJIT_MIPS_REV >= 6 */
504 		FAIL_IF(push_inst(compiler, SELECT_OP(DSRA32, SRA) | T(dst) | DA(OTHER_FLAG) | SH_IMM(31), OTHER_FLAG));
505 		return push_inst(compiler, SELECT_OP(DSUBU, SUBU) | SA(EQUAL_FLAG) | TA(OTHER_FLAG) | DA(OTHER_FLAG), OTHER_FLAG);
506 
507 	case SLJIT_AND:
508 		EMIT_LOGICAL(ANDI, AND);
509 		return SLJIT_SUCCESS;
510 
511 	case SLJIT_OR:
512 		EMIT_LOGICAL(ORI, OR);
513 		return SLJIT_SUCCESS;
514 
515 	case SLJIT_XOR:
516 		EMIT_LOGICAL(XORI, XOR);
517 		return SLJIT_SUCCESS;
518 
519 	case SLJIT_SHL:
520 		EMIT_SHIFT(DSLL, DSLL32, SLL, DSLLV, SLLV);
521 		return SLJIT_SUCCESS;
522 
523 	case SLJIT_LSHR:
524 		EMIT_SHIFT(DSRL, DSRL32, SRL, DSRLV, SRLV);
525 		return SLJIT_SUCCESS;
526 
527 	case SLJIT_ASHR:
528 		EMIT_SHIFT(DSRA, DSRA32, SRA, DSRAV, SRAV);
529 		return SLJIT_SUCCESS;
530 	}
531 
532 	SLJIT_UNREACHABLE();
533 	return SLJIT_SUCCESS;
534 }
535 
emit_const(struct sljit_compiler * compiler,sljit_s32 dst,sljit_sw init_value)536 static SLJIT_INLINE sljit_s32 emit_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw init_value)
537 {
538 	FAIL_IF(push_inst(compiler, LUI | T(dst) | IMM(init_value >> 48), DR(dst)));
539 	FAIL_IF(push_inst(compiler, ORI | S(dst) | T(dst) | IMM(init_value >> 32), DR(dst)));
540 	FAIL_IF(push_inst(compiler, DSLL | T(dst) | D(dst) | SH_IMM(16), DR(dst)));
541 	FAIL_IF(push_inst(compiler, ORI | S(dst) | T(dst) | IMM(init_value >> 16), DR(dst)));
542 	FAIL_IF(push_inst(compiler, DSLL | T(dst) | D(dst) | SH_IMM(16), DR(dst)));
543 	return push_inst(compiler, ORI | S(dst) | T(dst) | IMM(init_value), DR(dst));
544 }
545 
sljit_set_jump_addr(sljit_uw addr,sljit_uw new_target,sljit_sw executable_offset)546 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_target, sljit_sw executable_offset)
547 {
548 	sljit_ins *inst = (sljit_ins *)addr;
549 	SLJIT_UNUSED_ARG(executable_offset);
550 
551 	SLJIT_UPDATE_WX_FLAGS(inst, inst + 6, 0);
552 	inst[0] = (inst[0] & 0xffff0000) | ((sljit_ins)(new_target >> 48) & 0xffff);
553 	inst[1] = (inst[1] & 0xffff0000) | ((sljit_ins)(new_target >> 32) & 0xffff);
554 	inst[3] = (inst[3] & 0xffff0000) | ((sljit_ins)(new_target >> 16) & 0xffff);
555 	inst[5] = (inst[5] & 0xffff0000) | ((sljit_ins)new_target & 0xffff);
556 	SLJIT_UPDATE_WX_FLAGS(inst, inst + 6, 1);
557 	inst = (sljit_ins *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
558 	SLJIT_CACHE_FLUSH(inst, inst + 6);
559 }
560 
sljit_set_const(sljit_uw addr,sljit_sw new_constant,sljit_sw executable_offset)561 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_const(sljit_uw addr, sljit_sw new_constant, sljit_sw executable_offset)
562 {
563 	sljit_set_jump_addr(addr, (sljit_uw)new_constant, executable_offset);
564 }
565 
call_with_args(struct sljit_compiler * compiler,sljit_s32 arg_types,sljit_ins * ins_ptr)566 static sljit_s32 call_with_args(struct sljit_compiler *compiler, sljit_s32 arg_types, sljit_ins *ins_ptr)
567 {
568 	sljit_s32 arg_count = 0;
569 	sljit_s32 word_arg_count = 0;
570 	sljit_s32 float_arg_count = 0;
571 	sljit_s32 types = 0;
572 	sljit_ins prev_ins = *ins_ptr;
573 	sljit_ins ins = NOP;
574 
575 	SLJIT_ASSERT(reg_map[TMP_REG1] == 4 && freg_map[TMP_FREG1] == 12);
576 
577 	arg_types >>= SLJIT_ARG_SHIFT;
578 
579 	while (arg_types) {
580 		types = (types << SLJIT_ARG_SHIFT) | (arg_types & SLJIT_ARG_MASK);
581 
582 		switch (arg_types & SLJIT_ARG_MASK) {
583 		case SLJIT_ARG_TYPE_F64:
584 		case SLJIT_ARG_TYPE_F32:
585 			arg_count++;
586 			float_arg_count++;
587 			break;
588 		default:
589 			arg_count++;
590 			word_arg_count++;
591 			break;
592 		}
593 
594 		arg_types >>= SLJIT_ARG_SHIFT;
595 	}
596 
597 	while (types) {
598 		switch (types & SLJIT_ARG_MASK) {
599 		case SLJIT_ARG_TYPE_F64:
600 			if (arg_count != float_arg_count)
601 				ins = MOV_S | FMT_D | FS(float_arg_count) | FD(arg_count);
602 			else if (arg_count == 1)
603 				ins = MOV_S | FMT_D | FS(SLJIT_FR0) | FD(TMP_FREG1);
604 			arg_count--;
605 			float_arg_count--;
606 			break;
607 		case SLJIT_ARG_TYPE_F32:
608 			if (arg_count != float_arg_count)
609 				ins = MOV_S | FMT_S | FS(float_arg_count) | FD(arg_count);
610 			else if (arg_count == 1)
611 				ins = MOV_S | FMT_S | FS(SLJIT_FR0) | FD(TMP_FREG1);
612 			arg_count--;
613 			float_arg_count--;
614 			break;
615 		default:
616 			if (arg_count != word_arg_count)
617 				ins = DADDU | S(word_arg_count) | TA(0) | D(arg_count);
618 			else if (arg_count == 1)
619 				ins = DADDU | S(SLJIT_R0) | TA(0) | DA(4);
620 			arg_count--;
621 			word_arg_count--;
622 			break;
623 		}
624 
625 		if (ins != NOP) {
626 			if (prev_ins != NOP)
627 				FAIL_IF(push_inst(compiler, prev_ins, MOVABLE_INS));
628 			prev_ins = ins;
629 			ins = NOP;
630 		}
631 
632 		types >>= SLJIT_ARG_SHIFT;
633 	}
634 
635 	*ins_ptr = prev_ins;
636 
637 	return SLJIT_SUCCESS;
638 }
639 
sljit_emit_call(struct sljit_compiler * compiler,sljit_s32 type,sljit_s32 arg_types)640 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_call(struct sljit_compiler *compiler, sljit_s32 type,
641 	sljit_s32 arg_types)
642 {
643 	struct sljit_jump *jump;
644 	sljit_ins ins = NOP;
645 
646 	CHECK_ERROR_PTR();
647 	CHECK_PTR(check_sljit_emit_call(compiler, type, arg_types));
648 
649 	jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
650 	PTR_FAIL_IF(!jump);
651 	set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
652 
653 	if (type & SLJIT_CALL_RETURN)
654 		PTR_FAIL_IF(emit_stack_frame_release(compiler, 0, &ins));
655 
656 	PTR_FAIL_IF(call_with_args(compiler, arg_types, &ins));
657 
658 	SLJIT_ASSERT(DR(PIC_ADDR_REG) == 25 && PIC_ADDR_REG == TMP_REG2);
659 
660 	PTR_FAIL_IF(emit_const(compiler, PIC_ADDR_REG, 0));
661 
662 	if (!(type & SLJIT_CALL_RETURN)) {
663 		jump->flags |= IS_JAL | IS_CALL;
664 		PTR_FAIL_IF(push_inst(compiler, JALR | S(PIC_ADDR_REG) | DA(RETURN_ADDR_REG), UNMOVABLE_INS));
665 	} else
666 		PTR_FAIL_IF(push_inst(compiler, JR | S(PIC_ADDR_REG), UNMOVABLE_INS));
667 
668 	jump->addr = compiler->size;
669 	PTR_FAIL_IF(push_inst(compiler, ins, UNMOVABLE_INS));
670 
671 	return jump;
672 }
673 
sljit_emit_icall(struct sljit_compiler * compiler,sljit_s32 type,sljit_s32 arg_types,sljit_s32 src,sljit_sw srcw)674 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_icall(struct sljit_compiler *compiler, sljit_s32 type,
675 	sljit_s32 arg_types,
676 	sljit_s32 src, sljit_sw srcw)
677 {
678 	sljit_ins ins = NOP;
679 
680 	CHECK_ERROR();
681 	CHECK(check_sljit_emit_icall(compiler, type, arg_types, src, srcw));
682 
683 	SLJIT_ASSERT(DR(PIC_ADDR_REG) == 25 && PIC_ADDR_REG == TMP_REG2);
684 
685 	if (src & SLJIT_IMM)
686 		FAIL_IF(load_immediate(compiler, DR(PIC_ADDR_REG), srcw));
687 	else if (FAST_IS_REG(src))
688 		FAIL_IF(push_inst(compiler, DADDU | S(src) | TA(0) | D(PIC_ADDR_REG), DR(PIC_ADDR_REG)));
689 	else if (src & SLJIT_MEM) {
690 		ADJUST_LOCAL_OFFSET(src, srcw);
691 		FAIL_IF(emit_op_mem(compiler, WORD_DATA | LOAD_DATA, DR(PIC_ADDR_REG), src, srcw));
692 	}
693 
694 	if (type & SLJIT_CALL_RETURN)
695 		FAIL_IF(emit_stack_frame_release(compiler, 0, &ins));
696 
697 	FAIL_IF(call_with_args(compiler, arg_types, &ins));
698 
699 	/* Register input. */
700 	if (!(type & SLJIT_CALL_RETURN))
701 		FAIL_IF(push_inst(compiler, JALR | S(PIC_ADDR_REG) | DA(RETURN_ADDR_REG), UNMOVABLE_INS));
702 	else
703 		FAIL_IF(push_inst(compiler, JR | S(PIC_ADDR_REG), UNMOVABLE_INS));
704 	return push_inst(compiler, ins, UNMOVABLE_INS);
705 }
706