From c776ab80729674f83622783a0cde57b91cb6022c Mon Sep 17 00:00:00 2001
From: Zbynek Slajchrt <zbynek.slajchrt@oracle.com>
Date: Thu, 8 Mar 2018 19:07:07 +0100
Subject: [PATCH] Enabling package stringi

---
 .../truffle/r/engine/shell/REmbedded.java     |  6 +-
 .../ffi/impl/common/JavaUpCallsRFFIImpl.java  |  2 +-
 .../truffle/r/ffi/impl/interop/DLLInfoMR.java | 23 +++++++
 .../r/ffi/impl/llvm/TruffleLLVM_Call.java     |  8 +--
 .../r/library/stats/RMultinomNode.java        |  2 +-
 .../fficall/src/truffle_llvm/Memory.c         |  2 -
 .../llvm_tools/llvm-c++                       | 12 ++++
 .../llvm_tools/llvm-cc                        | 13 ++++
 .../llvm_tools/llvm-helper                    | 61 +++++++++++++------
 .../com/oracle/truffle/r/runtime/ffi/DLL.java | 11 ++++
 .../truffle/r/runtime/ffi/RFFIVariables.java  |  6 +-
 11 files changed, 112 insertions(+), 34 deletions(-)

diff --git a/com.oracle.truffle.r.engine/src/com/oracle/truffle/r/engine/shell/REmbedded.java b/com.oracle.truffle.r.engine/src/com/oracle/truffle/r/engine/shell/REmbedded.java
index 0815c3bc09..e40dcb1c72 100644
--- a/com.oracle.truffle.r.engine/src/com/oracle/truffle/r/engine/shell/REmbedded.java
+++ b/com.oracle.truffle.r.engine/src/com/oracle/truffle/r/engine/shell/REmbedded.java
@@ -54,9 +54,9 @@ import com.oracle.truffle.r.runtime.context.RContext;
  * Rf_mainloop();
  * </pre>
  *
- * {@code Rf_initialize_R} invokes {@link #initializeR(String[])}, which creates new polyglot
- * {@link Context}. The call to {@code R_SetParams} adjusts the values stored in the
- * {@link RStartParams} object and then {@code Rf_mainloop}, which calls {@link #setupRmainloop()}
+ * {@code Rf_initialize_R} invokes {@link #initializeR(String[], boolean)}, which creates new
+ * polyglot {@link Context}. The call to {@code R_SetParams} adjusts the values stored in the
+ * {@link RStartParams} object and then {@code Rf_mainloop}, which calls {@code #setupRmainloop()}
  * and then {@link #runRmainloop()}, which will complete the FastR initialization and enter the
  * read-eval-print loop.
  */
diff --git a/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/common/JavaUpCallsRFFIImpl.java b/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/common/JavaUpCallsRFFIImpl.java
index 523b80e3b1..c773bc1fb1 100644
--- a/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/common/JavaUpCallsRFFIImpl.java
+++ b/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/common/JavaUpCallsRFFIImpl.java
@@ -1935,7 +1935,7 @@ public abstract class JavaUpCallsRFFIImpl implements UpCallsRFFI {
             NativeDataAccess.setNativeWrapper((RObject) vector, this);
         }
 
-        static Object get(TruffleObject x) {
+        public static Object get(TruffleObject x) {
             assert x instanceof RObject;
             Object wrapper = NativeDataAccess.getNativeWrapper((RObject) x);
             if (wrapper != null) {
diff --git a/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/interop/DLLInfoMR.java b/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/interop/DLLInfoMR.java
index 57ee20e69d..042dd55698 100644
--- a/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/interop/DLLInfoMR.java
+++ b/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/interop/DLLInfoMR.java
@@ -27,6 +27,8 @@ import com.oracle.truffle.api.interop.MessageResolution;
 import com.oracle.truffle.api.interop.Resolve;
 import com.oracle.truffle.api.interop.TruffleObject;
 import com.oracle.truffle.api.nodes.Node;
+import com.oracle.truffle.r.ffi.impl.common.JavaUpCallsRFFIImpl.VectorWrapper;
+import com.oracle.truffle.r.runtime.data.CharSXPWrapper;
 import com.oracle.truffle.r.runtime.ffi.DLL;
 import com.oracle.truffle.r.runtime.ffi.DLL.DLLInfo;
 import com.oracle.truffle.r.runtime.interop.RObjectNativeWrapper;
@@ -47,4 +49,25 @@ public class DLLInfoMR {
             return new RObjectNativeWrapper((DLLInfo) receiver);
         }
     }
+
+    @Resolve(message = "READ")
+    public abstract static class ReadNode extends Node {
+        public Object access(Object receiver, Object index) {
+            DLLInfo dllInfo = (DLLInfo) receiver;
+            int i = ((Number) index).intValue();
+            CharSXPWrapper res;
+            switch (i) {
+                case 0:
+                    res = dllInfo.pathSXP;
+                    break;
+                case 1:
+                    res = dllInfo.nameSXP;
+                    break;
+                default:
+                    throw new IndexOutOfBoundsException("Index can be 0 or 1");
+            }
+
+            return VectorWrapper.get(res);
+        }
+    }
 }
diff --git a/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/llvm/TruffleLLVM_Call.java b/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/llvm/TruffleLLVM_Call.java
index 27448accd6..f6978cf182 100644
--- a/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/llvm/TruffleLLVM_Call.java
+++ b/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/llvm/TruffleLLVM_Call.java
@@ -163,12 +163,12 @@ final class TruffleLLVM_Call implements CallRFFI {
 
         @Specialization
         protected static Object convert(int value) {
-            return RDataFactory.createIntVector(new int[]{value}, !RRuntime.isNA(value));
+            return RDataFactory.createIntVectorFromScalar(value);
         }
 
         @Specialization
         protected static Object convert(double value) {
-            return RDataFactory.createDoubleVector(new double[]{value}, !RRuntime.isNA(value));
+            return RDataFactory.createDoubleVectorFromScalar(value);
         }
 
         @Specialization
@@ -183,12 +183,12 @@ final class TruffleLLVM_Call implements CallRFFI {
 
         @Specialization
         protected static Object convert(byte value) {
-            return RDataFactory.createLogicalVector(new byte[]{value}, !RRuntime.isNA(value));
+            return RDataFactory.createLogicalVectorFromScalar(value);
         }
 
         @Specialization
         protected static Object convert(String value) {
-            return RDataFactory.createStringVector(new String[]{value}, !RRuntime.isNA(value));
+            return RDataFactory.createStringVectorFromScalar(value).makeSharedPermanent();
         }
 
         @Fallback
diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/RMultinomNode.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/RMultinomNode.java
index 6c302b8e33..066f93e9a1 100644
--- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/RMultinomNode.java
+++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/RMultinomNode.java
@@ -42,7 +42,7 @@ import com.oracle.truffle.r.runtime.nodes.RBaseNode;
 import com.oracle.truffle.r.runtime.rng.RRNG;
 
 /**
- * Implements the vectorization of {@link RMultinom}.
+ * Implements the vectorization of {@link RMultinomNode}.
  */
 public abstract class RMultinomNode extends RExternalBuiltinNode.Arg3 {
 
diff --git a/com.oracle.truffle.r.native/fficall/src/truffle_llvm/Memory.c b/com.oracle.truffle.r.native/fficall/src/truffle_llvm/Memory.c
index 734c99bdd7..a3211da56c 100644
--- a/com.oracle.truffle.r.native/fficall/src/truffle_llvm/Memory.c
+++ b/com.oracle.truffle.r.native/fficall/src/truffle_llvm/Memory.c
@@ -13,8 +13,6 @@
 #include <stdlib.h>
 #include <string.h>
 
-// TODO: this file can likely be removed, using the NFI version instead
-
 #define T_MEM_TABLE_INITIAL_SIZE 0
 // The table of transient objects that have been allocated dur the current FFI call
 static void **tMemTable;
diff --git a/com.oracle.truffle.r.native/llvm_tools/llvm-c++ b/com.oracle.truffle.r.native/llvm_tools/llvm-c++
index ec93f97e3d..b6fec6459e 100755
--- a/com.oracle.truffle.r.native/llvm_tools/llvm-c++
+++ b/com.oracle.truffle.r.native/llvm_tools/llvm-c++
@@ -44,10 +44,22 @@ else
     llvm_args="-stdlib=libc++ -I/usr/include/libcxxabi $llvm_args"
   fi
   runit $llvm_tool_bin $llvm_args
+  ecode=$?
+  if [[ $ecode -ne 0 ]]; then
+    exit $ecode
+  fi
 
   # the llvm_ir_file is empty if the result is sent to stdout
   if [ -n "$llvm_ir_file" ]; then
     mem2reg_opt
+    ecode=$?
+    if [[ $ecode -ne 0 ]]; then
+      exit $ecode
+    fi
     fake_obj
+    ecode=$?
+    if [[ $ecode -ne 0 ]]; then
+      exit $ecode
+    fi
   fi
 fi
diff --git a/com.oracle.truffle.r.native/llvm_tools/llvm-cc b/com.oracle.truffle.r.native/llvm_tools/llvm-cc
index 42acf7a327..62f42e6f3f 100755
--- a/com.oracle.truffle.r.native/llvm_tools/llvm-cc
+++ b/com.oracle.truffle.r.native/llvm_tools/llvm-cc
@@ -40,9 +40,22 @@ else
   llvm_tool=clang
   get_llvm_tool
   runit $llvm_tool_bin $llvm_args
+  ecode=$?
+  if [[ $ecode -ne 0 ]]; then
+    exit $ecode
+  fi
+
   # the llvm_ir_file is empty if the result is sent to stdout
   if [ -n "$llvm_ir_file" ]; then
     mem2reg_opt
+    ecode=$?
+    if [[ $ecode -ne 0 ]]; then
+      exit $ecode
+    fi
     fake_obj
+    ecode=$?
+    if [[ $ecode -ne 0 ]]; then
+      exit $ecode
+    fi
   fi
 fi
diff --git a/com.oracle.truffle.r.native/llvm_tools/llvm-helper b/com.oracle.truffle.r.native/llvm_tools/llvm-helper
index 2f06ecefc7..4c04a4abeb 100644
--- a/com.oracle.truffle.r.native/llvm_tools/llvm-helper
+++ b/com.oracle.truffle.r.native/llvm_tools/llvm-helper
@@ -53,41 +53,66 @@ function analyze_args() {
   llvm_args="-g "
   if [ $fortran -eq 1 ]
   then
-    llvm_args+='-S '
     llvm_file_ext='.ll'
   else
     llvm_file_ext='.bc'
-    llvm_args+='-emit-llvm '
   fi
 
   is_link=0
+  out_file_opt=""
   llvm_ir_file=""
+  llvm_ir_file_opt=""
+  c_opt_found=0
 
   while [[ $# -gt 0 ]]
   do
-    llvm_args+="$1 "
     case $1 in
+      -c)
+        c_opt_found=1
+        llvm_args+="$1 "
+      ;;
       -o)
         shift
-		p=$1
-		f=`basename $p`
-		d=`dirname $p`
-		ext=${f##*.}
-		if [ $ext == 'so' ] || [ $ext == 'dylib' ]
-		then
-		  is_link=1
-		elif [ $ext == 'o' ]
-		then
-		  llvm_ir_file=${d}/${f%%.*}
-		  llvm_ir_file+=$llvm_file_ext
-		  llvm_args+="$llvm_ir_file "
-		fi
+        p=$1
+        f=`basename $p`
+        d=`dirname $p`
+        ext=${f##*.}
+        if [ $ext == 'so' ] || [ $ext == 'dylib' ]
+        then
+          is_link=1
+        elif [ $ext == 'o' ]
+		    then
+		      llvm_ir_file=${d}/${f%%.*}
+		      llvm_ir_file="${llvm_ir_file}${llvm_file_ext}"
+          llvm_ir_file_opt="-o ${llvm_ir_file}"
+        else
+          out_file_opt="-o $p"
+        fi
+      ;;
+      *)
+        llvm_args+="$1 "
       ;;
-     *)
-     ;;
     esac
     shift
   done
+
+  if [ $fortran -eq 1 ]
+  then
+    if [ $c_opt_found -eq 1 ]
+    then
+      llvm_args="-S $llvm_ir_file_opt $llvm_args "
+    else
+      llvm_args="$out_file_opt $llvm_args "
+    fi
+  else
+    if [ $c_opt_found -eq 1 ]
+    then
+      llvm_args="-emit-llvm $llvm_ir_file_opt $llvm_args "
+    else
+      llvm_args="$out_file_opt $llvm_args "
+    fi
+  fi
+
 }
 
 # Input arguments:
diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/ffi/DLL.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/ffi/DLL.java
index ce08cd0808..dbfdc02e70 100644
--- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/ffi/DLL.java
+++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/ffi/DLL.java
@@ -39,6 +39,7 @@ import com.oracle.truffle.r.runtime.context.RContext;
 import com.oracle.truffle.r.runtime.context.RContext.ContextKind;
 import com.oracle.truffle.r.runtime.context.RContext.ContextState;
 import com.oracle.truffle.r.runtime.data.NativeDataAccess.CustomNativeMirror;
+import com.oracle.truffle.r.runtime.data.CharSXPWrapper;
 import com.oracle.truffle.r.runtime.data.RDataFactory;
 import com.oracle.truffle.r.runtime.data.RExternalPtr;
 import com.oracle.truffle.r.runtime.data.RList;
@@ -191,8 +192,16 @@ public class DLL {
         private static final String DLLINFO_CLASS = "DLLInfo";
 
         private final int id;
+
         public final String name;
         public final String path;
+        /**
+         * The CharSXPWrapper fields maintain the wrapped strings that are returned as a response to
+         * the READ message sent to this Truffle object. See {@code DLLInfoMR}.
+         */
+        public final CharSXPWrapper nameSXP;
+        public final CharSXPWrapper pathSXP;
+
         public final Object handle;
         private boolean dynamicLookup;
         private boolean forceSymbols;
@@ -203,7 +212,9 @@ public class DLL {
         private DLLInfo(String name, String path, boolean dynamicLookup, Object handle) {
             this.id = ID.getAndIncrement();
             this.name = name;
+            this.nameSXP = CharSXPWrapper.create(name);
             this.path = path;
+            this.pathSXP = CharSXPWrapper.create(path);
             this.dynamicLookup = dynamicLookup;
             this.handle = handle;
         }
diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/ffi/RFFIVariables.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/ffi/RFFIVariables.java
index babe89a0c1..39c36aab61 100644
--- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/ffi/RFFIVariables.java
+++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/ffi/RFFIVariables.java
@@ -22,9 +22,6 @@
  */
 package com.oracle.truffle.r.runtime.ffi;
 
-import java.util.Arrays;
-import java.util.stream.Stream;
-
 import com.oracle.truffle.r.runtime.REnvVars;
 import com.oracle.truffle.r.runtime.RRuntime;
 import com.oracle.truffle.r.runtime.TempPathName;
@@ -33,12 +30,11 @@ import com.oracle.truffle.r.runtime.data.CharSXPWrapper;
 import com.oracle.truffle.r.runtime.data.RDataFactory;
 import com.oracle.truffle.r.runtime.data.RMissing;
 import com.oracle.truffle.r.runtime.data.RNull;
-import com.oracle.truffle.r.runtime.data.RSymbol;
 import com.oracle.truffle.r.runtime.data.RUnboundValue;
 import com.oracle.truffle.r.runtime.env.REnvironment;
 
 /**
- * Note: regenerate the C glue code upon any change in this enum, use {@link #main(String[])}.
+ * Note: regenerate the C glue code upon any change in this enum.
  */
 public enum RFFIVariables {
     R_Home("dummy string"),
-- 
GitLab