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