Upload
barrie-wells
View
212
Download
0
Embed Size (px)
Citation preview
Program Compilation Program Compilation and Executionand Execution
Today’s ObjectivesToday’s Objectives
Explain why runtime stack needed for Explain why runtime stack needed for CC
Draw logical division of memory into Draw logical division of memory into stack, heapstack, heap
Compare and contrast stack and heapCompare and contrast stack and heap List variables stored in heap; List variables stored in heap; stack; neitherstack; neither
Describe 2 different function Describe 2 different function calling conventionscalling conventions
Logical Memory LayoutLogical Memory Layout
Stack
Heap
D
Y
N
A
M
I
C
Static Space
Currently not in useCurrently not in use
Dice.cDice.cint NUM = 100000;int NUM = 100000;main()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 + 2roll = 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);}}
Where Are the Where Are the Variables?Variables?
Stack
Heap
D
Y
N
A
M
I
CSpace for 13 ints – pointed to by counts
i; roll; counts
Static SpaceNum
“There were %d rolls of %d\n”
Currently not in useCurrently not in use
Runtime StackRuntime Stack
Activation record for mainActivation 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
Support for Function Support for Function CallsCalls
Call usually simple, but setup notCall usually simple, but setup not call printfcall printf what comes before and after call?what comes before and after call?
Support recursion?Support recursion? No --- simple, use static spaceNo --- simple, use static space Yes --- runtime stack of activation Yes --- runtime stack of activation record(s)record(s)
Return value(s)Return value(s)
Activation Record Activation Record InformationInformation
ParametersParameters LocalsLocals Compiler TemporariesCompiler Temporaries Register Save/RestoreRegister Save/Restore Previous Activation RecordPrevious Activation Record Previous ScopePrevious Scope Anything else?Anything else?
A Function Call A Function Call ConventionConvention
fp and sp defined by assemblerfp and sp defined by assembler Caller function (in order)Caller function (in order)
push parameters on stack, in reversepush parameters on stack, in reverse push fppush fp fp = sp - 4fp = sp - 4 call calleecall 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 functionsp = sp + size of function execute functionexecute function sp = sp – size of functionsp = sp – size of function returnreturn
An Activation RecordAn 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 …
Reg save …
sp Next stack location – beyond the stack
Function Call Function Call Convention (2)Convention (2)
Alpha conventionsAlpha conventions Parameters passed in registers, r16 - Parameters passed in registers, r16 - r24 or d16 - d24r24 or d16 - d24
Return in r0 (or d0)Return in r0 (or d0) Registers r1 - r8, d1 - d8 are NOT Registers r1 - r8, d1 - d8 are NOT saved across function call invocations saved across function call invocations (caller save)(caller save)
Registers r25 - r31 and d25 - d31 ARE Registers r25 - r31 and d25 - d31 ARE saved across function call invocations saved across function call invocations (callee save)(callee save)