1 package com.sun.org.apache.bcel.internal.generic; 2 3 56 import java.io.*; 57 import com.sun.org.apache.bcel.internal.util.ByteSequence; 58 import com.sun.org.apache.bcel.internal.classfile.Utility; 59 import com.sun.org.apache.bcel.internal.Constants; 60 61 67 public abstract class LocalVariableInstruction extends Instruction 68 implements TypedInstruction, IndexedInstruction { 69 protected int n = -1; private short c_tag = -1; private short canon_tag = -1; 73 private final boolean wide() { return n > Constants.MAX_BYTE; } 74 75 80 LocalVariableInstruction(short canon_tag, short c_tag) { 81 super(); 82 this.canon_tag = canon_tag; 83 this.c_tag = c_tag; 84 } 85 86 90 LocalVariableInstruction() { 91 } 92 93 98 protected LocalVariableInstruction(short opcode, short c_tag, int n) { 99 super(opcode, (short)2); 100 101 this.c_tag = c_tag; 102 canon_tag = opcode; 103 104 setIndex(n); 105 } 106 107 111 public void dump(DataOutputStream out) throws IOException { 112 if(wide()) out.writeByte(Constants.WIDE); 114 115 out.writeByte(opcode); 116 117 if(length > 1) { if(wide()) 119 out.writeShort(n); 120 else 121 out.writeByte(n); 122 } 123 } 124 125 134 public String toString(boolean verbose) { 135 if(((opcode >= Constants.ILOAD_0) && 136 (opcode <= Constants.ALOAD_3)) || 137 ((opcode >= Constants.ISTORE_0) && 138 (opcode <= Constants.ASTORE_3))) 139 return super.toString(verbose); 140 else 141 return super.toString(verbose) + " " + n; 142 } 143 144 148 protected void initFromFile(ByteSequence bytes, boolean wide) 149 throws IOException 150 { 151 if(wide) { 152 n = bytes.readUnsignedShort(); 153 length = 4; 154 } else if(((opcode >= Constants.ILOAD) && 155 (opcode <= Constants.ALOAD)) || 156 ((opcode >= Constants.ISTORE) && 157 (opcode <= Constants.ASTORE))) { 158 n = bytes.readUnsignedByte(); 159 length = 2; 160 } else if(opcode <= Constants.ALOAD_3) { n = (opcode - Constants.ILOAD_0) % 4; 162 length = 1; 163 } else { n = (opcode - Constants.ISTORE_0) % 4; 165 length = 1; 166 } 167 } 168 169 172 public final int getIndex() { return n; } 173 174 177 public void setIndex(int n) { 178 if((n < 0) || (n > Constants.MAX_SHORT)) 179 throw new ClassGenException("Illegal value: " + n); 180 181 this.n = n; 182 183 if(n >= 0 && n <= 3) { opcode = (short)(c_tag + n); 185 length = 1; 186 } else { 187 opcode = canon_tag; 188 189 if(wide()) length = 4; 191 else 192 length = 2; 193 } 194 } 195 196 198 public short getCanonicalTag() { 199 return canon_tag; 200 } 201 202 210 public Type getType(ConstantPoolGen cp) { 211 switch(canon_tag) { 212 case Constants.ILOAD: case Constants.ISTORE: 213 return Type.INT; 214 case Constants.LLOAD: case Constants.LSTORE: 215 return Type.LONG; 216 case Constants.DLOAD: case Constants.DSTORE: 217 return Type.DOUBLE; 218 case Constants.FLOAD: case Constants.FSTORE: 219 return Type.FLOAT; 220 case Constants.ALOAD: case Constants.ASTORE: 221 return Type.OBJECT; 222 223 default: throw new ClassGenException("Oops: unknown case in switch" + canon_tag); 224 } 225 } 226 } 227 228 | Popular Tags |