2002-05-01 04:51:32 +08:00
|
|
|
/* assemble.c code generation for the Netwide Assembler
|
|
|
|
*
|
|
|
|
* The Netwide Assembler is copyright (C) 1996 Simon Tatham and
|
|
|
|
* Julian Hall. All rights reserved. The software is
|
|
|
|
* redistributable under the licence given in the file "Licence"
|
|
|
|
* distributed in the NASM archive.
|
|
|
|
*
|
|
|
|
* the actual codes (C syntax, i.e. octal):
|
|
|
|
* \0 - terminates the code. (Unless it's a literal of course.)
|
|
|
|
* \1, \2, \3 - that many literal bytes follow in the code stream
|
|
|
|
* \4, \6 - the POP/PUSH (respectively) codes for CS, DS, ES, SS
|
|
|
|
* (POP is never used for CS) depending on operand 0
|
|
|
|
* \5, \7 - the second byte of POP/PUSH codes for FS, GS, depending
|
|
|
|
* on operand 0
|
2007-09-18 06:49:30 +08:00
|
|
|
* \10..\13 - a literal byte follows in the code stream, to be added
|
|
|
|
* to the register value of operand 0..3
|
|
|
|
* \14..\17 - a signed byte immediate operand, from operand 0..3
|
|
|
|
* \20..\23 - a byte immediate operand, from operand 0..3
|
|
|
|
* \24..\27 - an unsigned byte immediate operand, from operand 0..3
|
|
|
|
* \30..\33 - a word immediate operand, from operand 0..3
|
|
|
|
* \34..\37 - select between \3[0-3] and \4[0-3] depending on 16/32 bit
|
2002-05-28 07:19:35 +08:00
|
|
|
* assembly mode or the operand-size override on the operand
|
2007-09-18 06:49:30 +08:00
|
|
|
* \40..\43 - a long immediate operand, from operand 0..3
|
|
|
|
* \44..\47 - select between \3[0-3], \4[0-3] and \5[4-7]
|
2007-04-16 10:02:06 +08:00
|
|
|
* depending on assembly mode or the address-size override
|
|
|
|
* on the operand.
|
2007-09-18 06:49:30 +08:00
|
|
|
* \50..\53 - a byte relative operand, from operand 0..3
|
|
|
|
* \54..\57 - a qword immediate operand, from operand 0..3
|
|
|
|
* \60..\63 - a word relative operand, from operand 0..3
|
|
|
|
* \64..\67 - select between \6[0-3] and \7[0-3] depending on 16/32 bit
|
2002-05-21 11:31:21 +08:00
|
|
|
* assembly mode or the operand-size override on the operand
|
2007-09-18 06:49:30 +08:00
|
|
|
* \70..\73 - a long relative operand, from operand 0..3
|
|
|
|
* \74..\77 - a word constant, from the _segment_ part of operand 0..3
|
2002-05-01 04:51:32 +08:00
|
|
|
* \1ab - a ModRM, calculated on EA in operand a, with the spare
|
|
|
|
* field the register value of operand b.
|
2007-09-18 06:49:30 +08:00
|
|
|
* \140..\143 - an immediate word or signed byte for operand 0..3
|
|
|
|
* \144..\147 - or 2 (s-field) into next opcode byte if operand 0..3
|
2002-05-01 04:59:21 +08:00
|
|
|
* is a signed byte rather than a word.
|
2007-09-18 06:49:30 +08:00
|
|
|
* \150..\153 - an immediate dword or signed byte for operand 0..3
|
|
|
|
* \154..\157 - or 2 (s-field) into next opcode byte if operand 0..3
|
2002-05-01 04:59:21 +08:00
|
|
|
* is a signed byte rather than a dword.
|
2007-09-18 07:55:04 +08:00
|
|
|
* \160..\163 - this instruction uses DREX rather than REX, with the
|
|
|
|
* OC0 field set to 0, and the dest field taken from
|
|
|
|
* operand 0..3.
|
|
|
|
* \164..\167 - this instruction uses DREX rather than REX, with the
|
|
|
|
* OC0 field set to 1, and the dest field taken from
|
|
|
|
* operand 0..3.
|
2007-09-18 06:49:30 +08:00
|
|
|
* \170 - encodes the literal byte 0. (Some compilers don't take
|
|
|
|
* kindly to a zero byte in the _middle_ of a compile time
|
|
|
|
* string constant, so I had to put this hack in.)
|
2007-09-18 07:55:04 +08:00
|
|
|
* \171 - placement of DREX suffix in the absence of an EA
|
2002-05-01 04:51:32 +08:00
|
|
|
* \2ab - a ModRM, calculated on EA in operand a, with the spare
|
|
|
|
* field equal to digit b.
|
|
|
|
* \310 - indicates fixed 16-bit address size, i.e. optional 0x67.
|
|
|
|
* \311 - indicates fixed 32-bit address size, i.e. optional 0x67.
|
2007-08-17 15:37:52 +08:00
|
|
|
* \312 - (disassembler only) marker on LOOP, LOOPxx instructions.
|
2007-05-31 06:21:11 +08:00
|
|
|
* \313 - indicates fixed 64-bit address size, 0x67 invalid.
|
2002-05-01 04:51:32 +08:00
|
|
|
* \320 - indicates fixed 16-bit operand size, i.e. optional 0x66.
|
|
|
|
* \321 - indicates fixed 32-bit operand size, i.e. optional 0x66.
|
|
|
|
* \322 - indicates that this instruction is only valid when the
|
|
|
|
* operand size is the default (instruction to disassembler,
|
|
|
|
* generates no code in the assembler)
|
2007-05-31 06:21:11 +08:00
|
|
|
* \323 - indicates fixed 64-bit operand size, REX on extensions only.
|
2007-04-12 10:40:54 +08:00
|
|
|
* \324 - indicates 64-bit operand size requiring REX prefix.
|
2002-05-01 04:51:32 +08:00
|
|
|
* \330 - a literal byte follows in the code stream, to be added
|
|
|
|
* to the condition code value of the instruction.
|
2007-08-17 15:37:52 +08:00
|
|
|
* \331 - instruction not valid with REP prefix. Hint for
|
2002-05-01 04:57:59 +08:00
|
|
|
* disassembler only; for SSE instructions.
|
2007-09-13 12:58:51 +08:00
|
|
|
* \332 - REP prefix (0xF2 byte) used as opcode extension.
|
|
|
|
* \333 - REP prefix (0xF3 byte) used as opcode extension.
|
2007-08-17 15:37:52 +08:00
|
|
|
* \334 - LOCK prefix used instead of REX.R
|
2007-09-13 12:58:51 +08:00
|
|
|
* \335 - disassemble a rep (0xF3 byte) prefix as repe not rep.
|
2007-04-12 10:40:54 +08:00
|
|
|
* \340 - reserve <operand 0> bytes of uninitialized storage.
|
2002-05-01 04:51:32 +08:00
|
|
|
* Operand 0 had better be a segmentless constant.
|
2007-09-12 06:44:03 +08:00
|
|
|
* \364 - operand-size prefix (0x66) not permitted
|
|
|
|
* \365 - address-size prefix (0x67) not permitted
|
|
|
|
* \366 - operand-size prefix (0x66) used as opcode extension
|
|
|
|
* \367 - address-size prefix (0x67) used as opcode extension
|
2002-05-01 05:02:01 +08:00
|
|
|
* \370,\371,\372 - match only if operand 0 meets byte jump criteria.
|
|
|
|
* 370 is used for Jcc, 371 is used for JMP.
|
2002-05-01 04:59:21 +08:00
|
|
|
* \373 - assemble 0x03 if bits==16, 0x05 if bits==32;
|
|
|
|
* used for conditional jump over longer jump
|
2002-05-01 04:51:32 +08:00
|
|
|
*/
|
|
|
|
|
2007-10-03 12:53:51 +08:00
|
|
|
#include "compiler.h"
|
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2007-04-12 10:40:54 +08:00
|
|
|
#include <inttypes.h>
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
#include "nasm.h"
|
2002-05-01 04:52:26 +08:00
|
|
|
#include "nasmlib.h"
|
2002-05-01 04:51:32 +08:00
|
|
|
#include "assemble.h"
|
|
|
|
#include "insns.h"
|
2003-08-27 19:33:56 +08:00
|
|
|
#include "preproc.h"
|
2007-05-30 11:25:21 +08:00
|
|
|
#include "regflags.c"
|
2007-04-12 10:40:54 +08:00
|
|
|
#include "regvals.c"
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
typedef struct {
|
2007-04-12 10:40:54 +08:00
|
|
|
int sib_present; /* is a SIB byte necessary? */
|
|
|
|
int bytes; /* # of bytes of offset needed */
|
|
|
|
int size; /* lazy - this is sib+bytes+1 */
|
|
|
|
uint8_t modrm, sib, rex, rip; /* the bytes themselves */
|
2002-05-01 04:51:32 +08:00
|
|
|
} ea;
|
|
|
|
|
2007-04-12 10:40:54 +08:00
|
|
|
static uint32_t cpu; /* cpu level received from nasm.c */
|
2002-05-01 04:51:32 +08:00
|
|
|
static efunc errfunc;
|
|
|
|
static struct ofmt *outfmt;
|
2002-05-01 04:52:26 +08:00
|
|
|
static ListGen *list;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2007-04-14 00:47:53 +08:00
|
|
|
static int32_t calcsize(int32_t, int32_t, int, insn *, const char *);
|
|
|
|
static void gencode(int32_t, int32_t, int, insn *, const char *, int32_t);
|
2007-09-11 12:16:57 +08:00
|
|
|
static int matches(const struct itemplate *, insn *, int bits);
|
2007-05-30 11:25:21 +08:00
|
|
|
static int32_t regflag(const operand *);
|
|
|
|
static int32_t regval(const operand *);
|
|
|
|
static int rexflags(int, int32_t, int);
|
|
|
|
static int op_rexflags(const operand *, int);
|
2007-07-07 10:01:08 +08:00
|
|
|
static ea *process_ea(operand *, ea *, int, int, int32_t, int);
|
2007-09-23 12:49:51 +08:00
|
|
|
static void add_asp(insn *, int);
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2007-09-23 12:49:51 +08:00
|
|
|
static int has_prefix(insn * ins, enum prefixes prefix)
|
2007-04-18 04:23:11 +08:00
|
|
|
{
|
|
|
|
int j;
|
|
|
|
for (j = 0; j < ins->nprefix; j++) {
|
2007-09-23 12:49:51 +08:00
|
|
|
if (ins->prefixes[j] == prefix)
|
|
|
|
return 1;
|
2007-04-18 04:23:11 +08:00
|
|
|
}
|
2007-09-23 12:49:51 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void assert_no_prefix(insn * ins, enum prefixes prefix)
|
|
|
|
{
|
|
|
|
if (has_prefix(ins, prefix))
|
|
|
|
errfunc(ERR_NONFATAL, "invalid %s prefix", prefix_name(prefix));
|
2007-04-18 04:23:11 +08:00
|
|
|
}
|
|
|
|
|
2002-05-01 04:52:26 +08:00
|
|
|
/*
|
|
|
|
* This routine wrappers the real output format's output routine,
|
|
|
|
* in order to pass a copy of the data off to the listing file
|
|
|
|
* generator at the same time.
|
|
|
|
*/
|
2007-04-12 10:40:54 +08:00
|
|
|
static void out(int32_t offset, int32_t segto, const void *data,
|
|
|
|
uint32_t type, int32_t segment, int32_t wrt)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2007-04-12 10:40:54 +08:00
|
|
|
static int32_t lineno = 0; /* static!!! */
|
2007-04-14 00:47:53 +08:00
|
|
|
static char *lnfname = NULL;
|
2002-05-01 04:53:55 +08:00
|
|
|
|
2002-05-01 04:52:26 +08:00
|
|
|
if ((type & OUT_TYPMASK) == OUT_ADDRESS) {
|
2005-01-16 06:15:51 +08:00
|
|
|
if (segment != NO_SEG || wrt != NO_SEG) {
|
|
|
|
/*
|
|
|
|
* This address is relocated. We must write it as
|
|
|
|
* OUT_ADDRESS, so there's no work to be done here.
|
|
|
|
*/
|
|
|
|
list->output(offset, data, type);
|
|
|
|
} else {
|
2007-04-12 10:40:54 +08:00
|
|
|
uint8_t p[8], *q = p;
|
2005-01-16 06:15:51 +08:00
|
|
|
/*
|
|
|
|
* This is a non-relocated address, and we're going to
|
|
|
|
* convert it into RAWDATA format.
|
|
|
|
*/
|
|
|
|
if ((type & OUT_SIZMASK) == 4) {
|
2007-04-12 10:40:54 +08:00
|
|
|
WRITELONG(q, *(int32_t *)data);
|
2005-01-16 06:15:51 +08:00
|
|
|
list->output(offset, p, OUT_RAWDATA + 4);
|
2007-04-12 10:40:54 +08:00
|
|
|
} else if ((type & OUT_SIZMASK) == 8) {
|
|
|
|
WRITEDLONG(q, *(int64_t *)data);
|
|
|
|
list->output(offset, p, OUT_RAWDATA + 8);
|
2005-01-16 06:15:51 +08:00
|
|
|
} else {
|
2007-04-12 10:40:54 +08:00
|
|
|
WRITESHORT(q, *(int32_t *)data);
|
2005-01-16 06:15:51 +08:00
|
|
|
list->output(offset, p, OUT_RAWDATA + 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if ((type & OUT_TYPMASK) == OUT_RAWDATA) {
|
|
|
|
list->output(offset, data, type);
|
|
|
|
} else if ((type & OUT_TYPMASK) == OUT_RESERVE) {
|
|
|
|
list->output(offset, NULL, type);
|
|
|
|
} else if ((type & OUT_TYPMASK) == OUT_REL2ADR ||
|
|
|
|
(type & OUT_TYPMASK) == OUT_REL4ADR) {
|
|
|
|
list->output(offset, data, type);
|
2002-05-01 04:52:26 +08:00
|
|
|
}
|
|
|
|
|
2003-09-06 12:45:37 +08:00
|
|
|
/*
|
|
|
|
* this call to src_get determines when we call the
|
|
|
|
* debug-format-specific "linenum" function
|
|
|
|
* it updates lineno and lnfname to the current values
|
|
|
|
* returning 0 if "same as last time", -2 if lnfname
|
|
|
|
* changed, and the amount by which lineno changed,
|
|
|
|
* if it did. thus, these variables must be static
|
|
|
|
*/
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
if (src_get(&lineno, &lnfname)) {
|
|
|
|
outfmt->current_dfmt->linenum(lnfname, lineno, segto);
|
2002-05-01 05:02:23 +08:00
|
|
|
}
|
2002-05-01 04:53:55 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
outfmt->output(segto, data, type, segment, wrt);
|
2002-05-01 04:52:26 +08:00
|
|
|
}
|
|
|
|
|
2007-04-12 10:40:54 +08:00
|
|
|
static int jmp_match(int32_t segment, int32_t offset, int bits,
|
2007-04-14 00:47:53 +08:00
|
|
|
insn * ins, const char *code)
|
2005-01-16 06:15:51 +08:00
|
|
|
{
|
2007-04-12 10:40:54 +08:00
|
|
|
int32_t isize;
|
|
|
|
uint8_t c = code[0];
|
2002-05-01 04:59:21 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
if (c != 0370 && c != 0371)
|
|
|
|
return 0;
|
2002-05-01 05:02:01 +08:00
|
|
|
if (ins->oprs[0].opflags & OPFLAG_FORWARD) {
|
2005-01-16 06:15:51 +08:00
|
|
|
if ((optimizing < 0 || (ins->oprs[0].type & STRICT))
|
|
|
|
&& c == 0370)
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return (pass0 == 0); /* match a forward reference */
|
2002-05-01 05:02:01 +08:00
|
|
|
}
|
2005-01-16 06:15:51 +08:00
|
|
|
isize = calcsize(segment, offset, bits, ins, code);
|
|
|
|
if (ins->oprs[0].segment != segment)
|
|
|
|
return 0;
|
|
|
|
isize = ins->oprs[0].offset - offset - isize; /* isize is now the delta */
|
|
|
|
if (isize >= -128L && isize <= 127L)
|
|
|
|
return 1; /* it is byte size */
|
2002-05-01 04:59:21 +08:00
|
|
|
|
|
|
|
return 0;
|
2005-01-16 06:15:51 +08:00
|
|
|
}
|
2002-05-01 04:59:21 +08:00
|
|
|
|
2007-04-12 10:40:54 +08:00
|
|
|
int32_t assemble(int32_t segment, int32_t offset, int bits, uint32_t cp,
|
2005-01-16 06:15:51 +08:00
|
|
|
insn * instruction, struct ofmt *output, efunc error,
|
|
|
|
ListGen * listgen)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2007-09-11 12:16:57 +08:00
|
|
|
const struct itemplate *temp;
|
2005-01-16 06:15:51 +08:00
|
|
|
int j;
|
|
|
|
int size_prob;
|
2007-04-12 10:40:54 +08:00
|
|
|
int32_t insn_end;
|
|
|
|
int32_t itimes;
|
|
|
|
int32_t start = offset;
|
|
|
|
int32_t wsize = 0; /* size for DB etc. */
|
2005-01-16 06:15:51 +08:00
|
|
|
|
|
|
|
errfunc = error; /* to pass to other functions */
|
2002-05-01 04:59:21 +08:00
|
|
|
cpu = cp;
|
2005-01-16 06:15:51 +08:00
|
|
|
outfmt = output; /* likewise */
|
|
|
|
list = listgen; /* and again */
|
|
|
|
|
|
|
|
switch (instruction->opcode) {
|
|
|
|
case -1:
|
|
|
|
return 0;
|
|
|
|
case I_DB:
|
|
|
|
wsize = 1;
|
|
|
|
break;
|
|
|
|
case I_DW:
|
|
|
|
wsize = 2;
|
|
|
|
break;
|
|
|
|
case I_DD:
|
|
|
|
wsize = 4;
|
|
|
|
break;
|
|
|
|
case I_DQ:
|
|
|
|
wsize = 8;
|
|
|
|
break;
|
|
|
|
case I_DT:
|
|
|
|
wsize = 10;
|
|
|
|
break;
|
2007-09-19 08:49:09 +08:00
|
|
|
case I_DO:
|
|
|
|
wsize = 16;
|
|
|
|
break;
|
2007-09-13 11:27:41 +08:00
|
|
|
default:
|
|
|
|
break;
|
2002-05-01 04:53:55 +08:00
|
|
|
}
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2002-05-01 04:53:55 +08:00
|
|
|
if (wsize) {
|
2005-01-16 06:15:51 +08:00
|
|
|
extop *e;
|
2007-04-12 10:40:54 +08:00
|
|
|
int32_t t = instruction->times;
|
2005-01-16 06:15:51 +08:00
|
|
|
if (t < 0)
|
|
|
|
errfunc(ERR_PANIC,
|
|
|
|
"instruction->times < 0 (%ld) in assemble()", t);
|
|
|
|
|
|
|
|
while (t--) { /* repeat TIMES times */
|
|
|
|
for (e = instruction->eops; e; e = e->next) {
|
|
|
|
if (e->type == EOT_DB_NUMBER) {
|
|
|
|
if (wsize == 1) {
|
|
|
|
if (e->segment != NO_SEG)
|
|
|
|
errfunc(ERR_NONFATAL,
|
|
|
|
"one-byte relocation attempted");
|
|
|
|
else {
|
2007-04-12 10:40:54 +08:00
|
|
|
uint8_t out_byte = e->offset;
|
2005-01-16 06:15:51 +08:00
|
|
|
out(offset, segment, &out_byte,
|
|
|
|
OUT_RAWDATA + 1, NO_SEG, NO_SEG);
|
|
|
|
}
|
2007-04-15 02:54:52 +08:00
|
|
|
} else if (wsize > 8) {
|
2007-09-23 12:49:51 +08:00
|
|
|
errfunc(ERR_NONFATAL, "integer supplied to a DT or DO"
|
2007-04-15 02:54:52 +08:00
|
|
|
" instruction");
|
2005-01-16 06:15:51 +08:00
|
|
|
} else
|
|
|
|
out(offset, segment, &e->offset,
|
|
|
|
OUT_ADDRESS + wsize, e->segment, e->wrt);
|
|
|
|
offset += wsize;
|
|
|
|
} else if (e->type == EOT_DB_STRING) {
|
|
|
|
int align;
|
|
|
|
|
|
|
|
out(offset, segment, e->stringval,
|
|
|
|
OUT_RAWDATA + e->stringlen, NO_SEG, NO_SEG);
|
|
|
|
align = e->stringlen % wsize;
|
|
|
|
|
|
|
|
if (align) {
|
|
|
|
align = wsize - align;
|
|
|
|
out(offset, segment, "\0\0\0\0\0\0\0\0",
|
|
|
|
OUT_RAWDATA + align, NO_SEG, NO_SEG);
|
|
|
|
}
|
|
|
|
offset += e->stringlen + align;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (t > 0 && t == instruction->times - 1) {
|
|
|
|
/*
|
|
|
|
* Dummy call to list->output to give the offset to the
|
|
|
|
* listing module.
|
|
|
|
*/
|
|
|
|
list->output(offset, NULL, OUT_RAWDATA);
|
|
|
|
list->uplevel(LIST_TIMES);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (instruction->times > 1)
|
|
|
|
list->downlevel(LIST_TIMES);
|
|
|
|
return offset - start;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
if (instruction->opcode == I_INCBIN) {
|
2007-04-14 00:47:53 +08:00
|
|
|
static char fname[FILENAME_MAX];
|
2005-01-16 06:15:51 +08:00
|
|
|
FILE *fp;
|
2007-04-12 10:40:54 +08:00
|
|
|
int32_t len;
|
2007-04-14 00:47:53 +08:00
|
|
|
char *prefix = "", *combine;
|
|
|
|
char **pPrevPath = NULL;
|
2005-01-16 06:15:51 +08:00
|
|
|
|
|
|
|
len = FILENAME_MAX - 1;
|
|
|
|
if (len > instruction->eops->stringlen)
|
|
|
|
len = instruction->eops->stringlen;
|
|
|
|
strncpy(fname, instruction->eops->stringval, len);
|
|
|
|
fname[len] = '\0';
|
|
|
|
|
2007-04-12 10:40:54 +08:00
|
|
|
while (1) { /* added by alexfru: 'incbin' uses include paths */
|
2005-01-16 06:15:51 +08:00
|
|
|
combine = nasm_malloc(strlen(prefix) + len + 1);
|
|
|
|
strcpy(combine, prefix);
|
|
|
|
strcat(combine, fname);
|
|
|
|
|
|
|
|
if ((fp = fopen(combine, "rb")) != NULL) {
|
|
|
|
nasm_free(combine);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
nasm_free(combine);
|
|
|
|
pPrevPath = pp_get_include_path_ptr(pPrevPath);
|
|
|
|
if (pPrevPath == NULL)
|
|
|
|
break;
|
|
|
|
prefix = *pPrevPath;
|
2003-08-27 19:33:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (fp == NULL)
|
2005-01-16 06:15:51 +08:00
|
|
|
error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
|
|
|
|
fname);
|
|
|
|
else if (fseek(fp, 0L, SEEK_END) < 0)
|
|
|
|
error(ERR_NONFATAL, "`incbin': unable to seek on file `%s'",
|
|
|
|
fname);
|
|
|
|
else {
|
2007-04-14 00:47:53 +08:00
|
|
|
static char buf[2048];
|
2007-04-12 10:40:54 +08:00
|
|
|
int32_t t = instruction->times;
|
|
|
|
int32_t base = 0;
|
2005-01-16 06:15:51 +08:00
|
|
|
|
|
|
|
len = ftell(fp);
|
|
|
|
if (instruction->eops->next) {
|
|
|
|
base = instruction->eops->next->offset;
|
|
|
|
len -= base;
|
|
|
|
if (instruction->eops->next->next &&
|
|
|
|
len > instruction->eops->next->next->offset)
|
|
|
|
len = instruction->eops->next->next->offset;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Dummy call to list->output to give the offset to the
|
|
|
|
* listing module.
|
|
|
|
*/
|
|
|
|
list->output(offset, NULL, OUT_RAWDATA);
|
|
|
|
list->uplevel(LIST_INCBIN);
|
|
|
|
while (t--) {
|
2007-04-12 10:40:54 +08:00
|
|
|
int32_t l;
|
2005-01-16 06:15:51 +08:00
|
|
|
|
|
|
|
fseek(fp, base, SEEK_SET);
|
|
|
|
l = len;
|
|
|
|
while (l > 0) {
|
2007-04-12 10:40:54 +08:00
|
|
|
int32_t m =
|
2005-01-16 06:15:51 +08:00
|
|
|
fread(buf, 1, (l > sizeof(buf) ? sizeof(buf) : l),
|
|
|
|
fp);
|
|
|
|
if (!m) {
|
|
|
|
/*
|
|
|
|
* This shouldn't happen unless the file
|
|
|
|
* actually changes while we are reading
|
|
|
|
* it.
|
|
|
|
*/
|
|
|
|
error(ERR_NONFATAL,
|
|
|
|
"`incbin': unexpected EOF while"
|
|
|
|
" reading file `%s'", fname);
|
|
|
|
t = 0; /* Try to exit cleanly */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
out(offset, segment, buf, OUT_RAWDATA + m,
|
|
|
|
NO_SEG, NO_SEG);
|
|
|
|
l -= m;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
list->downlevel(LIST_INCBIN);
|
|
|
|
if (instruction->times > 1) {
|
|
|
|
/*
|
|
|
|
* Dummy call to list->output to give the offset to the
|
|
|
|
* listing module.
|
|
|
|
*/
|
|
|
|
list->output(offset, NULL, OUT_RAWDATA);
|
|
|
|
list->uplevel(LIST_TIMES);
|
|
|
|
list->downlevel(LIST_TIMES);
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
return instruction->times * len;
|
|
|
|
}
|
|
|
|
return 0; /* if we're here, there's an error */
|
2002-05-01 04:52:08 +08:00
|
|
|
}
|
|
|
|
|
2007-09-23 12:49:51 +08:00
|
|
|
/* Check to see if we need an address-size prefix */
|
|
|
|
add_asp(instruction, bits);
|
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
size_prob = FALSE;
|
2007-04-12 10:40:54 +08:00
|
|
|
|
|
|
|
for (temp = nasm_instructions[instruction->opcode]; temp->opcode != -1; temp++){
|
|
|
|
int m = matches(temp, instruction, bits);
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
if (m == 99)
|
|
|
|
m += jmp_match(segment, offset, bits, instruction, temp->code);
|
|
|
|
|
|
|
|
if (m == 100) { /* matches! */
|
2007-04-14 00:47:53 +08:00
|
|
|
const char *codes = temp->code;
|
2007-04-12 10:40:54 +08:00
|
|
|
int32_t insn_size = calcsize(segment, offset, bits,
|
2005-01-16 06:15:51 +08:00
|
|
|
instruction, codes);
|
|
|
|
itimes = instruction->times;
|
|
|
|
if (insn_size < 0) /* shouldn't be, on pass two */
|
|
|
|
error(ERR_PANIC, "errors made it through from pass one");
|
|
|
|
else
|
|
|
|
while (itimes--) {
|
|
|
|
for (j = 0; j < instruction->nprefix; j++) {
|
2007-04-12 10:40:54 +08:00
|
|
|
uint8_t c = 0;
|
2005-01-16 06:15:51 +08:00
|
|
|
switch (instruction->prefixes[j]) {
|
|
|
|
case P_LOCK:
|
|
|
|
c = 0xF0;
|
|
|
|
break;
|
|
|
|
case P_REPNE:
|
|
|
|
case P_REPNZ:
|
|
|
|
c = 0xF2;
|
|
|
|
break;
|
|
|
|
case P_REPE:
|
|
|
|
case P_REPZ:
|
|
|
|
case P_REP:
|
|
|
|
c = 0xF3;
|
|
|
|
break;
|
|
|
|
case R_CS:
|
2007-04-16 23:46:46 +08:00
|
|
|
if (bits == 64) {
|
|
|
|
error(ERR_WARNING,
|
|
|
|
"cs segment base ignored in 64-bit mode");
|
|
|
|
}
|
2005-01-16 06:15:51 +08:00
|
|
|
c = 0x2E;
|
|
|
|
break;
|
|
|
|
case R_DS:
|
2007-04-16 23:46:46 +08:00
|
|
|
if (bits == 64) {
|
|
|
|
error(ERR_WARNING,
|
|
|
|
"ds segment base ignored in 64-bit mode");
|
|
|
|
}
|
2005-01-16 06:15:51 +08:00
|
|
|
c = 0x3E;
|
|
|
|
break;
|
|
|
|
case R_ES:
|
2007-04-16 23:46:46 +08:00
|
|
|
if (bits == 64) {
|
|
|
|
error(ERR_WARNING,
|
|
|
|
"es segment base ignored in 64-bit mode");
|
|
|
|
}
|
2005-01-16 06:15:51 +08:00
|
|
|
c = 0x26;
|
|
|
|
break;
|
|
|
|
case R_FS:
|
|
|
|
c = 0x64;
|
|
|
|
break;
|
|
|
|
case R_GS:
|
|
|
|
c = 0x65;
|
|
|
|
break;
|
|
|
|
case R_SS:
|
2007-04-16 23:46:46 +08:00
|
|
|
if (bits == 64) {
|
|
|
|
error(ERR_WARNING,
|
|
|
|
"ss segment base ignored in 64-bit mode");
|
|
|
|
}
|
2005-01-16 06:15:51 +08:00
|
|
|
c = 0x36;
|
|
|
|
break;
|
|
|
|
case R_SEGR6:
|
|
|
|
case R_SEGR7:
|
|
|
|
error(ERR_NONFATAL,
|
|
|
|
"segr6 and segr7 cannot be used as prefixes");
|
|
|
|
break;
|
|
|
|
case P_A16:
|
2007-04-12 10:40:54 +08:00
|
|
|
if (bits == 64) {
|
2007-04-18 04:23:11 +08:00
|
|
|
error(ERR_NONFATAL,
|
|
|
|
"16-bit addressing is not supported "
|
|
|
|
"in 64-bit mode");
|
2007-04-12 10:40:54 +08:00
|
|
|
break;
|
|
|
|
}
|
2005-01-16 06:15:51 +08:00
|
|
|
if (bits != 16)
|
|
|
|
c = 0x67;
|
|
|
|
break;
|
|
|
|
case P_A32:
|
|
|
|
if (bits != 32)
|
|
|
|
c = 0x67;
|
|
|
|
break;
|
|
|
|
case P_O16:
|
|
|
|
if (bits != 16)
|
|
|
|
c = 0x66;
|
|
|
|
break;
|
|
|
|
case P_O32:
|
2007-04-12 10:40:54 +08:00
|
|
|
if (bits == 16)
|
2005-01-16 06:15:51 +08:00
|
|
|
c = 0x66;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error(ERR_PANIC, "invalid instruction prefix");
|
|
|
|
}
|
|
|
|
if (c != 0) {
|
|
|
|
out(offset, segment, &c, OUT_RAWDATA + 1,
|
|
|
|
NO_SEG, NO_SEG);
|
|
|
|
offset++;
|
|
|
|
}
|
2007-04-12 10:40:54 +08:00
|
|
|
}
|
2005-01-16 06:15:51 +08:00
|
|
|
insn_end = offset + insn_size;
|
|
|
|
gencode(segment, offset, bits, instruction, codes,
|
|
|
|
insn_end);
|
|
|
|
offset += insn_size;
|
|
|
|
if (itimes > 0 && itimes == instruction->times - 1) {
|
|
|
|
/*
|
|
|
|
* Dummy call to list->output to give the offset to the
|
|
|
|
* listing module.
|
|
|
|
*/
|
|
|
|
list->output(offset, NULL, OUT_RAWDATA);
|
|
|
|
list->uplevel(LIST_TIMES);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (instruction->times > 1)
|
|
|
|
list->downlevel(LIST_TIMES);
|
|
|
|
return offset - start;
|
|
|
|
} else if (m > 0 && m > size_prob) {
|
|
|
|
size_prob = m;
|
|
|
|
}
|
2007-04-12 10:40:54 +08:00
|
|
|
// temp++;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
2002-05-01 04:53:55 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
if (temp->opcode == -1) { /* didn't match any instruction */
|
2007-05-30 12:27:58 +08:00
|
|
|
switch (size_prob) {
|
|
|
|
case 1:
|
|
|
|
error(ERR_NONFATAL, "operation size not specified");
|
|
|
|
break;
|
|
|
|
case 2:
|
2005-01-16 06:15:51 +08:00
|
|
|
error(ERR_NONFATAL, "mismatch in operand sizes");
|
2007-05-30 12:27:58 +08:00
|
|
|
break;
|
|
|
|
case 3:
|
2005-01-16 06:15:51 +08:00
|
|
|
error(ERR_NONFATAL, "no instruction for this cpu level");
|
2007-05-30 12:27:58 +08:00
|
|
|
break;
|
|
|
|
case 4:
|
2007-04-18 04:23:11 +08:00
|
|
|
error(ERR_NONFATAL, "instruction not supported in 64-bit mode");
|
2007-05-30 12:27:58 +08:00
|
|
|
break;
|
|
|
|
default:
|
2005-01-16 06:15:51 +08:00
|
|
|
error(ERR_NONFATAL,
|
|
|
|
"invalid combination of opcode and operands");
|
2007-05-30 12:27:58 +08:00
|
|
|
break;
|
|
|
|
}
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-04-12 10:40:54 +08:00
|
|
|
int32_t insn_size(int32_t segment, int32_t offset, int bits, uint32_t cp,
|
2005-01-16 06:15:51 +08:00
|
|
|
insn * instruction, efunc error)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2007-09-11 12:16:57 +08:00
|
|
|
const struct itemplate *temp;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
errfunc = error; /* to pass to other functions */
|
2002-05-01 04:59:21 +08:00
|
|
|
cpu = cp;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
if (instruction->opcode == -1)
|
2005-01-16 06:15:51 +08:00
|
|
|
return 0;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2007-09-19 08:49:09 +08:00
|
|
|
if (instruction->opcode == I_DB || instruction->opcode == I_DW ||
|
|
|
|
instruction->opcode == I_DD || instruction->opcode == I_DQ ||
|
|
|
|
instruction->opcode == I_DT || instruction->opcode == I_DO) {
|
2005-01-16 06:15:51 +08:00
|
|
|
extop *e;
|
2007-04-12 10:40:54 +08:00
|
|
|
int32_t isize, osize, wsize = 0; /* placate gcc */
|
2005-01-16 06:15:51 +08:00
|
|
|
|
|
|
|
isize = 0;
|
|
|
|
switch (instruction->opcode) {
|
|
|
|
case I_DB:
|
|
|
|
wsize = 1;
|
|
|
|
break;
|
|
|
|
case I_DW:
|
|
|
|
wsize = 2;
|
|
|
|
break;
|
|
|
|
case I_DD:
|
|
|
|
wsize = 4;
|
|
|
|
break;
|
|
|
|
case I_DQ:
|
|
|
|
wsize = 8;
|
|
|
|
break;
|
|
|
|
case I_DT:
|
|
|
|
wsize = 10;
|
|
|
|
break;
|
2007-09-19 08:49:09 +08:00
|
|
|
case I_DO:
|
|
|
|
wsize = 16;
|
|
|
|
break;
|
2007-09-13 11:27:41 +08:00
|
|
|
default:
|
|
|
|
break;
|
2005-01-16 06:15:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (e = instruction->eops; e; e = e->next) {
|
2007-04-12 10:40:54 +08:00
|
|
|
int32_t align;
|
2005-01-16 06:15:51 +08:00
|
|
|
|
|
|
|
osize = 0;
|
|
|
|
if (e->type == EOT_DB_NUMBER)
|
|
|
|
osize = 1;
|
|
|
|
else if (e->type == EOT_DB_STRING)
|
|
|
|
osize = e->stringlen;
|
|
|
|
|
|
|
|
align = (-osize) % wsize;
|
|
|
|
if (align < 0)
|
|
|
|
align += wsize;
|
|
|
|
isize += osize + align;
|
|
|
|
}
|
|
|
|
return isize * instruction->times;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
if (instruction->opcode == I_INCBIN) {
|
2007-04-14 00:47:53 +08:00
|
|
|
char fname[FILENAME_MAX];
|
2005-01-16 06:15:51 +08:00
|
|
|
FILE *fp;
|
2007-04-12 10:40:54 +08:00
|
|
|
int32_t len;
|
2007-04-14 00:47:53 +08:00
|
|
|
char *prefix = "", *combine;
|
|
|
|
char **pPrevPath = NULL;
|
2005-01-16 06:15:51 +08:00
|
|
|
|
|
|
|
len = FILENAME_MAX - 1;
|
|
|
|
if (len > instruction->eops->stringlen)
|
|
|
|
len = instruction->eops->stringlen;
|
|
|
|
strncpy(fname, instruction->eops->stringval, len);
|
|
|
|
fname[len] = '\0';
|
|
|
|
|
|
|
|
while (1) { /* added by alexfru: 'incbin' uses include paths */
|
|
|
|
combine = nasm_malloc(strlen(prefix) + len + 1);
|
|
|
|
strcpy(combine, prefix);
|
|
|
|
strcat(combine, fname);
|
|
|
|
|
|
|
|
if ((fp = fopen(combine, "rb")) != NULL) {
|
|
|
|
nasm_free(combine);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
nasm_free(combine);
|
|
|
|
pPrevPath = pp_get_include_path_ptr(pPrevPath);
|
|
|
|
if (pPrevPath == NULL)
|
|
|
|
break;
|
|
|
|
prefix = *pPrevPath;
|
2003-08-27 19:33:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (fp == NULL)
|
2005-01-16 06:15:51 +08:00
|
|
|
error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
|
|
|
|
fname);
|
|
|
|
else if (fseek(fp, 0L, SEEK_END) < 0)
|
|
|
|
error(ERR_NONFATAL, "`incbin': unable to seek on file `%s'",
|
|
|
|
fname);
|
|
|
|
else {
|
|
|
|
len = ftell(fp);
|
|
|
|
fclose(fp);
|
|
|
|
if (instruction->eops->next) {
|
|
|
|
len -= instruction->eops->next->offset;
|
|
|
|
if (instruction->eops->next->next &&
|
|
|
|
len > instruction->eops->next->next->offset) {
|
|
|
|
len = instruction->eops->next->next->offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return instruction->times * len;
|
|
|
|
}
|
|
|
|
return 0; /* if we're here, there's an error */
|
2002-05-01 04:52:08 +08:00
|
|
|
}
|
|
|
|
|
2007-09-23 12:49:51 +08:00
|
|
|
/* Check to see if we need an address-size prefix */
|
|
|
|
add_asp(instruction, bits);
|
|
|
|
|
2007-04-12 10:40:54 +08:00
|
|
|
for (temp = nasm_instructions[instruction->opcode]; temp->opcode != -1; temp++) {
|
|
|
|
int m = matches(temp, instruction, bits);
|
2005-01-16 06:15:51 +08:00
|
|
|
if (m == 99)
|
|
|
|
m += jmp_match(segment, offset, bits, instruction, temp->code);
|
|
|
|
|
|
|
|
if (m == 100) {
|
|
|
|
/* we've matched an instruction. */
|
2007-04-12 10:40:54 +08:00
|
|
|
int32_t isize;
|
2007-04-14 00:47:53 +08:00
|
|
|
const char *codes = temp->code;
|
2005-01-16 06:15:51 +08:00
|
|
|
int j;
|
|
|
|
|
|
|
|
isize = calcsize(segment, offset, bits, instruction, codes);
|
|
|
|
if (isize < 0)
|
|
|
|
return -1;
|
|
|
|
for (j = 0; j < instruction->nprefix; j++) {
|
2007-09-23 12:49:51 +08:00
|
|
|
switch (instruction->prefixes[j]) {
|
|
|
|
case P_A16:
|
|
|
|
if (bits != 16)
|
|
|
|
isize++;
|
|
|
|
break;
|
|
|
|
case P_A32:
|
|
|
|
if (bits != 32)
|
|
|
|
isize++;
|
|
|
|
break;
|
|
|
|
case P_O16:
|
|
|
|
if (bits != 16)
|
|
|
|
isize++;
|
|
|
|
break;
|
|
|
|
case P_O32:
|
|
|
|
if (bits == 16)
|
|
|
|
isize++;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
isize++;
|
|
|
|
break;
|
|
|
|
}
|
2005-01-16 06:15:51 +08:00
|
|
|
}
|
|
|
|
return isize * instruction->times;
|
|
|
|
}
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
2005-01-16 06:15:51 +08:00
|
|
|
return -1; /* didn't match any instruction */
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2002-05-01 04:59:21 +08:00
|
|
|
/* check that opn[op] is a signed byte of size 16 or 32,
|
|
|
|
and return the signed value*/
|
2005-01-16 06:15:51 +08:00
|
|
|
static int is_sbyte(insn * ins, int op, int size)
|
2002-05-01 04:59:21 +08:00
|
|
|
{
|
2007-04-12 10:40:54 +08:00
|
|
|
int32_t v;
|
2002-05-01 04:59:21 +08:00
|
|
|
int ret;
|
2005-01-16 06:15:51 +08:00
|
|
|
|
|
|
|
ret = !(ins->forw_ref && ins->oprs[op].opflags) && /* dead in the water on forward reference or External */
|
|
|
|
optimizing >= 0 &&
|
|
|
|
!(ins->oprs[op].type & STRICT) &&
|
|
|
|
ins->oprs[op].wrt == NO_SEG && ins->oprs[op].segment == NO_SEG;
|
2002-05-01 05:01:08 +08:00
|
|
|
|
2002-05-01 04:59:21 +08:00
|
|
|
v = ins->oprs[op].offset;
|
2005-01-16 06:15:51 +08:00
|
|
|
if (size == 16)
|
2007-04-12 10:40:54 +08:00
|
|
|
v = (int16_t)v; /* sign extend if 16 bits */
|
2005-01-16 06:15:51 +08:00
|
|
|
|
|
|
|
return ret && v >= -128L && v <= 127L;
|
2002-05-01 04:59:21 +08:00
|
|
|
}
|
|
|
|
|
2007-04-12 10:40:54 +08:00
|
|
|
static int32_t calcsize(int32_t segment, int32_t offset, int bits,
|
2007-04-14 00:47:53 +08:00
|
|
|
insn * ins, const char *codes)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2007-04-12 10:40:54 +08:00
|
|
|
int32_t length = 0;
|
|
|
|
uint8_t c;
|
2007-05-30 11:25:21 +08:00
|
|
|
int rex_mask = ~0;
|
2007-04-18 10:27:18 +08:00
|
|
|
ins->rex = 0; /* Ensure REX is reset */
|
2005-01-16 06:15:51 +08:00
|
|
|
|
|
|
|
(void)segment; /* Don't warn that this parameter is unused */
|
|
|
|
(void)offset; /* Don't warn that this parameter is unused */
|
|
|
|
|
|
|
|
while (*codes)
|
|
|
|
switch (c = *codes++) {
|
|
|
|
case 01:
|
|
|
|
case 02:
|
|
|
|
case 03:
|
|
|
|
codes += c, length += c;
|
|
|
|
break;
|
|
|
|
case 04:
|
|
|
|
case 05:
|
|
|
|
case 06:
|
|
|
|
case 07:
|
|
|
|
length++;
|
|
|
|
break;
|
|
|
|
case 010:
|
|
|
|
case 011:
|
|
|
|
case 012:
|
2007-09-18 06:49:30 +08:00
|
|
|
case 013:
|
2007-05-30 11:25:21 +08:00
|
|
|
ins->rex |=
|
|
|
|
op_rexflags(&ins->oprs[c - 010], REX_B|REX_H|REX_P|REX_W);
|
2005-01-16 06:15:51 +08:00
|
|
|
codes++, length++;
|
|
|
|
break;
|
|
|
|
case 014:
|
|
|
|
case 015:
|
|
|
|
case 016:
|
2007-09-18 06:49:30 +08:00
|
|
|
case 017:
|
2005-01-16 06:15:51 +08:00
|
|
|
length++;
|
|
|
|
break;
|
|
|
|
case 020:
|
|
|
|
case 021:
|
|
|
|
case 022:
|
2007-09-18 06:49:30 +08:00
|
|
|
case 023:
|
2005-01-16 06:15:51 +08:00
|
|
|
length++;
|
|
|
|
break;
|
|
|
|
case 024:
|
|
|
|
case 025:
|
|
|
|
case 026:
|
2007-09-18 06:49:30 +08:00
|
|
|
case 027:
|
2005-01-16 06:15:51 +08:00
|
|
|
length++;
|
|
|
|
break;
|
|
|
|
case 030:
|
|
|
|
case 031:
|
|
|
|
case 032:
|
2007-09-18 06:49:30 +08:00
|
|
|
case 033:
|
2005-01-16 06:15:51 +08:00
|
|
|
length += 2;
|
|
|
|
break;
|
|
|
|
case 034:
|
|
|
|
case 035:
|
|
|
|
case 036:
|
2007-09-18 06:49:30 +08:00
|
|
|
case 037:
|
2007-04-12 10:40:54 +08:00
|
|
|
if (ins->oprs[c - 034].type & (BITS16 | BITS32 | BITS64))
|
2005-01-16 06:15:51 +08:00
|
|
|
length += (ins->oprs[c - 034].type & BITS16) ? 2 : 4;
|
|
|
|
else
|
|
|
|
length += (bits == 16) ? 2 : 4;
|
|
|
|
break;
|
|
|
|
case 040:
|
|
|
|
case 041:
|
|
|
|
case 042:
|
2007-09-18 06:49:30 +08:00
|
|
|
case 043:
|
2005-01-16 06:15:51 +08:00
|
|
|
length += 4;
|
|
|
|
break;
|
|
|
|
case 044:
|
|
|
|
case 045:
|
|
|
|
case 046:
|
2007-09-18 06:49:30 +08:00
|
|
|
case 047:
|
2005-01-16 06:15:51 +08:00
|
|
|
length += ((ins->oprs[c - 044].addr_size ?
|
2007-04-12 10:40:54 +08:00
|
|
|
ins->oprs[c - 044].addr_size : bits) >> 3);
|
2005-01-16 06:15:51 +08:00
|
|
|
break;
|
|
|
|
case 050:
|
|
|
|
case 051:
|
|
|
|
case 052:
|
2007-09-18 06:49:30 +08:00
|
|
|
case 053:
|
2005-01-16 06:15:51 +08:00
|
|
|
length++;
|
|
|
|
break;
|
2007-04-12 10:40:54 +08:00
|
|
|
case 054:
|
|
|
|
case 055:
|
|
|
|
case 056:
|
2007-09-18 06:49:30 +08:00
|
|
|
case 057:
|
2007-04-12 10:40:54 +08:00
|
|
|
length += 8; /* MOV reg64/imm */
|
|
|
|
break;
|
2005-01-16 06:15:51 +08:00
|
|
|
case 060:
|
|
|
|
case 061:
|
|
|
|
case 062:
|
2007-09-18 06:49:30 +08:00
|
|
|
case 063:
|
2005-01-16 06:15:51 +08:00
|
|
|
length += 2;
|
|
|
|
break;
|
|
|
|
case 064:
|
|
|
|
case 065:
|
|
|
|
case 066:
|
2007-09-18 06:49:30 +08:00
|
|
|
case 067:
|
2007-04-12 10:40:54 +08:00
|
|
|
if (ins->oprs[c - 064].type & (BITS16 | BITS32 | BITS64))
|
2005-01-16 06:15:51 +08:00
|
|
|
length += (ins->oprs[c - 064].type & BITS16) ? 2 : 4;
|
|
|
|
else
|
|
|
|
length += (bits == 16) ? 2 : 4;
|
|
|
|
break;
|
|
|
|
case 070:
|
|
|
|
case 071:
|
|
|
|
case 072:
|
2007-09-18 06:49:30 +08:00
|
|
|
case 073:
|
2005-01-16 06:15:51 +08:00
|
|
|
length += 4;
|
|
|
|
break;
|
2007-09-18 06:49:30 +08:00
|
|
|
case 074:
|
|
|
|
case 075:
|
|
|
|
case 076:
|
|
|
|
case 077:
|
|
|
|
length += 2;
|
2005-01-16 06:15:51 +08:00
|
|
|
break;
|
|
|
|
case 0140:
|
|
|
|
case 0141:
|
|
|
|
case 0142:
|
2007-09-18 06:49:30 +08:00
|
|
|
case 0143:
|
|
|
|
length += is_sbyte(ins, c - 0140, 16) ? 1 : 2;
|
2005-01-16 06:15:51 +08:00
|
|
|
break;
|
|
|
|
case 0144:
|
|
|
|
case 0145:
|
2007-09-18 06:49:30 +08:00
|
|
|
case 0146:
|
|
|
|
case 0147:
|
|
|
|
codes += 2;
|
|
|
|
length++;
|
|
|
|
break;
|
|
|
|
case 0150:
|
|
|
|
case 0151:
|
|
|
|
case 0152:
|
|
|
|
case 0153:
|
|
|
|
length += is_sbyte(ins, c - 0150, 32) ? 1 : 4;
|
|
|
|
break;
|
|
|
|
case 0154:
|
|
|
|
case 0155:
|
|
|
|
case 0156:
|
|
|
|
case 0157:
|
2005-01-16 06:15:51 +08:00
|
|
|
codes += 2;
|
|
|
|
length++;
|
|
|
|
break;
|
2007-09-18 07:55:04 +08:00
|
|
|
case 0160:
|
|
|
|
case 0161:
|
|
|
|
case 0162:
|
|
|
|
case 0163:
|
|
|
|
length++;
|
|
|
|
ins->rex |= REX_D;
|
2007-09-18 08:25:27 +08:00
|
|
|
ins->drexdst = regval(&ins->oprs[c & 3]);
|
2007-09-18 07:55:04 +08:00
|
|
|
break;
|
|
|
|
case 0164:
|
|
|
|
case 0165:
|
|
|
|
case 0166:
|
|
|
|
case 0167:
|
|
|
|
length++;
|
|
|
|
ins->rex |= REX_D|REX_OC;
|
2007-09-18 08:25:27 +08:00
|
|
|
ins->drexdst = regval(&ins->oprs[c & 3]);
|
2007-09-18 07:55:04 +08:00
|
|
|
break;
|
2007-09-18 06:49:30 +08:00
|
|
|
case 0170:
|
|
|
|
length++;
|
|
|
|
break;
|
2007-09-18 07:55:04 +08:00
|
|
|
case 0171:
|
|
|
|
break;
|
2005-01-16 06:15:51 +08:00
|
|
|
case 0300:
|
|
|
|
case 0301:
|
2007-04-12 10:40:54 +08:00
|
|
|
case 0302:
|
2007-09-18 06:49:30 +08:00
|
|
|
case 0303:
|
2005-01-16 06:15:51 +08:00
|
|
|
break;
|
|
|
|
case 0310:
|
2007-09-23 12:49:51 +08:00
|
|
|
if (bits == 64)
|
|
|
|
return -1;
|
|
|
|
length += (bits != 16) && !has_prefix(ins,P_A16);
|
2005-01-16 06:15:51 +08:00
|
|
|
break;
|
|
|
|
case 0311:
|
2007-09-23 12:49:51 +08:00
|
|
|
length += (bits != 32) && !has_prefix(ins,P_A32);
|
2005-01-16 06:15:51 +08:00
|
|
|
break;
|
|
|
|
case 0312:
|
2007-04-12 10:40:54 +08:00
|
|
|
break;
|
|
|
|
case 0313:
|
2007-09-23 12:49:51 +08:00
|
|
|
if (bits != 64 || has_prefix(ins,P_A16) || has_prefix(ins,P_A32))
|
|
|
|
return -1;
|
2005-01-16 06:15:51 +08:00
|
|
|
break;
|
|
|
|
case 0320:
|
2007-04-12 10:40:54 +08:00
|
|
|
length += (bits != 16);
|
2005-01-16 06:15:51 +08:00
|
|
|
break;
|
|
|
|
case 0321:
|
|
|
|
length += (bits == 16);
|
|
|
|
break;
|
|
|
|
case 0322:
|
|
|
|
break;
|
2007-04-12 10:40:54 +08:00
|
|
|
case 0323:
|
2007-05-30 11:25:21 +08:00
|
|
|
rex_mask &= ~REX_W;
|
2007-04-12 10:40:54 +08:00
|
|
|
break;
|
|
|
|
case 0324:
|
2007-05-30 11:25:21 +08:00
|
|
|
ins->rex |= REX_W;
|
2007-04-18 10:27:18 +08:00
|
|
|
break;
|
2005-01-16 06:15:51 +08:00
|
|
|
case 0330:
|
|
|
|
codes++, length++;
|
|
|
|
break;
|
|
|
|
case 0331:
|
|
|
|
break;
|
2007-09-13 12:58:51 +08:00
|
|
|
case 0332:
|
2005-01-16 06:15:51 +08:00
|
|
|
case 0333:
|
|
|
|
length++;
|
|
|
|
break;
|
2007-04-18 04:23:11 +08:00
|
|
|
case 0334:
|
|
|
|
assert_no_prefix(ins, P_LOCK);
|
2007-05-30 11:25:21 +08:00
|
|
|
ins->rex |= REX_L;
|
2007-04-18 04:23:11 +08:00
|
|
|
break;
|
2007-09-13 12:58:51 +08:00
|
|
|
case 0335:
|
|
|
|
break;
|
2005-01-16 06:15:51 +08:00
|
|
|
case 0340:
|
|
|
|
case 0341:
|
|
|
|
case 0342:
|
|
|
|
if (ins->oprs[0].segment != NO_SEG)
|
|
|
|
errfunc(ERR_NONFATAL, "attempt to reserve non-constant"
|
|
|
|
" quantity of BSS space");
|
|
|
|
else
|
|
|
|
length += ins->oprs[0].offset << (c - 0340);
|
|
|
|
break;
|
2007-09-12 06:44:03 +08:00
|
|
|
case 0364:
|
|
|
|
case 0365:
|
|
|
|
break;
|
2007-08-17 15:37:52 +08:00
|
|
|
case 0366:
|
2007-09-12 06:44:03 +08:00
|
|
|
case 0367:
|
|
|
|
length++;
|
|
|
|
break;
|
2005-01-16 06:15:51 +08:00
|
|
|
case 0370:
|
|
|
|
case 0371:
|
|
|
|
case 0372:
|
|
|
|
break;
|
|
|
|
case 0373:
|
|
|
|
length++;
|
|
|
|
break;
|
|
|
|
default: /* can't do it by 'case' statements */
|
|
|
|
if (c >= 0100 && c <= 0277) { /* it's an EA */
|
|
|
|
ea ea_data;
|
2007-04-12 10:40:54 +08:00
|
|
|
int rfield;
|
2007-05-30 11:25:21 +08:00
|
|
|
int32_t rflags;
|
2007-04-12 10:40:54 +08:00
|
|
|
ea_data.rex = 0; /* Ensure ea.REX is initially 0 */
|
|
|
|
|
2007-05-30 11:25:21 +08:00
|
|
|
if (c <= 0177) {
|
|
|
|
/* pick rfield from operand b */
|
|
|
|
rflags = regflag(&ins->oprs[c & 7]);
|
|
|
|
rfield = regvals[ins->oprs[c & 7].basereg];
|
|
|
|
} else {
|
|
|
|
rflags = 0;
|
2007-04-18 04:23:11 +08:00
|
|
|
rfield = c & 7;
|
2007-05-30 11:25:21 +08:00
|
|
|
}
|
2007-04-12 10:40:54 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
if (!process_ea
|
2007-04-12 10:40:54 +08:00
|
|
|
(&ins->oprs[(c >> 3) & 7], &ea_data, bits,
|
2007-05-30 11:25:21 +08:00
|
|
|
rfield, rflags, ins->forw_ref)) {
|
2005-01-16 06:15:51 +08:00
|
|
|
errfunc(ERR_NONFATAL, "invalid effective address");
|
|
|
|
return -1;
|
2007-04-12 10:40:54 +08:00
|
|
|
} else {
|
2007-04-18 04:23:11 +08:00
|
|
|
ins->rex |= ea_data.rex;
|
2005-01-16 06:15:51 +08:00
|
|
|
length += ea_data.size;
|
2007-04-12 10:40:54 +08:00
|
|
|
}
|
2005-01-16 06:15:51 +08:00
|
|
|
} else
|
|
|
|
errfunc(ERR_PANIC, "internal instruction table corrupt"
|
|
|
|
": instruction code 0x%02X given", c);
|
|
|
|
}
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2007-04-18 04:23:11 +08:00
|
|
|
ins->rex &= rex_mask;
|
2007-09-18 07:55:04 +08:00
|
|
|
|
|
|
|
if (ins->rex & REX_D) {
|
|
|
|
if (ins->rex & REX_H) {
|
|
|
|
errfunc(ERR_NONFATAL, "cannot use high register in drex instruction");
|
|
|
|
return -1;
|
|
|
|
}
|
2007-09-18 08:25:27 +08:00
|
|
|
if (bits != 64 && ((ins->rex & (REX_W|REX_X|REX_B)) ||
|
|
|
|
ins->drexdst > 7)) {
|
|
|
|
errfunc(ERR_NONFATAL, "invalid operands in non-64-bit mode");
|
|
|
|
return -1;
|
|
|
|
}
|
2007-09-18 07:55:04 +08:00
|
|
|
length++;
|
|
|
|
} else if (ins->rex & REX_REAL) {
|
2007-05-30 11:25:21 +08:00
|
|
|
if (ins->rex & REX_H) {
|
|
|
|
errfunc(ERR_NONFATAL, "cannot use high register in rex instruction");
|
|
|
|
return -1;
|
|
|
|
} else if (bits == 64 ||
|
|
|
|
((ins->rex & REX_L) &&
|
|
|
|
!(ins->rex & (REX_P|REX_W|REX_X|REX_B)) &&
|
|
|
|
cpu >= IF_X86_64)) {
|
2007-04-18 04:23:11 +08:00
|
|
|
length++;
|
2007-04-18 10:27:18 +08:00
|
|
|
} else {
|
2007-09-18 08:25:27 +08:00
|
|
|
errfunc(ERR_NONFATAL, "invalid operands in non-64-bit mode");
|
|
|
|
return -1;
|
2007-04-18 10:27:18 +08:00
|
|
|
}
|
2007-04-12 10:40:54 +08:00
|
|
|
}
|
|
|
|
|
2007-04-18 04:23:11 +08:00
|
|
|
return length;
|
|
|
|
}
|
2007-04-12 10:40:54 +08:00
|
|
|
|
2007-05-30 11:25:21 +08:00
|
|
|
#define EMIT_REX() \
|
2007-09-18 07:55:04 +08:00
|
|
|
if (!(ins->rex & REX_D) && (ins->rex & REX_REAL) && (bits == 64)) { \
|
2007-05-30 11:25:21 +08:00
|
|
|
ins->rex = (ins->rex & REX_REAL)|REX_P; \
|
|
|
|
out(offset, segment, &ins->rex, OUT_RAWDATA+1, NO_SEG, NO_SEG); \
|
|
|
|
ins->rex = 0; \
|
|
|
|
offset += 1; \
|
|
|
|
}
|
|
|
|
|
2007-04-12 10:40:54 +08:00
|
|
|
static void gencode(int32_t segment, int32_t offset, int bits,
|
2007-04-14 00:47:53 +08:00
|
|
|
insn * ins, const char *codes, int32_t insn_end)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2007-04-14 00:47:53 +08:00
|
|
|
static char condval[] = { /* conditional opcodes */
|
2005-01-16 06:15:51 +08:00
|
|
|
0x7, 0x3, 0x2, 0x6, 0x2, 0x4, 0xF, 0xD, 0xC, 0xE, 0x6, 0x2,
|
|
|
|
0x3, 0x7, 0x3, 0x5, 0xE, 0xC, 0xD, 0xF, 0x1, 0xB, 0x9, 0x5,
|
|
|
|
0x0, 0xA, 0xA, 0xB, 0x8, 0x4
|
2002-05-01 04:51:32 +08:00
|
|
|
};
|
2007-04-12 10:40:54 +08:00
|
|
|
uint8_t c;
|
|
|
|
uint8_t bytes[4];
|
|
|
|
int32_t size;
|
|
|
|
int64_t data;
|
|
|
|
|
2002-05-01 04:53:55 +08:00
|
|
|
while (*codes)
|
2005-01-16 06:15:51 +08:00
|
|
|
switch (c = *codes++) {
|
|
|
|
case 01:
|
|
|
|
case 02:
|
|
|
|
case 03:
|
2007-05-30 11:25:21 +08:00
|
|
|
EMIT_REX();
|
2005-01-16 06:15:51 +08:00
|
|
|
out(offset, segment, codes, OUT_RAWDATA + c, NO_SEG, NO_SEG);
|
|
|
|
codes += c;
|
|
|
|
offset += c;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 04:
|
|
|
|
case 06:
|
|
|
|
switch (ins->oprs[0].basereg) {
|
|
|
|
case R_CS:
|
|
|
|
bytes[0] = 0x0E + (c == 0x04 ? 1 : 0);
|
|
|
|
break;
|
|
|
|
case R_DS:
|
|
|
|
bytes[0] = 0x1E + (c == 0x04 ? 1 : 0);
|
|
|
|
break;
|
|
|
|
case R_ES:
|
|
|
|
bytes[0] = 0x06 + (c == 0x04 ? 1 : 0);
|
|
|
|
break;
|
|
|
|
case R_SS:
|
|
|
|
bytes[0] = 0x16 + (c == 0x04 ? 1 : 0);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
errfunc(ERR_PANIC,
|
|
|
|
"bizarre 8086 segment register received");
|
|
|
|
}
|
|
|
|
out(offset, segment, bytes, OUT_RAWDATA + 1, NO_SEG, NO_SEG);
|
|
|
|
offset++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 05:
|
|
|
|
case 07:
|
|
|
|
switch (ins->oprs[0].basereg) {
|
|
|
|
case R_FS:
|
|
|
|
bytes[0] = 0xA0 + (c == 0x05 ? 1 : 0);
|
|
|
|
break;
|
|
|
|
case R_GS:
|
|
|
|
bytes[0] = 0xA8 + (c == 0x05 ? 1 : 0);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
errfunc(ERR_PANIC,
|
|
|
|
"bizarre 386 segment register received");
|
|
|
|
}
|
|
|
|
out(offset, segment, bytes, OUT_RAWDATA + 1, NO_SEG, NO_SEG);
|
|
|
|
offset++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 010:
|
|
|
|
case 011:
|
|
|
|
case 012:
|
2007-09-18 06:49:30 +08:00
|
|
|
case 013:
|
2007-05-30 11:25:21 +08:00
|
|
|
EMIT_REX();
|
2007-04-12 10:40:54 +08:00
|
|
|
bytes[0] = *codes++ + ((regval(&ins->oprs[c - 010])) & 7);
|
2005-01-16 06:15:51 +08:00
|
|
|
out(offset, segment, bytes, OUT_RAWDATA + 1, NO_SEG, NO_SEG);
|
|
|
|
offset += 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 014:
|
|
|
|
case 015:
|
|
|
|
case 016:
|
2007-09-18 06:49:30 +08:00
|
|
|
case 017:
|
2005-01-16 06:15:51 +08:00
|
|
|
if (ins->oprs[c - 014].offset < -128
|
|
|
|
|| ins->oprs[c - 014].offset > 127) {
|
|
|
|
errfunc(ERR_WARNING, "signed byte value exceeds bounds");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ins->oprs[c - 014].segment != NO_SEG) {
|
|
|
|
data = ins->oprs[c - 014].offset;
|
|
|
|
out(offset, segment, &data, OUT_ADDRESS + 1,
|
|
|
|
ins->oprs[c - 014].segment, ins->oprs[c - 014].wrt);
|
|
|
|
} else {
|
|
|
|
bytes[0] = ins->oprs[c - 014].offset;
|
|
|
|
out(offset, segment, bytes, OUT_RAWDATA + 1, NO_SEG,
|
|
|
|
NO_SEG);
|
|
|
|
}
|
|
|
|
offset += 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 020:
|
|
|
|
case 021:
|
|
|
|
case 022:
|
2007-09-18 06:49:30 +08:00
|
|
|
case 023:
|
2005-01-16 06:15:51 +08:00
|
|
|
if (ins->oprs[c - 020].offset < -256
|
|
|
|
|| ins->oprs[c - 020].offset > 255) {
|
|
|
|
errfunc(ERR_WARNING, "byte value exceeds bounds");
|
|
|
|
}
|
|
|
|
if (ins->oprs[c - 020].segment != NO_SEG) {
|
|
|
|
data = ins->oprs[c - 020].offset;
|
|
|
|
out(offset, segment, &data, OUT_ADDRESS + 1,
|
|
|
|
ins->oprs[c - 020].segment, ins->oprs[c - 020].wrt);
|
|
|
|
} else {
|
|
|
|
bytes[0] = ins->oprs[c - 020].offset;
|
|
|
|
out(offset, segment, bytes, OUT_RAWDATA + 1, NO_SEG,
|
|
|
|
NO_SEG);
|
|
|
|
}
|
|
|
|
offset += 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 024:
|
|
|
|
case 025:
|
|
|
|
case 026:
|
2007-09-18 06:49:30 +08:00
|
|
|
case 027:
|
2005-01-16 06:15:51 +08:00
|
|
|
if (ins->oprs[c - 024].offset < 0
|
|
|
|
|| ins->oprs[c - 024].offset > 255)
|
|
|
|
errfunc(ERR_WARNING, "unsigned byte value exceeds bounds");
|
|
|
|
if (ins->oprs[c - 024].segment != NO_SEG) {
|
|
|
|
data = ins->oprs[c - 024].offset;
|
|
|
|
out(offset, segment, &data, OUT_ADDRESS + 1,
|
|
|
|
ins->oprs[c - 024].segment, ins->oprs[c - 024].wrt);
|
|
|
|
} else {
|
|
|
|
bytes[0] = ins->oprs[c - 024].offset;
|
|
|
|
out(offset, segment, bytes, OUT_RAWDATA + 1, NO_SEG,
|
|
|
|
NO_SEG);
|
|
|
|
}
|
|
|
|
offset += 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 030:
|
|
|
|
case 031:
|
|
|
|
case 032:
|
2007-09-18 06:49:30 +08:00
|
|
|
case 033:
|
2005-01-16 06:15:51 +08:00
|
|
|
if (ins->oprs[c - 030].segment == NO_SEG &&
|
|
|
|
ins->oprs[c - 030].wrt == NO_SEG &&
|
|
|
|
(ins->oprs[c - 030].offset < -65536L ||
|
|
|
|
ins->oprs[c - 030].offset > 65535L)) {
|
|
|
|
errfunc(ERR_WARNING, "word value exceeds bounds");
|
|
|
|
}
|
|
|
|
data = ins->oprs[c - 030].offset;
|
|
|
|
out(offset, segment, &data, OUT_ADDRESS + 2,
|
|
|
|
ins->oprs[c - 030].segment, ins->oprs[c - 030].wrt);
|
|
|
|
offset += 2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 034:
|
|
|
|
case 035:
|
|
|
|
case 036:
|
2007-09-18 06:49:30 +08:00
|
|
|
case 037:
|
2005-01-16 06:15:51 +08:00
|
|
|
if (ins->oprs[c - 034].type & (BITS16 | BITS32))
|
|
|
|
size = (ins->oprs[c - 034].type & BITS16) ? 2 : 4;
|
|
|
|
else
|
|
|
|
size = (bits == 16) ? 2 : 4;
|
|
|
|
data = ins->oprs[c - 034].offset;
|
|
|
|
if (size == 2 && (data < -65536L || data > 65535L))
|
|
|
|
errfunc(ERR_WARNING, "word value exceeds bounds");
|
|
|
|
out(offset, segment, &data, OUT_ADDRESS + size,
|
|
|
|
ins->oprs[c - 034].segment, ins->oprs[c - 034].wrt);
|
|
|
|
offset += size;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 040:
|
|
|
|
case 041:
|
|
|
|
case 042:
|
2007-09-18 06:49:30 +08:00
|
|
|
case 043:
|
2005-01-16 06:15:51 +08:00
|
|
|
data = ins->oprs[c - 040].offset;
|
|
|
|
out(offset, segment, &data, OUT_ADDRESS + 4,
|
|
|
|
ins->oprs[c - 040].segment, ins->oprs[c - 040].wrt);
|
|
|
|
offset += 4;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 044:
|
|
|
|
case 045:
|
|
|
|
case 046:
|
2007-09-18 06:49:30 +08:00
|
|
|
case 047:
|
2005-01-16 06:15:51 +08:00
|
|
|
data = ins->oprs[c - 044].offset;
|
|
|
|
size = ((ins->oprs[c - 044].addr_size ?
|
2007-04-12 10:40:54 +08:00
|
|
|
ins->oprs[c - 044].addr_size : bits) >> 3);
|
2005-01-16 06:15:51 +08:00
|
|
|
if (size == 2 && (data < -65536L || data > 65535L))
|
|
|
|
errfunc(ERR_WARNING, "word value exceeds bounds");
|
|
|
|
out(offset, segment, &data, OUT_ADDRESS + size,
|
|
|
|
ins->oprs[c - 044].segment, ins->oprs[c - 044].wrt);
|
|
|
|
offset += size;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 050:
|
|
|
|
case 051:
|
|
|
|
case 052:
|
2007-09-18 06:49:30 +08:00
|
|
|
case 053:
|
2005-01-16 06:15:51 +08:00
|
|
|
if (ins->oprs[c - 050].segment != segment)
|
|
|
|
errfunc(ERR_NONFATAL,
|
|
|
|
"short relative jump outside segment");
|
|
|
|
data = ins->oprs[c - 050].offset - insn_end;
|
|
|
|
if (data > 127 || data < -128)
|
|
|
|
errfunc(ERR_NONFATAL, "short jump is out of range");
|
|
|
|
bytes[0] = data;
|
|
|
|
out(offset, segment, bytes, OUT_RAWDATA + 1, NO_SEG, NO_SEG);
|
|
|
|
offset += 1;
|
|
|
|
break;
|
2007-04-12 10:40:54 +08:00
|
|
|
|
|
|
|
case 054:
|
|
|
|
case 055:
|
|
|
|
case 056:
|
2007-09-18 06:49:30 +08:00
|
|
|
case 057:
|
2007-04-12 10:40:54 +08:00
|
|
|
data = (int64_t)ins->oprs[c - 054].offset;
|
|
|
|
out(offset, segment, &data, OUT_ADDRESS + 8,
|
|
|
|
ins->oprs[c - 054].segment, ins->oprs[c - 054].wrt);
|
|
|
|
offset += 8;
|
|
|
|
break;
|
2005-01-16 06:15:51 +08:00
|
|
|
|
|
|
|
case 060:
|
|
|
|
case 061:
|
|
|
|
case 062:
|
2007-09-18 06:49:30 +08:00
|
|
|
case 063:
|
2005-01-16 06:15:51 +08:00
|
|
|
if (ins->oprs[c - 060].segment != segment) {
|
|
|
|
data = ins->oprs[c - 060].offset;
|
|
|
|
out(offset, segment, &data,
|
|
|
|
OUT_REL2ADR + insn_end - offset,
|
|
|
|
ins->oprs[c - 060].segment, ins->oprs[c - 060].wrt);
|
|
|
|
} else {
|
|
|
|
data = ins->oprs[c - 060].offset - insn_end;
|
|
|
|
out(offset, segment, &data,
|
|
|
|
OUT_ADDRESS + 2, NO_SEG, NO_SEG);
|
|
|
|
}
|
|
|
|
offset += 2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 064:
|
|
|
|
case 065:
|
|
|
|
case 066:
|
2007-09-18 06:49:30 +08:00
|
|
|
case 067:
|
2007-04-12 10:40:54 +08:00
|
|
|
if (ins->oprs[c - 064].type & (BITS16 | BITS32 | BITS64))
|
2005-01-16 06:15:51 +08:00
|
|
|
size = (ins->oprs[c - 064].type & BITS16) ? 2 : 4;
|
|
|
|
else
|
|
|
|
size = (bits == 16) ? 2 : 4;
|
|
|
|
if (ins->oprs[c - 064].segment != segment) {
|
2007-04-12 10:40:54 +08:00
|
|
|
int32_t reltype = (size == 2 ? OUT_REL2ADR : OUT_REL4ADR);
|
2005-01-16 06:15:51 +08:00
|
|
|
data = ins->oprs[c - 064].offset;
|
|
|
|
out(offset, segment, &data, reltype + insn_end - offset,
|
|
|
|
ins->oprs[c - 064].segment, ins->oprs[c - 064].wrt);
|
|
|
|
} else {
|
|
|
|
data = ins->oprs[c - 064].offset - insn_end;
|
|
|
|
out(offset, segment, &data,
|
|
|
|
OUT_ADDRESS + size, NO_SEG, NO_SEG);
|
|
|
|
}
|
|
|
|
offset += size;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 070:
|
|
|
|
case 071:
|
|
|
|
case 072:
|
2007-09-18 06:49:30 +08:00
|
|
|
case 073:
|
2005-01-16 06:15:51 +08:00
|
|
|
if (ins->oprs[c - 070].segment != segment) {
|
|
|
|
data = ins->oprs[c - 070].offset;
|
|
|
|
out(offset, segment, &data,
|
|
|
|
OUT_REL4ADR + insn_end - offset,
|
|
|
|
ins->oprs[c - 070].segment, ins->oprs[c - 070].wrt);
|
|
|
|
} else {
|
|
|
|
data = ins->oprs[c - 070].offset - insn_end;
|
|
|
|
out(offset, segment, &data,
|
|
|
|
OUT_ADDRESS + 4, NO_SEG, NO_SEG);
|
|
|
|
}
|
|
|
|
offset += 4;
|
|
|
|
break;
|
|
|
|
|
2007-09-18 06:49:30 +08:00
|
|
|
case 074:
|
|
|
|
case 075:
|
|
|
|
case 076:
|
|
|
|
case 077:
|
|
|
|
if (ins->oprs[c - 074].segment == NO_SEG)
|
|
|
|
errfunc(ERR_NONFATAL, "value referenced by FAR is not"
|
|
|
|
" relocatable");
|
|
|
|
data = 0L;
|
|
|
|
out(offset, segment, &data, OUT_ADDRESS + 2,
|
|
|
|
outfmt->segbase(1 + ins->oprs[c - 074].segment),
|
|
|
|
ins->oprs[c - 074].wrt);
|
|
|
|
offset += 2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0140:
|
|
|
|
case 0141:
|
|
|
|
case 0142:
|
|
|
|
case 0143:
|
|
|
|
data = ins->oprs[c - 0140].offset;
|
|
|
|
if (is_sbyte(ins, c - 0140, 16)) {
|
2005-01-16 06:15:51 +08:00
|
|
|
bytes[0] = data;
|
|
|
|
out(offset, segment, bytes, OUT_RAWDATA + 1, NO_SEG,
|
|
|
|
NO_SEG);
|
|
|
|
offset++;
|
|
|
|
} else {
|
2007-09-18 06:49:30 +08:00
|
|
|
if (ins->oprs[c - 0140].segment == NO_SEG &&
|
|
|
|
ins->oprs[c - 0140].wrt == NO_SEG &&
|
2005-01-16 06:15:51 +08:00
|
|
|
(data < -65536L || data > 65535L)) {
|
|
|
|
errfunc(ERR_WARNING, "word value exceeds bounds");
|
|
|
|
}
|
|
|
|
out(offset, segment, &data, OUT_ADDRESS + 2,
|
2007-09-18 07:31:33 +08:00
|
|
|
ins->oprs[c - 0140].segment, ins->oprs[c - 0140].wrt);
|
2005-01-16 06:15:51 +08:00
|
|
|
offset += 2;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2007-09-18 06:49:30 +08:00
|
|
|
case 0144:
|
|
|
|
case 0145:
|
|
|
|
case 0146:
|
|
|
|
case 0147:
|
2007-05-30 11:25:21 +08:00
|
|
|
EMIT_REX();
|
2005-01-16 06:15:51 +08:00
|
|
|
codes++;
|
|
|
|
bytes[0] = *codes++;
|
2007-09-18 06:49:30 +08:00
|
|
|
if (is_sbyte(ins, c - 0144, 16))
|
2005-01-16 06:15:51 +08:00
|
|
|
bytes[0] |= 2; /* s-bit */
|
|
|
|
out(offset, segment, bytes, OUT_RAWDATA + 1, NO_SEG, NO_SEG);
|
|
|
|
offset++;
|
|
|
|
break;
|
|
|
|
|
2007-09-18 06:49:30 +08:00
|
|
|
case 0150:
|
|
|
|
case 0151:
|
|
|
|
case 0152:
|
|
|
|
case 0153:
|
|
|
|
data = ins->oprs[c - 0150].offset;
|
|
|
|
if (is_sbyte(ins, c - 0150, 32)) {
|
2005-01-16 06:15:51 +08:00
|
|
|
bytes[0] = data;
|
|
|
|
out(offset, segment, bytes, OUT_RAWDATA + 1, NO_SEG,
|
|
|
|
NO_SEG);
|
|
|
|
offset++;
|
|
|
|
} else {
|
|
|
|
out(offset, segment, &data, OUT_ADDRESS + 4,
|
2007-09-18 07:31:33 +08:00
|
|
|
ins->oprs[c - 0150].segment, ins->oprs[c - 0150].wrt);
|
2005-01-16 06:15:51 +08:00
|
|
|
offset += 4;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2007-09-18 06:49:30 +08:00
|
|
|
case 0154:
|
|
|
|
case 0155:
|
|
|
|
case 0156:
|
|
|
|
case 0157:
|
2007-05-30 11:25:21 +08:00
|
|
|
EMIT_REX();
|
2005-01-16 06:15:51 +08:00
|
|
|
codes++;
|
|
|
|
bytes[0] = *codes++;
|
2007-09-18 06:49:30 +08:00
|
|
|
if (is_sbyte(ins, c - 0154, 32))
|
2005-01-16 06:15:51 +08:00
|
|
|
bytes[0] |= 2; /* s-bit */
|
|
|
|
out(offset, segment, bytes, OUT_RAWDATA + 1, NO_SEG, NO_SEG);
|
|
|
|
offset++;
|
|
|
|
break;
|
|
|
|
|
2007-09-18 07:55:04 +08:00
|
|
|
case 0160:
|
|
|
|
case 0161:
|
|
|
|
case 0162:
|
|
|
|
case 0163:
|
|
|
|
case 0164:
|
|
|
|
case 0165:
|
|
|
|
case 0166:
|
|
|
|
case 0167:
|
|
|
|
break;
|
|
|
|
|
2007-09-18 06:49:30 +08:00
|
|
|
case 0170:
|
2007-10-06 08:04:32 +08:00
|
|
|
EMIT_REX();
|
2007-09-18 06:49:30 +08:00
|
|
|
bytes[0] = 0;
|
|
|
|
out(offset, segment, bytes, OUT_RAWDATA + 1, NO_SEG, NO_SEG);
|
|
|
|
offset += 1;
|
|
|
|
break;
|
|
|
|
|
2007-09-18 07:55:04 +08:00
|
|
|
case 0171:
|
|
|
|
bytes[0] =
|
|
|
|
(ins->drexdst << 4) |
|
|
|
|
(ins->rex & REX_OC ? 0x08 : 0) |
|
|
|
|
(ins->rex & (REX_R|REX_X|REX_B));
|
|
|
|
ins->rex = 0;
|
|
|
|
out(offset, segment, bytes, OUT_RAWDATA + 1, NO_SEG, NO_SEG);
|
|
|
|
offset++;
|
|
|
|
break;
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
case 0300:
|
|
|
|
case 0301:
|
|
|
|
case 0302:
|
2007-09-18 06:49:30 +08:00
|
|
|
case 0303:
|
2005-01-16 06:15:51 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0310:
|
2007-09-23 12:49:51 +08:00
|
|
|
if (bits == 32 && !has_prefix(ins,P_A16)) {
|
2005-01-16 06:15:51 +08:00
|
|
|
*bytes = 0x67;
|
|
|
|
out(offset, segment, bytes,
|
|
|
|
OUT_RAWDATA + 1, NO_SEG, NO_SEG);
|
|
|
|
offset += 1;
|
|
|
|
} else
|
|
|
|
offset += 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0311:
|
2007-09-23 12:49:51 +08:00
|
|
|
if (bits != 32 && !has_prefix(ins,P_A32)) {
|
2005-01-16 06:15:51 +08:00
|
|
|
*bytes = 0x67;
|
|
|
|
out(offset, segment, bytes,
|
|
|
|
OUT_RAWDATA + 1, NO_SEG, NO_SEG);
|
|
|
|
offset += 1;
|
|
|
|
} else
|
|
|
|
offset += 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0312:
|
|
|
|
break;
|
|
|
|
|
2007-04-12 10:40:54 +08:00
|
|
|
case 0313:
|
|
|
|
ins->rex = 0;
|
|
|
|
break;
|
2007-09-23 12:49:51 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
case 0320:
|
2007-04-12 10:40:54 +08:00
|
|
|
if (bits != 16) {
|
2005-01-16 06:15:51 +08:00
|
|
|
*bytes = 0x66;
|
|
|
|
out(offset, segment, bytes,
|
|
|
|
OUT_RAWDATA + 1, NO_SEG, NO_SEG);
|
|
|
|
offset += 1;
|
|
|
|
} else
|
|
|
|
offset += 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0321:
|
|
|
|
if (bits == 16) {
|
|
|
|
*bytes = 0x66;
|
|
|
|
out(offset, segment, bytes,
|
|
|
|
OUT_RAWDATA + 1, NO_SEG, NO_SEG);
|
|
|
|
offset += 1;
|
|
|
|
} else
|
|
|
|
offset += 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0322:
|
2007-04-12 10:40:54 +08:00
|
|
|
case 0323:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0324:
|
2007-05-30 11:25:21 +08:00
|
|
|
ins->rex |= REX_W;
|
2005-01-16 06:15:51 +08:00
|
|
|
break;
|
2007-04-12 10:40:54 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
case 0330:
|
|
|
|
*bytes = *codes++ ^ condval[ins->condition];
|
|
|
|
out(offset, segment, bytes, OUT_RAWDATA + 1, NO_SEG, NO_SEG);
|
|
|
|
offset += 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0331:
|
|
|
|
break;
|
|
|
|
|
2007-09-13 12:58:51 +08:00
|
|
|
case 0332:
|
2005-01-16 06:15:51 +08:00
|
|
|
case 0333:
|
2007-09-13 12:58:51 +08:00
|
|
|
*bytes = c - 0332 + 0xF2;
|
2005-01-16 06:15:51 +08:00
|
|
|
out(offset, segment, bytes, OUT_RAWDATA + 1, NO_SEG, NO_SEG);
|
|
|
|
offset += 1;
|
|
|
|
break;
|
|
|
|
|
2007-08-17 15:37:52 +08:00
|
|
|
case 0334:
|
|
|
|
if (ins->rex & REX_R) {
|
|
|
|
*bytes = 0xF0;
|
|
|
|
out(offset, segment, bytes, OUT_RAWDATA + 1, NO_SEG, NO_SEG);
|
|
|
|
offset += 1;
|
|
|
|
}
|
|
|
|
ins->rex &= ~(REX_L|REX_R);
|
|
|
|
break;
|
2007-04-18 04:23:11 +08:00
|
|
|
|
2007-09-13 12:58:51 +08:00
|
|
|
case 0335:
|
|
|
|
break;
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
case 0340:
|
|
|
|
case 0341:
|
|
|
|
case 0342:
|
|
|
|
if (ins->oprs[0].segment != NO_SEG)
|
|
|
|
errfunc(ERR_PANIC, "non-constant BSS size in pass two");
|
|
|
|
else {
|
2007-04-12 10:40:54 +08:00
|
|
|
int32_t size = ins->oprs[0].offset << (c - 0340);
|
2005-01-16 06:15:51 +08:00
|
|
|
if (size > 0)
|
|
|
|
out(offset, segment, NULL,
|
|
|
|
OUT_RESERVE + size, NO_SEG, NO_SEG);
|
|
|
|
offset += size;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2007-09-12 06:44:03 +08:00
|
|
|
case 0364:
|
|
|
|
case 0365:
|
|
|
|
break;
|
|
|
|
|
2007-08-17 15:37:52 +08:00
|
|
|
case 0366:
|
2007-09-12 06:44:03 +08:00
|
|
|
case 0367:
|
|
|
|
*bytes = c - 0366 + 0x66;
|
2007-08-17 15:37:52 +08:00
|
|
|
out(offset, segment, bytes, OUT_RAWDATA + 1, NO_SEG, NO_SEG);
|
|
|
|
offset += 1;
|
|
|
|
break;
|
2007-09-12 06:44:03 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
case 0370:
|
|
|
|
case 0371:
|
|
|
|
case 0372:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0373:
|
|
|
|
*bytes = bits == 16 ? 3 : 5;
|
|
|
|
out(offset, segment, bytes, OUT_RAWDATA + 1, NO_SEG, NO_SEG);
|
|
|
|
offset += 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: /* can't do it by 'case' statements */
|
2007-05-30 11:25:21 +08:00
|
|
|
if (c >= 0100 && c <= 0277) { /* it's an EA */
|
2005-01-16 06:15:51 +08:00
|
|
|
ea ea_data;
|
|
|
|
int rfield;
|
2007-05-30 11:25:21 +08:00
|
|
|
int32_t rflags;
|
2007-04-12 10:40:54 +08:00
|
|
|
uint8_t *p;
|
|
|
|
int32_t s;
|
|
|
|
|
2007-05-30 11:25:21 +08:00
|
|
|
if (c <= 0177) {
|
|
|
|
/* pick rfield from operand b */
|
|
|
|
rflags = regflag(&ins->oprs[c & 7]);
|
|
|
|
rfield = regvals[ins->oprs[c & 7].basereg];
|
|
|
|
} else {
|
|
|
|
/* rfield is constant */
|
|
|
|
rflags = 0;
|
2005-01-16 06:15:51 +08:00
|
|
|
rfield = c & 7;
|
2007-05-30 11:25:21 +08:00
|
|
|
}
|
2005-01-16 06:15:51 +08:00
|
|
|
|
|
|
|
if (!process_ea
|
2007-05-30 11:25:21 +08:00
|
|
|
(&ins->oprs[(c >> 3) & 7], &ea_data, bits,
|
|
|
|
rfield, rflags, ins->forw_ref)) {
|
2005-01-16 06:15:51 +08:00
|
|
|
errfunc(ERR_NONFATAL, "invalid effective address");
|
|
|
|
}
|
2007-04-12 10:40:54 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
p = bytes;
|
|
|
|
*p++ = ea_data.modrm;
|
|
|
|
if (ea_data.sib_present)
|
|
|
|
*p++ = ea_data.sib;
|
|
|
|
|
2007-09-18 07:55:04 +08:00
|
|
|
/* DREX suffixes come between the SIB and the displacement */
|
|
|
|
if (ins->rex & REX_D) {
|
|
|
|
*p++ =
|
|
|
|
(ins->drexdst << 4) |
|
|
|
|
(ins->rex & REX_OC ? 0x08 : 0) |
|
|
|
|
(ins->rex & (REX_R|REX_X|REX_B));
|
|
|
|
ins->rex = 0;
|
|
|
|
}
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
s = p - bytes;
|
|
|
|
out(offset, segment, bytes, OUT_RAWDATA + s,
|
|
|
|
NO_SEG, NO_SEG);
|
|
|
|
|
|
|
|
switch (ea_data.bytes) {
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
if (ins->oprs[(c >> 3) & 7].segment != NO_SEG) {
|
|
|
|
data = ins->oprs[(c >> 3) & 7].offset;
|
|
|
|
out(offset, segment, &data, OUT_ADDRESS + 1,
|
|
|
|
ins->oprs[(c >> 3) & 7].segment,
|
|
|
|
ins->oprs[(c >> 3) & 7].wrt);
|
|
|
|
} else {
|
|
|
|
*bytes = ins->oprs[(c >> 3) & 7].offset;
|
|
|
|
out(offset, segment, bytes, OUT_RAWDATA + 1,
|
|
|
|
NO_SEG, NO_SEG);
|
|
|
|
}
|
|
|
|
s++;
|
|
|
|
break;
|
2007-04-12 10:40:54 +08:00
|
|
|
case 8:
|
2005-01-16 06:15:51 +08:00
|
|
|
case 2:
|
|
|
|
case 4:
|
|
|
|
data = ins->oprs[(c >> 3) & 7].offset;
|
2007-09-29 08:17:20 +08:00
|
|
|
out(offset, segment, &data,
|
|
|
|
(ea_data.rip ? OUT_REL4ADR : OUT_ADDRESS)
|
|
|
|
+ ea_data.bytes,
|
2005-01-16 06:15:51 +08:00
|
|
|
ins->oprs[(c >> 3) & 7].segment,
|
|
|
|
ins->oprs[(c >> 3) & 7].wrt);
|
|
|
|
s += ea_data.bytes;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
offset += s;
|
|
|
|
} else
|
|
|
|
errfunc(ERR_PANIC, "internal instruction table corrupt"
|
|
|
|
": instruction code 0x%02X given", c);
|
|
|
|
}
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2007-07-07 09:59:52 +08:00
|
|
|
static int32_t regflag(const operand * o)
|
2007-05-30 11:25:21 +08:00
|
|
|
{
|
|
|
|
if (o->basereg < EXPR_REG_START || o->basereg >= REG_ENUM_LIMIT) {
|
|
|
|
errfunc(ERR_PANIC, "invalid operand passed to regflag()");
|
|
|
|
}
|
|
|
|
return reg_flags[o->basereg];
|
|
|
|
}
|
|
|
|
|
2007-07-07 10:01:08 +08:00
|
|
|
static int32_t regval(const operand * o)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2005-01-16 06:15:51 +08:00
|
|
|
if (o->basereg < EXPR_REG_START || o->basereg >= REG_ENUM_LIMIT) {
|
|
|
|
errfunc(ERR_PANIC, "invalid operand passed to regval()");
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
2002-06-06 10:41:20 +08:00
|
|
|
return regvals[o->basereg];
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2007-05-30 11:25:21 +08:00
|
|
|
static int op_rexflags(const operand * o, int mask)
|
|
|
|
{
|
|
|
|
int32_t flags;
|
|
|
|
int val;
|
|
|
|
|
|
|
|
if (o->basereg < EXPR_REG_START || o->basereg >= REG_ENUM_LIMIT) {
|
|
|
|
errfunc(ERR_PANIC, "invalid operand passed to op_rexflags()");
|
|
|
|
}
|
|
|
|
|
|
|
|
flags = reg_flags[o->basereg];
|
|
|
|
val = regvals[o->basereg];
|
|
|
|
|
|
|
|
return rexflags(val, flags, mask);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rexflags(int val, int32_t flags, int mask)
|
|
|
|
{
|
|
|
|
int rex = 0;
|
|
|
|
|
|
|
|
if (val >= 8)
|
|
|
|
rex |= REX_B|REX_X|REX_R;
|
|
|
|
if (flags & BITS64)
|
|
|
|
rex |= REX_W;
|
|
|
|
if (!(REG_HIGH & ~flags)) /* AH, CH, DH, BH */
|
|
|
|
rex |= REX_H;
|
|
|
|
else if (!(REG8 & ~flags) && val >= 4) /* SPL, BPL, SIL, DIL */
|
|
|
|
rex |= REX_P;
|
|
|
|
|
|
|
|
return rex & mask;
|
|
|
|
}
|
|
|
|
|
2007-09-11 12:16:57 +08:00
|
|
|
static int matches(const struct itemplate *itemp, insn * instruction, int bits)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2007-09-18 06:49:30 +08:00
|
|
|
int i, size[MAX_OPERANDS], asize, oprs, ret;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
ret = 100;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check the opcode
|
|
|
|
*/
|
2005-01-16 06:15:51 +08:00
|
|
|
if (itemp->opcode != instruction->opcode)
|
|
|
|
return 0;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Count the operands
|
|
|
|
*/
|
2005-01-16 06:15:51 +08:00
|
|
|
if (itemp->operands != instruction->operands)
|
|
|
|
return 0;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check that no spurious colons or TOs are present
|
|
|
|
*/
|
2005-01-16 06:15:51 +08:00
|
|
|
for (i = 0; i < itemp->operands; i++)
|
|
|
|
if (instruction->oprs[i].type & ~itemp->opd[i] & (COLON | TO))
|
|
|
|
return 0;
|
2007-04-12 10:40:54 +08:00
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
/*
|
|
|
|
* Check that the operand flags all match up
|
|
|
|
*/
|
2007-09-13 11:27:41 +08:00
|
|
|
for (i = 0; i < itemp->operands; i++) {
|
2007-09-18 08:25:27 +08:00
|
|
|
if (itemp->opd[i] & SAME_AS) {
|
|
|
|
int j = itemp->opd[i] & ~SAME_AS;
|
|
|
|
if (instruction->oprs[i].type != instruction->oprs[j].type ||
|
|
|
|
instruction->oprs[i].basereg != instruction->oprs[j].basereg)
|
|
|
|
return 0;
|
2007-09-26 06:41:19 +08:00
|
|
|
} else if (itemp->opd[i] & ~instruction->oprs[i].type ||
|
2005-01-16 06:15:51 +08:00
|
|
|
((itemp->opd[i] & SIZE_MASK) &&
|
|
|
|
((itemp->opd[i] ^ instruction->oprs[i].type) & SIZE_MASK))) {
|
2007-05-30 07:57:12 +08:00
|
|
|
if ((itemp->opd[i] & ~instruction->oprs[i].type & ~SIZE_MASK) ||
|
2005-01-16 06:15:51 +08:00
|
|
|
(instruction->oprs[i].type & SIZE_MASK))
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
}
|
2007-09-13 11:27:41 +08:00
|
|
|
}
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check operand sizes
|
|
|
|
*/
|
2002-05-01 04:57:59 +08:00
|
|
|
if (itemp->flags & IF_ARMASK) {
|
2007-09-18 06:49:30 +08:00
|
|
|
memset(size, 0, sizeof size);
|
2005-01-16 06:15:51 +08:00
|
|
|
|
|
|
|
switch (itemp->flags & IF_ARMASK) {
|
|
|
|
case IF_AR0:
|
|
|
|
i = 0;
|
|
|
|
break;
|
|
|
|
case IF_AR1:
|
|
|
|
i = 1;
|
|
|
|
break;
|
|
|
|
case IF_AR2:
|
|
|
|
i = 2;
|
|
|
|
break;
|
2007-09-18 06:49:30 +08:00
|
|
|
case IF_AR3:
|
|
|
|
i = 3;
|
|
|
|
break;
|
2005-01-16 06:15:51 +08:00
|
|
|
default:
|
|
|
|
break; /* Shouldn't happen */
|
|
|
|
}
|
2007-09-18 06:49:30 +08:00
|
|
|
switch (itemp->flags & IF_SMASK) {
|
|
|
|
case IF_SB:
|
2005-01-16 06:15:51 +08:00
|
|
|
size[i] = BITS8;
|
2007-09-18 06:49:30 +08:00
|
|
|
break;
|
|
|
|
case IF_SW:
|
2005-01-16 06:15:51 +08:00
|
|
|
size[i] = BITS16;
|
2007-09-18 06:49:30 +08:00
|
|
|
break;
|
|
|
|
case IF_SD:
|
2005-01-16 06:15:51 +08:00
|
|
|
size[i] = BITS32;
|
2007-09-18 06:49:30 +08:00
|
|
|
break;
|
|
|
|
case IF_SQ:
|
2007-04-12 10:40:54 +08:00
|
|
|
size[i] = BITS64;
|
2007-09-18 06:49:30 +08:00
|
|
|
break;
|
2007-09-19 04:01:32 +08:00
|
|
|
case IF_SO:
|
|
|
|
size[i] = BITS128;
|
|
|
|
break;
|
2007-09-18 06:49:30 +08:00
|
|
|
default:
|
|
|
|
break;
|
2005-01-16 06:15:51 +08:00
|
|
|
}
|
2002-05-01 04:57:59 +08:00
|
|
|
} else {
|
2005-01-16 06:15:51 +08:00
|
|
|
asize = 0;
|
2007-09-18 06:49:30 +08:00
|
|
|
switch (itemp->flags & IF_SMASK) {
|
|
|
|
case IF_SB:
|
2005-01-16 06:15:51 +08:00
|
|
|
asize = BITS8;
|
|
|
|
oprs = itemp->operands;
|
2007-09-18 06:49:30 +08:00
|
|
|
break;
|
|
|
|
case IF_SW:
|
2005-01-16 06:15:51 +08:00
|
|
|
asize = BITS16;
|
|
|
|
oprs = itemp->operands;
|
2007-09-18 06:49:30 +08:00
|
|
|
break;
|
|
|
|
case IF_SD:
|
2005-01-16 06:15:51 +08:00
|
|
|
asize = BITS32;
|
|
|
|
oprs = itemp->operands;
|
2007-09-18 06:49:30 +08:00
|
|
|
break;
|
|
|
|
case IF_SQ:
|
2007-04-12 10:40:54 +08:00
|
|
|
asize = BITS64;
|
|
|
|
oprs = itemp->operands;
|
2007-09-18 06:49:30 +08:00
|
|
|
break;
|
2007-09-19 04:01:32 +08:00
|
|
|
case IF_SO:
|
|
|
|
asize = BITS128;
|
|
|
|
oprs = itemp->operands;
|
|
|
|
break;
|
2007-09-18 06:49:30 +08:00
|
|
|
default:
|
|
|
|
break;
|
2005-01-16 06:15:51 +08:00
|
|
|
}
|
2007-09-18 06:49:30 +08:00
|
|
|
for (i = 0; i < MAX_OPERANDS; i++)
|
|
|
|
size[i] = asize;
|
2002-05-01 04:57:59 +08:00
|
|
|
}
|
2007-04-12 10:40:54 +08:00
|
|
|
|
2002-05-01 04:57:59 +08:00
|
|
|
if (itemp->flags & (IF_SM | IF_SM2)) {
|
2005-01-16 06:15:51 +08:00
|
|
|
oprs = (itemp->flags & IF_SM2 ? 2 : itemp->operands);
|
|
|
|
asize = 0;
|
|
|
|
for (i = 0; i < oprs; i++) {
|
|
|
|
if ((asize = itemp->opd[i] & SIZE_MASK) != 0) {
|
|
|
|
int j;
|
|
|
|
for (j = 0; j < oprs; j++)
|
|
|
|
size[j] = asize;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2002-05-01 04:51:32 +08:00
|
|
|
} else {
|
2005-01-16 06:15:51 +08:00
|
|
|
oprs = itemp->operands;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2007-04-12 10:40:54 +08:00
|
|
|
for (i = 0; i < itemp->operands; i++) {
|
2005-01-16 06:15:51 +08:00
|
|
|
if (!(itemp->opd[i] & SIZE_MASK) &&
|
|
|
|
(instruction->oprs[i].type & SIZE_MASK & ~size[i]))
|
|
|
|
return 2;
|
2007-04-12 10:40:54 +08:00
|
|
|
}
|
|
|
|
|
2002-05-01 04:59:21 +08:00
|
|
|
/*
|
|
|
|
* Check template is okay at the set cpu level
|
|
|
|
*/
|
2007-04-12 10:40:54 +08:00
|
|
|
if (((itemp->flags & IF_PLEVEL) > cpu))
|
2005-01-16 06:15:51 +08:00
|
|
|
return 3;
|
2007-04-12 10:40:54 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if instruction is available in long mode
|
|
|
|
*/
|
|
|
|
if ((itemp->flags & IF_NOLONG) && (bits == 64))
|
|
|
|
return 4;
|
2005-01-16 06:15:51 +08:00
|
|
|
|
2002-05-01 04:59:21 +08:00
|
|
|
/*
|
|
|
|
* Check if special handling needed for Jumps
|
|
|
|
*/
|
2007-04-12 10:40:54 +08:00
|
|
|
if ((uint8_t)(itemp->code[0]) >= 0370)
|
2005-01-16 06:15:51 +08:00
|
|
|
return 99;
|
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
static ea *process_ea(operand * input, ea * output, int addrbits,
|
2007-05-30 11:25:21 +08:00
|
|
|
int rfield, int32_t rflags, int forw_ref)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2007-08-29 07:06:00 +08:00
|
|
|
output->rip = FALSE;
|
|
|
|
|
2007-05-30 11:25:21 +08:00
|
|
|
/* REX flags for the rfield operand */
|
|
|
|
output->rex |= rexflags(rfield, rflags, REX_R|REX_P|REX_W|REX_H);
|
|
|
|
|
2007-04-12 10:40:54 +08:00
|
|
|
if (!(REGISTER & ~input->type)) { /* register direct */
|
2005-01-16 06:15:51 +08:00
|
|
|
int i;
|
2007-05-30 11:25:21 +08:00
|
|
|
int32_t f;
|
|
|
|
|
|
|
|
if (input->basereg < EXPR_REG_START /* Verify as Register */
|
2007-04-12 10:40:54 +08:00
|
|
|
|| input->basereg >= REG_ENUM_LIMIT)
|
2005-01-16 06:15:51 +08:00
|
|
|
return NULL;
|
2007-05-30 11:25:21 +08:00
|
|
|
f = regflag(input);
|
2007-04-12 10:40:54 +08:00
|
|
|
i = regvals[input->basereg];
|
|
|
|
|
2007-05-30 11:25:21 +08:00
|
|
|
if (REG_EA & ~f)
|
|
|
|
return NULL; /* Invalid EA register */
|
|
|
|
|
|
|
|
output->rex |= op_rexflags(input, REX_B|REX_P|REX_W|REX_H);
|
|
|
|
|
2007-04-12 10:40:54 +08:00
|
|
|
output->sib_present = FALSE; /* no SIB necessary */
|
|
|
|
output->bytes = 0; /* no offset necessary either */
|
|
|
|
output->modrm = 0xC0 | ((rfield & 7) << 3) | (i & 7);
|
2005-01-16 06:15:51 +08:00
|
|
|
} else { /* it's a memory reference */
|
|
|
|
if (input->basereg == -1
|
|
|
|
&& (input->indexreg == -1 || input->scale == 0)) {
|
|
|
|
/* it's a pure offset */
|
|
|
|
if (input->addr_size)
|
|
|
|
addrbits = input->addr_size;
|
2007-08-29 07:06:00 +08:00
|
|
|
|
|
|
|
if (globalbits == 64 && (~input->type & IP_REL)) {
|
2007-06-03 10:42:41 +08:00
|
|
|
int scale, index, base;
|
|
|
|
output->sib_present = TRUE;
|
|
|
|
scale = 0;
|
|
|
|
index = 4;
|
|
|
|
base = 5;
|
|
|
|
output->sib = (scale << 6) | (index << 3) | base;
|
|
|
|
output->bytes = 4;
|
|
|
|
output->modrm = 4 | ((rfield & 7) << 3);
|
2007-08-29 07:06:00 +08:00
|
|
|
output->rip = FALSE;
|
2007-06-03 10:42:41 +08:00
|
|
|
} else {
|
|
|
|
output->sib_present = FALSE;
|
|
|
|
output->bytes = (addrbits != 16 ? 4 : 2);
|
|
|
|
output->modrm = (addrbits != 16 ? 5 : 6) | ((rfield & 7) << 3);
|
2007-08-29 07:06:00 +08:00
|
|
|
output->rip = globalbits == 64;
|
2007-06-03 10:42:41 +08:00
|
|
|
}
|
2005-01-16 06:15:51 +08:00
|
|
|
} else { /* it's an indirection */
|
|
|
|
int i = input->indexreg, b = input->basereg, s = input->scale;
|
2007-04-12 10:40:54 +08:00
|
|
|
int32_t o = input->offset, seg = input->segment;
|
2005-01-16 06:15:51 +08:00
|
|
|
int hb = input->hintbase, ht = input->hinttype;
|
|
|
|
int t;
|
2007-04-12 10:40:54 +08:00
|
|
|
int it, bt;
|
2007-05-30 11:25:21 +08:00
|
|
|
int32_t ix, bx; /* register flags */
|
2005-01-16 06:15:51 +08:00
|
|
|
|
|
|
|
if (s == 0)
|
|
|
|
i = -1; /* make this easy, at least */
|
2007-04-12 10:40:54 +08:00
|
|
|
|
2007-05-30 11:25:21 +08:00
|
|
|
if (i >= EXPR_REG_START && i < REG_ENUM_LIMIT) {
|
2007-04-12 10:40:54 +08:00
|
|
|
it = regvals[i];
|
2007-05-30 11:25:21 +08:00
|
|
|
ix = reg_flags[i];
|
|
|
|
} else {
|
2007-04-12 10:40:54 +08:00
|
|
|
it = -1;
|
2007-05-30 11:25:21 +08:00
|
|
|
ix = 0;
|
|
|
|
}
|
2007-04-12 10:40:54 +08:00
|
|
|
|
2007-05-30 11:25:21 +08:00
|
|
|
if (b != -1 && b >= EXPR_REG_START && b < REG_ENUM_LIMIT) {
|
2007-04-12 10:40:54 +08:00
|
|
|
bt = regvals[b];
|
2007-05-30 11:25:21 +08:00
|
|
|
bx = reg_flags[b];
|
|
|
|
} else {
|
2007-04-12 10:40:54 +08:00
|
|
|
bt = -1;
|
2007-05-30 11:25:21 +08:00
|
|
|
bx = 0;
|
|
|
|
}
|
2007-04-12 10:40:54 +08:00
|
|
|
|
2007-05-30 11:25:21 +08:00
|
|
|
/* check for a 32/64-bit memory reference... */
|
|
|
|
if ((ix|bx) & (BITS32|BITS64)) {
|
2007-04-12 10:40:54 +08:00
|
|
|
/* it must be a 32/64-bit memory reference. Firstly we have
|
|
|
|
* to check that all registers involved are type E/Rxx. */
|
2007-05-30 11:25:21 +08:00
|
|
|
int32_t sok = BITS32|BITS64;
|
|
|
|
|
2007-04-12 10:40:54 +08:00
|
|
|
if (it != -1) {
|
2007-05-30 11:25:21 +08:00
|
|
|
if (!(REG64 & ~ix) || !(REG32 & ~ix))
|
|
|
|
sok &= ix;
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bt != -1) {
|
2007-08-29 07:06:00 +08:00
|
|
|
if (REG_GPR & ~bx)
|
2007-05-30 11:25:21 +08:00
|
|
|
return NULL; /* Invalid register */
|
2007-05-30 11:44:02 +08:00
|
|
|
if (~sok & bx & SIZE_MASK)
|
2007-05-30 11:25:21 +08:00
|
|
|
return NULL; /* Invalid size */
|
|
|
|
sok &= ~bx;
|
|
|
|
}
|
2007-04-12 10:40:54 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
/* While we're here, ensure the user didn't specify WORD. */
|
2007-05-30 11:25:21 +08:00
|
|
|
if (input->addr_size == 16 ||
|
|
|
|
(input->addr_size == 32 && !(sok & BITS32)) ||
|
|
|
|
(input->addr_size == 64 && !(sok & BITS64)))
|
2005-01-16 06:15:51 +08:00
|
|
|
return NULL;
|
|
|
|
|
2007-04-12 10:40:54 +08:00
|
|
|
/* now reorganize base/index */
|
|
|
|
if (s == 1 && bt != it && bt != -1 && it != -1 &&
|
2007-05-30 11:25:21 +08:00
|
|
|
((hb == b && ht == EAH_NOTBASE)
|
|
|
|
|| (hb == i && ht == EAH_MAKEBASE))) {
|
|
|
|
/* swap if hints say so */
|
|
|
|
t = bt, bt = it, it = t;
|
|
|
|
t = bx, bx = ix, ix = t;
|
|
|
|
}
|
2007-04-12 10:40:54 +08:00
|
|
|
if (bt == it) /* convert EAX+2*EAX to 3*EAX */
|
2007-05-30 11:25:21 +08:00
|
|
|
bt = -1, bx = 0, s++;
|
|
|
|
if (bt == -1 && s == 1 && !(hb == it && ht == EAH_NOTBASE)) {
|
|
|
|
/* make single reg base, unless hint */
|
|
|
|
bt = it, bx = ix, it = -1, ix = 0;
|
|
|
|
}
|
2007-09-11 02:59:26 +08:00
|
|
|
if (((s == 2 && it != REG_NUM_ESP
|
2005-01-16 06:15:51 +08:00
|
|
|
&& !(input->eaflags & EAF_TIMESTWO)) || s == 3
|
2007-04-12 10:40:54 +08:00
|
|
|
|| s == 5 || s == 9) && bt == -1)
|
2007-05-30 11:25:21 +08:00
|
|
|
bt = it, bx = ix, s--; /* convert 3*EAX to EAX+2*EAX */
|
2007-08-17 15:37:52 +08:00
|
|
|
if (it == -1 && (bt & 7) != REG_NUM_ESP
|
2005-01-16 06:15:51 +08:00
|
|
|
&& (input->eaflags & EAF_TIMESTWO))
|
2007-05-30 11:25:21 +08:00
|
|
|
it = bt, ix = bx, bt = -1, bx = 0, s = 1;
|
2005-01-16 06:15:51 +08:00
|
|
|
/* convert [NOSPLIT EAX] to sib format with 0x0 displacement */
|
2007-08-17 15:37:52 +08:00
|
|
|
if (s == 1 && it == REG_NUM_ESP) {
|
2007-05-30 11:25:21 +08:00
|
|
|
/* swap ESP into base if scale is 1 */
|
2007-04-12 10:40:54 +08:00
|
|
|
t = it, it = bt, bt = t;
|
2007-05-30 11:25:21 +08:00
|
|
|
t = ix, ix = bx, bx = t;
|
|
|
|
}
|
2007-08-17 15:37:52 +08:00
|
|
|
if (it == REG_NUM_ESP
|
2007-04-12 10:40:54 +08:00
|
|
|
|| (s != 1 && s != 2 && s != 4 && s != 8 && it != -1))
|
2005-01-16 06:15:51 +08:00
|
|
|
return NULL; /* wrong, for various reasons */
|
|
|
|
|
2007-05-30 11:25:21 +08:00
|
|
|
output->rex |= rexflags(it, ix, REX_X);
|
|
|
|
output->rex |= rexflags(bt, bx, REX_B);
|
2007-04-12 10:40:54 +08:00
|
|
|
|
2007-08-17 15:37:52 +08:00
|
|
|
if (it == -1 && (bt & 7) != REG_NUM_ESP) {
|
2007-05-30 11:25:21 +08:00
|
|
|
/* no SIB needed */
|
2005-01-16 06:15:51 +08:00
|
|
|
int mod, rm;
|
2007-04-12 10:40:54 +08:00
|
|
|
|
|
|
|
if (bt == -1) {
|
2005-01-16 06:15:51 +08:00
|
|
|
rm = 5;
|
|
|
|
mod = 0;
|
2007-04-12 10:40:54 +08:00
|
|
|
} else {
|
|
|
|
rm = (bt & 7);
|
2007-08-17 15:37:52 +08:00
|
|
|
if (rm != REG_NUM_EBP && o == 0 &&
|
2007-04-12 10:40:54 +08:00
|
|
|
seg == NO_SEG && !forw_ref &&
|
|
|
|
!(input->eaflags &
|
|
|
|
(EAF_BYTEOFFS | EAF_WORDOFFS)))
|
|
|
|
mod = 0;
|
|
|
|
else if (input->eaflags & EAF_BYTEOFFS ||
|
|
|
|
(o >= -128 && o <= 127 && seg == NO_SEG
|
|
|
|
&& !forw_ref
|
|
|
|
&& !(input->eaflags & EAF_WORDOFFS)))
|
|
|
|
mod = 1;
|
|
|
|
else
|
|
|
|
mod = 2;
|
|
|
|
}
|
2005-01-16 06:15:51 +08:00
|
|
|
|
|
|
|
output->sib_present = FALSE;
|
2007-04-12 10:40:54 +08:00
|
|
|
output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
|
|
|
|
output->modrm = (mod << 6) | ((rfield & 7) << 3) | rm;
|
2007-05-30 11:25:21 +08:00
|
|
|
} else {
|
|
|
|
/* we need a SIB */
|
2005-01-16 06:15:51 +08:00
|
|
|
int mod, scale, index, base;
|
2007-04-12 10:40:54 +08:00
|
|
|
|
|
|
|
if (it == -1)
|
|
|
|
index = 4, s = 1;
|
|
|
|
else
|
|
|
|
index = (it & 7);
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
switch (s) {
|
|
|
|
case 1:
|
|
|
|
scale = 0;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
scale = 1;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
scale = 2;
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
scale = 3;
|
|
|
|
break;
|
|
|
|
default: /* then what the smeg is it? */
|
|
|
|
return NULL; /* panic */
|
|
|
|
}
|
2007-04-12 10:40:54 +08:00
|
|
|
|
|
|
|
if (bt == -1) {
|
|
|
|
base = 5;
|
|
|
|
mod = 0;
|
|
|
|
} else {
|
|
|
|
base = (bt & 7);
|
2007-08-17 15:37:52 +08:00
|
|
|
if (base != REG_NUM_EBP && o == 0 &&
|
2005-01-16 06:15:51 +08:00
|
|
|
seg == NO_SEG && !forw_ref &&
|
|
|
|
!(input->eaflags &
|
2007-04-12 10:40:54 +08:00
|
|
|
(EAF_BYTEOFFS | EAF_WORDOFFS)))
|
|
|
|
mod = 0;
|
|
|
|
else if (input->eaflags & EAF_BYTEOFFS ||
|
|
|
|
(o >= -128 && o <= 127 && seg == NO_SEG
|
|
|
|
&& !forw_ref
|
|
|
|
&& !(input->eaflags & EAF_WORDOFFS)))
|
|
|
|
mod = 1;
|
|
|
|
else
|
|
|
|
mod = 2;
|
|
|
|
}
|
2005-01-16 06:15:51 +08:00
|
|
|
|
|
|
|
output->sib_present = TRUE;
|
2007-04-12 10:40:54 +08:00
|
|
|
output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
|
|
|
|
output->modrm = (mod << 6) | ((rfield & 7) << 3) | 4;
|
2005-01-16 06:15:51 +08:00
|
|
|
output->sib = (scale << 6) | (index << 3) | base;
|
|
|
|
}
|
|
|
|
} else { /* it's 16-bit */
|
|
|
|
int mod, rm;
|
2007-04-12 10:40:54 +08:00
|
|
|
|
|
|
|
/* check for 64-bit long mode */
|
|
|
|
if (addrbits == 64)
|
|
|
|
return NULL;
|
2005-01-16 06:15:51 +08:00
|
|
|
|
|
|
|
/* check all registers are BX, BP, SI or DI */
|
|
|
|
if ((b != -1 && b != R_BP && b != R_BX && b != R_SI
|
|
|
|
&& b != R_DI) || (i != -1 && i != R_BP && i != R_BX
|
|
|
|
&& i != R_SI && i != R_DI))
|
|
|
|
return NULL;
|
|
|
|
|
2007-04-12 10:40:54 +08:00
|
|
|
/* ensure the user didn't specify DWORD/QWORD */
|
|
|
|
if (input->addr_size == 32 || input->addr_size == 64)
|
2005-01-16 06:15:51 +08:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (s != 1 && i != -1)
|
|
|
|
return NULL; /* no can do, in 16-bit EA */
|
|
|
|
if (b == -1 && i != -1) {
|
|
|
|
int tmp = b;
|
|
|
|
b = i;
|
|
|
|
i = tmp;
|
|
|
|
} /* swap */
|
|
|
|
if ((b == R_SI || b == R_DI) && i != -1) {
|
|
|
|
int tmp = b;
|
|
|
|
b = i;
|
|
|
|
i = tmp;
|
|
|
|
}
|
|
|
|
/* have BX/BP as base, SI/DI index */
|
|
|
|
if (b == i)
|
|
|
|
return NULL; /* shouldn't ever happen, in theory */
|
|
|
|
if (i != -1 && b != -1 &&
|
|
|
|
(i == R_BP || i == R_BX || b == R_SI || b == R_DI))
|
|
|
|
return NULL; /* invalid combinations */
|
|
|
|
if (b == -1) /* pure offset: handled above */
|
|
|
|
return NULL; /* so if it gets to here, panic! */
|
|
|
|
|
|
|
|
rm = -1;
|
|
|
|
if (i != -1)
|
|
|
|
switch (i * 256 + b) {
|
|
|
|
case R_SI * 256 + R_BX:
|
|
|
|
rm = 0;
|
|
|
|
break;
|
|
|
|
case R_DI * 256 + R_BX:
|
|
|
|
rm = 1;
|
|
|
|
break;
|
|
|
|
case R_SI * 256 + R_BP:
|
|
|
|
rm = 2;
|
|
|
|
break;
|
|
|
|
case R_DI * 256 + R_BP:
|
|
|
|
rm = 3;
|
|
|
|
break;
|
|
|
|
} else
|
|
|
|
switch (b) {
|
|
|
|
case R_SI:
|
|
|
|
rm = 4;
|
|
|
|
break;
|
|
|
|
case R_DI:
|
|
|
|
rm = 5;
|
|
|
|
break;
|
|
|
|
case R_BP:
|
|
|
|
rm = 6;
|
|
|
|
break;
|
|
|
|
case R_BX:
|
|
|
|
rm = 7;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (rm == -1) /* can't happen, in theory */
|
|
|
|
return NULL; /* so panic if it does */
|
|
|
|
|
|
|
|
if (o == 0 && seg == NO_SEG && !forw_ref && rm != 6 &&
|
|
|
|
!(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
|
|
|
|
mod = 0;
|
|
|
|
else if (input->eaflags & EAF_BYTEOFFS ||
|
|
|
|
(o >= -128 && o <= 127 && seg == NO_SEG
|
|
|
|
&& !forw_ref
|
|
|
|
&& !(input->eaflags & EAF_WORDOFFS)))
|
|
|
|
mod = 1;
|
|
|
|
else
|
|
|
|
mod = 2;
|
|
|
|
|
|
|
|
output->sib_present = FALSE; /* no SIB - it's 16-bit */
|
|
|
|
output->bytes = mod; /* bytes of offset needed */
|
2007-04-12 10:40:54 +08:00
|
|
|
output->modrm = (mod << 6) | ((rfield & 7) << 3) | rm;
|
2005-01-16 06:15:51 +08:00
|
|
|
}
|
|
|
|
}
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
2007-04-12 10:40:54 +08:00
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
output->size = 1 + output->sib_present + output->bytes;
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2007-09-23 12:49:51 +08:00
|
|
|
static void add_asp(insn *instruction, int addrbits)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2007-09-23 12:49:51 +08:00
|
|
|
int j, valid;
|
|
|
|
|
|
|
|
valid = (addrbits == 64) ? 64|32 : 32|16;
|
|
|
|
|
|
|
|
for (j = 0; j < instruction->operands; j++) {
|
|
|
|
if (!(MEMORY & ~instruction->oprs[j].type)) {
|
|
|
|
int32_t i, b;
|
|
|
|
|
|
|
|
/* Verify as Register */
|
|
|
|
if (instruction->oprs[j].indexreg < EXPR_REG_START
|
|
|
|
|| instruction->oprs[j].indexreg >= REG_ENUM_LIMIT)
|
|
|
|
i = 0;
|
|
|
|
else
|
|
|
|
i = reg_flags[instruction->oprs[j].indexreg];
|
|
|
|
|
|
|
|
/* Verify as Register */
|
|
|
|
if (instruction->oprs[j].basereg < EXPR_REG_START
|
|
|
|
|| instruction->oprs[j].basereg >= REG_ENUM_LIMIT)
|
|
|
|
b = 0;
|
|
|
|
else
|
|
|
|
b = reg_flags[instruction->oprs[j].basereg];
|
|
|
|
|
|
|
|
if (instruction->oprs[j].scale == 0)
|
|
|
|
i = 0;
|
|
|
|
|
|
|
|
if (!i && !b) {
|
|
|
|
if (instruction->oprs[j].addr_size)
|
|
|
|
valid &= instruction->oprs[j].addr_size;
|
|
|
|
} else {
|
|
|
|
if (!(REG16 & ~b))
|
|
|
|
valid &= 16;
|
|
|
|
if (!(REG32 & ~b))
|
|
|
|
valid &= 32;
|
|
|
|
if (!(REG64 & ~b))
|
|
|
|
valid &= 64;
|
|
|
|
|
|
|
|
if (!(REG16 & ~i))
|
|
|
|
valid &= 16;
|
|
|
|
if (!(REG32 & ~i))
|
|
|
|
valid &= 32;
|
|
|
|
if (!(REG64 & ~i))
|
|
|
|
valid &= 64;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-01-16 06:15:51 +08:00
|
|
|
|
2007-09-23 12:49:51 +08:00
|
|
|
if (valid & addrbits) {
|
|
|
|
/* Don't do anything */
|
|
|
|
} else if (valid & ((addrbits == 32) ? 16 : 32)) {
|
|
|
|
/* Add an instruction size prefix */
|
|
|
|
enum prefixes pref = (addrbits == 32) ? P_A16 : P_A32;
|
|
|
|
for (j = 0; j < instruction->nprefix; j++) {
|
|
|
|
if (instruction->prefixes[j] == pref)
|
|
|
|
return; /* Already there */
|
|
|
|
}
|
|
|
|
instruction->prefixes[j] = pref;
|
|
|
|
instruction->nprefix++;
|
2007-05-30 11:25:21 +08:00
|
|
|
} else {
|
2007-09-23 12:49:51 +08:00
|
|
|
/* Impossible... */
|
|
|
|
errfunc(ERR_NONFATAL, "impossible combination of address sizes");
|
2007-05-30 11:25:21 +08:00
|
|
|
}
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|