rust-lang / rust-lang/rust

Support for const string interpolation into inline assembly

Open
#132,083 6 comments 0 reactions 1 assignee View on GitHub

@dingxiangfei2009 is already working on this.

Since Oct 23, 2024.

A-inline-assembly C-enhancement T-compiler T-lang
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.