




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、實驗三 中間的代碼優(yōu)化某些編譯程序在中間代碼或目標(biāo)代碼生產(chǎn)之后要對其進(jìn)行優(yōu)化,所謂優(yōu)化就是對代碼進(jìn)行等價的變換。而變換后的代碼運(yùn)行結(jié)果與變換前的代碼運(yùn)行結(jié)果相同。而運(yùn)行速度加快或占用內(nèi)存空間減少。中間的代碼優(yōu)化就是對中間代碼進(jìn)行等價的變換。 基本塊的有向圖 DAG(Directed Acyclic Graph)有向圖中任何一條通路都不是環(huán)路,則稱該有向圖為無環(huán)路有向圖,簡稱為DAG。一、 實驗題目:中間代碼的局部優(yōu)化二、 實驗?zāi)康模赫莆站植績?yōu)化方法、提高機(jī)器的運(yùn)行速度三、實驗內(nèi)容:1 、構(gòu)造基本塊內(nèi)的優(yōu)化DAG 假設(shè):(1) ni 為已知結(jié)點(diǎn)號,n為新結(jié)點(diǎn)號;(2)訪問各結(jié)點(diǎn)信息時,按結(jié)點(diǎn)號逆
2、序排序2、完成對下例三類表達(dá)式的優(yōu)化(1)常值表達(dá)式的優(yōu)化(2)公共表達(dá)式的優(yōu)化(3)無用賦值的優(yōu)化3、輸出根據(jù)優(yōu)化的DAG重組四元式四、設(shè)計概要:首先要實現(xiàn)表達(dá)式中間代碼生成,采用遞歸下降子程序法實現(xiàn)。ET0 “push(SYN,w)”T“QUAT” TF1”push(SYN,w)”F“QUAT”Fi“push(SEM,entry(w)”|(E)其中:·push(SYN,w)-當(dāng)前單詞w入符號棧SYN;·push(SEM,entry(w)- 當(dāng)前i在符號表中的入口值壓入語義棧SEM;·QUAT-生成四元式函數(shù) T:=newtemp; QTj=(SYNk,SEMs
3、-1,SEMs,T);j+; pop(SYN,_);pop(SEM,_);pop(SEM,_); push(SEM,T);在對中間代碼進(jìn)行局部優(yōu)化五、程序代碼及運(yùn)行結(jié)果:1.表達(dá)式中間代碼生成#include<iostream>#include<cstdlib>using namespace std;char str50;char sem50;char syn50;char ch;int i=0;int j=0;int n=0;int p=1;void push_sem(char w)semj+=w;void push_syn(char w)synn+=w;void G
4、en()char s22;char w;w=sem-j;if(w>='1'&&w<='9')s01=w;s00=sem-j;elses00=w;s01=' 'w=sem-j;if(w>='1'&&w<='9')s11=w;s10=sem-j;elses10=w;s11=' 'cout<<"("<<syn-n<<","<<s10<<s11<&
5、lt;","<<s00<<s01<<","<<'t'<<p+<<")"<<endl;push_sem('t');push_sem(p+47);int F()int m;int E();if(ch='(')ch=stri+;m=E();if(ch=')') ch=stri+;else/cout<<"表達(dá)式error!"<<endl;return 0
6、;else if(ch>='a'&&ch<='z')|(ch>='1'&&ch<='9') push_sem(ch);ch=stri+;else/cout<<"表達(dá)式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=stri+;
7、 m=F(); if(m=0)return 0; Gen(); else if(ch='/') push_syn(ch); ch=stri+; 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=stri+; m=T(); if(m=0)return 0; Gen(); else if(ch='-') push_syn(ch
8、); ch=stri+; l=T(); if(l=0)return 0; Gen();else break;return 1;int main()int k,q=0;char w1,w2,w;char s12;cout<<"輸入表達(dá)式(以'#'結(jié)束):"cin>>str;w1=stri+;w2=stri+;if(w2!='=') i=i-2;q=1;ch=stri+;k=E();if(q=0)w=sem-j; if(w>='1'&&w<='9') s01=w;
9、 s00=sem-j;elses00=w;s01=' 'cout<<"("<<w2<<","<<s00<<s01<<","<<" "<<","<<w1<<")"<<endl;if(k=0) cout<<"error!"<<endl;elseif(ch='#') cout&
10、lt;<"OK!"<<endl;else cout<<"error!"<<endl;return 0;運(yùn)行結(jié)果:2.代碼優(yōu)化:(采用遞歸下降子程序法判斷表達(dá)式是否合法,方法如上)#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 prog100; char ch;char syn
11、20,sem503; void SEM(void) int i,j;for(i=0;i<50;i+)for(j=0;j<3;j+)semij='0'struct quat/四元式結(jié)構(gòu)char result8;char ag18;char op;char ag28;quad25,newquad15;struct Ni/節(jié)點(diǎn)結(jié)構(gòu)int pre2;char op;char bz258;N25;void newN(void)int l,j;i+;for(j=0;j<25;j+)for(l=0;l<8;l+)Ni-1.bzjl='0'for(j=0
12、;j<2;j+)Ni-1.prej=0;Ni-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 sz503);void push1(char sz503,char x3);void quat1(void); void quat0(char w);void print1(void)
13、;void print2(void);char *newT(void)char *p;char m8;p=(char *)malloc(8);Ti+;itoa(Ti,m,10);strcpy(p+1,m);p0='t'return(p);void main()p=0;syn0='#'SEM();sem00='#'cout<<"請輸入表達(dá)式:"<<endl; docin.get(ch);if(ch != 'n') progp+=ch;while(ch!='#');p=0;c
14、h=progp+;while(ch!='#')fuzhi();print1();dagt();newquat();print2();void fuzhi(void)char temp3;temp0='0'temp1='0'temp2='0'if(ch<='z'&&ch>='a')|(ch<='Z'&&ch>='A')temp0=ch;push1(sem,temp);ch=progp+;if(ch='=
15、39;)push0(syn,ch);ch=progp+;E();if(m=0)cout<<"錯誤1!"<<endl;system("pause"); /return;if(ch='')ch=progp+;quat1();elsecout<<"錯誤2!"<<endl;system("pause"); return;elsecout<<"錯誤3!"<<endl;system("pause");
16、 return;elsecout<<"錯誤4!"<<endl;printf("%d",ch);system("pause"); return;/E、T、F是遞歸下降子程序的語法分析void E(void)char w;T();while(ch='+'|ch='-')push0(syn,ch);w=synstrlen(syn)-1;ch=progp+;T();quat0(w);void T(void)char w;F();while(ch='*'|ch='/
17、')push0(syn,ch);w=synstrlen(syn)-1;ch=progp+;F();quat0(w);void F(void)char temp3;temp0='0'temp1='0'temp2='0'if(ch='(')ch=progp+;E();if(ch=')')ch=progp+;else m=0;else if(ch<='z'&&ch>='a')|(ch<='Z'&&ch>=
18、9;A')|(ch<='9'&&ch>='0')temp0=ch;push1(sem,temp);ch=progp+; else m=0;void push0(char sz,char x)int top;top=strlen(sz);sztop=x;top+;sztop+1='0'void pop0(char sz)int top;top=strlen(sz)-1;sztop='0'void push1(char sz503,char x3)int top=1;while(sztop0)top
19、+;strcpy(sztop,x);top+;sztop+10='0'void pop1(char sz503)int top=1;while(sztop0)top+;top-;sztop0='0'sztop1='0'sztop2='0'void quat0(char w)int top=1,i;char *p;while(semtop0)top+;strcpy(quadj.ag1,semtop-2);strcpy(quadj.ag2,semtop-1);quadj.op=w;p=newT();for(i=0;i<8;i+)
20、quadj.resulti=pi;pop1(sem);top-;pop1(sem);top-;for(i=0;i<3;i+)semtopi=quadj.resulti;semtop2='0'j+;void quat1(void)char ag28;int top,i;top=1;while(semtop0)top+;ag20='_'for(i=1;i<8;i+)ag2i='0'strcpy(quadj.ag1,semtop-1);strcpy(quadj.ag2,ag2);quadj.op='='strcpy(quad
21、j.result,semtop-2);pop0(syn);pop1(sem);pop1(sem);j+;void print1(void)int i;cout<<"原來的四元組:"<<endl;for(i=0;i<j;i+)cout<<(i+1)<<"、("<<quadi.op<<","<<quadi.ag1<<","<<quadi.ag2<<","<<qua
22、di.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(quadm.ag1,Nn-1.bzl)=0)tag=n;break;if(tag!=0)tag1=tag-1;if('0'<quadm.ag10&&quadm.ag10<'9')goto N3;
23、else goto N3;elseif('0'<quadm.ag10&&quadm.ag10<'9')if(quadm.ag20!='_')if('0'<quadm.ag20&&quadm.ag20<'9')quadm.ag10=quadm.ag10-'0'quadm.ag20=quadm.ag20-'0'switch(quadm.op)case '+':temp=quadm.ag10+quadm.ag20;br
24、eak;case '-':temp=quadm.ag10-quadm.ag20;break;case '*':temp=quadm.ag10*quadm.ag20;break;case '/':temp=quadm.ag10/quadm.ag20;break;default:break;tag=0;for(n=i;n>0;n-)for(l=0;l<25;l+)if(strcmp(quadm.result,Nn-1.bzl)=0)tag=n;break;if(tag!=0)continue;elsenewN();Ni-1.bz00=te
25、mp+'0' ;strcpy(Ni-1.bz1,quadm.result);continue;else newN();tag1=i-1;strcpy(Ni-1.bz0,quadm.ag1);goto N2;else goto N1;else N1:newN();strcpy(Ni-1.bz0,quadm.ag1);tag1=i-1;N3:if(quadm.ag20='_')tag=0;for(n=i;n>0;n-)for(l=0;l<25;l+)if(strcmp(quadm.result,Nn-1.bzl)=0)tag=n;top=l;break;
26、if(tag!=0)for(l=top+1;l<25;l+)strcpy(Ntag-1.bzl-1,Ntag-1.bzl);goto N5;elseN5:if(Ni-1.bz01)if(quadm.result1)top=0;while(Ntag1.bztop0)top+;strcpy(Ntag1.bztop,quadm.result);continue;elsetemp=Ni-1.bz01;strcpy(Ni-1.bz0,quadm.result);top=0;while(Ntag1.bztop0)top+;Ni-1.bztop0='t'Ni-1.bztop1=temp
27、;continue;elsetop=0;while(Ntag1.bztop0)top+;strcpy(Ntag1.bztop,quadm.result);continue;elseN2:tag=0;for(n=i;n>0;n-)for(l=0;l<25;l+)if(strcmp(quadm.ag2,Nn-1.bzl)=0)tag=n;break;if(tag!=0)tag2=tag-1;tag=0;for(n=i;n>0;n-)if(Nn-1.pre0=tag1)&&(Nn-1.pre1=tag2)tag=n;break;if(tag!=0)if(Ntag-1
28、.op=quadm.op)if(!Ntag-1.bz01)top=1;while(Ntag-1.bztop0)top+;strcpy(Ntag-1.bztop,quadm.result);else if(!quadm.result1)temp=Ntag-1.bz01;strcpy(Ntag-1.bz0,quadm.result);top=1;while(Ntag-1.bztop0)top+;Ntag.bztop0='t'Ntag.bztop1=temp; else top=1; while(Ntag-1.bztop0) top+; strcpy(Ntag-1.bztop,qua
29、dm.result); continue;elsenewN();Ni-1.op=quadm.op;strcpy(Ni-1.bz0,quadm.result);Ni-1.pre0=tag1;Ni-1.pre1=tag2;continue;elsenewN();Ni-1.op=quadm.op;strcpy(Ni-1.bz0,quadm.result);Ni-1.pre0=tag1;Ni-1.pre1=tag2;continue;elsenewN();strcpy(Ni-1.bz0,quadm.ag2);tag2=i-1;tag=0;for(n=i;n>0;n-)for(l=0;l<25;l+)if(strcmp(quadm.result,Nn-1.bzl)=0)tag=n;top=l;break;if(tag=0)newN();strcpy(Ni-1.bz0,quadm.result);Ni-1.op=quadm.op;Ni-1.pre0=tag1;Ni-1.pre1=tag2;continue;elsefor(l=top+1;l<25;l+)strcpy(Ntag-1.bzl-1,Ntag-1.bzl);newN();strcpy(Ni-1.bz0,quadm.result);N
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 臺球助教套路合同范本
- T/CADBM 45-2021涂覆材料凈化性能檢測方法環(huán)境測試艙法
- 建設(shè)工程合伙施工協(xié)議書
- 嘉興品牌投資理財協(xié)議書
- 土地直補(bǔ)過戶合同范本
- 農(nóng)村山林流轉(zhuǎn)合同范本
- 小孩子暑假工合同范本
- 德國合同到期延期協(xié)議書
- 買賣安置房屋合同范本
- 駕駛員事故合同范本
- 大型商場裝修施工組織設(shè)計方案
- 【MOOC】材料力學(xué)-西北工業(yè)大學(xué) 中國大學(xué)慕課MOOC答案
- 《英語翻譯》教案全套 陳霞 第1-8章 中西方翻譯史 - 文體翻譯
- 人教版(2024)八年級上冊物理期中模擬試卷3套(含答案)
- DB11∕T 2115-2023 機(jī)械式停車設(shè)備使用管理和維護(hù)保養(yǎng)安全技術(shù)規(guī)范
- 北京市通州區(qū)2023-2024學(xué)年四年級下學(xué)期語文期末試卷
- 2024年四川省綿陽市中考學(xué)情調(diào)查地理試題(原卷版)
- 穿越時空的音樂鑒賞之旅智慧樹知到期末考試答案章節(jié)答案2024年浙江中醫(yī)藥大學(xué)
- 重慶市藻渡水庫工程環(huán)境影響報告書-上報
- DZ∕T 0207-2020 礦產(chǎn)地質(zhì)勘查規(guī)范 硅質(zhì)原料類(正式版)
- 云南省勞動合同范本下載
評論
0/150
提交評論