简体   繁体   English

如何让 cargo/rustc 为 Rust 代码生成简洁的汇编语言?

[英]How to have cargo/rustc generate concise assembly language for Rust code?

I find godbolt very useful for comparing how different implementations of the same algorithm result in different assembly code.我发现 godbolt 对于比较同一算法的不同实现如何产生不同的汇编代码非常有用。 For example :例如

pub fn range_north(sq: u8) -> impl Iterator<Item = u8> {
    (sq..=(sq | 0b111000)).step_by(8)
}

pub fn range_north2(sq: u8) -> impl Iterator<Item = u8> {
    let (rk, fl) = (sq / 8, sq % 8);
    let n = 8 - rk;
    (sq..=63).step_by(8).take(n as usize)
}

gives

example::range_north:
        mov     rax, rdi
        movzx   ecx, sil
        or      sil, 56
        movzx   edx, sil
        shl     edx, 8
        or      edx, ecx
        mov     word ptr [rdi + 8], dx
        mov     word ptr [rdi + 10], 256
        mov     qword ptr [rdi], 7
        ret

example::range_north2:
        mov     rax, rdi
        movzx   ecx, sil
        shr     sil, 3
        mov     dl, 8
        sub     dl, sil
        or      ecx, 16128
        movzx   edx, dl
        mov     qword ptr [rdi], 7
        mov     word ptr [rdi + 8], cx
        mov     word ptr [rdi + 10], 256
        mov     qword ptr [rdi + 16], rdx
        ret

Nothing more.而已。 How can I produce this output offline with Rust's command line tools without the boilerplate code and other pieces that don't help me compare generated assembly, like the above?我如何使用 Rust 的命令行工具离线生成这个 output 而没有样板代码和其他无法帮助我比较生成的程序集的部分,如上所示?

cargo rustc --release -- --emit asm -C "llvm-args=-x86-asm-syntax=intel" will produce optimized GAS with Intel syntax. cargo rustc --release -- --emit asm -C "llvm-args=-x86-asm-syntax=intel"将生成具有 Intel 语法的优化 GAS。

You can also use cargo asm .您也可以使用cargo asm

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

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