C to rust : "register" keyword

register u64 r0  __asm__("x0") = v.a0;
register u64 r1  __asm__("x1") = v.a1;

in c lang, the "register" keyword is used to declare a register variable.

how to do in rust?

No idea. But note that register in C is only a suggestion and whatever it does, if anything, is compiler implementation defined.

As such I suspect there is no way to force a Rust variable into a register.

8 Likes

Rust does not have register variables.

However, when using inline assembly, you can specify which register should hold a given input:

use std::arch::asm;

fn main() {
    // Multiply x by 6 using shifts and adds
    let mut x: u64 = 4;
    unsafe {
        asm!(
            "mov {tmp}, rax",
            "shl {tmp}, 1",
            "shl rax, 2",
            "add rax, {tmp}",
            inout("rax") x,
            tmp = out(reg) _,
        );
    }
    assert_eq!(x, 4 * 6);
}

This means that when the asm block runs, eax must hold the value of x. That said, the compiler could still move x around between registers or the stack before/after the asm block.

9 Likes

Please also note that while this sentence is, technically, true – it's also entirely useless here because topicstarter clearly discusses well-documented feature that has nothing is common with regular register keyword use.

The fact that topicstarter, as usual, mixes different things (well-defined GNU C feature vs nebulous and obsolete C standard feature) doesn't mean that we should mix them.

But no, I don't think writing GNU C in Rust is feasible or even possible.

1 Like

That's the exact same as in GNU C case: The only supported use for this feature is to specify registers for input and output operands when calling Extended asm.

Even if many users of GNU C ignore rules and [ab]use that syntax for other purposes but as documentation clearly says us it's intended use is fully covered by Rust's assembler syntax already.

Anything else was already not guaranteed in GNU C land and is just simply not possible in Rust.

1 Like

Quoting the linked docs:

The only supported use for this feature is to specify registers for input and output operands when calling Extended asm (see Extended Asm - Assembler Instructions with C Expression Operands). This may be necessary if the constraints for a particular machine don’t provide sufficient control to select the desired register. To force an operand into a register, create a local variable and specify the register name after the variable’s declaration. Then use the local variable for the asm operand and specify any constraint letter that matches the register

So it's only for specifying registers in asm statements. Rust supports this directly.

Outside of asm blocks, the C-standard register keyword doesn't have any use in current compilers, and is just ignored.

4 Likes

It's not that simple, unfortunately. You may still use it like topicstarter did and try to use them to access the desired registers.

It may or may not work depending on the options of the compiler and the phase of the moon… which is definitely not the semantic Rust could or should embrace – but, as usual, “nobody reads the documentation” and thus embedded developers use these register variables for various things… then they have to fix the precise version of the compiler, otherwise their firmware stops working.

1 Like

Right, but note that it's a GNU register which pins the variable to a specific register. Not the C-standard register which just asks "put in whatever register, maybe". That one isn't strong enough to be used in whatever capacity. It's just a very poor attempt at micro-optimization, that has no worth in 2024.

1 Like

Sure, but we are discussing precisely that since example in the very first message uses it like this.

That's why I said that ZiCog's message is technically, true – but useless. It discusses entirely unrelated language facility that no one uses these days – not even in C!

But topicstarter never even mentioned that one… I'm not even sure s/he's aware it even exists.

1 Like

thanks for your answer. i want to do this

https://users.rust-lang.org/t/c-convert-to-rust-smc-0/122445

  • in stdc, register is just a suggestion, like inline.
  • in rust, seems no way for it
1 Like