#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <ctype.h>
#include "nasm.h"
#include "insns.h"
#include "nasmlib.h"
#include "parser.h"
#include "float.h"
extern int in_abs_seg;
extern long abs_seg;
extern long abs_offset;
#include "regflags.c"
enum {
S_BYTE, S_DWORD, S_FAR, S_LONG, S_NEAR, S_NOSPLIT, S_QWORD,
S_SHORT, S_STRICT, S_TO, S_TWORD, S_WORD
};
static int is_comma_next(void);
static int i;
static struct tokenval tokval;
static efunc error;
static struct ofmt *outfmt;
static loc_t *location;
void parser_global_info(struct ofmt *output, loc_t * locp)
{
outfmt = output;
location = locp;
}
insn *parse_line(int pass, char *buffer, insn * result,
efunc errfunc, evalfunc evaluate, ldfunc ldef)
{
int operand;
int critical;
struct eval_hints hints;
result->forw_ref = FALSE;
error = errfunc;
stdscan_reset();
stdscan_bufptr = buffer;
i = stdscan(NULL, &tokval);
result->label = NULL;
result->eops = NULL;
result->operands = 0;
if (i == 0) {
result->opcode = -1;
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) {
result->label = tokval.t_charptr;
i = stdscan(NULL, &tokval);
if (i == ':') {
i = stdscan(NULL, &tokval);
} else if (i == 0) {
error(ERR_WARNING | ERR_WARN_OL | ERR_PASS1,
"label alone on a line without a colon might be in error");
}
if (i != TOKEN_INSN || tokval.t_integer != I_EQU) {
ldef(result->label, in_abs_seg ? abs_seg : location->segment,
location->offset, NULL, TRUE, FALSE, outfmt, errfunc);
}
}
if (i == 0) {
result->opcode = -1;
return result;
}
result->nprefix = 0;
result->times = 1L;
while (i == TOKEN_PREFIX ||
(i == TOKEN_REG && !(REG_SREG & ~reg_flags[tokval.t_integer])))
{
if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
expr *value;
i = stdscan(NULL, &tokval);
value =
evaluate(stdscan, NULL, &tokval, NULL, pass0, error, NULL);
i = tokval.t_type;
if (!value) {
result->opcode = -1;
return result;
}
if (!is_simple(value)) {
error(ERR_NONFATAL,
"non-constant argument supplied to TIMES");
result->times = 1L;
} else {
result->times = value->value;
if (value->value < 0) {
error(ERR_NONFATAL, "TIMES value %d is negative",
value->value);
result->times = 0;
}
}
} else {
if (result->nprefix == MAXPREFIX)
error(ERR_NONFATAL,
"instruction has more than %d prefixes", MAXPREFIX);
else
result->prefixes[result->nprefix++] = tokval.t_integer;
i = stdscan(NULL, &tokval);
}
}
if (i != TOKEN_INSN) {
if (result->nprefix > 0 && i == 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;
}
}
result->opcode = tokval.t_integer;
result->condition = tokval.t_inttwo;
if (result->opcode == I_RESB || result->opcode == I_RESW || result->opcode == I_RESD || result->opcode == I_RESQ || result->opcode == I_REST || result->opcode == I_EQU || result->opcode == I_INCBIN) {
critical = pass0;
} else
critical = (pass == 2 ? 2 : 0);
if (result->opcode == I_DB ||
result->opcode == I_DW ||
result->opcode == I_DD ||
result->opcode == I_DQ ||
result->opcode == I_DT || result->opcode == I_INCBIN) {
extop *eop, **tail = &result->eops, **fixptr;
int oper_num = 0;
result->eops_float = FALSE;
while (1) {
i = stdscan(NULL, &tokval);
if (i == 0)
break;
fixptr = tail;
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;
i = stdscan(NULL, &tokval);
continue;
}
if ((i == TOKEN_FLOAT && is_comma_next()) || i == '-') {
long sign = +1L;
if (i == '-') {
char *save = stdscan_bufptr;
i = stdscan(NULL, &tokval);
sign = -1L;
if (i != TOKEN_FLOAT || !is_comma_next()) {
stdscan_bufptr = save;
i = tokval.t_type = '-';
}
}
if (i == TOKEN_FLOAT) {
eop->type = EOT_DB_STRING;
result->eops_float = TRUE;
if (result->opcode == I_DD)
eop->stringlen = 4;
else if (result->opcode == I_DQ)
eop->stringlen = 8;
else if (result->opcode == I_DT)
eop->stringlen = 10;
else {
error(ERR_NONFATAL, "floating-point constant"
" encountered in `D%c' instruction",
result->opcode == I_DW ? 'W' : 'B');
eop->stringlen = 0;
}
eop =
nasm_realloc(eop, sizeof(extop) + eop->stringlen);
tail = &eop->next;
*fixptr = eop;
eop->stringval = (char *)eop + sizeof(extop);
if (eop->stringlen < 4 ||
!float_const(tokval.t_charptr, sign,
(unsigned char *)eop->stringval,
eop->stringlen, error))
eop->type = EOT_NOTHING;
i = stdscan(NULL, &tokval);
continue;
}
}
{
expr *value;
value = evaluate(stdscan, NULL, &tokval, NULL,
critical, error, NULL);
i = tokval.t_type;
if (!value) {
result->opcode = -1;
return result;
}
if (is_unknown(value)) {
eop->type = EOT_DB_NUMBER;
eop->offset = 0;
eop->segment = eop->wrt = NO_SEG;
} else if (is_reloc(value)) {
eop->type = EOT_DB_NUMBER;
eop->offset = reloc_value(value);
eop->segment = reloc_seg(value);
eop->wrt = reloc_wrt(value);
} else {
error(ERR_NONFATAL,
"operand %d: expression is not simple"
" or relocatable", oper_num);
}
}
if (i == 0)
break;
if (i != ',') {
error(ERR_NONFATAL, "comma expected after operand %d",
oper_num);
result->opcode = -1;
return result;
}
}
if (result->opcode == I_INCBIN) {
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;
result->opcode = -1;
return result;
} else if (oper_num == 0)
error(ERR_WARNING | ERR_PASS1,
"no operand for data declaration");
else
result->operands = oper_num;
return result;
}
for (operand = 0; operand < 3; operand++) {
expr *value;
int mref;
int bracket;
int setsize = 0;
result->oprs[operand].addr_size = 0;
result->oprs[operand].eaflags = 0;
result->oprs[operand].opflags = 0;
i = stdscan(NULL, &tokval);
if (i == 0)
break;
result->oprs[operand].type = 0;
while (i == TOKEN_SPECIAL) {
switch ((int)tokval.t_integer) {
case S_BYTE:
if (!setsize)
result->oprs[operand].type |= BITS8;
setsize = 1;
break;
case S_WORD:
if (!setsize)
result->oprs[operand].type |= BITS16;
setsize = 1;
break;
case S_DWORD:
case S_LONG:
if (!setsize)
result->oprs[operand].type |= BITS32;
setsize = 1;
break;
case S_QWORD:
if (!setsize)
result->oprs[operand].type |= BITS64;
setsize = 1;
break;
case S_TWORD:
if (!setsize)
result->oprs[operand].type |= BITS80;
setsize = 1;
break;
case S_TO:
result->oprs[operand].type |= TO;
break;
case S_STRICT:
result->oprs[operand].type |= STRICT;
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;
default:
error(ERR_NONFATAL, "invalid operand size specification");
}
i = stdscan(NULL, &tokval);
}
if (i == '[' || i == '&') {
mref = TRUE;
bracket = (i == '[');
i = stdscan(NULL, &tokval);
if (i == TOKEN_SPECIAL) {
if (tasm_compatible_mode) {
switch ((int)tokval.t_integer) {
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 {
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");
}
}
i = stdscan(NULL, &tokval);
}
} else {
mref = FALSE;
bracket = FALSE;
}
if ((result->oprs[operand].type & FAR) && !mref &&
result->opcode != I_JMP && result->opcode != I_CALL) {
error(ERR_NONFATAL, "invalid use of FAR operand specifier");
}
value = evaluate(stdscan, NULL, &tokval,
&result->oprs[operand].opflags,
critical, error, &hints);
i = tokval.t_type;
if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
result->forw_ref = TRUE;
}
if (!value) {
result->opcode = -1;
return result;
}
if (i == ':' && mref) {
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);
if (i == TOKEN_SPECIAL) {
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");
}
i = stdscan(NULL, &tokval);
}
value = evaluate(stdscan, NULL, &tokval,
&result->oprs[operand].opflags,
critical, error, &hints);
i = tokval.t_type;
if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
result->forw_ref = TRUE;
}
if (!value) {
result->opcode = -1;
return result;
}
}
if (mref && bracket) {
if (i != ']') {
error(ERR_NONFATAL, "parser: expecting ]");
do {
i = stdscan(NULL, &tokval);
} while (i != 0 && i != ',');
} else
i = stdscan(NULL, &tokval);
} else {
if (i != 0 && i != ',' && i != ':') {
error(ERR_NONFATAL, "comma or end of line expected");
do {
i = stdscan(NULL, &tokval);
} while (i != 0 && i != ',');
} else if (i == ':') {
result->oprs[operand].type |= COLON;
}
}
if (mref) {
expr *e = value;
int b, i, s;
long o;
b = i = -1, o = s = 0;
result->oprs[operand].hintbase = hints.base;
result->oprs[operand].hinttype = hints.type;
if (e->type && e->type <= EXPR_REG_END) {
if (e->value == 1)
b = e->type;
else
i = e->type, s = e->value;
e++;
}
if (e->type && e->type <= EXPR_REG_END) {
if (b != -1)
i = e->type, s = e->value;
else if (e->value != 1) {
error(ERR_NONFATAL,
"beroset-p-592-invalid effective address");
result->opcode = -1;
return result;
} else
b = e->type;
e++;
}
if (e->type != 0) {
if (e->type <= EXPR_REG_END) {
error(ERR_NONFATAL,
"beroset-p-603-invalid effective address");
result->opcode = -1;
return result;
} else {
if (e->type == EXPR_UNKNOWN) {
o = 0;
result->oprs[operand].wrt = NO_SEG;
result->oprs[operand].segment = NO_SEG;
while (e->type)
e++;
} else {
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;
if (e->type && e->type < EXPR_SEGBASE) {
error(ERR_NONFATAL,
"beroset-p-630-invalid effective address");
result->opcode = -1;
return result;
}
while (e->type && e->value == 0)
e++;
if (e->type && e->value != 1) {
error(ERR_NONFATAL,
"beroset-p-637-invalid effective address");
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) {
error(ERR_NONFATAL,
"beroset-p-650-invalid effective address");
result->opcode = -1;
return result;
}
}
}
} else {
o = 0;
result->oprs[operand].wrt = NO_SEG;
result->oprs[operand].segment = NO_SEG;
}
if (e->type != 0) {
error(ERR_NONFATAL,
"beroset-p-663-invalid effective address");
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;
} else {
if (is_just_unknown(value)) {
result->oprs[operand].type |= IMMEDIATE;
result->oprs[operand].offset = 0;
result->oprs[operand].segment = NO_SEG;
result->oprs[operand].wrt = NO_SEG;
} else if (is_reloc(value)) {
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);
if (is_simple(value)) {
if (reloc_value(value) == 1)
result->oprs[operand].type |= UNITY;
if (optimizing >= 0 &&
!(result->oprs[operand].type & STRICT)) {
if (reloc_value(value) >= -128 &&
reloc_value(value) <= 127)
result->oprs[operand].type |= SBYTE;
}
}
} else {
if (value->type >= EXPR_SIMPLE || value->value != 1) {
error(ERR_NONFATAL, "invalid operand type");
result->opcode = -1;
return result;
}
for (i = 1; value[i].type; i++)
if (value[i].value) {
error(ERR_NONFATAL, "invalid operand type");
result->opcode = -1;
return result;
}
if (result->oprs[operand].type & ~TO) {
i = result->oprs[operand].type & SIZE_MASK;
} else
i = 0;
result->oprs[operand].type &= TO;
result->oprs[operand].type |= REGISTER;
result->oprs[operand].type |= reg_flags[value->type];
result->oprs[operand].basereg = value->type;
if (i && (result->oprs[operand].type & SIZE_MASK) != i)
error(ERR_WARNING | ERR_PASS1,
"register size specification ignored");
}
}
}
result->operands = operand;
while (operand < 3)
result->oprs[operand++].type = 0;
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;
}
static int is_comma_next(void)
{
char *p;
int i;
struct tokenval tv;
p = stdscan_bufptr;
i = stdscan(NULL, &tv);
stdscan_bufptr = p;
return (i == ',' || i == ';' || !i);
}
void cleanup_insn(insn * i)
{
extop *e;
while (i->eops) {
e = i->eops;
i->eops = i->eops->next;
nasm_free(e);
}
}