1 /* 2 * Copyright (c) 2017, 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 24 25 package org.graalvm.compiler.phases.contract; 26 27 import java.lang.reflect.Modifier; 28 import java.util.function.Predicate; 29 30 import org.graalvm.compiler.graph.Node; 31 import org.graalvm.compiler.graph.NodeClass; 32 import org.graalvm.compiler.nodeinfo.NodeCycles; 33 import org.graalvm.compiler.nodeinfo.NodeInfo; 34 import org.graalvm.compiler.nodeinfo.NodeSize; 35 import org.graalvm.compiler.phases.VerifyPhase; 36 37 /** 38 * Utility class that verifies that every {@link Class} extending {@link Node} specifies non default 39 * values for {@link NodeCycles} and {@link NodeSize} in its {@link NodeInfo} annotation. 40 */ 41 public class VerifyNodeCosts { 42 verifyNodeClass(Class<?> clazz)43 public static void verifyNodeClass(Class<?> clazz) { 44 Class<?> nodeClass = Node.class; 45 if (nodeClass.isAssignableFrom(clazz)) { 46 if (!clazz.isAnnotationPresent(NodeInfo.class)) { 47 throw new VerifyPhase.VerificationError("%s.java extends Node.java but does not specify a NodeInfo annotation.", clazz.getName()); 48 } 49 50 if (!Modifier.isAbstract(clazz.getModifiers())) { 51 boolean cyclesSet = walkCHUntil(getType(clazz), getType(nodeClass), cur -> { 52 return cur.cycles() != NodeCycles.CYCLES_UNSET; 53 }); 54 boolean sizeSet = walkCHUntil(getType(clazz), getType(nodeClass), cur -> { 55 return cur.size() != NodeSize.SIZE_UNSET; 56 }); 57 if (!cyclesSet) { 58 throw new VerifyPhase.VerificationError("%s.java does not specify a NodeCycles value in its class hierarchy.", clazz.getName()); 59 } 60 if (!sizeSet) { 61 throw new VerifyPhase.VerificationError("%s.java does not specify a NodeSize value in its class hierarchy.", clazz.getName()); 62 } 63 } 64 } 65 } 66 getType(Class<?> c)67 private static NodeClass<?> getType(Class<?> c) { 68 try { 69 return NodeClass.get(c); 70 } catch (Throwable t) { 71 throw new VerifyPhase.VerificationError("%s.java does not specify a TYPE field.", c.getName()); 72 } 73 } 74 walkCHUntil(NodeClass<?> start, NodeClass<?> until, Predicate<NodeClass<?>> p)75 private static boolean walkCHUntil(NodeClass<?> start, NodeClass<?> until, Predicate<NodeClass<?>> p) { 76 NodeClass<?> cur = start; 77 while (cur != until && cur != null) { 78 if (p.test(cur)) { 79 return true; 80 } 81 cur = cur.getSuperNodeClass(); 82 } 83 return false; 84 } 85 86 } 87