Languages and Compilers (SProg og Overs - PowerPoint PPT Presentation

About This Presentation
Title:

Languages and Compilers (SProg og Overs

Description:

Languages and Compilers (SProg og Overs ttere) Code Generation – PowerPoint PPT presentation

Number of Views:94
Avg rating:3.0/5.0
Slides: 17
Provided by: aau49
Category:

less

Transcript and Presenter's Notes

Title: Languages and Compilers (SProg og Overs


1
Languages and Compilers(SProg og Oversættere)
  • Code Generation

2
Code Generation
  • Describe the purpose of the code generator
  • Code templates
  • Back patching

3
The Phases of a Compiler
Source Program
Syntax Analysis
Error Reports
Abstract Syntax Tree
Contextual Analysis
Error Reports
Decorated Abstract Syntax Tree
Code Generation
Object Code
4
Multi Pass Compiler
A multi pass compiler makes several passes over
the program. The output of a preceding phase is
stored in a data structure and used by subsequent
phases.
Dependency diagram of a typical Multi Pass
Compiler
Compiler Driver
calls
calls
calls
Syntactic Analyzer
Contextual Analyzer
Code Generator
5
Issues in Code Generation
  • Code Selection
  • Deciding which sequence of target machine
    instructions will be used to implement each
    phrase in the source language.
  • Storage Allocation
  • Deciding the storage address for each variable in
    the source program. (static allocation, stack
    allocation etc.)
  • Register Allocation (for register-based machines)
  • How to use registers efficiently to store
    intermediate results.

6
Code Generation
Source Program
Target program
let var n integer var c charin begin c
n n1end
PUSH 2LOADL 38STORE 1SBLOAD 0LOADL 1CALL
addSTORE 0SBPOP 2HALT
Source and target program must be semantically
equivalent
Semantic specification of the source language is
structured in terms of phrases in the SL
expressions, commands, etc. gt Code generation
follows the same inductive structure.
Q Can you see the connection with formal
semantics?
7
Specifying Code Generation with Code Templates
Example Code templates specification for Mini
Triangle
RECAP The mini triangle AST
Program Command Program Command
V-name Expression AssignCmd let
Declaration in Command LetCmd ... Expression
Integer-Literal IntegerExp
V-name VnameExp Operator
Expression UnaryExp Expression Op
Expression BinaryExp Declaration
... V-name Identifier SimpleVName
8
Specifying Code Generation with Code Templates
The code generation functions for Mini Triangle
Phrase Class Function Effect of the generated
code
Run program P then halt. Starting and finishing
with empty stack Execute Command C. May update
variables but does not shrink or grow the
stack! Evaluate E, net result is pushing the
value of E on the stack. Push value of constant
or variable on the stack. Pop value from stack
and store in variable V Elaborate declaration,
make space on the stack for constants and
variables in the decl.
Program Command Expres- sion V-name V-name Decl
a-ration
run P execute C evaluate E fetch V assign
V elaborate D
9
Code Generation with Code Templates
The code generation functions for Mini Triangle
Programs
run C execute C HALT
Commands
execute V E evaluate E assign
V execute I ( E ) evaluate E CALL
p where p is address of the routine named I
10
Code Generation with Code Templates
Commands
execute C1 C2 execute C1 execute
C2 execute if E then C1 else C2 evaluate
E JUMPIF(0) g execute C1 JUMP h g execute
C2 h
11
Code Generation with Code Templates
While command
execute while E do C JUMP h g execute
C h evaluateE JUMPIF(1) g
C
E
12
Developing a Code Generator Visitor
execute C1 C2 executeC1 executeC2
public Object visitSequentialCommand( Sequent
ialCommand com,Object arg) com.C1.visit(this,a
rg) com.C2.visit(this,arg) return null
LetCommand, IfCommand, WhileCommand gt later. -
LetCommand is more complex memory allocation and
addresses - IfCommand and WhileCommand
complications with jumps
13
Backpatching Example
public Object WhileCommand ( WhileCommand
com,Object arg) short j nextInstrAddr emit
(Instruction.JUMPop, 0,
Instruction.CBr,0) short g nextInstrAddr com
.C.visit(this,arg) short h nextInstrAddr cod
ej.d h com.E.visit(this,arg) emit(Instruc
tion.JUMPIFop, 1, Instruction.CBr,g) ret
urn null
dummy address
backpatch
execute while E do C JUMP h g execute
C h evaluateE JUMPIF(1) g
14
Constants and Variables
We have not yet discussed generation of
LetCommand. This is the place in MiniTriangle
where declarations are.
execute let D in C elaborateD execute
C POP(0) s if sgt0 where s amount of
storage allocated by D
How to know these?
fetch V LOAD dSB where d address of V
relative to SB assign V STORE dSB where
d address of V relative to SB
15
Code Template Global Procedure
elaborate proc I () C JUMP g e execute
C RETURN(0) 0 g
C
execute I () CALL(SB) e
16
Code Template Global Procedure
Example
let var n Integer proc double() n
n2 in begin n 9 double() end
var n Integer
0 PUSH 1 1 JUMP 7 2 LOAD 0SB 3 LOADL 2 4 CA
LL mult 5 STORE 0SB 6 RETURN(0) 0 7 LOADL 9 8
STORE 0SB 9 CALL(SB) 2 10POP(0) 1 11HALT
proc double() n n2
n n2
n 9
double()
Write a Comment
User Comments (0)
About PowerShow.com