Macro for att syntax asm

Rust uses intel syntax by default on x86, and att syntax by default everywhere else. I'd like a macro that uses att syntax everywhere to avoid this footgun.

I put together this

/// Wrapper around `asm!` for x86 using att syntax.
#[cfg(target_arch = "x86_64")]
#[macro_export]
macro_rules! asm {
    ($($asm:tt)*) => {
        ::core::arch::asm!( $($asm)*, options(att_syntax) )
    };
}

// Rust only uses intel syntax by default on x86, so no need for a wrapper on other arches.
#[cfg(not(target_arch = "x86_64"))]
pub use ::core::arch::asm;

This works, but it doesn't allow trailing commas. How can I get this to allow trailing commas? Do I have to reproduce the full syntax of the asm! macro in my macro match group?

I don't see how support for that can be added without first modeling the full syntax possible for each line of ASM. Such comma's are part of the line, after all...

...so yeah unless there's something I'm missing w.r.t. how declarative macros work, that's the distinctly displeasurable task in front of you if you really want AT&T syntax.

I'll admit, it'd be nice to be able to freely choose between the 2. So if you do end up implementing this, perhaps your code could be added upstream, with perhaps an RFC in between?

I came up with this, which allows trailing commas, but changes the native syntax to use a semicolon after the string.

/// Wrapper around `asm!` that uses at&t syntax on x86.
// Uses a semicolon to avoid parsing ambiguities, even though this
// does not match native `asm!` syntax.
#[cfg(target_arch = "x86_64")]
#[macro_export]
macro_rules! asm {
    ($($asm:expr),* ; $($rest:tt)*) => {
        ::core::arch::asm!( $($asm)*, options(att_syntax), $($rest)* )
    };
}

/// Wrapper around `asm!` that uses at&t syntax on x86.
// For non-x86 arches we just pass through to `asm!`.
#[cfg(not(target_arch = "x86_64"))]
#[macro_export]
macro_rules! asm {
    ($($asm:expr),* ; $($rest:tt)*) => {
        ::core::arch::asm!( $($asm)*, $($rest)* )
    };
}

It would still be nice to keep it working with a comma instead of a semicolon.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.