Use code offsets rather than indexes for instructions

I had initially wanted to use instruction indexes at the dexlib level,
rather than the actual code unit offsets/addresses. But after additional
thought, I decided to stick with code units, and then provide a utility
for easily mapping between indexes/offsets (not implemented yet).
This commit is contained in:
Ben Gruver 2012-10-23 20:01:18 -07:00
parent 8b1508ee58
commit ccc4c13ae6
17 changed files with 70 additions and 178 deletions

View File

@ -31,16 +31,13 @@
package org.jf.dexlib2.dexbacked; package org.jf.dexlib2.dexbacked;
import org.jf.dexlib2.dexbacked.util.InstructionOffsetMap;
import org.jf.dexlib2.immutable.ImmutableExceptionHandler; import org.jf.dexlib2.immutable.ImmutableExceptionHandler;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
public class DexBackedCatchAllExceptionHandler extends ImmutableExceptionHandler { public class DexBackedCatchAllExceptionHandler extends ImmutableExceptionHandler {
public DexBackedCatchAllExceptionHandler(@Nonnull DexReader reader, public DexBackedCatchAllExceptionHandler(@Nonnull DexReader reader) {
@Nonnull InstructionOffsetMap instructionOffsetMap) { super(null, reader.readSmallUleb128());
super(null,
instructionOffsetMap.getInstructionIndexAtOffsetExact(reader.readSmallUleb128()));
} }
public static void skipFrom(@Nonnull DexReader reader) { public static void skipFrom(@Nonnull DexReader reader) {

View File

@ -31,17 +31,14 @@
package org.jf.dexlib2.dexbacked; package org.jf.dexlib2.dexbacked;
import org.jf.dexlib2.dexbacked.util.InstructionOffsetMap;
import org.jf.dexlib2.immutable.ImmutableExceptionHandler; import org.jf.dexlib2.immutable.ImmutableExceptionHandler;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
public class DexBackedExceptionHandler extends ImmutableExceptionHandler { public class DexBackedExceptionHandler extends ImmutableExceptionHandler {
public DexBackedExceptionHandler(@Nonnull DexReader reader, public DexBackedExceptionHandler(@Nonnull DexReader reader) {
@Nonnull InstructionOffsetMap instructionOffsetMap) {
// TODO: verify dalvik doesn't accept an exception handler that points in the middle of an instruction // TODO: verify dalvik doesn't accept an exception handler that points in the middle of an instruction
super(reader.getType(reader.readSmallUleb128()), super(reader.getType(reader.readSmallUleb128()), reader.readSmallUleb128());
instructionOffsetMap.getInstructionIndexAtOffsetExact(reader.readSmallUleb128()));
} }
public static void skipFrom(@Nonnull DexReader reader) { public static void skipFrom(@Nonnull DexReader reader) {

View File

@ -34,7 +34,6 @@ package org.jf.dexlib2.dexbacked;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import org.jf.dexlib2.dexbacked.instruction.DexBackedInstruction; import org.jf.dexlib2.dexbacked.instruction.DexBackedInstruction;
import org.jf.dexlib2.dexbacked.util.FixedSizeList; import org.jf.dexlib2.dexbacked.util.FixedSizeList;
import org.jf.dexlib2.dexbacked.util.InstructionOffsetMap;
import org.jf.dexlib2.iface.MethodImplementation; import org.jf.dexlib2.iface.MethodImplementation;
import org.jf.dexlib2.iface.TryBlock; import org.jf.dexlib2.iface.TryBlock;
import org.jf.dexlib2.iface.instruction.Instruction; import org.jf.dexlib2.iface.instruction.Instruction;
@ -50,7 +49,6 @@ public class DexBackedMethodImplementation implements MethodImplementation {
public final int registerCount; public final int registerCount;
@Nonnull public final ImmutableList<? extends Instruction> instructions; @Nonnull public final ImmutableList<? extends Instruction> instructions;
@Nonnull private final InstructionOffsetMap instructionOffsetMap;
// code_item offsets // code_item offsets
private static final int TRIES_SIZE_OFFSET = 6; private static final int TRIES_SIZE_OFFSET = 6;
@ -66,7 +64,6 @@ public class DexBackedMethodImplementation implements MethodImplementation {
this.registerCount = dexBuf.readUshort(codeOffset); this.registerCount = dexBuf.readUshort(codeOffset);
instructions = buildInstructionList(); instructions = buildInstructionList();
instructionOffsetMap = buildInstructionOffsetMap();
} }
@Override public int getRegisterCount() { return registerCount; } @Override public int getRegisterCount() { return registerCount; }
@ -88,8 +85,7 @@ public class DexBackedMethodImplementation implements MethodImplementation {
public TryBlock readItem(int index) { public TryBlock readItem(int index) {
return new DexBackedTryBlock(dexBuf, return new DexBackedTryBlock(dexBuf,
triesStartOffset + index*TRY_ITEM_SIZE, triesStartOffset + index*TRY_ITEM_SIZE,
handlersStartOffset, handlersStartOffset);
instructionOffsetMap);
} }
@Override @Override
@ -118,23 +114,4 @@ public class DexBackedMethodImplementation implements MethodImplementation {
return ImmutableList.copyOf(instructions); return ImmutableList.copyOf(instructions);
} }
/**
* Builds an InstructionOffsetMap that maps an instruction offset to its index.
*
* This must be called after the instructions field has been set
*
* @return An InstructionOffsetMap object
*/
@Nonnull
private InstructionOffsetMap buildInstructionOffsetMap() {
int[] offsets = new int[instructions.size()];
int currentOffset = 0;
for (int i=0; i<offsets.length; i++) {
offsets[i] = currentOffset;
// TODO: need to handle variable size instructions
currentOffset += instructions.get(i).getOpcode().format.size;
}
return new InstructionOffsetMap(offsets);
}
} }

View File

@ -31,7 +31,6 @@
package org.jf.dexlib2.dexbacked; package org.jf.dexlib2.dexbacked;
import org.jf.dexlib2.dexbacked.util.InstructionOffsetMap;
import org.jf.dexlib2.iface.ExceptionHandler; import org.jf.dexlib2.iface.ExceptionHandler;
import org.jf.dexlib2.iface.TryBlock; import org.jf.dexlib2.iface.TryBlock;
import org.jf.dexlib2.dexbacked.util.VariableSizeList; import org.jf.dexlib2.dexbacked.util.VariableSizeList;
@ -41,10 +40,9 @@ import java.util.List;
public class DexBackedTryBlock implements TryBlock { public class DexBackedTryBlock implements TryBlock {
@Nonnull public final DexBuffer dexBuf; @Nonnull public final DexBuffer dexBuf;
@Nonnull private final InstructionOffsetMap instructionOffsetMap;
public final int startIndex; public final int startCodeOffset;
public final int instructionCount; public final int codeUnitCount;
private final int exceptionHandlersOffset; private final int exceptionHandlersOffset;
@ -54,29 +52,15 @@ public class DexBackedTryBlock implements TryBlock {
public DexBackedTryBlock(@Nonnull DexBuffer dexBuf, public DexBackedTryBlock(@Nonnull DexBuffer dexBuf,
int tryItemOffset, int tryItemOffset,
int handlersStartOffset, int handlersStartOffset) {
@Nonnull InstructionOffsetMap instructionOffsetMap) {
this.dexBuf = dexBuf; this.dexBuf = dexBuf;
this.instructionOffsetMap = instructionOffsetMap; this.startCodeOffset = dexBuf.readSmallUint(tryItemOffset + START_ADDRESS_OFFSET);
this.codeUnitCount = dexBuf.readUshort(tryItemOffset + CODE_UNIT_COUNT_OFFSET);
int startOffset = dexBuf.readSmallUint(tryItemOffset + START_ADDRESS_OFFSET);
// map the code unit offset to the instruction index
this.startIndex = instructionOffsetMap.getInstructionIndexAtOffsetExact(startOffset);
int codeUnitCount = dexBuf.readUshort(tryItemOffset + CODE_UNIT_COUNT_OFFSET);
// TODO: check if dalivk accepts insns_size = 0
if (codeUnitCount == 0) {
this.instructionCount = 0;
} else {
int lastIndex = instructionOffsetMap.getInstructionIndexAtOffset(startOffset + codeUnitCount - 1);
this.instructionCount = lastIndex - startIndex + 1;
}
this.exceptionHandlersOffset = handlersStartOffset + dexBuf.readUshort(tryItemOffset + HANDLER_OFFSET_OFFSET); this.exceptionHandlersOffset = handlersStartOffset + dexBuf.readUshort(tryItemOffset + HANDLER_OFFSET_OFFSET);
} }
@Override public int getStartIndex() { return startIndex; } @Override public int getStartCodeOffset() { return startCodeOffset; }
@Override public int getInstructionCount() { return instructionCount; } @Override public int getCodeUnitCount() { return codeUnitCount; }
@Nonnull @Nonnull
@Override @Override
@ -90,7 +74,7 @@ public class DexBackedTryBlock implements TryBlock {
@Nonnull @Nonnull
@Override @Override
protected ExceptionHandler readItem(@Nonnull DexReader reader, int index) { protected ExceptionHandler readItem(@Nonnull DexReader reader, int index) {
return new DexBackedExceptionHandler(reader, instructionOffsetMap); return new DexBackedExceptionHandler(reader);
} }
@Override @Override
@ -108,9 +92,9 @@ public class DexBackedTryBlock implements TryBlock {
@Override @Override
protected ExceptionHandler readItem(@Nonnull DexReader dexReader, int index) { protected ExceptionHandler readItem(@Nonnull DexReader dexReader, int index) {
if (index == sizeWithCatchAll-1) { if (index == sizeWithCatchAll-1) {
return new DexBackedCatchAllExceptionHandler(dexReader, instructionOffsetMap); return new DexBackedCatchAllExceptionHandler(dexReader);
} else { } else {
return new DexBackedExceptionHandler(dexReader, instructionOffsetMap); return new DexBackedExceptionHandler(dexReader);
} }
} }

View File

@ -1,63 +0,0 @@
/*
* Copyright 2012, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.jf.dexlib2.dexbacked.util;
import org.jf.util.ExceptionWithContext;
import javax.annotation.Nonnull;
import java.util.Arrays;
public class InstructionOffsetMap {
@Nonnull private final int[] instructionOffsets;
public InstructionOffsetMap(@Nonnull int[] instructionOffsets) {
this.instructionOffsets = instructionOffsets;
}
public int getInstructionIndexAtOffsetExact(int offset) {
int index = Arrays.binarySearch(instructionOffsets, offset);
if (index < 0) {
throw new ExceptionWithContext("No instruction at offset %d", offset);
}
return index;
}
public int getInstructionIndexAtOffset(int offset) {
int index = Arrays.binarySearch(instructionOffsets, offset);
if (index < 0) {
// This would fail if index was -1 (i.e. insertion point of 0). Luckily, we can ignore this case, because
// offset will always be non-negative, and the offset of the first instruction will always be 0.
return (~index) - 1;
}
return index;
}
}

View File

@ -35,5 +35,5 @@ import javax.annotation.Nullable;
public interface ExceptionHandler { public interface ExceptionHandler {
@Nullable String getExceptionType(); @Nullable String getExceptionType();
int getHandlerIndex(); int getHandlerCodeOffset();
} }

View File

@ -35,7 +35,7 @@ import javax.annotation.Nonnull;
import java.util.List; import java.util.List;
public interface TryBlock { public interface TryBlock {
int getStartIndex(); int getStartCodeOffset();
int getInstructionCount(); int getCodeUnitCount();
@Nonnull List<? extends ExceptionHandler> getExceptionHandlers(); @Nonnull List<? extends ExceptionHandler> getExceptionHandlers();
} }

View File

@ -32,5 +32,5 @@
package org.jf.dexlib2.iface.instruction; package org.jf.dexlib2.iface.instruction;
public interface OffsetInstruction extends Instruction { public interface OffsetInstruction extends Instruction {
int getOffset(); int getCodeOffset();
} }

View File

@ -41,12 +41,12 @@ import java.util.List;
public class ImmutableExceptionHandler implements ExceptionHandler { public class ImmutableExceptionHandler implements ExceptionHandler {
@Nullable public final String exceptionType; @Nullable public final String exceptionType;
public final int handlerIndex; public final int handlerCodeOffset;
public ImmutableExceptionHandler(@Nullable String exceptionType, public ImmutableExceptionHandler(@Nullable String exceptionType,
int handlerIndex) { int handlerCodeOffset) {
this.exceptionType = exceptionType; this.exceptionType = exceptionType;
this.handlerIndex = handlerIndex; this.handlerCodeOffset = handlerCodeOffset;
} }
public static ImmutableExceptionHandler of(ExceptionHandler exceptionHandler) { public static ImmutableExceptionHandler of(ExceptionHandler exceptionHandler) {
@ -55,11 +55,11 @@ public class ImmutableExceptionHandler implements ExceptionHandler {
} }
return new ImmutableExceptionHandler( return new ImmutableExceptionHandler(
exceptionHandler.getExceptionType(), exceptionHandler.getExceptionType(),
exceptionHandler.getHandlerIndex()); exceptionHandler.getHandlerCodeOffset());
} }
@Nullable @Override public String getExceptionType() { return exceptionType; } @Nullable @Override public String getExceptionType() { return exceptionType; }
@Override public int getHandlerIndex() { return handlerIndex; } @Override public int getHandlerCodeOffset() { return handlerCodeOffset; }
@Nonnull @Nonnull
public static ImmutableList<ImmutableExceptionHandler> immutableListOf(List<? extends ExceptionHandler> list) { public static ImmutableList<ImmutableExceptionHandler> immutableListOf(List<? extends ExceptionHandler> list) {

View File

@ -42,23 +42,23 @@ import javax.annotation.Nullable;
import java.util.List; import java.util.List;
public class ImmutableTryBlock implements TryBlock { public class ImmutableTryBlock implements TryBlock {
public final int startIndex; public final int startCodeOffset;
public final int instructionCount; public final int codeUnitCount;
@Nonnull public final ImmutableList<? extends ImmutableExceptionHandler> exceptionHandlers; @Nonnull public final ImmutableList<? extends ImmutableExceptionHandler> exceptionHandlers;
public ImmutableTryBlock(int startIndex, public ImmutableTryBlock(int startCodeOffset,
int instructionCount, int codeUnitCount,
@Nullable List<? extends ExceptionHandler> exceptionHandlers) { @Nullable List<? extends ExceptionHandler> exceptionHandlers) {
this.startIndex = startIndex; this.startCodeOffset = startCodeOffset;
this.instructionCount = instructionCount; this.codeUnitCount = codeUnitCount;
this.exceptionHandlers = ImmutableExceptionHandler.immutableListOf(exceptionHandlers); this.exceptionHandlers = ImmutableExceptionHandler.immutableListOf(exceptionHandlers);
} }
public ImmutableTryBlock(int startIndex, public ImmutableTryBlock(int startCodeOffset,
int instructionCount, int codeUnitCount,
@Nullable ImmutableList<? extends ImmutableExceptionHandler> exceptionHandlers) { @Nullable ImmutableList<? extends ImmutableExceptionHandler> exceptionHandlers) {
this.startIndex = startIndex; this.startCodeOffset = startCodeOffset;
this.instructionCount = instructionCount; this.codeUnitCount = codeUnitCount;
this.exceptionHandlers = Objects.firstNonNull(exceptionHandlers, ImmutableList.<ImmutableExceptionHandler>of()); this.exceptionHandlers = Objects.firstNonNull(exceptionHandlers, ImmutableList.<ImmutableExceptionHandler>of());
} }
@ -67,13 +67,13 @@ public class ImmutableTryBlock implements TryBlock {
return (ImmutableTryBlock)tryBlock; return (ImmutableTryBlock)tryBlock;
} }
return new ImmutableTryBlock( return new ImmutableTryBlock(
tryBlock.getStartIndex(), tryBlock.getStartCodeOffset(),
tryBlock.getInstructionCount(), tryBlock.getCodeUnitCount(),
tryBlock.getExceptionHandlers()); tryBlock.getExceptionHandlers());
} }
@Override public int getStartIndex() { return startIndex; } @Override public int getStartCodeOffset() { return startCodeOffset; }
@Override public int getInstructionCount() { return instructionCount; } @Override public int getCodeUnitCount() { return codeUnitCount; }
@Nonnull @Override public ImmutableList<? extends ImmutableExceptionHandler> getExceptionHandlers() { @Nonnull @Override public ImmutableList<? extends ImmutableExceptionHandler> getExceptionHandlers() {
return exceptionHandlers; return exceptionHandlers;

View File

@ -41,13 +41,13 @@ import javax.annotation.Nonnull;
public class ImmutableInstruction10t extends ImmutableInstruction implements Instruction10t { public class ImmutableInstruction10t extends ImmutableInstruction implements Instruction10t {
public static final Format FORMAT = Format.Format12x; public static final Format FORMAT = Format.Format12x;
public final int offset; public final int codeOffset;
public ImmutableInstruction10t(@Nonnull Opcode opcode, public ImmutableInstruction10t(@Nonnull Opcode opcode,
int offset) { int codeOffset) {
super(opcode); super(opcode);
Preconditions.checkFormat(opcode, FORMAT); Preconditions.checkFormat(opcode, FORMAT);
this.offset = Preconditions.checkByteOffset(offset); this.codeOffset = Preconditions.checkByteCodeOffset(codeOffset);
} }
public static ImmutableInstruction10t of(Instruction10t instruction) { public static ImmutableInstruction10t of(Instruction10t instruction) {
@ -56,9 +56,9 @@ public class ImmutableInstruction10t extends ImmutableInstruction implements Ins
} }
return new ImmutableInstruction10t( return new ImmutableInstruction10t(
instruction.getOpcode(), instruction.getOpcode(),
instruction.getOffset()); instruction.getCodeOffset());
} }
@Override public int getOffset() { return offset; } @Override public int getCodeOffset() { return codeOffset; }
@Override public Format getFormat() { return FORMAT; } @Override public Format getFormat() { return FORMAT; }
} }

View File

@ -41,13 +41,13 @@ import javax.annotation.Nonnull;
public class ImmutableInstruction20t extends ImmutableInstruction implements Instruction20t { public class ImmutableInstruction20t extends ImmutableInstruction implements Instruction20t {
public static final Format FORMAT = Format.Format12x; public static final Format FORMAT = Format.Format12x;
public final int offset; public final int codeOffset;
public ImmutableInstruction20t(@Nonnull Opcode opcode, public ImmutableInstruction20t(@Nonnull Opcode opcode,
int offset) { int codeOffset) {
super(opcode); super(opcode);
Preconditions.checkFormat(opcode, FORMAT); Preconditions.checkFormat(opcode, FORMAT);
this.offset = Preconditions.checkShortOffset(offset); this.codeOffset = Preconditions.checkShortCodeOffset(codeOffset);
} }
public static ImmutableInstruction20t of(Instruction20t instruction) { public static ImmutableInstruction20t of(Instruction20t instruction) {
@ -56,9 +56,9 @@ public class ImmutableInstruction20t extends ImmutableInstruction implements Ins
} }
return new ImmutableInstruction20t( return new ImmutableInstruction20t(
instruction.getOpcode(), instruction.getOpcode(),
(byte)instruction.getOffset()); (byte)instruction.getCodeOffset());
} }
@Override public int getOffset() { return offset; } @Override public int getCodeOffset() { return codeOffset; }
@Override public Format getFormat() { return FORMAT; } @Override public Format getFormat() { return FORMAT; }
} }

View File

@ -42,15 +42,15 @@ public class ImmutableInstruction21t extends ImmutableInstruction implements Ins
public static final Format FORMAT = Format.Format21t; public static final Format FORMAT = Format.Format21t;
public final int registerA; public final int registerA;
public final int offset; public final int codeOffset;
public ImmutableInstruction21t(@Nonnull Opcode opcode, public ImmutableInstruction21t(@Nonnull Opcode opcode,
int registerA, int registerA,
int offset) { int codeOffset) {
super(opcode); super(opcode);
Preconditions.checkFormat(opcode, FORMAT); Preconditions.checkFormat(opcode, FORMAT);
this.registerA = Preconditions.checkByteRegister(registerA); this.registerA = Preconditions.checkByteRegister(registerA);
this.offset = Preconditions.checkShortOffset(offset); this.codeOffset = Preconditions.checkShortCodeOffset(codeOffset);
} }
public static ImmutableInstruction21t of(Instruction21t instruction) { public static ImmutableInstruction21t of(Instruction21t instruction) {
@ -60,11 +60,11 @@ public class ImmutableInstruction21t extends ImmutableInstruction implements Ins
return new ImmutableInstruction21t( return new ImmutableInstruction21t(
instruction.getOpcode(), instruction.getOpcode(),
instruction.getRegisterA(), instruction.getRegisterA(),
instruction.getOffset()); instruction.getCodeOffset());
} }
@Override public int getRegisterA() { return registerA; } @Override public int getRegisterA() { return registerA; }
@Override public int getOffset() { return offset; } @Override public int getCodeOffset() { return codeOffset; }
@Override public Format getFormat() { return FORMAT; } @Override public Format getFormat() { return FORMAT; }
} }

View File

@ -43,17 +43,17 @@ public class ImmutableInstruction22t extends ImmutableInstruction implements Ins
public final int registerA; public final int registerA;
public final int registerB; public final int registerB;
public final int offset; public final int codeOffset;
public ImmutableInstruction22t(@Nonnull Opcode opcode, public ImmutableInstruction22t(@Nonnull Opcode opcode,
int registerA, int registerA,
int registerB, int registerB,
int offset) { int codeOffset) {
super(opcode); super(opcode);
Preconditions.checkFormat(opcode, Format.Format35c); Preconditions.checkFormat(opcode, Format.Format35c);
this.registerA = Preconditions.checkNibbleRegister(registerA); this.registerA = Preconditions.checkNibbleRegister(registerA);
this.registerB = Preconditions.checkNibbleRegister(registerB); this.registerB = Preconditions.checkNibbleRegister(registerB);
this.offset = Preconditions.checkShortOffset(offset); this.codeOffset = Preconditions.checkShortCodeOffset(codeOffset);
} }
public static ImmutableInstruction22t of(Instruction22t instruction) { public static ImmutableInstruction22t of(Instruction22t instruction) {
@ -64,12 +64,12 @@ public class ImmutableInstruction22t extends ImmutableInstruction implements Ins
instruction.getOpcode(), instruction.getOpcode(),
instruction.getRegisterA(), instruction.getRegisterA(),
instruction.getRegisterB(), instruction.getRegisterB(),
instruction.getOffset()); instruction.getCodeOffset());
} }
@Override public int getRegisterA() { return registerA; } @Override public int getRegisterA() { return registerA; }
@Override public int getRegisterB() { return registerB; } @Override public int getRegisterB() { return registerB; }
@Override public int getOffset() { return offset; } @Override public int getCodeOffset() { return codeOffset; }
@Override public Format getFormat() { return FORMAT; } @Override public Format getFormat() { return FORMAT; }
} }

View File

@ -41,13 +41,13 @@ import javax.annotation.Nonnull;
public class ImmutableInstruction30t extends ImmutableInstruction implements Instruction30t { public class ImmutableInstruction30t extends ImmutableInstruction implements Instruction30t {
public static final Format FORMAT = Format.Format12x; public static final Format FORMAT = Format.Format12x;
public final int offset; public final int codeOffset;
public ImmutableInstruction30t(@Nonnull Opcode opcode, public ImmutableInstruction30t(@Nonnull Opcode opcode,
int offset) { int codeOffset) {
super(opcode); super(opcode);
Preconditions.checkFormat(opcode, FORMAT); Preconditions.checkFormat(opcode, FORMAT);
this.offset = offset; this.codeOffset = codeOffset;
} }
public static ImmutableInstruction30t of(Instruction30t instruction) { public static ImmutableInstruction30t of(Instruction30t instruction) {
@ -56,10 +56,10 @@ public class ImmutableInstruction30t extends ImmutableInstruction implements Ins
} }
return new ImmutableInstruction30t( return new ImmutableInstruction30t(
instruction.getOpcode(), instruction.getOpcode(),
instruction.getOffset()); instruction.getCodeOffset());
} }
@Override public int getOffset() { return offset; } @Override public int getCodeOffset() { return codeOffset; }
@Override public Format getFormat() { return FORMAT; } @Override public Format getFormat() { return FORMAT; }
} }

View File

@ -42,15 +42,15 @@ public class ImmutableInstruction31t extends ImmutableInstruction implements Ins
public static final Format FORMAT = Format.Format31t; public static final Format FORMAT = Format.Format31t;
public final int registerA; public final int registerA;
public final int offset; public final int codeOffset;
public ImmutableInstruction31t(@Nonnull Opcode opcode, public ImmutableInstruction31t(@Nonnull Opcode opcode,
int registerA, int registerA,
int offset) { int codeOffset) {
super(opcode); super(opcode);
Preconditions.checkFormat(opcode, FORMAT); Preconditions.checkFormat(opcode, FORMAT);
this.registerA = Preconditions.checkByteRegister(registerA); this.registerA = Preconditions.checkByteRegister(registerA);
this.offset = offset; this.codeOffset = codeOffset;
} }
public static ImmutableInstruction31t of(Instruction31t instruction) { public static ImmutableInstruction31t of(Instruction31t instruction) {
@ -60,11 +60,11 @@ public class ImmutableInstruction31t extends ImmutableInstruction implements Ins
return new ImmutableInstruction31t( return new ImmutableInstruction31t(
instruction.getOpcode(), instruction.getOpcode(),
instruction.getRegisterA(), instruction.getRegisterA(),
instruction.getOffset()); instruction.getCodeOffset());
} }
@Override public int getRegisterA() { return registerA; } @Override public int getRegisterA() { return registerA; }
@Override public int getOffset() { return offset; } @Override public int getCodeOffset() { return codeOffset; }
@Override public Format getFormat() { return FORMAT; } @Override public Format getFormat() { return FORMAT; }
} }

View File

@ -107,7 +107,7 @@ public class Preconditions {
return literal; return literal;
} }
public static int checkByteOffset(int register) { public static int checkByteCodeOffset(int register) {
if ((register & 0xFFFFFF00) != 0) { if ((register & 0xFFFFFF00) != 0) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
String.format("Invalid code offset: %d. Must be between -8 and 7, inclusive.", register)); String.format("Invalid code offset: %d. Must be between -8 and 7, inclusive.", register));
@ -115,7 +115,7 @@ public class Preconditions {
return register; return register;
} }
public static int checkShortOffset(int register) { public static int checkShortCodeOffset(int register) {
if ((register & 0xFFFF0000) != 0) { if ((register & 0xFFFF0000) != 0) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
String.format("Invalid code offset: %d. Must be between -32768 and 32767, inclusive.", register)); String.format("Invalid code offset: %d. Must be between -32768 and 32767, inclusive.", register));