jjg@46: /*
ohair@554: * Copyright (c) 2007, 2009, Oracle and/or its affiliates. All rights reserved.
jjg@46: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@46: *
jjg@46: * This code is free software; you can redistribute it and/or modify it
jjg@46: * under the terms of the GNU General Public License version 2 only, as
ohair@554: * published by the Free Software Foundation. Oracle designates this
jjg@46: * particular file as subject to the "Classpath" exception as provided
ohair@554: * by Oracle in the LICENSE file that accompanied this code.
jjg@46: *
jjg@46: * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@46: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@46: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@46: * version 2 for more details (a copy is included in the LICENSE file that
jjg@46: * accompanied this code).
jjg@46: *
jjg@46: * You should have received a copy of the GNU General Public License version
jjg@46: * 2 along with this work; if not, write to the Free Software Foundation,
jjg@46: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@46: *
ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554: * or visit www.oracle.com if you need additional information or have any
ohair@554: * questions.
jjg@46: */
jjg@46:
jjg@46: package com.sun.tools.classfile;
jjg@46:
jjg@46: import java.io.IOException;
jjg@46:
jjg@46: /**
jjh@972: * See JVMS, section 4.8.4.
jjg@46: *
jjg@581: *
This is NOT part of any supported API.
jjg@581: * If you write code that depends on this, you do so at your own risk.
jjg@46: * This code and its internal interfaces are subject to change or
jjg@46: * deletion without notice.
jjg@46: */
jjg@46: public class StackMapTable_attribute extends Attribute {
jjg@46: static class InvalidStackMap extends AttributeException {
jjg@198: private static final long serialVersionUID = -5659038410855089780L;
jjg@46: InvalidStackMap(String msg) {
jjg@46: super(msg);
jjg@46: }
jjg@46: }
jjg@46:
jjg@46: StackMapTable_attribute(ClassReader cr, int name_index, int length)
jjg@46: throws IOException, InvalidStackMap {
jjg@46: super(name_index, length);
jjg@46: number_of_entries = cr.readUnsignedShort();
jjg@46: entries = new stack_map_frame[number_of_entries];
jjg@46: for (int i = 0; i < number_of_entries; i++)
jjg@46: entries[i] = stack_map_frame.read(cr);
jjg@46: }
jjg@46:
jjg@46: public StackMapTable_attribute(ConstantPool constant_pool, stack_map_frame[] entries)
jjg@46: throws ConstantPoolException {
jjg@46: this(constant_pool.getUTF8Index(Attribute.StackMapTable), entries);
jjg@46: }
jjg@46:
jjg@46: public StackMapTable_attribute(int name_index, stack_map_frame[] entries) {
jjg@46: super(name_index, length(entries));
jjg@46: this.number_of_entries = entries.length;
jjg@46: this.entries = entries;
jjg@46: }
jjg@46:
jjg@46: public R accept(Visitor visitor, D data) {
jjg@46: return visitor.visitStackMapTable(this, data);
jjg@46: }
jjg@46:
jjg@46: static int length(stack_map_frame[] entries) {
jjg@46: int n = 2;
jjg@46: for (stack_map_frame entry: entries)
jjg@46: n += entry.length();
jjg@46: return n;
jjg@46: }
jjg@46:
jjg@46: public final int number_of_entries;
jjg@46: public final stack_map_frame entries[];
jjg@46:
jjg@46: public static abstract class stack_map_frame {
jjg@46: static stack_map_frame read(ClassReader cr)
jjg@46: throws IOException, InvalidStackMap {
jjg@46: int frame_type = cr.readUnsignedByte();
jjg@46: if (frame_type <= 63)
jjg@46: return new same_frame(frame_type);
jjg@46: else if (frame_type <= 127)
jjg@46: return new same_locals_1_stack_item_frame(frame_type, cr);
jjg@46: else if (frame_type <= 246)
jjg@46: throw new Error("unknown frame_type " + frame_type);
jjg@46: else if (frame_type == 247)
jjg@46: return new same_locals_1_stack_item_frame_extended(frame_type, cr);
jjg@46: else if (frame_type <= 250)
jjg@46: return new chop_frame(frame_type, cr);
jjg@46: else if (frame_type == 251)
jjg@46: return new same_frame_extended(frame_type, cr);
jjg@46: else if (frame_type <= 254)
jjg@46: return new append_frame(frame_type, cr);
jjg@46: else
jjg@46: return new full_frame(frame_type, cr);
jjg@46: }
jjg@46:
jjg@46: protected stack_map_frame(int frame_type) {
jjg@46: this.frame_type = frame_type;
jjg@46: }
jjg@46:
jjg@46: public int length() {
jjg@46: return 1;
jjg@46: }
jjg@46:
jjg@283: public abstract int getOffsetDelta();
jjg@283:
jjg@46: public abstract R accept(Visitor visitor, D data);
jjg@46:
jjg@46: public final int frame_type;
jjg@46:
jjg@46: public static interface Visitor {
jjg@46: R visit_same_frame(same_frame frame, P p);
jjg@46: R visit_same_locals_1_stack_item_frame(same_locals_1_stack_item_frame frame, P p);
jjg@46: R visit_same_locals_1_stack_item_frame_extended(same_locals_1_stack_item_frame_extended frame, P p);
jjg@46: R visit_chop_frame(chop_frame frame, P p);
jjg@46: R visit_same_frame_extended(same_frame_extended frame, P p);
jjg@46: R visit_append_frame(append_frame frame, P p);
jjg@46: R visit_full_frame(full_frame frame, P p);
jjg@46: }
jjg@46: }
jjg@46:
jjg@46: public static class same_frame extends stack_map_frame {
jjg@46: same_frame(int frame_type) {
jjg@46: super(frame_type);
jjg@46: }
jjg@46:
jjg@46: public R accept(Visitor visitor, D data) {
jjg@46: return visitor.visit_same_frame(this, data);
jjg@46: }
jjg@283:
jjg@283: public int getOffsetDelta() {
jjg@283: return frame_type;
jjg@283: }
jjg@46: }
jjg@46:
jjg@46: public static class same_locals_1_stack_item_frame extends stack_map_frame {
jjg@46: same_locals_1_stack_item_frame(int frame_type, ClassReader cr)
jjg@46: throws IOException, InvalidStackMap {
jjg@46: super(frame_type);
jjg@46: stack = new verification_type_info[1];
jjg@46: stack[0] = verification_type_info.read(cr);
jjg@46: }
jjg@46:
jjg@46: @Override
jjg@46: public int length() {
jjg@46: return super.length() + stack[0].length();
jjg@46: }
jjg@46:
jjg@46: public R accept(Visitor visitor, D data) {
jjg@46: return visitor.visit_same_locals_1_stack_item_frame(this, data);
jjg@46: }
jjg@46:
jjg@283: public int getOffsetDelta() {
jjg@283: return frame_type - 64;
jjg@283: }
jjg@283:
jjg@46: public final verification_type_info[] stack;
jjg@46: }
jjg@46:
jjg@46: public static class same_locals_1_stack_item_frame_extended extends stack_map_frame {
jjg@46: same_locals_1_stack_item_frame_extended(int frame_type, ClassReader cr)
jjg@46: throws IOException, InvalidStackMap {
jjg@46: super(frame_type);
jjg@46: offset_delta = cr.readUnsignedShort();
jjg@46: stack = new verification_type_info[1];
jjg@46: stack[0] = verification_type_info.read(cr);
jjg@46: }
jjg@46:
jjg@46: @Override
jjg@46: public int length() {
jjg@46: return super.length() + 2 + stack[0].length();
jjg@46: }
jjg@46:
jjg@46: public R accept(Visitor visitor, D data) {
jjg@46: return visitor.visit_same_locals_1_stack_item_frame_extended(this, data);
jjg@46: }
jjg@46:
jjg@283: public int getOffsetDelta() {
jjg@283: return offset_delta;
jjg@283: }
jjg@283:
jjg@46: public final int offset_delta;
jjg@46: public final verification_type_info[] stack;
jjg@46: }
jjg@46:
jjg@46: public static class chop_frame extends stack_map_frame {
jjg@46: chop_frame(int frame_type, ClassReader cr) throws IOException {
jjg@46: super(frame_type);
jjg@46: offset_delta = cr.readUnsignedShort();
jjg@46: }
jjg@46:
jjg@46: @Override
jjg@46: public int length() {
jjg@46: return super.length() + 2;
jjg@46: }
jjg@46:
jjg@46: public R accept(Visitor visitor, D data) {
jjg@46: return visitor.visit_chop_frame(this, data);
jjg@46: }
jjg@46:
jjg@283: public int getOffsetDelta() {
jjg@283: return offset_delta;
jjg@283: }
jjg@283:
jjg@46: public final int offset_delta;
jjg@46: }
jjg@46:
jjg@46: public static class same_frame_extended extends stack_map_frame {
jjg@46: same_frame_extended(int frame_type, ClassReader cr) throws IOException {
jjg@46: super(frame_type);
jjg@46: offset_delta = cr.readUnsignedShort();
jjg@46: }
jjg@46:
jjg@46: @Override
jjg@46: public int length() {
jjg@46: return super.length() + 2;
jjg@46: }
jjg@46:
jjg@46: public R accept(Visitor visitor, D data) {
jjg@46: return visitor.visit_same_frame_extended(this, data);
jjg@46: }
jjg@46:
jjg@283: public int getOffsetDelta() {
jjg@283: return offset_delta;
jjg@283: }
jjg@283:
jjg@46: public final int offset_delta;
jjg@46: }
jjg@46:
jjg@46: public static class append_frame extends stack_map_frame {
jjg@46: append_frame(int frame_type, ClassReader cr)
jjg@46: throws IOException, InvalidStackMap {
jjg@46: super(frame_type);
jjg@46: offset_delta = cr.readUnsignedShort();
jjg@46: locals = new verification_type_info[frame_type - 251];
jjg@46: for (int i = 0; i < locals.length; i++)
jjg@46: locals[i] = verification_type_info.read(cr);
jjg@46: }
jjg@46:
jjg@46: @Override
jjg@46: public int length() {
jjg@46: int n = super.length() + 2;
jjg@46: for (verification_type_info local: locals)
jjg@46: n += local.length();
jjg@46: return n;
jjg@46: }
jjg@46:
jjg@46: public R accept(Visitor visitor, D data) {
jjg@46: return visitor.visit_append_frame(this, data);
jjg@46: }
jjg@46:
jjg@283: public int getOffsetDelta() {
jjg@283: return offset_delta;
jjg@283: }
jjg@283:
jjg@46: public final int offset_delta;
jjg@46: public final verification_type_info[] locals;
jjg@46: }
jjg@46:
jjg@46: public static class full_frame extends stack_map_frame {
jjg@46: full_frame(int frame_type, ClassReader cr)
jjg@46: throws IOException, InvalidStackMap {
jjg@46: super(frame_type);
jjg@46: offset_delta = cr.readUnsignedShort();
jjg@46: number_of_locals = cr.readUnsignedShort();
jjg@46: locals = new verification_type_info[number_of_locals];
jjg@46: for (int i = 0; i < locals.length; i++)
jjg@46: locals[i] = verification_type_info.read(cr);
jjg@46: number_of_stack_items = cr.readUnsignedShort();
jjg@46: stack = new verification_type_info[number_of_stack_items];
jjg@46: for (int i = 0; i < stack.length; i++)
jjg@46: stack[i] = verification_type_info.read(cr);
jjg@46: }
jjg@46:
jjg@46: @Override
jjg@46: public int length() {
jjg@46: int n = super.length() + 2;
jjg@46: for (verification_type_info local: locals)
jjg@46: n += local.length();
jjg@46: n += 2;
jjg@46: for (verification_type_info item: stack)
jjg@46: n += item.length();
jjg@46: return n;
jjg@46: }
jjg@46:
jjg@46: public R accept(Visitor visitor, D data) {
jjg@46: return visitor.visit_full_frame(this, data);
jjg@46: }
jjg@46:
jjg@283: public int getOffsetDelta() {
jjg@283: return offset_delta;
jjg@283: }
jjg@283:
jjg@46: public final int offset_delta;
jjg@46: public final int number_of_locals;
jjg@46: public final verification_type_info[] locals;
jjg@46: public final int number_of_stack_items;
jjg@46: public final verification_type_info[] stack;
jjg@46: }
jjg@46:
jjg@46: public static class verification_type_info {
jjg@46: public static final int ITEM_Top = 0;
jjg@46: public static final int ITEM_Integer = 1;
jjg@46: public static final int ITEM_Float = 2;
jjg@46: public static final int ITEM_Long = 4;
jjg@46: public static final int ITEM_Double = 3;
jjg@46: public static final int ITEM_Null = 5;
jjg@46: public static final int ITEM_UninitializedThis = 6;
jjg@46: public static final int ITEM_Object = 7;
jjg@46: public static final int ITEM_Uninitialized = 8;
jjg@46:
jjg@46: static verification_type_info read(ClassReader cr)
jjg@46: throws IOException, InvalidStackMap {
jjg@46: int tag = cr.readUnsignedByte();
jjg@46: switch (tag) {
jjg@46: case ITEM_Top:
jjg@46: case ITEM_Integer:
jjg@46: case ITEM_Float:
jjg@46: case ITEM_Long:
jjg@46: case ITEM_Double:
jjg@46: case ITEM_Null:
jjg@46: case ITEM_UninitializedThis:
jjg@46: return new verification_type_info(tag);
jjg@46:
jjg@46: case ITEM_Object:
jjg@46: return new Object_variable_info(cr);
jjg@46:
jjg@46: case ITEM_Uninitialized:
jjg@46: return new Uninitialized_variable_info(cr);
jjg@46:
jjg@46: default:
jjg@46: throw new InvalidStackMap("unrecognized verification_type_info tag");
jjg@46: }
jjg@46: }
jjg@46:
jjg@283: protected verification_type_info(int tag) {
jjg@46: this.tag = tag;
jjg@46: }
jjg@46:
jjg@46: public int length() {
jjg@46: return 1;
jjg@46: }
jjg@46:
jjg@46: public final int tag;
jjg@46: }
jjg@46:
jjg@46: public static class Object_variable_info extends verification_type_info {
jjg@46: Object_variable_info(ClassReader cr) throws IOException {
jjg@46: super(ITEM_Object);
jjg@46: cpool_index = cr.readUnsignedShort();
jjg@46: }
jjg@46:
jjg@46: @Override
jjg@46: public int length() {
jjg@46: return super.length() + 2;
jjg@46: }
jjg@46:
jjg@46: public final int cpool_index;
jjg@46: }
jjg@46:
jjg@46: public static class Uninitialized_variable_info extends verification_type_info {
jjg@46: Uninitialized_variable_info(ClassReader cr) throws IOException {
jjg@46: super(ITEM_Uninitialized);
jjg@46: offset = cr.readUnsignedShort();
jjg@46: }
jjg@46:
jjg@46: @Override
jjg@46: public int length() {
jjg@46: return super.length() + 2;
jjg@46: }
jjg@46:
jjg@46: public final int offset;
jjg@46:
jjg@46: }
jjg@46: }