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.RegisterSpecList; 23 import com.android.dx.rop.cst.Constant; 24 import com.android.dx.rop.cst.CstLiteralBits; 25 import com.android.dx.util.AnnotatedOutput; 26 import java.util.BitSet; 27 28 /** 29 * Instruction format {@code 21h}. See the instruction format spec 30 * for details. 31 */ 32 public final class Form21h extends InsnFormat { 33 /** {@code non-null;} unique instance of this class */ 34 public static final InsnFormat THE_ONE = new Form21h(); 35 36 /** 37 * Constructs an instance. This class is not publicly 38 * instantiable. Use {@link #THE_ONE}. 39 */ 40 private Form21h() { 41 // This space intentionally left blank. 42 } 43 44 /** {@inheritDoc} */ 45 @Override 46 public String insnArgString(DalvInsn insn) { 47 RegisterSpecList regs = insn.getRegisters(); 48 CstLiteralBits value = (CstLiteralBits) ((CstInsn) insn).getConstant(); 49 50 return regs.get(0).regString() + ", " + literalBitsString(value); 51 } 52 53 /** {@inheritDoc} */ 54 @Override 55 public String insnCommentString(DalvInsn insn, boolean noteIndices) { 56 RegisterSpecList regs = insn.getRegisters(); 57 CstLiteralBits value = (CstLiteralBits) ((CstInsn) insn).getConstant(); 58 59 return 60 literalBitsComment(value, 61 (regs.get(0).getCategory() == 1) ? 32 : 64); 62 } 63 64 /** {@inheritDoc} */ 65 @Override 66 public int codeSize() { 67 return 2; 68 } 69 70 /** {@inheritDoc} */ 71 @Override 72 public boolean isCompatible(DalvInsn insn) { 73 RegisterSpecList regs = insn.getRegisters(); 74 if (!((insn instanceof CstInsn) && 75 (regs.size() == 1) && 76 unsignedFitsInByte(regs.get(0).getReg()))) { 77 return false; 78 } 79 80 CstInsn ci = (CstInsn) insn; 81 Constant cst = ci.getConstant(); 82 83 if (!(cst instanceof CstLiteralBits)) { 84 return false; 85 } 86 87 CstLiteralBits cb = (CstLiteralBits) cst; 88 89 // Where the high bits are depends on the category of the target. 90 if (regs.get(0).getCategory() == 1) { 91 int bits = cb.getIntBits(); 92 return ((bits & 0xffff) == 0); 93 } else { 94 long bits = cb.getLongBits(); 95 return ((bits & 0xffffffffffffL) == 0); 96 } 97 } 98 99 /** {@inheritDoc} */ 100 @Override 101 public BitSet compatibleRegs(DalvInsn insn) { 102 RegisterSpecList regs = insn.getRegisters(); 103 BitSet bits = new BitSet(1); 104 105 bits.set(0, unsignedFitsInByte(regs.get(0).getReg())); 106 return bits; 107 } 108 109 /** {@inheritDoc} */ 110 @Override 111 public void writeTo(AnnotatedOutput out, DalvInsn insn) { 112 RegisterSpecList regs = insn.getRegisters(); 113 CstLiteralBits cb = (CstLiteralBits) ((CstInsn) insn).getConstant(); 114 short bits; 115 116 // Where the high bits are depends on the category of the target. 117 if (regs.get(0).getCategory() == 1) { 118 bits = (short) (cb.getIntBits() >>> 16); 119 } else { 120 bits = (short) (cb.getLongBits() >>> 48); 121 } 122 123 write(out, opcodeUnit(insn, regs.get(0).getReg()), bits); 124 } 125 } 126