2 collectAnnTypeBinders, collectAnnTypeArgs, isAnnTypeArg,
4 mkDataConTag, mkDataConTagLit,
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(..) )
41 import Literal ( Literal, mkMachInt )
46 import Data.List ( zipWith4 )
47 import Control.Monad ( liftM, liftM2, zipWithM_ )
49 collectAnnTypeArgs :: AnnExpr b ann -> (AnnExpr b ann, [Type])
50 collectAnnTypeArgs expr = go expr []
52 go (_, AnnApp f (_, AnnType ty)) tys = go f (ty : tys)
55 collectAnnTypeBinders :: AnnExpr Var ann -> ([Var], AnnExpr Var ann)
56 collectAnnTypeBinders expr = go [] expr
58 go bs (_, AnnLam b e) | isTyVar b = go (b:bs) e
59 go bs e = (reverse bs, e)
61 collectAnnValBinders :: AnnExpr Var ann -> ([Var], AnnExpr Var ann)
62 collectAnnValBinders expr = go [] expr
64 go bs (_, AnnLam b e) | isId b = go (b:bs) e
65 go bs e = (reverse bs, e)
67 isAnnTypeArg :: AnnExpr b ann -> Bool
68 isAnnTypeArg (_, AnnType t) = True
69 isAnnTypeArg _ = False
71 mkDataConTagLit :: DataCon -> Literal
73 = mkMachInt . toInteger $ dataConTag con - fIRST_TAG
75 mkDataConTag :: DataCon -> CoreExpr
76 mkDataConTag con = mkIntLitInt (dataConTag con - fIRST_TAG)
78 splitUnTy :: String -> Name -> Type -> Type
80 | Just (tc, [ty']) <- splitTyConApp_maybe ty
81 , tyConName tc == name
84 | otherwise = pprPanic s (ppr ty)
86 splitBinTy :: String -> Name -> Type -> (Type, Type)
88 | Just (tc, [ty1, ty2]) <- splitTyConApp_maybe ty
89 , tyConName tc == name
92 | otherwise = pprPanic s (ppr ty)
94 splitFixedTyConApp :: TyCon -> Type -> [Type]
95 splitFixedTyConApp tc ty
96 | Just (tc', tys) <- splitTyConApp_maybe ty
100 | otherwise = pprPanic "splitFixedTyConApp" (ppr tc <+> ppr ty)
102 splitClosureTy :: Type -> (Type, Type)
103 splitClosureTy = splitBinTy "splitClosureTy" closureTyConName
105 splitPArrayTy :: Type -> Type
106 splitPArrayTy = splitUnTy "splitPArrayTy" parrayTyConName
108 splitPrimTyCon :: Type -> Maybe TyCon
110 | Just (tycon, []) <- splitTyConApp_maybe ty
114 | otherwise = Nothing
116 mkBuiltinTyConApp :: (Builtins -> TyCon) -> [Type] -> VM Type
117 mkBuiltinTyConApp get_tc tys
120 return $ mkTyConApp tc tys
122 mkBuiltinTyConApps :: (Builtins -> TyCon) -> [Type] -> Type -> VM Type
123 mkBuiltinTyConApps get_tc tys ty
126 return $ foldr (mk tc) ty tys
128 mk tc ty1 ty2 = mkTyConApp tc [ty1,ty2]
130 mkBuiltinTyConApps1 :: (Builtins -> TyCon) -> Type -> [Type] -> VM Type
131 mkBuiltinTyConApps1 get_tc dft [] = return dft
132 mkBuiltinTyConApps1 get_tc dft tys
136 [] -> pprPanic "mkBuiltinTyConApps1" (ppr tc)
137 _ -> return $ foldr1 (mk tc) tys
139 mk tc ty1 ty2 = mkTyConApp tc [ty1,ty2]
141 mkClosureType :: Type -> Type -> VM Type
142 mkClosureType arg_ty res_ty = mkBuiltinTyConApp closureTyCon [arg_ty, res_ty]
144 mkClosureTypes :: [Type] -> Type -> VM Type
145 mkClosureTypes = mkBuiltinTyConApps closureTyCon
147 mkPReprType :: Type -> VM Type
148 mkPReprType ty = mkBuiltinTyConApp preprTyCon [ty]
150 mkPADictType :: Type -> VM Type
151 mkPADictType ty = mkBuiltinTyConApp paTyCon [ty]
153 mkPArrayType :: Type -> VM Type
155 | Just tycon <- splitPrimTyCon ty
157 arr <- traceMaybeV "mkPArrayType" (ppr tycon)
158 $ lookupPrimPArray tycon
159 return $ mkTyConApp arr []
160 mkPArrayType ty = mkBuiltinTyConApp parrayTyCon [ty]
162 mkBuiltinCo :: (Builtins -> TyCon) -> VM Coercion
166 return $ mkTyConApp tc []
168 parrayReprTyCon :: Type -> VM (TyCon, [Type])
169 parrayReprTyCon ty = builtin parrayTyCon >>= (`lookupFamInst` [ty])
171 parrayReprDataCon :: Type -> VM (DataCon, [Type])
174 (tc, arg_tys) <- parrayReprTyCon ty
175 let [dc] = tyConDataCons tc
178 mkVScrut :: VExpr -> VM (VExpr, TyCon, [Type])
181 (tc, arg_tys) <- parrayReprTyCon (exprType ve)
182 return ((ve, unwrapFamInstScrut tc arg_tys le), tc, arg_tys)
184 prDFunOfTyCon :: TyCon -> VM CoreExpr
186 = liftM Var (traceMaybeV "prDictOfTyCon" (ppr tycon) (lookupTyConPR tycon))
188 paDictArgType :: TyVar -> VM (Maybe Type)
189 paDictArgType tv = go (TyVarTy tv) (tyVarKind tv)
191 go ty k | Just k' <- kindView k = go ty k'
194 tv <- newTyVar FSLIT("a") k1
195 mty1 <- go (TyVarTy tv) k1
198 mty2 <- go (AppTy ty (TyVarTy tv)) k2
199 return $ fmap (ForAllTy tv . FunTy ty1) mty2
204 = liftM Just (mkPADictType ty)
206 go ty k = return Nothing
208 paDictOfType :: Type -> VM CoreExpr
209 paDictOfType ty = paDictOfTyApp ty_fn ty_args
211 (ty_fn, ty_args) = splitAppTys ty
213 paDictOfTyApp :: Type -> [Type] -> VM CoreExpr
214 paDictOfTyApp ty_fn ty_args
215 | Just ty_fn' <- coreView ty_fn = paDictOfTyApp ty_fn' ty_args
216 paDictOfTyApp (TyVarTy tv) ty_args
218 dfun <- maybeV (lookupTyVarPA tv)
219 paDFunApply dfun ty_args
220 paDictOfTyApp (TyConApp tc _) ty_args
222 dfun <- traceMaybeV "paDictOfTyApp" (ppr tc) (lookupTyConPA tc)
223 paDFunApply (Var dfun) ty_args
224 paDictOfTyApp ty ty_args = pprPanic "paDictOfTyApp" (ppr ty)
226 paDFunType :: TyCon -> VM Type
229 margs <- mapM paDictArgType tvs
230 res <- mkPADictType (mkTyConApp tc arg_tys)
231 return . mkForAllTys tvs
232 $ mkFunTys [arg | Just arg <- margs] res
235 arg_tys = mkTyVarTys tvs
237 paDFunApply :: CoreExpr -> [Type] -> VM CoreExpr
240 dicts <- mapM paDictOfType tys
241 return $ mkApps (mkTyApps dfun tys) dicts
243 type PAMethod = (Builtins -> Var, String)
245 pa_length = (lengthPAVar, "lengthPA")
246 pa_replicate = (replicatePAVar, "replicatePA")
247 pa_empty = (emptyPAVar, "emptyPA")
249 paMethod :: PAMethod -> Type -> VM CoreExpr
250 paMethod (method, name) ty
251 | Just tycon <- splitPrimTyCon ty
253 fn <- traceMaybeV "paMethod" (ppr tycon <+> text name)
254 $ lookupPrimMethod tycon name
257 paMethod (method, name) ty
260 dict <- paDictOfType ty
261 return $ mkApps (Var fn) [Type ty, dict]
263 mkPR :: Type -> VM CoreExpr
266 fn <- builtin mkPRVar
267 dict <- paDictOfType ty
268 return $ mkApps (Var fn) [Type ty, dict]
270 lengthPA :: CoreExpr -> VM CoreExpr
271 lengthPA x = liftM (`App` x) (paMethod pa_length ty)
273 ty = splitPArrayTy (exprType x)
275 replicatePA :: CoreExpr -> CoreExpr -> VM CoreExpr
276 replicatePA len x = liftM (`mkApps` [len,x])
277 (paMethod pa_replicate (exprType x))
279 emptyPA :: Type -> VM CoreExpr
280 emptyPA = paMethod pa_empty
282 liftPA :: CoreExpr -> VM CoreExpr
285 lc <- builtin liftingContext
286 replicatePA (Var lc) x
288 newLocalVVar :: FastString -> Type -> VM VVar
291 lty <- mkPArrayType vty
292 vv <- newLocalVar fs vty
293 lv <- newLocalVar fs lty
296 polyAbstract :: [TyVar] -> ((CoreExpr -> CoreExpr) -> VM a) -> VM a
300 mdicts <- mapM mk_dict_var tvs
301 zipWithM_ (\tv -> maybe (defLocalTyVar tv) (defLocalTyVarWithPA tv . Var)) tvs mdicts
305 r <- paDictArgType tv
307 Just ty -> liftM Just (newLocalVar FSLIT("dPA") ty)
308 Nothing -> return Nothing
310 mk_lams mdicts = mkLams (tvs ++ [dict | Just dict <- mdicts])
312 polyApply :: CoreExpr -> [Type] -> VM CoreExpr
315 dicts <- mapM paDictOfType tys
316 return $ expr `mkTyApps` tys `mkApps` dicts
318 polyVApply :: VExpr -> [Type] -> VM VExpr
321 dicts <- mapM paDictOfType tys
322 return $ mapVect (\e -> e `mkTyApps` tys `mkApps` dicts) expr
324 hoistBinding :: Var -> CoreExpr -> VM ()
325 hoistBinding v e = updGEnv $ \env ->
326 env { global_bindings = (v,e) : global_bindings env }
328 hoistExpr :: FastString -> CoreExpr -> VM Var
331 var <- newLocalVar fs (exprType expr)
332 hoistBinding var expr
335 hoistVExpr :: VExpr -> VM VVar
339 vv <- hoistExpr ('v' `consFS` fs) ve
340 lv <- hoistExpr ('l' `consFS` fs) le
343 hoistPolyVExpr :: [TyVar] -> VM VExpr -> VM VExpr
346 expr <- closedV . polyAbstract tvs $ \abstract ->
347 liftM (mapVect abstract) p
348 fn <- hoistVExpr expr
349 polyVApply (vVar fn) (mkTyVarTys tvs)
351 takeHoisted :: VM [(Var, CoreExpr)]
355 setGEnv $ env { global_bindings = [] }
356 return $ global_bindings env
358 mkClosure :: Type -> Type -> Type -> VExpr -> VExpr -> VM VExpr
359 mkClosure arg_ty res_ty env_ty (vfn,lfn) (venv,lenv)
361 dict <- paDictOfType env_ty
362 mkv <- builtin mkClosureVar
363 mkl <- builtin mkClosurePVar
364 return (Var mkv `mkTyApps` [arg_ty, res_ty, env_ty] `mkApps` [dict, vfn, lfn, venv],
365 Var mkl `mkTyApps` [arg_ty, res_ty, env_ty] `mkApps` [dict, vfn, lfn, lenv])
367 mkClosureApp :: VExpr -> VExpr -> VM VExpr
368 mkClosureApp (vclo, lclo) (varg, larg)
370 vapply <- builtin applyClosureVar
371 lapply <- builtin applyClosurePVar
372 return (Var vapply `mkTyApps` [arg_ty, res_ty] `mkApps` [vclo, varg],
373 Var lapply `mkTyApps` [arg_ty, res_ty] `mkApps` [lclo, larg])
375 (arg_ty, res_ty) = splitClosureTy (exprType vclo)
377 buildClosures :: [TyVar] -> [VVar] -> [Type] -> Type -> VM VExpr -> VM VExpr
378 buildClosures tvs vars [] res_ty mk_body
380 buildClosures tvs vars [arg_ty] res_ty mk_body
381 = buildClosure tvs vars arg_ty res_ty mk_body
382 buildClosures tvs vars (arg_ty : arg_tys) res_ty mk_body
384 res_ty' <- mkClosureTypes arg_tys res_ty
385 arg <- newLocalVVar FSLIT("x") arg_ty
386 buildClosure tvs vars arg_ty res_ty'
389 lc <- builtin liftingContext
390 clo <- buildClosures tvs (vars ++ [arg]) arg_tys res_ty mk_body
391 return $ vLams lc (vars ++ [arg]) clo
393 -- (clo <x1,...,xn> <f,f^>, aclo (Arr lc xs1 ... xsn) <f,f^>)
395 -- f = \env v -> case env of <x1,...,xn> -> e x1 ... xn v
396 -- f^ = \env v -> case env of Arr l xs1 ... xsn -> e^ l x1 ... xn v
398 buildClosure :: [TyVar] -> [VVar] -> Type -> Type -> VM VExpr -> VM VExpr
399 buildClosure tvs vars arg_ty res_ty mk_body
401 (env_ty, env, bind) <- buildEnv vars
402 env_bndr <- newLocalVVar FSLIT("env") env_ty
403 arg_bndr <- newLocalVVar FSLIT("arg") arg_ty
405 fn <- hoistPolyVExpr tvs
407 lc <- builtin liftingContext
409 body' <- bind (vVar env_bndr)
410 (vVarApps lc body (vars ++ [arg_bndr]))
411 return (vLamsWithoutLC [env_bndr, arg_bndr] body')
413 mkClosure arg_ty res_ty env_ty fn env
415 buildEnv :: [VVar] -> VM (Type, VExpr, VExpr -> VExpr -> VM VExpr)
418 lc <- builtin liftingContext
419 let (ty, venv, vbind) = mkVectEnv tys vs
420 (lenv, lbind) <- mkLiftEnv lc tys ls
421 return (ty, (venv, lenv),
422 \(venv,lenv) (vbody,lbody) ->
424 let vbody' = vbind venv vbody
425 lbody' <- lbind lenv lbody
426 return (vbody', lbody'))
431 mkVectEnv :: [Type] -> [Var] -> (Type, CoreExpr, CoreExpr -> CoreExpr -> CoreExpr)
432 mkVectEnv [] [] = (unitTy, Var unitDataConId, \env body -> body)
433 mkVectEnv [ty] [v] = (ty, Var v, \env body -> Let (NonRec v env) body)
434 mkVectEnv tys vs = (ty, mkCoreTup (map Var vs),
435 \env body -> Case env (mkWildId ty) (exprType body)
436 [(DataAlt (tupleCon Boxed (length vs)), vs, body)])
440 mkLiftEnv :: Var -> [Type] -> [Var] -> VM (CoreExpr, CoreExpr -> CoreExpr -> VM CoreExpr)
441 mkLiftEnv lc [ty] [v]
442 = return (Var v, \env body ->
444 len <- lengthPA (Var v)
445 return . Let (NonRec v env)
446 $ Case len lc (exprType body) [(DEFAULT, [], body)])
448 -- NOTE: this transparently deals with empty environments
451 (env_tc, env_tyargs) <- parrayReprTyCon vty
452 let [env_con] = tyConDataCons env_tc
454 env = Var (dataConWrapId env_con)
455 `mkTyApps` env_tyargs
456 `mkVarApps` (lc : vs)
458 bind env body = let scrut = unwrapFamInstScrut env_tc env_tyargs env
460 return $ Case scrut (mkWildId (exprType scrut))
462 [(DataAlt env_con, lc : bndrs, body)]
465 vty = mkCoreTupTy tys
467 bndrs | null vs = [mkWildId unitTy]