src/share/classes/com/sun/tools/classfile/StackMapTable_attribute.java

Tue, 19 May 2009 11:50:54 -0700

author
jjg
date
Tue, 19 May 2009 11:50:54 -0700
changeset 283
cd0630109de5
parent 229
03bcd66bd8e7
child 554
9d9f26857129
permissions
-rw-r--r--

6824493: experimental support for additional info for instructions
Reviewed-by: mcimadamore

jjg@46 1 /*
xdono@229 2 * Copyright 2007-2009 Sun Microsystems, Inc. All Rights Reserved.
jjg@46 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@46 4 *
jjg@46 5 * This code is free software; you can redistribute it and/or modify it
jjg@46 6 * under the terms of the GNU General Public License version 2 only, as
jjg@46 7 * published by the Free Software Foundation. Sun designates this
jjg@46 8 * particular file as subject to the "Classpath" exception as provided
jjg@46 9 * by Sun in the LICENSE file that accompanied this code.
jjg@46 10 *
jjg@46 11 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@46 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@46 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@46 14 * version 2 for more details (a copy is included in the LICENSE file that
jjg@46 15 * accompanied this code).
jjg@46 16 *
jjg@46 17 * You should have received a copy of the GNU General Public License version
jjg@46 18 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@46 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@46 20 *
jjg@46 21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
jjg@46 22 * CA 95054 USA or visit www.sun.com if you need additional information or
jjg@46 23 * have any questions.
jjg@46 24 */
jjg@46 25
jjg@46 26 package com.sun.tools.classfile;
jjg@46 27
jjg@46 28 import java.io.IOException;
jjg@46 29
jjg@46 30 /**
jjg@46 31 * See JVMS3, section 4.8.4.
jjg@46 32 *
jjg@46 33 * <p><b>This is NOT part of any API supported by Sun Microsystems. If
jjg@46 34 * you write code that depends on this, you do so at your own risk.
jjg@46 35 * This code and its internal interfaces are subject to change or
jjg@46 36 * deletion without notice.</b>
jjg@46 37 */
jjg@46 38 public class StackMapTable_attribute extends Attribute {
jjg@46 39 static class InvalidStackMap extends AttributeException {
jjg@198 40 private static final long serialVersionUID = -5659038410855089780L;
jjg@46 41 InvalidStackMap(String msg) {
jjg@46 42 super(msg);
jjg@46 43 }
jjg@46 44 }
jjg@46 45
jjg@46 46 StackMapTable_attribute(ClassReader cr, int name_index, int length)
jjg@46 47 throws IOException, InvalidStackMap {
jjg@46 48 super(name_index, length);
jjg@46 49 number_of_entries = cr.readUnsignedShort();
jjg@46 50 entries = new stack_map_frame[number_of_entries];
jjg@46 51 for (int i = 0; i < number_of_entries; i++)
jjg@46 52 entries[i] = stack_map_frame.read(cr);
jjg@46 53 }
jjg@46 54
jjg@46 55 public StackMapTable_attribute(ConstantPool constant_pool, stack_map_frame[] entries)
jjg@46 56 throws ConstantPoolException {
jjg@46 57 this(constant_pool.getUTF8Index(Attribute.StackMapTable), entries);
jjg@46 58 }
jjg@46 59
jjg@46 60 public StackMapTable_attribute(int name_index, stack_map_frame[] entries) {
jjg@46 61 super(name_index, length(entries));
jjg@46 62 this.number_of_entries = entries.length;
jjg@46 63 this.entries = entries;
jjg@46 64 }
jjg@46 65
jjg@46 66 public <R, D> R accept(Visitor<R, D> visitor, D data) {
jjg@46 67 return visitor.visitStackMapTable(this, data);
jjg@46 68 }
jjg@46 69
jjg@46 70 static int length(stack_map_frame[] entries) {
jjg@46 71 int n = 2;
jjg@46 72 for (stack_map_frame entry: entries)
jjg@46 73 n += entry.length();
jjg@46 74 return n;
jjg@46 75 }
jjg@46 76
jjg@46 77 public final int number_of_entries;
jjg@46 78 public final stack_map_frame entries[];
jjg@46 79
jjg@46 80 public static abstract class stack_map_frame {
jjg@46 81 static stack_map_frame read(ClassReader cr)
jjg@46 82 throws IOException, InvalidStackMap {
jjg@46 83 int frame_type = cr.readUnsignedByte();
jjg@46 84 if (frame_type <= 63)
jjg@46 85 return new same_frame(frame_type);
jjg@46 86 else if (frame_type <= 127)
jjg@46 87 return new same_locals_1_stack_item_frame(frame_type, cr);
jjg@46 88 else if (frame_type <= 246)
jjg@46 89 throw new Error("unknown frame_type " + frame_type);
jjg@46 90 else if (frame_type == 247)
jjg@46 91 return new same_locals_1_stack_item_frame_extended(frame_type, cr);
jjg@46 92 else if (frame_type <= 250)
jjg@46 93 return new chop_frame(frame_type, cr);
jjg@46 94 else if (frame_type == 251)
jjg@46 95 return new same_frame_extended(frame_type, cr);
jjg@46 96 else if (frame_type <= 254)
jjg@46 97 return new append_frame(frame_type, cr);
jjg@46 98 else
jjg@46 99 return new full_frame(frame_type, cr);
jjg@46 100 }
jjg@46 101
jjg@46 102 protected stack_map_frame(int frame_type) {
jjg@46 103 this.frame_type = frame_type;
jjg@46 104 }
jjg@46 105
jjg@46 106 public int length() {
jjg@46 107 return 1;
jjg@46 108 }
jjg@46 109
jjg@283 110 public abstract int getOffsetDelta();
jjg@283 111
jjg@46 112 public abstract <R,D> R accept(Visitor<R,D> visitor, D data);
jjg@46 113
jjg@46 114 public final int frame_type;
jjg@46 115
jjg@46 116 public static interface Visitor<R,P> {
jjg@46 117 R visit_same_frame(same_frame frame, P p);
jjg@46 118 R visit_same_locals_1_stack_item_frame(same_locals_1_stack_item_frame frame, P p);
jjg@46 119 R visit_same_locals_1_stack_item_frame_extended(same_locals_1_stack_item_frame_extended frame, P p);
jjg@46 120 R visit_chop_frame(chop_frame frame, P p);
jjg@46 121 R visit_same_frame_extended(same_frame_extended frame, P p);
jjg@46 122 R visit_append_frame(append_frame frame, P p);
jjg@46 123 R visit_full_frame(full_frame frame, P p);
jjg@46 124 }
jjg@46 125 }
jjg@46 126
jjg@46 127 public static class same_frame extends stack_map_frame {
jjg@46 128 same_frame(int frame_type) {
jjg@46 129 super(frame_type);
jjg@46 130 }
jjg@46 131
jjg@46 132 public <R, D> R accept(Visitor<R, D> visitor, D data) {
jjg@46 133 return visitor.visit_same_frame(this, data);
jjg@46 134 }
jjg@283 135
jjg@283 136 public int getOffsetDelta() {
jjg@283 137 return frame_type;
jjg@283 138 }
jjg@46 139 }
jjg@46 140
jjg@46 141 public static class same_locals_1_stack_item_frame extends stack_map_frame {
jjg@46 142 same_locals_1_stack_item_frame(int frame_type, ClassReader cr)
jjg@46 143 throws IOException, InvalidStackMap {
jjg@46 144 super(frame_type);
jjg@46 145 stack = new verification_type_info[1];
jjg@46 146 stack[0] = verification_type_info.read(cr);
jjg@46 147 }
jjg@46 148
jjg@46 149 @Override
jjg@46 150 public int length() {
jjg@46 151 return super.length() + stack[0].length();
jjg@46 152 }
jjg@46 153
jjg@46 154 public <R, D> R accept(Visitor<R, D> visitor, D data) {
jjg@46 155 return visitor.visit_same_locals_1_stack_item_frame(this, data);
jjg@46 156 }
jjg@46 157
jjg@283 158 public int getOffsetDelta() {
jjg@283 159 return frame_type - 64;
jjg@283 160 }
jjg@283 161
jjg@46 162 public final verification_type_info[] stack;
jjg@46 163 }
jjg@46 164
jjg@46 165 public static class same_locals_1_stack_item_frame_extended extends stack_map_frame {
jjg@46 166 same_locals_1_stack_item_frame_extended(int frame_type, ClassReader cr)
jjg@46 167 throws IOException, InvalidStackMap {
jjg@46 168 super(frame_type);
jjg@46 169 offset_delta = cr.readUnsignedShort();
jjg@46 170 stack = new verification_type_info[1];
jjg@46 171 stack[0] = verification_type_info.read(cr);
jjg@46 172 }
jjg@46 173
jjg@46 174 @Override
jjg@46 175 public int length() {
jjg@46 176 return super.length() + 2 + stack[0].length();
jjg@46 177 }
jjg@46 178
jjg@46 179 public <R, D> R accept(Visitor<R, D> visitor, D data) {
jjg@46 180 return visitor.visit_same_locals_1_stack_item_frame_extended(this, data);
jjg@46 181 }
jjg@46 182
jjg@283 183 public int getOffsetDelta() {
jjg@283 184 return offset_delta;
jjg@283 185 }
jjg@283 186
jjg@46 187 public final int offset_delta;
jjg@46 188 public final verification_type_info[] stack;
jjg@46 189 }
jjg@46 190
jjg@46 191 public static class chop_frame extends stack_map_frame {
jjg@46 192 chop_frame(int frame_type, ClassReader cr) throws IOException {
jjg@46 193 super(frame_type);
jjg@46 194 offset_delta = cr.readUnsignedShort();
jjg@46 195 }
jjg@46 196
jjg@46 197 @Override
jjg@46 198 public int length() {
jjg@46 199 return super.length() + 2;
jjg@46 200 }
jjg@46 201
jjg@46 202 public <R, D> R accept(Visitor<R, D> visitor, D data) {
jjg@46 203 return visitor.visit_chop_frame(this, data);
jjg@46 204 }
jjg@46 205
jjg@283 206 public int getOffsetDelta() {
jjg@283 207 return offset_delta;
jjg@283 208 }
jjg@283 209
jjg@46 210 public final int offset_delta;
jjg@46 211 }
jjg@46 212
jjg@46 213 public static class same_frame_extended extends stack_map_frame {
jjg@46 214 same_frame_extended(int frame_type, ClassReader cr) throws IOException {
jjg@46 215 super(frame_type);
jjg@46 216 offset_delta = cr.readUnsignedShort();
jjg@46 217 }
jjg@46 218
jjg@46 219 @Override
jjg@46 220 public int length() {
jjg@46 221 return super.length() + 2;
jjg@46 222 }
jjg@46 223
jjg@46 224 public <R, D> R accept(Visitor<R, D> visitor, D data) {
jjg@46 225 return visitor.visit_same_frame_extended(this, data);
jjg@46 226 }
jjg@46 227
jjg@283 228 public int getOffsetDelta() {
jjg@283 229 return offset_delta;
jjg@283 230 }
jjg@283 231
jjg@46 232 public final int offset_delta;
jjg@46 233 }
jjg@46 234
jjg@46 235 public static class append_frame extends stack_map_frame {
jjg@46 236 append_frame(int frame_type, ClassReader cr)
jjg@46 237 throws IOException, InvalidStackMap {
jjg@46 238 super(frame_type);
jjg@46 239 offset_delta = cr.readUnsignedShort();
jjg@46 240 locals = new verification_type_info[frame_type - 251];
jjg@46 241 for (int i = 0; i < locals.length; i++)
jjg@46 242 locals[i] = verification_type_info.read(cr);
jjg@46 243 }
jjg@46 244
jjg@46 245 @Override
jjg@46 246 public int length() {
jjg@46 247 int n = super.length() + 2;
jjg@46 248 for (verification_type_info local: locals)
jjg@46 249 n += local.length();
jjg@46 250 return n;
jjg@46 251 }
jjg@46 252
jjg@46 253 public <R, D> R accept(Visitor<R, D> visitor, D data) {
jjg@46 254 return visitor.visit_append_frame(this, data);
jjg@46 255 }
jjg@46 256
jjg@283 257 public int getOffsetDelta() {
jjg@283 258 return offset_delta;
jjg@283 259 }
jjg@283 260
jjg@46 261 public final int offset_delta;
jjg@46 262 public final verification_type_info[] locals;
jjg@46 263 }
jjg@46 264
jjg@46 265 public static class full_frame extends stack_map_frame {
jjg@46 266 full_frame(int frame_type, ClassReader cr)
jjg@46 267 throws IOException, InvalidStackMap {
jjg@46 268 super(frame_type);
jjg@46 269 offset_delta = cr.readUnsignedShort();
jjg@46 270 number_of_locals = cr.readUnsignedShort();
jjg@46 271 locals = new verification_type_info[number_of_locals];
jjg@46 272 for (int i = 0; i < locals.length; i++)
jjg@46 273 locals[i] = verification_type_info.read(cr);
jjg@46 274 number_of_stack_items = cr.readUnsignedShort();
jjg@46 275 stack = new verification_type_info[number_of_stack_items];
jjg@46 276 for (int i = 0; i < stack.length; i++)
jjg@46 277 stack[i] = verification_type_info.read(cr);
jjg@46 278 }
jjg@46 279
jjg@46 280 @Override
jjg@46 281 public int length() {
jjg@46 282 int n = super.length() + 2;
jjg@46 283 for (verification_type_info local: locals)
jjg@46 284 n += local.length();
jjg@46 285 n += 2;
jjg@46 286 for (verification_type_info item: stack)
jjg@46 287 n += item.length();
jjg@46 288 return n;
jjg@46 289 }
jjg@46 290
jjg@46 291 public <R, D> R accept(Visitor<R, D> visitor, D data) {
jjg@46 292 return visitor.visit_full_frame(this, data);
jjg@46 293 }
jjg@46 294
jjg@283 295 public int getOffsetDelta() {
jjg@283 296 return offset_delta;
jjg@283 297 }
jjg@283 298
jjg@46 299 public final int offset_delta;
jjg@46 300 public final int number_of_locals;
jjg@46 301 public final verification_type_info[] locals;
jjg@46 302 public final int number_of_stack_items;
jjg@46 303 public final verification_type_info[] stack;
jjg@46 304 }
jjg@46 305
jjg@46 306 public static class verification_type_info {
jjg@46 307 public static final int ITEM_Top = 0;
jjg@46 308 public static final int ITEM_Integer = 1;
jjg@46 309 public static final int ITEM_Float = 2;
jjg@46 310 public static final int ITEM_Long = 4;
jjg@46 311 public static final int ITEM_Double = 3;
jjg@46 312 public static final int ITEM_Null = 5;
jjg@46 313 public static final int ITEM_UninitializedThis = 6;
jjg@46 314 public static final int ITEM_Object = 7;
jjg@46 315 public static final int ITEM_Uninitialized = 8;
jjg@46 316
jjg@46 317 static verification_type_info read(ClassReader cr)
jjg@46 318 throws IOException, InvalidStackMap {
jjg@46 319 int tag = cr.readUnsignedByte();
jjg@46 320 switch (tag) {
jjg@46 321 case ITEM_Top:
jjg@46 322 case ITEM_Integer:
jjg@46 323 case ITEM_Float:
jjg@46 324 case ITEM_Long:
jjg@46 325 case ITEM_Double:
jjg@46 326 case ITEM_Null:
jjg@46 327 case ITEM_UninitializedThis:
jjg@46 328 return new verification_type_info(tag);
jjg@46 329
jjg@46 330 case ITEM_Object:
jjg@46 331 return new Object_variable_info(cr);
jjg@46 332
jjg@46 333 case ITEM_Uninitialized:
jjg@46 334 return new Uninitialized_variable_info(cr);
jjg@46 335
jjg@46 336 default:
jjg@46 337 throw new InvalidStackMap("unrecognized verification_type_info tag");
jjg@46 338 }
jjg@46 339 }
jjg@46 340
jjg@283 341 protected verification_type_info(int tag) {
jjg@46 342 this.tag = tag;
jjg@46 343 }
jjg@46 344
jjg@46 345 public int length() {
jjg@46 346 return 1;
jjg@46 347 }
jjg@46 348
jjg@46 349 public final int tag;
jjg@46 350 }
jjg@46 351
jjg@46 352 public static class Object_variable_info extends verification_type_info {
jjg@46 353 Object_variable_info(ClassReader cr) throws IOException {
jjg@46 354 super(ITEM_Object);
jjg@46 355 cpool_index = cr.readUnsignedShort();
jjg@46 356 }
jjg@46 357
jjg@46 358 @Override
jjg@46 359 public int length() {
jjg@46 360 return super.length() + 2;
jjg@46 361 }
jjg@46 362
jjg@46 363 public final int cpool_index;
jjg@46 364 }
jjg@46 365
jjg@46 366 public static class Uninitialized_variable_info extends verification_type_info {
jjg@46 367 Uninitialized_variable_info(ClassReader cr) throws IOException {
jjg@46 368 super(ITEM_Uninitialized);
jjg@46 369 offset = cr.readUnsignedShort();
jjg@46 370 }
jjg@46 371
jjg@46 372 @Override
jjg@46 373 public int length() {
jjg@46 374 return super.length() + 2;
jjg@46 375 }
jjg@46 376
jjg@46 377 public final int offset;
jjg@46 378
jjg@46 379 }
jjg@46 380 }

mercurial