1 /*
  2  * Copyright (c) 2003, 2023, 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/interpreter.hpp"
 29 #include "interpreter/interpreterRuntime.hpp"
 30 #include "interpreter/zero/bytecodeInterpreter.hpp"
 31 #include "interpreter/zero/zeroInterpreter.hpp"
 32 #include "interpreter/zero/zeroInterpreterGenerator.hpp"
 33 #include "oops/access.inline.hpp"
 34 #include "oops/cpCache.inline.hpp"
 35 #include "oops/klass.inline.hpp"
 36 #include "oops/methodData.hpp"
 37 #include "oops/method.hpp"
 38 #include "oops/oop.inline.hpp"
 39 #include "prims/jvmtiExport.hpp"
 40 #include "runtime/basicLock.inline.hpp"
 41 #include "runtime/frame.inline.hpp"
 42 #include "runtime/handles.inline.hpp"
 43 #include "runtime/interfaceSupport.inline.hpp"
 44 #include "runtime/jniHandles.inline.hpp"
 45 #include "runtime/timer.hpp"
 46 #include "runtime/timerTrace.hpp"
 47 #include "utilities/debug.hpp"
 48 #include "utilities/globalDefinitions.hpp"
 49 #include "utilities/macros.hpp"
 50 
 51 #include "entry_zero.hpp"
 52 #include "stack_zero.inline.hpp"
 53 
 54 void ZeroInterpreter::initialize_stub() {
 55   if (_code != nullptr) return;
 56 
 57   // generate interpreter
 58   int code_size = InterpreterCodeSize;
 59   NOT_PRODUCT(code_size *= 4;)  // debug uses extra interpreter code space
 60   _code = new StubQueue(new InterpreterCodeletInterface, code_size, nullptr,
 61                          "Interpreter");
 62 }
 63 
 64 void ZeroInterpreter::initialize_code() {
 65   AbstractInterpreter::initialize();
 66 
 67   // generate interpreter
 68   { ResourceMark rm;
 69     TraceTime timer("Interpreter generation", TRACETIME_LOG(Info, startuptime));
 70     ZeroInterpreterGenerator g;
 71     if (PrintInterpreter) print();
 72   }
 73 }
 74 
 75 void ZeroInterpreter::invoke_method(Method* method, address entry_point, TRAPS) {
 76   ((ZeroEntry *) entry_point)->invoke(method, THREAD);
 77 }
 78 
 79 void ZeroInterpreter::invoke_osr(Method* method,
 80                                 address   entry_point,
 81                                 address   osr_buf,
 82                                 TRAPS) {
 83   ((ZeroEntry *) entry_point)->invoke_osr(method, osr_buf, THREAD);
 84 }
 85 
 86 
 87 
 88 InterpreterCodelet* ZeroInterpreter::codelet_containing(address pc) {
 89   // FIXME: I'm pretty sure _code is null and this is never called, which is why it's copied.
 90   return (InterpreterCodelet*)_code->stub_containing(pc);
 91 }
 92 #define fixup_after_potential_safepoint()       \
 93   method = istate->method()
 94 
 95 #define CALL_VM_NOCHECK_NOFIX(func)             \
 96   thread->set_last_Java_frame();                \
 97   func;                                         \
 98   thread->reset_last_Java_frame();
 99 
100 #define CALL_VM_NOCHECK(func)                   \
101   CALL_VM_NOCHECK_NOFIX(func)                   \
102   fixup_after_potential_safepoint()
103 
104 int ZeroInterpreter::normal_entry(Method* method, intptr_t UNUSED, TRAPS) {
105   JavaThread *thread = THREAD;
106 
107   // Allocate and initialize our frame.
108   InterpreterFrame *frame = InterpreterFrame::build(method, CHECK_0);
109   thread->push_zero_frame(frame);
110 
111   // Execute those bytecodes!
112   main_loop(0, THREAD);
113 
114   // No deoptimized frames on the stack
115   return 0;
116 }
117 
118 int ZeroInterpreter::Reference_get_entry(Method* method, intptr_t UNUSED, TRAPS) {
119   JavaThread* thread = THREAD;
120   ZeroStack* stack = thread->zero_stack();
121   intptr_t* topOfStack = stack->sp();
122 
123   oop ref = STACK_OBJECT(0);
124 
125   // Shortcut if reference is known null
126   if (ref == nullptr) {
127     return normal_entry(method, 0, THREAD);
128   }
129 
130   // Read the referent with weaker semantics, and let GCs handle the rest.
131   const int referent_offset = java_lang_ref_Reference::referent_offset();
132   oop obj = HeapAccess<IN_HEAP | ON_WEAK_OOP_REF>::oop_load_at(ref, referent_offset);
133 
134   SET_STACK_OBJECT(obj, 0);
135 
136   // No deoptimized frames on the stack
137   return 0;
138 }
139 
140 intptr_t narrow(BasicType type, intptr_t result) {
141   // mask integer result to narrower return type.
142   switch (type) {
143     case T_BOOLEAN:
144       return result&1;
145     case T_BYTE:
146       return (intptr_t)(jbyte)result;
147     case T_CHAR:
148       return (intptr_t)(uintptr_t)(jchar)result;
149     case T_SHORT:
150       return (intptr_t)(jshort)result;
151     case T_OBJECT:  // nothing to do fall through
152     case T_ARRAY:
153     case T_LONG:
154     case T_INT:
155     case T_FLOAT:
156     case T_DOUBLE:
157     case T_VOID:
158       return result;
159     default:
160       ShouldNotReachHere();
161       return result; // silence compiler warnings
162   }
163 }
164 
165 
166 void ZeroInterpreter::main_loop(int recurse, TRAPS) {
167   JavaThread *thread = THREAD;
168   ZeroStack *stack = thread->zero_stack();
169 
170   // If we are entering from a deopt we may need to call
171   // ourself a few times in order to get to our frame.
172   if (recurse)
173     main_loop(recurse - 1, THREAD);
174 
175   InterpreterFrame *frame = thread->top_zero_frame()->as_interpreter_frame();
176   interpreterState istate = frame->interpreter_state();
177   Method* method = istate->method();
178 
179   intptr_t *result = nullptr;
180   int result_slots = 0;
181 
182   while (true) {
183     // We can set up the frame anchor with everything we want at
184     // this point as we are thread_in_Java and no safepoints can
185     // occur until we go to vm mode.  We do have to clear flags
186     // on return from vm but that is it.
187     thread->set_last_Java_frame();
188 
189     // Call the interpreter
190     if (JvmtiExport::can_post_interpreter_events()) {
191       if (RewriteBytecodes) {
192         BytecodeInterpreter::run<true, true>(istate);
193       } else {
194         BytecodeInterpreter::run<true, false>(istate);
195       }
196     } else {
197       if (RewriteBytecodes) {
198         BytecodeInterpreter::run<false, true>(istate);
199       } else {
200         BytecodeInterpreter::run<false, false>(istate);
201       }
202     }
203     fixup_after_potential_safepoint();
204 
205     // If we are unwinding, notify the stack watermarks machinery.
206     // Should do this before resetting the frame anchor.
207     if (istate->msg() == BytecodeInterpreter::return_from_method ||
208         istate->msg() == BytecodeInterpreter::do_osr) {
209       stack_watermark_unwind_check(thread);
210     } else {
211       assert(istate->msg() == BytecodeInterpreter::call_method ||
212              istate->msg() == BytecodeInterpreter::more_monitors ||
213              istate->msg() == BytecodeInterpreter::throwing_exception,
214              "Should be one of these otherwise");
215     }
216 
217     // Clear the frame anchor
218     thread->reset_last_Java_frame();
219 
220     // Examine the message from the interpreter to decide what to do
221     if (istate->msg() == BytecodeInterpreter::call_method) {
222       Method* callee = istate->callee();
223 
224       // Trim back the stack to put the parameters at the top
225       stack->set_sp(istate->stack() + 1);
226 
227       // Make the call
228       Interpreter::invoke_method(callee, istate->callee_entry_point(), THREAD);
229       fixup_after_potential_safepoint();
230 
231       // Convert the result
232       istate->set_stack(stack->sp() - 1);
233 
234       // Restore the stack
235       stack->set_sp(istate->stack_limit() + 1);
236 
237       // Resume the interpreter
238       istate->set_msg(BytecodeInterpreter::method_resume);
239     }
240     else if (istate->msg() == BytecodeInterpreter::more_monitors) {
241       int monitor_words = frame::interpreter_frame_monitor_size();
242 
243       // Allocate the space
244       stack->overflow_check(monitor_words, THREAD);
245       if (HAS_PENDING_EXCEPTION)
246         break;
247       stack->alloc(monitor_words * wordSize);
248 
249       // Move the expression stack contents
250       for (intptr_t *p = istate->stack() + 1; p < istate->stack_base(); p++)
251         *(p - monitor_words) = *p;
252 
253       // Move the expression stack pointers
254       istate->set_stack_limit(istate->stack_limit() - monitor_words);
255       istate->set_stack(istate->stack() - monitor_words);
256       istate->set_stack_base(istate->stack_base() - monitor_words);
257 
258       // Zero the new monitor so the interpreter can find it.
259       ((BasicObjectLock *) istate->stack_base())->set_obj(nullptr);
260 
261       // Resume the interpreter
262       istate->set_msg(BytecodeInterpreter::got_monitors);
263     }
264     else if (istate->msg() == BytecodeInterpreter::return_from_method) {
265       // Copy the result into the caller's frame
266       result_slots = type2size[method->result_type()];
267       assert(result_slots >= 0 && result_slots <= 2, "what?");
268       result = istate->stack() + result_slots;
269       break;
270     }
271     else if (istate->msg() == BytecodeInterpreter::throwing_exception) {
272       assert(HAS_PENDING_EXCEPTION, "should do");
273       break;
274     }
275     else if (istate->msg() == BytecodeInterpreter::do_osr) {
276       // Unwind the current frame
277       thread->pop_zero_frame();
278 
279       // Remove any extension of the previous frame
280       int extra_locals = method->max_locals() - method->size_of_parameters();
281       stack->set_sp(stack->sp() + extra_locals);
282 
283       // Jump into the OSR method
284       Interpreter::invoke_osr(
285         method, istate->osr_entry(), istate->osr_buf(), THREAD);
286       return;
287     }
288     else {
289       ShouldNotReachHere();
290     }
291   }
292 
293   // Unwind the current frame
294   thread->pop_zero_frame();
295 
296   // Pop our local variables
297   stack->set_sp(stack->sp() + method->max_locals());
298 
299   // Push our result
300   for (int i = 0; i < result_slots; i++) {
301     // Adjust result to smaller
302     union {
303       intptr_t res;
304       jint res_jint;
305     };
306     res = result[-i];
307     if (result_slots == 1) {
308       BasicType t = method->result_type();
309       if (is_subword_type(t)) {
310         res_jint = (jint)narrow(t, res_jint);
311       }
312     }
313     stack->push(res);
314   }
315 }
316 
317 int ZeroInterpreter::native_entry(Method* method, intptr_t UNUSED, TRAPS) {
318   // Make sure method is native and not abstract
319   assert(method->is_native() && !method->is_abstract(), "should be");
320 
321   JavaThread *thread = THREAD;
322   ZeroStack *stack = thread->zero_stack();
323 
324   // Allocate and initialize our frame
325   InterpreterFrame *frame = InterpreterFrame::build(method, CHECK_0);
326   thread->push_zero_frame(frame);
327   interpreterState istate = frame->interpreter_state();
328   intptr_t *locals = istate->locals();
329 
330   // Lock if necessary
331   BasicObjectLock *monitor;
332   monitor = nullptr;
333   if (method->is_synchronized()) {
334     monitor = (BasicObjectLock*) istate->stack_base();
335     oop lockee = monitor->obj();
336     bool success = false;
337     if (LockingMode == LM_LEGACY) {
338       markWord disp = lockee->mark().set_unlocked();
339       monitor->lock()->set_displaced_header(disp);
340       success = true;
341       if (lockee->cas_set_mark(markWord::from_pointer(monitor), disp) != disp) {
342         // Is it simple recursive case?
343         if (thread->is_lock_owned((address) disp.clear_lock_bits().to_pointer())) {
344           monitor->lock()->set_displaced_header(markWord::from_pointer(nullptr));
345         } else {
346           success = false;
347         }
348       }
349       if (success) {
350         THREAD->inc_held_monitor_count();
351       }
352     }
353     if (!success) {
354       CALL_VM_NOCHECK(InterpreterRuntime::monitorenter(thread, monitor));
355           if (HAS_PENDING_EXCEPTION)
356             goto unwind_and_return;
357     }
358   }
359 
360   // Get the signature handler
361   InterpreterRuntime::SignatureHandler *handler; {
362     address handlerAddr = method->signature_handler();
363     if (handlerAddr == nullptr) {
364       CALL_VM_NOCHECK(InterpreterRuntime::prepare_native_call(thread, method));
365       if (HAS_PENDING_EXCEPTION)
366         goto unlock_unwind_and_return;
367 
368       handlerAddr = method->signature_handler();
369       assert(handlerAddr != nullptr, "eh?");
370     }
371     if (handlerAddr == (address) InterpreterRuntime::slow_signature_handler) {
372       CALL_VM_NOCHECK(handlerAddr =
373         InterpreterRuntime::slow_signature_handler(thread, method, nullptr,nullptr));
374       if (HAS_PENDING_EXCEPTION)
375         goto unlock_unwind_and_return;
376     }
377     handler = \
378       InterpreterRuntime::SignatureHandler::from_handlerAddr(handlerAddr);
379   }
380 
381   // Get the native function entry point
382   address function;
383   function = method->native_function();
384   assert(function != nullptr, "should be set if signature handler is");
385 
386   // Build the argument list
387   stack->overflow_check(handler->argument_count() * 2, THREAD);
388   if (HAS_PENDING_EXCEPTION)
389     goto unlock_unwind_and_return;
390 
391   void **arguments;
392   void *mirror; {
393     arguments =
394       (void **) stack->alloc(handler->argument_count() * sizeof(void **));
395     void **dst = arguments;
396 
397     void *env = thread->jni_environment();
398     *(dst++) = &env;
399 
400     if (method->is_static()) {
401       istate->set_oop_temp(
402         method->constants()->pool_holder()->java_mirror());
403       mirror = istate->oop_temp_addr();
404       *(dst++) = &mirror;
405     }
406 
407     intptr_t *src = locals;
408     for (int i = dst - arguments; i < handler->argument_count(); i++) {
409       ffi_type *type = handler->argument_type(i);
410       if (type == &ffi_type_pointer) {
411         if (*src) {
412           stack->push((intptr_t) src);
413           *(dst++) = stack->sp();
414         }
415         else {
416           *(dst++) = src;
417         }
418         src--;
419       }
420       else if (type->size == 4) {
421         *(dst++) = src--;
422       }
423       else if (type->size == 8) {
424         src--;
425         *(dst++) = src--;
426       }
427       else {
428         ShouldNotReachHere();
429       }
430     }
431   }
432 
433   // Set up the Java frame anchor
434   thread->set_last_Java_frame();
435 
436   // Change the thread state to _thread_in_native
437   ThreadStateTransition::transition_from_java(thread, _thread_in_native);
438 
439   // Make the call
440   intptr_t result[4 - LogBytesPerWord];
441   ffi_call(handler->cif(), (void (*)()) function, result, arguments);
442 
443   // Change the thread state back to _thread_in_Java and ensure it
444   // is seen by the GC thread.
445   // ThreadStateTransition::transition_from_native() cannot be used
446   // here because it does not check for asynchronous exceptions.
447   // We have to manage the transition ourself.
448   thread->set_thread_state_fence(_thread_in_native_trans);
449 
450   // Handle safepoint operations, pending suspend requests,
451   // and pending asynchronous exceptions.
452   if (SafepointMechanism::should_process(thread) ||
453       thread->has_special_condition_for_native_trans()) {
454     JavaThread::check_special_condition_for_native_trans(thread);
455     CHECK_UNHANDLED_OOPS_ONLY(thread->clear_unhandled_oops());
456   }
457 
458   // Finally we can change the thread state to _thread_in_Java.
459   thread->set_thread_state(_thread_in_Java);
460   fixup_after_potential_safepoint();
461 
462   // Notify the stack watermarks machinery that we are unwinding.
463   // Should do this before resetting the frame anchor.
464   stack_watermark_unwind_check(thread);
465 
466   // Clear the frame anchor
467   thread->reset_last_Java_frame();
468 
469   // If the result was an oop then unbox it and store it in
470   // oop_temp where the garbage collector can see it before
471   // we release the handle it might be protected by.
472   if (handler->result_type() == &ffi_type_pointer) {
473     if (result[0] == 0) {
474       istate->set_oop_temp(nullptr);
475     } else {
476       jobject handle = reinterpret_cast<jobject>(result[0]);
477       istate->set_oop_temp(JNIHandles::resolve(handle));
478     }
479   }
480 
481   // Reset handle block
482   thread->active_handles()->clear();
483 
484  unlock_unwind_and_return:
485 
486   // Unlock if necessary
487   if (monitor) {
488     BasicLock *lock = monitor->lock();
489     markWord header = lock->displaced_header();
490     oop rcvr = monitor->obj();
491     monitor->set_obj(nullptr);
492 
493     bool dec_monitor_count = true;
494     if (header.to_pointer() != nullptr) {
495       markWord old_header = markWord::encode(lock);
496       if (rcvr->cas_set_mark(header, old_header) != old_header) {
497         monitor->set_obj(rcvr);
498         dec_monitor_count = false;
499         InterpreterRuntime::monitorexit(monitor);
500       }
501     }
502     if (dec_monitor_count) {
503       THREAD->dec_held_monitor_count();
504     }
505   }
506 
507  unwind_and_return:
508 
509   // Unwind the current activation
510   thread->pop_zero_frame();
511 
512   // Pop our parameters
513   stack->set_sp(stack->sp() + method->size_of_parameters());
514 
515   // Push our result
516   if (!HAS_PENDING_EXCEPTION) {
517     BasicType type = method->result_type();
518     stack->set_sp(stack->sp() - type2size[type]);
519 
520     switch (type) {
521     case T_VOID:
522       break;
523 
524     case T_BOOLEAN:
525 #ifndef VM_LITTLE_ENDIAN
526       result[0] <<= (BitsPerWord - BitsPerByte);
527 #endif
528       SET_LOCALS_INT(*(jboolean *) result != 0, 0);
529       break;
530 
531     case T_CHAR:
532 #ifndef VM_LITTLE_ENDIAN
533       result[0] <<= (BitsPerWord - BitsPerShort);
534 #endif
535       SET_LOCALS_INT(*(jchar *) result, 0);
536       break;
537 
538     case T_BYTE:
539 #ifndef VM_LITTLE_ENDIAN
540       result[0] <<= (BitsPerWord - BitsPerByte);
541 #endif
542       SET_LOCALS_INT(*(jbyte *) result, 0);
543       break;
544 
545     case T_SHORT:
546 #ifndef VM_LITTLE_ENDIAN
547       result[0] <<= (BitsPerWord - BitsPerShort);
548 #endif
549       SET_LOCALS_INT(*(jshort *) result, 0);
550       break;
551 
552     case T_INT:
553 #ifndef VM_LITTLE_ENDIAN
554       result[0] <<= (BitsPerWord - BitsPerInt);
555 #endif
556       SET_LOCALS_INT(*(jint *) result, 0);
557       break;
558 
559     case T_LONG:
560       SET_LOCALS_LONG(*(jlong *) result, 0);
561       break;
562 
563     case T_FLOAT:
564       SET_LOCALS_FLOAT(*(jfloat *) result, 0);
565       break;
566 
567     case T_DOUBLE:
568       SET_LOCALS_DOUBLE(*(jdouble *) result, 0);
569       break;
570 
571     case T_OBJECT:
572     case T_ARRAY:
573       SET_LOCALS_OBJECT(istate->oop_temp(), 0);
574       break;
575 
576     default:
577       ShouldNotReachHere();
578     }
579   }
580 
581   // Already did every pending exception check here.
582   // If HAS_PENDING_EXCEPTION is true, the interpreter would handle the rest.
583   if (CheckJNICalls) {
584     THREAD->clear_pending_jni_exception_check();
585   }
586 
587   // No deoptimized frames on the stack
588   return 0;
589 }
590 
591 int ZeroInterpreter::getter_entry(Method* method, intptr_t UNUSED, TRAPS) {
592   JavaThread* thread = THREAD;
593   // Drop into the slow path if we need a safepoint check
594   if (SafepointMechanism::should_process(thread)) {
595     return normal_entry(method, 0, THREAD);
596   }
597 
598   // Read the field index from the bytecode:
599   //  0:  aload_0
600   //  1:  getfield
601   //  2:    index
602   //  3:    index
603   //  4:  return
604   //
605   // NB this is not raw bytecode: index is in machine order
606 
607   assert(method->is_getter(), "Expect the particular bytecode shape");
608   u1* code = method->code_base();
609   u2 index = Bytes::get_native_u2(&code[2]);
610 
611   // Get the entry from the constant pool cache, and drop into
612   // the slow path if it has not been resolved
613   ConstantPoolCache* cache = method->constants()->cache();
614   ResolvedFieldEntry* entry = cache->resolved_field_entry_at(index);
615   if (!entry->is_resolved(Bytecodes::_getfield)) {
616     return normal_entry(method, 0, THREAD);
617   }
618 
619   ZeroStack* stack = thread->zero_stack();
620   intptr_t* topOfStack = stack->sp();
621 
622   // Load the object pointer and drop into the slow path
623   // if we have a NullPointerException
624   oop object = STACK_OBJECT(0);
625   if (object == nullptr) {
626     return normal_entry(method, 0, THREAD);
627   }
628 
629   // If needed, allocate additional slot on stack: we already have one
630   // for receiver, and double/long need another one.
631   switch (entry->tos_state()) {
632     case ltos:
633     case dtos:
634       stack->overflow_check(1, CHECK_0);
635       stack->alloc(wordSize);
636       topOfStack = stack->sp();
637       break;
638     default:
639       ;
640   }
641 
642   // Read the field to stack(0)
643   int offset = entry->field_offset();
644   if (entry->is_volatile()) {
645     if (support_IRIW_for_not_multiple_copy_atomic_cpu) {
646       OrderAccess::fence();
647     }
648     switch (entry->tos_state()) {
649       case btos:
650       case ztos: SET_STACK_INT(object->byte_field_acquire(offset),      0); break;
651       case ctos: SET_STACK_INT(object->char_field_acquire(offset),      0); break;
652       case stos: SET_STACK_INT(object->short_field_acquire(offset),     0); break;
653       case itos: SET_STACK_INT(object->int_field_acquire(offset),       0); break;
654       case ltos: SET_STACK_LONG(object->long_field_acquire(offset),     0); break;
655       case ftos: SET_STACK_FLOAT(object->float_field_acquire(offset),   0); break;
656       case dtos: SET_STACK_DOUBLE(object->double_field_acquire(offset), 0); break;
657       case atos: SET_STACK_OBJECT(object->obj_field_acquire(offset),    0); break;
658       default:
659         ShouldNotReachHere();
660     }
661   } else {
662     switch (entry->tos_state()) {
663       case btos:
664       case ztos: SET_STACK_INT(object->byte_field(offset),      0); break;
665       case ctos: SET_STACK_INT(object->char_field(offset),      0); break;
666       case stos: SET_STACK_INT(object->short_field(offset),     0); break;
667       case itos: SET_STACK_INT(object->int_field(offset),       0); break;
668       case ltos: SET_STACK_LONG(object->long_field(offset),     0); break;
669       case ftos: SET_STACK_FLOAT(object->float_field(offset),   0); break;
670       case dtos: SET_STACK_DOUBLE(object->double_field(offset), 0); break;
671       case atos: SET_STACK_OBJECT(object->obj_field(offset),    0); break;
672       default:
673         ShouldNotReachHere();
674     }
675   }
676 
677   // No deoptimized frames on the stack
678   return 0;
679 }
680 
681 int ZeroInterpreter::setter_entry(Method* method, intptr_t UNUSED, TRAPS) {
682   JavaThread* thread = THREAD;
683   // Drop into the slow path if we need a safepoint check
684   if (SafepointMechanism::should_process(thread)) {
685     return normal_entry(method, 0, THREAD);
686   }
687 
688   // Read the field index from the bytecode:
689   //  0:  aload_0
690   //  1:  *load_1
691   //  2:  putfield
692   //  3:    index
693   //  4:    index
694   //  5:  return
695   //
696   // NB this is not raw bytecode: index is in machine order
697 
698   assert(method->is_setter(), "Expect the particular bytecode shape");
699   u1* code = method->code_base();
700   u2 index = Bytes::get_native_u2(&code[3]);
701 
702   // Get the entry from the constant pool cache, and drop into
703   // the slow path if it has not been resolved
704   ConstantPoolCache* cache = method->constants()->cache();
705   ResolvedFieldEntry* entry = cache->resolved_field_entry_at(index);
706   if (!entry->is_resolved(Bytecodes::_putfield)) {
707     return normal_entry(method, 0, THREAD);
708   }
709 
710   ZeroStack* stack = thread->zero_stack();
711   intptr_t* topOfStack = stack->sp();
712 
713   // Figure out where the receiver is. If there is a long/double
714   // operand on stack top, then receiver is two slots down.
715   oop object = nullptr;
716   switch (entry->tos_state()) {
717     case ltos:
718     case dtos:
719       object = STACK_OBJECT(-2);
720       break;
721     default:
722       object = STACK_OBJECT(-1);
723       break;
724   }
725 
726   // Load the receiver pointer and drop into the slow path
727   // if we have a NullPointerException
728   if (object == nullptr) {
729     return normal_entry(method, 0, THREAD);
730   }
731 
732   // Store the stack(0) to field
733   int offset = entry->field_offset();
734   if (entry->is_volatile()) {
735     switch (entry->tos_state()) {
736       case btos: object->release_byte_field_put(offset,   STACK_INT(0));     break;
737       case ztos: object->release_byte_field_put(offset,   STACK_INT(0) & 1); break; // only store LSB
738       case ctos: object->release_char_field_put(offset,   STACK_INT(0));     break;
739       case stos: object->release_short_field_put(offset,  STACK_INT(0));     break;
740       case itos: object->release_int_field_put(offset,    STACK_INT(0));     break;
741       case ltos: object->release_long_field_put(offset,   STACK_LONG(0));    break;
742       case ftos: object->release_float_field_put(offset,  STACK_FLOAT(0));   break;
743       case dtos: object->release_double_field_put(offset, STACK_DOUBLE(0));  break;
744       case atos: object->release_obj_field_put(offset,    STACK_OBJECT(0));  break;
745       default:
746         ShouldNotReachHere();
747     }
748     OrderAccess::storeload();
749   } else {
750     switch (entry->tos_state()) {
751       case btos: object->byte_field_put(offset,   STACK_INT(0));     break;
752       case ztos: object->byte_field_put(offset,   STACK_INT(0) & 1); break; // only store LSB
753       case ctos: object->char_field_put(offset,   STACK_INT(0));     break;
754       case stos: object->short_field_put(offset,  STACK_INT(0));     break;
755       case itos: object->int_field_put(offset,    STACK_INT(0));     break;
756       case ltos: object->long_field_put(offset,   STACK_LONG(0));    break;
757       case ftos: object->float_field_put(offset,  STACK_FLOAT(0));   break;
758       case dtos: object->double_field_put(offset, STACK_DOUBLE(0));  break;
759       case atos: object->obj_field_put(offset,    STACK_OBJECT(0));  break;
760       default:
761         ShouldNotReachHere();
762     }
763   }
764 
765   // Nothing is returned, pop out parameters
766   stack->set_sp(stack->sp() + method->size_of_parameters());
767 
768   // No deoptimized frames on the stack
769   return 0;
770 }
771 
772 int ZeroInterpreter::empty_entry(Method* method, intptr_t UNUSED, TRAPS) {
773   JavaThread *thread = THREAD;
774   ZeroStack *stack = thread->zero_stack();
775 
776   // Drop into the slow path if we need a safepoint check
777   if (SafepointMechanism::should_process(thread)) {
778     return normal_entry(method, 0, THREAD);
779   }
780 
781   // Pop our parameters
782   stack->set_sp(stack->sp() + method->size_of_parameters());
783 
784   // No deoptimized frames on the stack
785   return 0;
786 }
787 
788 InterpreterFrame *InterpreterFrame::build(Method* const method, TRAPS) {
789   JavaThread *thread = THREAD;
790   ZeroStack *stack = thread->zero_stack();
791 
792   // Calculate the size of the frame we'll build, including
793   // any adjustments to the caller's frame that we'll make.
794   int extra_locals  = 0;
795   int monitor_words = 0;
796   int stack_words   = 0;
797 
798   if (!method->is_native()) {
799     extra_locals = method->max_locals() - method->size_of_parameters();
800     stack_words  = method->max_stack();
801   }
802   if (method->is_synchronized()) {
803     monitor_words = frame::interpreter_frame_monitor_size();
804   }
805   stack->overflow_check(
806     extra_locals + header_words + monitor_words + stack_words, CHECK_NULL);
807 
808   // Adjust the caller's stack frame to accommodate any additional
809   // local variables we have contiguously with our parameters.
810   for (int i = 0; i < extra_locals; i++)
811     stack->push(0);
812 
813   intptr_t *locals;
814   if (method->is_native())
815     locals = stack->sp() + (method->size_of_parameters() - 1);
816   else
817     locals = stack->sp() + (method->max_locals() - 1);
818 
819   stack->push(0); // next_frame, filled in later
820   intptr_t *fp = stack->sp();
821   assert(fp - stack->sp() == next_frame_off, "should be");
822 
823   stack->push(INTERPRETER_FRAME);
824   assert(fp - stack->sp() == frame_type_off, "should be");
825 
826   interpreterState istate =
827     (interpreterState) stack->alloc(sizeof(BytecodeInterpreter));
828   assert(fp - stack->sp() == istate_off, "should be");
829 
830   istate->set_locals(locals);
831   istate->set_method(method);
832   istate->set_mirror(method->method_holder()->java_mirror());
833   istate->set_self_link(istate);
834   istate->set_prev_link(nullptr);
835   istate->set_thread(thread);
836   istate->set_bcp(method->is_native() ? nullptr : method->code_base());
837   istate->set_constants(method->constants()->cache());
838   istate->set_msg(BytecodeInterpreter::method_entry);
839   istate->set_oop_temp(nullptr);
840   istate->set_callee(nullptr);
841 
842   istate->set_monitor_base((BasicObjectLock *) stack->sp());
843   if (method->is_synchronized()) {
844     BasicObjectLock *monitor =
845       (BasicObjectLock *) stack->alloc(monitor_words * wordSize);
846     oop object;
847     if (method->is_static())
848       object = method->constants()->pool_holder()->java_mirror();
849     else
850       object = cast_to_oop((void*)locals[0]);
851     monitor->set_obj(object);
852   }
853 
854   istate->set_stack_base(stack->sp());
855   istate->set_stack(stack->sp() - 1);
856   if (stack_words)
857     stack->alloc(stack_words * wordSize);
858   istate->set_stack_limit(stack->sp() - 1);
859 
860   return (InterpreterFrame *) fp;
861 }
862 
863 InterpreterFrame *InterpreterFrame::build(int size, TRAPS) {
864   ZeroStack *stack = THREAD->zero_stack();
865 
866   int size_in_words = size >> LogBytesPerWord;
867   assert(size_in_words * wordSize == size, "unaligned");
868   assert(size_in_words >= header_words, "too small");
869   stack->overflow_check(size_in_words, CHECK_NULL);
870 
871   stack->push(0); // next_frame, filled in later
872   intptr_t *fp = stack->sp();
873   assert(fp - stack->sp() == next_frame_off, "should be");
874 
875   stack->push(INTERPRETER_FRAME);
876   assert(fp - stack->sp() == frame_type_off, "should be");
877 
878   interpreterState istate =
879     (interpreterState) stack->alloc(sizeof(BytecodeInterpreter));
880   assert(fp - stack->sp() == istate_off, "should be");
881   istate->set_self_link(nullptr); // mark invalid
882 
883   stack->alloc((size_in_words - header_words) * wordSize);
884 
885   return (InterpreterFrame *) fp;
886 }
887 
888 address ZeroInterpreter::return_entry(TosState state, int length, Bytecodes::Code code) {
889   ShouldNotCallThis();
890   return nullptr;
891 }
892 
893 address ZeroInterpreter::deopt_entry(TosState state, int length) {
894   return nullptr;
895 }
896 
897 address ZeroInterpreter::remove_activation_preserving_args_entry() {
898   // Do an uncommon trap type entry. c++ interpreter will know
899   // to pop frame and preserve the args
900   return Interpreter::deopt_entry(vtos, 0);
901 }
902 
903 address ZeroInterpreter::remove_activation_early_entry(TosState state) {
904   return nullptr;
905 }
906 
907 // Helper for figuring out if frames are interpreter frames
908 
909 bool ZeroInterpreter::contains(address pc) {
910   return false; // make frame::print_value_on work
911 }
912 
913 void ZeroInterpreter::stack_watermark_unwind_check(JavaThread* thread) {
914   // If frame pointer is in the danger zone, notify the runtime that
915   // it needs to act before continuing the unwinding.
916   uintptr_t fp = (uintptr_t)thread->last_Java_fp();
917   uintptr_t watermark = thread->poll_data()->get_polling_word();
918   if (fp > watermark) {
919     InterpreterRuntime::at_unwind(thread);
920   }
921 }