2 * Stack-less Just-In-Time compiler
4 * Copyright 2009-2012 Zoltan Herczeg (hzmester@freemail.hu). All rights reserved.
6 * Redistribution and use in source and binary forms, with or without modification, are
7 * permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright notice, this list of
10 * conditions and the following disclaimer.
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.
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.
27 /* Latest MIPS architecture. */
28 /* Automatically detect SLJIT_MIPS_32_64 */
30 SLJIT_API_FUNC_ATTRIBUTE SLJIT_CONST char* sljit_get_platform_name(void)
32 #if (defined SLJIT_MIPS_32_64 && SLJIT_MIPS_32_64)
33 return "MIPS V" SLJIT_CPUINFO;
35 return "MIPS III" SLJIT_CPUINFO;
39 /* Length of an instruction word
40 Both for mips-32 and mips-64 */
41 typedef sljit_ui sljit_ins;
43 #define TMP_REG1 (SLJIT_NO_REGISTERS + 1)
44 #define TMP_REG2 (SLJIT_NO_REGISTERS + 2)
45 #define TMP_REG3 (SLJIT_NO_REGISTERS + 3)
47 /* For position independent code, t9 must contain the function address. */
48 #define PIC_ADDR_REG TMP_REG2
50 /* TMP_EREGs are used mainly for arithmetic operations. */
53 /* Floating point status register. */
55 /* Return address register. */
56 #define RETURN_ADDR_REG 31
58 /* Flags are keept in volatile registers. */
60 /* And carry flag as well. */
62 #define UGREATER_FLAG 11
64 #define GREATER_FLAG 13
65 #define OVERFLOW_FLAG 14
68 #define TMP_FREG2 ((SLJIT_FLOAT_REG6 + 1) << 1)
70 static SLJIT_CONST sljit_ub reg_map[SLJIT_NO_REGISTERS + 4] = {
71 0, 2, 5, 6, 3, 8, 16, 17, 18, 19, 20, 29, 4, 25, 9
74 /* --------------------------------------------------------------------- */
75 /* Instrucion forms */
76 /* --------------------------------------------------------------------- */
78 #define S(s) (reg_map[s] << 21)
79 #define T(t) (reg_map[t] << 16)
80 #define D(d) (reg_map[d] << 11)
81 /* Absolute registers. */
82 #define SA(s) ((s) << 21)
83 #define TA(t) ((t) << 16)
84 #define DA(d) ((d) << 11)
85 #define FT(t) ((t) << 16)
86 #define FS(s) ((s) << 11)
87 #define FD(d) ((d) << 6)
88 #define IMM(imm) ((imm) & 0xffff)
89 #define SH_IMM(imm) ((imm) << 6)
91 #define DR(dr) (reg_map[dr])
92 #define HI(opcode) ((opcode) << 26)
93 #define LO(opcode) (opcode)
94 /* S = (16 << 21) D = (17 << 21) */
95 #define FMT_SD (16 << 21)
97 #define ABS_fmt (HI(17) | FMT_SD | LO(5))
98 #define ADD_fmt (HI(17) | FMT_SD | LO(0))
100 #define ADDU (HI(0) | LO(33))
101 #define AND (HI(0) | LO(36))
102 #define ANDI (HI(12))
104 #define BAL (HI(1) | (17 << 16))
105 #define BC1F (HI(17) | (8 << 21))
106 #define BC1T (HI(17) | (8 << 21) | (1 << 16))
108 #define BGEZ (HI(1) | (1 << 16))
111 #define BLTZ (HI(1) | (0 << 16))
113 #define BREAK (HI(0) | LO(13))
114 #define CFC1 (HI(17) | (2 << 21))
115 #define C_UN_fmt (HI(17) | FMT_SD | LO(49))
116 #define C_UEQ_fmt (HI(17) | FMT_SD | LO(51))
117 #define C_ULE_fmt (HI(17) | FMT_SD | LO(55))
118 #define C_ULT_fmt (HI(17) | FMT_SD | LO(53))
119 #define DADDIU (HI(25))
120 #define DADDU (HI(0) | LO(45))
121 #define DDIV (HI(0) | LO(30))
122 #define DDIVU (HI(0) | LO(31))
123 #define DIV (HI(0) | LO(26))
124 #define DIVU (HI(0) | LO(27))
125 #define DIV_fmt (HI(17) | FMT_SD | LO(3))
126 #define DMULT (HI(0) | LO(28))
127 #define DMULTU (HI(0) | LO(29))
128 #define DSLL (HI(0) | LO(56))
129 #define DSLL32 (HI(0) | LO(60))
130 #define DSLLV (HI(0) | LO(20))
131 #define DSRA (HI(0) | LO(59))
132 #define DSRA32 (HI(0) | LO(63))
133 #define DSRAV (HI(0) | LO(23))
134 #define DSRL (HI(0) | LO(58))
135 #define DSRL32 (HI(0) | LO(62))
136 #define DSRLV (HI(0) | LO(22))
137 #define DSUBU (HI(0) | LO(47))
140 #define JALR (HI(0) | LO(9))
141 #define JR (HI(0) | LO(8))
145 #define MFHI (HI(0) | LO(16))
146 #define MFLO (HI(0) | LO(18))
147 #define MOV_fmt (HI(17) | FMT_SD | LO(6))
148 #define MUL_fmt (HI(17) | FMT_SD | LO(2))
149 #define MULT (HI(0) | LO(24))
150 #define MULTU (HI(0) | LO(25))
151 #define NEG_fmt (HI(17) | FMT_SD | LO(7))
152 #define NOP (HI(0) | LO(0))
153 #define NOR (HI(0) | LO(39))
154 #define OR (HI(0) | LO(37))
157 #define SLT (HI(0) | LO(42))
158 #define SLTI (HI(10))
159 #define SLTIU (HI(11))
160 #define SLTU (HI(0) | LO(43))
161 #define SLL (HI(0) | LO(0))
162 #define SLLV (HI(0) | LO(4))
163 #define SRL (HI(0) | LO(2))
164 #define SRLV (HI(0) | LO(6))
165 #define SRA (HI(0) | LO(3))
166 #define SRAV (HI(0) | LO(7))
167 #define SUB_fmt (HI(17) | FMT_SD | LO(1))
168 #define SUBU (HI(0) | LO(35))
170 #define XOR (HI(0) | LO(38))
171 #define XORI (HI(14))
173 #if (defined SLJIT_MIPS_32_64 && SLJIT_MIPS_32_64)
174 #define CLZ (HI(28) | LO(32))
175 #define DCLZ (HI(28) | LO(36))
176 #define MUL (HI(28) | LO(2))
177 #define SEB (HI(31) | (16 << 6) | LO(32))
178 #define SEH (HI(31) | (24 << 6) | LO(32))
181 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
183 #define ADDIU_W ADDIU
188 #define ADDIU_W DADDIU
193 #define SIMM_MAX (0x7fff)
194 #define SIMM_MIN (-0x8000)
195 #define UIMM_MAX (0xffff)
197 /* dest_reg is the absolute name of the register
198 Useful for reordering instructions in the delay slot. */
199 static sljit_si push_inst(struct sljit_compiler *compiler, sljit_ins ins, sljit_si delay_slot)
201 SLJIT_ASSERT(delay_slot == MOVABLE_INS || delay_slot >= UNMOVABLE_INS
202 || delay_slot == ((ins >> 11) & 0x1f) || delay_slot == ((ins >> 16) & 0x1f));
203 sljit_ins *ptr = (sljit_ins*)ensure_buf(compiler, sizeof(sljit_ins));
207 compiler->delay_slot = delay_slot;
208 return SLJIT_SUCCESS;
211 static SLJIT_INLINE sljit_ins invert_branch(sljit_si flags)
213 return (flags & IS_BIT26_COND) ? (1 << 26) : (1 << 16);
216 static SLJIT_INLINE sljit_ins* detect_jump_type(struct sljit_jump *jump, sljit_ins *code_ptr, sljit_ins *code)
219 sljit_uw target_addr;
221 sljit_ins saved_inst;
223 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
224 if (jump->flags & (SLJIT_REWRITABLE_JUMP | IS_CALL))
227 if (jump->flags & SLJIT_REWRITABLE_JUMP)
231 if (jump->flags & JUMP_ADDR)
232 target_addr = jump->u.target;
234 SLJIT_ASSERT(jump->flags & JUMP_LABEL);
235 target_addr = (sljit_uw)(code + jump->u.label->size);
237 inst = (sljit_ins*)jump->addr;
238 if (jump->flags & IS_COND)
241 #if (defined SLJIT_CONFIG_MIPS_64 && SLJIT_CONFIG_MIPS_64)
242 if (jump->flags & IS_CALL)
246 /* B instructions. */
247 if (jump->flags & IS_MOVABLE) {
248 diff = ((sljit_sw)target_addr - (sljit_sw)(inst)) >> 2;
249 if (diff <= SIMM_MAX && diff >= SIMM_MIN) {
250 jump->flags |= PATCH_B;
252 if (!(jump->flags & IS_COND)) {
254 inst[-1] = (jump->flags & IS_JAL) ? BAL : B;
255 jump->addr -= sizeof(sljit_ins);
258 saved_inst = inst[0];
260 inst[-1] = saved_inst ^ invert_branch(jump->flags);
261 jump->addr -= 2 * sizeof(sljit_ins);
266 diff = ((sljit_sw)target_addr - (sljit_sw)(inst + 1)) >> 2;
267 if (diff <= SIMM_MAX && diff >= SIMM_MIN) {
268 jump->flags |= PATCH_B;
270 if (!(jump->flags & IS_COND)) {
271 inst[0] = (jump->flags & IS_JAL) ? BAL : B;
275 inst[0] = inst[0] ^ invert_branch(jump->flags);
277 jump->addr -= sizeof(sljit_ins);
282 if (jump->flags & IS_COND) {
283 if ((jump->flags & IS_MOVABLE) && (target_addr & ~0xfffffff) == ((jump->addr + 2 * sizeof(sljit_ins)) & ~0xfffffff)) {
284 jump->flags |= PATCH_J;
285 saved_inst = inst[0];
287 inst[-1] = (saved_inst & 0xffff0000) | 3;
292 else if ((target_addr & ~0xfffffff) == ((jump->addr + 3 * sizeof(sljit_ins)) & ~0xfffffff)) {
293 jump->flags |= PATCH_J;
294 inst[0] = (inst[0] & 0xffff0000) | 3;
298 jump->addr += sizeof(sljit_ins);
304 if ((jump->flags & IS_MOVABLE) && (target_addr & ~0xfffffff) == (jump->addr & ~0xfffffff)) {
305 jump->flags |= PATCH_J;
307 inst[-1] = (jump->flags & IS_JAL) ? JAL : J;
308 jump->addr -= sizeof(sljit_ins);
312 if ((target_addr & ~0xfffffff) == ((jump->addr + sizeof(sljit_ins)) & ~0xfffffff)) {
313 jump->flags |= PATCH_J;
314 inst[0] = (jump->flags & IS_JAL) ? JAL : J;
320 #if (defined SLJIT_CONFIG_MIPS_64 && SLJIT_CONFIG_MIPS_64)
322 if (target_addr <= 0x7fffffff) {
323 jump->flags |= PATCH_ABS32;
324 if (jump->flags & IS_COND) {
332 if (target_addr <= 0x7fffffffffffl) {
333 jump->flags |= PATCH_ABS48;
334 if (jump->flags & IS_COND) {
348 static __attribute__ ((noinline)) void sljit_cache_flush(void* code, void* code_ptr)
350 SLJIT_CACHE_FLUSH(code, code_ptr);
354 SLJIT_API_FUNC_ATTRIBUTE void* sljit_generate_code(struct sljit_compiler *compiler)
356 struct sljit_memory_fragment *buf;
364 struct sljit_label *label;
365 struct sljit_jump *jump;
366 struct sljit_const *const_;
369 check_sljit_generate_code(compiler);
370 reverse_buf(compiler);
372 code = (sljit_ins*)SLJIT_MALLOC_EXEC(compiler->size * sizeof(sljit_ins));
373 PTR_FAIL_WITH_EXEC_IF(code);
378 label = compiler->labels;
379 jump = compiler->jumps;
380 const_ = compiler->consts;
382 buf_ptr = (sljit_ins*)buf->memory;
383 buf_end = buf_ptr + (buf->used_size >> 2);
385 *code_ptr = *buf_ptr++;
386 SLJIT_ASSERT(!label || label->size >= word_count);
387 SLJIT_ASSERT(!jump || jump->addr >= word_count);
388 SLJIT_ASSERT(!const_ || const_->addr >= word_count);
389 /* These structures are ordered by their address. */
390 if (label && label->size == word_count) {
391 /* Just recording the address. */
392 label->addr = (sljit_uw)code_ptr;
393 label->size = code_ptr - code;
396 if (jump && jump->addr == word_count) {
397 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
398 jump->addr = (sljit_uw)(code_ptr - 3);
400 jump->addr = (sljit_uw)(code_ptr - 7);
402 code_ptr = detect_jump_type(jump, code_ptr, code);
405 if (const_ && const_->addr == word_count) {
406 /* Just recording the address. */
407 const_->addr = (sljit_uw)code_ptr;
408 const_ = const_->next;
412 } while (buf_ptr < buf_end);
417 if (label && label->size == word_count) {
418 label->addr = (sljit_uw)code_ptr;
419 label->size = code_ptr - code;
423 SLJIT_ASSERT(!label);
425 SLJIT_ASSERT(!const_);
426 SLJIT_ASSERT(code_ptr - code <= (sljit_sw)compiler->size);
428 jump = compiler->jumps;
431 addr = (jump->flags & JUMP_LABEL) ? jump->u.label->addr : jump->u.target;
432 buf_ptr = (sljit_ins*)jump->addr;
434 if (jump->flags & PATCH_B) {
435 addr = (sljit_sw)(addr - (jump->addr + sizeof(sljit_ins))) >> 2;
436 SLJIT_ASSERT((sljit_sw)addr <= SIMM_MAX && (sljit_sw)addr >= SIMM_MIN);
437 buf_ptr[0] = (buf_ptr[0] & 0xffff0000) | (addr & 0xffff);
440 if (jump->flags & PATCH_J) {
441 SLJIT_ASSERT((addr & ~0xfffffff) == ((jump->addr + sizeof(sljit_ins)) & ~0xfffffff));
442 buf_ptr[0] |= (addr >> 2) & 0x03ffffff;
446 /* Set the fields of immediate loads. */
447 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
448 buf_ptr[0] = (buf_ptr[0] & 0xffff0000) | ((addr >> 16) & 0xffff);
449 buf_ptr[1] = (buf_ptr[1] & 0xffff0000) | (addr & 0xffff);
451 if (jump->flags & PATCH_ABS32) {
452 SLJIT_ASSERT(addr <= 0x7fffffff);
453 buf_ptr[0] = (buf_ptr[0] & 0xffff0000) | ((addr >> 16) & 0xffff);
454 buf_ptr[1] = (buf_ptr[1] & 0xffff0000) | (addr & 0xffff);
456 else if (jump->flags & PATCH_ABS48) {
457 SLJIT_ASSERT(addr <= 0x7fffffffffffl);
458 buf_ptr[0] = (buf_ptr[0] & 0xffff0000) | ((addr >> 32) & 0xffff);
459 buf_ptr[1] = (buf_ptr[1] & 0xffff0000) | ((addr >> 16) & 0xffff);
460 buf_ptr[3] = (buf_ptr[3] & 0xffff0000) | (addr & 0xffff);
463 buf_ptr[0] = (buf_ptr[0] & 0xffff0000) | ((addr >> 48) & 0xffff);
464 buf_ptr[1] = (buf_ptr[1] & 0xffff0000) | ((addr >> 32) & 0xffff);
465 buf_ptr[3] = (buf_ptr[3] & 0xffff0000) | ((addr >> 16) & 0xffff);
466 buf_ptr[5] = (buf_ptr[5] & 0xffff0000) | (addr & 0xffff);
473 compiler->error = SLJIT_ERR_COMPILED;
474 compiler->executable_size = (code_ptr - code) * sizeof(sljit_ins);
476 SLJIT_CACHE_FLUSH(code, code_ptr);
478 /* GCC workaround for invalid code generation with -O2. */
479 sljit_cache_flush(code, code_ptr);
484 /* --------------------------------------------------------------------- */
486 /* --------------------------------------------------------------------- */
488 /* Creates an index in data_transfer_insts array. */
489 #define LOAD_DATA 0x01
490 #define WORD_DATA 0x00
491 #define BYTE_DATA 0x02
492 #define HALF_DATA 0x04
493 #define INT_DATA 0x06
494 #define SIGNED_DATA 0x08
495 /* Separates integer and floating point registers */
497 #define DOUBLE_DATA 0x10
499 #define MEM_MASK 0x1f
501 #define WRITE_BACK 0x00020
502 #define ARG_TEST 0x00040
503 #define ALT_KEEP_CACHE 0x00080
504 #define CUMULATIVE_OP 0x00100
505 #define LOGICAL_OP 0x00200
506 #define IMM_OP 0x00400
507 #define SRC2_IMM 0x00800
509 #define UNUSED_DEST 0x01000
510 #define REG_DEST 0x02000
511 #define REG1_SOURCE 0x04000
512 #define REG2_SOURCE 0x08000
513 #define SLOW_SRC1 0x10000
514 #define SLOW_SRC2 0x20000
515 #define SLOW_DEST 0x40000
517 /* Only these flags are set. UNUSED_DEST is not set when no flags should be set. */
518 #define CHECK_FLAGS(list) \
519 (!(flags & UNUSED_DEST) || (op & GET_FLAGS(~(list))))
521 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
522 #define STACK_STORE SW
523 #define STACK_LOAD LW
525 #define STACK_STORE SD
526 #define STACK_LOAD LD
529 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
530 #include "sljitNativeMIPS_32.c"
532 #include "sljitNativeMIPS_64.c"
535 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_enter(struct sljit_compiler *compiler, sljit_si args, sljit_si scratches, sljit_si saveds, sljit_si local_size)
540 check_sljit_emit_enter(compiler, args, scratches, saveds, local_size);
542 compiler->scratches = scratches;
543 compiler->saveds = saveds;
544 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
545 compiler->logical_local_size = local_size;
548 local_size += (saveds + 1 + 4) * sizeof(sljit_sw);
549 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
550 local_size = (local_size + 15) & ~0xf;
552 local_size = (local_size + 31) & ~0x1f;
554 compiler->local_size = local_size;
556 if (local_size <= SIMM_MAX) {
558 FAIL_IF(push_inst(compiler, ADDIU_W | S(SLJIT_LOCALS_REG) | T(SLJIT_LOCALS_REG) | IMM(-local_size), DR(SLJIT_LOCALS_REG)));
559 base = S(SLJIT_LOCALS_REG);
562 FAIL_IF(load_immediate(compiler, DR(TMP_REG1), local_size));
563 FAIL_IF(push_inst(compiler, ADDU_W | S(SLJIT_LOCALS_REG) | TA(0) | D(TMP_REG2), DR(TMP_REG2)));
564 FAIL_IF(push_inst(compiler, SUBU_W | S(SLJIT_LOCALS_REG) | T(TMP_REG1) | D(SLJIT_LOCALS_REG), DR(SLJIT_LOCALS_REG)));
569 FAIL_IF(push_inst(compiler, STACK_STORE | base | TA(RETURN_ADDR_REG) | IMM(local_size - 1 * (sljit_si)sizeof(sljit_sw)), MOVABLE_INS));
571 FAIL_IF(push_inst(compiler, STACK_STORE | base | T(SLJIT_SAVED_REG1) | IMM(local_size - 2 * (sljit_si)sizeof(sljit_sw)), MOVABLE_INS));
573 FAIL_IF(push_inst(compiler, STACK_STORE | base | T(SLJIT_SAVED_REG2) | IMM(local_size - 3 * (sljit_si)sizeof(sljit_sw)), MOVABLE_INS));
575 FAIL_IF(push_inst(compiler, STACK_STORE | base | T(SLJIT_SAVED_REG3) | IMM(local_size - 4 * (sljit_si)sizeof(sljit_sw)), MOVABLE_INS));
577 FAIL_IF(push_inst(compiler, STACK_STORE | base | T(SLJIT_SAVED_EREG1) | IMM(local_size - 5 * (sljit_si)sizeof(sljit_sw)), MOVABLE_INS));
579 FAIL_IF(push_inst(compiler, STACK_STORE | base | T(SLJIT_SAVED_EREG2) | IMM(local_size - 6 * (sljit_si)sizeof(sljit_sw)), MOVABLE_INS));
582 FAIL_IF(push_inst(compiler, ADDU_W | SA(4) | TA(0) | D(SLJIT_SAVED_REG1), DR(SLJIT_SAVED_REG1)));
584 FAIL_IF(push_inst(compiler, ADDU_W | SA(5) | TA(0) | D(SLJIT_SAVED_REG2), DR(SLJIT_SAVED_REG2)));
586 FAIL_IF(push_inst(compiler, ADDU_W | SA(6) | TA(0) | D(SLJIT_SAVED_REG3), DR(SLJIT_SAVED_REG3)));
588 return SLJIT_SUCCESS;
591 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_context(struct sljit_compiler *compiler, sljit_si args, sljit_si scratches, sljit_si saveds, sljit_si local_size)
594 check_sljit_set_context(compiler, args, scratches, saveds, local_size);
596 compiler->scratches = scratches;
597 compiler->saveds = saveds;
598 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
599 compiler->logical_local_size = local_size;
602 local_size += (saveds + 1 + 4) * sizeof(sljit_sw);
603 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
604 compiler->local_size = (local_size + 15) & ~0xf;
606 compiler->local_size = (local_size + 31) & ~0x1f;
610 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_return(struct sljit_compiler *compiler, sljit_si op, sljit_si src, sljit_sw srcw)
616 check_sljit_emit_return(compiler, op, src, srcw);
618 FAIL_IF(emit_mov_before_return(compiler, op, src, srcw));
620 local_size = compiler->local_size;
621 if (local_size <= SIMM_MAX)
622 base = S(SLJIT_LOCALS_REG);
624 FAIL_IF(load_immediate(compiler, DR(TMP_REG1), local_size));
625 FAIL_IF(push_inst(compiler, ADDU_W | S(SLJIT_LOCALS_REG) | T(TMP_REG1) | D(TMP_REG1), DR(TMP_REG1)));
630 FAIL_IF(push_inst(compiler, STACK_LOAD | base | TA(RETURN_ADDR_REG) | IMM(local_size - 1 * (sljit_si)sizeof(sljit_sw)), RETURN_ADDR_REG));
631 if (compiler->saveds >= 5)
632 FAIL_IF(push_inst(compiler, STACK_LOAD | base | T(SLJIT_SAVED_EREG2) | IMM(local_size - 6 * (sljit_si)sizeof(sljit_sw)), DR(SLJIT_SAVED_EREG2)));
633 if (compiler->saveds >= 4)
634 FAIL_IF(push_inst(compiler, STACK_LOAD | base | T(SLJIT_SAVED_EREG1) | IMM(local_size - 5 * (sljit_si)sizeof(sljit_sw)), DR(SLJIT_SAVED_EREG1)));
635 if (compiler->saveds >= 3)
636 FAIL_IF(push_inst(compiler, STACK_LOAD | base | T(SLJIT_SAVED_REG3) | IMM(local_size - 4 * (sljit_si)sizeof(sljit_sw)), DR(SLJIT_SAVED_REG3)));
637 if (compiler->saveds >= 2)
638 FAIL_IF(push_inst(compiler, STACK_LOAD | base | T(SLJIT_SAVED_REG2) | IMM(local_size - 3 * (sljit_si)sizeof(sljit_sw)), DR(SLJIT_SAVED_REG2)));
639 if (compiler->saveds >= 1)
640 FAIL_IF(push_inst(compiler, STACK_LOAD | base | T(SLJIT_SAVED_REG1) | IMM(local_size - 2 * (sljit_si)sizeof(sljit_sw)), DR(SLJIT_SAVED_REG1)));
642 FAIL_IF(push_inst(compiler, JR | SA(RETURN_ADDR_REG), UNMOVABLE_INS));
643 if (compiler->local_size <= SIMM_MAX)
644 return push_inst(compiler, ADDIU_W | S(SLJIT_LOCALS_REG) | T(SLJIT_LOCALS_REG) | IMM(compiler->local_size), UNMOVABLE_INS);
646 return push_inst(compiler, ADDU_W | S(TMP_REG1) | TA(0) | D(SLJIT_LOCALS_REG), UNMOVABLE_INS);
652 /* --------------------------------------------------------------------- */
654 /* --------------------------------------------------------------------- */
656 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
657 #define ARCH_32_64(a, b) a
659 #define ARCH_32_64(a, b) b
662 static SLJIT_CONST sljit_ins data_transfer_insts[16 + 4] = {
663 /* u w s */ ARCH_32_64(HI(43) /* sw */, HI(63) /* sd */),
664 /* u w l */ ARCH_32_64(HI(35) /* lw */, HI(55) /* ld */),
665 /* u b s */ HI(40) /* sb */,
666 /* u b l */ HI(36) /* lbu */,
667 /* u h s */ HI(41) /* sh */,
668 /* u h l */ HI(37) /* lhu */,
669 /* u i s */ HI(43) /* sw */,
670 /* u i l */ ARCH_32_64(HI(35) /* lw */, HI(39) /* lwu */),
672 /* s w s */ ARCH_32_64(HI(43) /* sw */, HI(63) /* sd */),
673 /* s w l */ ARCH_32_64(HI(35) /* lw */, HI(55) /* ld */),
674 /* s b s */ HI(40) /* sb */,
675 /* s b l */ HI(32) /* lb */,
676 /* s h s */ HI(41) /* sh */,
677 /* s h l */ HI(33) /* lh */,
678 /* s i s */ HI(43) /* sw */,
679 /* s i l */ HI(35) /* lw */,
681 /* d s */ HI(61) /* sdc1 */,
682 /* d l */ HI(53) /* ldc1 */,
683 /* s s */ HI(57) /* swc1 */,
684 /* s l */ HI(49) /* lwc1 */,
689 /* reg_ar is an absoulute register! */
691 /* Can perform an operation using at most 1 instruction. */
692 static sljit_si getput_arg_fast(struct sljit_compiler *compiler, sljit_si flags, sljit_si reg_ar, sljit_si arg, sljit_sw argw)
694 SLJIT_ASSERT(arg & SLJIT_MEM);
696 if ((!(flags & WRITE_BACK) || !(arg & REG_MASK)) && !(arg & OFFS_REG_MASK) && argw <= SIMM_MAX && argw >= SIMM_MIN) {
697 /* Works for both absoulte and relative addresses. */
698 if (SLJIT_UNLIKELY(flags & ARG_TEST))
700 FAIL_IF(push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | S(arg & REG_MASK)
701 | TA(reg_ar) | IMM(argw), ((flags & MEM_MASK) <= GPR_REG && (flags & LOAD_DATA)) ? reg_ar : MOVABLE_INS));
707 /* See getput_arg below.
708 Note: can_cache is called only for binary operators. Those
709 operators always uses word arguments without write back. */
710 static sljit_si can_cache(sljit_si arg, sljit_sw argw, sljit_si next_arg, sljit_sw next_argw)
712 SLJIT_ASSERT((arg & SLJIT_MEM) && (next_arg & SLJIT_MEM));
714 /* Simple operation except for updates. */
715 if (arg & OFFS_REG_MASK) {
718 if (argw && argw == next_argw && (arg == next_arg || (arg & OFFS_REG_MASK) == (next_arg & OFFS_REG_MASK)))
723 if (arg == next_arg) {
724 if (((next_argw - argw) <= SIMM_MAX && (next_argw - argw) >= SIMM_MIN))
732 /* Emit the necessary instructions. See can_cache above. */
733 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)
735 sljit_si tmp_ar, base, delay_slot;
737 SLJIT_ASSERT(arg & SLJIT_MEM);
738 if (!(next_arg & SLJIT_MEM)) {
743 if ((flags & MEM_MASK) <= GPR_REG && (flags & LOAD_DATA)) {
747 tmp_ar = DR(TMP_REG1);
748 delay_slot = MOVABLE_INS;
750 base = arg & REG_MASK;
752 if (SLJIT_UNLIKELY(arg & OFFS_REG_MASK)) {
754 if ((flags & WRITE_BACK) && reg_ar == DR(base)) {
755 SLJIT_ASSERT(!(flags & LOAD_DATA) && DR(TMP_REG1) != reg_ar);
756 FAIL_IF(push_inst(compiler, ADDU_W | SA(reg_ar) | TA(0) | D(TMP_REG1), DR(TMP_REG1)));
757 reg_ar = DR(TMP_REG1);
760 /* Using the cache. */
761 if (argw == compiler->cache_argw) {
762 if (!(flags & WRITE_BACK)) {
763 if (arg == compiler->cache_arg)
764 return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | S(TMP_REG3) | TA(reg_ar), delay_slot);
765 if ((SLJIT_MEM | (arg & OFFS_REG_MASK)) == compiler->cache_arg) {
766 if (arg == next_arg && argw == (next_argw & 0x3)) {
767 compiler->cache_arg = arg;
768 compiler->cache_argw = argw;
769 FAIL_IF(push_inst(compiler, ADDU_W | S(base) | T(TMP_REG3) | D(TMP_REG3), DR(TMP_REG3)));
770 return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | S(TMP_REG3) | TA(reg_ar), delay_slot);
772 FAIL_IF(push_inst(compiler, ADDU_W | S(base) | T(TMP_REG3) | DA(tmp_ar), tmp_ar));
773 return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | SA(tmp_ar) | TA(reg_ar), delay_slot);
777 if ((SLJIT_MEM | (arg & OFFS_REG_MASK)) == compiler->cache_arg) {
778 FAIL_IF(push_inst(compiler, ADDU_W | S(base) | T(TMP_REG3) | D(base), DR(base)));
779 return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | S(base) | TA(reg_ar), delay_slot);
784 if (SLJIT_UNLIKELY(argw)) {
785 compiler->cache_arg = SLJIT_MEM | (arg & OFFS_REG_MASK);
786 compiler->cache_argw = argw;
787 FAIL_IF(push_inst(compiler, SLL_W | T(OFFS_REG(arg)) | D(TMP_REG3) | SH_IMM(argw), DR(TMP_REG3)));
790 if (!(flags & WRITE_BACK)) {
791 if (arg == next_arg && argw == (next_argw & 0x3)) {
792 compiler->cache_arg = arg;
793 compiler->cache_argw = argw;
794 FAIL_IF(push_inst(compiler, ADDU_W | S(base) | T(!argw ? OFFS_REG(arg) : TMP_REG3) | D(TMP_REG3), DR(TMP_REG3)));
795 tmp_ar = DR(TMP_REG3);
798 FAIL_IF(push_inst(compiler, ADDU_W | S(base) | T(!argw ? OFFS_REG(arg) : TMP_REG3) | DA(tmp_ar), tmp_ar));
799 return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | SA(tmp_ar) | TA(reg_ar), delay_slot);
801 FAIL_IF(push_inst(compiler, ADDU_W | S(base) | T(!argw ? OFFS_REG(arg) : TMP_REG3) | D(base), DR(base)));
802 return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | S(base) | TA(reg_ar), delay_slot);
805 if (SLJIT_UNLIKELY(flags & WRITE_BACK) && base) {
806 /* Update only applies if a base register exists. */
807 if (reg_ar == DR(base)) {
808 SLJIT_ASSERT(!(flags & LOAD_DATA) && DR(TMP_REG1) != reg_ar);
809 if (argw <= SIMM_MAX && argw >= SIMM_MIN) {
810 FAIL_IF(push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | S(base) | TA(reg_ar) | IMM(argw), MOVABLE_INS));
812 return push_inst(compiler, ADDIU_W | S(base) | T(base) | IMM(argw), DR(base));
813 return SLJIT_SUCCESS;
815 FAIL_IF(push_inst(compiler, ADDU_W | SA(reg_ar) | TA(0) | D(TMP_REG1), DR(TMP_REG1)));
816 reg_ar = DR(TMP_REG1);
819 if (argw <= SIMM_MAX && argw >= SIMM_MIN) {
821 FAIL_IF(push_inst(compiler, ADDIU_W | S(base) | T(base) | IMM(argw), DR(base)));
824 if (compiler->cache_arg == SLJIT_MEM && argw - compiler->cache_argw <= SIMM_MAX && argw - compiler->cache_argw >= SIMM_MIN) {
825 if (argw != compiler->cache_argw) {
826 FAIL_IF(push_inst(compiler, ADDIU_W | S(TMP_REG3) | T(TMP_REG3) | IMM(argw - compiler->cache_argw), DR(TMP_REG3)));
827 compiler->cache_argw = argw;
829 FAIL_IF(push_inst(compiler, ADDU_W | S(base) | T(TMP_REG3) | D(base), DR(base)));
832 compiler->cache_arg = SLJIT_MEM;
833 compiler->cache_argw = argw;
834 FAIL_IF(load_immediate(compiler, DR(TMP_REG3), argw));
835 FAIL_IF(push_inst(compiler, ADDU_W | S(base) | T(TMP_REG3) | D(base), DR(base)));
838 return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | S(base) | TA(reg_ar), delay_slot);
841 if (compiler->cache_arg == arg && argw - compiler->cache_argw <= SIMM_MAX && argw - compiler->cache_argw >= SIMM_MIN) {
842 if (argw != compiler->cache_argw) {
843 FAIL_IF(push_inst(compiler, ADDIU_W | S(TMP_REG3) | T(TMP_REG3) | IMM(argw - compiler->cache_argw), DR(TMP_REG3)));
844 compiler->cache_argw = argw;
846 return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | S(TMP_REG3) | TA(reg_ar), delay_slot);
849 if (compiler->cache_arg == SLJIT_MEM && argw - compiler->cache_argw <= SIMM_MAX && argw - compiler->cache_argw >= SIMM_MIN) {
850 if (argw != compiler->cache_argw)
851 FAIL_IF(push_inst(compiler, ADDIU_W | S(TMP_REG3) | T(TMP_REG3) | IMM(argw - compiler->cache_argw), DR(TMP_REG3)));
854 compiler->cache_arg = SLJIT_MEM;
855 FAIL_IF(load_immediate(compiler, DR(TMP_REG3), argw));
857 compiler->cache_argw = argw;
860 return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | S(TMP_REG3) | TA(reg_ar), delay_slot);
862 if (arg == next_arg && next_argw - argw <= SIMM_MAX && next_argw - argw >= SIMM_MIN) {
863 compiler->cache_arg = arg;
864 FAIL_IF(push_inst(compiler, ADDU_W | S(TMP_REG3) | T(base) | D(TMP_REG3), DR(TMP_REG3)));
865 return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | S(TMP_REG3) | TA(reg_ar), delay_slot);
868 FAIL_IF(push_inst(compiler, ADDU_W | S(TMP_REG3) | T(base) | DA(tmp_ar), tmp_ar));
869 return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | SA(tmp_ar) | TA(reg_ar), delay_slot);
872 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)
874 if (getput_arg_fast(compiler, flags, reg_ar, arg, argw))
875 return compiler->error;
876 compiler->cache_arg = 0;
877 compiler->cache_argw = 0;
878 return getput_arg(compiler, flags, reg_ar, arg, argw, 0, 0);
881 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)
883 if (getput_arg_fast(compiler, flags, reg, arg1, arg1w))
884 return compiler->error;
885 return getput_arg(compiler, flags, reg, arg1, arg1w, arg2, arg2w);
888 static sljit_si emit_op(struct sljit_compiler *compiler, sljit_si op, sljit_si flags,
889 sljit_si dst, sljit_sw dstw,
890 sljit_si src1, sljit_sw src1w,
891 sljit_si src2, sljit_sw src2w)
893 /* arg1 goes to TMP_REG1 or src reg
894 arg2 goes to TMP_REG2, imm or src reg
895 TMP_REG3 can be used for caching
896 result goes to TMP_REG2, so put result can use TMP_REG1 and TMP_REG3. */
897 sljit_si dst_r = TMP_REG2;
900 sljit_si sugg_src2_r = TMP_REG2;
902 if (!(flags & ALT_KEEP_CACHE)) {
903 compiler->cache_arg = 0;
904 compiler->cache_argw = 0;
907 if (SLJIT_UNLIKELY(dst == SLJIT_UNUSED)) {
908 if (op >= SLJIT_MOV && op <= SLJIT_MOVU_SI && !(src2 & SLJIT_MEM))
909 return SLJIT_SUCCESS;
911 flags |= UNUSED_DEST;
913 else if (FAST_IS_REG(dst)) {
916 if (op >= SLJIT_MOV && op <= SLJIT_MOVU_SI)
919 else if ((dst & SLJIT_MEM) && !getput_arg_fast(compiler, flags | ARG_TEST, DR(TMP_REG1), dst, dstw))
922 if (flags & IMM_OP) {
923 if ((src2 & SLJIT_IMM) && src2w) {
924 if ((!(flags & LOGICAL_OP) && (src2w <= SIMM_MAX && src2w >= SIMM_MIN))
925 || ((flags & LOGICAL_OP) && !(src2w & ~UIMM_MAX))) {
930 if (!(flags & SRC2_IMM) && (flags & CUMULATIVE_OP) && (src1 & SLJIT_IMM) && src1w) {
931 if ((!(flags & LOGICAL_OP) && (src1w <= SIMM_MAX && src1w >= SIMM_MIN))
932 || ((flags & LOGICAL_OP) && !(src1w & ~UIMM_MAX))) {
936 /* And swap arguments. */
940 /* src2w = src2_r unneeded. */
946 if (FAST_IS_REG(src1)) {
948 flags |= REG1_SOURCE;
950 else if (src1 & SLJIT_IMM) {
952 FAIL_IF(load_immediate(compiler, DR(TMP_REG1), src1w));
959 if (getput_arg_fast(compiler, flags | LOAD_DATA, DR(TMP_REG1), src1, src1w))
960 FAIL_IF(compiler->error);
967 if (FAST_IS_REG(src2)) {
969 flags |= REG2_SOURCE;
970 if (!(flags & REG_DEST) && op >= SLJIT_MOV && op <= SLJIT_MOVU_SI)
973 else if (src2 & SLJIT_IMM) {
974 if (!(flags & SRC2_IMM)) {
976 FAIL_IF(load_immediate(compiler, DR(sugg_src2_r), src2w));
977 src2_r = sugg_src2_r;
981 if ((op >= SLJIT_MOV && op <= SLJIT_MOVU_SI) && (dst & SLJIT_MEM))
987 if (getput_arg_fast(compiler, flags | LOAD_DATA, DR(sugg_src2_r), src2, src2w))
988 FAIL_IF(compiler->error);
991 src2_r = sugg_src2_r;
994 if ((flags & (SLOW_SRC1 | SLOW_SRC2)) == (SLOW_SRC1 | SLOW_SRC2)) {
995 SLJIT_ASSERT(src2_r == TMP_REG2);
996 if (!can_cache(src1, src1w, src2, src2w) && can_cache(src1, src1w, dst, dstw)) {
997 FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, DR(TMP_REG2), src2, src2w, src1, src1w));
998 FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, DR(TMP_REG1), src1, src1w, dst, dstw));
1001 FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, DR(TMP_REG1), src1, src1w, src2, src2w));
1002 FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, DR(TMP_REG2), src2, src2w, dst, dstw));
1005 else if (flags & SLOW_SRC1)
1006 FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, DR(TMP_REG1), src1, src1w, dst, dstw));
1007 else if (flags & SLOW_SRC2)
1008 FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, DR(sugg_src2_r), src2, src2w, dst, dstw));
1010 FAIL_IF(emit_single_op(compiler, op, flags, dst_r, src1_r, src2_r));
1012 if (dst & SLJIT_MEM) {
1013 if (!(flags & SLOW_DEST)) {
1014 getput_arg_fast(compiler, flags, DR(dst_r), dst, dstw);
1015 return compiler->error;
1017 return getput_arg(compiler, flags, DR(dst_r), dst, dstw, 0, 0);
1020 return SLJIT_SUCCESS;
1023 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op0(struct sljit_compiler *compiler, sljit_si op)
1025 #if (defined SLJIT_CONFIG_MIPS_64 && SLJIT_CONFIG_MIPS_64)
1026 sljit_si int_op = op & SLJIT_INT_OP;
1030 check_sljit_emit_op0(compiler, op);
1032 op = GET_OPCODE(op);
1034 case SLJIT_BREAKPOINT:
1035 return push_inst(compiler, BREAK, UNMOVABLE_INS);
1037 return push_inst(compiler, NOP, UNMOVABLE_INS);
1040 #if (defined SLJIT_CONFIG_MIPS_64 && SLJIT_CONFIG_MIPS_64)
1041 FAIL_IF(push_inst(compiler, (op == SLJIT_UMUL ? DMULTU : DMULT) | S(SLJIT_SCRATCH_REG1) | T(SLJIT_SCRATCH_REG2), MOVABLE_INS));
1043 FAIL_IF(push_inst(compiler, (op == SLJIT_UMUL ? MULTU : MULT) | S(SLJIT_SCRATCH_REG1) | T(SLJIT_SCRATCH_REG2), MOVABLE_INS));
1045 FAIL_IF(push_inst(compiler, MFLO | D(SLJIT_SCRATCH_REG1), DR(SLJIT_SCRATCH_REG1)));
1046 return push_inst(compiler, MFHI | D(SLJIT_SCRATCH_REG2), DR(SLJIT_SCRATCH_REG2));
1049 #if !(defined SLJIT_MIPS_32_64 && SLJIT_MIPS_32_64)
1050 FAIL_IF(push_inst(compiler, NOP, UNMOVABLE_INS));
1051 FAIL_IF(push_inst(compiler, NOP, UNMOVABLE_INS));
1054 #if (defined SLJIT_CONFIG_MIPS_64 && SLJIT_CONFIG_MIPS_64)
1056 FAIL_IF(push_inst(compiler, (op == SLJIT_UDIV ? DIVU : DIV) | S(SLJIT_SCRATCH_REG1) | T(SLJIT_SCRATCH_REG2), MOVABLE_INS));
1058 FAIL_IF(push_inst(compiler, (op == SLJIT_UDIV ? DDIVU : DDIV) | S(SLJIT_SCRATCH_REG1) | T(SLJIT_SCRATCH_REG2), MOVABLE_INS));
1060 FAIL_IF(push_inst(compiler, (op == SLJIT_UDIV ? DIVU : DIV) | S(SLJIT_SCRATCH_REG1) | T(SLJIT_SCRATCH_REG2), MOVABLE_INS));
1063 FAIL_IF(push_inst(compiler, MFLO | D(SLJIT_SCRATCH_REG1), DR(SLJIT_SCRATCH_REG1)));
1064 return push_inst(compiler, MFHI | D(SLJIT_SCRATCH_REG2), DR(SLJIT_SCRATCH_REG2));
1067 return SLJIT_SUCCESS;
1070 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op1(struct sljit_compiler *compiler, sljit_si op,
1071 sljit_si dst, sljit_sw dstw,
1072 sljit_si src, sljit_sw srcw)
1074 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
1081 check_sljit_emit_op1(compiler, op, dst, dstw, src, srcw);
1082 ADJUST_LOCAL_OFFSET(dst, dstw);
1083 ADJUST_LOCAL_OFFSET(src, srcw);
1085 #if (defined SLJIT_CONFIG_MIPS_64 && SLJIT_CONFIG_MIPS_64)
1086 if ((op & SLJIT_INT_OP) && GET_OPCODE(op) >= SLJIT_NOT) {
1087 flags |= INT_DATA | SIGNED_DATA;
1088 if (src & SLJIT_IMM)
1089 srcw = (sljit_si)srcw;
1093 switch (GET_OPCODE(op)) {
1096 return emit_op(compiler, SLJIT_MOV, WORD_DATA, dst, dstw, TMP_REG1, 0, src, srcw);
1099 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
1100 return emit_op(compiler, SLJIT_MOV_UI, INT_DATA, dst, dstw, TMP_REG1, 0, src, srcw);
1102 return emit_op(compiler, SLJIT_MOV_UI, INT_DATA, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_ui)srcw : srcw);
1106 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
1107 return emit_op(compiler, SLJIT_MOV_SI, INT_DATA | SIGNED_DATA, dst, dstw, TMP_REG1, 0, src, srcw);
1109 return emit_op(compiler, SLJIT_MOV_SI, INT_DATA | SIGNED_DATA, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_si)srcw : srcw);
1113 return emit_op(compiler, SLJIT_MOV_UB, BYTE_DATA, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_ub)srcw : srcw);
1116 return emit_op(compiler, SLJIT_MOV_SB, BYTE_DATA | SIGNED_DATA, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_sb)srcw : srcw);
1119 return emit_op(compiler, SLJIT_MOV_UH, HALF_DATA, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_uh)srcw : srcw);
1122 return emit_op(compiler, SLJIT_MOV_SH, HALF_DATA | SIGNED_DATA, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_sh)srcw : srcw);
1126 return emit_op(compiler, SLJIT_MOV, WORD_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, srcw);
1129 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
1130 return emit_op(compiler, SLJIT_MOV_UI, INT_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, srcw);
1132 return emit_op(compiler, SLJIT_MOV_UI, INT_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_ui)srcw : srcw);
1136 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
1137 return emit_op(compiler, SLJIT_MOV_SI, INT_DATA | SIGNED_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, srcw);
1139 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);
1143 return emit_op(compiler, SLJIT_MOV_UB, BYTE_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_ub)srcw : srcw);
1146 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);
1149 return emit_op(compiler, SLJIT_MOV_UH, HALF_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_uh)srcw : srcw);
1152 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);
1155 return emit_op(compiler, op, flags, dst, dstw, TMP_REG1, 0, src, srcw);
1158 return emit_op(compiler, SLJIT_SUB | GET_ALL_FLAGS(op), flags | IMM_OP, dst, dstw, SLJIT_IMM, 0, src, srcw);
1161 return emit_op(compiler, op, flags, dst, dstw, TMP_REG1, 0, src, srcw);
1164 return SLJIT_SUCCESS;
1166 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
1171 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op2(struct sljit_compiler *compiler, sljit_si op,
1172 sljit_si dst, sljit_sw dstw,
1173 sljit_si src1, sljit_sw src1w,
1174 sljit_si src2, sljit_sw src2w)
1176 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
1183 check_sljit_emit_op2(compiler, op, dst, dstw, src1, src1w, src2, src2w);
1184 ADJUST_LOCAL_OFFSET(dst, dstw);
1185 ADJUST_LOCAL_OFFSET(src1, src1w);
1186 ADJUST_LOCAL_OFFSET(src2, src2w);
1188 #if (defined SLJIT_CONFIG_MIPS_64 && SLJIT_CONFIG_MIPS_64)
1189 if (op & SLJIT_INT_OP) {
1190 flags |= INT_DATA | SIGNED_DATA;
1191 if (src1 & SLJIT_IMM)
1192 src1w = (sljit_si)src1w;
1193 if (src2 & SLJIT_IMM)
1194 src2w = (sljit_si)src2w;
1198 switch (GET_OPCODE(op)) {
1201 return emit_op(compiler, op, flags | CUMULATIVE_OP | IMM_OP, dst, dstw, src1, src1w, src2, src2w);
1205 return emit_op(compiler, op, flags | IMM_OP, dst, dstw, src1, src1w, src2, src2w);
1208 return emit_op(compiler, op, flags | CUMULATIVE_OP, dst, dstw, src1, src1w, src2, src2w);
1213 return emit_op(compiler, op, flags | CUMULATIVE_OP | LOGICAL_OP | IMM_OP, dst, dstw, src1, src1w, src2, src2w);
1218 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
1219 if (src2 & SLJIT_IMM)
1222 if (src2 & SLJIT_IMM) {
1223 if (op & SLJIT_INT_OP)
1229 return emit_op(compiler, op, flags | IMM_OP, dst, dstw, src1, src1w, src2, src2w);
1232 return SLJIT_SUCCESS;
1234 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
1239 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_get_register_index(sljit_si reg)
1241 check_sljit_get_register_index(reg);
1242 return reg_map[reg];
1245 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_get_float_register_index(sljit_si reg)
1247 check_sljit_get_float_register_index(reg);
1251 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op_custom(struct sljit_compiler *compiler,
1252 void *instruction, sljit_si size)
1255 check_sljit_emit_op_custom(compiler, instruction, size);
1256 SLJIT_ASSERT(size == 4);
1258 return push_inst(compiler, *(sljit_ins*)instruction, UNMOVABLE_INS);
1261 /* --------------------------------------------------------------------- */
1262 /* Floating point operators */
1263 /* --------------------------------------------------------------------- */
1265 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_is_fpu_available(void)
1267 #ifdef SLJIT_IS_FPU_AVAILABLE
1268 return SLJIT_IS_FPU_AVAILABLE;
1269 #elif defined(__GNUC__)
1271 asm ("cfc1 %0, $0" : "=r"(fir));
1272 return (fir >> 22) & 0x1;
1274 #error "FIR check is not implemented for this architecture"
1278 #define FLOAT_DATA(op) (DOUBLE_DATA | ((op & SLJIT_SINGLE_OP) >> 7))
1279 #define FMT(op) (((op & SLJIT_SINGLE_OP) ^ SLJIT_SINGLE_OP) << (21 - 8))
1281 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fop1(struct sljit_compiler *compiler, sljit_si op,
1282 sljit_si dst, sljit_sw dstw,
1283 sljit_si src, sljit_sw srcw)
1288 check_sljit_emit_fop1(compiler, op, dst, dstw, src, srcw);
1289 SLJIT_COMPILE_ASSERT((SLJIT_SINGLE_OP == 0x100) && !(DOUBLE_DATA & 0x2), float_transfer_bit_error);
1291 compiler->cache_arg = 0;
1292 compiler->cache_argw = 0;
1294 if (GET_OPCODE(op) == SLJIT_CMPD) {
1295 if (dst & SLJIT_MEM) {
1296 FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG1, dst, dstw, src, srcw));
1302 if (src & SLJIT_MEM) {
1303 FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG2, src, srcw, 0, 0));
1309 /* src and dst are swapped. */
1310 if (op & SLJIT_SET_E) {
1311 FAIL_IF(push_inst(compiler, C_UEQ_fmt | FMT(op) | FT(src) | FS(dst), UNMOVABLE_INS));
1312 FAIL_IF(push_inst(compiler, CFC1 | TA(EQUAL_FLAG) | DA(FCSR_REG), EQUAL_FLAG));
1313 FAIL_IF(push_inst(compiler, SRL | TA(EQUAL_FLAG) | DA(EQUAL_FLAG) | SH_IMM(23), EQUAL_FLAG));
1314 FAIL_IF(push_inst(compiler, ANDI | SA(EQUAL_FLAG) | TA(EQUAL_FLAG) | IMM(1), EQUAL_FLAG));
1316 if (op & SLJIT_SET_S) {
1317 /* Mixing the instructions for the two checks. */
1318 FAIL_IF(push_inst(compiler, C_ULT_fmt | FMT(op) | FT(src) | FS(dst), UNMOVABLE_INS));
1319 FAIL_IF(push_inst(compiler, CFC1 | TA(ULESS_FLAG) | DA(FCSR_REG), ULESS_FLAG));
1320 FAIL_IF(push_inst(compiler, C_ULT_fmt | FMT(op) | FT(dst) | FS(src), UNMOVABLE_INS));
1321 FAIL_IF(push_inst(compiler, SRL | TA(ULESS_FLAG) | DA(ULESS_FLAG) | SH_IMM(23), ULESS_FLAG));
1322 FAIL_IF(push_inst(compiler, ANDI | SA(ULESS_FLAG) | TA(ULESS_FLAG) | IMM(1), ULESS_FLAG));
1323 FAIL_IF(push_inst(compiler, CFC1 | TA(UGREATER_FLAG) | DA(FCSR_REG), UGREATER_FLAG));
1324 FAIL_IF(push_inst(compiler, SRL | TA(UGREATER_FLAG) | DA(UGREATER_FLAG) | SH_IMM(23), UGREATER_FLAG));
1325 FAIL_IF(push_inst(compiler, ANDI | SA(UGREATER_FLAG) | TA(UGREATER_FLAG) | IMM(1), UGREATER_FLAG));
1327 return push_inst(compiler, C_UN_fmt | FMT(op) | FT(src) | FS(dst), FCSR_FCC);
1330 dst_fr = FAST_IS_REG(dst) ? (dst << 1) : TMP_FREG1;
1332 if (src & SLJIT_MEM) {
1333 FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op) | LOAD_DATA, dst_fr, src, srcw, dst, dstw));
1339 switch (GET_OPCODE(op)) {
1341 if (src != dst_fr && dst_fr != TMP_FREG1)
1342 FAIL_IF(push_inst(compiler, MOV_fmt | FMT(op) | FS(src) | FD(dst_fr), MOVABLE_INS));
1345 FAIL_IF(push_inst(compiler, NEG_fmt | FMT(op) | FS(src) | FD(dst_fr), MOVABLE_INS));
1348 FAIL_IF(push_inst(compiler, ABS_fmt | FMT(op) | FS(src) | FD(dst_fr), MOVABLE_INS));
1352 if (dst_fr == TMP_FREG1) {
1353 if (GET_OPCODE(op) == SLJIT_MOVD)
1355 FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op), dst_fr, dst, dstw, 0, 0));
1358 return SLJIT_SUCCESS;
1361 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fop2(struct sljit_compiler *compiler, sljit_si op,
1362 sljit_si dst, sljit_sw dstw,
1363 sljit_si src1, sljit_sw src1w,
1364 sljit_si src2, sljit_sw src2w)
1366 sljit_si dst_fr, flags = 0;
1369 check_sljit_emit_fop2(compiler, op, dst, dstw, src1, src1w, src2, src2w);
1371 compiler->cache_arg = 0;
1372 compiler->cache_argw = 0;
1374 dst_fr = FAST_IS_REG(dst) ? (dst << 1) : TMP_FREG2;
1376 if (src1 & SLJIT_MEM) {
1377 if (getput_arg_fast(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG1, src1, src1w)) {
1378 FAIL_IF(compiler->error);
1386 if (src2 & SLJIT_MEM) {
1387 if (getput_arg_fast(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG2, src2, src2w)) {
1388 FAIL_IF(compiler->error);
1396 if ((flags & (SLOW_SRC1 | SLOW_SRC2)) == (SLOW_SRC1 | SLOW_SRC2)) {
1397 if (!can_cache(src1, src1w, src2, src2w) && can_cache(src1, src1w, dst, dstw)) {
1398 FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG2, src2, src2w, src1, src1w));
1399 FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG1, src1, src1w, dst, dstw));
1402 FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG1, src1, src1w, src2, src2w));
1403 FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG2, src2, src2w, dst, dstw));
1406 else if (flags & SLOW_SRC1)
1407 FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG1, src1, src1w, dst, dstw));
1408 else if (flags & SLOW_SRC2)
1409 FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG2, src2, src2w, dst, dstw));
1411 if (flags & SLOW_SRC1)
1413 if (flags & SLOW_SRC2)
1416 switch (GET_OPCODE(op)) {
1418 FAIL_IF(push_inst(compiler, ADD_fmt | FMT(op) | FT(src2) | FS(src1) | FD(dst_fr), MOVABLE_INS));
1422 FAIL_IF(push_inst(compiler, SUB_fmt | FMT(op) | FT(src2) | FS(src1) | FD(dst_fr), MOVABLE_INS));
1426 FAIL_IF(push_inst(compiler, MUL_fmt | FMT(op) | FT(src2) | FS(src1) | FD(dst_fr), MOVABLE_INS));
1430 FAIL_IF(push_inst(compiler, DIV_fmt | FMT(op) | FT(src2) | FS(src1) | FD(dst_fr), MOVABLE_INS));
1434 if (dst_fr == TMP_FREG2)
1435 FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op), TMP_FREG2, dst, dstw, 0, 0));
1437 return SLJIT_SUCCESS;
1440 /* --------------------------------------------------------------------- */
1441 /* Other instructions */
1442 /* --------------------------------------------------------------------- */
1444 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fast_enter(struct sljit_compiler *compiler, sljit_si dst, sljit_sw dstw)
1447 check_sljit_emit_fast_enter(compiler, dst, dstw);
1448 ADJUST_LOCAL_OFFSET(dst, dstw);
1450 /* For UNUSED dst. Uncommon, but possible. */
1451 if (dst == SLJIT_UNUSED)
1452 return SLJIT_SUCCESS;
1454 if (FAST_IS_REG(dst))
1455 return push_inst(compiler, ADDU_W | SA(RETURN_ADDR_REG) | TA(0) | D(dst), DR(dst));
1458 return emit_op_mem(compiler, WORD_DATA, RETURN_ADDR_REG, dst, dstw);
1461 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fast_return(struct sljit_compiler *compiler, sljit_si src, sljit_sw srcw)
1464 check_sljit_emit_fast_return(compiler, src, srcw);
1465 ADJUST_LOCAL_OFFSET(src, srcw);
1467 if (FAST_IS_REG(src))
1468 FAIL_IF(push_inst(compiler, ADDU_W | S(src) | TA(0) | DA(RETURN_ADDR_REG), RETURN_ADDR_REG));
1469 else if (src & SLJIT_MEM)
1470 FAIL_IF(emit_op_mem(compiler, WORD_DATA | LOAD_DATA, RETURN_ADDR_REG, src, srcw));
1471 else if (src & SLJIT_IMM)
1472 FAIL_IF(load_immediate(compiler, RETURN_ADDR_REG, srcw));
1474 FAIL_IF(push_inst(compiler, JR | SA(RETURN_ADDR_REG), UNMOVABLE_INS));
1475 return push_inst(compiler, NOP, UNMOVABLE_INS);
1478 /* --------------------------------------------------------------------- */
1479 /* Conditional instructions */
1480 /* --------------------------------------------------------------------- */
1482 SLJIT_API_FUNC_ATTRIBUTE struct sljit_label* sljit_emit_label(struct sljit_compiler *compiler)
1484 struct sljit_label *label;
1487 check_sljit_emit_label(compiler);
1489 if (compiler->last_label && compiler->last_label->size == compiler->size)
1490 return compiler->last_label;
1492 label = (struct sljit_label*)ensure_abuf(compiler, sizeof(struct sljit_label));
1493 PTR_FAIL_IF(!label);
1494 set_label(label, compiler);
1495 compiler->delay_slot = UNMOVABLE_INS;
1499 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
1500 #define JUMP_LENGTH 4
1502 #define JUMP_LENGTH 8
1506 inst = BEQ | SA(src) | TA(0) | JUMP_LENGTH; \
1507 flags = IS_BIT26_COND; \
1510 #define BR_NZ(src) \
1511 inst = BNE | SA(src) | TA(0) | JUMP_LENGTH; \
1512 flags = IS_BIT26_COND; \
1516 inst = BC1T | JUMP_LENGTH; \
1517 flags = IS_BIT16_COND; \
1518 delay_check = FCSR_FCC;
1521 inst = BC1F | JUMP_LENGTH; \
1522 flags = IS_BIT16_COND; \
1523 delay_check = FCSR_FCC;
1525 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_jump(struct sljit_compiler *compiler, sljit_si type)
1527 struct sljit_jump *jump;
1530 sljit_si delay_check = UNMOVABLE_INS;
1533 check_sljit_emit_jump(compiler, type);
1535 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1537 set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
1542 case SLJIT_C_FLOAT_NOT_EQUAL:
1545 case SLJIT_C_NOT_EQUAL:
1546 case SLJIT_C_FLOAT_EQUAL:
1550 case SLJIT_C_FLOAT_LESS:
1553 case SLJIT_C_GREATER_EQUAL:
1554 case SLJIT_C_FLOAT_GREATER_EQUAL:
1557 case SLJIT_C_GREATER:
1558 case SLJIT_C_FLOAT_GREATER:
1559 BR_Z(UGREATER_FLAG);
1561 case SLJIT_C_LESS_EQUAL:
1562 case SLJIT_C_FLOAT_LESS_EQUAL:
1563 BR_NZ(UGREATER_FLAG);
1565 case SLJIT_C_SIG_LESS:
1568 case SLJIT_C_SIG_GREATER_EQUAL:
1571 case SLJIT_C_SIG_GREATER:
1574 case SLJIT_C_SIG_LESS_EQUAL:
1575 BR_NZ(GREATER_FLAG);
1577 case SLJIT_C_OVERFLOW:
1578 case SLJIT_C_MUL_OVERFLOW:
1579 BR_Z(OVERFLOW_FLAG);
1581 case SLJIT_C_NOT_OVERFLOW:
1582 case SLJIT_C_MUL_NOT_OVERFLOW:
1583 BR_NZ(OVERFLOW_FLAG);
1585 case SLJIT_C_FLOAT_UNORDERED:
1588 case SLJIT_C_FLOAT_ORDERED:
1592 /* Not conditional branch. */
1597 jump->flags |= flags;
1598 if (compiler->delay_slot == MOVABLE_INS || (compiler->delay_slot != UNMOVABLE_INS && compiler->delay_slot != delay_check))
1599 jump->flags |= IS_MOVABLE;
1602 PTR_FAIL_IF(push_inst(compiler, inst, UNMOVABLE_INS));
1604 PTR_FAIL_IF(emit_const(compiler, TMP_REG2, 0));
1605 if (type <= SLJIT_JUMP) {
1606 PTR_FAIL_IF(push_inst(compiler, JR | S(TMP_REG2), UNMOVABLE_INS));
1607 jump->addr = compiler->size;
1608 PTR_FAIL_IF(push_inst(compiler, NOP, UNMOVABLE_INS));
1610 SLJIT_ASSERT(DR(PIC_ADDR_REG) == 25 && PIC_ADDR_REG == TMP_REG2);
1611 /* Cannot be optimized out if type is >= CALL0. */
1612 jump->flags |= IS_JAL | (type >= SLJIT_CALL0 ? IS_CALL : 0);
1613 PTR_FAIL_IF(push_inst(compiler, JALR | S(TMP_REG2) | DA(RETURN_ADDR_REG), UNMOVABLE_INS));
1614 jump->addr = compiler->size;
1615 /* A NOP if type < CALL1. */
1616 PTR_FAIL_IF(push_inst(compiler, ADDU_W | S(SLJIT_SCRATCH_REG1) | TA(0) | DA(4), UNMOVABLE_INS));
1621 #define RESOLVE_IMM1() \
1622 if (src1 & SLJIT_IMM) { \
1624 PTR_FAIL_IF(load_immediate(compiler, DR(TMP_REG1), src1w)); \
1631 #define RESOLVE_IMM2() \
1632 if (src2 & SLJIT_IMM) { \
1634 PTR_FAIL_IF(load_immediate(compiler, DR(TMP_REG2), src2w)); \
1641 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_cmp(struct sljit_compiler *compiler, sljit_si type,
1642 sljit_si src1, sljit_sw src1w,
1643 sljit_si src2, sljit_sw src2w)
1645 struct sljit_jump *jump;
1650 check_sljit_emit_cmp(compiler, type, src1, src1w, src2, src2w);
1651 ADJUST_LOCAL_OFFSET(src1, src1w);
1652 ADJUST_LOCAL_OFFSET(src2, src2w);
1654 compiler->cache_arg = 0;
1655 compiler->cache_argw = 0;
1656 flags = ((type & SLJIT_INT_OP) ? INT_DATA : WORD_DATA) | LOAD_DATA;
1657 if (src1 & SLJIT_MEM) {
1658 PTR_FAIL_IF(emit_op_mem2(compiler, flags, DR(TMP_REG1), src1, src1w, src2, src2w));
1661 if (src2 & SLJIT_MEM) {
1662 PTR_FAIL_IF(emit_op_mem2(compiler, flags, DR(TMP_REG2), src2, src2w, 0, 0));
1666 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1668 set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
1671 if (type <= SLJIT_C_NOT_EQUAL) {
1674 jump->flags |= IS_BIT26_COND;
1675 if (compiler->delay_slot == MOVABLE_INS || (compiler->delay_slot != UNMOVABLE_INS && compiler->delay_slot != DR(src1) && compiler->delay_slot != DR(src2)))
1676 jump->flags |= IS_MOVABLE;
1677 PTR_FAIL_IF(push_inst(compiler, (type == SLJIT_C_EQUAL ? BNE : BEQ) | S(src1) | T(src2) | JUMP_LENGTH, UNMOVABLE_INS));
1679 else if (type >= SLJIT_C_SIG_LESS && (((src1 & SLJIT_IMM) && (src1w == 0)) || ((src2 & SLJIT_IMM) && (src2w == 0)))) {
1681 if ((src1 & SLJIT_IMM) && (src1w == 0)) {
1684 case SLJIT_C_SIG_LESS:
1686 jump->flags |= IS_BIT26_COND;
1688 case SLJIT_C_SIG_GREATER_EQUAL:
1690 jump->flags |= IS_BIT26_COND;
1692 case SLJIT_C_SIG_GREATER:
1694 jump->flags |= IS_BIT16_COND;
1696 case SLJIT_C_SIG_LESS_EQUAL:
1698 jump->flags |= IS_BIT16_COND;
1706 case SLJIT_C_SIG_LESS:
1708 jump->flags |= IS_BIT16_COND;
1710 case SLJIT_C_SIG_GREATER_EQUAL:
1712 jump->flags |= IS_BIT16_COND;
1714 case SLJIT_C_SIG_GREATER:
1716 jump->flags |= IS_BIT26_COND;
1718 case SLJIT_C_SIG_LESS_EQUAL:
1720 jump->flags |= IS_BIT26_COND;
1724 PTR_FAIL_IF(push_inst(compiler, inst | S(src1) | JUMP_LENGTH, UNMOVABLE_INS));
1727 if (type == SLJIT_C_LESS || type == SLJIT_C_GREATER_EQUAL || type == SLJIT_C_SIG_LESS || type == SLJIT_C_SIG_GREATER_EQUAL) {
1729 if ((src2 & SLJIT_IMM) && src2w <= SIMM_MAX && src2w >= SIMM_MIN)
1730 PTR_FAIL_IF(push_inst(compiler, (type <= SLJIT_C_LESS_EQUAL ? SLTIU : SLTI) | S(src1) | T(TMP_REG1) | IMM(src2w), DR(TMP_REG1)));
1733 PTR_FAIL_IF(push_inst(compiler, (type <= SLJIT_C_LESS_EQUAL ? SLTU : SLT) | S(src1) | T(src2) | D(TMP_REG1), DR(TMP_REG1)));
1735 type = (type == SLJIT_C_LESS || type == SLJIT_C_SIG_LESS) ? SLJIT_C_NOT_EQUAL : SLJIT_C_EQUAL;
1739 if ((src1 & SLJIT_IMM) && src1w <= SIMM_MAX && src1w >= SIMM_MIN)
1740 PTR_FAIL_IF(push_inst(compiler, (type <= SLJIT_C_LESS_EQUAL ? SLTIU : SLTI) | S(src2) | T(TMP_REG1) | IMM(src1w), DR(TMP_REG1)));
1743 PTR_FAIL_IF(push_inst(compiler, (type <= SLJIT_C_LESS_EQUAL ? SLTU : SLT) | S(src2) | T(src1) | D(TMP_REG1), DR(TMP_REG1)));
1745 type = (type == SLJIT_C_GREATER || type == SLJIT_C_SIG_GREATER) ? SLJIT_C_NOT_EQUAL : SLJIT_C_EQUAL;
1748 jump->flags |= IS_BIT26_COND;
1749 PTR_FAIL_IF(push_inst(compiler, (type == SLJIT_C_EQUAL ? BNE : BEQ) | S(TMP_REG1) | TA(0) | JUMP_LENGTH, UNMOVABLE_INS));
1752 PTR_FAIL_IF(emit_const(compiler, TMP_REG2, 0));
1753 PTR_FAIL_IF(push_inst(compiler, JR | S(TMP_REG2), UNMOVABLE_INS));
1754 jump->addr = compiler->size;
1755 PTR_FAIL_IF(push_inst(compiler, NOP, UNMOVABLE_INS));
1762 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_fcmp(struct sljit_compiler *compiler, sljit_si type,
1763 sljit_si src1, sljit_sw src1w,
1764 sljit_si src2, sljit_sw src2w)
1766 struct sljit_jump *jump;
1771 check_sljit_emit_fcmp(compiler, type, src1, src1w, src2, src2w);
1773 compiler->cache_arg = 0;
1774 compiler->cache_argw = 0;
1776 if (src1 & SLJIT_MEM) {
1777 PTR_FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(type) | LOAD_DATA, TMP_FREG1, src1, src1w, src2, src2w));
1783 if (src2 & SLJIT_MEM) {
1784 PTR_FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(type) | LOAD_DATA, TMP_FREG2, src2, src2w, 0, 0));
1790 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1792 set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
1793 jump->flags |= IS_BIT16_COND;
1795 switch (type & 0xff) {
1796 case SLJIT_C_FLOAT_EQUAL:
1800 case SLJIT_C_FLOAT_NOT_EQUAL:
1804 case SLJIT_C_FLOAT_LESS:
1808 case SLJIT_C_FLOAT_GREATER_EQUAL:
1812 case SLJIT_C_FLOAT_GREATER:
1816 case SLJIT_C_FLOAT_LESS_EQUAL:
1820 case SLJIT_C_FLOAT_UNORDERED:
1824 case SLJIT_C_FLOAT_ORDERED:
1825 default: /* Make compilers happy. */
1831 PTR_FAIL_IF(push_inst(compiler, inst | FMT(type) | FT(src2) | FS(src1), UNMOVABLE_INS));
1832 /* Intentionally the other opcode. */
1833 PTR_FAIL_IF(push_inst(compiler, (if_true ? BC1F : BC1T) | JUMP_LENGTH, UNMOVABLE_INS));
1834 PTR_FAIL_IF(emit_const(compiler, TMP_REG2, 0));
1835 PTR_FAIL_IF(push_inst(compiler, JR | S(TMP_REG2), UNMOVABLE_INS));
1836 jump->addr = compiler->size;
1837 PTR_FAIL_IF(push_inst(compiler, NOP, UNMOVABLE_INS));
1850 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_ijump(struct sljit_compiler *compiler, sljit_si type, sljit_si src, sljit_sw srcw)
1852 sljit_si src_r = TMP_REG2;
1853 struct sljit_jump *jump = NULL;
1856 check_sljit_emit_ijump(compiler, type, src, srcw);
1857 ADJUST_LOCAL_OFFSET(src, srcw);
1859 if (FAST_IS_REG(src)) {
1863 FAIL_IF(push_inst(compiler, ADDU_W | S(src) | TA(0) | D(TMP_REG2), DR(TMP_REG2)));
1866 if (type >= SLJIT_CALL0) {
1867 SLJIT_ASSERT(DR(PIC_ADDR_REG) == 25 && PIC_ADDR_REG == TMP_REG2);
1868 if (src & (SLJIT_IMM | SLJIT_MEM)) {
1869 if (src & SLJIT_IMM)
1870 FAIL_IF(load_immediate(compiler, DR(PIC_ADDR_REG), srcw));
1872 SLJIT_ASSERT(src_r == TMP_REG2 && (src & SLJIT_MEM));
1873 FAIL_IF(emit_op(compiler, SLJIT_MOV, WORD_DATA, TMP_REG2, 0, TMP_REG1, 0, src, srcw));
1875 FAIL_IF(push_inst(compiler, JALR | S(PIC_ADDR_REG) | DA(RETURN_ADDR_REG), UNMOVABLE_INS));
1876 /* We need an extra instruction in any case. */
1877 return push_inst(compiler, ADDU_W | S(SLJIT_SCRATCH_REG1) | TA(0) | DA(4), UNMOVABLE_INS);
1880 /* Register input. */
1881 if (type >= SLJIT_CALL1)
1882 FAIL_IF(push_inst(compiler, ADDU_W | S(SLJIT_SCRATCH_REG1) | TA(0) | DA(4), 4));
1883 FAIL_IF(push_inst(compiler, JALR | S(src_r) | DA(RETURN_ADDR_REG), UNMOVABLE_INS));
1884 return push_inst(compiler, ADDU_W | S(src_r) | TA(0) | D(PIC_ADDR_REG), UNMOVABLE_INS);
1887 if (src & SLJIT_IMM) {
1888 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1890 set_jump(jump, compiler, JUMP_ADDR | ((type >= SLJIT_FAST_CALL) ? IS_JAL : 0));
1891 jump->u.target = srcw;
1893 if (compiler->delay_slot != UNMOVABLE_INS)
1894 jump->flags |= IS_MOVABLE;
1896 FAIL_IF(emit_const(compiler, TMP_REG2, 0));
1898 else if (src & SLJIT_MEM)
1899 FAIL_IF(emit_op(compiler, SLJIT_MOV, WORD_DATA, TMP_REG2, 0, TMP_REG1, 0, src, srcw));
1901 FAIL_IF(push_inst(compiler, JR | S(src_r), UNMOVABLE_INS));
1903 jump->addr = compiler->size;
1904 FAIL_IF(push_inst(compiler, NOP, UNMOVABLE_INS));
1905 return SLJIT_SUCCESS;
1908 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op_flags(struct sljit_compiler *compiler, sljit_si op,
1909 sljit_si dst, sljit_sw dstw,
1910 sljit_si src, sljit_sw srcw,
1913 sljit_si sugg_dst_ar, dst_ar;
1914 sljit_si flags = GET_ALL_FLAGS(op);
1915 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
1916 # define mem_type WORD_DATA
1918 sljit_si mem_type = (op & SLJIT_INT_OP) ? (INT_DATA | SIGNED_DATA) : WORD_DATA;
1922 check_sljit_emit_op_flags(compiler, op, dst, dstw, src, srcw, type);
1923 ADJUST_LOCAL_OFFSET(dst, dstw);
1925 if (dst == SLJIT_UNUSED)
1926 return SLJIT_SUCCESS;
1928 op = GET_OPCODE(op);
1929 #if (defined SLJIT_CONFIG_MIPS_64 && SLJIT_CONFIG_MIPS_64)
1930 if (op == SLJIT_MOV_SI || op == SLJIT_MOV_UI)
1931 mem_type = INT_DATA | SIGNED_DATA;
1933 sugg_dst_ar = DR((op < SLJIT_ADD && FAST_IS_REG(dst)) ? dst : TMP_REG2);
1935 compiler->cache_arg = 0;
1936 compiler->cache_argw = 0;
1937 if (op >= SLJIT_ADD && (src & SLJIT_MEM)) {
1938 ADJUST_LOCAL_OFFSET(src, srcw);
1939 FAIL_IF(emit_op_mem2(compiler, mem_type | LOAD_DATA, DR(TMP_REG1), src, srcw, dst, dstw));
1946 case SLJIT_C_NOT_EQUAL:
1947 FAIL_IF(push_inst(compiler, SLTIU | SA(EQUAL_FLAG) | TA(sugg_dst_ar) | IMM(1), sugg_dst_ar));
1948 dst_ar = sugg_dst_ar;
1951 case SLJIT_C_GREATER_EQUAL:
1952 case SLJIT_C_FLOAT_LESS:
1953 case SLJIT_C_FLOAT_GREATER_EQUAL:
1954 dst_ar = ULESS_FLAG;
1956 case SLJIT_C_GREATER:
1957 case SLJIT_C_LESS_EQUAL:
1958 case SLJIT_C_FLOAT_GREATER:
1959 case SLJIT_C_FLOAT_LESS_EQUAL:
1960 dst_ar = UGREATER_FLAG;
1962 case SLJIT_C_SIG_LESS:
1963 case SLJIT_C_SIG_GREATER_EQUAL:
1966 case SLJIT_C_SIG_GREATER:
1967 case SLJIT_C_SIG_LESS_EQUAL:
1968 dst_ar = GREATER_FLAG;
1970 case SLJIT_C_OVERFLOW:
1971 case SLJIT_C_NOT_OVERFLOW:
1972 dst_ar = OVERFLOW_FLAG;
1974 case SLJIT_C_MUL_OVERFLOW:
1975 case SLJIT_C_MUL_NOT_OVERFLOW:
1976 FAIL_IF(push_inst(compiler, SLTIU | SA(OVERFLOW_FLAG) | TA(sugg_dst_ar) | IMM(1), sugg_dst_ar));
1977 dst_ar = sugg_dst_ar;
1978 type ^= 0x1; /* Flip type bit for the XORI below. */
1980 case SLJIT_C_FLOAT_EQUAL:
1981 case SLJIT_C_FLOAT_NOT_EQUAL:
1982 dst_ar = EQUAL_FLAG;
1985 case SLJIT_C_FLOAT_UNORDERED:
1986 case SLJIT_C_FLOAT_ORDERED:
1987 FAIL_IF(push_inst(compiler, CFC1 | TA(sugg_dst_ar) | DA(FCSR_REG), sugg_dst_ar));
1988 FAIL_IF(push_inst(compiler, SRL | TA(sugg_dst_ar) | DA(sugg_dst_ar) | SH_IMM(23), sugg_dst_ar));
1989 FAIL_IF(push_inst(compiler, ANDI | SA(sugg_dst_ar) | TA(sugg_dst_ar) | IMM(1), sugg_dst_ar));
1990 dst_ar = sugg_dst_ar;
1994 SLJIT_ASSERT_STOP();
1995 dst_ar = sugg_dst_ar;
2000 FAIL_IF(push_inst(compiler, XORI | SA(dst_ar) | TA(sugg_dst_ar) | IMM(1), sugg_dst_ar));
2001 dst_ar = sugg_dst_ar;
2004 if (op >= SLJIT_ADD) {
2005 if (DR(TMP_REG2) != dst_ar)
2006 FAIL_IF(push_inst(compiler, ADDU_W | SA(dst_ar) | TA(0) | D(TMP_REG2), DR(TMP_REG2)));
2007 return emit_op(compiler, op | flags, mem_type | CUMULATIVE_OP | LOGICAL_OP | IMM_OP | ALT_KEEP_CACHE, dst, dstw, src, srcw, TMP_REG2, 0);
2010 if (dst & SLJIT_MEM)
2011 return emit_op_mem(compiler, mem_type, dst_ar, dst, dstw);
2013 if (sugg_dst_ar != dst_ar)
2014 return push_inst(compiler, ADDU_W | SA(dst_ar) | TA(0) | DA(sugg_dst_ar), sugg_dst_ar);
2015 return SLJIT_SUCCESS;
2017 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
2022 SLJIT_API_FUNC_ATTRIBUTE struct sljit_const* sljit_emit_const(struct sljit_compiler *compiler, sljit_si dst, sljit_sw dstw, sljit_sw init_value)
2024 struct sljit_const *const_;
2028 check_sljit_emit_const(compiler, dst, dstw, init_value);
2029 ADJUST_LOCAL_OFFSET(dst, dstw);
2031 const_ = (struct sljit_const*)ensure_abuf(compiler, sizeof(struct sljit_const));
2032 PTR_FAIL_IF(!const_);
2033 set_const(const_, compiler);
2035 reg = SLOW_IS_REG(dst) ? dst : TMP_REG2;
2037 PTR_FAIL_IF(emit_const(compiler, reg, init_value));
2039 if (dst & SLJIT_MEM)
2040 PTR_FAIL_IF(emit_op(compiler, SLJIT_MOV, WORD_DATA, dst, dstw, TMP_REG1, 0, TMP_REG2, 0));