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

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...

2 minutes read.

Intermediate-Code Generator Compiler Design

Intermediate-Code Generator The process of translating a source language into machine code for a given target machine is done by intermediate-code. It lies between the high-level language and the machine language....

2 minutes 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.

LR Parser Compiler Design

LR Parser LR parsing is a type of bottom-up parsing that is used to parse the large class of grammars. Here "L" stands for left-to-right scanning of the input "R" stands...

6 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.

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.

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.

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.

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.

CLR Parsing Compiler Design

CLR Parsing CLR parsing refers to the canonical lookahead. We will use the canonical collection of LR(1) items for the construction of the CLR(1) parsing table. Generally, CLR(1) parsing has more...

6 minutes read.

Shift Reduce Parsing in Compiler Design

Like the bottom-up parsing, the shift reduce parser also builds the parse tree from the leaves (bottom) to the root (up). The LR parser is a more versatile variation of...

4 minutes read.

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.

SLR 1 Parsing Compiler Design

SLR(1) Parsing It is a simple LR parsing. Most of the function of this parsing is the same as LR(0) parsing. The parsing table for both the parser vary. The SLR(1)...

5 minutes read.

Bottom-Up Parsing in Compiler Design

Bottom-Up Parsing A bottom-up parsing constructs the parse tree for an input string beginning from the bottom (the leaves) and moves to work towards the top (the root). Bottom-up parsing is...

6 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.

Compiler Design Tutorial

Our compiler design tutorial will provide all the information about compiler from basic to advanced level. This compiler tutorial will help the student for their semester as well as for...

3 minutes read.

Boolean Expression in Compiler Design

Boolean Expression The translation of conditional statements such as if-else statements and while-do statements is associated with Boolean expression's translation. The main use of the Boolean expression is the following: Boolean expressions...

3 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.

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.

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.