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