src/cpu/x86/vm/sharedRuntime_x86_32.cpp

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 6723
0bf37f737702
parent 0
f90c822e73f8
child 9041
95a08233f46c
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "asm/macroAssembler.hpp"
aoqi@0 27 #include "asm/macroAssembler.inline.hpp"
aoqi@0 28 #include "code/debugInfoRec.hpp"
aoqi@0 29 #include "code/icBuffer.hpp"
aoqi@0 30 #include "code/vtableStubs.hpp"
aoqi@0 31 #include "interpreter/interpreter.hpp"
aoqi@0 32 #include "oops/compiledICHolder.hpp"
aoqi@0 33 #include "prims/jvmtiRedefineClassesTrace.hpp"
aoqi@0 34 #include "runtime/sharedRuntime.hpp"
aoqi@0 35 #include "runtime/vframeArray.hpp"
aoqi@0 36 #include "vmreg_x86.inline.hpp"
aoqi@0 37 #ifdef COMPILER1
aoqi@0 38 #include "c1/c1_Runtime1.hpp"
aoqi@0 39 #endif
aoqi@0 40 #ifdef COMPILER2
aoqi@0 41 #include "opto/runtime.hpp"
aoqi@0 42 #endif
aoqi@0 43
aoqi@0 44 #define __ masm->
aoqi@0 45
aoqi@0 46 const int StackAlignmentInSlots = StackAlignmentInBytes / VMRegImpl::stack_slot_size;
aoqi@0 47
aoqi@0 48 class RegisterSaver {
aoqi@0 49 // Capture info about frame layout
aoqi@0 50 #define DEF_XMM_OFFS(regnum) xmm ## regnum ## _off = xmm_off + (regnum)*16/BytesPerInt, xmm ## regnum ## H_off
aoqi@0 51 enum layout {
aoqi@0 52 fpu_state_off = 0,
aoqi@0 53 fpu_state_end = fpu_state_off+FPUStateSizeInWords,
aoqi@0 54 st0_off, st0H_off,
aoqi@0 55 st1_off, st1H_off,
aoqi@0 56 st2_off, st2H_off,
aoqi@0 57 st3_off, st3H_off,
aoqi@0 58 st4_off, st4H_off,
aoqi@0 59 st5_off, st5H_off,
aoqi@0 60 st6_off, st6H_off,
aoqi@0 61 st7_off, st7H_off,
aoqi@0 62 xmm_off,
aoqi@0 63 DEF_XMM_OFFS(0),
aoqi@0 64 DEF_XMM_OFFS(1),
aoqi@0 65 DEF_XMM_OFFS(2),
aoqi@0 66 DEF_XMM_OFFS(3),
aoqi@0 67 DEF_XMM_OFFS(4),
aoqi@0 68 DEF_XMM_OFFS(5),
aoqi@0 69 DEF_XMM_OFFS(6),
aoqi@0 70 DEF_XMM_OFFS(7),
aoqi@0 71 flags_off = xmm7_off + 16/BytesPerInt + 1, // 16-byte stack alignment fill word
aoqi@0 72 rdi_off,
aoqi@0 73 rsi_off,
aoqi@0 74 ignore_off, // extra copy of rbp,
aoqi@0 75 rsp_off,
aoqi@0 76 rbx_off,
aoqi@0 77 rdx_off,
aoqi@0 78 rcx_off,
aoqi@0 79 rax_off,
aoqi@0 80 // The frame sender code expects that rbp will be in the "natural" place and
aoqi@0 81 // will override any oopMap setting for it. We must therefore force the layout
aoqi@0 82 // so that it agrees with the frame sender code.
aoqi@0 83 rbp_off,
aoqi@0 84 return_off, // slot for return address
aoqi@0 85 reg_save_size };
aoqi@0 86 enum { FPU_regs_live = flags_off - fpu_state_end };
aoqi@0 87
aoqi@0 88 public:
aoqi@0 89
aoqi@0 90 static OopMap* save_live_registers(MacroAssembler* masm, int additional_frame_words,
aoqi@0 91 int* total_frame_words, bool verify_fpu = true, bool save_vectors = false);
aoqi@0 92 static void restore_live_registers(MacroAssembler* masm, bool restore_vectors = false);
aoqi@0 93
aoqi@0 94 static int rax_offset() { return rax_off; }
aoqi@0 95 static int rbx_offset() { return rbx_off; }
aoqi@0 96
aoqi@0 97 // Offsets into the register save area
aoqi@0 98 // Used by deoptimization when it is managing result register
aoqi@0 99 // values on its own
aoqi@0 100
aoqi@0 101 static int raxOffset(void) { return rax_off; }
aoqi@0 102 static int rdxOffset(void) { return rdx_off; }
aoqi@0 103 static int rbxOffset(void) { return rbx_off; }
aoqi@0 104 static int xmm0Offset(void) { return xmm0_off; }
aoqi@0 105 // This really returns a slot in the fp save area, which one is not important
aoqi@0 106 static int fpResultOffset(void) { return st0_off; }
aoqi@0 107
aoqi@0 108 // During deoptimization only the result register need to be restored
aoqi@0 109 // all the other values have already been extracted.
aoqi@0 110
aoqi@0 111 static void restore_result_registers(MacroAssembler* masm);
aoqi@0 112
aoqi@0 113 };
aoqi@0 114
aoqi@0 115 OopMap* RegisterSaver::save_live_registers(MacroAssembler* masm, int additional_frame_words,
aoqi@0 116 int* total_frame_words, bool verify_fpu, bool save_vectors) {
aoqi@0 117 int vect_words = 0;
aoqi@0 118 #ifdef COMPILER2
aoqi@0 119 if (save_vectors) {
aoqi@0 120 assert(UseAVX > 0, "256bit vectors are supported only with AVX");
aoqi@0 121 assert(MaxVectorSize == 32, "only 256bit vectors are supported now");
aoqi@0 122 // Save upper half of YMM registes
aoqi@0 123 vect_words = 8 * 16 / wordSize;
aoqi@0 124 additional_frame_words += vect_words;
aoqi@0 125 }
aoqi@0 126 #else
aoqi@0 127 assert(!save_vectors, "vectors are generated only by C2");
aoqi@0 128 #endif
aoqi@0 129 int frame_size_in_bytes = (reg_save_size + additional_frame_words) * wordSize;
aoqi@0 130 int frame_words = frame_size_in_bytes / wordSize;
aoqi@0 131 *total_frame_words = frame_words;
aoqi@0 132
aoqi@0 133 assert(FPUStateSizeInWords == 27, "update stack layout");
aoqi@0 134
aoqi@0 135 // save registers, fpu state, and flags
aoqi@0 136 // We assume caller has already has return address slot on the stack
aoqi@0 137 // We push epb twice in this sequence because we want the real rbp,
aoqi@0 138 // to be under the return like a normal enter and we want to use pusha
aoqi@0 139 // We push by hand instead of pusing push
aoqi@0 140 __ enter();
aoqi@0 141 __ pusha();
aoqi@0 142 __ pushf();
aoqi@0 143 __ subptr(rsp,FPU_regs_live*wordSize); // Push FPU registers space
aoqi@0 144 __ push_FPU_state(); // Save FPU state & init
aoqi@0 145
aoqi@0 146 if (verify_fpu) {
aoqi@0 147 // Some stubs may have non standard FPU control word settings so
aoqi@0 148 // only check and reset the value when it required to be the
aoqi@0 149 // standard value. The safepoint blob in particular can be used
aoqi@0 150 // in methods which are using the 24 bit control word for
aoqi@0 151 // optimized float math.
aoqi@0 152
aoqi@0 153 #ifdef ASSERT
aoqi@0 154 // Make sure the control word has the expected value
aoqi@0 155 Label ok;
aoqi@0 156 __ cmpw(Address(rsp, 0), StubRoutines::fpu_cntrl_wrd_std());
aoqi@0 157 __ jccb(Assembler::equal, ok);
aoqi@0 158 __ stop("corrupted control word detected");
aoqi@0 159 __ bind(ok);
aoqi@0 160 #endif
aoqi@0 161
aoqi@0 162 // Reset the control word to guard against exceptions being unmasked
aoqi@0 163 // since fstp_d can cause FPU stack underflow exceptions. Write it
aoqi@0 164 // into the on stack copy and then reload that to make sure that the
aoqi@0 165 // current and future values are correct.
aoqi@0 166 __ movw(Address(rsp, 0), StubRoutines::fpu_cntrl_wrd_std());
aoqi@0 167 }
aoqi@0 168
aoqi@0 169 __ frstor(Address(rsp, 0));
aoqi@0 170 if (!verify_fpu) {
aoqi@0 171 // Set the control word so that exceptions are masked for the
aoqi@0 172 // following code.
aoqi@0 173 __ fldcw(ExternalAddress(StubRoutines::addr_fpu_cntrl_wrd_std()));
aoqi@0 174 }
aoqi@0 175
aoqi@0 176 // Save the FPU registers in de-opt-able form
aoqi@0 177
aoqi@0 178 __ fstp_d(Address(rsp, st0_off*wordSize)); // st(0)
aoqi@0 179 __ fstp_d(Address(rsp, st1_off*wordSize)); // st(1)
aoqi@0 180 __ fstp_d(Address(rsp, st2_off*wordSize)); // st(2)
aoqi@0 181 __ fstp_d(Address(rsp, st3_off*wordSize)); // st(3)
aoqi@0 182 __ fstp_d(Address(rsp, st4_off*wordSize)); // st(4)
aoqi@0 183 __ fstp_d(Address(rsp, st5_off*wordSize)); // st(5)
aoqi@0 184 __ fstp_d(Address(rsp, st6_off*wordSize)); // st(6)
aoqi@0 185 __ fstp_d(Address(rsp, st7_off*wordSize)); // st(7)
aoqi@0 186
aoqi@0 187 if( UseSSE == 1 ) { // Save the XMM state
aoqi@0 188 __ movflt(Address(rsp,xmm0_off*wordSize),xmm0);
aoqi@0 189 __ movflt(Address(rsp,xmm1_off*wordSize),xmm1);
aoqi@0 190 __ movflt(Address(rsp,xmm2_off*wordSize),xmm2);
aoqi@0 191 __ movflt(Address(rsp,xmm3_off*wordSize),xmm3);
aoqi@0 192 __ movflt(Address(rsp,xmm4_off*wordSize),xmm4);
aoqi@0 193 __ movflt(Address(rsp,xmm5_off*wordSize),xmm5);
aoqi@0 194 __ movflt(Address(rsp,xmm6_off*wordSize),xmm6);
aoqi@0 195 __ movflt(Address(rsp,xmm7_off*wordSize),xmm7);
aoqi@0 196 } else if( UseSSE >= 2 ) {
aoqi@0 197 // Save whole 128bit (16 bytes) XMM regiters
aoqi@0 198 __ movdqu(Address(rsp,xmm0_off*wordSize),xmm0);
aoqi@0 199 __ movdqu(Address(rsp,xmm1_off*wordSize),xmm1);
aoqi@0 200 __ movdqu(Address(rsp,xmm2_off*wordSize),xmm2);
aoqi@0 201 __ movdqu(Address(rsp,xmm3_off*wordSize),xmm3);
aoqi@0 202 __ movdqu(Address(rsp,xmm4_off*wordSize),xmm4);
aoqi@0 203 __ movdqu(Address(rsp,xmm5_off*wordSize),xmm5);
aoqi@0 204 __ movdqu(Address(rsp,xmm6_off*wordSize),xmm6);
aoqi@0 205 __ movdqu(Address(rsp,xmm7_off*wordSize),xmm7);
aoqi@0 206 }
aoqi@0 207
aoqi@0 208 if (vect_words > 0) {
aoqi@0 209 assert(vect_words*wordSize == 128, "");
aoqi@0 210 __ subptr(rsp, 128); // Save upper half of YMM registes
aoqi@0 211 __ vextractf128h(Address(rsp, 0),xmm0);
aoqi@0 212 __ vextractf128h(Address(rsp, 16),xmm1);
aoqi@0 213 __ vextractf128h(Address(rsp, 32),xmm2);
aoqi@0 214 __ vextractf128h(Address(rsp, 48),xmm3);
aoqi@0 215 __ vextractf128h(Address(rsp, 64),xmm4);
aoqi@0 216 __ vextractf128h(Address(rsp, 80),xmm5);
aoqi@0 217 __ vextractf128h(Address(rsp, 96),xmm6);
aoqi@0 218 __ vextractf128h(Address(rsp,112),xmm7);
aoqi@0 219 }
aoqi@0 220
aoqi@0 221 // Set an oopmap for the call site. This oopmap will map all
aoqi@0 222 // oop-registers and debug-info registers as callee-saved. This
aoqi@0 223 // will allow deoptimization at this safepoint to find all possible
aoqi@0 224 // debug-info recordings, as well as let GC find all oops.
aoqi@0 225
aoqi@0 226 OopMapSet *oop_maps = new OopMapSet();
aoqi@0 227 OopMap* map = new OopMap( frame_words, 0 );
aoqi@0 228
aoqi@0 229 #define STACK_OFFSET(x) VMRegImpl::stack2reg((x) + additional_frame_words)
aoqi@0 230
aoqi@0 231 map->set_callee_saved(STACK_OFFSET( rax_off), rax->as_VMReg());
aoqi@0 232 map->set_callee_saved(STACK_OFFSET( rcx_off), rcx->as_VMReg());
aoqi@0 233 map->set_callee_saved(STACK_OFFSET( rdx_off), rdx->as_VMReg());
aoqi@0 234 map->set_callee_saved(STACK_OFFSET( rbx_off), rbx->as_VMReg());
aoqi@0 235 // rbp, location is known implicitly, no oopMap
aoqi@0 236 map->set_callee_saved(STACK_OFFSET( rsi_off), rsi->as_VMReg());
aoqi@0 237 map->set_callee_saved(STACK_OFFSET( rdi_off), rdi->as_VMReg());
aoqi@0 238 map->set_callee_saved(STACK_OFFSET(st0_off), as_FloatRegister(0)->as_VMReg());
aoqi@0 239 map->set_callee_saved(STACK_OFFSET(st1_off), as_FloatRegister(1)->as_VMReg());
aoqi@0 240 map->set_callee_saved(STACK_OFFSET(st2_off), as_FloatRegister(2)->as_VMReg());
aoqi@0 241 map->set_callee_saved(STACK_OFFSET(st3_off), as_FloatRegister(3)->as_VMReg());
aoqi@0 242 map->set_callee_saved(STACK_OFFSET(st4_off), as_FloatRegister(4)->as_VMReg());
aoqi@0 243 map->set_callee_saved(STACK_OFFSET(st5_off), as_FloatRegister(5)->as_VMReg());
aoqi@0 244 map->set_callee_saved(STACK_OFFSET(st6_off), as_FloatRegister(6)->as_VMReg());
aoqi@0 245 map->set_callee_saved(STACK_OFFSET(st7_off), as_FloatRegister(7)->as_VMReg());
aoqi@0 246 map->set_callee_saved(STACK_OFFSET(xmm0_off), xmm0->as_VMReg());
aoqi@0 247 map->set_callee_saved(STACK_OFFSET(xmm1_off), xmm1->as_VMReg());
aoqi@0 248 map->set_callee_saved(STACK_OFFSET(xmm2_off), xmm2->as_VMReg());
aoqi@0 249 map->set_callee_saved(STACK_OFFSET(xmm3_off), xmm3->as_VMReg());
aoqi@0 250 map->set_callee_saved(STACK_OFFSET(xmm4_off), xmm4->as_VMReg());
aoqi@0 251 map->set_callee_saved(STACK_OFFSET(xmm5_off), xmm5->as_VMReg());
aoqi@0 252 map->set_callee_saved(STACK_OFFSET(xmm6_off), xmm6->as_VMReg());
aoqi@0 253 map->set_callee_saved(STACK_OFFSET(xmm7_off), xmm7->as_VMReg());
aoqi@0 254 // %%% This is really a waste but we'll keep things as they were for now
aoqi@0 255 if (true) {
aoqi@0 256 #define NEXTREG(x) (x)->as_VMReg()->next()
aoqi@0 257 map->set_callee_saved(STACK_OFFSET(st0H_off), NEXTREG(as_FloatRegister(0)));
aoqi@0 258 map->set_callee_saved(STACK_OFFSET(st1H_off), NEXTREG(as_FloatRegister(1)));
aoqi@0 259 map->set_callee_saved(STACK_OFFSET(st2H_off), NEXTREG(as_FloatRegister(2)));
aoqi@0 260 map->set_callee_saved(STACK_OFFSET(st3H_off), NEXTREG(as_FloatRegister(3)));
aoqi@0 261 map->set_callee_saved(STACK_OFFSET(st4H_off), NEXTREG(as_FloatRegister(4)));
aoqi@0 262 map->set_callee_saved(STACK_OFFSET(st5H_off), NEXTREG(as_FloatRegister(5)));
aoqi@0 263 map->set_callee_saved(STACK_OFFSET(st6H_off), NEXTREG(as_FloatRegister(6)));
aoqi@0 264 map->set_callee_saved(STACK_OFFSET(st7H_off), NEXTREG(as_FloatRegister(7)));
aoqi@0 265 map->set_callee_saved(STACK_OFFSET(xmm0H_off), NEXTREG(xmm0));
aoqi@0 266 map->set_callee_saved(STACK_OFFSET(xmm1H_off), NEXTREG(xmm1));
aoqi@0 267 map->set_callee_saved(STACK_OFFSET(xmm2H_off), NEXTREG(xmm2));
aoqi@0 268 map->set_callee_saved(STACK_OFFSET(xmm3H_off), NEXTREG(xmm3));
aoqi@0 269 map->set_callee_saved(STACK_OFFSET(xmm4H_off), NEXTREG(xmm4));
aoqi@0 270 map->set_callee_saved(STACK_OFFSET(xmm5H_off), NEXTREG(xmm5));
aoqi@0 271 map->set_callee_saved(STACK_OFFSET(xmm6H_off), NEXTREG(xmm6));
aoqi@0 272 map->set_callee_saved(STACK_OFFSET(xmm7H_off), NEXTREG(xmm7));
aoqi@0 273 #undef NEXTREG
aoqi@0 274 #undef STACK_OFFSET
aoqi@0 275 }
aoqi@0 276
aoqi@0 277 return map;
aoqi@0 278
aoqi@0 279 }
aoqi@0 280
aoqi@0 281 void RegisterSaver::restore_live_registers(MacroAssembler* masm, bool restore_vectors) {
aoqi@0 282 // Recover XMM & FPU state
aoqi@0 283 int additional_frame_bytes = 0;
aoqi@0 284 #ifdef COMPILER2
aoqi@0 285 if (restore_vectors) {
aoqi@0 286 assert(UseAVX > 0, "256bit vectors are supported only with AVX");
aoqi@0 287 assert(MaxVectorSize == 32, "only 256bit vectors are supported now");
aoqi@0 288 additional_frame_bytes = 128;
aoqi@0 289 }
aoqi@0 290 #else
aoqi@0 291 assert(!restore_vectors, "vectors are generated only by C2");
aoqi@0 292 #endif
aoqi@0 293 if (UseSSE == 1) {
aoqi@0 294 assert(additional_frame_bytes == 0, "");
aoqi@0 295 __ movflt(xmm0,Address(rsp,xmm0_off*wordSize));
aoqi@0 296 __ movflt(xmm1,Address(rsp,xmm1_off*wordSize));
aoqi@0 297 __ movflt(xmm2,Address(rsp,xmm2_off*wordSize));
aoqi@0 298 __ movflt(xmm3,Address(rsp,xmm3_off*wordSize));
aoqi@0 299 __ movflt(xmm4,Address(rsp,xmm4_off*wordSize));
aoqi@0 300 __ movflt(xmm5,Address(rsp,xmm5_off*wordSize));
aoqi@0 301 __ movflt(xmm6,Address(rsp,xmm6_off*wordSize));
aoqi@0 302 __ movflt(xmm7,Address(rsp,xmm7_off*wordSize));
aoqi@0 303 } else if (UseSSE >= 2) {
aoqi@0 304 #define STACK_ADDRESS(x) Address(rsp,(x)*wordSize + additional_frame_bytes)
aoqi@0 305 __ movdqu(xmm0,STACK_ADDRESS(xmm0_off));
aoqi@0 306 __ movdqu(xmm1,STACK_ADDRESS(xmm1_off));
aoqi@0 307 __ movdqu(xmm2,STACK_ADDRESS(xmm2_off));
aoqi@0 308 __ movdqu(xmm3,STACK_ADDRESS(xmm3_off));
aoqi@0 309 __ movdqu(xmm4,STACK_ADDRESS(xmm4_off));
aoqi@0 310 __ movdqu(xmm5,STACK_ADDRESS(xmm5_off));
aoqi@0 311 __ movdqu(xmm6,STACK_ADDRESS(xmm6_off));
aoqi@0 312 __ movdqu(xmm7,STACK_ADDRESS(xmm7_off));
aoqi@0 313 #undef STACK_ADDRESS
aoqi@0 314 }
aoqi@0 315 if (restore_vectors) {
aoqi@0 316 // Restore upper half of YMM registes.
aoqi@0 317 assert(additional_frame_bytes == 128, "");
aoqi@0 318 __ vinsertf128h(xmm0, Address(rsp, 0));
aoqi@0 319 __ vinsertf128h(xmm1, Address(rsp, 16));
aoqi@0 320 __ vinsertf128h(xmm2, Address(rsp, 32));
aoqi@0 321 __ vinsertf128h(xmm3, Address(rsp, 48));
aoqi@0 322 __ vinsertf128h(xmm4, Address(rsp, 64));
aoqi@0 323 __ vinsertf128h(xmm5, Address(rsp, 80));
aoqi@0 324 __ vinsertf128h(xmm6, Address(rsp, 96));
aoqi@0 325 __ vinsertf128h(xmm7, Address(rsp,112));
aoqi@0 326 __ addptr(rsp, additional_frame_bytes);
aoqi@0 327 }
aoqi@0 328 __ pop_FPU_state();
aoqi@0 329 __ addptr(rsp, FPU_regs_live*wordSize); // Pop FPU registers
aoqi@0 330
aoqi@0 331 __ popf();
aoqi@0 332 __ popa();
aoqi@0 333 // Get the rbp, described implicitly by the frame sender code (no oopMap)
aoqi@0 334 __ pop(rbp);
aoqi@0 335
aoqi@0 336 }
aoqi@0 337
aoqi@0 338 void RegisterSaver::restore_result_registers(MacroAssembler* masm) {
aoqi@0 339
aoqi@0 340 // Just restore result register. Only used by deoptimization. By
aoqi@0 341 // now any callee save register that needs to be restore to a c2
aoqi@0 342 // caller of the deoptee has been extracted into the vframeArray
aoqi@0 343 // and will be stuffed into the c2i adapter we create for later
aoqi@0 344 // restoration so only result registers need to be restored here.
aoqi@0 345 //
aoqi@0 346
aoqi@0 347 __ frstor(Address(rsp, 0)); // Restore fpu state
aoqi@0 348
aoqi@0 349 // Recover XMM & FPU state
aoqi@0 350 if( UseSSE == 1 ) {
aoqi@0 351 __ movflt(xmm0, Address(rsp, xmm0_off*wordSize));
aoqi@0 352 } else if( UseSSE >= 2 ) {
aoqi@0 353 __ movdbl(xmm0, Address(rsp, xmm0_off*wordSize));
aoqi@0 354 }
aoqi@0 355 __ movptr(rax, Address(rsp, rax_off*wordSize));
aoqi@0 356 __ movptr(rdx, Address(rsp, rdx_off*wordSize));
aoqi@0 357 // Pop all of the register save are off the stack except the return address
aoqi@0 358 __ addptr(rsp, return_off * wordSize);
aoqi@0 359 }
aoqi@0 360
aoqi@0 361 // Is vector's size (in bytes) bigger than a size saved by default?
aoqi@0 362 // 16 bytes XMM registers are saved by default using SSE2 movdqu instructions.
aoqi@0 363 // Note, MaxVectorSize == 0 with UseSSE < 2 and vectors are not generated.
aoqi@0 364 bool SharedRuntime::is_wide_vector(int size) {
aoqi@0 365 return size > 16;
aoqi@0 366 }
aoqi@0 367
aoqi@0 368 // The java_calling_convention describes stack locations as ideal slots on
aoqi@0 369 // a frame with no abi restrictions. Since we must observe abi restrictions
aoqi@0 370 // (like the placement of the register window) the slots must be biased by
aoqi@0 371 // the following value.
aoqi@0 372 static int reg2offset_in(VMReg r) {
aoqi@0 373 // Account for saved rbp, and return address
aoqi@0 374 // This should really be in_preserve_stack_slots
aoqi@0 375 return (r->reg2stack() + 2) * VMRegImpl::stack_slot_size;
aoqi@0 376 }
aoqi@0 377
aoqi@0 378 static int reg2offset_out(VMReg r) {
aoqi@0 379 return (r->reg2stack() + SharedRuntime::out_preserve_stack_slots()) * VMRegImpl::stack_slot_size;
aoqi@0 380 }
aoqi@0 381
aoqi@0 382 // ---------------------------------------------------------------------------
aoqi@0 383 // Read the array of BasicTypes from a signature, and compute where the
aoqi@0 384 // arguments should go. Values in the VMRegPair regs array refer to 4-byte
aoqi@0 385 // quantities. Values less than SharedInfo::stack0 are registers, those above
aoqi@0 386 // refer to 4-byte stack slots. All stack slots are based off of the stack pointer
aoqi@0 387 // as framesizes are fixed.
aoqi@0 388 // VMRegImpl::stack0 refers to the first slot 0(sp).
aoqi@0 389 // and VMRegImpl::stack0+1 refers to the memory word 4-byes higher. Register
aoqi@0 390 // up to RegisterImpl::number_of_registers) are the 32-bit
aoqi@0 391 // integer registers.
aoqi@0 392
aoqi@0 393 // Pass first two oop/int args in registers ECX and EDX.
aoqi@0 394 // Pass first two float/double args in registers XMM0 and XMM1.
aoqi@0 395 // Doubles have precedence, so if you pass a mix of floats and doubles
aoqi@0 396 // the doubles will grab the registers before the floats will.
aoqi@0 397
aoqi@0 398 // Note: the INPUTS in sig_bt are in units of Java argument words, which are
aoqi@0 399 // either 32-bit or 64-bit depending on the build. The OUTPUTS are in 32-bit
aoqi@0 400 // units regardless of build. Of course for i486 there is no 64 bit build
aoqi@0 401
aoqi@0 402
aoqi@0 403 // ---------------------------------------------------------------------------
aoqi@0 404 // The compiled Java calling convention.
aoqi@0 405 // Pass first two oop/int args in registers ECX and EDX.
aoqi@0 406 // Pass first two float/double args in registers XMM0 and XMM1.
aoqi@0 407 // Doubles have precedence, so if you pass a mix of floats and doubles
aoqi@0 408 // the doubles will grab the registers before the floats will.
aoqi@0 409 int SharedRuntime::java_calling_convention(const BasicType *sig_bt,
aoqi@0 410 VMRegPair *regs,
aoqi@0 411 int total_args_passed,
aoqi@0 412 int is_outgoing) {
aoqi@0 413 uint stack = 0; // Starting stack position for args on stack
aoqi@0 414
aoqi@0 415
aoqi@0 416 // Pass first two oop/int args in registers ECX and EDX.
aoqi@0 417 uint reg_arg0 = 9999;
aoqi@0 418 uint reg_arg1 = 9999;
aoqi@0 419
aoqi@0 420 // Pass first two float/double args in registers XMM0 and XMM1.
aoqi@0 421 // Doubles have precedence, so if you pass a mix of floats and doubles
aoqi@0 422 // the doubles will grab the registers before the floats will.
aoqi@0 423 // CNC - TURNED OFF FOR non-SSE.
aoqi@0 424 // On Intel we have to round all doubles (and most floats) at
aoqi@0 425 // call sites by storing to the stack in any case.
aoqi@0 426 // UseSSE=0 ==> Don't Use ==> 9999+0
aoqi@0 427 // UseSSE=1 ==> Floats only ==> 9999+1
aoqi@0 428 // UseSSE>=2 ==> Floats or doubles ==> 9999+2
aoqi@0 429 enum { fltarg_dontuse = 9999+0, fltarg_float_only = 9999+1, fltarg_flt_dbl = 9999+2 };
aoqi@0 430 uint fargs = (UseSSE>=2) ? 2 : UseSSE;
aoqi@0 431 uint freg_arg0 = 9999+fargs;
aoqi@0 432 uint freg_arg1 = 9999+fargs;
aoqi@0 433
aoqi@0 434 // Pass doubles & longs aligned on the stack. First count stack slots for doubles
aoqi@0 435 int i;
aoqi@0 436 for( i = 0; i < total_args_passed; i++) {
aoqi@0 437 if( sig_bt[i] == T_DOUBLE ) {
aoqi@0 438 // first 2 doubles go in registers
aoqi@0 439 if( freg_arg0 == fltarg_flt_dbl ) freg_arg0 = i;
aoqi@0 440 else if( freg_arg1 == fltarg_flt_dbl ) freg_arg1 = i;
aoqi@0 441 else // Else double is passed low on the stack to be aligned.
aoqi@0 442 stack += 2;
aoqi@0 443 } else if( sig_bt[i] == T_LONG ) {
aoqi@0 444 stack += 2;
aoqi@0 445 }
aoqi@0 446 }
aoqi@0 447 int dstack = 0; // Separate counter for placing doubles
aoqi@0 448
aoqi@0 449 // Now pick where all else goes.
aoqi@0 450 for( i = 0; i < total_args_passed; i++) {
aoqi@0 451 // From the type and the argument number (count) compute the location
aoqi@0 452 switch( sig_bt[i] ) {
aoqi@0 453 case T_SHORT:
aoqi@0 454 case T_CHAR:
aoqi@0 455 case T_BYTE:
aoqi@0 456 case T_BOOLEAN:
aoqi@0 457 case T_INT:
aoqi@0 458 case T_ARRAY:
aoqi@0 459 case T_OBJECT:
aoqi@0 460 case T_ADDRESS:
aoqi@0 461 if( reg_arg0 == 9999 ) {
aoqi@0 462 reg_arg0 = i;
aoqi@0 463 regs[i].set1(rcx->as_VMReg());
aoqi@0 464 } else if( reg_arg1 == 9999 ) {
aoqi@0 465 reg_arg1 = i;
aoqi@0 466 regs[i].set1(rdx->as_VMReg());
aoqi@0 467 } else {
aoqi@0 468 regs[i].set1(VMRegImpl::stack2reg(stack++));
aoqi@0 469 }
aoqi@0 470 break;
aoqi@0 471 case T_FLOAT:
aoqi@0 472 if( freg_arg0 == fltarg_flt_dbl || freg_arg0 == fltarg_float_only ) {
aoqi@0 473 freg_arg0 = i;
aoqi@0 474 regs[i].set1(xmm0->as_VMReg());
aoqi@0 475 } else if( freg_arg1 == fltarg_flt_dbl || freg_arg1 == fltarg_float_only ) {
aoqi@0 476 freg_arg1 = i;
aoqi@0 477 regs[i].set1(xmm1->as_VMReg());
aoqi@0 478 } else {
aoqi@0 479 regs[i].set1(VMRegImpl::stack2reg(stack++));
aoqi@0 480 }
aoqi@0 481 break;
aoqi@0 482 case T_LONG:
aoqi@0 483 assert(sig_bt[i+1] == T_VOID, "missing Half" );
aoqi@0 484 regs[i].set2(VMRegImpl::stack2reg(dstack));
aoqi@0 485 dstack += 2;
aoqi@0 486 break;
aoqi@0 487 case T_DOUBLE:
aoqi@0 488 assert(sig_bt[i+1] == T_VOID, "missing Half" );
aoqi@0 489 if( freg_arg0 == (uint)i ) {
aoqi@0 490 regs[i].set2(xmm0->as_VMReg());
aoqi@0 491 } else if( freg_arg1 == (uint)i ) {
aoqi@0 492 regs[i].set2(xmm1->as_VMReg());
aoqi@0 493 } else {
aoqi@0 494 regs[i].set2(VMRegImpl::stack2reg(dstack));
aoqi@0 495 dstack += 2;
aoqi@0 496 }
aoqi@0 497 break;
aoqi@0 498 case T_VOID: regs[i].set_bad(); break;
aoqi@0 499 break;
aoqi@0 500 default:
aoqi@0 501 ShouldNotReachHere();
aoqi@0 502 break;
aoqi@0 503 }
aoqi@0 504 }
aoqi@0 505
aoqi@0 506 // return value can be odd number of VMRegImpl stack slots make multiple of 2
aoqi@0 507 return round_to(stack, 2);
aoqi@0 508 }
aoqi@0 509
aoqi@0 510 // Patch the callers callsite with entry to compiled code if it exists.
aoqi@0 511 static void patch_callers_callsite(MacroAssembler *masm) {
aoqi@0 512 Label L;
aoqi@0 513 __ cmpptr(Address(rbx, in_bytes(Method::code_offset())), (int32_t)NULL_WORD);
aoqi@0 514 __ jcc(Assembler::equal, L);
aoqi@0 515 // Schedule the branch target address early.
aoqi@0 516 // Call into the VM to patch the caller, then jump to compiled callee
aoqi@0 517 // rax, isn't live so capture return address while we easily can
aoqi@0 518 __ movptr(rax, Address(rsp, 0));
aoqi@0 519 __ pusha();
aoqi@0 520 __ pushf();
aoqi@0 521
aoqi@0 522 if (UseSSE == 1) {
aoqi@0 523 __ subptr(rsp, 2*wordSize);
aoqi@0 524 __ movflt(Address(rsp, 0), xmm0);
aoqi@0 525 __ movflt(Address(rsp, wordSize), xmm1);
aoqi@0 526 }
aoqi@0 527 if (UseSSE >= 2) {
aoqi@0 528 __ subptr(rsp, 4*wordSize);
aoqi@0 529 __ movdbl(Address(rsp, 0), xmm0);
aoqi@0 530 __ movdbl(Address(rsp, 2*wordSize), xmm1);
aoqi@0 531 }
aoqi@0 532 #ifdef COMPILER2
aoqi@0 533 // C2 may leave the stack dirty if not in SSE2+ mode
aoqi@0 534 if (UseSSE >= 2) {
aoqi@0 535 __ verify_FPU(0, "c2i transition should have clean FPU stack");
aoqi@0 536 } else {
aoqi@0 537 __ empty_FPU_stack();
aoqi@0 538 }
aoqi@0 539 #endif /* COMPILER2 */
aoqi@0 540
aoqi@0 541 // VM needs caller's callsite
aoqi@0 542 __ push(rax);
aoqi@0 543 // VM needs target method
aoqi@0 544 __ push(rbx);
aoqi@0 545 __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, SharedRuntime::fixup_callers_callsite)));
aoqi@0 546 __ addptr(rsp, 2*wordSize);
aoqi@0 547
aoqi@0 548 if (UseSSE == 1) {
aoqi@0 549 __ movflt(xmm0, Address(rsp, 0));
aoqi@0 550 __ movflt(xmm1, Address(rsp, wordSize));
aoqi@0 551 __ addptr(rsp, 2*wordSize);
aoqi@0 552 }
aoqi@0 553 if (UseSSE >= 2) {
aoqi@0 554 __ movdbl(xmm0, Address(rsp, 0));
aoqi@0 555 __ movdbl(xmm1, Address(rsp, 2*wordSize));
aoqi@0 556 __ addptr(rsp, 4*wordSize);
aoqi@0 557 }
aoqi@0 558
aoqi@0 559 __ popf();
aoqi@0 560 __ popa();
aoqi@0 561 __ bind(L);
aoqi@0 562 }
aoqi@0 563
aoqi@0 564
aoqi@0 565 static void move_c2i_double(MacroAssembler *masm, XMMRegister r, int st_off) {
aoqi@0 566 int next_off = st_off - Interpreter::stackElementSize;
aoqi@0 567 __ movdbl(Address(rsp, next_off), r);
aoqi@0 568 }
aoqi@0 569
aoqi@0 570 static void gen_c2i_adapter(MacroAssembler *masm,
aoqi@0 571 int total_args_passed,
aoqi@0 572 int comp_args_on_stack,
aoqi@0 573 const BasicType *sig_bt,
aoqi@0 574 const VMRegPair *regs,
aoqi@0 575 Label& skip_fixup) {
aoqi@0 576 // Before we get into the guts of the C2I adapter, see if we should be here
aoqi@0 577 // at all. We've come from compiled code and are attempting to jump to the
aoqi@0 578 // interpreter, which means the caller made a static call to get here
aoqi@0 579 // (vcalls always get a compiled target if there is one). Check for a
aoqi@0 580 // compiled target. If there is one, we need to patch the caller's call.
aoqi@0 581 patch_callers_callsite(masm);
aoqi@0 582
aoqi@0 583 __ bind(skip_fixup);
aoqi@0 584
aoqi@0 585 #ifdef COMPILER2
aoqi@0 586 // C2 may leave the stack dirty if not in SSE2+ mode
aoqi@0 587 if (UseSSE >= 2) {
aoqi@0 588 __ verify_FPU(0, "c2i transition should have clean FPU stack");
aoqi@0 589 } else {
aoqi@0 590 __ empty_FPU_stack();
aoqi@0 591 }
aoqi@0 592 #endif /* COMPILER2 */
aoqi@0 593
aoqi@0 594 // Since all args are passed on the stack, total_args_passed * interpreter_
aoqi@0 595 // stack_element_size is the
aoqi@0 596 // space we need.
aoqi@0 597 int extraspace = total_args_passed * Interpreter::stackElementSize;
aoqi@0 598
aoqi@0 599 // Get return address
aoqi@0 600 __ pop(rax);
aoqi@0 601
aoqi@0 602 // set senderSP value
aoqi@0 603 __ movptr(rsi, rsp);
aoqi@0 604
aoqi@0 605 __ subptr(rsp, extraspace);
aoqi@0 606
aoqi@0 607 // Now write the args into the outgoing interpreter space
aoqi@0 608 for (int i = 0; i < total_args_passed; i++) {
aoqi@0 609 if (sig_bt[i] == T_VOID) {
aoqi@0 610 assert(i > 0 && (sig_bt[i-1] == T_LONG || sig_bt[i-1] == T_DOUBLE), "missing half");
aoqi@0 611 continue;
aoqi@0 612 }
aoqi@0 613
aoqi@0 614 // st_off points to lowest address on stack.
aoqi@0 615 int st_off = ((total_args_passed - 1) - i) * Interpreter::stackElementSize;
aoqi@0 616 int next_off = st_off - Interpreter::stackElementSize;
aoqi@0 617
aoqi@0 618 // Say 4 args:
aoqi@0 619 // i st_off
aoqi@0 620 // 0 12 T_LONG
aoqi@0 621 // 1 8 T_VOID
aoqi@0 622 // 2 4 T_OBJECT
aoqi@0 623 // 3 0 T_BOOL
aoqi@0 624 VMReg r_1 = regs[i].first();
aoqi@0 625 VMReg r_2 = regs[i].second();
aoqi@0 626 if (!r_1->is_valid()) {
aoqi@0 627 assert(!r_2->is_valid(), "");
aoqi@0 628 continue;
aoqi@0 629 }
aoqi@0 630
aoqi@0 631 if (r_1->is_stack()) {
aoqi@0 632 // memory to memory use fpu stack top
aoqi@0 633 int ld_off = r_1->reg2stack() * VMRegImpl::stack_slot_size + extraspace;
aoqi@0 634
aoqi@0 635 if (!r_2->is_valid()) {
aoqi@0 636 __ movl(rdi, Address(rsp, ld_off));
aoqi@0 637 __ movptr(Address(rsp, st_off), rdi);
aoqi@0 638 } else {
aoqi@0 639
aoqi@0 640 // ld_off == LSW, ld_off+VMRegImpl::stack_slot_size == MSW
aoqi@0 641 // st_off == MSW, st_off-wordSize == LSW
aoqi@0 642
aoqi@0 643 __ movptr(rdi, Address(rsp, ld_off));
aoqi@0 644 __ movptr(Address(rsp, next_off), rdi);
aoqi@0 645 #ifndef _LP64
aoqi@0 646 __ movptr(rdi, Address(rsp, ld_off + wordSize));
aoqi@0 647 __ movptr(Address(rsp, st_off), rdi);
aoqi@0 648 #else
aoqi@0 649 #ifdef ASSERT
aoqi@0 650 // Overwrite the unused slot with known junk
aoqi@0 651 __ mov64(rax, CONST64(0xdeadffffdeadaaaa));
aoqi@0 652 __ movptr(Address(rsp, st_off), rax);
aoqi@0 653 #endif /* ASSERT */
aoqi@0 654 #endif // _LP64
aoqi@0 655 }
aoqi@0 656 } else if (r_1->is_Register()) {
aoqi@0 657 Register r = r_1->as_Register();
aoqi@0 658 if (!r_2->is_valid()) {
aoqi@0 659 __ movl(Address(rsp, st_off), r);
aoqi@0 660 } else {
aoqi@0 661 // long/double in gpr
aoqi@0 662 NOT_LP64(ShouldNotReachHere());
aoqi@0 663 // Two VMRegs can be T_OBJECT, T_ADDRESS, T_DOUBLE, T_LONG
aoqi@0 664 // T_DOUBLE and T_LONG use two slots in the interpreter
aoqi@0 665 if ( sig_bt[i] == T_LONG || sig_bt[i] == T_DOUBLE) {
aoqi@0 666 // long/double in gpr
aoqi@0 667 #ifdef ASSERT
aoqi@0 668 // Overwrite the unused slot with known junk
aoqi@0 669 LP64_ONLY(__ mov64(rax, CONST64(0xdeadffffdeadaaab)));
aoqi@0 670 __ movptr(Address(rsp, st_off), rax);
aoqi@0 671 #endif /* ASSERT */
aoqi@0 672 __ movptr(Address(rsp, next_off), r);
aoqi@0 673 } else {
aoqi@0 674 __ movptr(Address(rsp, st_off), r);
aoqi@0 675 }
aoqi@0 676 }
aoqi@0 677 } else {
aoqi@0 678 assert(r_1->is_XMMRegister(), "");
aoqi@0 679 if (!r_2->is_valid()) {
aoqi@0 680 __ movflt(Address(rsp, st_off), r_1->as_XMMRegister());
aoqi@0 681 } else {
aoqi@0 682 assert(sig_bt[i] == T_DOUBLE || sig_bt[i] == T_LONG, "wrong type");
aoqi@0 683 move_c2i_double(masm, r_1->as_XMMRegister(), st_off);
aoqi@0 684 }
aoqi@0 685 }
aoqi@0 686 }
aoqi@0 687
aoqi@0 688 // Schedule the branch target address early.
aoqi@0 689 __ movptr(rcx, Address(rbx, in_bytes(Method::interpreter_entry_offset())));
aoqi@0 690 // And repush original return address
aoqi@0 691 __ push(rax);
aoqi@0 692 __ jmp(rcx);
aoqi@0 693 }
aoqi@0 694
aoqi@0 695
aoqi@0 696 static void move_i2c_double(MacroAssembler *masm, XMMRegister r, Register saved_sp, int ld_off) {
aoqi@0 697 int next_val_off = ld_off - Interpreter::stackElementSize;
aoqi@0 698 __ movdbl(r, Address(saved_sp, next_val_off));
aoqi@0 699 }
aoqi@0 700
aoqi@0 701 static void range_check(MacroAssembler* masm, Register pc_reg, Register temp_reg,
aoqi@0 702 address code_start, address code_end,
aoqi@0 703 Label& L_ok) {
aoqi@0 704 Label L_fail;
aoqi@0 705 __ lea(temp_reg, ExternalAddress(code_start));
aoqi@0 706 __ cmpptr(pc_reg, temp_reg);
aoqi@0 707 __ jcc(Assembler::belowEqual, L_fail);
aoqi@0 708 __ lea(temp_reg, ExternalAddress(code_end));
aoqi@0 709 __ cmpptr(pc_reg, temp_reg);
aoqi@0 710 __ jcc(Assembler::below, L_ok);
aoqi@0 711 __ bind(L_fail);
aoqi@0 712 }
aoqi@0 713
aoqi@0 714 static void gen_i2c_adapter(MacroAssembler *masm,
aoqi@0 715 int total_args_passed,
aoqi@0 716 int comp_args_on_stack,
aoqi@0 717 const BasicType *sig_bt,
aoqi@0 718 const VMRegPair *regs) {
aoqi@0 719
aoqi@0 720 // Note: rsi contains the senderSP on entry. We must preserve it since
aoqi@0 721 // we may do a i2c -> c2i transition if we lose a race where compiled
aoqi@0 722 // code goes non-entrant while we get args ready.
aoqi@0 723
aoqi@0 724 // Adapters can be frameless because they do not require the caller
aoqi@0 725 // to perform additional cleanup work, such as correcting the stack pointer.
aoqi@0 726 // An i2c adapter is frameless because the *caller* frame, which is interpreted,
aoqi@0 727 // routinely repairs its own stack pointer (from interpreter_frame_last_sp),
aoqi@0 728 // even if a callee has modified the stack pointer.
aoqi@0 729 // A c2i adapter is frameless because the *callee* frame, which is interpreted,
aoqi@0 730 // routinely repairs its caller's stack pointer (from sender_sp, which is set
aoqi@0 731 // up via the senderSP register).
aoqi@0 732 // In other words, if *either* the caller or callee is interpreted, we can
aoqi@0 733 // get the stack pointer repaired after a call.
aoqi@0 734 // This is why c2i and i2c adapters cannot be indefinitely composed.
aoqi@0 735 // In particular, if a c2i adapter were to somehow call an i2c adapter,
aoqi@0 736 // both caller and callee would be compiled methods, and neither would
aoqi@0 737 // clean up the stack pointer changes performed by the two adapters.
aoqi@0 738 // If this happens, control eventually transfers back to the compiled
aoqi@0 739 // caller, but with an uncorrected stack, causing delayed havoc.
aoqi@0 740
aoqi@0 741 // Pick up the return address
aoqi@0 742 __ movptr(rax, Address(rsp, 0));
aoqi@0 743
aoqi@0 744 if (VerifyAdapterCalls &&
aoqi@0 745 (Interpreter::code() != NULL || StubRoutines::code1() != NULL)) {
aoqi@0 746 // So, let's test for cascading c2i/i2c adapters right now.
aoqi@0 747 // assert(Interpreter::contains($return_addr) ||
aoqi@0 748 // StubRoutines::contains($return_addr),
aoqi@0 749 // "i2c adapter must return to an interpreter frame");
aoqi@0 750 __ block_comment("verify_i2c { ");
aoqi@0 751 Label L_ok;
aoqi@0 752 if (Interpreter::code() != NULL)
aoqi@0 753 range_check(masm, rax, rdi,
aoqi@0 754 Interpreter::code()->code_start(), Interpreter::code()->code_end(),
aoqi@0 755 L_ok);
aoqi@0 756 if (StubRoutines::code1() != NULL)
aoqi@0 757 range_check(masm, rax, rdi,
aoqi@0 758 StubRoutines::code1()->code_begin(), StubRoutines::code1()->code_end(),
aoqi@0 759 L_ok);
aoqi@0 760 if (StubRoutines::code2() != NULL)
aoqi@0 761 range_check(masm, rax, rdi,
aoqi@0 762 StubRoutines::code2()->code_begin(), StubRoutines::code2()->code_end(),
aoqi@0 763 L_ok);
aoqi@0 764 const char* msg = "i2c adapter must return to an interpreter frame";
aoqi@0 765 __ block_comment(msg);
aoqi@0 766 __ stop(msg);
aoqi@0 767 __ bind(L_ok);
aoqi@0 768 __ block_comment("} verify_i2ce ");
aoqi@0 769 }
aoqi@0 770
aoqi@0 771 // Must preserve original SP for loading incoming arguments because
aoqi@0 772 // we need to align the outgoing SP for compiled code.
aoqi@0 773 __ movptr(rdi, rsp);
aoqi@0 774
aoqi@0 775 // Cut-out for having no stack args. Since up to 2 int/oop args are passed
aoqi@0 776 // in registers, we will occasionally have no stack args.
aoqi@0 777 int comp_words_on_stack = 0;
aoqi@0 778 if (comp_args_on_stack) {
aoqi@0 779 // Sig words on the stack are greater-than VMRegImpl::stack0. Those in
aoqi@0 780 // registers are below. By subtracting stack0, we either get a negative
aoqi@0 781 // number (all values in registers) or the maximum stack slot accessed.
aoqi@0 782 // int comp_args_on_stack = VMRegImpl::reg2stack(max_arg);
aoqi@0 783 // Convert 4-byte stack slots to words.
aoqi@0 784 comp_words_on_stack = round_to(comp_args_on_stack*4, wordSize)>>LogBytesPerWord;
aoqi@0 785 // Round up to miminum stack alignment, in wordSize
aoqi@0 786 comp_words_on_stack = round_to(comp_words_on_stack, 2);
aoqi@0 787 __ subptr(rsp, comp_words_on_stack * wordSize);
aoqi@0 788 }
aoqi@0 789
aoqi@0 790 // Align the outgoing SP
aoqi@0 791 __ andptr(rsp, -(StackAlignmentInBytes));
aoqi@0 792
aoqi@0 793 // push the return address on the stack (note that pushing, rather
aoqi@0 794 // than storing it, yields the correct frame alignment for the callee)
aoqi@0 795 __ push(rax);
aoqi@0 796
aoqi@0 797 // Put saved SP in another register
aoqi@0 798 const Register saved_sp = rax;
aoqi@0 799 __ movptr(saved_sp, rdi);
aoqi@0 800
aoqi@0 801
aoqi@0 802 // Will jump to the compiled code just as if compiled code was doing it.
aoqi@0 803 // Pre-load the register-jump target early, to schedule it better.
aoqi@0 804 __ movptr(rdi, Address(rbx, in_bytes(Method::from_compiled_offset())));
aoqi@0 805
aoqi@0 806 // Now generate the shuffle code. Pick up all register args and move the
aoqi@0 807 // rest through the floating point stack top.
aoqi@0 808 for (int i = 0; i < total_args_passed; i++) {
aoqi@0 809 if (sig_bt[i] == T_VOID) {
aoqi@0 810 // Longs and doubles are passed in native word order, but misaligned
aoqi@0 811 // in the 32-bit build.
aoqi@0 812 assert(i > 0 && (sig_bt[i-1] == T_LONG || sig_bt[i-1] == T_DOUBLE), "missing half");
aoqi@0 813 continue;
aoqi@0 814 }
aoqi@0 815
aoqi@0 816 // Pick up 0, 1 or 2 words from SP+offset.
aoqi@0 817
aoqi@0 818 assert(!regs[i].second()->is_valid() || regs[i].first()->next() == regs[i].second(),
aoqi@0 819 "scrambled load targets?");
aoqi@0 820 // Load in argument order going down.
aoqi@0 821 int ld_off = (total_args_passed - i) * Interpreter::stackElementSize;
aoqi@0 822 // Point to interpreter value (vs. tag)
aoqi@0 823 int next_off = ld_off - Interpreter::stackElementSize;
aoqi@0 824 //
aoqi@0 825 //
aoqi@0 826 //
aoqi@0 827 VMReg r_1 = regs[i].first();
aoqi@0 828 VMReg r_2 = regs[i].second();
aoqi@0 829 if (!r_1->is_valid()) {
aoqi@0 830 assert(!r_2->is_valid(), "");
aoqi@0 831 continue;
aoqi@0 832 }
aoqi@0 833 if (r_1->is_stack()) {
aoqi@0 834 // Convert stack slot to an SP offset (+ wordSize to account for return address )
aoqi@0 835 int st_off = regs[i].first()->reg2stack()*VMRegImpl::stack_slot_size + wordSize;
aoqi@0 836
aoqi@0 837 // We can use rsi as a temp here because compiled code doesn't need rsi as an input
aoqi@0 838 // and if we end up going thru a c2i because of a miss a reasonable value of rsi
aoqi@0 839 // we be generated.
aoqi@0 840 if (!r_2->is_valid()) {
aoqi@0 841 // __ fld_s(Address(saved_sp, ld_off));
aoqi@0 842 // __ fstp_s(Address(rsp, st_off));
aoqi@0 843 __ movl(rsi, Address(saved_sp, ld_off));
aoqi@0 844 __ movptr(Address(rsp, st_off), rsi);
aoqi@0 845 } else {
aoqi@0 846 // Interpreter local[n] == MSW, local[n+1] == LSW however locals
aoqi@0 847 // are accessed as negative so LSW is at LOW address
aoqi@0 848
aoqi@0 849 // ld_off is MSW so get LSW
aoqi@0 850 // st_off is LSW (i.e. reg.first())
aoqi@0 851 // __ fld_d(Address(saved_sp, next_off));
aoqi@0 852 // __ fstp_d(Address(rsp, st_off));
aoqi@0 853 //
aoqi@0 854 // We are using two VMRegs. This can be either T_OBJECT, T_ADDRESS, T_LONG, or T_DOUBLE
aoqi@0 855 // the interpreter allocates two slots but only uses one for thr T_LONG or T_DOUBLE case
aoqi@0 856 // So we must adjust where to pick up the data to match the interpreter.
aoqi@0 857 //
aoqi@0 858 // Interpreter local[n] == MSW, local[n+1] == LSW however locals
aoqi@0 859 // are accessed as negative so LSW is at LOW address
aoqi@0 860
aoqi@0 861 // ld_off is MSW so get LSW
aoqi@0 862 const int offset = (NOT_LP64(true ||) sig_bt[i]==T_LONG||sig_bt[i]==T_DOUBLE)?
aoqi@0 863 next_off : ld_off;
aoqi@0 864 __ movptr(rsi, Address(saved_sp, offset));
aoqi@0 865 __ movptr(Address(rsp, st_off), rsi);
aoqi@0 866 #ifndef _LP64
aoqi@0 867 __ movptr(rsi, Address(saved_sp, ld_off));
aoqi@0 868 __ movptr(Address(rsp, st_off + wordSize), rsi);
aoqi@0 869 #endif // _LP64
aoqi@0 870 }
aoqi@0 871 } else if (r_1->is_Register()) { // Register argument
aoqi@0 872 Register r = r_1->as_Register();
aoqi@0 873 assert(r != rax, "must be different");
aoqi@0 874 if (r_2->is_valid()) {
aoqi@0 875 //
aoqi@0 876 // We are using two VMRegs. This can be either T_OBJECT, T_ADDRESS, T_LONG, or T_DOUBLE
aoqi@0 877 // the interpreter allocates two slots but only uses one for thr T_LONG or T_DOUBLE case
aoqi@0 878 // So we must adjust where to pick up the data to match the interpreter.
aoqi@0 879
aoqi@0 880 const int offset = (NOT_LP64(true ||) sig_bt[i]==T_LONG||sig_bt[i]==T_DOUBLE)?
aoqi@0 881 next_off : ld_off;
aoqi@0 882
aoqi@0 883 // this can be a misaligned move
aoqi@0 884 __ movptr(r, Address(saved_sp, offset));
aoqi@0 885 #ifndef _LP64
aoqi@0 886 assert(r_2->as_Register() != rax, "need another temporary register");
aoqi@0 887 // Remember r_1 is low address (and LSB on x86)
aoqi@0 888 // So r_2 gets loaded from high address regardless of the platform
aoqi@0 889 __ movptr(r_2->as_Register(), Address(saved_sp, ld_off));
aoqi@0 890 #endif // _LP64
aoqi@0 891 } else {
aoqi@0 892 __ movl(r, Address(saved_sp, ld_off));
aoqi@0 893 }
aoqi@0 894 } else {
aoqi@0 895 assert(r_1->is_XMMRegister(), "");
aoqi@0 896 if (!r_2->is_valid()) {
aoqi@0 897 __ movflt(r_1->as_XMMRegister(), Address(saved_sp, ld_off));
aoqi@0 898 } else {
aoqi@0 899 move_i2c_double(masm, r_1->as_XMMRegister(), saved_sp, ld_off);
aoqi@0 900 }
aoqi@0 901 }
aoqi@0 902 }
aoqi@0 903
aoqi@0 904 // 6243940 We might end up in handle_wrong_method if
aoqi@0 905 // the callee is deoptimized as we race thru here. If that
aoqi@0 906 // happens we don't want to take a safepoint because the
aoqi@0 907 // caller frame will look interpreted and arguments are now
aoqi@0 908 // "compiled" so it is much better to make this transition
aoqi@0 909 // invisible to the stack walking code. Unfortunately if
aoqi@0 910 // we try and find the callee by normal means a safepoint
aoqi@0 911 // is possible. So we stash the desired callee in the thread
aoqi@0 912 // and the vm will find there should this case occur.
aoqi@0 913
aoqi@0 914 __ get_thread(rax);
aoqi@0 915 __ movptr(Address(rax, JavaThread::callee_target_offset()), rbx);
aoqi@0 916
aoqi@0 917 // move Method* to rax, in case we end up in an c2i adapter.
aoqi@0 918 // the c2i adapters expect Method* in rax, (c2) because c2's
aoqi@0 919 // resolve stubs return the result (the method) in rax,.
aoqi@0 920 // I'd love to fix this.
aoqi@0 921 __ mov(rax, rbx);
aoqi@0 922
aoqi@0 923 __ jmp(rdi);
aoqi@0 924 }
aoqi@0 925
aoqi@0 926 // ---------------------------------------------------------------
aoqi@0 927 AdapterHandlerEntry* SharedRuntime::generate_i2c2i_adapters(MacroAssembler *masm,
aoqi@0 928 int total_args_passed,
aoqi@0 929 int comp_args_on_stack,
aoqi@0 930 const BasicType *sig_bt,
aoqi@0 931 const VMRegPair *regs,
aoqi@0 932 AdapterFingerPrint* fingerprint) {
aoqi@0 933 address i2c_entry = __ pc();
aoqi@0 934
aoqi@0 935 gen_i2c_adapter(masm, total_args_passed, comp_args_on_stack, sig_bt, regs);
aoqi@0 936
aoqi@0 937 // -------------------------------------------------------------------------
aoqi@0 938 // Generate a C2I adapter. On entry we know rbx, holds the Method* during calls
aoqi@0 939 // to the interpreter. The args start out packed in the compiled layout. They
aoqi@0 940 // need to be unpacked into the interpreter layout. This will almost always
aoqi@0 941 // require some stack space. We grow the current (compiled) stack, then repack
aoqi@0 942 // the args. We finally end in a jump to the generic interpreter entry point.
aoqi@0 943 // On exit from the interpreter, the interpreter will restore our SP (lest the
aoqi@0 944 // compiled code, which relys solely on SP and not EBP, get sick).
aoqi@0 945
aoqi@0 946 address c2i_unverified_entry = __ pc();
aoqi@0 947 Label skip_fixup;
aoqi@0 948
aoqi@0 949 Register holder = rax;
aoqi@0 950 Register receiver = rcx;
aoqi@0 951 Register temp = rbx;
aoqi@0 952
aoqi@0 953 {
aoqi@0 954
aoqi@0 955 Label missed;
aoqi@0 956 __ movptr(temp, Address(receiver, oopDesc::klass_offset_in_bytes()));
aoqi@0 957 __ cmpptr(temp, Address(holder, CompiledICHolder::holder_klass_offset()));
aoqi@0 958 __ movptr(rbx, Address(holder, CompiledICHolder::holder_method_offset()));
aoqi@0 959 __ jcc(Assembler::notEqual, missed);
aoqi@0 960 // Method might have been compiled since the call site was patched to
aoqi@0 961 // interpreted if that is the case treat it as a miss so we can get
aoqi@0 962 // the call site corrected.
aoqi@0 963 __ cmpptr(Address(rbx, in_bytes(Method::code_offset())), (int32_t)NULL_WORD);
aoqi@0 964 __ jcc(Assembler::equal, skip_fixup);
aoqi@0 965
aoqi@0 966 __ bind(missed);
aoqi@0 967 __ jump(RuntimeAddress(SharedRuntime::get_ic_miss_stub()));
aoqi@0 968 }
aoqi@0 969
aoqi@0 970 address c2i_entry = __ pc();
aoqi@0 971
aoqi@0 972 gen_c2i_adapter(masm, total_args_passed, comp_args_on_stack, sig_bt, regs, skip_fixup);
aoqi@0 973
aoqi@0 974 __ flush();
aoqi@0 975 return AdapterHandlerLibrary::new_entry(fingerprint, i2c_entry, c2i_entry, c2i_unverified_entry);
aoqi@0 976 }
aoqi@0 977
aoqi@0 978 int SharedRuntime::c_calling_convention(const BasicType *sig_bt,
aoqi@0 979 VMRegPair *regs,
aoqi@0 980 VMRegPair *regs2,
aoqi@0 981 int total_args_passed) {
aoqi@0 982 assert(regs2 == NULL, "not needed on x86");
aoqi@0 983 // We return the amount of VMRegImpl stack slots we need to reserve for all
aoqi@0 984 // the arguments NOT counting out_preserve_stack_slots.
aoqi@0 985
aoqi@0 986 uint stack = 0; // All arguments on stack
aoqi@0 987
aoqi@0 988 for( int i = 0; i < total_args_passed; i++) {
aoqi@0 989 // From the type and the argument number (count) compute the location
aoqi@0 990 switch( sig_bt[i] ) {
aoqi@0 991 case T_BOOLEAN:
aoqi@0 992 case T_CHAR:
aoqi@0 993 case T_FLOAT:
aoqi@0 994 case T_BYTE:
aoqi@0 995 case T_SHORT:
aoqi@0 996 case T_INT:
aoqi@0 997 case T_OBJECT:
aoqi@0 998 case T_ARRAY:
aoqi@0 999 case T_ADDRESS:
aoqi@0 1000 case T_METADATA:
aoqi@0 1001 regs[i].set1(VMRegImpl::stack2reg(stack++));
aoqi@0 1002 break;
aoqi@0 1003 case T_LONG:
aoqi@0 1004 case T_DOUBLE: // The stack numbering is reversed from Java
aoqi@0 1005 // Since C arguments do not get reversed, the ordering for
aoqi@0 1006 // doubles on the stack must be opposite the Java convention
aoqi@0 1007 assert(sig_bt[i+1] == T_VOID, "missing Half" );
aoqi@0 1008 regs[i].set2(VMRegImpl::stack2reg(stack));
aoqi@0 1009 stack += 2;
aoqi@0 1010 break;
aoqi@0 1011 case T_VOID: regs[i].set_bad(); break;
aoqi@0 1012 default:
aoqi@0 1013 ShouldNotReachHere();
aoqi@0 1014 break;
aoqi@0 1015 }
aoqi@0 1016 }
aoqi@0 1017 return stack;
aoqi@0 1018 }
aoqi@0 1019
aoqi@0 1020 // A simple move of integer like type
aoqi@0 1021 static void simple_move32(MacroAssembler* masm, VMRegPair src, VMRegPair dst) {
aoqi@0 1022 if (src.first()->is_stack()) {
aoqi@0 1023 if (dst.first()->is_stack()) {
aoqi@0 1024 // stack to stack
aoqi@0 1025 // __ ld(FP, reg2offset(src.first()) + STACK_BIAS, L5);
aoqi@0 1026 // __ st(L5, SP, reg2offset(dst.first()) + STACK_BIAS);
aoqi@0 1027 __ movl2ptr(rax, Address(rbp, reg2offset_in(src.first())));
aoqi@0 1028 __ movptr(Address(rsp, reg2offset_out(dst.first())), rax);
aoqi@0 1029 } else {
aoqi@0 1030 // stack to reg
aoqi@0 1031 __ movl2ptr(dst.first()->as_Register(), Address(rbp, reg2offset_in(src.first())));
aoqi@0 1032 }
aoqi@0 1033 } else if (dst.first()->is_stack()) {
aoqi@0 1034 // reg to stack
aoqi@0 1035 // no need to sign extend on 64bit
aoqi@0 1036 __ movptr(Address(rsp, reg2offset_out(dst.first())), src.first()->as_Register());
aoqi@0 1037 } else {
aoqi@0 1038 if (dst.first() != src.first()) {
aoqi@0 1039 __ mov(dst.first()->as_Register(), src.first()->as_Register());
aoqi@0 1040 }
aoqi@0 1041 }
aoqi@0 1042 }
aoqi@0 1043
aoqi@0 1044 // An oop arg. Must pass a handle not the oop itself
aoqi@0 1045 static void object_move(MacroAssembler* masm,
aoqi@0 1046 OopMap* map,
aoqi@0 1047 int oop_handle_offset,
aoqi@0 1048 int framesize_in_slots,
aoqi@0 1049 VMRegPair src,
aoqi@0 1050 VMRegPair dst,
aoqi@0 1051 bool is_receiver,
aoqi@0 1052 int* receiver_offset) {
aoqi@0 1053
aoqi@0 1054 // Because of the calling conventions we know that src can be a
aoqi@0 1055 // register or a stack location. dst can only be a stack location.
aoqi@0 1056
aoqi@0 1057 assert(dst.first()->is_stack(), "must be stack");
aoqi@0 1058 // must pass a handle. First figure out the location we use as a handle
aoqi@0 1059
aoqi@0 1060 if (src.first()->is_stack()) {
aoqi@0 1061 // Oop is already on the stack as an argument
aoqi@0 1062 Register rHandle = rax;
aoqi@0 1063 Label nil;
aoqi@0 1064 __ xorptr(rHandle, rHandle);
aoqi@0 1065 __ cmpptr(Address(rbp, reg2offset_in(src.first())), (int32_t)NULL_WORD);
aoqi@0 1066 __ jcc(Assembler::equal, nil);
aoqi@0 1067 __ lea(rHandle, Address(rbp, reg2offset_in(src.first())));
aoqi@0 1068 __ bind(nil);
aoqi@0 1069 __ movptr(Address(rsp, reg2offset_out(dst.first())), rHandle);
aoqi@0 1070
aoqi@0 1071 int offset_in_older_frame = src.first()->reg2stack() + SharedRuntime::out_preserve_stack_slots();
aoqi@0 1072 map->set_oop(VMRegImpl::stack2reg(offset_in_older_frame + framesize_in_slots));
aoqi@0 1073 if (is_receiver) {
aoqi@0 1074 *receiver_offset = (offset_in_older_frame + framesize_in_slots) * VMRegImpl::stack_slot_size;
aoqi@0 1075 }
aoqi@0 1076 } else {
aoqi@0 1077 // Oop is in an a register we must store it to the space we reserve
aoqi@0 1078 // on the stack for oop_handles
aoqi@0 1079 const Register rOop = src.first()->as_Register();
aoqi@0 1080 const Register rHandle = rax;
aoqi@0 1081 int oop_slot = (rOop == rcx ? 0 : 1) * VMRegImpl::slots_per_word + oop_handle_offset;
aoqi@0 1082 int offset = oop_slot*VMRegImpl::stack_slot_size;
aoqi@0 1083 Label skip;
aoqi@0 1084 __ movptr(Address(rsp, offset), rOop);
aoqi@0 1085 map->set_oop(VMRegImpl::stack2reg(oop_slot));
aoqi@0 1086 __ xorptr(rHandle, rHandle);
aoqi@0 1087 __ cmpptr(rOop, (int32_t)NULL_WORD);
aoqi@0 1088 __ jcc(Assembler::equal, skip);
aoqi@0 1089 __ lea(rHandle, Address(rsp, offset));
aoqi@0 1090 __ bind(skip);
aoqi@0 1091 // Store the handle parameter
aoqi@0 1092 __ movptr(Address(rsp, reg2offset_out(dst.first())), rHandle);
aoqi@0 1093 if (is_receiver) {
aoqi@0 1094 *receiver_offset = offset;
aoqi@0 1095 }
aoqi@0 1096 }
aoqi@0 1097 }
aoqi@0 1098
aoqi@0 1099 // A float arg may have to do float reg int reg conversion
aoqi@0 1100 static void float_move(MacroAssembler* masm, VMRegPair src, VMRegPair dst) {
aoqi@0 1101 assert(!src.second()->is_valid() && !dst.second()->is_valid(), "bad float_move");
aoqi@0 1102
aoqi@0 1103 // Because of the calling convention we know that src is either a stack location
aoqi@0 1104 // or an xmm register. dst can only be a stack location.
aoqi@0 1105
aoqi@0 1106 assert(dst.first()->is_stack() && ( src.first()->is_stack() || src.first()->is_XMMRegister()), "bad parameters");
aoqi@0 1107
aoqi@0 1108 if (src.first()->is_stack()) {
aoqi@0 1109 __ movl(rax, Address(rbp, reg2offset_in(src.first())));
aoqi@0 1110 __ movptr(Address(rsp, reg2offset_out(dst.first())), rax);
aoqi@0 1111 } else {
aoqi@0 1112 // reg to stack
aoqi@0 1113 __ movflt(Address(rsp, reg2offset_out(dst.first())), src.first()->as_XMMRegister());
aoqi@0 1114 }
aoqi@0 1115 }
aoqi@0 1116
aoqi@0 1117 // A long move
aoqi@0 1118 static void long_move(MacroAssembler* masm, VMRegPair src, VMRegPair dst) {
aoqi@0 1119
aoqi@0 1120 // The only legal possibility for a long_move VMRegPair is:
aoqi@0 1121 // 1: two stack slots (possibly unaligned)
aoqi@0 1122 // as neither the java or C calling convention will use registers
aoqi@0 1123 // for longs.
aoqi@0 1124
aoqi@0 1125 if (src.first()->is_stack() && dst.first()->is_stack()) {
aoqi@0 1126 assert(src.second()->is_stack() && dst.second()->is_stack(), "must be all stack");
aoqi@0 1127 __ movptr(rax, Address(rbp, reg2offset_in(src.first())));
aoqi@0 1128 NOT_LP64(__ movptr(rbx, Address(rbp, reg2offset_in(src.second()))));
aoqi@0 1129 __ movptr(Address(rsp, reg2offset_out(dst.first())), rax);
aoqi@0 1130 NOT_LP64(__ movptr(Address(rsp, reg2offset_out(dst.second())), rbx));
aoqi@0 1131 } else {
aoqi@0 1132 ShouldNotReachHere();
aoqi@0 1133 }
aoqi@0 1134 }
aoqi@0 1135
aoqi@0 1136 // A double move
aoqi@0 1137 static void double_move(MacroAssembler* masm, VMRegPair src, VMRegPair dst) {
aoqi@0 1138
aoqi@0 1139 // The only legal possibilities for a double_move VMRegPair are:
aoqi@0 1140 // The painful thing here is that like long_move a VMRegPair might be
aoqi@0 1141
aoqi@0 1142 // Because of the calling convention we know that src is either
aoqi@0 1143 // 1: a single physical register (xmm registers only)
aoqi@0 1144 // 2: two stack slots (possibly unaligned)
aoqi@0 1145 // dst can only be a pair of stack slots.
aoqi@0 1146
aoqi@0 1147 assert(dst.first()->is_stack() && (src.first()->is_XMMRegister() || src.first()->is_stack()), "bad args");
aoqi@0 1148
aoqi@0 1149 if (src.first()->is_stack()) {
aoqi@0 1150 // source is all stack
aoqi@0 1151 __ movptr(rax, Address(rbp, reg2offset_in(src.first())));
aoqi@0 1152 NOT_LP64(__ movptr(rbx, Address(rbp, reg2offset_in(src.second()))));
aoqi@0 1153 __ movptr(Address(rsp, reg2offset_out(dst.first())), rax);
aoqi@0 1154 NOT_LP64(__ movptr(Address(rsp, reg2offset_out(dst.second())), rbx));
aoqi@0 1155 } else {
aoqi@0 1156 // reg to stack
aoqi@0 1157 // No worries about stack alignment
aoqi@0 1158 __ movdbl(Address(rsp, reg2offset_out(dst.first())), src.first()->as_XMMRegister());
aoqi@0 1159 }
aoqi@0 1160 }
aoqi@0 1161
aoqi@0 1162
aoqi@0 1163 void SharedRuntime::save_native_result(MacroAssembler *masm, BasicType ret_type, int frame_slots) {
aoqi@0 1164 // We always ignore the frame_slots arg and just use the space just below frame pointer
aoqi@0 1165 // which by this time is free to use
aoqi@0 1166 switch (ret_type) {
aoqi@0 1167 case T_FLOAT:
aoqi@0 1168 __ fstp_s(Address(rbp, -wordSize));
aoqi@0 1169 break;
aoqi@0 1170 case T_DOUBLE:
aoqi@0 1171 __ fstp_d(Address(rbp, -2*wordSize));
aoqi@0 1172 break;
aoqi@0 1173 case T_VOID: break;
aoqi@0 1174 case T_LONG:
aoqi@0 1175 __ movptr(Address(rbp, -wordSize), rax);
aoqi@0 1176 NOT_LP64(__ movptr(Address(rbp, -2*wordSize), rdx));
aoqi@0 1177 break;
aoqi@0 1178 default: {
aoqi@0 1179 __ movptr(Address(rbp, -wordSize), rax);
aoqi@0 1180 }
aoqi@0 1181 }
aoqi@0 1182 }
aoqi@0 1183
aoqi@0 1184 void SharedRuntime::restore_native_result(MacroAssembler *masm, BasicType ret_type, int frame_slots) {
aoqi@0 1185 // We always ignore the frame_slots arg and just use the space just below frame pointer
aoqi@0 1186 // which by this time is free to use
aoqi@0 1187 switch (ret_type) {
aoqi@0 1188 case T_FLOAT:
aoqi@0 1189 __ fld_s(Address(rbp, -wordSize));
aoqi@0 1190 break;
aoqi@0 1191 case T_DOUBLE:
aoqi@0 1192 __ fld_d(Address(rbp, -2*wordSize));
aoqi@0 1193 break;
aoqi@0 1194 case T_LONG:
aoqi@0 1195 __ movptr(rax, Address(rbp, -wordSize));
aoqi@0 1196 NOT_LP64(__ movptr(rdx, Address(rbp, -2*wordSize)));
aoqi@0 1197 break;
aoqi@0 1198 case T_VOID: break;
aoqi@0 1199 default: {
aoqi@0 1200 __ movptr(rax, Address(rbp, -wordSize));
aoqi@0 1201 }
aoqi@0 1202 }
aoqi@0 1203 }
aoqi@0 1204
aoqi@0 1205
aoqi@0 1206 static void save_or_restore_arguments(MacroAssembler* masm,
aoqi@0 1207 const int stack_slots,
aoqi@0 1208 const int total_in_args,
aoqi@0 1209 const int arg_save_area,
aoqi@0 1210 OopMap* map,
aoqi@0 1211 VMRegPair* in_regs,
aoqi@0 1212 BasicType* in_sig_bt) {
aoqi@0 1213 // if map is non-NULL then the code should store the values,
aoqi@0 1214 // otherwise it should load them.
aoqi@0 1215 int handle_index = 0;
aoqi@0 1216 // Save down double word first
aoqi@0 1217 for ( int i = 0; i < total_in_args; i++) {
aoqi@0 1218 if (in_regs[i].first()->is_XMMRegister() && in_sig_bt[i] == T_DOUBLE) {
aoqi@0 1219 int slot = handle_index * VMRegImpl::slots_per_word + arg_save_area;
aoqi@0 1220 int offset = slot * VMRegImpl::stack_slot_size;
aoqi@0 1221 handle_index += 2;
aoqi@0 1222 assert(handle_index <= stack_slots, "overflow");
aoqi@0 1223 if (map != NULL) {
aoqi@0 1224 __ movdbl(Address(rsp, offset), in_regs[i].first()->as_XMMRegister());
aoqi@0 1225 } else {
aoqi@0 1226 __ movdbl(in_regs[i].first()->as_XMMRegister(), Address(rsp, offset));
aoqi@0 1227 }
aoqi@0 1228 }
aoqi@0 1229 if (in_regs[i].first()->is_Register() && in_sig_bt[i] == T_LONG) {
aoqi@0 1230 int slot = handle_index * VMRegImpl::slots_per_word + arg_save_area;
aoqi@0 1231 int offset = slot * VMRegImpl::stack_slot_size;
aoqi@0 1232 handle_index += 2;
aoqi@0 1233 assert(handle_index <= stack_slots, "overflow");
aoqi@0 1234 if (map != NULL) {
aoqi@0 1235 __ movl(Address(rsp, offset), in_regs[i].first()->as_Register());
aoqi@0 1236 if (in_regs[i].second()->is_Register()) {
aoqi@0 1237 __ movl(Address(rsp, offset + 4), in_regs[i].second()->as_Register());
aoqi@0 1238 }
aoqi@0 1239 } else {
aoqi@0 1240 __ movl(in_regs[i].first()->as_Register(), Address(rsp, offset));
aoqi@0 1241 if (in_regs[i].second()->is_Register()) {
aoqi@0 1242 __ movl(in_regs[i].second()->as_Register(), Address(rsp, offset + 4));
aoqi@0 1243 }
aoqi@0 1244 }
aoqi@0 1245 }
aoqi@0 1246 }
aoqi@0 1247 // Save or restore single word registers
aoqi@0 1248 for ( int i = 0; i < total_in_args; i++) {
aoqi@0 1249 if (in_regs[i].first()->is_Register()) {
aoqi@0 1250 int slot = handle_index++ * VMRegImpl::slots_per_word + arg_save_area;
aoqi@0 1251 int offset = slot * VMRegImpl::stack_slot_size;
aoqi@0 1252 assert(handle_index <= stack_slots, "overflow");
aoqi@0 1253 if (in_sig_bt[i] == T_ARRAY && map != NULL) {
aoqi@0 1254 map->set_oop(VMRegImpl::stack2reg(slot));;
aoqi@0 1255 }
aoqi@0 1256
aoqi@0 1257 // Value is in an input register pass we must flush it to the stack
aoqi@0 1258 const Register reg = in_regs[i].first()->as_Register();
aoqi@0 1259 switch (in_sig_bt[i]) {
aoqi@0 1260 case T_ARRAY:
aoqi@0 1261 if (map != NULL) {
aoqi@0 1262 __ movptr(Address(rsp, offset), reg);
aoqi@0 1263 } else {
aoqi@0 1264 __ movptr(reg, Address(rsp, offset));
aoqi@0 1265 }
aoqi@0 1266 break;
aoqi@0 1267 case T_BOOLEAN:
aoqi@0 1268 case T_CHAR:
aoqi@0 1269 case T_BYTE:
aoqi@0 1270 case T_SHORT:
aoqi@0 1271 case T_INT:
aoqi@0 1272 if (map != NULL) {
aoqi@0 1273 __ movl(Address(rsp, offset), reg);
aoqi@0 1274 } else {
aoqi@0 1275 __ movl(reg, Address(rsp, offset));
aoqi@0 1276 }
aoqi@0 1277 break;
aoqi@0 1278 case T_OBJECT:
aoqi@0 1279 default: ShouldNotReachHere();
aoqi@0 1280 }
aoqi@0 1281 } else if (in_regs[i].first()->is_XMMRegister()) {
aoqi@0 1282 if (in_sig_bt[i] == T_FLOAT) {
aoqi@0 1283 int slot = handle_index++ * VMRegImpl::slots_per_word + arg_save_area;
aoqi@0 1284 int offset = slot * VMRegImpl::stack_slot_size;
aoqi@0 1285 assert(handle_index <= stack_slots, "overflow");
aoqi@0 1286 if (map != NULL) {
aoqi@0 1287 __ movflt(Address(rsp, offset), in_regs[i].first()->as_XMMRegister());
aoqi@0 1288 } else {
aoqi@0 1289 __ movflt(in_regs[i].first()->as_XMMRegister(), Address(rsp, offset));
aoqi@0 1290 }
aoqi@0 1291 }
aoqi@0 1292 } else if (in_regs[i].first()->is_stack()) {
aoqi@0 1293 if (in_sig_bt[i] == T_ARRAY && map != NULL) {
aoqi@0 1294 int offset_in_older_frame = in_regs[i].first()->reg2stack() + SharedRuntime::out_preserve_stack_slots();
aoqi@0 1295 map->set_oop(VMRegImpl::stack2reg(offset_in_older_frame + stack_slots));
aoqi@0 1296 }
aoqi@0 1297 }
aoqi@0 1298 }
aoqi@0 1299 }
aoqi@0 1300
aoqi@0 1301 // Check GC_locker::needs_gc and enter the runtime if it's true. This
aoqi@0 1302 // keeps a new JNI critical region from starting until a GC has been
aoqi@0 1303 // forced. Save down any oops in registers and describe them in an
aoqi@0 1304 // OopMap.
aoqi@0 1305 static void check_needs_gc_for_critical_native(MacroAssembler* masm,
aoqi@0 1306 Register thread,
aoqi@0 1307 int stack_slots,
aoqi@0 1308 int total_c_args,
aoqi@0 1309 int total_in_args,
aoqi@0 1310 int arg_save_area,
aoqi@0 1311 OopMapSet* oop_maps,
aoqi@0 1312 VMRegPair* in_regs,
aoqi@0 1313 BasicType* in_sig_bt) {
aoqi@0 1314 __ block_comment("check GC_locker::needs_gc");
aoqi@0 1315 Label cont;
aoqi@0 1316 __ cmp8(ExternalAddress((address)GC_locker::needs_gc_address()), false);
aoqi@0 1317 __ jcc(Assembler::equal, cont);
aoqi@0 1318
aoqi@0 1319 // Save down any incoming oops and call into the runtime to halt for a GC
aoqi@0 1320
aoqi@0 1321 OopMap* map = new OopMap(stack_slots * 2, 0 /* arg_slots*/);
aoqi@0 1322
aoqi@0 1323 save_or_restore_arguments(masm, stack_slots, total_in_args,
aoqi@0 1324 arg_save_area, map, in_regs, in_sig_bt);
aoqi@0 1325
aoqi@0 1326 address the_pc = __ pc();
aoqi@0 1327 oop_maps->add_gc_map( __ offset(), map);
aoqi@0 1328 __ set_last_Java_frame(thread, rsp, noreg, the_pc);
aoqi@0 1329
aoqi@0 1330 __ block_comment("block_for_jni_critical");
aoqi@0 1331 __ push(thread);
aoqi@0 1332 __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, SharedRuntime::block_for_jni_critical)));
aoqi@0 1333 __ increment(rsp, wordSize);
aoqi@0 1334
aoqi@0 1335 __ get_thread(thread);
aoqi@0 1336 __ reset_last_Java_frame(thread, false, true);
aoqi@0 1337
aoqi@0 1338 save_or_restore_arguments(masm, stack_slots, total_in_args,
aoqi@0 1339 arg_save_area, NULL, in_regs, in_sig_bt);
aoqi@0 1340
aoqi@0 1341 __ bind(cont);
aoqi@0 1342 #ifdef ASSERT
aoqi@0 1343 if (StressCriticalJNINatives) {
aoqi@0 1344 // Stress register saving
aoqi@0 1345 OopMap* map = new OopMap(stack_slots * 2, 0 /* arg_slots*/);
aoqi@0 1346 save_or_restore_arguments(masm, stack_slots, total_in_args,
aoqi@0 1347 arg_save_area, map, in_regs, in_sig_bt);
aoqi@0 1348 // Destroy argument registers
aoqi@0 1349 for (int i = 0; i < total_in_args - 1; i++) {
aoqi@0 1350 if (in_regs[i].first()->is_Register()) {
aoqi@0 1351 const Register reg = in_regs[i].first()->as_Register();
aoqi@0 1352 __ xorptr(reg, reg);
aoqi@0 1353 } else if (in_regs[i].first()->is_XMMRegister()) {
aoqi@0 1354 __ xorpd(in_regs[i].first()->as_XMMRegister(), in_regs[i].first()->as_XMMRegister());
aoqi@0 1355 } else if (in_regs[i].first()->is_FloatRegister()) {
aoqi@0 1356 ShouldNotReachHere();
aoqi@0 1357 } else if (in_regs[i].first()->is_stack()) {
aoqi@0 1358 // Nothing to do
aoqi@0 1359 } else {
aoqi@0 1360 ShouldNotReachHere();
aoqi@0 1361 }
aoqi@0 1362 if (in_sig_bt[i] == T_LONG || in_sig_bt[i] == T_DOUBLE) {
aoqi@0 1363 i++;
aoqi@0 1364 }
aoqi@0 1365 }
aoqi@0 1366
aoqi@0 1367 save_or_restore_arguments(masm, stack_slots, total_in_args,
aoqi@0 1368 arg_save_area, NULL, in_regs, in_sig_bt);
aoqi@0 1369 }
aoqi@0 1370 #endif
aoqi@0 1371 }
aoqi@0 1372
aoqi@0 1373 // Unpack an array argument into a pointer to the body and the length
aoqi@0 1374 // if the array is non-null, otherwise pass 0 for both.
aoqi@0 1375 static void unpack_array_argument(MacroAssembler* masm, VMRegPair reg, BasicType in_elem_type, VMRegPair body_arg, VMRegPair length_arg) {
aoqi@0 1376 Register tmp_reg = rax;
aoqi@0 1377 assert(!body_arg.first()->is_Register() || body_arg.first()->as_Register() != tmp_reg,
aoqi@0 1378 "possible collision");
aoqi@0 1379 assert(!length_arg.first()->is_Register() || length_arg.first()->as_Register() != tmp_reg,
aoqi@0 1380 "possible collision");
aoqi@0 1381
aoqi@0 1382 // Pass the length, ptr pair
aoqi@0 1383 Label is_null, done;
aoqi@0 1384 VMRegPair tmp(tmp_reg->as_VMReg());
aoqi@0 1385 if (reg.first()->is_stack()) {
aoqi@0 1386 // Load the arg up from the stack
aoqi@0 1387 simple_move32(masm, reg, tmp);
aoqi@0 1388 reg = tmp;
aoqi@0 1389 }
aoqi@0 1390 __ testptr(reg.first()->as_Register(), reg.first()->as_Register());
aoqi@0 1391 __ jccb(Assembler::equal, is_null);
aoqi@0 1392 __ lea(tmp_reg, Address(reg.first()->as_Register(), arrayOopDesc::base_offset_in_bytes(in_elem_type)));
aoqi@0 1393 simple_move32(masm, tmp, body_arg);
aoqi@0 1394 // load the length relative to the body.
aoqi@0 1395 __ movl(tmp_reg, Address(tmp_reg, arrayOopDesc::length_offset_in_bytes() -
aoqi@0 1396 arrayOopDesc::base_offset_in_bytes(in_elem_type)));
aoqi@0 1397 simple_move32(masm, tmp, length_arg);
aoqi@0 1398 __ jmpb(done);
aoqi@0 1399 __ bind(is_null);
aoqi@0 1400 // Pass zeros
aoqi@0 1401 __ xorptr(tmp_reg, tmp_reg);
aoqi@0 1402 simple_move32(masm, tmp, body_arg);
aoqi@0 1403 simple_move32(masm, tmp, length_arg);
aoqi@0 1404 __ bind(done);
aoqi@0 1405 }
aoqi@0 1406
aoqi@0 1407 static void verify_oop_args(MacroAssembler* masm,
aoqi@0 1408 methodHandle method,
aoqi@0 1409 const BasicType* sig_bt,
aoqi@0 1410 const VMRegPair* regs) {
aoqi@0 1411 Register temp_reg = rbx; // not part of any compiled calling seq
aoqi@0 1412 if (VerifyOops) {
aoqi@0 1413 for (int i = 0; i < method->size_of_parameters(); i++) {
aoqi@0 1414 if (sig_bt[i] == T_OBJECT ||
aoqi@0 1415 sig_bt[i] == T_ARRAY) {
aoqi@0 1416 VMReg r = regs[i].first();
aoqi@0 1417 assert(r->is_valid(), "bad oop arg");
aoqi@0 1418 if (r->is_stack()) {
aoqi@0 1419 __ movptr(temp_reg, Address(rsp, r->reg2stack() * VMRegImpl::stack_slot_size + wordSize));
aoqi@0 1420 __ verify_oop(temp_reg);
aoqi@0 1421 } else {
aoqi@0 1422 __ verify_oop(r->as_Register());
aoqi@0 1423 }
aoqi@0 1424 }
aoqi@0 1425 }
aoqi@0 1426 }
aoqi@0 1427 }
aoqi@0 1428
aoqi@0 1429 static void gen_special_dispatch(MacroAssembler* masm,
aoqi@0 1430 methodHandle method,
aoqi@0 1431 const BasicType* sig_bt,
aoqi@0 1432 const VMRegPair* regs) {
aoqi@0 1433 verify_oop_args(masm, method, sig_bt, regs);
aoqi@0 1434 vmIntrinsics::ID iid = method->intrinsic_id();
aoqi@0 1435
aoqi@0 1436 // Now write the args into the outgoing interpreter space
aoqi@0 1437 bool has_receiver = false;
aoqi@0 1438 Register receiver_reg = noreg;
aoqi@0 1439 int member_arg_pos = -1;
aoqi@0 1440 Register member_reg = noreg;
aoqi@0 1441 int ref_kind = MethodHandles::signature_polymorphic_intrinsic_ref_kind(iid);
aoqi@0 1442 if (ref_kind != 0) {
aoqi@0 1443 member_arg_pos = method->size_of_parameters() - 1; // trailing MemberName argument
aoqi@0 1444 member_reg = rbx; // known to be free at this point
aoqi@0 1445 has_receiver = MethodHandles::ref_kind_has_receiver(ref_kind);
aoqi@0 1446 } else if (iid == vmIntrinsics::_invokeBasic) {
aoqi@0 1447 has_receiver = true;
aoqi@0 1448 } else {
aoqi@0 1449 fatal(err_msg_res("unexpected intrinsic id %d", iid));
aoqi@0 1450 }
aoqi@0 1451
aoqi@0 1452 if (member_reg != noreg) {
aoqi@0 1453 // Load the member_arg into register, if necessary.
aoqi@0 1454 SharedRuntime::check_member_name_argument_is_last_argument(method, sig_bt, regs);
aoqi@0 1455 VMReg r = regs[member_arg_pos].first();
aoqi@0 1456 if (r->is_stack()) {
aoqi@0 1457 __ movptr(member_reg, Address(rsp, r->reg2stack() * VMRegImpl::stack_slot_size + wordSize));
aoqi@0 1458 } else {
aoqi@0 1459 // no data motion is needed
aoqi@0 1460 member_reg = r->as_Register();
aoqi@0 1461 }
aoqi@0 1462 }
aoqi@0 1463
aoqi@0 1464 if (has_receiver) {
aoqi@0 1465 // Make sure the receiver is loaded into a register.
aoqi@0 1466 assert(method->size_of_parameters() > 0, "oob");
aoqi@0 1467 assert(sig_bt[0] == T_OBJECT, "receiver argument must be an object");
aoqi@0 1468 VMReg r = regs[0].first();
aoqi@0 1469 assert(r->is_valid(), "bad receiver arg");
aoqi@0 1470 if (r->is_stack()) {
aoqi@0 1471 // Porting note: This assumes that compiled calling conventions always
aoqi@0 1472 // pass the receiver oop in a register. If this is not true on some
aoqi@0 1473 // platform, pick a temp and load the receiver from stack.
aoqi@0 1474 fatal("receiver always in a register");
aoqi@0 1475 receiver_reg = rcx; // known to be free at this point
aoqi@0 1476 __ movptr(receiver_reg, Address(rsp, r->reg2stack() * VMRegImpl::stack_slot_size + wordSize));
aoqi@0 1477 } else {
aoqi@0 1478 // no data motion is needed
aoqi@0 1479 receiver_reg = r->as_Register();
aoqi@0 1480 }
aoqi@0 1481 }
aoqi@0 1482
aoqi@0 1483 // Figure out which address we are really jumping to:
aoqi@0 1484 MethodHandles::generate_method_handle_dispatch(masm, iid,
aoqi@0 1485 receiver_reg, member_reg, /*for_compiler_entry:*/ true);
aoqi@0 1486 }
aoqi@0 1487
aoqi@0 1488 // ---------------------------------------------------------------------------
aoqi@0 1489 // Generate a native wrapper for a given method. The method takes arguments
aoqi@0 1490 // in the Java compiled code convention, marshals them to the native
aoqi@0 1491 // convention (handlizes oops, etc), transitions to native, makes the call,
aoqi@0 1492 // returns to java state (possibly blocking), unhandlizes any result and
aoqi@0 1493 // returns.
aoqi@0 1494 //
aoqi@0 1495 // Critical native functions are a shorthand for the use of
aoqi@0 1496 // GetPrimtiveArrayCritical and disallow the use of any other JNI
aoqi@0 1497 // functions. The wrapper is expected to unpack the arguments before
aoqi@0 1498 // passing them to the callee and perform checks before and after the
aoqi@0 1499 // native call to ensure that they GC_locker
aoqi@0 1500 // lock_critical/unlock_critical semantics are followed. Some other
aoqi@0 1501 // parts of JNI setup are skipped like the tear down of the JNI handle
aoqi@0 1502 // block and the check for pending exceptions it's impossible for them
aoqi@0 1503 // to be thrown.
aoqi@0 1504 //
aoqi@0 1505 // They are roughly structured like this:
aoqi@0 1506 // if (GC_locker::needs_gc())
aoqi@0 1507 // SharedRuntime::block_for_jni_critical();
aoqi@0 1508 // tranistion to thread_in_native
aoqi@0 1509 // unpack arrray arguments and call native entry point
aoqi@0 1510 // check for safepoint in progress
aoqi@0 1511 // check if any thread suspend flags are set
aoqi@0 1512 // call into JVM and possible unlock the JNI critical
aoqi@0 1513 // if a GC was suppressed while in the critical native.
aoqi@0 1514 // transition back to thread_in_Java
aoqi@0 1515 // return to caller
aoqi@0 1516 //
aoqi@0 1517 nmethod* SharedRuntime::generate_native_wrapper(MacroAssembler* masm,
aoqi@0 1518 methodHandle method,
aoqi@0 1519 int compile_id,
aoqi@0 1520 BasicType* in_sig_bt,
aoqi@0 1521 VMRegPair* in_regs,
aoqi@0 1522 BasicType ret_type) {
aoqi@0 1523 if (method->is_method_handle_intrinsic()) {
aoqi@0 1524 vmIntrinsics::ID iid = method->intrinsic_id();
aoqi@0 1525 intptr_t start = (intptr_t)__ pc();
aoqi@0 1526 int vep_offset = ((intptr_t)__ pc()) - start;
aoqi@0 1527 gen_special_dispatch(masm,
aoqi@0 1528 method,
aoqi@0 1529 in_sig_bt,
aoqi@0 1530 in_regs);
aoqi@0 1531 int frame_complete = ((intptr_t)__ pc()) - start; // not complete, period
aoqi@0 1532 __ flush();
aoqi@0 1533 int stack_slots = SharedRuntime::out_preserve_stack_slots(); // no out slots at all, actually
aoqi@0 1534 return nmethod::new_native_nmethod(method,
aoqi@0 1535 compile_id,
aoqi@0 1536 masm->code(),
aoqi@0 1537 vep_offset,
aoqi@0 1538 frame_complete,
aoqi@0 1539 stack_slots / VMRegImpl::slots_per_word,
aoqi@0 1540 in_ByteSize(-1),
aoqi@0 1541 in_ByteSize(-1),
aoqi@0 1542 (OopMapSet*)NULL);
aoqi@0 1543 }
aoqi@0 1544 bool is_critical_native = true;
aoqi@0 1545 address native_func = method->critical_native_function();
aoqi@0 1546 if (native_func == NULL) {
aoqi@0 1547 native_func = method->native_function();
aoqi@0 1548 is_critical_native = false;
aoqi@0 1549 }
aoqi@0 1550 assert(native_func != NULL, "must have function");
aoqi@0 1551
aoqi@0 1552 // An OopMap for lock (and class if static)
aoqi@0 1553 OopMapSet *oop_maps = new OopMapSet();
aoqi@0 1554
aoqi@0 1555 // We have received a description of where all the java arg are located
aoqi@0 1556 // on entry to the wrapper. We need to convert these args to where
aoqi@0 1557 // the jni function will expect them. To figure out where they go
aoqi@0 1558 // we convert the java signature to a C signature by inserting
aoqi@0 1559 // the hidden arguments as arg[0] and possibly arg[1] (static method)
aoqi@0 1560
aoqi@0 1561 const int total_in_args = method->size_of_parameters();
aoqi@0 1562 int total_c_args = total_in_args;
aoqi@0 1563 if (!is_critical_native) {
aoqi@0 1564 total_c_args += 1;
aoqi@0 1565 if (method->is_static()) {
aoqi@0 1566 total_c_args++;
aoqi@0 1567 }
aoqi@0 1568 } else {
aoqi@0 1569 for (int i = 0; i < total_in_args; i++) {
aoqi@0 1570 if (in_sig_bt[i] == T_ARRAY) {
aoqi@0 1571 total_c_args++;
aoqi@0 1572 }
aoqi@0 1573 }
aoqi@0 1574 }
aoqi@0 1575
aoqi@0 1576 BasicType* out_sig_bt = NEW_RESOURCE_ARRAY(BasicType, total_c_args);
aoqi@0 1577 VMRegPair* out_regs = NEW_RESOURCE_ARRAY(VMRegPair, total_c_args);
aoqi@0 1578 BasicType* in_elem_bt = NULL;
aoqi@0 1579
aoqi@0 1580 int argc = 0;
aoqi@0 1581 if (!is_critical_native) {
aoqi@0 1582 out_sig_bt[argc++] = T_ADDRESS;
aoqi@0 1583 if (method->is_static()) {
aoqi@0 1584 out_sig_bt[argc++] = T_OBJECT;
aoqi@0 1585 }
aoqi@0 1586
aoqi@0 1587 for (int i = 0; i < total_in_args ; i++ ) {
aoqi@0 1588 out_sig_bt[argc++] = in_sig_bt[i];
aoqi@0 1589 }
aoqi@0 1590 } else {
aoqi@0 1591 Thread* THREAD = Thread::current();
aoqi@0 1592 in_elem_bt = NEW_RESOURCE_ARRAY(BasicType, total_in_args);
aoqi@0 1593 SignatureStream ss(method->signature());
aoqi@0 1594 for (int i = 0; i < total_in_args ; i++ ) {
aoqi@0 1595 if (in_sig_bt[i] == T_ARRAY) {
aoqi@0 1596 // Arrays are passed as int, elem* pair
aoqi@0 1597 out_sig_bt[argc++] = T_INT;
aoqi@0 1598 out_sig_bt[argc++] = T_ADDRESS;
aoqi@0 1599 Symbol* atype = ss.as_symbol(CHECK_NULL);
aoqi@0 1600 const char* at = atype->as_C_string();
aoqi@0 1601 if (strlen(at) == 2) {
aoqi@0 1602 assert(at[0] == '[', "must be");
aoqi@0 1603 switch (at[1]) {
aoqi@0 1604 case 'B': in_elem_bt[i] = T_BYTE; break;
aoqi@0 1605 case 'C': in_elem_bt[i] = T_CHAR; break;
aoqi@0 1606 case 'D': in_elem_bt[i] = T_DOUBLE; break;
aoqi@0 1607 case 'F': in_elem_bt[i] = T_FLOAT; break;
aoqi@0 1608 case 'I': in_elem_bt[i] = T_INT; break;
aoqi@0 1609 case 'J': in_elem_bt[i] = T_LONG; break;
aoqi@0 1610 case 'S': in_elem_bt[i] = T_SHORT; break;
aoqi@0 1611 case 'Z': in_elem_bt[i] = T_BOOLEAN; break;
aoqi@0 1612 default: ShouldNotReachHere();
aoqi@0 1613 }
aoqi@0 1614 }
aoqi@0 1615 } else {
aoqi@0 1616 out_sig_bt[argc++] = in_sig_bt[i];
aoqi@0 1617 in_elem_bt[i] = T_VOID;
aoqi@0 1618 }
aoqi@0 1619 if (in_sig_bt[i] != T_VOID) {
aoqi@0 1620 assert(in_sig_bt[i] == ss.type(), "must match");
aoqi@0 1621 ss.next();
aoqi@0 1622 }
aoqi@0 1623 }
aoqi@0 1624 }
aoqi@0 1625
aoqi@0 1626 // Now figure out where the args must be stored and how much stack space
aoqi@0 1627 // they require.
aoqi@0 1628 int out_arg_slots;
aoqi@0 1629 out_arg_slots = c_calling_convention(out_sig_bt, out_regs, NULL, total_c_args);
aoqi@0 1630
aoqi@0 1631 // Compute framesize for the wrapper. We need to handlize all oops in
aoqi@0 1632 // registers a max of 2 on x86.
aoqi@0 1633
aoqi@0 1634 // Calculate the total number of stack slots we will need.
aoqi@0 1635
aoqi@0 1636 // First count the abi requirement plus all of the outgoing args
aoqi@0 1637 int stack_slots = SharedRuntime::out_preserve_stack_slots() + out_arg_slots;
aoqi@0 1638
aoqi@0 1639 // Now the space for the inbound oop handle area
aoqi@0 1640 int total_save_slots = 2 * VMRegImpl::slots_per_word; // 2 arguments passed in registers
aoqi@0 1641 if (is_critical_native) {
aoqi@0 1642 // Critical natives may have to call out so they need a save area
aoqi@0 1643 // for register arguments.
aoqi@0 1644 int double_slots = 0;
aoqi@0 1645 int single_slots = 0;
aoqi@0 1646 for ( int i = 0; i < total_in_args; i++) {
aoqi@0 1647 if (in_regs[i].first()->is_Register()) {
aoqi@0 1648 const Register reg = in_regs[i].first()->as_Register();
aoqi@0 1649 switch (in_sig_bt[i]) {
aoqi@0 1650 case T_ARRAY: // critical array (uses 2 slots on LP64)
aoqi@0 1651 case T_BOOLEAN:
aoqi@0 1652 case T_BYTE:
aoqi@0 1653 case T_SHORT:
aoqi@0 1654 case T_CHAR:
aoqi@0 1655 case T_INT: single_slots++; break;
aoqi@0 1656 case T_LONG: double_slots++; break;
aoqi@0 1657 default: ShouldNotReachHere();
aoqi@0 1658 }
aoqi@0 1659 } else if (in_regs[i].first()->is_XMMRegister()) {
aoqi@0 1660 switch (in_sig_bt[i]) {
aoqi@0 1661 case T_FLOAT: single_slots++; break;
aoqi@0 1662 case T_DOUBLE: double_slots++; break;
aoqi@0 1663 default: ShouldNotReachHere();
aoqi@0 1664 }
aoqi@0 1665 } else if (in_regs[i].first()->is_FloatRegister()) {
aoqi@0 1666 ShouldNotReachHere();
aoqi@0 1667 }
aoqi@0 1668 }
aoqi@0 1669 total_save_slots = double_slots * 2 + single_slots;
aoqi@0 1670 // align the save area
aoqi@0 1671 if (double_slots != 0) {
aoqi@0 1672 stack_slots = round_to(stack_slots, 2);
aoqi@0 1673 }
aoqi@0 1674 }
aoqi@0 1675
aoqi@0 1676 int oop_handle_offset = stack_slots;
aoqi@0 1677 stack_slots += total_save_slots;
aoqi@0 1678
aoqi@0 1679 // Now any space we need for handlizing a klass if static method
aoqi@0 1680
aoqi@0 1681 int klass_slot_offset = 0;
aoqi@0 1682 int klass_offset = -1;
aoqi@0 1683 int lock_slot_offset = 0;
aoqi@0 1684 bool is_static = false;
aoqi@0 1685
aoqi@0 1686 if (method->is_static()) {
aoqi@0 1687 klass_slot_offset = stack_slots;
aoqi@0 1688 stack_slots += VMRegImpl::slots_per_word;
aoqi@0 1689 klass_offset = klass_slot_offset * VMRegImpl::stack_slot_size;
aoqi@0 1690 is_static = true;
aoqi@0 1691 }
aoqi@0 1692
aoqi@0 1693 // Plus a lock if needed
aoqi@0 1694
aoqi@0 1695 if (method->is_synchronized()) {
aoqi@0 1696 lock_slot_offset = stack_slots;
aoqi@0 1697 stack_slots += VMRegImpl::slots_per_word;
aoqi@0 1698 }
aoqi@0 1699
aoqi@0 1700 // Now a place (+2) to save return values or temp during shuffling
aoqi@0 1701 // + 2 for return address (which we own) and saved rbp,
aoqi@0 1702 stack_slots += 4;
aoqi@0 1703
aoqi@0 1704 // Ok The space we have allocated will look like:
aoqi@0 1705 //
aoqi@0 1706 //
aoqi@0 1707 // FP-> | |
aoqi@0 1708 // |---------------------|
aoqi@0 1709 // | 2 slots for moves |
aoqi@0 1710 // |---------------------|
aoqi@0 1711 // | lock box (if sync) |
aoqi@0 1712 // |---------------------| <- lock_slot_offset (-lock_slot_rbp_offset)
aoqi@0 1713 // | klass (if static) |
aoqi@0 1714 // |---------------------| <- klass_slot_offset
aoqi@0 1715 // | oopHandle area |
aoqi@0 1716 // |---------------------| <- oop_handle_offset (a max of 2 registers)
aoqi@0 1717 // | outbound memory |
aoqi@0 1718 // | based arguments |
aoqi@0 1719 // | |
aoqi@0 1720 // |---------------------|
aoqi@0 1721 // | |
aoqi@0 1722 // SP-> | out_preserved_slots |
aoqi@0 1723 //
aoqi@0 1724 //
aoqi@0 1725 // ****************************************************************************
aoqi@0 1726 // WARNING - on Windows Java Natives use pascal calling convention and pop the
aoqi@0 1727 // arguments off of the stack after the jni call. Before the call we can use
aoqi@0 1728 // instructions that are SP relative. After the jni call we switch to FP
aoqi@0 1729 // relative instructions instead of re-adjusting the stack on windows.
aoqi@0 1730 // ****************************************************************************
aoqi@0 1731
aoqi@0 1732
aoqi@0 1733 // Now compute actual number of stack words we need rounding to make
aoqi@0 1734 // stack properly aligned.
aoqi@0 1735 stack_slots = round_to(stack_slots, StackAlignmentInSlots);
aoqi@0 1736
aoqi@0 1737 int stack_size = stack_slots * VMRegImpl::stack_slot_size;
aoqi@0 1738
aoqi@0 1739 intptr_t start = (intptr_t)__ pc();
aoqi@0 1740
aoqi@0 1741 // First thing make an ic check to see if we should even be here
aoqi@0 1742
aoqi@0 1743 // We are free to use all registers as temps without saving them and
aoqi@0 1744 // restoring them except rbp. rbp is the only callee save register
aoqi@0 1745 // as far as the interpreter and the compiler(s) are concerned.
aoqi@0 1746
aoqi@0 1747
aoqi@0 1748 const Register ic_reg = rax;
aoqi@0 1749 const Register receiver = rcx;
aoqi@0 1750 Label hit;
aoqi@0 1751 Label exception_pending;
aoqi@0 1752
aoqi@0 1753 __ verify_oop(receiver);
aoqi@0 1754 __ cmpptr(ic_reg, Address(receiver, oopDesc::klass_offset_in_bytes()));
aoqi@0 1755 __ jcc(Assembler::equal, hit);
aoqi@0 1756
aoqi@0 1757 __ jump(RuntimeAddress(SharedRuntime::get_ic_miss_stub()));
aoqi@0 1758
aoqi@0 1759 // verified entry must be aligned for code patching.
aoqi@0 1760 // and the first 5 bytes must be in the same cache line
aoqi@0 1761 // if we align at 8 then we will be sure 5 bytes are in the same line
aoqi@0 1762 __ align(8);
aoqi@0 1763
aoqi@0 1764 __ bind(hit);
aoqi@0 1765
aoqi@0 1766 int vep_offset = ((intptr_t)__ pc()) - start;
aoqi@0 1767
aoqi@0 1768 #ifdef COMPILER1
aoqi@0 1769 if (InlineObjectHash && method->intrinsic_id() == vmIntrinsics::_hashCode) {
aoqi@0 1770 // Object.hashCode can pull the hashCode from the header word
aoqi@0 1771 // instead of doing a full VM transition once it's been computed.
aoqi@0 1772 // Since hashCode is usually polymorphic at call sites we can't do
aoqi@0 1773 // this optimization at the call site without a lot of work.
aoqi@0 1774 Label slowCase;
aoqi@0 1775 Register receiver = rcx;
aoqi@0 1776 Register result = rax;
aoqi@0 1777 __ movptr(result, Address(receiver, oopDesc::mark_offset_in_bytes()));
aoqi@0 1778
aoqi@0 1779 // check if locked
aoqi@0 1780 __ testptr(result, markOopDesc::unlocked_value);
aoqi@0 1781 __ jcc (Assembler::zero, slowCase);
aoqi@0 1782
aoqi@0 1783 if (UseBiasedLocking) {
aoqi@0 1784 // Check if biased and fall through to runtime if so
aoqi@0 1785 __ testptr(result, markOopDesc::biased_lock_bit_in_place);
aoqi@0 1786 __ jcc (Assembler::notZero, slowCase);
aoqi@0 1787 }
aoqi@0 1788
aoqi@0 1789 // get hash
aoqi@0 1790 __ andptr(result, markOopDesc::hash_mask_in_place);
aoqi@0 1791 // test if hashCode exists
aoqi@0 1792 __ jcc (Assembler::zero, slowCase);
aoqi@0 1793 __ shrptr(result, markOopDesc::hash_shift);
aoqi@0 1794 __ ret(0);
aoqi@0 1795 __ bind (slowCase);
aoqi@0 1796 }
aoqi@0 1797 #endif // COMPILER1
aoqi@0 1798
aoqi@0 1799 // The instruction at the verified entry point must be 5 bytes or longer
aoqi@0 1800 // because it can be patched on the fly by make_non_entrant. The stack bang
aoqi@0 1801 // instruction fits that requirement.
aoqi@0 1802
aoqi@0 1803 // Generate stack overflow check
aoqi@0 1804
aoqi@0 1805 if (UseStackBanging) {
aoqi@0 1806 __ bang_stack_with_offset(StackShadowPages*os::vm_page_size());
aoqi@0 1807 } else {
aoqi@0 1808 // need a 5 byte instruction to allow MT safe patching to non-entrant
aoqi@0 1809 __ fat_nop();
aoqi@0 1810 }
aoqi@0 1811
aoqi@0 1812 // Generate a new frame for the wrapper.
aoqi@0 1813 __ enter();
aoqi@0 1814 // -2 because return address is already present and so is saved rbp
aoqi@0 1815 __ subptr(rsp, stack_size - 2*wordSize);
aoqi@0 1816
aoqi@0 1817 // Frame is now completed as far as size and linkage.
aoqi@0 1818 int frame_complete = ((intptr_t)__ pc()) - start;
aoqi@0 1819
aoqi@0 1820 if (UseRTMLocking) {
aoqi@0 1821 // Abort RTM transaction before calling JNI
aoqi@0 1822 // because critical section will be large and will be
aoqi@0 1823 // aborted anyway. Also nmethod could be deoptimized.
aoqi@0 1824 __ xabort(0);
aoqi@0 1825 }
aoqi@0 1826
aoqi@0 1827 // Calculate the difference between rsp and rbp,. We need to know it
aoqi@0 1828 // after the native call because on windows Java Natives will pop
aoqi@0 1829 // the arguments and it is painful to do rsp relative addressing
aoqi@0 1830 // in a platform independent way. So after the call we switch to
aoqi@0 1831 // rbp, relative addressing.
aoqi@0 1832
aoqi@0 1833 int fp_adjustment = stack_size - 2*wordSize;
aoqi@0 1834
aoqi@0 1835 #ifdef COMPILER2
aoqi@0 1836 // C2 may leave the stack dirty if not in SSE2+ mode
aoqi@0 1837 if (UseSSE >= 2) {
aoqi@0 1838 __ verify_FPU(0, "c2i transition should have clean FPU stack");
aoqi@0 1839 } else {
aoqi@0 1840 __ empty_FPU_stack();
aoqi@0 1841 }
aoqi@0 1842 #endif /* COMPILER2 */
aoqi@0 1843
aoqi@0 1844 // Compute the rbp, offset for any slots used after the jni call
aoqi@0 1845
aoqi@0 1846 int lock_slot_rbp_offset = (lock_slot_offset*VMRegImpl::stack_slot_size) - fp_adjustment;
aoqi@0 1847
aoqi@0 1848 // We use rdi as a thread pointer because it is callee save and
aoqi@0 1849 // if we load it once it is usable thru the entire wrapper
aoqi@0 1850 const Register thread = rdi;
aoqi@0 1851
aoqi@0 1852 // We use rsi as the oop handle for the receiver/klass
aoqi@0 1853 // It is callee save so it survives the call to native
aoqi@0 1854
aoqi@0 1855 const Register oop_handle_reg = rsi;
aoqi@0 1856
aoqi@0 1857 __ get_thread(thread);
aoqi@0 1858
aoqi@0 1859 if (is_critical_native) {
aoqi@0 1860 check_needs_gc_for_critical_native(masm, thread, stack_slots, total_c_args, total_in_args,
aoqi@0 1861 oop_handle_offset, oop_maps, in_regs, in_sig_bt);
aoqi@0 1862 }
aoqi@0 1863
aoqi@0 1864 //
aoqi@0 1865 // We immediately shuffle the arguments so that any vm call we have to
aoqi@0 1866 // make from here on out (sync slow path, jvmti, etc.) we will have
aoqi@0 1867 // captured the oops from our caller and have a valid oopMap for
aoqi@0 1868 // them.
aoqi@0 1869
aoqi@0 1870 // -----------------
aoqi@0 1871 // The Grand Shuffle
aoqi@0 1872 //
aoqi@0 1873 // Natives require 1 or 2 extra arguments over the normal ones: the JNIEnv*
aoqi@0 1874 // and, if static, the class mirror instead of a receiver. This pretty much
aoqi@0 1875 // guarantees that register layout will not match (and x86 doesn't use reg
aoqi@0 1876 // parms though amd does). Since the native abi doesn't use register args
aoqi@0 1877 // and the java conventions does we don't have to worry about collisions.
aoqi@0 1878 // All of our moved are reg->stack or stack->stack.
aoqi@0 1879 // We ignore the extra arguments during the shuffle and handle them at the
aoqi@0 1880 // last moment. The shuffle is described by the two calling convention
aoqi@0 1881 // vectors we have in our possession. We simply walk the java vector to
aoqi@0 1882 // get the source locations and the c vector to get the destinations.
aoqi@0 1883
aoqi@0 1884 int c_arg = is_critical_native ? 0 : (method->is_static() ? 2 : 1 );
aoqi@0 1885
aoqi@0 1886 // Record rsp-based slot for receiver on stack for non-static methods
aoqi@0 1887 int receiver_offset = -1;
aoqi@0 1888
aoqi@0 1889 // This is a trick. We double the stack slots so we can claim
aoqi@0 1890 // the oops in the caller's frame. Since we are sure to have
aoqi@0 1891 // more args than the caller doubling is enough to make
aoqi@0 1892 // sure we can capture all the incoming oop args from the
aoqi@0 1893 // caller.
aoqi@0 1894 //
aoqi@0 1895 OopMap* map = new OopMap(stack_slots * 2, 0 /* arg_slots*/);
aoqi@0 1896
aoqi@0 1897 // Mark location of rbp,
aoqi@0 1898 // map->set_callee_saved(VMRegImpl::stack2reg( stack_slots - 2), stack_slots * 2, 0, rbp->as_VMReg());
aoqi@0 1899
aoqi@0 1900 // We know that we only have args in at most two integer registers (rcx, rdx). So rax, rbx
aoqi@0 1901 // Are free to temporaries if we have to do stack to steck moves.
aoqi@0 1902 // All inbound args are referenced based on rbp, and all outbound args via rsp.
aoqi@0 1903
aoqi@0 1904 for (int i = 0; i < total_in_args ; i++, c_arg++ ) {
aoqi@0 1905 switch (in_sig_bt[i]) {
aoqi@0 1906 case T_ARRAY:
aoqi@0 1907 if (is_critical_native) {
aoqi@0 1908 unpack_array_argument(masm, in_regs[i], in_elem_bt[i], out_regs[c_arg + 1], out_regs[c_arg]);
aoqi@0 1909 c_arg++;
aoqi@0 1910 break;
aoqi@0 1911 }
aoqi@0 1912 case T_OBJECT:
aoqi@0 1913 assert(!is_critical_native, "no oop arguments");
aoqi@0 1914 object_move(masm, map, oop_handle_offset, stack_slots, in_regs[i], out_regs[c_arg],
aoqi@0 1915 ((i == 0) && (!is_static)),
aoqi@0 1916 &receiver_offset);
aoqi@0 1917 break;
aoqi@0 1918 case T_VOID:
aoqi@0 1919 break;
aoqi@0 1920
aoqi@0 1921 case T_FLOAT:
aoqi@0 1922 float_move(masm, in_regs[i], out_regs[c_arg]);
aoqi@0 1923 break;
aoqi@0 1924
aoqi@0 1925 case T_DOUBLE:
aoqi@0 1926 assert( i + 1 < total_in_args &&
aoqi@0 1927 in_sig_bt[i + 1] == T_VOID &&
aoqi@0 1928 out_sig_bt[c_arg+1] == T_VOID, "bad arg list");
aoqi@0 1929 double_move(masm, in_regs[i], out_regs[c_arg]);
aoqi@0 1930 break;
aoqi@0 1931
aoqi@0 1932 case T_LONG :
aoqi@0 1933 long_move(masm, in_regs[i], out_regs[c_arg]);
aoqi@0 1934 break;
aoqi@0 1935
aoqi@0 1936 case T_ADDRESS: assert(false, "found T_ADDRESS in java args");
aoqi@0 1937
aoqi@0 1938 default:
aoqi@0 1939 simple_move32(masm, in_regs[i], out_regs[c_arg]);
aoqi@0 1940 }
aoqi@0 1941 }
aoqi@0 1942
aoqi@0 1943 // Pre-load a static method's oop into rsi. Used both by locking code and
aoqi@0 1944 // the normal JNI call code.
aoqi@0 1945 if (method->is_static() && !is_critical_native) {
aoqi@0 1946
aoqi@0 1947 // load opp into a register
aoqi@0 1948 __ movoop(oop_handle_reg, JNIHandles::make_local(method->method_holder()->java_mirror()));
aoqi@0 1949
aoqi@0 1950 // Now handlize the static class mirror it's known not-null.
aoqi@0 1951 __ movptr(Address(rsp, klass_offset), oop_handle_reg);
aoqi@0 1952 map->set_oop(VMRegImpl::stack2reg(klass_slot_offset));
aoqi@0 1953
aoqi@0 1954 // Now get the handle
aoqi@0 1955 __ lea(oop_handle_reg, Address(rsp, klass_offset));
aoqi@0 1956 // store the klass handle as second argument
aoqi@0 1957 __ movptr(Address(rsp, wordSize), oop_handle_reg);
aoqi@0 1958 }
aoqi@0 1959
aoqi@0 1960 // Change state to native (we save the return address in the thread, since it might not
aoqi@0 1961 // be pushed on the stack when we do a a stack traversal). It is enough that the pc()
aoqi@0 1962 // points into the right code segment. It does not have to be the correct return pc.
aoqi@0 1963 // We use the same pc/oopMap repeatedly when we call out
aoqi@0 1964
aoqi@0 1965 intptr_t the_pc = (intptr_t) __ pc();
aoqi@0 1966 oop_maps->add_gc_map(the_pc - start, map);
aoqi@0 1967
aoqi@0 1968 __ set_last_Java_frame(thread, rsp, noreg, (address)the_pc);
aoqi@0 1969
aoqi@0 1970
aoqi@0 1971 // We have all of the arguments setup at this point. We must not touch any register
aoqi@0 1972 // argument registers at this point (what if we save/restore them there are no oop?
aoqi@0 1973
aoqi@0 1974 {
aoqi@0 1975 SkipIfEqual skip_if(masm, &DTraceMethodProbes, 0);
aoqi@0 1976 __ mov_metadata(rax, method());
aoqi@0 1977 __ call_VM_leaf(
aoqi@0 1978 CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_entry),
aoqi@0 1979 thread, rax);
aoqi@0 1980 }
aoqi@0 1981
aoqi@0 1982 // RedefineClasses() tracing support for obsolete method entry
aoqi@0 1983 if (RC_TRACE_IN_RANGE(0x00001000, 0x00002000)) {
aoqi@0 1984 __ mov_metadata(rax, method());
aoqi@0 1985 __ call_VM_leaf(
aoqi@0 1986 CAST_FROM_FN_PTR(address, SharedRuntime::rc_trace_method_entry),
aoqi@0 1987 thread, rax);
aoqi@0 1988 }
aoqi@0 1989
aoqi@0 1990 // These are register definitions we need for locking/unlocking
aoqi@0 1991 const Register swap_reg = rax; // Must use rax, for cmpxchg instruction
aoqi@0 1992 const Register obj_reg = rcx; // Will contain the oop
aoqi@0 1993 const Register lock_reg = rdx; // Address of compiler lock object (BasicLock)
aoqi@0 1994
aoqi@0 1995 Label slow_path_lock;
aoqi@0 1996 Label lock_done;
aoqi@0 1997
aoqi@0 1998 // Lock a synchronized method
aoqi@0 1999 if (method->is_synchronized()) {
aoqi@0 2000 assert(!is_critical_native, "unhandled");
aoqi@0 2001
aoqi@0 2002
aoqi@0 2003 const int mark_word_offset = BasicLock::displaced_header_offset_in_bytes();
aoqi@0 2004
aoqi@0 2005 // Get the handle (the 2nd argument)
aoqi@0 2006 __ movptr(oop_handle_reg, Address(rsp, wordSize));
aoqi@0 2007
aoqi@0 2008 // Get address of the box
aoqi@0 2009
aoqi@0 2010 __ lea(lock_reg, Address(rbp, lock_slot_rbp_offset));
aoqi@0 2011
aoqi@0 2012 // Load the oop from the handle
aoqi@0 2013 __ movptr(obj_reg, Address(oop_handle_reg, 0));
aoqi@0 2014
aoqi@0 2015 if (UseBiasedLocking) {
aoqi@0 2016 // Note that oop_handle_reg is trashed during this call
aoqi@0 2017 __ biased_locking_enter(lock_reg, obj_reg, swap_reg, oop_handle_reg, false, lock_done, &slow_path_lock);
aoqi@0 2018 }
aoqi@0 2019
aoqi@0 2020 // Load immediate 1 into swap_reg %rax,
aoqi@0 2021 __ movptr(swap_reg, 1);
aoqi@0 2022
aoqi@0 2023 // Load (object->mark() | 1) into swap_reg %rax,
aoqi@0 2024 __ orptr(swap_reg, Address(obj_reg, 0));
aoqi@0 2025
aoqi@0 2026 // Save (object->mark() | 1) into BasicLock's displaced header
aoqi@0 2027 __ movptr(Address(lock_reg, mark_word_offset), swap_reg);
aoqi@0 2028
aoqi@0 2029 if (os::is_MP()) {
aoqi@0 2030 __ lock();
aoqi@0 2031 }
aoqi@0 2032
aoqi@0 2033 // src -> dest iff dest == rax, else rax, <- dest
aoqi@0 2034 // *obj_reg = lock_reg iff *obj_reg == rax, else rax, = *(obj_reg)
aoqi@0 2035 __ cmpxchgptr(lock_reg, Address(obj_reg, 0));
aoqi@0 2036 __ jcc(Assembler::equal, lock_done);
aoqi@0 2037
aoqi@0 2038 // Test if the oopMark is an obvious stack pointer, i.e.,
aoqi@0 2039 // 1) (mark & 3) == 0, and
aoqi@0 2040 // 2) rsp <= mark < mark + os::pagesize()
aoqi@0 2041 // These 3 tests can be done by evaluating the following
aoqi@0 2042 // expression: ((mark - rsp) & (3 - os::vm_page_size())),
aoqi@0 2043 // assuming both stack pointer and pagesize have their
aoqi@0 2044 // least significant 2 bits clear.
aoqi@0 2045 // NOTE: the oopMark is in swap_reg %rax, as the result of cmpxchg
aoqi@0 2046
aoqi@0 2047 __ subptr(swap_reg, rsp);
aoqi@0 2048 __ andptr(swap_reg, 3 - os::vm_page_size());
aoqi@0 2049
aoqi@0 2050 // Save the test result, for recursive case, the result is zero
aoqi@0 2051 __ movptr(Address(lock_reg, mark_word_offset), swap_reg);
aoqi@0 2052 __ jcc(Assembler::notEqual, slow_path_lock);
aoqi@0 2053 // Slow path will re-enter here
aoqi@0 2054 __ bind(lock_done);
aoqi@0 2055
aoqi@0 2056 if (UseBiasedLocking) {
aoqi@0 2057 // Re-fetch oop_handle_reg as we trashed it above
aoqi@0 2058 __ movptr(oop_handle_reg, Address(rsp, wordSize));
aoqi@0 2059 }
aoqi@0 2060 }
aoqi@0 2061
aoqi@0 2062
aoqi@0 2063 // Finally just about ready to make the JNI call
aoqi@0 2064
aoqi@0 2065
aoqi@0 2066 // get JNIEnv* which is first argument to native
aoqi@0 2067 if (!is_critical_native) {
aoqi@0 2068 __ lea(rdx, Address(thread, in_bytes(JavaThread::jni_environment_offset())));
aoqi@0 2069 __ movptr(Address(rsp, 0), rdx);
aoqi@0 2070 }
aoqi@0 2071
aoqi@0 2072 // Now set thread in native
aoqi@0 2073 __ movl(Address(thread, JavaThread::thread_state_offset()), _thread_in_native);
aoqi@0 2074
aoqi@0 2075 __ call(RuntimeAddress(native_func));
aoqi@0 2076
aoqi@0 2077 // Verify or restore cpu control state after JNI call
aoqi@0 2078 __ restore_cpu_control_state_after_jni();
aoqi@0 2079
aoqi@0 2080 // WARNING - on Windows Java Natives use pascal calling convention and pop the
aoqi@0 2081 // arguments off of the stack. We could just re-adjust the stack pointer here
aoqi@0 2082 // and continue to do SP relative addressing but we instead switch to FP
aoqi@0 2083 // relative addressing.
aoqi@0 2084
aoqi@0 2085 // Unpack native results.
aoqi@0 2086 switch (ret_type) {
aoqi@0 2087 case T_BOOLEAN: __ c2bool(rax); break;
aoqi@0 2088 case T_CHAR : __ andptr(rax, 0xFFFF); break;
aoqi@0 2089 case T_BYTE : __ sign_extend_byte (rax); break;
aoqi@0 2090 case T_SHORT : __ sign_extend_short(rax); break;
aoqi@0 2091 case T_INT : /* nothing to do */ break;
aoqi@0 2092 case T_DOUBLE :
aoqi@0 2093 case T_FLOAT :
aoqi@0 2094 // Result is in st0 we'll save as needed
aoqi@0 2095 break;
aoqi@0 2096 case T_ARRAY: // Really a handle
aoqi@0 2097 case T_OBJECT: // Really a handle
aoqi@0 2098 break; // can't de-handlize until after safepoint check
aoqi@0 2099 case T_VOID: break;
aoqi@0 2100 case T_LONG: break;
aoqi@0 2101 default : ShouldNotReachHere();
aoqi@0 2102 }
aoqi@0 2103
aoqi@0 2104 // Switch thread to "native transition" state before reading the synchronization state.
aoqi@0 2105 // This additional state is necessary because reading and testing the synchronization
aoqi@0 2106 // state is not atomic w.r.t. GC, as this scenario demonstrates:
aoqi@0 2107 // Java thread A, in _thread_in_native state, loads _not_synchronized and is preempted.
aoqi@0 2108 // VM thread changes sync state to synchronizing and suspends threads for GC.
aoqi@0 2109 // Thread A is resumed to finish this native method, but doesn't block here since it
aoqi@0 2110 // didn't see any synchronization is progress, and escapes.
aoqi@0 2111 __ movl(Address(thread, JavaThread::thread_state_offset()), _thread_in_native_trans);
aoqi@0 2112
aoqi@0 2113 if(os::is_MP()) {
aoqi@0 2114 if (UseMembar) {
aoqi@0 2115 // Force this write out before the read below
aoqi@0 2116 __ membar(Assembler::Membar_mask_bits(
aoqi@0 2117 Assembler::LoadLoad | Assembler::LoadStore |
aoqi@0 2118 Assembler::StoreLoad | Assembler::StoreStore));
aoqi@0 2119 } else {
aoqi@0 2120 // Write serialization page so VM thread can do a pseudo remote membar.
aoqi@0 2121 // We use the current thread pointer to calculate a thread specific
aoqi@0 2122 // offset to write to within the page. This minimizes bus traffic
aoqi@0 2123 // due to cache line collision.
aoqi@0 2124 __ serialize_memory(thread, rcx);
aoqi@0 2125 }
aoqi@0 2126 }
aoqi@0 2127
aoqi@0 2128 if (AlwaysRestoreFPU) {
aoqi@0 2129 // Make sure the control word is correct.
aoqi@0 2130 __ fldcw(ExternalAddress(StubRoutines::addr_fpu_cntrl_wrd_std()));
aoqi@0 2131 }
aoqi@0 2132
aoqi@0 2133 Label after_transition;
aoqi@0 2134
aoqi@0 2135 // check for safepoint operation in progress and/or pending suspend requests
aoqi@0 2136 { Label Continue;
aoqi@0 2137
aoqi@0 2138 __ cmp32(ExternalAddress((address)SafepointSynchronize::address_of_state()),
aoqi@0 2139 SafepointSynchronize::_not_synchronized);
aoqi@0 2140
aoqi@0 2141 Label L;
aoqi@0 2142 __ jcc(Assembler::notEqual, L);
aoqi@0 2143 __ cmpl(Address(thread, JavaThread::suspend_flags_offset()), 0);
aoqi@0 2144 __ jcc(Assembler::equal, Continue);
aoqi@0 2145 __ bind(L);
aoqi@0 2146
aoqi@0 2147 // Don't use call_VM as it will see a possible pending exception and forward it
aoqi@0 2148 // and never return here preventing us from clearing _last_native_pc down below.
aoqi@0 2149 // Also can't use call_VM_leaf either as it will check to see if rsi & rdi are
aoqi@0 2150 // preserved and correspond to the bcp/locals pointers. So we do a runtime call
aoqi@0 2151 // by hand.
aoqi@0 2152 //
aoqi@0 2153 save_native_result(masm, ret_type, stack_slots);
aoqi@0 2154 __ push(thread);
aoqi@0 2155 if (!is_critical_native) {
aoqi@0 2156 __ call(RuntimeAddress(CAST_FROM_FN_PTR(address,
aoqi@0 2157 JavaThread::check_special_condition_for_native_trans)));
aoqi@0 2158 } else {
aoqi@0 2159 __ call(RuntimeAddress(CAST_FROM_FN_PTR(address,
aoqi@0 2160 JavaThread::check_special_condition_for_native_trans_and_transition)));
aoqi@0 2161 }
aoqi@0 2162 __ increment(rsp, wordSize);
aoqi@0 2163 // Restore any method result value
aoqi@0 2164 restore_native_result(masm, ret_type, stack_slots);
aoqi@0 2165
aoqi@0 2166 if (is_critical_native) {
aoqi@0 2167 // The call above performed the transition to thread_in_Java so
aoqi@0 2168 // skip the transition logic below.
aoqi@0 2169 __ jmpb(after_transition);
aoqi@0 2170 }
aoqi@0 2171
aoqi@0 2172 __ bind(Continue);
aoqi@0 2173 }
aoqi@0 2174
aoqi@0 2175 // change thread state
aoqi@0 2176 __ movl(Address(thread, JavaThread::thread_state_offset()), _thread_in_Java);
aoqi@0 2177 __ bind(after_transition);
aoqi@0 2178
aoqi@0 2179 Label reguard;
aoqi@0 2180 Label reguard_done;
aoqi@0 2181 __ cmpl(Address(thread, JavaThread::stack_guard_state_offset()), JavaThread::stack_guard_yellow_disabled);
aoqi@0 2182 __ jcc(Assembler::equal, reguard);
aoqi@0 2183
aoqi@0 2184 // slow path reguard re-enters here
aoqi@0 2185 __ bind(reguard_done);
aoqi@0 2186
aoqi@0 2187 // Handle possible exception (will unlock if necessary)
aoqi@0 2188
aoqi@0 2189 // native result if any is live
aoqi@0 2190
aoqi@0 2191 // Unlock
aoqi@0 2192 Label slow_path_unlock;
aoqi@0 2193 Label unlock_done;
aoqi@0 2194 if (method->is_synchronized()) {
aoqi@0 2195
aoqi@0 2196 Label done;
aoqi@0 2197
aoqi@0 2198 // Get locked oop from the handle we passed to jni
aoqi@0 2199 __ movptr(obj_reg, Address(oop_handle_reg, 0));
aoqi@0 2200
aoqi@0 2201 if (UseBiasedLocking) {
aoqi@0 2202 __ biased_locking_exit(obj_reg, rbx, done);
aoqi@0 2203 }
aoqi@0 2204
aoqi@0 2205 // Simple recursive lock?
aoqi@0 2206
aoqi@0 2207 __ cmpptr(Address(rbp, lock_slot_rbp_offset), (int32_t)NULL_WORD);
aoqi@0 2208 __ jcc(Assembler::equal, done);
aoqi@0 2209
aoqi@0 2210 // Must save rax, if if it is live now because cmpxchg must use it
aoqi@0 2211 if (ret_type != T_FLOAT && ret_type != T_DOUBLE && ret_type != T_VOID) {
aoqi@0 2212 save_native_result(masm, ret_type, stack_slots);
aoqi@0 2213 }
aoqi@0 2214
aoqi@0 2215 // get old displaced header
aoqi@0 2216 __ movptr(rbx, Address(rbp, lock_slot_rbp_offset));
aoqi@0 2217
aoqi@0 2218 // get address of the stack lock
aoqi@0 2219 __ lea(rax, Address(rbp, lock_slot_rbp_offset));
aoqi@0 2220
aoqi@0 2221 // Atomic swap old header if oop still contains the stack lock
aoqi@0 2222 if (os::is_MP()) {
aoqi@0 2223 __ lock();
aoqi@0 2224 }
aoqi@0 2225
aoqi@0 2226 // src -> dest iff dest == rax, else rax, <- dest
aoqi@0 2227 // *obj_reg = rbx, iff *obj_reg == rax, else rax, = *(obj_reg)
aoqi@0 2228 __ cmpxchgptr(rbx, Address(obj_reg, 0));
aoqi@0 2229 __ jcc(Assembler::notEqual, slow_path_unlock);
aoqi@0 2230
aoqi@0 2231 // slow path re-enters here
aoqi@0 2232 __ bind(unlock_done);
aoqi@0 2233 if (ret_type != T_FLOAT && ret_type != T_DOUBLE && ret_type != T_VOID) {
aoqi@0 2234 restore_native_result(masm, ret_type, stack_slots);
aoqi@0 2235 }
aoqi@0 2236
aoqi@0 2237 __ bind(done);
aoqi@0 2238
aoqi@0 2239 }
aoqi@0 2240
aoqi@0 2241 {
aoqi@0 2242 SkipIfEqual skip_if(masm, &DTraceMethodProbes, 0);
aoqi@0 2243 // Tell dtrace about this method exit
aoqi@0 2244 save_native_result(masm, ret_type, stack_slots);
aoqi@0 2245 __ mov_metadata(rax, method());
aoqi@0 2246 __ call_VM_leaf(
aoqi@0 2247 CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_exit),
aoqi@0 2248 thread, rax);
aoqi@0 2249 restore_native_result(masm, ret_type, stack_slots);
aoqi@0 2250 }
aoqi@0 2251
aoqi@0 2252 // We can finally stop using that last_Java_frame we setup ages ago
aoqi@0 2253
aoqi@0 2254 __ reset_last_Java_frame(thread, false, true);
aoqi@0 2255
aoqi@0 2256 // Unpack oop result
aoqi@0 2257 if (ret_type == T_OBJECT || ret_type == T_ARRAY) {
aoqi@0 2258 Label L;
aoqi@0 2259 __ cmpptr(rax, (int32_t)NULL_WORD);
aoqi@0 2260 __ jcc(Assembler::equal, L);
aoqi@0 2261 __ movptr(rax, Address(rax, 0));
aoqi@0 2262 __ bind(L);
aoqi@0 2263 __ verify_oop(rax);
aoqi@0 2264 }
aoqi@0 2265
aoqi@0 2266 if (!is_critical_native) {
aoqi@0 2267 // reset handle block
aoqi@0 2268 __ movptr(rcx, Address(thread, JavaThread::active_handles_offset()));
aoqi@0 2269 __ movl(Address(rcx, JNIHandleBlock::top_offset_in_bytes()), NULL_WORD);
aoqi@0 2270
aoqi@0 2271 // Any exception pending?
aoqi@0 2272 __ cmpptr(Address(thread, in_bytes(Thread::pending_exception_offset())), (int32_t)NULL_WORD);
aoqi@0 2273 __ jcc(Assembler::notEqual, exception_pending);
aoqi@0 2274 }
aoqi@0 2275
aoqi@0 2276 // no exception, we're almost done
aoqi@0 2277
aoqi@0 2278 // check that only result value is on FPU stack
aoqi@0 2279 __ verify_FPU(ret_type == T_FLOAT || ret_type == T_DOUBLE ? 1 : 0, "native_wrapper normal exit");
aoqi@0 2280
aoqi@0 2281 // Fixup floating pointer results so that result looks like a return from a compiled method
aoqi@0 2282 if (ret_type == T_FLOAT) {
aoqi@0 2283 if (UseSSE >= 1) {
aoqi@0 2284 // Pop st0 and store as float and reload into xmm register
aoqi@0 2285 __ fstp_s(Address(rbp, -4));
aoqi@0 2286 __ movflt(xmm0, Address(rbp, -4));
aoqi@0 2287 }
aoqi@0 2288 } else if (ret_type == T_DOUBLE) {
aoqi@0 2289 if (UseSSE >= 2) {
aoqi@0 2290 // Pop st0 and store as double and reload into xmm register
aoqi@0 2291 __ fstp_d(Address(rbp, -8));
aoqi@0 2292 __ movdbl(xmm0, Address(rbp, -8));
aoqi@0 2293 }
aoqi@0 2294 }
aoqi@0 2295
aoqi@0 2296 // Return
aoqi@0 2297
aoqi@0 2298 __ leave();
aoqi@0 2299 __ ret(0);
aoqi@0 2300
aoqi@0 2301 // Unexpected paths are out of line and go here
aoqi@0 2302
aoqi@0 2303 // Slow path locking & unlocking
aoqi@0 2304 if (method->is_synchronized()) {
aoqi@0 2305
aoqi@0 2306 // BEGIN Slow path lock
aoqi@0 2307
aoqi@0 2308 __ bind(slow_path_lock);
aoqi@0 2309
aoqi@0 2310 // has last_Java_frame setup. No exceptions so do vanilla call not call_VM
aoqi@0 2311 // args are (oop obj, BasicLock* lock, JavaThread* thread)
aoqi@0 2312 __ push(thread);
aoqi@0 2313 __ push(lock_reg);
aoqi@0 2314 __ push(obj_reg);
aoqi@0 2315 __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, SharedRuntime::complete_monitor_locking_C)));
aoqi@0 2316 __ addptr(rsp, 3*wordSize);
aoqi@0 2317
aoqi@0 2318 #ifdef ASSERT
aoqi@0 2319 { Label L;
aoqi@0 2320 __ cmpptr(Address(thread, in_bytes(Thread::pending_exception_offset())), (int)NULL_WORD);
aoqi@0 2321 __ jcc(Assembler::equal, L);
aoqi@0 2322 __ stop("no pending exception allowed on exit from monitorenter");
aoqi@0 2323 __ bind(L);
aoqi@0 2324 }
aoqi@0 2325 #endif
aoqi@0 2326 __ jmp(lock_done);
aoqi@0 2327
aoqi@0 2328 // END Slow path lock
aoqi@0 2329
aoqi@0 2330 // BEGIN Slow path unlock
aoqi@0 2331 __ bind(slow_path_unlock);
aoqi@0 2332
aoqi@0 2333 // Slow path unlock
aoqi@0 2334
aoqi@0 2335 if (ret_type == T_FLOAT || ret_type == T_DOUBLE ) {
aoqi@0 2336 save_native_result(masm, ret_type, stack_slots);
aoqi@0 2337 }
aoqi@0 2338 // Save pending exception around call to VM (which contains an EXCEPTION_MARK)
aoqi@0 2339
aoqi@0 2340 __ pushptr(Address(thread, in_bytes(Thread::pending_exception_offset())));
aoqi@0 2341 __ movptr(Address(thread, in_bytes(Thread::pending_exception_offset())), NULL_WORD);
aoqi@0 2342
aoqi@0 2343
aoqi@0 2344 // should be a peal
aoqi@0 2345 // +wordSize because of the push above
aoqi@0 2346 __ lea(rax, Address(rbp, lock_slot_rbp_offset));
aoqi@0 2347 __ push(rax);
aoqi@0 2348
aoqi@0 2349 __ push(obj_reg);
aoqi@0 2350 __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, SharedRuntime::complete_monitor_unlocking_C)));
aoqi@0 2351 __ addptr(rsp, 2*wordSize);
aoqi@0 2352 #ifdef ASSERT
aoqi@0 2353 {
aoqi@0 2354 Label L;
aoqi@0 2355 __ cmpptr(Address(thread, in_bytes(Thread::pending_exception_offset())), (int32_t)NULL_WORD);
aoqi@0 2356 __ jcc(Assembler::equal, L);
aoqi@0 2357 __ stop("no pending exception allowed on exit complete_monitor_unlocking_C");
aoqi@0 2358 __ bind(L);
aoqi@0 2359 }
aoqi@0 2360 #endif /* ASSERT */
aoqi@0 2361
aoqi@0 2362 __ popptr(Address(thread, in_bytes(Thread::pending_exception_offset())));
aoqi@0 2363
aoqi@0 2364 if (ret_type == T_FLOAT || ret_type == T_DOUBLE ) {
aoqi@0 2365 restore_native_result(masm, ret_type, stack_slots);
aoqi@0 2366 }
aoqi@0 2367 __ jmp(unlock_done);
aoqi@0 2368 // END Slow path unlock
aoqi@0 2369
aoqi@0 2370 }
aoqi@0 2371
aoqi@0 2372 // SLOW PATH Reguard the stack if needed
aoqi@0 2373
aoqi@0 2374 __ bind(reguard);
aoqi@0 2375 save_native_result(masm, ret_type, stack_slots);
aoqi@0 2376 {
aoqi@0 2377 __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, SharedRuntime::reguard_yellow_pages)));
aoqi@0 2378 }
aoqi@0 2379 restore_native_result(masm, ret_type, stack_slots);
aoqi@0 2380 __ jmp(reguard_done);
aoqi@0 2381
aoqi@0 2382
aoqi@0 2383 // BEGIN EXCEPTION PROCESSING
aoqi@0 2384
aoqi@0 2385 if (!is_critical_native) {
aoqi@0 2386 // Forward the exception
aoqi@0 2387 __ bind(exception_pending);
aoqi@0 2388
aoqi@0 2389 // remove possible return value from FPU register stack
aoqi@0 2390 __ empty_FPU_stack();
aoqi@0 2391
aoqi@0 2392 // pop our frame
aoqi@0 2393 __ leave();
aoqi@0 2394 // and forward the exception
aoqi@0 2395 __ jump(RuntimeAddress(StubRoutines::forward_exception_entry()));
aoqi@0 2396 }
aoqi@0 2397
aoqi@0 2398 __ flush();
aoqi@0 2399
aoqi@0 2400 nmethod *nm = nmethod::new_native_nmethod(method,
aoqi@0 2401 compile_id,
aoqi@0 2402 masm->code(),
aoqi@0 2403 vep_offset,
aoqi@0 2404 frame_complete,
aoqi@0 2405 stack_slots / VMRegImpl::slots_per_word,
aoqi@0 2406 (is_static ? in_ByteSize(klass_offset) : in_ByteSize(receiver_offset)),
aoqi@0 2407 in_ByteSize(lock_slot_offset*VMRegImpl::stack_slot_size),
aoqi@0 2408 oop_maps);
aoqi@0 2409
aoqi@0 2410 if (is_critical_native) {
aoqi@0 2411 nm->set_lazy_critical_native(true);
aoqi@0 2412 }
aoqi@0 2413
aoqi@0 2414 return nm;
aoqi@0 2415
aoqi@0 2416 }
aoqi@0 2417
aoqi@0 2418 #ifdef HAVE_DTRACE_H
aoqi@0 2419 // ---------------------------------------------------------------------------
aoqi@0 2420 // Generate a dtrace nmethod for a given signature. The method takes arguments
aoqi@0 2421 // in the Java compiled code convention, marshals them to the native
aoqi@0 2422 // abi and then leaves nops at the position you would expect to call a native
aoqi@0 2423 // function. When the probe is enabled the nops are replaced with a trap
aoqi@0 2424 // instruction that dtrace inserts and the trace will cause a notification
aoqi@0 2425 // to dtrace.
aoqi@0 2426 //
aoqi@0 2427 // The probes are only able to take primitive types and java/lang/String as
aoqi@0 2428 // arguments. No other java types are allowed. Strings are converted to utf8
aoqi@0 2429 // strings so that from dtrace point of view java strings are converted to C
aoqi@0 2430 // strings. There is an arbitrary fixed limit on the total space that a method
aoqi@0 2431 // can use for converting the strings. (256 chars per string in the signature).
aoqi@0 2432 // So any java string larger then this is truncated.
aoqi@0 2433
aoqi@0 2434 nmethod *SharedRuntime::generate_dtrace_nmethod(
aoqi@0 2435 MacroAssembler *masm, methodHandle method) {
aoqi@0 2436
aoqi@0 2437 // generate_dtrace_nmethod is guarded by a mutex so we are sure to
aoqi@0 2438 // be single threaded in this method.
aoqi@0 2439 assert(AdapterHandlerLibrary_lock->owned_by_self(), "must be");
aoqi@0 2440
aoqi@0 2441 // Fill in the signature array, for the calling-convention call.
aoqi@0 2442 int total_args_passed = method->size_of_parameters();
aoqi@0 2443
aoqi@0 2444 BasicType* in_sig_bt = NEW_RESOURCE_ARRAY(BasicType, total_args_passed);
aoqi@0 2445 VMRegPair *in_regs = NEW_RESOURCE_ARRAY(VMRegPair, total_args_passed);
aoqi@0 2446
aoqi@0 2447 // The signature we are going to use for the trap that dtrace will see
aoqi@0 2448 // java/lang/String is converted. We drop "this" and any other object
aoqi@0 2449 // is converted to NULL. (A one-slot java/lang/Long object reference
aoqi@0 2450 // is converted to a two-slot long, which is why we double the allocation).
aoqi@0 2451 BasicType* out_sig_bt = NEW_RESOURCE_ARRAY(BasicType, total_args_passed * 2);
aoqi@0 2452 VMRegPair* out_regs = NEW_RESOURCE_ARRAY(VMRegPair, total_args_passed * 2);
aoqi@0 2453
aoqi@0 2454 int i=0;
aoqi@0 2455 int total_strings = 0;
aoqi@0 2456 int first_arg_to_pass = 0;
aoqi@0 2457 int total_c_args = 0;
aoqi@0 2458
aoqi@0 2459 if( !method->is_static() ) { // Pass in receiver first
aoqi@0 2460 in_sig_bt[i++] = T_OBJECT;
aoqi@0 2461 first_arg_to_pass = 1;
aoqi@0 2462 }
aoqi@0 2463
aoqi@0 2464 // We need to convert the java args to where a native (non-jni) function
aoqi@0 2465 // would expect them. To figure out where they go we convert the java
aoqi@0 2466 // signature to a C signature.
aoqi@0 2467
aoqi@0 2468 SignatureStream ss(method->signature());
aoqi@0 2469 for ( ; !ss.at_return_type(); ss.next()) {
aoqi@0 2470 BasicType bt = ss.type();
aoqi@0 2471 in_sig_bt[i++] = bt; // Collect remaining bits of signature
aoqi@0 2472 out_sig_bt[total_c_args++] = bt;
aoqi@0 2473 if( bt == T_OBJECT) {
aoqi@0 2474 Symbol* s = ss.as_symbol_or_null(); // symbol is created
aoqi@0 2475 if (s == vmSymbols::java_lang_String()) {
aoqi@0 2476 total_strings++;
aoqi@0 2477 out_sig_bt[total_c_args-1] = T_ADDRESS;
aoqi@0 2478 } else if (s == vmSymbols::java_lang_Boolean() ||
aoqi@0 2479 s == vmSymbols::java_lang_Character() ||
aoqi@0 2480 s == vmSymbols::java_lang_Byte() ||
aoqi@0 2481 s == vmSymbols::java_lang_Short() ||
aoqi@0 2482 s == vmSymbols::java_lang_Integer() ||
aoqi@0 2483 s == vmSymbols::java_lang_Float()) {
aoqi@0 2484 out_sig_bt[total_c_args-1] = T_INT;
aoqi@0 2485 } else if (s == vmSymbols::java_lang_Long() ||
aoqi@0 2486 s == vmSymbols::java_lang_Double()) {
aoqi@0 2487 out_sig_bt[total_c_args-1] = T_LONG;
aoqi@0 2488 out_sig_bt[total_c_args++] = T_VOID;
aoqi@0 2489 }
aoqi@0 2490 } else if ( bt == T_LONG || bt == T_DOUBLE ) {
aoqi@0 2491 in_sig_bt[i++] = T_VOID; // Longs & doubles take 2 Java slots
aoqi@0 2492 out_sig_bt[total_c_args++] = T_VOID;
aoqi@0 2493 }
aoqi@0 2494 }
aoqi@0 2495
aoqi@0 2496 assert(i==total_args_passed, "validly parsed signature");
aoqi@0 2497
aoqi@0 2498 // Now get the compiled-Java layout as input arguments
aoqi@0 2499 int comp_args_on_stack;
aoqi@0 2500 comp_args_on_stack = SharedRuntime::java_calling_convention(
aoqi@0 2501 in_sig_bt, in_regs, total_args_passed, false);
aoqi@0 2502
aoqi@0 2503 // Now figure out where the args must be stored and how much stack space
aoqi@0 2504 // they require (neglecting out_preserve_stack_slots).
aoqi@0 2505
aoqi@0 2506 int out_arg_slots;
aoqi@0 2507 out_arg_slots = c_calling_convention(out_sig_bt, out_regs, NULL, total_c_args);
aoqi@0 2508
aoqi@0 2509 // Calculate the total number of stack slots we will need.
aoqi@0 2510
aoqi@0 2511 // First count the abi requirement plus all of the outgoing args
aoqi@0 2512 int stack_slots = SharedRuntime::out_preserve_stack_slots() + out_arg_slots;
aoqi@0 2513
aoqi@0 2514 // Now space for the string(s) we must convert
aoqi@0 2515
aoqi@0 2516 int* string_locs = NEW_RESOURCE_ARRAY(int, total_strings + 1);
aoqi@0 2517 for (i = 0; i < total_strings ; i++) {
aoqi@0 2518 string_locs[i] = stack_slots;
aoqi@0 2519 stack_slots += max_dtrace_string_size / VMRegImpl::stack_slot_size;
aoqi@0 2520 }
aoqi@0 2521
aoqi@0 2522 // + 2 for return address (which we own) and saved rbp,
aoqi@0 2523
aoqi@0 2524 stack_slots += 2;
aoqi@0 2525
aoqi@0 2526 // Ok The space we have allocated will look like:
aoqi@0 2527 //
aoqi@0 2528 //
aoqi@0 2529 // FP-> | |
aoqi@0 2530 // |---------------------|
aoqi@0 2531 // | string[n] |
aoqi@0 2532 // |---------------------| <- string_locs[n]
aoqi@0 2533 // | string[n-1] |
aoqi@0 2534 // |---------------------| <- string_locs[n-1]
aoqi@0 2535 // | ... |
aoqi@0 2536 // | ... |
aoqi@0 2537 // |---------------------| <- string_locs[1]
aoqi@0 2538 // | string[0] |
aoqi@0 2539 // |---------------------| <- string_locs[0]
aoqi@0 2540 // | outbound memory |
aoqi@0 2541 // | based arguments |
aoqi@0 2542 // | |
aoqi@0 2543 // |---------------------|
aoqi@0 2544 // | |
aoqi@0 2545 // SP-> | out_preserved_slots |
aoqi@0 2546 //
aoqi@0 2547 //
aoqi@0 2548
aoqi@0 2549 // Now compute actual number of stack words we need rounding to make
aoqi@0 2550 // stack properly aligned.
aoqi@0 2551 stack_slots = round_to(stack_slots, 2 * VMRegImpl::slots_per_word);
aoqi@0 2552
aoqi@0 2553 int stack_size = stack_slots * VMRegImpl::stack_slot_size;
aoqi@0 2554
aoqi@0 2555 intptr_t start = (intptr_t)__ pc();
aoqi@0 2556
aoqi@0 2557 // First thing make an ic check to see if we should even be here
aoqi@0 2558
aoqi@0 2559 // We are free to use all registers as temps without saving them and
aoqi@0 2560 // restoring them except rbp. rbp, is the only callee save register
aoqi@0 2561 // as far as the interpreter and the compiler(s) are concerned.
aoqi@0 2562
aoqi@0 2563 const Register ic_reg = rax;
aoqi@0 2564 const Register receiver = rcx;
aoqi@0 2565 Label hit;
aoqi@0 2566 Label exception_pending;
aoqi@0 2567
aoqi@0 2568
aoqi@0 2569 __ verify_oop(receiver);
aoqi@0 2570 __ cmpl(ic_reg, Address(receiver, oopDesc::klass_offset_in_bytes()));
aoqi@0 2571 __ jcc(Assembler::equal, hit);
aoqi@0 2572
aoqi@0 2573 __ jump(RuntimeAddress(SharedRuntime::get_ic_miss_stub()));
aoqi@0 2574
aoqi@0 2575 // verified entry must be aligned for code patching.
aoqi@0 2576 // and the first 5 bytes must be in the same cache line
aoqi@0 2577 // if we align at 8 then we will be sure 5 bytes are in the same line
aoqi@0 2578 __ align(8);
aoqi@0 2579
aoqi@0 2580 __ bind(hit);
aoqi@0 2581
aoqi@0 2582 int vep_offset = ((intptr_t)__ pc()) - start;
aoqi@0 2583
aoqi@0 2584
aoqi@0 2585 // The instruction at the verified entry point must be 5 bytes or longer
aoqi@0 2586 // because it can be patched on the fly by make_non_entrant. The stack bang
aoqi@0 2587 // instruction fits that requirement.
aoqi@0 2588
aoqi@0 2589 // Generate stack overflow check
aoqi@0 2590
aoqi@0 2591
aoqi@0 2592 if (UseStackBanging) {
aoqi@0 2593 if (stack_size <= StackShadowPages*os::vm_page_size()) {
aoqi@0 2594 __ bang_stack_with_offset(StackShadowPages*os::vm_page_size());
aoqi@0 2595 } else {
aoqi@0 2596 __ movl(rax, stack_size);
aoqi@0 2597 __ bang_stack_size(rax, rbx);
aoqi@0 2598 }
aoqi@0 2599 } else {
aoqi@0 2600 // need a 5 byte instruction to allow MT safe patching to non-entrant
aoqi@0 2601 __ fat_nop();
aoqi@0 2602 }
aoqi@0 2603
aoqi@0 2604 assert(((int)__ pc() - start - vep_offset) >= 5,
aoqi@0 2605 "valid size for make_non_entrant");
aoqi@0 2606
aoqi@0 2607 // Generate a new frame for the wrapper.
aoqi@0 2608 __ enter();
aoqi@0 2609
aoqi@0 2610 // -2 because return address is already present and so is saved rbp,
aoqi@0 2611 if (stack_size - 2*wordSize != 0) {
aoqi@0 2612 __ subl(rsp, stack_size - 2*wordSize);
aoqi@0 2613 }
aoqi@0 2614
aoqi@0 2615 // Frame is now completed as far a size and linkage.
aoqi@0 2616
aoqi@0 2617 int frame_complete = ((intptr_t)__ pc()) - start;
aoqi@0 2618
aoqi@0 2619 // First thing we do store all the args as if we are doing the call.
aoqi@0 2620 // Since the C calling convention is stack based that ensures that
aoqi@0 2621 // all the Java register args are stored before we need to convert any
aoqi@0 2622 // string we might have.
aoqi@0 2623
aoqi@0 2624 int sid = 0;
aoqi@0 2625 int c_arg, j_arg;
aoqi@0 2626 int string_reg = 0;
aoqi@0 2627
aoqi@0 2628 for (j_arg = first_arg_to_pass, c_arg = 0 ;
aoqi@0 2629 j_arg < total_args_passed ; j_arg++, c_arg++ ) {
aoqi@0 2630
aoqi@0 2631 VMRegPair src = in_regs[j_arg];
aoqi@0 2632 VMRegPair dst = out_regs[c_arg];
aoqi@0 2633 assert(dst.first()->is_stack() || in_sig_bt[j_arg] == T_VOID,
aoqi@0 2634 "stack based abi assumed");
aoqi@0 2635
aoqi@0 2636 switch (in_sig_bt[j_arg]) {
aoqi@0 2637
aoqi@0 2638 case T_ARRAY:
aoqi@0 2639 case T_OBJECT:
aoqi@0 2640 if (out_sig_bt[c_arg] == T_ADDRESS) {
aoqi@0 2641 // Any register based arg for a java string after the first
aoqi@0 2642 // will be destroyed by the call to get_utf so we store
aoqi@0 2643 // the original value in the location the utf string address
aoqi@0 2644 // will eventually be stored.
aoqi@0 2645 if (src.first()->is_reg()) {
aoqi@0 2646 if (string_reg++ != 0) {
aoqi@0 2647 simple_move32(masm, src, dst);
aoqi@0 2648 }
aoqi@0 2649 }
aoqi@0 2650 } else if (out_sig_bt[c_arg] == T_INT || out_sig_bt[c_arg] == T_LONG) {
aoqi@0 2651 // need to unbox a one-word value
aoqi@0 2652 Register in_reg = rax;
aoqi@0 2653 if ( src.first()->is_reg() ) {
aoqi@0 2654 in_reg = src.first()->as_Register();
aoqi@0 2655 } else {
aoqi@0 2656 simple_move32(masm, src, in_reg->as_VMReg());
aoqi@0 2657 }
aoqi@0 2658 Label skipUnbox;
aoqi@0 2659 __ movl(Address(rsp, reg2offset_out(dst.first())), NULL_WORD);
aoqi@0 2660 if ( out_sig_bt[c_arg] == T_LONG ) {
aoqi@0 2661 __ movl(Address(rsp, reg2offset_out(dst.second())), NULL_WORD);
aoqi@0 2662 }
aoqi@0 2663 __ testl(in_reg, in_reg);
aoqi@0 2664 __ jcc(Assembler::zero, skipUnbox);
aoqi@0 2665 assert(dst.first()->is_stack() &&
aoqi@0 2666 (!dst.second()->is_valid() || dst.second()->is_stack()),
aoqi@0 2667 "value(s) must go into stack slots");
aoqi@0 2668
aoqi@0 2669 BasicType bt = out_sig_bt[c_arg];
aoqi@0 2670 int box_offset = java_lang_boxing_object::value_offset_in_bytes(bt);
aoqi@0 2671 if ( bt == T_LONG ) {
aoqi@0 2672 __ movl(rbx, Address(in_reg,
aoqi@0 2673 box_offset + VMRegImpl::stack_slot_size));
aoqi@0 2674 __ movl(Address(rsp, reg2offset_out(dst.second())), rbx);
aoqi@0 2675 }
aoqi@0 2676 __ movl(in_reg, Address(in_reg, box_offset));
aoqi@0 2677 __ movl(Address(rsp, reg2offset_out(dst.first())), in_reg);
aoqi@0 2678 __ bind(skipUnbox);
aoqi@0 2679 } else {
aoqi@0 2680 // Convert the arg to NULL
aoqi@0 2681 __ movl(Address(rsp, reg2offset_out(dst.first())), NULL_WORD);
aoqi@0 2682 }
aoqi@0 2683 if (out_sig_bt[c_arg] == T_LONG) {
aoqi@0 2684 assert(out_sig_bt[c_arg+1] == T_VOID, "must be");
aoqi@0 2685 ++c_arg; // Move over the T_VOID To keep the loop indices in sync
aoqi@0 2686 }
aoqi@0 2687 break;
aoqi@0 2688
aoqi@0 2689 case T_VOID:
aoqi@0 2690 break;
aoqi@0 2691
aoqi@0 2692 case T_FLOAT:
aoqi@0 2693 float_move(masm, src, dst);
aoqi@0 2694 break;
aoqi@0 2695
aoqi@0 2696 case T_DOUBLE:
aoqi@0 2697 assert( j_arg + 1 < total_args_passed &&
aoqi@0 2698 in_sig_bt[j_arg + 1] == T_VOID, "bad arg list");
aoqi@0 2699 double_move(masm, src, dst);
aoqi@0 2700 break;
aoqi@0 2701
aoqi@0 2702 case T_LONG :
aoqi@0 2703 long_move(masm, src, dst);
aoqi@0 2704 break;
aoqi@0 2705
aoqi@0 2706 case T_ADDRESS: assert(false, "found T_ADDRESS in java args");
aoqi@0 2707
aoqi@0 2708 default:
aoqi@0 2709 simple_move32(masm, src, dst);
aoqi@0 2710 }
aoqi@0 2711 }
aoqi@0 2712
aoqi@0 2713 // Now we must convert any string we have to utf8
aoqi@0 2714 //
aoqi@0 2715
aoqi@0 2716 for (sid = 0, j_arg = first_arg_to_pass, c_arg = 0 ;
aoqi@0 2717 sid < total_strings ; j_arg++, c_arg++ ) {
aoqi@0 2718
aoqi@0 2719 if (out_sig_bt[c_arg] == T_ADDRESS) {
aoqi@0 2720
aoqi@0 2721 Address utf8_addr = Address(
aoqi@0 2722 rsp, string_locs[sid++] * VMRegImpl::stack_slot_size);
aoqi@0 2723 __ leal(rax, utf8_addr);
aoqi@0 2724
aoqi@0 2725 // The first string we find might still be in the original java arg
aoqi@0 2726 // register
aoqi@0 2727 VMReg orig_loc = in_regs[j_arg].first();
aoqi@0 2728 Register string_oop;
aoqi@0 2729
aoqi@0 2730 // This is where the argument will eventually reside
aoqi@0 2731 Address dest = Address(rsp, reg2offset_out(out_regs[c_arg].first()));
aoqi@0 2732
aoqi@0 2733 if (sid == 1 && orig_loc->is_reg()) {
aoqi@0 2734 string_oop = orig_loc->as_Register();
aoqi@0 2735 assert(string_oop != rax, "smashed arg");
aoqi@0 2736 } else {
aoqi@0 2737
aoqi@0 2738 if (orig_loc->is_reg()) {
aoqi@0 2739 // Get the copy of the jls object
aoqi@0 2740 __ movl(rcx, dest);
aoqi@0 2741 } else {
aoqi@0 2742 // arg is still in the original location
aoqi@0 2743 __ movl(rcx, Address(rbp, reg2offset_in(orig_loc)));
aoqi@0 2744 }
aoqi@0 2745 string_oop = rcx;
aoqi@0 2746
aoqi@0 2747 }
aoqi@0 2748 Label nullString;
aoqi@0 2749 __ movl(dest, NULL_WORD);
aoqi@0 2750 __ testl(string_oop, string_oop);
aoqi@0 2751 __ jcc(Assembler::zero, nullString);
aoqi@0 2752
aoqi@0 2753 // Now we can store the address of the utf string as the argument
aoqi@0 2754 __ movl(dest, rax);
aoqi@0 2755
aoqi@0 2756 // And do the conversion
aoqi@0 2757 __ call_VM_leaf(CAST_FROM_FN_PTR(
aoqi@0 2758 address, SharedRuntime::get_utf), string_oop, rax);
aoqi@0 2759 __ bind(nullString);
aoqi@0 2760 }
aoqi@0 2761
aoqi@0 2762 if (in_sig_bt[j_arg] == T_OBJECT && out_sig_bt[c_arg] == T_LONG) {
aoqi@0 2763 assert(out_sig_bt[c_arg+1] == T_VOID, "must be");
aoqi@0 2764 ++c_arg; // Move over the T_VOID To keep the loop indices in sync
aoqi@0 2765 }
aoqi@0 2766 }
aoqi@0 2767
aoqi@0 2768
aoqi@0 2769 // Ok now we are done. Need to place the nop that dtrace wants in order to
aoqi@0 2770 // patch in the trap
aoqi@0 2771
aoqi@0 2772 int patch_offset = ((intptr_t)__ pc()) - start;
aoqi@0 2773
aoqi@0 2774 __ nop();
aoqi@0 2775
aoqi@0 2776
aoqi@0 2777 // Return
aoqi@0 2778
aoqi@0 2779 __ leave();
aoqi@0 2780 __ ret(0);
aoqi@0 2781
aoqi@0 2782 __ flush();
aoqi@0 2783
aoqi@0 2784 nmethod *nm = nmethod::new_dtrace_nmethod(
aoqi@0 2785 method, masm->code(), vep_offset, patch_offset, frame_complete,
aoqi@0 2786 stack_slots / VMRegImpl::slots_per_word);
aoqi@0 2787 return nm;
aoqi@0 2788
aoqi@0 2789 }
aoqi@0 2790
aoqi@0 2791 #endif // HAVE_DTRACE_H
aoqi@0 2792
aoqi@0 2793 // this function returns the adjust size (in number of words) to a c2i adapter
aoqi@0 2794 // activation for use during deoptimization
aoqi@0 2795 int Deoptimization::last_frame_adjust(int callee_parameters, int callee_locals ) {
aoqi@0 2796 return (callee_locals - callee_parameters) * Interpreter::stackElementWords;
aoqi@0 2797 }
aoqi@0 2798
aoqi@0 2799
aoqi@0 2800 uint SharedRuntime::out_preserve_stack_slots() {
aoqi@0 2801 return 0;
aoqi@0 2802 }
aoqi@0 2803
aoqi@0 2804 //------------------------------generate_deopt_blob----------------------------
aoqi@0 2805 void SharedRuntime::generate_deopt_blob() {
aoqi@0 2806 // allocate space for the code
aoqi@0 2807 ResourceMark rm;
aoqi@0 2808 // setup code generation tools
aoqi@0 2809 CodeBuffer buffer("deopt_blob", 1024, 1024);
aoqi@0 2810 MacroAssembler* masm = new MacroAssembler(&buffer);
aoqi@0 2811 int frame_size_in_words;
aoqi@0 2812 OopMap* map = NULL;
aoqi@0 2813 // Account for the extra args we place on the stack
aoqi@0 2814 // by the time we call fetch_unroll_info
aoqi@0 2815 const int additional_words = 2; // deopt kind, thread
aoqi@0 2816
aoqi@0 2817 OopMapSet *oop_maps = new OopMapSet();
aoqi@0 2818
aoqi@0 2819 // -------------
aoqi@0 2820 // This code enters when returning to a de-optimized nmethod. A return
aoqi@0 2821 // address has been pushed on the the stack, and return values are in
aoqi@0 2822 // registers.
aoqi@0 2823 // If we are doing a normal deopt then we were called from the patched
aoqi@0 2824 // nmethod from the point we returned to the nmethod. So the return
aoqi@0 2825 // address on the stack is wrong by NativeCall::instruction_size
aoqi@0 2826 // We will adjust the value to it looks like we have the original return
aoqi@0 2827 // address on the stack (like when we eagerly deoptimized).
aoqi@0 2828 // In the case of an exception pending with deoptimized then we enter
aoqi@0 2829 // with a return address on the stack that points after the call we patched
aoqi@0 2830 // into the exception handler. We have the following register state:
aoqi@0 2831 // rax,: exception
aoqi@0 2832 // rbx,: exception handler
aoqi@0 2833 // rdx: throwing pc
aoqi@0 2834 // So in this case we simply jam rdx into the useless return address and
aoqi@0 2835 // the stack looks just like we want.
aoqi@0 2836 //
aoqi@0 2837 // At this point we need to de-opt. We save the argument return
aoqi@0 2838 // registers. We call the first C routine, fetch_unroll_info(). This
aoqi@0 2839 // routine captures the return values and returns a structure which
aoqi@0 2840 // describes the current frame size and the sizes of all replacement frames.
aoqi@0 2841 // The current frame is compiled code and may contain many inlined
aoqi@0 2842 // functions, each with their own JVM state. We pop the current frame, then
aoqi@0 2843 // push all the new frames. Then we call the C routine unpack_frames() to
aoqi@0 2844 // populate these frames. Finally unpack_frames() returns us the new target
aoqi@0 2845 // address. Notice that callee-save registers are BLOWN here; they have
aoqi@0 2846 // already been captured in the vframeArray at the time the return PC was
aoqi@0 2847 // patched.
aoqi@0 2848 address start = __ pc();
aoqi@0 2849 Label cont;
aoqi@0 2850
aoqi@0 2851 // Prolog for non exception case!
aoqi@0 2852
aoqi@0 2853 // Save everything in sight.
aoqi@0 2854
aoqi@0 2855 map = RegisterSaver::save_live_registers(masm, additional_words, &frame_size_in_words, false);
aoqi@0 2856 // Normal deoptimization
aoqi@0 2857 __ push(Deoptimization::Unpack_deopt);
aoqi@0 2858 __ jmp(cont);
aoqi@0 2859
aoqi@0 2860 int reexecute_offset = __ pc() - start;
aoqi@0 2861
aoqi@0 2862 // Reexecute case
aoqi@0 2863 // return address is the pc describes what bci to do re-execute at
aoqi@0 2864
aoqi@0 2865 // No need to update map as each call to save_live_registers will produce identical oopmap
aoqi@0 2866 (void) RegisterSaver::save_live_registers(masm, additional_words, &frame_size_in_words, false);
aoqi@0 2867
aoqi@0 2868 __ push(Deoptimization::Unpack_reexecute);
aoqi@0 2869 __ jmp(cont);
aoqi@0 2870
aoqi@0 2871 int exception_offset = __ pc() - start;
aoqi@0 2872
aoqi@0 2873 // Prolog for exception case
aoqi@0 2874
aoqi@0 2875 // all registers are dead at this entry point, except for rax, and
aoqi@0 2876 // rdx which contain the exception oop and exception pc
aoqi@0 2877 // respectively. Set them in TLS and fall thru to the
aoqi@0 2878 // unpack_with_exception_in_tls entry point.
aoqi@0 2879
aoqi@0 2880 __ get_thread(rdi);
aoqi@0 2881 __ movptr(Address(rdi, JavaThread::exception_pc_offset()), rdx);
aoqi@0 2882 __ movptr(Address(rdi, JavaThread::exception_oop_offset()), rax);
aoqi@0 2883
aoqi@0 2884 int exception_in_tls_offset = __ pc() - start;
aoqi@0 2885
aoqi@0 2886 // new implementation because exception oop is now passed in JavaThread
aoqi@0 2887
aoqi@0 2888 // Prolog for exception case
aoqi@0 2889 // All registers must be preserved because they might be used by LinearScan
aoqi@0 2890 // Exceptiop oop and throwing PC are passed in JavaThread
aoqi@0 2891 // tos: stack at point of call to method that threw the exception (i.e. only
aoqi@0 2892 // args are on the stack, no return address)
aoqi@0 2893
aoqi@0 2894 // make room on stack for the return address
aoqi@0 2895 // It will be patched later with the throwing pc. The correct value is not
aoqi@0 2896 // available now because loading it from memory would destroy registers.
aoqi@0 2897 __ push(0);
aoqi@0 2898
aoqi@0 2899 // Save everything in sight.
aoqi@0 2900
aoqi@0 2901 // No need to update map as each call to save_live_registers will produce identical oopmap
aoqi@0 2902 (void) RegisterSaver::save_live_registers(masm, additional_words, &frame_size_in_words, false);
aoqi@0 2903
aoqi@0 2904 // Now it is safe to overwrite any register
aoqi@0 2905
aoqi@0 2906 // store the correct deoptimization type
aoqi@0 2907 __ push(Deoptimization::Unpack_exception);
aoqi@0 2908
aoqi@0 2909 // load throwing pc from JavaThread and patch it as the return address
aoqi@0 2910 // of the current frame. Then clear the field in JavaThread
aoqi@0 2911 __ get_thread(rdi);
aoqi@0 2912 __ movptr(rdx, Address(rdi, JavaThread::exception_pc_offset()));
aoqi@0 2913 __ movptr(Address(rbp, wordSize), rdx);
aoqi@0 2914 __ movptr(Address(rdi, JavaThread::exception_pc_offset()), NULL_WORD);
aoqi@0 2915
aoqi@0 2916 #ifdef ASSERT
aoqi@0 2917 // verify that there is really an exception oop in JavaThread
aoqi@0 2918 __ movptr(rax, Address(rdi, JavaThread::exception_oop_offset()));
aoqi@0 2919 __ verify_oop(rax);
aoqi@0 2920
aoqi@0 2921 // verify that there is no pending exception
aoqi@0 2922 Label no_pending_exception;
aoqi@0 2923 __ movptr(rax, Address(rdi, Thread::pending_exception_offset()));
aoqi@0 2924 __ testptr(rax, rax);
aoqi@0 2925 __ jcc(Assembler::zero, no_pending_exception);
aoqi@0 2926 __ stop("must not have pending exception here");
aoqi@0 2927 __ bind(no_pending_exception);
aoqi@0 2928 #endif
aoqi@0 2929
aoqi@0 2930 __ bind(cont);
aoqi@0 2931
aoqi@0 2932 // Compiled code leaves the floating point stack dirty, empty it.
aoqi@0 2933 __ empty_FPU_stack();
aoqi@0 2934
aoqi@0 2935
aoqi@0 2936 // Call C code. Need thread and this frame, but NOT official VM entry
aoqi@0 2937 // crud. We cannot block on this call, no GC can happen.
aoqi@0 2938 __ get_thread(rcx);
aoqi@0 2939 __ push(rcx);
aoqi@0 2940 // fetch_unroll_info needs to call last_java_frame()
aoqi@0 2941 __ set_last_Java_frame(rcx, noreg, noreg, NULL);
aoqi@0 2942
aoqi@0 2943 __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, Deoptimization::fetch_unroll_info)));
aoqi@0 2944
aoqi@0 2945 // Need to have an oopmap that tells fetch_unroll_info where to
aoqi@0 2946 // find any register it might need.
aoqi@0 2947
aoqi@0 2948 oop_maps->add_gc_map( __ pc()-start, map);
aoqi@0 2949
aoqi@0 2950 // Discard arg to fetch_unroll_info
aoqi@0 2951 __ pop(rcx);
aoqi@0 2952
aoqi@0 2953 __ get_thread(rcx);
aoqi@0 2954 __ reset_last_Java_frame(rcx, false, false);
aoqi@0 2955
aoqi@0 2956 // Load UnrollBlock into EDI
aoqi@0 2957 __ mov(rdi, rax);
aoqi@0 2958
aoqi@0 2959 // Move the unpack kind to a safe place in the UnrollBlock because
aoqi@0 2960 // we are very short of registers
aoqi@0 2961
aoqi@0 2962 Address unpack_kind(rdi, Deoptimization::UnrollBlock::unpack_kind_offset_in_bytes());
aoqi@0 2963 // retrieve the deopt kind from where we left it.
aoqi@0 2964 __ pop(rax);
aoqi@0 2965 __ movl(unpack_kind, rax); // save the unpack_kind value
aoqi@0 2966
aoqi@0 2967 Label noException;
aoqi@0 2968 __ cmpl(rax, Deoptimization::Unpack_exception); // Was exception pending?
aoqi@0 2969 __ jcc(Assembler::notEqual, noException);
aoqi@0 2970 __ movptr(rax, Address(rcx, JavaThread::exception_oop_offset()));
aoqi@0 2971 __ movptr(rdx, Address(rcx, JavaThread::exception_pc_offset()));
aoqi@0 2972 __ movptr(Address(rcx, JavaThread::exception_oop_offset()), NULL_WORD);
aoqi@0 2973 __ movptr(Address(rcx, JavaThread::exception_pc_offset()), NULL_WORD);
aoqi@0 2974
aoqi@0 2975 __ verify_oop(rax);
aoqi@0 2976
aoqi@0 2977 // Overwrite the result registers with the exception results.
aoqi@0 2978 __ movptr(Address(rsp, RegisterSaver::raxOffset()*wordSize), rax);
aoqi@0 2979 __ movptr(Address(rsp, RegisterSaver::rdxOffset()*wordSize), rdx);
aoqi@0 2980
aoqi@0 2981 __ bind(noException);
aoqi@0 2982
aoqi@0 2983 // Stack is back to only having register save data on the stack.
aoqi@0 2984 // Now restore the result registers. Everything else is either dead or captured
aoqi@0 2985 // in the vframeArray.
aoqi@0 2986
aoqi@0 2987 RegisterSaver::restore_result_registers(masm);
aoqi@0 2988
aoqi@0 2989 // Non standard control word may be leaked out through a safepoint blob, and we can
aoqi@0 2990 // deopt at a poll point with the non standard control word. However, we should make
aoqi@0 2991 // sure the control word is correct after restore_result_registers.
aoqi@0 2992 __ fldcw(ExternalAddress(StubRoutines::addr_fpu_cntrl_wrd_std()));
aoqi@0 2993
aoqi@0 2994 // All of the register save area has been popped of the stack. Only the
aoqi@0 2995 // return address remains.
aoqi@0 2996
aoqi@0 2997 // Pop all the frames we must move/replace.
aoqi@0 2998 //
aoqi@0 2999 // Frame picture (youngest to oldest)
aoqi@0 3000 // 1: self-frame (no frame link)
aoqi@0 3001 // 2: deopting frame (no frame link)
aoqi@0 3002 // 3: caller of deopting frame (could be compiled/interpreted).
aoqi@0 3003 //
aoqi@0 3004 // Note: by leaving the return address of self-frame on the stack
aoqi@0 3005 // and using the size of frame 2 to adjust the stack
aoqi@0 3006 // when we are done the return to frame 3 will still be on the stack.
aoqi@0 3007
aoqi@0 3008 // Pop deoptimized frame
aoqi@0 3009 __ addptr(rsp, Address(rdi,Deoptimization::UnrollBlock::size_of_deoptimized_frame_offset_in_bytes()));
aoqi@0 3010
aoqi@0 3011 // sp should be pointing at the return address to the caller (3)
aoqi@0 3012
aoqi@0 3013 // Pick up the initial fp we should save
aoqi@0 3014 // restore rbp before stack bang because if stack overflow is thrown it needs to be pushed (and preserved)
aoqi@0 3015 __ movptr(rbp, Address(rdi, Deoptimization::UnrollBlock::initial_info_offset_in_bytes()));
aoqi@0 3016
aoqi@0 3017 #ifdef ASSERT
aoqi@0 3018 // Compilers generate code that bang the stack by as much as the
aoqi@0 3019 // interpreter would need. So this stack banging should never
aoqi@0 3020 // trigger a fault. Verify that it does not on non product builds.
aoqi@0 3021 if (UseStackBanging) {
aoqi@0 3022 __ movl(rbx, Address(rdi ,Deoptimization::UnrollBlock::total_frame_sizes_offset_in_bytes()));
aoqi@0 3023 __ bang_stack_size(rbx, rcx);
aoqi@0 3024 }
aoqi@0 3025 #endif
aoqi@0 3026
aoqi@0 3027 // Load array of frame pcs into ECX
aoqi@0 3028 __ movptr(rcx,Address(rdi,Deoptimization::UnrollBlock::frame_pcs_offset_in_bytes()));
aoqi@0 3029
aoqi@0 3030 __ pop(rsi); // trash the old pc
aoqi@0 3031
aoqi@0 3032 // Load array of frame sizes into ESI
aoqi@0 3033 __ movptr(rsi,Address(rdi,Deoptimization::UnrollBlock::frame_sizes_offset_in_bytes()));
aoqi@0 3034
aoqi@0 3035 Address counter(rdi, Deoptimization::UnrollBlock::counter_temp_offset_in_bytes());
aoqi@0 3036
aoqi@0 3037 __ movl(rbx, Address(rdi, Deoptimization::UnrollBlock::number_of_frames_offset_in_bytes()));
aoqi@0 3038 __ movl(counter, rbx);
aoqi@0 3039
aoqi@0 3040 // Now adjust the caller's stack to make up for the extra locals
aoqi@0 3041 // but record the original sp so that we can save it in the skeletal interpreter
aoqi@0 3042 // frame and the stack walking of interpreter_sender will get the unextended sp
aoqi@0 3043 // value and not the "real" sp value.
aoqi@0 3044
aoqi@0 3045 Address sp_temp(rdi, Deoptimization::UnrollBlock::sender_sp_temp_offset_in_bytes());
aoqi@0 3046 __ movptr(sp_temp, rsp);
aoqi@0 3047 __ movl2ptr(rbx, Address(rdi, Deoptimization::UnrollBlock::caller_adjustment_offset_in_bytes()));
aoqi@0 3048 __ subptr(rsp, rbx);
aoqi@0 3049
aoqi@0 3050 // Push interpreter frames in a loop
aoqi@0 3051 Label loop;
aoqi@0 3052 __ bind(loop);
aoqi@0 3053 __ movptr(rbx, Address(rsi, 0)); // Load frame size
aoqi@0 3054 #ifdef CC_INTERP
aoqi@0 3055 __ subptr(rbx, 4*wordSize); // we'll push pc and ebp by hand and
aoqi@0 3056 #ifdef ASSERT
aoqi@0 3057 __ push(0xDEADDEAD); // Make a recognizable pattern
aoqi@0 3058 __ push(0xDEADDEAD);
aoqi@0 3059 #else /* ASSERT */
aoqi@0 3060 __ subptr(rsp, 2*wordSize); // skip the "static long no_param"
aoqi@0 3061 #endif /* ASSERT */
aoqi@0 3062 #else /* CC_INTERP */
aoqi@0 3063 __ subptr(rbx, 2*wordSize); // we'll push pc and rbp, by hand
aoqi@0 3064 #endif /* CC_INTERP */
aoqi@0 3065 __ pushptr(Address(rcx, 0)); // save return address
aoqi@0 3066 __ enter(); // save old & set new rbp,
aoqi@0 3067 __ subptr(rsp, rbx); // Prolog!
aoqi@0 3068 __ movptr(rbx, sp_temp); // sender's sp
aoqi@0 3069 #ifdef CC_INTERP
aoqi@0 3070 __ movptr(Address(rbp,
aoqi@0 3071 -(sizeof(BytecodeInterpreter)) + in_bytes(byte_offset_of(BytecodeInterpreter, _sender_sp))),
aoqi@0 3072 rbx); // Make it walkable
aoqi@0 3073 #else /* CC_INTERP */
aoqi@0 3074 // This value is corrected by layout_activation_impl
aoqi@0 3075 __ movptr(Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize), NULL_WORD);
aoqi@0 3076 __ movptr(Address(rbp, frame::interpreter_frame_sender_sp_offset * wordSize), rbx); // Make it walkable
aoqi@0 3077 #endif /* CC_INTERP */
aoqi@0 3078 __ movptr(sp_temp, rsp); // pass to next frame
aoqi@0 3079 __ addptr(rsi, wordSize); // Bump array pointer (sizes)
aoqi@0 3080 __ addptr(rcx, wordSize); // Bump array pointer (pcs)
aoqi@0 3081 __ decrementl(counter); // decrement counter
aoqi@0 3082 __ jcc(Assembler::notZero, loop);
aoqi@0 3083 __ pushptr(Address(rcx, 0)); // save final return address
aoqi@0 3084
aoqi@0 3085 // Re-push self-frame
aoqi@0 3086 __ enter(); // save old & set new rbp,
aoqi@0 3087
aoqi@0 3088 // Return address and rbp, are in place
aoqi@0 3089 // We'll push additional args later. Just allocate a full sized
aoqi@0 3090 // register save area
aoqi@0 3091 __ subptr(rsp, (frame_size_in_words-additional_words - 2) * wordSize);
aoqi@0 3092
aoqi@0 3093 // Restore frame locals after moving the frame
aoqi@0 3094 __ movptr(Address(rsp, RegisterSaver::raxOffset()*wordSize), rax);
aoqi@0 3095 __ movptr(Address(rsp, RegisterSaver::rdxOffset()*wordSize), rdx);
aoqi@0 3096 __ fstp_d(Address(rsp, RegisterSaver::fpResultOffset()*wordSize)); // Pop float stack and store in local
aoqi@0 3097 if( UseSSE>=2 ) __ movdbl(Address(rsp, RegisterSaver::xmm0Offset()*wordSize), xmm0);
aoqi@0 3098 if( UseSSE==1 ) __ movflt(Address(rsp, RegisterSaver::xmm0Offset()*wordSize), xmm0);
aoqi@0 3099
aoqi@0 3100 // Set up the args to unpack_frame
aoqi@0 3101
aoqi@0 3102 __ pushl(unpack_kind); // get the unpack_kind value
aoqi@0 3103 __ get_thread(rcx);
aoqi@0 3104 __ push(rcx);
aoqi@0 3105
aoqi@0 3106 // set last_Java_sp, last_Java_fp
aoqi@0 3107 __ set_last_Java_frame(rcx, noreg, rbp, NULL);
aoqi@0 3108
aoqi@0 3109 // Call C code. Need thread but NOT official VM entry
aoqi@0 3110 // crud. We cannot block on this call, no GC can happen. Call should
aoqi@0 3111 // restore return values to their stack-slots with the new SP.
aoqi@0 3112 __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, Deoptimization::unpack_frames)));
aoqi@0 3113 // Set an oopmap for the call site
aoqi@0 3114 oop_maps->add_gc_map( __ pc()-start, new OopMap( frame_size_in_words, 0 ));
aoqi@0 3115
aoqi@0 3116 // rax, contains the return result type
aoqi@0 3117 __ push(rax);
aoqi@0 3118
aoqi@0 3119 __ get_thread(rcx);
aoqi@0 3120 __ reset_last_Java_frame(rcx, false, false);
aoqi@0 3121
aoqi@0 3122 // Collect return values
aoqi@0 3123 __ movptr(rax,Address(rsp, (RegisterSaver::raxOffset() + additional_words + 1)*wordSize));
aoqi@0 3124 __ movptr(rdx,Address(rsp, (RegisterSaver::rdxOffset() + additional_words + 1)*wordSize));
aoqi@0 3125
aoqi@0 3126 // Clear floating point stack before returning to interpreter
aoqi@0 3127 __ empty_FPU_stack();
aoqi@0 3128
aoqi@0 3129 // Check if we should push the float or double return value.
aoqi@0 3130 Label results_done, yes_double_value;
aoqi@0 3131 __ cmpl(Address(rsp, 0), T_DOUBLE);
aoqi@0 3132 __ jcc (Assembler::zero, yes_double_value);
aoqi@0 3133 __ cmpl(Address(rsp, 0), T_FLOAT);
aoqi@0 3134 __ jcc (Assembler::notZero, results_done);
aoqi@0 3135
aoqi@0 3136 // return float value as expected by interpreter
aoqi@0 3137 if( UseSSE>=1 ) __ movflt(xmm0, Address(rsp, (RegisterSaver::xmm0Offset() + additional_words + 1)*wordSize));
aoqi@0 3138 else __ fld_d(Address(rsp, (RegisterSaver::fpResultOffset() + additional_words + 1)*wordSize));
aoqi@0 3139 __ jmp(results_done);
aoqi@0 3140
aoqi@0 3141 // return double value as expected by interpreter
aoqi@0 3142 __ bind(yes_double_value);
aoqi@0 3143 if( UseSSE>=2 ) __ movdbl(xmm0, Address(rsp, (RegisterSaver::xmm0Offset() + additional_words + 1)*wordSize));
aoqi@0 3144 else __ fld_d(Address(rsp, (RegisterSaver::fpResultOffset() + additional_words + 1)*wordSize));
aoqi@0 3145
aoqi@0 3146 __ bind(results_done);
aoqi@0 3147
aoqi@0 3148 // Pop self-frame.
aoqi@0 3149 __ leave(); // Epilog!
aoqi@0 3150
aoqi@0 3151 // Jump to interpreter
aoqi@0 3152 __ ret(0);
aoqi@0 3153
aoqi@0 3154 // -------------
aoqi@0 3155 // make sure all code is generated
aoqi@0 3156 masm->flush();
aoqi@0 3157
aoqi@0 3158 _deopt_blob = DeoptimizationBlob::create( &buffer, oop_maps, 0, exception_offset, reexecute_offset, frame_size_in_words);
aoqi@0 3159 _deopt_blob->set_unpack_with_exception_in_tls_offset(exception_in_tls_offset);
aoqi@0 3160 }
aoqi@0 3161
aoqi@0 3162
aoqi@0 3163 #ifdef COMPILER2
aoqi@0 3164 //------------------------------generate_uncommon_trap_blob--------------------
aoqi@0 3165 void SharedRuntime::generate_uncommon_trap_blob() {
aoqi@0 3166 // allocate space for the code
aoqi@0 3167 ResourceMark rm;
aoqi@0 3168 // setup code generation tools
aoqi@0 3169 CodeBuffer buffer("uncommon_trap_blob", 512, 512);
aoqi@0 3170 MacroAssembler* masm = new MacroAssembler(&buffer);
aoqi@0 3171
aoqi@0 3172 enum frame_layout {
aoqi@0 3173 arg0_off, // thread sp + 0 // Arg location for
aoqi@0 3174 arg1_off, // unloaded_class_index sp + 1 // calling C
aoqi@0 3175 // The frame sender code expects that rbp will be in the "natural" place and
aoqi@0 3176 // will override any oopMap setting for it. We must therefore force the layout
aoqi@0 3177 // so that it agrees with the frame sender code.
aoqi@0 3178 rbp_off, // callee saved register sp + 2
aoqi@0 3179 return_off, // slot for return address sp + 3
aoqi@0 3180 framesize
aoqi@0 3181 };
aoqi@0 3182
aoqi@0 3183 address start = __ pc();
aoqi@0 3184
aoqi@0 3185 if (UseRTMLocking) {
aoqi@0 3186 // Abort RTM transaction before possible nmethod deoptimization.
aoqi@0 3187 __ xabort(0);
aoqi@0 3188 }
aoqi@0 3189
aoqi@0 3190 // Push self-frame.
aoqi@0 3191 __ subptr(rsp, return_off*wordSize); // Epilog!
aoqi@0 3192
aoqi@0 3193 // rbp, is an implicitly saved callee saved register (i.e. the calling
aoqi@0 3194 // convention will save restore it in prolog/epilog) Other than that
aoqi@0 3195 // there are no callee save registers no that adapter frames are gone.
aoqi@0 3196 __ movptr(Address(rsp, rbp_off*wordSize), rbp);
aoqi@0 3197
aoqi@0 3198 // Clear the floating point exception stack
aoqi@0 3199 __ empty_FPU_stack();
aoqi@0 3200
aoqi@0 3201 // set last_Java_sp
aoqi@0 3202 __ get_thread(rdx);
aoqi@0 3203 __ set_last_Java_frame(rdx, noreg, noreg, NULL);
aoqi@0 3204
aoqi@0 3205 // Call C code. Need thread but NOT official VM entry
aoqi@0 3206 // crud. We cannot block on this call, no GC can happen. Call should
aoqi@0 3207 // capture callee-saved registers as well as return values.
aoqi@0 3208 __ movptr(Address(rsp, arg0_off*wordSize), rdx);
aoqi@0 3209 // argument already in ECX
aoqi@0 3210 __ movl(Address(rsp, arg1_off*wordSize),rcx);
aoqi@0 3211 __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, Deoptimization::uncommon_trap)));
aoqi@0 3212
aoqi@0 3213 // Set an oopmap for the call site
aoqi@0 3214 OopMapSet *oop_maps = new OopMapSet();
aoqi@0 3215 OopMap* map = new OopMap( framesize, 0 );
aoqi@0 3216 // No oopMap for rbp, it is known implicitly
aoqi@0 3217
aoqi@0 3218 oop_maps->add_gc_map( __ pc()-start, map);
aoqi@0 3219
aoqi@0 3220 __ get_thread(rcx);
aoqi@0 3221
aoqi@0 3222 __ reset_last_Java_frame(rcx, false, false);
aoqi@0 3223
aoqi@0 3224 // Load UnrollBlock into EDI
aoqi@0 3225 __ movptr(rdi, rax);
aoqi@0 3226
aoqi@0 3227 // Pop all the frames we must move/replace.
aoqi@0 3228 //
aoqi@0 3229 // Frame picture (youngest to oldest)
aoqi@0 3230 // 1: self-frame (no frame link)
aoqi@0 3231 // 2: deopting frame (no frame link)
aoqi@0 3232 // 3: caller of deopting frame (could be compiled/interpreted).
aoqi@0 3233
aoqi@0 3234 // Pop self-frame. We have no frame, and must rely only on EAX and ESP.
aoqi@0 3235 __ addptr(rsp,(framesize-1)*wordSize); // Epilog!
aoqi@0 3236
aoqi@0 3237 // Pop deoptimized frame
aoqi@0 3238 __ movl2ptr(rcx, Address(rdi,Deoptimization::UnrollBlock::size_of_deoptimized_frame_offset_in_bytes()));
aoqi@0 3239 __ addptr(rsp, rcx);
aoqi@0 3240
aoqi@0 3241 // sp should be pointing at the return address to the caller (3)
aoqi@0 3242
aoqi@0 3243 // Pick up the initial fp we should save
aoqi@0 3244 // restore rbp before stack bang because if stack overflow is thrown it needs to be pushed (and preserved)
aoqi@0 3245 __ movptr(rbp, Address(rdi, Deoptimization::UnrollBlock::initial_info_offset_in_bytes()));
aoqi@0 3246
aoqi@0 3247 #ifdef ASSERT
aoqi@0 3248 // Compilers generate code that bang the stack by as much as the
aoqi@0 3249 // interpreter would need. So this stack banging should never
aoqi@0 3250 // trigger a fault. Verify that it does not on non product builds.
aoqi@0 3251 if (UseStackBanging) {
aoqi@0 3252 __ movl(rbx, Address(rdi ,Deoptimization::UnrollBlock::total_frame_sizes_offset_in_bytes()));
aoqi@0 3253 __ bang_stack_size(rbx, rcx);
aoqi@0 3254 }
aoqi@0 3255 #endif
aoqi@0 3256
aoqi@0 3257 // Load array of frame pcs into ECX
aoqi@0 3258 __ movl(rcx,Address(rdi,Deoptimization::UnrollBlock::frame_pcs_offset_in_bytes()));
aoqi@0 3259
aoqi@0 3260 __ pop(rsi); // trash the pc
aoqi@0 3261
aoqi@0 3262 // Load array of frame sizes into ESI
aoqi@0 3263 __ movptr(rsi,Address(rdi,Deoptimization::UnrollBlock::frame_sizes_offset_in_bytes()));
aoqi@0 3264
aoqi@0 3265 Address counter(rdi, Deoptimization::UnrollBlock::counter_temp_offset_in_bytes());
aoqi@0 3266
aoqi@0 3267 __ movl(rbx, Address(rdi, Deoptimization::UnrollBlock::number_of_frames_offset_in_bytes()));
aoqi@0 3268 __ movl(counter, rbx);
aoqi@0 3269
aoqi@0 3270 // Now adjust the caller's stack to make up for the extra locals
aoqi@0 3271 // but record the original sp so that we can save it in the skeletal interpreter
aoqi@0 3272 // frame and the stack walking of interpreter_sender will get the unextended sp
aoqi@0 3273 // value and not the "real" sp value.
aoqi@0 3274
aoqi@0 3275 Address sp_temp(rdi, Deoptimization::UnrollBlock::sender_sp_temp_offset_in_bytes());
aoqi@0 3276 __ movptr(sp_temp, rsp);
aoqi@0 3277 __ movl(rbx, Address(rdi, Deoptimization::UnrollBlock::caller_adjustment_offset_in_bytes()));
aoqi@0 3278 __ subptr(rsp, rbx);
aoqi@0 3279
aoqi@0 3280 // Push interpreter frames in a loop
aoqi@0 3281 Label loop;
aoqi@0 3282 __ bind(loop);
aoqi@0 3283 __ movptr(rbx, Address(rsi, 0)); // Load frame size
aoqi@0 3284 #ifdef CC_INTERP
aoqi@0 3285 __ subptr(rbx, 4*wordSize); // we'll push pc and ebp by hand and
aoqi@0 3286 #ifdef ASSERT
aoqi@0 3287 __ push(0xDEADDEAD); // Make a recognizable pattern
aoqi@0 3288 __ push(0xDEADDEAD); // (parm to RecursiveInterpreter...)
aoqi@0 3289 #else /* ASSERT */
aoqi@0 3290 __ subptr(rsp, 2*wordSize); // skip the "static long no_param"
aoqi@0 3291 #endif /* ASSERT */
aoqi@0 3292 #else /* CC_INTERP */
aoqi@0 3293 __ subptr(rbx, 2*wordSize); // we'll push pc and rbp, by hand
aoqi@0 3294 #endif /* CC_INTERP */
aoqi@0 3295 __ pushptr(Address(rcx, 0)); // save return address
aoqi@0 3296 __ enter(); // save old & set new rbp,
aoqi@0 3297 __ subptr(rsp, rbx); // Prolog!
aoqi@0 3298 __ movptr(rbx, sp_temp); // sender's sp
aoqi@0 3299 #ifdef CC_INTERP
aoqi@0 3300 __ movptr(Address(rbp,
aoqi@0 3301 -(sizeof(BytecodeInterpreter)) + in_bytes(byte_offset_of(BytecodeInterpreter, _sender_sp))),
aoqi@0 3302 rbx); // Make it walkable
aoqi@0 3303 #else /* CC_INTERP */
aoqi@0 3304 // This value is corrected by layout_activation_impl
aoqi@0 3305 __ movptr(Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize), NULL_WORD );
aoqi@0 3306 __ movptr(Address(rbp, frame::interpreter_frame_sender_sp_offset * wordSize), rbx); // Make it walkable
aoqi@0 3307 #endif /* CC_INTERP */
aoqi@0 3308 __ movptr(sp_temp, rsp); // pass to next frame
aoqi@0 3309 __ addptr(rsi, wordSize); // Bump array pointer (sizes)
aoqi@0 3310 __ addptr(rcx, wordSize); // Bump array pointer (pcs)
aoqi@0 3311 __ decrementl(counter); // decrement counter
aoqi@0 3312 __ jcc(Assembler::notZero, loop);
aoqi@0 3313 __ pushptr(Address(rcx, 0)); // save final return address
aoqi@0 3314
aoqi@0 3315 // Re-push self-frame
aoqi@0 3316 __ enter(); // save old & set new rbp,
aoqi@0 3317 __ subptr(rsp, (framesize-2) * wordSize); // Prolog!
aoqi@0 3318
aoqi@0 3319
aoqi@0 3320 // set last_Java_sp, last_Java_fp
aoqi@0 3321 __ get_thread(rdi);
aoqi@0 3322 __ set_last_Java_frame(rdi, noreg, rbp, NULL);
aoqi@0 3323
aoqi@0 3324 // Call C code. Need thread but NOT official VM entry
aoqi@0 3325 // crud. We cannot block on this call, no GC can happen. Call should
aoqi@0 3326 // restore return values to their stack-slots with the new SP.
aoqi@0 3327 __ movptr(Address(rsp,arg0_off*wordSize),rdi);
aoqi@0 3328 __ movl(Address(rsp,arg1_off*wordSize), Deoptimization::Unpack_uncommon_trap);
aoqi@0 3329 __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, Deoptimization::unpack_frames)));
aoqi@0 3330 // Set an oopmap for the call site
aoqi@0 3331 oop_maps->add_gc_map( __ pc()-start, new OopMap( framesize, 0 ) );
aoqi@0 3332
aoqi@0 3333 __ get_thread(rdi);
aoqi@0 3334 __ reset_last_Java_frame(rdi, true, false);
aoqi@0 3335
aoqi@0 3336 // Pop self-frame.
aoqi@0 3337 __ leave(); // Epilog!
aoqi@0 3338
aoqi@0 3339 // Jump to interpreter
aoqi@0 3340 __ ret(0);
aoqi@0 3341
aoqi@0 3342 // -------------
aoqi@0 3343 // make sure all code is generated
aoqi@0 3344 masm->flush();
aoqi@0 3345
aoqi@0 3346 _uncommon_trap_blob = UncommonTrapBlob::create(&buffer, oop_maps, framesize);
aoqi@0 3347 }
aoqi@0 3348 #endif // COMPILER2
aoqi@0 3349
aoqi@0 3350 //------------------------------generate_handler_blob------
aoqi@0 3351 //
aoqi@0 3352 // Generate a special Compile2Runtime blob that saves all registers,
aoqi@0 3353 // setup oopmap, and calls safepoint code to stop the compiled code for
aoqi@0 3354 // a safepoint.
aoqi@0 3355 //
aoqi@0 3356 SafepointBlob* SharedRuntime::generate_handler_blob(address call_ptr, int poll_type) {
aoqi@0 3357
aoqi@0 3358 // Account for thread arg in our frame
aoqi@0 3359 const int additional_words = 1;
aoqi@0 3360 int frame_size_in_words;
aoqi@0 3361
aoqi@0 3362 assert (StubRoutines::forward_exception_entry() != NULL, "must be generated before");
aoqi@0 3363
aoqi@0 3364 ResourceMark rm;
aoqi@0 3365 OopMapSet *oop_maps = new OopMapSet();
aoqi@0 3366 OopMap* map;
aoqi@0 3367
aoqi@0 3368 // allocate space for the code
aoqi@0 3369 // setup code generation tools
aoqi@0 3370 CodeBuffer buffer("handler_blob", 1024, 512);
aoqi@0 3371 MacroAssembler* masm = new MacroAssembler(&buffer);
aoqi@0 3372
aoqi@0 3373 const Register java_thread = rdi; // callee-saved for VC++
aoqi@0 3374 address start = __ pc();
aoqi@0 3375 address call_pc = NULL;
aoqi@0 3376 bool cause_return = (poll_type == POLL_AT_RETURN);
aoqi@0 3377 bool save_vectors = (poll_type == POLL_AT_VECTOR_LOOP);
aoqi@0 3378
aoqi@0 3379 if (UseRTMLocking) {
aoqi@0 3380 // Abort RTM transaction before calling runtime
aoqi@0 3381 // because critical section will be large and will be
aoqi@0 3382 // aborted anyway. Also nmethod could be deoptimized.
aoqi@0 3383 __ xabort(0);
aoqi@0 3384 }
aoqi@0 3385
aoqi@0 3386 // If cause_return is true we are at a poll_return and there is
aoqi@0 3387 // the return address on the stack to the caller on the nmethod
aoqi@0 3388 // that is safepoint. We can leave this return on the stack and
aoqi@0 3389 // effectively complete the return and safepoint in the caller.
aoqi@0 3390 // Otherwise we push space for a return address that the safepoint
aoqi@0 3391 // handler will install later to make the stack walking sensible.
aoqi@0 3392 if (!cause_return)
aoqi@0 3393 __ push(rbx); // Make room for return address (or push it again)
aoqi@0 3394
aoqi@0 3395 map = RegisterSaver::save_live_registers(masm, additional_words, &frame_size_in_words, false, save_vectors);
aoqi@0 3396
aoqi@0 3397 // The following is basically a call_VM. However, we need the precise
aoqi@0 3398 // address of the call in order to generate an oopmap. Hence, we do all the
aoqi@0 3399 // work ourselves.
aoqi@0 3400
aoqi@0 3401 // Push thread argument and setup last_Java_sp
aoqi@0 3402 __ get_thread(java_thread);
aoqi@0 3403 __ push(java_thread);
aoqi@0 3404 __ set_last_Java_frame(java_thread, noreg, noreg, NULL);
aoqi@0 3405
aoqi@0 3406 // if this was not a poll_return then we need to correct the return address now.
aoqi@0 3407 if (!cause_return) {
aoqi@0 3408 __ movptr(rax, Address(java_thread, JavaThread::saved_exception_pc_offset()));
aoqi@0 3409 __ movptr(Address(rbp, wordSize), rax);
aoqi@0 3410 }
aoqi@0 3411
aoqi@0 3412 // do the call
aoqi@0 3413 __ call(RuntimeAddress(call_ptr));
aoqi@0 3414
aoqi@0 3415 // Set an oopmap for the call site. This oopmap will map all
aoqi@0 3416 // oop-registers and debug-info registers as callee-saved. This
aoqi@0 3417 // will allow deoptimization at this safepoint to find all possible
aoqi@0 3418 // debug-info recordings, as well as let GC find all oops.
aoqi@0 3419
aoqi@0 3420 oop_maps->add_gc_map( __ pc() - start, map);
aoqi@0 3421
aoqi@0 3422 // Discard arg
aoqi@0 3423 __ pop(rcx);
aoqi@0 3424
aoqi@0 3425 Label noException;
aoqi@0 3426
aoqi@0 3427 // Clear last_Java_sp again
aoqi@0 3428 __ get_thread(java_thread);
aoqi@0 3429 __ reset_last_Java_frame(java_thread, false, false);
aoqi@0 3430
aoqi@0 3431 __ cmpptr(Address(java_thread, Thread::pending_exception_offset()), (int32_t)NULL_WORD);
aoqi@0 3432 __ jcc(Assembler::equal, noException);
aoqi@0 3433
aoqi@0 3434 // Exception pending
aoqi@0 3435 RegisterSaver::restore_live_registers(masm, save_vectors);
aoqi@0 3436
aoqi@0 3437 __ jump(RuntimeAddress(StubRoutines::forward_exception_entry()));
aoqi@0 3438
aoqi@0 3439 __ bind(noException);
aoqi@0 3440
aoqi@0 3441 // Normal exit, register restoring and exit
aoqi@0 3442 RegisterSaver::restore_live_registers(masm, save_vectors);
aoqi@0 3443
aoqi@0 3444 __ ret(0);
aoqi@0 3445
aoqi@0 3446 // make sure all code is generated
aoqi@0 3447 masm->flush();
aoqi@0 3448
aoqi@0 3449 // Fill-out other meta info
aoqi@0 3450 return SafepointBlob::create(&buffer, oop_maps, frame_size_in_words);
aoqi@0 3451 }
aoqi@0 3452
aoqi@0 3453 //
aoqi@0 3454 // generate_resolve_blob - call resolution (static/virtual/opt-virtual/ic-miss
aoqi@0 3455 //
aoqi@0 3456 // Generate a stub that calls into vm to find out the proper destination
aoqi@0 3457 // of a java call. All the argument registers are live at this point
aoqi@0 3458 // but since this is generic code we don't know what they are and the caller
aoqi@0 3459 // must do any gc of the args.
aoqi@0 3460 //
aoqi@0 3461 RuntimeStub* SharedRuntime::generate_resolve_blob(address destination, const char* name) {
aoqi@0 3462 assert (StubRoutines::forward_exception_entry() != NULL, "must be generated before");
aoqi@0 3463
aoqi@0 3464 // allocate space for the code
aoqi@0 3465 ResourceMark rm;
aoqi@0 3466
aoqi@0 3467 CodeBuffer buffer(name, 1000, 512);
aoqi@0 3468 MacroAssembler* masm = new MacroAssembler(&buffer);
aoqi@0 3469
aoqi@0 3470 int frame_size_words;
aoqi@0 3471 enum frame_layout {
aoqi@0 3472 thread_off,
aoqi@0 3473 extra_words };
aoqi@0 3474
aoqi@0 3475 OopMapSet *oop_maps = new OopMapSet();
aoqi@0 3476 OopMap* map = NULL;
aoqi@0 3477
aoqi@0 3478 int start = __ offset();
aoqi@0 3479
aoqi@0 3480 map = RegisterSaver::save_live_registers(masm, extra_words, &frame_size_words);
aoqi@0 3481
aoqi@0 3482 int frame_complete = __ offset();
aoqi@0 3483
aoqi@0 3484 const Register thread = rdi;
aoqi@0 3485 __ get_thread(rdi);
aoqi@0 3486
aoqi@0 3487 __ push(thread);
aoqi@0 3488 __ set_last_Java_frame(thread, noreg, rbp, NULL);
aoqi@0 3489
aoqi@0 3490 __ call(RuntimeAddress(destination));
aoqi@0 3491
aoqi@0 3492
aoqi@0 3493 // Set an oopmap for the call site.
aoqi@0 3494 // We need this not only for callee-saved registers, but also for volatile
aoqi@0 3495 // registers that the compiler might be keeping live across a safepoint.
aoqi@0 3496
aoqi@0 3497 oop_maps->add_gc_map( __ offset() - start, map);
aoqi@0 3498
aoqi@0 3499 // rax, contains the address we are going to jump to assuming no exception got installed
aoqi@0 3500
aoqi@0 3501 __ addptr(rsp, wordSize);
aoqi@0 3502
aoqi@0 3503 // clear last_Java_sp
aoqi@0 3504 __ reset_last_Java_frame(thread, true, false);
aoqi@0 3505 // check for pending exceptions
aoqi@0 3506 Label pending;
aoqi@0 3507 __ cmpptr(Address(thread, Thread::pending_exception_offset()), (int32_t)NULL_WORD);
aoqi@0 3508 __ jcc(Assembler::notEqual, pending);
aoqi@0 3509
aoqi@0 3510 // get the returned Method*
aoqi@0 3511 __ get_vm_result_2(rbx, thread);
aoqi@0 3512 __ movptr(Address(rsp, RegisterSaver::rbx_offset() * wordSize), rbx);
aoqi@0 3513
aoqi@0 3514 __ movptr(Address(rsp, RegisterSaver::rax_offset() * wordSize), rax);
aoqi@0 3515
aoqi@0 3516 RegisterSaver::restore_live_registers(masm);
aoqi@0 3517
aoqi@0 3518 // We are back the the original state on entry and ready to go.
aoqi@0 3519
aoqi@0 3520 __ jmp(rax);
aoqi@0 3521
aoqi@0 3522 // Pending exception after the safepoint
aoqi@0 3523
aoqi@0 3524 __ bind(pending);
aoqi@0 3525
aoqi@0 3526 RegisterSaver::restore_live_registers(masm);
aoqi@0 3527
aoqi@0 3528 // exception pending => remove activation and forward to exception handler
aoqi@0 3529
aoqi@0 3530 __ get_thread(thread);
aoqi@0 3531 __ movptr(Address(thread, JavaThread::vm_result_offset()), NULL_WORD);
aoqi@0 3532 __ movptr(rax, Address(thread, Thread::pending_exception_offset()));
aoqi@0 3533 __ jump(RuntimeAddress(StubRoutines::forward_exception_entry()));
aoqi@0 3534
aoqi@0 3535 // -------------
aoqi@0 3536 // make sure all code is generated
aoqi@0 3537 masm->flush();
aoqi@0 3538
aoqi@0 3539 // return the blob
aoqi@0 3540 // frame_size_words or bytes??
aoqi@0 3541 return RuntimeStub::new_runtime_stub(name, &buffer, frame_complete, frame_size_words, oop_maps, true);
aoqi@0 3542 }

mercurial