1 module VectType ( vectTyCon, vectType )
4 #include "HsVersions.h"
15 import BasicTypes ( StrictnessMark(..), boolToRecFlag )
19 import Digraph ( SCC(..), stronglyConnComp )
23 import Control.Monad ( liftM2, zipWithM_ )
25 -- ----------------------------------------------------------------------------
28 vectTyCon :: TyCon -> VM TyCon
30 | isFunTyCon tc = builtin closureTyCon
31 | isBoxedTupleTyCon tc = return tc
32 | isUnLiftedTyCon tc = return tc
36 Just tc' -> return tc'
38 -- FIXME: just for now
39 Nothing -> pprTrace "ccTyCon:" (ppr tc) $ return tc
41 vectType :: Type -> VM Type
42 vectType ty | Just ty' <- coreView ty = vectType ty'
43 vectType (TyVarTy tv) = return $ TyVarTy tv
44 vectType (AppTy ty1 ty2) = liftM2 AppTy (vectType ty1) (vectType ty2)
45 vectType (TyConApp tc tys) = liftM2 TyConApp (vectTyCon tc) (mapM vectType tys)
46 vectType (FunTy ty1 ty2) = liftM2 TyConApp (builtin closureTyCon)
47 (mapM vectType [ty1,ty2])
48 vectType ty@(ForAllTy _ _)
50 mdicts <- mapM paDictArgType tyvars
51 mono_ty' <- vectType mono_ty
52 return $ tyvars `mkForAllTys` ([dict | Just dict <- mdicts] `mkFunTys` mono_ty')
54 (tyvars, mono_ty) = splitForAllTys ty
56 vectType ty = pprPanic "vectType:" (ppr ty)
58 -- ----------------------------------------------------------------------------
61 type TyConGroup = ([TyCon], UniqSet TyCon)
63 vectTyConDecls :: [TyCon] -> VM [TyCon]
64 vectTyConDecls tcs = fixV $ \tcs' ->
66 mapM_ (uncurry defTyCon) (lazy_zip tcs tcs')
67 mapM vectTyConDecl tcs
70 lazy_zip (x:xs) ~(y:ys) = (x,y) : lazy_zip xs ys
72 vectTyConDecl :: TyCon -> VM TyCon
75 name' <- cloneName mkVectTyConOcc name
76 rhs' <- vectAlgTyConRhs (algTyConRhs tc)
78 return $ mkAlgTyCon name'
84 NoParentTyCon -- FIXME
85 rec_flag -- FIXME: is this ok?
86 False -- FIXME: no generics
87 False -- not GADT syntax
91 tyvars = tyConTyVars tc
92 rec_flag = boolToRecFlag (isRecursiveTyCon tc)
94 vectAlgTyConRhs :: AlgTyConRhs -> VM AlgTyConRhs
95 vectAlgTyConRhs (DataTyCon { data_cons = data_cons
99 data_cons' <- mapM vectDataCon data_cons
100 zipWithM_ defDataCon data_cons data_cons'
101 return $ DataTyCon { data_cons = data_cons'
105 vectDataCon :: DataCon -> VM DataCon
107 | not . null $ dataConExTyVars dc = pprPanic "vectDataCon: existentials" (ppr dc)
108 | not . null $ dataConEqSpec dc = pprPanic "vectDataCon: eq spec" (ppr dc)
111 name' <- cloneName mkVectDataConOcc name
112 tycon' <- vectTyCon tycon
113 arg_tys <- mapM vectType rep_arg_tys
114 wrk_name <- cloneName mkDataConWorkerOcc name'
116 let ids = mkDataConIds (panic "vectDataCon: wrapped id")
119 data_con = mkDataCon name'
121 (map (const NotMarkedStrict) arg_tys)
122 [] -- no labelled fields
124 [] -- no existential tvs for now
125 [] -- no eq spec for now
129 [] -- no stupid theta
133 name = dataConName dc
134 univ_tvs = dataConUnivTyVars dc
135 rep_arg_tys = dataConOrigArgTys dc
136 tycon = dataConTyCon dc
138 -- | Split the given tycons into two sets depending on whether they have to be
139 -- converted (first list) or not (second list). The first argument contains
140 -- information about the conversion status of external tycons:
142 -- * tycons which have converted versions are mapped to True
143 -- * tycons which are not changed by vectorisation are mapped to False
144 -- * tycons which can't be converted are not elements of the map
146 classifyTyCons :: UniqFM Bool -> [TyConGroup] -> ([TyCon], [TyCon])
147 classifyTyCons = classify [] []
149 classify conv keep cs [] = (conv, keep)
150 classify conv keep cs ((tcs, ds) : rs)
151 | can_convert && must_convert
152 = classify (tcs ++ conv) keep (cs `addListToUFM` [(tc,True) | tc <- tcs]) rs
154 = classify conv (tcs ++ keep) (cs `addListToUFM` [(tc,False) | tc <- tcs]) rs
156 = classify conv keep cs rs
158 refs = ds `delListFromUniqSet` tcs
160 can_convert = isNullUFM (refs `minusUFM` cs) && all convertable tcs
161 must_convert = foldUFM (||) False (intersectUFM_C const cs refs)
163 convertable tc = isDataTyCon tc && all isVanillaDataCon (tyConDataCons tc)
165 -- | Compute mutually recursive groups of tycons in topological order
167 tyConGroups :: [TyCon] -> [TyConGroup]
168 tyConGroups tcs = map mk_grp (stronglyConnComp edges)
170 edges = [((tc, ds), tc, uniqSetToList ds) | tc <- tcs
171 , let ds = tyConsOfTyCon tc]
173 mk_grp (AcyclicSCC (tc, ds)) = ([tc], ds)
174 mk_grp (CyclicSCC els) = (tcs, unionManyUniqSets dss)
176 (tcs, dss) = unzip els
178 tyConsOfTyCon :: TyCon -> UniqSet TyCon
180 = tyConsOfTypes . concatMap dataConRepArgTys . tyConDataCons
182 tyConsOfType :: Type -> UniqSet TyCon
184 | Just ty' <- coreView ty = tyConsOfType ty'
185 tyConsOfType (TyVarTy v) = emptyUniqSet
186 tyConsOfType (TyConApp tc tys) = tyConsOfTypes tys `addOneToUniqSet` tc
187 tyConsOfType (AppTy a b) = tyConsOfType a `unionUniqSets` tyConsOfType b
188 tyConsOfType (FunTy a b) = (tyConsOfType a `unionUniqSets` tyConsOfType b)
189 `addOneToUniqSet` funTyCon
190 tyConsOfType (ForAllTy _ ty) = tyConsOfType ty
191 tyConsOfType other = pprPanic "ClosureConv.tyConsOfType" $ ppr other
193 tyConsOfTypes :: [Type] -> UniqSet TyCon
194 tyConsOfTypes = unionManyUniqSets . map tyConsOfType