1   package org.apache.bcel.util;
2   
3   /* ====================================================================
4    * The Apache Software License, Version 1.1
5    *
6    * Copyright (c) 2001 The Apache Software Foundation.  All rights
7    * reserved.
8    *
9    * Redistribution and use in source and binary forms, with or without
10   * modification, are permitted provided that the following conditions
11   * are met:
12   *
13   * 1. Redistributions of source code must retain the above copyright
14   *    notice, this list of conditions and the following disclaimer.
15   *
16   * 2. Redistributions in binary form must reproduce the above copyright
17   *    notice, this list of conditions and the following disclaimer in
18   *    the documentation and/or other materials provided with the
19   *    distribution.
20   *
21   * 3. The end-user documentation included with the redistribution,
22   *    if any, must include the following acknowledgment:
23   *       "This product includes software developed by the
24   *        Apache Software Foundation (http://www.apache.org/)."
25   *    Alternately, this acknowledgment may appear in the software itself,
26   *    if and wherever such third-party acknowledgments normally appear.
27   *
28   * 4. The names "Apache" and "Apache Software Foundation" and
29   *    "Apache BCEL" must not be used to endorse or promote products
30   *    derived from this software without prior written permission. For
31   *    written permission, please contact apache@apache.org.
32   *
33   * 5. Products derived from this software may not be called "Apache",
34   *    "Apache BCEL", nor may "Apache" appear in their name, without
35   *    prior written permission of the Apache Software Foundation.
36   *
37   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48   * SUCH DAMAGE.
49   * ====================================================================
50   *
51   * This software consists of voluntary contributions made by many
52   * individuals on behalf of the Apache Software Foundation.  For more
53   * information on the Apache Software Foundation, please see
54   * <http://www.apache.org/>.
55   */
56  
57  import java.util.*;
58  import org.apache.bcel.Constants;
59  import org.apache.bcel.generic.*;
60  import org.apache.regexp.*;
61  
62  /*** 
63   * InstructionFinder is a tool to search for given instructions patterns,
64   * i.e., match sequences of instructions in an instruction list via
65   * regular expressions. This can be used, e.g., in order to implement
66   * a peep hole optimizer that looks for code patterns and replaces
67   * them with faster equivalents.
68   *
69   * <p>This class internally uses the <a href="http://jakarta.apache.org/regexp/">
70   * Regexp</a> package to search for regular expressions.
71   *
72   * A typical application would look like this:
73  <pre>
74      InstructionFinder f   = new InstructionFinder(il);
75      String            pat = "IfInstruction ICONST_0 GOTO ICONST_1 NOP (IFEQ|IFNE)";
76      
77      for(Iterator i = f.search(pat, constraint); i.hasNext(); ) {
78        InstructionHandle[] match = (InstructionHandle[])i.next();
79        ...
80        il.delete(match[1], match[5]);
81        ...
82      }
83  </pre>
84   * @version $Id: InstructionFinder.java,v 1.1.1.1 2001/10/29 20:00:30 jvanzyl Exp $
85   * @author  <A HREF="http://www.berlin.de/~markus.dahm/">M. Dahm</A>
86   * @see Instruction
87   * @see InstructionList
88   */
89  public class InstructionFinder {
90    private static final int OFFSET     = 32767; // char + OFFSET is outside of LATIN-1
91    private static final int NO_OPCODES = 256;   // Potential number, some are not used
92  
93    private static final HashMap map = new HashMap(); // Map<String,Pattern>
94  
95    private InstructionList     il;
96    private String              il_string;    // instruction list as string
97    private InstructionHandle[] handles;      // map instruction list to array
98  
99    /***
100    * @param il instruction list to search for given patterns
101    */
102   public InstructionFinder(InstructionList il) {
103     this.il = il;
104     reread();
105   }
106 
107   /***
108    * Reread the instruction list, e.g., after you've altered the list upon a match.
109    */
110   public final void reread() {
111     int    size  = il.getLength();
112     char[] buf   = new char[size]; // Create a string with length equal to il length
113     handles      = il.getInstructionHandles();
114 
115     // Map opcodes to characters
116     for(int i=0; i < size; i++)
117       buf[i] = makeChar(handles[i].getInstruction().getOpcode());
118 
119     il_string = new String(buf);
120   }
121 
122   /***
123    * Map symbolic instruction names like "getfield" to a single character.
124    *
125    * @param pattern instruction pattern in lower case
126    * @return encoded string for a pattern such as "BranchInstruction".
127    */
128   private static final String mapName(String pattern) {
129     String result = (String)map.get(pattern);
130 
131     if(result != null)
132       return result;
133 
134     for(short i=0; i < NO_OPCODES; i++)
135       if(pattern.equals(Constants.OPCODE_NAMES[i]))
136 	return "" + makeChar(i);
137 
138     throw new RuntimeException("Instruction unknown: " + pattern);
139   }
140 
141   /***
142    * Replace symbolic names of instructions with the appropiate character and remove
143    * all white space from string. Meta characters such as +, * are ignored.
144    *
145    * @param pattern The pattern to compile
146    * @return translated regular expression string
147    */
148   private static final String compilePattern(String pattern) {
149     String       lower      = pattern.toLowerCase();
150     StringBuffer buf        = new StringBuffer();
151     int          size       = pattern.length();
152 
153     for(int i=0; i < size; i++) {
154       char ch = lower.charAt(i);
155       
156       if(Character.isLetterOrDigit(ch)) {
157 	StringBuffer name = new StringBuffer();
158 	
159 	while((Character.isLetterOrDigit(ch) || ch == '_') && i < size) {
160 	  name.append(ch);
161 
162 	  if(++i < size)
163 	    ch = lower.charAt(i);
164 	  else
165 	    break;
166 	}
167 	
168 	i--;
169 
170 	buf.append(mapName(name.toString()));
171       } else if(!Character.isWhitespace(ch))
172 	buf.append(ch);
173     }
174 
175     return buf.toString();
176   }
177 
178   /***
179    * @return the matched piece of code as an array of instruction (handles)
180    */
181   private InstructionHandle[] getMatch(int matched_from, int match_length) {
182     InstructionHandle[] match = new InstructionHandle[match_length];
183     System.arraycopy(handles, matched_from, match, 0, match_length);
184 
185     return match;
186   }
187 
188   /***
189    * Search for the given pattern in the instruction list. You can search for any valid
190    * opcode via its symbolic name, e.g. "istore". You can also use a super class or
191    * an interface name to match a whole set of instructions, e.g. "BranchInstruction" or
192    * "LoadInstruction". "istore" is also an alias for all "istore_x" instructions. Additional
193    * aliases are "if" for "ifxx", "if_icmp" for "if_icmpxx", "if_acmp" for "if_acmpxx".
194    *
195    * Consecutive instruction names must be separated by white space which will be removed
196    * during the compilation of the pattern.
197    *
198    * For the rest the usual pattern matching rules for regular expressions apply.<P>
199    * Example pattern:
200    * <pre>
201      search("BranchInstruction NOP ((IfInstruction|GOTO)+ ISTORE Instruction)*");
202    * </pre>
203    *
204    * <p>If you alter the instruction list upon a match such that other
205    * matching areas are affected, you should call reread() to update
206    * the finder and call search() again, because the matches are cached.
207    *
208    * @param pattern the instruction pattern to search for, where case is ignored
209    * @param from where to start the search in the instruction list
210    * @param constraint optional CodeConstraint to check the found code pattern for
211    * user-defined constraints
212    * @return iterator of matches where e.nextElement() returns an array of instruction handles
213    * describing the matched area 
214    */
215   public final Iterator search(String pattern, InstructionHandle from,
216 			       CodeConstraint constraint)
217   {
218     String search = compilePattern(pattern);
219     int  start    = -1;
220 
221     for(int i=0; i < handles.length; i++) {
222       if(handles[i] == from) {
223 	start = i; // Where to start search from (index)
224 	break;
225       }
226     }
227 
228     if(start == -1)
229       throw new ClassGenException("Instruction handle " + from + 
230 				  " not found in instruction list.");
231     try {
232       RE regex = new RE(search);
233       ArrayList matches = new ArrayList();
234 
235       while(start < il_string.length() && regex.match(il_string, start)) {
236 	int startExpr = regex.getParenStart(0); 
237 	int endExpr   = regex.getParenEnd(0);
238 	int lenExpr   = regex.getParenLength(0);
239 	
240 	InstructionHandle[] match = getMatch(startExpr, lenExpr);
241 
242 	if((constraint == null) || constraint.checkCode(match))
243 	  matches.add(match);
244 	start = endExpr;
245       }
246 
247       return matches.iterator();
248     } catch(RESyntaxException e) {
249       System.err.println(e);
250     }
251 
252     return null;
253   }
254 
255   /***
256    * Start search beginning from the start of the given instruction list.
257    *
258    * @param pattern the instruction pattern to search for, where case is ignored
259    * @return iterator of matches where e.nextElement()
260    * returns an array of instruction handles describing the matched
261    * area
262    */
263   public final Iterator search(String pattern) {
264     return search(pattern, il.getStart(), null);
265   }
266 
267   /***
268    * Start search beginning from `from'.
269    *
270    * @param pattern the instruction pattern to search for, where case is ignored
271    * @param from where to start the search in the instruction list
272    * @return  iterator of matches where e.nextElement() returns an array of instruction handles
273    * describing the matched area
274    */
275   public final Iterator search(String pattern, InstructionHandle from) {
276     return search(pattern, from, null);
277   }
278 
279   /***
280    * Start search beginning from the start of the given instruction list.
281    * Check found matches with the constraint object.
282    *
283    * @param pattern the instruction pattern to search for, case is ignored
284    * @param constraint constraints to be checked on matching code
285    * @return instruction handle or `null' if the match failed
286    */
287   public final Iterator search(String pattern, CodeConstraint constraint) {
288     return search(pattern, il.getStart(), constraint);
289   }
290 
291   /***
292    * Convert opcode number to char.
293    */
294   private static final char makeChar(short opcode) {
295     return (char)(opcode + OFFSET);
296   }
297 
298   /***
299    * @return the inquired instruction list
300    */
301   public final InstructionList getInstructionList() { return il; }
302 
303   /***
304    * Code patterns found may be checked using an additional
305    * user-defined constraint object whether they really match the needed criterion.
306    * I.e., check constraints that can not expressed with regular expressions.
307    *
308    */
309   public interface CodeConstraint {
310     /***
311      * @param match array of instructions matching the requested pattern
312      * @return true if the matched area is really useful
313      */
314     public boolean checkCode(InstructionHandle[] match);
315   }
316 
317   // Initialize pattern map
318 
319   static {
320     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)");
321     map.put("invokeinstruction", "(invokevirtual|invokeinterface|invokestatic|invokespecial)");
322     map.put("arrayinstruction", "(baload|aastore|saload|caload|fastore|lastore|iaload|castore|iastore|aaload|bastore|sastore|faload|laload|daload|dastore)");
323     map.put("gotoinstruction", "(goto|goto_w)");
324     map.put("conversioninstruction", "(d2l|l2d|i2s|d2i|l2i|i2b|l2f|d2f|f2i|i2d|i2l|f2d|i2c|f2l|i2f)");
325     map.put("localvariableinstruction", "(fstore|iinc|lload|dstore|dload|iload|aload|astore|istore|fload|lstore)");
326     map.put("loadinstruction", "(fload|dload|lload|iload|aload)");
327     map.put("fieldinstruction", "(getfield|putstatic|getstatic|putfield)");
328     map.put("cpinstruction", "(ldc2_w|invokeinterface|multianewarray|putstatic|instanceof|getstatic|checkcast|getfield|invokespecial|ldc_w|invokestatic|invokevirtual|putfield|ldc|new|anewarray)");
329     map.put("stackinstruction", "(dup2|swap|dup2_x2|pop|pop2|dup|dup2_x1|dup_x2|dup_x1)");
330     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)");
331     map.put("returninstruction", "(lreturn|ireturn|freturn|dreturn|areturn|return)");
332     map.put("storeinstruction", "(istore|fstore|dstore|astore|lstore)");
333     map.put("select", "(tableswitch|lookupswitch)");
334     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)");
335     map.put("jsrinstruction", "(jsr|jsr_w)");
336     map.put("variablelengthinstruction", "(tableswitch|jsr|goto|lookupswitch)");
337     map.put("unconditionalbranch", "(goto|jsr|jsr_w|athrow|goto_w)");
338     map.put("constantpushinstruction", "(dconst|bipush|sipush|fconst|iconst|lconst)");
339     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)");
340     map.put("popinstruction", "(fstore|dstore|pop|pop2|astore|putstatic|istore|lstore)");
341     map.put("allocationinstruction", "(multianewarray|new|anewarray|newarray)");
342     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)");
343     map.put("pushinstruction", "(dup|lload|dup2|bipush|fload|ldc2_w|sipush|lconst|fconst|dload|getstatic|ldc_w|aconst_null|dconst|iload|ldc|iconst|aload)");
344     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)");
345     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)");
346     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)");
347     map.put("loadclass", "(multianewarray|invokeinterface|instanceof|invokespecial|putfield|checkcast|putstatic|invokevirtual|new|getstatic|invokestatic|getfield|anewarray)");
348     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)");
349 
350     // Some aliases
351     map.put("if_icmp", "(if_icmpne|if_icmpeq|if_icmple|if_icmpge|if_icmplt|if_icmpgt)");
352     map.put("if_acmp", "(if_acmpeq|if_acmpne)");
353     map.put("if", "(ifeq|ifne|iflt|ifge|ifgt|ifle)");
354 
355     // Precompile some aliases first
356     map.put("iconst", precompile(Constants.ICONST_0, Constants.ICONST_5, Constants.ICONST_M1));
357     map.put("lconst", new String(new char[] { '(', makeChar(Constants.LCONST_0), '|',
358 					      makeChar(Constants.LCONST_1), ')' }));
359     map.put("dconst", new String(new char[] { '(', makeChar(Constants.DCONST_0), '|',
360 					      makeChar(Constants.DCONST_1), ')' }));
361     map.put("fconst", new String(new char[] { '(', makeChar(Constants.FCONST_0), '|',
362 					      makeChar(Constants.FCONST_1), ')' }));
363 
364     map.put("iload", precompile(Constants.ILOAD_0, Constants.ILOAD_3, Constants.ILOAD));
365     map.put("dload", precompile(Constants.DLOAD_0, Constants.DLOAD_3, Constants.DLOAD));
366     map.put("fload", precompile(Constants.FLOAD_0, Constants.FLOAD_3, Constants.FLOAD));
367     map.put("aload", precompile(Constants.ALOAD_0, Constants.ALOAD_3, Constants.ALOAD));
368 
369     map.put("istore", precompile(Constants.ISTORE_0, Constants.ISTORE_3, Constants.ISTORE));
370     map.put("dstore", precompile(Constants.DSTORE_0, Constants.DSTORE_3, Constants.DSTORE));
371     map.put("fstore", precompile(Constants.FSTORE_0, Constants.FSTORE_3, Constants.FSTORE));
372     map.put("astore", precompile(Constants.ASTORE_0, Constants.ASTORE_3, Constants.ASTORE));
373 
374     // Compile strings
375 
376     for(Iterator i = map.keySet().iterator(); i.hasNext(); ) {
377       String key   = (String)i.next();
378       String value = (String)map.get(key);
379 
380       char ch = value.charAt(1); // Omit already precompiled patterns
381       if(ch < OFFSET) {
382 	map.put(key, compilePattern(value)); // precompile all patterns
383       }
384     }
385 
386     // Add instruction alias to match anything
387 
388     StringBuffer buf = new StringBuffer("(");
389     
390     for(short i=0; i < NO_OPCODES; i++) {
391       if(Constants.NO_OF_OPERANDS[i] != Constants.UNDEFINED) { // Not an invalid opcode
392 	buf.append(makeChar(i));
393 
394 	if(i < NO_OPCODES - 1)
395 	  buf.append('|');
396       }
397     }
398     buf.append(')');
399 
400     map.put("instruction", buf.toString());
401   }
402 
403   private static String precompile(short from, short to, short extra) {
404     StringBuffer buf = new StringBuffer("(");
405 
406     for(short i=from; i <= to; i++) {
407       buf.append(makeChar(i));
408       buf.append('|');
409     }
410 
411     buf.append(makeChar(extra));
412     buf.append(")");
413     return buf.toString();
414   }
415 
416   /*
417    * Internal debugging routines.
418    */
419   private static final String pattern2string(String pattern) {
420     return pattern2string(pattern, true);
421   }
422 
423   private static final String pattern2string(String pattern, boolean make_string) {
424     StringBuffer buf = new StringBuffer();
425 
426     for(int i=0; i < pattern.length(); i++) {
427       char ch = pattern.charAt(i);
428 
429       if(ch >= OFFSET) {
430 	if(make_string)
431 	  buf.append(Constants.OPCODE_NAMES[ch - OFFSET]);
432 	else
433 	  buf.append((int)(ch - OFFSET));
434       } else
435 	buf.append(ch);
436     }
437 
438     return buf.toString();
439   }
440 }
This page was automatically generated by Maven