cop 4620 / 5625 programming language translation / compiler writing fall 2003 lecture 10, 10/30/2003...
TRANSCRIPT
![Page 1: COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 10, 10/30/2003 Prof. Roy Levow](https://reader033.vdocuments.mx/reader033/viewer/2022052603/56649e925503460f94b97d8b/html5/thumbnails/1.jpg)
COP 4620 / 5625Programming Language Translation /
Compiler WritingFall 2003
Lecture 10, 10/30/2003
Prof. Roy Levow
![Page 2: COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 10, 10/30/2003 Prof. Roy Levow](https://reader033.vdocuments.mx/reader033/viewer/2022052603/56649e925503460f94b97d8b/html5/thumbnails/2.jpg)
Attribute Grammars:Manual Methods
• Attribute grammars and evaluators provide a useful framework for providing context to a program
• But the technology is not yet well developed
• And it can be difficult to use
• Thus, manual methods are frequently used
![Page 3: COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 10, 10/30/2003 Prof. Roy Levow](https://reader033.vdocuments.mx/reader033/viewer/2022052603/56649e925503460f94b97d8b/html5/thumbnails/3.jpg)
Manual Methods.2
• Symbolic Interpretation– Uses interpreter approach on AST to compute
attributes
• Data-flow Equations– More general technique based on set
equations to reveal relationships among attributes
![Page 4: COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 10, 10/30/2003 Prof. Roy Levow](https://reader033.vdocuments.mx/reader033/viewer/2022052603/56649e925503460f94b97d8b/html5/thumbnails/4.jpg)
Symbolic Interpretation
• Control flow graph can be constructed by threading nodes of AST– Possibly adding nodes for simplicity– A threading rule is provided for each kind of
node
• Then simulate run-time behavior of program
• Requires wide compiler
![Page 5: COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 10, 10/30/2003 Prof. Roy Levow](https://reader033.vdocuments.mx/reader033/viewer/2022052603/56649e925503460f94b97d8b/html5/thumbnails/5.jpg)
Flow Control & Threading
![Page 6: COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 10, 10/30/2003 Prof. Roy Levow](https://reader033.vdocuments.mx/reader033/viewer/2022052603/56649e925503460f94b97d8b/html5/thumbnails/6.jpg)
Flow Control & Threading.2
![Page 7: COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 10, 10/30/2003 Prof. Roy Levow](https://reader033.vdocuments.mx/reader033/viewer/2022052603/56649e925503460f94b97d8b/html5/thumbnails/7.jpg)
Flow Control & Threading.3
![Page 8: COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 10, 10/30/2003 Prof. Roy Levow](https://reader033.vdocuments.mx/reader033/viewer/2022052603/56649e925503460f94b97d8b/html5/thumbnails/8.jpg)
Flow Control & Threading.4
![Page 9: COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 10, 10/30/2003 Prof. Roy Levow](https://reader033.vdocuments.mx/reader033/viewer/2022052603/56649e925503460f94b97d8b/html5/thumbnails/9.jpg)
Symbolic Interpretation
• Attach a stack representation to each arrow in the control flow graph indicating the items that might be on the stack at that time in program execution with relevant attributes
• Cal also identify features such as uninitialized variables
![Page 10: COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 10, 10/30/2003 Prof. Roy Levow](https://reader033.vdocuments.mx/reader033/viewer/2022052603/56649e925503460f94b97d8b/html5/thumbnails/10.jpg)
Stack Representation of If
![Page 11: COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 10, 10/30/2003 Prof. Roy Levow](https://reader033.vdocuments.mx/reader033/viewer/2022052603/56649e925503460f94b97d8b/html5/thumbnails/11.jpg)
Symbolic Interpretation of If
![Page 12: COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 10, 10/30/2003 Prof. Roy Levow](https://reader033.vdocuments.mx/reader033/viewer/2022052603/56649e925503460f94b97d8b/html5/thumbnails/12.jpg)
Data Flow Equations
• Data Flow Equations– Used on well-structured programs– Used to evaluate properties of data values
across a program• Liveness (accessed later)• Initialized before use
– Describe condition leaving block in terms of condition on entry and effects of block
![Page 13: COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 10, 10/30/2003 Prof. Roy Levow](https://reader033.vdocuments.mx/reader033/viewer/2022052603/56649e925503460f94b97d8b/html5/thumbnails/13.jpg)
Data Flow Equations.2
• Example– Can be used to check for uninitialized variables– Solve by setting conditions for entry to initial block
and compute closure
![Page 14: COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 10, 10/30/2003 Prof. Roy Levow](https://reader033.vdocuments.mx/reader033/viewer/2022052603/56649e925503460f94b97d8b/html5/thumbnails/14.jpg)
Intermediate Code
• Intermediate Code is usually tree structured like the AST– But may be linear or have other forms
• Intermediate Code features– Expressions, including assignment– Routine class, Procedure headings, Returns– Conditional and unconditional jumps– Administrative activities
![Page 15: COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 10, 10/30/2003 Prof. Roy Levow](https://reader033.vdocuments.mx/reader033/viewer/2022052603/56649e925503460f94b97d8b/html5/thumbnails/15.jpg)
Intermediate Code.2
• Assume wide compiler
• Traverse AST replacing language context structures with intermediate code instructions
• Produce is Intermediate Code Tree but is often still referred to as AST
(Skipping 4.1 on Interpretation)
![Page 16: COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 10, 10/30/2003 Prof. Roy Levow](https://reader033.vdocuments.mx/reader033/viewer/2022052603/56649e925503460f94b97d8b/html5/thumbnails/16.jpg)
Code Generation
• Can be viewed as tree rewriting– In which tree branches are replaced by linear
code– Working through threaded AST
![Page 17: COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 10, 10/30/2003 Prof. Roy Levow](https://reader033.vdocuments.mx/reader033/viewer/2022052603/56649e925503460f94b97d8b/html5/thumbnails/17.jpg)
Simple Code GenerationStack Machine
• Identify code for each node
• Replace node with corresponding code
• Emit code in bottom-up order
![Page 18: COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 10, 10/30/2003 Prof. Roy Levow](https://reader033.vdocuments.mx/reader033/viewer/2022052603/56649e925503460f94b97d8b/html5/thumbnails/18.jpg)
Example for Stack Machine
![Page 19: COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 10, 10/30/2003 Prof. Roy Levow](https://reader033.vdocuments.mx/reader033/viewer/2022052603/56649e925503460f94b97d8b/html5/thumbnails/19.jpg)
Example for Stack Machine.2
![Page 20: COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 10, 10/30/2003 Prof. Roy Levow](https://reader033.vdocuments.mx/reader033/viewer/2022052603/56649e925503460f94b97d8b/html5/thumbnails/20.jpg)
Example for Stack Machine.3
![Page 21: COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 10, 10/30/2003 Prof. Roy Levow](https://reader033.vdocuments.mx/reader033/viewer/2022052603/56649e925503460f94b97d8b/html5/thumbnails/21.jpg)
Emit Code for Stack Machine
![Page 22: COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 10, 10/30/2003 Prof. Roy Levow](https://reader033.vdocuments.mx/reader033/viewer/2022052603/56649e925503460f94b97d8b/html5/thumbnails/22.jpg)
Emitted Stack Machine Code
Push_Local #bPush_Local #bMult_Top2Push_Cosnt 4Push_Local #aPush_Local #cMult_Top2Mult_Top2Subtr_Top2
![Page 23: COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 10, 10/30/2003 Prof. Roy Levow](https://reader033.vdocuments.mx/reader033/viewer/2022052603/56649e925503460f94b97d8b/html5/thumbnails/23.jpg)
Execution Profile for Stack Machine
![Page 24: COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 10, 10/30/2003 Prof. Roy Levow](https://reader033.vdocuments.mx/reader033/viewer/2022052603/56649e925503460f94b97d8b/html5/thumbnails/24.jpg)
Simple Code GenerationRegister Machine
• Need to assign computational values to registers– Normally start with unlimited number of
registers in intermediate code and then handle actual limitations
– Follow machine architecture for one or both operands in registers
– May specify target register for result or not
![Page 25: COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 10, 10/30/2003 Prof. Roy Levow](https://reader033.vdocuments.mx/reader033/viewer/2022052603/56649e925503460f94b97d8b/html5/thumbnails/25.jpg)
Register Code for AST Nodes
![Page 26: COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 10, 10/30/2003 Prof. Roy Levow](https://reader033.vdocuments.mx/reader033/viewer/2022052603/56649e925503460f94b97d8b/html5/thumbnails/26.jpg)
Register Code Generationwith Register Numbering
![Page 27: COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 10, 10/30/2003 Prof. Roy Levow](https://reader033.vdocuments.mx/reader033/viewer/2022052603/56649e925503460f94b97d8b/html5/thumbnails/27.jpg)
Generated Register Code
![Page 28: COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 10, 10/30/2003 Prof. Roy Levow](https://reader033.vdocuments.mx/reader033/viewer/2022052603/56649e925503460f94b97d8b/html5/thumbnails/28.jpg)
![Page 29: COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 10, 10/30/2003 Prof. Roy Levow](https://reader033.vdocuments.mx/reader033/viewer/2022052603/56649e925503460f94b97d8b/html5/thumbnails/29.jpg)
Register Code Generation
• When the number of registers is limited and more are needed than are available
• Spill values from registers to memory
• See example with 2 registers on next slide
![Page 30: COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 10, 10/30/2003 Prof. Roy Levow](https://reader033.vdocuments.mx/reader033/viewer/2022052603/56649e925503460f94b97d8b/html5/thumbnails/30.jpg)