Fix warnings in CgClosure
[ghc-hetmet.git] / compiler / codeGen / CgClosure.lhs
1 %
2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 %
5 \section[CgClosure]{Code generation for closures}
6
7 This module provides the support code for @StgToAbstractC@ to deal
8 with {\em closures} on the RHSs of let(rec)s.  See also
9 @CgCon@, which deals with constructors.
10
11 \begin{code}
12 module CgClosure ( cgTopRhsClosure, 
13                    cgStdRhsClosure, 
14                    cgRhsClosure,
15                    emitBlackHoleCode,
16                    ) where
17
18 #include "HsVersions.h"
19
20 import {-# SOURCE #-} CgExpr ( cgExpr )
21
22 import CgMonad
23 import CgBindery
24 import CgHeapery
25 import CgStackery
26 import CgProf
27 import CgTicky
28 import CgParallel
29 import CgInfoTbls
30 import CgCallConv
31 import CgUtils
32 import ClosureInfo
33 import SMRep
34 import Cmm
35 import CmmUtils
36 import CLabel
37 import StgSyn
38 import CostCentre       
39 import Id
40 import Name
41 import Module
42 import ListSetOps
43 import Util
44 import BasicTypes
45 import StaticFlags
46 import DynFlags
47 import Constants
48 import Outputable
49 import FastString
50
51 import Data.List
52 \end{code}
53
54 %********************************************************
55 %*                                                      *
56 \subsection[closures-no-free-vars]{Top-level closures}
57 %*                                                      *
58 %********************************************************
59
60 For closures bound at top level, allocate in static space.
61 They should have no free variables.
62
63 \begin{code}
64 cgTopRhsClosure :: Id
65                 -> CostCentreStack      -- Optional cost centre annotation
66                 -> StgBinderInfo
67                 -> UpdateFlag
68                 -> [Id]         -- Args
69                 -> StgExpr
70                 -> FCode (Id, CgIdInfo)
71
72 cgTopRhsClosure id ccs binder_info upd_flag args body = do
73   {     -- LAY OUT THE OBJECT
74     let name = idName id
75   ; lf_info  <- mkClosureLFInfo id TopLevel [] upd_flag args
76   ; srt_info <- getSRTInfo
77   ; mod_name <- getModuleName
78   ; let descr         = closureDescription mod_name name
79         closure_info  = mkClosureInfo True id lf_info 0 0 srt_info descr
80         closure_label = mkLocalClosureLabel name $ idCafInfo id
81         cg_id_info    = stableIdInfo id (mkLblExpr closure_label) lf_info
82         closure_rep   = mkStaticClosureFields closure_info ccs True []
83
84          -- BUILD THE OBJECT, AND GENERATE INFO TABLE (IF NECESSARY)
85   ; emitDataLits closure_label closure_rep
86   ; forkClosureBody (closureCodeBody binder_info closure_info
87                                      ccs args body)
88
89   ; returnFC (id, cg_id_info) }
90 \end{code}
91
92 %********************************************************
93 %*                                                      *
94 \subsection[non-top-level-closures]{Non top-level closures}
95 %*                                                      *
96 %********************************************************
97
98 For closures with free vars, allocate in heap.
99
100 \begin{code}
101 cgStdRhsClosure
102         :: Id
103         -> CostCentreStack      -- Optional cost centre annotation
104         -> StgBinderInfo
105         -> [Id]                 -- Free vars
106         -> [Id]                 -- Args
107         -> StgExpr
108         -> LambdaFormInfo
109         -> [StgArg]             -- payload
110         -> FCode (Id, CgIdInfo)
111
112 cgStdRhsClosure bndr cc _bndr_info _fvs args body lf_info payload 
113   = do  -- AHA!  A STANDARD-FORM THUNK
114   {     -- LAY OUT THE OBJECT
115     amodes <- getArgAmodes payload
116   ; mod_name <- getModuleName
117   ; let (tot_wds, ptr_wds, amodes_w_offsets) 
118             = mkVirtHeapOffsets (isLFThunk lf_info) amodes
119
120         descr        = closureDescription mod_name (idName bndr)
121         closure_info = mkClosureInfo False      -- Not static
122                                      bndr lf_info tot_wds ptr_wds 
123                                      NoC_SRT    -- No SRT for a std-form closure
124                                      descr
125                 
126   ; (use_cc, blame_cc) <- chooseDynCostCentres cc args body
127
128         -- BUILD THE OBJECT
129   ; heap_offset <- allocDynClosure closure_info use_cc blame_cc amodes_w_offsets
130
131         -- RETURN
132   ; returnFC (bndr, heapIdInfo bndr heap_offset lf_info) }
133 \end{code}
134
135 Here's the general case.
136
137 \begin{code}
138 cgRhsClosure    :: Id
139                 -> CostCentreStack      -- Optional cost centre annotation
140                 -> StgBinderInfo
141                 -> [Id]                 -- Free vars
142                 -> UpdateFlag
143                 -> [Id]                 -- Args
144                 -> StgExpr
145                 -> FCode (Id, CgIdInfo)
146
147 cgRhsClosure bndr cc bndr_info fvs upd_flag args body = do
148   {     -- LAY OUT THE OBJECT
149         -- If the binder is itself a free variable, then don't store
150         -- it in the closure.  Instead, just bind it to Node on entry.
151         -- NB we can be sure that Node will point to it, because we
152         -- havn't told mkClosureLFInfo about this; so if the binder
153         -- _was_ a free var of its RHS, mkClosureLFInfo thinks it *is*
154         -- stored in the closure itself, so it will make sure that
155         -- Node points to it...
156     let
157         name         = idName bndr
158         is_elem      = isIn "cgRhsClosure"
159         bndr_is_a_fv = bndr `is_elem` fvs
160         reduced_fvs | bndr_is_a_fv = fvs `minusList` [bndr]
161                     | otherwise    = fvs
162
163   ; lf_info <- mkClosureLFInfo bndr NotTopLevel fvs upd_flag args
164   ; fv_infos <- mapFCs getCgIdInfo reduced_fvs
165   ; srt_info <- getSRTInfo
166   ; mod_name <- getModuleName
167   ; let bind_details :: [(CgIdInfo, VirtualHpOffset)]
168         (tot_wds, ptr_wds, bind_details) 
169            = mkVirtHeapOffsets (isLFThunk lf_info) (map add_rep fv_infos)
170
171         add_rep info = (cgIdInfoArgRep info, info)
172
173         descr        = closureDescription mod_name name
174         closure_info = mkClosureInfo False      -- Not static
175                                      bndr lf_info tot_wds ptr_wds
176                                      srt_info descr
177
178         -- BUILD ITS INFO TABLE AND CODE
179   ; forkClosureBody (do
180         {       -- Bind the fvs
181           let 
182               -- A function closure pointer may be tagged, so we
183               -- must take it into account when accessing the free variables.
184               mbtag       = tagForArity (length args)
185               bind_fv (info, offset)
186                 | Just tag <- mbtag
187                 = bindNewToUntagNode (cgIdInfoId info) offset (cgIdInfoLF info) tag
188                 | otherwise
189                 = bindNewToNode (cgIdInfoId info) offset (cgIdInfoLF info)
190         ; mapCs bind_fv bind_details
191
192                 -- Bind the binder itself, if it is a free var
193         ; whenC bndr_is_a_fv (bindNewToReg bndr nodeReg lf_info)
194         
195                 -- Compile the body
196         ; closureCodeBody bndr_info closure_info cc args body })
197
198         -- BUILD THE OBJECT
199   ; let
200         to_amode (info, offset) = do { amode <- idInfoToAmode info
201                                      ; return (amode, offset) }
202   ; (use_cc, blame_cc) <- chooseDynCostCentres cc args body
203   ; amodes_w_offsets <- mapFCs to_amode bind_details
204   ; heap_offset <- allocDynClosure closure_info use_cc blame_cc amodes_w_offsets
205
206         -- RETURN
207   ; returnFC (bndr, heapIdInfo bndr heap_offset lf_info) }
208
209
210 mkClosureLFInfo :: Id           -- The binder
211                 -> TopLevelFlag -- True of top level
212                 -> [Id]         -- Free vars
213                 -> UpdateFlag   -- Update flag
214                 -> [Id]         -- Args
215                 -> FCode LambdaFormInfo
216 mkClosureLFInfo bndr top fvs upd_flag args
217   | null args = return (mkLFThunk (idType bndr) top fvs upd_flag)
218   | otherwise = do { arg_descr <- mkArgDescr (idName bndr) args
219                    ; return (mkLFReEntrant top fvs args arg_descr) }
220 \end{code}
221
222
223 %************************************************************************
224 %*                                                                      *
225 \subsection[code-for-closures]{The code for closures}
226 %*                                                                      *
227 %************************************************************************
228
229 \begin{code}
230 closureCodeBody :: StgBinderInfo
231                 -> ClosureInfo     -- Lots of information about this closure
232                 -> CostCentreStack -- Optional cost centre attached to closure
233                 -> [Id]
234                 -> StgExpr
235                 -> Code
236 \end{code}
237
238 There are two main cases for the code for closures.  If there are {\em
239 no arguments}, then the closure is a thunk, and not in normal form.
240 So it should set up an update frame (if it is shared).
241 NB: Thunks cannot have a primitive type!
242
243 \begin{code}
244 closureCodeBody _binder_info cl_info cc [{- No args i.e. thunk -}] body = do
245   { body_absC <- getCgStmts $ do
246         { tickyEnterThunk cl_info
247         ; ldvEnterClosure cl_info  -- NB: Node always points when profiling
248         ; thunkWrapper cl_info $ do
249                 -- We only enter cc after setting up update so
250                 -- that cc of enclosing scope will be recorded
251                 -- in update frame CAF/DICT functions will be
252                 -- subsumed by this enclosing cc
253             { enterCostCentre cl_info cc body
254             ; stmtsC [CmmComment $ mkFastString $ showSDoc $ ppr body]
255             ; cgExpr body }
256         }
257     
258   ; emitClosureCodeAndInfoTable cl_info [] body_absC }
259 \end{code}
260
261 If there is /at least one argument/, then this closure is in
262 normal form, so there is no need to set up an update frame.
263
264 The Macros for GrAnSim are produced at the beginning of the
265 argSatisfactionCheck (by calling fetchAndReschedule).  There info if
266 Node points to closure is available. -- HWL
267
268 \begin{code}
269 closureCodeBody _binder_info cl_info cc args body 
270   = ASSERT( length args > 0 )
271   do {  -- Get the current virtual Sp (it might not be zero, 
272         -- eg. if we're compiling a let-no-escape).
273     vSp <- getVirtSp
274   ; let (reg_args, other_args) = assignCallRegs (addIdReps args)
275         (sp_top, stk_args)     = mkVirtStkOffsets vSp other_args
276
277         -- Allocate the global ticky counter
278   ; let ticky_ctr_lbl = mkRednCountsLabel (closureName cl_info) (clHasCafRefs cl_info)
279   ; emitTickyCounter cl_info args sp_top
280
281         -- ...and establish the ticky-counter 
282         -- label for this block
283   ; setTickyCtrLabel ticky_ctr_lbl $ do
284
285         -- Emit the slow-entry code
286   { reg_save_code <- mkSlowEntryCode cl_info reg_args
287
288         -- Emit the main entry code
289   ; blks <- forkProc $
290             mkFunEntryCode cl_info cc reg_args stk_args
291                            sp_top reg_save_code body
292   ; emitClosureCodeAndInfoTable cl_info [] blks
293   }}
294
295
296
297 mkFunEntryCode :: ClosureInfo
298                -> CostCentreStack
299                -> [(Id,GlobalReg)]        -- Args in regs
300                -> [(Id,VirtualSpOffset)]  -- Args on stack
301                -> VirtualSpOffset         -- Last allocated word on stack
302                -> CmmStmts                -- Register-save code in case of GC
303                -> StgExpr
304                -> Code
305 -- The main entry code for the closure
306 mkFunEntryCode cl_info cc reg_args stk_args sp_top reg_save_code body = do
307   {     -- Bind args to regs/stack as appropriate,
308         -- and record expected position of sps
309   ; bindArgsToRegs  reg_args
310   ; bindArgsToStack stk_args
311   ; setRealAndVirtualSp sp_top
312
313         -- Enter the cost-centre, if required
314         -- ToDo: It's not clear why this is outside the funWrapper,
315         --       but the tickyEnterFun is inside. Perhaps we can put
316         --       them together?
317   ; enterCostCentre cl_info cc body
318
319         -- Do the business
320   ; funWrapper cl_info reg_args reg_save_code $ do
321         { tickyEnterFun cl_info
322         ; cgExpr body }
323   }
324 \end{code}
325
326 The "slow entry" code for a function.  This entry point takes its
327 arguments on the stack.  It loads the arguments into registers
328 according to the calling convention, and jumps to the function's
329 normal entry point.  The function's closure is assumed to be in
330 R1/node.
331
332 The slow entry point is used in two places:
333
334  (a) unknown calls: eg. stg_PAP_entry 
335  (b) returning from a heap-check failure
336
337 \begin{code}
338 mkSlowEntryCode :: ClosureInfo -> [(Id,GlobalReg)] -> FCode CmmStmts
339 -- If this function doesn't have a specialised ArgDescr, we need
340 -- to generate the function's arg bitmap, slow-entry code, and
341 -- register-save code for the heap-check failure
342 -- Here, we emit the slow-entry code, and 
343 -- return the register-save assignments
344 mkSlowEntryCode cl_info reg_args
345   | Just (_, ArgGen _) <- closureFunInfo cl_info
346   = do  { emitSimpleProc slow_lbl (emitStmts load_stmts)
347         ; return save_stmts }
348   | otherwise = return noStmts
349   where
350      name = closureName cl_info
351      has_caf_refs = clHasCafRefs cl_info
352      slow_lbl = mkSlowEntryLabel name has_caf_refs
353
354      load_stmts = mkStmts load_assts `plusStmts` mkStmts [stk_adj_pop, jump_to_entry]
355      save_stmts = oneStmt stk_adj_push `plusStmts`  mkStmts save_assts
356
357      reps_w_regs :: [(CgRep,GlobalReg)]
358      reps_w_regs = [(idCgRep id, reg) | (id,reg) <- reverse reg_args]
359      (final_stk_offset, stk_offsets)
360         = mapAccumL (\off (rep,_) -> (off + cgRepSizeW rep, off))
361                     0 reps_w_regs
362
363      load_assts = zipWithEqual "mk_load" mk_load reps_w_regs stk_offsets
364      mk_load (rep,reg) offset = CmmAssign (CmmGlobal reg) 
365                                           (CmmLoad (cmmRegOffW spReg offset)
366                                                    (argMachRep rep))
367
368      save_assts = zipWithEqual "mk_save" mk_save reps_w_regs stk_offsets
369      mk_save (rep,reg) offset = ASSERT( argMachRep rep `cmmEqType` globalRegType reg )
370                                 CmmStore (cmmRegOffW spReg offset) 
371                                          (CmmReg (CmmGlobal reg))
372
373      stk_adj_pop   = CmmAssign spReg (cmmRegOffW spReg final_stk_offset)
374      stk_adj_push  = CmmAssign spReg (cmmRegOffW spReg (- final_stk_offset))
375      jump_to_entry = CmmJump (mkLblExpr (enterLocalIdLabel name has_caf_refs)) []
376 \end{code}
377
378
379 %************************************************************************
380 %*                                                                      *
381 \subsubsection[closure-code-wrappers]{Wrappers around closure code}
382 %*                                                                      *
383 %************************************************************************
384
385 \begin{code}
386 thunkWrapper:: ClosureInfo -> Code -> Code
387 thunkWrapper closure_info thunk_code = do
388   { let node_points = nodeMustPointToIt (closureLFInfo closure_info)
389
390     -- HWL: insert macros for GrAnSim; 2 versions depending on liveness of node
391     -- (we prefer fetchAndReschedule-style context switches to yield ones)
392   ; if node_points 
393     then granFetchAndReschedule [] node_points 
394     else granYield              [] node_points
395
396         -- Stack and/or heap checks
397   ; thunkEntryChecks closure_info $ do
398         {       -- Overwrite with black hole if necessary
399           whenC (blackHoleOnEntry closure_info && node_points)
400                 (blackHoleIt closure_info)
401         ; setupUpdate closure_info thunk_code }
402                 -- setupUpdate *encloses* the thunk_code
403   }
404
405 funWrapper :: ClosureInfo       -- Closure whose code body this is
406            -> [(Id,GlobalReg)]  -- List of argument registers (if any)
407            -> CmmStmts          -- reg saves for the heap check failure
408            -> Code              -- Body of function being compiled
409            -> Code
410 funWrapper closure_info arg_regs reg_save_code fun_body = do
411   { let node_points = nodeMustPointToIt (closureLFInfo closure_info)
412
413   {-
414         -- Debugging: check that R1 has the correct tag
415   ; let tag = funTag closure_info
416   ; whenC (tag /= 0 && node_points) $ do
417         l <- newLabelC
418         stmtC (CmmCondBranch (CmmMachOp mo_wordEq [cmmGetTag (CmmReg nodeReg),
419                                                    CmmLit (mkIntCLit tag)]) l)
420         stmtC (CmmStore (CmmLit (mkWordCLit 0)) (CmmLit (mkWordCLit 0)))
421         labelC l
422   -}
423
424         -- Enter for Ldv profiling
425   ; whenC node_points (ldvEnterClosure closure_info)
426
427         -- GranSim yeild poin
428   ; granYield arg_regs node_points
429
430         -- Heap and/or stack checks wrap the function body
431   ; funEntryChecks closure_info reg_save_code 
432                    fun_body
433   }
434 \end{code}
435
436
437 %************************************************************************
438 %*                                                                      *
439 \subsubsubsection[update-and-BHs]{Update and black-hole wrappers}
440 %*                                                                      *
441 %************************************************************************
442
443
444 \begin{code}
445 blackHoleIt :: ClosureInfo -> Code
446 -- Only called for closures with no args
447 -- Node points to the closure
448 blackHoleIt closure_info = emitBlackHoleCode (closureSingleEntry closure_info)
449
450 emitBlackHoleCode :: Bool -> Code
451 emitBlackHoleCode is_single_entry = do
452
453   dflags <- getDynFlags
454
455         -- If we wanted to do eager blackholing with slop filling,
456         -- we'd need to do it at the *end* of a basic block, otherwise
457         -- we overwrite the free variables in the thunk that we still
458         -- need.  We have a patch for this from Andy Cheadle, but not
459         -- incorporated yet. --SDM [6/2004]
460         --
461         -- Profiling needs slop filling (to support LDV profiling), so
462         -- currently eager blackholing doesn't work with profiling.
463         --
464         -- Previously, eager blackholing was enabled when ticky-ticky
465         -- was on. But it didn't work, and it wasn't strictly necessary 
466         -- to bring back minimal ticky-ticky, so now EAGER_BLACKHOLING 
467         -- is unconditionally disabled. -- krc 1/2007
468
469   let eager_blackholing =  not opt_SccProfilingOn
470                         && dopt Opt_EagerBlackHoling dflags
471
472   if eager_blackholing
473      then do
474           tickyBlackHole (not is_single_entry)
475           let bh_info = CmmReg (CmmGlobal EagerBlackholeInfo)
476           stmtC (CmmStore (CmmReg nodeReg) bh_info)
477      else
478           nopC
479 \end{code}
480
481 \begin{code}
482 setupUpdate :: ClosureInfo -> Code -> Code      -- Only called for closures with no args
483         -- Nota Bene: this function does not change Node (even if it's a CAF),
484         -- so that the cost centre in the original closure can still be
485         -- extracted by a subsequent enterCostCentre
486 setupUpdate closure_info code
487   | closureReEntrant closure_info
488   = code
489
490   | not (isStaticClosure closure_info)
491   = if closureUpdReqd closure_info
492     then do { tickyPushUpdateFrame;  pushUpdateFrame (CmmReg nodeReg) code }
493     else do { tickyUpdateFrameOmitted; code }
494  
495   | otherwise   -- A static closure
496   = do  { tickyUpdateBhCaf closure_info
497
498         ; if closureUpdReqd closure_info
499           then do       -- Blackhole the (updatable) CAF:
500                 { upd_closure <- link_caf closure_info True
501                 ; pushUpdateFrame upd_closure code }
502           else do
503                 { -- krc: removed some ticky-related code here.
504                 ; tickyUpdateFrameOmitted
505                 ; code }
506     }
507
508
509 -----------------------------------------------------------------------------
510 -- Entering a CAF
511 --
512 -- When a CAF is first entered, it creates a black hole in the heap,
513 -- and updates itself with an indirection to this new black hole.
514 --
515 -- We update the CAF with an indirection to a newly-allocated black
516 -- hole in the heap.  We also set the blocking queue on the newly
517 -- allocated black hole to be empty.
518 --
519 -- Why do we make a black hole in the heap when we enter a CAF?
520 --    
521 --     - for a  generational garbage collector, which needs a fast
522 --       test for whether an updatee is in an old generation or not
523 --
524 --     - for the parallel system, which can implement updates more
525 --       easily if the updatee is always in the heap. (allegedly).
526 --
527 -- When debugging, we maintain a separate CAF list so we can tell when
528 -- a CAF has been garbage collected.
529
530 -- newCAF must be called before the itbl ptr is overwritten, since
531 -- newCAF records the old itbl ptr in order to do CAF reverting
532 -- (which Hugs needs to do in order that combined mode works right.)
533 --
534
535 -- ToDo [Feb 04]  This entire link_caf nonsense could all be moved
536 -- into the "newCAF" RTS procedure, which we call anyway, including
537 -- the allocation of the black-hole indirection closure.
538 -- That way, code size would fall, the CAF-handling code would 
539 -- be closer together, and the compiler wouldn't need to know
540 -- about off_indirectee etc.
541
542 link_caf :: ClosureInfo
543          -> Bool                -- True <=> updatable, False <=> single-entry
544          -> FCode CmmExpr       -- Returns amode for closure to be updated
545 -- To update a CAF we must allocate a black hole, link the CAF onto the
546 -- CAF list, then update the CAF to point to the fresh black hole.
547 -- This function returns the address of the black hole, so it can be
548 -- updated with the new value when available.  The reason for all of this
549 -- is that we only want to update dynamic heap objects, not static ones,
550 -- so that generational GC is easier.
551 link_caf cl_info _is_upd = do
552   {     -- Alloc black hole specifying CC_HDR(Node) as the cost centre
553   ; let use_cc   = costCentreFrom (CmmReg nodeReg)
554         blame_cc = use_cc
555   ; hp_offset <- allocDynClosure bh_cl_info use_cc blame_cc []
556   ; hp_rel    <- getHpRelOffset hp_offset
557
558         -- Call the RTS function newCAF to add the CAF to the CafList
559         -- so that the garbage collector can find them
560         -- This must be done *before* the info table pointer is overwritten, 
561         -- because the old info table ptr is needed for reversion
562   ; emitRtsCallWithVols (sLit "newCAF") [CmmHinted (CmmReg nodeReg) AddrHint] [node] False
563         -- node is live, so save it.
564
565         -- Overwrite the closure with a (static) indirection 
566         -- to the newly-allocated black hole
567   ; stmtsC [ CmmStore (cmmRegOffW nodeReg off_indirectee) hp_rel
568            , CmmStore (CmmReg nodeReg) ind_static_info ]
569
570   ; returnFC hp_rel }
571   where
572     bh_cl_info :: ClosureInfo
573     bh_cl_info = cafBlackHoleClosureInfo cl_info
574
575     ind_static_info :: CmmExpr
576     ind_static_info = mkLblExpr mkIndStaticInfoLabel
577
578     off_indirectee :: WordOff
579     off_indirectee = fixedHdrSize + oFFSET_StgInd_indirectee*wORD_SIZE
580 \end{code}
581
582
583 %************************************************************************
584 %*                                                                      *
585 \subsection[CgClosure-Description]{Profiling Closure Description.}
586 %*                                                                      *
587 %************************************************************************
588
589 For "global" data constructors the description is simply occurrence
590 name of the data constructor itself.  Otherwise it is determined by
591 @closureDescription@ from the let binding information.
592
593 \begin{code}
594 closureDescription :: Module            -- Module
595                    -> Name              -- Id of closure binding
596                    -> String
597         -- Not called for StgRhsCon which have global info tables built in
598         -- CgConTbls.lhs with a description generated from the data constructor
599 closureDescription mod_name name
600   = showSDocDump (char '<' <>
601                     (if isExternalName name
602                       then ppr name -- ppr will include the module name prefix
603                       else pprModule mod_name <> char '.' <> ppr name) <>
604                     char '>')
605    -- showSDocDump, because we want to see the unique on the Name.
606 \end{code}
607