Docsity

CS 440: Understanding Parameter Passing Styles and Memory Management in C-like Languages -, Study notes of Programming Languages

Toggle sidebar
bg1
CS 440 — Parameter Passing Styles March 13, 2009
1 Introduction and Objectives
This lecture covers some of the different places variables can be kept in memory. By the end of
lecture, you should be familiar with
•The function call stack
–static and dynamic links, and how nested scopes are resolved
–frame pointer, stack pointer
•how local variables are stored
•how object member variables are stored
•the difference between boxed and unboxed data
2 Global Variables
2.1 Global Variables
Global variables are kept on the system stack.
Consider leti=5*2
Order of operations:
•First, 5*2is evaluated.
•Second, 10 is place at the top of stack.
•Third, SP is decremented by one word.
Stack
Addr Value
8000 10 i
7996 SP
7992
7988
7984
· · · · · ·
What happens when
let (j,k) = 10 + i, i + 2 is executed?
2.2 Answer
Stack
Addr Value
8000 10 i
7996 20 j
7992 12 k
7988 SP
7984
· · · · · ·
What if we now execute leti=i+4?
Mattox Beckman Page 1 Illinois Institute of Technology
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download CS 440: Understanding Parameter Passing Styles and Memory Management in C-like Languages - and more Study notes Programming Languages in PDF only on Docsity!

1 Introduction and Objectives

This lecture covers some of the different places variables can be kept in memory. By the end of lecture, you should be familiar with

  • The function call stack
    • static and dynamic links, and how nested scopes are resolved
    • frame pointer, stack pointer
  • how local variables are stored
  • how object member variables are stored
  • the difference between boxed and unboxed data

2 Global Variables

2.1 Global Variables

Global variables are kept on the system stack. Consider let i = 5 * 2 Order of operations:

  • First, 5 * 2 is evaluated.
  • Second, 10 is place at the top of stack.
  • Third, SP is decremented by one word.

Stack Addr Value 8000 10 i 7996 SP 7992 7988 7984 · · · · · · What happens when let (j,k) = 10 + i, i + 2 is executed?

2.2 Answer

Stack Addr Value 8000 10 i 7996 20 j 7992 12 k 7988 SP 7984 · · · · · · What if we now execute let i = i + 4?

2.3 Garbage created!

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.

2.4 Stack-based languages

There are plenty of languages that use this kind of access!

  • Forth
  • Postscript
  • Assembly

These languages are called stack-based. They are suitable for small, cheap processors, low amounts of memory....

3 Function Calls

3.1 C-Like Languages

C-like languages have functions with these properties:

  • Two layers of scope (local and global).
  • Multiple, possibly variable number of parameters.
  • Functions not first-class.

In order to call a function in a C-like language, we need space on the stack for several things.

  • Return address
  • Arguments
  • Pointer to previous stack frame
  • Local variables

3.5 Calling inc

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 · · · · · ·

3.6 Stack before inc returns.

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 · · · · · ·

3.7 Stack after inc returns

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 · · · · · ·

4 Nested Scopes

4.1 Pascal Like Languages

  • Many languages have nested scope... functions can be defined within functions.
  • Pascal is the most famous.
  • Haskell, OCaml (and most functional languages) let you do this too.

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

  • We now need a pointer to the parent scope’s stack frame.

4.2 New Frame

arg n · · · dynamic link FP local var 1 · · · return address static link other stuff arg m · · · dynamic link

  • The static link points to the stack frame of the parent func- tion.
  • The dynamic link points to the stack frame of the calling func- tion. - Don’t confuse them!!

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 · · · · · ·

5 Objects

5.1 Objects

  • The stack is used for local variables, parameters, and scalar data.
    • Integers, references, etc.
  • Objects, and other “large” (non-scalar) data is usually placed on the heap.
    • Java: objects
    • Haskell, OCaml: user-defined types, objects
    • C: things allocated with malloc()
  • Heap allocated data persists across function calls.

5.2 Heap Example

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 · · · · · ·

5.3 Heap example 2

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 · · · · · ·

5.4 Heap example 3

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 · · · · · ·

5.5 Heap example 4 — Unboxed Leaf

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 · · · · · ·

5.6 Heap example 4 — Boxed Leaf

data Tree = Branch Int Tree Tree | Leaf t = Branch (5, Branch(4,Leaf,Leaf),Leaf)

6.2 Stack Activity Answer

  • Consider the code below. The indentation is supposed to show the scope. What does the stack look like if foo calls funa calls funb calls func calls fund calls fune? Just show the static and dynamic links. The links from funa to foo have been done for you.

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

6.3 Heap activity

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)

6.4 Heap Activity Answer

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 · · · · · ·

7 Colophon

7.1 Credits

The stack frame diagram for C languages is based on the one in Modern Compiler Implementation in ML by Andrew Appel, p127.