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