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