1 /* 2 * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "interpreter/interp_masm.hpp" 27 #include "interpreter/interpreter.hpp" 28 #include "interpreter/interpreterRuntime.hpp" 29 #include "memory/allocation.inline.hpp" 30 #include "memory/universe.hpp" 31 #include "oops/method.hpp" 32 #include "oops/oop.inline.hpp" 33 #include "runtime/handles.inline.hpp" 34 #include "runtime/icache.hpp" 35 #include "runtime/interfaceSupport.inline.hpp" 36 #include "runtime/signature.hpp" 37 38 #define __ _masm-> 39 40 // Implementation of SignatureHandlerGenerator 41 42 InterpreterRuntime::SignatureHandlerGenerator::SignatureHandlerGenerator(const methodHandle& method, CodeBuffer* buffer) : 43 NativeSignatureIterator(method) { 44 _masm = new MacroAssembler(buffer); 45 #ifdef AMD64 46 #ifdef _WIN64 47 _num_args = (method->is_static() ? 1 : 0); 48 _stack_offset = (Argument::n_int_register_parameters_c+1)* wordSize; // don't overwrite return address 49 #else 50 _num_int_args = (method->is_static() ? 1 : 0); 51 _num_fp_args = 0; 52 _stack_offset = wordSize; // don't overwrite return address 53 #endif // _WIN64 54 #endif // AMD64 55 } 56 57 Register InterpreterRuntime::SignatureHandlerGenerator::from() { return r14; } 58 Register InterpreterRuntime::SignatureHandlerGenerator::to() { return rsp; } 59 Register InterpreterRuntime::SignatureHandlerGenerator::temp() { return rscratch1; } 60 61 void InterpreterRuntime::SignatureHandlerGenerator::pass_int() { 62 const Address src(from(), Interpreter::local_offset_in_bytes(offset())); 63 64 #ifdef _WIN64 65 switch (_num_args) { 66 case 0: 67 __ movl(c_rarg1, src); 68 _num_args++; 69 break; 70 case 1: 71 __ movl(c_rarg2, src); 72 _num_args++; 73 break; 74 case 2: 75 __ movl(c_rarg3, src); 76 _num_args++; 77 break; 78 default: 79 __ movl(rax, src); 80 __ movl(Address(to(), _stack_offset), rax); 81 _stack_offset += wordSize; 82 break; 83 } 84 #else 85 switch (_num_int_args) { 86 case 0: 87 __ movl(c_rarg1, src); 88 _num_int_args++; 89 break; 90 case 1: 91 __ movl(c_rarg2, src); 92 _num_int_args++; 93 break; 94 case 2: 95 __ movl(c_rarg3, src); 96 _num_int_args++; 97 break; 98 case 3: 99 __ movl(c_rarg4, src); 100 _num_int_args++; 101 break; 102 case 4: 103 __ movl(c_rarg5, src); 104 _num_int_args++; 105 break; 106 default: 107 __ movl(rax, src); 108 __ movl(Address(to(), _stack_offset), rax); 109 _stack_offset += wordSize; 110 break; 111 } 112 #endif 113 } 114 115 void InterpreterRuntime::SignatureHandlerGenerator::pass_long() { 116 const Address src(from(), Interpreter::local_offset_in_bytes(offset() + 1)); 117 118 #ifdef _WIN64 119 switch (_num_args) { 120 case 0: 121 __ movptr(c_rarg1, src); 122 _num_args++; 123 break; 124 case 1: 125 __ movptr(c_rarg2, src); 126 _num_args++; 127 break; 128 case 2: 129 __ movptr(c_rarg3, src); 130 _num_args++; 131 break; 132 case 3: 133 default: 134 __ movptr(rax, src); 135 __ movptr(Address(to(), _stack_offset), rax); 136 _stack_offset += wordSize; 137 break; 138 } 139 #else 140 switch (_num_int_args) { 141 case 0: 142 __ movptr(c_rarg1, src); 143 _num_int_args++; 144 break; 145 case 1: 146 __ movptr(c_rarg2, src); 147 _num_int_args++; 148 break; 149 case 2: 150 __ movptr(c_rarg3, src); 151 _num_int_args++; 152 break; 153 case 3: 154 __ movptr(c_rarg4, src); 155 _num_int_args++; 156 break; 157 case 4: 158 __ movptr(c_rarg5, src); 159 _num_int_args++; 160 break; 161 default: 162 __ movptr(rax, src); 163 __ movptr(Address(to(), _stack_offset), rax); 164 _stack_offset += wordSize; 165 break; 166 } 167 #endif 168 } 169 170 void InterpreterRuntime::SignatureHandlerGenerator::pass_float() { 171 const Address src(from(), Interpreter::local_offset_in_bytes(offset())); 172 173 #ifdef _WIN64 174 if (_num_args < Argument::n_float_register_parameters_c-1) { 175 __ movflt(as_XMMRegister(++_num_args), src); 176 } else { 177 __ movl(rax, src); 178 __ movl(Address(to(), _stack_offset), rax); 179 _stack_offset += wordSize; 180 } 181 #else 182 if (_num_fp_args < Argument::n_float_register_parameters_c) { 183 __ movflt(as_XMMRegister(_num_fp_args++), src); 184 } else { 185 __ movl(rax, src); 186 __ movl(Address(to(), _stack_offset), rax); 187 _stack_offset += wordSize; 188 } 189 #endif 190 } 191 192 void InterpreterRuntime::SignatureHandlerGenerator::pass_double() { 193 const Address src(from(), Interpreter::local_offset_in_bytes(offset() + 1)); 194 195 #ifdef _WIN64 196 if (_num_args < Argument::n_float_register_parameters_c-1) { 197 __ movdbl(as_XMMRegister(++_num_args), src); 198 } else { 199 __ movptr(rax, src); 200 __ movptr(Address(to(), _stack_offset), rax); 201 _stack_offset += wordSize; 202 } 203 #else 204 if (_num_fp_args < Argument::n_float_register_parameters_c) { 205 __ movdbl(as_XMMRegister(_num_fp_args++), src); 206 } else { 207 __ movptr(rax, src); 208 __ movptr(Address(to(), _stack_offset), rax); 209 _stack_offset += wordSize; 210 } 211 #endif 212 } 213 214 void InterpreterRuntime::SignatureHandlerGenerator::pass_object() { 215 const Address src(from(), Interpreter::local_offset_in_bytes(offset())); 216 217 #ifdef _WIN64 218 switch (_num_args) { 219 case 0: 220 assert(offset() == 0, "argument register 1 can only be (non-null) receiver"); 221 __ lea(c_rarg1, src); 222 _num_args++; 223 break; 224 case 1: 225 __ lea(rax, src); 226 __ xorl(c_rarg2, c_rarg2); 227 __ cmpptr(src, 0); 228 __ cmov(Assembler::notEqual, c_rarg2, rax); 229 _num_args++; 230 break; 231 case 2: 232 __ lea(rax, src); 233 __ xorl(c_rarg3, c_rarg3); 234 __ cmpptr(src, 0); 235 __ cmov(Assembler::notEqual, c_rarg3, rax); 236 _num_args++; 237 break; 238 default: 239 __ lea(rax, src); 240 __ xorl(temp(), temp()); 241 __ cmpptr(src, 0); 242 __ cmov(Assembler::notEqual, temp(), rax); 243 __ movptr(Address(to(), _stack_offset), temp()); 244 _stack_offset += wordSize; 245 break; 246 } 247 #else 248 switch (_num_int_args) { 249 case 0: 250 assert(offset() == 0, "argument register 1 can only be (non-null) receiver"); 251 __ lea(c_rarg1, src); 252 _num_int_args++; 253 break; 254 case 1: 255 __ lea(rax, src); 256 __ xorl(c_rarg2, c_rarg2); 257 __ cmpptr(src, 0); 258 __ cmov(Assembler::notEqual, c_rarg2, rax); 259 _num_int_args++; 260 break; 261 case 2: 262 __ lea(rax, src); 263 __ xorl(c_rarg3, c_rarg3); 264 __ cmpptr(src, 0); 265 __ cmov(Assembler::notEqual, c_rarg3, rax); 266 _num_int_args++; 267 break; 268 case 3: 269 __ lea(rax, src); 270 __ xorl(c_rarg4, c_rarg4); 271 __ cmpptr(src, 0); 272 __ cmov(Assembler::notEqual, c_rarg4, rax); 273 _num_int_args++; 274 break; 275 case 4: 276 __ lea(rax, src); 277 __ xorl(c_rarg5, c_rarg5); 278 __ cmpptr(src, 0); 279 __ cmov(Assembler::notEqual, c_rarg5, rax); 280 _num_int_args++; 281 break; 282 default: 283 __ lea(rax, src); 284 __ xorl(temp(), temp()); 285 __ cmpptr(src, 0); 286 __ cmov(Assembler::notEqual, temp(), rax); 287 __ movptr(Address(to(), _stack_offset), temp()); 288 _stack_offset += wordSize; 289 break; 290 } 291 #endif 292 } 293 294 void InterpreterRuntime::SignatureHandlerGenerator::pass_valuetype() { 295 pass_object(); 296 } 297 298 void InterpreterRuntime::SignatureHandlerGenerator::generate(uint64_t fingerprint) { 299 // generate code to handle arguments 300 iterate(fingerprint); 301 302 // return result handler 303 __ lea(rax, ExternalAddress(Interpreter::result_handler(method()->result_type()))); 304 __ ret(0); 305 306 __ flush(); 307 } 308 309 310 // Implementation of SignatureHandlerLibrary 311 312 void SignatureHandlerLibrary::pd_set_handler(address handler) {} 313 314 315 #ifdef _WIN64 316 class SlowSignatureHandler 317 : public NativeSignatureIterator { 318 private: 319 address _from; 320 intptr_t* _to; 321 intptr_t* _reg_args; 322 intptr_t* _fp_identifiers; 323 unsigned int _num_args; 324 325 virtual void pass_int() 326 { 327 jint from_obj = *(jint *)(_from+Interpreter::local_offset_in_bytes(0)); 328 _from -= Interpreter::stackElementSize; 329 330 if (_num_args < Argument::n_int_register_parameters_c-1) { 331 *_reg_args++ = from_obj; 332 _num_args++; 333 } else { 334 *_to++ = from_obj; 335 } 336 } 337 338 virtual void pass_long() 339 { 340 intptr_t from_obj = *(intptr_t*)(_from+Interpreter::local_offset_in_bytes(1)); 341 _from -= 2*Interpreter::stackElementSize; 342 343 if (_num_args < Argument::n_int_register_parameters_c-1) { 344 *_reg_args++ = from_obj; 345 _num_args++; 346 } else { 347 *_to++ = from_obj; 348 } 349 } 350 351 virtual void pass_object() 352 { 353 intptr_t *from_addr = (intptr_t*)(_from + Interpreter::local_offset_in_bytes(0)); 354 _from -= Interpreter::stackElementSize; 355 if (_num_args < Argument::n_int_register_parameters_c-1) { 356 *_reg_args++ = (*from_addr == 0) ? NULL : (intptr_t) from_addr; 357 _num_args++; 358 } else { 359 *_to++ = (*from_addr == 0) ? NULL : (intptr_t) from_addr; 360 } 361 } 362 363 virtual void pass_valuetype() { 364 // values are handled with oops, like objects 365 pass_object(); 366 } 367 368 virtual void pass_float() 369 { 370 jint from_obj = *(jint *)(_from+Interpreter::local_offset_in_bytes(0)); 371 _from -= Interpreter::stackElementSize; 372 373 if (_num_args < Argument::n_float_register_parameters_c-1) { 374 assert((_num_args*2) < BitsPerWord, "_num_args*2 is out of range"); 375 *_reg_args++ = from_obj; 376 *_fp_identifiers |= ((intptr_t)0x01 << (_num_args*2)); // mark as float 377 _num_args++; 378 } else { 379 *_to++ = from_obj; 380 } 381 } 382 383 virtual void pass_double() 384 { 385 intptr_t from_obj = *(intptr_t*)(_from+Interpreter::local_offset_in_bytes(1)); 386 _from -= 2*Interpreter::stackElementSize; 387 388 if (_num_args < Argument::n_float_register_parameters_c-1) { 389 assert((_num_args*2) < BitsPerWord, "_num_args*2 is out of range"); 390 *_reg_args++ = from_obj; 391 *_fp_identifiers |= ((intptr_t)0x3 << (_num_args*2)); // mark as double 392 _num_args++; 393 } else { 394 *_to++ = from_obj; 395 } 396 } 397 398 public: 399 SlowSignatureHandler(const methodHandle& method, address from, intptr_t* to) 400 : NativeSignatureIterator(method) 401 { 402 _from = from; 403 _to = to; 404 405 _reg_args = to - (method->is_static() ? 4 : 5); 406 _fp_identifiers = to - 2; 407 _to = _to + 4; // Windows reserves stack space for register arguments 408 *(int*) _fp_identifiers = 0; 409 _num_args = (method->is_static() ? 1 : 0); 410 } 411 }; 412 #else 413 class SlowSignatureHandler 414 : public NativeSignatureIterator { 415 private: 416 address _from; 417 intptr_t* _to; 418 intptr_t* _int_args; 419 intptr_t* _fp_args; 420 intptr_t* _fp_identifiers; 421 unsigned int _num_int_args; 422 unsigned int _num_fp_args; 423 424 virtual void pass_int() 425 { 426 jint from_obj = *(jint *)(_from+Interpreter::local_offset_in_bytes(0)); 427 _from -= Interpreter::stackElementSize; 428 429 if (_num_int_args < Argument::n_int_register_parameters_c-1) { 430 *_int_args++ = from_obj; 431 _num_int_args++; 432 } else { 433 *_to++ = from_obj; 434 } 435 } 436 437 virtual void pass_long() 438 { 439 intptr_t from_obj = *(intptr_t*)(_from+Interpreter::local_offset_in_bytes(1)); 440 _from -= 2*Interpreter::stackElementSize; 441 442 if (_num_int_args < Argument::n_int_register_parameters_c-1) { 443 *_int_args++ = from_obj; 444 _num_int_args++; 445 } else { 446 *_to++ = from_obj; 447 } 448 } 449 450 virtual void pass_object() 451 { 452 intptr_t *from_addr = (intptr_t*)(_from + Interpreter::local_offset_in_bytes(0)); 453 _from -= Interpreter::stackElementSize; 454 455 if (_num_int_args < Argument::n_int_register_parameters_c-1) { 456 *_int_args++ = (*from_addr == 0) ? NULL : (intptr_t)from_addr; 457 _num_int_args++; 458 } else { 459 *_to++ = (*from_addr == 0) ? NULL : (intptr_t) from_addr; 460 } 461 } 462 463 virtual void pass_valuetype() { 464 // values are handled with oops, like objects 465 pass_object(); 466 } 467 468 virtual void pass_float() 469 { 470 jint from_obj = *(jint*)(_from+Interpreter::local_offset_in_bytes(0)); 471 _from -= Interpreter::stackElementSize; 472 473 if (_num_fp_args < Argument::n_float_register_parameters_c) { 474 *_fp_args++ = from_obj; 475 _num_fp_args++; 476 } else { 477 *_to++ = from_obj; 478 } 479 } 480 481 virtual void pass_double() 482 { 483 intptr_t from_obj = *(intptr_t*)(_from+Interpreter::local_offset_in_bytes(1)); 484 _from -= 2*Interpreter::stackElementSize; 485 486 if (_num_fp_args < Argument::n_float_register_parameters_c) { 487 *_fp_args++ = from_obj; 488 *_fp_identifiers |= (1 << _num_fp_args); // mark as double 489 _num_fp_args++; 490 } else { 491 *_to++ = from_obj; 492 } 493 } 494 495 public: 496 SlowSignatureHandler(const methodHandle& method, address from, intptr_t* to) 497 : NativeSignatureIterator(method) 498 { 499 _from = from; 500 _to = to; 501 502 _int_args = to - (method->is_static() ? 14 : 15); 503 _fp_args = to - 9; 504 _fp_identifiers = to - 10; 505 *(int*) _fp_identifiers = 0; 506 _num_int_args = (method->is_static() ? 1 : 0); 507 _num_fp_args = 0; 508 } 509 }; 510 #endif 511 512 513 IRT_ENTRY(address, 514 InterpreterRuntime::slow_signature_handler(JavaThread* thread, 515 Method* method, 516 intptr_t* from, 517 intptr_t* to)) 518 methodHandle m(thread, (Method*)method); 519 assert(m->is_native(), "sanity check"); 520 521 // handle arguments 522 SlowSignatureHandler(m, (address)from, to + 1).iterate((uint64_t)CONST64(-1)); 523 524 // return result handler 525 return Interpreter::result_handler(m->result_type()); 526 IRT_END