Académique Documents
Professionnel Documents
Culture Documents
6/14 2006
1. (8 pts) A compiler can be decomposed into front end and back end.
(a) What are the main components of a compiler front-end ?
Lexical analyzer, parser, semantic analyzer, IR code generation
(b) What are the main components of a compiler back-end ?
(IR and machine code) optimization, Code generation.
2. (12 pts) Show the right hand side of the following rules that can be used for eliminating
ESEQs from IR tree.
(a) EXP( ESEQ(s,e)) SEQ(S, EXP(e))
(b) EXP(CALL(e1, [e2,ESEQ(s,e3)]))
[ MOV(TEMP t1, e1), MOV(TEMP t2,e2), s, EXP(t1, [t2,e3]) ]
(c) CJUMP(>, e1, ESEQ(S, e2), Ltrue, Lfalse)
[MOV(TEMP t1, e1), S, CIMP(>, t1, e2, Ltrue,Lfalse).
3. [10 pts] List five data items that may appear as contents of a stack frame.
actual parameters, machine registers (caller-side, callee-side,SP,), return address,
static link, return value, dynamic link, local variables, intermediate operands, etc.
4. [10pts] For each of the variables a,b,c,d,e in the C program, determine whether the
variable should be kept in memory or register, and why.
int f( int a, int b) {
int c[3], d, e;
d = a + 1;
e = g(c, &b);
return e + c[1] + b ;
<IDENTIFIER>
Identifier Identifier():{}
{
}
{
"if" "(" cond = Exp() ")"
s1 = Statement() s2=Statement()
MethodDecl MethodDecl() : {
Type type ;
Identifier name;
VarDecl local;
Exp result;
{
"public" type = Type() name = Identifier()
"(" formals = Formals() ")"
"{" ( local = varDecl() { locals.add(local);}
( s = Statement() { stmts.add(s);}
"return"
result = Exp()
"}"
)*
)*
L1:
a = b * 2;
b = a + 1;
c = c + b;
return c;
ANS (a):
a = 0;
L1:
b = a + 1; 2
c = c + b; 3
^|6|^
a = b * 2; 4 if a < 100 goto L1; 5
return c;
ANS(b):
a = 0;
ac L1:
b = a + 1; bc
c = c + b; bc
^|ac|^
a = b * 2; ac if a < 100 goto L1; c
return
c;
10. [18pts] In the following program, there are six temporaries a,b,c,d,e,f.
(a) Show the interference graph for these temporaries. [8 pts]
a = 1 ;
b = 10;
c = a + b;
d = a + c;
e = c + d; f = c + e;
b = c + f;
d = 5 + e;
return c+d;
ANS:
r2 = 1 ;
r1 = 10;
r1 = r2 + r1;
r2 = r2 + r1;
r3 = r1 + r2; r2 = r1 + r3;
r1 = r1 + r2;
r2 = 5 + r3;
return r1+r2;