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( methodDef.classDef.options, baseCodeAddress + switchElement.getOffset(),
55                                "sswitch_"));
56                targets.add(new SparseSwitchLabelTarget(switchElement.getKey(), label));
57            }
58        } else {
59            //if we couldn't determine a base address, just use relative offsets rather than labels
60            for (SwitchElement switchElement: instruction.getSwitchElements()) {
61                targets.add(new SparseSwitchOffsetTarget(switchElement.getKey(), switchElement.getOffset()));
62            }
63        }
64    }
65
66    @Override
67    public boolean writeTo(IndentingWriter writer) throws IOException {
68        writer.write(".sparse-switch\n");
69        writer.indent(4);
70        for (SparseSwitchTarget target: targets) {
71            IntegerRenderer.writeTo(writer, target.getKey());
72            writer.write(" -> ");
73            target.writeTargetTo(writer);
74            writeResourceId(writer, target.getKey());
75            writer.write('\n');
76        }
77        writer.deindent(4);
78        writer.write(".end sparse-switch");
79        return true;
80    }
81
82    private static abstract class SparseSwitchTarget {
83        private final int key;
84        public SparseSwitchTarget(int key) {
85            this.key = key;
86        }
87        public int getKey() { return key; }
88        public abstract void writeTargetTo(IndentingWriter writer) throws IOException;
89    }
90
91    private static class SparseSwitchLabelTarget extends SparseSwitchTarget {
92        private final LabelMethodItem target;
93        public SparseSwitchLabelTarget(int key, LabelMethodItem target) {
94            super(key);
95            this.target = target;
96        }
97
98        public void writeTargetTo(IndentingWriter writer) throws IOException {
99            target.writeTo(writer);
100        }
101    }
102
103    private static class SparseSwitchOffsetTarget extends SparseSwitchTarget {
104        private final int target;
105        public SparseSwitchOffsetTarget(int key, int target) {
106            super(key);
107            this.target = target;
108        }
109
110        public void writeTargetTo(IndentingWriter writer) throws IOException {
111            if (target >= 0) {
112                writer.write('+');
113            }
114            writer.printSignedIntAsDec(target);
115        }
116    }
117}
118