Prof. Roy Levow
description
Transcript of Prof. Roy Levow
COP 4620 / 5625Programming Language Translation /
Compiler WritingFall 2003
Lecture 10, 10/30/2003
Prof. Roy Levow
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
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
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
Flow Control & Threading
Flow Control & Threading.2
Flow Control & Threading.3
Flow Control & Threading.4
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
Stack Representation of If
Symbolic Interpretation of If
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
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
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
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)
Code Generation
• Can be viewed as tree rewriting– In which tree branches are replaced by linear
code– Working through threaded AST
Simple Code GenerationStack Machine
• Identify code for each node• Replace node with corresponding code• Emit code in bottom-up order
Example for Stack Machine
Example for Stack Machine.2
Example for Stack Machine.3
Emit Code for Stack Machine
Emitted Stack Machine CodePush_Local #bPush_Local #bMult_Top2Push_Cosnt 4Push_Local #aPush_Local #cMult_Top2Mult_Top2Subtr_Top2
Execution Profile for Stack Machine
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
Register Code for AST Nodes
Register Code Generationwith Register Numbering
Generated Register Code
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