From adfcd2ff170ebd2ce2663797c2ab34702b64c5e5 Mon Sep 17 00:00:00 2001
From: Miloslav Metelka <miloslav.metelka@oracle.com>
Date: Fri, 24 Nov 2017 16:37:51 +0100
Subject: [PATCH] Added gctorture and gctorture2 builtins.

---
 .../r/nodes/builtin/base/BasePackage.java     |   4 +-
 .../truffle/r/nodes/builtin/base/Gc.java      |  59 -----------
 .../r/nodes/builtin/base/GcFunctions.java     | 100 ++++++++++++++++++
 .../truffle/r/nodes/builtin/InternalNode.java |   2 +-
 .../truffle/r/test/ExpectedTestOutput.test    |  14 ++-
 .../test/builtins/TestBuiltin_gctorture2.java |  13 ++-
 6 files changed, 125 insertions(+), 67 deletions(-)
 delete mode 100644 com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/Gc.java
 create mode 100644 com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/GcFunctions.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 f768bc4201..4172862ca8 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
@@ -510,7 +510,9 @@ public class BasePackage extends RBuiltinPackage {
         add(FrameFunctions.SysNFrame.class, FrameFunctionsFactory.SysNFrameNodeGen::create);
         add(FrameFunctions.SysParent.class, FrameFunctionsFactory.SysParentNodeGen::create);
         add(FrameFunctions.SysParents.class, FrameFunctionsFactory.SysParentsNodeGen::create);
-        add(Gc.class, GcNodeGen::create);
+        add(GcFunctions.Gc.class, GcFunctionsFactory.GcNodeGen::create);
+        add(GcFunctions.Gctorture.class, GcFunctionsFactory.GctortureNodeGen::create);
+        add(GcFunctions.Gctorture2.class, GcFunctionsFactory.Gctorture2NodeGen::create);
         add(GetClass.class, GetClassNodeGen::create);
         add(GetFunctions.Get.class, GetFunctionsFactory.GetNodeGen::create);
         add(GetFunctions.Get0.class, GetFunctionsFactory.Get0NodeGen::create);
diff --git a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/Gc.java b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/Gc.java
deleted file mode 100644
index 7a5326f066..0000000000
--- a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/Gc.java
+++ /dev/null
@@ -1,59 +0,0 @@
-/*
- * Copyright (c) 2014, 2017, 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.base;
-
-import static com.oracle.truffle.r.nodes.builtin.CastBuilder.Predef.toBoolean;
-import static com.oracle.truffle.r.runtime.builtins.RBehavior.COMPLEX;
-import static com.oracle.truffle.r.runtime.builtins.RBuiltinKind.INTERNAL;
-
-import java.util.Arrays;
-
-import com.oracle.truffle.api.dsl.Specialization;
-import com.oracle.truffle.r.nodes.builtin.RBuiltinNode;
-import com.oracle.truffle.r.runtime.RRuntime;
-import com.oracle.truffle.r.runtime.builtins.RBuiltin;
-import com.oracle.truffle.r.runtime.data.RDataFactory;
-import com.oracle.truffle.r.runtime.data.RDoubleVector;
-
-@RBuiltin(name = "gc", kind = INTERNAL, parameterNames = {"verbose", "reset"}, behavior = COMPLEX)
-public abstract class Gc extends RBuiltinNode.Arg2 {
-
-    static {
-        Casts casts = new Casts(Gc.class);
-        casts.arg("verbose").asLogicalVector().findFirst().map(toBoolean());
-        casts.arg("reset").asLogicalVector().findFirst().map(toBoolean());
-    }
-
-    @SuppressWarnings("unused")
-    @Specialization
-    protected RDoubleVector gc(boolean verbose, boolean reset) {
-        /*
-         * It is rarely advisable to actually force a gc in Java, therefore we simply ignore this
-         * builtin.
-         */
-        // TODO: somehow produce the (semi?) correct values
-        double[] data = new double[14];
-        Arrays.fill(data, RRuntime.DOUBLE_NA);
-        return RDataFactory.createDoubleVector(data, RDataFactory.INCOMPLETE_VECTOR);
-    }
-}
diff --git a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/GcFunctions.java b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/GcFunctions.java
new file mode 100644
index 0000000000..3dafaefebb
--- /dev/null
+++ b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/GcFunctions.java
@@ -0,0 +1,100 @@
+/*
+ * Copyright (c) 2015, 2017, 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.base;
+
+import static com.oracle.truffle.r.nodes.builtin.CastBuilder.Predef.toBoolean;
+import static com.oracle.truffle.r.runtime.builtins.RBehavior.COMPLEX;
+import static com.oracle.truffle.r.runtime.builtins.RBehavior.PURE;
+import static com.oracle.truffle.r.runtime.builtins.RBuiltinKind.INTERNAL;
+import static com.oracle.truffle.r.runtime.RVisibility.OFF;
+
+import com.oracle.truffle.api.dsl.Specialization;
+import com.oracle.truffle.r.nodes.builtin.RBuiltinNode;
+import com.oracle.truffle.r.runtime.RRuntime;
+import com.oracle.truffle.r.runtime.builtins.RBuiltin;
+import com.oracle.truffle.r.runtime.data.RDataFactory;
+import com.oracle.truffle.r.runtime.data.RDoubleVector;
+import com.oracle.truffle.r.runtime.data.RNull;
+import java.util.Arrays;
+
+/**
+ * Implementation of GC related builtins.
+ */
+
+public final class GcFunctions {
+
+    @RBuiltin(name = "gc", kind = INTERNAL, parameterNames = {"verbose", "reset"}, behavior = COMPLEX)
+    public abstract static class Gc extends RBuiltinNode.Arg2 {
+
+        static {
+            Casts casts = new Casts(Gc.class);
+            casts.arg("verbose").asLogicalVector().findFirst().map(toBoolean());
+            casts.arg("reset").asLogicalVector().findFirst().map(toBoolean());
+        }
+
+        @SuppressWarnings("unused")
+        @Specialization
+        protected RDoubleVector gc(boolean verbose, boolean reset) {
+            /*
+             * It is rarely advisable to actually force a gc in Java, therefore we simply ignore
+             * this builtin.
+             */
+            // TODO: somehow produce the (semi?) correct values
+            double[] data = new double[14];
+            Arrays.fill(data, RRuntime.DOUBLE_NA);
+            return RDataFactory.createDoubleVector(data, RDataFactory.INCOMPLETE_VECTOR);
+        }
+
+    }
+
+    @RBuiltin(name = "gctorture", visibility = OFF, kind = INTERNAL, parameterNames = "on", behavior = PURE)
+    public abstract static class Gctorture extends RBuiltinNode.Arg1 {
+
+        static {
+            Casts casts = new Casts(Gctorture.class);
+            casts.arg("on").allowNull().asLogicalVector().findFirst().map(toBoolean());
+        }
+
+        @Specialization
+        protected Object gctorture(@SuppressWarnings("unused") Object on) {
+            return RNull.instance;
+        }
+    }
+
+    @RBuiltin(name = "gctorture2", kind = INTERNAL, parameterNames = {"step", "wait", "inhibit_release"}, behavior = PURE)
+    public abstract static class Gctorture2 extends RBuiltinNode.Arg3 {
+
+        static {
+            Casts casts = new Casts(Gctorture2.class);
+            casts.arg("step").allowNull().asIntegerVector().findFirst();
+            casts.arg("wait").allowNull().asIntegerVector().findFirst();
+            casts.arg("inhibit_release").allowNull().asLogicalVector().findFirst().map(toBoolean());
+        }
+
+        @Specialization
+        protected Object gctorture2(@SuppressWarnings("unused") Object step, @SuppressWarnings("unused") Object wait, @SuppressWarnings("unused") Object inhibitRelease) {
+            return 0;
+        }
+    }
+
+}
diff --git a/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/builtin/InternalNode.java b/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/builtin/InternalNode.java
index 4e27e47919..dbff8a0ce6 100644
--- a/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/builtin/InternalNode.java
+++ b/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/builtin/InternalNode.java
@@ -347,7 +347,7 @@ public abstract class InternalNode extends OperatorNode {
                     "qweibull", "dnchisq", "pnchisq", "qnchisq", "dnt", "pnt", "qnt", "dwilcox", "pwilcox", "qwilcox", "besselI", "besselK", "dnbinom_mu", "pnbinom_mu", "qnbinom_mu", "dhyper",
                     "phyper", "qhyper", "dnbeta", "pnbeta", "qnbeta", "dnf", "pnf", "qnf", "dtukey", "ptukey", "qtukey", "rchisq", "rexp", "rgeom", "rpois", "rt", "rsignrank", "rbeta", "rbinom",
                     "rcauchy", "rf", "rgamma", "rlnorm", "rlogis", "rnbinom", "rnbinom_mu", "rnchisq", "rnorm", "runif", "rweibull", "rwilcox", "rhyper",
-                    "format.info", "grepRaw", "regexec", "adist", "aregexec", "chartr", "strtrim", "eapply", "machine", "save", "dump", "prmatrix", "gcinfo", "gctorture", "gctorture2",
+                    "format.info", "grepRaw", "regexec", "adist", "aregexec", "chartr", "strtrim", "eapply", "machine", "save", "dump", "prmatrix", "gcinfo",
                     "memory.profile", "sys.on.exit", "builtins", "bodyCode", "rapply", "inspect",
                     "mem.limits", "capabilitiesX11", "Cstack_info", "file.choose", "polyroot",
                     "setNumMathThreads", "setMaxNumMathThreads", "isatty", "isIncomplete", "pipe", "fifo", "unz", "truncate", "rawConnection",
diff --git a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/ExpectedTestOutput.test b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/ExpectedTestOutput.test
index 7713a1a6af..78298764ca 100644
--- a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/ExpectedTestOutput.test
+++ b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/ExpectedTestOutput.test
@@ -27011,11 +27011,21 @@ In gamma(argv[[1]]) : NaNs produced
 #argv <- list(FALSE); .Internal(gcinfo(argv[[1]]))
 [1] FALSE
 
-##com.oracle.truffle.r.test.builtins.TestBuiltin_gctorture2.testgctorture21#Ignored.Unimplemented#
+##com.oracle.truffle.r.test.builtins.TestBuiltin_gctorture2.testgctorture#
+#argv <- list(FALSE); .Internal(gctorture(argv[[1]]))
+
+##com.oracle.truffle.r.test.builtins.TestBuiltin_gctorture2.testgctorture#
+#argv <- list(NULL); .Internal(gctorture(argv[[1]]))
+
+##com.oracle.truffle.r.test.builtins.TestBuiltin_gctorture2.testgctorture21#
 #argv <- list(NULL, NULL, FALSE); .Internal(gctorture2(argv[[1]], argv[[2]], argv[[3]]))
 [1] 0
 
-##com.oracle.truffle.r.test.builtins.TestBuiltin_gctorture2.testgctorture22#Ignored.Unimplemented#
+##com.oracle.truffle.r.test.builtins.TestBuiltin_gctorture2.testgctorture21#
+#argv <- list(NULL, NULL, NULL); .Internal(gctorture2(argv[[1]], argv[[2]], argv[[3]]))
+[1] 0
+
+##com.oracle.truffle.r.test.builtins.TestBuiltin_gctorture2.testgctorture22#
 #argv <- list(FALSE, FALSE, FALSE); .Internal(gctorture2(argv[[1]], argv[[2]], argv[[3]]))
 [1] 0
 
diff --git a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/builtins/TestBuiltin_gctorture2.java b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/builtins/TestBuiltin_gctorture2.java
index b2bd1d9da0..b0c6e40bef 100644
--- a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/builtins/TestBuiltin_gctorture2.java
+++ b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/builtins/TestBuiltin_gctorture2.java
@@ -17,15 +17,20 @@ import com.oracle.truffle.r.test.TestBase;
 // Checkstyle: stop line length check
 public class TestBuiltin_gctorture2 extends TestBase {
 
+    @Test
+    public void testgctorture() {
+        assertEval("argv <- list(FALSE); .Internal(gctorture(argv[[1]]))");
+        assertEval("argv <- list(NULL); .Internal(gctorture(argv[[1]]))");
+    }
+
     @Test
     public void testgctorture21() {
-        // FIXME not implemented: .Internal gctorture2
-        assertEval(Ignored.Unimplemented, "argv <- list(NULL, NULL, FALSE); .Internal(gctorture2(argv[[1]], argv[[2]], argv[[3]]))");
+        assertEval("argv <- list(NULL, NULL, FALSE); .Internal(gctorture2(argv[[1]], argv[[2]], argv[[3]]))");
+        assertEval("argv <- list(NULL, NULL, NULL); .Internal(gctorture2(argv[[1]], argv[[2]], argv[[3]]))");
     }
 
     @Test
     public void testgctorture22() {
-        // FIXME not implemented: .Internal gctorture2
-        assertEval(Ignored.Unimplemented, "argv <- list(FALSE, FALSE, FALSE); .Internal(gctorture2(argv[[1]], argv[[2]], argv[[3]]))");
+        assertEval("argv <- list(FALSE, FALSE, FALSE); .Internal(gctorture2(argv[[1]], argv[[2]], argv[[3]]))");
     }
 }
-- 
GitLab