|
| 1 | +/* |
| 2 | + * Copyright (c) 2022, 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 org.graalvm.compiler.truffle.test; |
| 26 | + |
| 27 | +import org.graalvm.compiler.nodes.ConstantNode; |
| 28 | +import org.graalvm.compiler.nodes.ReturnNode; |
| 29 | +import org.graalvm.compiler.nodes.StructuredGraph; |
| 30 | +import org.graalvm.compiler.truffle.runtime.OptimizedCallTarget; |
| 31 | +import org.junit.Assert; |
| 32 | +import org.junit.Test; |
| 33 | + |
| 34 | +import com.oracle.truffle.api.CompilerAsserts; |
| 35 | +import com.oracle.truffle.api.Truffle; |
| 36 | +import com.oracle.truffle.api.frame.Frame; |
| 37 | +import com.oracle.truffle.api.frame.FrameDescriptor; |
| 38 | +import com.oracle.truffle.api.frame.MaterializedFrame; |
| 39 | +import com.oracle.truffle.api.frame.VirtualFrame; |
| 40 | +import com.oracle.truffle.api.nodes.RootNode; |
| 41 | + |
| 42 | +public class FrameDescriptorTest extends PartialEvaluationTest { |
| 43 | + |
| 44 | + @Test |
| 45 | + public void constantInfo() { |
| 46 | + FrameDescriptor.Builder builder = FrameDescriptor.newBuilder(); |
| 47 | + Object obj = new Object(); |
| 48 | + FrameDescriptor fd = builder.info("foo").info(obj).build(); |
| 49 | + RootNode root1 = new RootNode(null, fd) { |
| 50 | + @Override |
| 51 | + public String toString() { |
| 52 | + return "constantInfo1"; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public Object execute(VirtualFrame frame) { |
| 57 | + CompilerAsserts.partialEvaluationConstant(frame.getFrameDescriptor().getInfo()); |
| 58 | + return frame.getFrameDescriptor().getInfo(); |
| 59 | + } |
| 60 | + }; |
| 61 | + RootNode root2 = new RootNode(null, fd) { |
| 62 | + |
| 63 | + private final FrameDescriptor descriptor = fd; |
| 64 | + |
| 65 | + @Override |
| 66 | + public String toString() { |
| 67 | + return "constantInfo2"; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public Object execute(VirtualFrame frame) { |
| 72 | + CompilerAsserts.partialEvaluationConstant(descriptor.getInfo()); |
| 73 | + return descriptor.getInfo(); |
| 74 | + } |
| 75 | + }; |
| 76 | + MaterializedFrame materializedFrame = Truffle.getRuntime().createMaterializedFrame(new Object[]{}, fd); |
| 77 | + RootNode root3 = new RootNode(null) { |
| 78 | + |
| 79 | + private final Frame fr = materializedFrame; |
| 80 | + |
| 81 | + @Override |
| 82 | + public String toString() { |
| 83 | + return "constantInfo3"; |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public Object execute(VirtualFrame frame) { |
| 88 | + CompilerAsserts.partialEvaluationConstant(fr.getFrameDescriptor().getInfo()); |
| 89 | + return fr.getFrameDescriptor().getInfo(); |
| 90 | + } |
| 91 | + }; |
| 92 | + testConstantReturn(root1, obj); |
| 93 | + testConstantReturn(root2, obj); |
| 94 | + testConstantReturn(root3, obj); |
| 95 | + } |
| 96 | + |
| 97 | + private void testConstantReturn(RootNode rootNode, Object result) { |
| 98 | + OptimizedCallTarget callTarget = (OptimizedCallTarget) rootNode.getCallTarget(); |
| 99 | + Assert.assertTrue(callTarget.call() == result); |
| 100 | + |
| 101 | + StructuredGraph graph = partialEval(callTarget, new Object[]{}); |
| 102 | + assertTrue(graph.getNodes().filter(ReturnNode.class).first().result() instanceof ConstantNode); |
| 103 | + compile(callTarget, graph); |
| 104 | + Assert.assertTrue("CallTarget is valid", callTarget.isValid()); |
| 105 | + Assert.assertTrue(callTarget.call() == result); |
| 106 | + } |
| 107 | + |
| 108 | + @SuppressWarnings("deprecation") |
| 109 | + private static void assertEmptyFrameDescriptor(FrameDescriptor fd, Object info) { |
| 110 | + Assert.assertEquals(0, fd.getNumberOfSlots()); |
| 111 | + Assert.assertEquals(0, fd.getNumberOfAuxiliarySlots()); |
| 112 | + Assert.assertEquals(0, fd.getSize()); |
| 113 | + Assert.assertEquals(null, fd.getDefaultValue()); |
| 114 | + Assert.assertTrue(info == fd.getInfo()); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void testEmpty() { |
| 119 | + FrameDescriptor.Builder builder = FrameDescriptor.newBuilder(); |
| 120 | + FrameDescriptor fd = builder.build(); |
| 121 | + assertEmptyFrameDescriptor(fd, null); |
| 122 | + assertEmptyFrameDescriptor(new FrameDescriptor(), null); |
| 123 | + assertEmptyFrameDescriptor(new FrameDescriptor(null), null); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void testInfo() { |
| 128 | + FrameDescriptor.Builder builder = FrameDescriptor.newBuilder(); |
| 129 | + Object obj = new Object(); |
| 130 | + FrameDescriptor fd = builder.info("foo").info(obj).build(); |
| 131 | + assertEmptyFrameDescriptor(fd, obj); |
| 132 | + } |
| 133 | +} |
0 commit comments