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

📄 parser.c

📁 汇编编译器的最新版本的源码.买了自己动手写操作系统这本书的人一定要下
💻 C
📖 第 1 页 / 共 3 页
字号:
                         &result->oprs[operand].opflags,
                         critical, error, &hints);
        i = tokval.t_type;
        if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
            result->forw_ref = true;
        }
        if (!value) {           /* error in evaluator */
            result->opcode = -1;        /* unrecoverable parse error: */
            return result;      /* ignore this instruction */
        }
        if (i == ':' && mref) { /* it was seg:offset */
            /*
             * Process the segment override.
             */
            if (value[1].type != 0 || value->value != 1 ||
                REG_SREG & ~nasm_reg_flags[value->type])
                error(ERR_NONFATAL, "invalid segment override");
            else if (result->prefixes[PPS_SEG])
                error(ERR_NONFATAL,
                      "instruction has conflicting segment overrides");
            else {
		result->prefixes[PPS_SEG] = value->type;
		if (!(REG_FSGS & ~nasm_reg_flags[value->type]))
		    result->oprs[operand].eaflags |= EAF_FSGS;
	    }

            i = stdscan(NULL, &tokval); /* then skip the colon */
            while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
		process_size_override(result, operand);
                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;
            }
            /* and get the offset */
            if (!value) {       /* but, error in evaluator */
                result->opcode = -1;    /* unrecoverable parse error: */
                return result;  /* ignore this instruction */
            }
        }
        if (mref && bracket) {  /* find ] at the end */
            if (i != ']') {
                error(ERR_NONFATAL, "parser: expecting ]");
                do {            /* error recovery again */
                    i = stdscan(NULL, &tokval);
                } while (i != 0 && i != ',');
            } else              /* we got the required ] */
                i = stdscan(NULL, &tokval);
        } else {                /* immediate operand */
            if (i != 0 && i != ',' && i != ':') {
                error(ERR_NONFATAL, "comma or end of line expected");
                do {            /* error recovery */
                    i = stdscan(NULL, &tokval);
                } 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 */
            int64_t o;             /* offset */

            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) {   /* this bit's a register */
                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++;
            }
            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 */
                    error(ERR_NONFATAL,
                          "beroset-p-592-invalid effective address");
                    result->opcode = -1;
                    return result;
                } else
                    b = e->type;
                e++;
            }
            if (e->type != 0) { /* is there an offset? */
                if (e->type <= EXPR_REG_END) {  /* in fact, is there an error? */
                    error(ERR_NONFATAL,
                          "beroset-p-603-invalid effective address");
                    result->opcode = -1;
                    return result;
                } else {
                    if (e->type == EXPR_UNKNOWN) {
                        o = 0;  /* doesn't matter what */
                        result->oprs[operand].wrt = NO_SEG;     /* nor this */
                        result->oprs[operand].segment = NO_SEG; /* or this */
                        while (e->type)
                            e++;        /* go to the end of the line */
                    } 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;
                        /*
                         * Look for a segment base type.
                         */
                        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) { /* there'd better be nothing left! */
                error(ERR_NONFATAL,
                      "beroset-p-663-invalid effective address");
                result->opcode = -1;
                return result;
            }

	    /* It is memory, but it can match any r/m operand */
            result->oprs[operand].type |= MEMORY_ANY;

	    if (b == -1 && (i == -1 || s == 0)) {
		int is_rel = globalbits == 64 &&
		    !(result->oprs[operand].eaflags & EAF_ABS) &&
		    ((globalrel &&
		      !(result->oprs[operand].eaflags & EAF_FSGS)) ||
		     (result->oprs[operand].eaflags & EAF_REL));

		result->oprs[operand].type |= is_rel ? IP_REL : MEM_OFFS;
	    }
            result->oprs[operand].basereg = b;
            result->oprs[operand].indexreg = i;
            result->oprs[operand].scale = s;
            result->oprs[operand].offset = o;
        } else {                /* it's not a memory reference */

            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 */
            } else if (is_reloc(value)) {       /* it's immediate */
                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)) {
			int64_t v64 = reloc_value(value);
			int32_t v32 = (int32_t)v64;
			int16_t v16 = (int16_t)v32;

			if (v64 >= -128 && v64 <= 127)
                            result->oprs[operand].type |= SBYTE64;
			if (v32 >= -128 && v32 <= 127)
                            result->oprs[operand].type |= SBYTE32;
			if (v16 >= -128 && v16 <= 127)
                            result->oprs[operand].type |= SBYTE16;
                    }
                }
            } else {            /* it's a register */
		unsigned int rs;

                if (value->type >= EXPR_SIMPLE || value->value != 1) {
                    error(ERR_NONFATAL, "invalid operand type");
                    result->opcode = -1;
                    return result;
                }

                /*
                 * 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;
                    }

                /* clear overrides, except TO which applies to FPU regs */
                if (result->oprs[operand].type & ~TO) {
                    /*
                     * we want to produce a warning iff the specified size
                     * is different from the register size
                     */
                    rs = result->oprs[operand].type & SIZE_MASK;
                } else
                    rs = 0;

                result->oprs[operand].type &= TO;
                result->oprs[operand].type |= REGISTER;
                result->oprs[operand].type |= nasm_reg_flags[value->type];
                result->oprs[operand].basereg = value->type;

                if (rs && (result->oprs[operand].type & SIZE_MASK) != rs)
                    error(ERR_WARNING | ERR_PASS1,
                          "register size specification ignored");
            }
        }
    }

    result->operands = operand; /* set operand count */

/* clear remaining operands */
while (operand < MAX_OPERANDS)
    result->oprs[operand++].type = 0;

    /*
     * Transform RESW, RESD, RESQ, REST, RESO, RESY 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;
    case I_RESO:
        result->opcode = I_RESB;
        result->oprs[0].offset *= 16;
        break;
    case I_RESY:
        result->opcode = I_RESB;
        result->oprs[0].offset *= 32;
        break;
    default:
	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 ((e = i->eops)) {
        i->eops = e->next;
	if (e->type == EOT_DB_STRING_FREE)
	    nasm_free(e->stringval);
        nasm_free(e);
    }
}

⌨️ 快捷键说明

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