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