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 | PrimPtrRep -- Used for MutVars and MVars;
82 -- a pointer to a primitive object
83 -- ToDo: subsumes WeakPtrRep, ThreadIdRep,
84 -- StableNameRep, ForeignObjRep, and BCORep ?
86 | VoidRep -- Occupies no space at all!
87 -- (Primitive states are mapped onto this)
89 -- Kinds are used in PrimTyCons, which need both Eq and Ord
92 These pretty much correspond to the C types declared in StgTypes.h,
93 with the following exceptions:
95 - when an Array or ByteArray is passed to C, we again pass a pointer
96 to the contents. The actual type that is passed is StgPtr for
97 ArrayRep, and StgByteArray (probably a char *) for ByteArrayRep.
99 These hacks are left until the final printing of the C, in
102 %************************************************************************
104 \subsection[PrimRep-predicates]{Follow-ness, sizes, and such---on @PrimitiveKinds@}
106 %************************************************************************
108 Whether or not the thing is a pointer that the garbage-collector
109 should follow. Or, to put it another (less confusing) way, whether
110 the object in question is a heap object.
112 Depending on the outcome, this predicate determines what stack
113 the pointer/object possibly will have to be saved onto, and the
114 computation of GC liveness info.
117 isFollowableRep :: PrimRep -> Bool
119 isFollowableRep PtrRep = True
120 isFollowableRep ArrayRep = True -- all heap objects:
121 isFollowableRep ByteArrayRep = True -- ''
122 isFollowableRep WeakPtrRep = True -- ''
123 isFollowableRep ForeignObjRep = True -- ''
124 isFollowableRep StableNameRep = True -- ''
125 isFollowableRep PrimPtrRep = True -- ''
126 isFollowableRep ThreadIdRep = True -- pointer to a TSO
128 isFollowableRep other = False
130 separateByPtrFollowness :: (a -> PrimRep) -> [a] -> ([a], [a])
132 separateByPtrFollowness kind_fun things
133 = sep_things kind_fun things [] []
134 -- accumulating params for follow-able and don't-follow things...
136 sep_things kfun [] bs us = (reverse bs, reverse us)
137 sep_things kfun (t:ts) bs us
138 = if (isFollowableRep . kfun) t then
139 sep_things kfun ts (t:bs) us
141 sep_things kfun ts bs (t:us)
144 @isFloatingRep@ is used to distinguish @Double@ and @Float@ which
145 cause inadvertent numeric conversions if you aren't jolly careful.
146 See codeGen/CgCon:cgTopRhsCon.
149 isFloatingRep :: PrimRep -> Bool
151 isFloatingRep DoubleRep = True
152 isFloatingRep FloatRep = True
153 isFloatingRep other = False
158 is64BitRep :: PrimRep -> Bool
160 is64BitRep Int64Rep = True
161 is64BitRep Word64Rep = True
162 is64BitRep other = False
169 getPrimRepSize :: PrimRep -> Int
171 getPrimRepSize DoubleRep = dOUBLE_SIZE -- "words", of course
172 getPrimRepSize Word64Rep = wORD64_SIZE
173 getPrimRepSize Int64Rep = iNT64_SIZE
174 getPrimRepSize VoidRep = 0
175 getPrimRepSize other = 1
177 retPrimRepSize = getPrimRepSize RetRep
180 -- (used in some settings to figure out how many bytes
181 -- we have to push onto the stack when calling external
182 -- entry points (e.g., stdcalling on win32)
183 getPrimRepSizeInBytes :: PrimRep -> Int
184 getPrimRepSizeInBytes pr =
190 FloatRep -> wORD_SIZE
191 DoubleRep -> dOUBLE_SIZE * wORD_SIZE
192 Word64Rep -> wORD64_SIZE * wORD_SIZE
193 Int64Rep -> iNT64_SIZE * wORD_SIZE
194 WeakPtrRep -> wORD_SIZE
195 ForeignObjRep -> wORD_SIZE
196 StablePtrRep -> wORD_SIZE
197 StableNameRep -> wORD_SIZE
198 ArrayRep -> wORD_SIZE
199 ByteArrayRep -> wORD_SIZE
200 _ -> panic "getPrimRepSize: ouch - this wasn't supposed to happen!"
204 %************************************************************************
206 \subsection[PrimRep-instances]{Boring instance decls for @PrimRep@}
208 %************************************************************************
211 instance Outputable PrimRep where
212 ppr kind = text (showPrimRep kind)
214 showPrimRep :: PrimRep -> String
215 showPrimRepToUser :: PrimRep -> String
217 showPrimRep PtrRep = "P_" -- short for StgPtr
218 showPrimRep CodePtrRep = "P_" -- DEATH to StgFunPtr! (94/02/22 WDP)
219 showPrimRep DataPtrRep = "D_"
220 showPrimRep RetRep = "P_"
221 showPrimRep CostCentreRep = "CostCentre"
222 showPrimRep CharRep = "C_"
223 showPrimRep Int8Rep = "StgInt8"
224 showPrimRep IntRep = "I_" -- short for StgInt
225 showPrimRep WordRep = "W_" -- short for StgWord
226 showPrimRep Int64Rep = "LI_" -- short for StgLongInt
227 showPrimRep Word64Rep = "LW_" -- short for StgLongWord
228 showPrimRep AddrRep = "StgAddr"
229 showPrimRep FloatRep = "StgFloat"
230 showPrimRep DoubleRep = "StgDouble"
231 showPrimRep ArrayRep = "P_" -- see comment below
232 showPrimRep PrimPtrRep = "P_"
233 showPrimRep ByteArrayRep = "StgByteArray"
234 showPrimRep StablePtrRep = "StgStablePtr"
235 showPrimRep StableNameRep = "P_"
236 showPrimRep ThreadIdRep = "StgTSO*"
237 showPrimRep WeakPtrRep = "P_"
238 showPrimRep ForeignObjRep = "StgAddr"
239 showPrimRep VoidRep = "!!VOID_KIND!!"
240 showPrimRep BCORep = "P_" -- not sure -- JRS 000708
242 primRepString CharRep = "Char"
243 primRepString Int8Rep = "Char" -- To have names like newCharArray#
244 primRepString IntRep = "Int"
245 primRepString WordRep = "Word"
246 primRepString Int64Rep = "Int64"
247 primRepString Word64Rep = "Word64"
248 primRepString AddrRep = "Addr"
249 primRepString FloatRep = "Float"
250 primRepString DoubleRep = "Double"
251 primRepString WeakPtrRep = "Weak"
252 primRepString ForeignObjRep = "ForeignObj"
253 primRepString StablePtrRep = "StablePtr"
254 primRepString StableNameRep = "StableName"
255 primRepString other = pprPanic "primRepString" (ppr other)
257 showPrimRepToUser pr = primRepString pr
260 Foreign Objects and Arrays are treated specially by the code for
261 _ccall_s: we pass a pointer to the contents of the object, not the