1 /*
   2  * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2015 SAP AG. All rights reserved.
   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 "interpreter/interpreter.hpp"
  28 #include "oops/constMethod.hpp"
  29 #include "oops/method.hpp"
  30 #include "runtime/frame.inline.hpp"
  31 #include "utilities/debug.hpp"
  32 #include "utilities/macros.hpp"
  33 
  34 // Size of interpreter code.  Increase if too small.  Interpreter will
  35 // fail with a guarantee ("not enough space for interpreter generation");
  36 // if too small.
  37 // Run with +PrintInterpreter to get the VM to print out the size.
  38 // Max size with JVMTI
  39 int TemplateInterpreter::InterpreterCodeSize = 230*K;
  40 
  41 int AbstractInterpreter::BasicType_as_index(BasicType type) {
  42   int i = 0;
  43   switch (type) {
  44     case T_BOOLEAN: i = 0; break;
  45     case T_CHAR   : i = 1; break;
  46     case T_BYTE   : i = 2; break;
  47     case T_SHORT  : i = 3; break;
  48     case T_INT    : i = 4; break;
  49     case T_LONG   : i = 5; break;
  50     case T_VOID   : i = 6; break;
  51     case T_FLOAT  : i = 7; break;
  52     case T_DOUBLE : i = 8; break;
  53     case T_OBJECT : i = 9; break;
  54     case T_ARRAY  : i = 9; break;
  55     default       : ShouldNotReachHere();
  56   }
  57   assert(0 <= i && i < AbstractInterpreter::number_of_result_handlers, "index out of bounds");
  58   return i;
  59 }
  60 
  61 // These should never be compiled since the interpreter will prefer
  62 // the compiled version to the intrinsic version.
  63 bool AbstractInterpreter::can_be_compiled(methodHandle m) {
  64   return !TemplateInterpreter::math_entry_available(method_kind(m));
  65 }
  66 
  67 // How much stack a method activation needs in stack slots.
  68 // We must calc this exactly like in generate_fixed_frame.
  69 // Note: This returns the conservative size assuming maximum alignment.
  70 int AbstractInterpreter::size_top_interpreter_activation(Method* method) {
  71   const int max_alignment_size = 2;
  72   const int abi_scratch = frame::abi_reg_args_size;
  73   return method->max_locals() + method->max_stack() +
  74          frame::interpreter_frame_monitor_size() + max_alignment_size + abi_scratch;
  75 }
  76 
  77 // Returns number of stackElementWords needed for the interpreter frame with the
  78 // given sections.
  79 // This overestimates the stack by one slot in case of alignments.
  80 int AbstractInterpreter::size_activation(int max_stack,
  81                                          int temps,
  82                                          int extra_args,
  83                                          int monitors,
  84                                          int callee_params,
  85                                          int callee_locals,
  86                                          bool is_top_frame) {
  87   // Note: This calculation must exactly parallel the frame setup
  88   // in TemplateInterpreterGenerator::generate_fixed_frame.
  89   assert(Interpreter::stackElementWords == 1, "sanity");
  90   const int max_alignment_space = StackAlignmentInBytes / Interpreter::stackElementSize;
  91   const int abi_scratch = is_top_frame ? (frame::abi_reg_args_size / Interpreter::stackElementSize) :
  92                                          (frame::abi_minframe_size / Interpreter::stackElementSize);
  93   const int size =
  94     max_stack                                                +
  95     (callee_locals - callee_params)                          +
  96     monitors * frame::interpreter_frame_monitor_size()       +
  97     max_alignment_space                                      +
  98     abi_scratch                                              +
  99     frame::ijava_state_size / Interpreter::stackElementSize;
 100 
 101   // Fixed size of an interpreter frame, align to 16-byte.
 102   return (size & -2);
 103 }
 104 
 105 // Fills a sceletal interpreter frame generated during deoptimizations.
 106 //
 107 // Parameters:
 108 //
 109 // interpreter_frame != NULL:
 110 //   set up the method, locals, and monitors.
 111 //   The frame interpreter_frame, if not NULL, is guaranteed to be the
 112 //   right size, as determined by a previous call to this method.
 113 //   It is also guaranteed to be walkable even though it is in a skeletal state
 114 //
 115 // is_top_frame == true:
 116 //   We're processing the *oldest* interpreter frame!
 117 //
 118 // pop_frame_extra_args:
 119 //   If this is != 0 we are returning to a deoptimized frame by popping
 120 //   off the callee frame. We want to re-execute the call that called the
 121 //   callee interpreted, but since the return to the interpreter would pop
 122 //   the arguments off advance the esp by dummy popframe_extra_args slots.
 123 //   Popping off those will establish the stack layout as it was before the call.
 124 //
 125 void AbstractInterpreter::layout_activation(Method* method,
 126                                             int tempcount,
 127                                             int popframe_extra_args,
 128                                             int moncount,
 129                                             int caller_actual_parameters,
 130                                             int callee_param_count,
 131                                             int callee_locals_count,
 132                                             frame* caller,
 133                                             frame* interpreter_frame,
 134                                             bool is_top_frame,
 135                                             bool is_bottom_frame) {
 136 
 137   const int abi_scratch = is_top_frame ? (frame::abi_reg_args_size / Interpreter::stackElementSize) :
 138                                          (frame::abi_minframe_size / Interpreter::stackElementSize);
 139 
 140   intptr_t* locals_base  = (caller->is_interpreted_frame()) ?
 141     caller->interpreter_frame_esp() + caller_actual_parameters :
 142     caller->sp() + method->max_locals() - 1 + (frame::abi_minframe_size / Interpreter::stackElementSize);
 143 
 144   intptr_t* monitor_base = caller->sp() - frame::ijava_state_size / Interpreter::stackElementSize;
 145   intptr_t* monitor      = monitor_base - (moncount * frame::interpreter_frame_monitor_size());
 146   intptr_t* esp_base     = monitor - 1;
 147   intptr_t* esp          = esp_base - tempcount - popframe_extra_args;
 148   intptr_t* sp           = (intptr_t *) (((intptr_t) (esp_base - callee_locals_count + callee_param_count - method->max_stack()- abi_scratch)) & -StackAlignmentInBytes);
 149   intptr_t* sender_sp    = caller->sp() + (frame::abi_minframe_size - frame::abi_reg_args_size) / Interpreter::stackElementSize;
 150   intptr_t* top_frame_sp = is_top_frame ? sp : sp + (frame::abi_minframe_size - frame::abi_reg_args_size) / Interpreter::stackElementSize;
 151 
 152   interpreter_frame->interpreter_frame_set_method(method);
 153   interpreter_frame->interpreter_frame_set_locals(locals_base);
 154   interpreter_frame->interpreter_frame_set_cpcache(method->constants()->cache());
 155   interpreter_frame->interpreter_frame_set_esp(esp);
 156   interpreter_frame->interpreter_frame_set_monitor_end((BasicObjectLock *)monitor);
 157   interpreter_frame->interpreter_frame_set_top_frame_sp(top_frame_sp);
 158   if (!is_bottom_frame) {
 159     interpreter_frame->interpreter_frame_set_sender_sp(sender_sp);
 160   }
 161 }
 162 
 163 // Support abs and sqrt like in compiler.
 164 // For others we can use a normal (native) entry.
 165 
 166 bool TemplateInterpreter::math_entry_available(AbstractInterpreter::MethodKind kind) {
 167   if (!InlineIntrinsics) return false;
 168 
 169   return ((kind==Interpreter::java_lang_math_sqrt && VM_Version::has_fsqrt()) ||
 170           (kind==Interpreter::java_lang_math_abs));
 171 }
 172 
 173