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.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 52c}. See the instruction format spec
32 * for details.
33 */
34public final class Form52c extends InsnFormat {
35    /** {@code non-null;} unique instance of this class */
36    public static final InsnFormat THE_ONE = new Form52c();
37
38    /**
39     * Constructs an instance. This class is not publicly
40     * instantiable. Use {@link #THE_ONE}.
41     */
42    private Form52c() {
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 5;
68    }
69
70    /** {@inheritDoc} */
71    @Override
72    public boolean isCompatible(DalvInsn insn) {
73        if (! ALLOW_EXTENDED_OPCODES) {
74            return false;
75        }
76
77        RegisterSpecList regs = insn.getRegisters();
78        if (!((insn instanceof CstInsn) &&
79              (regs.size() == 2) &&
80              unsignedFitsInShort(regs.get(0).getReg()) &&
81              unsignedFitsInShort(regs.get(1).getReg()))) {
82            return false;
83        }
84
85        CstInsn ci = (CstInsn) insn;
86        Constant cst = ci.getConstant();
87
88        return (cst instanceof CstType) ||
89            (cst instanceof CstFieldRef);
90    }
91
92    /** {@inheritDoc} */
93    @Override
94    public BitSet compatibleRegs(DalvInsn insn) {
95        RegisterSpecList regs = insn.getRegisters();
96        BitSet bits = new BitSet(2);
97
98        bits.set(0, unsignedFitsInShort(regs.get(0).getReg()));
99        bits.set(1, unsignedFitsInShort(regs.get(1).getReg()));
100        return bits;
101    }
102
103    /** {@inheritDoc} */
104    @Override
105    public void writeTo(AnnotatedOutput out, DalvInsn insn) {
106        RegisterSpecList regs = insn.getRegisters();
107        int cpi = ((CstInsn) insn).getIndex();
108
109        write(out,
110                opcodeUnit(insn),
111                cpi,
112                (short) regs.get(0).getReg(),
113                (short) regs.get(1).getReg());
114    }
115}
116