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(..),
12 import CmmBrokenBlock -- Data types only
32 -- The format for the call to a continuation
33 -- The fst is the arguments that must be passed to the continuation
34 -- by the continuation's caller.
35 -- The snd is the live values that must be saved on stack.
36 -- A Nothing indicates an ignored slot.
37 -- The head of each list is the stack top or the first parameter.
39 -- The format for live values for a particular continuation
40 -- All on stack for now.
41 -- Head element is the top of the stack (or just under the header).
42 -- Nothing means an empty slot.
43 -- Future possibilities include callee save registers (i.e. passing slots in register)
44 -- and heap memory (not sure if that's usefull at all though, but it may
45 -- be worth exploring the design space).
47 continuationLabel :: Continuation (Either C_SRT CmmInfo) -> CLabel
48 continuationLabel (Continuation _ l _ _ _) = l
49 data Continuation info =
51 info -- Left <=> Continuation created by the CPS
52 -- Right <=> Function or Proc point
53 CLabel -- Used to generate both info & entry labels
54 CmmFormals -- Argument locals live on entry (C-- procedure params)
55 Bool -- True <=> GC block so ignore stack size
56 [BrokenBlock] -- Code, may be empty. The first block is
57 -- the entry point. The order is otherwise initially
58 -- unimportant, but at some point the code gen will
61 -- the BlockId of the first block does not give rise
62 -- to a label. To jump to the first block in a Proc,
63 -- use the appropriate CLabel.
65 data ContinuationFormat
66 = ContinuationFormat {
67 continuation_formals :: CmmFormals,
68 continuation_label :: Maybe CLabel, -- The label occupying the top slot
69 continuation_frame_size :: WordOff, -- Total frame size in words (not including arguments)
70 continuation_stack :: [Maybe LocalReg] -- local reg offsets from stack top
73 -- A block can be a continuation of a call
74 -- A block can be a continuation of another block (w/ or w/o joins)
75 -- A block can be an entry to a function
77 -----------------------------------------------------------------------------
78 continuationToProc :: (WordOff, WordOff, [(CLabel, ContinuationFormat)])
81 -> Continuation CmmInfo
83 continuationToProc (max_stack, update_frame_size, formats) stack_use uniques
84 (Continuation info label formals _ blocks) =
85 CmmProc info label formals (ListGraph blocks')
87 blocks' = concat $ zipWith3 continuationToProc' uniques blocks
89 curr_format = maybe unknown_block id $ lookup label formats
90 unknown_block = panic "unknown BlockId in continuationToProc"
91 curr_stack = continuation_frame_size curr_format
92 arg_stack = argumentsSize localRegType formals
94 param_stmts :: [CmmStmt]
95 param_stmts = function_entry curr_format
99 assign_gc_stack_use stack_use arg_stack (max_stack - curr_stack)
101 update_stmts :: [CmmStmt]
104 CmmInfo _ (Just (UpdateFrame target args)) _ ->
105 pack_frame curr_stack update_frame_size (Just target) (map Just args) ++
106 adjust_sp_reg (curr_stack - update_frame_size)
107 CmmInfo _ Nothing _ -> []
109 continuationToProc' :: [[Unique]]
113 continuationToProc' uniques (BrokenBlock ident entry stmts _ exit) is_entry =
114 prefix_blocks ++ [BasicBlock ident fixed_main_stmts] ++ concat new_blocks
119 (BlockId prefix_unique)
120 (param_stmts ++ [CmmBranch ident])]
123 (prefix_unique : call_uniques) : new_block_uniques = uniques
124 toCLabel = mkReturnPtLabel . getUnique
126 block_for_branch :: Unique -> BlockId -> (BlockId, [CmmBasicBlock])
127 block_for_branch unique next
128 -- branches to the current function don't have to jump
129 | (mkReturnPtLabel $ getUnique next) == label
132 -- branches to any other function have to jump
133 | (Just cont_format) <- lookup (toCLabel next) formats
135 new_next = BlockId unique
136 cont_stack = continuation_frame_size cont_format
137 arguments = map formal_to_actual (continuation_formals cont_format)
139 [BasicBlock new_next $
140 pack_continuation curr_format cont_format ++
141 tail_call (curr_stack - cont_stack)
142 (CmmLit $ CmmLabel $ toCLabel next)
145 -- branches to blocks in the current function don't have to jump
149 -- Wrapper for block_for_branch for when the target
150 -- is inside a 'Maybe'.
151 block_for_branch' :: Unique -> Maybe BlockId -> (Maybe BlockId, [CmmBasicBlock])
152 block_for_branch' _ Nothing = (Nothing, [])
153 block_for_branch' unique (Just next) = (Just new_next, new_blocks)
154 where (new_next, new_blocks) = block_for_branch unique next
156 -- If the target of a switch, branch or cond branch becomes a proc point
157 -- then we have to make a new block what will then *jump* to the original target.
158 proc_point_fix unique (CmmCondBranch test target)
159 = (CmmCondBranch test new_target, new_blocks)
160 where (new_target, new_blocks) = block_for_branch (head unique) target
161 proc_point_fix unique (CmmSwitch test targets)
162 = (CmmSwitch test new_targets, concat new_blocks)
163 where (new_targets, new_blocks) =
164 unzip $ zipWith block_for_branch' unique targets
165 proc_point_fix unique (CmmBranch target)
166 = (CmmBranch new_target, new_blocks)
167 where (new_target, new_blocks) = block_for_branch (head unique) target
168 proc_point_fix _ other = (other, [])
170 (fixed_main_stmts, new_blocks) = unzip $ zipWith proc_point_fix new_block_uniques main_stmts
173 FunctionEntry _ _ _ ->
174 -- The statements for an update frame must come /after/
175 -- the GC check that was added at the beginning of the
176 -- CPS pass. So we have do edit the statements a bit.
177 -- This depends on the knowledge that the statements in
178 -- the first block are only the GC check. That's
179 -- fragile but it works for now.
180 gc_stmts ++ stmts ++ update_stmts ++ postfix_stmts
181 ControlEntry -> stmts ++ postfix_stmts
182 ContinuationEntry _ _ _ -> stmts ++ postfix_stmts
183 postfix_stmts = case exit of
184 -- Branches and switches may get modified by proc_point_fix
185 FinalBranch next -> [CmmBranch next]
186 FinalSwitch expr targets -> [CmmSwitch expr targets]
188 -- A return is a tail call to the stack top
189 FinalReturn arguments ->
191 (entryCode (CmmLoad (CmmReg spReg) bWord))
195 FinalJump target arguments ->
196 tail_call curr_stack target arguments
198 -- A regular Cmm function call
199 FinalCall next (CmmCallee target CmmCallConv)
201 pack_continuation curr_format cont_format ++
202 tail_call (curr_stack - cont_stack)
205 cont_format = maybe unknown_block id $
206 lookup (mkReturnPtLabel $ getUnique next) formats
207 cont_stack = continuation_frame_size cont_format
209 -- A safe foreign call
210 FinalCall _ (CmmCallee target conv)
211 results arguments _ _ _ ->
213 foreignCall call_uniques' (CmmCallee new_target conv)
216 (call_uniques', target_stmts, new_target) =
217 maybeAssignTemp call_uniques target
220 FinalCall _ (CmmPrim target)
221 results arguments _ _ _ ->
222 foreignCall call_uniques (CmmPrim target)
225 formal_to_actual :: LocalReg -> CmmHinted CmmExpr
226 formal_to_actual reg = CmmHinted (CmmReg (CmmLocal reg)) NoHint
228 foreignCall :: [Unique] -> CmmCallTarget -> HintedCmmFormals -> HintedCmmActuals -> [CmmStmt]
229 foreignCall uniques call results arguments =
233 [CmmCall (CmmCallee suspendThread CCallConv)
234 [ CmmHinted id AddrHint ]
235 [ CmmHinted (CmmReg (CmmGlobal BaseReg)) AddrHint ]
238 CmmCall call results new_args CmmUnsafe CmmMayReturn,
239 CmmCall (CmmCallee resumeThread CCallConv)
240 [ CmmHinted new_base AddrHint ]
241 [ CmmHinted (CmmReg (CmmLocal id)) AddrHint ]
244 -- Assign the result to BaseReg: we
245 -- might now have a different Capability!
246 CmmAssign (CmmGlobal BaseReg) (CmmReg (CmmLocal new_base))] ++
248 loadThreadState tso_unique ++
249 [CmmJump (CmmReg spReg) (map (formal_to_actual . hintlessCmm) results)]
251 (_, arg_stmts, new_args) =
252 loadArgsIntoTemps argument_uniques arguments
253 (caller_save, caller_load) =
254 callerSaveVolatileRegs (Just [{-only system regs-}])
255 new_base = LocalReg base_unique (cmmRegType (CmmGlobal BaseReg))
256 id = LocalReg id_unique bWord
257 tso_unique : base_unique : id_unique : argument_uniques = uniques
259 -- -----------------------------------------------------------------------------
260 -- Save/restore the thread state in the TSO
262 suspendThread, resumeThread :: CmmExpr
263 suspendThread = CmmLit (CmmLabel (mkRtsCodeLabel (sLit "suspendThread")))
264 resumeThread = CmmLit (CmmLabel (mkRtsCodeLabel (sLit "resumeThread")))
266 -- This stuff can't be done in suspendThread/resumeThread, because it
267 -- refers to global registers which aren't available in the C world.
269 saveThreadState :: [CmmStmt]
271 -- CurrentTSO->sp = Sp;
272 [CmmStore (cmmOffset stgCurrentTSO tso_SP) stgSp,
274 -- and save the current cost centre stack in the TSO when profiling:
275 if opt_SccProfilingOn
276 then [CmmStore (cmmOffset stgCurrentTSO tso_CCCS) curCCS]
279 -- CurrentNursery->free = Hp+1;
280 closeNursery :: CmmStmt
281 closeNursery = CmmStore nursery_bdescr_free (cmmOffsetW stgHp 1)
283 loadThreadState :: Unique -> [CmmStmt]
284 loadThreadState tso_unique =
287 CmmAssign (CmmLocal tso) stgCurrentTSO,
289 CmmAssign sp (CmmLoad (cmmOffset (CmmReg (CmmLocal tso)) tso_SP)
291 -- SpLim = tso->stack + RESERVED_STACK_WORDS;
292 CmmAssign spLim (cmmOffsetW (cmmOffset (CmmReg (CmmLocal tso)) tso_STACK)
293 rESERVED_STACK_WORDS)
296 -- and load the current cost centre stack from the TSO when profiling:
297 if opt_SccProfilingOn
298 then [CmmStore curCCSAddr
299 (CmmLoad (cmmOffset (CmmReg (CmmLocal tso)) tso_CCCS) bWord)]
301 where tso = LocalReg tso_unique bWord -- TODO FIXME NOW
304 openNursery :: [CmmStmt]
306 -- Hp = CurrentNursery->free - 1;
307 CmmAssign hp (cmmOffsetW (CmmLoad nursery_bdescr_free bWord) (-1)),
309 -- HpLim = CurrentNursery->start +
310 -- CurrentNursery->blocks*BLOCK_SIZE_W - 1;
313 (CmmLoad nursery_bdescr_start bWord)
315 (CmmMachOp mo_wordMul [
316 CmmMachOp (MO_SS_Conv W32 wordWidth)
317 [CmmLoad nursery_bdescr_blocks b32],
318 CmmLit (mkIntCLit bLOCK_SIZE)
326 nursery_bdescr_free, nursery_bdescr_start, nursery_bdescr_blocks :: CmmExpr
327 nursery_bdescr_free = cmmOffset stgCurrentNursery oFFSET_bdescr_free
328 nursery_bdescr_start = cmmOffset stgCurrentNursery oFFSET_bdescr_start
329 nursery_bdescr_blocks = cmmOffset stgCurrentNursery oFFSET_bdescr_blocks
331 tso_SP, tso_STACK, tso_CCCS :: ByteOff
332 tso_SP = tsoFieldB oFFSET_StgTSO_sp
333 tso_STACK = tsoFieldB oFFSET_StgTSO_stack
334 tso_CCCS = tsoProfFieldB oFFSET_StgTSO_CCCS
336 -- The TSO struct has a variable header, and an optional StgTSOProfInfo in
337 -- the middle. The fields we're interested in are after the StgTSOProfInfo.
338 tsoFieldB :: ByteOff -> ByteOff
340 | opt_SccProfilingOn = off + sIZEOF_StgTSOProfInfo + fixedHdrSize * wORD_SIZE
341 | otherwise = off + fixedHdrSize * wORD_SIZE
343 tsoProfFieldB :: ByteOff -> ByteOff
344 tsoProfFieldB off = off + fixedHdrSize * wORD_SIZE
346 stgSp, stgHp, stgCurrentTSO, stgCurrentNursery :: CmmExpr
349 stgCurrentTSO = CmmReg currentTSO
350 stgCurrentNursery = CmmReg currentNursery
352 sp, spLim, hp, hpLim, currentTSO, currentNursery :: CmmReg
354 spLim = CmmGlobal SpLim
356 hpLim = CmmGlobal HpLim
357 currentTSO = CmmGlobal CurrentTSO
358 currentNursery = CmmGlobal CurrentNursery
360 -----------------------------------------------------------------------------
361 -- Functions that generate CmmStmt sequences
362 -- for packing/unpacking continuations
363 -- and entering/exiting functions
365 tail_call :: WordOff -> CmmExpr -> HintedCmmActuals -> [CmmStmt]
366 tail_call spRel target arguments
367 = store_arguments ++ adjust_sp_reg spRel ++ jump where
369 [stack_put spRel expr offset
370 | ((CmmHinted expr _), StackParam offset) <- argument_formats] ++
371 [global_put expr global
372 | ((CmmHinted expr _), RegisterParam global) <- argument_formats]
373 jump = [CmmJump target arguments]
375 argument_formats = assignArguments (cmmExprType . hintlessCmm) arguments
377 adjust_sp_reg :: Int -> [CmmStmt]
378 adjust_sp_reg spRel =
381 else [CmmAssign spReg (CmmRegOff spReg (spRel*wORD_SIZE))]
383 assign_gc_stack_use :: CmmReg -> Int -> Int -> [CmmStmt]
384 assign_gc_stack_use stack_use arg_stack max_frame_size =
385 if max_frame_size > arg_stack
386 then [CmmAssign stack_use (CmmRegOff spReg (-max_frame_size*wORD_SIZE))]
387 else [CmmAssign stack_use (CmmReg spLimReg)]
388 -- Trick the optimizer into eliminating the branch for us
393 gc_stack_check :: BlockId -> WordOff -> [CmmStmt]
394 gc_stack_check gc_block max_frame_size
395 = check_stack_limit where
396 check_stack_limit = [
398 (CmmMachOp (MO_U_Lt (typeWidth (cmmRegType spReg)))
399 [CmmRegOff spReg (-max_frame_size*wORD_SIZE),
404 pack_continuation :: ContinuationFormat -- ^ The current format
405 -> ContinuationFormat -- ^ The return point format
407 pack_continuation (ContinuationFormat _ curr_id curr_frame_size _)
408 (ContinuationFormat _ cont_id cont_frame_size live_regs)
409 = pack_frame curr_frame_size cont_frame_size maybe_header continuation_args
411 continuation_args = map (maybe Nothing (Just . CmmReg . CmmLocal))
414 case (curr_id, cont_id) of
415 (Just x, Just y) -> x /= y
418 maybe_header = if needs_header_set
419 then maybe Nothing (Just . CmmLit . CmmLabel . entryLblToInfoLbl) cont_id
422 pack_frame :: WordOff -- ^ Current frame size
423 -> WordOff -- ^ Next frame size
424 -> Maybe CmmExpr -- ^ Next frame header if any
425 -> [Maybe CmmExpr] -- ^ Next frame data
427 pack_frame curr_frame_size next_frame_size next_frame_header frame_args =
428 store_live_values ++ set_stack_header
430 -- TODO: only save variables when actually needed
431 -- (may be handled by latter pass)
433 [stack_put spRel expr offset
434 | (expr, offset) <- cont_offsets]
436 case next_frame_header of
438 Just expr -> [stack_put spRel expr 0]
440 -- TODO: factor with function_entry and CmmInfo.hs(?)
441 cont_offsets = mkOffsets label_size frame_args
443 label_size = 1 :: WordOff
446 mkOffsets size (Nothing:exprs) = mkOffsets (size+1) exprs
447 mkOffsets size (Just expr:exprs) = (expr, size):mkOffsets (size + width) exprs
449 width = (widthInBytes $ typeWidth $ cmmExprType expr) `quot` wORD_SIZE
450 -- TODO: it would be better if we had a machRepWordWidth
452 spRel = curr_frame_size - next_frame_size
455 -- Lazy adjustment of stack headers assumes all blocks
456 -- that could branch to eachother (i.e. control blocks)
457 -- have the same stack format (this causes a problem
458 -- only for proc-point).
459 function_entry :: ContinuationFormat -> [CmmStmt]
460 function_entry (ContinuationFormat formals _ _ live_regs)
461 = load_live_values ++ load_args where
462 -- TODO: only save variables when actually needed
463 -- (may be handled by latter pass)
465 [stack_get 0 reg offset
466 | (reg, offset) <- curr_offsets]
468 [stack_get 0 reg offset
469 | (reg, StackParam offset) <- argument_formats] ++
470 [global_get reg global
471 | (reg, RegisterParam global) <- argument_formats]
473 argument_formats = assignArguments (localRegType) formals
475 -- TODO: eliminate copy/paste with pack_continuation
476 curr_offsets = mkOffsets label_size live_regs
478 label_size = 1 :: WordOff
481 mkOffsets size (Nothing:regs) = mkOffsets (size+1) regs
482 mkOffsets size (Just reg:regs) = (reg, size):mkOffsets (size + width) regs
484 width = (widthInBytes $ typeWidth $ localRegType reg) `quot` wORD_SIZE
485 -- TODO: it would be better if we had a machRepWordWidth
487 -----------------------------------------------------------------------------
488 -- Section: Stack and argument register puts and gets
489 -----------------------------------------------------------------------------
492 -- |Construct a 'CmmStmt' that will save a value on the stack
493 stack_put :: WordOff -- ^ Offset from the real 'Sp' that 'offset'
494 -- is relative to (added to offset)
495 -> CmmExpr -- ^ What to store onto the stack
496 -> WordOff -- ^ Where on the stack to store it
497 -- (positive <=> higher addresses)
499 stack_put spRel expr offset =
500 CmmStore (CmmRegOff spReg (wORD_SIZE*(spRel + offset))) expr
502 --------------------------------
508 stack_get spRel reg offset =
509 CmmAssign (CmmLocal reg)
510 (CmmLoad (CmmRegOff spReg (wORD_SIZE*(spRel + offset)))
512 global_put :: CmmExpr -> GlobalReg -> CmmStmt
513 global_put expr global = CmmAssign (CmmGlobal global) expr
514 global_get :: LocalReg -> GlobalReg -> CmmStmt
515 global_get reg global = CmmAssign (CmmLocal reg) (CmmReg (CmmGlobal global))