2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1995
4 %********************************************************
6 \section[CgExpr]{Converting @StgExpr@s}
8 %********************************************************
11 #include "HsVersions.h"
13 module CgExpr ( cgExpr, cgSccExpr, getPrimOpArgAmodes ) where
16 import CgLoop2 -- here for paranoia-checking
22 import AbsCUtils ( mkAbsCStmts, mkAbstractCs )
23 import CgBindery ( getArgAmodes )
24 import CgCase ( cgCase, saveVolatileVarsAndRegs )
25 import CgClosure ( cgRhsClosure )
26 import CgCon ( buildDynCon, cgReturnDataCon )
27 import CgHeapery ( allocHeap )
28 import CgLetNoEscape ( cgLetNoEscapeClosure )
29 import CgRetConv ( dataReturnConvAlg, ctrlReturnConvAlg,
30 DataReturnConvention(..), CtrlReturnConvention(..),
31 assignPrimOpResultRegs, makePrimOpArgsRobust
33 import CgTailCall ( cgTailCall, performReturn,
34 mkDynamicAlgReturnCode, mkPrimReturnCode
36 import CLabel ( mkPhantomInfoTableLabel, mkInfoTableVecTblLabel )
37 import ClosureInfo ( mkClosureLFInfo )
38 import CostCentre ( setToAbleCostCentre, isDupdCC )
39 import HeapOffs ( VirtualSpBOffset(..) )
40 import Id ( mkIdSet, unionIdSets, GenId{-instance Outputable-} )
41 import PprStyle ( PprStyle(..) )
42 import PrimOp ( primOpCanTriggerGC, primOpHeapReq, HeapRequirement(..),
43 getPrimOpResultInfo, PrimOp(..), PrimOpResultInfo(..)
45 import PrimRep ( getPrimRepSize, PrimRep(..) )
46 import TyCon ( tyConDataCons )
47 import Util ( panic, pprPanic )
50 This module provides the support code for @StgToAbstractC@ to deal
51 with STG {\em expressions}. See also @CgClosure@, which deals
52 with closures, and @CgCon@, which deals with constructors.
55 cgExpr :: StgExpr -- input
59 %********************************************************
63 %********************************************************
65 ``Applications'' mean {\em tail calls}, a service provided by module
66 @CgTailCall@. This includes literals, which show up as
67 @(STGApp (StgLitArg 42) [])@.
70 cgExpr (StgApp fun args live_vars) = cgTailCall fun args live_vars
73 %********************************************************
75 %* STG ConApps (for inline versions) *
77 %********************************************************
80 cgExpr (StgCon con args live_vars)
81 = getArgAmodes args `thenFC` \ amodes ->
82 cgReturnDataCon con amodes (all zero_size args) live_vars
84 zero_size atom = getPrimRepSize (getArgPrimRep atom) == 0
87 %********************************************************
89 %* STG PrimApps (unboxed primitive ops) *
91 %********************************************************
93 Here is where we insert real live machine instructions.
96 cgExpr x@(StgPrim op args live_vars)
97 = getPrimOpArgAmodes op args `thenFC` \ arg_amodes ->
99 result_regs = assignPrimOpResultRegs op
100 result_amodes = map CReg result_regs
101 may_gc = primOpCanTriggerGC op
102 dyn_tag = head result_amodes
103 -- The tag from a primitive op returning an algebraic data type
104 -- is returned in the first result_reg_amode
107 -- Use registers for args, and assign args to the regs
108 -- (Can-trigger-gc primops guarantee to have their args in regs)
110 (arg_robust_amodes, liveness_mask, arg_assts)
111 = makePrimOpArgsRobust op arg_amodes
113 liveness_arg = mkIntCLit liveness_mask
117 COpStmt result_amodes op
118 (pin_liveness op liveness_arg arg_robust_amodes)
123 -- Use args from their current amodes.
125 liveness_mask = panic "cgExpr: liveness of non-GC-ing primop touched\n"
128 COpStmt result_amodes op arg_amodes liveness_mask [{-no vol_regs-}],
131 ) `thenFC` \ (do_before_stack_cleanup,
132 do_just_before_jump) ->
134 case (getPrimOpResultInfo op) of
137 performReturn do_before_stack_cleanup
138 (\ sequel -> robustifySequel may_gc sequel
139 `thenFC` \ (ret_asst, sequel') ->
140 absC (ret_asst `mkAbsCStmts` do_just_before_jump)
142 mkPrimReturnCode sequel')
146 profCtrC SLIT("RET_NEW_IN_REGS") [num_of_fields] `thenC`
148 performReturn do_before_stack_cleanup
149 (\ sequel -> robustifySequel may_gc sequel
150 `thenFC` \ (ret_asst, sequel') ->
151 absC (mkAbstractCs [ret_asst,
154 -- Must load info ptr here, not in do_just_before_stack_cleanup,
155 -- because the info-ptr reg clashes with argument registers
158 mkDynamicAlgReturnCode tycon dyn_tag sequel')
162 -- Here, the destination _can_ be an update frame, so we need to make sure that
163 -- infoptr (R2) is loaded with the constructor's info ptr.
165 info_ptr_assign = CAssign (CReg infoptr) info_lbl
168 = case (ctrlReturnConvAlg tycon) of
169 VectoredReturn _ -> vec_lbl
170 UnvectoredReturn _ -> dir_lbl
172 vec_lbl = CTableEntry (CLbl (mkInfoTableVecTblLabel tycon) DataPtrRep)
175 data_con = head (tyConDataCons tycon)
177 (dir_lbl, num_of_fields)
178 = case (dataReturnConvAlg data_con) of
180 -> (CLbl (mkPhantomInfoTableLabel data_con) DataPtrRep,
181 mkIntCLit (length rs)) -- for ticky-ticky only
184 -> pprPanic "CgExpr: can't return prim in heap:" (ppr PprDebug data_con)
185 -- Never used, and no point in generating
188 -- for all PrimOps except ccalls, we pin the liveness info
189 -- on as the first "argument"
190 -- ToDo: un-duplicate?
192 pin_liveness (CCallOp _ _ _ _ _) _ args = args
193 pin_liveness other_op liveness_arg args
196 -- We only need to worry about the sequel when we may GC and the
197 -- sequel is OnStack. If that's the case, arrange to pull the
198 -- sequel out into RetReg before performing the primOp.
200 robustifySequel True sequel@(OnStack _) =
201 sequelToAmode sequel `thenFC` \ amode ->
202 returnFC (CAssign (CReg RetReg) amode, InRetReg)
203 robustifySequel _ sequel = returnFC (AbsCNop, sequel)
206 %********************************************************
208 %* Case expressions *
210 %********************************************************
211 Case-expression conversion is complicated enough to have its own
215 cgExpr (StgCase expr live_vars save_vars uniq alts)
216 = cgCase expr live_vars save_vars uniq alts
220 %********************************************************
224 %********************************************************
225 \subsection[let-and-letrec-codegen]{Converting @StgLet@ and @StgLetrec@}
228 cgExpr (StgLet (StgNonRec name rhs) expr)
229 = cgRhs name rhs `thenFC` \ (name, info) ->
230 addBindC name info `thenC`
233 cgExpr (StgLet (StgRec pairs) expr)
234 = fixC (\ new_bindings -> addBindsC new_bindings `thenC`
235 listFCs [ cgRhs b e | (b,e) <- pairs ]
236 ) `thenFC` \ new_bindings ->
238 addBindsC new_bindings `thenC`
243 cgExpr (StgLetNoEscape live_in_whole_let live_in_rhss bindings body)
244 = -- Figure out what volatile variables to save
245 nukeDeadBindings live_in_whole_let `thenC`
246 saveVolatileVarsAndRegs live_in_rhss
247 `thenFC` \ (save_assts, rhs_eob_info, maybe_cc_slot) ->
249 -- ToDo: cost centre???
251 -- Save those variables right now!
252 absC save_assts `thenC`
254 -- Produce code for the rhss
255 -- and add suitable bindings to the environment
256 cgLetNoEscapeBindings live_in_rhss rhs_eob_info maybe_cc_slot bindings `thenC`
259 setEndOfBlockInfo rhs_eob_info (cgExpr body)
263 %********************************************************
267 %********************************************************
268 \subsection[scc-codegen]{Converting StgSCC}
270 SCC expressions are treated specially. They set the current cost
273 For evaluation scoping we also need to save the cost centre in an
274 ``restore CC frame''. We only need to do this once before setting all
278 cgExpr scc_expr@(StgSCC ty cc expr) = cgSccExpr scc_expr
281 @cgSccExpr@ (also used in \tr{CgClosure}):
282 We *don't* set the cost centre for CAF/Dict cost centres
283 [Likewise Subsumed and NoCostCentre, but they probably
284 don't exist in an StgSCC expression.]
286 cgSccExpr (StgSCC ty cc expr)
287 = (if setToAbleCostCentre cc then
288 costCentresC SLIT("SET_CCC")
289 [mkCCostCentre cc, mkIntCLit (if isDupdCC cc then 1 else 0)]
297 %********************************************************
299 %* Non-top-level bindings *
301 %********************************************************
302 \subsection[non-top-level-bindings]{Converting non-top-level bindings}
304 @cgBinding@ is only used for let/letrec, not for unboxed bindings.
305 So the kind should always be @PtrRep@.
307 We rely on the support code in @CgCon@ (to do constructors) and
308 in @CgClosure@ (to do closures).
311 cgRhs :: Id -> StgRhs -> FCode (Id, CgIdInfo)
312 -- the Id is passed along so a binding can be set up
314 cgRhs name (StgRhsCon maybe_cc con args)
315 = getArgAmodes args `thenFC` \ amodes ->
316 buildDynCon name maybe_cc con amodes (all zero_size args)
318 returnFC (name, idinfo)
320 zero_size atom = getPrimRepSize (getArgPrimRep atom) == 0
322 cgRhs name (StgRhsClosure cc bi fvs upd_flag args body)
323 = cgRhsClosure name cc bi fvs args body lf_info
325 lf_info = mkClosureLFInfo False{-not top level-} fvs upd_flag args body
329 cgLetNoEscapeBindings live_in_rhss rhs_eob_info maybe_cc_slot (StgNonRec binder rhs)
330 = cgLetNoEscapeRhs live_in_rhss rhs_eob_info maybe_cc_slot binder rhs
331 `thenFC` \ (binder, info) ->
334 cgLetNoEscapeBindings live_in_rhss rhs_eob_info maybe_cc_slot (StgRec pairs)
335 = fixC (\ new_bindings ->
336 addBindsC new_bindings `thenC`
337 listFCs [ cgLetNoEscapeRhs full_live_in_rhss rhs_eob_info
338 maybe_cc_slot b e | (b,e) <- pairs ]
339 ) `thenFC` \ new_bindings ->
341 addBindsC new_bindings
343 -- We add the binders to the live-in-rhss set so that we don't
344 -- delete the bindings for the binder from the environment!
345 full_live_in_rhss = live_in_rhss `unionIdSets` (mkIdSet [b | (b,r) <- pairs])
348 :: StgLiveVars -- Live in rhss
350 -> Maybe VirtualSpBOffset
353 -> FCode (Id, CgIdInfo)
355 cgLetNoEscapeRhs full_live_in_rhss rhs_eob_info maybe_cc_slot binder
356 (StgRhsClosure cc bi _ upd_flag args body)
357 = -- We could check the update flag, but currently we don't switch it off
358 -- for let-no-escaped things, so we omit the check too!
360 -- Updatable -> panic "cgLetNoEscapeRhs" -- Nothing to update!
361 -- other -> cgLetNoEscapeClosure binder cc bi live_in_whole_let live_in_rhss args body
362 cgLetNoEscapeClosure binder cc bi full_live_in_rhss rhs_eob_info maybe_cc_slot args body
364 -- For a constructor RHS we want to generate a single chunk of code which
365 -- can be jumped to from many places, which will return the constructor.
366 -- It's easy; just behave as if it was an StgRhsClosure with a ConApp inside!
367 cgLetNoEscapeRhs full_live_in_rhss rhs_eob_info maybe_cc_slot binder
368 (StgRhsCon cc con args)
369 = cgLetNoEscapeClosure binder cc stgArgOcc{-safe-} full_live_in_rhss rhs_eob_info maybe_cc_slot
370 [] --No args; the binder is data structure, not a function
371 (StgCon con args full_live_in_rhss)
374 Some PrimOps require a {\em fixed} amount of heap allocation. Rather
375 than tidy away ready for GC and do a full heap check, we simply
376 allocate a completely uninitialised block in-line, just like any other
377 thunk/constructor allocation, and pass it to the PrimOp as its first
378 argument. Remember! The PrimOp is entirely responsible for
379 initialising the object. In particular, the PrimOp had better not
380 trigger GC before it has filled it in, and even then it had better
381 make sure that the GC can find the object somehow.
383 Main current use: allocating SynchVars.
386 getPrimOpArgAmodes op args
387 = getArgAmodes args `thenFC` \ arg_amodes ->
389 case primOpHeapReq op of
390 FixedHeapRequired size -> allocHeap size `thenFC` \ amode ->
391 returnFC (amode : arg_amodes)
393 _ -> returnFC arg_amodes