Calling Macro in extended assembly

80 Views Asked by At

I want to add a macro in in extended assembly in c code. is this possible ?

I am looking for something like this:

   ANY_MACRO (Instruction) asm volatile(
        "li t0, 0xcafebabe;"
        "li t1, 0x00c0fee;"
        <----///Replace instruction here like ()////---->
        :[dummy_output]"=r"(dummy_output_var)
        :[dummy_var_addr]"r"(&dummy_var1)
        :"t0","t1","t2","a0"
        );

Note: I am also manipulating some C-Variables in this Macro as shown above!

Compiler: GCC Assembly: RISCV

1

There are 1 best solutions below

1
Eric Postpischil On

The __asm__ construct takes the assembly as a quoted string (a string literal), so the macro needs to produce its result as a quoted string. This can be in the form of consecutive quoted strings, which the compiler will automatically concatenate into one string, and you can use the # preprocessor operator to convert macro arguments into quoted strings:

#define INSERT_INST( inst, rd, rs2, rs1 ) #inst " " #rd "," #rs2 "," #rs1 ";"

If any of the arguments to your INSERT_INST macro are themselves macros that you want expanded, you will need another macro to replace and quote them:

#define Quote(x)    #x
#define INSERT_INST( inst, rd, rs2, rs1 ) Quote(inst) " " Quote(rd) "," Quote(rs2) "," Quote(rs1) ";"