2 collectAnnTypeBinders, collectAnnTypeArgs, isAnnTypeArg,
4 dataConTagZ, mkDataConTag, mkDataConTagLit,
9 mkPADictType, mkPArrayType, mkPReprType,
11 parrayReprTyCon, parrayReprDataCon, mkVScrut,
13 paDictArgType, paDictOfType, paDFunType,
14 paMethod, mkPR, lengthPA, replicatePA, emptyPA, packPA, combinePA, liftPA,
15 polyAbstract, polyApply, polyVApply,
16 hoistBinding, hoistExpr, hoistPolyVExpr, takeHoisted,
17 buildClosure, buildClosures,
21 #include "HsVersions.h"
35 import Id ( mkWildId )
36 import MkId ( unwrapFamInstScrut )
38 import BasicTypes ( Boxity(..) )
39 import Literal ( Literal, mkMachInt )
47 collectAnnTypeArgs :: AnnExpr b ann -> (AnnExpr b ann, [Type])
48 collectAnnTypeArgs expr = go expr []
50 go (_, AnnApp f (_, AnnType ty)) tys = go f (ty : tys)
53 collectAnnTypeBinders :: AnnExpr Var ann -> ([Var], AnnExpr Var ann)
54 collectAnnTypeBinders expr = go [] expr
56 go bs (_, AnnLam b e) | isTyVar b = go (b:bs) e
57 go bs e = (reverse bs, e)
59 collectAnnValBinders :: AnnExpr Var ann -> ([Var], AnnExpr Var ann)
60 collectAnnValBinders expr = go [] expr
62 go bs (_, AnnLam b e) | isId b = go (b:bs) e
63 go bs e = (reverse bs, e)
65 isAnnTypeArg :: AnnExpr b ann -> Bool
66 isAnnTypeArg (_, AnnType _) = True
67 isAnnTypeArg _ = False
69 dataConTagZ :: DataCon -> Int
70 dataConTagZ con = dataConTag con - fIRST_TAG
72 mkDataConTagLit :: DataCon -> Literal
73 mkDataConTagLit = mkMachInt . toInteger . dataConTagZ
75 mkDataConTag :: DataCon -> CoreExpr
76 mkDataConTag = mkIntLitInt . dataConTagZ
78 splitPrimTyCon :: Type -> Maybe TyCon
80 | Just (tycon, []) <- splitTyConApp_maybe ty
86 mkBuiltinTyConApp :: (Builtins -> TyCon) -> [Type] -> VM Type
87 mkBuiltinTyConApp get_tc tys
90 return $ mkTyConApp tc tys
92 mkBuiltinTyConApps :: (Builtins -> TyCon) -> [Type] -> Type -> VM Type
93 mkBuiltinTyConApps get_tc tys ty
96 return $ foldr (mk tc) ty tys
98 mk tc ty1 ty2 = mkTyConApp tc [ty1,ty2]
101 mkBuiltinTyConApps1 :: (Builtins -> TyCon) -> Type -> [Type] -> VM Type
102 mkBuiltinTyConApps1 _ dft [] = return dft
103 mkBuiltinTyConApps1 get_tc _ tys
107 [] -> pprPanic "mkBuiltinTyConApps1" (ppr tc)
108 _ -> return $ foldr1 (mk tc) tys
110 mk tc ty1 ty2 = mkTyConApp tc [ty1,ty2]
112 mkClosureType :: Type -> Type -> VM Type
113 mkClosureType arg_ty res_ty = mkBuiltinTyConApp closureTyCon [arg_ty, res_ty]
116 mkClosureTypes :: [Type] -> Type -> VM Type
117 mkClosureTypes = mkBuiltinTyConApps closureTyCon
119 mkPReprType :: Type -> VM Type
120 mkPReprType ty = mkBuiltinTyConApp preprTyCon [ty]
122 mkPADictType :: Type -> VM Type
123 mkPADictType ty = mkBuiltinTyConApp paTyCon [ty]
125 mkPArrayType :: Type -> VM Type
127 | Just tycon <- splitPrimTyCon ty
129 arr <- traceMaybeV "mkPArrayType" (ppr tycon)
130 $ lookupPrimPArray tycon
131 return $ mkTyConApp arr []
132 mkPArrayType ty = mkBuiltinTyConApp parrayTyCon [ty]
134 mkBuiltinCo :: (Builtins -> TyCon) -> VM Coercion
138 return $ mkTyConApp tc []
140 parrayReprTyCon :: Type -> VM (TyCon, [Type])
141 parrayReprTyCon ty = builtin parrayTyCon >>= (`lookupFamInst` [ty])
143 parrayReprDataCon :: Type -> VM (DataCon, [Type])
146 (tc, arg_tys) <- parrayReprTyCon ty
147 let [dc] = tyConDataCons tc
150 mkVScrut :: VExpr -> VM (VExpr, TyCon, [Type])
153 (tc, arg_tys) <- parrayReprTyCon (exprType ve)
154 return ((ve, unwrapFamInstScrut tc arg_tys le), tc, arg_tys)
156 prDFunOfTyCon :: TyCon -> VM CoreExpr
158 = liftM Var (traceMaybeV "prDictOfTyCon" (ppr tycon) (lookupTyConPR tycon))
160 paDictArgType :: TyVar -> VM (Maybe Type)
161 paDictArgType tv = go (TyVarTy tv) (tyVarKind tv)
163 go ty k | Just k' <- kindView k = go ty k'
166 tv <- newTyVar FSLIT("a") k1
167 mty1 <- go (TyVarTy tv) k1
170 mty2 <- go (AppTy ty (TyVarTy tv)) k2
171 return $ fmap (ForAllTy tv . FunTy ty1) mty2
176 = liftM Just (mkPADictType ty)
178 go _ _ = return Nothing
180 paDictOfType :: Type -> VM CoreExpr
181 paDictOfType ty = paDictOfTyApp ty_fn ty_args
183 (ty_fn, ty_args) = splitAppTys ty
185 paDictOfTyApp :: Type -> [Type] -> VM CoreExpr
186 paDictOfTyApp ty_fn ty_args
187 | Just ty_fn' <- coreView ty_fn = paDictOfTyApp ty_fn' ty_args
188 paDictOfTyApp (TyVarTy tv) ty_args
190 dfun <- maybeV (lookupTyVarPA tv)
191 paDFunApply dfun ty_args
192 paDictOfTyApp (TyConApp tc _) ty_args
194 dfun <- traceMaybeV "paDictOfTyApp" (ppr tc) (lookupTyConPA tc)
195 paDFunApply (Var dfun) ty_args
196 paDictOfTyApp ty _ = pprPanic "paDictOfTyApp" (ppr ty)
198 paDFunType :: TyCon -> VM Type
201 margs <- mapM paDictArgType tvs
202 res <- mkPADictType (mkTyConApp tc arg_tys)
203 return . mkForAllTys tvs
204 $ mkFunTys [arg | Just arg <- margs] res
207 arg_tys = mkTyVarTys tvs
209 paDFunApply :: CoreExpr -> [Type] -> VM CoreExpr
212 dicts <- mapM paDictOfType tys
213 return $ mkApps (mkTyApps dfun tys) dicts
215 type PAMethod = (Builtins -> Var, String)
217 pa_length, pa_replicate, pa_empty, pa_pack :: (Builtins -> Var, String)
218 pa_length = (lengthPAVar, "lengthPA")
219 pa_replicate = (replicatePAVar, "replicatePA")
220 pa_empty = (emptyPAVar, "emptyPA")
221 pa_pack = (packPAVar, "packPA")
223 paMethod :: PAMethod -> Type -> VM CoreExpr
224 paMethod (_method, name) ty
225 | Just tycon <- splitPrimTyCon ty
227 fn <- traceMaybeV "paMethod" (ppr tycon <+> text name)
228 $ lookupPrimMethod tycon name
231 paMethod (method, _name) ty
234 dict <- paDictOfType ty
235 return $ mkApps (Var fn) [Type ty, dict]
237 mkPR :: Type -> VM CoreExpr
240 fn <- builtin mkPRVar
241 dict <- paDictOfType ty
242 return $ mkApps (Var fn) [Type ty, dict]
244 lengthPA :: Type -> CoreExpr -> VM CoreExpr
245 lengthPA ty x = liftM (`App` x) (paMethod pa_length ty)
247 replicatePA :: CoreExpr -> CoreExpr -> VM CoreExpr
248 replicatePA len x = liftM (`mkApps` [len,x])
249 (paMethod pa_replicate (exprType x))
251 emptyPA :: Type -> VM CoreExpr
252 emptyPA = paMethod pa_empty
254 packPA :: Type -> CoreExpr -> CoreExpr -> CoreExpr -> VM CoreExpr
255 packPA ty xs len sel = liftM (`mkApps` [xs, len, sel])
256 (paMethod pa_pack ty)
258 combinePA :: Type -> CoreExpr -> CoreExpr -> CoreExpr -> [CoreExpr]
260 combinePA ty len sel is xs
261 = liftM (`mkApps` (len : sel : is : xs))
262 (paMethod (combinePAVar n, "combine" ++ show n ++ "PA") ty)
266 liftPA :: CoreExpr -> VM CoreExpr
269 lc <- builtin liftingContext
270 replicatePA (Var lc) x
272 newLocalVVar :: FastString -> Type -> VM VVar
275 lty <- mkPArrayType vty
276 vv <- newLocalVar fs vty
277 lv <- newLocalVar fs lty
280 polyAbstract :: [TyVar] -> ((CoreExpr -> CoreExpr) -> VM a) -> VM a
284 mdicts <- mapM mk_dict_var tvs
285 zipWithM_ (\tv -> maybe (defLocalTyVar tv) (defLocalTyVarWithPA tv . Var)) tvs mdicts
289 r <- paDictArgType tv
291 Just ty -> liftM Just (newLocalVar FSLIT("dPA") ty)
292 Nothing -> return Nothing
294 mk_lams mdicts = mkLams (tvs ++ [dict | Just dict <- mdicts])
296 polyApply :: CoreExpr -> [Type] -> VM CoreExpr
299 dicts <- mapM paDictOfType tys
300 return $ expr `mkTyApps` tys `mkApps` dicts
302 polyVApply :: VExpr -> [Type] -> VM VExpr
305 dicts <- mapM paDictOfType tys
306 return $ mapVect (\e -> e `mkTyApps` tys `mkApps` dicts) expr
308 hoistBinding :: Var -> CoreExpr -> VM ()
309 hoistBinding v e = updGEnv $ \env ->
310 env { global_bindings = (v,e) : global_bindings env }
312 hoistExpr :: FastString -> CoreExpr -> VM Var
315 var <- newLocalVar fs (exprType expr)
316 hoistBinding var expr
319 hoistVExpr :: VExpr -> VM VVar
323 vv <- hoistExpr ('v' `consFS` fs) ve
324 lv <- hoistExpr ('l' `consFS` fs) le
327 hoistPolyVExpr :: [TyVar] -> VM VExpr -> VM VExpr
330 expr <- closedV . polyAbstract tvs $ \abstract ->
331 liftM (mapVect abstract) p
332 fn <- hoistVExpr expr
333 polyVApply (vVar fn) (mkTyVarTys tvs)
335 takeHoisted :: VM [(Var, CoreExpr)]
339 setGEnv $ env { global_bindings = [] }
340 return $ global_bindings env
343 boxExpr :: Type -> VExpr -> VM VExpr
344 boxExpr ty (vexpr, lexpr)
345 | Just (tycon, []) <- splitTyConApp_maybe ty
346 , isUnLiftedTyCon tycon
348 r <- lookupBoxedTyCon tycon
350 Just tycon' -> let [dc] = tyConDataCons tycon'
352 return (mkConApp dc [vexpr], lexpr)
353 Nothing -> return (vexpr, lexpr)
356 mkClosure :: Type -> Type -> Type -> VExpr -> VExpr -> VM VExpr
357 mkClosure arg_ty res_ty env_ty (vfn,lfn) (venv,lenv)
359 dict <- paDictOfType env_ty
360 mkv <- builtin mkClosureVar
361 mkl <- builtin mkClosurePVar
362 return (Var mkv `mkTyApps` [arg_ty, res_ty, env_ty] `mkApps` [dict, vfn, lfn, venv],
363 Var mkl `mkTyApps` [arg_ty, res_ty, env_ty] `mkApps` [dict, vfn, lfn, lenv])
365 mkClosureApp :: Type -> Type -> VExpr -> VExpr -> VM VExpr
366 mkClosureApp arg_ty res_ty (vclo, lclo) (varg, larg)
368 vapply <- builtin applyClosureVar
369 lapply <- builtin applyClosurePVar
370 return (Var vapply `mkTyApps` [arg_ty, res_ty] `mkApps` [vclo, varg],
371 Var lapply `mkTyApps` [arg_ty, res_ty] `mkApps` [lclo, larg])
373 buildClosures :: [TyVar] -> [VVar] -> [Type] -> Type -> VM VExpr -> VM VExpr
374 buildClosures _ _ [] _ mk_body
376 buildClosures tvs vars [arg_ty] res_ty mk_body
377 = buildClosure tvs vars arg_ty res_ty mk_body
378 buildClosures tvs vars (arg_ty : arg_tys) res_ty mk_body
380 res_ty' <- mkClosureTypes arg_tys res_ty
381 arg <- newLocalVVar FSLIT("x") arg_ty
382 buildClosure tvs vars arg_ty res_ty'
385 lc <- builtin liftingContext
386 clo <- buildClosures tvs (vars ++ [arg]) arg_tys res_ty mk_body
387 return $ vLams lc (vars ++ [arg]) clo
389 -- (clo <x1,...,xn> <f,f^>, aclo (Arr lc xs1 ... xsn) <f,f^>)
391 -- f = \env v -> case env of <x1,...,xn> -> e x1 ... xn v
392 -- f^ = \env v -> case env of Arr l xs1 ... xsn -> e^ l x1 ... xn v
394 buildClosure :: [TyVar] -> [VVar] -> Type -> Type -> VM VExpr -> VM VExpr
395 buildClosure tvs vars arg_ty res_ty mk_body
397 (env_ty, env, bind) <- buildEnv vars
398 env_bndr <- newLocalVVar FSLIT("env") env_ty
399 arg_bndr <- newLocalVVar FSLIT("arg") arg_ty
401 fn <- hoistPolyVExpr tvs
403 lc <- builtin liftingContext
405 body' <- bind (vVar env_bndr)
406 (vVarApps lc body (vars ++ [arg_bndr]))
407 return (vLamsWithoutLC [env_bndr, arg_bndr] body')
409 mkClosure arg_ty res_ty env_ty fn env
411 buildEnv :: [VVar] -> VM (Type, VExpr, VExpr -> VExpr -> VM VExpr)
414 lc <- builtin liftingContext
415 let (ty, venv, vbind) = mkVectEnv tys vs
416 (lenv, lbind) <- mkLiftEnv lc tys ls
417 return (ty, (venv, lenv),
418 \(venv,lenv) (vbody,lbody) ->
420 let vbody' = vbind venv vbody
421 lbody' <- lbind lenv lbody
422 return (vbody', lbody'))
427 mkVectEnv :: [Type] -> [Var] -> (Type, CoreExpr, CoreExpr -> CoreExpr -> CoreExpr)
428 mkVectEnv [] [] = (unitTy, Var unitDataConId, \_ body -> body)
429 mkVectEnv [ty] [v] = (ty, Var v, \env body -> Let (NonRec v env) body)
430 mkVectEnv tys vs = (ty, mkCoreTup (map Var vs),
431 \env body -> Case env (mkWildId ty) (exprType body)
432 [(DataAlt (tupleCon Boxed (length vs)), vs, body)])
436 mkLiftEnv :: Var -> [Type] -> [Var] -> VM (CoreExpr, CoreExpr -> CoreExpr -> VM CoreExpr)
437 mkLiftEnv lc [ty] [v]
438 = return (Var v, \env body ->
440 len <- lengthPA ty (Var v)
441 return . Let (NonRec v env)
442 $ Case len lc (exprType body) [(DEFAULT, [], body)])
444 -- NOTE: this transparently deals with empty environments
447 (env_tc, env_tyargs) <- parrayReprTyCon vty
449 bndrs <- if null vs then do
450 v <- newDummyVar unitTy
453 let [env_con] = tyConDataCons env_tc
455 env = Var (dataConWrapId env_con)
456 `mkTyApps` env_tyargs
457 `mkApps` (Var lc : args)
459 bind env body = let scrut = unwrapFamInstScrut env_tc env_tyargs env
461 return $ Case scrut (mkWildId (exprType scrut))
463 [(DataAlt env_con, lc : bndrs, body)]
466 vty = mkCoreTupTy tys
468 args | null vs = [Var unitDataConId]
469 | otherwise = map Var vs