Implement LDC2_W bytecode
[jamvm-avr32-jem.git] / src / interp / engine / interp_jem.c
blobd0982fc636853197a273f83bd3801670e2670aa1
1 /*
2 * Copyright (C) 2003, 2004, 2005, 2006, 2007
3 * Robert Lougher <rob@lougher.org.uk>.
5 * This file is part of JamVM.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2,
10 * or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #define _GNU_SOURCE
23 #include <arpa/inet.h>
24 #include <math.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
30 #include "jam.h"
31 #include "thread.h"
32 #include "lock.h"
33 #include "interp.h"
35 #include "interp-direct.h"
37 #include <sys/syscall.h>
38 #include <unistd.h>
39 #include <sys/mman.h>
41 #include <sys/types.h>
42 #include <linux/unistd.h>
44 #include <asm/byteorder.h>
45 #include <asm/cachectl.h>
47 #include "arch/avr32_jem.h"
48 #include "interp_jem.h"
50 union jecr {
51 struct {
52 uint8_t dummy;
53 uint8_t opcode;
54 uint8_t op1;
55 uint8_t op2;
56 } s;
57 uint32_t i;
60 /**
61 * Interpreter execution context
63 struct intrp_ctx {
64 /* JEM state */
65 struct jem_state jem;
67 /* the interpreter execution context */
68 Frame *frame;
69 MethodBlock *mb;
70 MethodBlock *new_mb;
71 ConstantPool *cp;
72 Object *this;
73 uintptr_t *lvars_jem;
74 uintptr_t *ostack;
75 uintptr_t *arg1;
76 ExecEnv *ee;
79 #define GET_THIS(ctx) do { \
80 if (!((ctx)->mb->access_flags & ACC_STATIC) && (ctx)->mb->max_locals && \
81 *(ctx)->lvars_jem) { \
82 uintptr_t *__p = (uintptr_t *)*(ctx)->lvars_jem; \
83 (ctx)->this = JAM_OBJECT(__p); \
84 } else \
85 (ctx)->this = NULL; \
86 jam_dprintf("[%s] %smethod with %d vars, assigning %p\n", __func__, \
87 (ctx)->mb->access_flags & ACC_STATIC ? "static " : "", \
88 (ctx)->mb->max_locals, (ctx)->this); \
89 } while (0)
91 typedef int handler_fn(struct intrp_ctx *ctx);
93 static void jem_opcode_rewrite(uint32_t new_opcode, char *pc, int argc, ...)
95 int i;
96 va_list vargs;
98 va_start(vargs, argc);
99 pc[0] = new_opcode;
100 for (i = 1; i <= argc; i++)
101 pc[i] = (char)va_arg(vargs, int);
102 va_end(vargs);
103 syscall(__NR_cacheflush, CACHE_IFLUSH, (int)pc & ~31, 32);
106 #define min(x, y) ({ typeof(x) _x = (x); typeof(y) _y = (y); _x > _y ? _y : _x; })
108 //check if it is an valid object ref (see alloc.c)
109 #define OBJECT_GRAIN 8
110 #define IS_OBJECT(ptr) !(((uintptr_t)(ptr))&(OBJECT_GRAIN-1))
112 #define JEM_THROW_EXCEPTION(excep_name, message) \
113 ({ \
114 ctx->frame->last_pc = (CodePntr)ctx->jem.jpc; \
115 signalException(excep_name, message); \
116 jem_throwException(ctx); \
119 #define JEM_NULL_POINTER_CHECK(ref) \
120 if (!ref) JEM_THROW_EXCEPTION("java/lang/NullPointerException", NULL);
122 /*********************************************
123 * JEM Trap Handlers **
124 * *******************************************/
125 static int trap_debug(struct intrp_ctx *ctx)
127 #ifdef JEM_DEBUG
128 struct jem_state *jem = &ctx->jem;
129 union jecr jecr_u;
130 int i;
131 static unsigned long count;
133 jecr_u.i = jem->jecr;
135 jam_printf("[%lu] (%u): Trap 0x%08x: ostack 0x%x, opcode 0x%x @ 0x%08x, "
136 "operand 1 0x%x, operand 2 0x%x. Operands:\n",
137 count++, pthread_self(), jem->trap_pc, ctx->ostack, jecr_u.s.opcode,
138 jem->jpc, jecr_u.s.op1, jecr_u.s.op2);
140 for (i = 0; i < jem->josp; i++) {
141 uint32_t v;
142 if (ostack_read_u32(ctx->frame->ostack, ctx->ostack, i, &v) >= 0)
143 jam_printf("\tToS-%d: 0x%x\n", i, v);
145 #endif
146 return 1;
150 static int jem_throwException(struct intrp_ctx *ctx)
152 struct jem_state *jem = &ctx->jem;
153 ExecEnv *ee = ctx->ee;
154 Object *excep = ee->exception;
155 ee->exception = NULL;
157 jem->jpc = (unsigned long)findCatchBlock(excep->class);
158 jam_dprintf("Found exception handler at 0x%08x\n", jem->jpc);
160 /* If we didn't find a handler, restore exception and
161 return to previous invocation */
163 if (!jem->jpc) {
164 ee->exception = excep;
165 /* Original code had a "return NULL" here, which means, end of
166 * executeJava */
167 return JEM_TRAP_HANDLER_FINISH;
170 /* If we're handling a stack overflow, reduce the stack
171 back past the red zone to enable handling of further
172 overflows */
174 if (ee->overflow) {
175 ee->overflow = FALSE;
176 ee->stack_end -= STACK_RED_ZONE_SIZE;
179 /* Setup intepreter to run the found catch block */
181 ctx->frame = ee->last_frame;
182 ctx->mb = ctx->frame->mb;
183 ctx->ostack = ctx->frame->ostack;
184 ctx->lvars_jem = ctx->frame->lvars_jem - 1;
186 GET_THIS(ctx);
188 ctx->cp = &(CLASS_CB(ctx->mb->class)->constant_pool);
190 /* FIXME: don't know if this is correct, depends how we implement
191 * exceptions */
192 ostack_push_u32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack, (uint32_t)excep);
194 /* Dispatch to the first bytecode */
195 return 0;
198 static int opc_return(struct intrp_ctx *ctx)
200 ExecEnv *ee = ctx->ee;
202 /* Set interpreter state to previous frame */
203 if (ctx->frame->prev->mb == NULL) {
204 /* The previous frame is a dummy frame - this indicates
205 * top of this Java invocation. */
206 return JEM_TRAP_HANDLER_FINISH;
209 ctx->frame = ctx->frame->prev;
211 jam_dprintf("[%s] return to frame %p mb %p\n", __func__, ctx->frame, ctx->frame->mb);
213 if (ctx->mb->access_flags & ACC_SYNCHRONIZED) {
214 Object *sync_ob = ctx->mb->access_flags & ACC_STATIC ? (Object*)ctx->mb->class : ctx->this;
215 jam_dprintf("[%s] %smethod unlock obj %p\n", __func__,
216 ctx->mb->access_flags & ACC_STATIC ? "static " : "", sync_ob);
217 objectUnlock(sync_ob);
220 ctx->ostack = ctx->lvars_jem - ctx->mb->max_locals + 1 - ctx->mb->args_count;
221 ctx->mb = ctx->frame->mb;
222 ctx->lvars_jem = ctx->frame->lvars_jem - 1;
224 GET_THIS(ctx);
226 ctx->jem.jpc = (uint32_t)ctx->frame->last_pc;
227 ctx->cp = &(CLASS_CB(ctx->mb->class)->constant_pool);
228 jam_dprintf("[OPC_RETURN] ostack: %p, lvars_jem: %p this: %p class %p\n",
229 ctx->ostack, ctx->lvars_jem, ctx->this,
230 ctx->this ? ctx->this->class : NULL);
232 /* Pop frame */
233 ee->last_frame = ctx->frame;
235 return 0;
238 static int invokeMethod(struct intrp_ctx *ctx)
240 ExecEnv *ee = ctx->ee;
241 /* Seems like the original jamvm reused top of caller's stack for callee's
242 * local variables for passing of method arguments. Our stack and
243 * local variables grow in opposite directions now, so, this is impossible.
244 * ctx->ostack points at the beginning of the free space above ostack.
246 Frame *new_frame = (Frame *)(ctx->ostack + ctx->new_mb->max_locals);
247 Object *sync_ob = NULL;
248 int i;
249 uintptr_t *aobj;
251 jam_dprintf("[invokeMethod] with args count %d, frame %p, return to 0x%08x\n",
252 ctx->new_mb->args_count, new_frame, ctx->jem.jpc);
254 ctx->frame->last_pc = (CodePntr)ctx->jem.jpc;
255 ctx->ostack = (uintptr_t *)(new_frame + 1);
257 if (ostack_overflows(ctx->ostack, ctx->new_mb->max_stack, (uintptr_t *)ee->stack_end)) {
258 if (ee->overflow++) {
259 /* Overflow when we're already throwing stack overflow.
260 Stack extension should be enough to throw exception,
261 so something's seriously gone wrong - abort the VM! */
262 jam_printf("Fatal stack overflow! Aborting VM.\n");
263 exitVM(1);
265 ee->stack_end += STACK_RED_ZONE_SIZE;
266 return JEM_THROW_EXCEPTION("java/lang/StackOverflowError", NULL);
269 //TODO
270 new_frame->mb = ctx->new_mb;
271 //TODO: it seemed lvars is in java stack, but jos[8] should be part of java stack
272 // lvars_jem requires that the highest pointer should refer to the first local variables LV0
273 // Here still not touch the java stack, allocate lvars_jem from native heap
275 new_frame->lvars_jem = (uintptr_t *)new_frame;
277 for (i = 0; i < ctx->new_mb->max_locals; i++) {
278 if (i < ctx->new_mb->args_count) {
279 *(new_frame->lvars_jem - 1 - i) = *(ctx->arg1 + i);
280 jam_dprintf("arg[%d] = 0x%x@%p\n", i, *(ctx->arg1 + i), ctx->arg1 + i);
282 #ifdef JEM_DEBUG
283 else
284 *(new_frame->lvars_jem - 1 - i) = 0xdeadbeef;
285 #endif
288 jam_dprintf("[invokeMethod] %s::%s() lvars_jem %p\n",
289 CLASS_CB(ctx->new_mb->class)->name, ctx->new_mb->name, new_frame->lvars_jem);
291 new_frame->ostack = ctx->ostack;
292 new_frame->prev = ctx->frame;
294 ee->last_frame = new_frame;
296 if (ctx->new_mb->access_flags & ACC_SYNCHRONIZED) {
297 aobj = (uintptr_t *)*ctx->arg1;
298 sync_ob = ctx->new_mb->access_flags & ACC_STATIC ?
299 (Object*)ctx->new_mb->class : JAM_OBJECT(aobj);
300 objectLock(sync_ob);
303 if (ctx->new_mb->access_flags & ACC_NATIVE) {
304 //FIXME: the object references in JEM are direct pointer to instance variables.
305 // While JamVM requires object references as pointers to Object type.
306 jam_dprintf("[invokeMethod] invoke native method using %p invoker\n",
307 ctx->new_mb->native_invoker);
308 ctx->ostack = (*(uintptr_t *(*)(Class*, MethodBlock*, uintptr_t*))
309 ctx->new_mb->native_invoker)(ctx->new_mb->class, ctx->new_mb, ctx->arg1);
311 if (sync_ob)
312 objectUnlock(sync_ob);
314 ee->last_frame = ctx->frame;
316 if (exceptionOccured0(ee)) {
317 jam_dprintf("[invokeMethod] exception occured, goto throwException\n");
318 return jem_throwException(ctx);
321 jam_dprintf("[invokeMethod] finish native method invoke, return to loop\n");
322 } else {
323 ctx->frame = new_frame;
324 ctx->mb = ctx->new_mb;
325 ctx->lvars_jem = new_frame->lvars_jem - 1;
327 GET_THIS(ctx);
329 ctx->jem.jpc = (unsigned long)ctx->mb->code & ~3;
330 ctx->cp = &(CLASS_CB(ctx->mb->class)->constant_pool);
332 jam_dprintf("[invokeMethod] invoke virtual method: mb %x "
333 "lvars %x jpc %x cp %x\n", ctx->mb,
334 ctx->lvars_jem, ctx->jem.jpc, ctx->cp);
337 return 0;
340 static int opc_invokesuper_quick(struct intrp_ctx *ctx)
342 struct jem_state *jem = &ctx->jem;
343 Operand *operand = &jem->operand;
345 jam_dprintf("[OPC_INVOKESUPER_QUICK] method table indx %d\n", operand->i);
346 ctx->new_mb = CLASS_CB(CLASS_CB(ctx->mb->class)->super)->method_table[operand->i];
347 jam_dprintf("[OPC_INVOKESUPER_QUICK] method args count %d\n", ctx->new_mb->args_count);
349 // set up jem operand stack
350 ctx->arg1 = ostack_address(ctx->frame->ostack, ctx->mb->max_stack,
351 ctx->ostack - ctx->frame->ostack - ctx->new_mb->args_count);
352 JEM_NULL_POINTER_CHECK(*ctx->arg1);
353 jam_dprintf("[OP_INVOKESUPER_QUICK] arg1 %x\n", *ctx->arg1);
354 return invokeMethod(ctx);
357 static int opc_invokenonvirtual_quick(struct intrp_ctx *ctx)
359 struct jem_state *jem = &ctx->jem;
360 Operand *operand = &jem->operand;
362 ctx->new_mb = (MethodBlock*)operand->pntr;
363 jam_dprintf("[%s] new methodblock %s (%x) with %d args\n",
364 __func__, ctx->new_mb->name, ctx->new_mb, ctx->new_mb->args_count);
365 ctx->arg1 = ostack_address(ctx->frame->ostack, ctx->mb->max_stack,
366 ctx->ostack - ctx->frame->ostack - ctx->new_mb->args_count);
368 jam_dprintf("[%s] ctx->arg1 %x\n", __func__, *ctx->arg1);
369 JEM_NULL_POINTER_CHECK(*ctx->arg1);
370 return invokeMethod(ctx);
373 static int opc_invokevirtual_quick(struct intrp_ctx *ctx)
375 struct jem_state *jem = &ctx->jem;
376 Object *obj;
377 ctx->arg1 = ostack_address(ctx->frame->ostack, ctx->mb->max_stack,
378 ctx->ostack - ctx->frame->ostack - INV_QUICK_ARGS(jem));
379 JEM_NULL_POINTER_CHECK(*ctx->arg1);
380 Class *new_class;
382 //in case of "AALOAD" loads JAM obj from reference array
383 obj = (Object *)*ctx->arg1;
384 if (!JAM_ON_STACK || !IS_JAM_OBJECT(obj)) {
385 obj = JAM_OBJECT((uintptr_t *)obj);
386 //we should ensure ostack contains no JAM object
387 *ctx->arg1 = (uintptr_t)INST_DATA(obj);
390 jam_dprintf("[OPC_INVOKEVIRTUAL_QUICK] invoke on object %p from %p class %p\n",
391 obj, ctx->arg1, obj->class);
392 new_class = obj->class;
393 ctx->new_mb = CLASS_CB(new_class)->method_table[INV_QUICK_IDX(jem)];
394 jam_dprintf("[OPC_INVOKEVIRTUAL_QUICK] invoke method 0x%08x\n",
395 ctx->new_mb);
396 return invokeMethod(ctx);
399 static int opc_invokevirtual(struct intrp_ctx *ctx)
401 int idx;
402 ExecEnv *ee = ctx->ee;
403 struct jem_state *jem = &ctx->jem;
405 idx = jem->jecr & 0xffff;
406 ctx->frame->last_pc = (CodePntr)jem->jpc;
407 ctx->new_mb = resolveMethod(ctx->mb->class, idx);
409 if (exceptionOccured0(ee))
410 return jem_throwException(ctx);
412 jem->operand.uu.u1 = ctx->new_mb->args_count;
413 jem->operand.uu.u2 = ctx->new_mb->method_table_index;
415 jam_dprintf("[OPC_INVOKEVIRTUAL] invoke 0x%08x with args count %d\n",
416 ctx->new_mb, ctx->new_mb->args_count);
418 return opc_invokevirtual_quick(ctx);
421 static int opc_invokespecial(struct intrp_ctx *ctx)
423 int idx;
424 struct jem_state *jem = &ctx->jem;
425 Operand *operand = &jem->operand;
426 ExecEnv *ee = ctx->ee;
428 /* INVOKESPECIAL's operands represent an index into the Constant Pool */
429 idx = jem->jecr & 0xffff;
431 jam_dprintf("[OPC_INVOKESPECIAL] constant pool index %d\n", idx);
433 ctx->new_mb = resolveMethod(ctx->mb->class, idx);
435 if (exceptionOccured0(ee))
436 return jem_throwException(ctx);
438 jam_dprintf("Resolved %s\n", ctx->new_mb->name);
440 /* Check if invoking a super method... */
441 if ((CLASS_CB(ctx->mb->class)->access_flags & ACC_SUPER) &&
442 ((ctx->new_mb->access_flags & ACC_PRIVATE) == 0) && (ctx->new_mb->name[0] != '<')) {
444 operand->i = ctx->new_mb->method_table_index;
445 return opc_invokesuper_quick(ctx);
446 #if 0 /* FIXME: verify this whole "pc" chemistry is indeed unneeded */
447 OPCODE_REWRITE(OPC_INVOKESUPER_QUICK, cache, operand);
448 #endif
449 } else {
450 operand->pntr = ctx->new_mb;
451 return opc_invokenonvirtual_quick(ctx);
454 return 0;
457 static int opc_invokestatic_quick(struct intrp_ctx *ctx)
459 struct jem_state *jem = &ctx->jem;
461 ctx->new_mb = RESOLVED_METHOD(jem);
462 ctx->arg1 = ostack_address(ctx->frame->ostack, ctx->mb->max_stack,
463 ctx->ostack - ctx->frame->ostack - ctx->new_mb->args_count);
464 return invokeMethod(ctx);
467 static int opc_invokestatic(struct intrp_ctx *ctx)
469 int idx, cache;
470 struct jem_state *jem = &ctx->jem;
471 Operand *operand = &jem->operand;
472 ExecEnv *ee = ctx->ee;
474 idx = jem->jecr & 0xffff;
476 ctx->frame->last_pc = (CodePntr)jem->jpc;
477 ctx->new_mb = resolveMethod(ctx->mb->class, idx);
479 if (exceptionOccured0(ee))
480 return jem_throwException(ctx);
482 operand->pntr = ctx->new_mb;
483 return opc_invokestatic_quick(ctx);
486 static int opc_new_quick(struct intrp_ctx *ctx)
488 struct jem_state *jem = &ctx->jem;
489 Operand *operand = &jem->operand;
490 Class *class = (Class*)CP_INFO(ctx->cp, operand->uui.u1);
491 Object *obj;
493 ctx->frame->last_pc = (CodePntr)jem->jpc;
494 if ((obj = allocObject(class)) == NULL)
495 return jem_throwException(ctx);
496 jam_dprintf("[OPC_NEW_QUICK] push obj ref %x to stack\n", obj);
497 return ostack_push_u32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack,
498 (uint32_t)INST_DATA(obj));
501 static int opc_new(struct intrp_ctx *ctx)
503 struct jem_state *jem = &ctx->jem;
504 Operand *operand = &jem->operand;
505 int idx = jem->jecr & 0xffff;
506 int opcode = (jem->jecr >> 16) & 0xff;
507 Class *class;
508 ExecEnv *ee = ctx->ee;
510 operand->uui.u1 = idx;
511 operand->uui.u2 = opcode;
513 jam_dprintf("[OPC_NEW] opcode: 0x%x, index: 0x%x\n", opcode, idx);
515 ctx->frame->last_pc = (CodePntr)jem->jpc;
516 class = resolveClass(ctx->mb->class, idx, opcode == OPC_NEW);
517 if (exceptionOccured0(ee))
518 return jem_throwException(ctx);
520 if (opcode == OPC_NEW) {
521 ClassBlock *cb = CLASS_CB(class);
522 if (cb->access_flags & (ACC_INTERFACE | ACC_ABSTRACT)) {
523 signalException("java/lang/InstantiationError", cb->name);
524 return jem_throwException(ctx);
526 /* rewrite opcode to OPC_NEW_QUICK (0xDD) */
527 jem_opcode_rewrite(0xDD, (char*)jem->jpc - 3, 0);
530 return opc_new_quick(ctx);
533 static int opc_anewarray_quick(struct intrp_ctx *ctx)
535 struct jem_state *jem = &ctx->jem;
536 ConstantPool *cp = ctx->cp;
537 Class *class = RESOLVED_CLASS(jem);
538 char *name = CLASS_CB(class)->name;
539 int count;
540 Class *array_class;
541 char *ac_name;
542 Object *obj;
543 ExecEnv *ee = ctx->ee;
545 ostack_pop_u32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack, count);
546 ctx->frame->last_pc = (CodePntr)jem->jpc;
548 jam_dprintf("[OPC_ANEWARRAY_QUICK] array %s size %d\n", name, count);
549 if (count < 0) {
550 signalException("java/lang/NegativeArraySizeException", NULL);
551 return jem_throwException(ctx);
554 ac_name = sysMalloc(strlen(name) + 4);
556 if (name[0] == '[')
557 sprintf(ac_name, "[%s", name);
558 else
559 sprintf(ac_name, "[L%s;", name);
561 array_class = findArrayClassFromClass(ac_name, ctx->mb->class);
562 free(ac_name);
564 if (exceptionOccured0(ee))
565 return jem_throwException(ctx);
567 if ((obj = allocArray(array_class, count, sizeof(Object*))) == NULL)
568 return jem_throwException(ctx);
570 return ostack_push_u32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack,
571 (uint32_t)ARRAY_DATA(obj));
574 static int opc_anewarray(struct intrp_ctx *ctx)
576 struct jem_state *jem = &ctx->jem;
577 Operand *operand = &jem->operand;
578 int idx = jem->jecr & 0xffff;
579 int opcode = (jem->jecr >> 16) & 0xff;
580 Class *class;
581 ExecEnv *ee = ctx->ee;
583 operand->uui.u1 = idx;
584 operand->uui.u2 = opcode;
586 jam_dprintf("[OPC_ANEWARRAY] index: 0x%x\n", idx);
588 ctx->frame->last_pc = (CodePntr)jem->jpc;
589 //resolve the array class
590 class = resolveClass(ctx->mb->class, idx, FALSE);
592 if (exceptionOccured0(ee))
593 return jem_throwException(ctx);
595 return opc_anewarray_quick(ctx);
598 static int opc_multianewarray_quick(struct intrp_ctx *ctx)
600 struct jem_state *jem = &ctx->jem;
601 ConstantPool *cp = ctx->cp;
602 Class *class = RESOLVED_CLASS(jem);
603 int i, dim = MULTI_ARRAY_DIM(jem);
604 int count;
605 Object *obj;
606 ExecEnv *ee = ctx->ee;
608 ctx->frame->last_pc = (CodePntr)jem->jpc;
610 jam_dprintf("[OPC_MULTIANEWARRAY_QUICK]: alloc %d dimensional array for class %s\n", dim, CLASS_CB(class)->name);
611 for (i = 0; i < dim; i++) {
612 ostack_pop_u32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack, count);
613 jam_dprintf("[OPC_MULTIANEWARRAY_QUICK]: elements %d for dim %d\n", count, i);
614 if (count < 0) {
615 signalException("java/lang/NegativeArraySizeException", NULL);
616 return jem_throwException(ctx);
620 /* counts are still stored in ostack even after we did pop them out */
621 if ((obj = allocMultiArray(class, dim, (intptr_t *)ctx->ostack)) == NULL)
622 return jem_throwException(ctx);
624 return ostack_push_u32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack,
625 (uint32_t)ARRAY_DATA(obj));
628 static int opc_multianewarray(struct intrp_ctx *ctx)
630 struct jem_state *jem = &ctx->jem;
631 Operand *operand = &jem->operand;
632 int idx = operand->uui.u1 = jem->jecr & 0xffff;
634 Class *class;
635 ExecEnv *ee = ctx->ee;
637 /* dimensions */
638 operand->uui.u2 = *(unsigned char*)jem->jpc;
639 jem->jpc++; /* skip dims */
641 ctx->frame->last_pc = (CodePntr)jem->jpc;
642 /* resolve the array class */
643 class = resolveClass(ctx->mb->class, idx, FALSE);
645 if (exceptionOccured0(ee))
646 return jem_throwException(ctx);
648 return opc_multianewarray_quick(ctx);
651 static int opc_newarray(struct intrp_ctx *ctx)
653 struct jem_state *jem = &ctx->jem;
654 int type = (jem->jecr >> 8) & 0xff;
655 int count;
656 Object *obj;
658 ostack_pop_u32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack, count);
659 ctx->frame->last_pc = (CodePntr)jem->jpc;
661 jam_dprintf("[OPC_NEWARRAY] alloc array type %d, count %d\n", type, count);
662 if ((obj = allocTypeArray(type, count)) == NULL)
663 return jem_throwException(ctx);
665 return ostack_push_u32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack,
666 (uint32_t)ARRAY_DATA(obj));
669 static int opc_putfield_quick(struct intrp_ctx *ctx)
671 uint32_t arg;
672 uint32_t obj;
673 struct jem_state *jem = &ctx->jem;
675 ostack_pop_u32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack, arg);
676 ostack_pop_u32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack, obj);
677 jam_dprintf("[OPC_PUTFIELD_QUICK] put field %x to obj(%d) %x\n", arg,
678 SINGLE_INDEX(jem), obj);
680 JEM_NULL_POINTER_CHECK(obj);
682 if (JAM_ON_STACK && IS_JAM_OBJECT(obj))
683 obj = (uint32_t)INST_DATA((Object *)obj);
685 ((uint32_t*)obj)[SINGLE_INDEX(jem)] = arg;
687 return 0;
690 static int opc_putfield2_quick(struct intrp_ctx *ctx)
692 uint64_t arg;
693 uint32_t obj;
694 struct jem_state *jem = &ctx->jem;
696 ostack_pop_u64(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack, arg);
697 ostack_pop_u32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack, obj);
699 JEM_NULL_POINTER_CHECK((uintptr_t*)obj);
701 if (JAM_ON_STACK && IS_JAM_OBJECT(obj))
702 obj = (uint32_t)INST_DATA((Object *)obj);
704 *(uint64_t *)&(((uint32_t*)obj)[SINGLE_INDEX(jem)]) = arg;
705 jam_dprintf("[OPC_PUTFIELD2_QUICK] put 64 bit %08x to obj %08x\n", arg, obj);
707 return 0;
710 static int opc_putfield_quick_jem0(struct intrp_ctx *ctx)
712 uint32_t arg;
713 uintptr_t *aobj;
714 struct jem_state *jem = &ctx->jem;
716 ostack_pop_u32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack, arg);
717 ostack_pop_u32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack, aobj);
718 jam_dprintf("[OPC_PUTFIELD_QUICK_JEM0] field %x\n", arg);
720 JEM_NULL_POINTER_CHECK(aobj);
722 if (JAM_ON_STACK && arg && !IS_JAM_OBJECT(arg))
723 arg = (uint32_t)JAM_OBJECT((uintptr_t *)arg);
725 if (JAM_ON_STACK && IS_JAM_OBJECT(aobj))
726 aobj = INST_DATA((Object *)aobj);
728 aobj[SINGLE_INDEX(jem)] = arg;
729 jam_dprintf("[OPC_PUTFIELD_QUICK_JEM0] put field %x to obj(%d) %x\n", arg,
730 SINGLE_INDEX(jem), aobj);
732 return 0;
735 static int opc_putfield_quick_jem1(struct intrp_ctx *ctx)
737 uintptr_t *aobj, *arg;
738 struct jem_state *jem = &ctx->jem;
740 ostack_pop_u32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack, arg);
741 ostack_pop_u32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack, aobj);
742 jam_dprintf("[OPC_PUTFIELD_QUICK_JEM1] field %p\n", arg);
744 if (JAM_ON_STACK && arg && !IS_JAM_ARRAY(arg))
745 arg = (uintptr_t *)JAM_ARRAY(arg);
747 if (JAM_ON_STACK && IS_JAM_OBJECT(aobj))
748 aobj = INST_DATA((Object *)aobj);
750 JEM_NULL_POINTER_CHECK(aobj);
751 aobj[SINGLE_INDEX(jem)] = (uintptr_t)arg;
752 jam_dprintf("[OPC_PUTFIELD_QUICK_JEM1] put field %p to obj(%d) %p\n", arg,
753 SINGLE_INDEX(jem), aobj);
755 return 0;
758 static int opc_putfield(struct intrp_ctx *ctx)
760 struct jem_state *jem = &ctx->jem;
761 int idx = jem->jecr & 0xffff;
762 FieldBlock *fb;
763 ExecEnv *ee = ctx->ee;
765 jam_dprintf("[OPC_PUTFIELD] constant pool index %d\n", idx);
767 ctx->frame->last_pc = (CodePntr)jem->jpc;
768 fb = resolveField(ctx->mb->class, idx);
769 jam_dprintf("[OPC_PUTFIELD] resolve field 0x%08x, type %c, name %s\n", fb, *fb->type, fb->name);
771 if (exceptionOccured0(ee))
772 return jem_throwException(ctx);
774 jem->operand.i = fb->offset;
775 switch (*fb->type) {
776 case 'J':
777 case 'D':
778 /* rewrite opc_putfield to opc_putfield2_quick */
779 jem_opcode_rewrite(0xD1, (char*)jem->jpc - 3, 2, fb->offset >> 8, fb->offset);
780 return opc_putfield2_quick(ctx);
781 case 'L':
782 jem_opcode_rewrite(0xCF, (char*)jem->jpc - 3, 2, fb->offset >> 8, fb->offset);
783 return opc_putfield_quick_jem0(ctx);
784 case '[':
785 jem_opcode_rewrite(0xCF, (char*)jem->jpc - 3, 2, fb->offset >> 8, fb->offset);
786 return opc_putfield_quick_jem1(ctx);
787 default:
788 jem_opcode_rewrite(0xCF, (char*)jem->jpc - 3, 2, fb->offset >> 8, fb->offset);
789 return opc_putfield_quick(ctx);
793 static int opc_getfield2_quick(struct intrp_ctx *ctx)
795 uint32_t *obj;
796 struct jem_state *jem = &ctx->jem;
797 ostack_pop_u32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack, obj);
798 JEM_NULL_POINTER_CHECK(obj);
800 if (JAM_ON_STACK && IS_JAM_OBJECT(obj))
801 obj = (uint32_t*)INST_DATA((Object*)obj);
803 jam_dprintf("[OPC_GETFIELD2_QUICK] get 64 bit field %08x from obj %08x\n",
804 *(uint64_t*)(&(obj[SINGLE_INDEX(jem)])), obj);
806 return ostack_push_u64(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack,
807 *(uint64_t*)(&(obj[SINGLE_INDEX(jem)])));
810 static int opc_getfield_quick(struct intrp_ctx *ctx)
812 uintptr_t *aobj;
813 struct jem_state *jem = &ctx->jem;
815 ostack_pop_u32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack, aobj);
816 JEM_NULL_POINTER_CHECK(aobj);
818 if (JAM_ON_STACK && IS_JAM_OBJECT(aobj))
819 aobj = (uint32_t*)INST_DATA((Object*)aobj);
821 jam_dprintf("[OPC_GETFIELD_QUICK] get field %08x from obj %08x\n",
822 aobj[SINGLE_INDEX(jem)], aobj);
823 return ostack_push_u32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack,
824 aobj[SINGLE_INDEX(jem)]);
827 static int opc_getfield_quick_jem0(struct intrp_ctx *ctx)
829 uint32_t *aobj;
830 struct jem_state *jem = &ctx->jem;
832 ostack_pop_u32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack, aobj);
833 JEM_NULL_POINTER_CHECK(aobj);
835 if (JAM_ON_STACK && IS_JAM_OBJECT(aobj))
836 aobj = (uint32_t*)INST_DATA((Object*)aobj);
838 uint32_t value = aobj[SINGLE_INDEX(jem)];
839 if (value) {
840 Object *ref = (Object*)value;
841 if (JAM_ON_STACK && IS_JAM_OBJECT(ref))
842 //jamvm objref
843 value = (uint32_t)INST_DATA(ref);
844 jam_dprintf("[OPC_GETFIELD_QUICK_JEM0] obj %p ref %x, class %p : %p\n",
845 aobj, value, JAM_OBJECT((void *)value)->class, ref->class);
847 jam_dprintf("[OPC_GETFIELD_QUICK_JEM0] put obj ref %x\n", value);
848 return ostack_push_u32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack, value);
851 static int opc_getfield_quick_jem1(struct intrp_ctx *ctx)
853 uint32_t *aobj;
854 struct jem_state *jem = &ctx->jem;
856 ostack_pop_u32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack, aobj);
857 JEM_NULL_POINTER_CHECK(aobj);
859 //"aaload" would put jamvm obj ref onto the ostack
860 if (JAM_ON_STACK && IS_JAM_OBJECT(aobj))
861 aobj = (uint32_t*)INST_DATA((Object*)aobj);
863 uint32_t value = aobj[SINGLE_INDEX(jem)];
864 jam_dprintf("[OPC_GETFIELD_QUICK_JEM1] array ref %x\n", value);
865 if (value) {
866 Object *ref = (Object*)value;
867 if (JAM_ON_STACK && IS_JAM_ARRAY(ref))
868 value = (uint32_t)ARRAY_DATA(ref);
870 jam_dprintf("[OPC_GETFIELD_QUICK_JEM1] put array ref %x\n", value);
871 return ostack_push_u32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack, value);
874 static int opc_getfield(struct intrp_ctx *ctx)
876 struct jem_state *jem = &ctx->jem;
877 int idx = jem->jecr & 0xffff;
878 FieldBlock *fb;
879 ExecEnv *ee = ctx->ee;
881 jam_dprintf("[OPC_GETFIELD] constant pool index %d\n", idx);
883 ctx->frame->last_pc = (CodePntr)jem->jpc;
884 fb = resolveField(ctx->mb->class, idx);
886 if (exceptionOccured0(ee))
887 return jem_throwException(ctx);
889 jem->operand.i = fb->offset;
891 jam_dprintf("[OPC_GETFIELD] resolve field %s @ %p, type %c, offset %i, class %p\n",
892 fb->name, fb, *fb->type, jem->operand.i, fb->class);
894 switch (*fb->type) {
895 case 'J':
896 case 'D':
897 jem_opcode_rewrite(0xD0, (char*)jem->jpc - 3, 2, fb->offset >> 8, fb->offset);
898 return opc_getfield2_quick(ctx);
900 * Though we assume all objrefs/arrayrefs here should be JEM compliant, but
901 * in case of some JNI code will set field natively, we still handle it
902 * separately here.
904 case 'L':
905 jem_opcode_rewrite(0xCE, (char*)jem->jpc - 3, 2, fb->offset >> 8, fb->offset);
906 return opc_getfield_quick_jem0(ctx);
907 case '[':
908 jem_opcode_rewrite(0xCE, (char*)jem->jpc - 3, 2, fb->offset >> 8, fb->offset);
909 return opc_getfield_quick_jem1(ctx);
910 default:
911 //jem_opcode_rewrite(0xCE, (char*)jem->jpc - 3, 2, (fb->offset>>8)&0xff, (fb->offset)&0xff);
912 return opc_getfield_quick(ctx);
916 static int opc_ldc2_w(struct intrp_ctx *ctx)
918 int idx;
919 ExecEnv *ee = ctx->ee;
921 struct jem_state *jem = &ctx->jem;
922 idx = (jem->jecr & 0xffff);
924 jam_dprintf("[OPC_LDC2_W] constant pool index %d\n", idx);
925 jem->operand.i = idx;
927 //read long long from constant pool
928 uint64_t val = (uint64_t)CP_LONG(ctx->cp, DOUBLE_INDEX(jem));
929 jam_dprintf("[OPC_LDC2_W] push constant %lld to stack\n", (long long)val);
930 return ostack_push_u64(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack, val);
933 static int opc_ldc_w_quick_jem(struct intrp_ctx *ctx)
935 struct jem_state *jem = &ctx->jem;
936 //CONSTANT_Class and CONSTANT_String are stored as object reference
937 Object *cp_info = (Object*)CP_INFO(ctx->cp, DOUBLE_INDEX(jem));
938 jam_dprintf("[OPC_LDC_W_QUICK_JEM] push String ref %x to stack\n", cp_info);
939 return ostack_push_u32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack,
940 (uint32_t)INST_DATA(cp_info));
943 static int opc_ldc_quick(struct intrp_ctx *ctx)
945 struct jem_state *jem = &ctx->jem;
946 uint32_t constant = RESOLVED_CONSTANT(jem);
947 jam_dprintf("[OPC_LDC_QUICK] push constant %x to stack\n", constant);
948 return ostack_push_u32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack, (uintptr_t)constant);
951 static int opc_ldc_common(struct intrp_ctx *ctx, int idx)
953 struct jem_state *jem = &ctx->jem;
954 ExecEnv *ee = ctx->ee;
955 ctx->frame->last_pc = (CodePntr)jem->jpc;
957 jem->operand.u = resolveSingleConstant(ctx->mb->class, idx);
959 if (exceptionOccured0(ee))
960 return jem_throwException(ctx);
962 switch (CP_TYPE(ctx->cp, idx)) {
963 case CONSTANT_ResolvedClass:
964 case CONSTANT_ResolvedString:
965 jem->operand.i = idx;
966 return opc_ldc_w_quick_jem(ctx);
967 default:
968 jem_opcode_rewrite(0xCB, (char*)jem->jpc - 2, 0);
969 return opc_ldc_quick(ctx);
973 static int opc_ldc(struct intrp_ctx *ctx)
975 int idx;
976 ExecEnv *ee = ctx->ee;
977 struct jem_state *jem = &ctx->jem;
979 idx = (jem->jecr & 0xff00) >> 8;
981 jam_dprintf("[OPC_LDC] constant pool index %d\n", idx);
982 return opc_ldc_common(ctx, idx);
985 static int opc_ldc_w(struct intrp_ctx *ctx)
987 int idx;
988 ExecEnv *ee = ctx->ee;
989 struct jem_state *jem = &ctx->jem;
991 idx = (jem->jecr & 0xffff);
993 jam_dprintf("[OPC_LDC_W] constant pool index %d\n", idx);
994 return opc_ldc_common(ctx, idx);
997 static int opc_monitorenter(struct intrp_ctx *ctx)
999 Object *obj;
1000 uintptr_t *ptr;
1001 struct jem_state *jem = &ctx->jem;
1002 ostack_pop_u32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack, ptr);
1004 obj = JAM_OBJECT(ptr);
1005 if (!IS_OBJECT(obj))
1006 /* is an array ref */
1007 obj = JAM_ARRAY(ptr);
1008 jam_dprintf("[OPC_MONITORENTER] lock %x\n", obj);
1009 JEM_NULL_POINTER_CHECK(obj);
1010 objectLock(obj);
1011 return 0;
1014 static int opc_monitorexit(struct intrp_ctx *ctx)
1016 Object *obj;
1017 uintptr_t *ptr;
1018 struct jem_state *jem = &ctx->jem;
1019 ostack_pop_u32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack, ptr);
1021 obj = JAM_OBJECT(ptr);
1022 if (!IS_OBJECT(obj))
1023 /* is an array ref */
1024 obj = JAM_ARRAY(ptr);
1025 jam_dprintf("[OPC_MONITOREXIT] unlock %x\n", obj);
1026 JEM_NULL_POINTER_CHECK(obj);
1027 objectUnlock(obj);
1028 return 0;
1031 static int opc_getstatic_quick(struct intrp_ctx *ctx)
1033 struct jem_state *jem = &ctx->jem;
1034 return ostack_push_u32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack,
1035 RESOLVED_FIELD(jem)->static_value);
1038 static int opc_getstatic2_quick(struct intrp_ctx *ctx)
1040 struct jem_state *jem = &ctx->jem;
1041 return ostack_push_u64(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack,
1042 RESOLVED_FIELD(jem)->static_value);
1045 static int opc_getstatic_quick_jem0(struct intrp_ctx *ctx)
1047 struct jem_state *jem = &ctx->jem;
1048 uintptr_t value = RESOLVED_FIELD(jem)->static_value;
1050 jam_dprintf("[OPC_GETSTATIC_QUICK_JEM0] obj ref %x\n", value);
1051 if (value) {
1052 Object *ref = (Object *)value;
1053 if (!JAM_ON_STACK || IS_JAM_OBJECT(ref))
1054 //jamvm objref
1055 value = (uintptr_t)INST_DATA(ref);
1057 jam_dprintf("[OPC_GETSTATIC_QUICK_JEM0] put obj ref %x\n", value);
1058 return ostack_push_u32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack, value);
1061 static int opc_getstatic_quick_jem1(struct intrp_ctx *ctx)
1063 struct jem_state *jem = &ctx->jem;
1064 uintptr_t value = RESOLVED_FIELD(jem)->static_value;
1065 jam_dprintf("[OPC_GETSTATIC_QUICK_JEM1] array ref %x\n", value);
1066 if (value) {
1067 Object *ref = (Object*)value;
1068 if (!JAM_ON_STACK || IS_JAM_ARRAY(ref))
1069 //jamvm arrayref
1070 value = (uintptr_t)ARRAY_DATA(ref);
1072 jam_dprintf("[OPC_GETSTATIC_QUICK_JEM1] put array ref %x\n", value);
1073 return ostack_push_u32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack, value);
1076 static int opc_getstatic(struct intrp_ctx *ctx)
1078 int idx;
1079 FieldBlock *fb;
1080 Operand operand;
1081 ExecEnv *ee = ctx->ee;
1082 struct jem_state *jem = &ctx->jem;
1084 idx = (int)(jem->jecr & 0xffff);
1085 jam_dprintf("[OPC_GETSTATIC] constant pool index %d class %s\n",idx,CLASS_CB(ctx->mb->class)->name);
1087 ctx->frame->last_pc = (CodePntr)jem->jpc;
1088 fb = resolveField(ctx->mb->class, idx);
1090 if (exceptionOccured0(ee))
1091 return jem_throwException(ctx);
1093 jam_dprintf("[OPC_GETSTATIC] get static for field %08x, type %c\n", fb, *fb->type);
1094 jem->operand.pntr = fb;
1095 switch (*fb->type) {
1096 case 'J':
1097 case 'D':
1098 return opc_getstatic2_quick(ctx);
1100 * see opc_getfield
1102 case 'L':
1103 return opc_getstatic_quick_jem0(ctx);
1104 case '[':
1105 return opc_getstatic_quick_jem1(ctx);
1106 default:
1107 return opc_getstatic_quick(ctx);
1111 static int opc_putstatic2_quick(struct intrp_ctx *ctx)
1113 struct jem_state *jem = &ctx->jem;
1114 return ostack_pop_u64(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack,
1115 *(u8*)&RESOLVED_FIELD(jem)->static_value);
1118 static int opc_putstatic_quick(struct intrp_ctx *ctx)
1120 struct jem_state *jem = &ctx->jem;
1121 return ostack_pop_u32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack,
1122 RESOLVED_FIELD(jem)->static_value);
1125 static int opc_putstatic_quick_jem0(struct intrp_ctx *ctx)
1127 uintptr_t value;
1128 struct jem_state *jem = &ctx->jem;
1129 ostack_pop_u32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack, value);
1131 jam_dprintf("[OPC_PUTSTATIC_QUICK_JEM0] obj %x\n", value);
1132 if (value && (!JAM_ON_STACK || !IS_JAM_OBJECT(value)))
1133 value = (uintptr_t)JAM_OBJECT((uintptr_t *)value);
1134 jam_dprintf("[OPC_PUTSTATIC_QUICK_JEM0] put obj %x\n", value);
1135 RESOLVED_FIELD(jem)->static_value = value;
1136 return 0;
1139 static int opc_putstatic_quick_jem1(struct intrp_ctx *ctx)
1141 uintptr_t *value;
1142 struct jem_state *jem = &ctx->jem;
1143 ostack_pop_u32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack, value);
1145 jam_dprintf("[OPC_PUTSTATIC_QUICK_JEM1] array %p\n", value);
1146 if (value && (!JAM_ON_STACK || !IS_JAM_ARRAY(value)))
1147 value = (uintptr_t*)JAM_ARRAY(value);
1148 jam_dprintf("[OPC_PUTSTATIC_QUICK_JEM1] put array %p\n", value);
1149 RESOLVED_FIELD(jem)->static_value = (uintptr_t)value;
1150 return 0;
1153 static int opc_putstatic(struct intrp_ctx *ctx)
1155 int idx;
1156 FieldBlock *fb;
1157 Operand operand;
1158 ExecEnv *ee = ctx->ee;
1159 struct jem_state *jem = &ctx->jem;
1161 idx = (int)(jem->jecr & 0xffff);
1163 ctx->frame->last_pc = (CodePntr)jem->jpc;
1164 fb = resolveField(ctx->mb->class, idx);
1166 if (exceptionOccured0(ee))
1167 return jem_throwException(ctx);
1169 jam_dprintf("[OPC_PUTSTATIC] put static for field %08x, type %c\n", fb, *fb->type);
1170 jem->operand.pntr = fb;
1171 switch (*fb->type) {
1172 case 'J':
1173 case 'D':
1174 //jem_opcode_rewrite(0xD5, (char*)jem->jpc - 3);
1175 return opc_putstatic2_quick(ctx);
1176 case 'L':
1177 return opc_putstatic_quick_jem0(ctx);
1178 case '[':
1179 return opc_putstatic_quick_jem1(ctx);
1180 default:
1181 return opc_putstatic_quick(ctx);
1185 static int opc_checkcast_quick(struct intrp_ctx *ctx)
1187 struct jem_state *jem = &ctx->jem;
1188 ConstantPool *cp = ctx->cp;
1189 Class *class = RESOLVED_CLASS(jem);
1190 Object *obj;
1191 uintptr_t *ptr;
1193 ostack_read_u32(ctx->frame->ostack, ctx->ostack, 0, &ptr);
1194 jam_dprintf("[OPC_CHECKCAST_QUICK] check cast for JEM type %x (class %s)\n", ptr, CLASS_CB(class)->name);
1195 if (ptr != NULL) {
1196 #warning Does not look right!
1197 if (!JAM_ON_STACK || !IS_JAM_OBJECT(ptr) || !IS_JAM_ARRAY(ptr)) {
1198 obj = JAM_OBJECT(ptr);
1199 if (!IS_OBJECT(obj)) {
1200 jam_dprintf("[OPC_CHECKCAST_QUICK] check cast for array\n");
1201 //should be array ref
1202 obj = JAM_ARRAY(ptr);
1204 jam_dprintf("[OPC_CHECKCAST_QUICK] check cast for object %x\n", obj);
1205 } else {
1206 obj = (Object*)ptr;
1208 jam_dprintf("[OPC_CHECKCAST_QUICK] check class %p (%s) for obj %p (%s)\n",
1209 class, CLASS_CB(class)->name, obj->class, CLASS_CB(obj->class)->name);
1210 if (!isInstanceOf(class, obj->class))
1211 return JEM_THROW_EXCEPTION("java/lang/ClassCastException", CLASS_CB(obj->class)->name);
1214 return 0;
1217 static int opc_checkcast(struct intrp_ctx *ctx)
1219 struct jem_state *jem = &ctx->jem;
1220 Operand *operand = &jem->operand;
1221 int idx = jem->jecr & 0xffff;
1222 int opcode = (jem->jecr >> 16) & 0xff;
1223 Class *class;
1224 ExecEnv *ee = ctx->ee;
1226 operand->uui.u1 = idx;
1227 operand->uui.u2 = opcode;
1229 jam_dprintf("[OPC_CHECKCAST] index: 0x%x\n", idx);
1231 ctx->frame->last_pc = (CodePntr)jem->jpc;
1232 //resolve the cast class
1233 class = resolveClass(ctx->mb->class, idx, FALSE);
1235 if (exceptionOccured0(ee))
1236 return jem_throwException(ctx);
1238 return opc_checkcast_quick(ctx);
1241 static int opc_invokeinterface_quick(struct intrp_ctx *ctx)
1243 int mtbl_idx;
1244 ClassBlock *cb;
1245 Object *objref;
1246 int cache = ctx->jem.operand.uu.u2;
1248 ctx->new_mb = (MethodBlock *)CP_INFO(ctx->cp, ctx->jem.operand.uu.u1);
1249 ctx->arg1 = ostack_address(ctx->frame->ostack, ctx->mb->max_stack,
1250 ctx->ostack - ctx->frame->ostack - ctx->new_mb->args_count);
1252 jam_dprintf("[OPC_INVOKEINTERFACE_QUICK] newmb %x (name %s) (args count %d) objref(jem) %x\n",
1253 ctx->new_mb, ctx->new_mb->name, ctx->new_mb->args_count, ctx->arg1);
1255 objref = (Object *)*ctx->arg1;
1256 #warning Does not look right!
1257 if (!JAM_ON_STACK || !IS_JAM_OBJECT(objref) || !IS_JAM_ARRAY(objref)) {
1258 objref = JAM_OBJECT((uintptr_t *)objref);
1259 if (!IS_OBJECT(objref))
1260 objref = JAM_ARRAY((uintptr_t *)*ctx->arg1);
1261 } else {
1262 //turn type in the stack to JEM type ref
1263 *ctx->arg1 = (uintptr_t)INST_DATA(objref);
1265 JEM_NULL_POINTER_CHECK(objref);
1267 cb = CLASS_CB(objref->class);
1269 if ((cache >= cb->imethod_table_size) ||
1270 (ctx->new_mb->class != cb->imethod_table[cache].interface)) {
1271 for (cache = 0; (cache < cb->imethod_table_size) &&
1272 (ctx->new_mb->class != cb->imethod_table[cache].interface); cache++);
1274 if (cache == cb->imethod_table_size)
1275 return JEM_THROW_EXCEPTION("java/lang/IncompatibleClassChangeError",
1276 "unimplemented interface");
1279 mtbl_idx = cb->imethod_table[cache].offsets[ctx->new_mb->method_table_index];
1280 jam_dprintf("[OPC_INVOKEINTERFACE_QUICK] find method at %d\n", mtbl_idx);
1281 ctx->new_mb = cb->method_table[mtbl_idx];
1282 jam_dprintf("[OPC_INVOKEINTERFACE_QUICK] invoking method at %d\n", mtbl_idx);
1283 return invokeMethod(ctx);
1286 static int opc_invokeinterface(struct intrp_ctx *ctx)
1288 struct jem_state *jem = &ctx->jem;
1289 Operand *operand = &jem->operand;
1290 ExecEnv *ee = ctx->ee;
1292 int idx = operand->uu.u1 = jem->jecr & 0xffff;
1293 /*NOTES: invokeinterface opcode is supposed to take 4 operands.
1294 * For JEM, two index bytes has been processed before trap,
1295 * so offset jpc 4 operands for next opcode
1296 *FIXME: ? not know why JEM does not handle this */
1297 ctx->frame->last_pc = (CodePntr)(jem->jpc += 2);
1298 ctx-> new_mb = resolveInterfaceMethod(ctx->mb->class, idx);
1300 jam_dprintf("[OPC_INVOKEINTERFACE] constant pool index %d\n", idx);
1301 if (exceptionOccured0(ee))
1302 return jem_throwException(ctx);
1304 if (CLASS_CB(ctx->new_mb->class)->access_flags & ACC_INTERFACE) {
1305 operand->uu.u2 = 0;
1306 return opc_invokeinterface_quick(ctx);
1307 } else {
1308 operand->uu.u1 = ctx->new_mb->args_count;
1309 operand->uu.u2 = ctx->new_mb->method_table_index;
1310 return opc_invokevirtual_quick(ctx);
1314 static int opc_instanceof_quick(struct intrp_ctx *ctx)
1316 struct jem_state *jem = &ctx->jem;
1317 ConstantPool *cp = ctx->cp;
1318 Class *class = RESOLVED_CLASS(jem);
1319 Object *obj;
1320 uintptr_t *ptr;
1322 ostack_pop_u32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack, ptr);
1323 if (ptr != NULL) {
1324 obj = JAM_OBJECT(ptr);
1325 if (!IS_OBJECT(obj)) {
1326 jam_dprintf("[OPC_INSTANCEOF_QUICK] instanceof for array\n");
1327 //should be array ref
1328 obj = JAM_ARRAY(ptr);
1331 jam_dprintf("[OPC_INSTANCEOF_QUICK] check instanceof class %s for obj(%s)\n",
1332 CLASS_CB(class)->name, CLASS_CB(obj->class)->name);
1334 ostack_push_u32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack,
1335 ptr ? (uint32_t)isInstanceOf(class, obj->class) : 0);
1336 return 0;
1339 static int opc_instanceof(struct intrp_ctx *ctx)
1341 struct jem_state *jem = &ctx->jem;
1342 Operand *operand = &jem->operand;
1343 int idx = jem->jecr & 0xffff;
1344 int opcode = (jem->jecr >> 16) & 0xff;
1345 Class *class;
1346 ExecEnv *ee = ctx->ee;
1348 operand->uui.u1 = idx;
1349 operand->uui.u2 = opcode;
1351 jam_dprintf("[OPC_INSTANCEOF] index: 0x%x\n", idx);
1353 ctx->frame->last_pc = (CodePntr)jem->jpc;
1354 //resolve the cast class
1355 class = resolveClass(ctx->mb->class, idx, FALSE);
1357 if (exceptionOccured0(ee))
1358 return jem_throwException(ctx);
1360 return opc_instanceof_quick(ctx);
1363 static int opc_aastore(struct intrp_ctx *ctx)
1365 Object *obj;
1366 uintptr_t *aobj;
1367 int idx ;
1368 Object *array;
1370 ostack_pop_u32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack, aobj);
1371 ostack_pop_u32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack, idx);
1372 ostack_pop_u32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack, array);
1374 jam_dprintf("[OPC_AASTORE] obj %p, index %d, array %p\n", aobj, idx, array);
1376 /* aobj does point to an object, but we do not know if that's an array or
1377 * not, and we need to know that for the arrayStoreCheck() check below */
1378 #warning Does not look right!
1379 if (aobj && (!JAM_ON_STACK || !IS_JAM_OBJECT(aobj) || !IS_JAM_ARRAY(aobj))) {
1380 if (!IS_OBJECT(JAM_OBJECT(aobj))) {
1381 //in case of storing an array to another array
1382 obj = JAM_ARRAY(aobj);
1383 } else {
1384 obj = JAM_OBJECT(aobj);
1386 } else
1387 obj = (Object *)aobj;
1388 array = JAM_ARRAY((uintptr_t *)array);
1390 jam_dprintf("[OPC_AASTORE] store obj ref %p of %p to array %p[%d/%d] of %p\n",
1391 obj, obj->class, array, idx, array->instance[0], array->class);
1392 JEM_NULL_POINTER_CHECK(array);
1394 if (idx >= ARRAY_LEN(array)) {
1395 char buff[MAX_INT_DIGITS];
1396 snprintf(buff, MAX_INT_DIGITS, "%d", idx);
1397 JEM_THROW_EXCEPTION("java/lang/ArrayIndexOutOfBoundsException", buff);
1400 if (obj != NULL && !arrayStoreCheck(array->class, obj->class))
1401 JEM_THROW_EXCEPTION("java/lang/ArrayStoreException", NULL);
1403 //FIXME: If we set JEM ref here, it is very likely that some native jamvm code would directly offset
1404 // the array to get the non jamvm refs to use, which then causes segfault.
1405 #if JAM_ON_STACK
1406 ((Object**)ARRAY_DATA(array))[idx] = obj;
1407 #else
1408 ((uintptr_t**)ARRAY_DATA(array))[idx] = aobj;
1409 #endif
1410 return 0;
1413 static int do_javaExceptions(struct intrp_ctx *ctx)
1415 union jecr jecr_u;
1416 struct jem_state *jem = &ctx->jem;
1418 /* deliberate segfault */
1419 /* *(int *)0 = 0; */
1421 jecr_u.i = jem->jecr;
1422 /* This should not happen, because we don't use the "Java Handle" (H)
1423 * bit in the Status Register and we set JBCR to 0x80000000 - still
1424 * it does happen, but seems to be harmless so far. Just ignore. */
1425 if (jecr_u.s.opcode == OPC_AASTORE)
1426 return opc_aastore(ctx);
1428 return 1;
1431 static int opc_afireturn(struct intrp_ctx *ctx)
1433 uint32_t val;
1434 int ret;
1435 struct jem_state *jem = &ctx->jem;
1437 ostack_pop_u32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack, val);
1439 jam_dprintf("[OPC_AFIRETURN] return 0x%08x @ %p, this %p\n",
1440 val, ctx->lvars_jem, ctx->this);
1442 ret = opc_return(ctx);
1443 if (!ret)
1444 ret = ostack_push_u32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack, val);
1445 else
1446 /* Function returning a value must have local variables */
1447 *ctx->lvars_jem = val;
1449 return ret;
1452 static int opc_ldreturn(struct intrp_ctx *ctx)
1454 uint64_t val;
1455 int ret;
1457 struct jem_state *jem = &ctx->jem;
1458 ostack_pop_u64(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack, val);
1460 jam_dprintf("[OPC_LDRETURN] return %lld\n", val);
1462 ret = opc_return(ctx);
1464 if (!ret)
1465 ret = ostack_push_u64(ctx->frame->ostack, ctx->mb->max_stack,
1466 ctx->ostack, val);
1467 else
1468 /* Function returning a value must have local variables */
1469 *(uint64_t*)ctx->lvars_jem = val;
1471 return ret;
1474 static int opc_fcmpg(struct intrp_ctx *ctx)
1476 float v1, v2;
1477 int res;
1478 struct jem_state *jem = &ctx->jem;
1479 ostack_pop_f32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack, v2);
1480 ostack_pop_f32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack, v1);
1481 if (v1 == v2)
1482 res = 0;
1483 else if (v1 < v2)
1484 res = -1;
1485 else if (v1 > v2)
1486 res = 1;
1487 else
1488 res = 1; /* isNaN */
1489 jam_dprintf("[OPC_FCMPG] result %d\n", res);
1490 ostack_push_u32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack, res);
1491 return 0;
1494 static int opc_fcmpl(struct intrp_ctx *ctx)
1496 float v1, v2;
1497 int res;
1498 struct jem_state *jem = &ctx->jem;
1499 ostack_pop_f32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack, v2);
1500 ostack_pop_f32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack, v1);
1501 if (v1 == v2)
1502 res = 0;
1503 else if (v1 < v2)
1504 res = -1;
1505 else if (v1 > v2)
1506 res = 1;
1507 else
1508 res = -1;
1509 jam_dprintf("[OPC_FCMPL] result %d\n", res);
1510 ostack_push_u32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack, res);
1511 return 0;
1514 static int opc_i2f(struct intrp_ctx *ctx)
1516 int i;
1517 struct jem_state *jem = &ctx->jem;
1518 ostack_pop_u32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack, i);
1520 float f = (float)i;
1521 jam_dprintf("[OPC_I2F] convert to %f\n", f);
1522 ostack_push_f32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack, f);
1523 return 0;
1526 static int opc_i2d(struct intrp_ctx *ctx)
1528 return JEM_TRAP_HANDLER_FINISH;
1531 static int opc_fmul(struct intrp_ctx *ctx)
1533 float v1, v2;
1534 float ret;
1535 struct jem_state *jem = &ctx->jem;
1536 ostack_pop_f32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack, v2);
1537 ostack_pop_f32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack, v1);
1538 ret = v1 * v2;
1539 jam_dprintf("[OPC_FMUL] result %f\n", ret);
1540 ostack_push_f32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack, ret);
1541 return 0;
1544 static int opc_fadd(struct intrp_ctx *ctx)
1546 float v1, v2;
1547 float ret;
1548 struct jem_state *jem = &ctx->jem;
1549 ostack_pop_f32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack, v2);
1550 ostack_pop_f32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack, v1);
1551 ret = v1 + v2;
1552 jam_dprintf("[OPC_FADD] result %f\n", ret);
1553 ostack_push_f32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack, ret);
1554 return 0;
1557 static int opc_fsub(struct intrp_ctx *ctx)
1559 float v1, v2;
1560 float ret;
1561 struct jem_state *jem = &ctx->jem;
1562 ostack_pop_f32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack, v2);
1563 ostack_pop_f32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack, v1);
1564 ret = v1 - v2;
1565 jam_dprintf("[OPC_FSUB] result %f\n", ret);
1566 ostack_push_f32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack, ret);
1567 return 0;
1570 static int opc_f2i(struct intrp_ctx *ctx)
1572 int res;
1573 float value;
1574 struct jem_state *jem = &ctx->jem;
1575 ostack_pop_f32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack, value);
1576 jam_dprintf("[OPC_F2I] float value %f\n", value);
1577 if (value >= (float)INT_MAX)
1578 res = INT_MAX;
1579 else if (value <= (float)INT_MIN)
1580 res = INT_MIN;
1581 else if (value != value)
1582 res = 0;
1583 else
1584 res = (int)value;
1585 jam_dprintf("[OPC_F2I] int value %d\n", res);
1586 ostack_push_u32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack, res);
1587 return 0;
1590 static int opc_iinc(struct intrp_ctx *ctx)
1592 union jecr jecr_u;
1593 jecr_u.i = ctx->jem.jecr;
1594 int index = (int)jecr_u.s.op1;
1595 int con = (int)jecr_u.s.op2;
1596 uintptr_t *ptr = ctx->lvars_jem;
1598 jam_dprintf("[OPC_IINC] add %d to local var %x at %d (%d)\n",
1599 con, ptr[-index], index, ctx->mb->max_locals);
1600 ptr[-index] = ptr[-index] + con;
1602 jam_dprintf("[OPC_IINC] local var is %x now\n", ptr[-index]);
1603 return 0;
1606 static int opc_tableswitch(struct intrp_ctx *ctx)
1608 /* On entry JPC points to "default" */
1609 int32_t dflt, high, low, *dflt_p;
1610 int32_t idx;
1611 uint32_t target;
1612 uint8_t *tswitch;
1613 int i;
1615 tswitch = (uint8_t *)(ctx->jem.jpc - 3);
1616 dflt_p = (int32_t *)(ctx->jem.jpc & ~3);
1618 dflt = __be32_to_cpu(*dflt_p);
1619 low = __be32_to_cpu(*(dflt_p + 1));
1620 high = __be32_to_cpu(*(dflt_p + 2));
1622 ostack_pop_u32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack, idx);
1624 if (idx < low || idx > high)
1625 target = dflt;
1626 else
1627 target = __be32_to_cpu(*(dflt_p + 3 + idx - low));
1629 #ifdef JEM_DEBUG
1630 for (i = 0; i <= high - low; i++) {
1631 int32_t *addr = dflt_p + 3 + i;
1633 if (!(i & 15))
1634 jam_dprintf("%p:", addr);
1635 jam_dprintf(" 0x%x", *addr);
1636 if ((i & 15) == 15 || i == high - low)
1637 jam_dprintf("\n", addr);
1639 #endif
1641 jam_dprintf("[%s] trap @%p, jpc 0x%x, default 0x%x @ %p, low %d, high %d, idx %d, target 0x%x\n",
1642 __func__, tswitch, ctx->jem.jpc, dflt, dflt_p, low, high, idx, target);
1644 ctx->jem.jpc = (uint32_t)(tswitch + target);
1646 return 0;
1649 static int opc_athrow(struct intrp_ctx *ctx)
1651 Object *obj;
1652 ostack_pop_u32(ctx->frame->ostack, ctx->mb->max_stack, ctx->ostack, obj);
1653 ctx->frame->last_pc = (CodePntr)ctx->jem.jpc;
1654 ExecEnv *ee = ctx->ee;
1656 JEM_NULL_POINTER_CHECK(obj);
1658 if (!JAM_ON_STACK || !IS_JAM_OBJECT(obj))
1659 obj = JAM_OBJECT((uintptr_t *)obj);
1661 jam_dprintf("[OPC_ATHROW] throw exception %s\n", CLASS_CB(obj->class)->name);
1662 ee->exception = obj;
1663 return jem_throwException(ctx);
1666 static int do_stackOverflow(struct intrp_ctx *ctx)
1668 jam_dprintf("[TRAP] STACK OVERFLOW XXXXXXXX\n");
1669 return 0;
1672 static int do_stackUnderflow(struct intrp_ctx *ctx)
1674 jam_dprintf("[TRAP] STACK UNDERFLOW XXXXXXXX\n");
1675 return 1;
1678 static int do_stackOverflow1(struct intrp_ctx *ctx)
1680 jam_dprintf("[TRAP] STACK OVERFLOW1XXXXXXXX\n");
1681 return 1;
1684 static int do_stackUnderflow1(struct intrp_ctx *ctx)
1686 jam_dprintf("[TRAP] STACK UNDERFLOW1XXXXXXXX\n");
1687 return 1;
1690 static handler_fn *trap_handler_f[256] = {
1691 [OPC_INVOKEINTERFACE] = opc_invokeinterface,
1692 [OPC_INVOKESPECIAL] = opc_invokespecial,
1693 [OPC_INVOKESUPER_QUICK] = opc_invokesuper_quick,
1694 [OPC_INVOKENONVIRTUAL_QUICK] = opc_invokenonvirtual_quick,
1695 [OPC_INVOKEVIRTUAL] = opc_invokevirtual,
1696 [OPC_INVOKEVIRTUAL_QUICK] = opc_invokevirtual_quick,
1697 [OPC_INVOKESTATIC] = opc_invokestatic,
1698 // [OPC_INVOKESTATIC_QUICK] = opc_invokestatic_quick,
1699 [OPC_NEW] = opc_new,
1700 [0xDD] = opc_new, /* JEM: NEW_QUICK */
1701 [OPC_RETURN] = opc_return,
1702 [OPC_ARETURN] = opc_afireturn,
1703 [OPC_IRETURN] = opc_afireturn,
1704 [OPC_FRETURN] = opc_afireturn,
1705 [OPC_PUTFIELD] = opc_putfield,
1706 // [OPC_PUTFIELD_QUICK] = opc_putfield_quick,
1707 // [OPC_PUTFIELD2_QUICK] = opc_putfield2_quick,
1708 // [OPC_LDC_W_QUICK] = opc_ldc_w_quick,
1709 // [OPC_LDC_QUICK] = opc_ldc_quick,
1710 [OPC_LDC_W] = opc_ldc_w,
1711 [OPC_LDC] = opc_ldc,
1712 [OPC_LDC2_W] = opc_ldc2_w,
1713 [OPC_MONITORENTER] = opc_monitorenter,
1714 [OPC_MONITOREXIT] = opc_monitorexit,
1715 [OPC_GETSTATIC] = opc_getstatic,
1716 // [OPC_GETSTATIC_QUICK] = opc_getstatic_quick,
1717 // [OPC_GETSTATIC2_QUICK] = opc_getstatic2_quick,
1718 [OPC_PUTSTATIC] = opc_putstatic,
1719 [OPC_PUTSTATIC_QUICK] = opc_putstatic_quick,
1720 [OPC_PUTSTATIC2_QUICK] = opc_putstatic2_quick,
1721 [OPC_ANEWARRAY] = opc_anewarray,
1722 [OPC_ANEWARRAY_QUICK] = opc_anewarray_quick,
1723 [OPC_MULTIANEWARRAY] = opc_multianewarray,
1724 [OPC_NEWARRAY] = opc_newarray,
1725 [OPC_CHECKCAST] = opc_checkcast,
1726 [OPC_CHECKCAST_QUICK] = opc_checkcast_quick,
1727 [OPC_GETFIELD] = opc_getfield,
1728 // [OPC_GETFIELD_QUICK] = opc_getfield_quick,
1729 // [OPC_GETFIELD2_QUICK] = opc_getfield2_quick,
1730 [OPC_FCMPL] = opc_fcmpl,
1731 [OPC_FCMPG] = opc_fcmpg,
1732 [OPC_I2F] = opc_i2f,
1733 [OPC_I2D] = opc_i2d,
1734 [OPC_FMUL] = opc_fmul,
1735 // [OPC_FADD] = opc_fadd,
1736 // [OPC_FSUB] = opc_fsub,
1737 [OPC_F2I] = opc_f2i,
1738 [OPC_IINC] = opc_iinc,
1739 [OPC_INSTANCEOF] = opc_instanceof,
1740 [OPC_ATHROW] = opc_athrow,
1741 [OPC_LRETURN] = opc_ldreturn,
1742 [OPC_TABLESWITCH] = opc_tableswitch,
1745 static handler_fn *exception_handler_f[] = {
1746 [0] = do_javaExceptions,
1747 [1] = do_stackOverflow,
1748 [2] = do_stackUnderflow,
1749 [3] = do_stackOverflow1,
1750 [4] = do_stackUnderflow1,
1753 struct jem_profile {
1754 unsigned long time;
1755 unsigned int n;
1758 static struct jem_profile prof[256];
1760 /******************************/
1761 uintptr_t *executeJava(void)
1764 * load trap entries to JEM trap table set when initializing JamVM
1766 int i;
1767 uint32_t jep;
1768 int done;
1769 #ifdef JEM_PROFILE
1770 static struct timeval tv1, tv2;
1771 #endif
1772 ExecEnv *ee = getExecEnv();
1774 /* Initialize interpreter's static environment */
1775 struct intrp_ctx ctx;
1776 struct jem_state *jem = &ctx.jem;
1778 ctx.ee = ee;
1779 ctx.frame = ee->last_frame;
1780 ctx.mb = ctx.frame->mb;
1782 * We use ostack to save overflowing Java Operand Stack elements. On a stack
1783 * overflow we free a half of the stack (4 elements) to avoid an immediate
1784 * next overflow. When entering a trap, the Operand Stack is saved in a
1785 * temporary array in the reverse order:
1786 * ATTENTION: Looks like ToS is really in r0, not in r7, as JEM manual says
1787 * jos[0] = r7 (?)
1788 * ...
1789 * jos[7 - N] = rN (ToS - N)
1790 * ...
1791 * jos[7] = r0 (ToS)
1792 * UPDATE (27.06.2008)
1793 * I think, actually, the manual is right in some way... If you access
1794 * registers individually _when_ JOSP == N + 1 != 0. If they are read out
1795 * with an ldm, you get this:
1796 * jos[0] = r7 (?)
1797 * ...
1798 * jos[7 - N] = rN (ToS)
1799 * ...
1800 * jos[7] = r0 (ToS - N)
1801 * on an overflow we push r3..r0, i.e., jos[4..7] to the frame stack, and
1802 * set saved JOSP to 4.
1804 unsigned long jos[8];
1806 ctx.ostack = ctx.frame->ostack;
1807 ctx.cp = &(CLASS_CB(ctx.mb->class)->constant_pool);
1808 /* End enterpreter's static environment */
1810 //fetch the original pc before prepare direct mb
1811 //TODO:
1812 /* I think, mb->code is indeed a copy of the original code[] array from the
1813 * class object. See class.c::defineClass():390 */
1815 if (ctx.frame->lvars_jem) {
1816 ctx.lvars_jem = ctx.frame->lvars_jem - 1;
1818 GET_THIS(&ctx);
1819 } else {
1820 jam_printf("BUG: lvars_jem == NULL!!!");
1821 exitVM(1);
1824 jam_dprintf("[executeJava] ostack %08x locals %d lvars_jem %08x\n",
1825 ctx.ostack, ctx.mb->max_locals, ctx.lvars_jem);
1826 jam_dprintf("[executeJava] execute method %s on class %s\n",
1827 ctx.mb->name, CLASS_CB(ctx.mb->class)->name);
1829 #if 0
1830 /* Don't think we need it, at least, not in the direct.c form. There the code
1831 * is converted into an internal representation, which is useless for JEM */
1832 PREPARE_MB(ctx.mb);//see direct.c (prepare)
1833 #endif
1835 jem->jpc = (unsigned long)ctx.mb->code & ~3;
1836 jam_dprintf("[executeJava] begin on opcode %04x (%04x)\n",
1837 *(char*)jem->jpc, *((char*)jem->jpc + 1));
1839 do {
1840 #ifdef JEM_PROFILE
1841 /* This is not thread-safe. Have to use TLS. */
1842 static
1843 #endif
1844 int opcode;
1846 #ifdef JEM_DEBUG
1847 jam_dprintf("About to enter JEM at 0x%08x, this = %p, class = %p. Code:",
1848 jem->jpc, ctx.this, ctx.this ? ctx.this->class : NULL);
1849 for (i = 0; i < 32 && jem->jpc + i < (unsigned long)(ctx.mb->code + ctx.mb->code_size); i++) {
1850 if (!(i & 0xf))
1851 jam_dprintf("\n0x%04x:", i);
1852 jam_dprintf(" 0x%02x", ((char*)jem->jpc)[i]);
1855 jam_dprintf("\nUp to 8 first Local Variables out of %d:\n", ctx.mb->max_locals);
1856 for (i = 0; i < min(8, ctx.mb->max_locals); i++)
1857 jam_dprintf("LVAR_%d: 0x%08x\n", i, *(ctx.lvars_jem - i), ctx.lvars_jem - i);
1858 #endif
1860 /* On entry we need last Java Operand Stack depth, set it not deeper
1861 * than half hardware stack */
1862 jem->josp = min(5, ostack_depth(ctx.frame->ostack, ctx.ostack));
1863 jam_dprintf("Pop %d words to JEM stack from %p:%p\n", jem->josp,
1864 ctx.frame->ostack, ctx.ostack);
1865 /* As long as we are in asm, top jem.josp stack elements belong JEM */
1866 for (i = 0; i < jem->josp; i++)
1867 ostack_pop_u32(ctx.frame->ostack, ctx.mb->max_stack, ctx.ostack,
1868 jos[7 - jem->josp + 1 + i]);
1870 #ifdef JEM_PROFILE
1871 if (tv1.tv_sec) {
1872 gettimeofday(&tv2, NULL);
1873 prof[opcode].time += (tv2.tv_sec - tv1.tv_sec) * 1000000 +
1874 tv2.tv_usec - tv1.tv_usec;
1875 prof[opcode].n++;
1877 #endif
1879 __asm__ __volatile__(
1880 ".globl debug_jem_enter\n"
1881 "debug_jem_enter:\n"
1882 " pushm r0-r7,r10-r11\n"
1883 " mov r8, %[cp]\n"
1884 " mov r9, %[lv0]\n"
1885 " mov lr, %[jpc]\n"
1886 " mov r12, %[josp]\n" /* Cannot use %[josp] ... */
1887 " mov r11, %[ostack]\n"
1888 " pushm r11\n" /* Need ostack below */
1889 " ldm r11, r0-r7\n" /* Restore Java Operands */
1890 " cp.w r12, 0\n" /* Test josp == 0 */
1891 " breq 4f\n"
1892 "5: incjosp 1\n" /* ... for loop counter, */
1893 " sub r12, 1\n" /* because it can be */
1894 " brne 5b\n" /* one of r0-r7 */
1895 "4: popjc\n" /* Restore Local Variables */
1896 " sub r10, pc, . - trap\n" /* These 3 instructions */
1897 " retj\n" /* have to stay */
1898 "trap: pushjc\n" /* together */
1899 " mfsr r11, 0x58\n" /* JOSP */
1900 " lsl r11, 28\n" /* Clear r11[31:3] using */
1901 " lsr r11, 28\n" /* one register */
1902 " mov r8, r11\n"
1903 " breq 2f\n"
1904 "3: incjosp -1\n"
1905 " sub r11, 1\n"
1906 " brne 3b\n"
1907 "2: popm r11\n" /* ostack */
1908 " stm r11, r0-r7\n" /* Save Java Operands */
1909 " popm r0-r7,r10-r11\n"
1910 " mov %[tpc], r12\n"
1911 " mov %[jpc], lr\n" /* Resume address */
1912 " mfsr %[jecr], 0x54\n" /* JECR */
1913 " mov %[josp], r8\n" /* JOSP */
1914 " .globl debug_jem_exit\n"
1915 "debug_jem_exit:\n"
1916 : [jecr] "=r" (jem->jecr), [tpc] "=r" (jem->trap_pc),
1917 [josp] "+r" (jem->josp), [jpc] "+r" (jem->jpc)
1918 : [cp] "r" (ctx.cp->info), [lv0] "r" (ctx.lvars_jem),
1919 [ostack] "r" (jos)
1920 : "r8", "r9", "lr", "r12", "sp", "memory", "cc"
1923 * trap_pc now holds trap pc, describing which trap has been entered,
1924 * jecr holds JECR with the trapping bytecode at bits 23..16, possibly
1925 * arguments at bits 7..0 and 15..8,
1926 * josp holds JOSP (JOSP & 0xf),
1927 * jos holds stored registers r7-r0 (note order!) - Java Operand Stack
1928 * jpc points at the trapping opcode, in case of exceptions and stack
1929 * over- and underflows, and at the next opcode, in case of a trap
1932 #ifdef JEM_PROFILE
1933 gettimeofday(&tv1, NULL);
1934 #endif
1936 for (i = 0; i < jem->josp; i++)
1937 ostack_push_u32(ctx.frame->ostack, ctx.mb->max_stack, ctx.ostack,
1938 jos[7 - i]);
1940 opcode = (jem->jecr >> 16) & 0xff;
1942 jep = jem->trap_pc & 0xfff;
1944 trap_debug(&ctx);
1945 if (jep < 0x280)
1946 done = exception_handler_f[jep >> 7](&ctx);
1947 else if (trap_handler_f[opcode])
1948 done = trap_handler_f[opcode](&ctx);
1949 else {
1950 /* Unimplemented */
1951 jam_printf("Unimplemented Java Opcode 0x%02x\n", opcode);
1952 exitVM(1);
1955 #ifdef JEM_DEBUG
1956 usleep(100);
1957 #endif
1958 } while (done != JEM_TRAP_HANDLER_FINISH);
1960 return ctx.ostack;
1962 // DISPATCH_FIRST;
1964 jam_dprintf("Unknown Java Opcodes\n");
1965 exitVM(1);
1968 #ifndef executeJava
1969 void initialiseInterpreter(InitArgs *args) {
1970 initialiseDirect(args);
1972 #endif
1974 void dump_profile(void)
1976 #ifdef JEM_PROFILE
1977 int i;
1979 for (i = 0; i < 256; i++)
1980 if (prof[i].n)
1981 jam_printf("0x%x:\t%u times,\t%lu usec\n", i, prof[i].n, prof[i].time);
1982 #endif