📄 instructionfinder.java
字号:
package org.apache.bcel.util;/* ==================================================================== * The Apache Software License, Version 1.1 * * Copyright (c) 2001 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" and * "Apache BCEL" must not be used to endorse or promote products * derived from this software without prior written permission. For * written permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", * "Apache BCEL", nor may "Apache" appear in their name, without * prior written permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. */import java.util.*;import org.apache.bcel.Constants;import org.apache.bcel.generic.*;import org.apache.regexp.*;/** * 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. * * <p>This class internally uses the <a href="http://jakarta.apache.org/regexp/"> * Regexp</a> package to search for regular expressions. * * A typical application would look like this:<pre> 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]); ... }</pre> * @version $Id: InstructionFinder.java,v 1.1.1.1 2001/10/29 20:00:30 jvanzyl Exp $ * @author <A HREF="http://www.berlin.de/~markus.dahm/">M. Dahm</A> * @see Instruction * @see InstructionList */public class InstructionFinder { private static final int OFFSET = 32767; // char + OFFSET is outside of LATIN-1 private static final int NO_OPCODES = 256; // Potential number, some are not used private static final HashMap map = new HashMap(); // Map<String,Pattern> private InstructionList il; private String il_string; // instruction list as string private InstructionHandle[] handles; // map instruction list to array /** * @param il instruction list to search for given patterns */ public InstructionFinder(InstructionList il) { this.il = il; reread(); } /** * Reread the instruction list, e.g., after you've altered the list upon a match. */ public final void reread() { int size = il.getLength(); char[] buf = new char[size]; // Create a string with length equal to il length handles = il.getInstructionHandles(); // Map opcodes to characters for(int i=0; i < size; i++) buf[i] = makeChar(handles[i].getInstruction().getOpcode()); il_string = new String(buf); } /** * Map symbolic instruction names like "getfield" to a single character. * * @param pattern instruction pattern in lower case * @return encoded string for a pattern such as "BranchInstruction". */ private static final String mapName(String pattern) { String result = (String)map.get(pattern); if(result != null) return result; for(short i=0; i < NO_OPCODES; i++) if(pattern.equals(Constants.OPCODE_NAMES[i])) return "" + makeChar(i); throw new RuntimeException("Instruction unknown: " + pattern); } /** * Replace symbolic names of instructions with the appropiate character and remove * all white space from string. Meta characters such as +, * are ignored. * * @param pattern The pattern to compile * @return translated regular expression string */ private static final String compilePattern(String pattern) { String lower = pattern.toLowerCase(); StringBuffer buf = new StringBuffer(); int size = pattern.length(); for(int i=0; i < size; i++) { char ch = lower.charAt(i); if(Character.isLetterOrDigit(ch)) { StringBuffer name = new StringBuffer(); while((Character.isLetterOrDigit(ch) || ch == '_') && i < size) { name.append(ch); if(++i < size) ch = lower.charAt(i); else break; } i--; buf.append(mapName(name.toString())); } else if(!Character.isWhitespace(ch)) buf.append(ch); } return buf.toString(); } /** * @return the matched piece of code as an array of instruction (handles) */ private InstructionHandle[] getMatch(int matched_from, int match_length) { InstructionHandle[] match = new InstructionHandle[match_length]; System.arraycopy(handles, matched_from, match, 0, match_length); return match; } /** * 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.<P> * Example pattern: * <pre> search("BranchInstruction NOP ((IfInstruction|GOTO)+ ISTORE Instruction)*"); * </pre> * * <p>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. * * @param pattern the instruction pattern to search for, where case is ignored * @param from where to start the search in the instruction list * @param constraint optional CodeConstraint to check the found code pattern for * user-defined constraints * @return iterator of matches where e.nextElement() returns an array of instruction handles * describing the matched area */ public final Iterator search(String pattern, InstructionHandle from, CodeConstraint constraint) { String search = compilePattern(pattern); int start = -1; for(int i=0; i < handles.length; i++) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -