pike.git / src / code / amd64.c

version» Context lines:

pike.git/src/code/amd64.c:4975:   const char *amd64_describe_reg(int rex, int reg)   {    if (rex) reg += 8;    return amd64_registers[reg];   }      size_t amd64_readint32(PIKE_OPCODE_T *pc, char *buf)   {    unsigned INT32 val = ((unsigned INT32 *)pc)[0];    buf += strlen(buf); -  sprintf(buf, "+0x%08x", val); +  sprintf(buf, "0x%08x", val);    return 4;   }      size_t amd64_disassemble_sib(PIKE_OPCODE_T *pc,    char *buf,    const int *UNUSED(legacy_prefix),    int modrm,    int rex)   {    int sib = pc[0];
pike.git/src/code/amd64.c:5077:    return 4;    }    if ((modrm & 0x07) == 0x04) {    bytes += amd64_disassemble_sib(pc + bytes, reg_buf, legacy_prefix,    modrm, rex);    } else {    sprintf(reg_buf, "%s", amd64_describe_reg(rex & 1, modrm & 7));    }    switch(modrm & 0xc0) {    case 0x00: -  sprintf(buf, "%s", reg_buf); +  sprintf(buf, "(%s)", reg_buf);    break;    case 0x40:    sprintf(buf, "%d(%s)", ((signed char *)pc)[bytes], reg_buf);    bytes++;    break;    case 0x80:    bytes += amd64_readint32(pc + bytes, buf);    sprintf(buf + strlen(buf), "(%s)", reg_buf);    break;    }
pike.git/src/code/amd64.c:5115:    const char *opcode = NULL;    const char *params[4] = { NULL, NULL, NULL, NULL, };    int legacy_prefix[4] = { 0, 0, 0, 0 };    int byte;    int rex = 0;    int modrm = 0;    struct amd64_opcode *table = amd64_opcodes[0];    struct amd64_opcode *op;    char buffers[4][256];    - #if 0 -  fprintf(stderr, "%p:\t", pc + pos); - #endif -  +     // Handle prefixes.    while(1) {    byte = pc[pos++];    op = table + byte;    if (op->flags & OP_PREFIX) {    legacy_prefix[op->flags & 3] = byte;    continue;    }    break;    }       /* Handle REX */    if ((byte & 0xf0) == 0x40) {    rex = byte;    byte = pc[pos++];    op = table + byte;    }       while (op->flags & OP_MULTIBYTE) { -  table = amd64_opcodes[1 + op->flags & 0xff]; +  table = amd64_opcodes[1 + (op->flags & 0xff)];    byte = pc[pos++];    op = table + byte;    }       opcode = op->name;       if (op->flags & OP_RM) {    modrm = pc[pos++]; -  params[0] = amd64_describe_reg(rex & 1, modrm & 0x07); -  if (op->flags & OP_OPS) { -  opcode = modrm_ops[op->flags & 0x0f][(modrm >> 3) & 0x07]; -  } else { +     params[0] = amd64_describe_reg(rex & 4, (modrm>>3) & 0x07);    params[1] = buffers[1];    pos += amd64_disassemble_modrm(pc + pos, buffers[1],    legacy_prefix, modrm, rex); -  +  if (op->flags & OP_OPS) { +  params[0] = NULL; +  opcode = modrm_ops[op->flags & 0x0f][(modrm >> 3) & 0x07];    }    }       if (op->flags & OP_REG) {    int reg = byte & 0x07; -  params[0] = amd64_describe_reg(rex & 1, byte & 0x07); +  params[1] = amd64_describe_reg(rex & 1, byte & 0x07);    }       if (op->flags & OP_IMM) { -  if (!params[1]) { -  params[1] = buffers[1]; -  buffers[1][0] = 0; +  if (!params[0]) { +  params[0] = buffers[0]; +  buffers[0][0] = 0;    }    if (op->flags & (OP_8|OP_S8)) { -  sprintf(buffers[1] + strlen(buffers[1]), "$%+d", ((signed char *)pc)[pos++]); +  sprintf(buffers[0] + strlen(buffers[0]), "$%d", ((signed char *)pc)[pos++]);    } else { -  sprintf(buffers[1] + strlen(buffers[1]), "$"); -  pos += amd64_readint32(pc + pos, buffers[1] + strlen(buffers[1])); +  sprintf(buffers[0] + strlen(buffers[0]), "$"); +  pos += amd64_readint32(pc + pos, buffers[0] + strlen(buffers[0]));    }    } else if (op->flags & OP_PCREL) {    INT32 val;    if (!params[0]) {    params[0] = buffers[0];    buffers[0][0] = 0;    }    if (op->flags & (OP_8|OP_S8)) {    val = ((signed char *)pc)[pos++];    } else {
pike.git/src/code/amd64.c:5196:    sprintf(buffers[0] + strlen(buffers[0]), "%p", pc + (pos + val));    }       if (op->flags & OP_B_RM) {    /* Swap the two parameters. */    const char *tmp = params[0];    params[0] = params[1];    params[1] = tmp;    }    - #if 1 -  string_builder_append_disassembly(&buf, pc + op_start, pc + pos, -  opcode, params, NULL); - #else -  if (opcode) { -  fprintf(stderr, "%s", opcode); +  if (!params[0]) { +  params[0] = params[1]; +  params[1] = NULL; +  }    -  +  if (!opcode) { +  opcode = ".byte"; +  } +     if (params[0]) { -  fprintf(stderr, " %s", params[0]); -  if (params[1]) { -  fprintf(stderr, ","); +  if ((params[0][0] == '%') && +  (!strcmp(opcode, "call") || !strcmp(opcode, "jmp"))) { +  /* NB: We know that these opcodes only have one parameter. */ +  /* Add the star prefix. */ +  sprintf(buffers[0], "*%s", params[0]); +  params[0] = buffers[0];    }    }    -  if (params[1]) { -  fprintf(stderr, " %s", params[1]); +  string_builder_append_disassembly(&buf, pc + op_start, pc + pos, +  opcode, params, NULL);    } -  } else { -  fprintf(stderr, ".byte 0x%02x", byte); -  } -  for(i = 0; (op_start + i) < pos; i++) { -  if (!(i & 7)) { -  fprintf(stderr, "\n\t#"); -  } -  fprintf(stderr, " %02x", pc[op_start + i]); -  } -  fprintf(stderr, "\n"); - #endif -  } - #if 1 +  +  /* NUL-terminate. */    string_builder_putchar(&buf, 0);    fprintf(stderr, "%s", buf.s->str);    free_string_builder(&buf); - #endif +    }