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.CommentingIndentingWriter;
32import org.jf.baksmali.Adaptors.LabelMethodItem;
33import org.jf.baksmali.Adaptors.MethodDefinition;
34import org.jf.dexlib2.iface.instruction.SwitchElement;
35import org.jf.dexlib2.iface.instruction.formats.SparseSwitchPayload;
36import org.jf.util.IndentingWriter;
37import org.jf.baksmali.Renderers.IntegerRenderer;
38
39import java.io.IOException;
40import java.util.ArrayList;
41import java.util.List;
42
43public class SparseSwitchMethodItem extends InstructionMethodItem<SparseSwitchPayload> {
44    private final List<SparseSwitchTarget> targets;
45
46    // Whether this sparse switch instruction should be commented out because it is never referenced
47    private boolean commentedOut;
48
49    public SparseSwitchMethodItem(MethodDefinition methodDef, int codeAddress, SparseSwitchPayload instruction) {
50        super(methodDef, codeAddress, instruction);
51
52        int baseCodeAddress = methodDef.getSparseSwitchBaseAddress(codeAddress);
53
54        targets = new ArrayList<SparseSwitchTarget>();
55        if (baseCodeAddress >= 0) {
56            for (SwitchElement switchElement: instruction.getSwitchElements()) {
57                LabelMethodItem label = methodDef.getLabelCache().internLabel(
58                        new LabelMethodItem( methodDef.classDef.options, baseCodeAddress + switchElement.getOffset(),
59                                "sswitch_"));
60                targets.add(new SparseSwitchLabelTarget(switchElement.getKey(), label));
61            }
62        } else {
63            commentedOut = true;
64            //if we couldn't determine a base address, just use relative offsets rather than labels
65            for (SwitchElement switchElement: instruction.getSwitchElements()) {
66                targets.add(new SparseSwitchOffsetTarget(switchElement.getKey(), switchElement.getOffset()));
67            }
68        }
69    }
70
71    @Override
72    public boolean writeTo(IndentingWriter writer) throws IOException {
73        if (commentedOut) {
74            writer = new CommentingIndentingWriter(writer);
75        }
76
77        writer.write(".sparse-switch\n");
78        writer.indent(4);
79        for (SparseSwitchTarget target: targets) {
80            IntegerRenderer.writeTo(writer, target.getKey());
81            writer.write(" -> ");
82            target.writeTargetTo(writer);
83            writeCommentIfResourceId(writer, target.getKey());
84            writer.write('\n');
85        }
86        writer.deindent(4);
87        writer.write(".end sparse-switch");
88        return true;
89    }
90
91    private static abstract class SparseSwitchTarget {
92        private final int key;
93        public SparseSwitchTarget(int key) {
94            this.key = key;
95        }
96        public int getKey() { return key; }
97        public abstract void writeTargetTo(IndentingWriter writer) throws IOException;
98    }
99
100    private static class SparseSwitchLabelTarget extends SparseSwitchTarget {
101        private final LabelMethodItem target;
102        public SparseSwitchLabelTarget(int key, LabelMethodItem target) {
103            super(key);
104            this.target = target;
105        }
106
107        public void writeTargetTo(IndentingWriter writer) throws IOException {
108            target.writeTo(writer);
109        }
110    }
111
112    private static class SparseSwitchOffsetTarget extends SparseSwitchTarget {
113        private final int target;
114        public SparseSwitchOffsetTarget(int key, int target) {
115            super(key);
116            this.target = target;
117        }
118
119        public void writeTargetTo(IndentingWriter writer) throws IOException {
120            if (target >= 0) {
121                writer.write('+');
122            }
123            writer.printSignedIntAsDec(target);
124        }
125    }
126}
127