Compiler – Run-time Environment

Compiler Design – Run-Time Environment ”; Previous Next A program as a source code is merely a collection of text (code, statements etc.) and to make it alive, it requires actions to be performed on the target machine. A program needs memory resources to execute instructions. A program contains names for procedures, identifiers etc., that require mapping with the actual memory location at runtime. By runtime, we mean a program in execution. 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. Runtime support system is a package, mostly generated with the executable program itself and facilitates the process communication between the process and the runtime environment. It takes care of memory allocation and de-allocation while the program is being executed. Activation Trees A program is a sequence of instructions combined into a number of procedures. Instructions in a procedure are executed sequentially. A procedure has a start and an end delimiter and everything inside it is called the body of the procedure. The procedure identifier and the sequence of finite instructions inside it make up the body of the procedure. The execution of a procedure is called its activation. An activation record contains all the necessary information required to call a procedure. An activation record may contain the following units (depending upon the source language used). Temporaries Stores temporary and intermediate values of an expression. Local Data Stores local data of the called procedure. Machine Status Stores machine status such as Registers, Program Counter etc., before the procedure is called. Control Link Stores the address of activation record of the caller procedure. Access Link Stores the information of data which is outside the local scope. Actual Parameters Stores actual parameters, i.e., parameters which are used to send input to the called procedure. Return Value Stores return values. Whenever a procedure is executed, its activation record is stored on the stack, also known as control stack. When a procedure calls another procedure, the execution of the caller is suspended until the called procedure finishes execution. At this time, the activation record of the called procedure is stored on the stack. We assume that the program control flows in a sequential manner and when a procedure is called, its control is transferred to the called procedure. When a called procedure is executed, it returns the control back to the caller. This type of control flow makes it easier to represent a series of activations in the form of a tree, known as the activation tree. To understand this concept, we take a piece of code as an example: . . . printf(“Enter Your Name: “); scanf(“%s”, username); show_data(username); printf(“Press any key to continue…”); . . . int show_data(char *user) { printf(“Your name is %s”, username); return 0; } . . . Below is the activation tree of the code given. Now we understand that procedures are executed in depth-first manner, thus stack allocation is the best suitable form of storage for procedure activations. Storage Allocation Runtime environment manages runtime memory requirements for the following entities: Code : It is known as the text part of a program that does not change at runtime. Its memory requirements are known at the compile time. Procedures : Their text part is static but they are called in a random manner. That is why, stack storage is used to manage procedure calls and activations. Variables : Variables are known at the runtime only, unless they are global or constant. Heap memory allocation scheme is used for managing allocation and de-allocation of memory for variables in runtime. Static Allocation In this allocation scheme, the compilation data is bound to a fixed location in the memory and it does not change when the program executes. As the memory requirement and storage locations are known in advance, runtime support package for memory allocation and de-allocation is not required. Stack Allocation Procedure calls and their activations are managed by means of stack memory allocation. It works in last-in-first-out (LIFO) method and this allocation strategy is very useful for recursive procedure calls. Heap Allocation Variables local to a procedure are allocated and de-allocated only at runtime. Heap allocation is used to dynamically allocate memory to the variables and claim it back when the variables are no more required. Except statically allocated memory area, both stack and heap memory can grow and shrink dynamically and unexpectedly. Therefore, they cannot be provided with a fixed amount of memory in the system. As shown in the image above, the text part of the code is allocated a fixed amount of memory. Stack and heap memory are arranged at the extremes of total memory allocated to the program. Both shrink and grow against each other. 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. Before moving ahead, first go through some basic terminologies pertaining to the values in a program. r-value The value of an expression is called its r-value. The value contained in a single variable also becomes an r-value if it appears on the right-hand side of the assignment operator. r-values can always be assigned to some other variable. l-value The location of memory (address) where an expression is stored is known as the l-value of that expression. It always appears at the left hand side of an assignment operator. For example: day = 1; week = day * 7; month = 1; year = month * 12; From this example, we understand that constant values like 1, 7, 12, and variables like day, week, month and year, all have r-values. Only variables have l-values as they also represent the memory location assigned to them. For example: 7 = x + y; is an l-value error, as the constant 7 does not represent any memory location. Formal Parameters Variables that

Compiler Design – Architecture

Compiler Design – Architecture ”; Previous Next A compiler can broadly be divided into two phases based on the way they compile. Analysis Phase Known as the front-end of the compiler, the analysis phase of the compiler reads the source program, divides it into core parts and then checks for lexical, grammar and syntax errors.The analysis phase generates an intermediate representation of the source program and symbol table, which should be fed to the Synthesis phase as input. Synthesis Phase Known as the back-end of the compiler, the synthesis phase generates the target program with the help of intermediate source code representation and symbol table. A compiler can have many phases and passes. Pass : A pass refers to the traversal of a compiler through the entire program. Phase : A phase of a compiler is a distinguishable stage, which takes input from the previous stage, processes and yields output that can be used as input for the next stage. A pass can have more than one phase. Print Page Previous Next Advertisements ”;