Haskell “例外情况”;序列化的GlobalReference具有类型PointerType“;

Haskell “例外情况”;序列化的GlobalReference具有类型PointerType“;,haskell,llvm,Haskell,Llvm,作为一名CS学生,我被要求使用LLVM用函数式语言编写编译器。我选择了Haskell,尽管事实上我对它非常陌生,而且我不理解源代码示例中的所有内容,这就是为什么我的代码可能看起来不体面的原因 在真正开始我的项目之前,我想玩一下LLVM的Haskell绑定,并创建一个调用另一个函数的函数,该函数返回作为参数传递的两个整数之间的差 运行代码时,出现以下异常: EncodeException“序列化的GlobalReference具有类型PointerType{pointerReferent=Func

作为一名CS学生,我被要求使用LLVM用函数式语言编写编译器。我选择了Haskell,尽管事实上我对它非常陌生,而且我不理解源代码示例中的所有内容,这就是为什么我的代码可能看起来不体面的原因

在真正开始我的项目之前,我想玩一下LLVM的Haskell绑定,并创建一个调用另一个函数的函数,该函数返回作为参数传递的两个整数之间的差

运行代码时,出现以下异常:

EncodeException“序列化的GlobalReference具有类型PointerType{pointerReferent=FunctionType{resultType=IntegerType{typeBits=32},argumentTypes=[IntegerType{typeBits=32},IntegerType{typeBits=32}],isVarArg=False},PointerAddressSpace=AddrSpace 0},但应具有类型IntegerType{typeBits=32}”

我真的不明白我的代码出了什么问题

这是我完整的源代码,谢谢你的帮助

{-# LANGUAGE OverloadedStrings #-}

module Main where


import Control.Monad.Except

-- Pretty Printer

import LLVM.Pretty (ppllvm, ppll)

import LLVM.Module
import LLVM.Context
import LLVM.Module

-- AST
import LLVM.AST
import qualified LLVM.AST as AST
import LLVM.AST.Global
import LLVM.AST.CallingConvention
import LLVM.AST.Constant as Kokai
import qualified Data.ByteString.Char8 as B


int :: Type
int = IntegerType 32

defAdd :: Definition
defAdd = GlobalDefinition functionDefaults
  { name = Name "subbing"
  , parameters =
      ( [ Parameter int (Name "a") []
        , Parameter int (Name "b") [] ]
      , False )
  , returnType = int
  , basicBlocks = [block]
  }
  where

block :: BasicBlock
block = BasicBlock
    (Name "entry")
    [ Name "result" :=
      AST.Sub False
      False
      (LocalReference int (Name "a"))
      (LocalReference int (Name "b"))
      [] ]
    (Do $ Ret (Just (LocalReference int (Name "result"))) [])

foo :: Definition
foo = GlobalDefinition functionDefaults
  { name = Name "random_func"
  , parameters = ([], False)
  , returnType = int
  , basicBlocks = [calli]
  }
  where
calli :: BasicBlock
calli = BasicBlock
            (Name "entry")
            [Name "res" :=
             Call
                Nothing
                C
                []
                (Right $ ConstantOperand $ GlobalReference int "subbing")
                [(ConstantOperand $ Int 32 10, []),
                (ConstantOperand $ Int 32 7, [])]
                []
                []
                    ]
            (Do $ Ret (Just (ConstantOperand $ Int 32 10)) [])
            --(Do $ Ret (Just (LocalReference int (Name "res"))) [])

astModule :: AST.Module
astModule = defaultModule
  { moduleName = "my-module"
  , moduleDefinitions = [defAdd, foo]
  }

nimoft :: IO B.ByteString
nimoft = withContext $ \context ->
    withModuleFromAST context astModule $ \m -> do
      llstr <- moduleLLVMAssembly m
      B.putStrLn llstr
      return llstr

main :: IO ()
main = do
  lol <- nimoft
  Prelude.putStrLn "hello"
{-#语言重载字符串}
模块主要在哪里
进口管制。单子除外
--漂亮的打印机
导入LLVM.Pretty(ppllvm,ppll)
导入LLVM.Module
导入LLVM.Context
导入LLVM.Module
--AST
导入LLVM.AST
将符合条件的LLVM.AST导入为AST
导入LLVM.AST.Global
导入LLVM.AST.CallingConvention
将LLVM.AST.Constant作为Kokai导入
将限定数据.ByteString.Char8作为B导入
int::Type
int=整数类型32
定义
defAdd=全局定义函数默认值
{name=name“subbing”
,参数=
([参数int(名称“a”)[]
,参数int(名称“b”)[]
,错)
,returnType=int
,基本块=[block]
}
哪里
块::基本块
块=基本块
(名称“条目”)
[名称“结果”:=
AST.Sub错误
假的
(LocalReference int(名称“a”))
(LocalReference int(名称“b”))
[] ]
(Do$Ret(Just(LocalReference int(Name“result”))[]))
foo::定义
foo=全局定义函数默认值
{name=name“random_func”
,参数=([],False)
,returnType=int
,basicBlocks=[calli]
}
哪里
基本块
calli=基本块
(名称“条目”)
[名称“res”:=
呼叫
没有什么
C
[]
(右$ConstantOperand$GlobalReference int“subbing”)
[(君士坦丁堡元整32 10,[]),
(君士坦丁堡币32.7,[])]
[]
[]
]
(Do$Ret(Just(ConstantOperand$Int 32 10))[])
--(Do$Ret(Just(LocalReference int(Name“res”))[]))
astModule::AST.Module
astModule=defaultModule
{moduleName=“我的模块”
,moduleDefinitions=[defAdd,foo]
}
nimoft::IO B.ByteString
NiSoft=withContext$\context->
withModuleFromAST上下文astModule$\m->do

llstr刚刚发现错误,我用以下命令更改了呼叫行:

(Right $ ConstantOperand $ GlobalReference (PointerType (FunctionType int [int, int] False) (A.AddrSpace 0)) (Name "subbing"))
我遇到了同样的问题——以下是一些我发现有用的额外资源:(1)关于函数调用的讨论,(2)函数调用示例: