|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, 2024, 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 | +package com.oracle.svm.hosted.lambda; |
| 26 | + |
| 27 | +import java.lang.reflect.Member; |
| 28 | +import java.util.Arrays; |
| 29 | +import java.util.stream.Stream; |
| 30 | + |
| 31 | +import com.oracle.graal.pointsto.phases.NoClassInitializationPlugin; |
| 32 | +import com.oracle.graal.pointsto.util.GraalAccess; |
| 33 | +import com.oracle.svm.util.ClassUtil; |
| 34 | + |
| 35 | +import jdk.graal.compiler.debug.DebugContext; |
| 36 | +import jdk.graal.compiler.java.BytecodeParser; |
| 37 | +import jdk.graal.compiler.java.GraphBuilderPhase; |
| 38 | +import jdk.graal.compiler.java.LambdaUtils; |
| 39 | +import jdk.graal.compiler.nodes.ConstantNode; |
| 40 | +import jdk.graal.compiler.nodes.StructuredGraph; |
| 41 | +import jdk.graal.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration; |
| 42 | +import jdk.graal.compiler.nodes.graphbuilderconf.IntrinsicContext; |
| 43 | +import jdk.graal.compiler.nodes.graphbuilderconf.InvocationPlugins; |
| 44 | +import jdk.graal.compiler.nodes.spi.CoreProviders; |
| 45 | +import jdk.graal.compiler.options.OptionValues; |
| 46 | +import jdk.graal.compiler.phases.OptimisticOptimizations; |
| 47 | +import jdk.graal.compiler.phases.tiers.HighTierContext; |
| 48 | +import jdk.graal.compiler.printer.GraalDebugHandlersFactory; |
| 49 | +import jdk.graal.compiler.replacements.MethodHandlePlugin; |
| 50 | +import jdk.vm.ci.meta.Constant; |
| 51 | +import jdk.vm.ci.meta.JavaConstant; |
| 52 | +import jdk.vm.ci.meta.ResolvedJavaField; |
| 53 | +import jdk.vm.ci.meta.ResolvedJavaMethod; |
| 54 | +import jdk.vm.ci.meta.ResolvedJavaType; |
| 55 | + |
| 56 | +public class LambdaParser { |
| 57 | + /** |
| 58 | + * Create a {@link StructuredGraph} using {@link LambdaGraphBuilderPhase.LambdaBytecodeParser}, |
| 59 | + * a simple {@link BytecodeParser}. |
| 60 | + */ |
| 61 | + @SuppressWarnings("try") |
| 62 | + public static StructuredGraph createMethodGraph(ResolvedJavaMethod method, OptionValues options) { |
| 63 | + GraphBuilderPhase lambdaParserPhase = new LambdaParser.LambdaGraphBuilderPhase(); |
| 64 | + DebugContext.Description description = new DebugContext.Description(method, ClassUtil.getUnqualifiedName(method.getClass()) + ":" + method.getName()); |
| 65 | + DebugContext debug = new DebugContext.Builder(options, new GraalDebugHandlersFactory(GraalAccess.getOriginalSnippetReflection())).description(description).build(); |
| 66 | + |
| 67 | + HighTierContext context = new HighTierContext(GraalAccess.getOriginalProviders(), null, OptimisticOptimizations.NONE); |
| 68 | + StructuredGraph graph = new StructuredGraph.Builder(debug.getOptions(), debug) |
| 69 | + .method(method) |
| 70 | + .recordInlinedMethods(false) |
| 71 | + .build(); |
| 72 | + try (DebugContext.Scope ignored = debug.scope("ParsingToMaterializeLambdas")) { |
| 73 | + lambdaParserPhase.apply(graph, context); |
| 74 | + } catch (Throwable e) { |
| 75 | + throw debug.handle(e); |
| 76 | + } |
| 77 | + return graph; |
| 78 | + } |
| 79 | + |
| 80 | + public static Stream<? extends ResolvedJavaMethod> allExecutablesDeclaredInClass(ResolvedJavaType t) { |
| 81 | + return Stream.concat(Stream.concat( |
| 82 | + Arrays.stream(t.getDeclaredMethods(false)), |
| 83 | + Arrays.stream(t.getDeclaredConstructors(false))), |
| 84 | + t.getClassInitializer() == null ? Stream.empty() : Stream.of(t.getClassInitializer())); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Get the lambda class in the constant if it is a {@code DirectMethodHandle}, by getting the |
| 89 | + * declaring class of the {@code member} field. |
| 90 | + */ |
| 91 | + public static Class<?> getLambdaClassFromConstantNode(ConstantNode constantNode) { |
| 92 | + Constant constant = constantNode.getValue(); |
| 93 | + Class<?> lambdaClass = getLambdaClassFromMemberField(constant); |
| 94 | + |
| 95 | + if (lambdaClass == null) { |
| 96 | + return null; |
| 97 | + } |
| 98 | + |
| 99 | + return LambdaUtils.isLambdaClass(lambdaClass) ? lambdaClass : null; |
| 100 | + } |
| 101 | + |
| 102 | + private static Class<?> getLambdaClassFromMemberField(Constant constant) { |
| 103 | + ResolvedJavaType constantType = GraalAccess.getOriginalProviders().getMetaAccess().lookupJavaType((JavaConstant) constant); |
| 104 | + |
| 105 | + if (constantType == null) { |
| 106 | + return null; |
| 107 | + } |
| 108 | + |
| 109 | + ResolvedJavaField[] fields = constantType.getInstanceFields(true); |
| 110 | + ResolvedJavaField targetField = null; |
| 111 | + for (ResolvedJavaField field : fields) { |
| 112 | + if (field.getName().equals("member")) { |
| 113 | + targetField = field; |
| 114 | + break; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + if (targetField == null) { |
| 119 | + return null; |
| 120 | + } |
| 121 | + |
| 122 | + JavaConstant fieldValue = GraalAccess.getOriginalProviders().getConstantReflection().readFieldValue(targetField, (JavaConstant) constant); |
| 123 | + Member memberField = GraalAccess.getOriginalProviders().getSnippetReflection().asObject(Member.class, fieldValue); |
| 124 | + return memberField.getDeclaringClass(); |
| 125 | + } |
| 126 | + |
| 127 | + static class LambdaGraphBuilderPhase extends GraphBuilderPhase { |
| 128 | + LambdaGraphBuilderPhase() { |
| 129 | + super(buildLambdaParserConfig()); |
| 130 | + } |
| 131 | + |
| 132 | + LambdaGraphBuilderPhase(GraphBuilderConfiguration config) { |
| 133 | + super(config); |
| 134 | + } |
| 135 | + |
| 136 | + private static GraphBuilderConfiguration buildLambdaParserConfig() { |
| 137 | + GraphBuilderConfiguration.Plugins plugins = new GraphBuilderConfiguration.Plugins(new InvocationPlugins()); |
| 138 | + plugins.setClassInitializationPlugin(new NoClassInitializationPlugin()); |
| 139 | + plugins.prependNodePlugin(new MethodHandlePlugin(GraalAccess.getOriginalProviders().getConstantReflection().getMethodHandleAccess(), false)); |
| 140 | + return GraphBuilderConfiguration.getDefault(plugins).withEagerResolving(true); |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public GraphBuilderPhase copyWithConfig(GraphBuilderConfiguration config) { |
| 145 | + return new LambdaGraphBuilderPhase(config); |
| 146 | + } |
| 147 | + |
| 148 | + static class LambdaBytecodeParser extends BytecodeParser { |
| 149 | + protected LambdaBytecodeParser(Instance graphBuilderInstance, StructuredGraph graph, BytecodeParser parent, ResolvedJavaMethod method, int entryBCI, IntrinsicContext intrinsicContext) { |
| 150 | + super(graphBuilderInstance, graph, parent, method, entryBCI, intrinsicContext); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + protected Instance createInstance(CoreProviders providers, GraphBuilderConfiguration instanceGBConfig, OptimisticOptimizations optimisticOpts, IntrinsicContext initialIntrinsicContext) { |
| 156 | + return new Instance(providers, instanceGBConfig, optimisticOpts, initialIntrinsicContext) { |
| 157 | + @Override |
| 158 | + protected BytecodeParser createBytecodeParser(StructuredGraph graph, BytecodeParser parent, ResolvedJavaMethod method, int entryBCI, IntrinsicContext intrinsicContext) { |
| 159 | + return new LambdaBytecodeParser(this, graph, parent, method, entryBCI, intrinsicContext); |
| 160 | + } |
| 161 | + }; |
| 162 | + } |
| 163 | + } |
| 164 | +} |
0 commit comments