1/*
2 * Copyright (c) 1998, 2017, 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/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
39#define __ _masm->
40
41
42// Implementation of SignatureHandlerGenerator
43
44void InterpreterRuntime::SignatureHandlerGenerator::pass_word(int size_of_arg, int offset_in_arg) {
45  Argument  jni_arg(jni_offset() + offset_in_arg, false);
46  Register     Rtmp = O0;
47  __ ld(Llocals, Interpreter::local_offset_in_bytes(offset()), Rtmp);
48
49  __ store_argument(Rtmp, jni_arg);
50}
51
52void InterpreterRuntime::SignatureHandlerGenerator::pass_long() {
53  Argument  jni_arg(jni_offset(), false);
54  Register  Rtmp = O0;
55
56  __ ldx(Llocals, Interpreter::local_offset_in_bytes(offset() + 1), Rtmp);
57  __ store_long_argument(Rtmp, jni_arg);
58}
59
60
61void InterpreterRuntime::SignatureHandlerGenerator::pass_float() {
62  Argument  jni_arg(jni_offset(), false);
63  FloatRegister  Rtmp = F0;
64  __ ldf(FloatRegisterImpl::S, Llocals, Interpreter::local_offset_in_bytes(offset()), Rtmp);
65  __ store_float_argument(Rtmp, jni_arg);
66}
67
68
69void InterpreterRuntime::SignatureHandlerGenerator::pass_double() {
70  Argument  jni_arg(jni_offset(), false);
71  FloatRegister  Rtmp = F0;
72  __ ldf(FloatRegisterImpl::D, Llocals, Interpreter::local_offset_in_bytes(offset() + 1), Rtmp);
73  __ store_double_argument(Rtmp, jni_arg);
74}
75
76void InterpreterRuntime::SignatureHandlerGenerator::pass_object() {
77  Argument  jni_arg(jni_offset(), false);
78  Argument java_arg(    offset(), true);
79  Register    Rtmp1 = O0;
80  Register    Rtmp2 =  jni_arg.is_register() ?  jni_arg.as_register() : O0;
81  Register    Rtmp3 =  G3_scratch;
82
83  // the handle for a receiver will never be null
84  bool do_NULL_check = offset() != 0 || is_static();
85
86  Address     h_arg = Address(Llocals, Interpreter::local_offset_in_bytes(offset()));
87  __ ld_ptr(h_arg, Rtmp1);
88  if (!do_NULL_check) {
89    __ add(h_arg.base(), h_arg.disp(), Rtmp2);
90  } else {
91    if (Rtmp1 == Rtmp2)
92          __ tst(Rtmp1);
93    else  __ addcc(G0, Rtmp1, Rtmp2); // optimize mov/test pair
94    Label L;
95    __ brx(Assembler::notZero, true, Assembler::pt, L);
96    __ delayed()->add(h_arg.base(), h_arg.disp(), Rtmp2);
97    __ bind(L);
98  }
99  __ store_ptr_argument(Rtmp2, jni_arg);    // this is often a no-op
100}
101
102
103void InterpreterRuntime::SignatureHandlerGenerator::generate(uint64_t fingerprint) {
104
105  // generate code to handle arguments
106  iterate(fingerprint);
107
108  // return result handler
109  AddressLiteral result_handler(Interpreter::result_handler(method()->result_type()));
110  __ sethi(result_handler, Lscratch);
111  __ retl();
112  __ delayed()->add(Lscratch, result_handler.low10(), Lscratch);
113
114  __ flush();
115}
116
117
118// Implementation of SignatureHandlerLibrary
119
120void SignatureHandlerLibrary::pd_set_handler(address handler) {}
121
122
123class SlowSignatureHandler: public NativeSignatureIterator {
124 private:
125  address   _from;
126  intptr_t* _to;
127  intptr_t* _RegArgSignature;                   // Signature of first Arguments to be passed in Registers
128  uint      _argcount;
129
130  enum {                                        // We need to differenciate float from non floats in reg args
131    non_float  = 0,
132    float_sig  = 1,
133    double_sig = 2,
134    long_sig   = 3
135  };
136
137  virtual void pass_int() {
138    *_to++ = *(jint *)(_from+Interpreter::local_offset_in_bytes(0));
139    _from -= Interpreter::stackElementSize;
140    add_signature( non_float );
141  }
142
143  virtual void pass_object() {
144    // pass address of from
145    intptr_t *from_addr = (intptr_t*)(_from + Interpreter::local_offset_in_bytes(0));
146    *_to++ = (*from_addr == 0) ? NULL : (intptr_t) from_addr;
147    _from -= Interpreter::stackElementSize;
148    add_signature( non_float );
149   }
150
151  virtual void pass_float()  {
152    *_to++ = *(jint *)(_from+Interpreter::local_offset_in_bytes(0));
153    _from -= Interpreter::stackElementSize;
154    add_signature( float_sig );
155   }
156
157  virtual void pass_double() {
158    *_to++ = *(intptr_t*)(_from+Interpreter::local_offset_in_bytes(1));
159    _from -= 2*Interpreter::stackElementSize;
160   add_signature( double_sig );
161   }
162
163  virtual void pass_long() {
164    _to[0] = *(intptr_t*)(_from+Interpreter::local_offset_in_bytes(1));
165    _to += 1;
166    _from -= 2*Interpreter::stackElementSize;
167    add_signature( long_sig );
168  }
169
170  virtual void add_signature( intptr_t sig_type ) {
171    if ( _argcount < (sizeof (intptr_t))*4 ) {
172      *_RegArgSignature |= (sig_type << (_argcount*2) );
173      _argcount++;
174    }
175  }
176
177
178 public:
179  SlowSignatureHandler(const methodHandle& method, address from, intptr_t* to, intptr_t *RegArgSig) : NativeSignatureIterator(method) {
180    _from = from;
181    _to   = to;
182    _RegArgSignature = RegArgSig;
183    *_RegArgSignature = 0;
184    _argcount = method->is_static() ? 2 : 1;
185  }
186};
187
188
189IRT_ENTRY(address, InterpreterRuntime::slow_signature_handler(
190                                                    JavaThread* thread,
191                                                    Method* method,
192                                                    intptr_t* from,
193                                                    intptr_t* to ))
194  methodHandle m(thread, method);
195  assert(m->is_native(), "sanity check");
196  // handle arguments
197  // Warning: We use reg arg slot 00 temporarily to return the RegArgSignature
198  // back to the code that pops the arguments into the CPU registers
199  SlowSignatureHandler(m, (address)from, m->is_static() ? to+2 : to+1, to).iterate((uint64_t)CONST64(-1));
200  // return result handler
201  return Interpreter::result_handler(m->result_type());
202IRT_END
203