risaldar
risaldar

Reputation: 71

stringifying inline assembly

How can I use integer parameters in the inline assembly? I have the following function parameters:

unsigned int __MRC(uint32 coproc, uint32 opcode1, uint32 CRn, uint32 CRm, uint32 opcode2)

I have moved CRn CRm to r0 and r1 by

register unsigned int r0 asm("r0") = (unsigned int) (CRn);

register unsigned int r1 asm("r1") = (unsigned int) (CRm);

and I now want to write it in form:

__asm__(" MRC ... ");

this is the format:

MRC <copr>, <op1>, Rd, CRn, CRm{, <op2>}

Upvotes: 1

Views: 557

Answers (1)

Balau
Balau

Reputation: 495

If I understand correctly your question, I think the answer is to use GCC Extended ASM functionality. You can avoid the register unsigned int ... asm("r*") ... code lines and just write an inline assembly statement with extended ASM syntax.

About the fact that you need to expand some constants, maybe your best bet is to use a combination of preprocessor stringification, inline ASM and compound statement expressions. Something like the following, where all the parameters are constants:

#define MRC(coproc, opcode1, CRn, CRm, opcode2)\
  ({unsigned int rd; __asm__(\
    "MRC p" #coproc ", " #opcode1 ", %0, c" #CRn ", c" #CRm ", " #opcode2 \
    :"=r"(rd)); rd; })

unsigned int foo(void) {
  // expands to something like:
  // MRC p15, 0, r0, c0, c0, 0
  return MRC(15, 0, 0, 0, 0);
}

Upvotes: 3

Related Questions