-
Notifications
You must be signed in to change notification settings - Fork 2
Stack
Jan Noha edited this page Nov 23, 2015
·
6 revisions
- The stack is dynamic and it grows upwards.
- Each stack element is a pointer (4 or 8 bytes wide).
- Alignment is therefore always the same and we use indices for addressing.
- Each method has its stack frame.
- We use these pointers to work with the stack:
- AP - points to the first argument given by the caller
- FP - points to the beginning of method's stack frame
- SP - points to the stack top (after the last element pushed or 0 when the stack is empty)
Layout of instance method stack-frame
----------------------- AP+0 | arg 0 | <-- AP | arg 1 | | ... | | arg n-1 | ----------------------- AP+n | instance ptr (this) | ----------------------- | return address | ----------------------- | caller AP | | caller FP | ----------------------- FP+0 | exception info ptr | <-- FP FP+1 | local var 0 | | local var 1 | | ... | FP+n | local var n-1 | ----------------------- | | <-- SP v v
Layout of class (static) method stack-frame
----------------------- AP+0 | arg 0 | <-- AP | arg 1 | | ... | | arg n-1 | ----------------------- AP+n | class ptr | ----------------------- | return address | ----------------------- | caller AP | | caller FP | ----------------------- FP+0 | exception info ptr | <-- FP FP+1 | local var 0 | | local var 1 | | ... | FP+n | local var n-1 | ----------------------- | | <-- SP v v
CALLE
- PUSH the return address (instruction after the call)
- PUSH AP
- AP = SP - arg_cnt - 3
- PUSH FP
- FP = SP
- SP = SP + locals_cnt + 1 (reserve space for ex. info ptr and locals)
- JMP to method entry point
In case of non-external call the VM pushes the value of stack[AP+n] onto the stack before making the call.
In case of external call the class/instance pointer is already pushed by the preceding operation.
RET
- POP retVal
- SP = AP
- retAddr = stack[FP-3]
- AP = stack[FP-2]
- FP = stack[FP-1]
- PUSH retVal
- JMP to retAddr