comparison graal/com.oracle.jvmci.hotspot/src/com/oracle/jvmci/hotspot/HotSpotMethodHandleAccessProvider.java @ 21551:5324104ac4f3

moved com.oracle.graal.hotspot.jvmci classes to com.oracle.jvmci.hotspot module (JBS:GRAAL-53)
author Doug Simon <doug.simon@oracle.com>
date Tue, 26 May 2015 17:13:37 +0200
parents graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/HotSpotMethodHandleAccessProvider.java@93c50cefb9e8
children
comparison
equal deleted inserted replaced
21550:f48a6cea31eb 21551:5324104ac4f3
1 /*
2 * Copyright (c) 2014, 2014, 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 package com.oracle.jvmci.hotspot;
24
25 import static com.oracle.jvmci.hotspot.HotSpotJVMCIRuntime.*;
26 import static com.oracle.jvmci.hotspot.HotSpotResolvedJavaType.*;
27 import static com.oracle.jvmci.hotspot.HotSpotResolvedObjectTypeImpl.*;
28
29 import com.oracle.graal.api.meta.*;
30 import com.oracle.jvmci.common.*;
31
32 public class HotSpotMethodHandleAccessProvider implements MethodHandleAccessProvider, HotSpotProxified {
33
34 private final ConstantReflectionProvider constantReflection;
35
36 public HotSpotMethodHandleAccessProvider(ConstantReflectionProvider constantReflection) {
37 this.constantReflection = constantReflection;
38 }
39
40 /**
41 * Lazy initialization to break class initialization cycle. Field and method lookup is only
42 * possible after the {@link HotSpotJVMCIRuntime} is fully initialized.
43 */
44 static class LazyInitialization {
45 static final ResolvedJavaField methodHandleFormField;
46 static final ResolvedJavaField lambdaFormVmentryField;
47 static final ResolvedJavaMethod lambdaFormCompileToBytecodeMethod;
48 static final ResolvedJavaField memberNameVmtargetField;
49
50 /**
51 * Search for an instance field with the given name in a class.
52 *
53 * @param className name of the class to search in
54 * @param fieldName name of the field to be searched
55 * @return resolved java field
56 * @throws ClassNotFoundException
57 */
58 private static ResolvedJavaField findFieldInClass(String className, String fieldName) throws ClassNotFoundException {
59 Class<?> clazz = Class.forName(className);
60 ResolvedJavaType type = fromClass(clazz);
61 ResolvedJavaField[] fields = type.getInstanceFields(false);
62 for (ResolvedJavaField field : fields) {
63 if (field.getName().equals(fieldName)) {
64 return field;
65 }
66 }
67 return null;
68 }
69
70 private static ResolvedJavaMethod findMethodInClass(String className, String methodName) throws ClassNotFoundException {
71 Class<?> clazz = Class.forName(className);
72 HotSpotResolvedObjectTypeImpl type = fromObjectClass(clazz);
73 ResolvedJavaMethod result = null;
74 for (ResolvedJavaMethod method : type.getDeclaredMethods()) {
75 if (method.getName().equals(methodName)) {
76 assert result == null : "more than one method found: " + className + "." + methodName;
77 result = method;
78 }
79 }
80 assert result != null : "method not found: " + className + "." + methodName;
81 return result;
82 }
83
84 static {
85 try {
86 methodHandleFormField = findFieldInClass("java.lang.invoke.MethodHandle", "form");
87 lambdaFormVmentryField = findFieldInClass("java.lang.invoke.LambdaForm", "vmentry");
88 lambdaFormCompileToBytecodeMethod = findMethodInClass("java.lang.invoke.LambdaForm", "compileToBytecode");
89 memberNameVmtargetField = findFieldInClass("java.lang.invoke.MemberName", "vmtarget");
90 } catch (Throwable ex) {
91 throw new JVMCIError(ex);
92 }
93 }
94 }
95
96 @Override
97 public IntrinsicMethod lookupMethodHandleIntrinsic(ResolvedJavaMethod method) {
98 int intrinsicId = ((HotSpotResolvedJavaMethodImpl) method).intrinsicId();
99 if (intrinsicId != 0) {
100 return getMethodHandleIntrinsic(intrinsicId);
101 }
102 return null;
103 }
104
105 public static IntrinsicMethod getMethodHandleIntrinsic(int intrinsicId) {
106 HotSpotVMConfig config = runtime().getConfig();
107 if (intrinsicId == config.vmIntrinsicInvokeBasic) {
108 return IntrinsicMethod.INVOKE_BASIC;
109 } else if (intrinsicId == config.vmIntrinsicLinkToInterface) {
110 return IntrinsicMethod.LINK_TO_INTERFACE;
111 } else if (intrinsicId == config.vmIntrinsicLinkToSpecial) {
112 return IntrinsicMethod.LINK_TO_SPECIAL;
113 } else if (intrinsicId == config.vmIntrinsicLinkToStatic) {
114 return IntrinsicMethod.LINK_TO_STATIC;
115 } else if (intrinsicId == config.vmIntrinsicLinkToVirtual) {
116 return IntrinsicMethod.LINK_TO_VIRTUAL;
117 }
118 return null;
119 }
120
121 @Override
122 public ResolvedJavaMethod resolveInvokeBasicTarget(JavaConstant methodHandle, boolean forceBytecodeGeneration) {
123 if (methodHandle.isNull()) {
124 return null;
125 }
126
127 /* Load non-public field: LambdaForm MethodHandle.form */
128 JavaConstant lambdaForm = constantReflection.readFieldValue(LazyInitialization.methodHandleFormField, methodHandle);
129 if (lambdaForm.isNull()) {
130 return null;
131 }
132
133 JavaConstant memberName;
134 if (forceBytecodeGeneration) {
135 /* Invoke non-public method: MemberName LambdaForm.compileToBytecode() */
136 memberName = LazyInitialization.lambdaFormCompileToBytecodeMethod.invoke(lambdaForm, new JavaConstant[0]);
137 } else {
138 /* Load non-public field: MemberName LambdaForm.vmentry */
139 memberName = constantReflection.readFieldValue(LazyInitialization.lambdaFormVmentryField, lambdaForm);
140 }
141 return getTargetMethod(memberName);
142 }
143
144 @Override
145 public ResolvedJavaMethod resolveLinkToTarget(JavaConstant memberName) {
146 return getTargetMethod(memberName);
147 }
148
149 /**
150 * Returns the {@link ResolvedJavaMethod} for the vmtarget of a java.lang.invoke.MemberName.
151 */
152 private ResolvedJavaMethod getTargetMethod(JavaConstant memberName) {
153 if (memberName.isNull()) {
154 return null;
155 }
156
157 /* Load injected field: JVM_Method* MemberName.vmtarget */
158 JavaConstant vmtarget = constantReflection.readFieldValue(LazyInitialization.memberNameVmtargetField, memberName);
159 /* Create a method from the vmtarget method pointer. */
160 return HotSpotResolvedJavaMethodImpl.fromMetaspace(vmtarget.asLong());
161 }
162 }