971fa3e8c1441e2d8b2b1a3d0f5bf2d89010afbd
[ghc.git] / compiler / vectorise / VectBuiltIn.hs
1 {-# OPTIONS -w #-}
2 -- The above warning supression flag is a temporary kludge.
3 -- While working on this module you are encouraged to remove it and fix
4 -- any warnings in the module. See
5 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
6 -- for details
7
8 module VectBuiltIn (
9 Builtins(..), sumTyCon, prodTyCon,
10 initBuiltins, initBuiltinTyCons, initBuiltinPAs, initBuiltinPRs,
11
12 primMethod, primPArray
13 ) where
14
15 #include "HsVersions.h"
16
17 import DsMonad
18 import IfaceEnv ( lookupOrig )
19
20 import Module ( Module )
21 import DataCon ( DataCon )
22 import TyCon ( TyCon, tyConName, tyConDataCons )
23 import Var ( Var )
24 import Id ( mkSysLocal )
25 import Name ( Name, getOccString )
26 import NameEnv
27 import OccName
28
29 import TypeRep ( funTyCon )
30 import Type ( Type )
31 import TysPrim
32 import TysWiredIn ( unitTyCon, tupleTyCon, intTyConName )
33 import Module
34 import BasicTypes ( Boxity(..) )
35
36 import FastString
37 import Outputable
38
39 import Data.Array
40 import Control.Monad ( liftM, zipWithM )
41
42 mAX_NDP_PROD :: Int
43 mAX_NDP_PROD = 3
44
45 mAX_NDP_SUM :: Int
46 mAX_NDP_SUM = 3
47
48 mkNDPModule :: FastString -> Module
49 mkNDPModule m = mkModule ndpPackageId (mkModuleNameFS m)
50
51 nDP_PARRAY = mkNDPModule FSLIT("Data.Array.Parallel.Lifted.PArray")
52 nDP_REPR = mkNDPModule FSLIT("Data.Array.Parallel.Lifted.Repr")
53 nDP_CLOSURE = mkNDPModule FSLIT("Data.Array.Parallel.Lifted.Closure")
54 nDP_PRIM = mkNDPModule FSLIT("Data.Array.Parallel.Lifted.Prim")
55 nDP_INSTANCES = mkNDPModule FSLIT("Data.Array.Parallel.Lifted.Instances")
56
57 data Builtins = Builtins {
58 parrayTyCon :: TyCon
59 , paTyCon :: TyCon
60 , paDataCon :: DataCon
61 , preprTyCon :: TyCon
62 , prTyCon :: TyCon
63 , prDataCon :: DataCon
64 , parrayIntPrimTyCon :: TyCon
65 , parrayBoolPrimTyCon :: TyCon
66 , voidTyCon :: TyCon
67 , wrapTyCon :: TyCon
68 , sumTyCons :: Array Int TyCon
69 , closureTyCon :: TyCon
70 , voidVar :: Var
71 , mkPRVar :: Var
72 , mkClosureVar :: Var
73 , applyClosureVar :: Var
74 , mkClosurePVar :: Var
75 , applyClosurePVar :: Var
76 , replicatePAIntPrimVar :: Var
77 , upToPAIntPrimVar :: Var
78 , selectPAIntPrimVar :: Var
79 , truesPABoolPrimVar :: Var
80 , lengthPAVar :: Var
81 , replicatePAVar :: Var
82 , emptyPAVar :: Var
83 , packPAVar :: Var
84 -- , combinePAVar :: Var
85 , liftingContext :: Var
86 }
87
88 sumTyCon :: Int -> Builtins -> TyCon
89 sumTyCon n bi
90 | n >= 2 && n <= mAX_NDP_SUM = sumTyCons bi ! n
91 | otherwise = pprPanic "sumTyCon" (ppr n)
92
93 prodTyCon :: Int -> Builtins -> TyCon
94 prodTyCon n bi
95 | n == 1 = wrapTyCon bi
96 | n >= 0 && n <= mAX_NDP_PROD = tupleTyCon Boxed n
97 | otherwise = pprPanic "prodTyCon" (ppr n)
98
99 initBuiltins :: DsM Builtins
100 initBuiltins
101 = do
102 parrayTyCon <- externalTyCon nDP_PARRAY FSLIT("PArray")
103 paTyCon <- externalTyCon nDP_PARRAY FSLIT("PA")
104 let [paDataCon] = tyConDataCons paTyCon
105 preprTyCon <- externalTyCon nDP_PARRAY FSLIT("PRepr")
106 prTyCon <- externalTyCon nDP_PARRAY FSLIT("PR")
107 let [prDataCon] = tyConDataCons prTyCon
108 parrayIntPrimTyCon <- externalTyCon nDP_PRIM FSLIT("PArray_Int#")
109 parrayBoolPrimTyCon <- externalTyCon nDP_PRIM FSLIT("PArray_Bool#")
110 closureTyCon <- externalTyCon nDP_CLOSURE FSLIT(":->")
111
112 voidTyCon <- externalTyCon nDP_REPR FSLIT("Void")
113 wrapTyCon <- externalTyCon nDP_REPR FSLIT("Wrap")
114 sum_tcs <- mapM (externalTyCon nDP_REPR)
115 [mkFastString ("Sum" ++ show i) | i <- [2..mAX_NDP_SUM]]
116
117 let sumTyCons = listArray (2, mAX_NDP_SUM) sum_tcs
118
119 voidVar <- externalVar nDP_REPR FSLIT("void")
120 mkPRVar <- externalVar nDP_PARRAY FSLIT("mkPR")
121 mkClosureVar <- externalVar nDP_CLOSURE FSLIT("mkClosure")
122 applyClosureVar <- externalVar nDP_CLOSURE FSLIT("$:")
123 mkClosurePVar <- externalVar nDP_CLOSURE FSLIT("mkClosureP")
124 applyClosurePVar <- externalVar nDP_CLOSURE FSLIT("$:^")
125 replicatePAIntPrimVar <- externalVar nDP_PRIM FSLIT("replicatePA_Int#")
126 upToPAIntPrimVar <- externalVar nDP_PRIM FSLIT("upToPA_Int#")
127 selectPAIntPrimVar <- externalVar nDP_PRIM FSLIT("selectPA_Int#")
128 truesPABoolPrimVar <- externalVar nDP_PRIM FSLIT("truesPA_Bool#")
129 lengthPAVar <- externalVar nDP_PARRAY FSLIT("lengthPA")
130 replicatePAVar <- externalVar nDP_PARRAY FSLIT("replicatePA")
131 emptyPAVar <- externalVar nDP_PARRAY FSLIT("emptyPA")
132 packPAVar <- externalVar nDP_PARRAY FSLIT("packPA")
133 -- combinePAVar <- dsLookupGlobalId combinePAName
134
135 liftingContext <- liftM (\u -> mkSysLocal FSLIT("lc") u intPrimTy)
136 newUnique
137
138 return $ Builtins {
139 parrayTyCon = parrayTyCon
140 , paTyCon = paTyCon
141 , paDataCon = paDataCon
142 , preprTyCon = preprTyCon
143 , prTyCon = prTyCon
144 , prDataCon = prDataCon
145 , parrayIntPrimTyCon = parrayIntPrimTyCon
146 , parrayBoolPrimTyCon = parrayBoolPrimTyCon
147 , voidTyCon = voidTyCon
148 , wrapTyCon = wrapTyCon
149 , sumTyCons = sumTyCons
150 , closureTyCon = closureTyCon
151 , voidVar = voidVar
152 , mkPRVar = mkPRVar
153 , mkClosureVar = mkClosureVar
154 , applyClosureVar = applyClosureVar
155 , mkClosurePVar = mkClosurePVar
156 , applyClosurePVar = applyClosurePVar
157 , replicatePAIntPrimVar = replicatePAIntPrimVar
158 , upToPAIntPrimVar = upToPAIntPrimVar
159 , selectPAIntPrimVar = selectPAIntPrimVar
160 , truesPABoolPrimVar = truesPABoolPrimVar
161 , lengthPAVar = lengthPAVar
162 , replicatePAVar = replicatePAVar
163 , emptyPAVar = emptyPAVar
164 , packPAVar = packPAVar
165 -- , combinePAVar = combinePAVar
166 , liftingContext = liftingContext
167 }
168
169 initBuiltinTyCons :: Builtins -> [(Name, TyCon)]
170 initBuiltinTyCons bi = [(tyConName funTyCon, closureTyCon bi)]
171
172 initBuiltinDicts :: [(Name, Module, FastString)] -> DsM [(Name, Var)]
173 initBuiltinDicts ps
174 = do
175 dicts <- zipWithM externalVar mods fss
176 return $ zip tcs dicts
177 where
178 (tcs, mods, fss) = unzip3 ps
179
180 initBuiltinPAs = initBuiltinDicts . builtinPAs
181
182 builtinPAs :: Builtins -> [(Name, Module, FastString)]
183 builtinPAs bi
184 = [
185 mk (tyConName $ closureTyCon bi) nDP_CLOSURE FSLIT("dPA_Clo")
186 , mk (tyConName $ voidTyCon bi) nDP_REPR FSLIT("dPA_Void")
187 , mk unitTyConName nDP_INSTANCES FSLIT("dPA_Unit")
188
189 , mk intTyConName nDP_INSTANCES FSLIT("dPA_Int")
190 ]
191 ++ tups
192 where
193 mk name mod fs = (name, mod, fs)
194
195 tups = map mk_tup [2..3]
196 mk_tup n = mk (tyConName $ tupleTyCon Boxed n)
197 nDP_INSTANCES
198 (mkFastString $ "dPA_" ++ show n)
199
200 initBuiltinPRs = initBuiltinDicts . builtinPRs
201
202 builtinPRs :: Builtins -> [(Name, Module, FastString)]
203 builtinPRs bi =
204 [
205 mk (tyConName unitTyCon) nDP_REPR FSLIT("dPR_Unit")
206 , mk (tyConName $ voidTyCon bi) nDP_REPR FSLIT("dPR_Void")
207 , mk (tyConName $ wrapTyCon bi) nDP_REPR FSLIT("dPR_Wrap")
208 , mk (tyConName $ closureTyCon bi) nDP_CLOSURE FSLIT("dPR_Clo")
209
210 -- temporary
211 , mk intTyConName nDP_INSTANCES FSLIT("dPR_Int")
212 ]
213
214 ++ map mk_sum [2..mAX_NDP_SUM]
215 ++ map mk_prod [2..mAX_NDP_PROD]
216 where
217 mk name mod fs = (name, mod, fs)
218
219 mk_sum n = (tyConName $ sumTyCon n bi, nDP_REPR,
220 mkFastString ("dPR_Sum" ++ show n))
221
222 mk_prod n = (tyConName $ prodTyCon n bi, nDP_REPR,
223 mkFastString ("dPR_" ++ show n))
224
225 externalVar :: Module -> FastString -> DsM Var
226 externalVar mod fs
227 = dsLookupGlobalId =<< lookupOrig mod (mkVarOccFS fs)
228
229 externalTyCon :: Module -> FastString -> DsM TyCon
230 externalTyCon mod fs
231 = dsLookupTyCon =<< lookupOrig mod (mkOccNameFS tcName fs)
232
233 unitTyConName = tyConName unitTyCon
234
235
236 primMethod :: TyCon -> String -> DsM (Maybe Var)
237 primMethod tycon method
238 | Just suffix <- lookupNameEnv prim_ty_cons (tyConName tycon)
239 = liftM Just
240 $ dsLookupGlobalId =<< lookupOrig nDP_PRIM (mkVarOcc $ method ++ suffix)
241
242 | otherwise = return Nothing
243
244 primPArray :: TyCon -> DsM (Maybe TyCon)
245 primPArray tycon
246 | Just suffix <- lookupNameEnv prim_ty_cons (tyConName tycon)
247 = liftM Just
248 $ dsLookupTyCon =<< lookupOrig nDP_PRIM (mkOccName tcName $ "PArray" ++ suffix)
249
250 | otherwise = return Nothing
251
252 prim_ty_cons = mkNameEnv [mk_prim intPrimTyCon]
253 where
254 mk_prim tycon = (tyConName tycon, '_' : getOccString tycon)