Reflection Question 1. Reflection Question 2 m Reflection Question 3.
Behavioral Reflection
-
Upload
marcus-denker -
Category
Technology
-
view
1.623 -
download
0
description
Transcript of Behavioral Reflection
![Page 1: Behavioral Reflection](https://reader035.fdocuments.us/reader035/viewer/2022062418/554a0954b4c905e56c8b5a78/html5/thumbnails/1.jpg)
Metaprogramming and ReflectionBehavioral Reflection
Universität Bern
Marcus Denker
Hasso-Plattner-Institut Potsdam
Software Architecture Group
Prof. Dr. Robert Hirschfeld
http://www.swa.hpi.uni-potsdam.de
WS 2006/2007
![Page 2: Behavioral Reflection](https://reader035.fdocuments.us/reader035/viewer/2022062418/554a0954b4c905e56c8b5a78/html5/thumbnails/2.jpg)
2Software Architecture Group (www.swa.hpi.uni-potsdam.de) 2006
Metaprogramming and Metamodelling – Behavioral Reflection
QuickTime™ and aTIFF (Uncompressed) decompressor
are needed to see this picture.
Topics Covered
• Overview• Introduction• Open implementations• OMG meta object facility• CLOS metaobject protocol• Smalltalk/Squeak• Behavioral reflection• Refactoring browser• AspectS and ContextS• Traits and mirrors• Metacircular interpreters
Objects
![Page 3: Behavioral Reflection](https://reader035.fdocuments.us/reader035/viewer/2022062418/554a0954b4c905e56c8b5a78/html5/thumbnails/3.jpg)
3Software Architecture Group (www.swa.hpi.uni-potsdam.de) 2006
Metaprogramming and Metamodelling – Behavioral Reflection
Outline
• Introduction: Reflection in Squeak• Sub-method Structure: Bytecode • ByteSurgeon: Bytecode Transformation• Partial Behavioral Reflection
![Page 4: Behavioral Reflection](https://reader035.fdocuments.us/reader035/viewer/2022062418/554a0954b4c905e56c8b5a78/html5/thumbnails/4.jpg)
4Software Architecture Group (www.swa.hpi.uni-potsdam.de) 2006
Metaprogramming and Metamodelling – Behavioral Reflection
Reflection
• Reflection: computation about computation– Base level / meta level– Causally connected
• Structural Reflection – Reification of structure
• Behavioral Reflection– Reification of execution
![Page 5: Behavioral Reflection](https://reader035.fdocuments.us/reader035/viewer/2022062418/554a0954b4c905e56c8b5a78/html5/thumbnails/5.jpg)
5Software Architecture Group (www.swa.hpi.uni-potsdam.de) 2006
Metaprogramming and Metamodelling – Behavioral Reflection
Reflection in Squeak
• Squeak has support for reflection
• Structural Reflection– Classes / Methods are Objects– Can be changed at runtime
• Behavioral Reflection– Current execution reified (thisContext)– #doesNotUnderstand / MethodWrappers
![Page 6: Behavioral Reflection](https://reader035.fdocuments.us/reader035/viewer/2022062418/554a0954b4c905e56c8b5a78/html5/thumbnails/6.jpg)
6Software Architecture Group (www.swa.hpi.uni-potsdam.de) 2006
Metaprogramming and Metamodelling – Behavioral Reflection
Can we do better?
• Structural Reflection stops at method level– Bytecode in the CompiledMethod: Numbers– Text: Just a String, needs to be compiled
• Behavior hard coded in the Virtual Machine– Message Sending– Variable Access
• Behavioral Reflection is limited in Squeak– We should do better!
![Page 7: Behavioral Reflection](https://reader035.fdocuments.us/reader035/viewer/2022062418/554a0954b4c905e56c8b5a78/html5/thumbnails/7.jpg)
7Software Architecture Group (www.swa.hpi.uni-potsdam.de) 2006
Metaprogramming and Metamodelling – Behavioral Reflection
Sub-Method Abstraction
• We need a model for method bodies• Posibilities:
– Text– Bytecode (CompiledMethod)– AST (Abstract Syntax Tree)
• For now: Bytecode– Bytecode is causally connected– Quite fast to edit
• Later: AST (next Lecture)
![Page 8: Behavioral Reflection](https://reader035.fdocuments.us/reader035/viewer/2022062418/554a0954b4c905e56c8b5a78/html5/thumbnails/8.jpg)
8Software Architecture Group (www.swa.hpi.uni-potsdam.de) 2006
Metaprogramming and Metamodelling – Behavioral Reflection
Outline
• Introduction: Reflection in Squeak• Sub-method Structure: Bytecode • ByteSurgeon: Bytecode Transformation• Partial Behavioral Reflection
![Page 9: Behavioral Reflection](https://reader035.fdocuments.us/reader035/viewer/2022062418/554a0954b4c905e56c8b5a78/html5/thumbnails/9.jpg)
9Software Architecture Group (www.swa.hpi.uni-potsdam.de) 2006
Metaprogramming and Metamodelling – Behavioral Reflection
Squeak VM
• Virtual machine provides a virtual processor
• Smalltalk (like Java): Stack machine– easy to implement interpreters for different processors– most hardware processors are register machines
• Bytecode: ‘Machine code’ of the VM
![Page 10: Behavioral Reflection](https://reader035.fdocuments.us/reader035/viewer/2022062418/554a0954b4c905e56c8b5a78/html5/thumbnails/10.jpg)
10Software Architecture Group (www.swa.hpi.uni-potsdam.de) 2006
Metaprogramming and Metamodelling – Behavioral Reflection
CompiledMethods
• CompiledMethod format:
Number of temps, literals...Number of temps, literals...
Array of all Literal ObjectsArray of all Literal Objects
Pointer toSourcePointer toSource
Header
Literals
Bytecode
Trailer
(Number methodDict at: #asInteger) inspect(Number methodDict at: #asInteger) inspect
(Number>>#asInteger) inspect(Number>>#asInteger) inspect
![Page 11: Behavioral Reflection](https://reader035.fdocuments.us/reader035/viewer/2022062418/554a0954b4c905e56c8b5a78/html5/thumbnails/11.jpg)
11Software Architecture Group (www.swa.hpi.uni-potsdam.de) 2006
Metaprogramming and Metamodelling – Behavioral Reflection
Squeak Bytecode
• 256 Bytecodes, four groups:
– Stack Bytecodes• Stack manipulation: push / pop / dup
– Send Bytecodes• Invoke Methods
– Return Bytecodes• Return to caller
– Jump Bytecodes• Control flow inside a method
![Page 12: Behavioral Reflection](https://reader035.fdocuments.us/reader035/viewer/2022062418/554a0954b4c905e56c8b5a78/html5/thumbnails/12.jpg)
12Software Architecture Group (www.swa.hpi.uni-potsdam.de) 2006
Metaprogramming and Metamodelling – Behavioral Reflection
Editing Bytecode
• Why Bytecode?– No source needed– Performance– Language independent (e.g. Ruby-on-Squeak)
• Problems of direct Bytecode editing– Hard to edit directly (e.g jump offsets)– Programmers don‘t know bytecode– Easy to make errors
![Page 13: Behavioral Reflection](https://reader035.fdocuments.us/reader035/viewer/2022062418/554a0954b4c905e56c8b5a78/html5/thumbnails/13.jpg)
13Software Architecture Group (www.swa.hpi.uni-potsdam.de) 2006
Metaprogramming and Metamodelling – Behavioral Reflection
Outline
• Introduction: Reflection in Squeak• Sub-method Structure: Bytecode • ByteSurgeon: Bytecode Transformation• Partial Behavioral Reflection
![Page 14: Behavioral Reflection](https://reader035.fdocuments.us/reader035/viewer/2022062418/554a0954b4c905e56c8b5a78/html5/thumbnails/14.jpg)
14Software Architecture Group (www.swa.hpi.uni-potsdam.de) 2006
Metaprogramming and Metamodelling – Behavioral Reflection
ByteSurgeon
• Library for bytecode transformation in Smalltalk
• Full flexibility of Smalltalk: Runtime
• Provides high-level API
• For Squeak, but portable
[Main Source] Marcus Denker, Stéphane Ducasse and Éric Tanter. Runtime Bytecode Transformation for Smalltalk. Journal of Computer Languages, Systems and Structures, vol. 32, no. 2-3.
![Page 15: Behavioral Reflection](https://reader035.fdocuments.us/reader035/viewer/2022062418/554a0954b4c905e56c8b5a78/html5/thumbnails/15.jpg)
15Software Architecture Group (www.swa.hpi.uni-potsdam.de) 2006
Metaprogramming and Metamodelling – Behavioral Reflection
Examples
• Counts the number of Bytecodes:
• Counts the number of Sends:
• Introspection:
InstrCounter reset. Example instrument: [:instr | InstrCounter increase]
InstrCounter reset. Example instrumentSend: [:instr|InstrCounter increase]
(Example>>#aMethod) instrumentSend: [:send | Transcript show: send selector printString]
![Page 16: Behavioral Reflection](https://reader035.fdocuments.us/reader035/viewer/2022062418/554a0954b4c905e56c8b5a78/html5/thumbnails/16.jpg)
16Software Architecture Group (www.swa.hpi.uni-potsdam.de) 2006
Metaprogramming and Metamodelling – Behavioral Reflection
Transformations
• Modification: inlining of code – insertBefore:, insertAfter:, replace:
(Example>>#aMethod) instrumentSend: [ :send | send insertAfter: ’[InstrCounter increase]’
(Example>>#aMethod) instrumentSend: [:send | send insertAfter: ‘Transcript show:’,
send selector printString].
![Page 17: Behavioral Reflection](https://reader035.fdocuments.us/reader035/viewer/2022062418/554a0954b4c905e56c8b5a78/html5/thumbnails/17.jpg)
17Software Architecture Group (www.swa.hpi.uni-potsdam.de) 2006
Metaprogramming and Metamodelling – Behavioral Reflection
Meta Variables
• Goal: extend a send with after logging • Problem: How to access receiver and args? • Solution: metavariables
• #receiver, #arguments, #argn, #result.... #value, #newvalue
Example instrumentSend: [:s |
s insertAfter: ‘Logger logSendTo: <meta: #receiver>‘]
![Page 18: Behavioral Reflection](https://reader035.fdocuments.us/reader035/viewer/2022062418/554a0954b4c905e56c8b5a78/html5/thumbnails/18.jpg)
18Software Architecture Group (www.swa.hpi.uni-potsdam.de) 2006
Metaprogramming and Metamodelling – Behavioral Reflection
ByteSurgeon
• Provides simple model for submethod structure– Too low level?
• Used in a number of projects– Trace Debugger– Test Coverage
• Next Step: Behavioral Reflection – Bytesurgeon just concerned with inlining of bytecode– We want to have reifications + meta objects
![Page 19: Behavioral Reflection](https://reader035.fdocuments.us/reader035/viewer/2022062418/554a0954b4c905e56c8b5a78/html5/thumbnails/19.jpg)
19Software Architecture Group (www.swa.hpi.uni-potsdam.de) 2006
Metaprogramming and Metamodelling – Behavioral Reflection
Outline
• Introduction: Reflection in Squeak• Sub-method Structure: Bytecode • ByteSurgeon: Bytecode Transformation• Partial Behavioral Reflection
![Page 20: Behavioral Reflection](https://reader035.fdocuments.us/reader035/viewer/2022062418/554a0954b4c905e56c8b5a78/html5/thumbnails/20.jpg)
20Software Architecture Group (www.swa.hpi.uni-potsdam.de) 2006
Metaprogramming and Metamodelling – Behavioral Reflection
Partial Reflection: Reflex
• Hooksets: collection of operation occurrences• Links
– Bind hooksets to metaobjects– Define Protocol between base and meta
• Goals– Highly selective reification– Flexible metalevel engineering
• Protocol specification
• Cross-cutting hooksets
![Page 21: Behavioral Reflection](https://reader035.fdocuments.us/reader035/viewer/2022062418/554a0954b4c905e56c8b5a78/html5/thumbnails/21.jpg)
21Software Architecture Group (www.swa.hpi.uni-potsdam.de) 2006
Metaprogramming and Metamodelling – Behavioral Reflection
Geppetto
• Partial Behavioral Reflection pioneered in Java– Code transformation at load time– Not unanticipated (it’s Java...)
• Geppetto: Partial Behavioral Reflection for Smalltalk– For Squeak 3.9 with ByteSurgeon– but portable to other dialects
• Let’s see an example!
[Main Source] David Röthlisberger, Marcus Denker and Éric Tanter. Unanticipated Partial Behavioral Reflection: Adapting Applications at runtime. to appear
![Page 22: Behavioral Reflection](https://reader035.fdocuments.us/reader035/viewer/2022062418/554a0954b4c905e56c8b5a78/html5/thumbnails/22.jpg)
22Software Architecture Group (www.swa.hpi.uni-potsdam.de) 2006
Metaprogramming and Metamodelling – Behavioral Reflection
Example
• Typical Web Applications (e.g. Wiki)
• Shows performance problem under high load
• Goals:
– Profile and fix the problem– No restart / interruption of service
![Page 23: Behavioral Reflection](https://reader035.fdocuments.us/reader035/viewer/2022062418/554a0954b4c905e56c8b5a78/html5/thumbnails/23.jpg)
23Software Architecture Group (www.swa.hpi.uni-potsdam.de) 2006
Metaprogramming and Metamodelling – Behavioral Reflection
Towards a Solution
• Analyze the problem
– Install Profiler– Analyze– Retract Profiler
• Solve the Problem
– Introduce a caching mechanism– Experiment with different solutions
![Page 24: Behavioral Reflection](https://reader035.fdocuments.us/reader035/viewer/2022062418/554a0954b4c905e56c8b5a78/html5/thumbnails/24.jpg)
24Software Architecture Group (www.swa.hpi.uni-potsdam.de) 2006
Metaprogramming and Metamodelling – Behavioral Reflection
Solution with Reflection
• Operation– Method Evaluation
• Hookset– All method executions in the wiki
• Metaobject– A profiler tool
![Page 25: Behavioral Reflection](https://reader035.fdocuments.us/reader035/viewer/2022062418/554a0954b4c905e56c8b5a78/html5/thumbnails/25.jpg)
25Software Architecture Group (www.swa.hpi.uni-potsdam.de) 2006
Metaprogramming and Metamodelling – Behavioral Reflection
Profiler: Hookset + Link
Hookset
allExecs := Hookset new. allExecs inPackage: ’Wiki’; operation: MethodEval.
profile := Link id: #profiler hookset: allExecs metaobject: Profiler new. profile
control: Control around.
Link
![Page 26: Behavioral Reflection](https://reader035.fdocuments.us/reader035/viewer/2022062418/554a0954b4c905e56c8b5a78/html5/thumbnails/26.jpg)
26Software Architecture Group (www.swa.hpi.uni-potsdam.de) 2006
Metaprogramming and Metamodelling – Behavioral Reflection
Profiler: Protocol
profile callDescriptor: (CallDescriptor
selector: #profileMethod:in:withArguments: parameters:{ Parameter selector.
Parameter self. Parameter arguments.}
passingMode: PassingMode plain).
Protocol
Install / Retract
profile install.profile uninstall.
![Page 27: Behavioral Reflection](https://reader035.fdocuments.us/reader035/viewer/2022062418/554a0954b4c905e56c8b5a78/html5/thumbnails/27.jpg)
27Software Architecture Group (www.swa.hpi.uni-potsdam.de) 2006
Metaprogramming and Metamodelling – Behavioral Reflection
Solving the Problem: Caching
• Operation:– Method Execution (Around)
• Hookset– The one slow method (#toughWork:)
• Metaobject– The cache
![Page 28: Behavioral Reflection](https://reader035.fdocuments.us/reader035/viewer/2022062418/554a0954b4c905e56c8b5a78/html5/thumbnails/28.jpg)
28Software Architecture Group (www.swa.hpi.uni-potsdam.de) 2006
Metaprogramming and Metamodelling – Behavioral Reflection
Cache: Hookset and Link
toughWorks := Hookset new. toughWorks inClass: Worker;
inMethod: #toughWork:; operation: MethodEval.
Hookset:
Link:cache := Link id: #cache
hookset: toughWorks metaobject: Cache new.
cache control: Control around. cache callDescriptor:(CallDescriptor
selector: #cacheFor: parameters: {Parameter arg1} passingMode: PassingMode plain).
![Page 29: Behavioral Reflection](https://reader035.fdocuments.us/reader035/viewer/2022062418/554a0954b4c905e56c8b5a78/html5/thumbnails/29.jpg)
29Software Architecture Group (www.swa.hpi.uni-potsdam.de) 2006
Metaprogramming and Metamodelling – Behavioral Reflection
Geppetto
• Operations– MethodEval– MessageSend, InstVarAccess, TempAccess
• Control– Before, After, Around, Replace
• Activation condition per Link
![Page 30: Behavioral Reflection](https://reader035.fdocuments.us/reader035/viewer/2022062418/554a0954b4c905e56c8b5a78/html5/thumbnails/30.jpg)
30Software Architecture Group (www.swa.hpi.uni-potsdam.de) 2006
Metaprogramming and Metamodelling – Behavioral Reflection
Future Work
• Pluggable Backends– Bytecode– AST– VM Support?
• AST: integrate with annotation framework
• Better tool support
![Page 31: Behavioral Reflection](https://reader035.fdocuments.us/reader035/viewer/2022062418/554a0954b4c905e56c8b5a78/html5/thumbnails/31.jpg)
31Software Architecture Group (www.swa.hpi.uni-potsdam.de) 2006
Metaprogramming and Metamodelling – Behavioral Reflection
Conclusion
• Introduction: Reflection in Squeak• Sub-method Structure: Bytecode • ByteSurgeon: Bytecode Transformation• Partial Behavioral Reflection
• Next Lecture: Refactoring Engine
![Page 32: Behavioral Reflection](https://reader035.fdocuments.us/reader035/viewer/2022062418/554a0954b4c905e56c8b5a78/html5/thumbnails/32.jpg)
32Software Architecture Group (www.swa.hpi.uni-potsdam.de) 2006
Metaprogramming and Metamodelling – Behavioral Reflection
Conclusion
• Introduction: Reflection in Squeak• Sub-method Structure: Bytecode • ByteSurgeon: Bytecode Transformation• Partial Behavioral Reflection
• Next Lecture: Refactoring Engine
Questions?