0% found this document useful (0 votes)
7 views

Compiler Module 6

Uploaded by

Souvik Gon
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)
7 views

Compiler Module 6

Uploaded by

Souvik Gon
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/ 5

MODULE-6 RUNTIME ENVIROMENT

Run-Time Environments in Compiler Design

A run-time environment is essential for the execution of programs. It manages the program's execution by handling
memory allocation, data management, and transferring control between functions or procedures. The key
components of a run-time environment include memory organization, symbol tables, storage allocation strategies,
and mechanisms for parameter passing.

Source Language Issues

The design of the source language significantly impacts how the compiler generates code and manages the run-time
environment. Important aspects include:

1. Activation Trees

• Definition: An activation tree graphically represents the execution flow of function or procedure calls in a
program. Each node corresponds to an activation (or instance) of a function, and the edges represent the
calling relationships.

• Key Features:

o Root Node: Represents the initial function, usually main().

o Child Nodes: Represent functions called by the parent function.

o Leaves: Represent functions that do not call any other function.

• Example:
2. Control Stack

• Definition: The control stack (or runtime stack) is a stack data structure used to manage function calls during
program execution. Each function call pushes an activation record onto the stack, and it is popped off when
the function returns.

• Purpose:

o Maintains the execution order.

o Ensures the return address and local variables are restored when a function call completes.

• Example:

Control stack during execution:

1. main() pushed first.

2. When foo() is called, it is pushed onto the stack.

3. When bar() is called, it is pushed next.

4. Upon returning from bar(), it is popped, followed by foo() and then main().

3. Scope of Declaration

• Definition: The scope of a variable or function determines the region of the program where it is accessible.

• Types:

o Local Scope: Variable declared inside a function or block is accessible only within that function or
block.

o Global Scope: Variable declared outside any function is accessible throughout the program.

• Static vs Dynamic Scope:

o Static Scope: Determined at compile time. Most programming languages like C and Java use static
scoping.

o Dynamic Scope: Determined at runtime. Rarely used, as it can make programs difficult to debug.

• Example:
Inside foo(), the local variable x is used, while outside foo(), the global variable x is accessible.

4. Binding of Names

• Definition: Binding is the process of associating names (e.g., variables, functions) with their memory
locations or definitions.

• Types:

o Static Binding: The association happens during compilation. Faster but less flexible.

o Dynamic Binding: The association happens during runtime. Slower but more flexible.

• Example: In object-oriented programming, overriding methods often involve dynamic binding.

Storage Organization

The program's memory is divided into different regions to store various kinds of data during execution. The main
subdivisions include:

1. Code Segment: Stores the compiled program code.

2. Static Data Segment: Stores global and static variables, which persist throughout program execution.

3. Heap: Used for dynamic memory allocation.

4. Stack: Used for local variables and function call information.

Activation Records

• Definition: An activation record (or stack frame) stores all the information required to manage a single
function call. It is pushed onto the control stack when a function is called and popped off when the function
returns.

• Contents:

o Return Address: Where to resume execution after the function returns.

o Parameters: Arguments passed to the function.

o Local Variables: Variables declared inside the function.

o Saved Registers: Stores the values of registers that may be modified by the function.
• Example:
For void foo(int x) { int y; }, the activation record might include:

o Return Address

o Parameter x

o Local Variable y

Storage Allocation Strategies

1. Static Allocation:

o Allocates memory at compile time.

o Variables and sizes must be known before execution.

o Used for global and static variables.

2. Stack Allocation:

o Memory is allocated and deallocated in a last-in, first-out (LIFO) order.

o Suitable for local variables and function calls.

3. Heap Allocation:

o Memory is allocated dynamically at runtime.

o Suitable for data structures like linked lists, trees, etc.

Parameter Passing

When a function is called, arguments are passed using one of the following strategies:

1. Call by Value:

o Copies the value of the argument into the function.

o Changes inside the function do not affect the original variable.


2. Call by Reference:

o Passes the address of the variable.

o Changes inside the function affect the original variable.

3. Copy Restore:

o Copies the argument into the function, and after execution, the modified value is restored.

4. Call by Name:

o Substitutes the argument expression into the function body.

Symbol Tables

• Definition: A symbol table is a data structure that stores information about program identifiers such as
variables, functions, and objects.

• Purpose: Helps the compiler in type checking, memory allocation, and code generation.

• Example:
For the program:

• int x = 5;

• float y;

The symbol table may include:

Name Type Scope Memory Address

x int global 0x1000

y float global 0x1004

Dynamic Storage Allocation Techniques

1. First Fit: Finds the first block of memory large enough for the request.

2. Best Fit: Allocates the smallest block that fits the request.

3. Worst Fit: Allocates the largest block available to minimize fragmentation.

4. Buddy System: Allocates memory in blocks that are powers of 2 for efficient splitting and merging.

You might also like