Amir reza Riahi
Amir reza Riahi

Reputation: 2450

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.

Upvotes: 0

Views: 663

Answers (2)

rodrigo
rodrigo

Reputation: 98378

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:

enter image description here

Upvotes: 3

maxicarlos08
maxicarlos08

Reputation: 121

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() { ... }

Upvotes: 4

Related Questions