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.DalvInsn; 20import com.android.dx.dex.code.InsnFormat; 21import com.android.dx.dex.code.TargetInsn; 22import com.android.dx.rop.code.RegisterSpecList; 23import com.android.dx.util.AnnotatedOutput; 24 25import java.util.BitSet; 26 27/** 28 * Instruction format {@code 22t}. See the instruction format spec 29 * for details. 30 */ 31public final class Form22t extends InsnFormat { 32 /** {@code non-null;} unique instance of this class */ 33 public static final InsnFormat THE_ONE = new Form22t(); 34 35 /** 36 * Constructs an instance. This class is not publicly 37 * instantiable. Use {@link #THE_ONE}. 38 */ 39 private Form22t() { 40 // This space intentionally left blank. 41 } 42 43 /** {@inheritDoc} */ 44 @Override 45 public String insnArgString(DalvInsn insn) { 46 RegisterSpecList regs = insn.getRegisters(); 47 return regs.get(0).regString() + ", " + regs.get(1).regString() + 48 ", " + branchString(insn); 49 } 50 51 /** {@inheritDoc} */ 52 @Override 53 public String insnCommentString(DalvInsn insn, boolean noteIndices) { 54 return branchComment(insn); 55 } 56 57 /** {@inheritDoc} */ 58 @Override 59 public int codeSize() { 60 return 2; 61 } 62 63 /** {@inheritDoc} */ 64 @Override 65 public boolean isCompatible(DalvInsn insn) { 66 RegisterSpecList regs = insn.getRegisters(); 67 68 if (!((insn instanceof TargetInsn) && 69 (regs.size() == 2) && 70 unsignedFitsInNibble(regs.get(0).getReg()) && 71 unsignedFitsInNibble(regs.get(1).getReg()))) { 72 return false; 73 } 74 75 TargetInsn ti = (TargetInsn) insn; 76 return ti.hasTargetOffset() ? branchFits(ti) : true; 77 } 78 79 /** {@inheritDoc} */ 80 @Override 81 public BitSet compatibleRegs(DalvInsn insn) { 82 RegisterSpecList regs = insn.getRegisters(); 83 BitSet bits = new BitSet(2); 84 85 bits.set(0, unsignedFitsInNibble(regs.get(0).getReg())); 86 bits.set(1, unsignedFitsInNibble(regs.get(1).getReg())); 87 return bits; 88 } 89 90 /** {@inheritDoc} */ 91 @Override 92 public boolean branchFits(TargetInsn insn) { 93 int offset = insn.getTargetOffset(); 94 95 // Note: A zero offset would fit, but it is prohibited by the spec. 96 return (offset != 0) && signedFitsInShort(offset); 97 } 98 99 /** {@inheritDoc} */ 100 @Override 101 public void writeTo(AnnotatedOutput out, DalvInsn insn) { 102 RegisterSpecList regs = insn.getRegisters(); 103 int offset = ((TargetInsn) insn).getTargetOffset(); 104 105 write(out, 106 opcodeUnit(insn, 107 makeByte(regs.get(0).getReg(), regs.get(1).getReg())), 108 (short) offset); 109 } 110} 111