VectUtils.hs 16.5 KB
Newer Older
1
module VectUtils (
2
  collectAnnTypeBinders, collectAnnTypeArgs, isAnnTypeArg,
3
  collectAnnValBinders,
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
4 5 6
  dataConTagZ, mkDataConTag, mkDataConTagLit,

  newLocalVVar,
7

8
  mkBuiltinCo, voidType, mkWrapType,
9
  mkPADictType, mkPArrayType, mkPDataType, mkPReprType, mkPArray,
10

11
  pdataReprTyCon, pdataReprDataCon, mkVScrut,
12
  prDictOfType, prDFunOfTyCon,
13
  paDictArgType, paDictOfType, paDFunType,
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
14
  paMethod, wrapPR, replicatePD, emptyPD, packByTagPD,
15 16
  combinePD,
  liftPD,
17
  zipScalars, scalarClosure,
18 19
  polyAbstract, polyApply, polyVApply, polyArity,
  Inline(..), addInlineArity, inlineMe,
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
20
  hoistBinding, hoistExpr, hoistPolyVExpr, takeHoisted,
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
21 22
  buildClosure, buildClosures,
  mkClosureApp
23 24
) where

25
import VectCore
26 27
import VectMonad

simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
28
import MkCore ( mkCoreTup, mkWildCase )
29
import CoreSyn
30
import CoreUtils
31
import CoreUnfold         ( mkInlineRule )
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
32
import Coercion
33 34
import Type
import TypeRep
35
import TyCon
36
import DataCon
37
import Var
38
import MkId               ( unwrapFamInstScrut )
39
import Id                 ( setIdUnfolding )
40
import TysWiredIn
41
import BasicTypes         ( Boxity(..), Arity )
42
import Literal            ( Literal, mkMachInt )
43

44
import Outputable
45
import FastString
46

twanvl's avatar
twanvl committed
47 48
import Control.Monad

49 50 51 52 53 54 55 56 57 58 59 60
collectAnnTypeArgs :: AnnExpr b ann -> (AnnExpr b ann, [Type])
collectAnnTypeArgs expr = go expr []
  where
    go (_, AnnApp f (_, AnnType ty)) tys = go f (ty : tys)
    go e                             tys = (e, tys)

collectAnnTypeBinders :: AnnExpr Var ann -> ([Var], AnnExpr Var ann)
collectAnnTypeBinders expr = go [] expr
  where
    go bs (_, AnnLam b e) | isTyVar b = go (b:bs) e
    go bs e                           = (reverse bs, e)

61 62 63
collectAnnValBinders :: AnnExpr Var ann -> ([Var], AnnExpr Var ann)
collectAnnValBinders expr = go [] expr
  where
64 65
    go bs (_, AnnLam b e) | isId b = go (b:bs) e
    go bs e                        = (reverse bs, e)
66

67
isAnnTypeArg :: AnnExpr b ann -> Bool
twanvl's avatar
twanvl committed
68
isAnnTypeArg (_, AnnType _) = True
69 70
isAnnTypeArg _              = False

rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
71 72 73
dataConTagZ :: DataCon -> Int
dataConTagZ con = dataConTag con - fIRST_TAG

74
mkDataConTagLit :: DataCon -> Literal
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
75
mkDataConTagLit = mkMachInt . toInteger . dataConTagZ
76

77
mkDataConTag :: DataCon -> CoreExpr
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
78
mkDataConTag = mkIntLitInt . dataConTagZ
79

80 81 82 83 84 85 86 87
splitPrimTyCon :: Type -> Maybe TyCon
splitPrimTyCon ty
  | Just (tycon, []) <- splitTyConApp_maybe ty
  , isPrimTyCon tycon
  = Just tycon

  | otherwise = Nothing

rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
88 89
mkBuiltinTyConApp :: (Builtins -> TyCon) -> [Type] -> VM Type
mkBuiltinTyConApp get_tc tys
90
  = do
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
91 92
      tc <- builtin get_tc
      return $ mkTyConApp tc tys
93

rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
94 95
mkBuiltinTyConApps :: (Builtins -> TyCon) -> [Type] -> Type -> VM Type
mkBuiltinTyConApps get_tc tys ty
96
  = do
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
97 98
      tc <- builtin get_tc
      return $ foldr (mk tc) ty tys
99
  where
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
100
    mk tc ty1 ty2 = mkTyConApp tc [ty1,ty2]
101

102
voidType :: VM Type
103
voidType = mkBuiltinTyConApp VectMonad.voidTyCon []
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
104

105 106 107
mkWrapType :: Type -> VM Type
mkWrapType ty = mkBuiltinTyConApp wrapTyCon [ty]

rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
108 109 110
mkClosureTypes :: [Type] -> Type -> VM Type
mkClosureTypes = mkBuiltinTyConApps closureTyCon

111 112 113
mkPReprType :: Type -> VM Type
mkPReprType ty = mkBuiltinTyConApp preprTyCon [ty]

rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
114 115
mkPADictType :: Type -> VM Type
mkPADictType ty = mkBuiltinTyConApp paTyCon [ty]
116 117

mkPArrayType :: Type -> VM Type
118 119 120
mkPArrayType ty
  | Just tycon <- splitPrimTyCon ty
  = do
121 122 123 124
      r <- lookupPrimPArray tycon
      case r of
        Just arr -> return $ mkTyConApp arr []
        Nothing  -> cantVectorise "Primitive tycon not vectorised" (ppr tycon)
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
125
mkPArrayType ty = mkBuiltinTyConApp parrayTyCon [ty]
126

127 128 129 130 131 132 133 134 135
mkPDataType :: Type -> VM Type
mkPDataType ty = mkBuiltinTyConApp pdataTyCon [ty]

mkPArray :: Type -> CoreExpr -> CoreExpr -> VM CoreExpr
mkPArray ty len dat = do
                        tc <- builtin parrayTyCon
                        let [dc] = tyConDataCons tc
                        return $ mkConApp dc [Type ty, len, dat]

136 137
mkBuiltinCo :: (Builtins -> TyCon) -> VM Coercion
mkBuiltinCo get_tc
138
  = do
139 140
      tc <- builtin get_tc
      return $ mkTyConApp tc []
141

142 143
pdataReprTyCon :: Type -> VM (TyCon, [Type])
pdataReprTyCon ty = builtin pdataTyCon >>= (`lookupFamInst` [ty])
144

145 146
pdataReprDataCon :: Type -> VM (DataCon, [Type])
pdataReprDataCon ty
147
  = do
148
      (tc, arg_tys) <- pdataReprTyCon ty
149 150 151
      let [dc] = tyConDataCons tc
      return (dc, arg_tys)

152
mkVScrut :: VExpr -> VM (CoreExpr, CoreExpr, TyCon, [Type])
153 154
mkVScrut (ve, le)
  = do
155 156 157 158
      (tc, arg_tys) <- pdataReprTyCon ty
      return (ve, unwrapFamInstScrut tc arg_tys le, tc, arg_tys)
  where
    ty = exprType ve
159

160 161
prDFunOfTyCon :: TyCon -> VM CoreExpr
prDFunOfTyCon tycon
162 163 164
  = liftM Var
  . maybeCantVectoriseM "No PR dictionary for tycon" (ppr tycon)
  $ lookupTyConPR tycon
165

166

167 168 169 170 171 172
paDictArgType :: TyVar -> VM (Maybe Type)
paDictArgType tv = go (TyVarTy tv) (tyVarKind tv)
  where
    go ty k | Just k' <- kindView k = go ty k'
    go ty (FunTy k1 k2)
      = do
Ian Lynagh's avatar
Ian Lynagh committed
173
          tv   <- newTyVar (fsLit "a") k1
174 175 176 177 178 179 180 181 182
          mty1 <- go (TyVarTy tv) k1
          case mty1 of
            Just ty1 -> do
                          mty2 <- go (AppTy ty (TyVarTy tv)) k2
                          return $ fmap (ForAllTy tv . FunTy ty1) mty2
            Nothing  -> go ty k2

    go ty k
      | isLiftedTypeKind k
183
      = liftM Just (mkPADictType ty)
184

twanvl's avatar
twanvl committed
185
    go _ _ = return Nothing
186

187 188 189 190 191

-- | Get the PA dictionary for some type, or `Nothing` if there isn't one.
paDictOfType :: Type -> VM (Maybe CoreExpr)
paDictOfType ty 
  = paDictOfTyApp ty_fn ty_args
192 193 194
  where
    (ty_fn, ty_args) = splitAppTys ty

195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
    paDictOfTyApp :: Type -> [Type] -> VM (Maybe CoreExpr)
    paDictOfTyApp ty_fn ty_args
        | Just ty_fn' <- coreView ty_fn 
        = paDictOfTyApp ty_fn' ty_args

    paDictOfTyApp (TyVarTy tv) ty_args
     = do dfun <- maybeV (lookupTyVarPA tv)
          liftM Just $ paDFunApply dfun ty_args

    paDictOfTyApp (TyConApp tc _) ty_args
     = do mdfun <- lookupTyConPA tc
          case mdfun of
	    Nothing	
	     -> pprTrace "VectUtils.paDictOfType"
	                 (vcat [ text "No PA dictionary"
			       , text "for tycon: " <> ppr tc
			       , text "in type:   " <> ppr ty])
	     $ return Nothing

	    Just dfun	-> liftM Just $ paDFunApply (Var dfun) ty_args

    paDictOfTyApp ty _
     = cantVectorise "Can't construct PA dictionary for type" (ppr ty)


220

221 222 223 224 225 226 227 228 229 230 231
paDFunType :: TyCon -> VM Type
paDFunType tc
  = do
      margs <- mapM paDictArgType tvs
      res   <- mkPADictType (mkTyConApp tc arg_tys)
      return . mkForAllTys tvs
             $ mkFunTys [arg | Just arg <- margs] res
  where
    tvs = tyConTyVars tc
    arg_tys = mkTyVarTys tvs

232 233
paDFunApply :: CoreExpr -> [Type] -> VM CoreExpr
paDFunApply dfun tys
234
 = do Just dicts <- liftM sequence $ mapM paDictOfType tys
235 236
      return $ mkApps (mkTyApps dfun tys) dicts

237

238 239
paMethod :: (Builtins -> Var) -> String -> Type -> VM CoreExpr
paMethod _ name ty
240
  | Just tycon <- splitPrimTyCon ty
241 242 243
  = liftM Var
  . maybeCantVectoriseM "No PA method" (text name <+> text "for" <+> ppr tycon)
  $ lookupPrimMethod tycon name
244

245
paMethod method _ ty
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
246
  = do
247 248
      fn        <- builtin method
      Just dict <- paDictOfType ty
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
249 250
      return $ mkApps (Var fn) [Type ty, dict]

251 252 253 254 255 256 257 258 259
prDictOfType :: Type -> VM CoreExpr
prDictOfType ty = prDictOfTyApp ty_fn ty_args
  where
    (ty_fn, ty_args) = splitAppTys ty

prDictOfTyApp :: Type -> [Type] -> VM CoreExpr
prDictOfTyApp ty_fn ty_args
  | Just ty_fn' <- coreView ty_fn = prDictOfTyApp ty_fn' ty_args
prDictOfTyApp (TyConApp tc _) ty_args
260
  = do
261
      dfun <- liftM Var $ maybeV (lookupTyConPR tc)
262
      prDFunApply dfun ty_args
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
263
prDictOfTyApp _ _ = noV
264 265 266 267 268 269 270 271 272 273

prDFunApply :: CoreExpr -> [Type] -> VM CoreExpr
prDFunApply dfun tys
  = do
      dicts <- mapM prDictOfType tys
      return $ mkApps (mkTyApps dfun tys) dicts

wrapPR :: Type -> VM CoreExpr
wrapPR ty
  = do
274 275
      Just  pa_dict <- paDictOfType ty
      pr_dfun       <- prDFunOfTyCon =<< builtin wrapTyCon
276
      return $ mkApps pr_dfun [Type ty, pa_dict]
277

278 279 280
replicatePD :: CoreExpr -> CoreExpr -> VM CoreExpr
replicatePD len x = liftM (`mkApps` [len,x])
                          (paMethod replicatePDVar "replicatePD" (exprType x))
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
281

282 283
emptyPD :: Type -> VM CoreExpr
emptyPD = paMethod emptyPDVar "emptyPD"
284

285 286 287 288 289 290
packByTagPD :: Type -> CoreExpr -> CoreExpr -> CoreExpr -> CoreExpr
                 -> VM CoreExpr
packByTagPD ty xs len tags t
  = liftM (`mkApps` [xs, len, tags, t])
          (paMethod packByTagPDVar "packByTagPD" ty)

291
combinePD :: Type -> CoreExpr -> CoreExpr -> [CoreExpr]
292
          -> VM CoreExpr
293 294 295
combinePD ty len sel xs
  = liftM (`mkApps` (len : sel : xs))
          (paMethod (combinePDVar n) ("combine" ++ show n ++ "PD") ty)
296 297 298
  where
    n = length xs

299
-- | Like `replicatePD` but use the lifting context in the vectoriser state.
300 301
liftPD :: CoreExpr -> VM CoreExpr
liftPD x
302 303
  = do
      lc <- builtin liftingContext
304
      replicatePD (Var lc) x
305

306 307 308 309 310 311 312 313 314 315 316 317 318
zipScalars :: [Type] -> Type -> VM CoreExpr
zipScalars arg_tys res_ty
  = do
      scalar <- builtin scalarClass
      (dfuns, _) <- mapAndUnzipM (\ty -> lookupInst scalar [ty]) ty_args
      zipf <- builtin (scalarZip $ length arg_tys)
      return $ Var zipf `mkTyApps` ty_args `mkApps` map Var dfuns
    where
      ty_args = arg_tys ++ [res_ty]

scalarClosure :: [Type] -> Type -> CoreExpr -> CoreExpr -> VM CoreExpr
scalarClosure arg_tys res_ty scalar_fun array_fun
  = do
319 320
      ctr      <- builtin (closureCtrFun $ length arg_tys)
      Just pas <- liftM sequence $ mapM paDictOfType (init arg_tys)
321 322 323
      return $ Var ctr `mkTyApps` (arg_tys ++ [res_ty])
                       `mkApps`   (pas ++ [scalar_fun, array_fun])

324 325 326
newLocalVVar :: FastString -> Type -> VM VVar
newLocalVVar fs vty
  = do
327
      lty <- mkPDataType vty
328 329 330 331
      vv  <- newLocalVar fs vty
      lv  <- newLocalVar fs lty
      return (vv,lv)

332
polyAbstract :: [TyVar] -> ([Var] -> VM a) -> VM a
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
333 334 335
polyAbstract tvs p
  = localV
  $ do
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
336
      mdicts <- mapM mk_dict_var tvs
337 338 339
      zipWithM_ (\tv -> maybe (defLocalTyVar tv)
                              (defLocalTyVarWithPA tv . Var)) tvs mdicts
      p (mk_args mdicts)
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
340 341 342 343
  where
    mk_dict_var tv = do
                       r <- paDictArgType tv
                       case r of
Ian Lynagh's avatar
Ian Lynagh committed
344
                         Just ty -> liftM Just (newLocalVar (fsLit "dPA") ty)
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
345 346
                         Nothing -> return Nothing

347 348 349 350 351 352
    mk_args mdicts = [dict | Just dict <- mdicts]

polyArity :: [TyVar] -> VM Int
polyArity tvs = do
                  tys <- mapM paDictArgType tvs
                  return $ length [() | Just _ <- tys]
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
353

rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
354 355
polyApply :: CoreExpr -> [Type] -> VM CoreExpr
polyApply expr tys
356
 = do Just dicts <- liftM sequence $ mapM paDictOfType tys
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
357 358
      return $ expr `mkTyApps` tys `mkApps` dicts

359 360
polyVApply :: VExpr -> [Type] -> VM VExpr
polyVApply expr tys
361 362
 = do Just dicts <- liftM sequence $ mapM paDictOfType tys
      return     $ mapVect (\e -> e `mkTyApps` tys `mkApps` dicts) expr
363

364 365 366 367 368
-- Inline ---------------------------------------------------------------------
-- | Records whether we should inline a particular binding.
data Inline 
        = Inline Arity
        | DontInline
369

370
-- | Add to the arity contained within an `Inline`, if any.
371 372 373 374
addInlineArity :: Inline -> Int -> Inline
addInlineArity (Inline m) n = Inline (m+n)
addInlineArity DontInline _ = DontInline

375
-- | Says to always inline a binding.
376 377 378
inlineMe :: Inline
inlineMe = Inline 0

379 380

-- Hoising --------------------------------------------------------------------
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
381 382 383 384
hoistBinding :: Var -> CoreExpr -> VM ()
hoistBinding v e = updGEnv $ \env ->
  env { global_bindings = (v,e) : global_bindings env }

385 386
hoistExpr :: FastString -> CoreExpr -> Inline -> VM Var
hoistExpr fs expr inl
387
  = do
388
      var <- mk_inline `liftM` newLocalVar fs (exprType expr)
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
389
      hoistBinding var expr
390
      return var
391 392 393
  where
    mk_inline var = case inl of
                      Inline arity -> var `setIdUnfolding`
394
                                      mkInlineRule expr (Just arity)
395
                      DontInline   -> var
396

397 398
hoistVExpr :: VExpr -> Inline -> VM VVar
hoistVExpr (ve, le) inl
399
  = do
400
      fs <- getBindName
401 402
      vv <- hoistExpr ('v' `consFS` fs) ve inl
      lv <- hoistExpr ('l' `consFS` fs) le (addInlineArity inl 1)
403
      return (vv, lv)
404

405 406
hoistPolyVExpr :: [TyVar] -> Inline -> VM VExpr -> VM VExpr
hoistPolyVExpr tvs inline p
407
  = do
408 409 410 411
      inline' <- liftM (addInlineArity inline) (polyArity tvs)
      expr <- closedV . polyAbstract tvs $ \args ->
              liftM (mapVect (mkLams $ tvs ++ args)) p
      fn   <- hoistVExpr expr inline'
412
      polyVApply (vVar fn) (mkTyVarTys tvs)
413

414 415 416 417 418 419 420
takeHoisted :: VM [(Var, CoreExpr)]
takeHoisted
  = do
      env <- readGEnv id
      setGEnv $ env { global_bindings = [] }
      return $ global_bindings env

twanvl's avatar
twanvl committed
421
{-
422 423 424 425 426 427 428 429 430 431 432
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)
twanvl's avatar
twanvl committed
433
-}
434

435
-- Closures -------------------------------------------------------------------
436 437
mkClosure :: Type -> Type -> Type -> VExpr -> VExpr -> VM VExpr
mkClosure arg_ty res_ty env_ty (vfn,lfn) (venv,lenv)
438 439 440
 = do Just dict <- paDictOfType env_ty
      mkv       <- builtin closureVar
      mkl       <- builtin liftedClosureVar
441 442
      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])
443

444

445 446
mkClosureApp :: Type -> Type -> VExpr -> VExpr -> VM VExpr
mkClosureApp arg_ty res_ty (vclo, lclo) (varg, larg)
447
 = do vapply <- builtin applyVar
448 449
      lapply <- builtin liftedApplyVar
      lc     <- builtin liftingContext
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
450
      return (Var vapply `mkTyApps` [arg_ty, res_ty] `mkApps` [vclo, varg],
451
              Var lapply `mkTyApps` [arg_ty, res_ty] `mkApps` [Var lc, lclo, larg])
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
452

453

454
buildClosures :: [TyVar] -> [VVar] -> [Type] -> Type -> VM VExpr -> VM VExpr
twanvl's avatar
twanvl committed
455
buildClosures _   _    [] _ mk_body
456
  = mk_body
457
buildClosures tvs vars [arg_ty] res_ty mk_body
458 459
  = -- liftM vInlineMe $
      buildClosure tvs vars arg_ty res_ty mk_body
460
buildClosures tvs vars (arg_ty : arg_tys) res_ty mk_body
461 462
  = do
      res_ty' <- mkClosureTypes arg_tys res_ty
Ian Lynagh's avatar
Ian Lynagh committed
463
      arg <- newLocalVVar (fsLit "x") arg_ty
464 465 466
      -- liftM vInlineMe
      buildClosure tvs vars arg_ty res_ty'
        . hoistPolyVExpr tvs (Inline (length vars + 1))
467
        $ do
468 469
            lc <- builtin liftingContext
            clo <- buildClosures tvs (vars ++ [arg]) arg_tys res_ty mk_body
470 471
            return $ vLams lc (vars ++ [arg]) clo

472 473 474 475
-- (clo <x1,...,xn> <f,f^>, aclo (Arr lc xs1 ... xsn) <f,f^>)
--   where
--     f  = \env v -> case env of <x1,...,xn> -> e x1 ... xn v
--     f^ = \env v -> case env of Arr l xs1 ... xsn -> e^ l x1 ... xn v
476
--
477 478
buildClosure :: [TyVar] -> [VVar] -> Type -> Type -> VM VExpr -> VM VExpr
buildClosure tvs vars arg_ty res_ty mk_body
479
  = do
480
      (env_ty, env, bind) <- buildEnv vars
Ian Lynagh's avatar
Ian Lynagh committed
481 482
      env_bndr <- newLocalVVar (fsLit "env") env_ty
      arg_bndr <- newLocalVVar (fsLit "arg") arg_ty
483

484
      fn <- hoistPolyVExpr tvs (Inline 2)
485
          $ do
486
              lc    <- builtin liftingContext
487
              body  <- mk_body
488
              return -- . vInlineMe
489 490
                     . vLams lc [env_bndr, arg_bndr]
                     $ bind (vVar env_bndr)
491
                            (vVarApps lc body (vars ++ [arg_bndr]))
492

493
      mkClosure arg_ty res_ty env_ty fn env
494

495 496

-- Environments ---------------------------------------------------------------
497 498 499 500 501 502 503 504 505 506 507
buildEnv :: [VVar] -> VM (Type, VExpr, VExpr -> VExpr -> VExpr)
buildEnv [] = do
             ty    <- voidType
             void  <- builtin voidVar
             pvoid <- builtin pvoidVar
             return (ty, vVar (void, pvoid), \_ body -> body)

buildEnv [v] = return (vVarType v, vVar v,
                    \env body -> vLet (vNonRec v env) body)

buildEnv vs
508
  = do
509 510
      
      (lenv_tc, lenv_tyargs) <- pdataReprTyCon ty
511

512 513 514 515 516 517 518 519
      let venv_con   = tupleCon Boxed (length vs) 
          [lenv_con] = tyConDataCons lenv_tc

          venv       = mkCoreTup (map Var vvs)
          lenv       = Var (dataConWrapId lenv_con)
                       `mkTyApps` lenv_tyargs
                       `mkApps`   map Var lvs

rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
520 521
          vbind env body = mkWildCase env ty (exprType body)
                           [(DataAlt venv_con, vvs, body)]
522 523

          lbind env body =
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
524
            let scrut = unwrapFamInstScrut lenv_tc lenv_tyargs env
525 526 527 528 529 530 531 532 533 534 535
            in
            mkWildCase scrut (exprType scrut) (exprType body)
              [(DataAlt lenv_con, lvs, body)]

          bind (venv, lenv) (vbody, lbody) = (vbind venv vbody,
                                              lbind lenv lbody)

      return (ty, (venv, lenv), bind)
  where
    (vvs, lvs) = unzip vs
    tys        = map vVarType vs
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
536
    ty         = mkBoxedTupleTy tys
537