06-trans

06-trans - Translation From ASTs to IR trees Copyright c...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Translation From ASTs to IR trees Copyright c 2010 by Antony L. Hosking. Permission to make digital or hard copies of part or all of this work for personal or classroom use is granted without fee provided that copies are not made or distributed for profit or commercial advantage and that copies bear this notice and full citation on the first page. To copy otherwise, to republish, to post on servers, or to redistribute to lists, requires prior specific permission and/or fee. Request permission to publish from hosking@cs.purdue.edu. IR trees: Expressions CONST i NAME n TEMP t BINOP e1 e2 Integer constant i Symbolic constant n Temporary t [a code label] [one of any number of "registers"] Application of binary operator: ADD, SUB, MUL, DIV [arithmetic] AND, OR, XOR [bitwise logical] SLL, SRL [logical shifts] SRA [arithmetic right-shift] to integer operands e1 (evaluated first) and e2 (evaluated second) Contents of a word of memory starting at address e Procedure call; expression f is evaluated before arguments e1 , . . . , en Expression sequence; evaluate s for side-effects, then e for result MEM e CALL f [e1, . . . , en] ESEQ se CS352 Translating ASTs to IR trees 2 IR trees: Statements MOVE TEMP t MOVE MEM e1 EXP e JUMP e [l1, . . . , ln] CJUMP e1 e2 t f Evaluate e and discard result Transfer control to address e; l1, . . . , ln are all possible values for e Evaluate e1 then e2 , yielding a and b, respectively; compare a with b using relational operators: BEQ, BNE [signed and unsigned integers] BLT, BGT, BLE, BGE [signed] jump to t if true, f if false Statement s1 followed by s2 Define constant value of name n as current code address; NAME(n) can be used as target of jumps, calls, etc. Translating ASTs to IR trees 3 e Evaluate e into temporary t e2 Evaluate e1 yielding address a, e2 into word at a SEQ s1 s2 LABEL n CS352 Kinds of expressions Expression kinds indicate "how expression might be used" Ex(exp) expressions that compute a value Nx(stm) statements: expressions that compute no value Cx conditionals (jump to true and false destinations) RelCx.op(left, right) eq, ne, gt, lt, ge, le IfThenElseExp expression or statement, depending on use Conversion operators allow use of one form in context of another: unEx convert to tree expression that computes value of inner tree unNx convert to tree statement that computes inner tree but returns no value unCx(t, f) convert to statement that evaluates inner tree and branches to true destination if non-zero, false destination otherwise CS352 Translating ASTs to IR trees 4 Translating MiniJava Local variables: Allocate as a temporary t TEMP Ex(TEMP t) t Array elements: Array expression is reference to array in heap. For exressions e and i, translate e[i] as: Ex(MEM(ADD(e.unEx(), (i.unEx(), CONST(w))))) where w is the target machine's word size: all values are word-sized (scalar) in MiniJava Array bounds check: array index i <e.size; runtime will put size in word preceding array base Object fields: Object expression is reference to object in heap. For expression e and field f , translate e.f as: Ex(MEM(ADD(e.unEx(), CONST(o)))) where o is the byte offset of the field f in the object Null pointer check: object expression must be non-null (i.e., non-zero) CS352 Translating ASTs to IR trees 5 Translating MiniJava String literals: Allocate statically: label: .word 11 .ascii "hello world" Translate as reference to label: Ex(NAME(label)) Object creation: Allocate object in heap. For class T , translate new T () as: Ex(CALL(NAME("new"), CONST(fields ), NAME(label for T 's vtable))) Array creation: Allocate array in heap. For type T , array expression e, translate newT [e] as: Ex(ESEQ(MOVE(TEMP(s), e.unEx()), CALL(NAME("new"), MUL(TEMP(s), CONST(w)), TEMP(s)))) where s is a fresh temporary, and w is the target machine's word size. CS352 Translating ASTs to IR trees 6 Control structures Basic blocks: a sequence of straight-line code if one instruction executes then they all execute a maximal sequence of instructions without branches a label starts a new basic block Overview of control structure translation: control flow links up the basic blocks ideas are simple implementation requires bookkeeping some care is needed for good code CS352 Translating ASTs to IR trees 7 while loops while (c) s: 1. evaluate c 2. if false jump to next statement after loop 3. evaluate loop body s 4. evaluate c 5. if true jump back to loop body e.g., if not(c) jump done body: s if c jump body done: Nx(SEQ(SEQ(c.unCx(b, x), SEQ(LABEL(b), s.unNx())), SEQ(c.unCx(b, x), LABEL(x)))) CS352 Translating ASTs to IR trees 8 for loops for (i, c, u) s 1. evaluate initialization statement i 2. evaluate c 3. if false jump to next statement after loop 4. evaluate loop body s 5. evaluate update statement u 6. evaluate c 7. if true jump to loop body Nx(SEQ(i.unNx(), SEQ(SEQ(c.unCx(b, x), SEQ(LABEL(b), SEQ(s.unNx(), u.unNx()))), SEQ(c.unCx(b, x), LABEL(x))))) For break statements: when translating a loop push the done label on some stack break simply jumps to label on top of stack when done translating loop and its body, pop the label CS352 Translating ASTs to IR trees 9 Method calls e0.m(e1, . . . , en): Ex(CALL(MEM(MEM(e0.unEx(), -w), m.index w), e1.unEx(), . . . en.unEx())) Null pointer check: expression e0 must be non-null (i.e., non-zero) CS352 Translating ASTs to IR trees 10 Comparisons Translate a op b as: RelCx.op(a.unEx(), b.unEx()) When used as a conditional unCx(t, f ) yields: CJUMP(a.unEx(), b.unEx(), t, f ) where t and f are labels. When used as a value unEx() yields: ESEQ(SEQ(MOVE(TEMP(r), CONST(1)), SEQ(unCx(t, f ), SEQ(LABEL( f ), SEQ(MOVE(TEMP(r), CONST(0)), LABEL(t))))), TEMP(r)) CS352 Translating ASTs to IR trees 11 Conditionals Translate short-circuiting Boolean operators (&&, ||, !) as if they were conditionals e.g., x < 5 && a > b is treated as (x < 5) ? (a > b) : 0 We translate e1 ? e2 : e3 into IfThenElseExp(e1, e2, e3) When used as a value IfThenElseExp.unEx() yields: ESEQ(SEQ(SEQ(e1.unCx(t, f ), SEQ(SEQ(LABEL(t), SEQ(MOVE(TEMP(r), e2.unEx()), JUMP( j))), SEQ(LABEL( f ), SEQ(MOVE(TEMP(r), e3.unEx()), JUMP( j))))), LABEL( j)), TEMP(r)) As a conditional IfThenElseExp.unCx(t, f ) yields: SEQ(e1.unCx(tt, ff ), SEQ(SEQ(LABEL(tt), e2.unCx(t, f )), SEQ(LABEL(ff ), e3.unCx(t, f )))) CS352 Translating ASTs to IR trees 12 Conditionals: Example Applying unCx(t, f ) to (x < 5) ? (a > b) : 0: SEQ(BLT(x.unEx(), CONST(5), tt, ff ), SEQ(SEQ(LABEL(tt, BGT(a.unEx(), b.unEx(), t, f )), SEQ(LABEL(ff , JUMP( f )))) or more optimally: SEQ(BLT(x.unEx(), CONST(5), tt, f ), SEQ(LABEL(tt, BGT(a.unEx(), b.uneX(), t, f ))) CS352 Translating ASTs to IR trees 13 One-dimensional fixed arrays: Pascal/Modula/C/C++ var a : array [2..5] of integer; ... a[e] translates to: MEM(ADD(TEMP(FP), ADD(CONST k - 2w, (CONST w, e.unEx)))) where k is offset of static array from the frame pointer FP, w is word size In Pascal, multidimensional arrays are treated as arrays of arrays, so A[i,j] is equivalent to A[i][j], so this translation works for subarrays. Not so in Fortran. CS352 Translating ASTs to IR trees 14 Multidimensional arrays Array allocation: constant bounds allocate in static area, stack, or heap no run-time descriptor is needed dynamic arrays: bounds fixed at run-time allocate in stack or heap descriptor is needed dynamic arrays: bounds can change at run-time allocate in heap descriptor is needed CS352 Translating ASTs to IR trees 15 Multidimensional arrays Array layout: Contiguous: 1. Row major Rightmost subscript varies most quickly: A[1,1], A[1,2], ... A[2,1], A[2,2], ... Used in PL/1, Algol, Pascal, C, Ada, Modula, Modula-2, Modula-3 2. Column major Leftmost subscript varies most quickly: A[1,1], A[2,1], ... A[1,2], A[2,2], ... Used in By vectors Contiguous vector of pointers to (non-contiguous) subarrays FORTRAN CS352 Translating ASTs to IR trees 16 Multi-dimensional arrays: row-major layout array [1..N,1..M] of T array [1..N] of array [1..M] of T no. of elt's in dimension j: D j = U j - L j + 1 position of A[i1, ..., in]: (in - Ln) +(in-1 - Ln-1)Dn +(in-2 - Ln-2)DnDn-1 + +(i1 - L1)Dn D2 which can be rewritten as variable part i1D2 Dn + i2D3 Dn + + in-1Dn + in - (L1D2 Dn + L2D3 Dn + + Ln-1Dn + Ln) constant part Address of A[i1, ..., in]: address(A) + ((variable part - constant part) element size) CS352 Translating ASTs to IR trees 17 case (switch) statements case E of V1: S1 . . . Vn: Sn end 1. evaluate the expression 2. find value in case list equal to value of expression 3. execute statement associated with value found 4. jump to next statement after case Key issue: finding the right case sequence of conditional jumps (small case set) O(| cases |) binary search of an ordered jump table (sparse case set) O(log2 | cases |) hash table (dense case set) O(1) CS352 Translating ASTs to IR trees 18 case (switch) statements case E of V1: S1 . . . Vn: Sn end One translation approach: t := expr jump test L1: code for S1 jump next L2: code for S2 jump next ... Ln: code for Sn jump next test: if t = V1 jump L1 if t = V2 jump L2 ... if t = Vn jump Ln code to raise run-time exception next: CS352 Translating ASTs to IR trees 19 Labels and gotos A little complicated! Resolving references to labels multiply-defined in different scopes: begin L: begin goto L; . . . { possible definition of L } end end Scope labels like variables On use, label definition is either resolved or unresolved On definition, backpatch previous unresolved label uses Jumping out of blocks or procedures: 1. Pop run-time stack 2. Fix display (if used); static chain needs no fixing 3. Restore registers if jumping out of a procedure CS352 Translating ASTs to IR trees 20 Parameter passing Place information in formal parameter location for callee to access actual parameter: value address dope vector Parameter passing modes: value (copy-in), result (copy-out), value-result Copy actual into formal on call, formal into actual on return reference (var), read-only Copy address of actual into formal name, formal procedures, label parameters Name parameters are re-evaluated on every reference Data objects distinguish: values (constants) locations (ordinary variables) addresses of locations containing values (indirect references, var parameters) CS352 Translating ASTs to IR trees 21 Value, result, value-result parameters Value: treat formal as a local variable initialized with actual actual can be any expression of correct type Result: treat formal as uninitialized local variable on return formal is copied into actual actual must be an l-value Value-result: treat formal as local variable initialized with actual on return formal is copied to actual actual must be an l-value CS352 Translating ASTs to IR trees 22 Value, result, value-result parameters Implementation: Scalars: result/value-result pass address of actual, copy value to/from local copy value simply pass value directly Arrays: pass dope vector static arrays pass pointer to base of array result/value-result two local dope vectors value one local dope vector Records: handle as scalar (since fixed in size) best to pass address, let callee copy (more compact calling sequences) CS352 Translating ASTs to IR trees 23 Reference and read-only parameters Usually pass address Scalars: reference pass address of actual read-only pass value (rather than address): copy actual into read-only local Arrays: pass dope vector (simple pointer if static) Records: pass base address CS352 Translating ASTs to IR trees 24 ...
View Full Document

Ask a homework question - tutors are online