Porting ASM/C code from GCC to Rust


#1

I have C function, which contains register and __asm GCC keywords:

__attribute__((always_inline)) static inline uint32_t __func (void) {
    register uint32_t __r0 __asm("r""0");
    register uint32_t __rf __asm("r12") = (uint32_t)base_func;
    __asm volatile ("svc 0" : "=r"(__r0) : "r"(__rf) : "r1");
    return __r0;
}

I ported this function, but I do not know how to port a register keyword:

#[inline(always)]
pub fn __func() -> u32 {
    let func_ptr: u32 = base_func as u32;
    let mut __r0: u32 = ???;
    let __rf: u32 = ???;
    asm!("svc 0" : "=r"(r) : "r"(__rf) : "r1" : "volatile");
    __r0
}

Tools and environments:

  • C Compiler: arm-none-eabi-gcc (GNU Tools for ARM Embedded Processors 6-2017-q2-update)
  • Rust compiler: rustc 1.26.0-nightly (3eeb5a665 2018-03-01)
  • Target: thumbv7m-none-eabi
  • ARM architecture: Cortex-M3

Links:


#2

I believe the syntax you need is

asm!("svc 0" : "={r0}r"(__r0) : "{r12}r"(__rf) : "r1" : "volatile");

Rust currently uses LLVM’s native inline assembler syntax, which is based on GCC’s but somewhat different. Here’s where the braced register names are documented.


#3

Thanks, this is really what I need!