30
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 10, 10/30/2003 Prof. Roy Levow

COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 10, 10/30/2003 Prof. Roy Levow

Embed Size (px)

Citation preview

Page 1: COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 10, 10/30/2003 Prof. Roy Levow

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

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

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

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

Flow Control & Threading

Page 6: COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 10, 10/30/2003 Prof. Roy Levow

Flow Control & Threading.2

Page 7: COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 10, 10/30/2003 Prof. Roy Levow

Flow Control & Threading.3

Page 8: COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 10, 10/30/2003 Prof. Roy Levow

Flow Control & Threading.4

Page 9: COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 10, 10/30/2003 Prof. Roy Levow

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

Stack Representation of If

Page 11: COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 10, 10/30/2003 Prof. Roy Levow

Symbolic Interpretation of If

Page 12: COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 10, 10/30/2003 Prof. Roy Levow

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

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

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

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

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

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

Example for Stack Machine

Page 19: COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 10, 10/30/2003 Prof. Roy Levow

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

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

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

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

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

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

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

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

Generated Register Code

Page 28: COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 10, 10/30/2003 Prof. Roy Levow
Page 29: COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 10, 10/30/2003 Prof. Roy Levow

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