1 /*
  2  * Copyright (c) 1998, 2021, 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 "oops/method.hpp"
 31 #include "oops/oop.inline.hpp"
 32 #include "runtime/handles.inline.hpp"
 33 #include "runtime/icache.hpp"
 34 #include "runtime/interfaceSupport.inline.hpp"
 35 #include "runtime/signature.hpp"
 36 
 37 
 38 #define __ _masm->
 39 
 40 
 41 // Implementation of SignatureHandlerGenerator
 42 InterpreterRuntime::SignatureHandlerGenerator::SignatureHandlerGenerator(const methodHandle& method, CodeBuffer* buffer) :
 43     NativeSignatureIterator(method) {
 44   _masm = new MacroAssembler(buffer);
 45 }
 46 
 47 void InterpreterRuntime::SignatureHandlerGenerator::pass_int() {
 48   move(offset(), jni_offset() + 1);
 49 }
 50 
 51 void InterpreterRuntime::SignatureHandlerGenerator::pass_float() {
 52   move(offset(), jni_offset() + 1);
 53 }
 54 
 55 void InterpreterRuntime::SignatureHandlerGenerator::pass_long() {
 56    move(offset(), jni_offset() + 2);
 57    move(offset() + 1, jni_offset() + 1);
 58 }
 59 
 60 void InterpreterRuntime::SignatureHandlerGenerator::pass_object() {
 61   box (offset(), jni_offset() + 1);
 62 }
 63 
 64 void InterpreterRuntime::SignatureHandlerGenerator::move(int from_offset, int to_offset) {
 65   __ movl(temp(), Address(from(), Interpreter::local_offset_in_bytes(from_offset)));
 66   __ movl(Address(to(), to_offset * wordSize), temp());
 67 }
 68 
 69 
 70 void InterpreterRuntime::SignatureHandlerGenerator::box(int from_offset, int to_offset) {
 71   __ lea(temp(), Address(from(), Interpreter::local_offset_in_bytes(from_offset)));
 72   __ cmpptr(Address(from(), Interpreter::local_offset_in_bytes(from_offset)), NULL_WORD); // do not use temp() to avoid AGI
 73   Label L;
 74   __ jcc(Assembler::notZero, L);
 75   __ movptr(temp(), NULL_WORD);
 76   __ bind(L);
 77   __ movptr(Address(to(), to_offset * wordSize), temp());
 78 }
 79 
 80 
 81 void InterpreterRuntime::SignatureHandlerGenerator::generate( uint64_t fingerprint) {
 82   // generate code to handle arguments
 83   iterate(fingerprint);
 84   // return result handler
 85   __ lea(rax,
 86          ExternalAddress((address)Interpreter::result_handler(method()->result_type())));
 87   // return
 88   __ ret(0);
 89   __ flush();
 90 }
 91 
 92 
 93 Register InterpreterRuntime::SignatureHandlerGenerator::from()       { return rdi; }
 94 Register InterpreterRuntime::SignatureHandlerGenerator::to()         { return rsp; }
 95 Register InterpreterRuntime::SignatureHandlerGenerator::temp()       { return rcx; }
 96 
 97 
 98 // Implementation of SignatureHandlerLibrary
 99 
100 void SignatureHandlerLibrary::pd_set_handler(address handler) {}
101 
102 class SlowSignatureHandler: public NativeSignatureIterator {
103  private:
104   address   _from;
105   intptr_t* _to;
106 
107   virtual void pass_int() {
108     *_to++ = *(jint *)(_from+Interpreter::local_offset_in_bytes(0));
109     _from -= Interpreter::stackElementSize;
110   }
111 
112   virtual void pass_float() {
113     *_to++ = *(jint *)(_from+Interpreter::local_offset_in_bytes(0));
114     _from -= Interpreter::stackElementSize;
115   }
116 
117   virtual void pass_long() {
118     _to[0] = *(intptr_t*)(_from+Interpreter::local_offset_in_bytes(1));
119     _to[1] = *(intptr_t*)(_from+Interpreter::local_offset_in_bytes(0));
120     _to += 2;
121     _from -= 2*Interpreter::stackElementSize;
122   }
123 
124   virtual void pass_object() {
125     // pass address of from
126     intptr_t from_addr = (intptr_t)(_from + Interpreter::local_offset_in_bytes(0));
127     *_to++ = (*(intptr_t*)from_addr == 0) ? NULL_WORD : from_addr;
128     _from -= Interpreter::stackElementSize;
129    }
130 
131  public:
132   SlowSignatureHandler(const methodHandle& method, address from, intptr_t* to) :
133     NativeSignatureIterator(method) {
134     _from = from;
135     _to   = to + (is_static() ? 2 : 1);
136   }
137 };
138 
139 JRT_ENTRY(address, InterpreterRuntime::slow_signature_handler(JavaThread* current, Method* method, intptr_t* from, intptr_t* to))
140   methodHandle m(current, (Method*)method);
141   assert(m->is_native(), "sanity check");
142   // handle arguments
143   SlowSignatureHandler(m, (address)from, to + 1).iterate((uint64_t)CONST64(-1));
144   // return result handler
145   return Interpreter::result_handler(m->result_type());
146 JRT_END