Created
July 8, 2011 04:35
-
-
Save NathanHowell/1071154 to your computer and use it in GitHub Desktop.
This file contains 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
withArgsM :: Monad m | |
=> Expression | |
-> (forall a . StructFields a => a -> m s) | |
-> m s | |
withArgsM exp f = inner vars f where | |
vars = nub [ x :: Factor | x <- universeBi exp ] | |
inner :: Monad m => [Factor] -> (forall a . StructFields a => a -> m s) -> m s | |
inner (x:xs) f = inner xs (\ x -> f ((undefined::Double) & x)) | |
inner [] f = f () | |
genModule :: forall a . StructFields a | |
=> Expression | |
-> a | |
-> CodeGenModule (Function (Int32 -> Ptr Int8 -> IO Double)) | |
genModule exp _ = createFunction ExternalLinkage (go exp) where | |
go exp idx args = do | |
c :: Value (Ptr (Struct a)) <- bitcast args | |
-- val :: Value (Ptr Double) <- getElementPtr c (0::Int32, (0::Int32, ())) ?? | |
val :: Value (Ptr Double) <- unsafeGetElementPtr c [0::Int32, 0] | |
val' <- load val | |
ret val' | |
compileExpression :: Expression -> IO (FunPtr (Int32 -> Ptr Int8 -> IO Double)) | |
compileExpression exp = withArgsM exp fun where | |
fun :: forall a . StructFields a => a -> IO (FunPtr (Int32 -> Ptr Int8 -> IO Double)) | |
fun a = do | |
m <- newModule | |
fun <- defineModule m (genModule exp a) | |
dumpValue fun | |
runEngineAccess $ do | |
addModule m | |
getPointerToFunction fun |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment