正在提取 Haskell 来源的 STG
Extracting STG of Haskell Source
我正在尝试通过 Outputable
将 Haskell 源的 STG 表示提取为 String
,但看起来 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 的正确方法,所以如果有更好的替代方法,我想听听它们。
编辑:
对于以下程序,STG 翻译似乎成功,其中 bar b = b + 3
更改为 bar b = 3
:
module FooBar where
foo a = a + a
bar :: Int -> Int
bar b = 3
事实上,乍一看,如果诱导核心 Haskell 不强制执行原始操作,事情似乎会起作用。例如 bar b = 3 + 9
失败。
非常感谢 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
我不确定从 Target
恢复 ModSummary
(以及 ModuleName
)的最佳方法是什么,但我确实记得它是第一个元素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
欢迎提出改进建议:)
编辑:我发现了一些反例——ModuleGraph
的 head
并不总是目标。我当前的解决方法是查看 ModuleGraph
中是否有任何 ModSummary
包含与初始源文件位置匹配的位置。
我正在尝试通过 Outputable
将 Haskell 源的 STG 表示提取为 String
,但看起来 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 的正确方法,所以如果有更好的替代方法,我想听听它们。
编辑:
对于以下程序,STG 翻译似乎成功,其中 bar b = b + 3
更改为 bar b = 3
:
module FooBar where
foo a = a + a
bar :: Int -> Int
bar b = 3
事实上,乍一看,如果诱导核心 Haskell 不强制执行原始操作,事情似乎会起作用。例如 bar b = 3 + 9
失败。
非常感谢 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
我不确定从 Target
恢复 ModSummary
(以及 ModuleName
)的最佳方法是什么,但我确实记得它是第一个元素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
欢迎提出改进建议:)
编辑:我发现了一些反例——ModuleGraph
的 head
并不总是目标。我当前的解决方法是查看 ModuleGraph
中是否有任何 ModSummary
包含与初始源文件位置匹配的位置。