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,
33 import Id ( mkWildId )
34 import MkId ( unwrapFamInstScrut )
36 import BasicTypes ( Boxity(..) )
37 import Literal ( Literal, mkMachInt )
45 collectAnnTypeArgs :: AnnExpr b ann -> (AnnExpr b ann, [Type])
46 collectAnnTypeArgs expr = go expr []
48 go (_, AnnApp f (_, AnnType ty)) tys = go f (ty : tys)
51 collectAnnTypeBinders :: AnnExpr Var ann -> ([Var], AnnExpr Var ann)
52 collectAnnTypeBinders expr = go [] expr
54 go bs (_, AnnLam b e) | isTyVar b = go (b:bs) e
55 go bs e = (reverse bs, e)
57 collectAnnValBinders :: AnnExpr Var ann -> ([Var], AnnExpr Var ann)
58 collectAnnValBinders expr = go [] expr
60 go bs (_, AnnLam b e) | isId b = go (b:bs) e
61 go bs e = (reverse bs, e)
63 isAnnTypeArg :: AnnExpr b ann -> Bool
64 isAnnTypeArg (_, AnnType _) = True
65 isAnnTypeArg _ = False
67 dataConTagZ :: DataCon -> Int
68 dataConTagZ con = dataConTag con - fIRST_TAG
70 mkDataConTagLit :: DataCon -> Literal
71 mkDataConTagLit = mkMachInt . toInteger . dataConTagZ
73 mkDataConTag :: DataCon -> CoreExpr
74 mkDataConTag = mkIntLitInt . dataConTagZ
76 splitPrimTyCon :: Type -> Maybe TyCon
78 | Just (tycon, []) <- splitTyConApp_maybe ty
84 mkBuiltinTyConApp :: (Builtins -> TyCon) -> [Type] -> VM Type
85 mkBuiltinTyConApp get_tc tys
88 return $ mkTyConApp tc tys
90 mkBuiltinTyConApps :: (Builtins -> TyCon) -> [Type] -> Type -> VM Type
91 mkBuiltinTyConApps get_tc tys ty
94 return $ foldr (mk tc) ty tys
96 mk tc ty1 ty2 = mkTyConApp tc [ty1,ty2]
99 mkBuiltinTyConApps1 :: (Builtins -> TyCon) -> Type -> [Type] -> VM Type
100 mkBuiltinTyConApps1 _ dft [] = return dft
101 mkBuiltinTyConApps1 get_tc _ tys
105 [] -> pprPanic "mkBuiltinTyConApps1" (ppr tc)
106 _ -> return $ foldr1 (mk tc) tys
108 mk tc ty1 ty2 = mkTyConApp tc [ty1,ty2]
110 mkClosureType :: Type -> Type -> VM Type
111 mkClosureType arg_ty res_ty = mkBuiltinTyConApp closureTyCon [arg_ty, res_ty]
114 mkClosureTypes :: [Type] -> Type -> VM Type
115 mkClosureTypes = mkBuiltinTyConApps closureTyCon
117 mkPReprType :: Type -> VM Type
118 mkPReprType ty = mkBuiltinTyConApp preprTyCon [ty]
120 mkPADictType :: Type -> VM Type
121 mkPADictType ty = mkBuiltinTyConApp paTyCon [ty]
123 mkPArrayType :: Type -> VM Type
125 | Just tycon <- splitPrimTyCon ty
127 arr <- traceMaybeV "mkPArrayType" (ppr tycon)
128 $ lookupPrimPArray tycon
129 return $ mkTyConApp arr []
130 mkPArrayType ty = mkBuiltinTyConApp parrayTyCon [ty]
132 mkBuiltinCo :: (Builtins -> TyCon) -> VM Coercion
136 return $ mkTyConApp tc []
138 parrayReprTyCon :: Type -> VM (TyCon, [Type])
139 parrayReprTyCon ty = builtin parrayTyCon >>= (`lookupFamInst` [ty])
141 parrayReprDataCon :: Type -> VM (DataCon, [Type])
144 (tc, arg_tys) <- parrayReprTyCon ty
145 let [dc] = tyConDataCons tc
148 mkVScrut :: VExpr -> VM (VExpr, TyCon, [Type])
151 (tc, arg_tys) <- parrayReprTyCon (exprType ve)
152 return ((ve, unwrapFamInstScrut tc arg_tys le), tc, arg_tys)
154 prDFunOfTyCon :: TyCon -> VM CoreExpr
156 = liftM Var (traceMaybeV "prDictOfTyCon" (ppr tycon) (lookupTyConPR tycon))
158 paDictArgType :: TyVar -> VM (Maybe Type)
159 paDictArgType tv = go (TyVarTy tv) (tyVarKind tv)
161 go ty k | Just k' <- kindView k = go ty k'
164 tv <- newTyVar (fsLit "a") k1
165 mty1 <- go (TyVarTy tv) k1
168 mty2 <- go (AppTy ty (TyVarTy tv)) k2
169 return $ fmap (ForAllTy tv . FunTy ty1) mty2
174 = liftM Just (mkPADictType ty)
176 go _ _ = return Nothing
178 paDictOfType :: Type -> VM CoreExpr
179 paDictOfType ty = paDictOfTyApp ty_fn ty_args
181 (ty_fn, ty_args) = splitAppTys ty
183 paDictOfTyApp :: Type -> [Type] -> VM CoreExpr
184 paDictOfTyApp ty_fn ty_args
185 | Just ty_fn' <- coreView ty_fn = paDictOfTyApp ty_fn' ty_args
186 paDictOfTyApp (TyVarTy tv) ty_args
188 dfun <- maybeV (lookupTyVarPA tv)
189 paDFunApply dfun ty_args
190 paDictOfTyApp (TyConApp tc _) ty_args
192 dfun <- traceMaybeV "paDictOfTyApp" (ppr tc) (lookupTyConPA tc)
193 paDFunApply (Var dfun) ty_args
194 paDictOfTyApp ty _ = pprPanic "paDictOfTyApp" (ppr ty)
196 paDFunType :: TyCon -> VM Type
199 margs <- mapM paDictArgType tvs
200 res <- mkPADictType (mkTyConApp tc arg_tys)
201 return . mkForAllTys tvs
202 $ mkFunTys [arg | Just arg <- margs] res
205 arg_tys = mkTyVarTys tvs
207 paDFunApply :: CoreExpr -> [Type] -> VM CoreExpr
210 dicts <- mapM paDictOfType tys
211 return $ mkApps (mkTyApps dfun tys) dicts
213 type PAMethod = (Builtins -> Var, String)
215 pa_length, pa_replicate, pa_empty, pa_pack :: (Builtins -> Var, String)
216 pa_length = (lengthPAVar, "lengthPA")
217 pa_replicate = (replicatePAVar, "replicatePA")
218 pa_empty = (emptyPAVar, "emptyPA")
219 pa_pack = (packPAVar, "packPA")
221 paMethod :: PAMethod -> Type -> VM CoreExpr
222 paMethod (_method, name) ty
223 | Just tycon <- splitPrimTyCon ty
225 fn <- traceMaybeV "paMethod" (ppr tycon <+> text name)
226 $ lookupPrimMethod tycon name
229 paMethod (method, _name) ty
232 dict <- paDictOfType ty
233 return $ mkApps (Var fn) [Type ty, dict]
235 mkPR :: Type -> VM CoreExpr
238 fn <- builtin mkPRVar
239 dict <- paDictOfType ty
240 return $ mkApps (Var fn) [Type ty, dict]
242 lengthPA :: Type -> CoreExpr -> VM CoreExpr
243 lengthPA ty x = liftM (`App` x) (paMethod pa_length ty)
245 replicatePA :: CoreExpr -> CoreExpr -> VM CoreExpr
246 replicatePA len x = liftM (`mkApps` [len,x])
247 (paMethod pa_replicate (exprType x))
249 emptyPA :: Type -> VM CoreExpr
250 emptyPA = paMethod pa_empty
252 packPA :: Type -> CoreExpr -> CoreExpr -> CoreExpr -> VM CoreExpr
253 packPA ty xs len sel = liftM (`mkApps` [xs, len, sel])
254 (paMethod pa_pack ty)
256 combinePA :: Type -> CoreExpr -> CoreExpr -> CoreExpr -> [CoreExpr]
258 combinePA ty len sel is xs
259 = liftM (`mkApps` (len : sel : is : xs))
260 (paMethod (combinePAVar n, "combine" ++ show n ++ "PA") ty)
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
341 boxExpr :: Type -> VExpr -> VM VExpr
342 boxExpr ty (vexpr, lexpr)
343 | Just (tycon, []) <- splitTyConApp_maybe ty
344 , isUnLiftedTyCon tycon
346 r <- lookupBoxedTyCon tycon
348 Just tycon' -> let [dc] = tyConDataCons tycon'
350 return (mkConApp dc [vexpr], lexpr)
351 Nothing -> return (vexpr, lexpr)
354 mkClosure :: Type -> Type -> Type -> VExpr -> VExpr -> VM VExpr
355 mkClosure arg_ty res_ty env_ty (vfn,lfn) (venv,lenv)
357 dict <- paDictOfType env_ty
358 mkv <- builtin mkClosureVar
359 mkl <- builtin mkClosurePVar
360 return (Var mkv `mkTyApps` [arg_ty, res_ty, env_ty] `mkApps` [dict, vfn, lfn, venv],
361 Var mkl `mkTyApps` [arg_ty, res_ty, env_ty] `mkApps` [dict, vfn, lfn, lenv])
363 mkClosureApp :: Type -> Type -> VExpr -> VExpr -> VM VExpr
364 mkClosureApp arg_ty res_ty (vclo, lclo) (varg, larg)
366 vapply <- builtin applyClosureVar
367 lapply <- builtin applyClosurePVar
368 return (Var vapply `mkTyApps` [arg_ty, res_ty] `mkApps` [vclo, varg],
369 Var lapply `mkTyApps` [arg_ty, res_ty] `mkApps` [lclo, larg])
371 buildClosures :: [TyVar] -> [VVar] -> [Type] -> Type -> VM VExpr -> VM VExpr
372 buildClosures _ _ [] _ mk_body
374 buildClosures tvs vars [arg_ty] res_ty mk_body
375 = buildClosure tvs vars arg_ty res_ty mk_body
376 buildClosures tvs vars (arg_ty : arg_tys) res_ty mk_body
378 res_ty' <- mkClosureTypes arg_tys res_ty
379 arg <- newLocalVVar (fsLit "x") arg_ty
380 buildClosure tvs vars arg_ty res_ty'
383 lc <- builtin liftingContext
384 clo <- buildClosures tvs (vars ++ [arg]) arg_tys res_ty mk_body
385 return $ vLams lc (vars ++ [arg]) clo
387 -- (clo <x1,...,xn> <f,f^>, aclo (Arr lc xs1 ... xsn) <f,f^>)
389 -- f = \env v -> case env of <x1,...,xn> -> e x1 ... xn v
390 -- f^ = \env v -> case env of Arr l xs1 ... xsn -> e^ l x1 ... xn v
392 buildClosure :: [TyVar] -> [VVar] -> Type -> Type -> VM VExpr -> VM VExpr
393 buildClosure tvs vars arg_ty res_ty mk_body
395 (env_ty, env, bind) <- buildEnv vars
396 env_bndr <- newLocalVVar (fsLit "env") env_ty
397 arg_bndr <- newLocalVVar (fsLit "arg") arg_ty
399 fn <- hoistPolyVExpr tvs
401 lc <- builtin liftingContext
403 body' <- bind (vVar env_bndr)
404 (vVarApps lc body (vars ++ [arg_bndr]))
405 return (vLamsWithoutLC [env_bndr, arg_bndr] body')
407 mkClosure arg_ty res_ty env_ty fn env
409 buildEnv :: [VVar] -> VM (Type, VExpr, VExpr -> VExpr -> VM VExpr)
412 lc <- builtin liftingContext
413 let (ty, venv, vbind) = mkVectEnv tys vs
414 (lenv, lbind) <- mkLiftEnv lc tys ls
415 return (ty, (venv, lenv),
416 \(venv,lenv) (vbody,lbody) ->
418 let vbody' = vbind venv vbody
419 lbody' <- lbind lenv lbody
420 return (vbody', lbody'))
425 mkVectEnv :: [Type] -> [Var] -> (Type, CoreExpr, CoreExpr -> CoreExpr -> CoreExpr)
426 mkVectEnv [] [] = (unitTy, Var unitDataConId, \_ body -> body)
427 mkVectEnv [ty] [v] = (ty, Var v, \env body -> Let (NonRec v env) body)
428 mkVectEnv tys vs = (ty, mkCoreTup (map Var vs),
429 \env body -> Case env (mkWildId ty) (exprType body)
430 [(DataAlt (tupleCon Boxed (length vs)), vs, body)])
434 mkLiftEnv :: Var -> [Type] -> [Var] -> VM (CoreExpr, CoreExpr -> CoreExpr -> VM CoreExpr)
435 mkLiftEnv lc [ty] [v]
436 = return (Var v, \env body ->
438 len <- lengthPA ty (Var v)
439 return . Let (NonRec v env)
440 $ Case len lc (exprType body) [(DEFAULT, [], body)])
442 -- NOTE: this transparently deals with empty environments
445 (env_tc, env_tyargs) <- parrayReprTyCon vty
447 bndrs <- if null vs then do
448 v <- newDummyVar unitTy
451 let [env_con] = tyConDataCons env_tc
453 env = Var (dataConWrapId env_con)
454 `mkTyApps` env_tyargs
455 `mkApps` (Var lc : args)
457 bind env body = let scrut = unwrapFamInstScrut env_tc env_tyargs env
459 return $ Case scrut (mkWildId (exprType scrut))
461 [(DataAlt env_con, lc : bndrs, body)]
464 vty = mkCoreTupTy tys
466 args | null vs = [Var unitDataConId]
467 | otherwise = map Var vs