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