Open In App

Runtime Environments in Compiler Design

A translation needs to relate the static source text of a program to the dynamic actions that must occur at runtime to implement the program. The program consists of names for procedures, identifiers, etc., that require mapping with the actual memory location at runtime. Runtime environment is a state of the target machine, which may include software libraries, environment variables, etc., to provide services to the processes running in the system.

SOURCE LANGUAGE ISSUES:

Activation Tree



A program consist of procedures, a procedure definition is a declaration that, in its simplest form, associates an identifier (procedure name) with a statement (body of the procedure).  Each execution of the procedure is referred to as an activation of the procedure. Lifetime of an activation is the sequence of steps present in the execution of the procedure. If ‘a’ and ‘b’ be two procedures then their activations will be non-overlapping (when one is called after other) or nested (nested procedures). A procedure is recursive if a new activation begins before an earlier activation of the same procedure has ended. An activation tree shows the way control enters and leaves activations. Properties of activation trees are :-

Example – Consider the following program of Quicksort



main() {

      Int n;
      readarray();
      quicksort(1,n);
}

quicksort(int m, int n) {

     Int i= partition(m,n);
     quicksort(m,i-1);
     quicksort(i+1,n);
}

The activation tree for this program will be:  

First main function as the root then main calls readarray and quicksort. Quicksort in turn calls partition and quicksort again. The flow of control in a program corresponds to a pre-order depth-first traversal of the activation tree which starts at the root.

CONTROL STACK AND ACTIVATION RECORDS

Control stack or runtime stack is used to keep track of the live procedure activations i.e the procedures whose execution have not been completed. A procedure name is pushed on to the stack when it is called (activation begins) and it is popped when it returns (activation ends). Information needed by a single execution of a procedure is managed using an activation record or frame. When a procedure is called, an activation record is pushed into the stack and as soon as the control returns to the caller function the activation record is popped. 

 A general activation record consists of the following things:

Control stack for the above quicksort example:

SUBDIVISION OF RUNTIME MEMORY

Runtime storage can be subdivided to hold :

STORAGE ALLOCATION TECHNIQUES

I. Static Storage Allocation

The names are bound with the storage at compiler time only and hence every time procedure is invoked its names are bound to the same storage location only So values of local names can be retained across activations of a procedure. Here compiler can decide where the activation records go with respect to the target code and can also fill the addresses in the target code for the data it operates on.

Eg- FORTRAN was designed to permit static storage allocation. 

II. Stack Storage Allocation

III. Heap Storage Allocation

PARAMETER PASSING: The communication medium among procedures is known as parameter passing. The values of the variables from a calling procedure are transferred to the called procedure by some mechanism.

Basic terminology :

Different ways of passing the parameters to the procedure:

 

Advantages:

Portability: A runtime environment can provide a layer of abstraction between the compiled code and the operating system, making it easier to port the program to different platforms.

Resource management: A runtime environment can manage system resources, such as memory and CPU time, making it easier to avoid memory leaks and other resource-related issues.

Dynamic memory allocation: A runtime environment can provide dynamic memory allocation, allowing memory to be allocated and freed as needed during program execution.

Garbage collection: A runtime environment can perform garbage collection, automatically freeing memory that is no longer being used by the program.

Exception handling: A runtime environment can provide exception handling, allowing the program to gracefully handle errors and prevent crashes.

Disadvantages:

Performance overhead: A runtime environment can add performance overhead, as it requires additional processing and memory usage.

Platform dependency: Some runtime environments may be specific to certain platforms, making it difficult to port programs to other platforms.

Debugging: Debugging can be more difficult in a runtime environment, as the additional layer of abstraction can make it harder to trace program execution.

Compatibility issues: Some runtime environments may not be compatible with certain operating systems or hardware architectures, which can limit their usefulness.

Versioning: Different versions of a runtime environment may have different features or APIs, which can lead to versioning issues when running programs compiled with different versions of the same runtime environment.


Article Tags :