94ab0c50f2f731e60ad766d157317d288e980076
[ghc-hetmet.git] / ghc / compiler / prelude / PrimRep.lhs
1 %
2 % (c) The GRASP Project, Glasgow University, 1992-1996
3 %
4 \section[PrimRep]{Primitive machine-level kinds of things.}
5
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
8 types.
9
10 \begin{code}
11 #include "HsVersions.h"
12
13 module PrimRep (
14         PrimRep(..),
15
16         separateByPtrFollowness, isFollowableRep, isFloatingRep,
17         getPrimRepSize, retPrimRepSize,
18         showPrimRep,
19         guessPrimRep
20     ) where
21
22 IMP_Ubiq()
23
24 import Pretty           -- pretty-printing code
25 import Util
26
27 #include "../../includes/GhcConstants.h"
28 \end{code}
29
30 %************************************************************************
31 %*                                                                      *
32 \subsection[PrimRep-datatype]{The @PrimRep@ datatype}
33 %*                                                                      *
34 %************************************************************************
35
36 \begin{code}
37 data PrimRep
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
45
46   | CharRep             -- Machine characters
47   | IntRep              --         integers (at least 32 bits)
48   | WordRep             --         ditto (but *unsigned*)
49   | AddrRep             --         addresses ("C pointers")
50   | FloatRep            --         floats
51   | DoubleRep           --         doubles
52
53   | ForeignObjRep       -- This has to be a special kind because ccall
54                         -- generates special code when passing/returning
55                         -- one of these. [ADR]
56
57   | StablePtrRep        -- We could replace this with IntRep but maybe
58                         -- there's some documentation gain from having
59                         -- it special? [ADR]
60
61   | ArrayRep            -- Primitive array of Haskell pointers
62   | ByteArrayRep        -- Primitive array of bytes (no Haskell pointers)
63
64   | VoidRep             -- Occupies no space at all!
65                         -- (Primitive states are mapped onto this)
66   deriving (Eq, Ord)
67         -- Kinds are used in PrimTyCons, which need both Eq and Ord
68 \end{code}
69
70 %************************************************************************
71 %*                                                                      *
72 \subsection[PrimRep-predicates]{Follow-ness, sizes, and such---on @PrimitiveKinds@}
73 %*                                                                      *
74 %************************************************************************
75
76 Whether or not the thing is a pointer that the garbage-collector
77 should follow.
78
79 Or, to put it another (less confusing) way, whether the object in
80 question is a heap object.
81
82 \begin{code}
83 isFollowableRep :: PrimRep -> Bool
84
85 isFollowableRep PtrRep        = True
86 isFollowableRep ArrayRep      = True
87 isFollowableRep ByteArrayRep  = True
88 -- why is a MallocPtr followable? 4/96 SOF
89 -- isFollowableRep ForeignObjRep  = True
90
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.
94
95 isFollowableRep other           = False
96
97 separateByPtrFollowness :: (a -> PrimRep) -> [a] -> ([a], [a])
98
99 separateByPtrFollowness kind_fun things
100   = sep_things kind_fun things [] []
101     -- accumulating params for follow-able and don't-follow things...
102   where
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
107         else
108             sep_things kfun ts bs (t:us)
109 \end{code}
110
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.
114
115 \begin{code}
116 isFloatingRep :: PrimRep -> Bool
117
118 isFloatingRep DoubleRep = True
119 isFloatingRep FloatRep  = True
120 isFloatingRep other     = False
121 \end{code}
122
123 \begin{code}
124 getPrimRepSize :: PrimRep -> Int
125
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
133
134 retPrimRepSize = getPrimRepSize RetRep
135 \end{code}
136
137 %************************************************************************
138 %*                                                                      *
139 \subsection[PrimRep-instances]{Boring instance decls for @PrimRep@}
140 %*                                                                      *
141 %************************************************************************
142
143 \begin{code}
144 instance Outputable PrimRep where
145     ppr sty kind = ppStr (showPrimRep kind)
146
147 showPrimRep  :: PrimRep -> String
148 guessPrimRep :: String -> PrimRep       -- a horrible "inverse" function
149
150 showPrimRep PtrRep          = "P_"      -- short for StgPtr
151
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)
156
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 ForeignObjRep  = "StgPtr" -- see comment below
170 showPrimRep VoidRep       = "!!VOID_KIND!!"
171
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
183 \end{code}
184
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).
188
189 Nota Bene. There are three types associated with @ForeignObj@ (MallocPtr++): 
190 \begin{itemize}
191 \item
192 @StgForeignObjClosure@ is the type of the thing the prim. op @mkForeignObj@ returns.
193 {- old comment for MallocPtr
194 (This typename is hardwired into @ppr_casm_results@ in
195 @PprAbsC.lhs@.)
196 -}
197
198 \item
199 @StgForeignObj@ is the type of the thing we give the C world.
200
201 \item
202 @StgPtr@ is the type of the (pointer to the) heap object which we
203 pass around inside the STG machine.
204 \end{itemize}
205
206 It is really easy to confuse the two.  (I'm not sure this choice of
207 type names helps.) [ADR]