From 548dcb9f089297c6791c63fd1d0a727617d76ae9 Mon Sep 17 00:00:00 2001 From: Justine Tunney Date: Sun, 27 Dec 2020 17:05:03 -0800 Subject: [PATCH] Further refine documentation --- Makefile | 8 +- examples/examples.mk | 1 + examples/fld.c | 120 ------ libc/bits/xmmintrin.internal.h | 44 ++- libc/integral/lp64arg.inc | 8 +- libc/log/vflogf.c | 6 +- libc/math.h | 22 -- libc/nexgen32e/bsrmax.S | 18 +- libc/nt/winsock.h | 2 +- libc/runtime/valist.c | 4 +- libc/runtime/valist.h | 4 +- third_party/chibicc/as.c | 145 ++------ third_party/chibicc/chibicc.h | 4 +- third_party/chibicc/dox1.c | 7 +- third_party/chibicc/dox2.c | 108 ++++-- third_party/chibicc/file.c | 106 ++++++ third_party/chibicc/file.h | 13 + third_party/chibicc/parse.c | 50 ++- third_party/chibicc/test/asm_test.c | 1 + third_party/chibicc/test/vla_test.c | 23 ++ third_party/chibicc/tokenize.c | 103 +----- third_party/getopt/getopt.c | 50 ++- third_party/getopt/getopt_long.3 | 444 ---------------------- third_party/getopt/getopt_long.c | 553 ---------------------------- third_party/getopt/initgetopt.S | 3 +- tool/build/calculator.inc | 10 +- tool/viz/printvideo.c | 10 - 27 files changed, 389 insertions(+), 1478 deletions(-) delete mode 100644 examples/fld.c create mode 100644 third_party/chibicc/file.c create mode 100644 third_party/chibicc/file.h delete mode 100644 third_party/getopt/getopt_long.3 delete mode 100644 third_party/getopt/getopt_long.c diff --git a/Makefile b/Makefile index 06465884..f865fae0 100644 --- a/Makefile +++ b/Makefile @@ -312,8 +312,12 @@ o/cosmopolitan.h: \ $(foreach x,$(COSMOPOLITAN_HEADERS),$($(x)_HDRS)) @ACTION=ROLLUP TARGET=$@ build/do $^ >$@ -o/cosmopolitan.html: o/$(MODE)/third_party/chibicc/chibicc.com.dbg - o/$(MODE)/third_party/chibicc/chibicc.com.dbg -J -fno-common -include libc/integral/normalize.inc -o $@ $(filter-out %.s,$(foreach x,$(COSMOPOLITAN_OBJECTS),$($(x)_SRCS))) +o/cosmopolitan.html: \ + o/$(MODE)/third_party/chibicc/chibicc.com.dbg \ + $(filter-out %.s,$(foreach x,$(COSMOPOLITAN_OBJECTS),$($(x)_SRCS))) + o/$(MODE)/third_party/chibicc/chibicc.com.dbg -J \ + -fno-common -include libc/integral/normalize.inc -o $@ \ + $(filter-out %.s,$(foreach x,$(COSMOPOLITAN_OBJECTS),$($(x)_SRCS))) # UNSPECIFIED PREREQUISITES TUTORIAL # diff --git a/examples/examples.mk b/examples/examples.mk index 5a51e2cd..4ca27f10 100644 --- a/examples/examples.mk +++ b/examples/examples.mk @@ -54,6 +54,7 @@ EXAMPLES_DIRECTDEPS = \ LIBC_NT_KERNELBASE \ LIBC_NT_NTDLL \ LIBC_NT_USER32 \ + LIBC_NT_WS2_32 \ LIBC_OHMYPLUS \ LIBC_RAND \ LIBC_RUNTIME \ diff --git a/examples/fld.c b/examples/fld.c deleted file mode 100644 index f18c1452..00000000 --- a/examples/fld.c +++ /dev/null @@ -1,120 +0,0 @@ -#if 0 -/*─────────────────────────────────────────────────────────────────╗ -│ To the extent possible under law, Justine Tunney has waived │ -│ all copyright and related or neighboring rights to this file, │ -│ as it is written in the following disclaimers: │ -│ • http://unlicense.org/ │ -│ • http://creativecommons.org/publicdomain/zero/1.0/ │ -╚─────────────────────────────────────────────────────────────────*/ -#endif -#include "libc/bits/bits.h" -#include "libc/inttypes.h" -#include "libc/literal.h" -#include "libc/math.h" -#include "libc/stdio/stdio.h" -#include "libc/str/str.h" - -static const char kHeaderBin[] = "\ -/\t┌sign\n\ -/\t│ ┌exponent\n\ -/\t│ │ ┌intpart\n\ -/\t│ │ │ ┌fraction\n\ -/\t│ │ │ │\n\ -/\t│┌┴────────────┐│┌┴────────────────────────────────────────────────────────────┐\n"; - -static const char kHeaderHex[] = "\ -/\t ┌sign/exponent\n\ -/\t │ ┌intpart/fraction\n\ -/\t │ │\n\ -/\t┌┴─┐┌┴─────────────┐\n"; - -void dobin(const char *op, long double x, FILE *f) { - uint16_t hi; - uint64_t lo; - uint8_t buf[16]; - memcpy(buf, &x, sizeof(x)); - memcpy(&lo, &buf[0], sizeof(lo)); - memcpy(&hi, &buf[8], sizeof(hi)); - fprintf(f, "/\t%016" PRIb16 "%064" PRIb64 " %-8s % 17.14Lf\n", hi, lo, op, x); -} - -void dohex(const char *op, long double x, FILE *f) { - uint16_t hi; - uint64_t lo; - uint8_t buf[16]; - memcpy(buf, &x, sizeof(x)); - memcpy(&lo, &buf[0], sizeof(lo)); - memcpy(&hi, &buf[8], sizeof(hi)); - fprintf(f, "/\t%04" PRIx16 "%016" PRIx64 " %-8s % 17.14Lf\n", hi, lo, op, x); -} - -#define DOBIN(OP) \ - dobin(" " #OP, OP(), stdout); \ - dobin("-" #OP, -OP(), stdout) - -#define DOHEX(OP) \ - dohex(" " #OP, OP(), stdout); \ - dohex("-" #OP, -OP(), stdout) - -int main(int argc, char *argv[]) { - fputs(kHeaderBin, stdout); - DOBIN(fldz); - DOBIN(fld1); - DOBIN(fldpi); - DOBIN(fldl2t); - DOBIN(fldlg2); - DOBIN(fldln2); - DOBIN(fldl2e); - fputc('\n', stdout); - fputs(kHeaderHex, stdout); - DOHEX(fldz); - DOHEX(fld1); - DOHEX(fldpi); - DOHEX(fldl2t); - DOHEX(fldlg2); - DOHEX(fldln2); - DOHEX(fldl2e); - return 0; -} - -// clang-format off -// -// ┌sign -// │ ┌exponent -// │ │ ┌intpart -// │ │ │ ┌fraction -// │ │ │ │ -// │┌┴────────────┐│┌┴────────────────────────────────────────────────────────────┐ -// 00000000000000000000000000000000000000000000000000000000000000000000000000000000 fldz 0.0000000000000000000 -// 10000000000000000000000000000000000000000000000000000000000000000000000000000000 -fldz -0.0000000000000000000 -// 00111111111111111000000000000000000000000000000000000000000000000000000000000000 fld1 1.0000000000000000000 -// 10111111111111111000000000000000000000000000000000000000000000000000000000000000 -fld1 -1.0000000000000000000 -// 01000000000000001100100100001111110110101010001000100001011010001100001000110101 fldpi 3.1415926540000000000 -// 11000000000000001100100100001111110110101010001000100001011010001100001000110101 -fldpi -3.1415926540000000000 -// 01000000000000001101010010011010011110000100101111001101000110111000101011111110 fldl2t 3.3219280950000000000 -// 11000000000000001101010010011010011110000100101111001101000110111000101011111110 -fldl2t -3.3219280950000000000 -// 00111111111111011001101000100000100110101000010011111011110011111111011110011001 fldlg2 0.3010299960000000000 -// 10111111111111011001101000100000100110101000010011111011110011111111011110011001 -fldlg2 -0.3010299960000000000 -// 00111111111111101011000101110010000101111111011111010001110011110111100110101100 fldln2 0.6931471810000000000 -// 10111111111111101011000101110010000101111111011111010001110011110111100110101100 -fldln2 -0.6931471810000000000 -// 00111111111111111011100010101010001110110010100101011100000101111111000010111100 fldl2e 1.4426950410000000000 -// 10111111111111111011100010101010001110110010100101011100000101111111000010111100 -fldl2e -1.4426950410000000000 -// -// ┌sign/exponent -// │ ┌intpart/fraction -// │ │ -// ┌┴─┐┌┴─────────────┐ -// 00000000000000000000 fldz 0.0000000000000000000 -// 80000000000000000000 -fldz -0.0000000000000000000 -// 3fff8000000000000000 fld1 1.0000000000000000000 -// bfff8000000000000000 -fld1 -1.0000000000000000000 -// 4000c90fdaa22168c235 fldpi 3.1415926540000000000 -// c000c90fdaa22168c235 -fldpi -3.1415926540000000000 -// 4000d49a784bcd1b8afe fldl2t 3.3219280950000000000 -// c000d49a784bcd1b8afe -fldl2t -3.3219280950000000000 -// 3ffd9a209a84fbcff799 fldlg2 0.3010299960000000000 -// bffd9a209a84fbcff799 -fldlg2 -0.3010299960000000000 -// 3ffeb17217f7d1cf79ac fldln2 0.6931471810000000000 -// bffeb17217f7d1cf79ac -fldln2 -0.6931471810000000000 -// 3fffb8aa3b295c17f0bc fldl2e 1.4426950410000000000 -// bfffb8aa3b295c17f0bc -fldl2e -1.4426950410000000000 diff --git a/libc/bits/xmmintrin.internal.h b/libc/bits/xmmintrin.internal.h index 8d7c9d08..b5e073c6 100644 --- a/libc/bits/xmmintrin.internal.h +++ b/libc/bits/xmmintrin.internal.h @@ -98,25 +98,37 @@ typedef float __m128_u _Vector_size(16) forcealign(1) mayalias; │ cosmopolitan § it's a trap! » sse » scalar ops ─╬─│┼ ╚────────────────────────────────────────────────────────────────────────────│*/ -forceinline __m128 _mm_add_ss(__m128 m128_0, __m128 m128_1) { - m128_0[0] += m128_1[0]; - return m128_0; -} +#define _mm_add_ss(m128_0, m128_1) \ + ({ \ + __m128 a = m128_0; \ + __m128 b = m128_0; \ + a[0] += b[0]; \ + a; \ + }) -forceinline __m128 _mm_sub_ss(__m128 m128_0, __m128 m128_1) { - m128_0[0] -= m128_1[0]; - return m128_0; -} +#define _mm_sub_ss(m128_0, m128_1) \ + ({ \ + __m128 a = m128_0; \ + __m128 b = m128_0; \ + a[0] -= b[0]; \ + a; \ + }) -forceinline __m128 _mm_mul_ss(__m128 m128_0, __m128 m128_1) { - m128_0[0] *= m128_1[0]; - return m128_0; -} +#define _mm_mul_ss(m128_0, m128_1) \ + ({ \ + __m128 a = m128_0; \ + __m128 b = m128_0; \ + a[0] *= b[0]; \ + a; \ + }) -forceinline __m128 _mm_div_ss(__m128 m128_0, __m128 m128_1) { - m128_0[0] /= m128_1[0]; - return m128_0; -} +#define _mm_div_ss(m128_0, m128_1) \ + ({ \ + __m128 a = m128_0; \ + __m128 b = m128_0; \ + a[0] /= b[0]; \ + a; \ + }) #define _mm_rcp_ss(M128) __builtin_ia32_rcpss((__v4sf)(M128)) /*~1/x*/ #define _mm_sqrt_ss(M128) __builtin_ia32_sqrtss((__v4sf)(M128)) /*sqrt𝑥*/ diff --git a/libc/integral/lp64arg.inc b/libc/integral/lp64arg.inc index ece9f466..669ec7c0 100644 --- a/libc/integral/lp64arg.inc +++ b/libc/integral/lp64arg.inc @@ -5,13 +5,13 @@ #define va_end(AP) #define va_copy(DST, SRC) ((DST)[0] = (SRC)[0]) -#define va_start(AP, LAST) \ - do { \ - *(AP) = *(struct __va *)__va_area__; \ +#define va_start(AP, LAST) \ + do { \ + *(AP) = *(struct __va_list *)__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]; +typedef struct __va_list va_list[1]; diff --git a/libc/log/vflogf.c b/libc/log/vflogf.c index db1f9345..19824ee3 100644 --- a/libc/log/vflogf.c +++ b/libc/log/vflogf.c @@ -73,7 +73,7 @@ void vflogf_onfail(FILE *f) { fseek(f, SEEK_SET, 0); f->beg = f->end = 0; clearerr(f); - (fprintf)(f, "performed emergency log truncation: %s\r\n", strerror(err)); + (fprintf)(f, "performed emergency log truncation: %s\n", strerror(err)); } } @@ -112,10 +112,10 @@ void(vflogf)(unsigned level, const char *file, int line, FILE *f, } (vfprintf)(f, fmt, va); va_end(va); - fputs("\r\n", f); + fputs("\n", f); if (level == kLogFatal) { __start_fatal(file, line); - (dprintf)(STDERR_FILENO, "fatal error see logfile\r\n"); + (dprintf)(STDERR_FILENO, "fatal error see logfile\n"); __die(); unreachable; } diff --git a/libc/math.h b/libc/math.h index f17ad172..a1e6396f 100644 --- a/libc/math.h +++ b/libc/math.h @@ -285,28 +285,6 @@ void sincos(double, double *, double *); void sincosf(float, float *, float *); void sincosl(long double, long double *, long double *); -/*───────────────────────────────────────────────────────────────────────────│─╗ -│ cosmopolitan § mathematics » x87 ─╬─│┼ -╚────────────────────────────────────────────────────────────────────────────│*/ - -#define fldz() __X87_CONST(fldz, 0x0p+0) -#define fld1() __X87_CONST(fld1, 0x8p-3) -#define fldpi() __X87_CONST(fldpi, M_PI) -#define fldl2t() __X87_CONST(fldl2t, M_LOG2_10) -#define fldlg2() __X87_CONST(fldlg2, M_LOG10_2) -#define fldln2() __X87_CONST(fldln2, M_LN2) -#define fldl2e() __X87_CONST(fldl2e, M_LOG2E) -#ifdef __x86__ -#define __X87_CONST(OP, VALUE) \ - ({ \ - long double St0##OP; \ - asm(#OP : "=t"(St0##OP)); \ - St0##OP; \ - }) -#else -#define __X87_CONST(OP, VALUE) VALUE -#endif - COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_LIBC_MATH_H_ */ diff --git a/libc/nexgen32e/bsrmax.S b/libc/nexgen32e/bsrmax.S index be493e34..3d22aaa8 100644 --- a/libc/nexgen32e/bsrmax.S +++ b/libc/nexgen32e/bsrmax.S @@ -21,15 +21,15 @@ / Returns binary logarithm of integer 𝑥. / -/ uint32 𝑥 bsf(𝑥) tzcnt(𝑥) ffs(𝑥) bsr(𝑥) lzcnt(𝑥) -/ 0x00000000 wut 32 0 wut 32 -/ 0x00000001 0 0 1 0 31 -/ 0x80000001 0 0 1 31 0 -/ 0x80000000 31 31 32 31 0 -/ 0x00000010 4 4 5 4 27 -/ 0x08000010 4 4 5 27 4 -/ 0x08000000 27 27 28 27 4 -/ 0xffffffff 0 0 1 31 0 +/ uint32 𝑥 bsf(𝑥) tzcnt(𝑥) ffs(𝑥) bsr(𝑥) lzcnt(𝑥) +/ 0x00000000 wut 32 0 wut 32 +/ 0x00000001 0 0 1 0 31 +/ 0x80000001 0 0 1 31 0 +/ 0x80000000 31 31 32 31 0 +/ 0x00000010 4 4 5 4 27 +/ 0x08000010 4 4 5 27 4 +/ 0x08000000 27 27 28 27 4 +/ 0xffffffff 0 0 1 31 0 / / @param rsi:rdi is 128-bit unsigned 𝑥 value / @return eax number in range [0,128) or undef if 𝑥 is 0 diff --git a/libc/nt/winsock.h b/libc/nt/winsock.h index 2d92713b..5cad3bc9 100644 --- a/libc/nt/winsock.h +++ b/libc/nt/winsock.h @@ -409,7 +409,7 @@ int WSARecvFrom(uint64_t s, const struct NtIovec *out_lpBuffers, uint32_t *inout_fromsockaddrlen, struct NtOverlapped *opt_inout_lpOverlapped, const NtWsaOverlappedCompletionRoutine opt_lpCompletionRoutine) - paramsnonnull((2, 5, 6, 7)); + paramsnonnull((2, 5)); int WSARecvDisconnect(uint64_t s, const struct NtIovec *opt_lpInboundDisconnectData); diff --git a/libc/runtime/valist.c b/libc/runtime/valist.c index 18d924b4..8a84c20a 100644 --- a/libc/runtime/valist.c +++ b/libc/runtime/valist.c @@ -20,13 +20,13 @@ #include "libc/macros.h" #include "libc/runtime/valist.h" -static void *__va_arg_mem(struct __va *ap, size_t sz, size_t align) { +static void *__va_arg_mem(struct __va_list *ap, size_t sz, size_t align) { void *r = (void *)ROUNDUP((intptr_t)ap->overflow_arg_area, align); ap->overflow_arg_area = (void *)ROUNDUP((intptr_t)r + sz, 8); return r; } -void *__va_arg(struct __va *ap, size_t sz, unsigned align, unsigned k) { +void *__va_arg(struct __va_list *ap, size_t sz, unsigned align, unsigned k) { void *r; switch (k) { case 0: diff --git a/libc/runtime/valist.h b/libc/runtime/valist.h index def8a2dd..73ae2cc2 100644 --- a/libc/runtime/valist.h +++ b/libc/runtime/valist.h @@ -3,14 +3,14 @@ #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ -struct __va { +struct __va_list { uint32_t gp_offset; uint32_t fp_offset; void *overflow_arg_area; void *reg_save_area; }; -void *__va_arg(struct __va *, size_t, unsigned, unsigned); +void *__va_arg(struct __va_list *, size_t, unsigned, unsigned); COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ diff --git a/third_party/chibicc/as.c b/third_party/chibicc/as.c index b4c58e5d..99136b5e 100644 --- a/third_party/chibicc/as.c +++ b/third_party/chibicc/as.c @@ -27,6 +27,8 @@ #include "libc/stdio/stdio.h" #include "libc/str/str.h" #include "libc/sysv/consts/o.h" +#include "libc/x/x.h" +#include "third_party/chibicc/file.h" #include "third_party/gdtoa/gdtoa.h" #include "tool/build/lib/elfwriter.h" @@ -443,49 +445,6 @@ static unsigned Hash(const void *p, unsigned long n) { return MAX(1, h); } -static bool StartsWith(const char *s, const char *prefix) { - for (;;) { - if (!*prefix) return true; - if (!*s) return false; - if (*s++ != *prefix++) return false; - } -} - -static bool EndsWith(const char *s, const char *suffix) { - size_t n, m; - n = strlen(s); - m = strlen(suffix); - if (m > n) return false; - return !memcmp(s + n - m, suffix, m); -} - -static char *Format(const char *fmt, ...) { - char *res; - va_list va; - va_start(va, fmt); - vasprintf(&res, fmt, va); - va_end(va); - return res; -} - -static char *DirName(const char *path) { - return dirname(strdup(path)); -} - -static char *JoinPaths(const char *path, const char *other) { - if (!*other) { - return strdup(path); - } else if (!*path) { - return strdup(other); - } else if (StartsWith(other, "/") || !strcmp(path, ".")) { - return strdup(other); - } else if (EndsWith(path, "/")) { - return Format("%s%s", path, other); - } else { - return Format("%s/%s", path, other); - } -} - static bool IsPunctMergeable(int c) { switch (c) { case ';': @@ -613,11 +572,11 @@ static void ReadFlags(struct As *a, int argc, char *argv[]) { for (i = 1; i < argc; ++i) { if (!strcmp(argv[i], "-o")) { a->outpath = StrDup(a, argv[++i]); - } else if (StartsWith(argv[i], "-o")) { + } else if (startswith(argv[i], "-o")) { a->outpath = StrDup(a, argv[i] + 2); } else if (!strcmp(argv[i], "-I")) { SaveString(&a->incpaths, strdup(argv[++i])); - } else if (StartsWith(argv[i], "-I")) { + } else if (startswith(argv[i], "-I")) { SaveString(&a->incpaths, strdup(argv[i] + 2)); } else if (!strcmp(argv[i], "-Z")) { a->inhibiterr = true; @@ -677,72 +636,6 @@ static int ReadCharLiteral(struct Slice *buf, int c, char *p, int *i) { } } -static void CanonicalizeNewline(char *p) { - int i = 0, j = 0; - while (p[i]) { - if (p[i] == '\r' && p[i + 1] == '\n') { - i += 2; - p[j++] = '\n'; - } else if (p[i] == '\r') { - i++; - p[j++] = '\n'; - } else { - p[j++] = p[i++]; - } - } - p[j] = '\0'; -} - -static void RemoveBackslashNewline(char *p) { - int i, j, n; - for (i = j = n = 0; p[i];) { - if (p[i] == '\\' && p[i + 1] == '\n') { - i += 2; - n++; - } else if (p[i] == '\n') { - p[j++] = p[i++]; - for (; n > 0; n--) p[j++] = '\n'; - } else { - p[j++] = p[i++]; - } - } - for (; n > 0; n--) p[j++] = '\n'; - p[j] = '\0'; -} - -static char *ReadFile(const char *path) { - char *p; - FILE *fp; - int buflen, nread, end, n; - if (!strcmp(path, "-")) { - fp = stdin; - } else { - fp = fopen(path, "r"); - if (!fp) return NULL; - } - buflen = 4096; - nread = 0; - p = calloc(1, buflen); - for (;;) { - end = buflen - 2; - n = fread(p + nread, 1, end - nread, fp); - if (n == 0) break; - nread += n; - if (nread == end) { - buflen *= 2; - p = realloc(p, buflen); - } - } - if (fp != stdin) fclose(fp); - if (nread > 0 && p[nread - 1] == '\\') { - p[nread - 1] = '\n'; - } else if (nread == 0 || p[nread - 1] != '\n') { - p[nread++] = '\n'; - } - p[nread] = '\0'; - return p; -} - static void PrintLocation(struct As *a) { fprintf(stderr, "%s:%d:: ", a->strings.p[a->sauces.p[a->things.p[a->i].s].path], @@ -768,7 +661,7 @@ static char *FindInclude(struct As *a, const char *file) { char *path; struct stat st; for (i = 0; i < a->incpaths.n; ++i) { - path = JoinPaths(a->incpaths.p[i], file); + path = xjoinpaths(a->incpaths.p[i], file); if (stat(path, &st) != -1 && S_ISREG(st.st_mode)) return path; free(path); } @@ -780,10 +673,10 @@ static void Tokenize(struct As *a, int path) { char *p, *path2; struct Slice buf; bool bol, isfloat, isfpu; - p = SaveString(&a->strings, ReadFile(a->strings.p[path])); - if (!memcmp(p, "\357\273\277", 3)) p += 3; - CanonicalizeNewline(p); - RemoveBackslashNewline(p); + p = SaveString(&a->strings, read_file(a->strings.p[path])); + p = skip_bom(p); + canonicalize_newline(p); + remove_backslash_newline(p); line = 1; bol = true; while ((c = *p)) { @@ -1031,7 +924,7 @@ static void OnSymbol(struct As *a, int name) { static void OnLocalLabel(struct As *a, int id) { int i; char *name; - name = Format(".Label.%d", a->counter++); + name = xasprintf(".Label.%d", a->counter++); SaveString(&a->strings, name); AppendSlice(a); a->slices.p[a->slices.n - 1].p = name; @@ -1796,13 +1689,13 @@ static int GrabSection(struct As *a, int name, int flags, int type) { static void OnSection(struct As *a, struct Slice s) { int name, flags, type; name = SliceDup(a, GetSlice(a)); - if (StartsWith(a->strings.p[name], ".text")) { + if (startswith(a->strings.p[name], ".text")) { flags = SHF_ALLOC | SHF_EXECINSTR; type = SHT_PROGBITS; - } else if (StartsWith(a->strings.p[name], ".data")) { + } else if (startswith(a->strings.p[name], ".data")) { flags = SHF_ALLOC | SHF_WRITE; type = SHT_PROGBITS; - } else if (StartsWith(a->strings.p[name], ".bss")) { + } else if (startswith(a->strings.p[name], ".bss")) { flags = SHF_ALLOC | SHF_WRITE; type = SHT_NOBITS; } else { @@ -2372,7 +2265,7 @@ static noinline void OpBsu(struct As *a, struct Slice opname, int op) { OpBsuImpl(a, opname, op); } -static int OpF6(struct As *a, struct Slice s, int reg) { +static noinline int OpF6Impl(struct As *a, struct Slice s, int reg) { int modrm, imm, disp; modrm = ParseModrm(a, &disp); reg |= GetOpSize(a, s, modrm, 1) << 3; @@ -2380,6 +2273,10 @@ static int OpF6(struct As *a, struct Slice s, int reg) { return reg; } +static noinline int OpF6(struct As *a, struct Slice s, int reg) { + return OpF6Impl(a, s, reg); +} + static void OnTest(struct As *a, struct Slice s) { int reg, modrm, imm, disp; if (IsPunct(a, a->i, '$')) { @@ -2571,8 +2468,8 @@ static bool HasXmmOnLine(struct As *a) { int i; for (i = 0; !IsPunct(a, a->i + i, ';'); ++i) { if (IsSlice(a, a->i + i) && a->slices.p[a->things.p[a->i + i].i].n >= 4 && - (StartsWith(a->slices.p[a->things.p[a->i + i].i].p, "xmm") || - StartsWith(a->slices.p[a->things.p[a->i + i].i].p, "%xmm"))) { + (startswith(a->slices.p[a->things.p[a->i + i].i].p, "xmm") || + startswith(a->slices.p[a->things.p[a->i + i].i].p, "%xmm"))) { return true; } } @@ -4002,7 +3899,7 @@ void Assembler(int argc, char *argv[]) { a = NewAssembler(); ReadFlags(a, argc, argv); SaveString(&a->incpaths, strdup(".")); - SaveString(&a->incpaths, DirName(a->strings.p[a->inpath])); + SaveString(&a->incpaths, xdirname(a->strings.p[a->inpath])); Tokenize(a, a->inpath); /* PrintThings(a); */ Assemble(a); diff --git a/third_party/chibicc/chibicc.h b/third_party/chibicc/chibicc.h index d9beb355..cfeb24cd 100644 --- a/third_party/chibicc/chibicc.h +++ b/third_party/chibicc/chibicc.h @@ -130,9 +130,6 @@ Token *tokenize_string_literal(Token *, Type *); bool consume(Token **, Token *, char *, size_t); bool equal(Token *, char *, size_t); void convert_pp_tokens(Token *); -void remove_backslash_newline(char *); -void canonicalize_newline(char *); -char *read_file(char *); int read_escaped_char(char **, char *); #define UNREACHABLE() error("internal error at %s:%d", __FILE__, __LINE__) @@ -450,6 +447,7 @@ struct Type { int align; // alignment bool is_unsigned; // unsigned or signed bool is_atomic; // true if _Atomic + bool is_const; // const bool is_ms_abi; // microsoft abi Type *origin; // for type compatibility check // Pointer-to or array-of type. We intentionally use the same member diff --git a/third_party/chibicc/dox1.c b/third_party/chibicc/dox1.c index ffefad3a..89b91f9b 100644 --- a/third_party/chibicc/dox1.c +++ b/third_party/chibicc/dox1.c @@ -79,7 +79,8 @@ static void SerializeJavadown(struct Buffer *buf, struct Javadown *jd) { } static char *DescribeScalar(struct Type *ty, char *name) { - return xasprintf("%s%s%s", ty->is_atomic ? "_Atomic " : "", + return xasprintf("%s%s%s%s", ty->is_atomic ? "_Atomic " : "", + ty->is_const ? "const " : "", ty->is_unsigned ? "unsigned " : "", name); } @@ -134,7 +135,7 @@ static char *DescribeType(struct Type *ty) { return strdup("ANONYMOUS-UNION"); } default: - return "UNKNOWN"; + return strdup("UNKNOWN"); } } @@ -178,6 +179,7 @@ static void SerializeDox(struct DoxWriter *dox, Obj *prog) { SerializeStr(&dox->buf, GetFileName(dox->objects.p[i])); SerializeInt(&dox->buf, GetLine(dox->objects.p[i])); SerializeInt(&dox->buf, dox->objects.p[i]->is_function); + SerializeInt(&dox->buf, dox->objects.p[i]->ty->is_variadic); SerializeInt(&dox->buf, dox->objects.p[i]->is_weak); SerializeInt(&dox->buf, dox->objects.p[i]->is_inline); SerializeInt(&dox->buf, dox->objects.p[i]->is_noreturn); @@ -248,6 +250,7 @@ static void SerializeAsmdown(struct DoxWriter *dox, struct Asmdown *ad, SerializeStr(&dox->buf, filename); SerializeInt(&dox->buf, ad->symbols.p[i].line); SerializeInt(&dox->buf, true); // TODO: is_function + SerializeInt(&dox->buf, false); // is_variadic SerializeInt(&dox->buf, false); // TODO: is_weak SerializeInt(&dox->buf, false); // is_inline SerializeInt(&dox->buf, false); // is_noreturn diff --git a/third_party/chibicc/dox2.c b/third_party/chibicc/dox2.c index 58816977..64863c0d 100644 --- a/third_party/chibicc/dox2.c +++ b/third_party/chibicc/dox2.c @@ -47,6 +47,7 @@ struct Dox { char *path; int line; bool is_function; + bool is_variadic; bool is_weak; bool is_inline; bool is_noreturn; @@ -177,6 +178,7 @@ static void DeserializeObject(struct Dox *dox, struct DoxObject *o) { o->path = DeserializeStr(dox); o->line = DeserializeInt(dox); o->is_function = DeserializeInt(dox); + o->is_variadic = DeserializeInt(dox); o->is_weak = DeserializeInt(dox); o->is_inline = DeserializeInt(dox); o->is_noreturn = DeserializeInt(dox); @@ -210,7 +212,7 @@ static void DeserializeMacro(struct Dox *dox, struct DoxMacro *m) { } } -static void DeserializeDox(struct Dox *dox) { +static void DeserializeDox(struct Dox *dox, const char *path) { int i, j, n; i = dox->objects.n; n = DeserializeInt(dox); @@ -242,7 +244,7 @@ static void ReadDox(struct Dox *dox, const StringArray *files) { CHECK_NE(MAP_FAILED, (map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0))); dox->p = map; - DeserializeDox(dox); + DeserializeDox(dox, files->data[i]); munmap(map, st.st_size); } close(fd); @@ -317,15 +319,19 @@ static void IndexDox(struct Dox *dox) { CompareDoxIndexEntry, dox); } +/** + * Escapes HTML entities and interprets basic Markdown syntax. + */ static void PrintText(FILE *f, const char *s) { int c; - bool bol, pre, ul0, ul2, bt1, bt2; - for (bt1 = bt2 = ul2 = ul0 = pre = false, bol = true;;) { + bool bol, pre, ul0, ul2, ol0, ol2, bt1, bt2; + for (bt1 = bt2 = ul2 = ul0 = ol2 = ol0 = pre = false, bol = true;;) { switch ((c = *s++)) { case '\0': if (bt1 || bt2) fprintf(f, ""); if (pre) fprintf(f, ""); if (ul0 || ul2) fprintf(f, ""); + if (ol0 || ol2) fprintf(f, ""); return; case '&': fprintf(f, "&"); @@ -368,14 +374,19 @@ static void PrintText(FILE *f, const char *s) { bol = false; break; case '\n': - if (!pre && !ul0 && !ul2 && *s == '\n') { + if (!pre && !ul0 && !ul2 && !ol0 && !ol2 && *s == '\n') { fprintf(f, "\n

"); bol = true; - } else if (pre && s[0] != '\n' && - (s[0] != ' ' || s[1] != ' ' || s[2] != ' ' || s[3] != ' ')) { - fprintf(f, "\n"); - pre = false; - bol = true; + } else if (pre && s[0] != '\n') { + if (s[0] != ' ' || s[1] != ' ' || s[2] != ' ' || s[3] != ' ') { + fprintf(f, "\n"); + pre = false; + bol = true; + } else { + fprintf(f, "\n"); + bol = false; + s += 4; + } } else if (ul0 && s[0] == '-' && s[1] == ' ') { fprintf(f, "\n

  • "); s += 2; @@ -394,29 +405,65 @@ static void PrintText(FILE *f, const char *s) { fprintf(f, "\n\n"); bol = true; ul2 = false; + } else if (ol0 && ('0' <= s[0] && s[0] <= '9') && s[1] == '.' && + s[2] == ' ') { + fprintf(f, "\n
  • "); + s += 3; + bol = false; + } else if (ol2 && s[0] == ' ' && s[1] == ' ' && + ('0' <= s[2] && s[2] <= '9') && s[3] == '.' && s[3] == ' ') { + fprintf(f, "\n
  • "); + s += 5; + bol = false; + } else if (ol0 && s[0] != '\n' && (s[0] != ' ' || s[1] != ' ')) { + fprintf(f, "\n\n"); + bol = true; + ol0 = false; + } else if (ol2 && s[0] != '\n' && + (s[0] != ' ' || s[1] != ' ' || s[2] != ' ' || s[3] != ' ')) { + fprintf(f, "\n\n"); + bol = true; + ol2 = false; } else { fprintf(f, "\n"); bol = true; } break; case '-': - if (bol && !ul0 && !ul2 && s[0] == ' ') { + if (bol && !ul0 && !ul2 && !ol0 && !ol2 && s[0] == ' ') { ul0 = true; fprintf(f, "