RET xref

View Javadoc

    1   /*
    2    * Copyright  2000-2004 The Apache Software Foundation
    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 org.apache.bcel.generic;
    18
    19  import java.io.DataOutputStream;
    20  import java.io.IOException;
    21  import org.apache.bcel.util.ByteSequence;
    22
    23  /***
    24   * RET - Return from subroutine
    25   *
    26   * <PRE>Stack: ... -&gt; ...</PRE>
    27   *
    28   * @version $Id: RET.java 386056 2006-03-15 11:31:56Z tcurdt $
    29   * @author  <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
    30   */
    31  public class RET extends Instruction implements IndexedInstruction, TypedInstruction {
    32
    33      private boolean wide;
    34      private int index; // index to local variable containg the return address
    35
    36
    37      /***
    38       * Empty constructor needed for the Class.newInstance() statement in
    39       * Instruction.readInstruction(). Not to be used otherwise.
    40       */
    41      RET() {
    42      }
    43
    44
    45      public RET(int index) {
    46          super(org.apache.bcel.Constants.RET, (short) 2);
    47          setIndex(index); // May set wide as side effect
    48      }
    49
    50
    51      /***
    52       * Dump instruction as byte code to stream out.
    53       * @param out Output stream
    54       */
    55      public void dump( DataOutputStream out ) throws IOException {
    56          if (wide) {
    57              out.writeByte(org.apache.bcel.Constants.WIDE);
    58          }
    59          out.writeByte(opcode);
    60          if (wide) {
    61              out.writeShort(index);
    62          } else {
    63              out.writeByte(index);
    64          }
    65      }
    66
    67
    68      private final void setWide() {
    69          wide = index > org.apache.bcel.Constants.MAX_BYTE;
    70          if (wide) {
    71              length = 4; // Including the wide byte
    72          } else {
    73              length = 2;
    74          }
    75      }
    76
    77
    78      /***
    79       * Read needed data (e.g. index) from file.
    80       */
    81      protected void initFromFile( ByteSequence bytes, boolean wide ) throws IOException {
    82          this.wide = wide;
    83          if (wide) {
    84              index = bytes.readUnsignedShort();
    85              length = 4;
    86          } else {
    87              index = bytes.readUnsignedByte();
    88              length = 2;
    89          }
    90      }
    91
    92
    93      /***
    94       * @return index of local variable containg the return address
    95       */
    96      public final int getIndex() {
    97          return index;
    98      }
    99
    100
    101     /***
    102      * Set index of local variable containg the return address
    103      */
    104     public final void setIndex( int n ) {
    105         if (n < 0) {
    106             throw new ClassGenException("Negative index value: " + n);
    107         }
    108         index = n;
    109         setWide();
    110     }
    111
    112
    113     /***
    114      * @return mnemonic for instruction
    115      */
    116     public String toString( boolean verbose ) {
    117         return super.toString(verbose) + " " + index;
    118     }
    119
    120
    121     /*** @return return address type
    122      */
    123     public Type getType( ConstantPoolGen cp ) {
    124         return ReturnaddressType.NO_TARGET;
    125     }
    126
    127
    128     /***
    129      * Call corresponding visitor method(s). The order is:
    130      * Call visitor methods of implemented interfaces first, then
    131      * call methods according to the class hierarchy in descending order,
    132      * i.e., the most specific visitXXX() call comes last.
    133      *
    134      * @param v Visitor object
    135      */
    136     public void accept( Visitor v ) {
    137         v.visitRET(this);
    138     }
    139 }