1 /*
2 * Copyright (c) 2023, 2025, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.lang.classfile.attribute;
27
28 import java.lang.classfile.ClassFile;
29 import java.lang.classfile.Label;
30 import java.lang.classfile.Opcode;
31 import java.lang.classfile.constantpool.ClassEntry;
32 import java.lang.classfile.instruction.BranchInstruction;
33 import java.lang.constant.ClassDesc;
34 import java.util.List;
35
36 import jdk.internal.classfile.impl.StackMapDecoder;
37 import jdk.internal.classfile.impl.StackMapGenerator;
38 import jdk.internal.classfile.impl.TemporaryConstantPool;
39
40 /**
41 * Models a stack map frame in a {@link StackMapTableAttribute StackMapTable}
42 * attribute (JVMS {@jvms 4.7.4}). A stack map frame must appear at the
43 * beginning of each basic block in a method (JVMS {@jvms 4.10.1}).
44 *
45 * @apiNote
46 * In general, a stack map frame should be defined for each target of a
47 * {@link BranchInstruction}, or unreachable code right after an unconditional
48 * branch instruction like {@link Opcode#GOTO goto}. The automatic stack map
49 * generation cannot handle unreachable code right after an unconditional jump;
50 * The {@link ClassFile.DeadCodeOption} allows substituting such code, or
51 * advanced users can provide their own stack maps for dead code.
52 *
53 * @see StackMapTableAttribute#entries()
54 * @jvms 4.7.4 The {@code StackMapTable} Attribute
55 * @jvms 4.10.1 Verification by Type Checking
56 * @since 24
57 */
58 public sealed interface StackMapFrameInfo
61 /**
62 * {@return the raw {@code u1 frame_type}}
63 */
64 int frameType();
65
66 /**
67 * {@return the frame target label}
68 */
69 Label target();
70
71 /**
72 * {@return the expanded local variable types}
73 */
74 List<VerificationTypeInfo> locals();
75
76 /**
77 * {@return the expanded operand stack types}
78 */
79 List<VerificationTypeInfo> stack();
80
81 /**
82 * {@return a new stack map frame}
83 *
84 * @param target the location of the frame
85 * @param locals the complete list of frame locals
86 * @param stack the complete frame stack
87 * @throws IllegalArgumentException if the number of types in {@code locals}
88 * or {@code stack} exceeds the limit of {@link java.lang.classfile##u2 u2}
89 */
90 public static StackMapFrameInfo of(Label target,
91 List<VerificationTypeInfo> locals,
92 List<VerificationTypeInfo> stack) {
93 return new StackMapDecoder.StackMapFrameImpl(255, target, locals, stack);
94 }
95
96 /**
97 * The type of a stack or local variable value.
98 *
99 * @see #locals()
100 * @see #stack()
101 * @jvms 4.7.4 The {@code StackMapTable} Attribute
102 * @since 24
103 */
104 sealed interface VerificationTypeInfo {
105
106 /** The {@link #tag() tag} for verification type info {@link SimpleVerificationTypeInfo#TOP TOP}. */
107 int ITEM_TOP = StackMapGenerator.ITEM_TOP;
108
109 /** The {@link #tag() tag} for verification type info {@link SimpleVerificationTypeInfo#INTEGER INTEGER}. */
110 int ITEM_INTEGER = StackMapGenerator.ITEM_INTEGER;
111
112 /** The {@link #tag() tag} for verification type info {@link SimpleVerificationTypeInfo#FLOAT FLOAT}. */
113 int ITEM_FLOAT = StackMapGenerator.ITEM_FLOAT;
|
1 /*
2 * Copyright (c) 2023, 2026, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.lang.classfile.attribute;
27
28 import java.lang.classfile.ClassFile;
29 import java.lang.classfile.Label;
30 import java.lang.classfile.Opcode;
31 import java.lang.classfile.constantpool.ClassEntry;
32 import java.lang.classfile.instruction.BranchInstruction;
33 import java.lang.classfile.constantpool.NameAndTypeEntry;
34 import java.lang.constant.ClassDesc;
35 import java.util.List;
36
37 import jdk.internal.classfile.impl.StackMapDecoder;
38 import jdk.internal.classfile.impl.StackMapGenerator;
39 import jdk.internal.classfile.impl.TemporaryConstantPool;
40 import jdk.internal.javac.PreviewFeature;
41
42 /**
43 * Models a stack map frame in a {@link StackMapTableAttribute StackMapTable}
44 * attribute (JVMS {@jvms 4.7.4}). A stack map frame must appear at the
45 * beginning of each basic block in a method (JVMS {@jvms 4.10.1}).
46 *
47 * @apiNote
48 * In general, a stack map frame should be defined for each target of a
49 * {@link BranchInstruction}, or unreachable code right after an unconditional
50 * branch instruction like {@link Opcode#GOTO goto}. The automatic stack map
51 * generation cannot handle unreachable code right after an unconditional jump;
52 * The {@link ClassFile.DeadCodeOption} allows substituting such code, or
53 * advanced users can provide their own stack maps for dead code.
54 *
55 * @see StackMapTableAttribute#entries()
56 * @jvms 4.7.4 The {@code StackMapTable} Attribute
57 * @jvms 4.10.1 Verification by Type Checking
58 * @since 24
59 */
60 public sealed interface StackMapFrameInfo
63 /**
64 * {@return the raw {@code u1 frame_type}}
65 */
66 int frameType();
67
68 /**
69 * {@return the frame target label}
70 */
71 Label target();
72
73 /**
74 * {@return the expanded local variable types}
75 */
76 List<VerificationTypeInfo> locals();
77
78 /**
79 * {@return the expanded operand stack types}
80 */
81 List<VerificationTypeInfo> stack();
82
83 /**
84 * {@return the expanded unset fields}
85 *
86 * @jvms strict-fields-4.7.4 The {@code StackMapTable} Attribute
87 * @since Valhalla
88 */
89 @PreviewFeature(feature = PreviewFeature.Feature.STRICT_FIELDS, reflective = true)
90 List<NameAndTypeEntry> unsetFields();
91
92 /**
93 * {@return a new stack map frame}
94 *
95 * @param target the location of the frame
96 * @param locals the complete list of frame locals
97 * @param stack the complete frame stack
98 * @throws IllegalArgumentException if the number of types in {@code locals}
99 * or {@code stack} exceeds the limit of {@link java.lang.classfile##u2 u2}
100 */
101 public static StackMapFrameInfo of(Label target,
102 List<VerificationTypeInfo> locals,
103 List<VerificationTypeInfo> stack) {
104
105 return of(target, locals, stack, List.of());
106 }
107
108 /**
109 * {@return a new stack map frame}
110 * @param target the location of the frame
111 * @param locals the complete list of frame locals
112 * @param stack the complete frame stack
113 * @param unsetFields the complete list of unset fields
114 * @throws IllegalArgumentException if the number of elements in {@code locals},
115 * {@code stack}, or {@code unsetFields} exceeds the limit of
116 * {@link java.lang.classfile##u2 u2}; or if unset fields has
117 * elements, but no {@link SimpleVerificationTypeInfo#UNINITIALIZED_THIS
118 * uninitializedThis} is present in {@code locals}
119 * @since Valhalla
120 */
121 @PreviewFeature(feature = PreviewFeature.Feature.STRICT_FIELDS, reflective = true)
122 public static StackMapFrameInfo of(Label target,
123 List<VerificationTypeInfo> locals,
124 List<VerificationTypeInfo> stack,
125 List<NameAndTypeEntry> unsetFields) {
126
127 return new StackMapDecoder.StackMapFrameImpl(255, target, locals, stack, unsetFields);
128 }
129
130 /**
131 * The type of a stack or local variable value.
132 *
133 * @see #locals()
134 * @see #stack()
135 * @jvms 4.7.4 The {@code StackMapTable} Attribute
136 * @since 24
137 */
138 sealed interface VerificationTypeInfo {
139
140 /** The {@link #tag() tag} for verification type info {@link SimpleVerificationTypeInfo#TOP TOP}. */
141 int ITEM_TOP = StackMapGenerator.ITEM_TOP;
142
143 /** The {@link #tag() tag} for verification type info {@link SimpleVerificationTypeInfo#INTEGER INTEGER}. */
144 int ITEM_INTEGER = StackMapGenerator.ITEM_INTEGER;
145
146 /** The {@link #tag() tag} for verification type info {@link SimpleVerificationTypeInfo#FLOAT FLOAT}. */
147 int ITEM_FLOAT = StackMapGenerator.ITEM_FLOAT;
|