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