/*
* Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.graalvm.compiler.core.test;
import static java.lang.reflect.Modifier.isStatic;
import static jdk.vm.ci.runtime.JVMCICompiler.INVOCATION_ENTRY_BCI;
import static org.graalvm.compiler.nodes.ConstantNode.getConstantNodes;
import static org.graalvm.compiler.nodes.graphbuilderconf.InlineInvokePlugin.InlineInfo.DO_NOT_INLINE_NO_EXCEPTION;
import static org.graalvm.compiler.nodes.graphbuilderconf.InlineInvokePlugin.InlineInfo.DO_NOT_INLINE_WITH_EXCEPTION;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Constructor;
import java.lang.reflect.Executable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;
import org.graalvm.compiler.api.directives.GraalDirectives;
import org.graalvm.compiler.api.replacements.SnippetReflectionProvider;
import org.graalvm.compiler.api.test.Graal;
import org.graalvm.compiler.code.CompilationResult;
import org.graalvm.compiler.core.CompilationPrinter;
import org.graalvm.compiler.core.GraalCompiler;
import org.graalvm.compiler.core.GraalCompiler.Request;
import org.graalvm.compiler.core.common.CompilationIdentifier;
import org.graalvm.compiler.core.common.type.StampFactory;
import org.graalvm.compiler.core.target.Backend;
import org.graalvm.compiler.debug.DebugContext;
import org.graalvm.compiler.debug.DebugDumpHandler;
import org.graalvm.compiler.debug.DebugHandlersFactory;
import org.graalvm.compiler.debug.GraalError;
import org.graalvm.compiler.debug.TTY;
import org.graalvm.compiler.graph.Node;
import org.graalvm.compiler.graph.NodeClass;
import org.graalvm.compiler.graph.NodeMap;
import org.graalvm.compiler.java.BytecodeParser;
import org.graalvm.compiler.java.ComputeLoopFrequenciesClosure;
import org.graalvm.compiler.java.GraphBuilderPhase;
import org.graalvm.compiler.lir.asm.CompilationResultBuilderFactory;
import org.graalvm.compiler.lir.phases.LIRSuites;
import org.graalvm.compiler.loop.phases.ConvertDeoptimizeToGuardPhase;
import org.graalvm.compiler.nodeinfo.NodeInfo;
import org.graalvm.compiler.nodeinfo.NodeSize;
import org.graalvm.compiler.nodeinfo.Verbosity;
import org.graalvm.compiler.nodes.BreakpointNode;
import org.graalvm.compiler.nodes.Cancellable;
import org.graalvm.compiler.nodes.ConstantNode;
import org.graalvm.compiler.nodes.FixedWithNextNode;
import org.graalvm.compiler.nodes.FrameState;
import org.graalvm.compiler.nodes.FullInfopointNode;
import org.graalvm.compiler.nodes.Invoke;
import org.graalvm.compiler.nodes.InvokeNode;
import org.graalvm.compiler.nodes.InvokeWithExceptionNode;
import org.graalvm.compiler.nodes.ParameterNode;
import org.graalvm.compiler.nodes.ProxyNode;
import org.graalvm.compiler.nodes.ReturnNode;
import org.graalvm.compiler.nodes.StructuredGraph;
import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
import org.graalvm.compiler.nodes.StructuredGraph.Builder;
import org.graalvm.compiler.nodes.StructuredGraph.ScheduleResult;
import org.graalvm.compiler.nodes.ValueNode;
import org.graalvm.compiler.nodes.cfg.Block;
import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration;
import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration.Plugins;
import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext;
import org.graalvm.compiler.nodes.graphbuilderconf.InlineInvokePlugin;
import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin;
import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins;
import org.graalvm.compiler.nodes.java.AccessFieldNode;
import org.graalvm.compiler.nodes.spi.LoweringProvider;
import org.graalvm.compiler.nodes.spi.Replacements;
import org.graalvm.compiler.nodes.virtual.VirtualObjectNode;
import org.graalvm.compiler.options.OptionValues;
import org.graalvm.compiler.phases.BasePhase;
import org.graalvm.compiler.phases.OptimisticOptimizations;
import org.graalvm.compiler.phases.Phase;
import org.graalvm.compiler.phases.PhaseSuite;
import org.graalvm.compiler.phases.common.CanonicalizerPhase;
import org.graalvm.compiler.phases.common.inlining.InliningPhase;
import org.graalvm.compiler.phases.common.inlining.info.InlineInfo;
import org.graalvm.compiler.phases.common.inlining.policy.GreedyInliningPolicy;
import org.graalvm.compiler.phases.schedule.SchedulePhase;
import org.graalvm.compiler.phases.schedule.SchedulePhase.SchedulingStrategy;
import org.graalvm.compiler.phases.tiers.HighTierContext;
import org.graalvm.compiler.phases.tiers.MidTierContext;
import org.graalvm.compiler.phases.tiers.Suites;
import org.graalvm.compiler.phases.tiers.TargetProvider;
import org.graalvm.compiler.phases.util.Providers;
import org.graalvm.compiler.printer.GraalDebugHandlersFactory;
import org.graalvm.compiler.runtime.RuntimeProvider;
import org.graalvm.compiler.test.AddExports;
import org.graalvm.compiler.test.GraalTest;
import org.graalvm.compiler.test.ModuleSupport;
import org.junit.After;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.internal.AssumptionViolatedException;
import jdk.vm.ci.code.Architecture;
import jdk.vm.ci.code.BailoutException;
import jdk.vm.ci.code.CodeCacheProvider;
import jdk.vm.ci.code.InstalledCode;
import jdk.vm.ci.code.TargetDescription;
import jdk.vm.ci.meta.Assumptions.Assumption;
import jdk.vm.ci.meta.ConstantReflectionProvider;
import jdk.vm.ci.meta.DeoptimizationReason;
import jdk.vm.ci.meta.JavaConstant;
import jdk.vm.ci.meta.JavaKind;
import jdk.vm.ci.meta.JavaType;
import jdk.vm.ci.meta.MetaAccessProvider;
import jdk.vm.ci.meta.ProfilingInfo;
import jdk.vm.ci.meta.ResolvedJavaMethod;
import jdk.vm.ci.meta.ResolvedJavaType;
import jdk.vm.ci.meta.SpeculationLog;
/**
* Base class for compiler unit tests.
*
* White box tests for compiler transformations use this pattern:
*
*
Create a graph by {@linkplain #parseEager parsing} a method.
*
Manually modify the graph (e.g. replace a parameter node with a constant).
*
Apply a transformation to the graph.
*
Assert that the transformed graph is equal to an expected graph.
*
*
* See {@link InvokeHintsTest} as an example of a white box test.
*
* Black box tests use the {@link #test(String, Object...)} or
* {@link #testN(int, String, Object...)} to execute some method in the interpreter and compare its
* result against that produced by a Graal compiled version of the method.
*
* These tests will be run by the {@code mx unittest} command.
*/
@AddExports({"java.base/jdk.internal.org.objectweb.asm", "java.base/jdk.internal.org.objectweb.asm.tree"})
public abstract class GraalCompilerTest extends GraalTest {
/**
* Gets the initial option values provided by the Graal runtime. These are option values
* typically parsed from the command line.
*/
public static OptionValues getInitialOptions() {
return Graal.getRequiredCapability(OptionValues.class);
}
private static final int BAILOUT_RETRY_LIMIT = 1;
private final Providers providers;
private final Backend backend;
/**
* Representative class for the {@code java.base} module.
*/
public static final Class> JAVA_BASE = Class.class;
/**
* Exports the package named {@code packageName} declared in {@code moduleMember}'s module to
* this object's module. This must be called before accessing packages that are no longer public
* as of JDK 9.
*/
protected final void exportPackage(Class> moduleMember, String packageName) {
ModuleSupport.exportPackageTo(moduleMember, packageName, getClass());
}
/**
* Denotes a test method that must be inlined by the {@link BytecodeParser}.
*/
@Target({ElementType.METHOD, ElementType.CONSTRUCTOR})
@Retention(RetentionPolicy.RUNTIME)
public @interface BytecodeParserForceInline {
}
/**
* Denotes a test method that must never be inlined by the {@link BytecodeParser}.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.CONSTRUCTOR})
public @interface BytecodeParserNeverInline {
/**
* Specifies if the call should be implemented with {@link InvokeWithExceptionNode} instead
* of {@link InvokeNode}.
*/
boolean invokeWithException() default false;
}
/**
* Can be overridden by unit tests to verify properties of the graph.
*
* @param graph the graph at the end of HighTier
* @throws AssertionError if the verification fails
*/
protected void checkHighTierGraph(StructuredGraph graph) {
}
/**
* Can be overridden by unit tests to verify properties of the graph.
*
* @param graph the graph at the end of MidTier
* @throws AssertionError if the verification fails
*/
protected void checkMidTierGraph(StructuredGraph graph) {
}
/**
* Can be overridden by unit tests to verify properties of the graph.
*
* @param graph the graph at the end of LowTier
* @throws AssertionError if the verification fails
*/
protected void checkLowTierGraph(StructuredGraph graph) {
}
protected static void breakpoint() {
}
@SuppressWarnings("unused")
protected static void breakpoint(int arg0) {
}
protected static void shouldBeOptimizedAway() {
}
protected Suites createSuites(OptionValues opts) {
Suites ret = backend.getSuites().getDefaultSuites(opts).copy();
ListIterator> iter = ret.getHighTier().findPhase(ConvertDeoptimizeToGuardPhase.class, true);
if (iter == null) {
/*
* in the economy configuration, we don't have the ConvertDeoptimizeToGuard phase, so we
* just select the first CanonicalizerPhase in HighTier
*/
iter = ret.getHighTier().findPhase(CanonicalizerPhase.class);
}
iter.add(new Phase() {
@Override
protected void run(StructuredGraph graph) {
ComputeLoopFrequenciesClosure.compute(graph);
}
@Override
public float codeSizeIncrease() {
return NodeSize.IGNORE_SIZE_CONTRACT_FACTOR;
}
@Override
protected CharSequence getName() {
return "ComputeLoopFrequenciesPhase";
}
});
ret.getHighTier().appendPhase(new Phase() {
@Override
protected void run(StructuredGraph graph) {
checkHighTierGraph(graph);
}
@Override
public float codeSizeIncrease() {
return NodeSize.IGNORE_SIZE_CONTRACT_FACTOR;
}
@Override
protected CharSequence getName() {
return "CheckGraphPhase";
}
});
ret.getMidTier().appendPhase(new Phase() {
@Override
protected void run(StructuredGraph graph) {
checkMidTierGraph(graph);
}
@Override
public float codeSizeIncrease() {
return NodeSize.IGNORE_SIZE_CONTRACT_FACTOR;
}
@Override
protected CharSequence getName() {
return "CheckGraphPhase";
}
});
ret.getLowTier().appendPhase(new Phase() {
@Override
protected void run(StructuredGraph graph) {
checkLowTierGraph(graph);
}
@Override
public float codeSizeIncrease() {
return NodeSize.IGNORE_SIZE_CONTRACT_FACTOR;
}
@Override
protected CharSequence getName() {
return "CheckGraphPhase";
}
});
return ret;
}
protected LIRSuites createLIRSuites(OptionValues opts) {
LIRSuites ret = backend.getSuites().getDefaultLIRSuites(opts).copy();
return ret;
}
private static final ThreadLocal> cache = ThreadLocal.withInitial(HashMap::new);
@BeforeClass
public static void resetCache() {
cache.get().clear();
}
public GraalCompilerTest() {
this.backend = Graal.getRequiredCapability(RuntimeProvider.class).getHostBackend();
this.providers = getBackend().getProviders();
}
/**
* Set up a test for a non-default backend. The test should check (via {@link #getBackend()} )
* whether the desired backend is available.
*
* @param arch the name of the desired backend architecture
*/
public GraalCompilerTest(Class extends Architecture> arch) {
RuntimeProvider runtime = Graal.getRequiredCapability(RuntimeProvider.class);
Backend b = runtime.getBackend(arch);
if (b != null) {
this.backend = b;
} else {
// Fall back to the default/host backend
this.backend = runtime.getHostBackend();
}
this.providers = backend.getProviders();
}
/**
* Set up a test for a non-default backend.
*
* @param backend the desired backend
*/
public GraalCompilerTest(Backend backend) {
this.backend = backend;
this.providers = backend.getProviders();
}
@Override
@After
public void afterTest() {
if (invocationPluginExtensions != null) {
synchronized (this) {
if (invocationPluginExtensions != null) {
extendedInvocationPlugins.removeTestPlugins(invocationPluginExtensions);
extendedInvocationPlugins = null;
invocationPluginExtensions = null;
}
}
}
super.afterTest();
}
/**
* Gets a {@link DebugContext} object corresponding to {@code options}, creating a new one if
* none currently exists. Debug contexts created by this method will have their
* {@link DebugDumpHandler}s closed in {@link #afterTest()}.
*/
protected DebugContext getDebugContext() {
return getDebugContext(getInitialOptions(), null, null);
}
@Override
protected Collection getDebugHandlersFactories() {
return Collections.singletonList(new GraalDebugHandlersFactory(getSnippetReflection()));
}
protected void assertEquals(StructuredGraph expected, StructuredGraph graph) {
assertEquals(expected, graph, false, true);
}
protected int countUnusedConstants(StructuredGraph graph) {
int total = 0;
for (ConstantNode node : getConstantNodes(graph)) {
if (node.hasNoUsages()) {
total++;
}
}
return total;
}
protected int getNodeCountExcludingUnusedConstants(StructuredGraph graph) {
return graph.getNodeCount() - countUnusedConstants(graph);
}
protected void assertEquals(StructuredGraph expected, StructuredGraph graph, boolean excludeVirtual, boolean checkConstants) {
String expectedString = getCanonicalGraphString(expected, excludeVirtual, checkConstants);
String actualString = getCanonicalGraphString(graph, excludeVirtual, checkConstants);
String mismatchString = compareGraphStrings(expected, expectedString, graph, actualString);
if (!excludeVirtual && getNodeCountExcludingUnusedConstants(expected) != getNodeCountExcludingUnusedConstants(graph)) {
expected.getDebug().dump(DebugContext.BASIC_LEVEL, expected, "Node count not matching - expected");
graph.getDebug().dump(DebugContext.BASIC_LEVEL, graph, "Node count not matching - actual");
Assert.fail("Graphs do not have the same number of nodes: " + expected.getNodeCount() + " vs. " + graph.getNodeCount() + "\n" + mismatchString);
}
if (!expectedString.equals(actualString)) {
expected.getDebug().dump(DebugContext.BASIC_LEVEL, expected, "mismatching graphs - expected");
graph.getDebug().dump(DebugContext.BASIC_LEVEL, graph, "mismatching graphs - actual");
Assert.fail(mismatchString);
}
}
private static String compareGraphStrings(StructuredGraph expectedGraph, String expectedString, StructuredGraph actualGraph, String actualString) {
if (!expectedString.equals(actualString)) {
String[] expectedLines = expectedString.split("\n");
String[] actualLines = actualString.split("\n");
int diffIndex = -1;
int limit = Math.min(actualLines.length, expectedLines.length);
String marker = " <<<";
for (int i = 0; i < limit; i++) {
if (!expectedLines[i].equals(actualLines[i])) {
diffIndex = i;
break;
}
}
if (diffIndex == -1) {
// Prefix is the same so add some space after the prefix
diffIndex = limit;
if (actualLines.length == limit) {
actualLines = Arrays.copyOf(actualLines, limit + 1);
actualLines[diffIndex] = "";
} else {
assert expectedLines.length == limit;
expectedLines = Arrays.copyOf(expectedLines, limit + 1);
expectedLines[diffIndex] = "";
}
}
// Place a marker next to the first line that differs
expectedLines[diffIndex] = expectedLines[diffIndex] + marker;
actualLines[diffIndex] = actualLines[diffIndex] + marker;
String ediff = String.join("\n", expectedLines);
String adiff = String.join("\n", actualLines);
return "mismatch in graphs:\n========= expected (" + expectedGraph + ") =========\n" + ediff + "\n\n========= actual (" + actualGraph + ") =========\n" + adiff;
} else {
return "mismatch in graphs";
}
}
protected void assertOptimizedAway(StructuredGraph g) {
Assert.assertEquals(0, g.getNodes().filter(NotOptimizedNode.class).count());
}
protected void assertConstantReturn(StructuredGraph graph, int value) {
String graphString = getCanonicalGraphString(graph, false, true);
Assert.assertEquals("unexpected number of ReturnNodes: " + graphString, graph.getNodes(ReturnNode.TYPE).count(), 1);
ValueNode result = graph.getNodes(ReturnNode.TYPE).first().result();
Assert.assertTrue("unexpected ReturnNode result node: " + graphString, result.isConstant());
Assert.assertEquals("unexpected ReturnNode result kind: " + graphString, result.asJavaConstant().getJavaKind(), JavaKind.Int);
Assert.assertEquals("unexpected ReturnNode result: " + graphString, result.asJavaConstant().asInt(), value);
}
protected static String getCanonicalGraphString(StructuredGraph graph, boolean excludeVirtual, boolean checkConstants) {
SchedulePhase schedule = new SchedulePhase(SchedulingStrategy.EARLIEST);
schedule.apply(graph);
ScheduleResult scheduleResult = graph.getLastSchedule();
NodeMap canonicalId = graph.createNodeMap();
int nextId = 0;
List constantsLines = new ArrayList<>();
StringBuilder result = new StringBuilder();
for (Block block : scheduleResult.getCFG().getBlocks()) {
result.append("Block ").append(block).append(' ');
if (block == scheduleResult.getCFG().getStartBlock()) {
result.append("* ");
}
result.append("-> ");
for (Block succ : block.getSuccessors()) {
result.append(succ).append(' ');
}
result.append('\n');
for (Node node : scheduleResult.getBlockToNodesMap().get(block)) {
if (node instanceof ValueNode && node.isAlive()) {
if (!excludeVirtual || !(node instanceof VirtualObjectNode || node instanceof ProxyNode || node instanceof FullInfopointNode || node instanceof ParameterNode)) {
if (node instanceof ConstantNode) {
String name = checkConstants ? node.toString(Verbosity.Name) : node.getClass().getSimpleName();
if (excludeVirtual) {
constantsLines.add(name);
} else {
constantsLines.add(name + " (" + filteredUsageCount(node) + ")");
}
} else {
int id;
if (canonicalId.get(node) != null) {
id = canonicalId.get(node);
} else {
id = nextId++;
canonicalId.set(node, id);
}
String name = node.getClass().getSimpleName();
result.append(" ").append(id).append('|').append(name);
if (node instanceof AccessFieldNode) {
result.append('#');
result.append(((AccessFieldNode) node).field());
}
if (!excludeVirtual) {
result.append(" (");
result.append(filteredUsageCount(node));
result.append(')');
}
result.append('\n');
}
}
}
}
}
StringBuilder constantsLinesResult = new StringBuilder();
constantsLinesResult.append(constantsLines.size()).append(" constants:\n");
Collections.sort(constantsLines);
for (String s : constantsLines) {
constantsLinesResult.append(s);
constantsLinesResult.append('\n');
}
return constantsLinesResult.toString() + result.toString();
}
/**
* @return usage count excluding {@link FrameState} usages
*/
private static int filteredUsageCount(Node node) {
return node.usages().filter(n -> !(n instanceof FrameState)).count();
}
/**
* @param graph
* @return a scheduled textual dump of {@code graph} .
*/
protected static String getScheduledGraphString(StructuredGraph graph) {
SchedulePhase schedule = new SchedulePhase(SchedulingStrategy.EARLIEST_WITH_GUARD_ORDER);
schedule.apply(graph);
ScheduleResult scheduleResult = graph.getLastSchedule();
StringBuilder result = new StringBuilder();
Block[] blocks = scheduleResult.getCFG().getBlocks();
for (Block block : blocks) {
result.append("Block ").append(block).append(' ');
if (block == scheduleResult.getCFG().getStartBlock()) {
result.append("* ");
}
result.append("-> ");
for (Block succ : block.getSuccessors()) {
result.append(succ).append(' ');
}
result.append('\n');
for (Node node : scheduleResult.getBlockToNodesMap().get(block)) {
result.append(String.format("%1S\n", node));
}
}
return result.toString();
}
protected Backend getBackend() {
return backend;
}
protected final Providers getProviders() {
return providers;
}
protected HighTierContext getDefaultHighTierContext() {
return new HighTierContext(getProviders(), getDefaultGraphBuilderSuite(), getOptimisticOptimizations());
}
protected MidTierContext getDefaultMidTierContext() {
return new MidTierContext(getProviders(), getTargetProvider(), getOptimisticOptimizations(), null);
}
protected SnippetReflectionProvider getSnippetReflection() {
return Graal.getRequiredCapability(SnippetReflectionProvider.class);
}
protected TargetDescription getTarget() {
return getTargetProvider().getTarget();
}
protected TargetProvider getTargetProvider() {
return getBackend();
}
protected CodeCacheProvider getCodeCache() {
return getProviders().getCodeCache();
}
protected ConstantReflectionProvider getConstantReflection() {
return getProviders().getConstantReflection();
}
protected MetaAccessProvider getMetaAccess() {
return getProviders().getMetaAccess();
}
protected LoweringProvider getLowerer() {
return getProviders().getLowerer();
}
protected final BasePhase createInliningPhase() {
return createInliningPhase(new CanonicalizerPhase());
}
protected BasePhase createInliningPhase(CanonicalizerPhase canonicalizer) {
return createInliningPhase(null, canonicalizer);
}
static class GreedyTestInliningPolicy extends GreedyInliningPolicy {
GreedyTestInliningPolicy(Map hints) {
super(hints);
}
@Override
protected int previousLowLevelGraphSize(InlineInfo info) {
// Ignore previous compiles for tests
return 0;
}
}
protected BasePhase createInliningPhase(Map hints, CanonicalizerPhase canonicalizer) {
return new InliningPhase(new GreedyTestInliningPolicy(hints), canonicalizer);
}
protected CompilationIdentifier getCompilationId(ResolvedJavaMethod method) {
return getBackend().getCompilationIdentifier(method);
}
protected CompilationIdentifier getOrCreateCompilationId(final ResolvedJavaMethod installedCodeOwner, StructuredGraph graph) {
if (graph != null) {
return graph.compilationId();
}
return getCompilationId(installedCodeOwner);
}
protected void testN(int n, final String name, final Object... args) {
final List errors = new ArrayList<>(n);
Thread[] threads = new Thread[n];
for (int i = 0; i < n; i++) {
Thread t = new Thread(i + ":" + name) {
@Override
public void run() {
try {
test(name, args);
} catch (Throwable e) {
errors.add(e);
}
}
};
threads[i] = t;
t.start();
}
for (int i = 0; i < n; i++) {
try {
threads[i].join();
} catch (InterruptedException e) {
errors.add(e);
}
}
if (!errors.isEmpty()) {
throw new MultiCauseAssertionError(errors.size() + " failures", errors.toArray(new Throwable[errors.size()]));
}
}
protected Object referenceInvoke(ResolvedJavaMethod method, Object receiver, Object... args)
throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException {
return invoke(method, receiver, args);
}
public static class Result {
public final Object returnValue;
public final Throwable exception;
public Result(Object returnValue, Throwable exception) {
this.returnValue = returnValue;
this.exception = exception;
}
@Override
public String toString() {
return exception == null ? returnValue == null ? "null" : returnValue.toString() : "!" + exception;
}
}
/**
* Called before a test is executed.
*/
protected void before(@SuppressWarnings("unused") ResolvedJavaMethod method) {
}
/**
* Called after a test is executed.
*/
protected void after() {
}
protected Result executeExpected(ResolvedJavaMethod method, Object receiver, Object... args) {
before(method);
try {
// This gives us both the expected return value as well as ensuring that the method to
// be compiled is fully resolved
return new Result(referenceInvoke(method, receiver, args), null);
} catch (InvocationTargetException e) {
return new Result(null, e.getTargetException());
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
after();
}
}
protected Result executeActual(ResolvedJavaMethod method, Object receiver, Object... args) {
return executeActual(getInitialOptions(), method, receiver, args);
}
protected Result executeActual(OptionValues options, ResolvedJavaMethod method, Object receiver, Object... args) {
before(method);
Object[] executeArgs = argsWithReceiver(receiver, args);
checkArgs(method, executeArgs);
InstalledCode compiledMethod = getCode(method, options);
try {
return new Result(compiledMethod.executeVarargs(executeArgs), null);
} catch (Throwable e) {
return new Result(null, e);
} finally {
after();
}
}
protected void checkArgs(ResolvedJavaMethod method, Object[] args) {
JavaType[] sig = method.toParameterTypes();
Assert.assertEquals(sig.length, args.length);
for (int i = 0; i < args.length; i++) {
JavaType javaType = sig[i];
JavaKind kind = javaType.getJavaKind();
Object arg = args[i];
if (kind == JavaKind.Object) {
if (arg != null && javaType instanceof ResolvedJavaType) {
ResolvedJavaType resolvedJavaType = (ResolvedJavaType) javaType;
Assert.assertTrue(resolvedJavaType + " from " + getMetaAccess().lookupJavaType(arg.getClass()), resolvedJavaType.isAssignableFrom(getMetaAccess().lookupJavaType(arg.getClass())));
}
} else {
Assert.assertNotNull(arg);
Assert.assertEquals(kind.toBoxedJavaClass(), arg.getClass());
}
}
}
/**
* Prepends a non-null receiver argument to a given list or args.
*
* @param receiver the receiver argument to prepend if it is non-null
*/
protected Object[] argsWithReceiver(Object receiver, Object... args) {
Object[] executeArgs;
if (receiver == null) {
executeArgs = args;
} else {
executeArgs = new Object[args.length + 1];
executeArgs[0] = receiver;
for (int i = 0; i < args.length; i++) {
executeArgs[i + 1] = args[i];
}
}
return applyArgSuppliers(executeArgs);
}
protected final Result test(String name, Object... args) {
return test(getInitialOptions(), name, args);
}
protected final Result test(OptionValues options, String name, Object... args) {
try {
ResolvedJavaMethod method = getResolvedJavaMethod(name);
Object receiver = method.isStatic() ? null : this;
return test(options, method, receiver, args);
} catch (AssumptionViolatedException e) {
// Suppress so that subsequent calls to this method within the
// same Junit @Test annotated method can proceed.
return null;
}
}
/**
* Type denoting a lambda that supplies a fresh value each time it is called. This is useful
* when supplying an argument to {@link GraalCompilerTest#test(String, Object...)} where the
* test modifies the state of the argument (e.g., updates a field).
*/
@FunctionalInterface
public interface ArgSupplier extends Supplier