13
Bytecode Engineering Sarath Soman [email protected] https://sarathsoman.wordpress.com/ @sarathsom4

Byte code engineering 21st May Saturday 2016

Embed Size (px)

Citation preview

Page 2: Byte code engineering   21st May Saturday 2016

Byte Code engineering● Modifying existing bytecode - Proxy Obj, AOP● Creating bytecode from scratch - Scala, Spring Data JPA

Page 3: Byte code engineering   21st May Saturday 2016

SLAng by Praseed Pai K T● Originally a language designed for .NET CLR● Used in production as rule engine generating JavaScript● Been ported to C++ with LLVM backend, Python, JS, VB and

Java● Experiments going on to support modules → Higher order

functions → Object orientation etc

Page 4: Byte code engineering   21st May Saturday 2016

Porting Slang to the JVM● Code generation strategy(Choosing the bytecode

engineering library)● Decided to go with Javassist, BCEL and ASM

Page 5: Byte code engineering   21st May Saturday 2016

Prerequisites to do bytecode generation● Know enough about the structure of a class file● Know enough about the JVM

Page 6: Byte code engineering   21st May Saturday 2016

0xCAFEBAE● Minor & Major version● Access Flags● Constant Pool● Field Instructions● Method Instructions

Page 7: Byte code engineering   21st May Saturday 2016

Bytecode execution● Heap● Java Stack● Method Frame - Operand Stack, Local Variable array● Method Area● Execution engine● Program Counter

Page 8: Byte code engineering   21st May Saturday 2016

What happens when Method A->x() calls Method b->y()● Instance of B is pushed to operand stack of X()● Parameters of Y() is pushed to operand stack of X() in

reverse order● When invoke instruction is seen → Stack frame of Y() is

pushed to Java stack● Parameters (including instance of B) is popped from

operand stack of X() one by one and are pushed to operand stack of Y()

● Starts executing Y()● Pops the frame of Y() when return instruction is seen

Page 9: Byte code engineering   21st May Saturday 2016

Method Invocation● Invokevirtual● Invokestatic● Invokeinterface● Invokespecial

Page 10: Byte code engineering   21st May Saturday 2016

How ‘IF’ works in JVM?● You know it if you know ‘goto’

Page 11: Byte code engineering   21st May Saturday 2016

How ‘loop’ works in JVM?● You know it if you know ‘goto’

Page 12: Byte code engineering   21st May Saturday 2016

Thank You

Page 13: Byte code engineering   21st May Saturday 2016

Code Links● https://github.com/nsarathsoman/Madras-JUG-Byte-Code-

Engineering● Slang Fork → https://github.com/nsarathsoman/slang4JVM ● Main Repo → https://github.com/bvaisakh/slang4JVM