SlideShare a Scribd company logo
UNIT IV
RUN-TIME ENVIRONMENT AND CODE GENERATION
Runtime Environments – source language issues – Storage organization – Storage Allocation
Strategies: Static, Stack and Heap allocation - Parameter Passing - Symbol Tables - Dynamic
Storage Allocation - Issues in the Design of a code generator – Basic Blocks and Flow graphs
- Design of a simple Code Generator - Optimal Code Generation for Expressions – Dynamic
Programming Code Generation
RUN-TIME ENVIRONMENT - STORAGE ORGANIZATION
 When the target program executes then it runs in its own
logical address space
 Logical address space is shared among the compiler,
operating system and target machine for management and
organization
 OS is used to map the logical address into physical
address
Subdivision of Run-time Memory
 Runtime storage comes into blocks, where a byte is the
smallest unit of addressable memory
 Run-time storage can be subdivide to hold the different
components of an executing program:
o Generated executable code
o Static data objects
o Dynamic data-object- heap
o Automatic data objects- stack
Activation Record
 Control stack is a run time stack which is used to keep track of
the live procedure activations i.e. it is used to find out the
procedures whose execution have not been completed.
 When it is called (activation begins) then the procedure name will
push on to the stack
 when it returns (activation ends) then it will popped.
 An activation record is pushed into the stack when a procedure
is called and it is popped when the control returns to the caller
function.
The diagram below shows the contents of activation records
 Return Value: used by calling procedure to return a value to calling procedure.
 Actual Parameter: used by calling procedures to supply parameters to the
called procedures.
 Control Link: It points to activation record of the caller.
 Access Link: used to refer to non-local data held in other activation records.
 Saved Machine Status: Holds the information about status of machine before
the procedure is called.
 Local Data: Holds the data that is local to the execution of the procedure.
 Temporaries: Stores the value that arises in the evaluation of an expression.
Storage Allocation Strategies
The different ways to allocate memory are:
 Static storage allocation
 Stack storage allocation
 Heap storage allocation
Static storage allocation
 In this, names are bound to storage locations
 If memory is created at compile time then the memory will be
created in static area and only once
 Supports the dynamic data structure that means memory is created
only at compile time and deallocated after program completion
Drawbacks:
 Size and position of data objects should be known at compile time
& restriction of the recursion procedure
Stack Storage Allocation
 In this, storage is organized as a stack
 An activation record is pushed into the stack when activation
begins and it is popped when the activation end
 Activation record contains the locals so that they are bound to
fresh storage in each activation record.
 The value of locals is deleted when the activation ends
 Works on last-in-first-out (LIFO)
 This allocation supports the recursion process
Heap Storage Allocation
 Most flexible allocation scheme
 Allocation and deallocation of memory can be done at any time
and at any place depending upon the user's requirement
 Used to allocate memory to the variables dynamically
 Supports the recursion process
Code Generator
 Code generator is used to produce the target code for three-
address statements.
 It uses registers to store the operands of the three address
statement.
Example:
Consider the three address statement x:= y + z.
It can have the following sequence of codes:
 MOV x, R0 ADD y, R0
Register and Address Descriptors:
 A register descriptor contains the track of what is currently
in each register
 The register descriptors show that all the registers are
initially empty
 An address descriptor is used to store the location where
current value of the name can be found at run time
Generating Code for Assignment Statements:
The assignment statement d:= (a-b) + (a-c) + (a-c)
can be translated into following sequence of three address code:
t:= a-b
u:= a-c
v:= t +u
d:= v+u
UNIT IV Compiler.pptx RUNTIMEENVIRONMENT
Design Issues in Code Generator
Various code generation phase are :
 Input to the code generator
 Target program
 Memory management
 Instruction selection
 Register allocation
 Evaluation order
1. Input to the code generator
 The input to the code generator contains the intermediate representation
of the source program and the information of the symbol table
 The source program is produced by the front end
 Intermediate representation has the several choices:
a) Postfix notation
b) Syntax tree
c) Three address code
 The code generation phase needs complete error-free intermediate code
as an input requires
2. Target program:
 The target program is the output of the code generator.
The output can be:
a) Assembly language: It allows subprogram to be
separately compiled
b) Relocatable machine language: It makes the process of
code generation easier
c) Absolute machine language: It can be placed in a fixed
location in memory and can be executed immediately.
3. Memory management
 Mapping name in the source program to address of data is done
by the front end and code generator
 A name in the three address statements refers to the symbol
table entry for name.
 Local variables are stack allocation in the activation record
while global variables are in static area
4. Instruction selection:
 Nature of instruction set of the target machine should be
complete and uniform.
 The quality of the generated code can be determined by its
speed and size.
Example
The Three address code is:
a:= b + c
d:= a + e
Inefficient assembly code is:
MOV b, R0 R0→b
ADD c, R0 R0 → c + R0
MOV R0, a a → R0
MOV a, R0 R0→ a
ADD e, R0 R0 → e + R0
MOV R0, d d → R0
5. Register allocation
 Register can be accessed faster than memory.
 The instructions involving operands in register are shorter and faster
than those involving in memory operand.
The following sub problems arise when we use registers:
 Register allocation: In this, Selects set of variables that will reside in
register.
 Register assignment: In this, Picks the register that contains variable.
 Certain machine requires even-odd pairs of registers for some operands
and result.
For example:
 Consider the following division instruction of the form:
D x, y
Where,
 x is the dividend even register in even/odd register pair
 y is the divisor
 Even register is used to hold the reminder.
 Old register is used to hold the quotient.
6. Evaluation order
 The code generator decides the order in which the instruction
will be executed.
 The order of computations affects the efficiency of the target
code
 Some computation orders need fewer registers to hold results
of intermediate than others.
Approaches to code generation issues:
Code generator must always generate the correct
code. Some of the design goals of code generator are:
 Correct
 Easily maintainable
 Testable
 Efficient
BASIC BLOCKS
 Source codes which are always executed in sequence
are considered as the basic blocks of the code.
 These basic blocks do not have any jump statements
among them
 A program can have various constructs as basic
blocks, like IF-THEN-ELSE,
 SWITCH-CASE conditional statements and
 loops such as DO-WHILE, FOR, and REPEAT-
UNTIL, etc.
Basic block identification
 Search header statements of all the basic blocks
from where a basic block starts:
 First statement of a program.
 Statements that are target of any branch
(conditional/unconditional).
 Statements that follow any branch statement.
 Header statements and the statements following
them form a basic block.
 A basic block does not include any header
statement of any other basic block.
UNIT IV Compiler.pptx RUNTIMEENVIRONMENT
DAG representation of Basic Blocks
 Directed a cyclic graphs (dags) are useful data structures for
implementing transformation on basic blocks.
 The Dag for the statement x := y + z is:
The Dag for the statements.
t1:= 4 * i
t2:= a[t ]
is
BASIC BLOCKS AND FLOW GRAPHS
 A graph representation of 3 address statements
is called a flow graph.
 Nodes in the flow graph represent computations
and the edges represents the flow of control.
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 halt or
possibility of branching except at the end.
 The following sequence of three address,
statements forms a basic block:
t:= a * a
t:= a * b
t:= 2 * t
t:= t+ t
begin
sum := 0
i := 1
do begin
sum := sum + a[i] + b[i];
i = i + 1;
end
While i < - 10
end
Algorithm - Partition into basic blocks
Input: A sequence of three address statements.
Output: A list of basic blocks with each three
address statement in exactly one block.
Source code to compute the sum of 2 arrays
List of 3 address statements:
1) sum := 0
2) i = 1
3) t1 := 4 * i
4) t2 := a[t1]
5) t3 := 4 * i
6) t4 := b[t3]
7) t5 := t2 + t4
8) t6 := sum + t5
9) sum := t6
10) t7 := i + 1
11) i = t7
12) i < = 10 goto 3
Basic Blocks

More Related Content

Similar to UNIT IV Compiler.pptx RUNTIMEENVIRONMENT (20)

PPT
PRESENTATION ON DATA STRUCTURE AND THEIR TYPE
nikhilcse1
 
PPT
456589.-Compiler-Design-Code-Generation (1).ppt
MohibKhan79
 
PPTX
Issues in design_of_code_generator
vinithapanneer
 
PPTX
Unit iv(simple code generator)
Kalaimathi Vijayakumar
 
PPT
ERTS UNIT 3.ppt
Pavithra525349
 
PDF
Compiler unit 4
BBDITM LUCKNOW
 
PDF
Wondershare UniConverter Crack Download Latest 2025
tanveerbhaikp06
 
PDF
Enscape 3D 3.6.6 License Key Crack Full Version
alihamzakpa09
 
PDF
Wondershare Filmora Crack 12.0.10 With Latest 2025
alihamzakpa010
 
PDF
Internet Download Manager (IDM) 6.42.27 Crack Latest 2025
umnazadiwe
 
PDF
Skype 125.0.201 Crack key Free Download
alihamzakpa015
 
PDF
Wondershare Recoverit Crack for MacOS Full Download (Latest 2025)
abidkhan77g77
 
PPT
Code_generatio.lk,jhgfdcxzcvgfhjkmnjhgfcxvfghjmh
sneharaju2025
 
PDF
Download GRAPHISOFT ArchiCAD Crack 28.1.0.4001 Full Version 2025
am2612067
 
PDF
New Wondershare UniConverter Crack Free Download (Latest 2025)
am2612067
 
PPTX
iii-ii cd nCompiler design UNIT-V-1.pptx
nandan543979
 
PPTX
Code Generation Part-2 in Compiler Construction
ProfMonikaShah
 
PPT
Lecture 16 17 code-generation
Iffat Anjum
 
PDF
Compiler unit 5
BBDITM LUCKNOW
 
PRESENTATION ON DATA STRUCTURE AND THEIR TYPE
nikhilcse1
 
456589.-Compiler-Design-Code-Generation (1).ppt
MohibKhan79
 
Issues in design_of_code_generator
vinithapanneer
 
Unit iv(simple code generator)
Kalaimathi Vijayakumar
 
ERTS UNIT 3.ppt
Pavithra525349
 
Compiler unit 4
BBDITM LUCKNOW
 
Wondershare UniConverter Crack Download Latest 2025
tanveerbhaikp06
 
Enscape 3D 3.6.6 License Key Crack Full Version
alihamzakpa09
 
Wondershare Filmora Crack 12.0.10 With Latest 2025
alihamzakpa010
 
Internet Download Manager (IDM) 6.42.27 Crack Latest 2025
umnazadiwe
 
Skype 125.0.201 Crack key Free Download
alihamzakpa015
 
Wondershare Recoverit Crack for MacOS Full Download (Latest 2025)
abidkhan77g77
 
Code_generatio.lk,jhgfdcxzcvgfhjkmnjhgfcxvfghjmh
sneharaju2025
 
Download GRAPHISOFT ArchiCAD Crack 28.1.0.4001 Full Version 2025
am2612067
 
New Wondershare UniConverter Crack Free Download (Latest 2025)
am2612067
 
iii-ii cd nCompiler design UNIT-V-1.pptx
nandan543979
 
Code Generation Part-2 in Compiler Construction
ProfMonikaShah
 
Lecture 16 17 code-generation
Iffat Anjum
 
Compiler unit 5
BBDITM LUCKNOW
 

Recently uploaded (20)

PDF
Set Relation Function Practice session 24.05.2025.pdf
DrStephenStrange4
 
PDF
Electrical Engineer operation Supervisor
ssaruntatapower143
 
PPTX
原版一样(Acadia毕业证书)加拿大阿卡迪亚大学毕业证办理方法
Taqyea
 
DOC
MRRS Strength and Durability of Concrete
CivilMythili
 
PPTX
Damage of stability of a ship and how its change .pptx
ehamadulhaque
 
PDF
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
PPTX
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
PPTX
Hashing Introduction , hash functions and techniques
sailajam21
 
PPTX
Depth First Search Algorithm in 🧠 DFS in Artificial Intelligence (AI)
rafeeqshaik212002
 
PPTX
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
PPTX
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
PPTX
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
PDF
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
PPTX
Green Building & Energy Conservation ppt
Sagar Sarangi
 
PPTX
What is Shot Peening | Shot Peening is a Surface Treatment Process
Vibra Finish
 
PPTX
Server Side Web Development Unit 1 of Nodejs.pptx
sneha852132
 
PDF
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
PDF
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
PPTX
Shinkawa Proposal to meet Vibration API670.pptx
AchmadBashori2
 
PDF
GTU Civil Engineering All Semester Syllabus.pdf
Vimal Bhojani
 
Set Relation Function Practice session 24.05.2025.pdf
DrStephenStrange4
 
Electrical Engineer operation Supervisor
ssaruntatapower143
 
原版一样(Acadia毕业证书)加拿大阿卡迪亚大学毕业证办理方法
Taqyea
 
MRRS Strength and Durability of Concrete
CivilMythili
 
Damage of stability of a ship and how its change .pptx
ehamadulhaque
 
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
Hashing Introduction , hash functions and techniques
sailajam21
 
Depth First Search Algorithm in 🧠 DFS in Artificial Intelligence (AI)
rafeeqshaik212002
 
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
Green Building & Energy Conservation ppt
Sagar Sarangi
 
What is Shot Peening | Shot Peening is a Surface Treatment Process
Vibra Finish
 
Server Side Web Development Unit 1 of Nodejs.pptx
sneha852132
 
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
Shinkawa Proposal to meet Vibration API670.pptx
AchmadBashori2
 
GTU Civil Engineering All Semester Syllabus.pdf
Vimal Bhojani
 
Ad

UNIT IV Compiler.pptx RUNTIMEENVIRONMENT

  • 1. UNIT IV RUN-TIME ENVIRONMENT AND CODE GENERATION Runtime Environments – source language issues – Storage organization – Storage Allocation Strategies: Static, Stack and Heap allocation - Parameter Passing - Symbol Tables - Dynamic Storage Allocation - Issues in the Design of a code generator – Basic Blocks and Flow graphs - Design of a simple Code Generator - Optimal Code Generation for Expressions – Dynamic Programming Code Generation
  • 2. RUN-TIME ENVIRONMENT - STORAGE ORGANIZATION  When the target program executes then it runs in its own logical address space  Logical address space is shared among the compiler, operating system and target machine for management and organization  OS is used to map the logical address into physical address
  • 4.  Runtime storage comes into blocks, where a byte is the smallest unit of addressable memory  Run-time storage can be subdivide to hold the different components of an executing program: o Generated executable code o Static data objects o Dynamic data-object- heap o Automatic data objects- stack
  • 5. Activation Record  Control stack is a run time stack which is used to keep track of the live procedure activations i.e. it is used to find out the procedures whose execution have not been completed.  When it is called (activation begins) then the procedure name will push on to the stack  when it returns (activation ends) then it will popped.  An activation record is pushed into the stack when a procedure is called and it is popped when the control returns to the caller function.
  • 6. The diagram below shows the contents of activation records
  • 7.  Return Value: used by calling procedure to return a value to calling procedure.  Actual Parameter: used by calling procedures to supply parameters to the called procedures.  Control Link: It points to activation record of the caller.  Access Link: used to refer to non-local data held in other activation records.  Saved Machine Status: Holds the information about status of machine before the procedure is called.  Local Data: Holds the data that is local to the execution of the procedure.  Temporaries: Stores the value that arises in the evaluation of an expression.
  • 8. Storage Allocation Strategies The different ways to allocate memory are:  Static storage allocation  Stack storage allocation  Heap storage allocation
  • 9. Static storage allocation  In this, names are bound to storage locations  If memory is created at compile time then the memory will be created in static area and only once  Supports the dynamic data structure that means memory is created only at compile time and deallocated after program completion Drawbacks:  Size and position of data objects should be known at compile time & restriction of the recursion procedure
  • 10. Stack Storage Allocation  In this, storage is organized as a stack  An activation record is pushed into the stack when activation begins and it is popped when the activation end  Activation record contains the locals so that they are bound to fresh storage in each activation record.  The value of locals is deleted when the activation ends  Works on last-in-first-out (LIFO)  This allocation supports the recursion process
  • 11. Heap Storage Allocation  Most flexible allocation scheme  Allocation and deallocation of memory can be done at any time and at any place depending upon the user's requirement  Used to allocate memory to the variables dynamically  Supports the recursion process
  • 12. Code Generator  Code generator is used to produce the target code for three- address statements.  It uses registers to store the operands of the three address statement. Example: Consider the three address statement x:= y + z. It can have the following sequence of codes:  MOV x, R0 ADD y, R0
  • 13. Register and Address Descriptors:  A register descriptor contains the track of what is currently in each register  The register descriptors show that all the registers are initially empty  An address descriptor is used to store the location where current value of the name can be found at run time
  • 14. Generating Code for Assignment Statements: The assignment statement d:= (a-b) + (a-c) + (a-c) can be translated into following sequence of three address code: t:= a-b u:= a-c v:= t +u d:= v+u
  • 16. Design Issues in Code Generator Various code generation phase are :  Input to the code generator  Target program  Memory management  Instruction selection  Register allocation  Evaluation order
  • 17. 1. Input to the code generator  The input to the code generator contains the intermediate representation of the source program and the information of the symbol table  The source program is produced by the front end  Intermediate representation has the several choices: a) Postfix notation b) Syntax tree c) Three address code  The code generation phase needs complete error-free intermediate code as an input requires
  • 18. 2. Target program:  The target program is the output of the code generator. The output can be: a) Assembly language: It allows subprogram to be separately compiled b) Relocatable machine language: It makes the process of code generation easier c) Absolute machine language: It can be placed in a fixed location in memory and can be executed immediately.
  • 19. 3. Memory management  Mapping name in the source program to address of data is done by the front end and code generator  A name in the three address statements refers to the symbol table entry for name.  Local variables are stack allocation in the activation record while global variables are in static area
  • 20. 4. Instruction selection:  Nature of instruction set of the target machine should be complete and uniform.  The quality of the generated code can be determined by its speed and size.
  • 21. Example The Three address code is: a:= b + c d:= a + e Inefficient assembly code is: MOV b, R0 R0→b ADD c, R0 R0 → c + R0 MOV R0, a a → R0 MOV a, R0 R0→ a ADD e, R0 R0 → e + R0 MOV R0, d d → R0
  • 22. 5. Register allocation  Register can be accessed faster than memory.  The instructions involving operands in register are shorter and faster than those involving in memory operand. The following sub problems arise when we use registers:  Register allocation: In this, Selects set of variables that will reside in register.  Register assignment: In this, Picks the register that contains variable.  Certain machine requires even-odd pairs of registers for some operands and result.
  • 23. For example:  Consider the following division instruction of the form: D x, y Where,  x is the dividend even register in even/odd register pair  y is the divisor  Even register is used to hold the reminder.  Old register is used to hold the quotient.
  • 24. 6. Evaluation order  The code generator decides the order in which the instruction will be executed.  The order of computations affects the efficiency of the target code  Some computation orders need fewer registers to hold results of intermediate than others.
  • 25. Approaches to code generation issues: Code generator must always generate the correct code. Some of the design goals of code generator are:  Correct  Easily maintainable  Testable  Efficient
  • 26. BASIC BLOCKS  Source codes which are always executed in sequence are considered as the basic blocks of the code.  These basic blocks do not have any jump statements among them  A program can have various constructs as basic blocks, like IF-THEN-ELSE,  SWITCH-CASE conditional statements and  loops such as DO-WHILE, FOR, and REPEAT- UNTIL, etc.
  • 27. Basic block identification  Search header statements of all the basic blocks from where a basic block starts:  First statement of a program.  Statements that are target of any branch (conditional/unconditional).  Statements that follow any branch statement.  Header statements and the statements following them form a basic block.  A basic block does not include any header statement of any other basic block.
  • 29. DAG representation of Basic Blocks  Directed a cyclic graphs (dags) are useful data structures for implementing transformation on basic blocks.  The Dag for the statement x := y + z is: The Dag for the statements. t1:= 4 * i t2:= a[t ] is
  • 30. BASIC BLOCKS AND FLOW GRAPHS  A graph representation of 3 address statements is called a flow graph.  Nodes in the flow graph represent computations and the edges represents the flow of control.
  • 31. 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 halt or possibility of branching except at the end.  The following sequence of three address, statements forms a basic block: t:= a * a t:= a * b t:= 2 * t t:= t+ t
  • 32. begin sum := 0 i := 1 do begin sum := sum + a[i] + b[i]; i = i + 1; end While i < - 10 end Algorithm - Partition into basic blocks Input: A sequence of three address statements. Output: A list of basic blocks with each three address statement in exactly one block. Source code to compute the sum of 2 arrays
  • 33. List of 3 address statements: 1) sum := 0 2) i = 1 3) t1 := 4 * i 4) t2 := a[t1] 5) t3 := 4 * i 6) t4 := b[t3] 7) t5 := t2 + t4 8) t6 := sum + t5 9) sum := t6 10) t7 := i + 1 11) i = t7 12) i < = 10 goto 3