2 % (c) The GRASP Project, Glasgow University, 1992-1998
4 \section[PrimRep]{Primitive machine-level kinds of things.}
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
14 , separateByPtrFollowness
19 , getPrimRepSizeInBytes
20 , getPrimRepArrayElemSize
24 #include "HsVersions.h"
26 import Constants ( dOUBLE_SIZE, iNT64_SIZE, wORD64_SIZE, wORD_SIZE )
30 %************************************************************************
32 \subsection[PrimRep-datatype]{The @PrimRep@ datatype}
34 %************************************************************************
38 = -- These pointer-kinds are all really the same, but we keep
39 -- them separate for documentation purposes.
40 PtrRep -- Pointer to a closure; a ``word''.
41 | CodePtrRep -- Pointer to code
42 | DataPtrRep -- Pointer to data
43 | RetRep -- Pointer to code or data (return vector or code pointer)
44 | CostCentreRep -- Pointer to a cost centre
46 | CharRep -- Machine characters
47 | IntRep -- signed integers (same size as ptr on this arch)
48 | WordRep -- unsigned integers (same size as ptr on this arch)
49 | AddrRep -- addresses (C pointers)
51 | DoubleRep -- doubles
53 | Int8Rep -- 8 bit signed integers
54 | Int16Rep -- 16 bit signed integers
55 | Int32Rep -- 32 bit signed integers
56 | Int64Rep -- 64 bit signed integers
57 | Word8Rep -- 8 bit unsigned integers
58 | Word16Rep -- 16 bit unsigned integers
59 | Word32Rep -- 32 bit unsigned integers
60 | Word64Rep -- 64 bit unsigned integers
66 | StablePtrRep -- guaranteed to be represented by a pointer
68 | StableNameRep -- A stable name is a real heap object, unpointed,
69 -- with one field containing an index into the
70 -- stable pointer table. It has to be a heap
71 -- object so the garbage collector can track these
72 -- objects and reclaim stable pointer entries.
74 | ThreadIdRep -- Really a pointer to a TSO
76 | ArrayRep -- Primitive array of Haskell pointers
77 | ByteArrayRep -- Primitive array of bytes (no Haskell pointers)
79 | PrimPtrRep -- Used for MutVars and MVars;
80 -- a pointer to a primitive object
81 -- ToDo: subsumes WeakPtrRep, ThreadIdRep,
82 -- StableNameRep, ForeignObjRep, and BCORep ?
84 | VoidRep -- Occupies no space at all!
85 -- (Primitive states are mapped onto this)
87 -- Kinds are used in PrimTyCons, which need both Eq and Ord
90 These pretty much correspond to the C types declared in StgTypes.h,
91 with the following exceptions:
93 - when an Array or ByteArray is passed to C, we again pass a pointer
94 to the contents. The actual type that is passed is StgPtr for
95 ArrayRep, and StgByteArray (probably a char *) for ByteArrayRep.
97 These hacks are left until the final printing of the C, in
100 %************************************************************************
102 \subsection[PrimRep-predicates]{Follow-ness, sizes, and such---on @PrimitiveKinds@}
104 %************************************************************************
106 Whether or not the thing is a pointer that the garbage-collector
107 should follow. Or, to put it another (less confusing) way, whether
108 the object in question is a heap object.
110 Depending on the outcome, this predicate determines what stack
111 the pointer/object possibly will have to be saved onto, and the
112 computation of GC liveness info.
115 isFollowableRep :: PrimRep -> Bool
117 isFollowableRep PtrRep = True
118 isFollowableRep ArrayRep = True -- all heap objects:
119 isFollowableRep ByteArrayRep = True -- ''
120 isFollowableRep WeakPtrRep = True -- ''
121 isFollowableRep ForeignObjRep = True -- ''
122 isFollowableRep StableNameRep = True -- ''
123 isFollowableRep PrimPtrRep = True -- ''
124 isFollowableRep ThreadIdRep = True -- pointer to a TSO
125 isFollowableRep BCORep = True
127 isFollowableRep other = False
129 separateByPtrFollowness :: (a -> PrimRep) -> [a] -> ([a], [a])
131 separateByPtrFollowness kind_fun things
132 = sep_things kind_fun things [] []
133 -- accumulating params for follow-able and don't-follow things...
135 sep_things kfun [] bs us = (reverse bs, reverse us)
136 sep_things kfun (t:ts) bs us
137 = if (isFollowableRep . kfun) t then
138 sep_things kfun ts (t:bs) us
140 sep_things kfun ts bs (t:us)
143 @isFloatingRep@ is used to distinguish @Double@ and @Float@ which
144 cause inadvertent numeric conversions if you aren't jolly careful.
145 See codeGen/CgCon:cgTopRhsCon.
148 isFloatingRep :: PrimRep -> Bool
149 isFloatingRep DoubleRep = True
150 isFloatingRep FloatRep = True
151 isFloatingRep _ = False
155 is64BitRep :: PrimRep -> Bool
156 is64BitRep Int64Rep = True
157 is64BitRep Word64Rep = True
162 getPrimRepSize :: PrimRep -> Int
163 getPrimRepSize DoubleRep = dOUBLE_SIZE -- "words", of course
164 getPrimRepSize Word64Rep = wORD64_SIZE
165 getPrimRepSize Int64Rep = iNT64_SIZE
166 getPrimRepSize VoidRep = 0
169 retPrimRepSize :: Int
170 retPrimRepSize = getPrimRepSize RetRep
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 CharRep = 4
178 getPrimRepSizeInBytes IntRep = wORD_SIZE
179 getPrimRepSizeInBytes WordRep = wORD_SIZE
180 getPrimRepSizeInBytes AddrRep = wORD_SIZE
181 getPrimRepSizeInBytes FloatRep = wORD_SIZE
182 getPrimRepSizeInBytes DoubleRep = dOUBLE_SIZE * wORD_SIZE
183 getPrimRepSizeInBytes Int8Rep = 1
184 getPrimRepSizeInBytes Int16Rep = 2
185 getPrimRepSizeInBytes Int32Rep = 4
186 getPrimRepSizeInBytes Int64Rep = 8
187 getPrimRepSizeInBytes Word8Rep = 1
188 getPrimRepSizeInBytes Word16Rep = 2
189 getPrimRepSizeInBytes Word32Rep = 4
190 getPrimRepSizeInBytes Word64Rep = 8
191 getPrimRepSizeInBytes WeakPtrRep = wORD_SIZE
192 getPrimRepSizeInBytes ForeignObjRep = wORD_SIZE
193 getPrimRepSizeInBytes StablePtrRep = wORD_SIZE
194 getPrimRepSizeInBytes StableNameRep = wORD_SIZE
195 getPrimRepSizeInBytes ArrayRep = wORD_SIZE
196 getPrimRepSizeInBytes ByteArrayRep = wORD_SIZE
197 getPrimRepSizeInBytes other = pprPanic "getPrimRepSizeInBytes" (ppr other)
200 -- Sizes in bytes of things when they are array elements,
201 -- so that we can generate the correct indexing code
202 -- inside the compiler. This is not the same as the above
203 -- getPrimRepSizeInBytes, the rationale behind which is
205 getPrimRepArrayElemSize :: PrimRep -> Int
206 getPrimRepArrayElemSize CharRep = 4
207 getPrimRepArrayElemSize DataPtrRep = wORD_SIZE
208 getPrimRepArrayElemSize PtrRep = wORD_SIZE
209 getPrimRepArrayElemSize IntRep = wORD_SIZE
210 getPrimRepArrayElemSize WordRep = wORD_SIZE
211 getPrimRepArrayElemSize AddrRep = wORD_SIZE
212 getPrimRepArrayElemSize StablePtrRep = wORD_SIZE
213 getPrimRepArrayElemSize ForeignObjRep = wORD_SIZE
214 getPrimRepArrayElemSize Word8Rep = 1
215 getPrimRepArrayElemSize Word16Rep = 2
216 getPrimRepArrayElemSize Word32Rep = 4
217 getPrimRepArrayElemSize Word64Rep = 8
218 getPrimRepArrayElemSize Int8Rep = 1
219 getPrimRepArrayElemSize Int16Rep = 2
220 getPrimRepArrayElemSize Int32Rep = 4
221 getPrimRepArrayElemSize Int64Rep = 8
222 getPrimRepArrayElemSize FloatRep = 4
223 getPrimRepArrayElemSize DoubleRep = 8
224 getPrimRepArrayElemSize other = pprPanic "getPrimRepSizeArrayElemSize" (ppr other)
228 %************************************************************************
230 \subsection[PrimRep-instances]{Boring instance decls for @PrimRep@}
232 %************************************************************************
235 instance Outputable PrimRep where
236 ppr kind = text (showPrimRep kind)
238 showPrimRep :: PrimRep -> String
239 showPrimRep PtrRep = "P_" -- short for StgPtr
240 showPrimRep CodePtrRep = "P_" -- DEATH to StgFunPtr! (94/02/22 WDP)
241 showPrimRep DataPtrRep = "D_"
242 showPrimRep RetRep = "P_"
243 showPrimRep CostCentreRep = "CostCentre"
244 showPrimRep CharRep = "C_"
245 showPrimRep Int8Rep = "StgInt8"
246 showPrimRep Int16Rep = "StgInt16"
247 showPrimRep Int32Rep = "StgInt32"
248 showPrimRep Word8Rep = "StgWord8"
249 showPrimRep Word16Rep = "StgWord16"
250 showPrimRep Word32Rep = "StgWord32"
251 showPrimRep IntRep = "I_" -- short for StgInt
252 showPrimRep WordRep = "W_" -- short for StgWord
253 showPrimRep Int64Rep = "LI_" -- short for StgLongInt
254 showPrimRep Word64Rep = "LW_" -- short for StgLongWord
255 showPrimRep AddrRep = "StgAddr"
256 showPrimRep FloatRep = "StgFloat"
257 showPrimRep DoubleRep = "StgDouble"
258 showPrimRep ArrayRep = "P_" -- see comment below
259 showPrimRep PrimPtrRep = "P_"
260 showPrimRep ByteArrayRep = "StgByteArray"
261 showPrimRep StablePtrRep = "StgStablePtr"
262 showPrimRep StableNameRep = "P_"
263 showPrimRep ThreadIdRep = "StgTSO*"
264 showPrimRep WeakPtrRep = "P_"
265 showPrimRep ForeignObjRep = "StgAddr"
266 showPrimRep VoidRep = "!!VOID_KIND!!"
267 showPrimRep BCORep = "P_" -- not sure -- JRS 000708
270 Foreign Objects and Arrays are treated specially by the code for
271 _ccall_s: we pass a pointer to the contents of the object, not the