compiler
DESCRIPTION
compiler notesTRANSCRIPT
![Page 1: compiler](https://reader036.vdocument.in/reader036/viewer/2022082404/5695d1991a28ab9b029727a4/html5/thumbnails/1.jpg)
Final ExaminationIntroduction to Compiler Design 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 ; }
ANS: b and c must be kept in memory since the address of
both b and c are passed as arguments and must have an address.
5. [10 pts] Draw the following expression :
MOVE(MEM (ESEQ
(SEQ(CJUMP(LT, TEMPi, CONST0, Lout, Lok),LABELok),TEMPi)
),CONST1)
as a tree diagram and then translate it into canonical form.
[ CJMP(<, TEMP i, CONST 0, Lout, Lok),
LABEL ok,
MOV(MEM(TEMP i), CONST 1) ]
6. [15pts]Translate the following three statements into IR trees.
(a) a * 10 // assume the local a is InReg(t10).
ANS: BINOP(*, t10, CONST 10)
(b) a = b[i+1] // assume starting address of b is stored in inFrame(k) and index
// var i is inReg(t11) and a is inReg( t10) and also
![Page 2: compiler](https://reader036.vdocument.in/reader036/viewer/2022082404/5695d1991a28ab9b029727a4/html5/thumbnails/2.jpg)
// assume the word size of an integer is 4.
ANS: MOV(t10, MEM(+ ( MEM(+(fp, CONST 4k)) ,
*( +(t11, CONST 1), CONST 4) ))).
(c) do {a = a - b;} While (a > 0); // assume a is InReg(t1) and b is
inReg(t2).
ANS: [ LABEL lb1: MOV(t1, -(t1,t1)), CJUMP(>, t1, CONST 0, lb1, lb2),
LABEL lb2: ]
7. (20 pts)[AST generation] Complete the following javacc productions for the generation of
abstract syntax tree from any miniJava input source.
// identifier -> <IDENTIFIER>
Identifier Identifier():{}
{ <IDENTIFIER> {return new Identifier( token.image); } }
// IfStatement "if" "(" Exp ")" Statement "else" Statement
If IfStatement() : {//locals added here
Exp cond; __Statement s1, s2;__
}
{
"if" "(" cond = Exp() ")" s1 = Statement() s2=Statement()
return new IfStatement(cond, s1,s2);
// MehtodDecl "public" Type Identifier "(" Formals ")" "{"
varDecl*
// Statement* "return" Exp "}"
MethodDecl MethodDecl() : {
Type type ; Identifier name; VarDecl local;
FormalList formals = new FormalList();
VarDeclList locals = new VarDeclList();
StatementList stms = new StatementList();
Statement s; Exp result; }
{
"public" type = Type() name = Identifier()
"(" formals = Formals() ")"
"{" ( local = varDecl() { locals.add(local);} )*
( s = Statement() { stmts.add(s);} )*
"return"
result = Exp() "}"
![Page 3: compiler](https://reader036.vdocument.in/reader036/viewer/2022082404/5695d1991a28ab9b029727a4/html5/thumbnails/3.jpg)
{ return new MethodDecl(type, name, formals, locals, stms,
result ); }
}
8. (15 pts) [Instruction Selection] Given the following IR statement:
MOVE(MEM(+(+(CONST100,MEM(TEMPa)), TEMPfp)),CONST0 )
(a) Draw its tree representation [4pts]
(b) Apply the maximal munch algorithm to find a tiling of the tree. Circle all tiles and
number them in the order they are munched. [7pts]
(c) Generate Jouette machine instructions. Note you can use as many fresh temporaries
as you need in you answer [4pts]
9. (12pts) [Liveness analysis]
(a) Draw the control flow graph for the following program [6 pts], and
(b) Show the edges on which each of the variables a, b, and c are live. Assume all
variables are useless after return.[6 pts]
a = 0; L1: b = a + 1; c = c + b;
a = b * 2; if a < 100 goto L1; return c;
ANS (a):
a = 0; 1 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]
ANS: IG = { ab, ac, cd, ce, cf, ef }
(b) What is the fewest number of registers that is needed for this program,
without spilling? Justify your answer by showing a coloring of the inference
graph. [6pts] ANS : 3 register is enough. r1 bc; r2 adf; r3 e
(c) Show the program after register allocation. [4pts]
![Page 4: compiler](https://reader036.vdocument.in/reader036/viewer/2022082404/5695d1991a28ab9b029727a4/html5/thumbnails/4.jpg)
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;
J ouette Architecture
Name Effect Trees
+
TEMP
ri rj + rkADD
*ri rj * rkMUL
-ri rj - rkSUB
/ri rj / rkDIV
ADDI+
CONST
+
CONST
CONSTri rj + c
SUBI ri rj - c -
CONST
LOAD M[rj + c]ri
+
CONST
+
CONST
CONST
MEM MEM MEM MEM
ri
![Page 5: compiler](https://reader036.vdocument.in/reader036/viewer/2022082404/5695d1991a28ab9b029727a4/html5/thumbnails/5.jpg)
J ouette Architecture
+
CONST
+
CONST
CONST
MEM MEM MEM MEM
MOVE MOVE MOVE MOVE
Name Effect Trees
STORE M[rj + c] ri
MOVEM M[rj] M[ri]MEM
MOVE
MEM