Generation of DataCons for implicit PArray instances
[ghc-hetmet.git] / compiler / vectorise / VectType.hs
1 module VectType ( vectTyCon, vectType, vectTypeEnv )
2 where
3
4 #include "HsVersions.h"
5
6 import VectMonad
7 import VectUtils
8
9 import HscTypes          ( TypeEnv, extendTypeEnvList, typeEnvTyCons )
10 import DataCon
11 import TyCon
12 import Type
13 import TypeRep
14 import OccName
15 import MkId
16 import BasicTypes        ( StrictnessMark(..), boolToRecFlag )
17 import NameEnv
18 import TysWiredIn
19
20 import Unique
21 import UniqFM
22 import UniqSet
23 import Digraph           ( SCC(..), stronglyConnComp )
24
25 import Outputable
26
27 import Control.Monad  ( liftM2, zipWithM_ )
28
29 -- ----------------------------------------------------------------------------
30 -- Types
31
32 vectTyCon :: TyCon -> VM TyCon
33 vectTyCon tc
34   | isFunTyCon tc        = builtin closureTyCon
35   | isBoxedTupleTyCon tc = return tc
36   | isUnLiftedTyCon tc   = return tc
37   | otherwise = do
38                   r <- lookupTyCon tc
39                   case r of
40                     Just tc' -> return tc'
41
42                     -- FIXME: just for now
43                     Nothing  -> pprTrace "ccTyCon:" (ppr tc) $ return tc
44
45 vectType :: Type -> VM Type
46 vectType ty | Just ty' <- coreView ty = vectType ty'
47 vectType (TyVarTy tv) = return $ TyVarTy tv
48 vectType (AppTy ty1 ty2) = liftM2 AppTy (vectType ty1) (vectType ty2)
49 vectType (TyConApp tc tys) = liftM2 TyConApp (vectTyCon tc) (mapM vectType tys)
50 vectType (FunTy ty1 ty2)   = liftM2 TyConApp (builtin closureTyCon)
51                                              (mapM vectType [ty1,ty2])
52 vectType ty@(ForAllTy _ _)
53   = do
54       mdicts   <- mapM paDictArgType tyvars
55       mono_ty' <- vectType mono_ty
56       return $ tyvars `mkForAllTys` ([dict | Just dict <- mdicts] `mkFunTys` mono_ty')
57   where
58     (tyvars, mono_ty) = splitForAllTys ty
59
60 vectType ty = pprPanic "vectType:" (ppr ty)
61
62 -- ----------------------------------------------------------------------------
63 -- Type definitions
64
65 type TyConGroup = ([TyCon], UniqSet TyCon)
66
67 vectTypeEnv :: TypeEnv -> VM TypeEnv
68 vectTypeEnv env
69   = do
70       cs <- readGEnv $ mk_map . global_tycons
71       let (conv_tcs, keep_tcs) = classifyTyCons cs groups
72           keep_dcs             = concatMap tyConDataCons keep_tcs
73       zipWithM_ defTyCon   keep_tcs keep_tcs
74       zipWithM_ defDataCon keep_dcs keep_dcs
75       new_tcs <- vectTyConDecls conv_tcs
76       return $ extendTypeEnvList env
77                  (map ATyCon new_tcs ++ [ADataCon dc | tc <- new_tcs
78                                                      , dc <- tyConDataCons tc])
79   where
80     tycons = typeEnvTyCons env
81     groups = tyConGroups tycons
82
83     mk_map env = listToUFM_Directly [(u, getUnique n /= u) | (u,n) <- nameEnvUniqueElts env]
84
85     keep_tc tc = let dcs = tyConDataCons tc
86                  in
87                  defTyCon tc tc >> zipWithM_ defDataCon dcs dcs
88
89
90 vectTyConDecls :: [TyCon] -> VM [TyCon]
91 vectTyConDecls tcs = fixV $ \tcs' ->
92   do
93     mapM_ (uncurry defTyCon) (lazy_zip tcs tcs')
94     mapM vectTyConDecl tcs
95   where
96     lazy_zip [] _ = []
97     lazy_zip (x:xs) ~(y:ys) = (x,y) : lazy_zip xs ys
98
99 vectTyConDecl :: TyCon -> VM TyCon
100 vectTyConDecl tc
101   = do
102       name' <- cloneName mkVectTyConOcc name
103       rhs'  <- vectAlgTyConRhs (algTyConRhs tc)
104
105       return $ mkAlgTyCon name'
106                           kind
107                           tyvars
108                           []              -- no stupid theta
109                           rhs'
110                           []              -- no selector ids
111                           NoParentTyCon   -- FIXME
112                           rec_flag        -- FIXME: is this ok?
113                           False           -- FIXME: no generics
114                           False           -- not GADT syntax
115   where
116     name   = tyConName tc
117     kind   = tyConKind tc
118     tyvars = tyConTyVars tc
119     rec_flag = boolToRecFlag (isRecursiveTyCon tc)
120
121 vectAlgTyConRhs :: AlgTyConRhs -> VM AlgTyConRhs
122 vectAlgTyConRhs (DataTyCon { data_cons = data_cons
123                            , is_enum   = is_enum
124                            })
125   = do
126       data_cons' <- mapM vectDataCon data_cons
127       zipWithM_ defDataCon data_cons data_cons'
128       return $ DataTyCon { data_cons = data_cons'
129                          , is_enum   = is_enum
130                          }
131
132 vectDataCon :: DataCon -> VM DataCon
133 vectDataCon dc
134   | not . null $ dataConExTyVars dc = pprPanic "vectDataCon: existentials" (ppr dc)
135   | not . null $ dataConEqSpec   dc = pprPanic "vectDataCon: eq spec" (ppr dc)
136   | otherwise
137   = do
138       name'    <- cloneName mkVectDataConOcc name
139       tycon'   <- vectTyCon tycon
140       arg_tys  <- mapM vectType rep_arg_tys
141       wrk_name <- cloneName mkDataConWorkerOcc name'
142
143       let ids      = mkDataConIds (panic "vectDataCon: wrapper id")
144                                   wrk_name
145                                   data_con
146           data_con = mkDataCon name'
147                                False           -- not infix
148                                (map (const NotMarkedStrict) arg_tys)
149                                []              -- no labelled fields
150                                univ_tvs
151                                []              -- no existential tvs for now
152                                []              -- no eq spec for now
153                                []              -- no theta
154                                arg_tys
155                                tycon'
156                                []              -- no stupid theta
157                                ids
158       return data_con
159   where
160     name        = dataConName dc
161     univ_tvs    = dataConUnivTyVars dc
162     rep_arg_tys = dataConRepArgTys dc
163     tycon       = dataConTyCon dc
164
165 {-
166 mkPArrTyCon :: TyCon -> VM TyCon
167 mkPArrTyCon tc = fixV $ \repr_tc ->
168   do
169 -}
170
171 mkPArrayDataCon :: TyCon -> TyCon -> VM DataCon
172 mkPArrayDataCon orig_tc repr_tc
173   = do
174       name     <- cloneName mkPArrayDataConOcc (tyConName orig_tc)
175       shape_ty <- mkPArrayType intTy   -- FIXME: we want to unbox this!
176       repr_tys <- mapM mkPArrayType types
177       wrk_name <- cloneName mkDataConWorkerOcc name
178       wrp_name <- cloneName mkDataConWrapperOcc name
179
180       let ids      = mkDataConIds wrp_name wrk_name data_con
181           data_con = mkDataCon name
182                                False
183                                (MarkedStrict : map (const NotMarkedStrict) repr_tys)
184                                []
185                                (tyConTyVars orig_tc)
186                                []
187                                []
188                                []
189                                repr_tys
190                                repr_tc
191                                []
192                                ids
193
194       return data_con
195   where
196     types = [ty | dc <- tyConDataCons orig_tc
197                 , ty <- dataConRepArgTys dc]
198
199 -- | Split the given tycons into two sets depending on whether they have to be
200 -- converted (first list) or not (second list). The first argument contains
201 -- information about the conversion status of external tycons:
202 -- 
203 --   * tycons which have converted versions are mapped to True
204 --   * tycons which are not changed by vectorisation are mapped to False
205 --   * tycons which can't be converted are not elements of the map
206 --
207 classifyTyCons :: UniqFM Bool -> [TyConGroup] -> ([TyCon], [TyCon])
208 classifyTyCons = classify [] []
209   where
210     classify conv keep cs [] = (conv, keep)
211     classify conv keep cs ((tcs, ds) : rs)
212       | can_convert && must_convert
213         = classify (tcs ++ conv) keep (cs `addListToUFM` [(tc,True) | tc <- tcs]) rs
214       | can_convert
215         = classify conv (tcs ++ keep) (cs `addListToUFM` [(tc,False) | tc <- tcs]) rs
216       | otherwise
217         = classify conv keep cs rs
218       where
219         refs = ds `delListFromUniqSet` tcs
220
221         can_convert  = isNullUFM (refs `minusUFM` cs) && all convertable tcs
222         must_convert = foldUFM (||) False (intersectUFM_C const cs refs)
223
224         convertable tc = isDataTyCon tc && all isVanillaDataCon (tyConDataCons tc)
225     
226 -- | Compute mutually recursive groups of tycons in topological order
227 --
228 tyConGroups :: [TyCon] -> [TyConGroup]
229 tyConGroups tcs = map mk_grp (stronglyConnComp edges)
230   where
231     edges = [((tc, ds), tc, uniqSetToList ds) | tc <- tcs
232                                 , let ds = tyConsOfTyCon tc]
233
234     mk_grp (AcyclicSCC (tc, ds)) = ([tc], ds)
235     mk_grp (CyclicSCC els)       = (tcs, unionManyUniqSets dss)
236       where
237         (tcs, dss) = unzip els
238
239 tyConsOfTyCon :: TyCon -> UniqSet TyCon
240 tyConsOfTyCon 
241   = tyConsOfTypes . concatMap dataConRepArgTys . tyConDataCons
242
243 tyConsOfType :: Type -> UniqSet TyCon
244 tyConsOfType ty
245   | Just ty' <- coreView ty    = tyConsOfType ty'
246 tyConsOfType (TyVarTy v)       = emptyUniqSet
247 tyConsOfType (TyConApp tc tys) = extend (tyConsOfTypes tys)
248   where
249     extend | isUnLiftedTyCon tc
250            || isTupleTyCon   tc = id
251
252            | otherwise          = (`addOneToUniqSet` tc)
253
254 tyConsOfType (AppTy a b)       = tyConsOfType a `unionUniqSets` tyConsOfType b
255 tyConsOfType (FunTy a b)       = (tyConsOfType a `unionUniqSets` tyConsOfType b)
256                                  `addOneToUniqSet` funTyCon
257 tyConsOfType (ForAllTy _ ty)   = tyConsOfType ty
258 tyConsOfType other             = pprPanic "ClosureConv.tyConsOfType" $ ppr other
259
260 tyConsOfTypes :: [Type] -> UniqSet TyCon
261 tyConsOfTypes = unionManyUniqSets . map tyConsOfType
262