haskell - 在 Haskell 中使用 llvm 绑定(bind)时,在模块中找不到“main”函数

标签 haskell llvm

我正在尝试使用 Haskell 的 LLVM 绑定(bind)来创建一个非常简单的“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 函数。我错过了什么?

最佳答案

问题出在liftA的使用上或fmap而不是=<< 。当使用类型签名时,它变得更加明显(构建中断)。在 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

关于haskell - 在 Haskell 中使用 llvm 绑定(bind)时,在模块中找不到“main”函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9427535/

相关文章:

haskell - 类型为 Num ([Char] -> t) => t 的奇怪 Haskell 表达式

haskell - 函数列表中的函数组成!

c++ - 为什么我的 LLVM JIT 实现会出现段错误?

x86 - 为什么 0xE1 0x4F 在 LLVM 和 NDISASM 中反汇编为不同的指令?

haskell - Haskell 类型类中的记录选择器

algorithm - 分析运行时间,Big O

haskell - 查找作为类型类实例的所有类型

c++ - 可以将 llvm::FunctionType 转换为 C/C++ 原始函数指针吗?

xcode - iOS 6.0 Apple LLVM 编译器 4.1 错误

haskell - 编码异常 "The serialized GlobalReference has type PointerType"