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::generate(uint64_t fingerprint) {
 259   // generate code to handle arguments
 260   iterate(fingerprint);
 261 
 262   // set the call format
 263   // n.b. allow extra 1 for the JNI_Env in c_rarg0
 264   unsigned int call_format = ((_num_int_args + 1) << 6) | (_num_fp_args << 2);
 265 
 266   switch (method()->result_type()) {
 267   case T_VOID:
 268     call_format |= MacroAssembler::ret_type_void;
 269     break;
 270   case T_FLOAT:
 271     call_format |= MacroAssembler::ret_type_float;
 272     break;
 273   case T_DOUBLE:
 274     call_format |= MacroAssembler::ret_type_double;
 275     break;
 276   default:
 277     call_format |= MacroAssembler::ret_type_integral;
 278     break;
 279   }
 280 
 281   // // store the call format in the method
 282   // __ movw(r0, call_format);
 283   // __ str(r0, Address(rmethod, Method::call_format_offset()));
 284 
 285   // return result handler
 286   __ lea(r0, ExternalAddress(Interpreter::result_handler(method()->result_type())));
 287   __ ret(lr);
 288 
 289   __ flush();
 290 }
 291 
 292 
 293 // Implementation of SignatureHandlerLibrary
 294 
 295 void SignatureHandlerLibrary::pd_set_handler(address handler) {}
 296 
 297 
 298 class SlowSignatureHandler
 299   : public NativeSignatureIterator {
 300  private:
 301   address   _from;
 302   intptr_t* _to;
 303   intptr_t* _int_args;
 304   intptr_t* _fp_args;
 305   intptr_t* _fp_identifiers;
 306   unsigned int _num_int_args;
 307   unsigned int _num_fp_args;
 308 
 309   virtual void pass_int()
 310   {
 311     jint from_obj = *(jint *)(_from+Interpreter::local_offset_in_bytes(0));
 312     _from -= Interpreter::stackElementSize;
 313 
 314     if (_num_int_args < Argument::n_int_register_parameters_c-1) {
 315       *_int_args++ = from_obj;
 316       _num_int_args++;
 317     } else {
 318       *_to++ = from_obj;
 319       _num_int_args++;
 320     }
 321   }
 322 
 323   virtual void pass_long()
 324   {
 325     intptr_t from_obj = *(intptr_t*)(_from+Interpreter::local_offset_in_bytes(1));
 326     _from -= 2*Interpreter::stackElementSize;
 327 
 328     if (_num_int_args < Argument::n_int_register_parameters_c-1) {
 329       *_int_args++ = from_obj;
 330       _num_int_args++;
 331     } else {
 332       *_to++ = from_obj;
 333       _num_int_args++;
 334     }
 335   }
 336 
 337   virtual void pass_object()
 338   {
 339     intptr_t *from_addr = (intptr_t*)(_from + Interpreter::local_offset_in_bytes(0));
 340     _from -= Interpreter::stackElementSize;
 341 
 342     if (_num_int_args < Argument::n_int_register_parameters_c-1) {
 343       *_int_args++ = (*from_addr == 0) ? NULL : (intptr_t)from_addr;
 344       _num_int_args++;
 345     } else {
 346       *_to++ = (*from_addr == 0) ? NULL : (intptr_t) from_addr;
 347       _num_int_args++;
 348     }
 349   }
 350 
 351   virtual void pass_float()
 352   {
 353     jint from_obj = *(jint*)(_from+Interpreter::local_offset_in_bytes(0));
 354     _from -= Interpreter::stackElementSize;
 355 
 356     if (_num_fp_args < Argument::n_float_register_parameters_c) {
 357       *_fp_args++ = from_obj;
 358       _num_fp_args++;
 359     } else {
 360       *_to++ = from_obj;
 361       _num_fp_args++;
 362     }
 363   }
 364 
 365   virtual void pass_double()
 366   {
 367     intptr_t from_obj = *(intptr_t*)(_from+Interpreter::local_offset_in_bytes(1));
 368     _from -= 2*Interpreter::stackElementSize;
 369 
 370     if (_num_fp_args < Argument::n_float_register_parameters_c) {
 371       *_fp_args++ = from_obj;
 372       *_fp_identifiers |= (1 << _num_fp_args); // mark as double
 373       _num_fp_args++;
 374     } else {
 375       *_to++ = from_obj;
 376       _num_fp_args++;
 377     }
 378   }
 379 
 380  public:
 381   SlowSignatureHandler(const methodHandle& method, address from, intptr_t* to)
 382     : NativeSignatureIterator(method)
 383   {
 384     _from = from;
 385     _to   = to;
 386 
 387     _int_args = to - (method->is_static() ? 16 : 17);
 388     _fp_args =  to - 8;
 389     _fp_identifiers = to - 9;
 390     *(int*) _fp_identifiers = 0;
 391     _num_int_args = (method->is_static() ? 1 : 0);
 392     _num_fp_args = 0;
 393   }
 394 
 395   // n.b. allow extra 1 for the JNI_Env in c_rarg0
 396   unsigned int get_call_format()
 397   {
 398     unsigned int call_format = ((_num_int_args + 1) << 6) | (_num_fp_args << 2);
 399 
 400     switch (method()->result_type()) {
 401     case T_VOID:
 402       call_format |= MacroAssembler::ret_type_void;
 403       break;
 404     case T_FLOAT:
 405       call_format |= MacroAssembler::ret_type_float;
 406       break;
 407     case T_DOUBLE:
 408       call_format |= MacroAssembler::ret_type_double;
 409       break;
 410     default:
 411       call_format |= MacroAssembler::ret_type_integral;
 412       break;
 413     }
 414 
 415     return call_format;
 416   }
 417 };
 418 
 419 
 420 JRT_ENTRY(address,
 421           InterpreterRuntime::slow_signature_handler(JavaThread* thread,
 422                                                      Method* method,
 423                                                      intptr_t* from,
 424                                                      intptr_t* to))
 425   methodHandle m(thread, (Method*)method);
 426   assert(m->is_native(), "sanity check");
 427 
 428   // handle arguments
 429   SlowSignatureHandler ssh(m, (address)from, to);
 430   ssh.iterate(UCONST64(-1));
 431 
 432   // // set the call format
 433   // method->set_call_format(ssh.get_call_format());
 434 
 435   // return result handler
 436   return Interpreter::result_handler(m->result_type());
 437 JRT_END