SlideShare a Scribd company logo
1
Code Generation (Part -2)
Course : 2CS701/IT794 – Compiler
Construction
Prof Monika Shah
Nirma University
Ref : Ch.9 Compilers Principles, Techniques, and Tools by Alfred Aho, Ravi Sethi, and Jeffrey Ullman
Glimpse
Part-1
• Introduction
• Code Generation Issues
Part-2
• Basic Code Generation Case Study
• Introduction to Basic Block, and Control Flow Diagram
Prof Monika Shah (Nirma University)
2
Machine independent asm to machine dependent
Lexical Analysis
Syntax Analysis
Semantic Analysis
Intermediate Code
Generation
Optimization
Code Generation
Source
Program
Assembly
Code
IR to low-level IR
Symbol
Table
Front
end
Back
end
Optimization
Token stream
AST or Parse tree
AST or Annotted Parse tree
Code Generation
• Final Phase of Compilation
• Produces a semantically equivalent target
program
• Main Functionalities :
• Instruction selection
• Register allocation and assignment
• Instruction ordering
• Functionalities for QoS
• Produce Correct code (semantic
preserving)
• Efficiency
• Effective use of resources
• User Heuristics to generate good, but
suboptimal code
4
Case Study of Basic Code Generation
The Borland 3.0 C Compiler for the 80X86
• Expression statement with symbols
• Array reference
• Structure Field reference
• Pointer reference
• If statement
• While statement
• Function call
5
Case Study of Basic Code Generation
The Borland 3.0 C Compiler for the 80X86
Expression statement
Expression statement : (x = x +3 ) + 4
Assembly Code :
mov ax, word ptr [bp-2]
add ax, 3
mov word ptr [bp-2], ax
add ax, 4
Notes
• The bp is used as the frame pointer.
• The static simulation method is used to convert the intermediate code into
the target code.
6
Case Study of Basic Code Generation
The Borland 3.0 C Compiler for the 80X86
Array Reference
Array reference Example :
(a [ i + 1 ] = 2 ) + a [ j ]
Where, i and j are declared as
int i, j;
int a[10];
( 1 ) mov bx,word ptr [bp-2]
( 2 ) shl bx , 1
( 3 ) l ea ax, word ptr [bp-22]
( 4 ) add bx , ax
( 5 ) mov ax , 2
( 6 ) mov word ptr [bx],ax
( 7 ) mov bx,word ptr [bp-4]
( 8 ) shl bx , 1
( 9 ) l ea dx,word ptr [bp-24]
( 1 0 ) add bx , dx
( 1 1 ) add ax,word ptr [bx]
7
Case Study of Basic Code Generation
The Borland 3.0 C Compiler for the 80X86
Structure Field Reference
Structure Example
typedef struct rec
{ int i;
char c;
int j;
} Rec;
Rec x;
x.j =x.i;
mov ax, word ptr [bp-6]
mov word ptr [bp-3],ax
Note:
Integer variable has size 2 bytes;
Character variable has size 1 bytes;
Local variables are allocated only on even-byte boundaries;
The offset computation for j (-6 + 3 = -3 ) is performed statically by
the compiler.
8
Case Study of Basic Code Generation
The Borland 3.0 C Compiler for the 80X86
Pointer Field Reference
typedef struct treeNode
{ int val;
struct treeNode * lchild,
* rchild;
} TreeNode;
…
Rec x;
TreeNode *p;
The code generated for the statement
p->lchild = p;
is
mov word ptr [si+2], si
And the statement
p = p->rchild;
is
mov si, word ptr [si+4]
Note:
Assume pointer has size 2
9
Case Study of Basic Code Generation
The Borland 3.0 C Compiler for the 80X86
IF Statement
if (x>y)
y++;
else x--;
mov bx, word ptr [bp-
2] //x
mov dx, word ptr [bp -
4] //y
cmp bx , dx
jle short @1@86
inc dx
jmp short @1@114
@1@86 :
dec bx
@1@114 :
10
Case Study of Basic Code Generation
The Borland 3.0 C Compiler for the 80X86
While Statement
while (x<y)
y -= x;
jmp short @1@170
@1@142 :
sub dx , bx
@1@170 :
cmp bx , dx
jl short @1@142
11
Case Study of Basic Code Generation
The Borland 3.0 C Compiler for the 80X86
Function Call
• function definition:
int f( int x, int y)
{
return x+y+1 ;
}
• And a corresponding call
f (2+3, 4)
mov ax,4
push ax
mov ax,5
push ax
call near ptr _f
pop cx
pop cx
Notes:
The arguments are pushed on the stack in reverse order;
The caller is responsible for removing the arguments from the stack after the call.
The call instruction on the 80x86 automatically pushes the return address onto the stack.
12
Case Study of Basic Code Generation
The Borland 3.0 C Compiler for the 80X86
Function Definition
_ f proc near
push bp
mov bp , sp
mov ax, word ptr [bp+4]
add ax, word ptr [bp+6]
inc ax
jmp short @1@58
@1@58 :
pop bp
ret
_ f endp
13
Basic Blocks and Flow of Control
• Partition the intermediate code into basic blocks
• The flow of control can only enter the basic block through the first
instruction in the block. That is, there are no jumps into the middle of the
block.
• Control will leave the block without halting or branching, except possibly at
the last instruction in the block.
• The basic blocks become the nodes of a flow graph
• Use : Code Optimization, Register Allocation
14
Flow Graphs
• A flow graph is a graphical depiction of a sequence of instructions with
control flow edges
• A flow graph can be defined at the intermediate code level or target code
level
MOV 1,R0
MOV n,R1
JMP L2
L1: MUL 2,R0
SUB 1,R1
L2: JMPNZ R1,L1
MOV 0,R0
MOV n,R1
JMP L2
L1: MUL 2,R0
SUB 1,R1
L2: JMPNZ R1,L1
15
Basic Blocks
• A basic block is a sequence of consecutive instructions with exactly one entry
point and one exit point (with natural flow or a branch instruction)
MOV 1,R0
MOV n,R1
JMP L2
MOV 1,R0
MOV n,R1
JMP L2
L1: MUL 2,R0
SUB 1,R1
L2: JMPNZ R1,L1
L1: MUL 2,R0
SUB 1,R1
L2: JMPNZ R1,L1
16
Basic Blocks and Control Flow Graphs
• A control flow graph (CFG) is a directed graph with basic blocks Bi as vertices
and with edges Bi  Bj iff Bj can be executed immediately after Bi
MOV 1,R0
MOV n,R1
JMP L2
MOV 1,R0
MOV n,R1
JMP L2
L1: MUL 2,R0
SUB 1,R1
L2: JMPNZ R1,L1
L1: MUL 2,R0
SUB 1,R1
L2: JMPNZ R1,L1
17
Successor and Predecessor Blocks
• Suppose the CFG has an edge B1  B2
• Basic block B1 is a predecessor of B2
• Basic block B2 is a successor of B1
MOV 1,R0
MOV n,R1
JMP L2
L1: MUL 2,R0
SUB 1,R1
L2: JMPNZ R1,L1
18
Partition Algorithm for Basic Blocks
Input: A sequence of three-address statements
Output: A list of basic blocks with each three-address statement
in exactly one block
1.Determine the set of leaders, the first statements if basic blocks
a)The first statement is the leader
b)Any statement that is the target of a goto is a leader
c)Any statement that immediately follows a goto is a leader
2.For each leader, its basic block consist of the leader and all
statements up to but not including the next leader or the end
of the program
19
Loops
• A loop is a collection of basic blocks, such that
• All blocks in the collection are strongly connected
• The collection has a unique entry, and the only way to reach a block in the loop is
through the entry
20
Loops (Example)
MOV 1,R0
MOV n,R1
JMP L2
L1: MUL 2,R0
SUB 1,R1
L2: JMPNZ R1,L1
B1:
B2:
B3:
L3: ADD 2,R2
SUB 1,R0
JMPNZ R0,L3
B4:
Strongly connected
components:
SCC={{B2,B3},
{B4} }
Entries:
B3, B4
21
Equivalence of Basic Blocks
• Two basic blocks are (semantically) equivalent if they compute the same set of
expressions
b := 0
t1 := a + b
t2 := c * t1
a := t2
a := c * a
b := 0
a := c*a
b := 0
a := c*a
b := 0
Blocks are equivalent, assuming t1 and t2 are dead: no longer used (no longer live)
22
Transformations on Basic Blocks
• A code-improving transformation is a code optimization to improve
speed or reduce code size
• Global transformations are performed across basic blocks
• Local transformations are only performed on single basic blocks
• Transformations must be safe and preserve the meaning of the
code
• A local transformation is safe if the transformed basic block is
guaranteed to be equivalent to its original form

More Related Content

PPT
Chapter Eight(3)
PDF
Compiler unit 4
PDF
02 isa
PPT
PRESENTATION ON DATA STRUCTURE AND THEIR TYPE
PPT
456589.-Compiler-Design-Code-Generation (1).ppt
PPT
456589.-Compiler-Design-Code-Generation (1).ppt
PPTX
Compiler Design_Code generation techniques.pptx
PDF
Enscape 3D 3.6.6 License Key Crack Full Version
Chapter Eight(3)
Compiler unit 4
02 isa
PRESENTATION ON DATA STRUCTURE AND THEIR TYPE
456589.-Compiler-Design-Code-Generation (1).ppt
456589.-Compiler-Design-Code-Generation (1).ppt
Compiler Design_Code generation techniques.pptx
Enscape 3D 3.6.6 License Key Crack Full Version

Similar to Code Generation Part-2 in Compiler Construction (20)

PDF
Wondershare Filmora Crack 12.0.10 With Latest 2025
PDF
Wondershare UniConverter Crack Download Latest 2025
PDF
Skype 125.0.201 Crack key Free Download
PPT
Code_generatio.lk,jhgfdcxzcvgfhjkmnjhgfcxvfghjmh
PPT
Compiler Design Unit 5
PPTX
Introduction to Assembly Language
PPT
ERTS UNIT 3.ppt
PPT
Org-&-Arch-2.ppt
PDF
Intermediate code generation
PPT
COMPILER_DESIGN_CLASS 2.ppt
PPTX
COMPILER_DESIGN_CLASS 1.pptx
PPT
Assembly language programming_fundamentals 8086
PPTX
Compiler Design theory and various phases of compiler.pptx
PDF
01 isa
PPTX
Unit iv(simple code generator)
PDF
Compiler unit 5
PPT
Code Generations - 1 compiler design.ppt
PDF
Stale pointers are the new black
PDF
TSR_CLASS CD-UNIT 4.pdf ewqhqhqhewhwiqhe
Wondershare Filmora Crack 12.0.10 With Latest 2025
Wondershare UniConverter Crack Download Latest 2025
Skype 125.0.201 Crack key Free Download
Code_generatio.lk,jhgfdcxzcvgfhjkmnjhgfcxvfghjmh
Compiler Design Unit 5
Introduction to Assembly Language
ERTS UNIT 3.ppt
Org-&-Arch-2.ppt
Intermediate code generation
COMPILER_DESIGN_CLASS 2.ppt
COMPILER_DESIGN_CLASS 1.pptx
Assembly language programming_fundamentals 8086
Compiler Design theory and various phases of compiler.pptx
01 isa
Unit iv(simple code generator)
Compiler unit 5
Code Generations - 1 compiler design.ppt
Stale pointers are the new black
TSR_CLASS CD-UNIT 4.pdf ewqhqhqhewhwiqhe
Ad

Recently uploaded (20)

PPT
Mechanical Engineering MATERIALS Selection
PDF
PPT on Performance Review to get promotions
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PDF
composite construction of structures.pdf
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
Lecture Notes Electrical Wiring System Components
PDF
Digital Logic Computer Design lecture notes
PPTX
additive manufacturing of ss316l using mig welding
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
Construction Project Organization Group 2.pptx
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Mechanical Engineering MATERIALS Selection
PPT on Performance Review to get promotions
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
composite construction of structures.pdf
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Lecture Notes Electrical Wiring System Components
Digital Logic Computer Design lecture notes
additive manufacturing of ss316l using mig welding
UNIT-1 - COAL BASED THERMAL POWER PLANTS
R24 SURVEYING LAB MANUAL for civil enggi
UNIT 4 Total Quality Management .pptx
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Construction Project Organization Group 2.pptx
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Ad

Code Generation Part-2 in Compiler Construction

  • 1. 1 Code Generation (Part -2) Course : 2CS701/IT794 – Compiler Construction Prof Monika Shah Nirma University Ref : Ch.9 Compilers Principles, Techniques, and Tools by Alfred Aho, Ravi Sethi, and Jeffrey Ullman
  • 2. Glimpse Part-1 • Introduction • Code Generation Issues Part-2 • Basic Code Generation Case Study • Introduction to Basic Block, and Control Flow Diagram Prof Monika Shah (Nirma University) 2
  • 3. Machine independent asm to machine dependent Lexical Analysis Syntax Analysis Semantic Analysis Intermediate Code Generation Optimization Code Generation Source Program Assembly Code IR to low-level IR Symbol Table Front end Back end Optimization Token stream AST or Parse tree AST or Annotted Parse tree Code Generation • Final Phase of Compilation • Produces a semantically equivalent target program • Main Functionalities : • Instruction selection • Register allocation and assignment • Instruction ordering • Functionalities for QoS • Produce Correct code (semantic preserving) • Efficiency • Effective use of resources • User Heuristics to generate good, but suboptimal code
  • 4. 4 Case Study of Basic Code Generation The Borland 3.0 C Compiler for the 80X86 • Expression statement with symbols • Array reference • Structure Field reference • Pointer reference • If statement • While statement • Function call
  • 5. 5 Case Study of Basic Code Generation The Borland 3.0 C Compiler for the 80X86 Expression statement Expression statement : (x = x +3 ) + 4 Assembly Code : mov ax, word ptr [bp-2] add ax, 3 mov word ptr [bp-2], ax add ax, 4 Notes • The bp is used as the frame pointer. • The static simulation method is used to convert the intermediate code into the target code.
  • 6. 6 Case Study of Basic Code Generation The Borland 3.0 C Compiler for the 80X86 Array Reference Array reference Example : (a [ i + 1 ] = 2 ) + a [ j ] Where, i and j are declared as int i, j; int a[10]; ( 1 ) mov bx,word ptr [bp-2] ( 2 ) shl bx , 1 ( 3 ) l ea ax, word ptr [bp-22] ( 4 ) add bx , ax ( 5 ) mov ax , 2 ( 6 ) mov word ptr [bx],ax ( 7 ) mov bx,word ptr [bp-4] ( 8 ) shl bx , 1 ( 9 ) l ea dx,word ptr [bp-24] ( 1 0 ) add bx , dx ( 1 1 ) add ax,word ptr [bx]
  • 7. 7 Case Study of Basic Code Generation The Borland 3.0 C Compiler for the 80X86 Structure Field Reference Structure Example typedef struct rec { int i; char c; int j; } Rec; Rec x; x.j =x.i; mov ax, word ptr [bp-6] mov word ptr [bp-3],ax Note: Integer variable has size 2 bytes; Character variable has size 1 bytes; Local variables are allocated only on even-byte boundaries; The offset computation for j (-6 + 3 = -3 ) is performed statically by the compiler.
  • 8. 8 Case Study of Basic Code Generation The Borland 3.0 C Compiler for the 80X86 Pointer Field Reference typedef struct treeNode { int val; struct treeNode * lchild, * rchild; } TreeNode; … Rec x; TreeNode *p; The code generated for the statement p->lchild = p; is mov word ptr [si+2], si And the statement p = p->rchild; is mov si, word ptr [si+4] Note: Assume pointer has size 2
  • 9. 9 Case Study of Basic Code Generation The Borland 3.0 C Compiler for the 80X86 IF Statement if (x>y) y++; else x--; mov bx, word ptr [bp- 2] //x mov dx, word ptr [bp - 4] //y cmp bx , dx jle short @1@86 inc dx jmp short @1@114 @1@86 : dec bx @1@114 :
  • 10. 10 Case Study of Basic Code Generation The Borland 3.0 C Compiler for the 80X86 While Statement while (x<y) y -= x; jmp short @1@170 @1@142 : sub dx , bx @1@170 : cmp bx , dx jl short @1@142
  • 11. 11 Case Study of Basic Code Generation The Borland 3.0 C Compiler for the 80X86 Function Call • function definition: int f( int x, int y) { return x+y+1 ; } • And a corresponding call f (2+3, 4) mov ax,4 push ax mov ax,5 push ax call near ptr _f pop cx pop cx Notes: The arguments are pushed on the stack in reverse order; The caller is responsible for removing the arguments from the stack after the call. The call instruction on the 80x86 automatically pushes the return address onto the stack.
  • 12. 12 Case Study of Basic Code Generation The Borland 3.0 C Compiler for the 80X86 Function Definition _ f proc near push bp mov bp , sp mov ax, word ptr [bp+4] add ax, word ptr [bp+6] inc ax jmp short @1@58 @1@58 : pop bp ret _ f endp
  • 13. 13 Basic Blocks and Flow of Control • Partition the intermediate code into basic blocks • The flow of control can only enter the basic block through the first instruction in the block. That is, there are no jumps into the middle of the block. • Control will leave the block without halting or branching, except possibly at the last instruction in the block. • The basic blocks become the nodes of a flow graph • Use : Code Optimization, Register Allocation
  • 14. 14 Flow Graphs • A flow graph is a graphical depiction of a sequence of instructions with control flow edges • A flow graph can be defined at the intermediate code level or target code level MOV 1,R0 MOV n,R1 JMP L2 L1: MUL 2,R0 SUB 1,R1 L2: JMPNZ R1,L1 MOV 0,R0 MOV n,R1 JMP L2 L1: MUL 2,R0 SUB 1,R1 L2: JMPNZ R1,L1
  • 15. 15 Basic Blocks • A basic block is a sequence of consecutive instructions with exactly one entry point and one exit point (with natural flow or a branch instruction) MOV 1,R0 MOV n,R1 JMP L2 MOV 1,R0 MOV n,R1 JMP L2 L1: MUL 2,R0 SUB 1,R1 L2: JMPNZ R1,L1 L1: MUL 2,R0 SUB 1,R1 L2: JMPNZ R1,L1
  • 16. 16 Basic Blocks and Control Flow Graphs • A control flow graph (CFG) is a directed graph with basic blocks Bi as vertices and with edges Bi  Bj iff Bj can be executed immediately after Bi MOV 1,R0 MOV n,R1 JMP L2 MOV 1,R0 MOV n,R1 JMP L2 L1: MUL 2,R0 SUB 1,R1 L2: JMPNZ R1,L1 L1: MUL 2,R0 SUB 1,R1 L2: JMPNZ R1,L1
  • 17. 17 Successor and Predecessor Blocks • Suppose the CFG has an edge B1  B2 • Basic block B1 is a predecessor of B2 • Basic block B2 is a successor of B1 MOV 1,R0 MOV n,R1 JMP L2 L1: MUL 2,R0 SUB 1,R1 L2: JMPNZ R1,L1
  • 18. 18 Partition Algorithm for Basic Blocks Input: A sequence of three-address statements Output: A list of basic blocks with each three-address statement in exactly one block 1.Determine the set of leaders, the first statements if basic blocks a)The first statement is the leader b)Any statement that is the target of a goto is a leader c)Any statement that immediately follows a goto is a leader 2.For each leader, its basic block consist of the leader and all statements up to but not including the next leader or the end of the program
  • 19. 19 Loops • A loop is a collection of basic blocks, such that • All blocks in the collection are strongly connected • The collection has a unique entry, and the only way to reach a block in the loop is through the entry
  • 20. 20 Loops (Example) MOV 1,R0 MOV n,R1 JMP L2 L1: MUL 2,R0 SUB 1,R1 L2: JMPNZ R1,L1 B1: B2: B3: L3: ADD 2,R2 SUB 1,R0 JMPNZ R0,L3 B4: Strongly connected components: SCC={{B2,B3}, {B4} } Entries: B3, B4
  • 21. 21 Equivalence of Basic Blocks • Two basic blocks are (semantically) equivalent if they compute the same set of expressions b := 0 t1 := a + b t2 := c * t1 a := t2 a := c * a b := 0 a := c*a b := 0 a := c*a b := 0 Blocks are equivalent, assuming t1 and t2 are dead: no longer used (no longer live)
  • 22. 22 Transformations on Basic Blocks • A code-improving transformation is a code optimization to improve speed or reduce code size • Global transformations are performed across basic blocks • Local transformations are only performed on single basic blocks • Transformations must be safe and preserve the meaning of the code • A local transformation is safe if the transformed basic block is guaranteed to be equivalent to its original form