使用 LLVM 编写 JIT 时,如何重用 C 操作码实现?

发布于 2024-07-13 06:11:02 字数 381 浏览 5 评论 0原文

在 llvm 教程和示例中,编译器通过进行这样的调用来输出 LLVM IR,

return Builder.CreateAdd(L, R, "addtmp");

但许多解释器都是这样编写的:

switch (opcode) {
     case ADD:
             result = L + R;
             break;
     ...

How would you extract every code snippets to make a JIT with LLVM without without re-implement every opcode in LLVM红外?

In the llvm tutorials and examples, the compiler outputs LLVM IR by making calls like this

return Builder.CreateAdd(L, R, "addtmp");

but many interpreters are written like this:

switch (opcode) {
     case ADD:
             result = L + R;
             break;
     ...

How would you extract each of these code snippets to make a JIT with LLVM without having to re-implement each opcode in LLVM IR?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贩梦商人 2024-07-20 06:11:03

好的,首先将所有代码片段重构为自己的函数。 所以你的代码会变成:

void addOpcode(uint32_t *result, uint32_t L, uint32_t R) {
    *result = L + R;
}

switch (opcode) {
    case ADD:
            addOpcode(&result, L, R);
            break;
     ....

好的,完成此操作后你的解释器应该仍然运行。 现在获取所有新函数并将它们放入自己的文件中。 现在使用 llvm-gcc 或 clang 编译该文件,而不是使用 "cpp" 后端 (-march -cpp)。 这将生成实例化编译单元字节码的 C++ 代码。 您可以指定选项以将其限制为特定功能等。您可能想使用“-cppgen module”。

现在,返回解释器循环,将对生成的 C++ 代码的调用粘合在一起,而不是直接执行原始代码,然后将其传递给一些优化器和本机代码生成器。 Gratz 谈 JIT ;-) 您可以在几个 LLVM 项目中看到这样的示例,例如 llvm-lua

Okay, first take all of your code snippets and refactor them into their own functions. So your code goes to:

void addOpcode(uint32_t *result, uint32_t L, uint32_t R) {
    *result = L + R;
}

switch (opcode) {
    case ADD:
            addOpcode(&result, L, R);
            break;
     ....

Okay, so after doing this your interpreter should still run. Now take all the new functions and place them in their own file. Now compile that file using either llvm-gcc or clang, and instead of generating native code compile it using the "cpp" backend (-march -cpp). That will generate C++ code that instantiates the byte code for the compilation unit. You can specify options to limit it to specific functions, etc. You probably want to use "-cppgen module" .

Now back your interpreter loop glue together calls to the generated C++ code instead of directly executing the original code, then pass it to some optimizers and a native codegenerator. Gratz on the JIT ;-) You can see an example of this in a couple of LLVM projects, like the vm_ops in llvm-lua.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文