You are on page 1of 14

Target Machine &

Simple Code Generator


Deekshant Yadav
Outline
• Target Machine
• Addressing Mode & Instruction Cost
• Code Generator
• Register and Address Descriptor
• Algorithm for Simple Code Generation
Where it stands
1. Lexical Analysis
2. Syntax Analysis
3. Semantic Analysis
4. Intermediate Code Generation
5. Code Optimization
6. Target Code Generation
Target Machine
• A target machine refers to a 4 byte addressable machine with n
number of registers
• The target machine is capable of two address instruction of form
operation source, destination .
• Operation is described by an op-code, source and destination are
described with an addressing mode
Addressing Mode
MODE FORM ADDRESS EXAMPLE COST

absolute M M MOV a, R0 1

register R R MOV R0, a 0

indexed c(R) C+ contents(R) MOV 4(R0), R1 1

indirect register *R contents(R) MOV *R0, R1 0

contents(c+
indirect indexed *c(R) MOV *3(R2), R1 1
contents(R))

literal #c NA MOV #5, R1 1


Instruction Cost
• Instruction Cost shows how much space an instruction will occupy
• Instruction Cost = 1 + (cost of source + cost of destination)

• MOV R0, a
• Total Cost = 1 + 0 (register) + 1 (absolute) = 2
• ADD #10, 4(R0)
• Total Cost = 1 + 1 (literal) + 1 (indexed addressing) = 3
Code Generator
• Code generator is used to generate target code for three address
statement, which consumes the registers for storing operands
• Example:
• Input:
• x=y+z
• Output:
• MOV y, R1
• MOV z, R2
• ADD R1, R2
• MOV R2, x
Register and Address Descriptor
• Register Descriptor – Keep track of what is currently stored in the
register, initially every register will be empty
• Address Descriptor – An address descriptor is used to store the
location where current value of the name can be found at run time
Simple Code Generator
• For each three address statement in form x = y op z .

1. Get the location L where the result x will be stored by invoking


getreg function

2. Get current location of y as y' using address descriptor, if y' is not


in L generate MOV y',L .
Simple Code Generator
3. Generate OP z', L where z' is current location of z. Update
address descriptor of x to indicate the x is in L, If L is a register
update register descriptor to indicate the it contain the value of x

4. If y and z has no further use update the register descriptor to


indicate the those register no longer contains y and z
Example
a = b + (c - d)

1. Three Address Statement


•t = c - d
•a = b + t

2. Two address instruction set


Example
Statement Code Generated Register Descriptor Address Descriptor

t = c - d MOV c, R0 R0 contains t t in R0
SUB d, R0

a = b + t MOV b, R1 R0 contains t t in R0
ADD t, R1 R1 contains a a in R1
MOV R1, a
Example
void main(){ main:
int a = 5; movl $5, -4(%rbp)
movl $6, -8(%rbp)
int b = 6;
movl $2, -12(%rbp)
int c = 2; movl -8(%rbp), %eax
int d = a + b * c; imull -12(%rbp), %eax
} movl %eax, %edx
movl -4(%rbp), %eax
addl %edx, %eax
movl %eax, -16(%rbp)
nop
addq $48, %rsp
ret
Thank You!

You might also like