Three-Address Code Generation: Suppose we have the following C code. int dtrsv(c
ID: 3836993 • Letter: T
Question
Three-Address Code Generation:
Suppose we have the following C code.
int dtrsv(const int M, const double *A,const int lda,
double *X,const double *Y)
{
int i,j;
for (i = 0; i < M; i += 1)
{
for (j = 0; j < i; j += 1)
{
X[i] = X[i] - A[j*lda+i] * Y[j];
}
}
return i;
}
Convert the given code to three-address code. Please rst clarify your assumptions about
the memory layout of this functions' activation record. Your three address code could use
the following types of instructions, where x, z are register names, a is a constant value, y is
a constant or a register, and L1/L2 are labels.
loadi a => x; load x, a => z; store z => x, a; mov x => y;
mult x y => z; multi x, a => z;
add x, y => z; addi x, a => z;
minus x, y=>z; minusi x, a => z;
if (x < y) goto L1; goto L2; if (x <=y) goto L1; jump x;
Regards,
Explanation / Answer
Following is the C code for Three address code generator.
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct three
{
char data[10],temp[7];
}s[30];
void main()
{
char *d1,*d2;
int i=0,len=0;
FILE *f1,*f2;
clrscr();
f1=fopen("exe1.txt","r");
f2=fopen("exe2.txt","w");
while(fscanf(f1,"%s",s[len].data)!=EOF)
len++;
for(i=0;i<=len;i++)
{
if(!strcmp(s[i].data,"="))
{
fprintf(f2," LDA %s",s[i+1].data);
if(!strcmp(s[i+2].data,"+"))
fprintf(f2," ADD %s",s[i+3].data);
if(!strcmp(s[i+2].data,"-"))
fprintf(f2," SUB %s",s[i+3].data);
fprintf(f2," STA %s",s[i-1].data);
}
}
fclose(f1);
fclose(f2);
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.