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,
28 nullIdEnv, addOneToIdEnv, lookupIdEnv, growIdEnvList,
29 SYN_IE(IdEnv), GenId{-instance NamedThing-}, SYN_IE(Id)
31 import Literal ( mkMachInt, Literal(..) )
32 import PrelVals ( unpackCStringId, unpackCString2Id,
33 integerZeroId, integerPlusOneId,
34 integerPlusTwoId, integerMinusOneId
36 import PrimOp ( PrimOp(..) )
37 import SpecUtils ( mkSpecialisedCon )
38 import SrcLoc ( noSrcLoc )
39 import TyCon ( TyCon{-instance Uniquable-} )
40 import Type ( maybeAppDataTyCon, getAppDataTyConExpandingDicts, SYN_IE(Type) )
41 import TysWiredIn ( stringTy )
42 import Unique ( integerTyConKey, ratioTyConKey, Unique{-instance Eq-} )
43 import UniqSupply -- all of it, really
44 import Util ( zipLazy, panic, assertPanic{-, pprTrace ToDo:rm-} )
45 --import Pretty--ToDo:rm
46 --import PprStyle--ToDo:rm
47 --import PprType --ToDo:rm
48 --import Outputable--ToDo:rm
49 --import PprEnv--ToDo:rm
51 isLeakFreeType x y = False -- safe option; ToDo
55 *************** OVERVIEW *********************
58 The business of this pass is to convert Core to Stg. On the way:
60 * We discard type lambdas and applications. In so doing we discard
61 "trivial" bindings such as
63 where t1, t2 are types
65 * We don't pin on correct arities any more, because they can be mucked up
66 by the lambda lifter. In particular, the lambda lifter can take a local
67 letrec-bound variable and make it a lambda argument, which shouldn't have
68 an arity. So SetStgVarInfo sets arities now.
70 * We do *not* pin on the correct free/live var info; that's done later.
71 Instead we use bOGUS_LVS and _FVS as a placeholder.
73 [Quite a bit of stuff that used to be here has moved
74 to tidyCorePgm (SimplCore.lhs) SLPJ Nov 96]
77 %************************************************************************
79 \subsection[coreToStg-programs]{Converting a core program and core bindings}
81 %************************************************************************
83 Because we're going to come across ``boring'' bindings like
84 \tr{let x = /\ tyvars -> y in ...}, we want to keep a small
85 environment, so we can just replace all occurrences of \tr{x}
89 type StgEnv = IdEnv StgArg
92 No free/live variable information is pinned on in this pass; it's added
94 we use @bOGUS_LVs@ and @bOGUS_FVs@ as placeholders.
97 bOGUS_LVs :: StgLiveVars
98 bOGUS_LVs = panic "bOGUS_LVs" -- emptyUniqSet (used when pprTracing)
101 bOGUS_FVs = panic "bOGUS_FVs" -- [] (ditto)
105 topCoreBindsToStg :: UniqSupply -- name supply
106 -> [CoreBinding] -- input
107 -> [StgBinding] -- output
109 topCoreBindsToStg us core_binds
110 = initUs us (coreBindsToStg nullIdEnv core_binds)
112 coreBindsToStg :: StgEnv -> [CoreBinding] -> UniqSM [StgBinding]
114 coreBindsToStg env [] = returnUs []
115 coreBindsToStg env (b:bs)
116 = coreBindToStg env b `thenUs` \ (new_b, new_env) ->
117 coreBindsToStg new_env bs `thenUs` \ new_bs ->
118 returnUs (new_b ++ new_bs)
121 %************************************************************************
123 \subsection[coreToStg-binds]{Converting bindings}
125 %************************************************************************
128 coreBindToStg :: StgEnv
130 -> UniqSM ([StgBinding], -- Empty or singleton
133 coreBindToStg env (NonRec binder rhs)
134 = coreRhsToStg env rhs `thenUs` \ stg_rhs ->
136 -- Binds to return if RHS is trivial
137 triv_binds | externallyVisibleId binder = [StgNonRec binder stg_rhs] -- Retain it
138 | otherwise = [] -- Discard it
141 StgRhsClosure cc bi fvs upd [] (StgApp atom [] lvs) ->
142 -- Trivial RHS, so augment envt, and ditch the binding
143 returnUs (triv_binds, new_env)
145 new_env = addOneToIdEnv env binder atom
147 StgRhsCon cc con_id [] ->
148 -- Trivial RHS, so augment envt, and ditch the binding
149 returnUs (triv_binds, new_env)
151 new_env = addOneToIdEnv env binder (StgConArg con_id)
153 other -> -- Non-trivial RHS, so don't augment envt
154 returnUs ([StgNonRec binder stg_rhs], env)
156 coreBindToStg env (Rec pairs)
157 = -- NB: *** WE DO NOT CHECK FOR TRIV_BINDS in REC BIND ****
160 (binders, rhss) = unzip pairs
162 mapUs (coreRhsToStg env) rhss `thenUs` \ stg_rhss ->
163 returnUs ([StgRec (binders `zip` stg_rhss)], env)
167 %************************************************************************
169 \subsection[coreToStg-rhss]{Converting right hand sides}
171 %************************************************************************
174 coreRhsToStg :: StgEnv -> CoreExpr -> UniqSM StgRhs
176 coreRhsToStg env core_rhs
177 = coreExprToStg env core_rhs `thenUs` \ stg_expr ->
179 let stg_rhs = case stg_expr of
180 StgLet (StgNonRec var1 rhs) (StgApp (StgVarArg var2) [] _)
181 | var1 == var2 -> rhs
182 -- This curious stuff is to unravel what a lambda turns into
183 -- We have to do it this way, rather than spot a lambda in the
184 -- incoming rhs. Why? Because trivial bindings might conceal
185 -- what the rhs is actually like.
187 StgCon con args _ -> StgRhsCon noCostCentre con args
189 other -> StgRhsClosure noCostCentre -- No cost centre (ToDo?)
192 Updatable -- Be pessimistic
200 %************************************************************************
202 \subsection[coreToStg-atoms{Converting atoms}
204 %************************************************************************
207 coreArgsToStg :: StgEnv -> [CoreArg] -> ([Type], [StgArg])
209 coreArgsToStg env [] = ([], [])
210 coreArgsToStg env (a:as)
212 TyArg t -> (t:trest, vrest)
213 UsageArg u -> (trest, vrest)
214 VarArg v -> (trest, stgLookup env v : vrest)
215 LitArg l -> (trest, StgLitArg l : vrest)
217 (trest,vrest) = coreArgsToStg env as
221 %************************************************************************
223 \subsection[coreToStg-exprs]{Converting core expressions}
225 %************************************************************************
228 coreExprToStg :: StgEnv -> CoreExpr -> UniqSM StgExpr
230 coreExprToStg env (Lit lit)
231 = returnUs (StgApp (StgLitArg lit) [] bOGUS_LVs)
233 coreExprToStg env (Var var)
234 = returnUs (mk_app (stgLookup env var) [])
236 coreExprToStg env (Con con args)
238 (types, stg_atoms) = coreArgsToStg env args
239 spec_con = mkSpecialisedCon con types
241 returnUs (StgCon spec_con stg_atoms bOGUS_LVs)
243 coreExprToStg env (Prim op args)
245 (types, stg_atoms) = coreArgsToStg env args
247 returnUs (StgPrim op stg_atoms bOGUS_LVs)
250 %************************************************************************
252 \subsubsection[coreToStg-lambdas]{Lambda abstractions}
254 %************************************************************************
257 coreExprToStg env expr@(Lam _ _)
259 (_,_, binders, body) = collectBinders expr
261 coreExprToStg env body `thenUs` \ stg_body ->
263 if null binders then -- it was all type/usage binders; tossed
266 newStgVar (coreExprType expr) `thenUs` \ var ->
268 (StgLet (StgNonRec var
269 (StgRhsClosure noCostCentre
272 ReEntrant -- binders is non-empty
275 (StgApp (StgVarArg var) [] bOGUS_LVs))
278 %************************************************************************
280 \subsubsection[coreToStg-applications]{Applications}
282 %************************************************************************
285 coreExprToStg env expr@(App _ _)
287 (fun,args) = collect_args expr []
288 (_, stg_args) = coreArgsToStg env args
290 -- Now deal with the function
292 (Var fun_id, _) -> -- A function Id, so do an StgApp; it's ok if
293 -- there are no arguments.
294 returnUs (mk_app (stgLookup env fun_id) stg_args)
296 (non_var_fun, []) -> -- No value args, so recurse into the function
297 coreExprToStg env non_var_fun
299 other -> -- A non-variable applied to things; better let-bind it.
300 newStgVar (coreExprType fun) `thenUs` \ fun_id ->
301 coreExprToStg env fun `thenUs` \ (stg_fun) ->
303 fun_rhs = StgRhsClosure noCostCentre -- No cost centre (ToDo?)
306 SingleEntry -- Only entered once
310 returnUs (StgLet (StgNonRec fun_id fun_rhs)
311 (StgApp (StgVarArg fun_id) stg_args bOGUS_LVs))
313 -- Collect arguments, discarding type/usage applications
314 collect_args (App e (TyArg _)) args = collect_args e args
315 collect_args (App e (UsageArg _)) args = collect_args e args
316 collect_args (App fun arg) args = collect_args fun (arg:args)
317 collect_args fun args = (fun, args)
320 %************************************************************************
322 \subsubsection[coreToStg-cases]{Case expressions}
324 %************************************************************************
327 coreExprToStg env (Case discrim alts)
328 = coreExprToStg env discrim `thenUs` \ stg_discrim ->
329 alts_to_stg discrim alts `thenUs` \ stg_alts ->
330 getUnique `thenUs` \ uniq ->
339 discrim_ty = coreExprType discrim
340 (_, discrim_ty_args, _) = getAppDataTyConExpandingDicts discrim_ty
342 alts_to_stg discrim (AlgAlts alts deflt)
343 = default_to_stg discrim deflt `thenUs` \ stg_deflt ->
344 mapUs boxed_alt_to_stg alts `thenUs` \ stg_alts ->
345 returnUs (StgAlgAlts discrim_ty stg_alts stg_deflt)
347 boxed_alt_to_stg (con, bs, rhs)
348 = coreExprToStg env rhs `thenUs` \ stg_rhs ->
349 returnUs (spec_con, bs, [ True | b <- bs ]{-bogus use mask-}, stg_rhs)
351 spec_con = mkSpecialisedCon con discrim_ty_args
353 alts_to_stg discrim (PrimAlts alts deflt)
354 = default_to_stg discrim deflt `thenUs` \ stg_deflt ->
355 mapUs unboxed_alt_to_stg alts `thenUs` \ stg_alts ->
356 returnUs (StgPrimAlts discrim_ty stg_alts stg_deflt)
358 unboxed_alt_to_stg (lit, rhs)
359 = coreExprToStg env rhs `thenUs` \ stg_rhs ->
360 returnUs (lit, stg_rhs)
362 default_to_stg discrim NoDefault
363 = returnUs StgNoDefault
365 default_to_stg discrim (BindDefault binder rhs)
366 = coreExprToStg env rhs `thenUs` \ stg_rhs ->
367 returnUs (StgBindDefault binder True{-used? no it is lying-} stg_rhs)
370 %************************************************************************
372 \subsubsection[coreToStg-let(rec)]{Let and letrec expressions}
374 %************************************************************************
377 coreExprToStg env (Let bind body)
378 = coreBindToStg env bind `thenUs` \ (stg_binds, new_env) ->
379 coreExprToStg new_env body `thenUs` \ stg_body ->
380 returnUs (mkStgLets stg_binds stg_body)
384 %************************************************************************
386 \subsubsection[coreToStg-scc]{SCC expressions}
388 %************************************************************************
390 Covert core @scc@ expression directly to STG @scc@ expression.
392 coreExprToStg env (SCC cc expr)
393 = coreExprToStg env expr `thenUs` \ stg_expr ->
394 returnUs (StgSCC (coreExprType expr) cc stg_expr)
398 coreExprToStg env (Coerce c ty expr) = coreExprToStg env expr
402 %************************************************************************
404 \subsection[coreToStg-misc]{Miscellaneous helping functions}
406 %************************************************************************
408 There's not anything interesting we can ASSERT about \tr{var} if it
409 isn't in the StgEnv. (WDP 94/06)
412 stgLookup :: StgEnv -> Id -> StgArg
413 stgLookup env var = case (lookupIdEnv env var) of
414 Nothing -> StgVarArg var
420 newStgVar :: Type -> UniqSM Id
422 = getUnique `thenUs` \ uniq ->
423 returnUs (mkSysLocal SLIT("stg") uniq ty noSrcLoc)
427 mkStgLets :: [StgBinding]
428 -> StgExpr -- body of let
431 mkStgLets binds body = foldr StgLet body binds
433 -- mk_app spots an StgCon in a function position,
434 -- and turns it into an StgCon. See notes with
435 -- getArgAmode in CgBindery.
436 mk_app (StgConArg con) args = StgCon con args bOGUS_LVs
437 mk_app other_fun args = StgApp other_fun args bOGUS_LVs