编写llvm字节代码

发布于 2024-10-26 06:05:11 字数 985 浏览 2 评论 0原文

我刚刚发现LLVM,对它还不太了解。我一直在使用浏览器中的llvm进行尝试。我可以看到我编写的任何 C 代码都会转换为 LLVM 字节代码,然后再转换为本机代码。该页面显示字节代码的文本表示。例如,对于以下 C 代码:

int array[] = { 1, 2, 3};

int foo(int X) {
  return array[X];
}

它显示以下字节代码:

target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"
target triple = "x86_64-linux-gnu"

@array = global [3 x i32] [i32 1, i32 2, i32 3]   ; <[3 x i32]*> [#uses=1]

define i32 @foo(i32 %X) nounwind readonly {
entry:
  %0 = sext i32 %X to i64                         ; <i64> [#uses=1]
  %1 = getelementptr inbounds [3 x i32]* @array, i64 0, i64 %0 ; <i32*> [#uses=1]
  %2 = load i32* %1, align 4                      ; <i32> [#uses=1]
   ret i32 %2
}

我的问题是:我可以编写字节代码并将其交给 llvm 汇编器以转换为本机代码,从而完全跳过编写 C 代码的第一步吗?如果是,我该怎么做?有没有人给我指点一下?

I have just discovered LLVM and don't know much about it yet. I have been trying it out using llvm in browser. I can see that any C code I write is converted to LLVM byte code which is then converted to native code. The page shows a textual representation of the byte code. For example for the following C code:

int array[] = { 1, 2, 3};

int foo(int X) {
  return array[X];
}

It shows the following byte code:

target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"
target triple = "x86_64-linux-gnu"

@array = global [3 x i32] [i32 1, i32 2, i32 3]   ; <[3 x i32]*> [#uses=1]

define i32 @foo(i32 %X) nounwind readonly {
entry:
  %0 = sext i32 %X to i64                         ; <i64> [#uses=1]
  %1 = getelementptr inbounds [3 x i32]* @array, i64 0, i64 %0 ; <i32*> [#uses=1]
  %2 = load i32* %1, align 4                      ; <i32> [#uses=1]
   ret i32 %2
}

My question is: Can I write the byte code and give it to the llvm assembler to convert to native code skipping the first step of writing C code altogether? If yes, how do I do it? Does any one have any pointers for me?

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

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

发布评论

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

评论(2

赴月观长安 2024-11-02 06:05:11

LLVM IR 语言的一个非常重要的功能(和设计目标)是它的 3 路表示:

  • 您可以在此处看到的文本表示
  • 字节码表示(或二进制形式)
  • 内存中表示

所有这 3 种表示实际上是完全可以互换的。凡是可以用其中一个来表达的东西,都不能用另外两个来表达。

因此,只要符合语法,确实可以自己写IR。但这是毫无意义的,除非用作练习来让自己习惯这种格式,无论是更好地阅读(和诊断)IR 还是生成自己的编译器:)

One very important feature (and design goal) of the LLVM IR language is its 3-way representation:

  • The textual representation you can see here
  • The bytecode representation (or binary form)
  • The in-memory representation

All 3 are indeed completely interchangeable. Nothing that can be expressed in one cannot be expressed in the 2 others as well.

Therefore, as long as you conform to the syntax, you can indeed write the IR yourself. It is rather pointless though, unless used as an exercise to accustom yourself with the format, whether to be better at reading (and diagnosing) the IR or to produce your own compiler :)

抠脚大汉 2024-11-02 06:05:11

是的,当然可以。首先,您可以手写LLVM IR。所有工具,如 llc(将为您生成本机代码)和 opt(LLVM IR => LLVM IR 优化器)都接受 LLVM IR 的文本表示作为输入。

Yes, surely you can. First, you can write LLVM IR by hand. All tools like llc (which will generate a native code for you) and opt (LLVM IR => LLVM IR optimizer) accept textual representation of LLVM IR as input.

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