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