use of org.objectweb.asm.tree.LabelNode in project phosphor by gmu-swe.
the class LocalVariableManager method newShadowLV.
// HashSet<String> usedShadowNames = new HashSet<>();
public int newShadowLV(Type type, int shadows) {
int idx = super.newLocal(type);
Label lbl = new Label();
super.visitLabel(lbl);
String shadowName = null;
if (primitiveArrayFixer != null)
for (Object o : primitiveArrayFixer.mn.localVariables) {
LocalVariableNode lv = (LocalVariableNode) o;
int id = remap(lv.index + (lv.index < lastArg - extraLVsInArg ? 0 : extraLVsInArg), Type.getType(lv.desc));
if (id == shadows) {
shadowName = lv.name + "$$PHOSPHORTAG";
}
}
if (shadowName == null)
shadowName = "phosphorShadowLVFor" + shadows + "XX" + createdLVIdx;
LocalVariableNode newLVN = new LocalVariableNode(shadowName, type.getDescriptor(), null, new LabelNode(lbl), new LabelNode(end), idx);
createdLVs.add(newLVN);
curLocalIdxToLVNode.put(idx, newLVN);
createdLVIdx++;
shadowLVMap.put(idx, origLVMap.get(shadows));
varToShadowVar.put(shadows, idx);
return idx;
}
use of org.objectweb.asm.tree.LabelNode in project phosphor by gmu-swe.
the class LocalVariableManager method newLocal.
@Deprecated
public int newLocal(Type type) {
int idx = super.newLocal(type);
Label lbl = new Label();
super.visitLabel(lbl);
LocalVariableNode newLVN = new LocalVariableNode("phosphorShadowLV" + createdLVIdx, type.getDescriptor(), null, new LabelNode(lbl), new LabelNode(end), idx);
createdLVs.add(newLVN);
curLocalIdxToLVNode.put(idx, newLVN);
createdLVIdx++;
return idx;
}
use of org.objectweb.asm.tree.LabelNode in project BuildCraft by BuildCraft.
the class EventBusProviderASM method generateDirectHandler.
private byte[] generateDirectHandler(Method meth, Class<?> parClass, String clsName) {
ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
ClassNode node = new ClassNode();
node.name = clsName.replace('.', '/');
node.access = Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL;
node.interfaces = Lists.newArrayList(IEventHandler.class.getName().replace('.', '/'));
node.version = Opcodes.V1_6;
node.superName = "java/lang/Object";
String fd = Type.getDescriptor(meth.getDeclaringClass());
node.fields.add(new FieldNode(Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL, "listener", fd, null, null));
// This method does:
// public ClassName(ListenerObject obj) {
// super();
// this.listener = obj;
// }
{
MethodNode consturctorMethod = new MethodNode();
consturctorMethod.access = Opcodes.ACC_PUBLIC;
consturctorMethod.desc = Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(meth.getDeclaringClass()));
consturctorMethod.name = "<init>";
consturctorMethod.exceptions = Lists.newArrayList();
consturctorMethod.instructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
consturctorMethod.instructions.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false));
consturctorMethod.instructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
consturctorMethod.instructions.add(new VarInsnNode(Opcodes.ALOAD, 1));
consturctorMethod.instructions.add(new FieldInsnNode(Opcodes.PUTFIELD, node.name, "listener", fd));
consturctorMethod.instructions.add(new InsnNode(Opcodes.RETURN));
node.methods.add(consturctorMethod);
}
// This method does:
// public final void handle(Object event) {
// if (!(event instanceof EventClass)) return;
// listener.<method_name>((EventClass) event);
// }
{
MethodNode generationMethod = new MethodNode();
// public final void handle(Object)
generationMethod.access = Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL;
generationMethod.desc = Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(Object.class));
generationMethod.name = "handle";
generationMethod.exceptions = Lists.newArrayList();
// This block does:
// if (!(event instanceof EventClass)) return;
{
// ADD the first object (given as [this, event])
// -> event
generationMethod.instructions.add(new VarInsnNode(Opcodes.ALOAD, 1));
// event -> event instanceof PAR_CLASS -> boolean
generationMethod.instructions.add(new TypeInsnNode(Opcodes.INSTANCEOF, Type.getInternalName(parClass)));
LabelNode instanceLabel = new LabelNode();
// boolean -> if (boolean) GOTO instanceLabel ->
generationMethod.instructions.add(new JumpInsnNode(Opcodes.IFNE, instanceLabel));
// return;
generationMethod.instructions.add(new InsnNode(Opcodes.RETURN));
generationMethod.instructions.add(instanceLabel);
}
// This block does:
// listener.<method_name>(event);
{
generationMethod.instructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
// -> ListenerObject
String desc = Type.getDescriptor(meth.getDeclaringClass());
generationMethod.instructions.add(new FieldInsnNode(Opcodes.GETFIELD, node.name, "listener", desc));
// -> Object
generationMethod.instructions.add(new VarInsnNode(Opcodes.ALOAD, 1));
// CheckCast
generationMethod.instructions.add(new TypeInsnNode(Opcodes.CHECKCAST, Type.getInternalName(parClass)));
// ListenerObject, EventObject -> <method_name> ->
generationMethod.instructions.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, Type.getInternalName(meth.getDeclaringClass()), meth.getName(), Type.getMethodDescriptor(meth), false));
}
// return;
generationMethod.instructions.add(new InsnNode(Opcodes.RETURN));
node.methods.add(generationMethod);
}
node.accept(writer);
byte[] bytecode = writer.toByteArray();
return bytecode;
}
use of org.objectweb.asm.tree.LabelNode in project soot by Sable.
the class AsmMethodSource method emitUnits.
private void emitUnits() {
AbstractInsnNode insn = instructions.getFirst();
ArrayDeque<LabelNode> labls = new ArrayDeque<LabelNode>();
while (insn != null) {
// Save the label to assign it to the next real unit
if (insn instanceof LabelNode)
labls.add((LabelNode) insn);
// Get the unit associated with the current instruction
Unit u = units.get(insn);
if (u == null) {
insn = insn.getNext();
continue;
}
emitUnits(u);
// If this is an exception handler, register the starting unit for it
{
IdentityStmt caughtEx = null;
if (u instanceof IdentityStmt)
caughtEx = (IdentityStmt) u;
else if (u instanceof UnitContainer)
caughtEx = getIdentityRefFromContrainer((UnitContainer) u);
if (insn instanceof LabelNode && caughtEx != null && caughtEx.getRightOp() instanceof CaughtExceptionRef) {
// We directly place this label
Collection<UnitBox> traps = trapHandlers.get((LabelNode) insn);
for (UnitBox ub : traps) ub.setUnit(caughtEx);
}
}
// Register this unit for all targets of the labels ending up at it
while (!labls.isEmpty()) {
LabelNode ln = labls.poll();
Collection<UnitBox> boxes = labels.get(ln);
if (boxes != null) {
for (UnitBox box : boxes) {
box.setUnit(u instanceof UnitContainer ? ((UnitContainer) u).getFirstUnit() : u);
}
}
}
insn = insn.getNext();
}
// Emit the inline exception handlers
for (LabelNode ln : this.inlineExceptionHandlers.keySet()) {
Unit handler = this.inlineExceptionHandlers.get(ln);
emitUnits(handler);
Collection<UnitBox> traps = trapHandlers.get(ln);
for (UnitBox ub : traps) ub.setUnit(handler);
// We need to jump to the original implementation
Unit targetUnit = units.get(ln);
GotoStmt gotoImpl = Jimple.v().newGotoStmt(targetUnit);
body.getUnits().add(gotoImpl);
}
/* set remaining labels & boxes to last unit of chain */
if (labls.isEmpty())
return;
Unit end = Jimple.v().newNopStmt();
body.getUnits().add(end);
while (!labls.isEmpty()) {
LabelNode ln = labls.poll();
Collection<UnitBox> boxes = labels.get(ln);
if (boxes != null) {
for (UnitBox box : boxes) box.setUnit(end);
}
}
}
use of org.objectweb.asm.tree.LabelNode in project soot by Sable.
the class AsmMethodSource method convert.
private void convert() {
ArrayDeque<Edge> worklist = new ArrayDeque<Edge>();
for (LabelNode ln : trapHandlers.keySet()) {
if (checkInlineExceptionHandler(ln))
handleInlineExceptionHandler(ln, worklist);
else
worklist.add(new Edge(ln, new ArrayList<Operand>()));
}
worklist.add(new Edge(instructions.getFirst(), new ArrayList<Operand>()));
conversionWorklist = worklist;
edges = HashBasedTable.create(1, 1);
do {
Edge edge = worklist.pollLast();
AbstractInsnNode insn = edge.insn;
stack = edge.stack;
edge.stack = null;
do {
int type = insn.getType();
if (type == FIELD_INSN) {
convertFieldInsn((FieldInsnNode) insn);
} else if (type == IINC_INSN) {
convertIincInsn((IincInsnNode) insn);
} else if (type == INSN) {
convertInsn((InsnNode) insn);
int op = insn.getOpcode();
if ((op >= IRETURN && op <= RETURN) || op == ATHROW) {
break;
}
} else if (type == INT_INSN) {
convertIntInsn((IntInsnNode) insn);
} else if (type == LDC_INSN) {
convertLdcInsn((LdcInsnNode) insn);
} else if (type == JUMP_INSN) {
JumpInsnNode jmp = (JumpInsnNode) insn;
convertJumpInsn(jmp);
int op = jmp.getOpcode();
if (op == JSR)
throw new UnsupportedOperationException("JSR!");
if (op != GOTO) {
/* ifX opcode, i.e. two successors */
AbstractInsnNode next = insn.getNext();
addEdges(insn, next, Collections.singletonList(jmp.label));
} else {
addEdges(insn, jmp.label, null);
}
break;
} else if (type == LOOKUPSWITCH_INSN) {
LookupSwitchInsnNode swtch = (LookupSwitchInsnNode) insn;
convertLookupSwitchInsn(swtch);
LabelNode dflt = swtch.dflt;
addEdges(insn, dflt, swtch.labels);
break;
} else if (type == METHOD_INSN) {
convertMethodInsn((MethodInsnNode) insn);
} else if (type == INVOKE_DYNAMIC_INSN) {
convertInvokeDynamicInsn((InvokeDynamicInsnNode) insn);
} else if (type == MULTIANEWARRAY_INSN) {
convertMultiANewArrayInsn((MultiANewArrayInsnNode) insn);
} else if (type == TABLESWITCH_INSN) {
TableSwitchInsnNode swtch = (TableSwitchInsnNode) insn;
convertTableSwitchInsn(swtch);
LabelNode dflt = swtch.dflt;
addEdges(insn, dflt, swtch.labels);
} else if (type == TYPE_INSN) {
convertTypeInsn((TypeInsnNode) insn);
} else if (type == VAR_INSN) {
if (insn.getOpcode() == RET)
throw new UnsupportedOperationException("RET!");
convertVarInsn((VarInsnNode) insn);
} else if (type == LABEL) {
convertLabel((LabelNode) insn);
} else if (type == LINE) {
convertLine((LineNumberNode) insn);
} else if (type == FRAME) {
// we can ignore it
} else
throw new RuntimeException("Unknown instruction type: " + type);
} while ((insn = insn.getNext()) != null);
} while (!worklist.isEmpty());
conversionWorklist = null;
edges = null;
}
Aggregations