1 / 24

Lecture #5, April 17, 2007

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.

lionel
Download Presentation

Lecture #5, April 17, 2007

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture #5, April 17, 2007 • Array Access • Case stmt • Jump tables • Procedure call • Machine dependent strategy • OO-langauages

  2. 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.

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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:

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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 

  20. 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 … 

  21. 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).

  22. 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

  23. 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

  24. 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.

More Related