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.
23 #include <arpa/inet.h>
35 #include "interp-direct.h"
37 #include <sys/syscall.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"
61 * Interpreter execution context
67 /* the interpreter execution context */
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); \
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); \
91 typedef int handler_fn(struct intrp_ctx
*ctx
);
93 static void jem_opcode_rewrite(uint32_t new_opcode
, char *pc
, int argc
, ...)
98 va_start(vargs
, argc
);
100 for (i
= 1; i
<= argc
; i
++)
101 pc
[i
] = (char)va_arg(vargs
, int);
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) \
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
)
128 struct jem_state
*jem
= &ctx
->jem
;
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
++) {
142 if (ostack_read_u32(ctx
->frame
->ostack
, ctx
->ostack
, i
, &v
) >= 0)
143 jam_printf("\tToS-%d: 0x%x\n", i
, v
);
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 */
164 ee
->exception
= excep
;
165 /* Original code had a "return NULL" here, which means, end of
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
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;
188 ctx
->cp
= &(CLASS_CB(ctx
->mb
->class)->constant_pool
);
190 /* FIXME: don't know if this is correct, depends how we implement
192 ostack_push_u32(ctx
->frame
->ostack
, ctx
->mb
->max_stack
, ctx
->ostack
, (uint32_t)excep
);
194 /* Dispatch to the first bytecode */
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;
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
);
233 ee
->last_frame
= ctx
->frame
;
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
;
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");
265 ee
->stack_end
+= STACK_RED_ZONE_SIZE
;
266 return JEM_THROW_EXCEPTION("java/lang/StackOverflowError", NULL
);
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
);
284 *(new_frame
->lvars_jem
- 1 - i
) = 0xdeadbeef;
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
);
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
);
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");
323 ctx
->frame
= new_frame
;
324 ctx
->mb
= ctx
->new_mb
;
325 ctx
->lvars_jem
= new_frame
->lvars_jem
- 1;
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
);
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
;
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
);
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",
396 return invokeMethod(ctx
);
399 static int opc_invokevirtual(struct intrp_ctx
*ctx
)
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
)
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
);
450 operand
->pntr
= ctx
->new_mb
;
451 return opc_invokenonvirtual_quick(ctx
);
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
)
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
);
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;
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
;
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
);
550 signalException("java/lang/NegativeArraySizeException", NULL
);
551 return jem_throwException(ctx
);
554 ac_name
= sysMalloc(strlen(name
) + 4);
557 sprintf(ac_name
, "[%s", name
);
559 sprintf(ac_name
, "[L%s;", name
);
561 array_class
= findArrayClassFromClass(ac_name
, ctx
->mb
->class);
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;
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
);
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
);
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;
635 ExecEnv
*ee
= ctx
->ee
;
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;
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
)
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
;
690 static int opc_putfield2_quick(struct intrp_ctx
*ctx
)
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
);
710 static int opc_putfield_quick_jem0(struct intrp_ctx
*ctx
)
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
);
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
);
758 static int opc_putfield(struct intrp_ctx
*ctx
)
760 struct jem_state
*jem
= &ctx
->jem
;
761 int idx
= jem
->jecr
& 0xffff;
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
;
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
);
782 jem_opcode_rewrite(0xCF, (char*)jem
->jpc
- 3, 2, fb
->offset
>> 8, fb
->offset
);
783 return opc_putfield_quick_jem0(ctx
);
785 jem_opcode_rewrite(0xCF, (char*)jem
->jpc
- 3, 2, fb
->offset
>> 8, fb
->offset
);
786 return opc_putfield_quick_jem1(ctx
);
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
)
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
)
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
)
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
)];
840 Object
*ref
= (Object
*)value
;
841 if (JAM_ON_STACK
&& IS_JAM_OBJECT(ref
))
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
)
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
);
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;
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);
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
905 jem_opcode_rewrite(0xCE, (char*)jem
->jpc
- 3, 2, fb
->offset
>> 8, fb
->offset
);
906 return opc_getfield_quick_jem0(ctx
);
908 jem_opcode_rewrite(0xCE, (char*)jem
->jpc
- 3, 2, fb
->offset
>> 8, fb
->offset
);
909 return opc_getfield_quick_jem1(ctx
);
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_ldc_w_quick_jem(struct intrp_ctx
*ctx
)
918 struct jem_state
*jem
= &ctx
->jem
;
919 //CONSTANT_Class and CONSTANT_String are stored as object reference
920 Object
*cp_info
= (Object
*)CP_INFO(ctx
->cp
, DOUBLE_INDEX(jem
));
921 jam_dprintf("[OPC_LDC_W_QUICK_JEM] push String ref %x to stack\n", cp_info
);
922 return ostack_push_u32(ctx
->frame
->ostack
, ctx
->mb
->max_stack
, ctx
->ostack
,
923 (uint32_t)INST_DATA(cp_info
));
926 static int opc_ldc_quick(struct intrp_ctx
*ctx
)
928 struct jem_state
*jem
= &ctx
->jem
;
929 uint32_t constant
= RESOLVED_CONSTANT(jem
);
930 jam_dprintf("[OPC_LDC_QUICK] push constant %x to stack\n", constant
);
931 return ostack_push_u32(ctx
->frame
->ostack
, ctx
->mb
->max_stack
, ctx
->ostack
, (uintptr_t)constant
);
934 static int opc_ldc_common(struct intrp_ctx
*ctx
, int idx
)
936 struct jem_state
*jem
= &ctx
->jem
;
937 ExecEnv
*ee
= ctx
->ee
;
938 ctx
->frame
->last_pc
= (CodePntr
)jem
->jpc
;
940 jem
->operand
.u
= resolveSingleConstant(ctx
->mb
->class, idx
);
942 if (exceptionOccured0(ee
))
943 return jem_throwException(ctx
);
945 switch (CP_TYPE(ctx
->cp
, idx
)) {
946 case CONSTANT_ResolvedClass
:
947 case CONSTANT_ResolvedString
:
948 jem
->operand
.i
= idx
;
949 return opc_ldc_w_quick_jem(ctx
);
951 jem_opcode_rewrite(0xCB, (char*)jem
->jpc
- 2, 0);
952 return opc_ldc_quick(ctx
);
956 static int opc_ldc(struct intrp_ctx
*ctx
)
959 ExecEnv
*ee
= ctx
->ee
;
960 struct jem_state
*jem
= &ctx
->jem
;
962 idx
= (jem
->jecr
& 0xff00) >> 8;
964 jam_dprintf("[OPC_LDC] constant pool index %d\n", idx
);
965 return opc_ldc_common(ctx
, idx
);
968 static int opc_ldc_w(struct intrp_ctx
*ctx
)
971 ExecEnv
*ee
= ctx
->ee
;
972 struct jem_state
*jem
= &ctx
->jem
;
974 idx
= (jem
->jecr
& 0xffff);
976 jam_dprintf("[OPC_LDC_W] constant pool index %d\n", idx
);
977 return opc_ldc_common(ctx
, idx
);
980 static int opc_monitorenter(struct intrp_ctx
*ctx
)
984 struct jem_state
*jem
= &ctx
->jem
;
985 ostack_pop_u32(ctx
->frame
->ostack
, ctx
->mb
->max_stack
, ctx
->ostack
, ptr
);
987 obj
= JAM_OBJECT(ptr
);
989 /* is an array ref */
990 obj
= JAM_ARRAY(ptr
);
991 jam_dprintf("[OPC_MONITORENTER] lock %x\n", obj
);
992 JEM_NULL_POINTER_CHECK(obj
);
997 static int opc_monitorexit(struct intrp_ctx
*ctx
)
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_MONITOREXIT] unlock %x\n", obj
);
1009 JEM_NULL_POINTER_CHECK(obj
);
1014 static int opc_getstatic_quick(struct intrp_ctx
*ctx
)
1016 struct jem_state
*jem
= &ctx
->jem
;
1017 return ostack_push_u32(ctx
->frame
->ostack
, ctx
->mb
->max_stack
, ctx
->ostack
,
1018 RESOLVED_FIELD(jem
)->static_value
);
1021 static int opc_getstatic2_quick(struct intrp_ctx
*ctx
)
1023 struct jem_state
*jem
= &ctx
->jem
;
1024 return ostack_push_u64(ctx
->frame
->ostack
, ctx
->mb
->max_stack
, ctx
->ostack
,
1025 RESOLVED_FIELD(jem
)->static_value
);
1028 static int opc_getstatic_quick_jem0(struct intrp_ctx
*ctx
)
1030 struct jem_state
*jem
= &ctx
->jem
;
1031 uintptr_t value
= RESOLVED_FIELD(jem
)->static_value
;
1033 jam_dprintf("[OPC_GETSTATIC_QUICK_JEM0] obj ref %x\n", value
);
1035 Object
*ref
= (Object
*)value
;
1036 if (!JAM_ON_STACK
|| IS_JAM_OBJECT(ref
))
1038 value
= (uintptr_t)INST_DATA(ref
);
1040 jam_dprintf("[OPC_GETSTATIC_QUICK_JEM0] put obj ref %x\n", value
);
1041 return ostack_push_u32(ctx
->frame
->ostack
, ctx
->mb
->max_stack
, ctx
->ostack
, value
);
1044 static int opc_getstatic_quick_jem1(struct intrp_ctx
*ctx
)
1046 struct jem_state
*jem
= &ctx
->jem
;
1047 uintptr_t value
= RESOLVED_FIELD(jem
)->static_value
;
1048 jam_dprintf("[OPC_GETSTATIC_QUICK_JEM1] array ref %x\n", value
);
1050 Object
*ref
= (Object
*)value
;
1051 if (!JAM_ON_STACK
|| IS_JAM_ARRAY(ref
))
1053 value
= (uintptr_t)ARRAY_DATA(ref
);
1055 jam_dprintf("[OPC_GETSTATIC_QUICK_JEM1] put array ref %x\n", value
);
1056 return ostack_push_u32(ctx
->frame
->ostack
, ctx
->mb
->max_stack
, ctx
->ostack
, value
);
1059 static int opc_getstatic(struct intrp_ctx
*ctx
)
1064 ExecEnv
*ee
= ctx
->ee
;
1065 struct jem_state
*jem
= &ctx
->jem
;
1067 idx
= (int)(jem
->jecr
& 0xffff);
1068 jam_dprintf("[OPC_GETSTATIC] constant pool index %d class %s\n",idx
,CLASS_CB(ctx
->mb
->class)->name
);
1070 ctx
->frame
->last_pc
= (CodePntr
)jem
->jpc
;
1071 fb
= resolveField(ctx
->mb
->class, idx
);
1073 if (exceptionOccured0(ee
))
1074 return jem_throwException(ctx
);
1076 jam_dprintf("[OPC_GETSTATIC] get static for field %08x, type %c\n", fb
, *fb
->type
);
1077 jem
->operand
.pntr
= fb
;
1078 switch (*fb
->type
) {
1081 return opc_getstatic2_quick(ctx
);
1086 return opc_getstatic_quick_jem0(ctx
);
1088 return opc_getstatic_quick_jem1(ctx
);
1090 return opc_getstatic_quick(ctx
);
1094 static int opc_putstatic2_quick(struct intrp_ctx
*ctx
)
1096 struct jem_state
*jem
= &ctx
->jem
;
1097 return ostack_pop_u64(ctx
->frame
->ostack
, ctx
->mb
->max_stack
, ctx
->ostack
,
1098 *(u8
*)&RESOLVED_FIELD(jem
)->static_value
);
1101 static int opc_putstatic_quick(struct intrp_ctx
*ctx
)
1103 struct jem_state
*jem
= &ctx
->jem
;
1104 return ostack_pop_u32(ctx
->frame
->ostack
, ctx
->mb
->max_stack
, ctx
->ostack
,
1105 RESOLVED_FIELD(jem
)->static_value
);
1108 static int opc_putstatic_quick_jem0(struct intrp_ctx
*ctx
)
1111 struct jem_state
*jem
= &ctx
->jem
;
1112 ostack_pop_u32(ctx
->frame
->ostack
, ctx
->mb
->max_stack
, ctx
->ostack
, value
);
1114 jam_dprintf("[OPC_PUTSTATIC_QUICK_JEM0] obj %x\n", value
);
1115 if (value
&& (!JAM_ON_STACK
|| !IS_JAM_OBJECT(value
)))
1116 value
= (uintptr_t)JAM_OBJECT((uintptr_t *)value
);
1117 jam_dprintf("[OPC_PUTSTATIC_QUICK_JEM0] put obj %x\n", value
);
1118 RESOLVED_FIELD(jem
)->static_value
= value
;
1122 static int opc_putstatic_quick_jem1(struct intrp_ctx
*ctx
)
1125 struct jem_state
*jem
= &ctx
->jem
;
1126 ostack_pop_u32(ctx
->frame
->ostack
, ctx
->mb
->max_stack
, ctx
->ostack
, value
);
1128 jam_dprintf("[OPC_PUTSTATIC_QUICK_JEM1] array %p\n", value
);
1129 if (value
&& (!JAM_ON_STACK
|| !IS_JAM_ARRAY(value
)))
1130 value
= (uintptr_t*)JAM_ARRAY(value
);
1131 jam_dprintf("[OPC_PUTSTATIC_QUICK_JEM1] put array %p\n", value
);
1132 RESOLVED_FIELD(jem
)->static_value
= (uintptr_t)value
;
1136 static int opc_putstatic(struct intrp_ctx
*ctx
)
1141 ExecEnv
*ee
= ctx
->ee
;
1142 struct jem_state
*jem
= &ctx
->jem
;
1144 idx
= (int)(jem
->jecr
& 0xffff);
1146 ctx
->frame
->last_pc
= (CodePntr
)jem
->jpc
;
1147 fb
= resolveField(ctx
->mb
->class, idx
);
1149 if (exceptionOccured0(ee
))
1150 return jem_throwException(ctx
);
1152 jam_dprintf("[OPC_PUTSTATIC] put static for field %08x, type %c\n", fb
, *fb
->type
);
1153 jem
->operand
.pntr
= fb
;
1154 switch (*fb
->type
) {
1157 //jem_opcode_rewrite(0xD5, (char*)jem->jpc - 3);
1158 return opc_putstatic2_quick(ctx
);
1160 return opc_putstatic_quick_jem0(ctx
);
1162 return opc_putstatic_quick_jem1(ctx
);
1164 return opc_putstatic_quick(ctx
);
1168 static int opc_checkcast_quick(struct intrp_ctx
*ctx
)
1170 struct jem_state
*jem
= &ctx
->jem
;
1171 ConstantPool
*cp
= ctx
->cp
;
1172 Class
*class = RESOLVED_CLASS(jem
);
1176 ostack_read_u32(ctx
->frame
->ostack
, ctx
->ostack
, 0, &ptr
);
1177 jam_dprintf("[OPC_CHECKCAST_QUICK] check cast for JEM type %x (class %s)\n", ptr
, CLASS_CB(class)->name
);
1179 #warning Does not look right!
1180 if (!JAM_ON_STACK
|| !IS_JAM_OBJECT(ptr
) || !IS_JAM_ARRAY(ptr
)) {
1181 obj
= JAM_OBJECT(ptr
);
1182 if (!IS_OBJECT(obj
)) {
1183 jam_dprintf("[OPC_CHECKCAST_QUICK] check cast for array\n");
1184 //should be array ref
1185 obj
= JAM_ARRAY(ptr
);
1187 jam_dprintf("[OPC_CHECKCAST_QUICK] check cast for object %x\n", obj
);
1191 jam_dprintf("[OPC_CHECKCAST_QUICK] check class %p (%s) for obj %p (%s)\n",
1192 class, CLASS_CB(class)->name
, obj
->class, CLASS_CB(obj
->class)->name
);
1193 if (!isInstanceOf(class, obj
->class))
1194 return JEM_THROW_EXCEPTION("java/lang/ClassCastException", CLASS_CB(obj
->class)->name
);
1200 static int opc_checkcast(struct intrp_ctx
*ctx
)
1202 struct jem_state
*jem
= &ctx
->jem
;
1203 Operand
*operand
= &jem
->operand
;
1204 int idx
= jem
->jecr
& 0xffff;
1205 int opcode
= (jem
->jecr
>> 16) & 0xff;
1207 ExecEnv
*ee
= ctx
->ee
;
1209 operand
->uui
.u1
= idx
;
1210 operand
->uui
.u2
= opcode
;
1212 jam_dprintf("[OPC_CHECKCAST] index: 0x%x\n", idx
);
1214 ctx
->frame
->last_pc
= (CodePntr
)jem
->jpc
;
1215 //resolve the cast class
1216 class = resolveClass(ctx
->mb
->class, idx
, FALSE
);
1218 if (exceptionOccured0(ee
))
1219 return jem_throwException(ctx
);
1221 return opc_checkcast_quick(ctx
);
1224 static int opc_invokeinterface_quick(struct intrp_ctx
*ctx
)
1229 int cache
= ctx
->jem
.operand
.uu
.u2
;
1231 ctx
->new_mb
= (MethodBlock
*)CP_INFO(ctx
->cp
, ctx
->jem
.operand
.uu
.u1
);
1232 ctx
->arg1
= ostack_address(ctx
->frame
->ostack
, ctx
->mb
->max_stack
,
1233 ctx
->ostack
- ctx
->frame
->ostack
- ctx
->new_mb
->args_count
);
1235 jam_dprintf("[OPC_INVOKEINTERFACE_QUICK] newmb %x (name %s) (args count %d) objref(jem) %x\n",
1236 ctx
->new_mb
, ctx
->new_mb
->name
, ctx
->new_mb
->args_count
, ctx
->arg1
);
1238 objref
= (Object
*)*ctx
->arg1
;
1239 #warning Does not look right!
1240 if (!JAM_ON_STACK
|| !IS_JAM_OBJECT(objref
) || !IS_JAM_ARRAY(objref
)) {
1241 objref
= JAM_OBJECT((uintptr_t *)objref
);
1242 if (!IS_OBJECT(objref
))
1243 objref
= JAM_ARRAY((uintptr_t *)*ctx
->arg1
);
1245 //turn type in the stack to JEM type ref
1246 *ctx
->arg1
= (uintptr_t)INST_DATA(objref
);
1248 JEM_NULL_POINTER_CHECK(objref
);
1250 cb
= CLASS_CB(objref
->class);
1252 if ((cache
>= cb
->imethod_table_size
) ||
1253 (ctx
->new_mb
->class != cb
->imethod_table
[cache
].interface
)) {
1254 for (cache
= 0; (cache
< cb
->imethod_table_size
) &&
1255 (ctx
->new_mb
->class != cb
->imethod_table
[cache
].interface
); cache
++);
1257 if (cache
== cb
->imethod_table_size
)
1258 return JEM_THROW_EXCEPTION("java/lang/IncompatibleClassChangeError",
1259 "unimplemented interface");
1262 mtbl_idx
= cb
->imethod_table
[cache
].offsets
[ctx
->new_mb
->method_table_index
];
1263 jam_dprintf("[OPC_INVOKEINTERFACE_QUICK] find method at %d\n", mtbl_idx
);
1264 ctx
->new_mb
= cb
->method_table
[mtbl_idx
];
1265 jam_dprintf("[OPC_INVOKEINTERFACE_QUICK] invoking method at %d\n", mtbl_idx
);
1266 return invokeMethod(ctx
);
1269 static int opc_invokeinterface(struct intrp_ctx
*ctx
)
1271 struct jem_state
*jem
= &ctx
->jem
;
1272 Operand
*operand
= &jem
->operand
;
1273 ExecEnv
*ee
= ctx
->ee
;
1275 int idx
= operand
->uu
.u1
= jem
->jecr
& 0xffff;
1276 /*NOTES: invokeinterface opcode is supposed to take 4 operands.
1277 * For JEM, two index bytes has been processed before trap,
1278 * so offset jpc 4 operands for next opcode
1279 *FIXME: ? not know why JEM does not handle this */
1280 ctx
->frame
->last_pc
= (CodePntr
)(jem
->jpc
+= 2);
1281 ctx
-> new_mb
= resolveInterfaceMethod(ctx
->mb
->class, idx
);
1283 jam_dprintf("[OPC_INVOKEINTERFACE] constant pool index %d\n", idx
);
1284 if (exceptionOccured0(ee
))
1285 return jem_throwException(ctx
);
1287 if (CLASS_CB(ctx
->new_mb
->class)->access_flags
& ACC_INTERFACE
) {
1289 return opc_invokeinterface_quick(ctx
);
1291 operand
->uu
.u1
= ctx
->new_mb
->args_count
;
1292 operand
->uu
.u2
= ctx
->new_mb
->method_table_index
;
1293 return opc_invokevirtual_quick(ctx
);
1297 static int opc_instanceof_quick(struct intrp_ctx
*ctx
)
1299 struct jem_state
*jem
= &ctx
->jem
;
1300 ConstantPool
*cp
= ctx
->cp
;
1301 Class
*class = RESOLVED_CLASS(jem
);
1305 ostack_pop_u32(ctx
->frame
->ostack
, ctx
->mb
->max_stack
, ctx
->ostack
, ptr
);
1307 obj
= JAM_OBJECT(ptr
);
1308 if (!IS_OBJECT(obj
)) {
1309 jam_dprintf("[OPC_INSTANCEOF_QUICK] instanceof for array\n");
1310 //should be array ref
1311 obj
= JAM_ARRAY(ptr
);
1314 jam_dprintf("[OPC_INSTANCEOF_QUICK] check instanceof class %s for obj(%s)\n",
1315 CLASS_CB(class)->name
, CLASS_CB(obj
->class)->name
);
1317 ostack_push_u32(ctx
->frame
->ostack
, ctx
->mb
->max_stack
, ctx
->ostack
,
1318 ptr
? (uint32_t)isInstanceOf(class, obj
->class) : 0);
1322 static int opc_instanceof(struct intrp_ctx
*ctx
)
1324 struct jem_state
*jem
= &ctx
->jem
;
1325 Operand
*operand
= &jem
->operand
;
1326 int idx
= jem
->jecr
& 0xffff;
1327 int opcode
= (jem
->jecr
>> 16) & 0xff;
1329 ExecEnv
*ee
= ctx
->ee
;
1331 operand
->uui
.u1
= idx
;
1332 operand
->uui
.u2
= opcode
;
1334 jam_dprintf("[OPC_INSTANCEOF] index: 0x%x\n", idx
);
1336 ctx
->frame
->last_pc
= (CodePntr
)jem
->jpc
;
1337 //resolve the cast class
1338 class = resolveClass(ctx
->mb
->class, idx
, FALSE
);
1340 if (exceptionOccured0(ee
))
1341 return jem_throwException(ctx
);
1343 return opc_instanceof_quick(ctx
);
1346 static int opc_aastore(struct intrp_ctx
*ctx
)
1353 ostack_pop_u32(ctx
->frame
->ostack
, ctx
->mb
->max_stack
, ctx
->ostack
, aobj
);
1354 ostack_pop_u32(ctx
->frame
->ostack
, ctx
->mb
->max_stack
, ctx
->ostack
, idx
);
1355 ostack_pop_u32(ctx
->frame
->ostack
, ctx
->mb
->max_stack
, ctx
->ostack
, array
);
1357 jam_dprintf("[OPC_AASTORE] obj %p, index %d, array %p\n", aobj
, idx
, array
);
1359 /* aobj does point to an object, but we do not know if that's an array or
1360 * not, and we need to know that for the arrayStoreCheck() check below */
1361 #warning Does not look right!
1362 if (aobj
&& (!JAM_ON_STACK
|| !IS_JAM_OBJECT(aobj
) || !IS_JAM_ARRAY(aobj
))) {
1363 if (!IS_OBJECT(JAM_OBJECT(aobj
))) {
1364 //in case of storing an array to another array
1365 obj
= JAM_ARRAY(aobj
);
1367 obj
= JAM_OBJECT(aobj
);
1370 obj
= (Object
*)aobj
;
1371 array
= JAM_ARRAY((uintptr_t *)array
);
1373 jam_dprintf("[OPC_AASTORE] store obj ref %p of %p to array %p[%d/%d] of %p\n",
1374 obj
, obj
->class, array
, idx
, array
->instance
[0], array
->class);
1375 JEM_NULL_POINTER_CHECK(array
);
1377 if (idx
>= ARRAY_LEN(array
)) {
1378 char buff
[MAX_INT_DIGITS
];
1379 snprintf(buff
, MAX_INT_DIGITS
, "%d", idx
);
1380 JEM_THROW_EXCEPTION("java/lang/ArrayIndexOutOfBoundsException", buff
);
1383 if (obj
!= NULL
&& !arrayStoreCheck(array
->class, obj
->class))
1384 JEM_THROW_EXCEPTION("java/lang/ArrayStoreException", NULL
);
1386 //FIXME: If we set JEM ref here, it is very likely that some native jamvm code would directly offset
1387 // the array to get the non jamvm refs to use, which then causes segfault.
1389 ((Object
**)ARRAY_DATA(array
))[idx
] = obj
;
1391 ((uintptr_t**)ARRAY_DATA(array
))[idx
] = aobj
;
1396 static int do_javaExceptions(struct intrp_ctx
*ctx
)
1399 struct jem_state
*jem
= &ctx
->jem
;
1401 /* deliberate segfault */
1402 /* *(int *)0 = 0; */
1404 jecr_u
.i
= jem
->jecr
;
1405 /* This should not happen, because we don't use the "Java Handle" (H)
1406 * bit in the Status Register and we set JBCR to 0x80000000 - still
1407 * it does happen, but seems to be harmless so far. Just ignore. */
1408 if (jecr_u
.s
.opcode
== OPC_AASTORE
)
1409 return opc_aastore(ctx
);
1414 static int opc_afireturn(struct intrp_ctx
*ctx
)
1418 struct jem_state
*jem
= &ctx
->jem
;
1420 ostack_pop_u32(ctx
->frame
->ostack
, ctx
->mb
->max_stack
, ctx
->ostack
, val
);
1422 jam_dprintf("[OPC_AFIRETURN] return 0x%08x @ %p, this %p\n",
1423 val
, ctx
->lvars_jem
, ctx
->this);
1425 ret
= opc_return(ctx
);
1427 ret
= ostack_push_u32(ctx
->frame
->ostack
, ctx
->mb
->max_stack
, ctx
->ostack
, val
);
1429 /* Function returning a value must have local variables */
1430 *ctx
->lvars_jem
= val
;
1435 static int opc_ldreturn(struct intrp_ctx
*ctx
)
1440 struct jem_state
*jem
= &ctx
->jem
;
1441 ostack_pop_u64(ctx
->frame
->ostack
, ctx
->mb
->max_stack
, ctx
->ostack
, val
);
1443 jam_dprintf("[OPC_LDRETURN] return %lld\n", val
);
1445 ret
= opc_return(ctx
);
1448 ret
= ostack_push_u64(ctx
->frame
->ostack
, ctx
->mb
->max_stack
,
1451 /* Function returning a value must have local variables */
1452 *(uint64_t*)ctx
->lvars_jem
= val
;
1457 static int opc_fcmpg(struct intrp_ctx
*ctx
)
1461 struct jem_state
*jem
= &ctx
->jem
;
1462 ostack_pop_f32(ctx
->frame
->ostack
, ctx
->mb
->max_stack
, ctx
->ostack
, v2
);
1463 ostack_pop_f32(ctx
->frame
->ostack
, ctx
->mb
->max_stack
, ctx
->ostack
, v1
);
1471 res
= 1; /* isNaN */
1472 jam_dprintf("[OPC_FCMPG] result %d\n", res
);
1473 ostack_push_u32(ctx
->frame
->ostack
, ctx
->mb
->max_stack
, ctx
->ostack
, res
);
1477 static int opc_fcmpl(struct intrp_ctx
*ctx
)
1481 struct jem_state
*jem
= &ctx
->jem
;
1482 ostack_pop_f32(ctx
->frame
->ostack
, ctx
->mb
->max_stack
, ctx
->ostack
, v2
);
1483 ostack_pop_f32(ctx
->frame
->ostack
, ctx
->mb
->max_stack
, ctx
->ostack
, v1
);
1492 jam_dprintf("[OPC_FCMPL] result %d\n", res
);
1493 ostack_push_u32(ctx
->frame
->ostack
, ctx
->mb
->max_stack
, ctx
->ostack
, res
);
1497 static int opc_i2f(struct intrp_ctx
*ctx
)
1500 struct jem_state
*jem
= &ctx
->jem
;
1501 ostack_pop_u32(ctx
->frame
->ostack
, ctx
->mb
->max_stack
, ctx
->ostack
, i
);
1504 jam_dprintf("[OPC_I2F] convert to %f\n", f
);
1505 ostack_push_f32(ctx
->frame
->ostack
, ctx
->mb
->max_stack
, ctx
->ostack
, f
);
1509 static int opc_i2d(struct intrp_ctx
*ctx
)
1511 return JEM_TRAP_HANDLER_FINISH
;
1514 static int opc_fmul(struct intrp_ctx
*ctx
)
1518 struct jem_state
*jem
= &ctx
->jem
;
1519 ostack_pop_f32(ctx
->frame
->ostack
, ctx
->mb
->max_stack
, ctx
->ostack
, v2
);
1520 ostack_pop_f32(ctx
->frame
->ostack
, ctx
->mb
->max_stack
, ctx
->ostack
, v1
);
1522 jam_dprintf("[OPC_FMUL] result %f\n", ret
);
1523 ostack_push_f32(ctx
->frame
->ostack
, ctx
->mb
->max_stack
, ctx
->ostack
, ret
);
1527 static int opc_fadd(struct intrp_ctx
*ctx
)
1531 struct jem_state
*jem
= &ctx
->jem
;
1532 ostack_pop_f32(ctx
->frame
->ostack
, ctx
->mb
->max_stack
, ctx
->ostack
, v2
);
1533 ostack_pop_f32(ctx
->frame
->ostack
, ctx
->mb
->max_stack
, ctx
->ostack
, v1
);
1535 jam_dprintf("[OPC_FADD] result %f\n", ret
);
1536 ostack_push_f32(ctx
->frame
->ostack
, ctx
->mb
->max_stack
, ctx
->ostack
, ret
);
1540 static int opc_fsub(struct intrp_ctx
*ctx
)
1544 struct jem_state
*jem
= &ctx
->jem
;
1545 ostack_pop_f32(ctx
->frame
->ostack
, ctx
->mb
->max_stack
, ctx
->ostack
, v2
);
1546 ostack_pop_f32(ctx
->frame
->ostack
, ctx
->mb
->max_stack
, ctx
->ostack
, v1
);
1548 jam_dprintf("[OPC_FSUB] result %f\n", ret
);
1549 ostack_push_f32(ctx
->frame
->ostack
, ctx
->mb
->max_stack
, ctx
->ostack
, ret
);
1553 static int opc_f2i(struct intrp_ctx
*ctx
)
1557 struct jem_state
*jem
= &ctx
->jem
;
1558 ostack_pop_f32(ctx
->frame
->ostack
, ctx
->mb
->max_stack
, ctx
->ostack
, value
);
1559 jam_dprintf("[OPC_F2I] float value %f\n", value
);
1560 if (value
>= (float)INT_MAX
)
1562 else if (value
<= (float)INT_MIN
)
1564 else if (value
!= value
)
1568 jam_dprintf("[OPC_F2I] int value %d\n", res
);
1569 ostack_push_u32(ctx
->frame
->ostack
, ctx
->mb
->max_stack
, ctx
->ostack
, res
);
1573 static int opc_iinc(struct intrp_ctx
*ctx
)
1576 jecr_u
.i
= ctx
->jem
.jecr
;
1577 int index
= (int)jecr_u
.s
.op1
;
1578 int con
= (int)jecr_u
.s
.op2
;
1579 uintptr_t *ptr
= ctx
->lvars_jem
;
1581 jam_dprintf("[OPC_IINC] add %d to local var %x at %d (%d)\n",
1582 con
, ptr
[-index
], index
, ctx
->mb
->max_locals
);
1583 ptr
[-index
] = ptr
[-index
] + con
;
1585 jam_dprintf("[OPC_IINC] local var is %x now\n", ptr
[-index
]);
1589 static int opc_tableswitch(struct intrp_ctx
*ctx
)
1591 /* On entry JPC points to "default" */
1592 int32_t dflt
, high
, low
, *dflt_p
;
1598 tswitch
= (uint8_t *)(ctx
->jem
.jpc
- 3);
1599 dflt_p
= (int32_t *)(ctx
->jem
.jpc
& ~3);
1601 dflt
= __be32_to_cpu(*dflt_p
);
1602 low
= __be32_to_cpu(*(dflt_p
+ 1));
1603 high
= __be32_to_cpu(*(dflt_p
+ 2));
1605 ostack_pop_u32(ctx
->frame
->ostack
, ctx
->mb
->max_stack
, ctx
->ostack
, idx
);
1607 if (idx
< low
|| idx
> high
)
1610 target
= __be32_to_cpu(*(dflt_p
+ 3 + idx
- low
));
1613 for (i
= 0; i
<= high
- low
; i
++) {
1614 int32_t *addr
= dflt_p
+ 3 + i
;
1617 jam_dprintf("%p:", addr
);
1618 jam_dprintf(" 0x%x", *addr
);
1619 if ((i
& 15) == 15 || i
== high
- low
)
1620 jam_dprintf("\n", addr
);
1624 jam_dprintf("[%s] trap @%p, jpc 0x%x, default 0x%x @ %p, low %d, high %d, idx %d, target 0x%x\n",
1625 __func__
, tswitch
, ctx
->jem
.jpc
, dflt
, dflt_p
, low
, high
, idx
, target
);
1627 ctx
->jem
.jpc
= (uint32_t)(tswitch
+ target
);
1632 static int opc_athrow(struct intrp_ctx
*ctx
)
1635 ostack_pop_u32(ctx
->frame
->ostack
, ctx
->mb
->max_stack
, ctx
->ostack
, obj
);
1636 ctx
->frame
->last_pc
= (CodePntr
)ctx
->jem
.jpc
;
1637 ExecEnv
*ee
= ctx
->ee
;
1639 JEM_NULL_POINTER_CHECK(obj
);
1641 if (!JAM_ON_STACK
|| !IS_JAM_OBJECT(obj
))
1642 obj
= JAM_OBJECT((uintptr_t *)obj
);
1644 jam_dprintf("[OPC_ATHROW] throw exception %s\n", CLASS_CB(obj
->class)->name
);
1645 ee
->exception
= obj
;
1646 return jem_throwException(ctx
);
1649 static int do_stackOverflow(struct intrp_ctx
*ctx
)
1651 jam_dprintf("[TRAP] STACK OVERFLOW XXXXXXXX\n");
1655 static int do_stackUnderflow(struct intrp_ctx
*ctx
)
1657 jam_dprintf("[TRAP] STACK UNDERFLOW XXXXXXXX\n");
1661 static int do_stackOverflow1(struct intrp_ctx
*ctx
)
1663 jam_dprintf("[TRAP] STACK OVERFLOW1XXXXXXXX\n");
1667 static int do_stackUnderflow1(struct intrp_ctx
*ctx
)
1669 jam_dprintf("[TRAP] STACK UNDERFLOW1XXXXXXXX\n");
1673 static handler_fn
*trap_handler_f
[256] = {
1674 [OPC_INVOKEINTERFACE
] = opc_invokeinterface
,
1675 [OPC_INVOKESPECIAL
] = opc_invokespecial
,
1676 [OPC_INVOKESUPER_QUICK
] = opc_invokesuper_quick
,
1677 [OPC_INVOKENONVIRTUAL_QUICK
] = opc_invokenonvirtual_quick
,
1678 [OPC_INVOKEVIRTUAL
] = opc_invokevirtual
,
1679 [OPC_INVOKEVIRTUAL_QUICK
] = opc_invokevirtual_quick
,
1680 [OPC_INVOKESTATIC
] = opc_invokestatic
,
1681 // [OPC_INVOKESTATIC_QUICK] = opc_invokestatic_quick,
1682 [OPC_NEW
] = opc_new
,
1683 [0xDD] = opc_new
, /* JEM: NEW_QUICK */
1684 [OPC_RETURN
] = opc_return
,
1685 [OPC_ARETURN
] = opc_afireturn
,
1686 [OPC_IRETURN
] = opc_afireturn
,
1687 [OPC_FRETURN
] = opc_afireturn
,
1688 [OPC_PUTFIELD
] = opc_putfield
,
1689 // [OPC_PUTFIELD_QUICK] = opc_putfield_quick,
1690 // [OPC_PUTFIELD2_QUICK] = opc_putfield2_quick,
1691 // [OPC_LDC_W_QUICK] = opc_ldc_w_quick,
1692 // [OPC_LDC_QUICK] = opc_ldc_quick,
1693 [OPC_LDC_W
] = opc_ldc_w
,
1694 [OPC_LDC
] = opc_ldc
,
1695 [OPC_MONITORENTER
] = opc_monitorenter
,
1696 [OPC_MONITOREXIT
] = opc_monitorexit
,
1697 [OPC_GETSTATIC
] = opc_getstatic
,
1698 // [OPC_GETSTATIC_QUICK] = opc_getstatic_quick,
1699 // [OPC_GETSTATIC2_QUICK] = opc_getstatic2_quick,
1700 [OPC_PUTSTATIC
] = opc_putstatic
,
1701 [OPC_PUTSTATIC_QUICK
] = opc_putstatic_quick
,
1702 [OPC_PUTSTATIC2_QUICK
] = opc_putstatic2_quick
,
1703 [OPC_ANEWARRAY
] = opc_anewarray
,
1704 [OPC_ANEWARRAY_QUICK
] = opc_anewarray_quick
,
1705 [OPC_MULTIANEWARRAY
] = opc_multianewarray
,
1706 [OPC_NEWARRAY
] = opc_newarray
,
1707 [OPC_CHECKCAST
] = opc_checkcast
,
1708 [OPC_CHECKCAST_QUICK
] = opc_checkcast_quick
,
1709 [OPC_GETFIELD
] = opc_getfield
,
1710 // [OPC_GETFIELD_QUICK] = opc_getfield_quick,
1711 // [OPC_GETFIELD2_QUICK] = opc_getfield2_quick,
1712 [OPC_FCMPL
] = opc_fcmpl
,
1713 [OPC_FCMPG
] = opc_fcmpg
,
1714 [OPC_I2F
] = opc_i2f
,
1715 [OPC_I2D
] = opc_i2d
,
1716 [OPC_FMUL
] = opc_fmul
,
1717 // [OPC_FADD] = opc_fadd,
1718 // [OPC_FSUB] = opc_fsub,
1719 [OPC_F2I
] = opc_f2i
,
1720 [OPC_IINC
] = opc_iinc
,
1721 [OPC_INSTANCEOF
] = opc_instanceof
,
1722 [OPC_ATHROW
] = opc_athrow
,
1723 [OPC_LRETURN
] = opc_ldreturn
,
1724 [OPC_TABLESWITCH
] = opc_tableswitch
,
1727 static handler_fn
*exception_handler_f
[] = {
1728 [0] = do_javaExceptions
,
1729 [1] = do_stackOverflow
,
1730 [2] = do_stackUnderflow
,
1731 [3] = do_stackOverflow1
,
1732 [4] = do_stackUnderflow1
,
1735 struct jem_profile
{
1740 static struct jem_profile prof
[256];
1742 /******************************/
1743 uintptr_t *executeJava(void)
1746 * load trap entries to JEM trap table set when initializing JamVM
1752 static struct timeval tv1
, tv2
;
1754 ExecEnv
*ee
= getExecEnv();
1756 /* Initialize interpreter's static environment */
1757 struct intrp_ctx ctx
;
1758 struct jem_state
*jem
= &ctx
.jem
;
1761 ctx
.frame
= ee
->last_frame
;
1762 ctx
.mb
= ctx
.frame
->mb
;
1764 * We use ostack to save overflowing Java Operand Stack elements. On a stack
1765 * overflow we free a half of the stack (4 elements) to avoid an immediate
1766 * next overflow. When entering a trap, the Operand Stack is saved in a
1767 * temporary array in the reverse order:
1768 * ATTENTION: Looks like ToS is really in r0, not in r7, as JEM manual says
1771 * jos[7 - N] = rN (ToS - N)
1774 * UPDATE (27.06.2008)
1775 * I think, actually, the manual is right in some way... If you access
1776 * registers individually _when_ JOSP == N + 1 != 0. If they are read out
1777 * with an ldm, you get this:
1780 * jos[7 - N] = rN (ToS)
1782 * jos[7] = r0 (ToS - N)
1783 * on an overflow we push r3..r0, i.e., jos[4..7] to the frame stack, and
1784 * set saved JOSP to 4.
1786 unsigned long jos
[8];
1788 ctx
.ostack
= ctx
.frame
->ostack
;
1789 ctx
.cp
= &(CLASS_CB(ctx
.mb
->class)->constant_pool
);
1790 /* End enterpreter's static environment */
1792 //fetch the original pc before prepare direct mb
1794 /* I think, mb->code is indeed a copy of the original code[] array from the
1795 * class object. See class.c::defineClass():390 */
1797 if (ctx
.frame
->lvars_jem
) {
1798 ctx
.lvars_jem
= ctx
.frame
->lvars_jem
- 1;
1802 jam_printf("BUG: lvars_jem == NULL!!!");
1806 jam_dprintf("[executeJava] ostack %08x locals %d lvars_jem %08x\n",
1807 ctx
.ostack
, ctx
.mb
->max_locals
, ctx
.lvars_jem
);
1808 jam_dprintf("[executeJava] execute method %s on class %s\n",
1809 ctx
.mb
->name
, CLASS_CB(ctx
.mb
->class)->name
);
1812 /* Don't think we need it, at least, not in the direct.c form. There the code
1813 * is converted into an internal representation, which is useless for JEM */
1814 PREPARE_MB(ctx
.mb
);//see direct.c (prepare)
1817 jem
->jpc
= (unsigned long)ctx
.mb
->code
& ~3;
1818 jam_dprintf("[executeJava] begin on opcode %04x (%04x)\n",
1819 *(char*)jem
->jpc
, *((char*)jem
->jpc
+ 1));
1823 /* This is not thread-safe. Have to use TLS. */
1829 jam_dprintf("About to enter JEM at 0x%08x, this = %p, class = %p. Code:",
1830 jem
->jpc
, ctx
.this, ctx
.this ? ctx
.this->class : NULL
);
1831 for (i
= 0; i
< 32 && jem
->jpc
+ i
< (unsigned long)(ctx
.mb
->code
+ ctx
.mb
->code_size
); i
++) {
1833 jam_dprintf("\n0x%04x:", i
);
1834 jam_dprintf(" 0x%02x", ((char*)jem
->jpc
)[i
]);
1837 jam_dprintf("\nUp to 8 first Local Variables out of %d:\n", ctx
.mb
->max_locals
);
1838 for (i
= 0; i
< min(8, ctx
.mb
->max_locals
); i
++)
1839 jam_dprintf("LVAR_%d: 0x%08x\n", i
, *(ctx
.lvars_jem
- i
), ctx
.lvars_jem
- i
);
1842 /* On entry we need last Java Operand Stack depth, set it not deeper
1843 * than half hardware stack */
1844 jem
->josp
= min(5, ostack_depth(ctx
.frame
->ostack
, ctx
.ostack
));
1845 jam_dprintf("Pop %d words to JEM stack from %p:%p\n", jem
->josp
,
1846 ctx
.frame
->ostack
, ctx
.ostack
);
1847 /* As long as we are in asm, top jem.josp stack elements belong JEM */
1848 for (i
= 0; i
< jem
->josp
; i
++)
1849 ostack_pop_u32(ctx
.frame
->ostack
, ctx
.mb
->max_stack
, ctx
.ostack
,
1850 jos
[7 - jem
->josp
+ 1 + i
]);
1854 gettimeofday(&tv2
, NULL
);
1855 prof
[opcode
].time
+= (tv2
.tv_sec
- tv1
.tv_sec
) * 1000000 +
1856 tv2
.tv_usec
- tv1
.tv_usec
;
1861 __asm__
__volatile__(
1862 ".globl debug_jem_enter\n"
1863 "debug_jem_enter:\n"
1864 " pushm r0-r7,r10-r11\n"
1868 " mov r12, %[josp]\n" /* Cannot use %[josp] ... */
1869 " mov r11, %[ostack]\n"
1870 " pushm r11\n" /* Need ostack below */
1871 " ldm r11, r0-r7\n" /* Restore Java Operands */
1872 " cp.w r12, 0\n" /* Test josp == 0 */
1874 "5: incjosp 1\n" /* ... for loop counter, */
1875 " sub r12, 1\n" /* because it can be */
1876 " brne 5b\n" /* one of r0-r7 */
1877 "4: popjc\n" /* Restore Local Variables */
1878 " sub r10, pc, . - trap\n" /* These 3 instructions */
1879 " retj\n" /* have to stay */
1880 "trap: pushjc\n" /* together */
1881 " mfsr r11, 0x58\n" /* JOSP */
1882 " lsl r11, 28\n" /* Clear r11[31:3] using */
1883 " lsr r11, 28\n" /* one register */
1889 "2: popm r11\n" /* ostack */
1890 " stm r11, r0-r7\n" /* Save Java Operands */
1891 " popm r0-r7,r10-r11\n"
1892 " mov %[tpc], r12\n"
1893 " mov %[jpc], lr\n" /* Resume address */
1894 " mfsr %[jecr], 0x54\n" /* JECR */
1895 " mov %[josp], r8\n" /* JOSP */
1896 " .globl debug_jem_exit\n"
1898 : [jecr
] "=r" (jem
->jecr
), [tpc
] "=r" (jem
->trap_pc
),
1899 [josp
] "+r" (jem
->josp
), [jpc
] "+r" (jem
->jpc
)
1900 : [cp
] "r" (ctx
.cp
->info
), [lv0
] "r" (ctx
.lvars_jem
),
1902 : "r8", "r9", "lr", "r12", "memory"
1905 * trap_pc now holds trap pc, describing which trap has been entered,
1906 * jecr holds JECR with the trapping bytecode at bits 23..16, possibly
1907 * arguments at bits 7..0 and 15..8,
1908 * josp holds JOSP (JOSP & 0xf),
1909 * jos holds stored registers r7-r0 (note order!) - Java Operand Stack
1910 * jpc points at the trapping opcode, in case of exceptions and stack
1911 * over- and underflows, and at the next opcode, in case of a trap
1915 gettimeofday(&tv1
, NULL
);
1918 for (i
= 0; i
< jem
->josp
; i
++)
1919 ostack_push_u32(ctx
.frame
->ostack
, ctx
.mb
->max_stack
, ctx
.ostack
,
1922 opcode
= (jem
->jecr
>> 16) & 0xff;
1924 jep
= jem
->trap_pc
& 0xfff;
1928 done
= exception_handler_f
[jep
>> 7](&ctx
);
1929 else if (trap_handler_f
[opcode
])
1930 done
= trap_handler_f
[opcode
](&ctx
);
1933 jam_printf("Unimplemented Java Opcode 0x%02x\n", opcode
);
1940 } while (done
!= JEM_TRAP_HANDLER_FINISH
);
1946 jam_dprintf("Unknown Java Opcodes\n");
1951 void initialiseInterpreter(InitArgs
*args
) {
1952 initialiseDirect(args
);
1956 void dump_profile(void)
1961 for (i
= 0; i
< 256; i
++)
1963 jam_printf("0x%x:\t%u times,\t%lu usec\n", i
, prof
[i
].n
, prof
[i
].time
);