2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1996
4 %************************************************************************
6 \section[HsCore]{Core-syntax unfoldings in Haskell interface files}
8 %************************************************************************
10 We could either use this, or parameterise @GenCoreExpr@ on @Types@ and
11 @TyVars@ as well. Currently trying the former.
14 #include "HsVersions.h"
18 UnfoldingCoreExpr(..), UnfoldingCoreAlts(..),
19 UnfoldingCoreDefault(..), UnfoldingCoreBinding(..),
20 UnfoldingCoreAtom(..), UfId(..), UnfoldingType(..),
21 UnfoldingPrimOp(..), UfCostCentre(..),
30 import HsTypes ( cmpPolyType, MonoType(..), PolyType(..) )
31 import PrimOp ( PrimOp, tagOf_PrimOp )
34 import Literal ( Literal )
35 import Outputable ( Outputable(..) {-instances-} )
37 import ProtoName ( cmpProtoName, eqProtoName, ProtoName )
41 %************************************************************************
43 \subsection[HsCore-types]{Types for read/written Core unfoldings}
45 %************************************************************************
48 data UnfoldingCoreExpr name
51 | UfCon name -- must be a "BoringUfId"...
53 [UnfoldingCoreAtom name]
54 | UfPrim (UnfoldingPrimOp name)
56 [UnfoldingCoreAtom name]
57 | UfLam (UfBinder name)
58 (UnfoldingCoreExpr name)
59 | UfApp (UnfoldingCoreExpr name)
60 (UnfoldingCoreAtom name)
61 | UfCase (UnfoldingCoreExpr name)
62 (UnfoldingCoreAlts name)
63 | UfLet (UnfoldingCoreBinding name)
64 (UnfoldingCoreExpr name)
65 | UfSCC (UfCostCentre name)
66 (UnfoldingCoreExpr name)
68 data UnfoldingPrimOp name
69 = UfCCallOp FAST_STRING -- callee
70 Bool -- True <=> casm, rather than ccall
71 Bool -- True <=> might cause GC
72 [UnfoldingType name] -- arg types, incl state token
73 -- (which will be first)
74 (UnfoldingType name) -- return type
77 data UnfoldingCoreAlts name
78 = UfCoAlgAlts [(name, [UfBinder name], UnfoldingCoreExpr name)]
79 (UnfoldingCoreDefault name)
80 | UfCoPrimAlts [(Literal, UnfoldingCoreExpr name)]
81 (UnfoldingCoreDefault name)
83 data UnfoldingCoreDefault name
85 | UfCoBindDefault (UfBinder name)
86 (UnfoldingCoreExpr name)
88 data UnfoldingCoreBinding name
89 = UfCoNonRec (UfBinder name)
90 (UnfoldingCoreExpr name)
91 | UfCoRec [(UfBinder name, UnfoldingCoreExpr name)]
93 data UnfoldingCoreAtom name
94 = UfCoVarAtom (UfId name)
97 data UfCostCentre name
99 Bool -- True <=> is dupd
100 | UfAllDictsCC FAST_STRING -- module and group
102 Bool -- True <=> is dupd
103 | UfUserCC FAST_STRING
104 FAST_STRING FAST_STRING -- module and group
105 Bool -- True <=> is dupd
106 Bool -- True <=> is CAF
107 | UfAutoCC (UfId name)
108 FAST_STRING FAST_STRING -- module and group
109 Bool Bool -- as above
110 | UfDictCC (UfId name)
111 FAST_STRING FAST_STRING -- module and group
112 Bool Bool -- as above
114 type UfBinder name = (name, UnfoldingType name)
118 | SuperDictSelUfId name name -- class and superclass
119 | ClassOpUfId name name -- class and class op
120 | DictFunUfId name -- class and type
122 | ConstMethodUfId name name -- class, class op, and type
124 | DefaultMethodUfId name name -- class and class op
125 | SpecUfId (UfId name) -- its unspecialised "parent"
126 [Maybe (MonoType name)]
127 | WorkerUfId (UfId name) -- its non-working "parent"
130 type UnfoldingType name = PolyType name
133 %************************************************************************
135 \subsection[HsCore-print]{Printing Core unfoldings}
137 %************************************************************************
140 instance Outputable name => Outputable (UnfoldingCoreExpr name) where
141 ppr sty (UfVar v) = pprUfId sty v
142 ppr sty (UfLit l) = ppr sty l
144 ppr sty (UfCon c tys as)
145 = ppCat [ppStr "(UfCon", ppr sty c, ppr sty tys, ppr sty as, ppStr ")"]
146 ppr sty (UfPrim o tys as)
147 = ppCat [ppStr "(UfPrim", ppr sty o, ppr sty tys, ppr sty as, ppStr ")"]
149 ppr sty (UfLam bs body)
150 = ppCat [ppChar '\\', ppr sty bs, ppStr "->", ppr sty body]
152 ppr sty (UfApp fun arg)
153 = ppCat [ppStr "(UfApp", ppr sty fun, ppr sty arg, ppStr ")"]
155 ppr sty (UfCase scrut alts)
156 = ppCat [ppStr "case", ppr sty scrut, ppStr "of {", pp_alts alts, ppStr "}"]
158 pp_alts (UfCoAlgAlts alts deflt)
159 = ppCat [ppInterleave ppSemi (map pp_alt alts), pp_deflt deflt]
161 pp_alt (c,bs,rhs) = ppCat [ppr sty c, ppr sty bs, ppStr "->", ppr sty rhs]
162 pp_alts (UfCoPrimAlts alts deflt)
163 = ppCat [ppInterleave ppSemi (map pp_alt alts), pp_deflt deflt]
165 pp_alt (l,rhs) = ppCat [ppr sty l, ppStr "->", ppr sty rhs]
167 pp_deflt UfCoNoDefault = ppNil
168 pp_deflt (UfCoBindDefault b rhs) = ppCat [ppr sty b, ppStr "->", ppr sty rhs]
170 ppr sty (UfLet (UfCoNonRec b rhs) body)
171 = ppCat [ppStr "let", ppr sty b, ppEquals, ppr sty rhs, ppStr "in", ppr sty body]
172 ppr sty (UfLet (UfCoRec pairs) body)
173 = ppCat [ppStr "letrec {", ppInterleave ppSemi (map pp_pair pairs), ppStr "} in", ppr sty body]
175 pp_pair (b,rhs) = ppCat [ppr sty b, ppEquals, ppr sty rhs]
177 ppr sty (UfSCC uf_cc body)
178 = ppCat [ppStr "_scc_ <cost-centre[ToDo]>", ppr sty body]
180 instance Outputable name => Outputable (UnfoldingPrimOp name) where
181 ppr sty (UfCCallOp str is_casm can_gc arg_tys result_ty)
183 before = ppStr (if is_casm then "_casm_ ``" else "_ccall_ ")
184 after = if is_casm then ppStr "'' " else ppSP
186 ppBesides [before, ppPStr str, after,
187 ppLbrack, ppr sty arg_tys, ppRbrack, ppSP, ppr sty result_ty]
188 ppr sty (UfOtherOp op)
191 instance Outputable name => Outputable (UnfoldingCoreAtom name) where
192 ppr sty (UfCoVarAtom v) = pprUfId sty v
193 ppr sty (UfCoLitAtom l) = ppr sty l
195 pprUfId sty (BoringUfId v) = ppr sty v
196 pprUfId sty (SuperDictSelUfId c sc)
197 = ppBesides [ppStr "({-superdict-}", ppr sty c, ppSP, ppr sty sc, ppStr ")"]
198 pprUfId sty (ClassOpUfId c op)
199 = ppBesides [ppStr "({-method-}", ppr sty c, ppSP, ppr sty op, ppStr ")"]
200 pprUfId sty (DictFunUfId c ty)
201 = ppBesides [ppStr "({-dfun-}", ppr sty c, ppSP, ppr sty ty, ppStr ")"]
202 pprUfId sty (ConstMethodUfId c op ty)
203 = ppBesides [ppStr "({-constm-}", ppr sty c, ppSP, ppr sty op, ppSP, ppr sty ty, ppStr ")"]
204 pprUfId sty (DefaultMethodUfId c ty)
205 = ppBesides [ppStr "({-defm-}", ppr sty c, ppSP, ppr sty ty, ppStr ")"]
207 pprUfId sty (SpecUfId unspec ty_maybes)
208 = ppBesides [ppStr "({-spec-} ", pprUfId sty unspec,
209 ppInterleave ppSP (map pp_ty_maybe ty_maybes), ppStr ")"]
211 pp_ty_maybe Nothing = ppStr "_N_"
212 pp_ty_maybe (Just t) = ppr sty t
214 pprUfId sty (WorkerUfId unwrkr)
215 = ppBesides [ppStr "({-wrkr-}", pprUfId sty unwrkr, ppStr ")"]
218 %************************************************************************
220 \subsection[HsCore-equality]{Comparing Core unfoldings}
222 %************************************************************************
224 We want to check that they are {\em exactly} the same.
227 --eqUfExpr :: ProtoNameCoreExpr -> ProtoNameCoreExpr -> Bool
229 eqUfExpr (UfVar v1) (UfVar v2) = eqUfId v1 v2
230 eqUfExpr (UfLit l1) (UfLit l2) = l1 == l2
232 eqUfExpr (UfCon c1 tys1 as1) (UfCon c2 tys2 as2)
233 = eq_name c1 c2 && eq_lists eq_type tys1 tys2 && eq_lists eq_atom as1 as2
234 eqUfExpr (UfPrim o1 tys1 as1) (UfPrim o2 tys2 as2)
235 = eq_op o1 o2 && eq_lists eq_type tys1 tys2 && eq_lists eq_atom as1 as2
237 eq_op (UfCCallOp _ _ _ _ _) (UfCCallOp _ _ _ _ _) = True
238 eq_op (UfOtherOp o1) (UfOtherOp o2)
239 = tagOf_PrimOp o1 _EQ_ tagOf_PrimOp o2
241 eqUfExpr (UfLam bs1 body1) (UfLam bs2 body2)
242 = eq_binder bs1 bs2 && eqUfExpr body1 body2
244 eqUfExpr (UfApp fun1 arg1) (UfApp fun2 arg2)
245 = eqUfExpr fun1 fun2 && eq_atom arg1 arg2
247 eqUfExpr (UfCase scrut1 alts1) (UfCase scrut2 alts2)
248 = eqUfExpr scrut1 scrut2 && eq_alts alts1 alts2
250 eq_alts (UfCoAlgAlts alts1 deflt1) (UfCoAlgAlts alts2 deflt2)
251 = eq_lists eq_alt alts1 alts2 && eq_deflt deflt1 deflt2
253 eq_alt (c1,bs1,rhs1) (c2,bs2,rhs2)
254 = eq_name c1 c2 && eq_lists eq_binder bs1 bs2 && eqUfExpr rhs1 rhs2
256 eq_alts (UfCoPrimAlts alts1 deflt1) (UfCoPrimAlts alts2 deflt2)
257 = eq_lists eq_alt alts1 alts2 && eq_deflt deflt1 deflt2
259 eq_alt (l1,rhs1) (l2,rhs2)
260 = l1 == l2 && eqUfExpr rhs1 rhs2
262 eq_alts _ _ = False -- catch-all
264 eq_deflt UfCoNoDefault UfCoNoDefault = True
265 eq_deflt (UfCoBindDefault b1 rhs1) (UfCoBindDefault b2 rhs2)
266 = eq_binder b1 b2 && eqUfExpr rhs1 rhs2
269 eqUfExpr (UfLet (UfCoNonRec b1 rhs1) body1) (UfLet (UfCoNonRec b2 rhs2) body2)
270 = eq_binder b1 b2 && eqUfExpr rhs1 rhs2 && eqUfExpr body1 body2
272 eqUfExpr (UfLet (UfCoRec pairs1) body1) (UfLet (UfCoRec pairs2) body2)
273 = eq_lists eq_pair pairs1 pairs2 && eqUfExpr body1 body2
275 eq_pair (b1,rhs1) (b2,rhs2) = eq_binder b1 b2 && eqUfExpr rhs1 rhs2
277 eqUfExpr (UfSCC cc1 body1) (UfSCC cc2 body2)
278 = {-trace "eqUfExpr: not comparing cost-centres!"-} (eqUfExpr body1 body2)
280 eqUfExpr _ _ = False -- Catch-all
284 eqUfId (BoringUfId n1) (BoringUfId n2)
286 eqUfId (SuperDictSelUfId a1 b1) (SuperDictSelUfId a2 b2)
287 = eq_name a1 a2 && eq_name b1 b2
288 eqUfId (ClassOpUfId a1 b1) (ClassOpUfId a2 b2)
289 = eq_name a1 a2 && eq_name b1 b2
290 eqUfId (DictFunUfId c1 t1) (DictFunUfId c2 t2)
291 = eq_name c1 c2 && eq_tycon t1 t2 -- NB: **** only compare TyCons ******
293 eq_tycon = panic "HsCore:eqUfId:eq_tycon:ToDo"
295 eq_tycon (UnoverloadedTy ty1) (UnoverloadedTy ty2)
296 = case (cmpInstanceTypes ty1 ty2) of { EQ_ -> True; _ -> False }
298 = trace "eq_tycon" (eq_type ty1 ty2) -- desperately try something else
301 eqUfId (ConstMethodUfId a1 b1 t1) (ConstMethodUfId a2 b2 t2)
302 = eq_name a1 a2 && eq_name b1 b2 && eq_type t1 t2
303 eqUfId (DefaultMethodUfId a1 b1) (DefaultMethodUfId a2 b2)
304 = eq_name a1 a2 && eq_name b1 b2
305 eqUfId (SpecUfId id1 tms1) (SpecUfId id2 tms2)
306 = eqUfId id1 id2 && eq_lists eq_ty_maybe tms1 tms2
308 eq_ty_maybe = panic "HsCore:eqUfId:eq_ty_maybe:ToDo"
310 eq_ty_maybe Nothing Nothing = True
311 eq_ty_maybe (Just ty1) (Just ty2)
312 = eq_type (UnoverloadedTy ty1) (UnoverloadedTy ty2)
313 -- a HACKy way to compare MonoTypes (ToDo) [WDP 94/05/02]
314 eq_ty_maybe _ _ = False
316 eqUfId (WorkerUfId id1) (WorkerUfId id2)
318 eqUfId _ _ = False -- catch-all
322 eq_atom (UfCoVarAtom id1) (UfCoVarAtom id2) = eqUfId id1 id2
323 eq_atom (UfCoLitAtom l1) (UfCoLitAtom l2) = l1 == l2
326 eq_binder (n1, ty1) (n2, ty2) = eq_name n1 n2 && eq_type ty1 ty2
328 eq_name :: ProtoName -> ProtoName -> Bool
329 eq_name pn1 pn2 = eqProtoName pn1 pn2 -- uses original names
332 = case (cmpPolyType cmpProtoName ty1 ty2) of { EQ_ -> True; _ -> False }
336 eq_lists :: (a -> a -> Bool) -> [a] -> [a] -> Bool
338 eq_lists eq [] [] = True
339 eq_lists eq [] _ = False
340 eq_lists eq _ [] = False
341 eq_lists eq (x:xs) (y:ys) = eq x y && eq_lists eq xs ys