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

Ben Gamari gitlab at gitlab.haskell.org
Mon Oct 19 20:46:50 UTC 2020



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


Commits:
86380ed1 by Ben Gamari at 2020-10-19T16:46:42-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.

- - - - -


4 changed files:

- docs/users_guide/exts/ffi.rst
- testsuite/tests/rts/T5423.hs
- 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
+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


=====================================
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 #-}
 


=====================================
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 r6,
+    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/86380ed17f0d5b2bc0b9d40a9edd03c1c7bbde39

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/86380ed17f0d5b2bc0b9d40a9edd03c1c7bbde39
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/20201019/e1cc75a0/attachment-0001.html>


More information about the ghc-commits mailing list