InstructionMethodItemFactory.java revision f7344d33d01953eee129f3cfa2b9655eb65bfe2f
15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)/*
25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * [The "BSD licence"]
35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * Copyright (c) 2010 Ben Gruver (JesusFreke)
45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * All rights reserved.
55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) *
65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * Redistribution and use in source and binary forms, with or without
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * modification, are permitted provided that the following conditions
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * are met:
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * 1. Redistributions of source code must retain the above copyright
105f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) *    notice, this list of conditions and the following disclaimer.
11116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch * 2. Redistributions in binary form must reproduce the above copyright
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) *    notice, this list of conditions and the following disclaimer in the
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) *    documentation and/or other materials provided with the distribution.
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * 3. The name of the author may not be used to endorse or promote products
155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) *    derived from this software without specific prior written permission.
165f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) *
175f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
185f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) */
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)package org.jf.baksmali.Adaptors.Format;
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)import org.jf.baksmali.Adaptors.MethodDefinition;
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)import org.jf.dexlib.Code.Format.ArrayDataPseudoInstruction;
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)import org.jf.dexlib.Code.Format.PackedSwitchDataPseudoInstruction;
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)import org.jf.dexlib.Code.Format.SparseSwitchDataPseudoInstruction;
355f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)import org.jf.dexlib.Code.Format.UnresolvedNullReference;
365f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)import org.jf.dexlib.Code.Instruction;
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)import org.jf.dexlib.Code.OffsetInstruction;
385f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)import org.jf.dexlib.CodeItem;
395f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
405f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)public class InstructionMethodItemFactory {
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    private InstructionMethodItemFactory() {
425f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    }
435f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
445f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    public static InstructionMethodItem makeInstructionFormatMethodItem(MethodDefinition methodDefinition,
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                                                              CodeItem codeItem,
465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                                                              int codeAddress,
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                                                              Instruction instruction) {
485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        if (instruction instanceof OffsetInstruction) {
495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            return new OffsetInstructionFormatMethodItem(methodDefinition.getLabelCache(), codeItem, codeAddress,
50                    instruction);
51        }
52
53        switch (instruction.getFormat()) {
54            case ArrayData:
55                return new ArrayDataMethodItem(codeItem, codeAddress, false,
56                        (ArrayDataPseudoInstruction)instruction);
57            case PackedSwitchData:
58                return new PackedSwitchMethodItem(methodDefinition, codeItem, codeAddress, false,
59                        (PackedSwitchDataPseudoInstruction)instruction);
60            case SparseSwitchData:
61                return new SparseSwitchMethodItem(methodDefinition, codeItem, codeAddress, false,
62                        (SparseSwitchDataPseudoInstruction)instruction);
63            case UnresolvedNullReference:
64                assert false;
65                throw new RuntimeException("UnresolvedNullReference not supported, use " +
66                        "makeAnalyzedInstructionFormatMethodItem instead");
67            default:
68                return new InstructionMethodItem(codeItem, codeAddress, instruction);
69        }
70    }
71
72    public static InstructionMethodItem makeAnalyzedInstructionFormatMethodItem(MethodDefinition methodDefinition,
73                                                                              CodeItem codeItem, int codeAddress,
74                                                                              boolean isDead,
75                                                                              Instruction instruction,
76                                                                              boolean isLastInstruction) {
77        if (instruction instanceof OffsetInstruction) {
78            return new OffsetInstructionFormatMethodItem(methodDefinition.getLabelCache(), codeItem, codeAddress,
79                    instruction);
80        }
81
82        switch (instruction.getFormat()) {
83            case ArrayData:
84                return new ArrayDataMethodItem(codeItem, codeAddress, isDead,
85                        (ArrayDataPseudoInstruction)instruction);
86            case PackedSwitchData:
87                return new PackedSwitchMethodItem(methodDefinition, codeItem, codeAddress, isDead,
88                        (PackedSwitchDataPseudoInstruction)instruction);
89            case SparseSwitchData:
90                return new SparseSwitchMethodItem(methodDefinition, codeItem, codeAddress, isDead,
91                        (SparseSwitchDataPseudoInstruction)instruction);
92            case UnresolvedNullReference:
93                return new UnresolvedNullReferenceMethodItem(codeItem, codeAddress,
94                        (UnresolvedNullReference)instruction, isLastInstruction);
95            default:
96                return new InstructionMethodItem(codeItem, codeAddress, instruction);
97        }
98    }
99}
100