1 /*
   2  * Copyright (c) 2008, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "asm/assembler.hpp"
  27 #include "interpreter/bytecode.hpp"
  28 #include "interpreter/interpreter.hpp"
  29 #include "oops/constMethod.hpp"
  30 #include "oops/method.hpp"
  31 #include "prims/methodHandles.hpp"
  32 #include "runtime/handles.inline.hpp"
  33 #include "runtime/frame.inline.hpp"
  34 #include "runtime/synchronizer.hpp"
  35 #include "utilities/macros.hpp"
  36 
  37 int AbstractInterpreter::BasicType_as_index(BasicType type) {
  38   int i = 0;
  39   switch (type) {
  40 #ifdef AARCH64
  41     case T_BOOLEAN: i = 0; break;
  42     case T_CHAR   : i = 1; break;
  43     case T_BYTE   : i = 2; break;
  44     case T_SHORT  : i = 3; break;
  45     case T_INT    : // fall through
  46     case T_LONG   : // fall through
  47     case T_VOID   : // fall through
  48     case T_FLOAT  : // fall through
  49     case T_DOUBLE : i = 4; break;
  50     case T_OBJECT : // fall through
  51     case T_ARRAY  : i = 5; break;
  52 #else
  53     case T_VOID   : i = 0; break;
  54     case T_BOOLEAN: i = 1; break;
  55     case T_CHAR   : i = 2; break;
  56     case T_BYTE   : i = 3; break;
  57     case T_SHORT  : i = 4; break;
  58     case T_INT    : i = 5; break;
  59     case T_OBJECT : // fall through
  60     case T_ARRAY  : i = 6; break;
  61     case T_LONG   : i = 7; break;
  62     case T_FLOAT  : i = 8; break;
  63     case T_DOUBLE : i = 9; break;
  64 #endif // AARCH64
  65     default       : ShouldNotReachHere();
  66   }
  67   assert(0 <= i && i < AbstractInterpreter::number_of_result_handlers, "index out of bounds");
  68   return i;
  69 }
  70 
  71 // How much stack a method activation needs in words.
  72 int AbstractInterpreter::size_top_interpreter_activation(Method* method) {
  73   const int stub_code = AARCH64_ONLY(24) NOT_AARCH64(12);  // see generate_call_stub
  74   // Save space for one monitor to get into the interpreted method in case
  75   // the method is synchronized
  76   int monitor_size    = method->is_synchronized() ?
  77                                 1*frame::interpreter_frame_monitor_size() : 0;
  78 
  79   // total overhead size: monitor_size + (sender SP, thru expr stack bottom).
  80   // be sure to change this if you add/subtract anything to/from the overhead area
  81   const int overhead_size = monitor_size +
  82                             (frame::sender_sp_offset - frame::interpreter_frame_initial_sp_offset);
  83   const int method_stack = (method->max_locals() + method->max_stack()) *
  84                            Interpreter::stackElementWords;
  85   return overhead_size + method_stack + stub_code;
  86 }
  87 
  88 // asm based interpreter deoptimization helpers
  89 int AbstractInterpreter::size_activation(int max_stack,
  90                                          int tempcount,
  91                                          int extra_args,
  92                                          int moncount,
  93                                          int callee_param_count,
  94                                          int callee_locals,
  95                                          bool is_top_frame) {
  96   // Note: This calculation must exactly parallel the frame setup
  97   // in TemplateInterpreterGenerator::generate_fixed_frame.
  98   // fixed size of an interpreter frame:
  99   int overhead = frame::sender_sp_offset - frame::interpreter_frame_initial_sp_offset;
 100 
 101   // Our locals were accounted for by the caller (or last_frame_adjust on the transistion)
 102   // Since the callee parameters already account for the callee's params we only need to account for
 103   // the extra locals.
 104 
 105   int size = overhead +
 106          ((callee_locals - callee_param_count)*Interpreter::stackElementWords) +
 107          (moncount*frame::interpreter_frame_monitor_size()) +
 108          tempcount*Interpreter::stackElementWords + extra_args;
 109 
 110 #ifdef AARCH64
 111   size = round_to(size, StackAlignmentInBytes/BytesPerWord);
 112 #endif // AARCH64
 113 
 114   return size;
 115 }
 116 
 117 void AbstractInterpreter::layout_activation(Method* method,
 118                                             int tempcount,
 119                                             int popframe_extra_args,
 120                                             int moncount,
 121                                             int caller_actual_parameters,
 122                                             int callee_param_count,
 123                                             int callee_locals,
 124                                             frame* caller,
 125                                             frame* interpreter_frame,
 126                                             bool is_top_frame,
 127                                             bool is_bottom_frame) {
 128 
 129   // Set up the method, locals, and monitors.
 130   // The frame interpreter_frame is guaranteed to be the right size,
 131   // as determined by a previous call to the size_activation() method.
 132   // It is also guaranteed to be walkable even though it is in a skeletal state
 133   // NOTE: return size is in words not bytes
 134 
 135   // fixed size of an interpreter frame:
 136   int max_locals = method->max_locals() * Interpreter::stackElementWords;
 137   int extra_locals = (method->max_locals() - method->size_of_parameters()) * Interpreter::stackElementWords;
 138 
 139 #ifdef ASSERT
 140   assert(caller->sp() == interpreter_frame->sender_sp(), "Frame not properly walkable");
 141 #endif
 142 
 143   interpreter_frame->interpreter_frame_set_method(method);
 144   // NOTE the difference in using sender_sp and interpreter_frame_sender_sp
 145   // interpreter_frame_sender_sp is the original sp of the caller (the unextended_sp)
 146   // and sender_sp is (fp + sender_sp_offset*wordSize)
 147 
 148 #ifdef AARCH64
 149   intptr_t* locals;
 150   if (caller->is_interpreted_frame()) {
 151     // attach locals to the expression stack of caller interpreter frame
 152     locals = caller->interpreter_frame_tos_address() + caller_actual_parameters*Interpreter::stackElementWords - 1;
 153   } else {
 154     assert (is_bottom_frame, "should be");
 155     locals = interpreter_frame->fp() + frame::sender_sp_offset + method->max_locals() - 1;
 156   }
 157 
 158   if (TraceDeoptimization) {
 159     tty->print_cr("layout_activation:");
 160 
 161     if (caller->is_entry_frame()) {
 162       tty->print("entry ");
 163     }
 164     if (caller->is_compiled_frame()) {
 165       tty->print("compiled ");
 166     }
 167     if (caller->is_interpreted_frame()) {
 168       tty->print("interpreted ");
 169     }
 170     tty->print_cr("caller: sp=%p, unextended_sp=%p, fp=%p, pc=%p", caller->sp(), caller->unextended_sp(), caller->fp(), caller->pc());
 171     tty->print_cr("interpreter_frame: sp=%p, unextended_sp=%p, fp=%p, pc=%p", interpreter_frame->sp(), interpreter_frame->unextended_sp(), interpreter_frame->fp(), interpreter_frame->pc());
 172     tty->print_cr("method: max_locals = %d, size_of_parameters = %d", method->max_locals(), method->size_of_parameters());
 173     tty->print_cr("caller_actual_parameters = %d", caller_actual_parameters);
 174     tty->print_cr("locals = %p", locals);
 175   }
 176 
 177 #ifdef ASSERT
 178   if (caller_actual_parameters != method->size_of_parameters()) {
 179     assert(caller->is_interpreted_frame(), "adjusted caller_actual_parameters, but caller is not interpreter frame");
 180     Bytecode_invoke inv(caller->interpreter_frame_method(), caller->interpreter_frame_bci());
 181 
 182     if (is_bottom_frame) {
 183       assert(caller_actual_parameters == 0, "invalid adjusted caller_actual_parameters value for bottom frame");
 184       assert(inv.is_invokedynamic() || inv.is_invokehandle(), "adjusted caller_actual_parameters for bottom frame, but not invokedynamic/invokehandle");
 185     } else {
 186       assert(caller_actual_parameters == method->size_of_parameters()+1, "invalid adjusted caller_actual_parameters value");
 187       assert(!inv.is_invokedynamic() && MethodHandles::has_member_arg(inv.klass(), inv.name()), "adjusted caller_actual_parameters, but no member arg");
 188     }
 189   }
 190   if (caller->is_interpreted_frame()) {
 191     intptr_t* locals_base = (locals - method->max_locals()*Interpreter::stackElementWords + 1);
 192     locals_base = (intptr_t*)round_down((intptr_t)locals_base, StackAlignmentInBytes);
 193     assert(interpreter_frame->sender_sp() <= locals_base, "interpreter-to-interpreter frame chaining");
 194 
 195   } else if (caller->is_compiled_frame()) {
 196     assert(locals + 1 <= caller->unextended_sp(), "compiled-to-interpreter frame chaining");
 197 
 198   } else {
 199     assert(caller->is_entry_frame(), "should be");
 200     assert(locals + 1 <= caller->fp(), "entry-to-interpreter frame chaining");
 201   }
 202 #endif // ASSERT
 203 
 204 #else
 205   intptr_t* locals = interpreter_frame->sender_sp() + max_locals - 1;
 206 #endif // AARCH64
 207 
 208   interpreter_frame->interpreter_frame_set_locals(locals);
 209   BasicObjectLock* montop = interpreter_frame->interpreter_frame_monitor_begin();
 210   BasicObjectLock* monbot = montop - moncount;
 211   interpreter_frame->interpreter_frame_set_monitor_end(monbot);
 212 
 213   // Set last_sp
 214   intptr_t* stack_top = (intptr_t*) monbot  -
 215     tempcount*Interpreter::stackElementWords -
 216     popframe_extra_args;
 217 #ifdef AARCH64
 218   interpreter_frame->interpreter_frame_set_stack_top(stack_top);
 219 
 220   // We have to add extra reserved slots to max_stack. There are 3 users of the extra slots,
 221   // none of which are at the same time, so we just need to make sure there is enough room
 222   // for the biggest user:
 223   //   -reserved slot for exception handler
 224   //   -reserved slots for JSR292. Method::extra_stack_entries() is the size.
 225   //   -3 reserved slots so get_method_counters() can save some registers before call_VM().
 226   int max_stack = method->constMethod()->max_stack() + MAX2(3, Method::extra_stack_entries());
 227   intptr_t* extended_sp = (intptr_t*) monbot  -
 228     (max_stack * Interpreter::stackElementWords) -
 229     popframe_extra_args;
 230   extended_sp = (intptr_t*)round_down((intptr_t)extended_sp, StackAlignmentInBytes);
 231   interpreter_frame->interpreter_frame_set_extended_sp(extended_sp);
 232 #else
 233   interpreter_frame->interpreter_frame_set_last_sp(stack_top);
 234 #endif // AARCH64
 235 
 236   // All frames but the initial (oldest) interpreter frame we fill in have a
 237   // value for sender_sp that allows walking the stack but isn't
 238   // truly correct. Correct the value here.
 239 
 240 #ifdef AARCH64
 241   if (caller->is_interpreted_frame()) {
 242     intptr_t* sender_sp = (intptr_t*)round_down((intptr_t)caller->interpreter_frame_tos_address(), StackAlignmentInBytes);
 243     interpreter_frame->set_interpreter_frame_sender_sp(sender_sp);
 244 
 245   } else {
 246     // in case of non-interpreter caller sender_sp of the oldest frame is already
 247     // set to valid value
 248   }
 249 #else
 250   if (extra_locals != 0 &&
 251       interpreter_frame->sender_sp() == interpreter_frame->interpreter_frame_sender_sp() ) {
 252     interpreter_frame->set_interpreter_frame_sender_sp(caller->sp() + extra_locals);
 253   }
 254 #endif // AARCH64
 255 
 256   *interpreter_frame->interpreter_frame_cache_addr() =
 257     method->constants()->cache();
 258   *interpreter_frame->interpreter_frame_mirror_addr() =
 259     method->method_holder()->java_mirror();
 260 }