1 / 31

Translating High IR to Low IR

Learn how to translate high-level intermediate representation (IR) to low-level IR, which captures low-level machine features. This lecture covers the tree node structure, expressions and statements, as well as the translation process.

Download Presentation

Translating High IR to Low IR

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 15: Translating High IR to Low IR 22 Feb 02

  2. Intermediate Representation • Intermediate representation = internal representation • Is language-independent and machine-independent • High IR: captures high-level language constructs • Low IR: captures low-level machine features Pentium C HIR LIR Fortran Java bytecode Alpha Pascal CS 412/413 Spring 2002 Introduction to Compilers

  3. High-level IR • Tree node structure very similar to the AST • Contains high-level constructs common to many languages • Expression nodes • Statement nodes • Expression nodes for: • Integers and program variables • Binary operations: e1 OP e2 • Arithmetic operations • Logic operations • Comparisons • Unary operations: OP e • Array accesses: e1[e2] CS 412/413 Spring 2002 Introduction to Compilers

  4. High-level IR • Statement nodes: • Block statements (statement sequences): (s1, …, sN) • Variable assignments: v = e • Array assignments: e1[e2] = e3 • If-then-else statements: if c then s1 else s2 • If-then statements: if c then s • While loops: while (c) s • Function call statements: f(e1, …, eN) • Return statements: return or return e • May also contain: • For loop statements: for(v = e1 to e2) s • Break and continue statements • Switch statements: switch(e) { v1: s1, …, vN: sN } CS 412/413 Spring 2002 Introduction to Compilers

  5. High-level IR • Statements may be expressions • Statement expression nodes: • Block statements: (s1, …, sN) • Variable assignments: v = e • Array assignments: e1[e2] = e3 • If-then-else statements: if c then s1 else s2 • Function calls: f(e1, …, eN) • There is a high IR node for each of the above. • All AST nodes are translated into the above IR nodes CS 412/413 Spring 2002 Introduction to Compilers

  6. Low-level IR • Represents a set of instructions which emulates anabstract machine • Arithmetic and logic instructions: • Binary : a = b OP c • Arithmetic operations • Logic operations • Comparisons • Unary operations: a = OP b • Data movement instructions: • Copy : a = b • Load : a = [b] (load in a the value at address b) • Store: [a] = b (store at address a the value b) CS 412/413 Spring 2002 Introduction to Compilers

  7. Low-level IR • Function call instructions: • Call instruction: call f(a1, …, aN) • Call assignment: a = call f(a1, …, aN) • Return instruction: return • Value return: return a • Branch instructions: • Unconditional jump: jump L • Conditional jump: cjump c L • These instructions are also called quadruples or three-address instructions CS 412/413 Spring 2002 Introduction to Compilers

  8. Translating High IR to Low IR • We need to translate each high-level IR node to a low-level IR sequence of instructions • Expressions nodes: arithmetic, logic, comparison, unary, etc. • Statements nodes: blocks, if-then-else, if-then, while, function calls, etc. • Expression statements nodes: if-then-else, calls, etc. CS 412/413 Spring 2002 Introduction to Compilers

  9. Notation • Use the following notation: e= the low-level IR representation of high-level IR construct e • eis a sequence of Low-level IR instructions • If e is an expression (or a statement expression), it represents a value • Denote by t = ethe low-level IR representation of e, whose result value is stored in t • For variable v: t = vis the copy instruction t = v CS 412/413 Spring 2002 Introduction to Compilers

  10. Translating Expressions OP e1 e2 OP e • Binary operations: t =  e1 OP e2 (arithmetic operations and comparisons) t1 = e1  t2 = e2  t = t1 OP t2 • Unary operations: t =  OP e  t1 = e  t = OP t1 CS 412/413 Spring 2002 Introduction to Compilers

  11. Translating Boolean Expressions OR e1 e2 • t = e1 OR e2  t1 = e1  t2 = e2  t = t1 OR t2 • … how about short-circuit OR? • Should compute e2 only if e1 evaluates to false CS 412/413 Spring 2002 Introduction to Compilers

  12. Translating Short-Circuit OR SC-OR e1 e2 • Short-circuit OR: t = e1 SC-OR e2  t = e1  cjump t Lend t = e2  label Lend • … how about short-circuit AND? CS 412/413 Spring 2002 Introduction to Compilers

  13. Translating Short-Circuit AND SC-AND e1 e2 • Short-circuit AND: t =  e1 SC-AND e2  t =  e1  cjump t Lnext jump Lend label Lnext t =  e2  label Lend CS 412/413 Spring 2002 Introduction to Compilers

  14. Another Translation SC-AND e1 e2 • Short-circuit AND: t =  e1 SC-AND e2  t1 = e1  t2 = not t1 cjump t2 Lend t =  e2  label Lend CS 412/413 Spring 2002 Introduction to Compilers

  15. Yet Another Translation SC-AND e1 e2 • Use another low-level IR: abstract machine with two kinds of conditional jumps • tjump c L : jump to L if c is true • fjump c L : jump to L if c is false • Short-circuit AND: t =  e1 SC-AND e2  t =  e1  fjump t2 Lend t =  e2  label Lend CS 412/413 Spring 2002 Introduction to Compilers

  16. Translating Array Accesses array v e • Array access: t =v[e]  (type of e1 is array[T] and S = size of T) t1 =addr v t2 = e  t3 = t2 * S t4 = t1 + t3 t = [t4] CS 412/413 Spring 2002 Introduction to Compilers

  17. Translating Statements seq s1 … s2 sN • Statement sequence: s1; s2; …; sN  s1  s2  … sN  • IR instructions of a statement sequence = concatenation of IR instructions of statements CS 412/413 Spring 2002 Introduction to Compilers

  18. Assignment Statements var-assign v e array-assign v e1 e2 • Variable assignment: v = e  v =  e  • Array assignment: v[e1] = e2  t1 =addr v t2 = e1  t3 = t2 * S t4 = t1 + t3 t5 = e2  [t4] = t5 CS 412/413 Spring 2002 Introduction to Compilers

  19. Translating If-Then-Else if-then-else s2 e s1 •  if (e) then s1 else s2  t1 = e  fjump t1 Lfalse s1  jump Lend label Lfalse s2  label Lend CS 412/413 Spring 2002 Introduction to Compilers

  20. Translating If-Then if-then e s •  if (e) then s  t1 = e  fjump t1 Lend s  label Lend CS 412/413 Spring 2002 Introduction to Compilers

  21. While Statements while e s1 •  while (e) { s }  label Ltest t1 = e  fjump t1 Lend s  jump Ltest label Lend CS 412/413 Spring 2002 Introduction to Compilers

  22. Switch Statements switch … e v1 s1 vN sN •  switch (e) { case v1: s1, …, case vN: sN }  t = e  c = t != v1 tjump c L2 s1  jump Lend label L2 c = t != v2 tjump c L3 s2  jump Lend … label LN c = t != vN tjump c Lend sN  label Lend CS 412/413 Spring 2002 Introduction to Compilers

  23. Call and Return Statements call … f e1 e2 eN return e • call f(e1, e2, …, eN)  t1 = e1  t2 = e2  … tN = eN  call f(t1, t2, …, tN) • return e  t = e  return t CS 412/413 Spring 2002 Introduction to Compilers

  24. Statement Expressions • So far: statements which do not return values • Easy extensions for statement expressions: • Block statements • If-then-else • Assignment statements • t =s  is the sequence of low IR code for statement s, whose result is stored in t CS 412/413 Spring 2002 Introduction to Compilers

  25. Statement Expressions if-then-else s2 e s1 • t = if (e) then s1 else s2  t1 =  e  cjump t1 Ltrue t = s2  jump Lend label Ltrue t = s1  label Lend assign t CS 412/413 Spring 2002 Introduction to Compilers

  26. Block Statements seq s1 … s2 sN • t = s1; s2; …; sN  s1  s2  … t = sN  • Result value of a block statement = value of last statement in the sequence CS 412/413 Spring 2002 Introduction to Compilers

  27. Assignment Statements assign v e • t = v = e  v = e  t = v • Result value of an assignment statement = value of the assigned expression CS 412/413 Spring 2002 Introduction to Compilers

  28. Nested Expressions • In these translations, expressions may be nested; • Translation recurses on the expression structure • Example: t = (a - b) * (c + d)  t1 = a t2 = b t3 = t1 – t2 t4 = b t5 = c t5 = t4 + t5 t = t3 * t5 (a - b)  (a - b) * (c + d) (c + d)  CS 412/413 Spring 2002 Introduction to Compilers

  29. Nested Statements • Same for statements: recursive translation • Example: if c then if d then a = b  t1 =c fjump t1 Lend1 t2 = d fjump t2 Lend2 t3 = b a = t3 label Lend2 label Lend1 if c then …  if d …  a = b  CS 412/413 Spring 2002 Introduction to Compilers

  30. Issues • These translations are straightforward • … and inefficient: • May generate many temporary variables • May generate many labels • Can optimize translation process: • Don’t create temporaries for variables • Reuse temporary variables • Merge adjacent labels CS 412/413 Spring 2002 Introduction to Compilers

  31. Optimize Translation • Example: t =〚 (a - b) * (c + d) 〛 t1 = a t2 = b t3 = t1 – t2 t4 = b t5 = c t5 = t4 + t5 t = t3 * t5 t = a - b t1 = b + c t = t * t1 CS 412/413 Spring 2002 Introduction to Compilers

More Related