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