Commit d797c942 authored by Lubomir Bulej's avatar Lubomir Bulej

DiSL: Use the InsnList iterable provided by AsmHelper.allInsnsFrom() to...

DiSL: Use the InsnList iterable provided by AsmHelper.allInsnsFrom() to copying instruction list at selected places
DiSL: UnprocessedCode: avoid instruction list copy and replace index-based insn iteration with iterable-based
DiSL: AbstractParser, ParserHelper, BodyMarker, BytecodeMarker, InsnNodeMarker, NewObjMarker, NonskippingBytecodeMarker, MaxCalculator: avoid instruction list copy
DiSL: AfterInitBodyMarker: replace use of iterator with iterable
DiSL: SnippetUnprocessedCode: replace index-based insn iteration with iterable-based, cosmetic cleanups
DiSL: BasicBlockCalc: avoid instruction list copy, kill index-based hasNextNonVirtInstr() in favor of AsmHelper.nextNonVirtualInsn
DiSL: Weaver, WeavingCode, PartialEvaluator: add comments to keep intruction list copying in place until iterator use can be verified to be OK
DiSL: WeavingCode: avoid instruction list copy when not being modified
parent b7509da8
......@@ -195,7 +195,7 @@ abstract class AbstractParser {
// first initialization instruction for some field
AbstractInsnNode firstInitInsn = origInitCodeIL.getFirst();
for (AbstractInsnNode instr : origInitCodeIL.toArray()) {
for (AbstractInsnNode instr : AsmHelper.allInsnsFrom(origInitCodeIL)) {
// if our instruction is field
if (instr instanceof FieldInsnNode) {
......
......@@ -13,6 +13,7 @@ import org.objectweb.asm.tree.VarInsnNode;
import ch.usi.dag.disl.exception.DiSLFatalException;
import ch.usi.dag.disl.exception.ParserException;
import ch.usi.dag.disl.exception.ReflectionException;
import ch.usi.dag.disl.util.AsmHelper;
import ch.usi.dag.disl.util.ReflectionHelper;
// package visible
......@@ -83,7 +84,7 @@ abstract class ParserHelper {
}
// The following code assumes that all disl snippets are static
for (AbstractInsnNode instr : instructions.toArray()) {
for (AbstractInsnNode instr : AsmHelper.allInsnsFrom (instructions)) {
switch (instr.getOpcode()) {
// test if the context is stored somewhere else
......
......@@ -55,7 +55,7 @@ public class UnprocessedCode {
Set<ThreadLocalVar> tlvList = new HashSet<ThreadLocalVar>();
for (AbstractInsnNode instr : instructions.toArray()) {
for (AbstractInsnNode instr : AsmHelper.allInsnsFrom(instructions)) {
// *** Parse synthetic local variables ***
......@@ -95,10 +95,7 @@ public class UnprocessedCode {
Map<String, StaticContextMethod> staticContexts =
new HashMap<String, StaticContextMethod>();
AbstractInsnNode[] instructionArray = instructions.toArray();
for (int i = 0; i < instructionArray.length; ++i) {
AbstractInsnNode instr = instructionArray[i];
for (AbstractInsnNode instr : AsmHelper.allInsnsFrom (instructions)) {
// *** Parse static context methods in use ***
......
......@@ -2,7 +2,6 @@ package ch.usi.dag.disl.marker;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.AbstractInsnNode;
......@@ -11,9 +10,9 @@ import org.objectweb.asm.tree.MethodNode;
import ch.usi.dag.disl.snippet.Shadow.WeavingRegion;
import ch.usi.dag.disl.util.AsmHelper;
// FIXME LB: For empty constructors, the order of After and Before snippets is reversed.
public class AfterInitBodyMarker extends AbstractMarker {
@Override
public List <MarkedRegion> mark (final MethodNode method) {
......@@ -24,10 +23,7 @@ public class AfterInitBodyMarker extends AbstractMarker {
// Add all instructions preceding the RETURN instructions
// as marked region ends.
//
final ListIterator <AbstractInsnNode>
insnIterator = method.instructions.iterator ();
while (insnIterator.hasNext ()) {
final AbstractInsnNode insn = insnIterator.next ();
for (final AbstractInsnNode insn : AsmHelper.allInsnsFrom (method.instructions)) {
int opcode = insn.getOpcode ();
if (opcode >= Opcodes.IRETURN && opcode <= Opcodes.RETURN) {
region.addEnd (insn.getPrevious ());
......
......@@ -8,6 +8,7 @@ import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.MethodNode;
import ch.usi.dag.disl.snippet.Shadow.WeavingRegion;
import ch.usi.dag.disl.util.AsmHelper;
public class BodyMarker extends AbstractMarker {
......@@ -18,7 +19,7 @@ public class BodyMarker extends AbstractMarker {
MarkedRegion region =
new MarkedRegion(method.instructions.getFirst());
for (AbstractInsnNode instr : method.instructions.toArray()) {
for (AbstractInsnNode instr : AsmHelper.allInsnsFrom (method.instructions)) {
int opcode = instr.getOpcode();
......
......@@ -10,6 +10,7 @@ import org.objectweb.asm.tree.InsnList;
import org.objectweb.asm.tree.MethodNode;
import ch.usi.dag.disl.exception.MarkerException;
import ch.usi.dag.disl.util.AsmHelper;
import ch.usi.dag.disl.util.AsmOpcodes;
public class BytecodeMarker extends AbstractDWRMarker {
......@@ -52,9 +53,7 @@ public class BytecodeMarker extends AbstractDWRMarker {
public List<MarkedRegion> markWithDefaultWeavingReg(MethodNode method) {
List<MarkedRegion> regions = new LinkedList<MarkedRegion>();
InsnList ilst = method.instructions;
for (AbstractInsnNode instruction : ilst.toArray()) {
for (AbstractInsnNode instruction : AsmHelper.allInsnsFrom (method.instructions)) {
if (searchedInstrNums.contains(instruction.getOpcode())) {
......
......@@ -9,6 +9,7 @@ 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.AsmHelper;
public class InsnNodeMarker extends AbstractInsnMarker {
......@@ -48,7 +49,7 @@ public class InsnNodeMarker extends AbstractInsnMarker {
List<AbstractInsnNode> seleted = new LinkedList<AbstractInsnNode>();
for (AbstractInsnNode instr : methodNode.instructions.toArray()) {
for (AbstractInsnNode instr : AsmHelper.allInsnsFrom (methodNode.instructions)) {
for (Class<? extends AbstractInsnNode> clazz : classes) {
......
......@@ -9,6 +9,7 @@ import org.objectweb.asm.tree.InsnList;
import org.objectweb.asm.tree.MethodInsnNode;
import org.objectweb.asm.tree.MethodNode;
import ch.usi.dag.disl.util.AsmHelper;
import ch.usi.dag.disl.util.Constants;
public class NewObjMarker extends AbstractDWRMarker {
......@@ -19,12 +20,10 @@ public class NewObjMarker extends AbstractDWRMarker {
public List<MarkedRegion> markWithDefaultWeavingReg(MethodNode method) {
List<MarkedRegion> regions = new LinkedList<MarkedRegion>();
InsnList ilst = method.instructions;
int invokedNews = 0;
// find invocation of constructor after new instruction
for (AbstractInsnNode instruction : ilst.toArray()) {
for (AbstractInsnNode instruction : AsmHelper.allInsnsFrom (method.instructions)) {
// track new instruction
if (instruction.getOpcode() == Opcodes.NEW) {
......
......@@ -9,6 +9,7 @@ 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.AsmHelper;
import ch.usi.dag.disl.util.AsmOpcodes;
public class NonskippingBytecodeMarker extends AbstractInsnMarker {
......@@ -46,7 +47,7 @@ public class NonskippingBytecodeMarker extends AbstractInsnMarker {
List<AbstractInsnNode> seleted = new LinkedList<AbstractInsnNode>();
for (AbstractInsnNode instruction : method.instructions.toArray()) {
for (AbstractInsnNode instruction : AsmHelper.allInsnsFrom (method.instructions)) {
if (searchedInstrNums.contains(instruction.getOpcode())) {
......
......@@ -96,30 +96,33 @@ public class SnippetUnprocessedCode extends UnprocessedCode {
Map<Integer, ProcInvocation> invokedProcessors =
new HashMap<Integer, ProcInvocation>();
AbstractInsnNode[] instructionArray = instructions.toArray();
for (int i = 0; i < instructionArray.length; ++i) {
AbstractInsnNode instr = instructionArray[i];
int insnIndex = 0;
for (AbstractInsnNode insn : AsmHelper.allInsnsFrom (instructions)) {
// *** Parse processors in use ***
// no other modifications to the code should be done before weaving
// otherwise, produced instruction reference can be invalid
ProcessorInfo processor =
insnInvokesProcessor(instr, i, processors, marker);
ProcessorInfo processor = insnInvokesProcessor (
insn, insnIndex, processors, marker
);
if (processor != null) {
invokedProcessors.put(processor.getInstrPos(),
processor.getProcInvoke());
continue;
invokedProcessors.put (
processor.getInstrPos (),
processor.getProcInvoke ()
);
}
insnIndex++;
}
return new SnippetCode(instructions, tryCatchBlocks,
code.getReferencedSLVs(), code.getReferencedTLVs(),
code.containsHandledException(), code.getStaticContexts(),
code.usesDynamicContext(), code.usesClassContext(),
usesProcessorContext, invokedProcessors);
return new SnippetCode(
instructions, tryCatchBlocks, code.getReferencedSLVs(),
code.getReferencedTLVs(), code.containsHandledException(),
code.getStaticContexts(), code.usesDynamicContext(),
code.usesClassContext(), usesProcessorContext,
invokedProcessors
);
}
private static class ProcessorInfo {
......
......@@ -16,34 +16,6 @@ import org.objectweb.asm.tree.TryCatchBlockNode;
public class BasicBlockCalc {
// Make sure an instruction has a valid next-instruction.
// NOTE that in asm, label might be an AbstractInsnNode. If an instruction
// is followed with a label which is the end of an instruction list, then
// it has no next instruction.
public static boolean hasNextNonVirtInstr(InsnList instrLst, int i) {
int nextInstrIndex = i + 1;
// not valid next index
if (nextInstrIndex >= instrLst.size()) {
return false;
}
AbstractInsnNode nextInstruction = instrLst.get(nextInstrIndex);
// is non-virtual instruction later in a list ?
while(nextInstruction != null) {
if(! AsmHelper.isVirtualInstr(nextInstruction)) {
return true;
}
nextInstruction = nextInstruction.getNext();
}
return false;
}
// Get basic blocks of the given method node.
public static List<AbstractInsnNode> getAll(InsnList instructions,
List<TryCatchBlockNode> tryCatchBlocks, boolean isPrecise) {
......@@ -72,12 +44,16 @@ public class BasicBlockCalc {
// IF_ACMPEQ, IF_ACMPNE, GOTO, JSR, IFNULL, and IFNONNULL.
bbStarts.add(((JumpInsnNode) instruction).label);
// goto never returns
if (opcode != Opcodes.GOTO &&
hasNextNonVirtInstr(instructions, i)) {
bbStarts.add(instruction.getNext());
if (instruction.getOpcode () != Opcodes.GOTO) {
//
// There must be a valid (non-virtual) instruction
// following a conditional/subroutine jump instruction.
//
AbstractInsnNode nextInsn = AsmHelper.nextNonVirtualInsn (instruction);
if (nextInsn != null) {
bbStarts.add (nextInsn);
}
}
break;
}
......@@ -122,8 +98,7 @@ public class BasicBlockCalc {
// sort starting instructions
List<AbstractInsnNode> bbSortedList = new ArrayList<AbstractInsnNode>();
for (AbstractInsnNode instruction : instructions.toArray()) {
for (AbstractInsnNode instruction : AsmHelper.allInsnsFrom (instructions)) {
if (bbStarts.contains(instruction)) {
bbSortedList.add(instruction);
}
......
......@@ -108,6 +108,7 @@ public class Weaver {
}
// Scan for FIELD instructions and replace with local load/store.
// TODO LB: iterate over a copy unless we are sure an iterator is OK
for (AbstractInsnNode instr : instructions.toArray()) {
int opcode = instr.getOpcode();
......
......@@ -76,6 +76,7 @@ public class WeavingCode {
// of fetching static information.
public void fixStaticInfo(SCGenerator staticInfoHolder) {
// TODO LB: iterate over a copy unless we are sure an iterator is OK
for (AbstractInsnNode instr : iList.toArray()) {
AbstractInsnNode previous = instr.getPrevious();
......@@ -112,7 +113,7 @@ public class WeavingCode {
}
public void fixClassInfo() {
// TODO LB: iterate over a copy unless we are sure an iterator is OK
for (AbstractInsnNode instr : iList.toArray()) {
AbstractInsnNode previous = instr.getPrevious();
......@@ -143,8 +144,7 @@ public class WeavingCode {
}
private void preFixDynamicInfoCheck() throws DynamicContextException {
for (AbstractInsnNode instr : iList.toArray()) {
for (AbstractInsnNode instr : AsmHelper.allInsnsFrom (iList)) {
// it is invocation...
if (instr.getOpcode() != Opcodes.INVOKEINTERFACE) {
......@@ -232,6 +232,7 @@ public class WeavingCode {
method.maxLocals++;
}
// TODO LB: iterate over a copy unless we are sure an iterator is OK
for (AbstractInsnNode instr : iList.toArray()) {
// pseudo function call
if (instr.getOpcode() != Opcodes.INVOKEINTERFACE) {
......@@ -436,6 +437,7 @@ public class WeavingCode {
}
}
// TODO LB: iterate over a copy unless we are sure an iterator is OK
for (AbstractInsnNode instr : iList.toArray()) {
AbstractInsnNode prev = instr.getPrevious();
......@@ -484,7 +486,7 @@ public class WeavingCode {
private int fixLocalIndex(InsnList src, int offset) {
int max = offset;
for (AbstractInsnNode instr : src.toArray()) {
for (AbstractInsnNode instr : AsmHelper.allInsnsFrom (src)) {
if (instr instanceof VarInsnNode) {
......@@ -517,6 +519,7 @@ public class WeavingCode {
private void fixArgumentContext(InsnList instructions, int position,
int totalCount, Type type) {
// TODO LB: iterate over a copy unless we are sure an iterator is OK
for (AbstractInsnNode instr : instructions.toArray()) {
AbstractInsnNode previous = instr.getPrevious();
......@@ -697,6 +700,7 @@ public class WeavingCode {
public void fixProcessorInfo() {
// TODO LB: iterate over a copy unless we are sure an iterator is OK
for (AbstractInsnNode instr : iList.toArray()) {
// it is invocation...
......
......@@ -15,6 +15,7 @@ import org.objectweb.asm.tree.MultiANewArrayInsnNode;
import org.objectweb.asm.tree.TryCatchBlockNode;
import org.objectweb.asm.tree.VarInsnNode;
import ch.usi.dag.disl.util.AsmHelper;
import ch.usi.dag.disl.util.cfg.BasicBlock;
import ch.usi.dag.disl.util.cfg.CtrlFlowGraph;
......@@ -52,7 +53,7 @@ public class MaxCalculator {
--maxLocals;
}
for (AbstractInsnNode instr : ilist.toArray()) {
for (AbstractInsnNode instr : AsmHelper.allInsnsFrom (ilist)) {
if (instr instanceof VarInsnNode) {
......
......@@ -304,6 +304,7 @@ public class PartialEvaluator {
boolean isOptimized = false;
// TODO LB: iterate over a copy unless we are sure an iterator is OK
for (AbstractInsnNode instr : ilist.toArray()) {
Frame<ConstValue> frame = frames.get(instr);
......@@ -432,6 +433,7 @@ public class PartialEvaluator {
CtrlFlowGraph cfg = CtrlFlowGraph.build(method);
boolean isOptimized = false;
// TODO LB: iterate over a copy unless we are sure an iterator is OK
for (AbstractInsnNode instr : ilist.toArray()) {
switch (instr.getOpcode()) {
......@@ -540,6 +542,7 @@ public class PartialEvaluator {
return;
}
// TODO LB: iterate over a copy unless we are sure an iterator is OK
for (AbstractInsnNode instr : ilist.toArray()) {
if (instr.getOpcode() == Opcodes.INVOKESPECIAL) {
Type[] args = Type
......@@ -571,6 +574,7 @@ public class PartialEvaluator {
boolean isOptimized = false;
// TODO LB: iterate over a copy unless we are sure an iterator is OK
for (AbstractInsnNode instr : ilist.toArray()) {
int opcode = instr.getOpcode();
......@@ -622,6 +626,7 @@ public class PartialEvaluator {
boolean isOptimized = false;
// TODO LB: iterate over a copy unless we are sure an iterator is OK
for (AbstractInsnNode instr : ilist.toArray()) {
int opcode = instr.getOpcode();
......
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