2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 % $Id: CgMonad.lhs,v 1.36 2002/12/11 15:36:26 simonmar Exp $
6 \section[CgMonad]{The code generation monad}
8 See the beginning of the top-level @CodeGen@ module, to see how this
9 monadic stuff fits into the Big Picture.
16 initC, thenC, thenFC, listCs, listFCs, mapCs, mapFCs,
17 returnFC, fixC, absC, nopC, getAbsC,
19 forkClosureBody, forkStatics, forkAlts, forkEval,
20 forkEvalHelp, forkAbsC,
24 setEndOfBlockInfo, getEndOfBlockInfo,
26 setSRTLabel, getSRTLabel, getSRTInfo,
27 setTickyCtrLabel, getTickyCtrLabel,
29 StackUsage, Slot(..), HeapUsage,
31 profCtrC, profCtrAbsC, ldvEnter,
33 costCentresC, moduleName,
35 Sequel(..), -- ToDo: unabstract?
38 -- ideally we wouldn't export these, but some other modules access internal state
39 getState, setState, getInfoDown,
41 -- more localised access to monad state
43 getBinds, setBinds, getStaticBinds,
45 -- out of general friendliness, we also export ...
46 CgInfoDownwards(..), CgState(..), -- non-abstract
50 #include "HsVersions.h"
52 import {-# SOURCE #-} CgBindery ( CgBindings, nukeVolatileBinds )
53 import {-# SOURCE #-} CgUsages ( getSpRelOffset )
57 import StgSyn ( SRT(..) )
58 import AbsCUtils ( mkAbsCStmts )
59 import CmdLineOpts ( opt_SccProfilingOn, opt_DoTickyProfiling )
60 import Module ( Module )
61 import DataCon ( ConTag )
64 import PrimRep ( PrimRep(..) )
68 infixr 9 `thenC` -- Right-associative!
72 %************************************************************************
74 \subsection[CgMonad-environment]{Stuff for manipulating environments}
76 %************************************************************************
78 This monadery has some information that it only passes {\em
79 downwards}, as well as some ``state'' which is modified as we go
83 data CgInfoDownwards -- information only passed *downwards* by the monad
85 CompilationInfo -- COMPLETELY STATIC info about this compilation
86 -- (e.g., what flags were passed to the compiler)
88 CgBindings -- [Id -> info] : static environment
90 CLabel -- label of the current SRT
92 CLabel -- current destination for ticky counts
94 EndOfBlockInfo -- Info for stuff to do at end of basic block:
99 Module -- the module name
103 AbstractC -- code accumulated so far
104 CgBindings -- [Id -> info] : *local* bindings environment
105 -- Bindings for top-level things are given in the info-down part
109 @EndOfBlockInfo@ tells what to do at the end of this block of code or,
110 if the expression is a @case@, what to do at the end of each
116 VirtualSpOffset -- Args Sp: trim the stack to this point at a
117 -- return; push arguments starting just
118 -- above this point on a tail call.
120 -- This is therefore the stk ptr as seen
121 -- by a case alternative.
124 initEobInfo = EndOfBlockInfo 0 (OnStack 0)
127 Any addressing modes inside @Sequel@ must be ``robust,'' in the sense
128 that it must survive stack pointer adjustments at the end of the
134 VirtualSpOffset -- Continuation is on the stack, at the
135 -- specified location
140 CAddrMode -- Jump to this; if the continuation is for a vectored
141 -- case this might be the label of a return
142 -- vector Guaranteed to be a non-volatile
143 -- addressing mode (I think)
146 Bool -- True <=> polymorphic, push a SEQ frame too
149 type SemiTaggingStuff
150 = Maybe -- Maybe[1] we don't have any semi-tagging stuff...
151 ([(ConTag, JoinDetails)], -- Alternatives
152 Maybe (Maybe Id, JoinDetails) -- Default (but Maybe[2] we don't have one)
153 -- Maybe[3] the default is a
154 -- bind-default (Just b); that is,
155 -- it expects a ptr to the thing
156 -- in Node, bound to b
160 = (AbstractC, CLabel) -- Code to load regs from heap object + profiling macros,
161 -- and join point label
163 -- The abstract C is executed only from a successful semitagging
164 -- venture, when a case has looked at a variable, found that it's
165 -- evaluated, and wants to load up the contents and go to the join
169 -- The OnStack case of sequelToAmode delivers an Amode which is only
170 -- valid just before the final control transfer, because it assumes
171 -- that Sp is pointing to the top word of the return address. This
172 -- seems unclean but there you go.
174 -- sequelToAmode returns an amode which refers to an info table. The info
175 -- table will always be of the RET(_VEC)?_(BIG|SMALL) kind. We're careful
176 -- not to handle real code pointers, just in case we're compiling for
177 -- an unregisterised/untailcallish architecture, where info pointers and
178 -- code pointers aren't the same.
180 sequelToAmode :: Sequel -> FCode CAddrMode
182 sequelToAmode (OnStack virt_sp_offset)
183 = getSpRelOffset virt_sp_offset `thenFC` \ sp_rel ->
184 returnFC (CVal sp_rel RetRep)
186 sequelToAmode UpdateCode = returnFC (CLbl mkUpdInfoLabel RetRep)
188 sequelToAmode (CaseAlts amode _ False) = returnFC amode
189 sequelToAmode (CaseAlts amode _ True) = returnFC (CLbl mkSeqInfoLabel RetRep)
191 type CgStksAndHeapUsage -- stacks and heap usage information
192 = (StackUsage, HeapUsage)
194 data Slot = Free | NonPointer
203 (Int, -- virtSp: Virtual offset of topmost allocated slot
204 Int, -- frameSp: End of the current stack frame
205 [(Int,Slot)], -- free: List of free slots, in increasing order
206 Int, -- realSp: Virtual offset of real stack pointer
207 Int) -- hwSp: Highest value ever taken by virtSp
210 (HeapOffset, -- virtHp: Virtual offset of highest-allocated word
211 HeapOffset) -- realHp: Virtual offset of real heap ptr
214 NB: absolutely every one of the above Ints is really
215 a VirtualOffset of some description (the code generator
216 works entirely in terms of VirtualOffsets).
221 initialStateC = MkCgState AbsCNop emptyVarEnv initUsage
223 initUsage :: CgStksAndHeapUsage
224 initUsage = ((0,0,[],0,0), (0,0))
227 @stateIncUsage@$~e_1~e_2$ incorporates in $e_1$ the stack and heap high water
228 marks found in $e_2$.
231 stateIncUsage :: CgState -> CgState -> CgState
233 stateIncUsage (MkCgState abs_c bs ((v,t,f,r,h1),(vH1,rH1)))
234 (MkCgState _ _ ((_,_,_,_,h2),(vH2, _)))
237 ((v,t,f,r,h1 `max` h2),
238 (vH1 `max` vH2, rH1))
241 %************************************************************************
243 \subsection[CgMonad-basics]{Basic code-generation monad magic}
245 %************************************************************************
248 newtype FCode a = FCode (CgInfoDownwards -> CgState -> (a, CgState))
251 instance Monad FCode where
256 {-# INLINE thenFC #-}
257 {-# INLINE returnFC #-}
259 The Abstract~C is not in the environment so as to improve strictness.
262 initC :: CompilationInfo -> Code -> AbstractC
264 initC cg_info (FCode code)
265 = case (code (MkCgInfoDown
267 emptyVarEnv -- (error "initC: statics")
272 ((),MkCgState abc _ _) -> abc
274 returnFC :: a -> FCode a
275 returnFC val = FCode (\info_down state -> (val, state))
279 thenC :: Code -> FCode a -> FCode a
280 thenC (FCode m) (FCode k) =
281 FCode (\info_down state -> let (_,new_state) = m info_down state in
282 k info_down new_state)
284 listCs :: [Code] -> Code
285 listCs [] = return ()
290 mapCs :: (a -> Code) -> [a] -> Code
295 thenFC :: FCode a -> (a -> FCode c) -> FCode c
296 thenFC (FCode m) k = FCode (
299 (m_result, new_state) = m info_down state
300 (FCode kcode) = k m_result
302 kcode info_down new_state
305 listFCs :: [FCode a] -> FCode [a]
308 mapFCs :: (a -> FCode b) -> [a] -> FCode [b]
312 And the knot-tying combinator:
314 fixC :: (a -> FCode a) -> FCode a
319 result@(v,_) = fc info_down state
326 Operators for getting and setting the state and "info_down".
327 To maximise encapsulation, code should try to only get and set the
328 state it actually uses.
331 getState :: FCode CgState
332 getState = FCode $ \info_down state -> (state,state)
334 setState :: CgState -> FCode ()
335 setState state = FCode $ \info_down _ -> ((),state)
337 getUsage :: FCode CgStksAndHeapUsage
339 MkCgState absC binds usage <- getState
342 setUsage :: CgStksAndHeapUsage -> FCode ()
343 setUsage newusage = do
344 MkCgState absC binds usage <- getState
345 setState $ MkCgState absC binds newusage
347 getBinds :: FCode CgBindings
349 MkCgState absC binds usage <- getState
352 setBinds :: CgBindings -> FCode ()
353 setBinds newbinds = do
354 MkCgState absC binds usage <- getState
355 setState $ MkCgState absC newbinds usage
357 getStaticBinds :: FCode CgBindings
359 (MkCgInfoDown _ static_binds _ _ _) <- getInfoDown
362 withState :: FCode a -> CgState -> FCode (a,CgState)
363 withState (FCode fcode) newstate = FCode $ \info_down state ->
364 let (retval, state2) = fcode info_down newstate in ((retval,state2), state)
366 getInfoDown :: FCode CgInfoDownwards
367 getInfoDown = FCode $ \info_down state -> (info_down,state)
369 withInfoDown :: FCode a -> CgInfoDownwards -> FCode a
370 withInfoDown (FCode fcode) info_down = FCode $ \_ state -> fcode info_down state
372 doFCode :: FCode a -> CgInfoDownwards -> CgState -> (a,CgState)
373 doFCode (FCode fcode) info_down state = fcode info_down state
377 @forkClosureBody@ takes a code, $c$, and compiles it in a completely
378 fresh environment, except that:
379 - compilation info and statics are passed in unchanged.
380 The current environment is passed on completely unaltered, except that
381 abstract C from the fork is incorporated.
383 @forkAbsC@ takes a code and compiles it in the current environment,
384 returning the abstract C thus constructed. The current environment
385 is passed on completely unchanged. It is pretty similar to @getAbsC@,
386 except that the latter does affect the environment. ToDo: combine?
388 @forkStatics@ $fc$ compiles $fc$ in an environment whose statics come
389 from the current bindings, but which is otherwise freshly initialised.
390 The Abstract~C returned is attached to the current state, but the
391 bindings and usage information is otherwise unchanged.
394 forkClosureBody :: Code -> Code
396 forkClosureBody (FCode code) = do
397 (MkCgInfoDown cg_info statics srt ticky _) <- getInfoDown
398 (MkCgState absC_in binds un_usage) <- getState
399 let body_info_down = MkCgInfoDown cg_info statics srt ticky initEobInfo
400 let ((),fork_state) = code body_info_down initialStateC
401 let MkCgState absC_fork _ _ = fork_state
402 setState $ MkCgState (AbsCStmts absC_in absC_fork) binds un_usage
404 forkStatics :: FCode a -> FCode a
406 forkStatics (FCode fcode) = FCode (
407 \(MkCgInfoDown cg_info _ srt ticky _)
408 (MkCgState absC_in statics un_usage)
411 (result, state) = fcode rhs_info_down initialStateC
412 MkCgState absC_fork _ _ = state -- Don't merge these this line with the one
413 -- above or it becomes too strict!
414 rhs_info_down = MkCgInfoDown cg_info statics srt ticky initEobInfo
416 (result, MkCgState (AbsCStmts absC_in absC_fork) statics un_usage)
419 forkAbsC :: Code -> FCode AbstractC
420 forkAbsC (FCode code) =
422 info_down <- getInfoDown
423 (MkCgState absC1 bs usage) <- getState
424 let ((),MkCgState absC2 _ ((_, _, _, _,h2), _)) = code info_down (MkCgState AbsCNop bs usage)
425 let ((v, t, f, r, h1), heap_usage) = usage
426 let new_usage = ((v, t, f, r, h1 `max` h2), heap_usage)
427 setState $ MkCgState absC1 bs new_usage
431 @forkAlts@ $bs~d$ takes fcodes $bs$ for the branches of a @case@, and
432 an fcode for the default case $d$, and compiles each in the current
433 environment. The current environment is passed on unmodified, except
435 - the worst stack high-water mark is incorporated
436 - the virtual Hp is moved on to the worst virtual Hp for the branches
439 forkAlts :: [FCode a] -> FCode b -> FCode ([a],b)
441 forkAlts branch_fcodes (FCode deflt_fcode) =
443 info_down <- getInfoDown
445 let compile (FCode fc) = fc info_down in_state
446 let (branch_results, branch_out_states) = unzip (map compile branch_fcodes)
447 let (deflt_result, deflt_out_state) = deflt_fcode info_down in_state
448 setState $ foldl stateIncUsage in_state (deflt_out_state:branch_out_states)
449 -- NB foldl. in_state is the *left* argument to stateIncUsage
450 return (branch_results, deflt_result)
454 @forkEval@ takes two blocks of code.
456 - The first meddles with the environment to set it up as expected by
457 the alternatives of a @case@ which does an eval (or gc-possible primop).
458 - The second block is the code for the alternatives.
459 (plus info for semi-tagging purposes)
461 @forkEval@ picks up the virtual stack pointer and returns a suitable
462 @EndOfBlockInfo@ for the caller to use, together with whatever value
463 is returned by the second block.
465 It uses @initEnvForAlternatives@ to initialise the environment, and
466 @stateIncUsageAlt@ to incorporate usage; the latter ignores the heap
470 forkEval :: EndOfBlockInfo -- For the body
471 -> Code -- Code to set environment
472 -> FCode Sequel -- Semi-tagging info to store
473 -> FCode EndOfBlockInfo -- The new end of block info
475 forkEval body_eob_info env_code body_code
476 = forkEvalHelp body_eob_info env_code body_code `thenFC` \ (v, sequel) ->
477 returnFC (EndOfBlockInfo v sequel)
479 forkEvalHelp :: EndOfBlockInfo -- For the body
480 -> Code -- Code to set environment
481 -> FCode a -- The code to do after the eval
483 a) -- Result of the FCode
485 forkEvalHelp body_eob_info env_code body_code =
487 info_down@(MkCgInfoDown cg_info statics srt ticky _) <- getInfoDown
489 let info_down_for_body = MkCgInfoDown cg_info statics srt ticky body_eob_info
490 let (_,MkCgState _ binds ((v,t,f,_,_),_)) =
491 doFCode env_code info_down_for_body state
492 let state_for_body = MkCgState AbsCNop
493 (nukeVolatileBinds binds)
495 let (value_returned, state_at_end_return) =
496 doFCode body_code info_down_for_body state_for_body
497 setState $ state `stateIncUsageEval` state_at_end_return
498 return (v,value_returned)
500 stateIncUsageEval :: CgState -> CgState -> CgState
501 stateIncUsageEval (MkCgState absC1 bs ((v,t,f,r,h1),heap_usage))
502 (MkCgState absC2 _ ((_,_,_,_,h2), _))
503 = MkCgState (absC1 `mkAbsCStmts` absC2)
504 -- The AbsC coming back should consist only of nested declarations,
505 -- notably of the return vector!
507 ((v,t,f,r,h1 `max` h2), heap_usage)
508 -- We don't max the heap high-watermark because stateIncUsageEval is
509 -- used only in forkEval, which in turn is only used for blocks of code
510 -- which do their own heap-check.
513 %************************************************************************
515 \subsection[CgMonad-spitting-AbstractC]{Spitting out @AbstractC@}
517 %************************************************************************
519 @nopC@ is the no-op for the @Code@ monad; it adds no Abstract~C to the
520 environment; @absC@ glues @ab_C@ onto the Abstract~C collected so far.
525 absC :: AbstractC -> Code
527 state@(MkCgState absC binds usage) <- getState
528 setState $ MkCgState (mkAbsCStmts absC more_absC) binds usage
531 These two are just like @absC@, except they examine the compilation
532 info (whether SCC profiling or profiling-ctrs going) and possibly emit
536 costCentresC :: FastString -> [CAddrMode] -> Code
537 costCentresC macro args
538 | opt_SccProfilingOn = absC (CCallProfCCMacro macro args)
541 profCtrC :: FastString -> [CAddrMode] -> Code
543 | opt_DoTickyProfiling = absC (CCallProfCtrMacro macro args)
546 profCtrAbsC :: FastString -> [CAddrMode] -> AbstractC
547 profCtrAbsC macro args
548 | opt_DoTickyProfiling = CCallProfCtrMacro macro args
549 | otherwise = AbsCNop
552 ldvEnter = costCentresC FSLIT("LDV_ENTER") [CReg node]
554 {- Try to avoid adding too many special compilation strategies here.
555 It's better to modify the header files as necessary for particular
556 targets, so that we can get away with as few variants of .hc files
561 @getAbsC@ compiles the code in the current environment, and returns
562 the abstract C thus constructed (leaving the abstract C being carried
563 around in the state untouched). @getAbsC@ does not generate any
564 in-line Abstract~C itself, but the environment it returns is that
565 obtained from the compilation.
568 getAbsC :: Code -> FCode AbstractC
570 MkCgState absC binds usage <- getState
571 ((),MkCgState absC2 binds2 usage2) <- withState code (MkCgState AbsCNop binds usage)
572 setState $ MkCgState absC binds2 usage2
577 moduleName :: FCode Module
579 (MkCgInfoDown (MkCompInfo mod_name) _ _ _ _) <- getInfoDown
584 setEndOfBlockInfo :: EndOfBlockInfo -> Code -> Code
585 setEndOfBlockInfo eob_info code = do
586 (MkCgInfoDown c_info statics srt ticky _) <- getInfoDown
587 withInfoDown code (MkCgInfoDown c_info statics srt ticky eob_info)
589 getEndOfBlockInfo :: FCode EndOfBlockInfo
590 getEndOfBlockInfo = do
591 (MkCgInfoDown c_info statics _ _ eob_info) <- getInfoDown
595 There is just one SRT for each top level binding; all the nested
596 bindings use sub-sections of this SRT. The label is passed down to
597 the nested bindings via the monad.
600 getSRTInfo :: SRT -> FCode C_SRT
601 getSRTInfo NoSRT = return NoC_SRT
602 getSRTInfo (SRT off len) = do srt_lbl <- getSRTLabel
603 return (C_SRT srt_lbl off len)
605 getSRTLabel :: FCode CLabel -- Used only by cgPanic
606 getSRTLabel = do MkCgInfoDown _ _ srt_lbl _ _ <- getInfoDown
609 setSRTLabel :: CLabel -> Code -> Code
610 setSRTLabel srt_lbl code
611 = do MkCgInfoDown c_info statics _ ticky eob_info <- getInfoDown
612 withInfoDown code (MkCgInfoDown c_info statics srt_lbl ticky eob_info)
616 getTickyCtrLabel :: FCode CLabel
617 getTickyCtrLabel = do
618 (MkCgInfoDown _ _ _ ticky _) <- getInfoDown
621 setTickyCtrLabel :: CLabel -> Code -> Code
622 setTickyCtrLabel ticky code = do
623 (MkCgInfoDown c_info statics srt _ eob_info) <- getInfoDown
624 withInfoDown code (MkCgInfoDown c_info statics srt ticky eob_info)