PackedSwitchMethodItem.java revision 93aa50139c4641d931b05608f73af8879c0de1c2
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(methodDef.classDef.options, baseCodeAddress + switchElement.getOffset(),
64                                "pswitch_"));
65                targets.add(new PackedSwitchLabelTarget(label));
66            }
67        } else {
68            for (SwitchElement switchElement: instruction.getSwitchElements()) {
69                if (first) {
70                    firstKey = switchElement.getKey();
71                    first = false;
72                }
73                targets.add(new PackedSwitchOffsetTarget(switchElement.getOffset()));
74            }
75        }
76        this.firstKey = firstKey;
77    }
78
79    @Override
80    public boolean writeTo(IndentingWriter writer) throws IOException {
81        writer.write(".packed-switch ");
82        IntegerRenderer.writeTo(writer, firstKey);
83        writer.indent(4);
84        writer.write('\n');
85        for (PackedSwitchTarget target: targets) {
86            target.writeTargetTo(writer);
87            writer.write('\n');
88        }
89        writer.deindent(4);
90        writer.write(".end packed-switch");
91        return true;
92    }
93
94    private static abstract class PackedSwitchTarget {
95        public abstract void writeTargetTo(IndentingWriter writer) throws IOException;
96    }
97
98    private static class PackedSwitchLabelTarget extends PackedSwitchTarget {
99        private final LabelMethodItem target;
100        public PackedSwitchLabelTarget(LabelMethodItem target) {
101            this.target = target;
102        }
103        public void writeTargetTo(IndentingWriter writer) throws IOException {
104            target.writeTo(writer);
105        }
106    }
107
108    private static class PackedSwitchOffsetTarget extends PackedSwitchTarget {
109        private final int target;
110        public PackedSwitchOffsetTarget(int target) {
111            this.target = target;
112        }
113        public void writeTargetTo(IndentingWriter writer) throws IOException {
114            if (target >= 0) {
115                writer.write('+');
116            }
117            writer.printSignedIntAsDec(target);
118        }
119    }
120}
121