1. Activation records, also known as stack frames, contain information about the execution of methods.
2. They are placed on the call stack and include the method's local variables, partial results, and return address.
3. As methods are invoked, new activation records are pushed onto the stack and popped off when the method returns.
4. The Java Virtual Machine uses a stack-based design where operands are pushed onto the stack, the operation is performed, and results are left on the stack or in local variables.
29. Code Pattern
dynamic method call
caller
• push object
• push parameters left-to-right
• call method
virtual machine on call
• allocate space (frame data, operand stack, local variables)
• store frame data (data pointer, return address, exception table)
• store parameters as local variables
• dynamic dispatch
• point pc to method code
Activation Records 27
30. Code Pattern
return from method call
callee
• parameters in local variables
• leave result on operand stack
• return to caller
Activation Records 28
31. Code Pattern
return from method call
callee
• parameters in local variables
• leave result on operand stack
• return to caller
virtual machine on return
• push result on caller’s operand stack
• point pc to return address
• destroy frame
Activation Records 28
63. Registers
x86 family
general purpose registers
• accumulator AX - arithmetic operations
• counter CX - shift/rotate instructions, loops
• data DX - arithmetic operations, I/O
• base BX - pointer to data
• stack pointer SP, base pointer BP - top and base of stack
• source SI, destination DI - stream operations
Activation Records 33
64. Registers
x86 family
general purpose registers
• accumulator AX - arithmetic operations
• counter CX - shift/rotate instructions, loops
• data DX - arithmetic operations, I/O
• base BX - pointer to data
• stack pointer SP, base pointer BP - top and base of stack
• source SI, destination DI - stream operations
special purpose registers
• segments SS, CS, DS, ES, FS, GS
• flags EFLAGS
Activation Records 33
65. Stack and Stack Frames
stack
• temporary storage
• grows from high to low memory addresses
• starts at SS
Activation Records 34
66. Stack and Stack Frames
stack
• temporary storage
• grows from high to low memory addresses
• starts at SS
stack frames
• return address
• local variables
• parameters
• stack base: BP
• stack top: SP
Activation Records 34
67. Calling Conventions
CDECL
caller
• push parameters right-to-left on the stack
• clean-up stack after call
callee
• save old BP
• initialise new BP
• save registers
• return result in AX
• restore registers
• restore BP
Activation Records 35
68. Calling Conventions
CDECL
caller push 21
push 42
• push parameters right-to-left on the stack call
add
_f
ESP 8
• clean-up stack after call
callee
• save old BP
• initialise new BP
• save registers
• return result in AX
• restore registers
• restore BP
Activation Records 35
69. Calling Conventions
CDECL
caller push 21
push 42
• push parameters right-to-left on the stack call
add
_f
ESP 8
• clean-up stack after call
callee
• save old BP push EBP
• initialise new BP mov
mov
EBP
EAX
ESP
[EBP + 8]
• save registers mov EDX [EBP + 12]
• return result in AX
add
pop
EAX
EBP
EDX
• restore registers ret
• restore BP
Activation Records 35
70. Calling Conventions
STDCALL
caller
• push parameters right-to-left on the stack
callee
• save old BP
• initialise new BP
• save registers
• return result in AX
• restore registers
• restore BP
Activation Records 36
71. Calling Conventions
STDCALL
caller push 21
push 42
• push parameters right-to-left on the stack call _f@8
callee
• save old BP
• initialise new BP
• save registers
• return result in AX
• restore registers
• restore BP
Activation Records 36
72. Calling Conventions
STDCALL
caller push 21
push 42
• push parameters right-to-left on the stack call _f@8
callee
• save old BP
• initialise new BP push EBP
• save registers mov
mov
EBP
EAX
ESP
[EBP + 8]
• return result in AX mov EDX [EBP + 12]
• restore registers
add
pop
EAX
EBP
EDX
• restore BP ret 8
Activation Records 36
73. Calling Conventions
FASTCALL
caller
• passes parameters in registers
• pushes additional parameters right-to-left on the stack
callee
• save old BP, initialise new BP
• save registers
• return result in AX
• restore registers
• restore BP
• cleans up the stack
Activation Records 37
74. Calling Conventions
FASTCALL
caller mov ECX 21
mov EDX 42
• passes parameters in registers call @f@8
• pushes additional parameters right-to-left on the stack
callee
• save old BP, initialise new BP
• save registers
• return result in AX
• restore registers
• restore BP
• cleans up the stack
Activation Records 37
75. Calling Conventions
FASTCALL
caller mov ECX 21
mov EDX 42
• passes parameters in registers call @f@8
• pushes additional parameters right-to-left on the stack
callee
• save old BP, initialise new BP push EBP
• save registers mov
mov
EBP ESP
EAX ECX
• return result in AX add EAX EDX
• restore registers
pop
ret
EBP
• restore BP
• cleans up the stack
Activation Records 37
78. Optimisations
reasons
• code overhead
• execution overhead
inlining
• replace calls by body of the procedure
• source code level
Activation Records 39
79. Optimisations
reasons
• code overhead
• execution overhead
inlining
• replace calls by body of the procedure
• source code level
tail recursion
• replace recursive calls by loops or jumps
• source or machine code level
Activation Records 39
84. Summary
lessons learned
stack frames in the Java Virtual Machine
• parameter passing, returning results
• implementation strategies
Activation Records 44
85. Summary
lessons learned
stack frames in the Java Virtual Machine
• parameter passing, returning results
• implementation strategies
stack frames in register-based machines
• registers x86 family
• manipulating stack registers
• calling conventions
Activation Records 44
86. Summary
lessons learned
stack frames in the Java Virtual Machine
• parameter passing, returning results
• implementation strategies
stack frames in register-based machines
• registers x86 family
• manipulating stack registers
• calling conventions
optimisations
Activation Records 44
87. Literature
learn more
Java Virtual Machine
Tim Lindholm, Frank Yellin: The Java Virtual Machine Specification, 2nd
edition. Addison-Wesley, 1999.
Bill Venners: Inside the Java 2 Virtual Machine. McGraw-Hill, 2000.
Activation Records 45
88. Literature
learn more
Java Virtual Machine
Tim Lindholm, Frank Yellin: The Java Virtual Machine Specification, 2nd
edition. Addison-Wesley, 1999.
Bill Venners: Inside the Java 2 Virtual Machine. McGraw-Hill, 2000.
Activation Records
Andrew W. Appel, Jens Palsberg: Modern Compiler Implementation in
Java, 2nd edition. 2002
Activation Records 45
89. Outlook
coming next
imperative and object-oriented languages
• Lecture 10: Dataflow Analysis Nov 13
• Lecture 11: Register Allocation Nov 20
• Lecture 12: Garbage Collection Nov 27
Lab Oct 25
• name analysis
• name-based errors
• type analysis for named elements
• type-based errors
Activation Records 46
92. Pictures
copyrights
Slide 1:
Framed by LexnGer, some rights reserved
Slide 37:
The Lemon Tree by Dominica Williamson, some rights reserved
Slide 46:
Oude Kerk by M.M. R, some rights reserved
Activation Records 49
Notas del editor
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
round-up on every lecture\n\nwhat to take with you\n\ncheck yourself, pre- and post-paration\n