在使用Haskell的LLVM绑定时,模块中找不到'main'函数

3

我正在尝试使用Haskell的LLVM绑定来创建一个非常简单的“hello world”独立应用程序。我的想法是,当我运行Haskell应用程序时,它将输出一些字节码,这些字节码可以反过来运行,并输出“hello world!”

-- hellofunc prints out "hello world"
hellofunc :: CodeGenModule (Function (IO ()))


_main :: (Function (IO ())) -> CodeGenModule (Function (IO ()))
_main func = createNamedFunction ExternalLinkage "main" $ do
        call func
        ret ()

main = writeCodeGenModule "hello.bc" (liftA _main hellofunc)

当我运行这个程序时,我看到以下错误:
'main' function not found in module.

我正在使用createNamedFunction明确创建main函数。 我错过了什么吗?


您可以尝试运行 "llvm-dis hello.bc" 来查看 "@main" 的问题所在。 - arrowd
它被写成_fun2。我正在尝试理解我是否错误地使用了createNamedFunction或该函数中存在错误。 - Vlad the Impala
顺便提一下,以下划线开头的标识符(如“_main”)表示未使用的标识符。如果它们没有被使用,GHC不会发出警告。因此,最好只在实际上未使用的函数上标记为未使用。另请参见:https://ghc.haskell.org/trac/ghc/ticket/4959 - Lemming
1个回答

3
问题出在使用liftAfmap而不是=<<。当使用类型签名时,这变得更加明显(构建中断)。在使用liftA的情况下,在CodeGenModule中_main没有被评估,因此它不会出现在输出文件中。
修改writeCodeGenModule以接受CodeGenModule ()而不是CodeGenModule a可能是合理的。这会使LLVM JIT的用户稍微复杂化一些,但有助于防止出现此类错误。
import Control.Applicative
import LLVM.Core
import LLVM.ExecutionEngine
import LLVM.Util.File

--
-- hellofunc prints out "hello world"
hellofunc :: CodeGenModule (Function (IO ()))
hellofunc = createNamedFunction ExternalLinkage "hello" $ do
  ret ()

_main :: (Function (IO ())) -> CodeGenModule (Function (IO ()))
_main func = createNamedFunction ExternalLinkage "main" $ do
  call func
  ret ()

generateModuleBind :: CodeGenModule (Function (IO ()))
generateModuleBind = _main =<< hellofunc

-- Couldn't match expected type `Function (IO ())'
--             with actual type `CodeGenModule (Function (IO ()))'
--
-- this is the desired type:
-- generateModuleLiftA :: CodeGenModule (Function (IO ()))
--
-- but this is the actual type:
generateModuleLiftA :: CodeGenModule (CodeGenModule (Function (IO ())))
generateModuleLiftA = liftA _main hellofunc

main :: IO ()
main = writeCodeGenModule "hello.bc" generateModuleBind

2
啊,谢谢。对于其他寻找答案的人来说,简而言之:我将 CodeGenModule 用作应用程序而不是单子。我用中缀 =<< 替换了 liftA,现在一切都正常工作。 - Vlad the Impala

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接