Skip to content

Instantly share code, notes, and snippets.

@Happy-Ferret
Last active November 8, 2016 16:17
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Happy-Ferret/c8e0f070c7edca117a6cb74a2df5c50b to your computer and use it in GitHub Desktop.
Save Happy-Ferret/c8e0f070c7edca117a6cb74a2df5c50b to your computer and use it in GitHub Desktop.
Mixed assembly (C/Haskell)
{-# LANGUAGE ForeignFunctionInterface #-}
module Test where
import Foreign.C.Types
hsfun :: CInt -> IO CInt
hsfun x = do
putStrLn "Hello from haskell"
return (42 * x)
hsfoo :: CInt -> IO CInt
hsfoo x = do
putStrLn "Hello foo"
return (1024 * x)
foreign export ccall hsfun :: CInt -> IO CInt
foreign export ccall hsfoo :: CInt -> IO CInt
# Creates a shared library called libMixed.so
ghc -O2 -dynamic -shared -fPIC -o libMixed.so lib.hs module_init.c -lHSrts-ghc7.10.3
#include <HsFFI.h>
extern void __stginit_Test(void);
static void library_init(void) __attribute__((constructor));
static void library_init(void)
{
static char *argv[] = { "libMixed.so", 0 }, **argv_ = argv;
static int argc = 1;
hs_init(&argc, &argv_);
hs_add_root(__stginit_Test);
}
static void library_exit(void) __attribute__((destructor));
static void library_exit(void)
{
hs_exit();
}
int add(int a, int b) {
return a + b;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment