1 /*
2  * Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.
3  * Copyright (c) 2016, 2020 SAP SE. 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 "gc/shared/tlab_globals.hpp"
30 #include "interpreter/interpreter.hpp"
31 #include "interpreter/interpreterRuntime.hpp"
32 #include "interpreter/interp_masm.hpp"
33 #include "interpreter/templateTable.hpp"
34 #include "memory/universe.hpp"
35 #include "oops/klass.inline.hpp"
36 #include "oops/methodData.hpp"
37 #include "oops/objArrayKlass.hpp"
38 #include "oops/oop.inline.hpp"
39 #include "prims/jvmtiExport.hpp"
40 #include "prims/methodHandles.hpp"
41 #include "runtime/frame.inline.hpp"
42 #include "runtime/safepointMechanism.hpp"
43 #include "runtime/sharedRuntime.hpp"
44 #include "runtime/stubRoutines.hpp"
45 #include "runtime/synchronizer.hpp"
46 #include "utilities/powerOfTwo.hpp"
47 
48 #ifdef PRODUCT
49 #define __ _masm->
50 #define BLOCK_COMMENT(str)
51 #define BIND(label)        __ bind(label);
52 #else
53 #define __ (PRODUCT_ONLY(false&&)Verbose ? (_masm->block_comment(FILE_AND_LINE),_masm):_masm)->
54 #define BLOCK_COMMENT(str) __ block_comment(str)
55 #define BIND(label)        __ bind(label); BLOCK_COMMENT(#label ":")
56 #endif
57 
58 // The assumed minimum size of a BranchTableBlock.
59 // The actual size of each block heavily depends on the CPU capabilities and,
60 // of course, on the logic implemented in each block.
61 #ifdef ASSERT
62   #define BTB_MINSIZE 256
63 #else
64   #define BTB_MINSIZE  64
65 #endif
66 
67 #ifdef ASSERT
68 // Macro to open a BranchTableBlock (a piece of code that is branched to by a calculated branch).
69 #define BTB_BEGIN(lbl, alignment, name)                                        \
70   __ align_address(alignment);                                                 \
71   __ bind(lbl);                                                                \
72   { unsigned int b_off = __ offset();                                          \
73     uintptr_t   b_addr = (uintptr_t)__ pc();                                   \
74     __ z_larl(Z_R0, (int64_t)0);     /* Check current address alignment. */    \
75     __ z_slgr(Z_R0, br_tab);         /* Current Address must be equal    */    \
76     __ z_slgr(Z_R0, flags);          /* to calculated branch target.     */    \
77     __ z_brc(Assembler::bcondLogZero, 3); /* skip trap if ok. */               \
78     __ z_illtrap(0x55);                                                        \
79     guarantee(b_addr%alignment == 0, "bad alignment at begin of block" name);
80 
81 // Macro to close a BranchTableBlock (a piece of code that is branched to by a calculated branch).
82 #define BTB_END(lbl, alignment, name)                                          \
83     uintptr_t   e_addr = (uintptr_t)__ pc();                                   \
84     unsigned int e_off = __ offset();                                          \
85     unsigned int len   = e_off-b_off;                                          \
86     if (len > alignment) {                                                     \
87       tty->print_cr("%4d of %4d @ " INTPTR_FORMAT ": Block len for %s",        \
88                     len, alignment, e_addr-len, name);                         \
89       guarantee(len <= alignment, "block too large");                          \
90     }                                                                          \
91     guarantee(len == e_addr-b_addr, "block len mismatch");                     \
92   }
93 #else
94 // Macro to open a BranchTableBlock (a piece of code that is branched to by a calculated branch).
95 #define BTB_BEGIN(lbl, alignment, name)                                        \
96   __ align_address(alignment);                                                 \
97   __ bind(lbl);                                                                \
98   { unsigned int b_off = __ offset();                                          \
99     uintptr_t   b_addr = (uintptr_t)__ pc();                                   \
100     guarantee(b_addr%alignment == 0, "bad alignment at begin of block" name);
101 
102 // Macro to close a BranchTableBlock (a piece of code that is branched to by a calculated branch).
103 #define BTB_END(lbl, alignment, name)                                          \
104     uintptr_t   e_addr = (uintptr_t)__ pc();                                   \
105     unsigned int e_off = __ offset();                                          \
106     unsigned int len   = e_off-b_off;                                          \
107     if (len > alignment) {                                                     \
108       tty->print_cr("%4d of %4d @ " INTPTR_FORMAT ": Block len for %s",        \
109                     len, alignment, e_addr-len, name);                         \
110       guarantee(len <= alignment, "block too large");                          \
111     }                                                                          \
112     guarantee(len == e_addr-b_addr, "block len mismatch");                     \
113   }
114 #endif // ASSERT
115 
116 // Address computation: local variables
117 
iaddress(int n)118 static inline Address iaddress(int n) {
119   return Address(Z_locals, Interpreter::local_offset_in_bytes(n));
120 }
121 
laddress(int n)122 static inline Address laddress(int n) {
123   return iaddress(n + 1);
124 }
125 
faddress(int n)126 static inline Address faddress(int n) {
127   return iaddress(n);
128 }
129 
daddress(int n)130 static inline Address daddress(int n) {
131   return laddress(n);
132 }
133 
aaddress(int n)134 static inline Address aaddress(int n) {
135   return iaddress(n);
136 }
137 
138 // Pass NULL, if no shift instruction should be emitted.
iaddress(InterpreterMacroAssembler * masm,Register r)139 static inline Address iaddress(InterpreterMacroAssembler *masm, Register r) {
140   if (masm) {
141     masm->z_sllg(r, r, LogBytesPerWord);  // index2bytes
142   }
143   return Address(Z_locals, r, Interpreter::local_offset_in_bytes(0));
144 }
145 
146 // Pass NULL, if no shift instruction should be emitted.
laddress(InterpreterMacroAssembler * masm,Register r)147 static inline Address laddress(InterpreterMacroAssembler *masm, Register r) {
148   if (masm) {
149     masm->z_sllg(r, r, LogBytesPerWord);  // index2bytes
150   }
151   return Address(Z_locals, r, Interpreter::local_offset_in_bytes(1) );
152 }
153 
faddress(InterpreterMacroAssembler * masm,Register r)154 static inline Address faddress(InterpreterMacroAssembler *masm, Register r) {
155   return iaddress(masm, r);
156 }
157 
daddress(InterpreterMacroAssembler * masm,Register r)158 static inline Address daddress(InterpreterMacroAssembler *masm, Register r) {
159   return laddress(masm, r);
160 }
161 
aaddress(InterpreterMacroAssembler * masm,Register r)162 static inline Address aaddress(InterpreterMacroAssembler *masm, Register r) {
163   return iaddress(masm, r);
164 }
165 
166 // At top of Java expression stack which may be different than esp(). It
167 // isn't for category 1 objects.
at_tos(int slot=0)168 static inline Address at_tos(int slot = 0) {
169   return Address(Z_esp, Interpreter::expr_offset_in_bytes(slot));
170 }
171 
172 // Condition conversion
j_not(TemplateTable::Condition cc)173 static Assembler::branch_condition j_not(TemplateTable::Condition cc) {
174   switch (cc) {
175     case TemplateTable::equal :
176       return Assembler::bcondNotEqual;
177     case TemplateTable::not_equal :
178       return Assembler::bcondEqual;
179     case TemplateTable::less :
180       return Assembler::bcondNotLow;
181     case TemplateTable::less_equal :
182       return Assembler::bcondHigh;
183     case TemplateTable::greater :
184       return Assembler::bcondNotHigh;
185     case TemplateTable::greater_equal:
186       return Assembler::bcondLow;
187   }
188   ShouldNotReachHere();
189   return Assembler::bcondZero;
190 }
191 
192 // Do an oop store like *(base + offset) = val
193 // offset can be a register or a constant.
do_oop_store(InterpreterMacroAssembler * _masm,const Address & addr,Register val,Register tmp1,Register tmp2,Register tmp3,DecoratorSet decorators)194 static void do_oop_store(InterpreterMacroAssembler* _masm,
195                          const Address&     addr,
196                          Register           val,         // Noreg means always null.
197                          Register           tmp1,
198                          Register           tmp2,
199                          Register           tmp3,
200                          DecoratorSet       decorators) {
201   assert_different_registers(tmp1, tmp2, tmp3, val, addr.base());
202   __ store_heap_oop(val, addr, tmp1, tmp2, tmp3, decorators);
203 }
204 
do_oop_load(InterpreterMacroAssembler * _masm,const Address & addr,Register dst,Register tmp1,Register tmp2,DecoratorSet decorators)205 static void do_oop_load(InterpreterMacroAssembler* _masm,
206                         const Address& addr,
207                         Register dst,
208                         Register tmp1,
209                         Register tmp2,
210                         DecoratorSet decorators) {
211   assert_different_registers(addr.base(), tmp1, tmp2);
212   assert_different_registers(dst, tmp1, tmp2);
213   __ load_heap_oop(dst, addr, tmp1, tmp2, decorators);
214 }
215 
at_bcp(int offset)216 Address TemplateTable::at_bcp(int offset) {
217   assert(_desc->uses_bcp(), "inconsistent uses_bcp information");
218   return Address(Z_bcp, offset);
219 }
220 
patch_bytecode(Bytecodes::Code bc,Register bc_reg,Register temp_reg,bool load_bc_into_bc_reg,int byte_no)221 void TemplateTable::patch_bytecode(Bytecodes::Code bc,
222                                    Register        bc_reg,
223                                    Register        temp_reg,
224                                    bool            load_bc_into_bc_reg, // = true
225                                    int             byte_no) {
226   if (!RewriteBytecodes) { return; }
227 
228   NearLabel L_patch_done;
229   BLOCK_COMMENT("patch_bytecode {");
230 
231   switch (bc) {
232     case Bytecodes::_fast_aputfield:
233     case Bytecodes::_fast_bputfield:
234     case Bytecodes::_fast_zputfield:
235     case Bytecodes::_fast_cputfield:
236     case Bytecodes::_fast_dputfield:
237     case Bytecodes::_fast_fputfield:
238     case Bytecodes::_fast_iputfield:
239     case Bytecodes::_fast_lputfield:
240     case Bytecodes::_fast_sputfield:
241       {
242         // We skip bytecode quickening for putfield instructions when
243         // the put_code written to the constant pool cache is zero.
244         // This is required so that every execution of this instruction
245         // calls out to InterpreterRuntime::resolve_get_put to do
246         // additional, required work.
247         assert(byte_no == f1_byte || byte_no == f2_byte, "byte_no out of range");
248         assert(load_bc_into_bc_reg, "we use bc_reg as temp");
249         __ get_cache_and_index_and_bytecode_at_bcp(Z_R1_scratch, bc_reg,
250                                                    temp_reg, byte_no, 1);
251         __ load_const_optimized(bc_reg, bc);
252         __ compareU32_and_branch(temp_reg, (intptr_t)0,
253                                  Assembler::bcondZero, L_patch_done);
254       }
255       break;
256     default:
257       assert(byte_no == -1, "sanity");
258       // The pair bytecodes have already done the load.
259       if (load_bc_into_bc_reg) {
260         __ load_const_optimized(bc_reg, bc);
261       }
262       break;
263   }
264 
265   if (JvmtiExport::can_post_breakpoint()) {
266 
267     Label   L_fast_patch;
268 
269     // If a breakpoint is present we can't rewrite the stream directly.
270     __ z_cli(at_bcp(0), Bytecodes::_breakpoint);
271     __ z_brne(L_fast_patch);
272     __ get_method(temp_reg);
273     // Let breakpoint table handling rewrite to quicker bytecode.
274     __ call_VM_static(noreg,
275                       CAST_FROM_FN_PTR(address, InterpreterRuntime::set_original_bytecode_at),
276                       temp_reg, Z_R13, bc_reg);
277     __ z_bru(L_patch_done);
278 
279     __ bind(L_fast_patch);
280   }
281 
282 #ifdef ASSERT
283   NearLabel   L_okay;
284 
285   // We load into 64 bits, since this works on any CPU.
286   __ z_llgc(temp_reg, at_bcp(0));
287   __ compareU32_and_branch(temp_reg, Bytecodes::java_code(bc),
288                             Assembler::bcondEqual, L_okay        );
289   __ compareU32_and_branch(temp_reg, bc_reg, Assembler::bcondEqual, L_okay);
290   __ stop_static("patching the wrong bytecode");
291   __ bind(L_okay);
292 #endif
293 
294   // Patch bytecode.
295   __ z_stc(bc_reg, at_bcp(0));
296 
297   __ bind(L_patch_done);
298   BLOCK_COMMENT("} patch_bytecode");
299 }
300 
301 // Individual instructions
302 
nop()303 void TemplateTable::nop() {
304   transition(vtos, vtos);
305 }
306 
shouldnotreachhere()307 void TemplateTable::shouldnotreachhere() {
308   transition(vtos, vtos);
309   __ stop("shouldnotreachhere bytecode");
310 }
311 
aconst_null()312 void TemplateTable::aconst_null() {
313   transition(vtos, atos);
314   __ clear_reg(Z_tos, true, false);
315 }
316 
iconst(int value)317 void TemplateTable::iconst(int value) {
318   transition(vtos, itos);
319   // Zero extension of the iconst makes zero extension at runtime obsolete.
320   __ load_const_optimized(Z_tos, ((unsigned long)(unsigned int)value));
321 }
322 
lconst(int value)323 void TemplateTable::lconst(int value) {
324   transition(vtos, ltos);
325   __ load_const_optimized(Z_tos, value);
326 }
327 
328 // No pc-relative load/store for floats.
fconst(int value)329 void TemplateTable::fconst(int value) {
330   transition(vtos, ftos);
331   static float   one = 1.0f, two = 2.0f;
332 
333   switch (value) {
334     case 0:
335       __ z_lzer(Z_ftos);
336       return;
337     case 1:
338       __ load_absolute_address(Z_R1_scratch, (address) &one);
339       __ mem2freg_opt(Z_ftos, Address(Z_R1_scratch), false);
340       return;
341     case 2:
342       __ load_absolute_address(Z_R1_scratch, (address) &two);
343       __ mem2freg_opt(Z_ftos, Address(Z_R1_scratch), false);
344       return;
345     default:
346       ShouldNotReachHere();
347       return;
348   }
349 }
350 
dconst(int value)351 void TemplateTable::dconst(int value) {
352   transition(vtos, dtos);
353   static double one = 1.0;
354 
355   switch (value) {
356     case 0:
357       __ z_lzdr(Z_ftos);
358       return;
359     case 1:
360       __ load_absolute_address(Z_R1_scratch, (address) &one);
361       __ mem2freg_opt(Z_ftos, Address(Z_R1_scratch));
362       return;
363     default:
364       ShouldNotReachHere();
365       return;
366   }
367 }
368 
bipush()369 void TemplateTable::bipush() {
370   transition(vtos, itos);
371   __ z_lb(Z_tos, at_bcp(1));
372 }
373 
sipush()374 void TemplateTable::sipush() {
375   transition(vtos, itos);
376   __ get_2_byte_integer_at_bcp(Z_tos, 1, InterpreterMacroAssembler::Signed);
377 }
378 
379 
ldc(bool wide)380 void TemplateTable::ldc(bool wide) {
381   transition(vtos, vtos);
382   Label call_ldc, notFloat, notClass, notInt, Done;
383   const Register RcpIndex = Z_tmp_1;
384   const Register Rtags = Z_ARG2;
385 
386   if (wide) {
387     __ get_2_byte_integer_at_bcp(RcpIndex, 1, InterpreterMacroAssembler::Unsigned);
388   } else {
389     __ z_llgc(RcpIndex, at_bcp(1));
390   }
391 
392   __ get_cpool_and_tags(Z_tmp_2, Rtags);
393 
394   const int      base_offset = ConstantPool::header_size() * wordSize;
395   const int      tags_offset = Array<u1>::base_offset_in_bytes();
396   const Register Raddr_type = Rtags;
397 
398   // Get address of type.
399   __ add2reg_with_index(Raddr_type, tags_offset, RcpIndex, Rtags);
400 
401   __ z_cli(0, Raddr_type, JVM_CONSTANT_UnresolvedClass);
402   __ z_bre(call_ldc);    // Unresolved class - get the resolved class.
403 
404   __ z_cli(0, Raddr_type, JVM_CONSTANT_UnresolvedClassInError);
405   __ z_bre(call_ldc);    // Unresolved class in error state - call into runtime
406                          // to throw the error from the first resolution attempt.
407 
408   __ z_cli(0, Raddr_type, JVM_CONSTANT_Class);
409   __ z_brne(notClass);   // Resolved class - need to call vm to get java
410                          // mirror of the class.
411 
412   // We deal with a class. Call vm to do the appropriate.
413   __ bind(call_ldc);
414   __ load_const_optimized(Z_ARG2, wide);
415   call_VM(Z_RET, CAST_FROM_FN_PTR(address, InterpreterRuntime::ldc), Z_ARG2);
416   __ push_ptr(Z_RET);
417   __ z_bru(Done);
418 
419   // Not a class.
420   __ bind(notClass);
421   Register RcpOffset = RcpIndex;
422   __ z_sllg(RcpOffset, RcpIndex, LogBytesPerWord); // Convert index to offset.
423   __ z_cli(0, Raddr_type, JVM_CONSTANT_Float);
424   __ z_brne(notFloat);
425 
426   // ftos
427   __ mem2freg_opt(Z_ftos, Address(Z_tmp_2, RcpOffset, base_offset), false);
428   __ push_f();
429   __ z_bru(Done);
430 
431   __ bind(notFloat);
432   __ z_cli(0, Raddr_type, JVM_CONSTANT_Integer);
433   __ z_brne(notInt);
434 
435   // itos
436   __ mem2reg_opt(Z_tos, Address(Z_tmp_2, RcpOffset, base_offset), false);
437   __ push_i(Z_tos);
438   __ z_bru(Done);
439 
440   // assume the tag is for condy; if not, the VM runtime will tell us
441   __ bind(notInt);
442   condy_helper(Done);
443 
444   __ bind(Done);
445 }
446 
447 // Fast path for caching oop constants.
448 // %%% We should use this to handle Class and String constants also.
449 // %%% It will simplify the ldc/primitive path considerably.
fast_aldc(bool wide)450 void TemplateTable::fast_aldc(bool wide) {
451   transition(vtos, atos);
452 
453   const Register index = Z_tmp_2;
454   int            index_size = wide ? sizeof(u2) : sizeof(u1);
455   Label          L_do_resolve, L_resolved;
456 
457   // We are resolved if the resolved reference cache entry contains a
458   // non-null object (CallSite, etc.).
459   __ get_cache_index_at_bcp(index, 1, index_size);  // Load index.
460   __ load_resolved_reference_at_index(Z_tos, index);
461   __ z_ltgr(Z_tos, Z_tos);
462   __ z_bre(L_do_resolve);
463 
464   // Convert null sentinel to NULL.
465   __ load_const_optimized(Z_R1_scratch, (intptr_t)Universe::the_null_sentinel_addr());
466   __ resolve_oop_handle(Z_R1_scratch);
467   __ z_cg(Z_tos, Address(Z_R1_scratch));
468   __ z_brne(L_resolved);
469   __ clear_reg(Z_tos);
470   __ z_bru(L_resolved);
471 
472   __ bind(L_do_resolve);
473   // First time invocation - must resolve first.
474   address entry = CAST_FROM_FN_PTR(address, InterpreterRuntime::resolve_ldc);
475   __ load_const_optimized(Z_ARG1, (int)bytecode());
476   __ call_VM(Z_tos, entry, Z_ARG1);
477 
478   __ bind(L_resolved);
479   __ verify_oop(Z_tos);
480 }
481 
ldc2_w()482 void TemplateTable::ldc2_w() {
483   transition(vtos, vtos);
484   Label notDouble, notLong, Done;
485 
486   // Z_tmp_1 = index of cp entry
487   __ get_2_byte_integer_at_bcp(Z_tmp_1, 1, InterpreterMacroAssembler::Unsigned);
488 
489   __ get_cpool_and_tags(Z_tmp_2, Z_tos);
490 
491   const int base_offset = ConstantPool::header_size() * wordSize;
492   const int tags_offset = Array<u1>::base_offset_in_bytes();
493 
494   // Get address of type.
495   __ add2reg_with_index(Z_tos, tags_offset, Z_tos, Z_tmp_1);
496 
497   // Index needed in both branches, so calculate here.
498   __ z_sllg(Z_tmp_1, Z_tmp_1, LogBytesPerWord);  // index2bytes
499 
500   // Check type.
501   __ z_cli(0, Z_tos, JVM_CONSTANT_Double);
502   __ z_brne(notDouble);
503   // dtos
504   __ mem2freg_opt(Z_ftos, Address(Z_tmp_2, Z_tmp_1, base_offset));
505   __ push_d();
506   __ z_bru(Done);
507 
508   __ bind(notDouble);
509   __ z_cli(0, Z_tos, JVM_CONSTANT_Long);
510   __ z_brne(notLong);
511   // ltos
512   __ mem2reg_opt(Z_tos, Address(Z_tmp_2, Z_tmp_1, base_offset));
513   __ push_l();
514   __ z_bru(Done);
515 
516   __ bind(notLong);
517   condy_helper(Done);
518 
519   __ bind(Done);
520 }
521 
condy_helper(Label & Done)522 void TemplateTable::condy_helper(Label& Done) {
523   const Register obj   = Z_tmp_1;
524   const Register off   = Z_tmp_2;
525   const Register flags = Z_ARG1;
526   const Register rarg  = Z_ARG2;
527   __ load_const_optimized(rarg, (int)bytecode());
528   call_VM(obj, CAST_FROM_FN_PTR(address, InterpreterRuntime::resolve_ldc), rarg);
529   __ get_vm_result_2(flags);
530 
531   // VMr = obj = base address to find primitive value to push
532   // VMr2 = flags = (tos, off) using format of CPCE::_flags
533   assert(ConstantPoolCacheEntry::field_index_mask == 0xffff, "or use other instructions");
534   __ z_llghr(off, flags);
535   const Address field(obj, off);
536 
537   // What sort of thing are we loading?
538   __ z_srl(flags, ConstantPoolCacheEntry::tos_state_shift);
539   // Make sure we don't need to mask flags for tos_state after the above shift.
540   ConstantPoolCacheEntry::verify_tos_state_shift();
541 
542   switch (bytecode()) {
543   case Bytecodes::_ldc:
544   case Bytecodes::_ldc_w:
545     {
546       // tos in (itos, ftos, stos, btos, ctos, ztos)
547       Label notInt, notFloat, notShort, notByte, notChar, notBool;
548       __ z_cghi(flags, itos);
549       __ z_brne(notInt);
550       // itos
551       __ z_l(Z_tos, field);
552       __ push(itos);
553       __ z_bru(Done);
554 
555       __ bind(notInt);
556       __ z_cghi(flags, ftos);
557       __ z_brne(notFloat);
558       // ftos
559       __ z_le(Z_ftos, field);
560       __ push(ftos);
561       __ z_bru(Done);
562 
563       __ bind(notFloat);
564       __ z_cghi(flags, stos);
565       __ z_brne(notShort);
566       // stos
567       __ z_lh(Z_tos, field);
568       __ push(stos);
569       __ z_bru(Done);
570 
571       __ bind(notShort);
572       __ z_cghi(flags, btos);
573       __ z_brne(notByte);
574       // btos
575       __ z_lb(Z_tos, field);
576       __ push(btos);
577       __ z_bru(Done);
578 
579       __ bind(notByte);
580       __ z_cghi(flags, ctos);
581       __ z_brne(notChar);
582       // ctos
583       __ z_llh(Z_tos, field);
584       __ push(ctos);
585       __ z_bru(Done);
586 
587       __ bind(notChar);
588       __ z_cghi(flags, ztos);
589       __ z_brne(notBool);
590       // ztos
591       __ z_lb(Z_tos, field);
592       __ push(ztos);
593       __ z_bru(Done);
594 
595       __ bind(notBool);
596       break;
597     }
598 
599   case Bytecodes::_ldc2_w:
600     {
601       Label notLong, notDouble;
602       __ z_cghi(flags, ltos);
603       __ z_brne(notLong);
604       // ltos
605       __ z_lg(Z_tos, field);
606       __ push(ltos);
607       __ z_bru(Done);
608 
609       __ bind(notLong);
610       __ z_cghi(flags, dtos);
611       __ z_brne(notDouble);
612       // dtos
613       __ z_ld(Z_ftos, field);
614       __ push(dtos);
615       __ z_bru(Done);
616 
617       __ bind(notDouble);
618       break;
619     }
620 
621   default:
622     ShouldNotReachHere();
623   }
624 
625   __ stop("bad ldc/condy");
626 }
627 
locals_index(Register reg,int offset)628 void TemplateTable::locals_index(Register reg, int offset) {
629   __ z_llgc(reg, at_bcp(offset));
630   __ z_lcgr(reg);
631 }
632 
iload()633 void TemplateTable::iload() {
634   iload_internal();
635 }
636 
nofast_iload()637 void TemplateTable::nofast_iload() {
638   iload_internal(may_not_rewrite);
639 }
640 
iload_internal(RewriteControl rc)641 void TemplateTable::iload_internal(RewriteControl rc) {
642   transition(vtos, itos);
643 
644   if (RewriteFrequentPairs && rc == may_rewrite) {
645     NearLabel rewrite, done;
646     const Register bc = Z_ARG4;
647 
648     assert(Z_R1_scratch != bc, "register damaged");
649 
650     // Get next byte.
651     __ z_llgc(Z_R1_scratch, at_bcp(Bytecodes::length_for (Bytecodes::_iload)));
652 
653     // If _iload, wait to rewrite to iload2. We only want to rewrite the
654     // last two iloads in a pair. Comparing against fast_iload means that
655     // the next bytecode is neither an iload or a caload, and therefore
656     // an iload pair.
657     __ compareU32_and_branch(Z_R1_scratch, Bytecodes::_iload,
658                              Assembler::bcondEqual, done);
659 
660     __ load_const_optimized(bc, Bytecodes::_fast_iload2);
661     __ compareU32_and_branch(Z_R1_scratch, Bytecodes::_fast_iload,
662                              Assembler::bcondEqual, rewrite);
663 
664     // If _caload, rewrite to fast_icaload.
665     __ load_const_optimized(bc, Bytecodes::_fast_icaload);
666     __ compareU32_and_branch(Z_R1_scratch, Bytecodes::_caload,
667                              Assembler::bcondEqual, rewrite);
668 
669     // Rewrite so iload doesn't check again.
670     __ load_const_optimized(bc, Bytecodes::_fast_iload);
671 
672     // rewrite
673     // bc: fast bytecode
674     __ bind(rewrite);
675     patch_bytecode(Bytecodes::_iload, bc, Z_R1_scratch, false);
676 
677     __ bind(done);
678 
679   }
680 
681   // Get the local value into tos.
682   locals_index(Z_R1_scratch);
683   __ mem2reg_opt(Z_tos, iaddress(_masm, Z_R1_scratch), false);
684 }
685 
fast_iload2()686 void TemplateTable::fast_iload2() {
687   transition(vtos, itos);
688 
689   locals_index(Z_R1_scratch);
690   __ mem2reg_opt(Z_tos, iaddress(_masm, Z_R1_scratch), false);
691   __ push_i(Z_tos);
692   locals_index(Z_R1_scratch, 3);
693   __ mem2reg_opt(Z_tos, iaddress(_masm, Z_R1_scratch), false);
694 }
695 
fast_iload()696 void TemplateTable::fast_iload() {
697   transition(vtos, itos);
698 
699   locals_index(Z_R1_scratch);
700   __ mem2reg_opt(Z_tos, iaddress(_masm, Z_R1_scratch), false);
701 }
702 
lload()703 void TemplateTable::lload() {
704   transition(vtos, ltos);
705 
706   locals_index(Z_R1_scratch);
707   __ mem2reg_opt(Z_tos, laddress(_masm, Z_R1_scratch));
708 }
709 
fload()710 void TemplateTable::fload() {
711   transition(vtos, ftos);
712 
713   locals_index(Z_R1_scratch);
714   __ mem2freg_opt(Z_ftos, faddress(_masm, Z_R1_scratch), false);
715 }
716 
dload()717 void TemplateTable::dload() {
718   transition(vtos, dtos);
719 
720   locals_index(Z_R1_scratch);
721   __ mem2freg_opt(Z_ftos, daddress(_masm, Z_R1_scratch));
722 }
723 
aload()724 void TemplateTable::aload() {
725   transition(vtos, atos);
726 
727   locals_index(Z_R1_scratch);
728   __ mem2reg_opt(Z_tos, aaddress(_masm, Z_R1_scratch));
729 }
730 
locals_index_wide(Register reg)731 void TemplateTable::locals_index_wide(Register reg) {
732   __ get_2_byte_integer_at_bcp(reg, 2, InterpreterMacroAssembler::Unsigned);
733   __ z_lcgr(reg);
734 }
735 
wide_iload()736 void TemplateTable::wide_iload() {
737   transition(vtos, itos);
738 
739   locals_index_wide(Z_tmp_1);
740   __ mem2reg_opt(Z_tos, iaddress(_masm, Z_tmp_1), false);
741 }
742 
wide_lload()743 void TemplateTable::wide_lload() {
744   transition(vtos, ltos);
745 
746   locals_index_wide(Z_tmp_1);
747   __ mem2reg_opt(Z_tos, laddress(_masm, Z_tmp_1));
748 }
749 
wide_fload()750 void TemplateTable::wide_fload() {
751   transition(vtos, ftos);
752 
753   locals_index_wide(Z_tmp_1);
754   __ mem2freg_opt(Z_ftos, faddress(_masm, Z_tmp_1), false);
755 }
756 
wide_dload()757 void TemplateTable::wide_dload() {
758   transition(vtos, dtos);
759 
760   locals_index_wide(Z_tmp_1);
761   __ mem2freg_opt(Z_ftos, daddress(_masm, Z_tmp_1));
762 }
763 
wide_aload()764 void TemplateTable::wide_aload() {
765   transition(vtos, atos);
766 
767   locals_index_wide(Z_tmp_1);
768   __ mem2reg_opt(Z_tos, aaddress(_masm, Z_tmp_1));
769 }
770 
index_check(Register array,Register index,unsigned int shift)771 void TemplateTable::index_check(Register array, Register index, unsigned int shift) {
772   assert_different_registers(Z_R1_scratch, array, index);
773 
774   // Check array.
775   __ null_check(array, Z_R0_scratch, arrayOopDesc::length_offset_in_bytes());
776 
777   // Sign extend index for use by indexed load.
778   __ z_lgfr(index, index);
779 
780   // Check index.
781   Label index_ok;
782   __ z_cl(index, Address(array, arrayOopDesc::length_offset_in_bytes()));
783   __ z_brl(index_ok);
784   __ lgr_if_needed(Z_ARG3, index); // See generate_ArrayIndexOutOfBounds_handler().
785   // Pass the array to create more detailed exceptions.
786   __ lgr_if_needed(Z_ARG2, array); // See generate_ArrayIndexOutOfBounds_handler().
787   __ load_absolute_address(Z_R1_scratch,
788                            Interpreter::_throw_ArrayIndexOutOfBoundsException_entry);
789   __ z_bcr(Assembler::bcondAlways, Z_R1_scratch);
790   __ bind(index_ok);
791 
792   if (shift > 0)
793     __ z_sllg(index, index, shift);
794 }
795 
iaload()796 void TemplateTable::iaload() {
797   transition(itos, itos);
798 
799   __ pop_ptr(Z_tmp_1);  // array
800   // Index is in Z_tos.
801   Register index = Z_tos;
802   index_check(Z_tmp_1, index, LogBytesPerInt); // Kills Z_ARG3.
803   // Load the value.
804   __ mem2reg_opt(Z_tos,
805                  Address(Z_tmp_1, index, arrayOopDesc::base_offset_in_bytes(T_INT)),
806                  false);
807 }
808 
laload()809 void TemplateTable::laload() {
810   transition(itos, ltos);
811 
812   __ pop_ptr(Z_tmp_2);
813   // Z_tos   : index
814   // Z_tmp_2 : array
815   Register index = Z_tos;
816   index_check(Z_tmp_2, index, LogBytesPerLong);
817   __ mem2reg_opt(Z_tos,
818                  Address(Z_tmp_2, index, arrayOopDesc::base_offset_in_bytes(T_LONG)));
819 }
820 
faload()821 void TemplateTable::faload() {
822   transition(itos, ftos);
823 
824   __ pop_ptr(Z_tmp_2);
825   // Z_tos   : index
826   // Z_tmp_2 : array
827   Register index = Z_tos;
828   index_check(Z_tmp_2, index, LogBytesPerInt);
829   __ mem2freg_opt(Z_ftos,
830                   Address(Z_tmp_2, index, arrayOopDesc::base_offset_in_bytes(T_FLOAT)),
831                   false);
832 }
833 
daload()834 void TemplateTable::daload() {
835   transition(itos, dtos);
836 
837   __ pop_ptr(Z_tmp_2);
838   // Z_tos   : index
839   // Z_tmp_2 : array
840   Register index = Z_tos;
841   index_check(Z_tmp_2, index, LogBytesPerLong);
842   __ mem2freg_opt(Z_ftos,
843                   Address(Z_tmp_2, index, arrayOopDesc::base_offset_in_bytes(T_DOUBLE)));
844 }
845 
aaload()846 void TemplateTable::aaload() {
847   transition(itos, atos);
848 
849   unsigned const int shift = LogBytesPerHeapOop;
850   __ pop_ptr(Z_tmp_1);  // array
851   // Index is in Z_tos.
852   Register index = Z_tos;
853   index_check(Z_tmp_1, index, shift);
854   // Now load array element.
855   do_oop_load(_masm, Address(Z_tmp_1, index, arrayOopDesc::base_offset_in_bytes(T_OBJECT)), Z_tos,
856               Z_tmp_2, Z_tmp_3, IS_ARRAY);
857   __ verify_oop(Z_tos);
858 }
859 
baload()860 void TemplateTable::baload() {
861   transition(itos, itos);
862 
863   __ pop_ptr(Z_tmp_1);
864   // Z_tos   : index
865   // Z_tmp_1 : array
866   Register index = Z_tos;
867   index_check(Z_tmp_1, index, 0);
868   __ z_lb(Z_tos,
869           Address(Z_tmp_1, index, arrayOopDesc::base_offset_in_bytes(T_BYTE)));
870 }
871 
caload()872 void TemplateTable::caload() {
873   transition(itos, itos);
874 
875   __ pop_ptr(Z_tmp_2);
876   // Z_tos   : index
877   // Z_tmp_2 : array
878   Register index = Z_tos;
879   index_check(Z_tmp_2, index, LogBytesPerShort);
880   // Load into 64 bits, works on all CPUs.
881   __ z_llgh(Z_tos,
882             Address(Z_tmp_2, index, arrayOopDesc::base_offset_in_bytes(T_CHAR)));
883 }
884 
885 // Iload followed by caload frequent pair.
fast_icaload()886 void TemplateTable::fast_icaload() {
887   transition(vtos, itos);
888 
889   // Load index out of locals.
890   locals_index(Z_R1_scratch);
891   __ mem2reg_opt(Z_ARG3, iaddress(_masm, Z_R1_scratch), false);
892   // Z_ARG3  : index
893   // Z_tmp_2 : array
894   __ pop_ptr(Z_tmp_2);
895   index_check(Z_tmp_2, Z_ARG3, LogBytesPerShort);
896   // Load into 64 bits, works on all CPUs.
897   __ z_llgh(Z_tos,
898             Address(Z_tmp_2, Z_ARG3, arrayOopDesc::base_offset_in_bytes(T_CHAR)));
899 }
900 
saload()901 void TemplateTable::saload() {
902   transition(itos, itos);
903 
904   __ pop_ptr(Z_tmp_2);
905   // Z_tos   : index
906   // Z_tmp_2 : array
907   Register index = Z_tos;
908   index_check(Z_tmp_2, index, LogBytesPerShort);
909   __ z_lh(Z_tos,
910           Address(Z_tmp_2, index, arrayOopDesc::base_offset_in_bytes(T_SHORT)));
911 }
912 
iload(int n)913 void TemplateTable::iload(int n) {
914   transition(vtos, itos);
915   __ z_ly(Z_tos, iaddress(n));
916 }
917 
lload(int n)918 void TemplateTable::lload(int n) {
919   transition(vtos, ltos);
920   __ z_lg(Z_tos, laddress(n));
921 }
922 
fload(int n)923 void TemplateTable::fload(int n) {
924   transition(vtos, ftos);
925   __ mem2freg_opt(Z_ftos, faddress(n), false);
926 }
927 
dload(int n)928 void TemplateTable::dload(int n) {
929   transition(vtos, dtos);
930   __ mem2freg_opt(Z_ftos, daddress(n));
931 }
932 
aload(int n)933 void TemplateTable::aload(int n) {
934   transition(vtos, atos);
935   __ mem2reg_opt(Z_tos, aaddress(n));
936 }
937 
aload_0()938 void TemplateTable::aload_0() {
939   aload_0_internal();
940 }
941 
nofast_aload_0()942 void TemplateTable::nofast_aload_0() {
943   aload_0_internal(may_not_rewrite);
944 }
945 
aload_0_internal(RewriteControl rc)946 void TemplateTable::aload_0_internal(RewriteControl rc) {
947   transition(vtos, atos);
948 
949   // According to bytecode histograms, the pairs:
950   //
951   // _aload_0, _fast_igetfield
952   // _aload_0, _fast_agetfield
953   // _aload_0, _fast_fgetfield
954   //
955   // occur frequently. If RewriteFrequentPairs is set, the (slow)
956   // _aload_0 bytecode checks if the next bytecode is either
957   // _fast_igetfield, _fast_agetfield or _fast_fgetfield and then
958   // rewrites the current bytecode into a pair bytecode; otherwise it
959   // rewrites the current bytecode into _fast_aload_0 that doesn't do
960   // the pair check anymore.
961   //
962   // Note: If the next bytecode is _getfield, the rewrite must be
963   //       delayed, otherwise we may miss an opportunity for a pair.
964   //
965   // Also rewrite frequent pairs
966   //   aload_0, aload_1
967   //   aload_0, iload_1
968   // These bytecodes with a small amount of code are most profitable
969   // to rewrite.
970   if (!(RewriteFrequentPairs && (rc == may_rewrite))) {
971     aload(0);
972     return;
973   }
974 
975   NearLabel rewrite, done;
976   const Register bc = Z_ARG4;
977 
978   assert(Z_R1_scratch != bc, "register damaged");
979   // Get next byte.
980   __ z_llgc(Z_R1_scratch, at_bcp(Bytecodes::length_for (Bytecodes::_aload_0)));
981 
982   // Do actual aload_0.
983   aload(0);
984 
985   // If _getfield then wait with rewrite.
986   __ compareU32_and_branch(Z_R1_scratch, Bytecodes::_getfield,
987                            Assembler::bcondEqual, done);
988 
989   // If _igetfield then rewrite to _fast_iaccess_0.
990   assert(Bytecodes::java_code(Bytecodes::_fast_iaccess_0)
991             == Bytecodes::_aload_0, "fix bytecode definition");
992 
993   __ load_const_optimized(bc, Bytecodes::_fast_iaccess_0);
994   __ compareU32_and_branch(Z_R1_scratch, Bytecodes::_fast_igetfield,
995                            Assembler::bcondEqual, rewrite);
996 
997   // If _agetfield then rewrite to _fast_aaccess_0.
998   assert(Bytecodes::java_code(Bytecodes::_fast_aaccess_0)
999             == Bytecodes::_aload_0, "fix bytecode definition");
1000 
1001   __ load_const_optimized(bc, Bytecodes::_fast_aaccess_0);
1002   __ compareU32_and_branch(Z_R1_scratch, Bytecodes::_fast_agetfield,
1003                            Assembler::bcondEqual, rewrite);
1004 
1005   // If _fgetfield then rewrite to _fast_faccess_0.
1006   assert(Bytecodes::java_code(Bytecodes::_fast_faccess_0)
1007             == Bytecodes::_aload_0, "fix bytecode definition");
1008 
1009   __ load_const_optimized(bc, Bytecodes::_fast_faccess_0);
1010   __ compareU32_and_branch(Z_R1_scratch, Bytecodes::_fast_fgetfield,
1011                            Assembler::bcondEqual, rewrite);
1012 
1013   // Else rewrite to _fast_aload0.
1014   assert(Bytecodes::java_code(Bytecodes::_fast_aload_0)
1015             == Bytecodes::_aload_0, "fix bytecode definition");
1016   __ load_const_optimized(bc, Bytecodes::_fast_aload_0);
1017 
1018   // rewrite
1019   // bc: fast bytecode
1020   __ bind(rewrite);
1021 
1022   patch_bytecode(Bytecodes::_aload_0, bc, Z_R1_scratch, false);
1023   // Reload local 0 because of VM call inside patch_bytecode().
1024   // this may trigger GC and thus change the oop.
1025   aload(0);
1026 
1027   __ bind(done);
1028 }
1029 
istore()1030 void TemplateTable::istore() {
1031   transition(itos, vtos);
1032   locals_index(Z_R1_scratch);
1033   __ reg2mem_opt(Z_tos, iaddress(_masm, Z_R1_scratch), false);
1034 }
1035 
lstore()1036 void TemplateTable::lstore() {
1037   transition(ltos, vtos);
1038   locals_index(Z_R1_scratch);
1039   __ reg2mem_opt(Z_tos, laddress(_masm, Z_R1_scratch));
1040 }
1041 
fstore()1042 void TemplateTable::fstore() {
1043   transition(ftos, vtos);
1044   locals_index(Z_R1_scratch);
1045   __ freg2mem_opt(Z_ftos, faddress(_masm, Z_R1_scratch));
1046 }
1047 
dstore()1048 void TemplateTable::dstore() {
1049   transition(dtos, vtos);
1050   locals_index(Z_R1_scratch);
1051   __ freg2mem_opt(Z_ftos, daddress(_masm, Z_R1_scratch));
1052 }
1053 
astore()1054 void TemplateTable::astore() {
1055   transition(vtos, vtos);
1056   __ pop_ptr(Z_tos);
1057   locals_index(Z_R1_scratch);
1058   __ reg2mem_opt(Z_tos, aaddress(_masm, Z_R1_scratch));
1059 }
1060 
wide_istore()1061 void TemplateTable::wide_istore() {
1062   transition(vtos, vtos);
1063   __ pop_i(Z_tos);
1064   locals_index_wide(Z_tmp_1);
1065   __ reg2mem_opt(Z_tos, iaddress(_masm, Z_tmp_1), false);
1066 }
1067 
wide_lstore()1068 void TemplateTable::wide_lstore() {
1069   transition(vtos, vtos);
1070   __ pop_l(Z_tos);
1071   locals_index_wide(Z_tmp_1);
1072   __ reg2mem_opt(Z_tos, laddress(_masm, Z_tmp_1));
1073 }
1074 
wide_fstore()1075 void TemplateTable::wide_fstore() {
1076   transition(vtos, vtos);
1077   __ pop_f(Z_ftos);
1078   locals_index_wide(Z_tmp_1);
1079   __ freg2mem_opt(Z_ftos, faddress(_masm, Z_tmp_1), false);
1080 }
1081 
wide_dstore()1082 void TemplateTable::wide_dstore() {
1083   transition(vtos, vtos);
1084   __ pop_d(Z_ftos);
1085   locals_index_wide(Z_tmp_1);
1086   __ freg2mem_opt(Z_ftos, daddress(_masm, Z_tmp_1));
1087 }
1088 
wide_astore()1089 void TemplateTable::wide_astore() {
1090   transition(vtos, vtos);
1091   __ pop_ptr(Z_tos);
1092   locals_index_wide(Z_tmp_1);
1093   __ reg2mem_opt(Z_tos, aaddress(_masm, Z_tmp_1));
1094 }
1095 
iastore()1096 void TemplateTable::iastore() {
1097   transition(itos, vtos);
1098 
1099   Register index = Z_ARG3; // Index_check expects index in Z_ARG3.
1100   // Value is in Z_tos ...
1101   __ pop_i(index);        // index
1102   __ pop_ptr(Z_tmp_1);    // array
1103   index_check(Z_tmp_1, index, LogBytesPerInt);
1104   // ... and then move the value.
1105   __ reg2mem_opt(Z_tos,
1106                  Address(Z_tmp_1, index, arrayOopDesc::base_offset_in_bytes(T_INT)),
1107                  false);
1108 }
1109 
lastore()1110 void TemplateTable::lastore() {
1111   transition(ltos, vtos);
1112 
1113   __ pop_i(Z_ARG3);
1114   __ pop_ptr(Z_tmp_2);
1115   // Z_tos   : value
1116   // Z_ARG3  : index
1117   // Z_tmp_2 : array
1118  index_check(Z_tmp_2, Z_ARG3, LogBytesPerLong); // Prefer index in Z_ARG3.
1119   __ reg2mem_opt(Z_tos,
1120                  Address(Z_tmp_2, Z_ARG3, arrayOopDesc::base_offset_in_bytes(T_LONG)));
1121 }
1122 
fastore()1123 void TemplateTable::fastore() {
1124   transition(ftos, vtos);
1125 
1126   __ pop_i(Z_ARG3);
1127   __ pop_ptr(Z_tmp_2);
1128   // Z_ftos  : value
1129   // Z_ARG3  : index
1130   // Z_tmp_2 : array
1131   index_check(Z_tmp_2, Z_ARG3, LogBytesPerInt); // Prefer index in Z_ARG3.
1132   __ freg2mem_opt(Z_ftos,
1133                   Address(Z_tmp_2, Z_ARG3, arrayOopDesc::base_offset_in_bytes(T_FLOAT)),
1134                   false);
1135 }
1136 
dastore()1137 void TemplateTable::dastore() {
1138   transition(dtos, vtos);
1139 
1140   __ pop_i(Z_ARG3);
1141   __ pop_ptr(Z_tmp_2);
1142   // Z_ftos  : value
1143   // Z_ARG3  : index
1144   // Z_tmp_2 : array
1145   index_check(Z_tmp_2, Z_ARG3, LogBytesPerLong); // Prefer index in Z_ARG3.
1146   __ freg2mem_opt(Z_ftos,
1147                   Address(Z_tmp_2, Z_ARG3, arrayOopDesc::base_offset_in_bytes(T_DOUBLE)));
1148 }
1149 
aastore()1150 void TemplateTable::aastore() {
1151   NearLabel is_null, ok_is_subtype, done;
1152   transition(vtos, vtos);
1153 
1154   // stack: ..., array, index, value
1155 
1156   Register Rvalue = Z_tos;
1157   Register Rarray = Z_ARG2;
1158   Register Rindex = Z_ARG3; // Convention for index_check().
1159 
1160   __ load_ptr(0, Rvalue);
1161   __ z_l(Rindex, Address(Z_esp, Interpreter::expr_offset_in_bytes(1)));
1162   __ load_ptr(2, Rarray);
1163 
1164   unsigned const int shift = LogBytesPerHeapOop;
1165   index_check(Rarray, Rindex, shift); // side effect: Rindex = Rindex << shift
1166   Register Rstore_addr  = Rindex;
1167   // Address where the store goes to, i.e. &(Rarry[index])
1168   __ load_address(Rstore_addr, Address(Rarray, Rindex, arrayOopDesc::base_offset_in_bytes(T_OBJECT)));
1169 
1170   // do array store check - check for NULL value first.
1171   __ compareU64_and_branch(Rvalue, (intptr_t)0, Assembler::bcondEqual, is_null);
1172 
1173   Register Rsub_klass   = Z_ARG4;
1174   Register Rsuper_klass = Z_ARG5;
1175   __ load_klass(Rsub_klass, Rvalue);
1176   // Load superklass.
1177   __ load_klass(Rsuper_klass, Rarray);
1178   __ z_lg(Rsuper_klass, Address(Rsuper_klass, ObjArrayKlass::element_klass_offset()));
1179 
1180   // Generate a fast subtype check.  Branch to ok_is_subtype if no failure.
1181   // Throw if failure.
1182   Register tmp1 = Z_tmp_1;
1183   Register tmp2 = Z_tmp_2;
1184   __ gen_subtype_check(Rsub_klass, Rsuper_klass, tmp1, tmp2, ok_is_subtype);
1185 
1186   // Fall through on failure.
1187   // Object is in Rvalue == Z_tos.
1188   assert(Rvalue == Z_tos, "that's the expected location");
1189   __ load_absolute_address(tmp1, Interpreter::_throw_ArrayStoreException_entry);
1190   __ z_br(tmp1);
1191 
1192   Register tmp3 = Rsub_klass;
1193 
1194   // Have a NULL in Rvalue.
1195   __ bind(is_null);
1196   __ profile_null_seen(tmp1);
1197 
1198   // Store a NULL.
1199   do_oop_store(_masm, Address(Rstore_addr, (intptr_t)0), noreg,
1200                tmp3, tmp2, tmp1, IS_ARRAY);
1201   __ z_bru(done);
1202 
1203   // Come here on success.
1204   __ bind(ok_is_subtype);
1205 
1206   // Now store using the appropriate barrier.
1207   do_oop_store(_masm, Address(Rstore_addr, (intptr_t)0), Rvalue,
1208                tmp3, tmp2, tmp1, IS_ARRAY | IS_NOT_NULL);
1209 
1210   // Pop stack arguments.
1211   __ bind(done);
1212   __ add2reg(Z_esp, 3 * Interpreter::stackElementSize);
1213 }
1214 
1215 
bastore()1216 void TemplateTable::bastore() {
1217   transition(itos, vtos);
1218 
1219   __ pop_i(Z_ARG3);
1220   __ pop_ptr(Z_tmp_2);
1221   // Z_tos   : value
1222   // Z_ARG3  : index
1223   // Z_tmp_2 : array
1224 
1225   // Need to check whether array is boolean or byte
1226   // since both types share the bastore bytecode.
1227   __ load_klass(Z_tmp_1, Z_tmp_2);
1228   __ z_llgf(Z_tmp_1, Address(Z_tmp_1, Klass::layout_helper_offset()));
1229   __ z_tmll(Z_tmp_1, Klass::layout_helper_boolean_diffbit());
1230   Label L_skip;
1231   __ z_bfalse(L_skip);
1232   // if it is a T_BOOLEAN array, mask the stored value to 0/1
1233   __ z_nilf(Z_tos, 0x1);
1234   __ bind(L_skip);
1235 
1236   // No index shift necessary - pass 0.
1237   index_check(Z_tmp_2, Z_ARG3, 0); // Prefer index in Z_ARG3.
1238   __ z_stc(Z_tos,
1239            Address(Z_tmp_2, Z_ARG3, arrayOopDesc::base_offset_in_bytes(T_BYTE)));
1240 }
1241 
castore()1242 void TemplateTable::castore() {
1243   transition(itos, vtos);
1244 
1245   __ pop_i(Z_ARG3);
1246   __ pop_ptr(Z_tmp_2);
1247   // Z_tos   : value
1248   // Z_ARG3  : index
1249   // Z_tmp_2 : array
1250   Register index = Z_ARG3; // prefer index in Z_ARG3
1251   index_check(Z_tmp_2, index, LogBytesPerShort);
1252   __ z_sth(Z_tos,
1253            Address(Z_tmp_2, index, arrayOopDesc::base_offset_in_bytes(T_CHAR)));
1254 }
1255 
sastore()1256 void TemplateTable::sastore() {
1257   castore();
1258 }
1259 
istore(int n)1260 void TemplateTable::istore(int n) {
1261   transition(itos, vtos);
1262   __ reg2mem_opt(Z_tos, iaddress(n), false);
1263 }
1264 
lstore(int n)1265 void TemplateTable::lstore(int n) {
1266   transition(ltos, vtos);
1267   __ reg2mem_opt(Z_tos, laddress(n));
1268 }
1269 
fstore(int n)1270 void TemplateTable::fstore(int n) {
1271   transition(ftos, vtos);
1272   __ freg2mem_opt(Z_ftos, faddress(n), false);
1273 }
1274 
dstore(int n)1275 void TemplateTable::dstore(int n) {
1276   transition(dtos, vtos);
1277   __ freg2mem_opt(Z_ftos, daddress(n));
1278 }
1279 
astore(int n)1280 void TemplateTable::astore(int n) {
1281   transition(vtos, vtos);
1282   __ pop_ptr(Z_tos);
1283   __ reg2mem_opt(Z_tos, aaddress(n));
1284 }
1285 
pop()1286 void TemplateTable::pop() {
1287   transition(vtos, vtos);
1288   __ add2reg(Z_esp, Interpreter::stackElementSize);
1289 }
1290 
pop2()1291 void TemplateTable::pop2() {
1292   transition(vtos, vtos);
1293   __ add2reg(Z_esp, 2 * Interpreter::stackElementSize);
1294 }
1295 
dup()1296 void TemplateTable::dup() {
1297   transition(vtos, vtos);
1298   __ load_ptr(0, Z_tos);
1299   __ push_ptr(Z_tos);
1300   // stack: ..., a, a
1301 }
1302 
dup_x1()1303 void TemplateTable::dup_x1() {
1304   transition(vtos, vtos);
1305 
1306   // stack: ..., a, b
1307   __ load_ptr(0, Z_tos);          // load b
1308   __ load_ptr(1, Z_R0_scratch);   // load a
1309   __ store_ptr(1, Z_tos);         // store b
1310   __ store_ptr(0, Z_R0_scratch);  // store a
1311   __ push_ptr(Z_tos);             // push b
1312   // stack: ..., b, a, b
1313 }
1314 
dup_x2()1315 void TemplateTable::dup_x2() {
1316   transition(vtos, vtos);
1317 
1318   // stack: ..., a, b, c
1319   __ load_ptr(0, Z_R0_scratch);   // load c
1320   __ load_ptr(2, Z_R1_scratch);   // load a
1321   __ store_ptr(2, Z_R0_scratch);  // store c in a
1322   __ push_ptr(Z_R0_scratch);      // push c
1323   // stack: ..., c, b, c, c
1324   __ load_ptr(2, Z_R0_scratch);   // load b
1325   __ store_ptr(2, Z_R1_scratch);  // store a in b
1326   // stack: ..., c, a, c, c
1327   __ store_ptr(1, Z_R0_scratch);  // store b in c
1328   // stack: ..., c, a, b, c
1329 }
1330 
dup2()1331 void TemplateTable::dup2() {
1332   transition(vtos, vtos);
1333 
1334   // stack: ..., a, b
1335   __ load_ptr(1, Z_R0_scratch);  // load a
1336   __ push_ptr(Z_R0_scratch);     // push a
1337   __ load_ptr(1, Z_R0_scratch);  // load b
1338   __ push_ptr(Z_R0_scratch);     // push b
1339   // stack: ..., a, b, a, b
1340 }
1341 
dup2_x1()1342 void TemplateTable::dup2_x1() {
1343   transition(vtos, vtos);
1344 
1345   // stack: ..., a, b, c
1346   __ load_ptr(0, Z_R0_scratch);  // load c
1347   __ load_ptr(1, Z_R1_scratch);  // load b
1348   __ push_ptr(Z_R1_scratch);     // push b
1349   __ push_ptr(Z_R0_scratch);     // push c
1350   // stack: ..., a, b, c, b, c
1351   __ store_ptr(3, Z_R0_scratch); // store c in b
1352   // stack: ..., a, c, c, b, c
1353   __ load_ptr( 4, Z_R0_scratch); // load a
1354   __ store_ptr(2, Z_R0_scratch); // store a in 2nd c
1355   // stack: ..., a, c, a, b, c
1356   __ store_ptr(4, Z_R1_scratch); // store b in a
1357   // stack: ..., b, c, a, b, c
1358 }
1359 
dup2_x2()1360 void TemplateTable::dup2_x2() {
1361   transition(vtos, vtos);
1362 
1363   // stack: ..., a, b, c, d
1364   __ load_ptr(0, Z_R0_scratch);   // load d
1365   __ load_ptr(1, Z_R1_scratch);   // load c
1366   __ push_ptr(Z_R1_scratch);      // push c
1367   __ push_ptr(Z_R0_scratch);      // push d
1368   // stack: ..., a, b, c, d, c, d
1369   __ load_ptr(4, Z_R1_scratch);   // load b
1370   __ store_ptr(2, Z_R1_scratch);  // store b in d
1371   __ store_ptr(4, Z_R0_scratch);  // store d in b
1372   // stack: ..., a, d, c, b, c, d
1373   __ load_ptr(5, Z_R0_scratch);   // load a
1374   __ load_ptr(3, Z_R1_scratch);   // load c
1375   __ store_ptr(3, Z_R0_scratch);  // store a in c
1376   __ store_ptr(5, Z_R1_scratch);  // store c in a
1377   // stack: ..., c, d, a, b, c, d
1378 }
1379 
swap()1380 void TemplateTable::swap() {
1381   transition(vtos, vtos);
1382 
1383   // stack: ..., a, b
1384   __ load_ptr(1, Z_R0_scratch);  // load a
1385   __ load_ptr(0, Z_R1_scratch);  // load b
1386   __ store_ptr(0, Z_R0_scratch);  // store a in b
1387   __ store_ptr(1, Z_R1_scratch);  // store b in a
1388   // stack: ..., b, a
1389 }
1390 
iop2(Operation op)1391 void TemplateTable::iop2(Operation op) {
1392   transition(itos, itos);
1393   switch (op) {
1394     case add  :                           __ z_ay(Z_tos,  __ stackTop()); __ pop_i(); break;
1395     case sub  :                           __ z_sy(Z_tos,  __ stackTop()); __ pop_i(); __ z_lcr(Z_tos, Z_tos); break;
1396     case mul  :                           __ z_msy(Z_tos, __ stackTop()); __ pop_i(); break;
1397     case _and :                           __ z_ny(Z_tos,  __ stackTop()); __ pop_i(); break;
1398     case _or  :                           __ z_oy(Z_tos,  __ stackTop()); __ pop_i(); break;
1399     case _xor :                           __ z_xy(Z_tos,  __ stackTop()); __ pop_i(); break;
1400     case shl  : __ z_lr(Z_tmp_1, Z_tos);
1401                 __ z_nill(Z_tmp_1, 31);  // Lowest 5 bits are shiftamount.
1402                                           __ pop_i(Z_tos);   __ z_sll(Z_tos, 0,  Z_tmp_1); break;
1403     case shr  : __ z_lr(Z_tmp_1, Z_tos);
1404                 __ z_nill(Z_tmp_1, 31);  // Lowest 5 bits are shiftamount.
1405                                           __ pop_i(Z_tos);   __ z_sra(Z_tos, 0,  Z_tmp_1); break;
1406     case ushr : __ z_lr(Z_tmp_1, Z_tos);
1407                 __ z_nill(Z_tmp_1, 31);  // Lowest 5 bits are shiftamount.
1408                                           __ pop_i(Z_tos);   __ z_srl(Z_tos, 0,  Z_tmp_1); break;
1409     default   : ShouldNotReachHere(); break;
1410   }
1411   return;
1412 }
1413 
lop2(Operation op)1414 void TemplateTable::lop2(Operation op) {
1415   transition(ltos, ltos);
1416 
1417   switch (op) {
1418     case add  :  __ z_ag(Z_tos,  __ stackTop()); __ pop_l(); break;
1419     case sub  :  __ z_sg(Z_tos,  __ stackTop()); __ pop_l(); __ z_lcgr(Z_tos, Z_tos); break;
1420     case mul  :  __ z_msg(Z_tos, __ stackTop()); __ pop_l(); break;
1421     case _and :  __ z_ng(Z_tos,  __ stackTop()); __ pop_l(); break;
1422     case _or  :  __ z_og(Z_tos,  __ stackTop()); __ pop_l(); break;
1423     case _xor :  __ z_xg(Z_tos,  __ stackTop()); __ pop_l(); break;
1424     default   : ShouldNotReachHere(); break;
1425   }
1426   return;
1427 }
1428 
1429 // Common part of idiv/irem.
idiv_helper(InterpreterMacroAssembler * _masm,address exception)1430 static void idiv_helper(InterpreterMacroAssembler * _masm, address exception) {
1431   NearLabel not_null;
1432 
1433   // Use register pair Z_tmp_1, Z_tmp_2 for DIVIDE SINGLE.
1434   assert(Z_tmp_1->successor() == Z_tmp_2, " need even/odd register pair for idiv/irem");
1435 
1436   // Get dividend.
1437   __ pop_i(Z_tmp_2);
1438 
1439   // If divisor == 0 throw exception.
1440   __ compare32_and_branch(Z_tos, (intptr_t) 0,
1441                           Assembler::bcondNotEqual, not_null   );
1442   __ load_absolute_address(Z_R1_scratch, exception);
1443   __ z_br(Z_R1_scratch);
1444 
1445   __ bind(not_null);
1446 
1447   __ z_lgfr(Z_tmp_2, Z_tmp_2);   // Sign extend dividend.
1448   __ z_dsgfr(Z_tmp_1, Z_tos);    // Do it.
1449 }
1450 
idiv()1451 void TemplateTable::idiv() {
1452   transition(itos, itos);
1453 
1454   idiv_helper(_masm, Interpreter::_throw_ArithmeticException_entry);
1455   __ z_llgfr(Z_tos, Z_tmp_2);     // Result is in Z_tmp_2.
1456 }
1457 
irem()1458 void TemplateTable::irem() {
1459   transition(itos, itos);
1460 
1461   idiv_helper(_masm, Interpreter::_throw_ArithmeticException_entry);
1462   __ z_llgfr(Z_tos, Z_tmp_1);     // Result is in Z_tmp_1.
1463 }
1464 
lmul()1465 void TemplateTable::lmul() {
1466   transition(ltos, ltos);
1467 
1468   // Multiply with memory operand.
1469   __ z_msg(Z_tos, __ stackTop());
1470   __ pop_l();  // Pop operand.
1471 }
1472 
1473 // Common part of ldiv/lrem.
1474 //
1475 // Input:
1476 //     Z_tos := the divisor (dividend still on stack)
1477 //
1478 // Updated registers:
1479 //     Z_tmp_1 := pop_l() % Z_tos     ; if is_ldiv == false
1480 //     Z_tmp_2 := pop_l() / Z_tos     ; if is_ldiv == true
1481 //
ldiv_helper(InterpreterMacroAssembler * _masm,address exception,bool is_ldiv)1482 static void ldiv_helper(InterpreterMacroAssembler * _masm, address exception, bool is_ldiv) {
1483   NearLabel not_null, done;
1484 
1485   // Use register pair Z_tmp_1, Z_tmp_2 for DIVIDE SINGLE.
1486   assert(Z_tmp_1->successor() == Z_tmp_2,
1487          " need even/odd register pair for idiv/irem");
1488 
1489   // Get dividend.
1490   __ pop_l(Z_tmp_2);
1491 
1492   // If divisor == 0 throw exception.
1493   __ compare64_and_branch(Z_tos, (intptr_t)0, Assembler::bcondNotEqual, not_null);
1494   __ load_absolute_address(Z_R1_scratch, exception);
1495   __ z_br(Z_R1_scratch);
1496 
1497   __ bind(not_null);
1498   // Special case for dividend == 0x8000 and divisor == -1.
1499   if (is_ldiv) {
1500     // result := Z_tmp_2 := - dividend
1501     __ z_lcgr(Z_tmp_2, Z_tmp_2);
1502   } else {
1503     // result remainder := Z_tmp_1 := 0
1504     __ clear_reg(Z_tmp_1, true, false);  // Don't set CC.
1505   }
1506 
1507   // if divisor == -1 goto done
1508   __ compare64_and_branch(Z_tos, -1, Assembler::bcondEqual, done);
1509   if (is_ldiv)
1510     // Restore sign, because divisor != -1.
1511     __ z_lcgr(Z_tmp_2, Z_tmp_2);
1512   __ z_dsgr(Z_tmp_1, Z_tos);    // Do it.
1513   __ bind(done);
1514 }
1515 
ldiv()1516 void TemplateTable::ldiv() {
1517   transition(ltos, ltos);
1518 
1519   ldiv_helper(_masm, Interpreter::_throw_ArithmeticException_entry, true /*is_ldiv*/);
1520   __ z_lgr(Z_tos, Z_tmp_2);     // Result is in Z_tmp_2.
1521 }
1522 
lrem()1523 void TemplateTable::lrem() {
1524   transition(ltos, ltos);
1525 
1526   ldiv_helper(_masm, Interpreter::_throw_ArithmeticException_entry, false /*is_ldiv*/);
1527   __ z_lgr(Z_tos, Z_tmp_1);     // Result is in Z_tmp_1.
1528 }
1529 
lshl()1530 void TemplateTable::lshl() {
1531   transition(itos, ltos);
1532 
1533   // Z_tos: shift amount
1534   __ pop_l(Z_tmp_1);              // Get shift value.
1535   __ z_sllg(Z_tos, Z_tmp_1, 0, Z_tos);
1536 }
1537 
lshr()1538 void TemplateTable::lshr() {
1539   transition(itos, ltos);
1540 
1541   // Z_tos: shift amount
1542   __ pop_l(Z_tmp_1);              // Get shift value.
1543   __ z_srag(Z_tos, Z_tmp_1, 0, Z_tos);
1544 }
1545 
lushr()1546 void TemplateTable::lushr() {
1547   transition(itos, ltos);
1548 
1549   // Z_tos: shift amount
1550   __ pop_l(Z_tmp_1);              // Get shift value.
1551   __ z_srlg(Z_tos, Z_tmp_1, 0, Z_tos);
1552 }
1553 
fop2(Operation op)1554 void TemplateTable::fop2(Operation op) {
1555   transition(ftos, ftos);
1556 
1557   switch (op) {
1558     case add:
1559       // Add memory operand.
1560       __ z_aeb(Z_ftos, __ stackTop()); __ pop_f(); return;
1561     case sub:
1562       // Sub memory operand.
1563       __ z_ler(Z_F1, Z_ftos);    // first operand
1564       __ pop_f(Z_ftos);          // second operand from stack
1565       __ z_sebr(Z_ftos, Z_F1);
1566       return;
1567     case mul:
1568       // Multiply with memory operand.
1569       __ z_meeb(Z_ftos, __ stackTop()); __ pop_f(); return;
1570     case div:
1571       __ z_ler(Z_F1, Z_ftos);    // first operand
1572       __ pop_f(Z_ftos);          // second operand from stack
1573       __ z_debr(Z_ftos, Z_F1);
1574       return;
1575     case rem:
1576       // Do runtime call.
1577       __ z_ler(Z_FARG2, Z_ftos);  // divisor
1578       __ pop_f(Z_FARG1);          // dividend
1579       __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::frem));
1580       // Result should be in the right place (Z_ftos == Z_FRET).
1581       return;
1582     default:
1583       ShouldNotReachHere();
1584       return;
1585   }
1586 }
1587 
dop2(Operation op)1588 void TemplateTable::dop2(Operation op) {
1589   transition(dtos, dtos);
1590 
1591   switch (op) {
1592     case add:
1593       // Add memory operand.
1594       __ z_adb(Z_ftos, __ stackTop()); __ pop_d(); return;
1595     case sub:
1596       // Sub memory operand.
1597       __ z_ldr(Z_F1, Z_ftos);    // first operand
1598       __ pop_d(Z_ftos);          // second operand from stack
1599       __ z_sdbr(Z_ftos, Z_F1);
1600       return;
1601     case mul:
1602       // Multiply with memory operand.
1603       __ z_mdb(Z_ftos, __ stackTop()); __ pop_d(); return;
1604     case div:
1605       __ z_ldr(Z_F1, Z_ftos);    // first operand
1606       __ pop_d(Z_ftos);          // second operand from stack
1607       __ z_ddbr(Z_ftos, Z_F1);
1608       return;
1609     case rem:
1610       // Do runtime call.
1611       __ z_ldr(Z_FARG2, Z_ftos);  // divisor
1612       __ pop_d(Z_FARG1);          // dividend
1613       __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::drem));
1614       // Result should be in the right place (Z_ftos == Z_FRET).
1615       return;
1616     default:
1617       ShouldNotReachHere();
1618       return;
1619   }
1620 }
1621 
ineg()1622 void TemplateTable::ineg() {
1623   transition(itos, itos);
1624   __ z_lcr(Z_tos);
1625 }
1626 
lneg()1627 void TemplateTable::lneg() {
1628   transition(ltos, ltos);
1629   __ z_lcgr(Z_tos);
1630 }
1631 
fneg()1632 void TemplateTable::fneg() {
1633   transition(ftos, ftos);
1634   __ z_lcebr(Z_ftos, Z_ftos);
1635 }
1636 
dneg()1637 void TemplateTable::dneg() {
1638   transition(dtos, dtos);
1639   __ z_lcdbr(Z_ftos, Z_ftos);
1640 }
1641 
iinc()1642 void TemplateTable::iinc() {
1643   transition(vtos, vtos);
1644 
1645   Address local;
1646   __ z_lb(Z_R0_scratch, at_bcp(2)); // Get constant.
1647   locals_index(Z_R1_scratch);
1648   local = iaddress(_masm, Z_R1_scratch);
1649   __ z_a(Z_R0_scratch, local);
1650   __ reg2mem_opt(Z_R0_scratch, local, false);
1651 }
1652 
wide_iinc()1653 void TemplateTable::wide_iinc() {
1654   transition(vtos, vtos);
1655 
1656   // Z_tmp_1 := increment
1657   __ get_2_byte_integer_at_bcp(Z_tmp_1, 4, InterpreterMacroAssembler::Signed);
1658   // Z_R1_scratch := index of local to increment
1659   locals_index_wide(Z_tmp_2);
1660   // Load, increment, and store.
1661   __ access_local_int(Z_tmp_2, Z_tos);
1662   __ z_agr(Z_tos,  Z_tmp_1);
1663   // Shifted index is still in Z_tmp_2.
1664   __ reg2mem_opt(Z_tos, Address(Z_locals, Z_tmp_2), false);
1665 }
1666 
1667 
convert()1668 void TemplateTable::convert() {
1669   // Checking
1670 #ifdef ASSERT
1671   TosState   tos_in  = ilgl;
1672   TosState   tos_out = ilgl;
1673 
1674   switch (bytecode()) {
1675     case Bytecodes::_i2l:
1676     case Bytecodes::_i2f:
1677     case Bytecodes::_i2d:
1678     case Bytecodes::_i2b:
1679     case Bytecodes::_i2c:
1680     case Bytecodes::_i2s:
1681       tos_in = itos;
1682       break;
1683     case Bytecodes::_l2i:
1684     case Bytecodes::_l2f:
1685     case Bytecodes::_l2d:
1686       tos_in = ltos;
1687       break;
1688     case Bytecodes::_f2i:
1689     case Bytecodes::_f2l:
1690     case Bytecodes::_f2d:
1691       tos_in = ftos;
1692       break;
1693     case Bytecodes::_d2i:
1694     case Bytecodes::_d2l:
1695     case Bytecodes::_d2f:
1696       tos_in = dtos;
1697       break;
1698     default :
1699       ShouldNotReachHere();
1700   }
1701   switch (bytecode()) {
1702     case Bytecodes::_l2i:
1703     case Bytecodes::_f2i:
1704     case Bytecodes::_d2i:
1705     case Bytecodes::_i2b:
1706     case Bytecodes::_i2c:
1707     case Bytecodes::_i2s:
1708       tos_out = itos;
1709       break;
1710     case Bytecodes::_i2l:
1711     case Bytecodes::_f2l:
1712     case Bytecodes::_d2l:
1713       tos_out = ltos;
1714       break;
1715     case Bytecodes::_i2f:
1716     case Bytecodes::_l2f:
1717     case Bytecodes::_d2f:
1718       tos_out = ftos;
1719       break;
1720     case Bytecodes::_i2d:
1721     case Bytecodes::_l2d:
1722     case Bytecodes::_f2d:
1723       tos_out = dtos;
1724       break;
1725     default :
1726       ShouldNotReachHere();
1727   }
1728 
1729   transition(tos_in, tos_out);
1730 #endif // ASSERT
1731 
1732   // Conversion
1733   Label done;
1734   switch (bytecode()) {
1735     case Bytecodes::_i2l:
1736       __ z_lgfr(Z_tos, Z_tos);
1737       return;
1738     case Bytecodes::_i2f:
1739       __ z_cefbr(Z_ftos, Z_tos);
1740       return;
1741     case Bytecodes::_i2d:
1742       __ z_cdfbr(Z_ftos, Z_tos);
1743       return;
1744     case Bytecodes::_i2b:
1745       // Sign extend least significant byte.
1746       __ move_reg_if_needed(Z_tos, T_BYTE, Z_tos, T_INT);
1747       return;
1748     case Bytecodes::_i2c:
1749       // Zero extend 2 least significant bytes.
1750       __ move_reg_if_needed(Z_tos, T_CHAR, Z_tos, T_INT);
1751       return;
1752     case Bytecodes::_i2s:
1753       // Sign extend 2 least significant bytes.
1754       __ move_reg_if_needed(Z_tos, T_SHORT, Z_tos, T_INT);
1755       return;
1756     case Bytecodes::_l2i:
1757       // Sign-extend not needed here, upper 4 bytes of int value in register are ignored.
1758       return;
1759     case Bytecodes::_l2f:
1760       __ z_cegbr(Z_ftos, Z_tos);
1761       return;
1762     case Bytecodes::_l2d:
1763       __ z_cdgbr(Z_ftos, Z_tos);
1764       return;
1765     case Bytecodes::_f2i:
1766     case Bytecodes::_f2l:
1767       __ clear_reg(Z_tos, true, false);  // Don't set CC.
1768       __ z_cebr(Z_ftos, Z_ftos);
1769       __ z_brno(done); // NaN -> 0
1770       if (bytecode() == Bytecodes::_f2i)
1771         __ z_cfebr(Z_tos, Z_ftos, Assembler::to_zero);
1772       else // bytecode() == Bytecodes::_f2l
1773         __ z_cgebr(Z_tos, Z_ftos, Assembler::to_zero);
1774       break;
1775     case Bytecodes::_f2d:
1776       __ move_freg_if_needed(Z_ftos, T_DOUBLE, Z_ftos, T_FLOAT);
1777       return;
1778     case Bytecodes::_d2i:
1779     case Bytecodes::_d2l:
1780       __ clear_reg(Z_tos, true, false);  // Ddon't set CC.
1781       __ z_cdbr(Z_ftos, Z_ftos);
1782       __ z_brno(done); // NaN -> 0
1783       if (bytecode() == Bytecodes::_d2i)
1784         __ z_cfdbr(Z_tos, Z_ftos, Assembler::to_zero);
1785       else // Bytecodes::_d2l
1786         __ z_cgdbr(Z_tos, Z_ftos, Assembler::to_zero);
1787       break;
1788     case Bytecodes::_d2f:
1789       __ move_freg_if_needed(Z_ftos, T_FLOAT, Z_ftos, T_DOUBLE);
1790       return;
1791     default:
1792       ShouldNotReachHere();
1793   }
1794   __ bind(done);
1795 }
1796 
lcmp()1797 void TemplateTable::lcmp() {
1798   transition(ltos, itos);
1799 
1800   Label   done;
1801   Register val1 = Z_R0_scratch;
1802   Register val2 = Z_R1_scratch;
1803 
1804   if (VM_Version::has_LoadStoreConditional()) {
1805     __ pop_l(val1);           // pop value 1.
1806     __ z_lghi(val2,  -1);     // lt value
1807     __ z_cgr(val1, Z_tos);    // Compare with Z_tos (value 2). Protect CC under all circumstances.
1808     __ z_lghi(val1,   1);     // gt value
1809     __ z_lghi(Z_tos,  0);     // eq value
1810 
1811     __ z_locgr(Z_tos, val1, Assembler::bcondHigh);
1812     __ z_locgr(Z_tos, val2, Assembler::bcondLow);
1813   } else {
1814     __ pop_l(val1);           // Pop value 1.
1815     __ z_cgr(val1, Z_tos);    // Compare with Z_tos (value 2). Protect CC under all circumstances.
1816 
1817     __ z_lghi(Z_tos,  0);     // eq value
1818     __ z_bre(done);
1819 
1820     __ z_lghi(Z_tos,  1);     // gt value
1821     __ z_brh(done);
1822 
1823     __ z_lghi(Z_tos, -1);     // lt value
1824   }
1825 
1826   __ bind(done);
1827 }
1828 
1829 
float_cmp(bool is_float,int unordered_result)1830 void TemplateTable::float_cmp(bool is_float, int unordered_result) {
1831   Label done;
1832 
1833   if (is_float) {
1834     __ pop_f(Z_FARG2);
1835     __ z_cebr(Z_FARG2, Z_ftos);
1836   } else {
1837     __ pop_d(Z_FARG2);
1838     __ z_cdbr(Z_FARG2, Z_ftos);
1839   }
1840 
1841   if (VM_Version::has_LoadStoreConditional()) {
1842     Register one       = Z_R0_scratch;
1843     Register minus_one = Z_R1_scratch;
1844     __ z_lghi(minus_one,  -1);
1845     __ z_lghi(one,  1);
1846     __ z_lghi(Z_tos, 0);
1847     __ z_locgr(Z_tos, one,       unordered_result == 1 ? Assembler::bcondHighOrNotOrdered : Assembler::bcondHigh);
1848     __ z_locgr(Z_tos, minus_one, unordered_result == 1 ? Assembler::bcondLow              : Assembler::bcondLowOrNotOrdered);
1849   } else {
1850     // Z_FARG2 == Z_ftos
1851     __ clear_reg(Z_tos, false, false);
1852     __ z_bre(done);
1853 
1854     // F_ARG2 > Z_Ftos, or unordered
1855     __ z_lhi(Z_tos, 1);
1856     __ z_brc(unordered_result == 1 ? Assembler::bcondHighOrNotOrdered : Assembler::bcondHigh, done);
1857 
1858     // F_ARG2 < Z_FTOS, or unordered
1859     __ z_lhi(Z_tos, -1);
1860 
1861     __ bind(done);
1862   }
1863 }
1864 
branch(bool is_jsr,bool is_wide)1865 void TemplateTable::branch(bool is_jsr, bool is_wide) {
1866   const Register   bumped_count = Z_tmp_1;
1867   const Register   method       = Z_tmp_2;
1868   const Register   m_counters   = Z_R1_scratch;
1869   const Register   mdo          = Z_tos;
1870 
1871   BLOCK_COMMENT("TemplateTable::branch {");
1872   __ get_method(method);
1873   __ profile_taken_branch(mdo, bumped_count);
1874 
1875   const ByteSize ctr_offset = InvocationCounter::counter_offset();
1876   const ByteSize be_offset  = MethodCounters::backedge_counter_offset()   + ctr_offset;
1877   const ByteSize inv_offset = MethodCounters::invocation_counter_offset() + ctr_offset;
1878 
1879   // Get (wide) offset to disp.
1880   const Register disp = Z_ARG5;
1881   if (is_wide) {
1882     __ get_4_byte_integer_at_bcp(disp, 1);
1883   } else {
1884     __ get_2_byte_integer_at_bcp(disp, 1, InterpreterMacroAssembler::Signed);
1885   }
1886 
1887   // Handle all the JSR stuff here, then exit.
1888   // It's much shorter and cleaner than intermingling with the
1889   // non-JSR normal-branch stuff occurring below.
1890   if (is_jsr) {
1891     // Compute return address as bci in Z_tos.
1892     __ z_lgr(Z_R1_scratch, Z_bcp);
1893     __ z_sg(Z_R1_scratch, Address(method, Method::const_offset()));
1894     __ add2reg(Z_tos, (is_wide ? 5 : 3) - in_bytes(ConstMethod::codes_offset()), Z_R1_scratch);
1895 
1896     // Bump bcp to target of JSR.
1897     __ z_agr(Z_bcp, disp);
1898     // Push return address for "ret" on stack.
1899     __ push_ptr(Z_tos);
1900     // And away we go!
1901     __ dispatch_next(vtos, 0 , true);
1902     return;
1903   }
1904 
1905   // Normal (non-jsr) branch handling.
1906 
1907   // Bump bytecode pointer by displacement (take the branch).
1908   __ z_agr(Z_bcp, disp);
1909 
1910   assert(UseLoopCounter || !UseOnStackReplacement,
1911          "on-stack-replacement requires loop counters");
1912 
1913   NearLabel backedge_counter_overflow;
1914   NearLabel dispatch;
1915   int       increment = InvocationCounter::count_increment;
1916 
1917   if (UseLoopCounter) {
1918     // Increment backedge counter for backward branches.
1919     // disp: target offset
1920     // Z_bcp: target bcp
1921     // Z_locals: locals pointer
1922     //
1923     // Count only if backward branch.
1924     __ compare32_and_branch(disp, (intptr_t)0, Assembler::bcondHigh, dispatch);
1925 
1926 
1927     if (ProfileInterpreter) {
1928       NearLabel   no_mdo;
1929 
1930       // Are we profiling?
1931       __ load_and_test_long(mdo, Address(method, Method::method_data_offset()));
1932       __ branch_optimized(Assembler::bcondZero, no_mdo);
1933 
1934       // Increment the MDO backedge counter.
1935       const Address mdo_backedge_counter(mdo, MethodData::backedge_counter_offset() + InvocationCounter::counter_offset());
1936 
1937       const Address mask(mdo, MethodData::backedge_mask_offset());
1938       __ increment_mask_and_jump(mdo_backedge_counter, increment, mask,
1939                                  Z_ARG2, false, Assembler::bcondZero,
1940                                  UseOnStackReplacement ? &backedge_counter_overflow : NULL);
1941       __ z_bru(dispatch);
1942       __ bind(no_mdo);
1943     }
1944 
1945     // Increment backedge counter in MethodCounters*.
1946     __ get_method_counters(method, m_counters, dispatch);
1947     const Address mask(m_counters, MethodCounters::backedge_mask_offset());
1948     __ increment_mask_and_jump(Address(m_counters, be_offset),
1949                                increment, mask,
1950                                Z_ARG2, false, Assembler::bcondZero,
1951                                UseOnStackReplacement ? &backedge_counter_overflow : NULL);
1952     __ bind(dispatch);
1953   }
1954 
1955   // Pre-load the next target bytecode into rbx.
1956   __ z_llgc(Z_bytecode, Address(Z_bcp, (intptr_t) 0));
1957 
1958   // Continue with the bytecode @ target.
1959   // Z_tos: Return bci for jsr's, unused otherwise.
1960   // Z_bytecode: target bytecode
1961   // Z_bcp: target bcp
1962   __ dispatch_only(vtos, true);
1963 
1964   // Out-of-line code runtime calls.
1965   if (UseLoopCounter && UseOnStackReplacement) {
1966     // invocation counter overflow
1967     __ bind(backedge_counter_overflow);
1968 
1969     __ z_lcgr(Z_ARG2, disp); // Z_ARG2 := -disp
1970     __ z_agr(Z_ARG2, Z_bcp); // Z_ARG2 := branch target bcp - disp == branch bcp
1971     __ call_VM(noreg,
1972                CAST_FROM_FN_PTR(address, InterpreterRuntime::frequency_counter_overflow),
1973                Z_ARG2);
1974 
1975     // Z_RET: osr nmethod (osr ok) or NULL (osr not possible).
1976     __ compare64_and_branch(Z_RET, (intptr_t) 0, Assembler::bcondEqual, dispatch);
1977 
1978     // Nmethod may have been invalidated (VM may block upon call_VM return).
1979     __ z_cliy(nmethod::state_offset(), Z_RET, nmethod::in_use);
1980     __ z_brne(dispatch);
1981 
1982     // Migrate the interpreter frame off of the stack.
1983 
1984     __ z_lgr(Z_tmp_1, Z_RET); // Save the nmethod.
1985 
1986     call_VM(noreg,
1987             CAST_FROM_FN_PTR(address, SharedRuntime::OSR_migration_begin));
1988 
1989     // Z_RET is OSR buffer, move it to expected parameter location.
1990     __ lgr_if_needed(Z_ARG1, Z_RET);
1991 
1992     // Pop the interpreter frame ...
1993     __ pop_interpreter_frame(Z_R14, Z_ARG2/*tmp1*/, Z_ARG3/*tmp2*/);
1994 
1995     // ... and begin the OSR nmethod.
1996     __ z_lg(Z_R1_scratch, Address(Z_tmp_1, nmethod::osr_entry_point_offset()));
1997     __ z_br(Z_R1_scratch);
1998   }
1999   BLOCK_COMMENT("} TemplateTable::branch");
2000 }
2001 
if_0cmp(Condition cc)2002 void TemplateTable::if_0cmp(Condition cc) {
2003   transition(itos, vtos);
2004 
2005   // Assume branch is more often taken than not (loops use backward branches).
2006   NearLabel not_taken;
2007   __ compare32_and_branch(Z_tos, (intptr_t) 0, j_not(cc), not_taken);
2008   branch(false, false);
2009   __ bind(not_taken);
2010   __ profile_not_taken_branch(Z_tos);
2011 }
2012 
if_icmp(Condition cc)2013 void TemplateTable::if_icmp(Condition cc) {
2014   transition(itos, vtos);
2015 
2016   // Assume branch is more often taken than not (loops use backward branches).
2017   NearLabel not_taken;
2018   __ pop_i(Z_R0_scratch);
2019   __ compare32_and_branch(Z_R0_scratch, Z_tos, j_not(cc), not_taken);
2020   branch(false, false);
2021   __ bind(not_taken);
2022   __ profile_not_taken_branch(Z_tos);
2023 }
2024 
if_nullcmp(Condition cc)2025 void TemplateTable::if_nullcmp(Condition cc) {
2026   transition(atos, vtos);
2027 
2028   // Assume branch is more often taken than not (loops use backward branches) .
2029   NearLabel not_taken;
2030   __ compare64_and_branch(Z_tos, (intptr_t) 0, j_not(cc), not_taken);
2031   branch(false, false);
2032   __ bind(not_taken);
2033   __ profile_not_taken_branch(Z_tos);
2034 }
2035 
if_acmp(Condition cc)2036 void TemplateTable::if_acmp(Condition cc) {
2037   transition(atos, vtos);
2038   // Assume branch is more often taken than not (loops use backward branches).
2039   NearLabel not_taken;
2040   __ pop_ptr(Z_ARG2);
2041   __ verify_oop(Z_ARG2);
2042   __ verify_oop(Z_tos);
2043   __ compareU64_and_branch(Z_tos, Z_ARG2, j_not(cc), not_taken);
2044   branch(false, false);
2045   __ bind(not_taken);
2046   __ profile_not_taken_branch(Z_ARG3);
2047 }
2048 
ret()2049 void TemplateTable::ret() {
2050   transition(vtos, vtos);
2051 
2052   locals_index(Z_tmp_1);
2053   // Get return bci, compute return bcp. Must load 64 bits.
2054   __ mem2reg_opt(Z_tmp_1, iaddress(_masm, Z_tmp_1));
2055   __ profile_ret(Z_tmp_1, Z_tmp_2);
2056   __ get_method(Z_tos);
2057   __ mem2reg_opt(Z_R1_scratch, Address(Z_tos, Method::const_offset()));
2058   __ load_address(Z_bcp, Address(Z_R1_scratch, Z_tmp_1, ConstMethod::codes_offset()));
2059   __ dispatch_next(vtos, 0 , true);
2060 }
2061 
wide_ret()2062 void TemplateTable::wide_ret() {
2063   transition(vtos, vtos);
2064 
2065   locals_index_wide(Z_tmp_1);
2066   // Get return bci, compute return bcp.
2067   __ mem2reg_opt(Z_tmp_1, aaddress(_masm, Z_tmp_1));
2068   __ profile_ret(Z_tmp_1, Z_tmp_2);
2069   __ get_method(Z_tos);
2070   __ mem2reg_opt(Z_R1_scratch, Address(Z_tos, Method::const_offset()));
2071   __ load_address(Z_bcp, Address(Z_R1_scratch, Z_tmp_1, ConstMethod::codes_offset()));
2072   __ dispatch_next(vtos, 0, true);
2073 }
2074 
tableswitch()2075 void TemplateTable::tableswitch () {
2076   transition(itos, vtos);
2077 
2078   NearLabel default_case, continue_execution;
2079   Register  bcp = Z_ARG5;
2080   // Align bcp.
2081   __ load_address(bcp, at_bcp(BytesPerInt));
2082   __ z_nill(bcp, (-BytesPerInt) & 0xffff);
2083 
2084   // Load lo & hi.
2085   Register low  = Z_tmp_1;
2086   Register high = Z_tmp_2;
2087 
2088   // Load low into 64 bits, since used for address calculation.
2089   __ mem2reg_signed_opt(low, Address(bcp, BytesPerInt));
2090   __ mem2reg_opt(high, Address(bcp, 2 * BytesPerInt), false);
2091   // Sign extend "label" value for address calculation.
2092   __ z_lgfr(Z_tos, Z_tos);
2093 
2094   // Check against lo & hi.
2095   __ compare32_and_branch(Z_tos, low, Assembler::bcondLow, default_case);
2096   __ compare32_and_branch(Z_tos, high, Assembler::bcondHigh, default_case);
2097 
2098   // Lookup dispatch offset.
2099   __ z_sgr(Z_tos, low);
2100   Register jump_table_offset = Z_ARG3;
2101   // Index2offset; index in Z_tos is killed by profile_switch_case.
2102   __ z_sllg(jump_table_offset, Z_tos, LogBytesPerInt);
2103   __ profile_switch_case(Z_tos, Z_ARG4 /*tmp for mdp*/, low/*tmp*/, Z_bytecode/*tmp*/);
2104 
2105   Register index = Z_tmp_2;
2106 
2107   // Load index sign extended for addressing.
2108   __ mem2reg_signed_opt(index, Address(bcp, jump_table_offset, 3 * BytesPerInt));
2109 
2110   // Continue execution.
2111   __ bind(continue_execution);
2112 
2113   // Load next bytecode.
2114   __ z_llgc(Z_bytecode, Address(Z_bcp, index));
2115   __ z_agr(Z_bcp, index); // Advance bcp.
2116   __ dispatch_only(vtos, true);
2117 
2118   // Handle default.
2119   __ bind(default_case);
2120 
2121   __ profile_switch_default(Z_tos);
2122   __ mem2reg_signed_opt(index, Address(bcp));
2123   __ z_bru(continue_execution);
2124 }
2125 
lookupswitch()2126 void TemplateTable::lookupswitch () {
2127   transition(itos, itos);
2128   __ stop("lookupswitch bytecode should have been rewritten");
2129 }
2130 
fast_linearswitch()2131 void TemplateTable::fast_linearswitch () {
2132   transition(itos, vtos);
2133 
2134   Label    loop_entry, loop, found, continue_execution;
2135   Register bcp = Z_ARG5;
2136 
2137   // Align bcp.
2138   __ load_address(bcp, at_bcp(BytesPerInt));
2139   __ z_nill(bcp, (-BytesPerInt) & 0xffff);
2140 
2141   // Start search with last case.
2142   Register current_case_offset = Z_tmp_1;
2143 
2144   __ mem2reg_signed_opt(current_case_offset, Address(bcp, BytesPerInt));
2145   __ z_sllg(current_case_offset, current_case_offset, LogBytesPerWord);   // index2bytes
2146   __ z_bru(loop_entry);
2147 
2148   // table search
2149   __ bind(loop);
2150 
2151   __ z_c(Z_tos, Address(bcp, current_case_offset, 2 * BytesPerInt));
2152   __ z_bre(found);
2153 
2154   __ bind(loop_entry);
2155   __ z_aghi(current_case_offset, -2 * BytesPerInt);  // Decrement.
2156   __ z_brnl(loop);
2157 
2158   // default case
2159   Register   offset = Z_tmp_2;
2160 
2161   __ profile_switch_default(Z_tos);
2162   // Load offset sign extended for addressing.
2163   __ mem2reg_signed_opt(offset, Address(bcp));
2164   __ z_bru(continue_execution);
2165 
2166   // Entry found -> get offset.
2167   __ bind(found);
2168   __ mem2reg_signed_opt(offset, Address(bcp, current_case_offset, 3 * BytesPerInt));
2169   // Profile that this case was taken.
2170   Register current_case_idx = Z_ARG4;
2171   __ z_srlg(current_case_idx, current_case_offset, LogBytesPerWord); // bytes2index
2172   __ profile_switch_case(current_case_idx, Z_tos, bcp, Z_bytecode);
2173 
2174   // Continue execution.
2175   __ bind(continue_execution);
2176 
2177   // Load next bytecode.
2178   __ z_llgc(Z_bytecode, Address(Z_bcp, offset, 0));
2179   __ z_agr(Z_bcp, offset); // Advance bcp.
2180   __ dispatch_only(vtos, true);
2181 }
2182 
2183 
fast_binaryswitch()2184 void TemplateTable::fast_binaryswitch() {
2185 
2186   transition(itos, vtos);
2187 
2188   // Implementation using the following core algorithm:
2189   //
2190   // int binary_search(int key, LookupswitchPair* array, int n) {
2191   //   // Binary search according to "Methodik des Programmierens" by
2192   //   // Edsger W. Dijkstra and W.H.J. Feijen, Addison Wesley Germany 1985.
2193   //   int i = 0;
2194   //   int j = n;
2195   //   while (i+1 < j) {
2196   //     // invariant P: 0 <= i < j <= n and (a[i] <= key < a[j] or Q)
2197   //     // with      Q: for all i: 0 <= i < n: key < a[i]
2198   //     // where a stands for the array and assuming that the (inexisting)
2199   //     // element a[n] is infinitely big.
2200   //     int h = (i + j) >> 1;
2201   //     // i < h < j
2202   //     if (key < array[h].fast_match()) {
2203   //       j = h;
2204   //     } else {
2205   //       i = h;
2206   //     }
2207   //   }
2208   //   // R: a[i] <= key < a[i+1] or Q
2209   //   // (i.e., if key is within array, i is the correct index)
2210   //   return i;
2211   // }
2212 
2213   // Register allocation
2214   // Note: Since we use the indices in address operands, we do all the
2215   // computation in 64 bits.
2216   const Register key   = Z_tos; // Already set (tosca).
2217   const Register array = Z_tmp_1;
2218   const Register i     = Z_tmp_2;
2219   const Register j     = Z_ARG5;
2220   const Register h     = Z_ARG4;
2221   const Register temp  = Z_R1_scratch;
2222 
2223   // Find array start.
2224   __ load_address(array, at_bcp(3 * BytesPerInt));
2225   __ z_nill(array, (-BytesPerInt) & 0xffff);   // align
2226 
2227   // Initialize i & j.
2228   __ clear_reg(i, true, false);  // i = 0;  Don't set CC.
2229   __ mem2reg_signed_opt(j, Address(array, -BytesPerInt)); // j = length(array);
2230 
2231   // And start.
2232   Label entry;
2233   __ z_bru(entry);
2234 
2235   // binary search loop
2236   {
2237     NearLabel   loop;
2238 
2239     __ bind(loop);
2240 
2241     // int h = (i + j) >> 1;
2242     __ add2reg_with_index(h, 0, i, j); // h = i + j;
2243     __ z_srag(h, h, 1);                // h = (i + j) >> 1;
2244 
2245     // if (key < array[h].fast_match()) {
2246     //   j = h;
2247     // } else {
2248     //   i = h;
2249     // }
2250 
2251     // Convert array[h].match to native byte-ordering before compare.
2252     __ z_sllg(temp, h, LogBytesPerWord);   // index2bytes
2253     __ mem2reg_opt(temp, Address(array, temp), false);
2254 
2255     NearLabel  else_;
2256 
2257     __ compare32_and_branch(key, temp, Assembler::bcondNotLow, else_);
2258     // j = h if (key <  array[h].fast_match())
2259     __ z_lgr(j, h);
2260     __ z_bru(entry); // continue
2261 
2262     __ bind(else_);
2263 
2264     // i = h if (key >= array[h].fast_match())
2265     __ z_lgr(i, h);  // and fallthrough
2266 
2267     // while (i+1 < j)
2268     __ bind(entry);
2269 
2270     // if (i + 1 < j) continue search
2271     __ add2reg(h, 1, i);
2272     __ compare64_and_branch(h, j, Assembler::bcondLow, loop);
2273   }
2274 
2275   // End of binary search, result index is i (must check again!).
2276   NearLabel default_case;
2277 
2278   // h is no longer needed, so use it to hold the byte offset.
2279   __ z_sllg(h, i, LogBytesPerWord);   // index2bytes
2280   __ mem2reg_opt(temp, Address(array, h), false);
2281   __ compare32_and_branch(key, temp, Assembler::bcondNotEqual, default_case);
2282 
2283   // entry found -> j = offset
2284   __ mem2reg_signed_opt(j, Address(array, h, BytesPerInt));
2285   __ profile_switch_case(i, key, array, Z_bytecode);
2286   // Load next bytecode.
2287   __ z_llgc(Z_bytecode, Address(Z_bcp, j));
2288   __ z_agr(Z_bcp, j);       // Advance bcp.
2289   __ dispatch_only(vtos, true);
2290 
2291   // default case -> j = default offset
2292   __ bind(default_case);
2293 
2294   __ profile_switch_default(i);
2295   __ mem2reg_signed_opt(j, Address(array, -2 * BytesPerInt));
2296   // Load next bytecode.
2297   __ z_llgc(Z_bytecode, Address(Z_bcp, j));
2298   __ z_agr(Z_bcp, j);       // Advance bcp.
2299   __ dispatch_only(vtos, true);
2300 }
2301 
_return(TosState state)2302 void TemplateTable::_return(TosState state) {
2303   transition(state, state);
2304   assert(_desc->calls_vm(),
2305          "inconsistent calls_vm information"); // call in remove_activation
2306 
2307   if (_desc->bytecode() == Bytecodes::_return_register_finalizer) {
2308     Register Rthis  = Z_ARG2;
2309     Register Rklass = Z_ARG5;
2310     Label skip_register_finalizer;
2311     assert(state == vtos, "only valid state");
2312     __ z_lg(Rthis, aaddress(0));
2313     __ load_klass(Rklass, Rthis);
2314     __ testbit(Address(Rklass, Klass::access_flags_offset()), exact_log2(JVM_ACC_HAS_FINALIZER));
2315     __ z_bfalse(skip_register_finalizer);
2316     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::register_finalizer), Rthis);
2317     __ bind(skip_register_finalizer);
2318   }
2319 
2320   if (_desc->bytecode() != Bytecodes::_return_register_finalizer) {
2321     Label no_safepoint;
2322     const Address poll_byte_addr(Z_thread, in_bytes(JavaThread::polling_word_offset()) + 7 /* Big Endian */);
2323     __ z_tm(poll_byte_addr, SafepointMechanism::poll_bit());
2324     __ z_braz(no_safepoint);
2325     __ push(state);
2326     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::at_safepoint));
2327     __ pop(state);
2328     __ bind(no_safepoint);
2329   }
2330 
2331   if (state == itos) {
2332     // Narrow result if state is itos but result type is smaller.
2333     // Need to narrow in the return bytecode rather than in generate_return_entry
2334     // since compiled code callers expect the result to already be narrowed.
2335     __ narrow(Z_tos, Z_tmp_1); /* fall through */
2336   }
2337 
2338   __ remove_activation(state, Z_R14);
2339   __ z_br(Z_R14);
2340 }
2341 
2342 // ----------------------------------------------------------------------------
2343 // NOTE: Cpe_offset is already computed as byte offset, so we must not
2344 // shift it afterwards!
resolve_cache_and_index(int byte_no,Register cache,Register cpe_offset,size_t index_size)2345 void TemplateTable::resolve_cache_and_index(int byte_no,
2346                                             Register cache,
2347                                             Register cpe_offset,
2348                                             size_t index_size) {
2349   BLOCK_COMMENT("resolve_cache_and_index {");
2350   NearLabel      resolved, clinit_barrier_slow;
2351   const Register bytecode_in_cpcache = Z_R1_scratch;
2352   const int      total_f1_offset = in_bytes(ConstantPoolCache::base_offset() + ConstantPoolCacheEntry::f1_offset());
2353   assert_different_registers(cache, cpe_offset, bytecode_in_cpcache);
2354 
2355   Bytecodes::Code code = bytecode();
2356   switch (code) {
2357     case Bytecodes::_nofast_getfield: code = Bytecodes::_getfield; break;
2358     case Bytecodes::_nofast_putfield: code = Bytecodes::_putfield; break;
2359     default:
2360       break;
2361   }
2362 
2363   {
2364     assert(byte_no == f1_byte || byte_no == f2_byte, "byte_no out of range");
2365     __ get_cache_and_index_and_bytecode_at_bcp(cache, cpe_offset, bytecode_in_cpcache, byte_no, 1, index_size);
2366     // Have we resolved this bytecode?
2367     __ compare32_and_branch(bytecode_in_cpcache, (int)code, Assembler::bcondEqual, resolved);
2368   }
2369 
2370   // Resolve first time through.
2371   // Class initialization barrier slow path lands here as well.
2372   __ bind(clinit_barrier_slow);
2373   address entry = CAST_FROM_FN_PTR(address, InterpreterRuntime::resolve_from_cache);
2374   __ load_const_optimized(Z_ARG2, (int) code);
2375   __ call_VM(noreg, entry, Z_ARG2);
2376 
2377   // Update registers with resolved info.
2378   __ get_cache_and_index_at_bcp(cache, cpe_offset, 1, index_size);
2379   __ bind(resolved);
2380 
2381   // Class initialization barrier for static methods
2382   if (VM_Version::supports_fast_class_init_checks() && bytecode() == Bytecodes::_invokestatic) {
2383     const Register method = Z_R1_scratch;
2384     const Register klass  = Z_R1_scratch;
2385 
2386     __ load_resolved_method_at_index(byte_no, cache, cpe_offset, method);
2387     __ load_method_holder(klass, method);
2388     __ clinit_barrier(klass, Z_thread, NULL /*L_fast_path*/, &clinit_barrier_slow);
2389   }
2390 
2391   BLOCK_COMMENT("} resolve_cache_and_index");
2392 }
2393 
2394 // The Rcache and index registers must be set before call.
2395 // Index is already a byte offset, don't shift!
load_field_cp_cache_entry(Register obj,Register cache,Register index,Register off,Register flags,bool is_static=false)2396 void TemplateTable::load_field_cp_cache_entry(Register obj,
2397                                               Register cache,
2398                                               Register index,
2399                                               Register off,
2400                                               Register flags,
2401                                               bool is_static = false) {
2402   assert_different_registers(cache, index, flags, off);
2403   ByteSize cp_base_offset = ConstantPoolCache::base_offset();
2404 
2405   // Field offset
2406   __ mem2reg_opt(off, Address(cache, index, cp_base_offset + ConstantPoolCacheEntry::f2_offset()));
2407   // Flags. Must load 64 bits.
2408   __ mem2reg_opt(flags, Address(cache, index, cp_base_offset + ConstantPoolCacheEntry::flags_offset()));
2409 
2410   // klass overwrite register
2411   if (is_static) {
2412     __ mem2reg_opt(obj, Address(cache, index, cp_base_offset + ConstantPoolCacheEntry::f1_offset()));
2413     __ mem2reg_opt(obj, Address(obj, Klass::java_mirror_offset()));
2414     __ resolve_oop_handle(obj);
2415   }
2416 }
2417 
load_invoke_cp_cache_entry(int byte_no,Register method,Register itable_index,Register flags,bool is_invokevirtual,bool is_invokevfinal,bool is_invokedynamic)2418 void TemplateTable::load_invoke_cp_cache_entry(int byte_no,
2419                                                Register method,
2420                                                Register itable_index,
2421                                                Register flags,
2422                                                bool is_invokevirtual,
2423                                                bool is_invokevfinal, // unused
2424                                                bool is_invokedynamic) {
2425   BLOCK_COMMENT("load_invoke_cp_cache_entry {");
2426   // Setup registers.
2427   const Register cache     = Z_ARG1;
2428   const Register cpe_offset= flags;
2429   const ByteSize base_off  = ConstantPoolCache::base_offset();
2430   const ByteSize f1_off    = ConstantPoolCacheEntry::f1_offset();
2431   const ByteSize f2_off    = ConstantPoolCacheEntry::f2_offset();
2432   const ByteSize flags_off = ConstantPoolCacheEntry::flags_offset();
2433   const int method_offset  = in_bytes(base_off + ((byte_no == f2_byte) ? f2_off : f1_off));
2434   const int flags_offset   = in_bytes(base_off + flags_off);
2435   // Access constant pool cache fields.
2436   const int index_offset   = in_bytes(base_off + f2_off);
2437 
2438   assert_different_registers(method, itable_index, flags, cache);
2439   assert(is_invokevirtual == (byte_no == f2_byte), "is_invokevirtual flag redundant");
2440 
2441   if (is_invokevfinal) {
2442     // Already resolved.
2443      assert(itable_index == noreg, "register not used");
2444      __ get_cache_and_index_at_bcp(cache, cpe_offset, 1);
2445   } else {
2446     // Need to resolve.
2447     resolve_cache_and_index(byte_no, cache, cpe_offset, is_invokedynamic ? sizeof(u4) : sizeof(u2));
2448   }
2449   __ z_lg(method, Address(cache, cpe_offset, method_offset));
2450 
2451   if (itable_index != noreg) {
2452     __ z_lg(itable_index, Address(cache, cpe_offset, index_offset));
2453   }
2454 
2455   // Only load the lower 4 bytes and fill high bytes of flags with zeros.
2456   // Callers depend on this zero-extension!!!
2457   // Attention: overwrites cpe_offset == flags
2458   __ z_llgf(flags, Address(cache, cpe_offset, flags_offset + (BytesPerLong-BytesPerInt)));
2459 
2460   BLOCK_COMMENT("} load_invoke_cp_cache_entry");
2461 }
2462 
2463 // The registers cache and index expected to be set before call.
2464 // Correct values of the cache and index registers are preserved.
jvmti_post_field_access(Register cache,Register index,bool is_static,bool has_tos)2465 void TemplateTable::jvmti_post_field_access(Register cache, Register index,
2466                                             bool is_static, bool has_tos) {
2467 
2468   // Do the JVMTI work here to avoid disturbing the register state below.
2469   // We use c_rarg registers here because we want to use the register used in
2470   // the call to the VM
2471   if (!JvmtiExport::can_post_field_access()) {
2472     return;
2473   }
2474 
2475   // Check to see if a field access watch has been set before we
2476   // take the time to call into the VM.
2477   Label exit;
2478   assert_different_registers(cache, index, Z_tos);
2479   __ load_absolute_address(Z_tos, (address)JvmtiExport::get_field_access_count_addr());
2480   __ load_and_test_int(Z_R0, Address(Z_tos));
2481   __ z_brz(exit);
2482 
2483   // Index is returned as byte offset, do not shift!
2484   __ get_cache_and_index_at_bcp(Z_ARG3, Z_R1_scratch, 1);
2485 
2486   // cache entry pointer
2487   __ add2reg_with_index(Z_ARG3,
2488                         in_bytes(ConstantPoolCache::base_offset()),
2489                         Z_ARG3, Z_R1_scratch);
2490 
2491   if (is_static) {
2492     __ clear_reg(Z_ARG2, true, false); // NULL object reference. Don't set CC.
2493   } else {
2494     __ mem2reg_opt(Z_ARG2, at_tos());  // Get object pointer without popping it.
2495     __ verify_oop(Z_ARG2);
2496   }
2497   // Z_ARG2: object pointer or NULL
2498   // Z_ARG3: cache entry pointer
2499   __ call_VM(noreg,
2500              CAST_FROM_FN_PTR(address, InterpreterRuntime::post_field_access),
2501              Z_ARG2, Z_ARG3);
2502   __ get_cache_and_index_at_bcp(cache, index, 1);
2503 
2504   __ bind(exit);
2505 }
2506 
pop_and_check_object(Register r)2507 void TemplateTable::pop_and_check_object(Register r) {
2508   __ pop_ptr(r);
2509   __ null_check(r);  // for field access must check obj.
2510   __ verify_oop(r);
2511 }
2512 
getfield_or_static(int byte_no,bool is_static,RewriteControl rc)2513 void TemplateTable::getfield_or_static(int byte_no, bool is_static, RewriteControl rc) {
2514   transition(vtos, vtos);
2515 
2516   const Register cache = Z_tmp_1;
2517   const Register index = Z_tmp_2;
2518   const Register obj   = Z_tmp_1;
2519   const Register off   = Z_ARG2;
2520   const Register flags = Z_ARG1;
2521   const Register bc    = Z_tmp_1;  // Uses same reg as obj, so don't mix them.
2522 
2523   resolve_cache_and_index(byte_no, cache, index, sizeof(u2));
2524   jvmti_post_field_access(cache, index, is_static, false);
2525   load_field_cp_cache_entry(obj, cache, index, off, flags, is_static);
2526 
2527   if (!is_static) {
2528     // Obj is on the stack.
2529     pop_and_check_object(obj);
2530   }
2531 
2532   // Displacement is 0, so any store instruction will be fine on any CPU.
2533   const Address field(obj, off);
2534 
2535   Label    is_Byte, is_Bool, is_Int, is_Short, is_Char,
2536            is_Long, is_Float, is_Object, is_Double;
2537   Label    is_badState8, is_badState9, is_badStateA, is_badStateB,
2538            is_badStateC, is_badStateD, is_badStateE, is_badStateF,
2539            is_badState;
2540   Label    branchTable, atosHandler,  Done;
2541   Register br_tab       = Z_R1_scratch;
2542   bool     do_rewrite   = !is_static && (rc == may_rewrite);
2543   bool     dont_rewrite = (is_static || (rc == may_not_rewrite));
2544 
2545   assert(do_rewrite == !dont_rewrite, "Oops, code is not fit for that");
2546   assert(btos == 0, "change code, btos != 0");
2547 
2548   // Calculate branch table size. Generated code size depends on ASSERT and on bytecode rewriting.
2549 #ifdef ASSERT
2550   const unsigned int bsize = dont_rewrite ? BTB_MINSIZE*1 : BTB_MINSIZE*4;
2551 #else
2552   const unsigned int bsize = dont_rewrite ? BTB_MINSIZE*1 : BTB_MINSIZE*4;
2553 #endif
2554 
2555   // Calculate address of branch table entry and branch there.
2556   {
2557     const int bit_shift = exact_log2(bsize); // Size of each branch table entry.
2558     const int r_bitpos  = 63 - bit_shift;
2559     const int l_bitpos  = r_bitpos - ConstantPoolCacheEntry::tos_state_bits + 1;
2560     const int n_rotate  = (bit_shift-ConstantPoolCacheEntry::tos_state_shift);
2561     __ z_larl(br_tab, branchTable);
2562     __ rotate_then_insert(flags, flags, l_bitpos, r_bitpos, n_rotate, true);
2563   }
2564   __ z_bc(Assembler::bcondAlways, 0, flags, br_tab);
2565 
2566   __ align_address(bsize);
2567   BIND(branchTable);
2568 
2569   // btos
2570   BTB_BEGIN(is_Byte, bsize, "getfield_or_static:is_Byte");
2571   __ z_lb(Z_tos, field);
2572   __ push(btos);
2573   // Rewrite bytecode to be faster.
2574   if (do_rewrite) {
2575     patch_bytecode(Bytecodes::_fast_bgetfield, bc, Z_ARG5);
2576   }
2577   __ z_bru(Done);
2578   BTB_END(is_Byte, bsize, "getfield_or_static:is_Byte");
2579 
2580   // ztos
2581   BTB_BEGIN(is_Bool, bsize, "getfield_or_static:is_Bool");
2582   __ z_lb(Z_tos, field);
2583   __ push(ztos);
2584   // Rewrite bytecode to be faster.
2585   if (do_rewrite) {
2586     // Use btos rewriting, no truncating to t/f bit is needed for getfield.
2587     patch_bytecode(Bytecodes::_fast_bgetfield, bc, Z_ARG5);
2588   }
2589   __ z_bru(Done);
2590   BTB_END(is_Bool, bsize, "getfield_or_static:is_Bool");
2591 
2592   // ctos
2593   BTB_BEGIN(is_Char, bsize, "getfield_or_static:is_Char");
2594   // Load into 64 bits, works on all CPUs.
2595   __ z_llgh(Z_tos, field);
2596   __ push(ctos);
2597   // Rewrite bytecode to be faster.
2598   if (do_rewrite) {
2599     patch_bytecode(Bytecodes::_fast_cgetfield, bc, Z_ARG5);
2600   }
2601   __ z_bru(Done);
2602   BTB_END(is_Char, bsize, "getfield_or_static:is_Char");
2603 
2604   // stos
2605   BTB_BEGIN(is_Short, bsize, "getfield_or_static:is_Short");
2606   __ z_lh(Z_tos, field);
2607   __ push(stos);
2608   // Rewrite bytecode to be faster.
2609   if (do_rewrite) {
2610     patch_bytecode(Bytecodes::_fast_sgetfield, bc, Z_ARG5);
2611   }
2612   __ z_bru(Done);
2613   BTB_END(is_Short, bsize, "getfield_or_static:is_Short");
2614 
2615   // itos
2616   BTB_BEGIN(is_Int, bsize, "getfield_or_static:is_Int");
2617   __ mem2reg_opt(Z_tos, field, false);
2618   __ push(itos);
2619   // Rewrite bytecode to be faster.
2620   if (do_rewrite) {
2621     patch_bytecode(Bytecodes::_fast_igetfield, bc, Z_ARG5);
2622   }
2623   __ z_bru(Done);
2624   BTB_END(is_Int, bsize, "getfield_or_static:is_Int");
2625 
2626   // ltos
2627   BTB_BEGIN(is_Long, bsize, "getfield_or_static:is_Long");
2628   __ mem2reg_opt(Z_tos, field);
2629   __ push(ltos);
2630   // Rewrite bytecode to be faster.
2631   if (do_rewrite) {
2632     patch_bytecode(Bytecodes::_fast_lgetfield, bc, Z_ARG5);
2633   }
2634   __ z_bru(Done);
2635   BTB_END(is_Long, bsize, "getfield_or_static:is_Long");
2636 
2637   // ftos
2638   BTB_BEGIN(is_Float, bsize, "getfield_or_static:is_Float");
2639   __ mem2freg_opt(Z_ftos, field, false);
2640   __ push(ftos);
2641   // Rewrite bytecode to be faster.
2642   if (do_rewrite) {
2643     patch_bytecode(Bytecodes::_fast_fgetfield, bc, Z_ARG5);
2644   }
2645   __ z_bru(Done);
2646   BTB_END(is_Float, bsize, "getfield_or_static:is_Float");
2647 
2648   // dtos
2649   BTB_BEGIN(is_Double, bsize, "getfield_or_static:is_Double");
2650   __ mem2freg_opt(Z_ftos, field);
2651   __ push(dtos);
2652   // Rewrite bytecode to be faster.
2653   if (do_rewrite) {
2654     patch_bytecode(Bytecodes::_fast_dgetfield, bc, Z_ARG5);
2655   }
2656   __ z_bru(Done);
2657   BTB_END(is_Double, bsize, "getfield_or_static:is_Double");
2658 
2659   // atos
2660   BTB_BEGIN(is_Object, bsize, "getfield_or_static:is_Object");
2661   __ z_bru(atosHandler);
2662   BTB_END(is_Object, bsize, "getfield_or_static:is_Object");
2663 
2664   // Bad state detection comes at no extra runtime cost.
2665   BTB_BEGIN(is_badState8, bsize, "getfield_or_static:is_badState8");
2666   __ z_illtrap();
2667   __ z_bru(is_badState);
2668   BTB_END( is_badState8, bsize, "getfield_or_static:is_badState8");
2669   BTB_BEGIN(is_badState9, bsize, "getfield_or_static:is_badState9");
2670   __ z_illtrap();
2671   __ z_bru(is_badState);
2672   BTB_END( is_badState9, bsize, "getfield_or_static:is_badState9");
2673   BTB_BEGIN(is_badStateA, bsize, "getfield_or_static:is_badStateA");
2674   __ z_illtrap();
2675   __ z_bru(is_badState);
2676   BTB_END( is_badStateA, bsize, "getfield_or_static:is_badStateA");
2677   BTB_BEGIN(is_badStateB, bsize, "getfield_or_static:is_badStateB");
2678   __ z_illtrap();
2679   __ z_bru(is_badState);
2680   BTB_END( is_badStateB, bsize, "getfield_or_static:is_badStateB");
2681   BTB_BEGIN(is_badStateC, bsize, "getfield_or_static:is_badStateC");
2682   __ z_illtrap();
2683   __ z_bru(is_badState);
2684   BTB_END( is_badStateC, bsize, "getfield_or_static:is_badStateC");
2685   BTB_BEGIN(is_badStateD, bsize, "getfield_or_static:is_badStateD");
2686   __ z_illtrap();
2687   __ z_bru(is_badState);
2688   BTB_END( is_badStateD, bsize, "getfield_or_static:is_badStateD");
2689   BTB_BEGIN(is_badStateE, bsize, "getfield_or_static:is_badStateE");
2690   __ z_illtrap();
2691   __ z_bru(is_badState);
2692   BTB_END( is_badStateE, bsize, "getfield_or_static:is_badStateE");
2693   BTB_BEGIN(is_badStateF, bsize, "getfield_or_static:is_badStateF");
2694   __ z_illtrap();
2695   __ z_bru(is_badState);
2696   BTB_END( is_badStateF, bsize, "getfield_or_static:is_badStateF");
2697 
2698   __ align_address(64);
2699   BIND(is_badState);  // Do this outside branch table. Needs a lot of space.
2700   {
2701     unsigned int b_off = __ offset();
2702     if (is_static) {
2703       __ stop_static("Bad state in getstatic");
2704     } else {
2705       __ stop_static("Bad state in getfield");
2706     }
2707     unsigned int e_off = __ offset();
2708   }
2709 
2710   __ align_address(64);
2711   BIND(atosHandler);  // Oops are really complicated to handle.
2712                       // There is a lot of code generated.
2713                       // Therefore: generate the handler outside of branch table.
2714                       // There is no performance penalty. The additional branch
2715                       // to here is compensated for by the fallthru to "Done".
2716   {
2717     unsigned int b_off = __ offset();
2718     do_oop_load(_masm, field, Z_tos, Z_tmp_2, Z_tmp_3, IN_HEAP);
2719     __ verify_oop(Z_tos);
2720     __ push(atos);
2721     if (do_rewrite) {
2722       patch_bytecode(Bytecodes::_fast_agetfield, bc, Z_ARG5);
2723     }
2724     unsigned int e_off = __ offset();
2725   }
2726 
2727   BIND(Done);
2728 }
2729 
getfield(int byte_no)2730 void TemplateTable::getfield(int byte_no) {
2731   BLOCK_COMMENT("getfield  {");
2732   getfield_or_static(byte_no, false);
2733   BLOCK_COMMENT("} getfield");
2734 }
2735 
nofast_getfield(int byte_no)2736 void TemplateTable::nofast_getfield(int byte_no) {
2737   getfield_or_static(byte_no, false, may_not_rewrite);
2738 }
2739 
getstatic(int byte_no)2740 void TemplateTable::getstatic(int byte_no) {
2741   BLOCK_COMMENT("getstatic {");
2742   getfield_or_static(byte_no, true);
2743   BLOCK_COMMENT("} getstatic");
2744 }
2745 
2746 // The registers cache and index expected to be set before call.  The
2747 // function may destroy various registers, just not the cache and
2748 // index registers.
jvmti_post_field_mod(Register cache,Register index,bool is_static)2749 void TemplateTable::jvmti_post_field_mod(Register cache,
2750                                          Register index, bool is_static) {
2751   transition(vtos, vtos);
2752 
2753   if (!JvmtiExport::can_post_field_modification()) {
2754     return;
2755   }
2756 
2757   BLOCK_COMMENT("jvmti_post_field_mod {");
2758 
2759   // Check to see if a field modification watch has been set before
2760   // we take the time to call into the VM.
2761   Label    L1;
2762   ByteSize cp_base_offset = ConstantPoolCache::base_offset();
2763   assert_different_registers(cache, index, Z_tos);
2764 
2765   __ load_absolute_address(Z_tos, (address)JvmtiExport::get_field_modification_count_addr());
2766   __ load_and_test_int(Z_R0, Address(Z_tos));
2767   __ z_brz(L1);
2768 
2769   // Index is returned as byte offset, do not shift!
2770   __ get_cache_and_index_at_bcp(Z_ARG3, Z_R1_scratch, 1);
2771 
2772   if (is_static) {
2773     // Life is simple. Null out the object pointer.
2774     __ clear_reg(Z_ARG2, true, false);   // Don't set CC.
2775   } else {
2776     // Life is harder. The stack holds the value on top, followed by
2777     // the object. We don't know the size of the value, though. It
2778     // could be one or two words depending on its type. As a result,
2779     // we must find the type to determine where the object is.
2780     __ mem2reg_opt(Z_ARG4,
2781                    Address(Z_ARG3, Z_R1_scratch,
2782                            in_bytes(cp_base_offset + ConstantPoolCacheEntry::flags_offset()) +
2783                            (BytesPerLong - BytesPerInt)),
2784                    false);
2785     __ z_srl(Z_ARG4, ConstantPoolCacheEntry::tos_state_shift);
2786     // Make sure we don't need to mask Z_ARG4 for tos_state after the above shift.
2787     ConstantPoolCacheEntry::verify_tos_state_shift();
2788     __ mem2reg_opt(Z_ARG2, at_tos(1));  // Initially assume a one word jvalue.
2789 
2790     NearLabel   load_dtos, cont;
2791 
2792     __ compareU32_and_branch(Z_ARG4, (intptr_t) ltos,
2793                               Assembler::bcondNotEqual, load_dtos);
2794     __ mem2reg_opt(Z_ARG2, at_tos(2)); // ltos (two word jvalue)
2795     __ z_bru(cont);
2796 
2797     __ bind(load_dtos);
2798     __ compareU32_and_branch(Z_ARG4, (intptr_t)dtos, Assembler::bcondNotEqual, cont);
2799     __ mem2reg_opt(Z_ARG2, at_tos(2)); // dtos (two word jvalue)
2800 
2801     __ bind(cont);
2802   }
2803   // cache entry pointer
2804 
2805   __ add2reg_with_index(Z_ARG3, in_bytes(cp_base_offset), Z_ARG3, Z_R1_scratch);
2806 
2807   // object(tos)
2808   __ load_address(Z_ARG4, Address(Z_esp, Interpreter::stackElementSize));
2809   // Z_ARG2: object pointer set up above (NULL if static)
2810   // Z_ARG3: cache entry pointer
2811   // Z_ARG4: jvalue object on the stack
2812   __ call_VM(noreg,
2813              CAST_FROM_FN_PTR(address, InterpreterRuntime::post_field_modification),
2814              Z_ARG2, Z_ARG3, Z_ARG4);
2815   __ get_cache_and_index_at_bcp(cache, index, 1);
2816 
2817   __ bind(L1);
2818   BLOCK_COMMENT("} jvmti_post_field_mod");
2819 }
2820 
2821 
putfield_or_static(int byte_no,bool is_static,RewriteControl rc)2822 void TemplateTable::putfield_or_static(int byte_no, bool is_static, RewriteControl rc) {
2823   transition(vtos, vtos);
2824 
2825   const Register cache         = Z_tmp_1;
2826   const Register index         = Z_ARG5;
2827   const Register obj           = Z_tmp_1;
2828   const Register off           = Z_tmp_2;
2829   const Register flags         = Z_R1_scratch;
2830   const Register br_tab        = Z_ARG5;
2831   const Register bc            = Z_tmp_1;
2832   const Register oopStore_tmp1 = Z_R1_scratch;
2833   const Register oopStore_tmp2 = Z_ARG5;
2834   const Register oopStore_tmp3 = Z_R0_scratch;
2835 
2836   resolve_cache_and_index(byte_no, cache, index, sizeof(u2));
2837   jvmti_post_field_mod(cache, index, is_static);
2838   load_field_cp_cache_entry(obj, cache, index, off, flags, is_static);
2839   // begin of life for:
2840   //   obj, off   long life range
2841   //   flags      short life range, up to branch into branch table
2842   // end of life for:
2843   //   cache, index
2844 
2845   const Address field(obj, off);
2846   Label is_Byte, is_Bool, is_Int, is_Short, is_Char,
2847         is_Long, is_Float, is_Object, is_Double;
2848   Label is_badState8, is_badState9, is_badStateA, is_badStateB,
2849         is_badStateC, is_badStateD, is_badStateE, is_badStateF,
2850         is_badState;
2851   Label branchTable, atosHandler, Done;
2852   bool  do_rewrite   = !is_static && (rc == may_rewrite);
2853   bool  dont_rewrite = (is_static || (rc == may_not_rewrite));
2854 
2855   assert(do_rewrite == !dont_rewrite, "Oops, code is not fit for that");
2856 
2857   assert(btos == 0, "change code, btos != 0");
2858 
2859 #ifdef ASSERT
2860   const unsigned int bsize = is_static ? BTB_MINSIZE*1 : BTB_MINSIZE*4;
2861 #else
2862   const unsigned int bsize = is_static ? BTB_MINSIZE*1 : BTB_MINSIZE*8;
2863 #endif
2864 
2865   // Calculate address of branch table entry and branch there.
2866   {
2867     const int bit_shift = exact_log2(bsize); // Size of each branch table entry.
2868     const int r_bitpos  = 63 - bit_shift;
2869     const int l_bitpos  = r_bitpos - ConstantPoolCacheEntry::tos_state_bits + 1;
2870     const int n_rotate  = (bit_shift-ConstantPoolCacheEntry::tos_state_shift);
2871     __ z_larl(br_tab, branchTable);
2872     __ rotate_then_insert(flags, flags, l_bitpos, r_bitpos, n_rotate, true);
2873     __ z_bc(Assembler::bcondAlways, 0, flags, br_tab);
2874   }
2875   // end of life for:
2876   //   flags, br_tab
2877 
2878   __ align_address(bsize);
2879   BIND(branchTable);
2880 
2881   // btos
2882   BTB_BEGIN(is_Byte, bsize, "putfield_or_static:is_Byte");
2883   __ pop(btos);
2884   if (!is_static) {
2885     pop_and_check_object(obj);
2886   }
2887   __ z_stc(Z_tos, field);
2888   if (do_rewrite) {
2889     patch_bytecode(Bytecodes::_fast_bputfield, bc, Z_ARG5, true, byte_no);
2890   }
2891   __ z_bru(Done);
2892   BTB_END( is_Byte, bsize, "putfield_or_static:is_Byte");
2893 
2894   // ztos
2895   BTB_BEGIN(is_Bool, bsize, "putfield_or_static:is_Bool");
2896   __ pop(ztos);
2897   if (!is_static) {
2898     pop_and_check_object(obj);
2899   }
2900   __ z_nilf(Z_tos, 0x1);
2901   __ z_stc(Z_tos, field);
2902   if (do_rewrite) {
2903     patch_bytecode(Bytecodes::_fast_zputfield, bc, Z_ARG5, true, byte_no);
2904   }
2905   __ z_bru(Done);
2906   BTB_END(is_Bool, bsize, "putfield_or_static:is_Bool");
2907 
2908   // ctos
2909   BTB_BEGIN(is_Char, bsize, "putfield_or_static:is_Char");
2910   __ pop(ctos);
2911   if (!is_static) {
2912     pop_and_check_object(obj);
2913   }
2914   __ z_sth(Z_tos, field);
2915   if (do_rewrite) {
2916     patch_bytecode(Bytecodes::_fast_cputfield, bc, Z_ARG5, true, byte_no);
2917   }
2918   __ z_bru(Done);
2919   BTB_END( is_Char, bsize, "putfield_or_static:is_Char");
2920 
2921   // stos
2922   BTB_BEGIN(is_Short, bsize, "putfield_or_static:is_Short");
2923   __ pop(stos);
2924   if (!is_static) {
2925     pop_and_check_object(obj);
2926   }
2927   __ z_sth(Z_tos, field);
2928   if (do_rewrite) {
2929     patch_bytecode(Bytecodes::_fast_sputfield, bc, Z_ARG5, true, byte_no);
2930   }
2931   __ z_bru(Done);
2932   BTB_END( is_Short, bsize, "putfield_or_static:is_Short");
2933 
2934   // itos
2935   BTB_BEGIN(is_Int, bsize, "putfield_or_static:is_Int");
2936   __ pop(itos);
2937   if (!is_static) {
2938     pop_and_check_object(obj);
2939   }
2940   __ reg2mem_opt(Z_tos, field, false);
2941   if (do_rewrite) {
2942     patch_bytecode(Bytecodes::_fast_iputfield, bc, Z_ARG5, true, byte_no);
2943   }
2944   __ z_bru(Done);
2945   BTB_END( is_Int, bsize, "putfield_or_static:is_Int");
2946 
2947   // ltos
2948   BTB_BEGIN(is_Long, bsize, "putfield_or_static:is_Long");
2949   __ pop(ltos);
2950   if (!is_static) {
2951     pop_and_check_object(obj);
2952   }
2953   __ reg2mem_opt(Z_tos, field);
2954   if (do_rewrite) {
2955     patch_bytecode(Bytecodes::_fast_lputfield, bc, Z_ARG5, true, byte_no);
2956   }
2957   __ z_bru(Done);
2958   BTB_END( is_Long, bsize, "putfield_or_static:is_Long");
2959 
2960   // ftos
2961   BTB_BEGIN(is_Float, bsize, "putfield_or_static:is_Float");
2962   __ pop(ftos);
2963   if (!is_static) {
2964     pop_and_check_object(obj);
2965   }
2966   __ freg2mem_opt(Z_ftos, field, false);
2967   if (do_rewrite) {
2968     patch_bytecode(Bytecodes::_fast_fputfield, bc, Z_ARG5, true, byte_no);
2969   }
2970   __ z_bru(Done);
2971   BTB_END( is_Float, bsize, "putfield_or_static:is_Float");
2972 
2973   // dtos
2974   BTB_BEGIN(is_Double, bsize, "putfield_or_static:is_Double");
2975   __ pop(dtos);
2976   if (!is_static) {
2977     pop_and_check_object(obj);
2978   }
2979   __ freg2mem_opt(Z_ftos, field);
2980   if (do_rewrite) {
2981     patch_bytecode(Bytecodes::_fast_dputfield, bc, Z_ARG5, true, byte_no);
2982   }
2983   __ z_bru(Done);
2984   BTB_END( is_Double, bsize, "putfield_or_static:is_Double");
2985 
2986   // atos
2987   BTB_BEGIN(is_Object, bsize, "putfield_or_static:is_Object");
2988   __ z_bru(atosHandler);
2989   BTB_END( is_Object, bsize, "putfield_or_static:is_Object");
2990 
2991   // Bad state detection comes at no extra runtime cost.
2992   BTB_BEGIN(is_badState8, bsize, "putfield_or_static:is_badState8");
2993   __ z_illtrap();
2994   __ z_bru(is_badState);
2995   BTB_END( is_badState8, bsize, "putfield_or_static:is_badState8");
2996   BTB_BEGIN(is_badState9, bsize, "putfield_or_static:is_badState9");
2997   __ z_illtrap();
2998   __ z_bru(is_badState);
2999   BTB_END( is_badState9, bsize, "putfield_or_static:is_badState9");
3000   BTB_BEGIN(is_badStateA, bsize, "putfield_or_static:is_badStateA");
3001   __ z_illtrap();
3002   __ z_bru(is_badState);
3003   BTB_END( is_badStateA, bsize, "putfield_or_static:is_badStateA");
3004   BTB_BEGIN(is_badStateB, bsize, "putfield_or_static:is_badStateB");
3005   __ z_illtrap();
3006   __ z_bru(is_badState);
3007   BTB_END( is_badStateB, bsize, "putfield_or_static:is_badStateB");
3008   BTB_BEGIN(is_badStateC, bsize, "putfield_or_static:is_badStateC");
3009   __ z_illtrap();
3010   __ z_bru(is_badState);
3011   BTB_END( is_badStateC, bsize, "putfield_or_static:is_badStateC");
3012   BTB_BEGIN(is_badStateD, bsize, "putfield_or_static:is_badStateD");
3013   __ z_illtrap();
3014   __ z_bru(is_badState);
3015   BTB_END( is_badStateD, bsize, "putfield_or_static:is_badStateD");
3016   BTB_BEGIN(is_badStateE, bsize, "putfield_or_static:is_badStateE");
3017   __ z_illtrap();
3018   __ z_bru(is_badState);
3019   BTB_END( is_badStateE, bsize, "putfield_or_static:is_badStateE");
3020   BTB_BEGIN(is_badStateF, bsize, "putfield_or_static:is_badStateF");
3021   __ z_illtrap();
3022   __ z_bru(is_badState);
3023   BTB_END( is_badStateF, bsize, "putfield_or_static:is_badStateF");
3024 
3025   __ align_address(64);
3026   BIND(is_badState);  // Do this outside branch table. Needs a lot of space.
3027   {
3028     unsigned int b_off = __ offset();
3029     if (is_static) __ stop_static("Bad state in putstatic");
3030     else            __ stop_static("Bad state in putfield");
3031     unsigned int e_off = __ offset();
3032   }
3033 
3034   __ align_address(64);
3035   BIND(atosHandler);  // Oops are really complicated to handle.
3036                       // There is a lot of code generated.
3037                       // Therefore: generate the handler outside of branch table.
3038                       // There is no performance penalty. The additional branch
3039                       // to here is compensated for by the fallthru to "Done".
3040   {
3041     unsigned int b_off = __ offset();
3042     __ pop(atos);
3043     if (!is_static) {
3044       pop_and_check_object(obj);
3045     }
3046     // Store into the field
3047     do_oop_store(_masm, Address(obj, off), Z_tos,
3048                  oopStore_tmp1, oopStore_tmp2, oopStore_tmp3, IN_HEAP);
3049     if (do_rewrite) {
3050       patch_bytecode(Bytecodes::_fast_aputfield, bc, Z_ARG5, true, byte_no);
3051     }
3052     // __ z_bru(Done); // fallthru
3053     unsigned int e_off = __ offset();
3054   }
3055 
3056   BIND(Done);
3057 
3058   // Check for volatile store.
3059   Label notVolatile;
3060 
3061   __ testbit(Z_ARG4, ConstantPoolCacheEntry::is_volatile_shift);
3062   __ z_brz(notVolatile);
3063   __ z_fence();
3064 
3065   BIND(notVolatile);
3066 }
3067 
putfield(int byte_no)3068 void TemplateTable::putfield(int byte_no) {
3069   BLOCK_COMMENT("putfield  {");
3070   putfield_or_static(byte_no, false);
3071   BLOCK_COMMENT("} putfield");
3072 }
3073 
nofast_putfield(int byte_no)3074 void TemplateTable::nofast_putfield(int byte_no) {
3075   putfield_or_static(byte_no, false, may_not_rewrite);
3076 }
3077 
putstatic(int byte_no)3078 void TemplateTable::putstatic(int byte_no) {
3079   BLOCK_COMMENT("putstatic {");
3080   putfield_or_static(byte_no, true);
3081   BLOCK_COMMENT("} putstatic");
3082 }
3083 
3084 // Push the tos value back to the stack.
3085 // gc will find oops there and update.
jvmti_post_fast_field_mod()3086 void TemplateTable::jvmti_post_fast_field_mod() {
3087 
3088   if (!JvmtiExport::can_post_field_modification()) {
3089     return;
3090   }
3091 
3092   // Check to see if a field modification watch has been set before
3093   // we take the time to call into the VM.
3094   Label   exit;
3095 
3096   BLOCK_COMMENT("jvmti_post_fast_field_mod {");
3097 
3098   __ load_absolute_address(Z_R1_scratch,
3099                            (address) JvmtiExport::get_field_modification_count_addr());
3100   __ load_and_test_int(Z_R0_scratch, Address(Z_R1_scratch));
3101   __ z_brz(exit);
3102 
3103   Register obj = Z_tmp_1;
3104 
3105   __ pop_ptr(obj);                  // Copy the object pointer from tos.
3106   __ verify_oop(obj);
3107   __ push_ptr(obj);                 // Put the object pointer back on tos.
3108 
3109   // Save tos values before call_VM() clobbers them. Since we have
3110   // to do it for every data type, we use the saved values as the
3111   // jvalue object.
3112   switch (bytecode()) {          // Load values into the jvalue object.
3113     case Bytecodes::_fast_aputfield:
3114       __ push_ptr(Z_tos);
3115       break;
3116     case Bytecodes::_fast_bputfield:
3117     case Bytecodes::_fast_zputfield:
3118     case Bytecodes::_fast_sputfield:
3119     case Bytecodes::_fast_cputfield:
3120     case Bytecodes::_fast_iputfield:
3121       __ push_i(Z_tos);
3122       break;
3123     case Bytecodes::_fast_dputfield:
3124       __ push_d();
3125       break;
3126     case Bytecodes::_fast_fputfield:
3127       __ push_f();
3128       break;
3129     case Bytecodes::_fast_lputfield:
3130       __ push_l(Z_tos);
3131       break;
3132 
3133     default:
3134       ShouldNotReachHere();
3135   }
3136 
3137   // jvalue on the stack
3138   __ load_address(Z_ARG4, Address(Z_esp, Interpreter::stackElementSize));
3139   // Access constant pool cache entry.
3140   __ get_cache_entry_pointer_at_bcp(Z_ARG3, Z_tos, 1);
3141   __ verify_oop(obj);
3142 
3143   // obj   : object pointer copied above
3144   // Z_ARG3: cache entry pointer
3145   // Z_ARG4: jvalue object on the stack
3146   __ call_VM(noreg,
3147              CAST_FROM_FN_PTR(address, InterpreterRuntime::post_field_modification),
3148              obj, Z_ARG3, Z_ARG4);
3149 
3150   switch (bytecode()) {             // Restore tos values.
3151     case Bytecodes::_fast_aputfield:
3152       __ pop_ptr(Z_tos);
3153       break;
3154     case Bytecodes::_fast_bputfield:
3155     case Bytecodes::_fast_zputfield:
3156     case Bytecodes::_fast_sputfield:
3157     case Bytecodes::_fast_cputfield:
3158     case Bytecodes::_fast_iputfield:
3159       __ pop_i(Z_tos);
3160       break;
3161     case Bytecodes::_fast_dputfield:
3162       __ pop_d(Z_ftos);
3163       break;
3164     case Bytecodes::_fast_fputfield:
3165       __ pop_f(Z_ftos);
3166       break;
3167     case Bytecodes::_fast_lputfield:
3168       __ pop_l(Z_tos);
3169       break;
3170     default:
3171       break;
3172   }
3173 
3174   __ bind(exit);
3175   BLOCK_COMMENT("} jvmti_post_fast_field_mod");
3176 }
3177 
fast_storefield(TosState state)3178 void TemplateTable::fast_storefield(TosState state) {
3179   transition(state, vtos);
3180 
3181   ByteSize base = ConstantPoolCache::base_offset();
3182   jvmti_post_fast_field_mod();
3183 
3184   // Access constant pool cache.
3185   Register cache = Z_tmp_1;
3186   Register index = Z_tmp_2;
3187   Register flags = Z_ARG5;
3188 
3189   // Index comes in bytes, don't shift afterwards!
3190   __ get_cache_and_index_at_bcp(cache, index, 1);
3191 
3192   // Test for volatile.
3193   assert(!flags->is_volatile(), "do_oop_store could perform leaf RT call");
3194   __ z_lg(flags, Address(cache, index, base + ConstantPoolCacheEntry::flags_offset()));
3195 
3196   // Replace index with field offset from cache entry.
3197   Register field_offset = index;
3198   __ z_lg(field_offset, Address(cache, index, base + ConstantPoolCacheEntry::f2_offset()));
3199 
3200   // Get object from stack.
3201   Register   obj = cache;
3202 
3203   pop_and_check_object(obj);
3204 
3205   // field address
3206   const Address   field(obj, field_offset);
3207 
3208   // access field
3209   switch (bytecode()) {
3210     case Bytecodes::_fast_aputfield:
3211       do_oop_store(_masm, Address(obj, field_offset), Z_tos,
3212                    Z_ARG2, Z_ARG3, Z_ARG4, IN_HEAP);
3213       break;
3214     case Bytecodes::_fast_lputfield:
3215       __ reg2mem_opt(Z_tos, field);
3216       break;
3217     case Bytecodes::_fast_iputfield:
3218       __ reg2mem_opt(Z_tos, field, false);
3219       break;
3220     case Bytecodes::_fast_zputfield:
3221       __ z_nilf(Z_tos, 0x1);
3222       // fall through to bputfield
3223     case Bytecodes::_fast_bputfield:
3224       __ z_stc(Z_tos, field);
3225       break;
3226     case Bytecodes::_fast_sputfield:
3227       // fall through
3228     case Bytecodes::_fast_cputfield:
3229       __ z_sth(Z_tos, field);
3230       break;
3231     case Bytecodes::_fast_fputfield:
3232       __ freg2mem_opt(Z_ftos, field, false);
3233       break;
3234     case Bytecodes::_fast_dputfield:
3235       __ freg2mem_opt(Z_ftos, field);
3236       break;
3237     default:
3238       ShouldNotReachHere();
3239   }
3240 
3241   //  Check for volatile store.
3242   Label notVolatile;
3243 
3244   __ testbit(flags, ConstantPoolCacheEntry::is_volatile_shift);
3245   __ z_brz(notVolatile);
3246   __ z_fence();
3247 
3248   __ bind(notVolatile);
3249 }
3250 
fast_accessfield(TosState state)3251 void TemplateTable::fast_accessfield(TosState state) {
3252   transition(atos, state);
3253 
3254   Register obj = Z_tos;
3255 
3256   // Do the JVMTI work here to avoid disturbing the register state below
3257   if (JvmtiExport::can_post_field_access()) {
3258     // Check to see if a field access watch has been set before we
3259     // take the time to call into the VM.
3260     Label cont;
3261 
3262     __ load_absolute_address(Z_R1_scratch,
3263                              (address)JvmtiExport::get_field_access_count_addr());
3264     __ load_and_test_int(Z_R0_scratch, Address(Z_R1_scratch));
3265     __ z_brz(cont);
3266 
3267     // Access constant pool cache entry.
3268 
3269     __ get_cache_entry_pointer_at_bcp(Z_ARG3, Z_tmp_1, 1);
3270     __ verify_oop(obj);
3271     __ push_ptr(obj);  // Save object pointer before call_VM() clobbers it.
3272     __ z_lgr(Z_ARG2, obj);
3273 
3274     // Z_ARG2: object pointer copied above
3275     // Z_ARG3: cache entry pointer
3276     __ call_VM(noreg,
3277                CAST_FROM_FN_PTR(address, InterpreterRuntime::post_field_access),
3278                Z_ARG2, Z_ARG3);
3279     __ pop_ptr(obj); // Restore object pointer.
3280 
3281     __ bind(cont);
3282   }
3283 
3284   // Access constant pool cache.
3285   Register   cache = Z_tmp_1;
3286   Register   index = Z_tmp_2;
3287 
3288   // Index comes in bytes, don't shift afterwards!
3289   __ get_cache_and_index_at_bcp(cache, index, 1);
3290   // Replace index with field offset from cache entry.
3291   __ mem2reg_opt(index,
3292                  Address(cache, index,
3293                          ConstantPoolCache::base_offset() + ConstantPoolCacheEntry::f2_offset()));
3294 
3295   __ verify_oop(obj);
3296   __ null_check(obj);
3297 
3298   Address field(obj, index);
3299 
3300   // access field
3301   switch (bytecode()) {
3302     case Bytecodes::_fast_agetfield:
3303       do_oop_load(_masm, field, Z_tos, Z_tmp_1, Z_tmp_2, IN_HEAP);
3304       __ verify_oop(Z_tos);
3305       return;
3306     case Bytecodes::_fast_lgetfield:
3307       __ mem2reg_opt(Z_tos, field);
3308       return;
3309     case Bytecodes::_fast_igetfield:
3310       __ mem2reg_opt(Z_tos, field, false);
3311       return;
3312     case Bytecodes::_fast_bgetfield:
3313       __ z_lb(Z_tos, field);
3314       return;
3315     case Bytecodes::_fast_sgetfield:
3316       __ z_lh(Z_tos, field);
3317       return;
3318     case Bytecodes::_fast_cgetfield:
3319       __ z_llgh(Z_tos, field);   // Load into 64 bits, works on all CPUs.
3320       return;
3321     case Bytecodes::_fast_fgetfield:
3322       __ mem2freg_opt(Z_ftos, field, false);
3323       return;
3324     case Bytecodes::_fast_dgetfield:
3325       __ mem2freg_opt(Z_ftos, field);
3326       return;
3327     default:
3328       ShouldNotReachHere();
3329   }
3330 }
3331 
fast_xaccess(TosState state)3332 void TemplateTable::fast_xaccess(TosState state) {
3333   transition(vtos, state);
3334 
3335   Register receiver = Z_tos;
3336   // Get receiver.
3337   __ mem2reg_opt(Z_tos, aaddress(0));
3338 
3339   // Access constant pool cache.
3340   Register cache = Z_tmp_1;
3341   Register index = Z_tmp_2;
3342 
3343   // Index comes in bytes, don't shift afterwards!
3344   __ get_cache_and_index_at_bcp(cache, index, 2);
3345   // Replace index with field offset from cache entry.
3346   __ mem2reg_opt(index,
3347                  Address(cache, index,
3348                          ConstantPoolCache::base_offset() + ConstantPoolCacheEntry::f2_offset()));
3349 
3350   // Make sure exception is reported in correct bcp range (getfield is
3351   // next instruction).
3352   __ add2reg(Z_bcp, 1);
3353   __ null_check(receiver);
3354   switch (state) {
3355     case itos:
3356       __ mem2reg_opt(Z_tos, Address(receiver, index), false);
3357       break;
3358     case atos:
3359       do_oop_load(_masm, Address(receiver, index), Z_tos, Z_tmp_1, Z_tmp_2, IN_HEAP);
3360       __ verify_oop(Z_tos);
3361       break;
3362     case ftos:
3363       __ mem2freg_opt(Z_ftos, Address(receiver, index));
3364       break;
3365     default:
3366       ShouldNotReachHere();
3367   }
3368 
3369   // Reset bcp to original position.
3370   __ add2reg(Z_bcp, -1);
3371 }
3372 
3373 //-----------------------------------------------------------------------------
3374 // Calls
3375 
prepare_invoke(int byte_no,Register method,Register index,Register recv,Register flags)3376 void TemplateTable::prepare_invoke(int byte_no,
3377                                    Register method,  // linked method (or i-klass)
3378                                    Register index,   // itable index, MethodType, etc.
3379                                    Register recv,    // If caller wants to see it.
3380                                    Register flags) { // If caller wants to test it.
3381   // Determine flags.
3382   const Bytecodes::Code code = bytecode();
3383   const bool is_invokeinterface  = code == Bytecodes::_invokeinterface;
3384   const bool is_invokedynamic    = code == Bytecodes::_invokedynamic;
3385   const bool is_invokehandle     = code == Bytecodes::_invokehandle;
3386   const bool is_invokevirtual    = code == Bytecodes::_invokevirtual;
3387   const bool is_invokespecial    = code == Bytecodes::_invokespecial;
3388   const bool load_receiver       = (recv != noreg);
3389   assert(load_receiver == (code != Bytecodes::_invokestatic && code != Bytecodes::_invokedynamic), "");
3390 
3391   // Setup registers & access constant pool cache.
3392   if (recv  == noreg) { recv  = Z_ARG1; }
3393   if (flags == noreg) { flags = Z_ARG2; }
3394   assert_different_registers(method, Z_R14, index, recv, flags);
3395 
3396   BLOCK_COMMENT("prepare_invoke {");
3397 
3398   load_invoke_cp_cache_entry(byte_no, method, index, flags, is_invokevirtual, false, is_invokedynamic);
3399 
3400   // Maybe push appendix to arguments.
3401   if (is_invokedynamic || is_invokehandle) {
3402     Label L_no_push;
3403     Register resolved_reference = Z_R1_scratch;
3404     __ testbit(flags, ConstantPoolCacheEntry::has_appendix_shift);
3405     __ z_bfalse(L_no_push);
3406     // Push the appendix as a trailing parameter.
3407     // This must be done before we get the receiver,
3408     // since the parameter_size includes it.
3409     __ load_resolved_reference_at_index(resolved_reference, index);
3410     __ verify_oop(resolved_reference);
3411     __ push_ptr(resolved_reference);  // Push appendix (MethodType, CallSite, etc.).
3412     __ bind(L_no_push);
3413   }
3414 
3415   // Load receiver if needed (after appendix is pushed so parameter size is correct).
3416   if (load_receiver) {
3417     assert(!is_invokedynamic, "");
3418     // recv := int2long(flags & ConstantPoolCacheEntry::parameter_size_mask) << 3
3419     // Flags is zero-extended int2long when loaded during load_invoke_cp_cache_entry().
3420     // Only the least significant byte (psize) of flags is used.
3421     {
3422       const unsigned int logSES = Interpreter::logStackElementSize;
3423       const int bit_shift = logSES;
3424       const int r_bitpos  = 63 - bit_shift;
3425       const int l_bitpos  = r_bitpos - ConstantPoolCacheEntry::parameter_size_bits + 1;
3426       const int n_rotate  = bit_shift;
3427       assert(ConstantPoolCacheEntry::parameter_size_mask == 255, "adapt bitpositions");
3428       __ rotate_then_insert(recv, flags, l_bitpos, r_bitpos, n_rotate, true);
3429     }
3430     // Recv now contains #arguments * StackElementSize.
3431 
3432     Address recv_addr(Z_esp, recv);
3433     __ z_lg(recv, recv_addr);
3434     __ verify_oop(recv);
3435   }
3436 
3437   // Compute return type.
3438   // ret_type is used by callers (invokespecial, invokestatic) at least.
3439   Register ret_type = Z_R1_scratch;
3440   assert_different_registers(ret_type, method);
3441 
3442   const address table_addr = (address)Interpreter::invoke_return_entry_table_for(code);
3443   __ load_absolute_address(Z_R14, table_addr);
3444 
3445   {
3446     const int bit_shift = LogBytesPerWord;           // Size of each table entry.
3447     const int r_bitpos  = 63 - bit_shift;
3448     const int l_bitpos  = r_bitpos - ConstantPoolCacheEntry::tos_state_bits + 1;
3449     const int n_rotate  = bit_shift-ConstantPoolCacheEntry::tos_state_shift;
3450     __ rotate_then_insert(ret_type, flags, l_bitpos, r_bitpos, n_rotate, true);
3451     // Make sure we don't need to mask flags for tos_state after the above shift.
3452     ConstantPoolCacheEntry::verify_tos_state_shift();
3453   }
3454 
3455     __ z_lg(Z_R14, Address(Z_R14, ret_type)); // Load return address.
3456   BLOCK_COMMENT("} prepare_invoke");
3457 }
3458 
3459 
invokevirtual_helper(Register index,Register recv,Register flags)3460 void TemplateTable::invokevirtual_helper(Register index,
3461                                          Register recv,
3462                                          Register flags) {
3463   // Uses temporary registers Z_tmp_2, Z_ARG4.
3464   assert_different_registers(index, recv, Z_tmp_2, Z_ARG4);
3465 
3466   // Test for an invoke of a final method.
3467   Label notFinal;
3468 
3469   BLOCK_COMMENT("invokevirtual_helper {");
3470 
3471   __ testbit(flags, ConstantPoolCacheEntry::is_vfinal_shift);
3472   __ z_brz(notFinal);
3473 
3474   const Register method = index;  // Method must be Z_ARG3.
3475   assert(method == Z_ARG3, "method must be second argument for interpreter calling convention");
3476 
3477   // Do the call - the index is actually the method to call.
3478   // That is, f2 is a vtable index if !is_vfinal, else f2 is a method.
3479 
3480   // It's final, need a null check here!
3481   __ null_check(recv);
3482 
3483   // Profile this call.
3484   __ profile_final_call(Z_tmp_2);
3485   __ profile_arguments_type(Z_tmp_2, method, Z_ARG5, true); // Argument type profiling.
3486   __ jump_from_interpreted(method, Z_tmp_2);
3487 
3488   __ bind(notFinal);
3489 
3490   // Get receiver klass.
3491   __ null_check(recv, Z_R0_scratch, oopDesc::klass_offset_in_bytes());
3492   __ load_klass(Z_tmp_2, recv);
3493 
3494   // Profile this call.
3495   __ profile_virtual_call(Z_tmp_2, Z_ARG4, Z_ARG5);
3496 
3497   // Get target method & entry point.
3498   __ z_sllg(index, index, exact_log2(vtableEntry::size_in_bytes()));
3499   __ mem2reg_opt(method,
3500                  Address(Z_tmp_2, index,
3501                          Klass::vtable_start_offset() + in_ByteSize(vtableEntry::method_offset_in_bytes())));
3502   __ profile_arguments_type(Z_ARG4, method, Z_ARG5, true);
3503   __ jump_from_interpreted(method, Z_ARG4);
3504   BLOCK_COMMENT("} invokevirtual_helper");
3505 }
3506 
invokevirtual(int byte_no)3507 void TemplateTable::invokevirtual(int byte_no) {
3508   transition(vtos, vtos);
3509 
3510   assert(byte_no == f2_byte, "use this argument");
3511   prepare_invoke(byte_no,
3512                  Z_ARG3,  // method or vtable index
3513                  noreg,   // unused itable index
3514                  Z_ARG1,  // recv
3515                  Z_ARG2); // flags
3516 
3517   // Z_ARG3 : index
3518   // Z_ARG1 : receiver
3519   // Z_ARG2 : flags
3520   invokevirtual_helper(Z_ARG3, Z_ARG1, Z_ARG2);
3521 }
3522 
invokespecial(int byte_no)3523 void TemplateTable::invokespecial(int byte_no) {
3524   transition(vtos, vtos);
3525 
3526   assert(byte_no == f1_byte, "use this argument");
3527   Register Rmethod = Z_tmp_2;
3528   prepare_invoke(byte_no, Rmethod, noreg, // Get f1 method.
3529                  Z_ARG3);   // Get receiver also for null check.
3530   __ verify_oop(Z_ARG3);
3531   __ null_check(Z_ARG3);
3532   // Do the call.
3533   __ profile_call(Z_ARG2);
3534   __ profile_arguments_type(Z_ARG2, Rmethod, Z_ARG5, false);
3535   __ jump_from_interpreted(Rmethod, Z_R1_scratch);
3536 }
3537 
invokestatic(int byte_no)3538 void TemplateTable::invokestatic(int byte_no) {
3539   transition(vtos, vtos);
3540 
3541   assert(byte_no == f1_byte, "use this argument");
3542   Register Rmethod = Z_tmp_2;
3543   prepare_invoke(byte_no, Rmethod);   // Get f1 method.
3544   // Do the call.
3545   __ profile_call(Z_ARG2);
3546   __ profile_arguments_type(Z_ARG2, Rmethod, Z_ARG5, false);
3547   __ jump_from_interpreted(Rmethod, Z_R1_scratch);
3548 }
3549 
3550 // Outdated feature, and we don't support it.
fast_invokevfinal(int byte_no)3551 void TemplateTable::fast_invokevfinal(int byte_no) {
3552   transition(vtos, vtos);
3553   assert(byte_no == f2_byte, "use this argument");
3554   __ stop("fast_invokevfinal not used on linuxs390x");
3555 }
3556 
invokeinterface(int byte_no)3557 void TemplateTable::invokeinterface(int byte_no) {
3558   transition(vtos, vtos);
3559 
3560   assert(byte_no == f1_byte, "use this argument");
3561   Register klass     = Z_ARG2,
3562            method    = Z_ARG3,
3563            interface = Z_ARG4,
3564            flags     = Z_ARG5,
3565            receiver  = Z_tmp_1;
3566 
3567   BLOCK_COMMENT("invokeinterface {");
3568 
3569   prepare_invoke(byte_no, interface, method,  // Get f1 klassOop, f2 Method*.
3570                  receiver, flags);
3571 
3572   // Z_R14 (== Z_bytecode) : return entry
3573 
3574   // First check for Object case, then private interface method,
3575   // then regular interface method.
3576 
3577   // Special case of invokeinterface called for virtual method of
3578   // java.lang.Object. See cpCache.cpp for details.
3579   NearLabel notObjectMethod, no_such_method;
3580   __ testbit(flags, ConstantPoolCacheEntry::is_forced_virtual_shift);
3581   __ z_brz(notObjectMethod);
3582   invokevirtual_helper(method, receiver, flags);
3583   __ bind(notObjectMethod);
3584 
3585   // Check for private method invocation - indicated by vfinal
3586   NearLabel notVFinal;
3587   __ testbit(flags, ConstantPoolCacheEntry::is_vfinal_shift);
3588   __ z_brz(notVFinal);
3589 
3590   // Get receiver klass into klass - also a null check.
3591   __ load_klass(klass, receiver);
3592 
3593   NearLabel subtype, no_such_interface;
3594 
3595   __ check_klass_subtype(klass, interface, Z_tmp_2, flags/*scratch*/, subtype);
3596   // If we get here the typecheck failed
3597   __ z_bru(no_such_interface);
3598   __ bind(subtype);
3599 
3600   // do the call
3601   __ profile_final_call(Z_tmp_2);
3602   __ profile_arguments_type(Z_tmp_2, method, Z_ARG5, true);
3603   __ jump_from_interpreted(method, Z_tmp_2);
3604 
3605   __ bind(notVFinal);
3606 
3607   // Get receiver klass into klass - also a null check.
3608   __ load_klass(klass, receiver);
3609 
3610   __ lookup_interface_method(klass, interface, noreg, noreg, /*temp*/Z_ARG1,
3611                              no_such_interface, /*return_method=*/false);
3612 
3613   // Profile this call.
3614   __ profile_virtual_call(klass, Z_ARG1/*mdp*/, flags/*scratch*/);
3615 
3616   // Find entry point to call.
3617 
3618   // Get declaring interface class from method
3619   __ load_method_holder(interface, method);
3620 
3621   // Get itable index from method
3622   Register index   = receiver,
3623            method2 = flags;
3624   __ z_lgf(index, Address(method, Method::itable_index_offset()));
3625   __ z_aghi(index, -Method::itable_index_max);
3626   __ z_lcgr(index, index);
3627 
3628   __ lookup_interface_method(klass, interface, index, method2, Z_tmp_2,
3629                              no_such_interface);
3630 
3631   // Check for abstract method error.
3632   // Note: This should be done more efficiently via a throw_abstract_method_error
3633   // interpreter entry point and a conditional jump to it in case of a null
3634   // method.
3635   __ compareU64_and_branch(method2, (intptr_t) 0,
3636                            Assembler::bcondZero, no_such_method);
3637 
3638   __ profile_arguments_type(Z_tmp_1, method2, Z_tmp_2, true);
3639 
3640   // Do the call.
3641   __ jump_from_interpreted(method2, Z_tmp_2);
3642   __ should_not_reach_here();
3643 
3644   // exception handling code follows...
3645   // Note: Must restore interpreter registers to canonical
3646   // state for exception handling to work correctly!
3647 
3648   __ bind(no_such_method);
3649 
3650   // Throw exception.
3651   // Pass arguments for generating a verbose error message.
3652   __ z_lgr(Z_tmp_1, method); // Prevent register clash.
3653   __ call_VM(noreg,
3654              CAST_FROM_FN_PTR(address,
3655                               InterpreterRuntime::throw_AbstractMethodErrorVerbose),
3656                               klass, Z_tmp_1);
3657   // The call_VM checks for exception, so we should never return here.
3658   __ should_not_reach_here();
3659 
3660   __ bind(no_such_interface);
3661 
3662   // Throw exception.
3663   // Pass arguments for generating a verbose error message.
3664   __ call_VM(noreg,
3665              CAST_FROM_FN_PTR(address,
3666                               InterpreterRuntime::throw_IncompatibleClassChangeErrorVerbose),
3667                               klass, interface);
3668   // The call_VM checks for exception, so we should never return here.
3669   __ should_not_reach_here();
3670 
3671   BLOCK_COMMENT("} invokeinterface");
3672   return;
3673 }
3674 
invokehandle(int byte_no)3675 void TemplateTable::invokehandle(int byte_no) {
3676   transition(vtos, vtos);
3677 
3678   const Register method = Z_tmp_2;
3679   const Register recv   = Z_ARG5;
3680   const Register mtype  = Z_tmp_1;
3681   prepare_invoke(byte_no,
3682                  method, mtype,   // Get f2 method, f1 MethodType.
3683                  recv);
3684   __ verify_method_ptr(method);
3685   __ verify_oop(recv);
3686   __ null_check(recv);
3687 
3688   // Note: Mtype is already pushed (if necessary) by prepare_invoke.
3689 
3690   // FIXME: profile the LambdaForm also.
3691   __ profile_final_call(Z_ARG2);
3692   __ profile_arguments_type(Z_ARG3, method, Z_ARG5, true);
3693 
3694   __ jump_from_interpreted(method, Z_ARG3);
3695 }
3696 
invokedynamic(int byte_no)3697 void TemplateTable::invokedynamic(int byte_no) {
3698   transition(vtos, vtos);
3699 
3700   const Register Rmethod   = Z_tmp_2;
3701   const Register Rcallsite = Z_tmp_1;
3702 
3703   prepare_invoke(byte_no, Rmethod, Rcallsite);
3704 
3705   // Rmethod: CallSite object (from f1)
3706   // Rcallsite: MH.linkToCallSite method (from f2)
3707 
3708   // Note: Callsite is already pushed by prepare_invoke.
3709 
3710   // TODO: should make a type profile for any invokedynamic that takes a ref argument.
3711   // Profile this call.
3712   __ profile_call(Z_ARG2);
3713   __ profile_arguments_type(Z_ARG2, Rmethod, Z_ARG5, false);
3714   __ jump_from_interpreted(Rmethod, Z_ARG2);
3715 }
3716 
3717 //-----------------------------------------------------------------------------
3718 // Allocation
3719 
3720 // Original comment on "allow_shared_alloc":
3721 // Always go the slow path.
3722 //  + Eliminated optimization within the template-based interpreter:
3723 //    If an allocation is done within the interpreter without using
3724 //    tlabs, the interpreter tries to do the allocation directly
3725 //    on the heap.
3726 //  + That means the profiling hooks are not considered and allocations
3727 //    get lost for the profiling framework.
3728 //  + However, we do not think that this optimization is really needed,
3729 //    so we always go now the slow path through the VM in this case --
3730 //    spec jbb2005 shows no measurable performance degradation.
_new()3731 void TemplateTable::_new() {
3732   transition(vtos, atos);
3733   address prev_instr_address = NULL;
3734   Register tags  = Z_tmp_1;
3735   Register RallocatedObject   = Z_tos;
3736   Register cpool = Z_ARG2;
3737   Register tmp = Z_ARG3; // RobjectFields==tmp and Rsize==offset must be a register pair.
3738   Register offset = Z_ARG4;
3739   Label slow_case;
3740   Label done;
3741   Label initialize_header;
3742 
3743   BLOCK_COMMENT("TemplateTable::_new {");
3744   __ get_2_byte_integer_at_bcp(offset/*dest*/, 1, InterpreterMacroAssembler::Unsigned);
3745   __ get_cpool_and_tags(cpool, tags);
3746   // Make sure the class we're about to instantiate has been resolved.
3747   // This is done before loading InstanceKlass to be consistent with the order
3748   // how Constant Pool is updated (see ConstantPool::klass_at_put).
3749   const int tags_offset = Array<u1>::base_offset_in_bytes();
3750   __ load_address(tmp, Address(tags, offset, tags_offset));
3751   __ z_cli(0, tmp, JVM_CONSTANT_Class);
3752   __ z_brne(slow_case);
3753 
3754   __ z_sllg(offset, offset, LogBytesPerWord); // Convert to to offset.
3755   // Get InstanceKlass.
3756   Register iklass = cpool;
3757   __ load_resolved_klass_at_offset(cpool, offset, iklass);
3758 
3759   // Make sure klass is initialized & doesn't have finalizer.
3760   // Make sure klass is fully initialized.
3761   const int state_offset = in_bytes(InstanceKlass::init_state_offset());
3762   if (Immediate::is_uimm12(state_offset)) {
3763     __ z_cli(state_offset, iklass, InstanceKlass::fully_initialized);
3764   } else {
3765     __ z_cliy(state_offset, iklass, InstanceKlass::fully_initialized);
3766   }
3767   __ z_brne(slow_case);
3768 
3769   // Get instance_size in InstanceKlass (scaled to a count of bytes).
3770   Register Rsize = offset;
3771   __ z_llgf(Rsize, Address(iklass, Klass::layout_helper_offset()));
3772   __ z_tmll(Rsize, Klass::_lh_instance_slow_path_bit);
3773   __ z_btrue(slow_case);
3774 
3775   // Allocate the instance
3776   // 1) Try to allocate in the TLAB.
3777   // 2) If the above fails (or is not applicable), go to a slow case
3778   // (creates a new TLAB, etc.).
3779   // Note: compared to other architectures, s390's implementation always goes
3780   // to the slow path if TLAB is used and fails.
3781   if (UseTLAB) {
3782     Register RoldTopValue = RallocatedObject;
3783     Register RnewTopValue = tmp;
3784     __ z_lg(RoldTopValue, Address(Z_thread, JavaThread::tlab_top_offset()));
3785     __ load_address(RnewTopValue, Address(RoldTopValue, Rsize));
3786     __ z_cg(RnewTopValue, Address(Z_thread, JavaThread::tlab_end_offset()));
3787     __ z_brh(slow_case);
3788     __ z_stg(RnewTopValue, Address(Z_thread, JavaThread::tlab_top_offset()));
3789 
3790     Register RobjectFields = tmp;
3791     Register Rzero = Z_R1_scratch;
3792     __ clear_reg(Rzero, true /*whole reg*/, false); // Load 0L into Rzero. Don't set CC.
3793 
3794     if (!ZeroTLAB) {
3795       // The object is initialized before the header. If the object size is
3796       // zero, go directly to the header initialization.
3797       __ z_aghi(Rsize, (int)-sizeof(oopDesc)); // Subtract header size, set CC.
3798       __ z_bre(initialize_header);             // Jump if size of fields is zero.
3799 
3800       // Initialize object fields.
3801       // See documentation for MVCLE instruction!!!
3802       assert(RobjectFields->encoding() % 2 == 0, "RobjectFields must be an even register");
3803       assert(Rsize->encoding() == (RobjectFields->encoding()+1),
3804              "RobjectFields and Rsize must be a register pair");
3805       assert(Rzero->encoding() % 2 == 1, "Rzero must be an odd register");
3806 
3807       // Set Rzero to 0 and use it as src length, then mvcle will copy nothing
3808       // and fill the object with the padding value 0.
3809       __ add2reg(RobjectFields, sizeof(oopDesc), RallocatedObject);
3810       __ move_long_ext(RobjectFields, as_Register(Rzero->encoding() - 1), 0);
3811     }
3812 
3813     // Initialize object header only.
3814     __ bind(initialize_header);
3815     if (UseBiasedLocking) {
3816       Register prototype = RobjectFields;
3817       __ z_lg(prototype, Address(iklass, Klass::prototype_header_offset()));
3818       __ z_stg(prototype, Address(RallocatedObject, oopDesc::mark_offset_in_bytes()));
3819     } else {
3820       __ store_const(Address(RallocatedObject, oopDesc::mark_offset_in_bytes()),
3821                      (long)markWord::prototype().value());
3822     }
3823 
3824     __ store_klass_gap(Rzero, RallocatedObject);  // Zero klass gap for compressed oops.
3825     __ store_klass(iklass, RallocatedObject);     // Store klass last.
3826 
3827     {
3828       SkipIfEqual skip(_masm, &DTraceAllocProbes, false, Z_ARG5 /*scratch*/);
3829       // Trigger dtrace event for fastpath.
3830       __ push(atos); // Save the return value.
3831       __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_object_alloc), RallocatedObject);
3832       __ pop(atos); // Restore the return value.
3833     }
3834     __ z_bru(done);
3835   }
3836 
3837   // slow case
3838   __ bind(slow_case);
3839   __ get_constant_pool(Z_ARG2);
3840   __ get_2_byte_integer_at_bcp(Z_ARG3/*dest*/, 1, InterpreterMacroAssembler::Unsigned);
3841   call_VM(Z_tos, CAST_FROM_FN_PTR(address, InterpreterRuntime::_new), Z_ARG2, Z_ARG3);
3842   __ verify_oop(Z_tos);
3843 
3844   // continue
3845   __ bind(done);
3846 
3847   BLOCK_COMMENT("} TemplateTable::_new");
3848 }
3849 
newarray()3850 void TemplateTable::newarray() {
3851   transition(itos, atos);
3852 
3853   // Call runtime.
3854   __ z_llgc(Z_ARG2, at_bcp(1));   // type
3855   __ z_lgfr(Z_ARG3, Z_tos);       // size
3856   call_VM(Z_RET,
3857           CAST_FROM_FN_PTR(address, InterpreterRuntime::newarray),
3858           Z_ARG2, Z_ARG3);
3859 }
3860 
anewarray()3861 void TemplateTable::anewarray() {
3862   transition(itos, atos);
3863   __ get_2_byte_integer_at_bcp(Z_ARG3, 1, InterpreterMacroAssembler::Unsigned);
3864   __ get_constant_pool(Z_ARG2);
3865   __ z_lgfr(Z_ARG4, Z_tos);
3866   call_VM(Z_tos, CAST_FROM_FN_PTR(address, InterpreterRuntime::anewarray),
3867           Z_ARG2, Z_ARG3, Z_ARG4);
3868 }
3869 
arraylength()3870 void TemplateTable::arraylength() {
3871   transition(atos, itos);
3872 
3873   int offset = arrayOopDesc::length_offset_in_bytes();
3874 
3875   __ null_check(Z_tos, Z_R0_scratch, offset);
3876   __ mem2reg_opt(Z_tos, Address(Z_tos, offset), false);
3877 }
3878 
checkcast()3879 void TemplateTable::checkcast() {
3880   transition(atos, atos);
3881 
3882   NearLabel done, is_null, ok_is_subtype, quicked, resolved;
3883 
3884   BLOCK_COMMENT("checkcast {");
3885   // If object is NULL, we are almost done.
3886   __ compareU64_and_branch(Z_tos, (intptr_t) 0, Assembler::bcondZero, is_null);
3887 
3888   // Get cpool & tags index.
3889   Register cpool = Z_tmp_1;
3890   Register tags = Z_tmp_2;
3891   Register index = Z_ARG5;
3892 
3893   __ get_cpool_and_tags(cpool, tags);
3894   __ get_2_byte_integer_at_bcp(index, 1, InterpreterMacroAssembler::Unsigned);
3895   // See if bytecode has already been quicked.
3896   // Note: For CLI, we would have to add the index to the tags pointer first,
3897   // thus load and compare in a "classic" manner.
3898   __ z_llgc(Z_R0_scratch,
3899             Address(tags, index, Array<u1>::base_offset_in_bytes()));
3900   __ compareU64_and_branch(Z_R0_scratch, JVM_CONSTANT_Class,
3901                            Assembler::bcondEqual, quicked);
3902 
3903   __ push(atos); // Save receiver for result, and for GC.
3904   call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::quicken_io_cc));
3905   __ get_vm_result_2(Z_tos);
3906 
3907   Register   receiver = Z_ARG4;
3908   Register   klass = Z_tos;
3909   Register   subklass = Z_ARG5;
3910 
3911   __ pop_ptr(receiver); // restore receiver
3912   __ z_bru(resolved);
3913 
3914   // Get superklass in klass and subklass in subklass.
3915   __ bind(quicked);
3916 
3917   __ z_lgr(Z_ARG4, Z_tos);  // Save receiver.
3918   __ z_sllg(index, index, LogBytesPerWord);  // index2bytes for addressing
3919   __ load_resolved_klass_at_offset(cpool, index, klass);
3920 
3921   __ bind(resolved);
3922 
3923   __ load_klass(subklass, receiver);
3924 
3925   // Generate subtype check. Object in receiver.
3926   // Superklass in klass. Subklass in subklass.
3927   __ gen_subtype_check(subklass, klass, Z_ARG3, Z_tmp_1, ok_is_subtype);
3928 
3929   // Come here on failure.
3930   __ push_ptr(receiver);
3931   // Object is at TOS, target klass oop expected in rax by convention.
3932   __ z_brul((address) Interpreter::_throw_ClassCastException_entry);
3933 
3934   // Come here on success.
3935   __ bind(ok_is_subtype);
3936 
3937   __ z_lgr(Z_tos, receiver); // Restore object.
3938 
3939   // Collect counts on whether this test sees NULLs a lot or not.
3940   if (ProfileInterpreter) {
3941     __ z_bru(done);
3942     __ bind(is_null);
3943     __ profile_null_seen(Z_tmp_1);
3944   } else {
3945     __ bind(is_null);   // Same as 'done'.
3946   }
3947 
3948   __ bind(done);
3949   BLOCK_COMMENT("} checkcast");
3950 }
3951 
instanceof()3952 void TemplateTable::instanceof() {
3953   transition(atos, itos);
3954 
3955   NearLabel done, is_null, ok_is_subtype, quicked, resolved;
3956 
3957   BLOCK_COMMENT("instanceof {");
3958   // If object is NULL, we are almost done.
3959   __ compareU64_and_branch(Z_tos, (intptr_t) 0, Assembler::bcondZero, is_null);
3960 
3961   // Get cpool & tags index.
3962   Register cpool = Z_tmp_1;
3963   Register tags = Z_tmp_2;
3964   Register index = Z_ARG5;
3965 
3966   __ get_cpool_and_tags(cpool, tags);
3967   __ get_2_byte_integer_at_bcp(index, 1, InterpreterMacroAssembler::Unsigned);
3968   // See if bytecode has already been quicked.
3969   // Note: For CLI, we would have to add the index to the tags pointer first,
3970   // thus load and compare in a "classic" manner.
3971   __ z_llgc(Z_R0_scratch,
3972             Address(tags, index, Array<u1>::base_offset_in_bytes()));
3973   __ compareU64_and_branch(Z_R0_scratch, JVM_CONSTANT_Class, Assembler::bcondEqual, quicked);
3974 
3975   __ push(atos); // Save receiver for result, and for GC.
3976   call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::quicken_io_cc));
3977   __ get_vm_result_2(Z_tos);
3978 
3979   Register receiver = Z_tmp_2;
3980   Register klass = Z_tos;
3981   Register subklass = Z_tmp_2;
3982 
3983   __ pop_ptr(receiver); // Restore receiver.
3984   __ verify_oop(receiver);
3985   __ load_klass(subklass, subklass);
3986   __ z_bru(resolved);
3987 
3988   // Get superklass in klass and subklass in subklass.
3989   __ bind(quicked);
3990 
3991   __ load_klass(subklass, Z_tos);
3992   __ z_sllg(index, index, LogBytesPerWord);  // index2bytes for addressing
3993   __ load_resolved_klass_at_offset(cpool, index, klass);
3994 
3995   __ bind(resolved);
3996 
3997   // Generate subtype check.
3998   // Superklass in klass. Subklass in subklass.
3999   __ gen_subtype_check(subklass, klass, Z_ARG4, Z_ARG5, ok_is_subtype);
4000 
4001   // Come here on failure.
4002   __ clear_reg(Z_tos, true, false);
4003   __ z_bru(done);
4004 
4005   // Come here on success.
4006   __ bind(ok_is_subtype);
4007   __ load_const_optimized(Z_tos, 1);
4008 
4009   // Collect counts on whether this test sees NULLs a lot or not.
4010   if (ProfileInterpreter) {
4011     __ z_bru(done);
4012     __ bind(is_null);
4013     __ profile_null_seen(Z_tmp_1);
4014   } else {
4015     __ bind(is_null);   // same as 'done'
4016   }
4017 
4018   __ bind(done);
4019   // tos = 0: obj == NULL or  obj is not an instanceof the specified klass
4020   // tos = 1: obj != NULL and obj is     an instanceof the specified klass
4021   BLOCK_COMMENT("} instanceof");
4022 }
4023 
4024 //-----------------------------------------------------------------------------
4025 // Breakpoints
_breakpoint()4026 void TemplateTable::_breakpoint() {
4027 
4028   // Note: We get here even if we are single stepping.
4029   // Jbug insists on setting breakpoints at every bytecode
4030   // even if we are in single step mode.
4031 
4032   transition(vtos, vtos);
4033 
4034   // Get the unpatched byte code.
4035   __ get_method(Z_ARG2);
4036   __ call_VM(noreg,
4037              CAST_FROM_FN_PTR(address, InterpreterRuntime::get_original_bytecode_at),
4038              Z_ARG2, Z_bcp);
4039   // Save the result to a register that is preserved over C-function calls.
4040   __ z_lgr(Z_tmp_1, Z_RET);
4041 
4042   // Post the breakpoint event.
4043   __ get_method(Z_ARG2);
4044   __ call_VM(noreg,
4045              CAST_FROM_FN_PTR(address, InterpreterRuntime::_breakpoint),
4046              Z_ARG2, Z_bcp);
4047 
4048   // Must restore the bytecode, because call_VM destroys Z_bytecode.
4049   __ z_lgr(Z_bytecode, Z_tmp_1);
4050 
4051   // Complete the execution of original bytecode.
4052   __ dispatch_only_normal(vtos);
4053 }
4054 
4055 
4056 // Exceptions
4057 
athrow()4058 void TemplateTable::athrow() {
4059   transition(atos, vtos);
4060   __ null_check(Z_tos);
4061   __ load_absolute_address(Z_ARG2, Interpreter::throw_exception_entry());
4062   __ z_br(Z_ARG2);
4063 }
4064 
4065 // Synchronization
4066 //
4067 // Note: monitorenter & exit are symmetric routines; which is reflected
4068 //       in the assembly code structure as well
4069 //
4070 // Stack layout:
4071 //
4072 //               callers_sp        <- Z_SP (callers_sp == Z_fp (own fp))
4073 //               return_pc
4074 //               [rest of ABI_160]
4075 //              /slot o:   free
4076 //             / ...       free
4077 //       oper. | slot n+1: free    <- Z_esp points to first free slot
4078 //       stack | slot n:   val                      caches IJAVA_STATE.esp
4079 //             | ...
4080 //              \slot 0:   val
4081 //              /slot m            <- IJAVA_STATE.monitors = monitor block top
4082 //             | ...
4083 //     monitors| slot 2
4084 //             | slot 1
4085 //              \slot 0
4086 //              /slot l            <- monitor block bot
4087 // ijava_state | ...
4088 //             | slot 2
4089 //              \slot 0
4090 //                                 <- Z_fp
monitorenter()4091 void TemplateTable::monitorenter() {
4092   transition(atos, vtos);
4093 
4094   BLOCK_COMMENT("monitorenter {");
4095 
4096   // Check for NULL object.
4097   __ null_check(Z_tos);
4098   const int entry_size = frame::interpreter_frame_monitor_size() * wordSize;
4099   NearLabel allocated;
4100   // Initialize entry pointer.
4101   const Register Rfree_slot = Z_tmp_1;
4102   __ clear_reg(Rfree_slot, true, false); // Points to free slot or NULL. Don't set CC.
4103 
4104   // Find a free slot in the monitor block from top to bot (result in Rfree_slot).
4105   {
4106     const Register Rcurr_monitor = Z_ARG2;
4107     const Register Rbot = Z_ARG3; // Points to word under bottom of monitor block.
4108     const Register Rlocked_obj = Z_ARG4;
4109     NearLabel loop, exit, not_free;
4110     // Starting with top-most entry.
4111     __ get_monitors(Rcurr_monitor); // Rcur_monitor = IJAVA_STATE.monitors
4112     __ add2reg(Rbot, -frame::z_ijava_state_size, Z_fp);
4113 
4114 #ifdef ASSERT
4115     address reentry = NULL;
4116     { NearLabel ok;
4117       __ compareU64_and_branch(Rcurr_monitor, Rbot, Assembler::bcondNotHigh, ok);
4118       reentry = __ stop_chain_static(reentry, "IJAVA_STATE.monitors points below monitor block bottom");
4119       __ bind(ok);
4120     }
4121     { NearLabel ok;
4122       __ compareU64_and_branch(Rcurr_monitor, Z_esp, Assembler::bcondHigh, ok);
4123       reentry = __ stop_chain_static(reentry, "IJAVA_STATE.monitors above Z_esp");
4124       __ bind(ok);
4125     }
4126 #endif
4127 
4128     // Check if bottom reached, i.e. if there is at least one monitor.
4129     __ compareU64_and_branch(Rcurr_monitor, Rbot, Assembler::bcondEqual, exit);
4130 
4131     __ bind(loop);
4132     // Check if current entry is used.
4133     __ load_and_test_long(Rlocked_obj, Address(Rcurr_monitor, BasicObjectLock::obj_offset_in_bytes()));
4134     __ z_brne(not_free);
4135     // If not used then remember entry in Rfree_slot.
4136     __ z_lgr(Rfree_slot, Rcurr_monitor);
4137     __ bind(not_free);
4138     // Exit if current entry is for same object; this guarantees, that new monitor
4139     // used for recursive lock is above the older one.
4140     __ compareU64_and_branch(Rlocked_obj, Z_tos, Assembler::bcondEqual, exit);
4141     // otherwise advance to next entry
4142     __ add2reg(Rcurr_monitor, entry_size);
4143     // Check if bottom reached, if not at bottom then check this entry.
4144     __ compareU64_and_branch(Rcurr_monitor, Rbot, Assembler::bcondNotEqual, loop);
4145     __ bind(exit);
4146   }
4147 
4148   // Rfree_slot != NULL -> found one
4149   __ compareU64_and_branch(Rfree_slot, (intptr_t)0L, Assembler::bcondNotEqual, allocated);
4150 
4151   // Allocate one if there's no free slot.
4152   __ add_monitor_to_stack(false, Z_ARG3, Z_ARG4, Z_ARG5);
4153   __ get_monitors(Rfree_slot);
4154 
4155   // Rfree_slot: points to monitor entry.
4156   __ bind(allocated);
4157 
4158   // Increment bcp to point to the next bytecode, so exception
4159   // handling for async. exceptions work correctly.
4160   // The object has already been poped from the stack, so the
4161   // expression stack looks correct.
4162   __ add2reg(Z_bcp, 1, Z_bcp);
4163 
4164   // Store object.
4165   __ z_stg(Z_tos, BasicObjectLock::obj_offset_in_bytes(), Rfree_slot);
4166   __ lock_object(Rfree_slot, Z_tos);
4167 
4168   // Check to make sure this monitor doesn't cause stack overflow after locking.
4169   __ save_bcp();  // in case of exception
4170   __ generate_stack_overflow_check(0);
4171 
4172   // The bcp has already been incremented. Just need to dispatch to
4173   // next instruction.
4174   __ dispatch_next(vtos);
4175 
4176   BLOCK_COMMENT("} monitorenter");
4177 }
4178 
4179 
monitorexit()4180 void TemplateTable::monitorexit() {
4181   transition(atos, vtos);
4182 
4183   BLOCK_COMMENT("monitorexit {");
4184 
4185   // Check for NULL object.
4186   __ null_check(Z_tos);
4187 
4188   NearLabel found, not_found;
4189   const Register Rcurr_monitor = Z_ARG2;
4190 
4191   // Find matching slot.
4192   {
4193     const int entry_size = frame::interpreter_frame_monitor_size() * wordSize;
4194     NearLabel entry, loop;
4195 
4196     const Register Rbot = Z_ARG3; // Points to word under bottom of monitor block.
4197     const Register Rlocked_obj = Z_ARG4;
4198     // Starting with top-most entry.
4199     __ get_monitors(Rcurr_monitor); // Rcur_monitor = IJAVA_STATE.monitors
4200     __ add2reg(Rbot, -frame::z_ijava_state_size, Z_fp);
4201 
4202 #ifdef ASSERT
4203     address reentry = NULL;
4204     { NearLabel ok;
4205       __ compareU64_and_branch(Rcurr_monitor, Rbot, Assembler::bcondNotHigh, ok);
4206       reentry = __ stop_chain_static(reentry, "IJAVA_STATE.monitors points below monitor block bottom");
4207       __ bind(ok);
4208     }
4209     { NearLabel ok;
4210       __ compareU64_and_branch(Rcurr_monitor, Z_esp, Assembler::bcondHigh, ok);
4211       reentry = __ stop_chain_static(reentry, "IJAVA_STATE.monitors above Z_esp");
4212       __ bind(ok);
4213     }
4214 #endif
4215 
4216     // Check if bottom reached, i.e. if there is at least one monitor.
4217     __ compareU64_and_branch(Rcurr_monitor, Rbot, Assembler::bcondEqual, not_found);
4218 
4219     __ bind(loop);
4220     // Check if current entry is for same object.
4221     __ z_lg(Rlocked_obj, Address(Rcurr_monitor, BasicObjectLock::obj_offset_in_bytes()));
4222     // If same object then stop searching.
4223     __ compareU64_and_branch(Rlocked_obj, Z_tos, Assembler::bcondEqual, found);
4224     // Otherwise advance to next entry.
4225     __ add2reg(Rcurr_monitor, entry_size);
4226     // Check if bottom reached, if not at bottom then check this entry.
4227     __ compareU64_and_branch(Rcurr_monitor, Rbot, Assembler::bcondNotEqual, loop);
4228   }
4229 
4230   __ bind(not_found);
4231   // Error handling. Unlocking was not block-structured.
4232   __ call_VM(noreg, CAST_FROM_FN_PTR(address,
4233                    InterpreterRuntime::throw_illegal_monitor_state_exception));
4234   __ should_not_reach_here();
4235 
4236   __ bind(found);
4237   __ push_ptr(Z_tos); // Make sure object is on stack (contract with oopMaps).
4238   __ unlock_object(Rcurr_monitor, Z_tos);
4239   __ pop_ptr(Z_tos); // Discard object.
4240   BLOCK_COMMENT("} monitorexit");
4241 }
4242 
4243 // Wide instructions
wide()4244 void TemplateTable::wide() {
4245   transition(vtos, vtos);
4246 
4247   __ z_llgc(Z_R1_scratch, at_bcp(1));
4248   __ z_sllg(Z_R1_scratch, Z_R1_scratch, LogBytesPerWord);
4249   __ load_absolute_address(Z_tmp_1, (address) Interpreter::_wentry_point);
4250   __ mem2reg_opt(Z_tmp_1, Address(Z_tmp_1, Z_R1_scratch));
4251   __ z_br(Z_tmp_1);
4252   // Note: the bcp increment step is part of the individual wide
4253   // bytecode implementations.
4254 }
4255 
4256 // Multi arrays
multianewarray()4257 void TemplateTable::multianewarray() {
4258   transition(vtos, atos);
4259 
4260   __ z_llgc(Z_tmp_1, at_bcp(3)); // Get number of dimensions.
4261   // Slot count to byte offset.
4262   __ z_sllg(Z_tmp_1, Z_tmp_1, Interpreter::logStackElementSize);
4263   // Z_esp points past last_dim, so set to Z_ARG2 to first_dim address.
4264   __ load_address(Z_ARG2, Address(Z_esp, Z_tmp_1));
4265   call_VM(Z_RET,
4266           CAST_FROM_FN_PTR(address, InterpreterRuntime::multianewarray),
4267           Z_ARG2);
4268   // Pop dimensions from expression stack.
4269   __ z_agr(Z_esp, Z_tmp_1);
4270 }
4271