CSE 3302Lecture 15: Functions19 Oct 2010Nate NystromUniversity of Texas at ArlingtonAgenda• Function implementation• Evaluation strategiesRuntime organization• The address space of a process is divided into segments:• code (aka text)• global data• heap• stack• Details depend on the OS232-1stacksp ➨sp ➨⬇⬆heapdatacode0An aside on bits• Recent operating systems (e.g., Windows 7, OS X 10.6) are 64-bit• What are the advantages?• What are the disadvantages?Call stack• Each thread in a process maintains a call stack•Stack frame (aka activation record) contains:•function parameters•saved frame pointer of caller•saved return address of caller•local variables•Stack frame pushed when calling a function•Stack frame popped on returnStack framefp - frame pointerpoints to current framesp - stack pointerpoints to current top of stacksaved ip - saved return addresssaved fp - saved frame pointer of calleraka dynamic linksometimes also: static link - pointer to frame oflexically enclosing function (this is notused by most languages nowadays)callee frame can access args from caller frame...arg 1arg 2...arg nfp ➨saved fpsaved iplocal 1local 2...local narg 1arg 2...arg nsp ➨⬇callee framecaller frameCall• Before call:• caller allocates callee stack frame• caller evaluates and stores parameters in registers or on stack• caller stores return address in register or on stack• Prologue:• callee stores frame pointer in stack• callee set fp to be top of stack• callee allocates local variables (bumping sp)Return• Epilogue:• callee stores return value in register or on stack• callee restores fp• callee jumps to return address• After return:• caller copies return value out• caller deallocates callee frameCall stackdef g(y, z) = y + zdef f(x, y) = { val a = 4 val b = g(x+a, 2) b + y} val x = 3f(x, 1)➨1288saved ip-fp ➨1284saved fp-1280x-sp ➨1276127212681264126012561252124812441240Call stackdef g(y, z) = y + zdef f(x, y) = { val a = 4 val b = g(x+a, 2) b + y} val x = 3f(x, 1)1288saved ip-fp ➨1284saved fp-1280x3sp ➨1276127212681264126012561252124812441240➨Call stackdef g(y, z) = y + zdef f(x, y) = { val a = 4 val b = g(x+a, 2) b + y} val x = 3f(x, 1)1288saved ip-fp ➨1284saved fp-1280x31276param 131272param 21sp ➨12681264126012561252124812441240➨Call stackdef g(y, z) = y + zdef f(x, y) = { val a = 4 val b = g(x+a, 2) b + y} val x = 3f(x, 1)1288saved ip-fp ➨1284saved fp-1280x31276param 131272param 211268saved ip<after f(x, 1)>1264saved fp1284sp ➨126012561252124812441240➨Call stackdef g(y, z) = y + zdef f(x, y) = { val a = 4 val b = g(x+a, 2) b + y} val x = 3f(x, 1)1288saved ip-1284saved fp-1280x31276param 131272param 211268saved ip<after f(x, 1)>fp ➨1264saved fp12841260a-1256b-sp ➨1252124812441240➨Call stackdef g(y, z) = y + zdef f(x, y) = { val a = 4 val b = g(x+a, 2) b + y} val x = 3f(x, 1)1288saved ip-1284saved fp-1280x31276param 131272param 211268saved ip<after f(x, 1)>fp ➨1264saved fp12841260a-1256b-sp ➨1252124812441240➨Call stackdef g(y, z) = y + zdef f(x, y) = { val a = 4 val b = g(x+a, 2) b + y} val x = 3f(x, 1)1288saved ip-1284saved fp-1280x31276param 131272param 211268saved ip<after f(x, 1)>fp ➨1264saved fp12841260a41256b-sp ➨1252124812441240➨Call stackdef g(y, z) = y + zdef f(x, y) = { val a = 4 val b = g(x+a, 2) b + y} val x = 3f(x, 1)1288saved ip-1284saved fp-1280x31276param 131272param 211268saved ip<after f(x, 1)>fp ➨1264saved fp12841260a41256b-1252param 171248param 22sp ➨12441240➨Call stackdef g(y, z) = y + zdef f(x, y) = { val a = 4 val b = g(x+a, 2) b + y} val x = 3f(x, 1)1288saved ip-1284saved fp-1280x31276param 131272param 211268saved ip<after f(x, 1)>fp ➨1264saved fp12841260a41256b-1252param 171248param 221244saved ip<after g(x+a,2)>1240saved fp1264➨Call stackdef g(y, z) = y + zdef f(x, y) = { val a = 4 val b = g(x+a, 2) b + y} val x = 3f(x, 1)1288saved ip-1284saved fp-1280x31276param 131272param 211268saved ip<after f(x, 1)>1264saved fp12841260a41256b-1252param 171248param 221244saved ip<after g(x+a,2)>fp ➨1240saved fp1264➨Call stackdef g(y, z) = y + zdef f(x, y) = { val a = 4 val b = g(x+a, 2) b + y} val x = 3f(x, 1)1288saved ip-1284saved fp-1280x31276param 131272param 211268saved ip<after f(x, 1)>fp ➨1264saved fp12841260a41256b-sp ➨1252124812441240➨Call stackdef g(y, z) = y + zdef f(x, y) = { val a = 4 val b = g(x+a, 2) b + y} val x = 3f(x, 1)1288saved ip-1284saved fp-1280x31276param 131272param 211268saved ip<after f(x, 1)>fp ➨1264saved fp12841260a41256b9sp ➨1252124812441240➨Call stackdef g(y, z) = y + zdef f(x, y) = { val a = 4 val b = g(x+a, 2) b + y} val x = 3f(x, 1)1288saved ip-fp ➨1284saved fp-1280x3sp ➨1276127212681264126012561252124812441240➨Tail calls• Recursive functions very common in functional languages• Tail call:• call is last operation before return• Can optimize:• before pushing new stack frame, pop the current frame• avoids explosive stack growth• essential in functional languages since they use lots of recursionTail callsdef fact(n) = { if (n <= 1) 1 else fact(n-1)*n}?Tail callsdef fact(n) = { if (n <= 1) 1 else fact(n-1)*n}No.after return from fact(n-1),result is multiplied with nTail callsdef fact(n) = { if (n <= 1) 1 else n*fact(n-1)}?Tail callsdef fact(n) = { if (n <= 1) 1 else n*fact(n-1)}No.after return from fact(n-1),result is multiplied with nTail callsdef fact(n) = fact2(n, 1)def fact2(n, acc) = { if (n <= 1) acc else fact2(n-1, acc*n)}Yes.Tail callsdef fact(n) = fact2(n, 1)def fact2(n, acc) = { if (n <= 1) acc else fact2(n-1, acc*n)}Compiler can rewrite as a loop:def fact2(n, acc) = { while (n > 1) { n = n-1 acc = acc*n } acc}Tail callsdef even(n) = { if (n == 0) true else odd(n-1)}def odd(n) = { if (n == 1) false else even(n-1)}Can’t (easily) rewrite as a loop,but can do tail call optimization.Yes.Tail call optimization• Compiler can optimize tail calls as follows:• first pop the current stack frame• push the arguments to the call• push the return address (== the return address of the caller)• make the call• If making a tail call to the same function, can just overwrite the arguments and jump to the function entrypointWithout TCOdef fact(n) = fact2(n, 1)def fact2(n, acc) = { if (n <= 1) acc
View Full Document