Browse Source

Auto-generate some documentation

main
Justine Tunney 11 months ago
parent
commit
13437dd19b
  1. 5
      Makefile
  2. 21
      ape/config.h
  3. 4
      ape/relocations.h
  4. 14
      libc/alg/memmem.c
  5. 3
      libc/bits/abs.c
  6. 3
      libc/bits/bitreverse16.c
  7. 3
      libc/bits/bitreverse32.c
  8. 3
      libc/bits/bitreverse64.c
  9. 3
      libc/bits/bitreverse8.c
  10. 9
      libc/bits/emptytonull.c
  11. 3
      libc/bits/firstnonnull.c
  12. 5
      libc/bits/gray.c
  13. 1
      libc/bits/hamming.c
  14. 6
      libc/bits/hilbert.c
  15. 7
      libc/bits/isempty.c
  16. 2
      libc/bits/morton.c
  17. 9
      libc/bits/nulltoempty.c
  18. 3
      libc/bits/popcnt.c
  19. 6
      libc/bits/pushpop.h
  20. 7
      libc/bits/rounddown.c
  21. 3
      libc/bits/roundup.c
  22. 2
      libc/bits/roundup2log.c
  23. 2
      libc/bits/roundup2pow.c
  24. 5
      libc/bits/ungray.c
  25. 1
      libc/bits/unmorton.c
  26. 12
      libc/calls/fchmod.c
  27. 2
      libc/calls/fcntl.c
  28. 14
      libc/calls/hefty/dirstream.c
  29. 25
      libc/calls/hefty/spawnve.c
  30. 2
      libc/calls/isatty.c
  31. 55
      libc/calls/kntprioritycombos.c
  32. 16
      libc/calls/readansi.c
  33. 31
      libc/calls/setitimer.c
  34. 8
      libc/calls/sigaction.c
  35. 6
      libc/calls/sigprocmask.c
  36. 2
      libc/dce.h
  37. 2
      libc/elf/struct/rela.h
  38. 34
      libc/fmt/bing.c
  39. 237
      libc/integral/c.inc
  40. 2
      libc/integral/lp64.inc
  41. 9
      libc/integral/normalize.inc
  42. 5
      libc/math.h
  43. 2
      libc/mem/vasprintf.c
  44. 2
      libc/nexgen32e/ffs.S
  45. 24
      libc/nexgen32e/ffs.h
  46. 2
      libc/nexgen32e/ffsl.S
  47. 7
      libc/nexgen32e/kcpuids.h
  48. 8
      libc/nt/thunk/msabi.h
  49. 2
      libc/rand/rand32.c
  50. 6
      libc/runtime/mmap.c
  51. 36
      libc/runtime/winmain.greg.c
  52. 4
      libc/stdio/g_stderr.c
  53. 4
      libc/stdio/g_stdin.c
  54. 4
      libc/stdio/g_stdout.c
  55. 3
      libc/str/isalnum.c
  56. 3
      libc/str/isalpha.c
  57. 3
      libc/str/isascii.c
  58. 3
      libc/str/isblank.c
  59. 3
      libc/str/iscntrl.c
  60. 3
      libc/str/isdigit.c
  61. 3
      libc/str/islower.c
  62. 3
      libc/str/isspace.c
  63. 3
      libc/str/isupper.c
  64. 3
      libc/str/iswcntrl.c
  65. 3
      libc/str/isxdigit.c
  66. 10
      libc/str/memccpy.c
  67. 1
      libc/str/stpncpy.c
  68. 1
      libc/str/strncpy.c
  69. 2
      libc/testlib/testrunner.c
  70. 14
      libc/time/strftime.c
  71. 2
      libc/unicode/strnwidth.c
  72. 8
      libc/x/unbingbuf.c
  73. 2
      libc/x/unbingstr.c
  74. 12
      libc/x/xjoinpaths.c
  75. 4
      libc/x/xsigaction.c
  76. 45
      test/libc/nexgen32e/ffs_test.c
  77. 230
      test/tool/build/lib/javadown_test.c
  78. 6
      third_party/chibicc/README.cosmo
  79. 184
      third_party/chibicc/as.c
  80. 49
      third_party/chibicc/chibicc.c
  81. 56
      third_party/chibicc/chibicc.h
  82. 1
      third_party/chibicc/chibicc.mk
  83. 36
      third_party/chibicc/codegen.c
  84. 248
      third_party/chibicc/dox1.c
  85. 392
      third_party/chibicc/dox2.c
  86. 107
      third_party/chibicc/parse.c
  87. 44
      third_party/chibicc/preprocess.c
  88. 3
      third_party/chibicc/test/attribute_test.c
  89. 6
      third_party/chibicc/test/vla_test.c
  90. 47
      third_party/chibicc/tokenize.c
  91. 35
      third_party/dlmalloc/mallinfo.c
  92. 14
      third_party/musl/glob.c
  93. 8
      third_party/regex/regcomp.c
  94. 80
      third_party/zlib/zlib.h
  95. 276
      tool/build/lib/javadown.c
  96. 24
      tool/build/lib/javadown.h
  97. 21
      tool/emacs/javadown.el

5
Makefile

@ -303,6 +303,7 @@ COSMOPOLITAN_HEADERS = \
THIRD_PARTY_GDTOA \
THIRD_PARTY_GETOPT \
THIRD_PARTY_MUSL \
THIRD_PARTY_ZLIB \
THIRD_PARTY_REGEX
o/$(MODE)/cosmopolitan.a: $(filter-out o/libc/stubs/exit11.o,$(foreach x,$(COSMOPOLITAN_OBJECTS),$($(x)_OBJS)))
@ -312,6 +313,10 @@ o/cosmopolitan.h: \
$(foreach x,$(COSMOPOLITAN_HEADERS),$($(x)_HDRS))
@ACTION=ROLLUP TARGET=$@ build/do $^ >$@
o/cosmopolitan.html: \
o//third_party/chibicc/chibicc.com.dbg
o//third_party/chibicc/chibicc.com.dbg -J -fno-common -include libc/integral/normalize.inc -o $@ $(filter %.c,$(foreach x,$(COSMOPOLITAN_OBJECTS),$($(x)_SRCS)))
# UNSPECIFIED PREREQUISITES TUTORIAL
#
# A build rule must exist for all files that make needs to consider in

21
ape/config.h

@ -3,11 +3,7 @@
#include "ape/relocations.h"
#include "libc/macros.h"
/**
* @fileverview αcτµαlly pδrταblε εxεcµταblε configuration.
*/
/**
/*
* Post-Initialization Read-Only Code Size Threshold.
*
* An executable needs to have at least this much code, before the
@ -22,9 +18,6 @@
#endif
#endif
/**
* PC Standard I/O Configuration.
*/
#ifndef METAL_STDIN
#define METAL_STDIN COM1
#endif
@ -35,11 +28,6 @@
#define METAL_STDERR COM1
#endif
/**
* PC Display Configuration (MDA/CGA)
* @see www.lammertbies.nl/comm/info/serial-uart.html
* @see ape/lib/vidya.internal.h
*/
#ifndef VIDYA_MODE
#define VIDYA_MODE VIDYA_MODE_CGA
#endif
@ -66,10 +54,6 @@
#define X87_DEFAULT X87_NORMAL
#endif
/**
* Serial Line Configuration (8250 UART 16550)
* @see ape/lib/uart.h
*/
#ifndef UART_BAUD_RATE
#define UART_BAUD_RATE 9600 /* bits per second ∈ [50,115200] */
#endif
@ -93,9 +77,6 @@
#define UART_CONF_LCR 0b01000011
#endif
/**
* eXtreme Low Memory.
*/
#define XLM(VAR) (XLM_BASE_REAL + XLM_##VAR)
#define XLMV(VAR) (__xlm + XLM_##VAR)
#define XLM_BASE_REAL 0x1000

4
ape/relocations.h

@ -19,23 +19,23 @@
#define IMAGE_BASE_PHYSICAL 0x100000
#endif
#ifndef REAL_SCRATCH_AREA
/**
* Location of anything goes memory for real mode.
*
* The MBR won't load program content beyond this address, so we have
* room for buffers, page tables, etc. before we reach the stack frame.
*/
#ifndef REAL_SCRATCH_AREA
#define REAL_SCRATCH_AREA 0x40000
#endif
#ifndef REAL_STACK_FRAME
/**
* Location of real mode 64kb stack frame.
*
* This address was chosen because memory beyond 0x80000 can't be
* accessed safely without consulting e820.
*/
#ifndef REAL_STACK_FRAME
#define REAL_STACK_FRAME 0x70000
#endif

14
libc/alg/memmem.c

@ -21,12 +21,8 @@
#include "libc/macros.h"
#include "libc/mem/mem.h"
#include "libc/str/str.h"
/* clang-format off */
static void KnuthMorrisPrattInit(m, T, W)
ssize_t m, T[m + 1];
const char W[m];
{
static void KnuthMorrisPrattInit(ssize_t m, ssize_t *T, const char *W) {
ssize_t i = 2;
ssize_t j = 0;
T[0] = -1;
@ -43,10 +39,8 @@ static void KnuthMorrisPrattInit(m, T, W)
T[m] = 0;
}
static size_t KnuthMorrisPratt(m, T, W, n, S)
const long n, m, T[m + 1];
const char W[m], S[n];
{
static size_t KnuthMorrisPratt(long m, const long *T, const char *W, long n,
const char *S) {
long i = 0, j = 0;
while (i + j < n) {
if (W[i] == S[i + j]) {
@ -60,8 +54,6 @@ static size_t KnuthMorrisPratt(m, T, W, n, S)
return j;
}
/* clang-format on */
/**
* Searches for fixed-length substring in memory region.
*

3
libc/bits/abs.c

@ -20,6 +20,9 @@
#include "libc/fmt/conv.h"
#include "libc/macros.h"
/**
* Returns absolute value of x.
*/
int(abs)(int x) {
return ABS(x);
}

3
libc/bits/bitreverse16.c

@ -19,6 +19,9 @@
*/
#include "libc/bits/bits.h"
/**
* Reverses bits in 16-bit word.
*/
uint16_t(bitreverse16)(uint16_t x) {
return kReverseBits[x & 0x00FF] << 8 | kReverseBits[(x & 0xFF00) >> 8];
}

3
libc/bits/bitreverse32.c

@ -20,6 +20,9 @@
#include "libc/bits/bits.h"
#include "libc/bits/bswap.h"
/**
* Reverses bits in 32-bit word.
*/
uint32_t(bitreverse32)(uint32_t x) {
x = bswap_32(x);
x = ((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1);

3
libc/bits/bitreverse64.c

@ -20,6 +20,9 @@
#include "libc/bits/bits.h"
#include "libc/bits/bswap.h"
/**
* Reverses bits in 64-bit word.
*/
uint64_t bitreverse64(uint64_t x) {
x = bswap_64(x);
x = ((x & 0xaaaaaaaaaaaaaaaa) >> 1) | ((x & 0x5555555555555555) << 1);

3
libc/bits/bitreverse8.c

@ -19,6 +19,9 @@
*/
#include "libc/bits/bits.h"
/**
* Reverses bits in 8-bit word.
*/
uint8_t(bitreverse8)(uint8_t x) {
return kReverseBits[x];
}

9
libc/bits/emptytonull.c

@ -17,5 +17,12 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/bits/safemacros.internal.h"
const char *emptytonull(const char *s) { return s && !*s ? 0 : s; }
/**
* Returns string where empty string is made null.
* @see nulltoempty()
*/
const char *(emptytonull)(const char *s) {
return s && !*s ? 0 : s;
}

3
libc/bits/firstnonnull.c

@ -19,6 +19,9 @@
*/
#include "libc/runtime/runtime.h"
/**
* Returns a or b or aborts if both are null.
*/
const char *(firstnonnull)(const char *a, const char *b) {
if (a) return a;
if (b) return b;

5
libc/bits/gray.c

@ -19,6 +19,11 @@
*/
#include "libc/bits/bits.h"
/**
* Returns gray code for x.
* @see https://en.wikipedia.org/wiki/Gray_code
* @see ungray()
*/
uint32_t gray(uint32_t x) {
return x ^ (x >> 1);
}

1
libc/bits/hamming.c

@ -21,6 +21,7 @@
/**
* Counts number of different bits.
* @see https://en.wikipedia.org/wiki/Hamming_code
*/
unsigned long hamming(unsigned long x, unsigned long y) {
return popcnt(x ^ y);

6
libc/bits/hilbert.c

@ -36,7 +36,8 @@ static axdx_t RotateQuadrant(long n, long y, long x, long ry, long rx) {
/**
* Generates Hilbert space-filling curve.
*
* @see morton()
* @see https://en.wikipedia.org/wiki/Hilbert_curve
* @see unhilbert()
*/
long hilbert(long n, long y, long x) {
axdx_t m;
@ -56,7 +57,8 @@ long hilbert(long n, long y, long x) {
/**
* Decodes Hilbert space-filling curve.
*
* @see unmorton()
* @see https://en.wikipedia.org/wiki/Hilbert_curve
* @see hilbert()
*/
axdx_t unhilbert(long n, long i) {
axdx_t m;

7
libc/bits/isempty.c

@ -18,4 +18,9 @@
02110-1301 USA
*/
bool isempty(const char *s) { return !s || !*s; }
/**
* Returns true if s is empty string or null.
*/
bool isempty(const char *s) {
return !s || !*s;
}

2
libc/bits/morton.c

@ -21,6 +21,8 @@
/**
* Interleaves bits.
* @see https://en.wikipedia.org/wiki/Z-order_curve
* @see unmorton()
*/
unsigned long(morton)(unsigned long y, unsigned long x) {
x = (x | x << 020) & 0x0000FFFF0000FFFF;

9
libc/bits/nulltoempty.c

@ -17,5 +17,12 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/bits/safemacros.internal.h"
const char *nulltoempty(const char *s) { return s ? s : ""; }
/**
* Returns 𝑠 converting null to empty string.
* @see emptytonull()
*/
const char *(nulltoempty)(const char *s) {
return s ? s : "";
}

3
libc/bits/popcnt.c

@ -19,6 +19,9 @@
*/
#include "libc/bits/popcnt.h"
/**
* Returns number of bits set in integer.
*/
uint64_t(popcnt)(uint64_t x) {
x = x - ((x >> 1) & 0x5555555555555555);
x = ((x >> 2) & 0x3333333333333333) + (x & 0x3333333333333333);

6
libc/bits/pushpop.h

@ -3,13 +3,13 @@
#include "libc/macros.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
#if !defined(__GNUC__) || defined(__STRICT_ANSI__)
#define pushpop(x) (x)
#else
/**
* PushPop
* An elegant weapon for a more civilized age.
*/
#if !defined(__GNUC__) || defined(__STRICT_ANSI__)
#define pushpop(x) (x)
#else
#define pushpop(x) \
({ \
typeof(x) Popped; \

7
libc/bits/rounddown.c

@ -19,4 +19,9 @@
*/
#include "libc/macros.h"
long(rounddown)(long w, long k) { return ROUNDDOWN(w, k); }
/**
* Rounds down 𝑤 to next two power 𝑘.
*/
long(rounddown)(long w, long k) {
return ROUNDDOWN(w, k);
}

3
libc/bits/roundup.c

@ -19,6 +19,9 @@
*/
#include "libc/macros.h"
/**
* Rounds up 𝑤 to next two power 𝑘.
*/
long(roundup)(long w, long k) {
return ROUNDUP(w, k);
}

2
libc/bits/roundup2log.c

@ -22,7 +22,7 @@
/**
* Returns 𝑥 rounded up to next two power and log'd.
* @see roundup2pow
* @see roundup2pow()
*/
unsigned long roundup2log(unsigned long x) {
return x > 1 ? (bsrl(x - 1) + 1) : x ? 1 : 0;

2
libc/bits/roundup2pow.c

@ -24,7 +24,7 @@
* Returns 𝑥 rounded up to next two power.
*
* @define (𝑥>02^logx, x=00, 𝑇)
* @see rounddown2pow)()
* @see rounddown2pow()
*/
unsigned long roundup2pow(unsigned long x) {
return x > 1 ? 1ul << (bsrl(x - 1) + 1) : x ? 1 : 0;

5
libc/bits/ungray.c

@ -19,6 +19,11 @@
*/
#include "libc/bits/bits.h"
/**
* Decodes gray code.
* @see https://en.wikipedia.org/wiki/Gray_code
* @see gray()
*/
uint32_t ungray(uint32_t x) {
x ^= x >> 16;
x ^= x >> 8;

1
libc/bits/unmorton.c

@ -35,6 +35,7 @@ static unsigned long GetOddBits(unsigned long x) {
* @param 𝑖 is interleaved index
* @return deinterleaved coordinate {ax := 𝑦, dx := 𝑥}
* @see en.wikipedia.org/wiki/Z-order_curve
* @see morton()
*/
axdx_t(unmorton)(unsigned long i) {
return (axdx_t){GetOddBits(i >> 1), GetOddBits(i)};

12
libc/calls/fchmod.c

@ -25,15 +25,15 @@
/**
* Changes file permissions via open()'d file descriptor, e.g.:
*
* CHECK_NE(-1, chmod("foo/bar.txt", 0644));
* CHECK_NE(-1, chmod("o/default/program.com", 0755));
* CHECK_NE(-1, chmod("privatefolder/", 0700));
* CHECK_NE(-1, chmod("foo/bar.txt", 0644));
* CHECK_NE(-1, chmod("o/default/program.com", 0755));
* CHECK_NE(-1, chmod("privatefolder/", 0700));
*
* The esoteric bits generally available on System V are:
*
* CHECK_NE(-1, chmod("/opt/", 01000)); // sticky bit
* CHECK_NE(-1, chmod("/usr/bin/sudo", 04755)); // setuid bit
* CHECK_NE(-1, chmod("/usr/bin/wall", 02755)); // setgid bit
* CHECK_NE(-1, chmod("/opt/", 01000)); // sticky bit
* CHECK_NE(-1, chmod("/usr/bin/sudo", 04755)); // setuid bit
* CHECK_NE(-1, chmod("/usr/bin/wall", 02755)); // setgid bit
*
* This works on Windows NT if you ignore the error ;-)
*

2
libc/calls/fcntl.c

@ -24,7 +24,7 @@
/**
* Does things with file descriptor, via re-imagined hourglass api, e.g.
*
* CHECK_NE(-1, fcntl(fd, F_SETFD, FD_CLOEXEC));
* CHECK_NE(-1, fcntl(fd, F_SETFD, FD_CLOEXEC));
*
* @param cmd can be F_{GET,SET}{FD,FL}, etc.
* @param arg can be FD_CLOEXEC, etc. depending

14
libc/calls/hefty/dirstream.c

@ -118,13 +118,13 @@ static textwindows noinline struct dirent *readdir$nt(DIR *dir) {
/**
* Opens directory, e.g.
*
* DIR *d;
* struct dirent *e;
* CHECK((d = opendir(path)));
* while ((e = readdir(d))) {
* printf("%s/%s\n", path, e->d_name);
* }
* LOGIFNEG1(closedir(d));
* DIR *d;
* struct dirent *e;
* CHECK((d = opendir(path)));
* while ((e = readdir(d))) {
* printf("%s/%s\n", path, e->d_name);
* }
* LOGIFNEG1(closedir(d));
*
* @returns newly allocated DIR object, or NULL w/ errno
* @errors ENOENT, ENOTDIR, EACCES, EMFILE, ENFILE, ENOMEM

25
libc/calls/hefty/spawnve.c

@ -27,18 +27,18 @@
/**
* Launches program, e.g.
*
* char buf[2];
* int ws, pid, fds[3] = {-1, -1, STDERR_FILENO};
* CHECK_NE(-1, (pid = spawnve(0, fds, commandv("ssh"),
* (char *const[]){"ssh", hostname, "cat", NULL},
* environ)));
* CHECK_EQ(+2, write(fds[0], "hi", 2));
* CHECK_NE(-1, close(fds[0]));
* CHECK_EQ(+2, read(fds[1], buf, 2)));
* CHECK_NE(-1, close(fds[1]));
* CHECK_EQ(+0, memcmp(buf, "hi", 2)));
* CHECK_NE(-1, waitpid(pid, &ws, 0));
* CHECK_EQ(+0, WEXITSTATUS(ws));
* char buf[2];
* int ws, pid, fds[3] = {-1, -1, STDERR_FILENO};
* CHECK_NE(-1, (pid = spawnve(0, fds, commandv("ssh"),
* (char *const[]){"ssh", hostname, "cat", 0},
* environ)));
* CHECK_EQ(+2, write(fds[0], "hi", 2));
* CHECK_NE(-1, close(fds[0]));
* CHECK_EQ(+2, read(fds[1], buf, 2)));
* CHECK_NE(-1, close(fds[1]));
* CHECK_EQ(+0, memcmp(buf, "hi", 2)));
* CHECK_NE(-1, waitpid(pid, &ws, 0));
* CHECK_EQ(+0, WEXITSTATUS(ws));
*
* @param stdiofds may optionally be passed to customize standard i/o
* @param stdiofds[𝑖] may be -1 to receive a pipe() fd
@ -49,6 +49,7 @@
* @param envp[0,n-2] specifies "foo=bar" environment variables
* @param envp[n-1] is NULL
* @return pid of child, or -1 w/ errno
* @deprecated just use vfork() and execve()
*/
int spawnve(unsigned flags, int stdiofds[3], const char *program,
char *const argv[], char *const envp[]) {

2
libc/calls/isatty.c

@ -27,7 +27,7 @@
* @asyncsignalsafe
*/
bool32 isatty(int fd) {
char buf[sizeof(uint16_t) * 4] forcealign(2);
_Alignas(short) char buf[sizeof(uint16_t) * 4];
if (!IsWindows()) {
return ioctl$sysv(fd, TIOCGWINSZ, &buf) != -1;
} else {

55
libc/calls/kntprioritycombos.c

@ -21,38 +21,37 @@
#include "libc/calls/kntprioritycombos.internal.h"
#include "libc/limits.h"
#include "libc/macros.h"
#include "libc/nexgen32e/ffs.h"
#include "libc/nt/enum/processcreationflags.h"
#include "libc/nt/enum/threadpriority.h"
#define FFS(x) __builtin_ffs(x)
const struct NtPriorityCombo kNtPriorityCombos[] = {
{-20, FFS(kNtHighPriorityClass), kNtThreadPriorityHighest, 15},
{-18, FFS(kNtHighPriorityClass), kNtThreadPriorityTimeCritical, 15},
{-17, FFS(kNtNormalPriorityClass), kNtThreadPriorityTimeCritical, 15},
{-15, FFS(kNtIdlePriorityClass), kNtThreadPriorityTimeCritical, 15},
{-13, FFS(kNtHighPriorityClass), kNtThreadPriorityAboveNormal, 14},
{-11, FFS(kNtHighPriorityClass), kNtThreadPriorityNormal, 13},
{-9, FFS(kNtHighPriorityClass), kNtThreadPriorityBelowNormal, 12},
{-7, FFS(kNtNormalPriorityClass), kNtThreadPriorityHighest, 11},
{-5, FFS(kNtHighPriorityClass), kNtThreadPriorityLowest, 11},
{-3, FFS(kNtNormalPriorityClass), kNtThreadPriorityAboveNormal, 10},
{-1, FFS(kNtNormalPriorityClass), kNtThreadPriorityHighest, 9},
{0, FFS(kNtNormalPriorityClass), kNtThreadPriorityNormal, 9},
{1, FFS(kNtNormalPriorityClass), kNtThreadPriorityAboveNormal, 8},
{2, FFS(kNtNormalPriorityClass), kNtThreadPriorityBelowNormal, 8},
{3, FFS(kNtNormalPriorityClass), kNtThreadPriorityNormal, 7},
{4, FFS(kNtNormalPriorityClass), kNtThreadPriorityLowest, 7},
{5, FFS(kNtIdlePriorityClass), kNtThreadPriorityHighest, 6},
{6, FFS(kNtNormalPriorityClass), kNtThreadPriorityBelowNormal, 6},
{7, FFS(kNtIdlePriorityClass), kNtThreadPriorityAboveNormal, 5},
{9, FFS(kNtNormalPriorityClass), kNtThreadPriorityLowest, 5},
{11, FFS(kNtIdlePriorityClass), kNtThreadPriorityNormal, 4},
{13, FFS(kNtIdlePriorityClass), kNtThreadPriorityBelowNormal, 3},
{15, FFS(kNtIdlePriorityClass), kNtThreadPriorityLowest, 2},
{17, FFS(kNtHighPriorityClass), kNtThreadPriorityIdle, 1},
{18, FFS(kNtNormalPriorityClass), kNtThreadPriorityIdle, 1},
{19, FFS(kNtIdlePriorityClass), kNtThreadPriorityIdle, 1},
{-20, ffs(kNtHighPriorityClass), kNtThreadPriorityHighest, 15},
{-18, ffs(kNtHighPriorityClass), kNtThreadPriorityTimeCritical, 15},
{-17, ffs(kNtNormalPriorityClass), kNtThreadPriorityTimeCritical, 15},
{-15, ffs(kNtIdlePriorityClass), kNtThreadPriorityTimeCritical, 15},
{-13, ffs(kNtHighPriorityClass), kNtThreadPriorityAboveNormal, 14},
{-11, ffs(kNtHighPriorityClass), kNtThreadPriorityNormal, 13},
{-9, ffs(kNtHighPriorityClass), kNtThreadPriorityBelowNormal, 12},
{-7, ffs(kNtNormalPriorityClass), kNtThreadPriorityHighest, 11},
{-5, ffs(kNtHighPriorityClass), kNtThreadPriorityLowest, 11},
{-3, ffs(kNtNormalPriorityClass), kNtThreadPriorityAboveNormal, 10},
{-1, ffs(kNtNormalPriorityClass), kNtThreadPriorityHighest, 9},
{0, ffs(kNtNormalPriorityClass), kNtThreadPriorityNormal, 9},
{1, ffs(kNtNormalPriorityClass), kNtThreadPriorityAboveNormal, 8},
{2, ffs(kNtNormalPriorityClass), kNtThreadPriorityBelowNormal, 8},
{3, ffs(kNtNormalPriorityClass), kNtThreadPriorityNormal, 7},
{4, ffs(kNtNormalPriorityClass), kNtThreadPriorityLowest, 7},
{5, ffs(kNtIdlePriorityClass), kNtThreadPriorityHighest, 6},
{6, ffs(kNtNormalPriorityClass), kNtThreadPriorityBelowNormal, 6},
{7, ffs(kNtIdlePriorityClass), kNtThreadPriorityAboveNormal, 5},
{9, ffs(kNtNormalPriorityClass), kNtThreadPriorityLowest, 5},
{11, ffs(kNtIdlePriorityClass), kNtThreadPriorityNormal, 4},
{13, ffs(kNtIdlePriorityClass), kNtThreadPriorityBelowNormal, 3},
{15, ffs(kNtIdlePriorityClass), kNtThreadPriorityLowest, 2},
{17, ffs(kNtHighPriorityClass), kNtThreadPriorityIdle, 1},
{18, ffs(kNtNormalPriorityClass), kNtThreadPriorityIdle, 1},
{19, ffs(kNtIdlePriorityClass), kNtThreadPriorityIdle, 1},
};
const unsigned kNtPriorityCombosLen = ARRAYLEN(kNtPriorityCombos);

16
libc/calls/readansi.c

@ -29,11 +29,11 @@
* how long each sequence is, so that each read consumes a single thing
* from the underlying file descriptor, e.g.
*
* "a" ALFA
* "\316\261" ALPHA
* "\033[A" CURSOR UP
* "\033[38;5;202m" ORANGERED
* "\eOP" PF1
* "a" ALFA
* "\316\261" ALPHA
* "\033[A" CURSOR UP
* "\033[38;5;202m" ORANGERED
* "\eOP" PF1
*
* This routine generalizes to ascii, utf-8, chorded modifier keys,
* function keys, color codes, c0/c1 control codes, cursor movement,
@ -48,9 +48,9 @@
* tokenized as a single read. Lastly note, this function has limited
* support for UNICODE representations of C0/C1 control codes, e.g.
*
* "\000" NUL
* "\300\200" NUL
* "\302\233A" CURSOR UP
* "\000" NUL
* "\300\200" NUL
* "\302\233A" CURSOR UP
*
* @param buf is guaranteed to receive a NUL terminator if size>0
* @return number of bytes read (helps differentiate "\0" vs. "")

31
libc/calls/setitimer.c

@ -28,27 +28,28 @@
*
* Raise SIGALRM every 1.5s:
*
* CHECK_NE(-1, sigaction(SIGALRM,
* &(struct sigaction){.sa_sigaction = missingno},
* NULL));
* CHECK_NE(-1, setitimer(ITIMER_REAL,
* &(const struct itimerval){{1, 500000}, {1, 500000}},
* NULL));
* CHECK_NE(-1, sigaction(SIGALRM,
* &(struct sigaction){.sa_sigaction = missingno},
* NULL));
* CHECK_NE(-1, setitimer(ITIMER_REAL,
* &(const struct itimerval){{1, 500000},
* {1, 500000}},
* NULL));
*
* Set single-shot 50ms timer callback to interrupt laggy connect():
*
* CHECK_NE(-1, sigaction(SIGALRM,
* &(struct sigaction){.sa_sigaction = missingno,
* .sa_flags = SA_RESETHAND},
* NULL));
* CHECK_NE(-1, setitimer(ITIMER_REAL,
* &(const struct itimerval){{0, 0}, {0, 50000}},
* NULL));
* if (connect(...) == -1 && errno == EINTR) { ... }
* CHECK_NE(-1, sigaction(SIGALRM,
* &(struct sigaction){.sa_sigaction = missingno,
* .sa_flags = SA_RESETHAND},
* NULL));
* CHECK_NE(-1, setitimer(ITIMER_REAL,
* &(const struct itimerval){{0, 0}, {0, 50000}},
* NULL));
* if (connect(...) == -1 && errno == EINTR) { ... }
*
* Disarm timer:
*
* CHECK_NE(-1, setitimer(ITIMER_REAL, &(const struct itimerval){0}, NULL));
* CHECK_NE(-1, setitimer(ITIMER_REAL, &(const struct itimerval){0}, NULL));
*
* Be sure to check for EINTR on your i/o calls, for best low latency.
*

8
libc/calls/sigaction.c

@ -112,10 +112,10 @@ static void sigaction$native2cosmo(union metasigaction *sa) {
/**
* Installs handler for kernel interrupt, e.g.:
*
* void GotCtrlC(int sig, siginfo_t *si, ucontext_t *ctx);
* struct sigaction sa = {.sa_sigaction = GotCtrlC,
* .sa_flags = SA_RESETHAND|SA_RESTART|SA_SIGINFO};
* CHECK_NE(-1, sigaction(SIGINT, &sa, NULL));
* void GotCtrlC(int sig, siginfo_t *si, ucontext_t *ctx);
* struct sigaction sa = {.sa_sigaction = GotCtrlC,
* .sa_flags = SA_RESETHAND|SA_RESTART|SA_SIGINFO};
* CHECK_NE(-1, sigaction(SIGINT, &sa, NULL));
*
* @see xsigaction() for a much better api
* @asyncsignalsafe

6
libc/calls/sigprocmask.c

@ -26,9 +26,9 @@
/**
* Changes program signal blocking state, e.g.:
*
* sigset_t oldmask;
* sigprocmask(SIG_BLOCK, &kSigsetFull, &oldmask);
* sigprocmask(SIG_SETMASK, &oldmask, NULL);
* sigset_t oldmask;
* sigprocmask(SIG_BLOCK, &kSigsetFull, &oldmask);
* sigprocmask(SIG_SETMASK, &oldmask, NULL);
*
* @param how can be SIG_BLOCK (U), SIG_UNBLOCK (/), SIG_SETMASK (=)
* @param set is the new mask content (optional)

2
libc/dce.h

@ -5,6 +5,7 @@
cosmopolitan § autotune » dead code elimination
*/
#ifndef SUPPORT_VECTOR
/**
* Supported Platforms Tuning Knob (Runtime & Compile-Time)
*
@ -17,7 +18,6 @@
* offer marginal improvements in terms of code size and performance, at
* the cost of portability.
*/
#ifndef SUPPORT_VECTOR
#define SUPPORT_VECTOR 0b11111111
#endif
#define LINUX 1

2
libc/elf/struct/rela.h

@ -10,7 +10,7 @@ typedef struct Elf64_Rela {
* ELF64_R_TYPE(r_info) R_X86_64_{64,PC32,GOTPCRELX,...}
* ELF64_R_INFO(sym, type) r_info
*/
/*u64*/ Elf64_Xword r_info; /** @see ELF64_R_{SYM,SIZE,INFO} */
/*u64*/ Elf64_Xword r_info; /* ELF64_R_{SYM,SIZE,INFO} */
/*i64*/ Elf64_Sxword r_addend;
} Elf64_Rela;

34
libc/fmt/bing.c

@ -26,23 +26,23 @@
*
* Cosmopolitan displays RADIX-256 numbers using these digits:
*
* 0123456789abcdef
* 0 
* 1§
* 2 !"#$%&'()*+,-./
* 30123456789:;<=>?
* 4@ABCDEFGHIJKLMNO
* 5PQRSTUVWXYZ[\]^_
* 6`abcdefghijklmno
* 7pqrstuvwxyz{|}~
* 8ÇüéâäàåçêëèïîìÄÅ
* 9ÉæÆôöòûùÿÖÜ¢£¥ƒ
* aáíóúñѪº¿¬½¼¡«»
* b
* c
* d
* eαßΓπΣσμτΦΘΩδφε
* f±÷°·²λ
* 0123456789abcdef
* 0 
* 1§
* 2 !"#$%&'()*+,-./
* 30123456789:;<=>?
* 4@ABCDEFGHIJKLMNO
* 5PQRSTUVWXYZ[\]^_
* 6`abcdefghijklmno
* 7pqrstuvwxyz{|}~
* 8ÇüéâäàåçêëèïîìÄÅ
* 9ÉæÆôöòûùÿÖÜ¢£¥ƒ
* aáíóúñѪº¿¬½¼¡«»
* b
* c
* d
* eαßΓπΣσμτΦΘΩδφε
* f±÷°·²λ
*
* IBM designed these glyphs for the PC to map onto the display bytes at
* (char *)0xb8000. Because IBM authorized buyers of its PCs to inspect

237
libc/integral/c.inc

@ -18,10 +18,6 @@
#define __far
#endif
/**
* Gets type of expression.
* @see autotype() which is a better alternative for certain use cases
*/
#if !defined(__GNUC__) && __cplusplus + 0 >= 201103L
#define typeof(x) decltype(x)
#elif (defined(__STRICT_ANSI__) || !defined(__GNUC__)) && \
@ -143,35 +139,10 @@ typedef __UINT32_TYPE__ uint32_t;
typedef __INT64_TYPE__ int64_t;
typedef __UINT64_TYPE__ uint64_t;
/**
* AX:DX register pair.
*
* Every ABI we support permits functions to return two machine words.
* Normally it's best to define a one-off struct. Sometimes we don't
* want the boilerplate.
*
* @see System V Application Binary Interface NexGen32e Architecture
* Processor Supplement, Version 1.0, December 5th, 2018
* @see agner.org/optimize/calling_conventions.pdf (chapter 6)
* @see LISP primitives CONS[CAR,CDR] w/ IBM 704 naming
* @see int128_t
*/
typedef struct {
intptr_t ax, dx;
} axdx_t;
/*
* GCC, Clang, and System V ABI all incorrectly define intmax_t.
*
* [intmax_t] designates a signed integer type capable of
* representing any value of any signed integer type.
* Quoth ISO/IEC 9899:201x 7.20.1.5
*
* This surprising contradiction is most likely due to Linux distro
* practices of using dynamic shared objects which needs to change.
*
* http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2303.pdf
*/
#ifdef __SIZEOF_INTMAX__
#undef __SIZEOF_INTMAX__
#endif
@ -210,11 +181,6 @@ typedef uint64_t uintmax_t;
#define mallocesque reallocesque returnspointerwithnoaliases
#define interruptfn nocallersavedregisters forcealignargpointer
/**
* Declares combinator, i.e. never reads/writes global memory.
* Thus enabling LICM, CSE, DCE, etc. optimizations.
* @see nosideeffect
*/
#ifndef pureconst
#ifndef __STRICT_ANSI__
#define pureconst __attribute__((__const__))
@ -223,9 +189,6 @@ typedef uint64_t uintmax_t;
#endif
#endif
/**
* Aligns automatic or static variable.
*/
#ifndef forcealign
#ifndef __STRICT_ANSI__
#define forcealign(bytes) __attribute__((__aligned__(bytes)))
@ -234,21 +197,12 @@ typedef uint64_t uintmax_t;
#endif
#endif
/**
* Disables alignment.
* @param opt_bytes defaults to __BIGGEST_ALIGNMENT__
* @see nosideeffect
*/
#ifndef __STRICT_ANSI__
#define thatispacked __attribute__((__packed__))
#else
#define thatispacked
#endif
/**
* Declares prototype as using well-known format string DSL.
* Thereby allowing compiler to identify certain bugs.
*/
#ifndef __STRICT_ANSI__
#define printfesque(n) __attribute__((__format__(__gnu_printf__, n, n + 1)))
#define scanfesque(n) __attribute__((__format__(__gnu_scanf__, n, n + 1)))
@ -297,11 +251,6 @@ typedef uint64_t uintmax_t;
#endif
#endif
/**
* Declares prototype as never mutating global memory.
* Thus enabling CSE, DCE, LICM [clang-only?], etc. optimizations.
* @see pureconst
*/
#ifndef nosideeffect
#if !defined(__STRICT_ANSI__) && \
(__has_attribute(__pure__) || \
@ -332,18 +281,6 @@ typedef uint64_t uintmax_t;
#endif
#endif
/**
* Makes function behave as much like macro as possible, meaning:
*
* 1. always inlined, i.e. even with -fno-inline
* 2. unlinkable, i.e. elf data is not generated
* 3. steppable, i.e. dwarf data is generated
* 4. unprofilable
* 5. unhookable
*
* @note consider static or writing a macro
* @see externinline
*/
#ifndef forceinline
#ifdef __cplusplus
#define forceinline inline
@ -372,19 +309,6 @@ typedef uint64_t uintmax_t;
#endif /* __cplusplus */
#endif /* forceinline */
/**
* Permits untyped or punned memory manipulation w/o asm.
*
* The fundamental problem is that it is not possible to write real
* programs using the X3J11 definition of C. The committee has created
* an unreal language that no one can or will actually use. While the
* problems of `const' may owe to careless drafting of the
* specification, `noalias' is an altogether mistaken notion, and must
* not survive. Dennis Ritchie in 1988-03-20.
*
* @see asm(), memcpy(), memset(), read32be(), etc.
* @see unsigned char
*/
#ifndef mayalias
#if !defined(__STRICT_ANSI__) && \
(__has_attribute(__may_alias__) || \
@ -395,11 +319,6 @@ typedef uint64_t uintmax_t;
#endif
#endif
/**
* Declares prototype as returning freeable resource.
* Compilation will fail if caller ignores return value.
* @see gc(), free(), close(), etc.
*/
#ifndef nodiscard
#if !defined(__STRICT_ANSI__) && \
((__GNUC__ + 0) * 100 + (__GNUC_MINOR__ + 0) >= 304 || \
@ -410,10 +329,6 @@ typedef uint64_t uintmax_t;
#endif
#endif
/**
* Declares variadic function as needing NULL sentinel argument.
* @see execl() for example
*/
#ifndef nullterminated
#if !defined(__STRICT_ANSI__) && \
(__has_attribute(__sentinel__) || __GNUC__ >= 4)
@ -448,10 +363,6 @@ typedef uint64_t uintmax_t;
#endif
#endif
/**
* Relocates function to .text.unlikely section of binary.
* @note can be used to minimize page-faults and improve locality
*/
#ifndef relegated
#if !defined(__STRICT_ANSI__) && \
(__has_attribute(__cold__) || \
@ -470,11 +381,6 @@ typedef uint64_t uintmax_t;
#define warnifused(s)
#endif
/**
* Relocates function to .text.hot section of binary.
* @note can be used to minimize page-faults w/ improved locality
* @note most appropriately automated by profile-guided opts
*/
#ifndef firstclass
#if !defined(__STRICT_ANSI__) && \
(__has_attribute(__hot__) || \
@ -485,12 +391,6 @@ typedef uint64_t uintmax_t;
#endif
#endif
/**
* Declares all or specific parameters as never receiving NULL.
*
* This can be checked at both compile-time (only for constexprs) and
* runtime too (only in MODE=dbg mode) by synthetic Ubsan code.
*/
#ifndef paramsnonnull
#if !defined(__STRICT_ANSI__) && \
(__has_attribute(__nonnull__) || \
@ -501,22 +401,12 @@ typedef uint64_t uintmax_t;
#endif
#endif
/**
* Declares array argument w/ minimum size contract, e.g.
*
* int foo(int bar[hasatleast 2]) { ... }
*/
#if __STDC_VERSION__ + 0 >= 199901L
#define hasatleast static
#else
#define hasatleast
#endif
/**
* Qualifies char pointer so it's treated like every other type.
*
* int foo(int bar[hasatleast 2]) { ... }
*/
#if __STDC_VERSION__ + 0 < 199901L && !defined(restrict)
#if !defined(__STRICT_ANSI__) && !defined(__cplusplus) && \
((__GNUC__ + 0) * 100 + (__GNUC_MINOR__ + 0) >= 301 || defined(_MSC_VER))
@ -527,10 +417,6 @@ typedef uint64_t uintmax_t;
#endif
#endif
/**
* Declares prototype that can't mutate caller's static variables.
* @note consider more .c files or declare in function
*/
#ifndef nocallback
#if !defined(__STRICT_ANSI__) && \
(__has_attribute(__leaf__) || \
@ -554,11 +440,6 @@ typedef uint64_t uintmax_t;
#endif
#endif
/**
* Asks compiler to not optimize function definition.
*
* @todo this is dangerous delete?
*/
#ifndef nooptimize
#ifndef __STRICT_ANSI__
#if (__GNUC__ + 0) * 100 + (__GNUC_MINOR__ + 0) >= 407 || \
@ -572,13 +453,6 @@ typedef uint64_t uintmax_t;
#endif
#endif
/**
* Asks compiler to generate as little code as possible for function.
*
* This does the same thing as relegated, but without relocation.
*
* @todo this is dangerous delete?
*/
#ifndef optimizesize
#ifndef __STRICT_ANSI__
#if (__GNUC__ + 0) * 100 + (__GNUC_MINOR__ + 0) >= 407 || \
@ -592,15 +466,6 @@ typedef uint64_t uintmax_t;
#endif
#endif
/**
* Asks compiler to always heavily optimize function.
*
* This keyword provides an alternative to build flag tuning, in cases
* where the compiler is reluctant to vectorize mathematical code that's
* written in standards-compliant C rather than GCC extensions.
*
* @todo this is dangerous delete?
*/
#ifndef optimizespeed
#if !defined(__STRICT_ANSI__) && \
((__GNUC__ + 0) * 100 + (__GNUC_MINOR__ + 0) >= 407 || \
@ -611,9 +476,6 @@ typedef uint64_t uintmax_t;
#endif
#endif
/**
* Declares prototype that behaves similar to setjmp() or vfork().
*/
#ifndef returnstwice
#if !defined(__STRICT_ANSI__) && \
(__has_attribute(__returns_twice__) || \
@ -624,10 +486,6 @@ typedef uint64_t uintmax_t;
#endif
#endif
/**
* Asks compiler to not emit DWARF assembly for function.
* @see artificial
*/
#ifndef nodebuginfo
#if !defined(__STRICT_ANSI__) && \
(__has_attribute(__nodebug__) || defined(__llvm__))
@ -637,10 +495,6 @@ typedef uint64_t uintmax_t;
#endif
#endif
/**
* Associates debug information with call site.
* @see nodebuginfo
*/
#ifndef artificial
#if !defined(__STRICT_ANSI__) && \
(__has_attribute(__artificial__) || \
@ -651,11 +505,6 @@ typedef uint64_t uintmax_t;
#endif
#endif
/**
* Defines function as specially compiled for newer cpu model.
* @see -ftree-vectorize and consider assembly
* @see libc/dce.h
*/
#ifndef microarchitecture
#if !defined(__STRICT_ANSI__) && \
(__has_attribute(__target__) || \
@ -666,10 +515,6 @@ typedef uint64_t uintmax_t;
#endif
#endif
/**
* Compiles function multiple times for different cpu models.
* @see libc/dce.h
*/
#ifndef targetclones
#if !defined(__STRICT_ANSI__) && \
(__has_attribute(__target_clones__) || __GNUC__ >= 6)
@ -679,10 +524,6 @@ typedef uint64_t uintmax_t;
#endif
#endif
/**
* Defines function with prologue that fixes misaligned stack.
* @see nocallersavedregisters and consider assembly
*/
#if (__GNUC__ + 0) * 100 + (__GNUC_MINOR__ + 0) >= 408 || \
__has_attribute(__force_align_arg_pointer__)
#define forcealignargpointer __attribute__((__force_align_arg_pointer__))
@ -690,12 +531,6 @@ typedef uint64_t uintmax_t;
#define forcealignargpointer "need modern compiler"
#endif
/**
* Declares prototype as never returning NULL.
*
* This is checked at compile-time for constexprs. It'll be checked at
* runtime too by synthetic code, only in MODE=dbg mode.
*/
#ifndef returnsnonnull
#if !defined(__STRICT_ANSI__) && \
(__has_attribute(__returns_nonnull__) || \
@ -706,13 +541,6 @@ typedef uint64_t uintmax_t;
#endif
#endif
/**
* Attests return value is aligned.
*
* @param (alignment)
* @param (alignment, misalignment)
* @see attributeallocalign(), returnspointerwithnoaliases, mallocesque
*/
#if !defined(__STRICT_ANSI__) && \
(__has_attribute(__assume_aligned__) || \
(__GNUC__ + 0) * 100 + (__GNUC_MINOR__ + 0) >= 409)
@ -721,10 +549,6 @@ typedef uint64_t uintmax_t;
#define returnsaligned(x)
#endif
/**
* Declares prototype as behaving similar to malloc().
* @see attributeallocsize(), attributeallocalign()
*/
#ifndef returnspointerwithnoaliases
#if !defined(__STRICT_ANSI__) && \
(__has_attribute(__malloc__) || \
@ -757,20 +581,6 @@ typedef uint64_t uintmax_t;
#endif
#endif
/**
* Defines variable as having same type as right-hand side.
*
* This enables safe, backwards-compatible, non-explosive macros, e.g.:
*
* #define bar(FOO) \
* ({ \
* autotype(FOO) Foo = (FOO); \
* Foo + Foo * 2; \
* })
*
* @param x must be identical to rhs
* @note typeof goes back to gcc 2.x
*/
#if __cplusplus + 0 >= 201103L
#define autotype(x) auto
#elif ((__has_builtin(auto_type) || defined(__llvm__) || \
@ -781,27 +591,16 @@ typedef uint64_t uintmax_t;
#define autotype(x) typeof(x)
#endif
/**
* Defines interrupt handler that can call non-interrupt functions.
* @see forcealignargpointer, -mgeneral-regs-only and consider assembly
*/
#if __GNUC__ >= 7 || __has_attribute(__no_caller_saved_registers__)
#define nocallersavedregisters __attribute__((__no_caller_saved_registers__))
#else
#define nocallersavedregisters "need modern compiler"
#endif
/**
* Attests that execution of statement is impossible.
*/
#ifndef unreachable
#define unreachable __builtin_unreachable()
#endif
/**
* Statement that does nothing.
* @note can help avoid drama w/ linters, warnings, formatters, etc.
*/
#define donothing \
do { \
} while (0)
@ -857,9 +656,6 @@ typedef uint64_t uintmax_t;
#define initarray