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

📄 parse_~1.c

📁 操作系统源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
	case '&':	case '|':	case '^':	case S_LEFTSHIFT:	case S_RIGHTSHIFT:		(*pn)++;		a1= e;		if ((a2= ack_get_C_expression(pn)) == nil) {			del_expr(a1);			return nil;		}		e= new_expr();		e->operator= t->symbol;		e->left= a1;		e->right= a2;	}	return e;}static expression_t *ack_get_operand(int *pn, int deref)/* get something like: (memory), offset(base)(index*scale), or simpler. */{	expression_t *e, *offset, *base, *index;	token_t *t;	int c;	/* Is it (memory)? */	if (get_token(*pn)->symbol == '('		&& ((t= get_token(*pn + 1))->type != T_WORD			|| !isregister(t->name))	) {		/* A memory dereference. */		(*pn)++;		if ((offset= ack_get_C_expression(pn)) == nil) return nil;		if (get_token(*pn)->symbol != ')') {			parse_err(1, t, "operand syntax error\n");			del_expr(offset);			return nil;		}		(*pn)++;		e= new_expr();		e->operator= '(';		e->middle= offset;		return e;	}	/* #constant? */	if (dialect == NCC && deref			&& ((c= get_token(*pn)->symbol) == '#' || c == '*')) {		/* NCC: mov ax,#constant  ->  ACK: mov ax,constant */		(*pn)++;		return ack_get_C_expression(pn);	}	/* @address? */	if (dialect == NCC && get_token(*pn)->symbol == '@') {		/* NCC: jmp @address  ->  ACK: jmp (address) */		(*pn)++;		if ((offset= ack_get_operand(pn, deref)) == nil) return nil;		e= new_expr();		e->operator= '(';		e->middle= offset;		return e;	}	/* Offset? */	if (get_token(*pn)->symbol != '(') {		/* There is an offset. */		if ((offset= ack_get_C_expression(pn)) == nil) return nil;	} else {		/* No offset. */		offset= nil;	}	/* (base)? */	if (get_token(*pn)->symbol == '('		&& (t= get_token(*pn + 1))->type == T_WORD		&& isregister(t->name)		&& get_token(*pn + 2)->symbol == ')'	) {		/* A base register expression. */		base= new_expr();		base->operator= 'B';		base->name= copystr(t->name);		(*pn)+= 3;	} else {		/* No base register expression. */		base= nil;	}	/* (index*scale)? */	if (get_token(*pn)->symbol == '(') {		/* An index most likely. */		token_t *m= nil;		if (!(		/* This must be true: */			(t= get_token(*pn + 1))->type == T_WORD			&& isregister(t->name)			&& (get_token(*pn + 2)->symbol == ')' || (				get_token(*pn + 2)->symbol == '*'				&& (m= get_token(*pn + 3))->type == T_WORD				&& strchr("1248", m->name[0]) != nil				&& m->name[1] == 0				&& get_token(*pn + 4)->symbol == ')'			))		)) {			/* Alas it isn't */			parse_err(1, t, "operand syntax error\n");			del_expr(offset);			del_expr(base);			return nil;		}		/* Found an index. */		index= new_expr();		index->operator= m == nil ? '1' : m->name[0];		index->name= copystr(t->name);		(*pn)+= (m == nil ? 3 : 5);	} else {		/* No index. */		index= nil;	}	if (dialect == NCC && deref && base == nil && index == nil		&& !(offset != nil && offset->operator == 'W'					&& isregister(offset->name))	) {		/* NCC: mov ax,thing  ->  ACK mov ax,(thing) */		e= new_expr();		e->operator= '(';		e->middle= offset;		return e;	}	if (base == nil && index == nil) {		/* Return a lone offset as is. */		e= offset;	} else {		e= new_expr();		e->operator= 'O';		e->left= offset;		e->middle= base;		e->right= index;	}	return e;}static expression_t *ack_get_oplist(int *pn, int deref)/* Get a comma (or colon for jmpf and callf) separated list of instruction * operands. */{	expression_t *e, *o1, *o2;	token_t *t;	if ((e= ack_get_operand(pn, deref)) == nil) return nil;	if ((t= get_token(*pn))->symbol == ',' || t->symbol == ':') {		o1= e;		(*pn)++;		if ((o2= ack_get_oplist(pn, deref)) == nil) {			del_expr(o1);			return nil;		}		e= new_expr();		e->operator= ',';		e->left= o1;		e->right= o2;	}	return e;}static asm86_t *ack_get_statement(void)/* Get a pseudo op or machine instruction with arguments. */{	token_t *t= get_token(0);	asm86_t *a;	mnemonic_t *m;	int n;	int prefix_seen;	int oaz_prefix;	int deref;	assert(t->type == T_WORD);	if (strcmp(t->name, ".sect") == 0) {		/* .sect .text etc.  Accept only four segment names. */		skip_token(1);		t= get_token(0);		if (t->type != T_WORD || (			strcmp(t->name, ".text") != 0			&& strcmp(t->name, ".rom") != 0			&& strcmp(t->name, ".data") != 0			&& strcmp(t->name, ".bss") != 0			&& strcmp(t->name, ".end") != 0		)) {			parse_err(1, t, "weird section name to .sect\n");			return nil;		}	}	a= new_asm86();	/* Process instruction prefixes. */	oaz_prefix= 0;	for (prefix_seen= 0;; prefix_seen= 1) {		if (strcmp(t->name, "o16") == 0) {			if (use16()) {				parse_err(1, t, "o16 in an 8086 section\n");			}			oaz_prefix|= OPZ;		} else		if (strcmp(t->name, "o32") == 0) {			if (use32()) {				parse_err(1, t, "o32 in an 80386 section\n");			}			oaz_prefix|= OPZ;		} else		if (strcmp(t->name, "a16") == 0) {			if (use16()) {				parse_err(1, t, "a16 in an 8086 section\n");			}			oaz_prefix|= ADZ;		} else		if (strcmp(t->name, "a32") == 0) {			if (use32()) {				parse_err(1, t, "a32 in an 80386 section\n");			}			oaz_prefix|= ADZ;		} else		if (strcmp(t->name, "rep") == 0			|| strcmp(t->name, "repe") == 0			|| strcmp(t->name, "repne") == 0			|| strcmp(t->name, "repz") == 0			|| strcmp(t->name, "repnz") == 0		) {			if (a->rep != ONCE) {				parse_err(1, t,					"can't have more than one rep\n");			}			switch (t->name[3]) {			case 0:		a->rep= REP;	break;			case 'e':			case 'z':	a->rep= REPE;	break;			case 'n':	a->rep= REPNE;	break;			}		} else		if (strchr("cdefgs", t->name[0]) != nil					&& strcmp(t->name+1, "seg") == 0) {			if (a->seg != DEFSEG) {				parse_err(1, t,				"can't have more than one segment prefix\n");			}			switch (t->name[0]) {			case 'c':	a->seg= CSEG;	break;			case 'd':	a->seg= DSEG;	break;			case 'e':	a->seg= ESEG;	break;			case 'f':	a->seg= FSEG;	break;			case 'g':	a->seg= GSEG;	break;			case 's':	a->seg= SSEG;	break;			}		} else		if (!prefix_seen) {			/* No prefix here, get out! */			break;		} else {			/* No more prefixes, next must be an instruction. */			if (t->type != T_WORD				|| (m= search_mnem(t->name)) == nil				|| m->optype == PSEUDO			) {				parse_err(1, t,		"machine instruction expected after instruction prefix\n");				del_asm86(a);				return nil;			}			if (oaz_prefix != 0 && m->optype != JUMP						&& m->optype != WORD) {				parse_err(1, t,			"'%s' can't have an operand size prefix\n", m->name);			}			break;		}		/* Skip the prefix and extra newlines. */		do {			skip_token(1);		} while ((t= get_token(0))->symbol == ';');	}	/* All the readahead being done upsets the line counter. */	a->line= t->line;	/* Read a machine instruction or pseudo op. */	if ((m= search_mnem(t->name)) == nil) {		parse_err(1, t, "unknown instruction\n");		del_asm86(a);		return nil;	}	a->opcode= m->opcode;	a->optype= m->optype;	a->oaz= oaz_prefix;	switch (a->opcode) {	case IN:	case OUT:	case INT:		deref= 0;		break;	default:		deref= (a->optype >= BYTE);	}	n= 1;	if (get_token(1)->symbol != ';'			&& (a->args= ack_get_oplist(&n, deref)) == nil) {		del_asm86(a);		return nil;	}	if (get_token(n)->symbol != ';') {		parse_err(1, t, "garbage at end of instruction\n");		del_asm86(a);		return nil;	}	switch (a->opcode) {	case DOT_ALIGN:		/* Restrict .align to have a single numeric argument, some		 * assemblers think of the argument as a power of two, so		 * we need to be able to change the value.		 */		if (a->args == nil || a->args->operator != 'W'					|| !isanumber(a->args->name)) {			parse_err(1, t,			  ".align is restricted to one numeric argument\n");			del_asm86(a);			return nil;		}		break;	case JMPF:	case CALLF:		/* NCC jmpf off,seg  ->  ACK jmpf seg:off */		if (dialect == NCC && a->args != nil						&& a->args->operator == ',') {			expression_t *t;			t= a->args->left;			a->args->left= a->args->right;			a->args->right= t;			break;		}		/*FALL THROUGH*/	case JMP:	case CALL:		/* NCC jmp @(reg)  ->  ACK jmp (reg) */		if (dialect == NCC && a->args != nil && (			(a->args->operator == '('				&& a->args->middle != nil				&& a->args->middle->operator == 'O')			|| (a->args->operator == 'O'				&& a->args->left == nil				&& a->args->middle != nil				&& a->args->right == nil)		)) {			expression_t *t;			t= a->args;			a->args= a->args->middle;			t->middle= nil;			del_expr(t);			if (a->args->operator == 'B') a->args->operator= 'W';		}		break;	default:;	}	skip_token(n+1);	return a;}asm86_t *ack_get_instruction(void){	asm86_t *a= nil;	expression_t *e;	token_t *t;	while ((t= get_token(0))->symbol == ';')		skip_token(1);	if (t->type == T_EOF) return nil;	if (t->symbol == '#') {		/* Preprocessor line and file change. */		if ((t= get_token(1))->type != T_WORD || !isanumber(t->name)			|| get_token(2)->type != T_STRING		) {			parse_err(1, t, "file not preprocessed?\n");			zap();		} else {			set_file(get_token(2)->name,				strtol(get_token(1)->name, nil, 0) - 1);			/* GNU CPP adds extra cruft, simply zap the line. */			zap();		}		a= ack_get_instruction();	} else	if (t->type == T_WORD && get_token(1)->symbol == ':') {		/* A label definition. */		a= new_asm86();		a->line= t->line;		a->opcode= DOT_LABEL;		a->optype= PSEUDO;		a->args= e= new_expr();		e->operator= ':';		e->name= copystr(t->name);		skip_token(2);	} else	if (t->type == T_WORD && get_token(1)->symbol == '=') {		int n= 2;		if ((e= ack_get_C_expression(&n)) == nil) {			zap();			a= ack_get_instruction();		} else		if (get_token(n)->symbol != ';') {			parse_err(1, t, "garbage after assignment\n");			zap();			a= ack_get_instruction();		} else {			a= new_asm86();			a->line= t->line;			a->opcode= DOT_EQU;			a->optype= PSEUDO;			a->args= new_expr();			a->args->operator= '=';			a->args->name= copystr(t->name);			a->args->middle= e;			skip_token(n+1);		}	} else	if (t->type == T_WORD) {		if ((a= ack_get_statement()) == nil) {			zap();			a= ack_get_instruction();		}	} else {		parse_err(1, t, "syntax error\n");		zap();		a= ack_get_instruction();	}	return a;}asm86_t *ncc_get_instruction(void){	return ack_get_instruction();}

⌨️ 快捷键说明

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