Presentation is loading. Please wait.

Presentation is loading. Please wait.

Activation Records and Function Calls

Similar presentations


Presentation on theme: "Activation Records and Function Calls"— Presentation transcript:

1 Activation Records and Function Calls

2 Today’s Objectives Explain why runtime stack needed for C
Draw logical division of memory into stack, heap Compare and contrast stack and heap List variables stored in heap; stack; neither Describe 2 different function calling conventions Implement function calls and activation records in your CTran compiler

3 Fortran Calling Requirements
Where should variables be stored? Globals (common blocks) ? Locals ? Basically two possible areas Data Code

4 What about Recursion? Not a problem for FORTRAN
What makes recursion a problem? Need to have MULTIPLE copies of local variables, one set per function in execution at any one time. Dynamic memory?

5 Logical Memory Layout Stack Heap Static Space D Y N A M I C
Currently not in use Currently not in use Heap Static Space

6 Dice.c int NUM = 100000; main() { int i, roll, *counts;
counts = (int *) malloc (13 * sizeof(int)); for (i = 0; i < 13; i++) counts[i] = 0; for ( i = 0; i < NUM; i++) roll = rand() % 6 + rand() %6 + 2 counts[roll]++; for (i = 2; i < 13; i++) printf(“There were %d rolls of %d\n”, counts[i],i); }

7 Where Are the Variables?
Stack D Y N A M I C i; roll; counts Currently not in use Currently not in use Space for 13 ints – pointed to by counts Heap Num “There were %d rolls of %d\n” Static Space

8 Runtime Stack Activation record for Hanoi; 1 2 3 1
Activation Record for Main Activation record for main

9 Support for Function Calls
Call usually simple, but setup not call printf what comes before and after call? Support recursion? No --- simple, use static space Yes --- runtime stack of activation record(s) Return value(s)

10 Solution? A contract between “architecture” and “compiler/OS”
One we’ll (ok I’ll) choose particularly useful for C

11 Activation Record Information
Parameters Locals Compiler Temporaries Register Save/Restore Previous Activation Record Previous Scope Anything else?

12 A Function Call Convention
fp and sp defined by assembler Caller function (in order) push parameters on stack, in reverse push fp fp = sp - 4 call callee fp = 0 (fp) sp = sp – (size of arguments + 4) Callee function (in order) sp = sp + size of function execute function sp = sp – size of function return

13 An Activation Record sp fp Next stack location – beyond the stack
Reg save … Reg save … Reg save … Temps … 8(fp) – local 2 4(fp) – local 1 fp 0(fp) – old fp -4(fp) – parameter 1 -8(fp) – parameter 2 -12(fp) – parameter 3

14 Function Call Convention (2)
Alpha conventions Parameters passed in registers, r16 - r24 or d16 - d24 Return in r0 (or d0) Registers r1 - r8, d1 - d8 are NOT saved across function call invocations (caller save) Registers r25 - r31 and d25 - d31 ARE saved across function call invocations (callee save)


Download ppt "Activation Records and Function Calls"

Similar presentations


Ads by Google