Skip to content

Support for const string interpolation into inline assembly #132083

Open
@dingxiangfei2009

Description

@dingxiangfei2009

Related to this comment.

Maybe related to #93332

This feature request targets the inline assembly macro asm! and globally scope assembly global_asm! to support direct string interpolation into the assembly template.

The semantic works very much like a format! in a narrower sense, that only constant string is supported. The proposed macro word is interpolate $expr where $expr is a const-evaluatable expression that yields a &'static str constant value.

An example of how it would work is as follows.

trait Helper {
    const SRC: &'static str;
}

fn make_it_work<H: Helper>(h: &H, x: i64) {
    asm!(
        "mov {0}, {1}",
        in(reg) x,
        interpolate H::SRC
    );
}

struct H;
impl Helper for H {
    const SRC: &'static str = "MAGIC";
}

fn invoke() {
    make_it_work(&H, 42);
}

The one and only instantiation of asm! macro, when completely expanded through codegen, might have yield the following assembly line.

    mov rcx, MAGIC

Metadata

Metadata

Labels

A-inline-assemblyArea: Inline assembly (`asm!(…)`)C-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions