Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Consider the following program written in Ada syntax with the execution stack sh

ID: 3714833 • Letter: C

Question

Consider the following program written in Ada syntax with the execution stack shown on the right side. The line numbers are used to refer to the code and are not part of the code

01 02 03 04 05 06 07 08 09 10 procedure env is x env integer: y env integer; procedure a is x a integeri x b: integeri procedure c is x e integeri begin 12 13 14 15 16 17 18 19 20 21 end c 10 begin 12 CF end b procedure d is begin envi end d 23 24 25 26 27 28 begin end ai begin end env Give the code that should be executed to set the access link for activation record 2 Give the code that should be executed to set the access link for activation record 3 Give the code that should be executed to set the access link for activation record 4 Give the code that should be executed to set the access link for activation record 5 Give the low-level code (in terms of mem[ ]) for x b = x a ; on line 15 Give the low-level code (in terms of mem I) for x_envx b + y env; on line 11 Note on Ada syntax. A procedure declaration has the following form procedure p is // declaration of local variables and other // procedures nested within p begin end p; procedure calls are written without parentheses. So, in the code above, the body of env has a call to procedure a in // body of p its body

Explanation / Answer

Activation Record:

Activation is an execution of a subprogram. Generally in most of the languages, local variables are allocated when this execution begins (activation time). The storage required for activation is organized as an activation record (or frame).

In a language with recursion, each simultaneous activation of a recursive subprogram can have different parameters, different values for local variables, return a different result ...

• each activation needs its own activation record

• the number of activation records needed isn’t known at compile time

• allocation of activation records is dynamic, and local variables are stack dynamic (unless declared static)

In Ada the allocation of activation record can be on the stack.

Static links are set in the following way. Here we need to find the most recent occurrence of an ARI of the static parent of the subprogram being called. This could be done by following the dynamic links from one ARI to the next checking for some piece of data that uniquely identifies the ARI. However, there is a simpler method using the nesting depth of the subprograms. The compiler can determine this depth because it has had to analyze the nesting of subprograms.

The actual access to non-locals starts with the static link, since this is the static parent, but if there are references to non-locals outside this scope, then they must be searched for by following further static links.

In the activation records 6,7 and 8 links executes when program was loaded and running. We can easily access them while the program is being executed.

This can be done when the address of these records. The address of a variable can be calculated using address attribute. The Attribute 'Address returns the address of a variable.

To print or display using Address Attribute:

            with System.Address_Image;

            i: Integer;

            ...

            put_line("Address of i: "& System.Address_Image(i'address));

           

We will convert a value of an access type into a type that we can print.