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

📄 gencode.c

📁 在linux下实行的简单的c语言编译器
💻 C
📖 第 1 页 / 共 2 页
字号:
#include "GenCode.h"

static int label_num = 0;
TreeNode *Fun;

void GenHeader(char *filename)
{
	fprintf(g_data_file, "#%s.S: target asm file generated by BenC\n",filename);
	fprintf(g_data_file, "#Benfaung @ Copyrights\n");
	fprintf(g_data_file, "#http://www.benfaung.org\n");
	fprintf(g_data_file, "#------------------------------\n");
	fprintf(g_data_file, "\n.section .data\n");
	fprintf(g_bss_file, "\n.section .bss\n");
	fprintf(g_asm_file, "\n.section .text\n");
}
/*-----------------------------------------*/
static void GenLabel(char lab[LABEL_SIZE])
{
	static int i = 1;
	char tmp[LABEL_SIZE];
	strcpy(lab, ".L");
	sprintf(tmp, "%d", i);
	strcat(lab, tmp);
	i++;
}

static void lab(char *label)
{
	fprintf(g_asm_file, "%s :\n", label);
}

static void ujp(char *label)
{
	fprintf(g_asm_file, "\tjmp\t%s\n", label);
}

static void fjp(char *label)
{
	fprintf(g_asm_file, "\tpopl\t%s\n", "%eax");
	fprintf(g_asm_file, "\tcmpl\t$0,\t%s\n", "%eax");
	fprintf(g_asm_file, "\tje\t%s\n", label);
}

static void VarDec_ini(char *name, int value, int level)
{
	int offset;
	ValEntry *pEntry = malloc(sizeof(ValEntry));

	if (pTable == NULL)
	{
			fprintf(g_data_file, "%s:\n\t.int\t%d\n",name, value);
	}
	else
	{
		Lookup_Var(pTable, pTable->funEntry, name, pEntry);
		offset = pEntry->offset;

		fprintf(g_asm_file, "\t#Initiate Varible with int:\n");
		fprintf(g_asm_file, "\tpushl\t%s\n", "%eax");
		fprintf(g_asm_file, "\tpushl\t%s\n", "%eax");
		fprintf(g_asm_file, "\tmovl\t$%d,\t%s\n", value, "%eax");
		fprintf(g_asm_file, "\tpushl\t%s\n", "%ebp");
		fprintf(g_asm_file, "\taddl\t$%d,\t%s\n", offset, "%ebp");
		fprintf(g_asm_file, "\tmovl\t%s,\t(%s)\n", "%eax", "%ebp");
		fprintf(g_asm_file, "\tpopl\t%s\n", "%ebp");
		fprintf(g_asm_file, "\tpopl\t%s\n\n", "%eax");
	}
}

static void VarDec_ini_id(char *name, char *id_name, int level)
{
	int offset;
	int id_offset;
	ValEntry *pEntry = malloc(sizeof(ValEntry));
	ValEntry *pIdEntry = malloc(sizeof(ValEntry));

	Lookup_Var(pTable, pTable->funEntry, name, pEntry);
	Lookup_Var(pTable, pTable->funEntry, id_name, pIdEntry);

	offset = pEntry->offset;
	id_offset = pIdEntry->offset;

	fprintf(g_asm_file, "\t#initiate variable with id:\n");
	fprintf(g_asm_file, "\tpushl\t%s\n", "%eax");
	fprintf(g_asm_file, "\tpushl\t%s\n", "%eax");
	fprintf(g_asm_file, "\tmovl\t%d(%s),\t%s\n",id_offset, "%ebp","%eax");
	fprintf(g_asm_file, "\tmovl\t%s,\t%d(%s)\n", "%eax", offset,"%ebp");
	fprintf(g_asm_file, "\tpopl\t%s\n\n", "%eax");
}


static void VarDec(char *name)
{
	if (pTable == NULL)
		fprintf(g_bss_file, "\t.comm\t%s\t4\n", name);
	else
	{
		fprintf(g_asm_file, "\t#declare variable without initiation:\n");
		fprintf(g_asm_file, "\tpushl\t%s\n\n", "%eax");
	}
}

static void ArrDec(char *name, int array_size)
{
	int i;
	if (pTable->level == 0)
		fprintf(g_bss_file, "\t.comm\t%s,\t%d\n",name, array_size); 
	else
	{
		fprintf(g_asm_file, "\t#declare array without initiation:\n");
		fprintf(g_asm_file, "\tpushl\t%s\n\n", "%eax");
	}
}

static void ent(char *name, int para, int retvalue)
{
	if (!strcmp("main", name))
	{
		fprintf(g_asm_file, "\n.global main\n");
	}/*end if (!strcmp("main", name))*/
	fprintf(g_asm_file, "\t.type %s, @function\n", name);
	fprintf(g_asm_file, "%s:\n", name);
/*
	fprintf(g_asm_file, "\tpushl\t%s\n","%eax");
	fprintf(g_asm_file, "\tpushl\t%s\n","%ebx");
	fprintf(g_asm_file, "\tpushl\t%s\n","%ecx");
	fprintf(g_asm_file, "\tpushl\t%s\n","%edx");
*/
	fprintf(g_asm_file, "\tenter\t$%d,\t$0\n", para*4);
}

static void ret(int r, char *name)
{
	int ret_addr = 0;
	FunEntry *pEntry = malloc(sizeof(FunEntry));
	if (r)
	{
		fprintf(g_asm_file, "\t#return with int:\n");
		fprintf(g_asm_file, "\tpopl\t%s\n", "%eax");
		pEntry = Lookup_Fun(name);
		ret_addr = pEntry->ret_val;
		fprintf(g_asm_file, "\tpushl\t%s\n", "%ebp");
		fprintf(g_asm_file, "\taddl\t$%d,\t%s\n", ret_addr, "%ebp");
		fprintf(g_asm_file, "\tmovl\t%s,\t(%s)\n", "%eax", "%ebp");
		fprintf(g_asm_file, "\tpopl\t%s\n\n", "%ebp");
	}/*end if (r)*/
	else
	{
		fprintf(g_asm_file, "\t#return without a value:\n");
	}/*end if(r) else*/

/*
	fprintf(g_asm_file, "\tpopl\t%s\n", "%edx");
	fprintf(g_asm_file, "\tpopl\t%s\n", "%ecx");
	fprintf(g_asm_file, "\tpopl\t%s\n", "%ebx");
	fprintf(g_asm_file, "\tpopl\t%s\n", "%eax");
*/
	fprintf(g_asm_file, "\tleave\n");
	fprintf(g_asm_file, "\tret\n");
}

static void lda(char *name)
{
	int addr = 0;
	ValEntry *pEntry = malloc(sizeof(ValEntry));
	fprintf(g_asm_file, "\t#load an address from memory:\n");
	if (pTable->level == 0)
	{
		fprintf(g_asm_file, "\tleal\t%s,\t%s\n", name, "%eax");
		fprintf(g_asm_file, "\tpushl\t%s\n\n", "%eax");
	}/*end if (pTable->level == 0)*/
	else
	{
		Lookup_Var(pTable, pTable->funEntry, name, pEntry);
		addr = pEntry->offset;
		fprintf(g_asm_file, "\tmovl\t%s,\t%s\n", "%ebp", "%eax");
		fprintf(g_asm_file, "\taddl\t$%d,\t%s\n", addr, "%eax");
		fprintf(g_asm_file, "\tpushl\t%s\n\n", "%eax");
	}/*end if (pTable->level == 0) else*/
}

static void pop()
{
	fprintf(g_asm_file, "\tpopl\t%s\n", "%eax");
}

static void adi()
{
	fprintf(g_asm_file, "\t#add expr:\n");
	fprintf(g_asm_file, "\tpopl\t%s\n", "%ebx");
	fprintf(g_asm_file, "\tpopl\t%s\n", "%eax");
	fprintf(g_asm_file, "\taddl\t%s,\t%s\n", "%ebx", "%eax");
	fprintf(g_asm_file, "\tpushl\t%s\n\n", "%eax");
}

static void stn(char *name)
{
	fprintf(g_asm_file, "\t#store a value to memory:\n");
	if (pTable->level == 0)
	{
		fprintf(g_asm_file, "\tpopl\t%s\n", "%ebx");
		fprintf(g_asm_file, "\tpopl\t%s\n", "%eax");
		fprintf(g_asm_file, "\tmovl\t%s,\t(%s)\n", "%ebx", "%eax");
		fprintf(g_asm_file, "\tpushl\t%s\n\n", "%ebx");
	}/*end if (pTable->level)*/
	else
	{
		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", "%ebp");
		fprintf(g_asm_file, "\tmovl\t%s,\t%s\n", "%eax", "%ebp");
		fprintf(g_asm_file, "\tmovl\t%s,\t(%s)\n", "%ebx", "%ebp");
		fprintf(g_asm_file, "\tpopl\t%s\n\n", "%ebp");
	}/*end if (pTable->level==0) else*/
}

static int lod(char *name)
{
	int addr = 0;
	ValEntry *pEntry = malloc(sizeof(ValEntry));
	if(Lookup_Var(pTable, pTable->funEntry, name, pEntry)==-1)
		return FALSE;

	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", name, "%eax");
		fprintf(g_asm_file, "\tpushl\t%s\n\n", "%eax");
	}
	else /*end if (pTable->level==0)*/
	{
		Lookup_Var(pTable, pTable->funEntry, 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*/

	return TRUE;
}

static void ldc(int value)
{
	fprintf(g_asm_file, "\t#load a constant:\n");
	fprintf(g_asm_file, "\tmovl\t$%d,\t%s\n", value, "%eax");
	fprintf(g_asm_file, "\tpushl\t%s\n\n", "%eax");
}

static void ori()
{
	fprintf(g_asm_file, "\t#or expr:\n");
	fprintf(g_asm_file, "\tpopl\t%s\n", "%ebx");
	fprintf(g_asm_file, "\tpopl\t%s\n", "%eax");
	fprintf(g_asm_file, "\tor\t%s,\t%s\n", "%ebx", "%eax");
	fprintf(g_asm_file, "\tpushl\t%s\n\n", "%eax");
}

static void andi()
{	
	fprintf(g_asm_file, "\t#and expr:\n");
	fprintf(g_asm_file, "\tpopl\t%s\n", "%ebx");
	fprintf(g_asm_file, "\tpopl\t%s\n", "%eax");
	fprintf(g_asm_file, "\tand\t%s,\t%s\n", "%ebx", "%eax");
	fprintf(g_asm_file, "\tpushl\t%s\n\n", "%eax");
}

static void greater()
{
	fprintf(g_asm_file, "\t#gt 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, "\tjg\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 less()
{
	fprintf(g_asm_file, "\t#lt 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, "\tjl\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 gorei()
{
	fprintf(g_asm_file, "\t#ge 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, "\tjge\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 lorei()
{
	fprintf(g_asm_file, "\t#le 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, "\tjle\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 neqi()
{
	fprintf(g_asm_file, "\t#ne 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, "\tjne\t.LABEL%d\n", label_num);

⌨️ 快捷键说明

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