diff options
-rw-r--r-- | dx/src/com/android/dx/cf/code/BytecodeArray.java | 4 | ||||
-rw-r--r-- | dx/src/com/android/dx/io/CodeInput.java | 14 | ||||
-rw-r--r-- | dx/src/com/android/dx/io/CodeOutput.java | 10 | ||||
-rw-r--r-- | dx/src/com/android/dx/io/InstructionCodec.java | 85 | ||||
-rw-r--r-- | dx/src/com/android/dx/io/ShortArrayCodeInput.java | 69 |
5 files changed, 129 insertions, 53 deletions
diff --git a/dx/src/com/android/dx/cf/code/BytecodeArray.java b/dx/src/com/android/dx/cf/code/BytecodeArray.java index e942b13bc..6ede25c87 100644 --- a/dx/src/com/android/dx/cf/code/BytecodeArray.java +++ b/dx/src/com/android/dx/cf/code/BytecodeArray.java @@ -1340,8 +1340,8 @@ public final class BytecodeArray { } /** - * Base implementation of {@link Visitor}, which has empty method - * bodies for all methods. + * Implementation of {@link Visitor}, which just pays attention + * to constant values. */ class ConstantParserVisitor extends BaseVisitor { Constant cst; diff --git a/dx/src/com/android/dx/io/CodeInput.java b/dx/src/com/android/dx/io/CodeInput.java index 911039085..a4f730694 100644 --- a/dx/src/com/android/dx/io/CodeInput.java +++ b/dx/src/com/android/dx/io/CodeInput.java @@ -16,22 +16,24 @@ package com.android.dx.io; +import java.io.EOFException; + /** * Input stream of code units, for reading in Dalvik bytecode. */ public interface CodeInput { /** - * Read a code unit. + * Reads a code unit. */ - public int read(); + public int read() throws EOFException; /** - * Read two code units, treating them as a little-endian {@code int}. + * Reads two code units, treating them as a little-endian {@code int}. */ - public int readInt(); + public int readInt() throws EOFException; /** - * Read four code units, treating them as a little-endian {@code long}. + * Reads four code units, treating them as a little-endian {@code long}. */ - public long readLong(); + public long readLong() throws EOFException; } diff --git a/dx/src/com/android/dx/io/CodeOutput.java b/dx/src/com/android/dx/io/CodeOutput.java index 4c48f472c..c87d1b1e0 100644 --- a/dx/src/com/android/dx/io/CodeOutput.java +++ b/dx/src/com/android/dx/io/CodeOutput.java @@ -21,27 +21,27 @@ package com.android.dx.io; */ public interface CodeOutput { /** - * Write a code unit. + * Writes a code unit. */ public void write(short codeUnit); /** - * Write two code units. + * Writes two code units. */ public void write(short u0, short u1); /** - * Write three code units. + * Writes three code units. */ public void write(short u0, short u1, short u2); /** - * Write four code units. + * Writes four code units. */ public void write(short u0, short u1, short u2, short u3); /** - * Write five code units. + * Writes five code units. */ public void write(short u0, short u1, short u2, short u3, short u4); } diff --git a/dx/src/com/android/dx/io/InstructionCodec.java b/dx/src/com/android/dx/io/InstructionCodec.java index 03917040b..45d955c0c 100644 --- a/dx/src/com/android/dx/io/InstructionCodec.java +++ b/dx/src/com/android/dx/io/InstructionCodec.java @@ -16,6 +16,8 @@ package com.android.dx.io; +import java.io.EOFException; + /** * Representation of an instruction format, which knows how to decode into * and encode from instances of {@link DecodedInstruction}. @@ -23,7 +25,7 @@ package com.android.dx.io; public enum InstructionCodec { FORMAT_00X() { @Override public DecodedInstruction decode(int opcodeUnit, - CodeInput in) { + CodeInput in) throws EOFException { return new DecodedInstruction(this, opcodeUnit, 0, null, 0, 0L, null, 0, 0, 0, 0, 0, 0); @@ -36,7 +38,7 @@ public enum InstructionCodec { FORMAT_10X() { @Override public DecodedInstruction decode(int opcodeUnit, - CodeInput in) { + CodeInput in) throws EOFException { int opcode = byte0(opcodeUnit); int literal = byte1(opcodeUnit); // should be zero return new DecodedInstruction(this, opcode, 0, null, @@ -51,7 +53,7 @@ public enum InstructionCodec { FORMAT_12X() { @Override public DecodedInstruction decode(int opcodeUnit, - CodeInput in) { + CodeInput in) throws EOFException { int opcode = byte0(opcodeUnit); int a = nibble2(opcodeUnit); int b = nibble3(opcodeUnit); @@ -69,7 +71,7 @@ public enum InstructionCodec { FORMAT_11N() { @Override public DecodedInstruction decode(int opcodeUnit, - CodeInput in) { + CodeInput in) throws EOFException { int opcode = byte0(opcodeUnit); int a = nibble2(opcodeUnit); int literal = (nibble3(opcodeUnit) << 28) >> 28; // sign-extend @@ -87,7 +89,7 @@ public enum InstructionCodec { FORMAT_11X() { @Override public DecodedInstruction decode(int opcodeUnit, - CodeInput in) { + CodeInput in) throws EOFException { int opcode = byte0(opcodeUnit); int a = byte1(opcodeUnit); return new DecodedInstruction(this, opcode, 0, null, @@ -102,7 +104,7 @@ public enum InstructionCodec { FORMAT_10T() { @Override public DecodedInstruction decode(int opcodeUnit, - CodeInput in) { + CodeInput in) throws EOFException { int opcode = byte0(opcodeUnit); int a = (byte) byte1(opcodeUnit); // sign-extend return new DecodedInstruction(this, opcode, 0, null, @@ -117,7 +119,7 @@ public enum InstructionCodec { FORMAT_20T() { @Override public DecodedInstruction decode(int opcodeUnit, - CodeInput in) { + CodeInput in) throws EOFException { int opcode = byte0(opcodeUnit); int literal = byte1(opcodeUnit); // should be zero int target = (short) in.read(); // sign-extend @@ -133,7 +135,7 @@ public enum InstructionCodec { FORMAT_20BC() { @Override public DecodedInstruction decode(int opcodeUnit, - CodeInput in) { + CodeInput in) throws EOFException { // Note: We use the literal field to hold the decoded AA value. int opcode = byte0(opcodeUnit); int literal = byte1(opcodeUnit); @@ -153,7 +155,7 @@ public enum InstructionCodec { FORMAT_22X() { @Override public DecodedInstruction decode(int opcodeUnit, - CodeInput in) { + CodeInput in) throws EOFException { int opcode = byte0(opcodeUnit); int a = byte1(opcodeUnit); int b = in.read(); @@ -171,7 +173,7 @@ public enum InstructionCodec { FORMAT_21T() { @Override public DecodedInstruction decode(int opcodeUnit, - CodeInput in) { + CodeInput in) throws EOFException { int opcode = byte0(opcodeUnit); int a = byte1(opcodeUnit); int target = (short) in.read(); // sign-extend @@ -189,7 +191,7 @@ public enum InstructionCodec { FORMAT_21S() { @Override public DecodedInstruction decode(int opcodeUnit, - CodeInput in) { + CodeInput in) throws EOFException { int opcode = byte0(opcodeUnit); int a = byte1(opcodeUnit); int literal = (short) in.read(); // sign-extend @@ -207,7 +209,7 @@ public enum InstructionCodec { FORMAT_21H() { @Override public DecodedInstruction decode(int opcodeUnit, - CodeInput in) { + CodeInput in) throws EOFException { int opcode = byte0(opcodeUnit); int a = byte1(opcodeUnit); int literal = (short) in.read(); // sign-extend @@ -236,7 +238,7 @@ public enum InstructionCodec { FORMAT_21C() { @Override public DecodedInstruction decode(int opcodeUnit, - CodeInput in) { + CodeInput in) throws EOFException { int opcode = byte0(opcodeUnit); int a = byte1(opcodeUnit); int index = in.read(); @@ -255,7 +257,7 @@ public enum InstructionCodec { FORMAT_23X() { @Override public DecodedInstruction decode(int opcodeUnit, - CodeInput in) { + CodeInput in) throws EOFException { int opcode = byte0(opcodeUnit); int a = byte1(opcodeUnit); int bc = in.read(); @@ -275,7 +277,7 @@ public enum InstructionCodec { FORMAT_22B() { @Override public DecodedInstruction decode(int opcodeUnit, - CodeInput in) { + CodeInput in) throws EOFException { int opcode = byte0(opcodeUnit); int a = byte1(opcodeUnit); int bc = in.read(); @@ -296,7 +298,7 @@ public enum InstructionCodec { FORMAT_22T() { @Override public DecodedInstruction decode(int opcodeUnit, - CodeInput in) { + CodeInput in) throws EOFException { int opcode = byte0(opcodeUnit); int a = nibble2(opcodeUnit); int b = nibble3(opcodeUnit); @@ -316,7 +318,7 @@ public enum InstructionCodec { FORMAT_22S() { @Override public DecodedInstruction decode(int opcodeUnit, - CodeInput in) { + CodeInput in) throws EOFException { int opcode = byte0(opcodeUnit); int a = nibble2(opcodeUnit); int b = nibble3(opcodeUnit); @@ -336,7 +338,7 @@ public enum InstructionCodec { FORMAT_22C() { @Override public DecodedInstruction decode(int opcodeUnit, - CodeInput in) { + CodeInput in) throws EOFException { int opcode = byte0(opcodeUnit); int a = nibble2(opcodeUnit); int b = nibble3(opcodeUnit); @@ -357,7 +359,7 @@ public enum InstructionCodec { FORMAT_22CS() { @Override public DecodedInstruction decode(int opcodeUnit, - CodeInput in) { + CodeInput in) throws EOFException { int opcode = byte0(opcodeUnit); int a = nibble2(opcodeUnit); int b = nibble3(opcodeUnit); @@ -378,7 +380,7 @@ public enum InstructionCodec { FORMAT_30T() { @Override public DecodedInstruction decode(int opcodeUnit, - CodeInput in) { + CodeInput in) throws EOFException { int opcode = byte0(opcodeUnit); int literal = byte1(opcodeUnit); // should be zero int target = in.readInt(); @@ -395,7 +397,7 @@ public enum InstructionCodec { FORMAT_32X() { @Override public DecodedInstruction decode(int opcodeUnit, - CodeInput in) { + CodeInput in) throws EOFException { int opcode = byte0(opcodeUnit); int literal = byte1(opcodeUnit); // should be zero int a = in.read(); @@ -412,7 +414,7 @@ public enum InstructionCodec { FORMAT_31I() { @Override public DecodedInstruction decode(int opcodeUnit, - CodeInput in) { + CodeInput in) throws EOFException { int opcode = byte0(opcodeUnit); int a = byte1(opcodeUnit); int literal = in.readInt(); @@ -432,7 +434,7 @@ public enum InstructionCodec { FORMAT_31T() { @Override public DecodedInstruction decode(int opcodeUnit, - CodeInput in) { + CodeInput in) throws EOFException { int opcode = byte0(opcodeUnit); int a = byte1(opcodeUnit); int target = in.readInt(); @@ -452,7 +454,7 @@ public enum InstructionCodec { FORMAT_31C() { @Override public DecodedInstruction decode(int opcodeUnit, - CodeInput in) { + CodeInput in) throws EOFException { int opcode = byte0(opcodeUnit); int a = byte1(opcodeUnit); int index = in.readInt(); @@ -473,7 +475,7 @@ public enum InstructionCodec { FORMAT_35C() { @Override public DecodedInstruction decode(int opcodeUnit, - CodeInput in) { + CodeInput in) throws EOFException { return decodeRegisterList(this, opcodeUnit, in); } @@ -484,7 +486,7 @@ public enum InstructionCodec { FORMAT_35MS() { @Override public DecodedInstruction decode(int opcodeUnit, - CodeInput in) { + CodeInput in) throws EOFException { return decodeRegisterList(this, opcodeUnit, in); } @@ -495,7 +497,7 @@ public enum InstructionCodec { FORMAT_35MI() { @Override public DecodedInstruction decode(int opcodeUnit, - CodeInput in) { + CodeInput in) throws EOFException { return decodeRegisterList(this, opcodeUnit, in); } @@ -506,7 +508,7 @@ public enum InstructionCodec { FORMAT_3RC() { @Override public DecodedInstruction decode(int opcodeUnit, - CodeInput in) { + CodeInput in) throws EOFException { return decodeRegisterRange(this, opcodeUnit, in); } @@ -517,7 +519,7 @@ public enum InstructionCodec { FORMAT_3RMS() { @Override public DecodedInstruction decode(int opcodeUnit, - CodeInput in) { + CodeInput in) throws EOFException { return decodeRegisterRange(this, opcodeUnit, in); } @@ -528,7 +530,7 @@ public enum InstructionCodec { FORMAT_3RMI() { @Override public DecodedInstruction decode(int opcodeUnit, - CodeInput in) { + CodeInput in) throws EOFException { return decodeRegisterRange(this, opcodeUnit, in); } @@ -539,7 +541,7 @@ public enum InstructionCodec { FORMAT_51L() { @Override public DecodedInstruction decode(int opcodeUnit, - CodeInput in) { + CodeInput in) throws EOFException { int opcode = byte0(opcodeUnit); int a = byte1(opcodeUnit); long literal = in.readLong(); @@ -561,7 +563,7 @@ public enum InstructionCodec { FORMAT_33X() { @Override public DecodedInstruction decode(int opcodeUnit, - CodeInput in) { + CodeInput in) throws EOFException { int ab = in.read(); int a = byte0(ab); int b = byte1(ab); @@ -581,7 +583,7 @@ public enum InstructionCodec { FORMAT_32S() { @Override public DecodedInstruction decode(int opcodeUnit, - CodeInput in) { + CodeInput in) throws EOFException { int ab = in.read(); int a = byte0(ab); int b = byte1(ab); @@ -601,7 +603,7 @@ public enum InstructionCodec { FORMAT_40SC() { @Override public DecodedInstruction decode(int opcodeUnit, - CodeInput in) { + CodeInput in) throws EOFException { // Note: We use the literal field to hold the decoded AA value. int index = in.readInt(); int literal = in.read(); @@ -623,7 +625,7 @@ public enum InstructionCodec { FORMAT_41C() { @Override public DecodedInstruction decode(int opcodeUnit, - CodeInput in) { + CodeInput in) throws EOFException { int index = in.readInt(); int a = in.read(); IndexType indexType = OpcodeInfo.getIndexType(opcodeUnit); @@ -644,7 +646,7 @@ public enum InstructionCodec { FORMAT_52C() { @Override public DecodedInstruction decode(int opcodeUnit, - CodeInput in) { + CodeInput in) throws EOFException { int index = in.readInt(); int a = in.read(); int b = in.read(); @@ -667,7 +669,7 @@ public enum InstructionCodec { FORMAT_5RC() { @Override public DecodedInstruction decode(int opcodeUnit, - CodeInput in) { + CodeInput in) throws EOFException { int index = in.readInt(); int registerCount = in.read(); int a = in.read(); @@ -692,7 +694,8 @@ public enum InstructionCodec { * Decodes an instruction specified by the given opcode unit, reading * any required additional code units from the given input source. */ - public abstract DecodedInstruction decode(int opcodeUnit, CodeInput in); + public abstract DecodedInstruction decode(int opcodeUnit, CodeInput in) + throws EOFException; /** * Encodes the given instruction. @@ -703,7 +706,8 @@ public enum InstructionCodec { * Helper method that decodes any of the register-list formats. */ private static DecodedInstruction decodeRegisterList( - InstructionCodec format, int opcodeUnit, CodeInput in) { + InstructionCodec format, int opcodeUnit, CodeInput in) + throws EOFException { int opcode = byte0(opcodeUnit); int e = nibble2(opcodeUnit); int registerCount = nibble3(opcodeUnit); @@ -734,7 +738,8 @@ public enum InstructionCodec { * Helper method that decodes any of the three-unit register-range formats. */ private static DecodedInstruction decodeRegisterRange( - InstructionCodec format, int opcodeUnit, CodeInput in) { + InstructionCodec format, int opcodeUnit, CodeInput in) + throws EOFException { int opcode = byte0(opcodeUnit); int registerCount = byte1(opcodeUnit); int index = in.read(); diff --git a/dx/src/com/android/dx/io/ShortArrayCodeInput.java b/dx/src/com/android/dx/io/ShortArrayCodeInput.java new file mode 100644 index 000000000..c0faf712d --- /dev/null +++ b/dx/src/com/android/dx/io/ShortArrayCodeInput.java @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2011 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. + */ + +package com.android.dx.io; + +import java.io.EOFException; + +/** + * Implementation of {@code CodeInput} that reads from a {@code short[]}. + */ +public final class ShortArrayCodeInput { + /** source array to read from */ + private final short[] array; + + /** next index within {@link #array} to read from */ + private int cursor; + + /** + * Constructs an instance. + */ + public ShortArrayCodeInput(short[] array) { + if (array == null) { + throw new NullPointerException("array == null"); + } + + this.array = array; + this.cursor = 0; + } + + /** @inheritDoc */ + public int read() throws EOFException { + try { + return array[cursor++]; + } catch (ArrayIndexOutOfBoundsException ex) { + throw new EOFException(); + } + } + + /** @inheritDoc */ + public int readInt() throws EOFException { + int short0 = read(); + int short1 = read(); + + return short0 | (short1 << 16); + } + + /** @inheritDoc */ + public long readLong() throws EOFException { + long short0 = read(); + long short1 = read(); + long short2 = read(); + long short3 = read(); + + return short0 | (short1 << 16) | (short2 << 32) | (short3 << 48); + } +} |