Import libffi-3.0.4, and use it to provide FFI support in GHCi
[ghc-hetmet.git] / compiler / ghci / ByteCodeFFI.lhs
1 %
2 % (c) The University of Glasgow 2001-2008
3 %
4
5 ByteCodeGen: Generate machine-code sequences for foreign import
6
7 \begin{code}
8 module ByteCodeFFI ( moan64, newExec ) where
9
10 import Outputable
11 import System.IO
12 import Foreign
13 import Foreign.C
14
15 moan64 :: String -> SDoc -> a
16 moan64 msg pp_rep
17    = unsafePerformIO (
18         hPutStrLn stderr (
19         "\nGHCi's bytecode generation machinery can't handle 64-bit\n" ++
20         "code properly yet.  You can work around this for the time being\n" ++
21         "by compiling this module and all those it imports to object code,\n" ++
22         "and re-starting your GHCi session.  The panic below contains information,\n" ++
23         "intended for the GHC implementors, about the exact place where GHC gave up.\n"
24         )
25      )
26      `seq`
27      pprPanic msg pp_rep
28
29 newExec :: Storable a => [a] -> IO (FunPtr ())
30 newExec code
31    = do ptr <- _allocateExec (fromIntegral $ codeSize undefined code)
32         pokeArray ptr code
33         return (castPtrToFunPtr ptr)
34    where
35    codeSize :: Storable a => a -> [a] -> Int
36    codeSize dummy array = sizeOf(dummy) * length array
37
38 foreign import ccall unsafe "allocateExec"
39   _allocateExec :: CUInt -> IO (Ptr a)  
40 \end{code}
41