![四元式序列轉(zhuǎn)換虛擬機目標(biāo)代碼_第1頁](http://file3.renrendoc.com/fileroot_temp3/2022-1/21/511d6b93-8ca4-40b3-9781-7b93e83680ec/511d6b93-8ca4-40b3-9781-7b93e83680ec1.gif)
![四元式序列轉(zhuǎn)換虛擬機目標(biāo)代碼_第2頁](http://file3.renrendoc.com/fileroot_temp3/2022-1/21/511d6b93-8ca4-40b3-9781-7b93e83680ec/511d6b93-8ca4-40b3-9781-7b93e83680ec2.gif)
![四元式序列轉(zhuǎn)換虛擬機目標(biāo)代碼_第3頁](http://file3.renrendoc.com/fileroot_temp3/2022-1/21/511d6b93-8ca4-40b3-9781-7b93e83680ec/511d6b93-8ca4-40b3-9781-7b93e83680ec3.gif)
![四元式序列轉(zhuǎn)換虛擬機目標(biāo)代碼_第4頁](http://file3.renrendoc.com/fileroot_temp3/2022-1/21/511d6b93-8ca4-40b3-9781-7b93e83680ec/511d6b93-8ca4-40b3-9781-7b93e83680ec4.gif)
![四元式序列轉(zhuǎn)換虛擬機目標(biāo)代碼_第5頁](http://file3.renrendoc.com/fileroot_temp3/2022-1/21/511d6b93-8ca4-40b3-9781-7b93e83680ec/511d6b93-8ca4-40b3-9781-7b93e83680ec5.gif)
下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、四元式序列轉(zhuǎn)換虛擬機目標(biāo)代碼、實驗?zāi)康?、熟悉和掌握四元式序列轉(zhuǎn)換成目標(biāo)代碼的原理2、設(shè)計一個程序,能使給定的任一四元式轉(zhuǎn)換成虛擬目標(biāo)代碼。 、實驗步驟1、需求分析(1)、產(chǎn)生四元式序列翻譯方案設(shè)計對于產(chǎn)生四元式序列的翻譯方案的設(shè)計,以下面的作為例子 關(guān)于賦值語句的四元式序列的翻譯方案S:=id=E p:=look up ();if p 工 NULL then genquad( ' : ' ,E.place, " *p.place) else errorE:=E1+E2E .pl ace:=newte mp;.p lace,E. place)Fenqua
2、d(' + ' , E 1 .place, EE:= e 1 * E 2 E.place:=newtemp;Fenquad(',E 1 . pl ace, E.p lace,E. place)E:= - E E.place:=newtemp;,E. place)Genquad(' NEG , E .place,'E:=( E1)E .pl ace:= E 1 .pl aceE:=idp:=l ook up ();if P 工 NULL then E.p lace:=* p.p laceelse error其中,過程genquad(op,x,
3、y,z)功能是生成四元式:op(2)、四元式生成目標(biāo)代碼從四元式序列生成目標(biāo)代碼的工作的主要問題是運算分量與計算結(jié)果的 存取問題,在生成目標(biāo)指令時,要考慮四元式中運算分量是在寄存器中還是在內(nèi) 存中。當(dāng)在寄存器中時,以后還會被使用否,等等。例如,對于四元式- x y z如果x和y都不在寄存器中,則可生成下列目標(biāo)指令:MOV x, RiSUB y, Ri計算結(jié)果z在寄存器Ri中。如果寄存器Ri與寄存器Rj分別包含x與y。及X與 y的值分別在寄存器Ri與Rj,且此四元式后不再引用x,可以為其生成目標(biāo)指令:SUB Rj, Ri計算結(jié)果z在Ri中。如果寄存器Ri包含x而y在內(nèi)存單元,且此四元式后不再以用
4、X,可以生成目標(biāo)指令:RiSUBy,或者y,Rj , Ri計算結(jié)果z仍然在Ri中所以,生成目標(biāo)代碼時應(yīng)考察四元式及其上下文。 針對具體情況生成 合適的目標(biāo)指令3、程序的源程序、source.cppMOVSUBRj#include<iostream.h>#include<stdio.h>#include<stdlib.h>#include<fstream.h>#include"source.h"void change_to_source(ofstream ptemp,char*s,DLNode*dl,struct four *t
5、emp,int &Rcount,char*item1,char *item2,int i);char karray4="+","-"," ","-","*","/"," ","<",">","<=",">=",":=","<>","go","="char
6、array177="add","sub","mov","neg","mpy","div","cmp","cj<","cj>","cj<=","cj>=","cj=","cj<>","goto","itof","r eturn","ca
7、ll"struct fourchar item44;struct four*next;void init_array0(char ay4,int length);void init_four(struct four *head)*head=new struct four();(*head)->next=NULL;init_array0(*head)->item,4);void add_four(struct four*fr,char parray4,int length) struct four*temp=(*fr); struct four*t;while(temp-&
8、gt;next!=NULL)temp=temp->next;t=new struct four();t->next=NULL;for(int i=0;i<length;i+)strcpy(t->itemi,parrayi);temp->next=t;int change_style(char* item)for(int i=0;i<15;i+)if(strcmp(item,karrayi)=0)return i;return -1;void add_char(char *sym,int len,char ch)int i=0;while(i<len&a
9、mp;&symi!='0')i+;symi=ch;void add_array(char array04,int index,char *array1)strcpy(array0index,array1);void init_array1(char*pch,int len)int i=0;while(i<len&&pchi!='0')pchi='0'i+;void change(char* s,char ch,int count) /char temp5;char temp25;init_array1(s,5);/數(shù)
10、字轉(zhuǎn)換字符 _itoa(count,temp2,10);s0=ch;strcat(s,temp2);void init_array0(char ay4,int length)for(int i=0;i<length;i+)init_array1(ayi,4);void get_from(FILE*fp,struct four*fr)int ch;char array044;char array14;int count=0;init_array1(array1,4);init_array0(array0,4);while(ch=fgetc(fp)!=EOF)while(ch=' &
11、#39;&&ch!=EOF) ch=fgetc(fp);while(ch!=' '&&ch!='n'&&ch!=EOF)add_char(array1,4,ch);ch=fgetc(fp);while(ch=' '&&ch!=EOF) /+ a b t1ch=fgetc(fp);if(ch!=' '&&ch!='n'&&ch!=EOF)add_array(array0,count,array1);count=(+coun
12、t)%4;fseek(fp,-1,SEEK_CUR);init_array1(array1,4);if(ch='n')add_array(array0,count,array1);add_four(&fr,array0,4);count=0;init_array1(array1,4);init_array0(array0,4);void convertor(ofstream ptemp,struct four*fr,DLNode*dl) int i;char s6;struct four*temp=fr;int Rcount=0;char item14,item24;w
13、hile(*temp)->next!=NULL)/char karray3="+","-"," ","-","*","/"," ","<",">","<=",">=",":=","<>","go"i=change_style(*temp)->next->item0)
14、;if(i=-1)break;else change_to_source(ptemp,s,dl,temp,Rcount,item1,item2,i);(*temp)=(*temp)->next;void output(struct four*head) struct four*temp=head;while(temp->next!=NULL) for(int i=0;i<4;i+)cout<<temp->next->itemi<<" "cout<<endl;temp=temp->next;void ma
15、in()FILE*fp=fopen("four.txt","r");ofstream tempp("temp.txt");FILE*fp2=fopen("result.txt","a+");struct four*head=new struct four();init_four(&head);DLNode*head2;ListInitiate(&head2);get_from(fp,head);output(head);convertor(tempp,&head,head
16、2);/_itoa if(fp=NULL) cout<<"DDD"<<endl;exit(0);fclose(fp);ListOutput(head2);void change_to_source(ofstream ptemp,char*s,DLNode*dl,struct four *stemp,int &Rcount,char*item1,char*item2,int i)struct four *temp=(*stemp);if(empty(dl)ptemp<<"mov "<<temp->
17、;next->item1<<" , "<<"R"<<Rcount<<endl;ptemp<<arrayi<<" "<<temp->next->item2<<" , "<<"R"<<Rcount<<endl;change(s,'R',Rcount);ListInsert(dl,s,temp->next->item3);el
18、seDLNode *p=dl;init_array1(item1,4);while(p->prior!=dl)if(strcmp(temp->next->item1,p->prior->destion)!=0)p=p->prior;else strcpy(item1,p->prior->source);break;p=dl;init_array1(item2,4);while(p->prior!=dl)if(strcmp(temp->next->item2,p->prior->destion)!=0) p=p->
19、;prior;elsestrcpy(item2,p->prior->source);break;if(item10!='0')if(item20!='0')/ptemp<<arrayi<<" "<<item1<<" , "<<item2<<endl;else ptemp<<arrayi<<" "<<item1<<" , "<<temp->
20、;next->item2<<endl;elseif(item20!='0') ptemp<<arrayi<<" "<<temp->next->item1<<" , "<<item2<<endl;elseRcount+;ptemp<<"mov "<<temp->next->item1<<" , "<<"R"<<
21、Rcount<<endl;ptemp<<arrayi<<" "<<temp->next->item2<<" , "<<"R"<<Rcount<<endl;change(s,'R',Rcount);/ListInsert(dl,s,temp->next->item3);change(s,'R',Rcount);ListInsert(dl,s,temp->next->item3
22、);(2)、source.h#include<stdlib.h>#include<string.h>struct nodechar source4;char destion4;struct node *next;struct node *prior;typedef struct node DLNode; int ListInitiate(DLNode*head)*head=new DLNode();if(*head)=NULL)return 0;(*head)->prior=*head;(*head)->next=*head;return 1;int ListInsert(DLNode*head,char s,char d) DLNode*p,*s0;p=head->next ;while(p->next!=head)p=p->nex
溫馨提示
- 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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 湘教版數(shù)學(xué)七年級下冊3.2.2《角的度量》聽評課記錄
- (湘教版)七年級數(shù)學(xué)下冊:2.1.4《多項式的乘法》聽評課記錄
- 七年級道德與法治上冊第三單元 師長情誼第六課師生之間第2框師生交往聽課評課記錄(新人教版)
- 人教版七年級數(shù)學(xué)上冊:4.1.2《點、線、面、體》聽評課記錄1
- 湘教版數(shù)學(xué)七年級上冊1.4.1《有理數(shù)的加法》聽評課記錄
- 部編版八年級道德與法治上冊聽課評課記錄《9.1認(rèn)識總體國家安全觀》
- 暑假小學(xué)一年級學(xué)習(xí)計劃
- 三年級下學(xué)期班主任工作計劃
- 出租房屋合同范本
- 知識產(chǎn)權(quán)作價入股協(xié)議書范本
- 2024人教新目標(biāo)(Go for it)八年級英語下冊【第1-10單元】全冊 知識點總結(jié)
- 2025中國移動安徽分公司春季社會招聘高頻重點提升(共500題)附帶答案詳解
- 七年級英語下學(xué)期開學(xué)考試(深圳專用)-2022-2023學(xué)年七年級英語下冊單元重難點易錯題精練(牛津深圳版)
- 杭州市房地產(chǎn)經(jīng)紀(jì)服務(wù)合同
- 放射科護(hù)理常規(guī)
- 新時代中小學(xué)教師職業(yè)行為十項準(zhǔn)則
- 人教版八年級上冊英語1-4單元測試卷(含答案)
- 2024年大宗貿(mào)易合作共贏協(xié)議書模板
- 初中數(shù)學(xué)教學(xué)經(jīng)驗分享
- 新聞記者證600道考試題-附標(biāo)準(zhǔn)答案
- 2024年公開招聘人員報名資格審查表
評論
0/150
提交評論