OffsetInstructionFormatMethodItem.java revision 754b3c4dc009b7a02e39001560c3f0fd6a7cc2c0
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/*
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * [The "BSD licence"]
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Copyright (c) 2010 Ben Gruver (JesusFreke)
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * All rights reserved.
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Redistribution and use in source and binary forms, with or without
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * modification, are permitted provided that the following conditions
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * are met:
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 1. Redistributions of source code must retain the above copyright
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) *    notice, this list of conditions and the following disclaimer.
11868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) * 2. Redistributions in binary form must reproduce the above copyright
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *    notice, this list of conditions and the following disclaimer in the
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *    documentation and/or other materials provided with the distribution.
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 3. The name of the author may not be used to endorse or promote products
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *    derived from this software without specific prior written permission.
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) *
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)package org.jf.baksmali.Adaptors.Format;
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)import org.jf.baksmali.Adaptors.LabelMethodItem;
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)import org.jf.baksmali.Adaptors.MethodDefinition;
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)import org.jf.dexlib2.Opcode;
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)import org.jf.dexlib2.iface.instruction.OffsetInstruction;
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)import org.jf.util.IndentingWriter;
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)import java.io.IOException;
382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)public class OffsetInstructionFormatMethodItem extends InstructionMethodItem<OffsetInstruction> {
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    protected LabelMethodItem label;
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    public OffsetInstructionFormatMethodItem(MethodDefinition methodDef, int codeAddress,
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                             OffsetInstruction instruction) {
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        super(methodDef, codeAddress, instruction);
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        label = new LabelMethodItem(codeAddress + instruction.getCodeOffset(), getLabelPrefix());
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        label = methodDef.getLabelCache().internLabel(label);
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
49a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
50a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    @Override
51a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    protected void writeTargetLabel(IndentingWriter writer) throws IOException {
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        label.writeTo(writer);
532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    public LabelMethodItem getLabel() {
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        return label;
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    private String getLabelPrefix() {
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        Opcode opcode = instruction.getOpcode();
612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        switch (opcode.format) {
622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            case Format10t:
632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            case Format20t:
642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            case Format30t:
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                return "goto_";
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            case Format21t:
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            case Format22t:
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                return "cond_";
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            case Format31t:
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                if (opcode == Opcode.FILL_ARRAY_DATA) {
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                    return "array_";
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                }
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                if (opcode == Opcode.PACKED_SWITCH) {
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                    return "pswitch_data_";
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                }
762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                // Opcode.SPARSE_SWITCH;
772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                return "sswitch_data_";
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        }
792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        assert false;
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        return null;
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)