2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1994
4 %************************************************************************
6 \section[HsCore]{Core-syntax unfoldings in Haskell interface files}
8 %************************************************************************
10 We could either use this, or parameterise @CoreExpr@ on @UniTypes@ 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(..),
29 import AbsPrel ( PrimOp, PrimKind )
30 import AbsSynFuns ( cmpInstanceTypes )
31 import BasicLit ( BasicLit )
32 import HsTypes -- ( cmpPolyType, PolyType(..), MonoType )
33 import Maybes ( Maybe(..) )
35 import Outputable -- class for printing, forcing
36 import Pretty -- pretty-printing utilities
37 import PrimOps ( tagOf_PrimOp -- HACK
38 IF_ATTACK_PRAGMAS(COMMA pprPrimOp)
40 import ProtoName ( cmpProtoName, eqProtoName, ProtoName(..) ) -- .. for pragmas
44 %************************************************************************
46 \subsection[HsCore-types]{Types for read/written Core unfoldings}
48 %************************************************************************
51 data UnfoldingCoreExpr name
54 | UfCoCon name -- must be a "BoringUfId"...
56 [UnfoldingCoreAtom name]
57 | UfCoPrim (UnfoldingPrimOp name)
59 [UnfoldingCoreAtom name]
60 | UfCoLam [UfBinder name]
61 (UnfoldingCoreExpr name)
63 (UnfoldingCoreExpr name)
64 | UfCoApp (UnfoldingCoreExpr name)
65 (UnfoldingCoreAtom name)
66 | UfCoTyApp (UnfoldingCoreExpr name)
68 | UfCoCase (UnfoldingCoreExpr name)
69 (UnfoldingCoreAlts name)
70 | UfCoLet (UnfoldingCoreBinding name)
71 (UnfoldingCoreExpr name)
72 | UfCoSCC (UfCostCentre name)
73 (UnfoldingCoreExpr name)
75 type ProtoNameCoreExpr = UnfoldingCoreExpr ProtoName
77 data UnfoldingPrimOp name
78 = UfCCallOp FAST_STRING -- callee
79 Bool -- True <=> casm, rather than ccall
80 Bool -- True <=> might cause GC
81 [UnfoldingType name] -- arg types, incl state token
82 -- (which will be first)
83 (UnfoldingType name) -- return type
86 data UnfoldingCoreAlts name
87 = UfCoAlgAlts [(name, [UfBinder name], UnfoldingCoreExpr name)]
88 (UnfoldingCoreDefault name)
89 | UfCoPrimAlts [(BasicLit, UnfoldingCoreExpr name)]
90 (UnfoldingCoreDefault name)
92 data UnfoldingCoreDefault name
94 | UfCoBindDefault (UfBinder name)
95 (UnfoldingCoreExpr name)
97 data UnfoldingCoreBinding name
98 = UfCoNonRec (UfBinder name)
99 (UnfoldingCoreExpr name)
100 | UfCoRec [(UfBinder name, UnfoldingCoreExpr name)]
102 data UnfoldingCoreAtom name
103 = UfCoVarAtom (UfId name)
104 | UfCoLitAtom BasicLit
106 data UfCostCentre name
108 Bool -- True <=> is dupd
109 | UfAllDictsCC FAST_STRING -- module and group
111 Bool -- True <=> is dupd
112 | UfUserCC FAST_STRING
113 FAST_STRING FAST_STRING -- module and group
114 Bool -- True <=> is dupd
115 Bool -- True <=> is CAF
116 | UfAutoCC (UfId name)
117 FAST_STRING FAST_STRING -- module and group
118 Bool Bool -- as above
119 | UfDictCC (UfId name)
120 FAST_STRING FAST_STRING -- module and group
121 Bool Bool -- as above
123 type UfBinder name = (name, UnfoldingType name)
127 | SuperDictSelUfId name name -- class and superclass
128 | ClassOpUfId name name -- class and class op
129 | DictFunUfId name -- class and type
131 | ConstMethodUfId name name -- class, class op, and type
133 | DefaultMethodUfId name name -- class and class op
134 | SpecUfId (UfId name) -- its unspecialised "parent"
135 [Maybe (MonoType name)]
136 | WorkerUfId (UfId name) -- its non-working "parent"
139 type UnfoldingType name = PolyType name
142 %************************************************************************
144 \subsection[HsCore-print]{Printing Core unfoldings}
146 %************************************************************************
149 instance Outputable name => Outputable (UnfoldingCoreExpr name) where
150 ppr sty (UfCoVar v) = pprUfId sty v
151 ppr sty (UfCoLit l) = ppr sty l
153 ppr sty (UfCoCon c tys as)
154 = ppCat [ppStr "(UfCoCon", ppr sty c, ppr sty tys, ppr sty as, ppStr ")"]
155 ppr sty (UfCoPrim o tys as)
156 = ppCat [ppStr "(UfCoPrim", ppr sty o, ppr sty tys, ppr sty as, ppStr ")"]
158 ppr sty (UfCoLam bs body)
159 = ppCat [ppChar '\\', ppr sty bs, ppStr "->", ppr sty body]
160 ppr sty (UfCoTyLam tv body)
161 = ppCat [ppStr "/\\", ppr sty tv, ppStr "->", ppr sty body]
163 ppr sty (UfCoApp fun arg)
164 = ppCat [ppStr "(UfCoApp", ppr sty fun, ppr sty arg, ppStr ")"]
165 ppr sty (UfCoTyApp expr ty)
166 = ppCat [ppStr "(UfCoTyApp", ppr sty expr, ppr sty ty, ppStr ")"]
168 ppr sty (UfCoCase scrut alts)
169 = ppCat [ppStr "case", ppr sty scrut, ppStr "of {", pp_alts alts, ppStr "}"]
171 pp_alts (UfCoAlgAlts alts deflt)
172 = ppCat [ppInterleave ppSemi (map pp_alt alts), pp_deflt deflt]
174 pp_alt (c,bs,rhs) = ppCat [ppr sty c, ppr sty bs, ppStr "->", ppr sty rhs]
175 pp_alts (UfCoPrimAlts alts deflt)
176 = ppCat [ppInterleave ppSemi (map pp_alt alts), pp_deflt deflt]
178 pp_alt (l,rhs) = ppCat [ppr sty l, ppStr "->", ppr sty rhs]
180 pp_deflt UfCoNoDefault = ppNil
181 pp_deflt (UfCoBindDefault b rhs) = ppCat [ppr sty b, ppStr "->", ppr sty rhs]
183 ppr sty (UfCoLet (UfCoNonRec b rhs) body)
184 = ppCat [ppStr "let", ppr sty b, ppEquals, ppr sty rhs, ppStr "in", ppr sty body]
185 ppr sty (UfCoLet (UfCoRec pairs) body)
186 = ppCat [ppStr "letrec {", ppInterleave ppSemi (map pp_pair pairs), ppStr "} in", ppr sty body]
188 pp_pair (b,rhs) = ppCat [ppr sty b, ppEquals, ppr sty rhs]
190 ppr sty (UfCoSCC uf_cc body)
191 = ppCat [ppStr "_scc_ <cost-centre[ToDo]>", ppr sty body]
193 instance Outputable name => Outputable (UnfoldingPrimOp name) where
194 ppr sty (UfCCallOp str is_casm can_gc arg_tys result_ty)
196 before = ppStr (if is_casm then "_casm_ ``" else "_ccall_ ")
197 after = if is_casm then ppStr "'' " else ppSP
199 ppBesides [before, ppPStr str, after,
200 ppLbrack, ppr sty arg_tys, ppRbrack, ppSP, ppr sty result_ty]
201 ppr sty (UfOtherOp op)
204 instance Outputable name => Outputable (UnfoldingCoreAtom name) where
205 ppr sty (UfCoVarAtom v) = pprUfId sty v
206 ppr sty (UfCoLitAtom l) = ppr sty l
208 pprUfId sty (BoringUfId v) = ppr sty v
209 pprUfId sty (SuperDictSelUfId c sc)
210 = ppBesides [ppStr "({-superdict-}", ppr sty c, ppSP, ppr sty sc, ppStr ")"]
211 pprUfId sty (ClassOpUfId c op)
212 = ppBesides [ppStr "({-method-}", ppr sty c, ppSP, ppr sty op, ppStr ")"]
213 pprUfId sty (DictFunUfId c ty)
214 = ppBesides [ppStr "({-dfun-}", ppr sty c, ppSP, ppr sty ty, ppStr ")"]
215 pprUfId sty (ConstMethodUfId c op ty)
216 = ppBesides [ppStr "({-constm-}", ppr sty c, ppSP, ppr sty op, ppSP, ppr sty ty, ppStr ")"]
217 pprUfId sty (DefaultMethodUfId c ty)
218 = ppBesides [ppStr "({-defm-}", ppr sty c, ppSP, ppr sty ty, ppStr ")"]
220 pprUfId sty (SpecUfId unspec ty_maybes)
221 = ppBesides [ppStr "({-spec-} ", pprUfId sty unspec,
222 ppInterleave ppSP (map pp_ty_maybe ty_maybes), ppStr ")"]
224 pp_ty_maybe Nothing = ppStr "_N_"
225 pp_ty_maybe (Just t) = ppr sty t
227 pprUfId sty (WorkerUfId unwrkr)
228 = ppBesides [ppStr "({-wrkr-}", pprUfId sty unwrkr, ppStr ")"]
231 %************************************************************************
233 \subsection[HsCore-equality]{Comparing Core unfoldings}
235 %************************************************************************
237 We want to check that they are {\em exactly} the same.
240 eqUfExpr :: ProtoNameCoreExpr -> ProtoNameCoreExpr -> Bool
242 eqUfExpr (UfCoVar v1) (UfCoVar v2) = eqUfId v1 v2
243 eqUfExpr (UfCoLit l1) (UfCoLit l2) = l1 == l2
245 eqUfExpr (UfCoCon c1 tys1 as1) (UfCoCon c2 tys2 as2)
246 = eq_name c1 c2 && eq_lists eq_type tys1 tys2 && eq_lists eq_atom as1 as2
247 eqUfExpr (UfCoPrim o1 tys1 as1) (UfCoPrim o2 tys2 as2)
248 = eq_op o1 o2 && eq_lists eq_type tys1 tys2 && eq_lists eq_atom as1 as2
250 eq_op (UfCCallOp _ _ _ _ _) (UfCCallOp _ _ _ _ _) = True
251 eq_op (UfOtherOp o1) (UfOtherOp o2)
252 = tagOf_PrimOp o1 _EQ_ tagOf_PrimOp o2
254 eqUfExpr (UfCoLam bs1 body1) (UfCoLam bs2 body2)
255 = eq_lists eq_binder bs1 bs2 && eqUfExpr body1 body2
256 eqUfExpr (UfCoTyLam tv1 body1) (UfCoTyLam tv2 body2)
257 = eq_name tv1 tv2 && eqUfExpr body1 body2
259 eqUfExpr (UfCoApp fun1 arg1) (UfCoApp fun2 arg2)
260 = eqUfExpr fun1 fun2 && eq_atom arg1 arg2
261 eqUfExpr (UfCoTyApp expr1 ty1) (UfCoTyApp expr2 ty2)
262 = eqUfExpr expr1 expr2 && eq_type ty1 ty2
264 eqUfExpr (UfCoCase scrut1 alts1) (UfCoCase scrut2 alts2)
265 = eqUfExpr scrut1 scrut2 && eq_alts alts1 alts2
267 eq_alts (UfCoAlgAlts alts1 deflt1) (UfCoAlgAlts alts2 deflt2)
268 = eq_lists eq_alt alts1 alts2 && eq_deflt deflt1 deflt2
270 eq_alt (c1,bs1,rhs1) (c2,bs2,rhs2)
271 = eq_name c1 c2 && eq_lists eq_binder bs1 bs2 && eqUfExpr rhs1 rhs2
273 eq_alts (UfCoPrimAlts alts1 deflt1) (UfCoPrimAlts alts2 deflt2)
274 = eq_lists eq_alt alts1 alts2 && eq_deflt deflt1 deflt2
276 eq_alt (l1,rhs1) (l2,rhs2)
277 = l1 == l2 && eqUfExpr rhs1 rhs2
279 eq_alts _ _ = False -- catch-all
281 eq_deflt UfCoNoDefault UfCoNoDefault = True
282 eq_deflt (UfCoBindDefault b1 rhs1) (UfCoBindDefault b2 rhs2)
283 = eq_binder b1 b2 && eqUfExpr rhs1 rhs2
286 eqUfExpr (UfCoLet (UfCoNonRec b1 rhs1) body1) (UfCoLet (UfCoNonRec b2 rhs2) body2)
287 = eq_binder b1 b2 && eqUfExpr rhs1 rhs2 && eqUfExpr body1 body2
289 eqUfExpr (UfCoLet (UfCoRec pairs1) body1) (UfCoLet (UfCoRec pairs2) body2)
290 = eq_lists eq_pair pairs1 pairs2 && eqUfExpr body1 body2
292 eq_pair (b1,rhs1) (b2,rhs2) = eq_binder b1 b2 && eqUfExpr rhs1 rhs2
294 eqUfExpr (UfCoSCC cc1 body1) (UfCoSCC cc2 body2)
295 = {-trace "eqUfExpr: not comparing cost-centres!"-} (eqUfExpr body1 body2)
297 eqUfExpr _ _ = False -- Catch-all
301 eqUfId (BoringUfId n1) (BoringUfId n2)
303 eqUfId (SuperDictSelUfId a1 b1) (SuperDictSelUfId a2 b2)
304 = eq_name a1 a2 && eq_name b1 b2
305 eqUfId (ClassOpUfId a1 b1) (ClassOpUfId a2 b2)
306 = eq_name a1 a2 && eq_name b1 b2
307 eqUfId (DictFunUfId c1 t1) (DictFunUfId c2 t2)
308 = eq_name c1 c2 && eq_tycon t1 t2 -- NB: **** only compare TyCons ******
310 eq_tycon (UnoverloadedTy ty1) (UnoverloadedTy ty2)
311 = case (cmpInstanceTypes ty1 ty2) of { EQ_ -> True; _ -> False }
313 = trace "eq_tycon" (eq_type ty1 ty2) -- desperately try something else
315 eqUfId (ConstMethodUfId a1 b1 t1) (ConstMethodUfId a2 b2 t2)
316 = eq_name a1 a2 && eq_name b1 b2 && eq_type t1 t2
317 eqUfId (DefaultMethodUfId a1 b1) (DefaultMethodUfId a2 b2)
318 = eq_name a1 a2 && eq_name b1 b2
319 eqUfId (SpecUfId id1 tms1) (SpecUfId id2 tms2)
320 = eqUfId id1 id2 && eq_lists eq_ty_maybe tms1 tms2
322 eq_ty_maybe Nothing Nothing = True
323 eq_ty_maybe (Just ty1) (Just ty2)
324 = eq_type (UnoverloadedTy ty1) (UnoverloadedTy ty2)
325 -- a HACKy way to compare MonoTypes (ToDo) [WDP 94/05/02]
326 eq_ty_maybe _ _ = False
327 eqUfId (WorkerUfId id1) (WorkerUfId id2)
329 eqUfId _ _ = False -- catch-all
333 eq_atom (UfCoVarAtom id1) (UfCoVarAtom id2) = eqUfId id1 id2
334 eq_atom (UfCoLitAtom l1) (UfCoLitAtom l2) = l1 == l2
337 eq_binder (n1, ty1) (n2, ty2) = eq_name n1 n2 && eq_type ty1 ty2
339 eq_name :: ProtoName -> ProtoName -> Bool
340 eq_name pn1 pn2 = eqProtoName pn1 pn2 -- uses original names
343 = case (cmpPolyType cmpProtoName ty1 ty2) of { EQ_ -> True; _ -> False }
347 eq_lists :: (a -> a -> Bool) -> [a] -> [a] -> Bool
349 eq_lists eq [] [] = True
350 eq_lists eq [] _ = False
351 eq_lists eq _ [] = False
352 eq_lists eq (x:xs) (y:ys) = eq x y && eq_lists eq xs ys