Wed, 13 Mar 2013 09:44:45 +0100
8009761: Deoptimization on sparc doesn't set Llast_SP correctly in the interpreter frames it creates
Summary: deoptimization doesn't set up callee frames so that they restore caller frames correctly.
Reviewed-by: kvn
1 /*
2 * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
3 * Copyright 2007, 2008, 2009, 2010, 2011 Red Hat, Inc.
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
6 * This code is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 only, as
8 * published by the Free Software Foundation.
9 *
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
22 * questions.
23 *
24 */
26 #include "precompiled.hpp"
27 #include "asm/assembler.hpp"
28 #include "interpreter/bytecodeHistogram.hpp"
29 #include "interpreter/cppInterpreter.hpp"
30 #include "interpreter/interpreter.hpp"
31 #include "interpreter/interpreterGenerator.hpp"
32 #include "interpreter/interpreterRuntime.hpp"
33 #include "oops/arrayOop.hpp"
34 #include "oops/methodData.hpp"
35 #include "oops/method.hpp"
36 #include "oops/oop.inline.hpp"
37 #include "prims/jvmtiExport.hpp"
38 #include "prims/jvmtiThreadState.hpp"
39 #include "runtime/arguments.hpp"
40 #include "runtime/deoptimization.hpp"
41 #include "runtime/frame.inline.hpp"
42 #include "runtime/interfaceSupport.hpp"
43 #include "runtime/sharedRuntime.hpp"
44 #include "runtime/stubRoutines.hpp"
45 #include "runtime/synchronizer.hpp"
46 #include "runtime/timer.hpp"
47 #include "runtime/vframeArray.hpp"
48 #include "stack_zero.inline.hpp"
49 #include "utilities/debug.hpp"
50 #include "utilities/macros.hpp"
51 #ifdef SHARK
52 #include "shark/shark_globals.hpp"
53 #endif
55 #ifdef CC_INTERP
57 #define fixup_after_potential_safepoint() \
58 method = istate->method()
60 #define CALL_VM_NOCHECK_NOFIX(func) \
61 thread->set_last_Java_frame(); \
62 func; \
63 thread->reset_last_Java_frame();
65 #define CALL_VM_NOCHECK(func) \
66 CALL_VM_NOCHECK_NOFIX(func) \
67 fixup_after_potential_safepoint()
69 int CppInterpreter::normal_entry(Method* method, intptr_t UNUSED, TRAPS) {
70 JavaThread *thread = (JavaThread *) THREAD;
72 // Allocate and initialize our frame.
73 InterpreterFrame *frame = InterpreterFrame::build(method, CHECK_0);
74 thread->push_zero_frame(frame);
76 // Execute those bytecodes!
77 main_loop(0, THREAD);
79 // No deoptimized frames on the stack
80 return 0;
81 }
83 void CppInterpreter::main_loop(int recurse, TRAPS) {
84 JavaThread *thread = (JavaThread *) THREAD;
85 ZeroStack *stack = thread->zero_stack();
87 // If we are entering from a deopt we may need to call
88 // ourself a few times in order to get to our frame.
89 if (recurse)
90 main_loop(recurse - 1, THREAD);
92 InterpreterFrame *frame = thread->top_zero_frame()->as_interpreter_frame();
93 interpreterState istate = frame->interpreter_state();
94 Method* method = istate->method();
96 intptr_t *result = NULL;
97 int result_slots = 0;
99 while (true) {
100 // We can set up the frame anchor with everything we want at
101 // this point as we are thread_in_Java and no safepoints can
102 // occur until we go to vm mode. We do have to clear flags
103 // on return from vm but that is it.
104 thread->set_last_Java_frame();
106 // Call the interpreter
107 if (JvmtiExport::can_post_interpreter_events())
108 BytecodeInterpreter::runWithChecks(istate);
109 else
110 BytecodeInterpreter::run(istate);
111 fixup_after_potential_safepoint();
113 // Clear the frame anchor
114 thread->reset_last_Java_frame();
116 // Examine the message from the interpreter to decide what to do
117 if (istate->msg() == BytecodeInterpreter::call_method) {
118 Method* callee = istate->callee();
120 // Trim back the stack to put the parameters at the top
121 stack->set_sp(istate->stack() + 1);
123 // Make the call
124 Interpreter::invoke_method(callee, istate->callee_entry_point(), THREAD);
125 fixup_after_potential_safepoint();
127 // Convert the result
128 istate->set_stack(stack->sp() - 1);
130 // Restore the stack
131 stack->set_sp(istate->stack_limit() + 1);
133 // Resume the interpreter
134 istate->set_msg(BytecodeInterpreter::method_resume);
135 }
136 else if (istate->msg() == BytecodeInterpreter::more_monitors) {
137 int monitor_words = frame::interpreter_frame_monitor_size();
139 // Allocate the space
140 stack->overflow_check(monitor_words, THREAD);
141 if (HAS_PENDING_EXCEPTION)
142 break;
143 stack->alloc(monitor_words * wordSize);
145 // Move the expression stack contents
146 for (intptr_t *p = istate->stack() + 1; p < istate->stack_base(); p++)
147 *(p - monitor_words) = *p;
149 // Move the expression stack pointers
150 istate->set_stack_limit(istate->stack_limit() - monitor_words);
151 istate->set_stack(istate->stack() - monitor_words);
152 istate->set_stack_base(istate->stack_base() - monitor_words);
154 // Zero the new monitor so the interpreter can find it.
155 ((BasicObjectLock *) istate->stack_base())->set_obj(NULL);
157 // Resume the interpreter
158 istate->set_msg(BytecodeInterpreter::got_monitors);
159 }
160 else if (istate->msg() == BytecodeInterpreter::return_from_method) {
161 // Copy the result into the caller's frame
162 result_slots = type2size[result_type_of(method)];
163 assert(result_slots >= 0 && result_slots <= 2, "what?");
164 result = istate->stack() + result_slots;
165 break;
166 }
167 else if (istate->msg() == BytecodeInterpreter::throwing_exception) {
168 assert(HAS_PENDING_EXCEPTION, "should do");
169 break;
170 }
171 else if (istate->msg() == BytecodeInterpreter::do_osr) {
172 // Unwind the current frame
173 thread->pop_zero_frame();
175 // Remove any extension of the previous frame
176 int extra_locals = method->max_locals() - method->size_of_parameters();
177 stack->set_sp(stack->sp() + extra_locals);
179 // Jump into the OSR method
180 Interpreter::invoke_osr(
181 method, istate->osr_entry(), istate->osr_buf(), THREAD);
182 return;
183 }
184 else {
185 ShouldNotReachHere();
186 }
187 }
189 // Unwind the current frame
190 thread->pop_zero_frame();
192 // Pop our local variables
193 stack->set_sp(stack->sp() + method->max_locals());
195 // Push our result
196 for (int i = 0; i < result_slots; i++)
197 stack->push(result[-i]);
198 }
200 int CppInterpreter::native_entry(Method* method, intptr_t UNUSED, TRAPS) {
201 // Make sure method is native and not abstract
202 assert(method->is_native() && !method->is_abstract(), "should be");
204 JavaThread *thread = (JavaThread *) THREAD;
205 ZeroStack *stack = thread->zero_stack();
207 // Allocate and initialize our frame
208 InterpreterFrame *frame = InterpreterFrame::build(method, CHECK_0);
209 thread->push_zero_frame(frame);
210 interpreterState istate = frame->interpreter_state();
211 intptr_t *locals = istate->locals();
213 // Update the invocation counter
214 if ((UseCompiler || CountCompiledCalls) && !method->is_synchronized()) {
215 InvocationCounter *counter = method->invocation_counter();
216 counter->increment();
217 if (counter->reached_InvocationLimit()) {
218 CALL_VM_NOCHECK(
219 InterpreterRuntime::frequency_counter_overflow(thread, NULL));
220 if (HAS_PENDING_EXCEPTION)
221 goto unwind_and_return;
222 }
223 }
225 // Lock if necessary
226 BasicObjectLock *monitor;
227 monitor = NULL;
228 if (method->is_synchronized()) {
229 monitor = (BasicObjectLock*) istate->stack_base();
230 oop lockee = monitor->obj();
231 markOop disp = lockee->mark()->set_unlocked();
233 monitor->lock()->set_displaced_header(disp);
234 if (Atomic::cmpxchg_ptr(monitor, lockee->mark_addr(), disp) != disp) {
235 if (thread->is_lock_owned((address) disp->clear_lock_bits())) {
236 monitor->lock()->set_displaced_header(NULL);
237 }
238 else {
239 CALL_VM_NOCHECK(InterpreterRuntime::monitorenter(thread, monitor));
240 if (HAS_PENDING_EXCEPTION)
241 goto unwind_and_return;
242 }
243 }
244 }
246 // Get the signature handler
247 InterpreterRuntime::SignatureHandler *handler; {
248 address handlerAddr = method->signature_handler();
249 if (handlerAddr == NULL) {
250 CALL_VM_NOCHECK(InterpreterRuntime::prepare_native_call(thread, method));
251 if (HAS_PENDING_EXCEPTION)
252 goto unlock_unwind_and_return;
254 handlerAddr = method->signature_handler();
255 assert(handlerAddr != NULL, "eh?");
256 }
257 if (handlerAddr == (address) InterpreterRuntime::slow_signature_handler) {
258 CALL_VM_NOCHECK(handlerAddr =
259 InterpreterRuntime::slow_signature_handler(thread, method, NULL,NULL));
260 if (HAS_PENDING_EXCEPTION)
261 goto unlock_unwind_and_return;
262 }
263 handler = \
264 InterpreterRuntime::SignatureHandler::from_handlerAddr(handlerAddr);
265 }
267 // Get the native function entry point
268 address function;
269 function = method->native_function();
270 assert(function != NULL, "should be set if signature handler is");
272 // Build the argument list
273 stack->overflow_check(handler->argument_count() * 2, THREAD);
274 if (HAS_PENDING_EXCEPTION)
275 goto unlock_unwind_and_return;
277 void **arguments;
278 void *mirror; {
279 arguments =
280 (void **) stack->alloc(handler->argument_count() * sizeof(void **));
281 void **dst = arguments;
283 void *env = thread->jni_environment();
284 *(dst++) = &env;
286 if (method->is_static()) {
287 istate->set_oop_temp(
288 method->constants()->pool_holder()->java_mirror());
289 mirror = istate->oop_temp_addr();
290 *(dst++) = &mirror;
291 }
293 intptr_t *src = locals;
294 for (int i = dst - arguments; i < handler->argument_count(); i++) {
295 ffi_type *type = handler->argument_type(i);
296 if (type == &ffi_type_pointer) {
297 if (*src) {
298 stack->push((intptr_t) src);
299 *(dst++) = stack->sp();
300 }
301 else {
302 *(dst++) = src;
303 }
304 src--;
305 }
306 else if (type->size == 4) {
307 *(dst++) = src--;
308 }
309 else if (type->size == 8) {
310 src--;
311 *(dst++) = src--;
312 }
313 else {
314 ShouldNotReachHere();
315 }
316 }
317 }
319 // Set up the Java frame anchor
320 thread->set_last_Java_frame();
322 // Change the thread state to _thread_in_native
323 ThreadStateTransition::transition_from_java(thread, _thread_in_native);
325 // Make the call
326 intptr_t result[4 - LogBytesPerWord];
327 ffi_call(handler->cif(), (void (*)()) function, result, arguments);
329 // Change the thread state back to _thread_in_Java.
330 // ThreadStateTransition::transition_from_native() cannot be used
331 // here because it does not check for asynchronous exceptions.
332 // We have to manage the transition ourself.
333 thread->set_thread_state(_thread_in_native_trans);
335 // Make sure new state is visible in the GC thread
336 if (os::is_MP()) {
337 if (UseMembar) {
338 OrderAccess::fence();
339 }
340 else {
341 InterfaceSupport::serialize_memory(thread);
342 }
343 }
345 // Handle safepoint operations, pending suspend requests,
346 // and pending asynchronous exceptions.
347 if (SafepointSynchronize::do_call_back() ||
348 thread->has_special_condition_for_native_trans()) {
349 JavaThread::check_special_condition_for_native_trans(thread);
350 CHECK_UNHANDLED_OOPS_ONLY(thread->clear_unhandled_oops());
351 }
353 // Finally we can change the thread state to _thread_in_Java.
354 thread->set_thread_state(_thread_in_Java);
355 fixup_after_potential_safepoint();
357 // Clear the frame anchor
358 thread->reset_last_Java_frame();
360 // If the result was an oop then unbox it and store it in
361 // oop_temp where the garbage collector can see it before
362 // we release the handle it might be protected by.
363 if (handler->result_type() == &ffi_type_pointer) {
364 if (result[0])
365 istate->set_oop_temp(*(oop *) result[0]);
366 else
367 istate->set_oop_temp(NULL);
368 }
370 // Reset handle block
371 thread->active_handles()->clear();
373 unlock_unwind_and_return:
375 // Unlock if necessary
376 if (monitor) {
377 BasicLock *lock = monitor->lock();
378 markOop header = lock->displaced_header();
379 oop rcvr = monitor->obj();
380 monitor->set_obj(NULL);
382 if (header != NULL) {
383 if (Atomic::cmpxchg_ptr(header, rcvr->mark_addr(), lock) != lock) {
384 monitor->set_obj(rcvr); {
385 HandleMark hm(thread);
386 CALL_VM_NOCHECK(InterpreterRuntime::monitorexit(thread, monitor));
387 }
388 }
389 }
390 }
392 unwind_and_return:
394 // Unwind the current activation
395 thread->pop_zero_frame();
397 // Pop our parameters
398 stack->set_sp(stack->sp() + method->size_of_parameters());
400 // Push our result
401 if (!HAS_PENDING_EXCEPTION) {
402 BasicType type = result_type_of(method);
403 stack->set_sp(stack->sp() - type2size[type]);
405 switch (type) {
406 case T_VOID:
407 break;
409 case T_BOOLEAN:
410 #ifndef VM_LITTLE_ENDIAN
411 result[0] <<= (BitsPerWord - BitsPerByte);
412 #endif
413 SET_LOCALS_INT(*(jboolean *) result != 0, 0);
414 break;
416 case T_CHAR:
417 #ifndef VM_LITTLE_ENDIAN
418 result[0] <<= (BitsPerWord - BitsPerShort);
419 #endif
420 SET_LOCALS_INT(*(jchar *) result, 0);
421 break;
423 case T_BYTE:
424 #ifndef VM_LITTLE_ENDIAN
425 result[0] <<= (BitsPerWord - BitsPerByte);
426 #endif
427 SET_LOCALS_INT(*(jbyte *) result, 0);
428 break;
430 case T_SHORT:
431 #ifndef VM_LITTLE_ENDIAN
432 result[0] <<= (BitsPerWord - BitsPerShort);
433 #endif
434 SET_LOCALS_INT(*(jshort *) result, 0);
435 break;
437 case T_INT:
438 #ifndef VM_LITTLE_ENDIAN
439 result[0] <<= (BitsPerWord - BitsPerInt);
440 #endif
441 SET_LOCALS_INT(*(jint *) result, 0);
442 break;
444 case T_LONG:
445 SET_LOCALS_LONG(*(jlong *) result, 0);
446 break;
448 case T_FLOAT:
449 SET_LOCALS_FLOAT(*(jfloat *) result, 0);
450 break;
452 case T_DOUBLE:
453 SET_LOCALS_DOUBLE(*(jdouble *) result, 0);
454 break;
456 case T_OBJECT:
457 case T_ARRAY:
458 SET_LOCALS_OBJECT(istate->oop_temp(), 0);
459 break;
461 default:
462 ShouldNotReachHere();
463 }
464 }
466 // No deoptimized frames on the stack
467 return 0;
468 }
470 int CppInterpreter::accessor_entry(Method* method, intptr_t UNUSED, TRAPS) {
471 JavaThread *thread = (JavaThread *) THREAD;
472 ZeroStack *stack = thread->zero_stack();
473 intptr_t *locals = stack->sp();
475 // Drop into the slow path if we need a safepoint check
476 if (SafepointSynchronize::do_call_back()) {
477 return normal_entry(method, 0, THREAD);
478 }
480 // Load the object pointer and drop into the slow path
481 // if we have a NullPointerException
482 oop object = LOCALS_OBJECT(0);
483 if (object == NULL) {
484 return normal_entry(method, 0, THREAD);
485 }
487 // Read the field index from the bytecode, which looks like this:
488 // 0: aload_0
489 // 1: getfield
490 // 2: index
491 // 3: index
492 // 4: ireturn/areturn
493 // NB this is not raw bytecode: index is in machine order
494 u1 *code = method->code_base();
495 assert(code[0] == Bytecodes::_aload_0 &&
496 code[1] == Bytecodes::_getfield &&
497 (code[4] == Bytecodes::_ireturn ||
498 code[4] == Bytecodes::_areturn), "should do");
499 u2 index = Bytes::get_native_u2(&code[2]);
501 // Get the entry from the constant pool cache, and drop into
502 // the slow path if it has not been resolved
503 ConstantPoolCache* cache = method->constants()->cache();
504 ConstantPoolCacheEntry* entry = cache->entry_at(index);
505 if (!entry->is_resolved(Bytecodes::_getfield)) {
506 return normal_entry(method, 0, THREAD);
507 }
509 // Get the result and push it onto the stack
510 switch (entry->flag_state()) {
511 case ltos:
512 case dtos:
513 stack->overflow_check(1, CHECK_0);
514 stack->alloc(wordSize);
515 break;
516 }
517 if (entry->is_volatile()) {
518 switch (entry->flag_state()) {
519 case ctos:
520 SET_LOCALS_INT(object->char_field_acquire(entry->f2_as_index()), 0);
521 break;
523 case btos:
524 SET_LOCALS_INT(object->byte_field_acquire(entry->f2_as_index()), 0);
525 break;
527 case stos:
528 SET_LOCALS_INT(object->short_field_acquire(entry->f2_as_index()), 0);
529 break;
531 case itos:
532 SET_LOCALS_INT(object->int_field_acquire(entry->f2_as_index()), 0);
533 break;
535 case ltos:
536 SET_LOCALS_LONG(object->long_field_acquire(entry->f2_as_index()), 0);
537 break;
539 case ftos:
540 SET_LOCALS_FLOAT(object->float_field_acquire(entry->f2_as_index()), 0);
541 break;
543 case dtos:
544 SET_LOCALS_DOUBLE(object->double_field_acquire(entry->f2_as_index()), 0);
545 break;
547 case atos:
548 SET_LOCALS_OBJECT(object->obj_field_acquire(entry->f2_as_index()), 0);
549 break;
551 default:
552 ShouldNotReachHere();
553 }
554 }
555 else {
556 switch (entry->flag_state()) {
557 case ctos:
558 SET_LOCALS_INT(object->char_field(entry->f2_as_index()), 0);
559 break;
561 case btos:
562 SET_LOCALS_INT(object->byte_field(entry->f2_as_index()), 0);
563 break;
565 case stos:
566 SET_LOCALS_INT(object->short_field(entry->f2_as_index()), 0);
567 break;
569 case itos:
570 SET_LOCALS_INT(object->int_field(entry->f2_as_index()), 0);
571 break;
573 case ltos:
574 SET_LOCALS_LONG(object->long_field(entry->f2_as_index()), 0);
575 break;
577 case ftos:
578 SET_LOCALS_FLOAT(object->float_field(entry->f2_as_index()), 0);
579 break;
581 case dtos:
582 SET_LOCALS_DOUBLE(object->double_field(entry->f2_as_index()), 0);
583 break;
585 case atos:
586 SET_LOCALS_OBJECT(object->obj_field(entry->f2_as_index()), 0);
587 break;
589 default:
590 ShouldNotReachHere();
591 }
592 }
594 // No deoptimized frames on the stack
595 return 0;
596 }
598 int CppInterpreter::empty_entry(Method* method, intptr_t UNUSED, TRAPS) {
599 JavaThread *thread = (JavaThread *) THREAD;
600 ZeroStack *stack = thread->zero_stack();
602 // Drop into the slow path if we need a safepoint check
603 if (SafepointSynchronize::do_call_back()) {
604 return normal_entry(method, 0, THREAD);
605 }
607 // Pop our parameters
608 stack->set_sp(stack->sp() + method->size_of_parameters());
610 // No deoptimized frames on the stack
611 return 0;
612 }
614 // The new slots will be inserted before slot insert_before.
615 // Slots < insert_before will have the same slot number after the insert.
616 // Slots >= insert_before will become old_slot + num_slots.
617 void CppInterpreter::insert_vmslots(int insert_before, int num_slots, TRAPS) {
618 JavaThread *thread = (JavaThread *) THREAD;
619 ZeroStack *stack = thread->zero_stack();
621 // Allocate the space
622 stack->overflow_check(num_slots, CHECK);
623 stack->alloc(num_slots * wordSize);
624 intptr_t *vmslots = stack->sp();
626 // Shuffle everything up
627 for (int i = 0; i < insert_before; i++)
628 SET_VMSLOTS_SLOT(VMSLOTS_SLOT(i + num_slots), i);
629 }
631 void CppInterpreter::remove_vmslots(int first_slot, int num_slots, TRAPS) {
632 JavaThread *thread = (JavaThread *) THREAD;
633 ZeroStack *stack = thread->zero_stack();
634 intptr_t *vmslots = stack->sp();
636 // Move everything down
637 for (int i = first_slot - 1; i >= 0; i--)
638 SET_VMSLOTS_SLOT(VMSLOTS_SLOT(i), i + num_slots);
640 // Deallocate the space
641 stack->set_sp(stack->sp() + num_slots);
642 }
644 BasicType CppInterpreter::result_type_of_handle(oop method_handle) {
645 oop method_type = java_lang_invoke_MethodHandle::type(method_handle);
646 oop return_type = java_lang_invoke_MethodType::rtype(method_type);
647 return java_lang_Class::as_BasicType(return_type, (Klass* *) NULL);
648 }
650 intptr_t* CppInterpreter::calculate_unwind_sp(ZeroStack* stack,
651 oop method_handle) {
652 oop method_type = java_lang_invoke_MethodHandle::type(method_handle);
653 int argument_slots = java_lang_invoke_MethodType::ptype_slot_count(method_type);
655 return stack->sp() + argument_slots;
656 }
658 IRT_ENTRY(void, CppInterpreter::throw_exception(JavaThread* thread,
659 Symbol* name,
660 char* message))
661 THROW_MSG(name, message);
662 IRT_END
664 InterpreterFrame *InterpreterFrame::build(Method* const method, TRAPS) {
665 JavaThread *thread = (JavaThread *) THREAD;
666 ZeroStack *stack = thread->zero_stack();
668 // Calculate the size of the frame we'll build, including
669 // any adjustments to the caller's frame that we'll make.
670 int extra_locals = 0;
671 int monitor_words = 0;
672 int stack_words = 0;
674 if (!method->is_native()) {
675 extra_locals = method->max_locals() - method->size_of_parameters();
676 stack_words = method->max_stack();
677 }
678 if (method->is_synchronized()) {
679 monitor_words = frame::interpreter_frame_monitor_size();
680 }
681 stack->overflow_check(
682 extra_locals + header_words + monitor_words + stack_words, CHECK_NULL);
684 // Adjust the caller's stack frame to accomodate any additional
685 // local variables we have contiguously with our parameters.
686 for (int i = 0; i < extra_locals; i++)
687 stack->push(0);
689 intptr_t *locals;
690 if (method->is_native())
691 locals = stack->sp() + (method->size_of_parameters() - 1);
692 else
693 locals = stack->sp() + (method->max_locals() - 1);
695 stack->push(0); // next_frame, filled in later
696 intptr_t *fp = stack->sp();
697 assert(fp - stack->sp() == next_frame_off, "should be");
699 stack->push(INTERPRETER_FRAME);
700 assert(fp - stack->sp() == frame_type_off, "should be");
702 interpreterState istate =
703 (interpreterState) stack->alloc(sizeof(BytecodeInterpreter));
704 assert(fp - stack->sp() == istate_off, "should be");
706 istate->set_locals(locals);
707 istate->set_method(method);
708 istate->set_self_link(istate);
709 istate->set_prev_link(NULL);
710 istate->set_thread(thread);
711 istate->set_bcp(method->is_native() ? NULL : method->code_base());
712 istate->set_constants(method->constants()->cache());
713 istate->set_msg(BytecodeInterpreter::method_entry);
714 istate->set_oop_temp(NULL);
715 istate->set_mdx(NULL);
716 istate->set_callee(NULL);
718 istate->set_monitor_base((BasicObjectLock *) stack->sp());
719 if (method->is_synchronized()) {
720 BasicObjectLock *monitor =
721 (BasicObjectLock *) stack->alloc(monitor_words * wordSize);
722 oop object;
723 if (method->is_static())
724 object = method->constants()->pool_holder()->java_mirror();
725 else
726 object = (oop) locals[0];
727 monitor->set_obj(object);
728 }
730 istate->set_stack_base(stack->sp());
731 istate->set_stack(stack->sp() - 1);
732 if (stack_words)
733 stack->alloc(stack_words * wordSize);
734 istate->set_stack_limit(stack->sp() - 1);
736 return (InterpreterFrame *) fp;
737 }
739 int AbstractInterpreter::BasicType_as_index(BasicType type) {
740 int i = 0;
741 switch (type) {
742 case T_BOOLEAN: i = 0; break;
743 case T_CHAR : i = 1; break;
744 case T_BYTE : i = 2; break;
745 case T_SHORT : i = 3; break;
746 case T_INT : i = 4; break;
747 case T_LONG : i = 5; break;
748 case T_VOID : i = 6; break;
749 case T_FLOAT : i = 7; break;
750 case T_DOUBLE : i = 8; break;
751 case T_OBJECT : i = 9; break;
752 case T_ARRAY : i = 9; break;
753 default : ShouldNotReachHere();
754 }
755 assert(0 <= i && i < AbstractInterpreter::number_of_result_handlers,
756 "index out of bounds");
757 return i;
758 }
760 BasicType CppInterpreter::result_type_of(Method* method) {
761 BasicType t;
762 switch (method->result_index()) {
763 case 0 : t = T_BOOLEAN; break;
764 case 1 : t = T_CHAR; break;
765 case 2 : t = T_BYTE; break;
766 case 3 : t = T_SHORT; break;
767 case 4 : t = T_INT; break;
768 case 5 : t = T_LONG; break;
769 case 6 : t = T_VOID; break;
770 case 7 : t = T_FLOAT; break;
771 case 8 : t = T_DOUBLE; break;
772 case 9 : t = T_OBJECT; break;
773 default: ShouldNotReachHere();
774 }
775 assert(AbstractInterpreter::BasicType_as_index(t) == method->result_index(),
776 "out of step with AbstractInterpreter::BasicType_as_index");
777 return t;
778 }
780 address InterpreterGenerator::generate_empty_entry() {
781 if (!UseFastEmptyMethods)
782 return NULL;
784 return generate_entry((address) CppInterpreter::empty_entry);
785 }
787 address InterpreterGenerator::generate_accessor_entry() {
788 if (!UseFastAccessorMethods)
789 return NULL;
791 return generate_entry((address) CppInterpreter::accessor_entry);
792 }
794 address InterpreterGenerator::generate_Reference_get_entry(void) {
795 #if INCLUDE_ALL_GCS
796 if (UseG1GC) {
797 // We need to generate have a routine that generates code to:
798 // * load the value in the referent field
799 // * passes that value to the pre-barrier.
800 //
801 // In the case of G1 this will record the value of the
802 // referent in an SATB buffer if marking is active.
803 // This will cause concurrent marking to mark the referent
804 // field as live.
805 Unimplemented();
806 }
807 #endif // INCLUDE_ALL_GCS
809 // If G1 is not enabled then attempt to go through the accessor entry point
810 // Reference.get is an accessor
811 return generate_accessor_entry();
812 }
814 address InterpreterGenerator::generate_native_entry(bool synchronized) {
815 assert(synchronized == false, "should be");
817 return generate_entry((address) CppInterpreter::native_entry);
818 }
820 address InterpreterGenerator::generate_normal_entry(bool synchronized) {
821 assert(synchronized == false, "should be");
823 return generate_entry((address) CppInterpreter::normal_entry);
824 }
826 address AbstractInterpreterGenerator::generate_method_entry(
827 AbstractInterpreter::MethodKind kind) {
828 address entry_point = NULL;
830 switch (kind) {
831 case Interpreter::zerolocals:
832 case Interpreter::zerolocals_synchronized:
833 break;
835 case Interpreter::native:
836 entry_point = ((InterpreterGenerator*) this)->generate_native_entry(false);
837 break;
839 case Interpreter::native_synchronized:
840 entry_point = ((InterpreterGenerator*) this)->generate_native_entry(false);
841 break;
843 case Interpreter::empty:
844 entry_point = ((InterpreterGenerator*) this)->generate_empty_entry();
845 break;
847 case Interpreter::accessor:
848 entry_point = ((InterpreterGenerator*) this)->generate_accessor_entry();
849 break;
851 case Interpreter::abstract:
852 entry_point = ((InterpreterGenerator*) this)->generate_abstract_entry();
853 break;
855 case Interpreter::java_lang_math_sin:
856 case Interpreter::java_lang_math_cos:
857 case Interpreter::java_lang_math_tan:
858 case Interpreter::java_lang_math_abs:
859 case Interpreter::java_lang_math_log:
860 case Interpreter::java_lang_math_log10:
861 case Interpreter::java_lang_math_sqrt:
862 case Interpreter::java_lang_math_pow:
863 case Interpreter::java_lang_math_exp:
864 entry_point = ((InterpreterGenerator*) this)->generate_math_entry(kind);
865 break;
867 case Interpreter::java_lang_ref_reference_get:
868 entry_point = ((InterpreterGenerator*)this)->generate_Reference_get_entry();
869 break;
871 default:
872 ShouldNotReachHere();
873 }
875 if (entry_point == NULL)
876 entry_point = ((InterpreterGenerator*) this)->generate_normal_entry(false);
878 return entry_point;
879 }
881 InterpreterGenerator::InterpreterGenerator(StubQueue* code)
882 : CppInterpreterGenerator(code) {
883 generate_all();
884 }
886 // Deoptimization helpers
888 InterpreterFrame *InterpreterFrame::build(int size, TRAPS) {
889 ZeroStack *stack = ((JavaThread *) THREAD)->zero_stack();
891 int size_in_words = size >> LogBytesPerWord;
892 assert(size_in_words * wordSize == size, "unaligned");
893 assert(size_in_words >= header_words, "too small");
894 stack->overflow_check(size_in_words, CHECK_NULL);
896 stack->push(0); // next_frame, filled in later
897 intptr_t *fp = stack->sp();
898 assert(fp - stack->sp() == next_frame_off, "should be");
900 stack->push(INTERPRETER_FRAME);
901 assert(fp - stack->sp() == frame_type_off, "should be");
903 interpreterState istate =
904 (interpreterState) stack->alloc(sizeof(BytecodeInterpreter));
905 assert(fp - stack->sp() == istate_off, "should be");
906 istate->set_self_link(NULL); // mark invalid
908 stack->alloc((size_in_words - header_words) * wordSize);
910 return (InterpreterFrame *) fp;
911 }
913 int AbstractInterpreter::layout_activation(Method* method,
914 int tempcount,
915 int popframe_extra_args,
916 int moncount,
917 int caller_actual_parameters,
918 int callee_param_count,
919 int callee_locals,
920 frame* caller,
921 frame* interpreter_frame,
922 bool is_top_frame,
923 bool is_bottom_frame) {
924 assert(popframe_extra_args == 0, "what to do?");
925 assert(!is_top_frame || (!callee_locals && !callee_param_count),
926 "top frame should have no caller");
928 // This code must exactly match what InterpreterFrame::build
929 // does (the full InterpreterFrame::build, that is, not the
930 // one that creates empty frames for the deoptimizer).
931 //
932 // If interpreter_frame is not NULL then it will be filled in.
933 // It's size is determined by a previous call to this method,
934 // so it should be correct.
935 //
936 // Note that tempcount is the current size of the expression
937 // stack. For top most frames we will allocate a full sized
938 // expression stack and not the trimmed version that non-top
939 // frames have.
941 int header_words = InterpreterFrame::header_words;
942 int monitor_words = moncount * frame::interpreter_frame_monitor_size();
943 int stack_words = is_top_frame ? method->max_stack() : tempcount;
944 int callee_extra_locals = callee_locals - callee_param_count;
946 if (interpreter_frame) {
947 intptr_t *locals = interpreter_frame->fp() + method->max_locals();
948 interpreterState istate = interpreter_frame->get_interpreterState();
949 intptr_t *monitor_base = (intptr_t*) istate;
950 intptr_t *stack_base = monitor_base - monitor_words;
951 intptr_t *stack = stack_base - tempcount - 1;
953 BytecodeInterpreter::layout_interpreterState(istate,
954 caller,
955 NULL,
956 method,
957 locals,
958 stack,
959 stack_base,
960 monitor_base,
961 NULL,
962 is_top_frame);
963 }
964 return header_words + monitor_words + stack_words + callee_extra_locals;
965 }
967 void BytecodeInterpreter::layout_interpreterState(interpreterState istate,
968 frame* caller,
969 frame* current,
970 Method* method,
971 intptr_t* locals,
972 intptr_t* stack,
973 intptr_t* stack_base,
974 intptr_t* monitor_base,
975 intptr_t* frame_bottom,
976 bool is_top_frame) {
977 istate->set_locals(locals);
978 istate->set_method(method);
979 istate->set_self_link(istate);
980 istate->set_prev_link(NULL);
981 // thread will be set by a hacky repurposing of frame::patch_pc()
982 // bcp will be set by vframeArrayElement::unpack_on_stack()
983 istate->set_constants(method->constants()->cache());
984 istate->set_msg(BytecodeInterpreter::method_resume);
985 istate->set_bcp_advance(0);
986 istate->set_oop_temp(NULL);
987 istate->set_mdx(NULL);
988 if (caller->is_interpreted_frame()) {
989 interpreterState prev = caller->get_interpreterState();
990 prev->set_callee(method);
991 if (*prev->bcp() == Bytecodes::_invokeinterface)
992 prev->set_bcp_advance(5);
993 else
994 prev->set_bcp_advance(3);
995 }
996 istate->set_callee(NULL);
997 istate->set_monitor_base((BasicObjectLock *) monitor_base);
998 istate->set_stack_base(stack_base);
999 istate->set_stack(stack);
1000 istate->set_stack_limit(stack_base - method->max_stack() - 1);
1001 }
1003 address CppInterpreter::return_entry(TosState state, int length) {
1004 ShouldNotCallThis();
1005 }
1007 address CppInterpreter::deopt_entry(TosState state, int length) {
1008 return NULL;
1009 }
1011 // Helper for (runtime) stack overflow checks
1013 int AbstractInterpreter::size_top_interpreter_activation(Method* method) {
1014 return 0;
1015 }
1017 // Helper for figuring out if frames are interpreter frames
1019 bool CppInterpreter::contains(address pc) {
1020 return false; // make frame::print_value_on work
1021 }
1023 // Result handlers and convertors
1025 address CppInterpreterGenerator::generate_result_handler_for(
1026 BasicType type) {
1027 assembler()->advance(1);
1028 return ShouldNotCallThisStub();
1029 }
1031 address CppInterpreterGenerator::generate_tosca_to_stack_converter(
1032 BasicType type) {
1033 assembler()->advance(1);
1034 return ShouldNotCallThisStub();
1035 }
1037 address CppInterpreterGenerator::generate_stack_to_stack_converter(
1038 BasicType type) {
1039 assembler()->advance(1);
1040 return ShouldNotCallThisStub();
1041 }
1043 address CppInterpreterGenerator::generate_stack_to_native_abi_converter(
1044 BasicType type) {
1045 assembler()->advance(1);
1046 return ShouldNotCallThisStub();
1047 }
1049 #endif // CC_INTERP