/*
* reserved comment block
* DO NOT REMOVE OR ALTER!
*/
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache BCEL" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache BCEL", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
*/
/**
* InstructionFinder is a tool to search for given instructions patterns,
* i.e., match sequences of instructions in an instruction list via
* regular expressions. This can be used, e.g., in order to implement
* a peep hole optimizer that looks for code patterns and replaces
* them with faster equivalents.
*
* <p>This class internally uses the <a href="http://jakarta.apache.org/regexp/">
* Regexp</a> package to search for regular expressions.
*
* A typical application would look like this:
<pre>
InstructionFinder f = new InstructionFinder(il);
String pat = "IfInstruction ICONST_0 GOTO ICONST_1 NOP (IFEQ|IFNE)";
for(Iterator i = f.search(pat, constraint); i.hasNext(); ) {
InstructionHandle[] match = (InstructionHandle[])i.next();
...
il.delete(match[1], match[5]);
...
}
</pre>
* @author <A HREF="http://www.berlin.de/~markus.dahm/">M. Dahm</A>
* @see Instruction
* @see InstructionList
*/
public class InstructionFinder {
/**
* @param il instruction list to search for given patterns
*/
reread();
}
/**
* Reread the instruction list, e.g., after you've altered the list upon a match.
*/
public final void reread() {
// Map opcodes to characters
for(int i=0; i < size; i++)
}
/**
* Map symbolic instruction names like "getfield" to a single character.
*
* @param pattern instruction pattern in lower case
* @return encoded string for a pattern such as "BranchInstruction".
*/
return result;
for(short i=0; i < NO_OPCODES; i++)
return "" + makeChar(i);
}
/**
* Replace symbolic names of instructions with the appropiate character and remove
* all white space from string. Meta characters such as +, * are ignored.
*
* @param pattern The pattern to compile
* @return translated regular expression string
*/
for(int i=0; i < size; i++) {
if(++i < size)
else
break;
}
i--;
}
}
/**
* @return the matched piece of code as an array of instruction (handles)
*/
return match;
}
/**
* Search for the given pattern in the instruction list. You can search for any valid
* opcode via its symbolic name, e.g. "istore". You can also use a super class or
* an interface name to match a whole set of instructions, e.g. "BranchInstruction" or
* "LoadInstruction". "istore" is also an alias for all "istore_x" instructions. Additional
* aliases are "if" for "ifxx", "if_icmp" for "if_icmpxx", "if_acmp" for "if_acmpxx".
*
* Consecutive instruction names must be separated by white space which will be removed
* during the compilation of the pattern.
*
* For the rest the usual pattern matching rules for regular expressions apply.<P>
* Example pattern:
* <pre>
search("BranchInstruction NOP ((IfInstruction|GOTO)+ ISTORE Instruction)*");
* </pre>
*
* <p>If you alter the instruction list upon a match such that other
* matching areas are affected, you should call reread() to update
* the finder and call search() again, because the matches are cached.
*
* @param pattern the instruction pattern to search for, where case is ignored
* @param from where to start the search in the instruction list
* @param constraint optional CodeConstraint to check the found code pattern for
* user-defined constraints
* @return iterator of matches where e.nextElement() returns an array of instruction handles
* describing the matched area
*/
{
int start = -1;
start = i; // Where to start search from (index)
break;
}
}
if(start == -1)
" not found in instruction list.");
try {
}
} catch(RESyntaxException e) {
}
return null;
}
/**
* Start search beginning from the start of the given instruction list.
*
* @param pattern the instruction pattern to search for, where case is ignored
* @return iterator of matches where e.nextElement()
* returns an array of instruction handles describing the matched
* area
*/
}
/**
* Start search beginning from `from'.
*
* @param pattern the instruction pattern to search for, where case is ignored
* @param from where to start the search in the instruction list
* @return iterator of matches where e.nextElement() returns an array of instruction handles
* describing the matched area
*/
}
/**
* Start search beginning from the start of the given instruction list.
* Check found matches with the constraint object.
*
* @param pattern the instruction pattern to search for, case is ignored
* @param constraint constraints to be checked on matching code
* @return instruction handle or `null' if the match failed
*/
}
/**
* Convert opcode number to char.
*/
}
/**
* @return the inquired instruction list
*/
/**
* Code patterns found may be checked using an additional
* user-defined constraint object whether they really match the needed criterion.
* I.e., check constraints that can not expressed with regular expressions.
*
*/
public interface CodeConstraint {
/**
* @param match array of instructions matching the requested pattern
* @return true if the matched area is really useful
*/
}
// Initialize pattern map
static {
map.put("arithmeticinstruction", "(irem|lrem|iand|ior|ineg|isub|lneg|fneg|fmul|ldiv|fadd|lxor|frem|idiv|land|ixor|ishr|fsub|lshl|fdiv|iadd|lor|dmul|lsub|ishl|imul|lmul|lushr|dneg|iushr|lshr|ddiv|drem|dadd|ladd|dsub)");
map.put("arrayinstruction", "(baload|aastore|saload|caload|fastore|lastore|iaload|castore|iastore|aaload|bastore|sastore|faload|laload|daload|dastore)");
map.put("localvariableinstruction", "(fstore|iinc|lload|dstore|dload|iload|aload|astore|istore|fload|lstore)");
map.put("cpinstruction", "(ldc2_w|invokeinterface|multianewarray|putstatic|instanceof|getstatic|checkcast|getfield|invokespecial|ldc_w|invokestatic|invokevirtual|putfield|ldc|new|anewarray)");
map.put("branchinstruction", "(ifle|if_acmpne|if_icmpeq|if_acmpeq|ifnonnull|goto_w|iflt|ifnull|if_icmpne|tableswitch|if_icmple|ifeq|if_icmplt|jsr_w|if_icmpgt|ifgt|jsr|goto|ifne|ifge|lookupswitch|if_icmpge)");
map.put("ifinstruction", "(ifeq|ifgt|if_icmpne|if_icmpeq|ifge|ifnull|ifne|if_icmple|if_icmpge|if_acmpeq|if_icmplt|if_acmpne|ifnonnull|iflt|if_icmpgt|ifle)");
map.put("typedinstruction", "(imul|lsub|aload|fload|lor|new|aaload|fcmpg|iand|iaload|lrem|idiv|d2l|isub|dcmpg|dastore|ret|f2d|f2i|drem|iinc|i2c|checkcast|frem|lreturn|astore|lushr|daload|dneg|fastore|istore|lshl|ldiv|lstore|areturn|ishr|ldc_w|invokeinterface|aastore|lxor|ishl|l2d|i2f|return|faload|sipush|iushr|caload|instanceof|invokespecial|putfield|fmul|ireturn|laload|d2f|lneg|ixor|i2l|fdiv|lastore|multianewarray|i2b|getstatic|i2d|putstatic|fcmpl|saload|ladd|irem|dload|jsr_w|dconst|dcmpl|fsub|freturn|ldc|aconst_null|castore|lmul|ldc2_w|dadd|iconst|f2l|ddiv|dstore|land|jsr|anewarray|dmul|bipush|dsub|sastore|d2i|i2s|lshr|iadd|l2i|lload|bastore|fstore|fneg|iload|fadd|baload|fconst|ior|ineg|dreturn|l2f|lconst|getfield|invokevirtual|invokestatic|iastore)");
map.put("indexedinstruction", "(lload|lstore|fload|ldc2_w|invokeinterface|multianewarray|astore|dload|putstatic|instanceof|getstatic|checkcast|getfield|invokespecial|dstore|istore|iinc|ldc_w|ret|fstore|invokestatic|iload|putfield|invokevirtual|ldc|new|aload|anewarray)");
map.put("pushinstruction", "(dup|lload|dup2|bipush|fload|ldc2_w|sipush|lconst|fconst|dload|getstatic|ldc_w|aconst_null|dconst|iload|ldc|iconst|aload)");
map.put("stackproducer", "(imul|lsub|aload|fload|lor|new|aaload|fcmpg|iand|iaload|lrem|idiv|d2l|isub|dcmpg|dup|f2d|f2i|drem|i2c|checkcast|frem|lushr|daload|dneg|lshl|ldiv|ishr|ldc_w|invokeinterface|lxor|ishl|l2d|i2f|faload|sipush|iushr|caload|instanceof|invokespecial|fmul|laload|d2f|lneg|ixor|i2l|fdiv|getstatic|i2b|swap|i2d|dup2|fcmpl|saload|ladd|irem|dload|jsr_w|dconst|dcmpl|fsub|ldc|arraylength|aconst_null|tableswitch|lmul|ldc2_w|iconst|dadd|f2l|ddiv|land|jsr|anewarray|dmul|bipush|dsub|d2i|newarray|i2s|lshr|iadd|lload|l2i|fneg|iload|fadd|baload|fconst|lookupswitch|ior|ineg|lconst|l2f|getfield|invokevirtual|invokestatic)");
map.put("stackconsumer", "(imul|lsub|lor|iflt|fcmpg|if_icmpgt|iand|ifeq|if_icmplt|lrem|ifnonnull|idiv|d2l|isub|dcmpg|dastore|if_icmpeq|f2d|f2i|drem|i2c|checkcast|frem|lreturn|astore|lushr|pop2|monitorexit|dneg|fastore|istore|lshl|ldiv|lstore|areturn|if_icmpge|ishr|monitorenter|invokeinterface|aastore|lxor|ishl|l2d|i2f|return|iushr|instanceof|invokespecial|fmul|ireturn|d2f|lneg|ixor|pop|i2l|ifnull|fdiv|lastore|i2b|if_acmpeq|ifge|swap|i2d|putstatic|fcmpl|ladd|irem|dcmpl|fsub|freturn|ifgt|castore|lmul|dadd|f2l|ddiv|dstore|land|if_icmpne|if_acmpne|dmul|dsub|sastore|ifle|d2i|i2s|lshr|iadd|l2i|bastore|fstore|fneg|fadd|ior|ineg|ifne|dreturn|l2f|if_icmple|getfield|invokevirtual|invokestatic|iastore)");
map.put("exceptionthrower", "(irem|lrem|laload|putstatic|baload|dastore|areturn|getstatic|ldiv|anewarray|iastore|castore|idiv|saload|lastore|fastore|putfield|lreturn|caload|getfield|return|aastore|freturn|newarray|instanceof|multianewarray|athrow|faload|iaload|aaload|dreturn|monitorenter|checkcast|bastore|arraylength|new|invokevirtual|sastore|ldc_w|ireturn|invokespecial|monitorexit|invokeinterface|ldc|invokestatic|daload)");
map.put("loadclass", "(multianewarray|invokeinterface|instanceof|invokespecial|putfield|checkcast|putstatic|invokevirtual|new|getstatic|invokestatic|getfield|anewarray)");
map.put("instructiontargeter", "(ifle|if_acmpne|if_icmpeq|if_acmpeq|ifnonnull|goto_w|iflt|ifnull|if_icmpne|tableswitch|if_icmple|ifeq|if_icmplt|jsr_w|if_icmpgt|ifgt|jsr|goto|ifne|ifge|lookupswitch|if_icmpge)");
// Some aliases
// Precompile some aliases first
// Compile strings
}
}
// Add instruction alias to match anything
for(short i=0; i < NO_OPCODES; i++) {
if(i < NO_OPCODES - 1)
}
}
}
}
}
/*
* Internal debugging routines.
*/
return pattern2string(pattern, true);
}
if(make_string)
else
} else
}
}
}