Skip to content

Instantly share code, notes, and snippets.

@nkpart
Last active February 25, 2020 03:57
Show Gist options
  • Save nkpart/257a035158510b18aae9461bc3a0e99e to your computer and use it in GitHub Desktop.
Save nkpart/257a035158510b18aae9461bc3a0e99e to your computer and use it in GitHub Desktop.
Issues calling C function from Haskell on aarch64

Using GHC 8.8.1 cross compiler, targeting aarch64-apple-ios.

Haskell:

type ArgType = Int32
foreign import ccall safe "f32" f32 :: ArgType -> ... 31 more times -> IO ()

runMe :: IO ()
runMe =
  f32 1 2 3 ... 32

C header

#define ARGTYPE int32_t
#define ARGPRINTF "%d"

void f32(ARGTYPE a1, ... 31 more times);

C impl

void f32(ARGTYPE a1, ... 31 more times) {
  printf("a1 " ARGPRINTF " ", a1);
  ... 31 more times...
}

Output:

a1 1 a2 2 a3 3 a4 4 a5 5 a6 6 a7 7 a8 8 a9 9 a10 0 a11 10 a12 0 a13 11 a14 0 a16 12 a17 0 a18 0 a19 14 a20 0 a21 15 a22 0 a23 16 a24 0 a25 17 a26 0 a27 18 a28 0 a29 19 a30 0 a31 20 a32 0

With Int64/int64_t/%lld

Output:

a1 1 a2 2 a3 3 a4 4 a5 5 a6 6 a7 7 a8 8 a9 9 a10 10 a11 11 a12 12 a13 13 a14 14 a16 15 a17 16 a18 18 a19 19 a20 20 a21 21 a22 22 a23 23 a24 24 a25 25 a26 26 a27 27 a28 28 a29 29 a30 30 a31 31 a32 32

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment