@@ -36,13 +36,12 @@ import Data.Array.Accelerate.LLVM.Native.State
3636import Data.Array.Accelerate.LLVM.Native.Target
3737
3838import Control.Concurrent.Unique
39- import Control.Monad
4039import Data.Hashable
4140import Foreign.Ptr
42- import Language.Haskell.TH ( Q , TExp )
41+ import Language.Haskell.TH.Extra ( Q , CodeQ )
4342import Numeric
4443import System.IO.Unsafe
45- import qualified Language.Haskell.TH as TH
44+ import qualified Language.Haskell.TH.Extra as TH
4645import qualified Language.Haskell.TH.Syntax as TH
4746
4847#if __GLASGOW_HASKELL__ >= 806
@@ -58,24 +57,27 @@ instance Embed Native where
5857-- and generate FFI declarations to access the external functions of that file.
5958-- The returned ExecutableR references the new FFI declarations.
6059--
61- embed :: Native -> ObjectR Native -> Q (TExp (ExecutableR Native ))
62- embed target (ObjectR uid nms ! _) = do
63- objFile <- getObjectFile
64- funtab <- forM nms $ \ fn -> return [|| ( $$ (liftSBS fn), $$ (makeFFI fn objFile) ) || ]
65- --
66- [|| NativeR (unsafePerformIO $ newLifetime (FunctionTable $$ (listE funtab))) || ]
60+ embed :: Native -> ObjectR Native -> CodeQ (ExecutableR Native )
61+ embed target (ObjectR uid nms ! _) =
62+ TH. bindCode getObjectFile $ \ objFile ->
63+ [|| NativeR (unsafePerformIO $ newLifetime (FunctionTable $$ (listE (makeTable objFile nms)))) || ]
6764 where
68- listE :: [Q (TExp a )] -> Q (TExp [a ])
69- listE xs = TH. unsafeTExpCoerce (TH. listE (map TH. unTypeQ xs))
70-
71- makeFFI :: ShortByteString -> FilePath -> Q (TExp (FunPtr () ))
72- makeFFI (S8. unpack -> fn) objFile = do
73- i <- TH. runIO newUnique
74- fn' <- TH. newName (" __accelerate_llvm_native_" ++ showHex (hash i) [] )
75- dec <- TH. forImpD TH. CCall TH. Unsafe (' &' : fn) fn' [t | FunPtr () |]
76- ann <- TH. pragAnnD (TH. ValueAnnotation fn') [| (Object objFile) | ]
77- TH. addTopDecls [dec, ann]
78- TH. unsafeTExpCoerce (TH. varE fn')
65+ listE :: [CodeQ a ] -> CodeQ [a ]
66+ listE xs = TH. unsafeCodeCoerce (TH. listE (map TH. unTypeCode xs))
67+
68+ makeTable :: FilePath -> [ShortByteString ] -> [CodeQ (ShortByteString , FunPtr () )]
69+ makeTable objFile = map (\ fn -> [|| ( $$ (liftSBS fn), $$ (makeFFI fn objFile) ) || ])
70+
71+ makeFFI :: ShortByteString -> FilePath -> CodeQ (FunPtr () )
72+ makeFFI (S8. unpack -> fn) objFile = TH. bindCode go (TH. unsafeCodeCoerce . return )
73+ where
74+ go = do
75+ i <- TH. runIO newUnique
76+ fn' <- TH. newName (" __accelerate_llvm_native_" ++ showHex (hash i) [] )
77+ dec <- TH. forImpD TH. CCall TH. Unsafe (' &' : fn) fn' [t | FunPtr () |]
78+ ann <- TH. pragAnnD (TH. ValueAnnotation fn') [| (Object objFile) | ]
79+ TH. addTopDecls [dec, ann]
80+ TH. varE fn'
7981
8082 -- Note: [Template Haskell and raw object files]
8183 --
0 commit comments