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     bool success = false;
489     if (LockingMode == LM_LEGACY) {
490       BasicLock* lock = monitor->lock();
491       oop rcvr = monitor->obj();
492       monitor->set_obj(nullptr);
493       success = true;
494       markWord header = lock->displaced_header();
495       if (header.to_pointer() != nullptr) { // Check for recursive lock
496         markWord old_header = markWord::encode(lock);
497         if (rcvr->cas_set_mark(header, old_header) != old_header) {
498           monitor->set_obj(rcvr);
499           success = false;
500         }
501       }
502       if (success) {
503         THREAD->dec_held_monitor_count();
504       }
505     }
506     if (!success) {
507       InterpreterRuntime::monitorexit(monitor);
508     }
509   }
510 
511   unwind_and_return:
512 
513   // Unwind the current activation
514   thread->pop_zero_frame();
515 
516   // Pop our parameters
517   stack->set_sp(stack->sp() + method->size_of_parameters());
518 
519   // Push our result
520   if (!HAS_PENDING_EXCEPTION) {
521     BasicType type = method->result_type();
522     stack->set_sp(stack->sp() - type2size[type]);
523 
524     switch (type) {
525     case T_VOID:
526       break;
527 
528     case T_BOOLEAN:
529 #ifndef VM_LITTLE_ENDIAN
530       result[0] <<= (BitsPerWord - BitsPerByte);
531 #endif
532       SET_LOCALS_INT(*(jboolean *) result != 0, 0);
533       break;
534 
535     case T_CHAR:
536 #ifndef VM_LITTLE_ENDIAN
537       result[0] <<= (BitsPerWord - BitsPerShort);
538 #endif
539       SET_LOCALS_INT(*(jchar *) result, 0);
540       break;
541 
542     case T_BYTE:
543 #ifndef VM_LITTLE_ENDIAN
544       result[0] <<= (BitsPerWord - BitsPerByte);
545 #endif
546       SET_LOCALS_INT(*(jbyte *) result, 0);
547       break;
548 
549     case T_SHORT:
550 #ifndef VM_LITTLE_ENDIAN
551       result[0] <<= (BitsPerWord - BitsPerShort);
552 #endif
553       SET_LOCALS_INT(*(jshort *) result, 0);
554       break;
555 
556     case T_INT:
557 #ifndef VM_LITTLE_ENDIAN
558       result[0] <<= (BitsPerWord - BitsPerInt);
559 #endif
560       SET_LOCALS_INT(*(jint *) result, 0);
561       break;
562 
563     case T_LONG:
564       SET_LOCALS_LONG(*(jlong *) result, 0);
565       break;
566 
567     case T_FLOAT:
568       SET_LOCALS_FLOAT(*(jfloat *) result, 0);
569       break;
570 
571     case T_DOUBLE:
572       SET_LOCALS_DOUBLE(*(jdouble *) result, 0);
573       break;
574 
575     case T_OBJECT:
576     case T_ARRAY:
577       SET_LOCALS_OBJECT(istate->oop_temp(), 0);
578       break;
579 
580     default:
581       ShouldNotReachHere();
582     }
583   }
584 
585   // Already did every pending exception check here.
586   // If HAS_PENDING_EXCEPTION is true, the interpreter would handle the rest.
587   if (CheckJNICalls) {
588     THREAD->clear_pending_jni_exception_check();
589   }
590 
591   // No deoptimized frames on the stack
592   return 0;
593 }
594 
595 int ZeroInterpreter::getter_entry(Method* method, intptr_t UNUSED, TRAPS) {
596   JavaThread* thread = THREAD;
597   // Drop into the slow path if we need a safepoint check
598   if (SafepointMechanism::should_process(thread)) {
599     return normal_entry(method, 0, THREAD);
600   }
601 
602   // Read the field index from the bytecode:
603   //  0:  aload_0
604   //  1:  getfield
605   //  2:    index
606   //  3:    index
607   //  4:  return
608   //
609   // NB this is not raw bytecode: index is in machine order
610 
611   assert(method->is_getter(), "Expect the particular bytecode shape");
612   u1* code = method->code_base();
613   u2 index = Bytes::get_native_u2(&code[2]);
614 
615   // Get the entry from the constant pool cache, and drop into
616   // the slow path if it has not been resolved
617   ConstantPoolCache* cache = method->constants()->cache();
618   ResolvedFieldEntry* entry = cache->resolved_field_entry_at(index);
619   if (!entry->is_resolved(Bytecodes::_getfield)) {
620     return normal_entry(method, 0, THREAD);
621   }
622 
623   ZeroStack* stack = thread->zero_stack();
624   intptr_t* topOfStack = stack->sp();
625 
626   // Load the object pointer and drop into the slow path
627   // if we have a NullPointerException
628   oop object = STACK_OBJECT(0);
629   if (object == nullptr) {
630     return normal_entry(method, 0, THREAD);
631   }
632 
633   // If needed, allocate additional slot on stack: we already have one
634   // for receiver, and double/long need another one.
635   switch (entry->tos_state()) {
636     case ltos:
637     case dtos:
638       stack->overflow_check(1, CHECK_0);
639       stack->alloc(wordSize);
640       topOfStack = stack->sp();
641       break;
642     default:
643       ;
644   }
645 
646   // Read the field to stack(0)
647   int offset = entry->field_offset();
648   if (entry->is_volatile()) {
649     if (support_IRIW_for_not_multiple_copy_atomic_cpu) {
650       OrderAccess::fence();
651     }
652     switch (entry->tos_state()) {
653       case btos:
654       case ztos: SET_STACK_INT(object->byte_field_acquire(offset),      0); break;
655       case ctos: SET_STACK_INT(object->char_field_acquire(offset),      0); break;
656       case stos: SET_STACK_INT(object->short_field_acquire(offset),     0); break;
657       case itos: SET_STACK_INT(object->int_field_acquire(offset),       0); break;
658       case ltos: SET_STACK_LONG(object->long_field_acquire(offset),     0); break;
659       case ftos: SET_STACK_FLOAT(object->float_field_acquire(offset),   0); break;
660       case dtos: SET_STACK_DOUBLE(object->double_field_acquire(offset), 0); break;
661       case atos: SET_STACK_OBJECT(object->obj_field_acquire(offset),    0); break;
662       default:
663         ShouldNotReachHere();
664     }
665   } else {
666     switch (entry->tos_state()) {
667       case btos:
668       case ztos: SET_STACK_INT(object->byte_field(offset),      0); break;
669       case ctos: SET_STACK_INT(object->char_field(offset),      0); break;
670       case stos: SET_STACK_INT(object->short_field(offset),     0); break;
671       case itos: SET_STACK_INT(object->int_field(offset),       0); break;
672       case ltos: SET_STACK_LONG(object->long_field(offset),     0); break;
673       case ftos: SET_STACK_FLOAT(object->float_field(offset),   0); break;
674       case dtos: SET_STACK_DOUBLE(object->double_field(offset), 0); break;
675       case atos: SET_STACK_OBJECT(object->obj_field(offset),    0); break;
676       default:
677         ShouldNotReachHere();
678     }
679   }
680 
681   // No deoptimized frames on the stack
682   return 0;
683 }
684 
685 int ZeroInterpreter::setter_entry(Method* method, intptr_t UNUSED, TRAPS) {
686   JavaThread* thread = THREAD;
687   // Drop into the slow path if we need a safepoint check
688   if (SafepointMechanism::should_process(thread)) {
689     return normal_entry(method, 0, THREAD);
690   }
691 
692   // Read the field index from the bytecode:
693   //  0:  aload_0
694   //  1:  *load_1
695   //  2:  putfield
696   //  3:    index
697   //  4:    index
698   //  5:  return
699   //
700   // NB this is not raw bytecode: index is in machine order
701 
702   assert(method->is_setter(), "Expect the particular bytecode shape");
703   u1* code = method->code_base();
704   u2 index = Bytes::get_native_u2(&code[3]);
705 
706   // Get the entry from the constant pool cache, and drop into
707   // the slow path if it has not been resolved
708   ConstantPoolCache* cache = method->constants()->cache();
709   ResolvedFieldEntry* entry = cache->resolved_field_entry_at(index);
710   if (!entry->is_resolved(Bytecodes::_putfield)) {
711     return normal_entry(method, 0, THREAD);
712   }
713 
714   ZeroStack* stack = thread->zero_stack();
715   intptr_t* topOfStack = stack->sp();
716 
717   // Figure out where the receiver is. If there is a long/double
718   // operand on stack top, then receiver is two slots down.
719   oop object = nullptr;
720   switch (entry->tos_state()) {
721     case ltos:
722     case dtos:
723       object = STACK_OBJECT(-2);
724       break;
725     default:
726       object = STACK_OBJECT(-1);
727       break;
728   }
729 
730   // Load the receiver pointer and drop into the slow path
731   // if we have a NullPointerException
732   if (object == nullptr) {
733     return normal_entry(method, 0, THREAD);
734   }
735 
736   // Store the stack(0) to field
737   int offset = entry->field_offset();
738   if (entry->is_volatile()) {
739     switch (entry->tos_state()) {
740       case btos: object->release_byte_field_put(offset,   STACK_INT(0));     break;
741       case ztos: object->release_byte_field_put(offset,   STACK_INT(0) & 1); break; // only store LSB
742       case ctos: object->release_char_field_put(offset,   STACK_INT(0));     break;
743       case stos: object->release_short_field_put(offset,  STACK_INT(0));     break;
744       case itos: object->release_int_field_put(offset,    STACK_INT(0));     break;
745       case ltos: object->release_long_field_put(offset,   STACK_LONG(0));    break;
746       case ftos: object->release_float_field_put(offset,  STACK_FLOAT(0));   break;
747       case dtos: object->release_double_field_put(offset, STACK_DOUBLE(0));  break;
748       case atos: object->release_obj_field_put(offset,    STACK_OBJECT(0));  break;
749       default:
750         ShouldNotReachHere();
751     }
752     OrderAccess::storeload();
753   } else {
754     switch (entry->tos_state()) {
755       case btos: object->byte_field_put(offset,   STACK_INT(0));     break;
756       case ztos: object->byte_field_put(offset,   STACK_INT(0) & 1); break; // only store LSB
757       case ctos: object->char_field_put(offset,   STACK_INT(0));     break;
758       case stos: object->short_field_put(offset,  STACK_INT(0));     break;
759       case itos: object->int_field_put(offset,    STACK_INT(0));     break;
760       case ltos: object->long_field_put(offset,   STACK_LONG(0));    break;
761       case ftos: object->float_field_put(offset,  STACK_FLOAT(0));   break;
762       case dtos: object->double_field_put(offset, STACK_DOUBLE(0));  break;
763       case atos: object->obj_field_put(offset,    STACK_OBJECT(0));  break;
764       default:
765         ShouldNotReachHere();
766     }
767   }
768 
769   // Nothing is returned, pop out parameters
770   stack->set_sp(stack->sp() + method->size_of_parameters());
771 
772   // No deoptimized frames on the stack
773   return 0;
774 }
775 
776 int ZeroInterpreter::empty_entry(Method* method, intptr_t UNUSED, TRAPS) {
777   JavaThread *thread = THREAD;
778   ZeroStack *stack = thread->zero_stack();
779 
780   // Drop into the slow path if we need a safepoint check
781   if (SafepointMechanism::should_process(thread)) {
782     return normal_entry(method, 0, THREAD);
783   }
784 
785   // Pop our parameters
786   stack->set_sp(stack->sp() + method->size_of_parameters());
787 
788   // No deoptimized frames on the stack
789   return 0;
790 }
791 
792 InterpreterFrame *InterpreterFrame::build(Method* const method, TRAPS) {
793   JavaThread *thread = THREAD;
794   ZeroStack *stack = thread->zero_stack();
795 
796   // Calculate the size of the frame we'll build, including
797   // any adjustments to the caller's frame that we'll make.
798   int extra_locals  = 0;
799   int monitor_words = 0;
800   int stack_words   = 0;
801 
802   if (!method->is_native()) {
803     extra_locals = method->max_locals() - method->size_of_parameters();
804     stack_words  = method->max_stack();
805   }
806   if (method->is_synchronized()) {
807     monitor_words = frame::interpreter_frame_monitor_size();
808   }
809   stack->overflow_check(
810     extra_locals + header_words + monitor_words + stack_words, CHECK_NULL);
811 
812   // Adjust the caller's stack frame to accommodate any additional
813   // local variables we have contiguously with our parameters.
814   for (int i = 0; i < extra_locals; i++)
815     stack->push(0);
816 
817   intptr_t *locals;
818   if (method->is_native())
819     locals = stack->sp() + (method->size_of_parameters() - 1);
820   else
821     locals = stack->sp() + (method->max_locals() - 1);
822 
823   stack->push(0); // next_frame, filled in later
824   intptr_t *fp = stack->sp();
825   assert(fp - stack->sp() == next_frame_off, "should be");
826 
827   stack->push(INTERPRETER_FRAME);
828   assert(fp - stack->sp() == frame_type_off, "should be");
829 
830   interpreterState istate =
831     (interpreterState) stack->alloc(sizeof(BytecodeInterpreter));
832   assert(fp - stack->sp() == istate_off, "should be");
833 
834   istate->set_locals(locals);
835   istate->set_method(method);
836   istate->set_mirror(method->method_holder()->java_mirror());
837   istate->set_self_link(istate);
838   istate->set_prev_link(nullptr);
839   istate->set_thread(thread);
840   istate->set_bcp(method->is_native() ? nullptr : method->code_base());
841   istate->set_constants(method->constants()->cache());
842   istate->set_msg(BytecodeInterpreter::method_entry);
843   istate->set_oop_temp(nullptr);
844   istate->set_callee(nullptr);
845 
846   istate->set_monitor_base((BasicObjectLock *) stack->sp());
847   if (method->is_synchronized()) {
848     BasicObjectLock *monitor =
849       (BasicObjectLock *) stack->alloc(monitor_words * wordSize);
850     oop object;
851     if (method->is_static())
852       object = method->constants()->pool_holder()->java_mirror();
853     else
854       object = cast_to_oop((void*)locals[0]);
855     monitor->set_obj(object);
856   }
857 
858   istate->set_stack_base(stack->sp());
859   istate->set_stack(stack->sp() - 1);
860   if (stack_words)
861     stack->alloc(stack_words * wordSize);
862   istate->set_stack_limit(stack->sp() - 1);
863 
864   return (InterpreterFrame *) fp;
865 }
866 
867 InterpreterFrame *InterpreterFrame::build(int size, TRAPS) {
868   ZeroStack *stack = THREAD->zero_stack();
869 
870   int size_in_words = size >> LogBytesPerWord;
871   assert(size_in_words * wordSize == size, "unaligned");
872   assert(size_in_words >= header_words, "too small");
873   stack->overflow_check(size_in_words, CHECK_NULL);
874 
875   stack->push(0); // next_frame, filled in later
876   intptr_t *fp = stack->sp();
877   assert(fp - stack->sp() == next_frame_off, "should be");
878 
879   stack->push(INTERPRETER_FRAME);
880   assert(fp - stack->sp() == frame_type_off, "should be");
881 
882   interpreterState istate =
883     (interpreterState) stack->alloc(sizeof(BytecodeInterpreter));
884   assert(fp - stack->sp() == istate_off, "should be");
885   istate->set_self_link(nullptr); // mark invalid
886 
887   stack->alloc((size_in_words - header_words) * wordSize);
888 
889   return (InterpreterFrame *) fp;
890 }
891 
892 address ZeroInterpreter::return_entry(TosState state, int length, Bytecodes::Code code) {
893   ShouldNotCallThis();
894   return nullptr;
895 }
896 
897 address ZeroInterpreter::deopt_entry(TosState state, int length) {
898   return nullptr;
899 }
900 
901 address ZeroInterpreter::remove_activation_preserving_args_entry() {
902   // Do an uncommon trap type entry. c++ interpreter will know
903   // to pop frame and preserve the args
904   return Interpreter::deopt_entry(vtos, 0);
905 }
906 
907 address ZeroInterpreter::remove_activation_early_entry(TosState state) {
908   return nullptr;
909 }
910 
911 // Helper for figuring out if frames are interpreter frames
912 
913 bool ZeroInterpreter::contains(address pc) {
914   return false; // make frame::print_value_on work
915 }
916 
917 void ZeroInterpreter::stack_watermark_unwind_check(JavaThread* thread) {
918   // If frame pointer is in the danger zone, notify the runtime that
919   // it needs to act before continuing the unwinding.
920   uintptr_t fp = (uintptr_t)thread->last_Java_fp();
921   uintptr_t watermark = thread->poll_data()->get_polling_word();
922   if (fp > watermark) {
923     InterpreterRuntime::at_unwind(thread);
924   }
925 }