Commit ca9d44a1 authored by Yudi Zheng's avatar Yudi Zheng
Browse files

Introduce primitive type boxing. User is able to fetch dynamic context without...

Introduce primitive type boxing. User is able to fetch dynamic context without store into local slot.

Partial evaluator removes sequential boxing and unboxing.
parent 664f2e0e
......@@ -245,6 +245,7 @@ public class WeavingCode {
int size = StackUtil.dupStack(sourceframe, method, operand,
sopcode, method.maxLocals + max);
// load the stack value
iList.insert(instr, AsmHelper.boxValueOnStack(targetType));
iList.insert(instr, new VarInsnNode(lopcode, max));
max += size;
}
......@@ -270,6 +271,7 @@ public class WeavingCode {
+ targetType + "\", while user needs \"" + t + "\"");
}
iList.insert(instr, AsmHelper.boxValueOnStack(targetType));
iList.insert(instr, new VarInsnNode(t.getOpcode(Opcodes.ILOAD),
slot - method.maxLocals));
} else if (invoke.name.equals("localVariableValue")) {
......@@ -289,6 +291,7 @@ public class WeavingCode {
+ targetType + "\", while user needs \"" + t + "\"");
}
iList.insert(instr, AsmHelper.boxValueOnStack(targetType));
iList.insert(instr, new VarInsnNode(t.getOpcode(Opcodes.ILOAD),
operand - method.maxLocals));
}
......@@ -301,13 +304,8 @@ public class WeavingCode {
// remove invoke
iList.remove(instr);
// remove checkcast, invoke
// remove checkcast
if (next.getOpcode() == Opcodes.CHECKCAST) {
if (next.getNext().getOpcode() == Opcodes.INVOKEVIRTUAL) {
iList.remove(next.getNext());
}
iList.remove(next);
}
}
......
......@@ -7,12 +7,14 @@ import java.util.Map;
import java.util.Set;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.InsnList;
import org.objectweb.asm.tree.InsnNode;
import org.objectweb.asm.tree.JumpInsnNode;
import org.objectweb.asm.tree.LabelNode;
import org.objectweb.asm.tree.LookupSwitchInsnNode;
import org.objectweb.asm.tree.MethodInsnNode;
import org.objectweb.asm.tree.MethodNode;
import org.objectweb.asm.tree.TableSwitchInsnNode;
import org.objectweb.asm.tree.TryCatchBlockNode;
......@@ -594,6 +596,54 @@ public class PartialEvaluator {
return isOptimized;
}
private static List<String> primitiveTypes;
static {
primitiveTypes = new LinkedList<String>();
primitiveTypes.add("java/lang/Boolean");
primitiveTypes.add("java/lang/Byte");
primitiveTypes.add("java/lang/Character");
primitiveTypes.add("java/lang/Double");
primitiveTypes.add("java/lang/Float");
primitiveTypes.add("java/lang/Integer");
primitiveTypes.add("java/lang/Long");
}
private static boolean removeBoxingAndUnboxing(InsnList ilist) {
boolean isOptimized = false;
for (AbstractInsnNode instr : ilist.toArray()) {
AbstractInsnNode prev = instr.getPrevious();
if (prev == null || (prev.getOpcode() != Opcodes.INVOKESTATIC)
|| (instr.getOpcode() != Opcodes.INVOKEVIRTUAL)) {
continue;
}
MethodInsnNode valueOf = (MethodInsnNode) prev;
MethodInsnNode toValue = (MethodInsnNode) instr;
if (!(primitiveTypes.contains(valueOf.owner)
&& valueOf.owner.equals(toValue.owner) && valueOf.name
.equals("valueOf")) && toValue.name.endsWith("Value")) {
continue;
}
if (!Type.getArgumentTypes(valueOf.desc)[0].equals(Type
.getReturnType(toValue.desc))) {
continue;
}
ilist.remove(prev);
ilist.remove(instr);
isOptimized = true;
}
return isOptimized;
}
public static boolean evaluate(InsnList ilist,
List<TryCatchBlockNode> tryCatchBlocks, String desc, int access) {
......@@ -636,6 +686,7 @@ public class PartialEvaluator {
isOptimized |= removeUnusedJump(ilist);
isOptimized |= removeUnusedHandler(cfg, ilist, tryCatchBlocks);
isOptimized |= removePop(ilist, method);
isOptimized |= removeBoxingAndUnboxing(ilist);
if (flag) {
ilist.remove(ilist.getLast());
......
......@@ -12,11 +12,9 @@ public class ProcessorTest2 {
public static String flag;
public static void objPM(Object c, ArgumentContext ac, DynamicContext dc) {
// TODO ! add directly to the println
int index = dc.stackValue(1, int.class);
System.out.println("Testing dynamic context in processor");
System.out.println("Accessed array index is " + index);
System.out.println("Accessed array index is "
+ dc.stackValue(1, int.class));
System.out.println("--------------------");
DiSLClass.flag = "OMG this is for the End";
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment