1 /*
2  * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
3  * Copyright (c) 2012, 2013 SAP SE. All rights reserved.
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This code is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License version 2 only, as
8  * published by the Free Software Foundation.
9  *
10  * This code is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13  * version 2 for more details (a copy is included in the LICENSE file that
14  * accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License version
17  * 2 along with this work; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19  *
20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21  * or visit www.oracle.com if you need additional information or have any
22  * questions.
23  *
24  */
25 
26 #include "precompiled.hpp"
27 #include "asm/assembler.inline.hpp"
28 #include "interpreter/interp_masm.hpp"
29 #include "interpreter/interpreter.hpp"
30 #include "interpreter/interpreterRuntime.hpp"
31 #include "memory/allocation.inline.hpp"
32 #include "memory/universe.hpp"
33 #include "oops/method.hpp"
34 #include "oops/oop.inline.hpp"
35 #include "runtime/handles.inline.hpp"
36 #include "runtime/icache.hpp"
37 #include "runtime/interfaceSupport.inline.hpp"
38 #include "runtime/signature.hpp"
39 
40 #define __ _masm->
41 
42 // Access macros for Java and C arguments.
43 // The first Java argument is at index -1.
44 #define locals_j_arg_at(index)    (Interpreter::local_offset_in_bytes(index)), R18_locals
45 // The first C argument is at index 0.
46 #define sp_c_arg_at(index)        ((index)*wordSize + _abi(carg_1)), R1_SP
47 
48 // Implementation of SignatureHandlerGenerator
49 
SignatureHandlerGenerator(const methodHandle & method,CodeBuffer * buffer)50 InterpreterRuntime::SignatureHandlerGenerator::SignatureHandlerGenerator(
51     const methodHandle& method, CodeBuffer* buffer) : NativeSignatureIterator(method) {
52   _masm = new MacroAssembler(buffer);
53   _num_used_fp_arg_regs = 0;
54 }
55 
pass_int()56 void InterpreterRuntime::SignatureHandlerGenerator::pass_int() {
57   Argument jni_arg(jni_offset());
58   Register r = jni_arg.is_register() ? jni_arg.as_register() : R0;
59 
60   __ lwa(r, locals_j_arg_at(offset())); // sign extension of integer
61   if (DEBUG_ONLY(true ||) !jni_arg.is_register()) {
62     __ std(r, sp_c_arg_at(jni_arg.number()));
63   }
64 }
65 
pass_long()66 void InterpreterRuntime::SignatureHandlerGenerator::pass_long() {
67   Argument jni_arg(jni_offset());
68   Register r = jni_arg.is_register() ? jni_arg.as_register() : R0;
69 
70   __ ld(r, locals_j_arg_at(offset()+1)); // long resides in upper slot
71   if (DEBUG_ONLY(true ||) !jni_arg.is_register()) {
72     __ std(r, sp_c_arg_at(jni_arg.number()));
73   }
74 }
75 
pass_float()76 void InterpreterRuntime::SignatureHandlerGenerator::pass_float() {
77   FloatRegister fp_reg = (_num_used_fp_arg_regs < 13/*max_fp_register_arguments*/)
78                          ? as_FloatRegister((_num_used_fp_arg_regs++) + F1_ARG1->encoding())
79                          : F0;
80 
81   __ lfs(fp_reg, locals_j_arg_at(offset()));
82   if (DEBUG_ONLY(true ||) jni_offset() > 8) {
83     __ stfs(fp_reg, sp_c_arg_at(jni_offset()));
84   }
85 }
86 
pass_double()87 void InterpreterRuntime::SignatureHandlerGenerator::pass_double() {
88   FloatRegister fp_reg = (_num_used_fp_arg_regs < 13/*max_fp_register_arguments*/)
89                          ? as_FloatRegister((_num_used_fp_arg_regs++) + F1_ARG1->encoding())
90                          : F0;
91 
92   __ lfd(fp_reg, locals_j_arg_at(offset()+1));
93   if (DEBUG_ONLY(true ||) jni_offset() > 8) {
94     __ stfd(fp_reg, sp_c_arg_at(jni_offset()));
95   }
96 }
97 
pass_object()98 void InterpreterRuntime::SignatureHandlerGenerator::pass_object() {
99   Argument jni_arg(jni_offset());
100   Register r = jni_arg.is_register() ? jni_arg.as_register() : R11_scratch1;
101 
102   // The handle for a receiver will never be null.
103   bool do_NULL_check = offset() != 0 || is_static();
104 
105   Label do_null;
106   if (do_NULL_check) {
107     __ ld(R0, locals_j_arg_at(offset()));
108     __ cmpdi(CCR0, R0, 0);
109     __ li(r, 0);
110     __ beq(CCR0, do_null);
111   }
112   __ addir(r, locals_j_arg_at(offset()));
113   __ bind(do_null);
114   if (DEBUG_ONLY(true ||) !jni_arg.is_register()) {
115     __ std(r, sp_c_arg_at(jni_arg.number()));
116   }
117 }
118 
generate(uint64_t fingerprint)119 void InterpreterRuntime::SignatureHandlerGenerator::generate(uint64_t fingerprint) {
120 #if !defined(ABI_ELFv2)
121   // Emit fd for current codebuffer. Needs patching!
122   __ emit_fd();
123 #endif
124 
125   // Generate code to handle arguments.
126   iterate(fingerprint);
127 
128   // Return the result handler.
129   __ load_const(R3_RET, AbstractInterpreter::result_handler(method()->result_type()));
130   __ blr();
131 
132   __ flush();
133 }
134 
135 #undef __
136 
137 // Implementation of SignatureHandlerLibrary
138 
pd_set_handler(address handler)139 void SignatureHandlerLibrary::pd_set_handler(address handler) {
140 #if !defined(ABI_ELFv2)
141   // patch fd here.
142   FunctionDescriptor* fd = (FunctionDescriptor*) handler;
143 
144   fd->set_entry(handler + (int)sizeof(FunctionDescriptor));
145   assert(fd->toc() == (address)0xcafe, "need to adjust TOC here");
146 #endif
147 }
148 
149 
150 // Access function to get the signature.
151 IRT_ENTRY(address, InterpreterRuntime::get_signature(JavaThread* thread, Method* method))
152   methodHandle m(thread, method);
153   assert(m->is_native(), "sanity check");
154   Symbol *s = m->signature();
155   return (address) s->base();
156 IRT_END
157 
158 IRT_ENTRY(address, InterpreterRuntime::get_result_handler(JavaThread* thread, Method* method))
159   methodHandle m(thread, method);
160   assert(m->is_native(), "sanity check");
161   return AbstractInterpreter::result_handler(m->result_type());
162 IRT_END
163