[PATCH v3 10/12] linux/linkage: add ASM() macro to reduce duplication between C/ASM code
From: Masahiro Yamada
Date: Mon Dec 17 2018 - 11:10:55 EST
We often duplicate similar assembly code to use it from .c and .S files.
The difference is mostly the presence of double quotes.
So, here is a new macro ASM().
(We have similar approach for __ASM_FORM(), etc.)
The usage is like this:
#define MY_CODE \
ASM( .section ".text" )\
ASM( movl $1, %eax )
In C context, the preprocessor expands it into:
".section \".text\"" "\n\t" "movl $1, %eax" "\n\t"
This is perfect for the use from the inline asm(...) in .c files.
In assembly context, the preprocessor expands it into:
.section ".text" ; movl $1, %eax ;
This is fine for the use in .S files.
I used double-expansion like __stringify() so that we can use
macros in ASM(...).
Signed-off-by: Masahiro Yamada <yamada.masahiro@xxxxxxxxxxxxx>
---
include/linux/linkage.h | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/include/linux/linkage.h b/include/linux/linkage.h
index 7c47b1a..80faeae 100644
--- a/include/linux/linkage.h
+++ b/include/linux/linkage.h
@@ -12,6 +12,14 @@
#define ASM_NL ;
#endif
+#ifdef __ASSEMBLY__
+#define _ASM(x...) x ASM_NL
+#else
+#define _ASM(x...) #x __stringify(\n\t)
+#endif
+/* Doing two levels allows macros to be used in ASM(...) */
+#define ASM(x...) _ASM(x)
+
#ifdef __cplusplus
#define CPP_ASMLINKAGE extern "C"
#else
--
2.7.4