1 /*
2  * Copyright (c) 2003, 2019, 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  */
25 
26 #include "precompiled.hpp"
27 #include "asm/assembler.hpp"
28 #include "interpreter/bytecodeHistogram.hpp"
29 #include "interpreter/cppInterpreter.hpp"
30 #include "interpreter/cppInterpreterGenerator.hpp"
31 #include "interpreter/interpreter.hpp"
32 #include "interpreter/interpreterRuntime.hpp"
33 #include "oops/arrayOop.hpp"
34 #include "oops/cpCache.inline.hpp"
35 #include "oops/methodData.hpp"
36 #include "oops/method.hpp"
37 #include "oops/oop.inline.hpp"
38 #include "prims/jvmtiExport.hpp"
39 #include "prims/jvmtiThreadState.hpp"
40 #include "runtime/arguments.hpp"
41 #include "runtime/deoptimization.hpp"
42 #include "runtime/frame.inline.hpp"
43 #include "runtime/handles.inline.hpp"
44 #include "runtime/interfaceSupport.inline.hpp"
45 #include "runtime/jniHandles.inline.hpp"
46 #include "runtime/sharedRuntime.hpp"
47 #include "runtime/stubRoutines.hpp"
48 #include "runtime/synchronizer.hpp"
49 #include "runtime/timer.hpp"
50 #include "runtime/vframeArray.hpp"
51 #include "stack_zero.inline.hpp"
52 #include "utilities/debug.hpp"
53 #include "utilities/macros.hpp"
54 
55 #ifdef CC_INTERP
56 
57 #define fixup_after_potential_safepoint()       \
58   method = istate->method()
59 
60 #define CALL_VM_NOCHECK_NOFIX(func)             \
61   thread->set_last_Java_frame();                \
62   func;                                         \
63   thread->reset_last_Java_frame();
64 
65 #define CALL_VM_NOCHECK(func)                   \
66   CALL_VM_NOCHECK_NOFIX(func)                   \
67   fixup_after_potential_safepoint()
68 
normal_entry(Method * method,intptr_t UNUSED,TRAPS)69 int CppInterpreter::normal_entry(Method* method, intptr_t UNUSED, TRAPS) {
70   JavaThread *thread = (JavaThread *) THREAD;
71 
72   // Allocate and initialize our frame.
73   InterpreterFrame *frame = InterpreterFrame::build(method, CHECK_0);
74   thread->push_zero_frame(frame);
75 
76   // Execute those bytecodes!
77   main_loop(0, THREAD);
78 
79   // No deoptimized frames on the stack
80   return 0;
81 }
82 
narrow(BasicType type,intptr_t result)83 intptr_t narrow(BasicType type, intptr_t result) {
84   // mask integer result to narrower return type.
85   switch (type) {
86     case T_BOOLEAN:
87       return result&1;
88     case T_BYTE:
89       return (intptr_t)(jbyte)result;
90     case T_CHAR:
91       return (intptr_t)(uintptr_t)(jchar)result;
92     case T_SHORT:
93       return (intptr_t)(jshort)result;
94     case T_OBJECT:  // nothing to do fall through
95     case T_ARRAY:
96     case T_LONG:
97     case T_INT:
98     case T_FLOAT:
99     case T_DOUBLE:
100     case T_VOID:
101       return result;
102     default:
103       ShouldNotReachHere();
104       return result; // silence compiler warnings
105   }
106 }
107 
108 
main_loop(int recurse,TRAPS)109 void CppInterpreter::main_loop(int recurse, TRAPS) {
110   JavaThread *thread = (JavaThread *) THREAD;
111   ZeroStack *stack = thread->zero_stack();
112 
113   // If we are entering from a deopt we may need to call
114   // ourself a few times in order to get to our frame.
115   if (recurse)
116     main_loop(recurse - 1, THREAD);
117 
118   InterpreterFrame *frame = thread->top_zero_frame()->as_interpreter_frame();
119   interpreterState istate = frame->interpreter_state();
120   Method* method = istate->method();
121 
122   intptr_t *result = NULL;
123   int result_slots = 0;
124 
125   while (true) {
126     // We can set up the frame anchor with everything we want at
127     // this point as we are thread_in_Java and no safepoints can
128     // occur until we go to vm mode.  We do have to clear flags
129     // on return from vm but that is it.
130     thread->set_last_Java_frame();
131 
132     // Call the interpreter
133     if (JvmtiExport::can_post_interpreter_events())
134       BytecodeInterpreter::runWithChecks(istate);
135     else
136       BytecodeInterpreter::run(istate);
137     fixup_after_potential_safepoint();
138 
139     // Clear the frame anchor
140     thread->reset_last_Java_frame();
141 
142     // Examine the message from the interpreter to decide what to do
143     if (istate->msg() == BytecodeInterpreter::call_method) {
144       Method* callee = istate->callee();
145 
146       // Trim back the stack to put the parameters at the top
147       stack->set_sp(istate->stack() + 1);
148 
149       // Make the call
150       Interpreter::invoke_method(callee, istate->callee_entry_point(), THREAD);
151       fixup_after_potential_safepoint();
152 
153       // Convert the result
154       istate->set_stack(stack->sp() - 1);
155 
156       // Restore the stack
157       stack->set_sp(istate->stack_limit() + 1);
158 
159       // Resume the interpreter
160       istate->set_msg(BytecodeInterpreter::method_resume);
161     }
162     else if (istate->msg() == BytecodeInterpreter::more_monitors) {
163       int monitor_words = frame::interpreter_frame_monitor_size();
164 
165       // Allocate the space
166       stack->overflow_check(monitor_words, THREAD);
167       if (HAS_PENDING_EXCEPTION)
168         break;
169       stack->alloc(monitor_words * wordSize);
170 
171       // Move the expression stack contents
172       for (intptr_t *p = istate->stack() + 1; p < istate->stack_base(); p++)
173         *(p - monitor_words) = *p;
174 
175       // Move the expression stack pointers
176       istate->set_stack_limit(istate->stack_limit() - monitor_words);
177       istate->set_stack(istate->stack() - monitor_words);
178       istate->set_stack_base(istate->stack_base() - monitor_words);
179 
180       // Zero the new monitor so the interpreter can find it.
181       ((BasicObjectLock *) istate->stack_base())->set_obj(NULL);
182 
183       // Resume the interpreter
184       istate->set_msg(BytecodeInterpreter::got_monitors);
185     }
186     else if (istate->msg() == BytecodeInterpreter::return_from_method) {
187       // Copy the result into the caller's frame
188       result_slots = type2size[method->result_type()];
189       assert(result_slots >= 0 && result_slots <= 2, "what?");
190       result = istate->stack() + result_slots;
191       break;
192     }
193     else if (istate->msg() == BytecodeInterpreter::throwing_exception) {
194       assert(HAS_PENDING_EXCEPTION, "should do");
195       break;
196     }
197     else if (istate->msg() == BytecodeInterpreter::do_osr) {
198       // Unwind the current frame
199       thread->pop_zero_frame();
200 
201       // Remove any extension of the previous frame
202       int extra_locals = method->max_locals() - method->size_of_parameters();
203       stack->set_sp(stack->sp() + extra_locals);
204 
205       // Jump into the OSR method
206       Interpreter::invoke_osr(
207         method, istate->osr_entry(), istate->osr_buf(), THREAD);
208       return;
209     }
210     else {
211       ShouldNotReachHere();
212     }
213   }
214 
215   // Unwind the current frame
216   thread->pop_zero_frame();
217 
218   // Pop our local variables
219   stack->set_sp(stack->sp() + method->max_locals());
220 
221   // Push our result
222   for (int i = 0; i < result_slots; i++) {
223     // Adjust result to smaller
224     union {
225       intptr_t res;
226       jint res_jint;
227     };
228     res = result[-i];
229     if (result_slots == 1) {
230       BasicType t = method->result_type();
231       if (is_subword_type(t)) {
232         res_jint = (jint)narrow(t, res_jint);
233       }
234     }
235     stack->push(res);
236   }
237 }
238 
native_entry(Method * method,intptr_t UNUSED,TRAPS)239 int CppInterpreter::native_entry(Method* method, intptr_t UNUSED, TRAPS) {
240   // Make sure method is native and not abstract
241   assert(method->is_native() && !method->is_abstract(), "should be");
242 
243   JavaThread *thread = (JavaThread *) THREAD;
244   ZeroStack *stack = thread->zero_stack();
245 
246   // Allocate and initialize our frame
247   InterpreterFrame *frame = InterpreterFrame::build(method, CHECK_0);
248   thread->push_zero_frame(frame);
249   interpreterState istate = frame->interpreter_state();
250   intptr_t *locals = istate->locals();
251 
252   // Update the invocation counter
253   if ((UseCompiler || CountCompiledCalls) && !method->is_synchronized()) {
254     MethodCounters* mcs = method->method_counters();
255     if (mcs == NULL) {
256       CALL_VM_NOCHECK(mcs = InterpreterRuntime::build_method_counters(thread, method));
257       if (HAS_PENDING_EXCEPTION)
258         goto unwind_and_return;
259     }
260     InvocationCounter *counter = mcs->invocation_counter();
261     counter->increment();
262     if (counter->reached_InvocationLimit(mcs->backedge_counter())) {
263       CALL_VM_NOCHECK(
264         InterpreterRuntime::frequency_counter_overflow(thread, NULL));
265       if (HAS_PENDING_EXCEPTION)
266         goto unwind_and_return;
267     }
268   }
269 
270   // Lock if necessary
271   BasicObjectLock *monitor;
272   monitor = NULL;
273   if (method->is_synchronized()) {
274     monitor = (BasicObjectLock*) istate->stack_base();
275     oop lockee = monitor->obj();
276     markWord disp = lockee->mark().set_unlocked();
277 
278     monitor->lock()->set_displaced_header(disp);
279     if (lockee->cas_set_mark(markWord::from_pointer(monitor), disp) != disp) {
280       if (thread->is_lock_owned((address) disp.clear_lock_bits().to_pointer())) {
281         monitor->lock()->set_displaced_header(markWord::from_pointer(NULL));
282       }
283       else {
284         CALL_VM_NOCHECK(InterpreterRuntime::monitorenter(thread, monitor));
285         if (HAS_PENDING_EXCEPTION)
286           goto unwind_and_return;
287       }
288     }
289   }
290 
291   // Get the signature handler
292   InterpreterRuntime::SignatureHandler *handler; {
293     address handlerAddr = method->signature_handler();
294     if (handlerAddr == NULL) {
295       CALL_VM_NOCHECK(InterpreterRuntime::prepare_native_call(thread, method));
296       if (HAS_PENDING_EXCEPTION)
297         goto unlock_unwind_and_return;
298 
299       handlerAddr = method->signature_handler();
300       assert(handlerAddr != NULL, "eh?");
301     }
302     if (handlerAddr == (address) InterpreterRuntime::slow_signature_handler) {
303       CALL_VM_NOCHECK(handlerAddr =
304         InterpreterRuntime::slow_signature_handler(thread, method, NULL,NULL));
305       if (HAS_PENDING_EXCEPTION)
306         goto unlock_unwind_and_return;
307     }
308     handler = \
309       InterpreterRuntime::SignatureHandler::from_handlerAddr(handlerAddr);
310   }
311 
312   // Get the native function entry point
313   address function;
314   function = method->native_function();
315   assert(function != NULL, "should be set if signature handler is");
316 
317   // Build the argument list
318   stack->overflow_check(handler->argument_count() * 2, THREAD);
319   if (HAS_PENDING_EXCEPTION)
320     goto unlock_unwind_and_return;
321 
322   void **arguments;
323   void *mirror; {
324     arguments =
325       (void **) stack->alloc(handler->argument_count() * sizeof(void **));
326     void **dst = arguments;
327 
328     void *env = thread->jni_environment();
329     *(dst++) = &env;
330 
331     if (method->is_static()) {
332       istate->set_oop_temp(
333         method->constants()->pool_holder()->java_mirror());
334       mirror = istate->oop_temp_addr();
335       *(dst++) = &mirror;
336     }
337 
338     intptr_t *src = locals;
339     for (int i = dst - arguments; i < handler->argument_count(); i++) {
340       ffi_type *type = handler->argument_type(i);
341       if (type == &ffi_type_pointer) {
342         if (*src) {
343           stack->push((intptr_t) src);
344           *(dst++) = stack->sp();
345         }
346         else {
347           *(dst++) = src;
348         }
349         src--;
350       }
351       else if (type->size == 4) {
352         *(dst++) = src--;
353       }
354       else if (type->size == 8) {
355         src--;
356         *(dst++) = src--;
357       }
358       else {
359         ShouldNotReachHere();
360       }
361     }
362   }
363 
364   // Set up the Java frame anchor
365   thread->set_last_Java_frame();
366 
367   // Change the thread state to _thread_in_native
368   ThreadStateTransition::transition_from_java(thread, _thread_in_native);
369 
370   // Make the call
371   intptr_t result[4 - LogBytesPerWord];
372   ffi_call(handler->cif(), (void (*)()) function, result, arguments);
373 
374   // Change the thread state back to _thread_in_Java and ensure it
375   // is seen by the GC thread.
376   // ThreadStateTransition::transition_from_native() cannot be used
377   // here because it does not check for asynchronous exceptions.
378   // We have to manage the transition ourself.
379   thread->set_thread_state_fence(_thread_in_native_trans);
380 
381   // Handle safepoint operations, pending suspend requests,
382   // and pending asynchronous exceptions.
383   if (SafepointMechanism::should_block(thread) ||
384       thread->has_special_condition_for_native_trans()) {
385     JavaThread::check_special_condition_for_native_trans(thread);
386     CHECK_UNHANDLED_OOPS_ONLY(thread->clear_unhandled_oops());
387   }
388 
389   // Finally we can change the thread state to _thread_in_Java.
390   thread->set_thread_state(_thread_in_Java);
391   fixup_after_potential_safepoint();
392 
393   // Clear the frame anchor
394   thread->reset_last_Java_frame();
395 
396   // If the result was an oop then unbox it and store it in
397   // oop_temp where the garbage collector can see it before
398   // we release the handle it might be protected by.
399   if (handler->result_type() == &ffi_type_pointer) {
400     if (result[0] == 0) {
401       istate->set_oop_temp(NULL);
402     } else {
403       jobject handle = reinterpret_cast<jobject>(result[0]);
404       istate->set_oop_temp(JNIHandles::resolve(handle));
405     }
406   }
407 
408   // Reset handle block
409   thread->active_handles()->clear();
410 
411  unlock_unwind_and_return:
412 
413   // Unlock if necessary
414   if (monitor) {
415     BasicLock *lock = monitor->lock();
416     markWord header = lock->displaced_header();
417     oop rcvr = monitor->obj();
418     monitor->set_obj(NULL);
419 
420     if (header.to_pointer() != NULL) {
421       markWord old_header = markWord::encode(lock);
422       if (rcvr->cas_set_mark(header, old_header) != old_header) {
423         monitor->set_obj(rcvr); {
424           HandleMark hm(thread);
425           CALL_VM_NOCHECK(InterpreterRuntime::monitorexit(thread, monitor));
426         }
427       }
428     }
429   }
430 
431  unwind_and_return:
432 
433   // Unwind the current activation
434   thread->pop_zero_frame();
435 
436   // Pop our parameters
437   stack->set_sp(stack->sp() + method->size_of_parameters());
438 
439   // Push our result
440   if (!HAS_PENDING_EXCEPTION) {
441     BasicType type = method->result_type();
442     stack->set_sp(stack->sp() - type2size[type]);
443 
444     switch (type) {
445     case T_VOID:
446       break;
447 
448     case T_BOOLEAN:
449 #ifndef VM_LITTLE_ENDIAN
450       result[0] <<= (BitsPerWord - BitsPerByte);
451 #endif
452       SET_LOCALS_INT(*(jboolean *) result != 0, 0);
453       break;
454 
455     case T_CHAR:
456 #ifndef VM_LITTLE_ENDIAN
457       result[0] <<= (BitsPerWord - BitsPerShort);
458 #endif
459       SET_LOCALS_INT(*(jchar *) result, 0);
460       break;
461 
462     case T_BYTE:
463 #ifndef VM_LITTLE_ENDIAN
464       result[0] <<= (BitsPerWord - BitsPerByte);
465 #endif
466       SET_LOCALS_INT(*(jbyte *) result, 0);
467       break;
468 
469     case T_SHORT:
470 #ifndef VM_LITTLE_ENDIAN
471       result[0] <<= (BitsPerWord - BitsPerShort);
472 #endif
473       SET_LOCALS_INT(*(jshort *) result, 0);
474       break;
475 
476     case T_INT:
477 #ifndef VM_LITTLE_ENDIAN
478       result[0] <<= (BitsPerWord - BitsPerInt);
479 #endif
480       SET_LOCALS_INT(*(jint *) result, 0);
481       break;
482 
483     case T_LONG:
484       SET_LOCALS_LONG(*(jlong *) result, 0);
485       break;
486 
487     case T_FLOAT:
488       SET_LOCALS_FLOAT(*(jfloat *) result, 0);
489       break;
490 
491     case T_DOUBLE:
492       SET_LOCALS_DOUBLE(*(jdouble *) result, 0);
493       break;
494 
495     case T_OBJECT:
496     case T_ARRAY:
497       SET_LOCALS_OBJECT(istate->oop_temp(), 0);
498       break;
499 
500     default:
501       ShouldNotReachHere();
502     }
503   }
504 
505   // No deoptimized frames on the stack
506   return 0;
507 }
508 
accessor_entry(Method * method,intptr_t UNUSED,TRAPS)509 int CppInterpreter::accessor_entry(Method* method, intptr_t UNUSED, TRAPS) {
510   JavaThread *thread = (JavaThread *) THREAD;
511   ZeroStack *stack = thread->zero_stack();
512   intptr_t *locals = stack->sp();
513 
514   // Drop into the slow path if we need a safepoint check
515   if (SafepointMechanism::should_block(THREAD)) {
516     return normal_entry(method, 0, THREAD);
517   }
518 
519   // Load the object pointer and drop into the slow path
520   // if we have a NullPointerException
521   oop object = LOCALS_OBJECT(0);
522   if (object == NULL) {
523     return normal_entry(method, 0, THREAD);
524   }
525 
526   // Read the field index from the bytecode, which looks like this:
527   //  0:  aload_0
528   //  1:  getfield
529   //  2:    index
530   //  3:    index
531   //  4:  ireturn/areturn/freturn/lreturn/dreturn
532   // NB this is not raw bytecode: index is in machine order
533   u1 *code = method->code_base();
534   assert(code[0] == Bytecodes::_aload_0 &&
535          code[1] == Bytecodes::_getfield &&
536          (code[4] == Bytecodes::_ireturn ||
537           code[4] == Bytecodes::_freturn ||
538           code[4] == Bytecodes::_lreturn ||
539           code[4] == Bytecodes::_dreturn ||
540           code[4] == Bytecodes::_areturn), "should do");
541   u2 index = Bytes::get_native_u2(&code[2]);
542 
543   // Get the entry from the constant pool cache, and drop into
544   // the slow path if it has not been resolved
545   ConstantPoolCache* cache = method->constants()->cache();
546   ConstantPoolCacheEntry* entry = cache->entry_at(index);
547   if (!entry->is_resolved(Bytecodes::_getfield)) {
548     return normal_entry(method, 0, THREAD);
549   }
550 
551   // Get the result and push it onto the stack
552   switch (entry->flag_state()) {
553   case ltos:
554   case dtos:
555     stack->overflow_check(1, CHECK_0);
556     stack->alloc(wordSize);
557     break;
558   }
559   if (entry->is_volatile()) {
560     if (support_IRIW_for_not_multiple_copy_atomic_cpu) {
561       OrderAccess::fence();
562     }
563     switch (entry->flag_state()) {
564     case ctos:
565       SET_LOCALS_INT(object->char_field_acquire(entry->f2_as_index()), 0);
566       break;
567 
568     case btos:
569     case ztos:
570       SET_LOCALS_INT(object->byte_field_acquire(entry->f2_as_index()), 0);
571       break;
572 
573     case stos:
574       SET_LOCALS_INT(object->short_field_acquire(entry->f2_as_index()), 0);
575       break;
576 
577     case itos:
578       SET_LOCALS_INT(object->int_field_acquire(entry->f2_as_index()), 0);
579       break;
580 
581     case ltos:
582       SET_LOCALS_LONG(object->long_field_acquire(entry->f2_as_index()), 0);
583       break;
584 
585     case ftos:
586       SET_LOCALS_FLOAT(object->float_field_acquire(entry->f2_as_index()), 0);
587       break;
588 
589     case dtos:
590       SET_LOCALS_DOUBLE(object->double_field_acquire(entry->f2_as_index()), 0);
591       break;
592 
593     case atos:
594       SET_LOCALS_OBJECT(object->obj_field_acquire(entry->f2_as_index()), 0);
595       break;
596 
597     default:
598       ShouldNotReachHere();
599     }
600   }
601   else {
602     switch (entry->flag_state()) {
603     case ctos:
604       SET_LOCALS_INT(object->char_field(entry->f2_as_index()), 0);
605       break;
606 
607     case btos:
608     case ztos:
609       SET_LOCALS_INT(object->byte_field(entry->f2_as_index()), 0);
610       break;
611 
612     case stos:
613       SET_LOCALS_INT(object->short_field(entry->f2_as_index()), 0);
614       break;
615 
616     case itos:
617       SET_LOCALS_INT(object->int_field(entry->f2_as_index()), 0);
618       break;
619 
620     case ltos:
621       SET_LOCALS_LONG(object->long_field(entry->f2_as_index()), 0);
622       break;
623 
624     case ftos:
625       SET_LOCALS_FLOAT(object->float_field(entry->f2_as_index()), 0);
626       break;
627 
628     case dtos:
629       SET_LOCALS_DOUBLE(object->double_field(entry->f2_as_index()), 0);
630       break;
631 
632     case atos:
633       SET_LOCALS_OBJECT(object->obj_field(entry->f2_as_index()), 0);
634       break;
635 
636     default:
637       ShouldNotReachHere();
638     }
639   }
640 
641   // No deoptimized frames on the stack
642   return 0;
643 }
644 
empty_entry(Method * method,intptr_t UNUSED,TRAPS)645 int CppInterpreter::empty_entry(Method* method, intptr_t UNUSED, TRAPS) {
646   JavaThread *thread = (JavaThread *) THREAD;
647   ZeroStack *stack = thread->zero_stack();
648 
649   // Drop into the slow path if we need a safepoint check
650   if (SafepointMechanism::should_block(THREAD)) {
651     return normal_entry(method, 0, THREAD);
652   }
653 
654   // Pop our parameters
655   stack->set_sp(stack->sp() + method->size_of_parameters());
656 
657   // No deoptimized frames on the stack
658   return 0;
659 }
660 
661 // The new slots will be inserted before slot insert_before.
662 // Slots < insert_before will have the same slot number after the insert.
663 // Slots >= insert_before will become old_slot + num_slots.
insert_vmslots(int insert_before,int num_slots,TRAPS)664 void CppInterpreter::insert_vmslots(int insert_before, int num_slots, TRAPS) {
665   JavaThread *thread = (JavaThread *) THREAD;
666   ZeroStack *stack = thread->zero_stack();
667 
668   // Allocate the space
669   stack->overflow_check(num_slots, CHECK);
670   stack->alloc(num_slots * wordSize);
671   intptr_t *vmslots = stack->sp();
672 
673   // Shuffle everything up
674   for (int i = 0; i < insert_before; i++)
675     SET_VMSLOTS_SLOT(VMSLOTS_SLOT(i + num_slots), i);
676 }
677 
remove_vmslots(int first_slot,int num_slots,TRAPS)678 void CppInterpreter::remove_vmslots(int first_slot, int num_slots, TRAPS) {
679   JavaThread *thread = (JavaThread *) THREAD;
680   ZeroStack *stack = thread->zero_stack();
681   intptr_t *vmslots = stack->sp();
682 
683   // Move everything down
684   for (int i = first_slot - 1; i >= 0; i--)
685     SET_VMSLOTS_SLOT(VMSLOTS_SLOT(i), i + num_slots);
686 
687   // Deallocate the space
688   stack->set_sp(stack->sp() + num_slots);
689 }
690 
result_type_of_handle(oop method_handle)691 BasicType CppInterpreter::result_type_of_handle(oop method_handle) {
692   oop method_type = java_lang_invoke_MethodHandle::type(method_handle);
693   oop return_type = java_lang_invoke_MethodType::rtype(method_type);
694   return java_lang_Class::as_BasicType(return_type, (Klass* *) NULL);
695 }
696 
calculate_unwind_sp(ZeroStack * stack,oop method_handle)697 intptr_t* CppInterpreter::calculate_unwind_sp(ZeroStack* stack,
698                                               oop method_handle) {
699   oop method_type = java_lang_invoke_MethodHandle::type(method_handle);
700   int argument_slots = java_lang_invoke_MethodType::ptype_slot_count(method_type);
701 
702   return stack->sp() + argument_slots;
703 }
704 
705 JRT_ENTRY(void, CppInterpreter::throw_exception(JavaThread* thread,
706                                                 Symbol*     name,
707                                                 char*       message))
708   THROW_MSG(name, message);
709 JRT_END
710 
build(Method * const method,TRAPS)711 InterpreterFrame *InterpreterFrame::build(Method* const method, TRAPS) {
712   JavaThread *thread = (JavaThread *) THREAD;
713   ZeroStack *stack = thread->zero_stack();
714 
715   // Calculate the size of the frame we'll build, including
716   // any adjustments to the caller's frame that we'll make.
717   int extra_locals  = 0;
718   int monitor_words = 0;
719   int stack_words   = 0;
720 
721   if (!method->is_native()) {
722     extra_locals = method->max_locals() - method->size_of_parameters();
723     stack_words  = method->max_stack();
724   }
725   if (method->is_synchronized()) {
726     monitor_words = frame::interpreter_frame_monitor_size();
727   }
728   stack->overflow_check(
729     extra_locals + header_words + monitor_words + stack_words, CHECK_NULL);
730 
731   // Adjust the caller's stack frame to accomodate any additional
732   // local variables we have contiguously with our parameters.
733   for (int i = 0; i < extra_locals; i++)
734     stack->push(0);
735 
736   intptr_t *locals;
737   if (method->is_native())
738     locals = stack->sp() + (method->size_of_parameters() - 1);
739   else
740     locals = stack->sp() + (method->max_locals() - 1);
741 
742   stack->push(0); // next_frame, filled in later
743   intptr_t *fp = stack->sp();
744   assert(fp - stack->sp() == next_frame_off, "should be");
745 
746   stack->push(INTERPRETER_FRAME);
747   assert(fp - stack->sp() == frame_type_off, "should be");
748 
749   interpreterState istate =
750     (interpreterState) stack->alloc(sizeof(BytecodeInterpreter));
751   assert(fp - stack->sp() == istate_off, "should be");
752 
753   istate->set_locals(locals);
754   istate->set_method(method);
755   istate->set_mirror(method->method_holder()->java_mirror());
756   istate->set_self_link(istate);
757   istate->set_prev_link(NULL);
758   istate->set_thread(thread);
759   istate->set_bcp(method->is_native() ? NULL : method->code_base());
760   istate->set_constants(method->constants()->cache());
761   istate->set_msg(BytecodeInterpreter::method_entry);
762   istate->set_oop_temp(NULL);
763   istate->set_mdx(NULL);
764   istate->set_callee(NULL);
765 
766   istate->set_monitor_base((BasicObjectLock *) stack->sp());
767   if (method->is_synchronized()) {
768     BasicObjectLock *monitor =
769       (BasicObjectLock *) stack->alloc(monitor_words * wordSize);
770     oop object;
771     if (method->is_static())
772       object = method->constants()->pool_holder()->java_mirror();
773     else
774       object = (oop) (void*)locals[0];
775     monitor->set_obj(object);
776   }
777 
778   istate->set_stack_base(stack->sp());
779   istate->set_stack(stack->sp() - 1);
780   if (stack_words)
781     stack->alloc(stack_words * wordSize);
782   istate->set_stack_limit(stack->sp() - 1);
783 
784   return (InterpreterFrame *) fp;
785 }
786 
build(int size,TRAPS)787 InterpreterFrame *InterpreterFrame::build(int size, TRAPS) {
788   ZeroStack *stack = ((JavaThread *) THREAD)->zero_stack();
789 
790   int size_in_words = size >> LogBytesPerWord;
791   assert(size_in_words * wordSize == size, "unaligned");
792   assert(size_in_words >= header_words, "too small");
793   stack->overflow_check(size_in_words, CHECK_NULL);
794 
795   stack->push(0); // next_frame, filled in later
796   intptr_t *fp = stack->sp();
797   assert(fp - stack->sp() == next_frame_off, "should be");
798 
799   stack->push(INTERPRETER_FRAME);
800   assert(fp - stack->sp() == frame_type_off, "should be");
801 
802   interpreterState istate =
803     (interpreterState) stack->alloc(sizeof(BytecodeInterpreter));
804   assert(fp - stack->sp() == istate_off, "should be");
805   istate->set_self_link(NULL); // mark invalid
806 
807   stack->alloc((size_in_words - header_words) * wordSize);
808 
809   return (InterpreterFrame *) fp;
810 }
811 
return_entry(TosState state,int length,Bytecodes::Code code)812 address CppInterpreter::return_entry(TosState state, int length, Bytecodes::Code code) {
813   ShouldNotCallThis();
814   return NULL;
815 }
816 
deopt_entry(TosState state,int length)817 address CppInterpreter::deopt_entry(TosState state, int length) {
818   return NULL;
819 }
820 
821 // Helper for figuring out if frames are interpreter frames
822 
contains(address pc)823 bool CppInterpreter::contains(address pc) {
824   return false; // make frame::print_value_on work
825 }
826 #endif // CC_INTERP
827