在llvmlite中链接C [英] Link C in llvmlite

查看:111
本文介绍了在llvmlite中链接C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Python编写编译器,并使用llvmlite生成中间LLVM IR. Lexer和解析器已经完成,现在我正在执行代码生成.编译器将是动态的且类型微弱的,因此我将需要在运行时做一些事情,例如分配.为此,我已经在C中实现了一些功能,现在我想使用llvmlite中的builder.call调用这些功能.

Im writing an compiler in Python, using llvmlite to generate intermediate LLVM IR. Lexer and parser are finished, now im doing code generation. The compiler will be dynamic and weakly typed, so i will need to do somethings at runtime, like allocation. For this, i've already implemented some functions in C, and now i want to call these functions using builder.call from llvmlite.

我没有找到文档或执行此操作的示例.

I have not found documentation or examples of how to do this.

此函数只是一个简单的示例,实际函数要大得多.

This function its just an simple example, the real ones are much larger.

C:

int some_function(int a)
{
     return a + 4;
}

Python:

...

    main_ty = ir.FunctionType(ir.IntType(32), [])
    func = ir.Function(module, main_ty, 'main')
    block = func.append_basic_block('entry')
    builder = ir.IRBuilder(block)

    # I want to do something like this...

    ret = builder.call(some_function, [ir.Constant(ir.IntType(32), 34)]);

...

我可以使用llvmlite构建器直接编写函数,但是用C语言可以更快,更干净,更轻松地进行.欢迎您提供任何帮助!

I could write the functions directly using llvmlite builders, but will be much quick, cleaner and easy do it in C. Any help are welcome!

推荐答案

您可以导入包含运行时的动态库.

You could import a dynamic library containing the runtime.

llvmlite.binding.load_library_permanently("runtime.so")

然后,您可以简单地生成普通的函数调用.

Then you could simply generate normal function calls.

这篇关于在llvmlite中链接C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆