# HG changeset patch # User Chris Seaton # Date 1386950010 0 # Node ID f28ea693056f39037245424d9c5a2a3d331eae36 # Parent e585ac5a385d9a07722f78e1aa18faf4888ba9b5 New assumption utilities. diff -r e585ac5a385d -r f28ea693056f graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/utilities/AlwaysValidAssumptionTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/utilities/AlwaysValidAssumptionTest.java Fri Dec 13 15:53:30 2013 +0000 @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2013, 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.truffle.api.test.utilities; + +import static org.junit.Assert.*; +import org.junit.*; + +import com.oracle.truffle.api.nodes.*; +import com.oracle.truffle.api.utilities.*; + +public class AlwaysValidAssumptionTest { + + @Test + public void testCheck() throws InvalidAssumptionException { + final AlwaysValidAssumption assumption = new AlwaysValidAssumption(); + assumption.check(); + } + + @Test + public void testIsValid() { + final AlwaysValidAssumption assumption = new AlwaysValidAssumption(); + assertTrue(assumption.isValid()); + } + + @Test(expected = UnsupportedOperationException.class) + public void testCannotInvalidate() { + final AlwaysValidAssumption assumption = new AlwaysValidAssumption(); + assumption.invalidate(); + } + +} diff -r e585ac5a385d -r f28ea693056f graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/utilities/AssumedValueTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/utilities/AssumedValueTest.java Fri Dec 13 15:53:30 2013 +0000 @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2013, 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.truffle.api.test.utilities; + +import static org.junit.Assert.*; +import org.junit.*; + +import com.oracle.truffle.api.utilities.*; + +public class AssumedValueTest { + + @Test + public void testGet() { + final AssumedValue assumedValue = new AssumedValue<>("assumed-value", "1"); + assertEquals("1", assumedValue.get()); + } + + @Test + public void testSet() { + final AssumedValue assumedValue = new AssumedValue<>("assumed-value", "1"); + assertEquals("1", assumedValue.get()); + assumedValue.set("2"); + assertEquals("2", assumedValue.get()); + } + +} diff -r e585ac5a385d -r f28ea693056f graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/utilities/CyclicAssumptionTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/utilities/CyclicAssumptionTest.java Fri Dec 13 15:53:30 2013 +0000 @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2013, 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.truffle.api.test.utilities; + +import static org.junit.Assert.*; + +import org.junit.*; + +import com.oracle.truffle.api.*; +import com.oracle.truffle.api.utilities.*; + +public class CyclicAssumptionTest { + + @Test + public void testIsValid() { + final CyclicAssumption assumption = new CyclicAssumption("cyclic-assumption"); + assertTrue(assumption.getAssumption().isValid()); + } + + @Test + public void testInvalidate() { + final CyclicAssumption cyclicAssumption = new CyclicAssumption("cyclic-assumption"); + + final Assumption firstAssumption = cyclicAssumption.getAssumption(); + assertEquals("cyclic-assumption", firstAssumption.getName()); + assertTrue(firstAssumption.isValid()); + + cyclicAssumption.invalidate(); + + assertFalse(firstAssumption.isValid()); + + final Assumption secondAssumption = cyclicAssumption.getAssumption(); + assertEquals("cyclic-assumption", secondAssumption.getName()); + assertTrue(secondAssumption.isValid()); + + cyclicAssumption.invalidate(); + + assertFalse(firstAssumption.isValid()); + assertFalse(secondAssumption.isValid()); + } + +} diff -r e585ac5a385d -r f28ea693056f graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/utilities/UnionAssumptionTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/utilities/UnionAssumptionTest.java Fri Dec 13 15:53:30 2013 +0000 @@ -0,0 +1,119 @@ +/* + * Copyright (c) 2013, 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.truffle.api.test.utilities; + +import static org.junit.Assert.*; + +import org.junit.*; + +import com.oracle.truffle.api.*; +import com.oracle.truffle.api.nodes.*; +import com.oracle.truffle.api.utilities.*; + +public class UnionAssumptionTest { + + @Test + public void testIsValid() { + final Assumption first = Truffle.getRuntime().createAssumption("first"); + final Assumption second = Truffle.getRuntime().createAssumption("second"); + final UnionAssumption union = new UnionAssumption(first, second); + assertTrue(union.isValid()); + } + + @Test + public void testCheck() throws InvalidAssumptionException { + final Assumption first = Truffle.getRuntime().createAssumption("first"); + final Assumption second = Truffle.getRuntime().createAssumption("second"); + final UnionAssumption union = new UnionAssumption(first, second); + union.check(); + } + + @Test + public void testFirstInvalidateIsValid() { + final Assumption first = Truffle.getRuntime().createAssumption("first"); + final Assumption second = Truffle.getRuntime().createAssumption("second"); + final UnionAssumption union = new UnionAssumption(first, second); + + first.invalidate(); + + assertFalse(union.isValid()); + } + + @Test(expected = InvalidAssumptionException.class) + public void testFirstInvalidateCheck() throws InvalidAssumptionException { + final Assumption first = Truffle.getRuntime().createAssumption("first"); + final Assumption second = Truffle.getRuntime().createAssumption("second"); + final UnionAssumption union = new UnionAssumption(first, second); + + first.invalidate(); + + union.check(); + } + + @Test + public void testSecondInvalidateIsValid() { + final Assumption first = Truffle.getRuntime().createAssumption("first"); + final Assumption second = Truffle.getRuntime().createAssumption("second"); + final UnionAssumption union = new UnionAssumption(first, second); + + second.invalidate(); + + assertFalse(union.isValid()); + } + + @Test(expected = InvalidAssumptionException.class) + public void testSecondInvalidateCheck() throws InvalidAssumptionException { + final Assumption first = Truffle.getRuntime().createAssumption("first"); + final Assumption second = Truffle.getRuntime().createAssumption("second"); + final UnionAssumption union = new UnionAssumption(first, second); + + second.invalidate(); + + union.check(); + } + + @Test + public void testBothInvalidateIsValid() { + final Assumption first = Truffle.getRuntime().createAssumption("first"); + final Assumption second = Truffle.getRuntime().createAssumption("second"); + final UnionAssumption union = new UnionAssumption(first, second); + + first.invalidate(); + second.invalidate(); + + assertFalse(union.isValid()); + } + + @Test(expected = InvalidAssumptionException.class) + public void testBothInvalidateCheck() throws InvalidAssumptionException { + final Assumption first = Truffle.getRuntime().createAssumption("first"); + final Assumption second = Truffle.getRuntime().createAssumption("second"); + final UnionAssumption union = new UnionAssumption(first, second); + + first.invalidate(); + second.invalidate(); + + union.check(); + } + +} diff -r e585ac5a385d -r f28ea693056f graal/com.oracle.truffle.api/src/com/oracle/truffle/api/utilities/AlwaysValidAssumption.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/utilities/AlwaysValidAssumption.java Fri Dec 13 15:53:30 2013 +0000 @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2013, 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. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * 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.truffle.api.utilities; + +import com.oracle.truffle.api.*; +import com.oracle.truffle.api.nodes.*; + +/** + * An assumption that is always valid. Used as a placeholder where an assumption is needed but never + * invalidated. + */ +public class AlwaysValidAssumption implements Assumption { + + public static final AlwaysValidAssumption INSTANCE = new AlwaysValidAssumption(); + + @Override + public void check() throws InvalidAssumptionException { + } + + @Override + public void invalidate() { + throw new UnsupportedOperationException("Cannot invalidation this assumption - it is always valid"); + } + + @Override + public String getName() { + return getClass().getName(); + } + + @Override + public boolean isValid() { + return true; + } + +} diff -r e585ac5a385d -r f28ea693056f graal/com.oracle.truffle.api/src/com/oracle/truffle/api/utilities/AssumedValue.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/utilities/AssumedValue.java Fri Dec 13 15:53:30 2013 +0000 @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2013, 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. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * 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.truffle.api.utilities; + +import com.oracle.truffle.api.*; +import com.oracle.truffle.api.CompilerDirectives.CompilationFinal; +import com.oracle.truffle.api.nodes.*; + +/** + * A {@link CompilationFinal} value combined with an {@link Assumption} to notify when it changes. + * Note that you should be careful that modifications to this value do not cause deoptimization + * loops. This could be by using a value that is monotonic. + */ +public class AssumedValue { + + @CompilationFinal private T value; + private final CyclicAssumption assumption; + + public AssumedValue(String name, T initialValue) { + assumption = new CyclicAssumption(name); + value = initialValue; + } + + /** + * Get the current value, updating it if it has been {@link #set}. The compiler may be able to + * make this method return a constant value, but still accommodate mutation. + */ + public T get() { + try { + assumption.getAssumption().check(); + } catch (InvalidAssumptionException e) { + // No need to rewrite anything - just pick up the new value + } + + return value; + } + + /** + * Set a new value, which will be picked up the next time {@link #get} is called. + */ + public void set(T newValue) { + value = newValue; + assumption.invalidate(); + } + +} diff -r e585ac5a385d -r f28ea693056f graal/com.oracle.truffle.api/src/com/oracle/truffle/api/utilities/CyclicAssumption.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/utilities/CyclicAssumption.java Fri Dec 13 15:53:30 2013 +0000 @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2013, 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. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * 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.truffle.api.utilities; + +import com.oracle.truffle.api.*; + +/** + * Holds an {@link Assumption}, and knows how to recreate it with the same properties on + * invalidation. Used so that mutability is isolated in this class, and all other classes that need + * an assumption that may be recreated can have a final reference to an object of this class. Note + * that you should be careful that repeated invalidations do not cause a deoptimization loop in that + * same way that you would with any other assumption. + */ +public class CyclicAssumption { + + private final String name; + private Assumption assumption; + + public CyclicAssumption(String name) { + this.name = name; + invalidate(); + } + + public void invalidate() { + if (assumption != null) { + assumption.invalidate(); + } + + assumption = Truffle.getRuntime().createAssumption(name); + } + + public Assumption getAssumption() { + return assumption; + } + +} diff -r e585ac5a385d -r f28ea693056f graal/com.oracle.truffle.api/src/com/oracle/truffle/api/utilities/UnionAssumption.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/utilities/UnionAssumption.java Fri Dec 13 15:53:30 2013 +0000 @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2013, 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. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * 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.truffle.api.utilities; + +import com.oracle.truffle.api.*; +import com.oracle.truffle.api.nodes.*; + +/** + * An assumption that combines two other assumptions. A check on this assumption checks both of the + * child assumptions. + */ +public class UnionAssumption implements Assumption { + + private final String name; + private final Assumption first; + private final Assumption second; + + public UnionAssumption(String name, Assumption first, Assumption second) { + this.name = name; + this.first = first; + this.second = second; + } + + public UnionAssumption(Assumption first, Assumption second) { + this(null, first, second); + } + + @Override + public void check() throws InvalidAssumptionException { + first.check(); + second.check(); + } + + @Override + public void invalidate() { + first.invalidate(); + second.invalidate(); + } + + @Override + public String getName() { + return name; + } + + @Override + public boolean isValid() { + return first.isValid() && second.isValid(); + } + +}