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