📄 codegen.c
字号:
#include <stdlib.h>
#include "globals.h"
#include "parse.h"
#include "symtab.h"
static int label_num = 0;
TreeNode * Fun;
void GenLabel(char lab[LABEL_SIZE])
{
static int i=1;
char temp[LABEL_SIZE];
strcpy(lab,"L");
_itoa(i,temp,10);
strcat(lab,temp);
i++;
}
void StackSegmentBegin(void)
{
fprintf(inter_code,";*******************************************************\n");
fprintf(inter_code,"Stack_seg\tsegment\n");
fprintf(inter_code,"\tdw\t1024 dup(?)\n");
fprintf(inter_code,"\ttos\tlabel\tword\n");
fprintf(inter_code,"Stack_seg\tends\n");
fprintf(inter_code,";*******************************************************\n");
fprintf(inter_code,"Data_seg\tsegment\n");
}
void StackSegmentEnd(void)
{
fprintf(inter_code,"Data_seg\tends\n");
fprintf(inter_code,";*******************************************************\n");
}
void CodeSegmentBegin(void)
{
fprintf(inter_code,"Code_seg\tsegment\n");
fprintf(inter_code,"\tassume\tCS:Code_seg, DS:Data_seg, SS:Stack_seg\n");
}
void CodeSegmentEnd(void)
{
fprintf(inter_code,"Code_seg\tends\n");
fprintf(inter_code,"\tend\tstart\n");
fprintf(inter_code,";*******************************************************\n");
}
void VarDec_ini(char *name, int value)//local variable declaration with initial value
{
int os;
ValEntry * pEntry = malloc(sizeof(ValEntry));
if(pTable ->nestlevel == 0)
fprintf(inter_code,"\t%s\tdw\t%d\n",name,value);
else
{
Lookup_Var(pTable, pTable ->funEntry, name, pEntry);
os = pEntry ->offset;
fprintf(inter_code,"\tPUSH\tAX\n");//为该变量占个位置,迫使sp移动
fprintf(inter_code,"\tPUSH\tAX\n");//store value
fprintf(inter_code,"\tMOV\tAX , %d\n",value);
fprintf(inter_code,"\tPUSH\tBP\n");//calculate variable address
fprintf(inter_code,"\tADD\tBP , %d\n",os);
fprintf(inter_code,"\tMOV\tSS:[BP] , AX\n",os);//store value
fprintf(inter_code,"\tPOP\tBP\n");
fprintf(inter_code,"\tPOP\tAX\n");
}
}
void VarDec(char *name)//local variable declaration without initial value
{
if(pTable ->nestlevel == 0)
fprintf(inter_code,"\t%s\tdw\t?\n",name);
else
fprintf(inter_code,"\tPUSH\tAX\n");//为该变量占个位置,迫使sp移动
}
void ArrDec(char *name, int entitynum)
{
int i;
if(pTable ->nestlevel == 0)
fprintf(inter_code,"\t%s\tdw\t%d dup(?)\n",name,entitynum);//to data file
else
for(i = 0; i < entitynum; i ++)
fprintf(inter_code,"\tPUSH\tAX\n");//为该变量占个位置,迫使sp移动
}
void ent(char *name, int para, int retvalue)
{
fprintf(inter_code,"\t%s\tproc\tfar\n",name);//to code file
if(!strcmp("main",name)){//main function
fprintf(inter_code,"start:\n");
fprintf(inter_code,"\tMOV\tAX , Stack_seg\n");
fprintf(inter_code,"\tMOV\tSS , AX\n");
fprintf(inter_code,"\tMOV\tSP , offset tos\n\n");
fprintf(inter_code,"\tPUSH\tDS\n");
fprintf(inter_code,"\tSUB\tAX , AX\n");
fprintf(inter_code,"\tPUSH\tAX\n\n");
fprintf(inter_code,"\tMOV\tAX , Data_seg\n");
fprintf(inter_code,"\tMOV\tDS , AX\n");
}
fprintf(inter_code,"\tPUSH\tAX\n");
fprintf(inter_code,"\tPUSH\tBX\n");
fprintf(inter_code,"\tPUSH\tCX\n");
fprintf(inter_code,"\tPUSH\tDX\n");
fprintf(inter_code,"\tPUSH\tBP\n");
fprintf(inter_code,"\tMOV\tBP , SP\n");//begin to accept local variables
}
//--------------------------------------------------
void adi(void)
{
fprintf(inter_code,"\tPOP\tBX\n");
fprintf(inter_code,"\tPOP\tAX\n");
fprintf(inter_code,"\tADD\tAX , BX\n");
fprintf(inter_code,"\tPUSH\tAX\n");
}
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 compound_enter(void)
{
}
void compound_exit(void)
{
pTable = pTable->parent;
}
void cup(char *name, int paranum) //function call end, 'int' is the number of actual parameters
{
fprintf(inter_code,"\tCALL\t%s\n",name);
}
void divi(void)
{
fprintf(inter_code,"\tPOP\tBX\n");
fprintf(inter_code,"\tPOP\tAX\n");
fprintf(inter_code,"\tPUSH\tDX\n");
fprintf(inter_code,"\tIDIV\tBX\n");
fprintf(inter_code,"\tPOP\tDX\n");
fprintf(inter_code,"\tPUSH\tAX\n");
}
void equi(void)
{
fprintf(inter_code,"\tPOP\tBX\n");
fprintf(inter_code,"\tPOP\tAX\n");
fprintf(inter_code,"\tCMP\tAX , BX\n");
fprintf(inter_code,"\tJE\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 leave(char *name)
{
fprintf(inter_code,"\t%s\tendp\n",name);
}
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 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 greater(void)
{
fprintf(inter_code,"\tPOP\tBX\n");
fprintf(inter_code,"\tPOP\tAX\n");
fprintf(inter_code,"\tCMP\tAX , BX\n");
fprintf(inter_code,"\tJG\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 ind(char *name)
{
if(pTable ->nestlevel == 0){
fprintf(inter_code,"\tPOP\tBX\n");
fprintf(inter_code,"\tPUSH\tDS:[BX]\n");
}
else{
fprintf(inter_code,"\tPOP\tBX\n");
fprintf(inter_code,"\tPUSH\tBP\n");
fprintf(inter_code,"\tMOV\tBP , BX\n");
fprintf(inter_code,"\tPUSH\tSS:[BP]\n");
fprintf(inter_code,"\tPOP\tBP\n");
}
}
void ixa_elem_size(void)
{
fprintf(inter_code,"\tPOP\tAX\n");
fprintf(inter_code,"\tPOP\tBX\n");
fprintf(inter_code,"\tSHL\tAX , %d\n",EXP_VARIABLE_LENGTH);//AX * VARIABLE_LENGTH
fprintf(inter_code,"\tADD\tAX , BX\n");
fprintf(inter_code,"\tPUSH\tAX\n");
}
void lab(char *label)
{
fprintf(inter_code,"%s :\n",label);
}
void ldl(char value)
{
fprintf(inter_code,"\tMOV\tAX , %d\n",(int)value);
fprintf(inter_code,"\tPUSH\tAX\n");
}
void lda(char *name)//load address
{
int addr = 0;
ValEntry * pEntry = malloc(sizeof(ValEntry));
if(pTable ->nestlevel == 0){
fprintf(inter_code,"\tLEA\tAX , %s\n",name);
fprintf(inter_code,"\tPUSH\tAX\n");
}
else{
Lookup_Var(pTable, pTable ->funEntry, name, pEntry);
addr = pEntry->offset;
fprintf(inter_code,"\tMOV\tAX , BP\n");
fprintf(inter_code,"\tADD\tAX , %d\n",addr);
fprintf(inter_code,"\tPUSH\tAX\n");
}
}
void ldc(int value)//load constant
{
fprintf(inter_code,"\tMOV\tAX , %d\n",value);
fprintf(inter_code,"\tPUSH\tAX\n");
}
void less(void)
{
fprintf(inter_code,"\tPOP\tBX\n");
fprintf(inter_code,"\tPOP\tAX\n");
fprintf(inter_code,"\tCMP\tAX , BX\n");
fprintf(inter_code,"\tJL\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 lod(char *name)
{
int addr = 0;
ValEntry * pEntry = malloc(sizeof(ValEntry));
Lookup_Var(pTable, pTable ->funEntry, name, pEntry);
if(pTable ->nestlevel == 0){
fprintf(inter_code,"\tMOV\tAX , %s\n",name);
fprintf(inter_code,"\tPUSH\tAX\n");
}
else{
Lookup_Var(pTable,pTable ->funEntry, name,pEntry);
addr = pEntry ->offset;
fprintf(inter_code,"\tPUSH\tBP\n");
fprintf(inter_code,"\tADD\tBP , %d\n",addr);
fprintf(inter_code,"\tMOV\tAX , SS:[BP]\n");
fprintf(inter_code,"\tPOP\tBP\n");
fprintf(inter_code,"\tPUSH\tAX\n");
}
}
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 mst(char *name)
{
}
void multi(void)
{
fprintf(inter_code,"\tPOP\tBX\n");
fprintf(inter_code,"\tPOP\tAX\n");
fprintf(inter_code,"\tPUSH\tDX\n");
fprintf(inter_code,"\tIMUL\tBX\n");
fprintf(inter_code,"\tPOP\tDX\n");
fprintf(inter_code,"\tPUSH\tAX\n");
}
void neqi(void)
{
fprintf(inter_code,"\tPOP\tBX\n");
fprintf(inter_code,"\tPOP\tAX\n");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -