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