1 /* 2 * Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved. 3 * Copyright (c) 2014, Red Hat Inc. 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/interp_masm.hpp" 29 #include "interpreter/interpreter.hpp" 30 #include "interpreter/interpreterRuntime.hpp" 31 #include "memory/allocation.inline.hpp" 32 #include "oops/method.hpp" 33 #include "oops/oop.inline.hpp" 34 #include "runtime/handles.inline.hpp" 35 #include "runtime/icache.hpp" 36 #include "runtime/interfaceSupport.inline.hpp" 37 #include "runtime/signature.hpp" 38 39 #define __ _masm-> 40 41 // Implementation of SignatureHandlerGenerator 42 Register InterpreterRuntime::SignatureHandlerGenerator::from() { return rlocals; } 43 Register InterpreterRuntime::SignatureHandlerGenerator::to() { return sp; } 44 Register InterpreterRuntime::SignatureHandlerGenerator::temp() { return rscratch1; } 45 46 InterpreterRuntime::SignatureHandlerGenerator::SignatureHandlerGenerator( 47 const methodHandle& method, CodeBuffer* buffer) : NativeSignatureIterator(method) { 48 _masm = new MacroAssembler(buffer); 49 _num_int_args = (method->is_static() ? 1 : 0); 50 _num_fp_args = 0; 51 _stack_offset = 0; 52 } 53 54 void InterpreterRuntime::SignatureHandlerGenerator::pass_int() { 55 const Address src(from(), Interpreter::local_offset_in_bytes(offset())); 56 57 switch (_num_int_args) { 58 case 0: 59 __ ldr(c_rarg1, src); 60 _num_int_args++; 61 break; 62 case 1: 63 __ ldr(c_rarg2, src); 64 _num_int_args++; 65 break; 66 case 2: 67 __ ldr(c_rarg3, src); 68 _num_int_args++; 69 break; 70 case 3: 71 __ ldr(c_rarg4, src); 72 _num_int_args++; 73 break; 74 case 4: 75 __ ldr(c_rarg5, src); 76 _num_int_args++; 77 break; 78 case 5: 79 __ ldr(c_rarg6, src); 80 _num_int_args++; 81 break; 82 case 6: 83 __ ldr(c_rarg7, src); 84 _num_int_args++; 85 break; 86 default: 87 __ ldr(r0, src); 88 __ str(r0, Address(to(), _stack_offset)); 89 _stack_offset += wordSize; 90 _num_int_args++; 91 break; 92 } 93 } 94 95 void InterpreterRuntime::SignatureHandlerGenerator::pass_long() { 96 const Address src(from(), Interpreter::local_offset_in_bytes(offset() + 1)); 97 98 switch (_num_int_args) { 99 case 0: 100 __ ldr(c_rarg1, src); 101 _num_int_args++; 102 break; 103 case 1: 104 __ ldr(c_rarg2, src); 105 _num_int_args++; 106 break; 107 case 2: 108 __ ldr(c_rarg3, src); 109 _num_int_args++; 110 break; 111 case 3: 112 __ ldr(c_rarg4, src); 113 _num_int_args++; 114 break; 115 case 4: 116 __ ldr(c_rarg5, src); 117 _num_int_args++; 118 break; 119 case 5: 120 __ ldr(c_rarg6, src); 121 _num_int_args++; 122 break; 123 case 6: 124 __ ldr(c_rarg7, src); 125 _num_int_args++; 126 break; 127 default: 128 __ ldr(r0, src); 129 __ str(r0, Address(to(), _stack_offset)); 130 _stack_offset += wordSize; 131 _num_int_args++; 132 break; 133 } 134 } 135 136 void InterpreterRuntime::SignatureHandlerGenerator::pass_float() { 137 const Address src(from(), Interpreter::local_offset_in_bytes(offset())); 138 139 if (_num_fp_args < Argument::n_float_register_parameters_c) { 140 __ ldrs(as_FloatRegister(_num_fp_args++), src); 141 } else { 142 __ ldrw(r0, src); 143 __ strw(r0, Address(to(), _stack_offset)); 144 _stack_offset += wordSize; 145 _num_fp_args++; 146 } 147 } 148 149 void InterpreterRuntime::SignatureHandlerGenerator::pass_double() { 150 const Address src(from(), Interpreter::local_offset_in_bytes(offset() + 1)); 151 152 if (_num_fp_args < Argument::n_float_register_parameters_c) { 153 __ ldrd(as_FloatRegister(_num_fp_args++), src); 154 } else { 155 __ ldr(r0, src); 156 __ str(r0, Address(to(), _stack_offset)); 157 _stack_offset += wordSize; 158 _num_fp_args++; 159 } 160 } 161 162 void InterpreterRuntime::SignatureHandlerGenerator::pass_object() { 163 164 switch (_num_int_args) { 165 case 0: 166 assert(offset() == 0, "argument register 1 can only be (non-null) receiver"); 167 __ add(c_rarg1, from(), Interpreter::local_offset_in_bytes(offset())); 168 _num_int_args++; 169 break; 170 case 1: 171 { 172 __ add(r0, from(), Interpreter::local_offset_in_bytes(offset())); 173 __ mov(c_rarg2, 0); 174 __ ldr(temp(), r0); 175 Label L; 176 __ cbz(temp(), L); 177 __ mov(c_rarg2, r0); 178 __ bind(L); 179 _num_int_args++; 180 break; 181 } 182 case 2: 183 { 184 __ add(r0, from(), Interpreter::local_offset_in_bytes(offset())); 185 __ mov(c_rarg3, 0); 186 __ ldr(temp(), r0); 187 Label L; 188 __ cbz(temp(), L); 189 __ mov(c_rarg3, r0); 190 __ bind(L); 191 _num_int_args++; 192 break; 193 } 194 case 3: 195 { 196 __ add(r0, from(), Interpreter::local_offset_in_bytes(offset())); 197 __ mov(c_rarg4, 0); 198 __ ldr(temp(), r0); 199 Label L; 200 __ cbz(temp(), L); 201 __ mov(c_rarg4, r0); 202 __ bind(L); 203 _num_int_args++; 204 break; 205 } 206 case 4: 207 { 208 __ add(r0, from(), Interpreter::local_offset_in_bytes(offset())); 209 __ mov(c_rarg5, 0); 210 __ ldr(temp(), r0); 211 Label L; 212 __ cbz(temp(), L); 213 __ mov(c_rarg5, r0); 214 __ bind(L); 215 _num_int_args++; 216 break; 217 } 218 case 5: 219 { 220 __ add(r0, from(), Interpreter::local_offset_in_bytes(offset())); 221 __ mov(c_rarg6, 0); 222 __ ldr(temp(), r0); 223 Label L; 224 __ cbz(temp(), L); 225 __ mov(c_rarg6, r0); 226 __ bind(L); 227 _num_int_args++; 228 break; 229 } 230 case 6: 231 { 232 __ add(r0, from(), Interpreter::local_offset_in_bytes(offset())); 233 __ mov(c_rarg7, 0); 234 __ ldr(temp(), r0); 235 Label L; 236 __ cbz(temp(), L); 237 __ mov(c_rarg7, r0); 238 __ bind(L); 239 _num_int_args++; 240 break; 241 } 242 default: 243 { 244 __ add(r0, from(), Interpreter::local_offset_in_bytes(offset())); 245 __ ldr(temp(), r0); 246 Label L; 247 __ cbnz(temp(), L); 248 __ mov(r0, zr); 249 __ bind(L); 250 __ str(r0, Address(to(), _stack_offset)); 251 _stack_offset += wordSize; 252 _num_int_args++; 253 break; 254 } 255 } 256 } 257 258 void InterpreterRuntime::SignatureHandlerGenerator::pass_valuetype() { 259 pass_object(); 260 } 261 262 void InterpreterRuntime::SignatureHandlerGenerator::generate(uint64_t fingerprint) { 263 // generate code to handle arguments 264 iterate(fingerprint); 265 266 // set the call format 267 // n.b. allow extra 1 for the JNI_Env in c_rarg0 268 unsigned int call_format = ((_num_int_args + 1) << 6) | (_num_fp_args << 2); 269 270 switch (method()->result_type()) { 271 case T_VOID: 272 call_format |= MacroAssembler::ret_type_void; 273 break; 274 case T_FLOAT: 275 call_format |= MacroAssembler::ret_type_float; 276 break; 277 case T_DOUBLE: 278 call_format |= MacroAssembler::ret_type_double; 279 break; 280 default: 281 call_format |= MacroAssembler::ret_type_integral; 282 break; 283 } 284 285 // // store the call format in the method 286 // __ movw(r0, call_format); 287 // __ str(r0, Address(rmethod, Method::call_format_offset())); 288 289 // return result handler 290 __ lea(r0, ExternalAddress(Interpreter::result_handler(method()->result_type()))); 291 __ ret(lr); 292 293 __ flush(); 294 } 295 296 297 // Implementation of SignatureHandlerLibrary 298 299 void SignatureHandlerLibrary::pd_set_handler(address handler) {} 300 301 302 class SlowSignatureHandler 303 : public NativeSignatureIterator { 304 private: 305 address _from; 306 intptr_t* _to; 307 intptr_t* _int_args; 308 intptr_t* _fp_args; 309 intptr_t* _fp_identifiers; 310 unsigned int _num_int_args; 311 unsigned int _num_fp_args; 312 313 virtual void pass_int() 314 { 315 jint from_obj = *(jint *)(_from+Interpreter::local_offset_in_bytes(0)); 316 _from -= Interpreter::stackElementSize; 317 318 if (_num_int_args < Argument::n_int_register_parameters_c-1) { 319 *_int_args++ = from_obj; 320 _num_int_args++; 321 } else { 322 *_to++ = from_obj; 323 _num_int_args++; 324 } 325 } 326 327 virtual void pass_long() 328 { 329 intptr_t from_obj = *(intptr_t*)(_from+Interpreter::local_offset_in_bytes(1)); 330 _from -= 2*Interpreter::stackElementSize; 331 332 if (_num_int_args < Argument::n_int_register_parameters_c-1) { 333 *_int_args++ = from_obj; 334 _num_int_args++; 335 } else { 336 *_to++ = from_obj; 337 _num_int_args++; 338 } 339 } 340 341 virtual void pass_object() 342 { 343 intptr_t *from_addr = (intptr_t*)(_from + Interpreter::local_offset_in_bytes(0)); 344 _from -= Interpreter::stackElementSize; 345 346 if (_num_int_args < Argument::n_int_register_parameters_c-1) { 347 *_int_args++ = (*from_addr == 0) ? NULL : (intptr_t)from_addr; 348 _num_int_args++; 349 } else { 350 *_to++ = (*from_addr == 0) ? NULL : (intptr_t) from_addr; 351 _num_int_args++; 352 } 353 } 354 355 virtual void pass_valuetype() { 356 // values are handled with oops, like objects 357 pass_object(); 358 } 359 360 virtual void pass_float() 361 { 362 jint from_obj = *(jint*)(_from+Interpreter::local_offset_in_bytes(0)); 363 _from -= Interpreter::stackElementSize; 364 365 if (_num_fp_args < Argument::n_float_register_parameters_c) { 366 *_fp_args++ = from_obj; 367 _num_fp_args++; 368 } else { 369 *_to++ = from_obj; 370 _num_fp_args++; 371 } 372 } 373 374 virtual void pass_double() 375 { 376 intptr_t from_obj = *(intptr_t*)(_from+Interpreter::local_offset_in_bytes(1)); 377 _from -= 2*Interpreter::stackElementSize; 378 379 if (_num_fp_args < Argument::n_float_register_parameters_c) { 380 *_fp_args++ = from_obj; 381 *_fp_identifiers |= (1 << _num_fp_args); // mark as double 382 _num_fp_args++; 383 } else { 384 *_to++ = from_obj; 385 _num_fp_args++; 386 } 387 } 388 389 public: 390 SlowSignatureHandler(const methodHandle& method, address from, intptr_t* to) 391 : NativeSignatureIterator(method) 392 { 393 _from = from; 394 _to = to; 395 396 _int_args = to - (method->is_static() ? 16 : 17); 397 _fp_args = to - 8; 398 _fp_identifiers = to - 9; 399 *(int*) _fp_identifiers = 0; 400 _num_int_args = (method->is_static() ? 1 : 0); 401 _num_fp_args = 0; 402 } 403 404 // n.b. allow extra 1 for the JNI_Env in c_rarg0 405 unsigned int get_call_format() 406 { 407 unsigned int call_format = ((_num_int_args + 1) << 6) | (_num_fp_args << 2); 408 409 switch (method()->result_type()) { 410 case T_VOID: 411 call_format |= MacroAssembler::ret_type_void; 412 break; 413 case T_FLOAT: 414 call_format |= MacroAssembler::ret_type_float; 415 break; 416 case T_DOUBLE: 417 call_format |= MacroAssembler::ret_type_double; 418 break; 419 default: 420 call_format |= MacroAssembler::ret_type_integral; 421 break; 422 } 423 424 return call_format; 425 } 426 }; 427 428 429 JRT_ENTRY(address, 430 InterpreterRuntime::slow_signature_handler(JavaThread* thread, 431 Method* method, 432 intptr_t* from, 433 intptr_t* to)) 434 methodHandle m(thread, (Method*)method); 435 assert(m->is_native(), "sanity check"); 436 437 // handle arguments 438 SlowSignatureHandler ssh(m, (address)from, to); 439 ssh.iterate(UCONST64(-1)); 440 441 // // set the call format 442 // method->set_call_format(ssh.get_call_format()); 443 444 // return result handler 445 return Interpreter::result_handler(m->result_type()); 446 JRT_END