Replacing Macros

In C langue:

#define Temp el2
asm("msr x, " #Temp)

=> asm("msr x, el2")

In Rust langue:

const Temp: &str = "el2";
asm!(concat!("msr x, " , stringfy!(Temp)))

=> asm!("msr x, Temp")

The results are different

I think by changing const Temp: &str = "el2"; or stringfy!(Temp), the result is the same as C.

You might be able to achieve what you want using the constcat crate

Thank you for your answer.
Can you be more detailed?

Oh sorry, I tried it out and it's not possible with the crate I mentioned. constcat can be used to produce a new &str usable from const contexts, but not a new string literal.

The only way that comes to mind for doing something similar to what you want is defining a macro which will expand to a string literal. I don't like this approach because it seems a bit messy defining macros just to act as a variable. Maybe others can think of a cleaner approach.

macro_rules! temp {
    () => {
        "el2"
    };
}

fn main() {
    unsafe { core::arch::asm!(std::concat!("msr x, ", temp!())) };
}

Also for future posts, please format code between triple backticks to do proper code block formatting as described here: Forum Code Formatting and Syntax Highlighting

2 Likes

Ah, I also see in this post you were asking the same / similar question and made me realize that if the purpose is for the asm macro, there may be sufficient formatting options provided by that macro itself. Unfortunately I'm not familiar with it but here is the documentation.

1 Like

In case it helps, my new crate, preinterpret can solve this use case as written, assuming you don't actually need to use a static string:

// If asm! takes a string
preinterpret::preinterpret!{
    [!set! #temp = "el2"]
    asm!(
        [!string! "msr x, " #temp]
    )
}
1 Like

I'm sorry, I don't quite understand

@zoey, I think you need to find someone who can explain the answers you're receiving on the forum. Without this I'm afraid you won't be able to make progress.

1 Like