0
Metaprogramming and Reflection Behavioral Reflection Universit ät Bern Marcus Denker Hasso-Plattner-Institut Potsdam Softw...
Topics Covered <ul><li>Overview </li></ul><ul><li>Introduction </li></ul><ul><li>Open implementations </li></ul><ul><li>OM...
Outline <ul><li>Introduction: Reflection in Squeak </li></ul><ul><li>Sub-method Structure: Bytecode  </li></ul><ul><li>Byt...
Reflection <ul><li>Reflection: computation about computation </li></ul><ul><ul><li>Base level / meta level </li></ul></ul>...
Reflection in Squeak <ul><li>Squeak has support for reflection </li></ul><ul><li>Structural Reflection </li></ul><ul><ul><...
Can we do better? <ul><li>Structural Reflection stops at method level </li></ul><ul><ul><li>Bytecode in the CompiledMethod...
Sub-Method Abstraction <ul><li>We need a model for method bodies </li></ul><ul><li>Posibilities: </li></ul><ul><ul><li>Tex...
Outline <ul><li>Introduction: Reflection in Squeak </li></ul><ul><li>Sub-method Structure: Bytecode  </li></ul><ul><li>Byt...
Squeak VM <ul><li>Virtual machine provides a virtual processor </li></ul><ul><li>Smalltalk (like Java): Stack machine </li...
CompiledMethods <ul><li>CompiledMethod format: </li></ul>Number of  temps, literals... Array of all  Literal Objects Point...
Squeak Bytecode <ul><li>256 Bytecodes, four groups: </li></ul><ul><ul><li>Stack Bytecodes </li></ul></ul><ul><ul><ul><li>S...
Editing Bytecode <ul><li>Why Bytecode? </li></ul><ul><ul><li>No source needed </li></ul></ul><ul><ul><li>Performance </li>...
Outline <ul><li>Introduction: Reflection in Squeak </li></ul><ul><li>Sub-method Structure: Bytecode  </li></ul><ul><li>Byt...
ByteSurgeon <ul><li>Library for bytecode transformation in Smalltalk  </li></ul><ul><li>Full flexibility of Smalltalk: Run...
Examples <ul><li>Counts the number of Bytecodes: </li></ul><ul><li>Counts the number of Sends:  </li></ul><ul><li>Introspe...
Transformations <ul><li>Modification: inlining of code  </li></ul><ul><ul><li>insertBefore:, insertAfter:, replace:  </li>...
Meta Variables <ul><li>Goal: extend a send with after logging  </li></ul><ul><li>Problem: How to access receiver and args?...
ByteSurgeon <ul><li>Provides simple model for submethod structure </li></ul><ul><ul><li>Too low level?  </li></ul></ul><ul...
Outline <ul><li>Introduction: Reflection in Squeak </li></ul><ul><li>Sub-method Structure: Bytecode  </li></ul><ul><li>Byt...
Partial Reflection: Reflex <ul><li>Hooksets: collection of operation occurrences </li></ul><ul><li>Links  </li></ul><ul><u...
Geppetto <ul><li>Partial Behavioral Reflection pioneered in Java </li></ul><ul><ul><li>Code transformation at load time </...
Example <ul><li>Typical Web Applications (e.g. Wiki) </li></ul><ul><li>Shows performance problem under high load </li></ul...
Towards a Solution <ul><li>Analyze the problem </li></ul><ul><ul><li>Install Profiler </li></ul></ul><ul><ul><li>Analyze <...
Solution with Reflection <ul><li>Operation </li></ul><ul><ul><li>Method Evaluation </li></ul></ul><ul><li>Hookset </li></u...
Profiler: Hookset + Link Hookset allExecs := Hookset new.  allExecs inPackage: ’Wiki’; operation: MethodEval.  profile := ...
Profiler: Protocol profile callDescriptor:  (CallDescriptor  selector: #profileMethod:in:withArguments:  parameters:{ Para...
Solving the Problem: Caching <ul><li>Operation: </li></ul><ul><ul><li>Method Execution (Around) </li></ul></ul><ul><li>Hoo...
Cache: Hookset and Link  toughWorks := Hookset new.  toughWorks inClass: Worker;  inMethod: #toughWork:;  operation: Metho...
Geppetto <ul><li>Operations </li></ul><ul><ul><li>MethodEval </li></ul></ul><ul><ul><li>MessageSend, InstVarAccess, TempAc...
Future Work <ul><li>Pluggable Backends </li></ul><ul><ul><li>Bytecode </li></ul></ul><ul><ul><li>AST </li></ul></ul><ul><u...
Conclusion <ul><li>Introduction: Reflection in Squeak </li></ul><ul><li>Sub-method Structure: Bytecode  </li></ul><ul><li>...
Conclusion <ul><li>Introduction: Reflection in Squeak </li></ul><ul><li>Sub-method Structure: Bytecode  </li></ul><ul><li>...
Upcoming SlideShare
Loading in...5
×

Behavioral Reflection

1,642

Published on

Lecture: "Behavioral Reflection", held at
Hasso Plattner Institute, Potsdam / Germany
05.12.2006

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,642
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
14
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Behavioral Reflection"

  1. 1. Metaprogramming and Reflection Behavioral 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
  2. 2. Topics Covered <ul><li>Overview </li></ul><ul><li>Introduction </li></ul><ul><li>Open implementations </li></ul><ul><li>OMG meta object facility </li></ul><ul><li>CLOS metaobject protocol </li></ul><ul><li>Smalltalk/Squeak </li></ul><ul><li>Behavioral reflection </li></ul><ul><li>Refactoring browser </li></ul><ul><li>AspectS and ContextS </li></ul><ul><li>Traits and mirrors </li></ul><ul><li>Metacircular interpreters </li></ul>Objects
  3. 3. Outline <ul><li>Introduction: Reflection in Squeak </li></ul><ul><li>Sub-method Structure: Bytecode </li></ul><ul><li>ByteSurgeon: Bytecode Transformation </li></ul><ul><li>Partial Behavioral Reflection </li></ul>
  4. 4. Reflection <ul><li>Reflection: computation about computation </li></ul><ul><ul><li>Base level / meta level </li></ul></ul><ul><ul><li>Causally connected </li></ul></ul><ul><li>Structural Reflection </li></ul><ul><ul><li>Reification of structure </li></ul></ul><ul><li>Behavioral Reflection </li></ul><ul><ul><li>Reification of execution </li></ul></ul>
  5. 5. Reflection in Squeak <ul><li>Squeak has support for reflection </li></ul><ul><li>Structural Reflection </li></ul><ul><ul><li>Classes / Methods are Objects </li></ul></ul><ul><ul><li>Can be changed at runtime </li></ul></ul><ul><li>Behavioral Reflection </li></ul><ul><ul><li>Current execution reified (thisContext) </li></ul></ul><ul><ul><li>#doesNotUnderstand / MethodWrappers </li></ul></ul>
  6. 6. Can we do better? <ul><li>Structural Reflection stops at method level </li></ul><ul><ul><li>Bytecode in the CompiledMethod: Numbers </li></ul></ul><ul><ul><li>Text: Just a String, needs to be compiled </li></ul></ul><ul><li>Behavior hard coded in the Virtual Machine </li></ul><ul><ul><li>Message Sending </li></ul></ul><ul><ul><li>Variable Access </li></ul></ul><ul><li>Behavioral Reflection is limited in Squeak </li></ul><ul><ul><li>We should do better! </li></ul></ul>
  7. 7. Sub-Method Abstraction <ul><li>We need a model for method bodies </li></ul><ul><li>Posibilities: </li></ul><ul><ul><li>Text </li></ul></ul><ul><ul><li>Bytecode (CompiledMethod) </li></ul></ul><ul><ul><li>AST (Abstract Syntax Tree) </li></ul></ul><ul><li>For now: Bytecode </li></ul><ul><ul><li>Bytecode is causally connected </li></ul></ul><ul><ul><li>Quite fast to edit </li></ul></ul><ul><li>Later: AST (next Lecture) </li></ul>
  8. 8. Outline <ul><li>Introduction: Reflection in Squeak </li></ul><ul><li>Sub-method Structure: Bytecode </li></ul><ul><li>ByteSurgeon: Bytecode Transformation </li></ul><ul><li>Partial Behavioral Reflection </li></ul>
  9. 9. Squeak VM <ul><li>Virtual machine provides a virtual processor </li></ul><ul><li>Smalltalk (like Java): Stack machine </li></ul><ul><ul><li>easy to implement interpreters for different processors </li></ul></ul><ul><ul><li>most hardware processors are register machines </li></ul></ul><ul><li>Bytecode: ‘Machine code’ of the VM </li></ul>
  10. 10. CompiledMethods <ul><li>CompiledMethod format: </li></ul>Number of temps, literals... Array of all Literal Objects Pointer to Source (Number methodDict at: #asInteger) inspect (Number>>#asInteger) inspect Trailer Bytecode Literals Header
  11. 11. Squeak Bytecode <ul><li>256 Bytecodes, four groups: </li></ul><ul><ul><li>Stack Bytecodes </li></ul></ul><ul><ul><ul><li>Stack manipulation: push / pop / dup </li></ul></ul></ul><ul><ul><li>Send Bytecodes </li></ul></ul><ul><ul><ul><li>Invoke Methods </li></ul></ul></ul><ul><ul><li>Return Bytecodes </li></ul></ul><ul><ul><ul><li>Return to caller </li></ul></ul></ul><ul><ul><li>Jump Bytecodes </li></ul></ul><ul><ul><ul><li>Control flow inside a method </li></ul></ul></ul>
  12. 12. Editing Bytecode <ul><li>Why Bytecode? </li></ul><ul><ul><li>No source needed </li></ul></ul><ul><ul><li>Performance </li></ul></ul><ul><ul><li>Language independent (e.g. Ruby-on-Squeak) </li></ul></ul><ul><li>Problems of direct Bytecode editing </li></ul><ul><ul><li>Hard to edit directly (e.g jump offsets) </li></ul></ul><ul><ul><li>Programmers don‘t know bytecode </li></ul></ul><ul><ul><li>Easy to make errors </li></ul></ul>
  13. 13. Outline <ul><li>Introduction: Reflection in Squeak </li></ul><ul><li>Sub-method Structure: Bytecode </li></ul><ul><li>ByteSurgeon: Bytecode Transformation </li></ul><ul><li>Partial Behavioral Reflection </li></ul>
  14. 14. ByteSurgeon <ul><li>Library for bytecode transformation in Smalltalk </li></ul><ul><li>Full flexibility of Smalltalk: Runtime </li></ul><ul><li>Provides high-level API </li></ul><ul><li>For Squeak, but portable </li></ul>[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.
  15. 15. Examples <ul><li>Counts the number of Bytecodes: </li></ul><ul><li>Counts the number of Sends: </li></ul><ul><li>Introspection: </li></ul>InstrCounter reset. Example instrument: [:instr | InstrCounter increase] InstrCounter reset. Example instrumentSend: [:instr|InstrCounter increase] (Example>>#aMethod) instrumentSend: [:send | Transcript show: send selector printString]
  16. 16. Transformations <ul><li>Modification: inlining of code </li></ul><ul><ul><li>insertBefore:, insertAfter:, replace: </li></ul></ul>(Example>>#aMethod) instrumentSend: [ :send | send insertAfter: ’ [ I nstrCounter increase ]’ (Example>>#aMethod) instrumentSend: [:send | send insertAfter: ‘ T ranscript show: ’ , send selector printString].
  17. 17. Meta Variables <ul><li>Goal: extend a send with after logging </li></ul><ul><li>Problem: How to access receiver and args? </li></ul><ul><li>Solution: metavariables </li></ul><ul><li>#receiver, #arguments, #argn, #result.... #value, #newvalue </li></ul>Example instrumentSend: [:s | s insertAfter: ‘ L ogger logSendTo: <meta: #receiver>‘]
  18. 18. ByteSurgeon <ul><li>Provides simple model for submethod structure </li></ul><ul><ul><li>Too low level? </li></ul></ul><ul><li>Used in a number of projects </li></ul><ul><ul><li>Trace Debugger </li></ul></ul><ul><ul><li>Test Coverage </li></ul></ul><ul><li>Next Step: Behavioral Reflection </li></ul><ul><ul><li>Bytesurgeon just concerned with inlining of bytecode </li></ul></ul><ul><ul><li>We want to have reifications + meta objects </li></ul></ul>
  19. 19. Outline <ul><li>Introduction: Reflection in Squeak </li></ul><ul><li>Sub-method Structure: Bytecode </li></ul><ul><li>ByteSurgeon: Bytecode Transformation </li></ul><ul><li>Partial Behavioral Reflection </li></ul>
  20. 20. Partial Reflection: Reflex <ul><li>Hooksets: collection of operation occurrences </li></ul><ul><li>Links </li></ul><ul><ul><li>Bind hooksets to metaobjects </li></ul></ul><ul><ul><li>Define Protocol between base and meta </li></ul></ul><ul><li>Goals </li></ul><ul><ul><li>Highly selective reification </li></ul></ul><ul><ul><li>Flexible metalevel engineering </li></ul></ul><ul><ul><ul><li>Protocol specification </li></ul></ul></ul><ul><ul><ul><li>Cross-cutting hooksets </li></ul></ul></ul>
  21. 21. Geppetto <ul><li>Partial Behavioral Reflection pioneered in Java </li></ul><ul><ul><li>Code transformation at load time </li></ul></ul><ul><ul><li>Not unanticipated (it’s Java...) </li></ul></ul><ul><li>Geppetto: Partial Behavioral Reflection for Smalltalk </li></ul><ul><ul><li>For Squeak 3.9 with ByteSurgeon </li></ul></ul><ul><ul><li>but portable to other dialects </li></ul></ul><ul><li>Let’s see an example! </li></ul>[Main Source] David R öthlisberger , Marcus Denker and Éric Tanter. Unanticipated Partial Behavioral Reflection: Adapting Applications at runtime . to appear
  22. 22. Example <ul><li>Typical Web Applications (e.g. Wiki) </li></ul><ul><li>Shows performance problem under high load </li></ul><ul><li>Goals: </li></ul><ul><ul><li>Profile and fix the problem </li></ul></ul><ul><ul><li>No restart / interruption of service </li></ul></ul>
  23. 23. Towards a Solution <ul><li>Analyze the problem </li></ul><ul><ul><li>Install Profiler </li></ul></ul><ul><ul><li>Analyze </li></ul></ul><ul><ul><li>Retract Profiler </li></ul></ul><ul><li>Solve the Problem </li></ul><ul><ul><li>Introduce a caching mechanism </li></ul></ul><ul><ul><li>Experiment with different solutions </li></ul></ul>
  24. 24. Solution with Reflection <ul><li>Operation </li></ul><ul><ul><li>Method Evaluation </li></ul></ul><ul><li>Hookset </li></ul><ul><ul><li>All method executions in the wiki </li></ul></ul><ul><li>Metaobject </li></ul><ul><ul><li>A profiler tool </li></ul></ul>
  25. 25. 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
  26. 26. 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.
  27. 27. Solving the Problem: Caching <ul><li>Operation: </li></ul><ul><ul><li>Method Execution (Around) </li></ul></ul><ul><li>Hookset </li></ul><ul><ul><li>The one slow method ( #toughWork: ) </li></ul></ul><ul><li>Metaobject </li></ul><ul><ul><li>The cache </li></ul></ul>
  28. 28. 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).
  29. 29. Geppetto <ul><li>Operations </li></ul><ul><ul><li>MethodEval </li></ul></ul><ul><ul><li>MessageSend, InstVarAccess, TempAccess </li></ul></ul><ul><li>Control </li></ul><ul><ul><li>Before, After, Around, Replace </li></ul></ul><ul><li>Activation condition per Link </li></ul>
  30. 30. Future Work <ul><li>Pluggable Backends </li></ul><ul><ul><li>Bytecode </li></ul></ul><ul><ul><li>AST </li></ul></ul><ul><ul><li>VM Support? </li></ul></ul><ul><li>AST: integrate with annotation framework </li></ul><ul><li>Better tool support </li></ul>
  31. 31. Conclusion <ul><li>Introduction: Reflection in Squeak </li></ul><ul><li>Sub-method Structure: Bytecode </li></ul><ul><li>ByteSurgeon: Bytecode Transformation </li></ul><ul><li>Partial Behavioral Reflection </li></ul><ul><li>Next Lecture: Refactoring Engine </li></ul>
  32. 32. Conclusion <ul><li>Introduction: Reflection in Squeak </li></ul><ul><li>Sub-method Structure: Bytecode </li></ul><ul><li>ByteSurgeon: Bytecode Transformation </li></ul><ul><li>Partial Behavioral Reflection </li></ul><ul><li>Next Lecture: Refactoring Engine </li></ul>Questions?
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×