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   // return result handler
 263   __ lea(r0, ExternalAddress(Interpreter::result_handler(method()->result_type())));
 264   __ ret(lr);
 265 
 266   __ flush();
 267 }
 268 
 269 
 270 // Implementation of SignatureHandlerLibrary
 271 
 272 void SignatureHandlerLibrary::pd_set_handler(address handler) {}
 273 
 274 
 275 class SlowSignatureHandler
 276   : public NativeSignatureIterator {
 277  private:
 278   address   _from;
 279   intptr_t* _to;
 280   intptr_t* _int_args;
 281   intptr_t* _fp_args;
 282   intptr_t* _fp_identifiers;
 283   unsigned int _num_int_args;
 284   unsigned int _num_fp_args;
 285 
 286   virtual void pass_int()
 287   {
 288     jint from_obj = *(jint *)(_from+Interpreter::local_offset_in_bytes(0));
 289     _from -= Interpreter::stackElementSize;
 290 
 291     if (_num_int_args < Argument::n_int_register_parameters_c-1) {
 292       *_int_args++ = from_obj;
 293       _num_int_args++;
 294     } else {
 295       *_to++ = from_obj;
 296       _num_int_args++;
 297     }
 298   }
 299 
 300   virtual void pass_long()
 301   {
 302     intptr_t from_obj = *(intptr_t*)(_from+Interpreter::local_offset_in_bytes(1));
 303     _from -= 2*Interpreter::stackElementSize;
 304 
 305     if (_num_int_args < Argument::n_int_register_parameters_c-1) {
 306       *_int_args++ = from_obj;
 307       _num_int_args++;
 308     } else {
 309       *_to++ = from_obj;
 310       _num_int_args++;
 311     }
 312   }
 313 
 314   virtual void pass_object()
 315   {
 316     intptr_t *from_addr = (intptr_t*)(_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_addr == 0) ? NULL : (intptr_t)from_addr;
 321       _num_int_args++;
 322     } else {
 323       *_to++ = (*from_addr == 0) ? NULL : (intptr_t) from_addr;
 324       _num_int_args++;
 325     }
 326   }
 327 
 328   virtual void pass_float()
 329   {
 330     jint from_obj = *(jint*)(_from+Interpreter::local_offset_in_bytes(0));
 331     _from -= Interpreter::stackElementSize;
 332 
 333     if (_num_fp_args < Argument::n_float_register_parameters_c) {
 334       *_fp_args++ = from_obj;
 335       _num_fp_args++;
 336     } else {
 337       *_to++ = from_obj;
 338       _num_fp_args++;
 339     }
 340   }
 341 
 342   virtual void pass_double()
 343   {
 344     intptr_t from_obj = *(intptr_t*)(_from+Interpreter::local_offset_in_bytes(1));
 345     _from -= 2*Interpreter::stackElementSize;
 346 
 347     if (_num_fp_args < Argument::n_float_register_parameters_c) {
 348       *_fp_args++ = from_obj;
 349       *_fp_identifiers |= (1ULL << _num_fp_args); // mark as double
 350       _num_fp_args++;
 351     } else {
 352       *_to++ = from_obj;
 353       _num_fp_args++;
 354     }
 355   }
 356 
 357  public:
 358   SlowSignatureHandler(const methodHandle& method, address from, intptr_t* to)
 359     : NativeSignatureIterator(method)
 360   {
 361     _from = from;
 362     _to   = to;
 363 
 364     _int_args = to - (method->is_static() ? 16 : 17);
 365     _fp_args =  to - 8;
 366     _fp_identifiers = to - 9;
 367     *(int*) _fp_identifiers = 0;
 368     _num_int_args = (method->is_static() ? 1 : 0);
 369     _num_fp_args = 0;
 370   }
 371 
 372 };
 373 
 374 
 375 JRT_ENTRY(address,
 376           InterpreterRuntime::slow_signature_handler(JavaThread* thread,
 377                                                      Method* method,
 378                                                      intptr_t* from,
 379                                                      intptr_t* to))
 380   methodHandle m(thread, (Method*)method);
 381   assert(m->is_native(), "sanity check");
 382 
 383   // handle arguments
 384   SlowSignatureHandler ssh(m, (address)from, to);
 385   ssh.iterate(UCONST64(-1));
 386 
 387   // return result handler
 388   return Interpreter::result_handler(m->result_type());
 389 JRT_END