Blame x86/disp8.c

Packit c1f960
/* ----------------------------------------------------------------------- *
Packit c1f960
 *
Packit c1f960
 *   Copyright 1996-2013 The NASM Authors - All Rights Reserved
Packit c1f960
 *   See the file AUTHORS included with the NASM distribution for
Packit c1f960
 *   the specific copyright holders.
Packit c1f960
 *
Packit c1f960
 *   Redistribution and use in source and binary forms, with or without
Packit c1f960
 *   modification, are permitted provided that the following
Packit c1f960
 *   conditions are met:
Packit c1f960
 *
Packit c1f960
 *   * Redistributions of source code must retain the above copyright
Packit c1f960
 *     notice, this list of conditions and the following disclaimer.
Packit c1f960
 *   * Redistributions in binary form must reproduce the above
Packit c1f960
 *     copyright notice, this list of conditions and the following
Packit c1f960
 *     disclaimer in the documentation and/or other materials provided
Packit c1f960
 *     with the distribution.
Packit c1f960
 *
Packit c1f960
 *     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
Packit c1f960
 *     CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
Packit c1f960
 *     INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
Packit c1f960
 *     MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Packit c1f960
 *     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
Packit c1f960
 *     CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Packit c1f960
 *     SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
Packit c1f960
 *     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
Packit c1f960
 *     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
Packit c1f960
 *     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
Packit c1f960
 *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
Packit c1f960
 *     OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
Packit c1f960
 *     EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit c1f960
 *
Packit c1f960
 * ----------------------------------------------------------------------- */
Packit c1f960
Packit c1f960
/*
Packit c1f960
 * disp8.c   : Contains a common logic for EVEX compressed displacement
Packit c1f960
 */
Packit c1f960
Packit c1f960
#include "disp8.h"
Packit c1f960
Packit c1f960
/*
Packit c1f960
 * Find N value for compressed displacement (disp8 * N)
Packit c1f960
 */
Packit c1f960
uint8_t get_disp8N(insn *ins)
Packit c1f960
{
Packit c1f960
    static const uint8_t fv_n[2][2][VLMAX] = {{{16, 32, 64}, {4, 4, 4}},
Packit c1f960
                                              {{16, 32, 64}, {8, 8, 8}}};
Packit c1f960
    static const uint8_t hv_n[2][VLMAX]    =  {{8, 16, 32}, {4, 4, 4}};
Packit c1f960
    static const uint8_t dup_n[VLMAX]      =   {8, 32, 64};
Packit c1f960
Packit c1f960
    bool evex_b           = (ins->evex_p[2] & EVEX_P2B) >> 4;
Packit c1f960
    enum ttypes   tuple   = ins->evex_tuple;
Packit c1f960
    enum vectlens vectlen = (ins->evex_p[2] & EVEX_P2LL) >> 5;
Packit c1f960
    bool evex_w           = (ins->evex_p[1] & EVEX_P1W) >> 7;
Packit c1f960
    uint8_t n = 0;
Packit c1f960
Packit c1f960
    switch(tuple) {
Packit c1f960
    case FV:
Packit c1f960
        n = fv_n[evex_w][evex_b][vectlen];
Packit c1f960
        break;
Packit c1f960
    case HV:
Packit c1f960
        n = hv_n[evex_b][vectlen];
Packit c1f960
        break;
Packit c1f960
Packit c1f960
    case FVM:
Packit c1f960
        /* 16, 32, 64 for VL 128, 256, 512 respectively*/
Packit c1f960
        n = 1 << (vectlen + 4);
Packit c1f960
        break;
Packit c1f960
    case T1S8:  /* N = 1 */
Packit c1f960
    case T1S16: /* N = 2 */
Packit c1f960
        n = tuple - T1S8 + 1;
Packit c1f960
        break;
Packit c1f960
    case T1S:
Packit c1f960
        /* N = 4 for 32bit, 8 for 64bit */
Packit c1f960
        n = evex_w ? 8 : 4;
Packit c1f960
        break;
Packit c1f960
    case T1F32:
Packit c1f960
    case T1F64:
Packit c1f960
        /* N = 4 for 32bit, 8 for 64bit */
Packit c1f960
        n = (tuple == T1F32 ? 4 : 8);
Packit c1f960
        break;
Packit c1f960
    case T2:
Packit c1f960
    case T4:
Packit c1f960
    case T8:
Packit c1f960
        if (vectlen + 7 <= (evex_w + 5) + (tuple - T2 + 1))
Packit c1f960
            n = 0;
Packit c1f960
        else
Packit c1f960
            n = 1 << (tuple - T2 + evex_w + 3);
Packit c1f960
        break;
Packit c1f960
    case HVM:
Packit c1f960
    case QVM:
Packit c1f960
    case OVM:
Packit c1f960
        n = 1 << (OVM - tuple + vectlen + 1);
Packit c1f960
        break;
Packit c1f960
    case M128:
Packit c1f960
        n = 16;
Packit c1f960
        break;
Packit c1f960
    case DUP:
Packit c1f960
        n = dup_n[vectlen];
Packit c1f960
        break;
Packit c1f960
Packit c1f960
    default:
Packit c1f960
        break;
Packit c1f960
    }
Packit c1f960
Packit c1f960
    return n;
Packit c1f960
}
Packit c1f960
Packit c1f960
/*
Packit c1f960
 * Check if offset is a multiple of N with corresponding tuple type
Packit c1f960
 * if Disp8*N is available, compressed displacement is stored in compdisp
Packit c1f960
 */
Packit c1f960
bool is_disp8n(operand *input, insn *ins, int8_t *compdisp)
Packit c1f960
{
Packit c1f960
    int32_t off           = input->offset;
Packit c1f960
    uint8_t n;
Packit c1f960
    int32_t disp8;
Packit c1f960
Packit c1f960
    n = get_disp8N(ins);
Packit c1f960
Packit c1f960
    if (n && !(off & (n - 1))) {
Packit c1f960
        disp8 = off / n;
Packit c1f960
        /* if it fits in Disp8 */
Packit c1f960
        if (disp8 >= -128 && disp8 <= 127) {
Packit c1f960
            *compdisp = disp8;
Packit c1f960
            return true;
Packit c1f960
        }
Packit c1f960
    }
Packit c1f960
Packit c1f960
    *compdisp = 0;
Packit c1f960
    return false;
Packit c1f960
}