Fix scoped type variables for expression type signatures
[ghc-hetmet.git] / compiler / ghci / ObjLink.lhs
1 %
2 % (c) The University of Glasgow, 2000
3 %
4
5 -- ---------------------------------------------------------------------------
6 --      The dynamic linker for object code (.o .so .dll files)
7 -- ---------------------------------------------------------------------------
8
9 Primarily, this module consists of an interface to the C-land dynamic linker.
10
11 \begin{code}
12 {-# OPTIONS -#include "Linker.h" #-}
13
14 module ObjLink ( 
15    initObjLinker,        -- :: IO ()
16    loadDLL,              -- :: String -> IO (Maybe String)
17    loadObj,              -- :: String -> IO ()
18    unloadObj,            -- :: String -> IO ()
19    insertSymbol,         -- :: String -> String -> Ptr a -> IO ()
20    insertStableSymbol,   -- :: String -> String -> a -> IO ()
21    lookupSymbol,         -- :: String -> IO (Maybe (Ptr a))
22    resolveObjs           -- :: IO SuccessFlag
23   )  where
24
25 import Monad            ( when )
26
27 import Foreign.C
28 import Foreign          ( nullPtr )
29 import Panic            ( panic )
30 import BasicTypes       ( SuccessFlag, successIf )
31 import Config           ( cLeadingUnderscore )
32 import Outputable
33
34 import GHC.Exts         ( Ptr(..), unsafeCoerce# )
35
36 -- ---------------------------------------------------------------------------
37 -- RTS Linker Interface
38 -- ---------------------------------------------------------------------------
39
40 insertSymbol :: String -> String -> Ptr a -> IO ()
41 insertSymbol obj_name key symbol
42     = let str = prefixUnderscore key
43       in withCString obj_name $ \c_obj_name ->
44          withCString str $ \c_str ->
45           c_insertSymbol c_obj_name c_str symbol
46
47 insertStableSymbol :: String -> String -> a -> IO ()
48 insertStableSymbol obj_name key symbol
49     = let str = prefixUnderscore key
50       in withCString obj_name $ \c_obj_name ->
51          withCString str $ \c_str ->
52           c_insertStableSymbol c_obj_name c_str (Ptr (unsafeCoerce# symbol))
53
54 lookupSymbol :: String -> IO (Maybe (Ptr a))
55 lookupSymbol str_in = do
56    let str = prefixUnderscore str_in
57    withCString str $ \c_str -> do
58      addr <- c_lookupSymbol c_str
59      if addr == nullPtr
60         then return Nothing
61         else return (Just addr)
62
63 prefixUnderscore :: String -> String
64 prefixUnderscore
65  | cLeadingUnderscore == "YES" = ('_':)
66  | otherwise                   = id
67
68 loadDLL :: String -> IO (Maybe String)
69 -- Nothing      => success
70 -- Just err_msg => failure
71 loadDLL str = do
72   maybe_errmsg <- withCString str $ \dll -> c_addDLL dll
73   if maybe_errmsg == nullPtr
74         then return Nothing
75         else do str <- peekCString maybe_errmsg
76                 return (Just str)
77
78 loadObj :: String -> IO ()
79 loadObj str = do
80    withCString str $ \c_str -> do
81      r <- c_loadObj c_str
82      when (r == 0) (panic "loadObj: failed")
83
84 unloadObj :: String -> IO ()
85 unloadObj str =
86    withCString str $ \c_str -> do
87      r <- c_unloadObj c_str
88      when (r == 0) (panic "unloadObj: failed")
89
90 resolveObjs :: IO SuccessFlag
91 resolveObjs = do
92    r <- c_resolveObjs
93    return (successIf (r /= 0))
94
95 -- ---------------------------------------------------------------------------
96 -- Foreign declaractions to RTS entry points which does the real work;
97 -- ---------------------------------------------------------------------------
98
99 #if __GLASGOW_HASKELL__ >= 504
100 foreign import ccall unsafe "addDLL"       c_addDLL :: CString -> IO CString
101 foreign import ccall unsafe "initLinker"   initObjLinker :: IO ()
102 foreign import ccall unsafe "insertSymbol" c_insertSymbol :: CString -> CString -> Ptr a -> IO ()
103 foreign import ccall unsafe "insertStableSymbol" c_insertStableSymbol
104     :: CString -> CString -> Ptr a -> IO ()
105 foreign import ccall unsafe "lookupSymbol" c_lookupSymbol :: CString -> IO (Ptr a)
106 foreign import ccall unsafe "loadObj"      c_loadObj :: CString -> IO Int
107 foreign import ccall unsafe "unloadObj"    c_unloadObj :: CString -> IO Int
108 foreign import ccall unsafe "resolveObjs"  c_resolveObjs :: IO Int
109 #else
110 foreign import "addDLL"       unsafe    c_addDLL :: CString -> IO CString
111 foreign import "initLinker"   unsafe    initLinker :: IO ()
112 foreign import "insertSymbol" unsafe    c_insertSymbol :: CString -> CString -> Ptr a -> IO ()
113 foreign import "insertStableSymbol" unsafe c_insertStableSymbol
114     :: CString -> CString -> Ptr a -> IO ()
115 foreign import "lookupSymbol" unsafe    c_lookupSymbol :: CString -> IO (Ptr a)
116 foreign import "loadObj"      unsafe    c_loadObj :: CString -> IO Int
117 foreign import "unloadObj"    unsafe    c_unloadObj :: CString -> IO Int
118 foreign import "resolveObjs"  unsafe    c_resolveObjs :: IO Int
119 #endif
120
121 \end{code}