Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests - Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests -

Runtime Environment in Compiler Design

A program is the collection of text like code, statements, instructions, etc. To give life to any program, certain actions need to be performed on the target machine, and the program requires memory resources to execute the instructions. The program in execution implies the runtime of the program. The runtime environment in a program is the state of the target machine that includes environment variables, software libraries, etc., to handle the processes running in the system.

The runtime support system is a package that is mainly generated with the executable program and maintains the communication between the process and the runtime environment. It also manages the memory allocation and deallocation when the program is in execution.

As we are talking about the runtime environment, it is important to understand the program flow control, activation tree, memory allocation, deallocation, and passing the parameter.

Table of Content:

Activation Tree

When a sequence of instructions combines into several procedures, it is called a program. A procedure mainly consists of a start, end delimiter, and everything written inside is considered the body of the procedure. All the instructions in a procedure execute sequentially.

When a procedure executes, it is called its activation, which consists of all the required information to call a procedure. The units that may be contained by the activation record are as follows:

Temporaries Stores temporary and values of an expression.
Local Data Stores local data.
Machine Status Holds machine status like registers, program counter, etc.
Control Link Keep the address of the activation record of the caller procedure.
Access Link It holds the information about the data that lies outside the local range.
Actual Parameters Stores actual parameters that are used to send input to the called procedure.
Return Value Stores return values.

When a procedure is called by another procedure, the control stack plays a vital role by storing the activation record on it. The execution of the caller is suspended until the execution of the called procedure gets finished, and during this duration, the activation record of the called procedure is stored on the stack.

Example:

. .

printf(“Enter the UID: “);

scanf(“%s”, UID);

show_data(UID);

printf(“Press any key to continue…”);

. . .

int show_data(char *user)

{

printf(“Your ID is %s”, UID);

return 0;

}

. . .

When a program executes and a procedure is called, the control is automatically transferred to that called procedure, and once the procedure is executed, the control returns to the caller. When you need to represent the series of activations in a tree pattern, such flow control makes it easier, and thus, it is called an Activation tree.

Runtime Environment in Compiler Design

Storage Allocation

Static Allocation:

When compiled data has a fixed memory location, and it doesn’t change while the program executes, it is called Static Allocation. The runtime support package is not required for the allocation and deallocation of the memory as the storage location is prior known.

Stack Allocation:

The Stack allocation mechanism works on LIFO, i.e. last in first out principle; it is mainly used during reversed procedure calls.

Heap Allocation:

Heap is a dynamic memory allocation. It dynamically allocates the memory to the variable and gets it back when the variable is no longer required. The local variable to a procedure allocates and deallocates the memory during runtime.

The code, procedures, and variables are the entities for which the runtime environment manages the memory requirements.

  • Code: It is the text portion of the program that doesn’t change at runtime, and its memory requirements are known at the compile time.
  • Procedures: The procedures in any program are called randomly though the text portion is static; thus, it requires stack storage to maintain the procedure call and activations.
  • Variables: The Heap allocation mechanism is used to maintain the allocation or reallocation as the variables are known at runtime only.

Parameter Passing:

The Parameter passing is a way of communication among the variables. The called procedure gets the value of the variable from the calling procedure through a default mechanism. Now before proceeding further, we need to understand some basic terminologies as well.

  • Formal Parameter:

The formal parameters are the variables declared in the called function’s definition and take the information passed by the calling process.

  • Actual Parameters:

The variables that are mentioned in the function are called arguments, and whose address or values are passed to the called process are known as actual parameters.

Pass by Value:

When the calling process passes the r-value of the actual parameter, and then it is transferred to the activation record of the called process is known as the pass by value mechanism. The formal parameter is responsible for holding the values passed by the calling procedure, and if these values hold by formal parameter changes, then it should not affect the actual parameter.

Pass by Reference:

When the l-value of the actual parameter is transferred to the activation record of the called procedure and now the called procedure contains the address of the actual parameter, and also the formal parameters refer to the same memory is known as pass by reference. Thus, If the value held by the formal parameter changes, there should be no change in the actual parameter as they point to the same value.

Pass by Copy-restore:

Pass by copy-restore is very much similar to pass by reference; the difference is that when the procedure call ends, the changes are visible in actual parameters. When there is the function call, the value of actual parameters is transferred to the activation record of the called procedure. If there is a change in the formal parameters, they will not have any real-time change in the actual parameters, but when the called procedure ends, the l-value of the formal parameter is transferred to the l-value of the actual parameters.

int x;

calling_procedure()

{

x = 10;

copy_restore(x); //l-value of x is passed

printf x; //prints 99

}

copy_restore(int y)

{

y = 99; // x still has value 10 (unaffected)

x = 0; // x is now 0

}

When this function ends, the l-value of formal parameter y is copied to the actual parameter x. If the value of x is changed before the procedure ends, the l-value of y is copied to the l-value of x, making it work as a call by reference.

Pass by Name:

When the name of the procedure is called and is replaced by its actual body, it is called pass by name. It substitutes the argument during procedure call for corresponding parameters in the body, and thus, it can work on actual parameters, much similar to pass by reference.

Practice Problem – Runtime Environment in Compiler Design

Q. Consider the program given below in a block-structured pseudo-language with lexical scoping and nesting of procedures permitted.

Runtime Environment in Compiler Design Image 1a

Consider the calling chain: Main → A1 → A2 → A21 → A1

The correct set of activation records, along with their access links, is given by:

A.

Runtime Environment in Compiler Design Image 2

B.

. Runtime Environment in Compiler Design Image 3

C.

Runtime Environment in Compiler Design Image 4

D.

Runtime Environment in Compiler Design Image 5

Also Explore,

Keep learning and stay tuned to get the latest updates on the GATE Exam along with Eligibility Criteria, GATE Syllabus for CSE (Computer Science Engineering)GATE CSE NotesGATE CSE Question Paper, and more.

Also Explore,

Comments

Leave a Comment

Your Mobile number and Email id will not be published.

*

*