CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 10 Ahmed Ezzat
CS 404Ahmed Ezzat 2 Intermediate Representation (IR) What is IR? – Intermediate code used by compiler, between source and target code Why? – Support multiple front and back end, for example, support a new machine architecture – Subdivide and postpone tasks – Allow for machine-independent optimizations (most important)
CS 404Ahmed Ezzat 3 Different IR Forms Low-level IR – Like RISC machine instructions – Use registers, literals, simple operations High-level IR – Syntax tree – Postfix notation – Three address code (TAC)
CS 404Ahmed Ezzat 4 Three-Address Code (TAC) Basic idea: X = Y op Z X, Y, Z are names, constants or compiler- generated temporaries, op is an operator Example – T = x+y*z – T1 = y*z – T2 = x+T1
CS 404Ahmed Ezzat 5 Why use TAC? Makes complex expressions simple Makes complex flow-of-control simple Easy to re-arrange Easy to optimize Syntax trees or dags can be represented by TAX Close to assembly code, so easy to generate target code
CS 404Ahmed Ezzat 6 Common Three Address Statements[1] Assignment: X := Y + Z Assignment: X := 2 + Z Assignment: X := -1 Copy: X := Y Jump: goto L. (L is a symbolic label, execute the statement labeled by L next.)
CS 404Ahmed Ezzat 7 Common Three Address Statements[2] Conditional jump: if X relop Y then goto L Function or procedure call Param x Param y Call f, 2 Indexed assignment: x := a[10] Indexed assignment: a[10] := x
CS 404Ahmed Ezzat 8 Common Three Address Statements[3] Address assignment: x := &y, x gets location of y Pointer assignment: x := *y, x gets the object pointed by y Pointer assignment: *x :=y, the object pointed by x gets the value of y
CS 404Ahmed Ezzat 9 TAC Design Trade-offs IRs need to be rich enough to implement the source language Smaller set of operators – Easier to implement – Long sequence of statements Larger set of operators – More difficult to implement – Short sequence of statements
CS 404Ahmed Ezzat 10 How to Generate TAC Use syntax directed translations Can be folded into parsing if desired For a non-terminal E, define attributes – E.place, the name that will hold the value of E – E.code, the TAC evaluating E – newtemp, a new temporary variable
CS 404Ahmed Ezzat 11 Implementation of TAC Compilers can choose to use quadruples, triples, and indirect triples Quadruples: op, arg1, arg2, result Triples: avoid temporary names by using the location of the statement that computes it Indirect triples: list pointers
CS 404Ahmed Ezzat 12 Differences of TAC Implementations Eventually we will put those TACs in different memory locations and run Space: – Quadruples take the most space, while triples take the least. – Optimizations: Easier to move quadruples and indirect triples around, hard to move triples.