Last active
November 8, 2016 16:17
-
-
Save Happy-Ferret/c8e0f070c7edca117a6cb74a2df5c50b to your computer and use it in GitHub Desktop.
Mixed assembly (C/Haskell)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Creates a shared library called libMixed.so | |
ghc -O2 -dynamic -shared -fPIC -o libMixed.so lib.hs module_init.c -lHSrts-ghc7.10.3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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