src/com/sun/org/apache/bcel/internal/util/InstructionFinder.java

Sat, 24 Oct 2020 16:43:03 +0800

author
aoqi
date
Sat, 24 Oct 2020 16:43:03 +0800
changeset 2116
aaee9ae4799a
parent 759
7ea027fae4d8
parent 2102
682b2794d6f3
permissions
-rw-r--r--

Merge

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

mercurial