1 /*
   2  * Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2014, Red Hat Inc. 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/align.hpp"
  32 #include "utilities/debug.hpp"
  33 #include "utilities/macros.hpp"
  34 
  35 
  36 int AbstractInterpreter::BasicType_as_index(BasicType type) {
  37   int i = 0;
  38   switch (type) {
  39     case T_BOOLEAN: i = 0; break;
  40     case T_CHAR   : i = 1; break;
  41     case T_BYTE   : i = 2; break;
  42     case T_SHORT  : i = 3; break;
  43     case T_INT    : i = 4; break;
  44     case T_LONG   : i = 5; break;
  45     case T_VOID   : i = 6; break;
  46     case T_FLOAT  : i = 7; break;
  47     case T_DOUBLE : i = 8; break;
  48     case T_OBJECT : i = 9; break;
  49     case T_ARRAY  : i = 9; break;
  50     case T_VALUETYPE : i = 10; break;
  51     default       : ShouldNotReachHere();
  52   }
  53   assert(0 <= i && i < AbstractInterpreter::number_of_result_handlers,
  54          "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 entry_size = frame::interpreter_frame_monitor_size();
  61 
  62   // total overhead size: entry_size + (saved rfp thru expr stack
  63   // bottom).  be sure to change this if you add/subtract anything
  64   // to/from the overhead area
  65   const int overhead_size =
  66     -(frame::interpreter_frame_initial_sp_offset) + entry_size;
  67 
  68   const int stub_code = frame::entry_frame_after_call_words;
  69   const int method_stack = (method->max_locals() + method->max_stack()) *
  70                            Interpreter::stackElementWords;
  71   return (overhead_size + method_stack + stub_code);
  72 }
  73 
  74 // asm based interpreter deoptimization helpers
  75 int AbstractInterpreter::size_activation(int max_stack,
  76                                          int temps,
  77                                          int extra_args,
  78                                          int monitors,
  79                                          int callee_params,
  80                                          int callee_locals,
  81                                          bool is_top_frame) {
  82   // Note: This calculation must exactly parallel the frame setup
  83   // in TemplateInterpreterGenerator::generate_method_entry.
  84 
  85   // fixed size of an interpreter frame:
  86   int overhead = frame::sender_sp_offset -
  87                  frame::interpreter_frame_initial_sp_offset;
  88   // Our locals were accounted for by the caller (or last_frame_adjust
  89   // on the transistion) Since the callee parameters already account
  90   // for the callee's params we only need to account for the extra
  91   // locals.
  92   int size = overhead +
  93          (callee_locals - callee_params) +
  94          monitors * frame::interpreter_frame_monitor_size() +
  95          // On the top frame, at all times SP <= ESP, and SP is
  96          // 16-aligned.  We ensure this by adjusting SP on method
  97          // entry and re-entry to allow room for the maximum size of
  98          // the expression stack.  When we call another method we bump
  99          // SP so that no stack space is wasted.  So, only on the top
 100          // frame do we need to allow max_stack words.
 101          (is_top_frame ? max_stack : temps + extra_args);
 102 
 103   // On AArch64 we always keep the stack pointer 16-aligned, so we
 104   // must round up here.
 105   size = align_up(size, 2);
 106 
 107   return size;
 108 }
 109 
 110 void AbstractInterpreter::layout_activation(Method* method,
 111                                             int tempcount,
 112                                             int popframe_extra_args,
 113                                             int moncount,
 114                                             int caller_actual_parameters,
 115                                             int callee_param_count,
 116                                             int callee_locals,
 117                                             frame* caller,
 118                                             frame* interpreter_frame,
 119                                             bool is_top_frame,
 120                                             bool is_bottom_frame) {
 121   // The frame interpreter_frame is guaranteed to be the right size,
 122   // as determined by a previous call to the size_activation() method.
 123   // It is also guaranteed to be walkable even though it is in a
 124   // skeletal state
 125 
 126   int max_locals = method->max_locals() * Interpreter::stackElementWords;
 127   int extra_locals = (method->max_locals() - method->size_of_parameters()) *
 128     Interpreter::stackElementWords;
 129 
 130 #ifdef ASSERT
 131   assert(caller->sp() == interpreter_frame->sender_sp(), "Frame not properly walkable");
 132 #endif
 133 
 134   interpreter_frame->interpreter_frame_set_method(method);
 135   // NOTE the difference in using sender_sp and
 136   // interpreter_frame_sender_sp interpreter_frame_sender_sp is
 137   // the original sp of the caller (the unextended_sp) and
 138   // sender_sp is fp+8/16 (32bit/64bit) XXX
 139   intptr_t* locals = interpreter_frame->sender_sp() + max_locals - 1;
 140 
 141 #ifdef ASSERT
 142   if (caller->is_interpreted_frame()) {
 143     assert(locals < caller->fp() + frame::interpreter_frame_initial_sp_offset, "bad placement");
 144   }
 145 #endif
 146 
 147   interpreter_frame->interpreter_frame_set_locals(locals);
 148   BasicObjectLock* montop = interpreter_frame->interpreter_frame_monitor_begin();
 149   BasicObjectLock* monbot = montop - moncount;
 150   interpreter_frame->interpreter_frame_set_monitor_end(monbot);
 151 
 152   // Set last_sp
 153   intptr_t*  esp = (intptr_t*) monbot -
 154     tempcount*Interpreter::stackElementWords -
 155     popframe_extra_args;
 156   interpreter_frame->interpreter_frame_set_last_sp(esp);
 157 
 158   // All frames but the initial (oldest) interpreter frame we fill in have
 159   // a value for sender_sp that allows walking the stack but isn't
 160   // truly correct. Correct the value here.
 161   if (extra_locals != 0 &&
 162       interpreter_frame->sender_sp() ==
 163       interpreter_frame->interpreter_frame_sender_sp()) {
 164     interpreter_frame->set_interpreter_frame_sender_sp(caller->sp() +
 165                                                        extra_locals);
 166   }
 167   *interpreter_frame->interpreter_frame_cache_addr() =
 168     method->constants()->cache();
 169   *interpreter_frame->interpreter_frame_mirror_addr() =
 170     method->method_holder()->java_mirror();
 171 }