Skip to content

Instantly share code, notes, and snippets.

@vito
Created August 28, 2010 15:55
Show Gist options
  • Save vito/555282 to your computer and use it in GitHub Desktop.
Save vito/555282 to your computer and use it in GitHub Desktop.
import Data.Int
import Data.Time.Clock.POSIX
import LLVM.Core
import LLVM.ExecutionEngine
import System.Environment
main :: IO ()
main = do
as <- getArgs
let num =
case as of
[] -> 20
[x] -> read x
initializeNativeTarget
f <- simpleFunction fib
b <- getPOSIXTime
f num >>= print
a <- getPOSIXTime
print (a - b)
fib :: CodeGenModule (Function (Int32 -> IO Int32))
fib = do
f <- newFunction ExternalLinkage
defineFunction f $ \n -> do
bottom <- newBasicBlock
recurse <- newBasicBlock
cmp <- icmp IntSLT n (2 :: Int32)
condBr cmp bottom recurse
defineBasicBlock bottom
ret (1 :: Int32)
defineBasicBlock recurse
atmp <- sub n (2 :: Int32)
btmp <- sub n (1 :: Int32)
afib <- call f atmp
bfib <- call f btmp
res <- add afib bfib
ret res
return f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment