Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for const string interpolation into inline assembly #132083

Open
dingxiangfei2009 opened this issue Oct 23, 2024 · 4 comments
Open

Support for const string interpolation into inline assembly #132083

dingxiangfei2009 opened this issue Oct 23, 2024 · 4 comments
Assignees
Labels
A-inline-assembly Area: Inline assembly (`asm!(…)`) C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@dingxiangfei2009
Copy link
Contributor

dingxiangfei2009 commented Oct 23, 2024

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
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Oct 23, 2024
@dingxiangfei2009
Copy link
Contributor Author

dingxiangfei2009 commented Oct 23, 2024

Requests from #132012 (comment) is fulfilled.

Bonus question I guess.

Do we want in(..) and out(..) constraints to accept const values as well? I imagine that it would be helpful to additionally mark registers clobbered, for instance.

@dingxiangfei2009
Copy link
Contributor Author

@rustbot claim

@jieyouxu jieyouxu added C-enhancement Category: An issue proposing an enhancement or a PR with one. A-inline-assembly Area: Inline assembly (`asm!(…)`) T-lang Relevant to the language team, which will review and decide on the PR/issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Oct 24, 2024
@Lokathor
Copy link
Contributor

I'm unclear why this is interpolate instead of sticking with const. The value is still a constant input to the asm, as far as I can tell.

@thomcc
Copy link
Member

thomcc commented Oct 25, 2024

It might be surprising to users that const on a string doesn't somehow give the assemnbly a refrerence to that constant string, rather than interpolating it directly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-inline-assembly Area: Inline assembly (`asm!(…)`) C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants