Form22b.java revision 579d7739c53a2707ad711a2d2cae46d7d782f061
1/*
2 * Copyright (C) 2007 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.CstInsn;
20import com.android.dx.dex.code.DalvInsn;
21import com.android.dx.dex.code.InsnFormat;
22import com.android.dx.rop.code.RegisterSpecList;
23import com.android.dx.rop.cst.Constant;
24import com.android.dx.rop.cst.CstLiteralBits;
25import com.android.dx.util.AnnotatedOutput;
26
27import java.util.BitSet;
28
29/**
30 * Instruction format {@code 22b}. See the instruction format spec
31 * for details.
32 */
33public final class Form22b extends InsnFormat {
34    /** {@code non-null;} unique instance of this class */
35    public static final InsnFormat THE_ONE = new Form22b();
36
37    /**
38     * Constructs an instance. This class is not publicly
39     * instantiable. Use {@link #THE_ONE}.
40     */
41    private Form22b() {
42        // This space intentionally left blank.
43    }
44
45    /** {@inheritDoc} */
46    @Override
47    public String insnArgString(DalvInsn insn) {
48        RegisterSpecList regs = insn.getRegisters();
49        CstLiteralBits value = (CstLiteralBits) ((CstInsn) insn).getConstant();
50
51        return regs.get(0).regString() + ", " + regs.get(1).regString() +
52            ", " + literalBitsString(value);
53    }
54
55    /** {@inheritDoc} */
56    @Override
57    public String insnCommentString(DalvInsn insn, boolean noteIndices) {
58        CstLiteralBits value = (CstLiteralBits) ((CstInsn) insn).getConstant();
59        return literalBitsComment(value, 8);
60    }
61
62    /** {@inheritDoc} */
63    @Override
64    public int codeSize() {
65        return 2;
66    }
67
68    /** {@inheritDoc} */
69    @Override
70    public boolean isCompatible(DalvInsn insn) {
71        RegisterSpecList regs = insn.getRegisters();
72        if (!((insn instanceof CstInsn) &&
73              (regs.size() == 2) &&
74              unsignedFitsInByte(regs.get(0).getReg()) &&
75              unsignedFitsInByte(regs.get(1).getReg()))) {
76            return false;
77        }
78
79        CstInsn ci = (CstInsn) insn;
80        Constant cst = ci.getConstant();
81
82        if (!(cst instanceof CstLiteralBits)) {
83            return false;
84        }
85
86        CstLiteralBits cb = (CstLiteralBits) cst;
87
88        return cb.fitsInInt() && signedFitsInByte(cb.getIntBits());
89    }
90
91    /** {@inheritDoc} */
92    @Override
93    public BitSet compatibleRegs(DalvInsn insn) {
94        RegisterSpecList regs = insn.getRegisters();
95        BitSet bits = new BitSet(2);
96
97        bits.set(0, unsignedFitsInByte(regs.get(0).getReg()));
98        bits.set(1, unsignedFitsInByte(regs.get(1).getReg()));
99        return bits;
100    }
101
102    /** {@inheritDoc} */
103    @Override
104    public void writeTo(AnnotatedOutput out, DalvInsn insn) {
105        RegisterSpecList regs = insn.getRegisters();
106        int value =
107            ((CstLiteralBits) ((CstInsn) insn).getConstant()).getIntBits();
108
109        write(out,
110              opcodeUnit(insn, regs.get(0).getReg()),
111              codeUnit(regs.get(1).getReg(), value & 0xff));
112    }
113}
114