Stack Allocation of Space

Stack Allocation of Space

Almost all compilers for languages that use procedure, functions, or methods manage their run-time memory as a stack. Whenever a procedure is called, the local variable's space is pushed into a stack and popped off from the stack when the procedure terminates.

Activation Trees

A program is a set of instruction with some operation associated with it. The sequence in which the procedure executes is known as activation. We also assume that a procedure executes in a sequential manner, and this sequence is easily represented by a tree known as the activation tree.

The following example illustrates the nesting of procedure calls:

Example

int a[11];                     
 void readArray() {                                                                                                                                                
       int i;                                                                                                                                                          
       ...                                                                                                                                                               
 }                                                                                                                                                                     
 int partition(int m, int n)  {                                                                                                                             
   ....                                                                                                                                                                 
 }                                                                                                                                                                       
 void quicksort(int m, int n) {                                                                                                                       
 int i;                                                                                                                                                              
 if (n > m) {                                                                                                                                                                                              
 i = partition(m, n);                                                                                                                                             
 quicksort(m, i-1);                                                                                                                                         
 quicksort(i+1, n);                                                                                                                                                                
            }                                                                                                                                                           
 }                                                                                                                                                                        
 main() {                                                                                                                                                  
      readArray();                                                                                                                                         
      a[0] = -9999;                                                                                                                                            
      a[10] = 9999;                                                                                                                                                
      quicksort(1,9);                                                                                                                                         
 }                                                                                                                               

The above code shows that array ‘a’ reads nine integers and sorts them using the recursive quicksort algorithm.

The task of the main function is to invoke the function readArray, sets the sentinels, and then call the function quicksort on the entire data array.

The code below shows a set of calls that might result from the execution of a program.

enter main( )        
         enter readArray()                                                                                                                                                                  
         leave readArray()                                                                                                                            
         enter quicksort(1, 9)                                                                                                                        
                 enter partition(1, 9)                                                                                                                  
                 leave partition(1, 9)                                                                                                                  
                 enter quicksort(1, 3)                                                                                                                                                                                                    
                     ....                                                                                                                                           
              leave quicksort(1, 3)                                                                                                                         
              enter quicksort(5, 9)                                                                                                                   
                    ....                                                                                                                                                      
             leave quicksort(5, 9)                                                                                                                    
       leave quicksort(1, 9)                                                                                                                         
 leave main()                                                                                                                                                            

The figure below shows the possible activation tree for the given code.

Stack Allocation of Space

Activation Records

A run-time stack known as a control stack manages the procedure calls and returns. The control stack stores the activation record of each live activation. The top of the stack will hold the latest activation record.

The content of the activation record is given below. This record may vary according to the implemented languages.

    Actual parameters
      Returned values
        Control link
        Access link
   Saved machine status
        Local data
        Temporaries

Temporaries: The values which arise from the evaluation of expression will be held by temporaries.

Local data: The data belonging to the execution of the procedure is stored in local data.

Saved machine status: The status of the machine that might contain register, program counter before the call to the procedure is stored in saved machine status.

Access link: The data's information outside the local scope is stored in the access link.

 Control link: The activation record of the caller is pointed by the control link.

Returned values: It represents the space for the return value of the called function if any.

Actual parameter: It represents the actual parameters used by the calling procedure.


Related Topics

LR Parser in Compiler Design

LR Parser The most popular type of bottom-up parsing is LR(K) parsing. The LR() parser scans the input from left – to – right, which is the actual abbreviation of L...

2 minutes read.

Regular Expression | Compiler Design

Regular Expression A regular expression is a set of patterns that can match a character or string. It can also match alternative characters or strings. The grammar defined by the regular...

3 minutes read.

Phases of Compiler

Phases of Compiler The compilation process of a compiler goes through various phases. Each phase of the compiler takes the output from the previous as an input. Here we will see all...

2 minutes read.

Finite State Machine

Finite State Machine A finite state machine is a simple machine to recognize patterns. It takes a string of symbols as the input and changes its state to another state, but...

3 minutes read.

Derivation and Parse Tree in Compiler Design

Derivation and Parse Tree In this article, we will learn Derivation and Parse Tree. Derivations The parse tree can be constructed by taking a derivational view in which production is treated as rewriting...

4 minutes read.

Evolution of Programming Languages in Compiler Design

The Evolution of Programming Languages The first computer came in the 1940s and was programmed in a binary language that told the computer what operations are to be performed and in...

4 minutes read.

Run-Time Environments

Run-Time Environments Storage Organization Every target program has its own logical address, and an executable program runs in it. The logical address space has the location for each program value. The...

2 minutes read.

Storage Allocation

Storage Allocation The storage allocation represents memory management. The allocation of memory can be done in the following ways: Static AllocationStack AllocationHeap Management Static Allocation: It is a procedure used for the allocation of...

1 minute read.

Translation of Array References in Compiler Design

Translation of Array References We can access the elements of an array stored in consecutive blocks very easily and quickly. In a programming language like C and Java, the size of...

2 minutes read.

YACC in Compiler Design

YACC  YACC is known as Yet Another Compiler Compiler. It is used to produce the source code of the syntactic analyzer of the language produced by LALR (1) grammar. The input...

5 minutes read.

Run-Time Storage Management

Run-Time Storage Management Every executing program has its own logical address space. Logical address space is partitioned into: Code: It is responsible for storing the executable target code. Static: It is used to...

3 minutes read.

Optimization of Basic Blocks in Compiler Design

Optimization of Basic Blocks We can apply the optimization process on a basic block. While optimization, there is no need to change the set of expressions computed by the block. The basic...

3 minutes read.

Lexical Analysis in Compiler Design

Lexical Analysis It is the first phase of the compiler. As we know, it is also known as a scanner. The input for lexical analysis is source code. After taking source...

2 minutes read.

Syntax Analysis Compiler Design

Syntax Analysis This article will describe the parsing method used in the compiler. The grammatical rule of programming language can be constructed with the help of context-free grammars or BNF (Backus–Naur...

3 minutes read.

Switch Case Statement Compiler Design

Case Statement The “case” or “switch” statement is available in various languages. The following is the syntax for the case statement: switch (E)  {             case V1: S1             case V2: S2 ...

1 minute read.

Three-Address Code

Three-Address Code If there is at most one operator on the right side of the instruction, then the instruction will be the three-address code so that no arithmetic expressions are permitted....

2 minutes read.

Symbol Table Compiler Design

Symbol Table It is an important data structure used by the compiler. It stores information about various entities such as object, class, variable names, functions name, interfaces, procedure, literals, string etc.  The...

3 minutes read.

Code Generation

Code Generation The last phase of the compiler is code generation. It is the compiler's back-end that makes multiple passes over the IR before generating the target program. The code generator's...

4 minutes read.

Data Flow Analysis in Compiler Design

Data Flow Analysis All the optimization techniques we have learned earlier depend on data flow analysis. DFA is a technique used to know about how the data is flowing in any...

3 minutes read.

Ambiguity Elimination Compiler Design

Ambiguity Elimination Ambiguity elimination makes the sentence clear and readable. A sentence is grammatically ambiguous if it can produce more than one parse tree for a particular grammar. In this article,...

3 minutes read.