Cse322, Programming Languages and Compilers 1 6/21/2015 Lecture #5, April 17, 2007 Array Access Case stmt Jump tables Procedure call Machine dependent strategy OO-langauages
Cse322, Programming Languages and Compilers 2 6/21/2015 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.
Cse322, Programming Languages and Compilers 3 6/21/2015 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 Y[0] = 6 Y[1] = 8 Y[2] = 12 X = 23 rA x= 1 y= 3 => r1 loadAO rA,r1 => = x = 1
Cse322, Programming Languages and Compilers 4 6/21/2015 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[0] = 6 Y[1] = 8 Y[2] = 12 X = 23 rA x= 1 y= 3 => r1... => r exp Add r1,r exp => r off loadAO rA,r off => r2 y[exp]
Cse322, Programming Languages and Compilers 5 6/21/2015 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 => r1... => r exp Add r1,r exp => r off loadAO rA,r off => r2 => r1 Add rA,r1 => r base... => r exp loadAO r base,r exp => r2
Cse322, Programming Languages and Compilers 6 6/21/2015 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 => r2 loadAO rA,r2 => r3 Mul r1,r3 => r4 ! 2*x => 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
Cse322, Programming Languages and Compilers 7 6/21/2015 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
Cse322, Programming Languages and Compilers 8 6/21/2015 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
Cse322, Programming Languages and Compilers 9 6/21/2015 Jump Table JumpI Top Table: JumpI L0 JumpI L1 JumpI L2 JumpI L3 Top:... => r exp LoadI Table => r 0 Add r 0,r exp => r target JumpR r target LO: L1:
Cse322, Programming Languages and Compilers 10 6/21/2015 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
Cse322, Programming Languages and Compilers 11 6/21/2015 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
Cse322, Programming Languages and Compilers 12 6/21/2015 jumpI -> L2 L1: jumpI -> L4 jumpI -> L5 jumpI -> L6 L2: nop => r3 loadAO rA,r3 => r4 loadI L1 => r2 Add r2,r4 => r2 jumpR r2 L4: nop => 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
Cse322, Programming Languages and Compilers 13 6/21/2015 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 prolog precall postcall epilog prolog epilog Call Return
Cse322, Programming Languages and Compilers 14 6/21/2015 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
Cse322, Programming Languages and Compilers 15 6/21/2015 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
Cse322, Programming Languages and Compilers 16 6/21/2015 Example f(3,z,2*x) loadI 3 => r1 => r2 loadAO rA,r2 => r3 loadI 2 => r4 => r5 loadAO rA,r5 => r6 Mul r4,r6 => r7 param r1 param r3 param r7 => r8 call r8 retval r9
Cse322, Programming Languages and Compilers 17 6/21/2015 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
Cse322, Programming Languages and Compilers 18 6/21/2015 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
Cse322, Programming Languages and Compilers 19 6/21/2015 class three fee fum class two fee foe class one fee fie Object Obj a X=2.0 Y=0.1 Z = N =1 Obj c X=5.0 Y=3.1 N =1 Obj b X=5.0 Y=3.0 Z = N =1 #1
Cse322, Programming Languages and Compilers 20 6/21/2015 class three fee fum Object class two fee fum foe class one fee fum foe fie fum … fee … fee … foe … fee … fie … Obj c X=5.0 Y=3.1 N =1 Obj a X=2.0 Y=0.1 Z = N =1 Obj b X=5.0 Y=3.0 Z = N =1 #2
Cse322, Programming Languages and Compilers 21 6/21/2015 Project #1 Project #1 will be assigned Thursday –It will be due in two weeks time, May 3 rd –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).
Cse322, Programming Languages and Compilers 22 6/21/2015 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
Cse322, Programming Languages and Compilers 23 6/21/2015 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
Cse322, Programming Languages and Compilers 24 6/21/2015 Assignment #3 CS322 Prog Lang & Compilers Prog Assignment #3 Assigned Wednesday April 12, 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.