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