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