Tải bản đầy đủ (.pdf) (6 trang)

Tutorial 6 solutions

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (87.02 KB, 6 trang )

Week 13 Solutions
Code Generation
1. Write Jasmin code for the following MC program:
int main() {
int a,b[3];
int e[3],f;
float c[4],d;
for (f = 1; f < 3; f = f +1)
int foo(boolean e,int f[]) {
e[f] = 0;
if (e && (f[1]==0))
foo(true,e);
a = 1;
repeat
else
b[0] = a + b[0];
b[1] = a = 2;
until (b[0] > 10);
return a;
return 0;
}
}

Change the above MC program to Java program:
public class Cout {
static int a;
static int b[] = new int[3];
static float c[] = new float[4];
static float d;
static int foo(boolean e,int f[]) {
if (e && (f[1] == 0))


a = 1;
else
b[1] = a = 2;
return a;
}

void main(String[] args) {
int e[] = new int[3];
int f;
for (f = 1; f < 3; f = f +1)
e[f] = 0;
foo(true,e);
do
b[0] = a + b[0];
while (!(b[0] > 10));
return ;
}
}

Compile the above Java program:
javac –g Cout.java

Translate to jasmin code by running JavaToJasmin.bat
.source Cout.java
.class public Cout
.super java/lang/Object
.field static a I
.field static b [I
.field static c [F
.field static d F

.method public <init>()V
.limit stack 1
.limit locals 1

.var 0 is this LCout; from
Label0 to Label1
Label0:
.line 1
aload_0
invokespecial
java/lang/Object/<init>()V
Label1:
return
.end method

.method static foo(Z[I)I
.limit stack 4
.limit locals 2
.var 0 is e Z from Label3 to
Label4
.var 1 is f [I from Label3 to
Label4
Label3:
.line 7
iload_0
ifeq Label0
aload_1


iconst_1

iaload
ifne Label0
.line 8
iconst_1
putstatic Cout.a I
goto Label2
Label0:
.line 10
getstatic Cout.b [I
iconst_1
iconst_2
dup
putstatic Cout.a I
iastore
Label2:
.line 11
getstatic Cout.a I
Label4:
ireturn
.end method
.method
main([Ljava/lang/String;)V
.limit stack 5
.limit locals 4
.var 0 is this LCout; from
Label3 to Label4
.var 1 is args
[Ljava/lang/String; from
Label3 to Label4
.var 2 is e [I from Label7 to

Label4
.var 3 is f I from Label1 to
Label4

Label3:
.line 15
iconst_3
newarray int
astore_2
Label7:
.line 17
iconst_1
istore_3
Label1:
iload_3
iconst_3
if_icmpge Label0
.line 18
aload_2
iload_3
iconst_0
iastore
.line 17
iload_3
iconst_1
iadd
istore_3
goto Label1
Label0:
.line 19

iconst_1
aload_2
invokestatic
Cout/foo(Z[I)I
pop
Label2:
.line 21
getstatic Cout.b [I
iconst_0

getstatic Cout.a I
getstatic Cout.b [I
iconst_0
iaload
iadd
iastore
.line 22
getstatic Cout.b [I
iconst_0
iaload
bipush 10
if_icmple Label2
Label4:
.line 23
return
.end method
.method static <clinit>()V
.limit stack 1
.limit locals 0
.line 3

iconst_3
newarray int
putstatic Cout.b [I
.line 4
iconst_4
newarray float
putstatic Cout.c [F
return
.end method

2. Given the following grammar:
S’ → P
P → T id ; P | F
F → T id (T id) { D L } F | ∈
D → T id ; D | ∈
L→SL|∈
S→E;|{DL}
T → int | float
E→T=E|T
T→T+U|U
U → INTLIT | FLOATLIT| id | id ( E ) | ( E )
Based on the interfaces of SymTable.java, SymEntry.java, Frame.java and Emitter.java,
write translation schemes to generate code for:


a. Jasmin directives: .source, .class, .super
Let e keep an instance of Emitter, symtable an instance of SymTable
S’ → {e.printout(e.emitPROLOG());
symtable.enterScope();}


P
{symtable.exitScope();}

b. global variable declarations
P → T id ;
{sym = new SymEntry(id,T.type);
sym.setScope(symtable.getCurrentScope());
symtable.insert(sym);
e.printout(e.emitSTATICFIELD(id.Lexeme,T.type));}

P
T → int {T.type = Type.INT}
| float {T.type = Type.FLOAT}
c. local variable declarations
F → T id (
{if (id.Lexeme.equals(“main”)) isMain = true; else isMain = false;
Frame o = new Frame(isMain);
L.frame = D.frame = o;
e.setFrame(o); symtable.enterScope();
o.enterScope(); }

T1 id1

{index = o.getNewIndex();
sym = new SymEntry(id1,T.type);
sym.setScope(symtable.getCurrentScope());
sym.setObject(new Integer(index));
symtable.insert(sym); e.printout(
e.emitVAR(index,id1.Lexeme,T.type,o.getStartLabel(),o.getEndLabel()));}


){D
{e.printout(e.emitLABEL(o.getStartLabel()));}

L
{e.printout(e.emitLABEL(o.getEndLabel());
symtable.exitScope();
o.exitScope();}

}F


D → T id ; { Frame o = D1.frame = D.frame;

index = o.getNewIndex();
sym = new SymEntry(id,T.type);
sym.setScope(symtable.getCurrentScope());
sym.setObject(new Integer(index));
symtable.insert(sym);e.printout(
e.emitVAR(index,id.Lexeme,T.type,o.getStartLabel(),o.getEndLabel())); }

S→ {

D1
{Frame o = D.frame = L.frame = S.frame;
o.enterScope();}

D
{e.printout(e.emitLabel(o.getStartLabel());}

L


L→

{e.printout(e.emitLabel(o.getEndLabel()));
o.exitScope();}
}
{S.frame = L1.frame = L.frame}

S L1
d. method declarations:
F → T id
{sym = new SymEntry(id,null);
symtable.insert(sym);}

(T1 id1)

{ft = new FunctionType(T1.type,T.type);
sym.setType(ft);
sym.setObject(MCC.CLASSNAME);
e.printout(e.emitMETHOD(id.Lexeme,ft,true));}

{DL
{e.printout(e.emitENDMETHOD()));}

}F
e. U
U → INTLIT {U.waitCode = null;
U.code = e.emitPUSHCONST(INTLIT.lexeme,Type.INT);
U.type = Type.INT;}
| FLOATLIT {U.waitCode = null;

U.code = e.emitPUSHCONST(FLOATLIT.lexeme,Type.FLOAT);
U.type = Type.FLOAT;}
| id
{U.waitCode = symtable.lookup(id);
U.code = null;
U.type = U.waitCode.getType();}


| id ( E ) {U.waitCode = null;
sym = symtable.lookup(id);
Type pt = ((FunctionType)sym.getType()).getParameterType();
if (pt == Type.FLOAT && E.type == Type.INT)
E.code += e.emitI2F();
U.code = E.code +

e.emitINVOKESTATIC(((String)sym.getObject()+’/’+id.Lexeme,sym.getType());

U.type = ((FunctionType)sym.getType()).getReturnType();}
| ( E ) { if (E.waitCode != null) U.code = e.emitREADVAR(E.waitCode);
else U.code = E.code;
U.waitCode = null;
U.type = E.type;}

f. T
T → T1 + U

{if (T1.waitCode != null) T1.code = e.emitREADVAR(T1.waitCode);
if (U.waitCode != null) U.code = e.emitREADVAR(U.waitCode);
if (T1.type != U.type) {
T.type = Type.FLOAT;

if (T1.type = Type.INT)
T1.code += e.emitI2F();
else
U.code += e.emitI2F();
} else T.type = T1.type;
T.code = T1.code + U.code + e.emitADDOP(“+”,T.type);
T.waitCode = null;
}
| U {T.waitCode = U.waitCode; T.code = U.code;T.type = U.type;}

g. E
E → T = E1

{if (E1.waitCode != null) E1.code = e.emitREADVAR(E1.waitCode);
if (E1.type != T.type) {
E.type = Type.FLOAT;
E1.code += e.emitI2F();
} else E.type = T.type;
E.code = E1.code + e.emitDUP() + e.emitWRITEVAR(T.waitCode);
E.waitCode = null;
}
| T {E.waitCode = T.waitCode; E.code = T.code;E.type = T.type}


3. Eliminating left recursion from T-productions and left factoring E-productions of the above
grammar, rewrite translation schemes of 2.f and 2.g.
f. T
T→U
{T’.iwaitCode = U.waitCode; T’.iCode = U.code; T’.iType = U.type;}


T’
{T.waitCode = T’.owaitCode;T.code = T’.ocode; T.type = T’.otype;}

T’ → + U

{if (T’.iwaitCode != null) T’.icode = e.emitREADVAR(T’.iwaitCode);
if (U.waitCode != null) U.code = e.emitREADVAR(U.waitCode);
if (T’.itype != U.type) {
T1’.itype = Type.FLOAT;
if (T’.itype = Type.INT)
T’.icode += e.emitI2F();
else
U.code += e.emitI2F();
} else T1’.itype = T’.itype;
T1’.icode = T’.icode + U.code + e.emitADDOP(“+”,T1’.itype);
T1’.iwaitCode = null;
}

T1’

{T’.ocode = T1’.ocode; T’.otype = T1’.otype; T’.owaitCode = T1’.owaitCode;}
| ∈ {T’.ocode = T’.icode; T’.otype = T’.itype; T’.owaitCode = T’.iwaitCode;}

g. E
E→T
{E’.icode = T.code; E’.iwaitCode = T.waitCode; E’.itype = T.type;}

E’
{E.code = E’.ocode; E.waitCode = E’.owaitCode; E.type = E’.otype;}


E’ → = E

{if (E.waitCode != null) E.code = e.emitREADVAR(E.waitCode);
if (E.type != E’.type) {
E’.otype = Type.FLOAT;
E.code += e.emitI2F();
} else E’.otype = E’.itype;
E’.ocode = E.code + e.emitDUP() + e.emitWRITEVAR(E’.iwaitCode);
E’.owaitCode = null;
}
| ∈ {E’.otype = E’.itype; E’.ocode = E’.icode; E’.owaitCode = E’.iwaitCode;}



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×