diff --git a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/infix/AccessField.java b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/infix/AccessField.java index c40c9d62e8fd21afbe53ed68cf82510f9f19a082..93c2ae565f2a4846962586a08636a541dceb9ad6 100644 --- a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/infix/AccessField.java +++ b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/infix/AccessField.java @@ -32,7 +32,6 @@ import com.oracle.truffle.api.dsl.Fallback; import com.oracle.truffle.api.dsl.NodeChild; import com.oracle.truffle.api.dsl.Specialization; import com.oracle.truffle.api.frame.FrameDescriptor; -import com.oracle.truffle.api.frame.VirtualFrame; import com.oracle.truffle.api.profiles.BranchProfile; import com.oracle.truffle.api.profiles.ConditionProfile; import com.oracle.truffle.r.nodes.access.vector.ElementAccessMode; @@ -119,7 +118,8 @@ public abstract class AccessField extends RBuiltinNode.Arg2 { if (queryId != null) { final QIRVariable v = new QIRVariable(null, "t"); RContext.queries.set((int) queryId, new QIRProject(getSourceSection(), - new QIRLambda(null, null, v, new QIRTcons(null, field, new QIRTdestr(null, v, field), QIRTnil.instance), new FrameDescriptor()), RContext.queries.get((int) queryId))); + new QIRLambda(null, null, v, new QIRTcons(null, field, new QIRTdestr(null, v, field), QIRTnil.getInstance()), new FrameDescriptor()), + RContext.queries.get((int) queryId))); return container; } } 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 d121b31e9afca3ff4518e438d8f8fd33d678cf7a..10a68241a3b73867d396ef8df4efb1fc7a47fd49 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 @@ -72,20 +72,20 @@ public final class QIRInterface { throw new RuntimeException("Internal error: query returned a non-list type"); // The query returned an empty result - if (qirRes == QIRLnil.instance) + if (qirRes == QIRLnil.getInstance()) return new HashMap<>(); final Map<String, List<Object>> results = new HashMap<>(); // Inhabit the result columns with empty lists (we need the keys to create the lists) TODO: // Find a way to add the first row here and skip it on the next loop - for (QIRNode firstRow = ((QIRLcons) qirRes).value; firstRow != QIRTnil.instance; firstRow = ((QIRTcons) firstRow).tail) - results.put(((QIRTcons) firstRow).id, new ArrayList<>()); + for (QIRNode firstRow = ((QIRLcons) qirRes).getValue(); firstRow != QIRTnil.getInstance(); firstRow = ((QIRTcons) firstRow).getTail()) + results.put(((QIRTcons) firstRow).getId(), new ArrayList<>()); // Process the rest of the rows TODO: Error if a row has unknown column - for (; qirRes != QIRLnil.instance; qirRes = ((QIRLcons) qirRes).tail) - for (QIRNode row = ((QIRLcons) qirRes).value; row != QIRTnil.instance; row = ((QIRTcons) row).tail) - results.get(((QIRTcons) row).id).add(QIRToRType(((QIRTcons) row).value)); + for (; qirRes != QIRLnil.getInstance(); qirRes = ((QIRLcons) qirRes).getTail()) + for (QIRNode row = ((QIRLcons) qirRes).getValue(); row != QIRTnil.getInstance(); row = ((QIRTcons) row).getTail()) + results.get(((QIRTcons) row).getId()).add(QIRToRType(((QIRTcons) row).getValue())); return results; } @@ -114,7 +114,7 @@ public final class QIRInterface { // Remove // this // hack - : RToQIRType(fv.sourceSection, varSlot != null ? frame.getValue(varSlot) : RContext.getInstance().lookupBuiltin(varName))); + : RToQIRType(fv.getSourceSection(), varSlot != null ? frame.getValue(varSlot) : RContext.getInstance().lookupBuiltin(varName))); } return query; } @@ -136,7 +136,7 @@ public final class QIRInterface { @SuppressWarnings("unchecked") static final <T> T QIRToRType(final QIRNode value) throws UnsupportedOperationException { if (value instanceof QIRBaseValue<?>) - return ((QIRBaseValue<T>) value).value; + return ((QIRBaseValue<T>) value).getValue(); throw new RuntimeException("Unsupported value: " + value); } @@ -172,7 +172,7 @@ public final class QIRInterface { if (tableName != null && dbName != null && configFile != null) // The object is a table return new QIRTable(src, new QIRString(null, tableName), new QIRString(null, dbName), new QIRString(null, configFile), new QIRString(null, schemaName)); // else, the object is considered a tuple - QIRTuple tuple = QIRTnil.instance; + QIRTuple tuple = QIRTnil.getInstance(); for (final Object x : env.getFrame().getFrameDescriptor().getIdentifiers()) tuple = new QIRTcons(src, (String) x, RToQIRType(src, env.get((String) x)), tuple); return tuple; @@ -184,7 +184,7 @@ public final class QIRInterface { final RFunction fun = (RFunction) value; switch (fun.getName()) { case "new.env": - return new QIRLambda(src, "new.env", new QIRVariable(null, "_", null), QIRTnil.instance, new FrameDescriptor()); + return new QIRLambda(src, "new.env", new QIRVariable(null, "_", null), QIRTnil.getInstance(), new FrameDescriptor()); case "return": case "(": case "query.force": { @@ -262,17 +262,17 @@ public final class QIRInterface { if (arg instanceof QIRTuple) { final String argName = "res" + argIndex; code += "\n" + argName + " = new.env();"; - for (QIRNode v = arg; v instanceof QIRTcons; v = ((QIRTcons) v).tail) { - final Object value = QIRToRType(((QIRTcons) v).value); - code += "\n" + argName + "$" + ((QIRTcons) v).id + " = " + (value instanceof String ? "\"" + value + "\"" : value) + ";"; + for (QIRNode v = arg; v instanceof QIRTcons; v = ((QIRTcons) v).getTail()) { + final Object value = QIRToRType(((QIRTcons) v).getValue()); + code += "\n" + argName + "$" + ((QIRTcons) v).getId() + " = " + (value instanceof String ? "\"" + value + "\"" : value) + ";"; } applyArgs.add(argName); } else applyArgs.add(QIRToRType(arg).toString()); } - code += "\n(" + fun.code + ")(" + applyArgs.stream().collect(Collectors.joining(",")) + ")"; - return fun instanceof QIRExportableTruffleNode ? new QIRExportableTruffleNode(fun.sourceSection, "r", QIRInterface::execute, fun.apply, code) - : new QIRUnexportableTruffleNode(fun.sourceSection, "r", QIRInterface::execute, fun.apply, code); + code += "\n(" + fun.getCode() + ")(" + applyArgs.stream().collect(Collectors.joining(",")) + ")"; + return fun instanceof QIRExportableTruffleNode ? new QIRExportableTruffleNode(fun.getSourceSection(), "r", QIRInterface::execute, QIRInterface::apply, code) + : new QIRUnexportableTruffleNode(fun.getSourceSection(), "r", QIRInterface::execute, QIRInterface::apply, code); } private static final QIRNode RFunctionToQIRType(final SourceSection src, final String funName, final FunctionDefinitionNode fun) { 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 fe81efe83c2cbe4f369150ac75db1af4d2064326..7bd42ba9fc0acfc1bcb7e8fa504b6a660921785c 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 @@ -1,6 +1,8 @@ package com.oracle.truffle.r.nodes.qirinterface; +import java.util.ArrayDeque; import java.util.Arrays; +import java.util.Deque; import java.util.List; import java.util.stream.Collectors; @@ -175,12 +177,12 @@ public final class QIRTranslateVisitor implements RSyntaxNodeVisitor<QIRNode> { private static final QIRNode visitBuiltin(final SourceSection src, final String name, final List<QIRNode> args) { switch (name) { case "c": - QIRList res = QIRLnil.instance; + QIRList res = QIRLnil.getInstance(); for (int i = args.size() - 1; i >= 0; i--) res = new QIRLcons(src, args.get(i), res); return res; case "$": - return new QIRTdestr(src, args.get(0), ((QIRString) args.get(1)).value); + return new QIRTdestr(src, args.get(0), ((QIRString) args.get(1)).getValue()); case "+": return QIRPlusNodeGen.create(src, args.get(0), args.get(1)); case "-": @@ -253,11 +255,15 @@ public final class QIRTranslateVisitor implements RSyntaxNodeVisitor<QIRNode> { @Override public final QIRNode visit(final ROrderNode order) { - QIRNode f = order.getIsAscending().asRSyntaxNode().accept(this); - - for (QIRNode l = ((QIRLambda) f).body; l instanceof QIRLcons; l = ((QIRLcons) l).tail) - ((QIRLcons) l).value = QIRBoolean.create(((QIRLcons) l).value.equals(new QIRNumber(null, 1))); - return new QIRSortBy(order.getSourceSection(), order.getOrder().asRSyntaxNode().accept(this), f, + final QIRNode f = order.getIsAscending().asRSyntaxNode().accept(this); + final Deque<QIRBoolean> tmp = new ArrayDeque<>(); + QIRNode res = QIRLnil.getInstance(); + + for (QIRNode l = ((QIRLambda) f).getBody(); l instanceof QIRLcons; l = ((QIRLcons) l).getTail()) + tmp.add(QIRBoolean.create(((QIRLcons) l).getValue().equals(new QIRNumber(null, 1)))); + for (QIRBoolean b : tmp) + res = new QIRLcons(dummy, b, res); + return new QIRSortBy(order.getSourceSection(), order.getOrder().asRSyntaxNode().accept(this), res, order.getQuery().asRSyntaxNode().accept(this)); }