2 -- | Converts continuations into full proceedures.
3 -- The main work of the CPS transform that everything else is setting-up.
5 Continuation(..), continuationLabel,
6 ContinuationFormat(..),
9 #include "HsVersions.h"
13 import CmmBrokenBlock -- Data types only
18 import CgProf (curCCS, curCCSAddr)
19 import CgUtils (cmmOffsetW)
30 import MachRegs (callerSaveVolatileRegs)
31 -- HACK: this is part of the NCG so we shouldn't use this, but we need
32 -- it for now to eliminate the need for saved regs to be in CmmCall.
33 -- The long term solution is to factor callerSaveVolatileRegs
34 -- from nativeGen into CPS
36 -- The format for the call to a continuation
37 -- The fst is the arguments that must be passed to the continuation
38 -- by the continuation's caller.
39 -- The snd is the live values that must be saved on stack.
40 -- A Nothing indicates an ignored slot.
41 -- The head of each list is the stack top or the first parameter.
43 -- The format for live values for a particular continuation
44 -- All on stack for now.
45 -- Head element is the top of the stack (or just under the header).
46 -- Nothing means an empty slot.
47 -- Future possibilities include callee save registers (i.e. passing slots in register)
48 -- and heap memory (not sure if that's usefull at all though, but it may
49 -- be worth exploring the design space).
51 continuationLabel (Continuation _ l _ _ _) = l
52 data Continuation info =
54 info -- Left <=> Continuation created by the CPS
55 -- Right <=> Function or Proc point
56 CLabel -- Used to generate both info & entry labels
57 CmmFormals -- Argument locals live on entry (C-- procedure params)
58 Bool -- ^ True <=> GC block so ignore stack size
59 [BrokenBlock] -- Code, may be empty. The first block is
60 -- the entry point. The order is otherwise initially
61 -- unimportant, but at some point the code gen will
64 -- the BlockId of the first block does not give rise
65 -- to a label. To jump to the first block in a Proc,
66 -- use the appropriate CLabel.
68 data ContinuationFormat
69 = ContinuationFormat {
70 continuation_formals :: CmmFormals,
71 continuation_label :: Maybe CLabel, -- The label occupying the top slot
72 continuation_frame_size :: WordOff, -- Total frame size in words (not including arguments)
73 continuation_stack :: [Maybe LocalReg] -- local reg offsets from stack top
76 -- A block can be a continuation of a call
77 -- A block can be a continuation of another block (w/ or w/o joins)
78 -- A block can be an entry to a function
80 -----------------------------------------------------------------------------
81 continuationToProc :: (WordOff, [(CLabel, ContinuationFormat)])
84 -> Continuation CmmInfo
86 continuationToProc (max_stack, formats) stack_use uniques
87 (Continuation info label formals _ blocks) =
88 CmmProc info label formals (concat $ zipWith3 continuationToProc' uniques blocks (True : repeat False))
90 curr_format = maybe unknown_block id $ lookup label formats
91 unknown_block = panic "unknown BlockId in continuationToProc"
92 curr_stack = continuation_frame_size curr_format
93 arg_stack = argumentsSize localRegRep formals
95 param_stmts :: [CmmStmt]
96 param_stmts = function_entry curr_format
101 CmmInfo _ (Just gc_block) _ _ ->
102 gc_stack_check' stack_use arg_stack (max_stack - curr_stack)
103 CmmInfo _ Nothing _ _ ->
104 panic "continuationToProc: missing GC block"
105 CmmNonInfo (Just gc_block) ->
106 gc_stack_check' stack_use arg_stack (max_stack - curr_stack)
107 CmmNonInfo Nothing ->
108 panic "continuationToProc: missing non-info GC block"
110 -- At present neither the Cmm parser nor the code generator
111 -- produce code that will allow the target of a CmmCondBranch
112 -- or a CmmSwitch to become a continuation or a proc-point.
113 -- If future revisions, might allow these to happen
114 -- then special care will have to be take to allow for that case.
115 continuationToProc' :: [Unique]
119 continuationToProc' uniques (BrokenBlock ident entry stmts _ exit) is_entry =
120 prefix_blocks ++ [main_block]
123 case gc_prefix ++ param_prefix of
125 entry_stmts -> [BasicBlock prefix_id
126 (entry_stmts ++ [CmmBranch ident])]
128 prefix_unique : call_uniques = uniques
129 toCLabel = mkReturnPtLabel . getUnique
131 block_for_branch unique next
132 | (Just cont_format) <- lookup (toCLabel next) formats
134 new_next = BlockId unique
135 cont_stack = continuation_frame_size cont_format
136 arguments = map formal_to_actual (continuation_formals cont_format)
138 [BasicBlock new_next $
139 pack_continuation False curr_format cont_format ++
140 tail_call (curr_stack - cont_stack)
141 (CmmLit $ CmmLabel $ toCLabel next)
146 block_for_branch' :: Unique -> Maybe BlockId -> (Maybe BlockId, [CmmBasicBlock])
147 block_for_branch' _ Nothing = (Nothing, [])
148 block_for_branch' unique (Just next) = (Just new_next, new_blocks)
149 where (new_next, new_blocks) = block_for_branch unique next
151 main_block = BasicBlock ident (stmts ++ postfix_stmts)
152 prefix_id = BlockId prefix_unique
153 gc_prefix = case entry of
154 FunctionEntry _ _ _ -> gc_stmts
156 ContinuationEntry _ _ _ -> []
157 param_prefix = if is_entry
160 postfix_stmts = case exit of
162 if (mkReturnPtLabel $ getUnique next) == label
163 then [CmmBranch next]
164 else case lookup (mkReturnPtLabel $ getUnique next) formats of
165 Nothing -> [CmmBranch next]
167 pack_continuation False curr_format cont_format ++
168 tail_call (curr_stack - cont_stack)
169 (CmmLit $ CmmLabel $ mkReturnPtLabel $ getUnique next)
172 cont_stack = continuation_frame_size cont_format
173 arguments = map formal_to_actual (continuation_formals cont_format)
174 FinalSwitch expr targets -> [CmmSwitch expr targets]
175 FinalReturn arguments ->
177 (CmmLoad (CmmReg spReg) wordRep)
179 FinalJump target arguments ->
180 tail_call curr_stack target arguments
182 -- A regular Cmm function call
183 FinalCall next (CmmForeignCall target CmmCallConv)
184 results arguments _ _ ->
185 pack_continuation True curr_format cont_format ++
186 tail_call (curr_stack - cont_stack)
189 cont_format = maybe unknown_block id $
190 lookup (mkReturnPtLabel $ getUnique next) formats
191 cont_stack = continuation_frame_size cont_format
193 -- A safe foreign call
194 FinalCall next (CmmForeignCall target conv)
195 results arguments _ _ ->
197 foreignCall call_uniques' (CmmForeignCall new_target conv)
200 (call_uniques', target_stmts, new_target) =
201 maybeAssignTemp call_uniques target
204 FinalCall next (CmmPrim target)
205 results arguments _ _ ->
206 foreignCall call_uniques (CmmPrim target)
209 formal_to_actual reg = (CmmReg (CmmLocal reg), NoHint)
211 foreignCall :: [Unique] -> CmmCallTarget -> CmmHintFormals -> CmmActuals -> [CmmStmt]
212 foreignCall uniques call results arguments =
216 [CmmCall (CmmForeignCall suspendThread CCallConv)
218 [ (CmmReg (CmmGlobal BaseReg), PtrHint) ]
220 CmmCall call results new_args CmmUnsafe,
221 CmmCall (CmmForeignCall resumeThread CCallConv)
222 [ (new_base, PtrHint) ]
223 [ (CmmReg (CmmLocal id), PtrHint) ]
225 -- Assign the result to BaseReg: we
226 -- might now have a different Capability!
227 CmmAssign (CmmGlobal BaseReg) (CmmReg (CmmLocal new_base))] ++
229 loadThreadState tso_unique ++
230 [CmmJump (CmmReg spReg) (map (formal_to_actual . fst) results)]
232 (_, arg_stmts, new_args) =
233 loadArgsIntoTemps argument_uniques arguments
234 (caller_save, caller_load) =
235 callerSaveVolatileRegs (Just [{-only system regs-}])
236 new_base = LocalReg base_unique (cmmRegRep (CmmGlobal BaseReg)) KindNonPtr
237 id = LocalReg id_unique wordRep KindNonPtr
238 tso_unique : base_unique : id_unique : argument_uniques = uniques
240 -- -----------------------------------------------------------------------------
241 -- Save/restore the thread state in the TSO
243 suspendThread = CmmLit (CmmLabel (mkRtsCodeLabel SLIT("suspendThread")))
244 resumeThread = CmmLit (CmmLabel (mkRtsCodeLabel SLIT("resumeThread")))
246 -- This stuff can't be done in suspendThread/resumeThread, because it
247 -- refers to global registers which aren't available in the C world.
250 -- CurrentTSO->sp = Sp;
251 [CmmStore (cmmOffset stgCurrentTSO tso_SP) stgSp,
253 -- and save the current cost centre stack in the TSO when profiling:
254 if opt_SccProfilingOn
255 then [CmmStore (cmmOffset stgCurrentTSO tso_CCCS) curCCS]
258 -- CurrentNursery->free = Hp+1;
259 closeNursery = CmmStore nursery_bdescr_free (cmmOffsetW stgHp 1)
261 loadThreadState tso_unique =
264 CmmAssign (CmmLocal tso) stgCurrentTSO,
266 CmmAssign sp (CmmLoad (cmmOffset (CmmReg (CmmLocal tso)) tso_SP)
268 -- SpLim = tso->stack + RESERVED_STACK_WORDS;
269 CmmAssign spLim (cmmOffsetW (cmmOffset (CmmReg (CmmLocal tso)) tso_STACK)
270 rESERVED_STACK_WORDS)
273 -- and load the current cost centre stack from the TSO when profiling:
274 if opt_SccProfilingOn
275 then [CmmStore curCCSAddr
276 (CmmLoad (cmmOffset (CmmReg (CmmLocal tso)) tso_CCCS) wordRep)]
278 where tso = LocalReg tso_unique wordRep KindNonPtr -- TODO FIXME NOW
282 -- Hp = CurrentNursery->free - 1;
283 CmmAssign hp (cmmOffsetW (CmmLoad nursery_bdescr_free wordRep) (-1)),
285 -- HpLim = CurrentNursery->start +
286 -- CurrentNursery->blocks*BLOCK_SIZE_W - 1;
289 (CmmLoad nursery_bdescr_start wordRep)
291 (CmmMachOp mo_wordMul [
292 CmmMachOp (MO_S_Conv I32 wordRep)
293 [CmmLoad nursery_bdescr_blocks I32],
294 CmmLit (mkIntCLit bLOCK_SIZE)
302 nursery_bdescr_free = cmmOffset stgCurrentNursery oFFSET_bdescr_free
303 nursery_bdescr_start = cmmOffset stgCurrentNursery oFFSET_bdescr_start
304 nursery_bdescr_blocks = cmmOffset stgCurrentNursery oFFSET_bdescr_blocks
306 tso_SP = tsoFieldB oFFSET_StgTSO_sp
307 tso_STACK = tsoFieldB oFFSET_StgTSO_stack
308 tso_CCCS = tsoProfFieldB oFFSET_StgTSO_CCCS
310 -- The TSO struct has a variable header, and an optional StgTSOProfInfo in
311 -- the middle. The fields we're interested in are after the StgTSOProfInfo.
312 tsoFieldB :: ByteOff -> ByteOff
314 | opt_SccProfilingOn = off + sIZEOF_StgTSOProfInfo + fixedHdrSize * wORD_SIZE
315 | otherwise = off + fixedHdrSize * wORD_SIZE
317 tsoProfFieldB :: ByteOff -> ByteOff
318 tsoProfFieldB off = off + fixedHdrSize * wORD_SIZE
322 stgCurrentTSO = CmmReg currentTSO
323 stgCurrentNursery = CmmReg currentNursery
326 spLim = CmmGlobal SpLim
328 hpLim = CmmGlobal HpLim
329 currentTSO = CmmGlobal CurrentTSO
330 currentNursery = CmmGlobal CurrentNursery
332 -----------------------------------------------------------------------------
333 -- Functions that generate CmmStmt sequences
334 -- for packing/unpacking continuations
335 -- and entering/exiting functions
337 tail_call :: WordOff -> CmmExpr -> CmmActuals -> [CmmStmt]
338 tail_call spRel target arguments
339 = store_arguments ++ adjust_spReg ++ jump where
341 [stack_put spRel expr offset
342 | ((expr, _), StackParam offset) <- argument_formats] ++
343 [global_put expr global
344 | ((expr, _), RegisterParam global) <- argument_formats]
348 else [CmmAssign spReg (CmmRegOff spReg (spRel*wORD_SIZE))]
349 jump = [CmmJump target arguments]
351 argument_formats = assignArguments (cmmExprRep . fst) arguments
353 gc_stack_check' stack_use arg_stack max_frame_size =
354 if max_frame_size > arg_stack
355 then [CmmAssign stack_use (CmmRegOff spReg (-max_frame_size*wORD_SIZE))]
356 else [CmmAssign stack_use (CmmReg spLimReg)]
357 -- Trick the optimizer into eliminating the branch for us
359 gc_stack_check :: BlockId -> WordOff -> [CmmStmt]
360 gc_stack_check gc_block max_frame_size
361 = check_stack_limit where
362 check_stack_limit = [
364 (CmmMachOp (MO_U_Lt $ cmmRegRep spReg)
365 [CmmRegOff spReg (-max_frame_size*wORD_SIZE),
370 -- TODO: fix branches to proc point
371 -- (we have to insert a new block to marshel the continuation)
374 pack_continuation :: Bool -- ^ Whether to set the top/header
375 -- of the stack. We only need to
376 -- set it if we are calling down
377 -- as opposed to continuation
379 -> ContinuationFormat -- ^ The current format
380 -> ContinuationFormat -- ^ The return point format
382 pack_continuation allow_header_set
383 (ContinuationFormat _ curr_id curr_frame_size _)
384 (ContinuationFormat _ cont_id cont_frame_size live_regs)
385 = store_live_values ++ set_stack_header where
386 -- TODO: only save variables when actually needed
387 -- (may be handled by latter pass)
389 [stack_put spRel (CmmReg (CmmLocal reg)) offset
390 | (reg, offset) <- cont_offsets]
392 if needs_header_set && allow_header_set
393 then [stack_put spRel continuation_function 0]
396 -- TODO: factor with function_entry and CmmInfo.hs(?)
397 cont_offsets = mkOffsets label_size live_regs
399 label_size = 1 :: WordOff
401 mkOffsets size [] = []
402 mkOffsets size (Nothing:regs) = mkOffsets (size+1) regs
403 mkOffsets size (Just reg:regs) = (reg, size):mkOffsets (size + width) regs
405 width = machRepByteWidth (localRegRep reg) `quot` wORD_SIZE
406 -- TODO: it would be better if we had a machRepWordWidth
408 spRel = curr_frame_size - cont_frame_size
409 continuation_function = CmmLit $ CmmLabel $ fromJust cont_id
411 case (curr_id, cont_id) of
412 (Just x, Just y) -> x /= y
415 -- Lazy adjustment of stack headers assumes all blocks
416 -- that could branch to eachother (i.e. control blocks)
417 -- have the same stack format (this causes a problem
418 -- only for proc-point).
419 function_entry :: ContinuationFormat -> [CmmStmt]
420 function_entry (ContinuationFormat formals _ _ live_regs)
421 = load_live_values ++ load_args where
422 -- TODO: only save variables when actually needed
423 -- (may be handled by latter pass)
425 [stack_get 0 reg offset
426 | (reg, offset) <- curr_offsets]
428 [stack_get 0 reg offset
429 | (reg, StackParam offset) <- argument_formats] ++
430 [global_get reg global
431 | (reg, RegisterParam global) <- argument_formats]
433 argument_formats = assignArguments (localRegRep) formals
435 -- TODO: eliminate copy/paste with pack_continuation
436 curr_offsets = mkOffsets label_size live_regs
438 label_size = 1 :: WordOff
440 mkOffsets size [] = []
441 mkOffsets size (Nothing:regs) = mkOffsets (size+1) regs
442 mkOffsets size (Just reg:regs) = (reg, size):mkOffsets (size + width) regs
444 width = machRepByteWidth (localRegRep reg) `quot` wORD_SIZE
445 -- TODO: it would be better if we had a machRepWordWidth
447 -----------------------------------------------------------------------------
448 -- Section: Stack and argument register puts and gets
449 -----------------------------------------------------------------------------
452 -- |Construct a 'CmmStmt' that will save a value on the stack
453 stack_put :: WordOff -- ^ Offset from the real 'Sp' that 'offset'
454 -- is relative to (added to offset)
455 -> CmmExpr -- ^ What to store onto the stack
456 -> WordOff -- ^ Where on the stack to store it
457 -- (positive <=> higher addresses)
459 stack_put spRel expr offset =
460 CmmStore (CmmRegOff spReg (wORD_SIZE*(spRel + offset))) expr
462 --------------------------------
468 stack_get spRel reg offset =
469 CmmAssign (CmmLocal reg)
470 (CmmLoad (CmmRegOff spReg (wORD_SIZE*(spRel + offset)))
472 global_put :: CmmExpr -> GlobalReg -> CmmStmt
473 global_put expr global = CmmAssign (CmmGlobal global) expr
474 global_get :: LocalReg -> GlobalReg -> CmmStmt
475 global_get reg global = CmmAssign (CmmLocal reg) (CmmReg (CmmGlobal global))