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
26 #include "HsVersions.h"
28 import Constants ( dOUBLE_SIZE, iNT64_SIZE, wORD64_SIZE, wORD_SIZE )
32 %************************************************************************
34 \subsection[PrimRep-datatype]{The @PrimRep@ datatype}
36 %************************************************************************
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
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")
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.)
58 -- Perhaps all sized integers and words should be primitive types.
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.
68 | StablePtrRep -- guaranteed to be represented by a pointer
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.
76 | ThreadIdRep -- Really a pointer to a TSO
78 | ArrayRep -- Primitive array of Haskell pointers
79 | ByteArrayRep -- Primitive array of bytes (no Haskell pointers)
81 | VoidRep -- Occupies no space at all!
82 -- (Primitive states are mapped onto this)
84 -- Kinds are used in PrimTyCons, which need both Eq and Ord
87 These pretty much correspond to the C types declared in StgTypes.h,
88 with the following exceptions:
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.
94 These hacks are left until the final printing of the C, in
97 %************************************************************************
99 \subsection[PrimRep-predicates]{Follow-ness, sizes, and such---on @PrimitiveKinds@}
101 %************************************************************************
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.
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.
112 isFollowableRep :: PrimRep -> Bool
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
122 isFollowableRep other = False
124 separateByPtrFollowness :: (a -> PrimRep) -> [a] -> ([a], [a])
126 separateByPtrFollowness kind_fun things
127 = sep_things kind_fun things [] []
128 -- accumulating params for follow-able and don't-follow things...
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
135 sep_things kfun ts bs (t:us)
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.
143 isFloatingRep :: PrimRep -> Bool
145 isFloatingRep DoubleRep = True
146 isFloatingRep FloatRep = True
147 isFloatingRep other = False
152 is64BitRep :: PrimRep -> Bool
154 is64BitRep Int64Rep = True
155 is64BitRep Word64Rep = True
156 is64BitRep other = False
163 getPrimRepSize :: PrimRep -> Int
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
175 retPrimRepSize = getPrimRepSize RetRep
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 =
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!"
202 %************************************************************************
204 \subsection[PrimRep-instances]{Boring instance decls for @PrimRep@}
206 %************************************************************************
209 instance Outputable PrimRep where
210 ppr kind = text (showPrimRep kind)
212 showPrimRep :: PrimRep -> String
213 showPrimRepToUser :: PrimRep -> String
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!!"
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)
253 showPrimRepToUser pr = primRepString pr
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