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 containing 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 * Gets the index of the local variable containing the return address. 086 * 087 * @return index of local variable containing the return address. 088 */ 089 @Override 090 public final int getIndex() { 091 return index; 092 } 093 094 /** 095 * Gets the return address type. 096 * 097 * @return return address type. 098 */ 099 @Override 100 public Type getType(final ConstantPoolGen cp) { 101 return ReturnaddressType.NO_TARGET; 102 } 103 104 /** 105 * Reads needed data (for example index) from file. 106 */ 107 @Override 108 protected void initFromFile(final ByteSequence bytes, final boolean wide) throws IOException { 109 this.wide = wide; 110 if (wide) { 111 index = bytes.readUnsignedShort(); 112 super.setLength(4); 113 } else { 114 index = bytes.readUnsignedByte(); 115 super.setLength(2); 116 } 117 } 118 119 /** 120 * Sets the index of the local variable containing the return address. 121 * 122 * @param index index of local variable containing the return address. 123 * @throws ClassGenException Thrown if index is out of bounds. 124 */ 125 @Override 126 public final void setIndex(final int index) { 127 if (!isNonNegativeUShort(index)) { 128 throw new ClassGenException("Illegal value: " + index); 129 } 130 this.index = index; 131 setWide(); 132 } 133 134 private void setWide() { 135 wide = index > Const.MAX_BYTE; 136 if (wide) { 137 super.setLength(4); // Including the wide byte 138 } else { 139 super.setLength(2); 140 } 141 } 142 143 /** 144 * @return mnemonic for instruction. 145 */ 146 @Override 147 public String toString(final boolean verbose) { 148 return super.toString(verbose) + " " + index; 149 } 150}