300 likes | 410 Views
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 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
E N D
COP 4620 / 5625Programming Language Translation /Compiler WritingFall 2003Lecture 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
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
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
Emitted Stack Machine Code Push_Local #b Push_Local #b Mult_Top2 Push_Cosnt 4 Push_Local #a Push_Local #c Mult_Top2 Mult_Top2 Subtr_Top2
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 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