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 // These should never be compiled since the interpreter will prefer
  72 // the compiled version to the intrinsic version.
  73 bool AbstractInterpreter::can_be_compiled(methodHandle m) {
  74   switch (method_kind(m)) {
  75     case Interpreter::java_lang_math_sin     : // fall thru
  76     case Interpreter::java_lang_math_cos     : // fall thru
  77     case Interpreter::java_lang_math_tan     : // fall thru
  78     case Interpreter::java_lang_math_abs     : // fall thru
  79     case Interpreter::java_lang_math_log     : // fall thru
  80     case Interpreter::java_lang_math_log10   : // fall thru
  81     case Interpreter::java_lang_math_sqrt    :
  82       return false;
  83     default:
  84       return true;
  85   }
  86 }
  87 
  88 // How much stack a method activation needs in words.
  89 int AbstractInterpreter::size_top_interpreter_activation(Method* method) {
  90   const int stub_code = AARCH64_ONLY(24) NOT_AARCH64(12);  // see generate_call_stub
  91   // Save space for one monitor to get into the interpreted method in case
  92   // the method is synchronized
  93   int monitor_size    = method->is_synchronized() ?
  94                                 1*frame::interpreter_frame_monitor_size() : 0;
  95 
  96   // total overhead size: monitor_size + (sender SP, thru expr stack bottom).
  97   // be sure to change this if you add/subtract anything to/from the overhead area
  98   const int overhead_size = monitor_size +
  99                             (frame::sender_sp_offset - frame::interpreter_frame_initial_sp_offset);
 100   const int method_stack = (method->max_locals() + method->max_stack()) *
 101                            Interpreter::stackElementWords;
 102   return overhead_size + method_stack + stub_code;
 103 }
 104 
 105 // asm based interpreter deoptimization helpers
 106 int AbstractInterpreter::size_activation(int max_stack,
 107                                          int tempcount,
 108                                          int extra_args,
 109                                          int moncount,
 110                                          int callee_param_count,
 111                                          int callee_locals,
 112                                          bool is_top_frame) {
 113   // Note: This calculation must exactly parallel the frame setup
 114   // in TemplateInterpreterGenerator::generate_fixed_frame.
 115   // fixed size of an interpreter frame:
 116   int overhead = frame::sender_sp_offset - frame::interpreter_frame_initial_sp_offset;
 117 
 118   // Our locals were accounted for by the caller (or last_frame_adjust on the transistion)
 119   // Since the callee parameters already account for the callee's params we only need to account for
 120   // the extra locals.
 121 
 122   int size = overhead +
 123          ((callee_locals - callee_param_count)*Interpreter::stackElementWords) +
 124          (moncount*frame::interpreter_frame_monitor_size()) +
 125          tempcount*Interpreter::stackElementWords + extra_args;
 126 
 127 #ifdef AARCH64
 128   size = round_to(size, StackAlignmentInBytes/BytesPerWord);
 129 #endif // AARCH64
 130 
 131   return size;
 132 }
 133 
 134 void AbstractInterpreter::layout_activation(Method* method,
 135                                             int tempcount,
 136                                             int popframe_extra_args,
 137                                             int moncount,
 138                                             int caller_actual_parameters,
 139                                             int callee_param_count,
 140                                             int callee_locals,
 141                                             frame* caller,
 142                                             frame* interpreter_frame,
 143                                             bool is_top_frame,
 144                                             bool is_bottom_frame) {
 145 
 146   // Set up the method, locals, and monitors.
 147   // The frame interpreter_frame is guaranteed to be the right size,
 148   // as determined by a previous call to the size_activation() method.
 149   // It is also guaranteed to be walkable even though it is in a skeletal state
 150   // NOTE: return size is in words not bytes
 151 
 152   // fixed size of an interpreter frame:
 153   int max_locals = method->max_locals() * Interpreter::stackElementWords;
 154   int extra_locals = (method->max_locals() - method->size_of_parameters()) * Interpreter::stackElementWords;
 155 
 156 #ifdef ASSERT
 157   assert(caller->sp() == interpreter_frame->sender_sp(), "Frame not properly walkable");
 158 #endif
 159 
 160   interpreter_frame->interpreter_frame_set_method(method);
 161   // NOTE the difference in using sender_sp and interpreter_frame_sender_sp
 162   // interpreter_frame_sender_sp is the original sp of the caller (the unextended_sp)
 163   // and sender_sp is (fp + sender_sp_offset*wordSize)
 164 
 165 #ifdef AARCH64
 166   intptr_t* locals;
 167   if (caller->is_interpreted_frame()) {
 168     // attach locals to the expression stack of caller interpreter frame
 169     locals = caller->interpreter_frame_tos_address() + caller_actual_parameters*Interpreter::stackElementWords - 1;
 170   } else {
 171     assert (is_bottom_frame, "should be");
 172     locals = interpreter_frame->fp() + frame::sender_sp_offset + method->max_locals() - 1;
 173   }
 174 
 175   if (TraceDeoptimization) {
 176     tty->print_cr("layout_activation:");
 177 
 178     if (caller->is_entry_frame()) {
 179       tty->print("entry ");
 180     }
 181     if (caller->is_compiled_frame()) {
 182       tty->print("compiled ");
 183     }
 184     if (caller->is_interpreted_frame()) {
 185       tty->print("interpreted ");
 186     }
 187     tty->print_cr("caller: sp=%p, unextended_sp=%p, fp=%p, pc=%p", caller->sp(), caller->unextended_sp(), caller->fp(), caller->pc());
 188     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());
 189     tty->print_cr("method: max_locals = %d, size_of_parameters = %d", method->max_locals(), method->size_of_parameters());
 190     tty->print_cr("caller_actual_parameters = %d", caller_actual_parameters);
 191     tty->print_cr("locals = %p", locals);
 192   }
 193 
 194 #ifdef ASSERT
 195   if (caller_actual_parameters != method->size_of_parameters()) {
 196     assert(caller->is_interpreted_frame(), "adjusted caller_actual_parameters, but caller is not interpreter frame");
 197     Bytecode_invoke inv(caller->interpreter_frame_method(), caller->interpreter_frame_bci());
 198 
 199     if (is_bottom_frame) {
 200       assert(caller_actual_parameters == 0, "invalid adjusted caller_actual_parameters value for bottom frame");
 201       assert(inv.is_invokedynamic() || inv.is_invokehandle(), "adjusted caller_actual_parameters for bottom frame, but not invokedynamic/invokehandle");
 202     } else {
 203       assert(caller_actual_parameters == method->size_of_parameters()+1, "invalid adjusted caller_actual_parameters value");
 204       assert(!inv.is_invokedynamic() && MethodHandles::has_member_arg(inv.klass(), inv.name()), "adjusted caller_actual_parameters, but no member arg");
 205     }
 206   }
 207   if (caller->is_interpreted_frame()) {
 208     intptr_t* locals_base = (locals - method->max_locals()*Interpreter::stackElementWords + 1);
 209     locals_base = (intptr_t*)round_down((intptr_t)locals_base, StackAlignmentInBytes);
 210     assert(interpreter_frame->sender_sp() <= locals_base, "interpreter-to-interpreter frame chaining");
 211 
 212   } else if (caller->is_compiled_frame()) {
 213     assert(locals + 1 <= caller->unextended_sp(), "compiled-to-interpreter frame chaining");
 214 
 215   } else {
 216     assert(caller->is_entry_frame(), "should be");
 217     assert(locals + 1 <= caller->fp(), "entry-to-interpreter frame chaining");
 218   }
 219 #endif // ASSERT
 220 
 221 #else
 222   intptr_t* locals = interpreter_frame->sender_sp() + max_locals - 1;
 223 #endif // AARCH64
 224 
 225   interpreter_frame->interpreter_frame_set_locals(locals);
 226   BasicObjectLock* montop = interpreter_frame->interpreter_frame_monitor_begin();
 227   BasicObjectLock* monbot = montop - moncount;
 228   interpreter_frame->interpreter_frame_set_monitor_end(monbot);
 229 
 230   // Set last_sp
 231   intptr_t* stack_top = (intptr_t*) monbot  -
 232     tempcount*Interpreter::stackElementWords -
 233     popframe_extra_args;
 234 #ifdef AARCH64
 235   interpreter_frame->interpreter_frame_set_stack_top(stack_top);
 236 
 237   // We have to add extra reserved slots to max_stack. There are 3 users of the extra slots,
 238   // none of which are at the same time, so we just need to make sure there is enough room
 239   // for the biggest user:
 240   //   -reserved slot for exception handler
 241   //   -reserved slots for JSR292. Method::extra_stack_entries() is the size.
 242   //   -3 reserved slots so get_method_counters() can save some registers before call_VM().
 243   int max_stack = method->constMethod()->max_stack() + MAX2(3, Method::extra_stack_entries());
 244   intptr_t* extended_sp = (intptr_t*) monbot  -
 245     (max_stack * Interpreter::stackElementWords) -
 246     popframe_extra_args;
 247   extended_sp = (intptr_t*)round_down((intptr_t)extended_sp, StackAlignmentInBytes);
 248   interpreter_frame->interpreter_frame_set_extended_sp(extended_sp);
 249 #else
 250   interpreter_frame->interpreter_frame_set_last_sp(stack_top);
 251 #endif // AARCH64
 252 
 253   // All frames but the initial (oldest) interpreter frame we fill in have a
 254   // value for sender_sp that allows walking the stack but isn't
 255   // truly correct. Correct the value here.
 256 
 257 #ifdef AARCH64
 258   if (caller->is_interpreted_frame()) {
 259     intptr_t* sender_sp = (intptr_t*)round_down((intptr_t)caller->interpreter_frame_tos_address(), StackAlignmentInBytes);
 260     interpreter_frame->set_interpreter_frame_sender_sp(sender_sp);
 261 
 262   } else {
 263     // in case of non-interpreter caller sender_sp of the oldest frame is already
 264     // set to valid value
 265   }
 266 #else
 267   if (extra_locals != 0 &&
 268       interpreter_frame->sender_sp() == interpreter_frame->interpreter_frame_sender_sp() ) {
 269     interpreter_frame->set_interpreter_frame_sender_sp(caller->sp() + extra_locals);
 270   }
 271 #endif // AARCH64
 272 
 273   *interpreter_frame->interpreter_frame_cache_addr() =
 274     method->constants()->cache();
 275   *interpreter_frame->interpreter_frame_mirror_addr() =
 276     method->method_holder()->java_mirror();
 277 }