+-- CPS transform for those procs that actually need it
+-- The plan is this:
+--
+-- * Introduce a stack-check block as the first block
+-- * The first blocks gets a FunctionEntry; the rest are ControlEntry
+-- * Now break each block into a bunch of blocks (at call sites);
+-- all but the first will be ContinuationEntry
+--
+cpsProc uniqSupply (CmmProc info ident params (ListGraph blocks)) = cps_procs
+ where
+ -- We need to be generating uniques for several things.
+ -- We could make this function monadic to handle that
+ -- but since there is no other reason to make it monadic,
+ -- we instead will just split them all up right here.
+ (uniqSupply1, uniqSupply2) = splitUniqSupply uniqSupply
+ uniques :: [[Unique]]
+ uniques = map uniqsFromSupply $ listSplitUniqSupply uniqSupply1
+ (stack_check_block_unique:stack_use_unique:adaptor_uniques) :
+ block_uniques = uniques
+ proc_uniques = map (map (map uniqsFromSupply . listSplitUniqSupply) . listSplitUniqSupply) $ listSplitUniqSupply uniqSupply2
+
+ stack_use = CmmLocal (LocalReg stack_use_unique (cmmRegRep spReg) GCKindPtr)
+ stack_check_block_id = BlockId stack_check_block_unique
+ stack_check_block = make_stack_check stack_check_block_id info stack_use (blockId $ head blocks)
+
+ forced_blocks = stack_check_block : blocks
+
+ CmmInfo maybe_gc_block_id update_frame _ = info
+
+ -- Break the block at each function call.
+ -- The part after the function call will have to become a continuation.
+ broken_blocks :: ([(BlockId, ContFormat)], [BrokenBlock])
+ broken_blocks =
+ (\x -> (concatMap fst x, concatMap snd x)) $
+ zipWith3 (breakBlock (maybeToList maybe_gc_block_id))
+ block_uniques
+ forced_blocks
+ (FunctionEntry info ident params :
+ repeat ControlEntry)
+
+ f' = selectContinuations (fst broken_blocks)
+ broken_blocks' = map (makeContinuationEntries f') $
+ concat $
+ zipWith (adaptBlockToFormat f')
+ adaptor_uniques
+ (snd broken_blocks)
+
+ -- Calculate live variables for each broken block.
+ --
+ -- Nothing can be live on entry to the first block
+ -- so we could take the tail, but for now we wont
+ -- to help future proof the code.
+ live :: BlockEntryLiveness
+ live = cmmLiveness $ map cmmBlockFromBrokenBlock broken_blocks'
+
+ -- Calculate which blocks must be made into full fledged procedures.
+ proc_points :: UniqSet BlockId
+ proc_points = calculateProcPoints broken_blocks'
+
+ -- Construct a map so we can lookup a broken block by its 'BlockId'.
+ block_env :: BlockEnv BrokenBlock
+ block_env = blocksToBlockEnv broken_blocks'
+
+ -- Group the blocks into continuations based on the set of proc-points.
+ continuations :: [Continuation (Either C_SRT CmmInfo)]
+ continuations = map (gatherBlocksIntoContinuation live proc_points block_env)
+ (uniqSetToList proc_points)
+
+ -- Select the stack format on entry to each continuation.
+ -- Return the max stack offset and an association list
+ --
+ -- This is an association list instead of a UniqFM because
+ -- CLabel's don't have a 'Uniqueable' instance.
+ formats :: [(CLabel, -- key
+ (CmmFormalsWithoutKinds, -- arguments
+ Maybe CLabel, -- label in top slot
+ [Maybe LocalReg]))] -- slots
+ formats = selectContinuationFormat live continuations
+
+ -- Do a little meta-processing on the stack formats such as
+ -- getting the individual frame sizes and the maximum frame size
+ formats' :: (WordOff, WordOff, [(CLabel, ContinuationFormat)])
+ formats'@(_, _, format_list) = processFormats formats update_frame continuations
+
+ -- Update the info table data on the continuations with
+ -- the selected stack formats.
+ continuations' :: [Continuation CmmInfo]
+ continuations' = map (applyContinuationFormat format_list) continuations
+
+ -- Do the actual CPS transform.
+ cps_procs :: [CmmTop]
+ cps_procs = zipWith (continuationToProc formats' stack_use) proc_uniques continuations'
+
+make_stack_check stack_check_block_id info stack_use next_block_id =
+ BasicBlock stack_check_block_id $
+ check_stmts ++ [CmmBranch next_block_id]
+ where
+ check_stmts =
+ case info of
+ -- If we are given a stack check handler,
+ -- then great, well check the stack.
+ CmmInfo (Just gc_block) _ _
+ -> [CmmCondBranch
+ (CmmMachOp (MO_U_Lt $ cmmRegRep spReg)
+ [CmmReg stack_use, CmmReg spLimReg])
+ gc_block]
+ -- If we aren't given a stack check handler,
+ -- then humph! we just won't check the stack for them.
+ CmmInfo Nothing _ _
+ -> []