Created
September 17, 2012 15:44
-
-
Save lewurm/3738101 to your computer and use it in GitHub Desktop.
Haskell: LLVM bindings versus Harpy
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
*.hi | |
*?main |
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 Main where | |
import Foreign | |
import Foreign.C.Types | |
import Control.Monad | |
import Harpy | |
type SomeFunType = CInt -> CInt -> IO CInt | |
foreign import ccall "dynamic" | |
call_int :: FunPtr SomeFunType -> SomeFunType | |
mSomeFun :: CodeGen () () (FunPtr SomeFunType) | |
mSomeFun = do | |
mov eax (Disp 0x4, esp) | |
mov ebx (Disp 0x8, esp) | |
replicateM_ 4000000 (add eax ebx) | |
ret | |
liftM castPtrToFunPtr getEntryPoint | |
main = do | |
(_, Right someFun) <- runCodeGen mSomeFun () () | |
call_int someFun 1337 432 >>= print |
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
module Main where | |
import Data.Word | |
import Data.Int | |
import Control.Monad | |
import LLVM.Core | |
import LLVM.ExecutionEngine | |
mSomeFun :: CodeGenModule (Function (Int32 -> Int32 -> IO Int32)) | |
mSomeFun = | |
createFunction ExternalLinkage $ \ x y -> do | |
r <- foldM (\a _ -> add a y) x [1..4000000] | |
ret r | |
main = do | |
someFun <- simpleFunction mSomeFun | |
someFun 1337 432 >>= print |
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
SHELL := bash | |
all: llvmmain harpymain | |
@echo "llvm..." | |
time ./llvmmain | |
@echo "harpy..." | |
time ./harpymain | |
llvmmain: LLVMMain.hs | |
ghc --make -O2 $< -o $@ | |
harpymain: HarpyMain.hs | |
ghc --make -O2 $< -o $@ | |
clean: | |
rm -rf *.o *.hi llvmmain harpymain |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment