From: Robert Pengelly Date: Sat, 27 Sep 2025 09:11:34 +0000 (+0100) Subject: Code improvements and bug fixes X-Git-Url: https://git.candlhat.org/?a=commitdiff_plain;h=1cd3ae3c91190129648ca8700479026420c9e40f;p=lightjvm.git Code improvements and bug fixes --- diff --git a/stack.c b/stack.c index d3312cb..de3e951 100644 --- a/stack.c +++ b/stack.c @@ -30,7 +30,7 @@ struct stack_frame *stack_init (int entry_size) { memset (&stack_frame->store[i], 0, sizeof (stack_frame->store[i])); } - stack_frame->size = entry_size; + stack_frame->index = entry_size; stack_frame->max_size = entry_size; return stack_frame; @@ -40,7 +40,7 @@ struct stack_frame *stack_init (int entry_size) { void push_double (struct stack_frame *stack, double value) { - struct stack_entry *entry = &stack->store[--stack->size]; + struct stack_entry *entry = &stack->store[--stack->index]; memset (entry, 0, sizeof (*entry)); memcpy (entry->entry, &value, sizeof (double)); @@ -50,7 +50,7 @@ void push_double (struct stack_frame *stack, double value) { void push_int (struct stack_frame *stack, int value) { - struct stack_entry *entry = &stack->store[--stack->size]; + struct stack_entry *entry = &stack->store[--stack->index]; memset (entry, 0, sizeof (*entry)); write_to_byte_array (entry->entry, value, sizeof (int), 1); @@ -60,7 +60,7 @@ void push_int (struct stack_frame *stack, int value) { void push_ref (struct stack_frame *stack, int value) { - struct stack_entry *entry = &stack->store[--stack->size]; + struct stack_entry *entry = &stack->store[--stack->index]; memset (entry, 0, sizeof (*entry)); write_to_byte_array (entry->entry, value, sizeof (int), 1); @@ -70,9 +70,21 @@ void push_ref (struct stack_frame *stack, int value) { double pop_double (struct stack_frame *stack) { - struct stack_entry *entry = &stack->store[stack->size++]; - + struct stack_entry *entry; double value = 0; + + if (stack->index == stack->max_size) { + + if ((stack->store = realloc (stack->store, sizeof (*stack->store) * (stack->index + 1)))) { + + memset (&stack->store[stack->index], 0, sizeof (stack->store[stack->index])); + stack->max_size++; + + } + + } + + entry = &stack->store[stack->index++]; memcpy (&value, entry->entry, sizeof (double)); return value; @@ -81,14 +93,39 @@ double pop_double (struct stack_frame *stack) { int pop_int (struct stack_frame *stack) { - struct stack_entry *entry = &stack->store[stack->size++]; + struct stack_entry *entry; + + if (stack->index == stack->max_size) { + + if ((stack->store = realloc (stack->store, sizeof (*stack->store) * (stack->index + 1)))) { + + memset (&stack->store[stack->index], 0, sizeof (stack->store[stack->index])); + stack->max_size++; + + } + + } + + entry = &stack->store[stack->index++]; return array_to_integer (entry->entry, 4, 1); } - struct stack_entry *pop_entry (struct stack_frame *stack) { - return &stack->store[stack->size++]; + + if (stack->index == stack->max_size) { + + if ((stack->store = realloc (stack->store, sizeof (*stack->store) * (stack->index + 1)))) { + + memset (&stack->store[stack->index], 0, sizeof (stack->store[stack->index])); + stack->max_size++; + + } + + } + + return &stack->store[stack->index++]; + } int entry_to_int (struct stack_entry *entry) { @@ -96,5 +133,5 @@ int entry_to_int (struct stack_entry *entry) { } int is_ref_entry (struct stack_frame *stack) { - return (stack->store[stack->size].type == STACK_ENTRY_REF); + return (stack->store[stack->index].type == STACK_ENTRY_REF); } diff --git a/stack.h b/stack.h index 4df831d..772e124 100644 --- a/stack.h +++ b/stack.h @@ -20,7 +20,7 @@ struct stack_entry { struct stack_frame { - int max_size, size; + int max_size, index; struct stack_entry *store; }; diff --git a/vm.c b/vm.c index 95356ae..429f4f4 100644 --- a/vm.c +++ b/vm.c @@ -37,7 +37,7 @@ static double get_double_parameter (struct stack_frame *stack, struct constant_p } -typedef int (*opcode_func) (unsigned char **opcode, struct stack_frame *stack, struct java_class *java_class); +typedef int (*opcode_func) (unsigned char *opcode, struct stack_frame *stack, struct java_class *java_class); typedef struct { @@ -50,71 +50,73 @@ typedef struct { } byte_code; -static int op_aload_0 (unsigned char **opcode, struct stack_frame *stack, struct java_class *java_class) { +static int op_aload_0 (unsigned char *opcode, struct stack_frame *stack, struct java_class *java_class) { push_int (stack, 0); + + (void) opcode; (void) java_class; - *opcode = *opcode + 1; return 0; } -static int op_bipush (unsigned char **opcode, struct stack_frame *stack, struct java_class *java_class) { +static int op_bipush (unsigned char *opcode, struct stack_frame *stack, struct java_class *java_class) { - push_int (stack, opcode[0][1]); - (void) java_class; + push_int (stack, opcode[1]); - *opcode = *opcode + 2; + (void) java_class; return 0; } -static int op_d2i (unsigned char **opcode, struct stack_frame *stack, struct java_class *java_class) { +static int op_d2i (unsigned char *opcode, struct stack_frame *stack, struct java_class *java_class) { push_int (stack, (int) pop_double (stack)); + + (void) opcode; (void) java_class; - *opcode = *opcode + 1; return 0; } -static int op_dadd (unsigned char **opcode, struct stack_frame *stack, struct java_class *java_class) { +static int op_dadd (unsigned char *opcode, struct stack_frame *stack, struct java_class *java_class) { double value1 = get_double_parameter (stack, java_class->constant_pool); double value2 = get_double_parameter (stack, java_class->constant_pool); push_double (stack, value1 + value2); - *opcode = *opcode + 1; + (void) opcode; return 0; } -static int op_dconst_1 (unsigned char **opcode, struct stack_frame *stack, struct java_class *java_class) { +static int op_dconst_1 (unsigned char *opcode, struct stack_frame *stack, struct java_class *java_class) { push_double (stack, 1.0f); + + (void) opcode; (void) java_class; - *opcode = *opcode + 1; return 0; } -static int op_dmul (unsigned char **opcode, struct stack_frame *stack, struct java_class *java_class) { +static int op_dmul (unsigned char *opcode, struct stack_frame *stack, struct java_class *java_class) { double value1 = get_double_parameter (stack, java_class->constant_pool); double value2 = get_double_parameter (stack, java_class->constant_pool); push_double (stack, value1 * value2); - *opcode = *opcode + 1; + (void) opcode; return 0; } -static int op_dup (unsigned char **opcode, struct stack_frame *stack, struct java_class *java_class) { +static int op_dup (unsigned char *opcode, struct stack_frame *stack, struct java_class *java_class) { struct stack_entry *entry = pop_entry (stack); int value = entry_to_int (entry); @@ -131,16 +133,16 @@ static int op_dup (unsigned char **opcode, struct stack_frame *stack, struct jav } + (void) opcode; (void) java_class; - *opcode = *opcode + 1; return 0; } -static int op_getstatic (unsigned char **opcode, struct stack_frame *stack, struct java_class *java_class) { +static int op_getstatic (unsigned char *opcode, struct stack_frame *stack, struct java_class *java_class) { - unsigned short field_index = (opcode[0][1] << 8) | opcode[0][2]; + unsigned short field_index = (opcode[1] << 8) | opcode[2]; /*struct constant_pool *p = java_class->constant_pool; struct vector *field_refs = &p->vec_field_refs; @@ -161,245 +163,244 @@ static int op_getstatic (unsigned char **opcode, struct stack_frame *stack, stru }*/ push_ref (stack, field_index); - (void) java_class; - *opcode = *opcode + 3; + (void) java_class; return 0; } -static int op_iadd (unsigned char **opcode, struct stack_frame *stack, struct java_class *java_class) { +static int op_iadd (unsigned char *opcode, struct stack_frame *stack, struct java_class *java_class) { - int result = pop_int (stack) + pop_int (stack); + push_int (stack, pop_int (stack) + pop_int (stack)); - push_int (stack, result); + (void) opcode; (void) java_class; - *opcode = *opcode + 1; return 0; } -static int op_iconst_0 (unsigned char **opcode, struct stack_frame *stack, struct java_class *java_class) { +static int op_iconst_0 (unsigned char *opcode, struct stack_frame *stack, struct java_class *java_class) { push_int (stack, 0); + + (void) opcode; (void) java_class; - *opcode = *opcode + 1; return 0; } -static int op_iconst_1 (unsigned char **opcode, struct stack_frame *stack, struct java_class *java_class) { +static int op_iconst_1 (unsigned char *opcode, struct stack_frame *stack, struct java_class *java_class) { push_int (stack, 1); + + (void) opcode; (void) java_class; - *opcode = *opcode + 1; return 0; } -static int op_iconst_2 (unsigned char **opcode, struct stack_frame *stack, struct java_class *java_class) { +static int op_iconst_2 (unsigned char *opcode, struct stack_frame *stack, struct java_class *java_class) { push_int (stack, 2); + + (void) opcode; (void) java_class; - *opcode = *opcode + 1; return 0; } -static int op_iconst_3 (unsigned char **opcode, struct stack_frame *stack, struct java_class *java_class) { +static int op_iconst_3 (unsigned char *opcode, struct stack_frame *stack, struct java_class *java_class) { push_int (stack, 3); + + (void) opcode; (void) java_class; - *opcode = *opcode + 1; return 0; } -static int op_iconst_4 (unsigned char **opcode, struct stack_frame *stack, struct java_class *java_class) { +static int op_iconst_4 (unsigned char *opcode, struct stack_frame *stack, struct java_class *java_class) { push_int (stack, 4); + + (void) opcode; (void) java_class; - *opcode = *opcode + 1; return 0; } -static int op_iconst_5 (unsigned char **opcode, struct stack_frame *stack, struct java_class *java_class) { +static int op_iconst_5 (unsigned char *opcode, struct stack_frame *stack, struct java_class *java_class) { push_int (stack, 5); + + (void) opcode; (void) java_class; - *opcode = *opcode + 1; return 0; } -static int op_idiv (unsigned char **opcode, struct stack_frame *stack, struct java_class *java_class) { +static int op_idiv (unsigned char *opcode, struct stack_frame *stack, struct java_class *java_class) { int value2 = pop_int (stack), value1 = pop_int (stack); - push_int (stack, value1 / value2); + + (void) opcode; (void) java_class; - *opcode = *opcode + 1; return 0; } -static int op_iload (unsigned char **opcode, struct stack_frame *stack, struct java_class *java_class) { +static int op_iload (unsigned char *opcode, struct stack_frame *stack, struct java_class *java_class) { - push_int (stack, local_variables.integer[opcode[0][1]]); - (void) java_class; + push_int (stack, local_variables.integer[opcode[1]]); - *opcode = *opcode + 2; + (void) java_class; return 0; } -static int op_iload_1 (unsigned char **opcode, struct stack_frame *stack, struct java_class *java_class) { +static int op_iload_1 (unsigned char *opcode, struct stack_frame *stack, struct java_class *java_class) { push_int (stack, local_variables.integer[1]); + + (void) opcode; (void) java_class; - *opcode = *opcode + 1; return 0; } -static int op_iload_2 (unsigned char **opcode, struct stack_frame *stack, struct java_class *java_class) { +static int op_iload_2 (unsigned char *opcode, struct stack_frame *stack, struct java_class *java_class) { push_int (stack, local_variables.integer[2]); + + (void) opcode; (void) java_class; - *opcode = *opcode + 1; return 0; } -static int op_iload_3 (unsigned char **opcode, struct stack_frame *stack, struct java_class *java_class) { +static int op_iload_3 (unsigned char *opcode, struct stack_frame *stack, struct java_class *java_class) { push_int (stack, local_variables.integer[3]); + + (void) opcode; (void) java_class; - *opcode = *opcode + 1; return 0; } -static int op_imul (unsigned char **opcode, struct stack_frame *stack, struct java_class *java_class) { +static int op_imul (unsigned char *opcode, struct stack_frame *stack, struct java_class *java_class) { int result = pop_int (stack) * pop_int (stack); - push_int (stack, result); + + (void) opcode; (void) java_class; - *opcode = *opcode + 1; return 0; } -/*static int op_invokedynamic (unsigned char **opcode, struct stack_frame *stack, struct java_class *java_class) { +static int op_invokedynamic (unsigned char *opcode, struct stack_frame *stack, struct java_class *java_class) { - struct constant_pool *p = java_class->constant_pool; - unsigned short method_index = (opcode[0][1] << 8) | opcode[0][2]; - - struct vector *methods = &p->vec_invoke_dynamic; + unsigned short method_index = (opcode[1] << 8) | opcode[2]; struct constant_invoke_dynamic *method; - *opcode = *opcode + 5; + (void) stack; - if ((method = find_invoke_dynamic (p, method_index))) { + if ((method = find_invoke_dynamic (java_class->constant_pool, method_index))) { struct constant_nat *nat; char name[255]; - if ((nat = find_nat (p, array_to_integer (method->nat_index, 2, 1)))) { + if ((nat = find_nat (java_class->constant_pool, array_to_integer (method->nat_index, 2, 1)))) { - get_utf8_string (p, array_to_integer (nat->name_index, 2, 1), 255, name); + get_utf8_string (java_class->constant_pool, array_to_integer (nat->name_index, 2, 1), 255, name); printf ("YAYAY: %s\n", name); } } - (void) stack; - - if (method_index < methods->length) { - - if ((method = methods->data[method_index])) { - return execute_method (method, stack, java_class); - } - - } - - if (vec_classes.length > 0) { - - java_class = vec_classes.data[vec_classes.length - 1]; - - p = java_class->constant_pool; - method_refs = java_class->method_pool; - - if (method_index < method_refs->length) { - - if ((method = method_refs->data[method_index])) { - return execute_method (method, stack, java_class); - } - - } - - } - - return 0; + return -1; -}*/ +} -static int op_invokespecial (unsigned char **opcode, struct stack_frame *stack, struct java_class *java_class) { +static int op_invokespecial (unsigned char *opcode, struct stack_frame *stack, struct java_class *java_class) { - unsigned short method_index = (opcode[0][1] << 8) | opcode[0][2]; - - struct vector *methods = java_class->method_pool; - struct method_info *method; - - *opcode = *opcode + 3; - - if (method_index < methods->length) { + unsigned short method_index = (opcode[1] << 8) | opcode[2]; - if ((method = methods->data[method_index])) { - return execute_method (method, stack, java_class); - } + struct constant_method_ref *method_ref; + struct constant_nat *nat; - } + struct java_class *class; - /*if (vec_classes.length > 0) { + if ((method_ref = find_method_ref (java_class->constant_pool, method_index))) { - java_class = vec_classes.data[vec_classes.length - 1]; + struct stack_entry *entry = pop_entry (stack); - p = java_class->constant_pool; - method_refs = java_class->method_pool; + struct constant_field_ref *field_ref; + struct method_info *method; - if (method_index < method_refs->length) { + char class_name[255]; + char method_name[255]; - if ((method = method_refs->data[method_index])) { - return execute_method (method, stack, java_class); + if ((nat = find_nat (java_class->constant_pool, array_to_integer (method_ref->nat_index, 2, 1)))) { + + get_utf8_string (java_class->constant_pool, array_to_integer (nat->name_index, 2, 1), 255, method_name); + + if (entry->type == STACK_ENTRY_REF) { + + int index = entry_to_int (entry); + + if ((field_ref = find_field_ref (java_class->constant_pool, index))) { + + struct constant_class_ref *class_ref; + + if ((class_ref = find_class_ref (java_class->constant_pool, array_to_integer (field_ref->class_index, 2, 1)))) { + + get_utf8_string (java_class->constant_pool, array_to_integer (class_ref->string_index, 2, 1), 255, class_name); + + if (strcmp (class_name, "java/lang/System") == 0) { + return 0; + } + + if ((class = parse_java_class (class_name))) { + + if ((method = find_method_in_pool (class->constant_pool, class->method_pool, 0, method_name, strlen (method_name)))) { + return execute_method (method, stack, class); + } + + } + + } + + } + } } - }*/ + } - return 0; + return -1; } -static int op_invokestatic (unsigned char **opcode, struct stack_frame *stack, struct java_class *java_class) { +static int op_invokestatic (unsigned char *opcode, struct stack_frame *stack, struct java_class *java_class) { - unsigned short method_index = (opcode[0][1] << 8) | opcode[0][2]; + unsigned short method_index = (opcode[1] << 8) | opcode[2]; struct constant_pool *p = java_class->constant_pool; struct vector *methods = java_class->method_pool; @@ -411,8 +412,6 @@ static int op_invokestatic (unsigned char **opcode, struct stack_frame *stack, s struct method_info *method; int ret = -1; - *opcode = *opcode + 3; - if (method_index < methods->length) { method = methods->data[method_index]; @@ -437,10 +436,14 @@ static int op_invokestatic (unsigned char **opcode, struct stack_frame *stack, s if ((ret = invoke_java_lang_library (p, stack, class_name, method_name))) { - struct java_class *class = parse_java_class (class_name); + struct java_class *class; + + if ((class = parse_java_class (class_name))) { + + if ((method = find_method_in_pool (class->constant_pool, class->method_pool, ACC_STATIC, method_name, strlen (method_name)))) { + ret = execute_method (method, stack, class); + } - if ((method = find_method_in_pool (class->constant_pool, class->method_pool, ACC_STATIC, method_name, strlen (method_name)))) { - ret = execute_method (method, stack, class); } } @@ -455,17 +458,16 @@ static int op_invokestatic (unsigned char **opcode, struct stack_frame *stack, s } -static int op_invokevirtual (unsigned char **opcode, struct stack_frame *stack, struct java_class *java_class) { +static int op_invokevirtual (unsigned char *opcode, struct stack_frame *stack, struct java_class *java_class) { struct constant_pool *p = java_class->constant_pool; - unsigned short object_ref = (opcode[0][1] << 8) | opcode[0][2]; + unsigned short object_ref = (opcode[1] << 8) | opcode[2]; char class_name[255]; char utf8[255]; char method_name[255]; struct constant_method_ref *method_ref; - *opcode = *opcode + 3; if ((method_ref = find_method_ref (p, object_ref))) { @@ -540,26 +542,15 @@ static int op_invokevirtual (unsigned char **opcode, struct stack_frame *stack, if (entry->type == STACK_ENTRY_REF) { - struct java_class *class = parse_java_class (class_name); - - /*struct constant_field_ref *ref; - char temp[255]; + struct java_class *class; - printf ("index: %d\n", index);*/ + if ((class = parse_java_class (class_name))) { - /*if ((ref = find_field_ref (p, index))) {*/ - - /*printf ("FUCKKKK! %ld\n", array_to_integer (ref->class_index, 2, 1)); - exit (1);*/ - - /*get_utf8_string (p, array_to_integer (ref->string_index, 2, 1), 255, temp);*/ - /*printf ("%s\n", temp);*/ - if ((method = find_method_in_pool (class->constant_pool, class->method_pool, 0, method_name, strlen (method_name)))) { return execute_method (method, stack, class); } - /*}*/ + } } else if (entry->type == STACK_ENTRY_INT) { @@ -589,95 +580,94 @@ static int op_invokevirtual (unsigned char **opcode, struct stack_frame *stack, } -static int op_irem (unsigned char **opcode, struct stack_frame *stack, struct java_class *java_class) { +static int op_irem (unsigned char *opcode, struct stack_frame *stack, struct java_class *java_class) { int value1 = pop_int (stack), value2 = pop_int (stack); - push_int (stack, value2 % value1); + + (void) opcode; (void) java_class; - *opcode = *opcode + 1; return 0; } -static int op_istore (unsigned char **opcode, struct stack_frame *stack, struct java_class *java_class) { +static int op_istore (unsigned char *opcode, struct stack_frame *stack, struct java_class *java_class) { - local_variables.integer[opcode[0][1]] = pop_int (stack); - (void) java_class; + local_variables.integer[opcode[1]] = pop_int (stack); - *opcode = *opcode + 2; + (void) java_class; return 0; } -static int op_istore_1 (unsigned char **opcode, struct stack_frame *stack, struct java_class *java_class) { +static int op_istore_1 (unsigned char *opcode, struct stack_frame *stack, struct java_class *java_class) { local_variables.integer[1] = pop_int (stack); + + (void) opcode; (void) java_class; - *opcode = *opcode + 1; return 0; } -static int op_istore_2 (unsigned char **opcode, struct stack_frame *stack, struct java_class *java_class) { +static int op_istore_2 (unsigned char *opcode, struct stack_frame *stack, struct java_class *java_class) { local_variables.integer[2] = pop_int (stack); + + (void) opcode; (void) java_class; - *opcode = *opcode + 1; return 0; } -static int op_istore_3 (unsigned char **opcode, struct stack_frame *stack, struct java_class *java_class) { +static int op_istore_3 (unsigned char *opcode, struct stack_frame *stack, struct java_class *java_class) { local_variables.integer[3] = pop_int (stack); + + (void) opcode; (void) java_class; - *opcode = *opcode + 1; return 0; } -static int op_isub (unsigned char **opcode, struct stack_frame *stack, struct java_class *java_class) { +static int op_isub (unsigned char *opcode, struct stack_frame *stack, struct java_class *java_class) { int value2 = pop_int (stack), value1 = pop_int (stack); - push_int (stack, value1 - value2); + + (void) opcode; (void) java_class; - *opcode = *opcode + 1; return 0; } -static int op_ldc (unsigned char **opcode, struct stack_frame *stack, struct java_class *java_class) { +static int op_ldc (unsigned char *opcode, struct stack_frame *stack, struct java_class *java_class) { - push_ref (stack, opcode[0][1]); - *opcode = *opcode + 2; + push_ref (stack, opcode[1]); (void) java_class; return 0; } -static int op_ldc2_w (unsigned char **opcode, struct stack_frame *stack, struct java_class *java_class) { +static int op_ldc2_w (unsigned char *opcode, struct stack_frame *stack, struct java_class *java_class) { - int index = (opcode[0][1] << 8) | opcode[0][2]; - + int index = (opcode[1] << 8) | opcode[2]; push_ref (stack, index); - *opcode = *opcode + 3; (void) java_class; return 0; } -static int op_new (unsigned char **opcode, struct stack_frame *stack, struct java_class *java_class) { +static int op_new (unsigned char *opcode, struct stack_frame *stack, struct java_class *java_class) { - unsigned short object_ref = (opcode[0][1] << 8) | opcode[0][2]; + unsigned short object_ref = (opcode[1] << 8) | opcode[2]; struct constant_class_ref *ref; struct java_class *class; @@ -688,8 +678,6 @@ static int op_new (unsigned char **opcode, struct stack_frame *stack, struct jav unsigned long count = (sizeof (java_lib) / sizeof (java_lib[0])); unsigned long i; - *opcode = *opcode + 3; - if ((ref = find_class_ref (java_class->constant_pool, object_ref))) { get_utf8_string (java_class->constant_pool, array_to_integer (ref->string_index, 2, 1), 255, class_name); @@ -713,10 +701,9 @@ static int op_new (unsigned char **opcode, struct stack_frame *stack, struct jav } -static int op_return (unsigned char **opcode, struct stack_frame *stack, struct java_class *java_class) { +static int op_return (unsigned char *opcode, struct stack_frame *stack, struct java_class *java_class) { - *opcode = *opcode + 1; - + (void) opcode; (void) stack; (void) java_class; @@ -724,23 +711,17 @@ static int op_return (unsigned char **opcode, struct stack_frame *stack, struct } -static int op_sipush (unsigned char **opcode, struct stack_frame *stack, struct java_class *java_class) { +static int op_sipush (unsigned char *opcode, struct stack_frame *stack, struct java_class *java_class) { - push_int (stack, (opcode[0][1] << 8) | opcode[0][2]); - (void) java_class; + push_int (stack, (opcode[1] << 8) | opcode[2]); - *opcode = *opcode + 3; + (void) java_class; return 0; } static byte_code opcodes[] = { - { "aload_0", 0x2A, 1, op_aload_0 }, - { "bipush", 0x10, 2, op_bipush }, - { "dup", 0x59, 1, op_dup }, - { "getstatic", 0xB2, 3, op_getstatic }, - { "iadd", 0x60, 1, op_iadd }, { "iconst_0", 0x03, 1, op_iconst_0 }, { "iconst_1", 0x04, 1, op_iconst_1 }, { "iconst_2", 0x05, 1, op_iconst_2 }, @@ -748,43 +729,48 @@ static byte_code opcodes[] = { { "iconst_4", 0x07, 1, op_iconst_4 }, { "iconst_5", 0x08, 1, op_iconst_5 }, { "dconst_1", 0x0F, 1, op_dconst_1 }, - { "idiv", 0x6C, 1, op_idiv }, - { "imul", 0x68, 1, op_imul }, - { "dadd", 0x63, 1, op_dadd }, - { "dmul", 0x6B, 1, op_dmul }, - { "d2i", 0x8e, 1, op_d2i }, - { "invokespecial", 0xB7, 3, op_invokespecial }, - { "invokevirtual", 0xB6, 3, op_invokevirtual }, - { "invokestatic", 0xB8, 3, op_invokestatic }, - /*{ "invokedynamic", 0xBA, 3, op_invokedynamic },*/ + { "bipush", 0x10, 2, op_bipush }, + { "sipush", 0x11, 3, op_sipush }, + { "ldc", 0x12, 2, op_ldc }, + { "ldc2_w", 0x14, 3, op_ldc2_w }, { "iload", 0x15, 2, op_iload }, { "iload_1", 0x1B, 1, op_iload_1 }, { "iload_2", 0x1C, 1, op_iload_2 }, { "iload_3", 0x1D, 1, op_iload_3 }, + { "aload_0", 0x2A, 1, op_aload_0 }, { "istore", 0x36, 2, op_istore }, { "istore_1", 0x3C, 1, op_istore_1 }, { "istore_2", 0x3D, 1, op_istore_2 }, { "istore_3", 0x3E, 1, op_istore_3 }, + { "dup", 0x59, 1, op_dup }, + { "iadd", 0x60, 1, op_iadd }, + { "dadd", 0x63, 1, op_dadd }, { "isub", 0x64, 1, op_isub }, - { "ldc", 0x12, 2, op_ldc }, - { "ldc2_w", 0x14, 3, op_ldc2_w }, - { "new", 0xBB, 3, op_new }, + { "imul", 0x68, 1, op_imul }, + { "dmul", 0x6B, 1, op_dmul }, + { "idiv", 0x6C, 1, op_idiv }, { "irem", 0x70, 1, op_irem }, - { "sipush", 0x11, 3, op_sipush }, - { "return", 0xB1, 1, op_return } + { "d2i", 0x8E, 1, op_d2i }, + { "return", 0xB1, 1, op_return }, + { "getstatic", 0xB2, 3, op_getstatic }, + { "invokevirtual", 0xB6, 3, op_invokevirtual }, + { "invokespecial", 0xB7, 3, op_invokespecial }, + { "invokestatic", 0xB8, 3, op_invokestatic }, + { "invokedynamic", 0xBA, 5, op_invokedynamic }, + { "new", 0xBB, 3, op_new } }; static unsigned long opcodes_size = (sizeof (opcodes) / sizeof (opcodes[0])); -static opcode_func find_opcode_func (unsigned char op) { +static byte_code *find_byte_code (unsigned char op) { unsigned long i; for (i = 0; i < opcodes_size; i++) { if (opcodes[i].opcode == op) { - return opcodes[i].func; + return &opcodes[i]; } } @@ -827,7 +813,7 @@ int execute_method (struct method_info *method, struct stack_frame *stack, struc unsigned long i; struct code_attribute ca; - opcode_func func; + byte_code *byte_code; memset (&ca, 0, sizeof (ca)); @@ -847,15 +833,19 @@ int execute_method (struct method_info *method, struct stack_frame *stack, struc for (;;) { - if (!(func = find_opcode_func (pc[0]))) { + if (!(byte_code = find_byte_code (pc[0]))) { break; } - if (func (&pc, stack, java_class)) { + if (byte_code->func (pc, stack, java_class)) { break; } + + pc += byte_code->offset; } + + free (ca.code); }