SlideShare a Scribd company logo
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
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
Outline Introduction: Reflection in Squeak Sub-method Structure: Bytecode  ByteSurgeon: Bytecode Transformation Partial Behavioral Reflection
Reflection Reflection: computation about computation Base level / meta level Causally connected Structural Reflection  Reification of structure Behavioral Reflection Reification of execution
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
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!
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)
Outline Introduction: Reflection in Squeak Sub-method Structure: Bytecode  ByteSurgeon: Bytecode Transformation Partial 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
CompiledMethods CompiledMethod format: Number of  temps, literals... Array of all  Literal Objects Pointer to Source (Number methodDict at: #asInteger) inspect (Number>>#asInteger) inspect Trailer Bytecode Literals Header
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
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
Outline Introduction: Reflection in Squeak Sub-method Structure: Bytecode  ByteSurgeon: Bytecode Transformation Partial 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.
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]
Transformations Modification: inlining of code  insertBefore:, insertAfter:, replace:  (Example>>#aMethod) instrumentSend: [ :send |  send insertAfter:  ’ [ I nstrCounter increase ]’   (Example>>#aMethod) instrumentSend: [:send |  send insertAfter:  ‘ T ranscript show: ’ ,   send selector printString].
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: ‘ L ogger logSendTo: <meta: #receiver>‘]
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
Outline Introduction: Reflection in Squeak Sub-method Structure: Bytecode  ByteSurgeon: Bytecode Transformation Partial 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
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
Example Typical Web Applications (e.g. Wiki) Shows performance problem under high load Goals: Profile and fix the problem No restart / interruption of service
Towards a Solution Analyze the problem Install Profiler Analyze Retract Profiler Solve the Problem Introduce a caching mechanism Experiment with different solutions
Solution with Reflection Operation Method Evaluation Hookset All method executions in the wiki Metaobject A profiler tool
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
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.
Solving the Problem: Caching Operation: Method Execution (Around) Hookset The one slow method ( #toughWork: ) Metaobject The cache
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).
Geppetto Operations MethodEval MessageSend, InstVarAccess, TempAccess Control Before, After, Around, Replace Activation condition per Link
Future Work Pluggable Backends Bytecode AST VM Support? AST: integrate with annotation framework Better tool support
Conclusion Introduction: Reflection in Squeak Sub-method Structure: Bytecode  ByteSurgeon: Bytecode Transformation Partial Behavioral Reflection Next Lecture: Refactoring Engine
Conclusion Introduction: Reflection in Squeak Sub-method Structure: Bytecode  ByteSurgeon: Bytecode Transformation Partial Behavioral Reflection Next Lecture: Refactoring Engine Questions?

More Related Content

PPT
Stoop 305-reflective programming5
PDF
Virtual Machines Lecture
PDF
RBuilder and ByteSurgeon
PPT
Stoop 390-instruction stream
KEY
Grand Central Dispatch Design Patterns
PPT
iOS Multithreading
PDF
Metrics ekon 14_2_kleiner
PDF
A closure ekon16
Stoop 305-reflective programming5
Virtual Machines Lecture
RBuilder and ByteSurgeon
Stoop 390-instruction stream
Grand Central Dispatch Design Patterns
iOS Multithreading
Metrics ekon 14_2_kleiner
A closure ekon16

What's hot (20)

PDF
Objective-C Blocks and Grand Central Dispatch
PDF
EKON 25 Python4Delphi_mX4
PPTX
Grand Central Dispatch
PPT
Java Performance Tuning
KEY
Opal compiler
PDF
Dennis Benkert & Matthias Lübken - Patterns in a containerized world? - code....
PDF
Qt Application Programming with C++ - Part 2
PDF
PDF
Reactive server with netty
PPTX
Qt Framework Events Signals Threads
PDF
The dedexer disassembler
PDF
Inside the JVM - Follow the white rabbit! / Breizh JUG
PDF
JVM Mechanics: When Does the JVM JIT & Deoptimize?
PDF
Understanding the Dalvik bytecode with the Dedexer tool
KEY
Runtime
PPTX
Objective-c for Java Developers
KEY
Automatic Reference Counting
PDF
What Makes Objective C Dynamic?
PDF
IronSmalltalk
Objective-C Blocks and Grand Central Dispatch
EKON 25 Python4Delphi_mX4
Grand Central Dispatch
Java Performance Tuning
Opal compiler
Dennis Benkert & Matthias Lübken - Patterns in a containerized world? - code....
Qt Application Programming with C++ - Part 2
Reactive server with netty
Qt Framework Events Signals Threads
The dedexer disassembler
Inside the JVM - Follow the white rabbit! / Breizh JUG
JVM Mechanics: When Does the JVM JIT & Deoptimize?
Understanding the Dalvik bytecode with the Dedexer tool
Runtime
Objective-c for Java Developers
Automatic Reference Counting
What Makes Objective C Dynamic?
IronSmalltalk
Ad

Similar to Behavioral Reflection (20)

PPT
Lecture: Refactoring
PDF
Lecture: "Advanced Reflection: MetaLinks"
PDF
Lecture. Advanced Reflection: MetaLinks
PDF
Reflection in Pharo: Beyond Smalltak
PDF
Reflection in Pharo: Beyond Smalltak
PDF
VUB Brussels Lecture 2019: Advanced Reflection: MetaLinks
PDF
Lecture: MetaLinks
PDF
Runtime Bytecode Transformation for Smalltalk
PDF
#Pharo Days 2016 Reflectivity
PPT
C#3.0 & Vb 9.0 New Features
PDF
Behavioral Reflection in Pharo
PDF
Reflection in Pharo5
PPTX
Play framework : A Walkthrough
PDF
ReactCodemod: An automated approach for refactoring class based components to...
PPTX
Framework engineering JCO 2011
PDF
Runtime Bytecode Transformation for Smalltalk
PPTX
react-slidlkjfl;kj;dlkjopidfjhopijgpoerjpofjiwoepifjopweifjepoies.pptx
PDF
Working Effectively With Legacy Perl Code
PPT
Monorail presentation at WebDevelopersCommunity, Feb 1, 2009
PDF
Breaking Dependencies Legacy Code - Cork Software Crafters - September 2019
Lecture: Refactoring
Lecture: "Advanced Reflection: MetaLinks"
Lecture. Advanced Reflection: MetaLinks
Reflection in Pharo: Beyond Smalltak
Reflection in Pharo: Beyond Smalltak
VUB Brussels Lecture 2019: Advanced Reflection: MetaLinks
Lecture: MetaLinks
Runtime Bytecode Transformation for Smalltalk
#Pharo Days 2016 Reflectivity
C#3.0 & Vb 9.0 New Features
Behavioral Reflection in Pharo
Reflection in Pharo5
Play framework : A Walkthrough
ReactCodemod: An automated approach for refactoring class based components to...
Framework engineering JCO 2011
Runtime Bytecode Transformation for Smalltalk
react-slidlkjfl;kj;dlkjopidfjhopijgpoerjpofjiwoepifjopweifjepoies.pptx
Working Effectively With Legacy Perl Code
Monorail presentation at WebDevelopersCommunity, Feb 1, 2009
Breaking Dependencies Legacy Code - Cork Software Crafters - September 2019
Ad

More from Marcus Denker (20)

PDF
Soil And Pharo
PDF
ConstantBlocks in Pharo11
PDF
Demo: Improved DoIt
PDF
First Class Variables as AST Annotations
PDF
Supporting Pharo / Getting Pharo Support
PDF
thisContext in the Debugger
PDF
Variables in Pharo
PDF
Improving code completion for Pharo
PDF
Slot Composition
PDF
Lecture: Advanced Reflection. MetaLinks
PDF
PHARO IOT
PDF
Open-Source: An Infinite Game
PDF
PharoTechTalk: Contributing to Pharo
PDF
Feedback Loops in Practice
PDF
Pharo6 - ESUG17
PDF
PDF
Perfection & Feedback Loops or: why worse is better
PDF
Dynamically Composing Collection Operations through Collection Promises
PDF
Variables in Pharo5
PDF
How to Contribute to Pharo
Soil And Pharo
ConstantBlocks in Pharo11
Demo: Improved DoIt
First Class Variables as AST Annotations
Supporting Pharo / Getting Pharo Support
thisContext in the Debugger
Variables in Pharo
Improving code completion for Pharo
Slot Composition
Lecture: Advanced Reflection. MetaLinks
PHARO IOT
Open-Source: An Infinite Game
PharoTechTalk: Contributing to Pharo
Feedback Loops in Practice
Pharo6 - ESUG17
Perfection & Feedback Loops or: why worse is better
Dynamically Composing Collection Operations through Collection Promises
Variables in Pharo5
How to Contribute to Pharo

Recently uploaded (20)

DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Approach and Philosophy of On baking technology
PPT
Teaching material agriculture food technology
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
cuic standard and advanced reporting.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Big Data Technologies - Introduction.pptx
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
KodekX | Application Modernization Development
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Empathic Computing: Creating Shared Understanding
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Encapsulation theory and applications.pdf
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Modernizing your data center with Dell and AMD
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Network Security Unit 5.pdf for BCA BBA.
The AUB Centre for AI in Media Proposal.docx
Approach and Philosophy of On baking technology
Teaching material agriculture food technology
The Rise and Fall of 3GPP – Time for a Sabbatical?
cuic standard and advanced reporting.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Unlocking AI with Model Context Protocol (MCP)
Big Data Technologies - Introduction.pptx
Agricultural_Statistics_at_a_Glance_2022_0.pdf
KodekX | Application Modernization Development
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Empathic Computing: Creating Shared Understanding
Mobile App Security Testing_ A Comprehensive Guide.pdf
Encapsulation theory and applications.pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
Modernizing your data center with Dell and AMD
Understanding_Digital_Forensics_Presentation.pptx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Network Security Unit 5.pdf for BCA BBA.

Behavioral Reflection

  • 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. 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
  • 3. Outline Introduction: Reflection in Squeak Sub-method Structure: Bytecode ByteSurgeon: Bytecode Transformation Partial Behavioral Reflection
  • 4. Reflection Reflection: computation about computation Base level / meta level Causally connected Structural Reflection Reification of structure Behavioral Reflection Reification of execution
  • 5. 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
  • 6. 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!
  • 7. 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)
  • 8. Outline Introduction: Reflection in Squeak Sub-method Structure: Bytecode ByteSurgeon: Bytecode Transformation Partial Behavioral Reflection
  • 9. 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
  • 10. CompiledMethods CompiledMethod format: 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. 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
  • 12. 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
  • 13. Outline Introduction: Reflection in Squeak Sub-method Structure: Bytecode ByteSurgeon: Bytecode Transformation Partial Behavioral Reflection
  • 14. 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.
  • 15. 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]
  • 16. Transformations Modification: inlining of code insertBefore:, insertAfter:, replace: (Example>>#aMethod) instrumentSend: [ :send | send insertAfter: ’ [ I nstrCounter increase ]’ (Example>>#aMethod) instrumentSend: [:send | send insertAfter: ‘ T ranscript show: ’ , send selector printString].
  • 17. 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: ‘ L ogger logSendTo: <meta: #receiver>‘]
  • 18. 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
  • 19. Outline Introduction: Reflection in Squeak Sub-method Structure: Bytecode ByteSurgeon: Bytecode Transformation Partial Behavioral Reflection
  • 20. 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
  • 21. 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
  • 22. Example Typical Web Applications (e.g. Wiki) Shows performance problem under high load Goals: Profile and fix the problem No restart / interruption of service
  • 23. Towards a Solution Analyze the problem Install Profiler Analyze Retract Profiler Solve the Problem Introduce a caching mechanism Experiment with different solutions
  • 24. Solution with Reflection Operation Method Evaluation Hookset All method executions in the wiki Metaobject A profiler tool
  • 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. 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. Solving the Problem: Caching Operation: Method Execution (Around) Hookset The one slow method ( #toughWork: ) Metaobject The cache
  • 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. Geppetto Operations MethodEval MessageSend, InstVarAccess, TempAccess Control Before, After, Around, Replace Activation condition per Link
  • 30. Future Work Pluggable Backends Bytecode AST VM Support? AST: integrate with annotation framework Better tool support
  • 31. Conclusion Introduction: Reflection in Squeak Sub-method Structure: Bytecode ByteSurgeon: Bytecode Transformation Partial Behavioral Reflection Next Lecture: Refactoring Engine
  • 32. Conclusion Introduction: Reflection in Squeak Sub-method Structure: Bytecode ByteSurgeon: Bytecode Transformation Partial Behavioral Reflection Next Lecture: Refactoring Engine Questions?