Compare commits
No commits in common. "9982538c89efceadedf3206fc9f0242b6246932a" and "45501b58cbd3b1f16bfeb8bfa667c3649bba6b87" have entirely different histories.
9982538c89
...
45501b58cb
@ -2,19 +2,18 @@
|
||||
|
||||
#include "assert.h"
|
||||
#include "class_file.h"
|
||||
#include "class_resolver.h"
|
||||
#include "debug.h"
|
||||
#include "fatal.h"
|
||||
#include "field_size.h"
|
||||
#include "find_attribute.h"
|
||||
#include "hash_table.h"
|
||||
#include "malloc.h"
|
||||
#include "memory_allocator.h"
|
||||
#include "native_method.h"
|
||||
#include "native_types_allocate.h"
|
||||
#include "parse_type.h"
|
||||
#include "printf.h"
|
||||
#include "class_resolver.h"
|
||||
#include "string.h"
|
||||
#include "memory_allocator.h"
|
||||
#include "printf.h"
|
||||
#include "field_size.h"
|
||||
#include "debug.h"
|
||||
#include "fatal.h"
|
||||
#include "parse_type.h"
|
||||
#include "find_attribute.h"
|
||||
#include "native_types_allocate.h"
|
||||
#include "vm_instance.h"
|
||||
|
||||
static int field_info_field_size(struct class_file * class_file, struct field_info * field_info)
|
||||
@ -442,7 +441,9 @@ struct method_info * class_resolver_lookup_method(int methods_hash_table_length,
|
||||
static struct Code_attribute * resolve_code_attribute(struct class_entry * class_entry,
|
||||
struct method_info * method_info)
|
||||
{
|
||||
assert((method_info->access_flags & METHOD_ACC_NATIVE) == 0);
|
||||
if ((method_info->access_flags & METHOD_ACC_NATIVE) != 0) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
int code_index = find_code_name_index(class_entry->class_file);
|
||||
assert(code_index != 0);
|
||||
@ -453,8 +454,7 @@ static struct Code_attribute * resolve_code_attribute(struct class_entry * class
|
||||
return attribute->code;
|
||||
}
|
||||
|
||||
static void resolve_method_entry(struct vm * vm,
|
||||
struct class_entry * class_entry,
|
||||
static void resolve_method_entry(struct class_entry * class_entry,
|
||||
struct constant * method_name_constant,
|
||||
struct constant * method_descriptor_constant,
|
||||
struct method_entry * method_entry)
|
||||
@ -470,26 +470,7 @@ static void resolve_method_entry(struct vm * vm,
|
||||
|
||||
method_entry->class_entry = class_entry;
|
||||
method_entry->method_info = method_info;
|
||||
if ((method_info->access_flags & METHOD_ACC_NATIVE) == 0) {
|
||||
method_entry->code_attribute = resolve_code_attribute(class_entry, method_info);
|
||||
} else {
|
||||
struct class_file * class_file = class_entry->class_file;
|
||||
|
||||
struct constant * class_constant = &class_file->constant_pool[class_file->this_class - 1];
|
||||
assert(class_constant->tag == CONSTANT_Class);
|
||||
struct constant * class_name_constant = &class_file->constant_pool[class_constant->class.name_index - 1];
|
||||
assert(class_name_constant->tag == CONSTANT_Utf8);
|
||||
struct constant * method_name_constant = &class_file->constant_pool[method_info->name_index - 1];
|
||||
assert(method_name_constant->tag == CONSTANT_Utf8);
|
||||
struct constant * method_descriptor_constant = &class_file->constant_pool[method_info->descriptor_index - 1];
|
||||
assert(method_descriptor_constant->tag == CONSTANT_Utf8);
|
||||
|
||||
method_entry->native_func = native_method_lookup(vm->native_hash_table.length,
|
||||
vm->native_hash_table.entry,
|
||||
class_name_constant,
|
||||
method_name_constant,
|
||||
method_descriptor_constant);
|
||||
}
|
||||
|
||||
debugf("method resolved:\n");
|
||||
debugf(" class: ");
|
||||
@ -499,7 +480,8 @@ static void resolve_method_entry(struct vm * vm,
|
||||
debugc('\n');
|
||||
}
|
||||
|
||||
static void resolve_method_entry_from_superclasses(struct vm * vm,
|
||||
static void resolve_method_entry_from_superclasses(int class_hash_table_length,
|
||||
struct hash_table_entry * class_hash_table,
|
||||
struct class_entry * class_entry,
|
||||
struct constant * method_name_constant,
|
||||
struct constant * method_descriptor_constant,
|
||||
@ -510,8 +492,7 @@ static void resolve_method_entry_from_superclasses(struct vm * vm,
|
||||
method_entry->method_info = nullptr;
|
||||
|
||||
while (true) {
|
||||
resolve_method_entry(vm,
|
||||
class_entry,
|
||||
resolve_method_entry(class_entry,
|
||||
method_name_constant,
|
||||
method_descriptor_constant,
|
||||
method_entry);
|
||||
@ -527,20 +508,20 @@ static void resolve_method_entry_from_superclasses(struct vm * vm,
|
||||
assert(class_name_constant->tag == CONSTANT_Utf8);
|
||||
|
||||
// lookup the method from the superclass
|
||||
class_entry = class_resolver_lookup_class_from_class_index(vm->class_hash_table.length,
|
||||
vm->class_hash_table.entry,
|
||||
class_entry = class_resolver_lookup_class_from_class_index(class_hash_table_length,
|
||||
class_hash_table,
|
||||
class_entry,
|
||||
class_entry->class_file->super_class);
|
||||
assert(class_entry != nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
struct method_entry class_resolver_lookup_method_from_objectref_class(struct vm * vm,
|
||||
struct method_entry class_resolver_lookup_method_from_objectref_class(int class_hash_table_length,
|
||||
struct hash_table_entry * class_hash_table,
|
||||
int32_t methodref_index,
|
||||
struct class_entry * objectref_class_entry)
|
||||
struct class_entry * objectref_class_entry,
|
||||
struct class_entry * origin_class_entry)
|
||||
{
|
||||
struct class_entry * origin_class_entry = vm->current_frame->class_entry;
|
||||
|
||||
struct constant * methodref_constant = &origin_class_entry->class_file->constant_pool[methodref_index - 1];
|
||||
assert(methodref_constant->tag == CONSTANT_InterfaceMethodref || methodref_constant->tag == CONSTANT_Methodref);
|
||||
struct constant * nameandtype_constant = &origin_class_entry->class_file->constant_pool[methodref_constant->methodref.name_and_type_index - 1];
|
||||
@ -554,7 +535,8 @@ struct method_entry class_resolver_lookup_method_from_objectref_class(struct vm
|
||||
|
||||
struct method_entry method_entry;
|
||||
|
||||
resolve_method_entry_from_superclasses(vm,
|
||||
resolve_method_entry_from_superclasses(class_hash_table_length,
|
||||
class_hash_table,
|
||||
class_entry,
|
||||
method_name_constant,
|
||||
method_descriptor_constant,
|
||||
@ -563,10 +545,11 @@ struct method_entry class_resolver_lookup_method_from_objectref_class(struct vm
|
||||
return method_entry;
|
||||
}
|
||||
|
||||
struct method_entry * class_resolver_lookup_method_from_origin_class(struct vm * vm,
|
||||
int32_t methodref_index)
|
||||
struct method_entry * class_resolver_lookup_method_from_origin_class(int class_hash_table_length,
|
||||
struct hash_table_entry * class_hash_table,
|
||||
int32_t methodref_index,
|
||||
struct class_entry * origin_class_entry)
|
||||
{
|
||||
struct class_entry * origin_class_entry = vm->current_frame->class_entry;
|
||||
if (origin_class_entry->attribute_entry[methodref_index - 1].method_entry != nullptr) {
|
||||
debugf("class_resolver_lookup_method_from_origin_class %d: [cached]\n", methodref_index);
|
||||
return origin_class_entry->attribute_entry[methodref_index - 1].method_entry;
|
||||
@ -581,14 +564,15 @@ struct method_entry * class_resolver_lookup_method_from_origin_class(struct vm *
|
||||
struct constant * method_descriptor_constant = &origin_class_entry->class_file->constant_pool[nameandtype_constant->nameandtype.descriptor_index - 1];
|
||||
assert(method_descriptor_constant->tag == CONSTANT_Utf8);
|
||||
|
||||
struct class_entry * class_entry = class_resolver_lookup_class_from_class_index(vm->class_hash_table.length,
|
||||
vm->class_hash_table.entry,
|
||||
struct class_entry * class_entry = class_resolver_lookup_class_from_class_index(class_hash_table_length,
|
||||
class_hash_table,
|
||||
origin_class_entry,
|
||||
methodref_constant->methodref.class_index);
|
||||
|
||||
struct method_entry * method_entry = malloc_class_arena((sizeof (struct method_entry)));
|
||||
|
||||
resolve_method_entry_from_superclasses(vm,
|
||||
resolve_method_entry_from_superclasses(class_hash_table_length,
|
||||
class_hash_table,
|
||||
class_entry,
|
||||
method_name_constant,
|
||||
method_descriptor_constant,
|
||||
@ -613,8 +597,8 @@ struct method_entry class_resolver_lookup_method_from_method_name_method_descrip
|
||||
method_name_length,
|
||||
method_descriptor,
|
||||
method_descriptor_length);
|
||||
|
||||
if (method_info != nullptr) {
|
||||
assert ((method_info->access_flags & METHOD_ACC_NATIVE) == 0);
|
||||
int code_index = find_code_name_index(class_entry->class_file);
|
||||
assert(code_index != 0);
|
||||
debugf("code_index: %d\n", code_index);
|
||||
|
@ -19,11 +19,15 @@ struct class_entry * class_resolver_lookup_class_from_class_index(int class_hash
|
||||
struct class_entry * class_entry,
|
||||
int32_t class_index);
|
||||
|
||||
struct method_entry class_resolver_lookup_method_from_objectref_class(struct vm * vm,
|
||||
struct method_entry class_resolver_lookup_method_from_objectref_class(int class_hash_table_length,
|
||||
struct hash_table_entry * class_hash_table,
|
||||
int32_t methodref_index,
|
||||
struct class_entry * objectref_class_entry);
|
||||
struct method_entry * class_resolver_lookup_method_from_origin_class(struct vm * vm,
|
||||
int32_t methodref_index);
|
||||
struct class_entry * objectref_class_entry,
|
||||
struct class_entry * origin_class_entry);
|
||||
struct method_entry * class_resolver_lookup_method_from_origin_class(int class_hash_table_length,
|
||||
struct hash_table_entry * class_hash_table,
|
||||
int32_t methodref_index,
|
||||
struct class_entry * origin_class_entry);
|
||||
|
||||
struct method_entry class_resolver_lookup_method_from_method_name_method_descriptor(struct class_entry * class_entry,
|
||||
const uint8_t * method_name,
|
||||
|
22
c/execute.c
22
c/execute.c
@ -1210,9 +1210,11 @@ void op_invokeinterface(struct vm * vm, uint32_t index, uint32_t count)
|
||||
return vm_exception(vm, vm_instance_create(vm, "java/lang/NullPointerException"));
|
||||
|
||||
struct method_entry method_entry =
|
||||
class_resolver_lookup_method_from_objectref_class(vm,
|
||||
class_resolver_lookup_method_from_objectref_class(vm->class_hash_table.length,
|
||||
vm->class_hash_table.entry,
|
||||
index,
|
||||
objectref->class_entry);
|
||||
objectref->class_entry,
|
||||
vm->current_frame->class_entry);
|
||||
|
||||
vm_special_method_call(vm, method_entry.class_entry, &method_entry);
|
||||
}
|
||||
@ -1237,7 +1239,10 @@ void op_invokespecial(struct vm * vm, uint32_t index)
|
||||
return vm_exception(vm, vm_instance_create(vm, "java/lang/NullPointerException"));
|
||||
|
||||
struct method_entry * method_entry =
|
||||
class_resolver_lookup_method_from_origin_class(vm, index);
|
||||
class_resolver_lookup_method_from_origin_class(vm->class_hash_table.length,
|
||||
vm->class_hash_table.entry,
|
||||
index,
|
||||
vm->current_frame->class_entry);
|
||||
|
||||
vm_special_method_call(vm, method_entry->class_entry, method_entry);
|
||||
}
|
||||
@ -1245,7 +1250,10 @@ void op_invokespecial(struct vm * vm, uint32_t index)
|
||||
void op_invokestatic(struct vm * vm, uint32_t index)
|
||||
{
|
||||
struct method_entry * method_entry =
|
||||
class_resolver_lookup_method_from_origin_class(vm, index);
|
||||
class_resolver_lookup_method_from_origin_class(vm->class_hash_table.length,
|
||||
vm->class_hash_table.entry,
|
||||
index,
|
||||
vm->current_frame->class_entry);
|
||||
|
||||
/* On successful resolution of the method, the class or interface that
|
||||
declared the resolved method is initialized if that class or interface has
|
||||
@ -1274,9 +1282,11 @@ void op_invokevirtual(struct vm * vm, uint32_t index)
|
||||
return vm_exception(vm, vm_instance_create(vm, "java/lang/NullPointerException"));
|
||||
|
||||
struct method_entry method_entry =
|
||||
class_resolver_lookup_method_from_objectref_class(vm,
|
||||
class_resolver_lookup_method_from_objectref_class(vm->class_hash_table.length,
|
||||
vm->class_hash_table.entry,
|
||||
index,
|
||||
objectref->class_entry);
|
||||
objectref->class_entry,
|
||||
vm->current_frame->class_entry);
|
||||
|
||||
vm_special_method_call(vm, method_entry.class_entry, &method_entry);
|
||||
}
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "vm.h"
|
||||
#include "assert.h"
|
||||
|
||||
enum initialization_state {
|
||||
@ -10,17 +11,10 @@ enum initialization_state {
|
||||
CLASS_INITIALIZED,
|
||||
};
|
||||
|
||||
struct vm;
|
||||
|
||||
typedef void (native_func_t)(struct vm * vm, uint32_t * args);
|
||||
|
||||
struct method_entry {
|
||||
struct class_entry * class_entry;
|
||||
struct method_info * method_info;
|
||||
union {
|
||||
struct Code_attribute * code_attribute;
|
||||
native_func_t * native_func;
|
||||
};
|
||||
};
|
||||
|
||||
union attribute_entry {
|
||||
@ -80,20 +74,6 @@ struct stack {
|
||||
int32_t capacity;
|
||||
};
|
||||
|
||||
struct vm {
|
||||
struct stack frame_stack;
|
||||
struct stack data_stack;
|
||||
struct frame * current_frame;
|
||||
struct {
|
||||
int length;
|
||||
struct hash_table_entry * entry;
|
||||
} class_hash_table;
|
||||
struct {
|
||||
int length;
|
||||
struct hash_table_entry * entry;
|
||||
} native_hash_table;
|
||||
};
|
||||
|
||||
static inline struct frame * stack_push_frame(struct stack * stack, int num_frames)
|
||||
{
|
||||
struct frame * frame = &stack->frame[stack->ix];
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
#include "string.h"
|
||||
#include "class_resolver.h"
|
||||
#include "native_method.h"
|
||||
#include "native.h"
|
||||
#include "malloc.h"
|
||||
|
||||
#include "classpath.h"
|
||||
|
@ -1,12 +1,12 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "printf.h"
|
||||
#include "class_resolver.h"
|
||||
#include "string.h"
|
||||
#include "file.h"
|
||||
#include "malloc.h"
|
||||
#include "native.h"
|
||||
#include "memory_allocator.h"
|
||||
#include "native_method.h"
|
||||
#include "printf.h"
|
||||
#include "string.h"
|
||||
|
||||
void * memset(void * s, int c, size_t n);
|
||||
|
||||
|
@ -1,6 +1,8 @@
|
||||
#include "frame_stack.h" // for native_func_t
|
||||
#include "hash_table.h"
|
||||
#include "malloc.h"
|
||||
#include "string.h"
|
||||
#include "printf.h"
|
||||
#include "native.h"
|
||||
#include "native/class.h"
|
||||
#include "native/libcinputstream.h"
|
||||
#include "native/loader.h"
|
||||
@ -11,15 +13,14 @@
|
||||
#include "native/runtime.h"
|
||||
#include "native/sh4intrinsic.h"
|
||||
#include "native/system.h"
|
||||
#include "native_method.h"
|
||||
#include "printf.h"
|
||||
#include "string.h"
|
||||
|
||||
typedef void (* native_func_t)(struct vm * vm, uint32_t * args);
|
||||
|
||||
struct native_method {
|
||||
const char * class_name;
|
||||
const char * method_name;
|
||||
const char * method_descriptor;
|
||||
native_func_t * func;
|
||||
native_func_t func;
|
||||
};
|
||||
|
||||
const static struct native_method native_method[] = {
|
||||
@ -300,14 +301,14 @@ struct hash_table_entry * native_init_hash_table(int * hash_table_length)
|
||||
return native_hash_table;
|
||||
}
|
||||
|
||||
native_func_t * native_method_lookup(int native_hash_table_length,
|
||||
struct hash_table_entry * native_hash_table,
|
||||
void native_method_call(struct vm * vm,
|
||||
struct constant * class_name_constant,
|
||||
struct constant * method_name_constant,
|
||||
struct constant * method_descriptor_constant)
|
||||
struct constant * method_descriptor_constant,
|
||||
uint32_t * args)
|
||||
{
|
||||
struct hash_table_entry * e = hash_table_find3(native_hash_table_length,
|
||||
native_hash_table,
|
||||
struct hash_table_entry * e = hash_table_find3(vm->native_hash_table.length,
|
||||
vm->native_hash_table.entry,
|
||||
class_name_constant->utf8.bytes,
|
||||
class_name_constant->utf8.length,
|
||||
method_name_constant->utf8.bytes,
|
||||
@ -326,5 +327,6 @@ native_func_t * native_method_lookup(int native_hash_table_length,
|
||||
assert(e != nullptr);
|
||||
assert(e->value != nullptr);
|
||||
|
||||
return(native_func_t *)e->value;
|
||||
native_func_t func = (native_func_t)e->value;
|
||||
func(vm, args);
|
||||
}
|
10
c/native.h
Normal file
10
c/native.h
Normal file
@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include "vm.h"
|
||||
|
||||
void native_method_call(struct vm * vm,
|
||||
struct constant * class_name_constant,
|
||||
struct constant * method_name_constant,
|
||||
struct constant * method_descriptor_constant,
|
||||
uint32_t * args);
|
||||
struct hash_table_entry * native_init_hash_table(int * hash_table_length);
|
@ -1,10 +1,10 @@
|
||||
#include "class_resolver.h"
|
||||
#include "loader.h"
|
||||
#include "malloc.h"
|
||||
#include "native_method.h"
|
||||
#include "native_types.h"
|
||||
#include "printf.h"
|
||||
#include "string.h"
|
||||
#include "malloc.h"
|
||||
#include "loader.h"
|
||||
#include "native.h"
|
||||
#include "class_resolver.h"
|
||||
|
||||
static uint8_t loader_buffer[0x100000] __attribute__ ((aligned (32)));
|
||||
|
||||
|
@ -1,11 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "vm.h"
|
||||
|
||||
struct hash_table_entry * native_init_hash_table(int * hash_table_length);
|
||||
|
||||
native_func_t * native_method_lookup(int native_hash_table_length,
|
||||
struct hash_table_entry * native_hash_table,
|
||||
struct constant * class_name_constant,
|
||||
struct constant * method_name_constant,
|
||||
struct constant * method_descriptor_constant);
|
23
c/vm.c
23
c/vm.c
@ -7,6 +7,7 @@
|
||||
#include "class_resolver.h"
|
||||
#include "printf.h"
|
||||
#include "string.h"
|
||||
#include "native.h"
|
||||
#include "fatal.h"
|
||||
#include "debug.h"
|
||||
#include "find_attribute.h"
|
||||
@ -173,9 +174,29 @@ void vm_native_method_call(struct vm * vm, struct class_entry * class_entry, str
|
||||
args[nargs - i - 1] = value;
|
||||
}
|
||||
|
||||
debugf("native: ");
|
||||
struct constant * class_constant = &class_entry->class_file->constant_pool[class_entry->class_file->this_class - 1];
|
||||
assert(class_constant->tag == CONSTANT_Class);
|
||||
struct constant * class_name_constant = &class_entry->class_file->constant_pool[class_constant->class.name_index - 1];
|
||||
assert(class_name_constant->tag == CONSTANT_Utf8);
|
||||
debug_print__constant__utf8_string(class_name_constant);
|
||||
debugs(" ");
|
||||
struct constant * method_name_constant = &class_entry->class_file->constant_pool[method_entry->method_info->name_index - 1];
|
||||
assert(method_name_constant->tag == CONSTANT_Utf8);
|
||||
debug_print__constant__utf8_string(method_name_constant);
|
||||
debugs(" ");
|
||||
struct constant * method_descriptor_constant = &class_entry->class_file->constant_pool[method_entry->method_info->descriptor_index - 1];
|
||||
assert(method_descriptor_constant->tag == CONSTANT_Utf8);
|
||||
debug_print__constant__utf8_string(method_descriptor_constant);
|
||||
debugc('\n');
|
||||
|
||||
int old_stack_ix = vm->current_frame->operand_stack_ix;
|
||||
|
||||
method_entry->native_func(vm, args);
|
||||
native_method_call(vm,
|
||||
class_name_constant,
|
||||
method_name_constant,
|
||||
method_descriptor_constant,
|
||||
args);
|
||||
|
||||
if (return_type != 'V') {
|
||||
assert(old_stack_ix == vm->current_frame->operand_stack_ix - 1);
|
||||
|
14
c/vm.h
14
c/vm.h
@ -3,6 +3,20 @@
|
||||
#include "frame_stack.h"
|
||||
#include "class_file.h"
|
||||
|
||||
struct vm {
|
||||
struct stack frame_stack;
|
||||
struct stack data_stack;
|
||||
struct frame * current_frame;
|
||||
struct {
|
||||
int length;
|
||||
struct hash_table_entry * entry;
|
||||
} class_hash_table;
|
||||
struct {
|
||||
int length;
|
||||
struct hash_table_entry * entry;
|
||||
} native_hash_table;
|
||||
};
|
||||
|
||||
bool vm_initialize_class(struct vm * vm, struct class_entry * class_entry);
|
||||
void vm_special_method_call(struct vm * vm, struct class_entry * class_entry, struct method_entry * method_entry);
|
||||
void vm_static_method_call(struct vm * vm, struct class_entry * class_entry, struct method_entry * method_entry);
|
||||
|
Loading…
x
Reference in New Issue
Block a user