1 /*
2  * Copyright (c) 2008, 2021, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  *
23  */
24 
25 #include "precompiled.hpp"
26 #include "asm/assembler.inline.hpp"
27 #include "code/debugInfoRec.hpp"
28 #include "code/icBuffer.hpp"
29 #include "code/vtableStubs.hpp"
30 #include "compiler/oopMap.hpp"
31 #include "interpreter/interpreter.hpp"
32 #include "logging/log.hpp"
33 #include "memory/resourceArea.hpp"
34 #include "oops/compiledICHolder.hpp"
35 #include "oops/klass.inline.hpp"
36 #include "prims/methodHandles.hpp"
37 #include "runtime/jniHandles.hpp"
38 #include "runtime/sharedRuntime.hpp"
39 #include "runtime/safepointMechanism.hpp"
40 #include "runtime/stubRoutines.hpp"
41 #include "runtime/vframeArray.hpp"
42 #include "utilities/align.hpp"
43 #include "utilities/powerOfTwo.hpp"
44 #include "vmreg_arm.inline.hpp"
45 #ifdef COMPILER1
46 #include "c1/c1_Runtime1.hpp"
47 #endif
48 #ifdef COMPILER2
49 #include "opto/runtime.hpp"
50 #endif
51 
52 #define __ masm->
53 
54 class RegisterSaver {
55 public:
56 
57   // Special registers:
58   //              32-bit ARM     64-bit ARM
59   //  Rthread:       R10            R28
60   //  LR:            R14            R30
61 
62   // Rthread is callee saved in the C ABI and never changed by compiled code:
63   // no need to save it.
64 
65   // 2 slots for LR: the one at LR_offset and an other one at R14/R30_offset.
66   // The one at LR_offset is a return address that is needed by stack walking.
67   // A c2 method uses LR as a standard register so it may be live when we
68   // branch to the runtime. The slot at R14/R30_offset is for the value of LR
69   // in case it's live in the method we are coming from.
70 
71 
72   enum RegisterLayout {
73     fpu_save_size = FloatRegisterImpl::number_of_registers,
74 #ifndef __SOFTFP__
75     D0_offset = 0,
76 #endif
77     R0_offset = fpu_save_size,
78     R1_offset,
79     R2_offset,
80     R3_offset,
81     R4_offset,
82     R5_offset,
83     R6_offset,
84 #if (FP_REG_NUM != 7)
85     // if not saved as FP
86     R7_offset,
87 #endif
88     R8_offset,
89     R9_offset,
90 #if (FP_REG_NUM != 11)
91     // if not saved as FP
92     R11_offset,
93 #endif
94     R12_offset,
95     R14_offset,
96     FP_offset,
97     LR_offset,
98     reg_save_size,
99 
100     Rmethod_offset = R9_offset,
101     Rtemp_offset = R12_offset,
102   };
103 
104   // all regs but Rthread (R10), FP (R7 or R11), SP and PC
105   // (altFP_7_11 is the one amoung R7 and R11 which is not FP)
106 #define SAVED_BASE_REGS (RegisterSet(R0, R6) | RegisterSet(R8, R9) | RegisterSet(R12) | R14 | altFP_7_11)
107 
108 
109   //  When LR may be live in the nmethod from which we are comming
110   //  then lr_saved is true, the return address is saved before the
111   //  call to save_live_register by the caller and LR contains the
112   //  live value.
113 
114   static OopMap* save_live_registers(MacroAssembler* masm,
115                                      int* total_frame_words,
116                                      bool lr_saved = false);
117   static void restore_live_registers(MacroAssembler* masm, bool restore_lr = true);
118 
119 };
120 
121 
122 
123 
save_live_registers(MacroAssembler * masm,int * total_frame_words,bool lr_saved)124 OopMap* RegisterSaver::save_live_registers(MacroAssembler* masm,
125                                            int* total_frame_words,
126                                            bool lr_saved) {
127   *total_frame_words = reg_save_size;
128 
129   OopMapSet *oop_maps = new OopMapSet();
130   OopMap* map = new OopMap(VMRegImpl::slots_per_word * (*total_frame_words), 0);
131 
132   if (lr_saved) {
133     __ push(RegisterSet(FP));
134   } else {
135     __ push(RegisterSet(FP) | RegisterSet(LR));
136   }
137   __ push(SAVED_BASE_REGS);
138   if (HaveVFP) {
139     if (VM_Version::has_vfp3_32()) {
140       __ fpush(FloatRegisterSet(D16, 16));
141     } else {
142       if (FloatRegisterImpl::number_of_registers > 32) {
143         assert(FloatRegisterImpl::number_of_registers == 64, "nb fp registers should be 64");
144         __ sub(SP, SP, 32 * wordSize);
145       }
146     }
147     __ fpush(FloatRegisterSet(D0, 16));
148   } else {
149     __ sub(SP, SP, fpu_save_size * wordSize);
150   }
151 
152   int i;
153   int j=0;
154   for (i = R0_offset; i <= R9_offset; i++) {
155     if (j == FP_REG_NUM) {
156       // skip the FP register, managed below.
157       j++;
158     }
159     map->set_callee_saved(VMRegImpl::stack2reg(i), as_Register(j)->as_VMReg());
160     j++;
161   }
162   assert(j == R10->encoding(), "must be");
163 #if (FP_REG_NUM != 11)
164   // add R11, if not managed as FP
165   map->set_callee_saved(VMRegImpl::stack2reg(R11_offset), R11->as_VMReg());
166 #endif
167   map->set_callee_saved(VMRegImpl::stack2reg(R12_offset), R12->as_VMReg());
168   map->set_callee_saved(VMRegImpl::stack2reg(R14_offset), R14->as_VMReg());
169   if (HaveVFP) {
170     for (i = 0; i < (VM_Version::has_vfp3_32() ? 64 : 32); i+=2) {
171       map->set_callee_saved(VMRegImpl::stack2reg(i), as_FloatRegister(i)->as_VMReg());
172       map->set_callee_saved(VMRegImpl::stack2reg(i + 1), as_FloatRegister(i)->as_VMReg()->next());
173     }
174   }
175 
176   return map;
177 }
178 
restore_live_registers(MacroAssembler * masm,bool restore_lr)179 void RegisterSaver::restore_live_registers(MacroAssembler* masm, bool restore_lr) {
180   if (HaveVFP) {
181     __ fpop(FloatRegisterSet(D0, 16));
182     if (VM_Version::has_vfp3_32()) {
183       __ fpop(FloatRegisterSet(D16, 16));
184     } else {
185       if (FloatRegisterImpl::number_of_registers > 32) {
186         assert(FloatRegisterImpl::number_of_registers == 64, "nb fp registers should be 64");
187         __ add(SP, SP, 32 * wordSize);
188       }
189     }
190   } else {
191     __ add(SP, SP, fpu_save_size * wordSize);
192   }
193   __ pop(SAVED_BASE_REGS);
194   if (restore_lr) {
195     __ pop(RegisterSet(FP) | RegisterSet(LR));
196   } else {
197     __ pop(RegisterSet(FP));
198   }
199 }
200 
201 
push_result_registers(MacroAssembler * masm,BasicType ret_type)202 static void push_result_registers(MacroAssembler* masm, BasicType ret_type) {
203 #ifdef __ABI_HARD__
204   if (ret_type == T_DOUBLE || ret_type == T_FLOAT) {
205     __ sub(SP, SP, 8);
206     __ fstd(D0, Address(SP));
207     return;
208   }
209 #endif // __ABI_HARD__
210   __ raw_push(R0, R1);
211 }
212 
pop_result_registers(MacroAssembler * masm,BasicType ret_type)213 static void pop_result_registers(MacroAssembler* masm, BasicType ret_type) {
214 #ifdef __ABI_HARD__
215   if (ret_type == T_DOUBLE || ret_type == T_FLOAT) {
216     __ fldd(D0, Address(SP));
217     __ add(SP, SP, 8);
218     return;
219   }
220 #endif // __ABI_HARD__
221   __ raw_pop(R0, R1);
222 }
223 
push_param_registers(MacroAssembler * masm,int fp_regs_in_arguments)224 static void push_param_registers(MacroAssembler* masm, int fp_regs_in_arguments) {
225   // R1-R3 arguments need to be saved, but we push 4 registers for 8-byte alignment
226   __ push(RegisterSet(R0, R3));
227 
228   // preserve arguments
229   // Likely not needed as the locking code won't probably modify volatile FP registers,
230   // but there is no way to guarantee that
231   if (fp_regs_in_arguments) {
232     // convert fp_regs_in_arguments to a number of double registers
233     int double_regs_num = (fp_regs_in_arguments + 1) >> 1;
234     __ fpush_hardfp(FloatRegisterSet(D0, double_regs_num));
235   }
236 }
237 
pop_param_registers(MacroAssembler * masm,int fp_regs_in_arguments)238 static void pop_param_registers(MacroAssembler* masm, int fp_regs_in_arguments) {
239   if (fp_regs_in_arguments) {
240     int double_regs_num = (fp_regs_in_arguments + 1) >> 1;
241     __ fpop_hardfp(FloatRegisterSet(D0, double_regs_num));
242   }
243   __ pop(RegisterSet(R0, R3));
244 }
245 
246 
247 
248 // Is vector's size (in bytes) bigger than a size saved by default?
249 // All vector registers are saved by default on ARM.
is_wide_vector(int size)250 bool SharedRuntime::is_wide_vector(int size) {
251   return false;
252 }
253 
c_calling_convention(const BasicType * sig_bt,VMRegPair * regs,VMRegPair * regs2,int total_args_passed)254 int SharedRuntime::c_calling_convention(const BasicType *sig_bt,
255                                         VMRegPair *regs,
256                                         VMRegPair *regs2,
257                                         int total_args_passed) {
258   assert(regs2 == NULL, "not needed on arm");
259 
260   int slot = 0;
261   int ireg = 0;
262 #ifdef __ABI_HARD__
263   int fp_slot = 0;
264   int single_fpr_slot = 0;
265 #endif // __ABI_HARD__
266   for (int i = 0; i < total_args_passed; i++) {
267     switch (sig_bt[i]) {
268     case T_SHORT:
269     case T_CHAR:
270     case T_BYTE:
271     case T_BOOLEAN:
272     case T_INT:
273     case T_ARRAY:
274     case T_OBJECT:
275     case T_ADDRESS:
276     case T_METADATA:
277 #ifndef __ABI_HARD__
278     case T_FLOAT:
279 #endif // !__ABI_HARD__
280       if (ireg < 4) {
281         Register r = as_Register(ireg);
282         regs[i].set1(r->as_VMReg());
283         ireg++;
284       } else {
285         regs[i].set1(VMRegImpl::stack2reg(slot));
286         slot++;
287       }
288       break;
289     case T_LONG:
290 #ifndef __ABI_HARD__
291     case T_DOUBLE:
292 #endif // !__ABI_HARD__
293       assert((i + 1) < total_args_passed && sig_bt[i+1] == T_VOID, "missing Half" );
294       if (ireg <= 2) {
295 #if (ALIGN_WIDE_ARGUMENTS == 1)
296         if(ireg & 1) ireg++;  // Aligned location required
297 #endif
298         Register r1 = as_Register(ireg);
299         Register r2 = as_Register(ireg + 1);
300         regs[i].set_pair(r2->as_VMReg(), r1->as_VMReg());
301         ireg += 2;
302 #if (ALIGN_WIDE_ARGUMENTS == 0)
303       } else if (ireg == 3) {
304         // uses R3 + one stack slot
305         Register r = as_Register(ireg);
306         regs[i].set_pair(VMRegImpl::stack2reg(slot), r->as_VMReg());
307         ireg += 1;
308         slot += 1;
309 #endif
310       } else {
311         if (slot & 1) slot++; // Aligned location required
312         regs[i].set_pair(VMRegImpl::stack2reg(slot+1), VMRegImpl::stack2reg(slot));
313         slot += 2;
314         ireg = 4;
315       }
316       break;
317     case T_VOID:
318       regs[i].set_bad();
319       break;
320 #ifdef __ABI_HARD__
321     case T_FLOAT:
322       if ((fp_slot < 16)||(single_fpr_slot & 1)) {
323         if ((single_fpr_slot & 1) == 0) {
324           single_fpr_slot = fp_slot;
325           fp_slot += 2;
326         }
327         FloatRegister r = as_FloatRegister(single_fpr_slot);
328         single_fpr_slot++;
329         regs[i].set1(r->as_VMReg());
330       } else {
331         regs[i].set1(VMRegImpl::stack2reg(slot));
332         slot++;
333       }
334       break;
335     case T_DOUBLE:
336       assert(ALIGN_WIDE_ARGUMENTS == 1, "ABI_HARD not supported with unaligned wide arguments");
337       if (fp_slot <= 14) {
338         FloatRegister r1 = as_FloatRegister(fp_slot);
339         FloatRegister r2 = as_FloatRegister(fp_slot+1);
340         regs[i].set_pair(r2->as_VMReg(), r1->as_VMReg());
341         fp_slot += 2;
342       } else {
343         if(slot & 1) slot++;
344         regs[i].set_pair(VMRegImpl::stack2reg(slot+1), VMRegImpl::stack2reg(slot));
345         slot += 2;
346         single_fpr_slot = 16;
347       }
348       break;
349 #endif // __ABI_HARD__
350     default:
351       ShouldNotReachHere();
352     }
353   }
354   return slot;
355 }
356 
vector_calling_convention(VMRegPair * regs,uint num_bits,uint total_args_passed)357 int SharedRuntime::vector_calling_convention(VMRegPair *regs,
358                                              uint num_bits,
359                                              uint total_args_passed) {
360   Unimplemented();
361   return 0;
362 }
363 
java_calling_convention(const BasicType * sig_bt,VMRegPair * regs,int total_args_passed)364 int SharedRuntime::java_calling_convention(const BasicType *sig_bt,
365                                            VMRegPair *regs,
366                                            int total_args_passed) {
367 #ifdef __SOFTFP__
368   // soft float is the same as the C calling convention.
369   return c_calling_convention(sig_bt, regs, NULL, total_args_passed);
370 #endif // __SOFTFP__
371   int slot = 0;
372   int ireg = 0;
373   int freg = 0;
374   int single_fpr = 0;
375 
376   for (int i = 0; i < total_args_passed; i++) {
377     switch (sig_bt[i]) {
378     case T_SHORT:
379     case T_CHAR:
380     case T_BYTE:
381     case T_BOOLEAN:
382     case T_INT:
383     case T_ARRAY:
384     case T_OBJECT:
385     case T_ADDRESS:
386       if (ireg < 4) {
387         Register r = as_Register(ireg++);
388         regs[i].set1(r->as_VMReg());
389       } else {
390         regs[i].set1(VMRegImpl::stack2reg(slot++));
391       }
392       break;
393     case T_FLOAT:
394       // C2 utilizes S14/S15 for mem-mem moves
395       if ((freg < 16 COMPILER2_PRESENT(-2)) || (single_fpr & 1)) {
396         if ((single_fpr & 1) == 0) {
397           single_fpr = freg;
398           freg += 2;
399         }
400         FloatRegister r = as_FloatRegister(single_fpr++);
401         regs[i].set1(r->as_VMReg());
402       } else {
403         regs[i].set1(VMRegImpl::stack2reg(slot++));
404       }
405       break;
406     case T_DOUBLE:
407       // C2 utilizes S14/S15 for mem-mem moves
408       if (freg <= 14 COMPILER2_PRESENT(-2)) {
409         FloatRegister r1 = as_FloatRegister(freg);
410         FloatRegister r2 = as_FloatRegister(freg + 1);
411         regs[i].set_pair(r2->as_VMReg(), r1->as_VMReg());
412         freg += 2;
413       } else {
414         // Keep internally the aligned calling convention,
415         // ignoring ALIGN_WIDE_ARGUMENTS
416         if (slot & 1) slot++;
417         regs[i].set_pair(VMRegImpl::stack2reg(slot + 1), VMRegImpl::stack2reg(slot));
418         slot += 2;
419         single_fpr = 16;
420       }
421       break;
422     case T_LONG:
423       // Keep internally the aligned calling convention,
424       // ignoring ALIGN_WIDE_ARGUMENTS
425       if (ireg <= 2) {
426         if (ireg & 1) ireg++;
427         Register r1 = as_Register(ireg);
428         Register r2 = as_Register(ireg + 1);
429         regs[i].set_pair(r2->as_VMReg(), r1->as_VMReg());
430         ireg += 2;
431       } else {
432         if (slot & 1) slot++;
433         regs[i].set_pair(VMRegImpl::stack2reg(slot + 1), VMRegImpl::stack2reg(slot));
434         slot += 2;
435         ireg = 4;
436       }
437       break;
438     case T_VOID:
439       regs[i].set_bad();
440       break;
441     default:
442       ShouldNotReachHere();
443     }
444   }
445 
446   if (slot & 1) slot++;
447   return slot;
448 }
449 
patch_callers_callsite(MacroAssembler * masm)450 static void patch_callers_callsite(MacroAssembler *masm) {
451   Label skip;
452 
453   __ ldr(Rtemp, Address(Rmethod, Method::code_offset()));
454   __ cbz(Rtemp, skip);
455 
456   // Pushing an even number of registers for stack alignment.
457   // Selecting R9, which had to be saved anyway for some platforms.
458   __ push(RegisterSet(R0, R3) | R9 | LR);
459   __ fpush_hardfp(FloatRegisterSet(D0, 8));
460 
461   __ mov(R0, Rmethod);
462   __ mov(R1, LR);
463   __ call(CAST_FROM_FN_PTR(address, SharedRuntime::fixup_callers_callsite));
464 
465   __ fpop_hardfp(FloatRegisterSet(D0, 8));
466   __ pop(RegisterSet(R0, R3) | R9 | LR);
467 
468   __ bind(skip);
469 }
470 
gen_i2c_adapter(MacroAssembler * masm,int total_args_passed,int comp_args_on_stack,const BasicType * sig_bt,const VMRegPair * regs)471 void SharedRuntime::gen_i2c_adapter(MacroAssembler *masm,
472                                     int total_args_passed, int comp_args_on_stack,
473                                     const BasicType *sig_bt, const VMRegPair *regs) {
474   // TODO: ARM - May be can use ldm to load arguments
475   const Register tmp = Rtemp; // avoid erasing R5_mh
476 
477   // Next assert may not be needed but safer. Extra analysis required
478   // if this there is not enough free registers and we need to use R5 here.
479   assert_different_registers(tmp, R5_mh);
480 
481   // 6243940 We might end up in handle_wrong_method if
482   // the callee is deoptimized as we race thru here. If that
483   // happens we don't want to take a safepoint because the
484   // caller frame will look interpreted and arguments are now
485   // "compiled" so it is much better to make this transition
486   // invisible to the stack walking code. Unfortunately if
487   // we try and find the callee by normal means a safepoint
488   // is possible. So we stash the desired callee in the thread
489   // and the vm will find there should this case occur.
490   Address callee_target_addr(Rthread, JavaThread::callee_target_offset());
491   __ str(Rmethod, callee_target_addr);
492 
493 
494   assert_different_registers(tmp, R0, R1, R2, R3, Rsender_sp, Rmethod);
495 
496   const Register initial_sp = Rmethod; // temporarily scratched
497 
498   // Old code was modifying R4 but this looks unsafe (particularly with JSR292)
499   assert_different_registers(tmp, R0, R1, R2, R3, Rsender_sp, initial_sp);
500 
501   __ mov(initial_sp, SP);
502 
503   if (comp_args_on_stack) {
504     __ sub_slow(SP, SP, comp_args_on_stack * VMRegImpl::stack_slot_size);
505   }
506   __ bic(SP, SP, StackAlignmentInBytes - 1);
507 
508   for (int i = 0; i < total_args_passed; i++) {
509     if (sig_bt[i] == T_VOID) {
510       assert(i > 0 && (sig_bt[i-1] == T_LONG || sig_bt[i-1] == T_DOUBLE), "missing half");
511       continue;
512     }
513     assert(!regs[i].second()->is_valid() || regs[i].first()->next() == regs[i].second(), "must be ordered");
514     int arg_offset = Interpreter::expr_offset_in_bytes(total_args_passed - 1 - i);
515 
516     VMReg r_1 = regs[i].first();
517     VMReg r_2 = regs[i].second();
518     if (r_1->is_stack()) {
519       int stack_offset = r_1->reg2stack() * VMRegImpl::stack_slot_size;
520       if (!r_2->is_valid()) {
521         __ ldr(tmp, Address(initial_sp, arg_offset));
522         __ str(tmp, Address(SP, stack_offset));
523       } else {
524         __ ldr(tmp, Address(initial_sp, arg_offset - Interpreter::stackElementSize));
525         __ str(tmp, Address(SP, stack_offset));
526         __ ldr(tmp, Address(initial_sp, arg_offset));
527         __ str(tmp, Address(SP, stack_offset + wordSize));
528       }
529     } else if (r_1->is_Register()) {
530       if (!r_2->is_valid()) {
531         __ ldr(r_1->as_Register(), Address(initial_sp, arg_offset));
532       } else {
533         __ ldr(r_1->as_Register(), Address(initial_sp, arg_offset - Interpreter::stackElementSize));
534         __ ldr(r_2->as_Register(), Address(initial_sp, arg_offset));
535       }
536     } else if (r_1->is_FloatRegister()) {
537 #ifdef __SOFTFP__
538       ShouldNotReachHere();
539 #endif // __SOFTFP__
540       if (!r_2->is_valid()) {
541         __ flds(r_1->as_FloatRegister(), Address(initial_sp, arg_offset));
542       } else {
543         __ fldd(r_1->as_FloatRegister(), Address(initial_sp, arg_offset - Interpreter::stackElementSize));
544       }
545     } else {
546       assert(!r_1->is_valid() && !r_2->is_valid(), "must be");
547     }
548   }
549 
550   // restore Rmethod (scratched for initial_sp)
551   __ ldr(Rmethod, callee_target_addr);
552   __ ldr(PC, Address(Rmethod, Method::from_compiled_offset()));
553 
554 }
555 
gen_c2i_adapter(MacroAssembler * masm,int total_args_passed,int comp_args_on_stack,const BasicType * sig_bt,const VMRegPair * regs,Label & skip_fixup)556 static void gen_c2i_adapter(MacroAssembler *masm,
557                             int total_args_passed,  int comp_args_on_stack,
558                             const BasicType *sig_bt, const VMRegPair *regs,
559                             Label& skip_fixup) {
560   // TODO: ARM - May be can use stm to deoptimize arguments
561   const Register tmp = Rtemp;
562 
563   patch_callers_callsite(masm);
564   __ bind(skip_fixup);
565 
566   __ mov(Rsender_sp, SP); // not yet saved
567 
568 
569   int extraspace = total_args_passed * Interpreter::stackElementSize;
570   if (extraspace) {
571     __ sub_slow(SP, SP, extraspace);
572   }
573 
574   for (int i = 0; i < total_args_passed; i++) {
575     if (sig_bt[i] == T_VOID) {
576       assert(i > 0 && (sig_bt[i-1] == T_LONG || sig_bt[i-1] == T_DOUBLE), "missing half");
577       continue;
578     }
579     int stack_offset = (total_args_passed - 1 - i) * Interpreter::stackElementSize;
580 
581     VMReg r_1 = regs[i].first();
582     VMReg r_2 = regs[i].second();
583     if (r_1->is_stack()) {
584       int arg_offset = r_1->reg2stack() * VMRegImpl::stack_slot_size + extraspace;
585       if (!r_2->is_valid()) {
586         __ ldr(tmp, Address(SP, arg_offset));
587         __ str(tmp, Address(SP, stack_offset));
588       } else {
589         __ ldr(tmp, Address(SP, arg_offset));
590         __ str(tmp, Address(SP, stack_offset - Interpreter::stackElementSize));
591         __ ldr(tmp, Address(SP, arg_offset + wordSize));
592         __ str(tmp, Address(SP, stack_offset));
593       }
594     } else if (r_1->is_Register()) {
595       if (!r_2->is_valid()) {
596         __ str(r_1->as_Register(), Address(SP, stack_offset));
597       } else {
598         __ str(r_1->as_Register(), Address(SP, stack_offset - Interpreter::stackElementSize));
599         __ str(r_2->as_Register(), Address(SP, stack_offset));
600       }
601     } else if (r_1->is_FloatRegister()) {
602 #ifdef __SOFTFP__
603       ShouldNotReachHere();
604 #endif // __SOFTFP__
605       if (!r_2->is_valid()) {
606         __ fsts(r_1->as_FloatRegister(), Address(SP, stack_offset));
607       } else {
608         __ fstd(r_1->as_FloatRegister(), Address(SP, stack_offset - Interpreter::stackElementSize));
609       }
610     } else {
611       assert(!r_1->is_valid() && !r_2->is_valid(), "must be");
612     }
613   }
614 
615   __ ldr(PC, Address(Rmethod, Method::interpreter_entry_offset()));
616 
617 }
618 
generate_i2c2i_adapters(MacroAssembler * masm,int total_args_passed,int comp_args_on_stack,const BasicType * sig_bt,const VMRegPair * regs,AdapterFingerPrint * fingerprint)619 AdapterHandlerEntry* SharedRuntime::generate_i2c2i_adapters(MacroAssembler *masm,
620                                                             int total_args_passed,
621                                                             int comp_args_on_stack,
622                                                             const BasicType *sig_bt,
623                                                             const VMRegPair *regs,
624                                                             AdapterFingerPrint* fingerprint) {
625   address i2c_entry = __ pc();
626   gen_i2c_adapter(masm, total_args_passed, comp_args_on_stack, sig_bt, regs);
627 
628   address c2i_unverified_entry = __ pc();
629   Label skip_fixup;
630   const Register receiver       = R0;
631   const Register holder_klass   = Rtemp; // XXX should be OK for C2 but not 100% sure
632   const Register receiver_klass = R4;
633 
634   __ load_klass(receiver_klass, receiver);
635   __ ldr(holder_klass, Address(Ricklass, CompiledICHolder::holder_klass_offset()));
636   __ ldr(Rmethod, Address(Ricklass, CompiledICHolder::holder_metadata_offset()));
637   __ cmp(receiver_klass, holder_klass);
638 
639   __ ldr(Rtemp, Address(Rmethod, Method::code_offset()), eq);
640   __ cmp(Rtemp, 0, eq);
641   __ b(skip_fixup, eq);
642   __ jump(SharedRuntime::get_ic_miss_stub(), relocInfo::runtime_call_type, noreg, ne);
643 
644   address c2i_entry = __ pc();
645   gen_c2i_adapter(masm, total_args_passed, comp_args_on_stack, sig_bt, regs, skip_fixup);
646 
647   __ flush();
648   return AdapterHandlerLibrary::new_entry(fingerprint, i2c_entry, c2i_entry, c2i_unverified_entry);
649 }
650 
651 
reg2offset_in(VMReg r)652 static int reg2offset_in(VMReg r) {
653   // Account for saved FP and LR
654   return r->reg2stack() * VMRegImpl::stack_slot_size + 2*wordSize;
655 }
656 
reg2offset_out(VMReg r)657 static int reg2offset_out(VMReg r) {
658   return (r->reg2stack() + SharedRuntime::out_preserve_stack_slots()) * VMRegImpl::stack_slot_size;
659 }
660 
661 
verify_oop_args(MacroAssembler * masm,const methodHandle & method,const BasicType * sig_bt,const VMRegPair * regs)662 static void verify_oop_args(MacroAssembler* masm,
663                             const methodHandle& method,
664                             const BasicType* sig_bt,
665                             const VMRegPair* regs) {
666   Register temp_reg = Rmethod;  // not part of any compiled calling seq
667   if (VerifyOops) {
668     for (int i = 0; i < method->size_of_parameters(); i++) {
669       if (sig_bt[i] == T_OBJECT || sig_bt[i] == T_ARRAY) {
670         VMReg r = regs[i].first();
671         assert(r->is_valid(), "bad oop arg");
672         if (r->is_stack()) {
673           __ ldr(temp_reg, Address(SP, r->reg2stack() * VMRegImpl::stack_slot_size));
674           __ verify_oop(temp_reg);
675         } else {
676           __ verify_oop(r->as_Register());
677         }
678       }
679     }
680   }
681 }
682 
gen_special_dispatch(MacroAssembler * masm,const methodHandle & method,const BasicType * sig_bt,const VMRegPair * regs)683 static void gen_special_dispatch(MacroAssembler* masm,
684                                  const methodHandle& method,
685                                  const BasicType* sig_bt,
686                                  const VMRegPair* regs) {
687   verify_oop_args(masm, method, sig_bt, regs);
688   vmIntrinsics::ID iid = method->intrinsic_id();
689 
690   // Now write the args into the outgoing interpreter space
691   bool     has_receiver   = false;
692   Register receiver_reg   = noreg;
693   int      member_arg_pos = -1;
694   Register member_reg     = noreg;
695   int      ref_kind       = MethodHandles::signature_polymorphic_intrinsic_ref_kind(iid);
696   if (ref_kind != 0) {
697     member_arg_pos = method->size_of_parameters() - 1;  // trailing MemberName argument
698     member_reg = Rmethod;  // known to be free at this point
699     has_receiver = MethodHandles::ref_kind_has_receiver(ref_kind);
700   } else if (iid == vmIntrinsics::_invokeBasic) {
701     has_receiver = true;
702   } else {
703     fatal("unexpected intrinsic id %d", vmIntrinsics::as_int(iid));
704   }
705 
706   if (member_reg != noreg) {
707     // Load the member_arg into register, if necessary.
708     SharedRuntime::check_member_name_argument_is_last_argument(method, sig_bt, regs);
709     VMReg r = regs[member_arg_pos].first();
710     if (r->is_stack()) {
711       __ ldr(member_reg, Address(SP, r->reg2stack() * VMRegImpl::stack_slot_size));
712     } else {
713       // no data motion is needed
714       member_reg = r->as_Register();
715     }
716   }
717 
718   if (has_receiver) {
719     // Make sure the receiver is loaded into a register.
720     assert(method->size_of_parameters() > 0, "oob");
721     assert(sig_bt[0] == T_OBJECT, "receiver argument must be an object");
722     VMReg r = regs[0].first();
723     assert(r->is_valid(), "bad receiver arg");
724     if (r->is_stack()) {
725       // Porting note:  This assumes that compiled calling conventions always
726       // pass the receiver oop in a register.  If this is not true on some
727       // platform, pick a temp and load the receiver from stack.
728       assert(false, "receiver always in a register");
729       receiver_reg = j_rarg0;  // known to be free at this point
730       __ ldr(receiver_reg, Address(SP, r->reg2stack() * VMRegImpl::stack_slot_size));
731     } else {
732       // no data motion is needed
733       receiver_reg = r->as_Register();
734     }
735   }
736 
737   // Figure out which address we are really jumping to:
738   MethodHandles::generate_method_handle_dispatch(masm, iid,
739                                                  receiver_reg, member_reg, /*for_compiler_entry:*/ true);
740 }
741 
742 // ---------------------------------------------------------------------------
743 // Generate a native wrapper for a given method.  The method takes arguments
744 // in the Java compiled code convention, marshals them to the native
745 // convention (handlizes oops, etc), transitions to native, makes the call,
746 // returns to java state (possibly blocking), unhandlizes any result and
747 // returns.
generate_native_wrapper(MacroAssembler * masm,const methodHandle & method,int compile_id,BasicType * in_sig_bt,VMRegPair * in_regs,BasicType ret_type,address critical_entry)748 nmethod* SharedRuntime::generate_native_wrapper(MacroAssembler* masm,
749                                                 const methodHandle& method,
750                                                 int compile_id,
751                                                 BasicType* in_sig_bt,
752                                                 VMRegPair* in_regs,
753                                                 BasicType ret_type,
754                                                 address critical_entry) {
755   if (method->is_method_handle_intrinsic()) {
756     vmIntrinsics::ID iid = method->intrinsic_id();
757     intptr_t start = (intptr_t)__ pc();
758     int vep_offset = ((intptr_t)__ pc()) - start;
759     gen_special_dispatch(masm,
760                          method,
761                          in_sig_bt,
762                          in_regs);
763     int frame_complete = ((intptr_t)__ pc()) - start;  // not complete, period
764     __ flush();
765     int stack_slots = SharedRuntime::out_preserve_stack_slots();  // no out slots at all, actually
766     return nmethod::new_native_nmethod(method,
767                                        compile_id,
768                                        masm->code(),
769                                        vep_offset,
770                                        frame_complete,
771                                        stack_slots / VMRegImpl::slots_per_word,
772                                        in_ByteSize(-1),
773                                        in_ByteSize(-1),
774                                        (OopMapSet*)NULL);
775   }
776   // Arguments for JNI method include JNIEnv and Class if static
777 
778   // Usage of Rtemp should be OK since scratched by native call
779 
780   bool is_static = method->is_static();
781 
782   const int total_in_args = method->size_of_parameters();
783   int total_c_args = total_in_args + 1;
784   if (is_static) {
785     total_c_args++;
786   }
787 
788   BasicType* out_sig_bt = NEW_RESOURCE_ARRAY(BasicType, total_c_args);
789   VMRegPair* out_regs   = NEW_RESOURCE_ARRAY(VMRegPair, total_c_args);
790 
791   int argc = 0;
792   out_sig_bt[argc++] = T_ADDRESS;
793   if (is_static) {
794     out_sig_bt[argc++] = T_OBJECT;
795   }
796 
797   int i;
798   for (i = 0; i < total_in_args; i++) {
799     out_sig_bt[argc++] = in_sig_bt[i];
800   }
801 
802   int out_arg_slots = c_calling_convention(out_sig_bt, out_regs, NULL, total_c_args);
803   int stack_slots = SharedRuntime::out_preserve_stack_slots() + out_arg_slots;
804   // Since object arguments need to be wrapped, we must preserve space
805   // for those object arguments which come in registers (GPR_PARAMS maximum)
806   // plus one more slot for Klass handle (for static methods)
807   int oop_handle_offset = stack_slots;
808   stack_slots += (GPR_PARAMS + 1) * VMRegImpl::slots_per_word;
809 
810   // Plus a lock if needed
811   int lock_slot_offset = 0;
812   if (method->is_synchronized()) {
813     lock_slot_offset = stack_slots;
814     assert(sizeof(BasicLock) == wordSize, "adjust this code");
815     stack_slots += VMRegImpl::slots_per_word;
816   }
817 
818   // Space to save return address and FP
819   stack_slots += 2 * VMRegImpl::slots_per_word;
820 
821   // Calculate the final stack size taking account of alignment
822   stack_slots = align_up(stack_slots, StackAlignmentInBytes / VMRegImpl::stack_slot_size);
823   int stack_size = stack_slots * VMRegImpl::stack_slot_size;
824   int lock_slot_fp_offset = stack_size - 2 * wordSize -
825     lock_slot_offset * VMRegImpl::stack_slot_size;
826 
827   // Unverified entry point
828   address start = __ pc();
829 
830   // Inline cache check, same as in C1_MacroAssembler::inline_cache_check()
831   const Register receiver = R0; // see receiverOpr()
832   __ load_klass(Rtemp, receiver);
833   __ cmp(Rtemp, Ricklass);
834   Label verified;
835 
836   __ b(verified, eq); // jump over alignment no-ops too
837   __ jump(SharedRuntime::get_ic_miss_stub(), relocInfo::runtime_call_type, Rtemp);
838   __ align(CodeEntryAlignment);
839 
840   // Verified entry point
841   __ bind(verified);
842   int vep_offset = __ pc() - start;
843 
844 
845   if ((InlineObjectHash && method->intrinsic_id() == vmIntrinsics::_hashCode) || (method->intrinsic_id() == vmIntrinsics::_identityHashCode)) {
846     // Object.hashCode, System.identityHashCode can pull the hashCode from the header word
847     // instead of doing a full VM transition once it's been computed.
848     Label slow_case;
849     const Register obj_reg = R0;
850 
851     // Unlike for Object.hashCode, System.identityHashCode is static method and
852     // gets object as argument instead of the receiver.
853     if (method->intrinsic_id() == vmIntrinsics::_identityHashCode) {
854       assert(method->is_static(), "method should be static");
855       // return 0 for null reference input, return val = R0 = obj_reg = 0
856       __ cmp(obj_reg, 0);
857       __ bx(LR, eq);
858     }
859 
860     __ ldr(Rtemp, Address(obj_reg, oopDesc::mark_offset_in_bytes()));
861 
862     assert(markWord::unlocked_value == 1, "adjust this code");
863     __ tbz(Rtemp, exact_log2(markWord::unlocked_value), slow_case);
864 
865     if (UseBiasedLocking) {
866       assert(is_power_of_2(markWord::biased_lock_bit_in_place), "adjust this code");
867       __ tbnz(Rtemp, exact_log2(markWord::biased_lock_bit_in_place), slow_case);
868     }
869 
870     __ bics(Rtemp, Rtemp, ~markWord::hash_mask_in_place);
871     __ mov(R0, AsmOperand(Rtemp, lsr, markWord::hash_shift), ne);
872     __ bx(LR, ne);
873 
874     __ bind(slow_case);
875   }
876 
877   // Bang stack pages
878   __ arm_stack_overflow_check(stack_size, Rtemp);
879 
880   // Setup frame linkage
881   __ raw_push(FP, LR);
882   __ mov(FP, SP);
883   __ sub_slow(SP, SP, stack_size - 2*wordSize);
884 
885   int frame_complete = __ pc() - start;
886 
887   OopMapSet* oop_maps = new OopMapSet();
888   OopMap* map = new OopMap(stack_slots * 2, 0 /* arg_slots*/);
889   const int extra_args = is_static ? 2 : 1;
890   int receiver_offset = -1;
891   int fp_regs_in_arguments = 0;
892 
893   for (i = total_in_args; --i >= 0; ) {
894     switch (in_sig_bt[i]) {
895     case T_ARRAY:
896     case T_OBJECT: {
897       VMReg src = in_regs[i].first();
898       VMReg dst = out_regs[i + extra_args].first();
899       if (src->is_stack()) {
900         assert(dst->is_stack(), "must be");
901         assert(i != 0, "Incoming receiver is always in a register");
902         __ ldr(Rtemp, Address(FP, reg2offset_in(src)));
903         __ cmp(Rtemp, 0);
904         __ add(Rtemp, FP, reg2offset_in(src), ne);
905         __ str(Rtemp, Address(SP, reg2offset_out(dst)));
906         int offset_in_older_frame = src->reg2stack() + SharedRuntime::out_preserve_stack_slots();
907         map->set_oop(VMRegImpl::stack2reg(offset_in_older_frame + stack_slots));
908       } else {
909         int offset = oop_handle_offset * VMRegImpl::stack_slot_size;
910         __ str(src->as_Register(), Address(SP, offset));
911         map->set_oop(VMRegImpl::stack2reg(oop_handle_offset));
912         if ((i == 0) && (!is_static)) {
913           receiver_offset = offset;
914         }
915         oop_handle_offset += VMRegImpl::slots_per_word;
916 
917         if (dst->is_stack()) {
918           __ movs(Rtemp, src->as_Register());
919           __ add(Rtemp, SP, offset, ne);
920           __ str(Rtemp, Address(SP, reg2offset_out(dst)));
921         } else {
922           __ movs(dst->as_Register(), src->as_Register());
923           __ add(dst->as_Register(), SP, offset, ne);
924         }
925       }
926     }
927 
928     case T_VOID:
929       break;
930 
931 
932 #ifdef __SOFTFP__
933     case T_DOUBLE:
934 #endif
935     case T_LONG: {
936       VMReg src_1 = in_regs[i].first();
937       VMReg src_2 = in_regs[i].second();
938       VMReg dst_1 = out_regs[i + extra_args].first();
939       VMReg dst_2 = out_regs[i + extra_args].second();
940 #if (ALIGN_WIDE_ARGUMENTS == 0)
941       // C convention can mix a register and a stack slot for a
942       // 64-bits native argument.
943 
944       // Note: following code should work independently of whether
945       // the Java calling convention follows C convention or whether
946       // it aligns 64-bit values.
947       if (dst_2->is_Register()) {
948         if (src_1->as_Register() != dst_1->as_Register()) {
949           assert(src_1->as_Register() != dst_2->as_Register() &&
950                  src_2->as_Register() != dst_2->as_Register(), "must be");
951           __ mov(dst_2->as_Register(), src_2->as_Register());
952           __ mov(dst_1->as_Register(), src_1->as_Register());
953         } else {
954           assert(src_2->as_Register() == dst_2->as_Register(), "must be");
955         }
956       } else if (src_2->is_Register()) {
957         if (dst_1->is_Register()) {
958           // dst mixes a register and a stack slot
959           assert(dst_2->is_stack() && src_1->is_Register() && src_2->is_Register(), "must be");
960           assert(src_1->as_Register() != dst_1->as_Register(), "must be");
961           __ str(src_2->as_Register(), Address(SP, reg2offset_out(dst_2)));
962           __ mov(dst_1->as_Register(), src_1->as_Register());
963         } else {
964           // registers to stack slots
965           assert(dst_2->is_stack() && src_1->is_Register() && src_2->is_Register(), "must be");
966           __ str(src_1->as_Register(), Address(SP, reg2offset_out(dst_1)));
967           __ str(src_2->as_Register(), Address(SP, reg2offset_out(dst_2)));
968         }
969       } else if (src_1->is_Register()) {
970         if (dst_1->is_Register()) {
971           // src and dst must be R3 + stack slot
972           assert(dst_1->as_Register() == src_1->as_Register(), "must be");
973           __ ldr(Rtemp,    Address(FP, reg2offset_in(src_2)));
974           __ str(Rtemp,    Address(SP, reg2offset_out(dst_2)));
975         } else {
976           // <R3,stack> -> <stack,stack>
977           assert(dst_2->is_stack() && src_2->is_stack(), "must be");
978           __ ldr(LR, Address(FP, reg2offset_in(src_2)));
979           __ str(src_1->as_Register(), Address(SP, reg2offset_out(dst_1)));
980           __ str(LR, Address(SP, reg2offset_out(dst_2)));
981         }
982       } else {
983         assert(src_2->is_stack() && dst_1->is_stack() && dst_2->is_stack(), "must be");
984         __ ldr(Rtemp, Address(FP, reg2offset_in(src_1)));
985         __ ldr(LR,    Address(FP, reg2offset_in(src_2)));
986         __ str(Rtemp, Address(SP, reg2offset_out(dst_1)));
987         __ str(LR,    Address(SP, reg2offset_out(dst_2)));
988       }
989 #else // ALIGN_WIDE_ARGUMENTS
990       if (src_1->is_stack()) {
991         assert(src_2->is_stack() && dst_1->is_stack() && dst_2->is_stack(), "must be");
992         __ ldr(Rtemp, Address(FP, reg2offset_in(src_1)));
993         __ ldr(LR,    Address(FP, reg2offset_in(src_2)));
994         __ str(Rtemp, Address(SP, reg2offset_out(dst_1)));
995         __ str(LR,    Address(SP, reg2offset_out(dst_2)));
996       } else if (dst_1->is_stack()) {
997         assert(dst_2->is_stack() && src_1->is_Register() && src_2->is_Register(), "must be");
998         __ str(src_1->as_Register(), Address(SP, reg2offset_out(dst_1)));
999         __ str(src_2->as_Register(), Address(SP, reg2offset_out(dst_2)));
1000       } else if (src_1->as_Register() == dst_1->as_Register()) {
1001         assert(src_2->as_Register() == dst_2->as_Register(), "must be");
1002       } else {
1003         assert(src_1->as_Register() != dst_2->as_Register() &&
1004                src_2->as_Register() != dst_2->as_Register(), "must be");
1005         __ mov(dst_2->as_Register(), src_2->as_Register());
1006         __ mov(dst_1->as_Register(), src_1->as_Register());
1007       }
1008 #endif // ALIGN_WIDE_ARGUMENTS
1009       break;
1010     }
1011 
1012 #if (!defined __SOFTFP__ && !defined __ABI_HARD__)
1013     case T_FLOAT: {
1014       VMReg src = in_regs[i].first();
1015       VMReg dst = out_regs[i + extra_args].first();
1016       if (src->is_stack()) {
1017         assert(dst->is_stack(), "must be");
1018         __ ldr(Rtemp, Address(FP, reg2offset_in(src)));
1019         __ str(Rtemp, Address(SP, reg2offset_out(dst)));
1020       } else if (dst->is_stack()) {
1021         __ fsts(src->as_FloatRegister(), Address(SP, reg2offset_out(dst)));
1022       } else {
1023         assert(src->is_FloatRegister() && dst->is_Register(), "must be");
1024         __ fmrs(dst->as_Register(), src->as_FloatRegister());
1025       }
1026       break;
1027     }
1028 
1029     case T_DOUBLE: {
1030       VMReg src_1 = in_regs[i].first();
1031       VMReg src_2 = in_regs[i].second();
1032       VMReg dst_1 = out_regs[i + extra_args].first();
1033       VMReg dst_2 = out_regs[i + extra_args].second();
1034       if (src_1->is_stack()) {
1035         assert(src_2->is_stack() && dst_1->is_stack() && dst_2->is_stack(), "must be");
1036         __ ldr(Rtemp, Address(FP, reg2offset_in(src_1)));
1037         __ ldr(LR,    Address(FP, reg2offset_in(src_2)));
1038         __ str(Rtemp, Address(SP, reg2offset_out(dst_1)));
1039         __ str(LR,    Address(SP, reg2offset_out(dst_2)));
1040       } else if (dst_1->is_stack()) {
1041         assert(dst_2->is_stack() && src_1->is_FloatRegister(), "must be");
1042         __ fstd(src_1->as_FloatRegister(), Address(SP, reg2offset_out(dst_1)));
1043 #if (ALIGN_WIDE_ARGUMENTS == 0)
1044       } else if (dst_2->is_stack()) {
1045         assert(! src_2->is_stack(), "must be"); // assuming internal java convention is aligned
1046         // double register must go into R3 + one stack slot
1047         __ fmrrd(dst_1->as_Register(), Rtemp, src_1->as_FloatRegister());
1048         __ str(Rtemp, Address(SP, reg2offset_out(dst_2)));
1049 #endif
1050       } else {
1051         assert(src_1->is_FloatRegister() && dst_1->is_Register() && dst_2->is_Register(), "must be");
1052         __ fmrrd(dst_1->as_Register(), dst_2->as_Register(), src_1->as_FloatRegister());
1053       }
1054       break;
1055     }
1056 #endif // __SOFTFP__
1057 
1058 #ifdef __ABI_HARD__
1059     case T_FLOAT: {
1060       VMReg src = in_regs[i].first();
1061       VMReg dst = out_regs[i + extra_args].first();
1062       if (src->is_stack()) {
1063         if (dst->is_stack()) {
1064           __ ldr(Rtemp, Address(FP, reg2offset_in(src)));
1065           __ str(Rtemp, Address(SP, reg2offset_out(dst)));
1066         } else {
1067           // C2 Java calling convention does not populate S14 and S15, therefore
1068           // those need to be loaded from stack here
1069           __ flds(dst->as_FloatRegister(), Address(FP, reg2offset_in(src)));
1070           fp_regs_in_arguments++;
1071         }
1072       } else {
1073         assert(src->is_FloatRegister(), "must be");
1074         fp_regs_in_arguments++;
1075       }
1076       break;
1077     }
1078     case T_DOUBLE: {
1079       VMReg src_1 = in_regs[i].first();
1080       VMReg src_2 = in_regs[i].second();
1081       VMReg dst_1 = out_regs[i + extra_args].first();
1082       VMReg dst_2 = out_regs[i + extra_args].second();
1083       if (src_1->is_stack()) {
1084         if (dst_1->is_stack()) {
1085           assert(dst_2->is_stack(), "must be");
1086           __ ldr(Rtemp, Address(FP, reg2offset_in(src_1)));
1087           __ ldr(LR,    Address(FP, reg2offset_in(src_2)));
1088           __ str(Rtemp, Address(SP, reg2offset_out(dst_1)));
1089           __ str(LR,    Address(SP, reg2offset_out(dst_2)));
1090         } else {
1091           // C2 Java calling convention does not populate S14 and S15, therefore
1092           // those need to be loaded from stack here
1093           __ fldd(dst_1->as_FloatRegister(), Address(FP, reg2offset_in(src_1)));
1094           fp_regs_in_arguments += 2;
1095         }
1096       } else {
1097         assert(src_1->is_FloatRegister() && src_2->is_FloatRegister(), "must be");
1098         fp_regs_in_arguments += 2;
1099       }
1100       break;
1101     }
1102 #endif // __ABI_HARD__
1103 
1104     default: {
1105       assert(in_sig_bt[i] != T_ADDRESS, "found T_ADDRESS in java args");
1106       VMReg src = in_regs[i].first();
1107       VMReg dst = out_regs[i + extra_args].first();
1108       if (src->is_stack()) {
1109         assert(dst->is_stack(), "must be");
1110         __ ldr(Rtemp, Address(FP, reg2offset_in(src)));
1111         __ str(Rtemp, Address(SP, reg2offset_out(dst)));
1112       } else if (dst->is_stack()) {
1113         __ str(src->as_Register(), Address(SP, reg2offset_out(dst)));
1114       } else {
1115         assert(src->is_Register() && dst->is_Register(), "must be");
1116         __ mov(dst->as_Register(), src->as_Register());
1117       }
1118     }
1119     }
1120   }
1121 
1122   // Get Klass mirror
1123   int klass_offset = -1;
1124   if (is_static) {
1125     klass_offset = oop_handle_offset * VMRegImpl::stack_slot_size;
1126     __ mov_oop(Rtemp, JNIHandles::make_local(method->method_holder()->java_mirror()));
1127     __ add(c_rarg1, SP, klass_offset);
1128     __ str(Rtemp, Address(SP, klass_offset));
1129     map->set_oop(VMRegImpl::stack2reg(oop_handle_offset));
1130   }
1131 
1132   // the PC offset given to add_gc_map must match the PC saved in set_last_Java_frame
1133   int pc_offset = __ set_last_Java_frame(SP, FP, true, Rtemp);
1134   assert(((__ pc()) - start) == __ offset(), "warning: start differs from code_begin");
1135   oop_maps->add_gc_map(pc_offset, map);
1136 
1137   // Order last_Java_pc store with the thread state transition (to _thread_in_native)
1138   __ membar(MacroAssembler::StoreStore, Rtemp);
1139 
1140   // RedefineClasses() tracing support for obsolete method entry
1141   if (log_is_enabled(Trace, redefine, class, obsolete)) {
1142     __ save_caller_save_registers();
1143     __ mov(R0, Rthread);
1144     __ mov_metadata(R1, method());
1145     __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::rc_trace_method_entry), R0, R1);
1146     __ restore_caller_save_registers();
1147   }
1148 
1149   const Register sync_handle = R5;
1150   const Register sync_obj    = R6;
1151   const Register disp_hdr    = altFP_7_11;
1152   const Register tmp         = R8;
1153 
1154   Label slow_lock, slow_lock_biased, lock_done, fast_lock;
1155   if (method->is_synchronized()) {
1156     // The first argument is a handle to sync object (a class or an instance)
1157     __ ldr(sync_obj, Address(R1));
1158     // Remember the handle for the unlocking code
1159     __ mov(sync_handle, R1);
1160 
1161     if(UseBiasedLocking) {
1162       __ biased_locking_enter(sync_obj, tmp, disp_hdr/*scratched*/, false, Rtemp, lock_done, slow_lock_biased);
1163     }
1164 
1165     const Register mark = tmp;
1166     // On MP platforms the next load could return a 'stale' value if the memory location has been modified by another thread.
1167     // That would be acceptable as either CAS or slow case path is taken in that case
1168 
1169     __ ldr(mark, Address(sync_obj, oopDesc::mark_offset_in_bytes()));
1170     __ sub(disp_hdr, FP, lock_slot_fp_offset);
1171     __ tst(mark, markWord::unlocked_value);
1172     __ b(fast_lock, ne);
1173 
1174     // Check for recursive lock
1175     // See comments in InterpreterMacroAssembler::lock_object for
1176     // explanations on the fast recursive locking check.
1177     // Check independently the low bits and the distance to SP
1178     // -1- test low 2 bits
1179     __ movs(Rtemp, AsmOperand(mark, lsl, 30));
1180     // -2- test (hdr - SP) if the low two bits are 0
1181     __ sub(Rtemp, mark, SP, eq);
1182     __ movs(Rtemp, AsmOperand(Rtemp, lsr, exact_log2(os::vm_page_size())), eq);
1183     // If still 'eq' then recursive locking OK
1184     // set to zero if recursive lock, set to non zero otherwise (see discussion in JDK-8267042)
1185     __ str(Rtemp, Address(disp_hdr, BasicLock::displaced_header_offset_in_bytes()));
1186     __ b(lock_done, eq);
1187     __ b(slow_lock);
1188 
1189     __ bind(fast_lock);
1190     __ str(mark, Address(disp_hdr, BasicLock::displaced_header_offset_in_bytes()));
1191 
1192     __ cas_for_lock_acquire(mark, disp_hdr, sync_obj, Rtemp, slow_lock);
1193 
1194     __ bind(lock_done);
1195   }
1196 
1197   // Get JNIEnv*
1198   __ add(c_rarg0, Rthread, in_bytes(JavaThread::jni_environment_offset()));
1199 
1200   // Perform thread state transition
1201   __ mov(Rtemp, _thread_in_native);
1202   __ str(Rtemp, Address(Rthread, JavaThread::thread_state_offset()));
1203 
1204   // Finally, call the native method
1205   __ call(method->native_function());
1206 
1207   // Set FPSCR/FPCR to a known state
1208   if (AlwaysRestoreFPU) {
1209     __ restore_default_fp_mode();
1210   }
1211 
1212   // Ensure a Boolean result is mapped to 0..1
1213   if (ret_type == T_BOOLEAN) {
1214     __ c2bool(R0);
1215   }
1216 
1217   // Do a safepoint check while thread is in transition state
1218   Label call_safepoint_runtime, return_to_java;
1219   __ mov(Rtemp, _thread_in_native_trans);
1220   __ str_32(Rtemp, Address(Rthread, JavaThread::thread_state_offset()));
1221 
1222   // make sure the store is observed before reading the SafepointSynchronize state and further mem refs
1223   __ membar(MacroAssembler::Membar_mask_bits(MacroAssembler::StoreLoad | MacroAssembler::StoreStore), Rtemp);
1224 
1225   __ safepoint_poll(R2, call_safepoint_runtime);
1226   __ ldr_u32(R3, Address(Rthread, JavaThread::suspend_flags_offset()));
1227   __ cmp(R3, 0);
1228   __ b(call_safepoint_runtime, ne);
1229 
1230   __ bind(return_to_java);
1231 
1232   // Perform thread state transition and reguard stack yellow pages if needed
1233   Label reguard, reguard_done;
1234   __ mov(Rtemp, _thread_in_Java);
1235   __ ldr_s32(R2, Address(Rthread, JavaThread::stack_guard_state_offset()));
1236   __ str_32(Rtemp, Address(Rthread, JavaThread::thread_state_offset()));
1237 
1238   __ cmp(R2, StackOverflow::stack_guard_yellow_reserved_disabled);
1239   __ b(reguard, eq);
1240   __ bind(reguard_done);
1241 
1242   Label slow_unlock, unlock_done;
1243   if (method->is_synchronized()) {
1244     __ ldr(sync_obj, Address(sync_handle));
1245 
1246     if(UseBiasedLocking) {
1247       __ biased_locking_exit(sync_obj, Rtemp, unlock_done);
1248       // disp_hdr may not have been saved on entry with biased locking
1249       __ sub(disp_hdr, FP, lock_slot_fp_offset);
1250     }
1251 
1252     // See C1_MacroAssembler::unlock_object() for more comments
1253     __ ldr(R2, Address(disp_hdr, BasicLock::displaced_header_offset_in_bytes()));
1254     __ cbz(R2, unlock_done);
1255 
1256     __ cas_for_lock_release(disp_hdr, R2, sync_obj, Rtemp, slow_unlock);
1257 
1258     __ bind(unlock_done);
1259   }
1260 
1261   // Set last java frame and handle block to zero
1262   __ ldr(LR, Address(Rthread, JavaThread::active_handles_offset()));
1263   __ reset_last_Java_frame(Rtemp); // sets Rtemp to 0 on 32-bit ARM
1264 
1265   __ str_32(Rtemp, Address(LR, JNIHandleBlock::top_offset_in_bytes()));
1266   if (CheckJNICalls) {
1267     __ str(__ zero_register(Rtemp), Address(Rthread, JavaThread::pending_jni_exception_check_fn_offset()));
1268   }
1269 
1270   // Unbox oop result, e.g. JNIHandles::resolve value in R0.
1271   if (ret_type == T_OBJECT || ret_type == T_ARRAY) {
1272     __ resolve_jobject(R0,      // value
1273                        Rtemp,   // tmp1
1274                        R1_tmp); // tmp2
1275   }
1276 
1277   // Any exception pending?
1278   __ ldr(Rtemp, Address(Rthread, Thread::pending_exception_offset()));
1279   __ mov(SP, FP);
1280 
1281   __ cmp(Rtemp, 0);
1282   // Pop the frame and return if no exception pending
1283   __ pop(RegisterSet(FP) | RegisterSet(PC), eq);
1284   // Pop the frame and forward the exception. Rexception_pc contains return address.
1285   __ ldr(FP, Address(SP, wordSize, post_indexed), ne);
1286   __ ldr(Rexception_pc, Address(SP, wordSize, post_indexed), ne);
1287   __ jump(StubRoutines::forward_exception_entry(), relocInfo::runtime_call_type, Rtemp);
1288 
1289   // Safepoint operation and/or pending suspend request is in progress.
1290   // Save the return values and call the runtime function by hand.
1291   __ bind(call_safepoint_runtime);
1292   push_result_registers(masm, ret_type);
1293   __ mov(R0, Rthread);
1294   __ call(CAST_FROM_FN_PTR(address, JavaThread::check_special_condition_for_native_trans));
1295   pop_result_registers(masm, ret_type);
1296   __ b(return_to_java);
1297 
1298   // Reguard stack pages. Save native results around a call to C runtime.
1299   __ bind(reguard);
1300   push_result_registers(masm, ret_type);
1301   __ call(CAST_FROM_FN_PTR(address, SharedRuntime::reguard_yellow_pages));
1302   pop_result_registers(masm, ret_type);
1303   __ b(reguard_done);
1304 
1305   if (method->is_synchronized()) {
1306     // Locking slow case
1307     if(UseBiasedLocking) {
1308       __ bind(slow_lock_biased);
1309       __ sub(disp_hdr, FP, lock_slot_fp_offset);
1310     }
1311 
1312     __ bind(slow_lock);
1313 
1314     push_param_registers(masm, fp_regs_in_arguments);
1315 
1316     // last_Java_frame is already set, so do call_VM manually; no exception can occur
1317     __ mov(R0, sync_obj);
1318     __ mov(R1, disp_hdr);
1319     __ mov(R2, Rthread);
1320     __ call(CAST_FROM_FN_PTR(address, SharedRuntime::complete_monitor_locking_C));
1321 
1322     pop_param_registers(masm, fp_regs_in_arguments);
1323 
1324     __ b(lock_done);
1325 
1326     // Unlocking slow case
1327     __ bind(slow_unlock);
1328 
1329     push_result_registers(masm, ret_type);
1330 
1331     // Clear pending exception before reentering VM.
1332     // Can store the oop in register since it is a leaf call.
1333     assert_different_registers(Rtmp_save1, sync_obj, disp_hdr);
1334     __ ldr(Rtmp_save1, Address(Rthread, Thread::pending_exception_offset()));
1335     Register zero = __ zero_register(Rtemp);
1336     __ str(zero, Address(Rthread, Thread::pending_exception_offset()));
1337     __ mov(R0, sync_obj);
1338     __ mov(R1, disp_hdr);
1339     __ mov(R2, Rthread);
1340     __ call(CAST_FROM_FN_PTR(address, SharedRuntime::complete_monitor_unlocking_C));
1341     __ str(Rtmp_save1, Address(Rthread, Thread::pending_exception_offset()));
1342 
1343     pop_result_registers(masm, ret_type);
1344 
1345     __ b(unlock_done);
1346   }
1347 
1348   __ flush();
1349   return nmethod::new_native_nmethod(method,
1350                                      compile_id,
1351                                      masm->code(),
1352                                      vep_offset,
1353                                      frame_complete,
1354                                      stack_slots / VMRegImpl::slots_per_word,
1355                                      in_ByteSize(is_static ? klass_offset : receiver_offset),
1356                                      in_ByteSize(lock_slot_offset * VMRegImpl::stack_slot_size),
1357                                      oop_maps);
1358 }
1359 
1360 // this function returns the adjust size (in number of words) to a c2i adapter
1361 // activation for use during deoptimization
last_frame_adjust(int callee_parameters,int callee_locals)1362 int Deoptimization::last_frame_adjust(int callee_parameters, int callee_locals) {
1363   int extra_locals_size = (callee_locals - callee_parameters) * Interpreter::stackElementWords;
1364   return extra_locals_size;
1365 }
1366 
1367 
1368 // Number of stack slots between incoming argument block and the start of
1369 // a new frame.  The PROLOG must add this many slots to the stack.  The
1370 // EPILOG must remove this many slots.
1371 // FP + LR
in_preserve_stack_slots()1372 uint SharedRuntime::in_preserve_stack_slots() {
1373   return 2 * VMRegImpl::slots_per_word;
1374 }
1375 
out_preserve_stack_slots()1376 uint SharedRuntime::out_preserve_stack_slots() {
1377   return 0;
1378 }
1379 
1380 //------------------------------generate_deopt_blob----------------------------
generate_deopt_blob()1381 void SharedRuntime::generate_deopt_blob() {
1382   ResourceMark rm;
1383   CodeBuffer buffer("deopt_blob", 1024, 1024);
1384   int frame_size_in_words;
1385   OopMapSet* oop_maps;
1386   int reexecute_offset;
1387   int exception_in_tls_offset;
1388   int exception_offset;
1389 
1390   MacroAssembler* masm = new MacroAssembler(&buffer);
1391   Label cont;
1392   const Register Rkind   = R9; // caller-saved
1393   const Register Rublock = R6;
1394   const Register Rsender = altFP_7_11;
1395   assert_different_registers(Rkind, Rublock, Rsender, Rexception_obj, Rexception_pc, R0, R1, R2, R3, R8, Rtemp);
1396 
1397   address start = __ pc();
1398 
1399   oop_maps = new OopMapSet();
1400   // LR saved by caller (can be live in c2 method)
1401 
1402   // A deopt is a case where LR may be live in the c2 nmethod. So it's
1403   // not possible to call the deopt blob from the nmethod and pass the
1404   // address of the deopt handler of the nmethod in LR. What happens
1405   // now is that the caller of the deopt blob pushes the current
1406   // address so the deopt blob doesn't have to do it. This way LR can
1407   // be preserved, contains the live value from the nmethod and is
1408   // saved at R14/R30_offset here.
1409   OopMap* map = RegisterSaver::save_live_registers(masm, &frame_size_in_words, true);
1410   __ mov(Rkind, Deoptimization::Unpack_deopt);
1411   __ b(cont);
1412 
1413   exception_offset = __ pc() - start;
1414 
1415   // Transfer Rexception_obj & Rexception_pc in TLS and fall thru to the
1416   // exception_in_tls_offset entry point.
1417   __ str(Rexception_obj, Address(Rthread, JavaThread::exception_oop_offset()));
1418   __ str(Rexception_pc, Address(Rthread, JavaThread::exception_pc_offset()));
1419   // Force return value to NULL to avoid confusing the escape analysis
1420   // logic. Everything is dead here anyway.
1421   __ mov(R0, 0);
1422 
1423   exception_in_tls_offset = __ pc() - start;
1424 
1425   // Exception data is in JavaThread structure
1426   // Patch the return address of the current frame
1427   __ ldr(LR, Address(Rthread, JavaThread::exception_pc_offset()));
1428   (void) RegisterSaver::save_live_registers(masm, &frame_size_in_words);
1429   {
1430     const Register Rzero = __ zero_register(Rtemp); // XXX should be OK for C2 but not 100% sure
1431     __ str(Rzero, Address(Rthread, JavaThread::exception_pc_offset()));
1432   }
1433   __ mov(Rkind, Deoptimization::Unpack_exception);
1434   __ b(cont);
1435 
1436   reexecute_offset = __ pc() - start;
1437 
1438   (void) RegisterSaver::save_live_registers(masm, &frame_size_in_words);
1439   __ mov(Rkind, Deoptimization::Unpack_reexecute);
1440 
1441   // Calculate UnrollBlock and save the result in Rublock
1442   __ bind(cont);
1443   __ mov(R0, Rthread);
1444   __ mov(R1, Rkind);
1445 
1446   int pc_offset = __ set_last_Java_frame(SP, FP, false, Rtemp); // note: FP may not need to be saved (not on x86)
1447   assert(((__ pc()) - start) == __ offset(), "warning: start differs from code_begin");
1448   __ call(CAST_FROM_FN_PTR(address, Deoptimization::fetch_unroll_info));
1449   if (pc_offset == -1) {
1450     pc_offset = __ offset();
1451   }
1452   oop_maps->add_gc_map(pc_offset, map);
1453   __ reset_last_Java_frame(Rtemp); // Rtemp free since scratched by far call
1454 
1455   __ mov(Rublock, R0);
1456 
1457   // Reload Rkind from the UnrollBlock (might have changed)
1458   __ ldr_s32(Rkind, Address(Rublock, Deoptimization::UnrollBlock::unpack_kind_offset_in_bytes()));
1459   Label noException;
1460   __ cmp_32(Rkind, Deoptimization::Unpack_exception);   // Was exception pending?
1461   __ b(noException, ne);
1462   // handle exception case
1463 #ifdef ASSERT
1464   // assert that exception_pc is zero in tls
1465   { Label L;
1466     __ ldr(Rexception_pc, Address(Rthread, JavaThread::exception_pc_offset()));
1467     __ cbz(Rexception_pc, L);
1468     __ stop("exception pc should be null");
1469     __ bind(L);
1470   }
1471 #endif
1472   __ ldr(Rexception_obj, Address(Rthread, JavaThread::exception_oop_offset()));
1473   __ verify_oop(Rexception_obj);
1474   {
1475     const Register Rzero = __ zero_register(Rtemp);
1476     __ str(Rzero, Address(Rthread, JavaThread::exception_oop_offset()));
1477   }
1478 
1479   __ bind(noException);
1480 
1481   // This frame is going away.  Fetch return value, so we can move it to
1482   // a new frame.
1483   __ ldr(R0, Address(SP, RegisterSaver::R0_offset * wordSize));
1484   __ ldr(R1, Address(SP, RegisterSaver::R1_offset * wordSize));
1485 #ifndef __SOFTFP__
1486   __ ldr_double(D0, Address(SP, RegisterSaver::D0_offset * wordSize));
1487 #endif
1488   // pop frame
1489   __ add(SP, SP, RegisterSaver::reg_save_size * wordSize);
1490 
1491   // Set initial stack state before pushing interpreter frames
1492   __ ldr_s32(Rtemp, Address(Rublock, Deoptimization::UnrollBlock::size_of_deoptimized_frame_offset_in_bytes()));
1493   __ ldr(R2, Address(Rublock, Deoptimization::UnrollBlock::frame_pcs_offset_in_bytes()));
1494   __ ldr(R3, Address(Rublock, Deoptimization::UnrollBlock::frame_sizes_offset_in_bytes()));
1495 
1496   __ add(SP, SP, Rtemp);
1497 
1498 #ifdef ASSERT
1499   // Compilers generate code that bang the stack by as much as the
1500   // interpreter would need. So this stack banging should never
1501   // trigger a fault. Verify that it does not on non product builds.
1502   // See if it is enough stack to push deoptimized frames.
1503   //
1504   // The compiled method that we are deoptimizing was popped from the stack.
1505   // If the stack bang results in a stack overflow, we don't return to the
1506   // method that is being deoptimized. The stack overflow exception is
1507   // propagated to the caller of the deoptimized method. Need to get the pc
1508   // from the caller in LR and restore FP.
1509   __ ldr(LR, Address(R2, 0));
1510   __ ldr(FP, Address(Rublock, Deoptimization::UnrollBlock::initial_info_offset_in_bytes()));
1511   __ ldr_s32(R8, Address(Rublock, Deoptimization::UnrollBlock::total_frame_sizes_offset_in_bytes()));
1512   __ arm_stack_overflow_check(R8, Rtemp);
1513 #endif
1514   __ ldr_s32(R8, Address(Rublock, Deoptimization::UnrollBlock::number_of_frames_offset_in_bytes()));
1515 
1516   // Pick up the initial fp we should save
1517   // XXX Note: was ldr(FP, Address(FP));
1518 
1519   // The compiler no longer uses FP as a frame pointer for the
1520   // compiled code. It can be used by the allocator in C2 or to
1521   // memorize the original SP for JSR292 call sites.
1522 
1523   // Hence, ldr(FP, Address(FP)) is probably not correct. For x86,
1524   // Deoptimization::fetch_unroll_info computes the right FP value and
1525   // stores it in Rublock.initial_info. This has been activated for ARM.
1526   __ ldr(FP, Address(Rublock, Deoptimization::UnrollBlock::initial_info_offset_in_bytes()));
1527 
1528   __ ldr_s32(Rtemp, Address(Rublock, Deoptimization::UnrollBlock::caller_adjustment_offset_in_bytes()));
1529   __ mov(Rsender, SP);
1530   __ sub(SP, SP, Rtemp);
1531 
1532   // Push interpreter frames in a loop
1533   Label loop;
1534   __ bind(loop);
1535   __ ldr(LR, Address(R2, wordSize, post_indexed));         // load frame pc
1536   __ ldr(Rtemp, Address(R3, wordSize, post_indexed));      // load frame size
1537 
1538   __ raw_push(FP, LR);                                     // create new frame
1539   __ mov(FP, SP);
1540   __ sub(Rtemp, Rtemp, 2*wordSize);
1541 
1542   __ sub(SP, SP, Rtemp);
1543 
1544   __ str(Rsender, Address(FP, frame::interpreter_frame_sender_sp_offset * wordSize));
1545   __ mov(LR, 0);
1546   __ str(LR, Address(FP, frame::interpreter_frame_last_sp_offset * wordSize));
1547 
1548   __ subs(R8, R8, 1);                               // decrement counter
1549   __ mov(Rsender, SP);
1550   __ b(loop, ne);
1551 
1552   // Re-push self-frame
1553   __ ldr(LR, Address(R2));
1554   __ raw_push(FP, LR);
1555   __ mov(FP, SP);
1556   __ sub(SP, SP, (frame_size_in_words - 2) * wordSize);
1557 
1558   // Restore frame locals after moving the frame
1559   __ str(R0, Address(SP, RegisterSaver::R0_offset * wordSize));
1560   __ str(R1, Address(SP, RegisterSaver::R1_offset * wordSize));
1561 
1562 #ifndef __SOFTFP__
1563   __ str_double(D0, Address(SP, RegisterSaver::D0_offset * wordSize));
1564 #endif // !__SOFTFP__
1565 
1566 #ifdef ASSERT
1567   // Reload Rkind from the UnrollBlock and check that it was not overwritten (Rkind is not callee-saved)
1568   { Label L;
1569     __ ldr_s32(Rtemp, Address(Rublock, Deoptimization::UnrollBlock::unpack_kind_offset_in_bytes()));
1570     __ cmp_32(Rkind, Rtemp);
1571     __ b(L, eq);
1572     __ stop("Rkind was overwritten");
1573     __ bind(L);
1574   }
1575 #endif
1576 
1577   // Call unpack_frames with proper arguments
1578   __ mov(R0, Rthread);
1579   __ mov(R1, Rkind);
1580 
1581   pc_offset = __ set_last_Java_frame(SP, FP, true, Rtemp);
1582   assert(((__ pc()) - start) == __ offset(), "warning: start differs from code_begin");
1583   __ call_VM_leaf(CAST_FROM_FN_PTR(address, Deoptimization::unpack_frames));
1584   if (pc_offset == -1) {
1585     pc_offset = __ offset();
1586   }
1587   oop_maps->add_gc_map(pc_offset, new OopMap(frame_size_in_words * VMRegImpl::slots_per_word, 0));
1588   __ reset_last_Java_frame(Rtemp); // Rtemp free since scratched by far call
1589 
1590   // Collect return values, pop self-frame and jump to interpreter
1591   __ ldr(R0, Address(SP, RegisterSaver::R0_offset * wordSize));
1592   __ ldr(R1, Address(SP, RegisterSaver::R1_offset * wordSize));
1593   // Interpreter floats controlled by __SOFTFP__, but compiler
1594   // float return value registers controlled by __ABI_HARD__
1595   // This matters for vfp-sflt builds.
1596 #ifndef __SOFTFP__
1597   // Interpreter hard float
1598 #ifdef __ABI_HARD__
1599   // Compiler float return value in FP registers
1600   __ ldr_double(D0, Address(SP, RegisterSaver::D0_offset * wordSize));
1601 #else
1602   // Compiler float return value in integer registers,
1603   // copy to D0 for interpreter (S0 <-- R0)
1604   __ fmdrr(D0_tos, R0, R1);
1605 #endif
1606 #endif // !__SOFTFP__
1607   __ mov(SP, FP);
1608 
1609   __ pop(RegisterSet(FP) | RegisterSet(PC));
1610 
1611   __ flush();
1612 
1613   _deopt_blob = DeoptimizationBlob::create(&buffer, oop_maps, 0, exception_offset,
1614                                            reexecute_offset, frame_size_in_words);
1615   _deopt_blob->set_unpack_with_exception_in_tls_offset(exception_in_tls_offset);
1616 }
1617 
1618 #ifdef COMPILER2
1619 
1620 //------------------------------generate_uncommon_trap_blob--------------------
1621 // Ought to generate an ideal graph & compile, but here's some ASM
1622 // instead.
generate_uncommon_trap_blob()1623 void SharedRuntime::generate_uncommon_trap_blob() {
1624   // allocate space for the code
1625   ResourceMark rm;
1626 
1627   // setup code generation tools
1628   int pad = VerifyThread ? 512 : 0;
1629 #ifdef _LP64
1630   CodeBuffer buffer("uncommon_trap_blob", 2700+pad, 512);
1631 #else
1632   // Measured 8/7/03 at 660 in 32bit debug build (no VerifyThread)
1633   // Measured 8/7/03 at 1028 in 32bit debug build (VerifyThread)
1634   CodeBuffer buffer("uncommon_trap_blob", 2000+pad, 512);
1635 #endif
1636   // bypassed when code generation useless
1637   MacroAssembler* masm               = new MacroAssembler(&buffer);
1638   const Register Rublock = R6;
1639   const Register Rsender = altFP_7_11;
1640   assert_different_registers(Rublock, Rsender, Rexception_obj, R0, R1, R2, R3, R8, Rtemp);
1641 
1642   //
1643   // This is the entry point for all traps the compiler takes when it thinks
1644   // it cannot handle further execution of compilation code. The frame is
1645   // deoptimized in these cases and converted into interpreter frames for
1646   // execution
1647   // The steps taken by this frame are as follows:
1648   //   - push a fake "unpack_frame"
1649   //   - call the C routine Deoptimization::uncommon_trap (this function
1650   //     packs the current compiled frame into vframe arrays and returns
1651   //     information about the number and size of interpreter frames which
1652   //     are equivalent to the frame which is being deoptimized)
1653   //   - deallocate the "unpack_frame"
1654   //   - deallocate the deoptimization frame
1655   //   - in a loop using the information returned in the previous step
1656   //     push interpreter frames;
1657   //   - create a dummy "unpack_frame"
1658   //   - call the C routine: Deoptimization::unpack_frames (this function
1659   //     lays out values on the interpreter frame which was just created)
1660   //   - deallocate the dummy unpack_frame
1661   //   - return to the interpreter entry point
1662   //
1663   //  Refer to the following methods for more information:
1664   //   - Deoptimization::uncommon_trap
1665   //   - Deoptimization::unpack_frame
1666 
1667   // the unloaded class index is in R0 (first parameter to this blob)
1668 
1669   __ raw_push(FP, LR);
1670   __ set_last_Java_frame(SP, FP, false, Rtemp);
1671   __ mov(R2, Deoptimization::Unpack_uncommon_trap);
1672   __ mov(R1, R0);
1673   __ mov(R0, Rthread);
1674   __ call(CAST_FROM_FN_PTR(address, Deoptimization::uncommon_trap));
1675   __ mov(Rublock, R0);
1676   __ reset_last_Java_frame(Rtemp);
1677   __ raw_pop(FP, LR);
1678 
1679 #ifdef ASSERT
1680   { Label L;
1681     __ ldr_s32(Rtemp, Address(Rublock, Deoptimization::UnrollBlock::unpack_kind_offset_in_bytes()));
1682     __ cmp_32(Rtemp, Deoptimization::Unpack_uncommon_trap);
1683     __ b(L, eq);
1684     __ stop("SharedRuntime::generate_uncommon_trap_blob: expected Unpack_uncommon_trap");
1685     __ bind(L);
1686   }
1687 #endif
1688 
1689 
1690   // Set initial stack state before pushing interpreter frames
1691   __ ldr_s32(Rtemp, Address(Rublock, Deoptimization::UnrollBlock::size_of_deoptimized_frame_offset_in_bytes()));
1692   __ ldr(R2, Address(Rublock, Deoptimization::UnrollBlock::frame_pcs_offset_in_bytes()));
1693   __ ldr(R3, Address(Rublock, Deoptimization::UnrollBlock::frame_sizes_offset_in_bytes()));
1694 
1695   __ add(SP, SP, Rtemp);
1696 
1697   // See if it is enough stack to push deoptimized frames.
1698 #ifdef ASSERT
1699   // Compilers generate code that bang the stack by as much as the
1700   // interpreter would need. So this stack banging should never
1701   // trigger a fault. Verify that it does not on non product builds.
1702   //
1703   // The compiled method that we are deoptimizing was popped from the stack.
1704   // If the stack bang results in a stack overflow, we don't return to the
1705   // method that is being deoptimized. The stack overflow exception is
1706   // propagated to the caller of the deoptimized method. Need to get the pc
1707   // from the caller in LR and restore FP.
1708   __ ldr(LR, Address(R2, 0));
1709   __ ldr(FP, Address(Rublock, Deoptimization::UnrollBlock::initial_info_offset_in_bytes()));
1710   __ ldr_s32(R8, Address(Rublock, Deoptimization::UnrollBlock::total_frame_sizes_offset_in_bytes()));
1711   __ arm_stack_overflow_check(R8, Rtemp);
1712 #endif
1713   __ ldr_s32(R8, Address(Rublock, Deoptimization::UnrollBlock::number_of_frames_offset_in_bytes()));
1714   __ ldr_s32(Rtemp, Address(Rublock, Deoptimization::UnrollBlock::caller_adjustment_offset_in_bytes()));
1715   __ mov(Rsender, SP);
1716   __ sub(SP, SP, Rtemp);
1717   //  __ ldr(FP, Address(FP));
1718   __ ldr(FP, Address(Rublock, Deoptimization::UnrollBlock::initial_info_offset_in_bytes()));
1719 
1720   // Push interpreter frames in a loop
1721   Label loop;
1722   __ bind(loop);
1723   __ ldr(LR, Address(R2, wordSize, post_indexed));         // load frame pc
1724   __ ldr(Rtemp, Address(R3, wordSize, post_indexed));      // load frame size
1725 
1726   __ raw_push(FP, LR);                                     // create new frame
1727   __ mov(FP, SP);
1728   __ sub(Rtemp, Rtemp, 2*wordSize);
1729 
1730   __ sub(SP, SP, Rtemp);
1731 
1732   __ str(Rsender, Address(FP, frame::interpreter_frame_sender_sp_offset * wordSize));
1733   __ mov(LR, 0);
1734   __ str(LR, Address(FP, frame::interpreter_frame_last_sp_offset * wordSize));
1735   __ subs(R8, R8, 1);                               // decrement counter
1736   __ mov(Rsender, SP);
1737   __ b(loop, ne);
1738 
1739   // Re-push self-frame
1740   __ ldr(LR, Address(R2));
1741   __ raw_push(FP, LR);
1742   __ mov(FP, SP);
1743 
1744   // Call unpack_frames with proper arguments
1745   __ mov(R0, Rthread);
1746   __ mov(R1, Deoptimization::Unpack_uncommon_trap);
1747   __ set_last_Java_frame(SP, FP, true, Rtemp);
1748   __ call_VM_leaf(CAST_FROM_FN_PTR(address, Deoptimization::unpack_frames));
1749   //  oop_maps->add_gc_map(__ pc() - start, new OopMap(frame_size_in_words, 0));
1750   __ reset_last_Java_frame(Rtemp);
1751 
1752   __ mov(SP, FP);
1753   __ pop(RegisterSet(FP) | RegisterSet(PC));
1754 
1755   masm->flush();
1756   _uncommon_trap_blob = UncommonTrapBlob::create(&buffer, NULL, 2 /* LR+FP */);
1757 }
1758 
1759 #endif // COMPILER2
1760 
1761 //------------------------------generate_handler_blob------
1762 //
1763 // Generate a special Compile2Runtime blob that saves all registers,
1764 // setup oopmap, and calls safepoint code to stop the compiled code for
1765 // a safepoint.
1766 //
generate_handler_blob(address call_ptr,int poll_type)1767 SafepointBlob* SharedRuntime::generate_handler_blob(address call_ptr, int poll_type) {
1768   assert(StubRoutines::forward_exception_entry() != NULL, "must be generated before");
1769 
1770   ResourceMark rm;
1771   CodeBuffer buffer("handler_blob", 256, 256);
1772   int frame_size_words;
1773   OopMapSet* oop_maps;
1774 
1775   bool cause_return = (poll_type == POLL_AT_RETURN);
1776 
1777   MacroAssembler* masm = new MacroAssembler(&buffer);
1778   address start = __ pc();
1779   oop_maps = new OopMapSet();
1780 
1781   if (!cause_return) {
1782     __ sub(SP, SP, 4); // make room for LR which may still be live
1783                        // here if we are coming from a c2 method
1784   }
1785 
1786   OopMap* map = RegisterSaver::save_live_registers(masm, &frame_size_words, !cause_return);
1787   if (!cause_return) {
1788     // update saved PC with correct value
1789     // need 2 steps because LR can be live in c2 method
1790     __ ldr(LR, Address(Rthread, JavaThread::saved_exception_pc_offset()));
1791     __ str(LR, Address(SP, RegisterSaver::LR_offset * wordSize));
1792   }
1793 
1794   __ mov(R0, Rthread);
1795   int pc_offset = __ set_last_Java_frame(SP, FP, false, Rtemp); // note: FP may not need to be saved (not on x86)
1796   assert(((__ pc()) - start) == __ offset(), "warning: start differs from code_begin");
1797   __ call(call_ptr);
1798   if (pc_offset == -1) {
1799     pc_offset = __ offset();
1800   }
1801   oop_maps->add_gc_map(pc_offset, map);
1802   __ reset_last_Java_frame(Rtemp); // Rtemp free since scratched by far call
1803 
1804   if (!cause_return) {
1805     // If our stashed return pc was modified by the runtime we avoid touching it
1806     __ ldr(R3_tmp, Address(Rthread, JavaThread::saved_exception_pc_offset()));
1807     __ ldr(R2_tmp, Address(SP, RegisterSaver::LR_offset * wordSize));
1808     __ cmp(R2_tmp, R3_tmp);
1809     // Adjust return pc forward to step over the safepoint poll instruction
1810     __ add(R2_tmp, R2_tmp, 4, eq);
1811     __ str(R2_tmp, Address(SP, RegisterSaver::LR_offset * wordSize), eq);
1812 
1813     // Check for pending exception
1814     __ ldr(Rtemp, Address(Rthread, Thread::pending_exception_offset()));
1815     __ cmp(Rtemp, 0);
1816 
1817     RegisterSaver::restore_live_registers(masm, false);
1818     __ pop(PC, eq);
1819     __ pop(Rexception_pc);
1820   } else {
1821     // Check for pending exception
1822     __ ldr(Rtemp, Address(Rthread, Thread::pending_exception_offset()));
1823     __ cmp(Rtemp, 0);
1824 
1825     RegisterSaver::restore_live_registers(masm);
1826     __ bx(LR, eq);
1827     __ mov(Rexception_pc, LR);
1828   }
1829 
1830   __ jump(StubRoutines::forward_exception_entry(), relocInfo::runtime_call_type, Rtemp);
1831 
1832   __ flush();
1833 
1834   return SafepointBlob::create(&buffer, oop_maps, frame_size_words);
1835 }
1836 
generate_resolve_blob(address destination,const char * name)1837 RuntimeStub* SharedRuntime::generate_resolve_blob(address destination, const char* name) {
1838   assert(StubRoutines::forward_exception_entry() != NULL, "must be generated before");
1839 
1840   ResourceMark rm;
1841   CodeBuffer buffer(name, 1000, 512);
1842   int frame_size_words;
1843   OopMapSet *oop_maps;
1844   int frame_complete;
1845 
1846   MacroAssembler* masm = new MacroAssembler(&buffer);
1847   Label pending_exception;
1848 
1849   int start = __ offset();
1850 
1851   oop_maps = new OopMapSet();
1852   OopMap* map = RegisterSaver::save_live_registers(masm, &frame_size_words);
1853 
1854   frame_complete = __ offset();
1855 
1856   __ mov(R0, Rthread);
1857 
1858   int pc_offset = __ set_last_Java_frame(SP, FP, false, Rtemp);
1859   assert(start == 0, "warning: start differs from code_begin");
1860   __ call(destination);
1861   if (pc_offset == -1) {
1862     pc_offset = __ offset();
1863   }
1864   oop_maps->add_gc_map(pc_offset, map);
1865   __ reset_last_Java_frame(Rtemp); // Rtemp free since scratched by far call
1866 
1867   __ ldr(R1, Address(Rthread, Thread::pending_exception_offset()));
1868   __ cbnz(R1, pending_exception);
1869 
1870   // Overwrite saved register values
1871 
1872   // Place metadata result of VM call into Rmethod
1873   __ get_vm_result_2(R1, Rtemp);
1874   __ str(R1, Address(SP, RegisterSaver::Rmethod_offset * wordSize));
1875 
1876   // Place target address (VM call result) into Rtemp
1877   __ str(R0, Address(SP, RegisterSaver::Rtemp_offset * wordSize));
1878 
1879   RegisterSaver::restore_live_registers(masm);
1880   __ jump(Rtemp);
1881 
1882   __ bind(pending_exception);
1883 
1884   RegisterSaver::restore_live_registers(masm);
1885   const Register Rzero = __ zero_register(Rtemp);
1886   __ str(Rzero, Address(Rthread, JavaThread::vm_result_2_offset()));
1887   __ mov(Rexception_pc, LR);
1888   __ jump(StubRoutines::forward_exception_entry(), relocInfo::runtime_call_type, Rtemp);
1889 
1890   __ flush();
1891 
1892   return RuntimeStub::new_runtime_stub(name, &buffer, frame_complete, frame_size_words, oop_maps, true);
1893 }
1894 
1895 #ifdef COMPILER2
make_native_invoker(address call_target,int shadow_space_bytes,const GrowableArray<VMReg> & input_registers,const GrowableArray<VMReg> & output_registers)1896 RuntimeStub* SharedRuntime::make_native_invoker(address call_target,
1897                                                 int shadow_space_bytes,
1898                                                 const GrowableArray<VMReg>& input_registers,
1899                                                 const GrowableArray<VMReg>& output_registers) {
1900   Unimplemented();
1901   return nullptr;
1902 }
1903 #endif
1904