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.dexlib.Code.Format;
30
31import org.jf.dexlib.Code.FiveRegisterInstruction;
32import org.jf.dexlib.Code.Instruction;
33import org.jf.dexlib.Code.InstructionWithReference;
34import org.jf.dexlib.Code.Opcode;
35import org.jf.dexlib.DexFile;
36import org.jf.dexlib.Item;
37import org.jf.dexlib.MethodIdItem;
38import org.jf.dexlib.TypeIdItem;
39import org.jf.dexlib.Util.AnnotatedOutput;
40import org.jf.dexlib.Util.NumberUtils;
41
42import static org.jf.dexlib.Code.Opcode.*;
43
44public class Instruction35s extends InstructionWithReference implements FiveRegisterInstruction {
45    public static final Instruction.InstructionFactory Factory = new Factory();
46    private byte regCount;
47    private byte regA;
48    private byte regD;
49    private byte regE;
50    private byte regF;
51    private byte regG;
52
53    public Instruction35s(Opcode opcode, int regCount, byte regD, byte regE, byte regF, byte regG,
54                          byte regA, Item referencedItem) {
55        super(opcode, referencedItem);
56
57        if (regCount > 5) {
58            throw new RuntimeException("regCount cannot be greater than 5");
59        }
60
61        if (regD >= 1 << 4 ||
62                regE >= 1 << 4 ||
63                regF >= 1 << 4 ||
64                regG >= 1 << 4 ||
65                regA >= 1 << 4) {
66            throw new RuntimeException("All register args must fit in 4 bits");
67        }
68
69        checkItem(opcode, referencedItem, regCount);
70
71        this.regCount = (byte)regCount;
72        this.regA = regA;
73        this.regD = regD;
74        this.regE = regE;
75        this.regF = regF;
76        this.regG = regG;
77    }
78
79    protected Instruction35s(DexFile dexFile, Opcode opcode, byte[] buffer, int bufferIndex) {
80        super(dexFile, opcode, buffer, bufferIndex);
81
82        if (getRegCount() > 5) {
83            throw new RuntimeException("regCount cannot be greater than 5");
84        }
85
86        checkItem(opcode, getReferencedItem(), getRegCount());
87
88        this.regCount = NumberUtils.decodeHighUnsignedNibble(buffer[bufferIndex + 1]);
89        this.regA = NumberUtils.decodeLowUnsignedNibble(buffer[bufferIndex + 1]);
90        this.regD = NumberUtils.decodeLowUnsignedNibble(buffer[bufferIndex + 4]);
91        this.regE = NumberUtils.decodeHighUnsignedNibble(buffer[bufferIndex + 4]);
92        this.regF = NumberUtils.decodeLowUnsignedNibble(buffer[bufferIndex + 5]);
93        this.regG = NumberUtils.decodeHighUnsignedNibble(buffer[bufferIndex + 5]);
94    }
95
96    protected void writeInstruction(AnnotatedOutput out, int currentCodeAddress) {
97        out.writeByte(opcode.value);
98        out.writeByte((regCount << 4) | regA);
99        out.writeShort(getReferencedItem().getIndex());
100        out.writeByte((regE << 4) | regD);
101        out.writeByte((regG << 4) | regF);
102    }
103
104    public Format getFormat() {
105        return Format.Format35s;
106    }
107
108    public byte getRegCount() {
109        return regCount;
110    }
111
112    public byte getRegisterA() {
113        return regA;
114    }
115
116    public byte getRegisterD() {
117        return regD;
118    }
119
120    public byte getRegisterE() {
121        return regE;
122    }
123
124    public byte getRegisterF() {
125        return regF;
126    }
127
128    public byte getRegisterG() {
129        return regG;
130    }
131
132    private static void checkItem(Opcode opcode, Item item, int regCount) {
133        if (opcode == FILLED_NEW_ARRAY) {
134            //check data for filled-new-array opcode
135            String type = ((TypeIdItem) item).getTypeDescriptor();
136            if (type.charAt(0) != '[') {
137                throw new RuntimeException("The type must be an array type");
138            }
139            if (type.charAt(1) == 'J' || type.charAt(1) == 'D') {
140                throw new RuntimeException("The type cannot be an array of longs or doubles");
141            }
142        } else if (opcode.value >= INVOKE_VIRTUAL.value && opcode.value <= INVOKE_INTERFACE.value) {
143            //check data for invoke-* opcodes
144            MethodIdItem methodIdItem = (MethodIdItem) item;
145            int parameterRegisterCount = methodIdItem.getPrototype().getParameterRegisterCount();
146            if (opcode != INVOKE_STATIC) {
147                parameterRegisterCount++;
148            }
149            if (parameterRegisterCount != regCount) {
150                throw new RuntimeException("regCount does not match the number of arguments of the method");
151            }
152        }
153    }
154
155    private static class Factory implements Instruction.InstructionFactory {
156        public Instruction makeInstruction(DexFile dexFile, Opcode opcode, byte[] buffer, int bufferIndex) {
157            return new Instruction35s(dexFile, opcode, buffer, bufferIndex);
158        }
159    }
160}
161