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/align.hpp"
  36 #include "utilities/macros.hpp"
  37 
  38 int AbstractInterpreter::BasicType_as_index(BasicType type) {
  39   int i = 0;
  40   switch (type) {
  41     case T_VOID   : i = 0; break;
  42     case T_BOOLEAN: i = 1; break;
  43     case T_CHAR   : i = 2; break;
  44     case T_BYTE   : i = 3; break;
  45     case T_SHORT  : i = 4; break;
  46     case T_INT    : i = 5; break;
  47     case T_OBJECT : // fall through
  48     case T_ARRAY  : i = 6; break;
  49     case T_LONG   : i = 7; break;
  50     case T_FLOAT  : i = 8; break;
  51     case T_DOUBLE : i = 9; break;
  52     default       : ShouldNotReachHere();
  53   }
  54   assert(0 <= i && i < AbstractInterpreter::number_of_result_handlers, "index out of bounds");
  55   return i;
  56 }
  57 
  58 // How much stack a method activation needs in words.
  59 int AbstractInterpreter::size_top_interpreter_activation(Method* method) {
  60   const int stub_code = 12;  // see generate_call_stub
  61   // Save space for one monitor to get into the interpreted method in case
  62   // the method is synchronized
  63   int monitor_size    = method->is_synchronized() ?
  64                                 1*frame::interpreter_frame_monitor_size() : 0;
  65 
  66   // total overhead size: monitor_size + (sender SP, thru expr stack bottom).
  67   // be sure to change this if you add/subtract anything to/from the overhead area
  68   const int overhead_size = monitor_size +
  69                             (frame::sender_sp_offset - frame::interpreter_frame_initial_sp_offset);
  70   const int method_stack = (method->max_locals() + method->max_stack()) *
  71                            Interpreter::stackElementWords;
  72   return overhead_size + method_stack + stub_code;
  73 }
  74 
  75 // asm based interpreter deoptimization helpers
  76 int AbstractInterpreter::size_activation(int max_stack,
  77                                          int tempcount,
  78                                          int extra_args,
  79                                          int moncount,
  80                                          int callee_param_count,
  81                                          int callee_locals,
  82                                          bool is_top_frame) {
  83   // Note: This calculation must exactly parallel the frame setup
  84   // in TemplateInterpreterGenerator::generate_fixed_frame.
  85   // fixed size of an interpreter frame:
  86   int overhead = frame::sender_sp_offset - frame::interpreter_frame_initial_sp_offset;
  87 
  88   // Our locals were accounted for by the caller (or last_frame_adjust on the transistion)
  89   // Since the callee parameters already account for the callee's params we only need to account for
  90   // the extra locals.
  91 
  92   int size = overhead +
  93          ((callee_locals - callee_param_count)*Interpreter::stackElementWords) +
  94          (moncount*frame::interpreter_frame_monitor_size()) +
  95          tempcount*Interpreter::stackElementWords + extra_args;
  96 
  97 
  98   return size;
  99 }
 100 
 101 void AbstractInterpreter::layout_activation(Method* method,
 102                                             int tempcount,
 103                                             int popframe_extra_args,
 104                                             int moncount,
 105                                             int caller_actual_parameters,
 106                                             int callee_param_count,
 107                                             int callee_locals,
 108                                             frame* caller,
 109                                             frame* interpreter_frame,
 110                                             bool is_top_frame,
 111                                             bool is_bottom_frame) {
 112 
 113   // Set up the method, locals, and monitors.
 114   // The frame interpreter_frame is guaranteed to be the right size,
 115   // as determined by a previous call to the size_activation() method.
 116   // It is also guaranteed to be walkable even though it is in a skeletal state
 117   // NOTE: return size is in words not bytes
 118 
 119   // fixed size of an interpreter frame:
 120   int max_locals = method->max_locals() * Interpreter::stackElementWords;
 121   int extra_locals = (method->max_locals() - method->size_of_parameters()) * Interpreter::stackElementWords;
 122 
 123 #ifdef ASSERT
 124   assert(caller->sp() == interpreter_frame->sender_sp(), "Frame not properly walkable");
 125 #endif
 126 
 127   interpreter_frame->interpreter_frame_set_method(method);
 128   // NOTE the difference in using sender_sp and interpreter_frame_sender_sp
 129   // interpreter_frame_sender_sp is the original sp of the caller (the unextended_sp)
 130   // and sender_sp is (fp + sender_sp_offset*wordSize)
 131 
 132   intptr_t* locals = interpreter_frame->sender_sp() + max_locals - 1;
 133 
 134   interpreter_frame->interpreter_frame_set_locals(locals);
 135   BasicObjectLock* montop = interpreter_frame->interpreter_frame_monitor_begin();
 136   BasicObjectLock* monbot = montop - moncount;
 137   interpreter_frame->interpreter_frame_set_monitor_end(monbot);
 138 
 139   // Set last_sp
 140   intptr_t* stack_top = (intptr_t*) monbot  -
 141     tempcount*Interpreter::stackElementWords -
 142     popframe_extra_args;
 143   interpreter_frame->interpreter_frame_set_last_sp(stack_top);
 144 
 145   // All frames but the initial (oldest) interpreter frame we fill in have a
 146   // value for sender_sp that allows walking the stack but isn't
 147   // truly correct. Correct the value here.
 148 
 149   if (extra_locals != 0 &&
 150       interpreter_frame->sender_sp() == interpreter_frame->interpreter_frame_sender_sp() ) {
 151     interpreter_frame->set_interpreter_frame_sender_sp(caller->sp() + extra_locals);
 152   }
 153 
 154   *interpreter_frame->interpreter_frame_cache_addr() =
 155     method->constants()->cache();
 156   *interpreter_frame->interpreter_frame_mirror_addr() =
 157     method->method_holder()->java_mirror();
 158 }