2 collectAnnTypeBinders, collectAnnTypeArgs, isAnnTypeArg,
7 mkPADictType, mkPArrayType, mkPReprType,
9 parrayCoerce, parrayReprTyCon, parrayReprDataCon, mkVScrut,
10 prDFunOfTyCon, prCoerce,
11 paDictArgType, paDictOfType, paDFunType,
12 paMethod, mkPR, lengthPA, replicatePA, emptyPA, liftPA,
13 polyAbstract, polyApply, polyVApply,
14 hoistBinding, hoistExpr, hoistPolyVExpr, takeHoisted,
15 buildClosure, buildClosures,
19 #include "HsVersions.h"
33 import Id ( mkWildId )
34 import MkId ( unwrapFamInstScrut )
38 import TysPrim ( intPrimTy )
39 import BasicTypes ( Boxity(..) )
44 import Data.List ( zipWith4 )
45 import Control.Monad ( liftM, liftM2, zipWithM_ )
47 collectAnnTypeArgs :: AnnExpr b ann -> (AnnExpr b ann, [Type])
48 collectAnnTypeArgs expr = go expr []
50 go (_, AnnApp f (_, AnnType ty)) tys = go f (ty : tys)
53 collectAnnTypeBinders :: AnnExpr Var ann -> ([Var], AnnExpr Var ann)
54 collectAnnTypeBinders expr = go [] expr
56 go bs (_, AnnLam b e) | isTyVar b = go (b:bs) e
57 go bs e = (reverse bs, e)
59 collectAnnValBinders :: AnnExpr Var ann -> ([Var], AnnExpr Var ann)
60 collectAnnValBinders expr = go [] expr
62 go bs (_, AnnLam b e) | isId b = go (b:bs) e
63 go bs e = (reverse bs, e)
65 isAnnTypeArg :: AnnExpr b ann -> Bool
66 isAnnTypeArg (_, AnnType t) = True
67 isAnnTypeArg _ = False
69 mkDataConTag :: DataCon -> CoreExpr
70 mkDataConTag dc = mkConApp intDataCon [mkIntLitInt $ dataConTag dc]
72 splitUnTy :: String -> Name -> Type -> Type
74 | Just (tc, [ty']) <- splitTyConApp_maybe ty
75 , tyConName tc == name
78 | otherwise = pprPanic s (ppr ty)
80 splitBinTy :: String -> Name -> Type -> (Type, Type)
82 | Just (tc, [ty1, ty2]) <- splitTyConApp_maybe ty
83 , tyConName tc == name
86 | otherwise = pprPanic s (ppr ty)
88 splitFixedTyConApp :: TyCon -> Type -> [Type]
89 splitFixedTyConApp tc ty
90 | Just (tc', tys) <- splitTyConApp_maybe ty
94 | otherwise = pprPanic "splitFixedTyConApp" (ppr tc <+> ppr ty)
96 splitClosureTy :: Type -> (Type, Type)
97 splitClosureTy = splitBinTy "splitClosureTy" closureTyConName
99 splitPArrayTy :: Type -> Type
100 splitPArrayTy = splitUnTy "splitPArrayTy" parrayTyConName
102 mkBuiltinTyConApp :: (Builtins -> TyCon) -> [Type] -> VM Type
103 mkBuiltinTyConApp get_tc tys
106 return $ mkTyConApp tc tys
108 mkBuiltinTyConApps :: (Builtins -> TyCon) -> [Type] -> Type -> VM Type
109 mkBuiltinTyConApps get_tc tys ty
112 return $ foldr (mk tc) ty tys
114 mk tc ty1 ty2 = mkTyConApp tc [ty1,ty2]
116 mkBuiltinTyConApps1 :: (Builtins -> TyCon) -> Type -> [Type] -> VM Type
117 mkBuiltinTyConApps1 get_tc dft [] = return dft
118 mkBuiltinTyConApps1 get_tc dft tys
122 [] -> pprPanic "mkBuiltinTyConApps1" (ppr tc)
123 _ -> return $ foldr1 (mk tc) tys
125 mk tc ty1 ty2 = mkTyConApp tc [ty1,ty2]
127 mkClosureType :: Type -> Type -> VM Type
128 mkClosureType arg_ty res_ty = mkBuiltinTyConApp closureTyCon [arg_ty, res_ty]
130 mkClosureTypes :: [Type] -> Type -> VM Type
131 mkClosureTypes = mkBuiltinTyConApps closureTyCon
133 mkPReprType :: Type -> VM Type
134 mkPReprType ty = mkBuiltinTyConApp preprTyCon [ty]
136 mkPADictType :: Type -> VM Type
137 mkPADictType ty = mkBuiltinTyConApp paTyCon [ty]
139 mkPArrayType :: Type -> VM Type
140 mkPArrayType ty = mkBuiltinTyConApp parrayTyCon [ty]
142 parrayCoerce :: TyCon -> [Type] -> CoreExpr -> VM CoreExpr
143 parrayCoerce repr_tc args expr
144 | Just arg_co <- tyConFamilyCoercion_maybe repr_tc
146 parray <- builtin parrayTyCon
148 let co = mkAppCoercion (mkTyConApp parray [])
149 (mkSymCoercion (mkTyConApp arg_co args))
151 return $ mkCoerce co expr
153 parrayReprTyCon :: Type -> VM (TyCon, [Type])
154 parrayReprTyCon ty = builtin parrayTyCon >>= (`lookupFamInst` [ty])
156 parrayReprDataCon :: Type -> VM (DataCon, [Type])
159 (tc, arg_tys) <- parrayReprTyCon ty
160 let [dc] = tyConDataCons tc
163 mkVScrut :: VExpr -> VM (VExpr, TyCon, [Type])
166 (tc, arg_tys) <- parrayReprTyCon (exprType ve)
167 return ((ve, unwrapFamInstScrut tc arg_tys le), tc, arg_tys)
169 prDFunOfTyCon :: TyCon -> VM CoreExpr
171 = liftM Var (traceMaybeV "prDictOfTyCon" (ppr tycon) (lookupTyConPR tycon))
173 prCoerce :: TyCon -> [Type] -> CoreExpr -> VM CoreExpr
174 prCoerce repr_tc args expr
175 | Just arg_co <- tyConFamilyCoercion_maybe repr_tc
177 pr_tc <- builtin prTyCon
179 let co = mkAppCoercion (mkTyConApp pr_tc [])
180 (mkSymCoercion (mkTyConApp arg_co args))
182 return $ mkCoerce co expr
184 paDictArgType :: TyVar -> VM (Maybe Type)
185 paDictArgType tv = go (TyVarTy tv) (tyVarKind tv)
187 go ty k | Just k' <- kindView k = go ty k'
190 tv <- newTyVar FSLIT("a") k1
191 mty1 <- go (TyVarTy tv) k1
194 mty2 <- go (AppTy ty (TyVarTy tv)) k2
195 return $ fmap (ForAllTy tv . FunTy ty1) mty2
200 = liftM Just (mkPADictType ty)
202 go ty k = return Nothing
204 paDictOfType :: Type -> VM CoreExpr
205 paDictOfType ty = paDictOfTyApp ty_fn ty_args
207 (ty_fn, ty_args) = splitAppTys ty
209 paDictOfTyApp :: Type -> [Type] -> VM CoreExpr
210 paDictOfTyApp ty_fn ty_args
211 | Just ty_fn' <- coreView ty_fn = paDictOfTyApp ty_fn' ty_args
212 paDictOfTyApp (TyVarTy tv) ty_args
214 dfun <- maybeV (lookupTyVarPA tv)
215 paDFunApply dfun ty_args
216 paDictOfTyApp (TyConApp tc _) ty_args
218 dfun <- traceMaybeV "paDictOfTyApp" (ppr tc) (lookupTyConPA tc)
219 paDFunApply (Var dfun) ty_args
220 paDictOfTyApp ty ty_args = pprPanic "paDictOfTyApp" (ppr ty)
222 paDFunType :: TyCon -> VM Type
225 margs <- mapM paDictArgType tvs
226 res <- mkPADictType (mkTyConApp tc arg_tys)
227 return . mkForAllTys tvs
228 $ mkFunTys [arg | Just arg <- margs] res
231 arg_tys = mkTyVarTys tvs
233 paDFunApply :: CoreExpr -> [Type] -> VM CoreExpr
236 dicts <- mapM paDictOfType tys
237 return $ mkApps (mkTyApps dfun tys) dicts
239 paMethod :: (Builtins -> Var) -> Type -> VM CoreExpr
243 dict <- paDictOfType ty
244 return $ mkApps (Var fn) [Type ty, dict]
246 mkPR :: Type -> VM CoreExpr
247 mkPR = paMethod mkPRVar
249 lengthPA :: CoreExpr -> VM CoreExpr
250 lengthPA x = liftM (`App` x) (paMethod lengthPAVar ty)
252 ty = splitPArrayTy (exprType x)
254 replicatePA :: CoreExpr -> CoreExpr -> VM CoreExpr
255 replicatePA len x = liftM (`mkApps` [len,x])
256 (paMethod replicatePAVar (exprType x))
258 emptyPA :: Type -> VM CoreExpr
259 emptyPA = paMethod emptyPAVar
261 liftPA :: CoreExpr -> VM CoreExpr
264 lc <- builtin liftingContext
265 replicatePA (Var lc) x
267 newLocalVVar :: FastString -> Type -> VM VVar
270 lty <- mkPArrayType vty
271 vv <- newLocalVar fs vty
272 lv <- newLocalVar fs lty
275 polyAbstract :: [TyVar] -> ((CoreExpr -> CoreExpr) -> VM a) -> VM a
279 mdicts <- mapM mk_dict_var tvs
280 zipWithM_ (\tv -> maybe (defLocalTyVar tv) (defLocalTyVarWithPA tv . Var)) tvs mdicts
284 r <- paDictArgType tv
286 Just ty -> liftM Just (newLocalVar FSLIT("dPA") ty)
287 Nothing -> return Nothing
289 mk_lams mdicts = mkLams (tvs ++ [dict | Just dict <- mdicts])
291 polyApply :: CoreExpr -> [Type] -> VM CoreExpr
294 dicts <- mapM paDictOfType tys
295 return $ expr `mkTyApps` tys `mkApps` dicts
297 polyVApply :: VExpr -> [Type] -> VM VExpr
300 dicts <- mapM paDictOfType tys
301 return $ mapVect (\e -> e `mkTyApps` tys `mkApps` dicts) expr
303 hoistBinding :: Var -> CoreExpr -> VM ()
304 hoistBinding v e = updGEnv $ \env ->
305 env { global_bindings = (v,e) : global_bindings env }
307 hoistExpr :: FastString -> CoreExpr -> VM Var
310 var <- newLocalVar fs (exprType expr)
311 hoistBinding var expr
314 hoistVExpr :: VExpr -> VM VVar
318 vv <- hoistExpr ('v' `consFS` fs) ve
319 lv <- hoistExpr ('l' `consFS` fs) le
322 hoistPolyVExpr :: [TyVar] -> VM VExpr -> VM VExpr
325 expr <- closedV . polyAbstract tvs $ \abstract ->
326 liftM (mapVect abstract) p
327 fn <- hoistVExpr expr
328 polyVApply (vVar fn) (mkTyVarTys tvs)
330 takeHoisted :: VM [(Var, CoreExpr)]
334 setGEnv $ env { global_bindings = [] }
335 return $ global_bindings env
337 mkClosure :: Type -> Type -> Type -> VExpr -> VExpr -> VM VExpr
338 mkClosure arg_ty res_ty env_ty (vfn,lfn) (venv,lenv)
340 dict <- paDictOfType env_ty
341 mkv <- builtin mkClosureVar
342 mkl <- builtin mkClosurePVar
343 return (Var mkv `mkTyApps` [arg_ty, res_ty, env_ty] `mkApps` [dict, vfn, lfn, venv],
344 Var mkl `mkTyApps` [arg_ty, res_ty, env_ty] `mkApps` [dict, vfn, lfn, lenv])
346 mkClosureApp :: VExpr -> VExpr -> VM VExpr
347 mkClosureApp (vclo, lclo) (varg, larg)
349 vapply <- builtin applyClosureVar
350 lapply <- builtin applyClosurePVar
351 return (Var vapply `mkTyApps` [arg_ty, res_ty] `mkApps` [vclo, varg],
352 Var lapply `mkTyApps` [arg_ty, res_ty] `mkApps` [lclo, larg])
354 (arg_ty, res_ty) = splitClosureTy (exprType vclo)
356 buildClosures :: [TyVar] -> [VVar] -> [Type] -> Type -> VM VExpr -> VM VExpr
357 buildClosures tvs vars [] res_ty mk_body
359 buildClosures tvs vars [arg_ty] res_ty mk_body
360 = buildClosure tvs vars arg_ty res_ty mk_body
361 buildClosures tvs vars (arg_ty : arg_tys) res_ty mk_body
363 res_ty' <- mkClosureTypes arg_tys res_ty
364 arg <- newLocalVVar FSLIT("x") arg_ty
365 buildClosure tvs vars arg_ty res_ty'
368 lc <- builtin liftingContext
369 clo <- buildClosures tvs (vars ++ [arg]) arg_tys res_ty mk_body
370 return $ vLams lc (vars ++ [arg]) clo
372 -- (clo <x1,...,xn> <f,f^>, aclo (Arr lc xs1 ... xsn) <f,f^>)
374 -- f = \env v -> case env of <x1,...,xn> -> e x1 ... xn v
375 -- f^ = \env v -> case env of Arr l xs1 ... xsn -> e^ l x1 ... xn v
377 buildClosure :: [TyVar] -> [VVar] -> Type -> Type -> VM VExpr -> VM VExpr
378 buildClosure tvs vars arg_ty res_ty mk_body
380 (env_ty, env, bind) <- buildEnv vars
381 env_bndr <- newLocalVVar FSLIT("env") env_ty
382 arg_bndr <- newLocalVVar FSLIT("arg") arg_ty
384 fn <- hoistPolyVExpr tvs
386 lc <- builtin liftingContext
388 body' <- bind (vVar env_bndr)
389 (vVarApps lc body (vars ++ [arg_bndr]))
390 return (vLamsWithoutLC [env_bndr, arg_bndr] body')
392 mkClosure arg_ty res_ty env_ty fn env
394 buildEnv :: [VVar] -> VM (Type, VExpr, VExpr -> VExpr -> VM VExpr)
397 lc <- builtin liftingContext
398 let (ty, venv, vbind) = mkVectEnv tys vs
399 (lenv, lbind) <- mkLiftEnv lc tys ls
400 return (ty, (venv, lenv),
401 \(venv,lenv) (vbody,lbody) ->
403 let vbody' = vbind venv vbody
404 lbody' <- lbind lenv lbody
405 return (vbody', lbody'))
410 mkVectEnv :: [Type] -> [Var] -> (Type, CoreExpr, CoreExpr -> CoreExpr -> CoreExpr)
411 mkVectEnv [] [] = (unitTy, Var unitDataConId, \env body -> body)
412 mkVectEnv [ty] [v] = (ty, Var v, \env body -> Let (NonRec v env) body)
413 mkVectEnv tys vs = (ty, mkCoreTup (map Var vs),
414 \env body -> Case env (mkWildId ty) (exprType body)
415 [(DataAlt (tupleCon Boxed (length vs)), vs, body)])
419 mkLiftEnv :: Var -> [Type] -> [Var] -> VM (CoreExpr, CoreExpr -> CoreExpr -> VM CoreExpr)
420 mkLiftEnv lc [ty] [v]
421 = return (Var v, \env body ->
423 len <- lengthPA (Var v)
424 return . Let (NonRec v env)
425 $ Case len lc (exprType body) [(DEFAULT, [], body)])
427 -- NOTE: this transparently deals with empty environments
430 (env_tc, env_tyargs) <- parrayReprTyCon vty
431 let [env_con] = tyConDataCons env_tc
433 env = Var (dataConWrapId env_con)
434 `mkTyApps` env_tyargs
435 `mkVarApps` (lc : vs)
437 bind env body = let scrut = unwrapFamInstScrut env_tc env_tyargs env
439 return $ Case scrut (mkWildId (exprType scrut))
441 [(DataAlt env_con, lc : bndrs, body)]
444 vty = mkCoreTupTy tys
446 bndrs | null vs = [mkWildId unitTy]