1 /* 2 * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved. 3 * Copyright 2012, 2015 SAP AG. All rights reserved. 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * This code is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 only, as 8 * published by the Free Software Foundation. 9 * 10 * This code is distributed in the hope that it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 * version 2 for more details (a copy is included in the LICENSE file that 14 * accompanied this code). 15 * 16 * You should have received a copy of the GNU General Public License version 17 * 2 along with this work; if not, write to the Free Software Foundation, 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19 * 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21 * or visit www.oracle.com if you need additional information or have any 22 * questions. 23 * 24 */ 25 26 #include "precompiled.hpp" 27 #include "asm/macroAssembler.inline.hpp" 28 #include "interpreter/bytecodeHistogram.hpp" 29 #include "interpreter/interpreter.hpp" 30 #include "interpreter/interpreterRuntime.hpp" 31 #include "interpreter/interp_masm.hpp" 32 #include "interpreter/templateInterpreterGenerator.hpp" 33 #include "interpreter/templateTable.hpp" 34 #include "oops/arrayOop.hpp" 35 #include "oops/methodData.hpp" 36 #include "oops/method.hpp" 37 #include "oops/oop.inline.hpp" 38 #include "prims/jvmtiExport.hpp" 39 #include "prims/jvmtiThreadState.hpp" 40 #include "prims/methodHandles.hpp" 41 #include "runtime/arguments.hpp" 42 #include "runtime/frame.inline.hpp" 43 #include "runtime/sharedRuntime.hpp" 44 #include "runtime/stubRoutines.hpp" 45 #include "runtime/synchronizer.hpp" 46 #include "runtime/timer.hpp" 47 #include "runtime/vframeArray.hpp" 48 #include "utilities/debug.hpp" 49 #ifdef COMPILER1 50 #include "c1/c1_Runtime1.hpp" 51 #endif 52 53 #define __ _masm-> 54 55 #ifdef PRODUCT 56 #define BLOCK_COMMENT(str) // nothing 57 #else 58 #define BLOCK_COMMENT(str) __ block_comment(str) 59 #endif 60 61 #define BIND(label) bind(label); BLOCK_COMMENT(#label ":") 62 63 address AbstractInterpreterGenerator::generate_slow_signature_handler() { 64 // Slow_signature handler that respects the PPC C calling conventions. 65 // 66 // We get called by the native entry code with our output register 67 // area == 8. First we call InterpreterRuntime::get_result_handler 68 // to copy the pointer to the signature string temporarily to the 69 // first C-argument and to return the result_handler in 70 // R3_RET. Since native_entry will copy the jni-pointer to the 71 // first C-argument slot later on, it is OK to occupy this slot 72 // temporarilly. Then we copy the argument list on the java 73 // expression stack into native varargs format on the native stack 74 // and load arguments into argument registers. Integer arguments in 75 // the varargs vector will be sign-extended to 8 bytes. 76 // 77 // On entry: 78 // R3_ARG1 - intptr_t* Address of java argument list in memory. 79 // R15_prev_state - BytecodeInterpreter* Address of interpreter state for 80 // this method 81 // R19_method 82 // 83 // On exit (just before return instruction): 84 // R3_RET - contains the address of the result_handler. 85 // R4_ARG2 - is not updated for static methods and contains "this" otherwise. 86 // R5_ARG3-R10_ARG8: - When the (i-2)th Java argument is not of type float or double, 87 // ARGi contains this argument. Otherwise, ARGi is not updated. 88 // F1_ARG1-F13_ARG13 - contain the first 13 arguments of type float or double. 89 90 const int LogSizeOfTwoInstructions = 3; 91 92 // FIXME: use Argument:: GL: Argument names different numbers! 93 const int max_fp_register_arguments = 13; 94 const int max_int_register_arguments = 6; // first 2 are reserved 95 96 const Register arg_java = R21_tmp1; 97 const Register arg_c = R22_tmp2; 98 const Register signature = R23_tmp3; // is string 99 const Register sig_byte = R24_tmp4; 100 const Register fpcnt = R25_tmp5; 101 const Register argcnt = R26_tmp6; 102 const Register intSlot = R27_tmp7; 103 const Register target_sp = R28_tmp8; 104 const FloatRegister floatSlot = F0; 105 106 address entry = __ function_entry(); 107 108 __ save_LR_CR(R0); 109 __ save_nonvolatile_gprs(R1_SP, _spill_nonvolatiles_neg(r14)); 110 // We use target_sp for storing arguments in the C frame. 111 __ mr(target_sp, R1_SP); 112 __ push_frame_reg_args_nonvolatiles(0, R11_scratch1); 113 114 __ mr(arg_java, R3_ARG1); 115 116 __ call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::get_signature), R16_thread, R19_method); 117 118 // Signature is in R3_RET. Signature is callee saved. 119 __ mr(signature, R3_RET); 120 121 // Get the result handler. 122 __ call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::get_result_handler), R16_thread, R19_method); 123 124 { 125 Label L; 126 // test if static 127 // _access_flags._flags must be at offset 0. 128 // TODO PPC port: requires change in shared code. 129 //assert(in_bytes(AccessFlags::flags_offset()) == 0, 130 // "MethodDesc._access_flags == MethodDesc._access_flags._flags"); 131 // _access_flags must be a 32 bit value. 132 assert(sizeof(AccessFlags) == 4, "wrong size"); 133 __ lwa(R11_scratch1/*access_flags*/, method_(access_flags)); 134 // testbit with condition register. 135 __ testbitdi(CCR0, R0, R11_scratch1/*access_flags*/, JVM_ACC_STATIC_BIT); 136 __ btrue(CCR0, L); 137 // For non-static functions, pass "this" in R4_ARG2 and copy it 138 // to 2nd C-arg slot. 139 // We need to box the Java object here, so we use arg_java 140 // (address of current Java stack slot) as argument and don't 141 // dereference it as in case of ints, floats, etc. 142 __ mr(R4_ARG2, arg_java); 143 __ addi(arg_java, arg_java, -BytesPerWord); 144 __ std(R4_ARG2, _abi(carg_2), target_sp); 145 __ bind(L); 146 } 147 148 // Will be incremented directly after loop_start. argcnt=0 149 // corresponds to 3rd C argument. 150 __ li(argcnt, -1); 151 // arg_c points to 3rd C argument 152 __ addi(arg_c, target_sp, _abi(carg_3)); 153 // no floating-point args parsed so far 154 __ li(fpcnt, 0); 155 156 Label move_intSlot_to_ARG, move_floatSlot_to_FARG; 157 Label loop_start, loop_end; 158 Label do_int, do_long, do_float, do_double, do_dontreachhere, do_object, do_array, do_boxed; 159 160 // signature points to '(' at entry 161 #ifdef ASSERT 162 __ lbz(sig_byte, 0, signature); 163 __ cmplwi(CCR0, sig_byte, '('); 164 __ bne(CCR0, do_dontreachhere); 165 #endif 166 167 __ bind(loop_start); 168 169 __ addi(argcnt, argcnt, 1); 170 __ lbzu(sig_byte, 1, signature); 171 172 __ cmplwi(CCR0, sig_byte, ')'); // end of signature 173 __ beq(CCR0, loop_end); 174 175 __ cmplwi(CCR0, sig_byte, 'B'); // byte 176 __ beq(CCR0, do_int); 177 178 __ cmplwi(CCR0, sig_byte, 'C'); // char 179 __ beq(CCR0, do_int); 180 181 __ cmplwi(CCR0, sig_byte, 'D'); // double 182 __ beq(CCR0, do_double); 183 184 __ cmplwi(CCR0, sig_byte, 'F'); // float 185 __ beq(CCR0, do_float); 186 187 __ cmplwi(CCR0, sig_byte, 'I'); // int 188 __ beq(CCR0, do_int); 189 190 __ cmplwi(CCR0, sig_byte, 'J'); // long 191 __ beq(CCR0, do_long); 192 193 __ cmplwi(CCR0, sig_byte, 'S'); // short 194 __ beq(CCR0, do_int); 195 196 __ cmplwi(CCR0, sig_byte, 'Z'); // boolean 197 __ beq(CCR0, do_int); 198 199 __ cmplwi(CCR0, sig_byte, 'L'); // object 200 __ beq(CCR0, do_object); 201 202 __ cmplwi(CCR0, sig_byte, '['); // array 203 __ beq(CCR0, do_array); 204 205 // __ cmplwi(CCR0, sig_byte, 'V'); // void cannot appear since we do not parse the return type 206 // __ beq(CCR0, do_void); 207 208 __ bind(do_dontreachhere); 209 210 __ unimplemented("ShouldNotReachHere in slow_signature_handler", 120); 211 212 __ bind(do_array); 213 214 { 215 Label start_skip, end_skip; 216 217 __ bind(start_skip); 218 __ lbzu(sig_byte, 1, signature); 219 __ cmplwi(CCR0, sig_byte, '['); 220 __ beq(CCR0, start_skip); // skip further brackets 221 __ cmplwi(CCR0, sig_byte, '9'); 222 __ bgt(CCR0, end_skip); // no optional size 223 __ cmplwi(CCR0, sig_byte, '0'); 224 __ bge(CCR0, start_skip); // skip optional size 225 __ bind(end_skip); 226 227 __ cmplwi(CCR0, sig_byte, 'L'); 228 __ beq(CCR0, do_object); // for arrays of objects, the name of the object must be skipped 229 __ b(do_boxed); // otherwise, go directly to do_boxed 230 } 231 232 __ bind(do_object); 233 { 234 Label L; 235 __ bind(L); 236 __ lbzu(sig_byte, 1, signature); 237 __ cmplwi(CCR0, sig_byte, ';'); 238 __ bne(CCR0, L); 239 } 240 // Need to box the Java object here, so we use arg_java (address of 241 // current Java stack slot) as argument and don't dereference it as 242 // in case of ints, floats, etc. 243 Label do_null; 244 __ bind(do_boxed); 245 __ ld(R0,0, arg_java); 246 __ cmpdi(CCR0, R0, 0); 247 __ li(intSlot,0); 248 __ beq(CCR0, do_null); 249 __ mr(intSlot, arg_java); 250 __ bind(do_null); 251 __ std(intSlot, 0, arg_c); 252 __ addi(arg_java, arg_java, -BytesPerWord); 253 __ addi(arg_c, arg_c, BytesPerWord); 254 __ cmplwi(CCR0, argcnt, max_int_register_arguments); 255 __ blt(CCR0, move_intSlot_to_ARG); 256 __ b(loop_start); 257 258 __ bind(do_int); 259 __ lwa(intSlot, 0, arg_java); 260 __ std(intSlot, 0, arg_c); 261 __ addi(arg_java, arg_java, -BytesPerWord); 262 __ addi(arg_c, arg_c, BytesPerWord); 263 __ cmplwi(CCR0, argcnt, max_int_register_arguments); 264 __ blt(CCR0, move_intSlot_to_ARG); 265 __ b(loop_start); 266 267 __ bind(do_long); 268 __ ld(intSlot, -BytesPerWord, arg_java); 269 __ std(intSlot, 0, arg_c); 270 __ addi(arg_java, arg_java, - 2 * BytesPerWord); 271 __ addi(arg_c, arg_c, BytesPerWord); 272 __ cmplwi(CCR0, argcnt, max_int_register_arguments); 273 __ blt(CCR0, move_intSlot_to_ARG); 274 __ b(loop_start); 275 276 __ bind(do_float); 277 __ lfs(floatSlot, 0, arg_java); 278 #if defined(LINUX) 279 // Linux uses ELF ABI. Both original ELF and ELFv2 ABIs have float 280 // in the least significant word of an argument slot. 281 #if defined(VM_LITTLE_ENDIAN) 282 __ stfs(floatSlot, 0, arg_c); 283 #else 284 __ stfs(floatSlot, 4, arg_c); 285 #endif 286 #elif defined(AIX) 287 // Although AIX runs on big endian CPU, float is in most significant 288 // word of an argument slot. 289 __ stfs(floatSlot, 0, arg_c); 290 #else 291 #error "unknown OS" 292 #endif 293 __ addi(arg_java, arg_java, -BytesPerWord); 294 __ addi(arg_c, arg_c, BytesPerWord); 295 __ cmplwi(CCR0, fpcnt, max_fp_register_arguments); 296 __ blt(CCR0, move_floatSlot_to_FARG); 297 __ b(loop_start); 298 299 __ bind(do_double); 300 __ lfd(floatSlot, - BytesPerWord, arg_java); 301 __ stfd(floatSlot, 0, arg_c); 302 __ addi(arg_java, arg_java, - 2 * BytesPerWord); 303 __ addi(arg_c, arg_c, BytesPerWord); 304 __ cmplwi(CCR0, fpcnt, max_fp_register_arguments); 305 __ blt(CCR0, move_floatSlot_to_FARG); 306 __ b(loop_start); 307 308 __ bind(loop_end); 309 310 __ pop_frame(); 311 __ restore_nonvolatile_gprs(R1_SP, _spill_nonvolatiles_neg(r14)); 312 __ restore_LR_CR(R0); 313 314 __ blr(); 315 316 Label move_int_arg, move_float_arg; 317 __ bind(move_int_arg); // each case must consist of 2 instructions (otherwise adapt LogSizeOfTwoInstructions) 318 __ mr(R5_ARG3, intSlot); __ b(loop_start); 319 __ mr(R6_ARG4, intSlot); __ b(loop_start); 320 __ mr(R7_ARG5, intSlot); __ b(loop_start); 321 __ mr(R8_ARG6, intSlot); __ b(loop_start); 322 __ mr(R9_ARG7, intSlot); __ b(loop_start); 323 __ mr(R10_ARG8, intSlot); __ b(loop_start); 324 325 __ bind(move_float_arg); // each case must consist of 2 instructions (otherwise adapt LogSizeOfTwoInstructions) 326 __ fmr(F1_ARG1, floatSlot); __ b(loop_start); 327 __ fmr(F2_ARG2, floatSlot); __ b(loop_start); 328 __ fmr(F3_ARG3, floatSlot); __ b(loop_start); 329 __ fmr(F4_ARG4, floatSlot); __ b(loop_start); 330 __ fmr(F5_ARG5, floatSlot); __ b(loop_start); 331 __ fmr(F6_ARG6, floatSlot); __ b(loop_start); 332 __ fmr(F7_ARG7, floatSlot); __ b(loop_start); 333 __ fmr(F8_ARG8, floatSlot); __ b(loop_start); 334 __ fmr(F9_ARG9, floatSlot); __ b(loop_start); 335 __ fmr(F10_ARG10, floatSlot); __ b(loop_start); 336 __ fmr(F11_ARG11, floatSlot); __ b(loop_start); 337 __ fmr(F12_ARG12, floatSlot); __ b(loop_start); 338 __ fmr(F13_ARG13, floatSlot); __ b(loop_start); 339 340 __ bind(move_intSlot_to_ARG); 341 __ sldi(R0, argcnt, LogSizeOfTwoInstructions); 342 __ load_const(R11_scratch1, move_int_arg); // Label must be bound here. 343 __ add(R11_scratch1, R0, R11_scratch1); 344 __ mtctr(R11_scratch1/*branch_target*/); 345 __ bctr(); 346 __ bind(move_floatSlot_to_FARG); 347 __ sldi(R0, fpcnt, LogSizeOfTwoInstructions); 348 __ addi(fpcnt, fpcnt, 1); 349 __ load_const(R11_scratch1, move_float_arg); // Label must be bound here. 350 __ add(R11_scratch1, R0, R11_scratch1); 351 __ mtctr(R11_scratch1/*branch_target*/); 352 __ bctr(); 353 354 return entry; 355 } 356 357 address AbstractInterpreterGenerator::generate_result_handler_for(BasicType type) { 358 // 359 // Registers alive 360 // R3_RET 361 // LR 362 // 363 // Registers updated 364 // R3_RET 365 // 366 367 Label done; 368 address entry = __ pc(); 369 370 switch (type) { 371 case T_BOOLEAN: 372 // convert !=0 to 1 373 __ neg(R0, R3_RET); 374 __ orr(R0, R3_RET, R0); 375 __ srwi(R3_RET, R0, 31); 376 break; 377 case T_BYTE: 378 // sign extend 8 bits 379 __ extsb(R3_RET, R3_RET); 380 break; 381 case T_CHAR: 382 // zero extend 16 bits 383 __ clrldi(R3_RET, R3_RET, 48); 384 break; 385 case T_SHORT: 386 // sign extend 16 bits 387 __ extsh(R3_RET, R3_RET); 388 break; 389 case T_INT: 390 // sign extend 32 bits 391 __ extsw(R3_RET, R3_RET); 392 break; 393 case T_LONG: 394 break; 395 case T_OBJECT: 396 // unbox result if not null 397 __ cmpdi(CCR0, R3_RET, 0); 398 __ beq(CCR0, done); 399 __ ld(R3_RET, 0, R3_RET); 400 __ verify_oop(R3_RET); 401 break; 402 case T_FLOAT: 403 break; 404 case T_DOUBLE: 405 break; 406 case T_VOID: 407 break; 408 default: ShouldNotReachHere(); 409 } 410 411 __ BIND(done); 412 __ blr(); 413 414 return entry; 415 } 416 417 // Abstract method entry. 418 // 419 address TemplateInterpreterGenerator::generate_abstract_entry(void) { 420 address entry = __ pc(); 421 422 // 423 // Registers alive 424 // R16_thread - JavaThread* 425 // R19_method - callee's method (method to be invoked) 426 // R1_SP - SP prepared such that caller's outgoing args are near top 427 // LR - return address to caller 428 // 429 // Stack layout at this point: 430 // 431 // 0 [TOP_IJAVA_FRAME_ABI] <-- R1_SP 432 // alignment (optional) 433 // [outgoing Java arguments] 434 // ... 435 // PARENT [PARENT_IJAVA_FRAME_ABI] 436 // ... 437 // 438 439 // Can't use call_VM here because we have not set up a new 440 // interpreter state. Make the call to the vm and make it look like 441 // our caller set up the JavaFrameAnchor. 442 __ set_top_ijava_frame_at_SP_as_last_Java_frame(R1_SP, R12_scratch2/*tmp*/); 443 444 // Push a new C frame and save LR. 445 __ save_LR_CR(R0); 446 __ push_frame_reg_args(0, R11_scratch1); 447 448 // This is not a leaf but we have a JavaFrameAnchor now and we will 449 // check (create) exceptions afterward so this is ok. 450 __ call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_AbstractMethodError), 451 R16_thread); 452 453 // Pop the C frame and restore LR. 454 __ pop_frame(); 455 __ restore_LR_CR(R0); 456 457 // Reset JavaFrameAnchor from call_VM_leaf above. 458 __ reset_last_Java_frame(); 459 460 // We don't know our caller, so jump to the general forward exception stub, 461 // which will also pop our full frame off. Satisfy the interface of 462 // SharedRuntime::generate_forward_exception() 463 __ load_const_optimized(R11_scratch1, StubRoutines::forward_exception_entry(), R0); 464 __ mtctr(R11_scratch1); 465 __ bctr(); 466 467 return entry; 468 } 469 470 // Interpreter intrinsic for WeakReference.get(). 471 // 1. Don't push a full blown frame and go on dispatching, but fetch the value 472 // into R8 and return quickly 473 // 2. If G1 is active we *must* execute this intrinsic for corrrectness: 474 // It contains a GC barrier which puts the reference into the satb buffer 475 // to indicate that someone holds a strong reference to the object the 476 // weak ref points to! 477 address TemplateInterpreterGenerator::generate_Reference_get_entry(void) { 478 // Code: _aload_0, _getfield, _areturn 479 // parameter size = 1 480 // 481 // The code that gets generated by this routine is split into 2 parts: 482 // 1. the "intrinsified" code for G1 (or any SATB based GC), 483 // 2. the slow path - which is an expansion of the regular method entry. 484 // 485 // Notes: 486 // * In the G1 code we do not check whether we need to block for 487 // a safepoint. If G1 is enabled then we must execute the specialized 488 // code for Reference.get (except when the Reference object is null) 489 // so that we can log the value in the referent field with an SATB 490 // update buffer. 491 // If the code for the getfield template is modified so that the 492 // G1 pre-barrier code is executed when the current method is 493 // Reference.get() then going through the normal method entry 494 // will be fine. 495 // * The G1 code can, however, check the receiver object (the instance 496 // of java.lang.Reference) and jump to the slow path if null. If the 497 // Reference object is null then we obviously cannot fetch the referent 498 // and so we don't need to call the G1 pre-barrier. Thus we can use the 499 // regular method entry code to generate the NPE. 500 // 501 502 if (UseG1GC) { 503 address entry = __ pc(); 504 505 const int referent_offset = java_lang_ref_Reference::referent_offset; 506 guarantee(referent_offset > 0, "referent offset not initialized"); 507 508 Label slow_path; 509 510 // Debugging not possible, so can't use __ skip_if_jvmti_mode(slow_path, GR31_SCRATCH); 511 512 // In the G1 code we don't check if we need to reach a safepoint. We 513 // continue and the thread will safepoint at the next bytecode dispatch. 514 515 // If the receiver is null then it is OK to jump to the slow path. 516 __ ld(R3_RET, Interpreter::stackElementSize, R15_esp); // get receiver 517 518 // Check if receiver == NULL and go the slow path. 519 __ cmpdi(CCR0, R3_RET, 0); 520 __ beq(CCR0, slow_path); 521 522 // Load the value of the referent field. 523 __ load_heap_oop(R3_RET, referent_offset, R3_RET); 524 525 // Generate the G1 pre-barrier code to log the value of 526 // the referent field in an SATB buffer. Note with 527 // these parameters the pre-barrier does not generate 528 // the load of the previous value. 529 530 // Restore caller sp for c2i case. 531 #ifdef ASSERT 532 __ ld(R9_ARG7, 0, R1_SP); 533 __ ld(R10_ARG8, 0, R21_sender_SP); 534 __ cmpd(CCR0, R9_ARG7, R10_ARG8); 535 __ asm_assert_eq("backlink", 0x544); 536 #endif // ASSERT 537 __ mr(R1_SP, R21_sender_SP); // Cut the stack back to where the caller started. 538 539 __ g1_write_barrier_pre(noreg, // obj 540 noreg, // offset 541 R3_RET, // pre_val 542 R11_scratch1, // tmp 543 R12_scratch2, // tmp 544 true); // needs_frame 545 546 __ blr(); 547 548 // Generate regular method entry. 549 __ bind(slow_path); 550 __ jump_to_entry(Interpreter::entry_for_kind(Interpreter::zerolocals), R11_scratch1); 551 return entry; 552 } 553 554 return NULL; 555 }