提取Haskell源的STG

时间:2017-06-24 09:34:08

标签: haskell ghc

我试图通过String将Haskell源的STG表示提取为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

以下是我要提取的文件FooBar.hs

module FooBar where

foo a = a + a

bar :: Int -> Int
bar b = b + 3

以下是我使用的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

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

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

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

修改 对于bar b = b + 3更改为bar b = 3的以下计划,STG翻译似乎成功:

module FooBar where

foo a = a + a

bar :: Int -> Int
bar b = 3

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

1 个答案:

答案 0 :(得分: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

我不确定从ModSummary恢复ModuleName(以及Target)的最佳方法是什么,但我依稀记得它是第一个元素ModuleGraph,定义为type ModuleGraph = [ModSummary]

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

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

欢迎提出改进建议:)

编辑:我已经找到了反例的实例 - head的{​​{1}}并不总是目标。我目前的解决方法是查看ModuleGraph中的任何ModSummary是否包含与初始源文件位置匹配的位置。