Import C++ Standard Template Library

You can now use the hardest fastest and most dangerous language there is
with Cosmopolitan. So far about 75% of LLVM libcxx has been added. A few
breaking changes needed to be made to help this go smoothly.

- Rename nothrow to dontthrow
- Rename nodiscard to dontdiscard
- Add some libm functions, e.g. lgamma, nan, etc.
- Change intmax_t from int128 to int64 like everything else
- Introduce %jjd formatting directive for int128_t
- Introduce strtoi128(), strtou128(), etc.
- Rename bsrmax() to bsr128()

Some of the templates that should be working currently are std::vector,
std::string, std::map, std::set, std::deque, etc.
This commit is contained in:
Justine Tunney 2022-03-22 05:51:41 -07:00
parent 5022f9e920
commit 868af3f950
286 changed files with 123987 additions and 507 deletions

View file

@ -37,7 +37,7 @@
#include "third_party/gdtoa/gdtoa.h"
#include "third_party/getopt/getopt.h"
#define INT intmax_t
#define INT int128_t
#define FLOAT long double
#define EPSILON 1e-16l
@ -156,7 +156,7 @@ struct Value stack[128];
int sp, comment, line, column, interactive;
INT Popcnt(INT x) {
uintmax_t word = x;
uint128_t word = x;
return popcnt(word >> 64) + popcnt(word);
}
@ -165,7 +165,7 @@ char *Repr(struct Value x) {
if (x.t == kFloat) {
g_xfmt_p(buf, &x.f, 16, sizeof(buf), 0);
} else {
sprintf(buf, "%jd", x.i);
sprintf(buf, "%jjd", x.i);
}
return buf;
}
@ -449,7 +449,7 @@ bool ConsumeLiteral(const char *literal) {
literal_ = literal;
errno = 0;
x.t = kInt;
x.i = strtoimax(literal, &e, 0);
x.i = strtoi128(literal, &e, 0);
if (*e) {
x.t = kFloat;
x.f = strtod(literal, &e);

View file

@ -55,7 +55,7 @@ struct ElfWriter {
struct Interner *shstrtab;
};
struct ElfWriter *elfwriter_open(const char *, int) nodiscard;
struct ElfWriter *elfwriter_open(const char *, int) dontdiscard;
void elfwriter_cargoculting(struct ElfWriter *);
void elfwriter_close(struct ElfWriter *);
void elfwriter_align(struct ElfWriter *, size_t, size_t);

View file

@ -169,7 +169,7 @@ struct Machine {
void (*onlongbranch)(struct Machine *);
} forcealign(64);
struct Machine *NewMachine(void) nodiscard;
struct Machine *NewMachine(void) dontdiscard;
void FreeMachine(struct Machine *);
void ResetMem(struct Machine *);
void ResetCpu(struct Machine *);
@ -181,7 +181,7 @@ long AllocateLinearPage(struct Machine *);
long AllocateLinearPageRaw(struct Machine *);
int ReserveReal(struct Machine *, size_t);
int ReserveVirtual(struct Machine *, int64_t, size_t, uint64_t);
char *FormatPml4t(struct Machine *) nodiscard;
char *FormatPml4t(struct Machine *) dontdiscard;
int64_t FindVirtual(struct Machine *, int64_t, size_t);
int FreeVirtual(struct Machine *, int64_t, size_t);

View file

@ -65,7 +65,7 @@ struct Pty {
};
void FreePty(struct Pty *);
struct Pty *NewPty(void) nodiscard;
struct Pty *NewPty(void) dontdiscard;
void PtyResize(struct Pty *, int, int);
ssize_t PtyRead(struct Pty *, void *, size_t);
ssize_t PtyWrite(struct Pty *, const void *, size_t);

View file

@ -139,7 +139,8 @@ void CheckExists(const char *path) {
}
}
nodiscard char *MakeDeployScript(struct addrinfo *remotenic, size_t combytes) {
dontdiscard char *MakeDeployScript(struct addrinfo *remotenic,
size_t combytes) {
const char *ip4 = (const char *)&remotenic->ai_addr4->sin_addr;
return xasprintf("mkdir -p o/ && "
"dd bs=%zu count=%zu of=o/runitd.$$.com 2>/dev/null && "

View file

@ -169,7 +169,7 @@ void GetOpts(int argc, char *argv[]) {
}
}
nodiscard char *DescribeAddress(struct sockaddr_in *addr) {
dontdiscard char *DescribeAddress(struct sockaddr_in *addr) {
char ip4buf[16];
return xasprintf("%s:%hu",
inet_ntop(addr->sin_family, &addr->sin_addr.s_addr, ip4buf,

View file

@ -35,7 +35,7 @@ char *format(char *buf, const char *fmt, ...) {
return buf;
}
nodiscard char *tabpad(const char *s, unsigned width) {
dontdiscard char *tabpad(const char *s, unsigned width) {
char *p;
size_t i, l, need;
l = strlen(s);

View file

@ -6,20 +6,20 @@ COSMOPOLITAN_C_START_
#define COLUMN_WIDTH 24
#define showint(x) show(".long", format(b1, "%d", x), #x)
#define showint64(x) show(".quad", format(b1, "%ld", x), #x)
#define showbyte(x) show(".byte", format(b1, "%hhn", x), #x)
#define showshort(x) show(".short", format(b1, "%hn", x), #x)
#define showint(x) show(".long", format(b1, "%d", x), #x)
#define showint64(x) show(".quad", format(b1, "%ld", x), #x)
#define showbyte(x) show(".byte", format(b1, "%hhn", x), #x)
#define showshort(x) show(".short", format(b1, "%hn", x), #x)
#define showshorthex(x) show(".short", format(b1, "%#-6hX", x), #x)
#define showinthex(x) show(".long", format(b1, "%#X", x), #x)
#define showinthex(x) show(".long", format(b1, "%#X", x), #x)
#define showint64hex(x) show(".quad", format(b1, "%#lX", x), #x)
#define showorg(x) show(".org", format(b1, "%#lX", x), #x)
#define showorg(x) show(".org", format(b1, "%#lX", x), #x)
extern char b1[BUFSIZ];
extern char b2[BUFSIZ];
char *format(char *buf, const char *fmt, ...);
nodiscard char *tabpad(const char *s, unsigned width);
char *tabpad(const char *s, unsigned width) dontdiscard;
void show(const char *directive, const char *value, const char *comment);
COSMOPOLITAN_C_END_

View file

@ -29,7 +29,7 @@
* @param id is the flags
* @return NUL-terminated string that needs free()
*/
nodiscard char *RecreateFlags(const struct IdName *names, unsigned long id) {
dontdiscard char *RecreateFlags(const struct IdName *names, unsigned long id) {
bool first;
size_t bufi, bufn;
char *bufp, extrabuf[20];

View file

@ -4,7 +4,7 @@
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
char *RecreateFlags(const struct IdName *, unsigned long) nodiscard;
char *RecreateFlags(const struct IdName *, unsigned long) dontdiscard;
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */

View file

@ -105,7 +105,7 @@ int main(int argc, char *argv[]) {
strsep(&tok, ";");
category = strsep(&tok, ";");
if (!s1 || !category) continue;
bit = strtoimax(s1, NULL, 16);
bit = strtol(s1, NULL, 16);
if (bit != 0x00AD &&
((0x1160 <= bit && bit <= 0x11FF) ||
(strcmp(category, "Me") == 0 || strcmp(category, "Mn") == 0 ||

View file

@ -46,12 +46,12 @@
* @fileoverview Zip File Disassembler.
*/
nodiscard char *FormatDosDate(uint16_t dosdate) {
dontdiscard char *FormatDosDate(uint16_t dosdate) {
return xasprintf("%04u-%02u-%02u", ((dosdate >> 9) & 0b1111111) + 1980,
(dosdate >> 5) & 0b1111, dosdate & 0b11111);
}
nodiscard char *FormatDosTime(uint16_t dostime) {
dontdiscard char *FormatDosTime(uint16_t dostime) {
return xasprintf("%02u:%02u:%02u", (dostime >> 11) & 0b11111,
(dostime >> 5) & 0b111111, (dostime << 1) & 0b111110);
}

View file

@ -102,7 +102,7 @@ Keywords={
"decltype",
"forceinline",
"nocallersavedregisters",
"nothrow",
"dontthrow",
"nooptimize",
"optimizesize",
"optimizespeed",
@ -132,7 +132,7 @@ Keywords={
"warnifused",
"attributeallocsize",
"attributeallocalign",
"nodiscard",
"dontdiscard",
"nointerpose",
"compatfn",
"returnsnonnull",

View file

@ -46,7 +46,7 @@
"decltype"
"forceinline"
"nocallersavedregisters"
"nothrow"
"dontthrow"
"nooptimize"
"optimizesize"
"optimizespeed"
@ -79,7 +79,7 @@
"warnifused"
"attributeallocsize"
"attributeallocalign"
"nodiscard"
"dontdiscard"
"nointerpose"
"compatfn"
"returnsnonnull"

View file

@ -132,6 +132,7 @@
(or (locate-dominating-file (buffer-name) "Makefile")
(file-name-directory (buffer-name))))))
(add-hook 'c-mode-common-hook 'stop-asking-questions-etags)
(add-hook 'c++-mode-common-hook 'stop-asking-questions-etags)
(setq tags-revert-without-query t)
(setq kill-buffer-query-functions ;; disable kill buffer w/ process question
(delq 'process-kill-buffer-query-function kill-buffer-query-functions))
@ -268,6 +269,7 @@
(add-hook 'ld-script-mode-hook 'cosmo-compile-hook)
(add-hook 'dired-mode-hook 'cosmo-compile-hook)
(add-hook 'c-mode-common-hook 'cosmo-compile-hook)
(add-hook 'c++-mode-common-hook 'cosmo-compile-hook)
(add-hook 'fortran-mode-hook 'cosmo-compile-hook)
(add-hook 'protobuf-mode-hook 'cosmo-compile-hook))
@ -781,9 +783,14 @@
root mode (cosmo-file-name-sans-extensions name))))
(when buddy
(find-file buddy))))))
(defun cosmo-lisp-is-the-worst-this-is-so-tiring ()
(defun cosmo-lisp-is-the-best ()
(define-key c-mode-base-map (kbd "C-c C-o") 'cosmo-show-optinfo))
(add-hook 'c-mode-common-hook 'cosmo-lisp-is-the-worst-this-is-so-tiring)
(add-hook 'c-mode-common-hook 'cosmo-lisp-is-the-best)
(defun cosmo-lisp-is-the-best++ ()
(define-key c++-mode-base-map (kbd "C-c C-o") 'cosmo-show-optinfo))
(add-hook 'c++-mode-common-hook 'cosmo-lisp-is-the-best++)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@ -804,6 +811,7 @@
(,cosmo-platform-constants-regex . font-lock-constant-face))))
(add-hook 'c-mode-common-hook 'cosmo-c-keywords-hook)
(add-hook 'c++-mode-common-hook 'cosmo-c-keywords-hook)
(add-hook 'asm-mode-hook 'cosmo-asm-keywords-hook)

View file

@ -345,7 +345,7 @@ cosmo_kws = frozenset([
"memcpyesque",
"nocallback",
"nodebuginfo",
"nodiscard",
"dontdiscard",
"dontinline",
"noinstrument",
"nointerpose",
@ -353,8 +353,7 @@ cosmo_kws = frozenset([
"noprune",
"wontreturn",
"nosideeffect",
"nothrow",
"nothrow",
"dontthrow",
"null",
"nullterminated",
"paramsnonnull",
@ -408,7 +407,7 @@ cosmo_kws = frozenset([
"memcpyesque",
"nocallback",
"nodebuginfo",
"nodiscard",
"dontdiscard",
"dontinline",
"noinstrument",
"nointerpose",
@ -416,8 +415,7 @@ cosmo_kws = frozenset([
"noprune",
"wontreturn",
"nosideeffect",
"nothrow",
"nothrow",
"dontthrow",
"null",
"nullterminated",
"paramsnonnull",

View file

@ -34,7 +34,7 @@ int main(int argc, char *argv[]) {
exit(1);
}
static uint32_t tab[256];
crc32init(tab, strtoimax(argv[1], NULL, 0));
crc32init(tab, strtol(argv[1], NULL, 0));
for (unsigned i = 0; i < ARRAYLEN(tab); ++i) {
if (i > 0) {
printf(",");

View file

@ -86,7 +86,7 @@ struct Polls {
struct Sockets g_sockets;
struct Polls g_polls;
nodiscard char *DescribeAddress(struct sockaddr_in *addr) {
dontdiscard char *DescribeAddress(struct sockaddr_in *addr) {
char ip4buf[16];
return xasprintf("%s:%hu",
inet_ntop(addr->sin_family, &addr->sin_addr.s_addr, ip4buf,
@ -94,7 +94,7 @@ nodiscard char *DescribeAddress(struct sockaddr_in *addr) {
ntohs(addr->sin_port));
}
nodiscard char *DescribeSocket(struct Socket *s) {
dontdiscard char *DescribeSocket(struct Socket *s) {
return xasprintf("%s:%s", s->protocol == IPPROTO_UDP ? "udp" : "tcp",
gc(DescribeAddress(&s->addr)));
}

View file

@ -157,7 +157,7 @@ static MMDB_entry_data_list_s *LuaMaxmindDump(lua_State *L,
lua_pushinteger(L, dl->entry_data.uint64);
return dl->next;
case MMDB_DATA_TYPE_UINT128:
sprintf(ibuf, "%#jx", dl->entry_data.uint128);
sprintf(ibuf, "%#jjx", dl->entry_data.uint128);
lua_pushstring(L, ibuf);
return dl->next;
case MMDB_DATA_TYPE_DOUBLE:

View file

@ -153,7 +153,6 @@ o/$(MODE)/tool/net/redbean-demo.com.dbg: \
o/$(MODE)/tool/net/lmaxmind.o \
o/$(MODE)/tool/net/lsqlite3.o \
o/$(MODE)/tool/net/largon2.o \
o/$(MODE)/tool/net/lre.o \
o/$(MODE)/tool/net/net.pkg \
o/$(MODE)/tool/net/demo/sql.lua.zip.o \
o/$(MODE)/tool/net/demo/fetch.lua.zip.o \

View file

@ -29,10 +29,10 @@ double b64;
long double b80;
uint32_t u32;
uint64_t u64;
intmax_t x;
int128_t x;
void int2float(const char *s) {
x = strtoimax(s, NULL, 0);
x = strtoi128(s, NULL, 0);
if ((0 <= x && x <= UINT32_MAX) && !startswith(s, "-") &&
(!endswith(s, "l") && !endswith(s, "L"))) {
u32 = x;