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.dx.dex.code.form;
     18 
     19 import com.android.dx.dex.code.CstInsn;
     20 import com.android.dx.dex.code.DalvInsn;
     21 import com.android.dx.dex.code.InsnFormat;
     22 import com.android.dx.rop.code.RegisterSpec;
     23 import com.android.dx.rop.code.RegisterSpecList;
     24 import com.android.dx.rop.cst.Constant;
     25 import com.android.dx.rop.cst.CstFieldRef;
     26 import com.android.dx.rop.cst.CstString;
     27 import com.android.dx.rop.cst.CstType;
     28 import com.android.dx.util.AnnotatedOutput;
     29 import java.util.BitSet;
     30 
     31 /**
     32  * Instruction format {@code 31c}. See the instruction format spec
     33  * for details.
     34  */
     35 public final class Form31c extends InsnFormat {
     36     /** {@code non-null;} unique instance of this class */
     37     public static final InsnFormat THE_ONE = new Form31c();
     38 
     39     /**
     40      * Constructs an instance. This class is not publicly
     41      * instantiable. Use {@link #THE_ONE}.
     42      */
     43     private Form31c() {
     44         // This space intentionally left blank.
     45     }
     46 
     47     /** {@inheritDoc} */
     48     @Override
     49     public String insnArgString(DalvInsn insn) {
     50         RegisterSpecList regs = insn.getRegisters();
     51         return regs.get(0).regString() + ", " + insn.cstString();
     52     }
     53 
     54     /** {@inheritDoc} */
     55     @Override
     56     public String insnCommentString(DalvInsn insn, boolean noteIndices) {
     57         if (noteIndices) {
     58             return insn.cstComment();
     59         } else {
     60             return "";
     61         }
     62     }
     63 
     64     /** {@inheritDoc} */
     65     @Override
     66     public int codeSize() {
     67         return 3;
     68     }
     69 
     70     /** {@inheritDoc} */
     71     @Override
     72     public boolean isCompatible(DalvInsn insn) {
     73         if (!(insn instanceof CstInsn)) {
     74             return false;
     75         }
     76 
     77         RegisterSpecList regs = insn.getRegisters();
     78         RegisterSpec reg;
     79 
     80         switch (regs.size()) {
     81             case 1: {
     82                 reg = regs.get(0);
     83                 break;
     84             }
     85             case 2: {
     86                 /*
     87                  * This format is allowed for ops that are effectively
     88                  * 2-arg but where the two args are identical.
     89                  */
     90                 reg = regs.get(0);
     91                 if (reg.getReg() != regs.get(1).getReg()) {
     92                     return false;
     93                 }
     94                 break;
     95             }
     96             default: {
     97                 return false;
     98             }
     99         }
    100 
    101         if (!unsignedFitsInByte(reg.getReg())) {
    102             return false;
    103         }
    104 
    105         CstInsn ci = (CstInsn) insn;
    106         Constant cst = ci.getConstant();
    107 
    108         return (cst instanceof CstType) ||
    109             (cst instanceof CstFieldRef) ||
    110             (cst instanceof CstString);
    111     }
    112 
    113     /** {@inheritDoc} */
    114     @Override
    115     public BitSet compatibleRegs(DalvInsn insn) {
    116         RegisterSpecList regs = insn.getRegisters();
    117         int sz = regs.size();
    118         BitSet bits = new BitSet(sz);
    119         boolean compat = unsignedFitsInByte(regs.get(0).getReg());
    120 
    121         if (sz == 1) {
    122             bits.set(0, compat);
    123         } else {
    124             if (regs.get(0).getReg() == regs.get(1).getReg()) {
    125                 bits.set(0, compat);
    126                 bits.set(1, compat);
    127             }
    128         }
    129 
    130         return bits;
    131     }
    132 
    133     /** {@inheritDoc} */
    134     @Override
    135     public void writeTo(AnnotatedOutput out, DalvInsn insn) {
    136         RegisterSpecList regs = insn.getRegisters();
    137         int cpi = ((CstInsn) insn).getIndex();
    138 
    139         write(out, opcodeUnit(insn, regs.get(0).getReg()), cpi);
    140     }
    141 }
    142