--- be arguments to the continuation of the block once the call (if any) returns.
-
-cmmBlockifyCalls :: [CmmBasicBlock] -> CPS [(CPSBlockInfo, CmmBasicBlock)]
-cmmBlockifyCalls blocks = liftM concat $ mapM breakBlock blocks
-
--- [(CmmReg,MachHint)] is the results from the previous block that are expected as parameters
---breakBlock :: CmmBasicBlock -> CPS [(Maybe BlockId, CmmBasicBlock)]
-breakBlock :: CmmBasicBlock -> CPS [(CPSBlockInfo, CmmBasicBlock)]
-breakBlock (BasicBlock ident stmts) = breakBlock' ident ControlBlock [] stmts
-
-breakBlock' current_id block_info accum_stmts [] =
- return [(block_info, BasicBlock current_id accum_stmts)]
--- TODO: notice a call just before a branch, jump, call, etc.
-breakBlock' current_id block_info accum_stmts (stmt@(CmmCall _ results _ _):stmts) = do
- new_id <- newLabelCPS
- let new_block = (block_info, BasicBlock current_id (accum_stmts ++ [stmt, CmmBranch new_id]))
- rest <- breakBlock' new_id (ContinuationBlock results) [] stmts
- return $ (new_block:rest)
-breakBlock' current_id arguments accum_stmts (stmt:stmts) =
- breakBlock' current_id arguments (accum_stmts ++ [stmt]) stmts
+-- be arguments to the continuation of the block once the call (if any)
+-- returns.
+
+breakBlock :: [Unique] -> CmmBasicBlock -> BlockEntryInfo -> [BrokenBlock]
+breakBlock uniques (BasicBlock ident stmts) entry =
+ breakBlock' uniques ident entry [] [] stmts where
+ breakBlock' uniques current_id entry exits accum_stmts stmts =
+ case stmts of
+ [] -> panic "block doesn't end in jump, goto or return"
+ [CmmJump target arguments] ->
+ [BrokenBlock current_id entry accum_stmts
+ exits
+ (FinalJump target arguments)]
+ [CmmReturn arguments] ->
+ [BrokenBlock current_id entry accum_stmts
+ exits
+ (FinalReturn arguments)]
+ [CmmBranch target] ->
+ [BrokenBlock current_id entry accum_stmts
+ (target:exits)
+ (FinalBranch target)]
+ [CmmSwitch expr targets] ->
+ [BrokenBlock current_id entry accum_stmts
+ (mapMaybe id targets ++ exits)
+ (FinalSwitch expr targets)]
+ (CmmJump _ _:_) ->
+ panic "jump in middle of block"
+ (CmmReturn _:_) ->
+ panic "return in middle of block"
+ (CmmBranch _:_) ->
+ panic "branch in middle of block"
+ (CmmSwitch _ _:_) ->
+ panic ("switch in middle of block" ++ (showSDoc $ ppr stmts))
+ (CmmCall target results arguments saves:stmts) -> block : rest
+ where
+ new_id = BlockId $ head uniques
+ block = BrokenBlock current_id entry accum_stmts
+ (new_id:exits)
+ (FinalCall new_id target results arguments saves)
+ rest = breakBlock' (tail uniques) new_id
+ (ContinuationEntry results) [] [] stmts
+ (s@(CmmCondBranch test target):stmts) ->
+ breakBlock' uniques current_id entry
+ (target:exits) (accum_stmts++[s]) stmts
+ (s:stmts) ->
+ breakBlock' uniques current_id entry
+ exits (accum_stmts++[s]) stmts
+
+--------------------------------
+-- Convert from a BrokenBlock
+-- to a CmmBasicBlock so the
+-- liveness analysis can run
+-- on it.
+--------------------------------
+cmmBlockFromBrokenBlock :: BrokenBlock -> CmmBasicBlock
+cmmBlockFromBrokenBlock (BrokenBlock ident _ stmts _ exit) =
+ BasicBlock ident (stmts++exit_stmt)
+ where
+ exit_stmt =
+ case exit of
+ FinalBranch target -> [CmmBranch target]
+ FinalReturn arguments -> [CmmReturn arguments]
+ FinalJump target arguments -> [CmmJump target arguments]
+ FinalSwitch expr targets -> [CmmSwitch expr targets]
+ FinalCall branch_target call_target results arguments saves ->
+ [CmmCall call_target results arguments saves,
+ CmmBranch branch_target]
+
+-----------------------------------------------------------------------------
+-- CPS a single CmmTop (proceedure)
+-----------------------------------------------------------------------------
+
+cpsProc :: UniqSupply -> CmmTop -> [CmmTop]
+cpsProc uniqSupply x@(CmmData _ _) = [x]
+cpsProc uniqSupply x@(CmmProc info_table ident params blocks) =
+ --[CmmProc info_table ident params cps_blocks]
+ cps_continuations
+ where
+ uniqes :: [[Unique]]
+ uniqes = map uniqsFromSupply $ listSplitUniqSupply uniqSupply
+
+ -- Break the block at each function call
+ broken_blocks :: [BrokenBlock]
+ broken_blocks = concat $ zipWith3 breakBlock uniqes blocks
+ (FunctionEntry ident params:repeat ControlEntry)
+
+ -- Calculate live variables for each broken block
+ live :: BlockEntryLiveness
+ live = cmmLiveness $ map cmmBlockFromBrokenBlock broken_blocks
+ -- nothing can be live on entry to the first block so we could take the tail
+
+ proc_points :: UniqSet BlockId
+ proc_points = calculateProcPoints broken_blocks
+
+ continuations :: [Continuation]
+ continuations = map (buildContinuation proc_points (blocksToBlockEnv broken_blocks)) (uniqSetToList proc_points)
+
+ -- TODO: insert proc point code here
+ -- * Branches and switches to proc points may cause new blocks to be created
+ -- (or proc points could leave behind phantom blocks that just jump to them)
+ -- * Proc points might get some live variables passed as arguments
+
+ -- TODO: let blocks_with_live = map (cmmLivenessComment live . snd) broken_blocks
+
+ --procs = groupBlocksIntoContinuations live broken_blocks
+
+ -- Select the stack format on entry to each block
+ formats2 :: [(CLabel, StackFormat)]
+ formats2 = selectStackFormat2 live continuations
+
+ -- Do the actual CPS transform
+ cps_continuations :: [CmmTop]
+ cps_continuations = map (constructContinuation formats2) continuations