8273434390e9fc04f6bd09ef8ee78abf6a8b2887
[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
85 makeForeignObj (A# obj) (A# finaliser) = ST $ \ (S# s#) ->
86     case makeForeignObj# obj finaliser s# of
87       StateAndForeignObj# s1# fo# -> (ForeignObj fo#, S# s1#)
88
89 eqForeignObj mp1 mp2
90   = unsafePerformPrimIO (_ccall_ eqForeignObj mp1 mp2) /= (0::Int)
91
92 instance Eq ForeignObj where 
93     p == q = eqForeignObj p q
94     p /= q = not (eqForeignObj p q)
95 \end{code}
96
97
98 %*********************************************************
99 %*                                                      *
100 \subsection{Type @StablePtr@ and its operations}
101 %*                                                      *
102 %*********************************************************
103
104 \begin{code}
105 #ifndef __PARALLEL_HASKELL__
106 data StablePtr a = StablePtr (StablePtr# a)
107 instance CCallable   (StablePtr a)
108 instance CCallable   (StablePtr# a)
109 instance CReturnable (StablePtr a)
110
111 -- Nota Bene: it is important {\em not\/} to inline calls to
112 -- @makeStablePtr#@ since the corresponding macro is very long and we'll
113 -- get terrible code-bloat.
114
115 makeStablePtr  :: a -> PrimIO (StablePtr a)
116 deRefStablePtr :: StablePtr a -> PrimIO a
117 freeStablePtr  :: StablePtr a -> PrimIO ()
118 performGC      :: PrimIO ()
119
120 {-# INLINE deRefStablePtr #-}
121 {-# INLINE freeStablePtr #-}
122 {-# INLINE performGC #-}
123
124 makeStablePtr f = ST $ \ (S# rw1#) ->
125     case makeStablePtr# f rw1# of
126       StateAndStablePtr# rw2# sp# -> (StablePtr sp#, S# rw2#)
127
128 deRefStablePtr (StablePtr sp#) = ST $ \ (S# rw1#) ->
129     case deRefStablePtr# sp# rw1# of
130       StateAndPtr# rw2# a -> (a, S# rw2#)
131
132 freeStablePtr sp = _ccall_ freeStablePointer sp
133
134 performGC = _ccall_GC_ StgPerformGarbageCollection
135
136 #endif /* !__PARALLEL_HASKELL__ */
137 \end{code}
138
139 %*********************************************************
140 %*                                                      *
141 \subsection{Ghastly return types}
142 %*                                                      *
143 %*********************************************************
144
145 \begin{code}
146 #ifndef __PARALLEL_HASKELL__
147 data StateAndStablePtr# s a = StateAndStablePtr# (State# s) (StablePtr# a)
148 #endif
149 data StateAndForeignObj# s  = StateAndForeignObj# (State# s) ForeignObj#
150 \end{code}