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 -- We could replace this with IntRep but maybe
62 -- there's some documentation gain from having
65 | StableNameRep -- A stable name is a real heap object, unpointed,
66 -- with one field containing an index into the
67 -- stable pointer table. It has to be a heap
68 -- object so the garbage collector can track these
69 -- objects and reclaim stable pointer entries.
71 | ThreadIdRep -- Really a pointer to a TSO
73 | ArrayRep -- Primitive array of Haskell pointers
74 | ByteArrayRep -- Primitive array of bytes (no Haskell pointers)
76 | VoidRep -- Occupies no space at all!
77 -- (Primitive states are mapped onto this)
79 -- Kinds are used in PrimTyCons, which need both Eq and Ord
82 These pretty much correspond to the C types declared in StgTypes.h,
83 with the following exceptions:
85 - when an Array or ByteArray is passed to C, we again pass a pointer
86 to the contents. The actual type that is passed is StgPtr for
87 ArrayRep, and StgByteArray (probably a char *) for ByteArrayRep.
89 These hacks are left until the final printing of the C, in
92 %************************************************************************
94 \subsection[PrimRep-predicates]{Follow-ness, sizes, and such---on @PrimitiveKinds@}
96 %************************************************************************
98 Whether or not the thing is a pointer that the garbage-collector
99 should follow. Or, to put it another (less confusing) way, whether
100 the object in question is a heap object.
102 Depending on the outcome, this predicate determines what stack
103 the pointer/object possibly will have to be saved onto, and the
104 computation of GC liveness info.
107 isFollowableRep :: PrimRep -> Bool
109 isFollowableRep PtrRep = True
110 isFollowableRep ArrayRep = True -- all heap objects:
111 isFollowableRep ByteArrayRep = True -- ''
112 isFollowableRep WeakPtrRep = True -- ''
113 isFollowableRep ForeignObjRep = True -- ''
114 isFollowableRep StableNameRep = True -- ''
115 isFollowableRep ThreadIdRep = True -- pointer to a TSO
117 isFollowableRep other = False
119 separateByPtrFollowness :: (a -> PrimRep) -> [a] -> ([a], [a])
121 separateByPtrFollowness kind_fun things
122 = sep_things kind_fun things [] []
123 -- accumulating params for follow-able and don't-follow things...
125 sep_things kfun [] bs us = (reverse bs, reverse us)
126 sep_things kfun (t:ts) bs us
127 = if (isFollowableRep . kfun) t then
128 sep_things kfun ts (t:bs) us
130 sep_things kfun ts bs (t:us)
133 @isFloatingRep@ is used to distinguish @Double@ and @Float@ which
134 cause inadvertent numeric conversions if you aren't jolly careful.
135 See codeGen/CgCon:cgTopRhsCon.
138 isFloatingRep :: PrimRep -> Bool
140 isFloatingRep DoubleRep = True
141 isFloatingRep FloatRep = True
142 isFloatingRep other = False
147 is64BitRep :: PrimRep -> Bool
149 is64BitRep Int64Rep = True
150 is64BitRep Word64Rep = True
151 is64BitRep other = False
158 getPrimRepSize :: PrimRep -> Int
160 getPrimRepSize DoubleRep = dOUBLE_SIZE -- "words", of course
161 getPrimRepSize Word64Rep = wORD64_SIZE
162 getPrimRepSize Int64Rep = iNT64_SIZE
163 --getPrimRepSize FloatRep = 1
164 --getPrimRepSize CharRep = 1 -- ToDo: count in bytes?
165 --getPrimRepSize ArrayRep = 1 -- Listed specifically for *documentation*
166 --getPrimRepSize ByteArrayRep = 1
167 getPrimRepSize VoidRep = 0
168 getPrimRepSize other = 1
170 retPrimRepSize = getPrimRepSize RetRep
172 -- size in bytes, ToDo: cpp in the right vals.
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 pr =
192 _ -> panic "getPrimRepSize: ouch - this wasn't supposed to happen!"
196 %************************************************************************
198 \subsection[PrimRep-instances]{Boring instance decls for @PrimRep@}
200 %************************************************************************
203 instance Outputable PrimRep where
204 ppr kind = text (showPrimRep kind)
206 showPrimRep :: PrimRep -> String
207 showPrimRepToUser :: PrimRep -> String
209 showPrimRep PtrRep = "P_" -- short for StgPtr
210 showPrimRep CodePtrRep = "P_" -- DEATH to StgFunPtr! (94/02/22 WDP)
211 showPrimRep DataPtrRep = "D_"
212 showPrimRep RetRep = "P_"
213 showPrimRep CostCentreRep = "CostCentre"
214 showPrimRep CharRep = "C_"
215 showPrimRep IntRep = "I_" -- short for StgInt
216 showPrimRep WordRep = "W_" -- short for StgWord
217 showPrimRep Int64Rep = "LI_" -- short for StgLongInt
218 showPrimRep Word64Rep = "LW_" -- short for StgLongWord
219 showPrimRep AddrRep = "StgAddr"
220 showPrimRep FloatRep = "StgFloat"
221 showPrimRep DoubleRep = "StgDouble"
222 showPrimRep ArrayRep = "P_" -- see comment below
223 showPrimRep ByteArrayRep = "StgByteArray"
224 showPrimRep StablePtrRep = "StgStablePtr"
225 showPrimRep StableNameRep = "P_"
226 showPrimRep ThreadIdRep = "StgTSO*"
227 showPrimRep WeakPtrRep = "P_"
228 showPrimRep ForeignObjRep = "StgAddr"
229 showPrimRep VoidRep = "!!VOID_KIND!!"
231 primRepString CharRep = "Char"
232 primRepString IntRep = "Int"
233 primRepString WordRep = "Word"
234 primRepString Int64Rep = "Int64"
235 primRepString Word64Rep = "Word64"
236 primRepString AddrRep = "Addr"
237 primRepString FloatRep = "Float"
238 primRepString DoubleRep = "Double"
239 primRepString WeakPtrRep = "Weak"
240 primRepString ForeignObjRep = "ForeignObj"
241 primRepString StablePtrRep = "StablePtr"
242 primRepString StableNameRep = "StableName"
243 primRepString other = pprPanic "primRepString" (ppr other)
245 showPrimRepToUser pr = primRepString pr
248 Foreign Objects and Arrays are treated specially by the code for
249 _ccall_s: we pass a pointer to the contents of the object, not the