module Vectorise.Generic.Description
( CompRepr(..)
, ProdRepr(..)
, ConRepr(..)
, SumRepr(..)
, tyConRepr
, sumReprType
, compOrigType
)
where
import GhcPrelude
import Vectorise.Utils
import Vectorise.Monad
import Vectorise.Builtins
import CoreSyn
import DataCon
import TyCon
import Type
import Control.Monad
import Outputable
data SumRepr
=
EmptySum
| UnarySum ConRepr
| Sum {
repr_sum_tc :: TyCon
, repr_psum_tc :: TyCon
, repr_psums_tc :: TyCon
, repr_sel_ty :: Type
, repr_sels_ty :: Type
, repr_selsLength_v :: CoreExpr
, repr_con_tys :: [Type]
, repr_cons :: [ConRepr]
}
data ConRepr
= ConRepr
{ repr_dc :: DataCon
, repr_prod :: ProdRepr
}
data ProdRepr
=
EmptyProd
| UnaryProd CompRepr
| Prod {
repr_tup_tc :: TyCon
, repr_ptup_tc :: TyCon
, repr_ptups_tc :: TyCon
, repr_comp_tys :: [Type]
, repr_comps :: [CompRepr]
}
data CompRepr
= Keep Type
CoreExpr
| Wrap Type
tyConRepr :: TyCon -> VM SumRepr
tyConRepr tc
= sum_repr (tyConDataCons tc)
where
sum_repr :: [DataCon] -> VM SumRepr
sum_repr [] = return EmptySum
sum_repr [con] = liftM UnarySum (con_repr con)
sum_repr cons
= do let arity = length cons
rs <- mapM con_repr cons
tys <- mapM conReprType rs
sum_tc <- builtin (sumTyCon arity)
psum_tc <- pdataReprTyConExact sum_tc
psums_tc <- pdatasReprTyConExact sum_tc
sel_ty <- builtin (selTy arity)
sels_ty <- builtin (selsTy arity)
selsLength_v <- builtin (selsLength arity)
return $ Sum
{ repr_sum_tc = sum_tc
, repr_psum_tc = psum_tc
, repr_psums_tc = psums_tc
, repr_sel_ty = sel_ty
, repr_sels_ty = sels_ty
, repr_selsLength_v = selsLength_v
, repr_con_tys = tys
, repr_cons = rs
}
con_repr con = liftM (ConRepr con) (prod_repr (dataConRepArgTys con))
prod_repr :: [Type] -> VM ProdRepr
prod_repr [] = return EmptyProd
prod_repr [ty] = liftM UnaryProd (comp_repr ty)
prod_repr tys
= do let arity = length tys
rs <- mapM comp_repr tys
tys' <- mapM compReprType rs
tup_tc <- builtin (prodTyCon arity)
ptup_tc <- pdataReprTyConExact tup_tc
ptups_tc <- pdatasReprTyConExact tup_tc
return $ Prod
{ repr_tup_tc = tup_tc
, repr_ptup_tc = ptup_tc
, repr_ptups_tc = ptups_tc
, repr_comp_tys = tys'
, repr_comps = rs
}
comp_repr ty = liftM (Keep ty) (prDictOfReprType ty)
`orElseV` return (Wrap ty)
sumReprType :: SumRepr -> VM Type
sumReprType EmptySum = voidType
sumReprType (UnarySum r) = conReprType r
sumReprType (Sum { repr_sum_tc = sum_tc, repr_con_tys = tys })
= return $ mkTyConApp sum_tc tys
conReprType :: ConRepr -> VM Type
conReprType (ConRepr _ r) = prodReprType r
prodReprType :: ProdRepr -> VM Type
prodReprType EmptyProd = voidType
prodReprType (UnaryProd r) = compReprType r
prodReprType (Prod { repr_tup_tc = tup_tc, repr_comp_tys = tys })
= return $ mkTyConApp tup_tc tys
compReprType :: CompRepr -> VM Type
compReprType (Keep ty _) = return ty
compReprType (Wrap ty) = mkWrapType ty
compOrigType :: CompRepr -> Type
compOrigType (Keep ty _) = ty
compOrigType (Wrap ty) = ty
instance Outputable SumRepr where
ppr ss
= case ss of
EmptySum
-> text "EmptySum"
UnarySum con
-> sep [text "UnarySum", ppr con]
Sum sumtc psumtc psumstc selty selsty selsLength contys cons
-> text "Sum" $+$ braces (nest 4
$ sep [ text "repr_sum_tc = " <> ppr sumtc
, text "repr_psum_tc = " <> ppr psumtc
, text "repr_psums_tc = " <> ppr psumstc
, text "repr_sel_ty = " <> ppr selty
, text "repr_sels_ty = " <> ppr selsty
, text "repr_selsLength_v = " <> ppr selsLength
, text "repr_con_tys = " <> ppr contys
, text "repr_cons = " <> ppr cons])
instance Outputable ConRepr where
ppr (ConRepr dc pr)
= text "ConRepr" $+$ braces (nest 4
$ sep [ text "repr_dc = " <> ppr dc
, text "repr_prod = " <> ppr pr])
instance Outputable ProdRepr where
ppr ss
= case ss of
EmptyProd
-> text "EmptyProd"
UnaryProd cr
-> sep [text "UnaryProd", ppr cr]
Prod tuptcs ptuptcs ptupstcs comptys comps
-> sep [text "Prod", ppr tuptcs, ppr ptuptcs, ppr ptupstcs, ppr comptys, ppr comps]
instance Outputable CompRepr where
ppr ss
= case ss of
Keep t ce
-> text "Keep" $+$ sep [ppr t, ppr ce]
Wrap t
-> sep [text "Wrap", ppr t]