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