Skip to content

Commit

Permalink
Comment out unused switch payload instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
JesusFreke authored and iBotPeaches committed May 1, 2015
1 parent 7b3e5a1 commit fa773b5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

package org.jf.baksmali.Adaptors.Format;

import org.jf.baksmali.Adaptors.CommentingIndentingWriter;
import org.jf.baksmali.Adaptors.LabelMethodItem;
import org.jf.baksmali.Adaptors.MethodDefinition;
import org.jf.dexlib2.iface.instruction.SwitchElement;
Expand All @@ -43,6 +44,9 @@ public class PackedSwitchMethodItem extends InstructionMethodItem<PackedSwitchPa
private final List<PackedSwitchTarget> targets;
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) {
super(methodDef, codeAddress, instruction);

Expand All @@ -51,7 +55,6 @@ public PackedSwitchMethodItem(MethodDefinition methodDef, int codeAddress, Packe
targets = new ArrayList<PackedSwitchTarget>();

boolean first = true;
//TODO: does dalvik allow switc payloads with no cases?
int firstKey = 0;
if (baseCodeAddress >= 0) {
for (SwitchElement switchElement: instruction.getSwitchElements()) {
Expand All @@ -65,6 +68,7 @@ public PackedSwitchMethodItem(MethodDefinition methodDef, int codeAddress, Packe
targets.add(new PackedSwitchLabelTarget(label));
}
} else {
commentedOut = true;
for (SwitchElement switchElement: instruction.getSwitchElements()) {
if (first) {
firstKey = switchElement.getKey();
Expand All @@ -78,6 +82,9 @@ public PackedSwitchMethodItem(MethodDefinition methodDef, int codeAddress, Packe

@Override
public boolean writeTo(IndentingWriter writer) throws IOException {
if (commentedOut) {
writer = new CommentingIndentingWriter(writer);
}
writer.write(".packed-switch ");
IntegerRenderer.writeTo(writer, firstKey);
writer.indent(4);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

package org.jf.baksmali.Adaptors.Format;

import org.jf.baksmali.Adaptors.CommentingIndentingWriter;
import org.jf.baksmali.Adaptors.LabelMethodItem;
import org.jf.baksmali.Adaptors.MethodDefinition;
import org.jf.dexlib2.iface.instruction.SwitchElement;
Expand All @@ -42,6 +43,9 @@
public class SparseSwitchMethodItem extends InstructionMethodItem<SparseSwitchPayload> {
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) {
super(methodDef, codeAddress, instruction);

Expand All @@ -56,6 +60,7 @@ public SparseSwitchMethodItem(MethodDefinition methodDef, int codeAddress, Spars
targets.add(new SparseSwitchLabelTarget(switchElement.getKey(), label));
}
} else {
commentedOut = true;
//if we couldn't determine a base address, just use relative offsets rather than labels
for (SwitchElement switchElement: instruction.getSwitchElements()) {
targets.add(new SparseSwitchOffsetTarget(switchElement.getKey(), switchElement.getOffset()));
Expand All @@ -65,6 +70,10 @@ public SparseSwitchMethodItem(MethodDefinition methodDef, int codeAddress, Spars

@Override
public boolean writeTo(IndentingWriter writer) throws IOException {
if (commentedOut) {
writer = new CommentingIndentingWriter(writer);
}

writer.write(".sparse-switch\n");
writer.indent(4);
for (SparseSwitchTarget target: targets) {
Expand Down

0 comments on commit fa773b5

Please sign in to comment.