13
13
#include <unistd.h>
14
14
#include <stdbool.h>
15
15
16
+ typedef uint8_t u8 ;
17
+ typedef uint16_t u16 ;
18
+ typedef uint32_t u32 ;
19
+ typedef uint64_t u64 ;
20
+
21
+ typedef int8_t i8 ;
22
+ typedef int16_t i16 ;
23
+ typedef int32_t i32 ;
16
24
typedef int64_t i64 ;
17
25
18
-
19
- typedef uint8_t u1 ;
20
- typedef uint16_t u2 ;
21
- typedef uint32_t u4 ;
22
- typedef uint64_t u8 ;
23
-
24
- typedef int8_t i1 ;
25
- typedef int16_t i2 ;
26
- typedef int32_t i4 ;
27
-
28
26
#define MIN (a , b ) (a < b ? a : b)
29
27
30
28
/* NOTE(Noah):
@@ -49,75 +47,75 @@ typedef int32_t i4;
49
47
#define CONSTANT_MethodType 16u
50
48
#define CONSTANT_InvokeDynamic 18u
51
49
52
- void pretty_print_constant_tag (u1 tag );
50
+ void pretty_print_constant_tag (u8 tag );
53
51
54
52
typedef struct {
55
- u1 tag ;
53
+ u8 tag ;
56
54
union {
57
55
struct {
58
- u2 name_index ;
56
+ u16 name_index ;
59
57
} class_info ;
60
58
61
59
struct {
62
- u2 class_index ;
63
- u2 name_and_type_index ;
60
+ u16 class_index ;
61
+ u16 name_and_type_index ;
64
62
} field_ref_info ;
65
63
66
64
struct {
67
- u2 class_index ;
68
- u2 name_and_type_index ;
65
+ u16 class_index ;
66
+ u16 name_and_type_index ;
69
67
} methodref_info ;
70
68
71
69
struct {
72
- u2 class_index ;
73
- u2 name_and_type_index ;
70
+ u16 class_index ;
71
+ u16 name_and_type_index ;
74
72
} interface_methodref_info ;
75
73
76
74
struct {
77
- u2 string_index ;
75
+ u16 string_index ;
78
76
} string_info ;
79
77
80
78
struct {
81
- u4 bytes ;
79
+ u32 bytes ;
82
80
} integer_info ;
83
81
84
82
float float_value ;
85
83
i64 long_value ;
86
84
double double_value ;
87
85
88
86
struct {
89
- u2 name_index ;
90
- u2 descriptor_index ;
87
+ u16 name_index ;
88
+ u16 descriptor_index ;
91
89
} name_and_type_info ;
92
90
93
91
struct {
94
- u2 length ;
95
- u1 * bytes ;
92
+ u16 length ;
93
+ u8 * bytes ;
96
94
} utf8_info ;
97
95
98
96
struct {
99
- u1 reference_kind ;
100
- u2 reference_index ;
97
+ u8 reference_kind ;
98
+ u16 reference_index ;
101
99
} method_handle_info ;
102
100
103
101
struct {
104
- u2 descriptor_index ;
102
+ u16 descriptor_index ;
105
103
} method_type_info ;
106
104
107
105
struct {
108
- u2 bootstrap_method_attr_index ;
109
- u2 name_and_type_index ;
106
+ u16 bootstrap_method_attr_index ;
107
+ u16 name_and_type_index ;
110
108
} invoke_dynamic_info ;
111
- } info ;
109
+ } as ;
112
110
// TODO(noah): rename "info" to "as"
113
- /*u1 info[]*/ ;
111
+ /*u8 info[]*/ ;
114
112
} cp_info ;
115
113
116
114
typedef struct {
117
- u2 start_pc ;
118
- u2 end_pc ;
119
- u2 handler_pc ;
120
- u2 catch_type ;
115
+ u16 start_pc ;
116
+ u16 end_pc ;
117
+ u16 handler_pc ;
118
+ u16 catch_type ;
121
119
} exception_table_entry ;
122
120
123
121
typedef enum {
@@ -151,15 +149,15 @@ typedef enum {
151
149
typedef struct attribute_info attribute_info ;
152
150
153
151
typedef struct {
154
- u2 max_stack ;
155
- u2 max_locals ;
156
- u4 code_length ;
157
- u1 * code ;
158
- /* u1 code[code_length]; */
159
- u2 exception_table_length ;
152
+ u16 max_stack ;
153
+ u16 max_locals ;
154
+ u32 code_length ;
155
+ u8 * code ;
156
+ /* u8 code[code_length]; */
157
+ u16 exception_table_length ;
160
158
exception_table_entry * exception_table ;
161
159
/* exception_table[exception_table_length]; */
162
- u2 attributes_count ;
160
+ u16 attributes_count ;
163
161
attribute_info * attributes ;
164
162
/* attribute_info attributes[attributes_count]; */
165
163
} code_attribute ;
@@ -174,41 +172,40 @@ typedef enum {
174
172
} attribute_type ;
175
173
176
174
struct attribute_info {
177
- u2 attribute_name_index ;
178
- u4 attribute_length ;
175
+ u16 attribute_name_index ;
176
+ u32 attribute_length ;
179
177
attribute_type type ;
180
- // TODO: Include the attribute kind as an enum?
181
178
union {
182
179
/* SourceFile_attribute */
183
- u2 sourcefile_index ;
180
+ u16 sourcefile_index ;
184
181
185
182
/* ConstantValue_attribute */
186
- u2 constantvalue_index ;
183
+ u16 constantvalue_index ;
187
184
188
185
/* Code_attribute */
189
186
code_attribute * code_attribute ;
190
187
191
188
/* other */
192
- u1 * bytes ;
193
- } info ;
194
- // u1 *info;
195
- /* u1 info[attribute_length]; */
189
+ u8 * bytes ;
190
+ } as ;
191
+ // u8 *info;
192
+ /* u8 info[attribute_length]; */
196
193
};
197
194
198
195
typedef struct {
199
- u2 access_flags ;
200
- u2 name_index ;
201
- u2 descriptor_index ;
202
- u2 attributes_count ;
196
+ u16 access_flags ;
197
+ u16 name_index ;
198
+ u16 descriptor_index ;
199
+ u16 attributes_count ;
203
200
attribute_info * attributes ;
204
201
/* attribute_info attributes[attributes_count]; */
205
202
} method_info ;
206
203
207
204
typedef struct {
208
- u2 access_flags ;
209
- u2 name_index ;
210
- u2 descriptor_index ;
211
- u2 attributes_count ;
205
+ u16 access_flags ;
206
+ u16 name_index ;
207
+ u16 descriptor_index ;
208
+ u16 attributes_count ;
212
209
attribute_info * attributes ;
213
210
/* attribute_info attributes[attributes_count]; */
214
211
} field_info ;
@@ -224,46 +221,46 @@ typedef struct {
224
221
#define ACC_ENUM 0x4000u
225
222
226
223
typedef struct {
227
- u4 magic ;
228
- u2 minor_version ;
229
- u2 major_version ;
230
- u2 constant_pool_count ;
224
+ u32 magic ;
225
+ u16 minor_version ;
226
+ u16 major_version ;
227
+ u16 constant_pool_count ;
231
228
cp_info * constant_pool ;
232
229
/* cp_info constant_pool[constant_pool_count-1]; */
233
- u2 access_flags ;
234
- u2 this_class ;
235
- u2 super_class ;
236
- u2 interfaces_count ;
237
- u2 * interfaces ;
238
- /* u2 interfaces[interfaces_count]; */
239
- u2 fields_count ;
230
+ u16 access_flags ;
231
+ u16 this_class ;
232
+ u16 super_class ;
233
+ u16 interfaces_count ;
234
+ u16 * interfaces ;
235
+ /* u16 interfaces[interfaces_count]; */
236
+ u16 fields_count ;
240
237
field_info * fields ;
241
238
/* field_info fields[fields_count]; */
242
- u2 methods_count ;
239
+ u16 methods_count ;
243
240
method_info * methods ;
244
241
/* method_info methods[methods_count]; */
245
- u2 attributes_count ;
242
+ u16 attributes_count ;
246
243
attribute_info * attributes ;
247
244
/* attribute_info attributes[attributes_count]; */
248
245
} ClassFile ;
249
246
250
247
attribute_type parse_attribute_type (char * unicode_name , int length );
251
248
252
- attribute_info parse_attribute_info (ClassFile * class_file , u1 * data , int * out_byte_size /* How many bytes were parsed */ );
249
+ attribute_info parse_attribute_info (ClassFile * class_file , u8 * data , int * out_byte_size /* How many bytes were parsed */ );
253
250
254
- method_info parse_method_info (ClassFile * class_file , u1 * data , int * out_byte_size /* How many bytes were parsed */ );
251
+ method_info parse_method_info (ClassFile * class_file , u8 * data , int * out_byte_size /* How many bytes were parsed */ );
255
252
256
- field_info parse_field_info (ClassFile * class_file , u1 * data , int * out_byte_size /* How many bytes were parsed */ );
253
+ field_info parse_field_info (ClassFile * class_file , u8 * data , int * out_byte_size /* How many bytes were parsed */ );
257
254
258
- cp_info parse_cp_info (u1 * data , int * out_byte_size /* How many bytes were parsed */ );
255
+ cp_info parse_cp_info (u8 * data , int * out_byte_size /* How many bytes were parsed */ );
259
256
260
257
ClassFile * parse_class_file (char * filename );
261
258
262
259
void free_class_file (ClassFile * class_file );
263
260
264
261
code_attribute * find_code (ClassFile * class_file , method_info method_info );
265
262
266
- method_info find_method (ClassFile * class_file , char * name , u4 name_length );
263
+ method_info find_method (ClassFile * class_file , char * name , u32 name_length );
267
264
268
265
void execute (ClassFile * class_file , code_attribute method_code );
269
266
0 commit comments