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
9 collectAnnTypeBinders, collectAnnTypeArgs, isAnnTypeArg,
11 dataConTagZ, mkDataConTag, mkDataConTagLit,
16 mkPADictType, mkPArrayType, mkPReprType,
18 parrayReprTyCon, parrayReprDataCon, mkVScrut,
20 paDictArgType, paDictOfType, paDFunType,
21 paMethod, mkPR, lengthPA, replicatePA, emptyPA, packPA, combinePA, liftPA,
22 polyAbstract, polyApply, polyVApply,
23 hoistBinding, hoistExpr, hoistPolyVExpr, takeHoisted,
24 buildClosure, buildClosures,
28 #include "HsVersions.h"
42 import Id ( mkWildId )
43 import MkId ( unwrapFamInstScrut )
47 import TysPrim ( intPrimTy )
48 import BasicTypes ( Boxity(..) )
49 import Literal ( Literal, mkMachInt )
54 import Data.List ( zipWith4 )
55 import Control.Monad ( liftM, liftM2, zipWithM_ )
57 collectAnnTypeArgs :: AnnExpr b ann -> (AnnExpr b ann, [Type])
58 collectAnnTypeArgs expr = go expr []
60 go (_, AnnApp f (_, AnnType ty)) tys = go f (ty : tys)
63 collectAnnTypeBinders :: AnnExpr Var ann -> ([Var], AnnExpr Var ann)
64 collectAnnTypeBinders expr = go [] expr
66 go bs (_, AnnLam b e) | isTyVar b = go (b:bs) e
67 go bs e = (reverse bs, e)
69 collectAnnValBinders :: AnnExpr Var ann -> ([Var], AnnExpr Var ann)
70 collectAnnValBinders expr = go [] expr
72 go bs (_, AnnLam b e) | isId b = go (b:bs) e
73 go bs e = (reverse bs, e)
75 isAnnTypeArg :: AnnExpr b ann -> Bool
76 isAnnTypeArg (_, AnnType t) = True
77 isAnnTypeArg _ = False
79 dataConTagZ :: DataCon -> Int
80 dataConTagZ con = dataConTag con - fIRST_TAG
82 mkDataConTagLit :: DataCon -> Literal
83 mkDataConTagLit = mkMachInt . toInteger . dataConTagZ
85 mkDataConTag :: DataCon -> CoreExpr
86 mkDataConTag = mkIntLitInt . dataConTagZ
88 splitPrimTyCon :: Type -> Maybe TyCon
90 | Just (tycon, []) <- splitTyConApp_maybe ty
96 mkBuiltinTyConApp :: (Builtins -> TyCon) -> [Type] -> VM Type
97 mkBuiltinTyConApp get_tc tys
100 return $ mkTyConApp tc tys
102 mkBuiltinTyConApps :: (Builtins -> TyCon) -> [Type] -> Type -> VM Type
103 mkBuiltinTyConApps get_tc tys ty
106 return $ foldr (mk tc) ty tys
108 mk tc ty1 ty2 = mkTyConApp tc [ty1,ty2]
110 mkBuiltinTyConApps1 :: (Builtins -> TyCon) -> Type -> [Type] -> VM Type
111 mkBuiltinTyConApps1 get_tc dft [] = return dft
112 mkBuiltinTyConApps1 get_tc dft tys
116 [] -> pprPanic "mkBuiltinTyConApps1" (ppr tc)
117 _ -> return $ foldr1 (mk tc) tys
119 mk tc ty1 ty2 = mkTyConApp tc [ty1,ty2]
121 mkClosureType :: Type -> Type -> VM Type
122 mkClosureType arg_ty res_ty = mkBuiltinTyConApp closureTyCon [arg_ty, res_ty]
124 mkClosureTypes :: [Type] -> Type -> VM Type
125 mkClosureTypes = mkBuiltinTyConApps closureTyCon
127 mkPReprType :: Type -> VM Type
128 mkPReprType ty = mkBuiltinTyConApp preprTyCon [ty]
130 mkPADictType :: Type -> VM Type
131 mkPADictType ty = mkBuiltinTyConApp paTyCon [ty]
133 mkPArrayType :: Type -> VM Type
135 | Just tycon <- splitPrimTyCon ty
137 arr <- traceMaybeV "mkPArrayType" (ppr tycon)
138 $ lookupPrimPArray tycon
139 return $ mkTyConApp arr []
140 mkPArrayType ty = mkBuiltinTyConApp parrayTyCon [ty]
142 mkBuiltinCo :: (Builtins -> TyCon) -> VM Coercion
146 return $ mkTyConApp tc []
148 parrayReprTyCon :: Type -> VM (TyCon, [Type])
149 parrayReprTyCon ty = builtin parrayTyCon >>= (`lookupFamInst` [ty])
151 parrayReprDataCon :: Type -> VM (DataCon, [Type])
154 (tc, arg_tys) <- parrayReprTyCon ty
155 let [dc] = tyConDataCons tc
158 mkVScrut :: VExpr -> VM (VExpr, TyCon, [Type])
161 (tc, arg_tys) <- parrayReprTyCon (exprType ve)
162 return ((ve, unwrapFamInstScrut tc arg_tys le), tc, arg_tys)
164 prDFunOfTyCon :: TyCon -> VM CoreExpr
166 = liftM Var (traceMaybeV "prDictOfTyCon" (ppr tycon) (lookupTyConPR tycon))
168 paDictArgType :: TyVar -> VM (Maybe Type)
169 paDictArgType tv = go (TyVarTy tv) (tyVarKind tv)
171 go ty k | Just k' <- kindView k = go ty k'
174 tv <- newTyVar FSLIT("a") k1
175 mty1 <- go (TyVarTy tv) k1
178 mty2 <- go (AppTy ty (TyVarTy tv)) k2
179 return $ fmap (ForAllTy tv . FunTy ty1) mty2
184 = liftM Just (mkPADictType ty)
186 go ty k = return Nothing
188 paDictOfType :: Type -> VM CoreExpr
189 paDictOfType ty = paDictOfTyApp ty_fn ty_args
191 (ty_fn, ty_args) = splitAppTys ty
193 paDictOfTyApp :: Type -> [Type] -> VM CoreExpr
194 paDictOfTyApp ty_fn ty_args
195 | Just ty_fn' <- coreView ty_fn = paDictOfTyApp ty_fn' ty_args
196 paDictOfTyApp (TyVarTy tv) ty_args
198 dfun <- maybeV (lookupTyVarPA tv)
199 paDFunApply dfun ty_args
200 paDictOfTyApp (TyConApp tc _) ty_args
202 dfun <- traceMaybeV "paDictOfTyApp" (ppr tc) (lookupTyConPA tc)
203 paDFunApply (Var dfun) ty_args
204 paDictOfTyApp ty ty_args = pprPanic "paDictOfTyApp" (ppr ty)
206 paDFunType :: TyCon -> VM Type
209 margs <- mapM paDictArgType tvs
210 res <- mkPADictType (mkTyConApp tc arg_tys)
211 return . mkForAllTys tvs
212 $ mkFunTys [arg | Just arg <- margs] res
215 arg_tys = mkTyVarTys tvs
217 paDFunApply :: CoreExpr -> [Type] -> VM CoreExpr
220 dicts <- mapM paDictOfType tys
221 return $ mkApps (mkTyApps dfun tys) dicts
223 type PAMethod = (Builtins -> Var, String)
225 pa_length = (lengthPAVar, "lengthPA")
226 pa_replicate = (replicatePAVar, "replicatePA")
227 pa_empty = (emptyPAVar, "emptyPA")
228 pa_pack = (packPAVar, "packPA")
230 paMethod :: PAMethod -> Type -> VM CoreExpr
231 paMethod (method, name) ty
232 | Just tycon <- splitPrimTyCon ty
234 fn <- traceMaybeV "paMethod" (ppr tycon <+> text name)
235 $ lookupPrimMethod tycon name
238 paMethod (method, name) ty
241 dict <- paDictOfType ty
242 return $ mkApps (Var fn) [Type ty, dict]
244 mkPR :: Type -> VM CoreExpr
247 fn <- builtin mkPRVar
248 dict <- paDictOfType ty
249 return $ mkApps (Var fn) [Type ty, dict]
251 lengthPA :: Type -> CoreExpr -> VM CoreExpr
252 lengthPA ty x = liftM (`App` x) (paMethod pa_length ty)
254 replicatePA :: CoreExpr -> CoreExpr -> VM CoreExpr
255 replicatePA len x = liftM (`mkApps` [len,x])
256 (paMethod pa_replicate (exprType x))
258 emptyPA :: Type -> VM CoreExpr
259 emptyPA = paMethod pa_empty
261 packPA :: Type -> CoreExpr -> CoreExpr -> CoreExpr -> VM CoreExpr
262 packPA ty xs len sel = liftM (`mkApps` [xs, len, sel])
263 (paMethod pa_pack ty)
265 combinePA :: Type -> CoreExpr -> CoreExpr -> CoreExpr -> [CoreExpr]
267 combinePA ty len sel is xs
268 = liftM (`mkApps` (len : sel : is : xs))
269 (paMethod (combinePAVar n, "combine" ++ show n ++ "PA") ty)
273 liftPA :: CoreExpr -> VM CoreExpr
276 lc <- builtin liftingContext
277 replicatePA (Var lc) x
279 newLocalVVar :: FastString -> Type -> VM VVar
282 lty <- mkPArrayType vty
283 vv <- newLocalVar fs vty
284 lv <- newLocalVar fs lty
287 polyAbstract :: [TyVar] -> ((CoreExpr -> CoreExpr) -> VM a) -> VM a
291 mdicts <- mapM mk_dict_var tvs
292 zipWithM_ (\tv -> maybe (defLocalTyVar tv) (defLocalTyVarWithPA tv . Var)) tvs mdicts
296 r <- paDictArgType tv
298 Just ty -> liftM Just (newLocalVar FSLIT("dPA") ty)
299 Nothing -> return Nothing
301 mk_lams mdicts = mkLams (tvs ++ [dict | Just dict <- mdicts])
303 polyApply :: CoreExpr -> [Type] -> VM CoreExpr
306 dicts <- mapM paDictOfType tys
307 return $ expr `mkTyApps` tys `mkApps` dicts
309 polyVApply :: VExpr -> [Type] -> VM VExpr
312 dicts <- mapM paDictOfType tys
313 return $ mapVect (\e -> e `mkTyApps` tys `mkApps` dicts) expr
315 hoistBinding :: Var -> CoreExpr -> VM ()
316 hoistBinding v e = updGEnv $ \env ->
317 env { global_bindings = (v,e) : global_bindings env }
319 hoistExpr :: FastString -> CoreExpr -> VM Var
322 var <- newLocalVar fs (exprType expr)
323 hoistBinding var expr
326 hoistVExpr :: VExpr -> VM VVar
330 vv <- hoistExpr ('v' `consFS` fs) ve
331 lv <- hoistExpr ('l' `consFS` fs) le
334 hoistPolyVExpr :: [TyVar] -> VM VExpr -> VM VExpr
337 expr <- closedV . polyAbstract tvs $ \abstract ->
338 liftM (mapVect abstract) p
339 fn <- hoistVExpr expr
340 polyVApply (vVar fn) (mkTyVarTys tvs)
342 takeHoisted :: VM [(Var, CoreExpr)]
346 setGEnv $ env { global_bindings = [] }
347 return $ global_bindings env
349 boxExpr :: Type -> VExpr -> VM VExpr
350 boxExpr ty (vexpr, lexpr)
351 | Just (tycon, []) <- splitTyConApp_maybe ty
352 , isUnLiftedTyCon tycon
354 r <- lookupBoxedTyCon tycon
356 Just tycon' -> let [dc] = tyConDataCons tycon'
358 return (mkConApp dc [vexpr], lexpr)
359 Nothing -> return (vexpr, lexpr)
362 mkClosure :: Type -> Type -> Type -> VExpr -> VExpr -> VM VExpr
363 mkClosure arg_ty res_ty env_ty (vfn,lfn) (venv,lenv)
365 dict <- paDictOfType env_ty
366 mkv <- builtin mkClosureVar
367 mkl <- builtin mkClosurePVar
368 return (Var mkv `mkTyApps` [arg_ty, res_ty, env_ty] `mkApps` [dict, vfn, lfn, venv],
369 Var mkl `mkTyApps` [arg_ty, res_ty, env_ty] `mkApps` [dict, vfn, lfn, lenv])
371 mkClosureApp :: Type -> Type -> VExpr -> VExpr -> VM VExpr
372 mkClosureApp arg_ty res_ty (vclo, lclo) (varg, larg)
374 vapply <- builtin applyClosureVar
375 lapply <- builtin applyClosurePVar
376 return (Var vapply `mkTyApps` [arg_ty, res_ty] `mkApps` [vclo, varg],
377 Var lapply `mkTyApps` [arg_ty, res_ty] `mkApps` [lclo, larg])
379 buildClosures :: [TyVar] -> [VVar] -> [Type] -> Type -> VM VExpr -> VM VExpr
380 buildClosures tvs vars [] res_ty mk_body
382 buildClosures tvs vars [arg_ty] res_ty mk_body
383 = buildClosure tvs vars arg_ty res_ty mk_body
384 buildClosures tvs vars (arg_ty : arg_tys) res_ty mk_body
386 res_ty' <- mkClosureTypes arg_tys res_ty
387 arg <- newLocalVVar FSLIT("x") arg_ty
388 buildClosure tvs vars arg_ty res_ty'
391 lc <- builtin liftingContext
392 clo <- buildClosures tvs (vars ++ [arg]) arg_tys res_ty mk_body
393 return $ vLams lc (vars ++ [arg]) clo
395 -- (clo <x1,...,xn> <f,f^>, aclo (Arr lc xs1 ... xsn) <f,f^>)
397 -- f = \env v -> case env of <x1,...,xn> -> e x1 ... xn v
398 -- f^ = \env v -> case env of Arr l xs1 ... xsn -> e^ l x1 ... xn v
400 buildClosure :: [TyVar] -> [VVar] -> Type -> Type -> VM VExpr -> VM VExpr
401 buildClosure tvs vars arg_ty res_ty mk_body
403 (env_ty, env, bind) <- buildEnv vars
404 env_bndr <- newLocalVVar FSLIT("env") env_ty
405 arg_bndr <- newLocalVVar FSLIT("arg") arg_ty
407 fn <- hoistPolyVExpr tvs
409 lc <- builtin liftingContext
411 body' <- bind (vVar env_bndr)
412 (vVarApps lc body (vars ++ [arg_bndr]))
413 return (vLamsWithoutLC [env_bndr, arg_bndr] body')
415 mkClosure arg_ty res_ty env_ty fn env
417 buildEnv :: [VVar] -> VM (Type, VExpr, VExpr -> VExpr -> VM VExpr)
420 lc <- builtin liftingContext
421 let (ty, venv, vbind) = mkVectEnv tys vs
422 (lenv, lbind) <- mkLiftEnv lc tys ls
423 return (ty, (venv, lenv),
424 \(venv,lenv) (vbody,lbody) ->
426 let vbody' = vbind venv vbody
427 lbody' <- lbind lenv lbody
428 return (vbody', lbody'))
433 mkVectEnv :: [Type] -> [Var] -> (Type, CoreExpr, CoreExpr -> CoreExpr -> CoreExpr)
434 mkVectEnv [] [] = (unitTy, Var unitDataConId, \env body -> body)
435 mkVectEnv [ty] [v] = (ty, Var v, \env body -> Let (NonRec v env) body)
436 mkVectEnv tys vs = (ty, mkCoreTup (map Var vs),
437 \env body -> Case env (mkWildId ty) (exprType body)
438 [(DataAlt (tupleCon Boxed (length vs)), vs, body)])
442 mkLiftEnv :: Var -> [Type] -> [Var] -> VM (CoreExpr, CoreExpr -> CoreExpr -> VM CoreExpr)
443 mkLiftEnv lc [ty] [v]
444 = return (Var v, \env body ->
446 len <- lengthPA ty (Var v)
447 return . Let (NonRec v env)
448 $ Case len lc (exprType body) [(DEFAULT, [], body)])
450 -- NOTE: this transparently deals with empty environments
453 (env_tc, env_tyargs) <- parrayReprTyCon vty
455 bndrs <- if null vs then do
456 v <- newDummyVar unitTy
459 let [env_con] = tyConDataCons env_tc
461 env = Var (dataConWrapId env_con)
462 `mkTyApps` env_tyargs
463 `mkApps` (Var lc : args)
465 bind env body = let scrut = unwrapFamInstScrut env_tc env_tyargs env
467 return $ Case scrut (mkWildId (exprType scrut))
469 [(DataAlt env_con, lc : bndrs, body)]
472 vty = mkCoreTupTy tys
474 args | null vs = [Var unitDataConId]
475 | otherwise = map Var vs