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

Errors in Compiler Design

Introduction Errors in compiler design refer to mistakes or issues that arise during the process of execution of the program. A compiler is a program that translates source code written in...

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

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.

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.

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.

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.

Machine-Independent Optimizations in Compiler Design

Machine-Independent Optimizations The main aim of machine-independent optimization is to improve the generated intermediate code so that compiler can get better target code. Eliminating unwanted code from the object code or replacing...

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

Target Machine

Target Machine A target machine is a byte-addressable machine. This machine has n general-purpose registers, R0, R1,.....Rn-1. A Simple Target Machine Model has three-address instruction. A full-edged assembly language would have...

3 minutes read.

LEX

LEX Lex is a tool/computer program that generates a Lexical analyzer. Lex is developed by Vern Paxson in C around 1987. Lex works together with the YACC parser generator. It allows...

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

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.

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.

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.

Syntax-Directed Translation

Syntax-Directed Translation A context-free-grammar with some additional rules is known as a syntax-directed definition. In SDT, attributes are associated with grammar symbols and rules are associated with productions. The attributes can...

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

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.

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.

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.

S-attributed and L-attributed SDTs

S-attributed and L-attributed SDTs STD stands for Syntax Directed Translation. When we associate some informal notations called semantic rules and the grammar, they are known as STD. So we can say...

2 minutes read.