SparseSwitchMethodItem.java revision 09058f9914385025020e01125452a884f1b1fe11
1/* 2 * [The "BSD licence"] 3 * Copyright (c) 2010 Ben Gruver (JesusFreke) 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29package org.jf.baksmali.Adaptors.Format; 30 31import org.jf.baksmali.Adaptors.LabelMethodItem; 32import org.jf.baksmali.Adaptors.MethodDefinition; 33import org.jf.dexlib2.iface.instruction.SwitchElement; 34import org.jf.dexlib2.iface.instruction.formats.SparseSwitchPayload; 35import org.jf.util.IndentingWriter; 36import org.jf.baksmali.Renderers.IntegerRenderer; 37 38import java.io.IOException; 39import java.util.ArrayList; 40import java.util.List; 41 42public class SparseSwitchMethodItem extends InstructionMethodItem<SparseSwitchPayload> { 43 private final List<SparseSwitchTarget> targets; 44 45 public SparseSwitchMethodItem(MethodDefinition methodDef, int codeAddress, SparseSwitchPayload instruction) { 46 super(methodDef, codeAddress, instruction); 47 48 int baseCodeAddress = methodDef.getSparseSwitchBaseAddress(codeAddress); 49 50 targets = new ArrayList<SparseSwitchTarget>(); 51 if (baseCodeAddress >= 0) { 52 for (SwitchElement switchElement: instruction.getSwitchElements()) { 53 LabelMethodItem label = methodDef.getLabelCache().internLabel( 54 new LabelMethodItem(baseCodeAddress + switchElement.getOffset(), "sswitch_")); 55 targets.add(new SparseSwitchLabelTarget(switchElement.getKey(), label)); 56 } 57 } else { 58 //if we couldn't determine a base address, just use relative offsets rather than labels 59 for (SwitchElement switchElement: instruction.getSwitchElements()) { 60 targets.add(new SparseSwitchOffsetTarget(switchElement.getKey(), switchElement.getOffset())); 61 } 62 } 63 } 64 65 @Override 66 public boolean writeTo(IndentingWriter writer) throws IOException { 67 writer.write(".sparse-switch\n"); 68 writer.indent(4); 69 for (SparseSwitchTarget target: targets) { 70 IntegerRenderer.writeTo(writer, target.getKey()); 71 writer.write(" -> "); 72 target.writeTargetTo(writer); 73 writer.write('\n'); 74 } 75 writer.deindent(4); 76 writer.write(".end sparse-switch"); 77 return true; 78 } 79 80 private static abstract class SparseSwitchTarget { 81 private final int key; 82 public SparseSwitchTarget(int key) { 83 this.key = key; 84 } 85 public int getKey() { return key; } 86 public abstract void writeTargetTo(IndentingWriter writer) throws IOException; 87 } 88 89 private static class SparseSwitchLabelTarget extends SparseSwitchTarget { 90 private final LabelMethodItem target; 91 public SparseSwitchLabelTarget(int key, LabelMethodItem target) { 92 super(key); 93 this.target = target; 94 } 95 96 public void writeTargetTo(IndentingWriter writer) throws IOException { 97 target.writeTo(writer); 98 } 99 } 100 101 private static class SparseSwitchOffsetTarget extends SparseSwitchTarget { 102 private final int target; 103 public SparseSwitchOffsetTarget(int key, int target) { 104 super(key); 105 this.target = target; 106 } 107 108 public void writeTargetTo(IndentingWriter writer) throws IOException { 109 if (target >= 0) { 110 writer.write('+'); 111 } 112 writer.printSignedIntAsDec(target); 113 } 114 } 115} 116