Skip to content

Commit 895646c

Browse files
committed
refactor multiarray/anewarray/newarray/instanceof/checkedcast
1 parent 1dcda3d commit 895646c

16 files changed

+617
-497
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ CC ?= gcc
77
ARCH = -m32
88
CFLAGS += -Wall -Werror -Wfatal-errors -Wno-error=unused-variable -fstack-protector -std=c2x -g
99
CFLAGS += -DDEBUG
10-
#CFLAGS += -DDEBUG_PRINT
10+
CFLAGS += -DDEBUG_PRINT
1111
LDFLAGS = -lm
1212
OPT ?= -O0
1313
DEPFLAGS = -MMD -MP

c/class_resolver.c

Lines changed: 89 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,6 @@ struct class_entry * class_resolver_lookup_class_from_class_index(int class_hash
309309
struct parse_type_ret parse_type_ret = parse_type(class_name_constant->utf8.bytes,
310310
class_name_constant->utf8.length);
311311

312-
313312
struct class_entry * _class_entry = class_resolver_lookup_class(class_hash_table_length,
314313
class_hash_table,
315314
parse_type_ret.bytes,
@@ -586,10 +585,10 @@ struct method_entry class_resolver_lookup_method_from_method_name_method_descrip
586585
}
587586
}
588587

589-
int32_t * class_resolver_lookup_string(int class_hash_table_length,
590-
struct hash_table_entry * class_hash_table,
591-
struct class_entry * class_entry,
592-
const int string_index)
588+
struct objectref * class_resolver_lookup_string(int class_hash_table_length,
589+
struct hash_table_entry * class_hash_table,
590+
struct class_entry * class_entry,
591+
const int string_index)
593592
{
594593
debugf("class_resolver_lookup_string: %d\n", string_index);
595594
if (class_entry->attribute_entry[string_index - 1].string_objectref != nullptr) {
@@ -605,22 +604,98 @@ int32_t * class_resolver_lookup_string(int class_hash_table_length,
605604
(const uint8_t *)"java/lang/String",
606605
16);
607606

608-
int32_t size = utf8_constant->utf8.length + 4;
609-
int32_t * arrayref = memory_allocate(size);
607+
int32_t size = utf8_constant->utf8.length + (sizeof (struct arrayref));
608+
struct arrayref * arrayref = memory_allocate(size);
610609
assert(arrayref != nullptr);
611-
arrayref[0] = utf8_constant->utf8.length;
612-
uint8_t * bytearray = (uint8_t *)&arrayref[1];
613-
for (int i = 0; i < utf8_constant->utf8.length; i++)
614-
bytearray[i] = utf8_constant->utf8.bytes[i];
610+
arrayref->class_entry = nullptr; // byte[]
611+
arrayref->length = utf8_constant->utf8.length;
612+
for (int i = 0; i < utf8_constant->utf8.length; i++) {
613+
arrayref->u8[i] = utf8_constant->utf8.bytes[i];
614+
}
615615

616616
assert(string_class_entry != nullptr);
617-
int32_t * objectref = memory_allocate(4 + 4);
617+
struct objectref * objectref = memory_allocate((sizeof (struct objectref)) + (sizeof (void *)));
618618
assert(objectref != nullptr);
619-
objectref[0] = (int32_t)string_class_entry;
620-
objectref[1] = (int32_t)arrayref;
619+
objectref->class_entry = string_class_entry;
620+
objectref->aref[0] = arrayref;
621621

622622
// cache the result
623623
class_entry->attribute_entry[string_index - 1].string_objectref = objectref;
624624

625625
return objectref;
626626
}
627+
628+
bool class_resolver_instanceof(int class_hash_table_length,
629+
struct hash_table_entry * class_hash_table,
630+
struct class_entry * origin_class_entry,
631+
const int class_index,
632+
struct objectref * objectref)
633+
{
634+
struct class_entry * index_class_entry =
635+
class_resolver_lookup_class_from_class_index(class_hash_table_length,
636+
class_hash_table,
637+
origin_class_entry,
638+
class_index);
639+
assert(index_class_entry != nullptr);
640+
641+
assert(objectref != nullptr);
642+
struct class_entry * class_entry = objectref->class_entry;
643+
while (true) {
644+
debugf("class_entry: %p\n", class_entry);
645+
646+
assert(class_entry != nullptr);
647+
if (class_entry == index_class_entry) {
648+
return true;
649+
}
650+
if (class_entry->class_file->super_class == 0) {
651+
return false;
652+
}
653+
654+
struct constant * class_constant = &class_entry->class_file->constant_pool[class_entry->class_file->super_class - 1];
655+
assert(class_constant->tag == CONSTANT_Class);
656+
struct constant * class_name_constant = &class_entry->class_file->constant_pool[class_constant->class.name_index - 1];
657+
assert(class_name_constant->tag == CONSTANT_Utf8);
658+
debug_print__constant__utf8_string(class_name_constant);
659+
debugf("\n");
660+
661+
// superclass lookup
662+
class_entry = class_resolver_lookup_class_from_class_index(class_hash_table_length,
663+
class_hash_table,
664+
class_entry,
665+
class_entry->class_file->super_class);
666+
}
667+
}
668+
669+
struct hash_table_entry * class_resolver_init_string_hash_table(int length)
670+
{
671+
assert((length & (length - 1)) == 0); // length must be a power of two
672+
int hash_table_length = length;
673+
uint32_t hash_table_size = (sizeof (struct hash_table_entry)) * hash_table_length;
674+
struct hash_table_entry * hash_table = malloc_class_arena(hash_table_size);
675+
hash_table_init(hash_table_length, hash_table);
676+
677+
return hash_table;
678+
}
679+
680+
void * class_resolver_memoize_string_type(int string_hash_table_length,
681+
struct hash_table_entry * string_hash_table,
682+
const uint8_t * type,
683+
int length)
684+
{
685+
struct hash_table_entry * e = hash_table_find(string_hash_table_length,
686+
string_hash_table,
687+
type,
688+
length);
689+
if (e != nullptr) {
690+
assert(e->value == (void *)0x12345678);
691+
return e;
692+
} else {
693+
struct hash_table_entry * e = hash_table_add(string_hash_table_length,
694+
string_hash_table,
695+
type,
696+
length,
697+
(void *)0x12345678);
698+
assert(e != nullptr);
699+
return e;
700+
}
701+
}

c/class_resolver.h

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "class_file.h"
66
#include "hash_table.h"
7+
#include "native_types.h"
78

89
enum initialization_state {
910
CLASS_UNINITIALIZED,
@@ -21,7 +22,7 @@ union attribute_entry {
2122
struct class_entry * class_entry;
2223
struct method_entry * method_entry;
2324
struct field_entry * field_entry;
24-
int32_t * string_objectref;
25+
struct objectref * string_objectref;
2526
};
2627

2728
struct field_entry {
@@ -84,7 +85,20 @@ struct field_entry * class_resolver_lookup_field_from_fieldref_index(int fields_
8485
struct class_entry * class_entry,
8586
int fieldref_index);
8687

87-
int32_t * class_resolver_lookup_string(int class_hash_table_length,
88-
struct hash_table_entry * class_hash_table,
89-
struct class_entry * class_entry,
90-
const int string_index);
88+
struct objectref * class_resolver_lookup_string(int class_hash_table_length,
89+
struct hash_table_entry * class_hash_table,
90+
struct class_entry * class_entry,
91+
const int string_index);
92+
93+
bool class_resolver_instanceof(int class_hash_table_length,
94+
struct hash_table_entry * class_hash_table,
95+
struct class_entry * origin_class_entry,
96+
const int class_index,
97+
struct objectref * objectref);
98+
99+
struct hash_table_entry * class_resolver_init_string_hash_table(int length);
100+
101+
void * class_resolver_memoize_string_type(int string_hash_table_length,
102+
struct hash_table_entry * string_hash_table,
103+
const uint8_t * type,
104+
int length);

0 commit comments

Comments
 (0)