1 /*
 2  * Copyright (c) 2020, 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.  Oracle designates this
 8  * particular file as subject to the "Classpath" exception as provided
 9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package jdk.internal.invoke;
27 
28 import java.lang.invoke.MethodType;
29 import java.util.Objects;
30 
31 /**
32  * This class describes a native call, including arguments/return shuffle moves, PC entry point and
33  * various other info which are relevant when the call will be intrinsified by C2.
34  */
35 public class NativeEntryPoint {
36     static {
37         registerNatives();
38     }
39 
40     private final int shadowSpace;
41 
42     // encoded as VMRegImpl*
43     private final long[] argMoves;
44     private final long[] returnMoves;
45 
46     private final boolean needTransition;
47     private final MethodType methodType; // C2 sees erased version (byte -> int), so need this explicitly
48     private final String name;
49 
50     private NativeEntryPoint(int shadowSpace, long[] argMoves, long[] returnMoves,
51                      boolean needTransition, MethodType methodType, String name) {
52         this.shadowSpace = shadowSpace;
53         this.argMoves = Objects.requireNonNull(argMoves);
54         this.returnMoves = Objects.requireNonNull(returnMoves);
55         this.needTransition = needTransition;
56         this.methodType = methodType;
57         this.name = name;
58     }
59 
60     public static NativeEntryPoint make(String name, ABIDescriptorProxy abi,
61                                         VMStorageProxy[] argMoves, VMStorageProxy[] returnMoves,
62                                         boolean needTransition, MethodType methodType) {
63         if (returnMoves.length > 1) {
64             throw new IllegalArgumentException("Multiple register return not supported");
65         }
66 
67         return new NativeEntryPoint(abi.shadowSpaceBytes(), encodeVMStorages(argMoves), encodeVMStorages(returnMoves),
68                 needTransition, methodType, name);
69     }
70 
71     private static long[] encodeVMStorages(VMStorageProxy[] moves) {
72         long[] out = new long[moves.length];
73         for (int i = 0; i < moves.length; i++) {
74             out[i] = vmStorageToVMReg(moves[i].type(), moves[i].index());
75         }
76         return out;
77     }
78 
79     private static native long vmStorageToVMReg(int type, int index);
80 
81     public MethodType type() {
82         return methodType;
83     }
84 
85     private static native void registerNatives();
86 }