简体   繁体   English

是否可以直接使用 LLVM-assembly?

[英]Is it possible to use LLVM-assembly directly?

I have read some webpages and articles about llvm and I am quite interested in this project.我已经阅读了一些关于 llvm 的网页和文章,我对这个项目非常感兴趣。 (Maybe to learn something about compiler writing without the need to struggle with the complicated points of x86). (也许可以学习一些有关编译器编写的知识,而无需纠结于 x86 的复杂点)。

There are pages that describe how to write llvm assembly and how to assemble it, but I did not find anything on what kind of environment is needed to actually execute these.有一些页面描述了如何编写 llvm 程序集以及如何组装它,但我没有找到关于实际执行这些需要什么样的环境的任何信息。 I know that I could run llvm-gcc on my files to get an object file that is executable in a C-context.我知道我可以在我的文件上运行 llvm-gcc 来获取在 C 上下文中可执行的目标文件。 But in the case that I don't want to use the C runtime environmen ( libc.so and friends), what is needed to run llvm code?但是在我不想使用C运行时环境( libc.so和朋友)的情况下,运行llvm代码需要什么? Is there any documentation on that?有没有这方面的文件?

There appears to be an LLVM assembler .似乎有一个LLVM 汇编程序

llvm-as is the LLVM assembler. llvm-as 是 LLVM 汇编器。 It reads a file containing human-readable LLVM assembly language, translates it to LLVM bitcode, and writes the result into a file or to standard output.它读取包含人类可读的 LLVM 汇编语言的文件,将其转换为 LLVM 位码,并将结果写入文件或标准输出。

Quick setup: (For llvm 3.4.0 .ll files on windows)快速设置:(对于 Windows 上的 llvm 3.4.0 .ll 文件)

advanced text editor from https://notepad-plus-plus.org/来自https://notepad-plus-plus.org/ 的高级文本编辑器

llvm binaries from https://github.com/CRogers/LLVM-Windows-Binaries来自https://github.com/CRogers/LLVM-Windows-Binaries 的llvm 二进制文件

hello.ll as "UTF-8 without BOM" (This code is in llvm 3.4.0 format): hello.ll 为“无 BOM 的 UTF-8”(此代码采用 llvm 3.4.0 格式):

@msg = internal constant [13 x i8] c"Hello World!\00"
declare i32 @puts(i8*)
define i32 @main() {
    call i32 @puts(i8* getelementptr inbounds ([13 x i8]* @msg, i32 0, i32 0))
    ret i32 0
}

In command prompt:在命令提示符中:

lli hello.ll

Quick setup: (For llvm 3.8.0 .ll files on windows)快速设置:(对于 Windows 上的 llvm 3.8.0 .ll 文件)

advanced text editor from https://notepad-plus-plus.org/来自https://notepad-plus-plus.org/ 的高级文本编辑器

clang binaries from: http://llvm.org/releases/download.html#3.8.0 clang 二进制文件来自: http : //llvm.org/releases/download.html#3.8.0

hello.ll as "UTF-8 without BOM" (This code is in llvm 3.8.0 format): hello.ll 为“无 BOM 的 UTF-8”(此代码采用 llvm 3.8.0 格式):

@msg = internal constant [13 x i8] c"Hello World!\00"
declare i32 @puts(i8*)
define i32 @main() {
    call i32 @puts(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @msg, i32 0, i32 0))
    ret i32 0
}

In command prompt:在命令提示符中:

clang hello.ll -o hello.exe
hello.exe

Or as a single command:或者作为单个命令:

clang hello.ll -o hello.exe & hello.exe

Errors about char16_t, u16String, etc means clang needs: -fms-compatibility-version=19关于 char16_t、u16String 等的错误意味着 clang 需要:-fms-compatibility-version=19

The static compiler, which accepts LLVM Assembly:接受 LLVM 程序集的静态编译器:

http://llvm.org/docs/CommandGuide/llc.html http://llvm.org/docs/CommandGuide/llc.html

The LLVM Assembly Language Reference: LLVM 汇编语言参考:

http://llvm.org/docs/LangRef.html http://llvm.org/docs/LangRef.html

LLVM 11.1, on Archlinux, did not accept the code from the answer above. Archlinux 上的 LLVM 11.1 不接受上述答案中的代码。 This is from the current LLVM IR docs :这是来自当前的LLVM IR 文档

cat > hello.ll <<EOF
@.str = private unnamed_addr constant [13 x i8] c"hello world\0A\00"
declare i32 @puts(i8* nocapture) nounwind
define i32 @main() {   ; i32()*
  %cast210 = getelementptr [13 x i8], [13 x i8]* @.str, i64 0, i64 0
  call i32 @puts(i8* %cast210)
  ret i32 0
}
!0 = !{i32 42, null, !"string"}
!foo = !{!0}
EOF
lli hello.ll

If you start lli alone, it does not show a prompt, but accepts input.如果单独启动lli ,它不会显示提示,但会接受输入。 This input is only evaluated after Ctrl-D (EOF), though.但是,此输入仅在Ctrl-D (EOF) 之后进行评估。

I came here, because I wanted to find a REPL.我来这里是因为我想找到一个 REPL。 No luck.没运气。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM