1 /*
   2  * Copyright (c) 1997, 2014, 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 "ci/ciMethod.hpp"
  27 #include "interpreter/interpreter.hpp"
  28 #include "runtime/frame.inline.hpp"
  29 
  30 #ifndef CC_INTERP
  31 
  32 // asm based interpreter deoptimization helpers
  33 template<class M> static int extra_locals_helper(M* method) {
  34   return (method->max_locals() - method->size_of_parameters()) *
  35     Interpreter::stackElementWords;
  36 }
  37 
  38 template<class M> int AbstractInterpreter::size_activation(M* method,
  39                                                            int temps,
  40                                                            int popframe_args,
  41                                                            int monitors,
  42                                                            int callee_params,
  43                                                            int callee_locals,
  44                                                            bool is_top_frame) {
  45   // Note: This calculation must exactly parallel the frame setup
  46   // in AbstractInterpreterGenerator::generate_method_entry.
  47 
  48   // fixed size of an interpreter frame:
  49   int extra_locals = extra_locals_helper<M>(method);
  50 
  51   int overhead = frame::sender_sp_offset -
  52                  frame::interpreter_frame_initial_sp_offset;
  53   // Our locals were accounted for by the caller (or last_frame_adjust
  54   // on the transistion) Since the callee parameters already account
  55   // for the callee's params we only need to account for the extra
  56   // locals.
  57   int size = overhead +
  58          (callee_locals - callee_params)*Interpreter::stackElementWords +
  59          monitors * frame::interpreter_frame_monitor_size() +
  60          temps* Interpreter::stackElementWords + popframe_args;
  61   
  62   return size;
  63 }
  64 
  65 template int AbstractInterpreter::size_activation<Method>(Method* method,
  66                                                           int temps,
  67                                                           int popframe_args,
  68                                                           int monitors,
  69                                                           int callee_params,
  70                                                           int callee_locals,
  71                                                           bool is_top_frame);
  72 
  73 template int AbstractInterpreter::size_activation<ciMethod>(ciMethod* method,
  74                                                             int temps,
  75                                                             int popframe_args,
  76                                                             int monitors,
  77                                                             int callee_params,
  78                                                             int callee_locals,
  79                                                             bool is_top_frame);
  80 
  81 void AbstractInterpreter::layout_activation(Method* method,
  82                                             int tempcount,
  83                                             int popframe_extra_args,
  84                                             int moncount,
  85                                             int caller_actual_parameters,
  86                                             int callee_param_count,
  87                                             int callee_locals,
  88                                             frame* caller,
  89                                             frame* interpreter_frame,
  90                                             bool is_top_frame,
  91                                             bool is_bottom_frame) {
  92   // The frame interpreter_frame is guaranteed to be the right size,
  93   // as determined by a previous call to the size_activation() method.
  94   // It is also guaranteed to be walkable even though it is in a
  95   // skeletal state
  96 
  97   int max_locals = method->max_locals() * Interpreter::stackElementWords;
  98   int extra_locals = extra_locals_helper<Method>(method);
  99 
 100 #ifdef ASSERT
 101   if (!EnableInvokeDynamic)
 102     // @@@ FIXME: Should we correct interpreter_frame_sender_sp in the calling sequences?
 103     // Probably, since deoptimization doesn't work yet.
 104     assert(caller->unextended_sp() == interpreter_frame->interpreter_frame_sender_sp(), "Frame not properly walkable");
 105   assert(caller->sp() == interpreter_frame->sender_sp(), "Frame not properly walkable(2)");
 106 #endif
 107 
 108   interpreter_frame->interpreter_frame_set_method(method);
 109   // NOTE the difference in using sender_sp and
 110   // interpreter_frame_sender_sp interpreter_frame_sender_sp is
 111   // the original sp of the caller (the unextended_sp) and
 112   // sender_sp is fp+8/16 (32bit/64bit) XXX
 113   intptr_t* locals = interpreter_frame->sender_sp() + max_locals - 1;
 114 
 115 #ifdef ASSERT
 116   if (caller->is_interpreted_frame()) {
 117     assert(locals < caller->fp() + frame::interpreter_frame_initial_sp_offset, "bad placement");
 118   }
 119 #endif
 120 
 121   interpreter_frame->interpreter_frame_set_locals(locals);
 122   BasicObjectLock* montop = interpreter_frame->interpreter_frame_monitor_begin();
 123   BasicObjectLock* monbot = montop - moncount;
 124   interpreter_frame->interpreter_frame_set_monitor_end(monbot);
 125 
 126   // Set last_sp
 127   intptr_t*  esp = (intptr_t*) monbot -
 128     tempcount*Interpreter::stackElementWords -
 129     popframe_extra_args;
 130   interpreter_frame->interpreter_frame_set_last_sp(esp);
 131 
 132   // All frames but the initial (oldest) interpreter frame we fill in have
 133   // a value for sender_sp that allows walking the stack but isn't
 134   // truly correct. Correct the value here.
 135   if (extra_locals != 0 &&
 136       interpreter_frame->sender_sp() ==
 137       interpreter_frame->interpreter_frame_sender_sp()) {
 138     interpreter_frame->set_interpreter_frame_sender_sp(caller->sp() +
 139                                                        extra_locals);
 140   }
 141   *interpreter_frame->interpreter_frame_cache_addr() =
 142     method->constants()->cache();
 143 }
 144 
 145 #endif // CC_INTERP