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.dexgen.dex.code.form;
18
19import com.android.dexgen.dex.code.CstInsn;
20import com.android.dexgen.dex.code.DalvInsn;
21import com.android.dexgen.dex.code.InsnFormat;
22import com.android.dexgen.rop.code.RegisterSpec;
23import com.android.dexgen.rop.code.RegisterSpecList;
24import com.android.dexgen.rop.cst.Constant;
25import com.android.dexgen.rop.cst.CstFieldRef;
26import com.android.dexgen.rop.cst.CstString;
27import com.android.dexgen.rop.cst.CstType;
28import com.android.dexgen.util.AnnotatedOutput;
29
30/**
31 * Instruction format {@code 21c}. See the instruction format spec
32 * for details.
33 */
34public final class Form21c extends InsnFormat {
35    /** {@code non-null;} unique instance of this class */
36    public static final InsnFormat THE_ONE = new Form21c();
37
38    /**
39     * Constructs an instance. This class is not publicly
40     * instantiable. Use {@link #THE_ONE}.
41     */
42    private Form21c() {
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() + ", " + 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        if (!(insn instanceof CstInsn)) {
73            return false;
74        }
75
76        RegisterSpecList regs = insn.getRegisters();
77        RegisterSpec reg;
78
79        switch (regs.size()) {
80            case 1: {
81                reg = regs.get(0);
82                break;
83            }
84            case 2: {
85                /*
86                 * This format is allowed for ops that are effectively
87                 * 2-arg but where the two args are identical.
88                 */
89                reg = regs.get(0);
90                if (reg.getReg() != regs.get(1).getReg()) {
91                    return false;
92                }
93                break;
94            }
95            default: {
96                return false;
97            }
98        }
99
100        if (!unsignedFitsInByte(reg.getReg())) {
101            return false;
102        }
103
104        CstInsn ci = (CstInsn) insn;
105        int cpi = ci.getIndex();
106
107        if (! unsignedFitsInShort(cpi)) {
108            return false;
109        }
110
111        Constant cst = ci.getConstant();
112        return (cst instanceof CstType) ||
113            (cst instanceof CstFieldRef) ||
114            (cst instanceof CstString);
115    }
116
117    /** {@inheritDoc} */
118    @Override
119    public InsnFormat nextUp() {
120        return Form31c.THE_ONE;
121    }
122
123    /** {@inheritDoc} */
124    @Override
125    public void writeTo(AnnotatedOutput out, DalvInsn insn) {
126        RegisterSpecList regs = insn.getRegisters();
127        int cpi = ((CstInsn) insn).getIndex();
128
129        write(out,
130              opcodeUnit(insn, regs.get(0).getReg()),
131              (short) cpi);
132    }
133}
134