8af9f4109bed6b019bdbd89776a467d3a82bc82d
[ghc-hetmet.git] / compiler / vectorise / Vectorise / Type / PADict.hs
1
2 module Vectorise.Type.PADict
3         (buildPADict)
4 where
5 import Vectorise.Monad
6 import Vectorise.Builtins
7 import Vectorise.Type.Repr
8 import Vectorise.Type.PRepr
9 import Vectorise.Type.PRDict
10 import Vectorise.Utils
11
12 import BasicTypes
13 import CoreSyn
14 import CoreUtils
15 import CoreUnfold
16 import TyCon
17 import Type
18 import TypeRep
19 import Id
20 import Var
21 import Name
22 import Outputable
23
24 -- debug                = False
25 -- dtrace s x   = if debug then pprTrace "Vectoris.Type.PADict" s x else x
26
27 -- | Build the PA dictionary for some type and hoist it to top level.
28 --   The PA dictionary holds fns that convert values to and from their vectorised representations.
29 buildPADict
30         :: TyCon        -- ^ tycon of the type being vectorised.
31         -> TyCon        -- ^ tycon of the type used for the vectorised representation.
32         -> TyCon        -- 
33         -> SumRepr      -- ^ representation used for the type being vectorised.
34         -> VM Var       -- ^ name of the top-level dictionary function.
35
36 buildPADict vect_tc prepr_tc arr_tc repr
37  = polyAbstract tvs $ \args ->
38  case args of
39   (_:_) -> pprPanic "Vectorise.Type.PADict.buildPADict" (text "why do we need superclass dicts?")
40   [] -> do
41       -- TODO: I'm forcing args to [] because I'm not sure why we need them.
42       --       class PA has superclass (PR (PRepr a)) but we're not using
43       --       the superclass dictionary to build the PA dictionary.
44
45       -- Get ids for each of the methods in the dictionary.
46       method_ids <- mapM (method args) paMethods
47
48       -- Expression to build the dictionary.
49       pa_dc  <- builtin paDataCon
50       let dict = mkLams (tvs ++ args)
51                $ mkConApp pa_dc
52                $ Type inst_ty : map (method_call args) method_ids
53
54       -- Build the type of the dictionary function.
55       pa_tc             <- builtin paTyCon
56       let Just pa_cls   = tyConClass_maybe pa_tc
57
58       let dfun_ty       = mkForAllTys tvs
59                         $ mkFunTys (map varType args) (PredTy $ ClassP pa_cls [inst_ty])
60
61       -- Set the unfolding for the inliner.
62       raw_dfun <- newExportedVar dfun_name dfun_ty
63       let dfun = raw_dfun `setIdUnfolding`  mkDFunUnfolding dfun_ty (map Var method_ids)
64                           `setInlinePragma` dfunInlinePragma
65
66       -- Add the new binding to the top-level environment.
67       hoistBinding dfun dict
68       return dfun
69   where
70     tvs       = tyConTyVars vect_tc
71     arg_tys   = mkTyVarTys tvs
72     inst_ty   = mkTyConApp vect_tc arg_tys
73
74     dfun_name = mkPADFunOcc (getOccName vect_tc)
75
76     method args (name, build)
77       = localV
78       $ do
79           expr     <- build vect_tc prepr_tc arr_tc repr
80           let body = mkLams (tvs ++ args) expr
81           raw_var  <- newExportedVar (method_name name) (exprType body)
82           let var  = raw_var
83                       `setIdUnfolding` mkInlineUnfolding (Just (length args)) body
84                       `setInlinePragma` alwaysInlinePragma
85           hoistBinding var body
86           return var
87
88     method_call args id = mkApps (Var id) (map Type arg_tys ++ map Var args)
89     method_name name    = mkVarOcc $ occNameString dfun_name ++ ('$' : name)
90
91
92 paMethods :: [(String, TyCon -> TyCon -> TyCon -> SumRepr -> VM CoreExpr)]
93 paMethods = [("dictPRepr",    buildPRDict),
94              ("toPRepr",      buildToPRepr),
95              ("fromPRepr",    buildFromPRepr),
96              ("toArrPRepr",   buildToArrPRepr),
97              ("fromArrPRepr", buildFromArrPRepr)]
98