2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
4 %************************************************************************
6 \section[CoreToStg]{Converting core syntax to STG syntax}
8 %************************************************************************
10 Convert a @CoreSyntax@ program to a @StgSyntax@ program.
13 #include "HsVersions.h"
15 module CoreToStg ( topCoreBindsToStg ) where
18 IMPORT_1_3(Ratio(numerator,denominator))
20 import CoreSyn -- input
21 import StgSyn -- output
23 import Bag ( emptyBag, unitBag, unionBags, unionManyBags, bagToList )
24 import CoreUtils ( coreExprType )
25 import CostCentre ( noCostCentre )
26 import Id ( mkSysLocal, idType, isBottomingId,
29 nullIdEnv, addOneToIdEnv, lookupIdEnv, growIdEnvList,
30 SYN_IE(IdEnv), GenId{-instance NamedThing-}, SYN_IE(Id)
32 import Literal ( mkMachInt, Literal(..) )
33 import PrelVals ( unpackCStringId, unpackCString2Id,
34 integerZeroId, integerPlusOneId,
35 integerPlusTwoId, integerMinusOneId
37 import PrimOp ( PrimOp(..) )
38 import SpecUtils ( mkSpecialisedCon )
39 import SrcLoc ( noSrcLoc )
40 import TyCon ( TyCon{-instance Uniquable-} )
41 import Type ( getAppDataTyConExpandingDicts, SYN_IE(Type) )
42 import TysWiredIn ( stringTy )
43 import Unique ( integerTyConKey, ratioTyConKey, Unique{-instance Eq-} )
44 import UniqSupply -- all of it, really
45 import Util ( zipLazy, panic, assertPanic{-, pprTrace ToDo:rm-} )
47 isLeakFreeType x y = False -- safe option; ToDo
51 *************** OVERVIEW *********************
54 The business of this pass is to convert Core to Stg. On the way:
56 * We discard type lambdas and applications. In so doing we discard
57 "trivial" bindings such as
59 where t1, t2 are types
61 * We don't pin on correct arities any more, because they can be mucked up
62 by the lambda lifter. In particular, the lambda lifter can take a local
63 letrec-bound variable and make it a lambda argument, which shouldn't have
64 an arity. So SetStgVarInfo sets arities now.
66 * We do *not* pin on the correct free/live var info; that's done later.
67 Instead we use bOGUS_LVS and _FVS as a placeholder.
69 [Quite a bit of stuff that used to be here has moved
70 to tidyCorePgm (SimplCore.lhs) SLPJ Nov 96]
73 %************************************************************************
75 \subsection[coreToStg-programs]{Converting a core program and core bindings}
77 %************************************************************************
79 Because we're going to come across ``boring'' bindings like
80 \tr{let x = /\ tyvars -> y in ...}, we want to keep a small
81 environment, so we can just replace all occurrences of \tr{x}
85 type StgEnv = IdEnv StgArg
88 No free/live variable information is pinned on in this pass; it's added
90 we use @bOGUS_LVs@ and @bOGUS_FVs@ as placeholders.
93 bOGUS_LVs :: StgLiveVars
94 bOGUS_LVs = panic "bOGUS_LVs" -- emptyUniqSet (used when pprTracing)
97 bOGUS_FVs = panic "bOGUS_FVs" -- [] (ditto)
101 topCoreBindsToStg :: UniqSupply -- name supply
102 -> [CoreBinding] -- input
103 -> [StgBinding] -- output
105 topCoreBindsToStg us core_binds
106 = initUs us (coreBindsToStg nullIdEnv core_binds)
108 coreBindsToStg :: StgEnv -> [CoreBinding] -> UniqSM [StgBinding]
110 coreBindsToStg env [] = returnUs []
111 coreBindsToStg env (b:bs)
112 = coreBindToStg env b `thenUs` \ (new_b, new_env) ->
113 coreBindsToStg new_env bs `thenUs` \ new_bs ->
114 returnUs (new_b ++ new_bs)
117 %************************************************************************
119 \subsection[coreToStg-binds]{Converting bindings}
121 %************************************************************************
124 coreBindToStg :: StgEnv
126 -> UniqSM ([StgBinding], -- Empty or singleton
129 coreBindToStg env (NonRec binder rhs)
130 = coreRhsToStg env rhs `thenUs` \ stg_rhs ->
132 -- Binds to return if RHS is trivial
133 triv_binds | externallyVisibleId binder = [StgNonRec binder stg_rhs] -- Retain it
134 | otherwise = [] -- Discard it
137 StgRhsClosure cc bi fvs upd [] (StgApp atom [] lvs) ->
138 -- Trivial RHS, so augment envt, and ditch the binding
139 returnUs (triv_binds, new_env)
141 new_env = addOneToIdEnv env binder atom
143 StgRhsCon cc con_id [] ->
144 -- Trivial RHS, so augment envt, and ditch the binding
145 returnUs (triv_binds, new_env)
147 new_env = addOneToIdEnv env binder (StgConArg con_id)
149 other -> -- Non-trivial RHS, so don't augment envt
150 returnUs ([StgNonRec binder stg_rhs], env)
152 coreBindToStg env (Rec pairs)
153 = -- NB: *** WE DO NOT CHECK FOR TRIV_BINDS in REC BIND ****
156 (binders, rhss) = unzip pairs
158 mapUs (coreRhsToStg env) rhss `thenUs` \ stg_rhss ->
159 returnUs ([StgRec (binders `zip` stg_rhss)], env)
163 %************************************************************************
165 \subsection[coreToStg-rhss]{Converting right hand sides}
167 %************************************************************************
170 coreRhsToStg :: StgEnv -> CoreExpr -> UniqSM StgRhs
172 coreRhsToStg env core_rhs
173 = coreExprToStg env core_rhs `thenUs` \ stg_expr ->
175 let stg_rhs = case stg_expr of
176 StgLet (StgNonRec var1 rhs) (StgApp (StgVarArg var2) [] _)
177 | var1 == var2 -> rhs
178 -- This curious stuff is to unravel what a lambda turns into
179 -- We have to do it this way, rather than spot a lambda in the
180 -- incoming rhs. Why? Because trivial bindings might conceal
181 -- what the rhs is actually like.
183 StgCon con args _ -> StgRhsCon noCostCentre con args
185 other -> StgRhsClosure noCostCentre -- No cost centre (ToDo?)
188 Updatable -- Be pessimistic
196 %************************************************************************
198 \subsection[coreToStg-atoms{Converting atoms}
200 %************************************************************************
203 coreArgsToStg :: StgEnv -> [CoreArg] -> ([Type], [StgArg])
205 coreArgsToStg env [] = ([], [])
206 coreArgsToStg env (a:as)
208 TyArg t -> (t:trest, vrest)
209 UsageArg u -> (trest, vrest)
210 VarArg v -> (trest, stgLookup env v : vrest)
211 LitArg l -> (trest, StgLitArg l : vrest)
213 (trest,vrest) = coreArgsToStg env as
217 %************************************************************************
219 \subsection[coreToStg-exprs]{Converting core expressions}
221 %************************************************************************
224 coreExprToStg :: StgEnv -> CoreExpr -> UniqSM StgExpr
226 coreExprToStg env (Lit lit)
227 = returnUs (StgApp (StgLitArg lit) [] bOGUS_LVs)
229 coreExprToStg env (Var var)
230 = returnUs (mk_app (stgLookup env var) [])
232 coreExprToStg env (Con con args)
234 (types, stg_atoms) = coreArgsToStg env args
235 spec_con = mkSpecialisedCon con types
237 returnUs (StgCon spec_con stg_atoms bOGUS_LVs)
239 coreExprToStg env (Prim op args)
241 (types, stg_atoms) = coreArgsToStg env args
243 returnUs (StgPrim op stg_atoms bOGUS_LVs)
246 %************************************************************************
248 \subsubsection[coreToStg-lambdas]{Lambda abstractions}
250 %************************************************************************
253 coreExprToStg env expr@(Lam _ _)
255 (_,_, binders, body) = collectBinders expr
257 coreExprToStg env body `thenUs` \ stg_body ->
259 if null binders then -- it was all type/usage binders; tossed
262 newStgVar (coreExprType expr) `thenUs` \ var ->
264 (StgLet (StgNonRec var
265 (StgRhsClosure noCostCentre
268 ReEntrant -- binders is non-empty
271 (StgApp (StgVarArg var) [] bOGUS_LVs))
274 %************************************************************************
276 \subsubsection[coreToStg-applications]{Applications}
278 %************************************************************************
281 coreExprToStg env expr@(App _ _)
283 (fun,args) = collect_args expr []
284 (_, stg_args) = coreArgsToStg env args
286 -- Now deal with the function
288 (Var fun_id, _) -> -- A function Id, so do an StgApp; it's ok if
289 -- there are no arguments.
290 returnUs (mk_app (stgLookup env fun_id) stg_args)
292 (non_var_fun, []) -> -- No value args, so recurse into the function
293 coreExprToStg env non_var_fun
295 other -> -- A non-variable applied to things; better let-bind it.
296 -- pprTrace "coreExprToStg" (ppr PprDebug expr) $
297 newStgVar (coreExprType fun) `thenUs` \ fun_id ->
298 coreExprToStg env fun `thenUs` \ (stg_fun) ->
300 fun_rhs = StgRhsClosure noCostCentre -- No cost centre (ToDo?)
303 SingleEntry -- Only entered once
307 returnUs (StgLet (StgNonRec fun_id fun_rhs)
308 (StgApp (StgVarArg fun_id) stg_args bOGUS_LVs))
310 -- Collect arguments, discarding type/usage applications
311 collect_args (App e (TyArg _)) args = collect_args e args
312 collect_args (App e (UsageArg _)) args = collect_args e args
313 collect_args (App fun arg) args = collect_args fun (arg:args)
314 collect_args (Coerce _ _ expr) args = collect_args expr args
315 collect_args fun args = (fun, args)
318 %************************************************************************
320 \subsubsection[coreToStg-cases]{Case expressions}
322 %************************************************************************
325 coreExprToStg env (Case discrim alts)
326 = coreExprToStg env discrim `thenUs` \ stg_discrim ->
327 alts_to_stg discrim alts `thenUs` \ stg_alts ->
328 getUnique `thenUs` \ uniq ->
337 discrim_ty = coreExprType discrim
338 (_, discrim_ty_args, _) = getAppDataTyConExpandingDicts discrim_ty
340 alts_to_stg discrim (AlgAlts alts deflt)
341 = default_to_stg discrim deflt `thenUs` \ stg_deflt ->
342 mapUs boxed_alt_to_stg alts `thenUs` \ stg_alts ->
343 returnUs (StgAlgAlts discrim_ty stg_alts stg_deflt)
345 boxed_alt_to_stg (con, bs, rhs)
346 = coreExprToStg env rhs `thenUs` \ stg_rhs ->
347 returnUs (spec_con, bs, [ True | b <- bs ]{-bogus use mask-}, stg_rhs)
349 spec_con = mkSpecialisedCon con discrim_ty_args
351 alts_to_stg discrim (PrimAlts alts deflt)
352 = default_to_stg discrim deflt `thenUs` \ stg_deflt ->
353 mapUs unboxed_alt_to_stg alts `thenUs` \ stg_alts ->
354 returnUs (StgPrimAlts discrim_ty stg_alts stg_deflt)
356 unboxed_alt_to_stg (lit, rhs)
357 = coreExprToStg env rhs `thenUs` \ stg_rhs ->
358 returnUs (lit, stg_rhs)
360 default_to_stg discrim NoDefault
361 = returnUs StgNoDefault
363 default_to_stg discrim (BindDefault binder rhs)
364 = coreExprToStg env rhs `thenUs` \ stg_rhs ->
365 returnUs (StgBindDefault binder True{-used? no it is lying-} stg_rhs)
368 %************************************************************************
370 \subsubsection[coreToStg-let(rec)]{Let and letrec expressions}
372 %************************************************************************
375 coreExprToStg env (Let bind body)
376 = coreBindToStg env bind `thenUs` \ (stg_binds, new_env) ->
377 coreExprToStg new_env body `thenUs` \ stg_body ->
378 returnUs (mkStgLets stg_binds stg_body)
382 %************************************************************************
384 \subsubsection[coreToStg-scc]{SCC expressions}
386 %************************************************************************
388 Covert core @scc@ expression directly to STG @scc@ expression.
390 coreExprToStg env (SCC cc expr)
391 = coreExprToStg env expr `thenUs` \ stg_expr ->
392 returnUs (StgSCC (coreExprType expr) cc stg_expr)
396 coreExprToStg env (Coerce c ty expr) = coreExprToStg env expr
400 %************************************************************************
402 \subsection[coreToStg-misc]{Miscellaneous helping functions}
404 %************************************************************************
406 There's not anything interesting we can ASSERT about \tr{var} if it
407 isn't in the StgEnv. (WDP 94/06)
410 stgLookup :: StgEnv -> Id -> StgArg
411 stgLookup env var = case (lookupIdEnv env var) of
412 Nothing -> StgVarArg var
418 newStgVar :: Type -> UniqSM Id
420 = getUnique `thenUs` \ uniq ->
421 returnUs (mkSysLocal SLIT("stg") uniq ty noSrcLoc)
425 mkStgLets :: [StgBinding]
426 -> StgExpr -- body of let
429 mkStgLets binds body = foldr StgLet body binds
431 -- mk_app spots an StgCon in a function position,
432 -- and turns it into an StgCon. See notes with
433 -- getArgAmode in CgBindery.
434 mk_app (StgConArg con) args = StgCon con args bOGUS_LVs
435 mk_app other_fun args = StgApp other_fun args bOGUS_LVs