Clean up more code

- Found some bugs in LLVM compiler-rt library
- The useless LIBC_STUBS package is now deleted
- Improve the overflow checking story even further
- Get chibicc tests working in MODE=dbg mode again
- The libc/isystem/ headers now have correctly named guards
This commit is contained in:
Justine Tunney 2023-06-18 00:55:09 -07:00
parent afc58a8b41
commit d7c79f43ef
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
294 changed files with 912 additions and 1208 deletions

View file

@ -1,5 +1,8 @@
#ifndef COSMOPOLITAN_LIBC_STDCKDINT_H_
#define COSMOPOLITAN_LIBC_STDCKDINT_H_
#define __STDC_VERSION_STDCKDINT_H__ 202311L
#if !defined(MODE_DBG) && \
((defined(__GNUC__) && __GNUC__ >= 5 && !defined(__ICC)) || \
(defined(__has_builtin) && (__has_builtin(__builtin_add_overflow) && \
@ -16,6 +19,10 @@
#define ckd_sub(res, x, y) __ckd_arithmetic(sub, res, x, y)
#define ckd_mul(res, x, y) __ckd_arithmetic(mul, res, x, y)
/*
* implementation details
*/
#if defined(__SIZEOF_LONG__) && __SIZEOF_LONG__ == 8 && \
((defined(__GNUC__) && __GNUC__ * 100 + __GNUC_MINOR__ >= 406) || \
defined(__llvm__)) && \
@ -25,18 +32,96 @@
#define __ckd_dword long long
#endif
#define __ckd_arithmetic(op, res, x, y) \
(sizeof(*(res)) == sizeof(int) ? __ckd_##op((int *)(res), (x), (y)) \
: sizeof(*(res)) == sizeof(long) ? __ckd_##op##l((long *)(res), (x), (y)) \
: sizeof(*(res)) == sizeof(__ckd_dword) \
? __ckd_##op##ll((__ckd_dword *)(res), (x), (y)) \
: __ckd_trap())
#if defined(__cplusplus) && __cplusplus >= 201103L
#include <type_traits>
#define __ckd_is_unsigned(res) std::is_unsigned<decltype(*(res))>::value
#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
#define __ckd_is_unsigned(res) \
_Generic(*(res), unsigned : 1, unsigned long : 1, unsigned long long : 1, \
unsigned __ckd_dword : 1, default : 0)
#else
#define __ckd_is_unsigned(res) ((__typeof(*(res)))-1 >= 0)
#endif
#define __ckd_arithmetic(op, res, x, y) \
(__ckd_is_unsigned(res) \
? (sizeof(*(res)) == sizeof(int) \
? __ckd_##op##u((unsigned *)(res), (x), (y)) \
: sizeof(*(res)) == sizeof(long) \
? __ckd_##op##ul((unsigned long *)(res), (x), (y)) \
: sizeof(*(res)) == sizeof(__ckd_dword) \
? __ckd_##op##ull((unsigned __ckd_dword *)(res), (x), (y)) \
: __ckd_trap()) \
: (sizeof(*(res)) == sizeof(int) \
? __ckd_##op((signed int *)(res), (x), (y)) \
: sizeof(*(res)) == sizeof(long) \
? __ckd_##op##l((long *)(res), (x), (y)) \
: sizeof(*(res)) == sizeof(__ckd_dword) \
? __ckd_##op##ll((__ckd_dword *)(res), (x), (y)) \
: __ckd_trap()))
__funline int __ckd_trap(void) {
volatile int __x = 0;
return 1 / __x;
}
/*
* unsigned checked arithmetic
*/
__funline int __ckd_addu(unsigned *__z, unsigned __x, unsigned __y) {
*__z = __x + __y;
return *__z < __x;
}
__funline int __ckd_addul(unsigned long *__z, unsigned long __x,
unsigned long __y) {
*__z = __x + __y;
return *__z < __x;
}
__funline int __ckd_addull(unsigned __ckd_dword *__z, unsigned __ckd_dword __x,
unsigned __ckd_dword __y) {
*__z = __x + __y;
return *__z < __x;
}
__funline int __ckd_subu(unsigned *__z, unsigned __x, unsigned __y) {
*__z = __x - __y;
return __x < __y;
}
__funline int __ckd_subul(unsigned long *__z, unsigned long __x,
unsigned long __y) {
*__z = __x - __y;
return __x < __y;
}
__funline int __ckd_subull(unsigned __ckd_dword *__z, unsigned __ckd_dword __x,
unsigned __ckd_dword __y) {
*__z = __x - __y;
return __x < __y;
}
__funline int __ckd_mulu(unsigned *__z, unsigned __x, unsigned __y) {
*__z = __x * __y;
return __x && *__z / __x != __y;
}
__funline int __ckd_mulul(unsigned long *__z, unsigned long __x,
unsigned long __y) {
*__z = __x * __y;
return __x && *__z / __x != __y;
}
__funline int __ckd_mulull(unsigned __ckd_dword *__z, unsigned __ckd_dword __x,
unsigned __ckd_dword __y) {
*__z = __x * __y;
return __x && *__z / __x != __y;
}
/*
* signed checked arithmetic
*/
int __mulosi4(int, int, int *);
long __mulodi4(long, long, int *);
__ckd_dword __muloti4(__ckd_dword, __ckd_dword, int *);
__funline int __ckd_add(int *__z, int __x, int __y) {
unsigned int __a, __b, __c;
*__z = __c = (__a = __x) + (__b = __y);
@ -69,10 +154,6 @@ __funline int __ckd_subll(__ckd_dword *__z, __ckd_dword __x, __ckd_dword __y) {
return ((__a ^ __b) & (__c ^ __a)) >> (sizeof(__ckd_dword) * CHAR_BIT - 1);
}
int __mulosi4(int, int, int *);
long __mulodi4(long, long, int *);
__ckd_dword __muloti4(__ckd_dword, __ckd_dword, int *);
__funline int __ckd_mul(int *__z, int __x, int __y) {
int __o;
*__z = __mulosi4(__x, __y, &__o);