VectBuiltIn.hs 15 KB
Newer Older
1
module VectBuiltIn (
2
3
  Builtins(..), sumTyCon, prodTyCon, uarrTy, intPrimArrayTy,
  combinePAVar,
4
  initBuiltins, initBuiltinVars, initBuiltinTyCons, initBuiltinDataCons,
5
  initBuiltinPAs, initBuiltinPRs,
6
  initBuiltinBoxedTyCons,
7

8
  primMethod, primPArray
9
10
11
12
13
) where

#include "HsVersions.h"

import DsMonad
14
import IfaceEnv        ( lookupOrig )
15

16
import Module
17
import DataCon         ( DataCon, dataConName, dataConWorkId )
18
import TyCon           ( TyCon, tyConName, tyConDataCons )
19
20
import Var             ( Var )
import Id              ( mkSysLocal )
21
22
23
import Name            ( Name, getOccString )
import NameEnv
import OccName
24

25
import TypeRep         ( funTyCon )
26
import Type            ( Type, mkTyConApp )
27
import TysPrim
28
29
import TysWiredIn      ( unitTyCon, unitDataCon,
                         tupleTyCon,
30
                         intTyCon, intTyConName, intTy,
31
                         doubleTyCon, doubleTyConName,
32
                         boolTyCon, boolTyConName, trueDataCon, falseDataCon,
33
                         parrTyConName )
34
import PrelNames       ( gHC_PARR )
35
import BasicTypes      ( Boxity(..) )
36

37
import FastString
38
import Outputable
39

40
import Data.Array
41
import Control.Monad   ( liftM, zipWithM )
42
import Data.List       ( unzip4 )
43

44
mAX_NDP_PROD :: Int
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
45
mAX_NDP_PROD = 5
46
47
48
49

mAX_NDP_SUM :: Int
mAX_NDP_SUM = 3

50
51
52
mAX_NDP_COMBINE :: Int
mAX_NDP_COMBINE = 2

53
54
55
mkNDPModule :: FastString -> Module
mkNDPModule m = mkModule ndpPackageId (mkModuleNameFS m)

56
57
58
nDP_UARR, nDP_PARRAY, nDP_REPR, nDP_CLOSURE, nDP_UNBOXED, nDP_INSTANCES, nDP_COMBINATORS,
    nDP_PRELUDE_PARR, nDP_PRELUDE_INT, nDP_PRELUDE_DOUBLE :: Module

59
nDP_UARR        = mkNDPModule FSLIT("Data.Array.Parallel.Unlifted.Flat.UArr")
60
61
62
nDP_PARRAY      = mkNDPModule FSLIT("Data.Array.Parallel.Lifted.PArray")
nDP_REPR        = mkNDPModule FSLIT("Data.Array.Parallel.Lifted.Repr")
nDP_CLOSURE     = mkNDPModule FSLIT("Data.Array.Parallel.Lifted.Closure")
63
nDP_UNBOXED     = mkNDPModule FSLIT("Data.Array.Parallel.Lifted.Unboxed")
64
nDP_INSTANCES   = mkNDPModule FSLIT("Data.Array.Parallel.Lifted.Instances")
65
66
nDP_COMBINATORS = mkNDPModule FSLIT("Data.Array.Parallel.Lifted.Combinators")

67
68
nDP_PRELUDE_PARR = mkNDPModule FSLIT("Data.Array.Parallel.Prelude.Base.PArr")
nDP_PRELUDE_INT  = mkNDPModule FSLIT("Data.Array.Parallel.Prelude.Base.Int")
69
nDP_PRELUDE_DOUBLE = mkNDPModule FSLIT("Data.Array.Parallel.Prelude.Base.Double")
70

71
72
73
74
75
76
77
data Builtins = Builtins {
                  parrayTyCon      :: TyCon
                , paTyCon          :: TyCon
                , paDataCon        :: DataCon
                , preprTyCon       :: TyCon
                , prTyCon          :: TyCon
                , prDataCon        :: DataCon
78
                , uarrTyCon        :: TyCon
79
                , voidTyCon        :: TyCon
80
                , wrapTyCon        :: TyCon
81
                , enumerationTyCon :: TyCon
82
                , sumTyCons        :: Array Int TyCon
83
                , closureTyCon     :: TyCon
84
                , voidVar          :: Var
85
                , mkPRVar          :: Var
86
87
88
89
                , mkClosureVar     :: Var
                , applyClosureVar  :: Var
                , mkClosurePVar    :: Var
                , applyClosurePVar :: Var
90
91
                , replicatePAIntPrimVar :: Var
                , upToPAIntPrimVar :: Var
92
93
                , selectPAIntPrimVar :: Var
                , truesPABoolPrimVar :: Var
94
95
96
                , lengthPAVar      :: Var
                , replicatePAVar   :: Var
                , emptyPAVar       :: Var
97
                , packPAVar        :: Var
98
                , combinePAVars    :: Array Int Var
99
100
101
                , liftingContext   :: Var
                }

102
103
104
105
106
107
uarrTy :: Type -> Builtins -> Type
uarrTy ty bi = mkTyConApp (uarrTyCon bi) [ty]

intPrimArrayTy :: Builtins -> Type
intPrimArrayTy = uarrTy intTy

108
109
110
111
112
113
114
sumTyCon :: Int -> Builtins -> TyCon
sumTyCon n bi
  | n >= 2 && n <= mAX_NDP_SUM = sumTyCons bi ! n
  | otherwise = pprPanic "sumTyCon" (ppr n)

prodTyCon :: Int -> Builtins -> TyCon
prodTyCon n bi
115
  | n == 1                      = wrapTyCon bi
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
116
  | n >= 0 && n <= mAX_NDP_PROD = tupleTyCon Boxed n
117
118
  | otherwise = pprPanic "prodTyCon" (ppr n)

119
120
121
122
123
combinePAVar :: Int -> Builtins -> Var
combinePAVar n bi
  | n >= 2 && n <= mAX_NDP_COMBINE = combinePAVars bi ! n
  | otherwise = pprPanic "combinePAVar" (ppr n)

124
125
126
initBuiltins :: DsM Builtins
initBuiltins
  = do
127
128
      parrayTyCon  <- externalTyCon nDP_PARRAY FSLIT("PArray")
      paTyCon      <- externalTyCon nDP_PARRAY FSLIT("PA")
129
      let [paDataCon] = tyConDataCons paTyCon
130
131
      preprTyCon   <- externalTyCon nDP_PARRAY FSLIT("PRepr")
      prTyCon      <- externalTyCon nDP_PARRAY FSLIT("PR")
132
      let [prDataCon] = tyConDataCons prTyCon
133
      uarrTyCon    <- externalTyCon nDP_UARR   FSLIT("UArr")
134
      closureTyCon <- externalTyCon nDP_CLOSURE FSLIT(":->")
135

136
137
      voidTyCon    <- externalTyCon nDP_REPR FSLIT("Void")
      wrapTyCon    <- externalTyCon nDP_REPR FSLIT("Wrap")
138
      enumerationTyCon <- externalTyCon nDP_REPR FSLIT("Enumeration")
139
      sum_tcs <- mapM (externalTyCon nDP_REPR)
140
141
142
143
                      [mkFastString ("Sum" ++ show i) | i <- [2..mAX_NDP_SUM]]

      let sumTyCons = listArray (2, mAX_NDP_SUM) sum_tcs

144
145
146
147
148
149
      voidVar          <- externalVar nDP_REPR FSLIT("void")
      mkPRVar          <- externalVar nDP_PARRAY FSLIT("mkPR")
      mkClosureVar     <- externalVar nDP_CLOSURE FSLIT("mkClosure")
      applyClosureVar  <- externalVar nDP_CLOSURE FSLIT("$:")
      mkClosurePVar    <- externalVar nDP_CLOSURE FSLIT("mkClosureP")
      applyClosurePVar <- externalVar nDP_CLOSURE FSLIT("$:^")
150
151
152
153
      replicatePAIntPrimVar <- externalVar nDP_UNBOXED FSLIT("replicatePA_Int#")
      upToPAIntPrimVar <- externalVar nDP_UNBOXED FSLIT("upToPA_Int#")
      selectPAIntPrimVar <- externalVar nDP_UNBOXED FSLIT("selectPA_Int#")
      truesPABoolPrimVar <- externalVar nDP_UNBOXED FSLIT("truesPA_Bool#")
154
155
      lengthPAVar      <- externalVar nDP_PARRAY FSLIT("lengthPA#")
      replicatePAVar   <- externalVar nDP_PARRAY FSLIT("replicatePA#")
156
      emptyPAVar       <- externalVar nDP_PARRAY FSLIT("emptyPA")
157
      packPAVar        <- externalVar nDP_PARRAY FSLIT("packPA#")
158
159

      combines <- mapM (externalVar nDP_PARRAY)
160
                       [mkFastString ("combine" ++ show i ++ "PA#")
161
162
                          | i <- [2..mAX_NDP_COMBINE]]
      let combinePAVars = listArray (2, mAX_NDP_COMBINE) combines
163
164
165
166
167
168
169
170
171
172
173

      liftingContext <- liftM (\u -> mkSysLocal FSLIT("lc") u intPrimTy)
                              newUnique

      return $ Builtins {
                 parrayTyCon      = parrayTyCon
               , paTyCon          = paTyCon
               , paDataCon        = paDataCon
               , preprTyCon       = preprTyCon
               , prTyCon          = prTyCon
               , prDataCon        = prDataCon
174
               , uarrTyCon        = uarrTyCon
175
               , voidTyCon        = voidTyCon
176
               , wrapTyCon        = wrapTyCon
177
               , enumerationTyCon = enumerationTyCon
178
               , sumTyCons        = sumTyCons
179
               , closureTyCon     = closureTyCon
180
               , voidVar          = voidVar
181
               , mkPRVar          = mkPRVar
182
183
184
185
               , mkClosureVar     = mkClosureVar
               , applyClosureVar  = applyClosureVar
               , mkClosurePVar    = mkClosurePVar
               , applyClosurePVar = applyClosurePVar
186
187
               , replicatePAIntPrimVar = replicatePAIntPrimVar
               , upToPAIntPrimVar = upToPAIntPrimVar
188
189
               , selectPAIntPrimVar = selectPAIntPrimVar
               , truesPABoolPrimVar = truesPABoolPrimVar
190
191
192
               , lengthPAVar      = lengthPAVar
               , replicatePAVar   = replicatePAVar
               , emptyPAVar       = emptyPAVar
193
               , packPAVar        = packPAVar
194
               , combinePAVars    = combinePAVars
195
196
197
               , liftingContext   = liftingContext
               }

198
initBuiltinVars :: Builtins -> DsM [(Var, Var)]
199
initBuiltinVars _
200
201
202
203
204
205
206
  = do
      uvars <- zipWithM externalVar umods ufs
      vvars <- zipWithM externalVar vmods vfs
      return $ [(v,v) | v <- map dataConWorkId defaultDataConWorkers]
               ++ zip uvars vvars
  where
    (umods, ufs, vmods, vfs) = unzip4 preludeVars
207
208

defaultDataConWorkers :: [DataCon]
209
defaultDataConWorkers = [trueDataCon, falseDataCon, unitDataCon]
210

211
212
213
preludeVars :: [(Module, FastString, Module, FastString)]
preludeVars
  = [
214
215
      mk gHC_PARR FSLIT("mapP")       nDP_COMBINATORS FSLIT("mapPA")
    , mk gHC_PARR FSLIT("zipWithP")   nDP_COMBINATORS FSLIT("zipWithPA")
216
    , mk gHC_PARR FSLIT("zipP")       nDP_COMBINATORS FSLIT("zipPA")
217
    , mk gHC_PARR FSLIT("filterP")    nDP_COMBINATORS FSLIT("filterPA")
218
    , mk gHC_PARR FSLIT("lengthP")    nDP_COMBINATORS FSLIT("lengthPA")
219
220
221
    , mk gHC_PARR FSLIT("replicateP") nDP_COMBINATORS FSLIT("replicatePA")
    , mk gHC_PARR FSLIT("!:")         nDP_COMBINATORS FSLIT("indexPA")
    , mk gHC_PARR FSLIT("crossMapP")  nDP_COMBINATORS FSLIT("crossMapPA")
222
    , mk gHC_PARR FSLIT("singletonP") nDP_COMBINATORS FSLIT("singletonPA")
223
    , mk gHC_PARR FSLIT("concatP")    nDP_COMBINATORS FSLIT("concatPA")
224
    , mk gHC_PARR FSLIT("+:+")        nDP_COMBINATORS FSLIT("appPA")
225

226
227
    , mk nDP_PRELUDE_INT  FSLIT("plus") nDP_PRELUDE_INT FSLIT("plusV")
    , mk nDP_PRELUDE_INT  FSLIT("minus") nDP_PRELUDE_INT FSLIT("minusV")
228
    , mk nDP_PRELUDE_INT  FSLIT("mult")  nDP_PRELUDE_INT FSLIT("multV")
229
230
    , mk nDP_PRELUDE_INT  FSLIT("sumP")  nDP_PRELUDE_INT FSLIT("sumPA")
    , mk nDP_PRELUDE_INT  FSLIT("upToP") nDP_PRELUDE_INT FSLIT("upToPA")
231

232
233
234
235
236
237
238
    , mk nDP_PRELUDE_INT  FSLIT("eq") nDP_PRELUDE_INT FSLIT("eqV")
    , mk nDP_PRELUDE_INT  FSLIT("neq") nDP_PRELUDE_INT FSLIT("neqV")
    , mk nDP_PRELUDE_INT  FSLIT("le")  nDP_PRELUDE_INT FSLIT("leV")
    , mk nDP_PRELUDE_INT  FSLIT("lt") nDP_PRELUDE_INT FSLIT("ltV")
    , mk nDP_PRELUDE_INT  FSLIT("ge") nDP_PRELUDE_INT FSLIT("geV")
    , mk nDP_PRELUDE_INT  FSLIT("gt")  nDP_PRELUDE_INT FSLIT("gtV")

239
240
241
242
    , mk nDP_PRELUDE_DOUBLE  FSLIT("plus") nDP_PRELUDE_DOUBLE FSLIT("plusV")
    , mk nDP_PRELUDE_DOUBLE  FSLIT("minus") nDP_PRELUDE_DOUBLE FSLIT("minusV")
    , mk nDP_PRELUDE_DOUBLE  FSLIT("mult")  nDP_PRELUDE_DOUBLE FSLIT("multV")
    , mk nDP_PRELUDE_DOUBLE  FSLIT("sumP")  nDP_PRELUDE_DOUBLE FSLIT("sumPA")
243
244
245
246
    , mk nDP_PRELUDE_DOUBLE  FSLIT("minIndexP") 
         nDP_PRELUDE_DOUBLE  FSLIT("minIndexPA")
    , mk nDP_PRELUDE_DOUBLE  FSLIT("maxIndexP")
         nDP_PRELUDE_DOUBLE  FSLIT("maxIndexPA")
247

248
249
250
251
252
253
254
    , mk nDP_PRELUDE_DOUBLE  FSLIT("eq") nDP_PRELUDE_DOUBLE FSLIT("eqV")
    , mk nDP_PRELUDE_DOUBLE  FSLIT("neq") nDP_PRELUDE_DOUBLE FSLIT("neqV")
    , mk nDP_PRELUDE_DOUBLE  FSLIT("le")  nDP_PRELUDE_DOUBLE FSLIT("leV")
    , mk nDP_PRELUDE_DOUBLE  FSLIT("lt") nDP_PRELUDE_DOUBLE FSLIT("ltV")
    , mk nDP_PRELUDE_DOUBLE  FSLIT("ge") nDP_PRELUDE_DOUBLE FSLIT("geV")
    , mk nDP_PRELUDE_DOUBLE  FSLIT("gt")  nDP_PRELUDE_DOUBLE FSLIT("gtV")

255
    -- FIXME: temporary
256
    , mk nDP_PRELUDE_PARR FSLIT("fromPArrayP") nDP_PRELUDE_PARR FSLIT("fromPArrayPA")
257
258
    , mk nDP_PRELUDE_PARR FSLIT("toPArrayP") nDP_PRELUDE_PARR FSLIT("toPArrayPA")
    , mk nDP_PRELUDE_PARR FSLIT("fromNestedPArrayP") nDP_PRELUDE_PARR FSLIT("fromNestedPArrayPA")
259
260
261
262
263
264
265
    ]
  where
    mk = (,,,)

initBuiltinTyCons :: Builtins -> DsM [(Name, TyCon)]
initBuiltinTyCons bi
  = do
266
      -- parr <- externalTyCon nDP_PRELUDE_PARR FSLIT("PArr")
267
      return $ (tyConName funTyCon, closureTyCon bi)
268
             : (parrTyConName,      parrayTyCon bi)
269
270
271
272

             -- FIXME: temporary
             : (tyConName $ parrayTyCon bi, parrayTyCon bi)

273
             : [(tyConName tc, tc) | tc <- defaultTyCons]
274
275

defaultTyCons :: [TyCon]
276
defaultTyCons = [intTyCon, boolTyCon, doubleTyCon]
277

278
initBuiltinDataCons :: Builtins -> [(Name, DataCon)]
279
initBuiltinDataCons _ = [(dataConName dc, dc)| dc <- defaultDataCons]
280
281

defaultDataCons :: [DataCon]
282
defaultDataCons = [trueDataCon, falseDataCon, unitDataCon]
283

284
285
initBuiltinDicts :: [(Name, Module, FastString)] -> DsM [(Name, Var)]
initBuiltinDicts ps
286
  = do
287
      dicts <- zipWithM externalVar mods fss
288
      return $ zip tcs dicts
289
  where
290
291
    (tcs, mods, fss) = unzip3 ps

292
initBuiltinPAs :: Builtins -> DsM [(Name, Var)]
293
initBuiltinPAs = initBuiltinDicts . builtinPAs
294

295
296
297
builtinPAs :: Builtins -> [(Name, Module, FastString)]
builtinPAs bi
  = [
298
299
      mk (tyConName $ closureTyCon bi)  nDP_CLOSURE     FSLIT("dPA_Clo")
    , mk (tyConName $ voidTyCon bi)     nDP_REPR        FSLIT("dPA_Void")
300
    , mk (tyConName $ parrayTyCon bi)   nDP_INSTANCES   FSLIT("dPA_PArray")
301
    , mk unitTyConName                  nDP_INSTANCES   FSLIT("dPA_Unit")
302

303
    , mk intTyConName                   nDP_INSTANCES   FSLIT("dPA_Int")
304
    , mk doubleTyConName                nDP_INSTANCES   FSLIT("dPA_Double")
305
    , mk boolTyConName                  nDP_INSTANCES   FSLIT("dPA_Bool")
306
307
    ]
    ++ tups
308
309
310
  where
    mk name mod fs = (name, mod, fs)

311
    tups = map mk_tup [2..mAX_NDP_PROD]
312
313
314
315
    mk_tup n = mk (tyConName $ tupleTyCon Boxed n)
                  nDP_INSTANCES
                  (mkFastString $ "dPA_" ++ show n)

316
initBuiltinPRs :: Builtins -> DsM [(Name, Var)]
317
initBuiltinPRs = initBuiltinDicts . builtinPRs
318

319
320
321
builtinPRs :: Builtins -> [(Name, Module, FastString)]
builtinPRs bi =
  [
322
323
324
    mk (tyConName unitTyCon)          nDP_REPR      FSLIT("dPR_Unit")
  , mk (tyConName $ voidTyCon bi)     nDP_REPR      FSLIT("dPR_Void")
  , mk (tyConName $ wrapTyCon bi)     nDP_REPR      FSLIT("dPR_Wrap")
325
  , mk (tyConName $ enumerationTyCon bi) nDP_REPR   FSLIT("dPR_Enumeration")
326
  , mk (tyConName $ closureTyCon bi)  nDP_CLOSURE   FSLIT("dPR_Clo")
327

328
329
    -- temporary
  , mk intTyConName          nDP_INSTANCES FSLIT("dPR_Int")
330
  , mk doubleTyConName       nDP_INSTANCES FSLIT("dPR_Double")
331
332
333
334
  ]

  ++ map mk_sum  [2..mAX_NDP_SUM]
  ++ map mk_prod [2..mAX_NDP_PROD]
335
336
  where
    mk name mod fs = (name, mod, fs)
337

338
339
340
341
342
343
    mk_sum n = (tyConName $ sumTyCon n bi, nDP_REPR,
                mkFastString ("dPR_Sum" ++ show n))

    mk_prod n = (tyConName $ prodTyCon n bi, nDP_REPR,
                 mkFastString ("dPR_" ++ show n))

344
345
346
347
initBuiltinBoxedTyCons :: Builtins -> DsM [(Name, TyCon)]
initBuiltinBoxedTyCons = return . builtinBoxedTyCons

builtinBoxedTyCons :: Builtins -> [(Name, TyCon)]
348
builtinBoxedTyCons _ =
349
350
  [(tyConName intPrimTyCon, intTyCon)]

351
352
externalVar :: Module -> FastString -> DsM Var
externalVar mod fs
353
  = dsLookupGlobalId =<< lookupOrig mod (mkVarOccFS fs)
354

355
356
externalTyCon :: Module -> FastString -> DsM TyCon
externalTyCon mod fs
357
358
  = dsLookupTyCon =<< lookupOrig mod (mkOccNameFS tcName fs)

359
unitTyConName :: Name
360
361
unitTyConName = tyConName unitTyCon

362
363
364
365
366

primMethod :: TyCon -> String -> DsM (Maybe Var)
primMethod tycon method
  | Just suffix <- lookupNameEnv prim_ty_cons (tyConName tycon)
  = liftM Just
367
  $ dsLookupGlobalId =<< lookupOrig nDP_UNBOXED (mkVarOcc $ method ++ suffix)
368
369
370

  | otherwise = return Nothing

371
372
373
374
primPArray :: TyCon -> DsM (Maybe TyCon)
primPArray tycon
  | Just suffix <- lookupNameEnv prim_ty_cons (tyConName tycon)
  = liftM Just
375
  $ dsLookupTyCon =<< lookupOrig nDP_UNBOXED (mkOccName tcName $ "PArray" ++ suffix)
376
377
378

  | otherwise = return Nothing

379
prim_ty_cons :: NameEnv String
380
381
382
prim_ty_cons = mkNameEnv [mk_prim intPrimTyCon]
  where
    mk_prim tycon = (tyConName tycon, '_' : getOccString tycon)
383