+{-# OPTIONS -w #-}
+-- The above warning supression flag is a temporary kludge.
+-- While working on this module you are encouraged to remove it and fix
+-- any warnings in the module. See
+-- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
+-- for details
+
module VectUtils (
collectAnnTypeBinders, collectAnnTypeArgs, isAnnTypeArg,
collectAnnValBinders,
- splitClosureTy,
- mkPADictType, mkPArrayType,
- paDictArgType, paDictOfType,
- paMethod, lengthPA, replicatePA, emptyPA,
+ dataConTagZ, mkDataConTag, mkDataConTagLit,
+
+ newLocalVVar,
+
+ mkBuiltinCo,
+ mkPADictType, mkPArrayType, mkPReprType,
+
+ parrayReprTyCon, parrayReprDataCon, mkVScrut,
+ prDFunOfTyCon,
+ paDictArgType, paDictOfType, paDFunType,
+ paMethod, mkPR, lengthPA, replicatePA, emptyPA, packPA, combinePA, liftPA,
polyAbstract, polyApply, polyVApply,
- lookupPArrayFamInst,
- hoistExpr, hoistPolyVExpr, takeHoisted,
- buildClosure, buildClosures
+ hoistBinding, hoistExpr, hoistPolyVExpr, takeHoisted,
+ buildClosure, buildClosures,
+ mkClosureApp
) where
#include "HsVersions.h"
import DsUtils
import CoreSyn
import CoreUtils
+import Coercion
import Type
import TypeRep
import TyCon
-import DataCon ( dataConWrapId )
+import DataCon
import Var
import Id ( mkWildId )
import MkId ( unwrapFamInstScrut )
+import Name ( Name )
import PrelNames
import TysWiredIn
+import TysPrim ( intPrimTy )
import BasicTypes ( Boxity(..) )
+import Literal ( Literal, mkMachInt )
import Outputable
import FastString
-import Control.Monad ( liftM, zipWithM_ )
+import Data.List ( zipWith4 )
+import Control.Monad ( liftM, liftM2, zipWithM_ )
collectAnnTypeArgs :: AnnExpr b ann -> (AnnExpr b ann, [Type])
collectAnnTypeArgs expr = go expr []
isAnnTypeArg (_, AnnType t) = True
isAnnTypeArg _ = False
-isClosureTyCon :: TyCon -> Bool
-isClosureTyCon tc = tyConName tc == closureTyConName
+dataConTagZ :: DataCon -> Int
+dataConTagZ con = dataConTag con - fIRST_TAG
-splitClosureTy :: Type -> (Type, Type)
-splitClosureTy ty
- | Just (tc, [arg_ty, res_ty]) <- splitTyConApp_maybe ty
- , isClosureTyCon tc
- = (arg_ty, res_ty)
+mkDataConTagLit :: DataCon -> Literal
+mkDataConTagLit = mkMachInt . toInteger . dataConTagZ
- | otherwise = pprPanic "splitClosureTy" (ppr ty)
+mkDataConTag :: DataCon -> CoreExpr
+mkDataConTag = mkIntLitInt . dataConTagZ
-isPArrayTyCon :: TyCon -> Bool
-isPArrayTyCon tc = tyConName tc == parrayTyConName
+splitPrimTyCon :: Type -> Maybe TyCon
+splitPrimTyCon ty
+ | Just (tycon, []) <- splitTyConApp_maybe ty
+ , isPrimTyCon tycon
+ = Just tycon
-splitPArrayTy :: Type -> Type
-splitPArrayTy ty
- | Just (tc, [arg_ty]) <- splitTyConApp_maybe ty
- , isPArrayTyCon tc
- = arg_ty
+ | otherwise = Nothing
- | otherwise = pprPanic "splitPArrayTy" (ppr ty)
+mkBuiltinTyConApp :: (Builtins -> TyCon) -> [Type] -> VM Type
+mkBuiltinTyConApp get_tc tys
+ = do
+ tc <- builtin get_tc
+ return $ mkTyConApp tc tys
-mkClosureType :: Type -> Type -> VM Type
-mkClosureType arg_ty res_ty
+mkBuiltinTyConApps :: (Builtins -> TyCon) -> [Type] -> Type -> VM Type
+mkBuiltinTyConApps get_tc tys ty
= do
- tc <- builtin closureTyCon
- return $ mkTyConApp tc [arg_ty, res_ty]
+ tc <- builtin get_tc
+ return $ foldr (mk tc) ty tys
+ where
+ mk tc ty1 ty2 = mkTyConApp tc [ty1,ty2]
-mkClosureTypes :: [Type] -> Type -> VM Type
-mkClosureTypes arg_tys res_ty
+mkBuiltinTyConApps1 :: (Builtins -> TyCon) -> Type -> [Type] -> VM Type
+mkBuiltinTyConApps1 get_tc dft [] = return dft
+mkBuiltinTyConApps1 get_tc dft tys
= do
- tc <- builtin closureTyCon
- return $ foldr (mk tc) res_ty arg_tys
+ tc <- builtin get_tc
+ case tys of
+ [] -> pprPanic "mkBuiltinTyConApps1" (ppr tc)
+ _ -> return $ foldr1 (mk tc) tys
where
- mk tc arg_ty res_ty = mkTyConApp tc [arg_ty, res_ty]
+ mk tc ty1 ty2 = mkTyConApp tc [ty1,ty2]
+
+mkClosureType :: Type -> Type -> VM Type
+mkClosureType arg_ty res_ty = mkBuiltinTyConApp closureTyCon [arg_ty, res_ty]
+
+mkClosureTypes :: [Type] -> Type -> VM Type
+mkClosureTypes = mkBuiltinTyConApps closureTyCon
+
+mkPReprType :: Type -> VM Type
+mkPReprType ty = mkBuiltinTyConApp preprTyCon [ty]
mkPADictType :: Type -> VM Type
-mkPADictType ty
- = do
- tc <- builtin paDictTyCon
- return $ TyConApp tc [ty]
+mkPADictType ty = mkBuiltinTyConApp paTyCon [ty]
mkPArrayType :: Type -> VM Type
mkPArrayType ty
+ | Just tycon <- splitPrimTyCon ty
= do
- tc <- builtin parrayTyCon
- return $ TyConApp tc [ty]
+ arr <- traceMaybeV "mkPArrayType" (ppr tycon)
+ $ lookupPrimPArray tycon
+ return $ mkTyConApp arr []
+mkPArrayType ty = mkBuiltinTyConApp parrayTyCon [ty]
+
+mkBuiltinCo :: (Builtins -> TyCon) -> VM Coercion
+mkBuiltinCo get_tc
+ = do
+ tc <- builtin get_tc
+ return $ mkTyConApp tc []
+
+parrayReprTyCon :: Type -> VM (TyCon, [Type])
+parrayReprTyCon ty = builtin parrayTyCon >>= (`lookupFamInst` [ty])
+
+parrayReprDataCon :: Type -> VM (DataCon, [Type])
+parrayReprDataCon ty
+ = do
+ (tc, arg_tys) <- parrayReprTyCon ty
+ let [dc] = tyConDataCons tc
+ return (dc, arg_tys)
+
+mkVScrut :: VExpr -> VM (VExpr, TyCon, [Type])
+mkVScrut (ve, le)
+ = do
+ (tc, arg_tys) <- parrayReprTyCon (exprType ve)
+ return ((ve, unwrapFamInstScrut tc arg_tys le), tc, arg_tys)
+
+prDFunOfTyCon :: TyCon -> VM CoreExpr
+prDFunOfTyCon tycon
+ = liftM Var (traceMaybeV "prDictOfTyCon" (ppr tycon) (lookupTyConPR tycon))
paDictArgType :: TyVar -> VM (Maybe Type)
paDictArgType tv = go (TyVarTy tv) (tyVarKind tv)
paDFunApply dfun ty_args
paDictOfTyApp (TyConApp tc _) ty_args
= do
- pa_class <- builtin paClass
- (dfun, ty_args') <- lookupInst pa_class [TyConApp tc ty_args]
- paDFunApply (Var dfun) ty_args'
+ dfun <- traceMaybeV "paDictOfTyApp" (ppr tc) (lookupTyConPA tc)
+ paDFunApply (Var dfun) ty_args
paDictOfTyApp ty ty_args = pprPanic "paDictOfTyApp" (ppr ty)
+paDFunType :: TyCon -> VM Type
+paDFunType tc
+ = do
+ margs <- mapM paDictArgType tvs
+ res <- mkPADictType (mkTyConApp tc arg_tys)
+ return . mkForAllTys tvs
+ $ mkFunTys [arg | Just arg <- margs] res
+ where
+ tvs = tyConTyVars tc
+ arg_tys = mkTyVarTys tvs
+
paDFunApply :: CoreExpr -> [Type] -> VM CoreExpr
paDFunApply dfun tys
= do
dicts <- mapM paDictOfType tys
return $ mkApps (mkTyApps dfun tys) dicts
-paMethod :: (Builtins -> Var) -> Type -> VM CoreExpr
-paMethod method ty
+type PAMethod = (Builtins -> Var, String)
+
+pa_length = (lengthPAVar, "lengthPA")
+pa_replicate = (replicatePAVar, "replicatePA")
+pa_empty = (emptyPAVar, "emptyPA")
+pa_pack = (packPAVar, "packPA")
+
+paMethod :: PAMethod -> Type -> VM CoreExpr
+paMethod (method, name) ty
+ | Just tycon <- splitPrimTyCon ty
+ = do
+ fn <- traceMaybeV "paMethod" (ppr tycon <+> text name)
+ $ lookupPrimMethod tycon name
+ return (Var fn)
+
+paMethod (method, name) ty
= do
fn <- builtin method
dict <- paDictOfType ty
return $ mkApps (Var fn) [Type ty, dict]
-lengthPA :: CoreExpr -> VM CoreExpr
-lengthPA x = liftM (`App` x) (paMethod lengthPAVar ty)
- where
- ty = splitPArrayTy (exprType x)
+mkPR :: Type -> VM CoreExpr
+mkPR ty
+ = do
+ fn <- builtin mkPRVar
+ dict <- paDictOfType ty
+ return $ mkApps (Var fn) [Type ty, dict]
+
+lengthPA :: Type -> CoreExpr -> VM CoreExpr
+lengthPA ty x = liftM (`App` x) (paMethod pa_length ty)
replicatePA :: CoreExpr -> CoreExpr -> VM CoreExpr
replicatePA len x = liftM (`mkApps` [len,x])
- (paMethod replicatePAVar (exprType x))
+ (paMethod pa_replicate (exprType x))
emptyPA :: Type -> VM CoreExpr
-emptyPA = paMethod emptyPAVar
+emptyPA = paMethod pa_empty
+
+packPA :: Type -> CoreExpr -> CoreExpr -> CoreExpr -> VM CoreExpr
+packPA ty xs len sel = liftM (`mkApps` [xs, len, sel])
+ (paMethod pa_pack ty)
+
+combinePA :: Type -> CoreExpr -> CoreExpr -> CoreExpr -> [CoreExpr]
+ -> VM CoreExpr
+combinePA ty len sel is xs
+ = liftM (`mkApps` (len : sel : is : xs))
+ (paMethod (combinePAVar n, "combine" ++ show n ++ "PA") ty)
+ where
+ n = length xs
+
+liftPA :: CoreExpr -> VM CoreExpr
+liftPA x
+ = do
+ lc <- builtin liftingContext
+ replicatePA (Var lc) x
newLocalVVar :: FastString -> Type -> VM VVar
newLocalVVar fs vty
dicts <- mapM paDictOfType tys
return $ mapVect (\e -> e `mkTyApps` tys `mkApps` dicts) expr
-lookupPArrayFamInst :: Type -> VM (TyCon, [Type])
-lookupPArrayFamInst ty = builtin parrayTyCon >>= (`lookupFamInst` [ty])
+hoistBinding :: Var -> CoreExpr -> VM ()
+hoistBinding v e = updGEnv $ \env ->
+ env { global_bindings = (v,e) : global_bindings env }
hoistExpr :: FastString -> CoreExpr -> VM Var
hoistExpr fs expr
= do
var <- newLocalVar fs (exprType expr)
- updGEnv $ \env ->
- env { global_bindings = (var, expr) : global_bindings env }
+ hoistBinding var expr
return var
-hoistVExpr :: FastString -> VExpr -> VM VVar
-hoistVExpr fs (ve, le)
+hoistVExpr :: VExpr -> VM VVar
+hoistVExpr (ve, le)
= do
+ fs <- getBindName
vv <- hoistExpr ('v' `consFS` fs) ve
lv <- hoistExpr ('l' `consFS` fs) le
return (vv, lv)
-hoistPolyVExpr :: FastString -> [TyVar] -> VM VExpr -> VM VExpr
-hoistPolyVExpr fs tvs p
+hoistPolyVExpr :: [TyVar] -> VM VExpr -> VM VExpr
+hoistPolyVExpr tvs p
= do
expr <- closedV . polyAbstract tvs $ \abstract ->
liftM (mapVect abstract) p
- fn <- hoistVExpr fs expr
+ fn <- hoistVExpr expr
polyVApply (vVar fn) (mkTyVarTys tvs)
takeHoisted :: VM [(Var, CoreExpr)]
setGEnv $ env { global_bindings = [] }
return $ global_bindings env
+boxExpr :: Type -> VExpr -> VM VExpr
+boxExpr ty (vexpr, lexpr)
+ | Just (tycon, []) <- splitTyConApp_maybe ty
+ , isUnLiftedTyCon tycon
+ = do
+ r <- lookupBoxedTyCon tycon
+ case r of
+ Just tycon' -> let [dc] = tyConDataCons tycon'
+ in
+ return (mkConApp dc [vexpr], lexpr)
+ Nothing -> return (vexpr, lexpr)
+
mkClosure :: Type -> Type -> Type -> VExpr -> VExpr -> VM VExpr
mkClosure arg_ty res_ty env_ty (vfn,lfn) (venv,lenv)
return (Var mkv `mkTyApps` [arg_ty, res_ty, env_ty] `mkApps` [dict, vfn, lfn, venv],
Var mkl `mkTyApps` [arg_ty, res_ty, env_ty] `mkApps` [dict, vfn, lfn, lenv])
-buildClosures :: [TyVar] -> Var -> [VVar] -> [Type] -> Type -> VM VExpr -> VM VExpr
-buildClosures tvs lc vars [arg_ty] res_ty mk_body
- = buildClosure tvs lc vars arg_ty res_ty mk_body
-buildClosures tvs lc vars (arg_ty : arg_tys) res_ty mk_body
+mkClosureApp :: Type -> Type -> VExpr -> VExpr -> VM VExpr
+mkClosureApp arg_ty res_ty (vclo, lclo) (varg, larg)
+ = do
+ vapply <- builtin applyClosureVar
+ lapply <- builtin applyClosurePVar
+ return (Var vapply `mkTyApps` [arg_ty, res_ty] `mkApps` [vclo, varg],
+ Var lapply `mkTyApps` [arg_ty, res_ty] `mkApps` [lclo, larg])
+
+buildClosures :: [TyVar] -> [VVar] -> [Type] -> Type -> VM VExpr -> VM VExpr
+buildClosures tvs vars [] res_ty mk_body
+ = mk_body
+buildClosures tvs vars [arg_ty] res_ty mk_body
+ = buildClosure tvs vars arg_ty res_ty mk_body
+buildClosures tvs vars (arg_ty : arg_tys) res_ty mk_body
= do
res_ty' <- mkClosureTypes arg_tys res_ty
arg <- newLocalVVar FSLIT("x") arg_ty
- buildClosure tvs lc vars arg_ty res_ty'
- . hoistPolyVExpr FSLIT("fn") tvs
+ buildClosure tvs vars arg_ty res_ty'
+ . hoistPolyVExpr tvs
$ do
- clo <- buildClosures tvs lc (vars ++ [arg]) arg_tys res_ty mk_body
+ lc <- builtin liftingContext
+ clo <- buildClosures tvs (vars ++ [arg]) arg_tys res_ty mk_body
return $ vLams lc (vars ++ [arg]) clo
-- (clo <x1,...,xn> <f,f^>, aclo (Arr lc xs1 ... xsn) <f,f^>)
-- f = \env v -> case env of <x1,...,xn> -> e x1 ... xn v
-- f^ = \env v -> case env of Arr l xs1 ... xsn -> e^ l x1 ... xn v
--
-buildClosure :: [TyVar] -> Var -> [VVar] -> Type -> Type -> VM VExpr -> VM VExpr
-buildClosure tvs lv vars arg_ty res_ty mk_body
+buildClosure :: [TyVar] -> [VVar] -> Type -> Type -> VM VExpr -> VM VExpr
+buildClosure tvs vars arg_ty res_ty mk_body
= do
- (env_ty, env, bind) <- buildEnv lv vars
+ (env_ty, env, bind) <- buildEnv vars
env_bndr <- newLocalVVar FSLIT("env") env_ty
arg_bndr <- newLocalVVar FSLIT("arg") arg_ty
- fn <- hoistPolyVExpr FSLIT("fn") tvs
+ fn <- hoistPolyVExpr tvs
$ do
+ lc <- builtin liftingContext
body <- mk_body
body' <- bind (vVar env_bndr)
- (vVarApps lv body (vars ++ [arg_bndr]))
+ (vVarApps lc body (vars ++ [arg_bndr]))
return (vLamsWithoutLC [env_bndr, arg_bndr] body')
mkClosure arg_ty res_ty env_ty fn env
-buildEnv :: Var -> [VVar] -> VM (Type, VExpr, VExpr -> VExpr -> VM VExpr)
-buildEnv lv vvs
+buildEnv :: [VVar] -> VM (Type, VExpr, VExpr -> VExpr -> VM VExpr)
+buildEnv vvs
= do
+ lc <- builtin liftingContext
let (ty, venv, vbind) = mkVectEnv tys vs
- (lenv, lbind) <- mkLiftEnv lv tys ls
+ (lenv, lbind) <- mkLiftEnv lc tys ls
return (ty, (venv, lenv),
\(venv,lenv) (vbody,lbody) ->
do
ty = mkCoreTupTy tys
mkLiftEnv :: Var -> [Type] -> [Var] -> VM (CoreExpr, CoreExpr -> CoreExpr -> VM CoreExpr)
-mkLiftEnv lv [ty] [v]
+mkLiftEnv lc [ty] [v]
= return (Var v, \env body ->
do
- len <- lengthPA (Var v)
+ len <- lengthPA ty (Var v)
return . Let (NonRec v env)
- $ Case len lv (exprType body) [(DEFAULT, [], body)])
+ $ Case len lc (exprType body) [(DEFAULT, [], body)])
-- NOTE: this transparently deals with empty environments
-mkLiftEnv lv tys vs
+mkLiftEnv lc tys vs
= do
- (env_tc, env_tyargs) <- lookupPArrayFamInst vty
+ (env_tc, env_tyargs) <- parrayReprTyCon vty
let [env_con] = tyConDataCons env_tc
env = Var (dataConWrapId env_con)
`mkTyApps` env_tyargs
- `mkVarApps` (lv : vs)
+ `mkApps` (Var lc : args)
bind env body = let scrut = unwrapFamInstScrut env_tc env_tyargs env
in
return $ Case scrut (mkWildId (exprType scrut))
(exprType body)
- [(DataAlt env_con, lv : bndrs, body)]
+ [(DataAlt env_con, lc : bndrs, body)]
return (env, bind)
where
vty = mkCoreTupTy tys
+ args | null vs = [Var unitDataConId]
+ | otherwise = map Var vs
+
bndrs | null vs = [mkWildId unitTy]
| otherwise = vs