1 /*
2 * Copyright (c) 1997, 2025, 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 "asm/macroAssembler.hpp"
26 #include "compiler/disassembler.hpp"
27 #include "gc/shared/collectedHeap.hpp"
28 #include "gc/shared/gc_globals.hpp"
29 #include "gc/shared/tlab_globals.hpp"
30 #include "interpreter/interpreter.hpp"
31 #include "interpreter/interpreterRuntime.hpp"
32 #include "interpreter/interp_masm.hpp"
33 #include "interpreter/templateTable.hpp"
34 #include "memory/universe.hpp"
35 #include "oops/methodCounters.hpp"
36 #include "oops/methodData.hpp"
37 #include "oops/objArrayKlass.hpp"
38 #include "oops/oop.inline.hpp"
39 #include "oops/inlineKlass.hpp"
40 #include "oops/resolvedFieldEntry.hpp"
41 #include "oops/resolvedIndyEntry.hpp"
42 #include "oops/resolvedMethodEntry.hpp"
43 #include "prims/jvmtiExport.hpp"
44 #include "prims/methodHandles.hpp"
45 #include "runtime/frame.inline.hpp"
46 #include "runtime/safepointMechanism.hpp"
47 #include "runtime/sharedRuntime.hpp"
48 #include "runtime/stubRoutines.hpp"
49 #include "runtime/synchronizer.hpp"
50 #include "utilities/macros.hpp"
51
52 #define __ Disassembler::hook<InterpreterMacroAssembler>(__FILE__, __LINE__, _masm)->
53
54 // Global Register Names
55 static const Register rbcp = r13;
56 static const Register rlocals = r14;
57
58 // Address Computation: local variables
59 static inline Address iaddress(int n) {
60 return Address(rlocals, Interpreter::local_offset_in_bytes(n));
61 }
62
63 static inline Address laddress(int n) {
64 return iaddress(n + 1);
65 }
66
67 static inline Address faddress(int n) {
68 return iaddress(n);
69 }
70
71 static inline Address daddress(int n) {
72 return laddress(n);
73 }
74
75 static inline Address aaddress(int n) {
76 return iaddress(n);
77 }
78
79 static inline Address iaddress(Register r) {
80 return Address(rlocals, r, Address::times_ptr);
81 }
82
83 static inline Address laddress(Register r) {
84 return Address(rlocals, r, Address::times_ptr, Interpreter::local_offset_in_bytes(1));
85 }
86
87 static inline Address faddress(Register r) {
88 return iaddress(r);
89 }
90
91 static inline Address daddress(Register r) {
92 return laddress(r);
93 }
94
95 static inline Address aaddress(Register r) {
96 return iaddress(r);
97 }
98
99
100 // expression stack
101 // (Note: Must not use symmetric equivalents at_rsp_m1/2 since they store
102 // data beyond the rsp which is potentially unsafe in an MT environment;
103 // an interrupt may overwrite that data.)
104 static inline Address at_rsp () {
105 return Address(rsp, 0);
106 }
107
108 // At top of Java expression stack which may be different than esp(). It
109 // isn't for category 1 objects.
110 static inline Address at_tos () {
111 return Address(rsp, Interpreter::expr_offset_in_bytes(0));
112 }
113
114 static inline Address at_tos_p1() {
115 return Address(rsp, Interpreter::expr_offset_in_bytes(1));
116 }
117
118 static inline Address at_tos_p2() {
119 return Address(rsp, Interpreter::expr_offset_in_bytes(2));
120 }
121
122 // Condition conversion
123 static Assembler::Condition j_not(TemplateTable::Condition cc) {
124 switch (cc) {
125 case TemplateTable::equal : return Assembler::notEqual;
126 case TemplateTable::not_equal : return Assembler::equal;
127 case TemplateTable::less : return Assembler::greaterEqual;
128 case TemplateTable::less_equal : return Assembler::greater;
129 case TemplateTable::greater : return Assembler::lessEqual;
130 case TemplateTable::greater_equal: return Assembler::less;
131 }
132 ShouldNotReachHere();
133 return Assembler::zero;
134 }
135
136
137
138 // Miscellaneous helper routines
139 // Store an oop (or null) at the address described by obj.
140 // If val == noreg this means store a null
141
142
143 static void do_oop_store(InterpreterMacroAssembler* _masm,
144 Address dst,
145 Register val,
146 DecoratorSet decorators = 0) {
147 assert(val == noreg || val == rax, "parameter is just for looks");
148 __ store_heap_oop(dst, val, rscratch2, r9, r8, decorators);
149 }
150
151 static void do_oop_load(InterpreterMacroAssembler* _masm,
152 Address src,
153 Register dst,
154 DecoratorSet decorators = 0) {
155 __ load_heap_oop(dst, src, rdx, decorators);
156 }
157
158 Address TemplateTable::at_bcp(int offset) {
159 assert(_desc->uses_bcp(), "inconsistent uses_bcp information");
160 return Address(rbcp, offset);
161 }
162
163
164 void TemplateTable::patch_bytecode(Bytecodes::Code bc, Register bc_reg,
165 Register temp_reg, bool load_bc_into_bc_reg/*=true*/,
166 int byte_no) {
167 if (!RewriteBytecodes) return;
168 Label L_patch_done;
169
170 switch (bc) {
171 case Bytecodes::_fast_vputfield:
172 case Bytecodes::_fast_aputfield:
173 case Bytecodes::_fast_bputfield:
174 case Bytecodes::_fast_zputfield:
175 case Bytecodes::_fast_cputfield:
176 case Bytecodes::_fast_dputfield:
177 case Bytecodes::_fast_fputfield:
178 case Bytecodes::_fast_iputfield:
179 case Bytecodes::_fast_lputfield:
180 case Bytecodes::_fast_sputfield:
181 {
182 // We skip bytecode quickening for putfield instructions when
183 // the put_code written to the constant pool cache is zero.
184 // This is required so that every execution of this instruction
185 // calls out to InterpreterRuntime::resolve_get_put to do
186 // additional, required work.
187 assert(byte_no == f1_byte || byte_no == f2_byte, "byte_no out of range");
188 assert(load_bc_into_bc_reg, "we use bc_reg as temp");
189 __ load_field_entry(temp_reg, bc_reg);
190 if (byte_no == f1_byte) {
191 __ load_unsigned_byte(temp_reg, Address(temp_reg, in_bytes(ResolvedFieldEntry::get_code_offset())));
192 } else {
193 __ load_unsigned_byte(temp_reg, Address(temp_reg, in_bytes(ResolvedFieldEntry::put_code_offset())));
194 }
195
196 __ movl(bc_reg, bc);
197 __ cmpl(temp_reg, (int) 0);
198 __ jcc(Assembler::zero, L_patch_done); // don't patch
199 }
200 break;
201 default:
202 assert(byte_no == -1, "sanity");
203 // the pair bytecodes have already done the load.
204 if (load_bc_into_bc_reg) {
205 __ movl(bc_reg, bc);
206 }
207 }
208
209 if (JvmtiExport::can_post_breakpoint()) {
210 Label L_fast_patch;
211 // if a breakpoint is present we can't rewrite the stream directly
212 __ movzbl(temp_reg, at_bcp(0));
213 __ cmpl(temp_reg, Bytecodes::_breakpoint);
214 __ jcc(Assembler::notEqual, L_fast_patch);
215 __ get_method(temp_reg);
216 // Let breakpoint table handling rewrite to quicker bytecode
217 __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::set_original_bytecode_at), temp_reg, rbcp, bc_reg);
218 #ifndef ASSERT
219 __ jmpb(L_patch_done);
220 #else
221 __ jmp(L_patch_done);
222 #endif
223 __ bind(L_fast_patch);
224 }
225
226 #ifdef ASSERT
227 Label L_okay;
228 __ load_unsigned_byte(temp_reg, at_bcp(0));
229 __ cmpl(temp_reg, (int) Bytecodes::java_code(bc));
230 __ jcc(Assembler::equal, L_okay);
231 __ cmpl(temp_reg, bc_reg);
232 __ jcc(Assembler::equal, L_okay);
233 __ stop("patching the wrong bytecode");
234 __ bind(L_okay);
235 #endif
236
237 // patch bytecode
238 __ movb(at_bcp(0), bc_reg);
239 __ bind(L_patch_done);
240 }
241 // Individual instructions
242
243
244 void TemplateTable::nop() {
245 transition(vtos, vtos);
246 // nothing to do
247 }
248
249 void TemplateTable::shouldnotreachhere() {
250 transition(vtos, vtos);
251 __ stop("shouldnotreachhere bytecode");
252 }
253
254 void TemplateTable::aconst_null() {
255 transition(vtos, atos);
256 __ xorl(rax, rax);
257 }
258
259 void TemplateTable::iconst(int value) {
260 transition(vtos, itos);
261 if (value == 0) {
262 __ xorl(rax, rax);
263 } else {
264 __ movl(rax, value);
265 }
266 }
267
268 void TemplateTable::lconst(int value) {
269 transition(vtos, ltos);
270 if (value == 0) {
271 __ xorl(rax, rax);
272 } else {
273 __ movl(rax, value);
274 }
275 }
276
277
278
279 void TemplateTable::fconst(int value) {
280 transition(vtos, ftos);
281 static float one = 1.0f, two = 2.0f;
282 switch (value) {
283 case 0:
284 __ xorps(xmm0, xmm0);
285 break;
286 case 1:
287 __ movflt(xmm0, ExternalAddress((address) &one), rscratch1);
288 break;
289 case 2:
290 __ movflt(xmm0, ExternalAddress((address) &two), rscratch1);
291 break;
292 default:
293 ShouldNotReachHere();
294 break;
295 }
296 }
297
298 void TemplateTable::dconst(int value) {
299 transition(vtos, dtos);
300 static double one = 1.0;
301 switch (value) {
302 case 0:
303 __ xorpd(xmm0, xmm0);
304 break;
305 case 1:
306 __ movdbl(xmm0, ExternalAddress((address) &one), rscratch1);
307 break;
308 default:
309 ShouldNotReachHere();
310 break;
311 }
312 }
313
314 void TemplateTable::bipush() {
315 transition(vtos, itos);
316 __ load_signed_byte(rax, at_bcp(1));
317 }
318
319 void TemplateTable::sipush() {
320 transition(vtos, itos);
321 __ load_unsigned_short(rax, at_bcp(1));
322 __ bswapl(rax);
323 __ sarl(rax, 16);
324 }
325
326 void TemplateTable::ldc(LdcType type) {
327 transition(vtos, vtos);
328 Register rarg = c_rarg1;
329 Label call_ldc, notFloat, notClass, notInt, Done;
330
331 if (is_ldc_wide(type)) {
332 __ get_unsigned_2_byte_index_at_bcp(rbx, 1);
333 } else {
334 __ load_unsigned_byte(rbx, at_bcp(1));
335 }
336
337 __ get_cpool_and_tags(rcx, rax);
338 const int base_offset = ConstantPool::header_size() * wordSize;
339 const int tags_offset = Array<u1>::base_offset_in_bytes();
340
341 // get type
342 __ movzbl(rdx, Address(rax, rbx, Address::times_1, tags_offset));
343
344 // unresolved class - get the resolved class
345 __ cmpl(rdx, JVM_CONSTANT_UnresolvedClass);
346 __ jccb(Assembler::equal, call_ldc);
347
348 // unresolved class in error state - call into runtime to throw the error
349 // from the first resolution attempt
350 __ cmpl(rdx, JVM_CONSTANT_UnresolvedClassInError);
351 __ jccb(Assembler::equal, call_ldc);
352
353 // resolved class - need to call vm to get java mirror of the class
354 __ cmpl(rdx, JVM_CONSTANT_Class);
355 __ jcc(Assembler::notEqual, notClass);
356
357 __ bind(call_ldc);
358
359 __ movl(rarg, is_ldc_wide(type) ? 1 : 0);
360 call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::ldc), rarg);
361
362 __ push(atos);
363 __ jmp(Done);
364
365 __ bind(notClass);
366 __ cmpl(rdx, JVM_CONSTANT_Float);
367 __ jccb(Assembler::notEqual, notFloat);
368
369 // ftos
370 __ movflt(xmm0, Address(rcx, rbx, Address::times_ptr, base_offset));
371 __ push(ftos);
372 __ jmp(Done);
373
374 __ bind(notFloat);
375 __ cmpl(rdx, JVM_CONSTANT_Integer);
376 __ jccb(Assembler::notEqual, notInt);
377
378 // itos
379 __ movl(rax, Address(rcx, rbx, Address::times_ptr, base_offset));
380 __ push(itos);
381 __ jmp(Done);
382
383 // assume the tag is for condy; if not, the VM runtime will tell us
384 __ bind(notInt);
385 condy_helper(Done);
386
387 __ bind(Done);
388 }
389
390 // Fast path for caching oop constants.
391 void TemplateTable::fast_aldc(LdcType type) {
392 transition(vtos, atos);
393
394 Register result = rax;
395 Register tmp = rdx;
396 Register rarg = c_rarg1;
397 int index_size = is_ldc_wide(type) ? sizeof(u2) : sizeof(u1);
398
399 Label resolved;
400
401 // We are resolved if the resolved reference cache entry contains a
402 // non-null object (String, MethodType, etc.)
403 assert_different_registers(result, tmp);
404 __ get_cache_index_at_bcp(tmp, 1, index_size);
405 __ load_resolved_reference_at_index(result, tmp);
406 __ testptr(result, result);
407 __ jcc(Assembler::notZero, resolved);
408
409 address entry = CAST_FROM_FN_PTR(address, InterpreterRuntime::resolve_ldc);
410
411 // first time invocation - must resolve first
412 __ movl(rarg, (int)bytecode());
413 __ call_VM(result, entry, rarg);
414 __ bind(resolved);
415
416 { // Check for the null sentinel.
417 // If we just called the VM, it already did the mapping for us,
418 // but it's harmless to retry.
419 Label notNull;
420 ExternalAddress null_sentinel((address)Universe::the_null_sentinel_addr());
421 __ movptr(tmp, null_sentinel);
422 __ resolve_oop_handle(tmp, rscratch2);
423 __ cmpoop(tmp, result);
424 __ jccb(Assembler::notEqual, notNull);
425 __ xorptr(result, result); // null object reference
426 __ bind(notNull);
427 }
428
429 if (VerifyOops) {
430 __ verify_oop(result);
431 }
432 }
433
434 void TemplateTable::ldc2_w() {
435 transition(vtos, vtos);
436 Label notDouble, notLong, Done;
437 __ get_unsigned_2_byte_index_at_bcp(rbx, 1);
438
439 __ get_cpool_and_tags(rcx, rax);
440 const int base_offset = ConstantPool::header_size() * wordSize;
441 const int tags_offset = Array<u1>::base_offset_in_bytes();
442
443 // get type
444 __ movzbl(rdx, Address(rax, rbx, Address::times_1, tags_offset));
445 __ cmpl(rdx, JVM_CONSTANT_Double);
446 __ jccb(Assembler::notEqual, notDouble);
447
448 // dtos
449 __ movdbl(xmm0, Address(rcx, rbx, Address::times_ptr, base_offset));
450 __ push(dtos);
451
452 __ jmp(Done);
453 __ bind(notDouble);
454 __ cmpl(rdx, JVM_CONSTANT_Long);
455 __ jccb(Assembler::notEqual, notLong);
456
457 // ltos
458 __ movptr(rax, Address(rcx, rbx, Address::times_ptr, base_offset + 0 * wordSize));
459 __ push(ltos);
460 __ jmp(Done);
461
462 __ bind(notLong);
463 condy_helper(Done);
464
465 __ bind(Done);
466 }
467
468 void TemplateTable::condy_helper(Label& Done) {
469 const Register obj = rax;
470 const Register off = rbx;
471 const Register flags = rcx;
472 const Register rarg = c_rarg1;
473 __ movl(rarg, (int)bytecode());
474 call_VM(obj, CAST_FROM_FN_PTR(address, InterpreterRuntime::resolve_ldc), rarg);
475 __ get_vm_result_metadata(flags);
476 // VMr = obj = base address to find primitive value to push
477 // VMr2 = flags = (tos, off) using format of CPCE::_flags
478 __ movl(off, flags);
479 __ andl(off, ConstantPoolCache::field_index_mask);
480 const Address field(obj, off, Address::times_1, 0*wordSize);
481
482 // What sort of thing are we loading?
483 __ shrl(flags, ConstantPoolCache::tos_state_shift);
484 __ andl(flags, ConstantPoolCache::tos_state_mask);
485
486 switch (bytecode()) {
487 case Bytecodes::_ldc:
488 case Bytecodes::_ldc_w:
489 {
490 // tos in (itos, ftos, stos, btos, ctos, ztos)
491 Label notInt, notFloat, notShort, notByte, notChar, notBool;
492 __ cmpl(flags, itos);
493 __ jccb(Assembler::notEqual, notInt);
494 // itos
495 __ movl(rax, field);
496 __ push(itos);
497 __ jmp(Done);
498
499 __ bind(notInt);
500 __ cmpl(flags, ftos);
501 __ jccb(Assembler::notEqual, notFloat);
502 // ftos
503 __ movflt(xmm0, field);
504 __ push(ftos);
505 __ jmp(Done);
506
507 __ bind(notFloat);
508 __ cmpl(flags, stos);
509 __ jccb(Assembler::notEqual, notShort);
510 // stos
511 __ load_signed_short(rax, field);
512 __ push(stos);
513 __ jmp(Done);
514
515 __ bind(notShort);
516 __ cmpl(flags, btos);
517 __ jccb(Assembler::notEqual, notByte);
518 // btos
519 __ load_signed_byte(rax, field);
520 __ push(btos);
521 __ jmp(Done);
522
523 __ bind(notByte);
524 __ cmpl(flags, ctos);
525 __ jccb(Assembler::notEqual, notChar);
526 // ctos
527 __ load_unsigned_short(rax, field);
528 __ push(ctos);
529 __ jmp(Done);
530
531 __ bind(notChar);
532 __ cmpl(flags, ztos);
533 __ jccb(Assembler::notEqual, notBool);
534 // ztos
535 __ load_signed_byte(rax, field);
536 __ push(ztos);
537 __ jmp(Done);
538
539 __ bind(notBool);
540 break;
541 }
542
543 case Bytecodes::_ldc2_w:
544 {
545 Label notLong, notDouble;
546 __ cmpl(flags, ltos);
547 __ jccb(Assembler::notEqual, notLong);
548 // ltos
549 // Loading high word first because movptr clobbers rax
550 __ movptr(rax, field);
551 __ push(ltos);
552 __ jmp(Done);
553
554 __ bind(notLong);
555 __ cmpl(flags, dtos);
556 __ jccb(Assembler::notEqual, notDouble);
557 // dtos
558 __ movdbl(xmm0, field);
559 __ push(dtos);
560 __ jmp(Done);
561
562 __ bind(notDouble);
563 break;
564 }
565
566 default:
567 ShouldNotReachHere();
568 }
569
570 __ stop("bad ldc/condy");
571 }
572
573 void TemplateTable::locals_index(Register reg, int offset) {
574 __ load_unsigned_byte(reg, at_bcp(offset));
575 __ negptr(reg);
576 }
577
578 void TemplateTable::iload() {
579 iload_internal();
580 }
581
582 void TemplateTable::nofast_iload() {
583 iload_internal(may_not_rewrite);
584 }
585
586 void TemplateTable::iload_internal(RewriteControl rc) {
587 transition(vtos, itos);
588 if (RewriteFrequentPairs && rc == may_rewrite) {
589 Label rewrite, done;
590 const Register bc = c_rarg3;
591 assert(rbx != bc, "register damaged");
592
593 // get next byte
594 __ load_unsigned_byte(rbx,
595 at_bcp(Bytecodes::length_for(Bytecodes::_iload)));
596 // if _iload, wait to rewrite to iload2. We only want to rewrite the
597 // last two iloads in a pair. Comparing against fast_iload means that
598 // the next bytecode is neither an iload or a caload, and therefore
599 // an iload pair.
600 __ cmpl(rbx, Bytecodes::_iload);
601 __ jcc(Assembler::equal, done);
602
603 __ cmpl(rbx, Bytecodes::_fast_iload);
604 __ movl(bc, Bytecodes::_fast_iload2);
605
606 __ jccb(Assembler::equal, rewrite);
607
608 // if _caload, rewrite to fast_icaload
609 __ cmpl(rbx, Bytecodes::_caload);
610 __ movl(bc, Bytecodes::_fast_icaload);
611 __ jccb(Assembler::equal, rewrite);
612
613 // rewrite so iload doesn't check again.
614 __ movl(bc, Bytecodes::_fast_iload);
615
616 // rewrite
617 // bc: fast bytecode
618 __ bind(rewrite);
619 patch_bytecode(Bytecodes::_iload, bc, rbx, false);
620 __ bind(done);
621 }
622
623 // Get the local value into tos
624 locals_index(rbx);
625 __ movl(rax, iaddress(rbx));
626 }
627
628 void TemplateTable::fast_iload2() {
629 transition(vtos, itos);
630 locals_index(rbx);
631 __ movl(rax, iaddress(rbx));
632 __ push(itos);
633 locals_index(rbx, 3);
634 __ movl(rax, iaddress(rbx));
635 }
636
637 void TemplateTable::fast_iload() {
638 transition(vtos, itos);
639 locals_index(rbx);
640 __ movl(rax, iaddress(rbx));
641 }
642
643 void TemplateTable::lload() {
644 transition(vtos, ltos);
645 locals_index(rbx);
646 __ movptr(rax, laddress(rbx));
647 }
648
649 void TemplateTable::fload() {
650 transition(vtos, ftos);
651 locals_index(rbx);
652 __ movflt(xmm0, faddress(rbx));
653 }
654
655 void TemplateTable::dload() {
656 transition(vtos, dtos);
657 locals_index(rbx);
658 __ movdbl(xmm0, daddress(rbx));
659 }
660
661 void TemplateTable::aload() {
662 transition(vtos, atos);
663 locals_index(rbx);
664 __ movptr(rax, aaddress(rbx));
665 }
666
667 void TemplateTable::locals_index_wide(Register reg) {
668 __ load_unsigned_short(reg, at_bcp(2));
669 __ bswapl(reg);
670 __ shrl(reg, 16);
671 __ negptr(reg);
672 }
673
674 void TemplateTable::wide_iload() {
675 transition(vtos, itos);
676 locals_index_wide(rbx);
677 __ movl(rax, iaddress(rbx));
678 }
679
680 void TemplateTable::wide_lload() {
681 transition(vtos, ltos);
682 locals_index_wide(rbx);
683 __ movptr(rax, laddress(rbx));
684 }
685
686 void TemplateTable::wide_fload() {
687 transition(vtos, ftos);
688 locals_index_wide(rbx);
689 __ movflt(xmm0, faddress(rbx));
690 }
691
692 void TemplateTable::wide_dload() {
693 transition(vtos, dtos);
694 locals_index_wide(rbx);
695 __ movdbl(xmm0, daddress(rbx));
696 }
697
698 void TemplateTable::wide_aload() {
699 transition(vtos, atos);
700 locals_index_wide(rbx);
701 __ movptr(rax, aaddress(rbx));
702 }
703
704 void TemplateTable::index_check(Register array, Register index) {
705 // Pop ptr into array
706 __ pop_ptr(array);
707 index_check_without_pop(array, index);
708 }
709
710 void TemplateTable::index_check_without_pop(Register array, Register index) {
711 // destroys rbx
712 // sign extend index for use by indexed load
713 __ movl2ptr(index, index);
714 // check index
715 __ cmpl(index, Address(array, arrayOopDesc::length_offset_in_bytes()));
716 if (index != rbx) {
717 // ??? convention: move aberrant index into rbx for exception message
718 assert(rbx != array, "different registers");
719 __ movl(rbx, index);
720 }
721 Label skip;
722 __ jccb(Assembler::below, skip);
723 // Pass array to create more detailed exceptions.
724 __ mov(c_rarg1, array);
725 __ jump(RuntimeAddress(Interpreter::_throw_ArrayIndexOutOfBoundsException_entry));
726 __ bind(skip);
727 }
728
729 void TemplateTable::iaload() {
730 transition(itos, itos);
731 // rax: index
732 // rdx: array
733 index_check(rdx, rax); // kills rbx
734 __ access_load_at(T_INT, IN_HEAP | IS_ARRAY, rax,
735 Address(rdx, rax, Address::times_4,
736 arrayOopDesc::base_offset_in_bytes(T_INT)),
737 noreg);
738 }
739
740 void TemplateTable::laload() {
741 transition(itos, ltos);
742 // rax: index
743 // rdx: array
744 index_check(rdx, rax); // kills rbx
745 // rbx,: index
746 __ access_load_at(T_LONG, IN_HEAP | IS_ARRAY, noreg /* ltos */,
747 Address(rdx, rbx, Address::times_8,
748 arrayOopDesc::base_offset_in_bytes(T_LONG)),
749 noreg);
750 }
751
752
753
754 void TemplateTable::faload() {
755 transition(itos, ftos);
756 // rax: index
757 // rdx: array
758 index_check(rdx, rax); // kills rbx
759 __ access_load_at(T_FLOAT, IN_HEAP | IS_ARRAY, noreg /* ftos */,
760 Address(rdx, rax,
761 Address::times_4,
762 arrayOopDesc::base_offset_in_bytes(T_FLOAT)),
763 noreg);
764 }
765
766 void TemplateTable::daload() {
767 transition(itos, dtos);
768 // rax: index
769 // rdx: array
770 index_check(rdx, rax); // kills rbx
771 __ access_load_at(T_DOUBLE, IN_HEAP | IS_ARRAY, noreg /* dtos */,
772 Address(rdx, rax,
773 Address::times_8,
774 arrayOopDesc::base_offset_in_bytes(T_DOUBLE)),
775 noreg);
776 }
777
778 void TemplateTable::aaload() {
779 transition(itos, atos);
780 Register array = rdx;
781 Register index = rax;
782
783 index_check(array, index); // kills rbx
784 __ profile_array_type<ArrayLoadData>(rbx, array, rcx);
785 if (UseArrayFlattening) {
786 Label is_flat_array, done;
787 __ test_flat_array_oop(array, rbx, is_flat_array);
788 do_oop_load(_masm,
789 Address(array, index,
790 UseCompressedOops ? Address::times_4 : Address::times_ptr,
791 arrayOopDesc::base_offset_in_bytes(T_OBJECT)),
792 rax,
793 IS_ARRAY);
794 __ jmp(done);
795 __ bind(is_flat_array);
796 __ movptr(rcx, array);
797 call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::flat_array_load), rcx, index);
798 __ bind(done);
799 } else {
800 do_oop_load(_masm,
801 Address(array, index,
802 UseCompressedOops ? Address::times_4 : Address::times_ptr,
803 arrayOopDesc::base_offset_in_bytes(T_OBJECT)),
804 rax,
805 IS_ARRAY);
806 }
807 __ profile_element_type(rbx, rax, rcx);
808 }
809
810 void TemplateTable::baload() {
811 transition(itos, itos);
812 // rax: index
813 // rdx: array
814 index_check(rdx, rax); // kills rbx
815 __ access_load_at(T_BYTE, IN_HEAP | IS_ARRAY, rax,
816 Address(rdx, rax, Address::times_1, arrayOopDesc::base_offset_in_bytes(T_BYTE)),
817 noreg);
818 }
819
820 void TemplateTable::caload() {
821 transition(itos, itos);
822 // rax: index
823 // rdx: array
824 index_check(rdx, rax); // kills rbx
825 __ access_load_at(T_CHAR, IN_HEAP | IS_ARRAY, rax,
826 Address(rdx, rax, Address::times_2, arrayOopDesc::base_offset_in_bytes(T_CHAR)),
827 noreg);
828 }
829
830 // iload followed by caload frequent pair
831 void TemplateTable::fast_icaload() {
832 transition(vtos, itos);
833 // load index out of locals
834 locals_index(rbx);
835 __ movl(rax, iaddress(rbx));
836
837 // rax: index
838 // rdx: array
839 index_check(rdx, rax); // kills rbx
840 __ access_load_at(T_CHAR, IN_HEAP | IS_ARRAY, rax,
841 Address(rdx, rax, Address::times_2, arrayOopDesc::base_offset_in_bytes(T_CHAR)),
842 noreg);
843 }
844
845
846 void TemplateTable::saload() {
847 transition(itos, itos);
848 // rax: index
849 // rdx: array
850 index_check(rdx, rax); // kills rbx
851 __ access_load_at(T_SHORT, IN_HEAP | IS_ARRAY, rax,
852 Address(rdx, rax, Address::times_2, arrayOopDesc::base_offset_in_bytes(T_SHORT)),
853 noreg);
854 }
855
856 void TemplateTable::iload(int n) {
857 transition(vtos, itos);
858 __ movl(rax, iaddress(n));
859 }
860
861 void TemplateTable::lload(int n) {
862 transition(vtos, ltos);
863 __ movptr(rax, laddress(n));
864 }
865
866 void TemplateTable::fload(int n) {
867 transition(vtos, ftos);
868 __ movflt(xmm0, faddress(n));
869 }
870
871 void TemplateTable::dload(int n) {
872 transition(vtos, dtos);
873 __ movdbl(xmm0, daddress(n));
874 }
875
876 void TemplateTable::aload(int n) {
877 transition(vtos, atos);
878 __ movptr(rax, aaddress(n));
879 }
880
881 void TemplateTable::aload_0() {
882 aload_0_internal();
883 }
884
885 void TemplateTable::nofast_aload_0() {
886 aload_0_internal(may_not_rewrite);
887 }
888
889 void TemplateTable::aload_0_internal(RewriteControl rc) {
890 transition(vtos, atos);
891 // According to bytecode histograms, the pairs:
892 //
893 // _aload_0, _fast_igetfield
894 // _aload_0, _fast_agetfield
895 // _aload_0, _fast_fgetfield
896 //
897 // occur frequently. If RewriteFrequentPairs is set, the (slow)
898 // _aload_0 bytecode checks if the next bytecode is either
899 // _fast_igetfield, _fast_agetfield or _fast_fgetfield and then
900 // rewrites the current bytecode into a pair bytecode; otherwise it
901 // rewrites the current bytecode into _fast_aload_0 that doesn't do
902 // the pair check anymore.
903 //
904 // Note: If the next bytecode is _getfield, the rewrite must be
905 // delayed, otherwise we may miss an opportunity for a pair.
906 //
907 // Also rewrite frequent pairs
908 // aload_0, aload_1
909 // aload_0, iload_1
910 // These bytecodes with a small amount of code are most profitable
911 // to rewrite
912 if (RewriteFrequentPairs && rc == may_rewrite) {
913 Label rewrite, done;
914
915 const Register bc = c_rarg3;
916 assert(rbx != bc, "register damaged");
917
918 // get next byte
919 __ load_unsigned_byte(rbx, at_bcp(Bytecodes::length_for(Bytecodes::_aload_0)));
920
921 // if _getfield then wait with rewrite
922 __ cmpl(rbx, Bytecodes::_getfield);
923 __ jcc(Assembler::equal, done);
924
925 // if _igetfield then rewrite to _fast_iaccess_0
926 assert(Bytecodes::java_code(Bytecodes::_fast_iaccess_0) == Bytecodes::_aload_0, "fix bytecode definition");
927 __ cmpl(rbx, Bytecodes::_fast_igetfield);
928 __ movl(bc, Bytecodes::_fast_iaccess_0);
929 __ jccb(Assembler::equal, rewrite);
930
931 // if _agetfield then rewrite to _fast_aaccess_0
932 assert(Bytecodes::java_code(Bytecodes::_fast_aaccess_0) == Bytecodes::_aload_0, "fix bytecode definition");
933 __ cmpl(rbx, Bytecodes::_fast_agetfield);
934 __ movl(bc, Bytecodes::_fast_aaccess_0);
935 __ jccb(Assembler::equal, rewrite);
936
937 // if _fgetfield then rewrite to _fast_faccess_0
938 assert(Bytecodes::java_code(Bytecodes::_fast_faccess_0) == Bytecodes::_aload_0, "fix bytecode definition");
939 __ cmpl(rbx, Bytecodes::_fast_fgetfield);
940 __ movl(bc, Bytecodes::_fast_faccess_0);
941 __ jccb(Assembler::equal, rewrite);
942
943 // else rewrite to _fast_aload0
944 assert(Bytecodes::java_code(Bytecodes::_fast_aload_0) == Bytecodes::_aload_0, "fix bytecode definition");
945 __ movl(bc, Bytecodes::_fast_aload_0);
946
947 // rewrite
948 // bc: fast bytecode
949 __ bind(rewrite);
950 patch_bytecode(Bytecodes::_aload_0, bc, rbx, false);
951
952 __ bind(done);
953 }
954
955 // Do actual aload_0 (must do this after patch_bytecode which might call VM and GC might change oop).
956 aload(0);
957 }
958
959 void TemplateTable::istore() {
960 transition(itos, vtos);
961 locals_index(rbx);
962 __ movl(iaddress(rbx), rax);
963 }
964
965
966 void TemplateTable::lstore() {
967 transition(ltos, vtos);
968 locals_index(rbx);
969 __ movptr(laddress(rbx), rax);
970 }
971
972 void TemplateTable::fstore() {
973 transition(ftos, vtos);
974 locals_index(rbx);
975 __ movflt(faddress(rbx), xmm0);
976 }
977
978 void TemplateTable::dstore() {
979 transition(dtos, vtos);
980 locals_index(rbx);
981 __ movdbl(daddress(rbx), xmm0);
982 }
983
984 void TemplateTable::astore() {
985 transition(vtos, vtos);
986 __ pop_ptr(rax);
987 locals_index(rbx);
988 __ movptr(aaddress(rbx), rax);
989 }
990
991 void TemplateTable::wide_istore() {
992 transition(vtos, vtos);
993 __ pop_i();
994 locals_index_wide(rbx);
995 __ movl(iaddress(rbx), rax);
996 }
997
998 void TemplateTable::wide_lstore() {
999 transition(vtos, vtos);
1000 __ pop_l();
1001 locals_index_wide(rbx);
1002 __ movptr(laddress(rbx), rax);
1003 }
1004
1005 void TemplateTable::wide_fstore() {
1006 transition(vtos, vtos);
1007 __ pop_f(xmm0);
1008 locals_index_wide(rbx);
1009 __ movflt(faddress(rbx), xmm0);
1010 }
1011
1012 void TemplateTable::wide_dstore() {
1013 transition(vtos, vtos);
1014 __ pop_d(xmm0);
1015 locals_index_wide(rbx);
1016 __ movdbl(daddress(rbx), xmm0);
1017 }
1018
1019 void TemplateTable::wide_astore() {
1020 transition(vtos, vtos);
1021 __ pop_ptr(rax);
1022 locals_index_wide(rbx);
1023 __ movptr(aaddress(rbx), rax);
1024 }
1025
1026 void TemplateTable::iastore() {
1027 transition(itos, vtos);
1028 __ pop_i(rbx);
1029 // rax: value
1030 // rbx: index
1031 // rdx: array
1032 index_check(rdx, rbx); // prefer index in rbx
1033 __ access_store_at(T_INT, IN_HEAP | IS_ARRAY,
1034 Address(rdx, rbx, Address::times_4,
1035 arrayOopDesc::base_offset_in_bytes(T_INT)),
1036 rax, noreg, noreg, noreg);
1037 }
1038
1039 void TemplateTable::lastore() {
1040 transition(ltos, vtos);
1041 __ pop_i(rbx);
1042 // rax,: low(value)
1043 // rcx: array
1044 // rdx: high(value)
1045 index_check(rcx, rbx); // prefer index in rbx,
1046 // rbx,: index
1047 __ access_store_at(T_LONG, IN_HEAP | IS_ARRAY,
1048 Address(rcx, rbx, Address::times_8,
1049 arrayOopDesc::base_offset_in_bytes(T_LONG)),
1050 noreg /* ltos */, noreg, noreg, noreg);
1051 }
1052
1053
1054 void TemplateTable::fastore() {
1055 transition(ftos, vtos);
1056 __ pop_i(rbx);
1057 // value is in xmm0
1058 // rbx: index
1059 // rdx: array
1060 index_check(rdx, rbx); // prefer index in rbx
1061 __ access_store_at(T_FLOAT, IN_HEAP | IS_ARRAY,
1062 Address(rdx, rbx, Address::times_4,
1063 arrayOopDesc::base_offset_in_bytes(T_FLOAT)),
1064 noreg /* ftos */, noreg, noreg, noreg);
1065 }
1066
1067 void TemplateTable::dastore() {
1068 transition(dtos, vtos);
1069 __ pop_i(rbx);
1070 // value is in xmm0
1071 // rbx: index
1072 // rdx: array
1073 index_check(rdx, rbx); // prefer index in rbx
1074 __ access_store_at(T_DOUBLE, IN_HEAP | IS_ARRAY,
1075 Address(rdx, rbx, Address::times_8,
1076 arrayOopDesc::base_offset_in_bytes(T_DOUBLE)),
1077 noreg /* dtos */, noreg, noreg, noreg);
1078 }
1079
1080 void TemplateTable::aastore() {
1081 Label is_null, is_flat_array, ok_is_subtype, done;
1082 transition(vtos, vtos);
1083 // stack: ..., array, index, value
1084 __ movptr(rax, at_tos()); // value
1085 __ movl(rcx, at_tos_p1()); // index
1086 __ movptr(rdx, at_tos_p2()); // array
1087
1088 Address element_address(rdx, rcx,
1089 UseCompressedOops? Address::times_4 : Address::times_ptr,
1090 arrayOopDesc::base_offset_in_bytes(T_OBJECT));
1091
1092 index_check_without_pop(rdx, rcx); // kills rbx
1093
1094 __ profile_array_type<ArrayStoreData>(rdi, rdx, rbx);
1095 __ profile_multiple_element_types(rdi, rax, rbx, rcx);
1096
1097 __ testptr(rax, rax);
1098 __ jcc(Assembler::zero, is_null);
1099
1100 // Move array class to rdi
1101 __ load_klass(rdi, rdx, rscratch1);
1102 if (UseArrayFlattening) {
1103 __ movl(rbx, Address(rdi, Klass::layout_helper_offset()));
1104 __ test_flat_array_layout(rbx, is_flat_array);
1105 }
1106
1107 // Move subklass into rbx
1108 __ load_klass(rbx, rax, rscratch1);
1109 // Move array element superklass into rax
1110 __ movptr(rax, Address(rdi,
1111 ObjArrayKlass::element_klass_offset()));
1112
1113 // Generate subtype check. Blows rcx, rdi
1114 // Superklass in rax. Subklass in rbx.
1115 // is "rbx <: rax" ? (value subclass <: array element superclass)
1116 __ gen_subtype_check(rbx, ok_is_subtype, false);
1117
1118 // Come here on failure
1119 // object is at TOS
1120 __ jump(RuntimeAddress(Interpreter::_throw_ArrayStoreException_entry));
1121
1122 // Come here on success
1123 __ bind(ok_is_subtype);
1124
1125 // Get the value we will store
1126 __ movptr(rax, at_tos());
1127 __ movl(rcx, at_tos_p1()); // index
1128 // Now store using the appropriate barrier
1129 do_oop_store(_masm, element_address, rax, IS_ARRAY);
1130 __ jmp(done);
1131
1132 // Have a null in rax, rdx=array, ecx=index. Store null at ary[idx]
1133 __ bind(is_null);
1134 if (EnableValhalla) {
1135 Label write_null_to_null_free_array, store_null;
1136
1137 // Move array class to rdi
1138 __ load_klass(rdi, rdx, rscratch1);
1139 if (UseArrayFlattening) {
1140 __ movl(rbx, Address(rdi, Klass::layout_helper_offset()));
1141 __ test_flat_array_layout(rbx, is_flat_array);
1142 }
1143
1144 // No way to store null in null-free array
1145 __ test_null_free_array_oop(rdx, rbx, write_null_to_null_free_array);
1146 __ jmp(store_null);
1147
1148 __ bind(write_null_to_null_free_array);
1149 __ jump(RuntimeAddress(Interpreter::_throw_NullPointerException_entry));
1150
1151 __ bind(store_null);
1152 }
1153 // Store a null
1154 do_oop_store(_masm, element_address, noreg, IS_ARRAY);
1155 __ jmp(done);
1156
1157 if (UseArrayFlattening) {
1158 Label is_type_ok;
1159 __ bind(is_flat_array); // Store non-null value to flat
1160
1161 __ movptr(rax, at_tos());
1162 __ movl(rcx, at_tos_p1()); // index
1163 __ movptr(rdx, at_tos_p2()); // array
1164
1165 call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::flat_array_store), rax, rdx, rcx);
1166 }
1167 // Pop stack arguments
1168 __ bind(done);
1169 __ addptr(rsp, 3 * Interpreter::stackElementSize);
1170 }
1171
1172 void TemplateTable::bastore() {
1173 transition(itos, vtos);
1174 __ pop_i(rbx);
1175 // rax: value
1176 // rbx: index
1177 // rdx: array
1178 index_check(rdx, rbx); // prefer index in rbx
1179 // Need to check whether array is boolean or byte
1180 // since both types share the bastore bytecode.
1181 __ load_klass(rcx, rdx, rscratch1);
1182 __ movl(rcx, Address(rcx, Klass::layout_helper_offset()));
1183 int diffbit = Klass::layout_helper_boolean_diffbit();
1184 __ testl(rcx, diffbit);
1185 Label L_skip;
1186 __ jccb(Assembler::zero, L_skip);
1187 __ andl(rax, 1); // if it is a T_BOOLEAN array, mask the stored value to 0/1
1188 __ bind(L_skip);
1189 __ access_store_at(T_BYTE, IN_HEAP | IS_ARRAY,
1190 Address(rdx, rbx,Address::times_1,
1191 arrayOopDesc::base_offset_in_bytes(T_BYTE)),
1192 rax, noreg, noreg, noreg);
1193 }
1194
1195 void TemplateTable::castore() {
1196 transition(itos, vtos);
1197 __ pop_i(rbx);
1198 // rax: value
1199 // rbx: index
1200 // rdx: array
1201 index_check(rdx, rbx); // prefer index in rbx
1202 __ access_store_at(T_CHAR, IN_HEAP | IS_ARRAY,
1203 Address(rdx, rbx, Address::times_2,
1204 arrayOopDesc::base_offset_in_bytes(T_CHAR)),
1205 rax, noreg, noreg, noreg);
1206 }
1207
1208
1209 void TemplateTable::sastore() {
1210 castore();
1211 }
1212
1213 void TemplateTable::istore(int n) {
1214 transition(itos, vtos);
1215 __ movl(iaddress(n), rax);
1216 }
1217
1218 void TemplateTable::lstore(int n) {
1219 transition(ltos, vtos);
1220 __ movptr(laddress(n), rax);
1221 }
1222
1223 void TemplateTable::fstore(int n) {
1224 transition(ftos, vtos);
1225 __ movflt(faddress(n), xmm0);
1226 }
1227
1228 void TemplateTable::dstore(int n) {
1229 transition(dtos, vtos);
1230 __ movdbl(daddress(n), xmm0);
1231 }
1232
1233
1234 void TemplateTable::astore(int n) {
1235 transition(vtos, vtos);
1236 __ pop_ptr(rax);
1237 __ movptr(aaddress(n), rax);
1238 }
1239
1240 void TemplateTable::pop() {
1241 transition(vtos, vtos);
1242 __ addptr(rsp, Interpreter::stackElementSize);
1243 }
1244
1245 void TemplateTable::pop2() {
1246 transition(vtos, vtos);
1247 __ addptr(rsp, 2 * Interpreter::stackElementSize);
1248 }
1249
1250
1251 void TemplateTable::dup() {
1252 transition(vtos, vtos);
1253 __ load_ptr(0, rax);
1254 __ push_ptr(rax);
1255 // stack: ..., a, a
1256 }
1257
1258 void TemplateTable::dup_x1() {
1259 transition(vtos, vtos);
1260 // stack: ..., a, b
1261 __ load_ptr( 0, rax); // load b
1262 __ load_ptr( 1, rcx); // load a
1263 __ store_ptr(1, rax); // store b
1264 __ store_ptr(0, rcx); // store a
1265 __ push_ptr(rax); // push b
1266 // stack: ..., b, a, b
1267 }
1268
1269 void TemplateTable::dup_x2() {
1270 transition(vtos, vtos);
1271 // stack: ..., a, b, c
1272 __ load_ptr( 0, rax); // load c
1273 __ load_ptr( 2, rcx); // load a
1274 __ store_ptr(2, rax); // store c in a
1275 __ push_ptr(rax); // push c
1276 // stack: ..., c, b, c, c
1277 __ load_ptr( 2, rax); // load b
1278 __ store_ptr(2, rcx); // store a in b
1279 // stack: ..., c, a, c, c
1280 __ store_ptr(1, rax); // store b in c
1281 // stack: ..., c, a, b, c
1282 }
1283
1284 void TemplateTable::dup2() {
1285 transition(vtos, vtos);
1286 // stack: ..., a, b
1287 __ load_ptr(1, rax); // load a
1288 __ push_ptr(rax); // push a
1289 __ load_ptr(1, rax); // load b
1290 __ push_ptr(rax); // push b
1291 // stack: ..., a, b, a, b
1292 }
1293
1294
1295 void TemplateTable::dup2_x1() {
1296 transition(vtos, vtos);
1297 // stack: ..., a, b, c
1298 __ load_ptr( 0, rcx); // load c
1299 __ load_ptr( 1, rax); // load b
1300 __ push_ptr(rax); // push b
1301 __ push_ptr(rcx); // push c
1302 // stack: ..., a, b, c, b, c
1303 __ store_ptr(3, rcx); // store c in b
1304 // stack: ..., a, c, c, b, c
1305 __ load_ptr( 4, rcx); // load a
1306 __ store_ptr(2, rcx); // store a in 2nd c
1307 // stack: ..., a, c, a, b, c
1308 __ store_ptr(4, rax); // store b in a
1309 // stack: ..., b, c, a, b, c
1310 }
1311
1312 void TemplateTable::dup2_x2() {
1313 transition(vtos, vtos);
1314 // stack: ..., a, b, c, d
1315 __ load_ptr( 0, rcx); // load d
1316 __ load_ptr( 1, rax); // load c
1317 __ push_ptr(rax); // push c
1318 __ push_ptr(rcx); // push d
1319 // stack: ..., a, b, c, d, c, d
1320 __ load_ptr( 4, rax); // load b
1321 __ store_ptr(2, rax); // store b in d
1322 __ store_ptr(4, rcx); // store d in b
1323 // stack: ..., a, d, c, b, c, d
1324 __ load_ptr( 5, rcx); // load a
1325 __ load_ptr( 3, rax); // load c
1326 __ store_ptr(3, rcx); // store a in c
1327 __ store_ptr(5, rax); // store c in a
1328 // stack: ..., c, d, a, b, c, d
1329 }
1330
1331 void TemplateTable::swap() {
1332 transition(vtos, vtos);
1333 // stack: ..., a, b
1334 __ load_ptr( 1, rcx); // load a
1335 __ load_ptr( 0, rax); // load b
1336 __ store_ptr(0, rcx); // store a in b
1337 __ store_ptr(1, rax); // store b in a
1338 // stack: ..., b, a
1339 }
1340
1341 void TemplateTable::iop2(Operation op) {
1342 transition(itos, itos);
1343 switch (op) {
1344 case add : __ pop_i(rdx); __ addl (rax, rdx); break;
1345 case sub : __ movl(rdx, rax); __ pop_i(rax); __ subl (rax, rdx); break;
1346 case mul : __ pop_i(rdx); __ imull(rax, rdx); break;
1347 case _and : __ pop_i(rdx); __ andl (rax, rdx); break;
1348 case _or : __ pop_i(rdx); __ orl (rax, rdx); break;
1349 case _xor : __ pop_i(rdx); __ xorl (rax, rdx); break;
1350 case shl : __ movl(rcx, rax); __ pop_i(rax); __ shll (rax); break;
1351 case shr : __ movl(rcx, rax); __ pop_i(rax); __ sarl (rax); break;
1352 case ushr : __ movl(rcx, rax); __ pop_i(rax); __ shrl (rax); break;
1353 default : ShouldNotReachHere();
1354 }
1355 }
1356
1357 void TemplateTable::lop2(Operation op) {
1358 transition(ltos, ltos);
1359 switch (op) {
1360 case add : __ pop_l(rdx); __ addptr(rax, rdx); break;
1361 case sub : __ mov(rdx, rax); __ pop_l(rax); __ subptr(rax, rdx); break;
1362 case _and : __ pop_l(rdx); __ andptr(rax, rdx); break;
1363 case _or : __ pop_l(rdx); __ orptr (rax, rdx); break;
1364 case _xor : __ pop_l(rdx); __ xorptr(rax, rdx); break;
1365 default : ShouldNotReachHere();
1366 }
1367 }
1368
1369 void TemplateTable::idiv() {
1370 transition(itos, itos);
1371 __ movl(rcx, rax);
1372 __ pop_i(rax);
1373 // Note: could xor rax and ecx and compare with (-1 ^ min_int). If
1374 // they are not equal, one could do a normal division (no correction
1375 // needed), which may speed up this implementation for the common case.
1376 // (see also JVM spec., p.243 & p.271)
1377 __ corrected_idivl(rcx);
1378 }
1379
1380 void TemplateTable::irem() {
1381 transition(itos, itos);
1382 __ movl(rcx, rax);
1383 __ pop_i(rax);
1384 // Note: could xor rax and ecx and compare with (-1 ^ min_int). If
1385 // they are not equal, one could do a normal division (no correction
1386 // needed), which may speed up this implementation for the common case.
1387 // (see also JVM spec., p.243 & p.271)
1388 __ corrected_idivl(rcx);
1389 __ movl(rax, rdx);
1390 }
1391
1392 void TemplateTable::lmul() {
1393 transition(ltos, ltos);
1394 __ pop_l(rdx);
1395 __ imulq(rax, rdx);
1396 }
1397
1398 void TemplateTable::ldiv() {
1399 transition(ltos, ltos);
1400 __ mov(rcx, rax);
1401 __ pop_l(rax);
1402 // generate explicit div0 check
1403 __ testq(rcx, rcx);
1404 __ jump_cc(Assembler::zero,
1405 RuntimeAddress(Interpreter::_throw_ArithmeticException_entry));
1406 // Note: could xor rax and rcx and compare with (-1 ^ min_int). If
1407 // they are not equal, one could do a normal division (no correction
1408 // needed), which may speed up this implementation for the common case.
1409 // (see also JVM spec., p.243 & p.271)
1410 __ corrected_idivq(rcx); // kills rbx
1411 }
1412
1413 void TemplateTable::lrem() {
1414 transition(ltos, ltos);
1415 __ mov(rcx, rax);
1416 __ pop_l(rax);
1417 __ testq(rcx, rcx);
1418 __ jump_cc(Assembler::zero,
1419 RuntimeAddress(Interpreter::_throw_ArithmeticException_entry));
1420 // Note: could xor rax and rcx and compare with (-1 ^ min_int). If
1421 // they are not equal, one could do a normal division (no correction
1422 // needed), which may speed up this implementation for the common case.
1423 // (see also JVM spec., p.243 & p.271)
1424 __ corrected_idivq(rcx); // kills rbx
1425 __ mov(rax, rdx);
1426 }
1427
1428 void TemplateTable::lshl() {
1429 transition(itos, ltos);
1430 __ movl(rcx, rax); // get shift count
1431 __ pop_l(rax); // get shift value
1432 __ shlq(rax);
1433 }
1434
1435 void TemplateTable::lshr() {
1436 transition(itos, ltos);
1437 __ movl(rcx, rax); // get shift count
1438 __ pop_l(rax); // get shift value
1439 __ sarq(rax);
1440 }
1441
1442 void TemplateTable::lushr() {
1443 transition(itos, ltos);
1444 __ movl(rcx, rax); // get shift count
1445 __ pop_l(rax); // get shift value
1446 __ shrq(rax);
1447 }
1448
1449 void TemplateTable::fop2(Operation op) {
1450 transition(ftos, ftos);
1451
1452 switch (op) {
1453 case add:
1454 __ addss(xmm0, at_rsp());
1455 __ addptr(rsp, Interpreter::stackElementSize);
1456 break;
1457 case sub:
1458 __ movflt(xmm1, xmm0);
1459 __ pop_f(xmm0);
1460 __ subss(xmm0, xmm1);
1461 break;
1462 case mul:
1463 __ mulss(xmm0, at_rsp());
1464 __ addptr(rsp, Interpreter::stackElementSize);
1465 break;
1466 case div:
1467 __ movflt(xmm1, xmm0);
1468 __ pop_f(xmm0);
1469 __ divss(xmm0, xmm1);
1470 break;
1471 case rem:
1472 // On x86_64 platforms the SharedRuntime::frem method is called to perform the
1473 // modulo operation. The frem method calls the function
1474 // double fmod(double x, double y) in math.h. The documentation of fmod states:
1475 // "If x or y is a NaN, a NaN is returned." without specifying what type of NaN
1476 // (signalling or quiet) is returned.
1477 __ movflt(xmm1, xmm0);
1478 __ pop_f(xmm0);
1479 __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::frem), 2);
1480 break;
1481 default:
1482 ShouldNotReachHere();
1483 break;
1484 }
1485 }
1486
1487 void TemplateTable::dop2(Operation op) {
1488 transition(dtos, dtos);
1489 switch (op) {
1490 case add:
1491 __ addsd(xmm0, at_rsp());
1492 __ addptr(rsp, 2 * Interpreter::stackElementSize);
1493 break;
1494 case sub:
1495 __ movdbl(xmm1, xmm0);
1496 __ pop_d(xmm0);
1497 __ subsd(xmm0, xmm1);
1498 break;
1499 case mul:
1500 __ mulsd(xmm0, at_rsp());
1501 __ addptr(rsp, 2 * Interpreter::stackElementSize);
1502 break;
1503 case div:
1504 __ movdbl(xmm1, xmm0);
1505 __ pop_d(xmm0);
1506 __ divsd(xmm0, xmm1);
1507 break;
1508 case rem:
1509 // Similar to fop2(), the modulo operation is performed using the
1510 // SharedRuntime::drem method on x86_64 platforms for the same reasons
1511 // as mentioned in fop2().
1512 __ movdbl(xmm1, xmm0);
1513 __ pop_d(xmm0);
1514 __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::drem), 2);
1515 break;
1516 default:
1517 ShouldNotReachHere();
1518 break;
1519 }
1520 }
1521
1522 void TemplateTable::ineg() {
1523 transition(itos, itos);
1524 __ negl(rax);
1525 }
1526
1527 void TemplateTable::lneg() {
1528 transition(ltos, ltos);
1529 __ negq(rax);
1530 }
1531
1532 // Note: 'double' and 'long long' have 32-bits alignment on x86.
1533 static jlong* double_quadword(jlong *adr, jlong lo, jlong hi) {
1534 // Use the expression (adr)&(~0xF) to provide 128-bits aligned address
1535 // of 128-bits operands for SSE instructions.
1536 jlong *operand = (jlong*)(((intptr_t)adr)&((intptr_t)(~0xF)));
1537 // Store the value to a 128-bits operand.
1538 operand[0] = lo;
1539 operand[1] = hi;
1540 return operand;
1541 }
1542
1543 // Buffer for 128-bits masks used by SSE instructions.
1544 static jlong float_signflip_pool[2*2];
1545 static jlong double_signflip_pool[2*2];
1546
1547 void TemplateTable::fneg() {
1548 transition(ftos, ftos);
1549 static jlong *float_signflip = double_quadword(&float_signflip_pool[1], CONST64(0x8000000080000000), CONST64(0x8000000080000000));
1550 __ xorps(xmm0, ExternalAddress((address) float_signflip), rscratch1);
1551 }
1552
1553 void TemplateTable::dneg() {
1554 transition(dtos, dtos);
1555 static jlong *double_signflip =
1556 double_quadword(&double_signflip_pool[1], CONST64(0x8000000000000000), CONST64(0x8000000000000000));
1557 __ xorpd(xmm0, ExternalAddress((address) double_signflip), rscratch1);
1558 }
1559
1560 void TemplateTable::iinc() {
1561 transition(vtos, vtos);
1562 __ load_signed_byte(rdx, at_bcp(2)); // get constant
1563 locals_index(rbx);
1564 __ addl(iaddress(rbx), rdx);
1565 }
1566
1567 void TemplateTable::wide_iinc() {
1568 transition(vtos, vtos);
1569 __ movl(rdx, at_bcp(4)); // get constant
1570 locals_index_wide(rbx);
1571 __ bswapl(rdx); // swap bytes & sign-extend constant
1572 __ sarl(rdx, 16);
1573 __ addl(iaddress(rbx), rdx);
1574 // Note: should probably use only one movl to get both
1575 // the index and the constant -> fix this
1576 }
1577
1578 void TemplateTable::convert() {
1579 // Checking
1580 #ifdef ASSERT
1581 {
1582 TosState tos_in = ilgl;
1583 TosState tos_out = ilgl;
1584 switch (bytecode()) {
1585 case Bytecodes::_i2l: // fall through
1586 case Bytecodes::_i2f: // fall through
1587 case Bytecodes::_i2d: // fall through
1588 case Bytecodes::_i2b: // fall through
1589 case Bytecodes::_i2c: // fall through
1590 case Bytecodes::_i2s: tos_in = itos; break;
1591 case Bytecodes::_l2i: // fall through
1592 case Bytecodes::_l2f: // fall through
1593 case Bytecodes::_l2d: tos_in = ltos; break;
1594 case Bytecodes::_f2i: // fall through
1595 case Bytecodes::_f2l: // fall through
1596 case Bytecodes::_f2d: tos_in = ftos; break;
1597 case Bytecodes::_d2i: // fall through
1598 case Bytecodes::_d2l: // fall through
1599 case Bytecodes::_d2f: tos_in = dtos; break;
1600 default : ShouldNotReachHere();
1601 }
1602 switch (bytecode()) {
1603 case Bytecodes::_l2i: // fall through
1604 case Bytecodes::_f2i: // fall through
1605 case Bytecodes::_d2i: // fall through
1606 case Bytecodes::_i2b: // fall through
1607 case Bytecodes::_i2c: // fall through
1608 case Bytecodes::_i2s: tos_out = itos; break;
1609 case Bytecodes::_i2l: // fall through
1610 case Bytecodes::_f2l: // fall through
1611 case Bytecodes::_d2l: tos_out = ltos; break;
1612 case Bytecodes::_i2f: // fall through
1613 case Bytecodes::_l2f: // fall through
1614 case Bytecodes::_d2f: tos_out = ftos; break;
1615 case Bytecodes::_i2d: // fall through
1616 case Bytecodes::_l2d: // fall through
1617 case Bytecodes::_f2d: tos_out = dtos; break;
1618 default : ShouldNotReachHere();
1619 }
1620 transition(tos_in, tos_out);
1621 }
1622 #endif // ASSERT
1623
1624 static const int64_t is_nan = 0x8000000000000000L;
1625
1626 // Conversion
1627 switch (bytecode()) {
1628 case Bytecodes::_i2l:
1629 __ movslq(rax, rax);
1630 break;
1631 case Bytecodes::_i2f:
1632 __ cvtsi2ssl(xmm0, rax);
1633 break;
1634 case Bytecodes::_i2d:
1635 __ cvtsi2sdl(xmm0, rax);
1636 break;
1637 case Bytecodes::_i2b:
1638 __ movsbl(rax, rax);
1639 break;
1640 case Bytecodes::_i2c:
1641 __ movzwl(rax, rax);
1642 break;
1643 case Bytecodes::_i2s:
1644 __ movswl(rax, rax);
1645 break;
1646 case Bytecodes::_l2i:
1647 __ movl(rax, rax);
1648 break;
1649 case Bytecodes::_l2f:
1650 __ cvtsi2ssq(xmm0, rax);
1651 break;
1652 case Bytecodes::_l2d:
1653 __ cvtsi2sdq(xmm0, rax);
1654 break;
1655 case Bytecodes::_f2i:
1656 {
1657 Label L;
1658 __ cvttss2sil(rax, xmm0);
1659 __ cmpl(rax, 0x80000000); // NaN or overflow/underflow?
1660 __ jcc(Assembler::notEqual, L);
1661 __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::f2i), 1);
1662 __ bind(L);
1663 }
1664 break;
1665 case Bytecodes::_f2l:
1666 {
1667 Label L;
1668 __ cvttss2siq(rax, xmm0);
1669 // NaN or overflow/underflow?
1670 __ cmp64(rax, ExternalAddress((address) &is_nan), rscratch1);
1671 __ jcc(Assembler::notEqual, L);
1672 __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::f2l), 1);
1673 __ bind(L);
1674 }
1675 break;
1676 case Bytecodes::_f2d:
1677 __ cvtss2sd(xmm0, xmm0);
1678 break;
1679 case Bytecodes::_d2i:
1680 {
1681 Label L;
1682 __ cvttsd2sil(rax, xmm0);
1683 __ cmpl(rax, 0x80000000); // NaN or overflow/underflow?
1684 __ jcc(Assembler::notEqual, L);
1685 __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::d2i), 1);
1686 __ bind(L);
1687 }
1688 break;
1689 case Bytecodes::_d2l:
1690 {
1691 Label L;
1692 __ cvttsd2siq(rax, xmm0);
1693 // NaN or overflow/underflow?
1694 __ cmp64(rax, ExternalAddress((address) &is_nan), rscratch1);
1695 __ jcc(Assembler::notEqual, L);
1696 __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::d2l), 1);
1697 __ bind(L);
1698 }
1699 break;
1700 case Bytecodes::_d2f:
1701 __ cvtsd2ss(xmm0, xmm0);
1702 break;
1703 default:
1704 ShouldNotReachHere();
1705 }
1706 }
1707
1708 void TemplateTable::lcmp() {
1709 transition(ltos, itos);
1710 Label done;
1711 __ pop_l(rdx);
1712 __ cmpq(rdx, rax);
1713 __ movl(rax, -1);
1714 __ jccb(Assembler::less, done);
1715 __ setb(Assembler::notEqual, rax);
1716 __ movzbl(rax, rax);
1717 __ bind(done);
1718 }
1719
1720 void TemplateTable::float_cmp(bool is_float, int unordered_result) {
1721 Label done;
1722 if (is_float) {
1723 // XXX get rid of pop here, use ... reg, mem32
1724 __ pop_f(xmm1);
1725 __ ucomiss(xmm1, xmm0);
1726 } else {
1727 // XXX get rid of pop here, use ... reg, mem64
1728 __ pop_d(xmm1);
1729 __ ucomisd(xmm1, xmm0);
1730 }
1731 if (unordered_result < 0) {
1732 __ movl(rax, -1);
1733 __ jccb(Assembler::parity, done);
1734 __ jccb(Assembler::below, done);
1735 __ setb(Assembler::notEqual, rdx);
1736 __ movzbl(rax, rdx);
1737 } else {
1738 __ movl(rax, 1);
1739 __ jccb(Assembler::parity, done);
1740 __ jccb(Assembler::above, done);
1741 __ movl(rax, 0);
1742 __ jccb(Assembler::equal, done);
1743 __ decrementl(rax);
1744 }
1745 __ bind(done);
1746 }
1747
1748 void TemplateTable::branch(bool is_jsr, bool is_wide) {
1749 __ get_method(rcx); // rcx holds method
1750 __ profile_taken_branch(rax); // rax holds updated MDP
1751
1752 const ByteSize be_offset = MethodCounters::backedge_counter_offset() +
1753 InvocationCounter::counter_offset();
1754 const ByteSize inv_offset = MethodCounters::invocation_counter_offset() +
1755 InvocationCounter::counter_offset();
1756
1757 // Load up edx with the branch displacement
1758 if (is_wide) {
1759 __ movl(rdx, at_bcp(1));
1760 } else {
1761 __ load_signed_short(rdx, at_bcp(1));
1762 }
1763 __ bswapl(rdx);
1764
1765 if (!is_wide) {
1766 __ sarl(rdx, 16);
1767 }
1768 __ movl2ptr(rdx, rdx);
1769
1770 // Handle all the JSR stuff here, then exit.
1771 // It's much shorter and cleaner than intermingling with the non-JSR
1772 // normal-branch stuff occurring below.
1773 if (is_jsr) {
1774 // Pre-load the next target bytecode into rbx
1775 __ load_unsigned_byte(rbx, Address(rbcp, rdx, Address::times_1, 0));
1776
1777 // compute return address as bci in rax
1778 __ lea(rax, at_bcp((is_wide ? 5 : 3) -
1779 in_bytes(ConstMethod::codes_offset())));
1780 __ subptr(rax, Address(rcx, Method::const_offset()));
1781 // Adjust the bcp in r13 by the displacement in rdx
1782 __ addptr(rbcp, rdx);
1783 // jsr returns atos that is not an oop
1784 __ push_i(rax);
1785 __ dispatch_only(vtos, true);
1786 return;
1787 }
1788
1789 // Normal (non-jsr) branch handling
1790
1791 // Adjust the bcp in r13 by the displacement in rdx
1792 __ addptr(rbcp, rdx);
1793
1794 assert(UseLoopCounter || !UseOnStackReplacement,
1795 "on-stack-replacement requires loop counters");
1796 Label backedge_counter_overflow;
1797 Label dispatch;
1798 if (UseLoopCounter) {
1799 // increment backedge counter for backward branches
1800 // rax: MDO
1801 // rcx: method
1802 // rdx: target offset
1803 // r13: target bcp
1804 // r14: locals pointer
1805 __ testl(rdx, rdx); // check if forward or backward branch
1806 __ jcc(Assembler::positive, dispatch); // count only if backward branch
1807
1808 // check if MethodCounters exists
1809 Label has_counters;
1810 __ movptr(rax, Address(rcx, Method::method_counters_offset()));
1811 __ testptr(rax, rax);
1812 __ jcc(Assembler::notZero, has_counters);
1813 __ push(rdx);
1814 __ push(rcx);
1815 __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::build_method_counters),
1816 rcx);
1817 __ pop(rcx);
1818 __ pop(rdx);
1819 __ movptr(rax, Address(rcx, Method::method_counters_offset()));
1820 __ testptr(rax, rax);
1821 __ jcc(Assembler::zero, dispatch);
1822 __ bind(has_counters);
1823
1824 Label no_mdo;
1825 if (ProfileInterpreter) {
1826 // Are we profiling?
1827 __ movptr(rbx, Address(rcx, in_bytes(Method::method_data_offset())));
1828 __ testptr(rbx, rbx);
1829 __ jccb(Assembler::zero, no_mdo);
1830 // Increment the MDO backedge counter
1831 const Address mdo_backedge_counter(rbx, in_bytes(MethodData::backedge_counter_offset()) +
1832 in_bytes(InvocationCounter::counter_offset()));
1833 const Address mask(rbx, in_bytes(MethodData::backedge_mask_offset()));
1834 __ increment_mask_and_jump(mdo_backedge_counter, mask, rax,
1835 UseOnStackReplacement ? &backedge_counter_overflow : nullptr);
1836 __ jmp(dispatch);
1837 }
1838 __ bind(no_mdo);
1839 // Increment backedge counter in MethodCounters*
1840 __ movptr(rcx, Address(rcx, Method::method_counters_offset()));
1841 const Address mask(rcx, in_bytes(MethodCounters::backedge_mask_offset()));
1842 __ increment_mask_and_jump(Address(rcx, be_offset), mask, rax,
1843 UseOnStackReplacement ? &backedge_counter_overflow : nullptr);
1844 __ bind(dispatch);
1845 }
1846
1847 // Pre-load the next target bytecode into rbx
1848 __ load_unsigned_byte(rbx, Address(rbcp, 0));
1849
1850 // continue with the bytecode @ target
1851 // rax: return bci for jsr's, unused otherwise
1852 // rbx: target bytecode
1853 // r13: target bcp
1854 __ dispatch_only(vtos, true);
1855
1856 if (UseLoopCounter) {
1857 if (UseOnStackReplacement) {
1858 Label set_mdp;
1859 // invocation counter overflow
1860 __ bind(backedge_counter_overflow);
1861 __ negptr(rdx);
1862 __ addptr(rdx, rbcp); // branch bcp
1863 // IcoResult frequency_counter_overflow([JavaThread*], address branch_bcp)
1864 __ call_VM(noreg,
1865 CAST_FROM_FN_PTR(address,
1866 InterpreterRuntime::frequency_counter_overflow),
1867 rdx);
1868
1869 // rax: osr nmethod (osr ok) or null (osr not possible)
1870 // rdx: scratch
1871 // r14: locals pointer
1872 // r13: bcp
1873 __ testptr(rax, rax); // test result
1874 __ jcc(Assembler::zero, dispatch); // no osr if null
1875 // nmethod may have been invalidated (VM may block upon call_VM return)
1876 __ cmpb(Address(rax, nmethod::state_offset()), nmethod::in_use);
1877 __ jcc(Assembler::notEqual, dispatch);
1878
1879 // We have the address of an on stack replacement routine in rax.
1880 // In preparation of invoking it, first we must migrate the locals
1881 // and monitors from off the interpreter frame on the stack.
1882 // Ensure to save the osr nmethod over the migration call,
1883 // it will be preserved in rbx.
1884 __ mov(rbx, rax);
1885
1886 JFR_ONLY(__ enter_jfr_critical_section();)
1887
1888 call_VM(noreg, CAST_FROM_FN_PTR(address, SharedRuntime::OSR_migration_begin));
1889
1890 // rax is OSR buffer, move it to expected parameter location
1891 __ mov(j_rarg0, rax);
1892 // We use j_rarg definitions here so that registers don't conflict as parameter
1893 // registers change across platforms as we are in the midst of a calling
1894 // sequence to the OSR nmethod and we don't want collision. These are NOT parameters.
1895
1896 const Register retaddr = j_rarg2;
1897 const Register sender_sp = j_rarg1;
1898
1899 // pop the interpreter frame
1900 __ movptr(sender_sp, Address(rbp, frame::interpreter_frame_sender_sp_offset * wordSize)); // get sender sp
1901 __ leave(); // remove frame anchor
1902 JFR_ONLY(__ leave_jfr_critical_section();)
1903 __ pop(retaddr); // get return address
1904 __ mov(rsp, sender_sp); // set sp to sender sp
1905 // Ensure compiled code always sees stack at proper alignment
1906 __ andptr(rsp, -(StackAlignmentInBytes));
1907
1908 // push the return address
1909 __ push(retaddr);
1910
1911 // and begin the OSR nmethod
1912 __ jmp(Address(rbx, nmethod::osr_entry_point_offset()));
1913 }
1914 }
1915 }
1916
1917 void TemplateTable::if_0cmp(Condition cc) {
1918 transition(itos, vtos);
1919 // assume branch is more often taken than not (loops use backward branches)
1920 Label not_taken;
1921 __ testl(rax, rax);
1922 __ jcc(j_not(cc), not_taken);
1923 branch(false, false);
1924 __ bind(not_taken);
1925 __ profile_not_taken_branch(rax);
1926 }
1927
1928 void TemplateTable::if_icmp(Condition cc) {
1929 transition(itos, vtos);
1930 // assume branch is more often taken than not (loops use backward branches)
1931 Label not_taken;
1932 __ pop_i(rdx);
1933 __ cmpl(rdx, rax);
1934 __ jcc(j_not(cc), not_taken);
1935 branch(false, false);
1936 __ bind(not_taken);
1937 __ profile_not_taken_branch(rax);
1938 }
1939
1940 void TemplateTable::if_nullcmp(Condition cc) {
1941 transition(atos, vtos);
1942 // assume branch is more often taken than not (loops use backward branches)
1943 Label not_taken;
1944 __ testptr(rax, rax);
1945 __ jcc(j_not(cc), not_taken);
1946 branch(false, false);
1947 __ bind(not_taken);
1948 __ profile_not_taken_branch(rax);
1949 }
1950
1951 void TemplateTable::if_acmp(Condition cc) {
1952 transition(atos, vtos);
1953 // assume branch is more often taken than not (loops use backward branches)
1954 Label taken, not_taken;
1955 __ pop_ptr(rdx);
1956
1957 __ profile_acmp(rbx, rdx, rax, rcx);
1958
1959 const int is_inline_type_mask = markWord::inline_type_pattern;
1960 if (EnableValhalla) {
1961 __ cmpoop(rdx, rax);
1962 __ jcc(Assembler::equal, (cc == equal) ? taken : not_taken);
1963
1964 // might be substitutable, test if either rax or rdx is null
1965 __ testptr(rax, rax);
1966 __ jcc(Assembler::zero, (cc == equal) ? not_taken : taken);
1967 __ testptr(rdx, rdx);
1968 __ jcc(Assembler::zero, (cc == equal) ? not_taken : taken);
1969
1970 // and both are values ?
1971 __ movptr(rbx, Address(rdx, oopDesc::mark_offset_in_bytes()));
1972 __ andptr(rbx, Address(rax, oopDesc::mark_offset_in_bytes()));
1973 __ andptr(rbx, is_inline_type_mask);
1974 __ cmpptr(rbx, is_inline_type_mask);
1975 __ jcc(Assembler::notEqual, (cc == equal) ? not_taken : taken);
1976
1977 // same value klass ?
1978 __ load_metadata(rbx, rdx);
1979 __ load_metadata(rcx, rax);
1980 __ cmpptr(rbx, rcx);
1981 __ jcc(Assembler::notEqual, (cc == equal) ? not_taken : taken);
1982
1983 // Know both are the same type, let's test for substitutability...
1984 if (cc == equal) {
1985 invoke_is_substitutable(rax, rdx, taken, not_taken);
1986 } else {
1987 invoke_is_substitutable(rax, rdx, not_taken, taken);
1988 }
1989 __ stop("Not reachable");
1990 }
1991
1992 __ cmpoop(rdx, rax);
1993 __ jcc(j_not(cc), not_taken);
1994 __ bind(taken);
1995 branch(false, false);
1996 __ bind(not_taken);
1997 __ profile_not_taken_branch(rax, true);
1998 }
1999
2000 void TemplateTable::invoke_is_substitutable(Register aobj, Register bobj,
2001 Label& is_subst, Label& not_subst) {
2002 __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::is_substitutable), aobj, bobj);
2003 // Restored...rax answer, jmp to outcome...
2004 __ testl(rax, rax);
2005 __ jcc(Assembler::zero, not_subst);
2006 __ jmp(is_subst);
2007 }
2008
2009 void TemplateTable::ret() {
2010 transition(vtos, vtos);
2011 locals_index(rbx);
2012 __ movslq(rbx, iaddress(rbx)); // get return bci, compute return bcp
2013 __ profile_ret(rbx, rcx);
2014 __ get_method(rax);
2015 __ movptr(rbcp, Address(rax, Method::const_offset()));
2016 __ lea(rbcp, Address(rbcp, rbx, Address::times_1,
2017 ConstMethod::codes_offset()));
2018 __ dispatch_next(vtos, 0, true);
2019 }
2020
2021 void TemplateTable::wide_ret() {
2022 transition(vtos, vtos);
2023 locals_index_wide(rbx);
2024 __ movptr(rbx, aaddress(rbx)); // get return bci, compute return bcp
2025 __ profile_ret(rbx, rcx);
2026 __ get_method(rax);
2027 __ movptr(rbcp, Address(rax, Method::const_offset()));
2028 __ lea(rbcp, Address(rbcp, rbx, Address::times_1, ConstMethod::codes_offset()));
2029 __ dispatch_next(vtos, 0, true);
2030 }
2031
2032 void TemplateTable::tableswitch() {
2033 Label default_case, continue_execution;
2034 transition(itos, vtos);
2035
2036 // align r13/rsi
2037 __ lea(rbx, at_bcp(BytesPerInt));
2038 __ andptr(rbx, -BytesPerInt);
2039 // load lo & hi
2040 __ movl(rcx, Address(rbx, BytesPerInt));
2041 __ movl(rdx, Address(rbx, 2 * BytesPerInt));
2042 __ bswapl(rcx);
2043 __ bswapl(rdx);
2044 // check against lo & hi
2045 __ cmpl(rax, rcx);
2046 __ jcc(Assembler::less, default_case);
2047 __ cmpl(rax, rdx);
2048 __ jcc(Assembler::greater, default_case);
2049 // lookup dispatch offset
2050 __ subl(rax, rcx);
2051 __ movl(rdx, Address(rbx, rax, Address::times_4, 3 * BytesPerInt));
2052 __ profile_switch_case(rax, rbx, rcx);
2053 // continue execution
2054 __ bind(continue_execution);
2055 __ bswapl(rdx);
2056 __ movl2ptr(rdx, rdx);
2057 __ load_unsigned_byte(rbx, Address(rbcp, rdx, Address::times_1));
2058 __ addptr(rbcp, rdx);
2059 __ dispatch_only(vtos, true);
2060 // handle default
2061 __ bind(default_case);
2062 __ profile_switch_default(rax);
2063 __ movl(rdx, Address(rbx, 0));
2064 __ jmp(continue_execution);
2065 }
2066
2067 void TemplateTable::lookupswitch() {
2068 transition(itos, itos);
2069 __ stop("lookupswitch bytecode should have been rewritten");
2070 }
2071
2072 void TemplateTable::fast_linearswitch() {
2073 transition(itos, vtos);
2074 Label loop_entry, loop, found, continue_execution;
2075 // bswap rax so we can avoid bswapping the table entries
2076 __ bswapl(rax);
2077 // align r13
2078 __ lea(rbx, at_bcp(BytesPerInt)); // btw: should be able to get rid of
2079 // this instruction (change offsets
2080 // below)
2081 __ andptr(rbx, -BytesPerInt);
2082 // set counter
2083 __ movl(rcx, Address(rbx, BytesPerInt));
2084 __ bswapl(rcx);
2085 __ jmpb(loop_entry);
2086 // table search
2087 __ bind(loop);
2088 __ cmpl(rax, Address(rbx, rcx, Address::times_8, 2 * BytesPerInt));
2089 __ jcc(Assembler::equal, found);
2090 __ bind(loop_entry);
2091 __ decrementl(rcx);
2092 __ jcc(Assembler::greaterEqual, loop);
2093 // default case
2094 __ profile_switch_default(rax);
2095 __ movl(rdx, Address(rbx, 0));
2096 __ jmp(continue_execution);
2097 // entry found -> get offset
2098 __ bind(found);
2099 __ movl(rdx, Address(rbx, rcx, Address::times_8, 3 * BytesPerInt));
2100 __ profile_switch_case(rcx, rax, rbx);
2101 // continue execution
2102 __ bind(continue_execution);
2103 __ bswapl(rdx);
2104 __ movl2ptr(rdx, rdx);
2105 __ load_unsigned_byte(rbx, Address(rbcp, rdx, Address::times_1));
2106 __ addptr(rbcp, rdx);
2107 __ dispatch_only(vtos, true);
2108 }
2109
2110 void TemplateTable::fast_binaryswitch() {
2111 transition(itos, vtos);
2112 // Implementation using the following core algorithm:
2113 //
2114 // int binary_search(int key, LookupswitchPair* array, int n) {
2115 // // Binary search according to "Methodik des Programmierens" by
2116 // // Edsger W. Dijkstra and W.H.J. Feijen, Addison Wesley Germany 1985.
2117 // int i = 0;
2118 // int j = n;
2119 // while (i+1 < j) {
2120 // // invariant P: 0 <= i < j <= n and (a[i] <= key < a[j] or Q)
2121 // // with Q: for all i: 0 <= i < n: key < a[i]
2122 // // where a stands for the array and assuming that the (inexisting)
2123 // // element a[n] is infinitely big.
2124 // int h = (i + j) >> 1;
2125 // // i < h < j
2126 // if (key < array[h].fast_match()) {
2127 // j = h;
2128 // } else {
2129 // i = h;
2130 // }
2131 // }
2132 // // R: a[i] <= key < a[i+1] or Q
2133 // // (i.e., if key is within array, i is the correct index)
2134 // return i;
2135 // }
2136
2137 // Register allocation
2138 const Register key = rax; // already set (tosca)
2139 const Register array = rbx;
2140 const Register i = rcx;
2141 const Register j = rdx;
2142 const Register h = rdi;
2143 const Register temp = rsi;
2144
2145 // Find array start
2146 __ lea(array, at_bcp(3 * BytesPerInt)); // btw: should be able to
2147 // get rid of this
2148 // instruction (change
2149 // offsets below)
2150 __ andptr(array, -BytesPerInt);
2151
2152 // Initialize i & j
2153 __ xorl(i, i); // i = 0;
2154 __ movl(j, Address(array, -BytesPerInt)); // j = length(array);
2155
2156 // Convert j into native byteordering
2157 __ bswapl(j);
2158
2159 // And start
2160 Label entry;
2161 __ jmp(entry);
2162
2163 // binary search loop
2164 {
2165 Label loop;
2166 __ bind(loop);
2167 // int h = (i + j) >> 1;
2168 __ leal(h, Address(i, j, Address::times_1)); // h = i + j;
2169 __ sarl(h, 1); // h = (i + j) >> 1;
2170 // if (key < array[h].fast_match()) {
2171 // j = h;
2172 // } else {
2173 // i = h;
2174 // }
2175 // Convert array[h].match to native byte-ordering before compare
2176 __ movl(temp, Address(array, h, Address::times_8));
2177 __ bswapl(temp);
2178 __ cmpl(key, temp);
2179 // j = h if (key < array[h].fast_match())
2180 __ cmov32(Assembler::less, j, h);
2181 // i = h if (key >= array[h].fast_match())
2182 __ cmov32(Assembler::greaterEqual, i, h);
2183 // while (i+1 < j)
2184 __ bind(entry);
2185 __ leal(h, Address(i, 1)); // i+1
2186 __ cmpl(h, j); // i+1 < j
2187 __ jcc(Assembler::less, loop);
2188 }
2189
2190 // end of binary search, result index is i (must check again!)
2191 Label default_case;
2192 // Convert array[i].match to native byte-ordering before compare
2193 __ movl(temp, Address(array, i, Address::times_8));
2194 __ bswapl(temp);
2195 __ cmpl(key, temp);
2196 __ jcc(Assembler::notEqual, default_case);
2197
2198 // entry found -> j = offset
2199 __ movl(j , Address(array, i, Address::times_8, BytesPerInt));
2200 __ profile_switch_case(i, key, array);
2201 __ bswapl(j);
2202 __ movslq(j, j);
2203
2204 __ load_unsigned_byte(rbx, Address(rbcp, j, Address::times_1));
2205 __ addptr(rbcp, j);
2206 __ dispatch_only(vtos, true);
2207
2208 // default case -> j = default offset
2209 __ bind(default_case);
2210 __ profile_switch_default(i);
2211 __ movl(j, Address(array, -2 * BytesPerInt));
2212 __ bswapl(j);
2213 __ movslq(j, j);
2214
2215 __ load_unsigned_byte(rbx, Address(rbcp, j, Address::times_1));
2216 __ addptr(rbcp, j);
2217 __ dispatch_only(vtos, true);
2218 }
2219
2220 void TemplateTable::_return(TosState state) {
2221 transition(state, state);
2222
2223 assert(_desc->calls_vm(),
2224 "inconsistent calls_vm information"); // call in remove_activation
2225
2226 if (_desc->bytecode() == Bytecodes::_return_register_finalizer) {
2227 assert(state == vtos, "only valid state");
2228 Register robj = c_rarg1;
2229 __ movptr(robj, aaddress(0));
2230 __ load_klass(rdi, robj, rscratch1);
2231 __ testb(Address(rdi, Klass::misc_flags_offset()), KlassFlags::_misc_has_finalizer);
2232 Label skip_register_finalizer;
2233 __ jcc(Assembler::zero, skip_register_finalizer);
2234
2235 __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::register_finalizer), robj);
2236
2237 __ bind(skip_register_finalizer);
2238 }
2239
2240 if (_desc->bytecode() != Bytecodes::_return_register_finalizer) {
2241 Label no_safepoint;
2242 NOT_PRODUCT(__ block_comment("Thread-local Safepoint poll"));
2243 __ testb(Address(r15_thread, JavaThread::polling_word_offset()), SafepointMechanism::poll_bit());
2244 __ jcc(Assembler::zero, no_safepoint);
2245 __ push(state);
2246 __ push_cont_fastpath();
2247 __ call_VM(noreg, CAST_FROM_FN_PTR(address,
2248 InterpreterRuntime::at_safepoint));
2249 __ pop_cont_fastpath();
2250 __ pop(state);
2251 __ bind(no_safepoint);
2252 }
2253
2254 // Narrow result if state is itos but result type is smaller.
2255 // Need to narrow in the return bytecode rather than in generate_return_entry
2256 // since compiled code callers expect the result to already be narrowed.
2257 if (state == itos) {
2258 __ narrow(rax);
2259 }
2260
2261 __ remove_activation(state, rbcp, true, true, true);
2262
2263 __ jmp(rbcp);
2264 }
2265
2266 // ----------------------------------------------------------------------------
2267 // Volatile variables demand their effects be made known to all CPU's
2268 // in order. Store buffers on most chips allow reads & writes to
2269 // reorder; the JMM's ReadAfterWrite.java test fails in -Xint mode
2270 // without some kind of memory barrier (i.e., it's not sufficient that
2271 // the interpreter does not reorder volatile references, the hardware
2272 // also must not reorder them).
2273 //
2274 // According to the new Java Memory Model (JMM):
2275 // (1) All volatiles are serialized wrt to each other. ALSO reads &
2276 // writes act as acquire & release, so:
2277 // (2) A read cannot let unrelated NON-volatile memory refs that
2278 // happen after the read float up to before the read. It's OK for
2279 // non-volatile memory refs that happen before the volatile read to
2280 // float down below it.
2281 // (3) Similar a volatile write cannot let unrelated NON-volatile
2282 // memory refs that happen BEFORE the write float down to after the
2283 // write. It's OK for non-volatile memory refs that happen after the
2284 // volatile write to float up before it.
2285 //
2286 // We only put in barriers around volatile refs (they are expensive),
2287 // not _between_ memory refs (that would require us to track the
2288 // flavor of the previous memory refs). Requirements (2) and (3)
2289 // require some barriers before volatile stores and after volatile
2290 // loads. These nearly cover requirement (1) but miss the
2291 // volatile-store-volatile-load case. This final case is placed after
2292 // volatile-stores although it could just as well go before
2293 // volatile-loads.
2294
2295 void TemplateTable::volatile_barrier(Assembler::Membar_mask_bits order_constraint ) {
2296 // Helper function to insert a is-volatile test and memory barrier
2297 __ membar(order_constraint);
2298 }
2299
2300 void TemplateTable::resolve_cache_and_index_for_method(int byte_no,
2301 Register cache,
2302 Register index) {
2303 const Register temp = rbx;
2304 assert_different_registers(cache, index, temp);
2305
2306 Label L_clinit_barrier_slow, L_done;
2307
2308 Bytecodes::Code code = bytecode();
2309
2310 assert(byte_no == f1_byte || byte_no == f2_byte, "byte_no out of range");
2311
2312 __ load_method_entry(cache, index);
2313 switch(byte_no) {
2314 case f1_byte:
2315 __ load_unsigned_byte(temp, Address(cache, in_bytes(ResolvedMethodEntry::bytecode1_offset())));
2316 break;
2317 case f2_byte:
2318 __ load_unsigned_byte(temp, Address(cache, in_bytes(ResolvedMethodEntry::bytecode2_offset())));
2319 break;
2320 default:
2321 ShouldNotReachHere();
2322 }
2323 __ cmpl(temp, code); // have we resolved this bytecode?
2324
2325 // Class initialization barrier for static methods
2326 if (VM_Version::supports_fast_class_init_checks() && bytecode() == Bytecodes::_invokestatic) {
2327 const Register method = temp;
2328 const Register klass = temp;
2329
2330 __ jcc(Assembler::notEqual, L_clinit_barrier_slow);
2331 __ movptr(method, Address(cache, in_bytes(ResolvedMethodEntry::method_offset())));
2332 __ load_method_holder(klass, method);
2333 __ clinit_barrier(klass, &L_done, /*L_slow_path*/ nullptr);
2334 __ bind(L_clinit_barrier_slow);
2335 } else {
2336 __ jcc(Assembler::equal, L_done);
2337 }
2338
2339 // resolve first time through
2340 // Class initialization barrier slow path lands here as well.
2341 address entry = CAST_FROM_FN_PTR(address, InterpreterRuntime::resolve_from_cache);
2342 __ movl(temp, code);
2343 __ call_VM(noreg, entry, temp);
2344 // Update registers with resolved info
2345 __ load_method_entry(cache, index);
2346 __ bind(L_done);
2347 }
2348
2349 void TemplateTable::resolve_cache_and_index_for_field(int byte_no,
2350 Register cache,
2351 Register index) {
2352 const Register temp = rbx;
2353 assert_different_registers(cache, index, temp);
2354
2355 Label L_clinit_barrier_slow, L_done;
2356
2357 Bytecodes::Code code = bytecode();
2358 switch (code) {
2359 case Bytecodes::_nofast_getfield: code = Bytecodes::_getfield; break;
2360 case Bytecodes::_nofast_putfield: code = Bytecodes::_putfield; break;
2361 default: break;
2362 }
2363
2364 assert(byte_no == f1_byte || byte_no == f2_byte, "byte_no out of range");
2365 __ load_field_entry(cache, index);
2366 if (byte_no == f1_byte) {
2367 __ load_unsigned_byte(temp, Address(cache, in_bytes(ResolvedFieldEntry::get_code_offset())));
2368 } else {
2369 __ load_unsigned_byte(temp, Address(cache, in_bytes(ResolvedFieldEntry::put_code_offset())));
2370 }
2371 __ cmpl(temp, code); // have we resolved this bytecode?
2372
2373 // Class initialization barrier for static fields
2374 if (VM_Version::supports_fast_class_init_checks() &&
2375 (bytecode() == Bytecodes::_getstatic || bytecode() == Bytecodes::_putstatic)) {
2376 const Register field_holder = temp;
2377
2378 __ jcc(Assembler::notEqual, L_clinit_barrier_slow);
2379 __ movptr(field_holder, Address(cache, in_bytes(ResolvedFieldEntry::field_holder_offset())));
2380 __ clinit_barrier(field_holder, &L_done, /*L_slow_path*/ nullptr);
2381 __ bind(L_clinit_barrier_slow);
2382 } else {
2383 __ jcc(Assembler::equal, L_done);
2384 }
2385
2386 // resolve first time through
2387 // Class initialization barrier slow path lands here as well.
2388 address entry = CAST_FROM_FN_PTR(address, InterpreterRuntime::resolve_from_cache);
2389 __ movl(temp, code);
2390 __ call_VM(noreg, entry, temp);
2391 // Update registers with resolved info
2392 __ load_field_entry(cache, index);
2393 __ bind(L_done);
2394 }
2395
2396 void TemplateTable::load_resolved_field_entry(Register obj,
2397 Register cache,
2398 Register tos_state,
2399 Register offset,
2400 Register flags,
2401 bool is_static = false) {
2402 assert_different_registers(cache, tos_state, flags, offset);
2403
2404 // Field offset
2405 __ load_sized_value(offset, Address(cache, in_bytes(ResolvedFieldEntry::field_offset_offset())), sizeof(int), true /*is_signed*/);
2406
2407 // Flags
2408 __ load_unsigned_byte(flags, Address(cache, in_bytes(ResolvedFieldEntry::flags_offset())));
2409
2410 // TOS state
2411 __ load_unsigned_byte(tos_state, Address(cache, in_bytes(ResolvedFieldEntry::type_offset())));
2412
2413 // Klass overwrite register
2414 if (is_static) {
2415 __ movptr(obj, Address(cache, ResolvedFieldEntry::field_holder_offset()));
2416 const int mirror_offset = in_bytes(Klass::java_mirror_offset());
2417 __ movptr(obj, Address(obj, mirror_offset));
2418 __ resolve_oop_handle(obj, rscratch2);
2419 }
2420
2421 }
2422
2423 void TemplateTable::load_invokedynamic_entry(Register method) {
2424 // setup registers
2425 const Register appendix = rax;
2426 const Register cache = rcx;
2427 const Register index = rdx;
2428 assert_different_registers(method, appendix, cache, index);
2429
2430 __ save_bcp();
2431
2432 Label resolved;
2433
2434 __ load_resolved_indy_entry(cache, index);
2435 __ movptr(method, Address(cache, in_bytes(ResolvedIndyEntry::method_offset())));
2436
2437 // Compare the method to zero
2438 __ testptr(method, method);
2439 __ jcc(Assembler::notZero, resolved);
2440
2441 Bytecodes::Code code = bytecode();
2442
2443 // Call to the interpreter runtime to resolve invokedynamic
2444 address entry = CAST_FROM_FN_PTR(address, InterpreterRuntime::resolve_from_cache);
2445 __ movl(method, code); // this is essentially Bytecodes::_invokedynamic
2446 __ call_VM(noreg, entry, method);
2447 // Update registers with resolved info
2448 __ load_resolved_indy_entry(cache, index);
2449 __ movptr(method, Address(cache, in_bytes(ResolvedIndyEntry::method_offset())));
2450
2451 #ifdef ASSERT
2452 __ testptr(method, method);
2453 __ jcc(Assembler::notZero, resolved);
2454 __ stop("Should be resolved by now");
2455 #endif // ASSERT
2456 __ bind(resolved);
2457
2458 Label L_no_push;
2459 // Check if there is an appendix
2460 __ load_unsigned_byte(index, Address(cache, in_bytes(ResolvedIndyEntry::flags_offset())));
2461 __ testl(index, (1 << ResolvedIndyEntry::has_appendix_shift));
2462 __ jcc(Assembler::zero, L_no_push);
2463
2464 // Get appendix
2465 __ load_unsigned_short(index, Address(cache, in_bytes(ResolvedIndyEntry::resolved_references_index_offset())));
2466 // Push the appendix as a trailing parameter
2467 // since the parameter_size includes it.
2468 __ load_resolved_reference_at_index(appendix, index);
2469 __ verify_oop(appendix);
2470 __ push(appendix); // push appendix (MethodType, CallSite, etc.)
2471 __ bind(L_no_push);
2472
2473 // compute return type
2474 __ load_unsigned_byte(index, Address(cache, in_bytes(ResolvedIndyEntry::result_type_offset())));
2475 // load return address
2476 {
2477 const address table_addr = (address) Interpreter::invoke_return_entry_table_for(code);
2478 ExternalAddress table(table_addr);
2479 __ lea(rscratch1, table);
2480 __ movptr(index, Address(rscratch1, index, Address::times_ptr));
2481 }
2482
2483 // push return address
2484 __ push(index);
2485 }
2486
2487 void TemplateTable::load_resolved_method_entry_special_or_static(Register cache,
2488 Register method,
2489 Register flags) {
2490 // setup registers
2491 const Register index = rdx;
2492 assert_different_registers(cache, index);
2493 assert_different_registers(method, cache, flags);
2494
2495 // determine constant pool cache field offsets
2496 resolve_cache_and_index_for_method(f1_byte, cache, index);
2497 __ load_unsigned_byte(flags, Address(cache, in_bytes(ResolvedMethodEntry::flags_offset())));
2498 __ movptr(method, Address(cache, in_bytes(ResolvedMethodEntry::method_offset())));
2499 }
2500
2501 void TemplateTable::load_resolved_method_entry_handle(Register cache,
2502 Register method,
2503 Register ref_index,
2504 Register flags) {
2505 // setup registers
2506 const Register index = rdx;
2507 assert_different_registers(cache, index);
2508 assert_different_registers(cache, method, ref_index, flags);
2509
2510 // determine constant pool cache field offsets
2511 resolve_cache_and_index_for_method(f1_byte, cache, index);
2512 __ load_unsigned_byte(flags, Address(cache, in_bytes(ResolvedMethodEntry::flags_offset())));
2513
2514 // Maybe push appendix
2515 Label L_no_push;
2516 __ testl(flags, (1 << ResolvedMethodEntry::has_appendix_shift));
2517 __ jcc(Assembler::zero, L_no_push);
2518 // invokehandle uses an index into the resolved references array
2519 __ load_unsigned_short(ref_index, Address(cache, in_bytes(ResolvedMethodEntry::resolved_references_index_offset())));
2520 // Push the appendix as a trailing parameter.
2521 // This must be done before we get the receiver,
2522 // since the parameter_size includes it.
2523 Register appendix = method;
2524 __ load_resolved_reference_at_index(appendix, ref_index);
2525 __ push(appendix); // push appendix (MethodType, CallSite, etc.)
2526 __ bind(L_no_push);
2527
2528 __ movptr(method, Address(cache, in_bytes(ResolvedMethodEntry::method_offset())));
2529 }
2530
2531 void TemplateTable::load_resolved_method_entry_interface(Register cache,
2532 Register klass,
2533 Register method_or_table_index,
2534 Register flags) {
2535 // setup registers
2536 const Register index = rdx;
2537 assert_different_registers(cache, klass, method_or_table_index, flags);
2538
2539 // determine constant pool cache field offsets
2540 resolve_cache_and_index_for_method(f1_byte, cache, index);
2541 __ load_unsigned_byte(flags, Address(cache, in_bytes(ResolvedMethodEntry::flags_offset())));
2542
2543 // Invokeinterface can behave in different ways:
2544 // If calling a method from java.lang.Object, the forced virtual flag is true so the invocation will
2545 // behave like an invokevirtual call. The state of the virtual final flag will determine whether a method or
2546 // vtable index is placed in the register.
2547 // Otherwise, the registers will be populated with the klass and method.
2548
2549 Label NotVirtual; Label NotVFinal; Label Done;
2550 __ testl(flags, 1 << ResolvedMethodEntry::is_forced_virtual_shift);
2551 __ jcc(Assembler::zero, NotVirtual);
2552 __ testl(flags, (1 << ResolvedMethodEntry::is_vfinal_shift));
2553 __ jcc(Assembler::zero, NotVFinal);
2554 __ movptr(method_or_table_index, Address(cache, in_bytes(ResolvedMethodEntry::method_offset())));
2555 __ jmp(Done);
2556
2557 __ bind(NotVFinal);
2558 __ load_unsigned_short(method_or_table_index, Address(cache, in_bytes(ResolvedMethodEntry::table_index_offset())));
2559 __ jmp(Done);
2560
2561 __ bind(NotVirtual);
2562 __ movptr(method_or_table_index, Address(cache, in_bytes(ResolvedMethodEntry::method_offset())));
2563 __ movptr(klass, Address(cache, in_bytes(ResolvedMethodEntry::klass_offset())));
2564 __ bind(Done);
2565 }
2566
2567 void TemplateTable::load_resolved_method_entry_virtual(Register cache,
2568 Register method_or_table_index,
2569 Register flags) {
2570 // setup registers
2571 const Register index = rdx;
2572 assert_different_registers(index, cache);
2573 assert_different_registers(method_or_table_index, cache, flags);
2574
2575 // determine constant pool cache field offsets
2576 resolve_cache_and_index_for_method(f2_byte, cache, index);
2577 __ load_unsigned_byte(flags, Address(cache, in_bytes(ResolvedMethodEntry::flags_offset())));
2578
2579 // method_or_table_index can either be an itable index or a method depending on the virtual final flag
2580 Label isVFinal; Label Done;
2581 __ testl(flags, (1 << ResolvedMethodEntry::is_vfinal_shift));
2582 __ jcc(Assembler::notZero, isVFinal);
2583 __ load_unsigned_short(method_or_table_index, Address(cache, in_bytes(ResolvedMethodEntry::table_index_offset())));
2584 __ jmp(Done);
2585 __ bind(isVFinal);
2586 __ movptr(method_or_table_index, Address(cache, in_bytes(ResolvedMethodEntry::method_offset())));
2587 __ bind(Done);
2588 }
2589
2590 // The registers cache and index expected to be set before call.
2591 // Correct values of the cache and index registers are preserved.
2592 void TemplateTable::jvmti_post_field_access(Register cache,
2593 Register index,
2594 bool is_static,
2595 bool has_tos) {
2596 if (JvmtiExport::can_post_field_access()) {
2597 // Check to see if a field access watch has been set before we take
2598 // the time to call into the VM.
2599 Label L1;
2600 assert_different_registers(cache, index, rax);
2601 __ mov32(rax, ExternalAddress((address) JvmtiExport::get_field_access_count_addr()));
2602 __ testl(rax,rax);
2603 __ jcc(Assembler::zero, L1);
2604
2605 // cache entry pointer
2606 __ load_field_entry(cache, index);
2607 if (is_static) {
2608 __ xorptr(rax, rax); // null object reference
2609 } else {
2610 __ pop(atos); // Get the object
2611 __ verify_oop(rax);
2612 __ push(atos); // Restore stack state
2613 }
2614 // rax,: object pointer or null
2615 // cache: cache entry pointer
2616 __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_field_access),
2617 rax, cache);
2618
2619 __ load_field_entry(cache, index);
2620 __ bind(L1);
2621 }
2622 }
2623
2624 void TemplateTable::pop_and_check_object(Register r) {
2625 __ pop_ptr(r);
2626 __ null_check(r); // for field access must check obj.
2627 __ verify_oop(r);
2628 }
2629
2630 void TemplateTable::getfield_or_static(int byte_no, bool is_static, RewriteControl rc) {
2631 transition(vtos, vtos);
2632
2633 const Register obj = r9;
2634 const Register cache = rcx;
2635 const Register index = rdx;
2636 const Register off = rbx;
2637 const Register tos_state = rax;
2638 const Register flags = rdx;
2639 const Register bc = c_rarg3; // uses same reg as obj, so don't mix them
2640
2641 resolve_cache_and_index_for_field(byte_no, cache, index);
2642 jvmti_post_field_access(cache, index, is_static, false);
2643 load_resolved_field_entry(obj, cache, tos_state, off, flags, is_static);
2644
2645 const Address field(obj, off, Address::times_1, 0*wordSize);
2646
2647 Label Done, notByte, notBool, notInt, notShort, notChar, notLong, notFloat, notObj, notInlineType;
2648
2649 // Make sure we don't need to mask edx after the above shift
2650 assert(btos == 0, "change code, btos != 0");
2651 __ testl(tos_state, tos_state);
2652 __ jcc(Assembler::notZero, notByte);
2653
2654 // btos
2655 if (!is_static) pop_and_check_object(obj);
2656 __ access_load_at(T_BYTE, IN_HEAP, rax, field, noreg);
2657 __ push(btos);
2658 // Rewrite bytecode to be faster
2659 if (!is_static && rc == may_rewrite) {
2660 patch_bytecode(Bytecodes::_fast_bgetfield, bc, rbx);
2661 }
2662 __ jmp(Done);
2663
2664 __ bind(notByte);
2665 __ cmpl(tos_state, ztos);
2666 __ jcc(Assembler::notEqual, notBool);
2667 if (!is_static) pop_and_check_object(obj);
2668 // ztos (same code as btos)
2669 __ access_load_at(T_BOOLEAN, IN_HEAP, rax, field, noreg);
2670 __ push(ztos);
2671 // Rewrite bytecode to be faster
2672 if (!is_static && rc == may_rewrite) {
2673 // use btos rewriting, no truncating to t/f bit is needed for getfield.
2674 patch_bytecode(Bytecodes::_fast_bgetfield, bc, rbx);
2675 }
2676 __ jmp(Done);
2677
2678 __ bind(notBool);
2679 __ cmpl(tos_state, atos);
2680 __ jcc(Assembler::notEqual, notObj);
2681 // atos
2682 if (!EnableValhalla) {
2683 if (!is_static) pop_and_check_object(obj);
2684 do_oop_load(_masm, field, rax);
2685 __ push(atos);
2686 if (!is_static && rc == may_rewrite) {
2687 patch_bytecode(Bytecodes::_fast_agetfield, bc, rbx);
2688 }
2689 __ jmp(Done);
2690 } else {
2691 if (is_static) {
2692 __ load_heap_oop(rax, field);
2693 __ push(atos);
2694 __ jmp(Done);
2695 } else {
2696 Label is_flat, rewrite_inline;
2697 __ test_field_is_flat(flags, rscratch1, is_flat);
2698 pop_and_check_object(obj);
2699 __ load_heap_oop(rax, field);
2700 __ push(atos);
2701 if (rc == may_rewrite) {
2702 patch_bytecode(Bytecodes::_fast_agetfield, bc, rbx);
2703 }
2704 __ jmp(Done);
2705 __ bind(is_flat);
2706 // field is flat (null-free or nullable with a null-marker)
2707 pop_and_check_object(rax);
2708 __ read_flat_field(rcx, rdx, rbx, rax);
2709 __ verify_oop(rax);
2710 __ push(atos);
2711 __ bind(rewrite_inline);
2712 if (rc == may_rewrite) {
2713 patch_bytecode(Bytecodes::_fast_vgetfield, bc, rbx);
2714 }
2715 __ jmp(Done);
2716 }
2717 }
2718
2719 __ bind(notObj);
2720
2721 if (!is_static) pop_and_check_object(obj);
2722
2723 __ cmpl(tos_state, itos);
2724 __ jcc(Assembler::notEqual, notInt);
2725 // itos
2726 __ access_load_at(T_INT, IN_HEAP, rax, field, noreg);
2727 __ push(itos);
2728 // Rewrite bytecode to be faster
2729 if (!is_static && rc == may_rewrite) {
2730 patch_bytecode(Bytecodes::_fast_igetfield, bc, rbx);
2731 }
2732 __ jmp(Done);
2733
2734 __ bind(notInt);
2735 __ cmpl(tos_state, ctos);
2736 __ jcc(Assembler::notEqual, notChar);
2737 // ctos
2738 __ access_load_at(T_CHAR, IN_HEAP, rax, field, noreg);
2739 __ push(ctos);
2740 // Rewrite bytecode to be faster
2741 if (!is_static && rc == may_rewrite) {
2742 patch_bytecode(Bytecodes::_fast_cgetfield, bc, rbx);
2743 }
2744 __ jmp(Done);
2745
2746 __ bind(notChar);
2747 __ cmpl(tos_state, stos);
2748 __ jcc(Assembler::notEqual, notShort);
2749 // stos
2750 __ access_load_at(T_SHORT, IN_HEAP, rax, field, noreg);
2751 __ push(stos);
2752 // Rewrite bytecode to be faster
2753 if (!is_static && rc == may_rewrite) {
2754 patch_bytecode(Bytecodes::_fast_sgetfield, bc, rbx);
2755 }
2756 __ jmp(Done);
2757
2758 __ bind(notShort);
2759 __ cmpl(tos_state, ltos);
2760 __ jcc(Assembler::notEqual, notLong);
2761 // ltos
2762 // Generate code as if volatile (x86_32). There just aren't enough registers to
2763 // save that information and this code is faster than the test.
2764 __ access_load_at(T_LONG, IN_HEAP | MO_RELAXED, noreg /* ltos */, field, noreg);
2765 __ push(ltos);
2766 // Rewrite bytecode to be faster
2767 if (!is_static && rc == may_rewrite) patch_bytecode(Bytecodes::_fast_lgetfield, bc, rbx);
2768 __ jmp(Done);
2769
2770 __ bind(notLong);
2771 __ cmpl(tos_state, ftos);
2772 __ jcc(Assembler::notEqual, notFloat);
2773 // ftos
2774
2775 __ access_load_at(T_FLOAT, IN_HEAP, noreg /* ftos */, field, noreg);
2776 __ push(ftos);
2777 // Rewrite bytecode to be faster
2778 if (!is_static && rc == may_rewrite) {
2779 patch_bytecode(Bytecodes::_fast_fgetfield, bc, rbx);
2780 }
2781 __ jmp(Done);
2782
2783 __ bind(notFloat);
2784 #ifdef ASSERT
2785 Label notDouble;
2786 __ cmpl(tos_state, dtos);
2787 __ jcc(Assembler::notEqual, notDouble);
2788 #endif
2789 // dtos
2790 // MO_RELAXED: for the case of volatile field, in fact it adds no extra work for the underlying implementation
2791 __ access_load_at(T_DOUBLE, IN_HEAP | MO_RELAXED, noreg /* dtos */, field, noreg);
2792 __ push(dtos);
2793 // Rewrite bytecode to be faster
2794 if (!is_static && rc == may_rewrite) {
2795 patch_bytecode(Bytecodes::_fast_dgetfield, bc, rbx);
2796 }
2797 #ifdef ASSERT
2798 __ jmp(Done);
2799
2800 __ bind(notDouble);
2801 __ stop("Bad state");
2802 #endif
2803
2804 __ bind(Done);
2805 // [jk] not needed currently
2806 // volatile_barrier(Assembler::Membar_mask_bits(Assembler::LoadLoad |
2807 // Assembler::LoadStore));
2808 }
2809
2810 void TemplateTable::getfield(int byte_no) {
2811 getfield_or_static(byte_no, false);
2812 }
2813
2814 void TemplateTable::nofast_getfield(int byte_no) {
2815 getfield_or_static(byte_no, false, may_not_rewrite);
2816 }
2817
2818 void TemplateTable::getstatic(int byte_no) {
2819 getfield_or_static(byte_no, true);
2820 }
2821
2822 // The registers cache and index expected to be set before call.
2823 // The function may destroy various registers, just not the cache and index registers.
2824 void TemplateTable::jvmti_post_field_mod(Register cache, Register index, bool is_static) {
2825 // Cache is rcx and index is rdx
2826 const Register entry = c_rarg2; // ResolvedFieldEntry
2827 const Register obj = c_rarg1; // Object pointer
2828 const Register value = c_rarg3; // JValue object
2829
2830 if (JvmtiExport::can_post_field_modification()) {
2831 // Check to see if a field modification watch has been set before
2832 // we take the time to call into the VM.
2833 Label L1;
2834 assert_different_registers(cache, obj, rax);
2835 __ mov32(rax, ExternalAddress((address)JvmtiExport::get_field_modification_count_addr()));
2836 __ testl(rax, rax);
2837 __ jcc(Assembler::zero, L1);
2838
2839 __ mov(entry, cache);
2840
2841 if (is_static) {
2842 // Life is simple. Null out the object pointer.
2843 __ xorl(obj, obj);
2844
2845 } else {
2846 // Life is harder. The stack holds the value on top, followed by
2847 // the object. We don't know the size of the value, though; it
2848 // could be one or two words depending on its type. As a result,
2849 // we must find the type to determine where the object is.
2850 __ load_unsigned_byte(value, Address(entry, in_bytes(ResolvedFieldEntry::type_offset())));
2851 __ movptr(obj, at_tos_p1()); // initially assume a one word jvalue
2852 __ cmpl(value, ltos);
2853 __ cmovptr(Assembler::equal,
2854 obj, at_tos_p2()); // ltos (two word jvalue)
2855 __ cmpl(value, dtos);
2856 __ cmovptr(Assembler::equal,
2857 obj, at_tos_p2()); // dtos (two word jvalue)
2858 }
2859
2860 // object (tos)
2861 __ mov(value, rsp);
2862 // obj: object pointer set up above (null if static)
2863 // cache: field entry pointer
2864 // value: jvalue object on the stack
2865 __ call_VM(noreg,
2866 CAST_FROM_FN_PTR(address,
2867 InterpreterRuntime::post_field_modification),
2868 obj, entry, value);
2869 // Reload field entry
2870 __ load_field_entry(cache, index);
2871 __ bind(L1);
2872 }
2873 }
2874
2875 void TemplateTable::putfield_or_static(int byte_no, bool is_static, RewriteControl rc) {
2876 transition(vtos, vtos);
2877
2878 const Register obj = rcx;
2879 const Register cache = rcx;
2880 const Register index = rdx;
2881 const Register tos_state = rdx;
2882 const Register off = rbx;
2883 const Register flags = r9;
2884
2885 resolve_cache_and_index_for_field(byte_no, cache, index);
2886 jvmti_post_field_mod(cache, index, is_static);
2887 load_resolved_field_entry(obj, cache, tos_state, off, flags, is_static);
2888
2889 // [jk] not needed currently
2890 // volatile_barrier(Assembler::Membar_mask_bits(Assembler::LoadStore |
2891 // Assembler::StoreStore));
2892
2893 Label notVolatile, Done;
2894
2895 // Check for volatile store
2896 __ movl(rscratch1, flags);
2897 __ andl(rscratch1, (1 << ResolvedFieldEntry::is_volatile_shift));
2898 __ testl(rscratch1, rscratch1);
2899 __ jcc(Assembler::zero, notVolatile);
2900
2901 putfield_or_static_helper(byte_no, is_static, rc, obj, off, tos_state, flags);
2902 volatile_barrier(Assembler::Membar_mask_bits(Assembler::StoreLoad |
2903 Assembler::StoreStore));
2904 __ jmp(Done);
2905 __ bind(notVolatile);
2906
2907 putfield_or_static_helper(byte_no, is_static, rc, obj, off, tos_state, flags);
2908
2909 __ bind(Done);
2910 }
2911
2912 void TemplateTable::putfield_or_static_helper(int byte_no, bool is_static, RewriteControl rc,
2913 Register obj, Register off, Register tos_state, Register flags) {
2914
2915 // field addresses
2916 const Address field(obj, off, Address::times_1, 0*wordSize);
2917
2918 Label notByte, notBool, notInt, notShort, notChar,
2919 notLong, notFloat, notObj, notInlineType;
2920 Label Done;
2921
2922 const Register bc = c_rarg3;
2923
2924 // Test TOS state
2925 __ testl(tos_state, tos_state);
2926 __ jcc(Assembler::notZero, notByte);
2927
2928 // btos
2929 {
2930 __ pop(btos);
2931 if (!is_static) pop_and_check_object(obj);
2932 __ access_store_at(T_BYTE, IN_HEAP, field, rax, noreg, noreg, noreg);
2933 if (!is_static && rc == may_rewrite) {
2934 patch_bytecode(Bytecodes::_fast_bputfield, bc, rbx, true, byte_no);
2935 }
2936 __ jmp(Done);
2937 }
2938
2939 __ bind(notByte);
2940 __ cmpl(tos_state, ztos);
2941 __ jcc(Assembler::notEqual, notBool);
2942
2943 // ztos
2944 {
2945 __ pop(ztos);
2946 if (!is_static) pop_and_check_object(obj);
2947 __ access_store_at(T_BOOLEAN, IN_HEAP, field, rax, noreg, noreg, noreg);
2948 if (!is_static && rc == may_rewrite) {
2949 patch_bytecode(Bytecodes::_fast_zputfield, bc, rbx, true, byte_no);
2950 }
2951 __ jmp(Done);
2952 }
2953
2954 __ bind(notBool);
2955 __ cmpl(tos_state, atos);
2956 __ jcc(Assembler::notEqual, notObj);
2957
2958 // atos
2959 {
2960 if (!EnableValhalla) {
2961 __ pop(atos);
2962 if (!is_static) pop_and_check_object(obj);
2963 // Store into the field
2964 do_oop_store(_masm, field, rax);
2965 if (!is_static && rc == may_rewrite) {
2966 patch_bytecode(Bytecodes::_fast_aputfield, bc, rbx, true, byte_no);
2967 }
2968 __ jmp(Done);
2969 } else {
2970 __ pop(atos);
2971 if (is_static) {
2972 Label is_nullable;
2973 __ test_field_is_not_null_free_inline_type(flags, rscratch1, is_nullable);
2974 __ null_check(rax); // FIXME JDK-8341120
2975 __ bind(is_nullable);
2976 do_oop_store(_masm, field, rax);
2977 __ jmp(Done);
2978 } else {
2979 Label is_flat, null_free_reference, rewrite_inline;
2980 __ test_field_is_flat(flags, rscratch1, is_flat);
2981 __ test_field_is_null_free_inline_type(flags, rscratch1, null_free_reference);
2982 pop_and_check_object(obj);
2983 // Store into the field
2984 do_oop_store(_masm, field, rax);
2985 if (rc == may_rewrite) {
2986 patch_bytecode(Bytecodes::_fast_aputfield, bc, rbx, true, byte_no);
2987 }
2988 __ jmp(Done);
2989 __ bind(null_free_reference);
2990 __ null_check(rax); // FIXME JDK-8341120
2991 pop_and_check_object(obj);
2992 // Store into the field
2993 do_oop_store(_masm, field, rax);
2994 __ jmp(rewrite_inline);
2995 __ bind(is_flat);
2996 pop_and_check_object(rscratch2);
2997 __ write_flat_field(rcx, r8, rscratch1, rscratch2, rbx, rax);
2998 __ bind(rewrite_inline);
2999 if (rc == may_rewrite) {
3000 patch_bytecode(Bytecodes::_fast_vputfield, bc, rbx, true, byte_no);
3001 }
3002 __ jmp(Done);
3003 }
3004 }
3005 }
3006
3007 __ bind(notObj);
3008 __ cmpl(tos_state, itos);
3009 __ jcc(Assembler::notEqual, notInt);
3010
3011 // itos
3012 {
3013 __ pop(itos);
3014 if (!is_static) pop_and_check_object(obj);
3015 __ access_store_at(T_INT, IN_HEAP, field, rax, noreg, noreg, noreg);
3016 if (!is_static && rc == may_rewrite) {
3017 patch_bytecode(Bytecodes::_fast_iputfield, bc, rbx, true, byte_no);
3018 }
3019 __ jmp(Done);
3020 }
3021
3022 __ bind(notInt);
3023 __ cmpl(tos_state, ctos);
3024 __ jcc(Assembler::notEqual, notChar);
3025
3026 // ctos
3027 {
3028 __ pop(ctos);
3029 if (!is_static) pop_and_check_object(obj);
3030 __ access_store_at(T_CHAR, IN_HEAP, field, rax, noreg, noreg, noreg);
3031 if (!is_static && rc == may_rewrite) {
3032 patch_bytecode(Bytecodes::_fast_cputfield, bc, rbx, true, byte_no);
3033 }
3034 __ jmp(Done);
3035 }
3036
3037 __ bind(notChar);
3038 __ cmpl(tos_state, stos);
3039 __ jcc(Assembler::notEqual, notShort);
3040
3041 // stos
3042 {
3043 __ pop(stos);
3044 if (!is_static) pop_and_check_object(obj);
3045 __ access_store_at(T_SHORT, IN_HEAP, field, rax, noreg, noreg, noreg);
3046 if (!is_static && rc == may_rewrite) {
3047 patch_bytecode(Bytecodes::_fast_sputfield, bc, rbx, true, byte_no);
3048 }
3049 __ jmp(Done);
3050 }
3051
3052 __ bind(notShort);
3053 __ cmpl(tos_state, ltos);
3054 __ jcc(Assembler::notEqual, notLong);
3055
3056 // ltos
3057 {
3058 __ pop(ltos);
3059 if (!is_static) pop_and_check_object(obj);
3060 // MO_RELAXED: generate atomic store for the case of volatile field (important for x86_32)
3061 __ access_store_at(T_LONG, IN_HEAP | MO_RELAXED, field, noreg /* ltos*/, noreg, noreg, noreg);
3062 if (!is_static && rc == may_rewrite) {
3063 patch_bytecode(Bytecodes::_fast_lputfield, bc, rbx, true, byte_no);
3064 }
3065 __ jmp(Done);
3066 }
3067
3068 __ bind(notLong);
3069 __ cmpl(tos_state, ftos);
3070 __ jcc(Assembler::notEqual, notFloat);
3071
3072 // ftos
3073 {
3074 __ pop(ftos);
3075 if (!is_static) pop_and_check_object(obj);
3076 __ access_store_at(T_FLOAT, IN_HEAP, field, noreg /* ftos */, noreg, noreg, noreg);
3077 if (!is_static && rc == may_rewrite) {
3078 patch_bytecode(Bytecodes::_fast_fputfield, bc, rbx, true, byte_no);
3079 }
3080 __ jmp(Done);
3081 }
3082
3083 __ bind(notFloat);
3084 #ifdef ASSERT
3085 Label notDouble;
3086 __ cmpl(tos_state, dtos);
3087 __ jcc(Assembler::notEqual, notDouble);
3088 #endif
3089
3090 // dtos
3091 {
3092 __ pop(dtos);
3093 if (!is_static) pop_and_check_object(obj);
3094 // MO_RELAXED: for the case of volatile field, in fact it adds no extra work for the underlying implementation
3095 __ access_store_at(T_DOUBLE, IN_HEAP | MO_RELAXED, field, noreg /* dtos */, noreg, noreg, noreg);
3096 if (!is_static && rc == may_rewrite) {
3097 patch_bytecode(Bytecodes::_fast_dputfield, bc, rbx, true, byte_no);
3098 }
3099 }
3100
3101 #ifdef ASSERT
3102 __ jmp(Done);
3103
3104 __ bind(notDouble);
3105 __ stop("Bad state");
3106 #endif
3107
3108 __ bind(Done);
3109 }
3110
3111 void TemplateTable::putfield(int byte_no) {
3112 putfield_or_static(byte_no, false);
3113 }
3114
3115 void TemplateTable::nofast_putfield(int byte_no) {
3116 putfield_or_static(byte_no, false, may_not_rewrite);
3117 }
3118
3119 void TemplateTable::putstatic(int byte_no) {
3120 putfield_or_static(byte_no, true);
3121 }
3122
3123 void TemplateTable::jvmti_post_fast_field_mod() {
3124
3125 const Register scratch = c_rarg3;
3126
3127 if (JvmtiExport::can_post_field_modification()) {
3128 // Check to see if a field modification watch has been set before
3129 // we take the time to call into the VM.
3130 Label L2;
3131 __ mov32(scratch, ExternalAddress((address)JvmtiExport::get_field_modification_count_addr()));
3132 __ testl(scratch, scratch);
3133 __ jcc(Assembler::zero, L2);
3134 __ pop_ptr(rbx); // copy the object pointer from tos
3135 __ verify_oop(rbx);
3136 __ push_ptr(rbx); // put the object pointer back on tos
3137 // Save tos values before call_VM() clobbers them. Since we have
3138 // to do it for every data type, we use the saved values as the
3139 // jvalue object.
3140 switch (bytecode()) { // load values into the jvalue object
3141 case Bytecodes::_fast_vputfield: //fall through
3142 case Bytecodes::_fast_aputfield: __ push_ptr(rax); break;
3143 case Bytecodes::_fast_bputfield: // fall through
3144 case Bytecodes::_fast_zputfield: // fall through
3145 case Bytecodes::_fast_sputfield: // fall through
3146 case Bytecodes::_fast_cputfield: // fall through
3147 case Bytecodes::_fast_iputfield: __ push_i(rax); break;
3148 case Bytecodes::_fast_dputfield: __ push(dtos); break;
3149 case Bytecodes::_fast_fputfield: __ push(ftos); break;
3150 case Bytecodes::_fast_lputfield: __ push_l(rax); break;
3151
3152 default:
3153 ShouldNotReachHere();
3154 }
3155 __ mov(scratch, rsp); // points to jvalue on the stack
3156 // access constant pool cache entry
3157 __ load_field_entry(c_rarg2, rax);
3158 __ verify_oop(rbx);
3159 // rbx: object pointer copied above
3160 // c_rarg2: cache entry pointer
3161 // c_rarg3: jvalue object on the stack
3162 __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_field_modification), rbx, c_rarg2, c_rarg3);
3163
3164 switch (bytecode()) { // restore tos values
3165 case Bytecodes::_fast_vputfield: // fall through
3166 case Bytecodes::_fast_aputfield: __ pop_ptr(rax); break;
3167 case Bytecodes::_fast_bputfield: // fall through
3168 case Bytecodes::_fast_zputfield: // fall through
3169 case Bytecodes::_fast_sputfield: // fall through
3170 case Bytecodes::_fast_cputfield: // fall through
3171 case Bytecodes::_fast_iputfield: __ pop_i(rax); break;
3172 case Bytecodes::_fast_dputfield: __ pop(dtos); break;
3173 case Bytecodes::_fast_fputfield: __ pop(ftos); break;
3174 case Bytecodes::_fast_lputfield: __ pop_l(rax); break;
3175 default: break;
3176 }
3177 __ bind(L2);
3178 }
3179 }
3180
3181 void TemplateTable::fast_storefield(TosState state) {
3182 transition(state, vtos);
3183
3184 Label notVolatile, Done;
3185
3186 jvmti_post_fast_field_mod();
3187
3188 __ push(rax);
3189 __ load_field_entry(rcx, rax);
3190 load_resolved_field_entry(noreg, rcx, rax, rbx, rdx);
3191 __ pop(rax);
3192 // RBX: field offset, RCX: RAX: TOS, RDX: flags
3193
3194 // Get object from stack
3195 pop_and_check_object(rcx);
3196
3197 // field address
3198 const Address field(rcx, rbx, Address::times_1);
3199
3200 // Check for volatile store
3201 __ movl(rscratch2, rdx); // saving flags for is_flat test
3202 __ andl(rscratch2, (1 << ResolvedFieldEntry::is_volatile_shift));
3203 __ testl(rscratch2, rscratch2);
3204 __ jcc(Assembler::zero, notVolatile);
3205
3206 fast_storefield_helper(field, rax, rdx);
3207 volatile_barrier(Assembler::Membar_mask_bits(Assembler::StoreLoad |
3208 Assembler::StoreStore));
3209 __ jmp(Done);
3210 __ bind(notVolatile);
3211
3212 fast_storefield_helper(field, rax, rdx);
3213
3214 __ bind(Done);
3215 }
3216
3217 void TemplateTable::fast_storefield_helper(Address field, Register rax, Register flags) {
3218
3219 // DANGER: 'field' argument depends on rcx and rbx
3220
3221 // access field
3222 switch (bytecode()) {
3223 case Bytecodes::_fast_vputfield:
3224 {
3225 // Field is either flat (nullable or not) or non-flat and null-free
3226 Label is_flat, done;
3227 __ test_field_is_flat(flags, rscratch1, is_flat);
3228 __ null_check(rax); // FIXME JDK-8341120
3229 do_oop_store(_masm, field, rax);
3230 __ jmp(done);
3231 __ bind(is_flat);
3232 __ load_field_entry(r8, r9);
3233 __ movptr(rscratch2, rcx); // re-shuffle registers because of VM call calling convention
3234 __ write_flat_field(r8, rscratch1, r9, rscratch2, rbx, rax);
3235 __ bind(done);
3236 }
3237 break;
3238 case Bytecodes::_fast_aputfield:
3239 {
3240 do_oop_store(_masm, field, rax);
3241 }
3242 break;
3243 case Bytecodes::_fast_lputfield:
3244 __ access_store_at(T_LONG, IN_HEAP, field, noreg /* ltos */, noreg, noreg, noreg);
3245 break;
3246 case Bytecodes::_fast_iputfield:
3247 __ access_store_at(T_INT, IN_HEAP, field, rax, noreg, noreg, noreg);
3248 break;
3249 case Bytecodes::_fast_zputfield:
3250 __ access_store_at(T_BOOLEAN, IN_HEAP, field, rax, noreg, noreg, noreg);
3251 break;
3252 case Bytecodes::_fast_bputfield:
3253 __ access_store_at(T_BYTE, IN_HEAP, field, rax, noreg, noreg, noreg);
3254 break;
3255 case Bytecodes::_fast_sputfield:
3256 __ access_store_at(T_SHORT, IN_HEAP, field, rax, noreg, noreg, noreg);
3257 break;
3258 case Bytecodes::_fast_cputfield:
3259 __ access_store_at(T_CHAR, IN_HEAP, field, rax, noreg, noreg, noreg);
3260 break;
3261 case Bytecodes::_fast_fputfield:
3262 __ access_store_at(T_FLOAT, IN_HEAP, field, noreg /* ftos*/, noreg, noreg, noreg);
3263 break;
3264 case Bytecodes::_fast_dputfield:
3265 __ access_store_at(T_DOUBLE, IN_HEAP, field, noreg /* dtos*/, noreg, noreg, noreg);
3266 break;
3267 default:
3268 ShouldNotReachHere();
3269 }
3270 }
3271
3272 void TemplateTable::fast_accessfield(TosState state) {
3273 transition(atos, state);
3274
3275 // Do the JVMTI work here to avoid disturbing the register state below
3276 if (JvmtiExport::can_post_field_access()) {
3277 // Check to see if a field access watch has been set before we
3278 // take the time to call into the VM.
3279 Label L1;
3280 __ mov32(rcx, ExternalAddress((address) JvmtiExport::get_field_access_count_addr()));
3281 __ testl(rcx, rcx);
3282 __ jcc(Assembler::zero, L1);
3283 // access constant pool cache entry
3284 __ load_field_entry(c_rarg2, rcx);
3285 __ verify_oop(rax);
3286 __ push_ptr(rax); // save object pointer before call_VM() clobbers it
3287 __ mov(c_rarg1, rax);
3288 // c_rarg1: object pointer copied above
3289 // c_rarg2: cache entry pointer
3290 __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_field_access), c_rarg1, c_rarg2);
3291 __ pop_ptr(rax); // restore object pointer
3292 __ bind(L1);
3293 }
3294
3295 // access constant pool cache
3296 __ load_field_entry(rcx, rbx);
3297 __ load_sized_value(rdx, Address(rcx, in_bytes(ResolvedFieldEntry::field_offset_offset())), sizeof(int), true /*is_signed*/);
3298
3299 // rax: object
3300 __ verify_oop(rax);
3301 __ null_check(rax);
3302 Address field(rax, rdx, Address::times_1);
3303
3304 // access field
3305 switch (bytecode()) {
3306 case Bytecodes::_fast_vgetfield:
3307 __ read_flat_field(rcx, rdx, rbx, rax);
3308 __ verify_oop(rax);
3309 break;
3310 case Bytecodes::_fast_agetfield:
3311 do_oop_load(_masm, field, rax);
3312 __ verify_oop(rax);
3313 break;
3314 case Bytecodes::_fast_lgetfield:
3315 __ access_load_at(T_LONG, IN_HEAP, noreg /* ltos */, field, noreg);
3316 break;
3317 case Bytecodes::_fast_igetfield:
3318 __ access_load_at(T_INT, IN_HEAP, rax, field, noreg);
3319 break;
3320 case Bytecodes::_fast_bgetfield:
3321 __ access_load_at(T_BYTE, IN_HEAP, rax, field, noreg);
3322 break;
3323 case Bytecodes::_fast_sgetfield:
3324 __ access_load_at(T_SHORT, IN_HEAP, rax, field, noreg);
3325 break;
3326 case Bytecodes::_fast_cgetfield:
3327 __ access_load_at(T_CHAR, IN_HEAP, rax, field, noreg);
3328 break;
3329 case Bytecodes::_fast_fgetfield:
3330 __ access_load_at(T_FLOAT, IN_HEAP, noreg /* ftos */, field, noreg);
3331 break;
3332 case Bytecodes::_fast_dgetfield:
3333 __ access_load_at(T_DOUBLE, IN_HEAP, noreg /* dtos */, field, noreg);
3334 break;
3335 default:
3336 ShouldNotReachHere();
3337 }
3338 // [jk] not needed currently
3339 // Label notVolatile;
3340 // __ testl(rdx, rdx);
3341 // __ jcc(Assembler::zero, notVolatile);
3342 // __ membar(Assembler::LoadLoad);
3343 // __ bind(notVolatile);
3344 }
3345
3346 void TemplateTable::fast_xaccess(TosState state) {
3347 transition(vtos, state);
3348
3349 // get receiver
3350 __ movptr(rax, aaddress(0));
3351 // access constant pool cache
3352 __ load_field_entry(rcx, rdx, 2);
3353 __ load_sized_value(rbx, Address(rcx, in_bytes(ResolvedFieldEntry::field_offset_offset())), sizeof(int), true /*is_signed*/);
3354
3355 // make sure exception is reported in correct bcp range (getfield is
3356 // next instruction)
3357 __ increment(rbcp);
3358 __ null_check(rax);
3359 const Address field = Address(rax, rbx, Address::times_1, 0*wordSize);
3360 switch (state) {
3361 case itos:
3362 __ access_load_at(T_INT, IN_HEAP, rax, field, noreg);
3363 break;
3364 case atos:
3365 do_oop_load(_masm, field, rax);
3366 __ verify_oop(rax);
3367 break;
3368 case ftos:
3369 __ access_load_at(T_FLOAT, IN_HEAP, noreg /* ftos */, field, noreg);
3370 break;
3371 default:
3372 ShouldNotReachHere();
3373 }
3374
3375 // [jk] not needed currently
3376 // Label notVolatile;
3377 // __ movl(rdx, Address(rcx, rdx, Address::times_8,
3378 // in_bytes(ConstantPoolCache::base_offset() +
3379 // ConstantPoolCacheEntry::flags_offset())));
3380 // __ shrl(rdx, ConstantPoolCacheEntry::is_volatile_shift);
3381 // __ testl(rdx, 0x1);
3382 // __ jcc(Assembler::zero, notVolatile);
3383 // __ membar(Assembler::LoadLoad);
3384 // __ bind(notVolatile);
3385
3386 __ decrement(rbcp);
3387 }
3388
3389 //-----------------------------------------------------------------------------
3390 // Calls
3391
3392 void TemplateTable::prepare_invoke(Register cache, Register recv, Register flags) {
3393 // determine flags
3394 const Bytecodes::Code code = bytecode();
3395 const bool load_receiver = (code != Bytecodes::_invokestatic) && (code != Bytecodes::_invokedynamic);
3396 assert_different_registers(recv, flags);
3397
3398 // save 'interpreter return address'
3399 __ save_bcp();
3400
3401 // Save flags and load TOS
3402 __ movl(rbcp, flags);
3403 __ load_unsigned_byte(flags, Address(cache, in_bytes(ResolvedMethodEntry::type_offset())));
3404
3405 // load receiver if needed (after appendix is pushed so parameter size is correct)
3406 // Note: no return address pushed yet
3407 if (load_receiver) {
3408 __ load_unsigned_short(recv, Address(cache, in_bytes(ResolvedMethodEntry::num_parameters_offset())));
3409 const int no_return_pc_pushed_yet = -1; // argument slot correction before we push return address
3410 const int receiver_is_at_end = -1; // back off one slot to get receiver
3411 Address recv_addr = __ argument_address(recv, no_return_pc_pushed_yet + receiver_is_at_end);
3412 __ movptr(recv, recv_addr);
3413 __ verify_oop(recv);
3414 }
3415
3416 // load return address
3417 {
3418 const address table_addr = (address) Interpreter::invoke_return_entry_table_for(code);
3419 ExternalAddress table(table_addr);
3420 __ lea(rscratch1, table);
3421 __ movptr(flags, Address(rscratch1, flags, Address::times_ptr));
3422 }
3423
3424 // push return address
3425 __ push(flags);
3426
3427 // Restore flags value from the constant pool cache entry, and restore rsi
3428 // for later null checks. r13 is the bytecode pointer
3429 __ movl(flags, rbcp);
3430 __ restore_bcp();
3431 }
3432
3433 void TemplateTable::invokevirtual_helper(Register index,
3434 Register recv,
3435 Register flags) {
3436 // Uses temporary registers rax, rdx
3437 assert_different_registers(index, recv, rax, rdx);
3438 assert(index == rbx, "");
3439 assert(recv == rcx, "");
3440
3441 // Test for an invoke of a final method
3442 Label notFinal;
3443 __ movl(rax, flags);
3444 __ andl(rax, (1 << ResolvedMethodEntry::is_vfinal_shift));
3445 __ jcc(Assembler::zero, notFinal);
3446
3447 const Register method = index; // method must be rbx
3448 assert(method == rbx,
3449 "Method* must be rbx for interpreter calling convention");
3450
3451 // do the call - the index is actually the method to call
3452 // that is, f2 is a vtable index if !is_vfinal, else f2 is a Method*
3453
3454 // It's final, need a null check here!
3455 __ null_check(recv);
3456
3457 // profile this call
3458 __ profile_final_call(rax);
3459 __ profile_arguments_type(rax, method, rbcp, true);
3460
3461 __ jump_from_interpreted(method, rax);
3462
3463 __ bind(notFinal);
3464
3465 // get receiver klass
3466 __ load_klass(rax, recv, rscratch1);
3467
3468 // profile this call
3469 __ profile_virtual_call(rax, rlocals, rdx);
3470 // get target Method* & entry point
3471 __ lookup_virtual_method(rax, index, method);
3472
3473 __ profile_arguments_type(rdx, method, rbcp, true);
3474 __ jump_from_interpreted(method, rdx);
3475 }
3476
3477 void TemplateTable::invokevirtual(int byte_no) {
3478 transition(vtos, vtos);
3479 assert(byte_no == f2_byte, "use this argument");
3480
3481 load_resolved_method_entry_virtual(rcx, // ResolvedMethodEntry*
3482 rbx, // Method or itable index
3483 rdx); // Flags
3484 prepare_invoke(rcx, // ResolvedMethodEntry*
3485 rcx, // Receiver
3486 rdx); // flags
3487
3488 // rbx: index
3489 // rcx: receiver
3490 // rdx: flags
3491 invokevirtual_helper(rbx, rcx, rdx);
3492 }
3493
3494 void TemplateTable::invokespecial(int byte_no) {
3495 transition(vtos, vtos);
3496 assert(byte_no == f1_byte, "use this argument");
3497
3498 load_resolved_method_entry_special_or_static(rcx, // ResolvedMethodEntry*
3499 rbx, // Method*
3500 rdx); // flags
3501 prepare_invoke(rcx,
3502 rcx, // get receiver also for null check
3503 rdx); // flags
3504
3505 __ verify_oop(rcx);
3506 __ null_check(rcx);
3507 // do the call
3508 __ profile_call(rax);
3509 __ profile_arguments_type(rax, rbx, rbcp, false);
3510 __ jump_from_interpreted(rbx, rax);
3511 }
3512
3513 void TemplateTable::invokestatic(int byte_no) {
3514 transition(vtos, vtos);
3515 assert(byte_no == f1_byte, "use this argument");
3516
3517 load_resolved_method_entry_special_or_static(rcx, // ResolvedMethodEntry*
3518 rbx, // Method*
3519 rdx // flags
3520 );
3521 prepare_invoke(rcx, rcx, rdx); // cache and flags
3522
3523 // do the call
3524 __ profile_call(rax);
3525 __ profile_arguments_type(rax, rbx, rbcp, false);
3526 __ jump_from_interpreted(rbx, rax);
3527 }
3528
3529
3530 void TemplateTable::fast_invokevfinal(int byte_no) {
3531 transition(vtos, vtos);
3532 assert(byte_no == f2_byte, "use this argument");
3533 __ stop("fast_invokevfinal not used on x86");
3534 }
3535
3536
3537 void TemplateTable::invokeinterface(int byte_no) {
3538 transition(vtos, vtos);
3539 assert(byte_no == f1_byte, "use this argument");
3540
3541 load_resolved_method_entry_interface(rcx, // ResolvedMethodEntry*
3542 rax, // Klass*
3543 rbx, // Method* or itable/vtable index
3544 rdx); // flags
3545 prepare_invoke(rcx, rcx, rdx); // receiver, flags
3546
3547 // First check for Object case, then private interface method,
3548 // then regular interface method.
3549
3550 // Special case of invokeinterface called for virtual method of
3551 // java.lang.Object. See cpCache.cpp for details.
3552 Label notObjectMethod;
3553 __ movl(rlocals, rdx);
3554 __ andl(rlocals, (1 << ResolvedMethodEntry::is_forced_virtual_shift));
3555 __ jcc(Assembler::zero, notObjectMethod);
3556
3557 invokevirtual_helper(rbx, rcx, rdx);
3558 // no return from above
3559 __ bind(notObjectMethod);
3560
3561 Label no_such_interface; // for receiver subtype check
3562 Register recvKlass; // used for exception processing
3563
3564 // Check for private method invocation - indicated by vfinal
3565 Label notVFinal;
3566 __ movl(rlocals, rdx);
3567 __ andl(rlocals, (1 << ResolvedMethodEntry::is_vfinal_shift));
3568 __ jcc(Assembler::zero, notVFinal);
3569
3570 // Get receiver klass into rlocals - also a null check
3571 __ load_klass(rlocals, rcx, rscratch1);
3572
3573 Label subtype;
3574 __ check_klass_subtype(rlocals, rax, rbcp, subtype);
3575 // If we get here the typecheck failed
3576 recvKlass = rdx;
3577 __ mov(recvKlass, rlocals); // shuffle receiver class for exception use
3578 __ jmp(no_such_interface);
3579
3580 __ bind(subtype);
3581
3582 // do the call - rbx is actually the method to call
3583
3584 __ profile_final_call(rdx);
3585 __ profile_arguments_type(rdx, rbx, rbcp, true);
3586
3587 __ jump_from_interpreted(rbx, rdx);
3588 // no return from above
3589 __ bind(notVFinal);
3590
3591 // Get receiver klass into rdx - also a null check
3592 __ restore_locals(); // restore r14
3593 __ load_klass(rdx, rcx, rscratch1);
3594
3595 Label no_such_method;
3596
3597 // Preserve method for throw_AbstractMethodErrorVerbose.
3598 __ mov(rcx, rbx);
3599 // Receiver subtype check against REFC.
3600 // Superklass in rax. Subklass in rdx. Blows rcx, rdi.
3601 __ lookup_interface_method(// inputs: rec. class, interface, itable index
3602 rdx, rax, noreg,
3603 // outputs: scan temp. reg, scan temp. reg
3604 rbcp, rlocals,
3605 no_such_interface,
3606 /*return_method=*/false);
3607
3608 // profile this call
3609 __ restore_bcp(); // rbcp was destroyed by receiver type check
3610 __ profile_virtual_call(rdx, rbcp, rlocals);
3611
3612 // Get declaring interface class from method, and itable index
3613 __ load_method_holder(rax, rbx);
3614 __ movl(rbx, Address(rbx, Method::itable_index_offset()));
3615 __ subl(rbx, Method::itable_index_max);
3616 __ negl(rbx);
3617
3618 // Preserve recvKlass for throw_AbstractMethodErrorVerbose.
3619 __ mov(rlocals, rdx);
3620 __ lookup_interface_method(// inputs: rec. class, interface, itable index
3621 rlocals, rax, rbx,
3622 // outputs: method, scan temp. reg
3623 rbx, rbcp,
3624 no_such_interface);
3625
3626 // rbx: Method* to call
3627 // rcx: receiver
3628 // Check for abstract method error
3629 // Note: This should be done more efficiently via a throw_abstract_method_error
3630 // interpreter entry point and a conditional jump to it in case of a null
3631 // method.
3632 __ testptr(rbx, rbx);
3633 __ jcc(Assembler::zero, no_such_method);
3634
3635 __ profile_arguments_type(rdx, rbx, rbcp, true);
3636
3637 // do the call
3638 // rcx: receiver
3639 // rbx,: Method*
3640 __ jump_from_interpreted(rbx, rdx);
3641 __ should_not_reach_here();
3642
3643 // exception handling code follows...
3644 // note: must restore interpreter registers to canonical
3645 // state for exception handling to work correctly!
3646
3647 __ bind(no_such_method);
3648 // throw exception
3649 __ pop(rbx); // pop return address (pushed by prepare_invoke)
3650 __ restore_bcp(); // rbcp must be correct for exception handler (was destroyed)
3651 __ restore_locals(); // make sure locals pointer is correct as well (was destroyed)
3652 // Pass arguments for generating a verbose error message.
3653 recvKlass = c_rarg1;
3654 Register method = c_rarg2;
3655 if (recvKlass != rdx) { __ movq(recvKlass, rdx); }
3656 if (method != rcx) { __ movq(method, rcx); }
3657 __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_AbstractMethodErrorVerbose),
3658 recvKlass, method);
3659 // The call_VM checks for exception, so we should never return here.
3660 __ should_not_reach_here();
3661
3662 __ bind(no_such_interface);
3663 // throw exception
3664 __ pop(rbx); // pop return address (pushed by prepare_invoke)
3665 __ restore_bcp(); // rbcp must be correct for exception handler (was destroyed)
3666 __ restore_locals(); // make sure locals pointer is correct as well (was destroyed)
3667 // Pass arguments for generating a verbose error message.
3668 if (recvKlass != rdx) {
3669 __ movq(recvKlass, rdx);
3670 }
3671 __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_IncompatibleClassChangeErrorVerbose),
3672 recvKlass, rax);
3673 // the call_VM checks for exception, so we should never return here.
3674 __ should_not_reach_here();
3675 }
3676
3677 void TemplateTable::invokehandle(int byte_no) {
3678 transition(vtos, vtos);
3679 assert(byte_no == f1_byte, "use this argument");
3680 const Register rbx_method = rbx;
3681 const Register rax_mtype = rax;
3682 const Register rcx_recv = rcx;
3683 const Register rdx_flags = rdx;
3684
3685 load_resolved_method_entry_handle(rcx, rbx_method, rax_mtype, rdx_flags);
3686 prepare_invoke(rcx, rcx_recv, rdx_flags);
3687
3688 __ verify_method_ptr(rbx_method);
3689 __ verify_oop(rcx_recv);
3690 __ null_check(rcx_recv);
3691
3692 // rax: MethodType object (from cpool->resolved_references[f1], if necessary)
3693 // rbx: MH.invokeExact_MT method
3694
3695 // Note: rax_mtype is already pushed (if necessary)
3696
3697 // FIXME: profile the LambdaForm also
3698 __ profile_final_call(rax);
3699 __ profile_arguments_type(rdx, rbx_method, rbcp, true);
3700
3701 __ jump_from_interpreted(rbx_method, rdx);
3702 }
3703
3704 void TemplateTable::invokedynamic(int byte_no) {
3705 transition(vtos, vtos);
3706 assert(byte_no == f1_byte, "use this argument");
3707
3708 const Register rbx_method = rbx;
3709 const Register rax_callsite = rax;
3710
3711 load_invokedynamic_entry(rbx_method);
3712 // rax: CallSite object (from cpool->resolved_references[])
3713 // rbx: MH.linkToCallSite method
3714
3715 // Note: rax_callsite is already pushed
3716
3717 // %%% should make a type profile for any invokedynamic that takes a ref argument
3718 // profile this call
3719 __ profile_call(rbcp);
3720 __ profile_arguments_type(rdx, rbx_method, rbcp, false);
3721
3722 __ verify_oop(rax_callsite);
3723
3724 __ jump_from_interpreted(rbx_method, rdx);
3725 }
3726
3727 //-----------------------------------------------------------------------------
3728 // Allocation
3729
3730 void TemplateTable::_new() {
3731 transition(vtos, atos);
3732 __ get_unsigned_2_byte_index_at_bcp(rdx, 1);
3733 Label slow_case;
3734 Label done;
3735
3736 __ get_cpool_and_tags(rcx, rax);
3737
3738 // Make sure the class we're about to instantiate has been resolved.
3739 // This is done before loading InstanceKlass to be consistent with the order
3740 // how Constant Pool is updated (see ConstantPool::klass_at_put)
3741 const int tags_offset = Array<u1>::base_offset_in_bytes();
3742 __ cmpb(Address(rax, rdx, Address::times_1, tags_offset), JVM_CONSTANT_Class);
3743 __ jcc(Assembler::notEqual, slow_case);
3744
3745 // get InstanceKlass
3746 __ load_resolved_klass_at_index(rcx, rcx, rdx);
3747
3748 // make sure klass is initialized
3749 // init_state needs acquire, but x86 is TSO, and so we are already good.
3750 assert(VM_Version::supports_fast_class_init_checks(), "must support fast class initialization checks");
3751 __ clinit_barrier(rcx, nullptr /*L_fast_path*/, &slow_case);
3752
3753 __ allocate_instance(rcx, rax, rdx, rbx, true, slow_case);
3754 __ jmp(done);
3755
3756 // slow case
3757 __ bind(slow_case);
3758
3759 __ get_constant_pool(c_rarg1);
3760 __ get_unsigned_2_byte_index_at_bcp(c_rarg2, 1);
3761 call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::_new), c_rarg1, c_rarg2);
3762 __ verify_oop(rax);
3763
3764 // continue
3765 __ bind(done);
3766 }
3767
3768 void TemplateTable::newarray() {
3769 transition(itos, atos);
3770 __ load_unsigned_byte(c_rarg1, at_bcp(1));
3771 call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::newarray),
3772 c_rarg1, rax);
3773 }
3774
3775 void TemplateTable::anewarray() {
3776 transition(itos, atos);
3777
3778 __ get_unsigned_2_byte_index_at_bcp(c_rarg2, 1);
3779 __ get_constant_pool(c_rarg1);
3780 call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::anewarray),
3781 c_rarg1, c_rarg2, rax);
3782 }
3783
3784 void TemplateTable::arraylength() {
3785 transition(atos, itos);
3786 __ movl(rax, Address(rax, arrayOopDesc::length_offset_in_bytes()));
3787 }
3788
3789 void TemplateTable::checkcast() {
3790 transition(atos, atos);
3791 Label done, is_null, ok_is_subtype, quicked, resolved;
3792 __ testptr(rax, rax); // object is in rax
3793 __ jcc(Assembler::zero, is_null);
3794
3795 // Get cpool & tags index
3796 __ get_cpool_and_tags(rcx, rdx); // rcx=cpool, rdx=tags array
3797 __ get_unsigned_2_byte_index_at_bcp(rbx, 1); // rbx=index
3798 // See if bytecode has already been quicked
3799 __ movzbl(rdx, Address(rdx, rbx,
3800 Address::times_1,
3801 Array<u1>::base_offset_in_bytes()));
3802 __ cmpl(rdx, JVM_CONSTANT_Class);
3803 __ jcc(Assembler::equal, quicked);
3804 __ push(atos); // save receiver for result, and for GC
3805 call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::quicken_io_cc));
3806
3807 __ get_vm_result_metadata(rax);
3808
3809 __ pop_ptr(rdx); // restore receiver
3810 __ jmpb(resolved);
3811
3812 // Get superklass in rax and subklass in rbx
3813 __ bind(quicked);
3814 __ mov(rdx, rax); // Save object in rdx; rax needed for subtype check
3815 __ load_resolved_klass_at_index(rax, rcx, rbx);
3816
3817 __ bind(resolved);
3818 __ load_klass(rbx, rdx, rscratch1);
3819
3820 // Generate subtype check. Blows rcx, rdi. Object in rdx.
3821 // Superklass in rax. Subklass in rbx.
3822 __ gen_subtype_check(rbx, ok_is_subtype);
3823
3824 // Come here on failure
3825 __ push_ptr(rdx);
3826 // object is at TOS
3827 __ jump(RuntimeAddress(Interpreter::_throw_ClassCastException_entry));
3828
3829 // Come here on success
3830 __ bind(ok_is_subtype);
3831 __ mov(rax, rdx); // Restore object in rdx
3832 __ jmp(done);
3833
3834 __ bind(is_null);
3835
3836 // Collect counts on whether this check-cast sees nulls a lot or not.
3837 if (ProfileInterpreter) {
3838 __ profile_null_seen(rcx);
3839 }
3840
3841 __ bind(done);
3842 }
3843
3844 void TemplateTable::instanceof() {
3845 transition(atos, itos);
3846 Label done, is_null, ok_is_subtype, quicked, resolved;
3847 __ testptr(rax, rax);
3848 __ jcc(Assembler::zero, is_null);
3849
3850 // Get cpool & tags index
3851 __ get_cpool_and_tags(rcx, rdx); // rcx=cpool, rdx=tags array
3852 __ get_unsigned_2_byte_index_at_bcp(rbx, 1); // rbx=index
3853 // See if bytecode has already been quicked
3854 __ movzbl(rdx, Address(rdx, rbx,
3855 Address::times_1,
3856 Array<u1>::base_offset_in_bytes()));
3857 __ cmpl(rdx, JVM_CONSTANT_Class);
3858 __ jcc(Assembler::equal, quicked);
3859
3860 __ push(atos); // save receiver for result, and for GC
3861 call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::quicken_io_cc));
3862
3863 __ get_vm_result_metadata(rax);
3864
3865 __ pop_ptr(rdx); // restore receiver
3866 __ verify_oop(rdx);
3867 __ load_klass(rdx, rdx, rscratch1);
3868 __ jmpb(resolved);
3869
3870 // Get superklass in rax and subklass in rdx
3871 __ bind(quicked);
3872 __ load_klass(rdx, rax, rscratch1);
3873 __ load_resolved_klass_at_index(rax, rcx, rbx);
3874
3875 __ bind(resolved);
3876
3877 // Generate subtype check. Blows rcx, rdi
3878 // Superklass in rax. Subklass in rdx.
3879 __ gen_subtype_check(rdx, ok_is_subtype);
3880
3881 // Come here on failure
3882 __ xorl(rax, rax);
3883 __ jmpb(done);
3884 // Come here on success
3885 __ bind(ok_is_subtype);
3886 __ movl(rax, 1);
3887
3888 // Collect counts on whether this test sees nulls a lot or not.
3889 if (ProfileInterpreter) {
3890 __ jmp(done);
3891 __ bind(is_null);
3892 __ profile_null_seen(rcx);
3893 } else {
3894 __ bind(is_null); // same as 'done'
3895 }
3896 __ bind(done);
3897 // rax = 0: obj == nullptr or obj is not an instanceof the specified klass
3898 // rax = 1: obj != nullptr and obj is an instanceof the specified klass
3899 }
3900
3901 //----------------------------------------------------------------------------------------------------
3902 // Breakpoints
3903 void TemplateTable::_breakpoint() {
3904 // Note: We get here even if we are single stepping..
3905 // jbug insists on setting breakpoints at every bytecode
3906 // even if we are in single step mode.
3907
3908 transition(vtos, vtos);
3909
3910 // get the unpatched byte code
3911 __ get_method(c_rarg1);
3912 __ call_VM(noreg,
3913 CAST_FROM_FN_PTR(address,
3914 InterpreterRuntime::get_original_bytecode_at),
3915 c_rarg1, rbcp);
3916 __ mov(rbx, rax); // why?
3917
3918 // post the breakpoint event
3919 __ get_method(c_rarg1);
3920 __ call_VM(noreg,
3921 CAST_FROM_FN_PTR(address, InterpreterRuntime::_breakpoint),
3922 c_rarg1, rbcp);
3923
3924 // complete the execution of original bytecode
3925 __ dispatch_only_normal(vtos);
3926 }
3927
3928 //-----------------------------------------------------------------------------
3929 // Exceptions
3930
3931 void TemplateTable::athrow() {
3932 transition(atos, vtos);
3933 __ null_check(rax);
3934 __ jump(RuntimeAddress(Interpreter::throw_exception_entry()));
3935 }
3936
3937 //-----------------------------------------------------------------------------
3938 // Synchronization
3939 //
3940 // Note: monitorenter & exit are symmetric routines; which is reflected
3941 // in the assembly code structure as well
3942 //
3943 // Stack layout:
3944 //
3945 // [expressions ] <--- rsp = expression stack top
3946 // ..
3947 // [expressions ]
3948 // [monitor entry] <--- monitor block top = expression stack bot
3949 // ..
3950 // [monitor entry]
3951 // [frame data ] <--- monitor block bot
3952 // ...
3953 // [saved rbp ] <--- rbp
3954 void TemplateTable::monitorenter() {
3955 transition(atos, vtos);
3956
3957 // check for null object
3958 __ null_check(rax);
3959
3960 Label is_inline_type;
3961 __ movptr(rbx, Address(rax, oopDesc::mark_offset_in_bytes()));
3962 __ test_markword_is_inline_type(rbx, is_inline_type);
3963
3964 const Address monitor_block_top(
3965 rbp, frame::interpreter_frame_monitor_block_top_offset * wordSize);
3966 const Address monitor_block_bot(
3967 rbp, frame::interpreter_frame_initial_sp_offset * wordSize);
3968 const int entry_size = frame::interpreter_frame_monitor_size_in_bytes();
3969
3970 Label allocated;
3971
3972 Register rtop = c_rarg3;
3973 Register rbot = c_rarg2;
3974 Register rmon = c_rarg1;
3975
3976 // initialize entry pointer
3977 __ xorl(rmon, rmon); // points to free slot or null
3978
3979 // find a free slot in the monitor block (result in rmon)
3980 {
3981 Label entry, loop, exit;
3982 __ movptr(rtop, monitor_block_top); // derelativize pointer
3983 __ lea(rtop, Address(rbp, rtop, Address::times_ptr));
3984 // rtop points to current entry, starting with top-most entry
3985
3986 __ lea(rbot, monitor_block_bot); // points to word before bottom
3987 // of monitor block
3988 __ jmpb(entry);
3989
3990 __ bind(loop);
3991 // check if current entry is used
3992 __ cmpptr(Address(rtop, BasicObjectLock::obj_offset()), NULL_WORD);
3993 // if not used then remember entry in rmon
3994 __ cmovptr(Assembler::equal, rmon, rtop); // cmov => cmovptr
3995 // check if current entry is for same object
3996 __ cmpptr(rax, Address(rtop, BasicObjectLock::obj_offset()));
3997 // if same object then stop searching
3998 __ jccb(Assembler::equal, exit);
3999 // otherwise advance to next entry
4000 __ addptr(rtop, entry_size);
4001 __ bind(entry);
4002 // check if bottom reached
4003 __ cmpptr(rtop, rbot);
4004 // if not at bottom then check this entry
4005 __ jcc(Assembler::notEqual, loop);
4006 __ bind(exit);
4007 }
4008
4009 __ testptr(rmon, rmon); // check if a slot has been found
4010 __ jcc(Assembler::notZero, allocated); // if found, continue with that one
4011
4012 // allocate one if there's no free slot
4013 {
4014 Label entry, loop;
4015 // 1. compute new pointers // rsp: old expression stack top
4016 __ movptr(rmon, monitor_block_bot); // rmon: old expression stack bottom
4017 __ lea(rmon, Address(rbp, rmon, Address::times_ptr));
4018 __ subptr(rsp, entry_size); // move expression stack top
4019 __ subptr(rmon, entry_size); // move expression stack bottom
4020 __ mov(rtop, rsp); // set start value for copy loop
4021 __ subptr(monitor_block_bot, entry_size / wordSize); // set new monitor block bottom
4022 __ jmp(entry);
4023 // 2. move expression stack contents
4024 __ bind(loop);
4025 __ movptr(rbot, Address(rtop, entry_size)); // load expression stack
4026 // word from old location
4027 __ movptr(Address(rtop, 0), rbot); // and store it at new location
4028 __ addptr(rtop, wordSize); // advance to next word
4029 __ bind(entry);
4030 __ cmpptr(rtop, rmon); // check if bottom reached
4031 __ jcc(Assembler::notEqual, loop); // if not at bottom then
4032 // copy next word
4033 }
4034
4035 // call run-time routine
4036 // rmon: points to monitor entry
4037 __ bind(allocated);
4038
4039 // Increment bcp to point to the next bytecode, so exception
4040 // handling for async. exceptions work correctly.
4041 // The object has already been popped from the stack, so the
4042 // expression stack looks correct.
4043 __ increment(rbcp);
4044
4045 // store object
4046 __ movptr(Address(rmon, BasicObjectLock::obj_offset()), rax);
4047 __ lock_object(rmon);
4048
4049 // check to make sure this monitor doesn't cause stack overflow after locking
4050 __ save_bcp(); // in case of exception
4051 __ generate_stack_overflow_check(0);
4052
4053 // The bcp has already been incremented. Just need to dispatch to
4054 // next instruction.
4055 __ dispatch_next(vtos);
4056
4057 __ bind(is_inline_type);
4058 __ call_VM(noreg, CAST_FROM_FN_PTR(address,
4059 InterpreterRuntime::throw_identity_exception), rax);
4060 __ should_not_reach_here();
4061 }
4062
4063 void TemplateTable::monitorexit() {
4064 transition(atos, vtos);
4065
4066 // check for null object
4067 __ null_check(rax);
4068
4069 const int is_inline_type_mask = markWord::inline_type_pattern;
4070 Label has_identity;
4071 __ movptr(rbx, Address(rax, oopDesc::mark_offset_in_bytes()));
4072 __ andptr(rbx, is_inline_type_mask);
4073 __ cmpl(rbx, is_inline_type_mask);
4074 __ jcc(Assembler::notEqual, has_identity);
4075 __ call_VM(noreg, CAST_FROM_FN_PTR(address,
4076 InterpreterRuntime::throw_illegal_monitor_state_exception));
4077 __ should_not_reach_here();
4078 __ bind(has_identity);
4079
4080 const Address monitor_block_top(
4081 rbp, frame::interpreter_frame_monitor_block_top_offset * wordSize);
4082 const Address monitor_block_bot(
4083 rbp, frame::interpreter_frame_initial_sp_offset * wordSize);
4084 const int entry_size = frame::interpreter_frame_monitor_size_in_bytes();
4085
4086 Register rtop = c_rarg1;
4087 Register rbot = c_rarg2;
4088
4089 Label found;
4090
4091 // find matching slot
4092 {
4093 Label entry, loop;
4094 __ movptr(rtop, monitor_block_top); // derelativize pointer
4095 __ lea(rtop, Address(rbp, rtop, Address::times_ptr));
4096 // rtop points to current entry, starting with top-most entry
4097
4098 __ lea(rbot, monitor_block_bot); // points to word before bottom
4099 // of monitor block
4100 __ jmpb(entry);
4101
4102 __ bind(loop);
4103 // check if current entry is for same object
4104 __ cmpptr(rax, Address(rtop, BasicObjectLock::obj_offset()));
4105 // if same object then stop searching
4106 __ jcc(Assembler::equal, found);
4107 // otherwise advance to next entry
4108 __ addptr(rtop, entry_size);
4109 __ bind(entry);
4110 // check if bottom reached
4111 __ cmpptr(rtop, rbot);
4112 // if not at bottom then check this entry
4113 __ jcc(Assembler::notEqual, loop);
4114 }
4115
4116 // error handling. Unlocking was not block-structured
4117 __ call_VM(noreg, CAST_FROM_FN_PTR(address,
4118 InterpreterRuntime::throw_illegal_monitor_state_exception));
4119 __ should_not_reach_here();
4120
4121 // call run-time routine
4122 __ bind(found);
4123 __ push_ptr(rax); // make sure object is on stack (contract with oopMaps)
4124 __ unlock_object(rtop);
4125 __ pop_ptr(rax); // discard object
4126 }
4127
4128 // Wide instructions
4129 void TemplateTable::wide() {
4130 transition(vtos, vtos);
4131 __ load_unsigned_byte(rbx, at_bcp(1));
4132 ExternalAddress wtable((address)Interpreter::_wentry_point);
4133 __ jump(ArrayAddress(wtable, Address(noreg, rbx, Address::times_ptr)), rscratch1);
4134 // Note: the rbcp increment step is part of the individual wide bytecode implementations
4135 }
4136
4137 // Multi arrays
4138 void TemplateTable::multianewarray() {
4139 transition(vtos, atos);
4140
4141 __ load_unsigned_byte(rax, at_bcp(3)); // get number of dimensions
4142 // last dim is on top of stack; we want address of first one:
4143 // first_addr = last_addr + (ndims - 1) * stackElementSize - 1*wordsize
4144 // the latter wordSize to point to the beginning of the array.
4145 __ lea(c_rarg1, Address(rsp, rax, Interpreter::stackElementScale(), -wordSize));
4146 call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::multianewarray), c_rarg1);
4147 __ load_unsigned_byte(rbx, at_bcp(3));
4148 __ lea(rsp, Address(rsp, rbx, Interpreter::stackElementScale())); // get rid of counts
4149 }