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