实验三中间的代码优化某些编译程序在中间代码或目标代码生产之后要对其进行优化,所谓优化就是对代码进行等价的变换。
而变换后的代码运行结果与变换前的代码运行结果相同。
而运行速度加快或占用内存空间减少。
中间的代码优化就是对中间代码进行等价的变换。
基本块的有向图DAG(Directed Acyclic Graph)有向图中任何一条通路都不是环路,则称该有向图为无环路有向图,简称为DAG。
一、实验题目:中间代码的局部优化二、实验目的:掌握局部优化方法、提高机器的运行速度三、实验内容:1 、构造基本块内的优化DAG假设:(1)ni 为已知结点号,n为新结点号;(2)访问各结点信息时,按结点号逆序排序2、完成对下例三类表达式的优化(1)常值表达式的优化(2)公共表达式的优化(3)无用赋值的优化3、输出根据优化的DAG重组四元式四、设计概要:首先要实现表达式中间代码生成,采用递归下降子程序法实现。
E→T{ω0 “push(SYN,w)”T“QUAT” }T→F{ω1”push(SYN,w)”F“QUAT”}F→i“push(SEM,entry(w))”|(E)其中:·push(SYN,w)---当前单词w入符号栈SYN;·push(SEM,entry(w))--- 当前i在符号表中的入口值压入语义栈SEM;·QUAT---生成四元式函数①T:=newtemp;②QT[j]=(SYN[k],SEM[s-1],SEM[s],T);j++;③ pop(SYN,_);pop(SEM,_);pop(SEM,_); push(SEM,T);在对中间代码进行局部优化五、程序代码及运行结果:1.表达式中间代码生成#include<iostream>#include<cstdlib>using namespace std;char str[50];char sem[50];char syn[50];char ch;int i=0;int j=0;int n=0;int p=1;void push_sem(char w){sem[j++]=w;}void push_syn(char w){syn[n++]=w;}void Gen(){char s[2][2];char w;w=sem[--j];if(w>='1'&&w<='9'){s[0][1]=w;s[0][0]=sem[--j];}else{s[0][0]=w;s[0][1]=' ';}w=sem[--j];if(w>='1'&&w<='9'){s[1][1]=w;s[1][0]=sem[--j];}else{s[1][0]=w;s[1][1]=' ';}cout<<"("<<syn[--n]<<","<<s[1][0]<<s[1][1]<<","<<s[0][0]<<s[0][1] <<","<<'t'<<p++<<")"<<endl;push_sem('t');push_sem(p+47);}int F(){int m;int E();if(ch=='('){ch=str[i++];m=E();if(ch==')') ch=str[i++];else{//cout<<"表达式error!"<<endl;return 0;}}else{if((ch>='a'&&ch<='z')||(ch>='1'&&ch<='9')){push_sem(ch);ch=str[i++];}else{//cout<<"表达式error!"<<endl;return 0;}}return 1;}int T(){int k,m,l;k=F();if(k==0){return 0;}while(1){//push_syn(ch);if(ch=='*'){push_syn(ch);ch=str[i++];m=F();if(m==0){return 0;}Gen();}else if(ch=='/'){push_syn(ch);ch=str[i++];l=F();if(l==0){return 0;}Gen();}else break;}return 1;}int E(){int k,m,l;k=T();if(k==0){return 0;}while(1){//push_syn(ch);if(ch=='+'){push_syn(ch);ch=str[i++];m=T();if(m==0){return 0;}Gen();}else if(ch=='-'){push_syn(ch);ch=str[i++];l=T();if(l==0){return 0;}Gen();}else break;}return 1;}int main(){int k,q=0;char w1,w2,w;char s[1][2];cout<<"输入表达式(以'#'结束):";cin>>str;w1=str[i++];w2=str[i++];if(w2!='=') {i=i-2;q=1;}ch=str[i++];k=E();if(q==0){w=sem[--j];if(w>='1'&&w<='9'){s[0][1]=w;s[0][0]=sem[--j];}else{s[0][0]=w;s[0][1]=' ';}cout<<"("<<w2<<","<<s[0][0]<<s[0][1]<<","<<" "<<","<<w1<<")"<<endl;}if(k==0) cout<<"error!"<<endl;else{if(ch=='#') cout<<"OK!"<<endl;else cout<<"error!"<<endl;}return 0;}运行结果:2.代码优化:(采用递归下降子程序法判断表达式是否合法,方法如上)#include <iostream>#include <cstdlib>#include <string.h>using namespace std;int i=1;int j=0,n=0;int p;int m=1;int Ti=0;char prog[100];char ch;char syn[20],sem[50][3];void SEM(void){int i,j;for(i=0;i<50;i++)for(j=0;j<3;j++)sem[i][j]='\0';}struct quat//四元式结构{char result[8];char ag1[8];char op;char ag2[8];}quad[25],newquad[15];struct Ni//节点结构{int pre[2];char op;char bz[25][8];}N[25];void newN(void){int l,j;i++;for(j=0;j<25;j++){for(l=0;l<8;l++){N[i-1].bz[j][l]='\0';}}for(j=0;j<2;j++)N[i-1].pre[j]=0;N[i-1].op='\0';}void dagt(void);void newquat(void);void fuzhi(void);//递归语法分析生成中间代码void E(void);void T(void);void F(void);void pop0(char sz[]);void push0(char sz[],char x);void pop1(char sz[50][3]);void push1(char sz[50][3],char x[3]); void quat1(void);void quat0(char w);void print1(void);void print2(void);char *newT(void){char *p;char m[8];p=(char *)malloc(8);Ti++;itoa(Ti,m,10);strcpy(p+1,m);p[0]='t';return(p);}void main(){p=0;syn[0]='#';SEM();sem[0][0]='#';cout<<"请输入表达式:"<<endl;do{cin.get(ch);if(ch != '\n') prog[p++]=ch;}while(ch!='#');p=0;ch=prog[p++];while(ch!='#'){fuzhi();}print1();dagt();newquat();print2();}void fuzhi(void){char temp[3];temp[0]='\0';temp[1]='\0';temp[2]='\0';if((ch<='z'&&ch>='a')||(ch<='Z'&&ch>='A')) {temp[0]=ch;push1(sem,temp);ch=prog[p++];if(ch=='='){push0(syn,ch);ch=prog[p++];E();if(m==0){cout<<"错误1!"<<endl;system("pause"); /////return;}if(ch==';'){ch=prog[p++];quat1();}else{cout<<"错误2!"<<endl;system("pause");return;}}else{cout<<"错误3!"<<endl;system("pause");return;}}else{cout<<"错误4!"<<endl;printf("%d",ch);system("pause");return;}}//E、T、F是递归下降子程序的语法分析void E(void){char w;T();while(ch=='+'||ch=='-'){push0(syn,ch);w=syn[strlen(syn)-1];ch=prog[p++];T();quat0(w);}}void T(void){char w;F();while(ch=='*'||ch=='/'){push0(syn,ch);w=syn[strlen(syn)-1];ch=prog[p++];F();quat0(w);}}void F(void){char temp[3];temp[0]='\0';temp[1]='\0';temp[2]='\0';if(ch=='('){ch=prog[p++];E();if(ch==')'){ch=prog[p++];}else m=0;}else if((ch<='z'&&ch>='a')||(ch<='Z'&&ch>='A')||(ch<='9'&&ch>='0')) {temp[0]=ch;push1(sem,temp);ch=prog[p++];}else m=0;}void push0(char sz[],char x){int top;top=strlen(sz);sz[top]=x;top++;sz[top+1]='\0';}void pop0(char sz[]){int top;top=strlen(sz)-1;sz[top]='\0';}void push1(char sz[50][3],char x[3]) {int top=1;while(sz[top][0])top++;strcpy(sz[top],x);top++;sz[top+1][0]='\0';}void pop1(char sz[50][3]){int top=1;while(sz[top][0])top++;top--;sz[top][0]='\0';sz[top][1]='\0';sz[top][2]='\0';}void quat0(char w){int top=1,i;char *p;while(sem[top][0])top++;strcpy(quad[j].ag1,sem[top-2]);strcpy(quad[j].ag2,sem[top-1]);quad[j].op=w;p=newT();for(i=0;i<8;i++)quad[j].result[i]=p[i];pop1(sem);top--;pop1(sem);top--;for(i=0;i<3;i++)sem[top][i]=quad[j].result[i];sem[top][2]='\0';j++;}void quat1(void){char ag2[8];int top,i;top=1;while(sem[top][0])top++;ag2[0]='_';for(i=1;i<8;i++)ag2[i]='\0';strcpy(quad[j].ag1,sem[top-1]);strcpy(quad[j].ag2,ag2);quad[j].op='=';strcpy(quad[j].result,sem[top-2]);pop0(syn);pop1(sem);pop1(sem);j++;}void print1(void){int i;cout<<"原来的四元组:"<<endl;for(i=0;i<j;i++)cout<<(i+1)<<"、("<<quad[i].op<<","<<quad[i].ag1<<","<<quad[i].ag2<<","<<quad[i].result<<")"<<endl;}void dagt(void){int m,n,top,l,tag=0,tag1=0,tag2=0;char temp;for(m=0;m<j;m++){tag=0;for(n=i;n>0;n--)for(l=0;l<25;l++){if(strcmp(quad[m].ag1,N[n-1].bz[l])==0){tag=n;break;}}if(tag!=0){tag1=tag-1;if('0'<quad[m].ag1[0]&&quad[m].ag1[0]<'9')goto N3;else goto N3;}else{if('0'<quad[m].ag1[0]&&quad[m].ag1[0]<'9'){if(quad[m].ag2[0]!='_'){if('0'<quad[m].ag2[0]&&quad[m].ag2[0]<'9'){quad[m].ag1[0]=quad[m].ag1[0]-'0';quad[m].ag2[0]=quad[m].ag2[0]-'0';switch(quad[m].op){case '+':temp=quad[m].ag1[0]+quad[m].ag2[0];break;case '-':temp=quad[m].ag1[0]-quad[m].ag2[0];break;case '*':temp=quad[m].ag1[0]*quad[m].ag2[0];break;case '/':temp=quad[m].ag1[0]/quad[m].ag2[0];break;default:break;}tag=0;for(n=i;n>0;n--)for(l=0;l<25;l++){if(strcmp(quad[m].result,N[n-1].bz[l])==0){tag=n;break;}}if(tag!=0)continue;else{newN();N[i-1].bz[0][0]=temp+'0' ;strcpy(N[i-1].bz[1],quad[m].result);continue;}}else{newN();tag1=i-1;strcpy(N[i-1].bz[0],quad[m].ag1);goto N2;}}else goto N1;}elseN1:{newN();strcpy(N[i-1].bz[0],quad[m].ag1);tag1=i-1;N3:if(quad[m].ag2[0]=='_'){tag=0;for(n=i;n>0;n--)for(l=0;l<25;l++){if(strcmp(quad[m].result,N[n-1].bz[l])==0){tag=n;top=l;break;}}if(tag!=0){for(l=top+1;l<25;l++){strcpy(N[tag-1].bz[l-1],N[tag-1].bz[l]);}goto N5;}else{N5:if(N[i-1].bz[0][1]){if(quad[m].result[1]){top=0;while(N[tag1].bz[top][0])top++;strcpy(N[tag1].bz[top],quad[m].result);continue;}else{temp=N[i-1].bz[0][1];strcpy(N[i-1].bz[0],quad[m].result);top=0;while(N[tag1].bz[top][0])top++;N[i-1].bz[top][0]='t';N[i-1].bz[top][1]=temp;continue;}}else{top=0;while(N[tag1].bz[top][0])top++;strcpy(N[tag1].bz[top],quad[m].result);continue;}}}elseN2:{tag=0;for(n=i;n>0;n--)for(l=0;l<25;l++){if(strcmp(quad[m].ag2,N[n-1].bz[l])==0){tag=n;break;}}if(tag!=0){tag2=tag-1;tag=0;for(n=i;n>0;n--)if((N[n-1].pre[0]==tag1)&&(N[n-1].pre[1]==tag2)){tag=n;break;}if(tag!=0){if(N[tag-1].op==quad[m].op){if(!N[tag-1].bz[0][1]){top=1;while(N[tag-1].bz[top][0])top++;strcpy(N[tag-1].bz[top],quad[m].result);}else if(!quad[m].result[1]){temp=N[tag-1].bz[0][1];strcpy(N[tag-1].bz[0],quad[m].result);top=1;while(N[tag-1].bz[top][0])top++;N[tag].bz[top][0]='t';N[tag].bz[top][1]=temp;}else{top=1;while(N[tag-1].bz[top][0])top++;strcpy(N[tag-1].bz[top],quad[m].result);}continue;}else{newN();N[i-1].op=quad[m].op;strcpy(N[i-1].bz[0],quad[m].result);N[i-1].pre[0]=tag1;N[i-1].pre[1]=tag2;}continue;}else{newN();N[i-1].op=quad[m].op;strcpy(N[i-1].bz[0],quad[m].result);N[i-1].pre[0]=tag1;N[i-1].pre[1]=tag2;continue;}}else{newN();strcpy(N[i-1].bz[0],quad[m].ag2);tag2=i-1;tag=0;for(n=i;n>0;n--)for(l=0;l<25;l++)if(strcmp(quad[m].result,N[n-1].bz[l])==0){tag=n;top=l;break;}if(tag==0){newN();strcpy(N[i-1].bz[0],quad[m].result);N[i-1].op=quad[m].op;N[i-1].pre[0]=tag1;N[i-1].pre[1]=tag2;continue;}else{for(l=top+1;l<25;l++){strcpy(N[tag-1].bz[l-1],N[tag-1].bz[l]);}newN();strcpy(N[i-1].bz[0],quad[m].result);N[i-1].op=quad[m].op;N[i-1].pre[0]=tag1;N[i-1].pre[1]=tag2;}}}}}}}void newquat(void){int l,top;for(l=1;l<i;l++){if(N[l].pre[1]==0&&N[l].pre[0]==0){if(!N[l].bz[0][1]){if(('0'<N[l].bz[0][0])&&(N[l].bz[0][0]<'9'))continue;else{for(top=1;N[l].bz[top][1];top++){if(!N[l].bz[top][0]){strcpy(newquad[n].ag1,N[l].bz[0]);newquad[n].ag2[0]='_';newquad[n].op='=';strcpy(newquad[n].result,N[l].bz[top]);n++;}}}}else continue;}else if(N[l].pre[1]!=0||N[l].pre[0]!=0){strcpy(newquad[n].ag1,N[N[l].pre[0]].bz[0]);strcpy(newquad[n].ag2,N[N[l].pre[1]].bz[0]);newquad[n].op=N[l].op;strcpy(newquad[n].result,N[l].bz[0]);n++;if(!N[l].bz[0][1]){for(top=1;N[l].bz[top][0];top++){if(!N[l].bz[top][1]){strcpy(newquad[n].ag1,N[l].bz[0]);newquad[n].ag2[0]='_';newquad[n].op='=';strcpy(newquad[n].result,N[l].bz[top]);n++;}}}}}}void print2(void){int i;cout<<"优化后的代码:"<<endl;for(i=0;i<n;i++)cout<<(i+1)<<"、("<<newquad[i].op<<","<<newquad[i].ag1<<","<<newquad[i].ag2<<","<<newquad[i].result<<")"<<endl;}运行结果:。