Byte code engineering 21st May Saturday 2016

Preview:

Citation preview

Bytecode EngineeringSarath Soman

nsarathsoman@gmail.comhttps://sarathsoman.wordpress.com/

@sarathsom4

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

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

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

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

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

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

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

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

Method Invocation● Invokevirtual● Invokestatic● Invokeinterface● Invokespecial

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

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

Thank You

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

Recommended