java_asm/jvms/attr/
mod.rs

1use java_asm_macro::{ReadFrom, WriteInto};
2
3use crate::jvms::attr::annotation::{AnnotationElementValueInfo, AnnotationInfo, ParameterAnnotationInfo};
4use crate::jvms::attr::module::{ModuleExports, ModuleOpens, ModuleProvides, ModuleRequires};
5use crate::jvms::attr::type_annotation::TypeAnnotation;
6use crate::jvms::element::AttributeInfo;
7use crate::node::element::LabelNode;
8
9pub mod annotation;
10pub mod module;
11pub mod type_annotation;
12
13#[derive(Clone, Debug, WriteInto)]
14pub enum Attribute {
15    Custom(Vec<u8>),
16    // ConstantValue_attribute {
17    //     u2 attribute_name_index; // CONSTANT_Utf8_info
18    //     u4 attribute_length;
19    //     u2 constantvalue_index; // index of const pool
20    // }
21    ConstantValue { constantvalue_index: u16 },
22    // Code_attribute {
23    //     u2 attribute_name_index;
24    //     u4 attribute_length;
25    //     u2 max_stack;
26    //     u2 max_locals;
27    //     u4 code_length;
28    //     u1 code[code_length];
29    //     u2 exception_table_length;
30    //     {   u2 start_pc;
31    //         u2 end_pc;
32    //         u2 handler_pc;
33    //         u2 catch_type;
34    //     } exception_table[exception_table_length];
35    //     u2 attributes_count;
36    //     attribute_info attributes[attributes_count];
37    // }
38    Code {
39        max_stack: u16,
40        max_locals: u16,
41        code_length: u32,
42        code: Vec<u8>,
43        exception_table_length: u16,
44        exception_table: Vec<ExceptionTable>,
45        attributes_count: u16,
46        attributes: Vec<AttributeInfo>,
47    },
48    // StackMapTable_attribute {
49    //     u2              attribute_name_index;
50    //     u4              attribute_length;
51    //     u2              number_of_entries;
52    //     stack_map_frame entries[number_of_entries];
53    // }
54    StackMapTable {
55        number_of_entries: u16,
56        entries: Vec<StackMapFrame>,
57    },
58    // Exceptions_attribute {
59    //     u2 attribute_name_index;
60    //     u4 attribute_length;
61    //     u2 number_of_exceptions;
62    //     u2 exception_index_table[number_of_exceptions]; // CONSTANT_Class_info
63    // }
64    Exceptions {
65        number_of_exceptions: u16,
66        exception_index_table: Vec<u16>,
67    },
68    // InnerClasses_attribute {
69    //     u2 attribute_name_index;
70    //     u4 attribute_length;
71    //     u2 number_of_classes;
72    //     {   u2 inner_class_info_index;
73    //         u2 outer_class_info_index;
74    //         u2 inner_name_index;
75    //         u2 inner_class_access_flags;
76    //     } classes[number_of_classes];
77    // }
78    InnerClasses {
79        number_of_classes: u16,
80        classes: Vec<InnerClassInfo>,
81    },
82    // EnclosingMethod_attribute {
83    //     u2 attribute_name_index;
84    //     u4 attribute_length;
85    //     u2 class_index; // CONSTANT_Class_info
86    //     u2 method_index; // CONSTANT_NameAndType_info or zero
87    // }
88    EnclosingMethod {
89        class_index: u16,
90        method_index: u16,
91    },
92    // Synthetic_attribute {
93    //     u2 attribute_name_index;
94    //     u4 attribute_length;
95    // }
96    Synthetic,
97    // Signature_attribute {
98    //     u2 attribute_name_index;
99    //     u4 attribute_length;
100    //     u2 signature_index; // CONSTANT_Utf8_info
101    // }
102    Signature {
103        signature_index: u16,
104    },
105    // SourceFile_attribute {
106    //     u2 attribute_name_index;
107    //     u4 attribute_length;
108    //     u2 sourcefile_index; // CONSTANT_Utf8_info
109    // }
110    SourceFile {
111        sourcefile_index: u16,
112    },
113    // SourceDebugExtension_attribute {
114    //     u2 attribute_name_index;
115    //     u4 attribute_length;
116    //     u1 debug_extension[attribute_length];
117    // }
118    SourceDebugExtension {
119        debug_extension: Vec<u8>,
120    },
121    // LineNumberTable_attribute {
122    //     u2 attribute_name_index;
123    //     u4 attribute_length;
124    //     u2 line_number_table_length;
125    //     {   u2 start_pc;
126    //         u2 line_number;
127    //     } line_number_table[line_number_table_length];
128    // }
129    LineNumberTable {
130        line_number_table_length: u16,
131        line_number_table: Vec<LineNumberTableInfo>,
132    },
133    // LocalVariableTable_attribute {
134    //     u2 attribute_name_index;
135    //     u4 attribute_length;
136    //     u2 local_variable_table_length;
137    //     {   u2 start_pc;
138    //         u2 length;
139    //         u2 name_index;
140    //         u2 descriptor_index;
141    //         u2 index;
142    //     } local_variable_table[local_variable_table_length];
143    // }
144    LocalVariableTable {
145        local_variable_table_length: u16,
146        local_variable_table: Vec<LocalVariableTableInfo>,
147    },
148    // LocalVariableTypeTable_attribute {
149    //     u2 attribute_name_index;
150    //     u4 attribute_length;
151    //     u2 local_variable_type_table_length;
152    //     {   u2 start_pc;
153    //         u2 length;
154    //         u2 name_index;
155    //         u2 signature_index;
156    //         u2 index;
157    //     } local_variable_type_table[local_variable_type_table_length];
158    // }
159    LocalVariableTypeTable {
160        local_variable_type_table_length: u16,
161        local_variable_table: Vec<LocalVariableTypeTableInfo>,
162    },
163    // Deprecated_attribute {
164    //     u2 attribute_name_index;
165    //     u4 attribute_length;
166    // }
167    Deprecated,
168    // RuntimeVisibleAnnotations_attribute {
169    //     u2         attribute_name_index;
170    //     u4         attribute_length;
171    //     u2         num_annotations;
172    //     annotation annotations[num_annotations];
173    // }
174    RuntimeVisibleAnnotations {
175        num_annotations: u16,
176        annotations: Vec<AnnotationInfo>,
177    },
178    RuntimeInvisibleAnnotations {
179        num_annotations: u16,
180        annotations: Vec<AnnotationInfo>,
181    },
182    RuntimeVisibleParameterAnnotations {
183        num_parameters: u8,
184        parameter_annotations: Vec<ParameterAnnotationInfo>,
185    },
186    RuntimeInvisibleParameterAnnotations {
187        num_parameters: u8,
188        parameter_annotations: Vec<ParameterAnnotationInfo>,
189    },
190    RuntimeVisibleTypeAnnotations {
191        num_parameters: u16,
192        annotations: Vec<TypeAnnotation>,
193    },
194    RuntimeInvisibleTypeAnnotations {
195        num_parameters: u16,
196        annotations: Vec<TypeAnnotation>,
197    },
198    AnnotationDefault {
199        default_value: AnnotationElementValueInfo,
200    },
201    // BootstrapMethods_attribute {
202    //     u2 attribute_name_index;
203    //     u4 attribute_length;
204    //     u2 num_bootstrap_methods;
205    //     {   u2 bootstrap_method_ref;
206    //         u2 num_bootstrap_arguments;
207    //         u2 bootstrap_arguments[num_bootstrap_arguments];
208    //     } bootstrap_methods[num_bootstrap_methods];
209    // }
210    BootstrapMethods {
211        num_bootstrap_methods: u16,
212        bootstrap_methods: Vec<BootstrapMethod>,
213    },
214    // MethodParameters_attribute {
215    //     u2 attribute_name_index;
216    //     u4 attribute_length;
217    //     u1 parameters_count;
218    //     {   u2 name_index;
219    //         u2 access_flags;
220    //     } parameters[parameters_count];
221    // }
222    MethodParameters {
223        parameters_count: u8,
224        parameters: Vec<MethodParameter>,
225    },
226    // Module_attribute {
227    //     u2 attribute_name_index;
228    //     u4 attribute_length;
229    //
230    //     u2 module_name_index; // CONSTANT_Module_info
231    //     u2 module_flags;
232    //     u2 module_version_index; // CONSTANT_Utf8_info
233    //
234    //     u2 requires_count;
235    //     {   u2 requires_index; // CONSTANT_Module_info
236    //         u2 requires_flags;
237    //         u2 requires_version_index; // CONSTANT_Utf8_info
238    //     } requires[requires_count];
239    //
240    //     u2 exports_count;
241    //     {   u2 exports_index; // CONSTANT_Package_info
242    //         u2 exports_flags;
243    //         u2 exports_to_count;
244    //         u2 exports_to_index[exports_to_count]; // CONSTANT_Module_info
245    //     } exports[exports_count];
246    //
247    //     u2 opens_count;
248    //     {   u2 opens_index; // CONSTANT_Package_info
249    //         u2 opens_flags;
250    //         u2 opens_to_count;
251    //         u2 opens_to_index[opens_to_count]; // CONSTANT_Module_info
252    //     } opens[opens_count];
253    //
254    //     u2 uses_count;
255    //     u2 uses_index[uses_count]; // CONSTANT_Class_info
256    //
257    //     u2 provides_count;
258    //     {   u2 provides_index; // CONSTANT_Class_info
259    //         u2 provides_with_count;
260    //         u2 provides_with_index[provides_with_count]; // CONSTANT_Class_info
261    //     } provides[provides_count];
262    // }
263    Module {
264        module_name_index: u16,
265        module_flags: u16,
266        module_version_index: u16,
267        requires_count: u16,
268        requires: Vec<ModuleRequires>,
269        exports_count: u16,
270        exports: Vec<ModuleExports>,
271        opens_count: u16,
272        opens: Vec<ModuleOpens>,
273        uses_count: u16,
274        uses_index: Vec<u16>,
275        provides_count: u16,
276        provides: Vec<ModuleProvides>,
277    },
278    // ModulePackages_attribute {
279    //     u2 attribute_name_index;
280    //     u4 attribute_length;
281    //     u2 package_count;
282    //     u2 package_index[package_count]; // CONSTANT_Package_info
283    // }
284    ModulePackages {
285        package_count: u16,
286        package_index: Vec<u16>,
287    },
288    // ModuleMainClass_attribute {
289    //     u2 attribute_name_index;
290    //     u4 attribute_length;
291    //     u2 main_class_index; // CONSTANT_Class_info
292    // }
293    ModuleMainClass {
294        main_class_index: u16,
295    },
296    // NestHost_attribute {
297    //     u2 attribute_name_index;
298    //     u4 attribute_length;
299    //     u2 host_class_index; // CONSTANT_Class_info
300    // }
301    NestHost {
302        host_class_index: u16,
303    },
304    // NestMembers_attribute {
305    //     u2 attribute_name_index;
306    //     u4 attribute_length;
307    //     u2 number_of_classes;
308    //     u2 classes[number_of_classes]; // CONSTANT_Class_info
309    // }
310    NestMembers {
311        number_of_classes: u16,
312        classes: Vec<u16>,
313    },
314    // Record_attribute {
315    //     u2                    attribute_name_index;
316    //     u4                    attribute_length;
317    //     u2                    components_count;
318    //     record_component_info components[components_count];
319    // }
320    Record {
321        components_count: u16,
322        components: Vec<RecordComponentInfo>,
323    },
324    // PermittedSubclasses_attribute {
325    //     u2 attribute_name_index;
326    //     u4 attribute_length;
327    //     u2 number_of_classes;
328    //     u2 classes[number_of_classes]; // CONSTANT_Class_info
329    // }
330    PermittedSubclasses {
331        number_of_classes: u16,
332        classes: Vec<u16>,
333    },
334}
335
336// {
337//     u2 inner_class_info_index; // CONSTANT_Class_info
338//     u2 outer_class_info_index; // CONSTANT_Class_info
339//     u2 inner_name_index; // CONSTANT_Utf8_info
340//     u2 inner_class_access_flags;
341// }
342#[derive(Clone, Copy, Debug, ReadFrom, WriteInto)]
343pub struct InnerClassInfo {
344    pub inner_class_info_index: u16,
345    pub outer_class_info_index: u16,
346    pub inner_name_index: u16,
347    pub inner_class_access_flags: u16,
348}
349
350// {
351//     u2 start_pc;
352//     u2 line_number;
353// }
354#[derive(Clone, Copy, Debug, ReadFrom, WriteInto)]
355pub struct LineNumberTableInfo {
356    pub start_pc: LabelNode,
357    pub line_number: u16,
358}
359
360// {
361//     u2 start_pc;
362//     u2 length;
363//     u2 name_index; // CONSTANT_Utf8_info
364//     u2 descriptor_index; // CONSTANT_Utf8_info
365//     u2 index;
366// }
367#[derive(Clone, Copy, Debug, ReadFrom, WriteInto)]
368pub struct LocalVariableTableInfo {
369    pub start_pc: LabelNode,
370    pub length: u16,
371    pub name_index: u16,
372    pub descriptor_index: u16,
373    // If the given local variable is of type double or long, it occupies both index and index + 1.
374    pub index: u16,
375}
376
377// {
378//     u2 start_pc;
379//     u2 length;
380//     u2 name_index; // CONSTANT_Utf8_info
381//     u2 signature_index; // CONSTANT_Utf8_info
382//     u2 index;
383// }
384#[derive(Clone, Copy, Debug, ReadFrom, WriteInto)]
385pub struct LocalVariableTypeTableInfo {
386    pub start_pc: LabelNode,
387    pub length: u16,
388    pub name_index: u16,
389    pub signature_index: u16,
390    // If the given local variable is of type double or long, it occupies both index and index + 1.
391    pub index: u16,
392}
393
394// union verification_type_info {
395//     Top_variable_info; // ITEM_Top
396//     Integer_variable_info; // ITEM_Integer
397//     Float_variable_info; // ITEM_Float
398//     Long_variable_info; // ITEM_Long
399//     Double_variable_info; // ITEM_Double
400//     Null_variable_info; // ITEM_Null
401//     UninitializedThis_variable_info; // ITEM_UninitializedThis
402//     Object_variable_info; // ITEM_Object CONSTANT_Class_info
403//     Uninitialized_variable_info; // ITEM_Uninitialized
404// }
405#[derive(Clone, Copy, Debug, WriteInto)]
406pub enum VerificationTypeInfo {
407    Top { tag: u8 },
408    Integer { tag: u8 },
409    Float { tag: u8 },
410    Null { tag: u8 },
411    UninitializedThis { tag: u8 },
412    Object { tag: u8, cpool_index: u16 },
413    Uninitialized { tag: u8, offset: u16 },
414    Long { tag: u8 },
415    Double { tag: u8 },
416}
417
418// union stack_map_frame {
419//     same_frame; // SAME; /* 0-63 */
420//     same_locals_1_stack_item_frame; // SAME_LOCALS_1_STACK_ITEM; /* 64-127 */
421//     same_locals_1_stack_item_frame_extended; // SAME_LOCALS_1_STACK_ITEM_EXTENDED; /* 247 */
422//     chop_frame; // CHOP; /* 248-250 */
423//     same_frame_extended; // SAME_FRAME_EXTENDED; /* 251 */
424//     append_frame; // APPEND; /* 252-254 */
425//     full_frame; // FULL_FRAME; /* 255 */
426// }
427#[derive(Clone, Debug, WriteInto)]
428pub enum StackMapFrame {
429    SameFrame { frame_type: u8 },
430    SameFrameExtended { frame_type: u8, offset_delta: u16 },
431    SameLocals1StackItemFrame {
432        frame_type: u8,
433        verification_type_info: VerificationTypeInfo,
434    },
435    SameLocals1StackItemFrameExtended {
436        frame_type: u8,
437        offset_delta: u16,
438        verification_type_info: VerificationTypeInfo,
439    },
440    ChopFrame { frame_type: u8, offset_delta: u16 },
441    AppendFrame {
442        frame_type: u8,
443        offset_delta: u16,
444        locals: Vec<VerificationTypeInfo>,
445    },
446    FullFrame {
447        frame_type: u8,
448        offset_delta: u16,
449        number_of_locals: u16,
450        locals: Vec<VerificationTypeInfo>,
451        number_of_stack_items: u16,
452        stack: Vec<VerificationTypeInfo>,
453    },
454}
455
456// ExceptionTable {
457//     u2 start_pc;
458//     u2 end_pc;
459//     u2 handler_pc;
460//     u2 catch_type; // CONSTANT_Class_info
461// }
462#[derive(Clone, Copy, Debug, ReadFrom, WriteInto)]
463pub struct ExceptionTable {
464    pub start_pc: LabelNode,
465    pub end_pc: LabelNode,
466    pub handler_pc: LabelNode,
467    pub catch_type: u16,
468}
469
470// {
471//     u2 bootstrap_method_ref; // CONSTANT_MethodHandle_info
472//     u2 num_bootstrap_arguments;
473//     u2 bootstrap_arguments[num_bootstrap_arguments];  // valid index in const_pool
474// }
475#[derive(Clone, Debug, ReadFrom, WriteInto)]
476pub struct BootstrapMethod {
477    pub bootstrap_method_ref: u16,
478    pub num_bootstrap_arguments: u16,
479    #[index(num_bootstrap_arguments)]
480    pub bootstrap_arguments: Vec<u16>,
481}
482
483// {
484//     u2 name_index;
485//     u2 access_flags;
486// }
487#[derive(Clone, Copy, Debug, ReadFrom, WriteInto)]
488pub struct MethodParameter {
489    pub name_index: u16,
490    pub access_flags: u16,
491}
492
493// record_component_info {
494//     u2             name_index;
495//     u2             descriptor_index;
496//     u2             attributes_count;
497//     attribute_info attributes[attributes_count];
498// }
499#[derive(Clone, Debug, ReadFrom, WriteInto)]
500pub struct RecordComponentInfo {
501    pub name_index: u16,
502    pub descriptor_index: u16,
503    pub attributes_count: u16,
504    #[index(attributes_count)]
505    pub attributes: Vec<AttributeInfo>,
506}