2020-06-15 14:18:57 +00:00
|
|
|
#ifndef COSMOPOLITAN_LIBC_BITS_PUSHPOP_H_
|
|
|
|
#define COSMOPOLITAN_LIBC_BITS_PUSHPOP_H_
|
2023-08-14 03:31:27 +00:00
|
|
|
#ifdef _COSMO_SOURCE
|
2021-03-01 07:42:35 +00:00
|
|
|
#include "libc/macros.internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
|
2023-06-15 00:02:57 +00:00
|
|
|
#if !defined(__GNUC__) || defined(__STRICT_ANSI__) || !defined(__x86_64__) || \
|
|
|
|
!defined(__MNO_RED_ZONE__)
|
2020-12-26 10:09:07 +00:00
|
|
|
#define pushpop(x) (x)
|
|
|
|
#else
|
2020-06-15 14:18:57 +00:00
|
|
|
/**
|
|
|
|
* PushPop
|
|
|
|
* An elegant weapon for a more civilized age.
|
|
|
|
*/
|
2020-12-24 07:42:56 +00:00
|
|
|
#define pushpop(x) \
|
|
|
|
({ \
|
|
|
|
typeof(x) Popped; \
|
|
|
|
if (__builtin_constant_p(x) && \
|
|
|
|
(TYPE_SIGNED(typeof(x)) ? (intptr_t)(x) + 128 < 256 \
|
|
|
|
: (intptr_t)(x) < 128)) { \
|
|
|
|
if (x) { \
|
|
|
|
asm("push\t%1\n\t" \
|
|
|
|
"pop\t%q0" \
|
|
|
|
: "=r"(Popped) \
|
|
|
|
: "ir"(x)); \
|
|
|
|
} else { \
|
|
|
|
asm("xor\t%k0,%k0" : "=r"(Popped)); \
|
|
|
|
} \
|
|
|
|
} else { \
|
|
|
|
asm("" : "=r"(Popped) : "0"(x)); \
|
|
|
|
} \
|
|
|
|
Popped; \
|
2020-06-15 14:18:57 +00:00
|
|
|
})
|
|
|
|
#endif
|
|
|
|
|
2023-06-15 00:02:57 +00:00
|
|
|
#if !defined(__GNUC__) || defined(__STRICT_ANSI__) || !defined(__x86_64__) || \
|
|
|
|
!defined(__MNO_RED_ZONE__)
|
2020-08-25 11:23:25 +00:00
|
|
|
#define pushmov(d, x) (*(d) = (x))
|
2020-06-15 14:18:57 +00:00
|
|
|
#else
|
2020-12-24 07:42:56 +00:00
|
|
|
#define pushmov(d, x) \
|
|
|
|
({ \
|
|
|
|
typeof(*(d)) Popped = (x); \
|
|
|
|
if (__builtin_constant_p(x) && \
|
|
|
|
(TYPE_SIGNED(typeof(x)) ? (intptr_t)(x) + 128 < 256 \
|
|
|
|
: (intptr_t)(x) < 128)) { \
|
|
|
|
asm("pushq\t%1\n\t" \
|
|
|
|
"popq\t%0" \
|
|
|
|
: "=m"(*(d)) \
|
|
|
|
: "ir"(Popped)); \
|
|
|
|
} else { \
|
|
|
|
*(d) = Popped; \
|
|
|
|
} \
|
|
|
|
Popped; \
|
2020-06-15 14:18:57 +00:00
|
|
|
})
|
|
|
|
#endif
|
|
|
|
|
2023-08-14 03:31:27 +00:00
|
|
|
#endif /* _COSMO_SOURCE */
|
2020-06-15 14:18:57 +00:00
|
|
|
#endif /* COSMOPOLITAN_LIBC_BITS_PUSHPOP_H_ */
|