1 /*
   2  * Copyright (c) 2008, 2019, 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 "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 InterpreterRuntime::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   _fp_slot = 0;
  47   _single_fpr_slot = 0;
  48 #endif
  49 }
  50 
  51 #ifdef SHARING_FAST_NATIVE_FINGERPRINTS
  52 // mapping from SignatureIterator param to (common) type of parsing
  53 static const u1 shared_type[] = {
  54   (u1) SignatureIterator::int_parm, // bool
  55   (u1) SignatureIterator::int_parm, // byte
  56   (u1) SignatureIterator::int_parm, // char
  57   (u1) SignatureIterator::int_parm, // short
  58   (u1) SignatureIterator::int_parm, // int
  59   (u1) SignatureIterator::long_parm, // long
  60 #ifndef __ABI_HARD__
  61   (u1) SignatureIterator::int_parm, // float, passed as int
  62   (u1) SignatureIterator::long_parm, // double, passed as long
  63 #else
  64   (u1) SignatureIterator::float_parm, // float
  65   (u1) SignatureIterator::double_parm, // double
  66 #endif
  67   (u1) SignatureIterator::obj_parm, // obj
  68   (u1) SignatureIterator::done_parm // done
  69 };
  70 
  71 uint64_t InterpreterRuntime::normalize_fast_native_fingerprint(uint64_t fingerprint) {
  72   if (fingerprint == UCONST64(-1)) {
  73     // special signature used when the argument list cannot be encoded in a 64 bits value
  74     return fingerprint;
  75   }
  76   int shift = SignatureIterator::static_feature_size;
  77   uint64_t result = fingerprint & ((1 << shift) - 1);
  78   fingerprint >>= shift;
  79 
  80   BasicType ret_type = (BasicType) (fingerprint & SignatureIterator::result_feature_mask);
  81   // For ARM, the fast signature handler only needs to know whether
  82   // the return value must be unboxed. T_OBJECT and T_ARRAY need not
  83   // be distinguished from each other and all other return values
  84   // behave like integers with respect to the handler except T_BOOLEAN
  85   // which must be mapped to the range 0..1.
  86   bool unbox = (ret_type == T_OBJECT) || (ret_type == T_ARRAY);
  87   if (unbox) {
  88     ret_type = T_OBJECT;
  89   } else if (ret_type != T_BOOLEAN) {
  90     ret_type = T_INT;
  91   }
  92   result |= ((uint64_t) ret_type) << shift;
  93   shift += SignatureIterator::result_feature_size;
  94   fingerprint >>= SignatureIterator::result_feature_size;
  95 
  96   while (true) {
  97     uint32_t type = (uint32_t) (fingerprint & SignatureIterator::parameter_feature_mask);
  98     if (type == SignatureIterator::done_parm) {
  99       result |= ((uint64_t) SignatureIterator::done_parm) << shift;
 100       return result;
 101     }
 102     assert((type >= SignatureIterator::bool_parm) && (type <= SignatureIterator::obj_parm), "check fingerprint encoding");
 103     int shared = shared_type[type - SignatureIterator::bool_parm];
 104     result |= ((uint64_t) shared) << shift;
 105     shift += SignatureIterator::parameter_feature_size;
 106     fingerprint >>= SignatureIterator::parameter_feature_size;
 107   }
 108 }
 109 #endif // SHARING_FAST_NATIVE_FINGERPRINTS
 110 
 111 // Implementation of SignatureHandlerGenerator
 112 void InterpreterRuntime::SignatureHandlerGenerator::pass_int() {
 113   if (_ireg < GPR_PARAMS) {
 114     Register dst = as_Register(_ireg);
 115     __ ldr_s32(dst, Address(Rlocals, Interpreter::local_offset_in_bytes(offset())));
 116     _ireg++;
 117   } else {
 118     __ ldr_s32(Rtemp, Address(Rlocals, Interpreter::local_offset_in_bytes(offset())));
 119     __ str_32(Rtemp, Address(SP, _abi_offset * wordSize));
 120     _abi_offset++;
 121   }
 122 }
 123 
 124 void InterpreterRuntime::SignatureHandlerGenerator::pass_long() {
 125   if (_ireg <= 2) {
 126 #if (ALIGN_WIDE_ARGUMENTS == 1)
 127     if ((_ireg & 1) != 0) {
 128       // 64-bit values should be 8-byte aligned
 129       _ireg++;
 130     }
 131 #endif
 132     Register dst1 = as_Register(_ireg);
 133     Register dst2 = as_Register(_ireg+1);
 134     __ ldr(dst1, Address(Rlocals, Interpreter::local_offset_in_bytes(offset()+1)));
 135     __ ldr(dst2, Address(Rlocals, Interpreter::local_offset_in_bytes(offset())));
 136     _ireg += 2;
 137 #if (ALIGN_WIDE_ARGUMENTS == 0)
 138   } else if (_ireg == 3) {
 139     // uses R3 + one stack slot
 140     Register dst1 = as_Register(_ireg);
 141     __ ldr(Rtemp, Address(Rlocals, Interpreter::local_offset_in_bytes(offset())));
 142     __ ldr(dst1, Address(Rlocals, Interpreter::local_offset_in_bytes(offset()+1)));
 143     __ str(Rtemp, Address(SP, _abi_offset * wordSize));
 144     _ireg += 1;
 145     _abi_offset += 1;
 146 #endif
 147   } else {
 148 #if (ALIGN_WIDE_ARGUMENTS == 1)
 149     if(_abi_offset & 1) _abi_offset++;
 150 #endif
 151     __ ldr(Rtemp, Address(Rlocals, Interpreter::local_offset_in_bytes(offset()+1)));
 152     __ str(Rtemp, Address(SP, (_abi_offset) * wordSize));
 153     __ ldr(Rtemp, Address(Rlocals, Interpreter::local_offset_in_bytes(offset())));
 154     __ str(Rtemp, Address(SP, (_abi_offset+1) * wordSize));
 155     _abi_offset += 2;
 156     _ireg = 4;
 157   }
 158 }
 159 
 160 void InterpreterRuntime::SignatureHandlerGenerator::pass_object() {
 161   if (_ireg < 4) {
 162     Register dst = as_Register(_ireg);
 163     __ ldr(dst, Address(Rlocals, Interpreter::local_offset_in_bytes(offset())));
 164     __ cmp(dst, 0);
 165     __ sub(dst, Rlocals, -Interpreter::local_offset_in_bytes(offset()), ne);
 166     _ireg++;
 167   } else {
 168     __ ldr(Rtemp, Address(Rlocals, Interpreter::local_offset_in_bytes(offset())));
 169     __ cmp(Rtemp, 0);
 170     __ sub(Rtemp, Rlocals, -Interpreter::local_offset_in_bytes(offset()), ne);
 171     __ str(Rtemp, Address(SP, _abi_offset * wordSize));
 172     _abi_offset++;
 173   }
 174 }
 175 
 176 #ifndef __ABI_HARD__
 177 void InterpreterRuntime::SignatureHandlerGenerator::pass_float() {
 178   if (_ireg < 4) {
 179     Register dst = as_Register(_ireg);
 180     __ ldr(dst, Address(Rlocals, Interpreter::local_offset_in_bytes(offset())));
 181     _ireg++;
 182   } else {
 183     __ ldr(Rtemp, Address(Rlocals, Interpreter::local_offset_in_bytes(offset())));
 184     __ str(Rtemp, Address(SP, _abi_offset * wordSize));
 185     _abi_offset++;
 186   }
 187 }
 188 
 189 #else
 190 #ifndef __SOFTFP__
 191 void InterpreterRuntime::SignatureHandlerGenerator::pass_float() {
 192     if((_fp_slot < 16) || (_single_fpr_slot & 1)) {
 193       if ((_single_fpr_slot & 1) == 0) {
 194         _single_fpr_slot = _fp_slot;
 195         _fp_slot += 2;
 196       }
 197       __ flds(as_FloatRegister(_single_fpr_slot), Address(Rlocals, Interpreter::local_offset_in_bytes(offset())));
 198       _single_fpr_slot++;
 199     } else {
 200       __ ldr(Rtemp, Address(Rlocals, Interpreter::local_offset_in_bytes(offset())));
 201       __ str(Rtemp, Address(SP, _abi_offset * wordSize));
 202       _abi_offset++;
 203     }
 204 }
 205 
 206 void InterpreterRuntime::SignatureHandlerGenerator::pass_double() {
 207     if(_fp_slot <= 14) {
 208       __ fldd(as_FloatRegister(_fp_slot), Address(Rlocals, Interpreter::local_offset_in_bytes(offset()+1)));
 209       _fp_slot += 2;
 210     } else {
 211       __ ldr(Rtemp, Address(Rlocals, Interpreter::local_offset_in_bytes(offset()+1)));
 212       __ str(Rtemp, Address(SP, (_abi_offset) * wordSize));
 213       __ ldr(Rtemp, Address(Rlocals, Interpreter::local_offset_in_bytes(offset())));
 214       __ str(Rtemp, Address(SP, (_abi_offset+1) * wordSize));
 215       _abi_offset += 2;
 216       _single_fpr_slot = 16;
 217     }
 218 }
 219 #endif // __SOFTFP__
 220 #endif // __ABI_HARD__
 221 
 222 void InterpreterRuntime::SignatureHandlerGenerator::generate(uint64_t fingerprint) {
 223   iterate(fingerprint);
 224 
 225   BasicType result_type = SignatureIterator::return_type(fingerprint);
 226 
 227   address result_handler = Interpreter::result_handler(result_type);
 228 
 229   __ mov_slow(R0, (intptr_t)result_handler);
 230 
 231   __ ret();
 232 }
 233 
 234 
 235 // Implementation of SignatureHandlerLibrary
 236 
 237 void SignatureHandlerLibrary::pd_set_handler(address handler) {}
 238 
 239 class SlowSignatureHandler: public NativeSignatureIterator {
 240  private:
 241   address   _from;
 242   intptr_t* _to;
 243 
 244 #ifndef __ABI_HARD__
 245   virtual void pass_int() {
 246     *_to++ = *(jint *)(_from+Interpreter::local_offset_in_bytes(0));
 247     _from -= Interpreter::stackElementSize;
 248   }
 249 
 250   virtual void pass_float() {
 251     *_to++ = *(jint *)(_from+Interpreter::local_offset_in_bytes(0));
 252     _from -= Interpreter::stackElementSize;
 253   }
 254 
 255   virtual void pass_long() {
 256 #if (ALIGN_WIDE_ARGUMENTS == 1)
 257     if (((intptr_t)_to & 7) != 0) {
 258       // 64-bit values should be 8-byte aligned
 259       _to++;
 260     }
 261 #endif
 262     _to[0] = *(intptr_t*)(_from+Interpreter::local_offset_in_bytes(1));
 263     _to[1] = *(intptr_t*)(_from+Interpreter::local_offset_in_bytes(0));
 264     _to += 2;
 265     _from -= 2*Interpreter::stackElementSize;
 266   }
 267 
 268   virtual void pass_object() {
 269     intptr_t from_addr = (intptr_t)(_from + Interpreter::local_offset_in_bytes(0));
 270     *_to++ = (*(intptr_t*)from_addr == 0) ? (intptr_t)NULL : from_addr;
 271     _from -= Interpreter::stackElementSize;
 272    }
 273 
 274 #else
 275 
 276   intptr_t* _toFP;
 277   intptr_t* _toGP;
 278   int       _last_gp;
 279   int       _last_fp;
 280   int       _last_single_fp;
 281 
 282   virtual void pass_int() {
 283     if(_last_gp < GPR_PARAMS) {
 284       _toGP[_last_gp++] = *(jint *)(_from+Interpreter::local_offset_in_bytes(0));
 285     } else {
 286       *_to++ = *(jint *)(_from+Interpreter::local_offset_in_bytes(0));
 287     }
 288     _from -= Interpreter::stackElementSize;
 289   }
 290 
 291   virtual void pass_long() {
 292     assert(ALIGN_WIDE_ARGUMENTS == 1, "ABI_HARD not supported with unaligned wide arguments");
 293     if (_last_gp <= 2) {
 294       if(_last_gp & 1) _last_gp++;
 295       _toGP[_last_gp++] = *(jint *)(_from+Interpreter::local_offset_in_bytes(1));
 296       _toGP[_last_gp++] = *(jint *)(_from+Interpreter::local_offset_in_bytes(0));
 297     } else {
 298       if (((intptr_t)_to & 7) != 0) {
 299         // 64-bit values should be 8-byte aligned
 300         _to++;
 301       }
 302       _to[0] = *(intptr_t*)(_from+Interpreter::local_offset_in_bytes(1));
 303       _to[1] = *(intptr_t*)(_from+Interpreter::local_offset_in_bytes(0));
 304       _to += 2;
 305       _last_gp = 4;
 306     }
 307     _from -= 2*Interpreter::stackElementSize;
 308   }
 309 
 310   virtual void pass_object() {
 311     intptr_t from_addr = (intptr_t)(_from + Interpreter::local_offset_in_bytes(0));
 312     if(_last_gp < GPR_PARAMS) {
 313       _toGP[_last_gp++] = (*(intptr_t*)from_addr == 0) ? NULL : from_addr;
 314     } else {
 315       *_to++ = (*(intptr_t*)from_addr == 0) ? NULL : from_addr;
 316     }
 317     _from -= Interpreter::stackElementSize;
 318   }
 319 
 320   virtual void pass_float() {
 321     if((_last_fp < 16) || (_last_single_fp & 1)) {
 322       if ((_last_single_fp & 1) == 0) {
 323         _last_single_fp = _last_fp;
 324         _last_fp += 2;
 325       }
 326 
 327       _toFP[_last_single_fp++] = *(jint *)(_from+Interpreter::local_offset_in_bytes(0));
 328     } else {
 329       *_to++ = *(jint *)(_from+Interpreter::local_offset_in_bytes(0));
 330     }
 331     _from -= Interpreter::stackElementSize;
 332   }
 333 
 334   virtual void pass_double() {
 335     assert(ALIGN_WIDE_ARGUMENTS == 1, "ABI_HARD not supported with unaligned wide arguments");
 336     if(_last_fp <= 14) {
 337       _toFP[_last_fp++] = *(intptr_t*)(_from+Interpreter::local_offset_in_bytes(1));
 338       _toFP[_last_fp++] = *(intptr_t*)(_from+Interpreter::local_offset_in_bytes(0));
 339     } else {
 340       if (((intptr_t)_to & 7) != 0) {      // 64-bit values should be 8-byte aligned
 341         _to++;
 342       }
 343       _to[0] = *(intptr_t*)(_from+Interpreter::local_offset_in_bytes(1));
 344       _to[1] = *(intptr_t*)(_from+Interpreter::local_offset_in_bytes(0));
 345       _to += 2;
 346       _last_single_fp = 16;
 347     }
 348     _from -= 2*Interpreter::stackElementSize;
 349   }
 350 
 351 #endif // !__ABI_HARD__
 352 
 353  public:
 354   SlowSignatureHandler(const methodHandle& method, address from, intptr_t* to) :
 355     NativeSignatureIterator(method) {
 356     _from = from;
 357 
 358 #ifdef __ABI_HARD__
 359     _toGP  = to;
 360     _toFP = _toGP + GPR_PARAMS;
 361     _to   = _toFP + (8*2);
 362     _last_gp = (is_static() ? 2 : 1);
 363     _last_fp = 0;
 364     _last_single_fp = 0;
 365 #else
 366     _to   = to + (is_static() ? 2 : 1);
 367 #endif // __ABI_HARD__
 368   }
 369 };
 370 
 371 JRT_ENTRY(address, InterpreterRuntime::slow_signature_handler(JavaThread* thread, Method* method, intptr_t* from, intptr_t* to))
 372   methodHandle m(thread, (Method*)method);
 373   assert(m->is_native(), "sanity check");
 374   SlowSignatureHandler(m, (address)from, to).iterate(UCONST64(-1));
 375   return Interpreter::result_handler(m->result_type());
 376 JRT_END