Mechanisms in Procedures
- Passing control
- To beggining of procedure code
- Back to return point
- Passing data
- Procedure arguments
- Return value
- Memory management
- Allocate during procedure execution
- Deallocate upon return
- Mechanisms all implemented with machine instructions
- x86-64 implemention of a procedure uses only those mechanisms required
Procedures
- Stack Structure
- Calling Conventions
- Passing control
- Passing data
- Managing local data
- Illustration of Recursion
x86-64 Stack
- Region of memory managed with stack discipline
- Grows toward lower address
- Register %rsp contains lowest stack address
- address of “top” element
- Operation: Push and Pop
Procedure Control Flow
- Use stack to support procedure call and return
- Procedure call: call label
- Push return address on stack
- Jump to label
- Return address:
- Address of the next instruction right after call
- Example from disassembly
- Procedure return: ret
- Pop address from stack
- Jump to address
Procedure Data Flow (ABI rule)
- Registers
- First 6 arguments: %rdi %rsi %rdx %rcx %r8 %r9
- Return value: %rax
- Only allocate stack space when needed
Stack-Based Languages
- Languages that support recursion
- C, Pascal, Java
- Code must be “Reentrant”
- Multiple simultaneous instantiations of single procedure
- Need some place to store state of each instantiation
- Arguments
- Local variables
- Return pointer
- Stack discipline
- State for given procedure needed for limited time
- From when called to when return
- Callee returns before caller does
- State for given procedure needed for limited time
- Stack allocated in Frames
- state for single procedure instantiation
Stack Frames
- Contents
- Return information
- Local storage (if needed)
- Temporary space (if needed)
- Management
- Space allocated when enter procedure
- “Set-up” code
- Includes push by call instruction
- Deallocated when return
- “Finish” code
- Includes pop by ret instruction
- Space allocated when enter procedure
Register Saving Conventions
- “Caller Saved”
- Caller saves temporary values in its frame before the call
- “Callee Saved”
- Callee saves temporary values in its frame before using
- Callee restores them before returning to caller
x86-64 Linux Register Usage
- %rax
- Return value
- Also caller-saved
- Can be modified by procedure
- %rdi, …, %r9
- Arguments
- Also caller-saved
- Can be modified by procedure
- %r10, %r11
- Caller-saved
- Can be modified by procedure
- %rbx, %r12, %r13, %r14
- Callee-saved
- Callee must save & restore
- %rbp
- Callee-saved
- Callee must save & restore
- May be used as frame pointer
- Can mix & match
- %rsp
- Special form of callee save
- Restored to original value upon exit from procedure
Observations About Recursion
- Handled Without Special Consideration
- Stack frames mean that each function call has private storage
- Saved registers & local variables
- Saved return pointer
- Register saving conventions prevent one function call from corrupting another’s call
- Unless the C code explicitly does so
- Stack discipline follows call/ return pattern
- Stack frames mean that each function call has private storage
- Also works for mutual recursion
- P calls Q; Q calls P
x86-64 Procedure Summary
- Important Points
- Stack is the right data structure for procedure call/ return
- If P calls Q, then Q returns before P
- Stack is the right data structure for procedure call/ return
- Recursion (& mutual recursion) handled by normal calling conventions
- Can safely store values in local stack frame and in callee-saved registers
- Put function arguments at top of stack
- Result return in %rax
- Pointers are addresses of values
- On stack or global