Presentation is loading. Please wait.

Presentation is loading. Please wait.

Program Compilation and Execution. Today’s Objectives Explain why runtime stack needed for C Explain why runtime stack needed for C Draw logical division.

Similar presentations


Presentation on theme: "Program Compilation and Execution. Today’s Objectives Explain why runtime stack needed for C Explain why runtime stack needed for C Draw logical division."— Presentation transcript:

1 Program Compilation and Execution

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

3 Logical Memory Layout Stack Heap DYNAMICDYNAMIC Static Space Currently not in use

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

5 Where Are the Variables? Stack Heap DYNAMICDYNAMIC Space for 13 ints – pointed to by counts i; roll; counts Static Space Num “There were %d rolls of %d\n” Currently not in use

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

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

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

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

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

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


Download ppt "Program Compilation and Execution. Today’s Objectives Explain why runtime stack needed for C Explain why runtime stack needed for C Draw logical division."

Similar presentations


Ads by Google