Académique Documents
Professionnel Documents
Culture Documents
Overview of Lecture 6
• Ch 7 – Code Generation
Code Generation 7.1 Code selection
7.2 A code generation algorithm
7.3 Constants and variables
HC7 7.4 Procedures and functions
7.5 Case study – Triangle compiler
Vertalerbouw HC6
VB HC6 http://fmt.cs.utwente.nl/courses/vertalerbouw/!
Michael Weber!
Theo Ruys kamer: INF 5037"
University of Twente
Department of Computer Science telefoon: 3716"
Formal Methods & Tools email: michaelw@cs.utwente.nl!
VB HC 6 Ch. 7 - Code Generation 3
Object Code The language Calc has a monolithic block structure; all variables
are global and can be allocated on the global stack (relative to SB).
Ch. 8: Interpreter (Abstract) Machine Ch. 6: Run-Time Organization
• A compiler translates a program from a high-level language into an • The translation of source language to target language is
equivalent program in a low-level language. defined inductively over the (abstract) syntax of the
! Source and target program must be semantically equivalent. language.
Given a phrase of the source language, we specify the
let PUSH 2
LOADL 2
sequence of corresponding target code instructions using
var x: integer;
var y: integer STORE(1) 1[SB] the translation of its sub-phrases.
in begin LOADL 7 Instruction* is a
y := 2;
STORE(1) 0[SB] • Code functions: sequence of target
LOAD(1) 1[SB]
x := 7; CALL putint run : Program ! Instruction* instructions.
printint(y); LOAD(1) 0[SB]
printint(x) CALL putint execute : Command ! Instruction*
end POP 2 evaluate : Expression ! Instruction* Naturally, the semantics
HALT of a language dictates
fetch : V-name ! Instruction* the code generation,
i.e., the code functions.
Code generation is concerned with the semantics of the language. assign : V-name ! Instruction*
elaborate : Declaration ! Instruction*
Expression evaluate E Evaluate the expression E, pushing its result on the stack
top, but having no other effects. • Assignment Command: V:= E
V-name fetch V Push the value of the constant or variable named V on the ! Semantics: the expression E is evaluated to yield a value:
stack. the variable identified by V is updated with this value.
V-name assign V Pop a value from the stack top, and store it in the variable
named V. ! execute [V:= E]
Declaration elaborate D Elaborate the declaration D, expanding the stack to make = evaluate [E]
space for any constants and variables declared therein. assign [V] = STORE a
where a is the address
of variable V.
VB HC 6 Ch. 7 - Code Generation 8 VB HC 6 Ch. 7 - Code Generation 9
© Theo Ruys © Theo Ruys
SeqDecl AssignCmd phrase class visitor method behaviour of the visitor method
• For variables we use two distinct code generation execute [V:= E] = evaluate [E]
assign [V]
methods: fetch and assign.
public Object visitAssignCmd(AssignCmd cmd, Object arg) {
These two methods deal with the scope information of the variables.
cmd.E.visit(this, arg);
encodeAssign(cmd.V); AssignCmd
public class Encoder implements Visitor {
... }
public void encodeFetch(Vname name) {
// as specified by fetch code template ... V E
} execute [I ( E ) ] = evaluate [E]
} CALL p
public void encodeAssign(Vname name) { public Object visitCallCmd(CallCmd cmd, Object arg) {
// as specified by assign code template ...
cmd.E.visit(this, arg);
}
} short p = address of primitive routine for name cmd.I
These methods are not implemented emit(Instruction.CALLop,
as visitor methods but as separate Instruction.SBr, CallCmd
Instruction.PBr, p);
methods of the class Encoder.
return null;
} The real encoder for the CallCmd is much more I E
complex due to parameter passing and scoping.
evaluate [E1 op E2] = evaluate [E1] execute [while E do C] = Lwhile: evaluate [E]
evaluate [E2] JUMPIF(0) Lend
CALL p execute [C]
JUMP Lwhile
public Object visitBinaryExpression( Lend:
BinaryExpression expr, Object arg) {
expr.E1.visit(this, arg); • Backwards jumps are easy: the address of the target has
expr.E2.visit(this, arg); already been generated and is known.
short p = address for expr.O operation
emit(Instruction.CALLop, • Forward jumps are harder: when the jump is generated the
Instruction.SBr, BinaryExpr
Instruction.PBr, p);
target is not yet generated so its address is not (yet) known.
}
return null; E1 O E2 • Solution: backpatching
1. Emit jump with dummy address (e.g. simply 0).
• Visiting methods for LetCmd, IfCmd, WhileCmd are more complex: 2. Remember the address where the jump instruction
occurred.
! LetCmd involves scope information
! IfCmd and WhileCmd are complicated due to jumps
3. When the target label is reached, go back and patch the
jump instruction.
VB HC 6 Ch. 7 - Code Generation 24 VB HC 6 Ch. 7 - Code Generation 25
© Theo Ruys © Theo Ruys
• Code generator: declarations and applied occurrences: public abstract class RuntimeEntity {
public short size;
! When a declaration of identifier id is encountered, the code ...
}
generator binds id to a newly created entity description. public class KnownValue extends RuntimeEntity {
– known value: just record the value + its size public short value;
...
– known address: record the address + reserve space }
public class UnknownValue extends RuntimeEntity {
! When an applied occurrence of identifier id is encountered, the public short address;
code generator consults the entity description bound to id, and ...
translates the applied occurrence w.r.t. the entity. }
public class KnownAddress extends RuntimeEntity {
public short address;
known value const declaration using a literal ...
}
unknown value const declaration using an expression
public abstract class AST {
known address variable declaration public RuntimeEntity entity;
... Mostly used within Declaration.
unknown address argument address bound to a var-parameter }
• Example: global variables • The code generator must keep track of how much
let
var size address storage has been allocated at each point in the source
var a: Integer;
var b: Boolean; a 1 [0]SB program.
var c: Integer b 1 [1]SB ! We use the extra argument Object arg to the visiting
in begin c 1 [2]SB
... methods to pass the current amount of storage in use.
end For TAM this is the case, ! Furthermore, we let a visiting method return an Object
but not for “real machines” with the extra amount of storage it needed.
• Example: nested blocks
! We encode both numbers in a Short-object.
let var a: Integer var size address
in begin
... a 1 [0]SB
public Object visitXYZ(XYZ xyz, Object arg)
let var b: Boolean; b 1 [1]SB
var c: Integer c 1 [2]SB
in ...
d 1 [1]SB
if not null, a Short-object with Short-object with the current
let var d: Integer the extra storage needed. amount storage so far.
in ... Note that variable d “reuses” the
end location of b of the previous scope.
VB HC 6 Ch. 7 - Code Generation 32 VB HC 6 Ch. 7 - Code Generation 33
© Theo Ruys © Theo Ruys