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