src/cpu/x86/vm/sharedRuntime_x86_64.cpp

Sun, 13 Apr 2008 17:43:42 -0400

author
coleenp
date
Sun, 13 Apr 2008 17:43:42 -0400
changeset 548
ba764ed4b6f2
parent 435
a61af66fc99e
child 551
018d5b58dd4f
permissions
-rw-r--r--

6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
Summary: Compressed oops in instances, arrays, and headers. Code contributors are coleenp, phh, never, swamyv
Reviewed-by: jmasa, kamg, acorn, tbell, kvn, rasbold

duke@435 1 /*
duke@435 2 * Copyright 2003-2007 Sun Microsystems, Inc. All Rights Reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
duke@435 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@435 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@435 21 * have any questions.
duke@435 22 *
duke@435 23 */
duke@435 24
duke@435 25 #include "incls/_precompiled.incl"
duke@435 26 #include "incls/_sharedRuntime_x86_64.cpp.incl"
duke@435 27
duke@435 28 DeoptimizationBlob *SharedRuntime::_deopt_blob;
duke@435 29 #ifdef COMPILER2
duke@435 30 UncommonTrapBlob *SharedRuntime::_uncommon_trap_blob;
duke@435 31 ExceptionBlob *OptoRuntime::_exception_blob;
duke@435 32 #endif // COMPILER2
duke@435 33
duke@435 34 SafepointBlob *SharedRuntime::_polling_page_safepoint_handler_blob;
duke@435 35 SafepointBlob *SharedRuntime::_polling_page_return_handler_blob;
duke@435 36 RuntimeStub* SharedRuntime::_wrong_method_blob;
duke@435 37 RuntimeStub* SharedRuntime::_ic_miss_blob;
duke@435 38 RuntimeStub* SharedRuntime::_resolve_opt_virtual_call_blob;
duke@435 39 RuntimeStub* SharedRuntime::_resolve_virtual_call_blob;
duke@435 40 RuntimeStub* SharedRuntime::_resolve_static_call_blob;
duke@435 41
duke@435 42 #define __ masm->
duke@435 43
duke@435 44 class SimpleRuntimeFrame {
duke@435 45
duke@435 46 public:
duke@435 47
duke@435 48 // Most of the runtime stubs have this simple frame layout.
duke@435 49 // This class exists to make the layout shared in one place.
duke@435 50 // Offsets are for compiler stack slots, which are jints.
duke@435 51 enum layout {
duke@435 52 // The frame sender code expects that rbp will be in the "natural" place and
duke@435 53 // will override any oopMap setting for it. We must therefore force the layout
duke@435 54 // so that it agrees with the frame sender code.
duke@435 55 rbp_off = frame::arg_reg_save_area_bytes/BytesPerInt,
duke@435 56 rbp_off2,
duke@435 57 return_off, return_off2,
duke@435 58 framesize
duke@435 59 };
duke@435 60 };
duke@435 61
duke@435 62 class RegisterSaver {
duke@435 63 // Capture info about frame layout. Layout offsets are in jint
duke@435 64 // units because compiler frame slots are jints.
duke@435 65 #define DEF_XMM_OFFS(regnum) xmm ## regnum ## _off = xmm_off + (regnum)*16/BytesPerInt, xmm ## regnum ## H_off
duke@435 66 enum layout {
duke@435 67 fpu_state_off = frame::arg_reg_save_area_bytes/BytesPerInt, // fxsave save area
duke@435 68 xmm_off = fpu_state_off + 160/BytesPerInt, // offset in fxsave save area
duke@435 69 DEF_XMM_OFFS(0),
duke@435 70 DEF_XMM_OFFS(1),
duke@435 71 DEF_XMM_OFFS(2),
duke@435 72 DEF_XMM_OFFS(3),
duke@435 73 DEF_XMM_OFFS(4),
duke@435 74 DEF_XMM_OFFS(5),
duke@435 75 DEF_XMM_OFFS(6),
duke@435 76 DEF_XMM_OFFS(7),
duke@435 77 DEF_XMM_OFFS(8),
duke@435 78 DEF_XMM_OFFS(9),
duke@435 79 DEF_XMM_OFFS(10),
duke@435 80 DEF_XMM_OFFS(11),
duke@435 81 DEF_XMM_OFFS(12),
duke@435 82 DEF_XMM_OFFS(13),
duke@435 83 DEF_XMM_OFFS(14),
duke@435 84 DEF_XMM_OFFS(15),
duke@435 85 fpu_state_end = fpu_state_off + ((FPUStateSizeInWords-1)*wordSize / BytesPerInt),
duke@435 86 fpu_stateH_end,
duke@435 87 r15_off, r15H_off,
duke@435 88 r14_off, r14H_off,
duke@435 89 r13_off, r13H_off,
duke@435 90 r12_off, r12H_off,
duke@435 91 r11_off, r11H_off,
duke@435 92 r10_off, r10H_off,
duke@435 93 r9_off, r9H_off,
duke@435 94 r8_off, r8H_off,
duke@435 95 rdi_off, rdiH_off,
duke@435 96 rsi_off, rsiH_off,
duke@435 97 ignore_off, ignoreH_off, // extra copy of rbp
duke@435 98 rsp_off, rspH_off,
duke@435 99 rbx_off, rbxH_off,
duke@435 100 rdx_off, rdxH_off,
duke@435 101 rcx_off, rcxH_off,
duke@435 102 rax_off, raxH_off,
duke@435 103 // 16-byte stack alignment fill word: see MacroAssembler::push/pop_IU_state
duke@435 104 align_off, alignH_off,
duke@435 105 flags_off, flagsH_off,
duke@435 106 // The frame sender code expects that rbp will be in the "natural" place and
duke@435 107 // will override any oopMap setting for it. We must therefore force the layout
duke@435 108 // so that it agrees with the frame sender code.
duke@435 109 rbp_off, rbpH_off, // copy of rbp we will restore
duke@435 110 return_off, returnH_off, // slot for return address
duke@435 111 reg_save_size // size in compiler stack slots
duke@435 112 };
duke@435 113
duke@435 114 public:
duke@435 115 static OopMap* save_live_registers(MacroAssembler* masm, int additional_frame_words, int* total_frame_words);
duke@435 116 static void restore_live_registers(MacroAssembler* masm);
duke@435 117
duke@435 118 // Offsets into the register save area
duke@435 119 // Used by deoptimization when it is managing result register
duke@435 120 // values on its own
duke@435 121
duke@435 122 static int rax_offset_in_bytes(void) { return BytesPerInt * rax_off; }
duke@435 123 static int rbx_offset_in_bytes(void) { return BytesPerInt * rbx_off; }
duke@435 124 static int xmm0_offset_in_bytes(void) { return BytesPerInt * xmm0_off; }
duke@435 125 static int return_offset_in_bytes(void) { return BytesPerInt * return_off; }
duke@435 126
duke@435 127 // During deoptimization only the result registers need to be restored,
duke@435 128 // all the other values have already been extracted.
duke@435 129 static void restore_result_registers(MacroAssembler* masm);
duke@435 130 };
duke@435 131
duke@435 132 OopMap* RegisterSaver::save_live_registers(MacroAssembler* masm, int additional_frame_words, int* total_frame_words) {
duke@435 133
duke@435 134 // Always make the frame size 16-byte aligned
duke@435 135 int frame_size_in_bytes = round_to(additional_frame_words*wordSize +
duke@435 136 reg_save_size*BytesPerInt, 16);
duke@435 137 // OopMap frame size is in compiler stack slots (jint's) not bytes or words
duke@435 138 int frame_size_in_slots = frame_size_in_bytes / BytesPerInt;
duke@435 139 // The caller will allocate additional_frame_words
duke@435 140 int additional_frame_slots = additional_frame_words*wordSize / BytesPerInt;
duke@435 141 // CodeBlob frame size is in words.
duke@435 142 int frame_size_in_words = frame_size_in_bytes / wordSize;
duke@435 143 *total_frame_words = frame_size_in_words;
duke@435 144
duke@435 145 // Save registers, fpu state, and flags.
duke@435 146 // We assume caller has already pushed the return address onto the
duke@435 147 // stack, so rsp is 8-byte aligned here.
duke@435 148 // We push rpb twice in this sequence because we want the real rbp
duke@435 149 // to be under the return like a normal enter.
duke@435 150
duke@435 151 __ enter(); // rsp becomes 16-byte aligned here
duke@435 152 __ push_CPU_state(); // Push a multiple of 16 bytes
duke@435 153 if (frame::arg_reg_save_area_bytes != 0) {
duke@435 154 // Allocate argument register save area
duke@435 155 __ subq(rsp, frame::arg_reg_save_area_bytes);
duke@435 156 }
duke@435 157
duke@435 158 // Set an oopmap for the call site. This oopmap will map all
duke@435 159 // oop-registers and debug-info registers as callee-saved. This
duke@435 160 // will allow deoptimization at this safepoint to find all possible
duke@435 161 // debug-info recordings, as well as let GC find all oops.
duke@435 162
duke@435 163 OopMapSet *oop_maps = new OopMapSet();
duke@435 164 OopMap* map = new OopMap(frame_size_in_slots, 0);
duke@435 165 map->set_callee_saved(VMRegImpl::stack2reg( rax_off + additional_frame_slots), rax->as_VMReg());
duke@435 166 map->set_callee_saved(VMRegImpl::stack2reg( rcx_off + additional_frame_slots), rcx->as_VMReg());
duke@435 167 map->set_callee_saved(VMRegImpl::stack2reg( rdx_off + additional_frame_slots), rdx->as_VMReg());
duke@435 168 map->set_callee_saved(VMRegImpl::stack2reg( rbx_off + additional_frame_slots), rbx->as_VMReg());
duke@435 169 // rbp location is known implicitly by the frame sender code, needs no oopmap
duke@435 170 // and the location where rbp was saved by is ignored
duke@435 171 map->set_callee_saved(VMRegImpl::stack2reg( rsi_off + additional_frame_slots), rsi->as_VMReg());
duke@435 172 map->set_callee_saved(VMRegImpl::stack2reg( rdi_off + additional_frame_slots), rdi->as_VMReg());
duke@435 173 map->set_callee_saved(VMRegImpl::stack2reg( r8_off + additional_frame_slots), r8->as_VMReg());
duke@435 174 map->set_callee_saved(VMRegImpl::stack2reg( r9_off + additional_frame_slots), r9->as_VMReg());
duke@435 175 map->set_callee_saved(VMRegImpl::stack2reg( r10_off + additional_frame_slots), r10->as_VMReg());
duke@435 176 map->set_callee_saved(VMRegImpl::stack2reg( r11_off + additional_frame_slots), r11->as_VMReg());
duke@435 177 map->set_callee_saved(VMRegImpl::stack2reg( r12_off + additional_frame_slots), r12->as_VMReg());
duke@435 178 map->set_callee_saved(VMRegImpl::stack2reg( r13_off + additional_frame_slots), r13->as_VMReg());
duke@435 179 map->set_callee_saved(VMRegImpl::stack2reg( r14_off + additional_frame_slots), r14->as_VMReg());
duke@435 180 map->set_callee_saved(VMRegImpl::stack2reg( r15_off + additional_frame_slots), r15->as_VMReg());
duke@435 181 map->set_callee_saved(VMRegImpl::stack2reg(xmm0_off + additional_frame_slots), xmm0->as_VMReg());
duke@435 182 map->set_callee_saved(VMRegImpl::stack2reg(xmm1_off + additional_frame_slots), xmm1->as_VMReg());
duke@435 183 map->set_callee_saved(VMRegImpl::stack2reg(xmm2_off + additional_frame_slots), xmm2->as_VMReg());
duke@435 184 map->set_callee_saved(VMRegImpl::stack2reg(xmm3_off + additional_frame_slots), xmm3->as_VMReg());
duke@435 185 map->set_callee_saved(VMRegImpl::stack2reg(xmm4_off + additional_frame_slots), xmm4->as_VMReg());
duke@435 186 map->set_callee_saved(VMRegImpl::stack2reg(xmm5_off + additional_frame_slots), xmm5->as_VMReg());
duke@435 187 map->set_callee_saved(VMRegImpl::stack2reg(xmm6_off + additional_frame_slots), xmm6->as_VMReg());
duke@435 188 map->set_callee_saved(VMRegImpl::stack2reg(xmm7_off + additional_frame_slots), xmm7->as_VMReg());
duke@435 189 map->set_callee_saved(VMRegImpl::stack2reg(xmm8_off + additional_frame_slots), xmm8->as_VMReg());
duke@435 190 map->set_callee_saved(VMRegImpl::stack2reg(xmm9_off + additional_frame_slots), xmm9->as_VMReg());
duke@435 191 map->set_callee_saved(VMRegImpl::stack2reg(xmm10_off + additional_frame_slots), xmm10->as_VMReg());
duke@435 192 map->set_callee_saved(VMRegImpl::stack2reg(xmm11_off + additional_frame_slots), xmm11->as_VMReg());
duke@435 193 map->set_callee_saved(VMRegImpl::stack2reg(xmm12_off + additional_frame_slots), xmm12->as_VMReg());
duke@435 194 map->set_callee_saved(VMRegImpl::stack2reg(xmm13_off + additional_frame_slots), xmm13->as_VMReg());
duke@435 195 map->set_callee_saved(VMRegImpl::stack2reg(xmm14_off + additional_frame_slots), xmm14->as_VMReg());
duke@435 196 map->set_callee_saved(VMRegImpl::stack2reg(xmm15_off + additional_frame_slots), xmm15->as_VMReg());
duke@435 197
duke@435 198 // %%% These should all be a waste but we'll keep things as they were for now
duke@435 199 if (true) {
duke@435 200 map->set_callee_saved(VMRegImpl::stack2reg( raxH_off + additional_frame_slots),
duke@435 201 rax->as_VMReg()->next());
duke@435 202 map->set_callee_saved(VMRegImpl::stack2reg( rcxH_off + additional_frame_slots),
duke@435 203 rcx->as_VMReg()->next());
duke@435 204 map->set_callee_saved(VMRegImpl::stack2reg( rdxH_off + additional_frame_slots),
duke@435 205 rdx->as_VMReg()->next());
duke@435 206 map->set_callee_saved(VMRegImpl::stack2reg( rbxH_off + additional_frame_slots),
duke@435 207 rbx->as_VMReg()->next());
duke@435 208 // rbp location is known implicitly by the frame sender code, needs no oopmap
duke@435 209 map->set_callee_saved(VMRegImpl::stack2reg( rsiH_off + additional_frame_slots),
duke@435 210 rsi->as_VMReg()->next());
duke@435 211 map->set_callee_saved(VMRegImpl::stack2reg( rdiH_off + additional_frame_slots),
duke@435 212 rdi->as_VMReg()->next());
duke@435 213 map->set_callee_saved(VMRegImpl::stack2reg( r8H_off + additional_frame_slots),
duke@435 214 r8->as_VMReg()->next());
duke@435 215 map->set_callee_saved(VMRegImpl::stack2reg( r9H_off + additional_frame_slots),
duke@435 216 r9->as_VMReg()->next());
duke@435 217 map->set_callee_saved(VMRegImpl::stack2reg( r10H_off + additional_frame_slots),
duke@435 218 r10->as_VMReg()->next());
duke@435 219 map->set_callee_saved(VMRegImpl::stack2reg( r11H_off + additional_frame_slots),
duke@435 220 r11->as_VMReg()->next());
duke@435 221 map->set_callee_saved(VMRegImpl::stack2reg( r12H_off + additional_frame_slots),
duke@435 222 r12->as_VMReg()->next());
duke@435 223 map->set_callee_saved(VMRegImpl::stack2reg( r13H_off + additional_frame_slots),
duke@435 224 r13->as_VMReg()->next());
duke@435 225 map->set_callee_saved(VMRegImpl::stack2reg( r14H_off + additional_frame_slots),
duke@435 226 r14->as_VMReg()->next());
duke@435 227 map->set_callee_saved(VMRegImpl::stack2reg( r15H_off + additional_frame_slots),
duke@435 228 r15->as_VMReg()->next());
duke@435 229 map->set_callee_saved(VMRegImpl::stack2reg(xmm0H_off + additional_frame_slots),
duke@435 230 xmm0->as_VMReg()->next());
duke@435 231 map->set_callee_saved(VMRegImpl::stack2reg(xmm1H_off + additional_frame_slots),
duke@435 232 xmm1->as_VMReg()->next());
duke@435 233 map->set_callee_saved(VMRegImpl::stack2reg(xmm2H_off + additional_frame_slots),
duke@435 234 xmm2->as_VMReg()->next());
duke@435 235 map->set_callee_saved(VMRegImpl::stack2reg(xmm3H_off + additional_frame_slots),
duke@435 236 xmm3->as_VMReg()->next());
duke@435 237 map->set_callee_saved(VMRegImpl::stack2reg(xmm4H_off + additional_frame_slots),
duke@435 238 xmm4->as_VMReg()->next());
duke@435 239 map->set_callee_saved(VMRegImpl::stack2reg(xmm5H_off + additional_frame_slots),
duke@435 240 xmm5->as_VMReg()->next());
duke@435 241 map->set_callee_saved(VMRegImpl::stack2reg(xmm6H_off + additional_frame_slots),
duke@435 242 xmm6->as_VMReg()->next());
duke@435 243 map->set_callee_saved(VMRegImpl::stack2reg(xmm7H_off + additional_frame_slots),
duke@435 244 xmm7->as_VMReg()->next());
duke@435 245 map->set_callee_saved(VMRegImpl::stack2reg(xmm8H_off + additional_frame_slots),
duke@435 246 xmm8->as_VMReg()->next());
duke@435 247 map->set_callee_saved(VMRegImpl::stack2reg(xmm9H_off + additional_frame_slots),
duke@435 248 xmm9->as_VMReg()->next());
duke@435 249 map->set_callee_saved(VMRegImpl::stack2reg(xmm10H_off + additional_frame_slots),
duke@435 250 xmm10->as_VMReg()->next());
duke@435 251 map->set_callee_saved(VMRegImpl::stack2reg(xmm11H_off + additional_frame_slots),
duke@435 252 xmm11->as_VMReg()->next());
duke@435 253 map->set_callee_saved(VMRegImpl::stack2reg(xmm12H_off + additional_frame_slots),
duke@435 254 xmm12->as_VMReg()->next());
duke@435 255 map->set_callee_saved(VMRegImpl::stack2reg(xmm13H_off + additional_frame_slots),
duke@435 256 xmm13->as_VMReg()->next());
duke@435 257 map->set_callee_saved(VMRegImpl::stack2reg(xmm14H_off + additional_frame_slots),
duke@435 258 xmm14->as_VMReg()->next());
duke@435 259 map->set_callee_saved(VMRegImpl::stack2reg(xmm15H_off + additional_frame_slots),
duke@435 260 xmm15->as_VMReg()->next());
duke@435 261 }
duke@435 262
duke@435 263 return map;
duke@435 264 }
duke@435 265
duke@435 266 void RegisterSaver::restore_live_registers(MacroAssembler* masm) {
duke@435 267 if (frame::arg_reg_save_area_bytes != 0) {
duke@435 268 // Pop arg register save area
duke@435 269 __ addq(rsp, frame::arg_reg_save_area_bytes);
duke@435 270 }
duke@435 271 // Recover CPU state
duke@435 272 __ pop_CPU_state();
duke@435 273 // Get the rbp described implicitly by the calling convention (no oopMap)
duke@435 274 __ popq(rbp);
duke@435 275 }
duke@435 276
duke@435 277 void RegisterSaver::restore_result_registers(MacroAssembler* masm) {
duke@435 278
duke@435 279 // Just restore result register. Only used by deoptimization. By
duke@435 280 // now any callee save register that needs to be restored to a c2
duke@435 281 // caller of the deoptee has been extracted into the vframeArray
duke@435 282 // and will be stuffed into the c2i adapter we create for later
duke@435 283 // restoration so only result registers need to be restored here.
duke@435 284
duke@435 285 // Restore fp result register
duke@435 286 __ movdbl(xmm0, Address(rsp, xmm0_offset_in_bytes()));
duke@435 287 // Restore integer result register
duke@435 288 __ movq(rax, Address(rsp, rax_offset_in_bytes()));
duke@435 289 // Pop all of the register save are off the stack except the return address
duke@435 290 __ addq(rsp, return_offset_in_bytes());
duke@435 291 }
duke@435 292
duke@435 293 // The java_calling_convention describes stack locations as ideal slots on
duke@435 294 // a frame with no abi restrictions. Since we must observe abi restrictions
duke@435 295 // (like the placement of the register window) the slots must be biased by
duke@435 296 // the following value.
duke@435 297 static int reg2offset_in(VMReg r) {
duke@435 298 // Account for saved rbp and return address
duke@435 299 // This should really be in_preserve_stack_slots
duke@435 300 return (r->reg2stack() + 4) * VMRegImpl::stack_slot_size;
duke@435 301 }
duke@435 302
duke@435 303 static int reg2offset_out(VMReg r) {
duke@435 304 return (r->reg2stack() + SharedRuntime::out_preserve_stack_slots()) * VMRegImpl::stack_slot_size;
duke@435 305 }
duke@435 306
duke@435 307 // ---------------------------------------------------------------------------
duke@435 308 // Read the array of BasicTypes from a signature, and compute where the
duke@435 309 // arguments should go. Values in the VMRegPair regs array refer to 4-byte
duke@435 310 // quantities. Values less than VMRegImpl::stack0 are registers, those above
duke@435 311 // refer to 4-byte stack slots. All stack slots are based off of the stack pointer
duke@435 312 // as framesizes are fixed.
duke@435 313 // VMRegImpl::stack0 refers to the first slot 0(sp).
duke@435 314 // and VMRegImpl::stack0+1 refers to the memory word 4-byes higher. Register
duke@435 315 // up to RegisterImpl::number_of_registers) are the 64-bit
duke@435 316 // integer registers.
duke@435 317
duke@435 318 // Note: the INPUTS in sig_bt are in units of Java argument words, which are
duke@435 319 // either 32-bit or 64-bit depending on the build. The OUTPUTS are in 32-bit
duke@435 320 // units regardless of build. Of course for i486 there is no 64 bit build
duke@435 321
duke@435 322 // The Java calling convention is a "shifted" version of the C ABI.
duke@435 323 // By skipping the first C ABI register we can call non-static jni methods
duke@435 324 // with small numbers of arguments without having to shuffle the arguments
duke@435 325 // at all. Since we control the java ABI we ought to at least get some
duke@435 326 // advantage out of it.
duke@435 327
duke@435 328 int SharedRuntime::java_calling_convention(const BasicType *sig_bt,
duke@435 329 VMRegPair *regs,
duke@435 330 int total_args_passed,
duke@435 331 int is_outgoing) {
duke@435 332
duke@435 333 // Create the mapping between argument positions and
duke@435 334 // registers.
duke@435 335 static const Register INT_ArgReg[Argument::n_int_register_parameters_j] = {
duke@435 336 j_rarg0, j_rarg1, j_rarg2, j_rarg3, j_rarg4, j_rarg5
duke@435 337 };
duke@435 338 static const XMMRegister FP_ArgReg[Argument::n_float_register_parameters_j] = {
duke@435 339 j_farg0, j_farg1, j_farg2, j_farg3,
duke@435 340 j_farg4, j_farg5, j_farg6, j_farg7
duke@435 341 };
duke@435 342
duke@435 343
duke@435 344 uint int_args = 0;
duke@435 345 uint fp_args = 0;
duke@435 346 uint stk_args = 0; // inc by 2 each time
duke@435 347
duke@435 348 for (int i = 0; i < total_args_passed; i++) {
duke@435 349 switch (sig_bt[i]) {
duke@435 350 case T_BOOLEAN:
duke@435 351 case T_CHAR:
duke@435 352 case T_BYTE:
duke@435 353 case T_SHORT:
duke@435 354 case T_INT:
duke@435 355 if (int_args < Argument::n_int_register_parameters_j) {
duke@435 356 regs[i].set1(INT_ArgReg[int_args++]->as_VMReg());
duke@435 357 } else {
duke@435 358 regs[i].set1(VMRegImpl::stack2reg(stk_args));
duke@435 359 stk_args += 2;
duke@435 360 }
duke@435 361 break;
duke@435 362 case T_VOID:
duke@435 363 // halves of T_LONG or T_DOUBLE
duke@435 364 assert(i != 0 && (sig_bt[i - 1] == T_LONG || sig_bt[i - 1] == T_DOUBLE), "expecting half");
duke@435 365 regs[i].set_bad();
duke@435 366 break;
duke@435 367 case T_LONG:
duke@435 368 assert(sig_bt[i + 1] == T_VOID, "expecting half");
duke@435 369 // fall through
duke@435 370 case T_OBJECT:
duke@435 371 case T_ARRAY:
duke@435 372 case T_ADDRESS:
duke@435 373 if (int_args < Argument::n_int_register_parameters_j) {
duke@435 374 regs[i].set2(INT_ArgReg[int_args++]->as_VMReg());
duke@435 375 } else {
duke@435 376 regs[i].set2(VMRegImpl::stack2reg(stk_args));
duke@435 377 stk_args += 2;
duke@435 378 }
duke@435 379 break;
duke@435 380 case T_FLOAT:
duke@435 381 if (fp_args < Argument::n_float_register_parameters_j) {
duke@435 382 regs[i].set1(FP_ArgReg[fp_args++]->as_VMReg());
duke@435 383 } else {
duke@435 384 regs[i].set1(VMRegImpl::stack2reg(stk_args));
duke@435 385 stk_args += 2;
duke@435 386 }
duke@435 387 break;
duke@435 388 case T_DOUBLE:
duke@435 389 assert(sig_bt[i + 1] == T_VOID, "expecting half");
duke@435 390 if (fp_args < Argument::n_float_register_parameters_j) {
duke@435 391 regs[i].set2(FP_ArgReg[fp_args++]->as_VMReg());
duke@435 392 } else {
duke@435 393 regs[i].set2(VMRegImpl::stack2reg(stk_args));
duke@435 394 stk_args += 2;
duke@435 395 }
duke@435 396 break;
duke@435 397 default:
duke@435 398 ShouldNotReachHere();
duke@435 399 break;
duke@435 400 }
duke@435 401 }
duke@435 402
duke@435 403 return round_to(stk_args, 2);
duke@435 404 }
duke@435 405
duke@435 406 // Patch the callers callsite with entry to compiled code if it exists.
duke@435 407 static void patch_callers_callsite(MacroAssembler *masm) {
duke@435 408 Label L;
duke@435 409 __ verify_oop(rbx);
duke@435 410 __ cmpq(Address(rbx, in_bytes(methodOopDesc::code_offset())), (int)NULL_WORD);
duke@435 411 __ jcc(Assembler::equal, L);
duke@435 412
duke@435 413 // Save the current stack pointer
duke@435 414 __ movq(r13, rsp);
duke@435 415 // Schedule the branch target address early.
duke@435 416 // Call into the VM to patch the caller, then jump to compiled callee
duke@435 417 // rax isn't live so capture return address while we easily can
duke@435 418 __ movq(rax, Address(rsp, 0));
duke@435 419
duke@435 420 // align stack so push_CPU_state doesn't fault
duke@435 421 __ andq(rsp, -(StackAlignmentInBytes));
duke@435 422 __ push_CPU_state();
duke@435 423
duke@435 424
duke@435 425 __ verify_oop(rbx);
duke@435 426 // VM needs caller's callsite
duke@435 427 // VM needs target method
duke@435 428 // This needs to be a long call since we will relocate this adapter to
duke@435 429 // the codeBuffer and it may not reach
duke@435 430
duke@435 431 // Allocate argument register save area
duke@435 432 if (frame::arg_reg_save_area_bytes != 0) {
duke@435 433 __ subq(rsp, frame::arg_reg_save_area_bytes);
duke@435 434 }
duke@435 435 __ movq(c_rarg0, rbx);
duke@435 436 __ movq(c_rarg1, rax);
duke@435 437 __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, SharedRuntime::fixup_callers_callsite)));
duke@435 438
duke@435 439 // De-allocate argument register save area
duke@435 440 if (frame::arg_reg_save_area_bytes != 0) {
duke@435 441 __ addq(rsp, frame::arg_reg_save_area_bytes);
duke@435 442 }
duke@435 443
duke@435 444 __ pop_CPU_state();
duke@435 445 // restore sp
duke@435 446 __ movq(rsp, r13);
duke@435 447 __ bind(L);
duke@435 448 }
duke@435 449
duke@435 450 // Helper function to put tags in interpreter stack.
duke@435 451 static void tag_stack(MacroAssembler *masm, const BasicType sig, int st_off) {
duke@435 452 if (TaggedStackInterpreter) {
duke@435 453 int tag_offset = st_off + Interpreter::expr_tag_offset_in_bytes(0);
duke@435 454 if (sig == T_OBJECT || sig == T_ARRAY) {
duke@435 455 __ mov64(Address(rsp, tag_offset), frame::TagReference);
duke@435 456 } else if (sig == T_LONG || sig == T_DOUBLE) {
duke@435 457 int next_tag_offset = st_off + Interpreter::expr_tag_offset_in_bytes(1);
duke@435 458 __ mov64(Address(rsp, next_tag_offset), frame::TagValue);
duke@435 459 __ mov64(Address(rsp, tag_offset), frame::TagValue);
duke@435 460 } else {
duke@435 461 __ mov64(Address(rsp, tag_offset), frame::TagValue);
duke@435 462 }
duke@435 463 }
duke@435 464 }
duke@435 465
duke@435 466
duke@435 467 static void gen_c2i_adapter(MacroAssembler *masm,
duke@435 468 int total_args_passed,
duke@435 469 int comp_args_on_stack,
duke@435 470 const BasicType *sig_bt,
duke@435 471 const VMRegPair *regs,
duke@435 472 Label& skip_fixup) {
duke@435 473 // Before we get into the guts of the C2I adapter, see if we should be here
duke@435 474 // at all. We've come from compiled code and are attempting to jump to the
duke@435 475 // interpreter, which means the caller made a static call to get here
duke@435 476 // (vcalls always get a compiled target if there is one). Check for a
duke@435 477 // compiled target. If there is one, we need to patch the caller's call.
duke@435 478 patch_callers_callsite(masm);
duke@435 479
duke@435 480 __ bind(skip_fixup);
duke@435 481
duke@435 482 // Since all args are passed on the stack, total_args_passed *
duke@435 483 // Interpreter::stackElementSize is the space we need. Plus 1 because
duke@435 484 // we also account for the return address location since
duke@435 485 // we store it first rather than hold it in rax across all the shuffling
duke@435 486
duke@435 487 int extraspace = (total_args_passed * Interpreter::stackElementSize()) + wordSize;
duke@435 488
duke@435 489 // stack is aligned, keep it that way
duke@435 490 extraspace = round_to(extraspace, 2*wordSize);
duke@435 491
duke@435 492 // Get return address
duke@435 493 __ popq(rax);
duke@435 494
duke@435 495 // set senderSP value
duke@435 496 __ movq(r13, rsp);
duke@435 497
duke@435 498 __ subq(rsp, extraspace);
duke@435 499
duke@435 500 // Store the return address in the expected location
duke@435 501 __ movq(Address(rsp, 0), rax);
duke@435 502
duke@435 503 // Now write the args into the outgoing interpreter space
duke@435 504 for (int i = 0; i < total_args_passed; i++) {
duke@435 505 if (sig_bt[i] == T_VOID) {
duke@435 506 assert(i > 0 && (sig_bt[i-1] == T_LONG || sig_bt[i-1] == T_DOUBLE), "missing half");
duke@435 507 continue;
duke@435 508 }
duke@435 509
duke@435 510 // offset to start parameters
duke@435 511 int st_off = (total_args_passed - i) * Interpreter::stackElementSize() +
duke@435 512 Interpreter::value_offset_in_bytes();
duke@435 513 int next_off = st_off - Interpreter::stackElementSize();
duke@435 514
duke@435 515 // Say 4 args:
duke@435 516 // i st_off
duke@435 517 // 0 32 T_LONG
duke@435 518 // 1 24 T_VOID
duke@435 519 // 2 16 T_OBJECT
duke@435 520 // 3 8 T_BOOL
duke@435 521 // - 0 return address
duke@435 522 //
duke@435 523 // However to make thing extra confusing. Because we can fit a long/double in
duke@435 524 // a single slot on a 64 bt vm and it would be silly to break them up, the interpreter
duke@435 525 // leaves one slot empty and only stores to a single slot. In this case the
duke@435 526 // slot that is occupied is the T_VOID slot. See I said it was confusing.
duke@435 527
duke@435 528 VMReg r_1 = regs[i].first();
duke@435 529 VMReg r_2 = regs[i].second();
duke@435 530 if (!r_1->is_valid()) {
duke@435 531 assert(!r_2->is_valid(), "");
duke@435 532 continue;
duke@435 533 }
duke@435 534 if (r_1->is_stack()) {
duke@435 535 // memory to memory use rax
duke@435 536 int ld_off = r_1->reg2stack() * VMRegImpl::stack_slot_size + extraspace;
duke@435 537 if (!r_2->is_valid()) {
duke@435 538 // sign extend??
duke@435 539 __ movl(rax, Address(rsp, ld_off));
duke@435 540 __ movq(Address(rsp, st_off), rax);
duke@435 541 tag_stack(masm, sig_bt[i], st_off);
duke@435 542
duke@435 543 } else {
duke@435 544
duke@435 545 __ movq(rax, Address(rsp, ld_off));
duke@435 546
duke@435 547 // Two VMREgs|OptoRegs can be T_OBJECT, T_ADDRESS, T_DOUBLE, T_LONG
duke@435 548 // T_DOUBLE and T_LONG use two slots in the interpreter
duke@435 549 if ( sig_bt[i] == T_LONG || sig_bt[i] == T_DOUBLE) {
duke@435 550 // ld_off == LSW, ld_off+wordSize == MSW
duke@435 551 // st_off == MSW, next_off == LSW
duke@435 552 __ movq(Address(rsp, next_off), rax);
duke@435 553 #ifdef ASSERT
duke@435 554 // Overwrite the unused slot with known junk
duke@435 555 __ mov64(rax, CONST64(0xdeadffffdeadaaaa));
duke@435 556 __ movq(Address(rsp, st_off), rax);
duke@435 557 #endif /* ASSERT */
duke@435 558 tag_stack(masm, sig_bt[i], next_off);
duke@435 559 } else {
duke@435 560 __ movq(Address(rsp, st_off), rax);
duke@435 561 tag_stack(masm, sig_bt[i], st_off);
duke@435 562 }
duke@435 563 }
duke@435 564 } else if (r_1->is_Register()) {
duke@435 565 Register r = r_1->as_Register();
duke@435 566 if (!r_2->is_valid()) {
duke@435 567 // must be only an int (or less ) so move only 32bits to slot
duke@435 568 // why not sign extend??
duke@435 569 __ movl(Address(rsp, st_off), r);
duke@435 570 tag_stack(masm, sig_bt[i], st_off);
duke@435 571 } else {
duke@435 572 // Two VMREgs|OptoRegs can be T_OBJECT, T_ADDRESS, T_DOUBLE, T_LONG
duke@435 573 // T_DOUBLE and T_LONG use two slots in the interpreter
duke@435 574 if ( sig_bt[i] == T_LONG || sig_bt[i] == T_DOUBLE) {
duke@435 575 // long/double in gpr
duke@435 576 #ifdef ASSERT
duke@435 577 // Overwrite the unused slot with known junk
duke@435 578 __ mov64(rax, CONST64(0xdeadffffdeadaaab));
duke@435 579 __ movq(Address(rsp, st_off), rax);
duke@435 580 #endif /* ASSERT */
duke@435 581 __ movq(Address(rsp, next_off), r);
duke@435 582 tag_stack(masm, sig_bt[i], next_off);
duke@435 583 } else {
duke@435 584 __ movq(Address(rsp, st_off), r);
duke@435 585 tag_stack(masm, sig_bt[i], st_off);
duke@435 586 }
duke@435 587 }
duke@435 588 } else {
duke@435 589 assert(r_1->is_XMMRegister(), "");
duke@435 590 if (!r_2->is_valid()) {
duke@435 591 // only a float use just part of the slot
duke@435 592 __ movflt(Address(rsp, st_off), r_1->as_XMMRegister());
duke@435 593 tag_stack(masm, sig_bt[i], st_off);
duke@435 594 } else {
duke@435 595 #ifdef ASSERT
duke@435 596 // Overwrite the unused slot with known junk
duke@435 597 __ mov64(rax, CONST64(0xdeadffffdeadaaac));
duke@435 598 __ movq(Address(rsp, st_off), rax);
duke@435 599 #endif /* ASSERT */
duke@435 600 __ movdbl(Address(rsp, next_off), r_1->as_XMMRegister());
duke@435 601 tag_stack(masm, sig_bt[i], next_off);
duke@435 602 }
duke@435 603 }
duke@435 604 }
duke@435 605
duke@435 606 // Schedule the branch target address early.
duke@435 607 __ movq(rcx, Address(rbx, in_bytes(methodOopDesc::interpreter_entry_offset())));
duke@435 608 __ jmp(rcx);
duke@435 609 }
duke@435 610
duke@435 611 static void gen_i2c_adapter(MacroAssembler *masm,
duke@435 612 int total_args_passed,
duke@435 613 int comp_args_on_stack,
duke@435 614 const BasicType *sig_bt,
duke@435 615 const VMRegPair *regs) {
duke@435 616
duke@435 617 //
duke@435 618 // We will only enter here from an interpreted frame and never from after
duke@435 619 // passing thru a c2i. Azul allowed this but we do not. If we lose the
duke@435 620 // race and use a c2i we will remain interpreted for the race loser(s).
duke@435 621 // This removes all sorts of headaches on the x86 side and also eliminates
duke@435 622 // the possibility of having c2i -> i2c -> c2i -> ... endless transitions.
duke@435 623
duke@435 624
duke@435 625 // Note: r13 contains the senderSP on entry. We must preserve it since
duke@435 626 // we may do a i2c -> c2i transition if we lose a race where compiled
duke@435 627 // code goes non-entrant while we get args ready.
duke@435 628 // In addition we use r13 to locate all the interpreter args as
duke@435 629 // we must align the stack to 16 bytes on an i2c entry else we
duke@435 630 // lose alignment we expect in all compiled code and register
duke@435 631 // save code can segv when fxsave instructions find improperly
duke@435 632 // aligned stack pointer.
duke@435 633
duke@435 634 __ movq(rax, Address(rsp, 0));
duke@435 635
duke@435 636 // Cut-out for having no stack args. Since up to 2 int/oop args are passed
duke@435 637 // in registers, we will occasionally have no stack args.
duke@435 638 int comp_words_on_stack = 0;
duke@435 639 if (comp_args_on_stack) {
duke@435 640 // Sig words on the stack are greater-than VMRegImpl::stack0. Those in
duke@435 641 // registers are below. By subtracting stack0, we either get a negative
duke@435 642 // number (all values in registers) or the maximum stack slot accessed.
duke@435 643
duke@435 644 // Convert 4-byte c2 stack slots to words.
duke@435 645 comp_words_on_stack = round_to(comp_args_on_stack*VMRegImpl::stack_slot_size, wordSize)>>LogBytesPerWord;
duke@435 646 // Round up to miminum stack alignment, in wordSize
duke@435 647 comp_words_on_stack = round_to(comp_words_on_stack, 2);
duke@435 648 __ subq(rsp, comp_words_on_stack * wordSize);
duke@435 649 }
duke@435 650
duke@435 651
duke@435 652 // Ensure compiled code always sees stack at proper alignment
duke@435 653 __ andq(rsp, -16);
duke@435 654
duke@435 655 // push the return address and misalign the stack that youngest frame always sees
duke@435 656 // as far as the placement of the call instruction
duke@435 657 __ pushq(rax);
duke@435 658
duke@435 659 // Will jump to the compiled code just as if compiled code was doing it.
duke@435 660 // Pre-load the register-jump target early, to schedule it better.
duke@435 661 __ movq(r11, Address(rbx, in_bytes(methodOopDesc::from_compiled_offset())));
duke@435 662
duke@435 663 // Now generate the shuffle code. Pick up all register args and move the
duke@435 664 // rest through the floating point stack top.
duke@435 665 for (int i = 0; i < total_args_passed; i++) {
duke@435 666 if (sig_bt[i] == T_VOID) {
duke@435 667 // Longs and doubles are passed in native word order, but misaligned
duke@435 668 // in the 32-bit build.
duke@435 669 assert(i > 0 && (sig_bt[i-1] == T_LONG || sig_bt[i-1] == T_DOUBLE), "missing half");
duke@435 670 continue;
duke@435 671 }
duke@435 672
duke@435 673 // Pick up 0, 1 or 2 words from SP+offset.
duke@435 674
duke@435 675 assert(!regs[i].second()->is_valid() || regs[i].first()->next() == regs[i].second(),
duke@435 676 "scrambled load targets?");
duke@435 677 // Load in argument order going down.
duke@435 678 // int ld_off = (total_args_passed + comp_words_on_stack -i)*wordSize;
duke@435 679 // base ld_off on r13 (sender_sp) as the stack alignment makes offsets from rsp
duke@435 680 // unpredictable
duke@435 681 int ld_off = ((total_args_passed - 1) - i)*Interpreter::stackElementSize();
duke@435 682
duke@435 683 // Point to interpreter value (vs. tag)
duke@435 684 int next_off = ld_off - Interpreter::stackElementSize();
duke@435 685 //
duke@435 686 //
duke@435 687 //
duke@435 688 VMReg r_1 = regs[i].first();
duke@435 689 VMReg r_2 = regs[i].second();
duke@435 690 if (!r_1->is_valid()) {
duke@435 691 assert(!r_2->is_valid(), "");
duke@435 692 continue;
duke@435 693 }
duke@435 694 if (r_1->is_stack()) {
duke@435 695 // Convert stack slot to an SP offset (+ wordSize to account for return address )
duke@435 696 int st_off = regs[i].first()->reg2stack()*VMRegImpl::stack_slot_size + wordSize;
duke@435 697 if (!r_2->is_valid()) {
duke@435 698 // sign extend???
duke@435 699 __ movl(rax, Address(r13, ld_off));
duke@435 700 __ movq(Address(rsp, st_off), rax);
duke@435 701 } else {
duke@435 702 //
duke@435 703 // We are using two optoregs. This can be either T_OBJECT, T_ADDRESS, T_LONG, or T_DOUBLE
duke@435 704 // the interpreter allocates two slots but only uses one for thr T_LONG or T_DOUBLE case
duke@435 705 // So we must adjust where to pick up the data to match the interpreter.
duke@435 706 //
duke@435 707 // Interpreter local[n] == MSW, local[n+1] == LSW however locals
duke@435 708 // are accessed as negative so LSW is at LOW address
duke@435 709
duke@435 710 // ld_off is MSW so get LSW
duke@435 711 const int offset = (sig_bt[i]==T_LONG||sig_bt[i]==T_DOUBLE)?
duke@435 712 next_off : ld_off;
duke@435 713 __ movq(rax, Address(r13, offset));
duke@435 714 // st_off is LSW (i.e. reg.first())
duke@435 715 __ movq(Address(rsp, st_off), rax);
duke@435 716 }
duke@435 717 } else if (r_1->is_Register()) { // Register argument
duke@435 718 Register r = r_1->as_Register();
duke@435 719 assert(r != rax, "must be different");
duke@435 720 if (r_2->is_valid()) {
duke@435 721 //
duke@435 722 // We are using two VMRegs. This can be either T_OBJECT, T_ADDRESS, T_LONG, or T_DOUBLE
duke@435 723 // the interpreter allocates two slots but only uses one for thr T_LONG or T_DOUBLE case
duke@435 724 // So we must adjust where to pick up the data to match the interpreter.
duke@435 725
duke@435 726 const int offset = (sig_bt[i]==T_LONG||sig_bt[i]==T_DOUBLE)?
duke@435 727 next_off : ld_off;
duke@435 728
duke@435 729 // this can be a misaligned move
duke@435 730 __ movq(r, Address(r13, offset));
duke@435 731 } else {
duke@435 732 // sign extend and use a full word?
duke@435 733 __ movl(r, Address(r13, ld_off));
duke@435 734 }
duke@435 735 } else {
duke@435 736 if (!r_2->is_valid()) {
duke@435 737 __ movflt(r_1->as_XMMRegister(), Address(r13, ld_off));
duke@435 738 } else {
duke@435 739 __ movdbl(r_1->as_XMMRegister(), Address(r13, next_off));
duke@435 740 }
duke@435 741 }
duke@435 742 }
duke@435 743
duke@435 744 // 6243940 We might end up in handle_wrong_method if
duke@435 745 // the callee is deoptimized as we race thru here. If that
duke@435 746 // happens we don't want to take a safepoint because the
duke@435 747 // caller frame will look interpreted and arguments are now
duke@435 748 // "compiled" so it is much better to make this transition
duke@435 749 // invisible to the stack walking code. Unfortunately if
duke@435 750 // we try and find the callee by normal means a safepoint
duke@435 751 // is possible. So we stash the desired callee in the thread
duke@435 752 // and the vm will find there should this case occur.
duke@435 753
duke@435 754 __ movq(Address(r15_thread, JavaThread::callee_target_offset()), rbx);
duke@435 755
duke@435 756 // put methodOop where a c2i would expect should we end up there
duke@435 757 // only needed becaus eof c2 resolve stubs return methodOop as a result in
duke@435 758 // rax
duke@435 759 __ movq(rax, rbx);
duke@435 760 __ jmp(r11);
duke@435 761 }
duke@435 762
duke@435 763 // ---------------------------------------------------------------
duke@435 764 AdapterHandlerEntry* SharedRuntime::generate_i2c2i_adapters(MacroAssembler *masm,
duke@435 765 int total_args_passed,
duke@435 766 int comp_args_on_stack,
duke@435 767 const BasicType *sig_bt,
duke@435 768 const VMRegPair *regs) {
duke@435 769 address i2c_entry = __ pc();
duke@435 770
duke@435 771 gen_i2c_adapter(masm, total_args_passed, comp_args_on_stack, sig_bt, regs);
duke@435 772
duke@435 773 // -------------------------------------------------------------------------
duke@435 774 // Generate a C2I adapter. On entry we know rbx holds the methodOop during calls
duke@435 775 // to the interpreter. The args start out packed in the compiled layout. They
duke@435 776 // need to be unpacked into the interpreter layout. This will almost always
duke@435 777 // require some stack space. We grow the current (compiled) stack, then repack
duke@435 778 // the args. We finally end in a jump to the generic interpreter entry point.
duke@435 779 // On exit from the interpreter, the interpreter will restore our SP (lest the
duke@435 780 // compiled code, which relys solely on SP and not RBP, get sick).
duke@435 781
duke@435 782 address c2i_unverified_entry = __ pc();
duke@435 783 Label skip_fixup;
duke@435 784 Label ok;
duke@435 785
duke@435 786 Register holder = rax;
duke@435 787 Register receiver = j_rarg0;
duke@435 788 Register temp = rbx;
duke@435 789
duke@435 790 {
duke@435 791 __ verify_oop(holder);
coleenp@548 792 __ load_klass(temp, receiver);
duke@435 793 __ verify_oop(temp);
duke@435 794
duke@435 795 __ cmpq(temp, Address(holder, compiledICHolderOopDesc::holder_klass_offset()));
duke@435 796 __ movq(rbx, Address(holder, compiledICHolderOopDesc::holder_method_offset()));
duke@435 797 __ jcc(Assembler::equal, ok);
duke@435 798 __ jump(RuntimeAddress(SharedRuntime::get_ic_miss_stub()));
duke@435 799
duke@435 800 __ bind(ok);
duke@435 801 // Method might have been compiled since the call site was patched to
duke@435 802 // interpreted if that is the case treat it as a miss so we can get
duke@435 803 // the call site corrected.
duke@435 804 __ cmpq(Address(rbx, in_bytes(methodOopDesc::code_offset())), (int)NULL_WORD);
duke@435 805 __ jcc(Assembler::equal, skip_fixup);
duke@435 806 __ jump(RuntimeAddress(SharedRuntime::get_ic_miss_stub()));
duke@435 807 }
duke@435 808
duke@435 809 address c2i_entry = __ pc();
duke@435 810
duke@435 811 gen_c2i_adapter(masm, total_args_passed, comp_args_on_stack, sig_bt, regs, skip_fixup);
duke@435 812
duke@435 813 __ flush();
duke@435 814 return new AdapterHandlerEntry(i2c_entry, c2i_entry, c2i_unverified_entry);
duke@435 815 }
duke@435 816
duke@435 817 int SharedRuntime::c_calling_convention(const BasicType *sig_bt,
duke@435 818 VMRegPair *regs,
duke@435 819 int total_args_passed) {
duke@435 820 // We return the amount of VMRegImpl stack slots we need to reserve for all
duke@435 821 // the arguments NOT counting out_preserve_stack_slots.
duke@435 822
duke@435 823 // NOTE: These arrays will have to change when c1 is ported
duke@435 824 #ifdef _WIN64
duke@435 825 static const Register INT_ArgReg[Argument::n_int_register_parameters_c] = {
duke@435 826 c_rarg0, c_rarg1, c_rarg2, c_rarg3
duke@435 827 };
duke@435 828 static const XMMRegister FP_ArgReg[Argument::n_float_register_parameters_c] = {
duke@435 829 c_farg0, c_farg1, c_farg2, c_farg3
duke@435 830 };
duke@435 831 #else
duke@435 832 static const Register INT_ArgReg[Argument::n_int_register_parameters_c] = {
duke@435 833 c_rarg0, c_rarg1, c_rarg2, c_rarg3, c_rarg4, c_rarg5
duke@435 834 };
duke@435 835 static const XMMRegister FP_ArgReg[Argument::n_float_register_parameters_c] = {
duke@435 836 c_farg0, c_farg1, c_farg2, c_farg3,
duke@435 837 c_farg4, c_farg5, c_farg6, c_farg7
duke@435 838 };
duke@435 839 #endif // _WIN64
duke@435 840
duke@435 841
duke@435 842 uint int_args = 0;
duke@435 843 uint fp_args = 0;
duke@435 844 uint stk_args = 0; // inc by 2 each time
duke@435 845
duke@435 846 for (int i = 0; i < total_args_passed; i++) {
duke@435 847 switch (sig_bt[i]) {
duke@435 848 case T_BOOLEAN:
duke@435 849 case T_CHAR:
duke@435 850 case T_BYTE:
duke@435 851 case T_SHORT:
duke@435 852 case T_INT:
duke@435 853 if (int_args < Argument::n_int_register_parameters_c) {
duke@435 854 regs[i].set1(INT_ArgReg[int_args++]->as_VMReg());
duke@435 855 #ifdef _WIN64
duke@435 856 fp_args++;
duke@435 857 // Allocate slots for callee to stuff register args the stack.
duke@435 858 stk_args += 2;
duke@435 859 #endif
duke@435 860 } else {
duke@435 861 regs[i].set1(VMRegImpl::stack2reg(stk_args));
duke@435 862 stk_args += 2;
duke@435 863 }
duke@435 864 break;
duke@435 865 case T_LONG:
duke@435 866 assert(sig_bt[i + 1] == T_VOID, "expecting half");
duke@435 867 // fall through
duke@435 868 case T_OBJECT:
duke@435 869 case T_ARRAY:
duke@435 870 case T_ADDRESS:
duke@435 871 if (int_args < Argument::n_int_register_parameters_c) {
duke@435 872 regs[i].set2(INT_ArgReg[int_args++]->as_VMReg());
duke@435 873 #ifdef _WIN64
duke@435 874 fp_args++;
duke@435 875 stk_args += 2;
duke@435 876 #endif
duke@435 877 } else {
duke@435 878 regs[i].set2(VMRegImpl::stack2reg(stk_args));
duke@435 879 stk_args += 2;
duke@435 880 }
duke@435 881 break;
duke@435 882 case T_FLOAT:
duke@435 883 if (fp_args < Argument::n_float_register_parameters_c) {
duke@435 884 regs[i].set1(FP_ArgReg[fp_args++]->as_VMReg());
duke@435 885 #ifdef _WIN64
duke@435 886 int_args++;
duke@435 887 // Allocate slots for callee to stuff register args the stack.
duke@435 888 stk_args += 2;
duke@435 889 #endif
duke@435 890 } else {
duke@435 891 regs[i].set1(VMRegImpl::stack2reg(stk_args));
duke@435 892 stk_args += 2;
duke@435 893 }
duke@435 894 break;
duke@435 895 case T_DOUBLE:
duke@435 896 assert(sig_bt[i + 1] == T_VOID, "expecting half");
duke@435 897 if (fp_args < Argument::n_float_register_parameters_c) {
duke@435 898 regs[i].set2(FP_ArgReg[fp_args++]->as_VMReg());
duke@435 899 #ifdef _WIN64
duke@435 900 int_args++;
duke@435 901 // Allocate slots for callee to stuff register args the stack.
duke@435 902 stk_args += 2;
duke@435 903 #endif
duke@435 904 } else {
duke@435 905 regs[i].set2(VMRegImpl::stack2reg(stk_args));
duke@435 906 stk_args += 2;
duke@435 907 }
duke@435 908 break;
duke@435 909 case T_VOID: // Halves of longs and doubles
duke@435 910 assert(i != 0 && (sig_bt[i - 1] == T_LONG || sig_bt[i - 1] == T_DOUBLE), "expecting half");
duke@435 911 regs[i].set_bad();
duke@435 912 break;
duke@435 913 default:
duke@435 914 ShouldNotReachHere();
duke@435 915 break;
duke@435 916 }
duke@435 917 }
duke@435 918 #ifdef _WIN64
duke@435 919 // windows abi requires that we always allocate enough stack space
duke@435 920 // for 4 64bit registers to be stored down.
duke@435 921 if (stk_args < 8) {
duke@435 922 stk_args = 8;
duke@435 923 }
duke@435 924 #endif // _WIN64
duke@435 925
duke@435 926 return stk_args;
duke@435 927 }
duke@435 928
duke@435 929 // On 64 bit we will store integer like items to the stack as
duke@435 930 // 64 bits items (sparc abi) even though java would only store
duke@435 931 // 32bits for a parameter. On 32bit it will simply be 32 bits
duke@435 932 // So this routine will do 32->32 on 32bit and 32->64 on 64bit
duke@435 933 static void move32_64(MacroAssembler* masm, VMRegPair src, VMRegPair dst) {
duke@435 934 if (src.first()->is_stack()) {
duke@435 935 if (dst.first()->is_stack()) {
duke@435 936 // stack to stack
duke@435 937 __ movslq(rax, Address(rbp, reg2offset_in(src.first())));
duke@435 938 __ movq(Address(rsp, reg2offset_out(dst.first())), rax);
duke@435 939 } else {
duke@435 940 // stack to reg
duke@435 941 __ movslq(dst.first()->as_Register(), Address(rbp, reg2offset_in(src.first())));
duke@435 942 }
duke@435 943 } else if (dst.first()->is_stack()) {
duke@435 944 // reg to stack
duke@435 945 // Do we really have to sign extend???
duke@435 946 // __ movslq(src.first()->as_Register(), src.first()->as_Register());
duke@435 947 __ movq(Address(rsp, reg2offset_out(dst.first())), src.first()->as_Register());
duke@435 948 } else {
duke@435 949 // Do we really have to sign extend???
duke@435 950 // __ movslq(dst.first()->as_Register(), src.first()->as_Register());
duke@435 951 if (dst.first() != src.first()) {
duke@435 952 __ movq(dst.first()->as_Register(), src.first()->as_Register());
duke@435 953 }
duke@435 954 }
duke@435 955 }
duke@435 956
duke@435 957
duke@435 958 // An oop arg. Must pass a handle not the oop itself
duke@435 959 static void object_move(MacroAssembler* masm,
duke@435 960 OopMap* map,
duke@435 961 int oop_handle_offset,
duke@435 962 int framesize_in_slots,
duke@435 963 VMRegPair src,
duke@435 964 VMRegPair dst,
duke@435 965 bool is_receiver,
duke@435 966 int* receiver_offset) {
duke@435 967
duke@435 968 // must pass a handle. First figure out the location we use as a handle
duke@435 969
duke@435 970 Register rHandle = dst.first()->is_stack() ? rax : dst.first()->as_Register();
duke@435 971
duke@435 972 // See if oop is NULL if it is we need no handle
duke@435 973
duke@435 974 if (src.first()->is_stack()) {
duke@435 975
duke@435 976 // Oop is already on the stack as an argument
duke@435 977 int offset_in_older_frame = src.first()->reg2stack() + SharedRuntime::out_preserve_stack_slots();
duke@435 978 map->set_oop(VMRegImpl::stack2reg(offset_in_older_frame + framesize_in_slots));
duke@435 979 if (is_receiver) {
duke@435 980 *receiver_offset = (offset_in_older_frame + framesize_in_slots) * VMRegImpl::stack_slot_size;
duke@435 981 }
duke@435 982
duke@435 983 __ cmpq(Address(rbp, reg2offset_in(src.first())), (int)NULL_WORD);
duke@435 984 __ leaq(rHandle, Address(rbp, reg2offset_in(src.first())));
duke@435 985 // conditionally move a NULL
duke@435 986 __ cmovq(Assembler::equal, rHandle, Address(rbp, reg2offset_in(src.first())));
duke@435 987 } else {
duke@435 988
duke@435 989 // Oop is in an a register we must store it to the space we reserve
duke@435 990 // on the stack for oop_handles and pass a handle if oop is non-NULL
duke@435 991
duke@435 992 const Register rOop = src.first()->as_Register();
duke@435 993 int oop_slot;
duke@435 994 if (rOop == j_rarg0)
duke@435 995 oop_slot = 0;
duke@435 996 else if (rOop == j_rarg1)
duke@435 997 oop_slot = 1;
duke@435 998 else if (rOop == j_rarg2)
duke@435 999 oop_slot = 2;
duke@435 1000 else if (rOop == j_rarg3)
duke@435 1001 oop_slot = 3;
duke@435 1002 else if (rOop == j_rarg4)
duke@435 1003 oop_slot = 4;
duke@435 1004 else {
duke@435 1005 assert(rOop == j_rarg5, "wrong register");
duke@435 1006 oop_slot = 5;
duke@435 1007 }
duke@435 1008
duke@435 1009 oop_slot = oop_slot * VMRegImpl::slots_per_word + oop_handle_offset;
duke@435 1010 int offset = oop_slot*VMRegImpl::stack_slot_size;
duke@435 1011
duke@435 1012 map->set_oop(VMRegImpl::stack2reg(oop_slot));
duke@435 1013 // Store oop in handle area, may be NULL
duke@435 1014 __ movq(Address(rsp, offset), rOop);
duke@435 1015 if (is_receiver) {
duke@435 1016 *receiver_offset = offset;
duke@435 1017 }
duke@435 1018
duke@435 1019 __ cmpq(rOop, (int)NULL);
duke@435 1020 __ leaq(rHandle, Address(rsp, offset));
duke@435 1021 // conditionally move a NULL from the handle area where it was just stored
duke@435 1022 __ cmovq(Assembler::equal, rHandle, Address(rsp, offset));
duke@435 1023 }
duke@435 1024
duke@435 1025 // If arg is on the stack then place it otherwise it is already in correct reg.
duke@435 1026 if (dst.first()->is_stack()) {
duke@435 1027 __ movq(Address(rsp, reg2offset_out(dst.first())), rHandle);
duke@435 1028 }
duke@435 1029 }
duke@435 1030
duke@435 1031 // A float arg may have to do float reg int reg conversion
duke@435 1032 static void float_move(MacroAssembler* masm, VMRegPair src, VMRegPair dst) {
duke@435 1033 assert(!src.second()->is_valid() && !dst.second()->is_valid(), "bad float_move");
duke@435 1034
duke@435 1035 // The calling conventions assures us that each VMregpair is either
duke@435 1036 // all really one physical register or adjacent stack slots.
duke@435 1037 // This greatly simplifies the cases here compared to sparc.
duke@435 1038
duke@435 1039 if (src.first()->is_stack()) {
duke@435 1040 if (dst.first()->is_stack()) {
duke@435 1041 __ movl(rax, Address(rbp, reg2offset_in(src.first())));
duke@435 1042 __ movq(Address(rsp, reg2offset_out(dst.first())), rax);
duke@435 1043 } else {
duke@435 1044 // stack to reg
duke@435 1045 assert(dst.first()->is_XMMRegister(), "only expect xmm registers as parameters");
duke@435 1046 __ movflt(dst.first()->as_XMMRegister(), Address(rbp, reg2offset_in(src.first())));
duke@435 1047 }
duke@435 1048 } else if (dst.first()->is_stack()) {
duke@435 1049 // reg to stack
duke@435 1050 assert(src.first()->is_XMMRegister(), "only expect xmm registers as parameters");
duke@435 1051 __ movflt(Address(rsp, reg2offset_out(dst.first())), src.first()->as_XMMRegister());
duke@435 1052 } else {
duke@435 1053 // reg to reg
duke@435 1054 // In theory these overlap but the ordering is such that this is likely a nop
duke@435 1055 if ( src.first() != dst.first()) {
duke@435 1056 __ movdbl(dst.first()->as_XMMRegister(), src.first()->as_XMMRegister());
duke@435 1057 }
duke@435 1058 }
duke@435 1059 }
duke@435 1060
duke@435 1061 // A long move
duke@435 1062 static void long_move(MacroAssembler* masm, VMRegPair src, VMRegPair dst) {
duke@435 1063
duke@435 1064 // The calling conventions assures us that each VMregpair is either
duke@435 1065 // all really one physical register or adjacent stack slots.
duke@435 1066 // This greatly simplifies the cases here compared to sparc.
duke@435 1067
duke@435 1068 if (src.is_single_phys_reg() ) {
duke@435 1069 if (dst.is_single_phys_reg()) {
duke@435 1070 if (dst.first() != src.first()) {
duke@435 1071 __ movq(dst.first()->as_Register(), src.first()->as_Register());
duke@435 1072 }
duke@435 1073 } else {
duke@435 1074 assert(dst.is_single_reg(), "not a stack pair");
duke@435 1075 __ movq(Address(rsp, reg2offset_out(dst.first())), src.first()->as_Register());
duke@435 1076 }
duke@435 1077 } else if (dst.is_single_phys_reg()) {
duke@435 1078 assert(src.is_single_reg(), "not a stack pair");
duke@435 1079 __ movq(dst.first()->as_Register(), Address(rbp, reg2offset_out(src.first())));
duke@435 1080 } else {
duke@435 1081 assert(src.is_single_reg() && dst.is_single_reg(), "not stack pairs");
duke@435 1082 __ movq(rax, Address(rbp, reg2offset_in(src.first())));
duke@435 1083 __ movq(Address(rsp, reg2offset_out(dst.first())), rax);
duke@435 1084 }
duke@435 1085 }
duke@435 1086
duke@435 1087 // A double move
duke@435 1088 static void double_move(MacroAssembler* masm, VMRegPair src, VMRegPair dst) {
duke@435 1089
duke@435 1090 // The calling conventions assures us that each VMregpair is either
duke@435 1091 // all really one physical register or adjacent stack slots.
duke@435 1092 // This greatly simplifies the cases here compared to sparc.
duke@435 1093
duke@435 1094 if (src.is_single_phys_reg() ) {
duke@435 1095 if (dst.is_single_phys_reg()) {
duke@435 1096 // In theory these overlap but the ordering is such that this is likely a nop
duke@435 1097 if ( src.first() != dst.first()) {
duke@435 1098 __ movdbl(dst.first()->as_XMMRegister(), src.first()->as_XMMRegister());
duke@435 1099 }
duke@435 1100 } else {
duke@435 1101 assert(dst.is_single_reg(), "not a stack pair");
duke@435 1102 __ movdbl(Address(rsp, reg2offset_out(dst.first())), src.first()->as_XMMRegister());
duke@435 1103 }
duke@435 1104 } else if (dst.is_single_phys_reg()) {
duke@435 1105 assert(src.is_single_reg(), "not a stack pair");
duke@435 1106 __ movdbl(dst.first()->as_XMMRegister(), Address(rbp, reg2offset_out(src.first())));
duke@435 1107 } else {
duke@435 1108 assert(src.is_single_reg() && dst.is_single_reg(), "not stack pairs");
duke@435 1109 __ movq(rax, Address(rbp, reg2offset_in(src.first())));
duke@435 1110 __ movq(Address(rsp, reg2offset_out(dst.first())), rax);
duke@435 1111 }
duke@435 1112 }
duke@435 1113
duke@435 1114
duke@435 1115 void SharedRuntime::save_native_result(MacroAssembler *masm, BasicType ret_type, int frame_slots) {
duke@435 1116 // We always ignore the frame_slots arg and just use the space just below frame pointer
duke@435 1117 // which by this time is free to use
duke@435 1118 switch (ret_type) {
duke@435 1119 case T_FLOAT:
duke@435 1120 __ movflt(Address(rbp, -wordSize), xmm0);
duke@435 1121 break;
duke@435 1122 case T_DOUBLE:
duke@435 1123 __ movdbl(Address(rbp, -wordSize), xmm0);
duke@435 1124 break;
duke@435 1125 case T_VOID: break;
duke@435 1126 default: {
duke@435 1127 __ movq(Address(rbp, -wordSize), rax);
duke@435 1128 }
duke@435 1129 }
duke@435 1130 }
duke@435 1131
duke@435 1132 void SharedRuntime::restore_native_result(MacroAssembler *masm, BasicType ret_type, int frame_slots) {
duke@435 1133 // We always ignore the frame_slots arg and just use the space just below frame pointer
duke@435 1134 // which by this time is free to use
duke@435 1135 switch (ret_type) {
duke@435 1136 case T_FLOAT:
duke@435 1137 __ movflt(xmm0, Address(rbp, -wordSize));
duke@435 1138 break;
duke@435 1139 case T_DOUBLE:
duke@435 1140 __ movdbl(xmm0, Address(rbp, -wordSize));
duke@435 1141 break;
duke@435 1142 case T_VOID: break;
duke@435 1143 default: {
duke@435 1144 __ movq(rax, Address(rbp, -wordSize));
duke@435 1145 }
duke@435 1146 }
duke@435 1147 }
duke@435 1148
duke@435 1149 static void save_args(MacroAssembler *masm, int arg_count, int first_arg, VMRegPair *args) {
duke@435 1150 for ( int i = first_arg ; i < arg_count ; i++ ) {
duke@435 1151 if (args[i].first()->is_Register()) {
duke@435 1152 __ pushq(args[i].first()->as_Register());
duke@435 1153 } else if (args[i].first()->is_XMMRegister()) {
duke@435 1154 __ subq(rsp, 2*wordSize);
duke@435 1155 __ movdbl(Address(rsp, 0), args[i].first()->as_XMMRegister());
duke@435 1156 }
duke@435 1157 }
duke@435 1158 }
duke@435 1159
duke@435 1160 static void restore_args(MacroAssembler *masm, int arg_count, int first_arg, VMRegPair *args) {
duke@435 1161 for ( int i = arg_count - 1 ; i >= first_arg ; i-- ) {
duke@435 1162 if (args[i].first()->is_Register()) {
duke@435 1163 __ popq(args[i].first()->as_Register());
duke@435 1164 } else if (args[i].first()->is_XMMRegister()) {
duke@435 1165 __ movdbl(args[i].first()->as_XMMRegister(), Address(rsp, 0));
duke@435 1166 __ addq(rsp, 2*wordSize);
duke@435 1167 }
duke@435 1168 }
duke@435 1169 }
duke@435 1170
duke@435 1171 // ---------------------------------------------------------------------------
duke@435 1172 // Generate a native wrapper for a given method. The method takes arguments
duke@435 1173 // in the Java compiled code convention, marshals them to the native
duke@435 1174 // convention (handlizes oops, etc), transitions to native, makes the call,
duke@435 1175 // returns to java state (possibly blocking), unhandlizes any result and
duke@435 1176 // returns.
duke@435 1177 nmethod *SharedRuntime::generate_native_wrapper(MacroAssembler *masm,
duke@435 1178 methodHandle method,
duke@435 1179 int total_in_args,
duke@435 1180 int comp_args_on_stack,
duke@435 1181 BasicType *in_sig_bt,
duke@435 1182 VMRegPair *in_regs,
duke@435 1183 BasicType ret_type) {
duke@435 1184 // Native nmethod wrappers never take possesion of the oop arguments.
duke@435 1185 // So the caller will gc the arguments. The only thing we need an
duke@435 1186 // oopMap for is if the call is static
duke@435 1187 //
duke@435 1188 // An OopMap for lock (and class if static)
duke@435 1189 OopMapSet *oop_maps = new OopMapSet();
duke@435 1190 intptr_t start = (intptr_t)__ pc();
duke@435 1191
duke@435 1192 // We have received a description of where all the java arg are located
duke@435 1193 // on entry to the wrapper. We need to convert these args to where
duke@435 1194 // the jni function will expect them. To figure out where they go
duke@435 1195 // we convert the java signature to a C signature by inserting
duke@435 1196 // the hidden arguments as arg[0] and possibly arg[1] (static method)
duke@435 1197
duke@435 1198 int total_c_args = total_in_args + 1;
duke@435 1199 if (method->is_static()) {
duke@435 1200 total_c_args++;
duke@435 1201 }
duke@435 1202
duke@435 1203 BasicType* out_sig_bt = NEW_RESOURCE_ARRAY(BasicType, total_c_args);
duke@435 1204 VMRegPair* out_regs = NEW_RESOURCE_ARRAY(VMRegPair, total_c_args);
duke@435 1205
duke@435 1206 int argc = 0;
duke@435 1207 out_sig_bt[argc++] = T_ADDRESS;
duke@435 1208 if (method->is_static()) {
duke@435 1209 out_sig_bt[argc++] = T_OBJECT;
duke@435 1210 }
duke@435 1211
duke@435 1212 for (int i = 0; i < total_in_args ; i++ ) {
duke@435 1213 out_sig_bt[argc++] = in_sig_bt[i];
duke@435 1214 }
duke@435 1215
duke@435 1216 // Now figure out where the args must be stored and how much stack space
duke@435 1217 // they require.
duke@435 1218 //
duke@435 1219 int out_arg_slots;
duke@435 1220 out_arg_slots = c_calling_convention(out_sig_bt, out_regs, total_c_args);
duke@435 1221
duke@435 1222 // Compute framesize for the wrapper. We need to handlize all oops in
duke@435 1223 // incoming registers
duke@435 1224
duke@435 1225 // Calculate the total number of stack slots we will need.
duke@435 1226
duke@435 1227 // First count the abi requirement plus all of the outgoing args
duke@435 1228 int stack_slots = SharedRuntime::out_preserve_stack_slots() + out_arg_slots;
duke@435 1229
duke@435 1230 // Now the space for the inbound oop handle area
duke@435 1231
duke@435 1232 int oop_handle_offset = stack_slots;
duke@435 1233 stack_slots += 6*VMRegImpl::slots_per_word;
duke@435 1234
duke@435 1235 // Now any space we need for handlizing a klass if static method
duke@435 1236
duke@435 1237 int oop_temp_slot_offset = 0;
duke@435 1238 int klass_slot_offset = 0;
duke@435 1239 int klass_offset = -1;
duke@435 1240 int lock_slot_offset = 0;
duke@435 1241 bool is_static = false;
duke@435 1242
duke@435 1243 if (method->is_static()) {
duke@435 1244 klass_slot_offset = stack_slots;
duke@435 1245 stack_slots += VMRegImpl::slots_per_word;
duke@435 1246 klass_offset = klass_slot_offset * VMRegImpl::stack_slot_size;
duke@435 1247 is_static = true;
duke@435 1248 }
duke@435 1249
duke@435 1250 // Plus a lock if needed
duke@435 1251
duke@435 1252 if (method->is_synchronized()) {
duke@435 1253 lock_slot_offset = stack_slots;
duke@435 1254 stack_slots += VMRegImpl::slots_per_word;
duke@435 1255 }
duke@435 1256
duke@435 1257 // Now a place (+2) to save return values or temp during shuffling
duke@435 1258 // + 4 for return address (which we own) and saved rbp
duke@435 1259 stack_slots += 6;
duke@435 1260
duke@435 1261 // Ok The space we have allocated will look like:
duke@435 1262 //
duke@435 1263 //
duke@435 1264 // FP-> | |
duke@435 1265 // |---------------------|
duke@435 1266 // | 2 slots for moves |
duke@435 1267 // |---------------------|
duke@435 1268 // | lock box (if sync) |
duke@435 1269 // |---------------------| <- lock_slot_offset
duke@435 1270 // | klass (if static) |
duke@435 1271 // |---------------------| <- klass_slot_offset
duke@435 1272 // | oopHandle area |
duke@435 1273 // |---------------------| <- oop_handle_offset (6 java arg registers)
duke@435 1274 // | outbound memory |
duke@435 1275 // | based arguments |
duke@435 1276 // | |
duke@435 1277 // |---------------------|
duke@435 1278 // | |
duke@435 1279 // SP-> | out_preserved_slots |
duke@435 1280 //
duke@435 1281 //
duke@435 1282
duke@435 1283
duke@435 1284 // Now compute actual number of stack words we need rounding to make
duke@435 1285 // stack properly aligned.
duke@435 1286 stack_slots = round_to(stack_slots, 4 * VMRegImpl::slots_per_word);
duke@435 1287
duke@435 1288 int stack_size = stack_slots * VMRegImpl::stack_slot_size;
duke@435 1289
duke@435 1290
duke@435 1291 // First thing make an ic check to see if we should even be here
duke@435 1292
duke@435 1293 // We are free to use all registers as temps without saving them and
duke@435 1294 // restoring them except rbp. rbp is the only callee save register
duke@435 1295 // as far as the interpreter and the compiler(s) are concerned.
duke@435 1296
duke@435 1297
duke@435 1298 const Register ic_reg = rax;
duke@435 1299 const Register receiver = j_rarg0;
coleenp@548 1300 const Register tmp = rdx;
duke@435 1301
duke@435 1302 Label ok;
duke@435 1303 Label exception_pending;
duke@435 1304
duke@435 1305 __ verify_oop(receiver);
coleenp@548 1306 __ pushq(tmp); // spill (any other registers free here???)
coleenp@548 1307 __ load_klass(tmp, receiver);
coleenp@548 1308 __ cmpq(ic_reg, tmp);
duke@435 1309 __ jcc(Assembler::equal, ok);
duke@435 1310
coleenp@548 1311 __ popq(tmp);
duke@435 1312 __ jump(RuntimeAddress(SharedRuntime::get_ic_miss_stub()));
duke@435 1313
coleenp@548 1314 __ bind(ok);
coleenp@548 1315 __ popq(tmp);
coleenp@548 1316
duke@435 1317 // Verified entry point must be aligned
duke@435 1318 __ align(8);
duke@435 1319
duke@435 1320 int vep_offset = ((intptr_t)__ pc()) - start;
duke@435 1321
duke@435 1322 // The instruction at the verified entry point must be 5 bytes or longer
duke@435 1323 // because it can be patched on the fly by make_non_entrant. The stack bang
duke@435 1324 // instruction fits that requirement.
duke@435 1325
duke@435 1326 // Generate stack overflow check
duke@435 1327
duke@435 1328 if (UseStackBanging) {
duke@435 1329 __ bang_stack_with_offset(StackShadowPages*os::vm_page_size());
duke@435 1330 } else {
duke@435 1331 // need a 5 byte instruction to allow MT safe patching to non-entrant
duke@435 1332 __ fat_nop();
duke@435 1333 }
duke@435 1334
duke@435 1335 // Generate a new frame for the wrapper.
duke@435 1336 __ enter();
duke@435 1337 // -2 because return address is already present and so is saved rbp
duke@435 1338 __ subq(rsp, stack_size - 2*wordSize);
duke@435 1339
duke@435 1340 // Frame is now completed as far as size and linkage.
duke@435 1341
duke@435 1342 int frame_complete = ((intptr_t)__ pc()) - start;
duke@435 1343
duke@435 1344 #ifdef ASSERT
duke@435 1345 {
duke@435 1346 Label L;
duke@435 1347 __ movq(rax, rsp);
duke@435 1348 __ andq(rax, -16); // must be 16 byte boundry (see amd64 ABI)
duke@435 1349 __ cmpq(rax, rsp);
duke@435 1350 __ jcc(Assembler::equal, L);
duke@435 1351 __ stop("improperly aligned stack");
duke@435 1352 __ bind(L);
duke@435 1353 }
duke@435 1354 #endif /* ASSERT */
duke@435 1355
duke@435 1356
duke@435 1357 // We use r14 as the oop handle for the receiver/klass
duke@435 1358 // It is callee save so it survives the call to native
duke@435 1359
duke@435 1360 const Register oop_handle_reg = r14;
duke@435 1361
duke@435 1362
duke@435 1363
duke@435 1364 //
duke@435 1365 // We immediately shuffle the arguments so that any vm call we have to
duke@435 1366 // make from here on out (sync slow path, jvmti, etc.) we will have
duke@435 1367 // captured the oops from our caller and have a valid oopMap for
duke@435 1368 // them.
duke@435 1369
duke@435 1370 // -----------------
duke@435 1371 // The Grand Shuffle
duke@435 1372
duke@435 1373 // The Java calling convention is either equal (linux) or denser (win64) than the
duke@435 1374 // c calling convention. However the because of the jni_env argument the c calling
duke@435 1375 // convention always has at least one more (and two for static) arguments than Java.
duke@435 1376 // Therefore if we move the args from java -> c backwards then we will never have
duke@435 1377 // a register->register conflict and we don't have to build a dependency graph
duke@435 1378 // and figure out how to break any cycles.
duke@435 1379 //
duke@435 1380
duke@435 1381 // Record esp-based slot for receiver on stack for non-static methods
duke@435 1382 int receiver_offset = -1;
duke@435 1383
duke@435 1384 // This is a trick. We double the stack slots so we can claim
duke@435 1385 // the oops in the caller's frame. Since we are sure to have
duke@435 1386 // more args than the caller doubling is enough to make
duke@435 1387 // sure we can capture all the incoming oop args from the
duke@435 1388 // caller.
duke@435 1389 //
duke@435 1390 OopMap* map = new OopMap(stack_slots * 2, 0 /* arg_slots*/);
duke@435 1391
duke@435 1392 // Mark location of rbp (someday)
duke@435 1393 // map->set_callee_saved(VMRegImpl::stack2reg( stack_slots - 2), stack_slots * 2, 0, vmreg(rbp));
duke@435 1394
duke@435 1395 // Use eax, ebx as temporaries during any memory-memory moves we have to do
duke@435 1396 // All inbound args are referenced based on rbp and all outbound args via rsp.
duke@435 1397
duke@435 1398
duke@435 1399 #ifdef ASSERT
duke@435 1400 bool reg_destroyed[RegisterImpl::number_of_registers];
duke@435 1401 bool freg_destroyed[XMMRegisterImpl::number_of_registers];
duke@435 1402 for ( int r = 0 ; r < RegisterImpl::number_of_registers ; r++ ) {
duke@435 1403 reg_destroyed[r] = false;
duke@435 1404 }
duke@435 1405 for ( int f = 0 ; f < XMMRegisterImpl::number_of_registers ; f++ ) {
duke@435 1406 freg_destroyed[f] = false;
duke@435 1407 }
duke@435 1408
duke@435 1409 #endif /* ASSERT */
duke@435 1410
duke@435 1411
duke@435 1412 int c_arg = total_c_args - 1;
duke@435 1413 for ( int i = total_in_args - 1; i >= 0 ; i--, c_arg-- ) {
duke@435 1414 #ifdef ASSERT
duke@435 1415 if (in_regs[i].first()->is_Register()) {
duke@435 1416 assert(!reg_destroyed[in_regs[i].first()->as_Register()->encoding()], "destroyed reg!");
duke@435 1417 } else if (in_regs[i].first()->is_XMMRegister()) {
duke@435 1418 assert(!freg_destroyed[in_regs[i].first()->as_XMMRegister()->encoding()], "destroyed reg!");
duke@435 1419 }
duke@435 1420 if (out_regs[c_arg].first()->is_Register()) {
duke@435 1421 reg_destroyed[out_regs[c_arg].first()->as_Register()->encoding()] = true;
duke@435 1422 } else if (out_regs[c_arg].first()->is_XMMRegister()) {
duke@435 1423 freg_destroyed[out_regs[c_arg].first()->as_XMMRegister()->encoding()] = true;
duke@435 1424 }
duke@435 1425 #endif /* ASSERT */
duke@435 1426 switch (in_sig_bt[i]) {
duke@435 1427 case T_ARRAY:
duke@435 1428 case T_OBJECT:
duke@435 1429 object_move(masm, map, oop_handle_offset, stack_slots, in_regs[i], out_regs[c_arg],
duke@435 1430 ((i == 0) && (!is_static)),
duke@435 1431 &receiver_offset);
duke@435 1432 break;
duke@435 1433 case T_VOID:
duke@435 1434 break;
duke@435 1435
duke@435 1436 case T_FLOAT:
duke@435 1437 float_move(masm, in_regs[i], out_regs[c_arg]);
duke@435 1438 break;
duke@435 1439
duke@435 1440 case T_DOUBLE:
duke@435 1441 assert( i + 1 < total_in_args &&
duke@435 1442 in_sig_bt[i + 1] == T_VOID &&
duke@435 1443 out_sig_bt[c_arg+1] == T_VOID, "bad arg list");
duke@435 1444 double_move(masm, in_regs[i], out_regs[c_arg]);
duke@435 1445 break;
duke@435 1446
duke@435 1447 case T_LONG :
duke@435 1448 long_move(masm, in_regs[i], out_regs[c_arg]);
duke@435 1449 break;
duke@435 1450
duke@435 1451 case T_ADDRESS: assert(false, "found T_ADDRESS in java args");
duke@435 1452
duke@435 1453 default:
duke@435 1454 move32_64(masm, in_regs[i], out_regs[c_arg]);
duke@435 1455 }
duke@435 1456 }
duke@435 1457
duke@435 1458 // point c_arg at the first arg that is already loaded in case we
duke@435 1459 // need to spill before we call out
duke@435 1460 c_arg++;
duke@435 1461
duke@435 1462 // Pre-load a static method's oop into r14. Used both by locking code and
duke@435 1463 // the normal JNI call code.
duke@435 1464 if (method->is_static()) {
duke@435 1465
duke@435 1466 // load oop into a register
duke@435 1467 __ movoop(oop_handle_reg, JNIHandles::make_local(Klass::cast(method->method_holder())->java_mirror()));
duke@435 1468
duke@435 1469 // Now handlize the static class mirror it's known not-null.
duke@435 1470 __ movq(Address(rsp, klass_offset), oop_handle_reg);
duke@435 1471 map->set_oop(VMRegImpl::stack2reg(klass_slot_offset));
duke@435 1472
duke@435 1473 // Now get the handle
duke@435 1474 __ leaq(oop_handle_reg, Address(rsp, klass_offset));
duke@435 1475 // store the klass handle as second argument
duke@435 1476 __ movq(c_rarg1, oop_handle_reg);
duke@435 1477 // and protect the arg if we must spill
duke@435 1478 c_arg--;
duke@435 1479 }
duke@435 1480
duke@435 1481 // Change state to native (we save the return address in the thread, since it might not
duke@435 1482 // be pushed on the stack when we do a a stack traversal). It is enough that the pc()
duke@435 1483 // points into the right code segment. It does not have to be the correct return pc.
duke@435 1484 // We use the same pc/oopMap repeatedly when we call out
duke@435 1485
duke@435 1486 intptr_t the_pc = (intptr_t) __ pc();
duke@435 1487 oop_maps->add_gc_map(the_pc - start, map);
duke@435 1488
duke@435 1489 __ set_last_Java_frame(rsp, noreg, (address)the_pc);
duke@435 1490
duke@435 1491
duke@435 1492 // We have all of the arguments setup at this point. We must not touch any register
duke@435 1493 // argument registers at this point (what if we save/restore them there are no oop?
duke@435 1494
duke@435 1495 {
duke@435 1496 SkipIfEqual skip(masm, &DTraceMethodProbes, false);
duke@435 1497 // protect the args we've loaded
duke@435 1498 save_args(masm, total_c_args, c_arg, out_regs);
duke@435 1499 __ movoop(c_rarg1, JNIHandles::make_local(method()));
duke@435 1500 __ call_VM_leaf(
duke@435 1501 CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_entry),
duke@435 1502 r15_thread, c_rarg1);
duke@435 1503 restore_args(masm, total_c_args, c_arg, out_regs);
duke@435 1504 }
duke@435 1505
duke@435 1506 // Lock a synchronized method
duke@435 1507
duke@435 1508 // Register definitions used by locking and unlocking
duke@435 1509
duke@435 1510 const Register swap_reg = rax; // Must use rax for cmpxchg instruction
duke@435 1511 const Register obj_reg = rbx; // Will contain the oop
duke@435 1512 const Register lock_reg = r13; // Address of compiler lock object (BasicLock)
duke@435 1513 const Register old_hdr = r13; // value of old header at unlock time
duke@435 1514
duke@435 1515 Label slow_path_lock;
duke@435 1516 Label lock_done;
duke@435 1517
duke@435 1518 if (method->is_synchronized()) {
duke@435 1519
duke@435 1520
duke@435 1521 const int mark_word_offset = BasicLock::displaced_header_offset_in_bytes();
duke@435 1522
duke@435 1523 // Get the handle (the 2nd argument)
duke@435 1524 __ movq(oop_handle_reg, c_rarg1);
duke@435 1525
duke@435 1526 // Get address of the box
duke@435 1527
duke@435 1528 __ leaq(lock_reg, Address(rsp, lock_slot_offset * VMRegImpl::stack_slot_size));
duke@435 1529
duke@435 1530 // Load the oop from the handle
duke@435 1531 __ movq(obj_reg, Address(oop_handle_reg, 0));
duke@435 1532
duke@435 1533 if (UseBiasedLocking) {
duke@435 1534 __ biased_locking_enter(lock_reg, obj_reg, swap_reg, rscratch1, false, lock_done, &slow_path_lock);
duke@435 1535 }
duke@435 1536
duke@435 1537 // Load immediate 1 into swap_reg %rax
duke@435 1538 __ movl(swap_reg, 1);
duke@435 1539
duke@435 1540 // Load (object->mark() | 1) into swap_reg %rax
duke@435 1541 __ orq(swap_reg, Address(obj_reg, 0));
duke@435 1542
duke@435 1543 // Save (object->mark() | 1) into BasicLock's displaced header
duke@435 1544 __ movq(Address(lock_reg, mark_word_offset), swap_reg);
duke@435 1545
duke@435 1546 if (os::is_MP()) {
duke@435 1547 __ lock();
duke@435 1548 }
duke@435 1549
duke@435 1550 // src -> dest iff dest == rax else rax <- dest
duke@435 1551 __ cmpxchgq(lock_reg, Address(obj_reg, 0));
duke@435 1552 __ jcc(Assembler::equal, lock_done);
duke@435 1553
duke@435 1554 // Hmm should this move to the slow path code area???
duke@435 1555
duke@435 1556 // Test if the oopMark is an obvious stack pointer, i.e.,
duke@435 1557 // 1) (mark & 3) == 0, and
duke@435 1558 // 2) rsp <= mark < mark + os::pagesize()
duke@435 1559 // These 3 tests can be done by evaluating the following
duke@435 1560 // expression: ((mark - rsp) & (3 - os::vm_page_size())),
duke@435 1561 // assuming both stack pointer and pagesize have their
duke@435 1562 // least significant 2 bits clear.
duke@435 1563 // NOTE: the oopMark is in swap_reg %rax as the result of cmpxchg
duke@435 1564
duke@435 1565 __ subq(swap_reg, rsp);
duke@435 1566 __ andq(swap_reg, 3 - os::vm_page_size());
duke@435 1567
duke@435 1568 // Save the test result, for recursive case, the result is zero
duke@435 1569 __ movq(Address(lock_reg, mark_word_offset), swap_reg);
duke@435 1570 __ jcc(Assembler::notEqual, slow_path_lock);
duke@435 1571
duke@435 1572 // Slow path will re-enter here
duke@435 1573
duke@435 1574 __ bind(lock_done);
duke@435 1575 }
duke@435 1576
duke@435 1577
duke@435 1578 // Finally just about ready to make the JNI call
duke@435 1579
duke@435 1580
duke@435 1581 // get JNIEnv* which is first argument to native
duke@435 1582
duke@435 1583 __ leaq(c_rarg0, Address(r15_thread, in_bytes(JavaThread::jni_environment_offset())));
duke@435 1584
duke@435 1585 // Now set thread in native
duke@435 1586 __ mov64(Address(r15_thread, JavaThread::thread_state_offset()), _thread_in_native);
duke@435 1587
duke@435 1588 __ call(RuntimeAddress(method->native_function()));
duke@435 1589
duke@435 1590 // Either restore the MXCSR register after returning from the JNI Call
duke@435 1591 // or verify that it wasn't changed.
duke@435 1592 if (RestoreMXCSROnJNICalls) {
duke@435 1593 __ ldmxcsr(ExternalAddress(StubRoutines::amd64::mxcsr_std()));
duke@435 1594
duke@435 1595 }
duke@435 1596 else if (CheckJNICalls ) {
duke@435 1597 __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, StubRoutines::amd64::verify_mxcsr_entry())));
duke@435 1598 }
duke@435 1599
duke@435 1600
duke@435 1601 // Unpack native results.
duke@435 1602 switch (ret_type) {
duke@435 1603 case T_BOOLEAN: __ c2bool(rax); break;
duke@435 1604 case T_CHAR : __ movzwl(rax, rax); break;
duke@435 1605 case T_BYTE : __ sign_extend_byte (rax); break;
duke@435 1606 case T_SHORT : __ sign_extend_short(rax); break;
duke@435 1607 case T_INT : /* nothing to do */ break;
duke@435 1608 case T_DOUBLE :
duke@435 1609 case T_FLOAT :
duke@435 1610 // Result is in xmm0 we'll save as needed
duke@435 1611 break;
duke@435 1612 case T_ARRAY: // Really a handle
duke@435 1613 case T_OBJECT: // Really a handle
duke@435 1614 break; // can't de-handlize until after safepoint check
duke@435 1615 case T_VOID: break;
duke@435 1616 case T_LONG: break;
duke@435 1617 default : ShouldNotReachHere();
duke@435 1618 }
duke@435 1619
duke@435 1620 // Switch thread to "native transition" state before reading the synchronization state.
duke@435 1621 // This additional state is necessary because reading and testing the synchronization
duke@435 1622 // state is not atomic w.r.t. GC, as this scenario demonstrates:
duke@435 1623 // Java thread A, in _thread_in_native state, loads _not_synchronized and is preempted.
duke@435 1624 // VM thread changes sync state to synchronizing and suspends threads for GC.
duke@435 1625 // Thread A is resumed to finish this native method, but doesn't block here since it
duke@435 1626 // didn't see any synchronization is progress, and escapes.
duke@435 1627 __ mov64(Address(r15_thread, JavaThread::thread_state_offset()), _thread_in_native_trans);
duke@435 1628
duke@435 1629 if(os::is_MP()) {
duke@435 1630 if (UseMembar) {
duke@435 1631 // Force this write out before the read below
duke@435 1632 __ membar(Assembler::Membar_mask_bits(
duke@435 1633 Assembler::LoadLoad | Assembler::LoadStore |
duke@435 1634 Assembler::StoreLoad | Assembler::StoreStore));
duke@435 1635 } else {
duke@435 1636 // Write serialization page so VM thread can do a pseudo remote membar.
duke@435 1637 // We use the current thread pointer to calculate a thread specific
duke@435 1638 // offset to write to within the page. This minimizes bus traffic
duke@435 1639 // due to cache line collision.
duke@435 1640 __ serialize_memory(r15_thread, rcx);
duke@435 1641 }
duke@435 1642 }
duke@435 1643
duke@435 1644
duke@435 1645 // check for safepoint operation in progress and/or pending suspend requests
duke@435 1646 {
duke@435 1647 Label Continue;
duke@435 1648
duke@435 1649 __ cmp32(ExternalAddress((address)SafepointSynchronize::address_of_state()),
duke@435 1650 SafepointSynchronize::_not_synchronized);
duke@435 1651
duke@435 1652 Label L;
duke@435 1653 __ jcc(Assembler::notEqual, L);
duke@435 1654 __ cmpl(Address(r15_thread, JavaThread::suspend_flags_offset()), 0);
duke@435 1655 __ jcc(Assembler::equal, Continue);
duke@435 1656 __ bind(L);
duke@435 1657
duke@435 1658 // Don't use call_VM as it will see a possible pending exception and forward it
duke@435 1659 // and never return here preventing us from clearing _last_native_pc down below.
duke@435 1660 // Also can't use call_VM_leaf either as it will check to see if rsi & rdi are
duke@435 1661 // preserved and correspond to the bcp/locals pointers. So we do a runtime call
duke@435 1662 // by hand.
duke@435 1663 //
duke@435 1664 save_native_result(masm, ret_type, stack_slots);
duke@435 1665 __ movq(c_rarg0, r15_thread);
duke@435 1666 __ movq(r12, rsp); // remember sp
duke@435 1667 __ subq(rsp, frame::arg_reg_save_area_bytes); // windows
duke@435 1668 __ andq(rsp, -16); // align stack as required by ABI
duke@435 1669 __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, JavaThread::check_special_condition_for_native_trans)));
duke@435 1670 __ movq(rsp, r12); // restore sp
coleenp@548 1671 __ reinit_heapbase();
duke@435 1672 // Restore any method result value
duke@435 1673 restore_native_result(masm, ret_type, stack_slots);
duke@435 1674 __ bind(Continue);
duke@435 1675 }
duke@435 1676
duke@435 1677 // change thread state
duke@435 1678 __ movl(Address(r15_thread, JavaThread::thread_state_offset()), _thread_in_Java);
duke@435 1679
duke@435 1680 Label reguard;
duke@435 1681 Label reguard_done;
duke@435 1682 __ cmpl(Address(r15_thread, JavaThread::stack_guard_state_offset()), JavaThread::stack_guard_yellow_disabled);
duke@435 1683 __ jcc(Assembler::equal, reguard);
duke@435 1684 __ bind(reguard_done);
duke@435 1685
duke@435 1686 // native result if any is live
duke@435 1687
duke@435 1688 // Unlock
duke@435 1689 Label unlock_done;
duke@435 1690 Label slow_path_unlock;
duke@435 1691 if (method->is_synchronized()) {
duke@435 1692
duke@435 1693 // Get locked oop from the handle we passed to jni
duke@435 1694 __ movq(obj_reg, Address(oop_handle_reg, 0));
duke@435 1695
duke@435 1696 Label done;
duke@435 1697
duke@435 1698 if (UseBiasedLocking) {
duke@435 1699 __ biased_locking_exit(obj_reg, old_hdr, done);
duke@435 1700 }
duke@435 1701
duke@435 1702 // Simple recursive lock?
duke@435 1703
duke@435 1704 __ cmpq(Address(rsp, lock_slot_offset * VMRegImpl::stack_slot_size), (int)NULL_WORD);
duke@435 1705 __ jcc(Assembler::equal, done);
duke@435 1706
duke@435 1707 // Must save rax if if it is live now because cmpxchg must use it
duke@435 1708 if (ret_type != T_FLOAT && ret_type != T_DOUBLE && ret_type != T_VOID) {
duke@435 1709 save_native_result(masm, ret_type, stack_slots);
duke@435 1710 }
duke@435 1711
duke@435 1712
duke@435 1713 // get address of the stack lock
duke@435 1714 __ leaq(rax, Address(rsp, lock_slot_offset * VMRegImpl::stack_slot_size));
duke@435 1715 // get old displaced header
duke@435 1716 __ movq(old_hdr, Address(rax, 0));
duke@435 1717
duke@435 1718 // Atomic swap old header if oop still contains the stack lock
duke@435 1719 if (os::is_MP()) {
duke@435 1720 __ lock();
duke@435 1721 }
duke@435 1722 __ cmpxchgq(old_hdr, Address(obj_reg, 0));
duke@435 1723 __ jcc(Assembler::notEqual, slow_path_unlock);
duke@435 1724
duke@435 1725 // slow path re-enters here
duke@435 1726 __ bind(unlock_done);
duke@435 1727 if (ret_type != T_FLOAT && ret_type != T_DOUBLE && ret_type != T_VOID) {
duke@435 1728 restore_native_result(masm, ret_type, stack_slots);
duke@435 1729 }
duke@435 1730
duke@435 1731 __ bind(done);
duke@435 1732
duke@435 1733 }
duke@435 1734 {
duke@435 1735 SkipIfEqual skip(masm, &DTraceMethodProbes, false);
duke@435 1736 save_native_result(masm, ret_type, stack_slots);
duke@435 1737 __ movoop(c_rarg1, JNIHandles::make_local(method()));
duke@435 1738 __ call_VM_leaf(
duke@435 1739 CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_exit),
duke@435 1740 r15_thread, c_rarg1);
duke@435 1741 restore_native_result(masm, ret_type, stack_slots);
duke@435 1742 }
duke@435 1743
duke@435 1744 __ reset_last_Java_frame(false, true);
duke@435 1745
duke@435 1746 // Unpack oop result
duke@435 1747 if (ret_type == T_OBJECT || ret_type == T_ARRAY) {
duke@435 1748 Label L;
duke@435 1749 __ testq(rax, rax);
duke@435 1750 __ jcc(Assembler::zero, L);
duke@435 1751 __ movq(rax, Address(rax, 0));
duke@435 1752 __ bind(L);
duke@435 1753 __ verify_oop(rax);
duke@435 1754 }
duke@435 1755
duke@435 1756 // reset handle block
duke@435 1757 __ movq(rcx, Address(r15_thread, JavaThread::active_handles_offset()));
duke@435 1758 __ movptr(Address(rcx, JNIHandleBlock::top_offset_in_bytes()), (int)NULL_WORD);
duke@435 1759
duke@435 1760 // pop our frame
duke@435 1761
duke@435 1762 __ leave();
duke@435 1763
duke@435 1764 // Any exception pending?
duke@435 1765 __ cmpq(Address(r15_thread, in_bytes(Thread::pending_exception_offset())), (int)NULL_WORD);
duke@435 1766 __ jcc(Assembler::notEqual, exception_pending);
duke@435 1767
duke@435 1768 // Return
duke@435 1769
duke@435 1770 __ ret(0);
duke@435 1771
duke@435 1772 // Unexpected paths are out of line and go here
duke@435 1773
duke@435 1774 // forward the exception
duke@435 1775 __ bind(exception_pending);
duke@435 1776
duke@435 1777 // and forward the exception
duke@435 1778 __ jump(RuntimeAddress(StubRoutines::forward_exception_entry()));
duke@435 1779
duke@435 1780
duke@435 1781 // Slow path locking & unlocking
duke@435 1782 if (method->is_synchronized()) {
duke@435 1783
duke@435 1784 // BEGIN Slow path lock
duke@435 1785 __ bind(slow_path_lock);
duke@435 1786
duke@435 1787 // has last_Java_frame setup. No exceptions so do vanilla call not call_VM
duke@435 1788 // args are (oop obj, BasicLock* lock, JavaThread* thread)
duke@435 1789
duke@435 1790 // protect the args we've loaded
duke@435 1791 save_args(masm, total_c_args, c_arg, out_regs);
duke@435 1792
duke@435 1793 __ movq(c_rarg0, obj_reg);
duke@435 1794 __ movq(c_rarg1, lock_reg);
duke@435 1795 __ movq(c_rarg2, r15_thread);
duke@435 1796
duke@435 1797 // Not a leaf but we have last_Java_frame setup as we want
duke@435 1798 __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::complete_monitor_locking_C), 3);
duke@435 1799 restore_args(masm, total_c_args, c_arg, out_regs);
duke@435 1800
duke@435 1801 #ifdef ASSERT
duke@435 1802 { Label L;
duke@435 1803 __ cmpq(Address(r15_thread, in_bytes(Thread::pending_exception_offset())), (int)NULL_WORD);
duke@435 1804 __ jcc(Assembler::equal, L);
duke@435 1805 __ stop("no pending exception allowed on exit from monitorenter");
duke@435 1806 __ bind(L);
duke@435 1807 }
duke@435 1808 #endif
duke@435 1809 __ jmp(lock_done);
duke@435 1810
duke@435 1811 // END Slow path lock
duke@435 1812
duke@435 1813 // BEGIN Slow path unlock
duke@435 1814 __ bind(slow_path_unlock);
duke@435 1815
duke@435 1816 // If we haven't already saved the native result we must save it now as xmm registers
duke@435 1817 // are still exposed.
duke@435 1818
duke@435 1819 if (ret_type == T_FLOAT || ret_type == T_DOUBLE ) {
duke@435 1820 save_native_result(masm, ret_type, stack_slots);
duke@435 1821 }
duke@435 1822
duke@435 1823 __ leaq(c_rarg1, Address(rsp, lock_slot_offset * VMRegImpl::stack_slot_size));
duke@435 1824
duke@435 1825 __ movq(c_rarg0, obj_reg);
duke@435 1826 __ movq(r12, rsp); // remember sp
duke@435 1827 __ subq(rsp, frame::arg_reg_save_area_bytes); // windows
duke@435 1828 __ andq(rsp, -16); // align stack as required by ABI
duke@435 1829
duke@435 1830 // Save pending exception around call to VM (which contains an EXCEPTION_MARK)
duke@435 1831 // NOTE that obj_reg == rbx currently
duke@435 1832 __ movq(rbx, Address(r15_thread, in_bytes(Thread::pending_exception_offset())));
duke@435 1833 __ movptr(Address(r15_thread, in_bytes(Thread::pending_exception_offset())), (int)NULL_WORD);
duke@435 1834
duke@435 1835 __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, SharedRuntime::complete_monitor_unlocking_C)));
duke@435 1836 __ movq(rsp, r12); // restore sp
coleenp@548 1837 __ reinit_heapbase();
duke@435 1838 #ifdef ASSERT
duke@435 1839 {
duke@435 1840 Label L;
duke@435 1841 __ cmpq(Address(r15_thread, in_bytes(Thread::pending_exception_offset())), (int)NULL_WORD);
duke@435 1842 __ jcc(Assembler::equal, L);
duke@435 1843 __ stop("no pending exception allowed on exit complete_monitor_unlocking_C");
duke@435 1844 __ bind(L);
duke@435 1845 }
duke@435 1846 #endif /* ASSERT */
duke@435 1847
duke@435 1848 __ movq(Address(r15_thread, in_bytes(Thread::pending_exception_offset())), rbx);
duke@435 1849
duke@435 1850 if (ret_type == T_FLOAT || ret_type == T_DOUBLE ) {
duke@435 1851 restore_native_result(masm, ret_type, stack_slots);
duke@435 1852 }
duke@435 1853 __ jmp(unlock_done);
duke@435 1854
duke@435 1855 // END Slow path unlock
duke@435 1856
duke@435 1857 } // synchronized
duke@435 1858
duke@435 1859 // SLOW PATH Reguard the stack if needed
duke@435 1860
duke@435 1861 __ bind(reguard);
duke@435 1862 save_native_result(masm, ret_type, stack_slots);
duke@435 1863 __ movq(r12, rsp); // remember sp
duke@435 1864 __ subq(rsp, frame::arg_reg_save_area_bytes); // windows
duke@435 1865 __ andq(rsp, -16); // align stack as required by ABI
duke@435 1866 __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, SharedRuntime::reguard_yellow_pages)));
duke@435 1867 __ movq(rsp, r12); // restore sp
coleenp@548 1868 __ reinit_heapbase();
duke@435 1869 restore_native_result(masm, ret_type, stack_slots);
duke@435 1870 // and continue
duke@435 1871 __ jmp(reguard_done);
duke@435 1872
duke@435 1873
duke@435 1874
duke@435 1875 __ flush();
duke@435 1876
duke@435 1877 nmethod *nm = nmethod::new_native_nmethod(method,
duke@435 1878 masm->code(),
duke@435 1879 vep_offset,
duke@435 1880 frame_complete,
duke@435 1881 stack_slots / VMRegImpl::slots_per_word,
duke@435 1882 (is_static ? in_ByteSize(klass_offset) : in_ByteSize(receiver_offset)),
duke@435 1883 in_ByteSize(lock_slot_offset*VMRegImpl::stack_slot_size),
duke@435 1884 oop_maps);
duke@435 1885 return nm;
duke@435 1886
duke@435 1887 }
duke@435 1888
duke@435 1889 // this function returns the adjust size (in number of words) to a c2i adapter
duke@435 1890 // activation for use during deoptimization
duke@435 1891 int Deoptimization::last_frame_adjust(int callee_parameters, int callee_locals ) {
duke@435 1892 return (callee_locals - callee_parameters) * Interpreter::stackElementWords();
duke@435 1893 }
duke@435 1894
duke@435 1895
duke@435 1896 uint SharedRuntime::out_preserve_stack_slots() {
duke@435 1897 return 0;
duke@435 1898 }
duke@435 1899
duke@435 1900
duke@435 1901 //------------------------------generate_deopt_blob----------------------------
duke@435 1902 void SharedRuntime::generate_deopt_blob() {
duke@435 1903 // Allocate space for the code
duke@435 1904 ResourceMark rm;
duke@435 1905 // Setup code generation tools
duke@435 1906 CodeBuffer buffer("deopt_blob", 2048, 1024);
duke@435 1907 MacroAssembler* masm = new MacroAssembler(&buffer);
duke@435 1908 int frame_size_in_words;
duke@435 1909 OopMap* map = NULL;
duke@435 1910 OopMapSet *oop_maps = new OopMapSet();
duke@435 1911
duke@435 1912 // -------------
duke@435 1913 // This code enters when returning to a de-optimized nmethod. A return
duke@435 1914 // address has been pushed on the the stack, and return values are in
duke@435 1915 // registers.
duke@435 1916 // If we are doing a normal deopt then we were called from the patched
duke@435 1917 // nmethod from the point we returned to the nmethod. So the return
duke@435 1918 // address on the stack is wrong by NativeCall::instruction_size
duke@435 1919 // We will adjust the value so it looks like we have the original return
duke@435 1920 // address on the stack (like when we eagerly deoptimized).
duke@435 1921 // In the case of an exception pending when deoptimizing, we enter
duke@435 1922 // with a return address on the stack that points after the call we patched
duke@435 1923 // into the exception handler. We have the following register state from,
duke@435 1924 // e.g., the forward exception stub (see stubGenerator_x86_64.cpp).
duke@435 1925 // rax: exception oop
duke@435 1926 // rbx: exception handler
duke@435 1927 // rdx: throwing pc
duke@435 1928 // So in this case we simply jam rdx into the useless return address and
duke@435 1929 // the stack looks just like we want.
duke@435 1930 //
duke@435 1931 // At this point we need to de-opt. We save the argument return
duke@435 1932 // registers. We call the first C routine, fetch_unroll_info(). This
duke@435 1933 // routine captures the return values and returns a structure which
duke@435 1934 // describes the current frame size and the sizes of all replacement frames.
duke@435 1935 // The current frame is compiled code and may contain many inlined
duke@435 1936 // functions, each with their own JVM state. We pop the current frame, then
duke@435 1937 // push all the new frames. Then we call the C routine unpack_frames() to
duke@435 1938 // populate these frames. Finally unpack_frames() returns us the new target
duke@435 1939 // address. Notice that callee-save registers are BLOWN here; they have
duke@435 1940 // already been captured in the vframeArray at the time the return PC was
duke@435 1941 // patched.
duke@435 1942 address start = __ pc();
duke@435 1943 Label cont;
duke@435 1944
duke@435 1945 // Prolog for non exception case!
duke@435 1946
duke@435 1947 // Save everything in sight.
duke@435 1948 map = RegisterSaver::save_live_registers(masm, 0, &frame_size_in_words);
duke@435 1949
duke@435 1950 // Normal deoptimization. Save exec mode for unpack_frames.
coleenp@548 1951 __ movl(r14, Deoptimization::Unpack_deopt); // callee-saved
duke@435 1952 __ jmp(cont);
duke@435 1953 int exception_offset = __ pc() - start;
duke@435 1954
duke@435 1955 // Prolog for exception case
duke@435 1956
duke@435 1957 // Push throwing pc as return address
duke@435 1958 __ pushq(rdx);
duke@435 1959
duke@435 1960 // Save everything in sight.
duke@435 1961 map = RegisterSaver::save_live_registers(masm, 0, &frame_size_in_words);
duke@435 1962
duke@435 1963 // Deopt during an exception. Save exec mode for unpack_frames.
coleenp@548 1964 __ movl(r14, Deoptimization::Unpack_exception); // callee-saved
duke@435 1965
duke@435 1966 __ bind(cont);
duke@435 1967
duke@435 1968 // Call C code. Need thread and this frame, but NOT official VM entry
duke@435 1969 // crud. We cannot block on this call, no GC can happen.
duke@435 1970 //
duke@435 1971 // UnrollBlock* fetch_unroll_info(JavaThread* thread)
duke@435 1972
duke@435 1973 // fetch_unroll_info needs to call last_java_frame().
duke@435 1974
duke@435 1975 __ set_last_Java_frame(noreg, noreg, NULL);
duke@435 1976 #ifdef ASSERT
duke@435 1977 { Label L;
duke@435 1978 __ cmpq(Address(r15_thread,
duke@435 1979 JavaThread::last_Java_fp_offset()),
duke@435 1980 0);
duke@435 1981 __ jcc(Assembler::equal, L);
duke@435 1982 __ stop("SharedRuntime::generate_deopt_blob: last_Java_fp not cleared");
duke@435 1983 __ bind(L);
duke@435 1984 }
duke@435 1985 #endif // ASSERT
duke@435 1986 __ movq(c_rarg0, r15_thread);
duke@435 1987 __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, Deoptimization::fetch_unroll_info)));
duke@435 1988
duke@435 1989 // Need to have an oopmap that tells fetch_unroll_info where to
duke@435 1990 // find any register it might need.
duke@435 1991 oop_maps->add_gc_map(__ pc() - start, map);
duke@435 1992
duke@435 1993 __ reset_last_Java_frame(false, false);
duke@435 1994
duke@435 1995 // Load UnrollBlock* into rdi
duke@435 1996 __ movq(rdi, rax);
duke@435 1997
duke@435 1998 // Only register save data is on the stack.
duke@435 1999 // Now restore the result registers. Everything else is either dead
duke@435 2000 // or captured in the vframeArray.
duke@435 2001 RegisterSaver::restore_result_registers(masm);
duke@435 2002
duke@435 2003 // All of the register save area has been popped of the stack. Only the
duke@435 2004 // return address remains.
duke@435 2005
duke@435 2006 // Pop all the frames we must move/replace.
duke@435 2007 //
duke@435 2008 // Frame picture (youngest to oldest)
duke@435 2009 // 1: self-frame (no frame link)
duke@435 2010 // 2: deopting frame (no frame link)
duke@435 2011 // 3: caller of deopting frame (could be compiled/interpreted).
duke@435 2012 //
duke@435 2013 // Note: by leaving the return address of self-frame on the stack
duke@435 2014 // and using the size of frame 2 to adjust the stack
duke@435 2015 // when we are done the return to frame 3 will still be on the stack.
duke@435 2016
duke@435 2017 // Pop deoptimized frame
duke@435 2018 __ movl(rcx, Address(rdi, Deoptimization::UnrollBlock::size_of_deoptimized_frame_offset_in_bytes()));
duke@435 2019 __ addq(rsp, rcx);
duke@435 2020
duke@435 2021 // rsp should be pointing at the return address to the caller (3)
duke@435 2022
duke@435 2023 // Stack bang to make sure there's enough room for these interpreter frames.
duke@435 2024 if (UseStackBanging) {
duke@435 2025 __ movl(rbx, Address(rdi, Deoptimization::UnrollBlock::total_frame_sizes_offset_in_bytes()));
duke@435 2026 __ bang_stack_size(rbx, rcx);
duke@435 2027 }
duke@435 2028
duke@435 2029 // Load address of array of frame pcs into rcx
duke@435 2030 __ movq(rcx, Address(rdi, Deoptimization::UnrollBlock::frame_pcs_offset_in_bytes()));
duke@435 2031
duke@435 2032 // Trash the old pc
duke@435 2033 __ addq(rsp, wordSize);
duke@435 2034
duke@435 2035 // Load address of array of frame sizes into rsi
duke@435 2036 __ movq(rsi, Address(rdi, Deoptimization::UnrollBlock::frame_sizes_offset_in_bytes()));
duke@435 2037
duke@435 2038 // Load counter into rdx
duke@435 2039 __ movl(rdx, Address(rdi, Deoptimization::UnrollBlock::number_of_frames_offset_in_bytes()));
duke@435 2040
duke@435 2041 // Pick up the initial fp we should save
duke@435 2042 __ movq(rbp, Address(rdi, Deoptimization::UnrollBlock::initial_fp_offset_in_bytes()));
duke@435 2043
duke@435 2044 // Now adjust the caller's stack to make up for the extra locals
duke@435 2045 // but record the original sp so that we can save it in the skeletal interpreter
duke@435 2046 // frame and the stack walking of interpreter_sender will get the unextended sp
duke@435 2047 // value and not the "real" sp value.
duke@435 2048
duke@435 2049 const Register sender_sp = r8;
duke@435 2050
duke@435 2051 __ movq(sender_sp, rsp);
duke@435 2052 __ movl(rbx, Address(rdi,
duke@435 2053 Deoptimization::UnrollBlock::
duke@435 2054 caller_adjustment_offset_in_bytes()));
duke@435 2055 __ subq(rsp, rbx);
duke@435 2056
duke@435 2057 // Push interpreter frames in a loop
duke@435 2058 Label loop;
duke@435 2059 __ bind(loop);
duke@435 2060 __ movq(rbx, Address(rsi, 0)); // Load frame size
duke@435 2061 __ subq(rbx, 2*wordSize); // We'll push pc and ebp by hand
duke@435 2062 __ pushq(Address(rcx, 0)); // Save return address
duke@435 2063 __ enter(); // Save old & set new ebp
duke@435 2064 __ subq(rsp, rbx); // Prolog
duke@435 2065 __ movq(Address(rbp, frame::interpreter_frame_sender_sp_offset * wordSize),
duke@435 2066 sender_sp); // Make it walkable
duke@435 2067 // This value is corrected by layout_activation_impl
duke@435 2068 __ movptr(Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize), (int)NULL_WORD );
duke@435 2069 __ movq(sender_sp, rsp); // Pass sender_sp to next frame
duke@435 2070 __ addq(rsi, wordSize); // Bump array pointer (sizes)
duke@435 2071 __ addq(rcx, wordSize); // Bump array pointer (pcs)
duke@435 2072 __ decrementl(rdx); // Decrement counter
duke@435 2073 __ jcc(Assembler::notZero, loop);
duke@435 2074 __ pushq(Address(rcx, 0)); // Save final return address
duke@435 2075
duke@435 2076 // Re-push self-frame
duke@435 2077 __ enter(); // Save old & set new ebp
duke@435 2078
duke@435 2079 // Allocate a full sized register save area.
duke@435 2080 // Return address and rbp are in place, so we allocate two less words.
duke@435 2081 __ subq(rsp, (frame_size_in_words - 2) * wordSize);
duke@435 2082
duke@435 2083 // Restore frame locals after moving the frame
duke@435 2084 __ movdbl(Address(rsp, RegisterSaver::xmm0_offset_in_bytes()), xmm0);
duke@435 2085 __ movq(Address(rsp, RegisterSaver::rax_offset_in_bytes()), rax);
duke@435 2086
duke@435 2087 // Call C code. Need thread but NOT official VM entry
duke@435 2088 // crud. We cannot block on this call, no GC can happen. Call should
duke@435 2089 // restore return values to their stack-slots with the new SP.
duke@435 2090 //
duke@435 2091 // void Deoptimization::unpack_frames(JavaThread* thread, int exec_mode)
duke@435 2092
duke@435 2093 // Use rbp because the frames look interpreted now
duke@435 2094 __ set_last_Java_frame(noreg, rbp, NULL);
duke@435 2095
duke@435 2096 __ movq(c_rarg0, r15_thread);
coleenp@548 2097 __ movl(c_rarg1, r14); // second arg: exec_mode
duke@435 2098 __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, Deoptimization::unpack_frames)));
duke@435 2099
duke@435 2100 // Set an oopmap for the call site
duke@435 2101 oop_maps->add_gc_map(__ pc() - start,
duke@435 2102 new OopMap( frame_size_in_words, 0 ));
duke@435 2103
duke@435 2104 __ reset_last_Java_frame(true, false);
duke@435 2105
duke@435 2106 // Collect return values
duke@435 2107 __ movdbl(xmm0, Address(rsp, RegisterSaver::xmm0_offset_in_bytes()));
duke@435 2108 __ movq(rax, Address(rsp, RegisterSaver::rax_offset_in_bytes()));
duke@435 2109
duke@435 2110 // Pop self-frame.
duke@435 2111 __ leave(); // Epilog
duke@435 2112
duke@435 2113 // Jump to interpreter
duke@435 2114 __ ret(0);
duke@435 2115
duke@435 2116 // Make sure all code is generated
duke@435 2117 masm->flush();
duke@435 2118
duke@435 2119 _deopt_blob = DeoptimizationBlob::create(&buffer, oop_maps, 0, exception_offset, 0, frame_size_in_words);
duke@435 2120 }
duke@435 2121
duke@435 2122 #ifdef COMPILER2
duke@435 2123 //------------------------------generate_uncommon_trap_blob--------------------
duke@435 2124 void SharedRuntime::generate_uncommon_trap_blob() {
duke@435 2125 // Allocate space for the code
duke@435 2126 ResourceMark rm;
duke@435 2127 // Setup code generation tools
duke@435 2128 CodeBuffer buffer("uncommon_trap_blob", 2048, 1024);
duke@435 2129 MacroAssembler* masm = new MacroAssembler(&buffer);
duke@435 2130
duke@435 2131 assert(SimpleRuntimeFrame::framesize % 4 == 0, "sp not 16-byte aligned");
duke@435 2132
duke@435 2133 address start = __ pc();
duke@435 2134
duke@435 2135 // Push self-frame. We get here with a return address on the
duke@435 2136 // stack, so rsp is 8-byte aligned until we allocate our frame.
duke@435 2137 __ subq(rsp, SimpleRuntimeFrame::return_off << LogBytesPerInt); // Epilog!
duke@435 2138
duke@435 2139 // No callee saved registers. rbp is assumed implicitly saved
duke@435 2140 __ movq(Address(rsp, SimpleRuntimeFrame::rbp_off << LogBytesPerInt), rbp);
duke@435 2141
duke@435 2142 // compiler left unloaded_class_index in j_rarg0 move to where the
duke@435 2143 // runtime expects it.
duke@435 2144 __ movl(c_rarg1, j_rarg0);
duke@435 2145
duke@435 2146 __ set_last_Java_frame(noreg, noreg, NULL);
duke@435 2147
duke@435 2148 // Call C code. Need thread but NOT official VM entry
duke@435 2149 // crud. We cannot block on this call, no GC can happen. Call should
duke@435 2150 // capture callee-saved registers as well as return values.
duke@435 2151 // Thread is in rdi already.
duke@435 2152 //
duke@435 2153 // UnrollBlock* uncommon_trap(JavaThread* thread, jint unloaded_class_index);
duke@435 2154
duke@435 2155 __ movq(c_rarg0, r15_thread);
duke@435 2156 __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, Deoptimization::uncommon_trap)));
duke@435 2157
duke@435 2158 // Set an oopmap for the call site
duke@435 2159 OopMapSet* oop_maps = new OopMapSet();
duke@435 2160 OopMap* map = new OopMap(SimpleRuntimeFrame::framesize, 0);
duke@435 2161
duke@435 2162 // location of rbp is known implicitly by the frame sender code
duke@435 2163
duke@435 2164 oop_maps->add_gc_map(__ pc() - start, map);
duke@435 2165
duke@435 2166 __ reset_last_Java_frame(false, false);
duke@435 2167
duke@435 2168 // Load UnrollBlock* into rdi
duke@435 2169 __ movq(rdi, rax);
duke@435 2170
duke@435 2171 // Pop all the frames we must move/replace.
duke@435 2172 //
duke@435 2173 // Frame picture (youngest to oldest)
duke@435 2174 // 1: self-frame (no frame link)
duke@435 2175 // 2: deopting frame (no frame link)
duke@435 2176 // 3: caller of deopting frame (could be compiled/interpreted).
duke@435 2177
duke@435 2178 // Pop self-frame. We have no frame, and must rely only on rax and rsp.
duke@435 2179 __ addq(rsp, (SimpleRuntimeFrame::framesize - 2) << LogBytesPerInt); // Epilog!
duke@435 2180
duke@435 2181 // Pop deoptimized frame (int)
duke@435 2182 __ movl(rcx, Address(rdi,
duke@435 2183 Deoptimization::UnrollBlock::
duke@435 2184 size_of_deoptimized_frame_offset_in_bytes()));
duke@435 2185 __ addq(rsp, rcx);
duke@435 2186
duke@435 2187 // rsp should be pointing at the return address to the caller (3)
duke@435 2188
duke@435 2189 // Stack bang to make sure there's enough room for these interpreter frames.
duke@435 2190 if (UseStackBanging) {
duke@435 2191 __ movl(rbx, Address(rdi ,Deoptimization::UnrollBlock::total_frame_sizes_offset_in_bytes()));
duke@435 2192 __ bang_stack_size(rbx, rcx);
duke@435 2193 }
duke@435 2194
duke@435 2195 // Load address of array of frame pcs into rcx (address*)
duke@435 2196 __ movq(rcx,
duke@435 2197 Address(rdi,
duke@435 2198 Deoptimization::UnrollBlock::frame_pcs_offset_in_bytes()));
duke@435 2199
duke@435 2200 // Trash the return pc
duke@435 2201 __ addq(rsp, wordSize);
duke@435 2202
duke@435 2203 // Load address of array of frame sizes into rsi (intptr_t*)
duke@435 2204 __ movq(rsi, Address(rdi,
duke@435 2205 Deoptimization::UnrollBlock::
duke@435 2206 frame_sizes_offset_in_bytes()));
duke@435 2207
duke@435 2208 // Counter
duke@435 2209 __ movl(rdx, Address(rdi,
duke@435 2210 Deoptimization::UnrollBlock::
duke@435 2211 number_of_frames_offset_in_bytes())); // (int)
duke@435 2212
duke@435 2213 // Pick up the initial fp we should save
duke@435 2214 __ movq(rbp,
duke@435 2215 Address(rdi,
duke@435 2216 Deoptimization::UnrollBlock::initial_fp_offset_in_bytes()));
duke@435 2217
duke@435 2218 // Now adjust the caller's stack to make up for the extra locals but
duke@435 2219 // record the original sp so that we can save it in the skeletal
duke@435 2220 // interpreter frame and the stack walking of interpreter_sender
duke@435 2221 // will get the unextended sp value and not the "real" sp value.
duke@435 2222
duke@435 2223 const Register sender_sp = r8;
duke@435 2224
duke@435 2225 __ movq(sender_sp, rsp);
duke@435 2226 __ movl(rbx, Address(rdi,
duke@435 2227 Deoptimization::UnrollBlock::
duke@435 2228 caller_adjustment_offset_in_bytes())); // (int)
duke@435 2229 __ subq(rsp, rbx);
duke@435 2230
duke@435 2231 // Push interpreter frames in a loop
duke@435 2232 Label loop;
duke@435 2233 __ bind(loop);
duke@435 2234 __ movq(rbx, Address(rsi, 0)); // Load frame size
duke@435 2235 __ subq(rbx, 2 * wordSize); // We'll push pc and rbp by hand
duke@435 2236 __ pushq(Address(rcx, 0)); // Save return address
duke@435 2237 __ enter(); // Save old & set new rbp
duke@435 2238 __ subq(rsp, rbx); // Prolog
duke@435 2239 __ movq(Address(rbp, frame::interpreter_frame_sender_sp_offset * wordSize),
duke@435 2240 sender_sp); // Make it walkable
duke@435 2241 // This value is corrected by layout_activation_impl
duke@435 2242 __ movptr(Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize), (int)NULL_WORD );
duke@435 2243 __ movq(sender_sp, rsp); // Pass sender_sp to next frame
duke@435 2244 __ addq(rsi, wordSize); // Bump array pointer (sizes)
duke@435 2245 __ addq(rcx, wordSize); // Bump array pointer (pcs)
duke@435 2246 __ decrementl(rdx); // Decrement counter
duke@435 2247 __ jcc(Assembler::notZero, loop);
duke@435 2248 __ pushq(Address(rcx, 0)); // Save final return address
duke@435 2249
duke@435 2250 // Re-push self-frame
duke@435 2251 __ enter(); // Save old & set new rbp
duke@435 2252 __ subq(rsp, (SimpleRuntimeFrame::framesize - 4) << LogBytesPerInt);
duke@435 2253 // Prolog
duke@435 2254
duke@435 2255 // Use rbp because the frames look interpreted now
duke@435 2256 __ set_last_Java_frame(noreg, rbp, NULL);
duke@435 2257
duke@435 2258 // Call C code. Need thread but NOT official VM entry
duke@435 2259 // crud. We cannot block on this call, no GC can happen. Call should
duke@435 2260 // restore return values to their stack-slots with the new SP.
duke@435 2261 // Thread is in rdi already.
duke@435 2262 //
duke@435 2263 // BasicType unpack_frames(JavaThread* thread, int exec_mode);
duke@435 2264
duke@435 2265 __ movq(c_rarg0, r15_thread);
duke@435 2266 __ movl(c_rarg1, Deoptimization::Unpack_uncommon_trap);
duke@435 2267 __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, Deoptimization::unpack_frames)));
duke@435 2268
duke@435 2269 // Set an oopmap for the call site
duke@435 2270 oop_maps->add_gc_map(__ pc() - start, new OopMap(SimpleRuntimeFrame::framesize, 0));
duke@435 2271
duke@435 2272 __ reset_last_Java_frame(true, false);
duke@435 2273
duke@435 2274 // Pop self-frame.
duke@435 2275 __ leave(); // Epilog
duke@435 2276
duke@435 2277 // Jump to interpreter
duke@435 2278 __ ret(0);
duke@435 2279
duke@435 2280 // Make sure all code is generated
duke@435 2281 masm->flush();
duke@435 2282
duke@435 2283 _uncommon_trap_blob = UncommonTrapBlob::create(&buffer, oop_maps,
duke@435 2284 SimpleRuntimeFrame::framesize >> 1);
duke@435 2285 }
duke@435 2286 #endif // COMPILER2
duke@435 2287
duke@435 2288
duke@435 2289 //------------------------------generate_handler_blob------
duke@435 2290 //
duke@435 2291 // Generate a special Compile2Runtime blob that saves all registers,
duke@435 2292 // and setup oopmap.
duke@435 2293 //
duke@435 2294 static SafepointBlob* generate_handler_blob(address call_ptr, bool cause_return) {
duke@435 2295 assert(StubRoutines::forward_exception_entry() != NULL,
duke@435 2296 "must be generated before");
duke@435 2297
duke@435 2298 ResourceMark rm;
duke@435 2299 OopMapSet *oop_maps = new OopMapSet();
duke@435 2300 OopMap* map;
duke@435 2301
duke@435 2302 // Allocate space for the code. Setup code generation tools.
duke@435 2303 CodeBuffer buffer("handler_blob", 2048, 1024);
duke@435 2304 MacroAssembler* masm = new MacroAssembler(&buffer);
duke@435 2305
duke@435 2306 address start = __ pc();
duke@435 2307 address call_pc = NULL;
duke@435 2308 int frame_size_in_words;
duke@435 2309
duke@435 2310 // Make room for return address (or push it again)
duke@435 2311 if (!cause_return) {
duke@435 2312 __ pushq(rbx);
duke@435 2313 }
duke@435 2314
duke@435 2315 // Save registers, fpu state, and flags
duke@435 2316 map = RegisterSaver::save_live_registers(masm, 0, &frame_size_in_words);
duke@435 2317
duke@435 2318 // The following is basically a call_VM. However, we need the precise
duke@435 2319 // address of the call in order to generate an oopmap. Hence, we do all the
duke@435 2320 // work outselves.
duke@435 2321
duke@435 2322 __ set_last_Java_frame(noreg, noreg, NULL);
duke@435 2323
duke@435 2324 // The return address must always be correct so that frame constructor never
duke@435 2325 // sees an invalid pc.
duke@435 2326
duke@435 2327 if (!cause_return) {
duke@435 2328 // overwrite the dummy value we pushed on entry
duke@435 2329 __ movq(c_rarg0, Address(r15_thread, JavaThread::saved_exception_pc_offset()));
duke@435 2330 __ movq(Address(rbp, wordSize), c_rarg0);
duke@435 2331 }
duke@435 2332
duke@435 2333 // Do the call
duke@435 2334 __ movq(c_rarg0, r15_thread);
duke@435 2335 __ call(RuntimeAddress(call_ptr));
duke@435 2336
duke@435 2337 // Set an oopmap for the call site. This oopmap will map all
duke@435 2338 // oop-registers and debug-info registers as callee-saved. This
duke@435 2339 // will allow deoptimization at this safepoint to find all possible
duke@435 2340 // debug-info recordings, as well as let GC find all oops.
duke@435 2341
duke@435 2342 oop_maps->add_gc_map( __ pc() - start, map);
duke@435 2343
duke@435 2344 Label noException;
duke@435 2345
duke@435 2346 __ reset_last_Java_frame(false, false);
duke@435 2347
duke@435 2348 __ cmpq(Address(r15_thread, Thread::pending_exception_offset()), (int)NULL_WORD);
duke@435 2349 __ jcc(Assembler::equal, noException);
duke@435 2350
duke@435 2351 // Exception pending
duke@435 2352
duke@435 2353 RegisterSaver::restore_live_registers(masm);
duke@435 2354
duke@435 2355 __ jump(RuntimeAddress(StubRoutines::forward_exception_entry()));
duke@435 2356
duke@435 2357 // No exception case
duke@435 2358 __ bind(noException);
duke@435 2359
duke@435 2360 // Normal exit, restore registers and exit.
duke@435 2361 RegisterSaver::restore_live_registers(masm);
duke@435 2362
duke@435 2363 __ ret(0);
duke@435 2364
duke@435 2365 // Make sure all code is generated
duke@435 2366 masm->flush();
duke@435 2367
duke@435 2368 // Fill-out other meta info
duke@435 2369 return SafepointBlob::create(&buffer, oop_maps, frame_size_in_words);
duke@435 2370 }
duke@435 2371
duke@435 2372 //
duke@435 2373 // generate_resolve_blob - call resolution (static/virtual/opt-virtual/ic-miss
duke@435 2374 //
duke@435 2375 // Generate a stub that calls into vm to find out the proper destination
duke@435 2376 // of a java call. All the argument registers are live at this point
duke@435 2377 // but since this is generic code we don't know what they are and the caller
duke@435 2378 // must do any gc of the args.
duke@435 2379 //
duke@435 2380 static RuntimeStub* generate_resolve_blob(address destination, const char* name) {
duke@435 2381 assert (StubRoutines::forward_exception_entry() != NULL, "must be generated before");
duke@435 2382
duke@435 2383 // allocate space for the code
duke@435 2384 ResourceMark rm;
duke@435 2385
duke@435 2386 CodeBuffer buffer(name, 1000, 512);
duke@435 2387 MacroAssembler* masm = new MacroAssembler(&buffer);
duke@435 2388
duke@435 2389 int frame_size_in_words;
duke@435 2390
duke@435 2391 OopMapSet *oop_maps = new OopMapSet();
duke@435 2392 OopMap* map = NULL;
duke@435 2393
duke@435 2394 int start = __ offset();
duke@435 2395
duke@435 2396 map = RegisterSaver::save_live_registers(masm, 0, &frame_size_in_words);
duke@435 2397
duke@435 2398 int frame_complete = __ offset();
duke@435 2399
duke@435 2400 __ set_last_Java_frame(noreg, noreg, NULL);
duke@435 2401
duke@435 2402 __ movq(c_rarg0, r15_thread);
duke@435 2403
duke@435 2404 __ call(RuntimeAddress(destination));
duke@435 2405
duke@435 2406
duke@435 2407 // Set an oopmap for the call site.
duke@435 2408 // We need this not only for callee-saved registers, but also for volatile
duke@435 2409 // registers that the compiler might be keeping live across a safepoint.
duke@435 2410
duke@435 2411 oop_maps->add_gc_map( __ offset() - start, map);
duke@435 2412
duke@435 2413 // rax contains the address we are going to jump to assuming no exception got installed
duke@435 2414
duke@435 2415 // clear last_Java_sp
duke@435 2416 __ reset_last_Java_frame(false, false);
duke@435 2417 // check for pending exceptions
duke@435 2418 Label pending;
duke@435 2419 __ cmpq(Address(r15_thread, Thread::pending_exception_offset()), (int)NULL_WORD);
duke@435 2420 __ jcc(Assembler::notEqual, pending);
duke@435 2421
duke@435 2422 // get the returned methodOop
duke@435 2423 __ movq(rbx, Address(r15_thread, JavaThread::vm_result_offset()));
duke@435 2424 __ movq(Address(rsp, RegisterSaver::rbx_offset_in_bytes()), rbx);
duke@435 2425
duke@435 2426 __ movq(Address(rsp, RegisterSaver::rax_offset_in_bytes()), rax);
duke@435 2427
duke@435 2428 RegisterSaver::restore_live_registers(masm);
duke@435 2429
duke@435 2430 // We are back the the original state on entry and ready to go.
duke@435 2431
duke@435 2432 __ jmp(rax);
duke@435 2433
duke@435 2434 // Pending exception after the safepoint
duke@435 2435
duke@435 2436 __ bind(pending);
duke@435 2437
duke@435 2438 RegisterSaver::restore_live_registers(masm);
duke@435 2439
duke@435 2440 // exception pending => remove activation and forward to exception handler
duke@435 2441
duke@435 2442 __ movptr(Address(r15_thread, JavaThread::vm_result_offset()), (int)NULL_WORD);
duke@435 2443
duke@435 2444 __ movq(rax, Address(r15_thread, Thread::pending_exception_offset()));
duke@435 2445 __ jump(RuntimeAddress(StubRoutines::forward_exception_entry()));
duke@435 2446
duke@435 2447 // -------------
duke@435 2448 // make sure all code is generated
duke@435 2449 masm->flush();
duke@435 2450
duke@435 2451 // return the blob
duke@435 2452 // frame_size_words or bytes??
duke@435 2453 return RuntimeStub::new_runtime_stub(name, &buffer, frame_complete, frame_size_in_words, oop_maps, true);
duke@435 2454 }
duke@435 2455
duke@435 2456
duke@435 2457 void SharedRuntime::generate_stubs() {
duke@435 2458
duke@435 2459 _wrong_method_blob = generate_resolve_blob(CAST_FROM_FN_PTR(address, SharedRuntime::handle_wrong_method),
duke@435 2460 "wrong_method_stub");
duke@435 2461 _ic_miss_blob = generate_resolve_blob(CAST_FROM_FN_PTR(address, SharedRuntime::handle_wrong_method_ic_miss),
duke@435 2462 "ic_miss_stub");
duke@435 2463 _resolve_opt_virtual_call_blob = generate_resolve_blob(CAST_FROM_FN_PTR(address, SharedRuntime::resolve_opt_virtual_call_C),
duke@435 2464 "resolve_opt_virtual_call");
duke@435 2465
duke@435 2466 _resolve_virtual_call_blob = generate_resolve_blob(CAST_FROM_FN_PTR(address, SharedRuntime::resolve_virtual_call_C),
duke@435 2467 "resolve_virtual_call");
duke@435 2468
duke@435 2469 _resolve_static_call_blob = generate_resolve_blob(CAST_FROM_FN_PTR(address, SharedRuntime::resolve_static_call_C),
duke@435 2470 "resolve_static_call");
duke@435 2471 _polling_page_safepoint_handler_blob =
duke@435 2472 generate_handler_blob(CAST_FROM_FN_PTR(address,
duke@435 2473 SafepointSynchronize::handle_polling_page_exception), false);
duke@435 2474
duke@435 2475 _polling_page_return_handler_blob =
duke@435 2476 generate_handler_blob(CAST_FROM_FN_PTR(address,
duke@435 2477 SafepointSynchronize::handle_polling_page_exception), true);
duke@435 2478
duke@435 2479 generate_deopt_blob();
duke@435 2480
duke@435 2481 #ifdef COMPILER2
duke@435 2482 generate_uncommon_trap_blob();
duke@435 2483 #endif // COMPILER2
duke@435 2484 }
duke@435 2485
duke@435 2486
duke@435 2487 #ifdef COMPILER2
duke@435 2488 // This is here instead of runtime_x86_64.cpp because it uses SimpleRuntimeFrame
duke@435 2489 //
duke@435 2490 //------------------------------generate_exception_blob---------------------------
duke@435 2491 // creates exception blob at the end
duke@435 2492 // Using exception blob, this code is jumped from a compiled method.
duke@435 2493 // (see emit_exception_handler in x86_64.ad file)
duke@435 2494 //
duke@435 2495 // Given an exception pc at a call we call into the runtime for the
duke@435 2496 // handler in this method. This handler might merely restore state
duke@435 2497 // (i.e. callee save registers) unwind the frame and jump to the
duke@435 2498 // exception handler for the nmethod if there is no Java level handler
duke@435 2499 // for the nmethod.
duke@435 2500 //
duke@435 2501 // This code is entered with a jmp.
duke@435 2502 //
duke@435 2503 // Arguments:
duke@435 2504 // rax: exception oop
duke@435 2505 // rdx: exception pc
duke@435 2506 //
duke@435 2507 // Results:
duke@435 2508 // rax: exception oop
duke@435 2509 // rdx: exception pc in caller or ???
duke@435 2510 // destination: exception handler of caller
duke@435 2511 //
duke@435 2512 // Note: the exception pc MUST be at a call (precise debug information)
duke@435 2513 // Registers rax, rdx, rcx, rsi, rdi, r8-r11 are not callee saved.
duke@435 2514 //
duke@435 2515
duke@435 2516 void OptoRuntime::generate_exception_blob() {
duke@435 2517 assert(!OptoRuntime::is_callee_saved_register(RDX_num), "");
duke@435 2518 assert(!OptoRuntime::is_callee_saved_register(RAX_num), "");
duke@435 2519 assert(!OptoRuntime::is_callee_saved_register(RCX_num), "");
duke@435 2520
duke@435 2521 assert(SimpleRuntimeFrame::framesize % 4 == 0, "sp not 16-byte aligned");
duke@435 2522
duke@435 2523 // Allocate space for the code
duke@435 2524 ResourceMark rm;
duke@435 2525 // Setup code generation tools
duke@435 2526 CodeBuffer buffer("exception_blob", 2048, 1024);
duke@435 2527 MacroAssembler* masm = new MacroAssembler(&buffer);
duke@435 2528
duke@435 2529
duke@435 2530 address start = __ pc();
duke@435 2531
duke@435 2532 // Exception pc is 'return address' for stack walker
duke@435 2533 __ pushq(rdx);
duke@435 2534 __ subq(rsp, SimpleRuntimeFrame::return_off << LogBytesPerInt); // Prolog
duke@435 2535
duke@435 2536 // Save callee-saved registers. See x86_64.ad.
duke@435 2537
duke@435 2538 // rbp is an implicitly saved callee saved register (i.e. the calling
duke@435 2539 // convention will save restore it in prolog/epilog) Other than that
duke@435 2540 // there are no callee save registers now that adapter frames are gone.
duke@435 2541
duke@435 2542 __ movq(Address(rsp, SimpleRuntimeFrame::rbp_off << LogBytesPerInt), rbp);
duke@435 2543
duke@435 2544 // Store exception in Thread object. We cannot pass any arguments to the
duke@435 2545 // handle_exception call, since we do not want to make any assumption
duke@435 2546 // about the size of the frame where the exception happened in.
duke@435 2547 // c_rarg0 is either rdi (Linux) or rcx (Windows).
duke@435 2548 __ movq(Address(r15_thread, JavaThread::exception_oop_offset()),rax);
duke@435 2549 __ movq(Address(r15_thread, JavaThread::exception_pc_offset()), rdx);
duke@435 2550
duke@435 2551 // This call does all the hard work. It checks if an exception handler
duke@435 2552 // exists in the method.
duke@435 2553 // If so, it returns the handler address.
duke@435 2554 // If not, it prepares for stack-unwinding, restoring the callee-save
duke@435 2555 // registers of the frame being removed.
duke@435 2556 //
duke@435 2557 // address OptoRuntime::handle_exception_C(JavaThread* thread)
duke@435 2558
duke@435 2559 __ set_last_Java_frame(noreg, noreg, NULL);
duke@435 2560 __ movq(c_rarg0, r15_thread);
duke@435 2561 __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, OptoRuntime::handle_exception_C)));
duke@435 2562
duke@435 2563 // Set an oopmap for the call site. This oopmap will only be used if we
duke@435 2564 // are unwinding the stack. Hence, all locations will be dead.
duke@435 2565 // Callee-saved registers will be the same as the frame above (i.e.,
duke@435 2566 // handle_exception_stub), since they were restored when we got the
duke@435 2567 // exception.
duke@435 2568
duke@435 2569 OopMapSet* oop_maps = new OopMapSet();
duke@435 2570
duke@435 2571 oop_maps->add_gc_map( __ pc()-start, new OopMap(SimpleRuntimeFrame::framesize, 0));
duke@435 2572
duke@435 2573 __ reset_last_Java_frame(false, false);
duke@435 2574
duke@435 2575 // Restore callee-saved registers
duke@435 2576
duke@435 2577 // rbp is an implicitly saved callee saved register (i.e. the calling
duke@435 2578 // convention will save restore it in prolog/epilog) Other than that
duke@435 2579 // there are no callee save registers no that adapter frames are gone.
duke@435 2580
duke@435 2581 __ movq(rbp, Address(rsp, SimpleRuntimeFrame::rbp_off << LogBytesPerInt));
duke@435 2582
duke@435 2583 __ addq(rsp, SimpleRuntimeFrame::return_off << LogBytesPerInt); // Epilog
duke@435 2584 __ popq(rdx); // No need for exception pc anymore
duke@435 2585
duke@435 2586 // rax: exception handler
duke@435 2587
duke@435 2588 // We have a handler in rax (could be deopt blob).
duke@435 2589 __ movq(r8, rax);
duke@435 2590
duke@435 2591 // Get the exception oop
duke@435 2592 __ movq(rax, Address(r15_thread, JavaThread::exception_oop_offset()));
duke@435 2593 // Get the exception pc in case we are deoptimized
duke@435 2594 __ movq(rdx, Address(r15_thread, JavaThread::exception_pc_offset()));
duke@435 2595 #ifdef ASSERT
duke@435 2596 __ movptr(Address(r15_thread, JavaThread::exception_handler_pc_offset()), (int)NULL_WORD);
duke@435 2597 __ movptr(Address(r15_thread, JavaThread::exception_pc_offset()), (int)NULL_WORD);
duke@435 2598 #endif
duke@435 2599 // Clear the exception oop so GC no longer processes it as a root.
duke@435 2600 __ movptr(Address(r15_thread, JavaThread::exception_oop_offset()), (int)NULL_WORD);
duke@435 2601
duke@435 2602 // rax: exception oop
duke@435 2603 // r8: exception handler
duke@435 2604 // rdx: exception pc
duke@435 2605 // Jump to handler
duke@435 2606
duke@435 2607 __ jmp(r8);
duke@435 2608
duke@435 2609 // Make sure all code is generated
duke@435 2610 masm->flush();
duke@435 2611
duke@435 2612 // Set exception blob
duke@435 2613 _exception_blob = ExceptionBlob::create(&buffer, oop_maps, SimpleRuntimeFrame::framesize >> 1);
duke@435 2614 }
duke@435 2615 #endif // COMPILER2

mercurial