On Linux use libffi for allocating executable memory (fixed #738)
[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    = alloca $ \pcode -> do
32         ptr <- _allocateExec (fromIntegral $ codeSize undefined code) pcode
33         pokeArray ptr code
34         code <- peek pcode
35         return (castPtrToFunPtr code)
36    where
37    codeSize :: Storable a => a -> [a] -> Int
38    codeSize dummy array = sizeOf(dummy) * length array
39
40 foreign import ccall unsafe "allocateExec"
41   _allocateExec :: CUInt -> Ptr (Ptr a) -> IO (Ptr a)  
42 \end{code}
43