9
158 Functions © 2021 Uwe R. Zimmer, The Australian National University page 158 of 487 (chapter 3: “Functions” up to page 232) mov r0, #1 bl Plus_1 mov r4, r0 Plus_1: add r0, #1 bx lr lr G How is the parameter x passed? G Where do you find the result after the function returns? G Could it be done differently? G Does it work? 155 Functions © 2021 Uwe R. Zimmer, The Australian National University page 155 of 487 (chapter 3: “Functions” up to page 232) (Greatness from …) Small beginnings plus_1 :: (Num a) => a -> a plus_1 x = x + 1 int plus1 (int x) { return x + 1; } function Plus_1 (x : Integer) return Integer is (x + 1); def plus1 (x): return x + 1; pure function plus_1 (x) int, intent (in) :: x int :: plus_1 plus_1 = x + 1; end function; function Plus_1 (x : integer) : integer; begin Plus_1 := x + 1; end; 152 3 Functions Uwe R. Zimmer - The Australian National University Computer Organisation & Program Execution 2021 159 Functions © 2021 Uwe R. Zimmer, The Australian National University page 159 of 487 (chapter 3: “Functions” up to page 232) mov r0, #1 add r0, #1 mov r4, r0 G Can/should this always be done? G This is called inlining 156 Functions © 2021 Uwe R. Zimmer, The Australian National University page 156 of 487 (chapter 3: “Functions” up to page 232) mov r0, #1 bl Plus_1 mov r4, r0 Plus_1: add r0, #1 bx lr 153 Functions © 2021 Uwe R. Zimmer, The Australian National University page 153 of 487 (chapter 3: “Functions” up to page 232) References for this chapter [Patterson17] David A. Patterson & John L. Hennessy Computer Organization and Design – The Hardware/Software Interface Chapter 2 “Instructions: Language of the Computer” ARM edition, Morgan Kaufmann 2017 160 Functions © 2021 Uwe R. Zimmer, The Australian National University page 160 of 487 (chapter 3: “Functions” up to page 232) 157 Functions © 2021 Uwe R. Zimmer, The Australian National University page 157 of 487 (chapter 3: “Functions” up to page 232) mov r0, #1 bl Plus_1 mov r4, r0 Plus_1: add r0, #1 bx lr lr 154 Functions © 2021 Uwe R. Zimmer, The Australian National University page 154 of 487 (chapter 3: “Functions” up to page 232)

Functions - Australian National University

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Functions - Australian National University

158

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

158

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

mov

r0, #1

bl

Plus_1

mov

r4, r0

Plus_1:

add

r0, #1

bx

lrlr

Ho

w is

the

par

amet

er x

pas

sed

?

Wh

ere

do

yo

u fi

nd

the

resu

lt a

fter

the

fun

ctio

n r

etu

rns?

Co

uld

it b

e d

on

e d

iffe

ren

tly?

Do

es it

wo

rk?

155

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

155

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

(Gre

atne

ss fr

om …

) Sm

all b

egin

ning

splus_1 :: (Num a) => a -> a

plus_1 x = x + 1

int plus1 (int x) {

return x + 1;

} function Plus_1 (x : Integer) return Integer is (x + 1);

def plus1 (x):

return x + 1;

pure function plus_1 (x)

int, intent (in) :: x

int :: plus_1

plus_1 = x + 1;

end function;

function Plus_1 (x : integer) : integer;

begin

Plus_1 := x + 1;

end;

152

3Fu

nctio

ns

Uw

e R

. Zim

mer

- T

he A

ustr

alia

n N

atio

nal U

nive

rsity

Co

mp

ute

r Org

anis

atio

n &

Pro

gram

Exe

cuti

on

202

1

159

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

159

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

mov

r0, #1

add

r0, #1

mov

r4, r0

Can

/sh

ou

ld th

is a

lway

s b

e d

on

e?

Th

is is

cal

led

inlin

ing

156

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

156

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

mov

r0, #1

bl

Plus_1

mov

r4, r0

Plus_1:

add

r0, #1

bx

lr

153

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

153

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

Ref

eren

ces

for

this

cha

pter

[Pat

ters

on1

7]D

avid

A. P

atte

rso

n &

Joh

n L

. Hen

nes

syC

om

pu

ter O

rgan

izat

ion

an

d D

esig

n –

Th

e H

ard

war

e/So

ftw

are

Inte

rfac

eC

hap

ter 2

“In

stru

ctio

ns:

Lan

guag

e o

f th

e C

om

pu

ter”

AR

M e

dit

ion

, Mo

rgan

Kau

fman

n 2

017

160

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

160

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

… … … … … … … … … …

… … … … … … …

… … … … … …

157

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

157

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

mov

r0, #1

bl

Plus_1

mov

r4, r0

Plus_1:

add

r0,

#1

bx

lrlr

154

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

154

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

… … … … … … … … … …

… … … … … … …

Page 2: Functions - Australian National University

167

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

167

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

sp

mov

r0, #1

bl

Fib_Fact

mov

r3, r0

Fib_Fact:

bl

Fib

mov

r4, r0

bl

Fact

add

r0, r4

bx

lr

Fib:

bx

lr

Fact:

bx

lr

167

f

bx

lr

… w

her

e d

oes

this

lead

us?

164

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

164

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

mov

r0, #1

add

r0, #2

mov

r3, r0

… w

e n

eed

an

exa

mp

le, w

her

e a

com

pile

r

will

no

t ju

st r

emo

ve a

ll o

ur

cod

e!

161

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

161

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

(Gre

atne

ss fr

om …

) Sm

all b

egin

ning

splus_2 :: (Num a) => a -> a

plus_2 x = plus_1 $ plus_1 x

int plus2 (int x) {

return plus1 (plus1 (x));

} function Plus_2 (x : Integer) return Integer is (Plus_1 (Plus_1 (x)));

def plus2 (x):

return plus1 (plus1 (x));

pure function plus_2 (x)

int, intent (in) :: x

int :: plus_2

plus_2 = plus_1 (plus_1 (x));

end function;

function Plus_2 (x : integer) : integer;

begin

Plus_2 := Plus_1 (Plus_1 (x));

end;

168

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

168

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

mov

r0, #1

bl

Fib_Fact

mov

r3, r0

Fib_Fact:

str

lr, [sp, #-4]!

bl

Fib

mov

r4, r0

bl

Fact

add

r0, r4

ldr

lr, [sp], #4

bx

lr

Fib:

bx

lr

Fact:

bx

lr lrsp

165

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

165

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

(Gre

atne

ss fr

om …

) Sm

all b

egin

ning

sfib_fact :: (Num a) => a -> a

fib_fact x = (fib x) + (fact x)

unsigned int fibFact (unsigned int x) {

return fib (x) + fact (x);

} function Fib_Fact (x : Natural) return Natural is (Fib (x) + Fact (x));

def fibFact (x):

return fib (x) + fact (x);

pure function fib_fact (x)

int, intent (in) :: x

int :: fib_fact

fib_fact = fib (x) + fact (x);

end function;

function Fib_Fact (x : cardinal) : cardinal;

begin

Fib_Fact := Fib (x) + Fact (x);

end;

162

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

162

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

mov

r0, #1

bl

Plus_2

mov

r3, r0

Plus_2:

bl Plus_1

bl Plus_1

bx

lr

Plus_1:

add

r0, #1

bx

lr

bx

lr is u

sed

thre

e ti

mes

Wh

at is

the

valu

e o

f lr

in e

ach

cas

e?

169

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

169

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

mov

r0, #1

bl

Fib_Fact

mov

r3, r0

Fib_Fact:

str

lr, [sp, #

-4]!

bl

Fib

mov

r4, r0

bl

Fact

add

r0, r4

ldr

lr, [sp], #4

bx

lr

Fib:

bx

lr

Fact:

bx

lr lrsp

Npa

ge 1

69N

atio

nal U

nive

rsity

pa

ge 1

y

r4

… w

hat

if th

is w

as h

old

ing

som

e in

form

atio

n

at th

e ti

me

wh

en w

e w

ere

calle

d?

166

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

166

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

sp

mov

r0, #1

bl

Fib_Fact

mov

r3, r0

Fib_Fact:

bl

Fib

mov

r4, r0

bl

Fact

add

r0, r4

bx

lr

Fib:

bx

lr

Fact:

bx

lr

163

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

163

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

mov

r0, #1

bl

Plus_2

mov

r3, r0

Plus_2:

str

lr, [sp, #

-4]!

bl Plus_1

bl Plus_1

ldr

lr, [sp], #4

bx

lr

Plus_1:

add

r0, #1

bx

lr

bx

lr is u

sed

thre

e ti

mes

Wh

at is

the

valu

e o

f lr

in e

ach

cas

e?

Page 3: Functions - Australian National University

176

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

176

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

Fib_FactFact 1Fact 0 Fact 2 Fact 3

fp r4

fp fp

lr fpx (3)

lrfp x (3)

lrx (2)

fplrx (1)

fplrx (0)

fpfpfpsp

Fact:

stmdb

sp!, {fp, lr}

add

fp,

sp, #4

sub

sp,

#4

str

r0, [fp,

#-8]

cmp

r0,

#0

bne

Case_Others

mov

r0,

#1

b End_Fact

Case_Others:

sub

r0,

#1

bl

Fact

mov

r1,

r0

ldr

r0, [fp,

#-8]

mul

r0,

r1End_Fact:

add

sp,

#4

ldmia

sp!, {fp, lr}

bx

lr

Fib_Fact:

stmdb

sp!, {r4, fp, lr

}

add

fp, sp, #8

sub

sp, #4

str

r0, [f

p, #-12]

bl

Fib

mov

r4, r0

ldr

r0, [f

p, #-12]

bl

Fact

add

r0, r0, r4

add sp, #4

ldmia

sp!, {r4, fp, lr

}

bx

lr

}}

Wh

ere

is th

e la

st lr

sto

red

?

Ho

w h

igh

do

w

e st

ack?

Fact

Is Fact

ree

ntra

nt?

173

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

173

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

mov

r0, #1

bl

Fib_Fact

mov

r3, r0

Fib_Fact:

stmdb

sp!, {r4,

lr}

sub

sp, #4

str

r0, [sp]

bl

Fib

mov

r4, r0

ldr

r0, [sp]

bl

Fact

add

r0, r4

add

sp, #4

ldmia

sp!, {r4

, lr}

bx

lr

Fib_Fact

r4

splrx (3)

Fib:

bx

lr

Fact:

bx

lr

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

itypa

ge

y

Wh

ile a

dd

ress

ing

via

the

sp is

po

ssib

le, i

t may

als

o b

e

com

ple

x to

kee

p tr

ack

of,

as th

e sp

may

ch

ange

furt

her

.

170

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

170

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

mov

r0, #1

bl

Fib_Fact

mov

r3, r0

Fib_Fact:

stmdb

sp!, {r4,

lr}

bl

Fib

mov

r4, r0

bl

Fact

add

r0, r4

ldmia

sp!, {r4

, lr}

bx

lr

Fib:

bx

lr

Fact:

bx

lr

Fib_Fact

r4sp

lr

177

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

177

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

A c

om

pile

r w

ill li

kely

re

pla

ce s

uch

a r

ecu

rsio

n!

177

f487

(h

3“F

i”

27

function Fact (x : Natural) return Positive is

Fac : Positive := 1;

begin

for i in 1 .. x loop

Fac := Fac * i;

end loop;

return Fac;

end Fact;

function Fact (x : Natural) return Positive is

(if x = 0 then 1

else x * Fact (x - 1));

174

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

174

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

mov

r0, #1

bl

Fib_Fact

mov

r3, r0

Fib_Fact:

stmdb

sp!, {r4,

fp,

lr}

add

fp, sp,

#8

sub

sp, #4

str

r0, [fp, #-12]

bl

Fib

mov

r4, r0

ldr

r0, [fp, #-12]

bl

Fact

add

r0, r4

add

sp, #4

ldmia

sp!, {r4

, fp,

lr}

bx

lr

Fib_Fact

r4fp

fplrx (1)

Fib:

bx

lr

Fact:

bx

lr

2021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

174

of

y20

21 U

we

R. Z

imm

er, T

he A

ustr

alia

n N

atio

nal U

nive

rsity

page

174

y

Kee

pin

g a

refe

ren

ce to

the

star

t of t

he

Stac

k Fr

ame

for

this

fun

ctio

n (w

ith

the

fram

e-p

oin

ter

fp) m

akes

thin

gs n

eate

r

and

en

able

s st

ruct

ure

d a

cces

s to

the

dyn

amic

co

nte

xt.

171

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

171

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

mov

r0, #1

bl

Fib_Fact

mov

r3, r0

Fib_Fact:

stmdb

sp!, {r4,

lr}

bl

Fib

mov

r4, r0

bl

Fact

add

r0, r4

ldmia

sp!, {r4

, lr}

bx

lr

Fib:

bx

lr

Fact:

bx

lr

Fib_Fact

r4sp

lr

nive

rsity

pa

ge 1

7y

iitW

hat

hap

pen

s to

ou

r p

aram

eter

x (1

) d

uri

ng

the

fun

ctio

n?

178

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

178

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

A c

om

pile

r w

ill li

kely

re

pla

ce s

uch

a r

ecu

rsio

n!

unsigned int fact (unsigned int x) {

if (x == 0) return 1;

else return x * fact (x - 1);

}

178

f487

(h

3“F

i”

27

unsigned int fact (unsigned int x) {

int fac = 1;

for (i = 1, i <= x, i++) {

fac = fac * i;

}

return fac;

}

175

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

175

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

Rec

ursi

veunsigned int fib (unsigned int x) {

switch (x) {

case 0 : return 0;

case 1 : return 1;

default : return fib (x - 1) + fib (x - 2);

} }

function Fib (x : Natural) return Natural is

(case x is

when 0 => 0,

when 1 => 1,

when others => Fib (x - 1) + Fib (x - 2));

unsigned int fact (unsigned int x) {

if (x == 0) return 1;

else return x * fact (x - 1);

} function Fact (x : Natural) return Positive is

(if x = 0 then 1

else x * Fact (x - 1));

Will

ou

r st

ack

han

dlin

g w

ork

fo

r re

curs

ive

fun

ctio

ns?

172

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

172

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

mov

r0, #1

bl

Fib_Fact

mov

r3, r0

Fib_Fact:

stmdb

sp!, {r4, lr

}

sub

sp, #4

str

r0, [sp]

bl

Fib

mov

r4, r0

ldr

r0, [sp]

bl

Fact

add

r0, r4

add

sp, #4

ldmia

sp!, {r4, lr

}

bx

lr

Fib_Fact

r4

splrx (3)

Fib:

bx

lr

Fact:

bx

lr

Page 4: Functions - Australian National University

185

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

185

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

Pro

logu

e

Epilo

gue

Com

pone

nts

/ ph

ases

of a

func

tion

cal

l:•

Val

ues

(par

amet

ers)

to b

e p

asse

d to

a fu

nct

ion

.

• Lo

cal v

aria

bles

insi

de

a fu

nct

ion

.

• V

alu

es (r

esul

ts) t

o b

e re

turn

ed fr

om

a fu

nct

ion

.

So fa

r w

e: …

pas

sed

para

met

er v

alu

es in

reg

iste

rs ( r

0 - r

3).

… c

alle

d th

e fu

ncti

on (s

tore

the

retu

rn a

dd

ress

an

d ju

mp

to th

e b

egin

nin

g o

f th

e fu

nct

ion

).

… p

ushe

d th

e re

turn

ad

dre

ss, t

he

pre

vio

us

stac

k fr

ame

and

use

d r

egis

ters

( r4

…).

… c

reat

ed a

new

sta

ck fr

ame

(an

d a

dd

ress

ed a

ll lo

cal v

aria

ble

s re

lati

ve to

this

).

… g

rew

the

sta

ck s

uch

that

it c

an h

old

the

loca

l var

iab

les.

… d

one

the

calc

ulat

ions

/ope

rati

ons

bas

ed o

n th

e lo

cal v

aria

ble

s an

d s

crat

ch r

egis

ters

.

… p

asse

d re

turn

val

ues

in r

egis

ters

( r0

- r1)

.

… r

esto

red

the

stac

k po

inte

r (a

nd

thu

s d

e-al

loca

ted

all

loca

l var

iab

les)

.

… p

oppe

d th

e re

turn

ad

dre

ss, t

he

pre

vio

us

stac

k fr

ame

and

use

d r

egis

ters

( r4

…).

… ju

mpe

d ba

ck to

the

nex

t in

stru

ctio

n a

fter

the

ori

gin

al fu

nct

ion

cal

l.

… u

sed

the

retu

rn v

alue

s fo

un

d in

r0

- r1.

Wh

at h

app

ens

if th

e p

aram

eter

s o

r

retu

rn v

alu

es w

on

’t fi

t in

to r

egis

ters

?

182

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

182

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

Fib_Fact:

stmdb

sp!, {r4, fp, lr

}

add

fp, sp, #8

sub

sp, #4

str

r0, [f

p, #-12]

bl

Fib

mov

r4, r0

ldr

r0, [f

p, #-12]

bl

Fact

add

r0, r0, r4

add sp, #4

ldmia

sp!, {r4, fp, lr

}

bx

lr

tytytytyy

Fib:

stmdb sp!, {r4, fp, lr}

add fp, sp

, #8

sub sp, #4

str r0, [fp, #

-12]

cmp r0, #0

beq End_Fib

cmp r0, #1

beq End_Fib

sub r0, r0

, #1

bl Fib

mov r4, r0

ldr r0, [fp, #

-12]

sub r0, r0

, #2

bl Fib

add r0, r4

, r0

End_Fib:

add sp, #4

ldmia sp!, {r4, fp, lr}

bx lr

Fib_Fact

r4fp

fplrx (3)

179

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

179

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

Fact:

adds

r3, r0, #0

mov

r0,

#1

beq

End_Fact

Fact_Loop:

mul

r0,

r3

subs

r3,

#1

bne

Fact_Loop

End_Fact:

bx

lr

Fib_Fact:

stmdb

sp!, {r4, fp, lr

}

add

fp, sp, #8

sub

sp, #4

str

r0, [f

p, #-12]

bl

Fib

mov

r4, r0

ldr

r0, [f

p, #-12]

bl

Fact

add

r0, r0, r4

add sp, #4

ldmia

sp!, {r4, fp, lr

}

bx

lr

}}

Fib:

bx

lr

mm

er, T

he A

ustr

alia

n N

atio

nal U

nive

rsity

pa

ge

y20

21 U

we

R. Z

imm

er, T

he A

ustr

alia

n N

atio

nal U

nive

rsity

pay

Bes

ides

all

the

inlin

ing,

un

rolli

ng,

fl at

ten

ing,

etc

. :

Sta

ck o

per

atio

ns

are

still

vit

al fo

r an

y n

on

-tri

vial

pro

gram

.

Co

mp

lexi

ty?

Ru

nti

mes

?

Is Fact

ree

ntra

nt?

Fib_Fact

r4fp

fplrx (3)

186

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

186

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

Con

vent

ions

AR

M a

rchi

tect

ure

calli

ng p

ract

ice

• r0

-r3

are

use

d fo

r p

aram

eter

s.

• r0

-r1

are

use

d fo

r re

turn

val

ues

.

• r0

-r3

are

no

t exp

ecte

d to

be

inta

ct a

fter

a fu

nct

ion

cal

l…

all

oth

er r

egis

ters

are

exp

ecte

d to

be

inta

ct!

If th

ose

reg

iste

rs d

o n

ot s

uffi

ce,

ad

dit

ion

al p

aram

eter

s an

d r

esu

lts

are

pas

sed

via

the

stac

k. Co

nve

nti

on

s ar

e d

iffe

ren

t in

oth

er

arch

ite

ctu

res

(e.g

. x86

, wh

ere

par

ame

ters

are

ge

ne

rall

y p

asse

d v

ia t

he

sta

ck).

fh

Wh

y ar

e t

he

se c

on

ven

tio

ns

arch

ite

ctu

re r

ela

ted

at

all?

Th

ere

are

als

o m

em

ory

al

ign

me

nt

con

stra

ints

.

(Mo

stly

du

e t

o m

em

ory

b

us

con

stra

ints

)

183

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

183

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

Fib_Fact:

stmdb

sp!, {

r4,

fp,

lr}

add

fp, sp, #8

sub

sp, #4

str

r0, [fp,

#-12]

bl

Fib

mov

r4, r0

ldr

r0, [fp,

#-12]

bl

Fact

add

r0, r0, r4

add sp, #4

ldmia

sp!, {

r4,

fp,

lr}

bx

lr

tytytytyy

Fib:

stmdb

sp!, {

r4,

fp, lr

} add fp, sp, #8

sub sp, #4

str r0, [fp, #

-12]

cmp

r0

, #0

beq

End_Fib

cmp

r0

, #1

beq

End_Fib

sub

r0

, r0, #1

bl

Fib

mov

r4

, r0

ldr

r0

, [fp, #

-12]

sub

r0

, r0, #2

bl

Fib

add

r0

, r4, r0

End_Fib:

add

sp

, #4

ldmia

sp!, {r4, fp

, lr}

bx

lr

Fib_Fact

r4fp

fplrx (3)

p p r4,r0

4, r0

0, [fp

, #-12

]act

0, r0, r4

p, #4

p!, {r4, fp, lr}

r

Th

ere

cou

ld b

e tw

o fu

rth

er F

ib-c

alls

fo

r ea

ch c

all

to F

ib …

Wh

at w

ou

ld th

e st

ack

loo

k lik

e?

Wh

at w

ou

ld b

e th

e m

axim

al d

epth

fo

r th

e st

ack?

180

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

180

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

Fact:

adds

r3, r0

, #0

mov

r0, #1

beq

End_Fact

Fact_Loop:

mul

r0, r3

subs

r3, #1

bne

Fact_Loop

End_Fact:

bx

lr

Fib_Fact:

stmdb

sp!, {

r4,

fp,

lr}

add

fp, sp, #8

sub

sp, #4

str

r0, [fp,

#-12]

bl

Fib

mov

r4, r0

ldr

r0, [fp,

#-12]

bl

Fact

add

r0, r0, r4

add sp, #4

ldmia

sp!, {

r4,

fp,

lr}

bx

lr

}}

Fib:

bx

lr

… … bxlr

Wh

y d

o w

e sa

ve r

4?

Bu

t we

do

n’t

save

r3?

Fib_Fact

r4fp

fplrx (3)

187

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

187

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

Para

met

er p

assi

ng

Cal

l by

Acc

ess

by

cop

yb

y re

fere

nce

Info

rmat

ion

fl

ow

in

by v

alue

Para

met

er b

eco

mes

a c

on

stan

t in

sid

e th

e fu

nct

ion

or

is

cop

ied

into

a lo

cal v

aria

ble

.

by r

efer

ence

(im

mut

able

)N

o w

rite

acc

ess

is a

llow

ed

wh

ile th

e fu

nct

ion

ru

ns

(als

o

fro

m o

uts

ide

the

fun

ctio

n).

ou

t

by r

esul

tC

allin

g fu

nct

ion

exp

ects

the

par

amet

er v

alu

e to

ap

pea

r in

a

spec

ifi c

spac

e at

ret

urn

.

by r

efer

ence

(m

utab

le, n

o re

ad)

No

rea

d a

cces

s fr

om

insi

de

the

fun

ctio

n, w

rite

acc

ess

on

ret

urn

.

in

&

ou

t

by v

alue

res

ult

Para

met

er is

co

pie

d to

a lo

cal

vari

able

an

d c

op

ied

bac

k at

ret

urn

.

by r

efer

ence

(m

utab

le)

Fun

ctio

n c

an r

ead

an

d w

rite

at a

ny

tim

e. O

uts

ide

cod

e sh

all n

ot w

rite

.

184

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

184

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

Com

pone

nts

/ ph

ases

of a

func

tion

cal

l:•

Val

ues

(par

amet

ers)

to b

e p

asse

d to

a fu

nct

ion

.

• Lo

cal v

aria

bles

insi

de

a fu

nct

ion

.

• V

alu

es (r

esul

ts) t

o b

e re

turn

ed fr

om

a fu

nct

ion

.

So fa

r w

e: …

pas

sed

para

met

er v

alu

es in

reg

iste

rs ( r

0 - r

3).

… c

alle

d th

e fu

ncti

on (s

tore

the

retu

rn a

dd

ress

an

d ju

mp

to th

e b

egin

nin

g o

f th

e fu

nct

ion

).

… p

ushe

d th

e re

turn

ad

dre

ss, t

he

pre

vio

us

stac

k fr

ame

and

use

d r

egis

ters

( r4

…).

… c

reat

ed a

new

sta

ck fr

ame

(an

d a

dd

ress

ed a

ll lo

cal v

aria

ble

s re

lati

ve to

this

).

… g

rew

the

sta

ck s

uch

that

it c

an h

old

the

loca

l var

iab

les.

… d

one

the

calc

ulat

ions

/ope

rati

ons

bas

ed o

n th

e lo

cal v

aria

ble

s an

d s

crat

ch r

egis

ters

.

… p

asse

d re

turn

val

ues

in r

egis

ters

( r0

- r1)

.

… r

esto

red

the

stac

k po

inte

r (a

nd

thu

s d

e-al

loca

ted

all

loca

l var

iab

les)

.

… p

oppe

d th

e re

turn

ad

dre

ss, t

he

pre

vio

us

stac

k fr

ame

and

use

d r

egis

ters

( r4

…).

… ju

mpe

d ba

ck to

the

nex

t in

stru

ctio

n a

fter

the

ori

gin

al fu

nct

ion

cal

l.

… u

sed

the

retu

rn v

alue

s fo

un

d in

r0

- r1.

Pro

logu

e

Epilo

gue

181

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

181

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

Fact:

adds

r3, r0, #0

mov

r0, #1

beq

End_Fact

Fact_Loop:

mul

r0, r3

subs

r3, #1

bne

Fact_Loop

End_Fact:

bx

lr

Fib_Fact:

stmdb

sp!, {r4, fp

, lr}

add

fp,

sp, #8

sub

sp,

#4

str

r0, [fp, #

-12]

bl

Fib

mov

r4,

r0

ldr

r0, [fp, #

-12]

bl

Fact

add

r0,

r0, r4

add

sp, #4

ldmia

sp!, {r4, fp

, lr}

bx

lr

}}

Fib:

bx

lr

Fact:

b}}

bxlr

We

keep

a c

op

y o

f r0

her

e.

Bu

t we

do

n’t

keep

a

cop

y o

f r0

her

e!

Fib_Fact

r4fp

fplrx (3)

Page 5: Functions - Australian National University

194

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

194

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

Fib_FactFact 1Fact 0 Fact 2 Fact 3

Fact:

stmdb

sp!, {fp, lr}

add

fp,

sp, #4

sub

sp,

#4

str

r0, [fp,

#-8]

cmp

r0,

#0

bne

Case_Others

mov

r0,

#1

b End_Fact

Case_Others:

sub

r0,

#1

bl

Fact

mov

r1,

r0

ldr

r0, [fp,

#-8]

mul

r0,

r1End_Fact:

add sp

, #4

ldmia

sp!, {fp, lr}

bx

lr

Fib_Fact:

stmdb

sp!, {r4, fp, lr

}

add

fp, sp, #8

sub

sp, #4

str

r0, [f

p, #-12]

bl

Fib

mov

r4, r0

ldr

r0, [f

p, #-12]

bl

Fact

add

r0, r0, r4

add sp, #4

ldmia

sp!, {r4, fp, lr

}

bx

lr

}}

fp r4

fp fp

lr fpx (3)

lrfp x (3)

lrx (2)

fplrx (1)

fplrx (0)

fpfpfpsp

Ho

w a

bo

ut u

sin

g ca

ll b

y re

fere

nce

her

e?

191

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

191

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

Acc

ess

by

cop

yb

y re

fere

nce

Info

rmat

ion

fl

ow

in

by v

alue

Para

met

er b

eco

mes

a c

on

stan

t in

sid

e th

e fu

nct

ion

or

is

cop

ied

into

a lo

cal v

aria

ble

.

by r

efer

ence

(im

mut

able

)N

o w

rite

acc

ess

is a

llow

ed

wh

ile th

e fu

nct

ion

ru

ns

(als

o

fro

m o

uts

ide

the

fun

ctio

n).

ou

t

by r

esul

tC

allin

g fu

nct

ion

exp

ects

the

par

amet

er v

alu

e to

ap

pea

r in

a

spec

ifi c

spac

e at

ret

urn

.

by r

efer

ence

(m

utab

le, n

o re

ad)

No

rea

d a

cces

s fr

om

insi

de

the

fun

ctio

n, w

rite

acc

ess

on

ret

urn

.

in

&

ou

t

by v

alue

res

ult

Para

met

er is

co

pie

d to

a lo

cal

vari

able

an

d c

op

ied

bac

k at

ret

urn

.

by r

efer

ence

(m

utab

le)

Fun

ctio

n c

an r

ead

an

d w

rite

at a

ny

tim

e. O

uts

ide

cod

e sh

all n

ot w

rite

.

Acc

ess

Ad

aLi

mit

ed c

on

tro

l ove

r “b

y va

lue

resu

lt”.

“by

valu

e” p

aram

eter

s ar

e co

nst

ants

.

188

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

188

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

Acc

ess

by

cop

yb

y re

fere

nce

Info

rmat

ion

fl

ow

in

by v

alue

Para

met

er b

eco

mes

a c

on

stan

t in

sid

e th

e fu

nct

ion

or

is

cop

ied

into

a lo

cal v

aria

ble

.

by r

efer

ence

(im

mut

able

)N

o w

rite

acc

ess

is a

llow

ed

wh

ile th

e fu

nct

ion

ru

ns

(als

o

fro

m o

uts

ide

the

fun

ctio

n).

ou

t

by r

esul

tC

allin

g fu

nct

ion

exp

ects

the

par

amet

er v

alu

e to

ap

pea

r in

a

spec

ifi c

spac

e at

ret

urn

.

by r

efer

ence

(m

utab

le, n

o re

ad)

No

rea

d a

cces

s fr

om

insi

de

the

fun

ctio

n, w

rite

acc

ess

on

ret

urn

.

in

&

ou

t

by v

alue

res

ult

Para

met

er is

co

pie

d to

a lo

cal

vari

able

an

d c

op

ied

bac

k at

ret

urn

.

by r

efer

ence

(m

utab

le)

Fun

ctio

n c

an r

ead

an

d w

rite

at a

ny

tim

e. O

uts

ide

cod

e sh

all n

ot w

rite

.

by

cop

yy

py

yp

yy

py

by

refe

ren

ceyyy

Acc

ess

by

cop

yb

yre

fere

nce

CFu

ll co

ntr

ol o

ver

tho

se th

ree

mo

des

.

“by

valu

e” p

aram

eter

s ar

e lo

cal v

aria

ble

s.

“in

& o

ut,

by

refe

ren

ce”

syn

tact

ical

ly a

s “b

y p

oin

ter

valu

e” p

aram

eter

s.

195

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

195

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

Fact:

stmdb

sp!, {

r5, fp, lr}

add

fp, sp,

#4

mov

r5, r0

ldr

r0, [r5]

cmp

r0, #0

bne

Case_Others

mov

r0, #1

b

End_Fact

Case_Others:

sub

r0, #1

str

r0, [r5]

mov

r0, r5

bl

Fact

mov

r1, r0

ldr

r0, [r5]

mul

r0, r1

End_Fact:

mov

sp, fp

ldmia

sp!, {

r5, fp, lr}

bx

lr

Fib_Fact:

stmdb

sp!, {

r4,

fp,

lr}

add

fp, sp, #8

sub

sp, #4

str

r0, [fp,

#-12]

bl

Fib

mov

r4, r0

sub

r0, fp, #12

bl

Fact

add

r0, r0, r4

add sp, #4

ldmia

sp!, {

r4,

fp,

lr}

bx

lr

}}

tytyy

r5 h

as b

een

no

min

ated

to h

old

th

e re

fere

nce

to x

insi

de Fact

Fib_FactFact 1Fact 0 Fact 2 Fact 3

r5 r4

fp fp

fp fplr lrr5 x (0)

fplrr5fplrr5fplr

fpfpfpsp

Fact Fact Fact FFac

r5

Fact Fact Fact Fact Fact FFact Fact

r5

Fact Fact Fact Fact Fac FFac Fact

r5

Fact Fact Fact Fact Fact FFact Fact

r5

192

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

192

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

Acc

ess

by

cop

yb

y re

fere

nce

Info

rmat

ion

fl

ow

in

by v

alue

Para

met

er b

eco

mes

a c

on

stan

t in

sid

e th

e fu

nct

ion

or

is

cop

ied

into

a lo

cal v

aria

ble

.

by r

efer

ence

(im

mut

able

)N

o w

rite

acc

ess

is a

llow

ed

wh

ile th

e fu

nct

ion

ru

ns

(als

o

fro

m o

uts

ide

the

fun

ctio

n).

ou

t

by r

esul

tC

allin

g fu

nct

ion

exp

ects

the

par

amet

er v

alu

e to

ap

pea

r in

a

spec

ifi c

spac

e at

ret

urn

.

by r

efer

ence

(m

utab

le, n

o re

ad)

No

rea

d a

cces

s fr

om

insi

de

the

fun

ctio

n, w

rite

acc

ess

on

ret

urn

.

in

&

ou

t

by v

alue

res

ult

Para

met

er is

co

pie

d to

a lo

cal

vari

able

an

d c

op

ied

bac

k at

ret

urn

.

by r

efer

ence

(m

utab

le)

Fun

ctio

n c

an r

ead

an

d w

rite

at a

ny

tim

e. O

uts

ide

cod

e sh

all n

ot w

rite

.

Ass

emb

ly“B

y re

fere

nce

” se

man

tics

by

con

ven

tio

n o

nly

.

189

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

189

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

Acc

ess

by

cop

yb

y re

fere

nce

Info

rmat

ion

fl

ow

in

by v

alue

Para

met

er b

eco

mes

a c

on

stan

t in

sid

e th

e fu

nct

ion

or

is

cop

ied

into

a lo

cal v

aria

ble

.

by r

efer

ence

(im

mut

able

)N

o w

rite

acc

ess

is a

llow

ed

wh

ile th

e fu

nct

ion

ru

ns

(als

o

fro

m o

uts

ide

the

fun

ctio

n).

ou

t

by r

esul

tC

allin

g fu

nct

ion

exp

ects

the

par

amet

er v

alu

e to

ap

pea

r in

a

spec

ifi c

spac

e at

ret

urn

.

by r

efer

ence

(m

utab

le, n

o re

ad)

No

rea

d a

cces

s fr

om

insi

de

the

fun

ctio

n, w

rite

acc

ess

on

ret

urn

.

in

&

ou

t

by v

alue

res

ult

Para

met

er is

co

pie

d to

a lo

cal

vari

able

an

d c

op

ied

bac

k at

ret

urn

.

by r

efer

ence

(m

utab

le)

Fun

ctio

n c

an r

ead

an

d w

rite

at a

ny

tim

e. O

uts

ide

cod

e sh

all n

ot w

rite

.

Has

kell

On

ly c

on

tro

l ove

r in

form

atio

n fl

ow

– n

ot o

ver

acce

ss. ttteteteteteteettee

sity

page

189

of

y48

7(c

hapt

er 3

: “Fu

nctio

ns7

nooooooooo

ris

cop

ied

toa

loca

lF

tir

is c

op

ied

to a

loca

l an

d c

op

ied

bac

k at

ret

urn

.Fu

nct

ion

can

rea

d a

nd

wri

tti

me.

Ou

tsid

e co

de

shal

l n“i

n &

ou

t” p

aram

eter

s ar

e si

de-

effe

ctin

g an

d c

an

ther

efo

re n

ot e

xist

in a

pu

re fu

nct

ion

al la

ngu

age.

196

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

196

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

Fact:

stmdb

sp!, {r5, fp

, lr}

add

fp,

sp, #4

mov

r5,

r0

ldr

r0, [r5]

cmp

r0,

#0

bne

Case_Others

mov

r0,

#1

b End_Fact

Case_Others:

sub

r0,

#1

str

r0, [r5]

mov

r0,

r5

bl

Fact

mov

r1,

r0

ldr

r0, [r5]

mul

r0,

r1End_Fact:

mov

sp,

fp

ldmia

sp!, {r5, fp

, lr}

bx

lr

Fib_Fact:

stmdb

sp!, {r4, fp

, lr}

add

fp,

sp, #8

sub

sp,

#4

str

r0, [fp, #

-12]

bl

Fib

mov

r4,

r0

sub

r0,

fp, #12

bl

Fact

add

r0,

r0, r4

add

sp, #4

ldmia

sp!, {r4, fp

, lr}

bx

lr

}}

tytyy

We

turn

ed Fact

into

th

e co

nst

ant 0

.

Fib_FactFact 1Fact 0 Fact 2 Fact 3

r5 r4fp fplr lrr5 x (0)

fplrr5fplrr5fplr

fpsp

Fact Fact Fact FFac

r5

Fact Fact Fact Fact Fact FFact Fact

r5

Fact Fact Fact Fact Fac FFac Fact

r5

Fact Fact Fact Fact Fact FFact Fact

r5r5 r4

fpfp fplr lrr5 x (0

fplrr5fplrr5fplr

fpfpfpfpfpfpfpfffpfpfpfp

fp,

sp, #4

r5,

r0r0, [r5]

r0,

#0Case_Others

r0,

#1End_Fa

thers

r000,

##1#r0, [r555]

r0,

r5Fact

r1,

r0r0, [r5]

r0,

r1ct:

s

s

_Fa

ssssssssssssss

s

iiiiiiii

s

_Fa

_FaFaFac

FFFac

Fac

Facacac

_Fa

_FacacFac

FacaFac

_Fa

_FacacFacacFaFacaFac

Faacttt

s:ss:s:s:s:s:ssssss:s:s:ss:sss:sss:ss:ss:s:: 0#1

55]]]]]]]]]]]]]]]]]]]]]r1r1r1r1r1111111111r111r1r11r1r111111

fpspspspspspspspssspspspspsspsspspspspspspsspspspppp

lrp!, {r5, fpp,

lr}

lll

ct:

c iiiiiisp,

fpfpfffiiiaaaa

spspsssp!, {r5,fp

,lr}

fpct:

ispspsss

iiiiiaa W

hat

did

we

ove

rlo

ok?

193

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

193

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

Para

met

er p

assi

ng

Cal

l by

nam

e…

is c

on

cep

tual

ly a

cal

l-b

y-va

lue,

wh

ere

the

valu

e h

as n

ot b

een

cal

cula

ted

yet

.

Tech

nic

ally

a r

efer

ence

to a

fun

ctio

n is

pas

sed

an

d th

e ev

alu

atio

n

of t

his

par

amet

er (f

un

ctio

n) i

s le

ft to

the

calle

d fu

nct

ion

.

Feat

ure

s:•

Val

ues

are

on

ly e

valu

ated

if a

nd

wh

en th

ey a

re n

eed

ed.

• V

alu

es c

an c

han

ge d

uri

ng

the

life-

tim

e o

f a fu

nct

ion

(in

cas

e o

f sid

e-ef

fect

ing

fun

ctio

ns)

.

• V

alu

es c

an b

e st

ore

d o

nce

cal

cula

ted

(in

cas

e o

f sid

e-ef

fect

-fre

e fu

nct

ion

s).

Wh

ile th

is is

po

ssib

le to

a d

egre

e in

mo

st p

rogr

amm

ing

lan

guag

es …

(eve

n if

ther

e is

no

sp

ecifi

c p

assi

ng

mo

de,

yo

u c

an s

till

pas

s a

refe

ren

ce to

a fu

nct

ion

)

… it

is a

co

re c

on

cep

t fo

r fu

nct

ion

al, l

azy

eval

uat

ion

lan

guag

es, l

ike

e.g.

Has

kell,

and

it d

oes

fi n

d it

s w

ay b

ack

into

mai

nst

ream

lan

guag

es li

ke C

++

, .N

ET la

ngu

ages

or

Pyth

on

as

ano

nym

ous

func

tion

s (s

om

etim

es r

efer

red

to a

s m

-fu

nct

ion

s o

r m

-exp

ress

ion

s).

Pop

s u

p in

pro

gram

min

g la

ngu

ages

sin

ce th

e 60

’s.

190

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

190

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

Acc

ess

by

cop

yb

y re

fere

nce

Info

rmat

ion

fl

ow

in

by v

alue

Para

met

er b

eco

mes

a c

on

stan

t in

sid

e th

e fu

nct

ion

or

is

cop

ied

into

a lo

cal v

aria

ble

.

by r

efer

ence

(im

mut

able

)N

o w

rite

acc

ess

is a

llow

ed

wh

ile th

e fu

nct

ion

ru

ns

(als

o

fro

m o

uts

ide

the

fun

ctio

n).

ou

t

by r

esul

tC

allin

g fu

nct

ion

exp

ects

the

par

amet

er v

alu

e to

ap

pea

r in

a

spec

ifi c

spac

e at

ret

urn

.

by r

efer

ence

(m

utab

le, n

o re

ad)

No

rea

d a

cces

s fr

om

insi

de

the

fun

ctio

n, w

rite

acc

ess

on

ret

urn

.

in

&

ou

t

by v

alue

res

ult

Para

met

er is

co

pie

d to

a lo

cal

vari

able

an

d c

op

ied

bac

k at

ret

urn

.

by r

efer

ence

(m

utab

le)

Fun

ctio

n c

an r

ead

an

d w

rite

at a

ny

tim

e. O

uts

ide

cod

e sh

all n

ot w

rite

.

Pyth

on

All

par

amet

er a

cces

s is

do

ub

le-i

nd

irec

t ( h

andl

es).

by

refe

ren

ceA

cces

sb

… th

is h

as a

n im

pac

t on

per

form

ance

.

Page 6: Functions - Australian National University

203

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

203

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

Acc

ess

by

cop

yb

y re

fere

nce

Info

rmat

ion

fl

ow

in

by v

alue

Para

met

er b

eco

mes

a c

on

stan

t in

sid

e th

e fu

nct

ion

or

is

cop

ied

into

a lo

cal v

aria

ble

.

by r

efer

ence

(im

mut

able

)N

o w

rite

acc

ess

is a

llow

ed

wh

ile th

e fu

nct

ion

ru

ns

(als

o

fro

m o

uts

ide

the

fun

ctio

n).

ou

t

by r

esul

tC

allin

g fu

nct

ion

exp

ects

the

par

amet

er v

alu

e to

ap

pea

r in

a

spec

ifi c

spac

e at

ret

urn

.

by r

efer

ence

(m

utab

le, n

o re

ad)

No

rea

d a

cces

s fr

om

insi

de

the

fun

ctio

n, w

rite

acc

ess

on

ret

urn

.

in

&

ou

t

by v

alue

res

ult

Para

met

er is

co

pie

d to

a lo

cal

vari

able

an

d c

op

ied

bac

k at

ret

urn

.

by r

efer

ence

(m

utab

le)

Fun

ctio

n c

an r

ead

an

d w

rite

at a

ny

tim

e. O

uts

ide

cod

e sh

all n

ot w

rite

.

A

Fun

ctio

ns

One

-way

-Out

and

by-

refe

renc

eSi

de-

effe

ct-f

ree,

if n

ew m

emo

ry is

allo

cate

d o

n r

etu

rn

– ca

nn

ot b

e en

forc

ed o

n a

ssem

bly

leve

l (re

qu

ires

co

mp

iler)

.

Val

ues

hav

e to

be

pas

sed

in m

emo

ry.

200

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

200

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

Acc

ess

by

cop

yb

y re

fere

nce

Info

rmat

ion

fl

ow

in

by v

alue

Para

met

er b

eco

mes

a c

on

stan

t in

sid

e th

e fu

nct

ion

or

is

cop

ied

into

a lo

cal v

aria

ble

.

by r

efer

ence

(im

mut

able

)N

o w

rite

acc

ess

is a

llow

ed

wh

ile th

e fu

nct

ion

ru

ns

(als

o

fro

m o

uts

ide

the

fun

ctio

n).

ou

t

by r

esul

tC

allin

g fu

nct

ion

exp

ects

the

par

amet

er v

alu

e to

ap

pea

r in

a

spec

ifi c

spac

e at

ret

urn

.

by r

efer

ence

(m

utab

le, n

o re

ad)

No

rea

d a

cces

s fr

om

insi

de

the

fun

ctio

n, w

rite

acc

ess

on

ret

urn

.

in

&

ou

t

by v

alue

res

ult

Para

met

er is

co

pie

d to

a lo

cal

vari

able

an

d c

op

ied

bac

k at

ret

urn

.

by r

efer

ence

(m

utab

le)

Fun

ctio

n c

an r

ead

an

d w

rite

at a

ny

tim

e. O

uts

ide

cod

e sh

all n

ot w

rite

.

A

Fun

ctio

ns

One

-way

and

by-

copy

Tho

se a

re s

ide-

effe

ct-f

ree

and

hen

ce th

e re

sult

ing

scen

ario

s ar

e ea

sy to

an

alys

e.

Co

pyi

ng

larg

e d

ata

stru

ctu

res

mig

ht b

e ti

me

con

sum

ing

or

infe

asib

le.

Val

ues

can

be

pas

sed

in r

egis

ters

.

197

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

197

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

Fact:

stmdb

sp!, {r5,

fp, lr}

add

fp,

sp, #4

mov

r5,

r0

ldr

r0, [r5]

cmp

r0,

#0

bne

Case_Others

mov

r0,

#1

b End_Fact

Case_Others:

sub

r0,

#1

str

r0, [r5]

mov

r0,

r5

bl

Fact

mov

r1,

r0

ldr

r0, [r5]

mul

r0,

r1End_Fact:

mov

sp,

fp

ldmia

sp!, {r5,

fp, lr}

bx

lr

Fib_Fact:

stmdb

sp!, {r4, fp, lr

}

add

fp, sp, #8

sub

sp, #4

str

r0, [f

p, #-12]

bl

Fib

mov

r4, r0

sub

r0, fp, #12

bl

Fact

add

r0, r0, r4

add sp, #4

ldmia

sp!, {r4, fp, lr

}

bx

lr

tytyy

p,Fib

db!

{4

fmdb

sp!, {r4, fp,lr

dfp,sp, #8

bsp,#4

rr0, [fp, #-12]

}}

Wh

at is

the

valu

e o

f x d

uri

ng

on

e ex

ecu

tio

n o

f Fact?

Fib_FactFact 1Fact 0 Fact 2 Fact 3

r5 r4

fp fp

fp fplr lrr5 x (0)

fplrr5fplrr5fplr

fpfpfpsp

Fact Fact Fact Fac Facc

r5

Fact Fact Fact Fact Fact Fac Fact Fact

r5

Fact Fact Fact Fact Fac Fac Fact Fac

r5

Fact Fact Fact Fact Fact Fac Fact Ft

r5

204

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

204

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

Acc

ess

by

cop

yb

y re

fere

nce

Info

rmat

ion

fl

ow

in

by v

alue

Para

met

er b

eco

mes

a c

on

stan

t in

sid

e th

e fu

nct

ion

or

is

cop

ied

into

a lo

cal v

aria

ble

.

by r

efer

ence

(im

mut

able

)N

o w

rite

acc

ess

is a

llow

ed

wh

ile th

e fu

nct

ion

ru

ns

(als

o

fro

m o

uts

ide

the

fun

ctio

n).

ou

t

by r

esul

tC

allin

g fu

nct

ion

exp

ects

the

par

amet

er v

alu

e to

ap

pea

r in

a

spec

ifi c

spac

e at

ret

urn

.

by r

efer

ence

(m

utab

le, n

o re

ad)

No

rea

d a

cces

s fr

om

insi

de

the

fun

ctio

n, w

rite

acc

ess

on

ret

urn

.

in

&

ou

t

by v

alue

res

ult

Para

met

er is

co

pie

d to

a lo

cal

vari

able

an

d c

op

ied

bac

k at

ret

urn

.

by r

efer

ence

(m

utab

le)

Fun

ctio

n c

an r

ead

an

d w

rite

at a

ny

tim

e. O

uts

ide

cod

e sh

all n

ot w

rite

.

A

Fun

ctio

ns

One

-way

-In

and

by-

refe

renc

e

Sid

e-ef

fect

-fre

e –

can

no

t be

enfo

rced

on

ass

emb

ly le

vel (

req

uir

es c

om

pile

r).

No

dat

a h

as to

be

rep

licat

ed.

Val

ues

hav

e to

be

pas

sed

in m

emo

ry.

201

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

201

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

Acc

ess

by

cop

yb

y re

fere

nce

Info

rmat

ion

fl

ow

in

by v

alue

Para

met

er b

eco

mes

a c

on

stan

t in

sid

e th

e fu

nct

ion

or

is

cop

ied

into

a lo

cal v

aria

ble

.

by r

efer

ence

(im

mut

able

)N

o w

rite

acc

ess

is a

llow

ed

wh

ile th

e fu

nct

ion

ru

ns

(als

o

fro

m o

uts

ide

the

fun

ctio

n).

ou

t

by r

esul

tC

allin

g fu

nct

ion

exp

ects

the

par

amet

er v

alu

e to

ap

pea

r in

a

spec

ifi c

spac

e at

ret

urn

.

by r

efer

ence

(m

utab

le, n

o re

ad)

No

rea

d a

cces

s fr

om

insi

de

the

fun

ctio

n, w

rite

acc

ess

on

ret

urn

.

in

&

ou

t

by v

alue

res

ult

Para

met

er is

co

pie

d to

a lo

cal

vari

able

an

d c

op

ied

bac

k at

ret

urn

.

by r

efer

ence

(m

utab

le)

Fun

ctio

n c

an r

ead

an

d w

rite

at a

ny

tim

e. O

uts

ide

cod

e sh

all n

ot w

rite

.

A

Fun

ctio

ns

Two

-way

and

by-

copy

Still

sid

e-ef

fect

-fre

e w

ith

in th

e fu

nct

ion

(bu

t no

t on

the

ou

tsid

e).

Pote

nti

ally

mo

re c

on

ven

ien

t as

mem

ory

sp

ace

can

be

reu

sed

.

Val

ues

can

be

pas

sed

in r

egis

ters

.

198

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

198

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

Para

met

er p

assi

ng

Cal

l by

Acc

ess

by

cop

yb

y re

fere

nce

Info

rmat

ion

fl

ow

in

by v

alue

Para

met

er b

eco

mes

a c

on

stan

t in

sid

e th

e fu

nct

ion

or

is

cop

ied

into

a lo

cal v

aria

ble

.

by r

efer

ence

(im

mut

able

)N

o w

rite

acc

ess

is a

llow

ed

wh

ile th

e fu

nct

ion

ru

ns

(als

o

fro

m o

uts

ide

the

fun

ctio

n).

ou

t

by r

esul

tC

allin

g fu

nct

ion

exp

ects

the

par

amet

er v

alu

e to

ap

pea

r in

a

spec

ifi c

spac

e at

ret

urn

.

by r

efer

ence

(m

utab

le, n

o re

ad)

No

rea

d a

cces

s fr

om

insi

de

the

fun

ctio

n, w

rite

acc

ess

on

ret

urn

.

in

&

ou

t

by v

alue

res

ult

Para

met

er is

co

pie

d to

a lo

cal

vari

able

an

d c

op

ied

bac

k at

ret

urn

.

by r

efer

ence

(m

utab

le)

Fun

ctio

n c

an r

ead

an

d w

rite

at a

ny

tim

e. O

uts

ide

cod

e sh

all n

ot w

rite

.

of 4

87(c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)7

f487

(h

3

Yet w

e u

sed

this

mo

de

eN

o r

ead

acce

ss fr

om

insi

de

the

))

eby

ref

eren

ce (

mut

able

, no

read

)N

df

iid

thW

e sh

ou

ld h

ave

use

d e

ith

er o

f th

ose

mo

des

205

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

205

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

Gen

eric

Sta

ck-F

ram

eLe

t th

ere

be

som

e (g

lob

al) d

ata

on

the

stac

k.

Stac

k-B

ase

(SB

) is

a st

atic

ad

dre

ss,

alw

ays

po

inti

ng

to th

e …

wel

l.

Stac

k-Po

inte

r (S

P) p

oin

ts to

the

curr

ent t

op

of t

he

stac

k.

page

205

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

7f

hf

h

(Glo

bal)

vari

able

sSBSP

©20

21U

we

RZ

imm

erTh

eA

ustr

alia

nN

atio

nalU

nive

rsityy

©20

21

(Glo

bal)

vari

able

sSBSP

Glo

bal

var

iab

les

can

als

o

be

sto

red

so

mep

lace

els

e.

202

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

202

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

Acc

ess

by

cop

yb

y re

fere

nce

Info

rmat

ion

fl

ow

in

by v

alue

Para

met

er b

eco

mes

a c

on

stan

t in

sid

e th

e fu

nct

ion

or

is

cop

ied

into

a lo

cal v

aria

ble

.

by r

efer

ence

(im

mut

able

)N

o w

rite

acc

ess

is a

llow

ed

wh

ile th

e fu

nct

ion

ru

ns

(als

o

fro

m o

uts

ide

the

fun

ctio

n).

ou

t

by r

esul

tC

allin

g fu

nct

ion

exp

ects

the

par

amet

er v

alu

e to

ap

pea

r in

a

spec

ifi c

spac

e at

ret

urn

.

by r

efer

ence

(m

utab

le, n

o re

ad)

No

rea

d a

cces

s fr

om

insi

de

the

fun

ctio

n, w

rite

acc

ess

on

ret

urn

.

in

&

ou

t

by v

alue

res

ult

Para

met

er is

co

pie

d to

a lo

cal

vari

able

an

d c

op

ied

bac

k at

ret

urn

.

by r

efer

ence

(m

utab

le)

Fun

ctio

n c

an r

ead

an

d w

rite

at a

ny

tim

e. O

uts

ide

cod

e sh

all n

ot w

rite

.

A

Fun

ctio

ns

Two

-way

and

by-

refe

renc

e

Sid

e-ef

fect

ing

and

par

ticu

lar

care

is r

equ

ired

as

mu

ltip

le e

nti

ties

co

uld

wri

te o

n th

is.

No

dat

a h

as to

be

rep

licat

ed.

Val

ues

hav

e to

be

pas

sed

in m

emo

ry.

199

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

199

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

Acc

ess

by

cop

yb

y re

fere

nce

Info

rmat

ion

fl

ow

in

by v

alue

Para

met

er b

eco

mes

a c

on

stan

t in

sid

e th

e fu

nct

ion

or

is

cop

ied

into

a lo

cal v

aria

ble

.

by r

efer

ence

(im

mut

able

)N

o w

rite

acc

ess

is a

llow

ed

wh

ile th

e fu

nct

ion

ru

ns

(als

o

fro

m o

uts

ide

the

fun

ctio

n).

ou

t

by r

esul

tC

allin

g fu

nct

ion

exp

ects

the

par

amet

er v

alu

e to

ap

pea

r in

a

spec

ifi c

spac

e at

ret

urn

.

by r

efer

ence

(m

utab

le, n

o re

ad)

No

rea

d a

cces

s fr

om

insi

de

the

fun

ctio

n, w

rite

acc

ess

on

ret

urn

.

in

&

ou

t

by v

alue

res

ult

Para

met

er is

co

pie

d to

a lo

cal

vari

able

an

d c

op

ied

bac

k at

ret

urn

.

by r

efer

ence

(m

utab

le)

Fun

ctio

n c

an r

ead

an

d w

rite

at a

ny

tim

e. O

uts

ide

cod

e sh

all n

ot w

rite

.

A

Whe

n to

use

wha

t?

Page 7: Functions - Australian National University

212

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

212

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

Stat

ic v

s. d

ynam

ic li

nks

function a (x : Integer) return Integer is

function b (y : Integer) return Integer is (x + y);

function c (z : Integer) return Integer is (b (z));

begin

return c (x);

end a;

a :: Integer -> Integer

a x = c x

where

b :: Integer -> Integer

b y = x + y

c :: Integer -> Integer

c z = b z

Th

e dy

nam

ic a

nd

sta

tic

link

for

function

c a

re b

oth

function a.

209

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

209

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

Gen

eric

Sta

ck-F

ram

eFu

nct

ion

s (i

n p

rogr

amm

ing

lan

guag

es) h

ave

a co

nte

xt.

E.g

. th

e su

rro

un

din

g fu

nct

ion

or

the

ho

stin

g o

bje

ct.

The

calle

r kn

ow

s th

is c

on

text

an

d p

rovi

des

it.

page

209

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

7f

hf

h

(Glo

bal)

vari

able

sSBSP

Para

met

ers

Con

text

(sta

tic) l

ink

©20

21U

we

RZ

imm

erTh

eA

ustr

alia

nN

atio

nalU

nive

rsityy

©20

21

(Glo

bal)

vari

able

sSBSP

Para

met

ers

Con

text

(sta

tic) l

ink

So

me

lan

guag

es w

ill n

ot h

ave

a co

nte

xt

by

def

ault

, lik

e C

or

Ass

emb

ly.

(gn

u C

exp

and

s th

e C

sta

nd

ard

an

d p

rovi

des

it th

ou

gh)

206

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

206

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

Gen

eric

Sta

ck-F

ram

eTh

e cu

rren

t co

de

pre

par

ed to

cal

l a fu

nct

ion

: P

ush

par

amet

ers

on

the

stac

k.

Wo

rks

for

any

dat

a si

ze (u

nle

ss th

e st

ack

ove

rfl o

ws)

an

d p

aram

eter

pas

sin

g m

od

e.

page

206

of 4

87(c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

7f

hf

h

(Glo

bal)

vari

able

sSBSP

Para

met

ers

©20

21U

we

RZ

imm

erTh

eA

ustr

alia

nN

atio

nalU

nive

rsityy

©20

21U

we

RZ

imm

erTh

eA

ustr

alia

nN

atio

nalU

nive

rsityy

©20

21

(Glo

bal)

vari

able

sSBSP

Para

met

ers

a

Typ

es, s

tora

ge s

tru

ctu

res

and

p

assi

ng

mo

des

hav

e to

be

agre

ed

up

on

bet

wee

n c

alle

r an

d c

alle

e.

213

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

213

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

Stat

ic v

s. d

ynam

ic li

nks

function a (x : Integer) return Integer is

function b (y : Integer) return Integer is (x + y);

function c (z : Integer) return Integer is (b (z));

begin

return c (x);

end a;

a :: Integer -> Integer

a x = c x

where

b :: Integer -> Integer

b y = x + y

c :: Integer -> Integer

c z = b z

Dyn

amic

link

(pri

or

fram

e)

Th

e ca

ller

of f

unction b

is function c.

Hen

ce e

xcep

tio

ns

rais

ed in

b a

re

han

dle

d fi

rst i

n b

, th

en in

c, a

nd

then

a.

Th

e dy

nam

ic a

nd

sta

tic

link

for

function

c a

re b

oth

function a.

210

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

210

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

Gen

eric

Sta

ck-F

ram

eTh

e ca

ller

also

pro

vid

es a

ref

eren

ce to

its

ow

n s

tack

fram

e.

Th

is b

uild

s a

linea

r ch

ain

of c

alls

thro

ugh

the

stac

k.

Will

be

use

d e

.g. f

or

deb

ugg

ing

(sta

ck tr

ace)

an

d e

xcep

tio

n p

rop

agat

ion

.

page

210

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

7f

hf

h

(Glo

bal)

vari

able

sSBSP

Para

met

ers

Con

text

(sta

tic) l

ink

Prio

r fr

ame

(dyn

amic

) lin

k

©20

21U

we

RZ

imm

erTh

eA

ustr

alia

nN

atio

nalU

nive

rsityy

©20

21

(Glo

bal)

vari

able

sSBSP

Para

met

ers

Con

text

(sta

tic) l

ink

Prio

r fr

ame

(dyn

amic

) lin

k

Th

e st

atic

an

d d

ynam

ic li

nk

mig

ht

be

iden

tica

l in

so

me

case

s.

207

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

207

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

Gen

eric

Sta

ck-F

ram

eTh

e cu

rren

t co

de

pre

par

ed to

cal

l a fu

nct

ion

: P

ush

par

amet

ers

on

the

stac

k.

Wo

rks

for

any

dat

a si

ze (u

nle

ss th

e st

ack

ove

rfl o

ws)

an

d p

aram

eter

pas

sin

g m

od

e.

page

207

of 4

87(c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

7f

hf

h

(Glo

bal)

vari

able

sSBSP

Para

met

ers

©20

21U

we

RZ

imm

erTh

eA

ustr

alia

nN

atio

nalU

nive

rsityy

©20

21U

we

RZ

imm

erTh

eA

ustr

alia

nN

atio

nalU

nive

rsityy

©20

21

(Glo

bal)

vari

able

sSBSP

Para

met

ers

Typ

es, s

tora

ge s

tru

ctu

res

and

p

assi

ng

mo

des

hav

e to

be

agre

ed

up

on

bet

wee

n c

alle

r an

d c

alle

e.

So

lved

if it

’s th

e sa

me

lan

guag

e, c

om

pile

r an

d p

rogr

am.

If t

he

lan

guag

es o

r th

e co

mp

ilers

are

dif

fere

nt,

then

sta

nd

ard

s w

ill b

e re

qu

ired

.

214

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

214

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

Stat

ic v

s. d

ynam

ic li

nks

function a (x : Integer) return Integer is

function b (y : Integer) return Integer is (x + y);

function c (z : Integer) return Integer is (b (z));

begin

return c (x);

end a;

a :: Integer -> Integer

a x = c x

where

b :: Integer -> Integer

b y = x + y

c :: Integer -> Integer

c z = b z

Dyn

amic

link

(pri

or

fram

e)

Th

e ca

ller

of f

unction b

is function c.

Stat

ic li

nk (c

on

text

)

Th

e co

nte

xt fo

r function

b is

function a.

page

214

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)7

page

214

of48

7(c

hapt

er3:

“Fun

ctio

ns”

upto

page

232)

7

Hen

ce b

can

acc

ess x

(bu

t no

t z).

Hen

ce e

xcep

tio

ns

rais

ed in

b a

re

han

dle

d fi

rst i

n b

, th

en in

c, a

nd

then

a.

Th

e dy

nam

ic a

nd

sta

tic

link

for

function

c a

re b

oth

function a.

211

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

211

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

Stat

ic v

s. d

ynam

ic li

nks

function a (x : Integer) return Integer is

function b (y : Integer) return Integer is (x + y);

function c (z : Integer) return Integer is (b (z));

begin

return c (x);

end a;

a :: Integer -> Integer

a x = c x

where

b :: Integer -> Integer

b y = x + y

c :: Integer -> Integer

c z = b z

208

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

208

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

Gen

eric

Sta

ck-F

ram

eFu

nct

ion

s (i

n p

rogr

amm

ing

lan

guag

es) h

ave

a co

nte

xt.

E.g

. th

e su

rro

un

din

g fu

nct

ion

or

the

ho

stin

g o

bje

ct.

The

calle

r kn

ow

s th

is c

on

text

an

d p

rovi

des

it.

page

208

of 4

87(c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

7f

hf

h

(Glo

bal)

vari

able

sSBSP

Para

met

ers

Con

text

(sta

tic) l

ink

©20

21U

we

RZ

imm

erTh

eA

ustr

alia

nN

atio

nalU

nive

rsityy

©20

21

(Glo

bal)

vari

able

sSBSP

Para

met

ers

Con

text

(sta

tic) l

ink

Page 8: Functions - Australian National University

221

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

221

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

Gen

eric

Sta

ck-F

ram

eTh

e n

ext f

un

ctio

n c

all w

ill p

rod

uce

the

nex

t sta

ck fr

ame.

Var

iab

les

and

par

amet

ers

fro

m th

e co

nte

xt s

tay

visi

ble

(v

ia th

e ch

ain

of s

tati

c lin

ks).

page

221

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

7f

hf

h

(Glo

bal)

vari

able

sSB

Para

met

ers

Con

text

(sta

tic) l

ink

Prio

r fr

ame

(dyn

amic

) lin

kR

etur

n ad

dres

s

Cal

ler

Cal

lee

Save

d re

sour

ces

FPSPLo

cal v

aria

bles

©20

21U

we

RZ

imm

erTh

eA

ustr

alia

nN

atio

nalU

nive

rsityy

(Glo

bal)

vari

able

sSBSP

Para

met

ers

Con

text

(sta

tic) l

ink

FP

Loca

l var

iabl

es

Para

met

ers

Con

text

(sta

tic) l

ink

Prio

r fr

ame

(dyn

amic

) lin

kR

etur

n ad

dres

s

Loca

l var

iabl

es

Cal

ler

Cal

lee

218

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

218

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

Gen

eric

Sta

ck-F

ram

eA

Fra

me

Poin

ter

(FP)

is e

stab

lish

ed a

t th

e b

ou

nd

-ar

y b

etw

een

the

calle

r an

d th

e ca

llee.

Up

war

ds

fro

m th

e FP

: dat

a fr

om

this

fun

ctio

n.

Do

wn

war

ds

fro

m th

e FP

: d

ata

pro

vid

ed b

y th

e p

revi

ou

s fu

nct

ion

.

page

218

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

7f

hf

h

(Glo

bal)

vari

able

sSB

Para

met

ers

Con

text

(sta

tic) l

ink

Prio

r fr

ame

(dyn

amic

) lin

kR

etur

n ad

dres

s

Cal

ler

Cal

lee

SPSa

ved

reso

urce

sFP

©20

21U

we

RZ

imm

erTh

eA

ustr

alia

nN

atio

nalU

nive

rsityy

©20

21

(Glo

bal)

vari

able

sSBSP

Para

met

ers

Con

text

(sta

tic) l

ink

Prio

r fr

ame

(dyn

amic

) lin

kR

etur

n ad

dres

s

Cal

ler

Cal

lee

Save

d re

sour

ces

FP

Save

d r

eso

urc

es a

re fo

r in

stan

ce r

egis

ters

wh

ich

the

calle

e is

pla

nn

ing

to u

se.

215

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

215

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

Gen

eric

Sta

ck-F

ram

eTh

e ca

ller

also

pro

vid

es a

ref

eren

ce to

its

ow

n s

tack

fram

e.

Th

is b

uild

s a

linea

r ch

ain

of c

alls

thro

ugh

the

stac

k.

Will

be

use

d e

.g. f

or

deb

ugg

ing

(sta

ck tr

ace)

an

d e

xcep

tio

n p

rop

agat

ion

.

page

215

of 4

87(c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

7f

hf

h

(Glo

bal)

vari

able

sSBSP

Para

met

ers

Con

text

(sta

tic) l

ink

Prio

r fr

ame

(dyn

amic

) lin

k

©20

21U

we

RZ

imm

erTh

eA

ustr

alia

nN

atio

nalU

nive

rsityy

©20

21

(Glo

bal)

vari

able

sSBSP

Para

met

ers

Con

text

(sta

tic) l

ink

Prio

r fr

ame

(dyn

amic

) lin

k

222

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

222

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

Gen

eric

Sta

ck-F

ram

eTh

e n

ext f

un

ctio

n c

all w

ill p

rod

uce

the

nex

t sta

ck fr

ame.

Var

iab

les

and

par

amet

ers

fro

m th

e co

nte

xt s

tay

visi

ble

(v

ia th

e ch

ain

of s

tati

c lin

ks).

page

222

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

7f

hf

h

(Glo

bal)

vari

able

sSB

Para

met

ers

Con

text

(sta

tic) l

ink

Prio

r fr

ame

(dyn

amic

) lin

kR

etur

n ad

dres

s

Cal

ler

Cal

lee

Save

d re

sour

ces

FPSPLo

cal v

aria

bles

©20

21U

we

RZ

imm

erTh

eA

ustr

alia

nN

atio

nalU

nive

rsityy

(Glo

bal)

vari

able

sSBSP

Para

met

ers

Con

text

(sta

tic) l

ink

Ret

urn

addr

ess

Save

d re

sour

ces

FP

Loca

l var

iabl

es

Para

met

ers

Con

text

(sta

tic) l

ink

Prio

r fr

ame

(dyn

amic

) lin

kR

etur

n ad

dres

s

Loca

l var

iabl

es

Ret

urn

addr

ess

SaSaSavvve

dre

sour

ed r

esou

red

reso

urce

sce

sce

s

Cal

ler

Cal

lee

Loca

l var

iab

les

can

on

ly

be

add

ed to

the

curr

entl

y ex

ecu

tin

g fu

nct

ion

.

219

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

219

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

Gen

eric

Sta

ck-F

ram

eLo

cal v

aria

ble

s ar

e al

loca

ted

(by

mo

vin

g th

e st

ack

po

inte

r).

Loca

l var

iab

les

can

be

of a

ny

size

or

stru

ctu

re,

un

less

the

stac

k o

verfl

ow

s.

Th

e co

mp

lete

s a

new

sta

ck fr

ame.

page

219

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

7f

hf

h

(Glo

bal)

vari

able

sSB

Para

met

ers

Con

text

(sta

tic) l

ink

Prio

r fr

ame

(dyn

amic

) lin

kR

etur

n ad

dres

s

Cal

ler

Cal

lee

Save

d re

sour

ces

FPSPLo

cal v

aria

bles

©20

21U

we

RZ

imm

erTh

eA

ustr

alia

nN

atio

nalU

nive

rsityy

©20

21

(Glo

bal)

vari

able

sSBSP

Para

met

ers

Con

text

(sta

tic) l

ink

Prio

r fr

ame

(dyn

amic

) lin

kR

etur

n ad

dres

s

Cal

ler

Cal

lee

Save

d re

sour

ces

FP

Loca

l var

iabl

es

216

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

216

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

Gen

eric

Sta

ck-F

ram

eTh

e la

st it

em to

be

sto

red

bef

ore

han

din

g o

ver

con

tro

lis

the

add

ress

of t

he

follo

win

g in

stru

ctio

n.

Th

e co

ntr

ol fl

ow

can

late

r re

turn

to th

is a

dd

ress

.

page

216

of 4

87(c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

7f

hf

h

(Glo

bal)

vari

able

sSBSP

Para

met

ers

Con

text

(sta

tic) l

ink

Prio

r fr

ame

(dyn

amic

) lin

kR

etur

n ad

dres

s

©20

21U

we

RZ

imm

erTh

eA

ustr

alia

nN

atio

nalU

nive

rsityy

©20

21

(Glo

bal)

vari

able

sSBSP

Para

met

ers

Con

text

(sta

tic) l

ink

Prio

r fr

ame

(dyn

amic

) lin

kR

etur

n ad

dres

s

223

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

223

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

Gen

eric

Sta

ck-F

ram

eTh

e n

ext f

un

ctio

n c

all w

ill p

rod

uce

the

nex

t sta

ck fr

ame.

No

te w

hic

h v

aria

ble

s an

d p

aram

eter

s ar

e vi

sib

le.

page

223

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

7f

hf

h

(Glo

bal)

vari

able

sSB

Para

met

ers

Con

text

(sta

tic) l

ink

Prio

r fr

ame

(dyn

amic

) lin

kR

etur

n ad

dres

s

Cal

ler

Cal

lee

Save

d re

sour

ces

FPSPLo

cal v

aria

bles

©20

21U

we

RZ

imm

erTh

eA

ustr

alia

nN

atio

nalU

nive

rsityy

©20

21

(Glo

bal)

vari

able

s

Para

met

ers

Prio

r fr

ame

(dyn

amic

) lin

kR

etur

n ad

dres

s

Loca

l var

iabl

es

FPSP

Para

met

ers

Con

text

(sta

tic) l

ink

Prio

r fr

ame

(dyn

amic

) lin

kR

etur

n ad

dres

s

Loca

l var

iabl

es

Para

met

ers

Con

text

(sta

tic) l

ink

ynam

ic) l

ink

Ret

urn

addr

ess

Loca

l var

iabl

es

Save

d re

sour

ces

Cal

ler

Cal

lee

Save

d re

sour

ces

Par

PPam

eter

s

Con

text

(sta

tic) l

ink

Prio

r fr

ame

(dyn

amic

) lin

kR

etur

n ad

dres

s

Loca

l vLo

calv

Loca

lvar

iabl

esar

iabl

esar

iabl

es

ynam

ic) l

ink

Ret

urn

addr

ess

SaSaSaSavvvve

dre

sour

ed r

esou

red

reso

ured

reso

urce

sce

sce

sce

s

Con

text

(sta

tic) l

ink

SB220

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

220

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

Gen

eric

Sta

ck-F

ram

eLo

cal v

aria

ble

s ar

e al

loca

ted

(by

mo

vin

g th

e st

ack

po

inte

r).

Loca

l var

iab

les

can

be

of a

ny

size

or

stru

ctu

re,

un

less

the

stac

k o

verfl

ow

s.

Th

e co

mp

lete

s a

new

sta

ck fr

ame.

page

220

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

7f

hf

h

(Glo

bal)

vari

able

sSB

Para

met

ers

Con

text

(sta

tic) l

ink

Prio

r fr

ame

(dyn

amic

) lin

kR

etur

n ad

dres

s

Cal

ler

Cal

lee

Save

d re

sour

ces

FPSPLo

cal v

aria

bles

©20

21U

we

RZ

imm

erTh

eA

ustr

alia

nN

atio

nalU

nive

rsityy

©20

21

(Glo

bal)

vari

able

sSBSP

Para

met

ers

Con

text

(sta

tic) l

ink

Prio

r fr

ame

(dyn

amic

) lin

kR

etur

n ad

dres

s

Cal

ler

Cal

lee

Save

d re

sour

ces

FP

Loca

l var

iabl

es

Wh

ile th

is fu

nct

ion

is

exec

uti

ng,

loca

l var

iab

les

can

sti

ll b

e ad

ded

.

Han

dy,

if e

.g. t

he

size

of a

loca

l va

riab

le is

no

t yet

det

erm

ined

w

hen

the

fun

ctio

n s

tart

s.

217

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

217

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

Gen

eric

Sta

ck-F

ram

eC

on

tro

l is

han

ded

ove

r to

the

calle

e.

Th

e In

stru

ctio

n P

oin

ter

(IP)

, so

met

imes

als

o c

alle

d

Pro

gram

Co

un

ter

(PC

) is

chan

ged

to a

new

ad

dre

ss.

Op

erat

ion

s fr

om

her

e ar

e in

the

con

tro

l of t

he

calle

e.

page

217

of 4

87(c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

7f

hf

h

(Glo

bal)

vari

able

sSBSP

Para

met

ers

Con

text

(sta

tic) l

ink

Prio

r fr

ame

(dyn

amic

) lin

kR

etur

n ad

dres

s

Cal

ler

Cal

lee

©20

21U

we

RZ

imm

erTh

eA

ustr

alia

nN

atio

nalU

nive

rsityy

©20

21

(Glo

bal)

vari

able

sSBSP

Para

met

ers

Con

text

(sta

tic) l

ink

Prio

r fr

ame

(dyn

amic

) lin

kR

etur

n ad

dres

s

Cal

ler

Cal

lee

Page 9: Functions - Australian National University

230

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

230

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

Gen

eric

Sta

ck-F

ram

e –

Hea

p

By

usi

ng

an o

ut,

by-

refe

ren

ce p

aram

eter

, th

e lin

k to

the

new

ly a

lloca

ted

m

emo

ry a

rea

is k

ept.

… a

nd

a lo

cal v

aria

ble

in th

e ca

llin

g fu

nct

ion

can

kee

p th

is li

nk.

©20

21U

we

RZ

imm

erTh

eA

ustr

alia

nN

atio

nalU

nive

rsityy

(Glo

bal)

vari

able

sSBSP

Para

met

ers

Con

text

(sta

tic) l

ink

FP

Loca

l var

iabl

es

Para

met

ers

Con

text

(sta

tic) l

ink

Prio

r fr

ame

(dyn

amic

) lin

kR

etur

n ad

dres

s

Loca

l var

iabl

es

Heap

memory

Ho

w to

kee

p a

ny

mem

ory

al

loca

tio

n a

fter

fun

ctio

n r

etu

rn?

227

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

227

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

Gen

eric

Sta

ck-F

ram

e –

Cal

lee

Prologue:

; Save all registers which are needed

; inside this function to the stack

; Establish a new frame pointer

; while potentially saving the previous fp

; Allocate/identify space for local variables

; Potentially initialize local variables

Op

erat

ion

s, w

hic

h w

ill u

se

loca

l an

d c

on

text

var

iab

les

and

par

amet

ers

(via

the

FP(s

))

Epilogue:

; Potentially restore the prior frame pointer

; Restore the stack to its state at entry

Ret

urn

fro

m fu

nct

ion

©20

21U

we

RZ

imm

erTh

eA

ustr

alia

nN

atio

nalU

nive

rsityy

(Glo

bal)

vari

able

s

Para

met

ers

Prio

r fr

ame

(dyn

amic

) lin

kR

etur

n ad

dres

s

Loca

l var

iabl

es

FPSP

Para

met

ers

Con

text

(sta

tic) l

ink

Prio

r fr

ame

(dyn

amic

) lin

kR

etur

n ad

dres

s

Loca

l var

iabl

es

Para

met

ers

Con

text

(sta

tic) l

ink

Prio

r fr

ame

(dyn

amic

) lin

kR

etur

n ad

dres

s

Loca

l var

iabl

es

Save

d re

sour

ces

Cal

ler

Cal

lee

Save

d re

sour

ces

Par

PPam

eter

s

Con

text

(sta

tic) l

ink

Prio

r fr

ame

(dyn

amic

) lin

kR

etur

n ad

dres

s

Loca

l vLo

calv

Loca

lvar

iabl

esar

iabl

esar

iabl

es

Prio

r fr

ame

(dyn

amic

) lin

kR

etur

n ad

dres

s

SaSaSaSavvvve

dre

sour

ed r

esou

red

reso

ured

reso

urce

sce

sce

sce

s

Con

text

(sta

tic) l

ink

SB224

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

224

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

Gen

eric

Sta

ck-F

ram

eTh

e n

ext f

un

ctio

n c

all w

ill p

rod

uce

the

nex

t sta

ck fr

ame.

No

te w

hic

h v

aria

ble

s an

d p

aram

eter

s ar

e vi

sib

le.

page

224

of 4

87(c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

7f

hf

h

(Glo

bal)

vari

able

sSB

Para

met

ers

Con

text

(sta

tic) l

ink

Prio

r fr

ame

(dyn

amic

) lin

kR

etur

n ad

dres

s

Cal

ler

Cal

lee

Save

d re

sour

ces

FPSPLo

cal v

aria

bles

©20

21U

we

RZ

imm

erTh

eA

ustr

alia

nN

atio

nalU

nive

rsityy

(Glo

bal)

vari

able

s

Para

met

ers

Prio

r fr

ame

(dyn

amic

) lin

kR

etur

n ad

dres

s

Loca

l var

iabl

es

FPSP

Para

met

ers

Con

text

(sta

tic) l

ink

Prio

r fr

ame

(dyn

amic

) lin

kR

etur

n ad

dres

s

Loca

l var

iabl

es

Para

met

ers

Con

text

(sta

tic) l

ink

Prio

r fr

ame

(dyn

amic

) lin

kR

etur

n ad

dres

s

Loca

l var

iabl

es

Save

d re

sour

ces

Cal

ler

Cal

lee

Save

d re

sour

ces

Par

PPam

eter

s

Con

text

(sta

tic) l

ink

Prio

r fr

ame

(dyn

amic

) lin

kR

etur

n ad

dres

s

Loca

l vLo

calv

Loca

lvar

iabl

esar

iabl

esar

iabl

es

Prio

r fr

ame

(dyn

amic

) lin

kR

etur

n ad

dres

s

SaSaSaSavvvve

dre

sour

ed r

esou

red

reso

ured

reso

urce

sce

sce

sce

s

Con

text

(sta

tic) l

ink

SB

Acc

essi

ng

the

con

text

like

th

at c

an b

e in

effi

cien

t! C

om

pile

rs m

ay c

ho

se o

ther

m

ech

anis

m (e

.g. d

ispl

ays,

w

hic

h m

ake

all c

on

text

le

vels

acc

essi

ble

at o

nce

).

231

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

231

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

Gen

eric

Sta

ck-F

ram

e –

Hea

p

By

usi

ng

an o

ut,

by-

refe

ren

ce p

aram

eter

, th

e lin

k to

the

new

ly a

lloca

ted

m

emo

ry a

rea

is k

ept.

… a

nd

a lo

cal v

aria

ble

in th

e ca

llin

g fu

nct

ion

can

kee

p th

is li

nk.

©20

21U

we

RZ

imm

erTh

eA

ustr

alia

nN

atio

nalU

nive

rsityy

(Glo

bal)

vari

able

sSBSP

Para

met

ers

Con

text

(sta

tic) l

ink

Ret

urn

addr

ess

Save

d re

sour

ces

FP

Loca

l var

iabl

es

Para

met

ers

Con

text

(sta

tic) l

ink

Prio

r fr

ame

(dyn

amic

) lin

kR

etur

n ad

dres

s

Loca

l var

iabl

es

Ret

urn

addr

ess

SaSaSavvve

dre

sour

ed r

esou

red

reso

urce

sce

sce

s

Heap

memory

Ho

w to

kee

p a

ny

mem

ory

al

loca

tio

n a

fter

fun

ctio

n r

etu

rn?

pag

page

231

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

p7

Wh

en to

dea

lloca

te th

ou

gh?

• G

arb

age

colle

ctio

n (J

ava)

?

• Sm

art p

oin

ters

(C+

+)?

• R

efer

ence

ow

ner

ship

s (R

ust

)?

• Sc

op

ed p

oin

ters

/ st

ora

ge p

oo

ls (A

da)

?

228

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

228

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

Gen

eric

Sta

ck-F

ram

e –

Hea

p

©20

21U

we

RZ

imm

erTh

eA

ustr

alia

nN

atio

nalU

nive

rsityy

©20

21

(Glo

bal)

vari

able

s

Para

met

ers

Prio

r fr

ame

(dyn

amic

) lin

kR

etur

n ad

dres

s

Loca

l var

iabl

es

FPSP

Para

met

ers

Con

text

(sta

tic) l

ink

Prio

r fr

ame

(dyn

amic

) lin

kR

etur

n ad

dres

s

Loca

l var

iabl

es

Para

met

ers

Con

text

(sta

tic) l

ink

Prio

r fr

ame

(dyn

amic

) lin

kR

etur

n ad

dres

s

Loca

l var

iabl

es

Save

d re

sour

ces

Cal

ler

Cal

lee

Save

d re

sour

ces

Par

PPam

eter

s

Con

text

(sta

tic) l

ink

Prio

r fr

ame

(dyn

amic

) lin

kR

etur

n ad

dres

s

Loca

l vLo

calv

Loca

lvar

iabl

esar

iabl

esar

iabl

es

Prio

r fr

ame

(dyn

amic

) lin

kR

etur

n ad

dres

s

SaSaSaSavvvve

dre

sour

ed r

esou

red

reso

ured

reso

urce

sce

sce

sce

s

Con

text

(sta

tic) l

ink

SB

Ho

w to

kee

p a

ny

mem

ory

al

loca

tio

n a

fter

fun

ctio

n r

etu

rn?

225

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

225

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

Gen

eric

Sta

ck-F

ram

e

page

225

of 4

87(c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

7f

hf

h

(Glo

bal)

vari

able

sSB

Para

met

ers

Con

text

(sta

tic) l

ink

Prio

r fr

ame

(dyn

amic

) lin

kR

etur

n ad

dres

s

Cal

ler

Cal

lee

Save

d re

sour

ces

FPSPLo

cal v

aria

bles

©20

21U

we

RZ

imm

erTh

eA

ustr

alia

nN

atio

nalU

nive

rsityy

©20

21

(Glo

bal)

vari

able

s

Para

met

ers

Prio

r fr

ame

(dyn

amic

) lin

kR

etur

n ad

dres

s

Loca

l var

iabl

es

FPSP

Para

met

ers

Con

text

(sta

tic) l

ink

Prio

r fr

ame

(dyn

amic

) lin

kR

etur

n ad

dres

s

Loca

l var

iabl

es

Para

met

ers

Con

text

(sta

tic) l

ink

Prio

r fr

ame

(dyn

amic

) lin

kR

etur

n ad

dres

s

Loca

l var

iabl

es

Save

d re

sour

ces

Cal

ler

Cal

lee

Save

d re

sour

ces

Par

PPam

eter

s

Con

text

(sta

tic) l

ink

Prio

r fr

ame

(dyn

amic

) lin

kR

etur

n ad

dres

s

Loca

l vLo

calv

Loca

lvar

iabl

esar

iabl

esar

iabl

es

Prio

r fr

ame

(dyn

amic

) lin

kR

etur

n ad

dres

s

SaSaSaSavvvve

dre

sour

ed r

esou

red

reso

ured

reso

urce

sce

sce

sce

s

Con

text

(sta

tic) l

ink

SB

Ho

w fa

st /

com

ple

x is

the

allo

cati

on a

nd

dea

lloca

tion

of l

oca

l var

iab

les

and

par

amet

ers

on

the

stac

k?

232

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

232

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

Func

tio

ns•

Fram

ewor

k

• R

etu

rn a

dd

ress

• R

elat

ive

add

ress

ing

• Pa

ram

eter

pas

sing

mod

es a

nd m

echa

nism

s

• C

op

y ve

rsu

s re

fere

nce

• In

form

atio

n fl

ow

dir

ecti

on

s

• La

te e

valu

atio

n

• St

ackf

ram

es

• St

atic

an

d d

ynam

ic li

nks

• Pa

ram

eter

s

• Lo

cal v

aria

ble

s

Sum

mar

y

229

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

229

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

Gen

eric

Sta

ck-F

ram

e –

Hea

p

By

usi

ng

an o

ut,

by-

refe

ren

ce p

aram

eter

, th

e lin

k to

the

new

ly a

lloca

ted

m

emo

ry a

rea

is k

ept.

©20

21U

we

RZ

imm

erTh

eA

ustr

alia

nN

atio

nalU

nive

rsityy

©20

21

(Glo

bal)

vari

able

s

Para

met

ers

Prio

r fr

ame

(dyn

amic

) lin

kR

etur

n ad

dres

s

Loca

l var

iabl

es

FPSP

Para

met

ers

Con

text

(sta

tic) l

ink

Prio

r fr

ame

(dyn

amic

) lin

kR

etur

n ad

dres

s

Loca

l var

iabl

es

Para

met

ers

Con

text

(sta

tic) l

ink

Prio

r fr

ame

(dyn

amic

) lin

kR

etur

n ad

dres

s

Loca

l var

iabl

es

Save

d re

sour

ces

Save

d re

sour

ces

Par

PPam

eter

s

Con

text

(sta

tic) l

ink

Prio

r fr

ame

(dyn

amic

) lin

kR

etur

n ad

dres

s

Loca

l vLo

calv

Loca

lvar

iabl

esar

iabl

esar

iabl

es

Prio

r fr

ame

(dyn

amic

) lin

kR

etur

n ad

dres

s

SaSaSaSavvvve

dre

sour

ed r

esou

red

reso

ured

reso

urce

sce

sce

sce

s

Con

text

(sta

tic) l

ink

SB

Heap

memory

Ho

w to

kee

p a

ny

mem

ory

al

loca

tio

n a

fter

fun

ctio

n r

etu

rn?

226

Fun

ctio

ns

© 2

021

Uw

e R

. Zim

mer

, The

Aus

tral

ian

Nat

iona

l Uni

vers

ity

page

226

of 4

87 (c

hapt

er 3

: “Fu

nctio

ns”

up to

pag

e 23

2)

Gen

eric

Sta

ck-F

ram

e –

Cal

ler

Pre_Call:

; Allocate/identify space for the parameters

; Copy the in and in-out

; parameters to this space

; Potentially provide links

; Provide a return address (“Post_Call”)

; (usually implicit with the call itself)

Cal

l th

e fu

nct

ion

Post_Call:

; Copy the out and in-out parameters

; to local variables or registers

; Potentially restore the frame pointer

; Restore the stack to its previous state

; (if the stack has been used)

©20

21U

we

RZ

imm

erTh

eA

ustr

alia

nN

atio

nalU

nive

rsityy

©20

21

(Glo

bal)

vari

able

s

Para

met

ers

Prio

r fr

ame

(dyn

amic

) lin

kR

etur

n ad

dres

s

Loca

l var

iabl

es

FPSP

Para

met

ers

Con

text

(sta

tic) l

ink

Prio

r fr

ame

(dyn

amic

) lin

kR

etur

n ad

dres

s

Loca

l var

iabl

es

Para

met

ers

Con

text

(sta

tic) l

ink

Prio

r fr

ame

(dyn

amic

) lin

kR

etur

n ad

dres

s

Loca

l var

iabl

es

Save

d re

sour

ces

Cal

ler

Cal

lee

Save

d re

sour

ces

Par

PPam

eter

s

Con

text

(sta

tic) l

ink

Prio

r fr

ame

(dyn

amic

) lin

kR

etur

n ad

dres

s

Loca

l vLo

calv

Loca

lvar

iabl

esar

iabl

esar

iabl

es

Prio

r fr

ame

(dyn

amic

) lin

kR

etur

n ad

dres

s

SaSaSaSavvvve

dre

sour

ed r

esou

red

reso

ured

reso

urce

sce

sce

sce

s

Con

text

(sta

tic) l

ink

SB