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.CstMethodRef; 26import com.android.dx.rop.cst.CstType; 27import com.android.dx.rop.type.Type; 28import com.android.dx.util.AnnotatedOutput; 29import java.util.BitSet; 30 31/** 32 * Instruction format {@code 35c}. See the instruction format spec 33 * for details. 34 */ 35public final class Form35c extends InsnFormat { 36 /** {@code non-null;} unique instance of this class */ 37 public static final InsnFormat THE_ONE = new Form35c(); 38 39 /** Maximal number of operands */ 40 private static final int MAX_NUM_OPS = 5; 41 42 /** 43 * Constructs an instance. This class is not publicly 44 * instantiable. Use {@link #THE_ONE}. 45 */ 46 private Form35c() { 47 // This space intentionally left blank. 48 } 49 50 /** {@inheritDoc} */ 51 @Override 52 public String insnArgString(DalvInsn insn) { 53 RegisterSpecList regs = explicitize(insn.getRegisters()); 54 return regListString(regs) + ", " + cstString(insn); 55 } 56 57 /** {@inheritDoc} */ 58 @Override 59 public String insnCommentString(DalvInsn insn, boolean noteIndices) { 60 if (noteIndices) { 61 return cstComment(insn); 62 } else { 63 return ""; 64 } 65 } 66 67 /** {@inheritDoc} */ 68 @Override 69 public int codeSize() { 70 return 3; 71 } 72 73 /** {@inheritDoc} */ 74 @Override 75 public boolean isCompatible(DalvInsn insn) { 76 if (!(insn instanceof CstInsn)) { 77 return false; 78 } 79 80 CstInsn ci = (CstInsn) insn; 81 int cpi = ci.getIndex(); 82 83 if (! unsignedFitsInShort(cpi)) { 84 return false; 85 } 86 87 Constant cst = ci.getConstant(); 88 if (!((cst instanceof CstMethodRef) || 89 (cst instanceof CstType))) { 90 return false; 91 } 92 93 RegisterSpecList regs = ci.getRegisters(); 94 return (wordCount(regs) >= 0); 95 } 96 97 /** {@inheritDoc} */ 98 @Override 99 public BitSet compatibleRegs(DalvInsn insn) { 100 RegisterSpecList regs = insn.getRegisters(); 101 int sz = regs.size(); 102 BitSet bits = new BitSet(sz); 103 104 for (int i = 0; i < sz; i++) { 105 RegisterSpec reg = regs.get(i); 106 /* 107 * The check below adds (category - 1) to the register, to 108 * account for the fact that the second half of a 109 * category-2 register has to be represented explicitly in 110 * the result. 111 */ 112 bits.set(i, unsignedFitsInNibble(reg.getReg() + 113 reg.getCategory() - 1)); 114 } 115 116 return bits; 117 } 118 119 /** {@inheritDoc} */ 120 @Override 121 public void writeTo(AnnotatedOutput out, DalvInsn insn) { 122 int cpi = ((CstInsn) insn).getIndex(); 123 RegisterSpecList regs = explicitize(insn.getRegisters()); 124 int sz = regs.size(); 125 int r0 = (sz > 0) ? regs.get(0).getReg() : 0; 126 int r1 = (sz > 1) ? regs.get(1).getReg() : 0; 127 int r2 = (sz > 2) ? regs.get(2).getReg() : 0; 128 int r3 = (sz > 3) ? regs.get(3).getReg() : 0; 129 int r4 = (sz > 4) ? regs.get(4).getReg() : 0; 130 131 write(out, 132 opcodeUnit(insn, 133 makeByte(r4, sz)), // encode the fifth operand here 134 (short) cpi, 135 codeUnit(r0, r1, r2, r3)); 136 } 137 138 /** 139 * Gets the number of words required for the given register list, where 140 * category-2 values count as two words. Return {@code -1} if the 141 * list requires more than five words or contains registers that need 142 * more than a nibble to identify them. 143 * 144 * @param regs {@code non-null;} the register list in question 145 * @return {@code >= -1;} the number of words required, or {@code -1} 146 * if the list couldn't possibly fit in this format 147 */ 148 private static int wordCount(RegisterSpecList regs) { 149 int sz = regs.size(); 150 151 if (sz > MAX_NUM_OPS) { 152 // It can't possibly fit. 153 return -1; 154 } 155 156 int result = 0; 157 158 for (int i = 0; i < sz; i++) { 159 RegisterSpec one = regs.get(i); 160 result += one.getCategory(); 161 /* 162 * The check below adds (category - 1) to the register, to 163 * account for the fact that the second half of a 164 * category-2 register has to be represented explicitly in 165 * the result. 166 */ 167 if (!unsignedFitsInNibble(one.getReg() + one.getCategory() - 1)) { 168 return -1; 169 } 170 } 171 172 return (result <= MAX_NUM_OPS) ? result : -1; 173 } 174 175 /** 176 * Returns a register list which is equivalent to the given one, 177 * except that it splits category-2 registers into two explicit 178 * entries. This returns the original list if no modification is 179 * required 180 * 181 * @param orig {@code non-null;} the original list 182 * @return {@code non-null;} the list with the described transformation 183 */ 184 private static RegisterSpecList explicitize(RegisterSpecList orig) { 185 int wordCount = wordCount(orig); 186 int sz = orig.size(); 187 188 if (wordCount == sz) { 189 return orig; 190 } 191 192 RegisterSpecList result = new RegisterSpecList(wordCount); 193 int wordAt = 0; 194 195 for (int i = 0; i < sz; i++) { 196 RegisterSpec one = orig.get(i); 197 result.set(wordAt, one); 198 if (one.getCategory() == 2) { 199 result.set(wordAt + 1, 200 RegisterSpec.make(one.getReg() + 1, Type.VOID)); 201 wordAt += 2; 202 } else { 203 wordAt++; 204 } 205 } 206 207 result.setImmutable(); 208 return result; 209 } 210} 211