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 paMethod :: (Builtins -> Var) -> Type -> VM CoreExpr
228 dict <- paDictOfType ty
229 return $ mkApps (Var fn) [Type ty, dict]
231 mkPR :: Type -> VM CoreExpr
232 mkPR = paMethod mkPRVar
234 lengthPA :: CoreExpr -> VM CoreExpr
235 lengthPA x = liftM (`App` x) (paMethod lengthPAVar ty)
237 ty = splitPArrayTy (exprType x)
239 replicatePA :: CoreExpr -> CoreExpr -> VM CoreExpr
240 replicatePA len x = liftM (`mkApps` [len,x])
241 (paMethod replicatePAVar (exprType x))
243 emptyPA :: Type -> VM CoreExpr
244 emptyPA = paMethod emptyPAVar
246 liftPA :: CoreExpr -> VM CoreExpr
249 lc <- builtin liftingContext
250 replicatePA (Var lc) x
252 newLocalVVar :: FastString -> Type -> VM VVar
255 lty <- mkPArrayType vty
256 vv <- newLocalVar fs vty
257 lv <- newLocalVar fs lty
260 polyAbstract :: [TyVar] -> ((CoreExpr -> CoreExpr) -> VM a) -> VM a
264 mdicts <- mapM mk_dict_var tvs
265 zipWithM_ (\tv -> maybe (defLocalTyVar tv) (defLocalTyVarWithPA tv . Var)) tvs mdicts
269 r <- paDictArgType tv
271 Just ty -> liftM Just (newLocalVar FSLIT("dPA") ty)
272 Nothing -> return Nothing
274 mk_lams mdicts = mkLams (tvs ++ [dict | Just dict <- mdicts])
276 polyApply :: CoreExpr -> [Type] -> VM CoreExpr
279 dicts <- mapM paDictOfType tys
280 return $ expr `mkTyApps` tys `mkApps` dicts
282 polyVApply :: VExpr -> [Type] -> VM VExpr
285 dicts <- mapM paDictOfType tys
286 return $ mapVect (\e -> e `mkTyApps` tys `mkApps` dicts) expr
288 hoistBinding :: Var -> CoreExpr -> VM ()
289 hoistBinding v e = updGEnv $ \env ->
290 env { global_bindings = (v,e) : global_bindings env }
292 hoistExpr :: FastString -> CoreExpr -> VM Var
295 var <- newLocalVar fs (exprType expr)
296 hoistBinding var expr
299 hoistVExpr :: VExpr -> VM VVar
303 vv <- hoistExpr ('v' `consFS` fs) ve
304 lv <- hoistExpr ('l' `consFS` fs) le
307 hoistPolyVExpr :: [TyVar] -> VM VExpr -> VM VExpr
310 expr <- closedV . polyAbstract tvs $ \abstract ->
311 liftM (mapVect abstract) p
312 fn <- hoistVExpr expr
313 polyVApply (vVar fn) (mkTyVarTys tvs)
315 takeHoisted :: VM [(Var, CoreExpr)]
319 setGEnv $ env { global_bindings = [] }
320 return $ global_bindings env
322 mkClosure :: Type -> Type -> Type -> VExpr -> VExpr -> VM VExpr
323 mkClosure arg_ty res_ty env_ty (vfn,lfn) (venv,lenv)
325 dict <- paDictOfType env_ty
326 mkv <- builtin mkClosureVar
327 mkl <- builtin mkClosurePVar
328 return (Var mkv `mkTyApps` [arg_ty, res_ty, env_ty] `mkApps` [dict, vfn, lfn, venv],
329 Var mkl `mkTyApps` [arg_ty, res_ty, env_ty] `mkApps` [dict, vfn, lfn, lenv])
331 mkClosureApp :: VExpr -> VExpr -> VM VExpr
332 mkClosureApp (vclo, lclo) (varg, larg)
334 vapply <- builtin applyClosureVar
335 lapply <- builtin applyClosurePVar
336 return (Var vapply `mkTyApps` [arg_ty, res_ty] `mkApps` [vclo, varg],
337 Var lapply `mkTyApps` [arg_ty, res_ty] `mkApps` [lclo, larg])
339 (arg_ty, res_ty) = splitClosureTy (exprType vclo)
341 buildClosures :: [TyVar] -> [VVar] -> [Type] -> Type -> VM VExpr -> VM VExpr
342 buildClosures tvs vars [] res_ty mk_body
344 buildClosures tvs vars [arg_ty] res_ty mk_body
345 = buildClosure tvs vars arg_ty res_ty mk_body
346 buildClosures tvs vars (arg_ty : arg_tys) res_ty mk_body
348 res_ty' <- mkClosureTypes arg_tys res_ty
349 arg <- newLocalVVar FSLIT("x") arg_ty
350 buildClosure tvs vars arg_ty res_ty'
353 lc <- builtin liftingContext
354 clo <- buildClosures tvs (vars ++ [arg]) arg_tys res_ty mk_body
355 return $ vLams lc (vars ++ [arg]) clo
357 -- (clo <x1,...,xn> <f,f^>, aclo (Arr lc xs1 ... xsn) <f,f^>)
359 -- f = \env v -> case env of <x1,...,xn> -> e x1 ... xn v
360 -- f^ = \env v -> case env of Arr l xs1 ... xsn -> e^ l x1 ... xn v
362 buildClosure :: [TyVar] -> [VVar] -> Type -> Type -> VM VExpr -> VM VExpr
363 buildClosure tvs vars arg_ty res_ty mk_body
365 (env_ty, env, bind) <- buildEnv vars
366 env_bndr <- newLocalVVar FSLIT("env") env_ty
367 arg_bndr <- newLocalVVar FSLIT("arg") arg_ty
369 fn <- hoistPolyVExpr tvs
371 lc <- builtin liftingContext
373 body' <- bind (vVar env_bndr)
374 (vVarApps lc body (vars ++ [arg_bndr]))
375 return (vLamsWithoutLC [env_bndr, arg_bndr] body')
377 mkClosure arg_ty res_ty env_ty fn env
379 buildEnv :: [VVar] -> VM (Type, VExpr, VExpr -> VExpr -> VM VExpr)
382 lc <- builtin liftingContext
383 let (ty, venv, vbind) = mkVectEnv tys vs
384 (lenv, lbind) <- mkLiftEnv lc tys ls
385 return (ty, (venv, lenv),
386 \(venv,lenv) (vbody,lbody) ->
388 let vbody' = vbind venv vbody
389 lbody' <- lbind lenv lbody
390 return (vbody', lbody'))
395 mkVectEnv :: [Type] -> [Var] -> (Type, CoreExpr, CoreExpr -> CoreExpr -> CoreExpr)
396 mkVectEnv [] [] = (unitTy, Var unitDataConId, \env body -> body)
397 mkVectEnv [ty] [v] = (ty, Var v, \env body -> Let (NonRec v env) body)
398 mkVectEnv tys vs = (ty, mkCoreTup (map Var vs),
399 \env body -> Case env (mkWildId ty) (exprType body)
400 [(DataAlt (tupleCon Boxed (length vs)), vs, body)])
404 mkLiftEnv :: Var -> [Type] -> [Var] -> VM (CoreExpr, CoreExpr -> CoreExpr -> VM CoreExpr)
405 mkLiftEnv lc [ty] [v]
406 = return (Var v, \env body ->
408 len <- lengthPA (Var v)
409 return . Let (NonRec v env)
410 $ Case len lc (exprType body) [(DEFAULT, [], body)])
412 -- NOTE: this transparently deals with empty environments
415 (env_tc, env_tyargs) <- parrayReprTyCon vty
416 let [env_con] = tyConDataCons env_tc
418 env = Var (dataConWrapId env_con)
419 `mkTyApps` env_tyargs
420 `mkVarApps` (lc : vs)
422 bind env body = let scrut = unwrapFamInstScrut env_tc env_tyargs env
424 return $ Case scrut (mkWildId (exprType scrut))
426 [(DataAlt env_con, lc : bndrs, body)]
429 vty = mkCoreTupTy tys
431 bndrs | null vs = [mkWildId unitTy]