Skip to content

Commit c578f84

Browse files
committed
Refactoring/renaming
1 parent a65999f commit c578f84

File tree

9 files changed

+218
-216
lines changed

9 files changed

+218
-216
lines changed

.vscode/launch.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
"name": "gcc - Aktive Datei erstellen und debuggen",
99
"type": "cppdbg",
1010
"request": "launch",
11-
"program": "${workspaceFolder}/main",
12-
"args": ["Main.class"],
11+
"program": "${workspaceFolder}/bin/main",
12+
"args": ["test/Main.class"],
1313
"stopAtEntry": false,
1414
"cwd": "${fileDirname}",
1515
"environment": [],
@@ -26,4 +26,4 @@
2626
"miDebuggerPath": "/usr/bin/gdb"
2727
}
2828
]
29-
}
29+
}

.vscode/tasks.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
{
77
"label": "build",
88
"type": "shell",
9-
"command": "${workspaceFolder}/build.sh"
9+
"command": "${workspaceFolder}/build/build.sh"
1010
}
1111
]
12-
}
12+
}

bin/main

24 Bytes
Binary file not shown.

buffer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ typedef struct {
2121
if (!buffer) { \
2222
void *base = malloc(sizeof(sb_header) + INITIAL_CAPACITY * sizeof(elem)); \
2323
*((sb_header *)base) = (sb_header) {.length = 1, .capacity = INITIAL_CAPACITY}; \
24-
buffer = (((char *) base) + sizeof(sb_header)); \
24+
buffer = (void *)(((char *) base) + sizeof(sb_header)); \
2525
buffer[0] = elem; \
2626
} else { \
2727
sb_header *base = __buf_header(buffer); \
2828
if (base->length == base->capacity) { \
2929
base = realloc(base, sizeof(sb_header) + base->capacity * 2 * sizeof(elem)); \
3030
base->capacity *= 2; \
31-
buffer = (((char *) base) + sizeof(sb_header)); \
31+
buffer = (void *)(((char *) base) + sizeof(sb_header)); \
3232
}\
3333
buffer[base->length] = elem; \
3434
base->length += 1; \

jvm.h

Lines changed: 75 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,16 @@
1313
#include <unistd.h>
1414
#include <stdbool.h>
1515

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;
1624
typedef int64_t i64;
1725

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-
2826
#define MIN(a, b) (a < b ? a : b)
2927

3028
/* NOTE(Noah):
@@ -49,75 +47,75 @@ typedef int32_t i4;
4947
#define CONSTANT_MethodType 16u
5048
#define CONSTANT_InvokeDynamic 18u
5149

52-
void pretty_print_constant_tag(u1 tag);
50+
void pretty_print_constant_tag(u8 tag);
5351

5452
typedef struct {
55-
u1 tag;
53+
u8 tag;
5654
union {
5755
struct {
58-
u2 name_index;
56+
u16 name_index;
5957
} class_info;
6058

6159
struct {
62-
u2 class_index;
63-
u2 name_and_type_index;
60+
u16 class_index;
61+
u16 name_and_type_index;
6462
} field_ref_info;
6563

6664
struct {
67-
u2 class_index;
68-
u2 name_and_type_index;
65+
u16 class_index;
66+
u16 name_and_type_index;
6967
} methodref_info;
7068

7169
struct {
72-
u2 class_index;
73-
u2 name_and_type_index;
70+
u16 class_index;
71+
u16 name_and_type_index;
7472
} interface_methodref_info;
7573

7674
struct {
77-
u2 string_index;
75+
u16 string_index;
7876
} string_info;
7977

8078
struct {
81-
u4 bytes;
79+
u32 bytes;
8280
} integer_info;
8381

8482
float float_value;
8583
i64 long_value;
8684
double double_value;
8785

8886
struct {
89-
u2 name_index;
90-
u2 descriptor_index;
87+
u16 name_index;
88+
u16 descriptor_index;
9189
} name_and_type_info;
9290

9391
struct {
94-
u2 length;
95-
u1* bytes;
92+
u16 length;
93+
u8* bytes;
9694
} utf8_info;
9795

9896
struct {
99-
u1 reference_kind;
100-
u2 reference_index;
97+
u8 reference_kind;
98+
u16 reference_index;
10199
} method_handle_info;
102100

103101
struct {
104-
u2 descriptor_index;
102+
u16 descriptor_index;
105103
} method_type_info;
106104

107105
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;
110108
} invoke_dynamic_info;
111-
} info;
109+
} as;
112110
// TODO(noah): rename "info" to "as"
113-
/*u1 info[]*/;
111+
/*u8 info[]*/;
114112
} cp_info;
115113

116114
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;
121119
} exception_table_entry;
122120

123121
typedef enum {
@@ -151,15 +149,15 @@ typedef enum {
151149
typedef struct attribute_info attribute_info;
152150

153151
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;
160158
exception_table_entry *exception_table;
161159
/* exception_table[exception_table_length]; */
162-
u2 attributes_count;
160+
u16 attributes_count;
163161
attribute_info *attributes;
164162
/* attribute_info attributes[attributes_count]; */
165163
} code_attribute;
@@ -174,41 +172,40 @@ typedef enum {
174172
} attribute_type;
175173

176174
struct attribute_info {
177-
u2 attribute_name_index;
178-
u4 attribute_length;
175+
u16 attribute_name_index;
176+
u32 attribute_length;
179177
attribute_type type;
180-
// TODO: Include the attribute kind as an enum?
181178
union {
182179
/* SourceFile_attribute */
183-
u2 sourcefile_index;
180+
u16 sourcefile_index;
184181

185182
/* ConstantValue_attribute */
186-
u2 constantvalue_index;
183+
u16 constantvalue_index;
187184

188185
/* Code_attribute */
189186
code_attribute *code_attribute;
190187

191188
/* 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]; */
196193
};
197194

198195
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;
203200
attribute_info *attributes;
204201
/* attribute_info attributes[attributes_count]; */
205202
} method_info;
206203

207204
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;
212209
attribute_info *attributes;
213210
/* attribute_info attributes[attributes_count]; */
214211
} field_info;
@@ -224,46 +221,46 @@ typedef struct {
224221
#define ACC_ENUM 0x4000u
225222

226223
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;
231228
cp_info *constant_pool;
232229
/* 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;
240237
field_info *fields;
241238
/* field_info fields[fields_count]; */
242-
u2 methods_count;
239+
u16 methods_count;
243240
method_info *methods;
244241
/* method_info methods[methods_count]; */
245-
u2 attributes_count;
242+
u16 attributes_count;
246243
attribute_info *attributes;
247244
/* attribute_info attributes[attributes_count]; */
248245
} ClassFile;
249246

250247
attribute_type parse_attribute_type(char *unicode_name, int length);
251248

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 */);
253250

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 */);
255252

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 */);
257254

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 */);
259256

260257
ClassFile *parse_class_file(char *filename);
261258

262259
void free_class_file(ClassFile *class_file);
263260

264261
code_attribute *find_code(ClassFile *class_file, method_info method_info);
265262

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);
267264

268265
void execute(ClassFile *class_file, code_attribute method_code);
269266

main.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,17 @@ int main(int argc, char **argv) {
1717
}
1818

1919
ClassFile *class_file = parse_class_file(argv[1]);
20+
if (!class_file) {
21+
printf("Failed to parse class file \"%s\"\n", argv[1]);
22+
return -1;
23+
}
24+
2025
printf("magic: %u %x\n", class_file->magic, class_file->magic);
2126
printf("minor version %u\n", class_file->minor_version);
2227
printf("major version %u\n", class_file->major_version);
2328
printf("constant pool count %u\n", class_file->constant_pool_count);
24-
printf("methodref %u %u %u\n", class_file->constant_pool[0].tag, class_file->constant_pool[0].info.methodref_info.class_index, class_file->constant_pool[0].info.methodref_info.name_and_type_index);
25-
printf("class %u %u\n", class_file->constant_pool[1].tag, class_file->constant_pool[1].info.class_info.name_index);
29+
printf("methodref %u %u %u\n", class_file->constant_pool[0].tag, class_file->constant_pool[0].as.methodref_info.class_index, class_file->constant_pool[0].as.methodref_info.name_and_type_index);
30+
printf("class %u %u\n", class_file->constant_pool[1].tag, class_file->constant_pool[1].as.class_info.name_index);
2631
printf("...\n");
2732
printf("access_flags 0x%x\n", class_file->access_flags);
2833
printf("this_class %d\n", class_file->this_class);
@@ -35,9 +40,9 @@ int main(int argc, char **argv) {
3540
// Print the source file name
3641
for (int i = 0; i < class_file->attributes_count; i++) {
3742
if (class_file->attributes[i].type == SourceFile_attribute) {
38-
int sourcefile_index = class_file->attributes[i].info.sourcefile_index - 1;
43+
int sourcefile_index = class_file->attributes[i].as.sourcefile_index - 1;
3944
cp_info constant = class_file->constant_pool[sourcefile_index];
40-
printf("source file name: \"%.*s\"\n", constant.info.utf8_info.length, constant.info.utf8_info.bytes);
45+
printf("source file name: \"%.*s\"\n", constant.as.utf8_info.length, constant.as.utf8_info.bytes);
4146
}
4247
}
4348

0 commit comments

Comments
 (0)