05 Coursework 2 + BCEL
Compilers
12/03/2019 Part of the slides are used with kind permission of Dr Shin Yoo and Dr Yue Jia
COMP0012 [email protected]
COMP0012
Coursework Part 2
Using Java and the Byte Code Engineering Library (BCEL), implement the constant folding peephole optimisation as much as possible.
UCL
File .java
File .class
optimisation
COMP0012 [email protected]
optimised .class
JVM
Coursework: Guidelines
This coursework is compulsory
It will be number-graded (0 to 10)
It will contribute to 10% of the overall module outcome
Group Project (4 members)
Due on 26th April 2019 (14:00 GMT)
COMP0012
[email protected]
UCL
Coursework: Group
UCL
Members have been randomly allocated to a group You can find info on your group on Moodle
COMP0012 [email protected]
Coursework: 40 Groups 4 members
UCL
COMP0012 [email protected]
Coursework: Tasks and Marking (1)
UCL
COMP0012 [email protected]
Coursework: Tasks and Marking (1)
UCL
COMP0012 [email protected]
Coursework: Tasks and Marking (3)
UCL
COMP0012 [email protected]
Coursework: Tasks and Marking (4)
UCL
COMP0012 [email protected]
Coursework: Files
Download from Moodle
comp0012-coursework2.pdf , comp0012-coursework2.zip
UCL
COMP0012 [email protected]
Coursework: Files
Download from Moodle
comp0012-coursework2.pdf , comp0012-coursework2.zip
UCL
Skeleton project with ant build script and relevant Java libraries
COMP0012 [email protected]
Coursework: Files
UCL
Skeleton project with ant build script and relevant Java libraries
COMP0012 [email protected]
Coursework: Files
Skeleton project with ant build script and relevant Java libraries
UCL
COMP0012 [email protected]
Coursework: Deliverables
Implementation: a Java implementation of the optimisation Use the given directory structure and build script in the
skeleton files (available from Moodle)
Report: contains a detailed descriptions of your optimisation algorithm
Describe the optimisation you have implemented providing as much detail as possible (no page limit)
UCL
COMP0012 [email protected]
Coursework: Deliverables
Implementation + Report
UCL
Your implementation deliverable should use the build script and preserve the directory structure provided below
COMP0012 [email protected]
Coursework: Deliverables
Submit groupXX.zip on Moodle
Implementation + Report
UCL
Compress the top level directory (named as groupXX where XX is your group number) and submit it through Moodle.
COMP0012 Violations will result in reduction of points! [email protected]
Coursework: Tools
UCL
COMP0012 [email protected]
Introduction to Apache BCEL
UCL
https://commons.apache.org/proper/commons-bcel/
Byte Code Engineering Library (BCEL) part of Apache Commons
dormant for years at version 5.2, version 6.2 is now available Alternatives:
ObjectWeb Consortiums ASM: http://asm.ow2.org Javassist: http://www.csg.ci.i.u-tokyo.ac.jp/~chiba/javassist/
COMP0012 [email protected]
BCEL
UCL
It enables various activities without having access to the source code:
Implementing Aspect Oriented Programming (AOP): dynamically weave code into existing class during runtime
Program analysis, find bugs, reveal unused code, etc
Program transformation (obfuscation)
Can provide nice code generation platform if your language targets JVM
COMP0012 [email protected]
BCEL
UCL
We focus on a few use cases that will enable you tackle the coursework
BCEL Manual https://commons.apache.org/proper/commons-bcel/manual.html
BCEL API https://commons.apache.org/proper/commons-bcel/apidocs/
COMP0012 [email protected]
The Java Virtual Machine
https://commons.apache.org/proper/commons-bcel/manual/jvm.html
Programs written in the Java language are compiled into a portable binary format called byte code
Every class is represented by a single class file containing class related data and byte code instructions. These files are loaded dynamically into an interpreter (Java Virtual Machine, a.k.a. JVM) and executed
UCL
COMP0012 [email protected]
Bytecode Instruction Set
https://commons.apache.org/proper/commons-bcel/manual/jvm.html
UCL
JVM is a stack-oriented interpreter that creates a local stack frame of fixed size for every method invocation.
Values may also be stored intermediately in a frame area containing local variables which can be used like a set of registers.
These local variables are numbered from 0 to 65535, i.e., you have a maximum of 65536 of local variables per method.
The byte code instruction set currently consists of 212 instructions.
see https://commons.apache.org/proper/commons-bcel/manual/jvm.html
COMP0012 [email protected]
BCEL
package org.apache.bcel.classfile
UCL
COMPGS03 UML diagram for the JavaClass API [email protected]
A Few Classes
JavaClass: represent a Java byte code class
Of its various parts, we are interested most in:
Method: represent a method (a list of byte code instructions)
ConstantPool: represents the collection of constants
UCL
JavaClass is parsed from .class file by a ClassParser COMP0012 [email protected]
BCEL
package org.apache.bcel.generic
UCL
COMPGS03 UML diagram of the ClassGen API [email protected]
A Few Classes
UCL
ClassGen: generates a Java class from parts, which include ConstantPool and Methods
ConstantPoolGen: generates the constant pool
MethodGen: generates Java methods Eventually, ClassGen outputs byte[], which is
our .class file
COMP0012 [email protected]
Example 1: CompilerString
Source code available on Moodle
UCL
Changes all String constants into Compiler
COMP0012 [email protected]
Example 1: CompilerString
Source code available on Moodle
UCL
// load the original class into a class generator
ClassGen cgen = new ClassGen(original);
ConstantPoolGen cpgen = cgen.getConstantPool();
// get the current constant pool
ConstantPool cp = cpgen.getConstantPool();
// get the constants in the pool
Constant[] constants = cp.getConstantPool();
Changes all String constants into Compiler
COMP0012 [email protected]
Example 1: CompilerString
Source code available on Moodle
UCL
Changes all String constants into Compiler
for (int i = 0; i < constants.length; i++){[email protected] // string constants take two entries in the pool // the first one is of ConstantString, which contains // an index to the second entry, which is ConstantUtf8 // (displayed Asciz when disassembled by javap) // // ConstantUtf8 (Asciz) entries are used to store method names, etc // whereas we are only interested in String constants // So we first look for ConstantString entry, // then retrieve the index of ConstantUtf8 entry, which we then replace if (constants[i] instanceof ConstantString) {ConstantString cs = (ConstantString) constants[i];cp.setConstant(cs.getStringIndex(), new ConstantUtf8(“Compiler”)); }} Example 2: FiveSource code available on MoodleUCLChanges any integer constants pushed to the stack to 5// load the original class into a class generatorClassGen cgen = new ClassGen(original);ConstantPoolGen cpgen = cgen.getConstantPool();// Do your optimization hereMethod[] methods = cgen.getMethods();for (Method m : methods){ optimizeMethod(cgen, cpgen, m);}[email protected] Example 2: FiveSource code available on MoodleUCLChanges any integer constants pushed to the stack to 5 InstructionList instList = new InstructionList(methodCode.getCode()); // InstructionHandle is a wrapper for actual Instructions for (InstructionHandle handle : instList.getInstructionHandles()) {// if the instruction inside is iconstif (handle.getInstruction() instanceof ICONST){// insert new one with integer 5, and…instList.insert(handle, new ICONST(5));try{ // delete the old one instList.delete(handle);}catch (TargetLostException e){ // TODO Auto-generated catch block e.printStackTrace();}COMP0012 }[email protected]}
Reviews
There are no reviews yet.