SlideShare uma empresa Scribd logo
1 de 47
Baixar para ler offline
Working with
 Bytecodes:

IRBuilder and
 ByteSurgeon

Marcus Denker
Reasons for working with Bytecode


 •   Generating Bytecode
     – Implementing compilers for other languages
     – Experimentation with new language features


 •   Bytecode Transformation
     – Adaptation of running Systems
     – Tracing / Debugging
     – New language features




Marcus Denker
Overview

 1. Introduction to Squeak Bytecodes
 2. Generating Bytecode with IRBuilder
 3. Introduction to ByteSurgeon




Marcus Denker
The Squeak Virtual Machine

 •   From last lecture:
     – Virtual machine provides a virtual processor
     – Bytecode: The ‘machine-code’ of the virtual machine
     – Smalltalk (like Java): Stack machine

 •   Today:
     – Closer look at Squeak bytecode




Marcus Denker
Bytecode in the CompiledMethod

 •   CompiledMethods format:

                                        (Number>>#asInteger)inspect
        Header     Number of
                   temps, literals...

                   Array of all
        Literals   Literal Objects




      Bytecode

                   Pointer to
        Trailer    Source




Marcus Denker
Example: Number>>asInteger

 •   Smalltalk code:
      Number>>asInteger
      	

 "Answer an Integer nearest the receiver toward zero."

      	

 ^self truncated


 •   Symbolic Bytecode
      9 <70> self
      10 <D0> send: truncated
      11 <7C> returnTop




Marcus Denker
Example: Step by Step

 •   9 <70> self
     – The receiver (self) is pushed on the stack
 •   10 <D0> send: truncated
     – Bytecode 208: send literal selector 1
     – Get the selector from the first literal
     – start message lookup in the class of the object
       that is top of the stack
     – result is pushed on the stack
 •   11 <7C> returnTop
     – return the object on top of the stack to the
       calling method

Marcus Denker
Squeak Bytecodes

 •   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


Marcus Denker
Stack Bytecodes

 •   Push values on the stack, e.g., temps, instVars, literals
     – e.g: 16 - 31: push instance variable
 •   Push Constants (False/True/Nil/1/0/2/-1)
 •   Push self, thisContext
 •   Duplicate top of stack
 •   Pop




Marcus Denker
Sends and Returns

 •   Sends: receiver is on top of stack
     – Normal send
     – Super Sends
     – Hard-coded sends for efficiency, e.g. +, -

 •   Returns
      – Return top of stack to the sender
      – Return from a block
      – Special bytecodes for return self, nil, true, false
        (for efficiency)



Marcus Denker
Jump Bytecodes

 •   Control Flow inside one method
 •   Used to implement control-flow efficiently
 •   Example:
                    	

 ^ 1<2 ifTrue: ['true']



                9 <76> pushConstant: 1
                10 <77> pushConstant: 2
                11 <B2> send: <
                12 <99> jumpFalse: 15
                13 <20> pushConstant: 'true'
                14 <90> jumpTo: 16
                15 <73> pushConstant: nil
                16 <7C> returnTop


Marcus Denker
What you should have learned...

•   ... dealing with bytecodes directly is possible, but
    very boring.
•   We want reusable abstractions that hide the details
    (e.g. the different send bytecodes)


•   We would like to have frameworks for
    – Generating bytecode easily
    – Transforming bytecode
Generating Bytecodes


 •   IRBuilder: A tool for generating bytecode
 •   Part of the new compiler for Squeak 3.9

 •   Idea: a symbolic Assembler for Squeak




Marcus Denker
IRBuilder: Simple Example

 •   Number>>asInteger
     
     iRMethod := IRBuilder new
     	

   	

 numRargs: 1;
     	

   	

 addTemps: #(self); "receiver"
     	

   	

 pushTemp: #self;	

     	

   	

 send: #truncated;
     	

   	

 returnTop;
     	

   	

 ir.

     	

 aCompiledMethod := iRMethod compiledMethod.

     	

 aCompiledMethod valueWithReceiver:3.5
     	

 	

 	

 	

 	

 	

 	

 arguments: #()




Marcus Denker
IRBuilder: Step by Step

 •   Number>>asInteger
     
   iRMethod := IRBuilder new
     	

 	

     – Make a instance of IRBuilder




Marcus Denker
IRBuilder: Step by Step

 •   Number>>asInteger
     
   iRMethod := IRBuilder new
     	

 	

 numRargs: 1;
     	

 	


     – Define arguments. Note: “self” is default argument




Marcus Denker
IRBuilder: Step by Step

 •   Number>>asInteger
     
   iRMethod := IRBuilder new
     	

 	

 numRargs: 1;
     	

 	

 addTemps: #(self); "receiver"
     	

 	

     – define temporary variables. Note: arguments are temps




Marcus Denker
IRBuilder: Step by Step

 •   Number>>asInteger
     
   iRMethod := IRBuilder new
     	

 	

 numRargs: 1;
     	

 	

 addTemps: #(self); "receiver"
     	

 	

 pushTemp: #self

     – push “self” on the stack




Marcus Denker
IRBuilder: Step by Step

 •   Number>>asInteger
     
      iRMethod := IRBuilder new
     	

    	

 numRargs: 1;
     	

    	

 addTemps: #(self); "receiver"
                pushTemp: #self
     	

    	

 send: #truncated;
      –
     	

   call method truncated on “self”
            	





Marcus Denker
IRBuilder: Step by Step

 •   Number>>asInteger
     
     iRMethod := IRBuilder new
     	

   	

 numRargs: 1;
     	

   	

 addTemps: #(self); "receiver"
               pushTemp: #self
     	

   	

 send: #truncated;
     	

   	

 returnTop;
     	


     – return Top of Stack




Marcus Denker
IRBuilder: Step by Step

 •   Number>>asInteger
     
     iRMethod := IRBuilder new
     	

   	

 numRargs: 1;
     	

   	

 addTemps: #(self); "receiver"
     	

   	

 pushTemp: #self
     	

   	

 send: #truncated;
     	

   	

 returnTop;
     	

   	

 ir.

     – tell IRBuilder to generate Intermediate Representation
       (IR)




Marcus Denker
IRBuilder: Step by Step

 •   Number>>asInteger
     
     iRMethod := IRBuilder new
     	

   	

 numRargs: 1;
     	

   	

 addTemps: #(self); "receiver"
               pushTemp: #self
     	

   	

 send: #truncated;
     	

   	

 returnTop;
     	

   	

 ir.

     	

 aCompiledMethod := iRMethod compiledMethod.

     – Generate method from IR




Marcus Denker
IRBuilder: Step by Step

 •   Number>>asInteger
     
     iRMethod := IRBuilder new
     	

   	

 numRargs: 1;
     	

   	

 addTemps: #(self); "receiver"
     	

   	

 pushTemp: #self
     	

   	

 send: #truncated;
     	

   	

 returnTop;
     	

   	

 ir.

     	

 aCompiledMethod := iRMethod compiledMethod.

     	

 aCompiledMethod valueWithReceiver:3.5
     	

 	

 	

 	

 	

 	

 	

 arguments: #()
     – Execute the method with reveiver 3.5 and no arguments.
     – “3.5 truncated”
Marcus Denker
IRBuilder: Stack Manipulation

 •   popTop - remove the top of stack
 •   pushDup - push top of stack on the stack
 •   pushLiteral:
 •   pushReceiver - push self
 •   pushThisContext




Marcus Denker
IRBuilder: Symbolic Jumps

 •   Jump targets are resolved:
 •   Example: false ifTrue: [’true’] ifFalse: [’false’]
 iRMethod := IRBuilder new
 	

 numRargs: 1;
 	

 addTemps: #(self); "receiver"
 	

 pushLiteral: false;
 	

 jumpAheadTo: #false if: false;
 	

 pushLiteral: 'true';	

	

 	

 	

 "ifTrue: ['true']"
 	

 jumpAheadTo: #end;
 	

 jumpAheadTarget: #false;
 	

 pushLiteral: 'false';	

 	

 	

 "ifFalse: ['false']"
 	

 jumpAheadTarget: #end;
 	

 returnTop;
 	

 ir.


Marcus Denker
IRBuiler: Instance Variables

 •   Access by offset
 •   Read: getField:
      – receiver on top of stack
 •   Write: setField:
      – receiver and value on stack
 •   Example: set the first instance variable to 2
     iRMethod := IRBuilder new
     	

 	

 numRargs: 1;
     	

 	

 addTemps: #(self); "receiver"
     	

 	

 pushLiteral: 2;
     	

 	

 pushTemp: #self;
     	

 	

 setField: 1;
     	

 	

 pushTemp: #self;
     	

 	

 returnTop;
     	

 	

 ir.
     	

 	

     	

 aCompiledMethod := iRMethod compiledMethod.
     	

 aCompiledMethod valueWithReceiver: 1@2 arguments: #()
Marcus Denker
IRBuilder: Temporary Variables

 •   Accessed by name
 •   Define with addTemp: / addTemps:
 •   Read with pushTemp:
 •   Write with storeTemp:
 •   Examle: set variables a and b, return value of a
     iRMethod := IRBuilder new
     	

 	

 numRargs: 1;
     	

 	

 addTemps: #(self); "receiver"
     	

 	

 addTemps: #(a b);
     	

 	

 pushLiteral: 1;	

     	

 	

 storeTemp: #a;
     	

 	

 pushLiteral: 2;	

     	

 	

 storeTemp: #b;
     	

 	

 pushTemp: #a;
     	

 	

 returnTop;
     	

 	

 ir.

Marcus Denker
IRBuilder: Sends

 •   normal send
            builder pushLiteral: ‘hello’
            builder send: #size;


 •   super send
      ....
      builder send: #selector toSuperOf: aClass;

     – The second parameter specifies the class were the lookup
       starts.



Marcus Denker
IRBuilder: Lessons learned
•   IRBuilder: Easy bytecode generation
    –   Jumps
    –   Instance variable
    –   Temporary variables
    –   Sends

•   Next: Manipulating bytecode
ByteSurgeon

 •   Library for bytecode transformation in Smalltalk
 •   Full flexibility of Smalltalk Runtime
 •   Provides high-level API
 •   For Squeak, but portable


 •   Runtime transformation needed for
     •   Adaptation of running systems
     •   Tracing / debugging
     •   New language features (MOP, AOP)



Marcus Denker
Example: Logging

 •   Goal: logging message send.
 •   First way: Just edit the text:

example
	

 self test.
	

	



                 example
                 	

 Transcript show: ‘sending #test’.
                 	

 self test.




Marcus Denker
Logging with Bytesurgen

 •   Goal: Change the method without changing program
     text
 •   Example:

     (Example>>#example)instrumentSend: [:send |
     	

 send insertBefore:
     	

 	

 ‘Transcript show: ‘’sending #test’’ ‘.
     ]	





Marcus Denker
Logging: Step by Step

      (Example>>#example)instrumentSend: [:send |
      	

 send insertBefore:
      	

 	

 ‘Transcript show: ‘’sending #test’’ ‘.
      ]	



                        Example >> #example


                Class                           Name of Method



                   >>: - takes a name of a method
                   
 - returns the CompiledMethod object


Marcus Denker
Logging: Step by Step

        (Example>>#example)instrumentSend: [:send |
        	

 send insertBefore:
        	

 	

 ‘Transcript show: ‘’sending #test’’ ‘.
        ]	





 •   instrumentSend:
     – takes a block as an argument
     – evaluates it for all send bytecodes




Marcus Denker
Logging: Step by Step

        (Example>>#example)instrumentSend: [:send |
        	

 send insertBefore:
        	

 	

 ‘Transcript show: ‘’sending #test’’ ‘.
        ]	



 •   The block has one parameter: send
 •   It is executed for each send bytecode in the method




Marcus Denker
Logging: Step by Step

        (Example>>#example)instrumentSend: [:send |
        	

 send insertBefore:
        	

 	

 ‘Transcript show: ‘’sending #test’’ ‘.
        ]	



 •   Objects describing bytecode understand how to
     insert code
     – insertBefor
     – insertAfter
     – replace




Marcus Denker
Logging: Step by Step

        (Example>>#example)instrumentSend: [:send |
        	

 send insertBefore:
        	

 	

 ‘Transcript show: ‘’sending #test’’ ‘.
        ]	



 •   The code to be inserted.
 •   Double quoting for string inside string
     –Transcript show: ’sending #test’




Marcus Denker
Inside ByteSurgeon

 •   Uses IRBuilder internally


                 Decompile        Compile



     Bytecode                IR             Bytecode



 •   Transformation (Code inlining) done on IR



Marcus Denker
ByteSurgeon Usage

 •   On Methods or Classes:
         MyClass instrument: [.... ].	

         (MyClass>>#myMethod) instrument: [.... ].


 •   Different instrument methods:
     –   instrument:
     –   instrumentSend:
     –   instrumentTempVarRead:
     –   instrumentTempVarStore:
     –   instrumentTempVarAccess:
     –   same for InstVar


Marcus Denker
ByteSurgeon: Lessons learned
•   ByteSurgeon: Tool for editing bytecode
    – Simple example
    – Based on IRBuilder


•   Next: Advanced ByteSurgeon
Advanced ByteSurgeon:

 •   Goal: extend a send with after logging


example
	

 self test.
	

	



                 example
                 	

 	

 self test.
                 	

 	

 Logger logSendTo: self.




Marcus Denker
Advanced ByteSurgeon

 •   With Bytesurgeon, something like:


        (Example>>#example)instrumentSend: [:send |
        	

 send insertAfter:
        	

 	

 ‘Logger logSendTo: ?’ .
        ]	




 •   How can we access the receiver of the send?
 •   Solution: Metavariable


Marcus Denker
Advanced ByteSurgeon

 •   With Bytesurgeon, something like:


        (Example>>#example)instrumentSend: [:send |
        	

 send insertAfter:
        	

 	

 ‘Logger logSendTo: <meta: #receiver>’ .
        ]	




 •   How can we access the receiver of the send?
 •   Solution: Metavariable


Marcus Denker
Implementation Metavariables

 •   Stack during send:

           receiver

           arg1

           arg2                        result



                before                     after


 •   Problem 1: After send, receiver is not available
 •   Problem II: Before send, receiver is deep in the stack

Marcus Denker
Metavariables: Implementation


 •   Solution: ByteSurgeon generates preamble

     –   Pop the arguments into temps
     –   Pop the receiver into temps
     –   Rebuild the stack
     –   Do the send
     –   Now we can acces the receiver even after the send




Marcus Denker
Metavariables: Implementation



         25 <70> self
         26 <81 40> storeIntoTemp: 0   Preamble
         28 <D0> send: test
         29 <41> pushLit: Transcript
         30 <10> pushTemp: 0
                                       Inlined Code
         31 <E2> send: show:
         32 <87> pop
         33 <87> pop
         34 <78> returnSelf



Marcus Denker
End


•   Short overview of Squeak bytecode
•   Introduction to bytecode generation with IRBuilder
•   Manipulating bytecode with ByteSurgeon


•   Questions?

Mais conteúdo relacionado

Mais procurados

EKON 25 Python4Delphi_mX4
EKON 25 Python4Delphi_mX4EKON 25 Python4Delphi_mX4
EKON 25 Python4Delphi_mX4Max Kleiner
 
Grand Central Dispatch
Grand Central DispatchGrand Central Dispatch
Grand Central Dispatchcqtt191
 
WTF is Twisted?
WTF is Twisted?WTF is Twisted?
WTF is Twisted?hawkowl
 
From Zero To Production (NixOS, Erlang) @ Erlang Factory SF 2016
From Zero To Production (NixOS, Erlang) @ Erlang Factory SF 2016From Zero To Production (NixOS, Erlang) @ Erlang Factory SF 2016
From Zero To Production (NixOS, Erlang) @ Erlang Factory SF 2016Susan Potter
 
Java Performance Tuning
Java Performance TuningJava Performance Tuning
Java Performance TuningMinh Hoang
 
Twisted: a quick introduction
Twisted: a quick introductionTwisted: a quick introduction
Twisted: a quick introductionRobert Coup
 
Node.js System: The Approach
Node.js System: The ApproachNode.js System: The Approach
Node.js System: The ApproachHaci Murat Yaman
 
Dennis Benkert & Matthias Lübken - Patterns in a containerized world? - code....
Dennis Benkert & Matthias Lübken - Patterns in a containerized world? - code....Dennis Benkert & Matthias Lübken - Patterns in a containerized world? - code....
Dennis Benkert & Matthias Lübken - Patterns in a containerized world? - code....AboutYouGmbH
 
Ekon 25 Python4Delphi_MX475
Ekon 25 Python4Delphi_MX475Ekon 25 Python4Delphi_MX475
Ekon 25 Python4Delphi_MX475Max Kleiner
 
What Makes Objective C Dynamic?
What Makes Objective C Dynamic?What Makes Objective C Dynamic?
What Makes Objective C Dynamic?Kyle Oba
 
Asynchronous Python with Twisted
Asynchronous Python with TwistedAsynchronous Python with Twisted
Asynchronous Python with TwistedAdam Englander
 
Other Approaches (Concurrency)
Other Approaches (Concurrency)Other Approaches (Concurrency)
Other Approaches (Concurrency)Sri Prasanna
 
JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?Doug Hawkins
 
Network programming in java - PPT
Network programming in java - PPTNetwork programming in java - PPT
Network programming in java - PPTkamal kotecha
 
Patterns for JVM languages JokerConf
Patterns for JVM languages JokerConfPatterns for JVM languages JokerConf
Patterns for JVM languages JokerConfJaroslaw Palka
 
2008 07-24 kwpm-threads_and_synchronization
2008 07-24 kwpm-threads_and_synchronization2008 07-24 kwpm-threads_and_synchronization
2008 07-24 kwpm-threads_and_synchronizationfangjiafu
 

Mais procurados (20)

EKON 25 Python4Delphi_mX4
EKON 25 Python4Delphi_mX4EKON 25 Python4Delphi_mX4
EKON 25 Python4Delphi_mX4
 
Grand Central Dispatch
Grand Central DispatchGrand Central Dispatch
Grand Central Dispatch
 
WTF is Twisted?
WTF is Twisted?WTF is Twisted?
WTF is Twisted?
 
From Zero To Production (NixOS, Erlang) @ Erlang Factory SF 2016
From Zero To Production (NixOS, Erlang) @ Erlang Factory SF 2016From Zero To Production (NixOS, Erlang) @ Erlang Factory SF 2016
From Zero To Production (NixOS, Erlang) @ Erlang Factory SF 2016
 
Java Performance Tuning
Java Performance TuningJava Performance Tuning
Java Performance Tuning
 
Twisted: a quick introduction
Twisted: a quick introductionTwisted: a quick introduction
Twisted: a quick introduction
 
Node.js System: The Approach
Node.js System: The ApproachNode.js System: The Approach
Node.js System: The Approach
 
Dennis Benkert & Matthias Lübken - Patterns in a containerized world? - code....
Dennis Benkert & Matthias Lübken - Patterns in a containerized world? - code....Dennis Benkert & Matthias Lübken - Patterns in a containerized world? - code....
Dennis Benkert & Matthias Lübken - Patterns in a containerized world? - code....
 
Runtime
RuntimeRuntime
Runtime
 
NodeJS: an Introduction
NodeJS: an IntroductionNodeJS: an Introduction
NodeJS: an Introduction
 
8 - OOP - Syntax & Messages
8 - OOP - Syntax & Messages8 - OOP - Syntax & Messages
8 - OOP - Syntax & Messages
 
Ekon 25 Python4Delphi_MX475
Ekon 25 Python4Delphi_MX475Ekon 25 Python4Delphi_MX475
Ekon 25 Python4Delphi_MX475
 
Cp7 rpc
Cp7 rpcCp7 rpc
Cp7 rpc
 
What Makes Objective C Dynamic?
What Makes Objective C Dynamic?What Makes Objective C Dynamic?
What Makes Objective C Dynamic?
 
Asynchronous Python with Twisted
Asynchronous Python with TwistedAsynchronous Python with Twisted
Asynchronous Python with Twisted
 
Other Approaches (Concurrency)
Other Approaches (Concurrency)Other Approaches (Concurrency)
Other Approaches (Concurrency)
 
JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?
 
Network programming in java - PPT
Network programming in java - PPTNetwork programming in java - PPT
Network programming in java - PPT
 
Patterns for JVM languages JokerConf
Patterns for JVM languages JokerConfPatterns for JVM languages JokerConf
Patterns for JVM languages JokerConf
 
2008 07-24 kwpm-threads_and_synchronization
2008 07-24 kwpm-threads_and_synchronization2008 07-24 kwpm-threads_and_synchronization
2008 07-24 kwpm-threads_and_synchronization
 

Semelhante a RBuilder and ByteSurgeon

VMs, Interpreters, JIT
VMs, Interpreters, JITVMs, Interpreters, JIT
VMs, Interpreters, JITMarcus Denker
 
Working with Bytecode
Working with BytecodeWorking with Bytecode
Working with BytecodeMarcus Denker
 
Constant Blocks in Pharo11
Constant Blocks in Pharo11 Constant Blocks in Pharo11
Constant Blocks in Pharo11 ESUG
 
ConstantBlocks in Pharo11
ConstantBlocks in Pharo11ConstantBlocks in Pharo11
ConstantBlocks in Pharo11Marcus Denker
 
Runtime Bytecode Transformation for Smalltalk
Runtime Bytecode Transformation for SmalltalkRuntime Bytecode Transformation for Smalltalk
Runtime Bytecode Transformation for SmalltalkMarcus Denker
 
Lecture: "Advanced Reflection: MetaLinks"
Lecture: "Advanced Reflection: MetaLinks"Lecture: "Advanced Reflection: MetaLinks"
Lecture: "Advanced Reflection: MetaLinks"Marcus Denker
 
Lecture. Advanced Reflection: MetaLinks
Lecture. Advanced Reflection: MetaLinksLecture. Advanced Reflection: MetaLinks
Lecture. Advanced Reflection: MetaLinksMarcus Denker
 
Runtime Bytecode Transformation for Smalltalk
Runtime Bytecode Transformation for SmalltalkRuntime Bytecode Transformation for Smalltalk
Runtime Bytecode Transformation for SmalltalkESUG
 
Begin with c++ Fekra Course #1
Begin with c++ Fekra Course #1Begin with c++ Fekra Course #1
Begin with c++ Fekra Course #1Amr Alaa El Deen
 
C# 101: Intro to Programming with C#
C# 101: Intro to Programming with C#C# 101: Intro to Programming with C#
C# 101: Intro to Programming with C#Hawkman Academy
 
2 BytesC++ course_2014_c1_basicsc++
2 BytesC++ course_2014_c1_basicsc++2 BytesC++ course_2014_c1_basicsc++
2 BytesC++ course_2014_c1_basicsc++kinan keshkeh
 
Automated Slow Query Analysis: Dex the Index Robot
Automated Slow Query Analysis: Dex the Index RobotAutomated Slow Query Analysis: Dex the Index Robot
Automated Slow Query Analysis: Dex the Index RobotMongoDB
 
VUB Brussels Lecture 2019: Advanced Reflection: MetaLinks
VUB Brussels Lecture 2019: Advanced Reflection: MetaLinksVUB Brussels Lecture 2019: Advanced Reflection: MetaLinks
VUB Brussels Lecture 2019: Advanced Reflection: MetaLinksMarcus Denker
 
Sista: Improving Cog’s JIT performance
Sista: Improving Cog’s JIT performanceSista: Improving Cog’s JIT performance
Sista: Improving Cog’s JIT performanceESUG
 
Tdd for BT E2E test community
Tdd for BT E2E test communityTdd for BT E2E test community
Tdd for BT E2E test communityKerry Buckley
 
JVM Memory Model - Yoav Abrahami, Wix
JVM Memory Model - Yoav Abrahami, WixJVM Memory Model - Yoav Abrahami, Wix
JVM Memory Model - Yoav Abrahami, WixCodemotion Tel Aviv
 

Semelhante a RBuilder and ByteSurgeon (20)

VMs, Interpreters, JIT
VMs, Interpreters, JITVMs, Interpreters, JIT
VMs, Interpreters, JIT
 
Working with Bytecode
Working with BytecodeWorking with Bytecode
Working with Bytecode
 
Refactoring
RefactoringRefactoring
Refactoring
 
C++ basic.ppt
C++ basic.pptC++ basic.ppt
C++ basic.ppt
 
Constant Blocks in Pharo11
Constant Blocks in Pharo11 Constant Blocks in Pharo11
Constant Blocks in Pharo11
 
ConstantBlocks in Pharo11
ConstantBlocks in Pharo11ConstantBlocks in Pharo11
ConstantBlocks in Pharo11
 
Runtime Bytecode Transformation for Smalltalk
Runtime Bytecode Transformation for SmalltalkRuntime Bytecode Transformation for Smalltalk
Runtime Bytecode Transformation for Smalltalk
 
Lecture: MetaLinks
Lecture: MetaLinksLecture: MetaLinks
Lecture: MetaLinks
 
Lecture: "Advanced Reflection: MetaLinks"
Lecture: "Advanced Reflection: MetaLinks"Lecture: "Advanced Reflection: MetaLinks"
Lecture: "Advanced Reflection: MetaLinks"
 
Lecture. Advanced Reflection: MetaLinks
Lecture. Advanced Reflection: MetaLinksLecture. Advanced Reflection: MetaLinks
Lecture. Advanced Reflection: MetaLinks
 
Runtime Bytecode Transformation for Smalltalk
Runtime Bytecode Transformation for SmalltalkRuntime Bytecode Transformation for Smalltalk
Runtime Bytecode Transformation for Smalltalk
 
Begin with c++ Fekra Course #1
Begin with c++ Fekra Course #1Begin with c++ Fekra Course #1
Begin with c++ Fekra Course #1
 
C# 101: Intro to Programming with C#
C# 101: Intro to Programming with C#C# 101: Intro to Programming with C#
C# 101: Intro to Programming with C#
 
2 BytesC++ course_2014_c1_basicsc++
2 BytesC++ course_2014_c1_basicsc++2 BytesC++ course_2014_c1_basicsc++
2 BytesC++ course_2014_c1_basicsc++
 
Automated Slow Query Analysis: Dex the Index Robot
Automated Slow Query Analysis: Dex the Index RobotAutomated Slow Query Analysis: Dex the Index Robot
Automated Slow Query Analysis: Dex the Index Robot
 
Introduction to C ++.pptx
Introduction to C ++.pptxIntroduction to C ++.pptx
Introduction to C ++.pptx
 
VUB Brussels Lecture 2019: Advanced Reflection: MetaLinks
VUB Brussels Lecture 2019: Advanced Reflection: MetaLinksVUB Brussels Lecture 2019: Advanced Reflection: MetaLinks
VUB Brussels Lecture 2019: Advanced Reflection: MetaLinks
 
Sista: Improving Cog’s JIT performance
Sista: Improving Cog’s JIT performanceSista: Improving Cog’s JIT performance
Sista: Improving Cog’s JIT performance
 
Tdd for BT E2E test community
Tdd for BT E2E test communityTdd for BT E2E test community
Tdd for BT E2E test community
 
JVM Memory Model - Yoav Abrahami, Wix
JVM Memory Model - Yoav Abrahami, WixJVM Memory Model - Yoav Abrahami, Wix
JVM Memory Model - Yoav Abrahami, Wix
 

Mais de Marcus Denker

First Class Variables as AST Annotations
First Class Variables as AST AnnotationsFirst Class Variables as AST Annotations
First Class Variables as AST AnnotationsMarcus Denker
 
Supporting Pharo / Getting Pharo Support
Supporting Pharo / Getting Pharo SupportSupporting Pharo / Getting Pharo Support
Supporting Pharo / Getting Pharo SupportMarcus Denker
 
thisContext in the Debugger
thisContext in the DebuggerthisContext in the Debugger
thisContext in the DebuggerMarcus Denker
 
Improving code completion for Pharo
Improving code completion for PharoImproving code completion for Pharo
Improving code completion for PharoMarcus Denker
 
Lecture: Advanced Reflection. MetaLinks
Lecture: Advanced Reflection. MetaLinksLecture: Advanced Reflection. MetaLinks
Lecture: Advanced Reflection. MetaLinksMarcus Denker
 
Open-Source: An Infinite Game
Open-Source: An Infinite GameOpen-Source: An Infinite Game
Open-Source: An Infinite GameMarcus Denker
 
PharoTechTalk: Contributing to Pharo
PharoTechTalk: Contributing to PharoPharoTechTalk: Contributing to Pharo
PharoTechTalk: Contributing to PharoMarcus Denker
 
Feedback Loops in Practice
Feedback Loops in PracticeFeedback Loops in Practice
Feedback Loops in PracticeMarcus Denker
 
Reflection in Pharo: Beyond Smalltak
Reflection in Pharo: Beyond SmalltakReflection in Pharo: Beyond Smalltak
Reflection in Pharo: Beyond SmalltakMarcus Denker
 
Reflection in Pharo: Beyond Smalltak
Reflection in Pharo: Beyond SmalltakReflection in Pharo: Beyond Smalltak
Reflection in Pharo: Beyond SmalltakMarcus Denker
 
Perfection & Feedback Loops or: why worse is better
Perfection & Feedback Loops or: why worse is betterPerfection & Feedback Loops or: why worse is better
Perfection & Feedback Loops or: why worse is betterMarcus Denker
 
Dynamically Composing Collection Operations through Collection Promises
Dynamically Composing Collection Operations through Collection PromisesDynamically Composing Collection Operations through Collection Promises
Dynamically Composing Collection Operations through Collection PromisesMarcus Denker
 

Mais de Marcus Denker (20)

Soil And Pharo
Soil And PharoSoil And Pharo
Soil And Pharo
 
Demo: Improved DoIt
Demo: Improved DoItDemo: Improved DoIt
Demo: Improved DoIt
 
First Class Variables as AST Annotations
First Class Variables as AST AnnotationsFirst Class Variables as AST Annotations
First Class Variables as AST Annotations
 
Supporting Pharo / Getting Pharo Support
Supporting Pharo / Getting Pharo SupportSupporting Pharo / Getting Pharo Support
Supporting Pharo / Getting Pharo Support
 
thisContext in the Debugger
thisContext in the DebuggerthisContext in the Debugger
thisContext in the Debugger
 
Variables in Pharo
Variables in PharoVariables in Pharo
Variables in Pharo
 
Improving code completion for Pharo
Improving code completion for PharoImproving code completion for Pharo
Improving code completion for Pharo
 
Slot Composition
Slot CompositionSlot Composition
Slot Composition
 
Lecture: Advanced Reflection. MetaLinks
Lecture: Advanced Reflection. MetaLinksLecture: Advanced Reflection. MetaLinks
Lecture: Advanced Reflection. MetaLinks
 
PHARO IOT
PHARO IOTPHARO IOT
PHARO IOT
 
Open-Source: An Infinite Game
Open-Source: An Infinite GameOpen-Source: An Infinite Game
Open-Source: An Infinite Game
 
PharoTechTalk: Contributing to Pharo
PharoTechTalk: Contributing to PharoPharoTechTalk: Contributing to Pharo
PharoTechTalk: Contributing to Pharo
 
Feedback Loops in Practice
Feedback Loops in PracticeFeedback Loops in Practice
Feedback Loops in Practice
 
Pharo6 - ESUG17
Pharo6 - ESUG17Pharo6 - ESUG17
Pharo6 - ESUG17
 
Pharo6
Pharo6Pharo6
Pharo6
 
Reflection in Pharo: Beyond Smalltak
Reflection in Pharo: Beyond SmalltakReflection in Pharo: Beyond Smalltak
Reflection in Pharo: Beyond Smalltak
 
Reflection in Pharo: Beyond Smalltak
Reflection in Pharo: Beyond SmalltakReflection in Pharo: Beyond Smalltak
Reflection in Pharo: Beyond Smalltak
 
Perfection & Feedback Loops or: why worse is better
Perfection & Feedback Loops or: why worse is betterPerfection & Feedback Loops or: why worse is better
Perfection & Feedback Loops or: why worse is better
 
Dynamically Composing Collection Operations through Collection Promises
Dynamically Composing Collection Operations through Collection PromisesDynamically Composing Collection Operations through Collection Promises
Dynamically Composing Collection Operations through Collection Promises
 
Variables in Pharo5
Variables in Pharo5Variables in Pharo5
Variables in Pharo5
 

Último

The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...Wes McKinney
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...itnewsafrica
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationKnoldus Inc.
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxGenerative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxfnnc6jmgwh
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Strongerpanagenda
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI AgeCprime
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentPim van der Noll
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observabilityitnewsafrica
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality AssuranceInflectra
 
Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Kaya Weers
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...itnewsafrica
 
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesManik S Magar
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPathCommunity
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructureitnewsafrica
 

Último (20)

The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog Presentation
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxGenerative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI Age
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
 
Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
 
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to Hero
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
 

RBuilder and ByteSurgeon

  • 1. Working with Bytecodes: IRBuilder and ByteSurgeon Marcus Denker
  • 2. Reasons for working with Bytecode • Generating Bytecode – Implementing compilers for other languages – Experimentation with new language features • Bytecode Transformation – Adaptation of running Systems – Tracing / Debugging – New language features Marcus Denker
  • 3. Overview 1. Introduction to Squeak Bytecodes 2. Generating Bytecode with IRBuilder 3. Introduction to ByteSurgeon Marcus Denker
  • 4. The Squeak Virtual Machine • From last lecture: – Virtual machine provides a virtual processor – Bytecode: The ‘machine-code’ of the virtual machine – Smalltalk (like Java): Stack machine • Today: – Closer look at Squeak bytecode Marcus Denker
  • 5. Bytecode in the CompiledMethod • CompiledMethods format: (Number>>#asInteger)inspect Header Number of temps, literals... Array of all Literals Literal Objects Bytecode Pointer to Trailer Source Marcus Denker
  • 6. Example: Number>>asInteger • Smalltalk code: Number>>asInteger "Answer an Integer nearest the receiver toward zero." ^self truncated • Symbolic Bytecode 9 <70> self 10 <D0> send: truncated 11 <7C> returnTop Marcus Denker
  • 7. Example: Step by Step • 9 <70> self – The receiver (self) is pushed on the stack • 10 <D0> send: truncated – Bytecode 208: send literal selector 1 – Get the selector from the first literal – start message lookup in the class of the object that is top of the stack – result is pushed on the stack • 11 <7C> returnTop – return the object on top of the stack to the calling method Marcus Denker
  • 8. Squeak Bytecodes • 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 Marcus Denker
  • 9. Stack Bytecodes • Push values on the stack, e.g., temps, instVars, literals – e.g: 16 - 31: push instance variable • Push Constants (False/True/Nil/1/0/2/-1) • Push self, thisContext • Duplicate top of stack • Pop Marcus Denker
  • 10. Sends and Returns • Sends: receiver is on top of stack – Normal send – Super Sends – Hard-coded sends for efficiency, e.g. +, - • Returns – Return top of stack to the sender – Return from a block – Special bytecodes for return self, nil, true, false (for efficiency) Marcus Denker
  • 11. Jump Bytecodes • Control Flow inside one method • Used to implement control-flow efficiently • Example: ^ 1<2 ifTrue: ['true'] 9 <76> pushConstant: 1 10 <77> pushConstant: 2 11 <B2> send: < 12 <99> jumpFalse: 15 13 <20> pushConstant: 'true' 14 <90> jumpTo: 16 15 <73> pushConstant: nil 16 <7C> returnTop Marcus Denker
  • 12. What you should have learned... • ... dealing with bytecodes directly is possible, but very boring. • We want reusable abstractions that hide the details (e.g. the different send bytecodes) • We would like to have frameworks for – Generating bytecode easily – Transforming bytecode
  • 13. Generating Bytecodes • IRBuilder: A tool for generating bytecode • Part of the new compiler for Squeak 3.9 • Idea: a symbolic Assembler for Squeak Marcus Denker
  • 14. IRBuilder: Simple Example • Number>>asInteger iRMethod := IRBuilder new numRargs: 1; addTemps: #(self); "receiver" pushTemp: #self; send: #truncated; returnTop; ir. aCompiledMethod := iRMethod compiledMethod. aCompiledMethod valueWithReceiver:3.5 arguments: #() Marcus Denker
  • 15. IRBuilder: Step by Step • Number>>asInteger iRMethod := IRBuilder new – Make a instance of IRBuilder Marcus Denker
  • 16. IRBuilder: Step by Step • Number>>asInteger iRMethod := IRBuilder new numRargs: 1; – Define arguments. Note: “self” is default argument Marcus Denker
  • 17. IRBuilder: Step by Step • Number>>asInteger iRMethod := IRBuilder new numRargs: 1; addTemps: #(self); "receiver" – define temporary variables. Note: arguments are temps Marcus Denker
  • 18. IRBuilder: Step by Step • Number>>asInteger iRMethod := IRBuilder new numRargs: 1; addTemps: #(self); "receiver" pushTemp: #self – push “self” on the stack Marcus Denker
  • 19. IRBuilder: Step by Step • Number>>asInteger iRMethod := IRBuilder new numRargs: 1; addTemps: #(self); "receiver" pushTemp: #self send: #truncated; – call method truncated on “self” Marcus Denker
  • 20. IRBuilder: Step by Step • Number>>asInteger iRMethod := IRBuilder new numRargs: 1; addTemps: #(self); "receiver" pushTemp: #self send: #truncated; returnTop; – return Top of Stack Marcus Denker
  • 21. IRBuilder: Step by Step • Number>>asInteger iRMethod := IRBuilder new numRargs: 1; addTemps: #(self); "receiver" pushTemp: #self send: #truncated; returnTop; ir. – tell IRBuilder to generate Intermediate Representation (IR) Marcus Denker
  • 22. IRBuilder: Step by Step • Number>>asInteger iRMethod := IRBuilder new numRargs: 1; addTemps: #(self); "receiver" pushTemp: #self send: #truncated; returnTop; ir. aCompiledMethod := iRMethod compiledMethod. – Generate method from IR Marcus Denker
  • 23. IRBuilder: Step by Step • Number>>asInteger iRMethod := IRBuilder new numRargs: 1; addTemps: #(self); "receiver" pushTemp: #self send: #truncated; returnTop; ir. aCompiledMethod := iRMethod compiledMethod. aCompiledMethod valueWithReceiver:3.5 arguments: #() – Execute the method with reveiver 3.5 and no arguments. – “3.5 truncated” Marcus Denker
  • 24. IRBuilder: Stack Manipulation • popTop - remove the top of stack • pushDup - push top of stack on the stack • pushLiteral: • pushReceiver - push self • pushThisContext Marcus Denker
  • 25. IRBuilder: Symbolic Jumps • Jump targets are resolved: • Example: false ifTrue: [’true’] ifFalse: [’false’] iRMethod := IRBuilder new numRargs: 1; addTemps: #(self); "receiver" pushLiteral: false; jumpAheadTo: #false if: false; pushLiteral: 'true'; "ifTrue: ['true']" jumpAheadTo: #end; jumpAheadTarget: #false; pushLiteral: 'false'; "ifFalse: ['false']" jumpAheadTarget: #end; returnTop; ir. Marcus Denker
  • 26. IRBuiler: Instance Variables • Access by offset • Read: getField: – receiver on top of stack • Write: setField: – receiver and value on stack • Example: set the first instance variable to 2 iRMethod := IRBuilder new numRargs: 1; addTemps: #(self); "receiver" pushLiteral: 2; pushTemp: #self; setField: 1; pushTemp: #self; returnTop; ir. aCompiledMethod := iRMethod compiledMethod. aCompiledMethod valueWithReceiver: 1@2 arguments: #() Marcus Denker
  • 27. IRBuilder: Temporary Variables • Accessed by name • Define with addTemp: / addTemps: • Read with pushTemp: • Write with storeTemp: • Examle: set variables a and b, return value of a iRMethod := IRBuilder new numRargs: 1; addTemps: #(self); "receiver" addTemps: #(a b); pushLiteral: 1; storeTemp: #a; pushLiteral: 2; storeTemp: #b; pushTemp: #a; returnTop; ir. Marcus Denker
  • 28. IRBuilder: Sends • normal send builder pushLiteral: ‘hello’ builder send: #size; • super send .... builder send: #selector toSuperOf: aClass; – The second parameter specifies the class were the lookup starts. Marcus Denker
  • 29. IRBuilder: Lessons learned • IRBuilder: Easy bytecode generation – Jumps – Instance variable – Temporary variables – Sends • Next: Manipulating bytecode
  • 30. ByteSurgeon • Library for bytecode transformation in Smalltalk • Full flexibility of Smalltalk Runtime • Provides high-level API • For Squeak, but portable • Runtime transformation needed for • Adaptation of running systems • Tracing / debugging • New language features (MOP, AOP) Marcus Denker
  • 31. Example: Logging • Goal: logging message send. • First way: Just edit the text: example self test. example Transcript show: ‘sending #test’. self test. Marcus Denker
  • 32. Logging with Bytesurgen • Goal: Change the method without changing program text • Example: (Example>>#example)instrumentSend: [:send | send insertBefore: ‘Transcript show: ‘’sending #test’’ ‘. ] Marcus Denker
  • 33. Logging: Step by Step (Example>>#example)instrumentSend: [:send | send insertBefore: ‘Transcript show: ‘’sending #test’’ ‘. ] Example >> #example Class Name of Method >>: - takes a name of a method - returns the CompiledMethod object Marcus Denker
  • 34. Logging: Step by Step (Example>>#example)instrumentSend: [:send | send insertBefore: ‘Transcript show: ‘’sending #test’’ ‘. ] • instrumentSend: – takes a block as an argument – evaluates it for all send bytecodes Marcus Denker
  • 35. Logging: Step by Step (Example>>#example)instrumentSend: [:send | send insertBefore: ‘Transcript show: ‘’sending #test’’ ‘. ] • The block has one parameter: send • It is executed for each send bytecode in the method Marcus Denker
  • 36. Logging: Step by Step (Example>>#example)instrumentSend: [:send | send insertBefore: ‘Transcript show: ‘’sending #test’’ ‘. ] • Objects describing bytecode understand how to insert code – insertBefor – insertAfter – replace Marcus Denker
  • 37. Logging: Step by Step (Example>>#example)instrumentSend: [:send | send insertBefore: ‘Transcript show: ‘’sending #test’’ ‘. ] • The code to be inserted. • Double quoting for string inside string –Transcript show: ’sending #test’ Marcus Denker
  • 38. Inside ByteSurgeon • Uses IRBuilder internally Decompile Compile Bytecode IR Bytecode • Transformation (Code inlining) done on IR Marcus Denker
  • 39. ByteSurgeon Usage • On Methods or Classes: MyClass instrument: [.... ]. (MyClass>>#myMethod) instrument: [.... ]. • Different instrument methods: – instrument: – instrumentSend: – instrumentTempVarRead: – instrumentTempVarStore: – instrumentTempVarAccess: – same for InstVar Marcus Denker
  • 40. ByteSurgeon: Lessons learned • ByteSurgeon: Tool for editing bytecode – Simple example – Based on IRBuilder • Next: Advanced ByteSurgeon
  • 41. Advanced ByteSurgeon: • Goal: extend a send with after logging example self test. example self test. Logger logSendTo: self. Marcus Denker
  • 42. Advanced ByteSurgeon • With Bytesurgeon, something like: (Example>>#example)instrumentSend: [:send | send insertAfter: ‘Logger logSendTo: ?’ . ] • How can we access the receiver of the send? • Solution: Metavariable Marcus Denker
  • 43. Advanced ByteSurgeon • With Bytesurgeon, something like: (Example>>#example)instrumentSend: [:send | send insertAfter: ‘Logger logSendTo: <meta: #receiver>’ . ] • How can we access the receiver of the send? • Solution: Metavariable Marcus Denker
  • 44. Implementation Metavariables • Stack during send: receiver arg1 arg2 result before after • Problem 1: After send, receiver is not available • Problem II: Before send, receiver is deep in the stack Marcus Denker
  • 45. Metavariables: Implementation • Solution: ByteSurgeon generates preamble – Pop the arguments into temps – Pop the receiver into temps – Rebuild the stack – Do the send – Now we can acces the receiver even after the send Marcus Denker
  • 46. Metavariables: Implementation 25 <70> self 26 <81 40> storeIntoTemp: 0 Preamble 28 <D0> send: test 29 <41> pushLit: Transcript 30 <10> pushTemp: 0 Inlined Code 31 <E2> send: show: 32 <87> pop 33 <87> pop 34 <78> returnSelf Marcus Denker
  • 47. End • Short overview of Squeak bytecode • Introduction to bytecode generation with IRBuilder • Manipulating bytecode with ByteSurgeon • Questions?