OSDN Git Service

Another bit of cleanup.
authorDan Bornstein <danfuzz@android.com>
Mon, 8 Nov 2010 21:13:54 +0000 (13:13 -0800)
committerDan Bornstein <danfuzz@android.com>
Mon, 8 Nov 2010 21:13:54 +0000 (13:13 -0800)
Making it a little easier to have more than 256 opcodes.

Change-Id: I050050f2b6078407dde4d4f214dec2c1a331000c

dx/src/com/android/dx/dex/code/DalvOps.java
dx/src/com/android/dx/dex/code/Dop.java

index 722a651..f57089b 100644 (file)
@@ -298,4 +298,32 @@ public final class DalvOps {
     private DalvOps() {
         // This space intentionally left blank.
     }
+
+    /**
+     * Determines if the given opcode has the right "shape" to be
+     * valid. This includes the range {@code 0x00..0xfe}, the range
+     * {@code 0x00ff..0xffff} where the low-order byte is {@code
+     * 0xff}, and the special opcode values {@code SPECIAL_FORMAT} and
+     * {@code NO_NEXT}. Note that not all of the opcode values that
+     * pass this test are in fact used. This method is meant to
+     * perform a quick check to reject blatantly wrong values (e.g.
+     * when validating arguments).
+     *
+     * @param opcode the opcode value
+     * @return {@code true} iff the value has the right "shape" to be
+     * possibly valid
+     */
+    public static boolean isValidShape(int opcode) {
+        // Note: SPECIAL_FORMAT == NO_NEXT.
+        if ((opcode >= SPECIAL_FORMAT) && (opcode <= 0xff)) {
+            return true;
+        }
+
+        if ((opcode >= 0xff) && (opcode <= 0xffff)
+                && ((opcode & 0xff) == 0xff)) {
+            return true;
+        }
+
+        return false;
+    }
 }
index 8a51606..565d8f9 100644 (file)
@@ -20,22 +20,17 @@ package com.android.dx.dex.code;
  * Representation of an opcode.
  */
 public final class Dop {
-    /**
-     * {@code DalvOps.MIN_VALUE..DalvOps.MAX_VALUE;} the opcode value
-     * itself
-     */
+    /** {@code DalvOps.isValid();} the opcode value itself */
     private final int opcode;
 
-    /**
-     * {@code DalvOps.MIN_VALUE..DalvOps.MAX_VALUE;} the opcode family
-     */
+    /** {@code DalvOps.isValid();} the opcode family */
     private final int family;
 
     /**
-     * {@code DalvOps.MIN_VALUE..DalvOps.MAX_VALUE;} what opcode (by
-     * number) to try next when attempting to match an opcode to
-     * particular arguments; {@code DalvOps.NO_NEXT} to indicate that
-     * this is the last opcode to try in a particular chain
+     * {@code DalvOps.isValid();} what opcode (by number) to try next
+     * when attempting to match an opcode to particular arguments;
+     * {@code DalvOps.NO_NEXT} to indicate that this is the last
+     * opcode to try in a particular chain
      */
     private final int nextOpcode;
 
@@ -51,15 +46,13 @@ public final class Dop {
     /**
      * Constructs an instance.
      *
-     * @param opcode {@code DalvOps.MIN_VALUE..DalvOps.MAX_VALUE;} the opcode
-     * value itself
-     * @param family {@code DalvOps.MIN_VALUE..DalvOps.MAX_VALUE;} the
-     * opcode family
-     * @param nextOpcode {@code DalvOps.NO_NEXT..DalvOps.MAX_VALUE;}
-     * what opcode (by number) to try next when attempting to match an
-     * opcode to particular arguments; {@code DalvOps.NO_NEXT} to
-     * indicate that this is the last opcode to try in a particular
-     * chain
+     * @param opcode {@code DalvOps.isValid();} the opcode value
+     * itself
+     * @param family {@code DalvOps.isValid();} the opcode family
+     * @param nextOpcode {@code DalvOps.isValid();} what opcode (by
+     * number) to try next when attempting to match an opcode to
+     * particular arguments; {@code DalvOps.NO_NEXT} to indicate that
+     * this is the last opcode to try in a particular chain
      * @param format {@code non-null;} the instruction format
      * @param hasResult whether the opcode has a result register; if so it
      * is always the first register
@@ -67,16 +60,15 @@ public final class Dop {
      */
     public Dop(int opcode, int family, int nextOpcode, InsnFormat format,
             boolean hasResult, String name) {
-        if ((opcode < DalvOps.MIN_VALUE) || (opcode > DalvOps.MAX_VALUE)) {
+        if (!DalvOps.isValidShape(opcode)) {
             throw new IllegalArgumentException("bogus opcode");
         }
 
-        if ((family < DalvOps.MIN_VALUE) || (family > DalvOps.MAX_VALUE)) {
+        if (!DalvOps.isValidShape(family)) {
             throw new IllegalArgumentException("bogus family");
         }
 
-        if ((nextOpcode < DalvOps.MIN_VALUE)
-                || (nextOpcode > DalvOps.MAX_VALUE)) {
+        if (!DalvOps.isValidShape(nextOpcode)) {
             throw new IllegalArgumentException("bogus nextOpcode");
         }