编写LLVM后端编译的代码时,架构是否重要?

bob*_*bob 9 linux compiler-construction llvm

我的问题实际上比标题更通用:

在编写最终将编译为LLVM中间代码,然后从那里到机器语言的代码时,架构在什么时候起作用?

  1. 假设我正在编写Rust(它使用LLVM作为后端).我是否能自动将Rust代码编译为LLVM可以定位的每个体系结构(假设该机器上有可以运行它的操作系统)?

  2. 或者可能是Rust标准库还没有"ARM兼容",所以即使LLVM以它为目标,我也无法编译到ARM?

  3. 如果我不使用任何标准库,我的整个程序只是一个立即返回的程序怎么办?可能是这样的情况,即使没有任何库,Rust(或你有什么)也无法编译到ARM(或者你有什么),即使LLVM以它为目标?

  4. 如果以上所有示例都编译得很好,那么我需要做些什么才能让我的代码在一个架构打破而不能编译到某个架构?

相同品种的奖金问题:

  1. 假设标准库使用OS系统调用(肯定会这样).在进行系统调用时,您是否需要关心架构?或者OS(例如Linux)是否也抽象了架构呢?

谢谢.

Mat*_*ers 6

长话短说

根据我的理解,您可以编译到任何目标 LLVM 支持(使用内联汇编器模块级内联汇编的前端可能仍然有一些警告),但是,您不能保证它实际上会正确执行。前端负责完成可跨作者支持的平台移植的工作。

Note also that as a frontend developer you are responsible for providing the data layout and target triple.

see also:

Your Questions:

Let's say I'm writing Rust (which uses LLVM as a backend). Am I automatically capable of compiling my Rust code to every architecture that LLVM can target (assuming there's an OS on that machine that can run it)?

This is dependent on the authors of the Rust frontend.

Or could it be that the Rust standard library hasn't been made "ARM compatible" yet, so I couldn't compile to ARM even if the LLVM targets it?

I'm pretty sure LLVM would be able to emit the instructions, but it may not be correct in terms of addressing.

I have not used the inline assembler facilities mentioned above myself, but I assume if it allows platform specific assembly then this would break platform agnostic compilation as well.

What if I don't use any of the standard library, my entire program is just a program that returns right away? Could it be the case that even without any libraries, Rust (or what have you) can't compile to ARM (or what have you) even if the LLVM targets it?

This again depends on what the Rust frontend emits. There may be some boilerplate setup logic it emits even before it emits instructions for your logic.

我正在用 LLVM 编写自己的语言,在名为“main”的特殊函数的情况下执行此操作。我的目标是 C ABI,因此它将用适当的 C 风格 main 包装这个 main,并使用一组更严格的参数调用它。

如果上述所有示例都编译得很好,那么我需要做什么才能让我的代码在一种架构上中断而不是编译到某种架构?

考虑使用 C/C++ 和 Clang,如llvm FAQ中所述。Clang 是 LLVM 的一个前端,可能是最流行的,编写 C/C++ 的用户负责 #include-ing 适当的平台特定功能。

有些语言可能被设计得更加独立于平台,然后前端可以为您处理工作。

假设标准库使用操作系统系统调用(确实如此)。进行系统调用时是否需要关心架构?或者操作系统(例如 Linux)是否也抽象了架构?

我假设您正在谈论前端针对 C 标准库的情况,在这种情况下 LLVM 具有可由前端使用的标准 C 库内在函数。然而,这不是唯一的方法,因为如果像万花筒示例中那样以 C ABI 为目标,则可以使用call 指令直接调用 C 函数。

最终,标准库可能是一个可移植性问题,必须由前端开发人员解决。