cppInterpreter_zero.cpp revision 3602:da91efe96a93
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 */
25
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#ifdef SHARK
51#include "shark/shark_globals.hpp"
52#endif
53
54#ifdef CC_INTERP
55
56#define fixup_after_potential_safepoint()       \
57  method = istate->method()
58
59#define CALL_VM_NOCHECK_NOFIX(func)             \
60  thread->set_last_Java_frame();                \
61  func;                                         \
62  thread->reset_last_Java_frame();
63
64#define CALL_VM_NOCHECK(func)                   \
65  CALL_VM_NOCHECK_NOFIX(func)                   \
66  fixup_after_potential_safepoint()
67
68int CppInterpreter::normal_entry(Method* method, intptr_t UNUSED, TRAPS) {
69  JavaThread *thread = (JavaThread *) THREAD;
70
71  // Allocate and initialize our frame.
72  InterpreterFrame *frame = InterpreterFrame::build(method, CHECK_0);
73  thread->push_zero_frame(frame);
74
75  // Execute those bytecodes!
76  main_loop(0, THREAD);
77
78  // No deoptimized frames on the stack
79  return 0;
80}
81
82void CppInterpreter::main_loop(int recurse, TRAPS) {
83  JavaThread *thread = (JavaThread *) THREAD;
84  ZeroStack *stack = thread->zero_stack();
85
86  // If we are entering from a deopt we may need to call
87  // ourself a few times in order to get to our frame.
88  if (recurse)
89    main_loop(recurse - 1, THREAD);
90
91  InterpreterFrame *frame = thread->top_zero_frame()->as_interpreter_frame();
92  interpreterState istate = frame->interpreter_state();
93  Method* method = istate->method();
94
95  intptr_t *result = NULL;
96  int result_slots = 0;
97
98  while (true) {
99    // We can set up the frame anchor with everything we want at
100    // this point as we are thread_in_Java and no safepoints can
101    // occur until we go to vm mode.  We do have to clear flags
102    // on return from vm but that is it.
103    thread->set_last_Java_frame();
104
105    // Call the interpreter
106    if (JvmtiExport::can_post_interpreter_events())
107      BytecodeInterpreter::runWithChecks(istate);
108    else
109      BytecodeInterpreter::run(istate);
110    fixup_after_potential_safepoint();
111
112    // Clear the frame anchor
113    thread->reset_last_Java_frame();
114
115    // Examine the message from the interpreter to decide what to do
116    if (istate->msg() == BytecodeInterpreter::call_method) {
117      Method* callee = istate->callee();
118
119      // Trim back the stack to put the parameters at the top
120      stack->set_sp(istate->stack() + 1);
121
122      // Make the call
123      Interpreter::invoke_method(callee, istate->callee_entry_point(), THREAD);
124      fixup_after_potential_safepoint();
125
126      // Convert the result
127      istate->set_stack(stack->sp() - 1);
128
129      // Restore the stack
130      stack->set_sp(istate->stack_limit() + 1);
131
132      // Resume the interpreter
133      istate->set_msg(BytecodeInterpreter::method_resume);
134    }
135    else if (istate->msg() == BytecodeInterpreter::more_monitors) {
136      int monitor_words = frame::interpreter_frame_monitor_size();
137
138      // Allocate the space
139      stack->overflow_check(monitor_words, THREAD);
140      if (HAS_PENDING_EXCEPTION)
141        break;
142      stack->alloc(monitor_words * wordSize);
143
144      // Move the expression stack contents
145      for (intptr_t *p = istate->stack() + 1; p < istate->stack_base(); p++)
146        *(p - monitor_words) = *p;
147
148      // Move the expression stack pointers
149      istate->set_stack_limit(istate->stack_limit() - monitor_words);
150      istate->set_stack(istate->stack() - monitor_words);
151      istate->set_stack_base(istate->stack_base() - monitor_words);
152
153      // Zero the new monitor so the interpreter can find it.
154      ((BasicObjectLock *) istate->stack_base())->set_obj(NULL);
155
156      // Resume the interpreter
157      istate->set_msg(BytecodeInterpreter::got_monitors);
158    }
159    else if (istate->msg() == BytecodeInterpreter::return_from_method) {
160      // Copy the result into the caller's frame
161      result_slots = type2size[result_type_of(method)];
162      assert(result_slots >= 0 && result_slots <= 2, "what?");
163      result = istate->stack() + result_slots;
164      break;
165    }
166    else if (istate->msg() == BytecodeInterpreter::throwing_exception) {
167      assert(HAS_PENDING_EXCEPTION, "should do");
168      break;
169    }
170    else if (istate->msg() == BytecodeInterpreter::do_osr) {
171      // Unwind the current frame
172      thread->pop_zero_frame();
173
174      // Remove any extension of the previous frame
175      int extra_locals = method->max_locals() - method->size_of_parameters();
176      stack->set_sp(stack->sp() + extra_locals);
177
178      // Jump into the OSR method
179      Interpreter::invoke_osr(
180        method, istate->osr_entry(), istate->osr_buf(), THREAD);
181      return;
182    }
183    else if (istate->msg() == BytecodeInterpreter::call_method_handle) {
184      oop method_handle = istate->callee();
185
186      // Trim back the stack to put the parameters at the top
187      stack->set_sp(istate->stack() + 1);
188
189      // Make the call
190      process_method_handle(method_handle, THREAD);
191      fixup_after_potential_safepoint();
192
193      // Convert the result
194      istate->set_stack(stack->sp() - 1);
195
196      // Restore the stack
197      stack->set_sp(istate->stack_limit() + 1);
198
199      // Resume the interpreter
200      istate->set_msg(BytecodeInterpreter::method_resume);
201    }
202    else {
203      ShouldNotReachHere();
204    }
205  }
206
207  // Unwind the current frame
208  thread->pop_zero_frame();
209
210  // Pop our local variables
211  stack->set_sp(stack->sp() + method->max_locals());
212
213  // Push our result
214  for (int i = 0; i < result_slots; i++)
215    stack->push(result[-i]);
216}
217
218int CppInterpreter::native_entry(Method* method, intptr_t UNUSED, TRAPS) {
219  // Make sure method is native and not abstract
220  assert(method->is_native() && !method->is_abstract(), "should be");
221
222  JavaThread *thread = (JavaThread *) THREAD;
223  ZeroStack *stack = thread->zero_stack();
224
225  // Allocate and initialize our frame
226  InterpreterFrame *frame = InterpreterFrame::build(method, CHECK_0);
227  thread->push_zero_frame(frame);
228  interpreterState istate = frame->interpreter_state();
229  intptr_t *locals = istate->locals();
230
231  // Update the invocation counter
232  if ((UseCompiler || CountCompiledCalls) && !method->is_synchronized()) {
233    InvocationCounter *counter = method->invocation_counter();
234    counter->increment();
235    if (counter->reached_InvocationLimit()) {
236      CALL_VM_NOCHECK(
237        InterpreterRuntime::frequency_counter_overflow(thread, NULL));
238      if (HAS_PENDING_EXCEPTION)
239        goto unwind_and_return;
240    }
241  }
242
243  // Lock if necessary
244  BasicObjectLock *monitor;
245  monitor = NULL;
246  if (method->is_synchronized()) {
247    monitor = (BasicObjectLock*) istate->stack_base();
248    oop lockee = monitor->obj();
249    markOop disp = lockee->mark()->set_unlocked();
250
251    monitor->lock()->set_displaced_header(disp);
252    if (Atomic::cmpxchg_ptr(monitor, lockee->mark_addr(), disp) != disp) {
253      if (thread->is_lock_owned((address) disp->clear_lock_bits())) {
254        monitor->lock()->set_displaced_header(NULL);
255      }
256      else {
257        CALL_VM_NOCHECK(InterpreterRuntime::monitorenter(thread, monitor));
258        if (HAS_PENDING_EXCEPTION)
259          goto unwind_and_return;
260      }
261    }
262  }
263
264  // Get the signature handler
265  InterpreterRuntime::SignatureHandler *handler; {
266    address handlerAddr = method->signature_handler();
267    if (handlerAddr == NULL) {
268      CALL_VM_NOCHECK(InterpreterRuntime::prepare_native_call(thread, method));
269      if (HAS_PENDING_EXCEPTION)
270        goto unlock_unwind_and_return;
271
272      handlerAddr = method->signature_handler();
273      assert(handlerAddr != NULL, "eh?");
274    }
275    if (handlerAddr == (address) InterpreterRuntime::slow_signature_handler) {
276      CALL_VM_NOCHECK(handlerAddr =
277        InterpreterRuntime::slow_signature_handler(thread, method, NULL,NULL));
278      if (HAS_PENDING_EXCEPTION)
279        goto unlock_unwind_and_return;
280    }
281    handler = \
282      InterpreterRuntime::SignatureHandler::from_handlerAddr(handlerAddr);
283  }
284
285  // Get the native function entry point
286  address function;
287  function = method->native_function();
288  assert(function != NULL, "should be set if signature handler is");
289
290  // Build the argument list
291  stack->overflow_check(handler->argument_count() * 2, THREAD);
292  if (HAS_PENDING_EXCEPTION)
293    goto unlock_unwind_and_return;
294
295  void **arguments;
296  void *mirror; {
297    arguments =
298      (void **) stack->alloc(handler->argument_count() * sizeof(void **));
299    void **dst = arguments;
300
301    void *env = thread->jni_environment();
302    *(dst++) = &env;
303
304    if (method->is_static()) {
305      istate->set_oop_temp(
306        method->constants()->pool_holder()->java_mirror());
307      mirror = istate->oop_temp_addr();
308      *(dst++) = &mirror;
309    }
310
311    intptr_t *src = locals;
312    for (int i = dst - arguments; i < handler->argument_count(); i++) {
313      ffi_type *type = handler->argument_type(i);
314      if (type == &ffi_type_pointer) {
315        if (*src) {
316          stack->push((intptr_t) src);
317          *(dst++) = stack->sp();
318        }
319        else {
320          *(dst++) = src;
321        }
322        src--;
323      }
324      else if (type->size == 4) {
325        *(dst++) = src--;
326      }
327      else if (type->size == 8) {
328        src--;
329        *(dst++) = src--;
330      }
331      else {
332        ShouldNotReachHere();
333      }
334    }
335  }
336
337  // Set up the Java frame anchor
338  thread->set_last_Java_frame();
339
340  // Change the thread state to _thread_in_native
341  ThreadStateTransition::transition_from_java(thread, _thread_in_native);
342
343  // Make the call
344  intptr_t result[4 - LogBytesPerWord];
345  ffi_call(handler->cif(), (void (*)()) function, result, arguments);
346
347  // Change the thread state back to _thread_in_Java.
348  // ThreadStateTransition::transition_from_native() cannot be used
349  // here because it does not check for asynchronous exceptions.
350  // We have to manage the transition ourself.
351  thread->set_thread_state(_thread_in_native_trans);
352
353  // Make sure new state is visible in the GC thread
354  if (os::is_MP()) {
355    if (UseMembar) {
356      OrderAccess::fence();
357    }
358    else {
359      InterfaceSupport::serialize_memory(thread);
360    }
361  }
362
363  // Handle safepoint operations, pending suspend requests,
364  // and pending asynchronous exceptions.
365  if (SafepointSynchronize::do_call_back() ||
366      thread->has_special_condition_for_native_trans()) {
367    JavaThread::check_special_condition_for_native_trans(thread);
368    CHECK_UNHANDLED_OOPS_ONLY(thread->clear_unhandled_oops());
369  }
370
371  // Finally we can change the thread state to _thread_in_Java.
372  thread->set_thread_state(_thread_in_Java);
373  fixup_after_potential_safepoint();
374
375  // Clear the frame anchor
376  thread->reset_last_Java_frame();
377
378  // If the result was an oop then unbox it and store it in
379  // oop_temp where the garbage collector can see it before
380  // we release the handle it might be protected by.
381  if (handler->result_type() == &ffi_type_pointer) {
382    if (result[0])
383      istate->set_oop_temp(*(oop *) result[0]);
384    else
385      istate->set_oop_temp(NULL);
386  }
387
388  // Reset handle block
389  thread->active_handles()->clear();
390
391 unlock_unwind_and_return:
392
393  // Unlock if necessary
394  if (monitor) {
395    BasicLock *lock = monitor->lock();
396    markOop header = lock->displaced_header();
397    oop rcvr = monitor->obj();
398    monitor->set_obj(NULL);
399
400    if (header != NULL) {
401      if (Atomic::cmpxchg_ptr(header, rcvr->mark_addr(), lock) != lock) {
402        monitor->set_obj(rcvr); {
403          HandleMark hm(thread);
404          CALL_VM_NOCHECK(InterpreterRuntime::monitorexit(thread, monitor));
405        }
406      }
407    }
408  }
409
410 unwind_and_return:
411
412  // Unwind the current activation
413  thread->pop_zero_frame();
414
415  // Pop our parameters
416  stack->set_sp(stack->sp() + method->size_of_parameters());
417
418  // Push our result
419  if (!HAS_PENDING_EXCEPTION) {
420    BasicType type = result_type_of(method);
421    stack->set_sp(stack->sp() - type2size[type]);
422
423    switch (type) {
424    case T_VOID:
425      break;
426
427    case T_BOOLEAN:
428#ifndef VM_LITTLE_ENDIAN
429      result[0] <<= (BitsPerWord - BitsPerByte);
430#endif
431      SET_LOCALS_INT(*(jboolean *) result != 0, 0);
432      break;
433
434    case T_CHAR:
435#ifndef VM_LITTLE_ENDIAN
436      result[0] <<= (BitsPerWord - BitsPerShort);
437#endif
438      SET_LOCALS_INT(*(jchar *) result, 0);
439      break;
440
441    case T_BYTE:
442#ifndef VM_LITTLE_ENDIAN
443      result[0] <<= (BitsPerWord - BitsPerByte);
444#endif
445      SET_LOCALS_INT(*(jbyte *) result, 0);
446      break;
447
448    case T_SHORT:
449#ifndef VM_LITTLE_ENDIAN
450      result[0] <<= (BitsPerWord - BitsPerShort);
451#endif
452      SET_LOCALS_INT(*(jshort *) result, 0);
453      break;
454
455    case T_INT:
456#ifndef VM_LITTLE_ENDIAN
457      result[0] <<= (BitsPerWord - BitsPerInt);
458#endif
459      SET_LOCALS_INT(*(jint *) result, 0);
460      break;
461
462    case T_LONG:
463      SET_LOCALS_LONG(*(jlong *) result, 0);
464      break;
465
466    case T_FLOAT:
467      SET_LOCALS_FLOAT(*(jfloat *) result, 0);
468      break;
469
470    case T_DOUBLE:
471      SET_LOCALS_DOUBLE(*(jdouble *) result, 0);
472      break;
473
474    case T_OBJECT:
475    case T_ARRAY:
476      SET_LOCALS_OBJECT(istate->oop_temp(), 0);
477      break;
478
479    default:
480      ShouldNotReachHere();
481    }
482  }
483
484  // No deoptimized frames on the stack
485  return 0;
486}
487
488int CppInterpreter::accessor_entry(Method* method, intptr_t UNUSED, TRAPS) {
489  JavaThread *thread = (JavaThread *) THREAD;
490  ZeroStack *stack = thread->zero_stack();
491  intptr_t *locals = stack->sp();
492
493  // Drop into the slow path if we need a safepoint check
494  if (SafepointSynchronize::do_call_back()) {
495    return normal_entry(method, 0, THREAD);
496  }
497
498  // Load the object pointer and drop into the slow path
499  // if we have a NullPointerException
500  oop object = LOCALS_OBJECT(0);
501  if (object == NULL) {
502    return normal_entry(method, 0, THREAD);
503  }
504
505  // Read the field index from the bytecode, which looks like this:
506  //  0:  aload_0
507  //  1:  getfield
508  //  2:    index
509  //  3:    index
510  //  4:  ireturn/areturn
511  // NB this is not raw bytecode: index is in machine order
512  u1 *code = method->code_base();
513  assert(code[0] == Bytecodes::_aload_0 &&
514         code[1] == Bytecodes::_getfield &&
515         (code[4] == Bytecodes::_ireturn ||
516          code[4] == Bytecodes::_areturn), "should do");
517  u2 index = Bytes::get_native_u2(&code[2]);
518
519  // Get the entry from the constant pool cache, and drop into
520  // the slow path if it has not been resolved
521  ConstantPoolCache* cache = method->constants()->cache();
522  ConstantPoolCacheEntry* entry = cache->entry_at(index);
523  if (!entry->is_resolved(Bytecodes::_getfield)) {
524    return normal_entry(method, 0, THREAD);
525  }
526
527  // Get the result and push it onto the stack
528  switch (entry->flag_state()) {
529  case ltos:
530  case dtos:
531    stack->overflow_check(1, CHECK_0);
532    stack->alloc(wordSize);
533    break;
534  }
535  if (entry->is_volatile()) {
536    switch (entry->flag_state()) {
537    case ctos:
538      SET_LOCALS_INT(object->char_field_acquire(entry->f2()), 0);
539      break;
540
541    case btos:
542      SET_LOCALS_INT(object->byte_field_acquire(entry->f2()), 0);
543      break;
544
545    case stos:
546      SET_LOCALS_INT(object->short_field_acquire(entry->f2()), 0);
547      break;
548
549    case itos:
550      SET_LOCALS_INT(object->int_field_acquire(entry->f2()), 0);
551      break;
552
553    case ltos:
554      SET_LOCALS_LONG(object->long_field_acquire(entry->f2()), 0);
555      break;
556
557    case ftos:
558      SET_LOCALS_FLOAT(object->float_field_acquire(entry->f2()), 0);
559      break;
560
561    case dtos:
562      SET_LOCALS_DOUBLE(object->double_field_acquire(entry->f2()), 0);
563      break;
564
565    case atos:
566      SET_LOCALS_OBJECT(object->obj_field_acquire(entry->f2()), 0);
567      break;
568
569    default:
570      ShouldNotReachHere();
571    }
572  }
573  else {
574    switch (entry->flag_state()) {
575    case ctos:
576      SET_LOCALS_INT(object->char_field(entry->f2()), 0);
577      break;
578
579    case btos:
580      SET_LOCALS_INT(object->byte_field(entry->f2()), 0);
581      break;
582
583    case stos:
584      SET_LOCALS_INT(object->short_field(entry->f2()), 0);
585      break;
586
587    case itos:
588      SET_LOCALS_INT(object->int_field(entry->f2()), 0);
589      break;
590
591    case ltos:
592      SET_LOCALS_LONG(object->long_field(entry->f2()), 0);
593      break;
594
595    case ftos:
596      SET_LOCALS_FLOAT(object->float_field(entry->f2()), 0);
597      break;
598
599    case dtos:
600      SET_LOCALS_DOUBLE(object->double_field(entry->f2()), 0);
601      break;
602
603    case atos:
604      SET_LOCALS_OBJECT(object->obj_field(entry->f2()), 0);
605      break;
606
607    default:
608      ShouldNotReachHere();
609    }
610  }
611
612  // No deoptimized frames on the stack
613  return 0;
614}
615
616int CppInterpreter::empty_entry(Method* method, intptr_t UNUSED, TRAPS) {
617  JavaThread *thread = (JavaThread *) THREAD;
618  ZeroStack *stack = thread->zero_stack();
619
620  // Drop into the slow path if we need a safepoint check
621  if (SafepointSynchronize::do_call_back()) {
622    return normal_entry(method, 0, THREAD);
623  }
624
625  // Pop our parameters
626  stack->set_sp(stack->sp() + method->size_of_parameters());
627
628  // No deoptimized frames on the stack
629  return 0;
630}
631
632int CppInterpreter::method_handle_entry(Method* method,
633                                        intptr_t UNUSED, TRAPS) {
634  JavaThread *thread = (JavaThread *) THREAD;
635  ZeroStack *stack = thread->zero_stack();
636  int argument_slots = method->size_of_parameters();
637  int result_slots = type2size[result_type_of(method)];
638  intptr_t *vmslots = stack->sp();
639  intptr_t *unwind_sp = vmslots + argument_slots;
640
641  // Find the MethodType
642  address p = (address) method;
643  for (jint* pc = method->method_type_offsets_chain(); (*pc) != -1; pc++) {
644    p = *(address*)(p + (*pc));
645  }
646  oop method_type = (oop) p;
647
648  // The MethodHandle is in the slot after the arguments
649  int num_vmslots = argument_slots - 1;
650  oop method_handle = VMSLOTS_OBJECT(num_vmslots);
651
652  // InvokeGeneric requires some extra shuffling
653  oop mhtype = java_lang_invoke_MethodHandle::type(method_handle);
654  bool is_exact = mhtype == method_type;
655  if (!is_exact) {
656    if (true || // FIXME
657        method->intrinsic_id() == vmIntrinsics::_invokeExact) {
658      CALL_VM_NOCHECK_NOFIX(
659        SharedRuntime::throw_WrongMethodTypeException(
660          thread, method_type, mhtype));
661      // NB all oops trashed!
662      assert(HAS_PENDING_EXCEPTION, "should do");
663      stack->set_sp(unwind_sp);
664      return 0;
665    }
666    assert(method->intrinsic_id() == vmIntrinsics::_invokeGeneric, "should be");
667
668    // Load up an adapter from the calling type
669    // NB the x86 code for this (in methodHandles_x86.cpp, search for
670    // "genericInvoker") is really really odd.  I'm hoping it's trying
671    // to accomodate odd VM/class library combinations I can ignore.
672    oop adapter = NULL; //FIXME: load the adapter from the CP cache
673    IF (adapter == NULL) {
674      CALL_VM_NOCHECK_NOFIX(
675        SharedRuntime::throw_WrongMethodTypeException(
676          thread, method_type, mhtype));
677      // NB all oops trashed!
678      assert(HAS_PENDING_EXCEPTION, "should do");
679      stack->set_sp(unwind_sp);
680      return 0;
681    }
682
683    // Adapters are shared among form-families of method-type.  The
684    // type being called is passed as a trusted first argument so that
685    // the adapter knows the actual types of its arguments and return
686    // values.
687    insert_vmslots(num_vmslots + 1, 1, THREAD);
688    if (HAS_PENDING_EXCEPTION) {
689      // NB all oops trashed!
690      stack->set_sp(unwind_sp);
691      return 0;
692    }
693
694    vmslots = stack->sp();
695    num_vmslots++;
696    SET_VMSLOTS_OBJECT(method_type, num_vmslots);
697
698    method_handle = adapter;
699  }
700
701  // Start processing
702  process_method_handle(method_handle, THREAD);
703  if (HAS_PENDING_EXCEPTION)
704    result_slots = 0;
705
706  // If this is an invokeExact then the eventual callee will not
707  // have unwound the method handle argument so we have to do it.
708  // If a result is being returned the it will be above the method
709  // handle argument we're unwinding.
710  if (is_exact) {
711    intptr_t result[2];
712    for (int i = 0; i < result_slots; i++)
713      result[i] = stack->pop();
714    stack->pop();
715    for (int i = result_slots - 1; i >= 0; i--)
716      stack->push(result[i]);
717  }
718
719  // Check
720  assert(stack->sp() == unwind_sp - result_slots, "should be");
721
722  // No deoptimized frames on the stack
723  return 0;
724}
725
726void CppInterpreter::process_method_handle(oop method_handle, TRAPS) {
727  JavaThread *thread = (JavaThread *) THREAD;
728  ZeroStack *stack = thread->zero_stack();
729  intptr_t *vmslots = stack->sp();
730
731  bool direct_to_method = false;
732  BasicType src_rtype = T_ILLEGAL;
733  BasicType dst_rtype = T_ILLEGAL;
734
735  MethodHandleEntry *entry =
736    java_lang_invoke_MethodHandle::vmentry(method_handle);
737  MethodHandles::EntryKind entry_kind =
738    (MethodHandles::EntryKind) (((intptr_t) entry) & 0xffffffff);
739
740  Method* method = NULL;
741  switch (entry_kind) {
742  case MethodHandles::_invokestatic_mh:
743    direct_to_method = true;
744    break;
745
746  case MethodHandles::_invokespecial_mh:
747  case MethodHandles::_invokevirtual_mh:
748  case MethodHandles::_invokeinterface_mh:
749    {
750      oop receiver =
751        VMSLOTS_OBJECT(
752          java_lang_invoke_MethodHandle::vmslots(method_handle) - 1);
753      if (receiver == NULL) {
754          stack->set_sp(calculate_unwind_sp(stack, method_handle));
755          CALL_VM_NOCHECK_NOFIX(
756            throw_exception(
757              thread, vmSymbols::java_lang_NullPointerException()));
758          // NB all oops trashed!
759          assert(HAS_PENDING_EXCEPTION, "should do");
760          return;
761      }
762      if (entry_kind != MethodHandles::_invokespecial_mh) {
763        intptr_t index = java_lang_invoke_DirectMethodHandle::vmindex(method_handle);
764        InstanceKlass* rcvrKlass =
765          (InstanceKlass *) receiver->klass();
766        if (entry_kind == MethodHandles::_invokevirtual_mh) {
767          method = (Method*) rcvrKlass->start_of_vtable()[index];
768        }
769        else {
770          oop iclass = java_lang_invoke_MethodHandle::next_target(method_handle);
771          itableOffsetEntry* ki =
772            (itableOffsetEntry *) rcvrKlass->start_of_itable();
773          int i, length = rcvrKlass->itable_length();
774          for (i = 0; i < length; i++, ki++ ) {
775            if (ki->interface_klass() == iclass)
776              break;
777          }
778          if (i == length) {
779            stack->set_sp(calculate_unwind_sp(stack, method_handle));
780            CALL_VM_NOCHECK_NOFIX(
781              throw_exception(
782                thread, vmSymbols::java_lang_IncompatibleClassChangeError()));
783            // NB all oops trashed!
784            assert(HAS_PENDING_EXCEPTION, "should do");
785            return;
786          }
787          itableMethodEntry* im = ki->first_method_entry(receiver->klass());
788          method = im[index].method();
789          if (method == NULL) {
790            stack->set_sp(calculate_unwind_sp(stack, method_handle));
791            CALL_VM_NOCHECK_NOFIX(
792              throw_exception(
793                thread, vmSymbols::java_lang_AbstractMethodError()));
794            // NB all oops trashed!
795            assert(HAS_PENDING_EXCEPTION, "should do");
796            return;
797          }
798        }
799      }
800    }
801    direct_to_method = true;
802    break;
803
804  case MethodHandles::_bound_ref_direct_mh:
805  case MethodHandles::_bound_int_direct_mh:
806  case MethodHandles::_bound_long_direct_mh:
807    direct_to_method = true;
808    // fall through
809  case MethodHandles::_bound_ref_mh:
810  case MethodHandles::_bound_int_mh:
811  case MethodHandles::_bound_long_mh:
812    {
813      BasicType arg_type  = T_ILLEGAL;
814      int       arg_mask  = -1;
815      int       arg_slots = -1;
816      MethodHandles::get_ek_bound_mh_info(
817        entry_kind, arg_type, arg_mask, arg_slots);
818      int arg_slot =
819        java_lang_invoke_BoundMethodHandle::vmargslot(method_handle);
820
821      // Create the new slot(s)
822      intptr_t *unwind_sp = calculate_unwind_sp(stack, method_handle);
823      insert_vmslots(arg_slot, arg_slots, THREAD);
824      if (HAS_PENDING_EXCEPTION) {
825        // all oops trashed
826        stack->set_sp(unwind_sp);
827        return;
828      }
829      vmslots = stack->sp();
830
831      // Store bound argument into new stack slot
832      oop arg = java_lang_invoke_BoundMethodHandle::argument(method_handle);
833      if (arg_type == T_OBJECT) {
834        assert(arg_slots == 1, "should be");
835        SET_VMSLOTS_OBJECT(arg, arg_slot);
836      }
837      else {
838        jvalue arg_value;
839        arg_type = java_lang_boxing_object::get_value(arg, &arg_value);
840        switch (arg_type) {
841        case T_BOOLEAN:
842          SET_VMSLOTS_INT(arg_value.z, arg_slot);
843          break;
844        case T_CHAR:
845          SET_VMSLOTS_INT(arg_value.c, arg_slot);
846          break;
847        case T_BYTE:
848          SET_VMSLOTS_INT(arg_value.b, arg_slot);
849          break;
850        case T_SHORT:
851          SET_VMSLOTS_INT(arg_value.s, arg_slot);
852          break;
853        case T_INT:
854          SET_VMSLOTS_INT(arg_value.i, arg_slot);
855          break;
856        case T_FLOAT:
857          SET_VMSLOTS_FLOAT(arg_value.f, arg_slot);
858          break;
859        case T_LONG:
860          SET_VMSLOTS_LONG(arg_value.j, arg_slot + 1);
861          break;
862        case T_DOUBLE:
863          SET_VMSLOTS_DOUBLE(arg_value.d, arg_slot + 1);
864          break;
865        default:
866          tty->print_cr("unhandled type %s", type2name(arg_type));
867          ShouldNotReachHere();
868        }
869      }
870    }
871    break;
872
873  case MethodHandles::_adapter_retype_only:
874  case MethodHandles::_adapter_retype_raw:
875    src_rtype = result_type_of_handle(
876      java_lang_invoke_MethodHandle::next_target(method_handle));
877    dst_rtype = result_type_of_handle(method_handle);
878    break;
879
880  case MethodHandles::_adapter_check_cast:
881    {
882      int arg_slot =
883        java_lang_invoke_AdapterMethodHandle::vmargslot(method_handle);
884      oop arg = VMSLOTS_OBJECT(arg_slot);
885      if (arg != NULL) {
886        Klass* objKlassOop = arg->klass();
887        Klass* klassOf = java_lang_Class::as_Klass(
888          java_lang_invoke_AdapterMethodHandle::argument(method_handle));
889
890        if (objKlassOop != klassOf &&
891            !objKlassOop->is_subtype_of(klassOf)) {
892          ResourceMark rm(THREAD);
893          const char* objName = Klass::cast(objKlassOop)->external_name();
894          const char* klassName = Klass::cast(klassOf)->external_name();
895          char* message = SharedRuntime::generate_class_cast_message(
896            objName, klassName);
897
898          stack->set_sp(calculate_unwind_sp(stack, method_handle));
899          CALL_VM_NOCHECK_NOFIX(
900            throw_exception(
901              thread, vmSymbols::java_lang_ClassCastException(), message));
902          // NB all oops trashed!
903          assert(HAS_PENDING_EXCEPTION, "should do");
904          return;
905        }
906      }
907    }
908    break;
909
910  case MethodHandles::_adapter_dup_args:
911    {
912      int arg_slot =
913        java_lang_invoke_AdapterMethodHandle::vmargslot(method_handle);
914      int conv =
915        java_lang_invoke_AdapterMethodHandle::conversion(method_handle);
916      int num_slots = -MethodHandles::adapter_conversion_stack_move(conv);
917      assert(num_slots > 0, "should be");
918
919      // Create the new slot(s)
920      intptr_t *unwind_sp = calculate_unwind_sp(stack, method_handle);
921      stack->overflow_check(num_slots, THREAD);
922      if (HAS_PENDING_EXCEPTION) {
923        // all oops trashed
924        stack->set_sp(unwind_sp);
925        return;
926      }
927
928      // Duplicate the arguments
929      for (int i = num_slots - 1; i >= 0; i--)
930        stack->push(*VMSLOTS_SLOT(arg_slot + i));
931
932      vmslots = stack->sp(); // unused, but let the compiler figure that out
933    }
934    break;
935
936  case MethodHandles::_adapter_drop_args:
937    {
938      int arg_slot =
939        java_lang_invoke_AdapterMethodHandle::vmargslot(method_handle);
940      int conv =
941        java_lang_invoke_AdapterMethodHandle::conversion(method_handle);
942      int num_slots = MethodHandles::adapter_conversion_stack_move(conv);
943      assert(num_slots > 0, "should be");
944
945      remove_vmslots(arg_slot, num_slots, THREAD); // doesn't trap
946      vmslots = stack->sp(); // unused, but let the compiler figure that out
947    }
948    break;
949
950  case MethodHandles::_adapter_opt_swap_1:
951  case MethodHandles::_adapter_opt_swap_2:
952  case MethodHandles::_adapter_opt_rot_1_up:
953  case MethodHandles::_adapter_opt_rot_1_down:
954  case MethodHandles::_adapter_opt_rot_2_up:
955  case MethodHandles::_adapter_opt_rot_2_down:
956    {
957      int arg1 =
958        java_lang_invoke_AdapterMethodHandle::vmargslot(method_handle);
959      int conv =
960        java_lang_invoke_AdapterMethodHandle::conversion(method_handle);
961      int arg2 = MethodHandles::adapter_conversion_vminfo(conv);
962
963      int swap_bytes = 0, rotate = 0;
964      MethodHandles::get_ek_adapter_opt_swap_rot_info(
965        entry_kind, swap_bytes, rotate);
966      int swap_slots = swap_bytes >> LogBytesPerWord;
967
968      intptr_t tmp;
969      switch (rotate) {
970      case 0: // swap
971        for (int i = 0; i < swap_slots; i++) {
972          tmp = *VMSLOTS_SLOT(arg1 + i);
973          SET_VMSLOTS_SLOT(VMSLOTS_SLOT(arg2 + i), arg1 + i);
974          SET_VMSLOTS_SLOT(&tmp, arg2 + i);
975        }
976        break;
977
978      case 1: // up
979        assert(arg1 - swap_slots > arg2, "should be");
980
981        tmp = *VMSLOTS_SLOT(arg1);
982        for (int i = arg1 - swap_slots; i >= arg2; i--)
983          SET_VMSLOTS_SLOT(VMSLOTS_SLOT(i), i + swap_slots);
984        SET_VMSLOTS_SLOT(&tmp, arg2);
985
986        break;
987
988      case -1: // down
989        assert(arg2 - swap_slots > arg1, "should be");
990
991        tmp = *VMSLOTS_SLOT(arg1);
992        for (int i = arg1 + swap_slots; i <= arg2; i++)
993          SET_VMSLOTS_SLOT(VMSLOTS_SLOT(i), i - swap_slots);
994        SET_VMSLOTS_SLOT(&tmp, arg2);
995        break;
996
997      default:
998        ShouldNotReachHere();
999      }
1000    }
1001    break;
1002
1003  case MethodHandles::_adapter_opt_i2l:
1004    {
1005      int arg_slot =
1006        java_lang_invoke_AdapterMethodHandle::vmargslot(method_handle);
1007      int arg = VMSLOTS_INT(arg_slot);
1008      intptr_t *unwind_sp = calculate_unwind_sp(stack, method_handle);
1009      insert_vmslots(arg_slot, 1, THREAD);
1010      if (HAS_PENDING_EXCEPTION) {
1011        // all oops trashed
1012        stack->set_sp(unwind_sp);
1013        return;
1014      }
1015      vmslots = stack->sp();
1016      arg_slot++;
1017      SET_VMSLOTS_LONG(arg, arg_slot);
1018    }
1019    break;
1020
1021  case MethodHandles::_adapter_opt_unboxi:
1022  case MethodHandles::_adapter_opt_unboxl:
1023    {
1024      int arg_slot =
1025        java_lang_invoke_AdapterMethodHandle::vmargslot(method_handle);
1026      oop arg = VMSLOTS_OBJECT(arg_slot);
1027      jvalue arg_value;
1028      if (arg == NULL) {
1029        // queue a nullpointer exception for the caller
1030        stack->set_sp(calculate_unwind_sp(stack, method_handle));
1031        CALL_VM_NOCHECK_NOFIX(
1032          throw_exception(
1033            thread, vmSymbols::java_lang_NullPointerException()));
1034        // NB all oops trashed!
1035        assert(HAS_PENDING_EXCEPTION, "should do");
1036        return;
1037      }
1038      BasicType arg_type = java_lang_boxing_object::get_value(arg, &arg_value);
1039      if (arg_type == T_LONG || arg_type == T_DOUBLE) {
1040        intptr_t *unwind_sp = calculate_unwind_sp(stack, method_handle);
1041        insert_vmslots(arg_slot, 1, THREAD);
1042        if (HAS_PENDING_EXCEPTION) {
1043          // all oops trashed
1044          stack->set_sp(unwind_sp);
1045          return;
1046        }
1047        vmslots = stack->sp();
1048        arg_slot++;
1049      }
1050      switch (arg_type) {
1051      case T_BOOLEAN:
1052        SET_VMSLOTS_INT(arg_value.z, arg_slot);
1053        break;
1054      case T_CHAR:
1055        SET_VMSLOTS_INT(arg_value.c, arg_slot);
1056        break;
1057      case T_BYTE:
1058        SET_VMSLOTS_INT(arg_value.b, arg_slot);
1059        break;
1060      case T_SHORT:
1061        SET_VMSLOTS_INT(arg_value.s, arg_slot);
1062        break;
1063      case T_INT:
1064        SET_VMSLOTS_INT(arg_value.i, arg_slot);
1065        break;
1066      case T_FLOAT:
1067        SET_VMSLOTS_FLOAT(arg_value.f, arg_slot);
1068        break;
1069      case T_LONG:
1070        SET_VMSLOTS_LONG(arg_value.j, arg_slot);
1071        break;
1072      case T_DOUBLE:
1073        SET_VMSLOTS_DOUBLE(arg_value.d, arg_slot);
1074        break;
1075      default:
1076        tty->print_cr("unhandled type %s", type2name(arg_type));
1077        ShouldNotReachHere();
1078      }
1079    }
1080    break;
1081
1082  default:
1083    tty->print_cr("unhandled entry_kind %s",
1084                  MethodHandles::entry_name(entry_kind));
1085    ShouldNotReachHere();
1086  }
1087
1088  // Continue along the chain
1089  if (direct_to_method) {
1090    if (method == NULL) {
1091      method =
1092        (Method*) java_lang_invoke_MethodHandle::vmtarget(method_handle);
1093    }
1094    address entry_point = method->from_interpreted_entry();
1095    Interpreter::invoke_method(method, entry_point, THREAD);
1096  }
1097  else {
1098    process_method_handle(
1099      java_lang_invoke_MethodHandle::next_target(method_handle), THREAD);
1100  }
1101  // NB all oops now trashed
1102
1103  // Adapt the result type, if necessary
1104  if (src_rtype != dst_rtype && !HAS_PENDING_EXCEPTION) {
1105    switch (dst_rtype) {
1106    case T_VOID:
1107      for (int i = 0; i < type2size[src_rtype]; i++)
1108        stack->pop();
1109      return;
1110
1111    case T_INT:
1112      switch (src_rtype) {
1113      case T_VOID:
1114        stack->overflow_check(1, CHECK);
1115        stack->push(0);
1116        return;
1117
1118      case T_BOOLEAN:
1119      case T_CHAR:
1120      case T_BYTE:
1121      case T_SHORT:
1122        return;
1123      }
1124      // INT results sometimes need narrowing
1125    case T_BOOLEAN:
1126    case T_CHAR:
1127    case T_BYTE:
1128    case T_SHORT:
1129      switch (src_rtype) {
1130      case T_INT:
1131        return;
1132      }
1133    }
1134
1135    tty->print_cr("unhandled conversion:");
1136    tty->print_cr("src_rtype = %s", type2name(src_rtype));
1137    tty->print_cr("dst_rtype = %s", type2name(dst_rtype));
1138    ShouldNotReachHere();
1139  }
1140}
1141
1142// The new slots will be inserted before slot insert_before.
1143// Slots < insert_before will have the same slot number after the insert.
1144// Slots >= insert_before will become old_slot + num_slots.
1145void CppInterpreter::insert_vmslots(int insert_before, int num_slots, TRAPS) {
1146  JavaThread *thread = (JavaThread *) THREAD;
1147  ZeroStack *stack = thread->zero_stack();
1148
1149  // Allocate the space
1150  stack->overflow_check(num_slots, CHECK);
1151  stack->alloc(num_slots * wordSize);
1152  intptr_t *vmslots = stack->sp();
1153
1154  // Shuffle everything up
1155  for (int i = 0; i < insert_before; i++)
1156    SET_VMSLOTS_SLOT(VMSLOTS_SLOT(i + num_slots), i);
1157}
1158
1159void CppInterpreter::remove_vmslots(int first_slot, int num_slots, TRAPS) {
1160  JavaThread *thread = (JavaThread *) THREAD;
1161  ZeroStack *stack = thread->zero_stack();
1162  intptr_t *vmslots = stack->sp();
1163
1164  // Move everything down
1165  for (int i = first_slot - 1; i >= 0; i--)
1166    SET_VMSLOTS_SLOT(VMSLOTS_SLOT(i), i + num_slots);
1167
1168  // Deallocate the space
1169  stack->set_sp(stack->sp() + num_slots);
1170}
1171
1172BasicType CppInterpreter::result_type_of_handle(oop method_handle) {
1173  oop method_type = java_lang_invoke_MethodHandle::type(method_handle);
1174  oop return_type = java_lang_invoke_MethodType::rtype(method_type);
1175  return java_lang_Class::as_BasicType(return_type, (Klass* *) NULL);
1176}
1177
1178intptr_t* CppInterpreter::calculate_unwind_sp(ZeroStack* stack,
1179                                              oop method_handle) {
1180  oop method_type = java_lang_invoke_MethodHandle::type(method_handle);
1181  int argument_slots = java_lang_invoke_MethodType::ptype_slot_count(method_type);
1182
1183  return stack->sp() + argument_slots;
1184}
1185
1186IRT_ENTRY(void, CppInterpreter::throw_exception(JavaThread* thread,
1187                                                Symbol*     name,
1188                                                char*       message))
1189  THROW_MSG(name, message);
1190IRT_END
1191
1192InterpreterFrame *InterpreterFrame::build(Method* const method, TRAPS) {
1193  JavaThread *thread = (JavaThread *) THREAD;
1194  ZeroStack *stack = thread->zero_stack();
1195
1196  // Calculate the size of the frame we'll build, including
1197  // any adjustments to the caller's frame that we'll make.
1198  int extra_locals  = 0;
1199  int monitor_words = 0;
1200  int stack_words   = 0;
1201
1202  if (!method->is_native()) {
1203    extra_locals = method->max_locals() - method->size_of_parameters();
1204    stack_words  = method->max_stack();
1205  }
1206  if (method->is_synchronized()) {
1207    monitor_words = frame::interpreter_frame_monitor_size();
1208  }
1209  stack->overflow_check(
1210    extra_locals + header_words + monitor_words + stack_words, CHECK_NULL);
1211
1212  // Adjust the caller's stack frame to accomodate any additional
1213  // local variables we have contiguously with our parameters.
1214  for (int i = 0; i < extra_locals; i++)
1215    stack->push(0);
1216
1217  intptr_t *locals;
1218  if (method->is_native())
1219    locals = stack->sp() + (method->size_of_parameters() - 1);
1220  else
1221    locals = stack->sp() + (method->max_locals() - 1);
1222
1223  stack->push(0); // next_frame, filled in later
1224  intptr_t *fp = stack->sp();
1225  assert(fp - stack->sp() == next_frame_off, "should be");
1226
1227  stack->push(INTERPRETER_FRAME);
1228  assert(fp - stack->sp() == frame_type_off, "should be");
1229
1230  interpreterState istate =
1231    (interpreterState) stack->alloc(sizeof(BytecodeInterpreter));
1232  assert(fp - stack->sp() == istate_off, "should be");
1233
1234  istate->set_locals(locals);
1235  istate->set_method(method);
1236  istate->set_self_link(istate);
1237  istate->set_prev_link(NULL);
1238  istate->set_thread(thread);
1239  istate->set_bcp(method->is_native() ? NULL : method->code_base());
1240  istate->set_constants(method->constants()->cache());
1241  istate->set_msg(BytecodeInterpreter::method_entry);
1242  istate->set_oop_temp(NULL);
1243  istate->set_mdx(NULL);
1244  istate->set_callee(NULL);
1245
1246  istate->set_monitor_base((BasicObjectLock *) stack->sp());
1247  if (method->is_synchronized()) {
1248    BasicObjectLock *monitor =
1249      (BasicObjectLock *) stack->alloc(monitor_words * wordSize);
1250    oop object;
1251    if (method->is_static())
1252      object = method->constants()->pool_holder()->java_mirror();
1253    else
1254      object = (oop) locals[0];
1255    monitor->set_obj(object);
1256  }
1257
1258  istate->set_stack_base(stack->sp());
1259  istate->set_stack(stack->sp() - 1);
1260  if (stack_words)
1261    stack->alloc(stack_words * wordSize);
1262  istate->set_stack_limit(stack->sp() - 1);
1263
1264  return (InterpreterFrame *) fp;
1265}
1266
1267int AbstractInterpreter::BasicType_as_index(BasicType type) {
1268  int i = 0;
1269  switch (type) {
1270    case T_BOOLEAN: i = 0; break;
1271    case T_CHAR   : i = 1; break;
1272    case T_BYTE   : i = 2; break;
1273    case T_SHORT  : i = 3; break;
1274    case T_INT    : i = 4; break;
1275    case T_LONG   : i = 5; break;
1276    case T_VOID   : i = 6; break;
1277    case T_FLOAT  : i = 7; break;
1278    case T_DOUBLE : i = 8; break;
1279    case T_OBJECT : i = 9; break;
1280    case T_ARRAY  : i = 9; break;
1281    default       : ShouldNotReachHere();
1282  }
1283  assert(0 <= i && i < AbstractInterpreter::number_of_result_handlers,
1284         "index out of bounds");
1285  return i;
1286}
1287
1288BasicType CppInterpreter::result_type_of(Method* method) {
1289  BasicType t;
1290  switch (method->result_index()) {
1291    case 0 : t = T_BOOLEAN; break;
1292    case 1 : t = T_CHAR;    break;
1293    case 2 : t = T_BYTE;    break;
1294    case 3 : t = T_SHORT;   break;
1295    case 4 : t = T_INT;     break;
1296    case 5 : t = T_LONG;    break;
1297    case 6 : t = T_VOID;    break;
1298    case 7 : t = T_FLOAT;   break;
1299    case 8 : t = T_DOUBLE;  break;
1300    case 9 : t = T_OBJECT;  break;
1301    default: ShouldNotReachHere();
1302  }
1303  assert(AbstractInterpreter::BasicType_as_index(t) == method->result_index(),
1304         "out of step with AbstractInterpreter::BasicType_as_index");
1305  return t;
1306}
1307
1308address InterpreterGenerator::generate_empty_entry() {
1309  if (!UseFastEmptyMethods)
1310    return NULL;
1311
1312  return generate_entry((address) CppInterpreter::empty_entry);
1313}
1314
1315address InterpreterGenerator::generate_accessor_entry() {
1316  if (!UseFastAccessorMethods)
1317    return NULL;
1318
1319  return generate_entry((address) CppInterpreter::accessor_entry);
1320}
1321
1322address InterpreterGenerator::generate_Reference_get_entry(void) {
1323#ifndef SERIALGC
1324  if (UseG1GC) {
1325    // We need to generate have a routine that generates code to:
1326    //   * load the value in the referent field
1327    //   * passes that value to the pre-barrier.
1328    //
1329    // In the case of G1 this will record the value of the
1330    // referent in an SATB buffer if marking is active.
1331    // This will cause concurrent marking to mark the referent
1332    // field as live.
1333    Unimplemented();
1334  }
1335#endif // SERIALGC
1336
1337  // If G1 is not enabled then attempt to go through the accessor entry point
1338  // Reference.get is an accessor
1339  return generate_accessor_entry();
1340}
1341
1342address InterpreterGenerator::generate_native_entry(bool synchronized) {
1343  assert(synchronized == false, "should be");
1344
1345  return generate_entry((address) CppInterpreter::native_entry);
1346}
1347
1348address InterpreterGenerator::generate_normal_entry(bool synchronized) {
1349  assert(synchronized == false, "should be");
1350
1351  return generate_entry((address) CppInterpreter::normal_entry);
1352}
1353
1354address AbstractInterpreterGenerator::generate_method_entry(
1355    AbstractInterpreter::MethodKind kind) {
1356  address entry_point = NULL;
1357
1358  switch (kind) {
1359  case Interpreter::zerolocals:
1360  case Interpreter::zerolocals_synchronized:
1361    break;
1362
1363  case Interpreter::native:
1364    entry_point = ((InterpreterGenerator*) this)->generate_native_entry(false);
1365    break;
1366
1367  case Interpreter::native_synchronized:
1368    entry_point = ((InterpreterGenerator*) this)->generate_native_entry(false);
1369    break;
1370
1371  case Interpreter::empty:
1372    entry_point = ((InterpreterGenerator*) this)->generate_empty_entry();
1373    break;
1374
1375  case Interpreter::accessor:
1376    entry_point = ((InterpreterGenerator*) this)->generate_accessor_entry();
1377    break;
1378
1379  case Interpreter::abstract:
1380    entry_point = ((InterpreterGenerator*) this)->generate_abstract_entry();
1381    break;
1382
1383  case Interpreter::method_handle:
1384    entry_point = ((InterpreterGenerator*) this)->generate_method_handle_entry();
1385    break;
1386
1387  case Interpreter::java_lang_math_sin:
1388  case Interpreter::java_lang_math_cos:
1389  case Interpreter::java_lang_math_tan:
1390  case Interpreter::java_lang_math_abs:
1391  case Interpreter::java_lang_math_log:
1392  case Interpreter::java_lang_math_log10:
1393  case Interpreter::java_lang_math_sqrt:
1394    entry_point = ((InterpreterGenerator*) this)->generate_math_entry(kind);
1395    break;
1396
1397  case Interpreter::java_lang_ref_reference_get:
1398    entry_point = ((InterpreterGenerator*)this)->generate_Reference_get_entry();
1399    break;
1400
1401  default:
1402    ShouldNotReachHere();
1403  }
1404
1405  if (entry_point == NULL)
1406    entry_point = ((InterpreterGenerator*) this)->generate_normal_entry(false);
1407
1408  return entry_point;
1409}
1410
1411InterpreterGenerator::InterpreterGenerator(StubQueue* code)
1412 : CppInterpreterGenerator(code) {
1413   generate_all();
1414}
1415
1416// Deoptimization helpers
1417
1418InterpreterFrame *InterpreterFrame::build(int size, TRAPS) {
1419  ZeroStack *stack = ((JavaThread *) THREAD)->zero_stack();
1420
1421  int size_in_words = size >> LogBytesPerWord;
1422  assert(size_in_words * wordSize == size, "unaligned");
1423  assert(size_in_words >= header_words, "too small");
1424  stack->overflow_check(size_in_words, CHECK_NULL);
1425
1426  stack->push(0); // next_frame, filled in later
1427  intptr_t *fp = stack->sp();
1428  assert(fp - stack->sp() == next_frame_off, "should be");
1429
1430  stack->push(INTERPRETER_FRAME);
1431  assert(fp - stack->sp() == frame_type_off, "should be");
1432
1433  interpreterState istate =
1434    (interpreterState) stack->alloc(sizeof(BytecodeInterpreter));
1435  assert(fp - stack->sp() == istate_off, "should be");
1436  istate->set_self_link(NULL); // mark invalid
1437
1438  stack->alloc((size_in_words - header_words) * wordSize);
1439
1440  return (InterpreterFrame *) fp;
1441}
1442
1443int AbstractInterpreter::layout_activation(Method* method,
1444                                           int       tempcount,
1445                                           int       popframe_extra_args,
1446                                           int       moncount,
1447                                           int       caller_actual_parameters,
1448                                           int       callee_param_count,
1449                                           int       callee_locals,
1450                                           frame*    caller,
1451                                           frame*    interpreter_frame,
1452                                           bool      is_top_frame) {
1453  assert(popframe_extra_args == 0, "what to do?");
1454  assert(!is_top_frame || (!callee_locals && !callee_param_count),
1455         "top frame should have no caller");
1456
1457  // This code must exactly match what InterpreterFrame::build
1458  // does (the full InterpreterFrame::build, that is, not the
1459  // one that creates empty frames for the deoptimizer).
1460  //
1461  // If interpreter_frame is not NULL then it will be filled in.
1462  // It's size is determined by a previous call to this method,
1463  // so it should be correct.
1464  //
1465  // Note that tempcount is the current size of the expression
1466  // stack.  For top most frames we will allocate a full sized
1467  // expression stack and not the trimmed version that non-top
1468  // frames have.
1469
1470  int header_words        = InterpreterFrame::header_words;
1471  int monitor_words       = moncount * frame::interpreter_frame_monitor_size();
1472  int stack_words         = is_top_frame ? method->max_stack() : tempcount;
1473  int callee_extra_locals = callee_locals - callee_param_count;
1474
1475  if (interpreter_frame) {
1476    intptr_t *locals        = interpreter_frame->fp() + method->max_locals();
1477    interpreterState istate = interpreter_frame->get_interpreterState();
1478    intptr_t *monitor_base  = (intptr_t*) istate;
1479    intptr_t *stack_base    = monitor_base - monitor_words;
1480    intptr_t *stack         = stack_base - tempcount - 1;
1481
1482    BytecodeInterpreter::layout_interpreterState(istate,
1483                                                 caller,
1484                                                 NULL,
1485                                                 method,
1486                                                 locals,
1487                                                 stack,
1488                                                 stack_base,
1489                                                 monitor_base,
1490                                                 NULL,
1491                                                 is_top_frame);
1492  }
1493  return header_words + monitor_words + stack_words + callee_extra_locals;
1494}
1495
1496void BytecodeInterpreter::layout_interpreterState(interpreterState istate,
1497                                                  frame*    caller,
1498                                                  frame*    current,
1499                                                  Method* method,
1500                                                  intptr_t* locals,
1501                                                  intptr_t* stack,
1502                                                  intptr_t* stack_base,
1503                                                  intptr_t* monitor_base,
1504                                                  intptr_t* frame_bottom,
1505                                                  bool      is_top_frame) {
1506  istate->set_locals(locals);
1507  istate->set_method(method);
1508  istate->set_self_link(istate);
1509  istate->set_prev_link(NULL);
1510  // thread will be set by a hacky repurposing of frame::patch_pc()
1511  // bcp will be set by vframeArrayElement::unpack_on_stack()
1512  istate->set_constants(method->constants()->cache());
1513  istate->set_msg(BytecodeInterpreter::method_resume);
1514  istate->set_bcp_advance(0);
1515  istate->set_oop_temp(NULL);
1516  istate->set_mdx(NULL);
1517  if (caller->is_interpreted_frame()) {
1518    interpreterState prev = caller->get_interpreterState();
1519    prev->set_callee(method);
1520    if (*prev->bcp() == Bytecodes::_invokeinterface)
1521      prev->set_bcp_advance(5);
1522    else
1523      prev->set_bcp_advance(3);
1524  }
1525  istate->set_callee(NULL);
1526  istate->set_monitor_base((BasicObjectLock *) monitor_base);
1527  istate->set_stack_base(stack_base);
1528  istate->set_stack(stack);
1529  istate->set_stack_limit(stack_base - method->max_stack() - 1);
1530}
1531
1532address CppInterpreter::return_entry(TosState state, int length) {
1533  ShouldNotCallThis();
1534}
1535
1536address CppInterpreter::deopt_entry(TosState state, int length) {
1537  return NULL;
1538}
1539
1540// Helper for (runtime) stack overflow checks
1541
1542int AbstractInterpreter::size_top_interpreter_activation(Method* method) {
1543  return 0;
1544}
1545
1546// Helper for figuring out if frames are interpreter frames
1547
1548bool CppInterpreter::contains(address pc) {
1549#ifdef PRODUCT
1550  ShouldNotCallThis();
1551#else
1552  return false; // make frame::print_value_on work
1553#endif // !PRODUCT
1554}
1555
1556// Result handlers and convertors
1557
1558address CppInterpreterGenerator::generate_result_handler_for(
1559    BasicType type) {
1560  assembler()->advance(1);
1561  return ShouldNotCallThisStub();
1562}
1563
1564address CppInterpreterGenerator::generate_tosca_to_stack_converter(
1565    BasicType type) {
1566  assembler()->advance(1);
1567  return ShouldNotCallThisStub();
1568}
1569
1570address CppInterpreterGenerator::generate_stack_to_stack_converter(
1571    BasicType type) {
1572  assembler()->advance(1);
1573  return ShouldNotCallThisStub();
1574}
1575
1576address CppInterpreterGenerator::generate_stack_to_native_abi_converter(
1577    BasicType type) {
1578  assembler()->advance(1);
1579  return ShouldNotCallThisStub();
1580}
1581
1582#endif // CC_INTERP
1583