PackedSwitchMethodItem.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.PackedSwitchPayload; 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 PackedSwitchMethodItem extends InstructionMethodItem<PackedSwitchPayload> { 43 private final List<PackedSwitchTarget> targets; 44 private final int firstKey; 45 46 public PackedSwitchMethodItem(MethodDefinition methodDef, int codeAddress, PackedSwitchPayload instruction) { 47 super(methodDef, codeAddress, instruction); 48 49 int baseCodeAddress = methodDef.getPackedSwitchBaseAddress(codeAddress); 50 51 targets = new ArrayList<PackedSwitchTarget>(); 52 53 boolean first = true; 54 //TODO: does dalvik allow switc payloads with no cases? 55 int firstKey = 0; 56 if (baseCodeAddress >= 0) { 57 for (SwitchElement switchElement: instruction.getSwitchElements()) { 58 if (first) { 59 firstKey = switchElement.getKey(); 60 first = false; 61 } 62 LabelMethodItem label = methodDef.getLabelCache().internLabel( 63 new LabelMethodItem(baseCodeAddress + switchElement.getOffset(), "pswitch_")); 64 targets.add(new PackedSwitchLabelTarget(label)); 65 } 66 } else { 67 for (SwitchElement switchElement: instruction.getSwitchElements()) { 68 if (first) { 69 firstKey = switchElement.getKey(); 70 first = false; 71 } 72 targets.add(new PackedSwitchOffsetTarget(switchElement.getOffset())); 73 } 74 } 75 this.firstKey = firstKey; 76 } 77 78 @Override 79 public boolean writeTo(IndentingWriter writer) throws IOException { 80 writer.write(".packed-switch "); 81 IntegerRenderer.writeTo(writer, firstKey); 82 writer.indent(4); 83 writer.write('\n'); 84 for (PackedSwitchTarget target: targets) { 85 target.writeTargetTo(writer); 86 writer.write('\n'); 87 } 88 writer.deindent(4); 89 writer.write(".end packed-switch"); 90 return true; 91 } 92 93 private static abstract class PackedSwitchTarget { 94 public abstract void writeTargetTo(IndentingWriter writer) throws IOException; 95 } 96 97 private static class PackedSwitchLabelTarget extends PackedSwitchTarget { 98 private final LabelMethodItem target; 99 public PackedSwitchLabelTarget(LabelMethodItem target) { 100 this.target = target; 101 } 102 public void writeTargetTo(IndentingWriter writer) throws IOException { 103 target.writeTo(writer); 104 } 105 } 106 107 private static class PackedSwitchOffsetTarget extends PackedSwitchTarget { 108 private final int target; 109 public PackedSwitchOffsetTarget(int target) { 110 this.target = target; 111 } 112 public void writeTargetTo(IndentingWriter writer) throws IOException { 113 if (target >= 0) { 114 writer.write('+'); 115 } 116 writer.printSignedIntAsDec(target); 117 } 118 } 119} 120