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