intermediate code representations. conceptual phases of compiler lexical analysis (scanner) syntax...
TRANSCRIPT
![Page 1: Intermediate Code Representations. Conceptual phases of compiler Lexical Analysis (scanner) Syntax analysis (parser) Semantic Analysis Code optimization](https://reader035.vdocuments.us/reader035/viewer/2022062305/5697bfa11a28abf838c95d7f/html5/thumbnails/1.jpg)
Intermediate Code Representations
![Page 2: Intermediate Code Representations. Conceptual phases of compiler Lexical Analysis (scanner) Syntax analysis (parser) Semantic Analysis Code optimization](https://reader035.vdocuments.us/reader035/viewer/2022062305/5697bfa11a28abf838c95d7f/html5/thumbnails/2.jpg)
Conceptual phases of compiler
LexicalAnalysis(scanner)
Syntaxanalysis(parser)
SemanticAnalysis
Codeoptimization
Codegeneration
Sequence oftokens
Intermediatecode - IR1
Intermediatecode IR2
Optimizedcode Target code
Front Endmachine independentlanguage dependent
Middle Back Endmachine dependentlanguage independent
![Page 3: Intermediate Code Representations. Conceptual phases of compiler Lexical Analysis (scanner) Syntax analysis (parser) Semantic Analysis Code optimization](https://reader035.vdocuments.us/reader035/viewer/2022062305/5697bfa11a28abf838c95d7f/html5/thumbnails/3.jpg)
Why use an IR?
![Page 4: Intermediate Code Representations. Conceptual phases of compiler Lexical Analysis (scanner) Syntax analysis (parser) Semantic Analysis Code optimization](https://reader035.vdocuments.us/reader035/viewer/2022062305/5697bfa11a28abf838c95d7f/html5/thumbnails/4.jpg)
IR – Encodes Compiler’s Program Knowledge
Thus, some IR PROPERTIES:
•Ease of generation•Ease of manipulation•Size•Freedom of Expression•Level of Abstraction
Selecting IR is critical.
![Page 5: Intermediate Code Representations. Conceptual phases of compiler Lexical Analysis (scanner) Syntax analysis (parser) Semantic Analysis Code optimization](https://reader035.vdocuments.us/reader035/viewer/2022062305/5697bfa11a28abf838c95d7f/html5/thumbnails/5.jpg)
3 Categories of IRs
1.Structural/Graphical - AST and Concrete ST - call graph - program dependence graph (PDG)2. Linear - 3-address code - abstract stack machine code3.Hybrid - control flow graph (CFG)
![Page 6: Intermediate Code Representations. Conceptual phases of compiler Lexical Analysis (scanner) Syntax analysis (parser) Semantic Analysis Code optimization](https://reader035.vdocuments.us/reader035/viewer/2022062305/5697bfa11a28abf838c95d7f/html5/thumbnails/6.jpg)
Level of Abstraction
Consider:A[j,i] = @A + j*10 + i
[ ]
A I J
Loadi 1, R1Sub RJ, R1, R2Loadi 10, R3Mult R2, R3, R4Sub Ri, R1, r5Add R4, R5, R6Loadi @A, R7Add R7, R6, R8Load R8, RAIJ
![Page 7: Intermediate Code Representations. Conceptual phases of compiler Lexical Analysis (scanner) Syntax analysis (parser) Semantic Analysis Code optimization](https://reader035.vdocuments.us/reader035/viewer/2022062305/5697bfa11a28abf838c95d7f/html5/thumbnails/7.jpg)
Some Design Issues for IRs
Questions to Ponder:
1.What is the minimum needed in the language’s set of operators?
2.What is the advantage of a small set of operators?
2. What is the concern of designing the operationsClose to actual machine operations?
4. What is the potential problem of having a smallSet of IR operations?
![Page 8: Intermediate Code Representations. Conceptual phases of compiler Lexical Analysis (scanner) Syntax analysis (parser) Semantic Analysis Code optimization](https://reader035.vdocuments.us/reader035/viewer/2022062305/5697bfa11a28abf838c95d7f/html5/thumbnails/8.jpg)
High LevelGraphical Representations
Consider:
A -> V := E
E -> E + E | E * E | - E | id
String: a := b * - c + b * - c
Exercise: Concrete ST? AST? DAG?
![Page 9: Intermediate Code Representations. Conceptual phases of compiler Lexical Analysis (scanner) Syntax analysis (parser) Semantic Analysis Code optimization](https://reader035.vdocuments.us/reader035/viewer/2022062305/5697bfa11a28abf838c95d7f/html5/thumbnails/9.jpg)
Linear IRs: Three Address Code
• Sequence of instructions of the form
X := y op z
where x, y and z are variable names, constants, or compiler generated variables (“temporaries”)
• Only one operator is permitted on the RHS – expressions computed using temporaries
9
![Page 10: Intermediate Code Representations. Conceptual phases of compiler Lexical Analysis (scanner) Syntax analysis (parser) Semantic Analysis Code optimization](https://reader035.vdocuments.us/reader035/viewer/2022062305/5697bfa11a28abf838c95d7f/html5/thumbnails/10.jpg)
Simple Linear IRs
Write the 3 – address code for:
a := b * - c + b * - c
? = -c
= b * ?
… complete the code from the ast? The dag?
![Page 11: Intermediate Code Representations. Conceptual phases of compiler Lexical Analysis (scanner) Syntax analysis (parser) Semantic Analysis Code optimization](https://reader035.vdocuments.us/reader035/viewer/2022062305/5697bfa11a28abf838c95d7f/html5/thumbnails/11.jpg)
![Page 12: Intermediate Code Representations. Conceptual phases of compiler Lexical Analysis (scanner) Syntax analysis (parser) Semantic Analysis Code optimization](https://reader035.vdocuments.us/reader035/viewer/2022062305/5697bfa11a28abf838c95d7f/html5/thumbnails/12.jpg)
![Page 13: Intermediate Code Representations. Conceptual phases of compiler Lexical Analysis (scanner) Syntax analysis (parser) Semantic Analysis Code optimization](https://reader035.vdocuments.us/reader035/viewer/2022062305/5697bfa11a28abf838c95d7f/html5/thumbnails/13.jpg)
![Page 14: Intermediate Code Representations. Conceptual phases of compiler Lexical Analysis (scanner) Syntax analysis (parser) Semantic Analysis Code optimization](https://reader035.vdocuments.us/reader035/viewer/2022062305/5697bfa11a28abf838c95d7f/html5/thumbnails/14.jpg)
Exercise
• Give the 3 address code for:
• Z := x * y + a[j] / sum(b)
14
![Page 15: Intermediate Code Representations. Conceptual phases of compiler Lexical Analysis (scanner) Syntax analysis (parser) Semantic Analysis Code optimization](https://reader035.vdocuments.us/reader035/viewer/2022062305/5697bfa11a28abf838c95d7f/html5/thumbnails/15.jpg)
More Simple Linear IRs
Stack machine code:
push, pop, ops
Consider: x – 2 * y
Advantages?
![Page 16: Intermediate Code Representations. Conceptual phases of compiler Lexical Analysis (scanner) Syntax analysis (parser) Semantic Analysis Code optimization](https://reader035.vdocuments.us/reader035/viewer/2022062305/5697bfa11a28abf838c95d7f/html5/thumbnails/16.jpg)
Hybrid IRs
![Page 17: Intermediate Code Representations. Conceptual phases of compiler Lexical Analysis (scanner) Syntax analysis (parser) Semantic Analysis Code optimization](https://reader035.vdocuments.us/reader035/viewer/2022062305/5697bfa11a28abf838c95d7f/html5/thumbnails/17.jpg)
![Page 18: Intermediate Code Representations. Conceptual phases of compiler Lexical Analysis (scanner) Syntax analysis (parser) Semantic Analysis Code optimization](https://reader035.vdocuments.us/reader035/viewer/2022062305/5697bfa11a28abf838c95d7f/html5/thumbnails/18.jpg)
Exercise – Construct the CFG
![Page 19: Intermediate Code Representations. Conceptual phases of compiler Lexical Analysis (scanner) Syntax analysis (parser) Semantic Analysis Code optimization](https://reader035.vdocuments.us/reader035/viewer/2022062305/5697bfa11a28abf838c95d7f/html5/thumbnails/19.jpg)
Call Graph Representation
Node = function or methodEdge from A to B : A has a call site where B is potentially called
![Page 20: Intermediate Code Representations. Conceptual phases of compiler Lexical Analysis (scanner) Syntax analysis (parser) Semantic Analysis Code optimization](https://reader035.vdocuments.us/reader035/viewer/2022062305/5697bfa11a28abf838c95d7f/html5/thumbnails/20.jpg)
Exercise: Construct a call graph
![Page 21: Intermediate Code Representations. Conceptual phases of compiler Lexical Analysis (scanner) Syntax analysis (parser) Semantic Analysis Code optimization](https://reader035.vdocuments.us/reader035/viewer/2022062305/5697bfa11a28abf838c95d7f/html5/thumbnails/21.jpg)
Multiple IRs: WHIRL
![Page 22: Intermediate Code Representations. Conceptual phases of compiler Lexical Analysis (scanner) Syntax analysis (parser) Semantic Analysis Code optimization](https://reader035.vdocuments.us/reader035/viewer/2022062305/5697bfa11a28abf838c95d7f/html5/thumbnails/22.jpg)
Key Highlights of IRs