2 collectAnnTypeBinders, collectAnnTypeArgs, isAnnTypeArg,
8 mkPADictType, mkPArrayType, mkPReprType,
10 parrayReprTyCon, parrayReprDataCon, mkVScrut,
12 paDictArgType, paDictOfType, paDFunType,
13 paMethod, mkPR, lengthPA, replicatePA, emptyPA, liftPA,
14 polyAbstract, polyApply, polyVApply,
15 hoistBinding, hoistExpr, hoistPolyVExpr, takeHoisted,
16 buildClosure, buildClosures,
20 #include "HsVersions.h"
34 import Id ( mkWildId )
35 import MkId ( unwrapFamInstScrut )
39 import TysPrim ( intPrimTy )
40 import BasicTypes ( Boxity(..) )
45 import Data.List ( zipWith4 )
46 import Control.Monad ( liftM, liftM2, zipWithM_ )
48 collectAnnTypeArgs :: AnnExpr b ann -> (AnnExpr b ann, [Type])
49 collectAnnTypeArgs expr = go expr []
51 go (_, AnnApp f (_, AnnType ty)) tys = go f (ty : tys)
54 collectAnnTypeBinders :: AnnExpr Var ann -> ([Var], AnnExpr Var ann)
55 collectAnnTypeBinders expr = go [] expr
57 go bs (_, AnnLam b e) | isTyVar b = go (b:bs) e
58 go bs e = (reverse bs, e)
60 collectAnnValBinders :: AnnExpr Var ann -> ([Var], AnnExpr Var ann)
61 collectAnnValBinders expr = go [] expr
63 go bs (_, AnnLam b e) | isId b = go (b:bs) e
64 go bs e = (reverse bs, e)
66 isAnnTypeArg :: AnnExpr b ann -> Bool
67 isAnnTypeArg (_, AnnType t) = True
68 isAnnTypeArg _ = False
70 mkDataConTag :: DataCon -> CoreExpr
71 mkDataConTag dc = mkConApp intDataCon [mkIntLitInt $ dataConTag dc]
73 splitUnTy :: String -> Name -> Type -> Type
75 | Just (tc, [ty']) <- splitTyConApp_maybe ty
76 , tyConName tc == name
79 | otherwise = pprPanic s (ppr ty)
81 splitBinTy :: String -> Name -> Type -> (Type, Type)
83 | Just (tc, [ty1, ty2]) <- splitTyConApp_maybe ty
84 , tyConName tc == name
87 | otherwise = pprPanic s (ppr ty)
89 splitFixedTyConApp :: TyCon -> Type -> [Type]
90 splitFixedTyConApp tc ty
91 | Just (tc', tys) <- splitTyConApp_maybe ty
95 | otherwise = pprPanic "splitFixedTyConApp" (ppr tc <+> ppr ty)
97 splitClosureTy :: Type -> (Type, Type)
98 splitClosureTy = splitBinTy "splitClosureTy" closureTyConName
100 splitPArrayTy :: Type -> Type
101 splitPArrayTy = splitUnTy "splitPArrayTy" parrayTyConName
103 mkBuiltinTyConApp :: (Builtins -> TyCon) -> [Type] -> VM Type
104 mkBuiltinTyConApp get_tc tys
107 return $ mkTyConApp tc tys
109 mkBuiltinTyConApps :: (Builtins -> TyCon) -> [Type] -> Type -> VM Type
110 mkBuiltinTyConApps get_tc tys ty
113 return $ foldr (mk tc) ty tys
115 mk tc ty1 ty2 = mkTyConApp tc [ty1,ty2]
117 mkBuiltinTyConApps1 :: (Builtins -> TyCon) -> Type -> [Type] -> VM Type
118 mkBuiltinTyConApps1 get_tc dft [] = return dft
119 mkBuiltinTyConApps1 get_tc dft tys
123 [] -> pprPanic "mkBuiltinTyConApps1" (ppr tc)
124 _ -> return $ foldr1 (mk tc) tys
126 mk tc ty1 ty2 = mkTyConApp tc [ty1,ty2]
128 mkClosureType :: Type -> Type -> VM Type
129 mkClosureType arg_ty res_ty = mkBuiltinTyConApp closureTyCon [arg_ty, res_ty]
131 mkClosureTypes :: [Type] -> Type -> VM Type
132 mkClosureTypes = mkBuiltinTyConApps closureTyCon
134 mkPReprType :: Type -> VM Type
135 mkPReprType ty = mkBuiltinTyConApp preprTyCon [ty]
137 mkPADictType :: Type -> VM Type
138 mkPADictType ty = mkBuiltinTyConApp paTyCon [ty]
140 mkPArrayType :: Type -> VM Type
141 mkPArrayType ty = mkBuiltinTyConApp parrayTyCon [ty]
143 mkBuiltinCo :: (Builtins -> TyCon) -> VM Coercion
147 return $ mkTyConApp tc []
149 parrayReprTyCon :: Type -> VM (TyCon, [Type])
150 parrayReprTyCon ty = builtin parrayTyCon >>= (`lookupFamInst` [ty])
152 parrayReprDataCon :: Type -> VM (DataCon, [Type])
155 (tc, arg_tys) <- parrayReprTyCon ty
156 let [dc] = tyConDataCons tc
159 mkVScrut :: VExpr -> VM (VExpr, TyCon, [Type])
162 (tc, arg_tys) <- parrayReprTyCon (exprType ve)
163 return ((ve, unwrapFamInstScrut tc arg_tys le), tc, arg_tys)
165 prDFunOfTyCon :: TyCon -> VM CoreExpr
167 = liftM Var (traceMaybeV "prDictOfTyCon" (ppr tycon) (lookupTyConPR tycon))
169 paDictArgType :: TyVar -> VM (Maybe Type)
170 paDictArgType tv = go (TyVarTy tv) (tyVarKind tv)
172 go ty k | Just k' <- kindView k = go ty k'
175 tv <- newTyVar FSLIT("a") k1
176 mty1 <- go (TyVarTy tv) k1
179 mty2 <- go (AppTy ty (TyVarTy tv)) k2
180 return $ fmap (ForAllTy tv . FunTy ty1) mty2
185 = liftM Just (mkPADictType ty)
187 go ty k = return Nothing
189 paDictOfType :: Type -> VM CoreExpr
190 paDictOfType ty = paDictOfTyApp ty_fn ty_args
192 (ty_fn, ty_args) = splitAppTys ty
194 paDictOfTyApp :: Type -> [Type] -> VM CoreExpr
195 paDictOfTyApp ty_fn ty_args
196 | Just ty_fn' <- coreView ty_fn = paDictOfTyApp ty_fn' ty_args
197 paDictOfTyApp (TyVarTy tv) ty_args
199 dfun <- maybeV (lookupTyVarPA tv)
200 paDFunApply dfun ty_args
201 paDictOfTyApp (TyConApp tc _) ty_args
203 dfun <- traceMaybeV "paDictOfTyApp" (ppr tc) (lookupTyConPA tc)
204 paDFunApply (Var dfun) ty_args
205 paDictOfTyApp ty ty_args = pprPanic "paDictOfTyApp" (ppr ty)
207 paDFunType :: TyCon -> VM Type
210 margs <- mapM paDictArgType tvs
211 res <- mkPADictType (mkTyConApp tc arg_tys)
212 return . mkForAllTys tvs
213 $ mkFunTys [arg | Just arg <- margs] res
216 arg_tys = mkTyVarTys tvs
218 paDFunApply :: CoreExpr -> [Type] -> VM CoreExpr
221 dicts <- mapM paDictOfType tys
222 return $ mkApps (mkTyApps dfun tys) dicts
224 type PAMethod = (Builtins -> Var, String)
226 pa_length = (lengthPAVar, "lengthPA")
227 pa_replicate = (replicatePAVar, "replicatePA")
228 pa_empty = (emptyPAVar, "emptyPA")
230 paMethod :: PAMethod -> Type -> VM CoreExpr
231 paMethod (method, name) ty
232 | Just (tycon, []) <- splitTyConApp_maybe ty
235 fn <- traceMaybeV "paMethod" (ppr tycon <+> text name)
236 $ lookupPrimMethod tycon name
239 paMethod (method, name) ty
242 dict <- paDictOfType ty
243 return $ mkApps (Var fn) [Type ty, dict]
245 mkPR :: Type -> VM CoreExpr
248 fn <- builtin mkPRVar
249 dict <- paDictOfType ty
250 return $ mkApps (Var fn) [Type ty, dict]
252 lengthPA :: CoreExpr -> VM CoreExpr
253 lengthPA x = liftM (`App` x) (paMethod pa_length ty)
255 ty = splitPArrayTy (exprType x)
257 replicatePA :: CoreExpr -> CoreExpr -> VM CoreExpr
258 replicatePA len x = liftM (`mkApps` [len,x])
259 (paMethod pa_replicate (exprType x))
261 emptyPA :: Type -> VM CoreExpr
262 emptyPA = paMethod pa_empty
264 liftPA :: CoreExpr -> VM CoreExpr
267 lc <- builtin liftingContext
268 replicatePA (Var lc) x
270 newLocalVVar :: FastString -> Type -> VM VVar
273 lty <- mkPArrayType vty
274 vv <- newLocalVar fs vty
275 lv <- newLocalVar fs lty
278 polyAbstract :: [TyVar] -> ((CoreExpr -> CoreExpr) -> VM a) -> VM a
282 mdicts <- mapM mk_dict_var tvs
283 zipWithM_ (\tv -> maybe (defLocalTyVar tv) (defLocalTyVarWithPA tv . Var)) tvs mdicts
287 r <- paDictArgType tv
289 Just ty -> liftM Just (newLocalVar FSLIT("dPA") ty)
290 Nothing -> return Nothing
292 mk_lams mdicts = mkLams (tvs ++ [dict | Just dict <- mdicts])
294 polyApply :: CoreExpr -> [Type] -> VM CoreExpr
297 dicts <- mapM paDictOfType tys
298 return $ expr `mkTyApps` tys `mkApps` dicts
300 polyVApply :: VExpr -> [Type] -> VM VExpr
303 dicts <- mapM paDictOfType tys
304 return $ mapVect (\e -> e `mkTyApps` tys `mkApps` dicts) expr
306 hoistBinding :: Var -> CoreExpr -> VM ()
307 hoistBinding v e = updGEnv $ \env ->
308 env { global_bindings = (v,e) : global_bindings env }
310 hoistExpr :: FastString -> CoreExpr -> VM Var
313 var <- newLocalVar fs (exprType expr)
314 hoistBinding var expr
317 hoistVExpr :: VExpr -> VM VVar
321 vv <- hoistExpr ('v' `consFS` fs) ve
322 lv <- hoistExpr ('l' `consFS` fs) le
325 hoistPolyVExpr :: [TyVar] -> VM VExpr -> VM VExpr
328 expr <- closedV . polyAbstract tvs $ \abstract ->
329 liftM (mapVect abstract) p
330 fn <- hoistVExpr expr
331 polyVApply (vVar fn) (mkTyVarTys tvs)
333 takeHoisted :: VM [(Var, CoreExpr)]
337 setGEnv $ env { global_bindings = [] }
338 return $ global_bindings env
340 mkClosure :: Type -> Type -> Type -> VExpr -> VExpr -> VM VExpr
341 mkClosure arg_ty res_ty env_ty (vfn,lfn) (venv,lenv)
343 dict <- paDictOfType env_ty
344 mkv <- builtin mkClosureVar
345 mkl <- builtin mkClosurePVar
346 return (Var mkv `mkTyApps` [arg_ty, res_ty, env_ty] `mkApps` [dict, vfn, lfn, venv],
347 Var mkl `mkTyApps` [arg_ty, res_ty, env_ty] `mkApps` [dict, vfn, lfn, lenv])
349 mkClosureApp :: VExpr -> VExpr -> VM VExpr
350 mkClosureApp (vclo, lclo) (varg, larg)
352 vapply <- builtin applyClosureVar
353 lapply <- builtin applyClosurePVar
354 return (Var vapply `mkTyApps` [arg_ty, res_ty] `mkApps` [vclo, varg],
355 Var lapply `mkTyApps` [arg_ty, res_ty] `mkApps` [lclo, larg])
357 (arg_ty, res_ty) = splitClosureTy (exprType vclo)
359 buildClosures :: [TyVar] -> [VVar] -> [Type] -> Type -> VM VExpr -> VM VExpr
360 buildClosures tvs vars [] res_ty mk_body
362 buildClosures tvs vars [arg_ty] res_ty mk_body
363 = buildClosure tvs vars arg_ty res_ty mk_body
364 buildClosures tvs vars (arg_ty : arg_tys) res_ty mk_body
366 res_ty' <- mkClosureTypes arg_tys res_ty
367 arg <- newLocalVVar FSLIT("x") arg_ty
368 buildClosure tvs vars arg_ty res_ty'
371 lc <- builtin liftingContext
372 clo <- buildClosures tvs (vars ++ [arg]) arg_tys res_ty mk_body
373 return $ vLams lc (vars ++ [arg]) clo
375 -- (clo <x1,...,xn> <f,f^>, aclo (Arr lc xs1 ... xsn) <f,f^>)
377 -- f = \env v -> case env of <x1,...,xn> -> e x1 ... xn v
378 -- f^ = \env v -> case env of Arr l xs1 ... xsn -> e^ l x1 ... xn v
380 buildClosure :: [TyVar] -> [VVar] -> Type -> Type -> VM VExpr -> VM VExpr
381 buildClosure tvs vars arg_ty res_ty mk_body
383 (env_ty, env, bind) <- buildEnv vars
384 env_bndr <- newLocalVVar FSLIT("env") env_ty
385 arg_bndr <- newLocalVVar FSLIT("arg") arg_ty
387 fn <- hoistPolyVExpr tvs
389 lc <- builtin liftingContext
391 body' <- bind (vVar env_bndr)
392 (vVarApps lc body (vars ++ [arg_bndr]))
393 return (vLamsWithoutLC [env_bndr, arg_bndr] body')
395 mkClosure arg_ty res_ty env_ty fn env
397 buildEnv :: [VVar] -> VM (Type, VExpr, VExpr -> VExpr -> VM VExpr)
400 lc <- builtin liftingContext
401 let (ty, venv, vbind) = mkVectEnv tys vs
402 (lenv, lbind) <- mkLiftEnv lc tys ls
403 return (ty, (venv, lenv),
404 \(venv,lenv) (vbody,lbody) ->
406 let vbody' = vbind venv vbody
407 lbody' <- lbind lenv lbody
408 return (vbody', lbody'))
413 mkVectEnv :: [Type] -> [Var] -> (Type, CoreExpr, CoreExpr -> CoreExpr -> CoreExpr)
414 mkVectEnv [] [] = (unitTy, Var unitDataConId, \env body -> body)
415 mkVectEnv [ty] [v] = (ty, Var v, \env body -> Let (NonRec v env) body)
416 mkVectEnv tys vs = (ty, mkCoreTup (map Var vs),
417 \env body -> Case env (mkWildId ty) (exprType body)
418 [(DataAlt (tupleCon Boxed (length vs)), vs, body)])
422 mkLiftEnv :: Var -> [Type] -> [Var] -> VM (CoreExpr, CoreExpr -> CoreExpr -> VM CoreExpr)
423 mkLiftEnv lc [ty] [v]
424 = return (Var v, \env body ->
426 len <- lengthPA (Var v)
427 return . Let (NonRec v env)
428 $ Case len lc (exprType body) [(DEFAULT, [], body)])
430 -- NOTE: this transparently deals with empty environments
433 (env_tc, env_tyargs) <- parrayReprTyCon vty
434 let [env_con] = tyConDataCons env_tc
436 env = Var (dataConWrapId env_con)
437 `mkTyApps` env_tyargs
438 `mkVarApps` (lc : vs)
440 bind env body = let scrut = unwrapFamInstScrut env_tc env_tyargs env
442 return $ Case scrut (mkWildId (exprType scrut))
444 [(DataAlt env_con, lc : bndrs, body)]
447 vty = mkCoreTupTy tys
449 bndrs | null vs = [mkWildId unitTy]