package org.jf.dexlib2.writer;
import org.jf.dexlib2.DebugItemType;
import org.jf.util.ExceptionWithContext;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.IOException;
public class DebugWriter<StringKey extends CharSequence, TypeKey extends CharSequence> {
@Nonnull private final StringSection<StringKey, ?> stringSection;
@Nonnull private final TypeSection<StringKey, TypeKey, ?> typeSection;
@Nonnull private final DexDataWriter writer;
private int currentAddress;
private int currentLine;
DebugWriter(@Nonnull StringSection<StringKey, ?> stringSection,
@Nonnull TypeSection<StringKey, TypeKey, ?> typeSection,
@Nonnull DexDataWriter writer) {
this.stringSection = stringSection;
this.typeSection = typeSection;
this.writer = writer;
}
void reset(int startLine) {
this.currentAddress = 0;
this.currentLine = startLine;
}
public void writeStartLocal(int codeAddress, int register,
@Nullable StringKey name,
@Nullable TypeKey type,
@Nullable StringKey signature) throws IOException {
int nameIndex = stringSection.getNullableItemIndex(name);
int typeIndex = typeSection.getNullableItemIndex(type);
int signatureIndex = stringSection.getNullableItemIndex(signature);
writeAdvancePC(codeAddress);
if (signatureIndex == DexWriter.NO_INDEX) {
writer.write(DebugItemType.START_LOCAL);
writer.writeUleb128(register);
writer.writeUleb128(nameIndex + 1);
writer.writeUleb128(typeIndex + 1);
} else {
writer.write(DebugItemType.START_LOCAL_EXTENDED);
writer.writeUleb128(register);
writer.writeUleb128(nameIndex + 1);
writer.writeUleb128(typeIndex + 1);
writer.writeUleb128(signatureIndex + 1);
}
}
public void writeEndLocal(int codeAddress, int register) throws IOException {
writeAdvancePC(codeAddress);
writer.write(DebugItemType.END_LOCAL);
writer.writeUleb128(register);
}
public void writeRestartLocal(int codeAddress, int register) throws IOException {
writeAdvancePC(codeAddress);
writer.write(DebugItemType.RESTART_LOCAL);
writer.writeUleb128(register);
}
public void writePrologueEnd(int codeAddress) throws IOException {
writeAdvancePC(codeAddress);
writer.write(DebugItemType.PROLOGUE_END);
}
public void writeEpilogueBegin(int codeAddress) throws IOException {
writeAdvancePC(codeAddress);
writer.write(DebugItemType.EPILOGUE_BEGIN);
}
public void writeLineNumber(int codeAddress, int lineNumber) throws IOException {
int lineDelta = lineNumber - currentLine;
int addressDelta = codeAddress - currentAddress;
if (addressDelta < 0) {
throw new ExceptionWithContext("debug info items must have non-decreasing code addresses");
}
if (lineDelta < -4 || lineDelta > 10) {
writeAdvanceLine(lineNumber);
lineDelta = 0;
}
if ((lineDelta < 2 && addressDelta > 16) || (lineDelta > 1 && addressDelta > 15)) {
writeAdvancePC(codeAddress);
addressDelta = 0;
}
writeSpecialOpcode(lineDelta, addressDelta);
}
public void writeSetSourceFile(int codeAddress, @Nullable StringKey sourceFile) throws IOException {
writeAdvancePC(codeAddress);
writer.write(DebugItemType.SET_SOURCE_FILE);
writer.writeUleb128(stringSection.getNullableItemIndex(sourceFile) + 1);
}
private void writeAdvancePC(int address) throws IOException {
int addressDelta = address - currentAddress;
if (addressDelta > 0) {
writer.write(1);
writer.writeUleb128(addressDelta);
currentAddress = address;
}
}
private void writeAdvanceLine(int line) throws IOException {
int lineDelta = line - currentLine;
if (lineDelta != 0) {
writer.write(2);
writer.writeSleb128(lineDelta);
currentLine = line;
}
}
private static final int LINE_BASE = -4;
private static final int LINE_RANGE = 15;
private static final int FIRST_SPECIAL = 0x0a;
private void writeSpecialOpcode(int lineDelta, int addressDelta) throws IOException {
writer.write((byte)(FIRST_SPECIAL + (addressDelta * LINE_RANGE) + (lineDelta - LINE_BASE)));
currentLine += lineDelta;
currentAddress += addressDelta;
}
}