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