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

📄 gencode.c

📁 在linux下实行的简单的c语言编译器
💻 C
📖 第 1 页 / 共 2 页
字号:
	fprintf(g_asm_file, "\tmovl\t$0,\t%s\n", "%eax");
	fprintf(g_asm_file, "\tpushl\t%s\n", "%eax");
	fprintf(g_asm_file, "\tjmp\t.LABEL%d\n", label_num+1);
	fprintf(g_asm_file, "\t.LABEL%d:\n",label_num);
	fprintf(g_asm_file, "\tmovl\t$1,\t%s\n", "%eax");
	fprintf(g_asm_file, "\t.LABEL%d:\n", label_num+1);
	label_num += 2;
}

static void equi()
{
	fprintf(g_asm_file, "\t#eq expr:\n");
	fprintf(g_asm_file, "\tpopl\t%s\n", "%ebx");
	fprintf(g_asm_file, "\tpopl\t%s\n", "%eax");
	fprintf(g_asm_file, "\tcmpl\t%s,\t%s\n", "%ebx", "%eax");
	fprintf(g_asm_file, "\tje\t.LABEL%d\n", label_num);
	fprintf(g_asm_file, "\tmovl\t$0,\t%s\n", "%eax");
	fprintf(g_asm_file, "\tpushl\t%s\n", "%eax");
	fprintf(g_asm_file, "\tjmp\t.LABEL%d\n", label_num+1);
	fprintf(g_asm_file, "\t.LABEL%d:\n",label_num);
	fprintf(g_asm_file, "\tmovl\t$1,\t%s\n", "%eax");
	fprintf(g_asm_file, "\t.LABEL%d:\n", label_num+1);
	label_num += 2;
}
static void divi()
{
	fprintf(g_asm_file, "\t#div expr:\n");
	fprintf(g_asm_file, "\tpopl\t%s\n", "%ebx");
	fprintf(g_asm_file, "\tpopl\t%s\n", "%eax");
	fprintf(g_asm_file, "\tpushl\t%s\n", "%edx");
	fprintf(g_asm_file, "\tidivl\t%s\n", "%ebx");
	fprintf(g_asm_file, "\tpopl\t%s\n", "%edx");
	fprintf(g_asm_file, "\tpushl\t%s\n\n", "%eax");
}

static void multi()
{
	fprintf(g_asm_file, "\t#mul expr:\n");
	fprintf(g_asm_file, "\tpopl\t%s\n", "%ebx");
	fprintf(g_asm_file, "\tpopl\t%s\n", "%eax");
	fprintf(g_asm_file, "\tpushl\t%s\n", "%edx");
	fprintf(g_asm_file, "\timull\t%s\n", "%ebx");
	fprintf(g_asm_file, "\tpopl\t%s\n", "%edx");
	fprintf(g_asm_file, "\tpushl\t%s\n\n", "%eax");
}

static void subi()
{
	fprintf(g_asm_file, "\t#sub expr:\n");
	fprintf(g_asm_file, "\tpopl\t%s\n", "%edx");
	fprintf(g_asm_file, "\tpopl\t%s\n", "%ecx");
	fprintf(g_asm_file, "\tsubl\t%s,\t%s\n", "%edx", "%ecx");
	fprintf(g_asm_file, "\tpushl\t%s\n\n", "%ecx");
}

static void cup(char *name, int para_num)
{
	fprintf(g_asm_file, "\t#call stmt:\n");
	fprintf(g_asm_file, "\tcall\t%s\n\n", name);
}

static void param(TreeNode * node)
{
	int addr = 0;
	ValEntry *pEntry = malloc(sizeof(ValEntry));

	fprintf(g_asm_file, "\t#passing params:\n");
	while (node)
	{
		if(Lookup_Var(pTable, pTable->funEntry, node->attr.name, pEntry)==-1)
			return;

		fprintf(g_asm_file, "\t#load a value from memory:\n");
		if (pTable->level == 0)
		{
			fprintf(g_asm_file, "\tmovl\t%s,\t%s\n", node->attr.name, "%eax");
			fprintf(g_asm_file, "\tpushl\t%s\n\n", "%eax");
		}
		else /*end if (pTable->level==0)*/
		{
			Lookup_Var(pTable, pTable->funEntry, node->attr.name, pEntry);
			addr = pEntry->offset;
			fprintf(g_asm_file, "\tpushl\t%s\n", "%ebp");
			fprintf(g_asm_file, "\taddl\t$%d,\t%s\n", addr, "%ebp");
			fprintf(g_asm_file, "\tmovl\t(%s),\t%s\n", "%ebp", "%eax");
			fprintf(g_asm_file, "\tpopl\t%s\n", "%ebp");
			fprintf(g_asm_file, "\tpushl\t%s\n\n", "%eax");
		}/*end if (pTable->level..)else*/

		node = node->sibling;
	}/*end while(node)*/
}

static void bkm()
{
	int i = 0;
	if (pTable == NULL || pTable->parent == NULL) return;

	ValEntry  *pVal; 
	for (i = 0; i < SIZE; i++)
	{
		for (pVal = pTable->parent->valTable[i]; pVal; pVal = pVal->next)
		{
			fprintf(g_asm_file, "\t#backup memory value:\n");
			fprintf(g_asm_file, "\tpushl\t%d(%s)\n\n", pVal->offset, "%ebp");
		}/*end if (pTable->parent...)*/
	}/*end for (i = 0; i < SIZE; i++)*/
}

static void rsm()
{
	int i = 0;
	if (pTable == NULL) return;

	ValEntry  *pVal; 
	for (i = 0; i < SIZE; i++)
	{
		for (pVal = pTable->valTable[i]; pVal; pVal = pVal->next)
		{
			fprintf(g_asm_file, "\t#restore memory value:\n");
			fprintf(g_asm_file, "\tpopl\t%s\n", "%ebx");
			fprintf(g_asm_file, "\tpopl\t%d(%s)\n\n", pVal->offset, "%ebp");
		}/*end if (pTable->parent...)*/
	}/*end for (i = 0; i < SIZE; i++)*/

}
/*-----------------------------------------*/
void GenCode(TreeNode *node)
{
	TreeNode *tmp;
	int count;

	while (node != NULL)
	{
	switch (node->nodeKind)
	{
	case Dec:
		switch (node->kind.dec)
		{
		case FunDecK:
			/*do nothing*/
			break;
		case FunDefK:
			Fun = node;	
			count = 0;
			tmp = node->child[0]; /*param*/
			while(tmp)
			{
				count++;
				tmp = tmp->sibling;
			}/*end while(tmp)*/	
			ent(node->attr.name, count, (node->type!=Void)?TRUE:FALSE);
			tmp = node->child[0]; /*param*/
			if (tmp)
				GenCode(tmp);

			GenCode(node->child[1]); /*compond stmt*/
			if (node->type==Void)
				ret(FALSE, node->attr.name);
			break;
		case VarK:
			tmp = node->child[0];
			if (node->kind.exp==NumK)
			while(tmp)
			{
				if (tmp->nodeKind==Stmt)
				{
					if (tmp->child[1]->kind.exp == IdK)
						VarDec_ini_id(tmp->child[0]->attr.name, tmp->child[1]->attr.name, tmp->child[0]->attr.table->level);
					else
						VarDec_ini(tmp->child[0]->attr.name, tmp->child[1]->attr.val.i, tmp->child[0]->attr.table->level);
				}/*end if (tmp->nodeKine==Stmt)*/
				else
				{
					if (tmp->child[0]==NULL)
						VarDec(tmp->attr.name);
					else
						ArrDec(tmp->attr.name, tmp->child[0]->attr.val.i);
				}/*end if (tmp->nodekind==Stmt) else*/
				tmp = tmp->sibling;
			}/*end while(tmp)*/
			break;
		case CompK:
			pTable = node->attr.table;
			bkm();
			tmp = node->child[0];
			if (tmp)
				GenCode(tmp);
			tmp = node->child[1];
			if (tmp)
				GenCode(tmp);
			pTable = pTable->parent;	
			rsm();
			break;
		case ParamK:
			break;
		default:
			break;
		}/*switch(node->kind.dec)*/
		break;
	case Exp:
		switch(node->kind.exp)
		{
		case OpK:
			if (node->child[0])
				GenCode(node->child[0]);
			if (node->child[1])
				GenCode(node->child[1]);
			switch (node->attr.op)
			{
			case PLUS:	adi();		break;
			case SUB:	subi();		break;
			case MUT:	multi();	break;
			case DIV:	divi();		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;
			defaut:		yyerror("Unknown Operator!"); break;
			}/*end switch(node->attr.op)*/
			break;
		case FNumK:
			break;
		case NumK:
			ldc(node->attr.val.i);
			break;
		case CharK:
			break;
		case IdK:
			lod(node->attr.name);
			break;
		case NotK:
			break;
		default:
			break;
		}/*switch(node->kind.exp)*/
		break;
	case Stmt:
		switch(node->kind.stmt)
		{
		case IfK:
			fprintf(g_asm_file, "\t#If Stmt:\n");
			tmp = node->child[0];
			GenLabel(pTable->lab1);
			GenLabel(pTable->lab2);
			if (tmp->kind.exp == NumK)
			{
				if (tmp->attr.val.i == 0)
					ujp(pTable->lab1);
			}/*end if (tmp->kind.exp == NumK)*/
			else
			{
				GenCode(tmp);
				fjp(pTable->lab1);
			}/*end if (tmp->kind.exp...) else*/

			//pTable = node->child[1]->attr.table;
			if (node->child[1])

				GenCode(node->child[1]);
			if (node->child[2])
				ujp(pTable->lab2);

			lab(pTable->lab1);

			if (node->child[2])
			{
				pTable = node->child[2]->attr.table;
				GenCode(node->child[2]);
				lab(pTable->lab2);
			}
			break;
		case WhileK:
			fprintf(g_asm_file, "\t#while stmt:\n");
			GenLabel(pTable->lab1);
			GenLabel(pTable->lab2);
			lab(pTable->lab1);
			tmp = node->child[0];
			if (tmp->kind.exp == NumK)
			{
				if (tmp->attr.val.i == 0)
					ujp(pTable->lab2);
			}/*end if (tmp->kind.exp == NumK)*/
			else
			{
				GenCode(tmp);
				fjp(pTable->lab2);
			}/*end if (tmp->kind.exp...) else*/
			if (node->child[1])
				GenCode(node->child[1]);
			ujp(pTable->lab1);
			lab(pTable->lab2);
			break;
		case CallK:
			tmp = node->child[0]; 	/*param*/
			param(tmp);
			cup(node->attr.name, count);
			while (tmp)
			{
				tmp = tmp->sibling;
			}/*end while (tmp)*/
			if (node->call_stmt == 1)
				pop();
			break;
		case ReturnK:
			tmp = node->child[0];
			if (tmp)
			{
				GenCode(tmp);		
				ret(TRUE, /*pTable->funEntry->name*/Fun->attr.name);
			}/*if (tmp)*/
			break;
		case BreakK:
			ujp(pTable->parent->lab2);
			break;
		case ContinueK:
			ujp(pTable->parent->lab1);
			break;
		case AssignK:
			tmp = node->child[0];
			lda(tmp->attr.name);
			if (node->child[1])
				GenCode(node->child[1]);
			stn(node->child[0]->attr.name);
			//pop();
			break;
		default:
			break;
		}
		break;
	}/*switch(node->nodeKine)*/
	node = node->sibling;
	}/*while(node!=NULL)*/
}

⌨️ 快捷键说明

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