2 -- The above warning supression flag is a temporary kludge.
3 -- While working on this module you are encouraged to remove it and fix
4 -- any warnings in the module. See
5 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
9 collectAnnTypeBinders, collectAnnTypeArgs, isAnnTypeArg,
11 mkDataConTag, mkDataConTagLit,
14 mkPADictType, mkPArrayType, mkPReprType,
16 parrayReprTyCon, parrayReprDataCon, mkVScrut,
18 paDictArgType, paDictOfType, paDFunType,
19 paMethod, mkPR, lengthPA, replicatePA, emptyPA, liftPA,
20 polyAbstract, polyApply, polyVApply,
21 hoistBinding, hoistExpr, hoistPolyVExpr, takeHoisted,
22 buildClosure, buildClosures,
26 #include "HsVersions.h"
40 import Id ( mkWildId )
41 import MkId ( unwrapFamInstScrut )
45 import TysPrim ( intPrimTy )
46 import BasicTypes ( Boxity(..) )
47 import Literal ( Literal, mkMachInt )
52 import Data.List ( zipWith4 )
53 import Control.Monad ( liftM, liftM2, zipWithM_ )
55 collectAnnTypeArgs :: AnnExpr b ann -> (AnnExpr b ann, [Type])
56 collectAnnTypeArgs expr = go expr []
58 go (_, AnnApp f (_, AnnType ty)) tys = go f (ty : tys)
61 collectAnnTypeBinders :: AnnExpr Var ann -> ([Var], AnnExpr Var ann)
62 collectAnnTypeBinders expr = go [] expr
64 go bs (_, AnnLam b e) | isTyVar b = go (b:bs) e
65 go bs e = (reverse bs, e)
67 collectAnnValBinders :: AnnExpr Var ann -> ([Var], AnnExpr Var ann)
68 collectAnnValBinders expr = go [] expr
70 go bs (_, AnnLam b e) | isId b = go (b:bs) e
71 go bs e = (reverse bs, e)
73 isAnnTypeArg :: AnnExpr b ann -> Bool
74 isAnnTypeArg (_, AnnType t) = True
75 isAnnTypeArg _ = False
77 mkDataConTagLit :: DataCon -> Literal
79 = mkMachInt . toInteger $ dataConTag con - fIRST_TAG
81 mkDataConTag :: DataCon -> CoreExpr
82 mkDataConTag con = mkIntLitInt (dataConTag con - fIRST_TAG)
84 splitPrimTyCon :: Type -> Maybe TyCon
86 | Just (tycon, []) <- splitTyConApp_maybe ty
92 mkBuiltinTyConApp :: (Builtins -> TyCon) -> [Type] -> VM Type
93 mkBuiltinTyConApp get_tc tys
96 return $ mkTyConApp tc tys
98 mkBuiltinTyConApps :: (Builtins -> TyCon) -> [Type] -> Type -> VM Type
99 mkBuiltinTyConApps get_tc tys ty
102 return $ foldr (mk tc) ty tys
104 mk tc ty1 ty2 = mkTyConApp tc [ty1,ty2]
106 mkBuiltinTyConApps1 :: (Builtins -> TyCon) -> Type -> [Type] -> VM Type
107 mkBuiltinTyConApps1 get_tc dft [] = return dft
108 mkBuiltinTyConApps1 get_tc dft tys
112 [] -> pprPanic "mkBuiltinTyConApps1" (ppr tc)
113 _ -> return $ foldr1 (mk tc) tys
115 mk tc ty1 ty2 = mkTyConApp tc [ty1,ty2]
117 mkClosureType :: Type -> Type -> VM Type
118 mkClosureType arg_ty res_ty = mkBuiltinTyConApp closureTyCon [arg_ty, res_ty]
120 mkClosureTypes :: [Type] -> Type -> VM Type
121 mkClosureTypes = mkBuiltinTyConApps closureTyCon
123 mkPReprType :: Type -> VM Type
124 mkPReprType ty = mkBuiltinTyConApp preprTyCon [ty]
126 mkPADictType :: Type -> VM Type
127 mkPADictType ty = mkBuiltinTyConApp paTyCon [ty]
129 mkPArrayType :: Type -> VM Type
131 | Just tycon <- splitPrimTyCon ty
133 arr <- traceMaybeV "mkPArrayType" (ppr tycon)
134 $ lookupPrimPArray tycon
135 return $ mkTyConApp arr []
136 mkPArrayType ty = mkBuiltinTyConApp parrayTyCon [ty]
138 mkBuiltinCo :: (Builtins -> TyCon) -> VM Coercion
142 return $ mkTyConApp tc []
144 parrayReprTyCon :: Type -> VM (TyCon, [Type])
145 parrayReprTyCon ty = builtin parrayTyCon >>= (`lookupFamInst` [ty])
147 parrayReprDataCon :: Type -> VM (DataCon, [Type])
150 (tc, arg_tys) <- parrayReprTyCon ty
151 let [dc] = tyConDataCons tc
154 mkVScrut :: VExpr -> VM (VExpr, TyCon, [Type])
157 (tc, arg_tys) <- parrayReprTyCon (exprType ve)
158 return ((ve, unwrapFamInstScrut tc arg_tys le), tc, arg_tys)
160 prDFunOfTyCon :: TyCon -> VM CoreExpr
162 = liftM Var (traceMaybeV "prDictOfTyCon" (ppr tycon) (lookupTyConPR tycon))
164 paDictArgType :: TyVar -> VM (Maybe Type)
165 paDictArgType tv = go (TyVarTy tv) (tyVarKind tv)
167 go ty k | Just k' <- kindView k = go ty k'
170 tv <- newTyVar FSLIT("a") k1
171 mty1 <- go (TyVarTy tv) k1
174 mty2 <- go (AppTy ty (TyVarTy tv)) k2
175 return $ fmap (ForAllTy tv . FunTy ty1) mty2
180 = liftM Just (mkPADictType ty)
182 go ty k = return Nothing
184 paDictOfType :: Type -> VM CoreExpr
185 paDictOfType ty = paDictOfTyApp ty_fn ty_args
187 (ty_fn, ty_args) = splitAppTys ty
189 paDictOfTyApp :: Type -> [Type] -> VM CoreExpr
190 paDictOfTyApp ty_fn ty_args
191 | Just ty_fn' <- coreView ty_fn = paDictOfTyApp ty_fn' ty_args
192 paDictOfTyApp (TyVarTy tv) ty_args
194 dfun <- maybeV (lookupTyVarPA tv)
195 paDFunApply dfun ty_args
196 paDictOfTyApp (TyConApp tc _) ty_args
198 dfun <- traceMaybeV "paDictOfTyApp" (ppr tc) (lookupTyConPA tc)
199 paDFunApply (Var dfun) ty_args
200 paDictOfTyApp ty ty_args = pprPanic "paDictOfTyApp" (ppr ty)
202 paDFunType :: TyCon -> VM Type
205 margs <- mapM paDictArgType tvs
206 res <- mkPADictType (mkTyConApp tc arg_tys)
207 return . mkForAllTys tvs
208 $ mkFunTys [arg | Just arg <- margs] res
211 arg_tys = mkTyVarTys tvs
213 paDFunApply :: CoreExpr -> [Type] -> VM CoreExpr
216 dicts <- mapM paDictOfType tys
217 return $ mkApps (mkTyApps dfun tys) dicts
219 type PAMethod = (Builtins -> Var, String)
221 pa_length = (lengthPAVar, "lengthPA")
222 pa_replicate = (replicatePAVar, "replicatePA")
223 pa_empty = (emptyPAVar, "emptyPA")
225 paMethod :: PAMethod -> Type -> VM CoreExpr
226 paMethod (method, name) ty
227 | Just tycon <- splitPrimTyCon ty
229 fn <- traceMaybeV "paMethod" (ppr tycon <+> text name)
230 $ lookupPrimMethod tycon name
233 paMethod (method, name) ty
236 dict <- paDictOfType ty
237 return $ mkApps (Var fn) [Type ty, dict]
239 mkPR :: Type -> VM CoreExpr
242 fn <- builtin mkPRVar
243 dict <- paDictOfType ty
244 return $ mkApps (Var fn) [Type ty, dict]
246 lengthPA :: Type -> CoreExpr -> VM CoreExpr
247 lengthPA ty x = liftM (`App` x) (paMethod pa_length ty)
249 replicatePA :: CoreExpr -> CoreExpr -> VM CoreExpr
250 replicatePA len x = liftM (`mkApps` [len,x])
251 (paMethod pa_replicate (exprType x))
253 emptyPA :: Type -> VM CoreExpr
254 emptyPA = paMethod pa_empty
256 liftPA :: CoreExpr -> VM CoreExpr
259 lc <- builtin liftingContext
260 replicatePA (Var lc) x
262 newLocalVVar :: FastString -> Type -> VM VVar
265 lty <- mkPArrayType vty
266 vv <- newLocalVar fs vty
267 lv <- newLocalVar fs lty
270 polyAbstract :: [TyVar] -> ((CoreExpr -> CoreExpr) -> VM a) -> VM a
274 mdicts <- mapM mk_dict_var tvs
275 zipWithM_ (\tv -> maybe (defLocalTyVar tv) (defLocalTyVarWithPA tv . Var)) tvs mdicts
279 r <- paDictArgType tv
281 Just ty -> liftM Just (newLocalVar FSLIT("dPA") ty)
282 Nothing -> return Nothing
284 mk_lams mdicts = mkLams (tvs ++ [dict | Just dict <- mdicts])
286 polyApply :: CoreExpr -> [Type] -> VM CoreExpr
289 dicts <- mapM paDictOfType tys
290 return $ expr `mkTyApps` tys `mkApps` dicts
292 polyVApply :: VExpr -> [Type] -> VM VExpr
295 dicts <- mapM paDictOfType tys
296 return $ mapVect (\e -> e `mkTyApps` tys `mkApps` dicts) expr
298 hoistBinding :: Var -> CoreExpr -> VM ()
299 hoistBinding v e = updGEnv $ \env ->
300 env { global_bindings = (v,e) : global_bindings env }
302 hoistExpr :: FastString -> CoreExpr -> VM Var
305 var <- newLocalVar fs (exprType expr)
306 hoistBinding var expr
309 hoistVExpr :: VExpr -> VM VVar
313 vv <- hoistExpr ('v' `consFS` fs) ve
314 lv <- hoistExpr ('l' `consFS` fs) le
317 hoistPolyVExpr :: [TyVar] -> VM VExpr -> VM VExpr
320 expr <- closedV . polyAbstract tvs $ \abstract ->
321 liftM (mapVect abstract) p
322 fn <- hoistVExpr expr
323 polyVApply (vVar fn) (mkTyVarTys tvs)
325 takeHoisted :: VM [(Var, CoreExpr)]
329 setGEnv $ env { global_bindings = [] }
330 return $ global_bindings env
332 mkClosure :: Type -> Type -> Type -> VExpr -> VExpr -> VM VExpr
333 mkClosure arg_ty res_ty env_ty (vfn,lfn) (venv,lenv)
335 dict <- paDictOfType env_ty
336 mkv <- builtin mkClosureVar
337 mkl <- builtin mkClosurePVar
338 return (Var mkv `mkTyApps` [arg_ty, res_ty, env_ty] `mkApps` [dict, vfn, lfn, venv],
339 Var mkl `mkTyApps` [arg_ty, res_ty, env_ty] `mkApps` [dict, vfn, lfn, lenv])
341 mkClosureApp :: Type -> Type -> VExpr -> VExpr -> VM VExpr
342 mkClosureApp arg_ty res_ty (vclo, lclo) (varg, larg)
344 vapply <- builtin applyClosureVar
345 lapply <- builtin applyClosurePVar
346 return (Var vapply `mkTyApps` [arg_ty, res_ty] `mkApps` [vclo, varg],
347 Var lapply `mkTyApps` [arg_ty, res_ty] `mkApps` [lclo, larg])
349 buildClosures :: [TyVar] -> [VVar] -> [Type] -> Type -> VM VExpr -> VM VExpr
350 buildClosures tvs vars [] res_ty mk_body
352 buildClosures tvs vars [arg_ty] res_ty mk_body
353 = buildClosure tvs vars arg_ty res_ty mk_body
354 buildClosures tvs vars (arg_ty : arg_tys) res_ty mk_body
356 res_ty' <- mkClosureTypes arg_tys res_ty
357 arg <- newLocalVVar FSLIT("x") arg_ty
358 buildClosure tvs vars arg_ty res_ty'
361 lc <- builtin liftingContext
362 clo <- buildClosures tvs (vars ++ [arg]) arg_tys res_ty mk_body
363 return $ vLams lc (vars ++ [arg]) clo
365 -- (clo <x1,...,xn> <f,f^>, aclo (Arr lc xs1 ... xsn) <f,f^>)
367 -- f = \env v -> case env of <x1,...,xn> -> e x1 ... xn v
368 -- f^ = \env v -> case env of Arr l xs1 ... xsn -> e^ l x1 ... xn v
370 buildClosure :: [TyVar] -> [VVar] -> Type -> Type -> VM VExpr -> VM VExpr
371 buildClosure tvs vars arg_ty res_ty mk_body
373 (env_ty, env, bind) <- buildEnv vars
374 env_bndr <- newLocalVVar FSLIT("env") env_ty
375 arg_bndr <- newLocalVVar FSLIT("arg") arg_ty
377 fn <- hoistPolyVExpr tvs
379 lc <- builtin liftingContext
381 body' <- bind (vVar env_bndr)
382 (vVarApps lc body (vars ++ [arg_bndr]))
383 return (vLamsWithoutLC [env_bndr, arg_bndr] body')
385 mkClosure arg_ty res_ty env_ty fn env
387 buildEnv :: [VVar] -> VM (Type, VExpr, VExpr -> VExpr -> VM VExpr)
390 lc <- builtin liftingContext
391 let (ty, venv, vbind) = mkVectEnv tys vs
392 (lenv, lbind) <- mkLiftEnv lc tys ls
393 return (ty, (venv, lenv),
394 \(venv,lenv) (vbody,lbody) ->
396 let vbody' = vbind venv vbody
397 lbody' <- lbind lenv lbody
398 return (vbody', lbody'))
403 mkVectEnv :: [Type] -> [Var] -> (Type, CoreExpr, CoreExpr -> CoreExpr -> CoreExpr)
404 mkVectEnv [] [] = (unitTy, Var unitDataConId, \env body -> body)
405 mkVectEnv [ty] [v] = (ty, Var v, \env body -> Let (NonRec v env) body)
406 mkVectEnv tys vs = (ty, mkCoreTup (map Var vs),
407 \env body -> Case env (mkWildId ty) (exprType body)
408 [(DataAlt (tupleCon Boxed (length vs)), vs, body)])
412 mkLiftEnv :: Var -> [Type] -> [Var] -> VM (CoreExpr, CoreExpr -> CoreExpr -> VM CoreExpr)
413 mkLiftEnv lc [ty] [v]
414 = return (Var v, \env body ->
416 len <- lengthPA ty (Var v)
417 return . Let (NonRec v env)
418 $ Case len lc (exprType body) [(DEFAULT, [], body)])
420 -- NOTE: this transparently deals with empty environments
423 (env_tc, env_tyargs) <- parrayReprTyCon vty
424 let [env_con] = tyConDataCons env_tc
426 env = Var (dataConWrapId env_con)
427 `mkTyApps` env_tyargs
428 `mkVarApps` (lc : vs)
430 bind env body = let scrut = unwrapFamInstScrut env_tc env_tyargs env
432 return $ Case scrut (mkWildId (exprType scrut))
434 [(DataAlt env_con, lc : bndrs, body)]
437 vty = mkCoreTupTy tys
439 bndrs | null vs = [mkWildId unitTy]