-vectTopRhs :: CoreExpr -> VM CoreExpr
-vectTopRhs = liftM fst . closedV . vectPolyExpr (panic "Empty lifting context") . freeVars
-
--- ----------------------------------------------------------------------------
--- Bindings
-
-vectBndr :: Var -> VM VVar
-vectBndr v
- = do
- vty <- vectType (idType v)
- lty <- mkPArrayType vty
- let vv = v `Id.setIdType` vty
- lv = v `Id.setIdType` lty
- updLEnv (mapTo vv lv)
- return (vv, lv)
- where
- mapTo vv lv env = env { local_vars = extendVarEnv (local_vars env) v (vv, lv) }
-
-vectBndrIn :: Var -> VM a -> VM (VVar, a)
-vectBndrIn v p
- = localV
- $ do
- vv <- vectBndr v
- x <- p
- return (vv, x)
-
-vectBndrsIn :: [Var] -> VM a -> VM ([VVar], a)
-vectBndrsIn vs p
- = localV
- $ do
- vvs <- mapM vectBndr vs
- x <- p
- return (vvs, x)
-
--- ----------------------------------------------------------------------------
--- Expressions
-
-capply :: VExpr -> VExpr -> VM VExpr
-capply (vfn, lfn) (varg, larg)
- = do
- apply <- builtin applyClosureVar
- applyP <- builtin applyClosurePVar
- return (mkApps (Var apply) [Type arg_ty, Type res_ty, vfn, varg],
- mkApps (Var applyP) [Type arg_ty, Type res_ty, lfn, larg])
- where
- fn_ty = exprType vfn
- (arg_ty, res_ty) = splitClosureTy fn_ty
-
-vectVar :: Var -> Var -> VM VExpr
-vectVar lc v
- = do
- r <- lookupVar v
- case r of
- Local (vv,lv) -> return (Var vv, Var lv)
- Global vv -> do
- let vexpr = Var vv
- lexpr <- replicatePA vexpr (Var lc)
- return (vexpr, lexpr)
-
-vectPolyVar :: Var -> Var -> [Type] -> VM VExpr
-vectPolyVar lc v tys
- = do
- vtys <- mapM vectType tys
- r <- lookupVar v
- case r of
- Local (vv, lv) -> liftM2 (,) (polyApply (Var vv) vtys)
- (polyApply (Var lv) vtys)
- Global poly -> do
- vexpr <- polyApply (Var poly) vtys
- lexpr <- replicatePA vexpr (Var lc)
- return (vexpr, lexpr)
-
-vectPolyExpr :: Var -> CoreExprWithFVs -> VM VExpr
-vectPolyExpr lc expr
- = polyAbstract tvs $ \abstract ->
- -- FIXME: shadowing (tvs in lc)
- do
- mono' <- vectExpr lc mono
- return $ mapVect abstract mono'
- where
- (tvs, mono) = collectAnnTypeBinders expr
-
-vectExpr :: Var -> CoreExprWithFVs -> VM VExpr
-vectExpr lc (_, AnnType ty)
- = do
- vty <- vectType ty
- return (Type vty, Type vty)
-
-vectExpr lc (_, AnnVar v) = vectVar lc v
-
-vectExpr lc (_, AnnLit lit)
- = do
- let vexpr = Lit lit
- lexpr <- replicatePA vexpr (Var lc)
- return (vexpr, lexpr)
-
-vectExpr lc (_, AnnNote note expr)
- = do
- (vexpr, lexpr) <- vectExpr lc expr
- return (Note note vexpr, Note note lexpr)
-
-vectExpr lc e@(_, AnnApp _ arg)
- | isAnnTypeArg arg
- = vectTyAppExpr lc fn tys
- where
- (fn, tys) = collectAnnTypeArgs e
-
-vectExpr lc (_, AnnApp fn arg)
- = do
- fn' <- vectExpr lc fn
- arg' <- vectExpr lc arg
- capply fn' arg'
-
-vectExpr lc (_, AnnCase expr bndr ty alts)
- = panic "vectExpr: case"
-
-vectExpr lc (_, AnnLet (AnnNonRec bndr rhs) body)
- = do
- (vrhs, lrhs) <- vectPolyExpr lc rhs
- ((vbndr, lbndr), (vbody, lbody)) <- vectBndrIn bndr (vectExpr lc body)
- return (Let (NonRec vbndr vrhs) vbody,
- Let (NonRec lbndr lrhs) lbody)
-
-vectExpr lc (_, AnnLet (AnnRec prs) body)
- = do
- (bndrs, (vrhss, vbody, lrhss, lbody)) <- vectBndrsIn bndrs vect
- let (vbndrs, lbndrs) = unzip bndrs
- return (Let (Rec (zip vbndrs vrhss)) vbody,
- Let (Rec (zip lbndrs lrhss)) lbody)
- where
- (bndrs, rhss) = unzip prs
-
- vect = do
- (vrhss, lrhss) <- mapAndUnzipM (vectExpr lc) rhss
- (vbody, lbody) <- vectPolyExpr lc body
- return (vrhss, vbody, lrhss, lbody)
-
-vectExpr lc e@(_, AnnLam bndr body)
- | isTyVar bndr = pprPanic "vectExpr" (ppr $ deAnnotate e)
-
-vectExpr lc (fvs, AnnLam bndr body)
- = do
- tyvars <- localTyVars
- info <- mkCEnvInfo fvs bndr body
- (poly_vfn, poly_lfn) <- mkClosureFns info tyvars bndr body
-
- vfn_var <- hoistExpr FSLIT("vfn") poly_vfn
- lfn_var <- hoistExpr FSLIT("lfn") poly_lfn
-
- let (venv, lenv) = mkClosureEnvs info (Var lc)
-
- let env_ty = cenv_vty info
-
- pa_dict <- paDictOfType env_ty
-
- arg_ty <- vectType (varType bndr)
- res_ty <- vectType (exprType $ deAnnotate body)
-
- -- FIXME: move the functions to the top level
- mono_vfn <- polyApply (Var vfn_var) (mkTyVarTys tyvars)
- mono_lfn <- polyApply (Var lfn_var) (mkTyVarTys tyvars)
-
- mk_clo <- builtin mkClosureVar
- mk_cloP <- builtin mkClosurePVar
-
- let vclo = Var mk_clo `mkTyApps` [arg_ty, res_ty, env_ty]
- `mkApps` [pa_dict, mono_vfn, mono_lfn, venv]
-
- lclo = Var mk_cloP `mkTyApps` [arg_ty, res_ty, env_ty]
- `mkApps` [pa_dict, mono_vfn, mono_lfn, lenv]
-
- return (vclo, lclo)
-
-data CEnvInfo = CEnvInfo {
- cenv_vars :: [Var]
- , cenv_values :: [(CoreExpr, CoreExpr)]
- , cenv_vty :: Type
- , cenv_lty :: Type
- , cenv_repr_tycon :: TyCon
- , cenv_repr_tyargs :: [Type]
- , cenv_repr_datacon :: DataCon
- }
-
-mkCEnvInfo :: VarSet -> Var -> CoreExprWithFVs -> VM CEnvInfo
-mkCEnvInfo fvs arg body
- = do
- locals <- readLEnv local_vars
- let
- (vars, vals) = unzip
- [(var, (Var v, Var v')) | var <- varSetElems fvs
- , Just (v,v') <- [lookupVarEnv locals var]]
- vtys <- mapM (vectType . varType) vars
-
- (vty, repr_tycon, repr_tyargs, repr_datacon) <- mk_env_ty vtys
- lty <- mkPArrayType vty