"Fossies" - the Fresh Open Source Software Archive

Member "pcre-8.44/sljit/sljitNativeARM_T2_32.c" (17 Jul 2019, 73860 Bytes) of package /linux/misc/pcre-8.44.tar.bz2:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "sljitNativeARM_T2_32.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 8.43_vs_8.44.

    1 /*
    2  *    Stack-less Just-In-Time compiler
    3  *
    4  *    Copyright Zoltan Herczeg (hzmester@freemail.hu). All rights reserved.
    5  *
    6  * Redistribution and use in source and binary forms, with or without modification, are
    7  * permitted provided that the following conditions are met:
    8  *
    9  *   1. Redistributions of source code must retain the above copyright notice, this list of
   10  *      conditions and the following disclaimer.
   11  *
   12  *   2. Redistributions in binary form must reproduce the above copyright notice, this list
   13  *      of conditions and the following disclaimer in the documentation and/or other materials
   14  *      provided with the distribution.
   15  *
   16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) AND CONTRIBUTORS ``AS IS'' AND ANY
   17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
   19  * SHALL THE COPYRIGHT HOLDER(S) OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
   21  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
   22  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   24  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   25  */
   26 
   27 SLJIT_API_FUNC_ATTRIBUTE const char* sljit_get_platform_name(void)
   28 {
   29 #ifdef __SOFTFP__
   30     return "ARM-Thumb2" SLJIT_CPUINFO " ABI:softfp";
   31 #else
   32     return "ARM-Thumb2" SLJIT_CPUINFO " ABI:hardfp";
   33 #endif
   34 }
   35 
   36 /* Length of an instruction word. */
   37 typedef sljit_u32 sljit_ins;
   38 
   39 /* Last register + 1. */
   40 #define TMP_REG1    (SLJIT_NUMBER_OF_REGISTERS + 2)
   41 #define TMP_REG2    (SLJIT_NUMBER_OF_REGISTERS + 3)
   42 #define TMP_PC      (SLJIT_NUMBER_OF_REGISTERS + 4)
   43 
   44 #define TMP_FREG1   (SLJIT_NUMBER_OF_FLOAT_REGISTERS + 1)
   45 #define TMP_FREG2   (SLJIT_NUMBER_OF_FLOAT_REGISTERS + 2)
   46 
   47 /* See sljit_emit_enter and sljit_emit_op0 if you want to change them. */
   48 static const sljit_u8 reg_map[SLJIT_NUMBER_OF_REGISTERS + 5] = {
   49     0, 0, 1, 2, 3, 11, 10, 9, 8, 7, 6, 5, 4, 13, 12, 14, 15
   50 };
   51 
   52 static const sljit_u8 freg_map[SLJIT_NUMBER_OF_FLOAT_REGISTERS + 3] = {
   53     0, 0, 1, 2, 3, 4, 5, 6, 7
   54 };
   55 
   56 #define COPY_BITS(src, from, to, bits) \
   57     ((from >= to ? (src >> (from - to)) : (src << (to - from))) & (((1 << bits) - 1) << to))
   58 
   59 /* Thumb16 encodings. */
   60 #define RD3(rd) (reg_map[rd])
   61 #define RN3(rn) (reg_map[rn] << 3)
   62 #define RM3(rm) (reg_map[rm] << 6)
   63 #define RDN3(rdn) (reg_map[rdn] << 8)
   64 #define IMM3(imm) (imm << 6)
   65 #define IMM8(imm) (imm)
   66 
   67 /* Thumb16 helpers. */
   68 #define SET_REGS44(rd, rn) \
   69     ((reg_map[rn] << 3) | (reg_map[rd] & 0x7) | ((reg_map[rd] & 0x8) << 4))
   70 #define IS_2_LO_REGS(reg1, reg2) \
   71     (reg_map[reg1] <= 7 && reg_map[reg2] <= 7)
   72 #define IS_3_LO_REGS(reg1, reg2, reg3) \
   73     (reg_map[reg1] <= 7 && reg_map[reg2] <= 7 && reg_map[reg3] <= 7)
   74 
   75 /* Thumb32 encodings. */
   76 #define RD4(rd) (reg_map[rd] << 8)
   77 #define RN4(rn) (reg_map[rn] << 16)
   78 #define RM4(rm) (reg_map[rm])
   79 #define RT4(rt) (reg_map[rt] << 12)
   80 #define DD4(dd) (freg_map[dd] << 12)
   81 #define DN4(dn) (freg_map[dn] << 16)
   82 #define DM4(dm) (freg_map[dm])
   83 #define IMM5(imm) \
   84     (COPY_BITS(imm, 2, 12, 3) | ((imm & 0x3) << 6))
   85 #define IMM12(imm) \
   86     (COPY_BITS(imm, 11, 26, 1) | COPY_BITS(imm, 8, 12, 3) | (imm & 0xff))
   87 
   88 /* --------------------------------------------------------------------- */
   89 /*  Instrucion forms                                                     */
   90 /* --------------------------------------------------------------------- */
   91 
   92 /* dot '.' changed to _
   93    I immediate form (possibly followed by number of immediate bits). */
   94 #define ADCI        0xf1400000
   95 #define ADCS        0x4140
   96 #define ADC_W       0xeb400000
   97 #define ADD     0x4400
   98 #define ADDS        0x1800
   99 #define ADDSI3      0x1c00
  100 #define ADDSI8      0x3000
  101 #define ADD_W       0xeb000000
  102 #define ADDWI       0xf2000000
  103 #define ADD_SP      0xb000
  104 #define ADD_W       0xeb000000
  105 #define ADD_WI      0xf1000000
  106 #define ANDI        0xf0000000
  107 #define ANDS        0x4000
  108 #define AND_W       0xea000000
  109 #define ASRS        0x4100
  110 #define ASRSI       0x1000
  111 #define ASR_W       0xfa40f000
  112 #define ASR_WI      0xea4f0020
  113 #define BCC     0xd000
  114 #define BICI        0xf0200000
  115 #define BKPT        0xbe00
  116 #define BLX     0x4780
  117 #define BX      0x4700
  118 #define CLZ     0xfab0f080
  119 #define CMNI_W      0xf1100f00
  120 #define CMP     0x4280
  121 #define CMPI        0x2800
  122 #define CMPI_W      0xf1b00f00
  123 #define CMP_X       0x4500
  124 #define CMP_W       0xebb00f00
  125 #define EORI        0xf0800000
  126 #define EORS        0x4040
  127 #define EOR_W       0xea800000
  128 #define IT      0xbf00
  129 #define LDRI        0xf8500800
  130 #define LSLS        0x4080
  131 #define LSLSI       0x0000
  132 #define LSL_W       0xfa00f000
  133 #define LSL_WI      0xea4f0000
  134 #define LSRS        0x40c0
  135 #define LSRSI       0x0800
  136 #define LSR_W       0xfa20f000
  137 #define LSR_WI      0xea4f0010
  138 #define MOV     0x4600
  139 #define MOVS        0x0000
  140 #define MOVSI       0x2000
  141 #define MOVT        0xf2c00000
  142 #define MOVW        0xf2400000
  143 #define MOV_W       0xea4f0000
  144 #define MOV_WI      0xf04f0000
  145 #define MUL     0xfb00f000
  146 #define MVNS        0x43c0
  147 #define MVN_W       0xea6f0000
  148 #define MVN_WI      0xf06f0000
  149 #define NOP     0xbf00
  150 #define ORNI        0xf0600000
  151 #define ORRI        0xf0400000
  152 #define ORRS        0x4300
  153 #define ORR_W       0xea400000
  154 #define POP     0xbc00
  155 #define POP_W       0xe8bd0000
  156 #define PUSH        0xb400
  157 #define PUSH_W      0xe92d0000
  158 #define RSB_WI      0xf1c00000
  159 #define RSBSI       0x4240
  160 #define SBCI        0xf1600000
  161 #define SBCS        0x4180
  162 #define SBC_W       0xeb600000
  163 #define SDIV        0xfb90f0f0
  164 #define SMULL       0xfb800000
  165 #define STR_SP      0x9000
  166 #define SUBS        0x1a00
  167 #define SUBSI3      0x1e00
  168 #define SUBSI8      0x3800
  169 #define SUB_W       0xeba00000
  170 #define SUBWI       0xf2a00000
  171 #define SUB_SP      0xb080
  172 #define SUB_WI      0xf1a00000
  173 #define SXTB        0xb240
  174 #define SXTB_W      0xfa4ff080
  175 #define SXTH        0xb200
  176 #define SXTH_W      0xfa0ff080
  177 #define TST     0x4200
  178 #define UDIV        0xfbb0f0f0
  179 #define UMULL       0xfba00000
  180 #define UXTB        0xb2c0
  181 #define UXTB_W      0xfa5ff080
  182 #define UXTH        0xb280
  183 #define UXTH_W      0xfa1ff080
  184 #define VABS_F32    0xeeb00ac0
  185 #define VADD_F32    0xee300a00
  186 #define VCMP_F32    0xeeb40a40
  187 #define VCVT_F32_S32    0xeeb80ac0
  188 #define VCVT_F64_F32    0xeeb70ac0
  189 #define VCVT_S32_F32    0xeebd0ac0
  190 #define VDIV_F32    0xee800a00
  191 #define VMOV_F32    0xeeb00a40
  192 #define VMOV        0xee000a10
  193 #define VMOV2       0xec400a10
  194 #define VMRS        0xeef1fa10
  195 #define VMUL_F32    0xee200a00
  196 #define VNEG_F32    0xeeb10a40
  197 #define VSTR_F32    0xed000a00
  198 #define VSUB_F32    0xee300a40
  199 
  200 static sljit_s32 push_inst16(struct sljit_compiler *compiler, sljit_ins inst)
  201 {
  202     sljit_u16 *ptr;
  203     SLJIT_ASSERT(!(inst & 0xffff0000));
  204 
  205     ptr = (sljit_u16*)ensure_buf(compiler, sizeof(sljit_u16));
  206     FAIL_IF(!ptr);
  207     *ptr = inst;
  208     compiler->size++;
  209     return SLJIT_SUCCESS;
  210 }
  211 
  212 static sljit_s32 push_inst32(struct sljit_compiler *compiler, sljit_ins inst)
  213 {
  214     sljit_u16 *ptr = (sljit_u16*)ensure_buf(compiler, sizeof(sljit_ins));
  215     FAIL_IF(!ptr);
  216     *ptr++ = inst >> 16;
  217     *ptr = inst;
  218     compiler->size += 2;
  219     return SLJIT_SUCCESS;
  220 }
  221 
  222 static SLJIT_INLINE sljit_s32 emit_imm32_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_uw imm)
  223 {
  224     FAIL_IF(push_inst32(compiler, MOVW | RD4(dst)
  225         | COPY_BITS(imm, 12, 16, 4) | COPY_BITS(imm, 11, 26, 1) | COPY_BITS(imm, 8, 12, 3) | (imm & 0xff)));
  226     return push_inst32(compiler, MOVT | RD4(dst)
  227         | COPY_BITS(imm, 12 + 16, 16, 4) | COPY_BITS(imm, 11 + 16, 26, 1) | COPY_BITS(imm, 8 + 16, 12, 3) | ((imm & 0xff0000) >> 16));
  228 }
  229 
  230 static SLJIT_INLINE void modify_imm32_const(sljit_u16 *inst, sljit_uw new_imm)
  231 {
  232     sljit_s32 dst = inst[1] & 0x0f00;
  233     SLJIT_ASSERT(((inst[0] & 0xfbf0) == (MOVW >> 16)) && ((inst[2] & 0xfbf0) == (MOVT >> 16)) && dst == (inst[3] & 0x0f00));
  234     inst[0] = (MOVW >> 16) | COPY_BITS(new_imm, 12, 0, 4) | COPY_BITS(new_imm, 11, 10, 1);
  235     inst[1] = dst | COPY_BITS(new_imm, 8, 12, 3) | (new_imm & 0xff);
  236     inst[2] = (MOVT >> 16) | COPY_BITS(new_imm, 12 + 16, 0, 4) | COPY_BITS(new_imm, 11 + 16, 10, 1);
  237     inst[3] = dst | COPY_BITS(new_imm, 8 + 16, 12, 3) | ((new_imm & 0xff0000) >> 16);
  238 }
  239 
  240 static SLJIT_INLINE sljit_s32 detect_jump_type(struct sljit_jump *jump, sljit_u16 *code_ptr, sljit_u16 *code, sljit_sw executable_offset)
  241 {
  242     sljit_sw diff;
  243 
  244     if (jump->flags & SLJIT_REWRITABLE_JUMP)
  245         return 0;
  246 
  247     if (jump->flags & JUMP_ADDR) {
  248         /* Branch to ARM code is not optimized yet. */
  249         if (!(jump->u.target & 0x1))
  250             return 0;
  251         diff = ((sljit_sw)jump->u.target - (sljit_sw)(code_ptr + 2) - executable_offset) >> 1;
  252     }
  253     else {
  254         SLJIT_ASSERT(jump->flags & JUMP_LABEL);
  255         diff = ((sljit_sw)(code + jump->u.label->size) - (sljit_sw)(code_ptr + 2)) >> 1;
  256     }
  257 
  258     if (jump->flags & IS_COND) {
  259         SLJIT_ASSERT(!(jump->flags & IS_BL));
  260         if (diff <= 127 && diff >= -128) {
  261             jump->flags |= PATCH_TYPE1;
  262             return 5;
  263         }
  264         if (diff <= 524287 && diff >= -524288) {
  265             jump->flags |= PATCH_TYPE2;
  266             return 4;
  267         }
  268         /* +1 comes from the prefix IT instruction. */
  269         diff--;
  270         if (diff <= 8388607 && diff >= -8388608) {
  271             jump->flags |= PATCH_TYPE3;
  272             return 3;
  273         }
  274     }
  275     else if (jump->flags & IS_BL) {
  276         if (diff <= 8388607 && diff >= -8388608) {
  277             jump->flags |= PATCH_BL;
  278             return 3;
  279         }
  280     }
  281     else {
  282         if (diff <= 1023 && diff >= -1024) {
  283             jump->flags |= PATCH_TYPE4;
  284             return 4;
  285         }
  286         if (diff <= 8388607 && diff >= -8388608) {
  287             jump->flags |= PATCH_TYPE5;
  288             return 3;
  289         }
  290     }
  291 
  292     return 0;
  293 }
  294 
  295 static SLJIT_INLINE void set_jump_instruction(struct sljit_jump *jump, sljit_sw executable_offset)
  296 {
  297     sljit_s32 type = (jump->flags >> 4) & 0xf;
  298     sljit_sw diff;
  299     sljit_u16 *jump_inst;
  300     sljit_s32 s, j1, j2;
  301 
  302     if (SLJIT_UNLIKELY(type == 0)) {
  303         modify_imm32_const((sljit_u16*)jump->addr, (jump->flags & JUMP_LABEL) ? jump->u.label->addr : jump->u.target);
  304         return;
  305     }
  306 
  307     if (jump->flags & JUMP_ADDR) {
  308         SLJIT_ASSERT(jump->u.target & 0x1);
  309         diff = ((sljit_sw)jump->u.target - (sljit_sw)(jump->addr + sizeof(sljit_u32)) - executable_offset) >> 1;
  310     }
  311     else {
  312         SLJIT_ASSERT(jump->u.label->addr & 0x1);
  313         diff = ((sljit_sw)(jump->u.label->addr) - (sljit_sw)(jump->addr + sizeof(sljit_u32)) - executable_offset) >> 1;
  314     }
  315     jump_inst = (sljit_u16*)jump->addr;
  316 
  317     switch (type) {
  318     case 1:
  319         /* Encoding T1 of 'B' instruction */
  320         SLJIT_ASSERT(diff <= 127 && diff >= -128 && (jump->flags & IS_COND));
  321         jump_inst[0] = 0xd000 | (jump->flags & 0xf00) | (diff & 0xff);
  322         return;
  323     case 2:
  324         /* Encoding T3 of 'B' instruction */
  325         SLJIT_ASSERT(diff <= 524287 && diff >= -524288 && (jump->flags & IS_COND));
  326         jump_inst[0] = 0xf000 | COPY_BITS(jump->flags, 8, 6, 4) | COPY_BITS(diff, 11, 0, 6) | COPY_BITS(diff, 19, 10, 1);
  327         jump_inst[1] = 0x8000 | COPY_BITS(diff, 17, 13, 1) | COPY_BITS(diff, 18, 11, 1) | (diff & 0x7ff);
  328         return;
  329     case 3:
  330         SLJIT_ASSERT(jump->flags & IS_COND);
  331         *jump_inst++ = IT | ((jump->flags >> 4) & 0xf0) | 0x8;
  332         diff--;
  333         type = 5;
  334         break;
  335     case 4:
  336         /* Encoding T2 of 'B' instruction */
  337         SLJIT_ASSERT(diff <= 1023 && diff >= -1024 && !(jump->flags & IS_COND));
  338         jump_inst[0] = 0xe000 | (diff & 0x7ff);
  339         return;
  340     }
  341 
  342     SLJIT_ASSERT(diff <= 8388607 && diff >= -8388608);
  343 
  344     /* Really complex instruction form for branches. */
  345     s = (diff >> 23) & 0x1;
  346     j1 = (~(diff >> 22) ^ s) & 0x1;
  347     j2 = (~(diff >> 21) ^ s) & 0x1;
  348     jump_inst[0] = 0xf000 | (s << 10) | COPY_BITS(diff, 11, 0, 10);
  349     jump_inst[1] = (j1 << 13) | (j2 << 11) | (diff & 0x7ff);
  350 
  351     /* The others have a common form. */
  352     if (type == 5) /* Encoding T4 of 'B' instruction */
  353         jump_inst[1] |= 0x9000;
  354     else if (type == 6) /* Encoding T1 of 'BL' instruction */
  355         jump_inst[1] |= 0xd000;
  356     else
  357         SLJIT_UNREACHABLE();
  358 }
  359 
  360 SLJIT_API_FUNC_ATTRIBUTE void* sljit_generate_code(struct sljit_compiler *compiler)
  361 {
  362     struct sljit_memory_fragment *buf;
  363     sljit_u16 *code;
  364     sljit_u16 *code_ptr;
  365     sljit_u16 *buf_ptr;
  366     sljit_u16 *buf_end;
  367     sljit_uw half_count;
  368     sljit_uw next_addr;
  369     sljit_sw executable_offset;
  370 
  371     struct sljit_label *label;
  372     struct sljit_jump *jump;
  373     struct sljit_const *const_;
  374     struct sljit_put_label *put_label;
  375 
  376     CHECK_ERROR_PTR();
  377     CHECK_PTR(check_sljit_generate_code(compiler));
  378     reverse_buf(compiler);
  379 
  380     code = (sljit_u16*)SLJIT_MALLOC_EXEC(compiler->size * sizeof(sljit_u16));
  381     PTR_FAIL_WITH_EXEC_IF(code);
  382     buf = compiler->buf;
  383 
  384     code_ptr = code;
  385     half_count = 0;
  386     next_addr = 0;
  387     executable_offset = SLJIT_EXEC_OFFSET(code);
  388 
  389     label = compiler->labels;
  390     jump = compiler->jumps;
  391     const_ = compiler->consts;
  392     put_label = compiler->put_labels;
  393 
  394     do {
  395         buf_ptr = (sljit_u16*)buf->memory;
  396         buf_end = buf_ptr + (buf->used_size >> 1);
  397         do {
  398             *code_ptr = *buf_ptr++;
  399             if (next_addr == half_count) {
  400                 SLJIT_ASSERT(!label || label->size >= half_count);
  401                 SLJIT_ASSERT(!jump || jump->addr >= half_count);
  402                 SLJIT_ASSERT(!const_ || const_->addr >= half_count);
  403                 SLJIT_ASSERT(!put_label || put_label->addr >= half_count);
  404 
  405                 /* These structures are ordered by their address. */
  406                 if (label && label->size == half_count) {
  407                     label->addr = ((sljit_uw)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset)) | 0x1;
  408                     label->size = code_ptr - code;
  409                     label = label->next;
  410                 }
  411                 if (jump && jump->addr == half_count) {
  412                         jump->addr = (sljit_uw)code_ptr - ((jump->flags & IS_COND) ? 10 : 8);
  413                         code_ptr -= detect_jump_type(jump, code_ptr, code, executable_offset);
  414                         jump = jump->next;
  415                 }
  416                 if (const_ && const_->addr == half_count) {
  417                     const_->addr = (sljit_uw)code_ptr;
  418                     const_ = const_->next;
  419                 }
  420                 if (put_label && put_label->addr == half_count) {
  421                     SLJIT_ASSERT(put_label->label);
  422                     put_label->addr = (sljit_uw)code_ptr;
  423                     put_label = put_label->next;
  424                 }
  425                 next_addr = compute_next_addr(label, jump, const_, put_label);
  426             }
  427             code_ptr ++;
  428             half_count ++;
  429         } while (buf_ptr < buf_end);
  430 
  431         buf = buf->next;
  432     } while (buf);
  433 
  434     if (label && label->size == half_count) {
  435         label->addr = ((sljit_uw)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset)) | 0x1;
  436         label->size = code_ptr - code;
  437         label = label->next;
  438     }
  439 
  440     SLJIT_ASSERT(!label);
  441     SLJIT_ASSERT(!jump);
  442     SLJIT_ASSERT(!const_);
  443     SLJIT_ASSERT(!put_label);
  444     SLJIT_ASSERT(code_ptr - code <= (sljit_sw)compiler->size);
  445 
  446     jump = compiler->jumps;
  447     while (jump) {
  448         set_jump_instruction(jump, executable_offset);
  449         jump = jump->next;
  450     }
  451 
  452     put_label = compiler->put_labels;
  453     while (put_label) {
  454         modify_imm32_const((sljit_u16 *)put_label->addr, put_label->label->addr);
  455         put_label = put_label->next;
  456     }
  457 
  458     compiler->error = SLJIT_ERR_COMPILED;
  459     compiler->executable_offset = executable_offset;
  460     compiler->executable_size = (code_ptr - code) * sizeof(sljit_u16);
  461 
  462     code = (sljit_u16 *)SLJIT_ADD_EXEC_OFFSET(code, executable_offset);
  463     code_ptr = (sljit_u16 *)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset);
  464 
  465     SLJIT_CACHE_FLUSH(code, code_ptr);
  466     /* Set thumb mode flag. */
  467     return (void*)((sljit_uw)code | 0x1);
  468 }
  469 
  470 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_has_cpu_feature(sljit_s32 feature_type)
  471 {
  472     switch (feature_type) {
  473     case SLJIT_HAS_FPU:
  474 #ifdef SLJIT_IS_FPU_AVAILABLE
  475         return SLJIT_IS_FPU_AVAILABLE;
  476 #else
  477         /* Available by default. */
  478         return 1;
  479 #endif
  480 
  481     case SLJIT_HAS_CLZ:
  482     case SLJIT_HAS_CMOV:
  483         return 1;
  484 
  485     default:
  486         return 0;
  487     }
  488 }
  489 
  490 /* --------------------------------------------------------------------- */
  491 /*  Core code generator functions.                                       */
  492 /* --------------------------------------------------------------------- */
  493 
  494 #define INVALID_IMM 0x80000000
  495 static sljit_uw get_imm(sljit_uw imm)
  496 {
  497     /* Thumb immediate form. */
  498     sljit_s32 counter;
  499 
  500     if (imm <= 0xff)
  501         return imm;
  502 
  503     if ((imm & 0xffff) == (imm >> 16)) {
  504         /* Some special cases. */
  505         if (!(imm & 0xff00))
  506             return (1 << 12) | (imm & 0xff);
  507         if (!(imm & 0xff))
  508             return (2 << 12) | ((imm >> 8) & 0xff);
  509         if ((imm & 0xff00) == ((imm & 0xff) << 8))
  510             return (3 << 12) | (imm & 0xff);
  511     }
  512 
  513     /* Assembly optimization: count leading zeroes? */
  514     counter = 8;
  515     if (!(imm & 0xffff0000)) {
  516         counter += 16;
  517         imm <<= 16;
  518     }
  519     if (!(imm & 0xff000000)) {
  520         counter += 8;
  521         imm <<= 8;
  522     }
  523     if (!(imm & 0xf0000000)) {
  524         counter += 4;
  525         imm <<= 4;
  526     }
  527     if (!(imm & 0xc0000000)) {
  528         counter += 2;
  529         imm <<= 2;
  530     }
  531     if (!(imm & 0x80000000)) {
  532         counter += 1;
  533         imm <<= 1;
  534     }
  535     /* Since imm >= 128, this must be true. */
  536     SLJIT_ASSERT(counter <= 31);
  537 
  538     if (imm & 0x00ffffff)
  539         return INVALID_IMM; /* Cannot be encoded. */
  540 
  541     return ((imm >> 24) & 0x7f) | COPY_BITS(counter, 4, 26, 1) | COPY_BITS(counter, 1, 12, 3) | COPY_BITS(counter, 0, 7, 1);
  542 }
  543 
  544 static sljit_s32 load_immediate(struct sljit_compiler *compiler, sljit_s32 dst, sljit_uw imm)
  545 {
  546     sljit_uw tmp;
  547 
  548     /* MOVS cannot be used since it destroy flags. */
  549 
  550     if (imm >= 0x10000) {
  551         tmp = get_imm(imm);
  552         if (tmp != INVALID_IMM)
  553             return push_inst32(compiler, MOV_WI | RD4(dst) | tmp);
  554         tmp = get_imm(~imm);
  555         if (tmp != INVALID_IMM)
  556             return push_inst32(compiler, MVN_WI | RD4(dst) | tmp);
  557     }
  558 
  559     /* set low 16 bits, set hi 16 bits to 0. */
  560     FAIL_IF(push_inst32(compiler, MOVW | RD4(dst)
  561         | COPY_BITS(imm, 12, 16, 4) | COPY_BITS(imm, 11, 26, 1) | COPY_BITS(imm, 8, 12, 3) | (imm & 0xff)));
  562 
  563     /* set hi 16 bit if needed. */
  564     if (imm >= 0x10000)
  565         return push_inst32(compiler, MOVT | RD4(dst)
  566             | COPY_BITS(imm, 12 + 16, 16, 4) | COPY_BITS(imm, 11 + 16, 26, 1) | COPY_BITS(imm, 8 + 16, 12, 3) | ((imm & 0xff0000) >> 16));
  567     return SLJIT_SUCCESS;
  568 }
  569 
  570 #define ARG1_IMM    0x0010000
  571 #define ARG2_IMM    0x0020000
  572 /* SET_FLAGS must be 0x100000 as it is also the value of S bit (can be used for optimization). */
  573 #define SET_FLAGS   0x0100000
  574 #define UNUSED_RETURN   0x0200000
  575 
  576 static sljit_s32 emit_op_imm(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 dst, sljit_uw arg1, sljit_uw arg2)
  577 {
  578     /* dst must be register, TMP_REG1
  579        arg1 must be register, imm
  580        arg2 must be register, imm */
  581     sljit_s32 reg;
  582     sljit_uw imm, nimm;
  583 
  584     if (SLJIT_UNLIKELY((flags & (ARG1_IMM | ARG2_IMM)) == (ARG1_IMM | ARG2_IMM))) {
  585         /* Both are immediates, no temporaries are used. */
  586         flags &= ~ARG1_IMM;
  587         FAIL_IF(load_immediate(compiler, TMP_REG1, arg1));
  588         arg1 = TMP_REG1;
  589     }
  590 
  591     if (flags & (ARG1_IMM | ARG2_IMM)) {
  592         reg = (flags & ARG2_IMM) ? arg1 : arg2;
  593         imm = (flags & ARG2_IMM) ? arg2 : arg1;
  594 
  595         switch (flags & 0xffff) {
  596         case SLJIT_CLZ:
  597         case SLJIT_MUL:
  598             /* No form with immediate operand. */
  599             break;
  600         case SLJIT_MOV:
  601             SLJIT_ASSERT(!(flags & SET_FLAGS) && (flags & ARG2_IMM) && arg1 == TMP_REG2);
  602             return load_immediate(compiler, dst, imm);
  603         case SLJIT_NOT:
  604             if (!(flags & SET_FLAGS))
  605                 return load_immediate(compiler, dst, ~imm);
  606             /* Since the flags should be set, we just fallback to the register mode.
  607                Although some clever things could be done here, "NOT IMM" does not worth the efforts. */
  608             break;
  609         case SLJIT_ADD:
  610             nimm = -imm;
  611             if (IS_2_LO_REGS(reg, dst)) {
  612                 if (imm <= 0x7)
  613                     return push_inst16(compiler, ADDSI3 | IMM3(imm) | RD3(dst) | RN3(reg));
  614                 if (nimm <= 0x7)
  615                     return push_inst16(compiler, SUBSI3 | IMM3(nimm) | RD3(dst) | RN3(reg));
  616                 if (reg == dst) {
  617                     if (imm <= 0xff)
  618                         return push_inst16(compiler, ADDSI8 | IMM8(imm) | RDN3(dst));
  619                     if (nimm <= 0xff)
  620                         return push_inst16(compiler, SUBSI8 | IMM8(nimm) | RDN3(dst));
  621                 }
  622             }
  623             if (!(flags & SET_FLAGS)) {
  624                 if (imm <= 0xfff)
  625                     return push_inst32(compiler, ADDWI | RD4(dst) | RN4(reg) | IMM12(imm));
  626                 if (nimm <= 0xfff)
  627                     return push_inst32(compiler, SUBWI | RD4(dst) | RN4(reg) | IMM12(nimm));
  628             }
  629             nimm = get_imm(imm);
  630             if (nimm != INVALID_IMM)
  631                 return push_inst32(compiler, ADD_WI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | nimm);
  632             nimm = get_imm(-imm);
  633             if (nimm != INVALID_IMM)
  634                 return push_inst32(compiler, SUB_WI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | nimm);
  635             break;
  636         case SLJIT_ADDC:
  637             imm = get_imm(imm);
  638             if (imm != INVALID_IMM)
  639                 return push_inst32(compiler, ADCI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
  640             break;
  641         case SLJIT_SUB:
  642             /* SUB operation can be replaced by ADD because of the negative carry flag. */
  643             if (flags & ARG1_IMM) {
  644                 if (imm == 0 && IS_2_LO_REGS(reg, dst))
  645                     return push_inst16(compiler, RSBSI | RD3(dst) | RN3(reg));
  646                 imm = get_imm(imm);
  647                 if (imm != INVALID_IMM)
  648                     return push_inst32(compiler, RSB_WI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
  649                 break;
  650             }
  651             if (flags & UNUSED_RETURN) {
  652                 if (imm <= 0xff && reg_map[reg] <= 7)
  653                     return push_inst16(compiler, CMPI | IMM8(imm) | RDN3(reg));
  654                 nimm = get_imm(imm);
  655                 if (nimm != INVALID_IMM)
  656                     return push_inst32(compiler, CMPI_W | RN4(reg) | nimm);
  657                 nimm = get_imm(-imm);
  658                 if (nimm != INVALID_IMM)
  659                     return push_inst32(compiler, CMNI_W | RN4(reg) | nimm);
  660             }
  661             nimm = -imm;
  662             if (IS_2_LO_REGS(reg, dst)) {
  663                 if (imm <= 0x7)
  664                     return push_inst16(compiler, SUBSI3 | IMM3(imm) | RD3(dst) | RN3(reg));
  665                 if (nimm <= 0x7)
  666                     return push_inst16(compiler, ADDSI3 | IMM3(nimm) | RD3(dst) | RN3(reg));
  667                 if (reg == dst) {
  668                     if (imm <= 0xff)
  669                         return push_inst16(compiler, SUBSI8 | IMM8(imm) | RDN3(dst));
  670                     if (nimm <= 0xff)
  671                         return push_inst16(compiler, ADDSI8 | IMM8(nimm) | RDN3(dst));
  672                 }
  673             }
  674             if (!(flags & SET_FLAGS)) {
  675                 if (imm <= 0xfff)
  676                     return push_inst32(compiler, SUBWI | RD4(dst) | RN4(reg) | IMM12(imm));
  677                 if (nimm <= 0xfff)
  678                     return push_inst32(compiler, ADDWI | RD4(dst) | RN4(reg) | IMM12(nimm));
  679             }
  680             nimm = get_imm(imm);
  681             if (nimm != INVALID_IMM)
  682                 return push_inst32(compiler, SUB_WI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | nimm);
  683             nimm = get_imm(-imm);
  684             if (nimm != INVALID_IMM)
  685                 return push_inst32(compiler, ADD_WI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | nimm);
  686             break;
  687         case SLJIT_SUBC:
  688             if (flags & ARG1_IMM)
  689                 break;
  690             imm = get_imm(imm);
  691             if (imm != INVALID_IMM)
  692                 return push_inst32(compiler, SBCI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
  693             break;
  694         case SLJIT_AND:
  695             nimm = get_imm(imm);
  696             if (nimm != INVALID_IMM)
  697                 return push_inst32(compiler, ANDI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | nimm);
  698             imm = get_imm(imm);
  699             if (imm != INVALID_IMM)
  700                 return push_inst32(compiler, BICI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
  701             break;
  702         case SLJIT_OR:
  703             nimm = get_imm(imm);
  704             if (nimm != INVALID_IMM)
  705                 return push_inst32(compiler, ORRI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | nimm);
  706             imm = get_imm(imm);
  707             if (imm != INVALID_IMM)
  708                 return push_inst32(compiler, ORNI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
  709             break;
  710         case SLJIT_XOR:
  711             imm = get_imm(imm);
  712             if (imm != INVALID_IMM)
  713                 return push_inst32(compiler, EORI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
  714             break;
  715         case SLJIT_SHL:
  716         case SLJIT_LSHR:
  717         case SLJIT_ASHR:
  718             if (flags & ARG1_IMM)
  719                 break;
  720             imm &= 0x1f;
  721             if (imm == 0) {
  722                 if (!(flags & SET_FLAGS))
  723                     return push_inst16(compiler, MOV | SET_REGS44(dst, reg));
  724                 if (IS_2_LO_REGS(dst, reg))
  725                     return push_inst16(compiler, MOVS | RD3(dst) | RN3(reg));
  726                 return push_inst32(compiler, MOV_W | SET_FLAGS | RD4(dst) | RM4(reg));
  727             }
  728             switch (flags & 0xffff) {
  729             case SLJIT_SHL:
  730                 if (IS_2_LO_REGS(dst, reg))
  731                     return push_inst16(compiler, LSLSI | RD3(dst) | RN3(reg) | (imm << 6));
  732                 return push_inst32(compiler, LSL_WI | (flags & SET_FLAGS) | RD4(dst) | RM4(reg) | IMM5(imm));
  733             case SLJIT_LSHR:
  734                 if (IS_2_LO_REGS(dst, reg))
  735                     return push_inst16(compiler, LSRSI | RD3(dst) | RN3(reg) | (imm << 6));
  736                 return push_inst32(compiler, LSR_WI | (flags & SET_FLAGS) | RD4(dst) | RM4(reg) | IMM5(imm));
  737             default: /* SLJIT_ASHR */
  738                 if (IS_2_LO_REGS(dst, reg))
  739                     return push_inst16(compiler, ASRSI | RD3(dst) | RN3(reg) | (imm << 6));
  740                 return push_inst32(compiler, ASR_WI | (flags & SET_FLAGS) | RD4(dst) | RM4(reg) | IMM5(imm));
  741             }
  742         default:
  743             SLJIT_UNREACHABLE();
  744             break;
  745         }
  746 
  747         if (flags & ARG2_IMM) {
  748             imm = arg2;
  749             arg2 = (arg1 == TMP_REG1) ? TMP_REG2 : TMP_REG1;
  750             FAIL_IF(load_immediate(compiler, arg2, imm));
  751         }
  752         else {
  753             imm = arg1;
  754             arg1 = (arg2 == TMP_REG1) ? TMP_REG2 : TMP_REG1;
  755             FAIL_IF(load_immediate(compiler, arg1, imm));
  756         }
  757 
  758         SLJIT_ASSERT(arg1 != arg2);
  759     }
  760 
  761     /* Both arguments are registers. */
  762     switch (flags & 0xffff) {
  763     case SLJIT_MOV:
  764     case SLJIT_MOV_U32:
  765     case SLJIT_MOV_S32:
  766     case SLJIT_MOV_P:
  767         SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG2);
  768         if (dst == arg2)
  769             return SLJIT_SUCCESS;
  770         return push_inst16(compiler, MOV | SET_REGS44(dst, arg2));
  771     case SLJIT_MOV_U8:
  772         SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG2);
  773         if (IS_2_LO_REGS(dst, arg2))
  774             return push_inst16(compiler, UXTB | RD3(dst) | RN3(arg2));
  775         return push_inst32(compiler, UXTB_W | RD4(dst) | RM4(arg2));
  776     case SLJIT_MOV_S8:
  777         SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG2);
  778         if (IS_2_LO_REGS(dst, arg2))
  779             return push_inst16(compiler, SXTB | RD3(dst) | RN3(arg2));
  780         return push_inst32(compiler, SXTB_W | RD4(dst) | RM4(arg2));
  781     case SLJIT_MOV_U16:
  782         SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG2);
  783         if (IS_2_LO_REGS(dst, arg2))
  784             return push_inst16(compiler, UXTH | RD3(dst) | RN3(arg2));
  785         return push_inst32(compiler, UXTH_W | RD4(dst) | RM4(arg2));
  786     case SLJIT_MOV_S16:
  787         SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG2);
  788         if (IS_2_LO_REGS(dst, arg2))
  789             return push_inst16(compiler, SXTH | RD3(dst) | RN3(arg2));
  790         return push_inst32(compiler, SXTH_W | RD4(dst) | RM4(arg2));
  791     case SLJIT_NOT:
  792         SLJIT_ASSERT(arg1 == TMP_REG2);
  793         if (IS_2_LO_REGS(dst, arg2))
  794             return push_inst16(compiler, MVNS | RD3(dst) | RN3(arg2));
  795         return push_inst32(compiler, MVN_W | (flags & SET_FLAGS) | RD4(dst) | RM4(arg2));
  796     case SLJIT_CLZ:
  797         SLJIT_ASSERT(arg1 == TMP_REG2);
  798         FAIL_IF(push_inst32(compiler, CLZ | RN4(arg2) | RD4(dst) | RM4(arg2)));
  799         return SLJIT_SUCCESS;
  800     case SLJIT_ADD:
  801         if (IS_3_LO_REGS(dst, arg1, arg2))
  802             return push_inst16(compiler, ADDS | RD3(dst) | RN3(arg1) | RM3(arg2));
  803         if (dst == arg1 && !(flags & SET_FLAGS))
  804             return push_inst16(compiler, ADD | SET_REGS44(dst, arg2));
  805         return push_inst32(compiler, ADD_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
  806     case SLJIT_ADDC:
  807         if (dst == arg1 && IS_2_LO_REGS(dst, arg2))
  808             return push_inst16(compiler, ADCS | RD3(dst) | RN3(arg2));
  809         return push_inst32(compiler, ADC_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
  810     case SLJIT_SUB:
  811         if (flags & UNUSED_RETURN) {
  812             if (IS_2_LO_REGS(arg1, arg2))
  813                 return push_inst16(compiler, CMP | RD3(arg1) | RN3(arg2));
  814             return push_inst16(compiler, CMP_X | SET_REGS44(arg1, arg2));
  815         }
  816         if (IS_3_LO_REGS(dst, arg1, arg2))
  817             return push_inst16(compiler, SUBS | RD3(dst) | RN3(arg1) | RM3(arg2));
  818         return push_inst32(compiler, SUB_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
  819     case SLJIT_SUBC:
  820         if (dst == arg1 && IS_2_LO_REGS(dst, arg2))
  821             return push_inst16(compiler, SBCS | RD3(dst) | RN3(arg2));
  822         return push_inst32(compiler, SBC_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
  823     case SLJIT_MUL:
  824         if (!(flags & SET_FLAGS))
  825             return push_inst32(compiler, MUL | RD4(dst) | RN4(arg1) | RM4(arg2));
  826         SLJIT_ASSERT(dst != TMP_REG2);
  827         FAIL_IF(push_inst32(compiler, SMULL | RT4(dst) | RD4(TMP_REG2) | RN4(arg1) | RM4(arg2)));
  828         /* cmp TMP_REG2, dst asr #31. */
  829         return push_inst32(compiler, CMP_W | RN4(TMP_REG2) | 0x70e0 | RM4(dst));
  830     case SLJIT_AND:
  831         if (dst == arg1 && IS_2_LO_REGS(dst, arg2))
  832             return push_inst16(compiler, ANDS | RD3(dst) | RN3(arg2));
  833         if ((flags & UNUSED_RETURN) && IS_2_LO_REGS(arg1, arg2))
  834             return push_inst16(compiler, TST | RD3(arg1) | RN3(arg2));
  835         return push_inst32(compiler, AND_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
  836     case SLJIT_OR:
  837         if (dst == arg1 && IS_2_LO_REGS(dst, arg2))
  838             return push_inst16(compiler, ORRS | RD3(dst) | RN3(arg2));
  839         return push_inst32(compiler, ORR_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
  840     case SLJIT_XOR:
  841         if (dst == arg1 && IS_2_LO_REGS(dst, arg2))
  842             return push_inst16(compiler, EORS | RD3(dst) | RN3(arg2));
  843         return push_inst32(compiler, EOR_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
  844     case SLJIT_SHL:
  845         if (dst == arg1 && IS_2_LO_REGS(dst, arg2))
  846             return push_inst16(compiler, LSLS | RD3(dst) | RN3(arg2));
  847         return push_inst32(compiler, LSL_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
  848     case SLJIT_LSHR:
  849         if (dst == arg1 && IS_2_LO_REGS(dst, arg2))
  850             return push_inst16(compiler, LSRS | RD3(dst) | RN3(arg2));
  851         return push_inst32(compiler, LSR_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
  852     case SLJIT_ASHR:
  853         if (dst == arg1 && IS_2_LO_REGS(dst, arg2))
  854             return push_inst16(compiler, ASRS | RD3(dst) | RN3(arg2));
  855         return push_inst32(compiler, ASR_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
  856     }
  857 
  858     SLJIT_UNREACHABLE();
  859     return SLJIT_SUCCESS;
  860 }
  861 
  862 #define STORE       0x01
  863 #define SIGNED      0x02
  864 
  865 #define WORD_SIZE   0x00
  866 #define BYTE_SIZE   0x04
  867 #define HALF_SIZE   0x08
  868 #define PRELOAD     0x0c
  869 
  870 #define IS_WORD_SIZE(flags)     (!(flags & (BYTE_SIZE | HALF_SIZE)))
  871 #define OFFSET_CHECK(imm, shift)    (!(argw & ~(imm << shift)))
  872 
  873 /*
  874   1st letter:
  875   w = word
  876   b = byte
  877   h = half
  878 
  879   2nd letter:
  880   s = signed
  881   u = unsigned
  882 
  883   3rd letter:
  884   l = load
  885   s = store
  886 */
  887 
  888 static const sljit_ins sljit_mem16[12] = {
  889 /* w u l */ 0x5800 /* ldr */,
  890 /* w u s */ 0x5000 /* str */,
  891 /* w s l */ 0x5800 /* ldr */,
  892 /* w s s */ 0x5000 /* str */,
  893 
  894 /* b u l */ 0x5c00 /* ldrb */,
  895 /* b u s */ 0x5400 /* strb */,
  896 /* b s l */ 0x5600 /* ldrsb */,
  897 /* b s s */ 0x5400 /* strb */,
  898 
  899 /* h u l */ 0x5a00 /* ldrh */,
  900 /* h u s */ 0x5200 /* strh */,
  901 /* h s l */ 0x5e00 /* ldrsh */,
  902 /* h s s */ 0x5200 /* strh */,
  903 };
  904 
  905 static const sljit_ins sljit_mem16_imm5[12] = {
  906 /* w u l */ 0x6800 /* ldr imm5 */,
  907 /* w u s */ 0x6000 /* str imm5 */,
  908 /* w s l */ 0x6800 /* ldr imm5 */,
  909 /* w s s */ 0x6000 /* str imm5 */,
  910 
  911 /* b u l */ 0x7800 /* ldrb imm5 */,
  912 /* b u s */ 0x7000 /* strb imm5 */,
  913 /* b s l */ 0x0000 /* not allowed */,
  914 /* b s s */ 0x7000 /* strb imm5 */,
  915 
  916 /* h u l */ 0x8800 /* ldrh imm5 */,
  917 /* h u s */ 0x8000 /* strh imm5 */,
  918 /* h s l */ 0x0000 /* not allowed */,
  919 /* h s s */ 0x8000 /* strh imm5 */,
  920 };
  921 
  922 #define MEM_IMM8    0xc00
  923 #define MEM_IMM12   0x800000
  924 static const sljit_ins sljit_mem32[13] = {
  925 /* w u l */ 0xf8500000 /* ldr.w */,
  926 /* w u s */ 0xf8400000 /* str.w */,
  927 /* w s l */ 0xf8500000 /* ldr.w */,
  928 /* w s s */ 0xf8400000 /* str.w */,
  929 
  930 /* b u l */ 0xf8100000 /* ldrb.w */,
  931 /* b u s */ 0xf8000000 /* strb.w */,
  932 /* b s l */ 0xf9100000 /* ldrsb.w */,
  933 /* b s s */ 0xf8000000 /* strb.w */,
  934 
  935 /* h u l */ 0xf8300000 /* ldrh.w */,
  936 /* h u s */ 0xf8200000 /* strsh.w */,
  937 /* h s l */ 0xf9300000 /* ldrsh.w */,
  938 /* h s s */ 0xf8200000 /* strsh.w */,
  939 
  940 /* p u l */ 0xf8100000 /* pld */,
  941 };
  942 
  943 /* Helper function. Dst should be reg + value, using at most 1 instruction, flags does not set. */
  944 static sljit_s32 emit_set_delta(struct sljit_compiler *compiler, sljit_s32 dst, sljit_s32 reg, sljit_sw value)
  945 {
  946     if (value >= 0) {
  947         if (value <= 0xfff)
  948             return push_inst32(compiler, ADDWI | RD4(dst) | RN4(reg) | IMM12(value));
  949         value = get_imm(value);
  950         if (value != INVALID_IMM)
  951             return push_inst32(compiler, ADD_WI | RD4(dst) | RN4(reg) | value);
  952     }
  953     else {
  954         value = -value;
  955         if (value <= 0xfff)
  956             return push_inst32(compiler, SUBWI | RD4(dst) | RN4(reg) | IMM12(value));
  957         value = get_imm(value);
  958         if (value != INVALID_IMM)
  959             return push_inst32(compiler, SUB_WI | RD4(dst) | RN4(reg) | value);
  960     }
  961     return SLJIT_ERR_UNSUPPORTED;
  962 }
  963 
  964 static SLJIT_INLINE sljit_s32 emit_op_mem(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg,
  965     sljit_s32 arg, sljit_sw argw, sljit_s32 tmp_reg)
  966 {
  967     sljit_s32 other_r;
  968     sljit_uw tmp;
  969 
  970     SLJIT_ASSERT(arg & SLJIT_MEM);
  971     SLJIT_ASSERT((arg & REG_MASK) != tmp_reg);
  972     arg &= ~SLJIT_MEM;
  973 
  974     if (SLJIT_UNLIKELY(!(arg & REG_MASK))) {
  975         tmp = get_imm(argw & ~0xfff);
  976         if (tmp != INVALID_IMM) {
  977             FAIL_IF(push_inst32(compiler, MOV_WI | RD4(tmp_reg) | tmp));
  978             return push_inst32(compiler, sljit_mem32[flags] | MEM_IMM12 | RT4(reg) | RN4(tmp_reg) | (argw & 0xfff));
  979         }
  980 
  981         FAIL_IF(load_immediate(compiler, tmp_reg, argw));
  982         if (IS_2_LO_REGS(reg, tmp_reg) && sljit_mem16_imm5[flags])
  983             return push_inst16(compiler, sljit_mem16_imm5[flags] | RD3(reg) | RN3(tmp_reg));
  984         return push_inst32(compiler, sljit_mem32[flags] | MEM_IMM12 | RT4(reg) | RN4(tmp_reg));
  985     }
  986 
  987     if (SLJIT_UNLIKELY(arg & OFFS_REG_MASK)) {
  988         argw &= 0x3;
  989         other_r = OFFS_REG(arg);
  990         arg &= 0xf;
  991 
  992         if (!argw && IS_3_LO_REGS(reg, arg, other_r))
  993             return push_inst16(compiler, sljit_mem16[flags] | RD3(reg) | RN3(arg) | RM3(other_r));
  994         return push_inst32(compiler, sljit_mem32[flags] | RT4(reg) | RN4(arg) | RM4(other_r) | (argw << 4));
  995     }
  996 
  997     if (argw > 0xfff) {
  998         tmp = get_imm(argw & ~0xfff);
  999         if (tmp != INVALID_IMM) {
 1000             push_inst32(compiler, ADD_WI | RD4(tmp_reg) | RN4(arg) | tmp);
 1001             arg = tmp_reg;
 1002             argw = argw & 0xfff;
 1003         }
 1004     }
 1005     else if (argw < -0xff) {
 1006         tmp = get_imm(-argw & ~0xff);
 1007         if (tmp != INVALID_IMM) {
 1008             push_inst32(compiler, SUB_WI | RD4(tmp_reg) | RN4(arg) | tmp);
 1009             arg = tmp_reg;
 1010             argw = -(-argw & 0xff);
 1011         }
 1012     }
 1013 
 1014     if (IS_2_LO_REGS(reg, arg) && sljit_mem16_imm5[flags]) {
 1015         tmp = 3;
 1016         if (IS_WORD_SIZE(flags)) {
 1017             if (OFFSET_CHECK(0x1f, 2))
 1018                 tmp = 2;
 1019         }
 1020         else if (flags & BYTE_SIZE)
 1021         {
 1022             if (OFFSET_CHECK(0x1f, 0))
 1023                 tmp = 0;
 1024         }
 1025         else {
 1026             SLJIT_ASSERT(flags & HALF_SIZE);
 1027             if (OFFSET_CHECK(0x1f, 1))
 1028                 tmp = 1;
 1029         }
 1030 
 1031         if (tmp < 3)
 1032             return push_inst16(compiler, sljit_mem16_imm5[flags] | RD3(reg) | RN3(arg) | (argw << (6 - tmp)));
 1033     }
 1034     else if (SLJIT_UNLIKELY(arg == SLJIT_SP) && IS_WORD_SIZE(flags) && OFFSET_CHECK(0xff, 2) && reg_map[reg] <= 7) {
 1035         /* SP based immediate. */
 1036         return push_inst16(compiler, STR_SP | ((flags & STORE) ? 0 : 0x800) | RDN3(reg) | (argw >> 2));
 1037     }
 1038 
 1039     if (argw >= 0 && argw <= 0xfff)
 1040         return push_inst32(compiler, sljit_mem32[flags] | MEM_IMM12 | RT4(reg) | RN4(arg) | argw);
 1041     else if (argw < 0 && argw >= -0xff)
 1042         return push_inst32(compiler, sljit_mem32[flags] | MEM_IMM8 | RT4(reg) | RN4(arg) | -argw);
 1043 
 1044     SLJIT_ASSERT(arg != tmp_reg);
 1045 
 1046     FAIL_IF(load_immediate(compiler, tmp_reg, argw));
 1047     if (IS_3_LO_REGS(reg, arg, tmp_reg))
 1048         return push_inst16(compiler, sljit_mem16[flags] | RD3(reg) | RN3(arg) | RM3(tmp_reg));
 1049     return push_inst32(compiler, sljit_mem32[flags] | RT4(reg) | RN4(arg) | RM4(tmp_reg));
 1050 }
 1051 
 1052 /* --------------------------------------------------------------------- */
 1053 /*  Entry, exit                                                          */
 1054 /* --------------------------------------------------------------------- */
 1055 
 1056 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_enter(struct sljit_compiler *compiler,
 1057     sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
 1058     sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
 1059 {
 1060     sljit_s32 args, size, i, tmp;
 1061     sljit_ins push = 0;
 1062 #ifdef _WIN32
 1063     sljit_uw imm;
 1064 #endif
 1065 
 1066     CHECK_ERROR();
 1067     CHECK(check_sljit_emit_enter(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size));
 1068     set_emit_enter(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size);
 1069 
 1070     tmp = saveds < SLJIT_NUMBER_OF_SAVED_REGISTERS ? (SLJIT_S0 + 1 - saveds) : SLJIT_FIRST_SAVED_REG;
 1071     for (i = SLJIT_S0; i >= tmp; i--)
 1072         push |= 1 << reg_map[i];
 1073 
 1074     for (i = scratches; i >= SLJIT_FIRST_SAVED_REG; i--)
 1075         push |= 1 << reg_map[i];
 1076 
 1077     FAIL_IF((push & 0xff00)
 1078         ? push_inst32(compiler, PUSH_W | (1 << 14) | push)
 1079         : push_inst16(compiler, PUSH | (1 << 8) | push));
 1080 
 1081     /* Stack must be aligned to 8 bytes: (LR, R4) */
 1082     size = GET_SAVED_REGISTERS_SIZE(scratches, saveds, 1);
 1083     local_size = ((size + local_size + 7) & ~7) - size;
 1084     compiler->local_size = local_size;
 1085 
 1086 #ifdef _WIN32
 1087     if (local_size >= 256) {
 1088         if (local_size > 4096)
 1089             imm = get_imm(4096);
 1090         else
 1091             imm = get_imm(local_size & ~0xff);
 1092 
 1093         SLJIT_ASSERT(imm != INVALID_IMM);
 1094         FAIL_IF(push_inst32(compiler, SUB_WI | RD4(TMP_REG1) | RN4(SLJIT_SP) | imm));
 1095     }
 1096 #else
 1097     if (local_size > 0) {
 1098         if (local_size <= (127 << 2))
 1099             FAIL_IF(push_inst16(compiler, SUB_SP | (local_size >> 2)));
 1100         else
 1101             FAIL_IF(emit_op_imm(compiler, SLJIT_SUB | ARG2_IMM, SLJIT_SP, SLJIT_SP, local_size));
 1102     }
 1103 #endif
 1104 
 1105     args = get_arg_count(arg_types);
 1106 
 1107     if (args >= 1)
 1108         FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(SLJIT_S0, SLJIT_R0)));
 1109     if (args >= 2)
 1110         FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(SLJIT_S1, SLJIT_R1)));
 1111     if (args >= 3)
 1112         FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(SLJIT_S2, SLJIT_R2)));
 1113 
 1114 #ifdef _WIN32
 1115     if (local_size >= 256) {
 1116         if (local_size > 4096) {
 1117             imm = get_imm(4096);
 1118             SLJIT_ASSERT(imm != INVALID_IMM);
 1119 
 1120             if (local_size < 4 * 4096) {
 1121                 if (local_size > 2 * 4096) {
 1122                     FAIL_IF(push_inst32(compiler, LDRI | 0x400 | RT4(TMP_REG2) | RN4(TMP_REG1)));
 1123                     FAIL_IF(push_inst32(compiler, SUB_WI | RD4(TMP_REG1) | RN4(TMP_REG1) | imm));
 1124                     local_size -= 4096;
 1125                 }
 1126 
 1127                 if (local_size > 2 * 4096) {
 1128                     FAIL_IF(push_inst32(compiler, LDRI | 0x400 | RT4(TMP_REG2) | RN4(TMP_REG1)));
 1129                     FAIL_IF(push_inst32(compiler, SUB_WI | RD4(TMP_REG1) | RN4(TMP_REG1) | imm));
 1130                     local_size -= 4096;
 1131                 }
 1132 
 1133                 FAIL_IF(push_inst32(compiler, LDRI | 0x400 | RT4(TMP_REG2) | RN4(TMP_REG1)));
 1134                 local_size -= 4096;
 1135 
 1136                 SLJIT_ASSERT(local_size > 0);
 1137             }
 1138             else {
 1139                 FAIL_IF(load_immediate(compiler, SLJIT_R3, (local_size >> 12) - 1));
 1140                 FAIL_IF(push_inst32(compiler, LDRI | 0x400 | RT4(TMP_REG2) | RN4(TMP_REG1)));
 1141                 FAIL_IF(push_inst32(compiler, SUB_WI | RD4(TMP_REG1) | RN4(TMP_REG1) | imm));
 1142                 SLJIT_ASSERT(reg_map[SLJIT_R3] < 7);
 1143                 FAIL_IF(push_inst16(compiler, SUBSI8 | RDN3(SLJIT_R3) | 1));
 1144                 FAIL_IF(push_inst16(compiler, BCC | (0x1 << 8) /* not-equal */ | (-7 & 0xff)));
 1145 
 1146                 local_size &= 0xfff;
 1147 
 1148                 if (local_size != 0)
 1149                     FAIL_IF(push_inst32(compiler, LDRI | 0x400 | RT4(TMP_REG2) | RN4(TMP_REG1)));
 1150             }
 1151 
 1152             if (local_size >= 256) {
 1153                 imm = get_imm(local_size & ~0xff);
 1154                 SLJIT_ASSERT(imm != INVALID_IMM);
 1155 
 1156                 FAIL_IF(push_inst32(compiler, SUB_WI | RD4(TMP_REG1) | RN4(TMP_REG1) | imm));
 1157             }
 1158         }
 1159 
 1160         local_size &= 0xff;
 1161         FAIL_IF(push_inst32(compiler, LDRI | 0x400 | (local_size > 0 ? 0x100 : 0) | RT4(TMP_REG2) | RN4(TMP_REG1) | local_size));
 1162 
 1163         FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(SLJIT_SP, TMP_REG1)));
 1164     }
 1165     else if (local_size > 0)
 1166         FAIL_IF(push_inst32(compiler, LDRI | 0x500 | RT4(TMP_REG1) | RN4(SLJIT_SP) | local_size));
 1167 #endif
 1168 
 1169     return SLJIT_SUCCESS;
 1170 }
 1171 
 1172 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_set_context(struct sljit_compiler *compiler,
 1173     sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
 1174     sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
 1175 {
 1176     sljit_s32 size;
 1177 
 1178     CHECK_ERROR();
 1179     CHECK(check_sljit_set_context(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size));
 1180     set_set_context(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size);
 1181 
 1182     size = GET_SAVED_REGISTERS_SIZE(scratches, saveds, 1);
 1183     compiler->local_size = ((size + local_size + 7) & ~7) - size;
 1184     return SLJIT_SUCCESS;
 1185 }
 1186 
 1187 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_return(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 src, sljit_sw srcw)
 1188 {
 1189     sljit_s32 i, tmp;
 1190     sljit_ins pop = 0;
 1191 
 1192     CHECK_ERROR();
 1193     CHECK(check_sljit_emit_return(compiler, op, src, srcw));
 1194 
 1195     FAIL_IF(emit_mov_before_return(compiler, op, src, srcw));
 1196 
 1197     if (compiler->local_size > 0) {
 1198         if (compiler->local_size <= (127 << 2))
 1199             FAIL_IF(push_inst16(compiler, ADD_SP | (compiler->local_size >> 2)));
 1200         else
 1201             FAIL_IF(emit_op_imm(compiler, SLJIT_ADD | ARG2_IMM, SLJIT_SP, SLJIT_SP, compiler->local_size));
 1202     }
 1203 
 1204     tmp = compiler->saveds < SLJIT_NUMBER_OF_SAVED_REGISTERS ? (SLJIT_S0 + 1 - compiler->saveds) : SLJIT_FIRST_SAVED_REG;
 1205     for (i = SLJIT_S0; i >= tmp; i--)
 1206         pop |= 1 << reg_map[i];
 1207 
 1208     for (i = compiler->scratches; i >= SLJIT_FIRST_SAVED_REG; i--)
 1209         pop |= 1 << reg_map[i];
 1210 
 1211     return (pop & 0xff00)
 1212         ? push_inst32(compiler, POP_W | (1 << 15) | pop)
 1213         : push_inst16(compiler, POP | (1 << 8) | pop);
 1214 }
 1215 
 1216 /* --------------------------------------------------------------------- */
 1217 /*  Operators                                                            */
 1218 /* --------------------------------------------------------------------- */
 1219 
 1220 #if !(defined __ARM_FEATURE_IDIV) && !(defined __ARM_ARCH_EXT_IDIV__)
 1221 
 1222 #ifdef __cplusplus
 1223 extern "C" {
 1224 #endif
 1225 
 1226 #ifdef _WIN32
 1227 extern unsigned long long __rt_udiv(unsigned int denominator, unsigned int numerator);
 1228 extern long long __rt_sdiv(int denominator, int numerator);
 1229 #elif defined(__GNUC__)
 1230 extern unsigned int __aeabi_uidivmod(unsigned int numerator, int unsigned denominator);
 1231 extern int __aeabi_idivmod(int numerator, int denominator);
 1232 #else
 1233 #error "Software divmod functions are needed"
 1234 #endif
 1235 
 1236 #ifdef __cplusplus
 1237 }
 1238 #endif
 1239 
 1240 #endif /* !__ARM_FEATURE_IDIV && !__ARM_ARCH_EXT_IDIV__ */
 1241 
 1242 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op0(struct sljit_compiler *compiler, sljit_s32 op)
 1243 {
 1244 #if !(defined __ARM_FEATURE_IDIV) && !(defined __ARM_ARCH_EXT_IDIV__)
 1245     sljit_sw saved_reg_list[3];
 1246     sljit_sw saved_reg_count;
 1247 #endif
 1248 
 1249     CHECK_ERROR();
 1250     CHECK(check_sljit_emit_op0(compiler, op));
 1251 
 1252     op = GET_OPCODE(op);
 1253     switch (op) {
 1254     case SLJIT_BREAKPOINT:
 1255         return push_inst16(compiler, BKPT);
 1256     case SLJIT_NOP:
 1257         return push_inst16(compiler, NOP);
 1258     case SLJIT_LMUL_UW:
 1259     case SLJIT_LMUL_SW:
 1260         return push_inst32(compiler, (op == SLJIT_LMUL_UW ? UMULL : SMULL)
 1261             | (reg_map[SLJIT_R1] << 8)
 1262             | (reg_map[SLJIT_R0] << 12)
 1263             | (reg_map[SLJIT_R0] << 16)
 1264             | reg_map[SLJIT_R1]);
 1265 #if (defined __ARM_FEATURE_IDIV) || (defined __ARM_ARCH_EXT_IDIV__)
 1266     case SLJIT_DIVMOD_UW:
 1267     case SLJIT_DIVMOD_SW:
 1268         FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(TMP_REG1, SLJIT_R0)));
 1269         FAIL_IF(push_inst32(compiler, (op == SLJIT_DIVMOD_UW ? UDIV : SDIV) | RD4(SLJIT_R0) | RN4(SLJIT_R0) | RM4(SLJIT_R1)));
 1270         FAIL_IF(push_inst32(compiler, MUL | RD4(SLJIT_R1) | RN4(SLJIT_R0) | RM4(SLJIT_R1)));
 1271         return push_inst32(compiler, SUB_W | RD4(SLJIT_R1) | RN4(TMP_REG1) | RM4(SLJIT_R1));
 1272     case SLJIT_DIV_UW:
 1273     case SLJIT_DIV_SW:
 1274         return push_inst32(compiler, (op == SLJIT_DIV_UW ? UDIV : SDIV) | RD4(SLJIT_R0) | RN4(SLJIT_R0) | RM4(SLJIT_R1));
 1275 #else /* !__ARM_FEATURE_IDIV && !__ARM_ARCH_EXT_IDIV__ */
 1276     case SLJIT_DIVMOD_UW:
 1277     case SLJIT_DIVMOD_SW:
 1278     case SLJIT_DIV_UW:
 1279     case SLJIT_DIV_SW:
 1280         SLJIT_COMPILE_ASSERT((SLJIT_DIVMOD_UW & 0x2) == 0 && SLJIT_DIV_UW - 0x2 == SLJIT_DIVMOD_UW, bad_div_opcode_assignments);
 1281         SLJIT_ASSERT(reg_map[2] == 1 && reg_map[3] == 2 && reg_map[4] == 3);
 1282 
 1283         saved_reg_count = 0;
 1284         if (compiler->scratches >= 4)
 1285             saved_reg_list[saved_reg_count++] = 3;
 1286         if (compiler->scratches >= 3)
 1287             saved_reg_list[saved_reg_count++] = 2;
 1288         if (op >= SLJIT_DIV_UW)
 1289             saved_reg_list[saved_reg_count++] = 1;
 1290 
 1291         if (saved_reg_count > 0) {
 1292             FAIL_IF(push_inst32(compiler, 0xf84d0d00 | (saved_reg_count >= 3 ? 16 : 8)
 1293                         | (saved_reg_list[0] << 12) /* str rX, [sp, #-8/-16]! */));
 1294             if (saved_reg_count >= 2) {
 1295                 SLJIT_ASSERT(saved_reg_list[1] < 8);
 1296                 FAIL_IF(push_inst16(compiler, 0x9001 | (saved_reg_list[1] << 8) /* str rX, [sp, #4] */));
 1297             }
 1298             if (saved_reg_count >= 3) {
 1299                 SLJIT_ASSERT(saved_reg_list[2] < 8);
 1300                 FAIL_IF(push_inst16(compiler, 0x9002 | (saved_reg_list[2] << 8) /* str rX, [sp, #8] */));
 1301             }
 1302         }
 1303 
 1304 #ifdef _WIN32
 1305         FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(TMP_REG1, SLJIT_R0)));
 1306         FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(SLJIT_R0, SLJIT_R1)));
 1307         FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(SLJIT_R1, TMP_REG1)));
 1308         FAIL_IF(sljit_emit_ijump(compiler, SLJIT_FAST_CALL, SLJIT_IMM,
 1309             ((op | 0x2) == SLJIT_DIV_UW ? SLJIT_FUNC_OFFSET(__rt_udiv) : SLJIT_FUNC_OFFSET(__rt_sdiv))));
 1310 #elif defined(__GNUC__)
 1311         FAIL_IF(sljit_emit_ijump(compiler, SLJIT_FAST_CALL, SLJIT_IMM,
 1312             ((op | 0x2) == SLJIT_DIV_UW ? SLJIT_FUNC_OFFSET(__aeabi_uidivmod) : SLJIT_FUNC_OFFSET(__aeabi_idivmod))));
 1313 #else
 1314 #error "Software divmod functions are needed"
 1315 #endif
 1316 
 1317         if (saved_reg_count > 0) {
 1318             if (saved_reg_count >= 3) {
 1319                 SLJIT_ASSERT(saved_reg_list[2] < 8);
 1320                 FAIL_IF(push_inst16(compiler, 0x9802 | (saved_reg_list[2] << 8) /* ldr rX, [sp, #8] */));
 1321             }
 1322             if (saved_reg_count >= 2) {
 1323                 SLJIT_ASSERT(saved_reg_list[1] < 8);
 1324                 FAIL_IF(push_inst16(compiler, 0x9801 | (saved_reg_list[1] << 8) /* ldr rX, [sp, #4] */));
 1325             }
 1326             return push_inst32(compiler, 0xf85d0b00 | (saved_reg_count >= 3 ? 16 : 8)
 1327                         | (saved_reg_list[0] << 12) /* ldr rX, [sp], #8/16 */);
 1328         }
 1329         return SLJIT_SUCCESS;
 1330 #endif /* __ARM_FEATURE_IDIV || __ARM_ARCH_EXT_IDIV__ */
 1331     }
 1332 
 1333     return SLJIT_SUCCESS;
 1334 }
 1335 
 1336 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op1(struct sljit_compiler *compiler, sljit_s32 op,
 1337     sljit_s32 dst, sljit_sw dstw,
 1338     sljit_s32 src, sljit_sw srcw)
 1339 {
 1340     sljit_s32 dst_r, flags;
 1341     sljit_s32 op_flags = GET_ALL_FLAGS(op);
 1342 
 1343     CHECK_ERROR();
 1344     CHECK(check_sljit_emit_op1(compiler, op, dst, dstw, src, srcw));
 1345     ADJUST_LOCAL_OFFSET(dst, dstw);
 1346     ADJUST_LOCAL_OFFSET(src, srcw);
 1347 
 1348     if (dst == SLJIT_UNUSED && !HAS_FLAGS(op)) {
 1349         /* Since TMP_PC has index 15, IS_2_LO_REGS and IS_3_LO_REGS checks always fail. */
 1350         if (op <= SLJIT_MOV_P && (src & SLJIT_MEM))
 1351             return emit_op_mem(compiler, PRELOAD, TMP_PC, src, srcw, TMP_REG1);
 1352         return SLJIT_SUCCESS;
 1353     }
 1354 
 1355     dst_r = SLOW_IS_REG(dst) ? dst : TMP_REG1;
 1356 
 1357     op = GET_OPCODE(op);
 1358     if (op >= SLJIT_MOV && op <= SLJIT_MOV_P) {
 1359         switch (op) {
 1360         case SLJIT_MOV:
 1361         case SLJIT_MOV_U32:
 1362         case SLJIT_MOV_S32:
 1363         case SLJIT_MOV_P:
 1364             flags = WORD_SIZE;
 1365             break;
 1366         case SLJIT_MOV_U8:
 1367             flags = BYTE_SIZE;
 1368             if (src & SLJIT_IMM)
 1369                 srcw = (sljit_u8)srcw;
 1370             break;
 1371         case SLJIT_MOV_S8:
 1372             flags = BYTE_SIZE | SIGNED;
 1373             if (src & SLJIT_IMM)
 1374                 srcw = (sljit_s8)srcw;
 1375             break;
 1376         case SLJIT_MOV_U16:
 1377             flags = HALF_SIZE;
 1378             if (src & SLJIT_IMM)
 1379                 srcw = (sljit_u16)srcw;
 1380             break;
 1381         case SLJIT_MOV_S16:
 1382             flags = HALF_SIZE | SIGNED;
 1383             if (src & SLJIT_IMM)
 1384                 srcw = (sljit_s16)srcw;
 1385             break;
 1386         default:
 1387             SLJIT_UNREACHABLE();
 1388             flags = 0;
 1389             break;
 1390         }
 1391 
 1392         if (src & SLJIT_IMM)
 1393             FAIL_IF(emit_op_imm(compiler, SLJIT_MOV | ARG2_IMM, dst_r, TMP_REG2, srcw));
 1394         else if (src & SLJIT_MEM) {
 1395             FAIL_IF(emit_op_mem(compiler, flags, dst_r, src, srcw, TMP_REG1));
 1396         } else {
 1397             if (dst_r != TMP_REG1)
 1398                 return emit_op_imm(compiler, op, dst_r, TMP_REG2, src);
 1399             dst_r = src;
 1400         }
 1401 
 1402         if (!(dst & SLJIT_MEM))
 1403             return SLJIT_SUCCESS;
 1404 
 1405         return emit_op_mem(compiler, flags | STORE, dst_r, dst, dstw, TMP_REG2);
 1406     }
 1407 
 1408     if (op == SLJIT_NEG) {
 1409 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
 1410             || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
 1411         compiler->skip_checks = 1;
 1412 #endif
 1413         return sljit_emit_op2(compiler, SLJIT_SUB | op_flags, dst, dstw, SLJIT_IMM, 0, src, srcw);
 1414     }
 1415 
 1416     flags = HAS_FLAGS(op_flags) ? SET_FLAGS : 0;
 1417 
 1418     if (src & SLJIT_MEM) {
 1419         FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG1, src, srcw, TMP_REG1));
 1420         src = TMP_REG1;
 1421     }
 1422 
 1423     emit_op_imm(compiler, flags | op, dst_r, TMP_REG2, src);
 1424 
 1425     if (SLJIT_UNLIKELY(dst & SLJIT_MEM))
 1426         return emit_op_mem(compiler, flags | STORE, dst_r, dst, dstw, TMP_REG2);
 1427     return SLJIT_SUCCESS;
 1428 }
 1429 
 1430 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op2(struct sljit_compiler *compiler, sljit_s32 op,
 1431     sljit_s32 dst, sljit_sw dstw,
 1432     sljit_s32 src1, sljit_sw src1w,
 1433     sljit_s32 src2, sljit_sw src2w)
 1434 {
 1435     sljit_s32 dst_reg, flags, src2_reg;
 1436 
 1437     CHECK_ERROR();
 1438     CHECK(check_sljit_emit_op2(compiler, op, dst, dstw, src1, src1w, src2, src2w));
 1439     ADJUST_LOCAL_OFFSET(dst, dstw);
 1440     ADJUST_LOCAL_OFFSET(src1, src1w);
 1441     ADJUST_LOCAL_OFFSET(src2, src2w);
 1442 
 1443     if (dst == SLJIT_UNUSED && !HAS_FLAGS(op))
 1444         return SLJIT_SUCCESS;
 1445 
 1446     dst_reg = SLOW_IS_REG(dst) ? dst : TMP_REG1;
 1447     flags = HAS_FLAGS(op) ? SET_FLAGS : 0;
 1448 
 1449     if (src1 & SLJIT_IMM)
 1450         flags |= ARG1_IMM;
 1451     else if (src1 & SLJIT_MEM) {
 1452         emit_op_mem(compiler, WORD_SIZE, TMP_REG1, src1, src1w, TMP_REG1);
 1453         src1w = TMP_REG1;
 1454     }
 1455     else
 1456         src1w = src1;
 1457 
 1458     if (src2 & SLJIT_IMM)
 1459         flags |= ARG2_IMM;
 1460     else if (src2 & SLJIT_MEM) {
 1461         src2_reg = (!(flags & ARG1_IMM) && (src1w == TMP_REG1)) ? TMP_REG2 : TMP_REG1;
 1462         emit_op_mem(compiler, WORD_SIZE, src2_reg, src2, src2w, src2_reg);
 1463         src2w = src2_reg;
 1464     }
 1465     else
 1466         src2w = src2;
 1467 
 1468     if (dst == SLJIT_UNUSED)
 1469         flags |= UNUSED_RETURN;
 1470 
 1471     emit_op_imm(compiler, flags | GET_OPCODE(op), dst_reg, src1w, src2w);
 1472 
 1473     if (!(dst & SLJIT_MEM))
 1474         return SLJIT_SUCCESS;
 1475     return emit_op_mem(compiler, WORD_SIZE | STORE, dst_reg, dst, dstw, TMP_REG2);
 1476 }
 1477 
 1478 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_register_index(sljit_s32 reg)
 1479 {
 1480     CHECK_REG_INDEX(check_sljit_get_register_index(reg));
 1481     return reg_map[reg];
 1482 }
 1483 
 1484 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_float_register_index(sljit_s32 reg)
 1485 {
 1486     CHECK_REG_INDEX(check_sljit_get_float_register_index(reg));
 1487     return (freg_map[reg] << 1);
 1488 }
 1489 
 1490 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_custom(struct sljit_compiler *compiler,
 1491     void *instruction, sljit_s32 size)
 1492 {
 1493     CHECK_ERROR();
 1494     CHECK(check_sljit_emit_op_custom(compiler, instruction, size));
 1495 
 1496     if (size == 2)
 1497         return push_inst16(compiler, *(sljit_u16*)instruction);
 1498     return push_inst32(compiler, *(sljit_ins*)instruction);
 1499 }
 1500 
 1501 /* --------------------------------------------------------------------- */
 1502 /*  Floating point operators                                             */
 1503 /* --------------------------------------------------------------------- */
 1504 
 1505 #define FPU_LOAD (1 << 20)
 1506 
 1507 static sljit_s32 emit_fop_mem(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg, sljit_s32 arg, sljit_sw argw)
 1508 {
 1509     sljit_uw imm;
 1510     sljit_sw inst = VSTR_F32 | (flags & (SLJIT_F32_OP | FPU_LOAD));
 1511 
 1512     SLJIT_ASSERT(arg & SLJIT_MEM);
 1513 
 1514     /* Fast loads and stores. */
 1515     if (SLJIT_UNLIKELY(arg & OFFS_REG_MASK)) {
 1516         FAIL_IF(push_inst32(compiler, ADD_W | RD4(TMP_REG1) | RN4(arg & REG_MASK) | RM4(OFFS_REG(arg)) | ((argw & 0x3) << 6)));
 1517         arg = SLJIT_MEM | TMP_REG1;
 1518         argw = 0;
 1519     }
 1520 
 1521     if ((arg & REG_MASK) && (argw & 0x3) == 0) {
 1522         if (!(argw & ~0x3fc))
 1523             return push_inst32(compiler, inst | 0x800000 | RN4(arg & REG_MASK) | DD4(reg) | (argw >> 2));
 1524         if (!(-argw & ~0x3fc))
 1525             return push_inst32(compiler, inst | RN4(arg & REG_MASK) | DD4(reg) | (-argw >> 2));
 1526     }
 1527 
 1528     if (arg & REG_MASK) {
 1529         if (emit_set_delta(compiler, TMP_REG1, arg & REG_MASK, argw) != SLJIT_ERR_UNSUPPORTED) {
 1530             FAIL_IF(compiler->error);
 1531             return push_inst32(compiler, inst | 0x800000 | RN4(TMP_REG1) | DD4(reg));
 1532         }
 1533         imm = get_imm(argw & ~0x3fc);
 1534         if (imm != INVALID_IMM) {
 1535             FAIL_IF(push_inst32(compiler, ADD_WI | RD4(TMP_REG1) | RN4(arg & REG_MASK) | imm));
 1536             return push_inst32(compiler, inst | 0x800000 | RN4(TMP_REG1) | DD4(reg) | ((argw & 0x3fc) >> 2));
 1537         }
 1538         imm = get_imm(-argw & ~0x3fc);
 1539         if (imm != INVALID_IMM) {
 1540             argw = -argw;
 1541             FAIL_IF(push_inst32(compiler, SUB_WI | RD4(TMP_REG1) | RN4(arg & REG_MASK) | imm));
 1542             return push_inst32(compiler, inst | RN4(TMP_REG1) | DD4(reg) | ((argw & 0x3fc) >> 2));
 1543         }
 1544     }
 1545 
 1546     FAIL_IF(load_immediate(compiler, TMP_REG1, argw));
 1547     if (arg & REG_MASK)
 1548         FAIL_IF(push_inst16(compiler, ADD | SET_REGS44(TMP_REG1, (arg & REG_MASK))));
 1549     return push_inst32(compiler, inst | 0x800000 | RN4(TMP_REG1) | DD4(reg));
 1550 }
 1551 
 1552 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_conv_sw_from_f64(struct sljit_compiler *compiler, sljit_s32 op,
 1553     sljit_s32 dst, sljit_sw dstw,
 1554     sljit_s32 src, sljit_sw srcw)
 1555 {
 1556     op ^= SLJIT_F32_OP;
 1557 
 1558     if (src & SLJIT_MEM) {
 1559         FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, TMP_FREG1, src, srcw));
 1560         src = TMP_FREG1;
 1561     }
 1562 
 1563     FAIL_IF(push_inst32(compiler, VCVT_S32_F32 | (op & SLJIT_F32_OP) | DD4(TMP_FREG1) | DM4(src)));
 1564 
 1565     if (FAST_IS_REG(dst))
 1566         return push_inst32(compiler, VMOV | (1 << 20) | RT4(dst) | DN4(TMP_FREG1));
 1567 
 1568     /* Store the integer value from a VFP register. */
 1569     return emit_fop_mem(compiler, 0, TMP_FREG1, dst, dstw);
 1570 }
 1571 
 1572 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_conv_f64_from_sw(struct sljit_compiler *compiler, sljit_s32 op,
 1573     sljit_s32 dst, sljit_sw dstw,
 1574     sljit_s32 src, sljit_sw srcw)
 1575 {
 1576     sljit_s32 dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
 1577 
 1578     op ^= SLJIT_F32_OP;
 1579 
 1580     if (FAST_IS_REG(src))
 1581         FAIL_IF(push_inst32(compiler, VMOV | RT4(src) | DN4(TMP_FREG1)));
 1582     else if (src & SLJIT_MEM) {
 1583         /* Load the integer value into a VFP register. */
 1584         FAIL_IF(emit_fop_mem(compiler, FPU_LOAD, TMP_FREG1, src, srcw));
 1585     }
 1586     else {
 1587         FAIL_IF(load_immediate(compiler, TMP_REG1, srcw));
 1588         FAIL_IF(push_inst32(compiler, VMOV | RT4(TMP_REG1) | DN4(TMP_FREG1)));
 1589     }
 1590 
 1591     FAIL_IF(push_inst32(compiler, VCVT_F32_S32 | (op & SLJIT_F32_OP) | DD4(dst_r) | DM4(TMP_FREG1)));
 1592 
 1593     if (dst & SLJIT_MEM)
 1594         return emit_fop_mem(compiler, (op & SLJIT_F32_OP), TMP_FREG1, dst, dstw);
 1595     return SLJIT_SUCCESS;
 1596 }
 1597 
 1598 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_cmp(struct sljit_compiler *compiler, sljit_s32 op,
 1599     sljit_s32 src1, sljit_sw src1w,
 1600     sljit_s32 src2, sljit_sw src2w)
 1601 {
 1602     op ^= SLJIT_F32_OP;
 1603 
 1604     if (src1 & SLJIT_MEM) {
 1605         emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, TMP_FREG1, src1, src1w);
 1606         src1 = TMP_FREG1;
 1607     }
 1608 
 1609     if (src2 & SLJIT_MEM) {
 1610         emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, TMP_FREG2, src2, src2w);
 1611         src2 = TMP_FREG2;
 1612     }
 1613 
 1614     FAIL_IF(push_inst32(compiler, VCMP_F32 | (op & SLJIT_F32_OP) | DD4(src1) | DM4(src2)));
 1615     return push_inst32(compiler, VMRS);
 1616 }
 1617 
 1618 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop1(struct sljit_compiler *compiler, sljit_s32 op,
 1619     sljit_s32 dst, sljit_sw dstw,
 1620     sljit_s32 src, sljit_sw srcw)
 1621 {
 1622     sljit_s32 dst_r;
 1623 
 1624     CHECK_ERROR();
 1625 
 1626     SLJIT_COMPILE_ASSERT((SLJIT_F32_OP == 0x100), float_transfer_bit_error);
 1627     SELECT_FOP1_OPERATION_WITH_CHECKS(compiler, op, dst, dstw, src, srcw);
 1628 
 1629     dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
 1630 
 1631     if (GET_OPCODE(op) != SLJIT_CONV_F64_FROM_F32)
 1632         op ^= SLJIT_F32_OP;
 1633 
 1634     if (src & SLJIT_MEM) {
 1635         emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, dst_r, src, srcw);
 1636         src = dst_r;
 1637     }
 1638 
 1639     switch (GET_OPCODE(op)) {
 1640     case SLJIT_MOV_F64:
 1641         if (src != dst_r) {
 1642             if (dst_r != TMP_FREG1)
 1643                 FAIL_IF(push_inst32(compiler, VMOV_F32 | (op & SLJIT_F32_OP) | DD4(dst_r) | DM4(src)));
 1644             else
 1645                 dst_r = src;
 1646         }
 1647         break;
 1648     case SLJIT_NEG_F64:
 1649         FAIL_IF(push_inst32(compiler, VNEG_F32 | (op & SLJIT_F32_OP) | DD4(dst_r) | DM4(src)));
 1650         break;
 1651     case SLJIT_ABS_F64:
 1652         FAIL_IF(push_inst32(compiler, VABS_F32 | (op & SLJIT_F32_OP) | DD4(dst_r) | DM4(src)));
 1653         break;
 1654     case SLJIT_CONV_F64_FROM_F32:
 1655         FAIL_IF(push_inst32(compiler, VCVT_F64_F32 | (op & SLJIT_F32_OP) | DD4(dst_r) | DM4(src)));
 1656         op ^= SLJIT_F32_OP;
 1657         break;
 1658     }
 1659 
 1660     if (dst & SLJIT_MEM)
 1661         return emit_fop_mem(compiler, (op & SLJIT_F32_OP), dst_r, dst, dstw);
 1662     return SLJIT_SUCCESS;
 1663 }
 1664 
 1665 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop2(struct sljit_compiler *compiler, sljit_s32 op,
 1666     sljit_s32 dst, sljit_sw dstw,
 1667     sljit_s32 src1, sljit_sw src1w,
 1668     sljit_s32 src2, sljit_sw src2w)
 1669 {
 1670     sljit_s32 dst_r;
 1671 
 1672     CHECK_ERROR();
 1673     CHECK(check_sljit_emit_fop2(compiler, op, dst, dstw, src1, src1w, src2, src2w));
 1674     ADJUST_LOCAL_OFFSET(dst, dstw);
 1675     ADJUST_LOCAL_OFFSET(src1, src1w);
 1676     ADJUST_LOCAL_OFFSET(src2, src2w);
 1677 
 1678     op ^= SLJIT_F32_OP;
 1679 
 1680     dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
 1681     if (src1 & SLJIT_MEM) {
 1682         emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, TMP_FREG1, src1, src1w);
 1683         src1 = TMP_FREG1;
 1684     }
 1685     if (src2 & SLJIT_MEM) {
 1686         emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, TMP_FREG2, src2, src2w);
 1687         src2 = TMP_FREG2;
 1688     }
 1689 
 1690     switch (GET_OPCODE(op)) {
 1691     case SLJIT_ADD_F64:
 1692         FAIL_IF(push_inst32(compiler, VADD_F32 | (op & SLJIT_F32_OP) | DD4(dst_r) | DN4(src1) | DM4(src2)));
 1693         break;
 1694     case SLJIT_SUB_F64:
 1695         FAIL_IF(push_inst32(compiler, VSUB_F32 | (op & SLJIT_F32_OP) | DD4(dst_r) | DN4(src1) | DM4(src2)));
 1696         break;
 1697     case SLJIT_MUL_F64:
 1698         FAIL_IF(push_inst32(compiler, VMUL_F32 | (op & SLJIT_F32_OP) | DD4(dst_r) | DN4(src1) | DM4(src2)));
 1699         break;
 1700     case SLJIT_DIV_F64:
 1701         FAIL_IF(push_inst32(compiler, VDIV_F32 | (op & SLJIT_F32_OP) | DD4(dst_r) | DN4(src1) | DM4(src2)));
 1702         break;
 1703     }
 1704 
 1705     if (!(dst & SLJIT_MEM))
 1706         return SLJIT_SUCCESS;
 1707     return emit_fop_mem(compiler, (op & SLJIT_F32_OP), TMP_FREG1, dst, dstw);
 1708 }
 1709 
 1710 #undef FPU_LOAD
 1711 
 1712 /* --------------------------------------------------------------------- */
 1713 /*  Other instructions                                                   */
 1714 /* --------------------------------------------------------------------- */
 1715 
 1716 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fast_enter(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw)
 1717 {
 1718     CHECK_ERROR();
 1719     CHECK(check_sljit_emit_fast_enter(compiler, dst, dstw));
 1720     ADJUST_LOCAL_OFFSET(dst, dstw);
 1721 
 1722     SLJIT_ASSERT(reg_map[TMP_REG2] == 14);
 1723 
 1724     if (FAST_IS_REG(dst))
 1725         return push_inst16(compiler, MOV | SET_REGS44(dst, TMP_REG2));
 1726 
 1727     /* Memory. */
 1728     return emit_op_mem(compiler, WORD_SIZE | STORE, TMP_REG2, dst, dstw, TMP_REG1);
 1729 }
 1730 
 1731 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fast_return(struct sljit_compiler *compiler, sljit_s32 src, sljit_sw srcw)
 1732 {
 1733     CHECK_ERROR();
 1734     CHECK(check_sljit_emit_fast_return(compiler, src, srcw));
 1735     ADJUST_LOCAL_OFFSET(src, srcw);
 1736 
 1737     SLJIT_ASSERT(reg_map[TMP_REG2] == 14);
 1738 
 1739     if (FAST_IS_REG(src))
 1740         FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(TMP_REG2, src)));
 1741     else
 1742         FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG2, src, srcw, TMP_REG2));
 1743 
 1744     return push_inst16(compiler, BX | RN3(TMP_REG2));
 1745 }
 1746 
 1747 /* --------------------------------------------------------------------- */
 1748 /*  Conditional instructions                                             */
 1749 /* --------------------------------------------------------------------- */
 1750 
 1751 static sljit_uw get_cc(sljit_s32 type)
 1752 {
 1753     switch (type) {
 1754     case SLJIT_EQUAL:
 1755     case SLJIT_MUL_NOT_OVERFLOW:
 1756     case SLJIT_EQUAL_F64:
 1757         return 0x0;
 1758 
 1759     case SLJIT_NOT_EQUAL:
 1760     case SLJIT_MUL_OVERFLOW:
 1761     case SLJIT_NOT_EQUAL_F64:
 1762         return 0x1;
 1763 
 1764     case SLJIT_LESS:
 1765     case SLJIT_LESS_F64:
 1766         return 0x3;
 1767 
 1768     case SLJIT_GREATER_EQUAL:
 1769     case SLJIT_GREATER_EQUAL_F64:
 1770         return 0x2;
 1771 
 1772     case SLJIT_GREATER:
 1773     case SLJIT_GREATER_F64:
 1774         return 0x8;
 1775 
 1776     case SLJIT_LESS_EQUAL:
 1777     case SLJIT_LESS_EQUAL_F64:
 1778         return 0x9;
 1779 
 1780     case SLJIT_SIG_LESS:
 1781         return 0xb;
 1782 
 1783     case SLJIT_SIG_GREATER_EQUAL:
 1784         return 0xa;
 1785 
 1786     case SLJIT_SIG_GREATER:
 1787         return 0xc;
 1788 
 1789     case SLJIT_SIG_LESS_EQUAL:
 1790         return 0xd;
 1791 
 1792     case SLJIT_OVERFLOW:
 1793     case SLJIT_UNORDERED_F64:
 1794         return 0x6;
 1795 
 1796     case SLJIT_NOT_OVERFLOW:
 1797     case SLJIT_ORDERED_F64:
 1798         return 0x7;
 1799 
 1800     default: /* SLJIT_JUMP */
 1801         SLJIT_UNREACHABLE();
 1802         return 0xe;
 1803     }
 1804 }
 1805 
 1806 SLJIT_API_FUNC_ATTRIBUTE struct sljit_label* sljit_emit_label(struct sljit_compiler *compiler)
 1807 {
 1808     struct sljit_label *label;
 1809 
 1810     CHECK_ERROR_PTR();
 1811     CHECK_PTR(check_sljit_emit_label(compiler));
 1812 
 1813     if (compiler->last_label && compiler->last_label->size == compiler->size)
 1814         return compiler->last_label;
 1815 
 1816     label = (struct sljit_label*)ensure_abuf(compiler, sizeof(struct sljit_label));
 1817     PTR_FAIL_IF(!label);
 1818     set_label(label, compiler);
 1819     return label;
 1820 }
 1821 
 1822 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_jump(struct sljit_compiler *compiler, sljit_s32 type)
 1823 {
 1824     struct sljit_jump *jump;
 1825     sljit_ins cc;
 1826 
 1827     CHECK_ERROR_PTR();
 1828     CHECK_PTR(check_sljit_emit_jump(compiler, type));
 1829 
 1830     jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
 1831     PTR_FAIL_IF(!jump);
 1832     set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
 1833     type &= 0xff;
 1834 
 1835     PTR_FAIL_IF(emit_imm32_const(compiler, TMP_REG1, 0));
 1836     if (type < SLJIT_JUMP) {
 1837         jump->flags |= IS_COND;
 1838         cc = get_cc(type);
 1839         jump->flags |= cc << 8;
 1840         PTR_FAIL_IF(push_inst16(compiler, IT | (cc << 4) | 0x8));
 1841     }
 1842 
 1843     jump->addr = compiler->size;
 1844     if (type <= SLJIT_JUMP)
 1845         PTR_FAIL_IF(push_inst16(compiler, BX | RN3(TMP_REG1)));
 1846     else {
 1847         jump->flags |= IS_BL;
 1848         PTR_FAIL_IF(push_inst16(compiler, BLX | RN3(TMP_REG1)));
 1849     }
 1850 
 1851     return jump;
 1852 }
 1853 
 1854 #ifdef __SOFTFP__
 1855 
 1856 static sljit_s32 softfloat_call_with_args(struct sljit_compiler *compiler, sljit_s32 arg_types, sljit_s32 *src)
 1857 {
 1858     sljit_s32 stack_offset = 0;
 1859     sljit_s32 arg_count = 0;
 1860     sljit_s32 word_arg_offset = 0;
 1861     sljit_s32 float_arg_count = 0;
 1862     sljit_s32 types = 0;
 1863     sljit_s32 src_offset = 4 * sizeof(sljit_sw);
 1864     sljit_u8 offsets[4];
 1865 
 1866     if (src && FAST_IS_REG(*src))
 1867         src_offset = reg_map[*src] * sizeof(sljit_sw);
 1868 
 1869     arg_types >>= SLJIT_DEF_SHIFT;
 1870 
 1871     while (arg_types) {
 1872         types = (types << SLJIT_DEF_SHIFT) | (arg_types & SLJIT_DEF_MASK);
 1873 
 1874         switch (arg_types & SLJIT_DEF_MASK) {
 1875         case SLJIT_ARG_TYPE_F32:
 1876             offsets[arg_count] = (sljit_u8)stack_offset;
 1877             stack_offset += sizeof(sljit_f32);
 1878             arg_count++;
 1879             float_arg_count++;
 1880             break;
 1881         case SLJIT_ARG_TYPE_F64:
 1882             if (stack_offset & 0x7)
 1883                 stack_offset += sizeof(sljit_sw);
 1884             offsets[arg_count] = (sljit_u8)stack_offset;
 1885             stack_offset += sizeof(sljit_f64);
 1886             arg_count++;
 1887             float_arg_count++;
 1888             break;
 1889         default:
 1890             offsets[arg_count] = (sljit_u8)stack_offset;
 1891             stack_offset += sizeof(sljit_sw);
 1892             arg_count++;
 1893             word_arg_offset += sizeof(sljit_sw);
 1894             break;
 1895         }
 1896 
 1897         arg_types >>= SLJIT_DEF_SHIFT;
 1898     }
 1899 
 1900     if (stack_offset > 16)
 1901         FAIL_IF(push_inst16(compiler, SUB_SP | (((stack_offset - 16) + 0x7) & ~0x7) >> 2));
 1902 
 1903     SLJIT_ASSERT(reg_map[TMP_REG1] == 12);
 1904 
 1905     /* Process arguments in reversed direction. */
 1906     while (types) {
 1907         switch (types & SLJIT_DEF_MASK) {
 1908         case SLJIT_ARG_TYPE_F32:
 1909             arg_count--;
 1910             float_arg_count--;
 1911             stack_offset = offsets[arg_count];
 1912 
 1913             if (stack_offset < 16) {
 1914                 if (src_offset == stack_offset) {
 1915                     FAIL_IF(push_inst16(compiler, MOV | (src_offset << 1) | 4 | (1 << 7)));
 1916                     *src = TMP_REG1;
 1917                 }
 1918                 FAIL_IF(push_inst32(compiler, VMOV | 0x100000 | (float_arg_count << 16) | (stack_offset << 10)));
 1919             } else
 1920                 FAIL_IF(push_inst32(compiler, VSTR_F32 | 0x800000 | RN4(SLJIT_SP) | (float_arg_count << 12) | ((stack_offset - 16) >> 2)));
 1921             break;
 1922         case SLJIT_ARG_TYPE_F64:
 1923             arg_count--;
 1924             float_arg_count--;
 1925             stack_offset = offsets[arg_count];
 1926 
 1927             SLJIT_ASSERT((stack_offset & 0x7) == 0);
 1928 
 1929             if (stack_offset < 16) {
 1930                 if (src_offset == stack_offset || src_offset == stack_offset + sizeof(sljit_sw)) {
 1931                     FAIL_IF(push_inst16(compiler, MOV | (src_offset << 1) | 4 | (1 << 7)));
 1932                     *src = TMP_REG1;
 1933                 }
 1934                 FAIL_IF(push_inst32(compiler, VMOV2 | 0x100000 | (stack_offset << 10) | ((stack_offset + sizeof(sljit_sw)) << 14) | float_arg_count));
 1935             } else
 1936                 FAIL_IF(push_inst32(compiler, VSTR_F32 | 0x800100 | RN4(SLJIT_SP) | (float_arg_count << 12) | ((stack_offset - 16) >> 2)));
 1937             break;
 1938         default:
 1939             arg_count--;
 1940             word_arg_offset -= sizeof(sljit_sw);
 1941             stack_offset = offsets[arg_count];
 1942 
 1943             SLJIT_ASSERT(stack_offset >= word_arg_offset);
 1944 
 1945             if (stack_offset != word_arg_offset) {
 1946                 if (stack_offset < 16) {
 1947                     if (src_offset == stack_offset) {
 1948                         FAIL_IF(push_inst16(compiler, MOV | (src_offset << 1) | 4 | (1 << 7)));
 1949                         *src = TMP_REG1;
 1950                     }
 1951                     else if (src_offset == word_arg_offset) {
 1952                         *src = 1 + (stack_offset >> 2);
 1953                         src_offset = stack_offset;
 1954                     }
 1955                     FAIL_IF(push_inst16(compiler, MOV | (stack_offset >> 2) | (word_arg_offset << 1)));
 1956                 } else
 1957                     FAIL_IF(push_inst16(compiler, STR_SP | (word_arg_offset << 6) | ((stack_offset - 16) >> 2)));
 1958             }
 1959             break;
 1960         }
 1961 
 1962         types >>= SLJIT_DEF_SHIFT;
 1963     }
 1964 
 1965     return SLJIT_SUCCESS;
 1966 }
 1967 
 1968 static sljit_s32 softfloat_post_call_with_args(struct sljit_compiler *compiler, sljit_s32 arg_types)
 1969 {
 1970     sljit_s32 stack_size = 0;
 1971 
 1972     if ((arg_types & SLJIT_DEF_MASK) == SLJIT_ARG_TYPE_F32)
 1973         FAIL_IF(push_inst32(compiler, VMOV | (0 << 16) | (0 << 12)));
 1974     if ((arg_types & SLJIT_DEF_MASK) == SLJIT_ARG_TYPE_F64)
 1975         FAIL_IF(push_inst32(compiler, VMOV2 | (1 << 16) | (0 << 12) | 0));
 1976 
 1977     arg_types >>= SLJIT_DEF_SHIFT;
 1978 
 1979     while (arg_types) {
 1980         switch (arg_types & SLJIT_DEF_MASK) {
 1981         case SLJIT_ARG_TYPE_F32:
 1982             stack_size += sizeof(sljit_f32);
 1983             break;
 1984         case SLJIT_ARG_TYPE_F64:
 1985             if (stack_size & 0x7)
 1986                 stack_size += sizeof(sljit_sw);
 1987             stack_size += sizeof(sljit_f64);
 1988             break;
 1989         default:
 1990             stack_size += sizeof(sljit_sw);
 1991             break;
 1992         }
 1993 
 1994         arg_types >>= SLJIT_DEF_SHIFT;
 1995     }
 1996 
 1997     if (stack_size <= 16)
 1998         return SLJIT_SUCCESS;
 1999 
 2000     return push_inst16(compiler, ADD_SP | ((((stack_size - 16) + 0x7) & ~0x7) >> 2));
 2001 }
 2002 
 2003 #else
 2004 
 2005 static sljit_s32 hardfloat_call_with_args(struct sljit_compiler *compiler, sljit_s32 arg_types)
 2006 {
 2007     sljit_u32 remap = 0;
 2008     sljit_u32 offset = 0;
 2009     sljit_u32 new_offset, mask;
 2010 
 2011     /* Remove return value. */
 2012     arg_types >>= SLJIT_DEF_SHIFT;
 2013 
 2014     while (arg_types) {
 2015         if ((arg_types & SLJIT_DEF_MASK) == SLJIT_ARG_TYPE_F32) {
 2016             new_offset = 0;
 2017             mask = 1;
 2018 
 2019             while (remap & mask) {
 2020                 new_offset++;
 2021                 mask <<= 1;
 2022             }
 2023             remap |= mask;
 2024 
 2025             if (offset != new_offset)
 2026                 FAIL_IF(push_inst32(compiler, VMOV_F32 | DD4((new_offset >> 1) + 1)
 2027                     | ((new_offset & 0x1) ? 0x400000 : 0) | DM4((offset >> 1) + 1)));
 2028 
 2029             offset += 2;
 2030         }
 2031         else if ((arg_types & SLJIT_DEF_MASK) == SLJIT_ARG_TYPE_F64) {
 2032             new_offset = 0;
 2033             mask = 3;
 2034 
 2035             while (remap & mask) {
 2036                 new_offset += 2;
 2037                 mask <<= 2;
 2038             }
 2039             remap |= mask;
 2040 
 2041             if (offset != new_offset)
 2042                 FAIL_IF(push_inst32(compiler, VMOV_F32 | SLJIT_F32_OP | DD4((new_offset >> 1) + 1) | DM4((offset >> 1) + 1)));
 2043 
 2044             offset += 2;
 2045         }
 2046         arg_types >>= SLJIT_DEF_SHIFT;
 2047     }
 2048 
 2049     return SLJIT_SUCCESS;
 2050 }
 2051 
 2052 #endif
 2053 
 2054 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_call(struct sljit_compiler *compiler, sljit_s32 type,
 2055     sljit_s32 arg_types)
 2056 {
 2057 #ifdef __SOFTFP__
 2058     struct sljit_jump *jump;
 2059 #endif
 2060 
 2061     CHECK_ERROR_PTR();
 2062     CHECK_PTR(check_sljit_emit_call(compiler, type, arg_types));
 2063 
 2064 #ifdef __SOFTFP__
 2065     PTR_FAIL_IF(softfloat_call_with_args(compiler, arg_types, NULL));
 2066 
 2067 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
 2068         || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
 2069     compiler->skip_checks = 1;
 2070 #endif
 2071 
 2072     jump = sljit_emit_jump(compiler, type);
 2073     PTR_FAIL_IF(jump == NULL);
 2074 
 2075     PTR_FAIL_IF(softfloat_post_call_with_args(compiler, arg_types));
 2076     return jump;
 2077 #else
 2078     PTR_FAIL_IF(hardfloat_call_with_args(compiler, arg_types));
 2079 
 2080 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
 2081         || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
 2082     compiler->skip_checks = 1;
 2083 #endif
 2084 
 2085     return sljit_emit_jump(compiler, type);
 2086 #endif
 2087 }
 2088 
 2089 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_ijump(struct sljit_compiler *compiler, sljit_s32 type, sljit_s32 src, sljit_sw srcw)
 2090 {
 2091     struct sljit_jump *jump;
 2092 
 2093     CHECK_ERROR();
 2094     CHECK(check_sljit_emit_ijump(compiler, type, src, srcw));
 2095     ADJUST_LOCAL_OFFSET(src, srcw);
 2096 
 2097     SLJIT_ASSERT(reg_map[TMP_REG1] != 14);
 2098 
 2099     if (!(src & SLJIT_IMM)) {
 2100         if (FAST_IS_REG(src)) {
 2101             SLJIT_ASSERT(reg_map[src] != 14);
 2102             return push_inst16(compiler, (type <= SLJIT_JUMP ? BX : BLX) | RN3(src));
 2103         }
 2104 
 2105         FAIL_IF(emit_op_mem(compiler, WORD_SIZE, type <= SLJIT_JUMP ? TMP_PC : TMP_REG1, src, srcw, TMP_REG1));
 2106         if (type >= SLJIT_FAST_CALL)
 2107             return push_inst16(compiler, BLX | RN3(TMP_REG1));
 2108     }
 2109 
 2110     /* These jumps are converted to jump/call instructions when possible. */
 2111     jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
 2112     FAIL_IF(!jump);
 2113     set_jump(jump, compiler, JUMP_ADDR | ((type >= SLJIT_FAST_CALL) ? IS_BL : 0));
 2114     jump->u.target = srcw;
 2115 
 2116     FAIL_IF(emit_imm32_const(compiler, TMP_REG1, 0));
 2117     jump->addr = compiler->size;
 2118     return push_inst16(compiler, (type <= SLJIT_JUMP ? BX : BLX) | RN3(TMP_REG1));
 2119 }
 2120 
 2121 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_icall(struct sljit_compiler *compiler, sljit_s32 type,
 2122     sljit_s32 arg_types,
 2123     sljit_s32 src, sljit_sw srcw)
 2124 {
 2125     CHECK_ERROR();
 2126     CHECK(check_sljit_emit_icall(compiler, type, arg_types, src, srcw));
 2127 
 2128 #ifdef __SOFTFP__
 2129     if (src & SLJIT_MEM) {
 2130         FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG1, src, srcw, TMP_REG1));
 2131         src = TMP_REG1;
 2132     }
 2133 
 2134     FAIL_IF(softfloat_call_with_args(compiler, arg_types, &src));
 2135 
 2136 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
 2137         || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
 2138     compiler->skip_checks = 1;
 2139 #endif
 2140 
 2141     FAIL_IF(sljit_emit_ijump(compiler, type, src, srcw));
 2142 
 2143     return softfloat_post_call_with_args(compiler, arg_types);
 2144 #else /* !__SOFTFP__ */
 2145     FAIL_IF(hardfloat_call_with_args(compiler, arg_types));
 2146 
 2147 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
 2148         || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
 2149     compiler->skip_checks = 1;
 2150 #endif
 2151 
 2152     return sljit_emit_ijump(compiler, type, src, srcw);
 2153 #endif /* __SOFTFP__ */
 2154 }
 2155 
 2156 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_flags(struct sljit_compiler *compiler, sljit_s32 op,
 2157     sljit_s32 dst, sljit_sw dstw,
 2158     sljit_s32 type)
 2159 {
 2160     sljit_s32 dst_r, flags = GET_ALL_FLAGS(op);
 2161     sljit_ins cc;
 2162 
 2163     CHECK_ERROR();
 2164     CHECK(check_sljit_emit_op_flags(compiler, op, dst, dstw, type));
 2165     ADJUST_LOCAL_OFFSET(dst, dstw);
 2166 
 2167     op = GET_OPCODE(op);
 2168     cc = get_cc(type & 0xff);
 2169     dst_r = FAST_IS_REG(dst) ? dst : TMP_REG1;
 2170 
 2171     if (op < SLJIT_ADD) {
 2172         FAIL_IF(push_inst16(compiler, IT | (cc << 4) | (((cc & 0x1) ^ 0x1) << 3) | 0x4));
 2173         if (reg_map[dst_r] > 7) {
 2174             FAIL_IF(push_inst32(compiler, MOV_WI | RD4(dst_r) | 1));
 2175             FAIL_IF(push_inst32(compiler, MOV_WI | RD4(dst_r) | 0));
 2176         } else {
 2177             /* The movsi (immediate) instruction does not set flags in IT block. */
 2178             FAIL_IF(push_inst16(compiler, MOVSI | RDN3(dst_r) | 1));
 2179             FAIL_IF(push_inst16(compiler, MOVSI | RDN3(dst_r) | 0));
 2180         }
 2181         if (!(dst & SLJIT_MEM))
 2182             return SLJIT_SUCCESS;
 2183         return emit_op_mem(compiler, WORD_SIZE | STORE, TMP_REG1, dst, dstw, TMP_REG2);
 2184     }
 2185 
 2186     if (dst & SLJIT_MEM)
 2187         FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG1, dst, dstw, TMP_REG2));
 2188 
 2189     if (op == SLJIT_AND) {
 2190         FAIL_IF(push_inst16(compiler, IT | (cc << 4) | (((cc & 0x1) ^ 0x1) << 3) | 0x4));
 2191         FAIL_IF(push_inst32(compiler, ANDI | RN4(dst_r) | RD4(dst_r) | 1));
 2192         FAIL_IF(push_inst32(compiler, ANDI | RN4(dst_r) | RD4(dst_r) | 0));
 2193     }
 2194     else {
 2195         FAIL_IF(push_inst16(compiler, IT | (cc << 4) | 0x8));
 2196         FAIL_IF(push_inst32(compiler, ((op == SLJIT_OR) ? ORRI : EORI) | RN4(dst_r) | RD4(dst_r) | 1));
 2197     }
 2198 
 2199     if (dst & SLJIT_MEM)
 2200         FAIL_IF(emit_op_mem(compiler, WORD_SIZE | STORE, TMP_REG1, dst, dstw, TMP_REG2));
 2201 
 2202     if (!(flags & SLJIT_SET_Z))
 2203         return SLJIT_SUCCESS;
 2204 
 2205     /* The condition must always be set, even if the ORR/EORI is not executed above. */
 2206     return push_inst32(compiler, MOV_W | SET_FLAGS | RD4(TMP_REG1) | RM4(dst_r));
 2207 }
 2208 
 2209 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_cmov(struct sljit_compiler *compiler, sljit_s32 type,
 2210     sljit_s32 dst_reg,
 2211     sljit_s32 src, sljit_sw srcw)
 2212 {
 2213     sljit_uw cc, tmp;
 2214 
 2215     CHECK_ERROR();
 2216     CHECK(check_sljit_emit_cmov(compiler, type, dst_reg, src, srcw));
 2217 
 2218     dst_reg &= ~SLJIT_I32_OP;
 2219 
 2220     cc = get_cc(type & 0xff);
 2221 
 2222     if (!(src & SLJIT_IMM)) {
 2223         FAIL_IF(push_inst16(compiler, IT | (cc << 4) | 0x8));
 2224         return push_inst16(compiler, MOV | SET_REGS44(dst_reg, src));
 2225     }
 2226 
 2227     tmp = (sljit_uw) srcw;
 2228 
 2229     if (tmp < 0x10000) {
 2230         /* set low 16 bits, set hi 16 bits to 0. */
 2231         FAIL_IF(push_inst16(compiler, IT | (cc << 4) | 0x8));
 2232         return push_inst32(compiler, MOVW | RD4(dst_reg)
 2233             | COPY_BITS(tmp, 12, 16, 4) | COPY_BITS(tmp, 11, 26, 1) | COPY_BITS(tmp, 8, 12, 3) | (tmp & 0xff));
 2234     }
 2235 
 2236     tmp = get_imm(srcw);
 2237     if (tmp != INVALID_IMM) {
 2238         FAIL_IF(push_inst16(compiler, IT | (cc << 4) | 0x8));
 2239         return push_inst32(compiler, MOV_WI | RD4(dst_reg) | tmp);
 2240     }
 2241 
 2242     tmp = get_imm(~srcw);
 2243     if (tmp != INVALID_IMM) {
 2244         FAIL_IF(push_inst16(compiler, IT | (cc << 4) | 0x8));
 2245         return push_inst32(compiler, MVN_WI | RD4(dst_reg) | tmp);
 2246     }
 2247 
 2248     FAIL_IF(push_inst16(compiler, IT | (cc << 4) | ((cc & 0x1) << 3) | 0x4));
 2249 
 2250     tmp = (sljit_uw) srcw;
 2251     FAIL_IF(push_inst32(compiler, MOVW | RD4(dst_reg)
 2252         | COPY_BITS(tmp, 12, 16, 4) | COPY_BITS(tmp, 11, 26, 1) | COPY_BITS(tmp, 8, 12, 3) | (tmp & 0xff)));
 2253     return push_inst32(compiler, MOVT | RD4(dst_reg)
 2254         | COPY_BITS(tmp, 12 + 16, 16, 4) | COPY_BITS(tmp, 11 + 16, 26, 1) | COPY_BITS(tmp, 8 + 16, 12, 3) | ((tmp & 0xff0000) >> 16));
 2255 }
 2256 
 2257 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_mem(struct sljit_compiler *compiler, sljit_s32 type,
 2258     sljit_s32 reg,
 2259     sljit_s32 mem, sljit_sw memw)
 2260 {
 2261     sljit_s32 flags;
 2262     sljit_ins inst;
 2263 
 2264     CHECK_ERROR();
 2265     CHECK(check_sljit_emit_mem(compiler, type, reg, mem, memw));
 2266 
 2267     if ((mem & OFFS_REG_MASK) || (memw > 255 && memw < -255))
 2268         return SLJIT_ERR_UNSUPPORTED;
 2269 
 2270     if (type & SLJIT_MEM_SUPP)
 2271         return SLJIT_SUCCESS;
 2272 
 2273     switch (type & 0xff) {
 2274     case SLJIT_MOV:
 2275     case SLJIT_MOV_U32:
 2276     case SLJIT_MOV_S32:
 2277     case SLJIT_MOV_P:
 2278         flags = WORD_SIZE;
 2279         break;
 2280     case SLJIT_MOV_U8:
 2281         flags = BYTE_SIZE;
 2282         break;
 2283     case SLJIT_MOV_S8:
 2284         flags = BYTE_SIZE | SIGNED;
 2285         break;
 2286     case SLJIT_MOV_U16:
 2287         flags = HALF_SIZE;
 2288         break;
 2289     case SLJIT_MOV_S16:
 2290         flags = HALF_SIZE | SIGNED;
 2291         break;
 2292     default:
 2293         SLJIT_UNREACHABLE();
 2294         flags = WORD_SIZE;
 2295         break;
 2296     }
 2297 
 2298     if (type & SLJIT_MEM_STORE)
 2299         flags |= STORE;
 2300 
 2301     inst = sljit_mem32[flags] | 0x900;
 2302 
 2303     if (type & SLJIT_MEM_PRE)
 2304         inst |= 0x400;
 2305 
 2306     if (memw >= 0)
 2307         inst |= 0x200;
 2308     else
 2309         memw = -memw;
 2310 
 2311     return push_inst32(compiler, inst | RT4(reg) | RN4(mem & REG_MASK) | memw);
 2312 }
 2313 
 2314 SLJIT_API_FUNC_ATTRIBUTE struct sljit_const* sljit_emit_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw, sljit_sw init_value)
 2315 {
 2316     struct sljit_const *const_;
 2317     sljit_s32 dst_r;
 2318 
 2319     CHECK_ERROR_PTR();
 2320     CHECK_PTR(check_sljit_emit_const(compiler, dst, dstw, init_value));
 2321     ADJUST_LOCAL_OFFSET(dst, dstw);
 2322 
 2323     const_ = (struct sljit_const*)ensure_abuf(compiler, sizeof(struct sljit_const));
 2324     PTR_FAIL_IF(!const_);
 2325     set_const(const_, compiler);
 2326 
 2327     dst_r = FAST_IS_REG(dst) ? dst : TMP_REG1;
 2328     PTR_FAIL_IF(emit_imm32_const(compiler, dst_r, init_value));
 2329 
 2330     if (dst & SLJIT_MEM)
 2331         PTR_FAIL_IF(emit_op_mem(compiler, WORD_SIZE | STORE, dst_r, dst, dstw, TMP_REG2));
 2332     return const_;
 2333 }
 2334 
 2335 SLJIT_API_FUNC_ATTRIBUTE struct sljit_put_label* sljit_emit_put_label(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw)
 2336 {
 2337     struct sljit_put_label *put_label;
 2338     sljit_s32 dst_r;
 2339 
 2340     CHECK_ERROR_PTR();
 2341     CHECK_PTR(check_sljit_emit_put_label(compiler, dst, dstw));
 2342     ADJUST_LOCAL_OFFSET(dst, dstw);
 2343 
 2344     put_label = (struct sljit_put_label*)ensure_abuf(compiler, sizeof(struct sljit_put_label));
 2345     PTR_FAIL_IF(!put_label);
 2346     set_put_label(put_label, compiler, 0);
 2347 
 2348     dst_r = FAST_IS_REG(dst) ? dst : TMP_REG1;
 2349     PTR_FAIL_IF(emit_imm32_const(compiler, dst_r, 0));
 2350 
 2351     if (dst & SLJIT_MEM)
 2352         PTR_FAIL_IF(emit_op_mem(compiler, WORD_SIZE | STORE, dst_r, dst, dstw, TMP_REG2));
 2353     return put_label;
 2354 }
 2355 
 2356 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_target, sljit_sw executable_offset)
 2357 {
 2358     sljit_u16 *inst = (sljit_u16*)addr;
 2359     modify_imm32_const(inst, new_target);
 2360     inst = (sljit_u16 *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
 2361     SLJIT_CACHE_FLUSH(inst, inst + 4);
 2362 }
 2363 
 2364 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_const(sljit_uw addr, sljit_sw new_constant, sljit_sw executable_offset)
 2365 {
 2366     sljit_u16 *inst = (sljit_u16*)addr;
 2367     modify_imm32_const(inst, new_constant);
 2368     inst = (sljit_u16 *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
 2369     SLJIT_CACHE_FLUSH(inst, inst + 4);
 2370 }