Skip to content

Instantly share code, notes, and snippets.

@wavewave
Created January 19, 2013 05:32
Show Gist options
  • Select an option

  • Save wavewave/4570925 to your computer and use it in GitHub Desktop.

Select an option

Save wavewave/4570925 to your computer and use it in GitHub Desktop.
reading llvm bitcode in haskell and execute. llvm created from clang-llvm. (SFMT: mersenne twister)
import Control.Monad
import Data.Int
import Data.Word
import LLVM.Core
import LLVM.ExecutionEngine
import Foreign.C.Types
main = do
initializeNativeTarget
m <- readBitcodeFromFile "SFMT.bc"
funcs <- getModuleValues m
mapM_ (print . fst) funcs
let fio_get_initialized :: Function ( IO Int32 )
Just fio_get_initialized = lookup "get_initialized" funcs >>= castModuleValue
fio_init_gen_rand :: Function (Int32 -> IO ())
Just fio_init_gen_rand = lookup "init_gen_rand" funcs >>= castModuleValue
fio_gen_rand32 :: Function ( IO Word32)
Just fio_gen_rand32 = lookup "gen_rand32" funcs >>= castModuleValue
io_get_initialized <- runEngineAccess (generateFunction fio_get_initialized)
io_init_gen_rand <- runEngineAccess (generateFunction fio_init_gen_rand)
io_gen_rand32 <- runEngineAccess (generateFunction fio_gen_rand32)
io_get_initialized
io_init_gen_rand 30
replicateM_ 50 $ do
x <- io_gen_rand32
print x
return ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment