1 /*
2  * Copyright (c) 2008, 2018, 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.hpp"
27 #include "assembler_arm.inline.hpp"
28 #include "code/debugInfoRec.hpp"
29 #include "code/icBuffer.hpp"
30 #include "code/vtableStubs.hpp"
31 #include "interpreter/interpreter.hpp"
32 #include "logging/log.hpp"
33 #include "memory/resourceArea.hpp"
34 #include "oops/compiledICHolder.hpp"
35 #include "runtime/sharedRuntime.hpp"
36 #include "runtime/vframeArray.hpp"
37 #include "utilities/align.hpp"
38 #include "vmreg_arm.inline.hpp"
39 #ifdef COMPILER1
40 #include "c1/c1_Runtime1.hpp"
41 #include "runtime/safepoint.hpp"
42 #endif
43 #ifdef COMPILER2
44 #include "opto/runtime.hpp"
45 #endif
46 
47 #define __ masm->
48 
49 class RegisterSaver {
50 public:
51 
52   // Special registers:
53   //              32-bit ARM     64-bit ARM
54   //  Rthread:       R10            R28
55   //  LR:            R14            R30
56 
57   // Rthread is callee saved in the C ABI and never changed by compiled code:
58   // no need to save it.
59 
60   // 2 slots for LR: the one at LR_offset and an other one at R14/R30_offset.
61   // The one at LR_offset is a return address that is needed by stack walking.
62   // A c2 method uses LR as a standard register so it may be live when we
63   // branch to the runtime. The slot at R14/R30_offset is for the value of LR
64   // in case it's live in the method we are coming from.
65 
66 
67   enum RegisterLayout {
68     fpu_save_size = FloatRegisterImpl::number_of_registers,
69 #ifndef __SOFTFP__
70     D0_offset = 0,
71 #endif
72     R0_offset = fpu_save_size,
73     R1_offset,
74     R2_offset,
75     R3_offset,
76     R4_offset,
77     R5_offset,
78     R6_offset,
79 #if (FP_REG_NUM != 7)
80     // if not saved as FP
81     R7_offset,
82 #endif
83     R8_offset,
84     R9_offset,
85 #if (FP_REG_NUM != 11)
86     // if not saved as FP
87     R11_offset,
88 #endif
89     R12_offset,
90     R14_offset,
91     FP_offset,
92     LR_offset,
93     reg_save_size,
94 
95     Rmethod_offset = R9_offset,
96     Rtemp_offset = R12_offset,
97   };
98 
99   // all regs but Rthread (R10), FP (R7 or R11), SP and PC
100   // (altFP_7_11 is the one amoung R7 and R11 which is not FP)
101 #define SAVED_BASE_REGS (RegisterSet(R0, R6) | RegisterSet(R8, R9) | RegisterSet(R12) | R14 | altFP_7_11)
102 
103 
104   //  When LR may be live in the nmethod from which we are comming
105   //  then lr_saved is true, the return address is saved before the
106   //  call to save_live_register by the caller and LR contains the
107   //  live value.
108 
109   static OopMap* save_live_registers(MacroAssembler* masm,
110                                      int* total_frame_words,
111                                      bool lr_saved = false);
112   static void restore_live_registers(MacroAssembler* masm, bool restore_lr = true);
113 
114 };
115 
116 
117 
118 
save_live_registers(MacroAssembler * masm,int * total_frame_words,bool lr_saved)119 OopMap* RegisterSaver::save_live_registers(MacroAssembler* masm,
120                                            int* total_frame_words,
121                                            bool lr_saved) {
122   *total_frame_words = reg_save_size;
123 
124   OopMapSet *oop_maps = new OopMapSet();
125   OopMap* map = new OopMap(VMRegImpl::slots_per_word * (*total_frame_words), 0);
126 
127   if (lr_saved) {
128     __ push(RegisterSet(FP));
129   } else {
130     __ push(RegisterSet(FP) | RegisterSet(LR));
131   }
132   __ push(SAVED_BASE_REGS);
133   if (HaveVFP) {
134     if (VM_Version::has_vfp3_32()) {
135       __ fpush(FloatRegisterSet(D16, 16));
136     } else {
137       if (FloatRegisterImpl::number_of_registers > 32) {
138         assert(FloatRegisterImpl::number_of_registers == 64, "nb fp registers should be 64");
139         __ sub(SP, SP, 32 * wordSize);
140       }
141     }
142     __ fpush(FloatRegisterSet(D0, 16));
143   } else {
144     __ sub(SP, SP, fpu_save_size * wordSize);
145   }
146 
147   int i;
148   int j=0;
149   for (i = R0_offset; i <= R9_offset; i++) {
150     if (j == FP_REG_NUM) {
151       // skip the FP register, managed below.
152       j++;
153     }
154     map->set_callee_saved(VMRegImpl::stack2reg(i), as_Register(j)->as_VMReg());
155     j++;
156   }
157   assert(j == R10->encoding(), "must be");
158 #if (FP_REG_NUM != 11)
159   // add R11, if not managed as FP
160   map->set_callee_saved(VMRegImpl::stack2reg(R11_offset), R11->as_VMReg());
161 #endif
162   map->set_callee_saved(VMRegImpl::stack2reg(R12_offset), R12->as_VMReg());
163   map->set_callee_saved(VMRegImpl::stack2reg(R14_offset), R14->as_VMReg());
164   if (HaveVFP) {
165     for (i = 0; i < (VM_Version::has_vfp3_32() ? 64 : 32); i+=2) {
166       map->set_callee_saved(VMRegImpl::stack2reg(i), as_FloatRegister(i)->as_VMReg());
167       map->set_callee_saved(VMRegImpl::stack2reg(i + 1), as_FloatRegister(i)->as_VMReg()->next());
168     }
169   }
170 
171   return map;
172 }
173 
restore_live_registers(MacroAssembler * masm,bool restore_lr)174 void RegisterSaver::restore_live_registers(MacroAssembler* masm, bool restore_lr) {
175   if (HaveVFP) {
176     __ fpop(FloatRegisterSet(D0, 16));
177     if (VM_Version::has_vfp3_32()) {
178       __ fpop(FloatRegisterSet(D16, 16));
179     } else {
180       if (FloatRegisterImpl::number_of_registers > 32) {
181         assert(FloatRegisterImpl::number_of_registers == 64, "nb fp registers should be 64");
182         __ add(SP, SP, 32 * wordSize);
183       }
184     }
185   } else {
186     __ add(SP, SP, fpu_save_size * wordSize);
187   }
188   __ pop(SAVED_BASE_REGS);
189   if (restore_lr) {
190     __ pop(RegisterSet(FP) | RegisterSet(LR));
191   } else {
192     __ pop(RegisterSet(FP));
193   }
194 }
195 
196 
push_result_registers(MacroAssembler * masm,BasicType ret_type)197 static void push_result_registers(MacroAssembler* masm, BasicType ret_type) {
198 #ifdef __ABI_HARD__
199   if (ret_type == T_DOUBLE || ret_type == T_FLOAT) {
200     __ sub(SP, SP, 8);
201     __ fstd(D0, Address(SP));
202     return;
203   }
204 #endif // __ABI_HARD__
205   __ raw_push(R0, R1);
206 }
207 
pop_result_registers(MacroAssembler * masm,BasicType ret_type)208 static void pop_result_registers(MacroAssembler* masm, BasicType ret_type) {
209 #ifdef __ABI_HARD__
210   if (ret_type == T_DOUBLE || ret_type == T_FLOAT) {
211     __ fldd(D0, Address(SP));
212     __ add(SP, SP, 8);
213     return;
214   }
215 #endif // __ABI_HARD__
216   __ raw_pop(R0, R1);
217 }
218 
push_param_registers(MacroAssembler * masm,int fp_regs_in_arguments)219 static void push_param_registers(MacroAssembler* masm, int fp_regs_in_arguments) {
220   // R1-R3 arguments need to be saved, but we push 4 registers for 8-byte alignment
221   __ push(RegisterSet(R0, R3));
222 
223   // preserve arguments
224   // Likely not needed as the locking code won't probably modify volatile FP registers,
225   // but there is no way to guarantee that
226   if (fp_regs_in_arguments) {
227     // convert fp_regs_in_arguments to a number of double registers
228     int double_regs_num = (fp_regs_in_arguments + 1) >> 1;
229     __ fpush_hardfp(FloatRegisterSet(D0, double_regs_num));
230   }
231 }
232 
pop_param_registers(MacroAssembler * masm,int fp_regs_in_arguments)233 static void pop_param_registers(MacroAssembler* masm, int fp_regs_in_arguments) {
234   if (fp_regs_in_arguments) {
235     int double_regs_num = (fp_regs_in_arguments + 1) >> 1;
236     __ fpop_hardfp(FloatRegisterSet(D0, double_regs_num));
237   }
238   __ pop(RegisterSet(R0, R3));
239 }
240 
241 
242 
243 // Is vector's size (in bytes) bigger than a size saved by default?
244 // All vector registers are saved by default on ARM.
is_wide_vector(int size)245 bool SharedRuntime::is_wide_vector(int size) {
246   return false;
247 }
248 
trampoline_size()249 size_t SharedRuntime::trampoline_size() {
250   return 16;
251 }
252 
generate_trampoline(MacroAssembler * masm,address destination)253 void SharedRuntime::generate_trampoline(MacroAssembler *masm, address destination) {
254   InlinedAddress dest(destination);
255   __ indirect_jump(dest, Rtemp);
256   __ bind_literal(dest);
257 }
258 
c_calling_convention(const BasicType * sig_bt,VMRegPair * regs,VMRegPair * regs2,int total_args_passed)259 int SharedRuntime::c_calling_convention(const BasicType *sig_bt,
260                                         VMRegPair *regs,
261                                         VMRegPair *regs2,
262                                         int total_args_passed) {
263   assert(regs2 == NULL, "not needed on arm");
264 
265   int slot = 0;
266   int ireg = 0;
267 #ifdef __ABI_HARD__
268   int fp_slot = 0;
269   int single_fpr_slot = 0;
270 #endif // __ABI_HARD__
271   for (int i = 0; i < total_args_passed; i++) {
272     switch (sig_bt[i]) {
273     case T_SHORT:
274     case T_CHAR:
275     case T_BYTE:
276     case T_BOOLEAN:
277     case T_INT:
278     case T_ARRAY:
279     case T_OBJECT:
280     case T_ADDRESS:
281     case T_METADATA:
282 #ifndef __ABI_HARD__
283     case T_FLOAT:
284 #endif // !__ABI_HARD__
285       if (ireg < 4) {
286         Register r = as_Register(ireg);
287         regs[i].set1(r->as_VMReg());
288         ireg++;
289       } else {
290         regs[i].set1(VMRegImpl::stack2reg(slot));
291         slot++;
292       }
293       break;
294     case T_LONG:
295 #ifndef __ABI_HARD__
296     case T_DOUBLE:
297 #endif // !__ABI_HARD__
298       assert((i + 1) < total_args_passed && sig_bt[i+1] == T_VOID, "missing Half" );
299       if (ireg <= 2) {
300 #if (ALIGN_WIDE_ARGUMENTS == 1)
301         if(ireg & 1) ireg++;  // Aligned location required
302 #endif
303         Register r1 = as_Register(ireg);
304         Register r2 = as_Register(ireg + 1);
305         regs[i].set_pair(r2->as_VMReg(), r1->as_VMReg());
306         ireg += 2;
307 #if (ALIGN_WIDE_ARGUMENTS == 0)
308       } else if (ireg == 3) {
309         // uses R3 + one stack slot
310         Register r = as_Register(ireg);
311         regs[i].set_pair(VMRegImpl::stack2reg(slot), r->as_VMReg());
312         ireg += 1;
313         slot += 1;
314 #endif
315       } else {
316         if (slot & 1) slot++; // Aligned location required
317         regs[i].set_pair(VMRegImpl::stack2reg(slot+1), VMRegImpl::stack2reg(slot));
318         slot += 2;
319         ireg = 4;
320       }
321       break;
322     case T_VOID:
323       regs[i].set_bad();
324       break;
325 #ifdef __ABI_HARD__
326     case T_FLOAT:
327       if ((fp_slot < 16)||(single_fpr_slot & 1)) {
328         if ((single_fpr_slot & 1) == 0) {
329           single_fpr_slot = fp_slot;
330           fp_slot += 2;
331         }
332         FloatRegister r = as_FloatRegister(single_fpr_slot);
333         single_fpr_slot++;
334         regs[i].set1(r->as_VMReg());
335       } else {
336         regs[i].set1(VMRegImpl::stack2reg(slot));
337         slot++;
338       }
339       break;
340     case T_DOUBLE:
341       assert(ALIGN_WIDE_ARGUMENTS == 1, "ABI_HARD not supported with unaligned wide arguments");
342       if (fp_slot <= 14) {
343         FloatRegister r1 = as_FloatRegister(fp_slot);
344         FloatRegister r2 = as_FloatRegister(fp_slot+1);
345         regs[i].set_pair(r2->as_VMReg(), r1->as_VMReg());
346         fp_slot += 2;
347       } else {
348         if(slot & 1) slot++;
349         regs[i].set_pair(VMRegImpl::stack2reg(slot+1), VMRegImpl::stack2reg(slot));
350         slot += 2;
351         single_fpr_slot = 16;
352       }
353       break;
354 #endif // __ABI_HARD__
355     default:
356       ShouldNotReachHere();
357     }
358   }
359   return slot;
360 }
361 
java_calling_convention(const BasicType * sig_bt,VMRegPair * regs,int total_args_passed,int is_outgoing)362 int SharedRuntime::java_calling_convention(const BasicType *sig_bt,
363                                            VMRegPair *regs,
364                                            int total_args_passed,
365                                            int is_outgoing) {
366 #ifdef __SOFTFP__
367   // soft float is the same as the C calling convention.
368   return c_calling_convention(sig_bt, regs, NULL, total_args_passed);
369 #endif // __SOFTFP__
370   (void) is_outgoing;
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", 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(markOopDesc::unlocked_value == 1, "adjust this code");
863     __ tbz(Rtemp, exact_log2(markOopDesc::unlocked_value), slow_case);
864 
865     if (UseBiasedLocking) {
866       assert(is_power_of_2(markOopDesc::biased_lock_bit_in_place), "adjust this code");
867       __ tbnz(Rtemp, exact_log2(markOopDesc::biased_lock_bit_in_place), slow_case);
868     }
869 
870     __ bics(Rtemp, Rtemp, ~markOopDesc::hash_mask_in_place);
871     __ mov(R0, AsmOperand(Rtemp, lsr, markOopDesc::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     __ resolve(IS_NOT_NULL, sync_obj);
1162 
1163     if(UseBiasedLocking) {
1164       __ biased_locking_enter(sync_obj, tmp, disp_hdr/*scratched*/, false, Rtemp, lock_done, slow_lock_biased);
1165     }
1166 
1167     const Register mark = tmp;
1168     // On MP platforms the next load could return a 'stale' value if the memory location has been modified by another thread.
1169     // That would be acceptable as either CAS or slow case path is taken in that case
1170 
1171     __ ldr(mark, Address(sync_obj, oopDesc::mark_offset_in_bytes()));
1172     __ sub(disp_hdr, FP, lock_slot_fp_offset);
1173     __ tst(mark, markOopDesc::unlocked_value);
1174     __ b(fast_lock, ne);
1175 
1176     // Check for recursive lock
1177     // See comments in InterpreterMacroAssembler::lock_object for
1178     // explanations on the fast recursive locking check.
1179     // Check independently the low bits and the distance to SP
1180     // -1- test low 2 bits
1181     __ movs(Rtemp, AsmOperand(mark, lsl, 30));
1182     // -2- test (hdr - SP) if the low two bits are 0
1183     __ sub(Rtemp, mark, SP, eq);
1184     __ movs(Rtemp, AsmOperand(Rtemp, lsr, exact_log2(os::vm_page_size())), eq);
1185     // If still 'eq' then recursive locking OK: set displaced header to 0
1186     __ str(Rtemp, Address(disp_hdr, BasicLock::displaced_header_offset_in_bytes()), eq);
1187     __ b(lock_done, eq);
1188     __ b(slow_lock);
1189 
1190     __ bind(fast_lock);
1191     __ str(mark, Address(disp_hdr, BasicLock::displaced_header_offset_in_bytes()));
1192 
1193     __ cas_for_lock_acquire(mark, disp_hdr, sync_obj, Rtemp, slow_lock);
1194 
1195     __ bind(lock_done);
1196   }
1197 
1198   // Get JNIEnv*
1199   __ add(c_rarg0, Rthread, in_bytes(JavaThread::jni_environment_offset()));
1200 
1201   // Perform thread state transition
1202   __ mov(Rtemp, _thread_in_native);
1203   __ str(Rtemp, Address(Rthread, JavaThread::thread_state_offset()));
1204 
1205   // Finally, call the native method
1206   __ call(method->native_function());
1207 
1208   // Set FPSCR/FPCR to a known state
1209   if (AlwaysRestoreFPU) {
1210     __ restore_default_fp_mode();
1211   }
1212 
1213   // Ensure a Boolean result is mapped to 0..1
1214   if (ret_type == T_BOOLEAN) {
1215     __ c2bool(R0);
1216   }
1217 
1218   // Do a safepoint check while thread is in transition state
1219   InlinedAddress safepoint_state(SafepointSynchronize::address_of_state());
1220   Label call_safepoint_runtime, return_to_java;
1221   __ mov(Rtemp, _thread_in_native_trans);
1222   __ ldr_literal(R2, safepoint_state);
1223   __ str_32(Rtemp, Address(Rthread, JavaThread::thread_state_offset()));
1224 
1225   // make sure the store is observed before reading the SafepointSynchronize state and further mem refs
1226   __ membar(MacroAssembler::Membar_mask_bits(MacroAssembler::StoreLoad | MacroAssembler::StoreStore), Rtemp);
1227 
1228   __ ldr_s32(R2, Address(R2));
1229   __ ldr_u32(R3, Address(Rthread, JavaThread::suspend_flags_offset()));
1230   __ cmp(R2, SafepointSynchronize::_not_synchronized);
1231   __ cond_cmp(R3, 0, eq);
1232   __ b(call_safepoint_runtime, ne);
1233   __ bind(return_to_java);
1234 
1235   // Perform thread state transition and reguard stack yellow pages if needed
1236   Label reguard, reguard_done;
1237   __ mov(Rtemp, _thread_in_Java);
1238   __ ldr_s32(R2, Address(Rthread, JavaThread::stack_guard_state_offset()));
1239   __ str_32(Rtemp, Address(Rthread, JavaThread::thread_state_offset()));
1240 
1241   __ cmp(R2, JavaThread::stack_guard_yellow_reserved_disabled);
1242   __ b(reguard, eq);
1243   __ bind(reguard_done);
1244 
1245   Label slow_unlock, unlock_done;
1246   if (method->is_synchronized()) {
1247     __ ldr(sync_obj, Address(sync_handle));
1248 
1249     __ resolve(IS_NOT_NULL, sync_obj);
1250 
1251     if(UseBiasedLocking) {
1252       __ biased_locking_exit(sync_obj, Rtemp, unlock_done);
1253       // disp_hdr may not have been saved on entry with biased locking
1254       __ sub(disp_hdr, FP, lock_slot_fp_offset);
1255     }
1256 
1257     // See C1_MacroAssembler::unlock_object() for more comments
1258     __ ldr(R2, Address(disp_hdr, BasicLock::displaced_header_offset_in_bytes()));
1259     __ cbz(R2, unlock_done);
1260 
1261     __ cas_for_lock_release(disp_hdr, R2, sync_obj, Rtemp, slow_unlock);
1262 
1263     __ bind(unlock_done);
1264   }
1265 
1266   // Set last java frame and handle block to zero
1267   __ ldr(LR, Address(Rthread, JavaThread::active_handles_offset()));
1268   __ reset_last_Java_frame(Rtemp); // sets Rtemp to 0 on 32-bit ARM
1269 
1270   __ str_32(Rtemp, Address(LR, JNIHandleBlock::top_offset_in_bytes()));
1271   if (CheckJNICalls) {
1272     __ str(__ zero_register(Rtemp), Address(Rthread, JavaThread::pending_jni_exception_check_fn_offset()));
1273   }
1274 
1275   // Unbox oop result, e.g. JNIHandles::resolve value in R0.
1276   if (ret_type == T_OBJECT || ret_type == T_ARRAY) {
1277     __ resolve_jobject(R0,      // value
1278                        Rtemp,   // tmp1
1279                        R1_tmp); // tmp2
1280   }
1281 
1282   // Any exception pending?
1283   __ ldr(Rtemp, Address(Rthread, Thread::pending_exception_offset()));
1284   __ mov(SP, FP);
1285 
1286   __ cmp(Rtemp, 0);
1287   // Pop the frame and return if no exception pending
1288   __ pop(RegisterSet(FP) | RegisterSet(PC), eq);
1289   // Pop the frame and forward the exception. Rexception_pc contains return address.
1290   __ ldr(FP, Address(SP, wordSize, post_indexed), ne);
1291   __ ldr(Rexception_pc, Address(SP, wordSize, post_indexed), ne);
1292   __ jump(StubRoutines::forward_exception_entry(), relocInfo::runtime_call_type, Rtemp);
1293 
1294   // Safepoint operation and/or pending suspend request is in progress.
1295   // Save the return values and call the runtime function by hand.
1296   __ bind(call_safepoint_runtime);
1297   push_result_registers(masm, ret_type);
1298   __ mov(R0, Rthread);
1299   __ call(CAST_FROM_FN_PTR(address, JavaThread::check_special_condition_for_native_trans));
1300   pop_result_registers(masm, ret_type);
1301   __ b(return_to_java);
1302 
1303   __ bind_literal(safepoint_state);
1304 
1305   // Reguard stack pages. Save native results around a call to C runtime.
1306   __ bind(reguard);
1307   push_result_registers(masm, ret_type);
1308   __ call(CAST_FROM_FN_PTR(address, SharedRuntime::reguard_yellow_pages));
1309   pop_result_registers(masm, ret_type);
1310   __ b(reguard_done);
1311 
1312   if (method->is_synchronized()) {
1313     // Locking slow case
1314     if(UseBiasedLocking) {
1315       __ bind(slow_lock_biased);
1316       __ sub(disp_hdr, FP, lock_slot_fp_offset);
1317     }
1318 
1319     __ bind(slow_lock);
1320 
1321     push_param_registers(masm, fp_regs_in_arguments);
1322 
1323     // last_Java_frame is already set, so do call_VM manually; no exception can occur
1324     __ mov(R0, sync_obj);
1325     __ mov(R1, disp_hdr);
1326     __ mov(R2, Rthread);
1327     __ call(CAST_FROM_FN_PTR(address, SharedRuntime::complete_monitor_locking_C));
1328 
1329     pop_param_registers(masm, fp_regs_in_arguments);
1330 
1331     __ b(lock_done);
1332 
1333     // Unlocking slow case
1334     __ bind(slow_unlock);
1335 
1336     push_result_registers(masm, ret_type);
1337 
1338     // Clear pending exception before reentering VM.
1339     // Can store the oop in register since it is a leaf call.
1340     assert_different_registers(Rtmp_save1, sync_obj, disp_hdr);
1341     __ ldr(Rtmp_save1, Address(Rthread, Thread::pending_exception_offset()));
1342     Register zero = __ zero_register(Rtemp);
1343     __ str(zero, Address(Rthread, Thread::pending_exception_offset()));
1344     __ mov(R0, sync_obj);
1345     __ mov(R1, disp_hdr);
1346     __ mov(R2, Rthread);
1347     __ call(CAST_FROM_FN_PTR(address, SharedRuntime::complete_monitor_unlocking_C));
1348     __ str(Rtmp_save1, Address(Rthread, Thread::pending_exception_offset()));
1349 
1350     pop_result_registers(masm, ret_type);
1351 
1352     __ b(unlock_done);
1353   }
1354 
1355   __ flush();
1356   return nmethod::new_native_nmethod(method,
1357                                      compile_id,
1358                                      masm->code(),
1359                                      vep_offset,
1360                                      frame_complete,
1361                                      stack_slots / VMRegImpl::slots_per_word,
1362                                      in_ByteSize(is_static ? klass_offset : receiver_offset),
1363                                      in_ByteSize(lock_slot_offset * VMRegImpl::stack_slot_size),
1364                                      oop_maps);
1365 }
1366 
1367 // this function returns the adjust size (in number of words) to a c2i adapter
1368 // activation for use during deoptimization
last_frame_adjust(int callee_parameters,int callee_locals)1369 int Deoptimization::last_frame_adjust(int callee_parameters, int callee_locals) {
1370   int extra_locals_size = (callee_locals - callee_parameters) * Interpreter::stackElementWords;
1371   return extra_locals_size;
1372 }
1373 
1374 
out_preserve_stack_slots()1375 uint SharedRuntime::out_preserve_stack_slots() {
1376   return 0;
1377 }
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   if (UseStackBanging) {
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   }
1514 #endif
1515   __ ldr_s32(R8, Address(Rublock, Deoptimization::UnrollBlock::number_of_frames_offset_in_bytes()));
1516 
1517   // Pick up the initial fp we should save
1518   // XXX Note: was ldr(FP, Address(FP));
1519 
1520   // The compiler no longer uses FP as a frame pointer for the
1521   // compiled code. It can be used by the allocator in C2 or to
1522   // memorize the original SP for JSR292 call sites.
1523 
1524   // Hence, ldr(FP, Address(FP)) is probably not correct. For x86,
1525   // Deoptimization::fetch_unroll_info computes the right FP value and
1526   // stores it in Rublock.initial_info. This has been activated for ARM.
1527   __ ldr(FP, Address(Rublock, Deoptimization::UnrollBlock::initial_info_offset_in_bytes()));
1528 
1529   __ ldr_s32(Rtemp, Address(Rublock, Deoptimization::UnrollBlock::caller_adjustment_offset_in_bytes()));
1530   __ mov(Rsender, SP);
1531   __ sub(SP, SP, Rtemp);
1532 
1533   // Push interpreter frames in a loop
1534   Label loop;
1535   __ bind(loop);
1536   __ ldr(LR, Address(R2, wordSize, post_indexed));         // load frame pc
1537   __ ldr(Rtemp, Address(R3, wordSize, post_indexed));      // load frame size
1538 
1539   __ raw_push(FP, LR);                                     // create new frame
1540   __ mov(FP, SP);
1541   __ sub(Rtemp, Rtemp, 2*wordSize);
1542 
1543   __ sub(SP, SP, Rtemp);
1544 
1545   __ str(Rsender, Address(FP, frame::interpreter_frame_sender_sp_offset * wordSize));
1546   __ mov(LR, 0);
1547   __ str(LR, Address(FP, frame::interpreter_frame_last_sp_offset * wordSize));
1548 
1549   __ subs(R8, R8, 1);                               // decrement counter
1550   __ mov(Rsender, SP);
1551   __ b(loop, ne);
1552 
1553   // Re-push self-frame
1554   __ ldr(LR, Address(R2));
1555   __ raw_push(FP, LR);
1556   __ mov(FP, SP);
1557   __ sub(SP, SP, (frame_size_in_words - 2) * wordSize);
1558 
1559   // Restore frame locals after moving the frame
1560   __ str(R0, Address(SP, RegisterSaver::R0_offset * wordSize));
1561   __ str(R1, Address(SP, RegisterSaver::R1_offset * wordSize));
1562 
1563 #ifndef __SOFTFP__
1564   __ str_double(D0, Address(SP, RegisterSaver::D0_offset * wordSize));
1565 #endif // !__SOFTFP__
1566 
1567 #ifdef ASSERT
1568   // Reload Rkind from the UnrollBlock and check that it was not overwritten (Rkind is not callee-saved)
1569   { Label L;
1570     __ ldr_s32(Rtemp, Address(Rublock, Deoptimization::UnrollBlock::unpack_kind_offset_in_bytes()));
1571     __ cmp_32(Rkind, Rtemp);
1572     __ b(L, eq);
1573     __ stop("Rkind was overwritten");
1574     __ bind(L);
1575   }
1576 #endif
1577 
1578   // Call unpack_frames with proper arguments
1579   __ mov(R0, Rthread);
1580   __ mov(R1, Rkind);
1581 
1582   pc_offset = __ set_last_Java_frame(SP, FP, true, Rtemp);
1583   assert(((__ pc()) - start) == __ offset(), "warning: start differs from code_begin");
1584   __ call_VM_leaf(CAST_FROM_FN_PTR(address, Deoptimization::unpack_frames));
1585   if (pc_offset == -1) {
1586     pc_offset = __ offset();
1587   }
1588   oop_maps->add_gc_map(pc_offset, new OopMap(frame_size_in_words * VMRegImpl::slots_per_word, 0));
1589   __ reset_last_Java_frame(Rtemp); // Rtemp free since scratched by far call
1590 
1591   // Collect return values, pop self-frame and jump to interpreter
1592   __ ldr(R0, Address(SP, RegisterSaver::R0_offset * wordSize));
1593   __ ldr(R1, Address(SP, RegisterSaver::R1_offset * wordSize));
1594   // Interpreter floats controlled by __SOFTFP__, but compiler
1595   // float return value registers controlled by __ABI_HARD__
1596   // This matters for vfp-sflt builds.
1597 #ifndef __SOFTFP__
1598   // Interpreter hard float
1599 #ifdef __ABI_HARD__
1600   // Compiler float return value in FP registers
1601   __ ldr_double(D0, Address(SP, RegisterSaver::D0_offset * wordSize));
1602 #else
1603   // Compiler float return value in integer registers,
1604   // copy to D0 for interpreter (S0 <-- R0)
1605   __ fmdrr(D0_tos, R0, R1);
1606 #endif
1607 #endif // !__SOFTFP__
1608   __ mov(SP, FP);
1609 
1610   __ pop(RegisterSet(FP) | RegisterSet(PC));
1611 
1612   __ flush();
1613 
1614   _deopt_blob = DeoptimizationBlob::create(&buffer, oop_maps, 0, exception_offset,
1615                                            reexecute_offset, frame_size_in_words);
1616   _deopt_blob->set_unpack_with_exception_in_tls_offset(exception_in_tls_offset);
1617 }
1618 
1619 #ifdef COMPILER2
1620 
1621 //------------------------------generate_uncommon_trap_blob--------------------
1622 // Ought to generate an ideal graph & compile, but here's some SPARC ASM
1623 // instead.
generate_uncommon_trap_blob()1624 void SharedRuntime::generate_uncommon_trap_blob() {
1625   // allocate space for the code
1626   ResourceMark rm;
1627 
1628   // setup code generation tools
1629   int pad = VerifyThread ? 512 : 0;
1630 #ifdef _LP64
1631   CodeBuffer buffer("uncommon_trap_blob", 2700+pad, 512);
1632 #else
1633   // Measured 8/7/03 at 660 in 32bit debug build (no VerifyThread)
1634   // Measured 8/7/03 at 1028 in 32bit debug build (VerifyThread)
1635   CodeBuffer buffer("uncommon_trap_blob", 2000+pad, 512);
1636 #endif
1637   // bypassed when code generation useless
1638   MacroAssembler* masm               = new MacroAssembler(&buffer);
1639   const Register Rublock = R6;
1640   const Register Rsender = altFP_7_11;
1641   assert_different_registers(Rublock, Rsender, Rexception_obj, R0, R1, R2, R3, R8, Rtemp);
1642 
1643   //
1644   // This is the entry point for all traps the compiler takes when it thinks
1645   // it cannot handle further execution of compilation code. The frame is
1646   // deoptimized in these cases and converted into interpreter frames for
1647   // execution
1648   // The steps taken by this frame are as follows:
1649   //   - push a fake "unpack_frame"
1650   //   - call the C routine Deoptimization::uncommon_trap (this function
1651   //     packs the current compiled frame into vframe arrays and returns
1652   //     information about the number and size of interpreter frames which
1653   //     are equivalent to the frame which is being deoptimized)
1654   //   - deallocate the "unpack_frame"
1655   //   - deallocate the deoptimization frame
1656   //   - in a loop using the information returned in the previous step
1657   //     push interpreter frames;
1658   //   - create a dummy "unpack_frame"
1659   //   - call the C routine: Deoptimization::unpack_frames (this function
1660   //     lays out values on the interpreter frame which was just created)
1661   //   - deallocate the dummy unpack_frame
1662   //   - return to the interpreter entry point
1663   //
1664   //  Refer to the following methods for more information:
1665   //   - Deoptimization::uncommon_trap
1666   //   - Deoptimization::unpack_frame
1667 
1668   // the unloaded class index is in R0 (first parameter to this blob)
1669 
1670   __ raw_push(FP, LR);
1671   __ set_last_Java_frame(SP, FP, false, Rtemp);
1672   __ mov(R2, Deoptimization::Unpack_uncommon_trap);
1673   __ mov(R1, R0);
1674   __ mov(R0, Rthread);
1675   __ call(CAST_FROM_FN_PTR(address, Deoptimization::uncommon_trap));
1676   __ mov(Rublock, R0);
1677   __ reset_last_Java_frame(Rtemp);
1678   __ raw_pop(FP, LR);
1679 
1680 #ifdef ASSERT
1681   { Label L;
1682     __ ldr_s32(Rtemp, Address(Rublock, Deoptimization::UnrollBlock::unpack_kind_offset_in_bytes()));
1683     __ cmp_32(Rtemp, Deoptimization::Unpack_uncommon_trap);
1684     __ b(L, eq);
1685     __ stop("SharedRuntime::generate_uncommon_trap_blob: expected Unpack_uncommon_trap");
1686     __ bind(L);
1687   }
1688 #endif
1689 
1690 
1691   // Set initial stack state before pushing interpreter frames
1692   __ ldr_s32(Rtemp, Address(Rublock, Deoptimization::UnrollBlock::size_of_deoptimized_frame_offset_in_bytes()));
1693   __ ldr(R2, Address(Rublock, Deoptimization::UnrollBlock::frame_pcs_offset_in_bytes()));
1694   __ ldr(R3, Address(Rublock, Deoptimization::UnrollBlock::frame_sizes_offset_in_bytes()));
1695 
1696   __ add(SP, SP, Rtemp);
1697 
1698   // See if it is enough stack to push deoptimized frames
1699 #ifdef ASSERT
1700   // Compilers generate code that bang the stack by as much as the
1701   // interpreter would need. So this stack banging should never
1702   // trigger a fault. Verify that it does not on non product builds.
1703   if (UseStackBanging) {
1704     // The compiled method that we are deoptimizing was popped from the stack.
1705     // If the stack bang results in a stack overflow, we don't return to the
1706     // method that is being deoptimized. The stack overflow exception is
1707     // propagated to the caller of the deoptimized method. Need to get the pc
1708     // from the caller in LR and restore FP.
1709     __ ldr(LR, Address(R2, 0));
1710     __ ldr(FP, Address(Rublock, Deoptimization::UnrollBlock::initial_info_offset_in_bytes()));
1711     __ ldr_s32(R8, Address(Rublock, Deoptimization::UnrollBlock::total_frame_sizes_offset_in_bytes()));
1712     __ arm_stack_overflow_check(R8, Rtemp);
1713   }
1714 #endif
1715   __ ldr_s32(R8, Address(Rublock, Deoptimization::UnrollBlock::number_of_frames_offset_in_bytes()));
1716   __ ldr_s32(Rtemp, Address(Rublock, Deoptimization::UnrollBlock::caller_adjustment_offset_in_bytes()));
1717   __ mov(Rsender, SP);
1718   __ sub(SP, SP, Rtemp);
1719   //  __ ldr(FP, Address(FP));
1720   __ ldr(FP, Address(Rublock, Deoptimization::UnrollBlock::initial_info_offset_in_bytes()));
1721 
1722   // Push interpreter frames in a loop
1723   Label loop;
1724   __ bind(loop);
1725   __ ldr(LR, Address(R2, wordSize, post_indexed));         // load frame pc
1726   __ ldr(Rtemp, Address(R3, wordSize, post_indexed));      // load frame size
1727 
1728   __ raw_push(FP, LR);                                     // create new frame
1729   __ mov(FP, SP);
1730   __ sub(Rtemp, Rtemp, 2*wordSize);
1731 
1732   __ sub(SP, SP, Rtemp);
1733 
1734   __ str(Rsender, Address(FP, frame::interpreter_frame_sender_sp_offset * wordSize));
1735   __ mov(LR, 0);
1736   __ str(LR, Address(FP, frame::interpreter_frame_last_sp_offset * wordSize));
1737   __ subs(R8, R8, 1);                               // decrement counter
1738   __ mov(Rsender, SP);
1739   __ b(loop, ne);
1740 
1741   // Re-push self-frame
1742   __ ldr(LR, Address(R2));
1743   __ raw_push(FP, LR);
1744   __ mov(FP, SP);
1745 
1746   // Call unpack_frames with proper arguments
1747   __ mov(R0, Rthread);
1748   __ mov(R1, Deoptimization::Unpack_uncommon_trap);
1749   __ set_last_Java_frame(SP, FP, true, Rtemp);
1750   __ call_VM_leaf(CAST_FROM_FN_PTR(address, Deoptimization::unpack_frames));
1751   //  oop_maps->add_gc_map(__ pc() - start, new OopMap(frame_size_in_words, 0));
1752   __ reset_last_Java_frame(Rtemp);
1753 
1754   __ mov(SP, FP);
1755   __ pop(RegisterSet(FP) | RegisterSet(PC));
1756 
1757   masm->flush();
1758   _uncommon_trap_blob = UncommonTrapBlob::create(&buffer, NULL, 2 /* LR+FP */);
1759 }
1760 
1761 #endif // COMPILER2
1762 
1763 //------------------------------generate_handler_blob------
1764 //
1765 // Generate a special Compile2Runtime blob that saves all registers,
1766 // setup oopmap, and calls safepoint code to stop the compiled code for
1767 // a safepoint.
1768 //
generate_handler_blob(address call_ptr,int poll_type)1769 SafepointBlob* SharedRuntime::generate_handler_blob(address call_ptr, int poll_type) {
1770   assert(StubRoutines::forward_exception_entry() != NULL, "must be generated before");
1771 
1772   ResourceMark rm;
1773   CodeBuffer buffer("handler_blob", 256, 256);
1774   int frame_size_words;
1775   OopMapSet* oop_maps;
1776 
1777   bool cause_return = (poll_type == POLL_AT_RETURN);
1778 
1779   MacroAssembler* masm = new MacroAssembler(&buffer);
1780   address start = __ pc();
1781   oop_maps = new OopMapSet();
1782 
1783   if (!cause_return) {
1784     __ sub(SP, SP, 4); // make room for LR which may still be live
1785                        // here if we are coming from a c2 method
1786   }
1787 
1788   OopMap* map = RegisterSaver::save_live_registers(masm, &frame_size_words, !cause_return);
1789   if (!cause_return) {
1790     // update saved PC with correct value
1791     // need 2 steps because LR can be live in c2 method
1792     __ ldr(LR, Address(Rthread, JavaThread::saved_exception_pc_offset()));
1793     __ str(LR, Address(SP, RegisterSaver::LR_offset * wordSize));
1794   }
1795 
1796   __ mov(R0, Rthread);
1797   int pc_offset = __ set_last_Java_frame(SP, FP, false, Rtemp); // note: FP may not need to be saved (not on x86)
1798   assert(((__ pc()) - start) == __ offset(), "warning: start differs from code_begin");
1799   __ call(call_ptr);
1800   if (pc_offset == -1) {
1801     pc_offset = __ offset();
1802   }
1803   oop_maps->add_gc_map(pc_offset, map);
1804   __ reset_last_Java_frame(Rtemp); // Rtemp free since scratched by far call
1805 
1806   // Check for pending exception
1807   __ ldr(Rtemp, Address(Rthread, Thread::pending_exception_offset()));
1808   __ cmp(Rtemp, 0);
1809 
1810   if (!cause_return) {
1811     RegisterSaver::restore_live_registers(masm, false);
1812     __ pop(PC, eq);
1813     __ pop(Rexception_pc);
1814   } else {
1815     RegisterSaver::restore_live_registers(masm);
1816     __ bx(LR, eq);
1817     __ mov(Rexception_pc, LR);
1818   }
1819 
1820   __ jump(StubRoutines::forward_exception_entry(), relocInfo::runtime_call_type, Rtemp);
1821 
1822   __ flush();
1823 
1824   return SafepointBlob::create(&buffer, oop_maps, frame_size_words);
1825 }
1826 
generate_resolve_blob(address destination,const char * name)1827 RuntimeStub* SharedRuntime::generate_resolve_blob(address destination, const char* name) {
1828   assert(StubRoutines::forward_exception_entry() != NULL, "must be generated before");
1829 
1830   ResourceMark rm;
1831   CodeBuffer buffer(name, 1000, 512);
1832   int frame_size_words;
1833   OopMapSet *oop_maps;
1834   int frame_complete;
1835 
1836   MacroAssembler* masm = new MacroAssembler(&buffer);
1837   Label pending_exception;
1838 
1839   int start = __ offset();
1840 
1841   oop_maps = new OopMapSet();
1842   OopMap* map = RegisterSaver::save_live_registers(masm, &frame_size_words);
1843 
1844   frame_complete = __ offset();
1845 
1846   __ mov(R0, Rthread);
1847 
1848   int pc_offset = __ set_last_Java_frame(SP, FP, false, Rtemp);
1849   assert(start == 0, "warning: start differs from code_begin");
1850   __ call(destination);
1851   if (pc_offset == -1) {
1852     pc_offset = __ offset();
1853   }
1854   oop_maps->add_gc_map(pc_offset, map);
1855   __ reset_last_Java_frame(Rtemp); // Rtemp free since scratched by far call
1856 
1857   __ ldr(R1, Address(Rthread, Thread::pending_exception_offset()));
1858   __ cbnz(R1, pending_exception);
1859 
1860   // Overwrite saved register values
1861 
1862   // Place metadata result of VM call into Rmethod
1863   __ get_vm_result_2(R1, Rtemp);
1864   __ str(R1, Address(SP, RegisterSaver::Rmethod_offset * wordSize));
1865 
1866   // Place target address (VM call result) into Rtemp
1867   __ str(R0, Address(SP, RegisterSaver::Rtemp_offset * wordSize));
1868 
1869   RegisterSaver::restore_live_registers(masm);
1870   __ jump(Rtemp);
1871 
1872   __ bind(pending_exception);
1873 
1874   RegisterSaver::restore_live_registers(masm);
1875   const Register Rzero = __ zero_register(Rtemp);
1876   __ str(Rzero, Address(Rthread, JavaThread::vm_result_2_offset()));
1877   __ mov(Rexception_pc, LR);
1878   __ jump(StubRoutines::forward_exception_entry(), relocInfo::runtime_call_type, Rtemp);
1879 
1880   __ flush();
1881 
1882   return RuntimeStub::new_runtime_stub(name, &buffer, frame_complete, frame_size_words, oop_maps, true);
1883 }
1884