module Vectorise.Env (
Scope(..),
LocalEnv(..),
emptyLocalEnv,
GlobalEnv(..),
initGlobalEnv,
extendImportedVarsEnv,
extendFamEnv,
setPAFunsEnv,
setPRFunsEnv,
modVectInfo
) where
import HscTypes
import InstEnv
import FamInstEnv
import CoreSyn
import Type
import Class
import TyCon
import DataCon
import VarEnv
import VarSet
import Var
import NameSet
import Name
import NameEnv
import FastString
import Data.Maybe
data Scope a b
= Global a
| Local b
data LocalEnv
= LocalEnv
{ local_vars :: VarEnv (Var, Var)
, local_tyvars :: [TyVar]
, local_tyvar_pa :: VarEnv CoreExpr
, local_bind_name :: FastString
}
emptyLocalEnv :: LocalEnv
emptyLocalEnv = LocalEnv
{ local_vars = emptyVarEnv
, local_tyvars = []
, local_tyvar_pa = emptyVarEnv
, local_bind_name = fsLit "fn"
}
data GlobalEnv
= GlobalEnv
{ global_vect_avoid :: Bool
, global_vars :: VarEnv Var
, global_parallel_vars :: VarSet
, global_vect_decls :: VarEnv (Maybe (Type, CoreExpr))
, global_tycons :: NameEnv TyCon
, global_parallel_tycons :: NameSet
, global_datacons :: NameEnv DataCon
, global_pa_funs :: NameEnv Var
, global_pr_funs :: NameEnv Var
, global_inst_env :: InstEnvs
, global_fam_inst_env :: FamInstEnvs
, global_bindings :: [(Var, CoreExpr)]
}
initGlobalEnv :: Bool
-> VectInfo
-> [CoreVect]
-> InstEnvs
-> FamInstEnvs
-> GlobalEnv
initGlobalEnv vectAvoid info vectDecls instEnvs famInstEnvs
= GlobalEnv
{ global_vect_avoid = vectAvoid
, global_vars = mapVarEnv snd $ vectInfoVar info
, global_vect_decls = mkVarEnv vects
, global_parallel_vars = vectInfoParallelVars info
, global_parallel_tycons = vectInfoParallelTyCons info
, global_tycons = mapNameEnv snd $ vectInfoTyCon info
, global_datacons = mapNameEnv snd $ vectInfoDataCon info
, global_pa_funs = emptyNameEnv
, global_pr_funs = emptyNameEnv
, global_inst_env = instEnvs
, global_fam_inst_env = famInstEnvs
, global_bindings = []
}
where
vects = [(var, Just (ty, exp)) | Vect var exp@(Var rhs_var) <- vectDecls
, let ty = varType rhs_var] ++
[(var, Nothing) | NoVect var <- vectDecls]
extendImportedVarsEnv :: [(Var, Var)] -> GlobalEnv -> GlobalEnv
extendImportedVarsEnv ps genv
= genv { global_vars = extendVarEnvList (global_vars genv) ps }
extendFamEnv :: [FamInst] -> GlobalEnv -> GlobalEnv
extendFamEnv new genv
= genv { global_fam_inst_env = (g_fam_inst, extendFamInstEnvList l_fam_inst new) }
where (g_fam_inst, l_fam_inst) = global_fam_inst_env genv
setPAFunsEnv :: [(Name, Var)] -> GlobalEnv -> GlobalEnv
setPAFunsEnv ps genv = genv { global_pa_funs = mkNameEnv ps }
setPRFunsEnv :: [(Name, Var)] -> GlobalEnv -> GlobalEnv
setPRFunsEnv ps genv = genv { global_pr_funs = mkNameEnv ps }
modVectInfo :: GlobalEnv -> [Id] -> [TyCon] -> [CoreVect]-> VectInfo -> VectInfo
modVectInfo env mg_ids mg_tyCons vectDecls info
= info
{ vectInfoVar = mk_env ids (global_vars env)
, vectInfoTyCon = mk_env tyCons (global_tycons env)
, vectInfoDataCon = mk_env dataCons (global_datacons env)
, vectInfoParallelVars = (global_parallel_vars env `minusVarSet` vectInfoParallelVars info)
`intersectVarSet` (mkVarSet ids)
, vectInfoParallelTyCons = global_parallel_tycons env `minusNameSet` vectInfoParallelTyCons info
}
where
vectIds = [id | Vect id _ <- vectDecls] ++
[id | VectInst id <- vectDecls]
vectTypeTyCons = [tycon | VectType _ tycon _ <- vectDecls] ++
[tycon | VectClass tycon <- vectDecls]
vectDataCons = concatMap tyConDataCons vectTypeTyCons
ids = mg_ids ++ vectIds ++ dataConIds ++ selIds
tyCons = mg_tyCons ++ vectTypeTyCons
dataCons = concatMap tyConDataCons mg_tyCons ++ vectDataCons
dataConIds = map dataConWorkId dataCons
selIds = concat [ classAllSelIds cls
| tycon <- tyCons
, cls <- maybeToList . tyConClass_maybe $ tycon]
mk_env decls inspectedEnv
= mkNameEnv [(name, (decl, to))
| decl <- decls
, let name = getName decl
, Just to <- [lookupNameEnv inspectedEnv name]]