Comment out unused switch payload instructions

This commit is contained in:
Ben Gruver 2015-03-20 16:38:19 -07:00 committed by Connor Tumbleson
parent 7b3e5a1668
commit fa773b5382
2 changed files with 17 additions and 1 deletions

View File

@ -28,6 +28,7 @@
package org.jf.baksmali.Adaptors.Format; package org.jf.baksmali.Adaptors.Format;
import org.jf.baksmali.Adaptors.CommentingIndentingWriter;
import org.jf.baksmali.Adaptors.LabelMethodItem; import org.jf.baksmali.Adaptors.LabelMethodItem;
import org.jf.baksmali.Adaptors.MethodDefinition; import org.jf.baksmali.Adaptors.MethodDefinition;
import org.jf.dexlib2.iface.instruction.SwitchElement; import org.jf.dexlib2.iface.instruction.SwitchElement;
@ -43,6 +44,9 @@ public class PackedSwitchMethodItem extends InstructionMethodItem<PackedSwitchPa
private final List<PackedSwitchTarget> targets; private final List<PackedSwitchTarget> targets;
private final int firstKey; private final int firstKey;
// Whether this sparse switch instruction should be commented out because it is never referenced
private boolean commentedOut;
public PackedSwitchMethodItem(MethodDefinition methodDef, int codeAddress, PackedSwitchPayload instruction) { public PackedSwitchMethodItem(MethodDefinition methodDef, int codeAddress, PackedSwitchPayload instruction) {
super(methodDef, codeAddress, instruction); super(methodDef, codeAddress, instruction);
@ -51,7 +55,6 @@ public class PackedSwitchMethodItem extends InstructionMethodItem<PackedSwitchPa
targets = new ArrayList<PackedSwitchTarget>(); targets = new ArrayList<PackedSwitchTarget>();
boolean first = true; boolean first = true;
//TODO: does dalvik allow switc payloads with no cases?
int firstKey = 0; int firstKey = 0;
if (baseCodeAddress >= 0) { if (baseCodeAddress >= 0) {
for (SwitchElement switchElement: instruction.getSwitchElements()) { for (SwitchElement switchElement: instruction.getSwitchElements()) {
@ -65,6 +68,7 @@ public class PackedSwitchMethodItem extends InstructionMethodItem<PackedSwitchPa
targets.add(new PackedSwitchLabelTarget(label)); targets.add(new PackedSwitchLabelTarget(label));
} }
} else { } else {
commentedOut = true;
for (SwitchElement switchElement: instruction.getSwitchElements()) { for (SwitchElement switchElement: instruction.getSwitchElements()) {
if (first) { if (first) {
firstKey = switchElement.getKey(); firstKey = switchElement.getKey();
@ -78,6 +82,9 @@ public class PackedSwitchMethodItem extends InstructionMethodItem<PackedSwitchPa
@Override @Override
public boolean writeTo(IndentingWriter writer) throws IOException { public boolean writeTo(IndentingWriter writer) throws IOException {
if (commentedOut) {
writer = new CommentingIndentingWriter(writer);
}
writer.write(".packed-switch "); writer.write(".packed-switch ");
IntegerRenderer.writeTo(writer, firstKey); IntegerRenderer.writeTo(writer, firstKey);
writer.indent(4); writer.indent(4);

View File

@ -28,6 +28,7 @@
package org.jf.baksmali.Adaptors.Format; package org.jf.baksmali.Adaptors.Format;
import org.jf.baksmali.Adaptors.CommentingIndentingWriter;
import org.jf.baksmali.Adaptors.LabelMethodItem; import org.jf.baksmali.Adaptors.LabelMethodItem;
import org.jf.baksmali.Adaptors.MethodDefinition; import org.jf.baksmali.Adaptors.MethodDefinition;
import org.jf.dexlib2.iface.instruction.SwitchElement; import org.jf.dexlib2.iface.instruction.SwitchElement;
@ -42,6 +43,9 @@ import java.util.List;
public class SparseSwitchMethodItem extends InstructionMethodItem<SparseSwitchPayload> { public class SparseSwitchMethodItem extends InstructionMethodItem<SparseSwitchPayload> {
private final List<SparseSwitchTarget> targets; private final List<SparseSwitchTarget> targets;
// Whether this sparse switch instruction should be commented out because it is never referenced
private boolean commentedOut;
public SparseSwitchMethodItem(MethodDefinition methodDef, int codeAddress, SparseSwitchPayload instruction) { public SparseSwitchMethodItem(MethodDefinition methodDef, int codeAddress, SparseSwitchPayload instruction) {
super(methodDef, codeAddress, instruction); super(methodDef, codeAddress, instruction);
@ -56,6 +60,7 @@ public class SparseSwitchMethodItem extends InstructionMethodItem<SparseSwitchPa
targets.add(new SparseSwitchLabelTarget(switchElement.getKey(), label)); targets.add(new SparseSwitchLabelTarget(switchElement.getKey(), label));
} }
} else { } else {
commentedOut = true;
//if we couldn't determine a base address, just use relative offsets rather than labels //if we couldn't determine a base address, just use relative offsets rather than labels
for (SwitchElement switchElement: instruction.getSwitchElements()) { for (SwitchElement switchElement: instruction.getSwitchElements()) {
targets.add(new SparseSwitchOffsetTarget(switchElement.getKey(), switchElement.getOffset())); targets.add(new SparseSwitchOffsetTarget(switchElement.getKey(), switchElement.getOffset()));
@ -65,6 +70,10 @@ public class SparseSwitchMethodItem extends InstructionMethodItem<SparseSwitchPa
@Override @Override
public boolean writeTo(IndentingWriter writer) throws IOException { public boolean writeTo(IndentingWriter writer) throws IOException {
if (commentedOut) {
writer = new CommentingIndentingWriter(writer);
}
writer.write(".sparse-switch\n"); writer.write(".sparse-switch\n");
writer.indent(4); writer.indent(4);
for (SparseSwitchTarget target: targets) { for (SparseSwitchTarget target: targets) {