[project @ 1999-01-26 11:12:41 by simonm]
[ghc-hetmet.git] / ghc / compiler / prelude / PrimRep.lhs
1 %
2 % (c) The GRASP Project, Glasgow University, 1992-1998
3 %
4 \section[PrimRep]{Primitive machine-level kinds of things.}
5
6 At various places in the back end, we want to be to tag things with a
7 ``primitive kind''---i.e., the machine-manipulable implementation
8 types.
9
10 \begin{code}
11 module PrimRep 
12       (
13         PrimRep(..)
14       , separateByPtrFollowness
15       , isFollowableRep
16       , isFloatingRep
17       , is64BitRep
18       , getPrimRepSize
19       , getPrimRepSizeInBytes
20       , retPrimRepSize
21       , showPrimRep
22       , primRepString
23       , showPrimRepToUser
24       ) where
25
26 #include "HsVersions.h"
27
28 import Constants ( dOUBLE_SIZE, iNT64_SIZE, wORD64_SIZE )
29 import Util
30 import Outputable
31 \end{code}
32
33 %************************************************************************
34 %*                                                                      *
35 \subsection[PrimRep-datatype]{The @PrimRep@ datatype}
36 %*                                                                      *
37 %************************************************************************
38
39 \begin{code}
40 data PrimRep
41   = -- These pointer-kinds are all really the same, but we keep
42     -- them separate for documentation purposes.
43     PtrRep              -- Pointer to a closure; a ``word''.
44   | CodePtrRep          -- Pointer to code
45   | DataPtrRep          -- Pointer to data
46   | RetRep              -- Pointer to code or data (return vector or code pointer)
47   | CostCentreRep       -- Pointer to a cost centre
48
49   | CharRep             -- Machine characters
50   | IntRep              --         integers (same size as ptr on this arch)
51   | WordRep             --         ditto (but *unsigned*)
52   | AddrRep             --         addresses ("C pointers")
53   | FloatRep            --         floats
54   | DoubleRep           --         doubles
55   | Word64Rep           --    guaranteed to be 64 bits (no more, no less.)
56   | Int64Rep            --    guaranteed to be 64 bits (no more, no less.)
57
58   | WeakPtrRep
59   | ForeignObjRep       
60
61   | StablePtrRep        -- We could replace this with IntRep but maybe
62                         -- there's some documentation gain from having
63                         -- it special? [ADR]
64
65   | StableNameRep       -- A stable name is a real heap object, unpointed,
66                         -- with one field containing an index into the
67                         -- stable pointer table.  It has to be a heap
68                         -- object so the garbage collector can track these
69                         -- objects and reclaim stable pointer entries.
70
71   | ThreadIdRep         -- Really a pointer to a TSO
72
73   | ArrayRep            -- Primitive array of Haskell pointers
74   | ByteArrayRep        -- Primitive array of bytes (no Haskell pointers)
75
76   | VoidRep             -- Occupies no space at all!
77                         -- (Primitive states are mapped onto this)
78   deriving (Eq, Ord)
79         -- Kinds are used in PrimTyCons, which need both Eq and Ord
80 \end{code}
81
82 These pretty much correspond to the C types declared in StgTypes.h,
83 with the following exceptions:
84
85    - when an Array or ByteArray is passed to C, we again pass a pointer
86      to the contents.  The actual type that is passed is StgPtr for
87      ArrayRep, and StgByteArray (probably a char *) for ByteArrayRep.
88
89 These hacks are left until the final printing of the C, in
90 PprAbsC.lhs.
91
92 %************************************************************************
93 %*                                                                      *
94 \subsection[PrimRep-predicates]{Follow-ness, sizes, and such---on @PrimitiveKinds@}
95 %*                                                                      *
96 %************************************************************************
97
98 Whether or not the thing is a pointer that the garbage-collector
99 should follow. Or, to put it another (less confusing) way, whether
100 the object in question is a heap object. 
101
102 Depending on the outcome, this predicate determines what stack
103 the pointer/object possibly will have to be saved onto, and the
104 computation of GC liveness info.
105
106 \begin{code}
107 isFollowableRep :: PrimRep -> Bool
108
109 isFollowableRep PtrRep        = True
110 isFollowableRep ArrayRep      = True    -- all heap objects:
111 isFollowableRep ByteArrayRep  = True    --      ''
112 isFollowableRep WeakPtrRep    = True    --      ''
113 isFollowableRep ForeignObjRep = True    --      ''
114 isFollowableRep StableNameRep = True    --      ''
115 isFollowableRep ThreadIdRep   = True    -- pointer to a TSO
116
117 isFollowableRep other           = False
118
119 separateByPtrFollowness :: (a -> PrimRep) -> [a] -> ([a], [a])
120
121 separateByPtrFollowness kind_fun things
122   = sep_things kind_fun things [] []
123     -- accumulating params for follow-able and don't-follow things...
124   where
125     sep_things kfun []     bs us = (reverse bs, reverse us)
126     sep_things kfun (t:ts) bs us
127       = if (isFollowableRep . kfun) t then
128             sep_things kfun ts (t:bs) us
129         else
130             sep_things kfun ts bs (t:us)
131 \end{code}
132
133 @isFloatingRep@ is used to distinguish @Double@ and @Float@ which
134 cause inadvertent numeric conversions if you aren't jolly careful.
135 See codeGen/CgCon:cgTopRhsCon.
136
137 \begin{code}
138 isFloatingRep :: PrimRep -> Bool
139
140 isFloatingRep DoubleRep = True
141 isFloatingRep FloatRep  = True
142 isFloatingRep other     = False
143
144 \end{code}
145
146 \begin{code}
147 is64BitRep :: PrimRep -> Bool
148
149 is64BitRep Int64Rep  = True
150 is64BitRep Word64Rep = True
151 is64BitRep other     = False
152
153 \end{code}
154
155
156
157 \begin{code}
158 getPrimRepSize :: PrimRep -> Int
159
160 getPrimRepSize DoubleRep  = dOUBLE_SIZE -- "words", of course
161 getPrimRepSize Word64Rep  = wORD64_SIZE
162 getPrimRepSize Int64Rep   = iNT64_SIZE
163 --getPrimRepSize FloatRep = 1
164 --getPrimRepSize CharRep  = 1   -- ToDo: count in bytes?
165 --getPrimRepSize ArrayRep = 1   -- Listed specifically for *documentation*
166 --getPrimRepSize ByteArrayRep = 1
167 getPrimRepSize VoidRep    = 0
168 getPrimRepSize other      = 1
169
170 retPrimRepSize = getPrimRepSize RetRep
171
172 -- size in bytes, ToDo: cpp in the right vals.
173 -- (used in some settings to figure out how many bytes
174 -- we have to push onto the stack when calling external
175 -- entry points (e.g., stdcalling on win32))
176 getPrimRepSizeInBytes :: PrimRep -> Int
177 getPrimRepSizeInBytes pr =
178  case pr of
179     CharRep        ->    1
180     IntRep         ->    4
181     AddrRep        ->    4
182     FloatRep       ->    4
183     DoubleRep      ->    8
184     Word64Rep      ->    8
185     Int64Rep       ->    8
186     WeakPtrRep     ->    4
187     ForeignObjRep  ->    4
188     StablePtrRep   ->    4
189     StableNameRep  ->    4
190     ArrayRep       ->    4
191     ByteArrayRep   ->    4
192     _              ->   panic "getPrimRepSize: ouch - this wasn't supposed to happen!"
193
194 \end{code}
195
196 %************************************************************************
197 %*                                                                      *
198 \subsection[PrimRep-instances]{Boring instance decls for @PrimRep@}
199 %*                                                                      *
200 %************************************************************************
201
202 \begin{code}
203 instance Outputable PrimRep where
204     ppr kind = text (showPrimRep kind)
205
206 showPrimRep  :: PrimRep -> String
207 showPrimRepToUser :: PrimRep -> String
208
209 showPrimRep PtrRep         = "P_"       -- short for StgPtr
210
211 showPrimRep CodePtrRep     = "P_"       -- DEATH to StgFunPtr! (94/02/22 WDP)
212 showPrimRep PtrRep         = "P_"       -- short for StgPtr
213 showPrimRep CodePtrRep     = "P_"       -- DEATH to StgFunPtr! (94/02/22 WDP)
214 showPrimRep DataPtrRep     = "D_"
215 showPrimRep RetRep         = "P_"
216 showPrimRep CostCentreRep  = "CostCentre"
217 showPrimRep CharRep        = "C_"
218 showPrimRep IntRep         = "I_"       -- short for StgInt
219 showPrimRep WordRep        = "W_"       -- short for StgWord
220 showPrimRep Int64Rep       = "LI_"       -- short for StgLongInt
221 showPrimRep Word64Rep      = "LW_"       -- short for StgLongWord
222 showPrimRep AddrRep        = "StgAddr"
223 showPrimRep FloatRep       = "StgFloat"
224 showPrimRep DoubleRep      = "StgDouble"
225 showPrimRep ArrayRep       = "P_" -- see comment below
226 showPrimRep ByteArrayRep   = "StgByteArray"
227 showPrimRep StablePtrRep   = "StgStablePtr"
228 showPrimRep StableNameRep  = "P_"
229 showPrimRep ThreadIdRep    = "StgTSO*"
230 showPrimRep WeakPtrRep     = "P_"
231 showPrimRep ForeignObjRep  = "StgAddr"
232 showPrimRep VoidRep        = "!!VOID_KIND!!"
233
234 primRepString CharRep           = "Char"
235 primRepString IntRep            = "Int"
236 primRepString WordRep           = "Word"
237 primRepString Int64Rep          = "Int64"
238 primRepString Word64Rep         = "Word64"
239 primRepString AddrRep           = "Addr"
240 primRepString FloatRep          = "Float"
241 primRepString DoubleRep         = "Double"
242 primRepString WeakPtrRep        = "Weak"
243 primRepString ForeignObjRep     = "ForeignObj"
244 primRepString StablePtrRep      = "StablePtr"
245 primRepString StableNameRep     = "StableName"
246 primRepString other             = pprPanic "primRepString" (ppr other)
247
248 showPrimRepToUser pr = primRepString pr
249 \end{code}
250
251 Foreign Objects and Arrays are treated specially by the code for
252 _ccall_s: we pass a pointer to the contents of the object, not the
253 object itself.