1/*
2 * Copyright (c) 1997, 2018, 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 "compiler/disassembler.hpp"
27#include "interpreter/interpreter.hpp"
28#include "interpreter/interpreterRuntime.hpp"
29#include "interpreter/interp_masm.hpp"
30#include "interpreter/templateInterpreter.hpp"
31#include "interpreter/templateInterpreterGenerator.hpp"
32#include "interpreter/templateTable.hpp"
33#include "oops/methodData.hpp"
34
35#ifndef CC_INTERP
36
37#define __ Disassembler::hook<InterpreterMacroAssembler>(__FILE__, __LINE__, _masm)->
38
39TemplateInterpreterGenerator::TemplateInterpreterGenerator(StubQueue* _code): AbstractInterpreterGenerator(_code) {
40 _unimplemented_bytecode = NULL;
41 _illegal_bytecode_sequence = NULL;
42 generate_all();
43}
44
45static const BasicType types[Interpreter::number_of_result_handlers] = {
46 T_BOOLEAN,
47 T_CHAR ,
48 T_BYTE ,
49 T_SHORT ,
50 T_INT ,
51 T_LONG ,
52 T_VOID ,
53 T_FLOAT ,
54 T_DOUBLE ,
55 T_OBJECT
56};
57
58void TemplateInterpreterGenerator::generate_all() {
59 { CodeletMark cm(_masm, "slow signature handler");
60 AbstractInterpreter::_slow_signature_handler = generate_slow_signature_handler();
61 }
62
63 { CodeletMark cm(_masm, "error exits");
64 _unimplemented_bytecode = generate_error_exit("unimplemented bytecode");
65 _illegal_bytecode_sequence = generate_error_exit("illegal bytecode sequence - method not verified");
66 }
67
68#ifndef PRODUCT
69 if (TraceBytecodes) {
70 CodeletMark cm(_masm, "bytecode tracing support");
71 Interpreter::_trace_code =
72 EntryPoint(
73 generate_trace_code(btos),
74 generate_trace_code(ztos),
75 generate_trace_code(ctos),
76 generate_trace_code(stos),
77 generate_trace_code(atos),
78 generate_trace_code(itos),
79 generate_trace_code(ltos),
80 generate_trace_code(ftos),
81 generate_trace_code(dtos),
82 generate_trace_code(vtos)
83 );
84 }
85#endif // !PRODUCT
86
87 { CodeletMark cm(_masm, "return entry points");
88 const int index_size = sizeof(u2);
89 Interpreter::_return_entry[0] = EntryPoint();
90 for (int i = 1; i < Interpreter::number_of_return_entries; i++) {
91 address return_itos = generate_return_entry_for(itos, i, index_size);
92 Interpreter::_return_entry[i] =
93 EntryPoint(
94 return_itos,
95 return_itos,
96 return_itos,
97 return_itos,
98 generate_return_entry_for(atos, i, index_size),
99 return_itos,
100 generate_return_entry_for(ltos, i, index_size),
101 generate_return_entry_for(ftos, i, index_size),
102 generate_return_entry_for(dtos, i, index_size),
103 generate_return_entry_for(vtos, i, index_size)
104 );
105 }
106 }
107
108 { CodeletMark cm(_masm, "invoke return entry points");
109 // These states are in order specified in TosState, except btos/ztos/ctos/stos are
110 // really the same as itos since there is no top of stack optimization for these types
111 const TosState states[] = {itos, itos, itos, itos, itos, ltos, ftos, dtos, atos, vtos, ilgl};
112 const int invoke_length = Bytecodes::length_for(Bytecodes::_invokestatic);
113 const int invokeinterface_length = Bytecodes::length_for(Bytecodes::_invokeinterface);
114 const int invokedynamic_length = Bytecodes::length_for(Bytecodes::_invokedynamic);
115
116 for (int i = 0; i < Interpreter::number_of_return_addrs; i++) {
117 TosState state = states[i];
118 assert(state != ilgl, "states array is wrong above");
119 Interpreter::_invoke_return_entry[i] = generate_return_entry_for(state, invoke_length, sizeof(u2));
120 Interpreter::_invokeinterface_return_entry[i] = generate_return_entry_for(state, invokeinterface_length, sizeof(u2));
121 Interpreter::_invokedynamic_return_entry[i] = generate_return_entry_for(state, invokedynamic_length, sizeof(u4));
122 }
123 }
124
125 { CodeletMark cm(_masm, "earlyret entry points");
126 Interpreter::_earlyret_entry =
127 EntryPoint(
128 generate_earlyret_entry_for(btos),
129 generate_earlyret_entry_for(ztos),
130 generate_earlyret_entry_for(ctos),
131 generate_earlyret_entry_for(stos),
132 generate_earlyret_entry_for(atos),
133 generate_earlyret_entry_for(itos),
134 generate_earlyret_entry_for(ltos),
135 generate_earlyret_entry_for(ftos),
136 generate_earlyret_entry_for(dtos),
137 generate_earlyret_entry_for(vtos)
138 );
139 }
140
141 { CodeletMark cm(_masm, "result handlers for native calls");
142 // The various result converter stublets.
143 int is_generated[Interpreter::number_of_result_handlers];
144 memset(is_generated, 0, sizeof(is_generated));
145
146 for (int i = 0; i < Interpreter::number_of_result_handlers; i++) {
147 BasicType type = types[i];
148 if (!is_generated[Interpreter::BasicType_as_index(type)]++) {
149 Interpreter::_native_abi_to_tosca[Interpreter::BasicType_as_index(type)] = generate_result_handler_for(type);
150 }
151 }
152 }
153
154
155 { CodeletMark cm(_masm, "safepoint entry points");
156 Interpreter::_safept_entry =
157 EntryPoint(
158 generate_safept_entry_for(btos, CAST_FROM_FN_PTR(address, InterpreterRuntime::at_safepoint)),
159 generate_safept_entry_for(ztos, CAST_FROM_FN_PTR(address, InterpreterRuntime::at_safepoint)),
160 generate_safept_entry_for(ctos, CAST_FROM_FN_PTR(address, InterpreterRuntime::at_safepoint)),
161 generate_safept_entry_for(stos, CAST_FROM_FN_PTR(address, InterpreterRuntime::at_safepoint)),
162 generate_safept_entry_for(atos, CAST_FROM_FN_PTR(address, InterpreterRuntime::at_safepoint)),
163 generate_safept_entry_for(itos, CAST_FROM_FN_PTR(address, InterpreterRuntime::at_safepoint)),
164 generate_safept_entry_for(ltos, CAST_FROM_FN_PTR(address, InterpreterRuntime::at_safepoint)),
165 generate_safept_entry_for(ftos, CAST_FROM_FN_PTR(address, InterpreterRuntime::at_safepoint)),
166 generate_safept_entry_for(dtos, CAST_FROM_FN_PTR(address, InterpreterRuntime::at_safepoint)),
167 generate_safept_entry_for(vtos, CAST_FROM_FN_PTR(address, InterpreterRuntime::at_safepoint))
168 );
169 }
170
171 { CodeletMark cm(_masm, "exception handling");
172 // (Note: this is not safepoint safe because thread may return to compiled code)
173 generate_throw_exception();
174 }
175
176 { CodeletMark cm(_masm, "throw exception entrypoints");
177 Interpreter::_throw_ArrayIndexOutOfBoundsException_entry = generate_ArrayIndexOutOfBounds_handler();
178 Interpreter::_throw_ArrayStoreException_entry = generate_klass_exception_handler("java/lang/ArrayStoreException");
179 Interpreter::_throw_ArithmeticException_entry = generate_exception_handler("java/lang/ArithmeticException", "/ by zero");
180 Interpreter::_throw_ClassCastException_entry = generate_ClassCastException_handler();
181 Interpreter::_throw_NullPointerException_entry = generate_exception_handler("java/lang/NullPointerException", NULL);
182 Interpreter::_throw_StackOverflowError_entry = generate_StackOverflowError_handler();
183 }
184
185
186
187#define method_entry(kind) \
188 { CodeletMark cm(_masm, "method entry point (kind = " #kind ")"); \
189 Interpreter::_entry_table[Interpreter::kind] = generate_method_entry(Interpreter::kind); \
190 Interpreter::update_cds_entry_table(Interpreter::kind); \
191 }
192
193 // all non-native method kinds
194 method_entry(zerolocals)
195 method_entry(zerolocals_synchronized)
196 method_entry(empty)
197 method_entry(accessor)
198 method_entry(abstract)
199 method_entry(java_lang_math_sin )
200 method_entry(java_lang_math_cos )
201 method_entry(java_lang_math_tan )
202 method_entry(java_lang_math_abs )
203 method_entry(java_lang_math_sqrt )
204 method_entry(java_lang_math_log )
205 method_entry(java_lang_math_log10)
206 method_entry(java_lang_math_exp )
207 method_entry(java_lang_math_pow )
208 method_entry(java_lang_math_fmaF )
209 method_entry(java_lang_math_fmaD )
210 method_entry(java_lang_ref_reference_get)
211
212 AbstractInterpreter::initialize_method_handle_entries();
213
214 // all native method kinds (must be one contiguous block)
215 Interpreter::_native_entry_begin = Interpreter::code()->code_end();
216 method_entry(native)
217 method_entry(native_synchronized)
218 Interpreter::_native_entry_end = Interpreter::code()->code_end();
219
220 method_entry(java_util_zip_CRC32_update)
221 method_entry(java_util_zip_CRC32_updateBytes)
222 method_entry(java_util_zip_CRC32_updateByteBuffer)
223 method_entry(java_util_zip_CRC32C_updateBytes)
224 method_entry(java_util_zip_CRC32C_updateDirectByteBuffer)
225
226 method_entry(java_lang_Float_intBitsToFloat);
227 method_entry(java_lang_Float_floatToRawIntBits);
228 method_entry(java_lang_Double_longBitsToDouble);
229 method_entry(java_lang_Double_doubleToRawLongBits);
230
231#undef method_entry
232
233 // Bytecodes
234 set_entry_points_for_all_bytes();
235
236 // installation of code in other places in the runtime
237 // (ExcutableCodeManager calls not needed to copy the entries)
238 set_safepoints_for_all_bytes();
239
240 { CodeletMark cm(_masm, "deoptimization entry points");
241 Interpreter::_deopt_entry[0] = EntryPoint();
242 Interpreter::_deopt_entry[0].set_entry(vtos, generate_deopt_entry_for(vtos, 0));
243 for (int i = 1; i < Interpreter::number_of_deopt_entries; i++) {
244 address deopt_itos = generate_deopt_entry_for(itos, i);
245 Interpreter::_deopt_entry[i] =
246 EntryPoint(
247 deopt_itos, /* btos */
248 deopt_itos, /* ztos */
249 deopt_itos, /* ctos */
250 deopt_itos, /* stos */
251 generate_deopt_entry_for(atos, i),
252 deopt_itos, /* itos */
253 generate_deopt_entry_for(ltos, i),
254 generate_deopt_entry_for(ftos, i),
255 generate_deopt_entry_for(dtos, i),
256 generate_deopt_entry_for(vtos, i)
257 );
258 }
259 address return_continuation = Interpreter::_normal_table.entry(Bytecodes::_return).entry(vtos);
260 vmassert(return_continuation != NULL, "return entry not generated yet");
261 Interpreter::_deopt_reexecute_return_entry = generate_deopt_entry_for(vtos, 0, return_continuation);
262 }
263
264}
265
266//------------------------------------------------------------------------------------------------------------------------
267
268address TemplateInterpreterGenerator::generate_error_exit(const char* msg) {
269 address entry = __ pc();
270 __ stop(msg);
271 return entry;
272}
273
274
275//------------------------------------------------------------------------------------------------------------------------
276
277void TemplateInterpreterGenerator::set_entry_points_for_all_bytes() {
278 for (int i = 0; i < DispatchTable::length; i++) {
279 Bytecodes::Code code = (Bytecodes::Code)i;
280 if (Bytecodes::is_defined(code)) {
281 set_entry_points(code);
282 } else {
283 set_unimplemented(i);
284 }
285 }
286}
287
288
289void TemplateInterpreterGenerator::set_safepoints_for_all_bytes() {
290 for (int i = 0; i < DispatchTable::length; i++) {
291 Bytecodes::Code code = (Bytecodes::Code)i;
292 if (Bytecodes::is_defined(code)) Interpreter::_safept_table.set_entry(code, Interpreter::_safept_entry);
293 }
294}
295
296
297void TemplateInterpreterGenerator::set_unimplemented(int i) {
298 address e = _unimplemented_bytecode;
299 EntryPoint entry(e, e, e, e, e, e, e, e, e, e);
300 Interpreter::_normal_table.set_entry(i, entry);
301 Interpreter::_wentry_point[i] = _unimplemented_bytecode;
302}
303
304
305void TemplateInterpreterGenerator::set_entry_points(Bytecodes::Code code) {
306 CodeletMark cm(_masm, Bytecodes::name(code), code);
307 // initialize entry points
308 assert(_unimplemented_bytecode != NULL, "should have been generated before");
309 assert(_illegal_bytecode_sequence != NULL, "should have been generated before");
310 address bep = _illegal_bytecode_sequence;
311 address zep = _illegal_bytecode_sequence;
312 address cep = _illegal_bytecode_sequence;
313 address sep = _illegal_bytecode_sequence;
314 address aep = _illegal_bytecode_sequence;
315 address iep = _illegal_bytecode_sequence;
316 address lep = _illegal_bytecode_sequence;
317 address fep = _illegal_bytecode_sequence;
318 address dep = _illegal_bytecode_sequence;
319 address vep = _unimplemented_bytecode;
320 address wep = _unimplemented_bytecode;
321 // code for short & wide version of bytecode
322 if (Bytecodes::is_defined(code)) {
323 Template* t = TemplateTable::template_for(code);
324 assert(t->is_valid(), "just checking");
325 set_short_entry_points(t, bep, cep, sep, aep, iep, lep, fep, dep, vep);
326 }
327 if (Bytecodes::wide_is_defined(code)) {
328 Template* t = TemplateTable::template_for_wide(code);
329 assert(t->is_valid(), "just checking");
330 set_wide_entry_point(t, wep);
331 }
332 // set entry points
333 EntryPoint entry(bep, zep, cep, sep, aep, iep, lep, fep, dep, vep);
334 Interpreter::_normal_table.set_entry(code, entry);
335 Interpreter::_wentry_point[code] = wep;
336}
337
338
339void TemplateInterpreterGenerator::set_wide_entry_point(Template* t, address& wep) {
340 assert(t->is_valid(), "template must exist");
341 assert(t->tos_in() == vtos, "only vtos tos_in supported for wide instructions");
342 wep = __ pc(); generate_and_dispatch(t);
343}
344
345
346void TemplateInterpreterGenerator::set_short_entry_points(Template* t, address& bep, address& cep, address& sep, address& aep, address& iep, address& lep, address& fep, address& dep, address& vep) {
347 assert(t->is_valid(), "template must exist");
348 switch (t->tos_in()) {
349 case btos:
350 case ztos:
351 case ctos:
352 case stos:
353 ShouldNotReachHere(); // btos/ctos/stos should use itos.
354 break;
355 case atos: vep = __ pc(); __ pop(atos); aep = __ pc(); generate_and_dispatch(t); break;
356 case itos: vep = __ pc(); __ pop(itos); iep = __ pc(); generate_and_dispatch(t); break;
357 case ltos: vep = __ pc(); __ pop(ltos); lep = __ pc(); generate_and_dispatch(t); break;
358 case ftos: vep = __ pc(); __ pop(ftos); fep = __ pc(); generate_and_dispatch(t); break;
359 case dtos: vep = __ pc(); __ pop(dtos); dep = __ pc(); generate_and_dispatch(t); break;
360 case vtos: set_vtos_entry_points(t, bep, cep, sep, aep, iep, lep, fep, dep, vep); break;
361 default : ShouldNotReachHere(); break;
362 }
363}
364
365
366//------------------------------------------------------------------------------------------------------------------------
367
368void TemplateInterpreterGenerator::generate_and_dispatch(Template* t, TosState tos_out) {
369 if (PrintBytecodeHistogram) histogram_bytecode(t);
370#ifndef PRODUCT
371 // debugging code
372 if (CountBytecodes || TraceBytecodes || StopInterpreterAt > 0) count_bytecode();
373 if (PrintBytecodePairHistogram) histogram_bytecode_pair(t);
374 if (TraceBytecodes) trace_bytecode(t);
375 if (StopInterpreterAt > 0) stop_interpreter_at();
376 __ verify_FPU(1, t->tos_in());
377#endif // !PRODUCT
378 int step = 0;
379 if (!t->does_dispatch()) {
380 step = t->is_wide() ? Bytecodes::wide_length_for(t->bytecode()) : Bytecodes::length_for(t->bytecode());
381 if (tos_out == ilgl) tos_out = t->tos_out();
382 // compute bytecode size
383 assert(step > 0, "just checkin'");
384 // setup stuff for dispatching next bytecode
385 if (ProfileInterpreter && VerifyDataPointer
386 && MethodData::bytecode_has_profile(t->bytecode())) {
387 __ verify_method_data_pointer();
388 }
389 __ dispatch_prolog(tos_out, step);
390 }
391 // generate template
392 t->generate(_masm);
393 // advance
394 if (t->does_dispatch()) {
395#ifdef ASSERT
396 // make sure execution doesn't go beyond this point if code is broken
397 __ should_not_reach_here();
398#endif // ASSERT
399 } else {
400 // dispatch to next bytecode
401 __ dispatch_epilog(tos_out, step);
402 }
403}
404
405// Generate method entries
406address TemplateInterpreterGenerator::generate_method_entry(
407 AbstractInterpreter::MethodKind kind) {
408 // determine code generation flags
409 bool native = false;
410 bool synchronized = false;
411 address entry_point = NULL;
412
413 switch (kind) {
414 case Interpreter::zerolocals : break;
415 case Interpreter::zerolocals_synchronized: synchronized = true; break;
416 case Interpreter::native : native = true; break;
417 case Interpreter::native_synchronized : native = true; synchronized = true; break;
418 case Interpreter::empty : break;
419 case Interpreter::accessor : break;
420 case Interpreter::abstract : entry_point = generate_abstract_entry(); break;
421
422 case Interpreter::java_lang_math_sin : // fall thru
423 case Interpreter::java_lang_math_cos : // fall thru
424 case Interpreter::java_lang_math_tan : // fall thru
425 case Interpreter::java_lang_math_abs : // fall thru
426 case Interpreter::java_lang_math_log : // fall thru
427 case Interpreter::java_lang_math_log10 : // fall thru
428 case Interpreter::java_lang_math_sqrt : // fall thru
429 case Interpreter::java_lang_math_pow : // fall thru
430 case Interpreter::java_lang_math_exp : // fall thru
431 case Interpreter::java_lang_math_fmaD : // fall thru
432 case Interpreter::java_lang_math_fmaF : entry_point = generate_math_entry(kind); break;
433 case Interpreter::java_lang_ref_reference_get
434 : entry_point = generate_Reference_get_entry(); break;
435 case Interpreter::java_util_zip_CRC32_update
436 : native = true; entry_point = generate_CRC32_update_entry(); break;
437 case Interpreter::java_util_zip_CRC32_updateBytes
438 : // fall thru
439 case Interpreter::java_util_zip_CRC32_updateByteBuffer
440 : native = true; entry_point = generate_CRC32_updateBytes_entry(kind); break;
441 case Interpreter::java_util_zip_CRC32C_updateBytes
442 : // fall thru
443 case Interpreter::java_util_zip_CRC32C_updateDirectByteBuffer
444 : entry_point = generate_CRC32C_updateBytes_entry(kind); break;
445#ifdef IA32
446 // On x86_32 platforms, a special entry is generated for the following four methods.
447 // On other platforms the normal entry is used to enter these methods.
448 case Interpreter::java_lang_Float_intBitsToFloat
449 : native = true; entry_point = generate_Float_intBitsToFloat_entry(); break;
450 case Interpreter::java_lang_Float_floatToRawIntBits
451 : native = true; entry_point = generate_Float_floatToRawIntBits_entry(); break;
452 case Interpreter::java_lang_Double_longBitsToDouble
453 : native = true; entry_point = generate_Double_longBitsToDouble_entry(); break;
454 case Interpreter::java_lang_Double_doubleToRawLongBits
455 : native = true; entry_point = generate_Double_doubleToRawLongBits_entry(); break;
456#else
457 case Interpreter::java_lang_Float_intBitsToFloat:
458 case Interpreter::java_lang_Float_floatToRawIntBits:
459 case Interpreter::java_lang_Double_longBitsToDouble:
460 case Interpreter::java_lang_Double_doubleToRawLongBits:
461 native = true;
462 break;
463#endif // !IA32
464 default:
465 fatal("unexpected method kind: %d", kind);
466 break;
467 }
468
469 if (entry_point) {
470 return entry_point;
471 }
472
473 // We expect the normal and native entry points to be generated first so we can reuse them.
474 if (native) {
475 entry_point = Interpreter::entry_for_kind(synchronized ? Interpreter::native_synchronized : Interpreter::native);
476 if (entry_point == NULL) {
477 entry_point = generate_native_entry(synchronized);
478 }
479 } else {
480 entry_point = Interpreter::entry_for_kind(synchronized ? Interpreter::zerolocals_synchronized : Interpreter::zerolocals);
481 if (entry_point == NULL) {
482 entry_point = generate_normal_entry(synchronized);
483 }
484 }
485
486 return entry_point;
487}
488#endif // !CC_INTERP
489