5 noV, tryV, maybeV, traceMaybeV, orElseV, fixV, localV, closedV, initV,
8 newExportedVar, newLocalVar, newDummyVar, newTyVar,
15 readGEnv, setGEnv, updGEnv,
18 readLEnv, setLEnv, updLEnv,
22 lookupVar, defGlobalVar,
23 lookupTyCon, defTyCon,
24 lookupDataCon, defDataCon,
25 lookupTyConPA, defTyConPA, defTyConPAs,
26 lookupTyVarPA, defLocalTyVar, defLocalTyVarWithPA, localTyVars,
28 {-lookupInst,-} lookupFamInst
31 #include "HsVersions.h"
47 import TysPrim ( intPrimTy )
50 import IOEnv ( ioToIOEnv )
61 import SrcLoc ( noSrcSpan )
63 import Control.Monad ( liftM, zipWithM )
65 data Scope a b = Global a | Local b
67 -- ----------------------------------------------------------------------------
68 -- Vectorisation monad
70 data GlobalEnv = GlobalEnv {
71 -- Mapping from global variables to their vectorised versions.
73 global_vars :: VarEnv Var
75 -- Exported variables which have a vectorised version
77 , global_exported_vars :: VarEnv (Var, Var)
79 -- Mapping from TyCons to their vectorised versions.
80 -- TyCons which do not have to be vectorised are mapped to
83 , global_tycons :: NameEnv TyCon
85 -- Mapping from DataCons to their vectorised versions
87 , global_datacons :: NameEnv DataCon
89 -- Mapping from TyCons to their PA dfuns
91 , global_pa_funs :: NameEnv Var
93 -- External package inst-env & home-package inst-env for class
96 , global_inst_env :: (InstEnv, InstEnv)
98 -- External package inst-env & home-package inst-env for family
101 , global_fam_inst_env :: FamInstEnvs
104 , global_bindings :: [(Var, CoreExpr)]
107 data LocalEnv = LocalEnv {
108 -- Mapping from local variables to their vectorised and
111 local_vars :: VarEnv (Var, Var)
113 -- In-scope type variables
115 , local_tyvars :: [TyVar]
117 -- Mapping from tyvars to their PA dictionaries
118 , local_tyvar_pa :: VarEnv CoreExpr
120 -- Local binding name
121 , local_bind_name :: FastString
124 initGlobalEnv :: VectInfo -> (InstEnv, InstEnv) -> FamInstEnvs -> GlobalEnv
125 initGlobalEnv info instEnvs famInstEnvs
127 global_vars = mapVarEnv snd $ vectInfoVar info
128 , global_exported_vars = emptyVarEnv
129 , global_tycons = mapNameEnv snd $ vectInfoTyCon info
130 , global_datacons = mapNameEnv snd $ vectInfoDataCon info
131 , global_pa_funs = mapNameEnv snd $ vectInfoPADFun info
132 , global_inst_env = instEnvs
133 , global_fam_inst_env = famInstEnvs
134 , global_bindings = []
137 setFamInstEnv :: FamInstEnv -> GlobalEnv -> GlobalEnv
138 setFamInstEnv l_fam_inst genv
139 = genv { global_fam_inst_env = (g_fam_inst, l_fam_inst) }
141 (g_fam_inst, _) = global_fam_inst_env genv
143 extendTyConsEnv :: [(Name, TyCon)] -> GlobalEnv -> GlobalEnv
144 extendTyConsEnv ps genv
145 = genv { global_tycons = extendNameEnvList (global_tycons genv) ps }
147 extendPAFunsEnv :: [(Name, Var)] -> GlobalEnv -> GlobalEnv
148 extendPAFunsEnv ps genv
149 = genv { global_pa_funs = extendNameEnvList (global_pa_funs genv) ps }
151 emptyLocalEnv = LocalEnv {
152 local_vars = emptyVarEnv
154 , local_tyvar_pa = emptyVarEnv
155 , local_bind_name = FSLIT("fn")
159 updVectInfo :: GlobalEnv -> TypeEnv -> VectInfo -> VectInfo
160 updVectInfo env tyenv info
162 vectInfoVar = global_exported_vars env
163 , vectInfoTyCon = mk_env typeEnvTyCons global_tycons
164 , vectInfoDataCon = mk_env typeEnvDataCons global_datacons
165 , vectInfoPADFun = mk_env typeEnvTyCons global_pa_funs
168 mk_env from_tyenv from_env = mkNameEnv [(name, (from,to))
169 | from <- from_tyenv tyenv
170 , let name = getName from
171 , Just to <- [lookupNameEnv (from_env env) name]]
173 data VResult a = Yes GlobalEnv LocalEnv a | No
175 newtype VM a = VM { runVM :: Builtins -> GlobalEnv -> LocalEnv -> DsM (VResult a) }
177 instance Monad VM where
178 return x = VM $ \bi genv lenv -> return (Yes genv lenv x)
179 VM p >>= f = VM $ \bi genv lenv -> do
182 Yes genv' lenv' x -> runVM (f x) bi genv' lenv'
186 noV = VM $ \_ _ _ -> return No
188 traceNoV :: String -> SDoc -> VM a
189 traceNoV s d = pprTrace s d noV
191 tryV :: VM a -> VM (Maybe a)
192 tryV (VM p) = VM $ \bi genv lenv ->
196 Yes genv' lenv' x -> return (Yes genv' lenv' (Just x))
197 No -> return (Yes genv lenv Nothing)
199 maybeV :: VM (Maybe a) -> VM a
200 maybeV p = maybe noV return =<< p
202 traceMaybeV :: String -> SDoc -> VM (Maybe a) -> VM a
203 traceMaybeV s d p = maybe (traceNoV s d) return =<< p
205 orElseV :: VM a -> VM a -> VM a
206 orElseV p q = maybe q return =<< tryV p
208 fixV :: (a -> VM a) -> VM a
209 fixV f = VM (\bi genv lenv -> fixDs $ \r -> runVM (f (unYes r)) bi genv lenv )
211 unYes (Yes _ _ x) = x
213 localV :: VM a -> VM a
220 closedV :: VM a -> VM a
223 setLEnv (emptyLocalEnv { local_bind_name = local_bind_name env })
228 liftDs :: DsM a -> VM a
229 liftDs p = VM $ \bi genv lenv -> do { x <- p; return (Yes genv lenv x) }
231 builtin :: (Builtins -> a) -> VM a
232 builtin f = VM $ \bi genv lenv -> return (Yes genv lenv (f bi))
234 readGEnv :: (GlobalEnv -> a) -> VM a
235 readGEnv f = VM $ \bi genv lenv -> return (Yes genv lenv (f genv))
237 setGEnv :: GlobalEnv -> VM ()
238 setGEnv genv = VM $ \_ _ lenv -> return (Yes genv lenv ())
240 updGEnv :: (GlobalEnv -> GlobalEnv) -> VM ()
241 updGEnv f = VM $ \_ genv lenv -> return (Yes (f genv) lenv ())
243 readLEnv :: (LocalEnv -> a) -> VM a
244 readLEnv f = VM $ \bi genv lenv -> return (Yes genv lenv (f lenv))
246 setLEnv :: LocalEnv -> VM ()
247 setLEnv lenv = VM $ \_ genv _ -> return (Yes genv lenv ())
249 updLEnv :: (LocalEnv -> LocalEnv) -> VM ()
250 updLEnv f = VM $ \_ genv lenv -> return (Yes genv (f lenv) ())
252 getInstEnv :: VM (InstEnv, InstEnv)
253 getInstEnv = readGEnv global_inst_env
255 getFamInstEnv :: VM FamInstEnvs
256 getFamInstEnv = readGEnv global_fam_inst_env
258 getBindName :: VM FastString
259 getBindName = readLEnv local_bind_name
261 inBind :: Id -> VM a -> VM a
263 = do updLEnv $ \env -> env { local_bind_name = occNameFS (getOccName id) }
266 cloneName :: (OccName -> OccName) -> Name -> VM Name
267 cloneName mk_occ name = liftM make (liftDs newUnique)
269 occ_name = mk_occ (nameOccName name)
271 make u | isExternalName name = mkExternalName u (nameModule name)
274 | otherwise = mkSystemName u occ_name
276 cloneId :: (OccName -> OccName) -> Id -> Type -> VM Id
279 name <- cloneName mk_occ (getName id)
280 let id' | isExportedId id = Id.mkExportedLocalId name ty
281 | otherwise = Id.mkLocalId name ty
284 newExportedVar :: OccName -> Type -> VM Var
285 newExportedVar occ_name ty
287 mod <- liftDs getModuleDs
288 u <- liftDs newUnique
290 let name = mkExternalName u mod occ_name noSrcSpan
292 return $ Id.mkExportedLocalId name ty
294 newLocalVar :: FastString -> Type -> VM Var
297 u <- liftDs newUnique
298 return $ mkSysLocal fs u ty
300 newDummyVar :: Type -> VM Var
301 newDummyVar = newLocalVar FSLIT("ds")
303 newTyVar :: FastString -> Kind -> VM Var
306 u <- liftDs newUnique
307 return $ mkTyVar (mkSysTvName u fs) k
309 defGlobalVar :: Var -> Var -> VM ()
310 defGlobalVar v v' = updGEnv $ \env ->
311 env { global_vars = extendVarEnv (global_vars env) v v'
312 , global_exported_vars = upd (global_exported_vars env)
315 upd env | isExportedId v = extendVarEnv env v (v, v')
318 lookupVar :: Var -> VM (Scope Var (Var, Var))
321 r <- readLEnv $ \env -> lookupVarEnv (local_vars env) v
323 Just e -> return (Local e)
324 Nothing -> liftM Global
325 $ traceMaybeV "lookupVar" (ppr v)
326 (readGEnv $ \env -> lookupVarEnv (global_vars env) v)
328 lookupTyCon :: TyCon -> VM (Maybe TyCon)
330 | isUnLiftedTyCon tc || isTupleTyCon tc = return (Just tc)
332 | otherwise = readGEnv $ \env -> lookupNameEnv (global_tycons env) (tyConName tc)
334 defTyCon :: TyCon -> TyCon -> VM ()
335 defTyCon tc tc' = updGEnv $ \env ->
336 env { global_tycons = extendNameEnv (global_tycons env) (tyConName tc) tc' }
338 lookupDataCon :: DataCon -> VM (Maybe DataCon)
339 lookupDataCon dc = readGEnv $ \env -> lookupNameEnv (global_datacons env) (dataConName dc)
341 defDataCon :: DataCon -> DataCon -> VM ()
342 defDataCon dc dc' = updGEnv $ \env ->
343 env { global_datacons = extendNameEnv (global_datacons env) (dataConName dc) dc' }
345 lookupTyConPA :: TyCon -> VM (Maybe Var)
346 lookupTyConPA tc = readGEnv $ \env -> lookupNameEnv (global_pa_funs env) (tyConName tc)
348 defTyConPA :: TyCon -> Var -> VM ()
349 defTyConPA tc pa = updGEnv $ \env ->
350 env { global_pa_funs = extendNameEnv (global_pa_funs env) (tyConName tc) pa }
352 defTyConPAs :: [(TyCon, Var)] -> VM ()
353 defTyConPAs ps = updGEnv $ \env ->
354 env { global_pa_funs = extendNameEnvList (global_pa_funs env)
355 [(tyConName tc, pa) | (tc, pa) <- ps] }
357 lookupTyVarPA :: Var -> VM (Maybe CoreExpr)
358 lookupTyVarPA tv = readLEnv $ \env -> lookupVarEnv (local_tyvar_pa env) tv
360 defLocalTyVar :: TyVar -> VM ()
361 defLocalTyVar tv = updLEnv $ \env ->
362 env { local_tyvars = tv : local_tyvars env
363 , local_tyvar_pa = local_tyvar_pa env `delVarEnv` tv
366 defLocalTyVarWithPA :: TyVar -> CoreExpr -> VM ()
367 defLocalTyVarWithPA tv pa = updLEnv $ \env ->
368 env { local_tyvars = tv : local_tyvars env
369 , local_tyvar_pa = extendVarEnv (local_tyvar_pa env) tv pa
372 localTyVars :: VM [TyVar]
373 localTyVars = readLEnv (reverse . local_tyvars)
375 -- Look up the dfun of a class instance.
377 -- The match must be unique - ie, match exactly one instance - but the
378 -- type arguments used for matching may be more specific than those of
379 -- the class instance declaration. The found class instances must not have
380 -- any type variables in the instance context that do not appear in the
381 -- instances head (i.e., no flexi vars); for details for what this means,
382 -- see the docs at InstEnv.lookupInstEnv.
385 lookupInst :: Class -> [Type] -> VM (DFunId, [Type])
387 = do { instEnv <- getInstEnv
388 ; case lookupInstEnv instEnv cls tys of
389 ([(inst, inst_tys)], _)
390 | noFlexiVar -> return (instanceDFunId inst, inst_tys')
391 | otherwise -> pprPanic "VectMonad.lookupInst: flexi var: "
392 (ppr $ mkTyConApp (classTyCon cls) tys)
394 inst_tys' = [ty | Right ty <- inst_tys]
395 noFlexiVar = all isRight inst_tys
396 _other -> traceNoV "lookupInst" (ppr cls <+> ppr tys)
399 isRight (Left _) = False
400 isRight (Right _) = True
403 -- Look up the representation tycon of a family instance.
405 -- The match must be unique - ie, match exactly one instance - but the
406 -- type arguments used for matching may be more specific than those of
407 -- the family instance declaration.
409 -- Return the instance tycon and its type instance. For example, if we have
411 -- lookupFamInst 'T' '[Int]' yields (':R42T', 'Int')
413 -- then we have a coercion (ie, type instance of family instance coercion)
415 -- :Co:R42T Int :: T [Int] ~ :R42T Int
417 -- which implies that :R42T was declared as 'data instance T [a]'.
419 lookupFamInst :: TyCon -> [Type] -> VM (TyCon, [Type])
420 lookupFamInst tycon tys
421 = ASSERT( isOpenTyCon tycon )
422 do { instEnv <- getFamInstEnv
423 ; case lookupFamInstEnv instEnv tycon tys of
424 [(fam_inst, rep_tys)] -> return (famInstTyCon fam_inst, rep_tys)
426 pprPanic "VectMonad.lookupFamInst: not found: "
427 (ppr $ mkTyConApp tycon tys)
430 initV :: HscEnv -> ModGuts -> VectInfo -> VM a -> IO (Maybe (VectInfo, a))
431 initV hsc_env guts info p
433 Just r <- initDs hsc_env (mg_module guts)
442 builtins <- initBuiltins
443 builtin_tycons <- initBuiltinTyCons
444 builtin_pas <- initBuiltinPAs
446 eps <- ioToIOEnv $ hscEPS hsc_env
447 let famInstEnvs = (eps_fam_inst_env eps, mg_fam_inst_env guts)
448 instEnvs = (eps_inst_env eps, mg_inst_env guts)
450 let genv = extendTyConsEnv builtin_tycons
451 . extendPAFunsEnv builtin_pas
452 $ initGlobalEnv info instEnvs famInstEnvs
454 r <- runVM p builtins genv emptyLocalEnv
456 Yes genv _ x -> return $ Just (new_info genv, x)
459 new_info genv = updVectInfo genv (mg_types guts) info