1 /*
   2  * Copyright (c) 1997, 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 "interpreter/interpreter.hpp"
  27 #include "oops/constMethod.hpp"
  28 #include "oops/klass.inline.hpp"
  29 #include "oops/method.hpp"
  30 #include "runtime/arguments.hpp"
  31 #include "runtime/frame.inline.hpp"
  32 #include "runtime/synchronizer.hpp"
  33 #include "utilities/align.hpp"
  34 #include "utilities/macros.hpp"
  35 
  36 
  37 int AbstractInterpreter::BasicType_as_index(BasicType type) {
  38   int i = 0;
  39   switch (type) {
  40     case T_BOOLEAN: i = 0; break;
  41     case T_CHAR   : i = 1; break;
  42     case T_BYTE   : i = 2; break;
  43     case T_SHORT  : i = 3; break;
  44     case T_INT    : i = 4; break;
  45     case T_LONG   : i = 5; break;
  46     case T_VOID   : i = 6; break;
  47     case T_FLOAT  : i = 7; break;
  48     case T_DOUBLE : i = 8; break;
  49     case T_OBJECT : i = 9; break;
  50     case T_ARRAY  : i = 9; break;
  51     default       : ShouldNotReachHere();
  52   }
  53   assert(0 <= i && i < AbstractInterpreter::number_of_result_handlers, "index out of bounds");
  54   return i;
  55 }
  56 
  57 static int size_activation_helper(int callee_extra_locals, int max_stack, int monitor_size) {
  58 
  59   // Figure out the size of an interpreter frame (in words) given that we have a fully allocated
  60   // expression stack, the callee will have callee_extra_locals (so we can account for
  61   // frame extension) and monitor_size for monitors. Basically we need to calculate
  62   // this exactly like generate_fixed_frame/generate_compute_interpreter_state.
  63   //
  64   //
  65   // The big complicating thing here is that we must ensure that the stack stays properly
  66   // aligned. This would be even uglier if monitor size wasn't modulo what the stack
  67   // needs to be aligned for). We are given that the sp (fp) is already aligned by
  68   // the caller so we must ensure that it is properly aligned for our callee.
  69   //
  70   const int rounded_vm_local_words =
  71       align_up((int)frame::interpreter_frame_vm_local_words,WordsPerLong);
  72   // callee_locals and max_stack are counts, not the size in frame.
  73   const int locals_size =
  74       align_up(callee_extra_locals * Interpreter::stackElementWords, WordsPerLong);
  75   const int max_stack_words = max_stack * Interpreter::stackElementWords;
  76   return (align_up((max_stack_words
  77                    + rounded_vm_local_words
  78                    + frame::memory_parameter_word_sp_offset), WordsPerLong)
  79                    // already rounded
  80                    + locals_size + monitor_size);
  81 }
  82 
  83 // How much stack a method top interpreter activation needs in words.
  84 int AbstractInterpreter::size_top_interpreter_activation(Method* method) {
  85 
  86   // See call_stub code
  87   int call_stub_size  = align_up(7 + frame::memory_parameter_word_sp_offset,
  88                                  WordsPerLong);    // 7 + register save area
  89 
  90   // Save space for one monitor to get into the interpreted method in case
  91   // the method is synchronized
  92   int monitor_size    = method->is_synchronized() ?
  93                                 1*frame::interpreter_frame_monitor_size() : 0;
  94   return size_activation_helper(method->max_locals(), method->max_stack(),
  95                                 monitor_size) + call_stub_size;
  96 }
  97 
  98 int AbstractInterpreter::size_activation(int max_stack,
  99                                          int temps,
 100                                          int extra_args,
 101                                          int monitors,
 102                                          int callee_params,
 103                                          int callee_locals,
 104                                          bool is_top_frame) {
 105   // Note: This calculation must exactly parallel the frame setup
 106   // in TemplateInterpreterGenerator::generate_fixed_frame.
 107 
 108   int monitor_size           = monitors * frame::interpreter_frame_monitor_size();
 109 
 110   assert(is_aligned(monitor_size, WordsPerLong), "must align");
 111 
 112   //
 113   // Note: if you look closely this appears to be doing something much different
 114   // than generate_fixed_frame. What is happening is this. On sparc we have to do
 115   // this dance with interpreter_sp_adjustment because the window save area would
 116   // appear just below the bottom (tos) of the caller's java expression stack. Because
 117   // the interpreter want to have the locals completely contiguous generate_fixed_frame
 118   // will adjust the caller's sp for the "extra locals" (max_locals - parameter_size).
 119   // Now in generate_fixed_frame the extension of the caller's sp happens in the callee.
 120   // In this code the opposite occurs the caller adjusts it's own stack base on the callee.
 121   // This is mostly ok but it does cause a problem when we get to the initial frame (the oldest)
 122   // because the oldest frame would have adjust its callers frame and yet that frame
 123   // already exists and isn't part of this array of frames we are unpacking. So at first
 124   // glance this would seem to mess up that frame. However Deoptimization::fetch_unroll_info_helper()
 125   // will after it calculates all of the frame's on_stack_size()'s will then figure out the
 126   // amount to adjust the caller of the initial (oldest) frame and the calculation will all
 127   // add up. It does seem like it simpler to account for the adjustment here (and remove the
 128   // callee... parameters here). However this would mean that this routine would have to take
 129   // the caller frame as input so we could adjust its sp (and set it's interpreter_sp_adjustment)
 130   // and run the calling loop in the reverse order. This would also would appear to mean making
 131   // this code aware of what the interactions are when that initial caller fram was an osr or
 132   // other adapter frame. deoptimization is complicated enough and  hard enough to debug that
 133   // there is no sense in messing working code.
 134   //
 135 
 136   int rounded_cls = align_up((callee_locals - callee_params), WordsPerLong);
 137   assert(is_aligned(rounded_cls, WordsPerLong), "must align");
 138 
 139   int raw_frame_size = size_activation_helper(rounded_cls, max_stack, monitor_size);
 140 
 141   return raw_frame_size;
 142 }
 143 
 144 void AbstractInterpreter::layout_activation(Method* method,
 145                                             int tempcount,
 146                                             int popframe_extra_args,
 147                                             int moncount,
 148                                             int caller_actual_parameters,
 149                                             int callee_param_count,
 150                                             int callee_local_count,
 151                                             frame* caller,
 152                                             frame* interpreter_frame,
 153                                             bool is_top_frame,
 154                                             bool is_bottom_frame) {
 155   // Set up the following variables:
 156   //   - Lmethod
 157   //   - Llocals
 158   //   - Lmonitors (to the indicated number of monitors)
 159   //   - Lesp (to the indicated number of temps)
 160   // The frame caller on entry is a description of the caller of the
 161   // frame we are about to layout. We are guaranteed that we will be
 162   // able to fill in a new interpreter frame as its callee (i.e. the
 163   // stack space is allocated and the amount was determined by an
 164   // earlier call to the size_activation() method).  On return caller
 165   // while describe the interpreter frame we just layed out.
 166 
 167   // The skeleton frame must already look like an interpreter frame
 168   // even if not fully filled out.
 169   assert(interpreter_frame->is_interpreted_frame(), "Must be interpreted frame");
 170 
 171   int rounded_vm_local_words = align_up((int)frame::interpreter_frame_vm_local_words,WordsPerLong);
 172   int monitor_size           = moncount * frame::interpreter_frame_monitor_size();
 173   assert(is_aligned(monitor_size, WordsPerLong), "must align");
 174 
 175   intptr_t* fp = interpreter_frame->fp();
 176 
 177   JavaThread* thread = JavaThread::current();
 178   RegisterMap map(thread, false);
 179   // More verification that skeleton frame is properly walkable
 180   assert(fp == caller->sp(), "fp must match");
 181 
 182   intptr_t* montop     = fp - rounded_vm_local_words;
 183 
 184   // preallocate monitors (cf. __ add_monitor_to_stack)
 185   intptr_t* monitors = montop - monitor_size;
 186 
 187   // preallocate stack space
 188   intptr_t*  esp = monitors - 1 -
 189     (tempcount * Interpreter::stackElementWords) -
 190     popframe_extra_args;
 191 
 192   int local_words = method->max_locals() * Interpreter::stackElementWords;
 193   NEEDS_CLEANUP;
 194   intptr_t* locals;
 195   if (caller->is_interpreted_frame()) {
 196     // Can force the locals area to end up properly overlapping the top of the expression stack.
 197     intptr_t* Lesp_ptr = caller->interpreter_frame_tos_address() - 1;
 198     // Note that this computation means we replace size_of_parameters() values from the caller
 199     // interpreter frame's expression stack with our argument locals
 200     int parm_words  = caller_actual_parameters * Interpreter::stackElementWords;
 201     locals = Lesp_ptr + parm_words;
 202     int delta = local_words - parm_words;
 203     int computed_sp_adjustment = (delta > 0) ? align_up(delta, WordsPerLong) : 0;
 204     *interpreter_frame->register_addr(I5_savedSP)    = (intptr_t) (fp + computed_sp_adjustment) - STACK_BIAS;
 205     if (!is_bottom_frame) {
 206       // Llast_SP is set below for the current frame to SP (with the
 207       // extra space for the callee's locals). Here we adjust
 208       // Llast_SP for the caller's frame, removing the extra space
 209       // for the current method's locals.
 210       *caller->register_addr(Llast_SP) = *interpreter_frame->register_addr(I5_savedSP);
 211     } else {
 212       assert(*caller->register_addr(Llast_SP) >= *interpreter_frame->register_addr(I5_savedSP), "strange Llast_SP");
 213     }
 214   } else {
 215     assert(caller->is_compiled_frame() || caller->is_entry_frame(), "only possible cases");
 216     // Don't have Lesp available; lay out locals block in the caller
 217     // adjacent to the register window save area.
 218     //
 219     // Compiled frames do not allocate a varargs area which is why this if
 220     // statement is needed.
 221     //
 222     if (caller->is_compiled_frame()) {
 223       locals = fp + frame::register_save_words + local_words - 1;
 224     } else {
 225       locals = fp + frame::memory_parameter_word_sp_offset + local_words - 1;
 226     }
 227     if (!caller->is_entry_frame()) {
 228       // Caller wants his own SP back
 229       int caller_frame_size = caller->cb()->frame_size();
 230       *interpreter_frame->register_addr(I5_savedSP) = (intptr_t)(caller->fp() - caller_frame_size) - STACK_BIAS;
 231     }
 232   }
 233   if (TraceDeoptimization) {
 234     if (caller->is_entry_frame()) {
 235       // make sure I5_savedSP and the entry frames notion of saved SP
 236       // agree.  This assertion duplicate a check in entry frame code
 237       // but catches the failure earlier.
 238       assert(*caller->register_addr(Lscratch) == *interpreter_frame->register_addr(I5_savedSP),
 239              "would change callers SP");
 240     }
 241     if (caller->is_entry_frame()) {
 242       tty->print("entry ");
 243     }
 244     if (caller->is_compiled_frame()) {
 245       tty->print("compiled ");
 246       if (caller->is_deoptimized_frame()) {
 247         tty->print("(deopt) ");
 248       }
 249     }
 250     if (caller->is_interpreted_frame()) {
 251       tty->print("interpreted ");
 252     }
 253     tty->print_cr("caller fp=" INTPTR_FORMAT " sp=" INTPTR_FORMAT, p2i(caller->fp()), p2i(caller->sp()));
 254     tty->print_cr("save area = " INTPTR_FORMAT ", " INTPTR_FORMAT, p2i(caller->sp()), p2i(caller->sp() + 16));
 255     tty->print_cr("save area = " INTPTR_FORMAT ", " INTPTR_FORMAT, p2i(caller->fp()), p2i(caller->fp() + 16));
 256     tty->print_cr("interpreter fp=" INTPTR_FORMAT ", " INTPTR_FORMAT, p2i(interpreter_frame->fp()), p2i(interpreter_frame->sp()));
 257     tty->print_cr("save area = " INTPTR_FORMAT ", " INTPTR_FORMAT, p2i(interpreter_frame->sp()), p2i(interpreter_frame->sp() + 16));
 258     tty->print_cr("save area = " INTPTR_FORMAT ", " INTPTR_FORMAT, p2i(interpreter_frame->fp()), p2i(interpreter_frame->fp() + 16));
 259     tty->print_cr("Llocals = " INTPTR_FORMAT, p2i(locals));
 260     tty->print_cr("Lesp = " INTPTR_FORMAT, p2i(esp));
 261     tty->print_cr("Lmonitors = " INTPTR_FORMAT, p2i(monitors));
 262   }
 263 
 264   if (method->max_locals() > 0) {
 265     assert(locals < caller->sp() || locals >= (caller->sp() + 16), "locals in save area");
 266     assert(locals < caller->fp() || locals > (caller->fp() + 16), "locals in save area");
 267     assert(locals < interpreter_frame->sp() || locals > (interpreter_frame->sp() + 16), "locals in save area");
 268     assert(locals < interpreter_frame->fp() || locals >= (interpreter_frame->fp() + 16), "locals in save area");
 269   }
 270   assert(*interpreter_frame->register_addr(I5_savedSP) & 1, "must be odd");
 271 
 272   *interpreter_frame->register_addr(Lmethod)     = (intptr_t) method;
 273   *interpreter_frame->register_addr(Llocals)     = (intptr_t) locals;
 274   *interpreter_frame->register_addr(Lmonitors)   = (intptr_t) monitors;
 275   *interpreter_frame->register_addr(Lesp)        = (intptr_t) esp;
 276   // Llast_SP will be same as SP as there is no adapter space
 277   *interpreter_frame->register_addr(Llast_SP)    = (intptr_t) interpreter_frame->sp() - STACK_BIAS;
 278   *interpreter_frame->register_addr(LcpoolCache) = (intptr_t) method->constants()->cache();
 279   // save the mirror in the interpreter frame
 280   *interpreter_frame->interpreter_frame_mirror_addr() = method->method_holder()->java_mirror();
 281 
 282 #ifdef ASSERT
 283   BasicObjectLock* mp = (BasicObjectLock*)monitors;
 284 
 285   assert(interpreter_frame->interpreter_frame_method() == method, "method matches");
 286   assert(interpreter_frame->interpreter_frame_local_at(9) == (intptr_t *)((intptr_t)locals - (9 * Interpreter::stackElementSize)), "locals match");
 287   assert(interpreter_frame->interpreter_frame_monitor_end()   == mp, "monitor_end matches");
 288   assert(((intptr_t *)interpreter_frame->interpreter_frame_monitor_begin()) == ((intptr_t *)mp)+monitor_size, "monitor_begin matches");
 289   assert(interpreter_frame->interpreter_frame_tos_address()-1 == esp, "esp matches");
 290 
 291   // check bounds
 292   intptr_t* lo = interpreter_frame->sp() + (frame::memory_parameter_word_sp_offset - 1);
 293   intptr_t* hi = interpreter_frame->fp() - rounded_vm_local_words;
 294   assert(lo < monitors && montop <= hi, "monitors in bounds");
 295   assert(lo <= esp && esp < monitors, "esp in bounds");
 296 #endif // ASSERT
 297 }