






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







This lecture covers some of the different places variables can be kept in memory. By the end of lecture, you should be familiar with
Global variables are kept on the system stack. Consider let i = 5 * 2 Order of operations:
Stack Addr Value 8000 10 i 7996 SP 7992 7988 7984 · · · · · · What happens when let (j,k) = 10 + i, i + 2 is executed?
Stack Addr Value 8000 10 i 7996 20 j 7992 12 k 7988 SP 7984 · · · · · · What if we now execute let i = i + 4?
Stack Addr Value 8000 10 i 7996 20 j 7992 12 k 7988 14 i 7984 SP · · · · · · The first i is unreachable, so it’s garbage.
There are plenty of languages that use this kind of access!
These languages are called stack-based. They are suitable for small, cheap processors, low amounts of memory....
C-like languages have functions with these properties:
In order to call a function in a C-like language, we need space on the stack for several things.
int g = 10;
int inc(int i) { int t = i + 1; return t; }
void foo() { int k = 12; k = inc(k+q); }
Addr Value 8000 10 g 7996 42 q 7992 8000 dyn link 7988 12 k 7984 line ?? return 7980 54 i 7976 7992 FP, dyn link 7972 0 t 7968 line 10 return 7964 SP · · · · · ·
int g = 10;
int inc(int i) { int t = i + 1; return t; }
int foo(int q) { int k = 12; k = inc(k+q); }
Addr Value 8000 10 g 7996 42 q 7992 8000 dyn link 7988 12 k 7984 line ?? return 7980 54 i 7976 7992 FP, dyn link 7972 55 t 7968 line 10 return 7964 SP · · · · · ·
int g = 10;
int inc(int i) { int t = i + 1; return t; }
int foo(int q) { int k = 12; k = inc(k+q); }
Addr Value 8000 10 g 7996 42 q 7992 8000 FP, dyn Link 7988 55 k 7984 line ?? return 7980 SP · · · · · ·
foo a = let t = 10 + a in let bar b = let u = 20 in a + u + b + t + foo 9 let baz c = let v = 30 in a + v + c + t + bar 5 in baz 4
arg n · · · dynamic link FP local var 1 · · · return address static link other stuff arg m · · · dynamic link
Addr Value 8000 7 a 7996 9000 dyn link 7992 17 t 7988 Line? return 7984 9000 static link 7980 4 c 7976 7996 dyn link 7972 30 v 7968 Line 11 return 7964 7996 static link · · · · · ·
Addr Value 7960 5 b 7956 7976 dyn link 7952 20 u 7948 9 a 7944 7956 FP, dyn link 7940 19 t 7936 Line 6 return 7932 9000 static link · · · · · ·
xx = 10 :: 20 :: []
Stack Addr Value 8000 10000 xx 7996 7992 7988 7984 · · · · · ·
Heap Addr Value 10000 10 Head of First Node 10004 10008 Tail of First Node 10008 20 Head of Second Node 10012 0 Tail of Second Node 10016 · · · · · ·
xx = 10 :: 20 :: [] x::xs = ...
Stack Addr Value 8000 10000 xx 7996 10 x 7992 10008 xs 7988 7984 · · · · · ·
Heap Addr Value 10000 10 Head of First Node 10004 10008 Tail of First Node 10008 20 Head of Second Node 10012 0 Tail of Second Node 10016 · · · · · ·
xx = 10 :: 20 :: [] match xx with x::xs = ...
Stack Addr Value 8000 10000 xx 7996 10 x 7992 10008 xs 7988 7984 · · · · · ·
Heap Addr Value 10000 10 Head of First Node 10004 10008 Tail of First Node 10008 20 Head of Second Node 10012 0 Tail of Second Node 10016 · · · · · ·
data Tree = Branch Int Tree Tree | Leaf t = Branch (5, Branch(4,Leaf,Leaf),Leaf)
Stack Addr Value 8000 10000 t 7996 7992 7988 7984 · · · · · ·
Heap Addr Value 10000 5 10004 10012 Left 10008 Leaf Right 10012 4 10016 Leaf Left 10020 Leaf Right · · · · · ·
data Tree = Branch Int Tree Tree | Leaf t = Branch (5, Branch(4,Leaf,Leaf),Leaf)
foo a = ... let funa x = ... let funb y = ... let func q = ... let fund z = let fune w = ...
Static Frame Dynamic foo foo funa foo funa funb funa funa func funb foo fund func fund fune fund
Show the contents of the stack and the heap after the following code is evaluated. Assume that Leaf is unboxed.
data Tree = Branch of Int Tree Tree | Leaf t = Branch (5, Leaf, Leaf) rec u = Branch (7, t, u)
Stack Addr Value 8000 10000 t 7996 10012 u 7992 7988 7984 · · · · · ·
Heap Addr Value 10000 5 10004 Leaf 10008 Leaf 10012 7 10016 10000 10020 10012 · · · · · ·
The stack frame diagram for C languages is based on the one in Modern Compiler Implementation in ML by Andrew Appel, p127.