声明在llvmlite中返回void的函数时出现AssertionError

时间:2019-02-26 11:02:29

标签: python llvm-ir llvmlite

我正在尝试创建一个在llvm-ir中返回void的函数,但是创建该函数会导致AssertionError

import llvmlite.ir as ir

int32 = ir.IntType(32)
m = ir.Module('demo')

main_ty = ir.FunctionType(int32, [])
main_fn = ir.Function(m, main_ty, "main")

print(str(m))

上面的代码可以正常工作,因为返回类型为int32,并提供以下输出,这与预期的一样。

; ModuleID = "demo"
target triple = "unknown-unknown-unknown"
target datalayout = ""

declare i32 @"main"() 

但是当我将返回类型从int32更改为VoidType时,会引发AssertionError。

m = ir.Module('demo')
main_ty = ir.FunctionType(ir.VoidType, [])
main_fn = ir.Function(m, main_ty, "main")
print(str(m))

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-2-298d109233fd> in <module>
      1 m = ir.Module('demo')
      2 main_ty = ir.FunctionType(ir.VoidType, [])
----> 3 main_fn = ir.Function(m, main_ty, "main")
      4 print(str(m))

~/.local/lib/python3.6/site-packages/llvmlite/ir/values.py in __init__(self, module, ftype, name)
    593         self.args = tuple([Argument(self, t)
    594                            for t in ftype.args])
--> 595         self.return_value = ReturnValue(self, ftype.return_type)
    596         self.parent.add_global(self)
    597         self.calling_convention = ''

~/.local/lib/python3.6/site-packages/llvmlite/ir/values.py in __init__(self, parent, typ, name)
    718 class _BaseArgument(NamedValue):
    719     def __init__(self, parent, typ, name=''):
--> 720         assert isinstance(typ, types.Type)
    721         super(_BaseArgument, self).__init__(parent, typ, name=name)
    722         self.parent = parent

AssertionError: 

有人可以帮助我在这里我想念的吗?

1 个答案:

答案 0 :(得分:2)

VoidType是一个类,就像IntType一样。您仍然需要应用它(使用零参数)来创建实例:

main_ty = ir.FunctionType(ir.VoidType(), [])
//                                   ^^