1 /*
   2  * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2014, Red Hat Inc. All rights reserved.
   4  * Copyright (c) 2015, Linaro Ltd. All rights reserved.
   5  * Copyright (c) 2015-2018, Azul Systems, Inc. All rights reserved.
   6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   7  *
   8  * This code is free software; you can redistribute it and/or modify it
   9  * under the terms of the GNU General Public License version 2 only, as
  10  * published by the Free Software Foundation.
  11  *
  12  * This code is distributed in the hope that it will be useful, but WITHOUT
  13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15  * version 2 for more details (a copy is included in the LICENSE file that
  16  * accompanied this code).
  17  *
  18  * You should have received a copy of the GNU General Public License version
  19  * 2 along with this work; if not, write to the Free Software Foundation,
  20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  21  *
  22  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  23  * or visit www.oracle.com if you need additional information or have any
  24  * questions.
  25  *
  26  */
  27 
  28 #include "precompiled.hpp"
  29 #include "oops/method.hpp"
  30 #include "runtime/frame.inline.hpp"
  31 #include "utilities/debug.hpp"
  32 
  33 int AbstractInterpreter::BasicType_as_index(BasicType type) {
  34   int i = 0;
  35   switch (type) {
  36     case T_BOOLEAN: i = 0; break;
  37     case T_CHAR   : i = 1; break;
  38     case T_BYTE   : i = 2; break;
  39     case T_SHORT  : i = 3; break;
  40     case T_INT    : i = 4; break;
  41     case T_LONG   : i = 5; break;
  42     case T_VOID   : i = 6; break;
  43     case T_FLOAT  : i = 7; break;
  44     case T_DOUBLE : i = 8; break;
  45     case T_OBJECT : i = 9; break;
  46     case T_ARRAY  : i = 9; break;
  47     default       : ShouldNotReachHere();
  48   }
  49   assert(0 <= i && i < AbstractInterpreter::number_of_result_handlers,
  50          "index out of bounds");
  51   return i;
  52 }
  53 
  54 // How much stack a method activation needs in words.
  55 int AbstractInterpreter::size_top_interpreter_activation(Method* method) {
  56   const int entry_size = frame::interpreter_frame_monitor_size();
  57 
  58   // total overhead size: entry_size + (saved rfp thru expr stack
  59   // bottom).  be sure to change this if you add/subtract anything
  60   // to/from the overhead area
  61   const int overhead_size =
  62     -(frame::get_interpreter_frame_initial_sp_offset()) + entry_size;
  63 
  64   const int stub_code = frame::get_entry_frame_after_call_words();
  65   const int method_stack = (method->max_locals() + method->max_stack()) *
  66                            Interpreter::stackElementWords;
  67   return (overhead_size + method_stack + stub_code);
  68 }
  69 
  70 // asm based interpreter deoptimization helpers
  71 int AbstractInterpreter::size_activation(int max_stack,
  72                                          int temps,
  73                                          int extra_args,
  74                                          int monitors,
  75                                          int callee_params,
  76                                          int callee_locals,
  77                                          bool is_top_frame) {
  78   // Note: This calculation must exactly parallel the frame setup
  79   // in TemplateInterpreterGenerator::generate_method_entry.
  80 
  81   // fixed size of an interpreter frame:
  82   int overhead = frame::sender_sp_offset -
  83                  frame::get_interpreter_frame_initial_sp_offset();
  84   // Our locals were accounted for by the caller (or last_frame_adjust
  85   // on the transistion) Since the callee parameters already account
  86   // for the callee's params we only need to account for the extra
  87   // locals.
  88   int size = overhead +
  89          (callee_locals - callee_params)*Interpreter::stackElementWords +
  90          monitors * frame::interpreter_frame_monitor_size() +
  91          temps* Interpreter::stackElementWords + extra_args;
  92 
  93   // On AArch32 we always keep the stack pointer 16-aligned, so we
  94   // must round up here.
  95   size = align_up(size, 2);
  96 
  97   return size;
  98 }
  99 
 100 void AbstractInterpreter::layout_activation(Method* method,
 101                                             int tempcount,
 102                                             int popframe_extra_args,
 103                                             int moncount,
 104                                             int caller_actual_parameters,
 105                                             int callee_param_count,
 106                                             int callee_locals,
 107                                             frame* caller,
 108                                             frame* interpreter_frame,
 109                                             bool is_top_frame,
 110                                             bool is_bottom_frame) {
 111   // The frame interpreter_frame is guaranteed to be the right size,
 112   // as determined by a previous call to the size_activation() method.
 113   // It is also guaranteed to be walkable even though it is in a
 114   // skeletal state
 115 
 116   int max_locals = method->max_locals() * Interpreter::stackElementWords;
 117   int extra_locals = (method->max_locals() - method->size_of_parameters()) *
 118     Interpreter::stackElementWords;
 119 
 120 #ifdef ASSERT
 121   assert(caller->sp() == interpreter_frame->sender_sp(), "Frame not properly walkable");
 122 #endif
 123 
 124   interpreter_frame->interpreter_frame_set_method(method);
 125   // NOTE the difference in using sender_sp and
 126   // interpreter_frame_sender_sp interpreter_frame_sender_sp is
 127   // the original sp of the caller (the unextended_sp) and
 128   // sender_sp is fp+8/16 (32bit/64bit) XXX
 129   intptr_t* locals = interpreter_frame->sender_sp() + max_locals - 1;
 130 
 131 #ifdef ASSERT
 132   if (caller->is_interpreted_frame()) {
 133     assert(locals < caller->fp() + frame::get_interpreter_frame_initial_sp_offset(), "bad placement");
 134   }
 135 #endif
 136 
 137   interpreter_frame->interpreter_frame_set_locals(locals);
 138   BasicObjectLock* montop = interpreter_frame->interpreter_frame_monitor_begin();
 139   BasicObjectLock* monbot = montop - moncount;
 140   interpreter_frame->interpreter_frame_set_monitor_end(monbot);
 141 
 142   // Set last_sp
 143   intptr_t*  last_sp = (intptr_t*) monbot -
 144     tempcount*Interpreter::stackElementWords -
 145     popframe_extra_args;
 146   interpreter_frame->interpreter_frame_set_last_sp(last_sp);
 147 
 148   // All frames but the initial (oldest) interpreter frame we fill in have
 149   // a value for sender_sp that allows walking the stack but isn't
 150   // truly correct. Correct the value here.
 151   if (extra_locals != 0 &&
 152       interpreter_frame->sender_sp() ==
 153       interpreter_frame->interpreter_frame_sender_sp()) {
 154     interpreter_frame->set_interpreter_frame_sender_sp(caller->sp() +
 155                                                        extra_locals);
 156   }
 157   *interpreter_frame->interpreter_frame_cache_addr() =
 158     method->constants()->cache();
 159   *interpreter_frame->interpreter_frame_mirror_addr() =
 160     method->method_holder()->java_mirror();
 161 }