Runtime Stack Snapshot for ML Code before last call to plus, Assignments of Programming Languages

The memory layout (runtime stack) for the given ml code, showing the activation records (ar) and their contents for each function call, including control links, access links, return addresses, return value addresses, parameters, and local variables. How function values are represented by closures and how the stack is used to keep track of function calls.

Typology: Assignments

Pre 2010

Uploaded on 07/30/2009

koofers-user-4jo
koofers-user-4jo 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Homework 5
due before class Oct 31, 2008
1. (20pts) Draw a snapshot of the runtime stack for the following ML code immediately
before the last call to plus returns to the body of repeat.
1 fun plus(x) = x + 1;
2 fun main(x) =
3 let val y = x + 1
4 in let fun repeat(f,x) =
5 let y = f(x)
6 in f(y)
7 end
8 in repeat(plus, y)
9 end
10 end;
11 main (5);
12
The activation record for each function needs to have a control link, an access link,
a return address, a return value address, parameters, and local variables. Remember
that function values are represented by closures and that a closure is a pair consisting
of an environment (pointer to an activation record) and compiled code. Use the line
numbers in the code to indicate the return address of each function call (the line
number immediately following the function call). Follow guidelines in lab8.
Solution:
/* "->" means "points-to"; ":" means "has value" */
/* <global, code for plus> is the closure storage for function plus*/
/* <main(5) : ?> means main(5) has an undefined value at the time*/
/* <main.main(5)> means the main(5) storage allocated in the AR of main*/
Memory layout (runtime stack):
global: {
Control Link -> NULL;
Access Link -> NULL;
local-vars--- plus -> <global, code for plus>
main -> <global, code for main>
main(5) : ?
}
main : {
1
pf2

Partial preview of the text

Download Runtime Stack Snapshot for ML Code before last call to plus and more Assignments Programming Languages in PDF only on Docsity!

Homework 5

due before class Oct 31, 2008

  1. (20pts) Draw a snapshot of the runtime stack for the following ML code immediately before the last call to plus returns to the body of repeat.

1 fun plus(x) = x + 1; 2 fun main(x) = 3 let val y = x + 1 4 in let fun repeat(f,x) = 5 let y = f(x) 6 in f(y) 7 end 8 in repeat(plus, y) 9 end 10 end; 11 main (5); 12

The activation record for each function needs to have a control link, an access link, a return address, a return value address, parameters, and local variables. Remember that function values are represented by closures and that a closure is a pair consisting of an environment (pointer to an activation record) and compiled code. Use the line numbers in the code to indicate the return address of each function call (the line number immediately following the function call). Follow guidelines in lab8. Solution:

/* "->" means "points-to"; ":" means "has value" / / <global, code for plus> is the closure storage for function plus/ / <main(5) : ?> means main(5) has an undefined value at the time/ / <main.main(5)> means the main(5) storage allocated in the AR of main*/ Memory layout (runtime stack): global: { Control Link -> NULL; Access Link -> NULL; local-vars--- plus -> <global, code for plus> main -> <global, code for main> main(5) :? }

main : {

Control Link -> global; Access Link -> global; Return Address -> line12; Return Result Address -> global.main(5); parameters--- x : 5 ; Local-vars--- y : 6; }

main-line4: { Control Link -> main; Access Link -> main; local-vars--- repeat -> <main-line4, code for repeat> ; repeat(plus,y) :? ; }

repeat(plus,6) : { Control Link -> main-line4; Access Link -> main-line4; Return Address -> line 9; Return Result Address -> main-line4.local-vars.repeat(plus,y); parameters--- f : <global, code for plus>; x : 6 local-vars --- y : 7 ; f(x) : 7; f(y) : ?; }

plus(7) : { Control Link -> repleat(plus,6); Access Link -> global; Return address -> line 7; Return Result Address -> repeat(plus,6).local-vars.f(y) parameters--- x : 7 ; }