Compiler Module 6
Compiler Module 6
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.
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:
• 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 Ensures the return address and local variables are restored when a function call completes.
• Example:
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.
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.
Storage Organization
The program's memory is divided into different regions to store various kinds of data during execution. The main
subdivisions include:
2. Static Data Segment: Stores global and static variables, which persist throughout program execution.
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 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
1. Static Allocation:
2. Stack Allocation:
3. Heap Allocation:
Parameter Passing
When a function is called, arguments are passed using one of the following strategies:
1. Call by Value:
3. Copy Restore:
o Copies the argument into the function, and after execution, the modified value is restored.
4. Call by Name:
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;
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.
4. Buddy System: Allocates memory in blocks that are powers of 2 for efficient splitting and merging.