2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
5 \section[CgMonad]{The code generation monad}
7 See the beginning of the top-level @CodeGen@ module, to see how this
8 monadic stuff fits into the Big Picture.
15 initC, thenC, thenFC, listCs, listFCs, mapCs, mapFCs,
16 returnFC, fixC, fixC_, checkedAbsC,
17 stmtC, stmtsC, labelC, emitStmts, nopC, whenC, newLabelC,
18 newUnique, newUniqSupply,
20 CgStmts, emitCgStmts, forkCgStmts, cgStmtsToBlocks,
21 getCgStmts', getCgStmts,
22 noCgStmts, oneCgStmt, consCgStmt,
25 emitData, emitProc, emitSimpleProc,
28 forkClosureBody, forkStatics, forkAlts, forkEval,
29 forkEvalHelp, forkProc, codeOnly,
30 SemiTaggingStuff, ConTagZ,
33 setEndOfBlockInfo, getEndOfBlockInfo,
36 setSRTLabel, getSRTLabel,
37 setTickyCtrLabel, getTickyCtrLabel,
39 StackUsage(..), HeapUsage(..),
40 VirtualSpOffset, VirtualHpOffset,
41 initStkUsage, initHpUsage,
42 getHpUsage, setHpUsage,
47 Sequel(..), -- ToDo: unabstract?
49 -- ideally we wouldn't export these, but some other modules access internal state
50 getState, setState, getInfoDown, getDynFlags, getThisPackage,
52 -- more localised access to monad state
53 getStkUsage, setStkUsage,
54 getBinds, setBinds, getStaticBinds,
56 -- out of general friendliness, we also export ...
57 CgInfoDownwards(..), CgState(..) -- non-abstract
60 #include "HsVersions.h"
62 import {-# SOURCE #-} CgBindery ( CgBindings, nukeVolatileBinds )
84 infixr 9 `thenC` -- Right-associative!
88 %************************************************************************
90 \subsection[CgMonad-environment]{Stuff for manipulating environments}
92 %************************************************************************
94 This monadery has some information that it only passes {\em
95 downwards}, as well as some ``state'' which is modified as we go
99 data CgInfoDownwards -- information only passed *downwards* by the monad
101 cgd_dflags :: DynFlags,
102 cgd_mod :: Module, -- Module being compiled
103 cgd_statics :: CgBindings, -- [Id -> info] : static environment
104 cgd_srt_lbl :: CLabel, -- label of the current SRT
105 cgd_srt :: SRT, -- the current SRT
106 cgd_ticky :: CLabel, -- current destination for ticky counts
107 cgd_eob :: EndOfBlockInfo -- Info for stuff to do at end of basic block:
110 initCgInfoDown :: DynFlags -> Module -> CgInfoDownwards
111 initCgInfoDown dflags mod
112 = MkCgInfoDown { cgd_dflags = dflags,
114 cgd_statics = emptyVarEnv,
115 cgd_srt_lbl = error "initC: srt_lbl",
116 cgd_srt = error "initC: srt",
117 cgd_ticky = mkTopTickyCtrLabel,
118 cgd_eob = initEobInfo }
122 cgs_stmts :: OrdList CgStmt, -- Current proc
123 cgs_tops :: OrdList CmmTop,
124 -- Other procedures and data blocks in this compilation unit
125 -- Both the latter two are ordered only so that we can
126 -- reduce forward references, when it's easy to do so
128 cgs_binds :: CgBindings, -- [Id -> info] : *local* bindings environment
129 -- Bindings for top-level things are given in
130 -- the info-down part
132 cgs_stk_usg :: StackUsage,
133 cgs_hp_usg :: HeapUsage,
135 cgs_uniqs :: UniqSupply }
137 initCgState :: UniqSupply -> CgState
139 = MkCgState { cgs_stmts = nilOL, cgs_tops = nilOL,
140 cgs_binds = emptyVarEnv,
141 cgs_stk_usg = initStkUsage,
142 cgs_hp_usg = initHpUsage,
146 @EndOfBlockInfo@ tells what to do at the end of this block of code or,
147 if the expression is a @case@, what to do at the end of each
153 VirtualSpOffset -- Args Sp: trim the stack to this point at a
154 -- return; push arguments starting just
155 -- above this point on a tail call.
157 -- This is therefore the stk ptr as seen
158 -- by a case alternative.
161 initEobInfo :: EndOfBlockInfo
162 initEobInfo = EndOfBlockInfo 0 OnStack
165 Any addressing modes inside @Sequel@ must be ``robust,'' in the sense
166 that it must survive stack pointer adjustments at the end of the
171 = OnStack -- Continuation is on the stack
172 | UpdateCode -- Continuation is update
175 CLabel -- Jump to this; if the continuation is for a vectored
176 -- case this might be the label of a return vector
178 Id -- The case binder, only used to see if it's dead
180 type SemiTaggingStuff
181 = Maybe -- Maybe[1] we don't have any semi-tagging stuff...
182 ([(ConTagZ, CmmLit)], -- Alternatives
183 CmmLit) -- Default (will be a can't happen RTS label if can't happen)
185 type ConTagZ = Int -- A *zero-indexed* contructor tag
187 -- The case branch is executed only from a successful semitagging
188 -- venture, when a case has looked at a variable, found that it's
189 -- evaluated, and wants to load up the contents and go to the join
193 %************************************************************************
197 %************************************************************************
199 The CgStmts type is what the code generator outputs: it is a tree of
200 statements, including in-line labels. The job of flattenCgStmts is to
201 turn this into a list of basic blocks, each of which ends in a jump
202 statement (either a local branch or a non-local jump).
205 type CgStmts = OrdList CgStmt
210 | CgFork BlockId CgStmts
212 flattenCgStmts :: BlockId -> CgStmts -> [CmmBasicBlock]
213 flattenCgStmts id stmts =
214 case flatten (fromOL stmts) of
215 ([],blocks) -> blocks
216 (block,blocks) -> BasicBlock id block : blocks
220 -- A label at the end of a function or fork: this label must not be reachable,
221 -- but it might be referred to from another BB that also isn't reachable.
222 -- Eliminating these has to be done with a dead-code analysis. For now,
223 -- we just make it into a well-formed block by adding a recursive jump.
225 = ( [CmmBranch id], [BasicBlock id [CmmBranch id]] )
227 -- A jump/branch: throw away all the code up to the next label, because
228 -- it is unreachable. Be careful to keep forks that we find on the way.
229 flatten (CgStmt stmt : stmts)
231 = case dropWhile isOrdinaryStmt stmts of
233 [CgLabel id] -> ( [stmt], [BasicBlock id [CmmBranch id]])
234 (CgLabel id : stmts) -> ( [stmt], BasicBlock id block : blocks )
235 where (block,blocks) = flatten stmts
236 (CgFork fork_id stmts : ss) ->
237 flatten (CgFork fork_id stmts : CgStmt stmt : ss)
238 (CgStmt {} : _) -> panic "CgStmt not seen as ordinary"
242 CgStmt stmt -> (stmt:block,blocks)
243 CgLabel id -> ([CmmBranch id],BasicBlock id block:blocks)
244 CgFork fork_id stmts ->
245 (block, BasicBlock fork_id fork_block : fork_blocks ++ blocks)
246 where (fork_block, fork_blocks) = flatten (fromOL stmts)
247 where (block,blocks) = flatten ss
249 isJump :: CmmStmt -> Bool
250 isJump (CmmJump _ _) = True
251 isJump (CmmBranch _) = True
252 isJump (CmmSwitch _ _) = True
253 isJump (CmmReturn _) = True
256 isOrdinaryStmt :: CgStmt -> Bool
257 isOrdinaryStmt (CgStmt _) = True
258 isOrdinaryStmt _ = False
261 %************************************************************************
263 Stack and heap models
265 %************************************************************************
268 type VirtualHpOffset = WordOff -- Both are in
269 type VirtualSpOffset = WordOff -- units of words
273 virtSp :: VirtualSpOffset,
274 -- Virtual offset of topmost allocated slot
276 frameSp :: VirtualSpOffset,
277 -- Virtual offset of the return address of the enclosing frame.
278 -- This RA describes the liveness/pointedness of
279 -- all the stack from frameSp downwards
280 -- INVARIANT: less than or equal to virtSp
282 freeStk :: [VirtualSpOffset],
283 -- List of free slots, in *increasing* order
284 -- INVARIANT: all <= virtSp
285 -- All slots <= virtSp are taken except these ones
287 realSp :: VirtualSpOffset,
288 -- Virtual offset of real stack pointer register
290 hwSp :: VirtualSpOffset
291 } -- Highest value ever taken by virtSp
293 -- INVARIANT: The environment contains no Stable references to
294 -- stack slots below (lower offset) frameSp
295 -- It can contain volatile references to this area though.
299 virtHp :: VirtualHpOffset, -- Virtual offset of highest-allocated word
300 realHp :: VirtualHpOffset -- realHp: Virtual offset of real heap ptr
304 The heap high water mark is the larger of virtHp and hwHp. The latter is
305 only records the high water marks of forked-off branches, so to find the
306 heap high water mark you have to take the max of virtHp and hwHp. Remember,
307 virtHp never retreats!
309 Note Jan 04: ok, so why do we only look at the virtual Hp??
312 heapHWM :: HeapUsage -> VirtualHpOffset
319 initStkUsage :: StackUsage
320 initStkUsage = StackUsage {
328 initHpUsage :: HeapUsage
329 initHpUsage = HeapUsage {
335 @stateIncUsage@$~e_1~e_2$ incorporates in $e_1$ the stack and heap high water
336 marks found in $e_2$.
339 stateIncUsage :: CgState -> CgState -> CgState
340 stateIncUsage s1 s2@(MkCgState { cgs_stk_usg = stk_usg, cgs_hp_usg = hp_usg })
341 = s1 { cgs_hp_usg = cgs_hp_usg s1 `maxHpHw` virtHp hp_usg,
342 cgs_stk_usg = cgs_stk_usg s1 `maxStkHw` hwSp stk_usg }
343 `addCodeBlocksFrom` s2
345 stateIncUsageEval :: CgState -> CgState -> CgState
346 stateIncUsageEval s1 s2
347 = s1 { cgs_stk_usg = cgs_stk_usg s1 `maxStkHw` hwSp (cgs_stk_usg s2) }
348 `addCodeBlocksFrom` s2
349 -- We don't max the heap high-watermark because stateIncUsageEval is
350 -- used only in forkEval, which in turn is only used for blocks of code
351 -- which do their own heap-check.
353 addCodeBlocksFrom :: CgState -> CgState -> CgState
354 -- Add code blocks from the latter to the former
355 -- (The cgs_stmts will often be empty, but not always; see codeOnly)
356 s1 `addCodeBlocksFrom` s2
357 = s1 { cgs_stmts = cgs_stmts s1 `appOL` cgs_stmts s2,
358 cgs_tops = cgs_tops s1 `appOL` cgs_tops s2 }
360 maxHpHw :: HeapUsage -> VirtualHpOffset -> HeapUsage
361 hp_usg `maxHpHw` hw = hp_usg { virtHp = virtHp hp_usg `max` hw }
363 maxStkHw :: StackUsage -> VirtualSpOffset -> StackUsage
364 stk_usg `maxStkHw` hw = stk_usg { hwSp = hwSp stk_usg `max` hw }
367 %************************************************************************
371 %************************************************************************
374 newtype FCode a = FCode (CgInfoDownwards -> CgState -> (a, CgState))
377 instance Monad FCode where
382 {-# INLINE thenFC #-}
383 {-# INLINE returnFC #-}
385 The Abstract~C is not in the environment so as to improve strictness.
388 initC :: DynFlags -> Module -> FCode a -> IO a
390 initC dflags mod (FCode code)
391 = do { uniqs <- mkSplitUniqSupply 'c'
392 ; case code (initCgInfoDown dflags mod) (initCgState uniqs) of
393 (res, _) -> return res
396 returnFC :: a -> FCode a
397 returnFC val = FCode (\_ state -> (val, state))
401 thenC :: Code -> FCode a -> FCode a
402 thenC (FCode m) (FCode k) =
403 FCode (\info_down state -> let (_,new_state) = m info_down state in
404 k info_down new_state)
406 listCs :: [Code] -> Code
407 listCs [] = return ()
412 mapCs :: (a -> Code) -> [a] -> Code
417 thenFC :: FCode a -> (a -> FCode c) -> FCode c
418 thenFC (FCode m) k = FCode (
421 (m_result, new_state) = m info_down state
422 (FCode kcode) = k m_result
424 kcode info_down new_state
427 listFCs :: [FCode a] -> FCode [a]
430 mapFCs :: (a -> FCode b) -> [a] -> FCode [b]
434 And the knot-tying combinator:
436 fixC :: (a -> FCode a) -> FCode a
441 result@(v,_) = fc info_down state
447 fixC_ :: (a -> FCode a) -> FCode ()
448 fixC_ fcode = fixC fcode >> return ()
451 %************************************************************************
453 Operators for getting and setting the state and "info_down".
456 %************************************************************************
459 getState :: FCode CgState
460 getState = FCode $ \_ state -> (state,state)
462 setState :: CgState -> FCode ()
463 setState state = FCode $ \_ _ -> ((),state)
465 getStkUsage :: FCode StackUsage
468 return $ cgs_stk_usg state
470 setStkUsage :: StackUsage -> Code
471 setStkUsage new_stk_usg = do
473 setState $ state {cgs_stk_usg = new_stk_usg}
475 getHpUsage :: FCode HeapUsage
478 return $ cgs_hp_usg state
480 setHpUsage :: HeapUsage -> Code
481 setHpUsage new_hp_usg = do
483 setState $ state {cgs_hp_usg = new_hp_usg}
485 getBinds :: FCode CgBindings
488 return $ cgs_binds state
490 setBinds :: CgBindings -> FCode ()
491 setBinds new_binds = do
493 setState $ state {cgs_binds = new_binds}
495 getStaticBinds :: FCode CgBindings
498 return (cgd_statics info)
500 withState :: FCode a -> CgState -> FCode (a,CgState)
501 withState (FCode fcode) newstate = FCode $ \info_down state ->
502 let (retval, state2) = fcode info_down newstate in ((retval,state2), state)
504 newUniqSupply :: FCode UniqSupply
507 let (us1, us2) = splitUniqSupply (cgs_uniqs state)
508 setState $ state { cgs_uniqs = us1 }
511 newUnique :: FCode Unique
514 return (uniqFromSupply us)
517 getInfoDown :: FCode CgInfoDownwards
518 getInfoDown = FCode $ \info_down state -> (info_down,state)
520 getDynFlags :: FCode DynFlags
521 getDynFlags = liftM cgd_dflags getInfoDown
523 getThisPackage :: FCode PackageId
524 getThisPackage = liftM thisPackage getDynFlags
526 withInfoDown :: FCode a -> CgInfoDownwards -> FCode a
527 withInfoDown (FCode fcode) info_down = FCode $ \_ state -> fcode info_down state
529 doFCode :: FCode a -> CgInfoDownwards -> CgState -> (a,CgState)
530 doFCode (FCode fcode) info_down state = fcode info_down state
534 %************************************************************************
538 %************************************************************************
540 @forkClosureBody@ takes a code, $c$, and compiles it in a completely
541 fresh environment, except that:
542 - compilation info and statics are passed in unchanged.
543 The current environment is passed on completely unaltered, except that
544 abstract C from the fork is incorporated.
546 @forkProc@ takes a code and compiles it in the current environment,
547 returning the basic blocks thus constructed. The current environment
548 is passed on completely unchanged. It is pretty similar to
549 @getBlocks@, except that the latter does affect the environment.
551 @forkStatics@ $fc$ compiles $fc$ in an environment whose statics come
552 from the current bindings, but which is otherwise freshly initialised.
553 The Abstract~C returned is attached to the current state, but the
554 bindings and usage information is otherwise unchanged.
557 forkClosureBody :: Code -> Code
558 forkClosureBody body_code
559 = do { info <- getInfoDown
560 ; us <- newUniqSupply
562 ; let body_info_down = info { cgd_eob = initEobInfo }
563 ((),fork_state) = doFCode body_code body_info_down
565 ; ASSERT( isNilOL (cgs_stmts fork_state) )
566 setState $ state `addCodeBlocksFrom` fork_state }
568 forkStatics :: FCode a -> FCode a
569 forkStatics body_code
570 = do { info <- getInfoDown
571 ; us <- newUniqSupply
573 ; let rhs_info_down = info { cgd_statics = cgs_binds state,
574 cgd_eob = initEobInfo }
575 (result, fork_state_out) = doFCode body_code rhs_info_down
577 ; ASSERT( isNilOL (cgs_stmts fork_state_out) )
578 setState (state `addCodeBlocksFrom` fork_state_out)
581 forkProc :: Code -> FCode CgStmts
583 = do { info_down <- getInfoDown
584 ; us <- newUniqSupply
586 ; let fork_state_in = (initCgState us)
587 { cgs_binds = cgs_binds state,
588 cgs_stk_usg = cgs_stk_usg state,
589 cgs_hp_usg = cgs_hp_usg state }
590 -- ToDo: is the hp usage necesary?
591 (code_blks, fork_state_out) = doFCode (getCgStmts body_code)
592 info_down fork_state_in
593 ; setState $ state `stateIncUsageEval` fork_state_out
596 codeOnly :: Code -> Code
597 -- Emit any code from the inner thing into the outer thing
598 -- Do not affect anything else in the outer state
599 -- Used in almost-circular code to prevent false loop dependencies
601 = do { info_down <- getInfoDown
602 ; us <- newUniqSupply
604 ; let fork_state_in = (initCgState us) { cgs_binds = cgs_binds state,
605 cgs_stk_usg = cgs_stk_usg state,
606 cgs_hp_usg = cgs_hp_usg state }
607 ((), fork_state_out) = doFCode body_code info_down fork_state_in
608 ; setState $ state `addCodeBlocksFrom` fork_state_out }
611 @forkAlts@ $bs~d$ takes fcodes $bs$ for the branches of a @case@, and
612 an fcode for the default case $d$, and compiles each in the current
613 environment. The current environment is passed on unmodified, except
615 - the worst stack high-water mark is incorporated
616 - the virtual Hp is moved on to the worst virtual Hp for the branches
619 forkAlts :: [FCode a] -> FCode [a]
621 forkAlts branch_fcodes
622 = do { info_down <- getInfoDown
623 ; us <- newUniqSupply
625 ; let compile us branch
626 = (us2, doFCode branch info_down branch_state)
628 (us1,us2) = splitUniqSupply us
629 branch_state = (initCgState us1) {
630 cgs_binds = cgs_binds state,
631 cgs_stk_usg = cgs_stk_usg state,
632 cgs_hp_usg = cgs_hp_usg state }
634 (_us, results) = mapAccumL compile us branch_fcodes
635 (branch_results, branch_out_states) = unzip results
636 ; setState $ foldl stateIncUsage state branch_out_states
637 -- NB foldl. state is the *left* argument to stateIncUsage
638 ; return branch_results }
641 @forkEval@ takes two blocks of code.
643 - The first meddles with the environment to set it up as expected by
644 the alternatives of a @case@ which does an eval (or gc-possible primop).
645 - The second block is the code for the alternatives.
646 (plus info for semi-tagging purposes)
648 @forkEval@ picks up the virtual stack pointer and returns a suitable
649 @EndOfBlockInfo@ for the caller to use, together with whatever value
650 is returned by the second block.
652 It uses @initEnvForAlternatives@ to initialise the environment, and
653 @stateIncUsageAlt@ to incorporate usage; the latter ignores the heap
657 forkEval :: EndOfBlockInfo -- For the body
658 -> Code -- Code to set environment
659 -> FCode Sequel -- Semi-tagging info to store
660 -> FCode EndOfBlockInfo -- The new end of block info
662 forkEval body_eob_info env_code body_code
663 = do { (v, sequel) <- forkEvalHelp body_eob_info env_code body_code
664 ; returnFC (EndOfBlockInfo v sequel) }
666 forkEvalHelp :: EndOfBlockInfo -- For the body
667 -> Code -- Code to set environment
668 -> FCode a -- The code to do after the eval
669 -> FCode (VirtualSpOffset, -- Sp
670 a) -- Result of the FCode
671 -- A disturbingly complicated function
672 forkEvalHelp body_eob_info env_code body_code
673 = do { info_down <- getInfoDown
674 ; us <- newUniqSupply
676 ; let { info_down_for_body = info_down {cgd_eob = body_eob_info}
677 ; (_, env_state) = doFCode env_code info_down_for_body
678 (state {cgs_uniqs = us})
679 ; state_for_body = (initCgState (cgs_uniqs env_state))
680 { cgs_binds = binds_for_body,
681 cgs_stk_usg = stk_usg_for_body }
682 ; binds_for_body = nukeVolatileBinds (cgs_binds env_state)
683 ; stk_usg_from_env = cgs_stk_usg env_state
684 ; virtSp_from_env = virtSp stk_usg_from_env
685 ; stk_usg_for_body = stk_usg_from_env {realSp = virtSp_from_env,
686 hwSp = virtSp_from_env}
687 ; (value_returned, state_at_end_return)
688 = doFCode body_code info_down_for_body state_for_body
690 ; ASSERT( isNilOL (cgs_stmts state_at_end_return) )
691 -- The code coming back should consist only of nested declarations,
692 -- notably of the return vector!
693 setState $ state `stateIncUsageEval` state_at_end_return
694 ; return (virtSp_from_env, value_returned) }
697 -- ----------------------------------------------------------------------------
698 -- Combinators for emitting code
703 whenC :: Bool -> Code -> Code
704 whenC True code = code
707 stmtC :: CmmStmt -> Code
708 stmtC stmt = emitCgStmt (CgStmt stmt)
710 labelC :: BlockId -> Code
711 labelC id = emitCgStmt (CgLabel id)
713 newLabelC :: FCode BlockId
714 newLabelC = do { u <- newUnique
715 ; return $ BlockId u }
717 checkedAbsC :: CmmStmt -> Code
718 -- Emit code, eliminating no-ops
719 checkedAbsC stmt = emitStmts (if isNopStmt stmt then nilOL
722 stmtsC :: [CmmStmt] -> Code
723 stmtsC stmts = emitStmts (toOL stmts)
725 -- Emit code; no no-op checking
726 emitStmts :: CmmStmts -> Code
727 emitStmts stmts = emitCgStmts (fmap CgStmt stmts)
729 -- forkLabelledCode is for emitting a chunk of code with a label, outside
730 -- of the current instruction stream.
731 forkLabelledCode :: Code -> FCode BlockId
732 forkLabelledCode code = getCgStmts code >>= forkCgStmts
734 emitCgStmt :: CgStmt -> Code
736 = do { state <- getState
737 ; setState $ state { cgs_stmts = cgs_stmts state `snocOL` stmt }
740 emitData :: Section -> [CmmStatic] -> Code
742 = do { state <- getState
743 ; setState $ state { cgs_tops = cgs_tops state `snocOL` data_block } }
745 data_block = CmmData sect lits
747 emitProc :: CmmInfo -> CLabel -> CmmFormals -> [CmmBasicBlock] -> Code
748 emitProc info lbl args blocks
749 = do { let proc_block = CmmProc info lbl args (ListGraph blocks)
751 ; setState $ state { cgs_tops = cgs_tops state `snocOL` proc_block } }
753 emitSimpleProc :: CLabel -> Code -> Code
754 -- Emit a procedure whose body is the specified code; no info table
755 emitSimpleProc lbl code
756 = do { stmts <- getCgStmts code
757 ; blks <- cgStmtsToBlocks stmts
758 ; emitProc (CmmInfo Nothing Nothing CmmNonInfoTable) lbl [] blks }
760 getCmm :: Code -> FCode Cmm
761 -- Get all the CmmTops (there should be no stmts)
762 -- Return a single Cmm which may be split from other Cmms by
763 -- object splitting (at a later stage)
765 = do { state1 <- getState
766 ; ((), state2) <- withState code (state1 { cgs_tops = nilOL })
767 ; setState $ state2 { cgs_tops = cgs_tops state1 }
768 ; return (Cmm (fromOL (cgs_tops state2)))
771 -- ----------------------------------------------------------------------------
774 -- These functions deal in terms of CgStmts, which is an abstract type
775 -- representing the code in the current proc.
778 -- emit CgStmts into the current instruction stream
779 emitCgStmts :: CgStmts -> Code
781 = do { state <- getState
782 ; setState $ state { cgs_stmts = cgs_stmts state `appOL` stmts } }
784 -- emit CgStmts outside the current instruction stream, and return a label
785 forkCgStmts :: CgStmts -> FCode BlockId
787 = do { id <- newLabelC
788 ; emitCgStmt (CgFork id stmts)
792 -- turn CgStmts into [CmmBasicBlock], for making a new proc.
793 cgStmtsToBlocks :: CgStmts -> FCode [CmmBasicBlock]
794 cgStmtsToBlocks stmts
795 = do { id <- newLabelC
796 ; return (flattenCgStmts id stmts)
799 -- collect the code emitted by an FCode computation
800 getCgStmts' :: FCode a -> FCode (a, CgStmts)
802 = do { state1 <- getState
803 ; (a, state2) <- withState fcode (state1 { cgs_stmts = nilOL })
804 ; setState $ state2 { cgs_stmts = cgs_stmts state1 }
805 ; return (a, cgs_stmts state2) }
807 getCgStmts :: FCode a -> FCode CgStmts
808 getCgStmts fcode = do { (_,stmts) <- getCgStmts' fcode; return stmts }
810 -- Simple ways to construct CgStmts:
814 oneCgStmt :: CmmStmt -> CgStmts
815 oneCgStmt stmt = unitOL (CgStmt stmt)
817 consCgStmt :: CmmStmt -> CgStmts -> CgStmts
818 consCgStmt stmt stmts = CgStmt stmt `consOL` stmts
820 -- ----------------------------------------------------------------------------
821 -- Get the current module name
823 getModuleName :: FCode Module
824 getModuleName = do { info <- getInfoDown; return (cgd_mod info) }
826 -- ----------------------------------------------------------------------------
827 -- Get/set the end-of-block info
829 setEndOfBlockInfo :: EndOfBlockInfo -> Code -> Code
830 setEndOfBlockInfo eob_info code = do
832 withInfoDown code (info {cgd_eob = eob_info})
834 getEndOfBlockInfo :: FCode EndOfBlockInfo
835 getEndOfBlockInfo = do
837 return (cgd_eob info)
839 -- ----------------------------------------------------------------------------
840 -- Get/set the current SRT label
842 -- There is just one SRT for each top level binding; all the nested
843 -- bindings use sub-sections of this SRT. The label is passed down to
844 -- the nested bindings via the monad.
846 getSRTLabel :: FCode CLabel -- Used only by cgPanic
847 getSRTLabel = do info <- getInfoDown
848 return (cgd_srt_lbl info)
850 setSRTLabel :: CLabel -> FCode a -> FCode a
851 setSRTLabel srt_lbl code
852 = do info <- getInfoDown
853 withInfoDown code (info { cgd_srt_lbl = srt_lbl})
856 getSRT = do info <- getInfoDown
857 return (cgd_srt info)
859 setSRT :: SRT -> FCode a -> FCode a
861 = do info <- getInfoDown
862 withInfoDown code (info { cgd_srt = srt})
864 -- ----------------------------------------------------------------------------
865 -- Get/set the current ticky counter label
867 getTickyCtrLabel :: FCode CLabel
868 getTickyCtrLabel = do
870 return (cgd_ticky info)
872 setTickyCtrLabel :: CLabel -> Code -> Code
873 setTickyCtrLabel ticky code = do
875 withInfoDown code (info {cgd_ticky = ticky})