?? codegen.c
字號:
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 + -