1 /*
 2  * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
 3  * Copyright (c) 2019, Arm Limited. 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 #include "precompiled.hpp"
26 #include "runtime/jniHandles.hpp"
27 #include "runtime/jniHandles.inline.hpp"
28 #include "oops/typeArrayOop.inline.hpp"
29 #include "prims/foreign_globals.hpp"
30 #include "prims/foreign_globals.inline.hpp"
31 
32 bool ABIDescriptor::is_volatile_reg(Register reg) const {
33   return _integer_argument_registers.contains(reg)
34     || _integer_additional_volatile_registers.contains(reg);
35 }
36 
37 bool ABIDescriptor::is_volatile_reg(FloatRegister reg) const {
38     return _vector_argument_registers.contains(reg)
39         || _vector_additional_volatile_registers.contains(reg);
40 }
41 
42 #define INTEGER_TYPE 0
43 #define VECTOR_TYPE 1
44 
45 const ABIDescriptor ForeignGlobals::parse_abi_descriptor_impl(jobject jabi) const {
46   oop abi_oop = JNIHandles::resolve_non_null(jabi);
47   ABIDescriptor abi;
48   const Register (*to_Register)(int) = as_Register;
49 
50   objArrayOop inputStorage = cast<objArrayOop>(abi_oop->obj_field(ABI.inputStorage_offset));
51   loadArray(inputStorage, INTEGER_TYPE, abi._integer_argument_registers, to_Register);
52   loadArray(inputStorage, VECTOR_TYPE, abi._vector_argument_registers, as_FloatRegister);
53 
54   objArrayOop outputStorage = cast<objArrayOop>(abi_oop->obj_field(ABI.outputStorage_offset));
55   loadArray(outputStorage, INTEGER_TYPE, abi._integer_return_registers, to_Register);
56   loadArray(outputStorage, VECTOR_TYPE, abi._vector_return_registers, as_FloatRegister);
57 
58   objArrayOop volatileStorage = cast<objArrayOop>(abi_oop->obj_field(ABI.volatileStorage_offset));
59   loadArray(volatileStorage, INTEGER_TYPE, abi._integer_additional_volatile_registers, to_Register);
60   loadArray(volatileStorage, VECTOR_TYPE, abi._vector_additional_volatile_registers, as_FloatRegister);
61 
62   abi._stack_alignment_bytes = abi_oop->int_field(ABI.stackAlignment_offset);
63   abi._shadow_space_bytes = abi_oop->int_field(ABI.shadowSpace_offset);
64 
65   return abi;
66 }
67 
68 const BufferLayout ForeignGlobals::parse_buffer_layout_impl(jobject jlayout) const {
69   oop layout_oop = JNIHandles::resolve_non_null(jlayout);
70   BufferLayout layout;
71 
72   layout.stack_args_bytes = layout_oop->long_field(BL.stack_args_bytes_offset);
73   layout.stack_args = layout_oop->long_field(BL.stack_args_offset);
74   layout.arguments_next_pc = layout_oop->long_field(BL.arguments_next_pc_offset);
75 
76   typeArrayOop input_offsets = cast<typeArrayOop>(layout_oop->obj_field(BL.input_type_offsets_offset));
77   layout.arguments_integer = (size_t) input_offsets->long_at(INTEGER_TYPE);
78   layout.arguments_vector = (size_t) input_offsets->long_at(VECTOR_TYPE);
79 
80   typeArrayOop output_offsets = cast<typeArrayOop>(layout_oop->obj_field(BL.output_type_offsets_offset));
81   layout.returns_integer = (size_t) output_offsets->long_at(INTEGER_TYPE);
82   layout.returns_vector = (size_t) output_offsets->long_at(VECTOR_TYPE);
83 
84   layout.buffer_size = layout_oop->long_field(BL.size_offset);
85 
86   return layout;
87 }
88 
89 const CallRegs ForeignGlobals::parse_call_regs_impl(jobject jconv) const {
90   ShouldNotCallThis();
91   return {};
92 }