[project @ 1996-01-08 20:28:12 by partain]
[ghc-hetmet.git] / ghc / lib / glaExts / PreludeGlaMisc.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1994
3 %
4 \section[PreludeGlaMisc]{Miscellaneous Glasgow Stuff}
5
6 \begin{code}
7 module PreludeGlaMisc( PreludeGlaMisc.. {-, PreludePS..-} ) where
8
9 import Cls
10 import Core
11 import IInt
12 import List             ( (++) )
13 import PreludeGlaST
14 import PS               ( _PackedString, _unpackPS )
15 import TyArray          ( Array(..) )
16 import Text
17 \end{code}
18
19 Note: the above used to say:
20
21 \begin{pseudocode}
22 module PreludeGlaMisc( 
23         _MallocPtr,
24
25 #ifndef __PARALLEL_HASKELL__
26         _StablePtr,
27         makeStablePtr, deRefStablePtr, freeStablePtr,
28
29         performGC
30 #endif /* !__PARALLEL_HASKELL__ */
31
32         ) where
33 \end{pseudocode}
34
35 But then the names @_MallocPtr@ and @_StablePtr@ get shoved out into
36 the interface file and anyone importing it becomes unhappy about
37 seeing a preludish name.
38
39 They report: 
40
41 @
42 Bad name on a datatype constructor (a Prelude name?): _MallocPtr
43 @
44
45 (This is horrid!)
46
47 (Oh, btw, don't try not exporting them either - that just makes the
48 info-tables, etc local to this module so that no-one can get at them.)
49
50
51
52
53
54 The next two definitions must match those in
55 @compiler/prelude/TysWiredIn.lhs@ exactly.
56
57 \begin{code}
58 #ifndef __PARALLEL_HASKELL__
59
60 -- ** MOVED TO prelude/TysBasic.hs **
61 -- data _MallocPtr = _MallocPtr MallocPtr#
62 -- data _StablePtr a = _StablePtr (StablePtr# a)
63
64 \end{code}
65
66 Nota Bene: it is important {\em not\/} to inline calls to
67 @makeStablePtr#@ since the corresponding macro is very long and we'll
68 get terrible code-bloat.
69
70 \begin{code}
71 makeStablePtr :: a -> PrimIO (_StablePtr a)
72 deRefStablePtr :: _StablePtr a -> PrimIO a
73 freeStablePtr :: _StablePtr a -> PrimIO ()
74
75 eqMallocPtr :: _MallocPtr -> _MallocPtr -> Bool
76
77 performGC :: PrimIO ()
78
79 {-# INLINE deRefStablePtr #-}
80 {-# INLINE freeStablePtr #-}
81 {-# INLINE performGC #-}
82
83 makeStablePtr f (S# rw1#) = 
84         case makeStablePtr# f rw1# of
85           StateAndStablePtr# rw2# sp# -> (_StablePtr sp#, S# rw2#)
86
87 deRefStablePtr (_StablePtr sp#) (S# rw1#) =
88         case deRefStablePtr# sp# rw1# of
89           StateAndPtr# rw2# a -> (a, S# rw2#)
90
91 freeStablePtr sp = _ccall_ freeStablePointer sp
92
93 eqMallocPtr mp1 mp2 = unsafePerformPrimIO (
94         _ccall_ eqMallocPtr mp1 mp2
95         )
96         /= (0::Int)
97
98 instance Eq _MallocPtr where 
99         p == q = eqMallocPtr p q
100         p /= q = if eqMallocPtr p q then False else True
101
102 performGC = _ccall_GC_ StgPerformGarbageCollection
103
104 #endif /* !__PARALLEL_HASKELL__ */
105 \end{code}
106
107 Like they say: this is as good a place as any to put it:
108
109 \begin{code}
110 addr2Int :: _Addr -> Int
111 addr2Int (A# a#) = I# (addr2Int# a#)
112
113 int2Addr :: Int -> _Addr
114 int2Addr (I# i#) = A# (int2Addr# i#)
115 \end{code}