简体   繁体   English

如何从与货物一起建造中获得装配 output?

[英]How to get assembly output from building with Cargo?

While I've seen docs on using rustc directly to output assembly, having to manually extract commands used by Cargo and edit them to write assembly is tedious.虽然我已经看到有关将rustc直接用于 output 程序集的文档,但必须手动提取 Cargo 使用的命令并对其进行编辑以编写程序集是乏味的。

Is there a way to run Cargo that writes out assembly files?有没有办法运行写出汇编文件的 Cargo?

You can use Cargo's cargo rustc command to send arguments to rustc directly:您可以使用 Cargo 的cargo rustc命令直接向rustc发送参数:

cargo rustc -- --emit asm
ls target/debug/deps/<crate_name>-<hash>.s

For optimized assembly:优化装配:

cargo rustc --release -- --emit asm
ls target/release/deps/<crate_name>-<hash>.s

If you see multiple <crate_name>-<hash>-<hash>.rcgu.s files instead of a <crate_name>-<hash>.s file, disable incremental compilation by setting the environment variable CARGO_INCREMENTAL=0 .如果您看到多个<crate_name>-<hash>-<hash>.rcgu.s文件而不是<crate_name>-<hash>.s文件,请通过设置环境变量CARGO_INCREMENTAL=0禁用增量编译。

In addition to kennytm's answer, you can also use the RUSTFLAGS environment variable and use the standard cargo commands:除了 kennytm 的回答,您还可以使用RUSTFLAGS环境变量并使用标准的货物命令:

RUSTFLAGS="--emit asm" cargo build
cat target/debug/deps/project_name-hash.s

Or in release mode (with optimizations):或者在发布模式下(经过优化):

RUSTFLAGS="--emit asm" cargo build --release
cat target/release/deps/project_name-hash.s

You can pass different values to the --emit parameter, including (but not limited to):您可以将不同的值传递给--emit参数,包括(但不限于):

  • mir (Rust intermediate representation) mir (Rust 中间表示)
  • llvm-ir (LLVM intermediate representation) llvm-ir (LLVM 中间表示)
  • llvm-bc (LLVM byte code) llvm-bc (LLVM 字节码)
  • asm (assembly) asm (组装)

Both existing answers (using cargo rustc and RUSTFLAGS ) are the best ways to obtain assembly with standard tools.现有的两种答案(使用cargo rustcRUSTFLAGS )都是使用标准工具进行组装的最佳方式。 If you find yourself trying to look at assembly fairly often, you might want to consider using the cargo asm subcommand .如果您发现自己经常尝试查看程序集,您可能需要考虑使用cargo asm子命令 After installing it with cargo install cargo-asm , you can print assembly like:使用cargo install cargo-asm ,你可以打印装配体,如:

cargo build --release
cargo asm my_crate::my_function

There are a few things to pay attention to, though:不过有几点需要注意:

  • Unsure about the path of your function?不确定函数的路径? Just run cargo asm and it will list all symbols you can inspect.只需运行cargo asm ,它就会列出您可以检查的所有符号。
  • You have to cargo build --release before trying to look at the assembly, because cargo asm (apparently) only looks at the already existing build-artifacts在尝试查看程序集之前,您必须执行cargo build --release ,因为cargo asm (显然)只查看已经存在的 build-artifacts
  • The code for the function you want to inspect has to be actually generated.您要检查的函数的代码必须实际生成。 For generic functions this means that the function has to be instantiated/monomorphized with a concrete type.对于泛型函数,这意味着该函数必须使用具体类型进行实例化/单态化。 If that doesn't happen in your crate, you can always add a dummy function at the top level that does everything you want to inspect the assembly of.如果您的箱子中没有发生这种情况,您始终可以在顶层添加一个虚拟函数,该函数可以执行您想要检查其组装的所有操作。

If you just want to look at the assembly output instead of saving it, eg to judge if it's well-optimized, then an easy option is to use:如果您只想查看程序集 output 而不是保存它,例如判断它是否优化良好,那么一个简单的选择是使用:

https://rust.godbolt.org/ https://rust.godbolt.org/

(don't forget to add -O to the Compiler options box) (不要忘记在编译器选项框中添加-O

[build]
rustflags = ["--emit", "asm", "-Cllvm-args=--x86-asm-syntax=intel"]

FROM https://godbolt.org 来自https://godbolt.org

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

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