1 /*
2 * Copyright 2003-2008 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
24
25 #include "incls/_precompiled.incl"
26 #include "incls/_interpreter_x86_64.cpp.incl"
27
28 #define __ _masm->
29
30 #ifndef CC_INTERP
31
32 const int method_offset = frame::interpreter_frame_method_offset * wordSize;
33 const int bci_offset = frame::interpreter_frame_bcx_offset * wordSize;
34 const int locals_offset = frame::interpreter_frame_locals_offset * wordSize;
35
36 //-----------------------------------------------------------------------------
37
38 address TemplateInterpreterGenerator::generate_StackOverflowError_handler() {
39 address entry = __ pc();
40
41 #ifdef ASSERT
42 {
43 Label L;
44 __ lea(rax, Address(rbp,
45 frame::interpreter_frame_monitor_block_top_offset *
46 wordSize));
47 __ cmpptr(rax, rsp); // rax = maximal rsp for current rbp (stack
48 // grows negative)
49 __ jcc(Assembler::aboveEqual, L); // check if frame is complete
50 __ stop ("interpreter frame not set up");
51 __ bind(L);
52 }
53 #endif // ASSERT
54 // Restore bcp under the assumption that the current frame is still
55 // interpreted
56 __ restore_bcp();
57
58 // expression stack must be empty before entering the VM if an
59 // exception happened
60 __ empty_expression_stack();
61 // throw exception
62 __ call_VM(noreg,
63 CAST_FROM_FN_PTR(address,
64 InterpreterRuntime::throw_StackOverflowError));
65 return entry;
66 }
67
68 address TemplateInterpreterGenerator::generate_ArrayIndexOutOfBounds_handler(
69 const char* name) {
70 address entry = __ pc();
71 // expression stack must be empty before entering the VM if an
72 // exception happened
73 __ empty_expression_stack();
74 // setup parameters
75 // ??? convention: expect aberrant index in register ebx
76 __ lea(c_rarg1, ExternalAddress((address)name));
77 __ call_VM(noreg,
78 CAST_FROM_FN_PTR(address,
79 InterpreterRuntime::
80 throw_ArrayIndexOutOfBoundsException),
81 c_rarg1, rbx);
82 return entry;
83 }
84
85 address TemplateInterpreterGenerator::generate_ClassCastException_handler() {
86 address entry = __ pc();
87
88 // object is at TOS
89 __ pop(c_rarg1);
90
91 // expression stack must be empty before entering the VM if an
92 // exception happened
93 __ empty_expression_stack();
94
95 __ call_VM(noreg,
96 CAST_FROM_FN_PTR(address,
97 InterpreterRuntime::
98 throw_ClassCastException),
99 c_rarg1);
100 return entry;
101 }
102
103 #ifdef ASSERT
104 address last_WrongMethodType_caller;
105 #endif //ASSERT
106
107 // Arguments are: required type in rarg1, failing object (or NULL) in rarg2
108 address TemplateInterpreterGenerator::generate_WrongMethodType_handler() {
109 address entry = __ pc();
110
111 __ pop(rax); // address raising the error (for debug)
112 #ifdef ASSERT
113 __ lea(c_rarg1, ExternalAddress((address) &last_WrongMethodType_caller));
114 __ movptr(Address(c_rarg1, 0), rax);
115 #endif //ASSERT
116
117 __ pop(c_rarg2); // failing object is at TOS
118 __ pop(c_rarg1); // required type is at TOS+8
119
120 // expression stack must be empty before entering the VM if an
121 // exception happened
122 __ empty_expression_stack();
123
124 __ call_VM(noreg,
125 CAST_FROM_FN_PTR(address,
126 InterpreterRuntime::
127 throw_WrongMethodTypeException),
128 // pass required type, failing object (or NULL)
129 c_rarg1, c_rarg2);
130 return entry;
131 }
132
133 address TemplateInterpreterGenerator::generate_exception_handler_common(
134 const char* name, const char* message, bool pass_oop) {
135 assert(!pass_oop || message == NULL, "either oop or message but not both");
136 address entry = __ pc();
137 if (pass_oop) {
138 // object is at TOS
139 __ pop(c_rarg2);
140 }
141 // expression stack must be empty before entering the VM if an
142 // exception happened
143 __ empty_expression_stack();
144 // setup parameters
145 __ lea(c_rarg1, ExternalAddress((address)name));
146 if (pass_oop) {
147 __ call_VM(rax, CAST_FROM_FN_PTR(address,
148 InterpreterRuntime::
149 create_klass_exception),
150 c_rarg1, c_rarg2);
151 } else {
152 // kind of lame ExternalAddress can't take NULL because
153 // external_word_Relocation will assert.
154 if (message != NULL) {
155 __ lea(c_rarg2, ExternalAddress((address)message));
156 } else {
157 __ movptr(c_rarg2, NULL_WORD);
158 }
159 __ call_VM(rax,
160 CAST_FROM_FN_PTR(address, InterpreterRuntime::create_exception),
161 c_rarg1, c_rarg2);
162 }
163 // throw exception
164 __ jump(ExternalAddress(Interpreter::throw_exception_entry()));
165 return entry;
166 }
167
168
169 address TemplateInterpreterGenerator::generate_continuation_for(TosState state) {
170 address entry = __ pc();
171 // NULL last_sp until next java call
172 __ movptr(Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize), (int32_t)NULL_WORD);
173 __ dispatch_next(state);
174 return entry;
175 }
176
177
178 address TemplateInterpreterGenerator::generate_return_entry_for(TosState state,
179 int step,
180 bool unbox) {
181 TosState incoming_state = state;
182 if (InvokeDynamic) {
183 if (unbox) {
184 incoming_state = atos;
185 }
186 Unimplemented();
187 } else {
188 assert(!unbox, "old behavior");
189 }
190
191 // amd64 doesn't need to do anything special about compiled returns
192 // to the interpreter so the code that exists on x86 to place a sentinel
193 // here and the specialized cleanup code is not needed here.
194
195 address entry = __ pc();
196
197 // Restore stack bottom in case i2c adjusted stack
198 __ movptr(rsp, Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize));
199 // and NULL it as marker that esp is now tos until next java call
200 __ movptr(Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize), (int32_t)NULL_WORD);
201
202 __ restore_bcp();
203 __ restore_locals();
204
205 Label L_got_cache, L_giant_index;
206 if (InvokeDynamic) {
207 __ cmpb(Address(r13, 0), Bytecodes::_invokedynamic);
208 __ jcc(Assembler::equal, L_giant_index);
209 }
210 __ get_cache_and_index_at_bcp(rbx, rcx, 1, false);
211 __ bind(L_got_cache);
212 __ movl(rbx, Address(rbx, rcx,
213 Address::times_8,
214 in_bytes(constantPoolCacheOopDesc::base_offset()) +
215 3 * wordSize));
216 __ andl(rbx, 0xFF);
217 if (TaggedStackInterpreter) __ shll(rbx, 1); // 2 slots per parameter.
218 __ lea(rsp, Address(rsp, rbx, Address::times_8));
219 __ dispatch_next(state, step);
220
221 if (InvokeDynamic) {
222 __ get_cache_and_index_at_bcp(rbx, rcx, 1, true);
223 __ jmp(L_got_cache);
224 }
225
226 return entry;
227 }
228
229
230 address TemplateInterpreterGenerator::generate_deopt_entry_for(TosState state,
231 int step) {
232 address entry = __ pc();
233 // NULL last_sp until next java call
234 __ movptr(Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize), (int32_t)NULL_WORD);
235 __ restore_bcp();
236 __ restore_locals();
237 // handle exceptions
238 {
239 Label L;
240 __ cmpptr(Address(r15_thread, Thread::pending_exception_offset()), (int32_t) NULL_WORD);
241 __ jcc(Assembler::zero, L);
242 __ call_VM(noreg,
243 CAST_FROM_FN_PTR(address,
244 InterpreterRuntime::throw_pending_exception));
245 __ should_not_reach_here();
246 __ bind(L);
247 }
248 __ dispatch_next(state, step);
249 return entry;
250 }
251
252 int AbstractInterpreter::BasicType_as_index(BasicType type) {
253 int i = 0;
254 switch (type) {
255 case T_BOOLEAN: i = 0; break;
256 case T_CHAR : i = 1; break;
257 case T_BYTE : i = 2; break;
258 case T_SHORT : i = 3; break;
259 case T_INT : i = 4; break;
260 case T_LONG : i = 5; break;
261 case T_VOID : i = 6; break;
262 case T_FLOAT : i = 7; break;
263 case T_DOUBLE : i = 8; break;
264 case T_OBJECT : i = 9; break;
265 case T_ARRAY : i = 9; break;
266 default : ShouldNotReachHere();
267 }
268 assert(0 <= i && i < AbstractInterpreter::number_of_result_handlers,
269 "index out of bounds");
270 return i;
271 }
272
273
274 address TemplateInterpreterGenerator::generate_result_handler_for(
275 BasicType type) {
276 address entry = __ pc();
277 switch (type) {
278 case T_BOOLEAN: __ c2bool(rax); break;
279 case T_CHAR : __ movzwl(rax, rax); break;
280 case T_BYTE : __ sign_extend_byte(rax); break;
281 case T_SHORT : __ sign_extend_short(rax); break;
282 case T_INT : /* nothing to do */ break;
283 case T_LONG : /* nothing to do */ break;
284 case T_VOID : /* nothing to do */ break;
285 case T_FLOAT : /* nothing to do */ break;
286 case T_DOUBLE : /* nothing to do */ break;
287 case T_OBJECT :
288 // retrieve result from frame
289 __ movptr(rax, Address(rbp, frame::interpreter_frame_oop_temp_offset*wordSize));
290 // and verify it
291 __ verify_oop(rax);
292 break;
293 default : ShouldNotReachHere();
294 }
295 __ ret(0); // return from result handler
296 return entry;
297 }
298
299 address TemplateInterpreterGenerator::generate_safept_entry_for(
300 TosState state,
301 address runtime_entry) {
302 address entry = __ pc();
303 __ push(state);
304 __ call_VM(noreg, runtime_entry);
305 __ dispatch_via(vtos, Interpreter::_normal_table.table_for(vtos));
306 return entry;
307 }
308
309
310
311 // Helpers for commoning out cases in the various type of method entries.
312 //
313
314
315 // increment invocation count & check for overflow
316 //
317 // Note: checking for negative value instead of overflow
318 // so we have a 'sticky' overflow test
319 //
320 // rbx: method
321 // ecx: invocation counter
322 //
323 void InterpreterGenerator::generate_counter_incr(
324 Label* overflow,
325 Label* profile_method,
326 Label* profile_method_continue) {
327
328 const Address invocation_counter(rbx,
329 methodOopDesc::invocation_counter_offset() +
330 InvocationCounter::counter_offset());
331 const Address backedge_counter(rbx,
332 methodOopDesc::backedge_counter_offset() +
333 InvocationCounter::counter_offset());
334
335 if (ProfileInterpreter) { // %%% Merge this into methodDataOop
336 __ incrementl(Address(rbx,
337 methodOopDesc::interpreter_invocation_counter_offset()));
338 }
339 // Update standard invocation counters
340 __ movl(rax, backedge_counter); // load backedge counter
341
342 __ incrementl(rcx, InvocationCounter::count_increment);
343 __ andl(rax, InvocationCounter::count_mask_value); // mask out the
344 // status bits
345
346 __ movl(invocation_counter, rcx); // save invocation count
347 __ addl(rcx, rax); // add both counters
348
349 // profile_method is non-null only for interpreted method so
350 // profile_method != NULL == !native_call
351
352 if (ProfileInterpreter && profile_method != NULL) {
353 // Test to see if we should create a method data oop
354 __ cmp32(rcx, ExternalAddress((address)&InvocationCounter::InterpreterProfileLimit));
355 __ jcc(Assembler::less, *profile_method_continue);
356
357 // if no method data exists, go to profile_method
358 __ test_method_data_pointer(rax, *profile_method);
359 }
360
361 __ cmp32(rcx, ExternalAddress((address)&InvocationCounter::InterpreterInvocationLimit));
362 __ jcc(Assembler::aboveEqual, *overflow);
363 }
364
365 void InterpreterGenerator::generate_counter_overflow(Label* do_continue) {
366
367 // Asm interpreter on entry
368 // r14 - locals
369 // r13 - bcp
370 // rbx - method
371 // edx - cpool --- DOES NOT APPEAR TO BE TRUE
372 // rbp - interpreter frame
373
374 // On return (i.e. jump to entry_point) [ back to invocation of interpreter ]
375 // Everything as it was on entry
376 // rdx is not restored. Doesn't appear to really be set.
377
378 const Address size_of_parameters(rbx,
379 methodOopDesc::size_of_parameters_offset());
380
381 // InterpreterRuntime::frequency_counter_overflow takes two
382 // arguments, the first (thread) is passed by call_VM, the second
383 // indicates if the counter overflow occurs at a backwards branch
384 // (NULL bcp). We pass zero for it. The call returns the address
385 // of the verified entry point for the method or NULL if the
386 // compilation did not complete (either went background or bailed
387 // out).
388 __ movl(c_rarg1, 0);
389 __ call_VM(noreg,
390 CAST_FROM_FN_PTR(address,
391 InterpreterRuntime::frequency_counter_overflow),
392 c_rarg1);
393
394 __ movptr(rbx, Address(rbp, method_offset)); // restore methodOop
395 // Preserve invariant that r13/r14 contain bcp/locals of sender frame
396 // and jump to the interpreted entry.
397 __ jmp(*do_continue, relocInfo::none);
398 }
399
400 // See if we've got enough room on the stack for locals plus overhead.
401 // The expression stack grows down incrementally, so the normal guard
402 // page mechanism will work for that.
403 //
404 // NOTE: Since the additional locals are also always pushed (wasn't
405 // obvious in generate_method_entry) so the guard should work for them
406 // too.
407 //
408 // Args:
409 // rdx: number of additional locals this frame needs (what we must check)
410 // rbx: methodOop
411 //
412 // Kills:
413 // rax
414 void InterpreterGenerator::generate_stack_overflow_check(void) {
415
416 // monitor entry size: see picture of stack set
417 // (generate_method_entry) and frame_amd64.hpp
418 const int entry_size = frame::interpreter_frame_monitor_size() * wordSize;
419
420 // total overhead size: entry_size + (saved rbp through expr stack
421 // bottom). be sure to change this if you add/subtract anything
422 // to/from the overhead area
423 const int overhead_size =
424 -(frame::interpreter_frame_initial_sp_offset * wordSize) + entry_size;
425
426 const int page_size = os::vm_page_size();
427
428 Label after_frame_check;
429
430 // see if the frame is greater than one page in size. If so,
431 // then we need to verify there is enough stack space remaining
432 // for the additional locals.
433 __ cmpl(rdx, (page_size - overhead_size) / Interpreter::stackElementSize());
434 __ jcc(Assembler::belowEqual, after_frame_check);
435
436 // compute rsp as if this were going to be the last frame on
437 // the stack before the red zone
438
439 const Address stack_base(r15_thread, Thread::stack_base_offset());
440 const Address stack_size(r15_thread, Thread::stack_size_offset());
441
442 // locals + overhead, in bytes
443 __ mov(rax, rdx);
444 __ shlptr(rax, Interpreter::logStackElementSize()); // 2 slots per parameter.
445 __ addptr(rax, overhead_size);
446
447 #ifdef ASSERT
448 Label stack_base_okay, stack_size_okay;
449 // verify that thread stack base is non-zero
450 __ cmpptr(stack_base, (int32_t)NULL_WORD);
451 __ jcc(Assembler::notEqual, stack_base_okay);
452 __ stop("stack base is zero");
453 __ bind(stack_base_okay);
454 // verify that thread stack size is non-zero
455 __ cmpptr(stack_size, 0);
456 __ jcc(Assembler::notEqual, stack_size_okay);
457 __ stop("stack size is zero");
458 __ bind(stack_size_okay);
459 #endif
460
461 // Add stack base to locals and subtract stack size
462 __ addptr(rax, stack_base);
463 __ subptr(rax, stack_size);
464
465 // add in the red and yellow zone sizes
466 __ addptr(rax, (StackRedPages + StackYellowPages) * page_size);
467
468 // check against the current stack bottom
469 __ cmpptr(rsp, rax);
470 __ jcc(Assembler::above, after_frame_check);
471
472 __ pop(rax); // get return address
473 __ jump(ExternalAddress(Interpreter::throw_StackOverflowError_entry()));
474
475 // all done with frame size check
476 __ bind(after_frame_check);
477 }
478
479 // Allocate monitor and lock method (asm interpreter)
480 //
481 // Args:
482 // rbx: methodOop
483 // r14: locals
484 //
485 // Kills:
486 // rax
487 // c_rarg0, c_rarg1, c_rarg2, c_rarg3, ...(param regs)
488 // rscratch1, rscratch2 (scratch regs)
489 void InterpreterGenerator::lock_method(void) {
490 // synchronize method
491 const Address access_flags(rbx, methodOopDesc::access_flags_offset());
492 const Address monitor_block_top(
493 rbp,
494 frame::interpreter_frame_monitor_block_top_offset * wordSize);
495 const int entry_size = frame::interpreter_frame_monitor_size() * wordSize;
496
497 #ifdef ASSERT
498 {
499 Label L;
500 __ movl(rax, access_flags);
501 __ testl(rax, JVM_ACC_SYNCHRONIZED);
502 __ jcc(Assembler::notZero, L);
503 __ stop("method doesn't need synchronization");
504 __ bind(L);
505 }
506 #endif // ASSERT
507
508 // get synchronization object
509 {
510 const int mirror_offset = klassOopDesc::klass_part_offset_in_bytes() +
511 Klass::java_mirror_offset_in_bytes();
512 Label done;
513 __ movl(rax, access_flags);
514 __ testl(rax, JVM_ACC_STATIC);
515 // get receiver (assume this is frequent case)
516 __ movptr(rax, Address(r14, Interpreter::local_offset_in_bytes(0)));
517 __ jcc(Assembler::zero, done);
518 __ movptr(rax, Address(rbx, methodOopDesc::constants_offset()));
519 __ movptr(rax, Address(rax,
520 constantPoolOopDesc::pool_holder_offset_in_bytes()));
521 __ movptr(rax, Address(rax, mirror_offset));
522
523 #ifdef ASSERT
524 {
525 Label L;
526 __ testptr(rax, rax);
527 __ jcc(Assembler::notZero, L);
528 __ stop("synchronization object is NULL");
529 __ bind(L);
530 }
531 #endif // ASSERT
532
533 __ bind(done);
534 }
535
536 // add space for monitor & lock
537 __ subptr(rsp, entry_size); // add space for a monitor entry
538 __ movptr(monitor_block_top, rsp); // set new monitor block top
539 // store object
540 __ movptr(Address(rsp, BasicObjectLock::obj_offset_in_bytes()), rax);
541 __ movptr(c_rarg1, rsp); // object address
542 __ lock_object(c_rarg1);
543 }
544
545 // Generate a fixed interpreter frame. This is identical setup for
546 // interpreted methods and for native methods hence the shared code.
547 //
548 // Args:
549 // rax: return address
550 // rbx: methodOop
551 // r14: pointer to locals
552 // r13: sender sp
553 // rdx: cp cache
554 void TemplateInterpreterGenerator::generate_fixed_frame(bool native_call) {
555 // initialize fixed part of activation frame
556 __ push(rax); // save return address
557 __ enter(); // save old & set new rbp
558 __ push(r13); // set sender sp
559 __ push((int)NULL_WORD); // leave last_sp as null
560 __ movptr(r13, Address(rbx, methodOopDesc::const_offset())); // get constMethodOop
561 __ lea(r13, Address(r13, constMethodOopDesc::codes_offset())); // get codebase
562 __ push(rbx); // save methodOop
563 if (ProfileInterpreter) {
564 Label method_data_continue;
565 __ movptr(rdx, Address(rbx, in_bytes(methodOopDesc::method_data_offset())));
566 __ testptr(rdx, rdx);
567 __ jcc(Assembler::zero, method_data_continue);
568 __ addptr(rdx, in_bytes(methodDataOopDesc::data_offset()));
569 __ bind(method_data_continue);
570 __ push(rdx); // set the mdp (method data pointer)
571 } else {
572 __ push(0);
573 }
574
575 __ movptr(rdx, Address(rbx, methodOopDesc::constants_offset()));
576 __ movptr(rdx, Address(rdx, constantPoolOopDesc::cache_offset_in_bytes()));
577 __ push(rdx); // set constant pool cache
578 __ push(r14); // set locals pointer
579 if (native_call) {
580 __ push(0); // no bcp
581 } else {
582 __ push(r13); // set bcp
583 }
584 __ push(0); // reserve word for pointer to expression stack bottom
585 __ movptr(Address(rsp, 0), rsp); // set expression stack bottom
586 }
587
588 // End of helpers
589
590 // Various method entries
591 //------------------------------------------------------------------------------------------------------------------------
592 //
593 //
594
595 // Call an accessor method (assuming it is resolved, otherwise drop
596 // into vanilla (slow path) entry
597 address InterpreterGenerator::generate_accessor_entry(void) {
598 // rbx: methodOop
599
600 // r13: senderSP must preserver for slow path, set SP to it on fast path
601
602 address entry_point = __ pc();
603 Label xreturn_path;
604
605 // do fastpath for resolved accessor methods
606 if (UseFastAccessorMethods) {
607 // Code: _aload_0, _(i|a)getfield, _(i|a)return or any rewrites
608 // thereof; parameter size = 1
609 // Note: We can only use this code if the getfield has been resolved
610 // and if we don't have a null-pointer exception => check for
611 // these conditions first and use slow path if necessary.
612 Label slow_path;
613 // If we need a safepoint check, generate full interpreter entry.
614 __ cmp32(ExternalAddress(SafepointSynchronize::address_of_state()),
615 SafepointSynchronize::_not_synchronized);
616
617 __ jcc(Assembler::notEqual, slow_path);
618 // rbx: method
619 __ movptr(rax, Address(rsp, wordSize));
620
621 // check if local 0 != NULL and read field
622 __ testptr(rax, rax);
623 __ jcc(Assembler::zero, slow_path);
624
625 __ movptr(rdi, Address(rbx, methodOopDesc::constants_offset()));
626 // read first instruction word and extract bytecode @ 1 and index @ 2
627 __ movptr(rdx, Address(rbx, methodOopDesc::const_offset()));
628 __ movl(rdx, Address(rdx, constMethodOopDesc::codes_offset()));
629 // Shift codes right to get the index on the right.
630 // The bytecode fetched looks like <index><0xb4><0x2a>
631 __ shrl(rdx, 2 * BitsPerByte);
632 __ shll(rdx, exact_log2(in_words(ConstantPoolCacheEntry::size())));
633 __ movptr(rdi, Address(rdi, constantPoolOopDesc::cache_offset_in_bytes()));
634
635 // rax: local 0
636 // rbx: method
637 // rdx: constant pool cache index
638 // rdi: constant pool cache
639
640 // check if getfield has been resolved and read constant pool cache entry
641 // check the validity of the cache entry by testing whether _indices field
642 // contains Bytecode::_getfield in b1 byte.
643 assert(in_words(ConstantPoolCacheEntry::size()) == 4,
644 "adjust shift below");
645 __ movl(rcx,
646 Address(rdi,
647 rdx,
648 Address::times_8,
649 constantPoolCacheOopDesc::base_offset() +
650 ConstantPoolCacheEntry::indices_offset()));
651 __ shrl(rcx, 2 * BitsPerByte);
652 __ andl(rcx, 0xFF);
653 __ cmpl(rcx, Bytecodes::_getfield);
654 __ jcc(Assembler::notEqual, slow_path);
655
656 // Note: constant pool entry is not valid before bytecode is resolved
657 __ movptr(rcx,
658 Address(rdi,
659 rdx,
660 Address::times_8,
661 constantPoolCacheOopDesc::base_offset() +
662 ConstantPoolCacheEntry::f2_offset()));
663 // edx: flags
664 __ movl(rdx,
665 Address(rdi,
666 rdx,
667 Address::times_8,
668 constantPoolCacheOopDesc::base_offset() +
669 ConstantPoolCacheEntry::flags_offset()));
670
671 Label notObj, notInt, notByte, notShort;
672 const Address field_address(rax, rcx, Address::times_1);
673
674 // Need to differentiate between igetfield, agetfield, bgetfield etc.
675 // because they are different sizes.
676 // Use the type from the constant pool cache
677 __ shrl(rdx, ConstantPoolCacheEntry::tosBits);
678 // Make sure we don't need to mask edx for tosBits after the above shift
679 ConstantPoolCacheEntry::verify_tosBits();
680
681 __ cmpl(rdx, atos);
682 __ jcc(Assembler::notEqual, notObj);
683 // atos
684 __ load_heap_oop(rax, field_address);
685 __ jmp(xreturn_path);
686
687 __ bind(notObj);
688 __ cmpl(rdx, itos);
689 __ jcc(Assembler::notEqual, notInt);
690 // itos
691 __ movl(rax, field_address);
692 __ jmp(xreturn_path);
693
694 __ bind(notInt);
695 __ cmpl(rdx, btos);
696 __ jcc(Assembler::notEqual, notByte);
697 // btos
698 __ load_signed_byte(rax, field_address);
699 __ jmp(xreturn_path);
700
701 __ bind(notByte);
702 __ cmpl(rdx, stos);
703 __ jcc(Assembler::notEqual, notShort);
704 // stos
705 __ load_signed_word(rax, field_address);
706 __ jmp(xreturn_path);
707
708 __ bind(notShort);
709 #ifdef ASSERT
710 Label okay;
711 __ cmpl(rdx, ctos);
712 __ jcc(Assembler::equal, okay);
713 __ stop("what type is this?");
714 __ bind(okay);
715 #endif
716 // ctos
717 __ load_unsigned_word(rax, field_address);
718
719 __ bind(xreturn_path);
720
721 // _ireturn/_areturn
722 __ pop(rdi);
723 __ mov(rsp, r13);
724 __ jmp(rdi);
725 __ ret(0);
726
727 // generate a vanilla interpreter entry as the slow path
728 __ bind(slow_path);
729 (void) generate_normal_entry(false);
730 } else {
731 (void) generate_normal_entry(false);
732 }
733
734 return entry_point;
735 }
736
737 // Interpreter stub for calling a native method. (asm interpreter)
738 // This sets up a somewhat different looking stack for calling the
739 // native method than the typical interpreter frame setup.
740 address InterpreterGenerator::generate_native_entry(bool synchronized) {
741 // determine code generation flags
742 bool inc_counter = UseCompiler || CountCompiledCalls;
743
744 // rbx: methodOop
745 // r13: sender sp
746
747 address entry_point = __ pc();
748
749 const Address size_of_parameters(rbx, methodOopDesc::
750 size_of_parameters_offset());
751 const Address invocation_counter(rbx, methodOopDesc::
752 invocation_counter_offset() +
753 InvocationCounter::counter_offset());
754 const Address access_flags (rbx, methodOopDesc::access_flags_offset());
755
756 // get parameter size (always needed)
757 __ load_unsigned_word(rcx, size_of_parameters);
758
759 // native calls don't need the stack size check since they have no
760 // expression stack and the arguments are already on the stack and
761 // we only add a handful of words to the stack
762
763 // rbx: methodOop
764 // rcx: size of parameters
765 // r13: sender sp
766 __ pop(rax); // get return address
767
768 // for natives the size of locals is zero
769
770 // compute beginning of parameters (r14)
771 if (TaggedStackInterpreter) __ shll(rcx, 1); // 2 slots per parameter.
772 __ lea(r14, Address(rsp, rcx, Address::times_8, -wordSize));
773
774 // add 2 zero-initialized slots for native calls
775 // initialize result_handler slot
776 __ push((int) NULL_WORD);
777 // slot for oop temp
778 // (static native method holder mirror/jni oop result)
779 __ push((int) NULL_WORD);
780
781 if (inc_counter) {
782 __ movl(rcx, invocation_counter); // (pre-)fetch invocation count
783 }
784
785 // initialize fixed part of activation frame
786 generate_fixed_frame(true);
787
788 // make sure method is native & not abstract
789 #ifdef ASSERT
790 __ movl(rax, access_flags);
791 {
792 Label L;
793 __ testl(rax, JVM_ACC_NATIVE);
794 __ jcc(Assembler::notZero, L);
795 __ stop("tried to execute non-native method as native");
796 __ bind(L);
797 }
798 {
799 Label L;
800 __ testl(rax, JVM_ACC_ABSTRACT);
801 __ jcc(Assembler::zero, L);
802 __ stop("tried to execute abstract method in interpreter");
803 __ bind(L);
804 }
805 #endif
806
807 // Since at this point in the method invocation the exception handler
808 // would try to exit the monitor of synchronized methods which hasn't
809 // been entered yet, we set the thread local variable
810 // _do_not_unlock_if_synchronized to true. The remove_activation will
811 // check this flag.
812
813 const Address do_not_unlock_if_synchronized(r15_thread,
814 in_bytes(JavaThread::do_not_unlock_if_synchronized_offset()));
815 __ movbool(do_not_unlock_if_synchronized, true);
816
817 // increment invocation count & check for overflow
818 Label invocation_counter_overflow;
819 if (inc_counter) {
820 generate_counter_incr(&invocation_counter_overflow, NULL, NULL);
821 }
822
823 Label continue_after_compile;
824 __ bind(continue_after_compile);
825
826 bang_stack_shadow_pages(true);
827
828 // reset the _do_not_unlock_if_synchronized flag
829 __ movbool(do_not_unlock_if_synchronized, false);
830
831 // check for synchronized methods
832 // Must happen AFTER invocation_counter check and stack overflow check,
833 // so method is not locked if overflows.
834 if (synchronized) {
835 lock_method();
836 } else {
837 // no synchronization necessary
838 #ifdef ASSERT
839 {
840 Label L;
841 __ movl(rax, access_flags);
842 __ testl(rax, JVM_ACC_SYNCHRONIZED);
843 __ jcc(Assembler::zero, L);
844 __ stop("method needs synchronization");
845 __ bind(L);
846 }
847 #endif
848 }
849
850 // start execution
851 #ifdef ASSERT
852 {
853 Label L;
854 const Address monitor_block_top(rbp,
855 frame::interpreter_frame_monitor_block_top_offset * wordSize);
856 __ movptr(rax, monitor_block_top);
857 __ cmpptr(rax, rsp);
858 __ jcc(Assembler::equal, L);
859 __ stop("broken stack frame setup in interpreter");
860 __ bind(L);
861 }
862 #endif
863
864 // jvmti support
865 __ notify_method_entry();
866
867 // work registers
868 const Register method = rbx;
869 const Register t = r11;
870
871 // allocate space for parameters
872 __ get_method(method);
873 __ verify_oop(method);
874 __ load_unsigned_word(t,
875 Address(method,
876 methodOopDesc::size_of_parameters_offset()));
877 __ shll(t, Interpreter::logStackElementSize());
878
879 __ subptr(rsp, t);
880 __ subptr(rsp, frame::arg_reg_save_area_bytes); // windows
881 __ andptr(rsp, -16); // must be 16 byte boundry (see amd64 ABI)
882
883 // get signature handler
884 {
885 Label L;
886 __ movptr(t, Address(method, methodOopDesc::signature_handler_offset()));
887 __ testptr(t, t);
888 __ jcc(Assembler::notZero, L);
889 __ call_VM(noreg,
890 CAST_FROM_FN_PTR(address,
891 InterpreterRuntime::prepare_native_call),
892 method);
893 __ get_method(method);
894 __ movptr(t, Address(method, methodOopDesc::signature_handler_offset()));
895 __ bind(L);
896 }
897
898 // call signature handler
899 assert(InterpreterRuntime::SignatureHandlerGenerator::from() == r14,
900 "adjust this code");
901 assert(InterpreterRuntime::SignatureHandlerGenerator::to() == rsp,
902 "adjust this code");
903 assert(InterpreterRuntime::SignatureHandlerGenerator::temp() == rscratch1,
904 "adjust this code");
905
906 // The generated handlers do not touch RBX (the method oop).
907 // However, large signatures cannot be cached and are generated
908 // each time here. The slow-path generator can do a GC on return,
909 // so we must reload it after the call.
910 __ call(t);
911 __ get_method(method); // slow path can do a GC, reload RBX
912
913
914 // result handler is in rax
915 // set result handler
916 __ movptr(Address(rbp,
917 (frame::interpreter_frame_result_handler_offset) * wordSize),
918 rax);
919
920 // pass mirror handle if static call
921 {
922 Label L;
923 const int mirror_offset = klassOopDesc::klass_part_offset_in_bytes() +
924 Klass::java_mirror_offset_in_bytes();
925 __ movl(t, Address(method, methodOopDesc::access_flags_offset()));
926 __ testl(t, JVM_ACC_STATIC);
927 __ jcc(Assembler::zero, L);
928 // get mirror
929 __ movptr(t, Address(method, methodOopDesc::constants_offset()));
930 __ movptr(t, Address(t, constantPoolOopDesc::pool_holder_offset_in_bytes()));
931 __ movptr(t, Address(t, mirror_offset));
932 // copy mirror into activation frame
933 __ movptr(Address(rbp, frame::interpreter_frame_oop_temp_offset * wordSize),
934 t);
935 // pass handle to mirror
936 __ lea(c_rarg1,
937 Address(rbp, frame::interpreter_frame_oop_temp_offset * wordSize));
938 __ bind(L);
939 }
940
941 // get native function entry point
942 {
943 Label L;
944 __ movptr(rax, Address(method, methodOopDesc::native_function_offset()));
945 ExternalAddress unsatisfied(SharedRuntime::native_method_throw_unsatisfied_link_error_entry());
946 __ movptr(rscratch2, unsatisfied.addr());
947 __ cmpptr(rax, rscratch2);
948 __ jcc(Assembler::notEqual, L);
949 __ call_VM(noreg,
950 CAST_FROM_FN_PTR(address,
951 InterpreterRuntime::prepare_native_call),
952 method);
953 __ get_method(method);
954 __ verify_oop(method);
955 __ movptr(rax, Address(method, methodOopDesc::native_function_offset()));
956 __ bind(L);
957 }
958
959 // pass JNIEnv
960 __ lea(c_rarg0, Address(r15_thread, JavaThread::jni_environment_offset()));
961
962 // It is enough that the pc() points into the right code
963 // segment. It does not have to be the correct return pc.
964 __ set_last_Java_frame(rsp, rbp, (address) __ pc());
965
966 // change thread state
967 #ifdef ASSERT
968 {
969 Label L;
970 __ movl(t, Address(r15_thread, JavaThread::thread_state_offset()));
971 __ cmpl(t, _thread_in_Java);
972 __ jcc(Assembler::equal, L);
973 __ stop("Wrong thread state in native stub");
974 __ bind(L);
975 }
976 #endif
977
978 // Change state to native
979
980 __ movl(Address(r15_thread, JavaThread::thread_state_offset()),
981 _thread_in_native);
982
983 // Call the native method.
984 __ call(rax);
985 // result potentially in rax or xmm0
986
987 // Depending on runtime options, either restore the MXCSR
988 // register after returning from the JNI Call or verify that
989 // it wasn't changed during -Xcheck:jni.
990 if (RestoreMXCSROnJNICalls) {
991 __ ldmxcsr(ExternalAddress(StubRoutines::x86::mxcsr_std()));
992 }
993 else if (CheckJNICalls) {
994 __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, StubRoutines::x86::verify_mxcsr_entry())));
995 }
996
997 // NOTE: The order of these pushes is known to frame::interpreter_frame_result
998 // in order to extract the result of a method call. If the order of these
999 // pushes change or anything else is added to the stack then the code in
1000 // interpreter_frame_result must also change.
1001
1002 __ push(dtos);
1003 __ push(ltos);
1004
1005 // change thread state
1006 __ movl(Address(r15_thread, JavaThread::thread_state_offset()),
1007 _thread_in_native_trans);
1008
1009 if (os::is_MP()) {
1010 if (UseMembar) {
1011 // Force this write out before the read below
1012 __ membar(Assembler::Membar_mask_bits(
1013 Assembler::LoadLoad | Assembler::LoadStore |
1014 Assembler::StoreLoad | Assembler::StoreStore));
1015 } else {
1016 // Write serialization page so VM thread can do a pseudo remote membar.
1017 // We use the current thread pointer to calculate a thread specific
1018 // offset to write to within the page. This minimizes bus traffic
1019 // due to cache line collision.
1020 __ serialize_memory(r15_thread, rscratch2);
1021 }
1022 }
1023
1024 // check for safepoint operation in progress and/or pending suspend requests
1025 {
1026 Label Continue;
1027 __ cmp32(ExternalAddress(SafepointSynchronize::address_of_state()),
1028 SafepointSynchronize::_not_synchronized);
1029
1030 Label L;
1031 __ jcc(Assembler::notEqual, L);
1032 __ cmpl(Address(r15_thread, JavaThread::suspend_flags_offset()), 0);
1033 __ jcc(Assembler::equal, Continue);
1034 __ bind(L);
1035
1036 // Don't use call_VM as it will see a possible pending exception
1037 // and forward it and never return here preventing us from
1038 // clearing _last_native_pc down below. Also can't use
1039 // call_VM_leaf either as it will check to see if r13 & r14 are
1040 // preserved and correspond to the bcp/locals pointers. So we do a
1041 // runtime call by hand.
1042 //
1043 __ mov(c_rarg0, r15_thread);
1044 __ mov(r12, rsp); // remember sp
1045 __ subptr(rsp, frame::arg_reg_save_area_bytes); // windows
1046 __ andptr(rsp, -16); // align stack as required by ABI
1047 __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, JavaThread::check_special_condition_for_native_trans)));
1048 __ mov(rsp, r12); // restore sp
1049 __ reinit_heapbase();
1050 __ bind(Continue);
1051 }
1052
1053 // change thread state
1054 __ movl(Address(r15_thread, JavaThread::thread_state_offset()), _thread_in_Java);
1055
1056 // reset_last_Java_frame
1057 __ reset_last_Java_frame(true, true);
1058
1059 // reset handle block
1060 __ movptr(t, Address(r15_thread, JavaThread::active_handles_offset()));
1061 __ movptr(Address(t, JNIHandleBlock::top_offset_in_bytes()), (int32_t)NULL_WORD);
1062
1063 // If result is an oop unbox and store it in frame where gc will see it
1064 // and result handler will pick it up
1065
1066 {
1067 Label no_oop, store_result;
1068 __ lea(t, ExternalAddress(AbstractInterpreter::result_handler(T_OBJECT)));
1069 __ cmpptr(t, Address(rbp, frame::interpreter_frame_result_handler_offset*wordSize));
1070 __ jcc(Assembler::notEqual, no_oop);
1071 // retrieve result
1072 __ pop(ltos);
1073 __ testptr(rax, rax);
1074 __ jcc(Assembler::zero, store_result);
1075 __ movptr(rax, Address(rax, 0));
1076 __ bind(store_result);
1077 __ movptr(Address(rbp, frame::interpreter_frame_oop_temp_offset*wordSize), rax);
1078 // keep stack depth as expected by pushing oop which will eventually be discarde
1079 __ push(ltos);
1080 __ bind(no_oop);
1081 }
1082
1083
1084 {
1085 Label no_reguard;
1086 __ cmpl(Address(r15_thread, JavaThread::stack_guard_state_offset()),
1087 JavaThread::stack_guard_yellow_disabled);
1088 __ jcc(Assembler::notEqual, no_reguard);
1089
1090 __ pusha(); // XXX only save smashed registers
1091 __ mov(r12, rsp); // remember sp
1092 __ subptr(rsp, frame::arg_reg_save_area_bytes); // windows
1093 __ andptr(rsp, -16); // align stack as required by ABI
1094 __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, SharedRuntime::reguard_yellow_pages)));
1095 __ mov(rsp, r12); // restore sp
1096 __ popa(); // XXX only restore smashed registers
1097 __ reinit_heapbase();
1098
1099 __ bind(no_reguard);
1100 }
1101
1102
1103 // The method register is junk from after the thread_in_native transition
1104 // until here. Also can't call_VM until the bcp has been
1105 // restored. Need bcp for throwing exception below so get it now.
1106 __ get_method(method);
1107 __ verify_oop(method);
1108
1109 // restore r13 to have legal interpreter frame, i.e., bci == 0 <=>
1110 // r13 == code_base()
1111 __ movptr(r13, Address(method, methodOopDesc::const_offset())); // get constMethodOop
1112 __ lea(r13, Address(r13, constMethodOopDesc::codes_offset())); // get codebase
1113 // handle exceptions (exception handling will handle unlocking!)
1114 {
1115 Label L;
1116 __ cmpptr(Address(r15_thread, Thread::pending_exception_offset()), (int32_t) NULL_WORD);
1117 __ jcc(Assembler::zero, L);
1118 // Note: At some point we may want to unify this with the code
1119 // used in call_VM_base(); i.e., we should use the
1120 // StubRoutines::forward_exception code. For now this doesn't work
1121 // here because the rsp is not correctly set at this point.
1122 __ MacroAssembler::call_VM(noreg,
1123 CAST_FROM_FN_PTR(address,
1124 InterpreterRuntime::throw_pending_exception));
1125 __ should_not_reach_here();
1126 __ bind(L);
1127 }
1128
1129 // do unlocking if necessary
1130 {
1131 Label L;
1132 __ movl(t, Address(method, methodOopDesc::access_flags_offset()));
1133 __ testl(t, JVM_ACC_SYNCHRONIZED);
1134 __ jcc(Assembler::zero, L);
1135 // the code below should be shared with interpreter macro
1136 // assembler implementation
1137 {
1138 Label unlock;
1139 // BasicObjectLock will be first in list, since this is a
1140 // synchronized method. However, need to check that the object
1141 // has not been unlocked by an explicit monitorexit bytecode.
1142 const Address monitor(rbp,
1143 (intptr_t)(frame::interpreter_frame_initial_sp_offset *
1144 wordSize - sizeof(BasicObjectLock)));
1145
1146 // monitor expect in c_rarg1 for slow unlock path
1147 __ lea(c_rarg1, monitor); // address of first monitor
1148
1149 __ movptr(t, Address(c_rarg1, BasicObjectLock::obj_offset_in_bytes()));
1150 __ testptr(t, t);
1151 __ jcc(Assembler::notZero, unlock);
1152
1153 // Entry already unlocked, need to throw exception
1154 __ MacroAssembler::call_VM(noreg,
1155 CAST_FROM_FN_PTR(address,
1156 InterpreterRuntime::throw_illegal_monitor_state_exception));
1157 __ should_not_reach_here();
1158
1159 __ bind(unlock);
1160 __ unlock_object(c_rarg1);
1161 }
1162 __ bind(L);
1163 }
1164
1165 // jvmti support
1166 // Note: This must happen _after_ handling/throwing any exceptions since
1167 // the exception handler code notifies the runtime of method exits
1168 // too. If this happens before, method entry/exit notifications are
1169 // not properly paired (was bug - gri 11/22/99).
1170 __ notify_method_exit(vtos, InterpreterMacroAssembler::NotifyJVMTI);
1171
1172 // restore potential result in edx:eax, call result handler to
1173 // restore potential result in ST0 & handle result
1174
1175 __ pop(ltos);
1176 __ pop(dtos);
1177
1178 __ movptr(t, Address(rbp,
1179 (frame::interpreter_frame_result_handler_offset) * wordSize));
1180 __ call(t);
1181
1182 // remove activation
1183 __ movptr(t, Address(rbp,
1184 frame::interpreter_frame_sender_sp_offset *
1185 wordSize)); // get sender sp
1186 __ leave(); // remove frame anchor
1187 __ pop(rdi); // get return address
1188 __ mov(rsp, t); // set sp to sender sp
1189 __ jmp(rdi);
1190
1191 if (inc_counter) {
1192 // Handle overflow of counter and compile method
1193 __ bind(invocation_counter_overflow);
1194 generate_counter_overflow(&continue_after_compile);
1195 }
1196
1197 return entry_point;
1198 }
1199
1200 //
1201 // Generic interpreted method entry to (asm) interpreter
1202 //
1203 address InterpreterGenerator::generate_normal_entry(bool synchronized) {
1204 // determine code generation flags
1205 bool inc_counter = UseCompiler || CountCompiledCalls;
1206
1207 // ebx: methodOop
1208 // r13: sender sp
1209 address entry_point = __ pc();
1210
1211 const Address size_of_parameters(rbx,
1212 methodOopDesc::size_of_parameters_offset());
1213 const Address size_of_locals(rbx, methodOopDesc::size_of_locals_offset());
1214 const Address invocation_counter(rbx,
1215 methodOopDesc::invocation_counter_offset() +
1216 InvocationCounter::counter_offset());
1217 const Address access_flags(rbx, methodOopDesc::access_flags_offset());
1218
1219 // get parameter size (always needed)
1220 __ load_unsigned_word(rcx, size_of_parameters);
1221
1222 // rbx: methodOop
1223 // rcx: size of parameters
1224 // r13: sender_sp (could differ from sp+wordSize if we were called via c2i )
1225
1226 __ load_unsigned_word(rdx, size_of_locals); // get size of locals in words
1227 __ subl(rdx, rcx); // rdx = no. of additional locals
1228
1229 // YYY
1230 // __ incrementl(rdx);
1231 // __ andl(rdx, -2);
1232
1233 // see if we've got enough room on the stack for locals plus overhead.
1234 generate_stack_overflow_check();
1235
1236 // get return address
1237 __ pop(rax);
1238
1239 // compute beginning of parameters (r14)
1240 if (TaggedStackInterpreter) __ shll(rcx, 1); // 2 slots per parameter.
1241 __ lea(r14, Address(rsp, rcx, Address::times_8, -wordSize));
1242
1243 // rdx - # of additional locals
1244 // allocate space for locals
1245 // explicitly initialize locals
1246 {
1247 Label exit, loop;
1248 __ testl(rdx, rdx);
1249 __ jcc(Assembler::lessEqual, exit); // do nothing if rdx <= 0
1250 __ bind(loop);
1251 if (TaggedStackInterpreter) __ push((int) NULL_WORD); // push tag
1252 __ push((int) NULL_WORD); // initialize local variables
1253 __ decrementl(rdx); // until everything initialized
1254 __ jcc(Assembler::greater, loop);
1255 __ bind(exit);
1256 }
1257
1258 // (pre-)fetch invocation count
1259 if (inc_counter) {
1260 __ movl(rcx, invocation_counter);
1261 }
1262 // initialize fixed part of activation frame
1263 generate_fixed_frame(false);
1264
1265 // make sure method is not native & not abstract
1266 #ifdef ASSERT
1267 __ movl(rax, access_flags);
1268 {
1269 Label L;
1270 __ testl(rax, JVM_ACC_NATIVE);
1271 __ jcc(Assembler::zero, L);
1272 __ stop("tried to execute native method as non-native");
1273 __ bind(L);
1274 }
1275 {
1276 Label L;
1277 __ testl(rax, JVM_ACC_ABSTRACT);
1278 __ jcc(Assembler::zero, L);
1279 __ stop("tried to execute abstract method in interpreter");
1280 __ bind(L);
1281 }
1282 #endif
1283
1284 // Since at this point in the method invocation the exception
1285 // handler would try to exit the monitor of synchronized methods
1286 // which hasn't been entered yet, we set the thread local variable
1287 // _do_not_unlock_if_synchronized to true. The remove_activation
1288 // will check this flag.
1289
1290 const Address do_not_unlock_if_synchronized(r15_thread,
1291 in_bytes(JavaThread::do_not_unlock_if_synchronized_offset()));
1292 __ movbool(do_not_unlock_if_synchronized, true);
1293
1294 // increment invocation count & check for overflow
1295 Label invocation_counter_overflow;
1296 Label profile_method;
1297 Label profile_method_continue;
1298 if (inc_counter) {
1299 generate_counter_incr(&invocation_counter_overflow,
1300 &profile_method,
1301 &profile_method_continue);
1302 if (ProfileInterpreter) {
1303 __ bind(profile_method_continue);
1304 }
1305 }
1306
1307 Label continue_after_compile;
1308 __ bind(continue_after_compile);
1309
1310 // check for synchronized interpreted methods
1311 bang_stack_shadow_pages(false);
1312
1313 // reset the _do_not_unlock_if_synchronized flag
1314 __ movbool(do_not_unlock_if_synchronized, false);
1315
1316 // check for synchronized methods
1317 // Must happen AFTER invocation_counter check and stack overflow check,
1318 // so method is not locked if overflows.
1319 if (synchronized) {
1320 // Allocate monitor and lock method
1321 lock_method();
1322 } else {
1323 // no synchronization necessary
1324 #ifdef ASSERT
1325 {
1326 Label L;
1327 __ movl(rax, access_flags);
1328 __ testl(rax, JVM_ACC_SYNCHRONIZED);
1329 __ jcc(Assembler::zero, L);
1330 __ stop("method needs synchronization");
1331 __ bind(L);
1332 }
1333 #endif
1334 }
1335
1336 // start execution
1337 #ifdef ASSERT
1338 {
1339 Label L;
1340 const Address monitor_block_top (rbp,
1341 frame::interpreter_frame_monitor_block_top_offset * wordSize);
1342 __ movptr(rax, monitor_block_top);
1343 __ cmpptr(rax, rsp);
1344 __ jcc(Assembler::equal, L);
1345 __ stop("broken stack frame setup in interpreter");
1346 __ bind(L);
1347 }
1348 #endif
1349
1350 // jvmti support
1351 __ notify_method_entry();
1352
1353 __ dispatch_next(vtos);
1354
1355 // invocation counter overflow
1356 if (inc_counter) {
1357 if (ProfileInterpreter) {
1358 // We have decided to profile this method in the interpreter
1359 __ bind(profile_method);
1360
1361 __ call_VM(noreg,
1362 CAST_FROM_FN_PTR(address, InterpreterRuntime::profile_method),
1363 r13, true);
1364
1365 __ movptr(rbx, Address(rbp, method_offset)); // restore methodOop
1366 __ movptr(rax, Address(rbx,
1367 in_bytes(methodOopDesc::method_data_offset())));
1368 __ movptr(Address(rbp, frame::interpreter_frame_mdx_offset * wordSize),
1369 rax);
1370 __ test_method_data_pointer(rax, profile_method_continue);
1371 __ addptr(rax, in_bytes(methodDataOopDesc::data_offset()));
1372 __ movptr(Address(rbp, frame::interpreter_frame_mdx_offset * wordSize),
1373 rax);
1374 __ jmp(profile_method_continue);
1375 }
1376 // Handle overflow of counter and compile method
1377 __ bind(invocation_counter_overflow);
1378 generate_counter_overflow(&continue_after_compile);
1379 }
1380
1381 return entry_point;
1382 }
1383
1384 // Entry points
1385 //
1386 // Here we generate the various kind of entries into the interpreter.
1387 // The two main entry type are generic bytecode methods and native
1388 // call method. These both come in synchronized and non-synchronized
1389 // versions but the frame layout they create is very similar. The
1390 // other method entry types are really just special purpose entries
1391 // that are really entry and interpretation all in one. These are for
1392 // trivial methods like accessor, empty, or special math methods.
1393 //
1394 // When control flow reaches any of the entry types for the interpreter
1395 // the following holds ->
1396 //
1397 // Arguments:
1398 //
1399 // rbx: methodOop
1400 //
1401 // Stack layout immediately at entry
1402 //
1403 // [ return address ] <--- rsp
1404 // [ parameter n ]
1405 // ...
1406 // [ parameter 1 ]
1407 // [ expression stack ] (caller's java expression stack)
1408
1409 // Assuming that we don't go to one of the trivial specialized entries
1410 // the stack will look like below when we are ready to execute the
1411 // first bytecode (or call the native routine). The register usage
1412 // will be as the template based interpreter expects (see
1413 // interpreter_amd64.hpp).
1414 //
1415 // local variables follow incoming parameters immediately; i.e.
1416 // the return address is moved to the end of the locals).
1417 //
1418 // [ monitor entry ] <--- rsp
1419 // ...
1420 // [ monitor entry ]
1421 // [ expr. stack bottom ]
1422 // [ saved r13 ]
1423 // [ current r14 ]
1424 // [ methodOop ]
1425 // [ saved ebp ] <--- rbp
1426 // [ return address ]
1427 // [ local variable m ]
1428 // ...
1429 // [ local variable 1 ]
1430 // [ parameter n ]
1431 // ...
1432 // [ parameter 1 ] <--- r14
1433
1434 address AbstractInterpreterGenerator::generate_method_entry(
1435 AbstractInterpreter::MethodKind kind) {
1436 // determine code generation flags
1437 bool synchronized = false;
1438 address entry_point = NULL;
1439
1440 switch (kind) {
1441 case Interpreter::zerolocals : break;
1442 case Interpreter::zerolocals_synchronized: synchronized = true; break;
1443 case Interpreter::native : entry_point = ((InterpreterGenerator*) this)->generate_native_entry(false); break;
1444 case Interpreter::native_synchronized : entry_point = ((InterpreterGenerator*) this)->generate_native_entry(true); break;
1445 case Interpreter::empty : entry_point = ((InterpreterGenerator*) this)->generate_empty_entry(); break;
1446 case Interpreter::accessor : entry_point = ((InterpreterGenerator*) this)->generate_accessor_entry(); break;
1447 case Interpreter::abstract : entry_point = ((InterpreterGenerator*) this)->generate_abstract_entry(); break;
1448 case Interpreter::method_handle : entry_point = ((InterpreterGenerator*) this)->generate_method_handle_entry();break;
1449 case Interpreter::java_lang_math_sin : break;
1450 case Interpreter::java_lang_math_cos : break;
1451 case Interpreter::java_lang_math_tan : break;
1452 case Interpreter::java_lang_math_abs : break;
1453 case Interpreter::java_lang_math_log : break;
1454 case Interpreter::java_lang_math_log10 : break;
1455 case Interpreter::java_lang_math_sqrt : entry_point = ((InterpreterGenerator*) this)->generate_math_entry(kind); break;
1456 default : ShouldNotReachHere(); break;
1457 }
1458
1459 if (entry_point) {
1460 return entry_point;
1461 }
1462
1463 return ((InterpreterGenerator*) this)->
1464 generate_normal_entry(synchronized);
1465 }
1466
1467 // How much stack a method activation needs in words.
1468 int AbstractInterpreter::size_top_interpreter_activation(methodOop method) {
1469 const int entry_size = frame::interpreter_frame_monitor_size();
1470
1471 // total overhead size: entry_size + (saved rbp thru expr stack
1472 // bottom). be sure to change this if you add/subtract anything
1473 // to/from the overhead area
1474 const int overhead_size =
1475 -(frame::interpreter_frame_initial_sp_offset) + entry_size;
1476
1477 const int stub_code = frame::entry_frame_after_call_words;
1478 const int extra_stack = methodOopDesc::extra_stack();
1479 const int method_stack = (method->max_locals() + method->max_stack() + extra_stack) *
1480 Interpreter::stackElementWords();
1481 return (overhead_size + method_stack + stub_code);
1482 }
1483
1484 int AbstractInterpreter::layout_activation(methodOop method,
1485 int tempcount,
1486 int popframe_extra_args,
1487 int moncount,
1488 int callee_param_count,
1489 int callee_locals,
1490 frame* caller,
1491 frame* interpreter_frame,
1492 bool is_top_frame) {
1493 // Note: This calculation must exactly parallel the frame setup
1494 // in AbstractInterpreterGenerator::generate_method_entry.
1495 // If interpreter_frame!=NULL, set up the method, locals, and monitors.
1496 // The frame interpreter_frame, if not NULL, is guaranteed to be the
1497 // right size, as determined by a previous call to this method.
1498 // It is also guaranteed to be walkable even though it is in a skeletal state
1499
1500 // fixed size of an interpreter frame:
1501 int max_locals = method->max_locals() * Interpreter::stackElementWords();
1502 int extra_locals = (method->max_locals() - method->size_of_parameters()) *
1503 Interpreter::stackElementWords();
1504
1505 int overhead = frame::sender_sp_offset -
1506 frame::interpreter_frame_initial_sp_offset;
1507 // Our locals were accounted for by the caller (or last_frame_adjust
1508 // on the transistion) Since the callee parameters already account
1509 // for the callee's params we only need to account for the extra
1510 // locals.
1511 int size = overhead +
1512 (callee_locals - callee_param_count)*Interpreter::stackElementWords() +
1513 moncount * frame::interpreter_frame_monitor_size() +
1514 tempcount* Interpreter::stackElementWords() + popframe_extra_args;
1515 if (interpreter_frame != NULL) {
1516 #ifdef ASSERT
1517 assert(caller->unextended_sp() == interpreter_frame->interpreter_frame_sender_sp(),
1518 "Frame not properly walkable");
1519 assert(caller->sp() == interpreter_frame->sender_sp(), "Frame not properly walkable(2)");
1520 #endif
1521
1522 interpreter_frame->interpreter_frame_set_method(method);
1523 // NOTE the difference in using sender_sp and
1524 // interpreter_frame_sender_sp interpreter_frame_sender_sp is
1525 // the original sp of the caller (the unextended_sp) and
1526 // sender_sp is fp+16 XXX
1527 intptr_t* locals = interpreter_frame->sender_sp() + max_locals - 1;
1528
1529 interpreter_frame->interpreter_frame_set_locals(locals);
1530 BasicObjectLock* montop = interpreter_frame->interpreter_frame_monitor_begin();
1531 BasicObjectLock* monbot = montop - moncount;
1532 interpreter_frame->interpreter_frame_set_monitor_end(monbot);
1533
1534 // Set last_sp
1535 intptr_t* esp = (intptr_t*) monbot -
1536 tempcount*Interpreter::stackElementWords() -
1537 popframe_extra_args;
1538 interpreter_frame->interpreter_frame_set_last_sp(esp);
1539
1540 // All frames but the initial (oldest) interpreter frame we fill in have
1541 // a value for sender_sp that allows walking the stack but isn't
1542 // truly correct. Correct the value here.
1543 if (extra_locals != 0 &&
1544 interpreter_frame->sender_sp() ==
1545 interpreter_frame->interpreter_frame_sender_sp()) {
1546 interpreter_frame->set_interpreter_frame_sender_sp(caller->sp() +
1547 extra_locals);
1548 }
1549 *interpreter_frame->interpreter_frame_cache_addr() =
1550 method->constants()->cache();
1551 }
1552 return size;
1553 }
1554
1555 //-----------------------------------------------------------------------------
1556 // Exceptions
1557
1558 void TemplateInterpreterGenerator::generate_throw_exception() {
1559 // Entry point in previous activation (i.e., if the caller was
1560 // interpreted)
1561 Interpreter::_rethrow_exception_entry = __ pc();
1562 // Restore sp to interpreter_frame_last_sp even though we are going
1563 // to empty the expression stack for the exception processing.
1564 __ movptr(Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize), (int32_t)NULL_WORD);
1565 // rax: exception
1566 // rdx: return address/pc that threw exception
1567 __ restore_bcp(); // r13 points to call/send
1568 __ restore_locals();
1569 __ reinit_heapbase(); // restore r12 as heapbase.
1570 // Entry point for exceptions thrown within interpreter code
1571 Interpreter::_throw_exception_entry = __ pc();
1572 // expression stack is undefined here
1573 // rax: exception
1574 // r13: exception bcp
1575 __ verify_oop(rax);
1576 __ mov(c_rarg1, rax);
1577
1578 // expression stack must be empty before entering the VM in case of
1579 // an exception
1580 __ empty_expression_stack();
1581 // find exception handler address and preserve exception oop
1582 __ call_VM(rdx,
1583 CAST_FROM_FN_PTR(address,
1584 InterpreterRuntime::exception_handler_for_exception),
1585 c_rarg1);
1586 // rax: exception handler entry point
1587 // rdx: preserved exception oop
1588 // r13: bcp for exception handler
1589 __ push_ptr(rdx); // push exception which is now the only value on the stack
1590 __ jmp(rax); // jump to exception handler (may be _remove_activation_entry!)
1591
1592 // If the exception is not handled in the current frame the frame is
1593 // removed and the exception is rethrown (i.e. exception
1594 // continuation is _rethrow_exception).
1595 //
1596 // Note: At this point the bci is still the bxi for the instruction
1597 // which caused the exception and the expression stack is
1598 // empty. Thus, for any VM calls at this point, GC will find a legal
1599 // oop map (with empty expression stack).
1600
1601 // In current activation
1602 // tos: exception
1603 // esi: exception bcp
1604
1605 //
1606 // JVMTI PopFrame support
1607 //
1608
1609 Interpreter::_remove_activation_preserving_args_entry = __ pc();
1610 __ empty_expression_stack();
1611 // Set the popframe_processing bit in pending_popframe_condition
1612 // indicating that we are currently handling popframe, so that
1613 // call_VMs that may happen later do not trigger new popframe
1614 // handling cycles.
1615 __ movl(rdx, Address(r15_thread, JavaThread::popframe_condition_offset()));
1616 __ orl(rdx, JavaThread::popframe_processing_bit);
1617 __ movl(Address(r15_thread, JavaThread::popframe_condition_offset()), rdx);
1618
1619 {
1620 // Check to see whether we are returning to a deoptimized frame.
1621 // (The PopFrame call ensures that the caller of the popped frame is
1622 // either interpreted or compiled and deoptimizes it if compiled.)
1623 // In this case, we can't call dispatch_next() after the frame is
1624 // popped, but instead must save the incoming arguments and restore
1625 // them after deoptimization has occurred.
1626 //
1627 // Note that we don't compare the return PC against the
1628 // deoptimization blob's unpack entry because of the presence of
1629 // adapter frames in C2.
1630 Label caller_not_deoptimized;
1631 __ movptr(c_rarg1, Address(rbp, frame::return_addr_offset * wordSize));
1632 __ super_call_VM_leaf(CAST_FROM_FN_PTR(address,
1633 InterpreterRuntime::interpreter_contains), c_rarg1);
1634 __ testl(rax, rax);
1635 __ jcc(Assembler::notZero, caller_not_deoptimized);
1636
1637 // Compute size of arguments for saving when returning to
1638 // deoptimized caller
1639 __ get_method(rax);
1640 __ load_unsigned_word(rax, Address(rax, in_bytes(methodOopDesc::
1641 size_of_parameters_offset())));
1642 __ shll(rax, Interpreter::logStackElementSize());
1643 __ restore_locals(); // XXX do we need this?
1644 __ subptr(r14, rax);
1645 __ addptr(r14, wordSize);
1646 // Save these arguments
1647 __ super_call_VM_leaf(CAST_FROM_FN_PTR(address,
1648 Deoptimization::
1649 popframe_preserve_args),
1650 r15_thread, rax, r14);
1651
1652 __ remove_activation(vtos, rdx,
1653 /* throw_monitor_exception */ false,
1654 /* install_monitor_exception */ false,
1655 /* notify_jvmdi */ false);
1656
1657 // Inform deoptimization that it is responsible for restoring
1658 // these arguments
1659 __ movl(Address(r15_thread, JavaThread::popframe_condition_offset()),
1660 JavaThread::popframe_force_deopt_reexecution_bit);
1661
1662 // Continue in deoptimization handler
1663 __ jmp(rdx);
1664
1665 __ bind(caller_not_deoptimized);
1666 }
1667
1668 __ remove_activation(vtos, rdx, /* rdx result (retaddr) is not used */
1669 /* throw_monitor_exception */ false,
1670 /* install_monitor_exception */ false,
1671 /* notify_jvmdi */ false);
1672
1673 // Finish with popframe handling
1674 // A previous I2C followed by a deoptimization might have moved the
1675 // outgoing arguments further up the stack. PopFrame expects the
1676 // mutations to those outgoing arguments to be preserved and other
1677 // constraints basically require this frame to look exactly as
1678 // though it had previously invoked an interpreted activation with
1679 // no space between the top of the expression stack (current
1680 // last_sp) and the top of stack. Rather than force deopt to
1681 // maintain this kind of invariant all the time we call a small
1682 // fixup routine to move the mutated arguments onto the top of our
1683 // expression stack if necessary.
1684 __ mov(c_rarg1, rsp);
1685 __ movptr(c_rarg2, Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize));
1686 // PC must point into interpreter here
1687 __ set_last_Java_frame(noreg, rbp, __ pc());
1688 __ super_call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::popframe_move_outgoing_args), r15_thread, c_rarg1, c_rarg2);
1689 __ reset_last_Java_frame(true, true);
1690 // Restore the last_sp and null it out
1691 __ movptr(rsp, Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize));
1692 __ movptr(Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize), (int32_t)NULL_WORD);
1693
1694 __ restore_bcp(); // XXX do we need this?
1695 __ restore_locals(); // XXX do we need this?
1696 // The method data pointer was incremented already during
1697 // call profiling. We have to restore the mdp for the current bcp.
1698 if (ProfileInterpreter) {
1699 __ set_method_data_pointer_for_bcp();
1700 }
1701
1702 // Clear the popframe condition flag
1703 __ movl(Address(r15_thread, JavaThread::popframe_condition_offset()),
1704 JavaThread::popframe_inactive);
1705
1706 __ dispatch_next(vtos);
1707 // end of PopFrame support
1708
1709 Interpreter::_remove_activation_entry = __ pc();
1710
1711 // preserve exception over this code sequence
1712 __ pop_ptr(rax);
1713 __ movptr(Address(r15_thread, JavaThread::vm_result_offset()), rax);
1714 // remove the activation (without doing throws on illegalMonitorExceptions)
1715 __ remove_activation(vtos, rdx, false, true, false);
1716 // restore exception
1717 __ movptr(rax, Address(r15_thread, JavaThread::vm_result_offset()));
1718 __ movptr(Address(r15_thread, JavaThread::vm_result_offset()), (int32_t)NULL_WORD);
1719 __ verify_oop(rax);
1720
1721 // In between activations - previous activation type unknown yet
1722 // compute continuation point - the continuation point expects the
1723 // following registers set up:
1724 //
1725 // rax: exception
1726 // rdx: return address/pc that threw exception
1727 // rsp: expression stack of caller
1728 // rbp: ebp of caller
1729 __ push(rax); // save exception
1730 __ push(rdx); // save return address
1731 __ super_call_VM_leaf(CAST_FROM_FN_PTR(address,
1732 SharedRuntime::exception_handler_for_return_address),
1733 rdx);
1734 __ mov(rbx, rax); // save exception handler
1735 __ pop(rdx); // restore return address
1736 __ pop(rax); // restore exception
1737 // Note that an "issuing PC" is actually the next PC after the call
1738 __ jmp(rbx); // jump to exception
1739 // handler of caller
1740 }
1741
1742
1743 //
1744 // JVMTI ForceEarlyReturn support
1745 //
1746 address TemplateInterpreterGenerator::generate_earlyret_entry_for(TosState state) {
1747 address entry = __ pc();
1748
1749 __ restore_bcp();
1750 __ restore_locals();
1751 __ empty_expression_stack();
1752 __ load_earlyret_value(state);
1753
1754 __ movptr(rdx, Address(r15_thread, JavaThread::jvmti_thread_state_offset()));
1755 Address cond_addr(rdx, JvmtiThreadState::earlyret_state_offset());
1756
1757 // Clear the earlyret state
1758 __ movl(cond_addr, JvmtiThreadState::earlyret_inactive);
1759
1760 __ remove_activation(state, rsi,
1761 false, /* throw_monitor_exception */
1762 false, /* install_monitor_exception */
1763 true); /* notify_jvmdi */
1764 __ jmp(rsi);
1765
1766 return entry;
1767 } // end of ForceEarlyReturn support
1768
1769
1770 //-----------------------------------------------------------------------------
1771 // Helper for vtos entry point generation
1772
1773 void TemplateInterpreterGenerator::set_vtos_entry_points(Template* t,
1774 address& bep,
1775 address& cep,
1776 address& sep,
1777 address& aep,
1778 address& iep,
1779 address& lep,
1780 address& fep,
1781 address& dep,
1782 address& vep) {
1783 assert(t->is_valid() && t->tos_in() == vtos, "illegal template");
1784 Label L;
1785 aep = __ pc(); __ push_ptr(); __ jmp(L);
1786 fep = __ pc(); __ push_f(); __ jmp(L);
1787 dep = __ pc(); __ push_d(); __ jmp(L);
1788 lep = __ pc(); __ push_l(); __ jmp(L);
1789 bep = cep = sep =
1790 iep = __ pc(); __ push_i();
1791 vep = __ pc();
1792 __ bind(L);
1793 generate_and_dispatch(t);
1794 }
1795
1796
1797 //-----------------------------------------------------------------------------
1798 // Generation of individual instructions
1799
1800 // helpers for generate_and_dispatch
1801
1802
1803 InterpreterGenerator::InterpreterGenerator(StubQueue* code)
1804 : TemplateInterpreterGenerator(code) {
1805 generate_all(); // down here so it can be "virtual"
1806 }
1807
1808 //-----------------------------------------------------------------------------
1809
1810 // Non-product code
1811 #ifndef PRODUCT
1812 address TemplateInterpreterGenerator::generate_trace_code(TosState state) {
1813 address entry = __ pc();
1814
1815 __ push(state);
1816 __ push(c_rarg0);
1817 __ push(c_rarg1);
1818 __ push(c_rarg2);
1819 __ push(c_rarg3);
1820 __ mov(c_rarg2, rax); // Pass itos
1821 #ifdef _WIN64
1822 __ movflt(xmm3, xmm0); // Pass ftos
1823 #endif
1824 __ call_VM(noreg,
1825 CAST_FROM_FN_PTR(address, SharedRuntime::trace_bytecode),
1826 c_rarg1, c_rarg2, c_rarg3);
1827 __ pop(c_rarg3);
1828 __ pop(c_rarg2);
1829 __ pop(c_rarg1);
1830 __ pop(c_rarg0);
1831 __ pop(state);
1832 __ ret(0); // return from result handler
1833
1834 return entry;
1835 }
1836
1837 void TemplateInterpreterGenerator::count_bytecode() {
1838 __ incrementl(ExternalAddress((address) &BytecodeCounter::_counter_value));
1839 }
1840
1841 void TemplateInterpreterGenerator::histogram_bytecode(Template* t) {
1842 __ incrementl(ExternalAddress((address) &BytecodeHistogram::_counters[t->bytecode()]));
1843 }
1844
1845 void TemplateInterpreterGenerator::histogram_bytecode_pair(Template* t) {
1846 __ mov32(rbx, ExternalAddress((address) &BytecodePairHistogram::_index));
1847 __ shrl(rbx, BytecodePairHistogram::log2_number_of_codes);
1848 __ orl(rbx,
1849 ((int) t->bytecode()) <<
1850 BytecodePairHistogram::log2_number_of_codes);
1851 __ mov32(ExternalAddress((address) &BytecodePairHistogram::_index), rbx);
1852 __ lea(rscratch1, ExternalAddress((address) BytecodePairHistogram::_counters));
1853 __ incrementl(Address(rscratch1, rbx, Address::times_4));
1854 }
1855
1856
1857 void TemplateInterpreterGenerator::trace_bytecode(Template* t) {
1858 // Call a little run-time stub to avoid blow-up for each bytecode.
1859 // The run-time runtime saves the right registers, depending on
1860 // the tosca in-state for the given template.
1861
1862 assert(Interpreter::trace_code(t->tos_in()) != NULL,
1863 "entry must have been generated");
1864 __ mov(r12, rsp); // remember sp
1865 __ andptr(rsp, -16); // align stack as required by ABI
1866 __ call(RuntimeAddress(Interpreter::trace_code(t->tos_in())));
1867 __ mov(rsp, r12); // restore sp
1868 __ reinit_heapbase();
1869 }
1870
1871
1872 void TemplateInterpreterGenerator::stop_interpreter_at() {
1873 Label L;
1874 __ cmp32(ExternalAddress((address) &BytecodeCounter::_counter_value),
1875 StopInterpreterAt);
1876 __ jcc(Assembler::notEqual, L);
1877 __ int3();
1878 __ bind(L);
1879 }
1880 #endif // !PRODUCT
1881 #endif // ! CC_INTERP