Add chibicc

This program popped up on Hacker News recently. It's the only modern
compiler I've ever seen that doesn't have dependencies and is easily
modified. So I added all of the missing GNU extensions I like to use
which means it might be possible soon to build on non-Linux and have
third party not vendor gcc binaries.
This commit is contained in:
Justine Tunney 2020-12-05 12:20:41 -08:00
parent e44a0cf6f8
commit 8da931a7f6
298 changed files with 19493 additions and 11950 deletions

View file

@ -2,6 +2,14 @@
#define __attribute__(x)
#endif
#ifndef __cplusplus
#define COSMOPOLITAN_C_START_
#define COSMOPOLITAN_C_END_
#define COSMOPOLITAN_CXX_START_
#define COSMOPOLITAN_CXX_END_
#define COSMOPOLITAN_CXX_USING_
#endif
#if defined(__STRICT_ANSI__) && __STDC_VERSION__ + 0 < 201112
#define asm __asm__
#endif
@ -67,18 +75,6 @@
} while (0)
#endif
#if defined(__STRICT_ANSI__) || \
(!defined(__GNUC__) && !__has_builtin(constant_p) && \
!defined(__builtin_constant_p))
#define __builtin_constant_p(x) 0
#endif
#if defined(__STRICT_ANSI__) || \
(!defined(__GNUC__) && !__has_builtin(choose_expr) && \
!defined(__builtin_choose_expr))
#define __builtin_choose_expr(x, a, b) ((x) ? (long)(a) : (long)(b))
#endif
#if __STDC_VERSION__ + 0 < 201112
#define ____Static_assert(x, y) A##B
#define ___Static_assert(x, y) ____Static_assert(x, y)
@ -195,7 +191,7 @@ typedef int64_t intmax_t;
typedef uint64_t uintmax_t;
#endif
#ifdef __GNUC__
#ifndef __chibicc__
#define va_list __builtin_va_list
#define va_arg(ap, type) __builtin_va_arg(ap, type)
#define va_copy(dest, src) __builtin_va_copy(dest, src)
@ -230,11 +226,11 @@ typedef uint64_t uintmax_t;
/**
* Aligns automatic or static variable.
*/
#ifndef aligned
#ifndef forcealign
#ifndef __STRICT_ANSI__
#define aligned(bytes) __attribute__((__aligned__(bytes)))
#define forcealign(bytes) __attribute__((__aligned__(bytes)))
#else
#define aligned(bytes)
#define forcealign(bytes)
#endif
#endif
@ -291,13 +287,13 @@ typedef uint64_t uintmax_t;
#endif
#endif
#ifndef noreturn
#ifndef wontreturn
#if !defined(__STRICT_ANSI__) && \
(__has_attribute(__noreturn__) || \
(__GNUC__ + 0) * 100 + (__GNUC_MINOR__ + 0) >= 208)
#define noreturn __attribute__((__noreturn__))
#define wontreturn __attribute__((__noreturn__))
#else
#define noreturn
#define wontreturn
#endif
#endif
@ -1067,13 +1063,5 @@ typedef uint64_t uintmax_t;
STATIC_YOINK_SOURCE(__BASE_FILE__);
#endif
#ifndef __cplusplus
#define COSMOPOLITAN_CXX_START_
#define COSMOPOLITAN_CXX_END_
#define COSMOPOLITAN_CXX_USING_
#define COSMOPOLITAN_C_START_
#define COSMOPOLITAN_C_END_
#endif
#define MACHINE_CODE_ANALYSIS_BEGIN_
#define MACHINE_CODE_ANALYSIS_END_

View file

@ -1,50 +1,17 @@
typedef struct {
unsigned int gp_offset;
unsigned int fp_offset;
void *overflow_arg_area;
void *reg_save_area;
} __va_elem;
typedef __va_elem va_list[1];
#define va_start(ap, last) \
do { \
*(ap) = *(__va_elem *)__va_area__; \
} while (0)
#define va_end(ap)
static inline void *__va_arg_mem(__va_elem *ap, int sz, int align) {
void *p = ap->overflow_arg_area;
if (align > 8) p = (void *)(((unsigned long)p + 15) / 16 * 16);
ap->overflow_arg_area = (void *)(((unsigned long)p + sz + 7) / 8 * 8);
return p;
}
static inline void *__va_arg_gp(__va_elem *ap, int sz, int align) {
if (ap->gp_offset >= 48) return __va_arg_mem(ap, sz, align);
void *r = ap->reg_save_area + ap->gp_offset;
ap->gp_offset += 8;
return r;
}
static inline void *__va_arg_fp(__va_elem *ap, int sz, int align) {
if (ap->fp_offset >= 112) return __va_arg_mem(ap, sz, align);
void *r = ap->reg_save_area + ap->fp_offset;
ap->fp_offset += 8;
return r;
}
#define va_arg(ap, ty) \
({ \
int klass = __builtin_reg_class(ty); \
*(ty *)(klass == 0 \
? __va_arg_gp(ap, sizeof(ty), _Alignof(ty)) \
: klass == 1 ? __va_arg_fp(ap, sizeof(ty), _Alignof(ty)) \
: __va_arg_mem(ap, sizeof(ty), _Alignof(ty))); \
})
#define va_copy(dest, src) ((dest)[0] = (src)[0])
#include "libc/runtime/valist.h"
#define __GNUC_VA_LIST 1
typedef va_list __gnuc_va_list;
#define __gnuc_va_list va_list
#define va_end(AP)
#define va_copy(DST, SRC) ((DST)[0] = (SRC)[0])
#define va_start(AP, LAST) \
do { \
*(AP) = *(struct __va *)__va_area__; \
} while (0)
#define va_arg(AP, TYPE) \
(*(TYPE *)__va_arg(AP, sizeof(TYPE), _Alignof(TYPE), \
__builtin_reg_class(TYPE)))
typedef struct __va va_list[1];