Form33x.java revision 579d7739c53a2707ad711a2d2cae46d7d782f061
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.dx.dex.code.form;
18
19import com.android.dx.dex.code.DalvInsn;
20import com.android.dx.dex.code.InsnFormat;
21import com.android.dx.dex.code.SimpleInsn;
22import com.android.dx.rop.code.RegisterSpecList;
23import com.android.dx.util.AnnotatedOutput;
24
25import java.util.BitSet;
26
27/**
28 * Instruction format {@code 33x}. See the instruction format spec
29 * for details.
30 */
31public final class Form33x extends InsnFormat {
32    /** {@code non-null;} unique instance of this class */
33    public static final InsnFormat THE_ONE = new Form33x();
34
35    /**
36     * Constructs an instance. This class is not publicly
37     * instantiable. Use {@link #THE_ONE}.
38     */
39    private Form33x() {
40        // This space intentionally left blank.
41    }
42
43    /** {@inheritDoc} */
44    @Override
45    public String insnArgString(DalvInsn insn) {
46        RegisterSpecList regs = insn.getRegisters();
47        return regs.get(0).regString() + ", " + regs.get(1).regString() +
48            ", " + regs.get(2).regString();
49    }
50
51    /** {@inheritDoc} */
52    @Override
53    public String insnCommentString(DalvInsn insn, boolean noteIndices) {
54        // This format has no comment.
55        return "";
56    }
57
58    /** {@inheritDoc} */
59    @Override
60    public int codeSize() {
61        return 3;
62    }
63
64    /** {@inheritDoc} */
65    @Override
66    public boolean isCompatible(DalvInsn insn) {
67        if (! ALLOW_EXTENDED_OPCODES) {
68            return false;
69        }
70
71        RegisterSpecList regs = insn.getRegisters();
72
73        return (insn instanceof SimpleInsn) &&
74            (regs.size() == 3) &&
75            unsignedFitsInByte(regs.get(0).getReg()) &&
76            unsignedFitsInByte(regs.get(1).getReg()) &&
77            unsignedFitsInShort(regs.get(2).getReg());
78    }
79
80    /** {@inheritDoc} */
81    @Override
82    public BitSet compatibleRegs(DalvInsn insn) {
83        RegisterSpecList regs = insn.getRegisters();
84        BitSet bits = new BitSet(3);
85
86        bits.set(0, unsignedFitsInByte(regs.get(0).getReg()));
87        bits.set(1, unsignedFitsInByte(regs.get(1).getReg()));
88        bits.set(2, unsignedFitsInShort(regs.get(2).getReg()));
89        return bits;
90    }
91
92    /** {@inheritDoc} */
93    @Override
94    public void writeTo(AnnotatedOutput out, DalvInsn insn) {
95        RegisterSpecList regs = insn.getRegisters();
96        write(out,
97                opcodeUnit(insn),
98                codeUnit(regs.get(0).getReg(), regs.get(1).getReg()),
99                (short) regs.get(2).getReg());
100    }
101}
102