}
-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 {
} 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);
}
+ (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;
}*/
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;
struct method_info *method;
int ret = -1;
- *opcode = *opcode + 3;
-
if (method_index < methods->length) {
method = methods->data[method_index];
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);
}
}
}
-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))) {
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) {
}
-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;
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);
}
-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;
}
-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 },
{ "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];
}
}
unsigned long i;
struct code_attribute ca;
- opcode_func func;
+ byte_code *byte_code;
memset (&ca, 0, sizeof (ca));
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);
}