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