The StackMapTable attribute is a variable-length attribute in the attributes table of a Code attribute (§4.7.3). A StackMapTable attribute is used during the process of verification by type checking (§4.10.1).
There may be at most one StackMapTable attribute in the attributes table of a Code attribute.
In a class file whose version number is 50.0 or above, if a method's Code attribute does not have a StackMapTable attribute, it has an implicit stack map attribute (§4.10.1). This implicit stack map attribute is equivalent to a StackMapTable attribute with number_of_entries equal to zero.
The StackMapTable attribute has the following format:
StackMapTable_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 number_of_entries;
stack_map_frame entries[number_of_entries];
}
The items of the StackMapTable_attribute structure are as follows:
-
attribute_name_index
The value of the
attribute_name_indexitem must be a valid index into theconstant_pooltable. Theconstant_poolentry at that index must be aCONSTANT_Utf8_infostructure (§4.4.7) representing the string "StackMapTable". -
attribute_length
The value of the
attribute_lengthitem indicates the length of the attribute, excluding the initial six bytes. -
number_of_entries
The value of the
number_of_entriesitem gives the number ofstack_map_frameentries in theentriestable. -
entries[]
Each entry in the
entriestable describes one stack map frame of the method. The order of the stack map frames in theentriestable is significant.
A stack map frame specifies (either explicitly or implicitly) the bytecode offset at which it applies, and the verification types of local variables and operand stack entries for that offset.
Each stack map frame described in the entries table relies on the previous frame for some of its semantics. The first stack map frame of a method is implicit, and computed from the method descriptor by the type checker (§4.10.1.5). The stack_map_frame structure at entries[0] therefore describes the second stack map frame of the method.
The bytecode offset at which a stack map frame applies is calculated by taking the value offset_delta specified in the frame (either explicitly or implicitly), and adding offset_delta + 1 to the bytecode offset of the previous frame, unless the previous frame is the initial frame of the method. In that case, the bytecode offset at which the stack map frame applies is the value offset_delta specified in the frame.
By using an offset delta rather than storing the actual bytecode offset, we ensure, by definition, that stack map frames are in the correctly sorted order. Furthermore, by consistently using the formula offset_delta + 1 for all explicit frames (as opposed to the implicit first frame), we guarantee the absence of duplicates.
We say that an instruction in the bytecode has a corresponding stack map frame if the instruction starts at offset i in the code array of a Code attribute, and the Code attribute has a StackMapTable attribute whose entries array contains a stack map frame that applies at bytecode offset i.
A verification type specifies the type of either one or two locations, where a location is either a single local variable or a single operand stack entry. A verification type is represented by a discriminated union, verification_type_info, that consists of a one-byte tag, indicating which item of the union is in use, followed by zero or more bytes, giving more information about the tag.
union verification_type_info {
Top_variable_info;
Integer_variable_info;
Float_variable_info;
Long_variable_info;
Double_variable_info;
Null_variable_info;
UninitializedThis_variable_info;
Object_variable_info;
Uninitialized_variable_info;
}
A verification type that specifies one location in the local variable array or in the operand stack is represented by the following items of the verification_type_info union:
-
The
Top_variable_infoitem indicates that the local variable has the verification typetop.Top_variable_info { u1 tag = ITEM_Top; /* 0 */ } -
The
Integer_variable_infoitem indicates that the location has the verification typeint.Integer_variable_info { u1 tag = ITEM_Integer; /* 1 */ } -
The
Float_variable_infoitem indicates that the location has the verification typefloat.Float_variable_info { u1 tag = ITEM_Float; /* 2 */ } -
The
Null_variable_infotype indicates that the location has the verification typenull.Null_variable_info { u1 tag = ITEM_Null; /* 5 */ } -
The
UninitializedThis_variable_infoitem indicates that the location has the verification typeuninitializedThis.UninitializedThis_variable_info { u1 tag = ITEM_UninitializedThis; /* 6 */ } -
The
Object_variable_infoitem indicates that the location has the verification type which is the class represented by theCONSTANT_Class_infostructure (§4.4.1) found in theconstant_pooltable at the index given bycpool_index.Object_variable_info { u1 tag = ITEM_Object; /* 7 */ u2 cpool_index; } -
The
Uninitialized_variable_infoitem indicates that the location has the verification typeuninitialized(Offset). TheOffsetitem indicates the offset, in thecodearray of theCodeattribute that contains thisStackMapTableattribute, of the new instruction (§new) that created the object being stored in the location.Uninitialized_variable_info { u1 tag = ITEM_Uninitialized; /* 8 */ u2 offset; }
A verification type that specifies two locations in the local variable array or in the operand stack is represented by the following items of the verification_type_info union:
-
The
Long_variable_infoitem indicates that the first of two locations has the verification typelong.Long_variable_info { u1 tag = ITEM_Long; /* 4 */ } -
The
Double_variable_infoitem indicates that the first of two locations has the verification typedouble.Double_variable_info { u1 tag = ITEM_Double; /* 3 */ } -
The
Long_variable_infoandDouble_variable_infoitems indicate the verification type of the second of two locations as follows:-
If the first of the two locations is a local variable, then:
- It must not be the local variable with the highest index.
- The next higher numbered local variable has the verification type
top.
-
If the first of the two locations is an operand stack entry, then:
- It must not be the topmost location of the operand stack.
- The next location closer to the top of the operand stack has the verification type
top.
-
A stack map frame is represented by a discriminated union, stack_map_frame, which consists of a one-byte tag, indicating which item of the union is in use, followed by zero or more bytes, giving more information about the tag.
union stack_map_frame {
same_frame;
same_locals_1_stack_item_frame;
same_locals_1_stack_item_frame_extended;
chop_frame;
same_frame_extended;
append_frame;
full_frame;
}
The tag indicates the frame type of the stack map frame:
-
The frame type
same_frameis represented by tags in the range [0-63]. This frame type indicates that the frame has exactly the same local variables as the previous frame and that the operand stack is empty. Theoffset_deltavalue for the frame is the value of the tag item,frame_type.same_frame { u1 frame_type = SAME; /* 0-63 */ } -
The frame type
same_locals_1_stack_item_frameis represented by tags in the range [64, 127]. This frame type indicates that the frame has exactly the same local variables as the previous frame and that the operand stack has one entry. Theoffset_deltavalue for the frame is given by the formulaframe_type - 64. The verification type of the one stack entry appears after the frame type.same_locals_1_stack_item_frame { u1 frame_type = SAME_LOCALS_1_STACK_ITEM; /* 64-127 */ verification_type_info stack[1]; } -
Tags in the range [128-246] are reserved for future use.
-
The frame type
same_locals_1_stack_item_frame_extendedis represented by the tag 247. This frame type indicates that the frame has exactly the same local variables as the previous frame and that the operand stack has one entry. Theoffset_deltavalue for the frame is given explicitly, unlike in the frame typesame_locals_1_stack_item_frame. The verification type of the one stack entry appears afteroffset_delta.same_locals_1_stack_item_frame_extended { u1 frame_type = SAME_LOCALS_1_STACK_ITEM_EXTENDED; /* 247 */ u2 offset_delta; verification_type_info stack[1]; } -
The frame type
chop_frameis represented by tags in the range [248-250]. This frame type indicates that the frame has the same local variables as the previous frame except that the last k local variables are absent, and that the operand stack is empty. The value of k is given by the formula251 - frame_type. Theoffset_deltavalue for the frame is given explicitly.chop_frame { u1 frame_type = CHOP; /* 248-250 */ u2 offset_delta; }Assume the verification types of local variables in the previous frame are given by
locals, an array structured as in thefull_frameframe type. Iflocals[M-1]in the previous frame represented local variable X andlocals[M]represented local variable Y, then the effect of removing one local variable is thatlocals[M-1]in the new frame represents local variable X andlocals[M]is undefined.It is an error if k is larger than the number of local variables in
localsfor the previous frame, that is, if the number of local variables in the new frame would be less than zero. -
The frame type
same_frame_extendedis represented by the tag 251. This frame type indicates that the frame has exactly the same local variables as the previous frame and that the operand stack is empty. Theoffset_deltavalue for the frame is given explicitly, unlike in the frame typesame_frame.same_frame_extended { u1 frame_type = SAME_FRAME_EXTENDED; /* 251 */ u2 offset_delta; } -
The frame type
append_frameis represented by tags in the range [252-254]. This frame type indicates that the frame has the same locals as the previous frame except that k additional locals are defined, and that the operand stack is empty. The value of k is given by the formulaframe_type - 251. Theoffset_deltavalue for the frame is given explicitly.append_frame { u1 frame_type = APPEND; /* 252-254 */ u2 offset_delta; verification_type_info locals[frame_type - 251]; }The 0th entry in
localsrepresents the verification type of the first additional local variable. Iflocals[M]represents local variableN, then:locals[M+1]represents local variableN+1iflocals[M]is one ofTop_variable_info,Integer_variable_info,Float_variable_info,Null_variable_info,UninitializedThis_variable_info,Object_variable_info, orUninitialized_variable_info; andlocals[M+1]represents local variableN+2iflocals[M]is eitherLong_variable_infoorDouble_variable_info.
It is an error if, for any index i,
locals[i]represents a local variable whose index is greater than the maximum number of local variables for the method. -
The frame type
full_frameis represented by the tag 255. Theoffset_deltavalue for the frame is given explicitly.full_frame { u1 frame_type = FULL_FRAME; /* 255 */ u2 offset_delta; u2 number_of_locals; verification_type_info locals[number_of_locals]; u2 number_of_stack_items; verification_type_info stack[number_of_stack_items]; }The 0th entry in
localsrepresents the verification type of local variable 0. Iflocals[M]represents local variableN, then:locals[M+1]represents local variableN+1iflocals[M]is one ofTop_variable_info,Integer_variable_info,Float_variable_info,Null_variable_info,UninitializedThis_variable_info,Object_variable_info, orUninitialized_variable_info; andlocals[M+1]represents local variableN+2iflocals[M]is eitherLong_variable_infoorDouble_variable_info.
It is an error if, for any index i,
locals[i]represents a local variable whose index is greater than the maximum number of local variables for the method.The 0th entry in
stackrepresents the verification type of the bottom of the operand stack, and subsequent entries instackrepresent the verification types of stack entries closer to the top of the operand stack. We refer to the bottom of the operand stack as stack entry 0, and to subsequent entries of the operand stack as stack entry 1, 2, etc. Ifstack[M]represents stack entryN, then:stack[M+1]represents stack entryN+1ifstack[M]is one ofTop_variable_info,Integer_variable_info,Float_variable_info,Null_variable_info,UninitializedThis_variable_info,Object_variable_info, orUninitialized_variable_info; andstack[M+1]represents stack entryN+2ifstack[M]is eitherLong_variable_infoorDouble_variable_info.
It is an error if, for any index i,
stack[i]represents a stack entry whose index is greater than the maximum operand stack size for the method.