OSDN Git Service

0335dc7318edf762a88c8536bcb689b3f9193413
[android-x86/dalvik.git] / dx / src / com / android / dx / dex / code / form / Form21c.java
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
17 package com.android.dx.dex.code.form;
18
19 import com.android.dx.dex.code.CstInsn;
20 import com.android.dx.dex.code.DalvInsn;
21 import com.android.dx.dex.code.InsnFormat;
22 import com.android.dx.rop.code.RegisterSpec;
23 import com.android.dx.rop.code.RegisterSpecList;
24 import com.android.dx.rop.cst.Constant;
25 import com.android.dx.rop.cst.CstFieldRef;
26 import com.android.dx.rop.cst.CstString;
27 import com.android.dx.rop.cst.CstType;
28 import com.android.dx.util.AnnotatedOutput;
29
30 import java.util.BitSet;
31
32 /**
33  * Instruction format {@code 21c}. See the instruction format spec
34  * for details.
35  */
36 public final class Form21c extends InsnFormat {
37     /** {@code non-null;} unique instance of this class */
38     public static final InsnFormat THE_ONE = new Form21c();
39
40     /**
41      * Constructs an instance. This class is not publicly
42      * instantiable. Use {@link #THE_ONE}.
43      */
44     private Form21c() {
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 2;
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         int cpi = ci.getIndex();
108         Constant cst = ci.getConstant();
109
110         if (! unsignedFitsInShort(cpi)) {
111             return false;
112         }
113
114         return (cst instanceof CstType) ||
115             (cst instanceof CstFieldRef) ||
116             (cst instanceof CstString);
117     }
118
119     /** {@inheritDoc} */
120     @Override
121     public BitSet compatibleRegs(DalvInsn insn) {
122         RegisterSpecList regs = insn.getRegisters();
123         int sz = regs.size();
124         BitSet bits = new BitSet(sz);
125         boolean compat = unsignedFitsInByte(regs.get(0).getReg());
126
127         if (sz == 1) {
128             bits.set(0, compat);
129         } else {
130             if (regs.get(0).getReg() == regs.get(1).getReg()) {
131                 bits.set(0, compat);
132                 bits.set(1, compat);
133             }
134         }
135
136         return bits;
137     }
138
139     /** {@inheritDoc} */
140     @Override
141     public void writeTo(AnnotatedOutput out, DalvInsn insn) {
142         RegisterSpecList regs = insn.getRegisters();
143         int cpi = ((CstInsn) insn).getIndex();
144
145         write(out,
146               opcodeUnit(insn, regs.get(0).getReg()),
147               (short) cpi);
148     }
149 }