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