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