Home | History | Annotate | Download | only in code
      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;
     18 
     19 import com.android.dx.rop.code.RegisterSpecList;
     20 import com.android.dx.rop.code.SourcePosition;
     21 import com.android.dx.util.AnnotatedOutput;
     22 
     23 /**
     24  * Base class for instructions which are of a fixed code size and
     25  * which use {@link InsnFormat} methods to write themselves. This
     26  * includes most — but not all — instructions.
     27  */
     28 public abstract class FixedSizeInsn extends DalvInsn {
     29     /**
     30      * Constructs an instance. The output address of this instance is initially
     31      * unknown ({@code -1}).
     32      *
     33      * <p><b>Note:</b> In the unlikely event that an instruction takes
     34      * absolutely no registers (e.g., a {@code nop} or a
     35      * no-argument no-result * static method call), then the given
     36      * register list may be passed as {@link
     37      * RegisterSpecList#EMPTY}.</p>
     38      *
     39      * @param opcode the opcode; one of the constants from {@link Dops}
     40      * @param position {@code non-null;} source position
     41      * @param registers {@code non-null;} register list, including a
     42      * result register if appropriate (that is, registers may be either
     43      * ins or outs)
     44      */
     45     public FixedSizeInsn(Dop opcode, SourcePosition position,
     46                          RegisterSpecList registers) {
     47         super(opcode, position, registers);
     48     }
     49 
     50     /** {@inheritDoc} */
     51     @Override
     52     public final int codeSize() {
     53         return getOpcode().getFormat().codeSize();
     54     }
     55 
     56     /** {@inheritDoc} */
     57     @Override
     58     public final void writeTo(AnnotatedOutput out) {
     59         getOpcode().getFormat().writeTo(out, this);
     60     }
     61 
     62     /** {@inheritDoc} */
     63     @Override
     64     public final DalvInsn withRegisterOffset(int delta) {
     65         return withRegisters(getRegisters().withOffset(delta));
     66     }
     67 
     68     /** {@inheritDoc} */
     69     @Override
     70     protected final String listingString0(boolean noteIndices) {
     71         return getOpcode().getFormat().listingString(this, noteIndices);
     72     }
     73 }
     74