TSR - Class Cd-Unit 4
TSR - Class Cd-Unit 4
1/28/2025 1
COURSE CONTENT
UNIT I Introduction to Compilers 9
Need and role of the parser- Context Free Grammars-Top Down parsing –
Recursive Descent Parser - Predictive Parser - LL (1) Parser -Shift Reduce Parser -
LR Parser - LR (0) item - Construction of SLR Parsing table -Introduction to LALR
Parser, YACC- Design of a syntax analyser for a sample language
Issues in the design of code generator – The target machine – Runtime Storage
management – Basic Blocks and Flow Graphs – Next-use Information – A simple Code
generator – DAG representation of Basic Blocks
The methods commonly used in compilers can be classified as being either top-
down or bottom-up.
Top-down methods build parse trees from the top (root) to the bottom (leaves),
while
Bottom-up methods start from the leaves and work their way up to the root.
– Instruction Selection
– Instruction Ordering
2 • Target program
3 • Instruction selection
• Memory management
4
5
• Register allocation
6
• Evaluation order
• For Example,
LITERALS #C C 1
MOV a,R0
ADD b,R0
t1:=a+b MOV R0,t1
t2:=c+d MOV c,R1
a+b-(c+d)*e ADD d,R1
t3:=e*t2
t4:=t1-t3 MOV e,R0 MOV c,R0
MUL R1,R0 ADD d,R0
MOV t1,R1 MOV e,R1
reorder
SUB R0,R1 MUL R0,R1
t2:=c+d MOV R1,t4 MOV a,R0
t3:=e*t2 ADD b,R0
t1:=a+b SUB R1,R0
t4:=t1-t3 MOV R0,t4
MOV *4(R0),M
Store contents(contents(4+contents(R0))) into M 3
STATIC ALLOCATION
STACK ALLOCAION
1/28/2025 T SAJU RAJ 59
ACTIVATION RECORD
stack
free space
heap
temporaries
Heap allocation –
- allocates and deallocates stroage as needed at
runtime from a data area known as heap.
Program sort
var
procedure readarray;
Main
….
function partition(…)
….
readarray quicksort(1, 9)
procedure quicksort(…)
……
partition
quicksort
partition(1, 9) quicksort(1, 3)
quicksort
….
Begin
……
partition(1, 3) quicksort(1, 0)
readarray
quicksort
1/28/2025 T SAJU RAJ 69
end
Explanation - Stack Allocation
Register Address
descriptor descriptor
➢Consult the address descriptor for y to determine y’, the current location
of y. If the value of y is not already in L, generate the instruction MOV y’ , L
to place a copy of y in L .
➢If the current values of y or z have no next uses, are not live on exit from
the block, and are in register s, alter the register descriptor to indicate
that, after execution of x : = y op z , those registers will no longer contain y
or z.
T SAJU RAJ 81
1/28/2025
Code Generator Algorithm
T SAJU RAJ 82
1/28/2025
A SIMPLE CODE GENERATOR-EXAMPLE
R0 = X X = R0
R1 = Y Y = R1(Z =MEMORY
T SAJU RAJ 85
1/28/2025
Simple Code Generator
T SAJU RAJ 86
1/28/2025
Simple Code Generator – Indexed Assignment
T SAJU RAJ 87
1/28/2025
Simple Code Generator - Pointer Assignment
T SAJU RAJ 88
1/28/2025
Simple Code Generator – Conditional Statements
T SAJU RAJ 89
1/28/2025
DAG representation for basic blocks
T1 = a + b
T2 = T1 + c
T3 = T1 x T2
(1) a = b x c
(2) d = b
(3) e = d x c
(4) b = e
(5) f = b + c
(6) g = f + d
T1 = a + b
T2 = T1 + c
T3 = T1 x T2
(1) a = b x c
(2) d = b
(3) e = d x c
(4) b = e
(5) f = b + c
(6) g = f + d
a=b+c
b=a-d
c=b+c
d=a-d
• a = b + c;
•b=b-d
•c=c+d
•e=b+c
Output -
o label for each node.
o label is an identifier for leaves.
o label is an operator symbols for
interior nodes.
o each node has list of identifiers.
Method -
o create nodes with one or two children left & right.
o create linked list of attached identifiers for each node.
o maintain all identifiers for which a node is associated.
o node (identifier) represents value that identifier has the
current point in dag construction process.
o Symbol table record for identifier- indicate the value of
node(identifier).
Basic Blocks
A basic block is a sequence of consecutive statements in
which flow of control enters at the beginning and leaves at
the end without any halt or possibility of branching
except at the end.
Method:
1. For each leader, its basic block consists of the leader and all
statements up to but not including the next leader or the end of the
Basic Blocks and Flow
Graphs
Construct the Basic Blocks
for the following statements
L1: I=1
I=1
While (I < 10)
L2: if (I < 10) then
{
goto TRUE
a = b[i] + c;
i++;
L3: goto EXIT
}
Result = a + c
L4: T1 = b[i]
I=1 T2 = T1 + c
WSTART: if (I < 10) a = T2
then goto TRUE T3 = I + 1
goto EXIT I = T3
TRUE: T1 = b[i] goto WSTART
T2 = T1 + c
a = T2 L5: T4 = a + c
T3 = I + 1 Result = T4
I = T3
goto WSTART
EXIT: T4 = a + c
Result = T4
Basic Blocks and Flow
Graphs
Flow Graphs
Flow graph is a directed graph containing the flow-of-control
information for the set of basic blocks making up a program.
The
L1:nodes of theI flow
= 1 graph are basic blocks.
L2: if (I < 10) then I=1
goto TRUE
L3: goto EXIT if (I < 10) then goto
L4: T1 = b[i] TRUE
T2 = T1 + c
goto
a = T2
EXIT
T3 = I + 1
I = T3 T1 = b[i]
goto WSTART T2 = T1 + c
L5: T4 = a + c a = T2
• B1 is the initial node. B2 immediately
Result = T4 T3 = I + 1
follows B1, so there is an edge from B1 to
B2. The target of jump from last statement I = T3
of B1 is the first statement B2, so there is goto WSTARTT
an edge from B1 (last statement) to B2 (first
statement).
T4 = a + c
• B1 is the predecessor of B2, and B2 is a Result =
successor of B1. T4