

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 2
This page cannot be seen from the preview
Don't miss anything!


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 ; }