1 /*
  2  * Copyright (c) 2003, 2022, 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/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/macros.hpp"
 48 
 49 #include "entry_zero.hpp"
 50 #include "stack_zero.inline.hpp"
 51 
 52 void ZeroInterpreter::initialize_stub() {
 53   if (_code != NULL) return;
 54 
 55   // generate interpreter
 56   int code_size = InterpreterCodeSize;
 57   NOT_PRODUCT(code_size *= 4;)  // debug uses extra interpreter code space
 58   _code = new StubQueue(new InterpreterCodeletInterface, code_size, NULL,
 59                          "Interpreter");
 60 }
 61 
 62 void ZeroInterpreter::initialize_code() {
 63   AbstractInterpreter::initialize();
 64 
 65   // generate interpreter
 66   { ResourceMark rm;
 67     TraceTime timer("Interpreter generation", TRACETIME_LOG(Info, startuptime));
 68     ZeroInterpreterGenerator g;
 69     if (PrintInterpreter) print();
 70   }
 71 }
 72 
 73 void ZeroInterpreter::invoke_method(Method* method, address entry_point, TRAPS) {
 74   ((ZeroEntry *) entry_point)->invoke(method, THREAD);
 75 }
 76 
 77 void ZeroInterpreter::invoke_osr(Method* method,
 78                                 address   entry_point,
 79                                 address   osr_buf,
 80                                 TRAPS) {
 81   ((ZeroEntry *) entry_point)->invoke_osr(method, osr_buf, THREAD);
 82 }
 83 
 84 
 85 
 86 InterpreterCodelet* ZeroInterpreter::codelet_containing(address pc) {
 87   // FIXME: I'm pretty sure _code is null and this is never called, which is why it's copied.
 88   return (InterpreterCodelet*)_code->stub_containing(pc);
 89 }
 90 #define fixup_after_potential_safepoint()       \
 91   method = istate->method()
 92 
 93 #define CALL_VM_NOCHECK_NOFIX(func)             \
 94   thread->set_last_Java_frame();                \
 95   func;                                         \
 96   thread->reset_last_Java_frame();
 97 
 98 #define CALL_VM_NOCHECK(func)                   \
 99   CALL_VM_NOCHECK_NOFIX(func)                   \
100   fixup_after_potential_safepoint()
101 
102 int ZeroInterpreter::normal_entry(Method* method, intptr_t UNUSED, TRAPS) {
103   JavaThread *thread = THREAD;
104 
105   // Allocate and initialize our frame.
106   InterpreterFrame *frame = InterpreterFrame::build(method, CHECK_0);
107   thread->push_zero_frame(frame);
108 
109   // Execute those bytecodes!
110   main_loop(0, THREAD);
111 
112   // No deoptimized frames on the stack
113   return 0;
114 }
115 
116 int ZeroInterpreter::Reference_get_entry(Method* method, intptr_t UNUSED, TRAPS) {
117   JavaThread* thread = THREAD;
118   ZeroStack* stack = thread->zero_stack();
119   intptr_t* topOfStack = stack->sp();
120 
121   oop ref = STACK_OBJECT(0);
122 
123   // Shortcut if reference is known NULL
124   if (ref == NULL) {
125     return normal_entry(method, 0, THREAD);
126   }
127 
128   // Read the referent with weaker semantics, and let GCs handle the rest.
129   const int referent_offset = java_lang_ref_Reference::referent_offset();
130   oop obj = HeapAccess<IN_HEAP | ON_WEAK_OOP_REF>::oop_load_at(ref, referent_offset);
131 
132   SET_STACK_OBJECT(obj, 0);
133 
134   // No deoptimized frames on the stack
135   return 0;
136 }
137 
138 intptr_t narrow(BasicType type, intptr_t result) {
139   // mask integer result to narrower return type.
140   switch (type) {
141     case T_BOOLEAN:
142       return result&1;
143     case T_BYTE:
144       return (intptr_t)(jbyte)result;
145     case T_CHAR:
146       return (intptr_t)(uintptr_t)(jchar)result;
147     case T_SHORT:
148       return (intptr_t)(jshort)result;
149     case T_OBJECT:  // nothing to do fall through
150     case T_ARRAY:
151     case T_LONG:
152     case T_INT:
153     case T_FLOAT:
154     case T_DOUBLE:
155     case T_VOID:
156       return result;
157     default:
158       ShouldNotReachHere();
159       return result; // silence compiler warnings
160   }
161 }
162 
163 
164 void ZeroInterpreter::main_loop(int recurse, TRAPS) {
165   JavaThread *thread = THREAD;
166   ZeroStack *stack = thread->zero_stack();
167 
168   // If we are entering from a deopt we may need to call
169   // ourself a few times in order to get to our frame.
170   if (recurse)
171     main_loop(recurse - 1, THREAD);
172 
173   InterpreterFrame *frame = thread->top_zero_frame()->as_interpreter_frame();
174   interpreterState istate = frame->interpreter_state();
175   Method* method = istate->method();
176 
177   intptr_t *result = NULL;
178   int result_slots = 0;
179 
180   while (true) {
181     // We can set up the frame anchor with everything we want at
182     // this point as we are thread_in_Java and no safepoints can
183     // occur until we go to vm mode.  We do have to clear flags
184     // on return from vm but that is it.
185     thread->set_last_Java_frame();
186 
187     // Call the interpreter
188     if (JvmtiExport::can_post_interpreter_events()) {
189       if (RewriteBytecodes) {
190         BytecodeInterpreter::run<true, true>(istate);
191       } else {
192         BytecodeInterpreter::run<true, false>(istate);
193       }
194     } else {
195       if (RewriteBytecodes) {
196         BytecodeInterpreter::run<false, true>(istate);
197       } else {
198         BytecodeInterpreter::run<false, false>(istate);
199       }
200     }
201     fixup_after_potential_safepoint();
202 
203     // If we are unwinding, notify the stack watermarks machinery.
204     // Should do this before resetting the frame anchor.
205     if (istate->msg() == BytecodeInterpreter::return_from_method ||
206         istate->msg() == BytecodeInterpreter::do_osr) {
207       stack_watermark_unwind_check(thread);
208     } else {
209       assert(istate->msg() == BytecodeInterpreter::call_method ||
210              istate->msg() == BytecodeInterpreter::more_monitors ||
211              istate->msg() == BytecodeInterpreter::throwing_exception,
212              "Should be one of these otherwise");
213     }
214 
215     // Clear the frame anchor
216     thread->reset_last_Java_frame();
217 
218     // Examine the message from the interpreter to decide what to do
219     if (istate->msg() == BytecodeInterpreter::call_method) {
220       Method* callee = istate->callee();
221 
222       // Trim back the stack to put the parameters at the top
223       stack->set_sp(istate->stack() + 1);
224 
225       // Make the call
226       Interpreter::invoke_method(callee, istate->callee_entry_point(), THREAD);
227       fixup_after_potential_safepoint();
228 
229       // Convert the result
230       istate->set_stack(stack->sp() - 1);
231 
232       // Restore the stack
233       stack->set_sp(istate->stack_limit() + 1);
234 
235       // Resume the interpreter
236       istate->set_msg(BytecodeInterpreter::method_resume);
237     }
238     else if (istate->msg() == BytecodeInterpreter::more_monitors) {
239       int monitor_words = frame::interpreter_frame_monitor_size();
240 
241       // Allocate the space
242       stack->overflow_check(monitor_words, THREAD);
243       if (HAS_PENDING_EXCEPTION)
244         break;
245       stack->alloc(monitor_words * wordSize);
246 
247       // Move the expression stack contents
248       for (intptr_t *p = istate->stack() + 1; p < istate->stack_base(); p++)
249         *(p - monitor_words) = *p;
250 
251       // Move the expression stack pointers
252       istate->set_stack_limit(istate->stack_limit() - monitor_words);
253       istate->set_stack(istate->stack() - monitor_words);
254       istate->set_stack_base(istate->stack_base() - monitor_words);
255 
256       // Zero the new monitor so the interpreter can find it.
257       ((BasicObjectLock *) istate->stack_base())->set_obj(NULL);
258 
259       // Resume the interpreter
260       istate->set_msg(BytecodeInterpreter::got_monitors);
261     }
262     else if (istate->msg() == BytecodeInterpreter::return_from_method) {
263       // Copy the result into the caller's frame
264       result_slots = type2size[method->result_type()];
265       assert(result_slots >= 0 && result_slots <= 2, "what?");
266       result = istate->stack() + result_slots;
267       break;
268     }
269     else if (istate->msg() == BytecodeInterpreter::throwing_exception) {
270       assert(HAS_PENDING_EXCEPTION, "should do");
271       break;
272     }
273     else if (istate->msg() == BytecodeInterpreter::do_osr) {
274       // Unwind the current frame
275       thread->pop_zero_frame();
276 
277       // Remove any extension of the previous frame
278       int extra_locals = method->max_locals() - method->size_of_parameters();
279       stack->set_sp(stack->sp() + extra_locals);
280 
281       // Jump into the OSR method
282       Interpreter::invoke_osr(
283         method, istate->osr_entry(), istate->osr_buf(), THREAD);
284       return;
285     }
286     else {
287       ShouldNotReachHere();
288     }
289   }
290 
291   // Unwind the current frame
292   thread->pop_zero_frame();
293 
294   // Pop our local variables
295   stack->set_sp(stack->sp() + method->max_locals());
296 
297   // Push our result
298   for (int i = 0; i < result_slots; i++) {
299     // Adjust result to smaller
300     union {
301       intptr_t res;
302       jint res_jint;
303     };
304     res = result[-i];
305     if (result_slots == 1) {
306       BasicType t = method->result_type();
307       if (is_subword_type(t)) {
308         res_jint = (jint)narrow(t, res_jint);
309       }
310     }
311     stack->push(res);
312   }
313 }
314 
315 int ZeroInterpreter::native_entry(Method* method, intptr_t UNUSED, TRAPS) {
316   // Make sure method is native and not abstract
317   assert(method->is_native() && !method->is_abstract(), "should be");
318 
319   JavaThread *thread = THREAD;
320   ZeroStack *stack = thread->zero_stack();
321 
322   // Allocate and initialize our frame
323   InterpreterFrame *frame = InterpreterFrame::build(method, CHECK_0);
324   thread->push_zero_frame(frame);
325   interpreterState istate = frame->interpreter_state();
326   intptr_t *locals = istate->locals();
327 
328   // Lock if necessary
329   BasicObjectLock *monitor;
330   monitor = NULL;
331   if (method->is_synchronized()) {
332     monitor = (BasicObjectLock*) istate->stack_base();
333     oop lockee = monitor->obj();
334 
335     CALL_VM_NOCHECK(InterpreterRuntime::monitorenter(thread, lockee));
336     if (HAS_PENDING_EXCEPTION)
337       goto unwind_and_return;
338   }
339 
340   // Get the signature handler
341   InterpreterRuntime::SignatureHandler *handler; {
342     address handlerAddr = method->signature_handler();
343     if (handlerAddr == NULL) {
344       CALL_VM_NOCHECK(InterpreterRuntime::prepare_native_call(thread, method));
345       if (HAS_PENDING_EXCEPTION)
346         goto unlock_unwind_and_return;
347 
348       handlerAddr = method->signature_handler();
349       assert(handlerAddr != NULL, "eh?");
350     }
351     if (handlerAddr == (address) InterpreterRuntime::slow_signature_handler) {
352       CALL_VM_NOCHECK(handlerAddr =
353         InterpreterRuntime::slow_signature_handler(thread, method, NULL,NULL));
354       if (HAS_PENDING_EXCEPTION)
355         goto unlock_unwind_and_return;
356     }
357     handler = \
358       InterpreterRuntime::SignatureHandler::from_handlerAddr(handlerAddr);
359   }
360 
361   // Get the native function entry point
362   address function;
363   function = method->native_function();
364   assert(function != NULL, "should be set if signature handler is");
365 
366   // Build the argument list
367   stack->overflow_check(handler->argument_count() * 2, THREAD);
368   if (HAS_PENDING_EXCEPTION)
369     goto unlock_unwind_and_return;
370 
371   void **arguments;
372   void *mirror; {
373     arguments =
374       (void **) stack->alloc(handler->argument_count() * sizeof(void **));
375     void **dst = arguments;
376 
377     void *env = thread->jni_environment();
378     *(dst++) = &env;
379 
380     if (method->is_static()) {
381       istate->set_oop_temp(
382         method->constants()->pool_holder()->java_mirror());
383       mirror = istate->oop_temp_addr();
384       *(dst++) = &mirror;
385     }
386 
387     intptr_t *src = locals;
388     for (int i = dst - arguments; i < handler->argument_count(); i++) {
389       ffi_type *type = handler->argument_type(i);
390       if (type == &ffi_type_pointer) {
391         if (*src) {
392           stack->push((intptr_t) src);
393           *(dst++) = stack->sp();
394         }
395         else {
396           *(dst++) = src;
397         }
398         src--;
399       }
400       else if (type->size == 4) {
401         *(dst++) = src--;
402       }
403       else if (type->size == 8) {
404         src--;
405         *(dst++) = src--;
406       }
407       else {
408         ShouldNotReachHere();
409       }
410     }
411   }
412 
413   // Set up the Java frame anchor
414   thread->set_last_Java_frame();
415 
416   // Change the thread state to _thread_in_native
417   ThreadStateTransition::transition_from_java(thread, _thread_in_native);
418 
419   // Make the call
420   intptr_t result[4 - LogBytesPerWord];
421   ffi_call(handler->cif(), (void (*)()) function, result, arguments);
422 
423   // Change the thread state back to _thread_in_Java and ensure it
424   // is seen by the GC thread.
425   // ThreadStateTransition::transition_from_native() cannot be used
426   // here because it does not check for asynchronous exceptions.
427   // We have to manage the transition ourself.
428   thread->set_thread_state_fence(_thread_in_native_trans);
429 
430   // Handle safepoint operations, pending suspend requests,
431   // and pending asynchronous exceptions.
432   if (SafepointMechanism::should_process(thread) ||
433       thread->has_special_condition_for_native_trans()) {
434     JavaThread::check_special_condition_for_native_trans(thread);
435     CHECK_UNHANDLED_OOPS_ONLY(thread->clear_unhandled_oops());
436   }
437 
438   // Finally we can change the thread state to _thread_in_Java.
439   thread->set_thread_state(_thread_in_Java);
440   fixup_after_potential_safepoint();
441 
442   // Notify the stack watermarks machinery that we are unwinding.
443   // Should do this before resetting the frame anchor.
444   stack_watermark_unwind_check(thread);
445 
446   // Clear the frame anchor
447   thread->reset_last_Java_frame();
448 
449   // If the result was an oop then unbox it and store it in
450   // oop_temp where the garbage collector can see it before
451   // we release the handle it might be protected by.
452   if (handler->result_type() == &ffi_type_pointer) {
453     if (result[0] == 0) {
454       istate->set_oop_temp(NULL);
455     } else {
456       jobject handle = reinterpret_cast<jobject>(result[0]);
457       istate->set_oop_temp(JNIHandles::resolve(handle));
458     }
459   }
460 
461   // Reset handle block
462   thread->active_handles()->clear();
463 
464  unlock_unwind_and_return:
465 
466   // Unlock if necessary
467   if (monitor) {
468     oop rcvr = monitor->obj();
469     monitor->set_obj(NULL);
470     InterpreterRuntime::monitorexit(rcvr);
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   ConstantPoolCacheEntry* entry = cache->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 == NULL) {
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->flag_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->f2_as_index();
610   if (entry->is_volatile()) {
611     if (support_IRIW_for_not_multiple_copy_atomic_cpu) {
612       OrderAccess::fence();
613     }
614     switch (entry->flag_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->flag_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   ConstantPoolCacheEntry* entry = cache->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 = NULL;
682   switch (entry->flag_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 == NULL) {
695     return normal_entry(method, 0, THREAD);
696   }
697 
698   // Store the stack(0) to field
699   int offset = entry->f2_as_index();
700   if (entry->is_volatile()) {
701     switch (entry->flag_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->flag_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(NULL);
801   istate->set_thread(thread);
802   istate->set_bcp(method->is_native() ? NULL : method->code_base());
803   istate->set_constants(method->constants()->cache());
804   istate->set_msg(BytecodeInterpreter::method_entry);
805   istate->set_oop_temp(NULL);
806   istate->set_callee(NULL);
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(NULL); // 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 NULL;
857 }
858 
859 address ZeroInterpreter::deopt_entry(TosState state, int length) {
860   return NULL;
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 NULL;
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 }