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