📄 parser.c
字号:
/* 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 "insns.h"
#include "nasmlib.h"
#include "parser.h"
#include "float.h"
extern int in_abs_seg; /* ABSOLUTE segment flag */
extern long abs_seg; /* ABSOLUTE segment */
extern long abs_offset; /* ABSOLUTE segment offset */
#include "regflags.c" /* List of register flags */
enum { /* special tokens */
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; /* 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;
}
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; /* Assume no label */
result->eops = NULL; /* must do this, whatever happens */
result->operands = 0; /* must initialise this */
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 */
result->label = tokval.t_charptr;
i = stdscan(NULL, &tokval);
if (i == ':') { /* skip over the optional colon */
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) {
/*
* 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, in_abs_seg ? abs_seg : location->segment,
location->offset, NULL, TRUE, FALSE, outfmt, errfunc);
}
}
if (i == 0) {
result->opcode = -1; /* this line contains just a label */
return result;
}
result->nprefix = 0;
result->times = 1L;
while (i == TOKEN_PREFIX ||
(i == TOKEN_REG && !(REG_SREG & ~reg_flags[tokval.t_integer])))
{
/*
* Handle special case: the TIMES prefix.
*/
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) { /* 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");
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) {
/*
* 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;
}
}
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 || result->opcode == I_EQU || result->opcode == I_INCBIN) { /* fbk */
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;
/*
* Begin to read the DB/DW/DD/DQ/DT/INCBIN operands.
*/
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); /* eat the comma */
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');
/*
* fix suggested by Pedro Gimeno... original line
* was:
* eop->type = EOT_NOTHING;
*/
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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -