240 likes | 329 Views
Lecture #5, April 17, 2007. Array Access Case stmt Jump tables Procedure call Machine dependent strategy OO-langauages. Assignments. Reading Read chapter 8 sections 8.1 – 8.3 Possible Quiz Wednesday on the reading.
E N D
Lecture #5, April 17, 2007 • Array Access • Case stmt • Jump tables • Procedure call • Machine dependent strategy • OO-langauages
Assignments • Reading • Read chapter 8 sections 8.1 – 8.3 • Possible Quiz Wednesday on the reading. • Programming assignment #3 is now available on the class website under the assignments link.
X = 23 Y[0] = 6 Y[1] = 8 Y[2] = 12 Array Access • Array access is a lot like variable access in a block structured or OO-language • We need to compute an address and an offset rA x= 1 y= 3 @x = x = 1 loadI @x => r1 loadAO rA,r1 => r2
X = 23 Y[0] = 6 Y[1] = 8 Y[2] = 12 Array Access #2 • The instruction LoadAO takes a base address and an offset. • In an array the offset includes the index of the array expression, as well as the delta from the rA y[exp] rA x= 1 loadI @y => r1 ... => rexp Add r1,rexp => roff loadAO rA,roff => r2 y= 3
Alternate approach • Another way is to approach this is to include both the rA and the y into the base and just use the index as the offset. • Compare loadI @y => r1 Add rA,r1 => rbase ... => rexp loadAO rbase,rexp => r2 loadI @y => r1 ... => rexp Add r1,rexp => roff loadAO rA,roff => r2
ML program • To add this to our ML program we can proceed either way. • We need to handle element size and 1 based indexing as well. • Consider y[2*x] loadI 2 => r1 loadI @x => r2 loadAO rA,r2 => r3 Mul r1,r3 => r4 ! 2*x loadI @y => r5 loadI 1 => r7 Sub r4,r7 => r4 ! 2*x – 1 handle 1-based loadI 4 => r6 Mul r4,r6 => r4 ! (2*x – 1) * 4 handle size Add r5,r4 => r4 ! Add the offset of y loadAO rA,r4 => r8 ! Access the correct word
fun expr dict node = case node of ArrayElm(array as(Var(NONE,y)),index,SOME Type) => let val size = (case Type of Bool => 1 | Int => 4 | Real => 8) val nbased = 1 val rindex = expr dict index val t1 = base array val t2 = offset array val rsize = NextRegister() val rbase = NextRegister() val result = NextRegister() in emit (LoadI(Int.toString nbased,rbase)); emit (Arith(SUB,rindex,rbase,rindex)); emit (LoadI(Int.toString size,rsize)); emit (Arith(MUL,rindex,rsize,rindex)); emit (Arith(ADD,t2,rindex,rindex)); emit (LoadAO(t1,rindex,result)); result end
Case stmt • Consider a case statement over integers case exp of 0 => exp0 | 1 => exp1 | 2 => exp2 | 3 => exp3 • How can we efficiently translate this? • Nested if-then-else • Binary search • Jump table Compact set of contiguous integers. Like Tags in a datatype
Jump Table JumpI Top Table: JumpI L0 JumpI L1 JumpI L2 JumpI L3 Top: . . . => rexp LoadI Table => r0 Add r0,rexp => rtarget JumpR rtarget LO: L1:
Add two new IR instructions datatype IR = LoadI of (string * Reg) | LoadAO of (Reg * Reg * Reg) | Arith of (Op * Reg * Reg * Reg) | JumpR of Reg | Move of Reg *Reg
fun caseExp dict exp arms = let val ns = map fst arms val [Table,Top,Bottom] = NextLabel 3 fun target (n,expr) = (n,hd (NextLabel 1),expr) val labels = map target arms fun emitJump (0,lab,exp) = emitAt Table (JumpI lab) | emitJump (_,lab,exp) = emit (JumpI lab) val result = NextRegister() val index = NextRegister () fun emitArm (_,lab,exp) = let val _ = emitAt lab Nop val r = expr dict exp in emit (Move(r,result)); emit (JumpI Bottom) end val _ = emit (JumpI Top) val _ = map emitJump labels val _ = emitAt Top Nop val rexp = expr dict exp in emit (LoadI(“L”^Int.toString Table,index)); emit (Arith(ADD,index,rexp,index)); emit (JumpR index); map emitArm labels; emitAt Bottom Nop; result end
jumpI -> L2 L1: jumpI -> L4 jumpI -> L5 jumpI -> L6 L2: nop loadI @y => r3 loadAO rA,r3 => r4 loadI L1 => r2 Add r2,r4 => r2 jumpR r2 L4: nop loadI @x => r5 loadAO rA,r5 => r6 Move r6 => r1 jumpI -> L3 L5: nop loadI 5 => r7 Move r7 => r1 jumpI -> L3 L6: nop loadI 2 => r8 Mul r8,r4 => r9 Move r9 => r1 jumpI -> L3 L3: nop case y of 0 => x | 1 => 5 | 2 => 2 * y
prolog Call prolog precall postcall Return epilog epilog Procedure Call • AR creation is a cooperative effort • Shared by the caller and the callee • Has 4 parts • Precall • Postreturn • Prolog • Epilog • Precall & Postcall can be split
Setting up parameters • On stack • In memory • In registers • Setting up the call • The address to point to • The return address • Special registers like the activation record or object pointer • Handling returned values
Machine dependencies • Many of these set up operations will be machine dependent • To avoid machine dependencies at the IR level we define some abstract IR instructions • Precall - param • Call - call • PostCall - retval • For each machine these will be implemented in a different way
Example f(3,z,2*x) loadI 3 => r1 loadI @z => r2 loadAO rA,r2 => r3 loadI 2 => r4 loadI @x => r5 loadAO rA,r5 => r6 Mul r4,r6 => r7 param r1 param r3 param r7 loadI @f => r8 call r8 retval r9
fun expr dict node = case node of Call(_,f,_,args) => let val rargs = map (expr dict) args fun emitParam r = emit (Param r) val rf = NextRegister() val result = NextRegister() in map emitParam rargs; emit (LoadI(f,rf)); emit (Callx rf); emit (Retval result); result end
OO languages • The code shape for OO languages is different from other languages in several places • Instance variables • Method calls • These must use the same code no matter where they are used, since we generate code from its abstract syntax. • In different instances, objects may different numbers of instance variables and methods. • Uniform way of laying out these things is necessary • Consider two different ways of laying out objects
Obj c class three N =1 fee X=5.0 fum Y=3.1 class two fee foe class one fee Obj a Obj b fie N =1 N =1 X=2.0 X=5.0 Y=0.1 Y=3.0 Z = Z = Object #1
Obj c class three N =1 fee X=5.0 fum Y=3.1 class two fee fum foe class one fee Obj a Obj b fum N =1 N =1 foe X=2.0 X=5.0 fie Y=0.1 Y=3.0 Z = Z = Object #2 fee … fee … fum … fee … foe … fie …
Project #1 • Project #1 will be assigned Thursday • It will be due in two weeks time, May 3rd • There will be no daily assignments next week, so get started as soon as you get the project #1 description • Project 1 will translate MiniJava through a sequence of IR languages. • These languages will be different (but also similar) to the IR we have seen in class. • The first IR is located on the notes web page. • Also see the Slides from Jenke Li about templates for MiniJava Translation (available on the notes web page).
IR #1 datatype BINOP = ADD | SUB | MUL | DIV | AND | OR datatype RELOP = EQ | NE | LT | LE | GT | GE datatype EXP = BINOP of BINOP * EXP * EXP | CALL of EXP * EXP list | MEM of EXP | NAME of string | TEMP of int | PARAM of int | VAR of int | CONST of string | STRING of string | ESEQ of STMT * EXP | EXPLIST of EXP list
IR #1 continued and STMT = MOVE of EXP * EXP | JUMP of EXP | CJUMP of RELOP * EXP * EXP * EXP | LABEL of EXP | CALLST of EXP * EXP list | RETURN of EXP | STMTlist of STMT list; datatype FUNC = FUNC of string * int * int * STMT list
Assignment #3 CS322 Prog Lang & Compilers Prog Assignment #3 Assigned Wednesday April 12, 2006. Due Monday, April 17, 2006 This assignment is to extend the stmt program discussed in class (and available for download) so that it can translate while loops fun stmt dict x = case x of (While(tst,body)) => • Base your solution on the if-then-else statement and the discussion in the text book about while-loops. • You don’t need to add any new IR instructions. • Be sure and test your code, print out the tests, and hand them in.