SlideShare une entreprise Scribd logo
1  sur  35
1
Compiler ConstructionCompiler Construction
Runtime EnvironmentRuntime Environment
2
Run-Time Environments (Chapter 7)
3
Run-Time Environments (Chapter 7)
A lot has to happen at run time to get your program
running.
At run time, we need a system to map NAMES (in the
source program) to STORAGE on the machine.
Allocation and deallocation of memory is handled by a
RUN-TIME SUPPORT SYSTEM typically linked and
loaded along with the compiled target code.
One of the primary responsibilities of the run-time system is
to manage ACTIVATIONS of procedures.
4
Procedures
We assume in this lecture that a program is no more than a
collection of PROCEDURES.
A PROCEDURE DEFINTION associates an identifier with a
statement (a statement could actually be a block of
statements, of course).
The identifier is called the PROCEDURE NAME.
The statement is called the PROCEDURE BODY.
A PROCEDURE CALL is an invocation of a procedure
within an executable statement.
Procedures that return values are normally called
FUNCTIONS, but we’ll just use the name “procedure.”
5
Example program
program sort( input, output );
var a: array [ 0..10 ] of integer;
procedure readarray;
var i: integer;
begin
for i := 1 to 9 do read( a[i] )
end;
function partition( y, z: integer ) : integer;
var i, j, x, v: integer;
begin …
end;
procedure quicksort( m, n: integer );
var i: integer;
begin
if ( n > m ) then
begin
i :=
pa
rtiti
on
(
m,
n )
;
quick
sor
t(
m,i
-
1);
quick
sor
6
Parameters of procedures
The FORMAL PARAMETERS are special identifiers
declared in the procedure definition.
The formal parameters must correspond to the ACTUAL
PARAMETERS in the function call.
E.g. m and n are formal parameters of the quicksort
procedure. The actual parameters in the call to quicksort
in the main program are 1 and 9.
Actual parameters can be a simple identifier, or more
complex expressions.
7
Control flow
Let’s assume, as in most mainstream programming
languages, that we have SEQUENTIAL program flow.
Procedure execution begins at the first statement of the
procedure body.
When a procedure returns, execution returns to the
instruction immediately following the procedure call.
8
Activations
Every execution of a procedure is called an ACTIVATION.
The LIFETIME of an activation of procedure P is the
sequence of steps between the first and last steps of P’s
body, including any procedures called while P is running.
Normally, when control flows from one activation to another,
it must (eventually) return to the same activation.
When activations are thusly nested, we can represent
control flow with ACTIVATION TREES.
9
Activation trees
Execution begins…
enter readarray
leave readarray
enter quicksort(1,9)
enter partition(1,9)
leave partition(1,9)
enter quicksort(1,3)
…
leave quicksort(1,3)
enter quicksort(5,9)
…
leave quicksort(5,9)
leave quicksort(1,9)
Execution terminated.
10
Control stacks
We can use a stack to keep track of currently-active
activations.
We push a record onto the stack when a procedure is
called, and pop that record off the stack when the
procedure returns.
At any point in time, the control stack represents a path
from the root of the activation tree to one of the nodes.
11
Example control stack
This partial activation tree
corresponds to control stack
(growing downward)
s
q(1,9)
q(1,3)
q(2,3)
12
Declarations
Every DECLARATION associates some information with a
name.
In Pascal and C, declarations are EXPLICIT:
var i : integer;
assocates the TYPE integer with the NAME i.
Some languages like Perl and Python have IMPLICIT
declarations.
13
Scope of a declaration
The SCOPING RULES of a language determine where in a
program a declaration applies.
The SCOPE of a declaration is the portion of the program
where the declaration applies.
An occurrence of a name in a procedure P is LOCAL to P if
it is in the scope of a declaration made in P.
If the relevant declaration is not in P, we say the reference
is NON-LOCAL.
During compilation, we use the symbol table to find the
right declaration for a given occurrence of a name.
The symbol table should return the entry if the name is in
scope, or otherwise return nothing.
14
Environments and states
The ENVIRONMENT is a function mapping from names to
storage locations.
The STATE is a function mapping storage locations to the
values held in those locations.
Environments map names to l-values.
States map l-values to r-values.
15
Name binding
When an environment maps name x to storage location s,
we say “x is BOUND to s”. The association is a BINDING.
Assignments change the state, but NOT the environment:
pi := 3.14
changes the value held in the storage location for pi, but
does NOT change the location (the binding) of pi.
Bindings do change, however, during execution, as we move
from activation to activation.
16
Run-time system design
The run-time system keeps track of a program’s dynamic components.
There are many relevant criteria for its design:
1. Can procedures be recursive?
2. What happens to values of local names when control returns from
the activations of a procedure?
3. Can a procedure refer to nonlocal names?
4. How are parameters passed when procedures are called?
5. Can procedures be passed as parameters?
6. Can procedures be returned from procedures?
7. Can programs dynamically allocate their own storage?
8. Does storage get deallocated explicitly or implicitly?
Static notion Dynamic counterpart
definition of a procedure activations of the procedure
declarations of a name bindings of the name
scope of a declaration lifetime of a binding
17
Storage allocation
18
Organization of storage
Fixed-size objects can be
placed in predefined
locations.
The heap and the stack
need room to grow,
however.
19
Run-time stack and heap
The STACK is used to store:
− Procedure activations.
− The status of the machine just before calling a procedure, so
that the status can be restored when the called procedure
returns.
The HEAP stores data allocated under program control
(e.g. by malloc() in C).
20
Activation records
Any information needed for a single
activation of a procedure is stored
in the ACTIVATION RECORD
(sometimes called the STACK
FRAME).
Today, we’ll assume the stack grows
DOWNWARD, as on, e.g., the
Intel architecture.
The activation record gets pushed for
each procedure call and popped
for each procedure return. (the access link is the “dynamic
link” in Sebesta’s terminology)
21
Compile-time layout of locals
Usually the BYTE is the smallest addressable unit of storage.
We lay out locals in the order they are declared.
Each local has an OFFSET from the beginning of the activation record
(or local data area of the record).
Some data objects require alignment with machine words.
Any resulting wasted space is called PADDING.
Type Size (typical) Alignment (typical)
char 8 8
short 16 16
int 32 32
float 32 32
double 64 32
22
Storage allocation strategies
23
Static allocation
Statically allocated names are bound to storage at compile
time.
Storage bindings of statically allocated names never
change, so even if a name is local to a procedure, its
name is always bound to the same storage.
The compiler uses the type of a name (retrieved from the
symbol table) to determine storage size required.
The required number of bytes (possibly aligned) is set
aside for the name.
The address of the storage is fixed at compile time.
24
Static allocation
Limitations:
− The size required must be known at compile time.
− Recursive procedures cannot be implemented as all locals are
statically allocated.
− No data structure can be created dynamicaly as all data is static.
25
Stack-dynamic allocation
Storage is organized as a stack.
Activation records are pushed and popped.
Locals and parameters are contained in the activation
records for the call.
This means locals are bound to fresh storage on every call.
If we have a stack growing downwards, we just need a
stack_top pointer.
To allocate a new activation record, we just increase
stack_top.
To deallocate an existing activation record, we just
decrease stack_top.
26
Position in
Activation Tree
Activation Records
on the Stack
27
Address generation in stack allocation
The position of the activation record on the stack cannot be
determined statically.
Therefore the compiler must generate addresses
RELATIVE to the activation record.
If we have a downward-growing stack and a stack_top
pointer, we generate addresses of the form stack_top +
offset
28
Calling sequences
The CALLING SEQUENCE for a procedure allocates an
activation record and fills its fields in with appropriate
values.
The RETURN SEQUENCE restores the machine state to
allow execution of the calling procedure to continue.
Some of the calling sequence code is part of the calling
procedure, and some is part of the called procedure.
What goes where depends on the language and machine
architecture.
29
30
Sample calling sequence
1. Caller evaluates the actual parameters and places them
into the activation record of the callee.
2. Caller stores a return address and old value for
stack_top in the callee’s activation record.
3. Caller increments stack_top to the beginning of the
temporaries and locals for the callee.
4. Caller branches to the code for the callee.
5. Callee saves all needed register values and status.
6. Callee initializes its locals and begins execution.
31
Sample return sequence
1. Callee places the return value at the correct location in
the activation record (next to caller’s activation record)
2. Callee uses status information previously saved to
restore stack_top and the other registers.
3. Callee branches to the return address previously
requested by the caller.
4. [Optional] Caller copies the return value into its own
activation record and uses it to evaluate an expression.
32
Variable-length data
In some languages, array size can depend on a value
passed to the procedure as a parameter.
This and any other variable-sized data can still be allocated
on the stack, but BELOW the callee’s activation record.
In the activation record itself, we simply store POINTERS to
the to-be-allocated data.
33
Example of variable- length data
All variable-length
data is pointed to
from the local
data area.
34
Dangling pointers
Stack dynamic allocation means that pointers might end up
DANGLING. Every novice C programmer makes this
mistake at least once:
int main( void ) { int *dangle( void ) {
int *p; int i = 23;
p = dangle(); return &i;
} }
35
Heap allocation
Some languages do not have tree-structured allocations.
In these cases, activations have to be allocated on the
heap.
This allows strange situations, like callee activations that
live longer than their callers’ activations.
This is not common.

Contenu connexe

Tendances (18)

Stored procedure
Stored procedureStored procedure
Stored procedure
 
09 implementing+subprograms
09 implementing+subprograms09 implementing+subprograms
09 implementing+subprograms
 
Data structures &algorithms
Data structures &algorithmsData structures &algorithms
Data structures &algorithms
 
Lesson 24
Lesson 24Lesson 24
Lesson 24
 
R hive tutorial - apply functions and map reduce
R hive tutorial - apply functions and map reduceR hive tutorial - apply functions and map reduce
R hive tutorial - apply functions and map reduce
 
Set, merge, and update
Set, merge, and updateSet, merge, and update
Set, merge, and update
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
Xpp b tspitr
Xpp b tspitrXpp b tspitr
Xpp b tspitr
 
Macro
MacroMacro
Macro
 
SAS Macros part 1
SAS Macros part 1SAS Macros part 1
SAS Macros part 1
 
381 Rac
381 Rac381 Rac
381 Rac
 
Oracle applications 11i dba faq
Oracle applications 11i dba faqOracle applications 11i dba faq
Oracle applications 11i dba faq
 
10 Creating Triggers
10 Creating Triggers10 Creating Triggers
10 Creating Triggers
 
oracle plsql training | oracle online training | oracle plsql demo | oracle p...
oracle plsql training | oracle online training | oracle plsql demo | oracle p...oracle plsql training | oracle online training | oracle plsql demo | oracle p...
oracle plsql training | oracle online training | oracle plsql demo | oracle p...
 
Stored procedure
Stored procedureStored procedure
Stored procedure
 
Hoisting Nested Functions
Hoisting Nested Functions Hoisting Nested Functions
Hoisting Nested Functions
 
Introduction to SAS Data Set Options
Introduction to SAS Data Set OptionsIntroduction to SAS Data Set Options
Introduction to SAS Data Set Options
 
Data Match Merging in SAS
Data Match Merging in SASData Match Merging in SAS
Data Match Merging in SAS
 

En vedette

46thingsthatbugpeopleplus1bonusslideshow 130322165436-phpapp01
46thingsthatbugpeopleplus1bonusslideshow 130322165436-phpapp0146thingsthatbugpeopleplus1bonusslideshow 130322165436-phpapp01
46thingsthatbugpeopleplus1bonusslideshow 130322165436-phpapp01Ganesh Amirineni
 
Learn BEM: CSS Naming Convention
Learn BEM: CSS Naming ConventionLearn BEM: CSS Naming Convention
Learn BEM: CSS Naming ConventionIn a Rocket
 
SEO: Getting Personal
SEO: Getting PersonalSEO: Getting Personal
SEO: Getting PersonalKirsty Hulse
 
How to Build a Dynamic Social Media Plan
How to Build a Dynamic Social Media PlanHow to Build a Dynamic Social Media Plan
How to Build a Dynamic Social Media PlanPost Planner
 

En vedette (6)

Ganesh groups
Ganesh groupsGanesh groups
Ganesh groups
 
46thingsthatbugpeopleplus1bonusslideshow 130322165436-phpapp01
46thingsthatbugpeopleplus1bonusslideshow 130322165436-phpapp0146thingsthatbugpeopleplus1bonusslideshow 130322165436-phpapp01
46thingsthatbugpeopleplus1bonusslideshow 130322165436-phpapp01
 
Мы принимаем культуру, чтобы научиться говорить
Мы принимаем культуру, чтобы научиться говоритьМы принимаем культуру, чтобы научиться говорить
Мы принимаем культуру, чтобы научиться говорить
 
Learn BEM: CSS Naming Convention
Learn BEM: CSS Naming ConventionLearn BEM: CSS Naming Convention
Learn BEM: CSS Naming Convention
 
SEO: Getting Personal
SEO: Getting PersonalSEO: Getting Personal
SEO: Getting Personal
 
How to Build a Dynamic Social Media Plan
How to Build a Dynamic Social Media PlanHow to Build a Dynamic Social Media Plan
How to Build a Dynamic Social Media Plan
 

Similaire à Compiler 2011-8-re1

Intermediate code optimization Unit-4.pdf
Intermediate code optimization Unit-4.pdfIntermediate code optimization Unit-4.pdf
Intermediate code optimization Unit-4.pdfHimanshu883663
 
chapter-7-runtime-environments.ppt
chapter-7-runtime-environments.pptchapter-7-runtime-environments.ppt
chapter-7-runtime-environments.pptssuser0db64b
 
Chapter Seven(1)
Chapter Seven(1)Chapter Seven(1)
Chapter Seven(1)bolovv
 
Relational Database Management System
Relational Database Management SystemRelational Database Management System
Relational Database Management Systemsweetysweety8
 
Runtimeenvironment
RuntimeenvironmentRuntimeenvironment
RuntimeenvironmentAnusuya123
 
04. WORKING WITH FUNCTIONS-2 (1).pptx
04. WORKING WITH FUNCTIONS-2 (1).pptx04. WORKING WITH FUNCTIONS-2 (1).pptx
04. WORKING WITH FUNCTIONS-2 (1).pptxManas40552
 
Chap6 procedures & macros
Chap6 procedures & macrosChap6 procedures & macros
Chap6 procedures & macrosHarshitParkar6677
 
Oracle DBA interview_questions
Oracle DBA interview_questionsOracle DBA interview_questions
Oracle DBA interview_questionsNaveen P
 
Assembler directives and basic steps ALP of 8086
Assembler directives and basic steps ALP of 8086Assembler directives and basic steps ALP of 8086
Assembler directives and basic steps ALP of 8086Urvashi Singh
 
9. DBMS Experiment Laboratory PresentationPPT
9. DBMS Experiment Laboratory PresentationPPT9. DBMS Experiment Laboratory PresentationPPT
9. DBMS Experiment Laboratory PresentationPPTTheVerse1
 
Unit 3 principles of programming language
Unit 3 principles of programming languageUnit 3 principles of programming language
Unit 3 principles of programming languageVasavi College of Engg
 
10 implementing subprograms
10 implementing subprograms10 implementing subprograms
10 implementing subprogramsMunawar Ahmed
 

Similaire à Compiler 2011-8-re1 (20)

Intermediate code optimization Unit-4.pdf
Intermediate code optimization Unit-4.pdfIntermediate code optimization Unit-4.pdf
Intermediate code optimization Unit-4.pdf
 
chapter-7-runtime-environments.ppt
chapter-7-runtime-environments.pptchapter-7-runtime-environments.ppt
chapter-7-runtime-environments.ppt
 
Run time storage
Run time storageRun time storage
Run time storage
 
Chapter Seven(1)
Chapter Seven(1)Chapter Seven(1)
Chapter Seven(1)
 
Co&al lecture-07
Co&al lecture-07Co&al lecture-07
Co&al lecture-07
 
Chapter 6 notes
Chapter 6 notesChapter 6 notes
Chapter 6 notes
 
Chapter 5 notes
Chapter 5 notesChapter 5 notes
Chapter 5 notes
 
Chapter 5 notes new
Chapter 5 notes newChapter 5 notes new
Chapter 5 notes new
 
Module04
Module04Module04
Module04
 
Relational Database Management System
Relational Database Management SystemRelational Database Management System
Relational Database Management System
 
Runtimeenvironment
RuntimeenvironmentRuntimeenvironment
Runtimeenvironment
 
04. WORKING WITH FUNCTIONS-2 (1).pptx
04. WORKING WITH FUNCTIONS-2 (1).pptx04. WORKING WITH FUNCTIONS-2 (1).pptx
04. WORKING WITH FUNCTIONS-2 (1).pptx
 
Chap6 procedures & macros
Chap6 procedures & macrosChap6 procedures & macros
Chap6 procedures & macros
 
Oracle DBA interview_questions
Oracle DBA interview_questionsOracle DBA interview_questions
Oracle DBA interview_questions
 
Assembler directives and basic steps ALP of 8086
Assembler directives and basic steps ALP of 8086Assembler directives and basic steps ALP of 8086
Assembler directives and basic steps ALP of 8086
 
9. DBMS Experiment Laboratory PresentationPPT
9. DBMS Experiment Laboratory PresentationPPT9. DBMS Experiment Laboratory PresentationPPT
9. DBMS Experiment Laboratory PresentationPPT
 
FUNCTIONS.pptx
FUNCTIONS.pptxFUNCTIONS.pptx
FUNCTIONS.pptx
 
Unit 3 principles of programming language
Unit 3 principles of programming languageUnit 3 principles of programming language
Unit 3 principles of programming language
 
10 implementing subprograms
10 implementing subprograms10 implementing subprograms
10 implementing subprograms
 
The Stack Frame
The Stack FrameThe Stack Frame
The Stack Frame
 

Dernier

PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 

Dernier (20)

PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 

Compiler 2011-8-re1

  • 3. 3 Run-Time Environments (Chapter 7) A lot has to happen at run time to get your program running. At run time, we need a system to map NAMES (in the source program) to STORAGE on the machine. Allocation and deallocation of memory is handled by a RUN-TIME SUPPORT SYSTEM typically linked and loaded along with the compiled target code. One of the primary responsibilities of the run-time system is to manage ACTIVATIONS of procedures.
  • 4. 4 Procedures We assume in this lecture that a program is no more than a collection of PROCEDURES. A PROCEDURE DEFINTION associates an identifier with a statement (a statement could actually be a block of statements, of course). The identifier is called the PROCEDURE NAME. The statement is called the PROCEDURE BODY. A PROCEDURE CALL is an invocation of a procedure within an executable statement. Procedures that return values are normally called FUNCTIONS, but we’ll just use the name “procedure.”
  • 5. 5 Example program program sort( input, output ); var a: array [ 0..10 ] of integer; procedure readarray; var i: integer; begin for i := 1 to 9 do read( a[i] ) end; function partition( y, z: integer ) : integer; var i, j, x, v: integer; begin … end; procedure quicksort( m, n: integer ); var i: integer; begin if ( n > m ) then begin i := pa rtiti on ( m, n ) ; quick sor t( m,i - 1); quick sor
  • 6. 6 Parameters of procedures The FORMAL PARAMETERS are special identifiers declared in the procedure definition. The formal parameters must correspond to the ACTUAL PARAMETERS in the function call. E.g. m and n are formal parameters of the quicksort procedure. The actual parameters in the call to quicksort in the main program are 1 and 9. Actual parameters can be a simple identifier, or more complex expressions.
  • 7. 7 Control flow Let’s assume, as in most mainstream programming languages, that we have SEQUENTIAL program flow. Procedure execution begins at the first statement of the procedure body. When a procedure returns, execution returns to the instruction immediately following the procedure call.
  • 8. 8 Activations Every execution of a procedure is called an ACTIVATION. The LIFETIME of an activation of procedure P is the sequence of steps between the first and last steps of P’s body, including any procedures called while P is running. Normally, when control flows from one activation to another, it must (eventually) return to the same activation. When activations are thusly nested, we can represent control flow with ACTIVATION TREES.
  • 9. 9 Activation trees Execution begins… enter readarray leave readarray enter quicksort(1,9) enter partition(1,9) leave partition(1,9) enter quicksort(1,3) … leave quicksort(1,3) enter quicksort(5,9) … leave quicksort(5,9) leave quicksort(1,9) Execution terminated.
  • 10. 10 Control stacks We can use a stack to keep track of currently-active activations. We push a record onto the stack when a procedure is called, and pop that record off the stack when the procedure returns. At any point in time, the control stack represents a path from the root of the activation tree to one of the nodes.
  • 11. 11 Example control stack This partial activation tree corresponds to control stack (growing downward) s q(1,9) q(1,3) q(2,3)
  • 12. 12 Declarations Every DECLARATION associates some information with a name. In Pascal and C, declarations are EXPLICIT: var i : integer; assocates the TYPE integer with the NAME i. Some languages like Perl and Python have IMPLICIT declarations.
  • 13. 13 Scope of a declaration The SCOPING RULES of a language determine where in a program a declaration applies. The SCOPE of a declaration is the portion of the program where the declaration applies. An occurrence of a name in a procedure P is LOCAL to P if it is in the scope of a declaration made in P. If the relevant declaration is not in P, we say the reference is NON-LOCAL. During compilation, we use the symbol table to find the right declaration for a given occurrence of a name. The symbol table should return the entry if the name is in scope, or otherwise return nothing.
  • 14. 14 Environments and states The ENVIRONMENT is a function mapping from names to storage locations. The STATE is a function mapping storage locations to the values held in those locations. Environments map names to l-values. States map l-values to r-values.
  • 15. 15 Name binding When an environment maps name x to storage location s, we say “x is BOUND to s”. The association is a BINDING. Assignments change the state, but NOT the environment: pi := 3.14 changes the value held in the storage location for pi, but does NOT change the location (the binding) of pi. Bindings do change, however, during execution, as we move from activation to activation.
  • 16. 16 Run-time system design The run-time system keeps track of a program’s dynamic components. There are many relevant criteria for its design: 1. Can procedures be recursive? 2. What happens to values of local names when control returns from the activations of a procedure? 3. Can a procedure refer to nonlocal names? 4. How are parameters passed when procedures are called? 5. Can procedures be passed as parameters? 6. Can procedures be returned from procedures? 7. Can programs dynamically allocate their own storage? 8. Does storage get deallocated explicitly or implicitly? Static notion Dynamic counterpart definition of a procedure activations of the procedure declarations of a name bindings of the name scope of a declaration lifetime of a binding
  • 18. 18 Organization of storage Fixed-size objects can be placed in predefined locations. The heap and the stack need room to grow, however.
  • 19. 19 Run-time stack and heap The STACK is used to store: − Procedure activations. − The status of the machine just before calling a procedure, so that the status can be restored when the called procedure returns. The HEAP stores data allocated under program control (e.g. by malloc() in C).
  • 20. 20 Activation records Any information needed for a single activation of a procedure is stored in the ACTIVATION RECORD (sometimes called the STACK FRAME). Today, we’ll assume the stack grows DOWNWARD, as on, e.g., the Intel architecture. The activation record gets pushed for each procedure call and popped for each procedure return. (the access link is the “dynamic link” in Sebesta’s terminology)
  • 21. 21 Compile-time layout of locals Usually the BYTE is the smallest addressable unit of storage. We lay out locals in the order they are declared. Each local has an OFFSET from the beginning of the activation record (or local data area of the record). Some data objects require alignment with machine words. Any resulting wasted space is called PADDING. Type Size (typical) Alignment (typical) char 8 8 short 16 16 int 32 32 float 32 32 double 64 32
  • 23. 23 Static allocation Statically allocated names are bound to storage at compile time. Storage bindings of statically allocated names never change, so even if a name is local to a procedure, its name is always bound to the same storage. The compiler uses the type of a name (retrieved from the symbol table) to determine storage size required. The required number of bytes (possibly aligned) is set aside for the name. The address of the storage is fixed at compile time.
  • 24. 24 Static allocation Limitations: − The size required must be known at compile time. − Recursive procedures cannot be implemented as all locals are statically allocated. − No data structure can be created dynamicaly as all data is static.
  • 25. 25 Stack-dynamic allocation Storage is organized as a stack. Activation records are pushed and popped. Locals and parameters are contained in the activation records for the call. This means locals are bound to fresh storage on every call. If we have a stack growing downwards, we just need a stack_top pointer. To allocate a new activation record, we just increase stack_top. To deallocate an existing activation record, we just decrease stack_top.
  • 27. 27 Address generation in stack allocation The position of the activation record on the stack cannot be determined statically. Therefore the compiler must generate addresses RELATIVE to the activation record. If we have a downward-growing stack and a stack_top pointer, we generate addresses of the form stack_top + offset
  • 28. 28 Calling sequences The CALLING SEQUENCE for a procedure allocates an activation record and fills its fields in with appropriate values. The RETURN SEQUENCE restores the machine state to allow execution of the calling procedure to continue. Some of the calling sequence code is part of the calling procedure, and some is part of the called procedure. What goes where depends on the language and machine architecture.
  • 29. 29
  • 30. 30 Sample calling sequence 1. Caller evaluates the actual parameters and places them into the activation record of the callee. 2. Caller stores a return address and old value for stack_top in the callee’s activation record. 3. Caller increments stack_top to the beginning of the temporaries and locals for the callee. 4. Caller branches to the code for the callee. 5. Callee saves all needed register values and status. 6. Callee initializes its locals and begins execution.
  • 31. 31 Sample return sequence 1. Callee places the return value at the correct location in the activation record (next to caller’s activation record) 2. Callee uses status information previously saved to restore stack_top and the other registers. 3. Callee branches to the return address previously requested by the caller. 4. [Optional] Caller copies the return value into its own activation record and uses it to evaluate an expression.
  • 32. 32 Variable-length data In some languages, array size can depend on a value passed to the procedure as a parameter. This and any other variable-sized data can still be allocated on the stack, but BELOW the callee’s activation record. In the activation record itself, we simply store POINTERS to the to-be-allocated data.
  • 33. 33 Example of variable- length data All variable-length data is pointed to from the local data area.
  • 34. 34 Dangling pointers Stack dynamic allocation means that pointers might end up DANGLING. Every novice C programmer makes this mistake at least once: int main( void ) { int *dangle( void ) { int *p; int i = 23; p = dangle(); return &i; } }
  • 35. 35 Heap allocation Some languages do not have tree-structured allocations. In these cases, activations have to be allocated on the heap. This allows strange situations, like callee activations that live longer than their callers’ activations. This is not common.