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 31c}. See the instruction format spec
32 * for details.
33 */
34public final class Form31c extends InsnFormat {
35    /** {@code non-null;} unique instance of this class */
36    public static final InsnFormat THE_ONE = new Form31c();
37
38    /**
39     * Constructs an instance. This class is not publicly
40     * instantiable. Use {@link #THE_ONE}.
41     */
42    private Form31c() {
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 3;
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        Constant cst = ci.getConstant();
106
107        return ((cst instanceof CstType) ||
108                (cst instanceof CstFieldRef) ||
109                (cst instanceof CstString));
110    }
111
112    /** {@inheritDoc} */
113    @Override
114    public InsnFormat nextUp() {
115        return null;
116    }
117
118    /** {@inheritDoc} */
119    @Override
120    public void writeTo(AnnotatedOutput out, DalvInsn insn) {
121        RegisterSpecList regs = insn.getRegisters();
122        int cpi = ((CstInsn) insn).getIndex();
123
124        write(out,
125                opcodeUnit(insn, regs.get(0).getReg()),
126                (short) cpi,
127                (short) (cpi >> 16));
128    }
129}
130