简体   繁体   中英

Why the assembly output of a compiled rust code does not include any asm instructions?

I wrote this snippet in the compiler explorer :


fn foo() -> u8 {
    54
}

fn bar() -> u128 {
    3423
}


fn main() {

    let a: (u8, u128) = (foo(), bar());

    println!("foo result: {}", a.0);
    println!("bar result: {}", a.1);

}

The assembly version is this:

        .text
        .file   "example.db4da305-cgu.0"
        .type   __rustc_debug_gdb_scripts_section__,@object
        .section        .debug_gdb_scripts,"aMS",@progbits,1,unique,1
        .weak   __rustc_debug_gdb_scripts_section__
__rustc_debug_gdb_scripts_section__:
        .asciz  "\001gdb_load_rust_pretty_printers.py"
        .size   __rustc_debug_gdb_scripts_section__, 34

        .section        .debug_aranges,"",@progbits
        .section        ".note.GNU-stack","",@progbits

I'm not super familiar with assembly but my little knowledge of it tells me that this doesn't include any asm instructions that implement any of these function. This is only directives.

Am I using compiler explorer in a wrong way? (checking a flag or something)

This is link of my snippet.

Changing fn main to pub fn main outputs the correct assembly.

Here is a comment from the rust example:

// Type your code here, or load an example.
pub fn square(num: i32) -> i32 {
    num * num
}

// If you use `main()`, declare it as `pub` to see it in the output: <- This is important
// pub fn main() { ... }

You are compiling a library by default. And since your library does not have any public symbol, it does nothing and your asm is empty.

The solution, either:

  • Declare your library main as public: pub fn main()
  • Compile a binary:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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