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