2 -- The above warning supression flag is a temporary kludge.
3 -- While working on this module you are encouraged to remove it and fix
4 -- any warnings in the module. See
5 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
8 -- -----------------------------------------------------------------------------
10 -- (c) The University of Glasgow 1993-2004
12 -- The native code generator's monad.
14 -- -----------------------------------------------------------------------------
17 NatM_State(..), mkNatM_State,
19 NatM, -- instance Monad
20 initNat, addImportNat, getUniqueNat,
21 mapAccumLNat, setDeltaNat, getDeltaNat,
22 getBlockIdNat, getNewLabelNat, getNewRegNat, getNewRegPairNat,
23 getPicBaseMaybeNat, getPicBaseNat, getDynFlagsNat
26 #include "HsVersions.h"
28 import Cmm ( BlockId(..) )
29 import CLabel ( CLabel, mkAsmTempLabel )
31 import MachOp ( MachRep )
33 import Unique ( Unique )
36 data NatM_State = NatM_State {
37 natm_us :: UniqSupply,
39 natm_imports :: [(CLabel)],
40 natm_pic :: Maybe Reg,
41 natm_dflags :: DynFlags
44 newtype NatM result = NatM (NatM_State -> (result, NatM_State))
48 mkNatM_State :: UniqSupply -> Int -> DynFlags -> NatM_State
49 mkNatM_State us delta dflags = NatM_State us delta [] Nothing dflags
51 initNat :: NatM_State -> NatM a -> (a, NatM_State)
52 initNat init_st m = case unNat m init_st of { (r,st) -> (r,st) }
54 instance Monad NatM where
58 thenNat :: NatM a -> (a -> NatM b) -> NatM b
60 = NatM $ \st -> case unNat expr st of
61 (result, st') -> unNat (cont result) st'
63 returnNat :: a -> NatM a
64 returnNat result = NatM $ \st -> (result, st)
66 mapAccumLNat :: (acc -> x -> NatM (acc, y))
73 mapAccumLNat f b (x:xs)
74 = do (b__2, x__2) <- f b x
75 (b__3, xs__2) <- mapAccumLNat f b__2 xs
76 return (b__3, x__2:xs__2)
78 getUniqueNat :: NatM Unique
79 getUniqueNat = NatM $ \ (NatM_State us delta imports pic dflags) ->
80 case splitUniqSupply us of
81 (us1,us2) -> (uniqFromSupply us1, (NatM_State us2 delta imports pic dflags))
84 getDynFlagsNat :: NatM DynFlags
85 getDynFlagsNat = NatM $ \ (NatM_State us delta imports pic dflags) ->
86 (dflags, (NatM_State us delta imports pic dflags))
88 getDeltaNat :: NatM Int
89 getDeltaNat = NatM $ \ st -> (natm_delta st, st)
91 setDeltaNat :: Int -> NatM ()
92 setDeltaNat delta = NatM $ \ (NatM_State us _ imports pic dflags) ->
93 ((), NatM_State us delta imports pic dflags)
95 addImportNat :: CLabel -> NatM ()
96 addImportNat imp = NatM $ \ (NatM_State us delta imports pic dflags) ->
97 ((), NatM_State us delta (imp:imports) pic dflags)
99 getBlockIdNat :: NatM BlockId
100 getBlockIdNat = do u <- getUniqueNat; return (BlockId u)
102 getNewLabelNat :: NatM CLabel
103 getNewLabelNat = do u <- getUniqueNat; return (mkAsmTempLabel u)
105 getNewRegNat :: MachRep -> NatM Reg
106 getNewRegNat rep = do u <- getUniqueNat; return (mkVReg u rep)
108 getNewRegPairNat :: MachRep -> NatM (Reg,Reg)
109 getNewRegPairNat rep = do
111 let lo = mkVReg u rep; hi = getHiVRegFromLo lo
114 getPicBaseMaybeNat :: NatM (Maybe Reg)
115 getPicBaseMaybeNat = NatM (\state -> (natm_pic state, state))
117 getPicBaseNat :: MachRep -> NatM Reg
118 getPicBaseNat rep = do
119 mbPicBase <- getPicBaseMaybeNat
121 Just picBase -> return picBase
123 reg <- getNewRegNat rep
124 NatM (\state -> (reg, state { natm_pic = Just reg }))