From: Dan Bornstein Date: Mon, 8 Nov 2010 21:13:54 +0000 (-0800) Subject: Another bit of cleanup. X-Git-Tag: android-x86-4.0-r1~250^2~53 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=25f7980a51488db2ebcd7822c83c3f4b25a2a730;p=android-x86%2Fdalvik.git Another bit of cleanup. Making it a little easier to have more than 256 opcodes. Change-Id: I050050f2b6078407dde4d4f214dec2c1a331000c --- diff --git a/dx/src/com/android/dx/dex/code/DalvOps.java b/dx/src/com/android/dx/dex/code/DalvOps.java index 722a6510c..f57089b95 100644 --- a/dx/src/com/android/dx/dex/code/DalvOps.java +++ b/dx/src/com/android/dx/dex/code/DalvOps.java @@ -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; + } } diff --git a/dx/src/com/android/dx/dex/code/Dop.java b/dx/src/com/android/dx/dex/code/Dop.java index 8a51606e2..565d8f9d0 100644 --- a/dx/src/com/android/dx/dex/code/Dop.java +++ b/dx/src/com/android/dx/dex/code/Dop.java @@ -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"); }