|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, 2023, 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 com.oracle.graal.pointsto.infrastructure.OriginalMethodProvider; |
| 28 | +import com.oracle.svm.core.bootstrap.BootstrapMethodConfiguration; |
| 29 | + |
| 30 | +import jdk.graal.compiler.core.common.BootstrapMethodIntrospection; |
| 31 | +import jdk.graal.compiler.java.BytecodeParser; |
| 32 | +import jdk.graal.compiler.java.GraphBuilderPhase; |
| 33 | +import jdk.graal.compiler.nodes.StructuredGraph; |
| 34 | +import jdk.graal.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration; |
| 35 | +import jdk.graal.compiler.nodes.graphbuilderconf.IntrinsicContext; |
| 36 | +import jdk.graal.compiler.nodes.spi.CoreProviders; |
| 37 | +import jdk.graal.compiler.phases.OptimisticOptimizations; |
| 38 | +import jdk.graal.compiler.serviceprovider.GraalServices; |
| 39 | +import jdk.vm.ci.meta.ResolvedJavaMethod; |
| 40 | + |
| 41 | +public class LambdaSubstrateGraphBuilderPhase extends GraphBuilderPhase { |
| 42 | + public LambdaSubstrateGraphBuilderPhase(GraphBuilderConfiguration config) { |
| 43 | + super(config); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + protected Instance createInstance(CoreProviders providers, GraphBuilderConfiguration instanceGBConfig, OptimisticOptimizations optimisticOpts, IntrinsicContext initialIntrinsicContext) { |
| 48 | + return new LambdaSubstrateGraphBuilderInstance(providers, instanceGBConfig, optimisticOpts, initialIntrinsicContext); |
| 49 | + } |
| 50 | + |
| 51 | + public static class LambdaSubstrateGraphBuilderInstance extends GraphBuilderPhase.Instance { |
| 52 | + LambdaSubstrateGraphBuilderInstance(CoreProviders theProviders, GraphBuilderConfiguration graphBuilderConfig, OptimisticOptimizations optimisticOpts, |
| 53 | + IntrinsicContext initialIntrinsicContext) { |
| 54 | + super(theProviders, graphBuilderConfig, optimisticOpts, initialIntrinsicContext); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + protected BytecodeParser createBytecodeParser(StructuredGraph graph, BytecodeParser parent, ResolvedJavaMethod method, int entryBCI, IntrinsicContext intrinsicContext) { |
| 59 | + return new LambdaSubstrateBytecodeParser(this, graph, parent, method, entryBCI, intrinsicContext); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + public static class LambdaSubstrateBytecodeParser extends BytecodeParser { |
| 64 | + protected LambdaSubstrateBytecodeParser(Instance graphBuilderInstance, StructuredGraph graph, BytecodeParser parent, ResolvedJavaMethod method, int entryBCI, |
| 65 | + IntrinsicContext intrinsicContext) { |
| 66 | + super(graphBuilderInstance, graph, parent, method, entryBCI, intrinsicContext); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + protected void genLoadConstant(int cpi, int opcode) { |
| 71 | + Object con = lookupConstant(cpi, opcode, false); |
| 72 | + BootstrapMethodIntrospection bootstrap = GraalServices.lookupBootstrapMethodIntrospection(constantPool, cpi, -1); |
| 73 | + if (con == null && bootstrap != null && BootstrapMethodConfiguration.singleton().isCondyTrusted(OriginalMethodProvider.getJavaMethod(bootstrap.getMethod()))) { |
| 74 | + /* |
| 75 | + * With the current implementation of LambdaUtils#findStableLambdaName, each lambda |
| 76 | + * must contain at least one method invocation to compute its name. Looking up the |
| 77 | + * constant with allowBootstrapMethodInvocation set to false will produce null if |
| 78 | + * the constant is dynamic. Returning it will cause a runtime exception and if the |
| 79 | + * lambda starts with the constant lookup, the lambda will contain no method |
| 80 | + * invocation. At this stage, the AnalysisBytecodeParser cannot be created, so the |
| 81 | + * graph with the bootstrap method call cannot be created. Thus, at the moment, |
| 82 | + * allowing those bootstrap method to be executed at build time is the only way to |
| 83 | + * obtain correct lambda names. |
| 84 | + */ |
| 85 | + con = lookupConstant(cpi, opcode, true); |
| 86 | + } |
| 87 | + genLoadConstantHelper(con, opcode); |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments