4c87fe5429a428ba854562e0ff47710ebc27c4f2
[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        -- guaranteed to be represented by a pointer
62
63   | StableNameRep       -- A stable name is a real heap object, unpointed,
64                         -- with one field containing an index into the
65                         -- stable pointer table.  It has to be a heap
66                         -- object so the garbage collector can track these
67                         -- objects and reclaim stable pointer entries.
68
69   | ThreadIdRep         -- Really a pointer to a TSO
70
71   | ArrayRep            -- Primitive array of Haskell pointers
72   | ByteArrayRep        -- Primitive array of bytes (no Haskell pointers)
73
74   | VoidRep             -- Occupies no space at all!
75                         -- (Primitive states are mapped onto this)
76   deriving (Eq, Ord)
77         -- Kinds are used in PrimTyCons, which need both Eq and Ord
78 \end{code}
79
80 These pretty much correspond to the C types declared in StgTypes.h,
81 with the following exceptions:
82
83    - when an Array or ByteArray is passed to C, we again pass a pointer
84      to the contents.  The actual type that is passed is StgPtr for
85      ArrayRep, and StgByteArray (probably a char *) for ByteArrayRep.
86
87 These hacks are left until the final printing of the C, in
88 PprAbsC.lhs.
89
90 %************************************************************************
91 %*                                                                      *
92 \subsection[PrimRep-predicates]{Follow-ness, sizes, and such---on @PrimitiveKinds@}
93 %*                                                                      *
94 %************************************************************************
95
96 Whether or not the thing is a pointer that the garbage-collector
97 should follow. Or, to put it another (less confusing) way, whether
98 the object in question is a heap object. 
99
100 Depending on the outcome, this predicate determines what stack
101 the pointer/object possibly will have to be saved onto, and the
102 computation of GC liveness info.
103
104 \begin{code}
105 isFollowableRep :: PrimRep -> Bool
106
107 isFollowableRep PtrRep        = True
108 isFollowableRep ArrayRep      = True    -- all heap objects:
109 isFollowableRep ByteArrayRep  = True    --      ''
110 isFollowableRep WeakPtrRep    = True    --      ''
111 isFollowableRep ForeignObjRep = True    --      ''
112 isFollowableRep StableNameRep = True    --      ''
113 isFollowableRep ThreadIdRep   = True    -- pointer to a TSO
114
115 isFollowableRep other           = False
116
117 separateByPtrFollowness :: (a -> PrimRep) -> [a] -> ([a], [a])
118
119 separateByPtrFollowness kind_fun things
120   = sep_things kind_fun things [] []
121     -- accumulating params for follow-able and don't-follow things...
122   where
123     sep_things kfun []     bs us = (reverse bs, reverse us)
124     sep_things kfun (t:ts) bs us
125       = if (isFollowableRep . kfun) t then
126             sep_things kfun ts (t:bs) us
127         else
128             sep_things kfun ts bs (t:us)
129 \end{code}
130
131 @isFloatingRep@ is used to distinguish @Double@ and @Float@ which
132 cause inadvertent numeric conversions if you aren't jolly careful.
133 See codeGen/CgCon:cgTopRhsCon.
134
135 \begin{code}
136 isFloatingRep :: PrimRep -> Bool
137
138 isFloatingRep DoubleRep = True
139 isFloatingRep FloatRep  = True
140 isFloatingRep other     = False
141
142 \end{code}
143
144 \begin{code}
145 is64BitRep :: PrimRep -> Bool
146
147 is64BitRep Int64Rep  = True
148 is64BitRep Word64Rep = True
149 is64BitRep other     = False
150
151 \end{code}
152
153
154
155 \begin{code}
156 getPrimRepSize :: PrimRep -> Int
157
158 getPrimRepSize DoubleRep  = dOUBLE_SIZE -- "words", of course
159 getPrimRepSize Word64Rep  = wORD64_SIZE
160 getPrimRepSize Int64Rep   = iNT64_SIZE
161 --getPrimRepSize FloatRep = 1
162 --getPrimRepSize CharRep  = 1   -- ToDo: count in bytes?
163 --getPrimRepSize ArrayRep = 1   -- Listed specifically for *documentation*
164 --getPrimRepSize ByteArrayRep = 1
165 getPrimRepSize VoidRep    = 0
166 getPrimRepSize other      = 1
167
168 retPrimRepSize = getPrimRepSize RetRep
169
170 -- size in bytes, ToDo: cpp in the right vals.
171 -- (used in some settings to figure out how many bytes
172 -- we have to push onto the stack when calling external
173 -- entry points (e.g., stdcalling on win32))
174 getPrimRepSizeInBytes :: PrimRep -> Int
175 getPrimRepSizeInBytes pr =
176  case pr of
177     CharRep        ->    1
178     IntRep         ->    4
179     AddrRep        ->    4
180     FloatRep       ->    4
181     DoubleRep      ->    8
182     Word64Rep      ->    8
183     Int64Rep       ->    8
184     WeakPtrRep     ->    4
185     ForeignObjRep  ->    4
186     StablePtrRep   ->    4
187     StableNameRep  ->    4
188     ArrayRep       ->    4
189     ByteArrayRep   ->    4
190     _              ->   panic "getPrimRepSize: ouch - this wasn't supposed to happen!"
191
192 \end{code}
193
194 %************************************************************************
195 %*                                                                      *
196 \subsection[PrimRep-instances]{Boring instance decls for @PrimRep@}
197 %*                                                                      *
198 %************************************************************************
199
200 \begin{code}
201 instance Outputable PrimRep where
202     ppr kind = text (showPrimRep kind)
203
204 showPrimRep  :: PrimRep -> String
205 showPrimRepToUser :: PrimRep -> String
206
207 showPrimRep PtrRep         = "P_"       -- short for StgPtr
208 showPrimRep CodePtrRep     = "P_"       -- DEATH to StgFunPtr! (94/02/22 WDP)
209 showPrimRep DataPtrRep     = "D_"
210 showPrimRep RetRep         = "P_"
211 showPrimRep CostCentreRep  = "CostCentre"
212 showPrimRep CharRep        = "C_"
213 showPrimRep IntRep         = "I_"       -- short for StgInt
214 showPrimRep WordRep        = "W_"       -- short for StgWord
215 showPrimRep Int64Rep       = "LI_"       -- short for StgLongInt
216 showPrimRep Word64Rep      = "LW_"       -- short for StgLongWord
217 showPrimRep AddrRep        = "StgAddr"
218 showPrimRep FloatRep       = "StgFloat"
219 showPrimRep DoubleRep      = "StgDouble"
220 showPrimRep ArrayRep       = "P_" -- see comment below
221 showPrimRep ByteArrayRep   = "StgByteArray"
222 showPrimRep StablePtrRep   = "StgStablePtr"
223 showPrimRep StableNameRep  = "P_"
224 showPrimRep ThreadIdRep    = "StgTSO*"
225 showPrimRep WeakPtrRep     = "P_"
226 showPrimRep ForeignObjRep  = "StgAddr"
227 showPrimRep VoidRep        = "!!VOID_KIND!!"
228
229 primRepString CharRep           = "Char"
230 primRepString IntRep            = "Int"
231 primRepString WordRep           = "Word"
232 primRepString Int64Rep          = "Int64"
233 primRepString Word64Rep         = "Word64"
234 primRepString AddrRep           = "Addr"
235 primRepString FloatRep          = "Float"
236 primRepString DoubleRep         = "Double"
237 primRepString WeakPtrRep        = "Weak"
238 primRepString ForeignObjRep     = "ForeignObj"
239 primRepString StablePtrRep      = "StablePtr"
240 primRepString StableNameRep     = "StableName"
241 primRepString other             = pprPanic "primRepString" (ppr other)
242
243 showPrimRepToUser pr = primRepString pr
244 \end{code}
245
246 Foreign Objects and Arrays are treated specially by the code for
247 _ccall_s: we pass a pointer to the contents of the object, not the
248 object itself.