aboutsummaryrefslogtreecommitdiff
path: root/src/share/classes/com/sun/tools/classfile/StackMapTable_attribute.java
blob: 13c43e423641eeacfc930232f2a8925d19396e6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
 * Copyright (c) 2007, 2009, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package com.sun.tools.classfile;

import java.io.IOException;

/**
 * See JVMS, section 4.8.4.
 *
 *  <p><b>This is NOT part of any supported API.
 *  If you write code that depends on this, you do so at your own risk.
 *  This code and its internal interfaces are subject to change or
 *  deletion without notice.</b>
 */
public class StackMapTable_attribute extends Attribute {
    static class InvalidStackMap extends AttributeException {
        private static final long serialVersionUID = -5659038410855089780L;
        InvalidStackMap(String msg) {
            super(msg);
        }
    }

    StackMapTable_attribute(ClassReader cr, int name_index, int length)
            throws IOException, InvalidStackMap {
        super(name_index, length);
        number_of_entries = cr.readUnsignedShort();
        entries = new stack_map_frame[number_of_entries];
        for (int i = 0; i < number_of_entries; i++)
            entries[i] = stack_map_frame.read(cr);
    }

    public StackMapTable_attribute(ConstantPool constant_pool, stack_map_frame[] entries)
            throws ConstantPoolException {
        this(constant_pool.getUTF8Index(Attribute.StackMapTable), entries);
    }

    public StackMapTable_attribute(int name_index, stack_map_frame[] entries) {
        super(name_index, length(entries));
        this.number_of_entries = entries.length;
        this.entries = entries;
    }

    public <R, D> R accept(Visitor<R, D> visitor, D data) {
        return visitor.visitStackMapTable(this, data);
    }

    static int length(stack_map_frame[] entries) {
        int n = 2;
        for (stack_map_frame entry: entries)
            n += entry.length();
        return n;
    }

    public final int number_of_entries;
    public final stack_map_frame entries[];

    public static abstract class stack_map_frame {
        static stack_map_frame read(ClassReader cr)
                throws IOException, InvalidStackMap {
            int frame_type = cr.readUnsignedByte();
            if (frame_type <= 63)
                return new same_frame(frame_type);
            else if (frame_type <= 127)
                return new same_locals_1_stack_item_frame(frame_type, cr);
            else if (frame_type <= 246)
                throw new Error("unknown frame_type " + frame_type);
            else if (frame_type == 247)
                return new same_locals_1_stack_item_frame_extended(frame_type, cr);
            else if (frame_type <= 250)
                return new chop_frame(frame_type, cr);
            else if (frame_type == 251)
                return new same_frame_extended(frame_type, cr);
            else if (frame_type <= 254)
                return new append_frame(frame_type, cr);
            else
                return new full_frame(frame_type, cr);
        }

        protected stack_map_frame(int frame_type) {
            this.frame_type = frame_type;
        }

        public int length() {
            return 1;
        }

        public abstract int getOffsetDelta();

        public abstract <R,D> R accept(Visitor<R,D> visitor, D data);

        public final int frame_type;

        public static interface Visitor<R,P> {
            R visit_same_frame(same_frame frame, P p);
            R visit_same_locals_1_stack_item_frame(same_locals_1_stack_item_frame frame, P p);
            R visit_same_locals_1_stack_item_frame_extended(same_locals_1_stack_item_frame_extended frame, P p);
            R visit_chop_frame(chop_frame frame, P p);
            R visit_same_frame_extended(same_frame_extended frame, P p);
            R visit_append_frame(append_frame frame, P p);
            R visit_full_frame(full_frame frame, P p);
        }
    }

    public static class same_frame extends stack_map_frame {
        same_frame(int frame_type) {
            super(frame_type);
        }

        public <R, D> R accept(Visitor<R, D> visitor, D data) {
            return visitor.visit_same_frame(this, data);
        }

        public int getOffsetDelta() {
            return frame_type;
        }
    }

    public static class same_locals_1_stack_item_frame extends stack_map_frame {
        same_locals_1_stack_item_frame(int frame_type, ClassReader cr)
                throws IOException, InvalidStackMap {
            super(frame_type);
            stack = new verification_type_info[1];
            stack[0] = verification_type_info.read(cr);
        }

        @Override
        public int length() {
            return super.length() + stack[0].length();
        }

        public <R, D> R accept(Visitor<R, D> visitor, D data) {
            return visitor.visit_same_locals_1_stack_item_frame(this, data);
        }

        public int getOffsetDelta() {
            return frame_type - 64;
        }

        public final verification_type_info[] stack;
    }

    public static class same_locals_1_stack_item_frame_extended extends stack_map_frame {
        same_locals_1_stack_item_frame_extended(int frame_type, ClassReader cr)
                throws IOException, InvalidStackMap {
            super(frame_type);
            offset_delta = cr.readUnsignedShort();
            stack = new verification_type_info[1];
            stack[0] = verification_type_info.read(cr);
        }

        @Override
        public int length() {
            return super.length() + 2 + stack[0].length();
        }

        public <R, D> R accept(Visitor<R, D> visitor, D data) {
            return visitor.visit_same_locals_1_stack_item_frame_extended(this, data);
        }

        public int getOffsetDelta() {
            return offset_delta;
        }

        public final int offset_delta;
        public final verification_type_info[] stack;
    }

    public static class chop_frame extends stack_map_frame {
        chop_frame(int frame_type, ClassReader cr) throws IOException {
            super(frame_type);
            offset_delta = cr.readUnsignedShort();
        }

        @Override
        public int length() {
            return super.length() + 2;
        }

        public <R, D> R accept(Visitor<R, D> visitor, D data) {
            return visitor.visit_chop_frame(this, data);
        }

        public int getOffsetDelta() {
            return offset_delta;
        }

        public final int offset_delta;
    }

    public static class same_frame_extended extends stack_map_frame {
        same_frame_extended(int frame_type, ClassReader cr) throws IOException {
            super(frame_type);
            offset_delta = cr.readUnsignedShort();
        }

        @Override
        public int length() {
            return super.length() + 2;
        }

        public <R, D> R accept(Visitor<R, D> visitor, D data) {
            return visitor.visit_same_frame_extended(this, data);
        }

        public int getOffsetDelta() {
            return offset_delta;
        }

        public final int offset_delta;
    }

    public static class append_frame extends stack_map_frame {
        append_frame(int frame_type, ClassReader cr)
                throws IOException, InvalidStackMap {
            super(frame_type);
            offset_delta = cr.readUnsignedShort();
            locals = new verification_type_info[frame_type - 251];
            for (int i = 0; i < locals.length; i++)
                locals[i] = verification_type_info.read(cr);
        }

        @Override
        public int length() {
            int n = super.length() + 2;
            for (verification_type_info local: locals)
                n += local.length();
            return n;
        }

        public <R, D> R accept(Visitor<R, D> visitor, D data) {
            return visitor.visit_append_frame(this, data);
        }

        public int getOffsetDelta() {
            return offset_delta;
        }

        public final int offset_delta;
        public final verification_type_info[] locals;
    }

    public static class full_frame extends stack_map_frame {
        full_frame(int frame_type, ClassReader cr)
                throws IOException, InvalidStackMap {
            super(frame_type);
            offset_delta = cr.readUnsignedShort();
            number_of_locals = cr.readUnsignedShort();
            locals = new verification_type_info[number_of_locals];
            for (int i = 0; i < locals.length; i++)
                locals[i] = verification_type_info.read(cr);
            number_of_stack_items = cr.readUnsignedShort();
            stack = new verification_type_info[number_of_stack_items];
            for (int i = 0; i < stack.length; i++)
                stack[i] = verification_type_info.read(cr);
        }

        @Override
        public int length() {
            int n = super.length() + 2;
            for (verification_type_info local: locals)
                n += local.length();
            n += 2;
            for (verification_type_info item: stack)
                n += item.length();
            return n;
        }

        public <R, D> R accept(Visitor<R, D> visitor, D data) {
            return visitor.visit_full_frame(this, data);
        }

        public int getOffsetDelta() {
            return offset_delta;
        }

        public final int offset_delta;
        public final int number_of_locals;
        public final verification_type_info[] locals;
        public final int number_of_stack_items;
        public final verification_type_info[] stack;
    }

    public static class verification_type_info {
        public static final int ITEM_Top = 0;
        public static final int ITEM_Integer = 1;
        public static final int ITEM_Float = 2;
        public static final int ITEM_Long = 4;
        public static final int ITEM_Double = 3;
        public static final int ITEM_Null = 5;
        public static final int ITEM_UninitializedThis = 6;
        public static final int ITEM_Object = 7;
        public static final int ITEM_Uninitialized = 8;

        static verification_type_info read(ClassReader cr)
                throws IOException, InvalidStackMap {
            int tag = cr.readUnsignedByte();
            switch (tag) {
            case ITEM_Top:
            case ITEM_Integer:
            case ITEM_Float:
            case ITEM_Long:
            case ITEM_Double:
            case ITEM_Null:
            case ITEM_UninitializedThis:
                return new verification_type_info(tag);

            case ITEM_Object:
                return new Object_variable_info(cr);

            case ITEM_Uninitialized:
                return new Uninitialized_variable_info(cr);

            default:
                throw new InvalidStackMap("unrecognized verification_type_info tag");
            }
        }

        protected verification_type_info(int tag) {
            this.tag = tag;
        }

        public int length() {
            return 1;
        }

        public final int tag;
    }

    public static class Object_variable_info extends verification_type_info {
        Object_variable_info(ClassReader cr) throws IOException {
            super(ITEM_Object);
            cpool_index = cr.readUnsignedShort();
        }

        @Override
        public int length() {
            return super.length() + 2;
        }

        public final int cpool_index;
    }

    public static class Uninitialized_variable_info extends verification_type_info {
        Uninitialized_variable_info(ClassReader cr) throws IOException {
            super(ITEM_Uninitialized);
            offset = cr.readUnsignedShort();
        }

        @Override
        public int length() {
            return super.length() + 2;
        }

        public final int offset;

    }
}