summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Gamari <ben@smart-cactus.org>2020-10-15 12:26:16 -0400
committerBen Gamari <ben@smart-cactus.org>2021-01-25 14:33:20 -0500
commitb846d37c787574efc502e29f53c172ce20ffd5ad (patch)
tree3f2848d853aa8289ff0bd42fb38dd2199f71cf60
parenteb74dbe526146a26350c3cfe3753240a930196df (diff)
downloadhaskell-b846d37c787574efc502e29f53c172ce20ffd5ad.tar.gz
Document that ccall convention doesn't support varargs
We do not support foreign "C" imports of varargs functions. While this works on amd64, in general the platform's calling convention may need more type information that our Cmm representation can currently provide. For instance, this is the case with Darwin's AArch64 calling convention. Document this fact in the users guide and fix T5423 which makes use of a disallowed foreign import. Closes #18854. (cherry picked from commit 0b7722219ffdb109c3a8b034a8e112d18e6e4336)
-rw-r--r--docs/users_guide/ffi-chap.rst15
-rw-r--r--testsuite/tests/rts/T5423.hs2
-rw-r--r--testsuite/tests/rts/T5423.stdout2
-rw-r--r--testsuite/tests/rts/T5423_c.c28
-rw-r--r--testsuite/tests/rts/T5423_cmm.cmm3
5 files changed, 47 insertions, 3 deletions
diff --git a/docs/users_guide/ffi-chap.rst b/docs/users_guide/ffi-chap.rst
index fb001a5b83..afa2d96e8c 100644
--- a/docs/users_guide/ffi-chap.rst
+++ b/docs/users_guide/ffi-chap.rst
@@ -63,6 +63,21 @@ bytecode interpreter, and further guarantees that ``unsafe`` calls will be
performed in the calling thread.
+Varargs not supported by ``ccall`` calling convention
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+Note that functions requiring varargs arguments are unsupported by the ``ccall``
+calling convention. Foreign imports needing to call such functions should rather
+use the ``capi`` convention, giving an explicit signature for the needed
+call-pattern. For instance, one could write: ::
+
+ foreign import "capi" "printf"
+ my_printf :: Ptr CChar -> CInt -> IO ()
+
+ printInt :: CInt -> IO ()
+ printInt n = my_printf "printed number %d" n
+
+
.. _ffi-ghcexts:
GHC extensions to the FFI Chapter
diff --git a/testsuite/tests/rts/T5423.hs b/testsuite/tests/rts/T5423.hs
index cda87048d7..f8be97cd6e 100644
--- a/testsuite/tests/rts/T5423.hs
+++ b/testsuite/tests/rts/T5423.hs
@@ -1,3 +1,5 @@
+-- | Verify that @foreign import prim@ calls with more than 10 arguments
+-- are lowered correctly.
{-# LANGUAGE MagicHash, GHCForeignImportPrim, UnliftedFFITypes #-}
diff --git a/testsuite/tests/rts/T5423.stdout b/testsuite/tests/rts/T5423.stdout
index fc92992860..43d0fd4348 100644
--- a/testsuite/tests/rts/T5423.stdout
+++ b/testsuite/tests/rts/T5423.stdout
@@ -1,2 +1,2 @@
-111 112 113 114 115 116 117 118 119 120
+111 112 113 114 115 116 117 118 119 120
120
diff --git a/testsuite/tests/rts/T5423_c.c b/testsuite/tests/rts/T5423_c.c
index efc9c845f5..eddba8741c 100644
--- a/testsuite/tests/rts/T5423_c.c
+++ b/testsuite/tests/rts/T5423_c.c
@@ -1,6 +1,34 @@
+#include <Rts.h>
#include <stdio.h>
void flush_stdout(void)
{
fflush(stdout);
}
+
+void print_it(
+ StgWord r1,
+ StgWord r2,
+ StgWord r3,
+ StgWord r4,
+ StgWord r5,
+ StgWord r6,
+ StgWord r7,
+ StgWord r8,
+ StgWord r9,
+ StgWord r10
+ )
+{
+ printf("%" FMT_Word
+ " %" FMT_Word
+ " %" FMT_Word
+ " %" FMT_Word
+ " %" FMT_Word
+ " %" FMT_Word
+ " %" FMT_Word
+ " %" FMT_Word
+ " %" FMT_Word
+ " %" FMT_Word "\n",
+ r1, r2, r3, r4, r5,
+ r6, r7, r8, r9, r10);
+}
diff --git a/testsuite/tests/rts/T5423_cmm.cmm b/testsuite/tests/rts/T5423_cmm.cmm
index 9be0e152e5..5faa9de0cf 100644
--- a/testsuite/tests/rts/T5423_cmm.cmm
+++ b/testsuite/tests/rts/T5423_cmm.cmm
@@ -10,7 +10,6 @@ test (W_ r1,
W_ r9,
W_ r10)
{
- foreign "C" printf("%d %d %d %d %d %d %d %d %d %d\n",
- r1, r2, r3, r4, r5, r6, r7, r8, r9, r10);
+ foreign "C" print_it(r1, r2, r3, r4, r5, r6, r7, r8, r9, r10);
return (r10);
}