# HG changeset patch # User Gilles Duboscq # Date 1411054966 -7200 # Node ID ac6e25901d62f7b5def3d3cfbfc78bd199df99d5 # Parent a8eb7473d58a2a4f86ae686459509ace09cbd12c Add trusted interface concept and use it for WordBase, fix a NPE and some tests diff -r a8eb7473d58a -r ac6e25901d62 graal/com.oracle.graal.api.meta.test/src/com/oracle/graal/api/meta/test/TestResolvedJavaType.java --- a/graal/com.oracle.graal.api.meta.test/src/com/oracle/graal/api/meta/test/TestResolvedJavaType.java Fri Sep 19 09:59:56 2014 +0200 +++ b/graal/com.oracle.graal.api.meta.test/src/com/oracle/graal/api/meta/test/TestResolvedJavaType.java Thu Sep 18 17:42:46 2014 +0200 @@ -685,6 +685,16 @@ } } + @Test + public void isTrustedInterfaceTypeTest() { + for (Class c : classes) { + ResolvedJavaType type = metaAccess.lookupJavaType(c); + if (TrustedInterface.class.isAssignableFrom(c)) { + assertTrue(type.isTrustedInterfaceType()); + } + } + } + private Method findTestMethod(Method apiMethod) { String testName = apiMethod.getName() + "Test"; for (Method m : getClass().getDeclaredMethods()) { @@ -731,5 +741,4 @@ } } } - } diff -r a8eb7473d58a -r ac6e25901d62 graal/com.oracle.graal.api.meta/src/com/oracle/graal/api/meta/ResolvedJavaType.java --- a/graal/com.oracle.graal.api.meta/src/com/oracle/graal/api/meta/ResolvedJavaType.java Fri Sep 19 09:59:56 2014 +0200 +++ b/graal/com.oracle.graal.api.meta/src/com/oracle/graal/api/meta/ResolvedJavaType.java Thu Sep 18 17:42:46 2014 +0200 @@ -336,4 +336,10 @@ * method is similar to {@link Array#newInstance(Class, int)}. */ Constant newArray(int length); + + /** + * Returns true if this type represents and interface and it should be trusted even in places + * where the JVM verifier would not give any guarantees other than {@link Object}. + */ + boolean isTrustedInterfaceType(); } diff -r a8eb7473d58a -r ac6e25901d62 graal/com.oracle.graal.api.meta/src/com/oracle/graal/api/meta/TrustedInterface.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.graal.api.meta/src/com/oracle/graal/api/meta/TrustedInterface.java Thu Sep 18 17:42:46 2014 +0200 @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2014, 2014, 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 com.oracle.graal.api.meta; + +/** + * Interfaces extanding this interface should be trusted by the compiler. See + * {@link ResolvedJavaType#isTrustedInterfaceType()}. + * + */ +public interface TrustedInterface { + +} diff -r a8eb7473d58a -r ac6e25901d62 graal/com.oracle.graal.compiler.common/src/com/oracle/graal/compiler/common/type/StampFactory.java --- a/graal/com.oracle.graal.compiler.common/src/com/oracle/graal/compiler/common/type/StampFactory.java Fri Sep 19 09:59:56 2014 +0200 +++ b/graal/com.oracle.graal.compiler.common/src/com/oracle/graal/compiler/common/type/StampFactory.java Thu Sep 18 17:42:46 2014 +0200 @@ -243,7 +243,7 @@ } return type.getSuperclass().getArrayClass(); // arrayType.getSuperClass() == Object type } - if (type.isInterface()) { + if (type.isInterface() && !type.isTrustedInterfaceType()) { return null; } return type; diff -r a8eb7473d58a -r ac6e25901d62 graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/HotSpotResolvedObjectType.java --- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/HotSpotResolvedObjectType.java Fri Sep 19 09:59:56 2014 +0200 +++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/HotSpotResolvedObjectType.java Thu Sep 18 17:42:46 2014 +0200 @@ -853,4 +853,11 @@ public String toString() { return "HotSpotType<" + getName() + ", resolved>"; } + + private static final ResolvedJavaType trustedInterfaceType = fromClass(TrustedInterface.class); + + @Override + public boolean isTrustedInterfaceType() { + return trustedInterfaceType.isAssignableFrom(this); + } } diff -r a8eb7473d58a -r ac6e25901d62 graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/HotSpotResolvedPrimitiveType.java --- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/HotSpotResolvedPrimitiveType.java Fri Sep 19 09:59:56 2014 +0200 +++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/HotSpotResolvedPrimitiveType.java Thu Sep 18 17:42:46 2014 +0200 @@ -272,4 +272,9 @@ public Constant newArray(int length) { return HotSpotObjectConstant.forObject(Array.newInstance(mirror(), length)); } + + @Override + public boolean isTrustedInterfaceType() { + return false; + } } diff -r a8eb7473d58a -r ac6e25901d62 graal/com.oracle.graal.nodes.test/src/com/oracle/graal/nodes/test/AbstractObjectStampTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.graal.nodes.test/src/com/oracle/graal/nodes/test/AbstractObjectStampTest.java Thu Sep 18 17:42:46 2014 +0200 @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2013, 2014, 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 com.oracle.graal.nodes.test; + +import org.junit.*; + +import com.oracle.graal.api.meta.*; +import com.oracle.graal.compiler.common.type.*; +import com.oracle.graal.compiler.test.*; + +public abstract class AbstractObjectStampTest extends GraalCompilerTest { + + protected static class A { + + } + + protected static class B extends A { + + } + + protected static class C extends B implements I { + + } + + protected static class D extends A { + + } + + protected abstract static class E extends A { + + } + + protected interface I { + + } + + protected static Stamp join(Stamp a, Stamp b) { + Stamp ab = a.join(b); + Stamp ba = b.join(a); + Assert.assertEquals(ab, ba); + return ab; + } + + protected static Stamp meet(Stamp a, Stamp b) { + Stamp ab = a.meet(b); + Stamp ba = b.meet(a); + Assert.assertEquals(ab, ba); + return ab; + } + + protected ResolvedJavaType getType(Class clazz) { + return getMetaAccess().lookupJavaType(clazz); + } +} diff -r a8eb7473d58a -r ac6e25901d62 graal/com.oracle.graal.nodes.test/src/com/oracle/graal/nodes/test/ObjectStampJoinTest.java --- a/graal/com.oracle.graal.nodes.test/src/com/oracle/graal/nodes/test/ObjectStampJoinTest.java Fri Sep 19 09:59:56 2014 +0200 +++ b/graal/com.oracle.graal.nodes.test/src/com/oracle/graal/nodes/test/ObjectStampJoinTest.java Thu Sep 18 17:42:46 2014 +0200 @@ -28,7 +28,7 @@ import com.oracle.graal.compiler.common.type.*; import com.oracle.graal.nodes.type.*; -public class ObjectStampJoinTest extends ObjectStampTest { +public class ObjectStampJoinTest extends AbstractObjectStampTest { // class A // class B extends A @@ -124,14 +124,14 @@ @Test public void testJoinInterface0() { Stamp a = StampFactory.declared(getType(A.class)); - Stamp i = StampFactory.declared(getType(I.class)); + Stamp i = StampFactory.declared(getType(I.class), false, true); Assert.assertNotSame(StampFactory.illegal(Kind.Object), join(a, i)); } @Test public void testJoinInterface1() { Stamp aNonNull = StampFactory.declaredNonNull(getType(A.class)); - Stamp i = StampFactory.declared(getType(I.class)); + Stamp i = StampFactory.declared(getType(I.class), false, true); Stamp join = join(aNonNull, i); Assert.assertTrue(join instanceof ObjectStamp); Assert.assertTrue(((ObjectStamp) join).nonNull()); @@ -140,9 +140,16 @@ @Test public void testJoinInterface2() { Stamp bExact = StampFactory.exactNonNull(getType(B.class)); - Stamp i = StampFactory.declared(getType(I.class)); + Stamp i = StampFactory.declared(getType(I.class), false, true); Stamp join = join(i, bExact); Assert.assertEquals(StampFactory.illegal(Kind.Object), join); } + @Test + public void testJoinInterface3() { + Stamp bExact = StampFactory.exactNonNull(getType(B.class)); + Stamp i = StampFactory.declared(getType(I.class)); // not trusted + Stamp join = join(i, bExact); + Assert.assertEquals(bExact, join); + } } diff -r a8eb7473d58a -r ac6e25901d62 graal/com.oracle.graal.nodes.test/src/com/oracle/graal/nodes/test/ObjectStampMeetTest.java --- a/graal/com.oracle.graal.nodes.test/src/com/oracle/graal/nodes/test/ObjectStampMeetTest.java Fri Sep 19 09:59:56 2014 +0200 +++ b/graal/com.oracle.graal.nodes.test/src/com/oracle/graal/nodes/test/ObjectStampMeetTest.java Thu Sep 18 17:42:46 2014 +0200 @@ -27,7 +27,7 @@ import com.oracle.graal.api.meta.*; import com.oracle.graal.compiler.common.type.*; -public class ObjectStampMeetTest extends ObjectStampTest { +public class ObjectStampMeetTest extends AbstractObjectStampTest { // class A // class B extends A @@ -104,7 +104,7 @@ @Test public void testMeetInterface0() { Stamp a = StampFactory.declared(getType(A.class)); - Stamp i = StampFactory.declared(getType(I.class)); + Stamp i = StampFactory.declared(getType(I.class), false, true); Assert.assertEquals(StampFactory.declared(getType(Object.class)), meet(a, i)); } diff -r a8eb7473d58a -r ac6e25901d62 graal/com.oracle.graal.nodes.test/src/com/oracle/graal/nodes/test/ObjectStampTest.java --- a/graal/com.oracle.graal.nodes.test/src/com/oracle/graal/nodes/test/ObjectStampTest.java Fri Sep 19 09:59:56 2014 +0200 +++ b/graal/com.oracle.graal.nodes.test/src/com/oracle/graal/nodes/test/ObjectStampTest.java Thu Sep 18 17:42:46 2014 +0200 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2014, 2014, 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 @@ -26,49 +26,25 @@ import com.oracle.graal.api.meta.*; import com.oracle.graal.compiler.common.type.*; -import com.oracle.graal.compiler.test.*; -public class ObjectStampTest extends GraalCompilerTest { - - protected static class A { - +public class ObjectStampTest extends AbstractObjectStampTest { + @Test + public void testInterfaceTrust0() { + Stamp notTrusted = StampFactory.declared(getType(I.class)); + Assert.assertEquals(StampFactory.object(), notTrusted); } - protected static class B extends A { - - } - - protected static class C extends B implements I { - - } - - protected static class D extends A { + private static interface TrustedI extends TrustedInterface { } - protected abstract static class E extends A { - - } - - protected interface I { - - } - - protected static Stamp join(Stamp a, Stamp b) { - Stamp ab = a.join(b); - Stamp ba = b.join(a); - Assert.assertEquals(ab, ba); - return ab; - } - - protected static Stamp meet(Stamp a, Stamp b) { - Stamp ab = a.meet(b); - Stamp ba = b.meet(a); - Assert.assertEquals(ab, ba); - return ab; - } - - protected ResolvedJavaType getType(Class clazz) { - return getMetaAccess().lookupJavaType(clazz); + @Test + public void testInterfaceTrust1() { + Stamp trusted = StampFactory.declared(getType(TrustedI.class)); + Assert.assertNotEquals(StampFactory.object(), trusted); + Assert.assertTrue("Should be an AbstractObjectStamp", trusted instanceof AbstractObjectStamp); + AbstractObjectStamp trustedObjectStamp = (AbstractObjectStamp) trusted; + Assert.assertNotNull(trustedObjectStamp.type()); + Assert.assertTrue("Should be an interface", trustedObjectStamp.type().isInterface()); } } diff -r a8eb7473d58a -r ac6e25901d62 graal/com.oracle.graal.word/src/com/oracle/graal/word/WordBase.java --- a/graal/com.oracle.graal.word/src/com/oracle/graal/word/WordBase.java Fri Sep 19 09:59:56 2014 +0200 +++ b/graal/com.oracle.graal.word/src/com/oracle/graal/word/WordBase.java Thu Sep 18 17:42:46 2014 +0200 @@ -22,7 +22,9 @@ */ package com.oracle.graal.word; -public interface WordBase { +import com.oracle.graal.api.meta.*; + +public interface WordBase extends TrustedInterface { long rawValue(); }