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