Annotation of embedaddon/pcre/sljit/sljitNativeMIPS_common.c, revision 1.1.1.4

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>