From b73f13eda31a5d1ed3532e08777af479f6bb0d7b Mon Sep 17 00:00:00 2001 From: Julien Lopez <julien.lopez@lri.fr> Date: Fri, 9 Dec 2016 16:59:05 +0100 Subject: [PATCH] Queries are now no longer syntactic constructs - Add builtin query - Add ReplacementDispatcher to visitors - Remove obsolete parser modifications for queries --- .../r/nodes/builtin/base/BasePackage.java | 5 +++ .../r/nodes/builtin/rquery/RQuery.java | 43 +++++++++++++++++++ .../oracle/truffle/r/nodes/RASTBuilder.java | 25 ----------- .../truffle/r/nodes/RSyntaxNodeVisitor.java | 4 ++ .../r/nodes/qirinterface/QIRInterface.java | 10 ++--- .../qirinterface/QIRTranslateVisitor.java | 9 +++- .../r/nodes/query/RQIRWrapperNode.java | 2 +- .../truffle/r/nodes/query/RQueryVisitor.java | 5 +++ .../src/com/oracle/truffle/r/parser/R.g | 8 +--- .../truffle/r/runtime/nodes/RCodeBuilder.java | 20 --------- 10 files changed, 71 insertions(+), 60 deletions(-) create mode 100644 com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/rquery/RQuery.java diff --git a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/BasePackage.java b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/BasePackage.java index d3871d27be..4a728419ba 100644 --- a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/BasePackage.java +++ b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/BasePackage.java @@ -120,6 +120,8 @@ import com.oracle.truffle.r.nodes.builtin.fastr.FastRTry; import com.oracle.truffle.r.nodes.builtin.fastr.FastRTryNodeGen; import com.oracle.truffle.r.nodes.builtin.fastr.FastrDqrls; import com.oracle.truffle.r.nodes.builtin.fastr.FastrDqrlsNodeGen; +import com.oracle.truffle.r.nodes.builtin.rquery.RQuery; +import com.oracle.truffle.r.nodes.builtin.rquery.RQueryNodeGen; import com.oracle.truffle.r.nodes.unary.UnaryNotNode; import com.oracle.truffle.r.nodes.unary.UnaryNotNodeGen; import com.oracle.truffle.r.runtime.RVisibility; @@ -677,6 +679,9 @@ public class BasePackage extends RBuiltinPackage { add(WhichFunctions.WhichMin.class, WhichFunctions.WhichMin::create); add(Xtfrm.class, XtfrmNodeGen::create); + // query + add(RQuery.class, RQueryNodeGen::create); + // infix functions add(Subscript.class, SubscriptNodeGen::create, Subscript::special); add(Subscript.DefaultBuiltin.class, SubscriptNodeGen::create, Subscript::special); diff --git a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/rquery/RQuery.java b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/rquery/RQuery.java new file mode 100644 index 0000000000..a7f8f803a1 --- /dev/null +++ b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/rquery/RQuery.java @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2014, 2016, 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.r.nodes.builtin.rquery; + +import static com.oracle.truffle.r.runtime.builtins.RBehavior.IO; +import static com.oracle.truffle.r.runtime.builtins.RBuiltinKind.PRIMITIVE; + +import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; +import com.oracle.truffle.api.dsl.Specialization; +import com.oracle.truffle.r.nodes.builtin.RBuiltinNode; +import com.oracle.truffle.r.runtime.RVisibility; +import com.oracle.truffle.r.runtime.builtins.RBuiltin; +import com.oracle.truffle.r.runtime.data.RDataFactory; +import com.oracle.truffle.r.runtime.env.REnvironment; + +@RBuiltin(name = "query", visibility = RVisibility.ON, kind = PRIMITIVE, parameterNames = {}, behavior = IO) +public abstract class RQuery extends RBuiltinNode { + @Specialization + @TruffleBoundary + protected REnvironment createQuery() { + return RDataFactory.createNewEnv(null); + } +} diff --git a/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/RASTBuilder.java b/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/RASTBuilder.java index 2b5e5d7c94..4b35745a57 100644 --- a/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/RASTBuilder.java +++ b/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/RASTBuilder.java @@ -50,15 +50,10 @@ import com.oracle.truffle.r.nodes.function.RCallSpecialNode; import com.oracle.truffle.r.nodes.function.SaveArgumentsNode; import com.oracle.truffle.r.nodes.function.WrapDefaultArgumentNode; import com.oracle.truffle.r.nodes.function.signature.MissingNode; -import com.oracle.truffle.r.nodes.query.RFromNode; -import com.oracle.truffle.r.nodes.query.RQueryVisitor; -import com.oracle.truffle.r.nodes.query.RSelectNode; -import com.oracle.truffle.r.nodes.query.RWhereNode; import com.oracle.truffle.r.runtime.ArgumentsSignature; import com.oracle.truffle.r.runtime.FastROptions; import com.oracle.truffle.r.runtime.RRuntime; import com.oracle.truffle.r.runtime.builtins.FastPathFactory; -import com.oracle.truffle.r.runtime.context.RContext; import com.oracle.truffle.r.runtime.data.REmpty; import com.oracle.truffle.r.runtime.data.RShareable; import com.oracle.truffle.r.runtime.env.frame.FrameSlotChangeMonitor; @@ -153,26 +148,6 @@ public final class RASTBuilder implements RCodeBuilder<RSyntaxNode> { return RCallSpecialNode.createCall(source, lhs.asRNode(), createSignature(args), createArguments(args)); } - @Override - public final RSyntaxNode handleQueries(final RSyntaxNode expression) { - return expression.accept(new RQueryVisitor(RContext.getInstance())); - } - - @Override - public final RSyntaxNode select(final SourceSection source, final RSyntaxNode formatter, final RSyntaxNode child) { - return new RSelectNode(source, formatter.asRNode(), child.asRNode()); - } - - @Override - public final RSyntaxNode from(final SourceSection source, final RSyntaxNode child) { - return new RFromNode(source, child.asRNode()); - } - - @Override - public final RSyntaxNode where(final SourceSection source, final RSyntaxNode filter, final RSyntaxNode child) { - return new RWhereNode(source, filter.asRNode(), child.asRNode()); - } - private static ArgumentsSignature createSignature(List<Argument<RSyntaxNode>> args) { String[] argumentNames = args.stream().map(arg -> arg.name).toArray(String[]::new); ArgumentsSignature signature = ArgumentsSignature.get(argumentNames); diff --git a/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/RSyntaxNodeVisitor.java b/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/RSyntaxNodeVisitor.java index f45cbb5fc0..ba3fb48f7d 100644 --- a/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/RSyntaxNodeVisitor.java +++ b/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/RSyntaxNodeVisitor.java @@ -34,6 +34,8 @@ public interface RSyntaxNodeVisitor<T> extends IRSyntaxNodeVisitor<T> { public abstract T visit(final RCallSpecialNode callNode); + public abstract T visit(final ReplacementDispatchNode repl); + public abstract T visit(final RQIRWrapperNode qir); public abstract T visit(final RSelectNode select); @@ -76,6 +78,8 @@ public interface RSyntaxNodeVisitor<T> extends IRSyntaxNodeVisitor<T> { return visit((RCallNode) node); if (node instanceof RCallSpecialNode) return visit((RCallSpecialNode) node); + if (node instanceof ReplacementDispatchNode) + return visit((ReplacementDispatchNode) node); if (node instanceof RQIRWrapperNode) return visit((RQIRWrapperNode) node); if (node instanceof RSelectNode) diff --git a/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/qirinterface/QIRInterface.java b/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/qirinterface/QIRInterface.java index d111ac4664..bb18c7aca4 100644 --- a/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/qirinterface/QIRInterface.java +++ b/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/qirinterface/QIRInterface.java @@ -160,10 +160,10 @@ public final class QIRInterface { } /** - * Translates a QIR expression into a QSL statement. + * Translates a QIR expression into a R statement. * * @param value The QIR expression to translate - * @return The translation of the QIR expression in QSL + * @return The translation of the QIR expression in R * @throws UnsupportedOperationException If the type of the value is not supported. */ @SuppressWarnings("unchecked") @@ -174,11 +174,11 @@ public final class QIRInterface { } /** - * Translates a QSL statement into a QIR expression. + * Translates a R statement into a QIR expression. * * @param src The {@link SourceSection} of the given value - * @param value The QSL statement to translate - * @return The translation of the QSL statement in QIR + * @param value The R statement to translate + * @return The translation of the R statement in QIR */ static final <T> QIRNode RToQIRType(final SourceSection src, final T value) { if (value instanceof BigInteger) diff --git a/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/qirinterface/QIRTranslateVisitor.java b/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/qirinterface/QIRTranslateVisitor.java index d573447734..fff041f2ca 100644 --- a/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/qirinterface/QIRTranslateVisitor.java +++ b/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/qirinterface/QIRTranslateVisitor.java @@ -72,7 +72,7 @@ public final class QIRTranslateVisitor implements RSyntaxNodeVisitor<QIRNode> { final Object name = ((WriteLocalFrameVariableNode) child).getName(); final RNode value = ((WriteLocalFrameVariableNode) child).getRhs(); - // If the assignment value is a QSLReadArgumentNode, then we translate to a lambda. + // If the assignment value reads an argument, then we translate to a lambda. result = new QIRLambda(dummy, null, new QIRVariable(dummy, (String) name, null), result); // Else we apply STRAD-ASSIGN-ID normally if (!(value instanceof AccessArgumentNode)) @@ -90,7 +90,7 @@ public final class QIRTranslateVisitor implements RSyntaxNodeVisitor<QIRNode> { @Override public final QIRNode visit(final WriteLocalFrameVariableNode var) { - // STRAD-ASSIGN-ID should be handled in QSLBlockNode. + // STRAD-ASSIGN-ID should be handled in BlockNode. throw new RuntimeException("Error in translation to QIR: should not have visited an assignment."); } @@ -118,6 +118,11 @@ public final class QIRTranslateVisitor implements RSyntaxNodeVisitor<QIRNode> { throw new RuntimeException("Can't translate R special call to QIR."); } + @Override + public final QIRNode visit(final ReplacementDispatchNode repl) { + throw new RuntimeException("Can't translate R replacement node to QIR."); + } + /** * Translation of a subquery. */ diff --git a/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/query/RQIRWrapperNode.java b/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/query/RQIRWrapperNode.java index 0067f65358..d6acb68a46 100644 --- a/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/query/RQIRWrapperNode.java +++ b/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/query/RQIRWrapperNode.java @@ -43,7 +43,7 @@ public final class RQIRWrapperNode extends RSourceSectionNode { } @Override - public DynamicObject execute(VirtualFrame frame) { + public final DynamicObject execute(VirtualFrame frame) { final int instanceId = context.envs.get(id) == null ? id : context.createFreshQueryFrom(id); context.envs.set(instanceId, frame); return createQuery(instanceId); diff --git a/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/query/RQueryVisitor.java b/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/query/RQueryVisitor.java index 2a919905ff..3f2f859189 100644 --- a/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/query/RQueryVisitor.java +++ b/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/query/RQueryVisitor.java @@ -83,6 +83,11 @@ public final class RQueryVisitor implements RSyntaxNodeVisitor<RSyntaxNode> { Arrays.asList((RSyntaxNode[]) call.getSyntaxArguments()).stream().map(arg -> arg.accept(this)).toArray(RSyntaxNode[]::new)); } + @Override + public final RSyntaxNode visit(final ReplacementDispatchNode repl) { + return repl; + } + @Override public final RSyntaxNode visit(final RQIRWrapperNode qir) { return qir; diff --git a/com.oracle.truffle.r.parser/src/com/oracle/truffle/r/parser/R.g b/com.oracle.truffle.r.parser/src/com/oracle/truffle/r/parser/R.g index 667b456252..78c92fcb0f 100644 --- a/com.oracle.truffle.r.parser/src/com/oracle/truffle/r/parser/R.g +++ b/com.oracle.truffle.r.parser/src/com/oracle/truffle/r/parser/R.g @@ -173,7 +173,7 @@ script returns [List<T> v] throw new RecognitionException(input); } } - : n_ ( s=statement { $v.add(builder.handleQueries($s.v)); })* + : n_ ( s=statement { $v.add($s.v); })* ; root_function [String name] returns [RootCallTarget v] @@ -409,13 +409,8 @@ simple_expr returns [T v] | op=LPAR n_ ea=expr_or_assign n_ y=RPAR { $v = builder.call(src($op, $y), operator($op), $ea.v); } | s=sequence { $v = $s.v; } | e=expr_wo_assign { $v = $e.v; } - | q=query { $v = $q.v; } ; -query returns [T v] - : op=SELECT n_ LPAR n_ formatter=expr n_ COMMA n_ child=expr end=RPAR { $v = builder.select(src($op, $end), $formatter.v, $child.v); } - ; - number returns [T v] : i=INTEGER { double value = RRuntime.string2doubleNoCheck($i.text); @@ -567,7 +562,6 @@ IF : 'if' ; ELSE : 'else' ; NEXT : 'next' ; BREAK : 'break' ; -SELECT : 'select' ; WS : ('\u0009'|'\u0020'|'\u00A0') { $channel=HIDDEN; } ; NEWLINE : LINE_BREAK { if(incompleteNesting > 0) $channel=HIDDEN; } ; diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/nodes/RCodeBuilder.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/nodes/RCodeBuilder.java index 45d89f4abd..45eb98dcea 100644 --- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/nodes/RCodeBuilder.java +++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/nodes/RCodeBuilder.java @@ -94,26 +94,6 @@ public interface RCodeBuilder<T> { */ T call(SourceSection source, T lhs, List<Argument<T>> arguments); - /** - * Processes queries in an expression to make them ready for evaluation. - */ - T handleQueries(final T expression); - - /** - * Creates a select query. - */ - T select(final SourceSection source, final T formatter, final T child); - - /** - * Creates a from query. - */ - T from(final SourceSection source, final T child); - - /** - * Creates a where query. - */ - T where(final SourceSection source, final T filter, final T child); - /** * Creates a constant, the value is expected to be one of FastR's scalar types (byte, int, * double, RComplex, String, RNull). -- GitLab