• No results found

BLOCK - STRUCTURED LANGUAGE

In document Compiler Notes (Page 44-47)

23. PARAM VARIANCE

3.1 STRUCTURED VARIABLES

8.3.3 BLOCK - STRUCTURED LANGUAGE

. .

FREE (P) ; frees the storage indicated by pointer P.

A variable that is dynamically allocated in this way does not occupy a fixed location in an activation record, so it cannot be referenced directly using base relative addressing. Such a variable is usually accessed using indirect addressing through a pointer variable P. Since P does occupy a fixed location in the activation record, it can be addressed in the usual way.

The mechanism to allocate a storage memory to a variable can be done in any of the following ways:

 A NEW or MALLOC statement would be translated into a request by the operating system for an area of storage of the required size.

 The required allocation is handled through a run-time support procedure associated with the compiler. With this method, a large block of free storage called a heap is obtained from the operating system at the beginning of the program. Allocations of the storage from the heap are managed by the run-time procedure.

 In some systems, the program need not free memory for storage. A run-time garbage collection procedure scans the pointer in the program and reclaims areas from the heap that are no longer used.

8.3.3 BLOCK - STRUCTURED LANGUAGE

A block is a unit that can be divided in a language. It is a portion of a program that has the ability to declare its own identifiers. This definition of a block is also meet the units such as procedures and functions.

Let us consider a Pascal program with number of procedure blocks as shown in fig. 40.

Each procedure corresponds to a block. Note that blocks are rested within other blocks. Example: Procedures B and D are rested within procedure A and procedure C is rested within procedure B. Each block may contain a declaration of variables. A block may also refer to variables that are defined in any block that contains it, provided the same names are not redefined in the inner block. Variables cannot be used outside the block in which they are declared.

In compiling a program written in a blocks structured language, it is convenient to number the blocks as shown in fig. 40. As the beginning of each new block is recognized, it is assigned the next block number in sequence. The compiler can then construct a table that describes the block structure. It is illustrated in fig. 41. The block- level entry gives the nesting depth for each block. The outer most block number that is one greater than that of the surrounding block.

PROCEDURE A ;

Since a name can be declared more than once in a program (by different blocks), each symbol-table entry for an identifier must contain the number of the declaring block.

A declaration of an identifier is legal if there has been no previous declaration of that identifier by the current block, so there can be several symbolic table entries for the same

name. The entries that represent declaration of the same name by different blocks can be linked together in the symbol table with a chain of pointers.

When a reference to an identifier appears in the source program, the compiler must first check the symbol table for a definition of that identifier by the current block. If not such definition is found, the compiler looks for a definition by the block that surrounds the current one, then by the block that surrounds that and so on. If the outermost block is reached without finding a definition of the identifier, then the reference is an error.

The search process just described can easily be implemented within a symbol table that uses hashed addressing. The hashing function is used to locate one definition of the identifier. The chain of definitions for that identifier is then searched for the appropriate entry.

Most block-structured languages make use of automatic storage allocation. The variables that are defined by a block are stored in an activation record that is created each time the block is entered. If a statement refers to a variable that is declared within the current block, this variable is present in the current activation record, so it can be accessed in the usual way. It is possible to refer to a variable that is declared in some surrounding block. In that case, the most recent activation record for that block must be located to access the variable.

Stack

(a) (b)

Fig. 42 Use of Display for Procedure

A data structure called display is used to access a variable in surrounding blocks.

The display contains pointers to the most recent activation records for the current block and for all blocks that surround the current one in the source program. When a block refers to a variable that is declared in some surrounding block, the generated object code uses the display to find the activation record that contains this variable.

Example:

When a procedure calls itself recursively thus an activation record is created on the stack as a result of the call. Assume procedure C calls itself recursively. It is shown in fig. 42(b) the record for C is created on the stack as a result of the call. Any reference to a variable declared by C should use this most recent activation record ; the display pointer for C is changed accordingly. Variables that correspond to the previous invocation of C are not accessible for the movement, so there is no display pointer to this

Activation

activation record.

Stack Display Fig 42(c)

Now if procedure 'C' call procedure D the resulting stack and display are as illustrated in fig. 42(c) . An activation record for D has been created in the usual way and added to the stack. Note, that the display now contains only two pointers: one each to the activation records for D and A. This is because procedure D cannot refer to variable in B or C, except through parameters that are passed to it, even though it is called from C.

According to the rules for the scope of names in as block-structured language, procedure D can refer only to variable that are declared by D or by some block that contains D in the source program.

In document Compiler Notes (Page 44-47)

Related documents