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