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 | ForeignObjRep -- 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 -- why is a MallocPtr followable? 4/96 SOF
90 -- isFollowableRep ForeignObjRep = True
92 isFollowableRep StablePtrRep = False
93 -- StablePtrs aren't followable because they are just indices into a
94 -- table for which explicit allocation/ deallocation is required.
96 isFollowableRep other = False
98 separateByPtrFollowness :: (a -> PrimRep) -> [a] -> ([a], [a])
100 separateByPtrFollowness kind_fun things
101 = sep_things kind_fun things [] []
102 -- accumulating params for follow-able and don't-follow things...
104 sep_things kfun [] bs us = (reverse bs, reverse us)
105 sep_things kfun (t:ts) bs us
106 = if (isFollowableRep . kfun) t then
107 sep_things kfun ts (t:bs) us
109 sep_things kfun ts bs (t:us)
112 @isFloatingRep@ is used to distinguish @Double@ and @Float@ which
113 cause inadvertent numeric conversions if you aren't jolly careful.
114 See codeGen/CgCon:cgTopRhsCon.
117 isFloatingRep :: PrimRep -> Bool
119 isFloatingRep DoubleRep = True
120 isFloatingRep FloatRep = True
121 isFloatingRep other = False
125 getPrimRepSize :: PrimRep -> Int
127 getPrimRepSize DoubleRep = DOUBLE_SIZE -- "words", of course
128 --getPrimRepSize FloatRep = 1
129 --getPrimRepSize CharRep = 1 -- ToDo: count in bytes?
130 --getPrimRepSize ArrayRep = 1 -- Listed specifically for *documentation*
131 --getPrimRepSize ByteArrayRep = 1
132 getPrimRepSize VoidRep = 0
133 getPrimRepSize other = 1
135 retPrimRepSize = getPrimRepSize RetRep
138 %************************************************************************
140 \subsection[PrimRep-instances]{Boring instance decls for @PrimRep@}
142 %************************************************************************
145 instance Outputable PrimRep where
146 ppr sty kind = ppStr (showPrimRep kind)
148 showPrimRep :: PrimRep -> String
149 guessPrimRep :: String -> PrimRep -- a horrible "inverse" function
151 showPrimRep PtrRep = "P_" -- short for StgPtr
153 showPrimRep CodePtrRep = "P_" -- DEATH to StgFunPtr! (94/02/22 WDP)
154 -- but aren't code pointers and function pointers different sizes
155 -- on some machines (eg 80x86)? ADR
156 -- Are you trying to ruin my life, or what? (WDP)
158 showPrimRep DataPtrRep = "D_"
159 showPrimRep RetRep = "StgRetAddr"
160 showPrimRep CostCentreRep = "CostCentre"
161 showPrimRep CharRep = "StgChar"
162 showPrimRep IntRep = "I_" -- short for StgInt
163 showPrimRep WordRep = "W_" -- short for StgWord
164 showPrimRep AddrRep = "StgAddr"
165 showPrimRep FloatRep = "StgFloat"
166 showPrimRep DoubleRep = "StgDouble"
167 showPrimRep ArrayRep = "StgArray" -- see comment below
168 showPrimRep ByteArrayRep = "StgByteArray"
169 showPrimRep StablePtrRep = "StgStablePtr"
170 showPrimRep ForeignObjRep = "StgPtr" -- see comment below
171 showPrimRep VoidRep = "!!VOID_KIND!!"
173 guessPrimRep "D_" = DataPtrRep
174 guessPrimRep "StgRetAddr" = RetRep
175 guessPrimRep "StgChar" = CharRep
176 guessPrimRep "I_" = IntRep
177 guessPrimRep "W_" = WordRep
178 guessPrimRep "StgAddr" = AddrRep
179 guessPrimRep "StgFloat" = FloatRep
180 guessPrimRep "StgDouble" = DoubleRep
181 guessPrimRep "StgArray" = ArrayRep
182 guessPrimRep "StgByteArray" = ByteArrayRep
183 guessPrimRep "StgStablePtr" = StablePtrRep
186 All local C variables of @ArrayRep@ are declared in C as type
187 @StgArray@. The coercion to a more precise C type is done just before
188 indexing (by the relevant C primitive-op macro).
190 Nota Bene. There are three types associated with @ForeignObj@ (MallocPtr++):
193 @StgForeignObjClosure@ is the type of the thing the prim. op @mkForeignObj@ returns.
194 {- old comment for MallocPtr
195 (This typename is hardwired into @ppr_casm_results@ in
200 @StgForeignObj@ is the type of the thing we give the C world.
203 @StgPtr@ is the type of the (pointer to the) heap object which we
204 pass around inside the STG machine.
207 It is really easy to confuse the two. (I'm not sure this choice of
208 type names helps.) [ADR]