[project @ 1997-03-17 20:34:25 by simonpj]
[ghc-hetmet.git] / ghc / lib / glaExts / Foreign.lhs
1 %
2 % (c) The AQUA Project, Glasgow University, 1994-1996
3 %
4
5 \section[Foreign]{Module @Foreign@}
6
7 \begin{code}
8 {-# OPTIONS -fno-implicit-prelude #-}
9
10 module Foreign (
11         module Foreign,
12         ForeignObj(..),
13         Addr, Word
14    ) where
15
16 import STBase
17 import ArrBase
18 import PrelBase
19 import GHC
20 \end{code}
21
22
23 %*********************************************************
24 %*                                                      *
25 \subsection{Classes @CCallable@ and @CReturnable@}
26 %*                                                      *
27 %*********************************************************
28
29 \begin{code}
30 class CCallable   a
31 class CReturnable a
32
33 instance CCallable Char
34 instance CCallable   Char#
35 instance CReturnable Char
36
37 instance CCallable   Int
38 instance CCallable   Int#
39 instance CReturnable Int
40
41 -- DsCCall knows how to pass strings...
42 instance CCallable   [Char]
43
44 instance CCallable   Float
45 instance CCallable   Float#
46 instance CReturnable Float
47
48 instance CCallable   Double
49 instance CCallable   Double#
50 instance CReturnable Double
51
52 instance CCallable Addr
53 instance CCallable Addr#
54 instance CReturnable Addr
55
56 instance CCallable Word
57 instance CCallable Word#
58 instance CReturnable Word
59
60 -- Is this right?
61 instance CCallable (MutableByteArray s ix)
62 instance CCallable (MutableByteArray# s)
63
64 instance CCallable (ByteArray ix)
65 instance CCallable ByteArray#
66
67 instance CReturnable () -- Why, exactly?
68 \end{code}
69
70
71 %*********************************************************
72 %*                                                      *
73 \subsection{Type @ForeignObj@ and its operations}
74 %*                                                      *
75 %*********************************************************
76
77 \begin{code}
78 --Defined in PrelBase: data ForeignObj = ForeignObj ForeignObj#
79 instance CCallable ForeignObj
80 instance CCallable ForeignObj#
81
82 eqForeignObj    :: ForeignObj  -> ForeignObj -> Bool
83 makeForeignObj  :: Addr        -> Addr       -> PrimIO ForeignObj
84 writeForeignObj :: ForeignObj  -> Addr       -> PrimIO ()
85
86 {- derived op - attaching a free() finaliser to a malloc() allocated reference. -}
87 makeMallocPtr   :: Addr        -> PrimIO ForeignObj
88
89 makeForeignObj (A# obj) (A# finaliser) = ST ( \ (S# s#) ->
90     case makeForeignObj# obj finaliser s# of
91       StateAndForeignObj# s1# fo# -> (ForeignObj fo#, S# s1#))
92
93 writeForeignObj (ForeignObj fo#) (A# datum#) = ST ( \ (S# s#) ->
94     case writeForeignObj# fo# datum# s# of { s1# -> ((), S# s1#) } )
95
96 makeMallocPtr a = makeForeignObj a (``&free''::Addr)
97
98 eqForeignObj mp1 mp2
99   = unsafePerformPrimIO (_ccall_ eqForeignObj mp1 mp2) /= (0::Int)
100
101 instance Eq ForeignObj where 
102     p == q = eqForeignObj p q
103     p /= q = not (eqForeignObj p q)
104 \end{code}
105
106
107 %*********************************************************
108 %*                                                      *
109 \subsection{Type @StablePtr@ and its operations}
110 %*                                                      *
111 %*********************************************************
112
113 \begin{code}
114 #ifndef __PARALLEL_HASKELL__
115 data StablePtr a = StablePtr (StablePtr# a)
116 instance CCallable   (StablePtr a)
117 instance CCallable   (StablePtr# a)
118 instance CReturnable (StablePtr a)
119
120 -- Nota Bene: it is important {\em not\/} to inline calls to
121 -- @makeStablePtr#@ since the corresponding macro is very long and we'll
122 -- get terrible code-bloat.
123
124 makeStablePtr  :: a -> PrimIO (StablePtr a)
125 deRefStablePtr :: StablePtr a -> PrimIO a
126 freeStablePtr  :: StablePtr a -> PrimIO ()
127 performGC      :: PrimIO ()
128
129 {-# INLINE deRefStablePtr #-}
130 {-# INLINE freeStablePtr #-}
131 {-# INLINE performGC #-}
132
133 makeStablePtr f = ST $ \ (S# rw1#) ->
134     case makeStablePtr# f rw1# of
135       StateAndStablePtr# rw2# sp# -> (StablePtr sp#, S# rw2#)
136
137 deRefStablePtr (StablePtr sp#) = ST $ \ (S# rw1#) ->
138     case deRefStablePtr# sp# rw1# of
139       StateAndPtr# rw2# a -> (a, S# rw2#)
140
141 freeStablePtr sp = _ccall_ freeStablePointer sp
142
143 performGC = _ccall_GC_ StgPerformGarbageCollection
144
145 #endif /* !__PARALLEL_HASKELL__ */
146 \end{code}
147
148 %*********************************************************
149 %*                                                      *
150 \subsection{Ghastly return types}
151 %*                                                      *
152 %*********************************************************
153
154 \begin{code}
155 #ifndef __PARALLEL_HASKELL__
156 data StateAndStablePtr# s a = StateAndStablePtr# (State# s) (StablePtr# a)
157 #endif
158 data StateAndForeignObj# s  = StateAndForeignObj# (State# s) ForeignObj#
159 \end{code}