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