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

📄 codegen.c

📁 一个C语言的编译器
💻 C
📖 第 1 页 / 共 2 页
字号:

void gorei(void) //对">="进行翻译的函数
{
	fprintf(inter_code,"\tPOP\tBX\n");
	fprintf(inter_code,"\tPOP\tAX\n");
	fprintf(inter_code,"\tCMP\tAX , BX\n");
	fprintf(inter_code,"\tJGE\tLabel%d\n",label_num);
	fprintf(inter_code,"\tMOV\tAX , 0\n");
	fprintf(inter_code,"\tPUSH\tAX\n");
	fprintf(inter_code,"\tJMP\tLabel%d\n",label_num+1);
	fprintf(inter_code,"Label%d:",label_num);
	fprintf(inter_code,"\tMOV\tAX , 1\n");
	fprintf(inter_code,"\tPUSH\tAX\n");
	fprintf(inter_code,"Label%d:",label_num+1);
	label_num += 2;
}

void lorei(void) //对"<="进行翻译的函数
{
	fprintf(inter_code,"\tPOP\tBX\n");
	fprintf(inter_code,"\tPOP\tAX\n");
	fprintf(inter_code,"\tCMP\tAX , BX\n");
	fprintf(inter_code,"\tJLE\tLabel%d\n",label_num);
	fprintf(inter_code,"\tMOV\tAX , 0\n");
	fprintf(inter_code,"\tPUSH\tAX\n");
	fprintf(inter_code,"\tJMP\tLabel%d\n",label_num+1);
	fprintf(inter_code,"Label%d:",label_num);
	fprintf(inter_code,"\tMOV\tAX , 1\n");
	fprintf(inter_code,"\tPUSH\tAX\n");
	fprintf(inter_code,"Label%d:",label_num+1);
	label_num += 2;
}

void neqi(void)  //对"!="进行翻译的函数  
{
	fprintf(inter_code,"\tPOP\tBX\n");
	fprintf(inter_code,"\tPOP\tAX\n");
	fprintf(inter_code,"\tCMP\tAX , BX\n");
	fprintf(inter_code,"\tJNE\tLabel%d\n",label_num);
	fprintf(inter_code,"\tMOV\tAX , 0\n");
	fprintf(inter_code,"\tPUSH\tAX\n");
	fprintf(inter_code,"\tJMP\tLabel%d\n",label_num+1);
	fprintf(inter_code,"Label%d:",label_num);
	fprintf(inter_code,"\tMOV\tAX , 1\n");
	fprintf(inter_code,"\tPUSH\tAX\n");
	fprintf(inter_code,"Label%d:",label_num+1);
	label_num += 2;
}
//-----------------------------------------------------------------------------------逻辑运算(&&,||,!)
void andi(void)  //对"$$"进行翻译的函数
{
	fprintf(inter_code,"\tPOP\tBX\n");
	fprintf(inter_code,"\tPOP\tAX\n");
	fprintf(inter_code,"\tAND\tAX , BX\n");
	fprintf(inter_code,"\tPUSH\tAX\n");
}

void ori(void)  //对"||"进行翻译的函数 
{
	fprintf(inter_code,"\tPOP\tBX\n");
	fprintf(inter_code,"\tPOP\tAX\n");
	fprintf(inter_code,"\tOR\tAX , BX\n");
	fprintf(inter_code,"\tPUSH\tAX\n");
}

void noti(void)   //对"!"进行翻译的函数
{
	fprintf(inter_code,"\tPOP\tAX\n");
	fprintf(inter_code,"\tCMP\tAX , 0\n");
	fprintf(inter_code,"\tJNE\tLabel%d\n",label_num);
	fprintf(inter_code,"\tMOV\tAX , 1\n");
	fprintf(inter_code,"\tPUSH\tAX\n");
	fprintf(inter_code,"\tJMP\tLabel%d\n",label_num+1);
	fprintf(inter_code,"Label%d:",label_num);
	fprintf(inter_code,"\tMOV\tAX , 0\n");
	fprintf(inter_code,"\tPUSH\tAX\n");
	fprintf(inter_code,"Label%d:",label_num+1);
	label_num += 2;
}
//------------------------------------------------------------------------------关于语句翻译的函数
//----------------涉及"IF;WHILE;FOR"进行翻译的函数
void ujp(char *label)
{
	fprintf(inter_code,"\tJMP\t%s\n",label);
}

void fjp(char*label)  
{
	fprintf(inter_code,"\tPOP\tAX\n");
	fprintf(inter_code,"\tCMP\tAX , 0\n");
	fprintf(inter_code,"\tJE\t%s\n",label);
}

void lab(char *label)
{
	fprintf(inter_code,"%s :\n",label);
}
//-----------------------------------------------
void call(char *name) //对"函数调用"进行翻译的函数
{
    fprintf(inter_code,"\tCALL\t%s\n",name);
}
void input(char *name)  //对"输入语句"进行翻译的函数
{
	fprintf(inter_code,"\tMOV\tAH , 01H\n");
	fprintf(inter_code,"\tINT\t21H\n");
	fprintf(inter_code,"\tSUB\tAL , 30H\n");
	fprintf(inter_code,"\tMOV\tBH , AL\n");
	fprintf(inter_code,"\tMOV\tAH , 01H\n");
	fprintf(inter_code,"\tINT\t21H\n");
	fprintf(inter_code,"\tSUB\tAL , 30H\n");
	fprintf(inter_code,"\tMOV\tBL , AL\n");
    fprintf(inter_code,"\tMOV\tDL , ';'\n");
    fprintf(inter_code,"\tMOV\tAH , 02H\n");
    fprintf(inter_code,"\tINT\t21H\n");
	fprintf(inter_code,"\tMOV\tAL , BH\n");
    fprintf(inter_code,"\tMOV\tBH , 10\n");
	fprintf(inter_code,"\tMUL\tBH\n");
    fprintf(inter_code,"\tMOV\tBH , 0\n");
    fprintf(inter_code,"\tADD\tBX , AX\n");
	fprintf(inter_code,"\tMOV\t%s , BX\n",name);
    fprintf(inter_code,"\tMOV\tDL , 0DH\n");
	fprintf(inter_code,"\tMOV\tAH , 02H\n");
	fprintf(inter_code,"\tINT\t21H\n");
	fprintf(inter_code,"\tMOV\tDL , 0AH\n");
	fprintf(inter_code,"\tMOV\tAH , 02H\n");
	fprintf(inter_code,"\tINT\t21H\n");
}
void output(char *name)  //对"输出语句"进行翻译的函数
{
    fprintf(inter_code,"\tMOV\tBX , %s\n",name);
	fprintf(inter_code,"\tCALL\tWRITE\n");
	fprintf(inter_code,"\tMOV\tDL , 0DH\n");
	fprintf(inter_code,"\tINT\t21H\n");
	fprintf(inter_code,"\tMOV\tDL , 0AH\n");
	fprintf(inter_code,"\tINT\t21H\n");
}
//----------------------------------------------------------------对树节点进行遍历并进行翻译(CODE段)
void CodeGen_TreeNode(TreeNode * node)    
{
	TreeNode * temp;
	int count;
	while (node != NULL) 
	{
	switch(node ->nodekind){
	case Dec:
		switch(node-> kind.dec){
		case FunDefK:
			Fun = node;
			count = 0;
			temp = node-> child[0];
			while(temp)
			{
				count ++;
				temp = temp-> sibling;
			}
			ent(node ->attr.name, count, (node -> type != Void)? TRUE:FALSE);
			temp = node -> child[0];	//参数
			if(temp)
				CodeGen_TreeNode(temp);
			if(count)
            {
				fprintf(inter_code,"\tMOV\t%s , AX\n",node -> child[0]->attr.name);
			    fprintf(inter_code,"\tPUSH\tAX\n");
	            fprintf(inter_code,"\tPUSH\tBX\n");
	            fprintf(inter_code,"\tPUSH\tCX\n");
			}
			CodeGen_TreeNode(node ->child[1]);	//复合结构体
			if(node -> type == Void)
				ret(FALSE,node ->attr.name);
			else
				ret(TRUE,node ->attr.name);
			leave(node ->attr.name);
			break;
		case CompK:
			pTable = node ->attr.table;
			temp = node ->child[0];
			if(temp)	// Dec
				CodeGen_TreeNode(temp);
			temp = node ->child[1];
			if(temp)	//stmt
				CodeGen_TreeNode(temp);
			compound_exit();
			break;
		case ParamK:
			break;
		default:
			break;
		}
		break;
//------------------------------------------------------------------------------------------------
	case Stmt:
		switch(node ->kind.stmt){
		case IfK:
			temp = node ->child[0];
			GenLabel(pTable ->lab1);
			GenLabel(pTable ->lab2);
			if(temp ->kind.exp == NumK)	//说明已经是0或1
			{
				if(temp -> attr.val.i == 0)
					ujp(pTable ->lab1);
			}
			else 
			{
				CodeGen_TreeNode(temp);
				fjp(pTable ->lab1);
			}
			pTable = node ->child[1] ->attr.table;
			if(node ->child[1])
				CodeGen_TreeNode(node ->child[1]);
			
			if(node ->child[2]) 
				ujp(pTable ->lab2);
			lab(pTable ->lab1);
			if(node ->child[2]) 
			{
				pTable = node ->child[2] ->attr.table;
				CodeGen_TreeNode(node ->child[2]);
				lab(pTable ->lab2);
			}
			break;
		case WhileK:
			GenLabel(pTable ->lab1);
			GenLabel(pTable ->lab2);
			lab(pTable ->lab1);
			temp = node ->child[0];
			if(temp ->kind.exp == NumK)	//说明已经是0或1
			{
				if(temp ->attr.val.i == 0)
					ujp(pTable ->lab2);
			}
			else
			{
				CodeGen_TreeNode(temp);
				fjp(pTable ->lab2);
			}
			if(node -> child[1])
				CodeGen_TreeNode(node -> child[1]);
			ujp(pTable ->lab1);
			lab(pTable ->lab2);
			break;
		case ForK:
            CodeGen_TreeNode(node -> child[0]);
			GenLabel(pTable ->lab1);
			GenLabel(pTable ->lab2);
			lab(pTable ->lab1);
			temp = node ->child[1];
			if(temp ->kind.exp == NumK)	//说明已经是0或1
			{
				if(temp ->attr.val.i == 0)
					ujp(pTable ->lab2);
			}
			else
			{
				CodeGen_TreeNode(temp);
				fjp(pTable ->lab2);
			}
			if(node -> child[2])
				CodeGen_TreeNode(node -> child[2]);
			CodeGen_TreeNode(node -> child[3]);
			ujp(pTable ->lab1);
			lab(pTable ->lab2);
			break;
		case CallK:
			temp = node ->child[0];
			fprintf(inter_code,"\tMOV\tAX , %s\n",node ->child[0] ->attr.name);
			call(node -> attr.name);
			fprintf(inter_code,"\tPUSH\tDX\n");
			break;
		case ReturnK:
			temp = node -> child[0];
			if(temp)
			{
				CodeGen_TreeNode(temp);
			}
			else ret(FALSE,pTable ->funEntry ->name);
			break;
		case AssignK:
			temp = node -> child[0];
			CodeGen_TreeNode(node -> child[1]);
			if(temp -> child[0])  //数组
			{
				ixa_elem_size(temp -> attr.name,temp ->child[0] ->attr.val.i);
			}
			else stn(node -> child[0] ->attr.name);
			break;
		case InputK:
			input(node->attr.name);
			break;
		case OutputK:
            output(node->attr.name);
		default:
			break;
		}
		break;
//---------------------------------------------------------------------------------------------------------
	case Exp:
		switch(node -> kind.exp){                       
		case OpK:
			if(node -> child[0])
				CodeGen_TreeNode(node -> child[0]);
			if(node -> child[1])
				CodeGen_TreeNode(node -> child[1]);
			switch(node -> attr.op) {
			//算术运算
			case PLUS:	adi();		break;
			case SUB:	subi();		break;
			case MUT:	multi();	break;
			case DIV:	divi();		break;
			case MOD:   modi();     break;
			case INC:   inci();
						stn(node -> child[0] -> attr.name);
						break;
			case DEC:   deci();     
				        stn(node -> child[0] -> attr.name);
						break;
			case B_XOR:   b_xori();     break;
			case B_LEFT:  b_lefti();    break;
			case B_RIGHT: b_righti();   break;
			case B_NOT:   b_noti();   
				          stn(node -> child[0] -> attr.name);
						  break;
			case B_AND:   b_andi();     break;
			case B_OR:    b_ori();      break;
			//关系运算
			case EQ:	equi();		break;
			case NEQ:	neqi();		break;
			case LE:	lorei();	break;
			case GE:	gorei();	break;
			case LT:	less();		break;
			case GT:	greater();	break;
			//逻辑运算
			case AND:	andi();		break;
			case OR:	ori();		break;
			case NOT:   noti();     break;
			default:	yyerror("Unknown Operator!");
						break;
			}
			break;
		case NumK:
			ldc(node -> attr.val.i);
			break;
		case IdK:
			lod(node -> attr.name);
			break;
		case CharK:
			ldd(node -> attr.val.i);
			break;
		default:
			break;
		}
		break;
	default:
		break;
	}
    node=node->sibling;
	}
}
//-------------------------------------------------------------------------------DATA段的数据定义形式
void DataSegment(TreeNode * node)
{

	TreeNode * temp;
	int count;
	while (node != NULL) 
	{
	switch(node ->nodekind)
	{
	case Dec:
		switch(node-> kind.dec){
		case FunDefK:
			Fun = node;
			count = 0;
			temp = node -> child[0];	//参数
			if(temp)
				DataSegment(temp);
			DataSegment(node ->child[1]);	//复合结构体
			break;
		case VarK:
			temp = node -> child[0];
			while(temp)
			{
				if(temp -> nodekind == Stmt)	// 定义并且赋值
						VarDec_ini(temp ->child[0] ->attr.name, temp->child[1]->attr.val.i);
				else
				{
					if(temp ->child[0] == NULL)  //定义但未赋值
						VarDec(temp ->attr.name);	
					else                         //定义数组
						ArrDec(temp ->attr.name, temp ->child[0] ->attr.val.i);
				}
				temp = temp ->sibling;
			}
			break;
		case ParamK:
			if(node->type==Integer)
			{
		       fprintf(inter_code,"%s\tdw\t",node->attr.name);
               fprintf(inter_code,"?\t\n");
			}
 	        if(node->type==Char)
			{
		       fprintf(inter_code,"%s\tdb\t",node->attr.name);
               fprintf(inter_code,"?\t\n");
			}	
			break;
		case CompK:
			pTable = node ->attr.table;
			temp = node ->child[0];
			if(temp)	//声明部分
				DataSegment(temp);
			temp = node ->child[1];
			if(temp)	//语句部分(语句中可能定义一些变量)
				DataSegment(temp);
			compound_exit();
			break;
		}
		break;
	case Stmt:
		switch(node ->kind.stmt)
		{
		case IfK:
			temp = node ->child[0];
			if(temp ->kind.exp != NumK)	
				DataSegment(temp);
			pTable = node ->child[1] ->attr.table;
			if(node ->child[1])
				DataSegment(node ->child[1]);
			if(node ->child[2]) 
			{
				pTable = node ->child[2] ->attr.table;
				DataSegment(node ->child[2]);
			}
			break;
		case WhileK:
			temp = node ->child[0];
			if(temp ->kind.exp != NumK)	//说明已经是0或1
				DataSegment(temp);
			if(node -> child[1])
				DataSegment(node -> child[1]);
			break;
		case ForK:
            DataSegment(node -> child[0]);
			temp = node ->child[1];
			if(temp ->kind.exp != NumK)	//说明已经是0或1
				DataSegment(temp);
			if(node -> child[2])
				DataSegment(node -> child[2]);
			DataSegment(node -> child[3]);
			break;
		}
	}
    node=node->sibling;
	}
}
//---------------------------------------------------------------------------------------------------

⌨️ 快捷键说明

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