[project @ 1996-05-17 16:02:43 by partain]
[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 import 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         -- Text is needed for derived-Text on PrimitiveOps
69 \end{code}
70
71 %************************************************************************
72 %*                                                                      *
73 \subsection[PrimRep-predicates]{Follow-ness, sizes, and such---on @PrimitiveKinds@}
74 %*                                                                      *
75 %************************************************************************
76
77 Whether or not the thing is a pointer that the garbage-collector
78 should follow.
79
80 Or, to put it another (less confusing) way, whether the object in
81 question is a heap object.
82
83 \begin{code}
84 isFollowableRep :: PrimRep -> Bool
85
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
91
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.
95
96 isFollowableRep other           = False
97
98 separateByPtrFollowness :: (a -> PrimRep) -> [a] -> ([a], [a])
99
100 separateByPtrFollowness kind_fun things
101   = sep_things kind_fun things [] []
102     -- accumulating params for follow-able and don't-follow things...
103   where
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
108         else
109             sep_things kfun ts bs (t:us)
110 \end{code}
111
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.
115
116 \begin{code}
117 isFloatingRep :: PrimRep -> Bool
118
119 isFloatingRep DoubleRep = True
120 isFloatingRep FloatRep  = True
121 isFloatingRep other     = False
122 \end{code}
123
124 \begin{code}
125 getPrimRepSize :: PrimRep -> Int
126
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
134
135 retPrimRepSize = getPrimRepSize RetRep
136 \end{code}
137
138 %************************************************************************
139 %*                                                                      *
140 \subsection[PrimRep-instances]{Boring instance decls for @PrimRep@}
141 %*                                                                      *
142 %************************************************************************
143
144 \begin{code}
145 instance Outputable PrimRep where
146     ppr sty kind = ppStr (showPrimRep kind)
147
148 showPrimRep  :: PrimRep -> String
149 guessPrimRep :: String -> PrimRep       -- a horrible "inverse" function
150
151 showPrimRep PtrRep          = "P_"      -- short for StgPtr
152
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)
157
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!!"
172
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
184 \end{code}
185
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).
189
190 Nota Bene. There are three types associated with @ForeignObj@ (MallocPtr++): 
191 \begin{itemize}
192 \item
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
196 @PprAbsC.lhs@.)
197 -}
198
199 \item
200 @StgForeignObj@ is the type of the thing we give the C world.
201
202 \item
203 @StgPtr@ is the type of the (pointer to the) heap object which we
204 pass around inside the STG machine.
205 \end{itemize}
206
207 It is really easy to confuse the two.  (I'm not sure this choice of
208 type names helps.) [ADR]