# HG changeset patch # User Christian Humer # Date 1426874380 -3600 # Node ID 596f6f9a7412a82416a1dc6ac5389bbfce1d329d # Parent cd59085cf0d89c25f84bcae8611dc140f3db39b3 Truffle: make loop node implementation exchangable for graal runtime versions. diff -r cd59085cf0d8 -r 596f6f9a7412 graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/DefaultLoopNodeFactory.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/DefaultLoopNodeFactory.java Fri Mar 20 18:59:40 2015 +0100 @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2015, 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.truffle; + +import com.oracle.graal.api.runtime.*; +import com.oracle.truffle.api.nodes.*; + +@ServiceProvider(LoopNodeFactory.class) +public class DefaultLoopNodeFactory implements LoopNodeFactory { + + public LoopNode create(RepeatingNode repeatingNode) { + return new OptimizedLoopNode(repeatingNode); + } + +} diff -r cd59085cf0d8 -r 596f6f9a7412 graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/GraalTruffleRuntime.java --- a/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/GraalTruffleRuntime.java Fri Mar 20 17:08:43 2015 +0100 +++ b/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/GraalTruffleRuntime.java Fri Mar 20 18:59:40 2015 +0100 @@ -56,10 +56,28 @@ private final List compilationListeners = new ArrayList<>(); private final GraalTruffleCompilationListener compilationNotify = new DispatchTruffleCompilationListener(); + private LoopNodeFactory loopNodeFactory; + public GraalTruffleRuntime() { Runtime.getRuntime().addShutdownHook(new Thread(this::shutdown)); } + private static T loadPrioritizedServiceProvider(Class clazz) { + ServiceLoader serviceLoader = ServiceLoader.load(clazz, GraalTruffleRuntime.class.getClassLoader()); + T bestFactory = null; + for (T factory : serviceLoader) { + if (bestFactory == null) { + bestFactory = factory; + } else if (factory.getPriority() > bestFactory.getPriority()) { + bestFactory = factory; + } + } + if (bestFactory == null) { + throw new IllegalStateException("Unable to load a factory for " + clazz.getName()); + } + return bestFactory; + } + protected void installDefaultListeners() { TraceCompilationFailureListener.install(this); TraceCompilationListener.install(this); @@ -80,11 +98,15 @@ } @Override - public LoopNode createLoopNode(RepeatingNode repeating) { - if (!(repeating instanceof Node)) { + public LoopNode createLoopNode(RepeatingNode repeatingNode) { + if (!(repeatingNode instanceof Node)) { throw new IllegalArgumentException("Repeating node must be of type Node."); } - return new OptimizedLoopNode(repeating); + if (loopNodeFactory == null) { + loopNodeFactory = loadPrioritizedServiceProvider(LoopNodeFactory.class); + } + + return loopNodeFactory.create(repeatingNode); } @Override diff -r cd59085cf0d8 -r 596f6f9a7412 graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/LoopNodeFactory.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/LoopNodeFactory.java Fri Mar 20 18:59:40 2015 +0100 @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2015, 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.truffle; + +import com.oracle.truffle.api.nodes.*; + +public interface LoopNodeFactory extends PrioritizedServiceProvider { + + LoopNode create(RepeatingNode repeatingNode); + +} diff -r cd59085cf0d8 -r 596f6f9a7412 graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/PrioritizedServiceProvider.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/PrioritizedServiceProvider.java Fri Mar 20 18:59:40 2015 +0100 @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2015, 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.truffle; + +public interface PrioritizedServiceProvider { + + default int getPriority() { + return 0; + } + +}