1/*
2 * Copyright 2011, Google Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 *     * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *     * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 *     * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32package org.jf.dexlib.Code.Format;
33
34import org.jf.dexlib.Code.*;
35import org.jf.dexlib.DexFile;
36import org.jf.dexlib.Util.AnnotatedOutput;
37import org.jf.dexlib.Util.NumberUtils;
38
39
40public class Instruction35mi extends Instruction implements FiveRegisterInstruction, OdexedInvokeInline {
41    public static final InstructionFactory Factory = new Factory();
42    private byte regCount;
43    private byte regA;
44    private byte regD;
45    private byte regE;
46    private byte regF;
47    private byte regG;
48    private short inlineIndex;
49
50    public Instruction35mi(Opcode opcode, int regCount, byte regD, byte regE, byte regF, byte regG,
51                           byte regA, int inlineIndex) {
52        super(opcode);
53        if (regCount > 5) {
54            throw new RuntimeException("regCount cannot be greater than 5");
55        }
56
57        if (regD >= 1 << 4 ||
58                regE >= 1 << 4 ||
59                regF >= 1 << 4 ||
60                regG >= 1 << 4 ||
61                regA >= 1 << 4) {
62            throw new RuntimeException("All register args must fit in 4 bits");
63        }
64
65        if (inlineIndex >= 1 << 16) {
66            throw new RuntimeException("The method index must be less than 65536");
67        }
68
69        this.regCount = (byte)regCount;
70        this.regA = regA;
71        this.regD = regD;
72        this.regE = regE;
73        this.regF = regF;
74        this.regG = regG;
75        this.inlineIndex = (short)inlineIndex;
76    }
77
78    private Instruction35mi(Opcode opcode, byte[] buffer, int bufferIndex) {
79        super(opcode);
80
81        this.regCount = NumberUtils.decodeHighUnsignedNibble(buffer[bufferIndex + 1]);
82        this.regA = NumberUtils.decodeLowUnsignedNibble(buffer[bufferIndex + 1]);
83        this.regD = NumberUtils.decodeLowUnsignedNibble(buffer[bufferIndex + 4]);
84        this.regE = NumberUtils.decodeHighUnsignedNibble(buffer[bufferIndex + 4]);
85        this.regF = NumberUtils.decodeLowUnsignedNibble(buffer[bufferIndex + 5]);
86        this.regG = NumberUtils.decodeHighUnsignedNibble(buffer[bufferIndex + 5]);
87        this.inlineIndex = (short)NumberUtils.decodeUnsignedShort(buffer, bufferIndex + 2);
88    }
89
90    protected void writeInstruction(AnnotatedOutput out, int currentCodeAddress) {
91        out.writeByte(opcode.value);
92        out.writeByte((regCount << 4) | regA);
93        out.writeShort(inlineIndex);
94        out.writeByte((regE << 4) | regD);
95        out.writeByte((regG << 4) | regF);
96    }
97
98    public Format getFormat() {
99        return Format.Format35mi;
100    }
101
102    public int getRegCount() {
103        return regCount;
104    }
105
106    public byte getRegisterA() {
107        return regA;
108    }
109
110    public byte getRegisterD() {
111        return regD;
112    }
113
114    public byte getRegisterE() {
115        return regE;
116    }
117
118    public byte getRegisterF() {
119        return regF;
120    }
121
122    public byte getRegisterG() {
123        return regG;
124    }
125
126    public int getInlineIndex() {
127        return inlineIndex & 0xFFFF;
128    }
129
130    private static class Factory implements InstructionFactory {
131        public Instruction makeInstruction(DexFile dexFile, Opcode opcode, byte[] buffer, int bufferIndex) {
132            return new Instruction35mi(opcode, buffer, bufferIndex);
133        }
134    }
135}
136
137