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