0% found this document useful (0 votes)
9 views36 pages

Runtime Environments-2-37

The document provides an overview of compilation, detailing the processes involved in translating source code into target programs, including lexical analysis, syntax analysis, and code generation. It discusses runtime environments, storage organization, and allocation strategies, highlighting the differences between static and dynamic allocation. Additionally, it covers procedure calls, activation records, and communication between procedures, emphasizing the importance of calling conventions and parameter passing methods.

Uploaded by

G Ramyasri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views36 pages

Runtime Environments-2-37

The document provides an overview of compilation, detailing the processes involved in translating source code into target programs, including lexical analysis, syntax analysis, and code generation. It discusses runtime environments, storage organization, and allocation strategies, highlighting the differences between static and dynamic allocation. Additionally, it covers procedure calls, activation records, and communication between procedures, emphasizing the importance of calling conventions and parameter passing methods.

Uploaded by

G Ramyasri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

An Overview of Compilation

source program target program

lexical analyzer code generator


symbol table

syntax analyzer error handler code optimizer

intermediate code
semantic analyzer
generator

CS 335 Swarnendu Biswas


Abstraction Spectrum
• Translating source code requires dealing with all programming
language abstractions
• For example, names, procedures, objects, flow of control, and exceptions
• Physical computer operates in terms of several primitive operations
• Arithmetic, data movement, and control jumps
• It is not enough to just translate intermediate code to machine code
• For e.g., need to manage memory when a program is executing

CS 335 Swarnendu Biswas


Runtime Environment
• A runtime environment is a set of data structures maintained at run
time to implement high-level structures
• For example, stack, heap, static area, and virtual function tables
• Depends on the features of the source and the target language
• Compilers create and manage a runtime environment in which the
target programs are executed
• Runtime deals with the layout, allocation, and deallocation of storage
locations, linkages between procedures, and passing parameters
among other concerns

CS 335 Swarnendu Biswas


Issues Dealt with Runtime Environments
• How to pass parameters when a procedure is called?
• What happens to locals when procedures return from an activation?
• How to support recursive procedures?
• Can a procedure refer to nonlocal names? If yes, then how?
•…

CS 335 Swarnendu Biswas


Storage Organization
• Target program runs in its own logical space Code
• Size of generated code is usually fixed at compile time Static
• Unless code is loaded or produced dynamically
Heap
• Compiler can place the executable at fixed addresses
• Runtime storage can be subdivided into
• Target code Free Memory
• Static data objects such as global constants
• Stack to keep track of procedure activations and local data Stack
• Heap to keep all other information like dynamic data

CS 335 Swarnendu Biswas


Strategies for Storage Allocation
• Static allocation – Lay out storage at compile time only by studying
the program text
• Memory allocated at compile time will be in the static area
• Dynamic allocation – Storage allocation decisions are made when the
program is running
• Stack allocation – Manage run-time allocation with a stack storage
• Local data are allocated on the stack
• Heap allocation – Memory allocation and deallocation can be done at any
time
• Requires memory reclamation support

CS 335 Swarnendu Biswas


Static Allocation
• Names are bound to storage locations at compilation time
• Bindings do not change, so no run time support is required
• Names are bound to the same location on every invocation
• Values are retained across activations of a procedure
• Limitations
• Size of all data objects must be known at compile time
• Data structures cannot be created dynamically
• Recursive procedures are not allowed

CS 335 Swarnendu Biswas


Stack vs Heap Allocation
Stack Heap

• Allocation/deallocation is • Allocation/deallocation is explicit


automatic
• Less expensive • More expensive
• Space for allocation is limited • Challenge is fragmentation

CS 335 Swarnendu Biswas


Static vs Dynamic Allocation
Static Dynamic

• Variable access is fast • Variable access is slow


• Addresses are known at compile • Accesses need redirection through
time stack/heap pointer
• Cannot support recursion • Supports recursion

CS 335 Swarnendu Biswas


Procedure Abstraction

CS 335 Swarnendu Biswas


Procedure Calls
• Procedure definition is a declaration that associates an identifier with
a statement (procedure body)
• Formal parameters appear in declaration
• Actual parameters appear when a procedure is called

• Important abstraction in programming


• Defines critical interfaces among large parts of a software

CS 335 Swarnendu Biswas


Procedure Calls
• Creates a controlled execution environment
• Each procedure has its own private named storage or name space
• Executing a call instantiates the callee’s name space

• Abstractions provided by procedures


• Control abstraction
• Name space
• External interface

CS 335 Swarnendu Biswas


Control Abstraction
• Each language has rules to
• Invoke a procedure
• Map a set of arguments from the caller’s name space to the callee’s name
space
• Return control to the caller, and continue execution after the call
• Linkage convention standardizes the actions taken by the compiler
and the OS to make a procedure call

CS 335 Swarnendu Biswas


Procedure Calls
• Each execution of a procedure 𝑃 is an activation of the procedure 𝑃
• A procedure is recursive if an activation can begin before an earlier
activation of the same procedure has ended
• If procedure is recursive, several activations may be alive at the same time

• The lifetime of an activation of 𝑃 is all the steps to execute 𝑃


including all the steps in procedures that 𝑃 calls
• Given activations of two procedures, their lifetimes are either non-
overlapping or nested

CS 335 Swarnendu Biswas


Activation Tree
• Depicts the way control enters and leaves int g() { return 42; }
activations int f() { return g(); }

• Root represents the activation of main main() {


g();
• Each node represents activation of a
procedure f():

• Node 𝑎 is the parent of 𝑏 if control flows }


from 𝑎 to 𝑏
• Node 𝑎 is to the left of 𝑏 if lifetime of 𝑎 main
occurs before 𝑏
• Flow of control in a program corresponds g f
to depth-first traversal of activation tree
g

CS 335 Swarnendu Biswas


Quicksort Code
int a[11]; void quicksort(int m, int n) {
void readArray() { int i;
int i; if (n > m) {
… i = partition(m, n);
} quicksort(m, i-1);
quicksort(i+1, n);
int partition(int m, int n) { } }

} int main() {
readArray();
a[0] = -99999;
a[10] = 99999;
quicksort(1, 9);
}

CS 335 Swarnendu Biswas


Activation Tree
main() Also referred to as dynamic
call graph

rdArr() qsort(1,9)

partn (1,9) qsort(1,3) qsort(5,9)

partn (5,9) qsort(5,5) qsort(7,9)


partn (1,3) qsort(1,0) qsort(2,3)

partn (2,3) qsort(2,1) qsort(3,3) partn (7,9) qsort(7,7) qsort(9,9)

CS 335 Swarnendu Biswas


enter main()

Example of Procedure Activations enter readArray()


leave readArray()
enter quicksort(1,9)
enter partition(1,9)
main()
leave partition(1,9)
enter quicksort(1,3)
...
rdArr() qsort(1,9)
leave quicksort(1,3)
enter quicksort(5,9)
partn (1,9) qsort(1,3) qsort(5,9) ...
leave quicksort(5,9)
leave quicksort(1,9)
partn (5,9) qsort(5,5) qsort(7,9)
partn (1,3) qsort(1,0) qsort(2,3) leave main()

partn (2,3) qsort(2,1) qsort(3,3) partn (7,9)qsort(7,7)qsort(9,9)

CS 335 Swarnendu Biswas


Control Stack
• Procedure calls and returns are usually managed by
a run-time stack called the control stack qsort(2,3)
• Each live activation has an activation record on the
control stack qsort(1,3)
• Stores control information and data storage needed to
manage the activation qsort(1,9)
• Also called a frame
main()
• Frame is pushed when activation begins and popped
when activation ends
• Suppose node 𝑛 is at the top of the stack, then the
stack contains the nodes along the path from 𝑛 to
the root
CS 335 Swarnendu Biswas
Is a Stack Sufficient?
When will a control stack work?
• Once a function returns, its activation record cannot be referenced again
• We do not need to store old nodes in the activation tree
• Every activation record has either finished executing or is an ancestor of
the current activation record
When will a control stack not work?
• Once a function returns, its activation record cannot be referenced again
• Function closures – procedure and run-time context to define free
variables
CS 335 Swarnendu Biswas
Is a Stack Sufficient?
When will a control stack work?
• Once a function returns, its activation record cannot be referenced again
• We do not need to store old nodes in the activation tree
• Every activation record has either finished executing or is an ancestor of
the current activation record
When will a control stack not work?
• Once a function returns, its activation record cannot be referenced again
• Function closures – procedure and run-time context to define free
variables
CS 335 Swarnendu Biswas
Activation Record
• A pointer to the current activation record Actual parameters
is maintained in a register
Returned values
• Fields in an activation record
• Temporaries – evaluation of expressions Control link
• Local data – field for local data
Access link
• Saved machine status – information about the
machine state before the procedure call Saved machine
status
• Return address (value of program counter)
• Register contents Local data
• Access link – access nonlocal data
Temporaries

CS 335 Swarnendu Biswas


Activation Record
• Fields in an activation record Actual parameters
• Control link – Points to the activation record
of the caller Returned values

• Returned values – Space for the value to be Control link


returned
• Actual parameters – Space for actual Access link
parameters
Saved machine
status

• Contents and position of fields may vary Local data

with language and implementations Temporaries

CS 335 Swarnendu Biswas


Example of Activation Records

global
main() variable main()
integer 𝑎[11] integer 𝑎[11]

main main
rdArr()
rdArr

integer 𝑖 local
variable

Frame for main() rdArr() is activated

CS 335 Swarnendu Biswas


Example of Activation Records

main() integer 𝑎[11]

main
rdArr() qsort(1,9)
integer 𝑚, 𝑛

qsort(1,9)

integer 𝑖 local
variable

rdArr() is popped, qsort(1,9) is pushed

CS 335 Swarnendu Biswas


What is in G’s Activation Record when F()
calls G()?
• If a procedure F calls G, then G’s activation record contains
information about both F and G
• F is suspended until G completes, at which point F resumes
• G’s activation record contains information needed to resume execution of F
• G’s activation record contains
• G’s return value (needed by F)
• Actual parameters to G (supplied by F)
• Space for G’s local variables

CS 335 Swarnendu Biswas


A Standard Procedure Linkage
• Procedure linkage is a procedure p

contract between the prologue


compiler, the OS, and the procedure q

target machine prologue


• Divides responsibility for precall

naming, allocation of
resources, addressability, postreturn
epilogue
and protection

epilogue

CS 335 Swarnendu Biswas


Calling Sequence
• Calling sequence allocates an activation record on the stack and
enters information into its fields
• Responsibility is shared between the caller and the callee
• Return sequence is code to restore the state of the machine so the
calling procedure can continue its execution after the call

CS 335 Swarnendu Biswas


Calling Sequence
• Policies and implementation strategies can differ
• Place values communicated between caller and callee at the beginning of the
callee’s activation record, close to the caller's activation record
• Fixed-length items are placed in the middle
• Data items whose size are not known during intermediate code generation
are placed at the end of the activation record
• Top-of-stack points to the end of the fixed-length fields
• Fixed-length data items are accessed by fixed offsets from top-of-stack pointer
• Variable-length fields records are actually "above" the top-of-stack

CS 335 Swarnendu Biswas


Division of Tasks Between Caller and Callee

Caller’s activation record


Parameters and return value

Control link

Caller’s responsibility
Links and saved status

Temporaries and local data

Callee’s activation record


Parameters and return value

responsibility
Control link
Links and saved status

Caller’s
top_stack
Temporaries and local data

CS 335 Swarnendu Biswas


Division of Tasks Between Caller and Callee

Call sequence
• Caller evaluates the actual parameters
• Caller stores a return address and the old value of top_stack into
the callee's activation record
• Caller then increments top_stack past the caller's local data and
temporaries and the callee's parameters and status fields
• Callee saves the register values and other status information
• Callee initializes its local data and begins execution

CS 335 Swarnendu Biswas


Division of Tasks Between Caller and Callee

Return Sequence
• Callee places the return value next to the parameters
• Callee restores top_stack and other registers
• Callee branches to the return address that the caller placed in the
status field
• Caller copies return value into its activation record

CS 335 Swarnendu Biswas


Communication between Procedures
• Calling convention is an implementation-level detail to specify
how callees receive parameters from their caller and how callees
return a result
• Parameter binding maps the actual parameters at a call site to the
callee’s formal parameters
• Types of mapping conventions
• Pass by value
• Pass by reference
• Pass by name

CS 335 Swarnendu Biswas


Call by Value and Call by Reference
Call by Value Call by Reference

• Convention where the caller • Convention where the compiler


evaluates the actual parameters passes an address for the formal
and passes their r-values to the
callee parameter to the callee
• Any redefinition of a reference
• Formal parameter in the callee is formal parameter is reflected in
treated like a local name
the corresponding actual
• Any modification of a value parameter
parameter in the callee is not
visible in the caller • A formal parameter requires an
• Example: C and Pascal extra indirection
CS 335 Swarnendu Biswas
Call by Name
• Reference to a formal parameter procedure double(x);

behaves as if the actual parameter real x;


begin
had been textually substituted in
x := x*2
its place
end;
• Renaming is used in case of clashes
• Can update the given parameters
double(c[j]) c[j] := c[j]*2
• Actual parameters are evaluated
inside the called function
• Example: Algol-60
https://www2.cs.sfu.ca/~cameron/Teaching/383/PassByName.html

CS 335 Swarnendu Biswas


Challenges with Call by Name
procedure swap(a, b) • What will happen when you call
integer a, b, temp; swap(i, x[i])?
begin
temp := a
a := b
b := temp
end;

CS 335 Swarnendu Biswas

You might also like