1、 四元式序列转换虚拟机目标代码 一、实验目的 1、熟悉和掌握四元式序列转换成目标代码的原理 2、设计一个程序,能使给定的任一四元式转换成虚拟目标代码。 二、实验步骤 1、需求分析 (1)、产生四元式序列翻译方案设计 对于产生四元式序列的翻译方案的设计,以下面的作为例子 关于赋值语句的四元式序列的翻译方案 S::=id=E{p:=lookup(id.name); if p≠NULL then genquad('::',E.place,''*p.place) else error}
2、 E::=E1+E2{E.place:=newtemp; Fenquad('+', E1 .place, E2 .place,E.place)} E::= E1 * E2 { E.place:=newtemp; Fenquad('*', E1 .place, E2 .place,E.place)} E::= - E1 {E.place:=newtemp; Genquad('NEG', E1.place,'',E.place)} E::=( E1){E.place:= E
3、1 .place} E::=id{p:=lookup(id.name); if p≠NULL then E.place:=*p.place else error} 其中,过程genquad(op,x,y,z)功能是生成四元式: op x y z (2)、四元式生成目标代码 从四元式序列生成目标代码的工作的主要问题是运算分量与计算结果的存取问题,在生成目标指令时,要考虑四元式中运算分量是在寄存器中还是在内存中。当在寄存器中时,以后还会被使用否,等等。例如,对于四元式 - x
4、y z 如果x和y都不在寄存器中,则可生成下列目标指令: MOV x, Ri SUB y, Ri 计算结果z在寄存器Ri中。如果寄存器Ri与寄存器Rj分别包含x与y。及x 与y的值分别在寄存器Ri 与Rj,且此四元式后不再引用x,可以为其生成目标指令: SUB Rj , Ri 计算结果z在Ri中。如果寄存器Ri包含x而y在内存单元,且此四元式后不再以用x,可以生成目标指令: SUB y, Ri 或者
5、 MOV y, Rj
SUB Rj , Ri
计算结果z仍然在Ri中
所以,生成目标代码时应考察四元式及其上下文。针对具体情况生成合适的目标指令
3、程序的源程序
(1)、source.cpp
#include
6、de*dl,struct four **temp,int &Rcount,char*item1,char *item2,int i); char karray[][4]={"+","-"," ","-","*","/"," ","<",">","<=",">=",":=","<>","go","[]="}; char array[17][7]={"add","sub","mov","neg","mpy","div","cmp","cj<","cj>","cj<=","cj>=","cj=","cj<>","goto","itof","return","call"}; struct fou
7、r { char item[4][4]; struct four*next; }; void init_array0(char ay[][4],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 parray[][4],int length) { struct four*temp=
8、fr);
struct four*t;
while(temp->next!=NULL)
temp=temp->next;
t=new struct four();
t->next=NULL;
for(int i=0;i
9、0)
return i;
}
return -1;
}
void add_char(char *sym,int len,char ch)
{
int i=0;
while(i 10、 while(i 11、0;i 12、e(ch!=' '&&ch!='\n'&&ch!=EOF)
{
add_char(array1,4,ch);
ch=fgetc(fp);
}
while(ch==' '&&ch!=EOF) //+ a b t1
ch=fgetc(fp);
if(ch!=' '&&ch!='\n'&&ch!=EOF)
{
add_array(array0,count,array1);
count=(++count)%4;
fseek(fp,-1,SEEK_CUR);
init_array1(array1,4);
}
13、
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 s[6];
struct four**temp=f 14、r;
int Rcount=0;
char item1[4],item2[4];
while((*temp)->next!=NULL)
{
//char karray[][3]={"+","-"," ","-","*","/"," ","<",">","<=",">=",":=","<>","go"};
i=change_style((*temp)->next->item[0]);
if(i==-1)
break;
else
change_to_source(ptemp,s,dl,temp,Rcount,item1,it 15、em2,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< 16、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,head2);//_itoa
if(fp==NULL)
{ cout<<"DDD"< 17、se(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 "< 18、y[i]<<" "< 19、
p=p->prior;
}
else
{
strcpy(item1,p->prior->source);
break;
}
}
p=dl;
init_array1(item2,4);
while(p->prior!=dl)
{
if(strcmp(temp->next->item[2],p->prior->destion)!=0)
{
p=p->prior;
}
else
{
strcpy(item2,p->pr 20、ior->source);
break;
}
}
if(item1[0]!='\0')
{
if(item2[0]!='\0')//////////////////////////
ptemp< 21、]<<" "< 22、 }
change(s,'R',Rcount);
ListInsert(dl,s,temp->next->item[3]);
}
}
(2)、source.h
#include 23、)
{
*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->next;
s0=new DLNode();
if(s==NULL)
return 0;
strcpy(s0- 24、>source,s);
strcpy(s0->destion,d);
s0->prior=p->prior;
p->prior->next=s0;
s0->next=p;
p->prior=s0;
return 1;
}
bool empty(DLNode*head)
{
if(head->next==head)
return true;
return false;
}
//int ListDelete(
void ListOutput(DLNode*head)
{ DLNode*temp=head;
while(temp 25、>next!=head)
{
cout<<"source "<






