001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 *  Unless required by applicable law or agreed to in writing, software
012 *  distributed under the License is distributed on an "AS IS" BASIS,
013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 *  See the License for the specific language governing permissions and
015 *  limitations under the License.
016 *
017 */
018package org.apache.bcel.generic;
019
020import java.io.DataOutputStream;
021import java.io.IOException;
022
023import org.apache.bcel.util.ByteSequence;
024
025/** 
026 * RET - Return from subroutine
027 *
028 * <PRE>Stack: ... -&gt; ...</PRE>
029 *
030 * @version $Id: RET.java 1747278 2016-06-07 17:28:43Z britter $
031 */
032public class RET extends Instruction implements IndexedInstruction, TypedInstruction {
033
034    private boolean wide;
035    private int index; // index to local variable containg the return address
036
037
038    /**
039     * Empty constructor needed for the Class.newInstance() statement in
040     * Instruction.readInstruction(). Not to be used otherwise.
041     */
042    RET() {
043    }
044
045
046    public RET(final int index) {
047        super(org.apache.bcel.Const.RET, (short) 2);
048        setIndex(index); // May set wide as side effect
049    }
050
051
052    /**
053     * Dump instruction as byte code to stream out.
054     * @param out Output stream
055     */
056    @Override
057    public void dump( final DataOutputStream out ) throws IOException {
058        if (wide) {
059            out.writeByte(org.apache.bcel.Const.WIDE);
060        }
061        out.writeByte(super.getOpcode());
062        if (wide) {
063            out.writeShort(index);
064        } else {
065            out.writeByte(index);
066        }
067    }
068
069
070    private void setWide() {
071        wide = index > org.apache.bcel.Const.MAX_BYTE;
072        if (wide) {
073            super.setLength(4); // Including the wide byte  
074        } else {
075            super.setLength(2);
076        }
077    }
078
079
080    /**
081     * Read needed data (e.g. index) from file.
082     */
083    @Override
084    protected void initFromFile( final ByteSequence bytes, final boolean wide ) throws IOException {
085        this.wide = wide;
086        if (wide) {
087            index = bytes.readUnsignedShort();
088            super.setLength(4);
089        } else {
090            index = bytes.readUnsignedByte();
091            super.setLength(2);
092        }
093    }
094
095
096    /**
097     * @return index of local variable containg the return address
098     */
099    @Override
100    public final int getIndex() {
101        return index;
102    }
103
104
105    /**
106     * Set index of local variable containg the return address
107     */
108    @Override
109    public final void setIndex( final int n ) {
110        if (n < 0) {
111            throw new ClassGenException("Negative index value: " + n);
112        }
113        index = n;
114        setWide();
115    }
116
117
118    /**
119     * @return mnemonic for instruction
120     */
121    @Override
122    public String toString( final boolean verbose ) {
123        return super.toString(verbose) + " " + index;
124    }
125
126
127    /** @return return address type
128     */
129    @Override
130    public Type getType( final ConstantPoolGen cp ) {
131        return ReturnaddressType.NO_TARGET;
132    }
133
134
135    /**
136     * Call corresponding visitor method(s). The order is:
137     * Call visitor methods of implemented interfaces first, then
138     * call methods according to the class hierarchy in descending order,
139     * i.e., the most specific visitXXX() call comes last.
140     *
141     * @param v Visitor object
142     */
143    @Override
144    public void accept( final Visitor v ) {
145        v.visitRET(this);
146    }
147}