2 % (c) The AQUA Project, Glasgow University, 1993-1998
4 \section[CoreMonad]{The core pipeline monad}
7 {-# LANGUAGE UndecidableInstances #-}
13 -- ** Reading from the monad
14 getHscEnv, getAnnEnv, getRuleBase, getModule,
15 getDynFlags, getOrigNameCache,
17 -- ** Writing to the monad
20 -- ** Lifting into the monad
21 liftIO, liftIOWithCount,
22 liftIO1, liftIO2, liftIO3, liftIO4,
24 -- ** Dealing with annotations
25 findAnnotations, addAnnotation,
28 putMsg, putMsgS, errorMsg, errorMsgS,
29 fatalErrorMsg, fatalErrorMsgS,
30 debugTraceMsg, debugTraceMsgS,
42 import PrelNames ( iNTERACTIVE )
44 import Module ( Module )
45 import DynFlags ( DynFlags, DynFlag )
46 import SimplMonad ( SimplCount, plusSimplCount, zeroSimplCount )
47 import Rules ( RuleBase )
51 import IOEnv hiding ( liftIO, failM, failWithM )
52 import qualified IOEnv ( liftIO )
53 import TcEnv ( tcLookupGlobal )
54 import TcRnMonad ( TcM, initTc )
57 import qualified ErrUtils as Err
66 import Control.Applicative
68 import Prelude hiding ( read )
71 import {-# SOURCE #-} TcSplice ( lookupThName_maybe )
72 import qualified Language.Haskell.TH as TH
76 \subsection{Monad and carried data structure definitions}
79 data CoreState = CoreState {
80 cs_uniq_supply :: UniqSupply,
84 data CoreReader = CoreReader {
86 cr_rule_base :: RuleBase,
90 data CoreWriter = CoreWriter {
91 cw_simpl_count :: SimplCount
94 emptyWriter :: DynFlags -> CoreWriter
95 emptyWriter dflags = CoreWriter {
96 cw_simpl_count = zeroSimplCount dflags
99 plusWriter :: CoreWriter -> CoreWriter -> CoreWriter
100 plusWriter w1 w2 = CoreWriter {
101 cw_simpl_count = (cw_simpl_count w1) `plusSimplCount` (cw_simpl_count w2)
104 type CoreIOEnv = IOEnv CoreReader
106 -- | The monad used by Core-to-Core passes to access common state, register simplification
107 -- statistics and so on
108 newtype CoreM a = CoreM { unCoreM :: CoreState -> CoreIOEnv (a, CoreState, CoreWriter) }
110 instance Functor CoreM where
115 instance Monad CoreM where
116 return x = CoreM (\s -> nop s x)
117 mx >>= f = CoreM $ \s -> do
118 (x, s', w1) <- unCoreM mx s
119 (y, s'', w2) <- unCoreM (f x) s'
120 return (y, s'', w1 `plusWriter` w2)
122 instance Applicative CoreM where
126 -- For use if the user has imported Control.Monad.Error from MTL
127 -- Requires UndecidableInstances
128 instance MonadPlus IO => MonadPlus CoreM where
129 mzero = CoreM (const mzero)
130 m `mplus` n = CoreM (\rs -> unCoreM m rs `mplus` unCoreM n rs)
132 instance MonadUnique CoreM where
133 getUniqueSupplyM = do
134 us <- getS cs_uniq_supply
135 let (us1, us2) = splitUniqSupply us
136 modifyS (\s -> s { cs_uniq_supply = us2 })
145 -> IO (a, SimplCount)
146 runCoreM hsc_env ann_env rule_base us mod m =
147 liftM extract $ runIOEnv reader $ unCoreM m state
149 reader = CoreReader {
150 cr_hsc_env = hsc_env,
151 cr_rule_base = rule_base,
159 extract :: (a, CoreState, CoreWriter) -> (a, SimplCount)
160 extract (value, _, writer) = (value, cw_simpl_count writer)
164 \subsection{Core combinators, not exported}
168 nop :: CoreState -> a -> CoreIOEnv (a, CoreState, CoreWriter)
171 return (x, s, emptyWriter $ (hsc_dflags . cr_hsc_env) r)
173 read :: (CoreReader -> a) -> CoreM a
174 read f = CoreM (\s -> getEnv >>= (\r -> nop s (f r)))
176 getS :: (CoreState -> a) -> CoreM a
177 getS f = CoreM (\s -> nop s (f s))
179 modifyS :: (CoreState -> CoreState) -> CoreM ()
180 modifyS f = CoreM (\s -> nop (f s) ())
182 write :: CoreWriter -> CoreM ()
183 write w = CoreM (\s -> return ((), s, w))
187 \subsection{Lifting IO into the monad}
191 -- | Lift an 'IOEnv' operation into 'CoreM'
192 liftIOEnv :: CoreIOEnv a -> CoreM a
193 liftIOEnv mx = CoreM (\s -> mx >>= (\x -> nop s x))
195 instance MonadIO CoreM where
196 liftIO = liftIOEnv . IOEnv.liftIO
198 -- | Lift an 'IO' operation into 'CoreM' while consuming its 'SimplCount'
199 liftIOWithCount :: IO (SimplCount, a) -> CoreM a
200 liftIOWithCount what = liftIO what >>= (\(count, x) -> addSimplCount count >> return x)
204 \subsection{Reader, writer and state accessors}
208 getHscEnv :: CoreM HscEnv
209 getHscEnv = read cr_hsc_env
211 getAnnEnv :: CoreM AnnEnv
212 getAnnEnv = getS cs_ann_env
214 getRuleBase :: CoreM RuleBase
215 getRuleBase = read cr_rule_base
217 getModule :: CoreM Module
218 getModule = read cr_module
220 addSimplCount :: SimplCount -> CoreM ()
221 addSimplCount count = write (CoreWriter { cw_simpl_count = count })
223 -- Convenience accessors for useful fields of HscEnv
225 getDynFlags :: CoreM DynFlags
226 getDynFlags = fmap hsc_dflags getHscEnv
228 -- | The original name cache is the current mapping from 'Module' and
229 -- 'OccName' to a compiler-wide unique 'Name'
230 getOrigNameCache :: CoreM OrigNameCache
231 getOrigNameCache = do
232 nameCacheRef <- fmap hsc_NC getHscEnv
233 liftIO $ fmap nsNames $ readIORef nameCacheRef
237 \subsection{Dealing with annotations}
241 -- | Find all the annotations we currently know about for the given target. Note that no
242 -- annotations will be returned if we haven't loaded information about the particular target
243 -- you are inquiring about: by default, only those modules that have been imported by the
244 -- program being compiled will have been loaded in this way.
246 -- To load the information from additional modules, you can use the functions 'DynamicLoading.forceLoadModuleInterfaces'
247 -- and 'DynamicLoading.forceLoadNameModuleInterface', but be aware that doing this indiscriminantly
248 -- will impose a performance penalty.
250 -- If no deserialization function is supplied, only transient annotations will be returned.
251 findAnnotations :: Typeable a => ([Word8] -> a) -> CoreAnnTarget -> CoreM [a]
252 findAnnotations deserialize target = do
254 return (findAnns deserialize ann_env target)
256 addAnnotation :: Typeable a => (a -> [Word8]) -> CoreAnnTarget -> a -> CoreM ()
257 addAnnotation serialize target what = addAnnotationToEnv $ Annotation { ann_target = target, ann_value = toSerialized serialize what }
259 addAnnotationToEnv :: Annotation -> CoreM ()
260 addAnnotationToEnv annotation = modifyS (\state -> state { cs_ann_env = extendAnnEnvList (cs_ann_env state) [annotation] })
264 \subsection{Direct screen output}
268 msg :: (DynFlags -> SDoc -> IO ()) -> SDoc -> CoreM ()
270 dflags <- getDynFlags
271 liftIO $ how dflags doc
273 -- | Output a String message to the screen
274 putMsgS :: String -> CoreM ()
275 putMsgS = putMsg . text
277 -- | Output a message to the screen
278 putMsg :: SDoc -> CoreM ()
279 putMsg = msg Err.putMsg
281 -- | Output a string error to the screen
282 errorMsgS :: String -> CoreM ()
283 errorMsgS = errorMsg . text
285 -- | Output an error to the screen
286 errorMsg :: SDoc -> CoreM ()
287 errorMsg = msg Err.errorMsg
289 -- | Output a fatal string error to the screen. Note this does not by itself cause the compiler to die
290 fatalErrorMsgS :: String -> CoreM ()
291 fatalErrorMsgS = fatalErrorMsg . text
293 -- | Output a fatal error to the screen. Note this does not by itself cause the compiler to die
294 fatalErrorMsg :: SDoc -> CoreM ()
295 fatalErrorMsg = msg Err.fatalErrorMsg
297 -- | Output a string debugging message at verbosity level of @-v@ or higher
298 debugTraceMsgS :: String -> CoreM ()
299 debugTraceMsgS = debugTraceMsg . text
301 -- | Outputs a debugging message at verbosity level of @-v@ or higher
302 debugTraceMsg :: SDoc -> CoreM ()
303 debugTraceMsg = msg (flip Err.debugTraceMsg 3)
305 -- | Show some labelled 'SDoc' if a particular flag is set or at a verbosity level of @-v -ddump-most@ or higher
306 dumpIfSet_dyn :: DynFlag -> String -> SDoc -> CoreM ()
307 dumpIfSet_dyn flag str = msg (\dflags -> Err.dumpIfSet_dyn dflags flag str)
313 initTcForLookup :: HscEnv -> TcM a -> IO a
314 initTcForLookup hsc_env = liftM (expectJust "initTcInteractive" . snd) . initTc hsc_env HsSrcFile False iNTERACTIVE
318 \subsection{Finding TyThings}
322 instance MonadThings CoreM where
323 lookupThing name = do
325 liftIO $ initTcForLookup hsc_env (tcLookupGlobal name)
329 \subsection{Template Haskell interoperability}
333 -- | Attempt to convert a Template Haskell name to one that GHC can
334 -- understand. Original TH names such as those you get when you use
335 -- the @'foo@ syntax will be translated to their equivalent GHC name
336 -- exactly. Qualified or unqualifed TH names will be dynamically bound
337 -- to names in the module being compiled, if possible. Exact TH names
338 -- will be bound to the name they represent, exactly.
339 thNameToGhcName :: TH.Name -> CoreM (Maybe Name)
340 thNameToGhcName th_name = do
342 liftIO $ initTcForLookup hsc_env (lookupThName_maybe th_name)