Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

org.apache.bcel.util
Class InstructionFinder  view InstructionFinder download InstructionFinder.java

java.lang.Object
  extended byorg.apache.bcel.util.InstructionFinder

public class InstructionFinder
extends java.lang.Object

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]);
   ...
   }
   
  
 

Version:
$Id: InstructionFinder.java 386056 2006-03-15 11:31:56Z tcurdt $

Nested 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.
 
Field Summary
private  org.apache.bcel.generic.InstructionHandle[] handles
           
private  org.apache.bcel.generic.InstructionList il
           
private  java.lang.String il_string
           
private static java.util.Map map
           
private static int NO_OPCODES
           
private static int OFFSET
           
 
Constructor Summary
InstructionFinder(org.apache.bcel.generic.InstructionList il)
           
 
Method Summary
private static java.lang.String compilePattern(java.lang.String pattern)
          Replace symbolic names of instructions with the appropiate character and remove all white space from string.
 org.apache.bcel.generic.InstructionList getInstructionList()
           
private  org.apache.bcel.generic.InstructionHandle[] getMatch(int matched_from, int match_length)
           
private static char makeChar(short opcode)
          Convert opcode number to char.
private static java.lang.String mapName(java.lang.String pattern)
          Map symbolic instruction names like "getfield" to a single character.
private static java.lang.String pattern2string(java.lang.String pattern)
           
private static java.lang.String pattern2string(java.lang.String pattern, boolean make_string)
           
private static java.lang.String precompile(short from, short to, short extra)
           
 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, org.apache.bcel.generic.InstructionHandle from)
          Start search beginning from `from'.
 java.util.Iterator search(java.lang.String pattern, org.apache.bcel.generic.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
 

Field Detail

OFFSET

private static final int OFFSET
See Also:
Constant Field Values

NO_OPCODES

private static final int NO_OPCODES
See Also:
Constant Field Values

map

private static final java.util.Map map

il

private org.apache.bcel.generic.InstructionList il

il_string

private java.lang.String il_string

handles

private org.apache.bcel.generic.InstructionHandle[] handles
Constructor Detail

InstructionFinder

public InstructionFinder(org.apache.bcel.generic.InstructionList il)
Method Detail

reread

public final void reread()
Reread the instruction list, e.g., after you've altered the list upon a match.


mapName

private static final java.lang.String mapName(java.lang.String pattern)
Map symbolic instruction names like "getfield" to a single character.


compilePattern

private static final java.lang.String compilePattern(java.lang.String pattern)
Replace symbolic names of instructions with the appropiate character and remove all white space from string. Meta characters such as +, * are ignored.


getMatch

private org.apache.bcel.generic.InstructionHandle[] getMatch(int matched_from,
                                                             int match_length)

search

public final java.util.Iterator search(java.lang.String pattern,
                                       org.apache.bcel.generic.InstructionHandle from,
                                       InstructionFinder.CodeConstraint constraint)
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.

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.


search

public final java.util.Iterator search(java.lang.String pattern)
Start search beginning from the start of the given instruction list.


search

public final java.util.Iterator search(java.lang.String pattern,
                                       org.apache.bcel.generic.InstructionHandle from)
Start search beginning from `from'.


search

public final java.util.Iterator search(java.lang.String pattern,
                                       InstructionFinder.CodeConstraint constraint)
Start search beginning from the start of the given instruction list. Check found matches with the constraint object.


makeChar

private static final char makeChar(short opcode)
Convert opcode number to char.


getInstructionList

public final org.apache.bcel.generic.InstructionList getInstructionList()

precompile

private static java.lang.String precompile(short from,
                                           short to,
                                           short extra)

pattern2string

private static final java.lang.String pattern2string(java.lang.String pattern)

pattern2string

private static final java.lang.String pattern2string(java.lang.String pattern,
                                                     boolean make_string)