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