1 /*
   2  * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2016 SAP SE. 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 int AbstractInterpreter::BasicType_as_index(BasicType type) {
  35   int i = 0;
  36   switch (type) {
  37     case T_BOOLEAN: i = 0; break;
  38     case T_CHAR   : i = 1; break;
  39     case T_BYTE   : i = 2; break;
  40     case T_SHORT  : i = 3; break;
  41     case T_INT    : i = 4; break;
  42     case T_LONG   : i = 5; break;
  43     case T_VOID   : i = 6; break;
  44     case T_FLOAT  : i = 7; break;
  45     case T_DOUBLE : i = 8; break;
  46     case T_OBJECT : i = 9; break;
  47     case T_ARRAY  : i = 9; break;
  48     default       : ShouldNotReachHere();
  49   }
  50   assert(0 <= i && i < AbstractInterpreter::number_of_result_handlers, "index out of bounds");
  51   return i;
  52 }
  53 
  54 // How much stack a method top interpreter activation needs in words.
  55 int AbstractInterpreter::size_top_interpreter_activation(Method* method) {
  56 
  57   // We have to size the following 2 frames:
  58   //
  59   //   [TOP_IJAVA_FRAME_ABI]
  60   //   [ENTRY_FRAME]
  61   //
  62   // This expands to (see frame_s390.hpp):
  63   //
  64   //   [TOP_IJAVA_FRAME_ABI]
  65   //   [operand stack]                 > stack
  66   //   [monitors]      (optional)      > monitors
  67   //   [IJAVA_STATE]                   > interpreter_state
  68   //   [PARENT_IJAVA_FRAME_ABI]
  69   //   [callee's locals w/o arguments] \ locals
  70   //   [outgoing arguments]            /
  71   //   [ENTRY_FRAME_LOCALS]
  72 
  73   int locals = method->max_locals() * BytesPerWord;
  74   int interpreter_state = frame::z_ijava_state_size;
  75 
  76   int stack = method->max_stack() * BytesPerWord;
  77   int monitors = method->is_synchronized() ? frame::interpreter_frame_monitor_size_in_bytes() : 0;
  78 
  79   int total_bytes =
  80     frame::z_top_ijava_frame_abi_size +
  81     stack +
  82     monitors +
  83     interpreter_state +
  84     frame::z_parent_ijava_frame_abi_size +
  85     locals +
  86     frame::z_entry_frame_locals_size;
  87 
  88   return (total_bytes/BytesPerWord);
  89 }
  90 
  91 // Returns number of stackElementWords needed for the interpreter frame with the
  92 // given sections.
  93 // This overestimates the stack by one slot in case of alignments.
  94 int AbstractInterpreter::size_activation(int max_stack,
  95                                          int temps,
  96                                          int extra_args,
  97                                          int monitors,
  98                                          int callee_params,
  99                                          int callee_locals,
 100                                          bool is_top_frame) {
 101   // Note: This calculation must exactly parallel the frame setup
 102   // in AbstractInterpreterGenerator::generate_method_entry.
 103 
 104   assert((Interpreter::stackElementSize == frame::alignment_in_bytes), "must align frame size");
 105   const int abi_scratch = is_top_frame ? (frame::z_top_ijava_frame_abi_size    / Interpreter::stackElementSize) :
 106                                          (frame::z_parent_ijava_frame_abi_size / Interpreter::stackElementSize);
 107 
 108   const int size =
 109     max_stack                                                 +
 110     (callee_locals - callee_params)                           + // Already counted in max_stack().
 111     monitors * frame::interpreter_frame_monitor_size()        +
 112     abi_scratch                                               +
 113     frame::z_ijava_state_size / Interpreter::stackElementSize;
 114 
 115   // Fixed size of an interpreter frame.
 116   return size;
 117 }
 118 
 119 // Fills a sceletal interpreter frame generated during deoptimizations.
 120 //
 121 // Parameters:
 122 //
 123 // interpreter_frame != NULL:
 124 //   set up the method, locals, and monitors.
 125 //   The frame interpreter_frame, if not NULL, is guaranteed to be the
 126 //   right size, as determined by a previous call to this method.
 127 //   It is also guaranteed to be walkable even though it is in a skeletal state
 128 //
 129 // is_top_frame == true:
 130 //   We're processing the *oldest* interpreter frame!
 131 //
 132 // pop_frame_extra_args:
 133 //   If this is != 0 we are returning to a deoptimized frame by popping
 134 //   off the callee frame. We want to re-execute the call that called the
 135 //   callee interpreted, but since the return to the interpreter would pop
 136 //   the arguments off advance the esp by dummy popframe_extra_args slots.
 137 //   Popping off those will establish the stack layout as it was before the call.
 138 //
 139 
 140 void AbstractInterpreter::layout_activation(Method* method,
 141                                             int tempcount,
 142                                             int popframe_extra_args,
 143                                             int moncount,
 144                                             int caller_actual_parameters,
 145                                             int callee_param_count,
 146                                             int callee_locals_count,
 147                                             frame* caller,
 148                                             frame* interpreter_frame,
 149                                             bool is_top_frame,
 150                                             bool is_bottom_frame) {
 151   // TOP_IJAVA_FRAME:
 152   //
 153   //    0 [TOP_IJAVA_FRAME_ABI]         -+
 154   //   16 [operand stack]                | size
 155   //      [monitors]      (optional)     |
 156   //      [IJAVA_STATE]                 -+
 157   //      Note: own locals are located in the caller frame.
 158   //
 159   // PARENT_IJAVA_FRAME:
 160   //
 161   //    0 [PARENT_IJAVA_FRAME_ABI]                    -+
 162   //      [callee's locals w/o arguments]              |
 163   //      [outgoing arguments]                         | size
 164   //      [used part of operand stack w/o arguments]   |
 165   //      [monitors]      (optional)                   |
 166   //      [IJAVA_STATE]                               -+
 167   //
 168 
 169   // Now we know our caller, calc the exact frame layout and size
 170   // z_ijava_state->locals - i*BytesPerWord points to i-th Java local (i starts at 0).
 171   intptr_t* locals_base = (caller->is_interpreted_frame())
 172     ? (caller->interpreter_frame_tos_address() + caller_actual_parameters - 1)
 173     : (caller->sp()                            + method->max_locals()     - 1 +
 174        frame::z_parent_ijava_frame_abi_size / Interpreter::stackElementSize);
 175 
 176   intptr_t* monitor_base = (intptr_t*)((address)interpreter_frame->fp() - frame::z_ijava_state_size);
 177   intptr_t* monitor      = monitor_base - (moncount * frame::interpreter_frame_monitor_size());
 178   intptr_t* operand_stack_base = monitor;
 179   intptr_t* tos          = operand_stack_base - tempcount - popframe_extra_args;
 180   intptr_t* top_frame_sp =
 181     operand_stack_base - method->max_stack() - frame::z_top_ijava_frame_abi_size / Interpreter::stackElementSize;
 182   intptr_t* sender_sp;
 183   if (caller->is_interpreted_frame()) {
 184     sender_sp = caller->interpreter_frame_top_frame_sp();
 185   } else if (caller->is_compiled_frame()) {
 186     sender_sp = caller->fp() - caller->cb()->frame_size();
 187     // The bottom frame's sender_sp is its caller's unextended_sp.
 188     // It was already set when its skeleton was pushed (see push_skeleton_frames()).
 189     // Note: the unextended_sp is required by nmethod::orig_pc_addr().
 190     assert(is_bottom_frame && (sender_sp == caller->unextended_sp()),
 191            "must initialize sender_sp of bottom skeleton frame when pushing it");
 192   } else {
 193     assert(caller->is_entry_frame(), "is there a new frame type??");
 194     sender_sp = caller->sp(); // Call_stub only uses it's fp.
 195   }
 196 
 197   interpreter_frame->interpreter_frame_set_method(method);
 198   interpreter_frame->interpreter_frame_set_mirror(method->method_holder()->java_mirror());
 199   interpreter_frame->interpreter_frame_set_locals(locals_base);
 200   interpreter_frame->interpreter_frame_set_monitor_end((BasicObjectLock *)monitor);
 201   *interpreter_frame->interpreter_frame_cache_addr() = method->constants()->cache();
 202   interpreter_frame->interpreter_frame_set_tos_address(tos);
 203   interpreter_frame->interpreter_frame_set_sender_sp(sender_sp);
 204   interpreter_frame->interpreter_frame_set_top_frame_sp(top_frame_sp);
 205 }