+{-# 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,
- mkDataConTag,
- splitClosureTy,
+ dataConTagZ, mkDataConTag, mkDataConTagLit,
+
+ newLocalVVar,
mkBuiltinCo,
mkPADictType, mkPArrayType, mkPReprType,
parrayReprTyCon, parrayReprDataCon, mkVScrut,
prDFunOfTyCon,
paDictArgType, paDictOfType, paDFunType,
- paMethod, mkPR, lengthPA, replicatePA, emptyPA, liftPA,
+ paMethod, mkPR, lengthPA, replicatePA, emptyPA, packPA, combinePA, liftPA,
polyAbstract, polyApply, polyVApply,
hoistBinding, hoistExpr, hoistPolyVExpr, takeHoisted,
buildClosure, buildClosures,
import TysWiredIn
import TysPrim ( intPrimTy )
import BasicTypes ( Boxity(..) )
+import Literal ( Literal, mkMachInt )
import Outputable
import FastString
isAnnTypeArg (_, AnnType t) = True
isAnnTypeArg _ = False
-mkDataConTag :: DataCon -> CoreExpr
-mkDataConTag = mkIntLitInt . dataConTag
-
-splitUnTy :: String -> Name -> Type -> Type
-splitUnTy s name ty
- | Just (tc, [ty']) <- splitTyConApp_maybe ty
- , tyConName tc == name
- = ty'
-
- | otherwise = pprPanic s (ppr ty)
+dataConTagZ :: DataCon -> Int
+dataConTagZ con = dataConTag con - fIRST_TAG
-splitBinTy :: String -> Name -> Type -> (Type, Type)
-splitBinTy s name ty
- | Just (tc, [ty1, ty2]) <- splitTyConApp_maybe ty
- , tyConName tc == name
- = (ty1, ty2)
+mkDataConTagLit :: DataCon -> Literal
+mkDataConTagLit = mkMachInt . toInteger . dataConTagZ
- | otherwise = pprPanic s (ppr ty)
-
-splitFixedTyConApp :: TyCon -> Type -> [Type]
-splitFixedTyConApp tc ty
- | Just (tc', tys) <- splitTyConApp_maybe ty
- , tc == tc'
- = tys
-
- | otherwise = pprPanic "splitFixedTyConApp" (ppr tc <+> ppr ty)
-
-splitClosureTy :: Type -> (Type, Type)
-splitClosureTy = splitBinTy "splitClosureTy" closureTyConName
-
-splitPArrayTy :: Type -> Type
-splitPArrayTy = splitUnTy "splitPArrayTy" parrayTyConName
+mkDataConTag :: DataCon -> CoreExpr
+mkDataConTag = mkIntLitInt . dataConTagZ
splitPrimTyCon :: Type -> Maybe TyCon
splitPrimTyCon ty
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
dict <- paDictOfType ty
return $ mkApps (Var fn) [Type ty, dict]
-lengthPA :: CoreExpr -> VM CoreExpr
-lengthPA x = liftM (`App` x) (paMethod pa_length ty)
- where
- ty = splitPArrayTy (exprType x)
+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])
emptyPA :: Type -> VM CoreExpr
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
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)
= do
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])
-mkClosureApp :: VExpr -> VExpr -> VM VExpr
-mkClosureApp (vclo, lclo) (varg, larg)
+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])
- where
- (arg_ty, res_ty) = splitClosureTy (exprType vclo)
buildClosures :: [TyVar] -> [VVar] -> [Type] -> Type -> VM VExpr -> VM VExpr
buildClosures tvs vars [] res_ty mk_body
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 lc (exprType body) [(DEFAULT, [], body)])
env = Var (dataConWrapId env_con)
`mkTyApps` env_tyargs
- `mkVarApps` (lc : vs)
+ `mkApps` (Var lc : args)
bind env body = let scrut = unwrapFamInstScrut env_tc env_tyargs env
in
where
vty = mkCoreTupTy tys
+ args | null vs = [Var unitDataConId]
+ | otherwise = map Var vs
+
bndrs | null vs = [mkWildId unitTy]
| otherwise = vs