Browse Source

Get Cosmopolitan into releasable state

A new rollup tool now exists for flattening out the headers in a way
that works better for our purposes than cpp. A lot of the API clutter
has been removed. APIs that aren't a sure thing in terms of general
recommendation are now marked internal.

There's now a smoke test for the amalgamation archive and gigantic
header file. So we can now guarantee you can use this project on the
easiest difficulty setting without the gigantic repository.

A website is being created, which is currently a work in progress:
https://justine.storage.googleapis.com/cosmopolitan/index.html
main
Justine Tunney 1 year ago
parent
commit
ea0b5d9d1c
  1. 29
      Makefile
  2. 67
      ape/ape.S
  3. 2
      ape/ape.lds
  4. 2
      ape/ape.mk
  5. 2
      ape/config.h
  6. 19
      ape/idata.internal.h
  7. 2
      ape/lib/flattenhighmemory.c
  8. 35
      ape/macros.h
  9. 41
      ape/mtime.h
  10. 2
      build/definitions.mk
  11. 18
      build/htags
  12. 2
      dsp/core/gamma.c
  13. 4
      dsp/core/scalevolume.c
  14. 2
      dsp/mpeg/mpeg1.c
  15. 4
      dsp/scale/magikarp.c
  16. 2
      dsp/tty/ident.c
  17. 2
      dsp/tty/internal.h
  18. 2
      dsp/tty/itoa8.c
  19. 2
      dsp/tty/quant.h
  20. 2
      dsp/tty/rgb2ansi.c
  21. 2
      dsp/tty/rgb2ttyf2i.c
  22. 2
      dsp/tty/sendtitle.c
  23. 2
      dsp/tty/ttymove.c
  24. 2
      dsp/tty/ttyquant.c
  25. 2
      dsp/tty/ttyraster.c
  26. 6
      examples/auto-launch-gdb-on-crash.c
  27. 63
      examples/backtrace.c
  28. 14
      examples/cplusplus.cc
  29. 2
      examples/ctrlc.c
  30. 2
      examples/dinlerp.c
  31. 2
      examples/findprime.c
  32. 2
      examples/gui.c
  33. 5
      examples/ispell.c
  34. 2
      examples/kilo.c
  35. 4
      examples/lstime.c
  36. 4
      examples/nesemu1.cc
  37. 2
      examples/now.c
  38. 2
      examples/printargs.c
  39. 36
      examples/printmysymbols.c
  40. 76
      libc/alg/alg.h
  41. 28
      libc/alg/arraylist.internal.h
  42. 40
      libc/alg/arraylist2.internal.h
  43. 0
      libc/alg/bisect.internal.h
  44. 9
      libc/alg/bisectcarleft.internal.h
  45. 27
      libc/alg/bisectleft.h
  46. 26
      libc/alg/bisectright.h
  47. 2
      libc/alg/bsearch.c
  48. 2
      libc/alg/bsearch_r.c
  49. 27
      libc/alg/critbit0.h
  50. 2
      libc/alg/critbit0_allprefixed.c
  51. 2
      libc/alg/critbit0_clear.c
  52. 2
      libc/alg/critbit0_contains.c
  53. 2
      libc/alg/critbit0_delete.c
  54. 2
      libc/alg/critbit0_emplace.c
  55. 2
      libc/alg/critbit0_get.c
  56. 2
      libc/alg/critbit0_insert.c
  57. 34
      libc/alg/insertionsort.h
  58. 4
      libc/alg/replacestr.c
  59. 5
      libc/alg/replacestr16.c
  60. 4
      libc/alg/reverse.h
  61. 2
      libc/alg/shuffle.h
  62. 2
      libc/bits/avx2intrin.internal.h
  63. 0
      libc/bits/avxintrin.internal.h
  64. 28
      libc/bits/bigword.h
  65. 16
      libc/bits/bigword.internal.h
  66. 19
      libc/bits/bitreverse16.c
  67. 19
      libc/bits/bitreverse8.c
  68. 65
      libc/bits/bits.h
  69. 4
      libc/bits/emmintrin.internal.h
  70. 12
      libc/bits/gray.c
  71. 48
      libc/bits/i2bcd.S
  72. 2
      libc/bits/initializer.internal.h
  73. 0
      libc/bits/mmintrin.internal.h
  74. 0
      libc/bits/pmmintrin.internal.h
  75. 0
      libc/bits/progn.internal.h
  76. 0
      libc/bits/safemacros.internal.h
  77. 4
      libc/bits/shaintrin.internal.h
  78. 0
      libc/bits/smmintrin.internal.h
  79. 2
      libc/bits/tmmintrin.internal.h
  80. 14
      libc/bits/typecheck.h
  81. 19
      libc/bits/ungray.c
  82. 2
      libc/bits/unsignedsubtract.c
  83. 2
      libc/bits/wmmintrin.internal.h
  84. 6
      libc/bits/xmmintrin.internal.h
  85. 1
      libc/calls/calls.h
  86. 2
      libc/calls/clock_gettime.c
  87. 5
      libc/calls/commandv.c
  88. 2
      libc/calls/copyfd.c
  89. 1
      libc/calls/fprot2nt.c
  90. 4
      libc/calls/fstat-nt.c
  91. 2
      libc/calls/g_fds.c
  92. 2
      libc/calls/getenv.c
  93. 19
      libc/calls/getpid.c
  94. 3
      libc/calls/getpriority-nt.c
  95. 4
      libc/calls/getprocaddressmodule.c
  96. 2
      libc/calls/hefty/copyfile.c
  97. 2
      libc/calls/hefty/dirstream.c
  98. 2
      libc/calls/hefty/filecmp.c
  99. 1
      libc/calls/hefty/fork-nt.c
  100. 4
      libc/calls/hefty/get_current_dir_name.c

29
Makefile

@ -187,6 +187,7 @@ include test/libc/rand/test.mk
include test/libc/time/test.mk
include test/libc/stdio/test.mk
include test/libc/conv/test.mk
include test/libc/release/test.mk
include test/libc/test.mk
include test/ape/lib/test.mk
include test/ape/test.mk
@ -253,8 +254,9 @@ COSMOPOLITAN_OBJECTS = \
LIBC_CONV \
LIBC_CRYPTO \
LIBC_DNS \
LIBC_FMT \
LIBC_ELF \
LIBC_FMT \
LIBC_INTRIN \
LIBC_LOG \
LIBC_MEM \
LIBC_NEXGEN32E \
@ -270,7 +272,9 @@ COSMOPOLITAN_OBJECTS = \
LIBC_TIME \
LIBC_TINYMATH \
LIBC_UNICODE \
LIBC_X \
LIBC_ZIPOS \
THIRD_PARTY_COMPILER_RT \
THIRD_PARTY_DLMALLOC \
THIRD_PARTY_DTOA \
THIRD_PARTY_GETOPT \
@ -279,22 +283,31 @@ COSMOPOLITAN_OBJECTS = \
COSMOPOLITAN_HEADERS = \
LIBC \
LIBC_ALG \
LIBC_BITS \
LIBC_CALLS \
LIBC_CONV \
LIBC_CRYPTO \
LIBC_DNS \
LIBC_ELF \
LIBC_FMT \
LIBC_INTRIN \
LIBC_LOG \
LIBC_MEM \
LIBC_NEXGEN32E \
LIBC_NT \
LIBC_OHMYPLUS \
LIBC_RAND \
LIBC_RUNTIME \
LIBC_SOCK \
LIBC_STDIO \
LIBC_STR \
LIBC_SYSV \
LIBC_TIME \
LIBC_TINYMATH \
LIBC_UNICODE \
LIBC_X \
LIBC_ZIPOS \
LIBC_SYSV \
LIBC_NT \
THIRD_PARTY_DLMALLOC \
THIRD_PARTY_DTOA \
THIRD_PARTY_GETOPT \
@ -302,11 +315,11 @@ COSMOPOLITAN_HEADERS = \
THIRD_PARTY_REGEX
o/$(MODE)/cosmopolitan.a: $(filter-out o/libc/stubs/exit11.o,$(foreach x,$(COSMOPOLITAN_OBJECTS),$($(x)_OBJS)))
o/$(MODE)/.cosmopolitan.h: $(foreach x,$(COSMOPOLITAN_HEADERS),$($(x)_HDRS))
build/rollup $^ >$@
o/$(MODE)/cosmopolitan.h: o/$(MODE)/.cosmopolitan.h
build/compile $(PREPROCESS) -P $(OUTPUT_OPTION) $<
clang-format-10 -i $@
o/cosmopolitan.h: \
o/$(MODE)/tool/build/rollup.com.dbg \
libc/integral/normalize.inc \
$(foreach x,$(COSMOPOLITAN_HEADERS),$($(x)_HDRS))
@ACTION=ROLLUP TARGET=$@ build/do $^ >$@
# UNSPECIFIED PREREQUISITES TUTORIAL
#

67
ape/ape.S

@ -40,11 +40,10 @@
#include "ape/notice.inc"
#include "ape/relocations.h"
#include "libc/elf/def.h"
#include "libc/macho.h"
#include "libc/nexgen32e/uart.h"
#include "libc/nexgen32e/vidya.h"
#include "libc/nt/pedef.h"
#include "libc/nexgen32e/vidya.h"
#include "libc/macho.internal.h"
#include "libc/nexgen32e/uart.internal.h"
#include "libc/nexgen32e/vidya.internal.h"
#include "libc/nt/pedef.internal.h"
#include "libc/dce.h"
#include "libc/sysv/consts/prot.h"
@ -440,33 +439,6 @@ apmoff: mov $0x5300,%ax # apm installation check
1: call panic
.endfn apmoff,globl
/ Video put char.
/
/ @param al is the char
/ @mode real
rvputc: push %bx # don't clobber bp,bx,di,si,cx
push %bp # original ibm pc scroll up bug
mov $7,%bx # normal mda/cga style page zero
mov $0x0e,%ah # teletype output al cp437
int $0x10 # vidya service
pop %bp # preserves al
pop %bx
ret
.endfn rvputc
/ Video put string.
/
/ @param di is the string
/ @mode real
rvputs: mov %di,%si
0: lodsb
test %al,%al
je 1f
call rvputc
jmp 0b
1: ret
.endfn rvputs,globl,hidden
/*
αcτµαlly pδrταblε εxεcµταblε § partition table
*/
@ -1267,6 +1239,33 @@ sputs: push %bx
ret
.endfn sputs,globl
/ Video put string.
/
/ @param di is the string
/ @mode real
rvputs: mov %di,%si
0: lodsb
test %al,%al
je 1f
rlcall rvputc
jmp 0b
1: ret
.endfn rvputs,globl,hidden
/ Video put char.
/
/ @param al is the char
/ @mode real
rvputc: push %bx # don't clobber bp,bx,di,si,cx
push %bp # original ibm pc scroll up bug
mov $7,%bx # normal mda/cga style page zero
mov $0x0e,%ah # teletype output al cp437
int $0x10 # vidya service
pop %bp # preserves al
pop %bx
ret
.endfn rvputc
/*
@ -1613,8 +1612,8 @@ metal:
mov $.Lape.bss.vaddr,%edi
mov $.Lape.bss.memsz,%ecx
rep stosb
.weak hostos
ezlea hostos,ax
.weak __hostos
ezlea __hostos,ax
test %rax,%rax
jz 1f
movb $METAL,(%rax)

2
ape/ape.lds

@ -177,7 +177,7 @@
#ifdef __LINKER__
#include "ape/macros.h"
#include "ape/config.h"
#include "libc/nt/pedef.h"
#include "libc/nt/pedef.internal.h"
#include "libc/zip.h"
ENTRY(_start)

2
ape/ape.mk

@ -39,7 +39,7 @@ APE_DEPS = $(APE_LIB)
APE_CHECKS = $(APE_HDRS:%=o/%.ok)
o/ape/idata.inc: \
ape/idata.h \
ape/idata.internal.h \
ape/relocations.h
$(APE_OBJS): $(BUILD_FILES) \

2
ape/config.h

@ -38,7 +38,7 @@
/**
* PC Display Configuration (MDA/CGA)
* @see www.lammertbies.nl/comm/info/serial-uart.html
* @see ape/lib/vidya.h
* @see ape/lib/vidya.internal.h
*/
#ifndef VIDYA_MODE
#define VIDYA_MODE VIDYA_MODE_CGA

19
ape/idata.h → ape/idata.internal.h

@ -1,22 +1,3 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#ifndef APE_IDATA_H_
#define APE_IDATA_H_
#ifdef __ASSEMBLER__

2
ape/lib/flattenhighmemory.c

@ -20,7 +20,7 @@
#include "ape/config.h"
#include "ape/lib/pc.h"
#include "libc/bits/bits.h"
#include "libc/bits/safemacros.h"
#include "libc/bits/safemacros.internal.h"
/**
* Virtualizes physical memory.

35
ape/macros.h

@ -1,38 +1,3 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
αcτµαlly pδrταblε εxεcµταblε § macros
*/
#ifndef APE_MACROS_H_
#define APE_MACROS_H_
#include "libc/macros.h"

41
ape/mtime.h

@ -1,41 +0,0 @@
#ifndef COSMOPOLITAN_APE_MTIME_H_
#define COSMOPOLITAN_APE_MTIME_H_
#include "libc/dos.h"
/**
* @fileoverview Deterministic last modified timestamp embedding.
*/
#ifndef MTIME_YEAR
#define MTIME_YEAR 2019
#endif
#ifndef MTIME_MONTH
#define MTIME_MONTH 1
#endif
#ifndef MTIME_DAY
#define MTIME_DAY 1
#endif
#ifndef MTIME_HOUR
#define MTIME_HOUR 0
#endif
#ifndef MTIME_MINUTES
#define MTIME_MINUTES 0
#endif
#ifndef MTIME_SECONDS
#define MTIME_SECONDS 0
#endif
#ifndef ZIP_MTIME_DATE
#define ZIP_MTIME_DATE DOS_DATE(MTIME_YEAR, MTIME_MONTH, MTIME_DAY)
#endif
#ifndef ZIP_MTIME_TIME
#define ZIP_MTIME_TIME DOS_TIME(MTIME_HOUR, MTIME_MINUTES, MTIME_SECONDS)
#endif
#endif /* COSMOPOLITAN_APE_MTIME_H_ */

2
build/definitions.mk

@ -383,5 +383,5 @@ TAGSFLAGS = \
--if0=no \
--langmap=c:.c.h.i \
--line-directives=yes \
--exclude=libc/nt/struct/imagefileheader.h \
--exclude=libc/nt/struct/imagefileheader.internal.h \
--exclude=libc/nt/struct/filesegmentelement.h

18
build/htags

@ -59,13 +59,13 @@ set -- --regex-c='/^extern [^(]*(\*const \([^)]*\))(/\1/b' "$@"
# struct WorstSoftwareEver;
set -- --regex-c='/^struct.*;$/uehocruehcroue/b' "$@"
exec ${TAGS:-ctags} \
-e \
--langmap=c:.c.h \
--exclude=libc/nt/struct/imagefileheader.h \
--exclude=libc/nt/struct/imageseparatedebugheader.h \
--exclude=libc/nt/struct/importobjectheader.h \
--exclude=libc/nt/struct/nonpageddebuginfo.h \
--exclude=libc/nt/struct/ansistring.h \
--exclude=libc/nt/struct/filesegmentelement.h \
exec ${TAGS:-ctags} \
-e \
--langmap=c:.c.h \
--exclude=libc/nt/struct/imagefileheader.internal.h \
--exclude=libc/nt/struct/imageseparatedebugheader.internal.h \
--exclude=libc/nt/struct/importobjectheader.h \
--exclude=libc/nt/struct/nonpageddebuginfo.h \
--exclude=libc/nt/struct/ansistring.h \
--exclude=libc/nt/struct/filesegmentelement.h \
"$@"

2
dsp/core/gamma.c

@ -24,6 +24,7 @@
double rgb2stdpc(double x, double g) {
return COMPANDLUMA_SRGB(x, g);
}
double rgb2linpc(double x, double g) {
return UNCOMPANDLUMA_SRGB(x, g);
}
@ -31,6 +32,7 @@ double rgb2linpc(double x, double g) {
double rgb2stdtv(double x) {
return COMPANDLUMA_BT1886(x);
}
double rgb2lintv(double x) {
return UNCOMPANDLUMA_BT1886(x);
}

4
dsp/core/scalevolume.c

@ -19,7 +19,7 @@
*/
#include "dsp/core/core.h"
#include "libc/bits/bits.h"
#include "libc/bits/safemacros.h"
#include "libc/bits/safemacros.internal.h"
#include "libc/limits.h"
/**
@ -43,7 +43,7 @@ void scalevolume(size_t n, int16_t pcm[n][8], int p) {
if (p > 15) p = 15;
for (i = 0; i < n; ++i) {
for (j = 0; j < 8; ++j) {
pcm[i][j] = SAR(pcm[i][j], p);
pcm[i][j] = pcm[i][j] >> p;
}
}
}

2
dsp/mpeg/mpeg1.c

@ -32,7 +32,7 @@
#include "dsp/mpeg/idct.h"
#include "dsp/mpeg/mpeg.h"
#include "dsp/mpeg/video.h"
#include "libc/bits/initializer.h"
#include "libc/bits/initializer.internal.h"
#include "libc/conv/conv.h"
#include "libc/log/log.h"
#include "libc/macros.h"

4
dsp/scale/magikarp.c

@ -40,8 +40,8 @@
signed char g_magikarp[8];
const signed char kMagikarp[8][8] = {
{-1, -3, 3, 17, 17, 3, -3, -1}, /* 1331+161 derived w/ one off cas */
{-1, -3, 6, 28, 6, -3, -1, 0}, /* due to the convolution theorem? */
{0, 0, -11, 53, -11, 0, 0, 0}, /* plus, some random experimenting */
{-1, -3, 6, 28, 6, -3, -1, 0}, /* no due to the convolution theorem? */
{0, 0, -11, 53, -11, 0, 0, 0}, /* no plus, some random experimenting */
{-2, -6, 2, 22, 22, 2, -6, -2}, /* one a line please clang-format? */
{-3, -9, 1, 27, 27, 1, -9, -3},
};

2
dsp/tty/ident.c

@ -18,7 +18,7 @@
02110-1301 USA
*/
#include "dsp/tty/tty.h"
#include "libc/bits/safemacros.h"
#include "libc/bits/safemacros.internal.h"
#include "libc/bits/weaken.h"
#include "libc/calls/calls.h"
#include "libc/calls/termios.h"

2
dsp/tty/internal.h

@ -1,7 +1,7 @@
#ifndef COSMOPOLITAN_DSP_TTY_INTERNAL_H_
#define COSMOPOLITAN_DSP_TTY_INTERNAL_H_
#include "dsp/tty/ttyrgb.h"
#include "libc/bits/xmmintrin.h"
#include "libc/bits/xmmintrin.internal.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_

2
dsp/tty/itoa8.c

@ -19,7 +19,7 @@
*/
#include "dsp/tty/itoa8.h"
#include "libc/bits/bits.h"
#include "libc/bits/initializer.h"
#include "libc/bits/initializer.internal.h"
#include "libc/str/str.h"
struct Itoa8 kItoa8;

2
dsp/tty/quant.h

@ -3,7 +3,7 @@
#include "dsp/tty/ttyrgb.h"
#include "libc/assert.h"
#include "libc/bits/bits.h"
#include "libc/bits/xmmintrin.h"
#include "libc/bits/xmmintrin.internal.h"
#include "libc/limits.h"
#include "libc/str/str.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)

2
dsp/tty/rgb2ansi.c

@ -20,7 +20,7 @@
#include "dsp/core/core.h"
#include "dsp/tty/quant.h"
#include "libc/assert.h"
#include "libc/bits/initializer.h"
#include "libc/bits/initializer.internal.h"
#include "libc/limits.h"
#include "libc/log/log.h"
#include "libc/macros.h"

2
dsp/tty/rgb2ttyf2i.c

@ -18,7 +18,7 @@
02110-1301 USA
*/
#include "dsp/tty/quant.h"
#include "libc/bits/xmmintrin.h"
#include "libc/bits/xmmintrin.internal.h"
struct TtyRgb rgb2ttyf2i_(__m128 rgb) {
__v4si i4;

2
dsp/tty/sendtitle.c

@ -18,7 +18,7 @@
02110-1301 USA
*/
#include "dsp/tty/tty.h"
#include "libc/alg/arraylist2.h"
#include "libc/alg/arraylist2.internal.h"
#include "libc/runtime/gc.h"
#include "libc/x/x.h"

2
dsp/tty/ttymove.c

@ -19,7 +19,7 @@
*/
#include "dsp/tty/itoa8.h"
#include "dsp/tty/tty.h"
#include "libc/bits/safemacros.h"
#include "libc/bits/safemacros.internal.h"
#include "libc/limits.h"
#include "libc/log/check.h"

2
dsp/tty/ttyquant.c

@ -19,7 +19,7 @@
*/
#include "dsp/tty/internal.h"
#include "dsp/tty/quant.h"
#include "libc/bits/initializer.h"
#include "libc/bits/initializer.internal.h"
#include "libc/dce.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"

2
dsp/tty/ttyraster.c

@ -24,7 +24,7 @@
#include "dsp/tty/windex.h"
#include "libc/assert.h"
#include "libc/bits/bits.h"
#include "libc/bits/safemacros.h"
#include "libc/bits/safemacros.internal.h"
#include "libc/limits.h"
#include "libc/log/check.h"
#include "libc/log/log.h"

6
examples/auto-launch-gdb-on-crash.c

@ -32,13 +32,13 @@
* @see examples and reference material on using the asm() keyword
* - libc/nexgen32e/bsf.h
* - libc/nexgen32e/tzcnt.h
* - libc/nexgen32e/cpuid4.h
* - libc/nexgen32e/tinystrcmp.h
* - libc/nexgen32e/cpuid4.internal.h
* - libc/nexgen32e/tinystrcmp.internal.h
* - https://gist.github.com/jart/fe8d104ef93149b5ba9b72912820282c
*/
int main(int argc, char *argv[]) {
showcrashreports();
asm("int3"); /* cf. die(), perror("msg"), abort(), exit(1), _Exit(1) */
asm("int3"); /* cf. __die(), perror("msg"), abort(), exit(1), _Exit(1) */
return 0;
}

63
examples/backtrace.c

@ -1,63 +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/calls/calls.h"
#include "libc/log/backtrace.h"
#include "libc/log/log.h"
#include "libc/mem/mem.h"
#include "libc/runtime/gc.h"
#include "libc/runtime/runtime.h"
#include "libc/runtime/symbols.h"
#include "libc/stdio/stdio.h"
#include "libc/sysv/consts/fileno.h"
/*
Your executables will try to find the addr2line tool, which is needed
to decrypt gcc debug information, enabling backtraces like this:
0x0000000000403a7a: Hello at examples/backtrace.c:31
0x0000000000403ac3: World at examples/backtrace.c:38
0x0000000000401608: main at examples/backtrace.c:42
0x0000000000401379: _start at libc/crt/crt.S:61
If that doesn't work, then your αcτµαlly pδrταblε εxεcµταblε will use
its own builtin fallback solution:
0000ac2fa7e0 000000403a9b Hello+0x49
0000ac2fa7f0 000000403ac4 World+0x22
0000ac2fa800 000000401609 main+0x7
0000ac2fa810 00000040137a _start+0x63
This is guaranteed to work if the .com.dbg file can be found.
Otherwise it'll print numbers:
0000268a8390 000000403a90 (null)+0x403a8f
0000268a83a0 000000403aef (null)+0x403aee
0000268a83b0 0000004015fe (null)+0x4015fd
0000268a83c0 00000040137a (null)+0x401379
*/
void hello(void) {
gc(malloc(1));
ShowBacktrace(STDOUT_FILENO, NULL);
setenv("ADDR2LINE", "", true);
ShowBacktrace(STDOUT_FILENO, NULL);
}
void world(void) {
gc(malloc(1));
hello();
}
int main(int argc, char *argv[]) {
world();
return 0;
}

14
examples/cplusplus.cc

@ -17,7 +17,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/bits/safemacros.h"
#include "libc/bits/safemacros.internal.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
@ -31,9 +31,15 @@ class Log {
int *x_;
};
Log::Log() { x_ = new int[64]; }
Log::~Log() { delete x_; }
int *Log::x() { return x_; }
Log::Log() {
x_ = new int[64];
}
Log::~Log() {
delete x_;
}
int *Log::x() {
return x_;
}
class Log g_log;

2
examples/ctrlc.c

@ -9,7 +9,7 @@
#endif
#include "libc/calls/calls.h"
#include "libc/log/check.h"
#include "libc/log/color.h"
#include "libc/log/color.internal.h"
#include "libc/log/log.h"
#include "libc/mem/mem.h"
#include "libc/runtime/gc.h"

2
examples/dinlerp.c

@ -12,7 +12,7 @@
#include "dsp/scale/scale.h"
#include "dsp/tty/itoa8.h"
#include "dsp/tty/quant.h"
#include "libc/alg/arraylist2.h"
#include "libc/alg/arraylist2.internal.h"
#include "libc/calls/calls.h"
#include "libc/calls/ioctl.h"
#include "libc/calls/struct/stat.h"

2
examples/findprime.c

@ -13,7 +13,7 @@
#include "libc/conv/conv.h"
#include "libc/limits.h"
#include "libc/log/check.h"
#include "libc/log/color.h"
#include "libc/log/color.internal.h"
#include "libc/log/log.h"
#include "libc/math.h"
#include "libc/runtime/runtime.h"

2
examples/gui.c

@ -7,7 +7,7 @@
http://creativecommons.org/publicdomain/zero/1.0/
*/
#endif
#include "libc/nt/enum/messageboxtype.h"
#include "libc/nt/enum/mb.h"
#include "libc/nt/messagebox.h"
int main(int argc, char *argv[]) {

5
examples/ispell.c

@ -8,7 +8,8 @@
*/
#endif
#include "libc/alg/alg.h"
#include "libc/alg/arraylist.h"
#include "libc/alg/arraylist.internal.h"
#include "libc/alg/critbit0.h"
#include "libc/bits/bits.h"
#include "libc/calls/calls.h"
#include "libc/conv/conv.h"
@ -18,7 +19,7 @@
#include "libc/mem/mem.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/str/tpdecode.h"
#include "libc/str/tpdecode.internal.h"
#include "libc/sysv/consts/fileno.h"
#include "libc/x/x.h"

2
examples/kilo.c

@ -59,7 +59,7 @@ Contact: antirez@gmail.com\"\n\
#define _GNU_SOURCE
#include "libc/alg/alg.h"
#include "libc/alg/arraylist2.h"
#include "libc/alg/arraylist2.internal.h"
#include "libc/calls/calls.h"
#include "libc/calls/termios.h"
#include "libc/calls/weirdtypes.h"

4
examples/lstime.c

@ -8,8 +8,8 @@
*/
#endif
#include "libc/alg/alg.h"
#include "libc/alg/arraylist2.h"
#include "libc/bits/safemacros.h"
#include "libc/alg/arraylist2.internal.h"
#include "libc/bits/safemacros.internal.h"
#include "libc/calls/calls.h"
#include "libc/calls/struct/dirent.h"
#include "libc/calls/struct/stat.h"

4
examples/nesemu1.cc

@ -10,10 +10,10 @@
#include "dsp/tty/itoa8.h"
#include "dsp/tty/quant.h"
#include "dsp/tty/tty.h"
#include "libc/alg/arraylist2.h"
#include "libc/alg/arraylist2.internal.h"
#include "libc/assert.h"
#include "libc/bits/bits.h"
#include "libc/bits/safemacros.h"
#include "libc/bits/safemacros.internal.h"
#include "libc/calls/calls.h"
#include "libc/calls/hefty/spawn.h"
#include "libc/calls/struct/itimerval.h"

2
examples/now.c

@ -8,7 +8,7 @@
*/
#endif
#include "libc/bits/bits.h"
#include "libc/bits/safemacros.h"
#include "libc/bits/safemacros.internal.h"
#include "libc/calls/calls.h"
#include "libc/dce.h"
#include "libc/stdio/stdio.h"

2
examples/printargs.c

@ -7,7 +7,7 @@
http://creativecommons.org/publicdomain/zero/1.0/
*/
#endif
#include "libc/bits/progn.h"
#include "libc/bits/progn.internal.h"
#include "libc/log/log.h"
#include "libc/macros.h"
#include "libc/runtime/runtime.h"

36
examples/printmysymbols.c

@ -1,36 +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/calls/calls.h"
#include "libc/elf/elf.h"
#include "libc/log/backtrace.h"
#include "libc/log/log.h"
#include "libc/runtime/symbols.h"
#include "libc/stdio/stdio.h"
#include "libc/sysv/consts/fileno.h"
int main(int argc, char *argv[]) {
int rc = 0;
char *filename;
struct SymbolTable *tab = NULL;
if ((filename = FindDebugBinary()) != NULL &&
(tab = OpenSymbolTable(filename))) {
for (unsigned i = 0; i < tab->count; ++i) {
printf("%p %s\n", tab->addr_base + tab->symbols[i].addr_rva,
GetElfString(tab->elf, tab->elfsize, tab->name_base,
tab->symbols[i].name_rva));
}
} else {
perror("printmysymbols");
ShowBacktrace(STDERR_FILENO, NULL);
rc = 1;
}
CloseSymbolTable(&tab);
return rc;
}

76
libc/alg/alg.h

@ -12,7 +12,7 @@ void *bsearch(const void *, const void *, size_t, size_t,
void *bsearch_r(const void *, const void *, size_t, size_t,
int cmp(const void *, const void *, void *), void *)
paramsnonnull((1, 2, 5)) nothrow nosideeffect;
void djbsort(size_t n, int32_t[n]);
void djbsort(int32_t *, size_t);
void qsort(void *, size_t, size_t, int (*)(const void *, const void *))
paramsnonnull();
void qsort_r(void *, size_t, size_t,
@ -43,72 +43,14 @@ char16_t *concatstr16(const char16_t *, ...) nullterminated()
wchar_t *concatwcs(const wchar_t *, ...) nullterminated()
paramsnonnull() __algalloc;
/*───────────────────────────────────────────────────────────────────────────│─╗
cosmopolitan § algorithms » containers
*/
struct critbit0 {
void *root;
size_t count;
};
bool critbit0_contains(struct critbit0 *, const char *) nothrow nosideeffect
paramsnonnull();
bool critbit0_insert(struct critbit0 *, const char *) paramsnonnull();
bool critbit0_delete(struct critbit0 *, const char *) nothrow paramsnonnull();
void critbit0_clear(struct critbit0 *) nothrow paramsnonnull();
char *critbit0_get(struct critbit0 *, const char *);
intptr_t critbit0_allprefixed(struct critbit0 *, const char *,
intptr_t (*)(const char *, void *), void *)
paramsnonnull((1, 2, 3)) nothrow;
bool critbit0_emplace(struct critbit0 *, char *, size_t) paramsnonnull();
/*───────────────────────────────────────────────────────────────────────────│─╗
cosmopolitan § algorithms » comparators
*/
int cmpsb(/*const signed char[1]*/ const void *, const void *)
paramsnonnull() nothrow nocallback nosideeffect;
int cmpub(/*const unsigned char[1]*/ const void *, const void *)
paramsnonnull() nothrow nocallback nosideeffect;
int cmpsw(/*const signed short[1]*/ const void *, const void *)
paramsnonnull() nothrow nocallback nosideeffect;
int cmpuw(/*const unsigned short[1]*/ const void *, const void *)
paramsnonnull() nothrow nocallback nosideeffect;
int cmpsl(/*const signed int[1]*/ const void *, const void *)
paramsnonnull() nothrow nocallback nosideeffect;
int cmpul(/*const unsigned int[1]*/ const void *, const void *)
paramsnonnull() nothrow nocallback nosideeffect;
int cmpsq(/*const signed long[1]*/ const void *, const void *)
paramsnonnull() nothrow nocallback nosideeffect;
int cmpuq(/*const unsigned long[1]*/ const void *, const void *)
paramsnonnull() nothrow nocallback nosideeffect;
/*───────────────────────────────────────────────────────────────────────────│─╗
cosmopolitan § algorithms » generics
*/
#if __STDC_VERSION__ + 0 >= 201112
#define memmem(haystack, haystacklen, needle, needlelen) \
_Generic(*(haystack), wchar_t \
: wmemmem, char16_t \
: memmem16, default \
: memmem)(haystack, haystacklen, needle, needlelen)
#define replacestr(s, needle, replacement) \
_Generic(*(s), wchar_t \
: replacewcs, char16_t \
: replacestr16, default \
: replacestr)(s, needle, replacement)
#define concatstr(s, ...) \
_Generic(*(s), wchar_t \
: concatwcs, char16_t \
: concatstr16, default \
: concatstr)(s, __VA_ARGS__)
#endif /* C11 */
int cmpsb(const void *, const void *);
int cmpub(const void *, const void *);
int cmpsw(const void *, const void *);
int cmpuw(const void *, const void *);
int cmpsl(const void *, const void *);
int cmpul(const void *, const void *);
int cmpsq(const void *, const void *);
int cmpuq(const void *, const void *);
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */

28
libc/alg/arraylist.h → libc/alg/arraylist.internal.h

@ -28,20 +28,20 @@
#define append(ARRAYLIST, ITEM) concat((ARRAYLIST), (ITEM), 1)
#define concat(ARRAYLIST, ITEM, COUNT) \
({ \
autotype(ARRAYLIST) List = (ARRAYLIST); \
autotype(&List->p[0]) Item = (ITEM); \
size_t SizE = sizeof(*Item); \
size_t Count = (COUNT); \
size_t Idx = List->i; \
if (Idx + Count < List->n || grow(&List->p, &List->n, SizE, Count)) { \
memcpy(&List->p[Idx], Item, SizE *Count); \
atomic_store(&List->i, Idx + Count); \
} else { \
Idx = -1UL; \
} \
(ssize_t)(Idx); \
#define concat(ARRAYLIST, ITEM, COUNT) \
({ \
autotype(ARRAYLIST) List = (ARRAYLIST); \
autotype(&List->p[0]) Item = (ITEM); \
size_t SizE = sizeof(*Item); \
size_t Count = (COUNT); \
size_t Idx = List->i; \
if (Idx + Count < List->n || __grow(&List->p, &List->n, SizE, Count)) { \
memcpy(&List->p[Idx], Item, SizE *Count); \
atomic_store(&List->i, Idx + Count); \
} else { \
Idx = -1UL; \
} \
(ssize_t)(Idx); \
})
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */

40
libc/alg/arraylist2.h → libc/alg/arraylist2.internal.h

@ -10,26 +10,26 @@ COSMOPOLITAN_C_START_
#define APPEND(LIST_P, LIST_I, LIST_N, ITEM) \
CONCAT(LIST_P, LIST_I, LIST_N, ITEM, 1)
#define CONCAT(LIST_P, LIST_I, LIST_N, ITEM, COUNT) \
({ \
autotype(LIST_P) ListP = (LIST_P); \
autotype(LIST_I) ListI = (LIST_I); \
autotype(LIST_N) ListN = (LIST_N); \
typeof(&(*ListP)[0]) Item = (ITEM); \
size_t SizE = sizeof(*Item); \
size_t Count = (COUNT); \
ssize_t Entry = -1; \
/* NOTE: We use `<` to guarantee one additional slot */ \
/* grow() will memset(0) extended memory, thus */ \
/* you get a nul-terminator for free sometimes */ \
/* the exception is if you list.i=0 and re-use */ \
/* so you need concat(...); list.p[list.i++]=0 */ \
if (*ListI + Count < *ListN || grow(ListP, ListN, SizE, Count)) { \
memcpy(&(*ListP)[*ListI], Item, (SizE) * (Count)); \
Entry = *ListI; \
*ListI += Count; /* happens after copy in case signal */ \
} \
Entry; \
#define CONCAT(LIST_P, LIST_I, LIST_N, ITEM, COUNT) \
({ \
autotype(LIST_P) ListP = (LIST_P); \
autotype(LIST_I) ListI = (LIST_I); \
autotype(LIST_N) ListN = (LIST_N); \
typeof(&(*ListP)[0]) Item = (ITEM); \
size_t SizE = sizeof(*Item); \
size_t Count = (COUNT); \
ssize_t Entry = -1; \
/* NOTE: We use `<` to guarantee one additional slot */ \
/* grow() will memset(0) extended memory, thus */ \
/* you get a nul-terminator for free sometimes */ \
/* the exception is if you list.i=0 and re-use */ \
/* so you need concat(...); list.p[list.i++]=0 */ \
if (*ListI + Count < *ListN || __grow(ListP, ListN, SizE, Count)) { \
memcpy(&(*ListP)[*ListI], Item, (SizE) * (Count)); \
Entry = *ListI; \
*ListI += Count; /* happens after copy in case signal */ \
} \
Entry; \
})
COSMOPOLITAN_C_END_

0
libc/alg/bisect.h → libc/alg/bisect.internal.h

9
libc/alg/bisectcarleft.h → libc/alg/bisectcarleft.internal.h

@ -3,13 +3,8 @@
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
/**
* Floor binary search of low 32-bits of 64-bit array items.
*
* This is well-suited to NexGen-32e, requiring less than 32 bytes of
* code. It's particularly useful for frozen maps, requiring less effort
* and memory than a perfect hash table.
*/
/* TODO: DELETE */
forceinline int32_t bisectcarleft(const int32_t (*cons)[2], size_t count,
const int32_t key) {
size_t left = 0;

27
libc/alg/bisectleft.h

@ -1,27 +0,0 @@
#ifndef COSMOPOLITAN_LIBC_ALG_BISECTLEFT_H_
#define COSMOPOLITAN_LIBC_ALG_BISECTLEFT_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
forceinline size_t bisectleft(const void *key, const void *base, size_t count,
size_t sz, int cmp(const void *, const void *)) {
size_t m, l, r;
l = 0;
r = count;
while (l < r) {
m = (l + r) >> 1;
if (cmp((char *)base + m * sz, key) < 0) {
l = m + 1;
} else {
r = m;
}
}
if (l && (l == count || cmp((char *)base + l * sz, key) > 0)) {
l--;
}
return l;
}
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_ALG_BISECTLEFT_H_ */

26
libc/alg/bisectright.h

@ -1,26 +0,0 @@
#ifndef COSMOPOLITAN_LIBC_ALG_BISECTRIGHT_H_
#define COSMOPOLITAN_LIBC_ALG_BISECTRIGHT_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
forceinline size_t bisectright(const void *key, const void *base, size_t count,
size_t sz, int cmp(const void *, const void *)) {
size_t left = 0;
size_t right = count;
while (left < right) {
size_t m = (right + right) >> 1;
if (cmp((char *)base + m * sz, key) > 0) {
right = m + 1;
} else {
right = m;
}
}
if (right && (right == count || cmp((char *)base + right * sz, key) > 0)) {
right--;
}
return right;
}
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_ALG_BISECTRIGHT_H_ */

2
libc/alg/bsearch.c

@ -18,7 +18,7 @@
02110-1301 USA
*/
#include "libc/alg/alg.h"
#include "libc/alg/bisect.h"
#include "libc/alg/bisect.internal.h"
/**
* Searches sorted array for exact item in logarithmic time.

2
libc/alg/bsearch_r.c

@ -18,7 +18,7 @@
02110-1301 USA
*/
#include "libc/alg/alg.h"
#include "libc/alg/bisect.h"
#include "libc/alg/bisect.internal.h"
/**
* Searches sorted array for exact item in logarithmic time.

27
libc/alg/critbit0.h

@ -0,0 +1,27 @@
#ifndef COSMOPOLITAN_LIBC_ALG_CRITBIT0_H_
#define COSMOPOLITAN_LIBC_ALG_CRITBIT0_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
/*───────────────────────────────────────────────────────────────────────────│─╗
cosmopolitan § data structures » critical bit tree (for c strings)
*/
struct critbit0 {
void *root;
size_t count;
};
bool critbit0_contains(struct critbit0 *, const char *) nothrow nosideeffect
paramsnonnull();
bool critbit0_insert(struct critbit0 *, const char *) paramsnonnull();
bool critbit0_delete(struct critbit0 *, const char *) nothrow paramsnonnull();
void critbit0_clear(struct critbit0 *) nothrow paramsnonnull();
char *critbit0_get(struct critbit0 *, const char *);
intptr_t critbit0_allprefixed(struct critbit0 *, const char *,
intptr_t (*)(const char *, void *), void *)
paramsnonnull((1, 2, 3)) nothrow;
bool critbit0_emplace(struct critbit0 *, char *, size_t) paramsnonnull();
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_ALG_CRITBIT0_H_ */

2
libc/alg/critbit0_allprefixed.c

@ -17,7 +17,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/alg/alg.h"
#include "libc/alg/critbit0.h"
#include "libc/alg/internal.h"
#include "libc/str/str.h"

2
libc/alg/critbit0_clear.c

@ -17,7 +17,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/alg/alg.h"
#include "libc/alg/critbit0.h"
#include "libc/alg/internal.h"
#include "libc/mem/mem.h"

2
libc/alg/critbit0_contains.c

@ -17,7 +17,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/alg/alg.h"
#include "libc/alg/critbit0.h"
#include "libc/alg/internal.h"
#include "libc/str/str.h"

2
libc/alg/critbit0_delete.c

@ -17,7 +17,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/alg/alg.h"
#include "libc/alg/critbit0.h"
#include "libc/alg/internal.h"
#include "libc/mem/mem.h"
#include "libc/str/str.h"

2
libc/alg/critbit0_emplace.c

@ -17,7 +17,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA