[Git][ghc/ghc][wip/T18854] Document that ccall convention doesn't support varargs

Ben Gamari gitlab at gitlab.haskell.org
Thu Oct 15 20:05:25 UTC 2020



Ben Gamari pushed to branch wip/T18854 at Glasgow Haskell Compiler / GHC


Commits:
be2ee946 by Ben Gamari at 2020-10-15T16:05:15-04:00
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.

- - - - -


3 changed files:

- docs/users_guide/exts/ffi.rst
- testsuite/tests/rts/T5423_c.c
- testsuite/tests/rts/T5423_cmm.cmm


Changes:

=====================================
docs/users_guide/exts/ffi.rst
=====================================
@@ -83,6 +83,21 @@ For more details on the implementation see the Paper:
 Last known to be accessible `here
 <https://www.microsoft.com/en-us/research/wp-content/uploads/2004/09/conc-ffi.pdf>`_.
 
+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
+``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


=====================================
testsuite/tests/rts/T5423_c.c
=====================================
@@ -1,6 +1,24 @@
+#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 r5,
+    StgWord r7,
+    StgWord r8,
+    StgWord r9,
+    StgWord r10
+    )
+{
+  printf("%d  %d  %d  %d  %d  %d  %d  %d  %d  %d\n",
+         r1, r2, r3, r4, r5, r6, r7, r8, r9, r10);
+}


=====================================
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" test_it(r1, r2, r3, r4, r5, r6, r7, r8, r9, r10);
     return (r10);
  }



View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/be2ee9467890b0ebf553d69e0576566dd7fe081b

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/be2ee9467890b0ebf553d69e0576566dd7fe081b
You're receiving this email because of your account on gitlab.haskell.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-commits/attachments/20201015/828be939/attachment-0001.html>


More information about the ghc-commits mailing list