2 collectAnnTypeBinders, collectAnnTypeArgs, isAnnTypeArg,
6 mkPADictType, mkPArrayType,
7 parrayReprTyCon, parrayReprDataCon, mkVScrut,
8 paDictArgType, paDictOfType, paDFunType,
9 paMethod, lengthPA, replicatePA, emptyPA, liftPA,
10 polyAbstract, polyApply, polyVApply,
11 hoistBinding, hoistExpr, hoistPolyVExpr, takeHoisted,
12 buildClosure, buildClosures,
16 #include "HsVersions.h"
27 import DataCon ( DataCon, dataConWrapId, dataConTag )
29 import Id ( mkWildId )
30 import MkId ( unwrapFamInstScrut )
33 import BasicTypes ( Boxity(..) )
38 import Control.Monad ( liftM, zipWithM_ )
40 collectAnnTypeArgs :: AnnExpr b ann -> (AnnExpr b ann, [Type])
41 collectAnnTypeArgs expr = go expr []
43 go (_, AnnApp f (_, AnnType ty)) tys = go f (ty : tys)
46 collectAnnTypeBinders :: AnnExpr Var ann -> ([Var], AnnExpr Var ann)
47 collectAnnTypeBinders expr = go [] expr
49 go bs (_, AnnLam b e) | isTyVar b = go (b:bs) e
50 go bs e = (reverse bs, e)
52 collectAnnValBinders :: AnnExpr Var ann -> ([Var], AnnExpr Var ann)
53 collectAnnValBinders expr = go [] expr
55 go bs (_, AnnLam b e) | isId b = go (b:bs) e
56 go bs e = (reverse bs, e)
58 isAnnTypeArg :: AnnExpr b ann -> Bool
59 isAnnTypeArg (_, AnnType t) = True
60 isAnnTypeArg _ = False
62 mkDataConTag :: DataCon -> CoreExpr
63 mkDataConTag dc = mkConApp intDataCon [mkIntLitInt $ dataConTag dc]
65 isClosureTyCon :: TyCon -> Bool
66 isClosureTyCon tc = tyConName tc == closureTyConName
68 splitClosureTy :: Type -> (Type, Type)
70 | Just (tc, [arg_ty, res_ty]) <- splitTyConApp_maybe ty
74 | otherwise = pprPanic "splitClosureTy" (ppr ty)
76 isPArrayTyCon :: TyCon -> Bool
77 isPArrayTyCon tc = tyConName tc == parrayTyConName
79 splitPArrayTy :: Type -> Type
81 | Just (tc, [arg_ty]) <- splitTyConApp_maybe ty
85 | otherwise = pprPanic "splitPArrayTy" (ppr ty)
87 mkBuiltinTyConApp :: (Builtins -> TyCon) -> [Type] -> VM Type
88 mkBuiltinTyConApp get_tc tys
91 return $ mkTyConApp tc tys
93 mkBuiltinTyConApps :: (Builtins -> TyCon) -> [Type] -> Type -> VM Type
94 mkBuiltinTyConApps get_tc tys ty
97 return $ foldr (mk tc) ty tys
99 mk tc ty1 ty2 = mkTyConApp tc [ty1,ty2]
101 mkBuiltinTyConApps1 :: (Builtins -> TyCon) -> [Type] -> VM Type
102 mkBuiltinTyConApps1 get_tc tys
106 [] -> pprPanic "mkBuiltinTyConApps1" (ppr tc)
107 _ -> return $ foldr1 (mk tc) tys
109 mk tc ty1 ty2 = mkTyConApp tc [ty1,ty2]
111 mkClosureType :: Type -> Type -> VM Type
112 mkClosureType arg_ty res_ty = mkBuiltinTyConApp closureTyCon [arg_ty, res_ty]
114 mkClosureTypes :: [Type] -> Type -> VM Type
115 mkClosureTypes = mkBuiltinTyConApps closureTyCon
117 mkPADictType :: Type -> VM Type
118 mkPADictType ty = mkBuiltinTyConApp paTyCon [ty]
120 mkPArrayType :: Type -> VM Type
121 mkPArrayType ty = mkBuiltinTyConApp parrayTyCon [ty]
123 parrayReprTyCon :: Type -> VM (TyCon, [Type])
124 parrayReprTyCon ty = builtin parrayTyCon >>= (`lookupFamInst` [ty])
126 parrayReprDataCon :: Type -> VM (DataCon, [Type])
129 (tc, arg_tys) <- parrayReprTyCon ty
130 let [dc] = tyConDataCons tc
133 mkVScrut :: VExpr -> VM (VExpr, TyCon, [Type])
136 (tc, arg_tys) <- parrayReprTyCon (exprType ve)
137 return ((ve, unwrapFamInstScrut tc arg_tys le), tc, arg_tys)
139 paDictArgType :: TyVar -> VM (Maybe Type)
140 paDictArgType tv = go (TyVarTy tv) (tyVarKind tv)
142 go ty k | Just k' <- kindView k = go ty k'
145 tv <- newTyVar FSLIT("a") k1
146 mty1 <- go (TyVarTy tv) k1
149 mty2 <- go (AppTy ty (TyVarTy tv)) k2
150 return $ fmap (ForAllTy tv . FunTy ty1) mty2
155 = liftM Just (mkPADictType ty)
157 go ty k = return Nothing
159 paDictOfType :: Type -> VM CoreExpr
160 paDictOfType ty = paDictOfTyApp ty_fn ty_args
162 (ty_fn, ty_args) = splitAppTys ty
164 paDictOfTyApp :: Type -> [Type] -> VM CoreExpr
165 paDictOfTyApp ty_fn ty_args
166 | Just ty_fn' <- coreView ty_fn = paDictOfTyApp ty_fn' ty_args
167 paDictOfTyApp (TyVarTy tv) ty_args
169 dfun <- maybeV (lookupTyVarPA tv)
170 paDFunApply dfun ty_args
171 paDictOfTyApp (TyConApp tc _) ty_args
173 dfun <- traceMaybeV "paDictOfTyApp" (ppr tc) (lookupTyConPA tc)
174 paDFunApply (Var dfun) ty_args
175 paDictOfTyApp ty ty_args = pprPanic "paDictOfTyApp" (ppr ty)
177 paDFunType :: TyCon -> VM Type
180 margs <- mapM paDictArgType tvs
181 res <- mkPADictType (mkTyConApp tc arg_tys)
182 return . mkForAllTys tvs
183 $ mkFunTys [arg | Just arg <- margs] res
186 arg_tys = mkTyVarTys tvs
188 paDFunApply :: CoreExpr -> [Type] -> VM CoreExpr
191 dicts <- mapM paDictOfType tys
192 return $ mkApps (mkTyApps dfun tys) dicts
194 paMethod :: (Builtins -> Var) -> Type -> VM CoreExpr
198 dict <- paDictOfType ty
199 return $ mkApps (Var fn) [Type ty, dict]
201 lengthPA :: CoreExpr -> VM CoreExpr
202 lengthPA x = liftM (`App` x) (paMethod lengthPAVar ty)
204 ty = splitPArrayTy (exprType x)
206 replicatePA :: CoreExpr -> CoreExpr -> VM CoreExpr
207 replicatePA len x = liftM (`mkApps` [len,x])
208 (paMethod replicatePAVar (exprType x))
210 emptyPA :: Type -> VM CoreExpr
211 emptyPA = paMethod emptyPAVar
213 liftPA :: CoreExpr -> VM CoreExpr
216 lc <- builtin liftingContext
217 replicatePA (Var lc) x
219 newLocalVVar :: FastString -> Type -> VM VVar
222 lty <- mkPArrayType vty
223 vv <- newLocalVar fs vty
224 lv <- newLocalVar fs lty
227 polyAbstract :: [TyVar] -> ((CoreExpr -> CoreExpr) -> VM a) -> VM a
231 mdicts <- mapM mk_dict_var tvs
232 zipWithM_ (\tv -> maybe (defLocalTyVar tv) (defLocalTyVarWithPA tv . Var)) tvs mdicts
236 r <- paDictArgType tv
238 Just ty -> liftM Just (newLocalVar FSLIT("dPA") ty)
239 Nothing -> return Nothing
241 mk_lams mdicts = mkLams (tvs ++ [dict | Just dict <- mdicts])
243 polyApply :: CoreExpr -> [Type] -> VM CoreExpr
246 dicts <- mapM paDictOfType tys
247 return $ expr `mkTyApps` tys `mkApps` dicts
249 polyVApply :: VExpr -> [Type] -> VM VExpr
252 dicts <- mapM paDictOfType tys
253 return $ mapVect (\e -> e `mkTyApps` tys `mkApps` dicts) expr
255 hoistBinding :: Var -> CoreExpr -> VM ()
256 hoistBinding v e = updGEnv $ \env ->
257 env { global_bindings = (v,e) : global_bindings env }
259 hoistExpr :: FastString -> CoreExpr -> VM Var
262 var <- newLocalVar fs (exprType expr)
263 hoistBinding var expr
266 hoistVExpr :: VExpr -> VM VVar
270 vv <- hoistExpr ('v' `consFS` fs) ve
271 lv <- hoistExpr ('l' `consFS` fs) le
274 hoistPolyVExpr :: [TyVar] -> VM VExpr -> VM VExpr
277 expr <- closedV . polyAbstract tvs $ \abstract ->
278 liftM (mapVect abstract) p
279 fn <- hoistVExpr expr
280 polyVApply (vVar fn) (mkTyVarTys tvs)
282 takeHoisted :: VM [(Var, CoreExpr)]
286 setGEnv $ env { global_bindings = [] }
287 return $ global_bindings env
289 mkClosure :: Type -> Type -> Type -> VExpr -> VExpr -> VM VExpr
290 mkClosure arg_ty res_ty env_ty (vfn,lfn) (venv,lenv)
292 dict <- paDictOfType env_ty
293 mkv <- builtin mkClosureVar
294 mkl <- builtin mkClosurePVar
295 return (Var mkv `mkTyApps` [arg_ty, res_ty, env_ty] `mkApps` [dict, vfn, lfn, venv],
296 Var mkl `mkTyApps` [arg_ty, res_ty, env_ty] `mkApps` [dict, vfn, lfn, lenv])
298 mkClosureApp :: VExpr -> VExpr -> VM VExpr
299 mkClosureApp (vclo, lclo) (varg, larg)
301 vapply <- builtin applyClosureVar
302 lapply <- builtin applyClosurePVar
303 return (Var vapply `mkTyApps` [arg_ty, res_ty] `mkApps` [vclo, varg],
304 Var lapply `mkTyApps` [arg_ty, res_ty] `mkApps` [lclo, larg])
306 (arg_ty, res_ty) = splitClosureTy (exprType vclo)
308 buildClosures :: [TyVar] -> [VVar] -> [Type] -> Type -> VM VExpr -> VM VExpr
309 buildClosures tvs vars [] res_ty mk_body
311 buildClosures tvs vars [arg_ty] res_ty mk_body
312 = buildClosure tvs vars arg_ty res_ty mk_body
313 buildClosures tvs vars (arg_ty : arg_tys) res_ty mk_body
315 res_ty' <- mkClosureTypes arg_tys res_ty
316 arg <- newLocalVVar FSLIT("x") arg_ty
317 buildClosure tvs vars arg_ty res_ty'
320 lc <- builtin liftingContext
321 clo <- buildClosures tvs (vars ++ [arg]) arg_tys res_ty mk_body
322 return $ vLams lc (vars ++ [arg]) clo
324 -- (clo <x1,...,xn> <f,f^>, aclo (Arr lc xs1 ... xsn) <f,f^>)
326 -- f = \env v -> case env of <x1,...,xn> -> e x1 ... xn v
327 -- f^ = \env v -> case env of Arr l xs1 ... xsn -> e^ l x1 ... xn v
329 buildClosure :: [TyVar] -> [VVar] -> Type -> Type -> VM VExpr -> VM VExpr
330 buildClosure tvs vars arg_ty res_ty mk_body
332 (env_ty, env, bind) <- buildEnv vars
333 env_bndr <- newLocalVVar FSLIT("env") env_ty
334 arg_bndr <- newLocalVVar FSLIT("arg") arg_ty
336 fn <- hoistPolyVExpr tvs
338 lc <- builtin liftingContext
340 body' <- bind (vVar env_bndr)
341 (vVarApps lc body (vars ++ [arg_bndr]))
342 return (vLamsWithoutLC [env_bndr, arg_bndr] body')
344 mkClosure arg_ty res_ty env_ty fn env
346 buildEnv :: [VVar] -> VM (Type, VExpr, VExpr -> VExpr -> VM VExpr)
349 lc <- builtin liftingContext
350 let (ty, venv, vbind) = mkVectEnv tys vs
351 (lenv, lbind) <- mkLiftEnv lc tys ls
352 return (ty, (venv, lenv),
353 \(venv,lenv) (vbody,lbody) ->
355 let vbody' = vbind venv vbody
356 lbody' <- lbind lenv lbody
357 return (vbody', lbody'))
362 mkVectEnv :: [Type] -> [Var] -> (Type, CoreExpr, CoreExpr -> CoreExpr -> CoreExpr)
363 mkVectEnv [] [] = (unitTy, Var unitDataConId, \env body -> body)
364 mkVectEnv [ty] [v] = (ty, Var v, \env body -> Let (NonRec v env) body)
365 mkVectEnv tys vs = (ty, mkCoreTup (map Var vs),
366 \env body -> Case env (mkWildId ty) (exprType body)
367 [(DataAlt (tupleCon Boxed (length vs)), vs, body)])
371 mkLiftEnv :: Var -> [Type] -> [Var] -> VM (CoreExpr, CoreExpr -> CoreExpr -> VM CoreExpr)
372 mkLiftEnv lc [ty] [v]
373 = return (Var v, \env body ->
375 len <- lengthPA (Var v)
376 return . Let (NonRec v env)
377 $ Case len lc (exprType body) [(DEFAULT, [], body)])
379 -- NOTE: this transparently deals with empty environments
382 (env_tc, env_tyargs) <- parrayReprTyCon vty
383 let [env_con] = tyConDataCons env_tc
385 env = Var (dataConWrapId env_con)
386 `mkTyApps` env_tyargs
387 `mkVarApps` (lc : vs)
389 bind env body = let scrut = unwrapFamInstScrut env_tc env_tyargs env
391 return $ Case scrut (mkWildId (exprType scrut))
393 [(DataAlt env_con, lc : bndrs, body)]
396 vty = mkCoreTupTy tys
398 bndrs | null vs = [mkWildId unitTy]