1 /*
2  * Copyright (c) 2008, 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 "asm/assembler.hpp"
27 #include "interpreter/bytecode.hpp"
28 #include "interpreter/interpreter.hpp"
29 #include "oops/constMethod.hpp"
30 #include "oops/method.hpp"
31 #include "prims/methodHandles.hpp"
32 #include "runtime/handles.inline.hpp"
33 #include "runtime/frame.inline.hpp"
34 #include "runtime/synchronizer.hpp"
35 #include "utilities/align.hpp"
36 #include "utilities/macros.hpp"
37 
BasicType_as_index(BasicType type)38 int AbstractInterpreter::BasicType_as_index(BasicType type) {
39   int i = 0;
40   switch (type) {
41 #ifdef AARCH64
42     case T_BOOLEAN: i = 0; break;
43     case T_CHAR   : i = 1; break;
44     case T_BYTE   : i = 2; break;
45     case T_SHORT  : i = 3; break;
46     case T_INT    : // fall through
47     case T_LONG   : // fall through
48     case T_VOID   : // fall through
49     case T_FLOAT  : // fall through
50     case T_DOUBLE : i = 4; break;
51     case T_OBJECT : // fall through
52     case T_ARRAY  : i = 5; break;
53 #else
54     case T_VOID   : i = 0; break;
55     case T_BOOLEAN: i = 1; break;
56     case T_CHAR   : i = 2; break;
57     case T_BYTE   : i = 3; break;
58     case T_SHORT  : i = 4; break;
59     case T_INT    : i = 5; break;
60     case T_OBJECT : // fall through
61     case T_ARRAY  : i = 6; break;
62     case T_LONG   : i = 7; break;
63     case T_FLOAT  : i = 8; break;
64     case T_DOUBLE : i = 9; break;
65 #endif // AARCH64
66     default       : ShouldNotReachHere();
67   }
68   assert(0 <= i && i < AbstractInterpreter::number_of_result_handlers, "index out of bounds");
69   return i;
70 }
71 
72 // How much stack a method activation needs in words.
size_top_interpreter_activation(Method * method)73 int AbstractInterpreter::size_top_interpreter_activation(Method* method) {
74   const int stub_code = AARCH64_ONLY(24) NOT_AARCH64(12);  // see generate_call_stub
75   // Save space for one monitor to get into the interpreted method in case
76   // the method is synchronized
77   int monitor_size    = method->is_synchronized() ?
78                                 1*frame::interpreter_frame_monitor_size() : 0;
79 
80   // total overhead size: monitor_size + (sender SP, thru expr stack bottom).
81   // be sure to change this if you add/subtract anything to/from the overhead area
82   const int overhead_size = monitor_size +
83                             (frame::sender_sp_offset - frame::interpreter_frame_initial_sp_offset);
84   const int method_stack = (method->max_locals() + method->max_stack()) *
85                            Interpreter::stackElementWords;
86   return overhead_size + method_stack + stub_code;
87 }
88 
89 // asm based interpreter deoptimization helpers
size_activation(int max_stack,int tempcount,int extra_args,int moncount,int callee_param_count,int callee_locals,bool is_top_frame)90 int AbstractInterpreter::size_activation(int max_stack,
91                                          int tempcount,
92                                          int extra_args,
93                                          int moncount,
94                                          int callee_param_count,
95                                          int callee_locals,
96                                          bool is_top_frame) {
97   // Note: This calculation must exactly parallel the frame setup
98   // in TemplateInterpreterGenerator::generate_fixed_frame.
99   // fixed size of an interpreter frame:
100   int overhead = frame::sender_sp_offset - frame::interpreter_frame_initial_sp_offset;
101 
102   // Our locals were accounted for by the caller (or last_frame_adjust on the transistion)
103   // Since the callee parameters already account for the callee's params we only need to account for
104   // the extra locals.
105 
106   int size = overhead +
107          ((callee_locals - callee_param_count)*Interpreter::stackElementWords) +
108          (moncount*frame::interpreter_frame_monitor_size()) +
109          tempcount*Interpreter::stackElementWords + extra_args;
110 
111 #ifdef AARCH64
112   size = align_up(size, StackAlignmentInBytes/BytesPerWord);
113 #endif // AARCH64
114 
115   return size;
116 }
117 
layout_activation(Method * method,int tempcount,int popframe_extra_args,int moncount,int caller_actual_parameters,int callee_param_count,int callee_locals,frame * caller,frame * interpreter_frame,bool is_top_frame,bool is_bottom_frame)118 void AbstractInterpreter::layout_activation(Method* method,
119                                             int tempcount,
120                                             int popframe_extra_args,
121                                             int moncount,
122                                             int caller_actual_parameters,
123                                             int callee_param_count,
124                                             int callee_locals,
125                                             frame* caller,
126                                             frame* interpreter_frame,
127                                             bool is_top_frame,
128                                             bool is_bottom_frame) {
129 
130   // Set up the method, locals, and monitors.
131   // The frame interpreter_frame is guaranteed to be the right size,
132   // as determined by a previous call to the size_activation() method.
133   // It is also guaranteed to be walkable even though it is in a skeletal state
134   // NOTE: return size is in words not bytes
135 
136   // fixed size of an interpreter frame:
137   int max_locals = method->max_locals() * Interpreter::stackElementWords;
138   int extra_locals = (method->max_locals() - method->size_of_parameters()) * Interpreter::stackElementWords;
139 
140 #ifdef ASSERT
141   assert(caller->sp() == interpreter_frame->sender_sp(), "Frame not properly walkable");
142 #endif
143 
144   interpreter_frame->interpreter_frame_set_method(method);
145   // NOTE the difference in using sender_sp and interpreter_frame_sender_sp
146   // interpreter_frame_sender_sp is the original sp of the caller (the unextended_sp)
147   // and sender_sp is (fp + sender_sp_offset*wordSize)
148 
149 #ifdef AARCH64
150   intptr_t* locals;
151   if (caller->is_interpreted_frame()) {
152     // attach locals to the expression stack of caller interpreter frame
153     locals = caller->interpreter_frame_tos_address() + caller_actual_parameters*Interpreter::stackElementWords - 1;
154   } else {
155     assert (is_bottom_frame, "should be");
156     locals = interpreter_frame->fp() + frame::sender_sp_offset + method->max_locals() - 1;
157   }
158 
159   if (TraceDeoptimization) {
160     tty->print_cr("layout_activation:");
161 
162     if (caller->is_entry_frame()) {
163       tty->print("entry ");
164     }
165     if (caller->is_compiled_frame()) {
166       tty->print("compiled ");
167     }
168     if (caller->is_interpreted_frame()) {
169       tty->print("interpreted ");
170     }
171     tty->print_cr("caller: sp=%p, unextended_sp=%p, fp=%p, pc=%p", caller->sp(), caller->unextended_sp(), caller->fp(), caller->pc());
172     tty->print_cr("interpreter_frame: sp=%p, unextended_sp=%p, fp=%p, pc=%p", interpreter_frame->sp(), interpreter_frame->unextended_sp(), interpreter_frame->fp(), interpreter_frame->pc());
173     tty->print_cr("method: max_locals = %d, size_of_parameters = %d", method->max_locals(), method->size_of_parameters());
174     tty->print_cr("caller_actual_parameters = %d", caller_actual_parameters);
175     tty->print_cr("locals = %p", locals);
176   }
177 
178 #ifdef ASSERT
179   if (caller_actual_parameters != method->size_of_parameters()) {
180     assert(caller->is_interpreted_frame(), "adjusted caller_actual_parameters, but caller is not interpreter frame");
181     Bytecode_invoke inv(caller->interpreter_frame_method(), caller->interpreter_frame_bci());
182 
183     if (is_bottom_frame) {
184       assert(caller_actual_parameters == 0, "invalid adjusted caller_actual_parameters value for bottom frame");
185       assert(inv.is_invokedynamic() || inv.is_invokehandle(), "adjusted caller_actual_parameters for bottom frame, but not invokedynamic/invokehandle");
186     } else {
187       assert(caller_actual_parameters == method->size_of_parameters()+1, "invalid adjusted caller_actual_parameters value");
188       assert(!inv.is_invokedynamic() && MethodHandles::has_member_arg(inv.klass(), inv.name()), "adjusted caller_actual_parameters, but no member arg");
189     }
190   }
191   if (caller->is_interpreted_frame()) {
192     intptr_t* locals_base = (locals - method->max_locals()*Interpreter::stackElementWords + 1);
193     locals_base = align_down(locals_base, StackAlignmentInBytes);
194     assert(interpreter_frame->sender_sp() <= locals_base, "interpreter-to-interpreter frame chaining");
195 
196   } else if (caller->is_compiled_frame()) {
197     assert(locals + 1 <= caller->unextended_sp(), "compiled-to-interpreter frame chaining");
198 
199   } else {
200     assert(caller->is_entry_frame(), "should be");
201     assert(locals + 1 <= caller->fp(), "entry-to-interpreter frame chaining");
202   }
203 #endif // ASSERT
204 
205 #else
206   intptr_t* locals = interpreter_frame->sender_sp() + max_locals - 1;
207 #endif // AARCH64
208 
209   interpreter_frame->interpreter_frame_set_locals(locals);
210   BasicObjectLock* montop = interpreter_frame->interpreter_frame_monitor_begin();
211   BasicObjectLock* monbot = montop - moncount;
212   interpreter_frame->interpreter_frame_set_monitor_end(monbot);
213 
214   // Set last_sp
215   intptr_t* stack_top = (intptr_t*) monbot  -
216     tempcount*Interpreter::stackElementWords -
217     popframe_extra_args;
218 #ifdef AARCH64
219   interpreter_frame->interpreter_frame_set_stack_top(stack_top);
220 
221   // We have to add extra reserved slots to max_stack. There are 3 users of the extra slots,
222   // none of which are at the same time, so we just need to make sure there is enough room
223   // for the biggest user:
224   //   -reserved slot for exception handler
225   //   -reserved slots for JSR292. Method::extra_stack_entries() is the size.
226   //   -3 reserved slots so get_method_counters() can save some registers before call_VM().
227   int max_stack = method->constMethod()->max_stack() + MAX2(3, Method::extra_stack_entries());
228   intptr_t* extended_sp = (intptr_t*) monbot  -
229     (max_stack * Interpreter::stackElementWords) -
230     popframe_extra_args;
231   extended_sp = align_down(extended_sp, StackAlignmentInBytes);
232   interpreter_frame->interpreter_frame_set_extended_sp(extended_sp);
233 #else
234   interpreter_frame->interpreter_frame_set_last_sp(stack_top);
235 #endif // AARCH64
236 
237   // All frames but the initial (oldest) interpreter frame we fill in have a
238   // value for sender_sp that allows walking the stack but isn't
239   // truly correct. Correct the value here.
240 
241 #ifdef AARCH64
242   if (caller->is_interpreted_frame()) {
243     intptr_t* sender_sp = align_down(caller->interpreter_frame_tos_address(), StackAlignmentInBytes);
244     interpreter_frame->set_interpreter_frame_sender_sp(sender_sp);
245 
246   } else {
247     // in case of non-interpreter caller sender_sp of the oldest frame is already
248     // set to valid value
249   }
250 #else
251   if (extra_locals != 0 &&
252       interpreter_frame->sender_sp() == interpreter_frame->interpreter_frame_sender_sp() ) {
253     interpreter_frame->set_interpreter_frame_sender_sp(caller->sp() + extra_locals);
254   }
255 #endif // AARCH64
256 
257   *interpreter_frame->interpreter_frame_cache_addr() =
258     method->constants()->cache();
259   *interpreter_frame->interpreter_frame_mirror_addr() =
260     method->method_holder()->java_mirror();
261 }
262