⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 parser.c

📁 汇编编译器的最新版本的源码.买了自己动手写操作系统这本书的人一定要下
💻 C
📖 第 1 页 / 共 3 页
字号:
/* 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 license given in the file "LICENSE"
 * distributed in the NASM archive.
 *
 * initial version 27/iii/95 by Simon Tatham
 */

#include "compiler.h"

#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <ctype.h>
#include <inttypes.h>

#include "nasm.h"
#include "insns.h"
#include "nasmlib.h"
#include "stdscan.h"
#include "parser.h"
#include "float.h"
#include "tables.h"

extern int in_abs_seg;          /* ABSOLUTE segment flag */
extern int32_t abs_seg;            /* ABSOLUTE segment */
extern int32_t abs_offset;         /* ABSOLUTE segment offset */

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 struct location *location;         /* Pointer to current line's segment,offset */

void parser_global_info(struct ofmt *output, struct location * locp)
{
    outfmt = output;
    location = locp;
}

static int prefix_slot(enum prefixes prefix)
{
    switch (prefix) {
    case R_CS:
    case R_DS:
    case R_SS:
    case R_ES:
    case R_FS:
    case R_GS:
	return PPS_SEG;
    case P_LOCK:
    case P_REP:
    case P_REPE:
    case P_REPZ:
    case P_REPNE:
    case P_REPNZ:
	return PPS_LREP;
    case P_O16:
    case P_O32:
    case P_O64:
    case P_OSP:
	return PPS_OSIZE;
    case P_A16:
    case P_A32:
    case P_A64:
    case P_ASP:
	return PPS_ASIZE;
    default:
	error(ERR_PANIC, "Invalid value %d passed to prefix_slot()", prefix);
	return -1;
    }
}

static void process_size_override(insn * result, int operand)
{
    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;
	case S_OWORD:
	    result->oprs[operand].type |= BITS128;
	    break;
	default:
	    error(ERR_NONFATAL,
		  "invalid operand size specification");
	    break;
	}
    } else {
	/* Standard NASM compatible syntax */
	switch ((int)tokval.t_integer) {
	case S_NOSPLIT:
	    result->oprs[operand].eaflags |= EAF_TIMESTWO;
	    break;
	case S_REL:
	    result->oprs[operand].eaflags |= EAF_REL;
	    break;
	case S_ABS:
	    result->oprs[operand].eaflags |= EAF_ABS;
	    break;
	case S_BYTE:
	    result->oprs[operand].disp_size = 8;
	    result->oprs[operand].eaflags |= EAF_BYTEOFFS;
	    break;
	case P_A16:
	case P_A32:
	case P_A64:
	    if (result->prefixes[PPS_ASIZE] &&
		result->prefixes[PPS_ASIZE] != tokval.t_integer)
		error(ERR_NONFATAL,
		      "conflicting address size specifications");
	    else
		result->prefixes[PPS_ASIZE] = tokval.t_integer;
	    break;
	case S_WORD:
	    result->oprs[operand].disp_size = 16;
	    result->oprs[operand].eaflags |= EAF_WORDOFFS;
	    break;
	case S_DWORD:
	case S_LONG:
	    result->oprs[operand].disp_size = 32;
	    result->oprs[operand].eaflags |= EAF_WORDOFFS;
	    break;
	case S_QWORD:
	    result->oprs[operand].disp_size = 64;
	    result->oprs[operand].eaflags |= EAF_WORDOFFS;
	    break;
	default:
	    error(ERR_NONFATAL, "invalid size specification in"
		  " effective address");
	    break;
	}
    }
}

insn *parse_line(int pass, char *buffer, insn * result,
                 efunc errfunc, evalfunc evaluate, ldfunc ldef)
{
    int operand;
    int critical;
    struct eval_hints hints;
    int j;
    bool first;
    bool insn_is_label = false;

restart_parse:
    first = true;
    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 initialize 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 & ~nasm_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 || (insn_is_label && i == TOKEN_INSN)) {
        /* there's a label here */
	first = false;
        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;
    }

    for (j = 0; j < MAXPREFIX; j++)
	result->prefixes[j] = P_none;
    result->times = 1L;

    while (i == TOKEN_PREFIX ||
           (i == TOKEN_REG && !(REG_SREG & ~nasm_reg_flags[tokval.t_integer])))
    {
	first = false;

        /*
         * 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 && pass0 == 2) {
                    error(ERR_NONFATAL, "TIMES value %d is negative",
                          value->value);
                    result->times = 0;
                }
            }
        } else {
	    int slot = prefix_slot(tokval.t_integer);
	    if (result->prefixes[slot]) {
               if (result->prefixes[slot] == tokval.t_integer)
		    error(ERR_WARNING,
		      "instruction has redundant prefixes");
               else
		    error(ERR_NONFATAL,
		      "instruction has conflicting prefixes");
	    }
	    result->prefixes[slot] = tokval.t_integer;
            i = stdscan(NULL, &tokval);
        }
    }

    if (i != TOKEN_INSN) {
	int j;
	enum prefixes pfx;

	for (j = 0; j < MAXPREFIX; j++)
	    if ((pfx = result->prefixes[j]) != P_none)
		break;

        if (i == 0 && pfx != P_none) {
            /*
             * 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;

    /*
     * INCBIN 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.
     */
    if (result->opcode == I_INCBIN) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -