?? util.cpp
字號:
/*從下一條語句開始,進入一個新的基本塊*/
if (code->next!= NULL)
{ code = code->next;
baseBlock[blocknum++] =code;
}
break;
/*變參傳遞*/
case VARACT:
/*找到對應的過程調用語句,作為本基本塊的結束*/
code = code->next;
while (code->codeR.codekind != CALL)
code = code->next;
/*從下一條語句開始,進入一個新的基本塊*/
if (code->next!= NULL)
{ code = code->next;
baseBlock[blocknum++] =code;
}
default : break;
}
code = code->next;
}
return(blocknum);
}
/********************************************************/
/* 函數名 PrintBaseBlock */
/* 功 能 打印基本塊第一條代碼,以顯示基本塊的劃分 */
/* 說 明 */
/********************************************************/
void PrintBaseBlock(int blocknum)
{
for (int i=0;i<blocknum ;i++)
{ PrintOneCode(baseBlock[i]);
fprintf(listing,"\n");
}
}
/**************用于公共表達式優化*****************/
/********************************************************/
/* 函數名 printValuNum */
/* 功 能 輸出值編碼表 */
/* 說 明 */
/********************************************************/
void printValuNum()
{
fprintf(listing,"\n************Value Num **************\n");
ValuNum *Item = valuNumT;
while (Item!=NULL)
{
PrintContent(Item->arg);
fprintf(listing," ");
if (Item->access == indir )
{ fprintf(listing ,"(");
fprintf(listing,"%d",Item->codeInfo.twoCode.valuecode);
fprintf(listing," , ");
fprintf(listing,"%d",Item->codeInfo.twoCode.addrcode);
fprintf(listing,")");
}
else fprintf(listing,"%d",Item->codeInfo.valueCode);
fprintf(listing,"\n");
Item = Item->next;
}
}
/********************************************************/
/* 函數名 printUsableExpr */
/* 功 能 輸出可用表達式編碼表 */
/* 說 明 */
/********************************************************/
void printUsbleExpr()
{
fprintf(listing,"\n************Usable Expression**************\n");
UsableExpr *Item = usableExprT;
while (Item!=NULL)
{ PrintOneCode(Item->code);
fprintf(listing, " : ");
fprintf(listing,"(");
fprintf(listing,"%d ",Item->mirrorC->op1);
fprintf(listing,"%d ",Item->mirrorC->op2);
fprintf(listing,"%d ",Item->mirrorC->result);
fprintf(listing,")");
fprintf(listing,"\n");
Item = Item->next;
}
}
/********************************************************/
/* 函數名 printTempEqua */
/* 功 能 輸出臨時變量等價表 */
/* 說 明 */
/********************************************************/
void printTempEqua()
{
fprintf(listing,"\n************Temp Equa **************\n");
TempEqua * Item = tempEquaT;
while (Item!=NULL)
{ PrintContent(Item->arg1);
fprintf(listing," ");
PrintContent(Item->arg2);
fprintf(listing,"\n");
Item = Item->next;
}
}
/*循環信息棧的壓棧實現過程*/
void PushLoop(LoopInfo *t)
{
LoopStack *p = (LoopStack *)malloc (sizeof(LoopStack));
p->loopInfo = t ;
p->under=loopTop;
loopTop = p;
loopStackEmpty = false;
}
/*循環信息棧的彈棧實現過程*/
LoopInfo *PopLoop()
{
LoopStack *p=NULL;
LoopInfo *backpointer = NULL;
p =loopTop;
backpointer=p->loopInfo;
loopTop=loopTop->under;
free(p);
if (loopTop==NULL)
loopStackEmpty = true;
return backpointer;
}
/***********************************************************/
/***********以下為目標代碼生成時所用的函數******************/
/***********************************************************/
/***********************************************************/
/* 函數名 FindSp */
/* 功 能 找到該變量所在AR的sp */
/* 說 明 */
/***********************************************************/
void findSp(int varlevel)
{
/*先求該變量層數在AR中的位置,其中varLevel表示變量所在層*/
emitRM("LDA",ac1,varlevel,displayOff," var process");
/*絕對地址*/
emitRO("ADD",ac1,ac1,sp," var sp relative address");
/*該變量所在AR的sp地址存在ac1中*/
emitRM("LD",ac1,0,ac1," var sp");
}
/***********************************************************/
/* 函數名 FindAdd */
/* 功 能 計算基本類型變量、下標變量和域變量的絕對地址 */
/* 說 明 將絕對地址存入ac中 */
/***********************************************************/
void FindAdd(TreeNode * t)
{
int Loc;
int varLevel;
fieldChain * fieldMem = NULL;
if(t!=NULL)
{
/*得到該變量在符號表中的地址*/
Loc = t->table[0]->attrIR.More.VarAttr.off;
/*記錄該變量所在層*/
varLevel = t->table[0]->attrIR.More.VarAttr.level;
/*可能是下標類型或者域類型或者是基本變量類型,把地址取出送入ac*/
/*普通變量*/
if(t->child[0] == NULL)
{
emitRM("LDC",ac,Loc,0," base type var relative address");
}
/*數組類型變量*/
else if(t->attr.ExpAttr.varkind==ArrayMembV)
{
/*將數組下標值送入ac中*/
cGen(t->child[0]);
/*數組下屆存入ac1中*//*attr.ArrayAttr.low*/
emitRM("LDC",ac1,t->table[0]->attrIR.idtype->More.ArrayAttr.low,0,"array low bound");
/*要用ac減去數組下屆*/
emitRO("SUB",ac,ac,ac1,"");
/*求出該數組變量的偏移*/
emitRM("LDA",ac,Loc,ac," array type var relative address");
}
/*記錄類型變量*/
else if(t->attr.ExpAttr.varkind==FieldMembV)
{
/*處理域變量的偏移*/
fieldMem = t->table[0]->attrIR.idtype->More.body;
/*在域表中查找該域變量*/
while(fieldMem != NULL)
{
int result = strcmp(t->child[0]->name[0],fieldMem->id);
/*如果相等*/
if(result==FALSE)
break;
else
fieldMem = fieldMem->Next;
}
/*域變量為基本類型變量*/
if(t->child[0]->child[0]==NULL)
{
emitRM("LDC",ac,Loc,0,"");
emitRM("LDA",ac,fieldMem->off,ac,"field type var relative address");
/*此時ac中存放的是相對偏移*/
}
/*域變量是數組變量的情況*/
else
{
genExp(t->child[0]->child[0]);
emitRM("LDC",ac1,t->child[0]->attr.ArrayAttr.low,0,"array low");
/*數組下標減去下屆*/
emitRO("SUB",ac,ac,ac1,"");
emitRM("LDA",ac,fieldMem->off,ac,"");
emitRM("LDA",ac,Loc,ac,"");
}/*ac中存儲的是域變量的在當前AR的偏移*/
}
/*計算該變量的sp*/
findSp(varLevel);
/******找到sp*****************/
/* 計算絕對偏移 */
emitRO("ADD",ac,ac,ac1," var absolute off");
}
}
/**************************************************/
/****************釋放指針空間部分******************/
/**************************************************/
void freeDec(TreeNode * p);
void freeStm(TreeNode * p);
void freeExp(TreeNode * t);
/***********************************************************/
/* 函數名 freeTree */
/* 功 能 通過遞歸調用釋放指針空間 */
/* 說 明 */
/***********************************************************/
void freeTree(TreeNode * t)
{
TreeNode * p = NULL;
if(t!=NULL)
free(t->child[0]);
p = t->child[1];
freeDec(p);
p = t->child[2]->child[0];
freeStm(p);
}
/***********************************************************/
/* 函數名 freeDec */
/* 功 能 通過遞歸調用釋放聲明類型指針空間 */
/* 說 明 */
/***********************************************************/
void freeDec(TreeNode * p)
{
TreeNode * p1 = NULL;
TreeNode * p2 = NULL;
while(p!=NULL)
{
switch(p->nodekind)
{
case TypeK:
case VarK:
/*p1指向類型聲明節點鏈或者變量聲明節點鏈*/
p1 = p->child[0];
while(p1!=NULL)
{
p2 = p1->sibling;
free(p1);
p1 = p2;
}
break;
case ProcDecK:
/*p1指向函數聲明節點的第一個兒子節點--形參節點*/
p1 = p->child[0];
while(p1!=NULL)
{
p2 = p1->sibling;
free(p1);
p1 = p2;
}
/*p1指向函數聲明節點的第二個兒子結點--聲明節點*/
p1 = p->child[1];
freeDec(p1);
/*p1指向函數聲明節點的三個兒子節點--函數體節點*/
p1 = p->child[2];
freeStm(p1);
break;
}
p1 = p->sibling;
free(p);
p = p1;
}
}
/***********************************************************/
/* 函數名 freeStm */
/* 功 能 通過遞歸調用釋放語句類型指針空間 */
/* 說 明 */
/***********************************************************/
void freeStm(TreeNode * p)
{
TreeNode * t = p;
TreeNode * p1 = NULL;
TreeNode * p2 = NULL;
while(t!=NULL)
{
switch(t->kind.stmt)
{
case IfK:
/*刪除條件表達式節點*/
freeExp(t->child[0]);
/*刪除then語句序列部分*/
freeStm(t->child[1]);
/*刪除else語句序列部分*/
freeStm(t->child[2]);
break;
case WhileK:
/*刪除條件表達式節點*/
freeExp(t->child[0]);
/*刪除while語句序列部分*/
freeStm(t->child[1]);
break;
case AssignK:
/*刪除賦值號左側*/
p1 = t->child[0];
freeExp(p1);
/*刪除賦值號右側*/
p1 = t->child[1];
freeExp(p1);
break;
case ReadK:
break;
case WriteK:
/*刪除兒子節點*/
freeExp(t->child[0]);
break;
case CallK:
/*刪除左兒子(調用函數名)*/
freeExp(t->child[0]);
/*刪除右兒子節點(實參鏈)*/
p1 = t->child[1];
while(p1 !=NULL)
{
p2 = p1->sibling;
freeExp(p1);
p1 = p2;
}
break;
case ReturnK:
break;
}
p1 = t->sibling;
free(t);
t = p1;
}
}
/***********************************************************/
/* 函數名 freeExp */
/* 功 能 通過遞歸調用釋放表達式類型指針空間 */
/* 說 明 */
/***********************************************************/
void freeExp(TreeNode * t)
{
TreeNode * p1 = NULL;
TreeNode * p2 = NULL;
switch(t->kind.exp)
{
case OpK:
/*刪除左操作數*/
freeExp(t->child[0]);
/*刪除右操作數*/
freeExp(t->child[1]);
break;
case ConstK:
/*直接刪除該節點*/
free(t);
break;
case VariK:
p1 = t;
while (p1!=NULL)
{
p2 = p1->child[0];
free(p1);
p1 = p2;
}
break;
}
}
/***********************************************************/
/* 函數名 freeTable */
/* 功 能 通過遞歸調用釋放符號表空間 */
/* 說 明 */
/***********************************************************/
void freeTable(void)
{
SymbTable * p1=NULL;
SymbTable * p2=NULL;
int i = 0;
while(scope[i]!=NULL)
{
p1 = scope[i];
while(p1!=NULL)
{
p2 = p1->next;
free(p1);
p1 = p2;
}
i++;
}
}
/***********************************************************/
/* 函數名 freeMidCode */
/* 功 能 釋放中間代碼空間 */
/* 說 明 */
/***********************************************************/
void freeMidCode(void)
{
CodeFile *code = firstCode;
CodeFile *code1 = NULL;
while(code!=NULL)
{
code1 = code;
code = code->next;
free(code1);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -