2 % (c) The GRASP Project, Glasgow University, 1992-1996
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
11 #include "HsVersions.h"
16 separateByPtrFollowness, isFollowableRep, isFloatingRep,
17 getPrimRepSize, retPrimRepSize,
24 import Pretty -- pretty-printing code
27 #include "../../includes/GhcConstants.h"
30 %************************************************************************
32 \subsection[PrimRep-datatype]{The @PrimRep@ datatype}
34 %************************************************************************
38 = -- These pointer-kinds are all really the same, but we keep
39 -- them separate for documentation purposes.
40 PtrRep -- Pointer to a closure; a ``word''.
41 | CodePtrRep -- Pointer to code
42 | DataPtrRep -- Pointer to data
43 | RetRep -- Pointer to code or data (return vector or code pointer)
44 | CostCentreRep -- Pointer to a cost centre
46 | CharRep -- Machine characters
47 | IntRep -- integers (at least 32 bits)
48 | WordRep -- ditto (but *unsigned*)
49 | AddrRep -- addresses ("C pointers")
51 | DoubleRep -- doubles
53 | MallocPtrRep -- This has to be a special kind because ccall
54 -- generates special code when passing/returning
55 -- one of these. [ADR]
57 | StablePtrRep -- We could replace this with IntRep but maybe
58 -- there's some documentation gain from having
61 | ArrayRep -- Primitive array of Haskell pointers
62 | ByteArrayRep -- Primitive array of bytes (no Haskell pointers)
64 | VoidRep -- Occupies no space at all!
65 -- (Primitive states are mapped onto this)
67 -- Kinds are used in PrimTyCons, which need both Eq and Ord
68 -- Text is needed for derived-Text on PrimitiveOps
71 %************************************************************************
73 \subsection[PrimRep-predicates]{Follow-ness, sizes, and such---on @PrimitiveKinds@}
75 %************************************************************************
77 Whether or not the thing is a pointer that the garbage-collector
80 Or, to put it another (less confusing) way, whether the object in
81 question is a heap object.
84 isFollowableRep :: PrimRep -> Bool
86 isFollowableRep PtrRep = True
87 isFollowableRep ArrayRep = True
88 isFollowableRep ByteArrayRep = True
89 isFollowableRep MallocPtrRep = True
91 isFollowableRep StablePtrRep = False
92 -- StablePtrs aren't followable because they are just indices into a
93 -- table for which explicit allocation/ deallocation is required.
95 isFollowableRep other = False
97 separateByPtrFollowness :: (a -> PrimRep) -> [a] -> ([a], [a])
99 separateByPtrFollowness kind_fun things
100 = sep_things kind_fun things [] []
101 -- accumulating params for follow-able and don't-follow things...
103 sep_things kfun [] bs us = (reverse bs, reverse us)
104 sep_things kfun (t:ts) bs us
105 = if (isFollowableRep . kfun) t then
106 sep_things kfun ts (t:bs) us
108 sep_things kfun ts bs (t:us)
111 @isFloatingRep@ is used to distinguish @Double@ and @Float@ which
112 cause inadvertent numeric conversions if you aren't jolly careful.
113 See codeGen/CgCon:cgTopRhsCon.
116 isFloatingRep :: PrimRep -> Bool
118 isFloatingRep DoubleRep = True
119 isFloatingRep FloatRep = True
120 isFloatingRep other = False
124 getPrimRepSize :: PrimRep -> Int
126 getPrimRepSize DoubleRep = DOUBLE_SIZE -- "words", of course
127 --getPrimRepSize FloatRep = 1
128 --getPrimRepSize CharRep = 1 -- ToDo: count in bytes?
129 --getPrimRepSize ArrayRep = 1 -- Listed specifically for *documentation*
130 --getPrimRepSize ByteArrayRep = 1
131 getPrimRepSize VoidRep = 0
132 getPrimRepSize other = 1
134 retPrimRepSize = getPrimRepSize RetRep
137 %************************************************************************
139 \subsection[PrimRep-instances]{Boring instance decls for @PrimRep@}
141 %************************************************************************
144 instance Outputable PrimRep where
145 ppr sty kind = ppStr (showPrimRep kind)
147 showPrimRep :: PrimRep -> String
148 guessPrimRep :: String -> PrimRep -- a horrible "inverse" function
150 showPrimRep PtrRep = "P_" -- short for StgPtr
152 showPrimRep CodePtrRep = "P_" -- DEATH to StgFunPtr! (94/02/22 WDP)
153 -- but aren't code pointers and function pointers different sizes
154 -- on some machines (eg 80x86)? ADR
155 -- Are you trying to ruin my life, or what? (WDP)
157 showPrimRep DataPtrRep = "D_"
158 showPrimRep RetRep = "StgRetAddr"
159 showPrimRep CostCentreRep = "CostCentre"
160 showPrimRep CharRep = "StgChar"
161 showPrimRep IntRep = "I_" -- short for StgInt
162 showPrimRep WordRep = "W_" -- short for StgWord
163 showPrimRep AddrRep = "StgAddr"
164 showPrimRep FloatRep = "StgFloat"
165 showPrimRep DoubleRep = "StgDouble"
166 showPrimRep ArrayRep = "StgArray" -- see comment below
167 showPrimRep ByteArrayRep = "StgByteArray"
168 showPrimRep StablePtrRep = "StgStablePtr"
169 showPrimRep MallocPtrRep = "StgPtr" -- see comment below
170 showPrimRep VoidRep = "!!VOID_KIND!!"
172 guessPrimRep "D_" = DataPtrRep
173 guessPrimRep "StgRetAddr" = RetRep
174 guessPrimRep "StgChar" = CharRep
175 guessPrimRep "I_" = IntRep
176 guessPrimRep "W_" = WordRep
177 guessPrimRep "StgAddr" = AddrRep
178 guessPrimRep "StgFloat" = FloatRep
179 guessPrimRep "StgDouble" = DoubleRep
180 guessPrimRep "StgArray" = ArrayRep
181 guessPrimRep "StgByteArray" = ByteArrayRep
182 guessPrimRep "StgStablePtr" = StablePtrRep
185 All local C variables of @ArrayRep@ are declared in C as type
186 @StgArray@. The coercion to a more precise C type is done just before
187 indexing (by the relevant C primitive-op macro).
189 Nota Bene. There are three types associated with Malloc Pointers:
192 @StgMallocClosure@ is the type of the thing the C world gives us.
193 (This typename is hardwired into @ppr_casm_results@ in
197 @StgMallocPtr@ is the type of the thing we give the C world.
200 @StgPtr@ is the type of the (pointer to the) heap object which we
201 pass around inside the STG machine.
204 It is really easy to confuse the two. (I'm not sure this choice of
205 type names helps.) [ADR]