From 08cb8af9c725e6dffde865b421f38e2f4bea05cf Mon Sep 17 00:00:00 2001
From: Mick Jordan <mick.jordan@oracle.com>
Date: Tue, 29 Dec 2015 14:31:58 -0800
Subject: [PATCH] refactor fficall

---
 com.oracle.truffle.r.native/fficall/Makefile  |  1 -
 com.oracle.truffle.r.native/fficall/README    | 21 +++---
 .../fficall/src/common/Defn.h                 | 65 +++++++++++++++++
 .../fficall/src/common/Graphics.h             | 15 ++++
 .../fficall/src/common/GraphicsBase.h         | 15 ++++
 .../fficall/src/common/Internal.h             | 14 ++++
 .../fficall/src/common/Print.h                | 15 ++++
 .../fficall/src/common/Rgraphics.h            | 15 ++++
 .../fficall/src/{jni => common}/appl_pretty.c |  4 --
 .../fficall/src/common/arithmetic.c           |  2 +-
 .../fficall/src/{jni => common}/coerce.c      |  2 +-
 .../fficall/src/{jni => common}/inlined.c     |  2 +-
 .../fficall/src/{jni => common}/main_colors.c |  4 --
 .../src/{jni => common}/main_devices.c        |  8 ---
 .../fficall/src/common/main_engine.c          | 13 ++++
 .../fficall/src/{jni => common}/main_format.c |  7 --
 .../src/{jni => common}/main_graphics.c       |  5 --
 .../fficall/src/{jni => common}/main_plot.c   |  7 --
 .../fficall/src/{jni => common}/main_plot3d.c |  6 --
 .../src/{jni => common}/main_plotmath.c       |  7 --
 .../fficall/src/{jni => common}/main_print.c  |  9 +--
 .../fficall/src/common/main_printutils.c      | 61 ++++++++++++++++
 .../src/{jni => common}/main_rlocale.c        |  4 --
 .../fficall/src/{jni => common}/main_sort.c   |  4 --
 .../fficall/src/common/rlocale.h              | 15 ++++
 .../fficall/src/{jni => common}/sys.c         |  0
 .../fficall/src/common/variable_defs.h        |  8 +--
 .../fficall/src/{jni => common}/xxxpr.f       |  0
 .../fficall/src/jni/Applic.c                  |  2 +-
 .../fficall/src/jni/Connections.c             |  2 +-
 .../fficall/src/jni/Defn.h                    | 46 ------------
 .../fficall/src/jni/Makefile                  | 45 ++++++------
 .../fficall/src/jni/Memory.c                  |  2 +-
 .../fficall/src/jni/Parse.c                   |  2 +-
 .../fficall/src/jni/Random.c                  |  2 +-
 .../fficall/src/jni/Rdynload.c                |  4 +-
 .../fficall/src/jni/Riconv.c                  |  2 +-
 .../fficall/src/jni/Rinternals.c              |  2 +-
 .../fficall/src/jni/Rmath.c                   |  2 +-
 .../fficall/src/jni/Utils.c                   |  2 +-
 .../fficall/src/jni/main_engine.c             | 70 -------------------
 .../fficall/src/jni/rfficall.c                |  2 +-
 .../fficall/src/jni/rffiutils.c               |  2 +-
 .../fficall/src/jni/unimplemented.c           | 44 +-----------
 .../fficall/src/jni/variables.c               |  4 +-
 .../library/stats/Makefile                    |  6 ++
 46 files changed, 298 insertions(+), 272 deletions(-)
 create mode 100644 com.oracle.truffle.r.native/fficall/src/common/Defn.h
 create mode 100644 com.oracle.truffle.r.native/fficall/src/common/Graphics.h
 create mode 100644 com.oracle.truffle.r.native/fficall/src/common/GraphicsBase.h
 create mode 100644 com.oracle.truffle.r.native/fficall/src/common/Internal.h
 create mode 100644 com.oracle.truffle.r.native/fficall/src/common/Print.h
 create mode 100644 com.oracle.truffle.r.native/fficall/src/common/Rgraphics.h
 rename com.oracle.truffle.r.native/fficall/src/{jni => common}/appl_pretty.c (97%)
 rename com.oracle.truffle.r.native/fficall/src/{jni => common}/coerce.c (99%)
 rename com.oracle.truffle.r.native/fficall/src/{jni => common}/inlined.c (99%)
 rename com.oracle.truffle.r.native/fficall/src/{jni => common}/main_colors.c (96%)
 rename com.oracle.truffle.r.native/fficall/src/{jni => common}/main_devices.c (86%)
 create mode 100644 com.oracle.truffle.r.native/fficall/src/common/main_engine.c
 rename com.oracle.truffle.r.native/fficall/src/{jni => common}/main_format.c (91%)
 rename com.oracle.truffle.r.native/fficall/src/{jni => common}/main_graphics.c (94%)
 rename com.oracle.truffle.r.native/fficall/src/{jni => common}/main_plot.c (89%)
 rename com.oracle.truffle.r.native/fficall/src/{jni => common}/main_plot3d.c (90%)
 rename com.oracle.truffle.r.native/fficall/src/{jni => common}/main_plotmath.c (86%)
 rename com.oracle.truffle.r.native/fficall/src/{jni => common}/main_print.c (97%)
 create mode 100644 com.oracle.truffle.r.native/fficall/src/common/main_printutils.c
 rename com.oracle.truffle.r.native/fficall/src/{jni => common}/main_rlocale.c (96%)
 rename com.oracle.truffle.r.native/fficall/src/{jni => common}/main_sort.c (96%)
 create mode 100644 com.oracle.truffle.r.native/fficall/src/common/rlocale.h
 rename com.oracle.truffle.r.native/fficall/src/{jni => common}/sys.c (100%)
 rename com.oracle.truffle.r.native/fficall/src/{jni => common}/xxxpr.f (100%)
 delete mode 100644 com.oracle.truffle.r.native/fficall/src/jni/Defn.h
 delete mode 100644 com.oracle.truffle.r.native/fficall/src/jni/main_engine.c

diff --git a/com.oracle.truffle.r.native/fficall/Makefile b/com.oracle.truffle.r.native/fficall/Makefile
index 12c32f3b32..811cca35f9 100644
--- a/com.oracle.truffle.r.native/fficall/Makefile
+++ b/com.oracle.truffle.r.native/fficall/Makefile
@@ -24,7 +24,6 @@
 .PHONY: all clean
 
 all:
-	$(MAKE) -C src/jni links
 	$(MAKE) -C src/jni all
 
 clean:
diff --git a/com.oracle.truffle.r.native/fficall/README b/com.oracle.truffle.r.native/fficall/README
index 8f67219eef..99ff45a884 100644
--- a/com.oracle.truffle.r.native/fficall/README
+++ b/com.oracle.truffle.r.native/fficall/README
@@ -1,19 +1,24 @@
 fficall contains the implementation of the R FFI, as described in https://cran.r-project.org/doc/manuals/r-release/R-exts.html.
+It's actually a bit more than that as it also contains code copied from GnuR, for example that supports graphics or is sufficiently
+simple that it is neither necessary nor desirable to implement in Java.
 
  There are two sub-directories:
    common
    jni
 
- 'jni' contains the implementation that is based on Java JNI. 'common' contains code that has no JNI dependencies and has been extracted for
- reuse in other implementations. Note however, that the common files cannot all be compiled in isolation, as they may depend on the implementation
- via rffiutils.h. During the build symbolic links are made to the files in common and they are compiled with the other 'jni' files.
+'common' contains code that has no explicit JNI dependencies and has been extracted for reuse in other implementations. This code is mostly
+copied/included from GnuR. 'jni' contains the implementation that is based on and has explicit dependencies on Java JNI.
+
+ Note that the 'common' files cannot all be compiled in isolation, as they typically depend on the implementation
+ via rffiutils.h (and Rinternals.h). N.B. the GnuR code may "include" header files that are private to the GnuR implementation.
+ These must always be replaced with local versions that make sense under FastR. Examples are Defn.h and Internal.h. When code
+ is compiled the C compiler include path NEVER reaches into GnuR; all internal headers that are needed must existing in the
+ common directory. They may explicitly include the original header but doing it this way ensures that we never accidently
+ include an internal header.
 
  The R FFI is rather baroque and defined in large set of header files in the sibling 'include' directory. In GnuR, the implementation
- of the functions is spread over the GnuR C files in 'src/main'. To ease navigation of the FastR implementation, in general, the implementation
- of the functions in a header file 'Rxxx.h' is stored in the file 'Rxxx.c'.
+ of the functions is spread over the GnuR C files in 'src/main'. To ease navigation of the FastR implementation, in general,
+ the implementation of the functions in a header file 'Rxxx.h' is stored in the file 'Rxxx.c'.
 
 The points of entry from Java are defined in the file rfficall.c. Various utility functions are defined if rffiutils.{h,c}.
 
-TODO
-
-Refactor the files that are copied from GnuR and not strictly part of the FFI but support the native graphics package.
diff --git a/com.oracle.truffle.r.native/fficall/src/common/Defn.h b/com.oracle.truffle.r.native/fficall/src/common/Defn.h
new file mode 100644
index 0000000000..7265f2a959
--- /dev/null
+++ b/com.oracle.truffle.r.native/fficall/src/common/Defn.h
@@ -0,0 +1,65 @@
+/*
+ * This material is distributed under the GNU General Public License
+ * Version 2. You may review the terms of this license at
+ * http://www.gnu.org/licenses/gpl-2.0.html
+ *
+ * Copyright (c) 1995-2015, The R Core Team
+ * Copyright (c) 2003, The R Foundation
+ * Copyright (c) 2015, Oracle and/or its affiliates
+ *
+ * All rights reserved.
+ */
+
+// This is a cut-down version of src/include/Defn.h that is a safe replacement for use with FastR
+
+#ifndef DEFN_H_
+#define DEFN_H_
+
+#include <jni.h>
+#include <stdlib.h>
+#include <Rinternals.h>
+
+// various definitions required to compile GNU-R code:
+
+#define attribute_hidden
+
+#define F77_SYMBOL(x)	x
+#define F77_QSYMBOL(x) #x
+
+#define Rexp10(x) pow(10.0, x)
+
+// no NLS:
+#ifndef _
+#define _(String) (String)
+#endif
+#define N_(String) String
+#define ngettext(String, StringP, N) (N > 1 ? StringP: String)
+
+void sortVector(SEXP, Rboolean);
+int Scollate(SEXP a, SEXP b);
+void Rf_checkArityCall(SEXP, SEXP, SEXP);
+
+/* ../main/devices.c, used in memory.c, gnuwin32/extra.c */
+#define R_MaxDevices 64
+
+extern SEXP R_DeviceSymbol;
+extern SEXP R_DevicesSymbol;
+extern SEXP R_Interactive;
+extern SEXP R_Visible;
+int	R_ReadConsole(const char *, unsigned char *, int, int);
+
+//#define HAVE_MBSTATE_T 1 // actually from config.h
+
+extern Rboolean utf8locale;
+extern Rboolean mbcslocale;
+extern Rboolean latin1locale;
+
+extern int R_dec_min_exponent;
+extern unsigned int max_contour_segments;
+
+typedef SEXP (*CCODE)(SEXP, SEXP, SEXP, SEXP);
+
+CCODE (PRIMFUN)(SEXP x);
+
+
+#endif /* DEFN_H_ */
diff --git a/com.oracle.truffle.r.native/fficall/src/common/Graphics.h b/com.oracle.truffle.r.native/fficall/src/common/Graphics.h
new file mode 100644
index 0000000000..25bd25ec04
--- /dev/null
+++ b/com.oracle.truffle.r.native/fficall/src/common/Graphics.h
@@ -0,0 +1,15 @@
+/*
+ * This material is distributed under the GNU General Public License
+ * Version 2. You may review the terms of this license at
+ * http://www.gnu.org/licenses/gpl-2.0.html
+ *
+ * Copyright (c) 1995-2015, The R Core Team
+ * Copyright (c) 2003, The R Foundation
+ * Copyright (c) 2015, Oracle and/or its affiliates
+ *
+ * All rights reserved.
+ */
+
+// This is a cut-down version of src/include/Graphics.h that is a safe replacement for use with FastR
+
+#include "../../../gnur/R-3.1.3/src/include/Graphics.h"
diff --git a/com.oracle.truffle.r.native/fficall/src/common/GraphicsBase.h b/com.oracle.truffle.r.native/fficall/src/common/GraphicsBase.h
new file mode 100644
index 0000000000..8ecd4b1c36
--- /dev/null
+++ b/com.oracle.truffle.r.native/fficall/src/common/GraphicsBase.h
@@ -0,0 +1,15 @@
+/*
+ * This material is distributed under the GNU General Public License
+ * Version 2. You may review the terms of this license at
+ * http://www.gnu.org/licenses/gpl-2.0.html
+ *
+ * Copyright (c) 1995-2015, The R Core Team
+ * Copyright (c) 2003, The R Foundation
+ * Copyright (c) 2015, Oracle and/or its affiliates
+ *
+ * All rights reserved.
+ */
+
+// This is a cut-down version of src/include/GraphicsBase.h that is a safe replacement for use with FastR
+
+#include "../../../gnur/R-3.1.3/src/include/GraphicsBase.h"
diff --git a/com.oracle.truffle.r.native/fficall/src/common/Internal.h b/com.oracle.truffle.r.native/fficall/src/common/Internal.h
new file mode 100644
index 0000000000..c216ba8a07
--- /dev/null
+++ b/com.oracle.truffle.r.native/fficall/src/common/Internal.h
@@ -0,0 +1,14 @@
+/*
+ * This material is distributed under the GNU General Public License
+ * Version 2. You may review the terms of this license at
+ * http://www.gnu.org/licenses/gpl-2.0.html
+ *
+ * Copyright (c) 1995-2015, The R Core Team
+ * Copyright (c) 2003, The R Foundation
+ * Copyright (c) 2015, Oracle and/or its affiliates
+ *
+ * All rights reserved.
+ */
+
+// This is a cut-down version of src/include/Internal.h that is a safe replacement for use with FastR
+
diff --git a/com.oracle.truffle.r.native/fficall/src/common/Print.h b/com.oracle.truffle.r.native/fficall/src/common/Print.h
new file mode 100644
index 0000000000..0e1edb942c
--- /dev/null
+++ b/com.oracle.truffle.r.native/fficall/src/common/Print.h
@@ -0,0 +1,15 @@
+/*
+ * This material is distributed under the GNU General Public License
+ * Version 2. You may review the terms of this license at
+ * http://www.gnu.org/licenses/gpl-2.0.html
+ *
+ * Copyright (c) 1995-2015, The R Core Team
+ * Copyright (c) 2003, The R Foundation
+ * Copyright (c) 2015, Oracle and/or its affiliates
+ *
+ * All rights reserved.
+ */
+
+// This is a cut-down version of src/include/Print.h that is a safe replacement for use with FastR
+
+#include "../../../gnur/R-3.1.3/src/include/Print.h"
diff --git a/com.oracle.truffle.r.native/fficall/src/common/Rgraphics.h b/com.oracle.truffle.r.native/fficall/src/common/Rgraphics.h
new file mode 100644
index 0000000000..841a21fedb
--- /dev/null
+++ b/com.oracle.truffle.r.native/fficall/src/common/Rgraphics.h
@@ -0,0 +1,15 @@
+/*
+ * This material is distributed under the GNU General Public License
+ * Version 2. You may review the terms of this license at
+ * http://www.gnu.org/licenses/gpl-2.0.html
+ *
+ * Copyright (c) 1995-2015, The R Core Team
+ * Copyright (c) 2003, The R Foundation
+ * Copyright (c) 2015, Oracle and/or its affiliates
+ *
+ * All rights reserved.
+ */
+
+// This is a cut-down version of src/include/Rgraphics.h that is a safe replacement for use with FastR
+
+#include "../../../gnur/R-3.1.3/src/include/Rgraphics.h"
diff --git a/com.oracle.truffle.r.native/fficall/src/jni/appl_pretty.c b/com.oracle.truffle.r.native/fficall/src/common/appl_pretty.c
similarity index 97%
rename from com.oracle.truffle.r.native/fficall/src/jni/appl_pretty.c
rename to com.oracle.truffle.r.native/fficall/src/common/appl_pretty.c
index e2abd5a743..ba48572fb9 100644
--- a/com.oracle.truffle.r.native/fficall/src/jni/appl_pretty.c
+++ b/com.oracle.truffle.r.native/fficall/src/common/appl_pretty.c
@@ -21,8 +21,4 @@
  * questions.
  */
 
-#include "Defn.h"
-
-#undef _
-
 #include "../../../gnur/R-3.1.3/src/appl/pretty.c"
diff --git a/com.oracle.truffle.r.native/fficall/src/common/arithmetic.c b/com.oracle.truffle.r.native/fficall/src/common/arithmetic.c
index 17ea1d495f..5ea6a98e43 100644
--- a/com.oracle.truffle.r.native/fficall/src/common/arithmetic.c
+++ b/com.oracle.truffle.r.native/fficall/src/common/arithmetic.c
@@ -21,7 +21,7 @@
  * questions.
  */
 
-#include "rffiutils.h"
+#include <rffiutils.h>
 #include <stdlib.h>
 
 // FastR: selected functions from arithmetic.c:
diff --git a/com.oracle.truffle.r.native/fficall/src/jni/coerce.c b/com.oracle.truffle.r.native/fficall/src/common/coerce.c
similarity index 99%
rename from com.oracle.truffle.r.native/fficall/src/jni/coerce.c
rename to com.oracle.truffle.r.native/fficall/src/common/coerce.c
index f8d675e7b9..61d421693b 100644
--- a/com.oracle.truffle.r.native/fficall/src/jni/coerce.c
+++ b/com.oracle.truffle.r.native/fficall/src/common/coerce.c
@@ -21,7 +21,7 @@
  * questions.
  */
 
-#include "rffiutils.h"
+#include <rffiutils.h>
 
 #define _(Source) (Source)
 
diff --git a/com.oracle.truffle.r.native/fficall/src/jni/inlined.c b/com.oracle.truffle.r.native/fficall/src/common/inlined.c
similarity index 99%
rename from com.oracle.truffle.r.native/fficall/src/jni/inlined.c
rename to com.oracle.truffle.r.native/fficall/src/common/inlined.c
index 18d8b98026..cecdc2aed4 100644
--- a/com.oracle.truffle.r.native/fficall/src/jni/inlined.c
+++ b/com.oracle.truffle.r.native/fficall/src/common/inlined.c
@@ -21,7 +21,7 @@
  * questions.
  */
 
-#include "rffiutils.h"
+#include <rffiutils.h>
 
 #define INLINE_FUN
 
diff --git a/com.oracle.truffle.r.native/fficall/src/jni/main_colors.c b/com.oracle.truffle.r.native/fficall/src/common/main_colors.c
similarity index 96%
rename from com.oracle.truffle.r.native/fficall/src/jni/main_colors.c
rename to com.oracle.truffle.r.native/fficall/src/common/main_colors.c
index bf5763495a..07e6d29717 100644
--- a/com.oracle.truffle.r.native/fficall/src/jni/main_colors.c
+++ b/com.oracle.truffle.r.native/fficall/src/common/main_colors.c
@@ -21,8 +21,4 @@
  * questions.
  */
 
-#define attribute_hidden
-
-#include "Defn.h"
-
 #include "../../../gnur/R-3.1.3/src/main/colors.c"
diff --git a/com.oracle.truffle.r.native/fficall/src/jni/main_devices.c b/com.oracle.truffle.r.native/fficall/src/common/main_devices.c
similarity index 86%
rename from com.oracle.truffle.r.native/fficall/src/jni/main_devices.c
rename to com.oracle.truffle.r.native/fficall/src/common/main_devices.c
index 77f8c951fc..48bafdcbe0 100644
--- a/com.oracle.truffle.r.native/fficall/src/jni/main_devices.c
+++ b/com.oracle.truffle.r.native/fficall/src/common/main_devices.c
@@ -21,12 +21,4 @@
  * questions.
  */
 
-#include "rffiutils.h"
-#include "Defn.h"
-
-extern SEXP    R_DevicesSymbol;  /* ".Devices" */
-extern Rboolean R_Interactive;
-#define attribute_hidden
-#define R_MaxDevices 64
-
 #include "../../../gnur/R-3.1.3/src/main/devices.c"
diff --git a/com.oracle.truffle.r.native/fficall/src/common/main_engine.c b/com.oracle.truffle.r.native/fficall/src/common/main_engine.c
new file mode 100644
index 0000000000..cc8e62c73b
--- /dev/null
+++ b/com.oracle.truffle.r.native/fficall/src/common/main_engine.c
@@ -0,0 +1,13 @@
+/*
+ * This material is distributed under the GNU General Public License
+ * Version 2. You may review the terms of this license at
+ * http://www.gnu.org/licenses/gpl-2.0.html
+ *
+ * Copyright (c) 1995-2015, The R Core Team
+ * Copyright (c) 2003, The R Foundation
+ * Copyright (c) 2015, Oracle and/or its affiliates
+ *
+ * All rights reserved.
+ */
+
+#include "../../../gnur/R-3.1.3/src/main/engine.c"
diff --git a/com.oracle.truffle.r.native/fficall/src/jni/main_format.c b/com.oracle.truffle.r.native/fficall/src/common/main_format.c
similarity index 91%
rename from com.oracle.truffle.r.native/fficall/src/jni/main_format.c
rename to com.oracle.truffle.r.native/fficall/src/common/main_format.c
index f677ed0b4c..fdb1641a9d 100644
--- a/com.oracle.truffle.r.native/fficall/src/jni/main_format.c
+++ b/com.oracle.truffle.r.native/fficall/src/common/main_format.c
@@ -21,11 +21,4 @@
  * questions.
  */
 
-#define HAVE_NEARBYINT
-#define attribute_hidden
-
-extern int R_dec_min_exponent;
-
-#include "Defn.h"
-
 #include "../../../gnur/R-3.1.3/src/main/format.c"
diff --git a/com.oracle.truffle.r.native/fficall/src/jni/main_graphics.c b/com.oracle.truffle.r.native/fficall/src/common/main_graphics.c
similarity index 94%
rename from com.oracle.truffle.r.native/fficall/src/jni/main_graphics.c
rename to com.oracle.truffle.r.native/fficall/src/common/main_graphics.c
index 4ea38008da..6a7725d82b 100644
--- a/com.oracle.truffle.r.native/fficall/src/jni/main_graphics.c
+++ b/com.oracle.truffle.r.native/fficall/src/common/main_graphics.c
@@ -21,9 +21,4 @@
  * questions.
  */
 
-#include "rffiutils.h"
-#include "Defn.h"
-
-#define attribute_hidden
-
 #include "../../../gnur/R-3.1.3/src/main/graphics.c"
diff --git a/com.oracle.truffle.r.native/fficall/src/jni/main_plot.c b/com.oracle.truffle.r.native/fficall/src/common/main_plot.c
similarity index 89%
rename from com.oracle.truffle.r.native/fficall/src/jni/main_plot.c
rename to com.oracle.truffle.r.native/fficall/src/common/main_plot.c
index 05417f7cce..4a1b0f2ed3 100644
--- a/com.oracle.truffle.r.native/fficall/src/jni/main_plot.c
+++ b/com.oracle.truffle.r.native/fficall/src/common/main_plot.c
@@ -21,11 +21,4 @@
  * questions.
  */
 
-#include "rffiutils.h"
-#include "Defn.h"
-
-
-#define attribute_hidden
-
-#include "../../../gnur/R-3.1.3/src/main/contour-common.h"
 #include "../../../gnur/R-3.1.3/src/main/plot.c"
diff --git a/com.oracle.truffle.r.native/fficall/src/jni/main_plot3d.c b/com.oracle.truffle.r.native/fficall/src/common/main_plot3d.c
similarity index 90%
rename from com.oracle.truffle.r.native/fficall/src/jni/main_plot3d.c
rename to com.oracle.truffle.r.native/fficall/src/common/main_plot3d.c
index dce8977ed9..5a1a04f808 100644
--- a/com.oracle.truffle.r.native/fficall/src/jni/main_plot3d.c
+++ b/com.oracle.truffle.r.native/fficall/src/common/main_plot3d.c
@@ -21,10 +21,4 @@
  * questions.
  */
 
-#include "rffiutils.h"
-#include "Defn.h"
-
-#define attribute_hidden
-extern unsigned int max_contour_segments;
-
 #include "../../../gnur/R-3.1.3/src/main/plot3d.c"
diff --git a/com.oracle.truffle.r.native/fficall/src/jni/main_plotmath.c b/com.oracle.truffle.r.native/fficall/src/common/main_plotmath.c
similarity index 86%
rename from com.oracle.truffle.r.native/fficall/src/jni/main_plotmath.c
rename to com.oracle.truffle.r.native/fficall/src/common/main_plotmath.c
index f401c82ba0..868e2eeed6 100644
--- a/com.oracle.truffle.r.native/fficall/src/jni/main_plotmath.c
+++ b/com.oracle.truffle.r.native/fficall/src/common/main_plotmath.c
@@ -21,11 +21,4 @@
  * questions.
  */
 
-#include "rffiutils.h"
-#include "Defn.h"
-
-#define attribute_hidden
-#include "../../../gnur/R-3.1.3/src/main/contour-common.h"
-unsigned int max_contour_segments = 25000;
-
 #include "../../../gnur/R-3.1.3/src/main/plotmath.c"
diff --git a/com.oracle.truffle.r.native/fficall/src/jni/main_print.c b/com.oracle.truffle.r.native/fficall/src/common/main_print.c
similarity index 97%
rename from com.oracle.truffle.r.native/fficall/src/jni/main_print.c
rename to com.oracle.truffle.r.native/fficall/src/common/main_print.c
index 3f19c4904d..43dfd2e0a1 100644
--- a/com.oracle.truffle.r.native/fficall/src/jni/main_print.c
+++ b/com.oracle.truffle.r.native/fficall/src/common/main_print.c
@@ -10,8 +10,6 @@
  * All rights reserved.
  */
 
-#include "rffiutils.h"
-
 // the following code is copied from "print.c"
 
 /*
@@ -77,10 +75,9 @@
 #include <config.h>
 #endif
 
-#include "rffiutils.h"
-#include "Defn.h"
-
-#include "Print.h"
+#include <rffiutils.h>
+#include <Defn.h>
+#include <Print.h>
 
 
 /* Global print parameter struct: */
diff --git a/com.oracle.truffle.r.native/fficall/src/common/main_printutils.c b/com.oracle.truffle.r.native/fficall/src/common/main_printutils.c
new file mode 100644
index 0000000000..fd06fa411b
--- /dev/null
+++ b/com.oracle.truffle.r.native/fficall/src/common/main_printutils.c
@@ -0,0 +1,61 @@
+/*
+ * This material is distributed under the GNU General Public License
+ * Version 2. You may review the terms of this license at
+ * http://www.gnu.org/licenses/gpl-2.0.html
+ *
+ * Copyright (c) 1995-2015, The R Core Team
+ * Copyright (c) 2003, The R Foundation
+ * Copyright (c) 2015, Oracle and/or its affiliates
+ *
+ * All rights reserved.
+ */
+
+// the following code is copied from "printutils.c"
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <rffiutils.h>
+#include <Defn.h>
+#include <Print.h>
+
+#ifndef min
+#define min(a, b) (((a)<(b))?(a):(b))
+#endif
+#define NB 1000
+const char *Rf_EncodeReal(double x, int w, int d, int e, char cdec)
+{
+    static char buff[NB];
+    char *p, fmt[20];
+
+    /* IEEE allows signed zeros (yuck!) */
+    if (x == 0.0) x = 0.0;
+    if (!R_FINITE(x)) {
+	if(ISNA(x)) snprintf(buff, NB, "%*s", min(w, (NB-1)), CHAR(R_print.na_string));
+	else if(ISNAN(x)) snprintf(buff, NB, "%*s", min(w, (NB-1)), "NaN");
+	else if(x > 0) snprintf(buff, NB, "%*s", min(w, (NB-1)), "Inf");
+	else snprintf(buff, NB, "%*s", min(w, (NB-1)), "-Inf");
+    }
+    else if (e) {
+	if(d) {
+	    sprintf(fmt,"%%#%d.%de", min(w, (NB-1)), d);
+	    snprintf(buff, NB, fmt, x);
+	}
+	else {
+	    sprintf(fmt,"%%%d.%de", min(w, (NB-1)), d);
+	    snprintf(buff, NB, fmt, x);
+	}
+    }
+    else { /* e = 0 */
+	sprintf(fmt,"%%%d.%df", min(w, (NB-1)), d);
+	snprintf(buff, NB, fmt, x);
+    }
+    buff[NB-1] = '\0';
+
+    if(cdec != '.')
+      for(p = buff; *p; p++) if(*p == '.') *p = cdec;
+
+    return buff;
+}
+
diff --git a/com.oracle.truffle.r.native/fficall/src/jni/main_rlocale.c b/com.oracle.truffle.r.native/fficall/src/common/main_rlocale.c
similarity index 96%
rename from com.oracle.truffle.r.native/fficall/src/jni/main_rlocale.c
rename to com.oracle.truffle.r.native/fficall/src/common/main_rlocale.c
index 4f8605f552..7353dcd990 100644
--- a/com.oracle.truffle.r.native/fficall/src/jni/main_rlocale.c
+++ b/com.oracle.truffle.r.native/fficall/src/common/main_rlocale.c
@@ -21,8 +21,4 @@
  * questions.
  */
 
-#define attribute_hidden
-
-#include "Defn.h"
-
 #include "../../../gnur/R-3.1.3/src/main/rlocale.c"
diff --git a/com.oracle.truffle.r.native/fficall/src/jni/main_sort.c b/com.oracle.truffle.r.native/fficall/src/common/main_sort.c
similarity index 96%
rename from com.oracle.truffle.r.native/fficall/src/jni/main_sort.c
rename to com.oracle.truffle.r.native/fficall/src/common/main_sort.c
index 0b07d6869b..50e9ce639f 100644
--- a/com.oracle.truffle.r.native/fficall/src/jni/main_sort.c
+++ b/com.oracle.truffle.r.native/fficall/src/common/main_sort.c
@@ -21,8 +21,4 @@
  * questions.
  */
 
-#define attribute_hidden
-
-#include "Defn.h"
-
 #include "../../../gnur/R-3.1.3/src/main/sort.c"
diff --git a/com.oracle.truffle.r.native/fficall/src/common/rlocale.h b/com.oracle.truffle.r.native/fficall/src/common/rlocale.h
new file mode 100644
index 0000000000..f1da411d1b
--- /dev/null
+++ b/com.oracle.truffle.r.native/fficall/src/common/rlocale.h
@@ -0,0 +1,15 @@
+/*
+ * This material is distributed under the GNU General Public License
+ * Version 2. You may review the terms of this license at
+ * http://www.gnu.org/licenses/gpl-2.0.html
+ *
+ * Copyright (c) 1995-2015, The R Core Team
+ * Copyright (c) 2003, The R Foundation
+ * Copyright (c) 2015, Oracle and/or its affiliates
+ *
+ * All rights reserved.
+ */
+
+// This is a cut-down version of src/include/rlocale.h that is a safe replacement for use with FastR
+
+#include "../../../gnur/R-3.1.3/src/include/rlocale.h"
diff --git a/com.oracle.truffle.r.native/fficall/src/jni/sys.c b/com.oracle.truffle.r.native/fficall/src/common/sys.c
similarity index 100%
rename from com.oracle.truffle.r.native/fficall/src/jni/sys.c
rename to com.oracle.truffle.r.native/fficall/src/common/sys.c
diff --git a/com.oracle.truffle.r.native/fficall/src/common/variable_defs.h b/com.oracle.truffle.r.native/fficall/src/common/variable_defs.h
index 51a953eebe..0ed51318fa 100644
--- a/com.oracle.truffle.r.native/fficall/src/common/variable_defs.h
+++ b/com.oracle.truffle.r.native/fficall/src/common/variable_defs.h
@@ -9,10 +9,10 @@
  *
  * All rights reserved.
  */
-#ifndef VARIABLE_DEFS_H_
-#define VARIABLE_DEFS_H_
 
-// The global variables that are assumed by the R FFI.
+// These are the definitions (i.e., not extern) that are defined as extern in RInternals.h.
+// They are in a a separate header to support a JNI and non-JNI implementation of their values.
+// Therefore this file must only be included by the implementation.
 // N.B. Some variables become functions in FastR, see RInternals.h
 
 /* Evaluation Environment */
@@ -92,5 +92,5 @@ Rboolean utf8locale = FALSE;
 Rboolean mbcslocale = FALSE;
 Rboolean latin1locale = FALSE;
 int R_dec_min_exponent = -308;
+int max_contour_segments = 25000;
 
-#endif /* VARIABLE_DEFS_H_ */
diff --git a/com.oracle.truffle.r.native/fficall/src/jni/xxxpr.f b/com.oracle.truffle.r.native/fficall/src/common/xxxpr.f
similarity index 100%
rename from com.oracle.truffle.r.native/fficall/src/jni/xxxpr.f
rename to com.oracle.truffle.r.native/fficall/src/common/xxxpr.f
diff --git a/com.oracle.truffle.r.native/fficall/src/jni/Applic.c b/com.oracle.truffle.r.native/fficall/src/jni/Applic.c
index e95b1c3c82..97db6cc689 100644
--- a/com.oracle.truffle.r.native/fficall/src/jni/Applic.c
+++ b/com.oracle.truffle.r.native/fficall/src/jni/Applic.c
@@ -20,7 +20,7 @@
  * or visit www.oracle.com if you need additional information or have any
  * questions.
  */
-#include "rffiutils.h"
+#include <rffiutils.h>
 #include <R_ext/Applic.h>
 
 void init_applic(JNIEnv *env) {
diff --git a/com.oracle.truffle.r.native/fficall/src/jni/Connections.c b/com.oracle.truffle.r.native/fficall/src/jni/Connections.c
index 9114fdd120..812f804dcd 100644
--- a/com.oracle.truffle.r.native/fficall/src/jni/Connections.c
+++ b/com.oracle.truffle.r.native/fficall/src/jni/Connections.c
@@ -21,7 +21,7 @@
  * questions.
  */
 
-#include "rffiutils.h"
+#include <rffiutils.h>
 #include <R_ext/Connections.h>
 
 SEXP R_new_custom_connection(const char *description, const char *mode, const char *class_name, Rconnection *ptr) {
diff --git a/com.oracle.truffle.r.native/fficall/src/jni/Defn.h b/com.oracle.truffle.r.native/fficall/src/jni/Defn.h
deleted file mode 100644
index da43dd42f3..0000000000
--- a/com.oracle.truffle.r.native/fficall/src/jni/Defn.h
+++ /dev/null
@@ -1,46 +0,0 @@
-/*
- * Copyright (c) 2015, 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.
- */
-
-#define DEFN_H_
-
-#include <jni.h>
-#include <stdlib.h>
-#include <Rinternals.h>
-
-// various definitions required to compile GNU-R code:
-#define F77_SYMBOL(x)	x
-#define F77_QSYMBOL(x) #x
-
-#define Rexp10(x) pow(10.0, x)
-
-// no NLS:
-#ifndef _
-#define _(String) (String)
-#endif
-#define N_(String) String
-#define ngettext(String, StringP, N) (N > 1 ? StringP: String)
-
-void sortVector(SEXP, Rboolean);
-int Scollate(SEXP a, SEXP b);
-void Rf_checkArityCall(SEXP, SEXP, SEXP);
-
diff --git a/com.oracle.truffle.r.native/fficall/src/jni/Makefile b/com.oracle.truffle.r.native/fficall/src/jni/Makefile
index b922b435e8..d8ab824a8a 100644
--- a/com.oracle.truffle.r.native/fficall/src/jni/Makefile
+++ b/com.oracle.truffle.r.native/fficall/src/jni/Makefile
@@ -33,37 +33,35 @@ endif
 
 OBJ = ../../lib
 
-COMMON = ../common
+COMMON = $(abspath ../common)
 C_COMMON_SOURCES := $(notdir $(wildcard $(COMMON)/*.c))
-#$(info C_COMMON_SOURCE=$(C_COMMON_SOURCES))
+$(info C_COMMON_SOURCE=$(C_COMMON_SOURCES))
+
+F_COMMON_SOURCES := $(notdir $(wildcard $(COMMON)/*.f))
+F_OBJECTS := $(patsubst %.f,$(OBJ)/%.o,$(F_COMMON_SOURCES))
+
+C_LOCAL_SOURCES := $(wildcard *.c)
 
-# if link was already set, C_COMMON_SOURCES will be included twice without following filter
-C_LOCAL_SOURCES := $(filter-out  $(C_COMMON_SOURCES),$(wildcard *.c))
-#$(info C_LOCAL_SOURCES=$(C_LOCAL_SOURCES))
 C_HDRS := $(wildcard *.h)
 C_LIBNAME := librfficall$(DYLIB_EXT)
 C_LIB := $(TOPDIR)/../lib/$(C_LIBNAME)
 
 C_SOURCES = $(C_LOCAL_SOURCES) $(C_COMMON_SOURCES)
-#$(info C_SOURCES=$(C_SOURCES))
-C_OBJECTS := $(patsubst %.c,$(OBJ)/%.o,$(C_SOURCES))
-#$(info C_OBJECTS=$(C_OBJECTS))
-
-F_SOURCES := $(wildcard *.f)
-F_OBJECTS := $(patsubst %.f,$(OBJ)/%.o,$(F_SOURCES))
+$(info C_SOURCES=$(C_SOURCES))
+C_OBJECTS := $(patsubst %.c,$(OBJ)/%.o,$(C_LOCAL_SOURCES)) $(patsubst %.c,$(OBJ)/%.o,$(C_COMMON_SOURCES))
+$(info C_OBJECTS=$(C_OBJECTS))
 
 JNI_INCLUDES = -I $(JAVA_HOME)/include -I $(JAVA_HOME)/include/$(JDK_OS_DIR)
-FFI_INCLUDES = -I$(TOPDIR)/include -I$(TOPDIR)/gnur/R-3.1.3/src/include -I$(TOPDIR)/include/R_ext
+LOCAL_INCLUDES = -I. -I$(COMMON) 
+FFI_INCLUDES = -I$(TOPDIR)/include -I$(TOPDIR)/include/R_ext
 
-# The need for thhis should be reviewed
-GNUR_INCLUDES = -I$(TOPDIR)/gnur/R-3.1.3/src/include -I$(TOPDIR)/gnur/R-3.1.3/src/nmath
+INCLUDES := $(LOCAL_INCLUDES) $(JNI_INCLUDES) $(FFI_INCLUDES)
 
-INCLUDES := $(JNI_INCLUDES) $(FFI_INCLUDES) $(GNUR_INCLUDES)
+COMMON_INCLUDES := $(LOCAL_INCLUDES) $(JNI_INCLUDES) $(FFI_INCLUDES) -I$(TOPDIR)/gnur/R-3.1.3/src/nmath
 
-all: Makefile $(C_COMMON_SOURCES) $(C_LIB) 
+#CFLAGS := $(CFLAGS) -H
 
-links:
-	$(foreach file,$(C_COMMON_SOURCES),ln -sf $(COMMON)/$(file) $(file);)	
+all: Makefile $(C_LIB) 
 
 $(C_LIB): $(OBJ) $(C_OBJECTS) $(F_OBJECTS)
 	$(DYLIB_LD) $(DYLIB_LDFLAGS) -o $(C_LIB) $(C_OBJECTS) $(F_OBJECTS)
@@ -74,12 +72,15 @@ $(OBJ):
 $(OBJ)/%.o: %.c $(TOPDIR)/include/Rinternals.h $(C_HDRS)
 	$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@
 	
-$(OBJ)/%.E: %.c $(TOPDIR)/include/Rinternals.h
-	$(CC) -E $(CFLAGS) $(INCLUDES) -c $< > $@
+$(OBJ)/%.o: $(COMMON)/%.c $(TOPDIR)/include/Rinternals.h $(C_HDRS)
+	$(CC) $(CFLAGS) $(COMMON_INCLUDES) -c $< -o $@
 
-$(OBJ)/%.o: %.f
+$(OBJ)/%.o: $(COMMON)/%.f
 	$(F77) $(FFLAGS) $(FPICFLAGS) -c $< -o $@
 
+# for debugging, to see what's really being compiled
+$(OBJ)/%.E: %.c $(TOPDIR)/include/Rinternals.h
+	$(CC) -E $(CFLAGS) $(INCLUDES) -c $< > $@
+
 clean:
 	rm -rf $(OBJ) $(C_LIB)
-	$(foreach file,$(C_COMMON_SOURCES),rm -f $(file);)	
diff --git a/com.oracle.truffle.r.native/fficall/src/jni/Memory.c b/com.oracle.truffle.r.native/fficall/src/jni/Memory.c
index c1e6b127cc..d166685891 100644
--- a/com.oracle.truffle.r.native/fficall/src/jni/Memory.c
+++ b/com.oracle.truffle.r.native/fficall/src/jni/Memory.c
@@ -9,7 +9,7 @@
  *
  * All rights reserved.
  */
-#include "rffiutils.h"
+#include <rffiutils.h>
 #include <stdlib.h>
 #include <string.h>
 
diff --git a/com.oracle.truffle.r.native/fficall/src/jni/Parse.c b/com.oracle.truffle.r.native/fficall/src/jni/Parse.c
index bf3b650784..387a7b43cb 100644
--- a/com.oracle.truffle.r.native/fficall/src/jni/Parse.c
+++ b/com.oracle.truffle.r.native/fficall/src/jni/Parse.c
@@ -20,7 +20,7 @@
  * or visit www.oracle.com if you need additional information or have any
  * questions.
  */
-#include "rffiutils.h"
+#include <rffiutils.h>
 #include <R_ext/Parse.h>
 
 SEXP R_ParseVector(SEXP x, int y, ParseStatus *z, SEXP w) {
diff --git a/com.oracle.truffle.r.native/fficall/src/jni/Random.c b/com.oracle.truffle.r.native/fficall/src/jni/Random.c
index be1c5cabdd..23656cdcf0 100644
--- a/com.oracle.truffle.r.native/fficall/src/jni/Random.c
+++ b/com.oracle.truffle.r.native/fficall/src/jni/Random.c
@@ -20,7 +20,7 @@
  * or visit www.oracle.com if you need additional information or have any
  * questions.
  */
-#include "rffiutils.h"
+#include <rffiutils.h>
 
 void init_random(JNIEnv *env) {
 }
diff --git a/com.oracle.truffle.r.native/fficall/src/jni/Rdynload.c b/com.oracle.truffle.r.native/fficall/src/jni/Rdynload.c
index 98bb8fc3c5..2983383669 100644
--- a/com.oracle.truffle.r.native/fficall/src/jni/Rdynload.c
+++ b/com.oracle.truffle.r.native/fficall/src/jni/Rdynload.c
@@ -9,7 +9,7 @@
  *
  * All rights reserved.
  */
-#include "rffiutils.h"
+#include <rffiutils.h>
 #include <Rdynload.h>
 
 // Registering routines from loaded shared libraries
@@ -133,9 +133,11 @@ Rboolean R_forceSymbols(DllInfo *dllInfo, Rboolean value) {
 
 DL_FUNC R_GetCCallable(const char *package, const char *name) {
 	unimplemented("R_GetCCallable");
+	return NULL;
 }
 
 DL_FUNC R_FindSymbol(char const *name, char const *pkg,
 		     R_RegisteredNativeSymbol *symbol) {
     unimplemented("R_FindSymbol");
+    return NULL;
 }
diff --git a/com.oracle.truffle.r.native/fficall/src/jni/Riconv.c b/com.oracle.truffle.r.native/fficall/src/jni/Riconv.c
index 886a31e5d3..3d54c1d6bf 100644
--- a/com.oracle.truffle.r.native/fficall/src/jni/Riconv.c
+++ b/com.oracle.truffle.r.native/fficall/src/jni/Riconv.c
@@ -20,7 +20,7 @@
  * or visit www.oracle.com if you need additional information or have any
  * questions.
  */
-#include "rffiutils.h"
+#include <rffiutils.h>
 #include <Riconv.h>
 
 void * Riconv_open (const char* tocode, const char* fromcode) {
diff --git a/com.oracle.truffle.r.native/fficall/src/jni/Rinternals.c b/com.oracle.truffle.r.native/fficall/src/jni/Rinternals.c
index df0edbd40e..5502b09dee 100644
--- a/com.oracle.truffle.r.native/fficall/src/jni/Rinternals.c
+++ b/com.oracle.truffle.r.native/fficall/src/jni/Rinternals.c
@@ -20,7 +20,7 @@
  * or visit www.oracle.com if you need additional information or have any
  * questions.
  */
-#include "rffiutils.h"
+#include <rffiutils.h>
 #include <string.h>
 
 // Most everything in RInternals.h
diff --git a/com.oracle.truffle.r.native/fficall/src/jni/Rmath.c b/com.oracle.truffle.r.native/fficall/src/jni/Rmath.c
index c847d6e6a3..2182b9c413 100644
--- a/com.oracle.truffle.r.native/fficall/src/jni/Rmath.c
+++ b/com.oracle.truffle.r.native/fficall/src/jni/Rmath.c
@@ -20,7 +20,7 @@
  * or visit www.oracle.com if you need additional information or have any
  * questions.
  */
-#include "rffiutils.h"
+#include <rffiutils.h>
 
 void init_rmath(JNIEnv *env) {
 
diff --git a/com.oracle.truffle.r.native/fficall/src/jni/Utils.c b/com.oracle.truffle.r.native/fficall/src/jni/Utils.c
index a8820e2ebe..a67edcbc04 100644
--- a/com.oracle.truffle.r.native/fficall/src/jni/Utils.c
+++ b/com.oracle.truffle.r.native/fficall/src/jni/Utils.c
@@ -21,7 +21,7 @@
  * questions.
  */
 
-#include "rffiutils.h"
+#include <rffiutils.h>
 
 void R_CheckStack(void) {
     // TODO: check for stack overflow
diff --git a/com.oracle.truffle.r.native/fficall/src/jni/main_engine.c b/com.oracle.truffle.r.native/fficall/src/jni/main_engine.c
deleted file mode 100644
index a06868cb5d..0000000000
--- a/com.oracle.truffle.r.native/fficall/src/jni/main_engine.c
+++ /dev/null
@@ -1,70 +0,0 @@
-/*
- * This material is distributed under the GNU General Public License
- * Version 2. You may review the terms of this license at
- * http://www.gnu.org/licenses/gpl-2.0.html
- *
- * Copyright (c) 1995-2015, The R Core Team
- * Copyright (c) 2003, The R Foundation
- * Copyright (c) 2015, Oracle and/or its affiliates
- *
- * All rights reserved.
- */
-
-#include "rffiutils.h"
-#include "Defn.h"
-
-extern SEXP R_DevicesSymbol;
-extern Rboolean R_Interactive;
-extern Rboolean R_Visible;
-
-#define attribute_hidden
-#define R_MaxDevices 64
-
-#define HAVE_MBSTATE_T 1
-
-extern Rboolean utf8locale;
-extern Rboolean mbcslocale;
-extern Rboolean latin1locale;
-
-#include <wchar.h>
-
-
-/* The type of the do_xxxx functions. */
-/* These are the built-in R functions. */
-typedef SEXP (*CCODE)(SEXP, SEXP, SEXP, SEXP);
-
-CCODE (PRIMFUN)(SEXP x);
-
-#define streql(s, t)	(!strcmp((s), (t)))
-
-
-/* main/util.c */
-void UNIMPLEMENTED_TYPE(const char *s, SEXP x);
-void UNIMPLEMENTED_TYPEt(const char *s, SEXPTYPE t);
-Rboolean Rf_strIsASCII(const char *str);
-int utf8clen(char c);
-
-typedef unsigned short ucs2_t;
-size_t mbcsToUcs2(const char *in, ucs2_t *out, int nout, int enc);
-/* size_t mbcsMblen(char *in);
-size_t ucs2ToMbcs(ucs2_t *in, char *out);
-size_t ucs2Mblen(ucs2_t *in); */
-size_t utf8toucs(wchar_t *wc, const char *s);
-size_t utf8towcs(wchar_t *wc, const char *s, size_t n);
-size_t ucstomb(char *s, const unsigned int wc);
-size_t ucstoutf8(char *s, const unsigned int wc);
-size_t mbtoucs(unsigned int *wc, const char *s, size_t n);
-size_t wcstoutf8(char *s, const wchar_t *wc, size_t n);
-
-SEXP Rf_installTrChar(SEXP);
-
-const wchar_t *wtransChar(SEXP x); /* from sysutils.c */
-
-#define mbs_init(x) memset(x, 0, sizeof(mbstate_t))
-size_t Mbrtowc(wchar_t *wc, const char *s, size_t n, mbstate_t *ps);
-Rboolean mbcsValid(const char *str);
-Rboolean utf8Valid(const char *str);
-char *Rf_strchr(const char *s, int c);
-char *Rf_strrchr(const char *s, int c);
-
-#include "../../../gnur/R-3.1.3/src/main/engine.c"
diff --git a/com.oracle.truffle.r.native/fficall/src/jni/rfficall.c b/com.oracle.truffle.r.native/fficall/src/jni/rfficall.c
index 4d42bd864f..34e65d0ee8 100644
--- a/com.oracle.truffle.r.native/fficall/src/jni/rfficall.c
+++ b/com.oracle.truffle.r.native/fficall/src/jni/rfficall.c
@@ -21,7 +21,7 @@
  * questions.
  */
 
-#include "rffiutils.h"
+#include <rffiutils.h>
 #include <string.h>
 #include <setjmp.h>
 
diff --git a/com.oracle.truffle.r.native/fficall/src/jni/rffiutils.c b/com.oracle.truffle.r.native/fficall/src/jni/rffiutils.c
index a3e08bb2f2..d485f0456d 100644
--- a/com.oracle.truffle.r.native/fficall/src/jni/rffiutils.c
+++ b/com.oracle.truffle.r.native/fficall/src/jni/rffiutils.c
@@ -20,7 +20,7 @@
  * or visit www.oracle.com if you need additional information or have any
  * questions.
  */
-#include "rffiutils.h"
+#include <rffiutils.h>
 #include <string.h>
 #include <stdlib.h>
 
diff --git a/com.oracle.truffle.r.native/fficall/src/jni/unimplemented.c b/com.oracle.truffle.r.native/fficall/src/jni/unimplemented.c
index 57bae74e53..e19b9fb285 100644
--- a/com.oracle.truffle.r.native/fficall/src/jni/unimplemented.c
+++ b/com.oracle.truffle.r.native/fficall/src/jni/unimplemented.c
@@ -9,9 +9,9 @@
  *
  * All rights reserved.
  */
-#include "rffiutils.h"
+#include <rffiutils.h>
 
-#include "Defn.h"
+#include <Defn.h>
 #include <Print.h>
 
 Rboolean known_to_be_latin1 = FALSE;
@@ -85,44 +85,4 @@ const char *Rf_EncodeLogical(int x, int w)
 	return NULL;
 }
 
-// from printutils.c
-#ifndef min
-#define min(a, b) (((a)<(b))?(a):(b))
-#endif
-#define NB 1000
-const char *Rf_EncodeReal(double x, int w, int d, int e, char cdec)
-{
-    static char buff[NB];
-    char *p, fmt[20];
-
-    /* IEEE allows signed zeros (yuck!) */
-    if (x == 0.0) x = 0.0;
-    if (!R_FINITE(x)) {
-	if(ISNA(x)) snprintf(buff, NB, "%*s", min(w, (NB-1)), CHAR(R_print.na_string));
-	else if(ISNAN(x)) snprintf(buff, NB, "%*s", min(w, (NB-1)), "NaN");
-	else if(x > 0) snprintf(buff, NB, "%*s", min(w, (NB-1)), "Inf");
-	else snprintf(buff, NB, "%*s", min(w, (NB-1)), "-Inf");
-    }
-    else if (e) {
-	if(d) {
-	    sprintf(fmt,"%%#%d.%de", min(w, (NB-1)), d);
-	    snprintf(buff, NB, fmt, x);
-	}
-	else {
-	    sprintf(fmt,"%%%d.%de", min(w, (NB-1)), d);
-	    snprintf(buff, NB, fmt, x);
-	}
-    }
-    else { /* e = 0 */
-	sprintf(fmt,"%%%d.%df", min(w, (NB-1)), d);
-	snprintf(buff, NB, fmt, x);
-    }
-    buff[NB-1] = '\0';
-
-    if(cdec != '.')
-      for(p = buff; *p; p++) if(*p == '.') *p = cdec;
-
-    return buff;
-}
-
 
diff --git a/com.oracle.truffle.r.native/fficall/src/jni/variables.c b/com.oracle.truffle.r.native/fficall/src/jni/variables.c
index 971a8c8802..d9c9497e38 100644
--- a/com.oracle.truffle.r.native/fficall/src/jni/variables.c
+++ b/com.oracle.truffle.r.native/fficall/src/jni/variables.c
@@ -28,8 +28,8 @@
 #include <string.h>
 #include <jni.h>
 #include <Rinternals.h>
-#include "rffiutils.h"
-#include "../common/variable_defs.h"
+#include <rffiutils.h>
+#include <variable_defs.h>
 
 jmethodID getGlobalEnvMethodID;
 jmethodID getBaseEnvMethodID;
diff --git a/com.oracle.truffle.r.native/library/stats/Makefile b/com.oracle.truffle.r.native/library/stats/Makefile
index 969795ea25..083188194a 100644
--- a/com.oracle.truffle.r.native/library/stats/Makefile
+++ b/com.oracle.truffle.r.native/library/stats/Makefile
@@ -55,6 +55,12 @@ GNUR_C_OBJECTS := $(addprefix $(OBJ)/, $(GNUR_C_FILES:.c=.o)) $(FFT_OBJECT)
 
 include ../lib.mk
 
+# Why is this necessary? Because if fft.c has been created by editing, lib.mk will
+# include it in C_OBJECTS but it's already in GNUR_C_OBJECTS (uncreated case)
+$(info C_OBJECTS=$(C_OBJECTS))
+C_OBJECTS := $(filter-out $(FFT_OBJECT), $(C_OBJECTS))
+$(info C_OBJECTS=$(C_OBJECTS))
+
 $(C_OBJECTS): | $(OBJ)
 
 $(SRC)/fft.c: $(GNUR_FFT) src/ed_fft
-- 
GitLab