|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--org.apache.bcel.util.InstructionFinder
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.
This class internally uses the Regexp package to search for regular expressions. A typical application would look like this:
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]); ... }
Instruction
,
InstructionList
Inner Class Summary | |
static interface |
InstructionFinder.CodeConstraint
Code patterns found may be checked using an additional user-defined constraint object whether they really match the needed criterion. |
Constructor Summary | |
InstructionFinder(InstructionList il)
|
Method Summary | |
InstructionList |
getInstructionList()
|
void |
reread()
Reread the instruction list, e.g., after you've altered the list upon a match. |
java.util.Iterator |
search(java.lang.String pattern)
Start search beginning from the start of the given instruction list. |
java.util.Iterator |
search(java.lang.String pattern,
InstructionFinder.CodeConstraint constraint)
Start search beginning from the start of the given instruction list. |
java.util.Iterator |
search(java.lang.String pattern,
InstructionHandle from)
Start search beginning from `from'. |
java.util.Iterator |
search(java.lang.String pattern,
InstructionHandle from,
InstructionFinder.CodeConstraint constraint)
Search for the given pattern in the instruction list. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public InstructionFinder(InstructionList il)
il
- instruction list to search for given patternsMethod Detail |
public final void reread()
public final java.util.Iterator search(java.lang.String pattern, InstructionHandle from, InstructionFinder.CodeConstraint constraint)
Example pattern:
search("BranchInstruction NOP ((IfInstruction|GOTO)+ ISTORE Instruction)*");
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.
pattern
- the instruction pattern to search for, where case is ignoredfrom
- where to start the search in the instruction listconstraint
- optional CodeConstraint to check the found code pattern for
user-defined constraintspublic final java.util.Iterator search(java.lang.String pattern)
pattern
- the instruction pattern to search for, where case is ignoredpublic final java.util.Iterator search(java.lang.String pattern, InstructionHandle from)
pattern
- the instruction pattern to search for, where case is ignoredfrom
- where to start the search in the instruction listpublic final java.util.Iterator search(java.lang.String pattern, InstructionFinder.CodeConstraint constraint)
pattern
- the instruction pattern to search for, case is ignoredconstraint
- constraints to be checked on matching codepublic final InstructionList getInstructionList()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |