What is Implementation of Block Structured Language in compiler design?


A block is a statement containing its own local data declaration. The concept of a block is originated with ALGOL. The block-structured language permits an array with adjustable length. The main feature of blocks is their bracketing structure (begin and end used in ALGOL) in which they can define their data.

Activation Record for Block Structured Languages

Block structured language like ALGOL, and PL/I permit adjustable arrays, i.e., of varying length. Therefore, we cannot store irregular size arrays in between activation records. It can allocate the flexible or variable arrays at one corner of the activation record or above the fixed-size data. A pointer to these adjustable arrays will be stored in a fixed position of the activation record.

The following figure shows an activation record of a procedure having two Adjustable length arrays.

Consider a block-structured program

procedure P;

real A ;

real Array X [10] ;

In this procedure, we have taken 3 Blocks. Execution of these blocks will lead to storage of their local data items A, B, C, D, and adjustable array X, Y, Z into the stack. Activation Record for a particular block will be generated when that Block is currently being executed.

If Block 2 is active or undercurrent execution, then Activation Record of Procedure P will save Local data and pointer to adjustable arrays of Block 2 at the top.

Display & Static Links

There are two methods to access Non-Local data for a procedure are as follows −

  • Static Link− In this method, a pointer called static link is attached with each procedure which points to the topmost activation record of that procedure that physically surrounds it in the program. So, Non-local data references for any procedure can be found out by descending chain of pointers to find all statically enclosing procedures.

  • Display− The display is an array of pointer which are maintained to speed up the access to non-local data.

Parameter Passing

After the procedure is called, the parameters are passed to the procedure. There are two parameters

  • Actual Parameter
  • Formal Parameter

Depending on these parameters, there are various parameter passing methods −

  • Call by Value− It is the simplest method for parameter passing. The actual parameters are computed, and their r-values are passed to the called procedure.
  • Call by Reference− When the parameter is passed by reference also known as call by address or call by location, the caller passes to the caller procedure, a pointer to the storage address of each actual parameter.
  • Call by Name− It is a less popular method of parameter passing. The procedure is treated like macro. The procedure body is substituted for the call-in caller with actual parameters substituted for formals.

Returns

When procedure P1calls procedure P2, procedure P2 will return some value to P1.

Following changes will be made.

  • The value returned by P2 will be stored in space kept free for it in the activation record.
  • The top pointer will be restored, i.e., again point to the location on which it was earlier before P1 called P2.
  • Restore SP, i.e., set SP pointer to old SP in an activation record of P1.
  • Get return address from P1 activation record to complete return done by P2.


Updated on: 08-Nov-2021

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements