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 Instruction35c 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 Instruction35c(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 Instruction35c(DexFile dexFile, Opcode opcode, byte[] buffer, int bufferIndex) {
80        super(dexFile, opcode, buffer, bufferIndex);
81
82        this.regCount = NumberUtils.decodeHighUnsignedNibble(buffer[bufferIndex + 1]);
83        this.regA = NumberUtils.decodeLowUnsignedNibble(buffer[bufferIndex + 1]);
84        this.regD = NumberUtils.decodeLowUnsignedNibble(buffer[bufferIndex + 4]);
85        this.regE = NumberUtils.decodeHighUnsignedNibble(buffer[bufferIndex + 4]);
86        this.regF = NumberUtils.decodeLowUnsignedNibble(buffer[bufferIndex + 5]);
87        this.regG = NumberUtils.decodeHighUnsignedNibble(buffer[bufferIndex + 5]);
88
89        if (getRegCount() > 5) {
90            throw new RuntimeException("regCount cannot be greater than 5");
91        }
92
93        checkItem(opcode, getReferencedItem(), getRegCount());
94    }
95
96    protected void writeInstruction(AnnotatedOutput out, int currentCodeAddress) {
97        if(getReferencedItem().getIndex() > 0xFFFF) {
98            if (opcode.hasJumboOpcode()) {
99                throw new RuntimeException(String.format("%s index is too large. Use the %s instruction instead.",
100                        opcode.referenceType.name(), opcode.getJumboOpcode().name));
101            } else {
102                throw new RuntimeException(String.format("%s index is too large.", opcode.referenceType.name()));
103            }
104        }
105
106        out.writeByte(opcode.value);
107        out.writeByte((regCount << 4) | regA);
108        out.writeShort(getReferencedItem().getIndex());
109        out.writeByte((regE << 4) | regD);
110        out.writeByte((regG << 4) | regF);
111    }
112
113    public Format getFormat() {
114        return Format.Format35c;
115    }
116
117    public int getRegCount() {
118        return regCount;
119    }
120
121    public byte getRegisterA() {
122        return regA;
123    }
124
125    public byte getRegisterD() {
126        return regD;
127    }
128
129    public byte getRegisterE() {
130        return regE;
131    }
132
133    public byte getRegisterF() {
134        return regF;
135    }
136
137    public byte getRegisterG() {
138        return regG;
139    }
140
141    private static void checkItem(Opcode opcode, Item item, int regCount) {
142        if (opcode == FILLED_NEW_ARRAY) {
143            //check data for filled-new-array opcode
144            String type = ((TypeIdItem) item).getTypeDescriptor();
145            if (type.charAt(0) != '[') {
146                throw new RuntimeException("The type must be an array type");
147            }
148            if (type.charAt(1) == 'J' || type.charAt(1) == 'D') {
149                throw new RuntimeException("The type cannot be an array of longs or doubles");
150            }
151        } else if (opcode.value >= INVOKE_VIRTUAL.value && opcode.value <= INVOKE_INTERFACE.value ||
152                opcode == INVOKE_DIRECT_EMPTY) {
153            //check data for invoke-* opcodes
154            MethodIdItem methodIdItem = (MethodIdItem) item;
155            int parameterRegisterCount = methodIdItem.getPrototype().getParameterRegisterCount();
156            if (opcode != INVOKE_STATIC) {
157                parameterRegisterCount++;
158            }
159            if (parameterRegisterCount != regCount) {
160                throw new RuntimeException("regCount does not match the number of arguments of the method");
161            }
162        }
163    }
164
165    private static class Factory implements Instruction.InstructionFactory {
166        public Instruction makeInstruction(DexFile dexFile, Opcode opcode, byte[] buffer, int bufferIndex) {
167            return new Instruction35c(dexFile, opcode, buffer, bufferIndex);
168        }
169    }
170}