1 /*
   2  * Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "interpreter/interp_masm.hpp"
  27 #include "interpreter/interpreter.hpp"
  28 #include "interpreter/interpreterRuntime.hpp"
  29 #include "memory/allocation.inline.hpp"
  30 #include "memory/universe.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.inline.hpp"
  36 #include "runtime/signature.hpp"
  37 
  38 #define __ _masm->
  39 
  40 Interpreter::SignatureHandlerGenerator::SignatureHandlerGenerator(
  41     const methodHandle& method, CodeBuffer* buffer) : NativeSignatureIterator(method) {
  42   _masm = new MacroAssembler(buffer);
  43   _abi_offset = 0;
  44   _ireg = is_static() ? 2 : 1;
  45 #ifdef __ABI_HARD__
  46 #ifdef AARCH64
  47   _freg = 0;
  48 #else
  49   _fp_slot = 0;
  50   _single_fpr_slot = 0;
  51 #endif
  52 #endif
  53 }
  54 
  55 #ifdef SHARING_FAST_NATIVE_FINGERPRINTS
  56 // mapping from SignatureIterator param to (common) type of parsing
  57 static const u1 shared_type[] = {
  58   (u1) SignatureIterator::int_parm, // bool
  59   (u1) SignatureIterator::int_parm, // byte
  60   (u1) SignatureIterator::int_parm, // char
  61   (u1) SignatureIterator::int_parm, // short
  62   (u1) SignatureIterator::int_parm, // int
  63   (u1) SignatureIterator::long_parm, // long
  64 #ifndef __ABI_HARD__
  65   (u1) SignatureIterator::int_parm, // float, passed as int
  66   (u1) SignatureIterator::long_parm, // double, passed as long
  67 #else
  68   (u1) SignatureIterator::float_parm, // float
  69   (u1) SignatureIterator::double_parm, // double
  70 #endif
  71   (u1) SignatureIterator::obj_parm, // obj
  72   (u1) SignatureIterator::done_parm // done
  73 };
  74 
  75 uint64_t InterpreterRuntime::normalize_fast_native_fingerprint(uint64_t fingerprint) {
  76   if (fingerprint == UCONST64(-1)) {
  77     // special signature used when the argument list cannot be encoded in a 64 bits value
  78     return fingerprint;
  79   }
  80   int shift = SignatureIterator::static_feature_size;
  81   uint64_t result = fingerprint & ((1 << shift) - 1);
  82   fingerprint >>= shift;
  83 
  84   BasicType ret_type = (BasicType) (fingerprint & SignatureIterator::result_feature_mask);
  85   // For ARM, the fast signature handler only needs to know whether
  86   // the return value must be unboxed. T_OBJECT and T_ARRAY need not
  87   // be distinguished from each other and all other return values
  88   // behave like integers with respect to the handler.
  89   bool unbox = (ret_type == T_OBJECT) || (ret_type == T_ARRAY);
  90   if (unbox) {
  91     ret_type = T_OBJECT;
  92   } else {
  93     ret_type = T_INT;
  94   }
  95   result |= ((uint64_t) ret_type) << shift;
  96   shift += SignatureIterator::result_feature_size;
  97   fingerprint >>= SignatureIterator::result_feature_size;
  98 
  99   while (true) {
 100     uint32_t type = (uint32_t) (fingerprint & SignatureIterator::parameter_feature_mask);
 101     if (type == SignatureIterator::done_parm) {
 102       result |= ((uint64_t) SignatureIterator::done_parm) << shift;
 103       return result;
 104     }
 105     assert((type >= SignatureIterator::bool_parm) && (type <= SignatureIterator::obj_parm), "check fingerprint encoding");
 106     int shared = shared_type[type - SignatureIterator::bool_parm];
 107     result |= ((uint64_t) shared) << shift;
 108     shift += SignatureIterator::parameter_feature_size;
 109     fingerprint >>= SignatureIterator::parameter_feature_size;
 110   }
 111 }
 112 #endif // SHARING_FAST_NATIVE_FINGERPRINTS
 113 
 114 // Implementation of SignatureHandlerGenerator
 115 void InterpreterRuntime::SignatureHandlerGenerator::pass_int() {
 116   if (_ireg < GPR_PARAMS) {
 117     Register dst = as_Register(_ireg);
 118     __ ldr_s32(dst, Address(Rlocals, Interpreter::local_offset_in_bytes(offset())));
 119     _ireg++;
 120   } else {
 121     __ ldr_s32(Rtemp, Address(Rlocals, Interpreter::local_offset_in_bytes(offset())));
 122     __ str_32(Rtemp, Address(SP, _abi_offset * wordSize));
 123     _abi_offset++;
 124   }
 125 }
 126 
 127 void InterpreterRuntime::SignatureHandlerGenerator::pass_long() {
 128 #ifdef AARCH64
 129   if (_ireg < GPR_PARAMS) {
 130     Register dst = as_Register(_ireg);
 131     __ ldr(dst, Address(Rlocals, Interpreter::local_offset_in_bytes(offset() + 1)));
 132     _ireg++;
 133   } else {
 134     __ ldr(Rtemp, Address(Rlocals, Interpreter::local_offset_in_bytes(offset() + 1)));
 135     __ str(Rtemp, Address(SP, _abi_offset * wordSize));
 136     _abi_offset++;
 137   }
 138 #else
 139   if (_ireg <= 2) {
 140 #if (ALIGN_WIDE_ARGUMENTS == 1)
 141     if ((_ireg & 1) != 0) {
 142       // 64-bit values should be 8-byte aligned
 143       _ireg++;
 144     }
 145 #endif
 146     Register dst1 = as_Register(_ireg);
 147     Register dst2 = as_Register(_ireg+1);
 148     __ ldr(dst1, Address(Rlocals, Interpreter::local_offset_in_bytes(offset()+1)));
 149     __ ldr(dst2, Address(Rlocals, Interpreter::local_offset_in_bytes(offset())));
 150     _ireg += 2;
 151 #if (ALIGN_WIDE_ARGUMENTS == 0)
 152   } else if (_ireg == 3) {
 153     // uses R3 + one stack slot
 154     Register dst1 = as_Register(_ireg);
 155     __ ldr(Rtemp, Address(Rlocals, Interpreter::local_offset_in_bytes(offset())));
 156     __ ldr(dst1, Address(Rlocals, Interpreter::local_offset_in_bytes(offset()+1)));
 157     __ str(Rtemp, Address(SP, _abi_offset * wordSize));
 158     _ireg += 1;
 159     _abi_offset += 1;
 160 #endif
 161   } else {
 162 #if (ALIGN_WIDE_ARGUMENTS == 1)
 163     if(_abi_offset & 1) _abi_offset++;
 164 #endif
 165     __ ldr(Rtemp, Address(Rlocals, Interpreter::local_offset_in_bytes(offset()+1)));
 166     __ str(Rtemp, Address(SP, (_abi_offset) * wordSize));
 167     __ ldr(Rtemp, Address(Rlocals, Interpreter::local_offset_in_bytes(offset())));
 168     __ str(Rtemp, Address(SP, (_abi_offset+1) * wordSize));
 169     _abi_offset += 2;
 170     _ireg = 4;
 171   }
 172 #endif // AARCH64
 173 }
 174 
 175 void InterpreterRuntime::SignatureHandlerGenerator::pass_object() {
 176 #ifdef AARCH64
 177   __ ldr(Rtemp, Address(Rlocals, Interpreter::local_offset_in_bytes(offset())));
 178   __ cmp(Rtemp, 0);
 179   __ sub(Rtemp, Rlocals, -Interpreter::local_offset_in_bytes(offset()));
 180   if (_ireg < GPR_PARAMS) {
 181     Register dst = as_Register(_ireg);
 182     __ csel(dst, ZR, Rtemp, eq);
 183     _ireg++;
 184   } else {
 185     __ csel(Rtemp, ZR, Rtemp, eq);
 186     __ str(Rtemp, Address(SP, _abi_offset * wordSize));
 187     _abi_offset++;
 188   }
 189 #else
 190   if (_ireg < 4) {
 191     Register dst = as_Register(_ireg);
 192     __ ldr(dst, Address(Rlocals, Interpreter::local_offset_in_bytes(offset())));
 193     __ cmp(dst, 0);
 194     __ sub(dst, Rlocals, -Interpreter::local_offset_in_bytes(offset()), ne);
 195     _ireg++;
 196   } else {
 197     __ ldr(Rtemp, Address(Rlocals, Interpreter::local_offset_in_bytes(offset())));
 198     __ cmp(Rtemp, 0);
 199     __ sub(Rtemp, Rlocals, -Interpreter::local_offset_in_bytes(offset()), ne);
 200     __ str(Rtemp, Address(SP, _abi_offset * wordSize));
 201     _abi_offset++;
 202   }
 203 #endif // AARCH64
 204 }
 205 
 206 #ifndef __ABI_HARD__
 207 void InterpreterRuntime::SignatureHandlerGenerator::pass_float() {
 208   if (_ireg < 4) {
 209     Register dst = as_Register(_ireg);
 210     __ ldr(dst, Address(Rlocals, Interpreter::local_offset_in_bytes(offset())));
 211     _ireg++;
 212   } else {
 213     __ ldr(Rtemp, Address(Rlocals, Interpreter::local_offset_in_bytes(offset())));
 214     __ str(Rtemp, Address(SP, _abi_offset * wordSize));
 215     _abi_offset++;
 216   }
 217 }
 218 
 219 #else
 220 #ifndef __SOFTFP__
 221 void InterpreterRuntime::SignatureHandlerGenerator::pass_float() {
 222 #ifdef AARCH64
 223     if (_freg < FPR_PARAMS) {
 224       FloatRegister dst = as_FloatRegister(_freg);
 225       __ ldr_s(dst, Address(Rlocals, Interpreter::local_offset_in_bytes(offset())));
 226       _freg++;
 227     } else {
 228       __ ldr_u32(Rtemp, Address(Rlocals, Interpreter::local_offset_in_bytes(offset())));
 229       __ str_32(Rtemp, Address(SP, _abi_offset * wordSize));
 230       _abi_offset++;
 231     }
 232 #else
 233     if((_fp_slot < 16) || (_single_fpr_slot & 1)) {
 234       if ((_single_fpr_slot & 1) == 0) {
 235         _single_fpr_slot = _fp_slot;
 236         _fp_slot += 2;
 237       }
 238       __ flds(as_FloatRegister(_single_fpr_slot), Address(Rlocals, Interpreter::local_offset_in_bytes(offset())));
 239       _single_fpr_slot++;
 240     } else {
 241       __ ldr(Rtemp, Address(Rlocals, Interpreter::local_offset_in_bytes(offset())));
 242       __ str(Rtemp, Address(SP, _abi_offset * wordSize));
 243       _abi_offset++;
 244     }
 245 #endif // AARCH64
 246 }
 247 
 248 void InterpreterRuntime::SignatureHandlerGenerator::pass_double() {
 249 #ifdef AARCH64
 250     if (_freg < FPR_PARAMS) {
 251       FloatRegister dst = as_FloatRegister(_freg);
 252       __ ldr_d(dst, Address(Rlocals, Interpreter::local_offset_in_bytes(offset() + 1)));
 253       _freg++;
 254     } else {
 255       __ ldr(Rtemp, Address(Rlocals, Interpreter::local_offset_in_bytes(offset() + 1)));
 256       __ str(Rtemp, Address(SP, _abi_offset * wordSize));
 257       _abi_offset++;
 258     }
 259 #else
 260     if(_fp_slot <= 14) {
 261       __ fldd(as_FloatRegister(_fp_slot), Address(Rlocals, Interpreter::local_offset_in_bytes(offset()+1)));
 262       _fp_slot += 2;
 263     } else {
 264       __ ldr(Rtemp, Address(Rlocals, Interpreter::local_offset_in_bytes(offset()+1)));
 265       __ str(Rtemp, Address(SP, (_abi_offset) * wordSize));
 266       __ ldr(Rtemp, Address(Rlocals, Interpreter::local_offset_in_bytes(offset())));
 267       __ str(Rtemp, Address(SP, (_abi_offset+1) * wordSize));
 268       _abi_offset += 2;
 269       _single_fpr_slot = 16;
 270     }
 271 #endif // AARCH64
 272 }
 273 #endif // __SOFTFP__
 274 #endif // __ABI_HARD__
 275 
 276 void InterpreterRuntime::SignatureHandlerGenerator::generate(uint64_t fingerprint) {
 277   iterate(fingerprint);
 278 
 279   BasicType result_type = SignatureIterator::return_type(fingerprint);
 280 
 281   address result_handler = Interpreter::result_handler(result_type);
 282 
 283 #ifdef AARCH64
 284   __ mov_slow(R0, (address)result_handler);
 285 #else
 286   // Check that result handlers are not real handler on ARM (0 or -1).
 287   // This ensures the signature handlers do not need symbolic information.
 288   assert((result_handler == NULL)||(result_handler==(address)0xffffffff),"");
 289   __ mov_slow(R0, (intptr_t)result_handler);
 290 #endif
 291 
 292   __ ret();
 293 }
 294 
 295 
 296 // Implementation of SignatureHandlerLibrary
 297 
 298 void SignatureHandlerLibrary::pd_set_handler(address handler) {}
 299 
 300 class SlowSignatureHandler: public NativeSignatureIterator {
 301  private:
 302   address   _from;
 303   intptr_t* _to;
 304 
 305 #ifndef __ABI_HARD__
 306   virtual void pass_int() {
 307     *_to++ = *(jint *)(_from+Interpreter::local_offset_in_bytes(0));
 308     _from -= Interpreter::stackElementSize;
 309   }
 310 
 311   virtual void pass_float() {
 312     *_to++ = *(jint *)(_from+Interpreter::local_offset_in_bytes(0));
 313     _from -= Interpreter::stackElementSize;
 314   }
 315 
 316   virtual void pass_long() {
 317 #if (ALIGN_WIDE_ARGUMENTS == 1)
 318     if (((intptr_t)_to & 7) != 0) {
 319       // 64-bit values should be 8-byte aligned
 320       _to++;
 321     }
 322 #endif
 323     _to[0] = *(intptr_t*)(_from+Interpreter::local_offset_in_bytes(1));
 324     _to[1] = *(intptr_t*)(_from+Interpreter::local_offset_in_bytes(0));
 325     _to += 2;
 326     _from -= 2*Interpreter::stackElementSize;
 327   }
 328 
 329   virtual void pass_object() {
 330     intptr_t from_addr = (intptr_t)(_from + Interpreter::local_offset_in_bytes(0));
 331     *_to++ = (*(intptr_t*)from_addr == 0) ? (intptr_t)NULL : from_addr;
 332     _from -= Interpreter::stackElementSize;
 333    }
 334 
 335 #else
 336 
 337   intptr_t* _toFP;
 338   intptr_t* _toGP;
 339   int       _last_gp;
 340   int       _last_fp;
 341 #ifndef AARCH64
 342   int       _last_single_fp;
 343 #endif // !AARCH64
 344 
 345   virtual void pass_int() {
 346     if(_last_gp < GPR_PARAMS) {
 347       _toGP[_last_gp++] = *(jint *)(_from+Interpreter::local_offset_in_bytes(0));
 348     } else {
 349       *_to++ = *(jint *)(_from+Interpreter::local_offset_in_bytes(0));
 350     }
 351     _from -= Interpreter::stackElementSize;
 352   }
 353 
 354   virtual void pass_long() {
 355 #ifdef AARCH64
 356     if(_last_gp < GPR_PARAMS) {
 357       _toGP[_last_gp++] = *(jlong *)(_from+Interpreter::local_offset_in_bytes(1));
 358     } else {
 359       *_to++ = *(jlong *)(_from+Interpreter::local_offset_in_bytes(1));
 360     }
 361 #else
 362     assert(ALIGN_WIDE_ARGUMENTS == 1, "ABI_HARD not supported with unaligned wide arguments");
 363     if (_last_gp <= 2) {
 364       if(_last_gp & 1) _last_gp++;
 365       _toGP[_last_gp++] = *(jint *)(_from+Interpreter::local_offset_in_bytes(1));
 366       _toGP[_last_gp++] = *(jint *)(_from+Interpreter::local_offset_in_bytes(0));
 367     } else {
 368       if (((intptr_t)_to & 7) != 0) {
 369         // 64-bit values should be 8-byte aligned
 370         _to++;
 371       }
 372       _to[0] = *(intptr_t*)(_from+Interpreter::local_offset_in_bytes(1));
 373       _to[1] = *(intptr_t*)(_from+Interpreter::local_offset_in_bytes(0));
 374       _to += 2;
 375       _last_gp = 4;
 376     }
 377 #endif // AARCH64
 378     _from -= 2*Interpreter::stackElementSize;
 379   }
 380 
 381   virtual void pass_object() {
 382     intptr_t from_addr = (intptr_t)(_from + Interpreter::local_offset_in_bytes(0));
 383     if(_last_gp < GPR_PARAMS) {
 384       _toGP[_last_gp++] = (*(intptr_t*)from_addr == 0) ? NULL : from_addr;
 385     } else {
 386       *_to++ = (*(intptr_t*)from_addr == 0) ? NULL : from_addr;
 387     }
 388     _from -= Interpreter::stackElementSize;
 389   }
 390 
 391   virtual void pass_float() {
 392 #ifdef AARCH64
 393     if(_last_fp < FPR_PARAMS) {
 394       _toFP[_last_fp++] = *(jint *)(_from+Interpreter::local_offset_in_bytes(0));
 395     } else {
 396       *_to++ = *(jint *)(_from+Interpreter::local_offset_in_bytes(0));
 397     }
 398 #else
 399     if((_last_fp < 16) || (_last_single_fp & 1)) {
 400       if ((_last_single_fp & 1) == 0) {
 401         _last_single_fp = _last_fp;
 402         _last_fp += 2;
 403       }
 404 
 405       _toFP[_last_single_fp++] = *(jint *)(_from+Interpreter::local_offset_in_bytes(0));
 406     } else {
 407       *_to++ = *(jint *)(_from+Interpreter::local_offset_in_bytes(0));
 408     }
 409 #endif // AARCH64
 410     _from -= Interpreter::stackElementSize;
 411   }
 412 
 413   virtual void pass_double() {
 414 #ifdef AARCH64
 415     if(_last_fp < FPR_PARAMS) {
 416       _toFP[_last_fp++] = *(intptr_t*)(_from+Interpreter::local_offset_in_bytes(1));
 417     } else {
 418       *_to++ = *(intptr_t*)(_from+Interpreter::local_offset_in_bytes(1));
 419     }
 420 #else
 421     assert(ALIGN_WIDE_ARGUMENTS == 1, "ABI_HARD not supported with unaligned wide arguments");
 422     if(_last_fp <= 14) {
 423       _toFP[_last_fp++] = *(intptr_t*)(_from+Interpreter::local_offset_in_bytes(1));
 424       _toFP[_last_fp++] = *(intptr_t*)(_from+Interpreter::local_offset_in_bytes(0));
 425     } else {
 426       if (((intptr_t)_to & 7) != 0) {      // 64-bit values should be 8-byte aligned
 427         _to++;
 428       }
 429       _to[0] = *(intptr_t*)(_from+Interpreter::local_offset_in_bytes(1));
 430       _to[1] = *(intptr_t*)(_from+Interpreter::local_offset_in_bytes(0));
 431       _to += 2;
 432       _last_single_fp = 16;
 433     }
 434 #endif // AARCH64
 435     _from -= 2*Interpreter::stackElementSize;
 436   }
 437 
 438 #endif // !__ABI_HARD__
 439 
 440  public:
 441   SlowSignatureHandler(const methodHandle& method, address from, intptr_t* to) :
 442     NativeSignatureIterator(method) {
 443     _from = from;
 444 
 445 #ifdef __ABI_HARD__
 446     _toGP  = to;
 447     _toFP = _toGP + GPR_PARAMS;
 448     _to   = _toFP + AARCH64_ONLY(FPR_PARAMS) NOT_AARCH64(8*2);
 449     _last_gp = (is_static() ? 2 : 1);
 450     _last_fp = 0;
 451 #ifndef AARCH64
 452     _last_single_fp = 0;
 453 #endif // !AARCH64
 454 #else
 455     _to   = to + (is_static() ? 2 : 1);
 456 #endif // __ABI_HARD__
 457   }
 458 };
 459 
 460 IRT_ENTRY(address, InterpreterRuntime::slow_signature_handler(JavaThread* thread, Method* method, intptr_t* from, intptr_t* to))
 461   methodHandle m(thread, (Method*)method);
 462   assert(m->is_native(), "sanity check");
 463   SlowSignatureHandler(m, (address)from, to).iterate(UCONST64(-1));
 464   return Interpreter::result_handler(m->result_type());
 465 IRT_END