270 likes | 291 Views
Objectives. Intermediate code Representation Intermediate languages & Classification Three Address Code Declarations & Assignment statements Boolean expressions & Case statements. Intermediate Representation (IR) – What ?.
E N D
Objectives • Intermediate code Representation • Intermediate languages & Classification • Three Address Code • Declarations & Assignment statements • Boolean expressions & Case statements www.Bookspar.com | Website for Students | VTU - Notes - Question Papers
Intermediate Representation (IR) – What ? • An abstract machine language that can express target-machine operations without too much machine-specific detail. • A compile time data structure • Encodes the compiler’s knowledge of the program Back-End not bothered with information specific to source language Front-End not bothered with machine-Specific details www.Bookspar.com | Website for Students | VTU - Notes - Question Papers
IR – Where used ? • Front end - produces an intermediate representation (IR) ( E.g. Parse tree) • Middle end - transforms the IR into an equivalent IR that runs more efficiently ( E.g. AST, DAG etc..) • Back end - transforms the IR into native code • Middle end usually consists of several passes www.Bookspar.com | Website for Students | VTU - Notes - Question Papers
IR – How used ? • There is no standard Intermediate Representation. IR is a step in expressing a source program so that machine understands it • As the translation takes place, IR is repeatedly analyzed and transformed • Compiler users want analysis and translation to be fast and correct • Compiler writers want optimizations to be simple to write, easy to understand and easy to extend • IR should be simple and light weight while allowing easy expression of optimizations and transformations. www.Bookspar.com | Website for Students | VTU - Notes - Question Papers
IR – Why Used ? • Good software engineering technique • Break the compiler into manageable pieces (Modularity) • Isolates back end from front end (Flexibility to manage / change them independently) • Enables machine independent optimization • Lets compiler consider more than one option • Simplifies retargeting to new host • Allow a complete pass before code is emitted www.Bookspar.com | Website for Students | VTU - Notes - Question Papers
IR – How created ? • More of a wizardry rather than science • each compiler uses 2-3 IRs (Compiler writers have tried to define Universal IRs and have failed. (UNCOL in 1958) • HIR (high level IR) preserves loop structure & array bounds • MIR (medium level IR) reflects range of features in a set of source languages • language independent • good for code generation for one or more architectures • appropriate for most optimizations • LIR (low level IR) low level similar to the machines www.Bookspar.com | Website for Students | VTU - Notes - Question Papers
Important Properties of IR • Ease of generation • Ease of manipulation • Cost of manipulation • Level of abstraction • Freedom of expression • Size of typical procedure Decisions in IR design affect the speed and efficiency of the compiler www.Bookspar.com | Website for Students | VTU - Notes - Question Papers
Categories of IR Examples: AST, DAG, • Structural IR’s • Graphically oriented notations • Heavily used in source to source translators • Have large number of nodes & edges • Linear IR’s • pseudocode for some abstract machine • Large variation in level of abstraction • Simple compact data structures • Easier to rearrange • Hybrids • Combination of graphs & linear code • Attempt to take best of each Examples: Three - Address Code, Stack Machine Code Example: Control Flow Graph (CFG) www.Bookspar.com | Website for Students | VTU - Notes - Question Papers
Stack Machine Code • Originally used for stack-based computers (famous example: Burroughs 5000 series) • Now used for Java, C# (MSIL) • Advantages Example: x – 2 * y becomes push x push 2 push y multiply Subtract • Compact; mostly 0-address opcodes • Easy to generate • Simple to translate to naïve machine code • But need to do better in production compilers • Useful where code is transmitted over slow communication links www.Bookspar.com | Website for Students | VTU - Notes - Question Papers
Precedence value • Input (out side the stack) + ,- : return 1; / ,*: return 3; ( : return 9 ) : return 0 Operand : return 7 • Stack (In side the stack) + ,- : return 2; / ,*: return 4; ( : return 0 # : return -1 Operand : return 8 www.Bookspar.com | Website for Students | VTU - Notes - Question Papers
Three - Address code • A term used to describe a variety of representations • In general they allow statements of the form x ← y op z with a single operator and at most three names • Simpler form of expression • Advantages • compact form direct naming • Adds names for intermediate values • Can include forms of prefix or postfix code • Often easy to rearrange Example: Z ← x – 2 * y becomes t1 ← 2 * y t2 ← x – t1 www.Bookspar.com | Website for Students | VTU - Notes - Question Papers
Statements in Three – address code • Typical statements allowed include: • Assignment like x := y op z and x:= op y • Copy statements like x := y • Unconditional Jumps like Goto L • Conditional Jumps like if x relop y goto L • Indexed assignments like x := y[j] or s[j] := z • Address and pointer assignments (for C) like • x := &y, x := *p; *x := y • Procedure call statements like • Parm x; call p, n; return y; (for calls) www.Bookspar.com | Website for Students | VTU - Notes - Question Papers
SDT To Produce 3-address Code for assignment E.place to hold the value of E E.code The sequence of 3-address statements evaluating E gen(id.place’:=‘E.place) gen(E.place’:=‘E1.place’+’ E2.place) gen(E.place’:=‘E1.place’*’ E2.place) gen(E.place’:=‘ ‘uminus’E1. place)
Generating code for a while statementS := while E do S1 S.begin: S.after: SEMANTIC RULES S.begin:= newlabel; S.after := newlabel; E.code if E.place = 0 go to S.after S1.code go to S.begin S.code := gen(S.begin ‘:’)|| E.code|| gen(‘if’ E.place’=‘ ‘0’ ’ go to’ S.after) || S1. Code || gen(‘go to ‘ S.begin)|| gen( S.after’:) www.Bookspar.com | Website for Students | VTU - Notes - Question Papers
SDT for Boolean Expressions • Have two primary purpose in programming languages • Computation of logical values • Conditional expressions inflow control statements • Composed of boolean opeators – AND OR and NOT • Relational Expressions are of the form E1relop E2 • The grammar for Boolean Expressions : E → E or E | E and E \ not E \ (E )| id relop id | true | false • Two techniques are used for translationg Boolean expressions • Numerical Representation • Implicit Representation ………. Contd. www.Bookspar.com | Website for Students | VTU - Notes - Question Papers
SDT for Boolean Expressions • Numerical representation • Use 1 to represent true, use 0 to represent false • For three-address code store this result in a temporary • For stack machine code store this result in the stack • Implicit representation • For the boolean expressions which are used in flow-of-control statements (such as if-statements, while-statements etc.) boolean expressions do not have to explicitly compute a value, they just need to branch to the right instruction • Generate code for boolean expressions which branch to the appropriate • instruction based on the result of the boolean expression ………. Contd. www.Bookspar.com | Website for Students | VTU - Notes - Question Papers
Numerical representation Attributes : E.place: location that holds the value of expression E • E.code: sequence of instructions that are generated for E • id.place: location for id • relop.func: the type of relational function ………. Contd. www.Bookspar.com | Website for Students | VTU - Notes - Question Papers
Implicit Representation • E.code: sequence of instructions that are generated for E • E.false: instruction to branch to if E evaluates to false • E.true: instruction to branch to if E evaluates to true • (E.code is synthesized whereas E.true and E.false are inherited) • id.place: location for id www.Bookspar.com | Website for Students | VTU - Notes - Question Papers
Three – Address Code Implementation I • Quadruples • Naïve representation of three address code • Simple record structure of the form • Table of k * 4 small integers • Easy to reorder • Explicit names • For the following code load r1, y loadI r2, 2 mult r3, r2, r1 load r4, x sub r5, r4, r3 thecorresponding Quadruple entry is:
Three – Address Code Implementation II • Triples • Temporary values referred with values by position of the statement that computes it • 25% less space consumed than quads • Much harder to reorder • For the following code load r1, y loadI r2, 2 mult r3, r2, r1 load r4, x sub r5, r4, r3 Index as implicit name takes no space thecorresponding Triple entry is:
0 (11) 1 (12) 2 (13) 3 (14) 4 (15) Three – Address Code Implementation III • Indirect Triples • Listing pointers to triples, rather than listing triples themselves • Uses an array to list pointers to statements • Uses more space, • but easier to reorder • For the following code load r1, y loadI r2, 2 mult r3, r2, r1 load r4, x sub r5, r4, r3 thecorresponding Indirect Triple entry is:
Hybrid IR – Control Flow graph (CFG) • Models the transfer of control in the procedure • Nodes in the graph are basic blocks ( maximum length sequential code) • Can be represented with quads or any other linear rnotation • Edges in the graph represent control flow www.Bookspar.com | Website for Students | VTU - Notes - Question Papers
Which one to use when ? • Common choice: all(!) • AST or other structural representation built by parser and used in early stages of the compiler • Closer to source code • Good for semantic analysis • Facilitates some higher-level optimizations • Flatten to linear IR for later stages of compiler • Closer to machine code • Exposes machine-related optimizations • Hybrid forms in optimization phases www.Bookspar.com | Website for Students | VTU - Notes - Question Papers
A summary of IRs • Many kinds of IR are used in practice • Best choice depends on application • There is no widespread agreement on this subject • A compiler may need several different IRs • Choose IR with right level of detail • Keep manipulation costs in mind www.Bookspar.com | Website for Students | VTU - Notes - Question Papers
Exercise 1. Translate the expression: (a + b) * (c + d ) + (a + b + c) into a. Three address code b. Quadruples c. Triples d. Indirect Triples 2. Translate the expression: a + b *c / 3 + 4 into a. Three address code b. Quadruples c. Triples d. Indirect Triples www.Bookspar.com | Website for Students | VTU - Notes - Question Papers
Example 3 Three address code • A=10 • B=12 • C= 10 • D =8 • T1 = A+B • T2 = T1 * C • T3 = E+ B • T4 = T3 / A • T5 = H * E • T6 = T5 / 20 Void main() { int A=10,B=12,C=10,D=8,E,H,R; E = A+B*C; H = E + B / A; R = H * E / 20; printf(“%d %d %d “,E,H,R); } www.Bookspar.com | Website for Students | VTU - Notes - Question Papers
Any Questions ???? Thank you www.Bookspar.com | Website for Students | VTU - Notes - Question Papers