-import UniqSupply
-import UniqFM
-import UniqSet
-import Unique
-
-import Monad
-import IO
-import Data.List
-
---------------------------------------------------------------------------------
-
--- The format for the call to a continuation
--- The fst is the arguments that must be passed to the continuation
--- by the continuation's caller.
--- The snd is the live values that must be saved on stack.
--- A Nothing indicates an ignored slot.
--- The head of each list is the stack top or the first parameter.
-
--- The format for live values for a particular continuation
--- All on stack for now.
--- Head element is the top of the stack (or just under the header).
--- Nothing means an empty slot.
--- Future possibilities include callee save registers (i.e. passing slots in register)
--- and heap memory (not sure if that's usefull at all though, but it may
--- be worth exploring the design space).
-
-continuationLabel (Continuation _ _ l _ _) = l
-data Continuation =
- Continuation
- Bool -- True => Function entry, False => Continuation/return point
- [CmmStatic] -- Info table, may be empty
- CLabel -- Used to generate both info & entry labels
- CmmFormals -- Argument locals live on entry (C-- procedure params)
- [BrokenBlock] -- Code, may be empty. The first block is
- -- the entry point. The order is otherwise initially
- -- unimportant, but at some point the code gen will
- -- fix the order.
-
- -- the BlockId of the first block does not give rise
- -- to a label. To jump to the first block in a Proc,
- -- use the appropriate CLabel.
-
--- Describes the layout of a stack frame for a continuation
-data StackFormat
- = StackFormat
- (Maybe CLabel) -- The label occupying the top slot
- WordOff -- Total frame size in words
- [(CmmReg, WordOff)] -- local reg offsets from stack top
-
--- A block can be a continuation of a call
--- A block can be a continuation of another block (w/ or w/o joins)
--- A block can be an entry to a function
-
------------------------------------------------------------------------------
-
-collectNonProcPointTargets ::
- UniqSet BlockId -> BlockEnv BrokenBlock
- -> UniqSet BlockId -> BlockId -> UniqSet BlockId
-collectNonProcPointTargets proc_points blocks current_targets block =
- if sizeUniqSet current_targets == sizeUniqSet new_targets
- then current_targets
- else foldl (collectNonProcPointTargets proc_points blocks) new_targets targets
- where
- block' = lookupWithDefaultUFM blocks (panic "TODO") block
- targets =
- -- Note the subtlety that since the extra branch after a call
- -- will always be to a block that is a proc-point,
- -- this subtraction will always remove that case
- uniqSetToList $ (mkUniqSet $ brokenBlockTargets block') `minusUniqSet` proc_points
- -- TODO: remove redundant uniqSetToList
- new_targets = current_targets `unionUniqSets` (mkUniqSet targets)
-
-buildContinuation ::
- UniqSet BlockId -> BlockEnv BrokenBlock
- -> BlockId -> Continuation
-buildContinuation proc_points blocks start =
- Continuation is_entry info_table clabel params body
- where
- children = (collectNonProcPointTargets proc_points blocks (unitUniqSet start) start) `delOneFromUniqSet` start
- start_block = lookupWithDefaultUFM blocks (panic "TODO") start
- children_blocks = map (lookupWithDefaultUFM blocks (panic "TODO")) (uniqSetToList children)
- body = start_block : children_blocks
- info_table = [] -- TODO
- start_block_entry = brokenBlockEntry start_block
- is_entry = case start_block_entry of
- FunctionEntry _ _ -> True
- _ -> False
- clabel = case start_block_entry of
- FunctionEntry label _ -> label
- _ -> mkReturnPtLabel $ getUnique start
- params = case start_block_entry of
- FunctionEntry _ args -> args
- ContinuationEntry args -> args
- ControlEntry -> [] -- TODO: it's a proc-point, we could pass lives in parameter registers
-
---------------------------------------------------------------------------------
--- For now just select the continuation orders in the order they are in the set with no gaps
-
-selectStackFormat2 :: BlockEnv CmmLive -> [Continuation] -> [(CLabel, StackFormat)]
-selectStackFormat2 live continuations =
- map (\c -> (continuationLabel c, selectStackFormat' c)) continuations
- where
- selectStackFormat' (Continuation True info_table label formals blocks) =
- --let ident = brokenBlockId $ head blocks -- TODO: CLabel isn't a uniquable, but we need a better way than this
- --in
- StackFormat (Just label) 0 []
- selectStackFormat' (Continuation False info_table label formals blocks) =
- -- TODO: assumes the first block is the entry block
- let ident = brokenBlockId $ head blocks -- TODO: CLabel isn't a uniquable, but we need a better way than this
- in live_to_format label formals $ lookupWithDefaultUFM live unknown_block ident
-
- live_to_format :: CLabel -> CmmFormals -> CmmLive -> StackFormat
- live_to_format label formals live =
- foldl extend_format
- (StackFormat (Just label) retAddrSizeW [])
- (uniqSetToList (live `minusUniqSet` mkUniqSet (cmmFormalsToLiveLocals formals)))
-
- extend_format :: StackFormat -> LocalReg -> StackFormat
- extend_format (StackFormat label size offsets) reg =
- StackFormat label (slot_size reg + size) ((CmmLocal reg, size) : offsets)
-
- unknown_block = panic "unknown BlockId in selectStackFormat"
-
-slot_size reg = ((machRepByteWidth (localRegRep reg) - 1) `div` wORD_SIZE) + 1
-
-constructContinuation :: [(CLabel, StackFormat)] -> Continuation -> CmmTop
-constructContinuation formats (Continuation is_entry info label formals blocks) =
- CmmProc info label formals (map (constructContinuation2' label formats) blocks)
-
-constructContinuation2' :: CLabel -> [(CLabel, StackFormat)] -> BrokenBlock
- -> CmmBasicBlock
-constructContinuation2' curr_ident formats (BrokenBlock ident entry stmts _ exit) =
- BasicBlock ident (prefix++stmts++postfix)
- where
- curr_format = maybe unknown_block id $ lookup curr_ident formats
- unknown_block = panic "unknown BlockId in constructContinuation"
- prefix = case entry of
- ControlEntry -> []
- FunctionEntry _ _ -> []
- ContinuationEntry formals ->
- unpack_continuation curr_format
- postfix = case exit of
- FinalBranch next -> [CmmBranch next]
- FinalSwitch expr targets -> [CmmSwitch expr targets]
- FinalReturn arguments ->
- exit_function curr_format
- (CmmLoad (CmmReg spReg) wordRep)
- arguments
- FinalJump target arguments ->
- exit_function curr_format target arguments
- -- TODO: do something about global saves
- FinalCall next (CmmForeignCall target CmmCallConv)
- results arguments saves ->
- pack_continuation curr_format cont_format ++
- [CmmJump target arguments]
- where
- cont_format = maybe unknown_block id $
- lookup (mkReturnPtLabel $ getUnique next) formats
- FinalCall next _ results arguments saves -> panic "unimplemented CmmCall"