2 collectAnnTypeBinders, collectAnnTypeArgs, isAnnTypeArg,
6 mkPADictType, mkPArrayType,
7 paDictArgType, paDictOfType, paDFunType,
8 paMethod, lengthPA, replicatePA, emptyPA, liftPA,
9 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 mkClosureType :: Type -> Type -> VM Type
88 mkClosureType arg_ty res_ty
90 tc <- builtin closureTyCon
91 return $ mkTyConApp tc [arg_ty, res_ty]
93 mkClosureTypes :: [Type] -> Type -> VM Type
94 mkClosureTypes arg_tys res_ty
96 tc <- builtin closureTyCon
97 return $ foldr (mk tc) res_ty arg_tys
99 mk tc arg_ty res_ty = mkTyConApp tc [arg_ty, res_ty]
101 mkPADictType :: Type -> VM Type
104 tc <- builtin paTyCon
105 return $ TyConApp tc [ty]
107 mkPArrayType :: Type -> VM Type
110 tc <- builtin parrayTyCon
111 return $ TyConApp tc [ty]
113 paDictArgType :: TyVar -> VM (Maybe Type)
114 paDictArgType tv = go (TyVarTy tv) (tyVarKind tv)
116 go ty k | Just k' <- kindView k = go ty k'
119 tv <- newTyVar FSLIT("a") k1
120 mty1 <- go (TyVarTy tv) k1
123 mty2 <- go (AppTy ty (TyVarTy tv)) k2
124 return $ fmap (ForAllTy tv . FunTy ty1) mty2
129 = liftM Just (mkPADictType ty)
131 go ty k = return Nothing
133 paDictOfType :: Type -> VM CoreExpr
134 paDictOfType ty = paDictOfTyApp ty_fn ty_args
136 (ty_fn, ty_args) = splitAppTys ty
138 paDictOfTyApp :: Type -> [Type] -> VM CoreExpr
139 paDictOfTyApp ty_fn ty_args
140 | Just ty_fn' <- coreView ty_fn = paDictOfTyApp ty_fn' ty_args
141 paDictOfTyApp (TyVarTy tv) ty_args
143 dfun <- maybeV (lookupTyVarPA tv)
144 paDFunApply dfun ty_args
145 paDictOfTyApp (TyConApp tc _) ty_args
147 dfun <- traceMaybeV "paDictOfTyApp" (ppr tc) (lookupTyConPA tc)
148 paDFunApply (Var dfun) ty_args
149 paDictOfTyApp ty ty_args = pprPanic "paDictOfTyApp" (ppr ty)
151 paDFunType :: TyCon -> VM Type
154 margs <- mapM paDictArgType tvs
155 res <- mkPADictType (mkTyConApp tc arg_tys)
156 return . mkForAllTys tvs
157 $ mkFunTys [arg | Just arg <- margs] res
160 arg_tys = mkTyVarTys tvs
162 paDFunApply :: CoreExpr -> [Type] -> VM CoreExpr
165 dicts <- mapM paDictOfType tys
166 return $ mkApps (mkTyApps dfun tys) dicts
168 paMethod :: (Builtins -> Var) -> Type -> VM CoreExpr
172 dict <- paDictOfType ty
173 return $ mkApps (Var fn) [Type ty, dict]
175 lengthPA :: CoreExpr -> VM CoreExpr
176 lengthPA x = liftM (`App` x) (paMethod lengthPAVar ty)
178 ty = splitPArrayTy (exprType x)
180 replicatePA :: CoreExpr -> CoreExpr -> VM CoreExpr
181 replicatePA len x = liftM (`mkApps` [len,x])
182 (paMethod replicatePAVar (exprType x))
184 emptyPA :: Type -> VM CoreExpr
185 emptyPA = paMethod emptyPAVar
187 liftPA :: CoreExpr -> VM CoreExpr
190 lc <- builtin liftingContext
191 replicatePA (Var lc) x
193 newLocalVVar :: FastString -> Type -> VM VVar
196 lty <- mkPArrayType vty
197 vv <- newLocalVar fs vty
198 lv <- newLocalVar fs lty
201 polyAbstract :: [TyVar] -> ((CoreExpr -> CoreExpr) -> VM a) -> VM a
205 mdicts <- mapM mk_dict_var tvs
206 zipWithM_ (\tv -> maybe (defLocalTyVar tv) (defLocalTyVarWithPA tv . Var)) tvs mdicts
210 r <- paDictArgType tv
212 Just ty -> liftM Just (newLocalVar FSLIT("dPA") ty)
213 Nothing -> return Nothing
215 mk_lams mdicts = mkLams (tvs ++ [dict | Just dict <- mdicts])
217 polyApply :: CoreExpr -> [Type] -> VM CoreExpr
220 dicts <- mapM paDictOfType tys
221 return $ expr `mkTyApps` tys `mkApps` dicts
223 polyVApply :: VExpr -> [Type] -> VM VExpr
226 dicts <- mapM paDictOfType tys
227 return $ mapVect (\e -> e `mkTyApps` tys `mkApps` dicts) expr
229 lookupPArrayFamInst :: Type -> VM (TyCon, [Type])
230 lookupPArrayFamInst ty = builtin parrayTyCon >>= (`lookupFamInst` [ty])
232 hoistBinding :: Var -> CoreExpr -> VM ()
233 hoistBinding v e = updGEnv $ \env ->
234 env { global_bindings = (v,e) : global_bindings env }
236 hoistExpr :: FastString -> CoreExpr -> VM Var
239 var <- newLocalVar fs (exprType expr)
240 hoistBinding var expr
243 hoistVExpr :: VExpr -> VM VVar
247 vv <- hoistExpr ('v' `consFS` fs) ve
248 lv <- hoistExpr ('l' `consFS` fs) le
251 hoistPolyVExpr :: [TyVar] -> VM VExpr -> VM VExpr
254 expr <- closedV . polyAbstract tvs $ \abstract ->
255 liftM (mapVect abstract) p
256 fn <- hoistVExpr expr
257 polyVApply (vVar fn) (mkTyVarTys tvs)
259 takeHoisted :: VM [(Var, CoreExpr)]
263 setGEnv $ env { global_bindings = [] }
264 return $ global_bindings env
266 mkClosure :: Type -> Type -> Type -> VExpr -> VExpr -> VM VExpr
267 mkClosure arg_ty res_ty env_ty (vfn,lfn) (venv,lenv)
269 dict <- paDictOfType env_ty
270 mkv <- builtin mkClosureVar
271 mkl <- builtin mkClosurePVar
272 return (Var mkv `mkTyApps` [arg_ty, res_ty, env_ty] `mkApps` [dict, vfn, lfn, venv],
273 Var mkl `mkTyApps` [arg_ty, res_ty, env_ty] `mkApps` [dict, vfn, lfn, lenv])
275 mkClosureApp :: VExpr -> VExpr -> VM VExpr
276 mkClosureApp (vclo, lclo) (varg, larg)
278 vapply <- builtin applyClosureVar
279 lapply <- builtin applyClosurePVar
280 return (Var vapply `mkTyApps` [arg_ty, res_ty] `mkApps` [vclo, varg],
281 Var lapply `mkTyApps` [arg_ty, res_ty] `mkApps` [lclo, larg])
283 (arg_ty, res_ty) = splitClosureTy (exprType vclo)
285 buildClosures :: [TyVar] -> [VVar] -> [Type] -> Type -> VM VExpr -> VM VExpr
286 buildClosures tvs vars [arg_ty] res_ty mk_body
287 = buildClosure tvs vars arg_ty res_ty mk_body
288 buildClosures tvs vars (arg_ty : arg_tys) res_ty mk_body
290 res_ty' <- mkClosureTypes arg_tys res_ty
291 arg <- newLocalVVar FSLIT("x") arg_ty
292 buildClosure tvs vars arg_ty res_ty'
295 lc <- builtin liftingContext
296 clo <- buildClosures tvs (vars ++ [arg]) arg_tys res_ty mk_body
297 return $ vLams lc (vars ++ [arg]) clo
299 -- (clo <x1,...,xn> <f,f^>, aclo (Arr lc xs1 ... xsn) <f,f^>)
301 -- f = \env v -> case env of <x1,...,xn> -> e x1 ... xn v
302 -- f^ = \env v -> case env of Arr l xs1 ... xsn -> e^ l x1 ... xn v
304 buildClosure :: [TyVar] -> [VVar] -> Type -> Type -> VM VExpr -> VM VExpr
305 buildClosure tvs vars arg_ty res_ty mk_body
307 (env_ty, env, bind) <- buildEnv vars
308 env_bndr <- newLocalVVar FSLIT("env") env_ty
309 arg_bndr <- newLocalVVar FSLIT("arg") arg_ty
311 fn <- hoistPolyVExpr tvs
313 lc <- builtin liftingContext
315 body' <- bind (vVar env_bndr)
316 (vVarApps lc body (vars ++ [arg_bndr]))
317 return (vLamsWithoutLC [env_bndr, arg_bndr] body')
319 mkClosure arg_ty res_ty env_ty fn env
321 buildEnv :: [VVar] -> VM (Type, VExpr, VExpr -> VExpr -> VM VExpr)
324 lc <- builtin liftingContext
325 let (ty, venv, vbind) = mkVectEnv tys vs
326 (lenv, lbind) <- mkLiftEnv lc tys ls
327 return (ty, (venv, lenv),
328 \(venv,lenv) (vbody,lbody) ->
330 let vbody' = vbind venv vbody
331 lbody' <- lbind lenv lbody
332 return (vbody', lbody'))
337 mkVectEnv :: [Type] -> [Var] -> (Type, CoreExpr, CoreExpr -> CoreExpr -> CoreExpr)
338 mkVectEnv [] [] = (unitTy, Var unitDataConId, \env body -> body)
339 mkVectEnv [ty] [v] = (ty, Var v, \env body -> Let (NonRec v env) body)
340 mkVectEnv tys vs = (ty, mkCoreTup (map Var vs),
341 \env body -> Case env (mkWildId ty) (exprType body)
342 [(DataAlt (tupleCon Boxed (length vs)), vs, body)])
346 mkLiftEnv :: Var -> [Type] -> [Var] -> VM (CoreExpr, CoreExpr -> CoreExpr -> VM CoreExpr)
347 mkLiftEnv lc [ty] [v]
348 = return (Var v, \env body ->
350 len <- lengthPA (Var v)
351 return . Let (NonRec v env)
352 $ Case len lc (exprType body) [(DEFAULT, [], body)])
354 -- NOTE: this transparently deals with empty environments
357 (env_tc, env_tyargs) <- lookupPArrayFamInst vty
358 let [env_con] = tyConDataCons env_tc
360 env = Var (dataConWrapId env_con)
361 `mkTyApps` env_tyargs
362 `mkVarApps` (lc : vs)
364 bind env body = let scrut = unwrapFamInstScrut env_tc env_tyargs env
366 return $ Case scrut (mkWildId (exprType scrut))
368 [(DataAlt env_con, lc : bndrs, body)]
371 vty = mkCoreTupTy tys
373 bndrs | null vs = [mkWildId unitTy]