<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
</head>
<body text="#000000" bgcolor="#FFFFFF">
Hi all,<br>
<br>
I'm currently trying to understand how STG works, and my goal right
now is to be able to inspect StgBinding values. I've written a short
program, based on the wiki article <a
href="https://wiki.haskell.org/GHC/As_a_library">GHC/As a library</a>,
like below:<br>
<br>
<tt>-- Code.hs --<br>
module Lib (printSTG, dumpSTG) where<br>
<br>
import Control.Monad.Ghc (lift, runGhcT)<br>
import CorePrep (corePrepPgm)<br>
import CoreToStg (coreToStg)<br>
import DynFlags (defaultFatalMessager,
defaultFlushOut)<br>
import GHC hiding (runGhcT)<br>
import GHC.Paths (libdir)<br>
import HscMain (newHscEnv)<br>
import HscTypes (hsc_dflags, typeEnvTyCons)<br>
import Outputable (interppSP, showSDoc)<br>
import System.Environment (getArgs)<br>
import StgSyn (StgBinding)<br>
</tt><br>
<tt>dumpSTG :: String -> IO [StgBinding]</tt><tt><br>
</tt><tt>dumpSTG fileName = defaultErrorHandler defaultFatalMessager
defaultFlushOut $</tt><tt><br>
</tt><tt> runGhcT (Just libdir) $ do</tt><tt><br>
</tt><tt> sess <- getSession</tt><tt><br>
</tt><tt> let dflags = hsc_dflags sess</tt><tt><br>
</tt><tt> setSessionDynFlags dflags</tt><tt><br>
</tt><tt> cm <- compileToCoreModule fileName</tt><tt><br>
</tt><tt> let md = cm_module cm</tt><tt><br>
</tt><tt> ml <- fmap ms_location $ getModSummary $
moduleName md</tt><tt><br>
</tt><tt> lift $ do</tt><tt><br>
</tt><tt> cp <- corePrepPgm sess ml <$> cm_binds
<*> (typeEnvTyCons . cm_types) $ cm</tt><tt><br>
</tt><tt> coreToStg dflags md cp</tt><br>
<br>
<tt>printSTG =</tt><tt><br>
</tt><tt> getArgs >>= \x -> case x of</tt><tt><br>
</tt><tt> [] -> putStrLn "usage: Main <file.hs>"</tt><tt><br>
</tt><tt> (fileName:_) -> do</tt><tt><br>
</tt><tt> bindings <- dumpSTG fileName</tt><tt><br>
</tt><tt> str <- runGhcT (Just libdir) $ do</tt><tt><br>
</tt><tt> dflags <- getSessionDynFlags</tt><tt><br>
</tt><tt> return $ showSDoc dflags $ interppSP bindings</tt><tt><br>
</tt><tt> putStrLn str</tt><br>
<br>
This works when I compile it to print out a Haskell file in STG
format.<br>
<br>
However, <b>my question is</b>, is there a way so that I can call <tt>dumpSTG</tt>
to get back that list of StgBindings, <b>from within Ghci</b>?
Whenever I do so within Ghci, i.e.<br>
<br>
<tt>ghci> :l code.hs</tt><tt><br>
</tt><tt>ghci> bindings <- dumpSTG "fileToTest.hs"</tt><tt><br>
</tt><tt><interactive>: panic! (the 'impossible' happened)</tt><tt><br>
</tt><tt> (GHC version 7.10.3 for x86_64-unknown-linux):</tt><tt><br>
</tt><tt> no package state yet: call GHC.setSessionDynFlags</tt><br>
<br>
I know that it's saying I need to set the session dyn flags, but I'm
not sure how that can get done from within Ghci, especially because
that needs to get done within the GhcMonad. Part of my problem may
stem from having a inadequate understanding of monads. Any help or
pointers would be greatly appreciated!<br>
<br>
Thank you very much,<br>
Mike<br>
<br>
</body>
</html>