1 module VectType ( vectTyCon, vectType, vectTypeEnv )
4 #include "HsVersions.h"
9 import HscTypes ( TypeEnv, extendTypeEnvList, typeEnvTyCons )
16 import BasicTypes ( StrictnessMark(..), boolToRecFlag )
22 import Digraph ( SCC(..), stronglyConnComp )
26 import Control.Monad ( liftM2, zipWithM_ )
28 -- ----------------------------------------------------------------------------
31 vectTyCon :: TyCon -> VM TyCon
33 | isFunTyCon tc = builtin closureTyCon
34 | isBoxedTupleTyCon tc = return tc
35 | isUnLiftedTyCon tc = return tc
39 Just tc' -> return tc'
41 -- FIXME: just for now
42 Nothing -> pprTrace "ccTyCon:" (ppr tc) $ return tc
44 vectType :: Type -> VM Type
45 vectType ty | Just ty' <- coreView ty = vectType ty'
46 vectType (TyVarTy tv) = return $ TyVarTy tv
47 vectType (AppTy ty1 ty2) = liftM2 AppTy (vectType ty1) (vectType ty2)
48 vectType (TyConApp tc tys) = liftM2 TyConApp (vectTyCon tc) (mapM vectType tys)
49 vectType (FunTy ty1 ty2) = liftM2 TyConApp (builtin closureTyCon)
50 (mapM vectType [ty1,ty2])
51 vectType ty@(ForAllTy _ _)
53 mdicts <- mapM paDictArgType tyvars
54 mono_ty' <- vectType mono_ty
55 return $ tyvars `mkForAllTys` ([dict | Just dict <- mdicts] `mkFunTys` mono_ty')
57 (tyvars, mono_ty) = splitForAllTys ty
59 vectType ty = pprPanic "vectType:" (ppr ty)
61 -- ----------------------------------------------------------------------------
64 type TyConGroup = ([TyCon], UniqSet TyCon)
66 vectTypeEnv :: TypeEnv -> VM TypeEnv
69 cs <- readGEnv $ mk_map . global_tycons
70 let (conv_tcs, keep_tcs) = classifyTyCons cs groups
71 keep_dcs = concatMap tyConDataCons keep_tcs
72 zipWithM_ defTyCon keep_tcs keep_tcs
73 zipWithM_ defDataCon keep_dcs keep_dcs
74 new_tcs <- vectTyConDecls conv_tcs
75 return $ extendTypeEnvList env
76 (map ATyCon new_tcs ++ [ADataCon dc | tc <- new_tcs
77 , dc <- tyConDataCons tc])
79 tycons = typeEnvTyCons env
80 groups = tyConGroups tycons
82 mk_map env = listToUFM_Directly [(u, getUnique n /= u) | (u,n) <- nameEnvUniqueElts env]
84 keep_tc tc = let dcs = tyConDataCons tc
86 defTyCon tc tc >> zipWithM_ defDataCon dcs dcs
89 vectTyConDecls :: [TyCon] -> VM [TyCon]
90 vectTyConDecls tcs = fixV $ \tcs' ->
92 mapM_ (uncurry defTyCon) (lazy_zip tcs tcs')
93 mapM vectTyConDecl tcs
96 lazy_zip (x:xs) ~(y:ys) = (x,y) : lazy_zip xs ys
98 vectTyConDecl :: TyCon -> VM TyCon
101 name' <- cloneName mkVectTyConOcc name
102 rhs' <- vectAlgTyConRhs (algTyConRhs tc)
104 return $ mkAlgTyCon name'
107 [] -- no stupid theta
109 [] -- no selector ids
110 NoParentTyCon -- FIXME
111 rec_flag -- FIXME: is this ok?
112 False -- FIXME: no generics
113 False -- not GADT syntax
117 tyvars = tyConTyVars tc
118 rec_flag = boolToRecFlag (isRecursiveTyCon tc)
120 vectAlgTyConRhs :: AlgTyConRhs -> VM AlgTyConRhs
121 vectAlgTyConRhs (DataTyCon { data_cons = data_cons
125 data_cons' <- mapM vectDataCon data_cons
126 zipWithM_ defDataCon data_cons data_cons'
127 return $ DataTyCon { data_cons = data_cons'
131 vectDataCon :: DataCon -> VM DataCon
133 | not . null $ dataConExTyVars dc = pprPanic "vectDataCon: existentials" (ppr dc)
134 | not . null $ dataConEqSpec dc = pprPanic "vectDataCon: eq spec" (ppr dc)
137 name' <- cloneName mkVectDataConOcc name
138 tycon' <- vectTyCon tycon
139 arg_tys <- mapM vectType rep_arg_tys
140 wrk_name <- cloneName mkDataConWorkerOcc name'
142 let ids = mkDataConIds (panic "vectDataCon: wrapped id")
145 data_con = mkDataCon name'
147 (map (const NotMarkedStrict) arg_tys)
148 [] -- no labelled fields
150 [] -- no existential tvs for now
151 [] -- no eq spec for now
155 [] -- no stupid theta
159 name = dataConName dc
160 univ_tvs = dataConUnivTyVars dc
161 rep_arg_tys = dataConOrigArgTys dc
162 tycon = dataConTyCon dc
164 -- | Split the given tycons into two sets depending on whether they have to be
165 -- converted (first list) or not (second list). The first argument contains
166 -- information about the conversion status of external tycons:
168 -- * tycons which have converted versions are mapped to True
169 -- * tycons which are not changed by vectorisation are mapped to False
170 -- * tycons which can't be converted are not elements of the map
172 classifyTyCons :: UniqFM Bool -> [TyConGroup] -> ([TyCon], [TyCon])
173 classifyTyCons = classify [] []
175 classify conv keep cs [] = (conv, keep)
176 classify conv keep cs ((tcs, ds) : rs)
177 | can_convert && must_convert
178 = classify (tcs ++ conv) keep (cs `addListToUFM` [(tc,True) | tc <- tcs]) rs
180 = classify conv (tcs ++ keep) (cs `addListToUFM` [(tc,False) | tc <- tcs]) rs
182 = classify conv keep cs rs
184 refs = ds `delListFromUniqSet` tcs
186 can_convert = isNullUFM (refs `minusUFM` cs) && all convertable tcs
187 must_convert = foldUFM (||) False (intersectUFM_C const cs refs)
189 convertable tc = isDataTyCon tc && all isVanillaDataCon (tyConDataCons tc)
191 -- | Compute mutually recursive groups of tycons in topological order
193 tyConGroups :: [TyCon] -> [TyConGroup]
194 tyConGroups tcs = map mk_grp (stronglyConnComp edges)
196 edges = [((tc, ds), tc, uniqSetToList ds) | tc <- tcs
197 , let ds = tyConsOfTyCon tc]
199 mk_grp (AcyclicSCC (tc, ds)) = ([tc], ds)
200 mk_grp (CyclicSCC els) = (tcs, unionManyUniqSets dss)
202 (tcs, dss) = unzip els
204 tyConsOfTyCon :: TyCon -> UniqSet TyCon
206 = tyConsOfTypes . concatMap dataConRepArgTys . tyConDataCons
208 tyConsOfType :: Type -> UniqSet TyCon
210 | Just ty' <- coreView ty = tyConsOfType ty'
211 tyConsOfType (TyVarTy v) = emptyUniqSet
212 tyConsOfType (TyConApp tc tys) = tyConsOfTypes tys `addOneToUniqSet` tc
213 tyConsOfType (AppTy a b) = tyConsOfType a `unionUniqSets` tyConsOfType b
214 tyConsOfType (FunTy a b) = (tyConsOfType a `unionUniqSets` tyConsOfType b)
215 `addOneToUniqSet` funTyCon
216 tyConsOfType (ForAllTy _ ty) = tyConsOfType ty
217 tyConsOfType other = pprPanic "ClosureConv.tyConsOfType" $ ppr other
219 tyConsOfTypes :: [Type] -> UniqSet TyCon
220 tyConsOfTypes = unionManyUniqSets . map tyConsOfType