my theory is that __statement (a gcc extension) was used to allow
macros to evaluate their argument(s) once by assigning it to a local
variable, and then returning a value. this is difficult with normal
macros.
these days you can use inline functions to get the same effect, but
it is a more obvious and standard language feature.
the last remaining uses of __statement are in some archs endian
implementations.
this changes them to use inline functions. after this we can stop
providing __statement.
tests? ok?
Index: amd64/include/endian.h
===================================================================
RCS file: /cvs/src/sys/arch/amd64/include/endian.h,v
retrieving revision 1.6
diff -u -p -r1.6 endian.h
--- amd64/include/endian.h 12 Jul 2014 16:25:08 -0000 1.6
+++ amd64/include/endian.h 3 Jan 2018 23:23:44 -0000
@@ -27,33 +27,31 @@
#ifndef _MACHINE_ENDIAN_H_
#define _MACHINE_ENDIAN_H_
-#ifdef __GNUC__
+#include <sys/_types.h>
-#define __swap32md(x) __statement({
\
- __uint32_t __swap32md_x = (x); \
- \
- __asm ("bswap %0" : "+r" (__swap32md_x)); \
- __swap32md_x; \
-})
+static inline __uint32_t
+__swap32md(__uint32_t x)
+{
+ __asm ("bswap %0" : "+r" (x));
+ return (x);
+}
-#define __swap64md(x) __statement({
\
- __uint64_t __swap64md_x = (x); \
- \
- __asm ("bswapq %0" : "+r" (__swap64md_x)); \
- __swap64md_x; \
-})
+static inline __uint64_t
+__swap64md(__uint64_t x)
+{
+ __asm ("bswapq %0" : "+r" (x));
+ return (x);
+}
-#define __swap16md(x) __statement({
\
- __uint16_t __swap16md_x = (x); \
- \
- __asm ("rorw $8, %w0" : "+r" (__swap16md_x)); \
- __swap16md_x; \
-})
+static inline __uint16_t
+__swap16md(__uint16_t x)
+{
+ __asm ("rorw $8, %w0" : "+r" (x));
+ return (x);
+}
/* Tell sys/endian.h we have MD variants of the swap macros. */
#define __HAVE_MD_SWAP
-
-#endif /* __GNUC__ */
#define _BYTE_ORDER _LITTLE_ENDIAN
Index: arm64/include/endian.h
===================================================================
RCS file: /cvs/src/sys/arch/arm64/include/endian.h,v
retrieving revision 1.2
diff -u -p -r1.2 endian.h
--- arm64/include/endian.h 6 Feb 2017 04:08:57 -0000 1.2
+++ arm64/include/endian.h 3 Jan 2018 23:23:44 -0000
@@ -19,30 +19,34 @@
#ifndef _MACHINE_ENDIAN_H_
#define _MACHINE_ENDIAN_H_
-#define __swap32md(x) __statement({ \
- __uint32_t __swap32md_x; \
- \
- __asm ("rev %w0, %w1" : "=r" (__swap32md_x) : "r"(x)); \
- __swap32md_x; \
-})
+#include <sys/_types.h>
-#define __swap64md(x) __statement({ \
- __uint64_t __swap64md_x; \
- \
- __asm ("rev %x0, %x1" : "=r" (__swap64md_x) : "r"(x)); \
- __swap64md_x; \
-})
+static inline __uint32_t
+__swap32md(__uint32_t i)
+{
+ __uint32_t o;
+ __asm ("rev %w0, %w1" : "=r" (o) : "r" (i));
+ return (o);
+}
-#define __swap16md(x) __statement({ \
- __uint16_t __swap16md_x; \
- \
- __asm ("rev16 %w0, %w1" : "=r" (__swap16md_x) : "r"(x)); \
- __swap16md_x; \
-})
+static inline __uint64_t
+__swap64md(__uint64_t i)
+{
+ __uint64_t o;
+ __asm ("rev %x0, %x1" : "=r" (o) : "r" (i));
+ return (o);
+}
+
+static inline __uint16_t
+__swap16md(__uint16_t i)
+{
+ __uint16_t o;
+ __asm ("rev16 %w0, %w1" : "=r" (o) : "r" (i));
+ return (o);
+}
/* Tell sys/endian.h we have MD variants of the swap macros. */
#define __HAVE_MD_SWAP
-
#define _BYTE_ORDER _LITTLE_ENDIAN
#define __STRICT_ALIGNMENT
Index: i386/include/endian.h
===================================================================
RCS file: /cvs/src/sys/arch/i386/include/endian.h,v
retrieving revision 1.18
diff -u -p -r1.18 endian.h
--- i386/include/endian.h 12 Jul 2014 16:25:08 -0000 1.18
+++ i386/include/endian.h 3 Jan 2018 23:23:44 -0000
@@ -27,32 +27,31 @@
#ifndef _MACHINE_ENDIAN_H_
#define _MACHINE_ENDIAN_H_
-#ifdef __GNUC__
+#include <sys/_types.h>
-#define __swap32md(x) __statement({
\
- __uint32_t __swap32md_x = (x); \
- \
- __asm ("bswap %0" : "+r" (__swap32md_x)); \
- __swap32md_x; \
-})
+static inline __uint32_t
+__swap32md(__uint32_t x)
+{
+ __asm ("bswap %0" : "+r" (x));
+ return (x);
+}
-#define __swap64md(x) __statement({
\
- __uint64_t __swap64md_x = (x); \
- \
- (__uint64_t)__swap32md(__swap64md_x >> 32) | \
- (__uint64_t)__swap32md(__swap64md_x & 0xffffffff) << 32; \
-})
-#define __swap16md(x) __statement({
\
- __uint16_t __swap16md_x = (x); \
- \
- __asm ("rorw $8, %w0" : "+r" (__swap16md_x)); \
- __swap16md_x; \
-})
+static inline __uint64_t
+__swap64md(__uint64_t x)
+{
+ return ((__uint64_t)__swap32md(x >> 32) |
+ (__uint64_t)__swap32md(x) << 32);
+}
+
+static inline __uint16_t
+__swap16md(__uint16_t x)
+{
+ __asm ("rorw $8, %w0" : "+r" (x));
+ return (x);
+}
/* Tell sys/endian.h we have MD variants of the swap macros. */
#define __HAVE_MD_SWAP
-
-#endif /* __GNUC__ */
#define _BYTE_ORDER _LITTLE_ENDIAN
Index: sh/include/endian.h
===================================================================
RCS file: /cvs/src/sys/arch/sh/include/endian.h,v
retrieving revision 1.5
diff -u -p -r1.5 endian.h
--- sh/include/endian.h 12 Jul 2014 16:25:09 -0000 1.5
+++ sh/include/endian.h 3 Jan 2018 23:23:44 -0000
@@ -6,30 +6,33 @@
#ifndef _SH_ENDIAN_H_
#define _SH_ENDIAN_H_
-#ifdef __GNUC__
+#include <sys/_types.h>
-#define __swap64md __swap64gen
+static inline __uint16_t
+__swap16md(__uint16_t i)
+{
+ __uint16_t o;
+ __asm volatile ("swap.b %1,%0" : "=r" (o) : "r" (i));
+ return (o);
+}
-#define __swap16md(x) __statement({ \
- uint16_t rval; \
- \
- __asm volatile ("swap.b %1,%0" : "=r"(rval) : "r"(x)); \
- \
- rval; \
+static inline __uint32_t
+__swap32md(__uint32_t i)
+{
+ __uint32_t o;
+ __asm volatile ("swap.b %1,%0; swap.w %0,%0; swap.b %0,%0"
+ : "=r" (o) : "r" (i));
+ return (o);
})
-#define __swap32md(x) __statement({ \
- uint32_t rval; \
- \
- __asm volatile ("swap.b %1,%0; swap.w %0,%0; swap.b %0,%0" \
- : "=r"(rval) : "r"(x)); \
- \
- rval; \
-})
+static inline __uint64_t
+__swap64md(__uint64_t x)
+{
+ return ((__uint64_t)__swap32md(x >> 32) |
+ (__uint64_t)__swap32md(x) << 32);
+}
#define __HAVE_MD_SWAP
-
-#endif /* __GNUC_ */
#ifdef __LITTLE_ENDIAN__
#define _BYTE_ORDER _LITTLE_ENDIAN