nasm/parser.c

780 lines
23 KiB
C
Raw Normal View History

2002-05-01 04:51:32 +08:00
/* parser.c source line parser 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.
*
* initial version 27/iii/95 by Simon Tatham
*/
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <ctype.h>
#include "nasm.h"
#include "nasmlib.h"
#include "parser.h"
#include "float.h"
static long reg_flags[] = { /* sizes and special flags */
0, REG8, REG_AL, REG_AX, REG8, REG8, REG16, REG16, REG8, REG_CL,
REG_CREG, REG_CREG, REG_CREG, REG_CR4, REG_CS, REG_CX, REG8,
REG16, REG8, REG_DREG, REG_DREG, REG_DREG, REG_DREG, REG_DREG,
REG_DREG, REG_DESS, REG_DX, REG_EAX, REG32, REG32, REG_ECX,
REG32, REG32, REG_DESS, REG32, REG32, REG_FSGS, REG_FSGS,
MMXREG, MMXREG, MMXREG, MMXREG, MMXREG, MMXREG, MMXREG, MMXREG,
REG16, REG16, REG_DESS, FPU0, FPUREG, FPUREG, FPUREG, FPUREG,
FPUREG, FPUREG, FPUREG, REG_TREG, REG_TREG, REG_TREG, REG_TREG,
2002-05-01 04:56:43 +08:00
REG_TREG,
XMMREG, XMMREG, XMMREG, XMMREG, XMMREG, XMMREG, XMMREG, XMMREG
2002-05-01 04:51:32 +08:00
};
enum { /* special tokens */
2002-05-01 04:52:49 +08:00
S_BYTE, S_DWORD, S_FAR, S_LONG, S_NEAR, S_NOSPLIT, S_QWORD,
S_SHORT, S_TO, S_TWORD, S_WORD
2002-05-01 04:51:32 +08:00
};
static int is_comma_next (void);
static int i;
static struct tokenval tokval;
static efunc error;
2002-05-01 04:53:55 +08:00
static struct ofmt *outfmt; /* Structure of addresses of output routines */
static loc_t *location; /* Pointer to current line's segment,offset */
void parser_global_info (struct ofmt *output, loc_t *locp)
{
outfmt = output;
location = locp;
}
2002-05-01 04:51:53 +08:00
2002-05-01 04:52:49 +08:00
insn *parse_line (int pass, char *buffer, insn *result,
2002-05-01 04:53:55 +08:00
efunc errfunc, evalfunc evaluate, ldfunc ldef)
{
2002-05-01 04:51:32 +08:00
int operand;
int critical;
2002-05-01 04:52:49 +08:00
struct eval_hints hints;
2002-05-01 04:51:32 +08:00
2002-05-01 04:52:49 +08:00
result->forw_ref = FALSE;
2002-05-01 04:51:32 +08:00
error = errfunc;
2002-05-01 04:52:49 +08:00
stdscan_reset();
stdscan_bufptr = buffer;
i = stdscan(NULL, &tokval);
2002-05-01 04:51:32 +08:00
2002-05-01 04:53:55 +08:00
result->label = NULL; /* Assume no label */
2002-05-01 04:51:32 +08:00
result->eops = NULL; /* must do this, whatever happens */
2002-05-01 04:52:49 +08:00
result->operands = 0; /* must initialise this */
2002-05-01 04:51:32 +08:00
if (i==0) { /* blank line - ignore */
result->opcode = -1; /* and no instruction either */
return result;
}
if (i != TOKEN_ID && i != TOKEN_INSN && i != TOKEN_PREFIX &&
(i!=TOKEN_REG || (REG_SREG & ~reg_flags[tokval.t_integer]))) {
error (ERR_NONFATAL, "label or instruction expected"
" at start of line");
result->opcode = -1;
return result;
}
if (i == TOKEN_ID) { /* there's a label here */
2002-05-01 04:52:49 +08:00
result->label = tokval.t_charptr;
i = stdscan(NULL, &tokval);
2002-05-01 04:51:32 +08:00
if (i == ':') { /* skip over the optional colon */
2002-05-01 04:52:49 +08:00
i = stdscan(NULL, &tokval);
2002-05-01 04:53:55 +08:00
} else if (i == 0) {
error (ERR_WARNING|ERR_WARN_OL|ERR_PASS1,
2002-05-01 04:52:26 +08:00
"label alone on a line without a colon might be in error");
2002-05-01 04:51:32 +08:00
}
2002-05-01 04:53:55 +08:00
if (i != TOKEN_INSN || tokval.t_integer != I_EQU)
{
/*
* FIXME: location->segment could be NO_SEG, in which case
* it is possible we should be passing 'abs_seg'. Look into this.
* Work out whether that is *really* what we should be doing.
* Generally fix things. I think this is right as it is, but
* am still not certain.
*/
ldef (result->label, location->segment,
location->offset, NULL, TRUE, FALSE, outfmt, errfunc);
}
}
2002-05-01 04:51:32 +08:00
if (i==0) {
result->opcode = -1; /* this line contains just a label */
return result;
}
result->nprefix = 0;
2002-05-01 04:52:08 +08:00
result->times = 1L;
2002-05-01 04:51:32 +08:00
while (i == TOKEN_PREFIX ||
2002-05-01 04:53:55 +08:00
(i==TOKEN_REG && !(REG_SREG & ~reg_flags[tokval.t_integer])))
{
2002-05-01 04:51:32 +08:00
/*
* Handle special case: the TIMES prefix.
*/
if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
expr *value;
2002-05-01 04:52:49 +08:00
i = stdscan(NULL, &tokval);
2002-05-01 05:09:12 +08:00
value = evaluate (stdscan, NULL, &tokval, NULL, pass0, error, NULL);
2002-05-01 04:52:49 +08:00
i = tokval.t_type;
2002-05-01 04:51:32 +08:00
if (!value) { /* but, error in evaluator */
result->opcode = -1; /* unrecoverable parse error: */
return result; /* ignore this instruction */
}
if (!is_simple (value)) {
error (ERR_NONFATAL,
"non-constant argument supplied to TIMES");
2002-05-01 04:52:08 +08:00
result->times = 1L;
} else {
2002-05-01 04:51:32 +08:00
result->times = value->value;
2002-05-01 04:53:55 +08:00
if (value->value < 0) {
2002-05-01 04:52:26 +08:00
error(ERR_NONFATAL, "TIMES value %d is negative",
2002-05-01 04:52:08 +08:00
value->value);
2002-05-01 04:53:55 +08:00
result->times = 0;
}
2002-05-01 04:52:08 +08:00
}
2002-05-01 04:51:32 +08:00
} else {
if (result->nprefix == MAXPREFIX)
error (ERR_NONFATAL,
"instruction has more than %d prefixes", MAXPREFIX);
else
result->prefixes[result->nprefix++] = tokval.t_integer;
2002-05-01 04:52:49 +08:00
i = stdscan(NULL, &tokval);
2002-05-01 04:51:32 +08:00
}
}
if (i != TOKEN_INSN) {
2002-05-01 04:52:49 +08:00
if (result->nprefix > 0 && i == 0) {
/*
* Instruction prefixes are present, but no actual
* instruction. This is allowed: at this point we
* invent a notional instruction of RESB 0.
*/
result->opcode = I_RESB;
result->operands = 1;
result->oprs[0].type = IMMEDIATE;
result->oprs[0].offset = 0L;
result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
return result;
} else {
error (ERR_NONFATAL, "parser: instruction expected");
result->opcode = -1;
return result;
}
2002-05-01 04:51:32 +08:00
}
result->opcode = tokval.t_integer;
result->condition = tokval.t_inttwo;
/*
* RESB, RESW and RESD cannot be satisfied with incorrectly
* evaluated operands, since the correct values _must_ be known
* on the first pass. Hence, even in pass one, we set the
* `critical' flag on calling evaluate(), so that it will bomb
* out on undefined symbols. Nasty, but there's nothing we can
* do about it.
*
* For the moment, EQU has the same difficulty, so we'll
* include that.
*/
if (result->opcode == I_RESB ||
result->opcode == I_RESW ||
result->opcode == I_RESD ||
result->opcode == I_RESQ ||
result->opcode == I_REST ||
2002-05-01 05:09:12 +08:00
result->opcode == I_EQU ||
result->opcode == I_INCBIN) /* fbk */
2002-05-01 04:53:55 +08:00
{
2002-05-01 05:09:12 +08:00
critical = pass0;
2002-05-01 04:53:55 +08:00
}
2002-05-01 04:51:32 +08:00
else
critical = (pass==2 ? 2 : 0);
if (result->opcode == I_DB ||
result->opcode == I_DW ||
result->opcode == I_DD ||
result->opcode == I_DQ ||
2002-05-01 04:52:08 +08:00
result->opcode == I_DT ||
2002-05-01 04:53:55 +08:00
result->opcode == I_INCBIN)
{
2002-05-01 04:53:16 +08:00
extop *eop, **tail = &result->eops, **fixptr;
2002-05-01 04:51:32 +08:00
int oper_num = 0;
2002-05-01 04:53:55 +08:00
result->eops_float = FALSE;
2002-05-01 04:51:32 +08:00
/*
2002-05-01 04:53:55 +08:00
* Begin to read the DB/DW/DD/DQ/DT/INCBIN operands.
2002-05-01 04:51:32 +08:00
*/
while (1) {
2002-05-01 04:52:49 +08:00
i = stdscan(NULL, &tokval);
2002-05-01 04:51:32 +08:00
if (i == 0)
break;
2002-05-01 04:53:16 +08:00
fixptr = tail;
2002-05-01 04:51:32 +08:00
eop = *tail = nasm_malloc(sizeof(extop));
tail = &eop->next;
eop->next = NULL;
eop->type = EOT_NOTHING;
oper_num++;
if (i == TOKEN_NUM && tokval.t_charptr && is_comma_next()) {
eop->type = EOT_DB_STRING;
eop->stringval = tokval.t_charptr;
eop->stringlen = tokval.t_inttwo;
2002-05-01 04:52:49 +08:00
i = stdscan(NULL, &tokval); /* eat the comma */
2002-05-01 04:51:32 +08:00
continue;
}
2002-05-01 04:53:55 +08:00
if ((i == TOKEN_FLOAT && is_comma_next()) || i == '-') {
2002-05-01 04:51:32 +08:00
long sign = +1L;
if (i == '-') {
2002-05-01 04:52:49 +08:00
char *save = stdscan_bufptr;
i = stdscan(NULL, &tokval);
2002-05-01 04:51:32 +08:00
sign = -1L;
2002-05-01 04:53:55 +08:00
if (i != TOKEN_FLOAT || !is_comma_next()) {
2002-05-01 04:52:49 +08:00
stdscan_bufptr = save;
i = tokval.t_type = '-';
2002-05-01 04:51:32 +08:00
}
}
if (i == TOKEN_FLOAT) {
eop->type = EOT_DB_STRING;
2002-05-01 04:53:55 +08:00
result->eops_float = TRUE;
2002-05-01 04:51:32 +08:00
if (result->opcode == I_DD)
eop->stringlen = 4;
else if (result->opcode == I_DQ)
eop->stringlen = 8;
else if (result->opcode == I_DT)
2002-05-01 04:53:55 +08:00
eop->stringlen = 10;
2002-05-01 04:51:32 +08:00
else {
error(ERR_NONFATAL, "floating-point constant"
" encountered in `D%c' instruction",
result->opcode == I_DW ? 'W' : 'B');
2002-05-01 04:53:55 +08:00
/*
* fix suggested by Pedro Gimeno... original line
* was:
* eop->type = EOT_NOTHING;
*/
eop->stringlen = 0;
2002-05-01 04:51:32 +08:00
}
2002-05-01 04:52:49 +08:00
eop = nasm_realloc(eop, sizeof(extop)+eop->stringlen);
2002-05-01 04:53:16 +08:00
tail = &eop->next;
*fixptr = eop;
2002-05-01 04:52:49 +08:00
eop->stringval = (char *)eop + sizeof(extop);
2002-05-01 04:53:55 +08:00
if (eop->stringlen < 4 ||
!float_const (tokval.t_charptr, sign,
2002-05-01 04:51:32 +08:00
(unsigned char *)eop->stringval,
eop->stringlen, error))
eop->type = EOT_NOTHING;
2002-05-01 04:52:49 +08:00
i = stdscan(NULL, &tokval); /* eat the comma */
2002-05-01 04:51:32 +08:00
continue;
}
}
2002-05-01 04:53:55 +08:00
/* anything else */
{
2002-05-01 04:51:32 +08:00
expr *value;
2002-05-01 04:52:49 +08:00
value = evaluate (stdscan, NULL, &tokval, NULL,
critical, error, NULL);
i = tokval.t_type;
if (!value) { /* error in evaluator */
2002-05-01 04:51:32 +08:00
result->opcode = -1;/* unrecoverable parse error: */
return result; /* ignore this instruction */
}
2002-05-01 04:52:49 +08:00
if (is_unknown(value)) {
eop->type = EOT_DB_NUMBER;
eop->offset = 0; /* doesn't matter what we put */
eop->segment = eop->wrt = NO_SEG; /* likewise */
} else if (is_reloc(value)) {
2002-05-01 04:51:32 +08:00
eop->type = EOT_DB_NUMBER;
eop->offset = reloc_value(value);
eop->segment = reloc_seg(value);
eop->wrt = reloc_wrt(value);
} else {
error (ERR_NONFATAL,
2002-05-01 04:52:49 +08:00
"operand %d: expression is not simple"
" or relocatable", oper_num);
2002-05-01 04:51:32 +08:00
}
}
2002-05-01 04:52:26 +08:00
/*
2002-05-01 04:52:49 +08:00
* We're about to call stdscan(), which will eat the
2002-05-01 04:52:26 +08:00
* comma that we're currently sitting on between
* arguments. However, we'd better check first that it
* _is_ a comma.
*/
if (i == 0) /* also could be EOL */
break;
if (i != ',') {
2002-05-01 04:52:49 +08:00
error (ERR_NONFATAL, "comma expected after operand %d",
oper_num);
2002-05-01 04:52:26 +08:00
result->opcode = -1;/* unrecoverable parse error: */
return result; /* ignore this instruction */
}
2002-05-01 04:51:32 +08:00
}
2002-05-01 04:52:08 +08:00
if (result->opcode == I_INCBIN) {
/*
* Correct syntax for INCBIN is that there should be
* one string operand, followed by one or two numeric
* operands.
*/
if (!result->eops || result->eops->type != EOT_DB_STRING)
error (ERR_NONFATAL, "`incbin' expects a file name");
else if (result->eops->next &&
result->eops->next->type != EOT_DB_NUMBER)
error (ERR_NONFATAL, "`incbin': second parameter is",
" non-numeric");
else if (result->eops->next && result->eops->next->next &&
result->eops->next->next->type != EOT_DB_NUMBER)
error (ERR_NONFATAL, "`incbin': third parameter is",
" non-numeric");
else if (result->eops->next && result->eops->next->next &&
result->eops->next->next->next)
error (ERR_NONFATAL, "`incbin': more than three parameters");
else
return result;
/*
* If we reach here, one of the above errors happened.
* Throw the instruction away.
*/
result->opcode = -1;
return result;
2002-05-01 04:53:55 +08:00
} else /* DB ... */
if (oper_num == 0)
error (ERR_WARNING|ERR_PASS1,
"no operand for data declaration");
else
result->operands = oper_num;
2002-05-01 04:52:08 +08:00
2002-05-01 04:51:32 +08:00
return result;
}
/* right. Now we begin to parse the operands. There may be up to three
* of these, separated by commas, and terminated by a zero token. */
for (operand = 0; operand < 3; operand++) {
2002-05-01 04:52:49 +08:00
expr *value; /* used most of the time */
2002-05-01 04:51:32 +08:00
int mref; /* is this going to be a memory ref? */
2002-05-01 04:52:26 +08:00
int bracket; /* is it a [] mref, or a & mref? */
2002-05-01 04:53:55 +08:00
int setsize = 0;
2002-05-01 04:51:32 +08:00
result->oprs[operand].addr_size = 0;/* have to zero this whatever */
2002-05-01 04:52:49 +08:00
result->oprs[operand].eaflags = 0; /* and this */
2002-05-01 04:53:55 +08:00
result->oprs[operand].opflags = 0;
2002-05-01 04:52:49 +08:00
i = stdscan(NULL, &tokval);
2002-05-01 04:51:32 +08:00
if (i == 0) break; /* end of operands: get out of here */
result->oprs[operand].type = 0; /* so far, no override */
while (i == TOKEN_SPECIAL) {/* size specifiers */
switch ((int)tokval.t_integer) {
case S_BYTE:
2002-05-01 04:53:55 +08:00
if (!setsize) /* we want to use only the first */
result->oprs[operand].type |= BITS8;
setsize = 1;
2002-05-01 04:51:32 +08:00
break;
case S_WORD:
2002-05-01 04:53:55 +08:00
if (!setsize)
result->oprs[operand].type |= BITS16;
setsize = 1;
2002-05-01 04:51:32 +08:00
break;
case S_DWORD:
case S_LONG:
2002-05-01 04:53:55 +08:00
if (!setsize)
result->oprs[operand].type |= BITS32;
setsize = 1;
2002-05-01 04:51:32 +08:00
break;
case S_QWORD:
2002-05-01 04:53:55 +08:00
if (!setsize)
result->oprs[operand].type |= BITS64;
setsize = 1;
2002-05-01 04:51:32 +08:00
break;
case S_TWORD:
2002-05-01 04:53:55 +08:00
if (!setsize)
result->oprs[operand].type |= BITS80;
setsize = 1;
2002-05-01 04:51:32 +08:00
break;
case S_TO:
result->oprs[operand].type |= TO;
break;
case S_FAR:
result->oprs[operand].type |= FAR;
break;
case S_NEAR:
result->oprs[operand].type |= NEAR;
break;
case S_SHORT:
result->oprs[operand].type |= SHORT;
break;
2002-05-01 04:53:55 +08:00
default:
error (ERR_NONFATAL, "invalid operand size specification");
2002-05-01 04:51:32 +08:00
}
2002-05-01 04:52:49 +08:00
i = stdscan(NULL, &tokval);
2002-05-01 04:51:32 +08:00
}
2002-05-01 04:52:26 +08:00
if (i == '[' || i == '&') { /* memory reference */
2002-05-01 04:51:32 +08:00
mref = TRUE;
2002-05-01 04:52:26 +08:00
bracket = (i == '[');
2002-05-01 05:00:33 +08:00
i = stdscan(NULL, &tokval);
2002-05-01 04:51:32 +08:00
if (i == TOKEN_SPECIAL) { /* check for address size override */
2002-05-01 05:00:33 +08:00
if (tasm_compatible_mode) {
switch ((int)tokval.t_integer) {
/* For TASM compatibility a size override inside the
* brackets changes the size of the operand, not the
* address type of the operand as it does in standard
* NASM syntax. Hence:
*
* mov eax,[DWORD val]
*
* is valid syntax in TASM compatibility mode. Note that
* you lose the ability to override the default address
* type for the instruction, but we never use anything
* but 32-bit flat model addressing in our code.
*/
case S_BYTE:
result->oprs[operand].type |= BITS8;
break;
case S_WORD:
result->oprs[operand].type |= BITS16;
break;
case S_DWORD:
case S_LONG:
result->oprs[operand].type |= BITS32;
break;
case S_QWORD:
result->oprs[operand].type |= BITS64;
break;
case S_TWORD:
result->oprs[operand].type |= BITS80;
break;
default:
error (ERR_NONFATAL, "invalid operand size specification");
}
} else {
/* Standard NASM compatible syntax */
switch ((int)tokval.t_integer) {
case S_NOSPLIT:
result->oprs[operand].eaflags |= EAF_TIMESTWO;
break;
case S_BYTE:
result->oprs[operand].eaflags |= EAF_BYTEOFFS;
break;
case S_WORD:
result->oprs[operand].addr_size = 16;
result->oprs[operand].eaflags |= EAF_WORDOFFS;
break;
case S_DWORD:
case S_LONG:
result->oprs[operand].addr_size = 32;
result->oprs[operand].eaflags |= EAF_WORDOFFS;
break;
default:
error (ERR_NONFATAL, "invalid size specification in"
" effective address");
}
2002-05-01 04:51:32 +08:00
}
2002-05-01 04:52:49 +08:00
i = stdscan(NULL, &tokval);
2002-05-01 04:51:32 +08:00
}
2002-05-01 04:52:26 +08:00
} else { /* immediate operand, or register */
2002-05-01 04:51:32 +08:00
mref = FALSE;
2002-05-01 04:52:26 +08:00
bracket = FALSE; /* placate optimisers */
}
2002-05-01 04:51:32 +08:00
2002-05-01 04:52:49 +08:00
value = evaluate (stdscan, NULL, &tokval,
2002-05-01 04:53:55 +08:00
&result->oprs[operand].opflags,
critical, error, &hints);
2002-05-01 04:52:49 +08:00
i = tokval.t_type;
2002-05-01 04:53:55 +08:00
if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
result->forw_ref = TRUE;
}
2002-05-01 04:51:32 +08:00
if (!value) { /* error in evaluator */
result->opcode = -1; /* unrecoverable parse error: */
return result; /* ignore this instruction */
}
if (i == ':' && mref) { /* it was seg:offset */
2002-05-01 04:52:49 +08:00
/*
* Process the segment override.
*/
if (value[1].type!=0 || value->value!=1 ||
REG_SREG & ~reg_flags[value->type])
error (ERR_NONFATAL, "invalid segment override");
else if (result->nprefix == MAXPREFIX)
error (ERR_NONFATAL,
"instruction has more than %d prefixes",
MAXPREFIX);
else
result->prefixes[result->nprefix++] = value->type;
i = stdscan(NULL, &tokval); /* then skip the colon */
2002-05-01 04:51:32 +08:00
if (i == TOKEN_SPECIAL) { /* another check for size override */
switch ((int)tokval.t_integer) {
case S_WORD:
result->oprs[operand].addr_size = 16;
break;
case S_DWORD:
case S_LONG:
result->oprs[operand].addr_size = 32;
break;
default:
error (ERR_NONFATAL, "invalid size specification in"
" effective address");
}
2002-05-01 04:52:49 +08:00
i = stdscan(NULL, &tokval);
2002-05-01 04:51:32 +08:00
}
2002-05-01 04:52:49 +08:00
value = evaluate (stdscan, NULL, &tokval,
2002-05-01 04:53:55 +08:00
&result->oprs[operand].opflags,
critical, error, &hints);
2002-05-01 04:52:49 +08:00
i = tokval.t_type;
2002-05-01 04:53:55 +08:00
if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
result->forw_ref = TRUE;
}
2002-05-01 04:51:32 +08:00
/* and get the offset */
if (!value) { /* but, error in evaluator */
result->opcode = -1; /* unrecoverable parse error: */
return result; /* ignore this instruction */
}
2002-05-01 04:52:49 +08:00
}
2002-05-01 04:52:26 +08:00
if (mref && bracket) { /* find ] at the end */
2002-05-01 04:51:32 +08:00
if (i != ']') {
error (ERR_NONFATAL, "parser: expecting ]");
do { /* error recovery again */
2002-05-01 04:52:49 +08:00
i = stdscan(NULL, &tokval);
2002-05-01 04:51:32 +08:00
} while (i != 0 && i != ',');
} else /* we got the required ] */
2002-05-01 04:52:49 +08:00
i = stdscan(NULL, &tokval);
2002-05-01 04:51:32 +08:00
} else { /* immediate operand */
if (i != 0 && i != ',' && i != ':') {
error (ERR_NONFATAL, "comma or end of line expected");
do { /* error recovery */
2002-05-01 04:52:49 +08:00
i = stdscan(NULL, &tokval);
2002-05-01 04:51:32 +08:00
} while (i != 0 && i != ',');
} else if (i == ':') {
result->oprs[operand].type |= COLON;
}
}
/* now convert the exprs returned from evaluate() into operand
* descriptions... */
if (mref) { /* it's a memory reference */
expr *e = value;
int b, i, s; /* basereg, indexreg, scale */
long o; /* offset */
b = i = -1, o = s = 0;
2002-05-01 04:52:49 +08:00
result->oprs[operand].hintbase = hints.base;
result->oprs[operand].hinttype = hints.type;
2002-05-01 04:51:32 +08:00
2002-05-01 04:52:49 +08:00
if (e->type <= EXPR_REG_END) { /* this bit's a register */
2002-05-01 04:51:32 +08:00
if (e->value == 1) /* in fact it can be basereg */
b = e->type;
else /* no, it has to be indexreg */
i = e->type, s = e->value;
e++;
}
2002-05-01 04:53:55 +08:00
if (e->type && e->type <= EXPR_REG_END) /* it's a 2nd register */
{
if (b != -1) /* If the first was the base, ... */
i = e->type, s = e->value; /* second has to be indexreg */
else if (e->value != 1) /* If both want to be index */
{
2002-05-01 05:01:08 +08:00
error(ERR_NONFATAL, "beroset-p-592-invalid effective address");
2002-05-01 04:53:55 +08:00
result->opcode = -1;
return result;
}
else
b = e->type;
2002-05-01 04:51:32 +08:00
e++;
}
if (e->type != 0) { /* is there an offset? */
2002-05-01 04:53:55 +08:00
if (e->type <= EXPR_REG_END) /* in fact, is there an error? */
{
2002-05-01 05:01:08 +08:00
error (ERR_NONFATAL, "beroset-p-603-invalid effective address");
2002-05-01 04:51:32 +08:00
result->opcode = -1;
return result;
2002-05-01 04:53:55 +08:00
}
else
{
2002-05-01 04:52:49 +08:00
if (e->type == EXPR_UNKNOWN) {
2002-05-01 04:53:55 +08:00
o = 0; /* doesn't matter what */
result->oprs[operand].wrt = NO_SEG; /* nor this */
2002-05-01 04:52:49 +08:00
result->oprs[operand].segment = NO_SEG; /* or this */
while (e->type) e++; /* go to the end of the line */
2002-05-01 04:53:55 +08:00
}
else
{
2002-05-01 04:52:49 +08:00
if (e->type == EXPR_SIMPLE) {
o = e->value;
e++;
}
if (e->type == EXPR_WRT) {
result->oprs[operand].wrt = e->value;
e++;
} else
result->oprs[operand].wrt = NO_SEG;
/*
* Look for a segment base type.
*/
if (e->type && e->type < EXPR_SEGBASE) {
2002-05-01 05:01:08 +08:00
error (ERR_NONFATAL, "beroset-p-630-invalid effective address");
2002-05-01 04:52:49 +08:00
result->opcode = -1;
return result;
}
while (e->type && e->value == 0)
e++;
if (e->type && e->value != 1) {
2002-05-01 05:01:08 +08:00
error (ERR_NONFATAL, "beroset-p-637-invalid effective address");
2002-05-01 04:52:49 +08:00
result->opcode = -1;
return result;
}
if (e->type) {
result->oprs[operand].segment =
e->type - EXPR_SEGBASE;
e++;
} else
result->oprs[operand].segment = NO_SEG;
while (e->type && e->value == 0)
e++;
if (e->type) {
2002-05-01 05:01:08 +08:00
error (ERR_NONFATAL, "beroset-p-650-invalid effective address");
2002-05-01 04:52:49 +08:00
result->opcode = -1;
return result;
}
2002-05-01 04:51:53 +08:00
}
2002-05-01 04:51:32 +08:00
}
} else {
o = 0;
result->oprs[operand].wrt = NO_SEG;
result->oprs[operand].segment = NO_SEG;
}
if (e->type != 0) { /* there'd better be nothing left! */
2002-05-01 05:01:08 +08:00
error (ERR_NONFATAL, "beroset-p-663-invalid effective address");
2002-05-01 04:51:32 +08:00
result->opcode = -1;
return result;
}
result->oprs[operand].type |= MEMORY;
if (b==-1 && (i==-1 || s==0))
result->oprs[operand].type |= MEM_OFFS;
result->oprs[operand].basereg = b;
result->oprs[operand].indexreg = i;
result->oprs[operand].scale = s;
result->oprs[operand].offset = o;
2002-05-01 04:53:55 +08:00
}
else /* it's not a memory reference */
{
2002-05-01 04:52:49 +08:00
if (is_just_unknown(value)) { /* it's immediate but unknown */
result->oprs[operand].type |= IMMEDIATE;
result->oprs[operand].offset = 0; /* don't care */
result->oprs[operand].segment = NO_SEG; /* don't care again */
result->oprs[operand].wrt = NO_SEG;/* still don't care */
2002-05-01 04:53:55 +08:00
}
else if (is_reloc(value)) /* it's immediate */
{
2002-05-01 04:51:32 +08:00
result->oprs[operand].type |= IMMEDIATE;
result->oprs[operand].offset = reloc_value(value);
result->oprs[operand].segment = reloc_seg(value);
result->oprs[operand].wrt = reloc_wrt(value);
2002-05-01 04:59:21 +08:00
if (is_simple(value)) {
if (reloc_value(value)==1)
result->oprs[operand].type |= UNITY;
2002-05-01 05:05:55 +08:00
if (optimizing>=0) {
2002-05-01 05:01:38 +08:00
if (reloc_value(value) >= -128 &&
reloc_value(value) <= 127)
result->oprs[operand].type |= SBYTE;
}
2002-05-01 04:59:21 +08:00
}
2002-05-01 04:53:55 +08:00
}
else /* it's a register */
{
int i;
2002-05-01 04:51:32 +08:00
if (value->type>=EXPR_SIMPLE || value->value!=1) {
error (ERR_NONFATAL, "invalid operand type");
result->opcode = -1;
return result;
}
2002-05-01 04:53:55 +08:00
/*
* check that its only 1 register, not an expression...
*/
for (i = 1; value[i].type; i++)
if (value[i].value) {
error (ERR_NONFATAL, "invalid operand type");
result->opcode = -1;
return result;
}
2002-05-01 04:51:32 +08:00
/* clear overrides, except TO which applies to FPU regs */
2002-05-01 04:53:55 +08:00
if (result->oprs[operand].type & ~TO) {
/*
* we want to produce a warning iff the specified size
* is different from the register size
*/
i = result->oprs[operand].type & SIZE_MASK;
}
else
i = 0;
2002-05-01 04:51:32 +08:00
result->oprs[operand].type &= TO;
result->oprs[operand].type |= REGISTER;
result->oprs[operand].type |= reg_flags[value->type];
result->oprs[operand].basereg = value->type;
2002-05-01 04:53:55 +08:00
if (i && (result->oprs[operand].type & SIZE_MASK) != i)
error (ERR_WARNING|ERR_PASS1,
"register size specification ignored");
2002-05-01 04:51:32 +08:00
}
}
}
result->operands = operand; /* set operand count */
while (operand<3) /* clear remaining operands */
result->oprs[operand++].type = 0;
/*
* Transform RESW, RESD, RESQ, REST into RESB.
*/
switch (result->opcode) {
case I_RESW: result->opcode=I_RESB; result->oprs[0].offset*=2; break;
case I_RESD: result->opcode=I_RESB; result->oprs[0].offset*=4; break;
case I_RESQ: result->opcode=I_RESB; result->oprs[0].offset*=8; break;
case I_REST: result->opcode=I_RESB; result->oprs[0].offset*=10; break;
}
return result;
}
2002-05-01 04:53:55 +08:00
static int is_comma_next (void)
{
2002-05-01 04:51:32 +08:00
char *p;
2002-05-01 04:52:49 +08:00
int i;
struct tokenval tv;
2002-05-01 04:51:32 +08:00
2002-05-01 04:52:49 +08:00
p = stdscan_bufptr;
i = stdscan (NULL, &tv);
stdscan_bufptr = p;
return (i == ',' || i == ';' || !i);
2002-05-01 04:51:32 +08:00
}
2002-05-01 04:53:55 +08:00
void cleanup_insn (insn *i)
{
2002-05-01 04:51:32 +08:00
extop *e;
while (i->eops) {
e = i->eops;
i->eops = i->eops->next;
nasm_free (e);
}
}