/**
* Peephole optimization: Remove sequences of [ALOAD, POP].
*/
private void peepHoleOptimization(MethodGenerator methodGen) {
InstructionList il = methodGen.getInstructionList();
InstructionFinder find = new InstructionFinder(il);
InstructionHandle ih;
String pattern;
// Remove seqences of ALOAD, POP (GTM)
pattern = "`ALOAD'`POP'`Instruction'";
for(Iterator iter=find.search(pattern); iter.hasNext();){
InstructionHandle[] match = (InstructionHandle[])iter.next();
try {
if ((!match[0].hasTargeters()) && (!match[1].hasTargeters())) {
il.delete(match[0], match[1]);
}
}
catch (TargetLostException e) {
// TODO: move target down into the list
}
}
// Replace sequences of ILOAD_?, ALOAD_?, SWAP with ALOAD_?, ILOAD_?
pattern = "`ILOAD'`ALOAD'`SWAP'`Instruction'";
for(Iterator iter=find.search(pattern); iter.hasNext();){
InstructionHandle[] match = (InstructionHandle[])iter.next();
try {
org.apache.bcel.generic.Instruction iload;
org.apache.bcel.generic.Instruction aload;
if ((!match[0].hasTargeters()) &&
(!match[1].hasTargeters()) &&
(!match[2].hasTargeters())) {
iload = match[0].getInstruction();
aload = match[1].getInstruction();
il.insert(match[0], aload);
il.insert(match[0], iload);
il.delete(match[0], match[2]);
}
}
catch (TargetLostException e) {
// TODO: move target down into the list
}
}
// Replace sequences of ALOAD_1, ALOAD_1 with ALOAD_1, DUP
pattern = "`ALOAD_1'`ALOAD_1'`Instruction'";
for(Iterator iter=find.search(pattern); iter.hasNext();){
InstructionHandle[] match = (InstructionHandle[])iter.next();
try {
org.apache.bcel.generic.Instruction iload;
org.apache.bcel.generic.Instruction aload;
if ((!match[0].hasTargeters()) && (!match[1].hasTargeters())) {