提取Haskell源的STG

Ant*_*Xue 6 haskell ghc

我试图将Haskell源的STG表示提取为Stringvia Outputable,但看起来像是coreToStgArgs在使用以下转储进行混乱:

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# 3

Please report this as a GHC bug:  http://www.haskell.org/ghc/reportabug
Run Code Online (Sandbox Code Playgroud)

这是FooBar.hs我要提取的文件:

module FooBar where

foo a = a + a

bar :: Int -> Int
bar b = b + 3
Run Code Online (Sandbox Code Playgroud)

这是test.hs我使用的来源:

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
Run Code Online (Sandbox Code Playgroud)

看起来像我之前几年的人遇到了类似的问题:

https://ghc.haskell.org/trac/ghc/ticket/7159

但是,我不知道自那以后发生了什么.我也不确定这是否是提取任意Haskell源的STG的正确方法,所以如果有更好的替代方法可行,我想听听它们.

编辑: STG翻译在以下程序中显示成功,其中bar b = b + 3更改为bar b = 3:

module FooBar where

foo a = a + a

bar :: Int -> Int
bar b = 3
Run Code Online (Sandbox Code Playgroud)

事实上,乍一看,如果诱导的Core Haskell不强制执行原始操作,事情就会起作用.例如bar b = 3 + 9失败.

Ant*_*Xue 1

非常感谢melpomene指出了我在文档中遗漏的一些内容。

这是有效的修改后的源代码test.hs

import CorePrep
import CoreToStg
import GHC
import GHC.Paths
import GhcMonad
import HscTypes
import Outputable
import StgSyn
import System.IO

mkSTG :: FilePath -> FilePath -> IO [StgBinding]
mkSTG proj src = runGhc (Just libdir) $ do
        env    <- getSession
        dflags <- getSessionDynFlags
        setSessionDynFlags (dflags {importPaths = [proj]})
        target <- guessTarget src Nothing
        setTargets [target]
        load LoadAllTargets

        mod_graph <- getModuleGraph
        let mod_sum = head mod_graph  -- This is bad practice
        pmod <- parseModule mod_sum
        tmod <- typecheckModule pmod
        dmod <- desugarModule tmod
        let guts  = coreModule dmod
        let loc   = ms_location mod_sum
        let binds = mg_binds guts
        let tcs   = mg_tcs guts
        prep <- liftIO $ corePrepPgm env loc binds tcs
        liftIO $ coreToStg dflags (mg_module guts) prep

mkIOStr :: (Outputable a) => a -> IO String
mkIOStr obj = do
    dflags <- runGhc (Just libdir) getSessionDynFlags
    let ppr_str = showPpr dflags obj
    return ppr_str

main :: IO ()
main = do
    let proj = "/home/celery/Desktop/hue"
    let src  = proj ++ "/FooBar.hs"
    res <- mkIOStr =<< mkSTG proj src
    putStrLn res
Run Code Online (Sandbox Code Playgroud)

我不确定从 a恢复 a ModSummary(以及因此的) 的最佳方法是什么,但我隐约记得它是 的第一个元素,它被定义为。ModuleNameTargetModuleGraphtype ModuleGraph = [ModSummary]

GHC 7 和 8 之间的类型签名corePrepPgm也不同:

https://downloads.haskell.org/~ghc/7.10.1/docs/html/libraries/ghc-7.10.1/CorePrep.html

https://downloads.haskell.org/~ghc/latest/docs/html/libraries/ghc-8.0.1/CorePrep.html

欢迎提出改进建议:)

编辑:我发现了反例的实例 - ahead并不ModuleGraph总是目标。我当前的解决方法是查看ModSummary其中是否ModuleGraph包含与初始源文件位置相匹配的位置。