Skip to content

Commit 9b5d940

Browse files
committed
add ldc2_w support.
1 parent 64777ee commit 9b5d940

File tree

5 files changed

+68
-30
lines changed

5 files changed

+68
-30
lines changed

bytecode.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ BYTECODE jvm_byte_code[OPCODE_LEN] = {
248248
{0x11, 3, "sipush", jvm_interp_sipush},
249249
{0x12, 2, "ldc", jvm_interp_ldc},
250250
{0x13, 1, "ldc_w", jvm_interp_ldc_w},
251-
{0x14, 1, "ldc2_w", jvm_interp_ldc2_w},
251+
{0x14, 3, "ldc2_w", jvm_interp_ldc2_w},
252252
{0x15, 2, "iload", jvm_interp_iload},
253253
{0x16, 2, "lload", jvm_interp_lload},
254254
{0x17, 2, "fload", jvm_interp_fload},

classloader.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -354,8 +354,8 @@ int handle_class_long(CLASS *jvm_class, u2 idx, u1 tag)
354354
return -1;
355355
}
356356

357-
CLASS_READ_U2(long_info->high_bytes, p_mem);
358-
CLASS_READ_U2(long_info->low_bytes, p_mem);
357+
CLASS_READ_U4(long_info->high_bytes, p_mem);
358+
CLASS_READ_U4(long_info->low_bytes, p_mem);
359359

360360
show_class_info("high bytes: %d, low bytes: %d\n",
361361
long_info->high_bytes, long_info->low_bytes);
@@ -476,6 +476,7 @@ int parse_class_constant(CLASS *jvm_class)
476476
case CONSTANT_Long:
477477
if (handle_class_long(jvm_class, idx, constant_tag) == -1)
478478
return -1;
479+
idx++;
479480
break;
480481
case CONSTANT_Integer:
481482
if (handle_class_integer(jvm_class, idx, constant_tag) == -1)
@@ -488,6 +489,7 @@ int parse_class_constant(CLASS *jvm_class)
488489
case CONSTANT_Double:
489490
if (handle_class_double(jvm_class, idx, constant_tag) == -1)
490491
return -1;
492+
idx++;
491493
break;
492494
case CONSTANT_NameAndType:
493495
if (handle_class_name_and_type(jvm_class, idx) == -1)
@@ -1024,8 +1026,8 @@ int init_method_stack(CLASS_CODE *code)
10241026
int stack_size = 0;
10251027
char *stack_base;
10261028

1027-
stack_size = (int)code->max_stack * sizeof(int) +
1028-
(int)code->max_locals * sizeof(int);
1029+
stack_size = (int)code->max_stack * sizeof(void *) +
1030+
(int)code->max_locals * sizeof(void *);
10291031
stack_base = (char *)malloc(stack_size);
10301032
if (!stack_base) {
10311033
jvm_error(VM_ERROR_MEMORY, "Malloc failed.");
@@ -1035,7 +1037,7 @@ int init_method_stack(CLASS_CODE *code)
10351037

10361038
code->stack_frame.local_var_table = (u1 *)stack_base;
10371039
code->stack_frame.operand_stack =
1038-
(u1 *)(stack_base + (int)code->max_locals * sizeof(int));
1040+
(u1 *)(stack_base + (int)code->max_locals * sizeof(void *));
10391041
code->stack_frame.method = code->method;
10401042
code->stack_frame.return_addr = NULL;
10411043
code->stack_frame.offset = 0;

interp_engine.c

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@
3737
print_local(curr_jvm_stack); \
3838
*(type *)(curr_jvm_stack->operand_stack + \
3939
curr_jvm_stack->offset) = (type)value; \
40-
curr_jvm_stack->offset += sizeof(type); \
40+
curr_jvm_stack->offset += sizeof(void *); \
4141
print_local(curr_jvm_stack); \
4242
} while(0);
4343

4444
#define pop_operand_stack(type, value) \
4545
do { \
4646
print_local(curr_jvm_stack); \
47-
curr_jvm_stack->offset -= sizeof(type); \
47+
curr_jvm_stack->offset -= sizeof(void *); \
4848
value = *(type *)(curr_jvm_stack->operand_stack + \
4949
curr_jvm_stack->offset); \
5050
*(type *)(curr_jvm_stack->operand_stack + \
@@ -55,42 +55,42 @@
5555
#define copy_operand_stack(type, value) \
5656
do { \
5757
print_local(curr_jvm_stack); \
58-
curr_jvm_stack->offset -= sizeof(type); \
58+
curr_jvm_stack->offset -= sizeof(void *); \
5959
value = *(type *)(curr_jvm_stack->operand_stack + \
6060
curr_jvm_stack->offset); \
61-
curr_jvm_stack->offset += sizeof(type); \
61+
curr_jvm_stack->offset += sizeof(void *); \
6262
*(type *)(curr_jvm_stack->operand_stack + \
6363
curr_jvm_stack->offset) = (type)value; \
64-
curr_jvm_stack->offset += sizeof(type); \
64+
curr_jvm_stack->offset += sizeof(void *); \
6565
print_local(curr_jvm_stack); \
6666
} while(0);
6767

6868
#define get_local_table(value, type, index) \
6969
do { \
7070
print_local(curr_jvm_stack); \
71-
value = *(type *)(curr_jvm_stack->local_var_table + index * sizeof(type));\
71+
value = *(type *)(curr_jvm_stack->local_var_table + index * sizeof(void *));\
7272
print_local(curr_jvm_stack); \
7373
} while(0);
7474

7575
#define set_local_table(type, index, value) \
7676
do { \
7777
print_local(curr_jvm_stack); \
78-
*(type *)(curr_jvm_stack->local_var_table + index * sizeof(type)) = value;\
78+
*(type *)(curr_jvm_stack->local_var_table + index * sizeof(void *)) = value;\
7979
print_local(curr_jvm_stack); \
8080
} while(0);
8181

8282
#define push_operand_stack_arg(jvm_stack, type, value) \
8383
do { \
8484
print_local(jvm_stack); \
8585
*(type *)(jvm_stack->operand_stack + jvm_stack->offset) = (type)value; \
86-
jvm_stack->offset += sizeof(type); \
86+
jvm_stack->offset += sizeof(void *); \
8787
print_local(jvm_stack); \
8888
} while(0);
8989

9090
#define pop_operand_stack_arg(jvm_stack, type, value) \
9191
do { \
9292
print_local(jvm_stack); \
93-
jvm_stack->offset -= sizeof(type); \
93+
jvm_stack->offset -= sizeof(void *); \
9494
value = *(type *)(jvm_stack->operand_stack + jvm_stack->offset); \
9595
*(type *)(jvm_stack->operand_stack + jvm_stack->offset) = '\0'; \
9696
print_local(jvm_stack); \
@@ -100,7 +100,7 @@
100100
#define set_local_table_arg(jvm_stack, type, index, value) \
101101
do { \
102102
print_local(jvm_stack); \
103-
*(type *)(jvm_stack->local_var_table + index * sizeof(type)) = value; \
103+
*(type *)(jvm_stack->local_var_table + index * sizeof(void *)) = value; \
104104
print_local(jvm_stack); \
105105
} while(0);
106106

@@ -111,10 +111,10 @@ void print_local(JVM_STACK_FRAME *jvm_stack)
111111

112112
printf("#local: ");
113113
for (i = 0; i < jvm_stack->max_locals; i++)
114-
printf("0x%x ", *(int *)(jvm_stack->local_var_table + i * sizeof(int)));
114+
printf("0x%x ", *(int *)(jvm_stack->local_var_table + i * sizeof(void *)));
115115
printf("\t#stack: ");
116116
for (i = 0; i < jvm_stack->max_stack; i++)
117-
printf("0x%x ", *(int *)(jvm_stack->operand_stack + i * sizeof(int)));
117+
printf("0x%x ", *(int *)(jvm_stack->operand_stack + i * sizeof(void *)));
118118
printf("\n");
119119
}
120120
#else
@@ -295,10 +295,28 @@ int jvm_interp_ldc_w(u2 len, char *symbol, void *base)
295295

296296
int jvm_interp_ldc2_w(u2 len, char *symbol, void *base)
297297
{
298+
u1 tmp1, tmp2;
299+
u2 index;
300+
int high_bytes, low_bytes;
301+
long value;
302+
303+
index = (u2)(((*(u1 *)(base + 1)) << 8) | (*(u1 *)(base + 2)));
298304
if (jvm_arg->disass_class) {
299-
printf("%s %x %x\n", symbol, base + 1, base + 3);
305+
show_disassember_code("%s #%x\n", symbol, index);
300306
return 0;
301307
}
308+
309+
debug_vm_interp("%s #%x\n", symbol, index);
310+
high_bytes = ((struct CONSTANT_Long_info *)
311+
curr_jvm_interp_env->constant_info[index].base)->high_bytes;
312+
low_bytes = ((struct CONSTANT_Long_info *)
313+
curr_jvm_interp_env->constant_info[index].base)->low_bytes;
314+
315+
value = ((long)high_bytes << 32) + low_bytes;
316+
push_operand_stack(long, value)
317+
318+
jvm_pc.pc += len;
319+
return 0;
302320
}
303321

304322
#define INTERP_LOAD_VAR(type, index, fmt, ...) \
@@ -1505,6 +1523,7 @@ int jvm_interp_return(u2 len, char *symbol, void *base)
15051523
tmp_env = curr_jvm_interp_env->prev_env;
15061524
memcpy(curr_jvm_interp_env, tmp_env, sizeof(JVM_INTERP_ENV));
15071525
free(tmp_env);
1526+
}
15081527

15091528
jvm_stack_depth--;
15101529
if (jvm_stack_depth == 0) {
@@ -2267,8 +2286,8 @@ int compute_stack_size(struct list_head *list_head)
22672286
list_for_each(s, list_head) {
22682287
p = list_entry(s, CLASS_METHOD, list);
22692288
if (p && p->code_attr) {
2270-
size += (int)p->code_attr->max_stack * sizeof(int);
2271-
size += (int)p->code_attr->max_locals * sizeof(int);
2289+
size += (int)p->code_attr->max_stack * sizeof(void *);
2290+
size += (int)p->code_attr->max_locals * sizeof(void *);
22722291
}
22732292
}
22742293

vm_error.c

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,25 @@
2626
#include "trace.h"
2727
#include "vm_error.h"
2828

29-
void jvm_warning(int flag, char *msg)
29+
void jvm_warning(int flag, char *fmt, ...)
3030
{
31+
va_list arg;
32+
char buf[1024];
33+
34+
va_start(arg, fmt);
35+
vsprintf(buf, fmt, arg);
36+
va_end(arg);
37+
3138
switch (flag) {
3239
case VM_ERROR_CLASS_FILE:
33-
__error(msg);
40+
printf("%s", buf);
3441
break;
3542
case VM_ERROR_MEMORY:
36-
__error(msg);
43+
printf("%s", buf);
3744
break;
45+
case VM_ERROR_INTERP:
46+
printf("%s", buf);
47+
break;
3848
default:
3949
printf("VM Error: Unkown flag.\n");
4050
break;
@@ -43,17 +53,24 @@ void jvm_warning(int flag, char *msg)
4353
calltrace();
4454
}
4555

46-
void jvm_error(int flag, char *msg)
56+
void jvm_error(int flag, char *fmt, ...)
4757
{
58+
va_list arg;
59+
char buf[1024];
60+
61+
va_start(arg, fmt);
62+
vsprintf(buf, fmt, arg);
63+
va_end(arg);
64+
4865
switch (flag) {
4966
case VM_ERROR_CLASS_FILE:
50-
__error(msg);
67+
printf("%s", buf);
5168
break;
5269
case VM_ERROR_MEMORY:
53-
__error(msg);
70+
printf("%s", buf);
5471
break;
5572
case VM_ERROR_INTERP:
56-
__error(msg);
73+
printf("%s", buf);
5774
break;
5875
default:
5976
printf("VM Error: Unkown flag.\n");

vm_error.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#define VM_ERROR_MEMORY 1
66
#define VM_ERROR_INTERP 2
77

8-
void jvm_warning(int flag, char *msg);
9-
void jvm_error(int flag, char *msg);
8+
void jvm_warning(int flag, char *fmt, ...);
9+
void jvm_error(int flag, char *fmt, ...);
1010

1111
#endif

0 commit comments

Comments
 (0)