Sat, 24 Oct 2020 16:43:03 +0800
Merge
1 /*
2 * reserved comment block
3 * DO NOT REMOVE OR ALTER!
4 */
5 package com.sun.org.apache.bcel.internal.util;
7 /*
8 * Licensed to the Apache Software Foundation (ASF) under one or more
9 * contributor license agreements. See the NOTICE file distributed with
10 * this work for additional information regarding copyright ownership.
11 * The ASF licenses this file to You under the Apache License, Version 2.0
12 * (the "License"); you may not use this file except in compliance with
13 * the License. You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
22 *
23 */
25 import com.sun.org.apache.bcel.internal.Constants;
26 import com.sun.org.apache.bcel.internal.generic.*;
27 import java.util.*;
28 import java.util.regex.Matcher;
29 import java.util.regex.Pattern;
31 /**
32 * InstructionFinder is a tool to search for given instructions patterns,
33 * i.e., match sequences of instructions in an instruction list via
34 * regular expressions. This can be used, e.g., in order to implement
35 * a peep hole optimizer that looks for code patterns and replaces
36 * them with faster equivalents.
37 *
38 * <p>This class internally uses the <a href="http://jakarta.apache.org/regexp/">
39 * Regexp</a> package to search for regular expressions.
40 *
41 * A typical application would look like this:
42 <pre>
43 InstructionFinder f = new InstructionFinder(il);
44 String pat = "IfInstruction ICONST_0 GOTO ICONST_1 NOP (IFEQ|IFNE)";
46 for(Iterator i = f.search(pat, constraint); i.hasNext(); ) {
47 InstructionHandle[] match = (InstructionHandle[])i.next();
48 ...
49 il.delete(match[1], match[5]);
50 ...
51 }
52 </pre>
53 * @author <A HREF="http://www.berlin.de/~markus.dahm/">M. Dahm</A>
54 * @see Instruction
55 * @see InstructionList
56 */
57 public class InstructionFinder {
58 private static final int OFFSET = 32767; // char + OFFSET is outside of LATIN-1
59 private static final int NO_OPCODES = 256; // Potential number, some are not used
61 private static final HashMap map = new HashMap(); // Map<String,Pattern>
63 private InstructionList il;
64 private String il_string; // instruction list as string
65 private InstructionHandle[] handles; // map instruction list to array
67 /**
68 * @param il instruction list to search for given patterns
69 */
70 public InstructionFinder(InstructionList il) {
71 this.il = il;
72 reread();
73 }
75 /**
76 * Reread the instruction list, e.g., after you've altered the list upon a match.
77 */
78 public final void reread() {
79 int size = il.getLength();
80 char[] buf = new char[size]; // Create a string with length equal to il length
81 handles = il.getInstructionHandles();
83 // Map opcodes to characters
84 for(int i=0; i < size; i++)
85 buf[i] = makeChar(handles[i].getInstruction().getOpcode());
87 il_string = new String(buf);
88 }
90 /**
91 * Map symbolic instruction names like "getfield" to a single character.
92 *
93 * @param pattern instruction pattern in lower case
94 * @return encoded string for a pattern such as "BranchInstruction".
95 */
96 private static final String mapName(String pattern) {
97 String result = (String)map.get(pattern);
99 if(result != null)
100 return result;
102 for(short i=0; i < NO_OPCODES; i++)
103 if(pattern.equals(Constants.OPCODE_NAMES[i]))
104 return "" + makeChar(i);
106 throw new RuntimeException("Instruction unknown: " + pattern);
107 }
109 /**
110 * Replace symbolic names of instructions with the appropiate character and remove
111 * all white space from string. Meta characters such as +, * are ignored.
112 *
113 * @param pattern The pattern to compile
114 * @return translated regular expression string
115 */
116 private static final String compilePattern(String pattern) {
117 String lower = pattern.toLowerCase();
118 StringBuffer buf = new StringBuffer();
119 int size = pattern.length();
121 for(int i=0; i < size; i++) {
122 char ch = lower.charAt(i);
124 if(Character.isLetterOrDigit(ch)) {
125 StringBuffer name = new StringBuffer();
127 while((Character.isLetterOrDigit(ch) || ch == '_') && i < size) {
128 name.append(ch);
130 if(++i < size)
131 ch = lower.charAt(i);
132 else
133 break;
134 }
136 i--;
138 buf.append(mapName(name.toString()));
139 } else if(!Character.isWhitespace(ch))
140 buf.append(ch);
141 }
143 return buf.toString();
144 }
146 /**
147 * @return the matched piece of code as an array of instruction (handles)
148 */
149 private InstructionHandle[] getMatch(int matched_from, int match_length) {
150 InstructionHandle[] match = new InstructionHandle[match_length];
151 System.arraycopy(handles, matched_from, match, 0, match_length);
153 return match;
154 }
156 /**
157 * Search for the given pattern in the instruction list. You can search for any valid
158 * opcode via its symbolic name, e.g. "istore". You can also use a super class or
159 * an interface name to match a whole set of instructions, e.g. "BranchInstruction" or
160 * "LoadInstruction". "istore" is also an alias for all "istore_x" instructions. Additional
161 * aliases are "if" for "ifxx", "if_icmp" for "if_icmpxx", "if_acmp" for "if_acmpxx".
162 *
163 * Consecutive instruction names must be separated by white space which will be removed
164 * during the compilation of the pattern.
165 *
166 * For the rest the usual pattern matching rules for regular expressions apply.<P>
167 * Example pattern:
168 * <pre>
169 search("BranchInstruction NOP ((IfInstruction|GOTO)+ ISTORE Instruction)*");
170 * </pre>
171 *
172 * <p>If you alter the instruction list upon a match such that other
173 * matching areas are affected, you should call reread() to update
174 * the finder and call search() again, because the matches are cached.
175 *
176 * @param pattern the instruction pattern to search for, where case is ignored
177 * @param from where to start the search in the instruction list
178 * @param constraint optional CodeConstraint to check the found code pattern for
179 * user-defined constraints
180 * @return iterator of matches where e.nextElement() returns an array of instruction handles
181 * describing the matched area
182 */
183 public final Iterator search(String pattern, InstructionHandle from,
184 CodeConstraint constraint)
185 {
186 String search = compilePattern(pattern);
187 int start = -1;
189 for(int i=0; i < handles.length; i++) {
190 if(handles[i] == from) {
191 start = i; // Where to start search from (index)
192 break;
193 }
194 }
196 if(start == -1)
197 throw new ClassGenException("Instruction handle " + from +
198 " not found in instruction list.");
200 Pattern regex = Pattern.compile(search);
201 List<InstructionHandle[]> matches = new ArrayList<>();
202 Matcher matcher = regex.matcher(il_string);
203 while(start < il_string.length() && matcher.find(start)) {
204 int startExpr = matcher.start();
205 int endExpr = matcher.end();
206 int lenExpr = endExpr - startExpr;
207 InstructionHandle[] match = getMatch(startExpr, lenExpr);
209 if((constraint == null) || constraint.checkCode(match))
210 matches.add(match);
211 start = endExpr;
212 }
214 return matches.iterator();
215 }
217 /**
218 * Start search beginning from the start of the given instruction list.
219 *
220 * @param pattern the instruction pattern to search for, where case is ignored
221 * @return iterator of matches where e.nextElement()
222 * returns an array of instruction handles describing the matched
223 * area
224 */
225 public final Iterator search(String pattern) {
226 return search(pattern, il.getStart(), null);
227 }
229 /**
230 * Start search beginning from `from'.
231 *
232 * @param pattern the instruction pattern to search for, where case is ignored
233 * @param from where to start the search in the instruction list
234 * @return iterator of matches where e.nextElement() returns an array of instruction handles
235 * describing the matched area
236 */
237 public final Iterator search(String pattern, InstructionHandle from) {
238 return search(pattern, from, null);
239 }
241 /**
242 * Start search beginning from the start of the given instruction list.
243 * Check found matches with the constraint object.
244 *
245 * @param pattern the instruction pattern to search for, case is ignored
246 * @param constraint constraints to be checked on matching code
247 * @return instruction handle or `null' if the match failed
248 */
249 public final Iterator search(String pattern, CodeConstraint constraint) {
250 return search(pattern, il.getStart(), constraint);
251 }
253 /**
254 * Convert opcode number to char.
255 */
256 private static final char makeChar(short opcode) {
257 return (char)(opcode + OFFSET);
258 }
260 /**
261 * @return the inquired instruction list
262 */
263 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 }
279 // Initialize pattern map
281 static {
282 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)");
283 map.put("invokeinstruction", "(invokevirtual|invokeinterface|invokestatic|invokespecial)");
284 map.put("arrayinstruction", "(baload|aastore|saload|caload|fastore|lastore|iaload|castore|iastore|aaload|bastore|sastore|faload|laload|daload|dastore)");
285 map.put("gotoinstruction", "(goto|goto_w)");
286 map.put("conversioninstruction", "(d2l|l2d|i2s|d2i|l2i|i2b|l2f|d2f|f2i|i2d|i2l|f2d|i2c|f2l|i2f)");
287 map.put("localvariableinstruction", "(fstore|iinc|lload|dstore|dload|iload|aload|astore|istore|fload|lstore)");
288 map.put("loadinstruction", "(fload|dload|lload|iload|aload)");
289 map.put("fieldinstruction", "(getfield|putstatic|getstatic|putfield)");
290 map.put("cpinstruction", "(ldc2_w|invokeinterface|multianewarray|putstatic|instanceof|getstatic|checkcast|getfield|invokespecial|ldc_w|invokestatic|invokevirtual|putfield|ldc|new|anewarray)");
291 map.put("stackinstruction", "(dup2|swap|dup2_x2|pop|pop2|dup|dup2_x1|dup_x2|dup_x1)");
292 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)");
293 map.put("returninstruction", "(lreturn|ireturn|freturn|dreturn|areturn|return)");
294 map.put("storeinstruction", "(istore|fstore|dstore|astore|lstore)");
295 map.put("select", "(tableswitch|lookupswitch)");
296 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)");
297 map.put("jsrinstruction", "(jsr|jsr_w)");
298 map.put("variablelengthinstruction", "(tableswitch|jsr|goto|lookupswitch)");
299 map.put("unconditionalbranch", "(goto|jsr|jsr_w|athrow|goto_w)");
300 map.put("constantpushinstruction", "(dconst|bipush|sipush|fconst|iconst|lconst)");
301 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)");
302 map.put("popinstruction", "(fstore|dstore|pop|pop2|astore|putstatic|istore|lstore)");
303 map.put("allocationinstruction", "(multianewarray|new|anewarray|newarray)");
304 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)");
305 map.put("pushinstruction", "(dup|lload|dup2|bipush|fload|ldc2_w|sipush|lconst|fconst|dload|getstatic|ldc_w|aconst_null|dconst|iload|ldc|iconst|aload)");
306 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)");
307 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)");
308 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)");
309 map.put("loadclass", "(multianewarray|invokeinterface|instanceof|invokespecial|putfield|checkcast|putstatic|invokevirtual|new|getstatic|invokestatic|getfield|anewarray)");
310 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)");
312 // Some aliases
313 map.put("if_icmp", "(if_icmpne|if_icmpeq|if_icmple|if_icmpge|if_icmplt|if_icmpgt)");
314 map.put("if_acmp", "(if_acmpeq|if_acmpne)");
315 map.put("if", "(ifeq|ifne|iflt|ifge|ifgt|ifle)");
317 // Precompile some aliases first
318 map.put("iconst", precompile(Constants.ICONST_0, Constants.ICONST_5, Constants.ICONST_M1));
319 map.put("lconst", new String(new char[] { '(', makeChar(Constants.LCONST_0), '|',
320 makeChar(Constants.LCONST_1), ')' }));
321 map.put("dconst", new String(new char[] { '(', makeChar(Constants.DCONST_0), '|',
322 makeChar(Constants.DCONST_1), ')' }));
323 map.put("fconst", new String(new char[] { '(', makeChar(Constants.FCONST_0), '|',
324 makeChar(Constants.FCONST_1), ')' }));
326 map.put("iload", precompile(Constants.ILOAD_0, Constants.ILOAD_3, Constants.ILOAD));
327 map.put("dload", precompile(Constants.DLOAD_0, Constants.DLOAD_3, Constants.DLOAD));
328 map.put("fload", precompile(Constants.FLOAD_0, Constants.FLOAD_3, Constants.FLOAD));
329 map.put("aload", precompile(Constants.ALOAD_0, Constants.ALOAD_3, Constants.ALOAD));
331 map.put("istore", precompile(Constants.ISTORE_0, Constants.ISTORE_3, Constants.ISTORE));
332 map.put("dstore", precompile(Constants.DSTORE_0, Constants.DSTORE_3, Constants.DSTORE));
333 map.put("fstore", precompile(Constants.FSTORE_0, Constants.FSTORE_3, Constants.FSTORE));
334 map.put("astore", precompile(Constants.ASTORE_0, Constants.ASTORE_3, Constants.ASTORE));
336 // Compile strings
338 for(Iterator i = map.keySet().iterator(); i.hasNext(); ) {
339 String key = (String)i.next();
340 String value = (String)map.get(key);
342 char ch = value.charAt(1); // Omit already precompiled patterns
343 if(ch < OFFSET) {
344 map.put(key, compilePattern(value)); // precompile all patterns
345 }
346 }
348 // Add instruction alias to match anything
350 StringBuffer buf = new StringBuffer("(");
352 for(short i=0; i < NO_OPCODES; i++) {
353 if(Constants.NO_OF_OPERANDS[i] != Constants.UNDEFINED) { // Not an invalid opcode
354 buf.append(makeChar(i));
356 if(i < NO_OPCODES - 1)
357 buf.append('|');
358 }
359 }
360 buf.append(')');
362 map.put("instruction", buf.toString());
363 }
365 private static String precompile(short from, short to, short extra) {
366 StringBuffer buf = new StringBuffer("(");
368 for(short i=from; i <= to; i++) {
369 buf.append(makeChar(i));
370 buf.append('|');
371 }
373 buf.append(makeChar(extra));
374 buf.append(")");
375 return buf.toString();
376 }
378 /*
379 * Internal debugging routines.
380 */
381 private static final String pattern2string(String pattern) {
382 return pattern2string(pattern, true);
383 }
385 private static final String pattern2string(String pattern, boolean make_string) {
386 StringBuffer buf = new StringBuffer();
388 for(int i=0; i < pattern.length(); i++) {
389 char ch = pattern.charAt(i);
391 if(ch >= OFFSET) {
392 if(make_string)
393 buf.append(Constants.OPCODE_NAMES[ch - OFFSET]);
394 else
395 buf.append((int)(ch - OFFSET));
396 } else
397 buf.append(ch);
398 }
400 return buf.toString();
401 }
402 }