"Fossies" - the Fresh Open Source Software Archive

Member "pcre-8.44/sljit/sljitNativeX86_32.c" (17 Jul 2019, 26622 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 "sljitNativeX86_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 /* x86 32-bit arch dependent functions. */
   28 
   29 static sljit_s32 emit_do_imm(struct sljit_compiler *compiler, sljit_u8 opcode, sljit_sw imm)
   30 {
   31     sljit_u8 *inst;
   32 
   33     inst = (sljit_u8*)ensure_buf(compiler, 1 + 1 + sizeof(sljit_sw));
   34     FAIL_IF(!inst);
   35     INC_SIZE(1 + sizeof(sljit_sw));
   36     *inst++ = opcode;
   37     sljit_unaligned_store_sw(inst, imm);
   38     return SLJIT_SUCCESS;
   39 }
   40 
   41 static sljit_u8* generate_far_jump_code(struct sljit_jump *jump, sljit_u8 *code_ptr, sljit_sw executable_offset)
   42 {
   43     sljit_s32 type = jump->flags >> TYPE_SHIFT;
   44 
   45     if (type == SLJIT_JUMP) {
   46         *code_ptr++ = JMP_i32;
   47         jump->addr++;
   48     }
   49     else if (type >= SLJIT_FAST_CALL) {
   50         *code_ptr++ = CALL_i32;
   51         jump->addr++;
   52     }
   53     else {
   54         *code_ptr++ = GROUP_0F;
   55         *code_ptr++ = get_jump_code(type);
   56         jump->addr += 2;
   57     }
   58 
   59     if (jump->flags & JUMP_LABEL)
   60         jump->flags |= PATCH_MW;
   61     else
   62         sljit_unaligned_store_sw(code_ptr, jump->u.target - (jump->addr + 4) - (sljit_uw)executable_offset);
   63     code_ptr += 4;
   64 
   65     return code_ptr;
   66 }
   67 
   68 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_enter(struct sljit_compiler *compiler,
   69     sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
   70     sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
   71 {
   72     sljit_s32 args, size;
   73     sljit_u8 *inst;
   74 
   75     CHECK_ERROR();
   76     CHECK(check_sljit_emit_enter(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size));
   77     set_emit_enter(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size);
   78 
   79     args = get_arg_count(arg_types);
   80     compiler->args = args;
   81 
   82     /* [esp+0] for saving temporaries and function calls. */
   83     compiler->stack_tmp_size = 2 * sizeof(sljit_sw);
   84 
   85 #if !(defined SLJIT_X86_32_FASTCALL && SLJIT_X86_32_FASTCALL)
   86     if (scratches > 3)
   87         compiler->stack_tmp_size = 3 * sizeof(sljit_sw);
   88 #endif
   89 
   90     compiler->saveds_offset = compiler->stack_tmp_size;
   91     if (scratches > 3)
   92         compiler->saveds_offset += ((scratches > (3 + 6)) ? 6 : (scratches - 3)) * sizeof(sljit_sw);
   93 
   94     compiler->locals_offset = compiler->saveds_offset;
   95 
   96     if (saveds > 3)
   97         compiler->locals_offset += (saveds - 3) * sizeof(sljit_sw);
   98 
   99     if (options & SLJIT_F64_ALIGNMENT)
  100         compiler->locals_offset = (compiler->locals_offset + sizeof(sljit_f64) - 1) & ~(sizeof(sljit_f64) - 1);
  101 
  102     size = 1 + (scratches > 9 ? (scratches - 9) : 0) + (saveds <= 3 ? saveds : 3);
  103 #if (defined SLJIT_X86_32_FASTCALL && SLJIT_X86_32_FASTCALL)
  104     size += (args > 0 ? (args * 2) : 0) + (args > 2 ? 2 : 0);
  105 #else
  106     size += (args > 0 ? (2 + args * 3) : 0);
  107 #endif
  108     inst = (sljit_u8*)ensure_buf(compiler, 1 + size);
  109     FAIL_IF(!inst);
  110 
  111     INC_SIZE(size);
  112     PUSH_REG(reg_map[TMP_REG1]);
  113 #if !(defined SLJIT_X86_32_FASTCALL && SLJIT_X86_32_FASTCALL)
  114     if (args > 0) {
  115         *inst++ = MOV_r_rm;
  116         *inst++ = MOD_REG | (reg_map[TMP_REG1] << 3) | 0x4 /* esp */;
  117     }
  118 #endif
  119     if (saveds > 2 || scratches > 9)
  120         PUSH_REG(reg_map[SLJIT_S2]);
  121     if (saveds > 1 || scratches > 10)
  122         PUSH_REG(reg_map[SLJIT_S1]);
  123     if (saveds > 0 || scratches > 11)
  124         PUSH_REG(reg_map[SLJIT_S0]);
  125 
  126 #if (defined SLJIT_X86_32_FASTCALL && SLJIT_X86_32_FASTCALL)
  127     if (args > 0) {
  128         inst[0] = MOV_r_rm;
  129         inst[1] = MOD_REG | (reg_map[SLJIT_S0] << 3) | reg_map[SLJIT_R2];
  130         inst += 2;
  131     }
  132     if (args > 1) {
  133         inst[0] = MOV_r_rm;
  134         inst[1] = MOD_REG | (reg_map[SLJIT_S1] << 3) | reg_map[SLJIT_R1];
  135         inst += 2;
  136     }
  137     if (args > 2) {
  138         inst[0] = MOV_r_rm;
  139         inst[1] = MOD_DISP8 | (reg_map[SLJIT_S2] << 3) | 0x4 /* esp */;
  140         inst[2] = 0x24;
  141         inst[3] = sizeof(sljit_sw) * (3 + 2); /* saveds >= 3 as well. */
  142     }
  143 #else
  144     if (args > 0) {
  145         inst[0] = MOV_r_rm;
  146         inst[1] = MOD_DISP8 | (reg_map[SLJIT_S0] << 3) | reg_map[TMP_REG1];
  147         inst[2] = sizeof(sljit_sw) * 2;
  148         inst += 3;
  149     }
  150     if (args > 1) {
  151         inst[0] = MOV_r_rm;
  152         inst[1] = MOD_DISP8 | (reg_map[SLJIT_S1] << 3) | reg_map[TMP_REG1];
  153         inst[2] = sizeof(sljit_sw) * 3;
  154         inst += 3;
  155     }
  156     if (args > 2) {
  157         inst[0] = MOV_r_rm;
  158         inst[1] = MOD_DISP8 | (reg_map[SLJIT_S2] << 3) | reg_map[TMP_REG1];
  159         inst[2] = sizeof(sljit_sw) * 4;
  160     }
  161 #endif
  162 
  163     SLJIT_ASSERT(SLJIT_LOCALS_OFFSET > 0);
  164 
  165 #if defined(__APPLE__)
  166     /* Ignore pushed registers and SLJIT_LOCALS_OFFSET when computing the aligned local size. */
  167     saveds = (2 + (scratches > 9 ? (scratches - 9) : 0) + (saveds <= 3 ? saveds : 3)) * sizeof(sljit_uw);
  168     local_size = ((SLJIT_LOCALS_OFFSET + saveds + local_size + 15) & ~15) - saveds;
  169 #else
  170     if (options & SLJIT_F64_ALIGNMENT)
  171         local_size = SLJIT_LOCALS_OFFSET + ((local_size + sizeof(sljit_f64) - 1) & ~(sizeof(sljit_f64) - 1));
  172     else
  173         local_size = SLJIT_LOCALS_OFFSET + ((local_size + sizeof(sljit_sw) - 1) & ~(sizeof(sljit_sw) - 1));
  174 #endif
  175 
  176     compiler->local_size = local_size;
  177 
  178 #ifdef _WIN32
  179     if (local_size > 0) {
  180         if (local_size <= 4 * 4096) {
  181             if (local_size > 4096)
  182                 EMIT_MOV(compiler, TMP_REG1, 0, SLJIT_MEM1(SLJIT_SP), -4096);
  183             if (local_size > 2 * 4096)
  184                 EMIT_MOV(compiler, TMP_REG1, 0, SLJIT_MEM1(SLJIT_SP), -4096 * 2);
  185             if (local_size > 3 * 4096)
  186                 EMIT_MOV(compiler, TMP_REG1, 0, SLJIT_MEM1(SLJIT_SP), -4096 * 3);
  187         }
  188         else {
  189             EMIT_MOV(compiler, SLJIT_R0, 0, SLJIT_SP, 0);
  190             EMIT_MOV(compiler, SLJIT_R1, 0, SLJIT_IMM, (local_size - 1) >> 12);
  191 
  192             SLJIT_ASSERT (reg_map[SLJIT_R0] == 0);
  193 
  194             EMIT_MOV(compiler, TMP_REG1, 0, SLJIT_MEM1(SLJIT_R0), -4096);
  195             FAIL_IF(emit_non_cum_binary(compiler, BINARY_OPCODE(SUB),
  196                 SLJIT_R0, 0, SLJIT_R0, 0, SLJIT_IMM, 4096));
  197             FAIL_IF(emit_non_cum_binary(compiler, BINARY_OPCODE(SUB),
  198                 SLJIT_R1, 0, SLJIT_R1, 0, SLJIT_IMM, 1));
  199 
  200             inst = (sljit_u8*)ensure_buf(compiler, 1 + 2);
  201             FAIL_IF(!inst);
  202 
  203             INC_SIZE(2);
  204             inst[0] = JNE_i8;
  205             inst[1] = (sljit_s8) -16;
  206         }
  207 
  208         EMIT_MOV(compiler, TMP_REG1, 0, SLJIT_MEM1(SLJIT_SP), -local_size);
  209     }
  210 #endif
  211 
  212     SLJIT_ASSERT(local_size > 0);
  213 
  214 #if !defined(__APPLE__)
  215     if (options & SLJIT_F64_ALIGNMENT) {
  216         EMIT_MOV(compiler, TMP_REG1, 0, SLJIT_SP, 0);
  217 
  218         /* Some space might allocated during sljit_grow_stack() above on WIN32. */
  219         FAIL_IF(emit_non_cum_binary(compiler, BINARY_OPCODE(SUB),
  220             SLJIT_SP, 0, SLJIT_SP, 0, SLJIT_IMM, local_size + sizeof(sljit_sw)));
  221 
  222 #if defined _WIN32 && !(defined SLJIT_X86_32_FASTCALL && SLJIT_X86_32_FASTCALL)
  223         if (compiler->local_size > 1024)
  224             FAIL_IF(emit_cum_binary(compiler, BINARY_OPCODE(ADD),
  225                 TMP_REG1, 0, TMP_REG1, 0, SLJIT_IMM, sizeof(sljit_sw)));
  226 #endif
  227 
  228         inst = (sljit_u8*)ensure_buf(compiler, 1 + 6);
  229         FAIL_IF(!inst);
  230 
  231         INC_SIZE(6);
  232         inst[0] = GROUP_BINARY_81;
  233         inst[1] = MOD_REG | AND | reg_map[SLJIT_SP];
  234         sljit_unaligned_store_sw(inst + 2, ~(sizeof(sljit_f64) - 1));
  235 
  236         /* The real local size must be used. */
  237         return emit_mov(compiler, SLJIT_MEM1(SLJIT_SP), compiler->local_size, TMP_REG1, 0);
  238     }
  239 #endif
  240     return emit_non_cum_binary(compiler, BINARY_OPCODE(SUB),
  241         SLJIT_SP, 0, SLJIT_SP, 0, SLJIT_IMM, local_size);
  242 }
  243 
  244 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_set_context(struct sljit_compiler *compiler,
  245     sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
  246     sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
  247 {
  248     CHECK_ERROR();
  249     CHECK(check_sljit_set_context(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size));
  250     set_set_context(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size);
  251 
  252     compiler->args = get_arg_count(arg_types);
  253 
  254     /* [esp+0] for saving temporaries and function calls. */
  255     compiler->stack_tmp_size = 2 * sizeof(sljit_sw);
  256 
  257 #if !(defined SLJIT_X86_32_FASTCALL && SLJIT_X86_32_FASTCALL)
  258     if (scratches > 3)
  259         compiler->stack_tmp_size = 3 * sizeof(sljit_sw);
  260 #endif
  261 
  262     compiler->saveds_offset = compiler->stack_tmp_size;
  263     if (scratches > 3)
  264         compiler->saveds_offset += ((scratches > (3 + 6)) ? 6 : (scratches - 3)) * sizeof(sljit_sw);
  265 
  266     compiler->locals_offset = compiler->saveds_offset;
  267 
  268     if (saveds > 3)
  269         compiler->locals_offset += (saveds - 3) * sizeof(sljit_sw);
  270 
  271     if (options & SLJIT_F64_ALIGNMENT)
  272         compiler->locals_offset = (compiler->locals_offset + sizeof(sljit_f64) - 1) & ~(sizeof(sljit_f64) - 1);
  273 
  274 #if defined(__APPLE__)
  275     saveds = (2 + (scratches > 9 ? (scratches - 9) : 0) + (saveds <= 3 ? saveds : 3)) * sizeof(sljit_uw);
  276     compiler->local_size = ((SLJIT_LOCALS_OFFSET + saveds + local_size + 15) & ~15) - saveds;
  277 #else
  278     if (options & SLJIT_F64_ALIGNMENT)
  279         compiler->local_size = SLJIT_LOCALS_OFFSET + ((local_size + sizeof(sljit_f64) - 1) & ~(sizeof(sljit_f64) - 1));
  280     else
  281         compiler->local_size = SLJIT_LOCALS_OFFSET + ((local_size + sizeof(sljit_sw) - 1) & ~(sizeof(sljit_sw) - 1));
  282 #endif
  283     return SLJIT_SUCCESS;
  284 }
  285 
  286 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_return(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 src, sljit_sw srcw)
  287 {
  288     sljit_s32 size;
  289     sljit_u8 *inst;
  290 
  291     CHECK_ERROR();
  292     CHECK(check_sljit_emit_return(compiler, op, src, srcw));
  293     SLJIT_ASSERT(compiler->args >= 0);
  294 
  295     FAIL_IF(emit_mov_before_return(compiler, op, src, srcw));
  296 
  297     SLJIT_ASSERT(compiler->local_size > 0);
  298 
  299 #if !defined(__APPLE__)
  300     if (compiler->options & SLJIT_F64_ALIGNMENT)
  301         EMIT_MOV(compiler, SLJIT_SP, 0, SLJIT_MEM1(SLJIT_SP), compiler->local_size)
  302     else
  303         FAIL_IF(emit_cum_binary(compiler, BINARY_OPCODE(ADD),
  304             SLJIT_SP, 0, SLJIT_SP, 0, SLJIT_IMM, compiler->local_size));
  305 #else
  306     FAIL_IF(emit_cum_binary(compiler, BINARY_OPCODE(ADD),
  307         SLJIT_SP, 0, SLJIT_SP, 0, SLJIT_IMM, compiler->local_size));
  308 #endif
  309 
  310     size = 2 + (compiler->scratches > 7 ? (compiler->scratches - 7) : 0) +
  311         (compiler->saveds <= 3 ? compiler->saveds : 3);
  312 #if (defined SLJIT_X86_32_FASTCALL && SLJIT_X86_32_FASTCALL)
  313     if (compiler->args > 2)
  314         size += 2;
  315 #else
  316     if (compiler->args > 0)
  317         size += 2;
  318 #endif
  319     inst = (sljit_u8*)ensure_buf(compiler, 1 + size);
  320     FAIL_IF(!inst);
  321 
  322     INC_SIZE(size);
  323 
  324     if (compiler->saveds > 0 || compiler->scratches > 11)
  325         POP_REG(reg_map[SLJIT_S0]);
  326     if (compiler->saveds > 1 || compiler->scratches > 10)
  327         POP_REG(reg_map[SLJIT_S1]);
  328     if (compiler->saveds > 2 || compiler->scratches > 9)
  329         POP_REG(reg_map[SLJIT_S2]);
  330     POP_REG(reg_map[TMP_REG1]);
  331 #if (defined SLJIT_X86_32_FASTCALL && SLJIT_X86_32_FASTCALL)
  332     if (compiler->args > 2)
  333         RET_I16(sizeof(sljit_sw));
  334     else
  335         RET();
  336 #else
  337     RET();
  338 #endif
  339 
  340     return SLJIT_SUCCESS;
  341 }
  342 
  343 /* --------------------------------------------------------------------- */
  344 /*  Operators                                                            */
  345 /* --------------------------------------------------------------------- */
  346 
  347 /* Size contains the flags as well. */
  348 static sljit_u8* emit_x86_instruction(struct sljit_compiler *compiler, sljit_s32 size,
  349     /* The register or immediate operand. */
  350     sljit_s32 a, sljit_sw imma,
  351     /* The general operand (not immediate). */
  352     sljit_s32 b, sljit_sw immb)
  353 {
  354     sljit_u8 *inst;
  355     sljit_u8 *buf_ptr;
  356     sljit_s32 flags = size & ~0xf;
  357     sljit_s32 inst_size;
  358 
  359     /* Both cannot be switched on. */
  360     SLJIT_ASSERT((flags & (EX86_BIN_INS | EX86_SHIFT_INS)) != (EX86_BIN_INS | EX86_SHIFT_INS));
  361     /* Size flags not allowed for typed instructions. */
  362     SLJIT_ASSERT(!(flags & (EX86_BIN_INS | EX86_SHIFT_INS)) || (flags & (EX86_BYTE_ARG | EX86_HALF_ARG)) == 0);
  363     /* Both size flags cannot be switched on. */
  364     SLJIT_ASSERT((flags & (EX86_BYTE_ARG | EX86_HALF_ARG)) != (EX86_BYTE_ARG | EX86_HALF_ARG));
  365     /* SSE2 and immediate is not possible. */
  366     SLJIT_ASSERT(!(a & SLJIT_IMM) || !(flags & EX86_SSE2));
  367     SLJIT_ASSERT((flags & (EX86_PREF_F2 | EX86_PREF_F3)) != (EX86_PREF_F2 | EX86_PREF_F3)
  368         && (flags & (EX86_PREF_F2 | EX86_PREF_66)) != (EX86_PREF_F2 | EX86_PREF_66)
  369         && (flags & (EX86_PREF_F3 | EX86_PREF_66)) != (EX86_PREF_F3 | EX86_PREF_66));
  370 
  371     size &= 0xf;
  372     inst_size = size;
  373 
  374     if (flags & (EX86_PREF_F2 | EX86_PREF_F3))
  375         inst_size++;
  376     if (flags & EX86_PREF_66)
  377         inst_size++;
  378 
  379     /* Calculate size of b. */
  380     inst_size += 1; /* mod r/m byte. */
  381     if (b & SLJIT_MEM) {
  382         if ((b & REG_MASK) == SLJIT_UNUSED)
  383             inst_size += sizeof(sljit_sw);
  384         else if (immb != 0 && !(b & OFFS_REG_MASK)) {
  385             /* Immediate operand. */
  386             if (immb <= 127 && immb >= -128)
  387                 inst_size += sizeof(sljit_s8);
  388             else
  389                 inst_size += sizeof(sljit_sw);
  390         }
  391 
  392         if ((b & REG_MASK) == SLJIT_SP && !(b & OFFS_REG_MASK))
  393             b |= TO_OFFS_REG(SLJIT_SP);
  394 
  395         if ((b & OFFS_REG_MASK) != SLJIT_UNUSED)
  396             inst_size += 1; /* SIB byte. */
  397     }
  398 
  399     /* Calculate size of a. */
  400     if (a & SLJIT_IMM) {
  401         if (flags & EX86_BIN_INS) {
  402             if (imma <= 127 && imma >= -128) {
  403                 inst_size += 1;
  404                 flags |= EX86_BYTE_ARG;
  405             } else
  406                 inst_size += 4;
  407         }
  408         else if (flags & EX86_SHIFT_INS) {
  409             imma &= 0x1f;
  410             if (imma != 1) {
  411                 inst_size ++;
  412                 flags |= EX86_BYTE_ARG;
  413             }
  414         } else if (flags & EX86_BYTE_ARG)
  415             inst_size++;
  416         else if (flags & EX86_HALF_ARG)
  417             inst_size += sizeof(short);
  418         else
  419             inst_size += sizeof(sljit_sw);
  420     }
  421     else
  422         SLJIT_ASSERT(!(flags & EX86_SHIFT_INS) || a == SLJIT_PREF_SHIFT_REG);
  423 
  424     inst = (sljit_u8*)ensure_buf(compiler, 1 + inst_size);
  425     PTR_FAIL_IF(!inst);
  426 
  427     /* Encoding the byte. */
  428     INC_SIZE(inst_size);
  429     if (flags & EX86_PREF_F2)
  430         *inst++ = 0xf2;
  431     if (flags & EX86_PREF_F3)
  432         *inst++ = 0xf3;
  433     if (flags & EX86_PREF_66)
  434         *inst++ = 0x66;
  435 
  436     buf_ptr = inst + size;
  437 
  438     /* Encode mod/rm byte. */
  439     if (!(flags & EX86_SHIFT_INS)) {
  440         if ((flags & EX86_BIN_INS) && (a & SLJIT_IMM))
  441             *inst = (flags & EX86_BYTE_ARG) ? GROUP_BINARY_83 : GROUP_BINARY_81;
  442 
  443         if (a & SLJIT_IMM)
  444             *buf_ptr = 0;
  445         else if (!(flags & EX86_SSE2_OP1))
  446             *buf_ptr = reg_map[a] << 3;
  447         else
  448             *buf_ptr = a << 3;
  449     }
  450     else {
  451         if (a & SLJIT_IMM) {
  452             if (imma == 1)
  453                 *inst = GROUP_SHIFT_1;
  454             else
  455                 *inst = GROUP_SHIFT_N;
  456         } else
  457             *inst = GROUP_SHIFT_CL;
  458         *buf_ptr = 0;
  459     }
  460 
  461     if (!(b & SLJIT_MEM))
  462         *buf_ptr++ |= MOD_REG + ((!(flags & EX86_SSE2_OP2)) ? reg_map[b] : b);
  463     else if ((b & REG_MASK) != SLJIT_UNUSED) {
  464         if ((b & OFFS_REG_MASK) == SLJIT_UNUSED || (b & OFFS_REG_MASK) == TO_OFFS_REG(SLJIT_SP)) {
  465             if (immb != 0) {
  466                 if (immb <= 127 && immb >= -128)
  467                     *buf_ptr |= 0x40;
  468                 else
  469                     *buf_ptr |= 0x80;
  470             }
  471 
  472             if ((b & OFFS_REG_MASK) == SLJIT_UNUSED)
  473                 *buf_ptr++ |= reg_map[b & REG_MASK];
  474             else {
  475                 *buf_ptr++ |= 0x04;
  476                 *buf_ptr++ = reg_map[b & REG_MASK] | (reg_map[OFFS_REG(b)] << 3);
  477             }
  478 
  479             if (immb != 0) {
  480                 if (immb <= 127 && immb >= -128)
  481                     *buf_ptr++ = immb; /* 8 bit displacement. */
  482                 else {
  483                     sljit_unaligned_store_sw(buf_ptr, immb); /* 32 bit displacement. */
  484                     buf_ptr += sizeof(sljit_sw);
  485                 }
  486             }
  487         }
  488         else {
  489             *buf_ptr++ |= 0x04;
  490             *buf_ptr++ = reg_map[b & REG_MASK] | (reg_map[OFFS_REG(b)] << 3) | (immb << 6);
  491         }
  492     }
  493     else {
  494         *buf_ptr++ |= 0x05;
  495         sljit_unaligned_store_sw(buf_ptr, immb); /* 32 bit displacement. */
  496         buf_ptr += sizeof(sljit_sw);
  497     }
  498 
  499     if (a & SLJIT_IMM) {
  500         if (flags & EX86_BYTE_ARG)
  501             *buf_ptr = imma;
  502         else if (flags & EX86_HALF_ARG)
  503             sljit_unaligned_store_s16(buf_ptr, imma);
  504         else if (!(flags & EX86_SHIFT_INS))
  505             sljit_unaligned_store_sw(buf_ptr, imma);
  506     }
  507 
  508     return !(flags & EX86_SHIFT_INS) ? inst : (inst + 1);
  509 }
  510 
  511 /* --------------------------------------------------------------------- */
  512 /*  Call / return instructions                                           */
  513 /* --------------------------------------------------------------------- */
  514 
  515 #if (defined SLJIT_X86_32_FASTCALL && SLJIT_X86_32_FASTCALL)
  516 
  517 static sljit_s32 c_fast_call_get_stack_size(sljit_s32 arg_types, sljit_s32 *word_arg_count_ptr)
  518 {
  519     sljit_s32 stack_size = 0;
  520     sljit_s32 word_arg_count = 0;
  521 
  522     arg_types >>= SLJIT_DEF_SHIFT;
  523 
  524     while (arg_types) {
  525         switch (arg_types & SLJIT_DEF_MASK) {
  526         case SLJIT_ARG_TYPE_F32:
  527             stack_size += sizeof(sljit_f32);
  528             break;
  529         case SLJIT_ARG_TYPE_F64:
  530             stack_size += sizeof(sljit_f64);
  531             break;
  532         default:
  533             word_arg_count++;
  534             if (word_arg_count > 2)
  535                 stack_size += sizeof(sljit_sw);
  536             break;
  537         }
  538 
  539         arg_types >>= SLJIT_DEF_SHIFT;
  540     }
  541 
  542     if (word_arg_count_ptr)
  543         *word_arg_count_ptr = word_arg_count;
  544 
  545     return stack_size;
  546 }
  547 
  548 static sljit_s32 c_fast_call_with_args(struct sljit_compiler *compiler,
  549     sljit_s32 arg_types, sljit_s32 stack_size, sljit_s32 word_arg_count, sljit_s32 swap_args)
  550 {
  551     sljit_u8 *inst;
  552     sljit_s32 float_arg_count;
  553 
  554     if (stack_size == sizeof(sljit_sw) && word_arg_count == 3) {
  555         inst = (sljit_u8*)ensure_buf(compiler, 1 + 1);
  556         FAIL_IF(!inst);
  557         INC_SIZE(1);
  558         PUSH_REG(reg_map[SLJIT_R2]);
  559     }
  560     else if (stack_size > 0) {
  561         if (word_arg_count >= 4)
  562             EMIT_MOV(compiler, TMP_REG1, 0, SLJIT_MEM1(SLJIT_SP), compiler->saveds_offset - sizeof(sljit_sw));
  563 
  564         FAIL_IF(emit_non_cum_binary(compiler, BINARY_OPCODE(SUB),
  565             SLJIT_SP, 0, SLJIT_SP, 0, SLJIT_IMM, stack_size));
  566 
  567         stack_size = 0;
  568         arg_types >>= SLJIT_DEF_SHIFT;
  569         word_arg_count = 0;
  570         float_arg_count = 0;
  571         while (arg_types) {
  572             switch (arg_types & SLJIT_DEF_MASK) {
  573             case SLJIT_ARG_TYPE_F32:
  574                 float_arg_count++;
  575                 FAIL_IF(emit_sse2_store(compiler, 1, SLJIT_MEM1(SLJIT_SP), stack_size, float_arg_count));
  576                 stack_size += sizeof(sljit_f32);
  577                 break;
  578             case SLJIT_ARG_TYPE_F64:
  579                 float_arg_count++;
  580                 FAIL_IF(emit_sse2_store(compiler, 0, SLJIT_MEM1(SLJIT_SP), stack_size, float_arg_count));
  581                 stack_size += sizeof(sljit_f64);
  582                 break;
  583             default:
  584                 word_arg_count++;
  585                 if (word_arg_count == 3) {
  586                     EMIT_MOV(compiler, SLJIT_MEM1(SLJIT_SP), stack_size, SLJIT_R2, 0);
  587                     stack_size += sizeof(sljit_sw);
  588                 }
  589                 else if (word_arg_count == 4) {
  590                     EMIT_MOV(compiler, SLJIT_MEM1(SLJIT_SP), stack_size, TMP_REG1, 0);
  591                     stack_size += sizeof(sljit_sw);
  592                 }
  593                 break;
  594             }
  595 
  596             arg_types >>= SLJIT_DEF_SHIFT;
  597         }
  598     }
  599 
  600     if (word_arg_count > 0) {
  601         if (swap_args) {
  602             inst = (sljit_u8*)ensure_buf(compiler, 1 + 1);
  603             FAIL_IF(!inst);
  604             INC_SIZE(1);
  605 
  606             *inst++ = XCHG_EAX_r | reg_map[SLJIT_R2];
  607         }
  608         else {
  609             inst = (sljit_u8*)ensure_buf(compiler, 1 + 2);
  610             FAIL_IF(!inst);
  611             INC_SIZE(2);
  612 
  613             *inst++ = MOV_r_rm;
  614             *inst++ = MOD_REG | (reg_map[SLJIT_R2] << 3) | reg_map[SLJIT_R0];
  615         }
  616     }
  617 
  618     return SLJIT_SUCCESS;
  619 }
  620 
  621 #endif
  622 
  623 static sljit_s32 cdecl_call_get_stack_size(struct sljit_compiler *compiler, sljit_s32 arg_types, sljit_s32 *word_arg_count_ptr)
  624 {
  625     sljit_s32 stack_size = 0;
  626     sljit_s32 word_arg_count = 0;
  627 
  628     arg_types >>= SLJIT_DEF_SHIFT;
  629 
  630     while (arg_types) {
  631         switch (arg_types & SLJIT_DEF_MASK) {
  632         case SLJIT_ARG_TYPE_F32:
  633             stack_size += sizeof(sljit_f32);
  634             break;
  635         case SLJIT_ARG_TYPE_F64:
  636             stack_size += sizeof(sljit_f64);
  637             break;
  638         default:
  639             word_arg_count++;
  640             stack_size += sizeof(sljit_sw);
  641             break;
  642         }
  643 
  644         arg_types >>= SLJIT_DEF_SHIFT;
  645     }
  646 
  647     if (word_arg_count_ptr)
  648         *word_arg_count_ptr = word_arg_count;
  649 
  650     if (stack_size <= compiler->stack_tmp_size)
  651         return 0;
  652 
  653 #if defined(__APPLE__)
  654     return ((stack_size - compiler->stack_tmp_size + 15) & ~15);
  655 #else
  656     return stack_size - compiler->stack_tmp_size;
  657 #endif
  658 }
  659 
  660 static sljit_s32 cdecl_call_with_args(struct sljit_compiler *compiler,
  661     sljit_s32 arg_types, sljit_s32 stack_size, sljit_s32 word_arg_count)
  662 {
  663     sljit_s32 float_arg_count = 0;
  664 
  665     if (word_arg_count >= 4)
  666         EMIT_MOV(compiler, TMP_REG1, 0, SLJIT_MEM1(SLJIT_SP), compiler->saveds_offset - sizeof(sljit_sw));
  667 
  668     if (stack_size > 0)
  669         FAIL_IF(emit_non_cum_binary(compiler, BINARY_OPCODE(SUB),
  670             SLJIT_SP, 0, SLJIT_SP, 0, SLJIT_IMM, stack_size));
  671 
  672     stack_size = 0;
  673     word_arg_count = 0;
  674     arg_types >>= SLJIT_DEF_SHIFT;
  675 
  676     while (arg_types) {
  677         switch (arg_types & SLJIT_DEF_MASK) {
  678         case SLJIT_ARG_TYPE_F32:
  679             float_arg_count++;
  680             FAIL_IF(emit_sse2_store(compiler, 1, SLJIT_MEM1(SLJIT_SP), stack_size, float_arg_count));
  681             stack_size += sizeof(sljit_f32);
  682             break;
  683         case SLJIT_ARG_TYPE_F64:
  684             float_arg_count++;
  685             FAIL_IF(emit_sse2_store(compiler, 0, SLJIT_MEM1(SLJIT_SP), stack_size, float_arg_count));
  686             stack_size += sizeof(sljit_f64);
  687             break;
  688         default:
  689             word_arg_count++;
  690             EMIT_MOV(compiler, SLJIT_MEM1(SLJIT_SP), stack_size, (word_arg_count >= 4) ? TMP_REG1 : word_arg_count, 0);
  691             stack_size += sizeof(sljit_sw);
  692             break;
  693         }
  694 
  695         arg_types >>= SLJIT_DEF_SHIFT;
  696     }
  697 
  698     return SLJIT_SUCCESS;
  699 }
  700 
  701 static sljit_s32 post_call_with_args(struct sljit_compiler *compiler,
  702     sljit_s32 arg_types, sljit_s32 stack_size)
  703 {
  704     sljit_u8 *inst;
  705     sljit_s32 single;
  706 
  707     if (stack_size > 0)
  708         FAIL_IF(emit_cum_binary(compiler, BINARY_OPCODE(ADD),
  709             SLJIT_SP, 0, SLJIT_SP, 0, SLJIT_IMM, stack_size));
  710 
  711     if ((arg_types & SLJIT_DEF_MASK) < SLJIT_ARG_TYPE_F32)
  712         return SLJIT_SUCCESS;
  713 
  714     single = ((arg_types & SLJIT_DEF_MASK) == SLJIT_ARG_TYPE_F32);
  715 
  716     inst = (sljit_u8*)ensure_buf(compiler, 1 + 3);
  717     FAIL_IF(!inst);
  718     INC_SIZE(3);
  719     inst[0] = single ? FSTPS : FSTPD;
  720     inst[1] = (0x03 << 3) | 0x04;
  721     inst[2] = (0x04 << 3) | reg_map[SLJIT_SP];
  722 
  723     return emit_sse2_load(compiler, single, SLJIT_FR0, SLJIT_MEM1(SLJIT_SP), 0);
  724 }
  725 
  726 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_call(struct sljit_compiler *compiler, sljit_s32 type,
  727     sljit_s32 arg_types)
  728 {
  729     struct sljit_jump *jump;
  730     sljit_s32 stack_size = 0;
  731     sljit_s32 word_arg_count;
  732 
  733     CHECK_ERROR_PTR();
  734     CHECK_PTR(check_sljit_emit_call(compiler, type, arg_types));
  735 
  736 #if (defined SLJIT_X86_32_FASTCALL && SLJIT_X86_32_FASTCALL)
  737     if ((type & 0xff) == SLJIT_CALL) {
  738         stack_size = c_fast_call_get_stack_size(arg_types, &word_arg_count);
  739         PTR_FAIL_IF(c_fast_call_with_args(compiler, arg_types, stack_size, word_arg_count, 0));
  740 
  741 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
  742         || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
  743         compiler->skip_checks = 1;
  744 #endif
  745 
  746         jump = sljit_emit_jump(compiler, type);
  747         PTR_FAIL_IF(jump == NULL);
  748 
  749         PTR_FAIL_IF(post_call_with_args(compiler, arg_types, 0));
  750         return jump;
  751     }
  752 #endif
  753 
  754     stack_size = cdecl_call_get_stack_size(compiler, arg_types, &word_arg_count);
  755     PTR_FAIL_IF(cdecl_call_with_args(compiler, arg_types, stack_size, word_arg_count));
  756 
  757 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
  758         || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
  759     compiler->skip_checks = 1;
  760 #endif
  761 
  762     jump = sljit_emit_jump(compiler, type);
  763     PTR_FAIL_IF(jump == NULL);
  764 
  765     PTR_FAIL_IF(post_call_with_args(compiler, arg_types, stack_size));
  766     return jump;
  767 }
  768 
  769 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_icall(struct sljit_compiler *compiler, sljit_s32 type,
  770     sljit_s32 arg_types,
  771     sljit_s32 src, sljit_sw srcw)
  772 {
  773     sljit_s32 stack_size = 0;
  774     sljit_s32 word_arg_count;
  775 #if (defined SLJIT_X86_32_FASTCALL && SLJIT_X86_32_FASTCALL)
  776     sljit_s32 swap_args;
  777 #endif
  778 
  779     CHECK_ERROR();
  780     CHECK(check_sljit_emit_icall(compiler, type, arg_types, src, srcw));
  781 
  782 #if (defined SLJIT_X86_32_FASTCALL && SLJIT_X86_32_FASTCALL)
  783     SLJIT_ASSERT(reg_map[SLJIT_R0] == 0 && reg_map[SLJIT_R2] == 1 && SLJIT_R0 == 1 && SLJIT_R2 == 3);
  784 
  785     if ((type & 0xff) == SLJIT_CALL) {
  786         stack_size = c_fast_call_get_stack_size(arg_types, &word_arg_count);
  787         swap_args = 0;
  788 
  789         if (word_arg_count > 0) {
  790             if ((src & REG_MASK) == SLJIT_R2 || OFFS_REG(src) == SLJIT_R2) {
  791                 swap_args = 1;
  792                 if (((src & REG_MASK) | 0x2) == SLJIT_R2)
  793                     src ^= 0x2;
  794                 if ((OFFS_REG(src) | 0x2) == SLJIT_R2)
  795                     src ^= TO_OFFS_REG(0x2);
  796             }
  797         }
  798 
  799         FAIL_IF(c_fast_call_with_args(compiler, arg_types, stack_size, word_arg_count, swap_args));
  800 
  801         compiler->saveds_offset += stack_size;
  802         compiler->locals_offset += stack_size;
  803 
  804 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
  805         || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
  806         compiler->skip_checks = 1;
  807 #endif
  808         FAIL_IF(sljit_emit_ijump(compiler, type, src, srcw));
  809 
  810         compiler->saveds_offset -= stack_size;
  811         compiler->locals_offset -= stack_size;
  812 
  813         return post_call_with_args(compiler, arg_types, 0);
  814     }
  815 #endif
  816 
  817     stack_size = cdecl_call_get_stack_size(compiler, arg_types, &word_arg_count);
  818     FAIL_IF(cdecl_call_with_args(compiler, arg_types, stack_size, word_arg_count));
  819 
  820     compiler->saveds_offset += stack_size;
  821     compiler->locals_offset += stack_size;
  822 
  823 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
  824         || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
  825     compiler->skip_checks = 1;
  826 #endif
  827     FAIL_IF(sljit_emit_ijump(compiler, type, src, srcw));
  828 
  829     compiler->saveds_offset -= stack_size;
  830     compiler->locals_offset -= stack_size;
  831 
  832     return post_call_with_args(compiler, arg_types, stack_size);
  833 }
  834 
  835 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fast_enter(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw)
  836 {
  837     sljit_u8 *inst;
  838 
  839     CHECK_ERROR();
  840     CHECK(check_sljit_emit_fast_enter(compiler, dst, dstw));
  841     ADJUST_LOCAL_OFFSET(dst, dstw);
  842 
  843     CHECK_EXTRA_REGS(dst, dstw, (void)0);
  844 
  845     /* For UNUSED dst. Uncommon, but possible. */
  846     if (dst == SLJIT_UNUSED)
  847         dst = TMP_REG1;
  848 
  849     if (FAST_IS_REG(dst)) {
  850         /* Unused dest is possible here. */
  851         inst = (sljit_u8*)ensure_buf(compiler, 1 + 1);
  852         FAIL_IF(!inst);
  853 
  854         INC_SIZE(1);
  855         POP_REG(reg_map[dst]);
  856         return SLJIT_SUCCESS;
  857     }
  858 
  859     /* Memory. */
  860     inst = emit_x86_instruction(compiler, 1, 0, 0, dst, dstw);
  861     FAIL_IF(!inst);
  862     *inst++ = POP_rm;
  863     return SLJIT_SUCCESS;
  864 }
  865 
  866 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fast_return(struct sljit_compiler *compiler, sljit_s32 src, sljit_sw srcw)
  867 {
  868     sljit_u8 *inst;
  869 
  870     CHECK_ERROR();
  871     CHECK(check_sljit_emit_fast_return(compiler, src, srcw));
  872     ADJUST_LOCAL_OFFSET(src, srcw);
  873 
  874     CHECK_EXTRA_REGS(src, srcw, (void)0);
  875 
  876     if (FAST_IS_REG(src)) {
  877         inst = (sljit_u8*)ensure_buf(compiler, 1 + 1 + 1);
  878         FAIL_IF(!inst);
  879 
  880         INC_SIZE(1 + 1);
  881         PUSH_REG(reg_map[src]);
  882     }
  883     else {
  884         inst = emit_x86_instruction(compiler, 1, 0, 0, src, srcw);
  885         FAIL_IF(!inst);
  886         *inst++ = GROUP_FF;
  887         *inst |= PUSH_rm;
  888 
  889         inst = (sljit_u8*)ensure_buf(compiler, 1 + 1);
  890         FAIL_IF(!inst);
  891         INC_SIZE(1);
  892     }
  893 
  894     RET();
  895     return SLJIT_SUCCESS;
  896 }