[英]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 rustc
和RUSTFLAGS
)都是使用标准工具进行组装的最佳方式。 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:不过有几点需要注意:
cargo asm
and it will list all symbols you can inspect.只需运行cargo asm
,它就会列出您可以检查的所有符号。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-artifactsIf 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"]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.