Commit 7606d670 authored by Yudi Zheng's avatar Yudi Zheng

Non skipping bytecode marker and instruction node marker.

parent cae98d0c
package ch.usi.dag.disl.marker;
import java.util.LinkedList;
import java.util.List;
import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.MethodNode;
import ch.usi.dag.disl.snippet.Shadow.WeavingRegion;
public abstract class AbstractInsnMarker extends AbstractMarker {
@Override
public final List<MarkedRegion> mark(MethodNode methodNode) {
List<MarkedRegion> regions = new LinkedList<MarkedRegion>();
for (AbstractInsnNode instruction : markInstruction(methodNode)) {
MarkedRegion region = new MarkedRegion(instruction, instruction);
region.setWeavingRegion(new WeavingRegion(instruction,
new LinkedList<AbstractInsnNode>(region.getEnds()),
instruction, instruction));
regions.add(region);
}
return regions;
}
public abstract List<AbstractInsnNode> markInstruction(MethodNode methodNode);
}
package ch.usi.dag.disl.marker;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.MethodNode;
import ch.usi.dag.disl.exception.MarkerException;
public class InsnNodeMarker extends AbstractInsnMarker {
protected Set<Class<? extends AbstractInsnNode>> classes;
public InsnNodeMarker(Parameter param)
throws MarkerException {
classes = new HashSet<Class<? extends AbstractInsnNode>>();
// translate all instructions to opcodes
for (String className : param.getMultipleValues()) {
try {
Class<?> clazz = Class.forName(className);
classes.add(clazz.asSubclass(AbstractInsnNode.class));
} catch (ClassNotFoundException e) {
throw new MarkerException("Instruction Node Class \""
+ className + "\" cannot be found.");
} catch (ClassCastException e) {
throw new MarkerException("Class \"" + className
+ "\" is not an instruction node class.");
}
}
if (classes.isEmpty()) {
throw new MarkerException(
"Instruction node class should be passed as a parameter.");
}
}
@Override
public List<AbstractInsnNode> markInstruction(MethodNode methodNode) {
List<AbstractInsnNode> seleted = new LinkedList<AbstractInsnNode>();
for (AbstractInsnNode instr : methodNode.instructions.toArray()) {
for (Class<? extends AbstractInsnNode> clazz : classes) {
if (clazz.isInstance(instr)) {
seleted.add(instr);
}
}
}
return seleted;
}
}
package ch.usi.dag.disl.marker;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.MethodNode;
import ch.usi.dag.disl.exception.MarkerException;
import ch.usi.dag.disl.util.AsmOpcodes;
public class NonskippingBytecodeMarker extends AbstractInsnMarker {
protected Set<Integer> searchedInstrNums = new HashSet<Integer>();
public NonskippingBytecodeMarker(Parameter param) throws MarkerException {
// translate all instructions to opcodes
for (String instr : param.getMultipleValues()) {
try {
AsmOpcodes opcode = AsmOpcodes.valueOf(instr.trim()
.toUpperCase());
searchedInstrNums.add(opcode.getNumber());
} catch (IllegalArgumentException e) {
throw new MarkerException("Instruction \"" + instr
+ "\" cannot be found. See "
+ AsmOpcodes.class.getName()
+ " enum for list of possible instructions");
}
}
if (searchedInstrNums.isEmpty()) {
throw new MarkerException("Bytecode marker cannot operate without"
+ " selected instructions. Pass instruction list using"
+ " \"param\" annotation attribute.");
}
}
@Override
public List<AbstractInsnNode> markInstruction(MethodNode method) {
List<AbstractInsnNode> seleted = new LinkedList<AbstractInsnNode>();
for (AbstractInsnNode instruction : method.instructions.toArray()) {
if (searchedInstrNums.contains(instruction.getOpcode())) {
seleted.add(instruction);
}
}
return seleted;
}
}
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