四元式序列转换虚拟机目标代码_第1页
四元式序列转换虚拟机目标代码_第2页
四元式序列转换虚拟机目标代码_第3页
四元式序列转换虚拟机目标代码_第4页
四元式序列转换虚拟机目标代码_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、四元式序列转换虚拟机目标代码、实验目的1、熟悉和掌握四元式序列转换成目标代码的原理2、设计一个程序,能使给定的任一四元式转换成虚拟目标代码。 、实验步骤1、需求分析(1)、产生四元式序列翻译方案设计对于产生四元式序列的翻译方案的设计,以下面的作为例子 关于赋值语句的四元式序列的翻译方案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)、四元式生成目标代码从四元式序列生成目标代码的工作的主要问题是运算分量与计算结果的 存取问题,在生成目标指令时,要考虑四元式中运算分量是在寄存器中还是在内 存中。当在寄存器中时,以后还会被使用否,等等。例如,对于四元式- x y z如果x和y都不在寄存器中,则可生成下列目标指令:MOV x, RiSUB y, Ri计算结果z在寄存器Ri中。如果寄存器Ri与寄存器Rj分别包含x与y。及X与 y的值分别在寄存器Ri与Rj,且此四元式后不再引用x,可以为其生成目标指令:SUB Rj, Ri计算结果z在Ri中。如果寄存器Ri包含x而y在内存单元,且此四元式后不再以用

4、X,可以生成目标指令:RiSUBy,或者y,Rj , Ri计算结果z仍然在Ri中所以,生成目标代码时应考察四元式及其上下文。 针对具体情况生成 合适的目标指令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);/数

10、字转换字符 _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. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论