diff options
author | The Android Open Source Project <initial-contribution@android.com> | 2012-04-01 00:00:00 -0700 |
---|---|---|
committer | The Android Open Source Project <initial-contribution@android.com> | 2013-03-01 09:33:00 -0800 |
commit | 4b279859aaa01ed58457a5cee3335069985ea53c (patch) | |
tree | 1c2bc735be22147da2a67ff3c3822662b7899289 /opcode-gen | |
download | dalvik2-4b279859aaa01ed58457a5cee3335069985ea53c.tar.gz |
Change-Id: Ifeff1cc0867b7a1459e6d83cb354883f134fda74
Diffstat (limited to 'opcode-gen')
-rw-r--r-- | opcode-gen/README.txt | 74 | ||||
-rw-r--r-- | opcode-gen/bytecode.txt | 346 | ||||
-rwxr-xr-x | opcode-gen/opcode-gen | 67 | ||||
-rw-r--r-- | opcode-gen/opcode-gen.awk | 538 | ||||
-rwxr-xr-x | opcode-gen/regen-all | 50 |
5 files changed, 1075 insertions, 0 deletions
diff --git a/opcode-gen/README.txt b/opcode-gen/README.txt new file mode 100644 index 0000000..a210842 --- /dev/null +++ b/opcode-gen/README.txt @@ -0,0 +1,74 @@ +# Copyright (C) 2010 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. + +--------------------------------------------------------------------- +Notes on updating the sets of defined opcodes and instruction formats +--------------------------------------------------------------------- + +########## + +If you want to add, delete, or change opcodes: + +* Update the file bytecode.txt, in this directory. + +* Run the regen-all script, in this directory. This will regenerate a + number of tables, definitions, and declarations in the code, in + dalvik/dx, dalvik/libdex, and libcore/dalvik. + +* Implement/update the opcode in C in vm/mterp/c/... + * Verify new code by running with "dalvik.vm.execution-mode = int:portable" + or "-Xint:portable". + +* Implement/update the instruction in assembly in vm/mterp/{arm*,x86*}/... + * Verify by enabling the assembly (e.g. ARM) handler for that instruction + in mterp/config-* and running "int:fast" as above. + +* Implement/update the instruction in + vm/compiler/codegen/{arm,x86}/CodegenDriver.c. + +* Rebuild the interpreter code. See the notes in vm/mterp/ReadMe.txt for + details. + +* Look in the directory vm/analysis at the files CodeVerify.c, + DexVerify.c, and Optimize.c. You may need to update them to account + for your changes. + * If you change anything here, be sure to try running the system with + the verifier enabled (which is in fact the default). + +########## + +If you want to add, delete, or change instruction formats: + +This is a more manual affair than changing opcodes. + +* Update the file bytecode.txt, and run regen-all, as per above. + +* Update the instruction format list in libdex/InstrUtils.h. + +* Update dexDecodeInstruction() in libdex/InstrUtils.c. + +* Update dumpInstruction() and its helper code in dexdump/DexDump.c. + +* Update the switch inside dvmCompilerMIR2LIR() in + vm/compiler/codegen/{arm,x86}/CodegenDriver.c. (There may be other + architectures to deal with too.) + +########## + +Testing your work: + +The Dalvik VM tests (in the vm/tests directory) provide a convenient +way to test most of the above without doing any rebuilds. In +particular, test 003-omnibus-opcodes will exercise most of the +opcodes. diff --git a/opcode-gen/bytecode.txt b/opcode-gen/bytecode.txt new file mode 100644 index 0000000..6b7b9b1 --- /dev/null +++ b/opcode-gen/bytecode.txt @@ -0,0 +1,346 @@ +# 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 # See note, above. +format 21c 31c +format 21t 31t +format 22b 22s +format 22c +format 22t +format 35c 3rc + +# Optimized formats +format 00x +format 20bc +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: +# unknown -- used for undefined opcodes and breakpoint +# 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; this is only used for true +# method invokes and notably *not* vm-implemented +# execute-inline nor the nop-equivalent +# invoke-direct-empty + +# +# 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) +# + +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 + +# Technically "breakpoint" isn't really an optimized opcode, but it +# fits the label in terms of not being valid in regular dex files. +op ec ^breakpoint 00x n unknown optimized + +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-object-init/range 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 + +# unused: op ff diff --git a/opcode-gen/opcode-gen b/opcode-gen/opcode-gen new file mode 100755 index 0000000..b8e4397 --- /dev/null +++ b/opcode-gen/opcode-gen @@ -0,0 +1,67 @@ +#!/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. Refer to those files to understand what's being +# generated. (Look for comments that say "BEGIN(name)" where "name" is +# one of the "emission" directives defined in opcode-gen.awk in this +# directory.) + +file="$1" +tmpfile="/tmp/$$.txt" + +echo "processing `basename $1`" 1>&2 + +if [ "x$1" = "x" ]; then + echo "must specify a file" 1>&2 + 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" -f "$progdir/opcode-gen.awk" \ + "$file" > "$tmpfile" + +if [ "$?" = "0" ]; then + cp "$tmpfile" "$file" + rm "$tmpfile" +else + echo "error running awk" 1>&2 + exit 1 +fi diff --git a/opcode-gen/opcode-gen.awk b/opcode-gen/opcode-gen.awk new file mode 100644 index 0000000..e26a60c --- /dev/null +++ b/opcode-gen/opcode-gen.awk @@ -0,0 +1,538 @@ +# 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. + +# +# Awk helper script for opcode-gen. +# + +# +# Initialization. +# + +BEGIN { + MAX_OPCODE = 65535; + MAX_PACKED_OPCODE = 511; + MAX_PACKED_OPCODE = 255; # TODO: Not for long! + initIndexTypes(); + initFlags(); + if (readBytecodes()) exit 1; + deriveOpcodeChains(); + createPackedTables(); + consumeUntil = ""; + emission = ""; +} + +# +# General control (must appear above directive handlers). +# + +# Clear out the preexisting output within a directive section. +consumeUntil != "" { + if (index($0, consumeUntil) != 0) { + consumeUntil = ""; + print; + } + + next; +} + +# Detect directives. +/BEGIN\([a-z-]*\)/ { + i = match($0, /BEGIN\([a-z-]*\)/); + emission = substr($0, i + 6, RLENGTH - 7); + consumeUntil = "END(" emission ")"; + emissionHandled = 0; +} + +# Most lines just get copied from the source as-is, including the start +# comment for directives. +{ + print; +} + +# +# Handlers for all of the directives. +# + +emission == "opcodes" { + emissionHandled = 1; + + for (i = 0; i <= MAX_OPCODE; i++) { + if (isUnused(i) || isOptimized(i)) continue; + printf(" public static final int %s = 0x%s;\n", + constName[i], hex[i]); + } +} + +emission == "first-opcodes" { + emissionHandled = 1; + + for (i = 0; i <= MAX_OPCODE; i++) { + if (isUnused(i) || isOptimized(i)) continue; + if (isFirst[i] == "true") { + printf(" // Opcodes.%s\n", constName[i]); + } + } +} + +emission == "dops" { + emissionHandled = 1; + + for (i = 0; i <= MAX_OPCODE; i++) { + if (isUnused(i) || isOptimized(i)) continue; + + nextOp = nextOpcode[i]; + nextOp = (nextOp == -1) ? "NO_NEXT" : constName[nextOp]; + + printf(" public static final Dop %s =\n" \ + " new Dop(Opcodes.%s, Opcodes.%s,\n" \ + " Opcodes.%s, Form%s.THE_ONE, %s);\n\n", + constName[i], constName[i], family[i], nextOp, format[i], + hasResult[i]); + } +} + +emission == "opcode-info-defs" { + emissionHandled = 1; + + for (i = 0; i <= MAX_OPCODE; i++) { + if (isUnused(i) || isOptimized(i)) continue; + + itype = toupper(indexType[i]); + gsub(/-/, "_", itype); + + printf(" public static final Info %s =\n" \ + " new Info(Opcodes.%s, \"%s\",\n" \ + " InstructionCodec.FORMAT_%s, IndexType.%s);\n\n", \ + constName[i], constName[i], name[i], toupper(format[i]), itype); + } +} + +emission == "dops-init" || emission == "opcode-info-init" { + emissionHandled = 1; + + for (i = 0; i <= MAX_OPCODE; i++) { + if (isUnused(i) || isOptimized(i)) continue; + printf(" set(%s);\n", constName[i]); + } +} + +emission == "libcore-opcodes" { + emissionHandled = 1; + + for (i = 0; i <= MAX_OPCODE; i++) { + if (isUnused(i) || isOptimized(i)) continue; + printf(" int OP_%-28s = 0x%04x;\n", constName[i], i); + } +} + +emission == "libcore-maximum-values" { + emissionHandled = 1; + + printf(" MAXIMUM_VALUE = %d;\n", MAX_OPCODE); + 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; + + for (i = 0; i <= MAX_PACKED_OPCODE; i++) { + printf(" OP_%-28s = 0x%02x,\n", packedConstName[i], i); + } +} + +emission == "libdex-goto-table" { + emissionHandled = 1; + + for (i = 0; i <= MAX_PACKED_OPCODE; i++) { + content = sprintf(" H(OP_%s),", packedConstName[i]); + printf("%-78s\\\n", content); + } +} + +emission == "libdex-opcode-names" { + emissionHandled = 1; + + for (i = 0; i <= MAX_PACKED_OPCODE; i++) { + printf(" \"%s\",\n", packedName[i]); + } +} + +emission == "libdex-widths" { + emissionHandled = 1; + + col = 1; + for (i = 0; i <= MAX_PACKED_OPCODE; i++) { + value = sprintf("%d,", packedWidth[i]); + col = colPrint(value, (i == MAX_PACKED_OPCODE), col, 16, 2, " "); + } +} + +emission == "libdex-flags" { + emissionHandled = 1; + + for (i = 0; i <= MAX_PACKED_OPCODE; i++) { + value = flagsToC(packedFlags[i]); + printf(" %s,\n", value); + } +} + +emission == "libdex-formats" { + emissionHandled = 1; + + col = 1; + for (i = 0; i <= MAX_PACKED_OPCODE; i++) { + value = sprintf("kFmt%s,", packedFormat[i]); + col = colPrint(value, (i == MAX_PACKED_OPCODE), col, 7, 9, " "); + } +} + +emission == "libdex-index-types" { + emissionHandled = 1; + + col = 1; + for (i = 0; i <= MAX_PACKED_OPCODE; i++) { + value = sprintf("%s,", indexTypeValues[packedIndexType[i]]); + col = colPrint(value, (i == MAX_PACKED_OPCODE), col, 3, 19, " "); + } +} + +# Handle the end of directive processing (must appear after the directive +# clauses). +emission != "" { + if (!emissionHandled) { + printf("WARNING: unknown tag \"%s\"\n", emission) >"/dev/stderr"; + consumeUntil = ""; + } + + emission = ""; +} + +# +# Helper functions. +# + +# Helper to print out an element in a multi-column fashion. It returns +# the (one-based) column number that the next element will be printed +# in. +function colPrint(value, isLast, col, numCols, colWidth, linePrefix) { + isLast = (isLast || (col == numCols)); + printf("%s%-*s%s", + (col == 1) ? linePrefix : " ", + isLast ? 1 : colWidth, value, + isLast ? "\n" : ""); + + return (col % numCols) + 1; +} + +# 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) >"/dev/stderr"; + 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. + + constName[idx] = toupper(name[idx]); + gsub("[/-]", "_", constName[idx]); # Dash and slash become underscore. + gsub("[+^]", "", constName[idx]); # Plus and caret are removed. + split(name[idx], parts, "/"); + + family[idx] = toupper(parts[1]); + gsub("-", "_", family[idx]); # Dash becomes underscore. + gsub("[+^]", "", family[idx]); # Plus and caret are removed. + + split(format[idx], parts, ""); # Width is the first format char. + width[idx] = parts[1]; + + # 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]) >"/dev/stderr"; + return 1; + } + + if (indexTypeValues[indexType[idx]] == "") { + printf("unknown index type: %s\n", indexType[idx]) >"/dev/stderr"; + return 1; + } + + if (flagsToC(flags[idx]) == "") { + printf("bogus flags: %s\n", flags[idx]) >"/dev/stderr"; + 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; +} + +# Construct the tables of info indexed by packed opcode. The packed opcode +# values are in the range 0-0x1ff, whereas the unpacked opcodes sparsely +# span the range 0-0xffff. +function createPackedTables(i, op) { + # locals: i, op + for (i = 0; i <= MAX_PACKED_OPCODE; i++) { + op = unpackOpcode(i); + if (isUnused(op)) { + packedName[i] = unusedName(op); + packedConstName[i] = unusedConstName(op); + packedFormat[i] = "00x"; + packedFlags[i] = 0; + packedWidth[i] = 0; + packedIndexType[i] = "unknown"; + } else { + packedName[i] = name[op]; + packedConstName[i] = constName[op]; + packedFormat[i] = format[op]; + packedFlags[i] = flags[op]; + packedWidth[i] = width[op]; + packedIndexType[i] = indexType[op]; + } + } +} + +# Given a packed opcode, returns the raw (unpacked) opcode value. +function unpackOpcode(idx) { + # Note: This must be the inverse of the corresponding code in + # libdex/DexOpcodes.h. + if (idx <= 255) { + return idx; + } else { + idx -= 256; + return (idx * 256) + 255; + } +} + +# Returns the "unused" name of the given opcode (by index). +# That is, this is the human-oriented name to use for an opcode +# definition in cases +# where the opcode isn't used. +function unusedName(idx) { + if (idx <= 255) { + return sprintf("unused-%02x", idx); + } else { + return sprintf("unused-%04x", idx); + } +} + +# Returns the "unused" constant name of the given opcode (by index). +# That is, this is the name to use for a constant definition in cases +# where the opcode isn't used. +function unusedConstName(idx) { + if (idx <= 255) { + return toupper(sprintf("UNUSED_%02x", idx)); + } else { + return toupper(sprintf("UNUSED_%04x", idx)); + } +} + +# 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) >"/dev/stderr"; + 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) >"/dev/stderr"; + 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 there is no definition for the given opcode (by index). +function isUnused(idx) { + return (name[idx] == ""); +} diff --git a/opcode-gen/regen-all b/opcode-gen/regen-all new file mode 100755 index 0000000..161cda9 --- /dev/null +++ b/opcode-gen/regen-all @@ -0,0 +1,50 @@ +#!/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/Dops.java +${progdir}/opcode-gen dx/src/com/android/dx/dex/code/RopToDop.java +${progdir}/opcode-gen dx/src/com/android/dx/io/OpcodeInfo.java +${progdir}/opcode-gen dx/src/com/android/dx/io/Opcodes.java +${progdir}/opcode-gen libdex/DexOpcodes.cpp +${progdir}/opcode-gen libdex/DexOpcodes.h +${progdir}/opcode-gen libdex/InstrUtils.cpp + +# It's a minor shame that these files live in a different top-level project. +# So it goes. +${progdir}/opcode-gen \ + ../libcore/dalvik/src/main/java/dalvik/bytecode/OpcodeInfo.java +${progdir}/opcode-gen \ + ../libcore/dalvik/src/main/java/dalvik/bytecode/Opcodes.java |