Another clarifying rename and a bit more automation.

kNumDalvikInstructions is now kNumPackedOpcodes, there is a new
kMaxOpcodeValue, and both are generated by opcode-gen.

Change-Id: Ic46f1f52d2d21382452c8e777024f4a985ad31d3
Bonus: Reworded the switch and array data comment for clarity.
diff --git a/libdex/DexOpcodes.c b/libdex/DexOpcodes.c
index b68fb4b..0cbc518 100644
--- a/libdex/DexOpcodes.c
+++ b/libdex/DexOpcodes.c
@@ -28,7 +28,7 @@
 /*
  * Dalvik opcode names.
  */
-static const char* gOpNames[kNumDalvikInstructions] = {
+static const char* gOpNames[kNumPackedOpcodes] = {
     // BEGIN(libdex-opcode-names); GENERATED AUTOMATICALLY BY opcode-gen
     "nop",
     "move",
@@ -294,6 +294,6 @@
  */
 const char* dexGetOpcodeName(Opcode op)
 {
-    assert(op >= 0 && op < kNumDalvikInstructions);
+    assert(op >= 0 && op < kNumPackedOpcodes);
     return gOpNames[op];
 }
diff --git a/libdex/DexOpcodes.h b/libdex/DexOpcodes.h
index 368a110..fe9e9d1 100644
--- a/libdex/DexOpcodes.h
+++ b/libdex/DexOpcodes.h
@@ -31,16 +31,23 @@
 #include "DexFile.h"
 
 /*
- * the highest possible packed opcode value of a valid Dalvik opcode, plus one
+ * kMaxOpcodeValue: the highest possible raw (unpacked) opcode value
+ *
+ * kNumPackedOpcodes: the highest possible packed opcode value of a
+ * valid Dalvik opcode, plus one
  *
  * TODO: Change this once the rest of the code is prepared to deal with
  * extended opcodes.
  */
-#define kNumDalvikInstructions 256
+// BEGIN(libdex-maximum-values); GENERATED AUTOMATICALLY BY opcode-gen
+#define kMaxOpcodeValue 0xff
+#define kNumPackedOpcodes 0x100
+// END(libdex-maximum-values); GENERATED AUTOMATICALLY BY opcode-gen
 
 /*
- * Switch-statement signatures are a "NOP" followed by a code.  (A true NOP
- * is 0x0000.)
+ * Switch table and array data signatures are a code unit consisting
+ * of "NOP" (0x00) in the low-order byte and a non-zero identifying
+ * code in the high-order byte. (A true NOP is 0x0000.)
  */
 #define kPackedSwitchSignature  0x0100
 #define kSparseSwitchSignature  0x0200
@@ -325,7 +332,7 @@
  * an interpreter in C.
  */
 #define DEFINE_GOTO_TABLE(_name) \
-    static const void* _name[kNumDalvikInstructions] = {                      \
+    static const void* _name[kNumPackedOpcodes] = {                      \
         /* BEGIN(libdex-goto-table); GENERATED AUTOMATICALLY BY opcode-gen */ \
         H(OP_NOP),                                                            \
         H(OP_MOVE),                                                           \
diff --git a/libdex/InstrUtils.c b/libdex/InstrUtils.c
index 0550617..9d21a04 100644
--- a/libdex/InstrUtils.c
+++ b/libdex/InstrUtils.c
@@ -30,7 +30,7 @@
  * use that opcode, in (16-bit) code units. Unimplemented opcodes as
  * well as the "breakpoint" opcode have a width of zero.
  */
-static InstructionWidth gInstructionWidthTable[kNumDalvikInstructions] = {
+static InstructionWidth gInstructionWidthTable[kNumPackedOpcodes] = {
     // BEGIN(libdex-widths); GENERATED AUTOMATICALLY BY opcode-gen
     1, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 1, 1, 1, 1, 1,
     1, 1, 1, 2, 3, 2, 2, 3, 5, 2, 2, 3, 2, 1, 1, 2,
@@ -55,7 +55,7 @@
  * Table that maps each opcode to the flags associated with that
  * opcode.
  */
-static u1 gOpcodeFlagsTable[kNumDalvikInstructions] = {
+static u1 gOpcodeFlagsTable[kNumPackedOpcodes] = {
     // BEGIN(libdex-flags); GENERATED AUTOMATICALLY BY opcode-gen
     kInstrCanContinue,
     kInstrCanContinue,
@@ -320,7 +320,7 @@
  * Table that maps each opcode to the instruction format associated
  * that opcode.
  */
-static u1 gInstructionFormatTable[kNumDalvikInstructions] = {
+static u1 gInstructionFormatTable[kNumPackedOpcodes] = {
     // BEGIN(libdex-formats); GENERATED AUTOMATICALLY BY opcode-gen
     kFmt10x,  kFmt12x,  kFmt22x,  kFmt32x,  kFmt12x,  kFmt22x,  kFmt32x,
     kFmt12x,  kFmt22x,  kFmt32x,  kFmt11x,  kFmt11x,  kFmt11x,  kFmt11x,
@@ -366,7 +366,7 @@
  * Table that maps each opcode to the index type implied by that
  * opcode.
  */
-static u1 gInstructionIndexTypeTable[kNumDalvikInstructions] = {
+static u1 gInstructionIndexTypeTable[kNumPackedOpcodes] = {
     // BEGIN(libdex-index-types); GENERATED AUTOMATICALLY BY opcode-gen
     kIndexNone,         kIndexNone,         kIndexNone,
     kIndexNone,         kIndexNone,         kIndexNone,
diff --git a/libdex/InstrUtils.h b/libdex/InstrUtils.h
index 3d964e6..10cb2a9 100644
--- a/libdex/InstrUtils.h
+++ b/libdex/InstrUtils.h
@@ -146,7 +146,7 @@
  */
 DEX_INLINE size_t dexGetWidthFromOpcode(Opcode opcode)
 {
-    //assert(/*opcode >= 0 &&*/ opcode < kNumDalvikInstructions);
+    //assert(/*opcode >= 0 &&*/ opcode < kNumPackedOpcodes);
     return gDexOpcodeInfo.widths[opcode];
 }
 
@@ -162,7 +162,7 @@
  */
 DEX_INLINE OpcodeFlags dexGetFlagsFromOpcode(Opcode opcode)
 {
-    //assert(/*opcode >= 0 &&*/ opcode < kNumDalvikInstructions);
+    //assert(/*opcode >= 0 &&*/ opcode < kNumPackedOpcodes);
     return gDexOpcodeInfo.flags[opcode];
 }
 
@@ -179,7 +179,7 @@
  */
 DEX_INLINE InstructionFormat dexGetFormatFromOpcode(Opcode opcode)
 {
-    //assert(/*opcode >= 0 &&*/ opcode < kNumDalvikInstructions);
+    //assert(/*opcode >= 0 &&*/ opcode < kNumPackedOpcodes);
     return (InstructionFormat) gDexOpcodeInfo.formats[opcode];
 }
 
@@ -188,7 +188,7 @@
  */
 DEX_INLINE InstructionIndexType dexGetIndexTypeFromOpcode(Opcode opcode)
 {
-    //assert(/*opcode >= 0 &&*/ opcode < kNumDalvikInstructions);
+    //assert(/*opcode >= 0 &&*/ opcode < kNumPackedOpcodes);
     return (InstructionIndexType) gDexOpcodeInfo.indexTypes[opcode];
 }
 
diff --git a/opcode-gen/opcode-gen.awk b/opcode-gen/opcode-gen.awk
index 9280ed2..2656625 100644
--- a/opcode-gen/opcode-gen.awk
+++ b/opcode-gen/opcode-gen.awk
@@ -124,6 +124,13 @@
     printf("        MAXIMUM_PACKED_VALUE = %d;\n", MAX_PACKED_OPCODE);
 }
 
+emission == "libdex-maximum-values" {
+    emissionHandled = 1;
+
+    printf("#define kMaxOpcodeValue = 0x%x;\n", MAX_OPCODE);
+    printf("#define kNumPackedOpcodes = 0x%x;\n", MAX_PACKED_OPCODE + 1);
+}
+
 emission == "libdex-opcode-enum" {
     emissionHandled = 1;
 
diff --git a/vm/Globals.h b/vm/Globals.h
index fe36673..c2639cb 100644
--- a/vm/Globals.h
+++ b/vm/Globals.h
@@ -618,7 +618,7 @@
     Method**    inlinedMethods;
 
     /*
-     * Dalvik instruction counts (kNumDalvikInstructions entries).
+     * Dalvik instruction counts (kNumPackedOpcodes entries).
      */
     int*        executedInstrCounts;
     int         instructionCountEnableCount;
diff --git a/vm/Profile.c b/vm/Profile.c
index 6463dab..9b356f6 100644
--- a/vm/Profile.c
+++ b/vm/Profile.c
@@ -164,10 +164,10 @@
     /*
      * Allocate storage for instruction counters.
      */
-    gDvm.executedInstrCounts = (int*) malloc(kNumDalvikInstructions * sizeof(int));
+    gDvm.executedInstrCounts = (int*) malloc(kNumPackedOpcodes * sizeof(int));
     if (gDvm.executedInstrCounts == NULL)
         return false;
-    memset(gDvm.executedInstrCounts, 0, kNumDalvikInstructions * sizeof(int));
+    memset(gDvm.executedInstrCounts, 0, kNumPackedOpcodes * sizeof(int));
 
 #ifdef UPDATE_MAGIC_PAGE
     /*
diff --git a/vm/compiler/CompilerIR.h b/vm/compiler/CompilerIR.h
index c83c0ba..712ca4c 100644
--- a/vm/compiler/CompilerIR.h
+++ b/vm/compiler/CompilerIR.h
@@ -78,7 +78,7 @@
 } LIR;
 
 enum ExtendedMIROpcode {
-    kMirOpFirst = kNumDalvikInstructions,
+    kMirOpFirst = kNumPackedOpcodes,
     kMirOpPhi = kMirOpFirst,
     kMirOpNullNRangeUpCheck,
     kMirOpNullNRangeDownCheck,
diff --git a/vm/compiler/Dataflow.c b/vm/compiler/Dataflow.c
index 924ba62..82f52b9 100644
--- a/vm/compiler/Dataflow.c
+++ b/vm/compiler/Dataflow.c
@@ -21,7 +21,7 @@
 
 /*
  * Main table containing data flow attributes for each bytecode. The
- * first kNumDalvikInstructions entries are for Dalvik bytecode
+ * first kNumPackedOpcodes entries are for Dalvik bytecode
  * instructions, where extended opcode at the MIR level are appended
  * afterwards.
  *
diff --git a/vm/compiler/Loop.c b/vm/compiler/Loop.c
index c03d6f5..031464c 100644
--- a/vm/compiler/Loop.c
+++ b/vm/compiler/Loop.c
@@ -309,7 +309,7 @@
             dvmCompilerDataFlowAttributes[mir->dalvikInsn.opcode];
 
         /* Skip extended MIR instructions */
-        if (dInsn->opcode >= kNumDalvikInstructions) continue;
+        if (dInsn->opcode >= kNumPackedOpcodes) continue;
 
         int instrFlags = dexGetFlagsFromOpcode(dInsn->opcode);
 
diff --git a/vm/compiler/codegen/arm/CodegenCommon.c b/vm/compiler/codegen/arm/CodegenCommon.c
index 05cead3..4a20579 100644
--- a/vm/compiler/codegen/arm/CodegenCommon.c
+++ b/vm/compiler/codegen/arm/CodegenCommon.c
@@ -30,7 +30,7 @@
 static intptr_t templateEntryOffsets[TEMPLATE_LAST_MARK];
 
 /* Track exercised opcodes */
-static int opcodeCoverage[kNumDalvikInstructions];
+static int opcodeCoverage[kNumPackedOpcodes];
 
 static void setMemRefType(ArmLIR *lir, bool isLoad, int memType)
 {
diff --git a/vm/compiler/codegen/arm/CodegenDriver.c b/vm/compiler/codegen/arm/CodegenDriver.c
index 0c5b617..98236b6 100644
--- a/vm/compiler/codegen/arm/CodegenDriver.c
+++ b/vm/compiler/codegen/arm/CodegenDriver.c
@@ -4383,13 +4383,13 @@
 
     streak = i = 0;
     buf[0] = 0;
-    while (opcodeCoverage[i] == 0 && i < kNumDalvikInstructions) {
+    while (opcodeCoverage[i] == 0 && i < kNumPackedOpcodes) {
         i++;
     }
-    if (i == kNumDalvikInstructions) {
+    if (i == kNumPackedOpcodes) {
         return;
     }
-    for (start = i++, streak = 1; i < kNumDalvikInstructions; i++) {
+    for (start = i++, streak = 1; i < kNumPackedOpcodes; i++) {
         if (opcodeCoverage[i]) {
             streak++;
         } else {
@@ -4399,10 +4399,10 @@
                 sprintf(buf+strlen(buf), "%x-%x,", start, start + streak - 1);
             }
             streak = 0;
-            while (opcodeCoverage[i] == 0 && i < kNumDalvikInstructions) {
+            while (opcodeCoverage[i] == 0 && i < kNumPackedOpcodes) {
                 i++;
             }
-            if (i < kNumDalvikInstructions) {
+            if (i < kNumPackedOpcodes) {
                 streak = 1;
                 start = i;
             }
diff --git a/vm/compiler/codegen/x86/CodegenDriver.c b/vm/compiler/codegen/x86/CodegenDriver.c
index 60f003b..69f637e 100644
--- a/vm/compiler/codegen/x86/CodegenDriver.c
+++ b/vm/compiler/codegen/x86/CodegenDriver.c
@@ -24,7 +24,7 @@
  * applicable directory below this one.
  */
 
-static int opcodeCoverage[kNumDalvikInstructions];
+static int opcodeCoverage[kNumPackedOpcodes];
 static intptr_t templateEntryOffsets[TEMPLATE_LAST_MARK];
 
 /*
@@ -196,13 +196,13 @@
 
     streak = i = 0;
     buf[0] = 0;
-    while (opcodeCoverage[i] == 0 && i < kNumDalvikInstructions) {
+    while (opcodeCoverage[i] == 0 && i < kNumPackedOpcodes) {
         i++;
     }
-    if (i == kNumDalvikInstructions) {
+    if (i == kNumPackedOpcodes) {
         return;
     }
-    for (start = i++, streak = 1; i < kNumDalvikInstructions; i++) {
+    for (start = i++, streak = 1; i < kNumPackedOpcodes; i++) {
         if (opcodeCoverage[i]) {
             streak++;
         } else {
@@ -212,10 +212,10 @@
                 sprintf(buf+strlen(buf), "%x-%x,", start, start + streak - 1);
             }
             streak = 0;
-            while (opcodeCoverage[i] == 0 && i < kNumDalvikInstructions) {
+            while (opcodeCoverage[i] == 0 && i < kNumPackedOpcodes) {
                 i++;
             }
-            if (i < kNumDalvikInstructions) {
+            if (i < kNumPackedOpcodes) {
                 streak = 1;
                 start = i;
             }
diff --git a/vm/compiler/template/gen-template.py b/vm/compiler/template/gen-template.py
index addb21e..02e9107 100755
--- a/vm/compiler/template/gen-template.py
+++ b/vm/compiler/template/gen-template.py
@@ -146,7 +146,7 @@
 
 #
 # Extract an ordered list of instructions from the VM sources.  We use the
-# "goto table" definition macro, which has exactly kNumDalvikInstructions
+# "goto table" definition macro, which has exactly kNumPackedOpcodes
 # entries.
 #
 def getOpcodeList():
@@ -164,7 +164,7 @@
 
 
 #
-# Load and emit opcodes for all kNumDalvikInstructions instructions.
+# Load and emit opcodes for all kNumPackedOpcodes instructions.
 #
 def loadAndEmitOpcodes():
     sister_list = []
diff --git a/vm/mterp/Mterp.c b/vm/mterp/Mterp.c
index 2b2e0d1..f4740fe 100644
--- a/vm/mterp/Mterp.c
+++ b/vm/mterp/Mterp.c
@@ -48,7 +48,7 @@
      */
     const int width = 64;
     int interpSize = dvmAsmInstructionEnd - dvmAsmInstructionStart;
-    if (interpSize != 0 && interpSize != kNumDalvikInstructions*width) {
+    if (interpSize != 0 && interpSize != kNumPackedOpcodes*width) {
         LOGE("ERROR: unexpected asm interp size %d\n", interpSize);
         LOGE("(did an instruction handler exceed %d bytes?)\n", width);
         dvmAbort();
diff --git a/vm/mterp/README.txt b/vm/mterp/README.txt
index 058312e..9e28ebc 100644
--- a/vm/mterp/README.txt
+++ b/vm/mterp/README.txt
@@ -90,7 +90,7 @@
 
   op-end
 
-    Indicates the end of the opcode list.  All kNumDalvikInstructions
+    Indicates the end of the opcode list.  All kNumPackedOpcodes
     opcodes are emitted when this is seen, followed by any code that
     didn't fit inside the fixed-size instruction handler space.
 
@@ -108,7 +108,7 @@
 is special in two ways: (1) the sources are assumed to be C code, and
 will be inserted into the generated C file; (2) when a C implementation
 is emitted, a "glue stub" is emitted in the assembly source file.
-(The generator script always emits kNumDalvikInstructions assembly
+(The generator script always emits kNumPackedOpcodes assembly
 instructions, unless "asm-stub" was left blank, in which case it only
 emits some labels.)
 
diff --git a/vm/mterp/gen-mterp.py b/vm/mterp/gen-mterp.py
index 2c072ac..b55ed58 100755
--- a/vm/mterp/gen-mterp.py
+++ b/vm/mterp/gen-mterp.py
@@ -23,7 +23,7 @@
 from string import Template
 
 interp_defs_file = "../../libdex/DexOpcodes.h" # need opcode list
-kNumDalvikInstructions = 256 # TODO: Derive this from DexOpcodes.h.
+kNumPackedOpcodes = 256 # TODO: Derive this from DexOpcodes.h.
 
 verbose = False
 handler_size_bits = -1000
@@ -152,7 +152,7 @@
 
 #
 # Extract an ordered list of instructions from the VM sources.  We use the
-# "goto table" definition macro, which has exactly kNumDalvikInstructions
+# "goto table" definition macro, which has exactly kNumPackedOpcodes
 # entries.
 #
 def getOpcodeList():
@@ -166,19 +166,19 @@
         opcodes.append("OP_" + match.group(1))
     opcode_fp.close()
 
-    if len(opcodes) != kNumDalvikInstructions:
+    if len(opcodes) != kNumPackedOpcodes:
         print "ERROR: found %d opcodes in Interp.h (expected %d)" \
-                % (len(opcodes), kNumDalvikInstructions)
+                % (len(opcodes), kNumPackedOpcodes)
         raise SyntaxError, "bad opcode count"
     return opcodes
 
 
 #
-# Load and emit opcodes for all kNumDalvikInstructions instructions.
+# Load and emit opcodes for all kNumPackedOpcodes instructions.
 #
 def loadAndEmitOpcodes():
     sister_list = []
-    assert len(opcodes) == kNumDalvikInstructions
+    assert len(opcodes) == kNumPackedOpcodes
     need_dummy_start = False
 
     # point dvmAsmInstructionStart at the first handler or stub
@@ -187,7 +187,7 @@
     asm_fp.write("dvmAsmInstructionStart = " + label_prefix + "_OP_NOP\n")
     asm_fp.write("    .text\n\n")
 
-    for i in xrange(kNumDalvikInstructions):
+    for i in xrange(kNumPackedOpcodes):
         op = opcodes[i]
 
         if opcode_locations.has_key(op):
diff --git a/vm/native/dalvik_system_VMDebug.c b/vm/native/dalvik_system_VMDebug.c
index c5a72e4..bbb62dc 100644
--- a/vm/native/dalvik_system_VMDebug.c
+++ b/vm/native/dalvik_system_VMDebug.c
@@ -537,11 +537,11 @@
         u4 length = countArray->length;
 
         /*
-         * Ensure that we copy at most kNumDalvikInstructions
+         * Ensure that we copy at most kNumPackedOpcodes
          * elements, but no more than the length of the given array.
          */
-        if (length > kNumDalvikInstructions) {
-            length = kNumDalvikInstructions;
+        if (length > kNumPackedOpcodes) {
+            length = kNumPackedOpcodes;
         }
 
         sched_yield();
@@ -560,7 +560,7 @@
     JValue* pResult)
 {
     sched_yield();
-    memset(gDvm.executedInstrCounts, 0, kNumDalvikInstructions * sizeof(int));
+    memset(gDvm.executedInstrCounts, 0, kNumPackedOpcodes * sizeof(int));
     RETURN_VOID();
 }