Download presentation
Presentation is loading. Please wait.
Published byJunior O’Neal’ Modified over 9 years ago
1
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 10, 10/30/2003 Prof. Roy Levow
2
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
3
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
4
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
5
Flow Control & Threading
6
Flow Control & Threading.2
7
Flow Control & Threading.3
8
Flow Control & Threading.4
9
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
10
Stack Representation of If
11
Symbolic Interpretation of If
12
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
13
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
14
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
15
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)
16
Code Generation Can be viewed as tree rewriting –In which tree branches are replaced by linear code –Working through threaded AST
17
Simple Code Generation Stack Machine Identify code for each node Replace node with corresponding code Emit code in bottom-up order
18
Example for Stack Machine
19
Example for Stack Machine.2
20
Example for Stack Machine.3
21
Emit Code for Stack Machine
22
Emitted Stack Machine Code Push_Local #b Mult_Top2 Push_Cosnt4 Push_Local #a Push_Local #c Mult_Top2 Subtr_Top2
23
Execution Profile for Stack Machine
24
Simple Code Generation Register 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
25
Register Code for AST Nodes
26
Register Code Generation with Register Numbering
27
Generated Register Code
29
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
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.