Thu, 15 Apr 2010 02:40:12 -0700
6941224: Improved stack overflow handling for Zero
Summary: Adding stack overflow checking to Shark brought to light a bunch of deficiencies in Zero's stack overflow code.
Reviewed-by: twisti
Contributed-by: Gary Benson <gbenson@redhat.com>
1 /*
2 * Copyright 2003-2007 Sun Microsystems, Inc. All Rights Reserved.
3 * Copyright 2007, 2008, 2009, 2010 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
22 * have any questions.
23 *
24 */
26 #include "incls/_precompiled.incl"
27 #include "incls/_cppInterpreter_zero.cpp.incl"
29 #ifdef CC_INTERP
31 #define fixup_after_potential_safepoint() \
32 method = istate->method()
34 #define CALL_VM_NOCHECK(func) \
35 thread->set_last_Java_frame(); \
36 func; \
37 thread->reset_last_Java_frame(); \
38 fixup_after_potential_safepoint()
40 void CppInterpreter::normal_entry(methodOop method, intptr_t UNUSED, TRAPS) {
41 JavaThread *thread = (JavaThread *) THREAD;
43 // Allocate and initialize our frame.
44 InterpreterFrame *frame = InterpreterFrame::build(method, CHECK);
45 thread->push_zero_frame(frame);
47 // Execute those bytecodes!
48 main_loop(0, THREAD);
49 }
51 void CppInterpreter::main_loop(int recurse, TRAPS) {
52 JavaThread *thread = (JavaThread *) THREAD;
53 ZeroStack *stack = thread->zero_stack();
55 // If we are entering from a deopt we may need to call
56 // ourself a few times in order to get to our frame.
57 if (recurse)
58 main_loop(recurse - 1, THREAD);
60 InterpreterFrame *frame = thread->top_zero_frame()->as_interpreter_frame();
61 interpreterState istate = frame->interpreter_state();
62 methodOop method = istate->method();
64 intptr_t *result = NULL;
65 int result_slots = 0;
67 while (true) {
68 // We can set up the frame anchor with everything we want at
69 // this point as we are thread_in_Java and no safepoints can
70 // occur until we go to vm mode. We do have to clear flags
71 // on return from vm but that is it.
72 thread->set_last_Java_frame();
74 // Call the interpreter
75 if (JvmtiExport::can_post_interpreter_events())
76 BytecodeInterpreter::runWithChecks(istate);
77 else
78 BytecodeInterpreter::run(istate);
79 fixup_after_potential_safepoint();
81 // Clear the frame anchor
82 thread->reset_last_Java_frame();
84 // Examine the message from the interpreter to decide what to do
85 if (istate->msg() == BytecodeInterpreter::call_method) {
86 methodOop callee = istate->callee();
88 // Trim back the stack to put the parameters at the top
89 stack->set_sp(istate->stack() + 1);
91 // Make the call
92 Interpreter::invoke_method(callee, istate->callee_entry_point(), THREAD);
93 fixup_after_potential_safepoint();
95 // Convert the result
96 istate->set_stack(stack->sp() - 1);
98 // Restore the stack
99 stack->set_sp(istate->stack_limit() + 1);
101 // Resume the interpreter
102 istate->set_msg(BytecodeInterpreter::method_resume);
103 }
104 else if (istate->msg() == BytecodeInterpreter::more_monitors) {
105 int monitor_words = frame::interpreter_frame_monitor_size();
107 // Allocate the space
108 stack->overflow_check(monitor_words, THREAD);
109 if (HAS_PENDING_EXCEPTION)
110 break;
111 stack->alloc(monitor_words * wordSize);
113 // Move the expression stack contents
114 for (intptr_t *p = istate->stack() + 1; p < istate->stack_base(); p++)
115 *(p - monitor_words) = *p;
117 // Move the expression stack pointers
118 istate->set_stack_limit(istate->stack_limit() - monitor_words);
119 istate->set_stack(istate->stack() - monitor_words);
120 istate->set_stack_base(istate->stack_base() - monitor_words);
122 // Zero the new monitor so the interpreter can find it.
123 ((BasicObjectLock *) istate->stack_base())->set_obj(NULL);
125 // Resume the interpreter
126 istate->set_msg(BytecodeInterpreter::got_monitors);
127 }
128 else if (istate->msg() == BytecodeInterpreter::return_from_method) {
129 // Copy the result into the caller's frame
130 result_slots = type2size[result_type_of(method)];
131 assert(result_slots >= 0 && result_slots <= 2, "what?");
132 result = istate->stack() + result_slots;
133 break;
134 }
135 else if (istate->msg() == BytecodeInterpreter::throwing_exception) {
136 assert(HAS_PENDING_EXCEPTION, "should do");
137 break;
138 }
139 else if (istate->msg() == BytecodeInterpreter::do_osr) {
140 // Unwind the current frame
141 thread->pop_zero_frame();
143 // Remove any extension of the previous frame
144 int extra_locals = method->max_locals() - method->size_of_parameters();
145 stack->set_sp(stack->sp() + extra_locals);
147 // Jump into the OSR method
148 Interpreter::invoke_osr(
149 method, istate->osr_entry(), istate->osr_buf(), THREAD);
150 return;
151 }
152 else {
153 ShouldNotReachHere();
154 }
155 }
157 // Unwind the current frame
158 thread->pop_zero_frame();
160 // Pop our local variables
161 stack->set_sp(stack->sp() + method->max_locals());
163 // Push our result
164 for (int i = 0; i < result_slots; i++)
165 stack->push(result[-i]);
166 }
168 void CppInterpreter::native_entry(methodOop method, intptr_t UNUSED, TRAPS) {
169 // Make sure method is native and not abstract
170 assert(method->is_native() && !method->is_abstract(), "should be");
172 JavaThread *thread = (JavaThread *) THREAD;
173 ZeroStack *stack = thread->zero_stack();
175 // Allocate and initialize our frame
176 InterpreterFrame *frame = InterpreterFrame::build(method, CHECK);
177 thread->push_zero_frame(frame);
178 interpreterState istate = frame->interpreter_state();
179 intptr_t *locals = istate->locals();
181 // Update the invocation counter
182 if ((UseCompiler || CountCompiledCalls) && !method->is_synchronized()) {
183 InvocationCounter *counter = method->invocation_counter();
184 counter->increment();
185 if (counter->reached_InvocationLimit()) {
186 CALL_VM_NOCHECK(
187 InterpreterRuntime::frequency_counter_overflow(thread, NULL));
188 if (HAS_PENDING_EXCEPTION)
189 goto unwind_and_return;
190 }
191 }
193 // Lock if necessary
194 BasicObjectLock *monitor;
195 monitor = NULL;
196 if (method->is_synchronized()) {
197 monitor = (BasicObjectLock*) istate->stack_base();
198 oop lockee = monitor->obj();
199 markOop disp = lockee->mark()->set_unlocked();
201 monitor->lock()->set_displaced_header(disp);
202 if (Atomic::cmpxchg_ptr(monitor, lockee->mark_addr(), disp) != disp) {
203 if (thread->is_lock_owned((address) disp->clear_lock_bits())) {
204 monitor->lock()->set_displaced_header(NULL);
205 }
206 else {
207 CALL_VM_NOCHECK(InterpreterRuntime::monitorenter(thread, monitor));
208 if (HAS_PENDING_EXCEPTION)
209 goto unwind_and_return;
210 }
211 }
212 }
214 // Get the signature handler
215 InterpreterRuntime::SignatureHandler *handler; {
216 address handlerAddr = method->signature_handler();
217 if (handlerAddr == NULL) {
218 CALL_VM_NOCHECK(InterpreterRuntime::prepare_native_call(thread, method));
219 if (HAS_PENDING_EXCEPTION)
220 goto unlock_unwind_and_return;
222 handlerAddr = method->signature_handler();
223 assert(handlerAddr != NULL, "eh?");
224 }
225 if (handlerAddr == (address) InterpreterRuntime::slow_signature_handler) {
226 CALL_VM_NOCHECK(handlerAddr =
227 InterpreterRuntime::slow_signature_handler(thread, method, NULL,NULL));
228 if (HAS_PENDING_EXCEPTION)
229 goto unlock_unwind_and_return;
230 }
231 handler = \
232 InterpreterRuntime::SignatureHandler::from_handlerAddr(handlerAddr);
233 }
235 // Get the native function entry point
236 address function;
237 function = method->native_function();
238 assert(function != NULL, "should be set if signature handler is");
240 // Build the argument list
241 stack->overflow_check(handler->argument_count() * 2, THREAD);
242 if (HAS_PENDING_EXCEPTION)
243 goto unlock_unwind_and_return;
245 void **arguments;
246 void *mirror; {
247 arguments =
248 (void **) stack->alloc(handler->argument_count() * sizeof(void **));
249 void **dst = arguments;
251 void *env = thread->jni_environment();
252 *(dst++) = &env;
254 if (method->is_static()) {
255 istate->set_oop_temp(
256 method->constants()->pool_holder()->klass_part()->java_mirror());
257 mirror = istate->oop_temp_addr();
258 *(dst++) = &mirror;
259 }
261 intptr_t *src = locals;
262 for (int i = dst - arguments; i < handler->argument_count(); i++) {
263 ffi_type *type = handler->argument_type(i);
264 if (type == &ffi_type_pointer) {
265 if (*src) {
266 stack->push((intptr_t) src);
267 *(dst++) = stack->sp();
268 }
269 else {
270 *(dst++) = src;
271 }
272 src--;
273 }
274 else if (type->size == 4) {
275 *(dst++) = src--;
276 }
277 else if (type->size == 8) {
278 src--;
279 *(dst++) = src--;
280 }
281 else {
282 ShouldNotReachHere();
283 }
284 }
285 }
287 // Set up the Java frame anchor
288 thread->set_last_Java_frame();
290 // Change the thread state to _thread_in_native
291 ThreadStateTransition::transition_from_java(thread, _thread_in_native);
293 // Make the call
294 intptr_t result[4 - LogBytesPerWord];
295 ffi_call(handler->cif(), (void (*)()) function, result, arguments);
297 // Change the thread state back to _thread_in_Java.
298 // ThreadStateTransition::transition_from_native() cannot be used
299 // here because it does not check for asynchronous exceptions.
300 // We have to manage the transition ourself.
301 thread->set_thread_state(_thread_in_native_trans);
303 // Make sure new state is visible in the GC thread
304 if (os::is_MP()) {
305 if (UseMembar) {
306 OrderAccess::fence();
307 }
308 else {
309 InterfaceSupport::serialize_memory(thread);
310 }
311 }
313 // Handle safepoint operations, pending suspend requests,
314 // and pending asynchronous exceptions.
315 if (SafepointSynchronize::do_call_back() ||
316 thread->has_special_condition_for_native_trans()) {
317 JavaThread::check_special_condition_for_native_trans(thread);
318 CHECK_UNHANDLED_OOPS_ONLY(thread->clear_unhandled_oops());
319 }
321 // Finally we can change the thread state to _thread_in_Java.
322 thread->set_thread_state(_thread_in_Java);
323 fixup_after_potential_safepoint();
325 // Clear the frame anchor
326 thread->reset_last_Java_frame();
328 // If the result was an oop then unbox it and store it in
329 // oop_temp where the garbage collector can see it before
330 // we release the handle it might be protected by.
331 if (handler->result_type() == &ffi_type_pointer) {
332 if (result[0])
333 istate->set_oop_temp(*(oop *) result[0]);
334 else
335 istate->set_oop_temp(NULL);
336 }
338 // Reset handle block
339 thread->active_handles()->clear();
341 unlock_unwind_and_return:
343 // Unlock if necessary
344 if (monitor) {
345 BasicLock *lock = monitor->lock();
346 markOop header = lock->displaced_header();
347 oop rcvr = monitor->obj();
348 monitor->set_obj(NULL);
350 if (header != NULL) {
351 if (Atomic::cmpxchg_ptr(header, rcvr->mark_addr(), lock) != lock) {
352 monitor->set_obj(rcvr); {
353 HandleMark hm(thread);
354 CALL_VM_NOCHECK(InterpreterRuntime::monitorexit(thread, monitor));
355 }
356 }
357 }
358 }
360 unwind_and_return:
362 // Unwind the current activation
363 thread->pop_zero_frame();
365 // Pop our parameters
366 stack->set_sp(stack->sp() + method->size_of_parameters());
368 // Push our result
369 if (!HAS_PENDING_EXCEPTION) {
370 BasicType type = result_type_of(method);
371 stack->set_sp(stack->sp() - type2size[type]);
373 switch (type) {
374 case T_VOID:
375 break;
377 case T_BOOLEAN:
378 #ifndef VM_LITTLE_ENDIAN
379 result[0] <<= (BitsPerWord - BitsPerByte);
380 #endif
381 SET_LOCALS_INT(*(jboolean *) result != 0, 0);
382 break;
384 case T_CHAR:
385 #ifndef VM_LITTLE_ENDIAN
386 result[0] <<= (BitsPerWord - BitsPerShort);
387 #endif
388 SET_LOCALS_INT(*(jchar *) result, 0);
389 break;
391 case T_BYTE:
392 #ifndef VM_LITTLE_ENDIAN
393 result[0] <<= (BitsPerWord - BitsPerByte);
394 #endif
395 SET_LOCALS_INT(*(jbyte *) result, 0);
396 break;
398 case T_SHORT:
399 #ifndef VM_LITTLE_ENDIAN
400 result[0] <<= (BitsPerWord - BitsPerShort);
401 #endif
402 SET_LOCALS_INT(*(jshort *) result, 0);
403 break;
405 case T_INT:
406 #ifndef VM_LITTLE_ENDIAN
407 result[0] <<= (BitsPerWord - BitsPerInt);
408 #endif
409 SET_LOCALS_INT(*(jint *) result, 0);
410 break;
412 case T_LONG:
413 SET_LOCALS_LONG(*(jlong *) result, 0);
414 break;
416 case T_FLOAT:
417 SET_LOCALS_FLOAT(*(jfloat *) result, 0);
418 break;
420 case T_DOUBLE:
421 SET_LOCALS_DOUBLE(*(jdouble *) result, 0);
422 break;
424 case T_OBJECT:
425 case T_ARRAY:
426 SET_LOCALS_OBJECT(istate->oop_temp(), 0);
427 break;
429 default:
430 ShouldNotReachHere();
431 }
432 }
433 }
435 void CppInterpreter::accessor_entry(methodOop method, intptr_t UNUSED, TRAPS) {
436 JavaThread *thread = (JavaThread *) THREAD;
437 ZeroStack *stack = thread->zero_stack();
438 intptr_t *locals = stack->sp();
440 // Drop into the slow path if we need a safepoint check
441 if (SafepointSynchronize::do_call_back()) {
442 normal_entry(method, 0, THREAD);
443 return;
444 }
446 // Load the object pointer and drop into the slow path
447 // if we have a NullPointerException
448 oop object = LOCALS_OBJECT(0);
449 if (object == NULL) {
450 normal_entry(method, 0, THREAD);
451 return;
452 }
454 // Read the field index from the bytecode, which looks like this:
455 // 0: aload_0
456 // 1: getfield
457 // 2: index
458 // 3: index
459 // 4: ireturn/areturn
460 // NB this is not raw bytecode: index is in machine order
461 u1 *code = method->code_base();
462 assert(code[0] == Bytecodes::_aload_0 &&
463 code[1] == Bytecodes::_getfield &&
464 (code[4] == Bytecodes::_ireturn ||
465 code[4] == Bytecodes::_areturn), "should do");
466 u2 index = Bytes::get_native_u2(&code[2]);
468 // Get the entry from the constant pool cache, and drop into
469 // the slow path if it has not been resolved
470 constantPoolCacheOop cache = method->constants()->cache();
471 ConstantPoolCacheEntry* entry = cache->entry_at(index);
472 if (!entry->is_resolved(Bytecodes::_getfield)) {
473 normal_entry(method, 0, THREAD);
474 return;
475 }
477 // Get the result and push it onto the stack
478 switch (entry->flag_state()) {
479 case ltos:
480 case dtos:
481 stack->overflow_check(1, CHECK);
482 stack->alloc(wordSize);
483 break;
484 }
485 if (entry->is_volatile()) {
486 switch (entry->flag_state()) {
487 case ctos:
488 SET_LOCALS_INT(object->char_field_acquire(entry->f2()), 0);
489 break;
491 case btos:
492 SET_LOCALS_INT(object->byte_field_acquire(entry->f2()), 0);
493 break;
495 case stos:
496 SET_LOCALS_INT(object->short_field_acquire(entry->f2()), 0);
497 break;
499 case itos:
500 SET_LOCALS_INT(object->int_field_acquire(entry->f2()), 0);
501 break;
503 case ltos:
504 SET_LOCALS_LONG(object->long_field_acquire(entry->f2()), 0);
505 break;
507 case ftos:
508 SET_LOCALS_FLOAT(object->float_field_acquire(entry->f2()), 0);
509 break;
511 case dtos:
512 SET_LOCALS_DOUBLE(object->double_field_acquire(entry->f2()), 0);
513 break;
515 case atos:
516 SET_LOCALS_OBJECT(object->obj_field_acquire(entry->f2()), 0);
517 break;
519 default:
520 ShouldNotReachHere();
521 }
522 }
523 else {
524 switch (entry->flag_state()) {
525 case ctos:
526 SET_LOCALS_INT(object->char_field(entry->f2()), 0);
527 break;
529 case btos:
530 SET_LOCALS_INT(object->byte_field(entry->f2()), 0);
531 break;
533 case stos:
534 SET_LOCALS_INT(object->short_field(entry->f2()), 0);
535 break;
537 case itos:
538 SET_LOCALS_INT(object->int_field(entry->f2()), 0);
539 break;
541 case ltos:
542 SET_LOCALS_LONG(object->long_field(entry->f2()), 0);
543 break;
545 case ftos:
546 SET_LOCALS_FLOAT(object->float_field(entry->f2()), 0);
547 break;
549 case dtos:
550 SET_LOCALS_DOUBLE(object->double_field(entry->f2()), 0);
551 break;
553 case atos:
554 SET_LOCALS_OBJECT(object->obj_field(entry->f2()), 0);
555 break;
557 default:
558 ShouldNotReachHere();
559 }
560 }
561 }
563 void CppInterpreter::empty_entry(methodOop method, intptr_t UNUSED, TRAPS) {
564 JavaThread *thread = (JavaThread *) THREAD;
565 ZeroStack *stack = thread->zero_stack();
567 // Drop into the slow path if we need a safepoint check
568 if (SafepointSynchronize::do_call_back()) {
569 normal_entry(method, 0, THREAD);
570 return;
571 }
573 // Pop our parameters
574 stack->set_sp(stack->sp() + method->size_of_parameters());
575 }
577 InterpreterFrame *InterpreterFrame::build(const methodOop method, TRAPS) {
578 JavaThread *thread = (JavaThread *) THREAD;
579 ZeroStack *stack = thread->zero_stack();
581 // Calculate the size of the frame we'll build, including
582 // any adjustments to the caller's frame that we'll make.
583 int extra_locals = 0;
584 int monitor_words = 0;
585 int stack_words = 0;
587 if (!method->is_native()) {
588 extra_locals = method->max_locals() - method->size_of_parameters();
589 stack_words = method->max_stack();
590 }
591 if (method->is_synchronized()) {
592 monitor_words = frame::interpreter_frame_monitor_size();
593 }
594 stack->overflow_check(
595 extra_locals + header_words + monitor_words + stack_words, CHECK_NULL);
597 // Adjust the caller's stack frame to accomodate any additional
598 // local variables we have contiguously with our parameters.
599 for (int i = 0; i < extra_locals; i++)
600 stack->push(0);
602 intptr_t *locals;
603 if (method->is_native())
604 locals = stack->sp() + (method->size_of_parameters() - 1);
605 else
606 locals = stack->sp() + (method->max_locals() - 1);
608 stack->push(0); // next_frame, filled in later
609 intptr_t *fp = stack->sp();
610 assert(fp - stack->sp() == next_frame_off, "should be");
612 stack->push(INTERPRETER_FRAME);
613 assert(fp - stack->sp() == frame_type_off, "should be");
615 interpreterState istate =
616 (interpreterState) stack->alloc(sizeof(BytecodeInterpreter));
617 assert(fp - stack->sp() == istate_off, "should be");
619 istate->set_locals(locals);
620 istate->set_method(method);
621 istate->set_self_link(istate);
622 istate->set_prev_link(NULL);
623 istate->set_thread(thread);
624 istate->set_bcp(method->is_native() ? NULL : method->code_base());
625 istate->set_constants(method->constants()->cache());
626 istate->set_msg(BytecodeInterpreter::method_entry);
627 istate->set_oop_temp(NULL);
628 istate->set_mdx(NULL);
629 istate->set_callee(NULL);
631 istate->set_monitor_base((BasicObjectLock *) stack->sp());
632 if (method->is_synchronized()) {
633 BasicObjectLock *monitor =
634 (BasicObjectLock *) stack->alloc(monitor_words * wordSize);
635 oop object;
636 if (method->is_static())
637 object = method->constants()->pool_holder()->klass_part()->java_mirror();
638 else
639 object = (oop) locals[0];
640 monitor->set_obj(object);
641 }
643 istate->set_stack_base(stack->sp());
644 istate->set_stack(stack->sp() - 1);
645 if (stack_words)
646 stack->alloc(stack_words * wordSize);
647 istate->set_stack_limit(stack->sp() - 1);
649 return (InterpreterFrame *) fp;
650 }
652 int AbstractInterpreter::BasicType_as_index(BasicType type) {
653 int i = 0;
654 switch (type) {
655 case T_BOOLEAN: i = 0; break;
656 case T_CHAR : i = 1; break;
657 case T_BYTE : i = 2; break;
658 case T_SHORT : i = 3; break;
659 case T_INT : i = 4; break;
660 case T_LONG : i = 5; break;
661 case T_VOID : i = 6; break;
662 case T_FLOAT : i = 7; break;
663 case T_DOUBLE : i = 8; break;
664 case T_OBJECT : i = 9; break;
665 case T_ARRAY : i = 9; break;
666 default : ShouldNotReachHere();
667 }
668 assert(0 <= i && i < AbstractInterpreter::number_of_result_handlers,
669 "index out of bounds");
670 return i;
671 }
673 BasicType CppInterpreter::result_type_of(methodOop method) {
674 BasicType t;
675 switch (method->result_index()) {
676 case 0 : t = T_BOOLEAN; break;
677 case 1 : t = T_CHAR; break;
678 case 2 : t = T_BYTE; break;
679 case 3 : t = T_SHORT; break;
680 case 4 : t = T_INT; break;
681 case 5 : t = T_LONG; break;
682 case 6 : t = T_VOID; break;
683 case 7 : t = T_FLOAT; break;
684 case 8 : t = T_DOUBLE; break;
685 case 9 : t = T_OBJECT; break;
686 default: ShouldNotReachHere();
687 }
688 assert(AbstractInterpreter::BasicType_as_index(t) == method->result_index(),
689 "out of step with AbstractInterpreter::BasicType_as_index");
690 return t;
691 }
693 address InterpreterGenerator::generate_empty_entry() {
694 if (!UseFastEmptyMethods)
695 return NULL;
697 return generate_entry((address) CppInterpreter::empty_entry);
698 }
700 address InterpreterGenerator::generate_accessor_entry() {
701 if (!UseFastAccessorMethods)
702 return NULL;
704 return generate_entry((address) CppInterpreter::accessor_entry);
705 }
707 address InterpreterGenerator::generate_native_entry(bool synchronized) {
708 assert(synchronized == false, "should be");
710 return generate_entry((address) CppInterpreter::native_entry);
711 }
713 address InterpreterGenerator::generate_normal_entry(bool synchronized) {
714 assert(synchronized == false, "should be");
716 return generate_entry((address) CppInterpreter::normal_entry);
717 }
719 address AbstractInterpreterGenerator::generate_method_entry(
720 AbstractInterpreter::MethodKind kind) {
721 address entry_point = NULL;
723 switch (kind) {
724 case Interpreter::zerolocals:
725 case Interpreter::zerolocals_synchronized:
726 break;
728 case Interpreter::native:
729 entry_point = ((InterpreterGenerator*) this)->generate_native_entry(false);
730 break;
732 case Interpreter::native_synchronized:
733 entry_point = ((InterpreterGenerator*) this)->generate_native_entry(false);
734 break;
736 case Interpreter::empty:
737 entry_point = ((InterpreterGenerator*) this)->generate_empty_entry();
738 break;
740 case Interpreter::accessor:
741 entry_point = ((InterpreterGenerator*) this)->generate_accessor_entry();
742 break;
744 case Interpreter::abstract:
745 entry_point = ((InterpreterGenerator*) this)->generate_abstract_entry();
746 break;
748 case Interpreter::method_handle:
749 entry_point = ((InterpreterGenerator*) this)->generate_method_handle_entry();
750 break;
752 case Interpreter::java_lang_math_sin:
753 case Interpreter::java_lang_math_cos:
754 case Interpreter::java_lang_math_tan:
755 case Interpreter::java_lang_math_abs:
756 case Interpreter::java_lang_math_log:
757 case Interpreter::java_lang_math_log10:
758 case Interpreter::java_lang_math_sqrt:
759 entry_point = ((InterpreterGenerator*) this)->generate_math_entry(kind);
760 break;
762 default:
763 ShouldNotReachHere();
764 }
766 if (entry_point == NULL)
767 entry_point = ((InterpreterGenerator*) this)->generate_normal_entry(false);
769 return entry_point;
770 }
772 InterpreterGenerator::InterpreterGenerator(StubQueue* code)
773 : CppInterpreterGenerator(code) {
774 generate_all();
775 }
777 // Deoptimization helpers
779 InterpreterFrame *InterpreterFrame::build(int size, TRAPS) {
780 ZeroStack *stack = ((JavaThread *) THREAD)->zero_stack();
782 int size_in_words = size >> LogBytesPerWord;
783 assert(size_in_words * wordSize == size, "unaligned");
784 assert(size_in_words >= header_words, "too small");
785 stack->overflow_check(size_in_words, CHECK_NULL);
787 stack->push(0); // next_frame, filled in later
788 intptr_t *fp = stack->sp();
789 assert(fp - stack->sp() == next_frame_off, "should be");
791 stack->push(INTERPRETER_FRAME);
792 assert(fp - stack->sp() == frame_type_off, "should be");
794 interpreterState istate =
795 (interpreterState) stack->alloc(sizeof(BytecodeInterpreter));
796 assert(fp - stack->sp() == istate_off, "should be");
797 istate->set_self_link(NULL); // mark invalid
799 stack->alloc((size_in_words - header_words) * wordSize);
801 return (InterpreterFrame *) fp;
802 }
804 int AbstractInterpreter::layout_activation(methodOop method,
805 int tempcount,
806 int popframe_extra_args,
807 int moncount,
808 int callee_param_count,
809 int callee_locals,
810 frame* caller,
811 frame* interpreter_frame,
812 bool is_top_frame) {
813 assert(popframe_extra_args == 0, "what to do?");
814 assert(!is_top_frame || (!callee_locals && !callee_param_count),
815 "top frame should have no caller")
817 // This code must exactly match what InterpreterFrame::build
818 // does (the full InterpreterFrame::build, that is, not the
819 // one that creates empty frames for the deoptimizer).
820 //
821 // If interpreter_frame is not NULL then it will be filled in.
822 // It's size is determined by a previous call to this method,
823 // so it should be correct.
824 //
825 // Note that tempcount is the current size of the expression
826 // stack. For top most frames we will allocate a full sized
827 // expression stack and not the trimmed version that non-top
828 // frames have.
830 int header_words = InterpreterFrame::header_words;
831 int monitor_words = moncount * frame::interpreter_frame_monitor_size();
832 int stack_words = is_top_frame ? method->max_stack() : tempcount;
833 int callee_extra_locals = callee_locals - callee_param_count;
835 if (interpreter_frame) {
836 intptr_t *locals = interpreter_frame->sp() + method->max_locals();
837 interpreterState istate = interpreter_frame->get_interpreterState();
838 intptr_t *monitor_base = (intptr_t*) istate;
839 intptr_t *stack_base = monitor_base - monitor_words;
840 intptr_t *stack = stack_base - tempcount - 1;
842 BytecodeInterpreter::layout_interpreterState(istate,
843 caller,
844 NULL,
845 method,
846 locals,
847 stack,
848 stack_base,
849 monitor_base,
850 NULL,
851 is_top_frame);
852 }
853 return header_words + monitor_words + stack_words + callee_extra_locals;
854 }
856 void BytecodeInterpreter::layout_interpreterState(interpreterState istate,
857 frame* caller,
858 frame* current,
859 methodOop method,
860 intptr_t* locals,
861 intptr_t* stack,
862 intptr_t* stack_base,
863 intptr_t* monitor_base,
864 intptr_t* frame_bottom,
865 bool is_top_frame) {
866 istate->set_locals(locals);
867 istate->set_method(method);
868 istate->set_self_link(istate);
869 istate->set_prev_link(NULL);
870 // thread will be set by a hacky repurposing of frame::patch_pc()
871 // bcp will be set by vframeArrayElement::unpack_on_stack()
872 istate->set_constants(method->constants()->cache());
873 istate->set_msg(BytecodeInterpreter::method_resume);
874 istate->set_bcp_advance(0);
875 istate->set_oop_temp(NULL);
876 istate->set_mdx(NULL);
877 if (caller->is_interpreted_frame()) {
878 interpreterState prev = caller->get_interpreterState();
879 prev->set_callee(method);
880 if (*prev->bcp() == Bytecodes::_invokeinterface)
881 prev->set_bcp_advance(5);
882 else
883 prev->set_bcp_advance(3);
884 }
885 istate->set_callee(NULL);
886 istate->set_monitor_base((BasicObjectLock *) monitor_base);
887 istate->set_stack_base(stack_base);
888 istate->set_stack(stack);
889 istate->set_stack_limit(stack_base - method->max_stack() - 1);
890 }
892 address CppInterpreter::return_entry(TosState state, int length) {
893 ShouldNotCallThis();
894 }
896 address CppInterpreter::deopt_entry(TosState state, int length) {
897 return NULL;
898 }
900 // Helper for (runtime) stack overflow checks
902 int AbstractInterpreter::size_top_interpreter_activation(methodOop method) {
903 return 0;
904 }
906 // Helper for figuring out if frames are interpreter frames
908 bool CppInterpreter::contains(address pc) {
909 #ifdef PRODUCT
910 ShouldNotCallThis();
911 #else
912 return false; // make frame::print_value_on work
913 #endif // !PRODUCT
914 }
916 // Result handlers and convertors
918 address CppInterpreterGenerator::generate_result_handler_for(
919 BasicType type) {
920 assembler()->advance(1);
921 return ShouldNotCallThisStub();
922 }
924 address CppInterpreterGenerator::generate_tosca_to_stack_converter(
925 BasicType type) {
926 assembler()->advance(1);
927 return ShouldNotCallThisStub();
928 }
930 address CppInterpreterGenerator::generate_stack_to_stack_converter(
931 BasicType type) {
932 assembler()->advance(1);
933 return ShouldNotCallThisStub();
934 }
936 address CppInterpreterGenerator::generate_stack_to_native_abi_converter(
937 BasicType type) {
938 assembler()->advance(1);
939 return ShouldNotCallThisStub();
940 }
942 #endif // CC_INTERP