Reorganisation of the source tree
[ghc-hetmet.git] / compiler / codeGen / CgTailCall.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 % $Id: CgTailCall.lhs,v 1.43 2005/06/21 10:44:41 simonmar Exp $
5 %
6 %********************************************************
7 %*                                                      *
8 \section[CgTailCall]{Tail calls: converting @StgApps@}
9 %*                                                      *
10 %********************************************************
11
12 \begin{code}
13 module CgTailCall (
14         cgTailCall, performTailCall,
15         performReturn, performPrimReturn,
16         emitKnownConReturnCode, emitAlgReturnCode,
17         returnUnboxedTuple, ccallReturnUnboxedTuple,
18         pushUnboxedTuple,
19         tailCallPrimOp,
20
21         pushReturnAddress
22     ) where
23
24 #include "HsVersions.h"
25
26 import CgMonad
27 import CgBindery        ( getArgAmodes, getCgIdInfo, CgIdInfo, maybeLetNoEscape,
28                           idInfoToAmode, cgIdInfoId, cgIdInfoLF,
29                           cgIdInfoArgRep )
30 import CgInfoTbls       ( entryCode, emitDirectReturnInstr, dataConTagZ,
31                           emitVectoredReturnInstr, closureInfoPtr )
32 import CgCallConv
33 import CgStackery       ( setRealSp, mkStkAmodes, adjustStackHW,
34                           getSpRelOffset )
35 import CgHeapery        ( setRealHp, getHpRelOffset )
36 import CgUtils          ( emitSimultaneously )
37 import CgTicky
38 import ClosureInfo
39 import SMRep            ( CgRep, isVoidArg, separateByPtrFollowness )
40 import Cmm      
41 import CmmUtils
42 import CLabel           ( CLabel, mkRtsPrimOpLabel, mkSeqInfoLabel )
43 import Type             ( isUnLiftedType )
44 import Id               ( Id, idName, idUnique, idType )
45 import DataCon          ( DataCon, dataConTyCon )
46 import StgSyn           ( StgArg )
47 import TyCon            ( TyCon )
48 import PrimOp           ( PrimOp )
49 import Outputable
50
51 import Monad            ( when )
52
53 -----------------------------------------------------------------------------
54 -- Tail Calls
55
56 cgTailCall :: Id -> [StgArg] -> Code
57
58 -- Here's the code we generate for a tail call.  (NB there may be no
59 -- arguments, in which case this boils down to just entering a variable.)
60 -- 
61 --    * Put args in the top locations of the stack.
62 --    * Adjust the stack ptr
63 --    * Make R1 point to the function closure if necessary.
64 --    * Perform the call.
65 --
66 -- Things to be careful about:
67 --
68 --    * Don't overwrite stack locations before you have finished with
69 --      them (remember you need the function and the as-yet-unmoved
70 --      arguments).
71 --    * Preferably, generate no code to replace x by x on the stack (a
72 --      common situation in tail-recursion).
73 --    * Adjust the stack high water mark appropriately.
74 -- 
75 -- Treat unboxed locals exactly like literals (above) except use the addr
76 -- mode for the local instead of (CLit lit) in the assignment.
77
78 cgTailCall fun args
79   = do  { fun_info <- getCgIdInfo fun
80
81         ; if isUnLiftedType (idType fun)
82           then  -- Primitive return
83                 ASSERT( null args )
84             do  { fun_amode <- idInfoToAmode fun_info
85                 ; performPrimReturn (cgIdInfoArgRep fun_info) fun_amode } 
86
87           else -- Normal case, fun is boxed
88             do  { arg_amodes <- getArgAmodes args
89                 ; performTailCall fun_info arg_amodes noStmts }
90         }
91                 
92
93 -- -----------------------------------------------------------------------------
94 -- The guts of a tail-call
95
96 performTailCall 
97         :: CgIdInfo             -- The function
98         -> [(CgRep,CmmExpr)]    -- Args
99         -> CmmStmts             -- Pending simultaneous assignments
100                                 --  *** GUARANTEED to contain only stack assignments.
101         -> Code
102
103 performTailCall fun_info arg_amodes pending_assts
104   | Just join_sp <- maybeLetNoEscape fun_info
105   =        -- A let-no-escape is slightly different, because we
106            -- arrange the stack arguments into pointers and non-pointers
107            -- to make the heap check easier.  The tail-call sequence
108            -- is very similar to returning an unboxed tuple, so we
109            -- share some code.
110      do { (final_sp, arg_assts) <- pushUnboxedTuple join_sp arg_amodes
111         ; emitSimultaneously (pending_assts `plusStmts` arg_assts)
112         ; let lbl = enterReturnPtLabel (idUnique (cgIdInfoId fun_info))
113         ; doFinalJump final_sp True {- Is LNE -} (jumpToLbl lbl) }
114
115   | otherwise
116   = do  { fun_amode <- idInfoToAmode fun_info
117         ; let node_asst = oneStmt (CmmAssign nodeReg fun_amode)
118               opt_node_asst | nodeMustPointToIt lf_info = node_asst
119                             | otherwise                 = noStmts
120         ; EndOfBlockInfo sp _ <- getEndOfBlockInfo
121         ; hmods <- getHomeModules
122
123         ; case (getCallMethod hmods fun_name lf_info (length arg_amodes)) of
124
125             -- Node must always point to things we enter
126             EnterIt -> do
127                 { emitSimultaneously (node_asst `plusStmts` pending_assts) 
128                 ; let target = entryCode (closureInfoPtr (CmmReg nodeReg))
129                 ; doFinalJump sp False (stmtC (CmmJump target [])) }
130     
131             -- A function, but we have zero arguments.  It is already in WHNF,
132             -- so we can just return it.  
133             -- As with any return, Node must point to it.
134             ReturnIt -> do
135                 { emitSimultaneously (node_asst `plusStmts` pending_assts)
136                 ; doFinalJump sp False emitDirectReturnInstr }
137     
138             -- A real constructor.  Don't bother entering it, 
139             -- just do the right sort of return instead.
140             -- As with any return, Node must point to it.
141             ReturnCon con -> do
142                 { emitSimultaneously (node_asst `plusStmts` pending_assts)
143                 ; doFinalJump sp False (emitKnownConReturnCode con) }
144
145             JumpToIt lbl -> do
146                 { emitSimultaneously (opt_node_asst `plusStmts` pending_assts)
147                 ; doFinalJump sp False (jumpToLbl lbl) }
148     
149             -- A slow function call via the RTS apply routines
150             -- Node must definitely point to the thing
151             SlowCall -> do 
152                 {  when (not (null arg_amodes)) $ do
153                    { if (isKnownFun lf_info) 
154                         then tickyKnownCallTooFewArgs
155                         else tickyUnknownCall
156                    ; tickySlowCallPat (map fst arg_amodes) 
157                    }
158
159                 ; let (apply_lbl, args, extra_args) 
160                         = constructSlowCall arg_amodes
161
162                 ; directCall sp apply_lbl args extra_args 
163                         (node_asst `plusStmts` pending_assts)
164                 }
165     
166             -- A direct function call (possibly with some left-over arguments)
167             DirectEntry lbl arity -> do
168                 { if arity == length arg_amodes
169                         then tickyKnownCallExact
170                         else do tickyKnownCallExtraArgs
171                                 tickySlowCallPat (map fst (drop arity arg_amodes))
172
173                 ; let
174                      -- The args beyond the arity go straight on the stack
175                      (arity_args, extra_args) = splitAt arity arg_amodes
176      
177                 ; directCall sp lbl arity_args extra_args
178                         (opt_node_asst `plusStmts` pending_assts)
179                 }
180         }
181   where
182     fun_name  = idName (cgIdInfoId fun_info)
183     lf_info   = cgIdInfoLF fun_info
184
185
186
187 directCall sp lbl args extra_args assts = do
188   let
189         -- First chunk of args go in registers
190         (reg_arg_amodes, stk_args) = assignCallRegs args
191      
192         -- Any "extra" arguments are placed in frames on the
193         -- stack after the other arguments.
194         slow_stk_args = slowArgs extra_args
195
196         reg_assts = assignToRegs reg_arg_amodes
197   --
198   (final_sp, stk_assts) <- mkStkAmodes sp (stk_args ++ slow_stk_args)
199
200   emitSimultaneously (reg_assts     `plusStmts`
201                       stk_assts     `plusStmts`
202                       assts)
203
204   doFinalJump final_sp False (jumpToLbl lbl)
205
206 -- -----------------------------------------------------------------------------
207 -- The final clean-up before we do a jump at the end of a basic block.
208 -- This code is shared by tail-calls and returns.
209
210 doFinalJump :: VirtualSpOffset -> Bool -> Code -> Code 
211 doFinalJump final_sp is_let_no_escape jump_code
212   = do  { -- Adjust the high-water mark if necessary
213           adjustStackHW final_sp
214
215         -- Push a return address if necessary (after the assignments
216         -- above, in case we clobber a live stack location)
217         --
218         -- DONT push the return address when we're about to jump to a
219         -- let-no-escape: the final tail call in the let-no-escape
220         -- will do this.
221         ; eob <- getEndOfBlockInfo
222         ; whenC (not is_let_no_escape) (pushReturnAddress eob)
223
224             -- Final adjustment of Sp/Hp
225         ; adjustSpAndHp final_sp
226
227             -- and do the jump
228         ; jump_code }
229
230 -- -----------------------------------------------------------------------------
231 -- A general return (just a special case of doFinalJump, above)
232
233 performReturn :: Code           -- The code to execute to actually do the return
234               -> Code
235
236 performReturn finish_code
237   = do  { EndOfBlockInfo args_sp sequel <- getEndOfBlockInfo
238         ; doFinalJump args_sp False{-not a LNE-} finish_code }
239
240 -- -----------------------------------------------------------------------------
241 -- Primitive Returns
242 -- Just load the return value into the right register, and return.
243
244 performPrimReturn :: CgRep -> CmmExpr   -- The thing to return
245                   -> Code
246 performPrimReturn rep amode
247   =  do { whenC (not (isVoidArg rep))
248                 (stmtC (CmmAssign ret_reg amode))
249         ; performReturn emitDirectReturnInstr }
250   where
251     ret_reg = dataReturnConvPrim rep
252
253 -- -----------------------------------------------------------------------------
254 -- Algebraic constructor returns
255
256 -- Constructor is built on the heap; Node is set.
257 -- All that remains is to do the right sort of jump.
258
259 emitKnownConReturnCode :: DataCon -> Code
260 emitKnownConReturnCode con
261   = emitAlgReturnCode (dataConTyCon con)
262                       (CmmLit (mkIntCLit (dataConTagZ con)))
263                         -- emitAlgReturnCode requires zero-indexed tag
264
265 emitAlgReturnCode :: TyCon -> CmmExpr -> Code
266 -- emitAlgReturnCode is used both by emitKnownConReturnCode,
267 -- and by by PrimOps that return enumerated types (i.e.
268 -- all the comparison operators).
269 emitAlgReturnCode tycon tag
270  =  do  { case ctrlReturnConvAlg tycon of
271             VectoredReturn fam_sz -> do { tickyVectoredReturn fam_sz
272                                         ; emitVectoredReturnInstr tag }
273             UnvectoredReturn _    -> emitDirectReturnInstr 
274         }
275
276
277 -- ---------------------------------------------------------------------------
278 -- Unboxed tuple returns
279
280 -- These are a bit like a normal tail call, except that:
281 --
282 --   - The tail-call target is an info table on the stack
283 --
284 --   - We separate stack arguments into pointers and non-pointers,
285 --     to make it easier to leave things in a sane state for a heap check.
286 --     This is OK because we can never partially-apply an unboxed tuple,
287 --     unlike a function.  The same technique is used when calling
288 --     let-no-escape functions, because they also can't be partially
289 --     applied.
290
291 returnUnboxedTuple :: [(CgRep, CmmExpr)] -> Code
292 returnUnboxedTuple amodes
293   = do  { eob@(EndOfBlockInfo args_sp sequel) <- getEndOfBlockInfo
294         ; tickyUnboxedTupleReturn (length amodes)
295         ; (final_sp, assts) <- pushUnboxedTuple args_sp amodes
296         ; emitSimultaneously assts
297         ; doFinalJump final_sp False{-not a LNE-} emitDirectReturnInstr }
298
299 pushUnboxedTuple :: VirtualSpOffset             -- Sp at which to start pushing
300                  -> [(CgRep, CmmExpr)]          -- amodes of the components
301                  -> FCode (VirtualSpOffset,     -- final Sp
302                            CmmStmts)            -- assignments (regs+stack)
303
304 pushUnboxedTuple sp [] 
305   = return (sp, noStmts)
306 pushUnboxedTuple sp amodes
307   = do  { let   (reg_arg_amodes, stk_arg_amodes) = assignReturnRegs amodes
308         
309                 -- separate the rest of the args into pointers and non-pointers
310                 (ptr_args, nptr_args) = separateByPtrFollowness stk_arg_amodes
311                 reg_arg_assts = assignToRegs reg_arg_amodes
312                 
313             -- push ptrs, then nonptrs, on the stack
314         ; (ptr_sp,   ptr_assts)  <- mkStkAmodes sp ptr_args
315         ; (final_sp, nptr_assts) <- mkStkAmodes ptr_sp nptr_args
316
317         ; returnFC (final_sp,
318                     reg_arg_assts `plusStmts` 
319                     ptr_assts `plusStmts` nptr_assts) }
320     
321                   
322 -- -----------------------------------------------------------------------------
323 -- Returning unboxed tuples.  This is mainly to support _ccall_GC_, where
324 -- we want to do things in a slightly different order to normal:
325 -- 
326 --              - push return address
327 --              - adjust stack pointer
328 --              - r = call(args...)
329 --              - assign regs for unboxed tuple (usually just R1 = r)
330 --              - return to continuation
331 -- 
332 -- The return address (i.e. stack frame) must be on the stack before
333 -- doing the call in case the call ends up in the garbage collector.
334 -- 
335 -- Sadly, the information about the continuation is lost after we push it
336 -- (in order to avoid pushing it again), so we end up doing a needless
337 -- indirect jump (ToDo).
338
339 ccallReturnUnboxedTuple :: [(CgRep, CmmExpr)] -> Code -> Code
340 ccallReturnUnboxedTuple amodes before_jump
341   = do  { eob@(EndOfBlockInfo args_sp _) <- getEndOfBlockInfo
342
343         -- Push a return address if necessary
344         ; pushReturnAddress eob
345         ; setEndOfBlockInfo (EndOfBlockInfo args_sp OnStack)
346             (do { adjustSpAndHp args_sp
347                 ; before_jump
348                 ; returnUnboxedTuple amodes })
349     }
350
351 -- -----------------------------------------------------------------------------
352 -- Calling an out-of-line primop
353
354 tailCallPrimOp :: PrimOp -> [StgArg] -> Code
355 tailCallPrimOp op args
356  = do   {       -- We're going to perform a normal-looking tail call, 
357                 -- except that *all* the arguments will be in registers.
358                 -- Hence the ASSERT( null leftovers )
359           arg_amodes <- getArgAmodes args
360         ; let (arg_regs, leftovers) = assignPrimOpCallRegs arg_amodes
361               jump_to_primop = jumpToLbl (mkRtsPrimOpLabel op)
362
363         ; ASSERT(null leftovers) -- no stack-resident args
364           emitSimultaneously (assignToRegs arg_regs)
365
366         ; EndOfBlockInfo args_sp _ <- getEndOfBlockInfo
367         ; doFinalJump args_sp False{-not a LNE-} jump_to_primop }
368
369 -- -----------------------------------------------------------------------------
370 -- Return Addresses
371
372 -- We always push the return address just before performing a tail call
373 -- or return.  The reason we leave it until then is because the stack
374 -- slot that the return address is to go into might contain something
375 -- useful.
376 -- 
377 -- If the end of block info is 'CaseAlts', then we're in the scrutinee of a
378 -- case expression and the return address is still to be pushed.
379 -- 
380 -- There are cases where it doesn't look necessary to push the return
381 -- address: for example, just before doing a return to a known
382 -- continuation.  However, the continuation will expect to find the
383 -- return address on the stack in case it needs to do a heap check.
384
385 pushReturnAddress :: EndOfBlockInfo -> Code
386
387 pushReturnAddress (EndOfBlockInfo args_sp sequel@(CaseAlts lbl _ _ False))
388   = do  { sp_rel <- getSpRelOffset args_sp
389         ; stmtC (CmmStore sp_rel (mkLblExpr lbl)) }
390
391 -- For a polymorphic case, we have two return addresses to push: the case
392 -- return, and stg_seq_frame_info which turns a possible vectored return
393 -- into a direct one.
394 pushReturnAddress (EndOfBlockInfo args_sp sequel@(CaseAlts lbl _ _ True))
395   = do  { sp_rel <- getSpRelOffset (args_sp-1)
396         ; stmtC (CmmStore sp_rel (mkLblExpr lbl))
397         ; sp_rel <- getSpRelOffset args_sp
398         ; stmtC (CmmStore sp_rel (CmmLit (CmmLabel mkSeqInfoLabel))) }
399
400 pushReturnAddress _ = nopC
401
402 -- -----------------------------------------------------------------------------
403 -- Misc.
404
405 jumpToLbl :: CLabel -> Code
406 -- Passes no argument to the destination procedure
407 jumpToLbl lbl = stmtC (CmmJump (CmmLit (CmmLabel lbl)) [{- No args -}])
408
409 assignToRegs :: [(CmmExpr, GlobalReg)] -> CmmStmts
410 assignToRegs reg_args 
411   = mkStmts [ CmmAssign (CmmGlobal reg_id) expr
412             | (expr, reg_id) <- reg_args ] 
413 \end{code}
414
415
416 %************************************************************************
417 %*                                                                      *
418 \subsection[CgStackery-adjust]{Adjusting the stack pointers}
419 %*                                                                      *
420 %************************************************************************
421
422 This function adjusts the stack and heap pointers just before a tail
423 call or return.  The stack pointer is adjusted to its final position
424 (i.e. to point to the last argument for a tail call, or the activation
425 record for a return).  The heap pointer may be moved backwards, in
426 cases where we overallocated at the beginning of the basic block (see
427 CgCase.lhs for discussion).
428
429 These functions {\em do not} deal with high-water-mark adjustment.
430 That's done by functions which allocate stack space.
431
432 \begin{code}
433 adjustSpAndHp :: VirtualSpOffset        -- New offset for Arg stack ptr
434               -> Code
435 adjustSpAndHp newRealSp 
436   = do  { -- Adjust stack, if necessary.
437           -- NB: the conditional on the monad-carried realSp
438           --     is out of line (via codeOnly), to avoid a black hole
439         ; new_sp <- getSpRelOffset newRealSp
440         ; checkedAbsC (CmmAssign spReg new_sp)  -- Will generate no code in the case
441         ; setRealSp newRealSp                   -- where realSp==newRealSp
442
443           -- Adjust heap.  The virtual heap pointer may be less than the real Hp
444           -- because the latter was advanced to deal with the worst-case branch
445           -- of the code, and we may be in a better-case branch.  In that case,
446           -- move the real Hp *back* and retract some ticky allocation count.
447         ; hp_usg <- getHpUsage
448         ; let rHp = realHp hp_usg
449               vHp = virtHp hp_usg
450         ; new_hp <- getHpRelOffset vHp
451         ; checkedAbsC (CmmAssign hpReg new_hp)  -- Generates nothing when vHp==rHp
452         ; tickyAllocHeap (vHp - rHp)            -- ...ditto
453         ; setRealHp vHp
454         }
455 \end{code}