summaryrefslogtreecommitdiffstats
path: root/opcode-gen
diff options
context:
space:
mode:
authorDan Bornstein <danfuzz@android.com>2010-11-16 12:21:50 -0800
committerDan Bornstein <danfuzz@android.com>2010-11-16 12:21:50 -0800
commit82dbe1c22c6a1088f73f1e11e5cacbdd492ca79d (patch)
treebe7286a5ced5ad7ee5f84ae4ee7f542beab3bae5 /opcode-gen
parenta1d6b0ea1a367188fceb7df60bf7f7cd41548898 (diff)
downloadandroid_dalvik-82dbe1c22c6a1088f73f1e11e5cacbdd492ca79d.tar.gz
android_dalvik-82dbe1c22c6a1088f73f1e11e5cacbdd492ca79d.tar.bz2
android_dalvik-82dbe1c22c6a1088f73f1e11e5cacbdd492ca79d.zip
Promote opcode-gen to the dalvik directory.
This tool will soon be used to generate code in libdex, so it doesn't make sense for it to live in the dx directory. Change-Id: I63ff7ccceac08fbe314fb0782dc8f2b5d75d155a
Diffstat (limited to 'opcode-gen')
-rw-r--r--opcode-gen/bytecode.txt374
-rwxr-xr-xopcode-gen/opcode-gen375
-rwxr-xr-xopcode-gen/regen-all42
3 files changed, 791 insertions, 0 deletions
diff --git a/opcode-gen/bytecode.txt b/opcode-gen/bytecode.txt
new file mode 100644
index 000000000..1dfd9f16f
--- /dev/null
+++ b/opcode-gen/bytecode.txt
@@ -0,0 +1,374 @@
+# Copyright (C) 2007 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+#
+# Bytecode definition file
+#
+
+# One line per instruction format family. Each line consists of a
+# series of instruction formats that all take (potentially) compatible
+# arguments. The order is the preferred order (most to least
+# preferable) of formats, when more than one opcode could be used for
+# a given instruction's arguments.
+#
+# Note: The family that starts with 12x has a mix of both two- and
+# three- register formats. This is because some of the two-register
+# opcodes effectively take three, with a destination and two sources
+# where the destination and one of the sources have to be the same.
+
+# Regular formats
+format 10t 20t 30t
+format 10x
+format 11n 21s 21h 31i 51l
+format 11x
+format 12x 22x 23x 32x 33x # See note, above.
+format 21c 31c 41c
+format 21t 31t
+format 22b 22s 32s
+format 22c 52c
+format 22t
+format 35c 3rc 5rc
+
+# Optimized formats
+format 00x # not in spec; used for breakpoint
+format 20bc # not in spec; used for throw-verification-error
+format 22cs
+format 35mi
+format 35ms
+format 3rmi
+format 3rms
+
+# One line per opcode. Columns are:
+# hex for opcode
+# opcode name
+# format
+# has result register; one of:
+# y
+# n
+# index type; one of:
+# none
+# varies
+# type-ref
+# string-ref
+# method-ref
+# field-ref
+# inline-method
+# vtable-offset
+# field-offset
+# flags; pipe-combined combo of one or more of:
+# optimized -- optimized; not to be included in unoptimized dex files
+# branch -- might branch to an address
+# continue -- might continue to the next address in sequence
+# switch -- is a switch
+# throw -- might throw an exception
+# return -- is a return from method
+# invoke -- is a method invoke
+
+# Regular opcodes (with a couple holes)
+op 00 nop 10x n none continue
+op 01 move 12x y none continue
+op 02 move/from16 22x y none continue
+op 03 move/16 32x y none continue
+op 04 move-wide 12x y none continue
+op 05 move-wide/from16 22x y none continue
+op 06 move-wide/16 32x y none continue
+op 07 move-object 12x y none continue
+op 08 move-object/from16 22x y none continue
+op 09 move-object/16 32x y none continue
+op 0a move-result 11x y none continue
+op 0b move-result-wide 11x y none continue
+op 0c move-result-object 11x y none continue
+op 0d move-exception 11x y none continue
+op 0e return-void 10x n none return
+op 0f return 11x n none return
+op 10 return-wide 11x n none return
+op 11 return-object 11x n none return
+op 12 const/4 11n y none continue
+op 13 const/16 21s y none continue
+op 14 const 31i y none continue
+op 15 const/high16 21h y none continue
+op 16 const-wide/16 21s y none continue
+op 17 const-wide/32 31i y none continue
+op 18 const-wide 51l y none continue
+op 19 const-wide/high16 21h y none continue
+op 1a const-string 21c y string-ref continue|throw
+op 1b const-string/jumbo 31c y string-ref continue|throw
+op 1c const-class 21c y type-ref continue|throw
+op 1d monitor-enter 11x n none continue|throw
+op 1e monitor-exit 11x n none continue|throw
+op 1f check-cast 21c y type-ref continue|throw
+op 20 instance-of 22c y type-ref continue|throw
+op 21 array-length 12x y none continue|throw
+op 22 new-instance 21c y type-ref continue|throw
+op 23 new-array 22c y type-ref continue|throw
+op 24 filled-new-array 35c n type-ref continue|throw
+op 25 filled-new-array/range 3rc n type-ref continue|throw
+op 26 fill-array-data 31t n none continue
+op 27 throw 11x n none throw
+op 28 goto 10t n none branch
+op 29 goto/16 20t n none branch
+op 2a goto/32 30t n none branch
+op 2b packed-switch 31t n none continue|switch
+op 2c sparse-switch 31t n none continue|switch
+op 2d cmpl-float 23x y none continue
+op 2e cmpg-float 23x y none continue
+op 2f cmpl-double 23x y none continue
+op 30 cmpg-double 23x y none continue
+op 31 cmp-long 23x y none continue
+op 32 if-eq 22t n none continue|branch
+op 33 if-ne 22t n none continue|branch
+op 34 if-lt 22t n none continue|branch
+op 35 if-ge 22t n none continue|branch
+op 36 if-gt 22t n none continue|branch
+op 37 if-le 22t n none continue|branch
+op 38 if-eqz 21t n none continue|branch
+op 39 if-nez 21t n none continue|branch
+op 3a if-ltz 21t n none continue|branch
+op 3b if-gez 21t n none continue|branch
+op 3c if-gtz 21t n none continue|branch
+op 3d if-lez 21t n none continue|branch
+# unused: op 3e..43
+op 44 aget 23x y none continue|throw
+op 45 aget-wide 23x y none continue|throw
+op 46 aget-object 23x y none continue|throw
+op 47 aget-boolean 23x y none continue|throw
+op 48 aget-byte 23x y none continue|throw
+op 49 aget-char 23x y none continue|throw
+op 4a aget-short 23x y none continue|throw
+op 4b aput 23x n none continue|throw
+op 4c aput-wide 23x n none continue|throw
+op 4d aput-object 23x n none continue|throw
+op 4e aput-boolean 23x n none continue|throw
+op 4f aput-byte 23x n none continue|throw
+op 50 aput-char 23x n none continue|throw
+op 51 aput-short 23x n none continue|throw
+op 52 iget 22c y field-ref continue|throw
+op 53 iget-wide 22c y field-ref continue|throw
+op 54 iget-object 22c y field-ref continue|throw
+op 55 iget-boolean 22c y field-ref continue|throw
+op 56 iget-byte 22c y field-ref continue|throw
+op 57 iget-char 22c y field-ref continue|throw
+op 58 iget-short 22c y field-ref continue|throw
+op 59 iput 22c n field-ref continue|throw
+op 5a iput-wide 22c n field-ref continue|throw
+op 5b iput-object 22c n field-ref continue|throw
+op 5c iput-boolean 22c n field-ref continue|throw
+op 5d iput-byte 22c n field-ref continue|throw
+op 5e iput-char 22c n field-ref continue|throw
+op 5f iput-short 22c n field-ref continue|throw
+op 60 sget 21c y field-ref continue|throw
+op 61 sget-wide 21c y field-ref continue|throw
+op 62 sget-object 21c y field-ref continue|throw
+op 63 sget-boolean 21c y field-ref continue|throw
+op 64 sget-byte 21c y field-ref continue|throw
+op 65 sget-char 21c y field-ref continue|throw
+op 66 sget-short 21c y field-ref continue|throw
+op 67 sput 21c n field-ref continue|throw
+op 68 sput-wide 21c n field-ref continue|throw
+op 69 sput-object 21c n field-ref continue|throw
+op 6a sput-boolean 21c n field-ref continue|throw
+op 6b sput-byte 21c n field-ref continue|throw
+op 6c sput-char 21c n field-ref continue|throw
+op 6d sput-short 21c n field-ref continue|throw
+op 6e invoke-virtual 35c n method-ref continue|throw|invoke
+op 6f invoke-super 35c n method-ref continue|throw|invoke
+op 70 invoke-direct 35c n method-ref continue|throw|invoke
+op 71 invoke-static 35c n method-ref continue|throw|invoke
+op 72 invoke-interface 35c n method-ref continue|throw|invoke
+# unused: op 73
+op 74 invoke-virtual/range 3rc n method-ref continue|throw|invoke
+op 75 invoke-super/range 3rc n method-ref continue|throw|invoke
+op 76 invoke-direct/range 3rc n method-ref continue|throw|invoke
+op 77 invoke-static/range 3rc n method-ref continue|throw|invoke
+op 78 invoke-interface/range 3rc n method-ref continue|throw|invoke
+# unused: op 79..7a
+op 7b neg-int 12x y none continue
+op 7c not-int 12x y none continue
+op 7d neg-long 12x y none continue
+op 7e not-long 12x y none continue
+op 7f neg-float 12x y none continue
+op 80 neg-double 12x y none continue
+op 81 int-to-long 12x y none continue
+op 82 int-to-float 12x y none continue
+op 83 int-to-double 12x y none continue
+op 84 long-to-int 12x y none continue
+op 85 long-to-float 12x y none continue
+op 86 long-to-double 12x y none continue
+op 87 float-to-int 12x y none continue
+op 88 float-to-long 12x y none continue
+op 89 float-to-double 12x y none continue
+op 8a double-to-int 12x y none continue
+op 8b double-to-long 12x y none continue
+op 8c double-to-float 12x y none continue
+op 8d int-to-byte 12x y none continue
+op 8e int-to-char 12x y none continue
+op 8f int-to-short 12x y none continue
+op 90 add-int 23x y none continue
+op 91 sub-int 23x y none continue
+op 92 mul-int 23x y none continue
+op 93 div-int 23x y none continue|throw
+op 94 rem-int 23x y none continue|throw
+op 95 and-int 23x y none continue
+op 96 or-int 23x y none continue
+op 97 xor-int 23x y none continue
+op 98 shl-int 23x y none continue
+op 99 shr-int 23x y none continue
+op 9a ushr-int 23x y none continue
+op 9b add-long 23x y none continue
+op 9c sub-long 23x y none continue
+op 9d mul-long 23x y none continue
+op 9e div-long 23x y none continue|throw
+op 9f rem-long 23x y none continue|throw
+op a0 and-long 23x y none continue
+op a1 or-long 23x y none continue
+op a2 xor-long 23x y none continue
+op a3 shl-long 23x y none continue
+op a4 shr-long 23x y none continue
+op a5 ushr-long 23x y none continue
+op a6 add-float 23x y none continue
+op a7 sub-float 23x y none continue
+op a8 mul-float 23x y none continue
+op a9 div-float 23x y none continue
+op aa rem-float 23x y none continue
+op ab add-double 23x y none continue
+op ac sub-double 23x y none continue
+op ad mul-double 23x y none continue
+op ae div-double 23x y none continue
+op af rem-double 23x y none continue
+op b0 add-int/2addr 12x y none continue
+op b1 sub-int/2addr 12x y none continue
+op b2 mul-int/2addr 12x y none continue
+op b3 div-int/2addr 12x y none continue|throw
+op b4 rem-int/2addr 12x y none continue|throw
+op b5 and-int/2addr 12x y none continue
+op b6 or-int/2addr 12x y none continue
+op b7 xor-int/2addr 12x y none continue
+op b8 shl-int/2addr 12x y none continue
+op b9 shr-int/2addr 12x y none continue
+op ba ushr-int/2addr 12x y none continue
+op bb add-long/2addr 12x y none continue
+op bc sub-long/2addr 12x y none continue
+op bd mul-long/2addr 12x y none continue
+op be div-long/2addr 12x y none continue|throw
+op bf rem-long/2addr 12x y none continue|throw
+op c0 and-long/2addr 12x y none continue
+op c1 or-long/2addr 12x y none continue
+op c2 xor-long/2addr 12x y none continue
+op c3 shl-long/2addr 12x y none continue
+op c4 shr-long/2addr 12x y none continue
+op c5 ushr-long/2addr 12x y none continue
+op c6 add-float/2addr 12x y none continue
+op c7 sub-float/2addr 12x y none continue
+op c8 mul-float/2addr 12x y none continue
+op c9 div-float/2addr 12x y none continue
+op ca rem-float/2addr 12x y none continue
+op cb add-double/2addr 12x y none continue
+op cc sub-double/2addr 12x y none continue
+op cd mul-double/2addr 12x y none continue
+op ce div-double/2addr 12x y none continue
+op cf rem-double/2addr 12x y none continue
+op d0 add-int/lit16 22s y none continue
+op d1 rsub-int 22s y none continue
+op d2 mul-int/lit16 22s y none continue
+op d3 div-int/lit16 22s y none continue|throw
+op d4 rem-int/lit16 22s y none continue|throw
+op d5 and-int/lit16 22s y none continue
+op d6 or-int/lit16 22s y none continue
+op d7 xor-int/lit16 22s y none continue
+op d8 add-int/lit8 22b y none continue
+op d9 rsub-int/lit8 22b y none continue
+op da mul-int/lit8 22b y none continue
+op db div-int/lit8 22b y none continue|throw
+op dc rem-int/lit8 22b y none continue|throw
+op dd and-int/lit8 22b y none continue
+op de or-int/lit8 22b y none continue
+op df xor-int/lit8 22b y none continue
+op e0 shl-int/lit8 22b y none continue
+op e1 shr-int/lit8 22b y none continue
+op e2 ushr-int/lit8 22b y none continue
+
+# Optimized opcodes (not valid in an unoptimized dex file)
+#
+# Note: Technically "breakpoint" isn't really an optimized opcode, but
+# it fits the label in terms of not being valid in dex files.
+op e3 iget-volatile 22c y field-ref optimized|continue|throw
+op e4 iput-volatile 22c n field-ref optimized|continue|throw
+op e5 sget-volatile 21c y field-ref optimized|continue|throw
+op e6 sput-volatile 21c n field-ref optimized|continue|throw
+op e7 iget-object-volatile 22c y field-ref optimized|continue|throw
+op e8 iget-wide-volatile 22c y field-ref optimized|continue|throw
+op e9 iput-wide-volatile 22c n field-ref optimized|continue|throw
+op ea sget-wide-volatile 21c y field-ref optimized|continue|throw
+op eb sput-wide-volatile 21c n field-ref optimized|continue|throw
+op ec breakpoint 00x n none optimized|continue|throw
+op ed throw-verification-error 20bc n varies optimized|throw
+op ee execute-inline 35mi n inline-method optimized|continue|throw
+op ef execute-inline/range 3rmi n inline-method optimized|continue|throw
+op f0 invoke-direct-empty 35c n method-ref optimized|continue|throw|invoke
+op f1 return-void-barrier 10x n none optimized|return
+op f2 iget-quick 22cs y field-offset optimized|continue|throw
+op f3 iget-wide-quick 22cs y field-offset optimized|continue|throw
+op f4 iget-object-quick 22cs y field-offset optimized|continue|throw
+op f5 iput-quick 22cs n field-offset optimized|continue|throw
+op f6 iput-wide-quick 22cs n field-offset optimized|continue|throw
+op f7 iput-object-quick 22cs n field-offset optimized|continue|throw
+op f8 invoke-virtual-quick 35ms n vtable-offset optimized|continue|throw|invoke
+op f9 invoke-virtual-quick/range 3rms n vtable-offset optimized|continue|throw|invoke
+op fa invoke-super-quick 35ms n vtable-offset optimized|continue|throw|invoke
+op fb invoke-super-quick/range 3rms n vtable-offset optimized|continue|throw|invoke
+op fc iput-object-volatile 22c n field-ref optimized|continue|throw
+op fd sget-object-volatile 21c y field-ref optimized|continue|throw
+op fe sput-object-volatile 21c n field-ref optimized|continue|throw
+
+# Extended-width opcodes
+op 00ff const-class/jumbo 41c y type-ref continue|throw
+op 01ff check-cast/jumbo 41c n type-ref continue|throw
+op 02ff instance-of/jumbo 52c y type-ref continue|throw
+op 03ff new-instance/jumbo 41c y type-ref continue|throw
+op 04ff new-array/jumbo 52c y type-ref continue|throw
+op 05ff filled-new-array/jumbo 5rc n type-ref continue|throw
+op 06ff iget/jumbo 52c y field-ref continue|throw
+op 07ff iget-wide/jumbo 52c y field-ref continue|throw
+op 08ff iget-object/jumbo 52c y field-ref continue|throw
+op 09ff iget-boolean/jumbo 52c y field-ref continue|throw
+op 0aff iget-byte/jumbo 52c y field-ref continue|throw
+op 0bff iget-char/jumbo 52c y field-ref continue|throw
+op 0cff iget-short/jumbo 52c y field-ref continue|throw
+op 0dff iput/jumbo 52c n field-ref continue|throw
+op 0eff iput-wide/jumbo 52c n field-ref continue|throw
+op 0fff iput-object/jumbo 52c n field-ref continue|throw
+op 10ff iput-boolean/jumbo 52c n field-ref continue|throw
+op 11ff iput-byte/jumbo 52c n field-ref continue|throw
+op 12ff iput-char/jumbo 52c n field-ref continue|throw
+op 13ff iput-short/jumbo 52c n field-ref continue|throw
+op 14ff sget/jumbo 41c y field-ref continue|throw
+op 15ff sget-wide/jumbo 41c y field-ref continue|throw
+op 16ff sget-object/jumbo 41c y field-ref continue|throw
+op 17ff sget-boolean/jumbo 41c y field-ref continue|throw
+op 18ff sget-byte/jumbo 41c y field-ref continue|throw
+op 19ff sget-char/jumbo 41c y field-ref continue|throw
+op 1aff sget-short/jumbo 41c y field-ref continue|throw
+op 1bff sput/jumbo 41c n field-ref continue|throw
+op 1cff sput-wide/jumbo 41c n field-ref continue|throw
+op 1dff sput-object/jumbo 41c n field-ref continue|throw
+op 1eff sput-boolean/jumbo 41c n field-ref continue|throw
+op 1fff sput-byte/jumbo 41c n field-ref continue|throw
+op 20ff sput-char/jumbo 41c n field-ref continue|throw
+op 21ff sput-short/jumbo 41c n field-ref continue|throw
+op 22ff invoke-virtual/jumbo 5rc n method-ref continue|throw|invoke
+op 23ff invoke-super/jumbo 5rc n method-ref continue|throw|invoke
+op 24ff invoke-direct/jumbo 5rc n method-ref continue|throw|invoke
+op 25ff invoke-static/jumbo 5rc n method-ref continue|throw|invoke
+op 26ff invoke-interface/jumbo 5rc n method-ref continue|throw|invoke
+# unused: op 27ff..ffff
diff --git a/opcode-gen/opcode-gen b/opcode-gen/opcode-gen
new file mode 100755
index 000000000..f57026815
--- /dev/null
+++ b/opcode-gen/opcode-gen
@@ -0,0 +1,375 @@
+#!/bin/bash
+#
+# Copyright (C) 2007 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# opcode-gen <file>
+#
+# This script uses the file bytecodes.txt (in this directory) to
+# generate code inside the given <file>, based on the directives found
+# in that file:
+#
+# opcodes: static final ints for each opcode (no optimized ops)
+# dops: static final objects for each opcode (no optimized ops)
+# dops-init: initialization code for the "dops" (no optimized ops)
+# first-opcodes: a comment indicating which opcodes are at the head
+# position of instruction fitting chains (no optimized ops)
+
+file="$1"
+tmpfile="/tmp/$$.txt"
+
+echo "processing `basename $1`"
+
+if [ "x$1" = "x" ]; then
+ echo "must specify a file"
+ exit 1
+fi
+
+# Set up prog to be the path of this script, including following symlinks,
+# and set up progdir to be the fully-qualified pathname of its directory.
+prog="$0"
+while [ -h "${prog}" ]; do
+ newProg=`/bin/ls -ld "${prog}"`
+ newProg=`expr "${newProg}" : ".* -> \(.*\)$"`
+ if expr "x${newProg}" : 'x/' >/dev/null; then
+ prog="${newProg}"
+ else
+ progdir=`dirname "${prog}"`
+ prog="${progdir}/${newProg}"
+ fi
+done
+oldwd=`pwd`
+progdir=`dirname "${prog}"`
+cd "${progdir}"
+progdir=`pwd`
+prog="${progdir}"/`basename "${prog}"`
+cd "${oldwd}"
+
+bytecodeFile="$progdir/bytecode.txt"
+
+awk -v "bytecodeFile=$bytecodeFile" '
+
+BEGIN {
+ MAX_OPCODE = 65535;
+ initIndexTypes();
+ initFlags();
+ if (readBytecodes()) exit 1;
+ deriveOpcodeChains();
+ consumeUntil = "";
+}
+
+consumeUntil != "" {
+ if (index($0, consumeUntil) != 0) {
+ consumeUntil = "";
+ } else {
+ next;
+ }
+}
+
+/BEGIN\(opcodes\)/ {
+ consumeUntil = "END(opcodes)";
+ print;
+
+ for (i = 0; i <= MAX_OPCODE; i++) {
+ if (isUnused(i) || isOptimized(i)) continue;
+ printf(" public static final int %s = 0x%s;\n",
+ uppername[i], hex[i]);
+ }
+
+ next;
+}
+
+/BEGIN\(first-opcodes\)/ {
+ consumeUntil = "END(first-opcodes)";
+ print;
+
+ for (i = 0; i <= MAX_OPCODE; i++) {
+ if (isUnused(i) || isOptimized(i)) continue;
+ if (isFirst[i] == "true") {
+ printf(" // DalvOps.%s\n", uppername[i]);
+ }
+ }
+
+ next;
+}
+
+/BEGIN\(dops\)/ {
+ consumeUntil = "END(dops)";
+ print;
+
+ for (i = 0; i <= MAX_OPCODE; i++) {
+ if (isUnused(i) || isOptimized(i)) continue;
+
+ nextOp = nextOpcode[i];
+ nextOp = (nextOp == -1) ? "NO_NEXT" : uppername[nextOp];
+
+ printf(" public static final Dop %s =\n" \
+ " new Dop(DalvOps.%s, DalvOps.%s,\n" \
+ " DalvOps.%s, Form%s.THE_ONE, %s,\n" \
+ " \"%s\");\n\n",
+ uppername[i], uppername[i], family[i], nextOp, format[i],
+ hasResult[i], name[i]);
+ }
+
+ next;
+}
+
+/BEGIN\(dops-init\)/ {
+ consumeUntil = "END(dops-init)";
+ print;
+
+ for (i = 0; i <= MAX_OPCODE; i++) {
+ if (isUnused(i) || isOptimized(i)) continue;
+ printf(" set(%s);\n", uppername[i]);
+ }
+
+ next;
+}
+
+{ print; }
+
+# Read the bytecode description file.
+function readBytecodes(i, parts, line, cmd, status, count) {
+ # locals: parts, line, cmd, status, count
+ for (;;) {
+ # Read a line.
+ status = getline line <bytecodeFile;
+ if (status == 0) break;
+ if (status < 0) {
+ print "trouble reading bytecode file";
+ exit 1;
+ }
+
+ # Clean up the line and extract the command.
+ gsub(/ */, " ", line);
+ sub(/ *#.*$/, "", line);
+ sub(/ $/, "", line);
+ sub(/^ /, "", line);
+ count = split(line, parts);
+ if (count == 0) continue; # Blank or comment line.
+ cmd = parts[1];
+ sub(/^[a-z][a-z]* */, "", line); # Remove the command from line.
+
+ if (cmd == "op") {
+ status = defineOpcode(line);
+ } else if (cmd == "format") {
+ status = defineFormat(line);
+ } else {
+ status = -1;
+ }
+
+ if (status != 0) {
+ printf("syntax error on line: %s\n", line);
+ return 1;
+ }
+ }
+
+ return 0;
+}
+
+# Define an opcode.
+function defineOpcode(line, count, parts, idx) {
+ # locals: count, parts, idx
+ count = split(line, parts);
+ if (count != 6) return -1;
+ idx = parseHex(parts[1]);
+ if (idx < 0) return -1;
+
+ # Extract directly specified values from the line.
+ hex[idx] = parts[1];
+ name[idx] = parts[2];
+ format[idx] = parts[3];
+ hasResult[idx] = (parts[4] == "n") ? "false" : "true";
+ indexType[idx] = parts[5];
+ flags[idx] = parts[6];
+
+ # Calculate derived values.
+ uppername[idx] = toupper(name[idx]);
+ gsub("[---/]", "_", uppername[idx]);
+ split(name[idx], parts, "/");
+ family[idx] = toupper(parts[1]);
+ gsub("-", "_", family[idx]);
+
+ # This association is used when computing "next" opcodes.
+ familyFormat[family[idx],format[idx]] = idx;
+
+ # Verify values.
+
+ if (nextFormat[format[idx]] == "") {
+ printf("unknown format: %s\n", format[idx]);
+ return 1;
+ }
+
+ if (indexTypeValues[indexType[idx]] == "") {
+ printf("unknown index type: %s\n", indexType[idx]);
+ return 1;
+ }
+
+ if (flagsToC(flags[idx]) == "") {
+ printf("bogus flags: %s\n", flags[idx]);
+ return 1;
+ }
+
+ return 0;
+}
+
+# Define a format family.
+function defineFormat(line, count, parts, i) {
+ # locals: count, parts, i
+ count = split(line, parts);
+ if (count < 1) return -1;
+ formats[parts[1]] = line;
+
+ parts[count + 1] = "none";
+ for (i = 1; i <= count; i++) {
+ nextFormat[parts[i]] = parts[i + 1];
+ }
+
+ return 0;
+}
+
+# Produce the nextOpcode and isFirst arrays. The former indicates, for
+# each opcode, which one should be tried next when doing instruction
+# fitting. The latter indicates which opcodes are at the head of an
+# instruction fitting chain.
+function deriveOpcodeChains(i, op) {
+ # locals: i, op
+
+ for (i = 0; i <= MAX_OPCODE; i++) {
+ if (isUnused(i)) continue;
+ isFirst[i] = "true";
+ }
+
+ for (i = 0; i <= MAX_OPCODE; i++) {
+ if (isUnused(i)) continue;
+ op = findNextOpcode(i);
+ nextOpcode[i] = op;
+ if (op != -1) {
+ isFirst[op] = "false";
+ }
+ }
+}
+
+# Given an opcode by index, find the next opcode in the same family
+# (that is, with the same base name) to try when matching instructions
+# to opcodes. This simply walks the nextFormat chain looking for a
+# match. This returns the index of the matching opcode or -1 if there
+# is none.
+function findNextOpcode(idx, fam, fmt, result) {
+ # locals: fam, fmt, result
+ fam = family[idx];
+ fmt = format[idx];
+
+ # Not every opcode has a version with every possible format, so
+ # we have to iterate down the chain until we find one or run out of
+ # formats to try.
+ for (fmt = nextFormat[format[idx]]; fmt != "none"; fmt = nextFormat[fmt]) {
+ result = familyFormat[fam,fmt];
+ if (result != "") {
+ return result;
+ }
+ }
+
+ return -1;
+}
+
+# Convert a hex value to an int.
+function parseHex(hex, result, chars, count, c, i) {
+ # locals: result, chars, count, c, i
+ hex = tolower(hex);
+ count = split(hex, chars, "");
+ result = 0;
+ for (i = 1; i <= count; i++) {
+ c = index("0123456789abcdef", chars[i]);
+ if (c == 0) {
+ printf("bogus hex value: %s\n", hex);
+ return -1;
+ }
+ result = (result * 16) + c - 1;
+ }
+ return result;
+}
+
+# Initialize the indexTypes data.
+function initIndexTypes() {
+ indexTypeValues["unknown"] = "kIndexUnknown";
+ indexTypeValues["none"] = "kIndexNone";
+ indexTypeValues["varies"] = "kIndexVaries";
+ indexTypeValues["type-ref"] = "kIndexTypeRef";
+ indexTypeValues["string-ref"] = "kIndexStringRef";
+ indexTypeValues["method-ref"] = "kIndexMethodRef";
+ indexTypeValues["field-ref"] = "kIndexFieldRef";
+ indexTypeValues["inline-method"] = "kIndexInlineMethod";
+ indexTypeValues["vtable-offset"] = "kIndexVtableOffset";
+ indexTypeValues["field-offset"] = "kIndexFieldOffset";
+}
+
+# Initialize the flags data.
+function initFlags() {
+ flagValues["branch"] = "kInstrCanBranch";
+ flagValues["continue"] = "kInstrCanContinue";
+ flagValues["switch"] = "kInstrCanSwitch";
+ flagValues["throw"] = "kInstrCanThrow";
+ flagValues["return"] = "kInstrCanReturn";
+ flagValues["invoke"] = "kInstrInvoke";
+ flagValues["optimized"] = "0"; # Not represented in C output
+ flagValues["0"] = "0";
+}
+
+# Translate the given flags into the equivalent C expression. Returns
+# "" on error.
+function flagsToC(f, parts, result, i) {
+ # locals: parts, result, i
+ count = split(f, parts, /\|/); # Split input at pipe characters.
+ result = "0";
+
+ for (i = 1; i <= count; i++) {
+ f = flagValues[parts[i]];
+ if (f == "") {
+ printf("bogus flag: %s\n", f);
+ return ""; # Bogus flag name.
+ } else if (f == "0") {
+ # Nothing to append for this case.
+ } else if (result == "0") {
+ result = f;
+ } else {
+ result = result "|" f;
+ }
+ }
+
+ return result;
+}
+
+# Returns true if the given opcode (by index) is an "optimized" opcode.
+function isOptimized(idx, parts, f) {
+ # locals: parts, f
+ split(flags[idx], parts, /\|/); # Split flags[idx] at pipes.
+ for (f in parts) {
+ if (parts[f] == "optimized") return 1;
+ }
+ return 0;
+}
+
+# Returns true if the given opcode (by index) is unused. This is true either
+# if there is no definition at all for the opcode or if there is a definition
+# and the name contains the string "unused".
+function isUnused(idx, n) {
+ # locals: n
+ n = name[idx];
+ return (n == "") || (index(n, "unused") != 0);
+}
+' "$file" > "$tmpfile"
+
+cp "$tmpfile" "$file"
+rm "$tmpfile"
diff --git a/opcode-gen/regen-all b/opcode-gen/regen-all
new file mode 100755
index 000000000..790bf84e8
--- /dev/null
+++ b/opcode-gen/regen-all
@@ -0,0 +1,42 @@
+#!/bin/bash
+#
+# Copyright (C) 2007 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Set up prog to be the path of this script, including following symlinks,
+# and set up progdir to be the fully-qualified pathname of its directory.
+prog="$0"
+while [ -h "${prog}" ]; do
+ newProg=`/bin/ls -ld "${prog}"`
+ newProg=`expr "${newProg}" : ".* -> \(.*\)$"`
+ if expr "x${newProg}" : 'x/' >/dev/null; then
+ prog="${newProg}"
+ else
+ progdir=`dirname "${prog}"`
+ prog="${progdir}/${newProg}"
+ fi
+done
+progdir=`dirname "${prog}"`
+cd "${progdir}"
+progdir=`pwd`
+
+# Be in the parent of the progdir when running the rest of the script.
+cd ".."
+
+${progdir}/opcode-gen dx/src/com/android/dx/dex/code/DalvOps.java
+${progdir}/opcode-gen dx/src/com/android/dx/dex/code/Dops.java
+${progdir}/opcode-gen dx/src/com/android/dx/dex/code/RopToDop.java
+
+# Coming soon!
+# ${progdir}/opcode-gen libdex/OpCode.h