I am trying to extract the STG representation of a Haskell source as Stringthrough Outputable, but it looks like it coreToStgArgsis panning with the following dump:
user@machine ~/Desktop/hue $ runhaskell test.hs
[foo :: forall a. Num a => a -> a
[GblId, Arity=2, Caf=NoCafRefs, Str=DmdType] =
\r srt:SRT:[] [$dNum a1] + $dNum a1 a1;,
bar :: Int -> Int
[GblId,test.hs: test.hs: panic! (the 'impossible' happened)
(GHC version 7.10.3 for x86_64-unknown-linux):
coreToStgArgs I
Please report this as a GHC bug: http:
Here is the file FooBar.hsI want to extract:
module FooBar where
foo a = a + a
bar :: Int -> Int
bar b = b + 3
Here is the source test.hsI used:
import CoreToStg
import GHC
import GHC.Paths
import Outputable
import StgSyn
mkDynFlags :: IO DynFlags
mkDynFlags = runGhc (Just libdir) getSessionDynFlags
mkSTG :: FilePath -> FilePath -> IO [StgBinding]
mkSTG proj src = do
dflags <- mkDynFlags
ghc_core <- runGhc (Just libdir) $ do
setSessionDynFlags (dflags {importPaths = [proj]})
compileToCoreSimplified src
-- compileToCoreModule src
coreToStg dflags (cm_module ghc_core) (cm_binds ghc_core)
mkIOStr :: (Outputable a) => a -> IO String
mkIOStr obj = do
dflags <- mkDynFlags
let ppr_str = showPpr dflags obj
return ppr_str
main :: IO ()
main = do
let proj = "/home/user/Desktop/hue"
let src = proj ++ "/FooBar.hs"
res <- mkIOStr =<< mkSTG proj src
putStrLn res
It seems like someone a few years before me ran into a similar problem:
https://ghc.haskell.org/trac/ghc/ticket/7159
However, I have no idea what has happened since then. I'm also not sure if this is the right way to extract the STG of an arbitrary Haskell source, so if there are better alternatives that work, I'd love to hear about them.
EDIT:
The STG translation seems successful for the next program, where it is bar b = b + 3changed to bar b = 3:
module FooBar where
foo a = a + a
bar :: Int -> Int
bar b = 3
, , , , Core Haskell . , bar b = 3 + 9 .