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     }
350     if (!success) {
351       CALL_VM_NOCHECK(InterpreterRuntime::monitorenter(thread, monitor));
352           if (HAS_PENDING_EXCEPTION)
353             goto unwind_and_return;
354     }
355   }
356 
357   // Get the signature handler
358   InterpreterRuntime::SignatureHandler *handler; {
359     address handlerAddr = method->signature_handler();
360     if (handlerAddr == nullptr) {
361       CALL_VM_NOCHECK(InterpreterRuntime::prepare_native_call(thread, method));
362       if (HAS_PENDING_EXCEPTION)
363         goto unlock_unwind_and_return;
364 
365       handlerAddr = method->signature_handler();
366       assert(handlerAddr != nullptr, "eh?");
367     }
368     if (handlerAddr == (address) InterpreterRuntime::slow_signature_handler) {
369       CALL_VM_NOCHECK(handlerAddr =
370         InterpreterRuntime::slow_signature_handler(thread, method, nullptr,nullptr));
371       if (HAS_PENDING_EXCEPTION)
372         goto unlock_unwind_and_return;
373     }
374     handler = \
375       InterpreterRuntime::SignatureHandler::from_handlerAddr(handlerAddr);
376   }
377 
378   // Get the native function entry point
379   address function;
380   function = method->native_function();
381   assert(function != nullptr, "should be set if signature handler is");
382 
383   // Build the argument list
384   stack->overflow_check(handler->argument_count() * 2, THREAD);
385   if (HAS_PENDING_EXCEPTION)
386     goto unlock_unwind_and_return;
387 
388   void **arguments;
389   void *mirror; {
390     arguments =
391       (void **) stack->alloc(handler->argument_count() * sizeof(void **));
392     void **dst = arguments;
393 
394     void *env = thread->jni_environment();
395     *(dst++) = &env;
396 
397     if (method->is_static()) {
398       istate->set_oop_temp(
399         method->constants()->pool_holder()->java_mirror());
400       mirror = istate->oop_temp_addr();
401       *(dst++) = &mirror;
402     }
403 
404     intptr_t *src = locals;
405     for (int i = dst - arguments; i < handler->argument_count(); i++) {
406       ffi_type *type = handler->argument_type(i);
407       if (type == &ffi_type_pointer) {
408         if (*src) {
409           stack->push((intptr_t) src);
410           *(dst++) = stack->sp();
411         }
412         else {
413           *(dst++) = src;
414         }
415         src--;
416       }
417       else if (type->size == 4) {
418         *(dst++) = src--;
419       }
420       else if (type->size == 8) {
421         src--;
422         *(dst++) = src--;
423       }
424       else {
425         ShouldNotReachHere();
426       }
427     }
428   }
429 
430   // Set up the Java frame anchor
431   thread->set_last_Java_frame();
432 
433   // Change the thread state to _thread_in_native
434   ThreadStateTransition::transition_from_java(thread, _thread_in_native);
435 
436   // Make the call
437   intptr_t result[4 - LogBytesPerWord];
438   ffi_call(handler->cif(), (void (*)()) function, result, arguments);
439 
440   // Change the thread state back to _thread_in_Java and ensure it
441   // is seen by the GC thread.
442   // ThreadStateTransition::transition_from_native() cannot be used
443   // here because it does not check for asynchronous exceptions.
444   // We have to manage the transition ourself.
445   thread->set_thread_state_fence(_thread_in_native_trans);
446 
447   // Handle safepoint operations, pending suspend requests,
448   // and pending asynchronous exceptions.
449   if (SafepointMechanism::should_process(thread) ||
450       thread->has_special_condition_for_native_trans()) {
451     JavaThread::check_special_condition_for_native_trans(thread);
452     CHECK_UNHANDLED_OOPS_ONLY(thread->clear_unhandled_oops());
453   }
454 
455   // Finally we can change the thread state to _thread_in_Java.
456   thread->set_thread_state(_thread_in_Java);
457   fixup_after_potential_safepoint();
458 
459   // Notify the stack watermarks machinery that we are unwinding.
460   // Should do this before resetting the frame anchor.
461   stack_watermark_unwind_check(thread);
462 
463   // Clear the frame anchor
464   thread->reset_last_Java_frame();
465 
466   // If the result was an oop then unbox it and store it in
467   // oop_temp where the garbage collector can see it before
468   // we release the handle it might be protected by.
469   if (handler->result_type() == &ffi_type_pointer) {
470     if (result[0] == 0) {
471       istate->set_oop_temp(nullptr);
472     } else {
473       jobject handle = reinterpret_cast<jobject>(result[0]);
474       istate->set_oop_temp(JNIHandles::resolve(handle));
475     }
476   }
477 
478   // Reset handle block
479   thread->active_handles()->clear();
480 
481  unlock_unwind_and_return:
482 
483   // Unlock if necessary
484   if (monitor) {
485     bool success = false;
486     if (LockingMode == LM_LEGACY) {
487       BasicLock* lock = monitor->lock();
488       oop rcvr = monitor->obj();
489       monitor->set_obj(nullptr);
490       success = true;
491       markWord header = lock->displaced_header();
492       if (header.to_pointer() != nullptr) { // Check for recursive lock
493         markWord old_header = markWord::encode(lock);
494         if (rcvr->cas_set_mark(header, old_header) != old_header) {
495           monitor->set_obj(rcvr);
496           success = false;
497         }
498       }
499     }
500     if (!success) {
501       InterpreterRuntime::monitorexit(monitor);
502     }
503   }
504 
505   unwind_and_return:
506 
507   // Unwind the current activation
508   thread->pop_zero_frame();
509 
510   // Pop our parameters
511   stack->set_sp(stack->sp() + method->size_of_parameters());
512 
513   // Push our result
514   if (!HAS_PENDING_EXCEPTION) {
515     BasicType type = method->result_type();
516     stack->set_sp(stack->sp() - type2size[type]);
517 
518     switch (type) {
519     case T_VOID:
520       break;
521 
522     case T_BOOLEAN:
523 #ifndef VM_LITTLE_ENDIAN
524       result[0] <<= (BitsPerWord - BitsPerByte);
525 #endif
526       SET_LOCALS_INT(*(jboolean *) result != 0, 0);
527       break;
528 
529     case T_CHAR:
530 #ifndef VM_LITTLE_ENDIAN
531       result[0] <<= (BitsPerWord - BitsPerShort);
532 #endif
533       SET_LOCALS_INT(*(jchar *) result, 0);
534       break;
535 
536     case T_BYTE:
537 #ifndef VM_LITTLE_ENDIAN
538       result[0] <<= (BitsPerWord - BitsPerByte);
539 #endif
540       SET_LOCALS_INT(*(jbyte *) result, 0);
541       break;
542 
543     case T_SHORT:
544 #ifndef VM_LITTLE_ENDIAN
545       result[0] <<= (BitsPerWord - BitsPerShort);
546 #endif
547       SET_LOCALS_INT(*(jshort *) result, 0);
548       break;
549 
550     case T_INT:
551 #ifndef VM_LITTLE_ENDIAN
552       result[0] <<= (BitsPerWord - BitsPerInt);
553 #endif
554       SET_LOCALS_INT(*(jint *) result, 0);
555       break;
556 
557     case T_LONG:
558       SET_LOCALS_LONG(*(jlong *) result, 0);
559       break;
560 
561     case T_FLOAT:
562       SET_LOCALS_FLOAT(*(jfloat *) result, 0);
563       break;
564 
565     case T_DOUBLE:
566       SET_LOCALS_DOUBLE(*(jdouble *) result, 0);
567       break;
568 
569     case T_OBJECT:
570     case T_ARRAY:
571       SET_LOCALS_OBJECT(istate->oop_temp(), 0);
572       break;
573 
574     default:
575       ShouldNotReachHere();
576     }
577   }
578 
579   // Already did every pending exception check here.
580   // If HAS_PENDING_EXCEPTION is true, the interpreter would handle the rest.
581   if (CheckJNICalls) {
582     THREAD->clear_pending_jni_exception_check();
583   }
584 
585   // No deoptimized frames on the stack
586   return 0;
587 }
588 
589 int ZeroInterpreter::getter_entry(Method* method, intptr_t UNUSED, TRAPS) {
590   JavaThread* thread = THREAD;
591   // Drop into the slow path if we need a safepoint check
592   if (SafepointMechanism::should_process(thread)) {
593     return normal_entry(method, 0, THREAD);
594   }
595 
596   // Read the field index from the bytecode:
597   //  0:  aload_0
598   //  1:  getfield
599   //  2:    index
600   //  3:    index
601   //  4:  return
602   //
603   // NB this is not raw bytecode: index is in machine order
604 
605   assert(method->is_getter(), "Expect the particular bytecode shape");
606   u1* code = method->code_base();
607   u2 index = Bytes::get_native_u2(&code[2]);
608 
609   // Get the entry from the constant pool cache, and drop into
610   // the slow path if it has not been resolved
611   ConstantPoolCache* cache = method->constants()->cache();
612   ResolvedFieldEntry* entry = cache->resolved_field_entry_at(index);
613   if (!entry->is_resolved(Bytecodes::_getfield)) {
614     return normal_entry(method, 0, THREAD);
615   }
616 
617   ZeroStack* stack = thread->zero_stack();
618   intptr_t* topOfStack = stack->sp();
619 
620   // Load the object pointer and drop into the slow path
621   // if we have a NullPointerException
622   oop object = STACK_OBJECT(0);
623   if (object == nullptr) {
624     return normal_entry(method, 0, THREAD);
625   }
626 
627   // If needed, allocate additional slot on stack: we already have one
628   // for receiver, and double/long need another one.
629   switch (entry->tos_state()) {
630     case ltos:
631     case dtos:
632       stack->overflow_check(1, CHECK_0);
633       stack->alloc(wordSize);
634       topOfStack = stack->sp();
635       break;
636     default:
637       ;
638   }
639 
640   // Read the field to stack(0)
641   int offset = entry->field_offset();
642   if (entry->is_volatile()) {
643     if (support_IRIW_for_not_multiple_copy_atomic_cpu) {
644       OrderAccess::fence();
645     }
646     switch (entry->tos_state()) {
647       case btos:
648       case ztos: SET_STACK_INT(object->byte_field_acquire(offset),      0); break;
649       case ctos: SET_STACK_INT(object->char_field_acquire(offset),      0); break;
650       case stos: SET_STACK_INT(object->short_field_acquire(offset),     0); break;
651       case itos: SET_STACK_INT(object->int_field_acquire(offset),       0); break;
652       case ltos: SET_STACK_LONG(object->long_field_acquire(offset),     0); break;
653       case ftos: SET_STACK_FLOAT(object->float_field_acquire(offset),   0); break;
654       case dtos: SET_STACK_DOUBLE(object->double_field_acquire(offset), 0); break;
655       case atos: SET_STACK_OBJECT(object->obj_field_acquire(offset),    0); break;
656       default:
657         ShouldNotReachHere();
658     }
659   } else {
660     switch (entry->tos_state()) {
661       case btos:
662       case ztos: SET_STACK_INT(object->byte_field(offset),      0); break;
663       case ctos: SET_STACK_INT(object->char_field(offset),      0); break;
664       case stos: SET_STACK_INT(object->short_field(offset),     0); break;
665       case itos: SET_STACK_INT(object->int_field(offset),       0); break;
666       case ltos: SET_STACK_LONG(object->long_field(offset),     0); break;
667       case ftos: SET_STACK_FLOAT(object->float_field(offset),   0); break;
668       case dtos: SET_STACK_DOUBLE(object->double_field(offset), 0); break;
669       case atos: SET_STACK_OBJECT(object->obj_field(offset),    0); break;
670       default:
671         ShouldNotReachHere();
672     }
673   }
674 
675   // No deoptimized frames on the stack
676   return 0;
677 }
678 
679 int ZeroInterpreter::setter_entry(Method* method, intptr_t UNUSED, TRAPS) {
680   JavaThread* thread = THREAD;
681   // Drop into the slow path if we need a safepoint check
682   if (SafepointMechanism::should_process(thread)) {
683     return normal_entry(method, 0, THREAD);
684   }
685 
686   // Read the field index from the bytecode:
687   //  0:  aload_0
688   //  1:  *load_1
689   //  2:  putfield
690   //  3:    index
691   //  4:    index
692   //  5:  return
693   //
694   // NB this is not raw bytecode: index is in machine order
695 
696   assert(method->is_setter(), "Expect the particular bytecode shape");
697   u1* code = method->code_base();
698   u2 index = Bytes::get_native_u2(&code[3]);
699 
700   // Get the entry from the constant pool cache, and drop into
701   // the slow path if it has not been resolved
702   ConstantPoolCache* cache = method->constants()->cache();
703   ResolvedFieldEntry* entry = cache->resolved_field_entry_at(index);
704   if (!entry->is_resolved(Bytecodes::_putfield)) {
705     return normal_entry(method, 0, THREAD);
706   }
707 
708   ZeroStack* stack = thread->zero_stack();
709   intptr_t* topOfStack = stack->sp();
710 
711   // Figure out where the receiver is. If there is a long/double
712   // operand on stack top, then receiver is two slots down.
713   oop object = nullptr;
714   switch (entry->tos_state()) {
715     case ltos:
716     case dtos:
717       object = STACK_OBJECT(-2);
718       break;
719     default:
720       object = STACK_OBJECT(-1);
721       break;
722   }
723 
724   // Load the receiver pointer and drop into the slow path
725   // if we have a NullPointerException
726   if (object == nullptr) {
727     return normal_entry(method, 0, THREAD);
728   }
729 
730   // Store the stack(0) to field
731   int offset = entry->field_offset();
732   if (entry->is_volatile()) {
733     switch (entry->tos_state()) {
734       case btos: object->release_byte_field_put(offset,   STACK_INT(0));     break;
735       case ztos: object->release_byte_field_put(offset,   STACK_INT(0) & 1); break; // only store LSB
736       case ctos: object->release_char_field_put(offset,   STACK_INT(0));     break;
737       case stos: object->release_short_field_put(offset,  STACK_INT(0));     break;
738       case itos: object->release_int_field_put(offset,    STACK_INT(0));     break;
739       case ltos: object->release_long_field_put(offset,   STACK_LONG(0));    break;
740       case ftos: object->release_float_field_put(offset,  STACK_FLOAT(0));   break;
741       case dtos: object->release_double_field_put(offset, STACK_DOUBLE(0));  break;
742       case atos: object->release_obj_field_put(offset,    STACK_OBJECT(0));  break;
743       default:
744         ShouldNotReachHere();
745     }
746     OrderAccess::storeload();
747   } else {
748     switch (entry->tos_state()) {
749       case btos: object->byte_field_put(offset,   STACK_INT(0));     break;
750       case ztos: object->byte_field_put(offset,   STACK_INT(0) & 1); break; // only store LSB
751       case ctos: object->char_field_put(offset,   STACK_INT(0));     break;
752       case stos: object->short_field_put(offset,  STACK_INT(0));     break;
753       case itos: object->int_field_put(offset,    STACK_INT(0));     break;
754       case ltos: object->long_field_put(offset,   STACK_LONG(0));    break;
755       case ftos: object->float_field_put(offset,  STACK_FLOAT(0));   break;
756       case dtos: object->double_field_put(offset, STACK_DOUBLE(0));  break;
757       case atos: object->obj_field_put(offset,    STACK_OBJECT(0));  break;
758       default:
759         ShouldNotReachHere();
760     }
761   }
762 
763   // Nothing is returned, pop out parameters
764   stack->set_sp(stack->sp() + method->size_of_parameters());
765 
766   // No deoptimized frames on the stack
767   return 0;
768 }
769 
770 int ZeroInterpreter::empty_entry(Method* method, intptr_t UNUSED, TRAPS) {
771   JavaThread *thread = THREAD;
772   ZeroStack *stack = thread->zero_stack();
773 
774   // Drop into the slow path if we need a safepoint check
775   if (SafepointMechanism::should_process(thread)) {
776     return normal_entry(method, 0, THREAD);
777   }
778 
779   // Pop our parameters
780   stack->set_sp(stack->sp() + method->size_of_parameters());
781 
782   // No deoptimized frames on the stack
783   return 0;
784 }
785 
786 InterpreterFrame *InterpreterFrame::build(Method* const method, TRAPS) {
787   JavaThread *thread = THREAD;
788   ZeroStack *stack = thread->zero_stack();
789 
790   // Calculate the size of the frame we'll build, including
791   // any adjustments to the caller's frame that we'll make.
792   int extra_locals  = 0;
793   int monitor_words = 0;
794   int stack_words   = 0;
795 
796   if (!method->is_native()) {
797     extra_locals = method->max_locals() - method->size_of_parameters();
798     stack_words  = method->max_stack();
799   }
800   if (method->is_synchronized()) {
801     monitor_words = frame::interpreter_frame_monitor_size();
802   }
803   stack->overflow_check(
804     extra_locals + header_words + monitor_words + stack_words, CHECK_NULL);
805 
806   // Adjust the caller's stack frame to accommodate any additional
807   // local variables we have contiguously with our parameters.
808   for (int i = 0; i < extra_locals; i++)
809     stack->push(0);
810 
811   intptr_t *locals;
812   if (method->is_native())
813     locals = stack->sp() + (method->size_of_parameters() - 1);
814   else
815     locals = stack->sp() + (method->max_locals() - 1);
816 
817   stack->push(0); // next_frame, filled in later
818   intptr_t *fp = stack->sp();
819   assert(fp - stack->sp() == next_frame_off, "should be");
820 
821   stack->push(INTERPRETER_FRAME);
822   assert(fp - stack->sp() == frame_type_off, "should be");
823 
824   interpreterState istate =
825     (interpreterState) stack->alloc(sizeof(BytecodeInterpreter));
826   assert(fp - stack->sp() == istate_off, "should be");
827 
828   istate->set_locals(locals);
829   istate->set_method(method);
830   istate->set_mirror(method->method_holder()->java_mirror());
831   istate->set_self_link(istate);
832   istate->set_prev_link(nullptr);
833   istate->set_thread(thread);
834   istate->set_bcp(method->is_native() ? nullptr : method->code_base());
835   istate->set_constants(method->constants()->cache());
836   istate->set_msg(BytecodeInterpreter::method_entry);
837   istate->set_oop_temp(nullptr);
838   istate->set_callee(nullptr);
839 
840   istate->set_monitor_base((BasicObjectLock *) stack->sp());
841   if (method->is_synchronized()) {
842     BasicObjectLock *monitor =
843       (BasicObjectLock *) stack->alloc(monitor_words * wordSize);
844     oop object;
845     if (method->is_static())
846       object = method->constants()->pool_holder()->java_mirror();
847     else
848       object = cast_to_oop((void*)locals[0]);
849     monitor->set_obj(object);
850   }
851 
852   istate->set_stack_base(stack->sp());
853   istate->set_stack(stack->sp() - 1);
854   if (stack_words)
855     stack->alloc(stack_words * wordSize);
856   istate->set_stack_limit(stack->sp() - 1);
857 
858   return (InterpreterFrame *) fp;
859 }
860 
861 InterpreterFrame *InterpreterFrame::build(int size, TRAPS) {
862   ZeroStack *stack = THREAD->zero_stack();
863 
864   int size_in_words = size >> LogBytesPerWord;
865   assert(size_in_words * wordSize == size, "unaligned");
866   assert(size_in_words >= header_words, "too small");
867   stack->overflow_check(size_in_words, CHECK_NULL);
868 
869   stack->push(0); // next_frame, filled in later
870   intptr_t *fp = stack->sp();
871   assert(fp - stack->sp() == next_frame_off, "should be");
872 
873   stack->push(INTERPRETER_FRAME);
874   assert(fp - stack->sp() == frame_type_off, "should be");
875 
876   interpreterState istate =
877     (interpreterState) stack->alloc(sizeof(BytecodeInterpreter));
878   assert(fp - stack->sp() == istate_off, "should be");
879   istate->set_self_link(nullptr); // mark invalid
880 
881   stack->alloc((size_in_words - header_words) * wordSize);
882 
883   return (InterpreterFrame *) fp;
884 }
885 
886 address ZeroInterpreter::return_entry(TosState state, int length, Bytecodes::Code code) {
887   ShouldNotCallThis();
888   return nullptr;
889 }
890 
891 address ZeroInterpreter::deopt_entry(TosState state, int length) {
892   return nullptr;
893 }
894 
895 address ZeroInterpreter::remove_activation_preserving_args_entry() {
896   // Do an uncommon trap type entry. c++ interpreter will know
897   // to pop frame and preserve the args
898   return Interpreter::deopt_entry(vtos, 0);
899 }
900 
901 address ZeroInterpreter::remove_activation_early_entry(TosState state) {
902   return nullptr;
903 }
904 
905 // Helper for figuring out if frames are interpreter frames
906 
907 bool ZeroInterpreter::contains(address pc) {
908   return false; // make frame::print_value_on work
909 }
910 
911 void ZeroInterpreter::stack_watermark_unwind_check(JavaThread* thread) {
912   // If frame pointer is in the danger zone, notify the runtime that
913   // it needs to act before continuing the unwinding.
914   uintptr_t fp = (uintptr_t)thread->last_Java_fp();
915   uintptr_t watermark = thread->poll_data()->get_polling_word();
916   if (fp > watermark) {
917     InterpreterRuntime::at_unwind(thread);
918   }
919 }