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.RegisterSpec;
23import com.android.dx.rop.code.RegisterSpecList;
24import com.android.dx.rop.cst.Constant;
25import com.android.dx.rop.cst.CstFieldRef;
26import com.android.dx.rop.cst.CstString;
27import com.android.dx.rop.cst.CstType;
28import com.android.dx.util.AnnotatedOutput;
29import java.util.BitSet;
30
31/**
32 * Instruction format {@code 21c}. See the instruction format spec
33 * for details.
34 */
35public final class Form21c extends InsnFormat {
36    /** {@code non-null;} unique instance of this class */
37    public static final InsnFormat THE_ONE = new Form21c();
38
39    /**
40     * Constructs an instance. This class is not publicly
41     * instantiable. Use {@link #THE_ONE}.
42     */
43    private Form21c() {
44        // This space intentionally left blank.
45    }
46
47    /** {@inheritDoc} */
48    @Override
49    public String insnArgString(DalvInsn insn) {
50        RegisterSpecList regs = insn.getRegisters();
51        return regs.get(0).regString() + ", " + 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        if (!(insn instanceof CstInsn)) {
74            return false;
75        }
76
77        RegisterSpecList regs = insn.getRegisters();
78        RegisterSpec reg;
79
80        switch (regs.size()) {
81            case 1: {
82                reg = regs.get(0);
83                break;
84            }
85            case 2: {
86                /*
87                 * This format is allowed for ops that are effectively
88                 * 2-arg but where the two args are identical.
89                 */
90                reg = regs.get(0);
91                if (reg.getReg() != regs.get(1).getReg()) {
92                    return false;
93                }
94                break;
95            }
96            default: {
97                return false;
98            }
99        }
100
101        if (!unsignedFitsInByte(reg.getReg())) {
102            return false;
103        }
104
105        CstInsn ci = (CstInsn) insn;
106        int cpi = ci.getIndex();
107        Constant cst = ci.getConstant();
108
109        if (! unsignedFitsInShort(cpi)) {
110            return false;
111        }
112
113        return (cst instanceof CstType) ||
114            (cst instanceof CstFieldRef) ||
115            (cst instanceof CstString);
116    }
117
118    /** {@inheritDoc} */
119    @Override
120    public BitSet compatibleRegs(DalvInsn insn) {
121        RegisterSpecList regs = insn.getRegisters();
122        int sz = regs.size();
123        BitSet bits = new BitSet(sz);
124        boolean compat = unsignedFitsInByte(regs.get(0).getReg());
125
126        if (sz == 1) {
127            bits.set(0, compat);
128        } else {
129            if (regs.get(0).getReg() == regs.get(1).getReg()) {
130                bits.set(0, compat);
131                bits.set(1, compat);
132            }
133        }
134
135        return bits;
136    }
137
138    /** {@inheritDoc} */
139    @Override
140    public void writeTo(AnnotatedOutput out, DalvInsn insn) {
141        RegisterSpecList regs = insn.getRegisters();
142        int cpi = ((CstInsn) insn).getIndex();
143
144        write(out,
145              opcodeUnit(insn, regs.get(0).getReg()),
146              (short) cpi);
147    }
148}
149