Description
Background
Currently there isn't an easy way to enable or disable portions of assembly based on configuration. This can be a annoyance for asm!
that can be worked around with multiple assembly blocks, but it is especially a problem in global_asm!
and naked_asm!
because small config-based changes mean the entire assembly block needs to be duplicated.
Another workaround is possible by defining multiple macro_rules!
of the same name that are enabled/disabled based on the desired config and expand to strings. This option is inconvenient and fragments the assembly.
Proposal
Allow #[cfg(...)]
in all assembly macros:
asm!( // or global_asm! or naked_asm!
"nop",
#[cfg(target_feature = "sse2")]
"nop",
// ...
#[cfg(target_feature = "sse2")]
a = const 123, // only used on sse2
);
The configuration applies to a single comma-separated segment.
We may want to also support blocks in order to allow grouping instructions without merging strings, or grouping directives:
asm!(
"nop",
#[cfg(target_feature = "sse2")] {
"nop",
"nop",
"nop",
} // does allowing/requiring a comma here make sense?
"nop",
#[cfg(target_feature = "sse2")] {
a = const 123,
a = const 123,
}
);
This design has some concerns and there may be a better option, see discussion in the thread.
Zulip discussion: #project-inline-asm > `cfg` in assembly blocks