Mon, 24 Sep 2012 10:30:14 -0700
7200163: add CodeComments functionality to assember stubs
Summary: Pass the codeBuffer to the Stub constructor, and adapts the disassembler to print the comments.
Reviewed-by: jrose, kvn, twisti
Contributed-by: goetz.lindenmaier@sap.com
1 /*
2 * Copyright (c) 1997, 2012, 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 */
25 #include "precompiled.hpp"
26 #include "asm/assembler.hpp"
27 #include "interpreter/bytecodeHistogram.hpp"
28 #include "interpreter/bytecodeInterpreter.hpp"
29 #include "interpreter/interpreter.hpp"
30 #include "interpreter/interpreterRuntime.hpp"
31 #include "interpreter/templateTable.hpp"
32 #include "memory/allocation.inline.hpp"
33 #include "memory/resourceArea.hpp"
34 #include "oops/arrayOop.hpp"
35 #include "oops/methodData.hpp"
36 #include "oops/method.hpp"
37 #include "oops/oop.inline.hpp"
38 #include "prims/forte.hpp"
39 #include "prims/jvmtiExport.hpp"
40 #include "prims/methodHandles.hpp"
41 #include "runtime/handles.inline.hpp"
42 #include "runtime/sharedRuntime.hpp"
43 #include "runtime/stubRoutines.hpp"
44 #include "runtime/timer.hpp"
46 # define __ _masm->
49 //------------------------------------------------------------------------------------------------------------------------
50 // Implementation of InterpreterCodelet
52 void InterpreterCodelet::initialize(const char* description, Bytecodes::Code bytecode) {
53 _description = description;
54 _bytecode = bytecode;
55 }
58 void InterpreterCodelet::verify() {
59 }
62 void InterpreterCodelet::print_on(outputStream* st) const {
63 ttyLocker ttyl;
65 if (PrintInterpreter) {
66 st->cr();
67 st->print_cr("----------------------------------------------------------------------");
68 }
70 if (description() != NULL) st->print("%s ", description());
71 if (bytecode() >= 0 ) st->print("%d %s ", bytecode(), Bytecodes::name(bytecode()));
72 st->print_cr("[" INTPTR_FORMAT ", " INTPTR_FORMAT "] %d bytes",
73 code_begin(), code_end(), code_size());
75 if (PrintInterpreter) {
76 st->cr();
77 Disassembler::decode(code_begin(), code_end(), st, DEBUG_ONLY(_comments) NOT_DEBUG(CodeComments()));
78 }
79 }
82 //------------------------------------------------------------------------------------------------------------------------
83 // Implementation of platform independent aspects of Interpreter
85 void AbstractInterpreter::initialize() {
86 if (_code != NULL) return;
88 // make sure 'imported' classes are initialized
89 if (CountBytecodes || TraceBytecodes || StopInterpreterAt) BytecodeCounter::reset();
90 if (PrintBytecodeHistogram) BytecodeHistogram::reset();
91 if (PrintBytecodePairHistogram) BytecodePairHistogram::reset();
93 InvocationCounter::reinitialize(DelayCompilationDuringStartup);
95 }
97 void AbstractInterpreter::print() {
98 tty->cr();
99 tty->print_cr("----------------------------------------------------------------------");
100 tty->print_cr("Interpreter");
101 tty->cr();
102 tty->print_cr("code size = %6dK bytes", (int)_code->used_space()/1024);
103 tty->print_cr("total space = %6dK bytes", (int)_code->total_space()/1024);
104 tty->print_cr("wasted space = %6dK bytes", (int)_code->available_space()/1024);
105 tty->cr();
106 tty->print_cr("# of codelets = %6d" , _code->number_of_stubs());
107 tty->print_cr("avg codelet size = %6d bytes", _code->used_space() / _code->number_of_stubs());
108 tty->cr();
109 _code->print();
110 tty->print_cr("----------------------------------------------------------------------");
111 tty->cr();
112 }
115 void interpreter_init() {
116 Interpreter::initialize();
117 #ifndef PRODUCT
118 if (TraceBytecodes) BytecodeTracer::set_closure(BytecodeTracer::std_closure());
119 #endif // PRODUCT
120 // need to hit every safepoint in order to call zapping routine
121 // register the interpreter
122 Forte::register_stub(
123 "Interpreter",
124 AbstractInterpreter::code()->code_start(),
125 AbstractInterpreter::code()->code_end()
126 );
128 // notify JVMTI profiler
129 if (JvmtiExport::should_post_dynamic_code_generated()) {
130 JvmtiExport::post_dynamic_code_generated("Interpreter",
131 AbstractInterpreter::code()->code_start(),
132 AbstractInterpreter::code()->code_end());
133 }
134 }
136 //------------------------------------------------------------------------------------------------------------------------
137 // Implementation of interpreter
139 StubQueue* AbstractInterpreter::_code = NULL;
140 bool AbstractInterpreter::_notice_safepoints = false;
141 address AbstractInterpreter::_rethrow_exception_entry = NULL;
143 address AbstractInterpreter::_native_entry_begin = NULL;
144 address AbstractInterpreter::_native_entry_end = NULL;
145 address AbstractInterpreter::_slow_signature_handler;
146 address AbstractInterpreter::_entry_table [AbstractInterpreter::number_of_method_entries];
147 address AbstractInterpreter::_native_abi_to_tosca [AbstractInterpreter::number_of_result_handlers];
149 //------------------------------------------------------------------------------------------------------------------------
150 // Generation of complete interpreter
152 AbstractInterpreterGenerator::AbstractInterpreterGenerator(StubQueue* _code) {
153 _masm = NULL;
154 }
157 static const BasicType types[Interpreter::number_of_result_handlers] = {
158 T_BOOLEAN,
159 T_CHAR ,
160 T_BYTE ,
161 T_SHORT ,
162 T_INT ,
163 T_LONG ,
164 T_VOID ,
165 T_FLOAT ,
166 T_DOUBLE ,
167 T_OBJECT
168 };
170 void AbstractInterpreterGenerator::generate_all() {
173 { CodeletMark cm(_masm, "slow signature handler");
174 Interpreter::_slow_signature_handler = generate_slow_signature_handler();
175 }
177 }
179 //------------------------------------------------------------------------------------------------------------------------
180 // Entry points
182 AbstractInterpreter::MethodKind AbstractInterpreter::method_kind(methodHandle m) {
183 // Abstract method?
184 if (m->is_abstract()) return abstract;
186 // Method handle primitive?
187 if (m->is_method_handle_intrinsic()) {
188 vmIntrinsics::ID id = m->intrinsic_id();
189 assert(MethodHandles::is_signature_polymorphic(id), "must match an intrinsic");
190 MethodKind kind = (MethodKind)( method_handle_invoke_FIRST +
191 ((int)id - vmIntrinsics::FIRST_MH_SIG_POLY) );
192 assert(kind <= method_handle_invoke_LAST, "parallel enum ranges");
193 return kind;
194 }
196 // Native method?
197 // Note: This test must come _before_ the test for intrinsic
198 // methods. See also comments below.
199 if (m->is_native()) {
200 assert(!m->is_method_handle_intrinsic(), "overlapping bits here, watch out");
201 return m->is_synchronized() ? native_synchronized : native;
202 }
204 // Synchronized?
205 if (m->is_synchronized()) {
206 return zerolocals_synchronized;
207 }
209 if (RegisterFinalizersAtInit && m->code_size() == 1 &&
210 m->intrinsic_id() == vmIntrinsics::_Object_init) {
211 // We need to execute the special return bytecode to check for
212 // finalizer registration so create a normal frame.
213 return zerolocals;
214 }
216 // Empty method?
217 if (m->is_empty_method()) {
218 return empty;
219 }
221 // Special intrinsic method?
222 // Note: This test must come _after_ the test for native methods,
223 // otherwise we will run into problems with JDK 1.2, see also
224 // AbstractInterpreterGenerator::generate_method_entry() for
225 // for details.
226 switch (m->intrinsic_id()) {
227 case vmIntrinsics::_dsin : return java_lang_math_sin ;
228 case vmIntrinsics::_dcos : return java_lang_math_cos ;
229 case vmIntrinsics::_dtan : return java_lang_math_tan ;
230 case vmIntrinsics::_dabs : return java_lang_math_abs ;
231 case vmIntrinsics::_dsqrt : return java_lang_math_sqrt ;
232 case vmIntrinsics::_dlog : return java_lang_math_log ;
233 case vmIntrinsics::_dlog10: return java_lang_math_log10;
234 case vmIntrinsics::_dpow : return java_lang_math_pow ;
235 case vmIntrinsics::_dexp : return java_lang_math_exp ;
237 case vmIntrinsics::_Reference_get:
238 return java_lang_ref_reference_get;
239 }
241 // Accessor method?
242 if (m->is_accessor()) {
243 assert(m->size_of_parameters() == 1, "fast code for accessors assumes parameter size = 1");
244 return accessor;
245 }
247 // Note: for now: zero locals for all non-empty methods
248 return zerolocals;
249 }
252 void AbstractInterpreter::set_entry_for_kind(AbstractInterpreter::MethodKind kind, address entry) {
253 assert(kind >= method_handle_invoke_FIRST &&
254 kind <= method_handle_invoke_LAST, "late initialization only for MH entry points");
255 assert(_entry_table[kind] == _entry_table[abstract], "previous value must be AME entry");
256 _entry_table[kind] = entry;
257 }
260 // Return true if the interpreter can prove that the given bytecode has
261 // not yet been executed (in Java semantics, not in actual operation).
262 bool AbstractInterpreter::is_not_reached(methodHandle method, int bci) {
263 Bytecodes::Code code = method()->code_at(bci);
265 if (!Bytecodes::must_rewrite(code)) {
266 // might have been reached
267 return false;
268 }
270 // the bytecode might not be rewritten if the method is an accessor, etc.
271 address ientry = method->interpreter_entry();
272 if (ientry != entry_for_kind(AbstractInterpreter::zerolocals) &&
273 ientry != entry_for_kind(AbstractInterpreter::zerolocals_synchronized))
274 return false; // interpreter does not run this method!
276 // otherwise, we can be sure this bytecode has never been executed
277 return true;
278 }
281 #ifndef PRODUCT
282 void AbstractInterpreter::print_method_kind(MethodKind kind) {
283 switch (kind) {
284 case zerolocals : tty->print("zerolocals" ); break;
285 case zerolocals_synchronized: tty->print("zerolocals_synchronized"); break;
286 case native : tty->print("native" ); break;
287 case native_synchronized : tty->print("native_synchronized" ); break;
288 case empty : tty->print("empty" ); break;
289 case accessor : tty->print("accessor" ); break;
290 case abstract : tty->print("abstract" ); break;
291 case java_lang_math_sin : tty->print("java_lang_math_sin" ); break;
292 case java_lang_math_cos : tty->print("java_lang_math_cos" ); break;
293 case java_lang_math_tan : tty->print("java_lang_math_tan" ); break;
294 case java_lang_math_abs : tty->print("java_lang_math_abs" ); break;
295 case java_lang_math_sqrt : tty->print("java_lang_math_sqrt" ); break;
296 case java_lang_math_log : tty->print("java_lang_math_log" ); break;
297 case java_lang_math_log10 : tty->print("java_lang_math_log10" ); break;
298 default:
299 if (kind >= method_handle_invoke_FIRST &&
300 kind <= method_handle_invoke_LAST) {
301 const char* kind_name = vmIntrinsics::name_at(method_handle_intrinsic(kind));
302 if (kind_name[0] == '_') kind_name = &kind_name[1]; // '_invokeExact' => 'invokeExact'
303 tty->print("method_handle_%s", kind_name);
304 break;
305 }
306 ShouldNotReachHere();
307 break;
308 }
309 }
310 #endif // PRODUCT
313 //------------------------------------------------------------------------------------------------------------------------
314 // Deoptimization support
316 // If deoptimization happens, this function returns the point of next bytecode to continue execution
317 address AbstractInterpreter::deopt_continue_after_entry(Method* method, address bcp, int callee_parameters, bool is_top_frame) {
318 assert(method->contains(bcp), "just checkin'");
319 Bytecodes::Code code = Bytecodes::java_code_at(method, bcp);
320 assert(!Interpreter::bytecode_should_reexecute(code), "should not reexecute");
321 int bci = method->bci_from(bcp);
322 int length = -1; // initial value for debugging
323 // compute continuation length
324 length = Bytecodes::length_at(method, bcp);
325 // compute result type
326 BasicType type = T_ILLEGAL;
328 switch (code) {
329 case Bytecodes::_invokevirtual :
330 case Bytecodes::_invokespecial :
331 case Bytecodes::_invokestatic :
332 case Bytecodes::_invokeinterface: {
333 Thread *thread = Thread::current();
334 ResourceMark rm(thread);
335 methodHandle mh(thread, method);
336 type = Bytecode_invoke(mh, bci).result_type();
337 // since the cache entry might not be initialized:
338 // (NOT needed for the old calling convension)
339 if (!is_top_frame) {
340 int index = Bytes::get_native_u2(bcp+1);
341 method->constants()->cache()->entry_at(index)->set_parameter_size(callee_parameters);
342 }
343 break;
344 }
346 case Bytecodes::_invokedynamic: {
347 Thread *thread = Thread::current();
348 ResourceMark rm(thread);
349 methodHandle mh(thread, method);
350 type = Bytecode_invoke(mh, bci).result_type();
351 // since the cache entry might not be initialized:
352 // (NOT needed for the old calling convension)
353 if (!is_top_frame) {
354 int index = Bytes::get_native_u4(bcp+1);
355 method->constants()->invokedynamic_cp_cache_entry_at(index)->set_parameter_size(callee_parameters);
356 }
357 break;
358 }
360 case Bytecodes::_ldc :
361 case Bytecodes::_ldc_w : // fall through
362 case Bytecodes::_ldc2_w:
363 {
364 Thread *thread = Thread::current();
365 ResourceMark rm(thread);
366 methodHandle mh(thread, method);
367 type = Bytecode_loadconstant(mh, bci).result_type();
368 break;
369 }
371 default:
372 type = Bytecodes::result_type(code);
373 break;
374 }
376 // return entry point for computed continuation state & bytecode length
377 return
378 is_top_frame
379 ? Interpreter::deopt_entry (as_TosState(type), length)
380 : Interpreter::return_entry(as_TosState(type), length);
381 }
383 // If deoptimization happens, this function returns the point where the interpreter reexecutes
384 // the bytecode.
385 // Note: Bytecodes::_athrow is a special case in that it does not return
386 // Interpreter::deopt_entry(vtos, 0) like others
387 address AbstractInterpreter::deopt_reexecute_entry(Method* method, address bcp) {
388 assert(method->contains(bcp), "just checkin'");
389 Bytecodes::Code code = Bytecodes::java_code_at(method, bcp);
390 #ifdef COMPILER1
391 if(code == Bytecodes::_athrow ) {
392 return Interpreter::rethrow_exception_entry();
393 }
394 #endif /* COMPILER1 */
395 return Interpreter::deopt_entry(vtos, 0);
396 }
398 // If deoptimization happens, the interpreter should reexecute these bytecodes.
399 // This function mainly helps the compilers to set up the reexecute bit.
400 bool AbstractInterpreter::bytecode_should_reexecute(Bytecodes::Code code) {
401 switch (code) {
402 case Bytecodes::_lookupswitch:
403 case Bytecodes::_tableswitch:
404 case Bytecodes::_fast_binaryswitch:
405 case Bytecodes::_fast_linearswitch:
406 // recompute condtional expression folded into _if<cond>
407 case Bytecodes::_lcmp :
408 case Bytecodes::_fcmpl :
409 case Bytecodes::_fcmpg :
410 case Bytecodes::_dcmpl :
411 case Bytecodes::_dcmpg :
412 case Bytecodes::_ifnull :
413 case Bytecodes::_ifnonnull :
414 case Bytecodes::_goto :
415 case Bytecodes::_goto_w :
416 case Bytecodes::_ifeq :
417 case Bytecodes::_ifne :
418 case Bytecodes::_iflt :
419 case Bytecodes::_ifge :
420 case Bytecodes::_ifgt :
421 case Bytecodes::_ifle :
422 case Bytecodes::_if_icmpeq :
423 case Bytecodes::_if_icmpne :
424 case Bytecodes::_if_icmplt :
425 case Bytecodes::_if_icmpge :
426 case Bytecodes::_if_icmpgt :
427 case Bytecodes::_if_icmple :
428 case Bytecodes::_if_acmpeq :
429 case Bytecodes::_if_acmpne :
430 // special cases
431 case Bytecodes::_getfield :
432 case Bytecodes::_putfield :
433 case Bytecodes::_getstatic :
434 case Bytecodes::_putstatic :
435 case Bytecodes::_aastore :
436 #ifdef COMPILER1
437 //special case of reexecution
438 case Bytecodes::_athrow :
439 #endif
440 return true;
442 default:
443 return false;
444 }
445 }
447 void AbstractInterpreterGenerator::bang_stack_shadow_pages(bool native_call) {
448 // Quick & dirty stack overflow checking: bang the stack & handle trap.
449 // Note that we do the banging after the frame is setup, since the exception
450 // handling code expects to find a valid interpreter frame on the stack.
451 // Doing the banging earlier fails if the caller frame is not an interpreter
452 // frame.
453 // (Also, the exception throwing code expects to unlock any synchronized
454 // method receiever, so do the banging after locking the receiver.)
456 // Bang each page in the shadow zone. We can't assume it's been done for
457 // an interpreter frame with greater than a page of locals, so each page
458 // needs to be checked. Only true for non-native.
459 if (UseStackBanging) {
460 const int start_page = native_call ? StackShadowPages : 1;
461 const int page_size = os::vm_page_size();
462 for (int pages = start_page; pages <= StackShadowPages ; pages++) {
463 __ bang_stack_with_offset(pages*page_size);
464 }
465 }
466 }