1 /* 2 * reserved comment block 3 * DO NOT REMOVE OR ALTER! 4 */ 5 /* 6 * Licensed to the Apache Software Foundation (ASF) under one or more 7 * contributor license agreements. See the NOTICE file distributed with 8 * this work for additional information regarding copyright ownership. 9 * The ASF licenses this file to You under the Apache License, Version 2.0 10 * (the "License"); you may not use this file except in compliance with 11 * the License. You may obtain a copy of the License at 12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 15 * Unless required by applicable law or agreed to in writing, software 16 * distributed under the License is distributed on an "AS IS" BASIS, 17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 * See the License for the specific language governing permissions and 19 * limitations under the License. 20 */ 21 22 package com.sun.org.apache.bcel.internal.generic; 23 24 import java.io.*; 25 import com.sun.org.apache.bcel.internal.util.ByteSequence; 26 import com.sun.org.apache.bcel.internal.classfile.Utility; 27 import com.sun.org.apache.bcel.internal.Constants; 28 29 /** 30 * Abstract super class for instructions dealing with local variables. 31 * 32 * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A> 33 */ 34 public abstract class LocalVariableInstruction extends Instruction 35 implements TypedInstruction, IndexedInstruction { 36 protected int n = -1; // index of referenced variable 37 private short c_tag = -1; // compact version, such as ILOAD_0 38 private short canon_tag = -1; // canonical tag such as ILOAD 39 40 private final boolean wide() { return n > Constants.MAX_BYTE; } 41 42 /** 43 * Empty constructor needed for the Class.newInstance() statement in 44 * Instruction.readInstruction(). Not to be used otherwise. 45 * tag and length are defined in readInstruction and initFromFile, respectively. 46 */ 47 LocalVariableInstruction(short canon_tag, short c_tag) { 48 super(); 49 this.canon_tag = canon_tag; 50 this.c_tag = c_tag; 51 } 52 53 /** 54 * Empty constructor needed for the Class.newInstance() statement in 55 * Instruction.readInstruction(). Also used by IINC()! 56 */ 57 LocalVariableInstruction() { 58 } 59 60 /** 61 * @param opcode Instruction opcode 62 * @param c_tag Instruction number for compact version, ALOAD_0, e.g. 63 * @param n local variable index (unsigned short) 64 */ 65 protected LocalVariableInstruction(short opcode, short c_tag, int n) { 66 super(opcode, (short)2); 67 68 this.c_tag = c_tag; 69 canon_tag = opcode; 70 71 setIndex(n); 72 } 73 74 /** 75 * Dump instruction as byte code to stream out. 76 * @param out Output stream 77 */ 78 public void dump(DataOutputStream out) throws IOException { 79 if(wide()) // Need WIDE prefix ? 80 out.writeByte(Constants.WIDE); 81 82 out.writeByte(opcode); 83 84 if(length > 1) { // Otherwise ILOAD_n, instruction, e.g. 85 if(wide()) 86 out.writeShort(n); 87 else 88 out.writeByte(n); 89 } 90 } 91 92 /** 93 * Long output format: 94 * 95 * <name of opcode> "["<opcode number>"]" 96 * "("<length of instruction>")" "<"< local variable index>">" 97 * 98 * @param verbose long/short format switch 99 * @return mnemonic for instruction 100 */ 101 public String toString(boolean verbose) { 102 if(((opcode >= Constants.ILOAD_0) && 103 (opcode <= Constants.ALOAD_3)) || 104 ((opcode >= Constants.ISTORE_0) && 105 (opcode <= Constants.ASTORE_3))) 106 return super.toString(verbose); 107 else 108 return super.toString(verbose) + " " + n; 109 } 110 111 /** 112 * Read needed data (e.g. index) from file. 113 * PRE: (ILOAD <= tag <= ALOAD_3) || (ISTORE <= tag <= ASTORE_3) 114 */ 115 protected void initFromFile(ByteSequence bytes, boolean wide) 116 throws IOException 117 { 118 if(wide) { 119 n = bytes.readUnsignedShort(); 120 length = 4; 121 } else if(((opcode >= Constants.ILOAD) && 122 (opcode <= Constants.ALOAD)) || 123 ((opcode >= Constants.ISTORE) && 124 (opcode <= Constants.ASTORE))) { 125 n = bytes.readUnsignedByte(); 126 length = 2; 127 } else if(opcode <= Constants.ALOAD_3) { // compact load instruction such as ILOAD_2 128 n = (opcode - Constants.ILOAD_0) % 4; 129 length = 1; 130 } else { // Assert ISTORE_0 <= tag <= ASTORE_3 131 n = (opcode - Constants.ISTORE_0) % 4; 132 length = 1; 133 } 134 } 135 136 /** 137 * @return local variable index referred by this instruction. 138 */ 139 public final int getIndex() { return n; } 140 141 /** 142 * Set the local variable index 143 */ 144 public void setIndex(int n) { 145 if((n < 0) || (n > Constants.MAX_SHORT)) 146 throw new ClassGenException("Illegal value: " + n); 147 148 this.n = n; 149 150 if(n >= 0 && n <= 3) { // Use more compact instruction xLOAD_n 151 opcode = (short)(c_tag + n); 152 length = 1; 153 } else { 154 opcode = canon_tag; 155 156 if(wide()) // Need WIDE prefix ? 157 length = 4; 158 else 159 length = 2; 160 } 161 } 162 163 /** @return canonical tag for instruction, e.g., ALOAD for ALOAD_0 164 */ 165 public short getCanonicalTag() { 166 return canon_tag; 167 } 168 169 /** 170 * Returns the type associated with the instruction - 171 * in case of ALOAD or ASTORE Type.OBJECT is returned. 172 * This is just a bit incorrect, because ALOAD and ASTORE 173 * may work on every ReferenceType (including Type.NULL) and 174 * ASTORE may even work on a ReturnaddressType . 175 * @return type associated with the instruction 176 */ 177 public Type getType(ConstantPoolGen cp) { 178 switch(canon_tag) { 179 case Constants.ILOAD: case Constants.ISTORE: 180 return Type.INT; 181 case Constants.LLOAD: case Constants.LSTORE: 182 return Type.LONG; 183 case Constants.DLOAD: case Constants.DSTORE: 184 return Type.DOUBLE; 185 case Constants.FLOAD: case Constants.FSTORE: 186 return Type.FLOAT; 187 case Constants.ALOAD: case Constants.ASTORE: 188 return Type.OBJECT; 189 190 default: throw new ClassGenException("Oops: unknown case in switch" + canon_tag); 191 } 192 } 193 } | 1 /* 2 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. 3 */ 4 /* 5 * Licensed to the Apache Software Foundation (ASF) under one or more 6 * contributor license agreements. See the NOTICE file distributed with 7 * this work for additional information regarding copyright ownership. 8 * The ASF licenses this file to You under the Apache License, Version 2.0 9 * (the "License"); you may not use this file except in compliance with 10 * the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, software 15 * distributed under the License is distributed on an "AS IS" BASIS, 16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 * See the License for the specific language governing permissions and 18 * limitations under the License. 19 */ 20 package com.sun.org.apache.bcel.internal.generic; 21 22 import java.io.DataOutputStream; 23 import java.io.IOException; 24 25 import com.sun.org.apache.bcel.internal.Const; 26 import com.sun.org.apache.bcel.internal.util.ByteSequence; 27 28 /** 29 * Abstract super class for instructions dealing with local variables. 30 * 31 * @version $Id: LocalVariableInstruction.java 1747278 2016-06-07 17:28:43Z 32 * britter $ 33 */ 34 public abstract class LocalVariableInstruction extends Instruction implements TypedInstruction, 35 IndexedInstruction { 36 37 private int n = -1; // index of referenced variable 38 private short c_tag = -1; // compact version, such as ILOAD_0 39 private short canon_tag = -1; // canonical tag such as ILOAD 40 41 private boolean wide() { 42 return n > Const.MAX_BYTE; 43 } 44 45 /** 46 * Empty constructor needed for the Class.newInstance() statement in 47 * Instruction.readInstruction(). Not to be used otherwise. tag and length 48 * are defined in readInstruction and initFromFile, respectively. 49 */ 50 LocalVariableInstruction(final short canon_tag, final short c_tag) { 51 super(); 52 this.canon_tag = canon_tag; 53 this.c_tag = c_tag; 54 } 55 56 /** 57 * Empty constructor needed for the Class.newInstance() statement in 58 * Instruction.readInstruction(). Also used by IINC()! 59 */ 60 LocalVariableInstruction() { 61 } 62 63 /** 64 * @param opcode Instruction opcode 65 * @param c_tag Instruction number for compact version, ALOAD_0, e.g. 66 * @param n local variable index (unsigned short) 67 */ 68 protected LocalVariableInstruction(final short opcode, final short c_tag, final int n) { 69 super(opcode, (short) 2); 70 this.c_tag = c_tag; 71 canon_tag = opcode; 72 setIndex(n); 73 } 74 75 /** 76 * Dump instruction as byte code to stream out. 77 * 78 * @param out Output stream 79 */ 80 @Override 81 public void dump(final DataOutputStream out) throws IOException { 82 if (wide()) { 83 out.writeByte(Const.WIDE); 84 } 85 out.writeByte(super.getOpcode()); 86 if (super.getLength() > 1) { // Otherwise ILOAD_n, instruction, e.g. 87 if (wide()) { 88 out.writeShort(n); 89 } else { 90 out.writeByte(n); 91 } 92 } 93 } 94 95 /** 96 * Long output format: 97 * 98 * <name of opcode> "["<opcode number>"]" "("<length of 99 * instruction>")" "<"< local variable index>">" 100 * 101 * @param verbose long/short format switch 102 * @return mnemonic for instruction 103 */ 104 @Override 105 public String toString(final boolean verbose) { 106 final short _opcode = super.getOpcode(); 107 if (((_opcode >= Const.ILOAD_0) && (_opcode <= Const.ALOAD_3)) 108 || ((_opcode >= Const.ISTORE_0) && (_opcode <= Const.ASTORE_3))) { 109 return super.toString(verbose); 110 } 111 return super.toString(verbose) + " " + n; 112 } 113 114 /** 115 * Read needed data (e.g. index) from file. 116 * <pre> 117 * (ILOAD <= tag <= ALOAD_3) || (ISTORE <= tag <= ASTORE_3) 118 * </pre> 119 */ 120 @Override 121 protected void initFromFile(final ByteSequence bytes, final boolean wide) throws IOException { 122 if (wide) { 123 n = bytes.readUnsignedShort(); 124 super.setLength(4); 125 } else { 126 final short _opcode = super.getOpcode(); 127 if (((_opcode >= Const.ILOAD) && (_opcode <= Const.ALOAD)) 128 || ((_opcode >= Const.ISTORE) && (_opcode <= Const.ASTORE))) { 129 n = bytes.readUnsignedByte(); 130 super.setLength(2); 131 } else if (_opcode <= Const.ALOAD_3) { // compact load instruction such as ILOAD_2 132 n = (_opcode - Const.ILOAD_0) % 4; 133 super.setLength(1); 134 } else { // Assert ISTORE_0 <= tag <= ASTORE_3 135 n = (_opcode - Const.ISTORE_0) % 4; 136 super.setLength(1); 137 } 138 } 139 } 140 141 /** 142 * @return local variable index (n) referred by this instruction. 143 */ 144 @Override 145 public final int getIndex() { 146 return n; 147 } 148 149 /** 150 * Set the local variable index. also updates opcode and length TODO Why? 151 * 152 * @see #setIndexOnly(int) 153 */ 154 @Override 155 public void setIndex(final int n) { // TODO could be package-protected? 156 if ((n < 0) || (n > Const.MAX_SHORT)) { 157 throw new ClassGenException("Illegal value: " + n); 158 } 159 this.n = n; 160 // Cannot be < 0 as this is checked above 161 if (n <= 3) { // Use more compact instruction xLOAD_n 162 super.setOpcode((short) (c_tag + n)); 163 super.setLength(1); 164 } else { 165 super.setOpcode(canon_tag); 166 if (wide()) { 167 super.setLength(4); 168 } else { 169 super.setLength(2); 170 } 171 } 172 } 173 174 /** 175 * @return canonical tag for instruction, e.g., ALOAD for ALOAD_0 176 */ 177 public short getCanonicalTag() { 178 return canon_tag; 179 } 180 181 /** 182 * Returns the type associated with the instruction - in case of ALOAD or 183 * ASTORE Type.OBJECT is returned. This is just a bit incorrect, because 184 * ALOAD and ASTORE may work on every ReferenceType (including Type.NULL) 185 * and ASTORE may even work on a ReturnaddressType . 186 * 187 * @return type associated with the instruction 188 */ 189 @Override 190 public Type getType(final ConstantPoolGen cp) { 191 switch (canon_tag) { 192 case Const.ILOAD: 193 case Const.ISTORE: 194 return Type.INT; 195 case Const.LLOAD: 196 case Const.LSTORE: 197 return Type.LONG; 198 case Const.DLOAD: 199 case Const.DSTORE: 200 return Type.DOUBLE; 201 case Const.FLOAD: 202 case Const.FSTORE: 203 return Type.FLOAT; 204 case Const.ALOAD: 205 case Const.ASTORE: 206 return Type.OBJECT; 207 default: 208 throw new ClassGenException("Oops: unknown case in switch" + canon_tag); 209 } 210 } 211 212 /** 213 * Sets the index of the referenced variable (n) only 214 * 215 * @since 6.0 216 * @see #setIndex(int) 217 */ 218 final void setIndexOnly(final int n) { 219 this.n = n; 220 } 221 } |