Question

In: Computer Science

For many languages, activation records form a stack at runtime. They are pushed on call and...

  • For many languages, activation records form a stack at runtime. They are pushed on call and popped on return. How can allocation and deallocation for such a stack of activation records be implemented?
  • ​​​​​​​

Solutions

Expert Solution

Activation Records :

1.Modern imperative programming languages typically have local variables.

*Created upon entry to function.

*Destroyed when function returns.

2.Each invocation of a function has its own instantiation of local variables.

*Recursive calls to a function require several instantiations to exist simultaneously.

*Functions return only after all functions it calls have returned last-in-first-out (LIFO) behavior.

* A LIFO structure called a stack is used to hold each instantiation.

3.The portion of the stack used for an invocation of a function is called the function’s stack frame or activation record.

Stack
*Used to hold local variables.
*Large array which typically grows downwards in memory toward lower addresses,shrinks upwards.
*Push(r1):
stack_pointer--;
M[stack_pointer] = r1;
*r1 = Pop():
r1 = M[stack_pointer];
stack_pointer++;
*Previous activation records need to be accessed, so push/pop not sufficient.
– Treat stack as array with index off of stack pointer.
– Push and pop entire activation records.

Consider the following example,
let
function g(x:int) =
let
var y := 10
in
x+y
end
function h(y:int):int =
y + g(y)

in
h(4)
end

Step 1: h(4) called

Chunk of memory allocated on the stack in order to hold local variables of h. The activation record (or stack frame) of h is pushed onto the stack.

Step 2: g(4) called

Activation record for g allocated (pushed) on stack.

Step 3: g(4) returns with value 14

Activation record for g deallocated (popped) from stack.

Step 4: h(4) returns with value 18

Activation record for h deallocated (popped) from stack. Stack now empty.


Related Solutions

5 How Many Stacks? An NFA has no stack. It recognizes regular languages. A PDA is...
5 How Many Stacks? An NFA has no stack. It recognizes regular languages. A PDA is defined as an NFA with one stack. It recognizes context-free languages. Prove that a PDA with two stacks recognizes Turing-recognizable languages
Using Java A stack is a type of data collection on which things can be “pushed”...
Using Java A stack is a type of data collection on which things can be “pushed” and “popped”. For example, a stack of plates can have a plate added (or “pushed”) onto the stack, and a plate removed (or “popped”) from the stack. Plates are pushed onto or popped off of the stack one at a time. The last plate pushed onto the stack is the first plate popped off of the stack. This type of structure is known as...
True/False 1. An automatic storage class variable is stored in the runtime stack 2. When a...
True/False 1. An automatic storage class variable is stored in the runtime stack 2. When a value parameter is passed to a function, the function called utilizes a copy of that parameter which is internal to the function called 3. When a reference parameter is passed to a function, the function called receives the memory address of that parameter and does not make a copy of it 4. Debug flags often control extra output to be printed conditionally Multiple choice...
Letters are pushed on a stack in order: R A N D O M O P...
Letters are pushed on a stack in order: R A N D O M O P S. Specify where to insert pop operations (shown by ‘*’) among the pushes of the given letters, in order to produce the output: ADONOMSPR . You can only do this process once. That is, you cannot take the output produced and then pass it again through the stack.
An Antique dealer needs to store records of the items in the inventory into a stack...
An Antique dealer needs to store records of the items in the inventory into a stack (the dealer believes that keeping items longer will be beneficial; nonetheless, the business logic here is not important!). Besides the usual push() and pop(); the dealer needs to always know what is the most expensive item in the inventory/stack. You are hired to implement a new method, called max(), which returns the current maximum value for any item in the stack. Notice that the...
Guided Assignment Problem 3: Call Stack Your Tasks: Answer the following questions. Include a function stack...
Guided Assignment Problem 3: Call Stack Your Tasks: Answer the following questions. Include a function stack chart/diagram for each question to verify your answers. Use a call stack to explain the result of calling example(3). int example(int n) {   if (n == 0) return 0;   else return example(n – 1) + n * n * n; } Use a call stack to explain how many time(s) the method factorial invokes itself with an argument of 5 of factorial(5). int factorial(int...
2. Convert the following infix form expression into the postfix form expression by using a stack:...
2. Convert the following infix form expression into the postfix form expression by using a stack: A+(B*(C-D)+E)/F-G*H Show the stack after each push/pop.
Some languages support many modes of parameter passing. Provide 2 examples using two different programming languages...
Some languages support many modes of parameter passing. Provide 2 examples using two different programming languages which support the user of the programming language deciding which to use when creating their method. (Programming Languages)
Once the form in this file is submitted, create a new paragraph that will stack on...
Once the form in this file is submitted, create a new paragraph that will stack on top of the existing paragraphs and will contain the data entered into the textarea. Each new paragraph should have the class 'tweet' applied to it. *Hint: Remember to use return false; to stop the form submission from reloading the page. <!DOCTYPE html> <html> <head>    <title>Simple Twitter</title>    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">    <style>    .tweet{border-top:1px solid #dedede; padding:20px;}    </style>    <script...
Once the form in this file is submitted, create a new paragraph that will stack on...
Once the form in this file is submitted, create a new paragraph that will stack on top of the existing paragraphs and will contain the data entered into the textarea. Each new paragraph should have the class 'tweet' applied to it. *Hint: Remember to use return false; to stop the form submission from reloading the page. <!DOCTYPE html> <html> <head> <title>Simple Twitter</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous"> <style> .tweet{border-top:1px solid #dedede; padding:20px;} </style> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> </head> <body class="container" style="width:50%"> <h1>Twitter</h1>...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT