Instruction35ms.java revision 3bfd77dff08cfa059ea230017791fca11fa08c53
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.OdexedInvokeVirtual;
34import org.jf.dexlib.Code.Opcode;
35import org.jf.dexlib.DexFile;
36import org.jf.dexlib.Util.AnnotatedOutput;
37import org.jf.dexlib.Util.NumberUtils;
38
39
40public class Instruction35ms extends Instruction implements FiveRegisterInstruction, OdexedInvokeVirtual {
41    public static final Instruction.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 vtableIndex;
49
50    public Instruction35ms(Opcode opcode, int regCount, byte regD, byte regE, byte regF, byte regG,
51                          byte regA, int vtableIndex) {
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 (vtableIndex >= 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.vtableIndex = (short)vtableIndex;
76    }
77
78    private Instruction35ms(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.vtableIndex = (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(vtableIndex);
94        out.writeByte((regE << 4) | regD);
95        out.writeByte((regG << 4) | regF);
96    }
97
98    public Format getFormat() {
99        return Format.Format35ms;
100    }
101
102    public byte 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 getVtableIndex() {
127        return vtableIndex & 0xFFFF;
128    }
129
130    private static class Factory implements Instruction.InstructionFactory {
131        public Instruction makeInstruction(DexFile dexFile, Opcode opcode, byte[] buffer, int bufferIndex) {
132            return new Instruction35ms(opcode, buffer, bufferIndex);
133        }
134    }
135}
136
137