// Star99: a single-file compiler from Star99 to C99. // Build: ./build.sh, or cc -std=c99 -O2 star99.c -o star99 // The design this implements is docs/design.md. #define _XOPEN_SOURCE 700 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include typedef uint8_t u8; typedef int32_t i32; typedef int64_t i64; typedef uint32_t u32; typedef uint64_t u64; typedef uintptr_t uptr; typedef ptrdiff_t iz; typedef double f64; typedef int b32; #define countof(a) (iz)(sizeof(a) / sizeof(*(a))) #define S(s) (Str){(u8 *)s, countof(s) - 1} #define SL(s) {(u8 *)s, countof(s) - 1} // S(s) in a static table #define new(a, n, t) (t *)alloc(a, n, (iz)sizeof(t)) #if defined(__GNUC__) || defined(__clang__) #define trap() __builtin_trap() #else #define trap() (*(volatile int *)0 = 0) #endif #if defined(__GNUC__) || defined(__clang__) || defined(__TINYC__) #define NORETURN __attribute__((noreturn)) #define UNUSED __attribute__((unused)) #else #define NORETURN #define UNUSED #endif // Mutable globals are marked STATE. The fuzz build keeps them together in // one section, so each test case can start from a clean program state. #ifdef FUZZ #define STATE __attribute__((section("star99_state"), no_sanitize("address"))) #else #define STATE #endif #define assert(c) \ do { \ if (!(c)) trap(); \ } while (0) // Memory // Every allocation lives in one arena for the whole compilation. Memory is // zeroed, aligned for any scalar, and never freed one object at a time. typedef struct { char *beg; char *end; } Arena; #ifdef FUZZ enum { ALIGN = 16, CHUNK = 4 << 10 }; // small chunks reach the chunk edges often #else enum { ALIGN = 16, CHUNK = 64 << 20 }; #endif NORETURN static void oom(void); #if defined(FUZZ) && defined(__has_feature) #if __has_feature(address_sanitizer) #include #define ARENA_ASAN #endif #endif #ifdef FUZZ static char *fuzz_chunks = 0; // every arena chunk, newest first #endif static void arena_chunk(Arena *a, iz need) { iz size = need + ALIGN > CHUNK ? need + ALIGN : CHUNK; #ifdef FUZZ // chunks are linked so each test case can free them; fresh memory is // filled so code relying on unzeroed memory shows char *p = malloc((size_t)size + sizeof(char *)); if (!p) oom(); memset(p, 0xa5, (size_t)size + sizeof(char *)); *(char **)p = fuzz_chunks; fuzz_chunks = p; p += sizeof(char *); #else char *p = malloc((size_t)size); if (!p) oom(); #endif a->beg = p; a->end = p + size; } static void * alloc(Arena *a, iz count, iz size) { assert(count >= 0 && size > 0); iz pad = -(uptr)a->beg & (ALIGN - 1); if (!a->beg || count >= (a->end - a->beg - pad) / size) { if (count > PTRDIFF_MAX / size - ALIGN) oom(); arena_chunk(a, count * size); pad = -(uptr)a->beg & (ALIGN - 1); } void *r = a->beg + pad; a->beg += pad + count * size; #ifdef ARENA_ASAN // a poisoned red zone after each allocation, so overruns into the next // one are caught (and slices never grow in place) if (a->end - a->beg >= ALIGN) { ASAN_POISON_MEMORY_REGION(a->beg, ALIGN); a->beg += ALIGN; } #endif return memset(r, 0, (size_t)(count * size)); } // Doubles a slice's capacity (8 for an empty one): in place when it is // the latest allocation, otherwise in a copy. static void * grow_(Arena *a, void *data, iz *cap, iz size) { iz old = *cap; assert(old >= 0 && size > 0 && (data || !old)); if (old > PTRDIFF_MAX / 2) oom(); // old + extend would overflow iz extend = old ? old : 8; if (data && a->beg == (char *)data + old * size && extend < (a->end - a->beg) / size) { // right after the slice, with no padding between (alloc would // align, and leave the bytes it skips unzeroed inside the slice) memset(a->beg, 0, (size_t)(extend * size)); a->beg += extend * size; *cap = old + extend; return data; } void *copy = alloc(a, old + extend, size); if (old) memcpy(copy, data, (size_t)(old * size)); *cap = old + extend; return copy; } #define push(a, s) \ ((s)->len >= (s)->cap \ ? (void)((s)->data = grow_((a), (s)->data, &(s)->cap, (iz)sizeof(*(s)->data))) \ : (void)0, \ (s)->data + (s)->len++) // Strings typedef struct { u8 *data; iz len; } Str; static STATE Arena perm = {0}; #ifdef FUZZ static Str fuzz_case = {0}; // the program being fuzzed #endif static b32 equals(Str a, Str b) { if (a.len != b.len) return 0; return !a.len || !memcmp(a.data, b.data, (size_t)a.len); } static u64 hash64(Str s, u64 seed) { u64 h = seed ^ 0x100; for (iz i = 0; i < s.len; i++) { h ^= s.data[i] & 255; h *= 1111111111111111111u; } return h ^ (h >> 32); } static Str copystr(Arena *a, Str s) { Str r = s; r.data = new(a, s.len, u8); if (s.len) memcpy(r.data, s.data, (size_t)s.len); return r; } static Str substr(Str s, iz beg, iz end) { assert(beg >= 0 && beg <= end && end <= s.len); return (Str){s.data + beg, end - beg}; } static b32 isdigitc(int c) { return c >= '0' && c <= '9'; } static b32 isalphac(int c) { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_'; } static b32 isupperc(int c) { return c >= 'A' && c <= 'Z'; } // The integer s starts with: an optional sign, then decimal digits (0 if // there are none). A value past the range of i64 is its nearest end. static i64 parse_int(Str s) { iz i = 0; b32 neg = s.len && s.data[0] == '-'; if (s.len && (s.data[0] == '-' || s.data[0] == '+')) i++; u64 v = 0; u64 max = neg ? (u64)INT64_MAX + 1 : (u64)INT64_MAX; for (; i < s.len && isdigitc(s.data[i]); i++) { u64 d = (u64)(s.data[i] - '0'); v = v > (max - d) / 10 ? max : v * 10 + d; } return neg ? (v ? -(i64)(v - 1) - 1 : 0) : (i64)v; } // Whether c is one of the bytes of set. static b32 oneof(u8 c, Str set) { for (iz i = 0; i < set.len; i++) { if (set.data[i] == c) return 1; } return 0; } // Where pat first appears in s, or -1. static iz indexof(Str s, Str pat) { if (!pat.len) return 0; for (iz i = 0; i <= s.len - pat.len; i++) { // memchr finds the candidates: a scan of the whole generated C // passes over megabytes u8 *c = memchr(s.data + i, pat.data[0], (size_t)(s.len - pat.len + 1 - i)); if (!c) return -1; i = c - s.data; if (!memcmp(c, pat.data, (size_t)pat.len)) return i; } return -1; } static b32 startswith(Str s, Str prefix) { return s.len >= prefix.len && (!prefix.len || !memcmp(s.data, prefix.data, (size_t)prefix.len)); } static b32 endswith(Str s, Str suffix) { iz at = s.len - suffix.len; return at >= 0 && (!suffix.len || !memcmp(s.data + at, suffix.data, (size_t)suffix.len)); } // A growable text buffer for building output. typedef struct { u8 *data; iz len; iz cap; } Buf; static void bput(Buf *b, Str s) { while (b->cap - b->len < s.len) b->data = grow_(&perm, b->data, &b->cap, 1); if (s.len > 0) memcpy(b->data + b->len, s.data, (size_t)s.len); b->len += s.len; } static void bputc(Buf *b, int c) { *push(&perm, b) = (u8)c; } static void bputu(Buf *b, u64 v) { u8 tmp[24] = {0}; iz n = 0; do tmp[n++] = (u8)('0' + v % 10); while (v /= 10); while (n) bputc(b, tmp[--n]); } static void bputi(Buf *b, i64 v) { if (v < 0) bputc(b, '-'); bputu(b, v < 0 ? (u64)0 - (u64)v : (u64)v); } // Appends fmt to b, as printf would but with sized strings: %s takes a // Str, %d an int, %lld a long long, %llu an unsigned long long, %c a // character, and %% is a percent sign. Any other conversion (%02x, %016llx, // %.17g, %a) takes what printf takes and is written by snprintf. static void vbputf(Buf *b, char *fmt, va_list ap) { for (char *f = fmt; *f;) { if (*f != '%') { char *e = f; while (*e && *e != '%') e++; bput(b, (Str){(u8 *)f, e - f}); f = e; continue; } char *spec = f++; switch (*f) { case 's': bput(b, va_arg(ap, Str)); break; case 'd': bputi(b, va_arg(ap, int)); break; case 'c': bputc(b, va_arg(ap, int)); break; case '%': bputc(b, '%'); break; case 'l': if (f[1] == 'l' && f[2] == 'd') { bputi(b, va_arg(ap, long long)); f += 2; break; } if (f[1] == 'l' && f[2] == 'u') { bputu(b, va_arg(ap, unsigned long long)); f += 2; break; } // fallthrough default: { // flags, a width, a precision, then the conversion while (*f && !((*f >= 'a' && *f <= 'z' && *f != 'l') || (*f >= 'A' && *f <= 'Z'))) f++; char one[16] = {0}; iz n = f + 1 - spec; assert(*f && oneof((u8)*f, S("diuoxXga")) && n < countof(one)); // a number memcpy(one, spec, (size_t)n); char out[64] = {0}; int len = 0; if (*f == 'g' || *f == 'a') { len = snprintf(out, sizeof(out), one, va_arg(ap, double)); } else if (f[-1] == 'l') { len = snprintf(out, sizeof(out), one, va_arg(ap, unsigned long long)); } else { len = snprintf(out, sizeof(out), one, va_arg(ap, int)); } assert(len >= 0 && len < (int)sizeof(out)); bput(b, (Str){(u8 *)out, len}); } } f++; } } static void bputf(Buf *b, char *fmt, ...) { va_list ap; // set by va_start va_start(ap, fmt); vbputf(b, fmt, ap); va_end(ap); } static Str bstr(Buf *b) { return (Str){b->data, b->len}; } // Lines of text, joined, each ending in a newline. static Str join_lines(Str *lines, iz n) { Buf b = {0}; for (iz i = 0; i < n; i++) { bput(&b, lines[i]); bputc(&b, '\n'); } return bstr(&b); } // s with every from replaced by to. static Str replace_all(Str s, Str from, Str to) { Buf b = {0}; for (iz i = 0; i < s.len;) { if (from.len && i + from.len <= s.len && !memcmp(s.data + i, from.data, (size_t)from.len)) { bput(&b, to); i += from.len; } else { bputc(&b, s.data[i++]); } } return bstr(&b); } // Formatted text of any length: generated C can hold long expressions. static Str fmtstr(char *fmt, ...) { Buf b = {0}; va_list ap; // set by va_start va_start(ap, fmt); vbputf(&b, fmt, ap); va_end(ap); return bstr(&b); } static Str concat(Str a, Str b) { if (b.len > PTRDIFF_MAX - a.len) oom(); Str r = {new(&perm, a.len + b.len, u8), a.len + b.len}; if (a.len) memcpy(r.data, a.data, (size_t)a.len); if (b.len) memcpy(r.data + a.len, b.data, (size_t)b.len); return r; } // A hash trie from strings to pointers, in the style of nullprogram. typedef struct Map Map; struct Map { Map *child[4]; Str key; void *value; }; static void ** upsert(Map **m, Str key, b32 create) { for (u64 h = hash64(key, 0); *m; h <<= 2) { if (equals(key, (*m)->key)) return &(*m)->value; m = &(*m)->child[h >> 62]; } if (!create) return 0; *m = new(&perm, 1, Map); (*m)->key = key; return &(*m)->value; } static void * lookup(Map *m, Str key) { void **v = upsert(&m, key, 0); return v ? *v : 0; } // Whether key is new to the set, which holds it from now on. static b32 first_time(Map **set, Str key) { void **seen = upsert(set, key, 1); if (*seen) return 0; *seen = (void *)1; return 1; } // A pointer as a key of a Map: its bytes. static Str ptrkey(void *p) { void **k = new(&perm, 1, void *); *k = p; return (Str){(u8 *)k, (iz)sizeof(p)}; } typedef struct { Str *data; iz len; iz cap; } Strs; // Platform: what the compiler asks of the operating system, in a few small // functions over Str and the arena. C strings, file descriptors and the // file, directory, process and environment calls of POSIX appear only // here (and in main, which turns argv into Str). // A copy of s with a zero byte after it, for a call into the C library. static char * tocstr(Str s) { char *r = new(&perm, s.len + 1, char); if (s.len) memcpy(r, s.data, (size_t)s.len); return r; } // A string the C library made, as a Str of the same bytes. static Str cstr(char *s) { return (Str){(u8 *)s, (iz)strlen(s)}; } // The number a decimal float literal spells, as strtod reads it: a correct // conversion is the C library's job. static f64 os_strtod(Str s) { return strtod(tocstr(s), 0); } // v with prec significant digits, as %.*g prints it. static Str os_gformat(f64 v, int prec) { char tmp[64] = {0}; int n = snprintf(tmp, sizeof(tmp), "%.*g", prec, v); return copystr(&perm, (Str){(u8 *)tmp, n}); } // All of s to the file descriptor fd: 1 is the standard output, 2 errors. static b32 os_write(i32 fd, Str s) { for (iz off = 0; off < s.len;) { iz n = write(fd, s.data + off, (size_t)(s.len - off)); if (n < 1) return 0; off += n; } return 1; } // Up to cap bytes from fd into buf: how many, 0 at the end, -1 on error. static iz os_read(i32 fd, u8 *buf, iz cap) { return read(fd, buf, (size_t)cap); } static void os_close(i32 fd) { close(fd); } NORETURN static void os_exit(i32 status) { exit(status); } // Ends a forked child at once, as _exit: nothing registered with atexit // runs twice. NORETURN static void os_child_exit(i32 status) { _exit(status); } typedef struct { Str data; b32 ok; } Contents; // The whole file at path; "-" is the standard input. static Contents os_readfile(Str path) { Contents r = {0}; i32 fd = equals(path, S("-")) ? 0 : open(tocstr(path), O_RDONLY); if (fd < 0) return r; Buf b = {0}; for (;;) { // read straight into the buffer's free space while (b.cap - b.len < 4096) b.data = grow_(&perm, b.data, &b.cap, 1); iz n = os_read(fd, b.data + b.len, b.cap - b.len); if (n < 0) { if (fd) os_close(fd); return r; } if (!n) break; b.len += n; } if (fd) os_close(fd); r.data = bstr(&b); r.ok = 1; return r; } static b32 os_writefile(Str path, Str data) { i32 fd = open(tocstr(path), O_WRONLY | O_CREAT | O_TRUNC, 0666); if (fd < 0) return 0; b32 ok = os_write(fd, data); ok &= !close(fd); return ok; } typedef struct { b32 exists, isdir; i64 size, mtime; } FileInfo; // What is at path, following a symbolic link unless nofollow. static FileInfo os_stat(Str path, b32 nofollow) { FileInfo r = {0}; struct stat st = {0}; int bad = nofollow ? lstat(tocstr(path), &st) : stat(tocstr(path), &st); if (bad) return r; r.exists = 1; r.isdir = S_ISDIR(st.st_mode); r.size = st.st_size; r.mtime = st.st_mtime; return r; } static b32 os_exists(Str path) { return !access(tocstr(path), F_OK); } static b32 os_runnable(Str path) { return !access(tocstr(path), X_OK); } static void os_mkdir(Str path) { mkdir(tocstr(path), 0777); } // Removes a file, or a directory that is empty. static b32 os_remove(Str path) { return !remove(tocstr(path)); } static b32 os_rename(Str from, Str to) { return !rename(tocstr(from), tocstr(to)); } // The absolute path of path without links, . or ..; empty if it cannot be // found. static Str os_realpath(Str path) { char buf[PATH_MAX] = {0}; return realpath(tocstr(path), buf) ? copystr(&perm, cstr(buf)) : (Str){0}; } typedef struct { Strs names; // but . and .., in the order the directory gives them b32 ok; } DirNames; static DirNames os_listdir(Str dir) { DirNames r = {0}; DIR *d = opendir(tocstr(dir)); if (!d) return r; for (struct dirent *e = readdir(d); e; e = readdir(d)) { Str name = cstr(e->d_name); if (!equals(name, S(".")) && !equals(name, S(".."))) *push(&perm, &r.names) = copystr(&perm, name); } closedir(d); r.ok = 1; return r; } static b32 os_chdir(Str dir) { return !chdir(tocstr(dir)); } static Str os_getcwd(void) { char buf[PATH_MAX] = {0}; return getcwd(buf, sizeof(buf)) ? copystr(&perm, cstr(buf)) : (Str){0}; } // From now on fd writes into the file at path, made empty first. static b32 os_redirect(i32 fd, Str path) { i32 f = open(tocstr(path), O_WRONLY | O_CREAT | O_TRUNC, 0666); if (f < 0) return 0; b32 ok = dup2(f, fd) == fd; if (f != fd) close(f); return ok; } // From now on fd writes where onto does. static void os_dup(i32 fd, i32 onto) { dup2(onto, fd); } // The value of an environment variable, empty when it is unset. static Str os_getenv(Str name) { char *v = getenv(tocstr(name)); return v ? copystr(&perm, cstr(v)) : (Str){0}; } static void os_setenv(Str name, Str value) { setenv(tocstr(name), tocstr(value), 1); } // Runs a command with the shell: its exit status, 127 when it cannot run, // 128 when a signal ends it. static i32 os_system(Str command) { int r = system(tocstr(command)); if (r == -1) return 127; if (WIFEXITED(r)) return WEXITSTATUS(r); return 128; } static i32 os_pid(void) { return (i32)getpid(); } // Whether the process pid exists. static b32 os_alive(i32 pid) { return !kill((pid_t)pid, 0) || errno != ESRCH; } // A copy of this process: 0 in the copy, its pid in this one, -1 if it fails. static i32 os_fork(void) { return (i32)fork(); } typedef struct { i32 pid; i32 code; // the exit status, or minus the signal that ended it } Ended; // Waits for the child pid to end, or for any child when pid is 0. static Ended os_wait(i32 pid) { Ended r = {0}; int st = 0; r.pid = (i32)(pid ? waitpid((pid_t)pid, &st, 0) : wait(&st)); r.code = WIFEXITED(st) ? WEXITSTATUS(st) : -WTERMSIG(st); return r; } static char ** os_argv(Strs args) { char **r = new(&perm, args.len + 1, char *); for (iz i = 0; i < args.len; i++) r[i] = tocstr(args.data[i]); return r; } // Replaces this process by the program args names, found in PATH; returns // only if it cannot. static void os_exec(Strs args) { char **argv = os_argv(args); execvp(argv[0], argv); } // A child process on two pipes: it reads requests on its fd 3 and writes // answers on its fd 4, and its standard output is our standard error. The // environment variable name is set to value for it. pid is 0 if it could // not start. typedef struct { i32 pid; i32 req; // where requests go i32 rep; // where answers come from } Piped; static Piped os_start_piped(Strs args, Str name, Str value) { Piped r = {0}; int req[2] = {0}, rep[2] = {0}; if (pipe(req) || pipe(rep)) return r; char **argv = os_argv(args); char *n = tocstr(name), *v = tocstr(value); pid_t pid = fork(); if (pid < 0) return r; if (!pid) { dup2(req[0], 3); dup2(rep[1], 4); dup2(2, 1); for (int fd = 5; fd < 64; fd++) close(fd); setenv(n, v, 1); execv(argv[0], argv); _exit(127); } close(req[0]); close(rep[1]); r.pid = (i32)pid; r.req = req[1]; r.rep = rep[0]; return r; } // Files typedef struct { i32 file; i32 offset; // where errors point i32 lo, hi; // the source the token or node spans, when hi > lo } Pos; typedef struct { Str path; Str text; i32 module; b32 code_import; b32 generated; // made by a macro: origin is the call in real source b32 builtin; // made by a prelude macro: errors show the call instead i32 macro_module; // 1 + the module whose macro made it (its private names work), or 0 Map *own_names; // generated: the offsets of names the macro template itself wrote Pos origin; struct { i32 *data; // where each comment starts iz len; iz cap; } comments; struct { i32 *data; // where each line starts, made on first use iz len; iz cap; } lines; } SourceFile; static STATE struct { SourceFile *data; iz len; iz cap; } files = {0}; // Errors stop compilation at the first one, with the source line underlined. // A table of where each line starts, made on first use: finding a line is // then a search, not a scan from the start of the file. static void index_lines(SourceFile *f) { if (f->lines.len) return; *push(&perm, &f->lines) = 0; for (iz i = 0; i < f->text.len; i++) if (f->text.data[i] == '\n') *push(&perm, &f->lines) = (i32)(i + 1); } typedef struct { i32 line, col; Str text; // the whole line } Location; static Location location(Pos p) { Location r = {0}; SourceFile *f = files.data + p.file; index_lines(f); iz off = p.offset < f->text.len ? p.offset : f->text.len; iz lo = 0, hi = f->lines.len - 1; // the last line that starts at or before off while (lo < hi) { iz mid = (lo + hi + 1) / 2; if (f->lines.data[mid] <= off) lo = mid; else hi = mid - 1; } iz start = f->lines.data[lo]; iz end = start; while (end < f->text.len && f->text.data[end] != '\n') end++; r.line = (i32)lo + 1; r.col = (i32)(p.offset - start) + 1; r.text = substr(f->text, start, end); return r; } static void report(Str kind, Pos p, Str msg) { Buf b = {0}; if (p.file < 0 || p.file >= files.len) { bputf(&b, "star99: %s: %s\n", kind, msg); os_write(2, bstr(&b)); return; } b32 span = p.hi > p.lo; if (span) p.offset = p.lo; Location at = location(p); i32 line = at.line, col = at.col; Str text = at.text; bputf(&b, "%s:%d:%d: %s: %s\n", files.data[p.file].path, line, col, kind, msg); if (text.len > 120) { // show the part of a long line around the column iz beg = col > 60 ? col - 60 : 0; iz end = beg + 120 < text.len ? beg + 120 : text.len; text = substr(text, beg, end); col -= (i32)beg; } bputf(&b, "%5d | %s\n | ", line, text); for (i32 i = 1; i < col; i++) bputc(&b, i - 1 < text.len && text.data[i - 1] == '\t' ? '\t' : ' '); // the whole span, up to the end of its first line iz width = span ? p.hi - p.lo : 1; if (width > text.len - (col - 1)) width = text.len - (col - 1); for (iz i = 0; i < (width < 1 ? 1 : width); i++) bputc(&b, '^'); bputc(&b, '\n'); os_write(2, bstr(&b)); } static STATE Str help_text = {0}; // The notes the next error or warning shows under it. typedef struct { Pos pos; Str msg; } Note; static STATE struct { Note *data; iz len; iz cap; } notes = {0}; static void note(Pos p, Str msg) { Note *n = push(&perm, ¬es); n->pos = p; n->msg = msg; } #ifdef FUZZ static jmp_buf fuzz_done = {0}; // a compile error ends the test case, not the process #endif NORETURN static void error_exit(void) { #ifdef FUZZ longjmp(fuzz_done, 1); #else os_exit(1); #endif } // Messages say "a %s" for any type; before a vowel that becomes "an". static Str with_articles(Str msg) { Buf b = {0}; for (iz i = 0; i < msg.len; i++) { u8 c = msg.data[i]; bputc(&b, c); if ((!i || msg.data[i - 1] == ' ') && c == 'a' && i + 2 < msg.len && msg.data[i + 1] == ' ') { u8 next = msg.data[i + 2]; if (next == 'A' || next == 'E' || next == 'I' || next == 'O') bputc(&b, 'n'); } } return bstr(&b); } // An error or warning at p: the message, its notes, then the help line // (a fix written after "; write" and the like in fmt moves there too). static STATE Pos format_call = {-1, 0, 0, 0}; // a format() call being answered static void stop_metaprogram(void); static void diagnose(Str kind, Pos p, char *fmt, va_list ap) { Buf b = {0}; vbputf(&b, fmt, ap); Str msg = bstr(&b); if (format_call.file >= 0 && p.file >= 0 && equals(files.data[p.file].path, S("format()")) && equals(kind, S("error"))) { // format(text) on text that does not parse: the call is at fault notes.len = 0; note(p, with_articles(msg)); Pos call = format_call; format_call = (Pos){-1, 0, 0, 0}; stop_metaprogram(); msg = S("format() got text that is not Star99"); p = call; } if (p.file >= 0 && files.data[p.file].builtin) { // unreachable(), reversed(...): the call is what the program wrote p = files.data[p.file].origin; iz k = 0; for (iz i = 0; i < notes.len; i++) { Pos at = notes.data[i].pos; if (at.file != p.file || at.offset != p.offset) notes.data[k++] = notes.data[i]; } notes.len = k; } static Str fixes[] = {SL("; write "), SL("; use "), SL("; add "), SL("; combine "), SL("; compute "), SL("; name "), SL("; call "), SL("; choose "), SL(": write "), SL(": pass ")}; for (iz i = 0; i < countof(fixes) && !help_text.len; i++) { iz at = indexof(msg, fixes[i]); if (at < 0) continue; help_text = substr(msg, at + 2, msg.len); // with articles below msg = substr(msg, 0, at); } report(kind, p, with_articles(msg)); if (help_text.len) help_text = with_articles(help_text); for (iz i = 0; i < notes.len; i++) report(S("note"), notes.data[i].pos, notes.data[i].msg); Buf help = {0}; for (iz i = 0, j = 0; i <= help_text.len && help_text.len; i++) { // later lines under the first if (i < help_text.len && help_text.data[i] != '\n') continue; bputf(&help, "%s%s\n", j ? S(" ") : S(" help: "), substr(help_text, j, i)); j = i + 1; } os_write(2, bstr(&help)); help_text = (Str){0}; notes.len = 0; } NORETURN static void errorat(Pos p, char *fmt, ...) { va_list ap; // set by va_start va_start(ap, fmt); diagnose(S("error"), p, fmt, ap); va_end(ap); error_exit(); } static void warnat(Pos p, char *fmt, ...) { va_list ap; // set by va_start va_start(ap, fmt); diagnose(S("warning"), p, fmt, ap); va_end(ap); } NORETURN static void fatal(char *fmt, ...) { Buf b = {0}; va_list ap; // set by va_start va_start(ap, fmt); vbputf(&b, fmt, ap); va_end(ap); os_write(2, fmtstr("star99: %s\n", bstr(&b))); error_exit(); } NORETURN static void oom(void) { os_write(2, S("star99: out of memory\n")); os_exit(1); } // Lexer typedef enum { T_EOF, T_NEWLINE, T_NAME, // snake_case T_TYPE, // CamelCase T_INT, T_FLOAT, T_STRING, T_LPAREN, T_RPAREN, T_LBRACKET, T_RBRACKET, T_LBRACE, T_RBRACE, T_COMMA, T_SEMI, T_COLON, T_DOT, T_DOTSTAR, // .* T_DOTLT, // ..< T_DOTEQ, // ..= T_LTDOTLT, // <..< T_LTDOTEQ, // <..= T_ARROW, // -> T_DOLLAR, T_AMP, T_PLUS, T_MINUS, T_STAR, T_SLASH, T_PERCENT, T_EQ, // = T_EQEQ, T_NE, T_LT, T_LE, T_GT, T_GE, T_PLUSEQ, T_MINUSEQ, T_STAREQ, T_SLASHEQ, T_PERCENTEQ, T_UNDERSCORE, T_STARSTAR, // ** // keywords T_FN, T_STRUCT, T_UNION, T_VAR, T_CONST, T_IF, T_ELSE, T_FOR, T_WHILE, T_IN, T_NOT, T_AND, T_OR, T_RETURN, T_BREAK, T_CONTINUE, T_SWITCH, T_CASE, T_DEFAULT, T_DO, T_MACRO, T_IMPORT, T_PRIVATE, T_LABEL, T_MODULE, T_COLLECTION, T_NIL, T_TRUE, T_FALSE, T_DEFER, T_AS, T_COUNT } TokKind; static Str tokname[T_COUNT] = { [T_EOF] = SL("end of file"), [T_NEWLINE] = SL("end of line"), [T_NAME] = SL("name"), [T_TYPE] = SL("type name"), [T_INT] = SL("integer"), [T_FLOAT] = SL("number"), [T_STRING] = SL("string"), [T_LPAREN] = SL("'('"), [T_RPAREN] = SL("')'"), [T_LBRACKET] = SL("'['"), [T_RBRACKET] = SL("']'"), [T_LBRACE] = SL("'{'"), [T_RBRACE] = SL("'}'"), [T_COMMA] = SL("','"), [T_SEMI] = SL("';'"), [T_COLON] = SL("':'"), [T_DOT] = SL("'.'"), [T_DOTSTAR] = SL("'.*'"), [T_DOTLT] = SL("'..<'"), [T_DOTEQ] = SL("'..='"), [T_LTDOTLT] = SL("'<..<'"), [T_LTDOTEQ] = SL("'<..='"), [T_ARROW] = SL("'->'"), [T_DOLLAR] = SL("'$'"), [T_AMP] = SL("'&'"), [T_PLUS] = SL("'+'"), [T_MINUS] = SL("'-'"), [T_STAR] = SL("'*'"), [T_SLASH] = SL("'/'"), [T_PERCENT] = SL("'%'"), [T_EQ] = SL("'='"), [T_EQEQ] = SL("'=='"), [T_NE] = SL("'!='"), [T_LT] = SL("'<'"), [T_LE] = SL("'<='"), [T_GT] = SL("'>'"), [T_GE] = SL("'>='"), [T_PLUSEQ] = SL("'+='"), [T_MINUSEQ] = SL("'-='"), [T_STAREQ] = SL("'*='"), [T_SLASHEQ] = SL("'/='"), [T_PERCENTEQ] = SL("'%='"), [T_STARSTAR] = SL("'**'"), [T_UNDERSCORE] = SL("'_'"), [T_FN] = SL("fn"), [T_STRUCT] = SL("struct"), [T_UNION] = SL("union"), [T_VAR] = SL("var"), [T_CONST] = SL("const"), [T_IF] = SL("if"), [T_ELSE] = SL("else"), [T_FOR] = SL("for"), [T_WHILE] = SL("while"), [T_IN] = SL("in"), [T_NOT] = SL("not"), [T_AND] = SL("and"), [T_OR] = SL("or"), [T_RETURN] = SL("return"), [T_BREAK] = SL("break"), [T_CONTINUE] = SL("continue"), [T_SWITCH] = SL("switch"), [T_CASE] = SL("case"), [T_DEFAULT] = SL("default"), [T_DO] = SL("do"), [T_MACRO] = SL("macro"), [T_IMPORT] = SL("import"), [T_PRIVATE] = SL("private"), [T_LABEL] = SL("label"), [T_MODULE] = SL("module"), [T_COLLECTION] = SL("collection"), [T_NIL] = SL("nil"), [T_TRUE] = SL("true"), [T_FALSE] = SL("false"), [T_DEFER] = SL("defer"), [T_AS] = SL("as"), }; static b32 range_open_low(TokKind op) { return op == T_LTDOTLT || op == T_LTDOTEQ; } static b32 range_open_high(TokKind op) { return op == T_DOTLT || op == T_LTDOTLT; } static b32 range_token(TokKind op) { return op == T_DOTLT || op == T_DOTEQ || range_open_low(op); } typedef struct { TokKind kind; Pos pos; Str text; // the token as written; for strings, the decoded value i64 ival; f64 fval; b32 space_before; } Tok; typedef struct { Tok *data; iz len; iz cap; } Toks; // Each file's tokens, made on its first lex(): later passes ask again. static STATE struct { Toks *data; iz len; iz cap; } lexed = {0}; // s is a word of at least one letter. The keywords by first letter, made // once: most words are compared with none. static TokKind keyword(Str s) { static int by_first[256][8] = {0}, nby_first[256] = {0}; static b32 ready = 0; if (!ready) { ready = 1; for (int k = T_FN; k < T_COUNT; k++) { u8 c = tokname[k].data[0]; assert(nby_first[c] < 8); by_first[c][nby_first[c]++] = k; } } u8 c = s.data[0]; for (int j = 0; j < nby_first[c]; j++) { int k = by_first[c][j]; if (equals(tokname[k], s)) return (TokKind)k; } return T_EOF; } // Which bytes go on with a name: letters, digits and _ (a table, since // lexing looks at every byte). static u8 * name_bytes(void) { static u8 t[256] = {0}; if (!t['a']) for (int c = 0; c < 256; c++) t[c] = (u8)(isalphac(c) || isdigitc(c)); return t; } // Decodes a double-quoted string body, reporting bad escapes. static Str unescape(Str body, Pos p) { Buf b = {0}; for (iz i = 0; i < body.len; i++) { u8 c = body.data[i]; if (c != '\\') { bputc(&b, c); continue; } if (++i >= body.len) break; Pos ep = {p.file, p.offset + (i32)i, 0, 0}; switch (body.data[i]) { case 'n': bputc(&b, '\n'); break; case 't': bputc(&b, '\t'); break; case 'r': bputc(&b, '\r'); break; case '0': bputc(&b, 0); break; case '\\': bputc(&b, '\\'); break; case '"': bputc(&b, '"'); break; case '\'': bputc(&b, '\''); break; case 'x': { int v = 0; for (int k = 0; k < 2; k++) { if (++i >= body.len) errorat(ep, "\\x needs two hexadecimal digits"); int d = body.data[i]; if (isdigitc(d)) v = v * 16 + d - '0'; else if (d >= 'a' && d <= 'f') v = v * 16 + d - 'a' + 10; else if (d >= 'A' && d <= 'F') v = v * 16 + d - 'A' + 10; else errorat(ep, "\\x needs two hexadecimal digits"); } bputc(&b, v); } break; default: errorat(ep, "unknown escape '\\%c'", body.data[i]); } } return copystr(&perm, bstr(&b)); } // Where the """ that closes a raw string whose text starts at at begins: at // the first """, as the text holds none. A run of four or five quotes there // ends it with one or two quotes of the text ("""say "hi"""" is say "hi"), // since a string never directly follows a string. Six or more would hold a // """: an error, as is no closing """ at all. static iz raw_close(Str s, iz at, Pos open) { iz j = at; while (j + 2 < s.len && !(s.data[j] == '"' && s.data[j + 1] == '"' && s.data[j + 2] == '"')) j++; if (j + 2 >= s.len) errorat(open, "this raw string is never closed with \"\"\""); iz run = j; while (run < s.len && s.data[run] == '"') run++; if (run - j > 5) errorat((Pos){open.file, (i32)j, 0, 0}, "a raw string cannot hold \"\"\": it ends at the first \"\"\" (one or " "two quotes just before it are its text)"); return run - 3; } // A raw string: """ then text then """, nothing escaped. A first line break // after the opening """ and the indentation of the closing """ are removed. static Str longstring(Str raw) { iz beg = 0; if (beg < raw.len && raw.data[beg] == '\n') beg++; iz end = raw.len; iz fence = end; while (fence > beg && (raw.data[fence - 1] == ' ' || raw.data[fence - 1] == '\t')) fence--; iz indent = 0; if (fence > beg && raw.data[fence - 1] == '\n') { indent = end - fence; end = fence - 1; } else if (fence == beg) { end = beg; } Buf b = {0}; iz i = beg; while (i < end) { iz skip = 0; while (skip < indent && i < end && (raw.data[i] == ' ' || raw.data[i] == '\t')) { i++; skip++; } while (i < end && raw.data[i] != '\n') bputc(&b, raw.data[i++]); if (i < end) bputc(&b, raw.data[i++]); } return copystr(&perm, bstr(&b)); } static Toks lex(i32 file) { while (lexed.len <= file) *push(&perm, &lexed) = (Toks){0}; if (lexed.data[file].data) return lexed.data[file]; Str s = files.data[file].text; u8 *more_name = name_bytes(); Toks t = {0}; iz i = 0; b32 space = 1; while (i < s.len) { if (t.len && !t.data[t.len - 1].pos.hi) t.data[t.len - 1].pos.hi = (i32)i; u8 c = s.data[i]; Pos p = {file, (i32)i, (i32)i, 0}; if (c == ' ' || c == '\t' || c == '\r') { while (++i < s.len && (s.data[i] == ' ' || s.data[i] == '\t' || s.data[i] == '\r')) { } space = 1; continue; } if (c == '#') { *push(&perm, &files.data[file].comments) = (i32)i; u8 *nl = memchr(s.data + i, '\n', (size_t)(s.len - i)); i = nl ? nl - s.data : s.len; continue; } Tok *k = push(&perm, &t); k->pos = p; k->space_before = space; space = 0; iz start = i; if (c == '\n') { k->kind = T_NEWLINE; i++; space = 1; } else if (isdigitc(c)) { b32 isfloat = 0; if (c == '0' && i + 1 < s.len && (s.data[i + 1] == 'x' || s.data[i + 1] == 'b')) { int base = s.data[i + 1] == 'x' ? 16 : 2; i += 2; u64 v = 0; iz digits = 0; for (; i < s.len; i++) { int d = s.data[i]; if (d == '_') continue; int dv = isdigitc(d) ? d - '0' : (d >= 'a' && d <= 'f') ? d - 'a' + 10 : (d >= 'A' && d <= 'F') ? d - 'A' + 10 : 99; if (dv >= base) break; if (v > (UINT64_MAX - (u64)dv) / (u64)base) errorat(p, "this number is larger than any integer type holds"); v = v * (u64)base + (u64)dv; digits++; } if (!digits) errorat(p, "number has no digits"); k->kind = T_INT; k->ival = (i64)v; } else { while (i < s.len && (isdigitc(s.data[i]) || s.data[i] == '_')) i++; if (i + 1 < s.len && s.data[i] == '.' && isdigitc(s.data[i + 1])) { isfloat = 1; i++; while (i < s.len && (isdigitc(s.data[i]) || s.data[i] == '_')) i++; } if (i < s.len && (s.data[i] == 'e' || s.data[i] == 'E')) { iz j = i + 1; if (j < s.len && (s.data[j] == '+' || s.data[j] == '-')) j++; if (j < s.len && isdigitc(s.data[j])) { isfloat = 1; i = j; while (i < s.len && isdigitc(s.data[i])) i++; } } if (isfloat) { Buf digits = {0}; // without their underscores for (iz j = start; j < i; j++) { if (s.data[j] != '_') bputc(&digits, s.data[j]); } k->kind = T_FLOAT; k->fval = os_strtod(bstr(&digits)); } else { u64 v = 0; for (iz j = start; j < i; j++) { if (s.data[j] == '_') continue; u64 d = (u64)(s.data[j] - '0'); if (v > (UINT64_MAX - d) / 10) { p.hi = (i32)i; errorat(p, "this number is larger than any integer type holds"); } v = v * 10 + d; } k->kind = T_INT; k->ival = (i64)v; } } if (i < s.len && isalphac(s.data[i])) errorat((Pos){file, (i32)i, 0, 0}, "unexpected letter after number"); } else if (isalphac(c)) { while (i < s.len && more_name[s.data[i]]) i++; Str w = substr(s, start, i); TokKind kw = keyword(w); if (kw != T_EOF) k->kind = kw; else if (w.len == 1 && w.data[0] == '_') k->kind = T_UNDERSCORE; else if (isupperc(c)) k->kind = T_TYPE; else k->kind = T_NAME; } else if (c == '"' && i + 2 < s.len && s.data[i + 1] == '"' && s.data[i + 2] == '"') { iz close = raw_close(s, i + 3, p); k->kind = T_STRING; k->text = longstring(substr(s, i + 3, close)); i = close + 3; continue; } else if (c == '"') { i++; while (i < s.len && s.data[i] != '"' && s.data[i] != '\n') { if (s.data[i] == '\\') i++; i++; } if (i >= s.len || s.data[i] != '"') errorat(p, "string is not closed on this line"); i++; k->kind = T_STRING; k->text = unescape(substr(s, start + 1, i - 1), (Pos){file, (i32)start + 1, 0, 0}); continue; } else if (c == '\'') { // a character literal: one byte, with the escapes of strings iz j = i + 1; while (j < s.len && s.data[j] != '\'' && s.data[j] != '\n') j += s.data[j] == '\\' ? 2 : 1; if (j >= s.len || s.data[j] != '\'') errorat(p, "a character literal is closed with ' on its line"); Str v = unescape(substr(s, i + 1, j), (Pos){file, (i32)i + 1, 0, 0}); if (v.len != 1) errorat(p, "a character literal is exactly one byte; this is %lld bytes%s", (long long)v.len, v.len > 1 ? S(" (use a String for more, or U8(n) for a byte by number)") : S("")); k->kind = T_INT; k->ival = v.data[0]; i = j + 1; k->text = substr(s, start, i); // as written, so it prints back the same continue; } else if (c == '`') { b32 fence = i + 2 < s.len && s.data[i + 1] == '`' && s.data[i + 2] == '`'; errorat(p, "%s is not a string: write \"\"\"...\"\"\", a raw string (no escapes; " "one line or many)", fence ? S("```...```") : S("`...`")); } else { static struct { Str text; TokKind kind; } ops[] = { {SL("<..<"), T_LTDOTLT}, {SL("<..="), T_LTDOTEQ}, {SL("..<"), T_DOTLT}, {SL("..="), T_DOTEQ}, {SL(".*"), T_DOTSTAR}, {SL("->"), T_ARROW}, {SL("**"), T_STARSTAR}, {SL("=="), T_EQEQ}, {SL("!="), T_NE}, {SL("<="), T_LE}, {SL(">="), T_GE}, {SL("+="), T_PLUSEQ}, {SL("-="), T_MINUSEQ}, {SL("*="), T_STAREQ}, {SL("/="), T_SLASHEQ}, {SL("%="), T_PERCENTEQ}, {SL("("), T_LPAREN}, {SL(")"), T_RPAREN}, {SL("["), T_LBRACKET}, {SL("]"), T_RBRACKET}, {SL("{"), T_LBRACE}, {SL("}"), T_RBRACE}, {SL(","), T_COMMA}, {SL(";"), T_SEMI}, {SL(":"), T_COLON}, {SL("."), T_DOT}, {SL("$"), T_DOLLAR}, {SL("&"), T_AMP}, {SL("+"), T_PLUS}, {SL("-"), T_MINUS}, {SL("*"), T_STAR}, {SL("/"), T_SLASH}, {SL("%"), T_PERCENT}, {SL("="), T_EQ}, {SL("<"), T_LT}, {SL(">"), T_GT}, }; iz n = 0; u8 after = i + 2 < s.len ? s.data[i + 2] : ' '; if (i + 1 < s.len && (c == '&' || c == '|' || c == '+' || c == '-') && s.data[i + 1] == c && (c == '&' || c == '|' || !(isdigitc(after) || isalphac(after) || after == '('))) k->kind = T_COUNT; // && || x++ x--: reported below for (iz o = 0; k->kind != T_COUNT && o < countof(ops); o++) { if (ops[o].text.data[0] != c) continue; // most start otherwise Str op = ops[o].text; if (startswith(substr(s, i, s.len), op)) { k->kind = ops[o].kind; n = op.len; break; } } if (!n) { // operators of other languages static Str other[][2] = {{SL("&&"), SL("and")}, {SL("||"), SL("or")}, {SL("!"), SL("not")}, {SL("++"), SL("+= 1")}, {SL("--"), SL("-= 1")}}; for (iz o = 0; o < countof(other); o++) { if (startswith(substr(s, i, s.len), other[o][0])) { help_text = fmtstr("write %s", other[o][1]); p.hi = p.lo + (i32)other[o][0].len; errorat(p, "Star99 has no %s operator", other[o][0]); } } if (c == '?') { help_text = S("write if c do a else b"); errorat(p, "Star99 has no ?: operator"); } errorat(p, "unexpected character '%c'", c); } if (k->kind == T_DOT && i + 1 < s.len && s.data[i + 1] == '.') { help_text = S("write a..toks.data + p->i; } static Tok * peekat(Parser *p, iz k) { iz j = p->i + k; if (j >= p->toks.len) j = p->toks.len - 1; return p->toks.data + j; } static TokKind pk(Parser *p) { return peek(p)->kind; } static Tok * next(Parser *p) { Tok *t = peek(p); if (t->kind != T_EOF) p->i++; return t; } static b32 accept(Parser *p, TokKind k) { if (pk(p) == k) { next(p); return 1; } return 0; } static void skipnl(Parser *p) { while (pk(p) == T_NEWLINE) next(p); } // Keep line breaks at list boundaries, not inside a nested entry. static void skip_list_nl(Parser *p, Nodes *out) { out->multiline |= pk(p) == T_NEWLINE; skipnl(p); } NORETURN static void unexpected(Parser *p, Str wanted) { Tok *t = peek(p); Str kw = tokname[t->kind]; if (t->kind >= T_FN && indexof(wanted, S("name")) >= 0) { help_text = S("choose another name"); errorat(t->pos, "expected %s, found %s, which is a keyword", wanted, kw); } if (t->kind == T_NAME && equals(t->text, S("then"))) help_text = S("write if c do a else b, or if c { ... }"); b32 text = t->kind == T_NAME || t->kind == T_TYPE || t->kind == T_INT || t->kind == T_FLOAT; errorat(t->pos, "expected %s, found %s", wanted, text ? t->text : kw); } static Tok * expect(Parser *p, TokKind k) { if (pk(p) != k) unexpected(p, tokname[k]); return next(p); } static Node *parse_expr(Parser *p); static Node *parse_binary(Parser *p, int minprec); static Node *parse_primary(Parser *p); static Node *parse_type(Parser *p); static Node *parse_block(Parser *p); static Node *parse_stmt(Parser *p); static Node *parse_decl(Parser *p, b32 toplevel); static Node *parse_if(Parser *p, b32 asvalue); static Node *parse_switch(Parser *p, b32 asvalue); static b32 is_type_start(TokKind k) { return k == T_TYPE || k == T_STRUCT || k == T_UNION || k == T_FN; } // A parameter list: entries separated by commas or newlines. Names joined by // commas share the type or default that follows them. In a union, an entry // may be a bare type, a variant named after that type. static void parse_params(Parser *p, Nodes *out, b32 isunion) { expect(p, T_LPAREN); for (;;) { skip_list_nl(p, out); if (pk(p) == T_RPAREN) break; b32 isvar = 0, israw = 0, isprivate = 0; if (accept(p, T_PRIVATE)) isprivate = 1; if (accept(p, T_VAR)) isvar = 1; if (isunion && is_type_start(pk(p)) && !(pk(p) == T_TYPE && peekat(p, 1)->kind == T_COLON)) { Node *v = mknode(N_PARAM, peek(p)->pos); v->a = parse_type(p); v->flags |= F_HASTYPE; addnode(out, v); } else { Nodes group = {0}; for (;;) { Tok *t = peek(p); Node *n = mknode(N_PARAM, t->pos); if (t->kind == T_DOLLAR && p->template_depth) { n->c = parse_primary(p); // $name in a template n->name = S("$"); } else { if (t->kind == T_TYPE && !isunion) errorat(t->pos, "parameter and field names use snake_case: start with a " "lowercase letter"); if (t->kind != T_NAME) unexpected(p, isunion ? S("a variant") : S("a name")); next(p); n->name = t->text; } if (isvar) n->flags |= F_VAR; if (isprivate) n->flags |= F_PRIVATE; addnode(&group, n); if (pk(p) == T_COMMA && peekat(p, 1)->kind == T_NAME && (peekat(p, 2)->kind == T_COMMA || peekat(p, 2)->kind == T_COLON || peekat(p, 2)->kind == T_EQ || peekat(p, 2)->kind == T_RPAREN || peekat(p, 2)->kind == T_NEWLINE)) { next(p); continue; } break; } Node *type = 0, *def = 0; if (accept(p, T_COLON)) { if (pk(p) == T_TYPE && equals(peek(p)->text, S("Raw"))) israw = 1; type = parse_type(p); } if (accept(p, T_EQ)) def = parse_expr(p); for (iz i = 0; i < group.len; i++) { group.data[i]->a = type; group.data[i]->b = def; if (type) group.data[i]->flags |= F_HASTYPE; if (israw) group.data[i]->flags |= F_RAW; addnode(out, group.data[i]); } } if (accept(p, T_COMMA)) continue; if (pk(p) == T_NEWLINE) continue; if (pk(p) != T_RPAREN) unexpected(p, S("',' or ')'")); } expect(p, T_RPAREN); } static Node *parse_type_(Parser *p); static Node * parse_type(Parser *p) { enter(p); Node *n = parse_type_(p); p->depth--; return n; } static Node * parse_type_(Parser *p) { Tok *t = peek(p); if (t->kind == T_FN) { next(p); Node *n = mknode(N_TFN, t->pos); expect(p, T_LPAREN); skip_list_nl(p, &n->list); while (pk(p) != T_RPAREN) { if (pk(p) == T_NAME && peekat(p, 1)->kind == T_COLON) { next(p); next(p); } addnode(&n->list, parse_type(p)); skip_list_nl(p, &n->list); if (!accept(p, T_COMMA)) break; skip_list_nl(p, &n->list); } expect(p, T_RPAREN); if (accept(p, T_ARROW)) n->a = parse_type(p); return n; } if (t->kind == T_STRUCT || t->kind == T_UNION) { next(p); Node *n = mknode(t->kind == T_STRUCT ? N_TSTRUCT : N_TUNION, t->pos); parse_params(p, &n->list, t->kind == T_UNION); return n; } if (t->kind == T_NAME && equals(t->text, S("no_nil")) && peekat(p, 1)->kind == T_LPAREN) { next(p); next(p); Node *u = parse_type(p); if (u->kind != N_TUNION) errorat(t->pos, "no_nil needs a union"); u->flags |= F_NONIL; expect(p, T_RPAREN); return u; } if (t->kind == T_NAME && peekat(p, 1)->kind == T_DOT && peekat(p, 2)->kind == T_TYPE) { // alias.Type next(p); next(p); Node *n = parse_type(p); if (n->kind == N_TNAME) { Node *q = mknode(N_NAME, t->pos); q->name = t->text; n->c = q; } return n; } if (t->kind == T_NAME && peekat(p, 1)->kind == T_LPAREN) { Node *call = parse_expr(p); Node *n = mknode(N_TMACRO, t->pos); n->a = call; return n; } if (t->kind == T_DOLLAR && p->template_depth) { Node *e = parse_expr(p); Node *n = mknode(N_TNAME, t->pos); n->name = S("$"); n->a = e; return n; } if (t->kind == T_NAME) { // var x: int Buf b = {0}; bput(&b, t->text); if (b.data[0] >= 'a' && b.data[0] <= 'z') b.data[0] -= 32; help_text = fmtstr("type names use CamelCase: did you mean %s?", bstr(&b)); } if (t->kind != T_TYPE) unexpected(p, S("a type")); next(p); Node *n = mknode(N_TNAME, t->pos); n->name = t->text; if (pk(p) == T_LPAREN) { next(p); skip_list_nl(p, &n->list); while (pk(p) != T_RPAREN) { if (pk(p) == T_INT || pk(p) == T_MINUS || (n->list.len == 1 && equals(n->name, S("List"))) || (pk(p) == T_NAME && equals(n->name, S("Coroutine")))) { // the length of List(T, n): a constant expression; the name // of the coroutine in Coroutine(name) addnode(&n->list, parse_expr(p)); } else { addnode(&n->list, parse_type(p)); } skip_list_nl(p, &n->list); if (!accept(p, T_COMMA)) break; skip_list_nl(p, &n->list); } expect(p, T_RPAREN); } // Shape.circle, a variant type, or Type.field projections while (pk(p) == T_DOT && peekat(p, 1)->kind == T_NAME && !peek(p)->space_before) { next(p); Tok *f = next(p); Node *q = mknode(N_TNAME, f->pos); q->name = f->text; q->a = n; q->flags |= F_CALLED; // marks a projection n = q; } return n; } // Arguments of a call: positional, then name: value pairs. static void parse_args(Parser *p, Nodes *out, TokKind close) { for (;;) { skip_list_nl(p, out); if (pk(p) == close) break; b32 spliced = pk(p) == T_DOLLAR && p->template_depth && peekat(p, 1)->kind == T_NAME && peekat(p, 2)->kind == T_COLON; // $name: value in a template if ((spliced || (pk(p) == T_NAME && peekat(p, 1)->kind == T_COLON)) && close == T_RPAREN) { Node *n = mknode(N_NAMED, peek(p)->pos); if (spliced) n->c = parse_primary(p); else n->name = next(p)->text; next(p); skip_list_nl(p, out); n->a = parse_expr(p); addnode(out, n); } else if (pk(p) == T_FN || pk(p) == T_STRUCT || pk(p) == T_UNION || pk(p) == T_FOR || pk(p) == T_WHILE || pk(p) == T_VAR || pk(p) == T_CONST || pk(p) == T_RETURN || pk(p) == T_LBRACE || pk(p) == T_LABEL) { // statements and declarations as arguments: only macros accept them if (pk(p) == T_FN && peekat(p, 1)->kind == T_LPAREN) { addnode(out, parse_expr(p)); } else if (pk(p) == T_LBRACE) { addnode(out, parse_stmt(p)); } else if (pk(p) == T_STRUCT || pk(p) == T_UNION) { if (peekat(p, 1)->kind == T_TYPE) addnode(out, parse_decl(p, 0)); else addnode(out, parse_expr(p)); } else if (pk(p) == T_FN) { addnode(out, parse_decl(p, 0)); } else { Node *st = parse_stmt(p); if (st->kind == N_VAR && st->list.len) { // var a, b: Int = 4 is one argument Node *g = mknode(N_DECLS, st->pos); g->flags |= F_GROUP; addnode(&g->list, st); st = g; } addnode(out, st); } } else { Node *e = parse_expr(p); skip_list_nl(p, out); if (close == T_RBRACKET && accept(p, T_COLON)) { Node *pair = mknode(N_PAIR, e->pos); pair->a = e; skip_list_nl(p, out); pair->b = parse_expr(p); e = pair; } addnode(out, e); } skip_list_nl(p, out); if (!accept(p, T_COMMA)) break; } skipnl(p); expect(p, close); } static b32 is_assign_op(TokKind k); static b32 is_parenless(Parser *p); static b32 is_coroutine(Parser *p); static Node * parse_fnlit(Parser *p, b32 ismacro) { Tok *t = expect(p, T_FN); Node *n = mknode(N_FNLIT, t->pos); if (ismacro) n->flags |= F_MACRO; parse_params(p, &n->list, 0); if (accept(p, T_ARROW)) n->a = parse_type(p); if (accept(p, T_DO)) { n->flags |= F_DO; Node *body = mknode(N_BLOCK, peek(p)->pos); body->flags |= F_DO; Node *e = mknode(N_EXPR, peek(p)->pos); e->a = parse_expr(p); if (is_assign_op(pk(p))) { // fn () do z = 2 Tok *op = next(p); e->kind = N_ASSIGN; e->op = op->kind; e->pos = op->pos; skipnl(p); e->b = parse_expr(p); } addnode(&body->list, e); n->b = body; } else { n->b = parse_block(p); } return n; } static Str raw_source(Pos p); // After fn in an expression: a function type (fn (Int) -> Int) rather than // a function literal, which has parameter names and a body. static b32 fn_type_ahead(Parser *p) { if (peekat(p, 1)->kind != T_LPAREN) return 0; TokKind first = peekat(p, 2)->kind; if (first == T_VAR) first = peekat(p, 3)->kind; if (first == T_NAME || first == T_DOLLAR) return 0; if (first != T_RPAREN) return 1; // fn (): a body after the result, if any, makes it a literal int depth = 0; for (iz k = 3;; k++) { TokKind tk = peekat(p, k)->kind; if (tk == T_EOF || tk == T_NEWLINE) return 1; if (tk == T_LPAREN || tk == T_LBRACKET) depth++; if (tk == T_RPAREN || tk == T_RBRACKET) { if (depth == 0) return 1; depth--; } if (depth == 0 && (tk == T_DO || tk == T_LBRACE)) return 0; if (depth == 0 && tk == T_COMMA) return 1; } } // n spans the source from start to the last token read static Node * widen(Parser *p, Node *n, Pos start) { Pos end = p->toks.data[p->i > 0 ? p->i - 1 : 0].pos; if (n && start.file == n->pos.file && end.hi > start.lo) { n->pos.lo = start.lo; n->pos.hi = end.hi; } return n; } static Node *parse_primary_(Parser *p); static Node * parse_primary(Parser *p) { Pos start = peek(p)->pos; return widen(p, parse_primary_(p), start); } static Node * parse_primary_(Parser *p) { Tok *t = peek(p); Node *n = 0; switch (t->kind) { case T_INT: next(p); n = mknode(N_INT, t->pos); n->ival = t->ival; n->name = t->text; // as written, for printing if (t->text.len && t->text.data[0] == '\'') n->flags |= F_CHAR; return n; case T_FLOAT: next(p); n = mknode(N_FLOAT, t->pos); n->fval = t->fval; n->name = t->text; return n; case T_STRING: next(p); n = mknode(N_STR, t->pos); n->sval = t->text; n->name = raw_source(t->pos); // strings print as written return n; case T_TRUE: case T_FALSE: next(p); n = mknode(N_BOOL, t->pos); n->ival = t->kind == T_TRUE; return n; case T_NIL: next(p); return mknode(N_NIL, t->pos); case T_NAME: if (equals(t->text, S("code")) && p->code_import && (peekat(p, 1)->kind == T_LPAREN || peekat(p, 1)->kind == T_LBRACE)) { next(p); n = mknode(N_TEMPLATE, t->pos); p->template_depth++; if (accept(p, T_LPAREN)) { skipnl(p); if (pk(p) == T_STRUCT || pk(p) == T_UNION || pk(p) == T_FN) { Node *ty = mknode(N_TYPEX, peek(p)->pos); ty->a = parse_type(p); n->a = ty; } else { n->a = parse_expr(p); } skipnl(p); expect(p, T_RPAREN); } else { Node *b = parse_block(p); n->list = b->list; n->flags |= F_DO; } p->template_depth--; return n; } if (equals(t->text, S("code")) && !p->code_import && peekat(p, 1)->kind == T_LBRACE) { help_text = S("add import \"std:code\" at the top of the file"); errorat(t->pos, "code { } is part of std:code"); } if (equals(t->text, S("no_nil")) && peekat(p, 1)->kind == T_LPAREN) { n = mknode(N_TYPEX, t->pos); n->a = parse_type(p); return n; } next(p); n = mknode(N_NAME, t->pos); n->name = t->text; return n; case T_UNDERSCORE: errorat(t->pos, "_ is only used to discard a value: _ = value"); case T_TYPE: next(p); n = mknode(N_TYPENAME, t->pos); n->name = t->text; return n; case T_STRUCT: case T_UNION: n = mknode(N_TYPEX, t->pos); n->a = parse_type(p); return n; case T_LPAREN: { next(p); skipnl(p); n = parse_expr(p); skipnl(p); expect(p, T_RPAREN); return n; } case T_LBRACKET: next(p); n = mknode(N_LIST, t->pos); if (pk(p) == T_COLON && peekat(p, 1)->kind == T_RBRACKET) { next(p); next(p); n->flags |= F_MAP; return n; } parse_args(p, &n->list, T_RBRACKET); return n; case T_LBRACE: n = mknode(N_BLOCKX, t->pos); n->a = parse_block(p); return n; case T_DOT: { next(p); if (pk(p) == T_LPAREN) { next(p); n = mknode(N_ANON, t->pos); parse_args(p, &n->list, T_RPAREN); return n; } if (pk(p) == T_DOLLAR) { n = mknode(N_VARIANT, t->pos); n->a = parse_primary(p); } else { Tok *v = peek(p); if (v->kind != T_NAME && v->kind != T_TYPE) unexpected(p, S("a variant name")); next(p); n = mknode(N_VARIANT, t->pos); n->name = v->text; } if (pk(p) == T_LPAREN && !peek(p)->space_before) { next(p); n->flags |= F_CALLED; parse_args(p, &n->list, T_RPAREN); } return n; } case T_IF: return parse_if(p, 1); case T_SWITCH: n = mknode(N_SWITCHX, t->pos); n->a = parse_switch(p, 1); return n; case T_FN: if (fn_type_ahead(p)) { // fn (List(Int)) -> Int without a body: a function type as a value n = mknode(N_TYPEX, t->pos); n->a = parse_type(p); return n; } return parse_fnlit(p, 0); case T_MACRO: if (peekat(p, 1)->kind == T_FN) { next(p); return parse_fnlit(p, 1); } break; case T_DOLLAR: if (!p->template_depth) errorat( t->pos, "$ splices a value into a template; it only works inside code(...) or code { ... }"); next(p); n = mknode(N_SPLICE, t->pos); if (accept(p, T_LPAREN)) { skipnl(p); p->template_depth--; n->a = parse_expr(p); p->template_depth++; skipnl(p); expect(p, T_RPAREN); } else { Tok *v = peek(p); if (v->kind != T_NAME) unexpected(p, S("a name after $")); next(p); Node *nm = mknode(N_NAME, v->pos); nm->name = v->text; n->a = nm; } return n; default: break; } unexpected(p, S("an expression")); } // After a name, on the same line and after a space: the start of an // argument of a call written without parentheses. static b32 starts_bare_args(Parser *p, Node *callee) { if (callee->kind != N_NAME && !(callee->kind == N_FIELD && callee->a->kind == N_NAME)) return 0; Tok *t = peek(p); if (!t->space_before || p->i == 0) return 0; Tok *prev = &p->toks.data[p->i - 1]; Str text = files.data[t->pos.file].text; for (i32 k = prev->pos.hi; k < t->pos.lo && k < (i32)text.len; k++) { if (text.data[k] == '\n') return 0; } switch (t->kind) { case T_NAME: case T_TYPE: case T_INT: case T_FLOAT: case T_STRING: case T_NIL: case T_TRUE: case T_FALSE: case T_LPAREN: case T_LBRACKET: case T_AMP: return 1; case T_NOT: return peekat(p, 1)->kind != T_IN; case T_FN: return peekat(p, 1)->kind == T_LPAREN; case T_DOT: { // an implicit variant or an anonymous struct: name .red, px .(x: 4) Tok *after = peekat(p, 1); return !after->space_before && (after->kind == T_NAME || after->kind == T_LPAREN || after->kind == T_DOLLAR); } default: return 0; } } static void parse_bare_args(Parser *p, Nodes *out) { do { if (pk(p) == T_NAME && peekat(p, 1)->kind == T_COLON) { Node *n = mknode(N_NAMED, peek(p)->pos); n->name = next(p)->text; next(p); n->a = parse_expr(p); addnode(out, n); } else { addnode(out, parse_expr(p)); } } while (accept(p, T_COMMA)); } static Node * parse_postfix(Parser *p) { Pos start = peek(p)->pos; Node *n = parse_primary(p); for (;;) { Tok *t = peek(p); if (t->kind == T_LPAREN && !t->space_before) { next(p); Node *c = mknode(N_CALL, t->pos); c->a = n; parse_args(p, &c->list, T_RPAREN); n = widen(p, c, start); } else if (t->kind == T_LBRACKET && !t->space_before) { next(p); Node *c = mknode(N_INDEX, t->pos); c->a = n; parse_args(p, &c->list, T_RBRACKET); if (!c->list.len) errorat(t->pos, "an index needs a value"); n = widen(p, c, start); } else if (t->kind == T_DOTSTAR) { next(p); Node *c = mknode(N_DEREF, t->pos); c->a = n; n = widen(p, c, start); } else if (t->kind == T_DOT && !t->space_before) { next(p); Tok *f = peek(p); Node *c = mknode(N_FIELD, f->pos); c->a = n; if (f->kind == T_DOLLAR && p->template_depth) { c->b = parse_primary(p); } else { if (f->kind != T_NAME && f->kind != T_TYPE) unexpected(p, S("a field name")); next(p); c->name = f->text; } n = c; } else if (starts_bare_args(p, n)) { // a b c d is a(b(c(d))): a parenless call takes the rest of the // expression, and commas belong to the innermost one Node *c = mknode(N_CALL, t->pos); c->a = n; c->flags |= F_COMMAND; parse_bare_args(p, &c->list); return widen(p, c, start); } else { return n; } } } static Node * parse_unary(Parser *p) { enter(p); Tok *t = peek(p); Node *n = 0; if (t->kind == T_MINUS || t->kind == T_PLUS) { next(p); n = mknode(N_UNARY, t->pos); n->op = t->kind; n->a = parse_unary(p); widen(p, n, t->pos); } else if (t->kind == T_AMP) { next(p); n = mknode(N_ADDR, t->pos); n->a = parse_unary(p); widen(p, n, t->pos); } else if (t->kind == T_DOTLT || t->kind == T_DOTEQ) { next(p); skipnl(p); n = mknode(N_RANGE, t->pos); n->op = t->kind; n->flags |= F_IMPLICIT; n->a = mknode(N_INT, t->pos); // the omitted start is zero n->b = parse_binary(p, 5); // the upper bound includes arithmetic widen(p, n, t->pos); } else { n = parse_postfix(p); if (pk(p) == T_STARSTAR) { // a ** b ** c is a ** (b ** c), and -a ** b is -(a ** b) Tok *op = next(p); skipnl(p); Node *e = mknode(N_BINARY, op->pos); e->op = T_STARSTAR; e->a = n; e->b = parse_unary(p); n = widen(p, e, n->pos); } } p->depth--; return n; } static int binprec(TokKind k) { switch (k) { case T_STAR: case T_SLASH: case T_PERCENT: return 6; case T_PLUS: case T_MINUS: return 5; case T_DOTLT: case T_DOTEQ: case T_LTDOTLT: case T_LTDOTEQ: return 4; case T_EQEQ: case T_NE: case T_LT: case T_LE: case T_GT: case T_GE: case T_IN: return 3; case T_AND: return 1; case T_OR: return 0; default: return -1; } } static Node * parse_not(Parser *p) { Tok *t = peek(p); if (t->kind == T_NOT) { next(p); Node *n = mknode(N_UNARY, t->pos); n->op = T_NOT; n->a = parse_binary(p, 2); return widen(p, n, t->pos); } return parse_unary(p); } static Node * parse_binary(Parser *p, int minprec) { Pos start = peek(p)->pos; Node *lhs = minprec <= 2 ? parse_not(p) : parse_unary(p); for (;;) { Tok *t = peek(p); TokKind k = t->kind; b32 notin = 0; if (k == T_NOT && peekat(p, 1)->kind == T_IN) { k = T_IN; notin = 1; } int prec = binprec(k); if (prec < minprec || prec < 0) return lhs; next(p); if (notin) next(p); skipnl(p); Node *rhs = parse_binary(p, prec + 1); Node *n = 0; if (k == T_IN) { n = mknode(N_IN, t->pos); if (notin) n->flags |= F_NOT; } else if (range_token(k)) { n = mknode(N_RANGE, t->pos); } else { n = mknode(N_BINARY, t->pos); } n->op = k; n->a = lhs; n->b = rhs; widen(p, n, start); if (prec == 3 && binprec(pk(p)) == 3 && pk(p) != T_IN) { errorat(peek(p)->pos, "comparisons do not chain; combine them with and"); } lhs = n; } } static Node * parse_expr(Parser *p) { enter(p); Node *n = parse_binary(p, 0); p->depth--; return n; } // Statements static void end_stmt(Parser *p) { TokKind k = pk(p); if (k == T_NEWLINE || k == T_SEMI) { next(p); return; } if (k == T_RBRACE || k == T_EOF) return; if (k == T_CASE || k == T_DEFAULT) return; unexpected(p, S("the end of the statement")); } // A body after if, for, while, else: a block, or do and one statement. static Node * parse_body(Parser *p) { if (accept(p, T_DO)) { Node *b = mknode(N_BLOCK, peek(p)->pos); b->flags |= F_DO; addnode(&b->list, parse_stmt(p)); return b; } return parse_block(p); } // The source of a string token starting at p, as written. static Str raw_source(Pos p) { Str s = files.data[p.file].text; iz i = p.offset; if (i + 2 < s.len && s.data[i] == '"' && s.data[i + 1] == '"' && s.data[i + 2] == '"') return substr(s, i, raw_close(s, i + 3, p) + 3); // a raw string if (i < s.len && s.data[i] == '"') { // a quoted string keeps its escapes as written iz j = i + 1; while (j < s.len && s.data[j] != '"' && s.data[j] != '\n') j += s.data[j] == '\\' ? 2 : 1; return substr(s, i, j < s.len ? j + 1 : s.len); } return (Str){0}; } static Node * parse_block(Parser *p) { Tok *t = expect(p, T_LBRACE); Node *b = mknode(N_BLOCK, t->pos); for (;;) { while (pk(p) == T_NEWLINE || pk(p) == T_SEMI) next(p); if (pk(p) == T_RBRACE) break; if (pk(p) == T_EOF) errorat(t->pos, "this block is never closed"); Node *s = parse_stmt(p); addnode(&b->list, s); end_stmt(p); } b->end = peek(p)->pos.offset; expect(p, T_RBRACE); return b; } // if as a statement or a value. As a value, each branch is an expression, // written after do, or a block whose last expression is its value. static Node * parse_if(Parser *p, b32 asvalue) { Tok *t = expect(p, T_IF); Node *n = mknode(asvalue ? N_IFX : N_IF, t->pos); n->a = parse_expr(p); if (asvalue) { if (accept(p, T_DO)) { n->b = parse_expr(p); } else { Node *bx = mknode(N_BLOCKX, peek(p)->pos); bx->a = parse_block(p); n->b = bx; } if (!accept(p, T_ELSE)) errorat(t->pos, "an if used as a value needs an else"); if (pk(p) == T_IF) { n->c = parse_if(p, 1); } else if (accept(p, T_DO)) { n->c = parse_expr(p); } else if (pk(p) == T_LBRACE) { Node *bx = mknode(N_BLOCKX, peek(p)->pos); bx->a = parse_block(p); n->c = bx; } else { n->c = parse_expr(p); } return n; } n->b = parse_body(p); // else may follow on the same line or the next one iz save = p->i; skipnl(p); if (accept(p, T_ELSE)) { if (pk(p) == T_IF) { Node *b = mknode(N_BLOCK, peek(p)->pos); addnode(&b->list, parse_if(p, 0)); n->c = b; } else if (pk(p) == T_DO || pk(p) == T_LBRACE) { n->c = parse_body(p); } else { // if c do a else b: one statement after else Node *b = mknode(N_BLOCK, peek(p)->pos); b->flags |= F_DO; addnode(&b->list, parse_stmt(p)); n->c = b; } } else { p->i = save; } return n; } static Node * parse_switch(Parser *p, b32 asvalue) { Tok *t = expect(p, T_SWITCH); Node *n = mknode(N_SWITCH, t->pos); if (asvalue) n->flags |= F_DO; b32 ref = accept(p, T_AMP); if (ref) n->flags |= F_REF; if (pk(p) == T_NAME && peekat(p, 1)->kind == T_IN) { n->name = next(p)->text; next(p); n->a = parse_expr(p); } else { n->a = parse_expr(p); if (n->a->kind == N_NAME) n->name = n->a->name; else if (ref) errorat(n->a->pos, "switch &name needs a plain name; write switch &v in value"); n->flags |= F_IMPLICIT; } skipnl(p); expect(p, T_LBRACE); for (;;) { while (pk(p) == T_NEWLINE || pk(p) == T_SEMI) next(p); if (pk(p) == T_RBRACE) break; Tok *c = peek(p); Node *cs = mknode(N_CASE, c->pos); if (accept(p, T_LABEL)) { // label c = case circle: names that case cs->name = expect(p, T_NAME)->text; expect(p, T_EQ); if (pk(p) != T_CASE && pk(p) != T_DEFAULT) errorat(peek(p)->pos, "a case label names a case"); } if (accept(p, T_DEFAULT)) { } else { expect(p, T_CASE); for (;;) { Node *pat = 0; if (pk(p) == T_DOT) { pat = parse_primary(p); } else if (pk(p) == T_NIL) { pat = mknode(N_NIL, next(p)->pos); } else if (is_type_start(pk(p)) || pk(p) == T_NAME) { // U8(97) is a value; List(Int) a type b32 conv = pk(p) == T_TYPE && peekat(p, 1)->kind == T_LPAREN && !is_type_start(peekat(p, 2)->kind); if (conv || (pk(p) == T_NAME && !(peekat(p, 1)->kind == T_DOT && peekat(p, 2)->kind == T_TYPE))) { pat = parse_expr(p); } else { pat = mknode(N_TYPEX, peek(p)->pos); pat->a = parse_type(p); } } else { pat = parse_expr(p); } addnode(&cs->list, pat); if (!accept(p, T_COMMA)) break; } } expect(p, T_COLON); Node *body = mknode(N_BLOCK, peek(p)->pos); for (;;) { while (pk(p) == T_NEWLINE || pk(p) == T_SEMI) next(p); if (pk(p) == T_CASE || pk(p) == T_DEFAULT || pk(p) == T_RBRACE || pk(p) == T_EOF) break; if (pk(p) == T_LABEL && peekat(p, 1)->kind == T_NAME && peekat(p, 2)->kind == T_EQ && (peekat(p, 3)->kind == T_CASE || peekat(p, 3)->kind == T_DEFAULT)) break; // label c = case ...: the next case addnode(&body->list, parse_stmt(p)); end_stmt(p); } cs->a = body; addnode(&n->list, cs); } expect(p, T_RBRACE); return n; } static Node * parse_for(Parser *p) { Tok *t = expect(p, T_FOR); Node *n = mknode(N_FOR, t->pos); if (pk(p) == T_NAME && peekat(p, 1)->kind == T_COLON) { // for x: T in xs: only the elements whose variant is T n->name = next(p)->text; next(p); n->c = parse_type(p); n->flags |= F_FILTER; expect(p, T_IN); } else if (pk(p) == T_NAME && (peekat(p, 1)->kind == T_IN || (peekat(p, 1)->kind == T_COMMA && peekat(p, 2)->kind == T_NAME && peekat(p, 3)->kind == T_IN))) { n->name = next(p)->text; if (accept(p, T_COMMA)) n->sval = next(p)->text; expect(p, T_IN); } else { n->name = S("it"); n->flags |= F_IMPLICIT; } n->a = parse_expr(p); n->b = parse_body(p); return n; } static Node * parse_var(Parser *p, u32 flags) { Tok *t = next(p); // var or const if (t->kind == T_CONST) flags |= F_CONST; Tok *name = peek(p); if (name->kind == T_DOLLAR && p->template_depth) { Node *n = mknode(N_VAR, name->pos); n->c = parse_primary(p); n->name = S("$"); n->flags = flags; if (accept(p, T_COLON)) n->a = parse_type(p); if (accept(p, T_EQ)) n->b = parse_expr(p); return n; } if (name->kind == T_TYPE) errorat(name->pos, "variable names use snake_case: start with a lowercase letter"); if (name->kind != T_NAME) unexpected(p, S("a variable name")); next(p); Node *n = mknode(N_VAR, name->pos); n->name = name->text; n->flags = flags; // var a, b: T declares both names with the type, and the value if // there is one: like a, b: Int in a parameter list while (accept(p, T_COMMA)) { Tok *more = peek(p); if (more->kind != T_NAME) unexpected(p, S("a variable name")); next(p); Node *m = mknode(N_VAR, more->pos); m->name = more->text; m->flags = flags | F_GROUP; addnode(&n->list, m); } if (accept(p, T_COLON)) n->a = parse_type(p); if (accept(p, T_EQ)) { skipnl(p); iz at = p->i; n->b = parse_expr(p); iz end = p->i; for (iz k = 0; k < n->list.len; k++) { // each name gets its own copy of the value, parsed again p->i = at; n->list.data[k]->b = parse_expr(p); } p->i = end; } for (iz k = 0; k < n->list.len; k++) n->list.data[k]->a = n->a; if (!n->a && !n->b) errorat(name->pos, "a variable needs a type or a value; write var %s: Type, or var %s = value", n->name, n->name); if ((flags & F_CONST) && !n->b) errorat(name->pos, "a constant needs a value"); return n; } static b32 is_assign_op(TokKind k) { return k == T_EQ || k == T_PLUSEQ || k == T_MINUSEQ || k == T_STAREQ || k == T_SLASHEQ || k == T_PERCENTEQ; } static Node * parse_import(Parser *p, u32 flags) { Tok *t = expect(p, T_IMPORT); Node *n = mknode(N_IMPORT, t->pos); n->flags = flags; Tok *path = expect(p, T_STRING); n->sval = path->text; if (pk(p) == T_LPAREN) { next(p); parse_args(p, &n->list, T_RPAREN); } if (accept(p, T_AS)) { Tok *a = expect(p, T_NAME); n->name = a->text; } if (equals(n->sval, S("std:code"))) p->code_import = 1; return n; } static Node *parse_stmt_(Parser *p); static STATE b32 printing_expanded = 0; // --expand: compile-time parts print as their contents static Node * parse_stmt(Parser *p) { enter(p); Node *n = parse_stmt_(p); p->depth--; return n; } static Node *parse_group(Parser *p, u32 flags, b32 toplevel); // = { ... } after macro label x: statements in a body, declarations at // file scope static void mlabel_rest(Parser *p, Node *n, b32 toplevel) { if (pk(p) != T_EQ) { help_text = S("write macro label x = { ... }; place(x, ...) puts code into its braces"); errorat(peek(p)->pos, "a macro label names a block, given with ="); } next(p); if (pk(p) != T_LBRACE) errorat(peek(p)->pos, "a macro label names a block: = { ... }"); n->a = toplevel ? parse_group(p, 0, 1) : parse_block(p); } static Node * parse_stmt_(Parser *p) { Tok *t = peek(p); switch (t->kind) { case T_VAR: case T_CONST: return parse_var(p, 0); case T_IF: return parse_if(p, 0); case T_FOR: return parse_for(p); case T_WHILE: { next(p); Node *n = mknode(N_WHILE, t->pos); n->a = parse_expr(p); n->b = parse_body(p); return n; } case T_LBRACE: return parse_block(p); case T_SWITCH: return parse_switch(p, 0); case T_STRUCT: case T_UNION: // a declaration: in a template, or reported as misplaced if (peekat(p, 1)->kind == T_TYPE || peekat(p, 1)->kind == T_NAME || (peekat(p, 1)->kind == T_DOLLAR && p->template_depth)) return parse_decl(p, 0); // struct foo says how to name it break; case T_RETURN: { next(p); Node *n = mknode(N_RETURN, t->pos); if (pk(p) != T_NEWLINE && pk(p) != T_SEMI && pk(p) != T_RBRACE && pk(p) != T_EOF && pk(p) != T_CASE && pk(p) != T_DEFAULT && pk(p) != T_ELSE) n->a = parse_expr(p); return n; } case T_BREAK: case T_CONTINUE: { next(p); Node *n = mknode(t->kind == T_BREAK ? N_BREAK : N_CONTINUE, t->pos); if (pk(p) == T_NAME) n->name = next(p)->text; if (pk(p) == T_DOLLAR && p->template_depth) n->c = parse_primary(p); // break $label if (pk(p) == T_INT) { help_text = S("label the loop and name it: label outer = for ... { break outer }"); errorat(peek(p)->pos, "%s takes the name of a loop, not a number", tokname[t->kind]); } return n; } case T_DEFER: { next(p); Node *n = mknode(N_DEFER, t->pos); n->a = parse_stmt(p); return n; } case T_LABEL: { // label x = for ..., while ..., { ... } or switch ...: it names those braces next(p); Node *n = mknode(N_LABELED, t->pos); if (pk(p) == T_DOLLAR && p->template_depth) n->c = parse_primary(p); // label $name in a template else n->name = expect(p, T_NAME)->text; if (pk(p) != T_EQ) { help_text = S("write label x = for ..., label x = while ..., label x = { ... } or " "label x = switch ..."); errorat(peek(p)->pos, "a label is given with ="); } next(p); if (pk(p) != T_FOR && pk(p) != T_WHILE && pk(p) != T_LBRACE && pk(p) != T_SWITCH) errorat(peek(p)->pos, "a label names a loop, a block, a switch or a case"); n->a = parse_stmt(p); return n; } case T_UNDERSCORE: { next(p); expect(p, T_EQ); Node *n = mknode(N_DISCARD, t->pos); n->a = parse_expr(p); return n; } case T_FN: if (peekat(p, 1)->kind == T_NAME || peekat(p, 1)->kind == T_TYPE || (peekat(p, 1)->kind != T_LPAREN && peekat(p, 2)->kind == T_LPAREN) || // fn ==( (peekat(p, 1)->kind == T_DOLLAR && p->template_depth)) return parse_decl(p, 0); // fn $name in a template; fn Foo says how to name it break; case T_IMPORT: return parse_import(p, 0); case T_NAME: if (is_parenless(p)) return parse_decl(p, 0); if (is_coroutine(p)) { if (p->template_depth) errorat(t->pos, "a code template cannot make a coroutine yet"); errorat(t->pos, "a coroutine is declared at file scope, not inside a function"); } break; case T_MACRO: { TokKind k = peekat(p, 1)->kind; if (k == T_IF || k == T_FOR || k == T_SWITCH) { next(p); p->template_depth++; // the bodies are templates: $ splices work there Node *n = parse_stmt(p); p->template_depth--; n->flags |= F_MACRO; return n; } if (k == T_LABEL) { // macro label x = { ... }: a compile-time block, not a scope next(p); next(p); Node *n = mknode(N_MLABEL, t->pos); n->name = expect(p, T_NAME)->text; mlabel_rest(p, n, 0); return n; } if (k == T_FN && peekat(p, 2)->kind == T_LPAREN) break; return parse_decl(p, 0); } default: break; } Node *e = parse_expr(p); if (is_assign_op(pk(p))) { Tok *op = next(p); Node *n = mknode(N_ASSIGN, op->pos); n->op = op->kind; n->a = e; skipnl(p); n->b = parse_expr(p); return n; } Node *n = mknode(N_EXPR, e->pos); n->a = e; return n; } // Declarations static Node * parse_fn(Parser *p, u32 flags) { expect(p, T_FN); Tok *name = peek(p); if (name->kind == T_TYPE) errorat(name->pos, "function names use snake_case"); Node *n = mknode(N_FN, name->pos); n->flags = flags; if (name->kind == T_DOLLAR && p->template_depth) { n->c = parse_primary(p); n->name = S("$"); } else if (name->kind == T_NAME) { next(p); n->name = name->text; } else if (name->kind == T_PLUS || name->kind == T_MINUS || name->kind == T_STAR || name->kind == T_SLASH || name->kind == T_PERCENT || name->kind == T_EQEQ || name->kind == T_LT || name->kind == T_IN || name->kind == T_STARSTAR) { next(p); n->name = name->text; n->op = name->kind; } else { unexpected(p, S("a function name")); } parse_params(p, &n->list, 0); for (iz i = 0; n->op && i < n->list.len; i++) { if (n->list.data[i]->flags & F_VAR) errorat(n->list.data[i]->pos, "an operator takes its operands as values, so none can be var"); } if (accept(p, T_ARROW)) n->a = parse_type(p); if (accept(p, T_DO)) { n->flags |= F_DO; Node *body = mknode(N_BLOCK, peek(p)->pos); body->flags |= F_DO; addnode(&body->list, parse_stmt(p)); n->b = body; } else if (pk(p) == T_LBRACE) { n->b = parse_block(p); } else { unexpected(p, S("a body: { ... } or do")); } return n; } static Node * parse_typedecl(Parser *p, u32 flags) { b32 nonil = 0; Tok *start = peek(p); if (start->kind == T_NAME && equals(start->text, S("no_nil"))) { next(p); expect(p, T_LPAREN); nonil = 1; } Tok *kw = next(p); if (kw->kind != T_STRUCT && kw->kind != T_UNION) errorat(kw->pos, "expected struct or union"); if (nonil && kw->kind != T_UNION) errorat(kw->pos, "no_nil only applies to a union"); Tok *name = peek(p); if (name->kind == T_NAME) errorat(name->pos, "type names use CamelCase: start with a capital letter"); Node *n = mknode(kw->kind == T_STRUCT ? N_STRUCT : N_UNION, name->pos); if (name->kind == T_DOLLAR && p->template_depth) { n->c = parse_primary(p); // struct $name in a template n->name = S("$"); } else { n->name = expect(p, T_TYPE)->text; } n->flags = flags | (nonil ? F_NONIL : 0); parse_params(p, &n->list, kw->kind == T_UNION); if (nonil) expect(p, T_RPAREN); return n; } static Node * parse_group(Parser *p, u32 flags, b32 toplevel) { Tok *t = expect(p, T_LBRACE); Node *g = mknode(N_DECLS, t->pos); g->flags = flags; for (;;) { while (pk(p) == T_NEWLINE || pk(p) == T_SEMI) next(p); if (pk(p) == T_RBRACE) break; if (pk(p) == T_EOF) errorat(t->pos, "this block is never closed"); addnode(&g->list, parse_decl(p, toplevel)); end_stmt(p); } g->end = peek(p)->pos.offset; expect(p, T_RBRACE); return g; } // parenless(fn name(...) ...) declares a function that may be called without // parentheses: name alone calls it, and name a, b is name(a, b). static b32 is_parenless(Parser *p) { return pk(p) == T_NAME && equals(peek(p)->text, S("parenless")) && peekat(p, 1)->kind == T_LPAREN && !peekat(p, 1)->space_before; } static Node * parse_parenless(Parser *p, u32 flags) { Tok *t = next(p); next(p); skipnl(p); if (pk(p) == T_MACRO) { next(p); flags |= F_MACRO; } if (pk(p) != T_FN) { help_text = S("write parenless(fn name(...) { ... })"); errorat(t->pos, "parenless takes a function declaration"); } Node *n = parse_fn(p, flags | F_COMMAND); skipnl(p); expect(p, T_RPAREN); return n; } // coroutine(fn name(...) -> R { ... }, Y) declares a coroutine that yields // Y values (docs/coroutines.md). coroutine( followed by anything but fn is // an ordinary call. static b32 is_coroutine(Parser *p) { if (pk(p) != T_NAME || !equals(peek(p)->text, S("coroutine")) || peekat(p, 1)->kind != T_LPAREN || peekat(p, 1)->space_before) return 0; iz k = 2; while (peekat(p, k)->kind == T_NEWLINE) k++; return peekat(p, k)->kind == T_FN || peekat(p, k)->kind == T_MACRO; } static Node * parse_coroutine(Parser *p, u32 flags) { Tok *t = next(p); next(p); skipnl(p); if (pk(p) == T_MACRO) errorat(peek(p)->pos, "a coroutine runs at run time; it cannot be a macro"); if (peekat(p, 1)->kind != T_NAME) { help_text = S("write coroutine(fn name(...) -> R { ... }, Y)"); errorat(t->pos, "a coroutine is a named function: coroutine(fn name(...) ..., Y)"); } Node *n = parse_fn(p, flags | F_COROUTINE); skipnl(p); if (!accept(p, T_COMMA)) { help_text = S("write coroutine(fn name(...) -> R { ... }, Y)"); errorat(peek(p)->pos, "coroutine needs the type it yields, after the function"); } skipnl(p); n->d = parse_type(p); skipnl(p); expect(p, T_RPAREN); return n; } static Node * parse_decl(Parser *p, b32 toplevel) { Tok *t = peek(p); u32 flags = 0; if (t->kind == T_PRIVATE) { next(p); if (pk(p) == T_LBRACE) return parse_group(p, F_PRIVATE, toplevel); flags |= F_PRIVATE; t = peek(p); } if (t->kind == T_MACRO) { next(p); if (pk(p) == T_LBRACE) return parse_group(p, flags | F_MACRO, toplevel); flags |= F_MACRO; t = peek(p); if (t->kind == T_IF || t->kind == T_FOR || t->kind == T_SWITCH) { p->template_depth++; Node *n = parse_stmt(p); p->template_depth--; n->flags |= F_MACRO; return n; } if (t->kind == T_LABEL) { next(p); Node *n = mknode(N_MLABEL, t->pos); n->name = expect(p, T_NAME)->text; mlabel_rest(p, n, 1); return n; } } if (is_parenless(p)) return parse_parenless(p, flags); if (is_coroutine(p)) { if (!toplevel) errorat(t->pos, "a coroutine is declared at file scope"); if (flags & F_MACRO) errorat(t->pos, "a coroutine runs at run time; it cannot be a macro"); return parse_coroutine(p, flags); } switch (t->kind) { case T_FN: return parse_fn(p, flags); case T_STRUCT: case T_UNION: return parse_typedecl(p, flags); case T_VAR: case T_CONST: return parse_var(p, flags); case T_IMPORT: return parse_import(p, flags); case T_NAME: if (equals(t->text, S("no_nil"))) return parse_typedecl(p, flags); break; case T_MODULE: { next(p); Node *n = mknode(N_MODULE, t->pos); parse_params(p, &n->list, 0); return n; } case T_COLLECTION: { next(p); Node *n = mknode(N_COLLECTION, t->pos); n->name = expect(p, T_NAME)->text; expect(p, T_EQ); n->sval = expect(p, T_STRING)->text; return n; } default: break; } if (!toplevel || flags) unexpected(p, S("a declaration")); if (t->kind == T_RBRACE) { help_text = S("remove it, or look above for a { that closed early"); errorat(t->pos, "this } closes nothing"); } // a macro call at file scope Node *e = parse_expr(p); if (e->kind != N_CALL) errorat(e->pos, "only declarations and macro calls may appear at file scope"); Node *n = mknode(N_EXPR, e->pos); n->a = e; return n; } static Nodes parse_file(i32 file) { Parser p = {0}; p.toks = lex(file); p.file = file; Nodes decls = {0}; for (;;) { while (pk(&p) == T_NEWLINE || pk(&p) == T_SEMI) next(&p); if (pk(&p) == T_EOF) break; addnode(&decls, parse_decl(&p, 1)); end_stmt(&p); } files.data[file].code_import = p.code_import; return decls; } // Types typedef enum { TY_VOID, TY_BOOL, TY_INT, TY_FLOAT, TY_STRING, // never a kind of a Type: a switch on t->text ? TY_STRING : t->kind TY_STRUCT, TY_UNION, TY_LIST, // List(T) TY_ARRAY, // List(T, n) TY_MAP, TY_POOL, TY_REF, TY_ANY, TY_FN, TY_NIL, // the type of a bare nil before it meets a target TY_RANGE, TY_TYPE, // a type used as a value TY_FILE, // an output stream: stdout or stderr TY_DRAW, // the choices a fuzz input makes, for arbitrary() and draw() TY_ERROR, } TypeKind; typedef struct { Str name; // empty for a bare variant Type *type; // 0 for a payload-free variant Node *def; // default value, or 0 b32 isprivate; b32 isvar; // var parameter b32 israw; // Raw(T) macro parameter Type *gives; // Expr(T) macro parameter: the T Pos pos; } Field; typedef struct { Field *data; iz len; iz cap; } Fields; struct Type { TypeKind kind; Str name; // named struct and union types i32 module; // module of a named type i32 id; b32 named; b32 nonil; b32 ismacro; // a macro struct or union: exists only at compile time b32 isprivate; b32 resolving; b32 resolved; b32 boxed; // union payloads live in separate storage i32 bits; // integers: 8, 16, 32, 64 b32 issigned; Type *elem; // lists, arrays, pools, refs; map values; fn result Type *key; // maps iz count; // arrays Fields fields; // struct fields, union variants, fn parameters Node *decl; Str cname; // the C name u32 helpers; // which C helper functions were emitted b32 managed; // owns heap storage that must be copied and released b32 has_defaults; // some field, at any depth, has a non-zero default b32 has_refs; // holds references that a copy must move over to itself b32 text; // String: a List(U8) that prints as text i32 state; // codegen: 0 none, 1 declared, 2 defined Coro *co; // Coroutine(NAME): its fields come from the body of NAME }; static STATE Type *t_void = 0, *t_bool = 0, *t_int = 0, *t_float = 0, *t_string = 0, *t_any = 0, *t_nil = 0, *t_range = 0, *t_error = 0, *t_type = 0, *t_u8 = 0; static STATE Type *t_file = 0, *t_draw = 0; static STATE Type *t_ints[8] = {0}; // I8 I16 I32 I64 U8 U16 U32 U64 static STATE i32 type_count = 0; static STATE Map *interned = 0; static STATE struct { Type **data; iz len; iz cap; } all_types = {0}; static Type * mktype(TypeKind k) { Type *t = new(&perm, 1, Type); t->kind = k; t->id = ++type_count; t->module = -1; *push(&perm, &all_types) = t; return t; } static Str type_name(Type *t); static void key_type(Buf *b, Type *t) { bputc(b, '#'); bputi(b, t ? t->id : 0); } // Anonymous and built-in compound types are interned: the same shape is the // same Type, wherever it is written. static Type * intern(Type *t) { Buf b = {0}; bputi(&b, t->kind); switch (t->kind) { case TY_LIST: case TY_POOL: case TY_REF: key_type(&b, t->elem); break; case TY_ARRAY: key_type(&b, t->elem); bputc(&b, ','); bputi(&b, t->count); break; case TY_MAP: key_type(&b, t->key); key_type(&b, t->elem); break; case TY_FN: key_type(&b, t->elem); // fallthrough case TY_STRUCT: case TY_UNION: if (t->nonil) bputc(&b, '!'); for (iz i = 0; i < t->fields.len; i++) { Field *f = t->fields.data + i; bputc(&b, '('); bput(&b, f->name); if (f->isvar) bputc(&b, '&'); if (f->israw) bputc(&b, '`'); key_type(&b, f->type); if (f->gives) { bputc(&b, '@'); key_type(&b, f->gives); } if (f->def) { bputc(&b, '='); bputi(&b, (i64)(uptr)f->def); } } break; default: assert(0); } Str k = copystr(&perm, bstr(&b)); void **slot = upsert(&interned, k, 1); if (*slot) { type_count--; return *slot; } *slot = t; return t; } static Type * list_of(Type *elem) { Type *t = mktype(TY_LIST); t->elem = elem; t->managed = 1; return intern(t); } static Type * array_of(Type *elem, iz n) { Type *t = mktype(TY_ARRAY); t->elem = elem; t->count = n; return intern(t); } static Type * ref_of(Type *elem) { Type *t = mktype(TY_REF); t->elem = elem; return intern(t); } static Type * pool_of(Type *elem) { Type *t = mktype(TY_POOL); t->elem = elem; t->managed = 1; return intern(t); } static Type * map_of(Type *k, Type *v) { Type *t = mktype(TY_MAP); t->key = k; t->elem = v; t->managed = 1; return intern(t); } static b32 isnumeric(Type *t) { return t->kind == TY_INT || t->kind == TY_FLOAT; } static void put_type_name(Buf *b, Type *t) { if (!t) { bput(b, S("?")); return; } if (t->named) { bput(b, t->name); return; } switch (t->kind) { case TY_VOID: bput(b, S("nothing")); break; case TY_BOOL: bput(b, S("Bool")); break; case TY_INT: if (t == t_int) bput(b, S("Int")); else bputf(b, "%c%d", t->issigned ? 'I' : 'U', t->bits); break; case TY_FLOAT: bput(b, S("Float")); break; case TY_STRING: bput(b, S("String")); break; case TY_ANY: bput(b, S("Any")); break; case TY_NIL: bput(b, S("nil")); break; case TY_RANGE: bput(b, S("range")); break; case TY_TYPE: bput(b, S("Type")); break; case TY_FILE: bput(b, S("File")); break; case TY_DRAW: bput(b, S("Draw")); break; case TY_ERROR: bput(b, S("?")); break; case TY_LIST: case TY_ARRAY: case TY_POOL: case TY_REF: case TY_MAP: if (t->text) { bput(b, S("String")); break; } bput(b, t->kind == TY_POOL ? S("Pool(") : t->kind == TY_REF ? S("Ref(") : t->kind == TY_MAP ? S("Map(") : S("List(")); if (t->key) put_type_name(b, t->key), bput(b, S(", ")); put_type_name(b, t->elem); if (t->kind == TY_ARRAY) bputf(b, ", %lld", (long long)t->count); bputc(b, ')'); break; case TY_FN: bput(b, S("fn (")); for (iz i = 0; i < t->fields.len; i++) { if (i) bput(b, S(", ")); if (t->fields.data[i].isvar) bput(b, S("var ")); put_type_name(b, t->fields.data[i].type); } bputc(b, ')'); if (t->elem && t->elem != t_void) { bput(b, S(" -> ")); put_type_name(b, t->elem); } break; case TY_STRUCT: case TY_UNION: if (t->nonil) bput(b, S("no_nil(")); bput(b, t->kind == TY_STRUCT ? S("struct (") : S("union (")); for (iz i = 0; i < t->fields.len; i++) { Field *f = t->fields.data + i; if (i) bput(b, S(", ")); if (f->name.len) { bput(b, f->name); if (f->type) bput(b, S(": ")); } if (f->type) put_type_name(b, f->type); } bputc(b, ')'); if (t->nonil) bputc(b, ')'); break; } } static Str type_name(Type *t) { Buf b = {0}; put_type_name(&b, t); return bstr(&b); } static Str type_name_vs(Type *t, Type *other); static Type * mkint(i32 bits, b32 issigned) { Type *t = mktype(TY_INT); t->bits = bits; t->issigned = issigned; return t; } static void init_types(void) { t_void = mktype(TY_VOID); t_bool = mktype(TY_BOOL); t_int = mkint(64, 1); t_float = mktype(TY_FLOAT); t_any = mktype(TY_ANY); t_any->managed = 1; t_nil = mktype(TY_NIL); t_range = mktype(TY_RANGE); t_error = mktype(TY_ERROR); t_type = mktype(TY_TYPE); t_file = mktype(TY_FILE); t_draw = mktype(TY_DRAW); int bits[] = {8, 16, 32, 64}; for (int i = 0; i < 4; i++) { t_ints[i] = mkint(bits[i], 1); t_ints[4 + i] = mkint(bits[i], 0); } t_ints[3] = t_int; t_u8 = t_ints[4]; // a String is a List(U8) as well; only printing and in differ t_string = mktype(TY_LIST); t_string->elem = t_u8; t_string->text = 1; t_string->managed = 1; } static b32 isunion(Type *t) { return t->kind == TY_UNION; } // The variant of a union that a type converts into, through nested unions // along exactly one path. Returns the number of paths found. static int union_paths(Type *u, Type *t, int depth) { if (depth > 16 || !isunion(u)) return 0; int n = 0; for (iz i = 0; i < u->fields.len; i++) { Field *f = u->fields.data + i; if (!f->type) continue; if (f->type == t) n++; else if (isunion(f->type)) n += union_paths(f->type, t, depth + 1); } return n; } static iz variant_index(Type *u, Str name) { for (iz i = 0; i < u->fields.len; i++) { Field *f = u->fields.data + i; if (equals(f->name, name)) return i; if (!f->name.len && f->type && f->type->named && equals(f->type->name, name)) return i; if (!f->name.len && f->type && equals(type_name(f->type), name)) return i; } return -1; } static Str variant_name(Type *u, iz i) { Field *f = u->fields.data + i; if (f->name.len) return f->name; return type_name(f->type); } // Symbols and scopes typedef enum { S_VAR, S_FN, S_TYPE, S_MODULE, S_LABEL, S_MLABEL } SymKind; typedef struct Fn Fn; typedef struct Scope Scope; typedef struct Module Module; typedef struct { Fn **data; iz len; iz cap; } Fns; typedef struct { Sym **data; iz len; iz cap; } Syms; struct Sym { SymKind kind; Str name; Pos pos; Type *type; Node *decl; i32 module; b32 isglobal; b32 isparam; b32 isvar; // a var parameter: access to the caller's variable b32 ismacro; // exists only at compile time b32 isprivate; b32 isconst; b32 pinned; // a stored reference reaches it: it lives in a heap cell b32 refpinned; // a stored Ref reaches it: its cell lives for the rest of the program Pos pin_site; // where the first stored reference to it was taken b32 readonly; // parameters, loop and switch bindings char bound_by; // s: a switch case binding, f: a for loop element b32 boxroute; // a switch & binding to a variant payload: a checked route of its own b32 used; b32 iscaptured; b32 moved_out; // codegen: being returned, so not released b32 borrowed; // codegen: a const that shares the storage of a place i64 rlo, rhi; // codegen: the values an Int local can hold, when ranged b32 ranged; b32 resolving; // a global whose value is being checked or computed Str cname; Fns fns; // S_FN: the overload set Fn *owner; // the function a local belongs to Module *mod; // S_MODULE Node *target; // S_LABEL: the loop or block Scope *scope; // S_MLABEL: the scope code is placed into // a local a coroutine keeps between resumes, and its arguments: their // place in its struct (.f_s1.f_level), the field of the scope (s1) and // the struct of that scope Coro *co; Str co_path, co_scope; Type *co_holder; }; typedef enum { B_NONE, B_PRINT, B_PRINTLN, B_STR, B_LEN, B_ADD, B_INSERT, B_REMOVE, B_EXIT, B_ARGUMENTS, B_READ_FILE, B_WRITE_FILE, B_FILE_EXISTS, B_RUN, B_GETENV, B_LITERAL, B_PLACE, B_ERROR, B_WARNING, B_HERE, B_TYPE_OF, B_ARGS_OF, B_RET_OF, B_OVERLOAD_OF, B_ABS, B_SQRT, B_FLOOR, B_FAIL, B_RESIZE, B_EXPANDED, B_ASK, B_KNOWN, B_ENCODE, B_DECODE, B_WITH_MAIN, B_DRAW, B_ASSUME, B_FORMAT, B_SHELL, B_YIELD, } Builtin; struct Fn { Str name; b32 ctused; // called from compile-time code Nodes gen_checks; // build: the run-time checks its code has (N_NIL: pos, ival kind, flags loop) Nodes gen_allocs; // build: the statements whose code may allocate b32 gen_hooks; // build: its code may run print and == overloads through the runtime b32 ct_walked; // its calls are marked ctused too b32 special_done; // checked for being a print, arbitrary or valid overload Node *decl; // N_FN or N_FNLIT Type *type; // TY_FN Str cname; i32 module; Builtin builtin; b32 ismacro; b32 isprivate; b32 checked; b32 checking; b32 isclosure; // a function literal with an environment b32 isnested; b32 infer; // the result type comes from a do body b32 escapes; // a closure value that may outlive its scope b32 var_ptr; // codegen: var parameters are plain pointers, not routes i32 area; // codegen: 1 stores nothing outside, 2 may (see areas_known) i32 writes; // codegen: 1 changes nothing outside itself, 2 may (writes_known) i32 alloc; // codegen: 1 may allocate, 2 never does, 3 being checked b32 used; b32 emitted; b32 queued; // codegen: referenced, waiting to be generated Fn *parent; Scope *scope; // where it is declared Syms params; Syms captures; Syms locals; i32 op; // operator overloads b32 isparenless; // parenless(fn ...): called by its name alone, or without parentheses Coro *co; // a coroutine, and the resume made for it: their coroutine b32 yields; // a function nested in a coroutine that yields: inlined at each call Node *c_code; // a function written in C: the c(...) of its body (C plugins) b32 c_seen; // whether it is one is known }; // A for loop with a yield inside keeps what a loop keeps in C locals in the // struct too: the end of a range, the position, the length it took, and a // value it loops over (not a variable). Their paths in the struct, if any. enum { CL_END, CL_INDEX, CL_COUNT, CL_OVER }; typedef struct { Node *loop; i32 scope; // the scope of its body and bindings Type *types[4]; // what it keeps, by CL_ Str paths[4]; // and where: .f_s3.f_end___ } CoLoop; // A coroutine (docs/coroutines.md): coroutine(fn NAME(...) -> R { ... }, Y). // NAME is checked as a function; resume(c: var Coroutine(NAME)) runs its // body as a state machine on the struct, which keeps the arguments, the // locals that live across a yield, and where it stopped. struct Coro { Fn *fn; // NAME, never called directly Fn *resume; // resume(var c: Coroutine(NAME)), made for it Type *type; // Coroutine(NAME): its fields once the body is checked Type *yields; // Y Type *result; // what resume gives: no_nil(union (yielded: Y, returned: R)) Sym *param; // c b32 analyzed; // the body is checked and its state is known i32 points; // yield points, each call of a nested function that yields with its own Nodes yloops; // the loops (for, while) with a yield inside Nodes yswitches; // the switches with a yield inside a case struct { CoLoop *data; iz len; iz cap; } fors; // the for loops among them }; enum { SC_MODULE, SC_FILE, SC_FN, SC_BLOCK, SC_LOOP }; struct Scope { Scope *parent; Map *names; i32 kind; Fn *fn; Node *loop; // SC_LOOP and labeled blocks i32 file; Syms order; // locals in declaration order, for release at scope exit Node *block; b32 isvalue; // a block whose value is used: break cannot leave it // 1: holds the names of the braces below it (a label, loop or case // bindings, a case label); 2: the braces of a switch, which hold cases i32 holds; }; struct Module { Str dir; Str name; Str key; // directory plus module arguments i32 id; Scope *scope; // every top-level declaration of the module Nodes decls; Nodes header; // its imports, collections and module parameters Node *params; // module (...) Nodes args; // arguments it was imported with b32 ismain; b32 isstd; b32 loaded; b32 checked; struct { i32 *data; iz len; iz cap; } fileids; struct { Module **data; iz len; iz cap; } deps; // the modules it imports: checked before it }; // C plugins: see that section static b32 c_header(Module *m, Node *d); static b32 c_function(Fn *f); static void check_c_function(Fn *f); static void gen_c_function(Fn *f); static Str c_section(void); static b32 c_text_at(i32 file, Toks t, iz k); static void c_note_own(Module *m, Nodes *perfile, iz npaths); static void c_effect_kind(Module *m); #define C_PLUGIN_EFFECT 8 // EffectKind.plugin, after the 7 kinds of std:code static b32 is_c_call(Node *call); NORETURN static void c_misplaced(Node *call); static b32 is_compile_hook(Fn *m); static Fn *c_hook_text_fn(Fn *m); static Str c_build_text(Str text); static void run_compile_hooks(Str arg, b32 with_build); typedef struct { Strs names, macros; } CHeaderWords; static CHeaderWords c_header_words(void); static Str c_read_strs(void); static STATE b32 c_build_fields = 0; // what the compile hooks gave: encoded lists of strings static STATE Str c_flags_used = {0}; static STATE Str c_link_used = {0}; static STATE Str c_summary = {0}; // "did you mean": the known name closest to a misspelled one, if any is // close enough. near_start(name), near_offer(...) for each candidate, then // near_help() sets the help line. static STATE Str near_want = {0}, near_best = {0}; static STATE iz near_dist = 0; // edits (insert, delete, replace, swap two neighbours) that turn a into b static iz edit_distance(Str a, Str b) { if (a.len > 63 || b.len > 63) return 99; iz d[64][64] = {0}; for (iz i = 0; i <= a.len; i++) d[i][0] = i; for (iz j = 0; j <= b.len; j++) d[0][j] = j; for (iz i = 1; i <= a.len; i++) { for (iz j = 1; j <= b.len; j++) { iz best = d[i - 1][j - 1] + (a.data[i - 1] != b.data[j - 1]); if (d[i - 1][j] + 1 < best) best = d[i - 1][j] + 1; if (d[i][j - 1] + 1 < best) best = d[i][j - 1] + 1; if (i > 1 && j > 1 && a.data[i - 1] == b.data[j - 2] && a.data[i - 2] == b.data[j - 1] && d[i - 2][j - 2] + 1 < best) best = d[i - 2][j - 2] + 1; d[i][j] = best; } } return d[a.len][b.len]; } static void near_start(Str want) { near_want = want; near_best = (Str){0}; near_dist = want.len < 3 ? 1 : want.len < 6 ? 2 : 3; // more than allowed } static void near_offer(Str cand) { if (!cand.len || equals(cand, near_want) || startswith(cand, S("macro_"))) return; iz d = edit_distance(near_want, cand); if (d < near_dist) near_dist = d, near_best = cand; } static void near_map(Map *m) { if (!m) return; near_offer(m->key); for (int i = 0; i < 4; i++) near_map(m->child[i]); } static void near_help(void) { if (near_best.len) help_text = fmtstr("did you mean %s?", near_best); } // "a", "a and b", "a, b and c" static Str join_and(Str *items, iz n) { Buf b = {0}; for (iz i = 0; i < n; i++) { bput(&b, i == 0 ? S("") : i == n - 1 ? S(" and ") : S(", ")); bput(&b, items[i]); } return bstr(&b); } // t has no field or variant name: say so, with the closest one or the list static b32 type_outside(Type *t); NORETURN static void no_member(Pos at, Type *t, Str name) { b32 un = isunion(t); Buf all = {0}; near_start(name); b32 outside = type_outside(t); for (iz i = 0; i < t->fields.len; i++) { Str m = un ? variant_name(t, i) : t->fields.data[i].name; if (outside && t->fields.data[i].isprivate) continue; // not reachable from here bputf(&all, "%s%s", all.len ? S(", ") : S(""), m); near_offer(m); } near_help(); if (!help_text.len && all.len) help_text = fmtstr("the %s of %s: %s", un ? S("variants") : S("fields"), type_name(t), bstr(&all)); errorat(at, "%s has no %s %s", type_name(t), un ? S("variant") : S("field"), name); } // module alias has nothing (of that kind) named name NORETURN static void no_module_member(Pos at, Str alias, Scope *scope, Str name, Str what) { near_start(name); near_map(scope->names); near_help(); errorat(at, "module %s has no %s%s", alias, what, name); } static STATE struct { Module **data; iz len; iz cap; } modules = {0}; typedef struct { Scope **data; iz len; iz cap; } Scopes; // Each file sees its imports: aliases by name, plain imports merged in. typedef struct { Map *aliases; struct { Module **data; iz len; iz cap; } open; Scope *scope; } FileImports; static STATE struct { FileImports *data; iz len; iz cap; } fileimports = {0}; static STATE i32 sym_counter = 0; static STATE Fn *signature_of = 0; // whose signature is being resolved, and for which call static STATE Pos signature_at = {-1, 0, 0, 0}; static void fn_signature_of(Fn *f); static Node *pending_macro_before(Fn *f); static STATE b32 meta_mode = 0; // star99 --debug: the run-time program is generated for debuggers (#line, // Star99 names, a shadow stack); the metaprogram never is static STATE b32 debug_build = 0; static STATE Str debugger = {0}; // star99 --gdb or --lldb: the debugger to start static b32 debugging(void) { return debug_build && !meta_mode; } static STATE struct { Fn **data; iz len; iz cap; } macro_fns = {0}; static STATE Module *code_module = 0; // std:code, when loaded static STATE struct { Fn **data; iz len; iz cap; } macro_texts = {0}; // per macro: a wrapper giving its result as source text static Scope * mkscope(Scope *parent, i32 kind) { Scope *s = new(&perm, 1, Scope); s->parent = parent; s->kind = kind; if (parent) { s->fn = parent->fn; s->file = parent->file; } return s; } static Sym * mksym(SymKind k, Str name, Pos p) { Sym *s = new(&perm, 1, Sym); s->kind = k; s->name = name; s->pos = p; s->module = -1; return s; } static Sym * scope_get(Scope *s, Str name) { return lookup(s->names, name); } static void declare(Scope *s, Sym *sym) { if (equals(sym->name, S("yield"))) { help_text = S("choose another name"); errorat(sym->pos, "yield is built in: it cannot be declared, overloaded or hidden"); } void **slot = upsert(&s->names, sym->name, 1); if (*slot) { Sym *old = *slot; notes.len = 0; note(old->pos, S("first declared here")); // and any later declarations of the name in the same block if (sym->pos.file >= 0 && sym->pos.file < files.len) { Toks toks = lex(sym->pos.file); int depth = 0; for (iz k = 1; k < toks.len && depth >= 0; k++) { Tok *t = toks.data + k; TokKind before = toks.data[k - 1].kind; if (t->pos.offset <= sym->pos.offset) continue; depth += (t->kind == T_LBRACE) - (t->kind == T_RBRACE); if (!depth && equals(t->text, sym->name) && (before == T_VAR || before == T_CONST || before == T_STRUCT || before == T_UNION)) { note(t->pos, S("declared again here")); } } } errorat(sym->pos, "%s is already declared", sym->name); } *slot = sym; } static Str cident(Str name) { Buf b = {0}; for (iz i = 0; i < name.len; i++) { u8 c = name.data[i]; if (isalphac(c) || isdigitc(c)) bputc(&b, c); else bputf(&b, "_%02x", c); } return bstr(&b); } static Str newcname(Str prefix, Str name) { return fmtstr("%s%d_%s", prefix, ++sym_counter, cident(name)); } // Variant types: a case function parameter written Shape.circle takes the // payload of that variant. The type node's info says which variant it is, // and param_variants keeps that for the parameter. typedef struct { Type *u; iz index; } VariantRef; static Type *resolve_type(Node *n, Scope *s); static void resolve_named(Type *t); static Coro *coroutine_named(Node *a, Scope *s); static void co_need_body(Coro *co); static Coro *yield_target(Scope *s, Pos at); static void no_const_overflow(Node *n, Scope *s); static void not_private(Str name, Pos p); static Sym *find(Scope *s, Str name, Pos p, b32 must); static Type *check_expr(Node *n, Scope *s, Type *want); static b32 const_int(Node *n, Scope *s, i64 *out); static void warn_zero_default(Node *param, Type *t); static b32 is_constant(Node *n, Scope *s); static Type *check_value(Node **slot, Scope *s, Type *want, Str what); static Type *builtin_type(Str name); static void fill_fields(Fields *out, Nodes *params, Scope *s, b32 isunion, b32 isfn) { for (iz i = 0; i < params->len; i++) { Node *pn = params->data[i]; Field f = {0}; f.name = pn->name; f.pos = pn->pos; f.isprivate = (pn->flags & F_PRIVATE) != 0; f.isvar = (pn->flags & F_VAR) != 0; f.israw = (pn->flags & F_RAW) != 0; f.def = pn->b; if (pn->a) { Node *tn0 = pn->a; b32 bare = isunion && !pn->name.len && tn0->kind == N_TNAME && !tn0->list.len && !find(s, tn0->name, tn0->pos, 0) && !builtin_type(tn0->name); if (bare) { // union T (I, EOF): meant as payload-free variants Buf lower = {0}; for (iz k = 0; k < tn0->name.len; k++) { u8 c = tn0->name.data[k]; bputc(&lower, c >= 'A' && c <= 'Z' ? c + 32 : c); } help_text = fmtstr("a variant without a payload uses snake_case: %s", bstr(&lower)); } f.type = resolve_type(pn->a, s); } else if (pn->b) { f.type = 0; // inferred from the default, filled in by the caller } else if (!isunion) { errorat(pn->pos, "%s needs a type; write %s: Type", pn->name, pn->name); } if (f.isvar && !isfn) errorat(pn->pos, "only function parameters can be var"); if (f.isvar && f.def) errorat(pn->pos, "a var parameter cannot have a default"); for (iz j = 0; j < out->len; j++) { if (f.name.len && equals(out->data[j].name, f.name)) errorat(pn->pos, "%s appears twice", f.name); } *push(&perm, out) = f; } } static Type * builtin_type(Str name) { if (equals(name, S("Int"))) return t_int; if (equals(name, S("Float"))) return t_float; if (equals(name, S("Bool"))) return t_bool; if (equals(name, S("String"))) return t_string; if (equals(name, S("Any"))) return t_any; static Str ints[] = {SL("I8"), SL("I16"), SL("I32"), SL("I64"), SL("U8"), SL("U16"), SL("U32"), SL("U64")}; for (int i = 0; i < 8; i++) { if (equals(name, ints[i])) return t_ints[i]; } return 0; } static Type *fn_value(Type *ft); static Type * fn_type(Fields params, Type *result) { Type *t = mktype(TY_FN); t->fields = params; t->elem = result ? result : t_void; t->managed = 1; // a closure value may hold a counted environment return intern(t); } // The type of a function used as a value: parameter names and defaults do // not matter, only the types and which ones are var. static Type * fn_value(Type *ft) { Fields ps = {0}; for (iz i = 0; i < ft->fields.len; i++) { Field f = {0}; f.type = ft->fields.data[i].type; f.isvar = ft->fields.data[i].isvar; *push(&perm, &ps) = f; } Type *t = mktype(TY_FN); t->fields = ps; t->elem = ft->elem ? ft->elem : t_void; t->managed = 1; return intern(t); } static void type_args(Node *n, iz want, Str form) { if (n->list.len != want) errorat(n->pos, "%s", form); } static STATE b32 in_macro_code = 0; // checking compile-time code enum { P_EXPR, P_STMT, P_DECL, P_TYPE }; static Sym *macro_sym(Node *call, Scope *s); static Node *expand(Node *call, Scope *s, int position); static Type *callee_type(Node *c, Scope *s); static Type *type_query(Node *call, Scope *s); static Type *type_arg(Node *a, Scope *s); static b32 is_type_query(Node *n); static Type * resolve_type(Node *n, Scope *s) { if (n->kind == N_TNAME && !n->name.len && n->type) return n->type; // filled in from a target switch (n->kind) { case N_TNAME: { if (n->a && (n->flags & F_CALLED)) { // a projection: Shape.circle, a variant type Type *u = resolve_type(n->a, s); resolve_named(u); if (u->kind == TY_STRUCT) { // Point.x: the type of a field for (iz i = 0; i < u->fields.len; i++) { if (equals(u->fields.data[i].name, n->name)) return u->fields.data[i].type; } no_member(n->pos, u, n->name); } if (!isunion(u)) errorat(n->pos, "%s has no fields or variants, so it has no %s", type_name(u), n->name); iz vi = variant_index(u, n->name); if (vi < 0) no_member(n->pos, u, n->name); Field *f = u->fields.data + vi; VariantRef *vr = new(&perm, 1, VariantRef); vr->u = u; vr->index = vi; n->info = vr; return f->type ? f->type : t_void; } Str name = n->name; if (equals(name, S("List"))) { if (n->list.len == 1) return list_of(resolve_type(n->list.data[0], s)); if (n->list.len == 2) { i64 count = 0; no_const_overflow(n->list.data[1], s); if (!const_int(n->list.data[1], s, &count) || count < 0) errorat(n->list.data[1]->pos, "the length of List(T, n) must be a constant that is not negative"); return array_of(resolve_type(n->list.data[0], s), count); } errorat(n->pos, "List takes an element type: List(T), or List(T, n) for a fixed length"); } if (equals(name, S("Map"))) { type_args(n, 2, S("Map takes a key and a value type: Map(K, V)")); Type *k = resolve_type(n->list.data[0], s); if (k->kind != TY_INT && !k->text && k->kind != TY_BOOL) errorat(n->list.data[0]->pos, "map keys are integers (Int, U8, I32, ...), strings or Bool, not %s", type_name(k)); return map_of(k, resolve_type(n->list.data[1], s)); } if (equals(name, S("Pool"))) { type_args(n, 1, S("Pool takes one element type: Pool(T)")); return pool_of(resolve_type(n->list.data[0], s)); } if (equals(name, S("Ref"))) { type_args(n, 1, S("Ref takes one type: Ref(T)")); return ref_of(resolve_type(n->list.data[0], s)); } if (equals(name, S("Raw"))) { type_args(n, 1, S("Raw takes one code type: Raw(Code)")); return resolve_type(n->list.data[0], s); } if (equals(name, S("Expr")) && n->list.len == 1) { // Expr(T): an expression giving a T. The type argument documents // the result and is checked where the code is placed. Node plain = *n; plain.list = (Nodes){0}; resolve_type(n->list.data[0], s); return resolve_type(&plain, s); } Type *bt = builtin_type(name); if (bt) { if (n->list.len) errorat(n->pos, "%s takes no type arguments", name); return bt; } if (equals(name, S("Coroutine")) && !n->c && n->list.len == 1 && (n->list.data[0]->kind == N_NAME || n->list.data[0]->kind == N_FIELD)) return coroutine_named(n->list.data[0], s)->type; // its fields come later Sym *sym = 0; if (n->c) { Sym *alias = find(s, n->c->name, n->c->pos, 1); if (alias->kind != S_MODULE) errorat(n->c->pos, "%s is not an imported module", n->c->name); sym = scope_get(alias->mod->scope, name); if (!sym || sym->kind != S_TYPE) no_module_member(n->pos, n->c->name, alias->mod->scope, name, S("type ")); if (sym->isprivate) errorat(n->pos, "%s is private to %s", name, alias->mod->name); } else { // Type and File are built in unless something else has the name sym = find(s, name, n->pos, 0); if (!sym && equals(name, S("Type")) && !n->list.len) return t_type; if (!sym && equals(name, S("File")) && !n->list.len) return t_file; if (!sym && equals(name, S("Draw")) && !n->list.len) return t_draw; if (!sym) { // the types of the scope, or a built-in type static Str builtin[] = {SL("Int"), SL("Float"), SL("Bool"), SL("String"), SL("Any"), SL("List"), SL("Map"), SL("Pool"), SL("Ref"), SL("Type"), SL("File"), SL("I8"), SL("I16"), SL("I32"), SL("I64"), SL("U8"), SL("U16"), SL("U32"), SL("U64")}; near_start(name); for (Scope *sc = s; sc; sc = sc->parent) near_map(sc->names); for (iz i = 0; i < countof(builtin); i++) near_offer(builtin[i]); near_help(); Node *later = signature_of ? pending_macro_before(signature_of) : 0; if (later) { // the signature of a function used early names a type made later notes.len = 0; note(later->pos, S("this macro call runs first")); help_text = (Str){0}; errorat(signature_at.file >= 0 ? signature_at : n->pos, "%s is used here, but its signature names %s, which is not declared " "yet: a macro call before %s has not run", signature_of->name, name, signature_of->name); } not_private(name, n->pos); errorat(n->pos, "unknown type %s", name); } } if (sym->kind != S_TYPE) errorat(n->pos, "%s is not a type", name); if (sym->ismacro && !in_macro_code) errorat(n->pos, "%s exists only at compile time", name); if (n->list.len) errorat(n->pos, "%s takes no type arguments", name); sym->used = 1; return sym->type; } case N_TFN: { Fields params = {0}; for (iz i = 0; i < n->list.len; i++) { Field f = {0}; f.type = resolve_type(n->list.data[i], s); *push(&perm, ¶ms) = f; } return fn_type(params, n->a ? resolve_type(n->a, s) : t_void); } case N_TSTRUCT: case N_TUNION: { Type *t = mktype(n->kind == N_TSTRUCT ? TY_STRUCT : TY_UNION); t->nonil = (n->flags & F_NONIL) != 0; fill_fields(&t->fields, &n->list, s, n->kind == N_TUNION, 0); for (iz i = 0; i < t->fields.len; i++) { Field *f = t->fields.data + i; if (f->def) { Node *pn = n->list.data[i]; if (!is_constant(pn->b, s)) errorat(pn->b->pos, "a default must be a constant"); check_value(&pn->b, s, f->type, f->name); f->def = pn->b; if (!f->type) f->type = pn->b->type; warn_zero_default(pn, f->type); } } t->resolved = 1; t->decl = n; Type *r = intern(t); if (r == t) resolve_named(t); return r; } case N_TMACRO: { // type_of, args_of and ret_of, or a macro call whose result is the type if (n->type) return n->type; if (is_type_query(n->a)) return n->type = type_query(n->a, s); Node *r = macro_sym(n->a, s) ? expand(n->a, s, P_TYPE) : 0; if (!r) errorat(n->pos, "only a macro call can stand for a type"); n->b = r; // what --expand prints n->type = resolve_type(r, s); return n->type; } default: errorat(n->pos, "expected a type"); } } // Whether the zero value of t contains a value of target: struct fields and // fixed lists hold their parts inline, a no_nil union starts as its first // variant, and other unions start as nil. static b32 contains_by_value(Type *t, Type *target, int depth) { if (depth > 64) return 0; switch (t->kind) { case TY_STRUCT: for (iz i = 0; i < t->fields.len; i++) { Type *ft = t->fields.data[i].type; if (!ft) continue; if (ft == target) return 1; if (contains_by_value(ft, target, depth + 1)) return 1; } return 0; case TY_UNION: if (!t->nonil || !t->fields.len || !t->fields.data[0].type) return 0; if (t->fields.data[0].type == target) return 1; return contains_by_value(t->fields.data[0].type, target, depth + 1); case TY_ARRAY: return t->count && (t->elem == target || contains_by_value(t->elem, target, depth + 1)); default: return 0; } } // A type that holds target anywhere inside: any field, variant or element // (a reference only points at one). Returns the type it goes through. static Type * holds_type(Type *t, Type *target, int depth) { if (!t || depth > 64) return 0; switch (t->kind) { case TY_STRUCT: case TY_UNION: for (iz i = 0; i < t->fields.len; i++) { Type *ft = t->fields.data[i].type; if (ft == target || holds_type(ft, target, depth + 1)) return ft; } return 0; case TY_ARRAY: case TY_LIST: case TY_POOL: case TY_MAP: return t->elem == target || holds_type(t->elem, target, depth + 1) ? t->elem : 0; default: return 0; } } static void compute_flags(Type *t, int depth); // Named struct and union types are created when declared and resolved on // first use, so they can refer to each other in any order. static void resolve_named(Type *t) { if (t->co) { // Coroutine(NAME): from the body of NAME co_need_body(t->co); return; } if (t->resolved || !t->decl) { compute_flags(t, 0); return; } if (t->resolving) return; t->resolving = 1; Node *d = t->decl; Scope *s = (Scope *)d->info; fill_fields(&t->fields, &d->list, s, t->kind == TY_UNION, 0); for (iz i = 0; i < t->fields.len; i++) { Field *f = t->fields.data + i; if (f->def) { Node *pn = d->list.data[i]; if (!is_constant(pn->b, s)) errorat( pn->b->pos, "a default must be a constant: a literal, a named constant, or constant arithmetic"); check_value(&pn->b, s, f->type, f->name); f->def = pn->b; if (!f->type) f->type = pn->b->type; if (f->type == t_nil) errorat(pn->b->pos, "nil needs a type here"); warn_zero_default(pn, f->type); } if (t->kind == TY_UNION) { if (f->name.len == 0 && !f->type) errorat(f->pos, "a variant needs a name"); for (iz j = 0; j < i; j++) { if (equals(variant_name(t, j), variant_name(t, i))) errorat(f->pos, "%s has two variants named %s", type_name(t), variant_name(t, i)); } if (equals(f->name, S("none"))) errorat(f->pos, "a variant cannot be named none; nil already means that"); } } if (t->kind == TY_UNION && !t->fields.len) errorat(d->pos, "a union needs at least one variant"); t->resolving = 0; t->resolved = 1; for (iz i = 0; i < t->fields.len; i++) { Type *ft = t->fields.data[i].type; if (ft && (ft->kind == TY_STRUCT || ft->kind == TY_UNION)) resolve_named(ft); } if (t->kind == TY_STRUCT && contains_by_value(t, t, 0)) { for (iz i = 0; i < t->fields.len; i++) { Type *ft = t->fields.data[i].type; // through a no_nil union: its fault if (ft && isunion(ft) && ft->nonil && ft->decl && holds_type(ft, ft, 0)) { help_text = S("nil is what lets a recursive value end: drop no_nil"); errorat(ft->decl->pos, "%s is no_nil, so it cannot contain itself (here through %s)", type_name(ft), type_name(t)); } } help_text = fmtstr("hold the inner one in union (%s) or List(%s)", type_name(t), type_name(t)); errorat(d->pos, "%s contains itself, so it would be infinitely large", type_name(t)); } Type *via = t->kind == TY_UNION && t->nonil ? holds_type(t, t, 0) : 0; if (via) { help_text = S("nil is what lets a recursive value end: drop no_nil"); if (via == t) errorat(d->pos, "%s is no_nil, so it cannot contain itself", type_name(t)); errorat(d->pos, "%s is no_nil, so it cannot contain itself (here through %s)", type_name(t), type_name(via)); } compute_flags(t, 0); } static b32 part_refs(Type *p, int depth) { if (!p) return 0; if (p->kind == TY_REF) return 1; if (p->state == -1) return 0; int save = p->state; p->state = -1; compute_flags(p, depth + 1); p->state = save; return p->has_refs; } static void compute_flags(Type *t, int depth) { if (depth > 64 || t->state == -1) return; switch (t->kind) { case TY_LIST: case TY_ARRAY: case TY_POOL: case TY_MAP: t->has_refs |= part_refs(t->elem, depth); break; case TY_STRUCT: case TY_UNION: // flags only ever turn on, so repeated passes reach a fixed point for (iz i = 0; i < t->fields.len; i++) t->has_refs |= part_refs(t->fields.data[i].type, depth); break; default: break; } switch (t->kind) { case TY_ANY: case TY_LIST: case TY_MAP: case TY_POOL: t->managed = 1; break; case TY_ARRAY: compute_flags(t->elem, depth + 1); t->managed |= t->elem->managed; t->has_defaults |= t->elem->has_defaults; break; case TY_UNION: t->boxed = 0; for (iz i = 0; i < t->fields.len; i++) { if (t->fields.data[i].type && t->fields.data[i].type != t_void) t->boxed = 1; } t->managed = t->boxed; if (t->nonil && t->fields.len && t->fields.data[0].type) { Type *ft = t->fields.data[0].type; if (ft->kind == TY_STRUCT || ft->kind == TY_UNION || ft->kind == TY_ARRAY) { int save = t->state; t->state = -1; compute_flags(ft, depth + 1); t->state = save; } } t->has_defaults = t->nonil; // the zero value of a no_nil union is its first variant break; case TY_STRUCT: for (iz i = 0; i < t->fields.len; i++) { Field *f = t->fields.data + i; if (!f->type) continue; int save = t->state; t->state = -1; compute_flags(f->type, depth + 1); t->state = save; if (f->type->managed) t->managed = 1; if (f->def || f->type->has_defaults) t->has_defaults = 1; } break; default: break; } } // A number as %g prints it, made to read as a float: with ".0" unless it // has a point or an exponent, or is nan or inf. static Str as_float(Str s) { for (iz i = 0; i < s.len; i++) { u8 c = s.data[i]; if (c == '.' || c == 'e' || c == 'n' || c == 'i') return s; } return concat(s, S(".0")); } // The shortest text that reads back as v. static Str float_text(f64 v) { for (int prec = 1;; prec++) { Str s = os_gformat(v, prec); if (prec == 17 || os_strtod(s) == v) return as_float(s); } } // Printing syntax back as source. Macros move code as text, so every node a // macro can see must print as code that parses to the same node. static void print_node(Buf *b, Node *n, int ind); // While a template is printed, splices print as \x01 and names the template // declares print as \x02name, to be filled in each time the template runs. static STATE struct { Node **data; iz len; iz cap; } splice_nodes = {0}; static STATE b32 printing_template = 0; static STATE Map *hyg_names = 0; // Names of a declaration interface (parameters, fields, variants) are never // renamed for hygiene; parameters of a function literal are its locals. static STATE b32 iface_names = 0, local_params = 0; static void own_prefix(Buf *b, Pos p, Str name, b32 type); static b32 other_module_sym(Sym *s); static void print_name(Buf *b, Str name) { if (printing_template && !iface_names && lookup(hyg_names, name)) bputc(b, 2); bput(b, name); } static void print_block(Buf *b, Node *blk, int ind); static void print_splice(Buf *b, Node *e) { if (printing_template) { *push(&perm, &splice_nodes) = e; bputc(b, 1); return; } if (e->kind == N_NAME) { bputc(b, '$'); print_name(b, e->name); return; } bput(b, S("$(")); print_node(b, e, 0); bputc(b, ')'); } // A $ in the place of a name (fn $f, var $v, .$x): the splice, or what it // splices. static void print_slot(Buf *b, Node *x) { print_splice(b, x->kind == N_SPLICE ? x->a : x); } static void print_indent(Buf *b, int ind) { for (int i = 0; i < ind; i++) bput(b, S(" ")); } static void print_quoted(Buf *b, Str s) { bputc(b, '"'); for (iz i = 0; i < s.len; i++) { u8 c = s.data[i]; if (c == '"' || c == '\\') { bputc(b, '\\'); bputc(b, c); } else if (c == '\n') { bput(b, S("\\n")); } else if (c == '\t') { bput(b, S("\\t")); } else if (c == '\r') { bput(b, S("\\r")); } else if (c < 32 || c == 127) { bputf(b, "\\x%02x", c); } else { bputc(b, c); } } bputc(b, '"'); } static void fmt_before(Buf *b, Pos p, int ind, b32 allow_blank); static STATE i32 fmt_file = 0; // the file being formatted, plus one; STATE starts at zero // Lists keep explicit source line breaks. Other lists split if their line // would go past 80 columns: one entry per line, each ending with a comma, // and the closing bracket on its own line. print_fit chooses outermost // lists first. A comment inside a list keeps it split too. static STATE iz fmt_next = 0; // the next comment to print static STATE int type_lists = 0; // type arguments and indexes: no automatic width splitting static STATE struct { Nodes **data; iz len; iz cap; } split_lists = {0}; // the lists print_fit split static STATE struct { Nodes **data; iz len; iz cap; } mark_lists = {0}; // each list printed on one line, and where it opened static STATE struct { iz *data; iz len; iz cap; } mark_offsets = {0}; // Where the source of list l closes: past its last entry, commas, spaces // and comments. static iz list_end(Nodes *l) { Node *last = l->data[l->len - 1]; Str t = files.data[last->pos.file].text; iz k = last->pos.hi > last->pos.lo ? last->pos.hi : last->pos.offset; while (k < t.len) { u8 c = t.data[k]; if (c == '#') { while (k < t.len && t.data[k] != '\n') k++; } else if (c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == ',') { k++; } else { break; } } return k; } static b32 list_split(Nodes *l) { if (l->len < 1) return 0; if ((fmt_file || printing_expanded) && l->multiline) return 1; for (iz i = 0; i < split_lists.len; i++) if (split_lists.data[i] == l) return 1; // a comment not printed yet that comes before the list closes Pos f = l->data[0]->pos; if (!fmt_file || f.file != fmt_file - 1) return 0; SourceFile *sf = files.data + f.file; return fmt_next < sf->comments.len && sf->comments.data[fmt_next] < list_end(l); } static void list_mark(Buf *b, Nodes *l) { *push(&perm, &mark_lists) = l; *push(&perm, &mark_offsets) = b->len; } // Comments on the source line where p starts, printed after it. static void fmt_rest_of_line(Buf *b, Pos p, int ind) { Str t = files.data[p.file].text; iz e = p.offset; while (e < t.len && t.data[e] != '\n') e++; fmt_before(b, (Pos){p.file, (i32)e, 0, 0}, ind, 0); } static void print_list(Buf *b, Nodes *l, int ind) { if (list_split(l)) { // one entry per line, one level deeper, each ending in a comma, the // closing bracket under the opening line for (iz i = 0; i < l->len; i++) { bputc(b, '\n'); fmt_before(b, l->data[i]->pos, ind + 1, 0); print_indent(b, ind + 1); print_node(b, l->data[i], ind + 1); bputc(b, ','); } bputc(b, '\n'); fmt_rest_of_line(b, l->data[l->len - 1]->pos, ind + 1); print_indent(b, ind); return; } if (l->len && !type_lists) list_mark(b, l); for (iz i = 0; i < l->len; i++) { if (i) bput(b, S(", ")); print_node(b, l->data[i], ind); } } static void print_params(Buf *b, Nodes *l, int ind) { b32 iface = !local_params; local_params = 0; // a lone name before another entry would join its group after a comma // on one line, so then each entry goes on its own line b32 lines = list_split(l); for (iz i = 0; i + 1 < l->len; i++) { Node *p = l->data[i]; lines |= p->name.len && !p->a && !p->b && l->data[i + 1]->a; } bputc(b, '('); if (!lines && l->len) list_mark(b, l); for (iz i = 0; i < l->len;) { Node *p = l->data[i]; // entries parsed as one group, "x, y: Int", print as one iz j = i + 1; while (j < l->len && p->a && l->data[j]->a == p->a && l->data[j]->b == p->b && l->data[j]->flags == p->flags && l->data[j]->name.len && p->name.len) j++; if (lines) { bputc(b, '\n'); fmt_before(b, p->pos, ind + 1, 0); print_indent(b, ind + 1); } else if (i) { bput(b, S(", ")); } if (p->flags & F_PRIVATE) bput(b, S("private ")); if (p->flags & F_VAR) bput(b, S("var ")); for (iz k = i; k < j; k++) { if (k > i) bput(b, S(", ")); if (l->data[k]->c) { print_slot(b, l->data[k]->c); } else { b32 save = iface_names; iface_names = iface; print_name(b, l->data[k]->name); iface_names = save; } } // what an entry on its own line holds is nested one level deeper int inner = lines ? ind + 1 : ind; if (p->a) { if (p->name.len) bput(b, S(": ")); print_node(b, p->a, inner); } if (p->b) { bput(b, S(" = ")); print_node(b, p->b, inner); } if (lines) bputc(b, ','); i = j; } if (lines) { bputc(b, '\n'); if (l->len && fmt_file) fmt_rest_of_line(b, l->data[l->len - 1]->pos, ind + 1); print_indent(b, ind); } bputc(b, ')'); } // star99 fmt: the printer keeps the comments of fmt_file, and single blank // lines between statements. Comments inside an expression move before its // statement. static b32 blank_line_before(Str s, iz at) { iz i = at; while (i > 0 && s.data[i - 1] != '\n') i--; // start of this line if (i == 0) return 0; iz j = i - 1; while (j > 0 && s.data[j - 1] != '\n') j--; // start of the line before for (iz k = j; k < i - 1; k++) { if (s.data[k] != ' ' && s.data[k] != '\t' && s.data[k] != '\r') return 0; } return 1; } static void fmt_before(Buf *b, Pos p, int ind, b32 allow_blank) { if (!fmt_file || p.file != fmt_file - 1) return; SourceFile *f = files.data + fmt_file - 1; Str s = f->text; while (fmt_next < f->comments.len && f->comments.data[fmt_next] < p.offset) { iz c = f->comments.data[fmt_next++], e = c; while (e < s.len && s.data[e] != '\n') e++; Str text = substr(s, c, e); while (text.len && (text.data[text.len - 1] == ' ' || text.data[text.len - 1] == '\r')) text.len--; iz ls = c; while (ls > 0 && s.data[ls - 1] != '\n') ls--; b32 own = 1; for (iz k = ls; k < c; k++) own &= s.data[k] == ' ' || s.data[k] == '\t'; if (!own && b->len && b->data[b->len - 1] == '\n') { b->len--; // a comment after code stays on its line bputc(b, ' '); bput(b, text); bputc(b, '\n'); continue; } if (allow_blank && blank_line_before(s, c)) bputc(b, '\n'); allow_blank = 1; print_indent(b, ind); bput(b, text); bputc(b, '\n'); } if (p.offset < s.len && allow_blank && blank_line_before(s, p.offset) && !(b->len >= 2 && b->data[b->len - 1] == '\n' && b->data[b->len - 2] == '\n')) bputc(b, '\n'); } // Whether a comment not printed yet comes before p static b32 comment_before(Pos p) { if (!fmt_file || p.file != fmt_file - 1) return 0; SourceFile *f = files.data + fmt_file - 1; return fmt_next < f->comments.len && f->comments.data[fmt_next] < p.offset; } static iz print_stmt_at(Buf *b, Nodes *l, iz i, int ind); // The width of a line in characters (UTF-8 continuation bytes are not // characters of their own). static iz text_width(Str t, iz from, iz to) { iz w = 0; for (iz k = from; k < to; k++) w += (t.data[k] & 0xc0) != 0x80; return w; } // l[i] as print_stmt_at prints it, printed again with a list split while a // line is longer than 80 columns and a list opens on that line: the first // such list, so outer lists split before inner ones. The lists of nested // statements are their own print_fit. static iz print_fit(Buf *b, Nodes *l, iz i, int ind) { iz start = b->len, next = fmt_next, marks = mark_lists.len; for (;;) { iz n = print_stmt_at(b, l, i, ind); Str t = {b->data, b->len}; Nodes *pick = 0; iz ls = start; while (ls > 0 && t.data[ls - 1] != '\n') ls--; while (!pick && ls < t.len) { iz le = ls; while (le < t.len && t.data[le] != '\n') le++; if (text_width(t, ls, le) > 80) { for (iz k = marks; k < mark_lists.len && !pick; k++) if (mark_offsets.data[k] >= ls && mark_offsets.data[k] <= le) pick = mark_lists.data[k]; } ls = le + 1; } mark_lists.len = mark_offsets.len = marks; if (!pick) return n; *push(&perm, &split_lists) = pick; b->len = start; fmt_next = next; } } // Prints l[i]; a var or const that starts a group (var a, b: T) prints the // whole group. Returns how many statements it printed. static iz print_stmt_at(Buf *b, Nodes *l, iz i, int ind) { Node *n = l->data[i]; iz k = i + 1; while (n->kind == N_VAR && k < l->len && l->data[k]->kind == N_VAR && (l->data[k]->flags & F_GROUP)) k++; if (k == i + 1) { print_node(b, n, ind); return 1; } if (n->flags & F_PRIVATE) bput(b, S("private ")); if (n->flags & F_MACRO) bput(b, S("macro ")); bput(b, n->flags & F_CONST ? S("const ") : S("var ")); for (iz j = i; j < k; j++) { if (j > i) bput(b, S(", ")); print_name(b, l->data[j]->name); } if (n->a) { bput(b, S(": ")); print_node(b, n->a, ind); } if (n->b) { bput(b, S(" = ")); print_node(b, n->b, ind); } return k - i; } static void print_block(Buf *b, Node *blk, int ind) { SourceFile *f = fmt_file && blk->pos.file == fmt_file - 1 ? files.data + fmt_file - 1 : 0; b32 comments = f && fmt_next < f->comments.len && f->comments.data[fmt_next] < blk->end; if (!blk->list.len && !comments) { bput(b, S("{}")); return; } bput(b, S("{\n")); for (iz i = 0; i < blk->list.len;) { if (blk->list.data[i]->flags & F_HIDDEN) { // shown as its macro call i++; continue; } iz before = b->len; fmt_before(b, blk->list.data[i]->pos, ind + 1, i > 0); print_indent(b, ind + 1); iz start = b->len; i += print_fit(b, &blk->list, i, ind + 1); if (printing_expanded && b->len == start) { // an empty macro label: no line b->len = before; continue; } bputc(b, '\n'); } if (blk->end) fmt_before(b, (Pos){blk->pos.file, blk->end, 0, 0}, ind + 1, blk->list.len > 0); if (b->len >= 2 && b->data[b->len - 1] == '\n' && b->data[b->len - 2] == '\n') b->len--; print_indent(b, ind); bputc(b, '}'); } // Binding strength for printing: what parses below minprec needs parentheses. static int node_prec(Node *n) { if (n->src) return node_prec(n->src); // it prints as written switch (n->kind) { case N_BINARY: case N_IN: case N_RANGE: return n->op == T_STARSTAR ? 8 : binprec(n->op); case N_UNARY: return n->op == T_NOT ? 2 : 7; case N_IFX: case N_FNLIT: return -1; // (fn (n: Int) do n + 1)(5) default: return 9; } } // A call written without parentheses (a b c) may print that way only where // it reads the same: a whole statement, the value of a declaration, // assignment or return, or the last argument of such a call. Elsewhere it // prints in parentheses: (a b) + 1. static STATE b32 bare_ok = 0; static void print_bare(Buf *b, Node *n, int ind) { bare_ok = 1; print_node(b, n, ind); } static void print_operand(Buf *b, Node *n, int ind, int minprec) { b32 parens = node_prec(n) < minprec; if (parens) bputc(b, '('); print_node(b, n, ind); if (parens) bputc(b, ')'); } static void print_body(Buf *b, Node *blk, int ind) { if ((blk->flags & F_DO) && blk->list.len == 1) { bput(b, S("do ")); print_node(b, blk->list.data[0], ind); return; } print_block(b, blk, ind); } static Str opname(TokKind k) { switch (k) { case T_PLUS: return S("+"); case T_MINUS: return S("-"); case T_STAR: return S("*"); case T_STARSTAR: return S("**"); case T_SLASH: return S("/"); case T_PERCENT: return S("%"); case T_EQEQ: return S("=="); case T_NE: return S("!="); case T_LT: return S("<"); case T_LE: return S("<="); case T_GT: return S(">"); case T_GE: return S(">="); case T_AND: return S("and"); case T_OR: return S("or"); case T_NOT: return S("not"); case T_DOTLT: return S("..<"); case T_DOTEQ: return S("..="); case T_LTDOTLT: return S("<..<"); case T_LTDOTEQ: return S("<..="); case T_EQ: return S("="); case T_PLUSEQ: return S("+="); case T_MINUSEQ: return S("-="); case T_STAREQ: return S("*="); case T_SLASHEQ: return S("/="); case T_PERCENTEQ: return S("%="); case T_IN: return S("in"); default: return S("?"); } } static b32 print_chosen_call(Buf *b, Node *n, int ind); static void print_node(Buf *b, Node *n, int ind) { if (!n) return; if (n->src && printing_expanded && (int)n->kind == X_CALLFN && print_chosen_call(b, n, ind)) return; if (n->src) { print_node(b, n->src, ind); return; } b32 bare = bare_ok; bare_ok = 0; switch ((int)n->kind) { case N_INT: if (n->name.len) bput(b, n->name); else bputf(b, "%lld", (long long)n->ival); return; case N_FLOAT: if (n->name.len) bput(b, n->name); else bput(b, float_text(n->fval)); return; case N_STR: if (n->name.len) bput(b, n->name); else print_quoted(b, n->sval); return; case N_BOOL: bput(b, n->ival ? S("true") : S("false")); return; case N_NIL: bput(b, S("nil")); return; case N_NAME: if (other_module_sym(n->sym)) own_prefix(b, n->pos, n->name, 0); print_name(b, n->name); return; case N_TYPENAME: own_prefix(b, n->pos, n->name, 1); bput(b, n->name); return; case N_SPLICE: print_splice(b, n->a); return; case N_TEMPLATE: if (n->flags & F_DO) { Node blk = {0}; blk.list = n->list; bput(b, S("code ")); print_block(b, &blk, ind); } else { bput(b, S("code(")); print_node(b, n->a, ind); bputc(b, ')'); } return; case X_CONV: print_node(b, n->a, ind); return; // a conversion the checker added case X_CALLFN: case N_CALL: if (n->flags & F_COMMAND) { if (!bare) bputc(b, '('); print_operand(b, n->a, ind, 9); for (iz i = 0; i < n->list.len; i++) { bput(b, i ? S(", ") : S(" ")); if (i == n->list.len - 1) print_bare(b, n->list.data[i], ind); else print_node(b, n->list.data[i], ind); } if (!bare) bputc(b, ')'); return; } print_operand(b, n->a, ind, 9); bputc(b, '('); print_list(b, &n->list, ind); bputc(b, ')'); return; case N_NAMED: if (n->c) print_slot(b, n->c); bput(b, n->name); bput(b, S(": ")); print_node(b, n->a, ind); return; case N_FIELD: print_operand(b, n->a, ind, 9); bputc(b, '.'); if (n->b) print_slot(b, n->b); else bput(b, n->name); return; case N_INDEX: print_operand(b, n->a, ind, 9); bputc(b, '['); type_lists++; // indexes do not split just because a line is long print_list(b, &n->list, ind); type_lists--; bputc(b, ']'); return; case N_DEREF: print_operand(b, n->a, ind, 9); bput(b, S(".*")); return; case N_ADDR: bputc(b, '&'); print_operand(b, n->a, ind, 7); return; case N_UNARY: bput(b, n->op == T_NOT ? S("not ") : opname(n->op)); bare_ok = bare; // not (a == b): a binary operand keeps parentheses, though not // binds looser and they are not needed if (n->op == T_NOT) print_operand( b, n->a, ind, n->a->kind == N_BINARY || n->a->kind == N_IN || n->a->kind == N_RANGE ? 9 : 2); else print_operand(b, n->a, ind, 7); return; case N_BINARY: case N_IN: case N_RANGE: { if (n->op == T_STARSTAR) { // right associative, and tighter than unary minus: -a ** b ** c print_operand(b, n->a, ind, 9); bput(b, S(" ** ")); bare_ok = bare; print_operand(b, n->b, ind, 7); return; } // left associative; comparisons and ranges do not chain at all int p = binprec(n->op); if (n->kind != N_RANGE || !(n->flags & F_IMPLICIT)) print_operand(b, n->a, ind, p == 3 || p == 4 ? p + 1 : p); if (n->kind == N_RANGE) bput(b, opname(n->op)); // 0..<10 else bputf(b, " %s%s ", n->kind == N_IN && (n->flags & F_NOT) ? S("not ") : S(""), opname(n->op)); bare_ok = bare; // 1 + double 3: the right end may stay bare print_operand(b, n->b, ind, p + 1); return; } case N_LIST: bputc(b, '['); print_list(b, &n->list, ind); if (n->flags & F_MAP) bputc(b, ':'); bputc(b, ']'); return; case N_PAIR: print_node(b, n->a, ind); bput(b, S(": ")); print_node(b, n->b, ind); return; case N_ANON: bput(b, S(".(")); print_list(b, &n->list, ind); bputc(b, ')'); return; case N_VARIANT: bputc(b, '.'); if (n->a) print_slot(b, n->a); else bput(b, n->name); if (n->flags & F_CALLED) { bputc(b, '('); print_list(b, &n->list, ind); bputc(b, ')'); } return; case N_CAST: print_node(b, n->a, ind); bputc(b, '('); print_node(b, n->b, ind); bputc(b, ')'); return; case N_BLOCKX: print_block(b, n->a, ind); return; case N_IFX: bput(b, S("if ")); print_node(b, n->a, ind); bput(b, n->b->kind == N_BLOCKX ? S(" ") : S(" do ")); print_operand(b, n->b, ind, 0); // an if inside needs parentheses bput(b, S(" else ")); print_node(b, n->c, ind); return; case N_SWITCHX: print_node(b, n->a, ind); return; case N_FNLIT: if (n->flags & F_MACRO) bput(b, S("macro ")); bput(b, S("fn ")); local_params = 1; print_params(b, &n->list, ind); if (n->a) { bput(b, S(" -> ")); print_node(b, n->a, ind); } bputc(b, ' '); print_body(b, n->b, ind); return; case N_TYPEX: print_node(b, n->a, ind); return; case N_TNAME: if (!n->name.len && n->type) { // a type the checker filled in put_type_name(b, n->type); return; } if (equals(n->name, S("$")) && n->a) { print_slot(b, n->a); return; } if (n->a && (n->flags & F_CALLED)) { print_node(b, n->a, ind); bputc(b, '.'); bput(b, n->name); return; } if (n->c) { bput(b, n->c->name); bputc(b, '.'); } else { own_prefix(b, n->pos, n->name, 1); } bput(b, n->name); if (n->list.len) { // type arguments split only when written across lines bputc(b, '('); type_lists++; print_list(b, &n->list, ind); type_lists--; bputc(b, ')'); } return; case N_TFN: bput(b, S("fn (")); type_lists++; print_list(b, &n->list, ind); type_lists--; bputc(b, ')'); if (n->a) { bput(b, S(" -> ")); print_node(b, n->a, ind); } return; case N_TSTRUCT: case N_TUNION: if (n->flags & F_NONIL) bput(b, S("no_nil(")); bput(b, n->kind == N_TSTRUCT ? S("struct ") : S("union ")); print_params(b, &n->list, ind); if (n->flags & F_NONIL) bputc(b, ')'); return; case N_TMACRO: print_node(b, n->b ? n->b : n->a, ind); return; case N_VAR: if (n->flags & F_PRIVATE) bput(b, S("private ")); if (n->flags & F_MACRO) bput(b, S("macro ")); bput(b, n->flags & F_CONST ? S("const ") : S("var ")); if (n->c) print_slot(b, n->c); else print_name(b, n->name); if (n->a) { bput(b, S(": ")); print_node(b, n->a, ind); } if (n->b) { bput(b, S(" = ")); print_bare(b, n->b, ind); } return; case N_ASSIGN: print_node(b, n->a, ind); bputf(b, " %s ", opname(n->op)); print_bare(b, n->b, ind); return; case N_DISCARD: bput(b, S("_ = ")); print_bare(b, n->a, ind); return; case N_EXPR: bare_ok = 1; print_operand(b, n->a, ind, 0); return; // (if ...) stays an expression case N_IF: if (n->flags & F_MACRO) bput(b, S("macro ")); bput(b, S("if ")); print_node(b, n->a, ind); bputc(b, ' '); print_body(b, n->b, ind); if (n->c) { bput(b, S(" else ")); Node *only = n->c->list.len == 1 ? n->c->list.data[0] : 0; if (only && only->kind == N_IF && !(n->c->flags & F_DO)) print_node(b, only, ind); // else if else if (n->c->flags & F_DO && only) print_node(b, only, ind); else print_block(b, n->c, ind); } return; case N_FOR: if (n->flags & F_MACRO) bput(b, S("macro ")); bput(b, S("for ")); if (n->flags & F_FILTER) { print_name(b, n->name); bput(b, S(": ")); print_node(b, n->c, ind); bput(b, S(" in ")); } else if (!(n->flags & F_IMPLICIT)) { print_name(b, n->name); if (n->sval.len) { bput(b, S(", ")); print_name(b, n->sval); } bput(b, S(" in ")); } print_node(b, n->a, ind); bputc(b, ' '); print_body(b, n->b, ind); return; case N_WHILE: bput(b, S("while ")); print_node(b, n->a, ind); bputc(b, ' '); print_body(b, n->b, ind); return; case N_BLOCK: print_block(b, n, ind); return; case N_RETURN: bput(b, S("return")); if (n->a) { bputc(b, ' '); bare_ok = 1; print_operand(b, n->a, ind, 0); // return (if ...) keeps its else } return; case N_BREAK: case N_CONTINUE: bput(b, n->kind == N_BREAK ? S("break") : S("continue")); if (n->name.len || n->c) bputc(b, ' '); if (n->c) print_slot(b, n->c); print_name(b, n->name); // a label the template declares is its own return; case N_DEFER: bput(b, S("defer ")); print_node(b, n->a, ind); return; case N_LABELED: bput(b, S("label ")); if (n->c) print_slot(b, n->c); else print_name(b, n->name); bput(b, S(" = ")); print_node(b, n->a, ind); return; case N_SWITCH: if (n->flags & F_MACRO) bput(b, S("macro ")); bput(b, S("switch ")); if (n->flags & F_REF) bputc(b, '&'); if (!(n->flags & F_IMPLICIT)) { bput(b, n->name); bput(b, S(" in ")); } print_node(b, n->a, ind); bput(b, S(" {\n")); for (iz i = 0; i < n->list.len; i++) { Node *cs = n->list.data[i]; fmt_before(b, cs->pos, ind, i > 0); print_indent(b, ind); if (cs->name.len) { bput(b, S("label ")); print_name(b, cs->name); bput(b, S(" = ")); } if (!cs->list.len) { bput(b, S("default:\n")); } else { bput(b, S("case ")); type_lists++; // the values of a case stay on its line print_list(b, &cs->list, ind); type_lists--; bput(b, S(":\n")); } Nodes *body = &cs->a->list; if (!body->len && fmt_file) fmt_rest_of_line(b, cs->pos, ind + 1); // default: # why NodeKind k = body->len == 1 ? body->data[0]->kind : N_NONE; b32 alone = body->len == 1 && !comment_before(body->data[0]->pos); if (alone && k != N_IF && k != N_FOR && k != N_WHILE && k != N_SWITCH && k != N_BLOCK && k != N_LABELED) { // one simple statement stays on the case line b->len--; bputc(b, ' '); print_node(b, body->data[0], ind + 1); bputc(b, '\n'); continue; } if (alone && k == N_BLOCK) { b->len--; // case X: { ... } bputc(b, ' '); print_block(b, body->data[0], ind); bputc(b, '\n'); continue; } for (iz j = 0; j < body->len;) { iz before = b->len; fmt_before(b, body->data[j]->pos, ind + 1, j > 0); print_indent(b, ind + 1); iz start = b->len; j += print_fit(b, body, j, ind + 1); if (printing_expanded && b->len == start) { // an empty macro label: no line b->len = before; continue; } bputc(b, '\n'); } } print_indent(b, ind); bputc(b, '}'); return; case N_MLABEL: if (printing_expanded) { // compile-time: only what is in it for (iz i = 0; n->a && i < n->a->list.len;) { if (i) { bputc(b, '\n'); print_indent(b, ind); } i += print_stmt_at(b, &n->a->list, i, ind); } return; } bput(b, S("macro label ")); bput(b, n->name); bput(b, S(" = ")); if (n->a && n->a->kind == N_DECLS) { Node blk = *n->a; blk.kind = N_BLOCK; print_block(b, &blk, ind); } else if (n->a) { print_block(b, n->a, ind); } return; case N_FN: if (n->flags & F_PRIVATE) bput(b, S("private ")); if (n->flags & F_COMMAND) bput(b, S("parenless(")); if (n->flags & F_COROUTINE) bput(b, S("coroutine(")); if (n->flags & F_MACRO) bput(b, S("macro ")); bput(b, S("fn ")); if (n->c) print_slot(b, n->c); else print_name(b, n->name); print_params(b, &n->list, ind); if (n->a) { bput(b, S(" -> ")); print_node(b, n->a, ind); } bputc(b, ' '); print_body(b, n->b, ind); if (n->flags & F_COMMAND) bputc(b, ')'); if (n->flags & F_COROUTINE) { bput(b, S(", ")); print_node(b, n->d, ind); bputc(b, ')'); } return; case N_STRUCT: case N_UNION: if (n->flags & F_PRIVATE) bput(b, S("private ")); if (n->flags & F_MACRO) bput(b, S("macro ")); if (n->flags & F_NONIL) bput(b, S("no_nil(")); bput(b, n->kind == N_STRUCT ? S("struct ") : S("union ")); if (n->c) print_slot(b, n->c); else bput(b, n->name); bputc(b, ' '); print_params(b, &n->list, ind); if (n->flags & F_NONIL) bputc(b, ')'); return; case N_MODULE: bput(b, S("module ")); print_params(b, &n->list, ind); return; case N_COLLECTION: bput(b, S("collection ")); bput(b, n->name); bput(b, S(" = ")); print_quoted(b, n->sval); return; case N_IMPORT: if (n->flags & F_MACRO) bput(b, S("macro ")); bput(b, S("import ")); print_quoted(b, n->sval); if (n->list.len) { bput(b, S(" (")); print_list(b, &n->list, ind); bputc(b, ')'); } if (n->name.len) { bput(b, S(" as ")); bput(b, n->name); } return; case N_DECLS: if (n->flags & F_GROUP) { // var a, b: Int as one macro argument print_stmt_at(b, &n->list, 0, ind); return; } if (n->flags & F_PRIVATE) bput(b, S("private ")); if (n->flags & F_MACRO) bput(b, S("macro ")); print_block(b, n, ind); return; default: bput(b, S("")); return; } } static Str node_source(Node *n) { Buf b = {0}; print_node(&b, n, 0); return bstr(&b); } // The name of t in a message that also names other: when they print alike // (Tag from two imports of one module), with the module arguments. static Str type_name_vs(Type *t, Type *other) { Str a = type_name(t); if (t == other || !equals(a, type_name(other)) || t->module < 0 || t->module >= modules.len) return a; Module *m = modules.data[t->module]; Buf b = {0}; bputf(&b, "%s from %s (", a, m->name); for (iz i = 0; i < m->args.len; i++) bputf(&b, "%s%s", i ? S(", ") : S(""), node_source(m->args.data[i])); bputc(&b, ')'); return bstr(&b); } // Checking typedef enum { CV_UNION, CV_ANY } ConvKind; typedef struct { ConvKind kind; iz path[16]; // variant indices from the outer union inward iz npath; } Conv; typedef enum { CALL_FN, // a declared function, or a builtin CALL_VALUE, // calling a function value CALL_CTOR, // constructing a struct CALL_ZERO, // T(): the starting value of a type CALL_DISPATCH, // case dispatch on one union argument } CallKind; typedef struct { CallKind kind; Fn *fn; Builtin builtin; Node **args; // in parameter order, defaults filled in iz nargs; Type *type; // the constructed type iz dispatch; // the argument dispatched on Fn **cases; // one per variant of the dispatched union, in variant order Node ***case_args; // the arguments of each case, converted for it Type *dispatch_union; Node *callee; // CALL_VALUE } Call; // --expand: a call made before a generated overload of its name existed // chose without it. In the expanded text every overload is written by hand // and seen everywhere, so the arguments it converted are written with the // type they went to (f(Any(1))), which only the chosen overload takes. static b32 print_chosen_call(Buf *b, Node *n, int ind) { Call *c = n->info; if (!c || c->kind != CALL_FN || !c->fn || c->fn->builtin || !c->fn->decl || !n->src || n->src->kind != N_CALL) return 0; Sym *set = scope_get(modules.data[c->fn->module]->scope, c->fn->name); b32 later = 0; for (iz i = 0; set && set->kind == S_FN && i < set->fns.len; i++) { Fn *g = set->fns.data[i]; later |= g != c->fn && g->decl && g->decl->pos.file >= 0 && files.data[g->decl->pos.file].generated; } if (!later) return 0; Fields *ps = &c->fn->type->fields; print_node(b, n->src->a, ind); bputc(b, '('); for (iz j = 0; j < c->nargs && j < ps->len; j++) { Node *a = c->args[j]; if (j) bput(b, S(", ")); if (!a) continue; if ((int)a->kind == X_CONV) { // converted: say to what bput(b, type_name(ps->data[j].type)); bputc(b, '('); print_node(b, a->a, ind); bputc(b, ')'); } else { print_node(b, a, ind); } } bputc(b, ')'); return 1; } // n becomes the checked call c static void make_call(Node *n, Call *c) { keep_source(n); n->kind = (NodeKind)X_CALLFN; n->info = c; } typedef struct { i32 kind; // FA_STRUCT, FA_UNION_ONE, FA_UNION_ALL iz index; // field index in the struct (or in the variant payloads) Type *base; // the struct type that owns the field iz *indices; // FA_UNION_ALL: the field index in each variant's payload } FieldAccess; enum { FA_STRUCT, FA_UNION_ONE, FA_UNION_ALL }; static STATE Fn *curfn = 0; static STATE Module *curmod = 0; // t belongs to another module than the one being checked static b32 type_outside(Type *t) { return curmod && t->module >= 0 && t->module < modules.len && modules.data[t->module] != curmod; } static STATE i32 curfile = 0; static STATE int loop_depth = 0; static STATE Scope *defer_scope = 0; // the scope of the deferred statement being checked static Node * wrap_conv(Node *n, Type *to, ConvKind k) { Node *c = mknode((NodeKind)X_CONV, n->pos); c->a = n; c->type = to; Conv *cv = new(&perm, 1, Conv); cv->kind = k; c->info = cv; return c; } // Finds the path of variant indices from union u to a variant of type t. static b32 union_path(Type *u, Type *t, Conv *cv, int depth) { if (depth >= 16) return 0; for (iz i = 0; i < u->fields.len; i++) { Field *f = u->fields.data + i; if (!f->type) continue; if (f->type == t) { cv->path[depth] = i; cv->npath = depth + 1; return 1; } } for (iz i = 0; i < u->fields.len; i++) { Field *f = u->fields.data + i; if (!f->type || !isunion(f->type)) continue; cv->path[depth] = i; if (union_path(f->type, t, cv, depth + 1)) return 1; } return 0; } static b32 is_literal_kind(Node *n) { return n->kind == N_INT || n->kind == N_FLOAT || n->kind == N_NIL || n->kind == N_ANON || n->kind == N_VARIANT || (n->kind == N_LIST); } // How a value of type from reaches type to: 0 exactly, 1 by conversion into a // listed union, 3 by conversion into Any, -1 not at all. static int conversion_rank(Type *from, Type *to) { if (from == to) return 0; if (from == t_error || to == t_error) return 0; if (from == t_nil) { if (to->kind == TY_REF || to->kind == TY_ANY || to->kind == TY_FN || to->kind == TY_TYPE) return 0; if (isunion(to) && !to->nonil) return 0; return -1; } if (isunion(to)) { resolve_named(to); if (union_paths(to, from, 0) == 1) return 1; } if (to->kind == TY_ANY && from->kind != TY_VOID) return 3; return -1; } // An integer literal, or one with a sign, used as an integer type t. The // lexer keeps literals up to 2^64 - 1 as their bits. static void literal_fits(Node *n, Type *t) { b32 neg = n->kind == N_UNARY && n->op == T_MINUS; u64 mag = (u64)(n->kind == N_INT ? n->ival : n->a->ival); u64 hi = t->issigned ? (t->bits == 64 ? (u64)INT64_MAX : ((u64)1 << (t->bits - 1)) - 1) : (t->bits == 64 ? UINT64_MAX : ((u64)1 << t->bits) - 1); if (neg && !t->issigned ? !mag : mag <= hi || (neg && mag == hi + 1)) return; errorat(n->pos, "%s%llu does not fit in %s, which holds %s%llu to %llu", neg ? S("-") : S(""), (unsigned long long)mag, type_name(t), t->issigned ? S("-") : S(""), (unsigned long long)(t->issigned ? hi + 1 : 0), (unsigned long long)hi); } // A field default that is the zero value anyway: done = false, n = 0, // s = "", xs = [], m = [:], r = nil, or a constructor of zero values. (A // parameter default is different: it makes the argument optional.) static b32 zero_value_node(Node *n) { switch ((int)n->kind) { case N_BOOL: return !n->ival; case N_INT: return !n->ival; case N_FLOAT: return n->fval == 0 && 1 / n->fval > 0; // not -0.0 case N_STR: return !n->sval.len; case N_LIST: return !n->list.len; case N_NIL: return 1; case N_UNARY: return n->op == T_MINUS && n->a->kind == N_INT && !n->a->ival; case X_CALLFN: { Call *c = n->info; if (c->kind == CALL_ZERO) return !c->type->has_defaults; if (c->kind != CALL_CTOR || c->type->has_defaults) return 0; for (iz i = 0; i < c->nargs; i++) { if (c->args[i] && !zero_value_node(c->args[i])) return 0; } return 1; } default: return 0; } } static void warn_zero_default(Node *param, Type *t) { Node *v = param->b; if (!v || !t || !zero_value_node(v)) return; Pos at = v->pos; // from the = sign Str text = files.data[at.file].text; i32 eq = at.lo; while (eq > 0 && text.data[eq - 1] == ' ') eq--; if (eq > 0 && text.data[eq - 1] == '=') at.lo = eq - 1; help_text = fmtstr("write %s: %s", param->name, type_name(t)); warnat(at, "%s = %s is already the zero value of %s", param->name, node_source(v), type_name(t)); } static void record_use(Str kind, Type *t, Pos at, int depth); static void generated_overloads(Node *d); static Type * coerce(Node **slot, Type *to, Str what) { Node *n = *slot; Type *from = n->type; if (!to || from == to || to == t_error || from == t_error) return from; if (from == t_nil) { if (conversion_rank(from, to) < 0) { if (isunion(to) && to->nonil) errorat(n->pos, "%s is a no_nil union, so it cannot be nil", type_name(to)); errorat(n->pos, "nil is not a %s", type_name(to)); } n->type = to; return to; } if (n->kind == N_INT && to->kind == TY_FLOAT) { n->kind = N_FLOAT; n->fval = (f64)n->ival; n->type = to; return to; } if ((n->kind == N_INT || (n->kind == N_UNARY && n->a->kind == N_INT)) && to->kind == TY_INT) { // a literal takes the type it is used as, if its value fits literal_fits(n, to); n->type = to; if (n->kind == N_UNARY) n->a->type = to; return to; } if (n->kind == N_UNARY && n->a->kind == N_INT && to->kind == TY_FLOAT) { n->a->kind = N_FLOAT; n->a->fval = (f64)n->a->ival; n->type = n->a->type = to; return to; } if (isunion(to)) { resolve_named(to); int paths = union_paths(to, from, 0); if (paths == 1) { Node *c = wrap_conv(n, to, CV_UNION); union_path(to, from, (Conv *)c->info, 0); *slot = c; return to; } if (paths > 1) errorat(n->pos, "%s fits %s in more than one way; name the variant", type_name(from), type_name(to)); } if (to->kind == TY_ANY && from->kind != TY_VOID) { record_use(S("print"), from, n->pos, 0); // an Any can be printed anywhere later record_use(S("any"), from, n->pos, 0); *slot = wrap_conv(n, to, CV_ANY); return to; } if (from->kind == TY_VOID) errorat(n->pos, "this has no value, but %s needs a %s", what, type_name(to)); if (to->kind == TY_VOID) errorat(n->pos, "%s goes with something that has no value", what); if (from->kind == TY_LIST && to->kind == TY_LIST && from->elem == t_u8 && to->elem == t_u8) help_text = fmtstr("convert it: %s(%s)", type_name(to), node_source(n)); errorat(n->pos, "%s needs a %s, but this is a %s", what, type_name_vs(to, from), type_name_vs(from, to)); } static Sym *builtin_sym(Str name); // A private name of an imported module: say so, not unknown. static void not_private(Str name, Pos p) { FileImports *fi = p.file >= 0 && p.file < fileimports.len ? fileimports.data + p.file : 0; for (iz i = 0; fi && i < fi->open.len; i++) { Sym *hid = scope_get(fi->open.data[i]->scope, name); if (hid && hid->isprivate) { help_text = (Str){0}; errorat(p, "%s is private to %s", name, fi->open.data[i]->name); } } } // One overload set of the functions of a, then those of b. static Sym * merge_fns(Str name, Pos pos, Sym *a, Sym *b) { Sym *merged = mksym(S_FN, name, pos); for (iz i = 0; i < a->fns.len; i++) *push(&perm, &merged->fns) = a->fns.data[i]; for (iz i = 0; i < b->fns.len; i++) *push(&perm, &merged->fns) = b->fns.data[i]; return merged; } static Sym * find_in_imports(i32 file, Str name) { if (file < 0 || file >= fileimports.len) return 0; FileImports *fi = fileimports.data + file; Sym *a = lookup(fi->aliases, name); if (a) return a; Sym *found = 0; for (iz i = 0; i < fi->open.len; i++) { Sym *s = scope_get(fi->open.data[i]->scope, name); if (s && !s->isprivate) { if (found && found != s && !(found->kind == S_FN && s->kind == S_FN)) { errorat((Pos){file, 0, 0, 0}, "%s is declared by two imported modules; import one with an alias", name); } // overloads from several modules merge into one set b32 overloads = found && found->kind == S_FN && s->kind == S_FN; found = overloads ? merge_fns(name, s->pos, found, s) : s; } } return found; } static Str closest_name(Scope *s, Str name); static Module *load_module(Str dir, Pos from, Node *imp, b32 single, Str single_file); static void resolve_types_of(Module *m); static STATE b32 checking_started = 0; static STATE Module *prelude_module = 0, *build_module = 0; static Map *prelude_names(void); static Map *prelude_order(void); // The prelude, the names every file sees without an import, is two std // modules, each loaded the first time a name of its own is looked up: // std:prelude (assert, reversed, unreachable; it imports std:code) and // std:build (Build, Cc, command and the fuzzing library). A program using // neither loads neither. part is 1 for std:prelude, 2 for std:build. static Module * prelude_part(iz part) { Module **m = part == 1 ? &prelude_module : &build_module; if (!*m) { static STATE b32 loading[3] = {0}; if (loading[part]) return 0; loading[part] = 1; Str dir = part == 1 ? S("std:prelude") : S("std:build"); Pos nowhere = {-1, 0, 0, 0}; Node imp = {0}; imp.kind = N_IMPORT; imp.sval = dir; *m = load_module(dir, nowhere, &imp, 1, dir); if (checking_started) { // loaded late: its types are resolved as check_program resolved // the others at its start Module *save = curmod; resolve_types_of(*m); curmod = save; } } return *m; } static Sym * prelude_sym(Str name) { iz part = (iz)lookup(prelude_names(), name); Module *m = part ? prelude_part(part) : 0; Sym *sym = m ? scope_get(m->scope, name) : 0; return sym && !sym->isprivate ? sym : 0; } // The alias of a module imported into file that declares name, if any static Str alias_with(i32 file, Str name) { if (file < 0 || file >= fileimports.len) return (Str){0}; Map *stack[256] = {0}; iz sp = 0; if (fileimports.data[file].aliases) stack[sp++] = fileimports.data[file].aliases; while (sp) { Map *m = stack[--sp]; Sym *a = m->value; Sym *found = a && a->kind == S_MODULE ? scope_get(a->mod->scope, name) : 0; if (found && !found->isprivate) return m->key; for (int k = 0; k < 4; k++) if (m->child[k] && sp < countof(stack)) stack[sp++] = m->child[k]; } return (Str){0}; } // A name a template of another module wrote: the caller's locals (code it // placed, bindings it was given), then that module, before the caller's // module and imports. static Sym * find_own(Scope *s, Str name, Pos p) { SourceFile *f = p.file >= 0 && p.file < files.len ? files.data + p.file : 0; if (!f || f->macro_module <= 0 || !f->own_names || f->macro_module - 1 == f->module || !lookup(f->own_names, fmtstr("%lld", (long long)p.offset))) return 0; for (Scope *sc = s; sc && sc->kind != SC_MODULE && sc->kind != SC_FILE; sc = sc->parent) { Sym *sym = scope_get(sc, name); if (sym) return sym; } return scope_get(modules.data[f->macro_module - 1]->scope, name); } static b32 own_name(Pos p) { SourceFile *f = p.file >= 0 && p.file < files.len ? files.data + p.file : 0; return f && f->own_names && lookup(f->own_names, fmtstr("%lld", (long long)p.offset)); } static STATE Module *main_module = 0; static b32 other_module_sym(Sym *s) { return s && s->module >= 0 && main_module && s->module != main_module->id; } // The alias a file imports the module of a name a module template wrote by, // for --expand, which writes the name through it static void own_prefix(Buf *b, Pos p, Str name, b32 type) { if (!printing_expanded || !own_name(p)) return; SourceFile *f = files.data + p.file; if (f->macro_module <= 0 || f->macro_module - 1 == f->module) return; Sym *d = scope_get(modules.data[f->macro_module - 1]->scope, name); if (!d || (type && d->kind != S_TYPE)) return; Str alias = alias_with(p.file, name); if (alias.len) { bput(b, alias); bputc(b, '.'); } } static Sym * find(Scope *s, Str name, Pos p, b32 must) { Sym *own = find_own(s, name, p); if (own) return own; for (Scope *sc = s; sc; sc = sc->parent) { Sym *sym = scope_get(sc, name); if (sym) { if (sc->kind == SC_MODULE) { // plain imports may add overloads to a module function Sym *more = find_in_imports(s->file, name); if (more && more->kind == S_FN && sym->kind == S_FN) { Sym *merged = merge_fns(name, sym->pos, sym, more); merged->module = sym->module; return merged; } } return sym; } if (sc->kind == SC_MODULE) { Sym *imp = find_in_imports(s->file, name); if (imp) return imp; } } // code a macro made may use the private names of the macro's module i32 mm = p.file >= 0 && p.file < files.len ? files.data[p.file].macro_module : 0; if (mm > 0) { Sym *own = scope_get(modules.data[mm - 1]->scope, name); if (own) return own; } Sym *b = builtin_sym(name); if (b) return b; Sym *pre = prelude_sym(name); if (pre) return pre; if (equals(name, S("stdout")) || equals(name, S("stderr"))) { // the two streams, as constants every file sees Sym *v = mksym(S_VAR, name, p); v->type = t_file; v->isglobal = v->isconst = 1; v->cname = equals(name, S("stdout")) ? S("s99_stdout_file") : S("s99_stderr_file"); return v; } if (!must) return 0; Str near = closest_name(s, name); if (near.len) help_text = fmtstr("did you mean %s?", near); Str alias = s ? alias_with(s->file, name) : (Str){0}; if (alias.len) help_text = fmtstr("it is in module %s: write %s.%s", alias, alias, name); not_private(name, p); for (Scope *sc = s; sc && sc->kind != SC_MODULE; sc = sc->parent) { for (iz i = 0; sc->block && i < sc->block->list.len; i++) { Node *d = sc->block->list.data[i]; // declared further down if ((d->kind == N_FN || d->kind == N_VAR) && equals(d->name, name) && d->pos.file == p.file && d->pos.offset > p.offset) { notes.len = 0; note(d->pos, S("declared here")); help_text = S("inside a function, declare it before its first use"); errorat(p, "%s is declared later in this block", name); } } } errorat(p, "unknown name %s", name); } // The function a scope belongs to, or 0 at file scope. static Fn * scope_fn(Scope *s) { for (; s; s = s->parent) { if (s->kind == SC_FN) return s->fn; } return 0; } static Type *check_call(Node *n, Scope *s, Type *want); static b32 parenless_set(Sym *sym, Pos at); static Type *check_template(Node *n, Scope *s, Type *want); static b32 expand_stmt_at(Node *blk, iz *i, Scope *s); // Replaces count statements of blk at i with those of with. static void replace_stmts(Node *blk, iz i, iz count, Nodes *with) { Nodes out = {0}; for (iz k = 0; k < i; k++) addnode(&out, blk->list.data[k]); for (iz k = 0; k < with->len; k++) addnode(&out, with->data[k]); for (iz k = i + count; k < blk->list.len; k++) addnode(&out, blk->list.data[k]); blk->list = out; } // Where st is in blk, or -1. static iz position_in(Node *blk, Node *st) { for (iz i = 0; i < blk->list.len; i++) if (blk->list.data[i] == st) return i; return -1; } // Where st is in blk, which holds it. static iz index_in(Node *blk, Node *st) { iz i = position_in(blk, st); assert(i >= 0); return i; } static Type *code_type(Str name); static Sym *code_sym(Str name); static void need_code_module(Pos at); static Node *name_node(Sym *sym, Pos p); static Node *call_node(Node *callee, Pos p); static Type *check_block_value(Node *b, Scope *s, Type *want, b32 needvalue); static void check_stmt(Node *n, Scope *s); static void check_fn(Fn *f); static b32 param_escapes(Fn *f, iz i); // A call to a function whose result is inferred checks it first. In the // in-order pass that is a detour: the body is checked before its place. static STATE b32 in_order = 0; // the in-order pass of check_program is running static STATE long long fresh_base = 0; // fresh() names start past it (name___N) // macro labels being checked, innermost last: place() can put code into them typedef struct { Node *region; Scope *scope; // the scope it is in; 0 at file scope } MLabel; static STATE struct { MLabel *data; iz len; iz cap; } mlabels = {0}; static b32 part_name(Str s); static STATE Fn *detour_fn = 0; static STATE Fn *at_place = 0; // the function the in-order pass is checking where it is static STATE Pos detour_at = {0}; static STATE Pos need_at = {-1, 0, 0, 0}; // the call being resolved, which may need a result type static b32 is_file_macro(Node *d); static STATE Map *decls_done = 0; // the in-order pass has handled these // A file-scope macro call that has not run yet and comes before decl: an // early check of decl would see a program without what it makes. static Node * macro_before(Nodes *list, Node *decl, b32 *found) { Node *first = 0; for (iz i = 0; i < list->len && !*found; i++) { Node *d = list->data[i]; if (d == decl) { *found = 1; break; } if (!first && is_file_macro(d) && !lookup(decls_done, ptrkey(d))) first = d; Node *inner = d->kind == N_DECLS ? macro_before(&d->list, decl, found) : d->kind == N_MLABEL ? macro_before(&d->a->list, decl, found) : 0; if (!first) first = inner; } return first; } static Node * pending_macro_before(Fn *f) { if (!in_order || !f->decl || f->module < 0 || f->module >= modules.len) return 0; b32 found = 0; Node *m = macro_before(&modules.data[f->module]->decls, f->decl, &found); return found ? m : 0; } // f is checked before its place: not when a macro call still to run comes // first (it could make what the body uses) static STATE b32 program_checked = 0; // checking has ended: nothing more can be declared static STATE b32 question_detour = 0; // the detour is for a question about the program static void early_check_allowed(Fn *f, Pos at) { Node *m = pending_macro_before(f); if (!m) return; notes.len = 0; note(m->pos, S("this macro call runs first")); if (question_detour) { stop_metaprogram(); help_text = S("ask after that macro call, or in build(b)"); errorat(at, "%s is checked later, and a macro call comes before it", f->name); } help_text = f->co ? fmtstr("declare coroutine %s before the code that uses its fields", f->name) : fmtstr("write -> Type on %s, so the call does not need its body", f->name); errorat(at, "%s is used before its body, and a macro call comes before its body", f->name); } // A macro about to run: not in a body checked early for its result type static void macro_runs_here(void) { if (!detour_fn) return; Fn *f = detour_fn; detour_fn = 0; notes.len = 0; if (question_detour) { note(f->decl->pos, S("its body")); stop_metaprogram(); help_text = S("ask after it, or in build(b)"); errorat(detour_at, "%s is checked later, and its body runs macros", f->name); } help_text = f->co ? fmtstr("declare coroutine %s before the code that uses its fields", f->name) : fmtstr("write -> Type on %s, so the call does not need its body", f->name); Fn *in = curfn; while (in && (in->isclosure || in->isnested) && in->parent) in = in->parent; if (in && in != f && in->decl && in->name.len) { // its body calls one whose body does note(in->decl->pos, fmtstr("the body of %s, which runs them", in->name)); errorat(detour_at, "%s is used before its body, and its body needs %s, whose body runs macros", f->name, in->name); } note(f->decl->pos, S("its body")); errorat(detour_at, "%s is used before its body, and its body runs macros", f->name); } // Checks f now. In the in-order pass, before its place, that is a detour: // its body may not run macros then. static void check_now(Fn *f, Pos at, b32 may_detour) { if (!may_detour || !in_order || detour_fn || f == at_place) { check_fn(f); return; } early_check_allowed(f, at); detour_fn = f; detour_at = at; check_fn(f); detour_fn = 0; } // A call to a function whose result is inferred checks it first. static void check_inferred(Fn *f, Pos at) { if (f->checking && !f->type->elem) { // a calls b calls a: say which one needs its result written notes.len = 0; note(f->decl->pos, fmtstr("%s is declared here without a result type", f->name)); help_text = fmtstr("write the result type of %s after its parameters: fn %s(...) -> T", f->name, f->name); errorat(at, "this call to %s needs its result before it is known", f->name); } check_now(f, at, !f->checked); } static Type *check_fnlit(Node *n, Scope *s, Type *want); static Type *check_switch(Node *n, Scope *s, Type *want, b32 asvalue); static Type * check_value(Node **slot, Scope *s, Type *want, Str what) { check_expr(*slot, s, want); if (want) coerce(slot, want, what); return (*slot)->type; } static b32 add_ok(i64 a, i64 b, i64 *r); static b32 mul_ok(i64 a, i64 b, i64 *r); static STATE b32 const_overflowed = 0; // const_int met arithmetic that does not fit in an Int static b32 const_int(Node *n, Scope *s, i64 *out) { switch (n->kind) { case N_INT: *out = n->ival; return 1; case N_UNARY: if (n->op == T_MINUS && const_int(n->a, s, out)) { *out = (i64)(0 - (u64)*out); return 1; } return 0; case N_BINARY: { i64 a = 0, b = 0; if (!const_int(n->a, s, &a) || !const_int(n->b, s, &b)) return 0; // what overflows is not a constant here: the program reports it, // or no_const_overflow where a constant is needed b32 ok = 1; switch (n->op) { case T_PLUS: ok = add_ok(a, b, out); break; case T_MINUS: ok = b != INT64_MIN && add_ok(a, -b, out); break; case T_STAR: ok = mul_ok(a, b, out); break; case T_STARSTAR: { if (b < 0) return 0; i64 r = 1; for (i64 k = 0; k < b; k++) { if (!mul_ok(r, a, &r)) { const_overflowed = 1; return 0; } if (r == 0 || r == 1) break; // 0 and 1 stay if (r == -1) { r = (b - k - 1) % 2 ? 1 : -1; break; } } *out = r; return 1; } case T_SLASH: case T_PERCENT: if (!b || (a == INT64_MIN && b == -1)) return 0; *out = n->op == T_SLASH ? a / b : a % b; return 1; default: return 0; } if (!ok) const_overflowed = 1; return ok; } case N_NAME: { Sym *sym = find(s, n->name, n->pos, 0); if (sym && sym->kind == S_VAR && sym->isconst && sym->isglobal && sym->decl && sym->decl->b && !sym->resolving) { sym->resolving = 1; b32 ok = const_int(sym->decl->b, s, out); sym->resolving = 0; return ok; } return 0; } default: return 0; } } // Where a constant is needed: arithmetic that does not fit in an Int is // that error, not a missing constant. static void no_const_overflow(Node *n, Scope *s) { i64 v = 0; const_overflowed = 0; if (!const_int(n, s, &v) && const_overflowed) errorat(n->pos, "%s overflows: it does not fit in an Int", node_source(n)); } // A constant is a literal, constant arithmetic, a named global constant, or // a struct, union or list literal of constants. Defaults and global // initializers must be constants. static b32 is_constant(Node *n, Scope *s) { switch ((int)n->kind) { case N_INT: case N_FLOAT: case N_STR: case N_BOOL: case N_NIL: return 1; case N_UNARY: return is_constant(n->a, s); case N_BINARY: return is_constant(n->a, s) && is_constant(n->b, s); case N_NAME: { Sym *sym = find(s, n->name, n->pos, 0); return sym && sym->kind == S_VAR && sym->isconst && sym->isglobal; } case N_LIST: case N_ANON: case N_VARIANT: for (iz i = 0; i < n->list.len; i++) { if (!is_constant(n->list.data[i], s)) return 0; } return 1; case N_PAIR: return is_constant(n->a, s) && is_constant(n->b, s); case N_NAMED: return is_constant(n->a, s); case N_CALL: if (n->a->kind == N_TYPENAME || (n->a->kind == N_FIELD && n->a->a->kind == N_TYPENAME)) { for (iz i = 0; i < n->list.len; i++) { if (!is_constant(n->list.data[i], s)) return 0; } return 1; } return 0; case N_FIELD: return n->a->kind == N_TYPENAME; case X_CONV: return is_constant(n->a, s); default: return 0; } } // Lvalues static STATE b32 addr_in_args = 0; // checking call arguments: & does not pin yet // Whether an expression names a place an assignment or & may change. enum { NOT_PLACE, PLACE, READONLY_PLACE }; static int place_kind(Node *n) { switch ((int)n->kind) { case N_NAME: if (!n->sym || n->sym->kind != S_VAR) return NOT_PLACE; return n->sym->readonly || n->sym->isconst ? READONLY_PLACE : PLACE; case N_FIELD: { FieldAccess *fa = n->info; if (fa && fa->kind == FA_UNION_ALL) return NOT_PLACE; return n->a->kind == N_DEREF ? PLACE : place_kind(n->a); } case N_INDEX: return n->a->kind == N_DEREF ? PLACE : place_kind(n->a); case N_DEREF: return PLACE; default: return NOT_PLACE; } } // The variable, reference or value a chain of fields and elements starts at. static Node * root_of(Node *n) { while (n->kind == N_FIELD || n->kind == N_INDEX) n = n->a; return n; } // Marks the local variable at the root of an lvalue as reached by a stored // reference, so it lives in a heap cell instead of the stack frame. static void pin_root(Node *n) { for (;;) { switch (n->kind) { case N_FIELD: case N_INDEX: n = n->a; continue; case N_NAME: if (n->sym && n->sym->kind == S_VAR && !n->sym->isglobal && !n->sym->isvar) { if (!n->sym->refpinned) n->sym->pin_site = n->pos; // locals(f): what keeps it n->sym->pinned = n->sym->refpinned = 1; } return; default: return; } } } static void capture(Sym *sym, Scope *s) { // Every function between the use and the owner captures the variable. for (Scope *sc = s; sc; sc = sc->parent) { if (sc->kind != SC_FN) continue; Fn *f = sc->fn; if (!f || f == sym->owner) return; b32 have = 0; for (iz i = 0; i < f->captures.len; i++) have |= f->captures.data[i] == sym; if (!have) *push(&perm, &f->captures) = sym; sym->iscaptured = 1; } } static Type * anon_union(Type **types, iz n) { Type *t = mktype(TY_UNION); for (iz i = 0; i < n; i++) { b32 dup = 0; for (iz j = 0; j < t->fields.len; j++) dup |= t->fields.data[j].type == types[i]; if (dup) continue; Field f = {0}; f.type = types[i]; *push(&perm, &t->fields) = f; } if (t->fields.len == 1) return t->fields.data[0].type; t->resolved = 1; Type *r = intern(t); compute_flags(r, 0); return r; } static Type * deref_auto(Node **slot) { // Field access goes through references automatically. Node *n = *slot; while (n->type->kind == TY_REF) { Node *d = mknode(N_DEREF, n->pos); d->a = n; d->type = n->type->elem; d->flags |= F_IMPLICIT; *slot = d; n = d; } return n->type; } static Fn *single_fn(Sym *sym, Pos p); static void check_global(Sym *g); static Type * check_field(Node *n, Scope *s) { Node *base = n->a; if (base->kind == N_NAME) { Sym *sym = find(s, base->name, base->pos, 1); if (sym->kind == S_MODULE) { Sym *m = scope_get(sym->mod->scope, n->name); if (!m) no_module_member(n->pos, base->name, sym->mod->scope, n->name, S("")); if (m->isprivate) errorat(n->pos, "%s is private to %s", n->name, sym->mod->name); keep_source(n); n->kind = N_NAME; n->a = 0; n->sym = m; if (m->kind == S_VAR) { if (!m->type) check_global(m); if (m->ismacro && !in_macro_code) errorat(n->pos, "%s exists only at compile time", n->name); n->type = m->type; return n->type; } if (m->kind == S_FN) { Fn *f = single_fn(m, n->pos); n->type = fn_value(f->type); return n->type; } errorat(n->pos, "%s is a type, not a value", n->name); } } if (base->kind == N_TYPENAME) { // Shape.dot: a variant of a union type, written with its type Node tn_ = {0}; tn_.kind = N_TNAME; tn_.name = base->name; tn_.pos = base->pos; Type *u = resolve_type(&tn_, s); if (!isunion(u)) errorat(base->pos, "%s is not a union", type_name(u)); resolve_named(u); iz vi = variant_index(u, n->name); if (vi < 0) no_member(n->pos, u, n->name); keep_source(n); n->kind = N_VARIANT; n->a = 0; n->ival = vi; n->type = u; Field *f = u->fields.data + vi; if (f->type && f->type != t_void) errorat(n->pos, "%s.%s carries a %s: write %s.%s(value)", type_name(u), n->name, type_name(f->type), type_name(u), n->name); return u; } check_expr(base, s, 0); Type *bt = deref_auto(&n->a); resolve_named(bt); FieldAccess *fa = new(&perm, 1, FieldAccess); n->info = fa; if (bt->kind == TY_STRUCT) { for (iz i = 0; i < bt->fields.len; i++) { Field *f = bt->fields.data + i; if (equals(f->name, n->name)) { if (f->isprivate && type_outside(bt)) errorat(n->pos, "%s.%s is private to %s", type_name(bt), n->name, modules.data[bt->module]->name); fa->kind = FA_STRUCT; fa->index = i; fa->base = bt; n->type = f->type; return n->type; } } no_member(n->pos, bt, n->name); } if (bt->kind == TY_UNION) { // Through a one-variant union, or a field every variant has. iz count = bt->fields.len; Type **types = new(&perm, count, Type *); fa->indices = new(&perm, count, iz); for (iz v = 0; v < count; v++) { Type *pt = bt->fields.data[v].type; if (pt) resolve_named(pt); iz found = -1; if (pt && pt->kind == TY_STRUCT) { for (iz i = 0; i < pt->fields.len; i++) { if (equals(pt->fields.data[i].name, n->name)) found = i; } } if (found < 0) { if (bt->fields.len == 1) no_member(n->pos, pt ? pt : t_void, n->name); Node *src = n->a; if (src->kind == N_NAME) help_text = fmtstr( "switch on it to reach the variants that have it: switch &%s { case ... }", src->name); errorat(n->pos, "not every variant of %s has a field %s (%s does not)", type_name(bt), n->name, variant_name(bt, v)); } fa->indices[v] = found; types[v] = pt->fields.data[found].type; } fa->kind = count == 1 ? FA_UNION_ONE : FA_UNION_ALL; fa->base = bt; n->type = anon_union(types, count); return n->type; } Sym *fn = find(s, n->name, n->pos, 0); if (fn && fn->kind == S_FN) help_text = fmtstr("Star99 has no methods; write %s(%s)", n->name, node_source(n->a)); errorat(n->pos, "a %s has no fields", type_name(bt)); } static Type * check_index(Node *n, Scope *s) { check_expr(n->a, s, 0); Type *bt = deref_auto(&n->a); if (n->list.len > 1) { // arr[1, 2] is arr[1][2] Node *inner = n->a; for (iz i = 0; i < n->list.len - 1; i++) { Node *ix = mknode(N_INDEX, n->pos); ix->a = inner; addnode(&ix->list, n->list.data[i]); inner = ix; } n->a = inner; Node *last = n->list.data[n->list.len - 1]; n->list = (Nodes){0}; addnode(&n->list, last); check_index(n->a, s); bt = n->a->type; } switch (bt->kind) { case TY_LIST: case TY_ARRAY: if (n->list.data[0]->kind == N_RANGE) errorat( n->list.data[0]->pos, "lists have no slices; copy the part you need with a loop: for i in a..list.data[0], s, t_int, S("an index")); n->type = bt->elem; return n->type; case TY_MAP: check_value(&n->list.data[0], s, bt->key, S("a key")); n->type = bt->elem; return n->type; default: errorat(n->pos, "a %s cannot be indexed", type_name(bt)); } } static Fn * find_operator(Scope *s, Str op, Type *a, Type *b) { Sym *sym = find(s, op, (Pos){-1, 0, 0, 0}, 0); if (!sym || sym->kind != S_FN) return 0; for (iz i = 0; i < sym->fns.len; i++) { Fn *f = sym->fns.data[i]; if (!f->type) check_fn(f); Fields *ps = &f->type->fields; if (b && ps->len == 2 && ps->data[0].type == a && ps->data[1].type == b) return f; if (!b && ps->len == 1 && ps->data[0].type == a) return f; } return 0; } // For an operator with one operand of type known: the type an overload // wants for the other operand (side 1: the right one), so a literal or a // variable of a built-in type checks as that, not as the known type. static Type * operator_want(Scope *s, TokKind op, Type *known, int side) { static Str names[] = {[T_PLUS] = SL("+"), [T_MINUS] = SL("-"), [T_STAR] = SL("*"), [T_SLASH] = SL("/"), [T_PERCENT] = SL("%"), [T_STARSTAR] = SL("**")}; if (op >= countof(names) || !names[op].len || !known) return 0; Sym *sym = find(s, names[op], (Pos){-1, 0, 0, 0}, 0); if (!sym || sym->kind != S_FN) return 0; for (iz i = 0; i < sym->fns.len; i++) { Fn *f = sym->fns.data[i]; if (!f->type) check_fn(f); Fields *ps = &f->type->fields; if (ps->len != 2) continue; Type *mine = ps->data[side ? 0 : 1].type, *other = ps->data[side].type; b32 user = mine->kind == TY_STRUCT || mine->kind == TY_UNION || other->kind == TY_STRUCT || other->kind == TY_UNION; if (user && mine == known && other != known && (known->kind == TY_STRUCT || known->kind == TY_UNION)) return other; } return 0; } static void finish_args(Fn *f, Node **bound, Scope *s); static void not_a_coroutine_call(Fn *f, Pos at) { if (!f->co || f != f->co->fn) return; help_text = fmtstr("start one with var c: Coroutine(%s) = .(args: .(...)), then resume(&c)", f->name); errorat(at, "%s is a coroutine: resume runs it, so it is not called directly", f->name); } // n becomes a call of f with its arguments bound to the parameters; with a // scope, they are checked against the parameter types first static Type * call_fn(Node *n, Fn *f, Node **bound, Scope *s) { not_a_coroutine_call(f, n->pos); if (s) finish_args(f, bound, s); Call *c = new(&perm, 1, Call); c->kind = CALL_FN; c->fn = f; c->args = bound; c->nargs = f->type->fields.len; make_call(n, c); f->used = 1; f->ctused |= in_macro_code; if (f->infer && !f->checked) check_inferred(f, n->pos); n->type = f->type->elem; return n->type; } static Type * operator_call(Node *n, Fn *f, Node *a, Node *b) { Node **args = new(&perm, 2, Node *); args[0] = a; args[1] = b; return call_fn(n, f, args, 0); } static b32 diverges(Node *n); static void fn_signature(Fn *f); static Type *resolve_overloads(Node *n, Sym *sym, Scope *s, Type *want); // A number literal, or arithmetic of them (2 * 3 + 1): it takes the type it // is used as, as a single literal does. static b32 literal_arith(Node *n) { if (n->kind == N_INT) return !(n->flags & F_CHAR); if (n->kind == N_FLOAT) return 1; if (n->kind == N_UNARY) return n->op == T_MINUS && literal_arith(n->a); if (n->kind != N_BINARY) return 0; TokKind op = n->op; return (op == T_PLUS || op == T_MINUS || op == T_STAR || op == T_SLASH || op == T_PERCENT || op == T_STARSTAR) && literal_arith(n->a) && literal_arith(n->b); } static Type * check_binary(Node *n, Scope *s, Type *want) { TokKind op = n->op; if (op == T_AND || op == T_OR) { check_value(&n->a, s, t_bool, tokname[op]); check_value(&n->b, s, t_bool, tokname[op]); n->type = t_bool; return t_bool; } b32 arith = op == T_PLUS || op == T_MINUS || op == T_STAR || op == T_SLASH || op == T_PERCENT || op == T_STARSTAR; if (arith && !is_literal_kind(n->a) && !literal_arith(n->a)) { // v + 1 for a union v with an operator per variant: case dispatch, as // the call +(v, 1) would Type *lt = check_expr(n->a, s, 0); Sym *sym = isunion(lt) ? find(s, opname(op), n->pos, 0) : 0; b32 cases = 0; for (iz i = 0; sym && sym->kind == S_FN && i < sym->fns.len; i++) { Fn *f = sym->fns.data[i]; if (f->builtin) continue; fn_signature(f); for (iz v = 0; f->type->fields.len == 2 && v < lt->fields.len; v++) cases |= lt->fields.data[v].type == f->type->fields.data[0].type; } if (cases && !operator_want(s, op, lt, 1)) { Node *call = call_node(name_node(sym, n->pos), n->pos); addnode(&call->list, n->a); addnode(&call->list, n->b); resolve_overloads(call, sym, s, want); replace_node(n, call); return n->type; } } // A literal takes the type of the other side. Type *hint = arith && want && isnumeric(want) ? want : 0; if ((is_literal_kind(n->a) || literal_arith(n->a)) && !is_literal_kind(n->b) && !literal_arith(n->b)) { check_expr(n->b, s, hint); // 2 * x with fn *(a: Int, b: Symbol): the literal is the overload's Int Type *ow = arith ? operator_want(s, op, n->b->type, 0) : 0; check_value(&n->a, s, ow ? ow : n->b->type, S("the left side")); } else { check_expr(n->a, s, hint); Node *b = n->b; b32 simple = b->kind == N_NAME || b->kind == N_FIELD || b->kind == N_INDEX || b->kind == N_CALL; if (arith && simple && n->a->type != t_nil) { // checked on its own first: an operator of a struct or union may // take it as it is (two * x with fn *(a: Int, b: Symbol)) check_expr(n->b, s, 0); Type *bt = n->b->type, *at = n->a->type; b32 user = at->kind == TY_STRUCT || at->kind == TY_UNION || bt->kind == TY_STRUCT || bt->kind == TY_UNION; if (!(user && bt != at)) coerce(&n->b, at, S("the right side")); } else { Type *ow = arith ? operator_want(s, op, n->a->type, 1) : 0; Type *want = ow ? ow : n->a->type == t_nil ? 0 : n->a->type; check_expr(n->b, s, want); if (op == T_PLUS && n->a->type->text) help_text = S("to join text with other values, write str(a, b)"); if (want) coerce(&n->b, want, S("the right side")); help_text = (Str){0}; if (n->a->type == t_nil) coerce(&n->a, n->b->type, S("the left side")); } } Type *lt = n->a->type, *rt = n->b->type; if (op == T_EQEQ || op == T_NE) { // compares every part, with its == if it has one record_use(S("op=="), lt, n->pos, 0); record_use(S("op=="), rt, n->pos, 0); } if (lt->kind == TY_STRUCT || lt->kind == TY_UNION || rt->kind == TY_STRUCT || rt->kind == TY_UNION) { static Str names[] = {[T_PLUS] = SL("+"), [T_MINUS] = SL("-"), [T_STAR] = SL("*"), [T_SLASH] = SL("/"), [T_PERCENT] = SL("%"), [T_EQEQ] = SL("=="), [T_NE] = SL("=="), [T_LT] = SL("<"), [T_STARSTAR] = SL("**"), [T_GT] = SL("<"), [T_LE] = SL("<"), [T_GE] = SL("<")}; if (op < countof(names) && names[op].len) { Str k = fmtstr("op%s", names[op]); // an operator a macro adds later record_use(k, lt, n->pos, 16); // the type itself, not its parts record_use(k, rt, n->pos, 16); b32 swap = op == T_GT || op == T_LE; Fn *f = swap ? find_operator(s, names[op], rt, lt) : find_operator(s, names[op], lt, rt); if (f) { if (op == T_NE || op == T_LE || op == T_GE) { // a != b is not (a == b); a <= b is not (b < a) keep_source(n); Node *inner = mknode(N_BINARY, n->pos); *inner = *n; inner->src = 0; operator_call(inner, f, swap ? n->b : n->a, swap ? n->a : n->b); n->kind = N_UNARY; n->op = T_NOT; n->a = inner; n->b = 0; n->type = t_bool; return t_bool; } return operator_call(n, f, swap ? n->b : n->a, swap ? n->a : n->b); } } } if (lt != rt && lt != t_nil && rt != t_nil) errorat(n->pos, "both sides of %s must have the same type, but they are %s and %s", opname(op), type_name(lt), type_name(rt)); if (arith) { if (!isnumeric(lt)) errorat(n->pos, "%s needs numbers, not %s", opname(op), type_name(lt)); if (op == T_PERCENT && lt->kind != TY_INT) errorat(n->pos, "%% needs integers"); if (op == T_STARSTAR && lt->kind != TY_INT) errorat(n->pos, "** works only on integers for now, not %s", type_name(lt)); n->type = lt; return lt; } if (op == T_LT || op == T_GT || op == T_LE || op == T_GE) { if (!isnumeric(lt) && !lt->text) errorat(n->pos, "%s compares numbers or strings, not %s", opname(op), type_name(lt)); n->type = t_bool; return t_bool; } if (op == T_EQEQ || op == T_NE) { if (lt->kind == TY_FN) errorat(n->pos, "functions cannot be compared"); n->type = t_bool; return t_bool; } errorat(n->pos, "%s does not apply to a %s", tokname[op], type_name(lt)); } static Type * check_in(Node *n, Scope *s) { check_expr(n->b, s, 0); Type *ct = n->b->type; if (n->b->kind == N_RANGE) { check_value(&n->a, s, t_int, S("in")); } else if (ct->text) { // a substring, or a byte: 'a' in "cat" Type *at = check_expr(n->a, s, t_string); if (at != t_u8) coerce(&n->a, t_string, S("in")); } else { switch (ct->kind) { case TY_LIST: case TY_ARRAY: check_value(&n->a, s, ct->elem, S("in")); record_use(S("op=="), ct->elem, n->pos, 0); // compared with == in turn break; case TY_MAP: check_value(&n->a, s, ct->key, S("in")); break; case TY_POOL: check_value(&n->a, s, ref_of(ct->elem), S("in")); break; default: { check_expr(n->a, s, 0); record_use(S("opin"), n->a->type, n->pos, 16); // the type itself, not its parts record_use(S("opin"), ct, n->pos, 16); // the type itself, not its parts Fn *f = find_operator(s, S("in"), n->a->type, ct); if (!f) errorat(n->pos, "in needs a list, map, pool, string or range, not %s", type_name(ct)); Node *a = n->a, *b = n->b; if (n->flags & F_NOT) { keep_source(n); Node *inner = mknode(N_IN, n->pos); operator_call(inner, f, a, b); n->kind = N_UNARY; n->op = T_NOT; n->a = inner; n->b = 0; n->type = t_bool; return t_bool; } return operator_call(n, f, a, b); } } } n->type = t_bool; return t_bool; } static Type * check_list_literal(Node *n, Scope *s, Type *want) { b32 ismap = (n->list.len && n->list.data[0]->kind == N_PAIR) || (n->flags & F_MAP); if (want && want->kind == TY_MAP) ismap = 1; if ((n->flags & F_MAP) && want && want->kind != TY_MAP && want->kind != TY_ANY) errorat(n->pos, "[:] is an empty map, but this needs a %s", type_name(want)); if (ismap) { Type *mt = want && want->kind == TY_MAP ? want : 0; for (iz i = 0; i < n->list.len; i++) { Node *pr = n->list.data[i]; if (pr->kind != N_PAIR) errorat(pr->pos, "every entry of a map literal is key: value"); check_value(&pr->a, s, mt ? mt->key : 0, S("a key")); check_value(&pr->b, s, mt ? mt->elem : 0, S("a value")); if (!mt) { if (pr->a->type == t_nil || pr->b->type == t_nil) errorat(pr->pos, "the map type is unknown; write var m: Map(K, V) = ..."); mt = map_of(pr->a->type, pr->b->type); } } if (!mt) errorat(n->pos, "an empty map needs a type; write var m: Map(K, V) = [:]"); n->type = mt; return mt; } Type *et = 0; if (want && (want->kind == TY_LIST || want->kind == TY_ARRAY)) et = want->elem; for (iz i = 0; i < n->list.len; i++) { if (n->list.data[i]->kind == N_PAIR) errorat(n->list.data[i]->pos, "a list literal has no keys"); if (i && !want && et == t_int && n->list.data[0]->kind == N_INT && n->list.data[i]->kind == N_FLOAT) help_text = fmtstr("write %lld.0", (long long)n->list.data[0]->ival); // [1, 2.5] check_value(&n->list.data[i], s, et, S("a list element")); help_text = (Str){0}; if (!et) { et = n->list.data[i]->type; if (et == t_nil) errorat(n->list.data[i]->pos, "the element type is unknown; write var x: List(T) = ..."); } } if (!et) { if (want && want->kind == TY_ANY) errorat(n->pos, "an empty list in an Any needs a type"); errorat(n->pos, "an empty list needs a type; write var x: List(T) = []"); } if (want && want->kind == TY_ARRAY) { if (n->list.len != want->count) errorat(n->pos, "%s needs %lld elements, not %lld", type_name(want), (long long)want->count, (long long)n->list.len); n->type = want; return want; } n->type = list_of(et); return n->type; } // Matches arguments to fields or parameters: positional first, then named, // then defaults. With a name for what is called, a mismatch is an error; // without, it gives 0. static STATE Str bind_why = {0}, bind_help = {0}; // why the last bind_fields failed, and a fix static STATE Pos bind_at = {0}; // the argument it is about static STATE b32 bind_quiet = 0; // a mismatch needs no reason: choosing an overload static Node ** bind_fields(Fields *fields, Nodes *args, Node *at, Str what, b32 is_struct) { // without at, a mismatch returns 0 and leaves its reason in bind_why bind_why = bind_help = (Str){0}; Node **out = new(&perm, fields->len + 1, Node *); iz pos = 0; b32 named = 0; Pos where = at ? at->pos : (Pos){-1, 0, 0, 0}; for (iz i = 0; i < args->len && !bind_why.len; i++) { Node *a = args->data[i]; where = a->pos; if (a->kind == N_NAMED) { named = 1; iz fi = -1; for (iz j = 0; j < fields->len; j++) { if (equals(fields->data[j].name, a->name)) fi = j; } if (fi < 0 && bind_quiet) { bind_why = S("no such name"); } else if (fi < 0) { near_start(a->name); for (iz j = 0; j < fields->len; j++) near_offer(fields->data[j].name); Buf all = {0}; for (iz j = 0; j < fields->len; j++) bputf(&all, "%s%s", j ? S(", ") : S(""), fields->data[j].name); bind_help = near_best.len ? fmtstr("did you mean %s?", near_best) : all.len ? fmtstr("the %s of %s: %s", is_struct ? S("fields") : S("parameters"), what, bstr(&all)) : (Str){0}; // every name that is not one of them Str *bad = new(&perm, args->len, Str); iz nbad = 0; for (iz k = i; k < args->len; k++) { Node *b = args->data[k]; b32 known = 0; for (iz j = 0; b->kind == N_NAMED && j < fields->len; j++) known |= equals(fields->data[j].name, b->name); if (b->kind == N_NAMED && !known) bad[nbad++] = b->name; } bind_why = fmtstr("%s has no %s%s %s", what, is_struct ? S("field") : S("parameter"), nbad > 1 ? S("s") : S(""), join_and(bad, nbad)); } else if (out[fi]) { bind_why = bind_quiet ? S("given twice") : fmtstr("%s is given twice", a->name); } else { out[fi] = a->a; } } else if (named) { bind_why = S("positional arguments come before named ones"); } else if (pos >= fields->len && bind_quiet) { bind_why = S("too many arguments"); } else if (pos >= fields->len) { iz npos = 0; for (iz k = 0; k < args->len; k++) npos += args->data[k]->kind != N_NAMED; bind_why = fmtstr("%s takes %lld argument%s, but this gives %lld", what, (long long)fields->len, fields->len == 1 ? S("") : S("s"), (long long)npos); } else { out[pos++] = a; } } Str *need = new(&perm, fields->len + 1, Str); iz nneed = 0; for (iz j = 0; j < fields->len && !bind_why.len; j++) { if (!out[j] && !fields->data[j].def && !is_struct) need[nneed++] = fields->data[j].name; } if (nneed) { where = at ? at->pos : (Pos){-1, 0, 0, 0}; bind_why = bind_quiet ? S("arguments missing") : fmtstr("%s needs %s", what, join_and(need, nneed)); } if (!bind_why.len) return out; bind_at = where; help_text = at ? bind_help : help_text; if (at) errorat(where, "%s", bind_why); return 0; } static Type * construct(Node *n, Type *t, Nodes *args, Scope *s) { resolve_named(t); if (t->kind != TY_STRUCT) { if (!args->len) { Call *c = new(&perm, 1, Call); c->kind = CALL_ZERO; c->type = t; make_call(n, c); n->type = t; return t; } if (args->len == 1 && args->data[0]->kind != N_NAMED) { // T(x) for any other type: x converted to T, as an assignment would. // String(bytes) and List(U8)(text) only relabel the same bytes. Node *a = args->data[0]; b32 intlit = a->kind == N_INT || (a->kind == N_UNARY && a->a->kind == N_INT); if (isnumeric(t) && !intlit) { // Int(2.9), U8(n), Float(n): a number of another type, checked // to fit (a float is cut toward zero) Type *from = check_expr(a, s, 0); if (isnumeric(from) && from != t) { Node *callee = n->a; n->kind = N_CAST; n->a = callee; n->b = a; n->list = (Nodes){0}; n->type = t; return t; } coerce(&args->data[0], t, S("the value")); // checked already replace_node(n, args->data[0]); return n->type; } Type *u8s = list_of(t_u8); if (t == t_string || t == u8s) { Type *from = check_expr(args->data[0], s, t == t_string ? u8s : t_string); if ((t == t_string && from == u8s) || (t == u8s && from == t_string)) { replace_node(n, args->data[0]); n->type = t; return t; } } check_value(&args->data[0], s, t, S("the value")); replace_node(n, args->data[0]); return n->type; } errorat(n->pos, "%s is not a struct, so it has no fields to fill in", type_name(t)); } Node **bound = bind_fields(&t->fields, args, n, type_name(t), 1); for (iz j = 0; j < t->fields.len; j++) { Field *f = t->fields.data + j; if (f->isprivate && type_outside(t) && bound[j]) errorat(bound[j]->pos, "%s.%s is private to %s", type_name(t), f->name, modules.data[t->module]->name); if (bound[j]) check_value(&bound[j], s, f->type, f->name); } Call *c = new(&perm, 1, Call); c->kind = CALL_CTOR; c->type = t; c->args = bound; c->nargs = t->fields.len; make_call(n, c); n->type = t; return t; } static Type * check_variant(Node *n, Scope *s, Type *want) { if (!want || !isunion(want)) { if (want && want->kind == TY_ANY) errorat(n->pos, ".%s needs a known union type", n->name); errorat(n->pos, ".%s needs a known union type; write Union.%s", n->name, n->name); } resolve_named(want); iz vi = variant_index(want, n->name); if (vi < 0) no_member(n->pos, want, n->name); n->ival = vi; n->type = want; Field *f = want->fields.data + vi; Type *pt = f->type; if (n->flags & F_CALLED) { if (!pt) errorat(n->pos, "%s.%s carries no value", type_name(want), n->name); resolve_named(pt); if (pt->kind == TY_STRUCT && (n->list.len != 1 || n->list.data[0]->kind == N_NAMED)) { Node *ctor = mknode(N_ANON, n->pos); ctor->list = n->list; construct(ctor, pt, &ctor->list, s); n->list = (Nodes){0}; addnode(&n->list, ctor); } else { if (n->list.len != 1) errorat(n->pos, "%s.%s carries one value", type_name(want), n->name); check_value(&n->list.data[0], s, pt, S("the variant")); } } else if (pt && pt != t_void) { errorat(n->pos, ".%s carries a %s: write .%s(value)", n->name, type_name(pt), n->name); } return want; } static Type * check_expr(Node *n, Scope *s, Type *want) { if (n->type && n->kind != N_NAME) return n->type; switch ((int)n->kind) { case N_INT: if (n->flags & F_CHAR) return n->type = t_u8; // 'x' is a byte n->type = want && want->kind == TY_INT ? want : t_int; literal_fits(n, n->type); if (want && want->kind == TY_FLOAT) { n->kind = N_FLOAT; n->fval = (f64)n->ival; n->type = want; } return n->type; case N_FLOAT: n->type = t_float; return t_float; case N_STR: n->type = t_string; return t_string; case N_BOOL: n->type = t_bool; return t_bool; case N_NIL: n->type = want && conversion_rank(t_nil, want) == 0 ? want : t_nil; return n->type; case N_NAME: { Sym *sym = n->sym ? n->sym : find(s, n->name, n->pos, 1); n->sym = sym; sym->used = 1; if (sym->kind == S_VAR) { if (sym->isglobal && !sym->type) check_global(sym); if (sym->ismacro && !in_macro_code) errorat(n->pos, "%s exists only at compile time; carry its value over with literal()", n->name); if (!sym->isglobal && sym->owner && sym->owner != scope_fn(s)) capture(sym, s); n->type = sym->type; return n->type; } if (sym->kind == S_FN && !(n->flags & F_FNVALUE) && parenless_set(sym, n->pos)) { // a parenless function named alone is called: tick is tick() Node *callee = new(&perm, 1, Node); *callee = *n; keep_source(n); n->kind = N_CALL; n->a = callee; n->list = (Nodes){0}; n->flags |= F_COMMAND; n->type = 0; return check_call(n, s, want); } if (sym->kind == S_FN) { Fn *f = single_fn(sym, n->pos); if (f->builtin) errorat(n->pos, "%s is built in and cannot be used as a value", n->name); if (f->ismacro && !in_macro_code) errorat(n->pos, "%s is a macro and cannot be used as a value", n->name); if (!f->type) check_fn(f); f->used = 1; if (f->isclosure) f->escapes = 1; n->type = fn_value(f->type); return n->type; } if (sym->kind == S_MODULE) errorat(n->pos, "%s is a module; use %s.name", n->name, n->name); if (sym->kind == S_LABEL || sym->kind == S_MLABEL) errorat(n->pos, "%s is a label", n->name); errorat(n->pos, "%s is a type, not a value", n->name); } case N_TYPENAME: // a type written as a value is a Type if (!n->info) n->info = callee_type(n, s); n->type = t_type; return t_type; case N_FIELD: return check_field(n, s); case N_INDEX: return check_index(n, s); case N_DEREF: { check_expr(n->a, s, 0); if (n->a->type->kind != TY_REF) errorat(n->pos, ".* follows a reference, but this is a %s", type_name(n->a->type)); n->type = n->a->type->elem; return n->type; } case N_ADDR: { Node *target = n->a; b32 asarg = addr_in_args; addr_in_args = 0; // only this & is an argument, not ones inside it check_expr(target, s, 0); addr_in_args = asarg; int place = place_kind(target); if (place == NOT_PLACE) { Node *root = root_of(target); if (root != target) { // &[1, 2][0]: part of a value no variable holds help_text = fmtstr("store it in a variable first: var v = %s", node_source(root)); errorat(n->pos, "& needs a place, but %s is a value no variable holds", node_source(root)); } errorat(n->pos, "& needs a variable, field or element"); } if (place == READONLY_PLACE) errorat(n->pos, "this cannot change here, so & cannot give access to it"); n->type = ref_of(target->type); if (!addr_in_args) { Node *root = root_of(target); if (root->kind == N_NAME && root->sym && root->sym->isvar && !root->sym->boxroute) errorat(n->pos, "%s is a var parameter: &%s may be passed on, but not stored", root->name, root->name); pin_root(target); } return n->type; } case N_UNARY: if (n->op == T_NOT) { check_value(&n->a, s, t_bool, S("not")); n->type = t_bool; return t_bool; } if (n->a->kind == N_INT && !(n->a->flags & F_CHAR) && (!want || want->kind == TY_INT)) { Type *t = want ? want : t_int; literal_fits(n, t); n->a->type = t; // -128 fits in I8, though 128 does not } check_expr(n->a, s, want && isnumeric(want) ? want : 0); if (!isnumeric(n->a->type)) errorat(n->pos, "%s needs a number", n->op == T_MINUS ? S("-") : S("+")); if (n->op == T_MINUS && n->a->type->kind == TY_INT && !n->a->type->issigned) errorat(n->pos, "an unsigned number cannot be negated"); n->type = n->a->type; return n->type; case N_BINARY: return check_binary(n, s, want); case N_IN: return check_in(n, s); case N_RANGE: check_value(&n->a, s, t_int, S("a range")); check_value(&n->b, s, t_int, S("a range")); n->type = t_range; return t_range; case N_LIST: return check_list_literal(n, s, want); case N_ANON: if (!want || (want->kind != TY_STRUCT && !(isunion(want)))) { errorat(n->pos, ".(...) needs a known struct type; write Type(...)"); } if (isunion(want)) { // .(...) for a union: its only struct variant resolve_named(want); iz found = -1; for (iz i = 0; i < want->fields.len; i++) { Type *ft = want->fields.data[i].type; if (ft && ft->kind == TY_STRUCT) { if (found >= 0) errorat(n->pos, "%s has several struct variants; name one", type_name(want)); found = i; } } if (found < 0) errorat(n->pos, ".(...) needs a struct type, not %s", type_name(want)); construct(n, want->fields.data[found].type, &n->list, s); return n->type; } return construct(n, want, &n->list, s); case N_VARIANT: return check_variant(n, s, want); case N_CALL: return check_call(n, s, want); case N_BLOCK: { // f({ ... }): parsed as a statement, for macros taking Code Node *b = mknode(N_BLOCK, n->pos); *b = *n; n->kind = N_BLOCKX; n->a = b; return check_expr(n, s, want); } case N_BLOCKX: { Scope *bs = mkscope(s, SC_BLOCK); n->type = check_block_value(n->a, bs, want, 1); return n->type; } case N_IFX: { check_value(&n->a, s, t_bool, S("if")); if (want) { check_value(&n->b, s, want, S("this branch")); check_value(&n->c, s, want, S("this branch")); n->type = want; return want; } check_expr(n->b, s, 0); b32 bgone = n->b->kind == N_BLOCKX && diverges(n->b->a); // if c do { return x } else 5 check_expr(n->c, s, n->b->type == t_nil || bgone ? 0 : n->b->type); Type *a = n->b->type, *b = n->c->type; if (bgone || (n->c->kind == N_BLOCKX && diverges(n->c->a))) { n->type = bgone ? b : a; } else if (isnumeric(a) && isnumeric(b) && a != b) { // if c do 1 else 2.5: one number type, as in [1, 2.5] Node *lit = n->b->kind == N_INT ? n->b : 0; if (lit) help_text = fmtstr("write %lld.0", (long long)lit->ival); errorat(n->c->pos, "the first branch is %s, so this one must be too, but it is %s", with_articles(fmtstr("a %s", type_name(a))), with_articles(fmtstr("a %s", type_name(b)))); } else if (a == b) { n->type = a; } else if (a == t_nil && conversion_rank(t_nil, b) == 0) { coerce(&n->b, b, S("this branch")); n->type = b; } else if (b == t_nil && conversion_rank(t_nil, a) == 0) { coerce(&n->c, a, S("this branch")); n->type = a; } else { Type *both[2] = {a, b}; Type *u = anon_union(both, 2); coerce(&n->b, u, S("this branch")); coerce(&n->c, u, S("this branch")); n->type = u; } return n->type; } case N_SWITCHX: n->type = check_switch(n->a, s, want, 1); return n->type; case N_FNLIT: return check_fnlit(n, s, want); case N_TYPEX: // a type written as a value is a Type: struct (...), fn (Int) -> Int if (!n->info) n->info = resolve_type(n->a, s); n->type = t_type; return t_type; case N_TEMPLATE: return check_template(n, s, want); case N_SPLICE: errorat(n->pos, "$ splices a value into a template"); case N_NAMED: errorat(n->pos, "name: value only appears among call arguments"); case N_PAIR: errorat(n->pos, "key: value only appears in a map literal"); case X_CONV: case X_CALLFN: return n->type; default: errorat(n->pos, "this is not a value"); } } static Fn * single_fn(Sym *sym, Pos p) { if (sym->fns.len != 1) errorat(p, "%s has several overloads; call it instead of naming it", sym->name); Fn *f = sym->fns.data[0]; if (f->co && f == f->co->fn) errorat(p, "%s is a coroutine, so it cannot be used as a value", sym->name); if (!f->builtin) fn_signature(f); return f; } // An argument as written, before a conversion wrapped it. static Node * args_original(Node *a) { return (int)a->kind == X_CONV ? a->a : a; } static b32 is_deferred_arg(Node *a) { switch (a->kind) { case N_INT: case N_FLOAT: case N_NIL: case N_ANON: case N_VARIANT: case N_LIST: case N_FNLIT: return 1; case N_UNARY: return a->a->kind == N_INT || a->a->kind == N_FLOAT; default: return 0; } } static int deferred_rank(Node *a, Type *pt) { switch (a->kind) { case N_INT: return pt == t_int ? 0 : (pt->kind == TY_INT || pt->kind == TY_FLOAT) ? 1 : isunion(pt) && conversion_rank(t_int, pt) == 1 ? 1 // into a listed union, before Any : pt->kind == TY_ANY ? 3 : -1; case N_UNARY: return deferred_rank(a->a, pt); case N_FLOAT: return pt->kind == TY_FLOAT ? 0 : isunion(pt) && conversion_rank(t_float, pt) == 1 ? 1 : pt->kind == TY_ANY ? 3 : -1; case N_NIL: return conversion_rank(t_nil, pt); case N_ANON: if (pt->kind == TY_STRUCT) return 0; return -1; case N_VARIANT: if (isunion(pt)) { resolve_named(pt); return variant_index(pt, a->name) >= 0 ? 0 : -1; } return -1; case N_LIST: if (pt->kind == TY_LIST || pt->kind == TY_ARRAY || pt->kind == TY_MAP) return 0; return pt->kind == TY_ANY ? 3 : -1; case N_FNLIT: return pt->kind == TY_FN ? 0 : -1; default: return -1; } } typedef struct { Fn *fn; Node **bound; int cost; // steps of union conversion: fewer wins a tie } Cand; static int conversion_steps(Node *a, Field *p) { if (p->isvar || is_deferred_arg(a) || a->type == p->type || !isunion(p->type)) return 0; Conv cv = {0}; union_path(p->type, a->type, &cv, 0); return (int)cv.npath; } static Node ** try_bind(Fn *f, Nodes *args) { return bind_fields(&f->type->fields, args, 0, f->name, 0); } // try_bind without a reason for a mismatch: choosing an overload tries // many that do not fit, and only an error explains, binding again. static Node ** try_fit(Fn *f, Nodes *args) { bind_quiet = 1; Node **bound = bind_fields(&f->type->fields, args, 0, S(""), 0); bind_quiet = 0; return bound; } static int arg_rank(Node *a, Field *p) { if (p->isvar) { if (a->kind != N_ADDR) return -1; return a->type->elem == p->type ? 0 : -1; } if (is_deferred_arg(a)) return deferred_rank(a, p->type); return conversion_rank(a->type, p->type); } static void finish_args(Fn *f, Node **bound, Scope *s) { Fields *ps = &f->type->fields; // arguments already known not to fit: the first is the error, the // others its notes, so all show at once notes.len = 0; b32 first = 1; for (iz j = 0; j < ps->len; j++) { Node *a = bound[j]; if (!a || ps->data[j].isvar || (!is_deferred_arg(a) && !a->type)) continue; if (arg_rank(a, ps->data + j) >= 0) continue; if (!first) { // cleared again below if nothing fails note(a->pos, with_articles(a->type ? fmtstr("and %s needs a %s, but this is a %s", ps->data[j].name, type_name_vs(ps->data[j].type, a->type), type_name_vs(a->type, ps->data[j].type)) : fmtstr("and %s needs a %s", ps->data[j].name, type_name(ps->data[j].type)))); } first = 0; } for (iz j = 0; j < ps->len; j++) { Field *p = ps->data + j; if (!bound[j]) continue; if (p->isvar) { Node *a = bound[j]; if (a->kind != N_ADDR) errorat(a->pos, "%s is a var parameter: pass &variable", p->name); a->flags |= F_VAR; if (a->type->elem != p->type) errorat(a->pos, "%s needs access to a %s, not a %s", p->name, type_name(p->type), type_name(a->type->elem)); continue; } check_value(&bound[j], s, p->type, p->name); if (bound[j]->kind == N_ADDR) pin_root(bound[j]->a); if (bound[j]->kind == N_FNLIT && !f->builtin && !param_escapes(f, j)) { // a closure passed straight to a parameter that only calls it // does not outlive the call ((Fn *)bound[j]->info)->escapes = 0; } } notes.len = 0; } static STATE Map *param_variants = 0; // (function, index) as bytes -> VariantRef // The key of a function's parameter in param_variants: the two words. static Str pvariant_key(uptr *k, Fn *f, iz i) { k[0] = (uptr)f; k[1] = (uptr)i; return (Str){(u8 *)k, 2 * (iz)sizeof(uptr)}; } static VariantRef * pvariant(Fn *f, iz i) { uptr k[2] = {0}; return lookup(param_variants, pvariant_key(k, f, i)); } static Type *check_builtin(Node *n, Fn *b, Scope *s, Type *want); // Why the arguments do not fit f, for the notes of a failed call static Str why_not(Fn *f, Nodes *args) { Node **bound = try_bind(f, args); if (!bound) return bind_why; Fields *ps = &f->type->fields; for (iz j = 0; j < ps->len; j++) { Field *p = ps->data + j; Node *a = bound[j]; if (!a) continue; Str pn = p->name; if (p->isvar && a->kind != N_ADDR) return fmtstr("%s is a var parameter, so it takes &variable", pn); if (p->isvar && a->type->elem != p->type) return fmtstr("%s needs access to a %s, not a %s", pn, type_name(p->type), type_name(a->type->elem)); if (!p->isvar && arg_rank(a, p) < 0) return is_deferred_arg(a) ? fmtstr("%s needs a %s", pn, type_name(p->type)) : fmtstr("%s needs a %s, but this is a %s", pn, type_name_vs(p->type, a->type), type_name_vs(a->type, p->type)); } return S("this one fits"); } static Type * resolve_overloads(Node *n, Sym *sym, Scope *s, Type *want) { // a built-in alone checks its own arguments, some of which are not values if (sym->fns.len == 1 && sym->fns.data[0]->builtin) return check_builtin(n, sym->fns.data[0], s, want); Nodes *args = &n->list; // Check the arguments that do not depend on their target first. for (iz i = 0; i < args->len; i++) { Node *a = args->data[i]->kind == N_NAMED ? args->data[i]->a : args->data[i]; addr_in_args = a->kind == N_ADDR; if (!is_deferred_arg(a)) check_expr(a, s, 0); addr_in_args = 0; } Cand best[64] = {0}; iz nbest = 0; int bestlevel = 99; Fn *builtin = 0; for (iz c = 0; c < sym->fns.len; c++) { Fn *f = sym->fns.data[c]; if (f->builtin) { builtin = f; continue; } if (f->ismacro && !in_macro_code) continue; fn_signature(f); Node **bound = try_fit(f, args); if (!bound) continue; int level = 0; Fields *ps = &f->type->fields; for (iz j = 0; j < ps->len && level >= 0; j++) { if (!bound[j]) continue; VariantRef *pv = pvariant(f, j); int r = 0; if (pv && !is_deferred_arg(bound[j]) && bound[j]->type == pv->u) r = -1; // a dispatch candidate else r = arg_rank(bound[j], ps->data + j); if (r < 0) level = -1; else if (r > level) level = r; } if (level < 0) continue; int cost = 0; for (iz j = 0; j < ps->len; j++) { if (bound[j]) cost += conversion_steps(bound[j], ps->data + j); } if (level < bestlevel) { bestlevel = level; nbest = 0; } if (level == bestlevel && nbest < 64) best[nbest++] = (Cand){f, bound, cost}; } if (nbest > 1 && bestlevel == 1) { // among conversions into listed unions, the shortest path wins int least = best[0].cost; for (iz i = 1; i < nbest; i++) least = best[i].cost < least ? best[i].cost : least; iz k = 0; for (iz i = 0; i < nbest; i++) { if (best[i].cost == least) best[k++] = best[i]; } nbest = k; } // A built-in of the name takes its arguments as they are: it wins over // a user overload that would need a conversion (print of a String // inside print(x: Symbol) is the built-in print, not a recursion). if (builtin && bestlevel >= 1 && nbest) return check_builtin(n, builtin, s, want); if (bestlevel <= 1 && nbest == 1) { return call_fn(n, best[0].fn, best[0].bound, s); } if (nbest > 1 && bestlevel <= 1) { notes.len = 0; for (iz i = 0; i < nbest; i++) { note(best[i].fn->decl ? best[i].fn->decl->pos : n->pos, S("candidate")); } errorat(n->pos, "this call matches several overloads of %s equally well", sym->name); } // Case dispatch: an argument is a union, and there is one case per variant. for (iz ai = 0; ai < args->len; ai++) { Node *a = args->data[ai]; if (a->kind == N_NAMED || is_deferred_arg(a) || !isunion(a->type)) continue; Type *u = a->type; resolve_named(u); Fn **cases = new(&perm, u->fields.len, Fn *); Node ***binds = new(&perm, u->fields.len, Node **); b32 any = 0; for (iz c = 0; c < sym->fns.len; c++) { Fn *f = sym->fns.data[c]; if (f->builtin || (f->ismacro && !in_macro_code)) continue; fn_signature(f); Node **bound = try_fit(f, args); if (!bound) continue; Fields *ps = &f->type->fields; if (ai >= ps->len || bound[ai] != a) continue; iz v = -1; VariantRef *pv = pvariant(f, ai); if (pv && pv->u == u) v = pv->index; else if (!pv) { for (iz k = 0; k < u->fields.len; k++) { Field *uf = u->fields.data + k; if (!uf->name.len && uf->type == ps->data[ai].type) v = k; } } if (v < 0) continue; b32 ok = 1; for (iz j = 0; j < ps->len && ok; j++) { if (j == ai || !bound[j]) continue; int r = arg_rank(bound[j], ps->data + j); if (r < 0 || r > 1) ok = 0; } if (!ok) continue; any = 1; if (cases[v]) errorat(n->pos, "two cases of %s handle %s.%s", sym->name, type_name(u), variant_name(u, v)); cases[v] = f; binds[v] = bound; } if (!any) continue; for (iz v = 0; v < u->fields.len; v++) { if (cases[v]) not_a_coroutine_call(cases[v], n->pos); if (!cases[v]) { errorat(n->pos, "%s is called on a %s, but has no case for %s", sym->name, type_name(u), variant_name(u, v)); } } Type **results = new(&perm, u->fields.len, Type *); // the other arguments are worked out once and converted for each case Fields *ps0 = &cases[0]->type->fields; for (iz v = 0; v < u->fields.len; v++) { Fn *f = cases[v]; Fields *ps = &f->type->fields; for (iz j = 0; j < ps->len; j++) { if (j == ai || !binds[v][j]) continue; Field *p0 = j < ps0->len ? ps0->data + j : 0; if (v > 0 && (!p0 || !binds[0][j] || p0->isvar != ps->data[j].isvar || (is_deferred_arg(args_original(binds[0][j])) && p0->type != ps->data[j].type))) { // a literal takes one type: the cases must agree on it notes.len = 0; note(cases[0]->decl->pos, fmtstr("this case takes %s as %s%s", ps->data[j].name, p0 && p0->isvar ? S("var ") : S(""), p0 ? type_name(p0->type) : S("nothing"))); note(f->decl->pos, fmtstr("this one as %s%s", ps->data[j].isvar ? S("var ") : S(""), type_name(ps->data[j].type))); help_text = S("give the argument its type first: const k: T = value"); errorat(binds[v][j]->pos, "the cases of %s take %s differently", sym->name, ps->data[j].name); } if (ps->data[j].isvar) { binds[v][j]->flags |= F_VAR; } else if (v == 0) { check_value(&binds[v][j], s, ps->data[j].type, ps->data[j].name); } else if (is_deferred_arg(args_original(binds[0][j]))) { binds[v][j] = binds[0][j]; // the same type: the same value } else { coerce(&binds[v][j], ps->data[j].type, ps->data[j].name); } } f->used = 1; if (f->infer && !f->checked) check_inferred(f, n->pos); results[v] = f->type->elem; } Call *c = new(&perm, 1, Call); c->kind = CALL_DISPATCH; c->cases = cases; c->case_args = binds; c->dispatch = ai; c->dispatch_union = u; c->args = binds[0]; c->nargs = cases[0]->type->fields.len; c->fn = cases[0]; make_call(n, c); n->type = anon_union(results, u->fields.len); return n->type; } if (bestlevel == 3 && nbest == 1) { return call_fn(n, best[0].fn, best[0].bound, s); } if (nbest > 1) errorat(n->pos, "this call matches several overloads of %s equally well", sym->name); if (builtin) return check_builtin(n, builtin, s, want); Buf b = {0}; for (iz i = 0; i < args->len; i++) { Node *a = args->data[i]->kind == N_NAMED ? args->data[i]->a : args->data[i]; if (i) bput(&b, S(", ")); if (args->data[i]->kind == N_NAMED) { bput(&b, args->data[i]->name); bput(&b, S(": ")); } if (is_deferred_arg(a)) bput(&b, S("?")); else put_type_name(&b, a->type); } if (sym->fns.len == 1 && !sym->fns.data[0]->builtin) { // explain the mismatch for a single candidate Fn *f = sym->fns.data[0]; Node **bound = try_bind(f, args); if (!bound) { help_text = bind_help; errorat(bind_at.file >= 0 ? bind_at : n->pos, "%s", bind_why); } // one candidate: its parameter types check the arguments, so // literals and .variants convert as they would in an assignment return call_fn(n, f, bound, s); } notes.len = 0; for (iz c = 0; c < sym->fns.len && c < 8; c++) { Fn *f = sym->fns.data[c]; if (f->decl && !(f->ismacro && !in_macro_code)) { note(f->decl->pos, with_articles(why_not(f, args))); } } errorat(n->pos, "no overload of %s takes (%s)", sym->name, bstr(&b)); } static Type * check_call_value(Node *n, Scope *s) { Type *ft = n->a->type; if (ft->kind != TY_FN) { Sym *f = n->a->kind == N_NAME ? builtin_sym(n->a->name) : 0; if (!f && n->a->kind == N_NAME) f = prelude_sym(n->a->name); if (!f && n->a->kind == N_NAME) f = find_in_imports(s->file, n->a->name); if (f && f->kind == S_FN) help_text = fmtstr("this %s hides the function %s; rename it", n->a->name, n->a->name); errorat(n->pos, "a %s cannot be called", type_name(ft)); } Fields *ps = &ft->fields; if (n->list.len != ps->len) errorat(n->pos, "this function takes %lld arguments, not %lld", (long long)ps->len, (long long)n->list.len); Call *c = new(&perm, 1, Call); c->kind = CALL_VALUE; c->callee = n->a; c->args = new(&perm, ps->len + 1, Node *); c->nargs = ps->len; for (iz i = 0; i < ps->len; i++) { Node *a = n->list.data[i]; if (a->kind == N_NAMED) errorat(a->pos, "a function value takes positional arguments only"); c->args[i] = a; if (ps->data[i].isvar) { addr_in_args = 1; check_expr(a, s, 0); addr_in_args = 0; if (a->kind != N_ADDR || a->type->elem != ps->data[i].type) errorat(a->pos, "this parameter is var: pass &variable"); a->flags |= F_VAR; } else { check_value(&c->args[i], s, ps->data[i].type, S("the argument")); if (c->args[i]->kind == N_ADDR) pin_root(c->args[i]->a); } } make_call(n, c); n->type = ft->elem; return n->type; } // A type argument parsed as a value, List(List(Int)), as a type node. static Node * as_type_node(Node *a) { if (a->kind == N_INT) return a; if (a->kind == N_TYPEX) return a->a; Node *t = a->kind == N_CALL ? a->a : a; if (t->kind != N_TYPENAME) errorat(a->pos, "expected a type"); Node *tn = mknode(N_TNAME, t->pos); tn->name = t->name; for (iz i = 0; a->kind == N_CALL && i < a->list.len; i++) { Node *x = a->list.data[i]; // Coroutine(name): a name, not a type b32 named = equals(t->name, S("Coroutine")) && (x->kind == N_NAME || x->kind == N_FIELD); addnode(&tn->list, named ? x : as_type_node(x)); } return tn; } // The type a call names: Point(...), or List(Int)() with type arguments. static Type * callee_type(Node *c, Scope *s) { return resolve_type(as_type_node(c), s); } // Whether every overload of a function name is parenless (1), none is (0); // a mix is an error. static b32 parenless_set(Sym *sym, Pos at) { iz marked = 0; for (iz i = 0; i < sym->fns.len; i++) marked += sym->fns.data[i]->isparenless; if (marked && marked < sym->fns.len) errorat( at, "some overloads of %s are parenless and some are not; wrap all of them in parenless(...), or none", sym->name); return marked > 0; } // A call written without parentheses goes to a parenless function. static void need_parenless(Node *n, Scope *s) { Node *callee = n->a; Sym *sym = 0; if (callee->kind == N_NAME) { sym = callee->sym ? callee->sym : find(s, callee->name, callee->pos, 1); } else if (callee->kind == N_FIELD && callee->a->kind == N_NAME) { Sym *base = find(s, callee->a->name, callee->a->pos, 1); if (base->kind == S_MODULE) sym = scope_get(base->mod->scope, callee->name); } if (sym && sym->kind == S_FN && parenless_set(sym, callee->pos)) return; Str name = node_source(callee); help_text = fmtstr("write %s(...), or declare it as parenless(fn %s(...) ...)", name, name); errorat(callee->pos, "%s is not parenless, so calling it needs parentheses", name); } // Questions about the program (std:code) take Defs. Where a call's // parameter is a Def, a name of the program written there is that // declaration: writes(scan), type(Point), overloads(log). A variable of // type Def stays what it is. static Node *def_handle_node(Node *decl, Pos at); static Fn *named_fn(Node *a, Scope *s); static void names_as_defs(Node *n, Scope *s) { Type *dt = code_type(S("Def")); if (!dt || n->a->kind != N_NAME) return; Sym *callee = find(s, n->a->name, n->a->pos, 0); if (!callee || callee->kind != S_FN) return; for (iz i = 0; i < n->list.len; i++) { Node *a = n->list.data[i]; b32 wants = 0; for (iz k = 0; k < callee->fns.len && !wants; k++) { Fn *f = callee->fns.data[k]; if (f->builtin || f->module < 0 || modules.data[f->module] != code_module) continue; fn_signature(f); wants = i < f->type->fields.len && f->type->fields.data[i].type == dt; } if (!wants) continue; Node *decl = 0; if (a->kind == N_CALL && a->a->kind == N_NAME && equals(a->a->name, S("overload_of"))) { decl = named_fn(a, s)->decl; } else if (a->kind == N_NAME || a->kind == N_TYPENAME) { Sym *sym = find(s, a->name, a->pos, 1); if (sym->kind == S_VAR && !sym->isglobal) continue; // a Def variable if (sym->kind == S_FN) { if (sym->fns.data[0]->builtin) errorat(a->pos, "%s is built in, not declared in the program", a->name); if (sym->fns.len > 1) { notes.len = 0; for (iz k = 0; k < sym->fns.len && k < 8; k++) { if (!sym->fns.data[k]->decl) continue; note(sym->fns.data[k]->decl->pos, S("an overload")); } help_text = fmtstr("name one with overload_of(%s, T, ...)", a->name); errorat(a->pos, "%s has %lld overloads", a->name, (long long)sym->fns.len); } decl = sym->fns.data[0]->decl; } else if (sym->kind == S_TYPE) { decl = sym->type->decl; } else if (sym->kind == S_VAR) { decl = sym->decl; } } if (!decl) continue; n->list.data[i] = def_handle_node(decl, a->pos); } } static Type *check_call_at(Node *n, Scope *s, Type *want); // A call: while it is resolved, a detour into a body names it as the use. static Type * check_call(Node *n, Scope *s, Type *want) { Pos save = need_at; need_at = n->pos; Type *t = check_call_at(n, s, want); need_at = save; return t; } static Type * check_call_at(Node *n, Scope *s, Type *want) { if (n->flags & F_COMMAND) need_parenless(n, s); Node *r = macro_sym(n, s) ? expand(n, s, P_EXPR) : 0; if (r) { // a macro called from run-time code: its result takes the call's place *n = *r; return check_expr(n, s, want); } names_as_defs(n, s); Node *callee = n->a; if (callee->kind == N_TYPEX) return construct(n, resolve_type(callee->a, s), &n->list, s); if (callee->kind == N_TYPENAME && (equals(callee->name, S("List")) || equals(callee->name, S("Map")) || equals(callee->name, S("Pool")) || equals(callee->name, S("Ref")) || (equals(callee->name, S("Coroutine")) && n->list.len == 1 && (n->list.data[0]->kind == N_NAME || n->list.data[0]->kind == N_FIELD)))) { // List(Int) as a value is a Type; List(Int)() makes a list n->info = callee_type(n, s); keep_source(n); n->kind = N_TYPENAME; n->type = t_type; return t_type; } if (callee->kind == N_TYPENAME || (callee->kind == N_CALL && callee->a->kind == N_TYPENAME)) return construct(n, callee_type(callee, s), &n->list, s); if (callee->kind == N_FIELD && callee->a->kind == N_TYPENAME) { // Shape.circle(value) Type *u = callee_type(callee->a, s); if (!isunion(u)) errorat(callee->pos, "%s is not a union", type_name(u)); keep_source(n); n->kind = N_VARIANT; n->name = callee->name; n->flags |= F_CALLED; n->a = 0; return check_variant(n, s, u); } if (callee->kind == N_FIELD && callee->a->kind == N_NAME) { Sym *base = find(s, callee->a->name, callee->a->pos, 1); if (base->kind == S_MODULE) { Sym *m = scope_get(base->mod->scope, callee->name); if (!m) no_module_member(callee->pos, callee->a->name, base->mod->scope, callee->name, S("")); if (m->isprivate) errorat(callee->pos, "%s is private to %s", callee->name, base->mod->name); if (m->kind == S_FN) return resolve_overloads(n, m, s, want); if (m->kind == S_TYPE) return construct(n, m->type, &n->list, s); keep_source(callee); callee->kind = N_NAME; callee->sym = m; } } if (callee->kind == N_NAME) { if (!callee->sym && is_c_call(n) && !find(s, callee->name, callee->pos, 0)) c_misplaced(n); Sym *sym = callee->sym ? callee->sym : find(s, callee->name, callee->pos, 1); callee->sym = sym; if (sym->kind == S_FN) { // user overloads of a built-in or prelude name keep those too Sym *b = builtin_sym(callee->name); Sym *pre = prelude_sym(callee->name); for (iz i = 0; pre && i < sym->fns.len; i++) { for (iz j = 0; pre && j < pre->fns.len; j++) { if (pre->kind != S_FN || sym->fns.data[i] == pre->fns.data[j]) pre = 0; // there } } if ((b && !sym->fns.data[0]->builtin) || (pre && pre != sym && pre->kind == S_FN)) { Sym *merged = mksym(S_FN, sym->name, sym->pos); for (iz i = 0; i < sym->fns.len; i++) *push(&perm, &merged->fns) = sym->fns.data[i]; if (b && !sym->fns.data[0]->builtin) *push(&perm, &merged->fns) = b->fns.data[0]; if (pre && pre != sym && pre->kind == S_FN) for (iz i = 0; i < pre->fns.len; i++) *push(&perm, &merged->fns) = pre->fns.data[i]; sym = merged; } return resolve_overloads(n, sym, s, want); } if (sym->kind == S_TYPE) return construct(n, sym->type, &n->list, s); } check_expr(callee, s, 0); return check_call_value(n, s); } // Built-in functions // A built-in with a fixed signature has its usage, its parameter types and // result (i Int, s String, b Bool, f Float, L List(String), v nothing) and // the C it becomes: %0, %1 are the arguments, %@ the source position. static struct { Str name; Builtin b; b32 macro; // only in compile-time code Str usage, params; char result; Str c; } builtin_table[] = { {SL("print"), B_PRINT, 0, {0}, {0}, 0, {0}}, {SL("println"), B_PRINTLN, 0, {0}, {0}, 0, {0}}, {SL("str"), B_STR, 0, {0}, {0}, 0, {0}}, {SL("len"), B_LEN, 0, {0}, {0}, 0, {0}}, {SL("add"), B_ADD, 0, {0}, {0}, 0, {0}}, {SL("insert"), B_INSERT, 0, {0}, {0}, 0, {0}}, {SL("remove"), B_REMOVE, 0, {0}, {0}, 0, {0}}, {SL("exit"), B_EXIT, 0, SL("exit(code)"), SL("i"), 'v', SL("s99_exit((int)%0)")}, {SL("arguments"), B_ARGUMENTS, 0, SL("arguments()"), SL(""), 'L', SL("s99_arguments()")}, {SL("read_file"), B_READ_FILE, 0, SL("read_file(path)"), SL("s"), 's', SL("s99_read_file(%0, %@)")}, {SL("write_file"), B_WRITE_FILE, 0, SL("write_file(path, text)"), SL("ss"), 'v', SL("s99_write_file(%0, %1, %@)")}, {SL("file_exists"), B_FILE_EXISTS, 0, SL("file_exists(path)"), SL("s"), 'b', SL("s99_file_exists(%0)")}, {SL("run"), B_RUN, 0, SL("run(command)"), SL("s"), 'i', SL("s99_run(%0)")}, {SL("getenv"), B_GETENV, 0, SL("getenv(name)"), SL("s"), 's', SL("s99_getenv(%0)")}, {SL("shell"), B_SHELL, 0, SL("shell(command)"), SL("s"), 's', SL("s99_shell(%0)")}, {SL("abs"), B_ABS, 0, {0}, {0}, 0, {0}}, {SL("sqrt"), B_SQRT, 0, SL("sqrt(x)"), SL("f"), 'f', SL("sqrt(%0)")}, {SL("floor"), B_FLOOR, 0, SL("floor(x)"), SL("f"), 'f', SL("floor(%0)")}, {SL("fail"), B_FAIL, 0, {0}, {0}, 0, {0}}, {SL("resize"), B_RESIZE, 0, {0}, {0}, 0, {0}}, {SL("expanded"), B_EXPANDED, 1, {0}, {0}, 0, {0}}, {SL("ask"), B_ASK, 1, {0}, {0}, 0, {0}}, {SL("known"), B_KNOWN, 1, {0}, {0}, 0, {0}}, {SL("code__encode"), B_ENCODE, 0, {0}, {0}, 0, {0}}, {SL("code__decode"), B_DECODE, 0, {0}, {0}, 0, {0}}, {SL("with_main"), B_WITH_MAIN, 1, {0}, {0}, 0, {0}}, {SL("draw"), B_DRAW, 0, {0}, {0}, 0, {0}}, {SL("assume"), B_ASSUME, 0, SL("assume(condition)"), SL("b"), 'v', SL("s99_assume(%0)")}, {SL("format"), B_FORMAT, 1, {0}, {0}, 0, {0}}, {SL("literal"), B_LITERAL, 1, {0}, {0}, 0, {0}}, {SL("here"), B_HERE, 1, {0}, {0}, 0, {0}}, {SL("error"), B_ERROR, 1, {0}, {0}, 0, {0}}, {SL("warning"), B_WARNING, 1, {0}, {0}, 0, {0}}, {SL("place"), B_PLACE, 1, {0}, {0}, 0, {0}}, {SL("type_of"), B_TYPE_OF, 0, {0}, {0}, 0, {0}}, {SL("args_of"), B_ARGS_OF, 0, {0}, {0}, 0, {0}}, {SL("ret_of"), B_RET_OF, 0, {0}, {0}, 0, {0}}, {SL("overload_of"), B_OVERLOAD_OF, 0, {0}, {0}, 0, {0}}, {SL("yield"), B_YIELD, 0, {0}, {0}, 0, {0}}, }; static iz builtin_entry(Builtin b) { for (iz i = 0; i < countof(builtin_table); i++) { if (builtin_table[i].b == b) return i; } return -1; } static Type * sig_type(char c) { return c == 'i' ? t_int : c == 's' ? t_string : c == 'b' ? t_bool : c == 'f' ? t_float : c == 'L' ? list_of(t_string) : t_void; } static Str closest_name(Scope *s, Str name) { near_start(name); for (Scope *sc = s; sc; sc = sc->parent) near_map(sc->names); if (s && s->file >= 0 && s->file < fileimports.len) { FileImports *fi = fileimports.data + s->file; near_map(fi->aliases); for (iz i = 0; i < fi->open.len; i++) near_map(fi->open.data[i]->scope->names); } for (iz i = 0; i < countof(builtin_table); i++) near_offer(builtin_table[i].name); near_map(prelude_order()); // all of it, loaded or not return near_best; } static STATE Map *builtin_syms = 0; static b32 macro_only(Builtin b) { iz e = builtin_entry(b); return e >= 0 && builtin_table[e].macro; } static Sym * builtin_sym(Str name) { Sym *s = lookup(builtin_syms, name); if (s) return s; for (iz i = 0; i < countof(builtin_table); i++) { if (equals(name, builtin_table[i].name)) { Sym *sym = mksym(S_FN, name, (Pos){-1, 0, 0, 0}); Fn *f = new(&perm, 1, Fn); f->name = name; f->builtin = builtin_table[i].b; f->module = -1; *push(&perm, &sym->fns) = f; *upsert(&builtin_syms, name, 1) = sym; return sym; } } return 0; } // The name of a built-in from its usage, "len(x)": len static Str usage_name(Str usage) { iz i = 0; while (i < usage.len && usage.data[i] != '(') i++; return substr(usage, 0, i); } static void need_args(Node *n, iz count, Str usage) { help_text = fmtstr("write %s", usage); if (n->list.len != count) errorat(n->pos, "%s takes %lld argument%s, but this gives %lld", usage_name(usage), (long long)count, count == 1 ? S("") : S("s"), (long long)n->list.len); for (iz i = 0; i < n->list.len; i++) { if (n->list.data[i]->kind == N_NAMED) errorat(n->list.data[i]->pos, "%s takes no named arguments", usage_name(usage)); } help_text = (Str){0}; } static Type * access_arg(Node *a, Scope *s, Str usage) { if (a->kind != N_ADDR) { help_text = fmtstr("write %s", usage); errorat(a->pos, "%s changes its first argument, so it takes &variable", usage_name(usage)); } addr_in_args = 1; check_expr(a, s, 0); addr_in_args = 0; a->flags |= F_VAR; return a->type->elem; } // The one function a name or overload_of(f, T, ...) stands for. static Fn * named_fn(Node *a, Scope *s) { if (a->kind == N_CALL && a->a->kind == N_NAME && equals(a->a->name, S("overload_of"))) { if (!a->list.len) { help_text = S("write overload_of(f, T, ...)"); errorat(a->pos, "overload_of needs a function and its parameter types"); } Node *f = a->list.data[0]; Sym *sym = f->kind == N_NAME ? find(s, f->name, f->pos, 1) : 0; if (!sym || sym->kind != S_FN) errorat(f->pos, "overload_of needs the name of a function"); Fields want = {0}; for (iz i = 1; i < a->list.len; i++) { Field fl = {0}; fl.type = type_arg(a->list.data[i], s); *push(&perm, &want) = fl; } for (iz i = 0; i < sym->fns.len; i++) { Fn *fn = sym->fns.data[i]; if (fn->builtin) continue; fn_signature(fn); Fields *ps = &fn->type->fields; b32 same = ps->len == want.len; for (iz j = 0; same && j < ps->len; j++) same = ps->data[j].type == want.data[j].type; if (same) return fn; } errorat(a->pos, "%s has no overload with exactly those parameter types", sym->name); } Sym *sym = a->kind == N_NAME ? find(s, a->name, a->pos, 1) : 0; if (!sym || sym->kind != S_FN || sym->fns.data[0]->builtin) errorat(a->pos, "this needs the name of a function"); if (sym->fns.len != 1) errorat(a->pos, "%s has several overloads; choose one with overload_of(%s, T, ...)", sym->name, sym->name); fn_signature(sym->fns.data[0]); return sym->fns.data[0]; } // type_of(x), args_of(f) and ret_of(f): the type they name, in type and // value positions alike. x is checked, never run. static Type * type_query(Node *call, Scope *s) { Str q = call->a->name; if (call->list.len != 1) errorat(call->pos, "%s takes one argument", q); Node *a = call->list.data[0]; if (equals(q, S("type_of"))) return check_expr(a, s, 0); Fn *f = named_fn(a, s); if (equals(q, S("ret_of"))) { if (f->type->elem == t_void) errorat(a->pos, "%s returns nothing", f->name); return f->type->elem; } // args_of: the parameters as an anonymous struct Type *t = mktype(TY_STRUCT); for (iz i = 0; i < f->type->fields.len; i++) { Field fl = f->type->fields.data[i]; if (fl.isvar) errorat(a->pos, "%s has a var parameter, which cannot be a field", f->name); fl.def = 0; *push(&perm, &t->fields) = fl; } return intern(t); } static b32 is_type_query(Node *n) { if (n->kind != N_CALL || n->a->kind != N_NAME) return 0; Str q = n->a->name; return equals(q, S("type_of")) || equals(q, S("args_of")) || equals(q, S("ret_of")); } // A type written as an argument: a name, List(Int), or a type query. static Type * type_arg(Node *a, Scope *s) { if (a->kind == N_TYPENAME || (a->kind == N_CALL && a->a->kind == N_TYPENAME)) return callee_type(a, s); if (a->kind == N_TYPEX) return resolve_type(a->a, s); // union (T), fn (...) -> T if (is_type_query(a)) return type_query(a, s); errorat(a->pos, "expected a type"); } static Node *typed_node(Type *t, Pos p); static Node *str_node(Str s, Pos p); // A type written as an argument (draw(d, T), ask(T, ...)): the type, with the // node made a type value. static Type * type_value_arg(Node *a, Scope *s) { Type *t = type_arg(a, s); a->info = t; keep_source(a); a->kind = N_TYPENAME; a->type = t_type; return t; } static Type * check_builtin(Node *n, Fn *b, Scope *s, Type *want) { Call *c = new(&perm, 1, Call); c->kind = CALL_FN; c->fn = b; c->builtin = b->builtin; c->nargs = n->list.len; c->args = n->list.data; make_call(n, c); Type *r = t_void; if (macro_only(b->builtin)) { // the std:code questions (writes(f), defs()) are plain functions // that compile-time code calls if (!in_macro_code && !(b->builtin == B_ASK && curmod == code_module)) errorat(n->pos, "%s only works in compile-time code", b->name); need_code_module(n->pos); } iz e = builtin_entry(b->builtin); if (e >= 0 && builtin_table[e].usage.len) { // a fixed signature: each argument checked as its parameter, named // as in the usage Str ps = builtin_table[e].params, u = builtin_table[e].usage; need_args(n, ps.len, u); Str pn = substr(u, indexof(u, S("(")) + 1, u.len); for (iz i = 0; i < ps.len; i++) { iz len = 0; while (len < pn.len && pn.data[len] != ',' && pn.data[len] != ')') len++; check_value(&c->args[i], s, sig_type(ps.data[i]), substr(pn, 0, len)); pn = substr(pn, len + 2 < pn.len ? len + 2 : pn.len, pn.len); } n->type = sig_type(builtin_table[e].result); return n->type; } switch (b->builtin) { case B_PRINT: case B_PRINTLN: case B_STR: for (iz i = 0; i < n->list.len; i++) { if (n->list.data[i]->kind == N_NAMED) errorat(n->list.data[i]->pos, "print takes values, not names"); check_value(&c->args[i], s, 0, S("print")); Type *t = c->args[i]->type; if (t == t_nil) errorat(c->args[i]->pos, "print needs a typed nil"); if (t->kind == TY_VOID) errorat(c->args[i]->pos, "this has no value to print"); if (t->kind == TY_FN) errorat(c->args[i]->pos, "a function cannot be printed"); record_use(S("print"), t, c->args[i]->pos, 0); } r = b->builtin == B_STR ? t_string : t_void; break; case B_LEN: { need_args(n, 1, S("len(x)")); check_value(&c->args[0], s, 0, S("len")); Type *t = c->args[0]->type; while (t->kind == TY_REF) t = deref_auto(&c->args[0]); b32 inreversed = n->pos.file >= 0 && files.data[n->pos.file].builtin && endswith(files.data[n->pos.file].path, S("(generated by reversed)")); if (inreversed && (t->kind == TY_MAP || t->kind == TY_POOL)) errorat(n->pos, "reversed visits a list, string or range, not %s", type_name(t)); if (t->kind != TY_LIST && t->kind != TY_ARRAY && t->kind != TY_MAP && t->kind != TY_POOL) { if (inreversed) errorat(n->pos, "reversed works only on built-in loops (a list, string or range), not %s", type_name(t)); errorat(n->pos, "len needs a list, string, map or pool, not %s", type_name(t)); } r = t_int; break; } case B_ADD: { if (n->list.len < 2) need_args(n, 2, S("add(&list, value) or add(&pool, value)")); Type *t = access_arg(c->args[0], s, S("add(&list, value)")); if (t->text) { // add(&s, values...): a U8 is a byte; anything else adds its printed text for (iz i = 1; i < c->nargs; i++) { check_value(&c->args[i], s, 0, S("add")); record_use(S("print"), c->args[i]->type, c->args[i]->pos, 0); } break; } need_args(n, 2, S("add(&list, value) or add(&pool, value)")); if (t->kind == TY_LIST) { check_value(&c->args[1], s, t->elem, S("add")); } else if (t->kind == TY_POOL) { check_value(&c->args[1], s, t->elem, S("add")); r = ref_of(t->elem); // the Ref it gives keeps the pool alive past its function (main's // locals live to the end anyway) if (!curfn || !equals(curfn->name, S("main")) || curfn->isnested) pin_root(c->args[0]->a); } else if (t->kind == TY_ARRAY) { errorat(n->pos, "%s has a fixed length; add needs a List(T)", type_name(t)); } else { errorat(n->pos, "add needs a list or pool, not %s", type_name(t)); } break; } case B_RESIZE: { need_args(n, 2, S("resize(&list, length)")); Type *t = access_arg(c->args[0], s, S("resize(&list, length)")); if (t->kind != TY_LIST) errorat(n->pos, "resize needs a List(T) or String, not %s", type_name(t)); check_value(&c->args[1], s, t_int, S("the length")); break; } case B_INSERT: { need_args(n, 3, S("insert(&list, index, value)")); Type *t = access_arg(c->args[0], s, S("insert(&list, index, value)")); if (t->kind != TY_LIST) errorat(n->pos, "insert needs a List(T), not %s", type_name(t)); check_value(&c->args[1], s, t_int, S("the index")); check_value(&c->args[2], s, t->elem, S("insert")); break; } case B_REMOVE: { need_args(n, 2, S("remove(&list, index), remove(&pool, ref) or remove(&map, key)")); Type *t = access_arg(c->args[0], s, S("remove(&container, which)")); if (t->kind == TY_LIST) check_value(&c->args[1], s, t_int, S("the index")); else if (t->kind == TY_POOL) check_value(&c->args[1], s, ref_of(t->elem), S("the item")); else if (t->kind == TY_MAP) check_value(&c->args[1], s, t->key, S("the key")); else errorat(n->pos, "remove needs a list, pool or map, not %s", type_name(t)); break; } case B_ABS: need_args(n, 1, S("abs(x)")); check_value(&c->args[0], s, 0, S("abs")); if (!isnumeric(c->args[0]->type)) errorat(n->pos, "abs needs a number"); r = c->args[0]->type; break; case B_LITERAL: { need_args(n, 1, S("literal(value)")); check_value(&c->args[0], s, 0, S("literal")); Type *t = c->args[0]->type; if (t == t_nil) errorat(n->pos, "literal needs a typed value"); r = code_type(S("Expr")); Sym *op = code_sym(S("literal_code")); check_fn(op->fns.data[0]); op->fns.data[0]->used = 1; break; } case B_PLACE: { need_args(n, 2, S("place(target, code)")); Node *t = c->args[0]; // the target names code in the program being compiled, sent as text: // start(x), end(x), before(l), after(l); x alone is start(x); x may // be a path (main.setup, sw.cases) Str position = S("start"); Node *at = t; if (t->kind == N_CALL && t->a->kind == N_NAME && t->list.len == 1 && (equals(t->a->name, S("start")) || equals(t->a->name, S("end")) || equals(t->a->name, S("before")) || equals(t->a->name, S("after")))) { position = t->a->name; at = t->list.data[0]; } Buf pb = {0}; Node *q = at; Str names[16] = {0}; int nn = 0; while (q->kind == N_FIELD && nn < 15) { names[nn++] = q->name; q = q->a; } if (q->kind != N_NAME) { help_text = S("write place(x, code), place(start(x), ...), place(end(x), ...), " "place(before(l), ...) or place(after(l), ...); x is a label, a " "function, a macro label, file, or a path like main.setup"); errorat(t->pos, "place needs a name for where the code goes"); } names[nn++] = q->name; bput(&pb, position); bputc(&pb, ':'); for (int k = nn; k-- > 0;) { bput(&pb, names[k]); if (k) bputc(&pb, '.'); } keep_source(t); t->kind = N_STR; t->sval = bstr(&pb); t->list = (Nodes){0}; t->a = 0; check_value(&c->args[0], s, t_string, S("the target")); // the code as Stmt, printed as Star99; what it declares keeps its names // (placing code adds visible names; no hygiene renaming) if (c->args[1]->kind == N_TEMPLATE) c->args[1]->flags |= F_RAW; check_value(&c->args[1], s, code_type(S("Stmt")), S("the placed code")); Node *src = call_node(name_node(builtin_sym(S("str")), n->pos), n->pos); addnode(&src->list, c->args[1]); check_expr(src, s, t_string); if (src->type != t_string) errorat(c->args[1]->pos, "place needs code"); c->args[1] = src; break; } case B_HERE: need_args(n, 0, S("here()")); r = code_type(S("Site")); break; case B_ERROR: case B_WARNING: { if (!n->list.len) { help_text = S("write error(at, values...)"); errorat(n->pos, "error needs the code it is about and a message"); } check_value(&c->args[0], s, 0, S("where")); if (c->args[0]->type != code_type(S("Site"))) { // any code value: its site Node *at = call_node(name_node(code_sym(S("site_of")), n->pos), n->pos); addnode(&at->list, c->args[0]); c->args[0] = at; check_value(&c->args[0], s, code_type(S("Site")), S("where")); } for (iz i = 1; i < c->nargs; i++) check_value(&c->args[i], s, 0, S("error")); break; } case B_DRAW: { // draw(d, a..args[0], s, t_draw, S("the draw")); Node *x = c->args[1]; if (x->kind == N_RANGE) { check_expr(x, s, 0); r = t_int; } else if (x->kind == N_TYPENAME || (x->kind == N_CALL && x->a->kind == N_TYPENAME)) { r = type_value_arg(x, s); record_use(S("fuzz"), r, x->pos, 0); } else { Type *lt = check_expr(x, s, 0); if (x->kind == N_RANGE) { // a macro returned range syntax r = t_int; } else { if (lt->kind != TY_LIST && lt->kind != TY_ARRAY) errorat(x->pos, "draw chooses from a range, a list or a type, not %s", type_name(lt)); r = lt->elem; } } break; } case B_EXPANDED: { need_args(n, 1, S("expanded(b: Build)")); Sym *bt = prelude_sym(S("Build")); check_value(&c->args[0], s, bt ? bt->type : 0, S("the build")); r = t_string; break; } case B_TYPE_OF: case B_ARGS_OF: case B_RET_OF: { // a Type value; the argument is checked, not run Node call = *n; call.kind = N_CALL; call.list.data = c->args; call.list.len = c->nargs; c->args = 0; c->nargs = 0; n->info = c; n->sval = (Str){0}; Type *t = type_query(&call, s); c->fn = 0; n->kind = N_TYPENAME; n->info = t; n->type = t_type; return t_type; } case B_OVERLOAD_OF: { // the chosen function, as a function value Node call = *n; call.list.data = c->args; call.list.len = c->nargs; Node *callee = mknode(N_NAME, n->pos); callee->name = S("overload_of"); call.a = callee; call.kind = N_CALL; Fn *f = named_fn(&call, s); Sym *one = mksym(S_FN, f->name, n->pos); *push(&perm, &one->fns) = f; n->kind = N_NAME; n->name = f->name; n->sym = one; n->list = (Nodes){0}; n->flags |= F_FNVALUE; return check_expr(n, s, want); } case B_FORMAT: need_args(n, 1, S("format(text)")); check_value(&c->args[0], s, t_string, S("the text")); r = t_string; break; case B_ASK: { // ask(T, "question", args...): the compiler answers with a T if (n->list.len < 2) errorat(n->pos, "ask(T, question, values...)"); r = type_value_arg(c->args[0], s); if (c->args[1]->kind != N_STR) errorat(c->args[1]->pos, "the question is a string literal"); check_value(&c->args[1], s, t_string, S("the question")); for (iz i = 2; i < c->nargs; i++) check_value(&c->args[i], s, 0, S("ask")); break; } case B_WITH_MAIN: { // with_main(b, name): the build of the program started at another // function, asked of the compiler as ask(Build, "with_main", b, name) need_args(n, 2, S("with_main(b, name)")); Sym *bt = prelude_sym(S("Build")); Pos at = n->a && n->a->pos.file >= 0 ? n->a->pos : n->pos; // the name: errors point there Node *q = call_node(name_node(builtin_sym(S("ask")), at), at); Node *tx = mknode(N_TYPEX, at); tx->a = typed_node(bt->type, at); addnode(&q->list, tx); addnode(&q->list, str_node(S("with_main"), at)); addnode(&q->list, c->args[0]); addnode(&q->list, c->args[1]); *n = *q; return check_expr(n, s, want); } case B_ENCODE: // a value as the text values cross between programs in (templates) need_args(n, 1, S("code__encode(value)")); check_value(&c->args[0], s, 0, S("the value")); r = t_string; break; case B_DECODE: // and back need_args(n, 2, S("code__decode(T, text)")); r = type_value_arg(c->args[0], s); check_value(&c->args[1], s, t_string, S("the text")); break; case B_KNOWN: { // known(question(...)): its answer, or nil while it is not final need_args(n, 1, S("known(question(...))")); if (c->args[0]->kind != N_CALL) errorat(c->args[0]->pos, "known takes one question, such as known(writes(scan))"); Type *t = check_value(&c->args[0], s, 0, S("the question")); if (!t || t == t_void) errorat(c->args[0]->pos, "this question gives no answer"); Type *u = mktype(TY_UNION); // union (T), nil while not final Field fl = {0}; fl.type = t; *push(&perm, &u->fields) = fl; u->resolved = 1; r = intern(u); compute_flags(r, 0); coerce(&c->args[0], r, S("the answer")); break; } case B_FAIL: need_args(n, 1, S("fail(message)")); check_value(&c->args[0], s, t_string, S("the message")); break; case B_YIELD: { need_args(n, 1, S("yield(value)")); Coro *co = yield_target(s, n->pos); check_value(&c->args[0], s, co->yields, S("yield")); break; } default: errorat(n->pos, "this built-in is only available in compile-time code"); } n->type = r; return r; } // Statements static b32 diverges(Node *n) { if (!n) return 0; switch ((int)n->kind) { case N_RETURN: case N_BREAK: case N_CONTINUE: return 1; case N_EXPR: // a switch, if or block that became the value of its block if (n->a->kind == N_SWITCHX || n->a->kind == N_BLOCKX) return diverges(n->a->a); if (n->a->kind == N_IFX) return n->a->b->kind == N_BLOCKX && n->a->c->kind == N_BLOCKX && diverges(n->a->b->a) && diverges(n->a->c->a); if (n->a->kind == N_CALL && n->a->a->kind == N_NAME) // as written: star99 fmt return equals(n->a->a->name, S("fail")) || equals(n->a->a->name, S("exit")) || equals(n->a->a->name, S("unreachable")); if (n->a->kind == (NodeKind)X_CALLFN) { Call *c = n->a->info; return c->kind == CALL_FN && c->fn && (c->builtin == B_EXIT || c->builtin == B_FAIL); // they do not come back } return 0; case N_BLOCK: return n->list.len && diverges(n->list.data[n->list.len - 1]); case N_IF: return n->c && diverges(n->b) && diverges(n->c); case N_SWITCH: { b32 hasdefault = 0; for (iz i = 0; i < n->list.len; i++) { Node *cs = n->list.data[i]; if (!cs->list.len) hasdefault = 1; if (!diverges(cs->a)) return 0; } return hasdefault || (n->flags & F_COMPLETE); // or every case is there } case N_WHILE: return n->a->kind == N_BOOL && n->a->ival && !n->ival; default: return 0; } } static Sym * declare_local(Scope *s, Str name, Pos p, Type *t, b32 readonly) { Sym *sym = mksym(S_VAR, name, p); sym->type = t; sym->owner = scope_fn(s); sym->readonly = readonly; sym->cname = newcname(S("v"), name); declare(s, sym); *push(&perm, &s->order) = sym; Fn *f = scope_fn(s); if (f) *push(&perm, &f->locals) = sym; return sym; } // One namespace: a label and the bindings of a loop or case live inside the // braces they name, so a local declared directly in those braces may not // take one of their names. Deeper braces hide them, as they hide any name. // reversed (a prelude macro) declares its element so: that is the binding of // its label, as in a forward loop. static b32 braces_name(Scope *s, Str name, Pos p) { for (Scope *sc = s; sc && (sc->kind == SC_BLOCK || sc->kind == SC_LOOP); sc = sc->parent) { if ((sc != s && sc->block) || sc->holds == 2) return 0; // outer braces if (sc->holds != 1) continue; Sym *o = scope_get(sc, name); Node *cs = sc->loop && sc->loop->kind == N_CASE && equals(sc->loop->name, name) ? sc->loop : 0; if (!o && !cs) continue; if (p.file >= 0 && p.file < files.len && files.data[p.file].builtin) return 1; notes.len = 0; note(o ? o->pos : cs->pos, !o || o->kind == S_LABEL ? S("first declared here, as a label") : o->bound_by == 's' ? S("first declared here, as a binding") : S("first declared here, as a loop binding")); errorat(p, "%s is already declared", name); } return 0; } static Scope * find_loop(Scope *s, Str label, Pos p, b32 iscontinue) { for (Scope *sc = s; sc; sc = sc->parent) { if (sc->kind == SC_FN) break; Node *lp = sc->loop; if (!lp) continue; Node *inner = lp->kind == N_LABELED ? lp->a : lp; b32 isloop = sc->kind == SC_LOOP; if (!label.len) { if (isloop) return sc; continue; } // a label, either name of a for loop, a switch binding, a case label b32 match = ((lp->kind == N_LABELED || lp->kind == N_SWITCH || lp->kind == N_CASE) && equals(lp->name, label)) || (isloop && inner->kind == N_FOR && (equals(inner->name, label) || equals(inner->sval, label))); if (!match) continue; // a variable declared inside hides the label, as it hides any name for (Scope *in = s; in != sc; in = in->parent) { Sym *v = scope_get(in, label); if (!v || v->kind != S_VAR || v->bound_by) continue; notes.len = 0; note(v->pos, S("the variable, which hides the label")); errorat(p, "%s is a variable here, not a label", label); } if (iscontinue && (lp->kind == N_SWITCH || lp->kind == N_CASE || (lp->kind == N_LABELED && lp->a->kind == N_SWITCH))) errorat(p, "continue %s needs a loop, but %s names a %s", label, label, lp->kind == N_CASE ? S("case") : S("switch")); if (lp->kind == N_CASE) return sc->parent; // its end is the end of the switch if (iscontinue && !isloop && (lp->flags & F_EXPANDED)) { help_text = fmtstr("break %s leaves it", label); errorat(p, "continue %s cannot reach a loop made by for_expansion", label); } if (iscontinue && !isloop) errorat(p, "continue %s needs a loop, but %s labels a block", label, label); return sc; } if (label.len) errorat(p, "no enclosing loop or block is named %s", label); errorat(p, "%s is only allowed inside a loop", iscontinue ? S("continue") : S("break")); } static void check_body(Node *b, Scope *s); static void check_for(Node *n, Scope *s); static void filtered_loop(Node *n); static void process_import(Node *d, i32 file, Scope *into); // Imports at the start of a block are allowed; later ones are errors. static void mark_block_imports(Node *b) { for (iz i = 0; i < b->list.len && b->list.data[i]->kind == N_IMPORT; i++) b->list.data[i]->flags |= F_GENERATED; } static void check_local_fn(Node *n, Scope *s); static void unused_value(Node *n) { Node *e = n->a; if (e->type && e->type->kind != TY_VOID && e->type != t_error) { help_text = S("write _ = value to discard it"); errorat(n->pos, e->kind == (NodeKind)X_CALLFN ? "the result of this call is unused" : "this value is unused"); } } // The statements of a block, in scope s. Each is found again after it is // checked: code placed there may move it. static void check_stmts(Node *b, Scope *s) { for (iz i = 0; i < b->list.len;) { Node *st = b->list.data[i]; if (expand_stmt_at(b, &i, s)) continue; check_stmt(st, s); i = index_in(b, st) + 1; } } static void check_stmt(Node *n, Scope *s) { // in compile-time code, macro if, for and switch are the ordinary ones if (in_macro_code && (n->kind == N_IF || n->kind == N_FOR || n->kind == N_SWITCH)) n->flags &= ~(u32)F_MACRO; switch ((int)n->kind) { case N_VAR: { Type *t = n->a ? resolve_type(n->a, s) : 0; if (n->b) { check_value(&n->b, s, t, n->name); if (!t) { t = n->b->type; if (t == t_nil) errorat(n->b->pos, "nil needs a type; write var %s: T = nil", n->name); if (t->kind == TY_VOID) errorat(n->b->pos, "this has no value to store"); if (t == t_range) errorat(n->b->pos, "a range is not a value"); } } b32 binding = braces_name(s, n->name, n->pos); Sym *sym = declare_local(s, n->name, n->pos, t, (n->flags & F_CONST) != 0); if (binding) sym->bound_by = 'f'; sym->isconst = (n->flags & F_CONST) != 0; sym->decl = n; n->sym = sym; n->type = t; return; } case N_ASSIGN: { // writing a variable is not a use of it Sym *target = n->a->kind == N_NAME ? find(s, n->a->name, n->a->pos, 0) : 0; b32 was_used = target && target->used; check_expr(n->a, s, 0); if (target && target->kind == S_VAR && !target->isglobal) target->used = was_used; int place = place_kind(n->a); if (place == NOT_PLACE) errorat(n->pos, "the left side of %s must be a variable, field or element", tokname[n->op]); if (place == READONLY_PLACE) { if (n->a->kind == N_NAME && n->a->sym->isconst) errorat(n->pos, "%s is a constant", n->a->sym->name); if (n->a->kind == N_NAME && n->a->sym->isparam) errorat( n->pos, "%s is a parameter and cannot change; to change the caller's, make it var %s", n->a->sym->name, n->a->sym->name); Node *root = root_of(n->a); Sym *rs = root->kind == N_NAME ? root->sym : 0; if (rs && rs->isparam) { help_text = fmtstr("to change the caller's, make it var %s", rs->name); errorat(n->pos, "%s is a parameter, so it cannot change", rs->name); } if (rs && rs->bound_by == 's') { help_text = fmtstr("to change it in place, write switch &%s", rs->name); errorat(n->pos, "%s is a copy this case made, so it cannot change", rs->name); } if (rs && rs->bound_by == 'f') { help_text = S( "to change the elements, loop over the indexes: for i in 0..pos, "%s is the loop element, so it cannot change", rs->name); } if (rs && rs->isconst) errorat(n->pos, "%s is a constant", rs->name); errorat(n->pos, "this cannot change here"); } Type *lt = n->a->type; check_value(&n->b, s, lt, S("the assignment")); if (n->op != T_EQ) { if (!isnumeric(lt)) errorat(n->pos, "%s needs numbers", tokname[n->op]); if (n->op == T_PERCENTEQ && lt->kind != TY_INT) errorat(n->pos, "%%= needs integers"); } if (n->b->kind == N_ADDR) pin_root(n->b->a); return; } case N_DISCARD: check_expr(n->a, s, 0); if (n->a->type->kind == TY_VOID) errorat(n->pos, "this has no value to discard"); return; case N_EXPR: check_expr(n->a, s, 0); unused_value(n); return; case N_IF: check_value(&n->a, s, t_bool, S("if")); check_body(n->b, s); if (n->c) check_body(n->c, s); return; case N_WHILE: { check_value(&n->a, s, t_bool, S("while")); Scope *ls = mkscope(s, SC_LOOP); ls->loop = n->info ? (Node *)n->info : n; loop_depth++; check_body(n->b, ls); loop_depth--; return; } case N_FOR: if (n->flags & F_FILTER) { filtered_loop(n); check_stmt(n, s); return; } check_for(n, s); return; case N_BLOCK: { Scope *bs = mkscope(s, SC_BLOCK); check_body(n, bs); return; } case N_LABELED: { Node *inner = n->a; Scope *ls = mkscope(s, SC_BLOCK); if (inner->kind != N_FOR && inner->kind != N_WHILE) ls->loop = n; ls->holds = !(n->flags & F_EXPANDED); // a for_expansion declares the loop names inside // one rule: no visible variable may share its name (locals, those of // enclosing functions, globals); a loop's own binding is not visible yet Sym *same = find(s, n->name, n->pos, 0); if (same && same->kind == S_VAR) { notes.len = 0; if (same->decl) { note(same->decl->pos, S("the variable")); } errorat(n->pos, "the label %s has the name of a variable", n->name); } if (n->name.len && part_name(n->name)) { // main.body, sw.cases: paths for place help_text = S("params, result, body, source, condition, subject and cases name parts of code"); errorat(n->pos, "a label cannot be named %s, which is the name of a part", n->name); } // one namespace: the label is a name inside what it labels (as a // for loop binding is), so it is not a value there if (n->name.len) { Sym *l = mksym(S_LABEL, n->name, n->pos); l->target = n; declare(ls, l); } if (inner->kind == N_FOR || inner->kind == N_WHILE) { inner->info = n; check_stmt(inner, ls); } else { Scope *bs = mkscope(ls, SC_BLOCK); check_body(inner, bs); } return; } case N_RETURN: { Fn *f = scope_fn(s); if (!f) errorat(n->pos, "return is only allowed inside a function"); if (defer_scope && scope_fn(defer_scope) == f) errorat(n->pos, "return cannot leave a deferred statement"); Type *rt = f->type ? f->type->elem : 0; if (f->infer && !rt) errorat( n->pos, "a function whose result is inferred returns its value with its body; write -> Type to use return"); if (n->a) { if (rt == t_void) errorat(n->pos, "%s returns nothing, so return takes no value", f->name); check_value(&n->a, s, rt, S("return")); if (n->a->kind == N_ADDR) pin_root(n->a->a); } else if (rt && rt != t_void) { errorat(n->pos, "%s returns a %s", f->name, type_name(rt)); } return; } case N_BREAK: case N_CONTINUE: { Scope *target = find_loop(s, n->name, n->pos, n->kind == N_CONTINUE); for (Scope *sc = s;; sc = sc->parent) { if (sc == defer_scope) errorat(n->pos, "%s cannot leave a deferred statement", n->kind == N_BREAK ? S("break") : S("continue")); if (sc == target) break; if (sc->isvalue) errorat(n->pos, "%s cannot leave a block whose value is used", n->kind == N_BREAK ? S("break") : S("continue")); } n->info = target->loop; if (target->loop && target->loop->kind != N_SWITCH) target->loop->ival = 1; // has a break (a switch keeps its kind there) return; } case N_DEFER: { // it runs as its block is left, so jumps stay inside it Scope *save = defer_scope; defer_scope = s; check_stmt(n->a, s); defer_scope = save; return; } case N_SWITCH: check_switch(n, s, 0, 0); return; case N_FN: check_local_fn(n, s); return; case N_IMPORT: if (!(n->flags & F_GENERATED)) errorat(n->pos, "an import inside a block comes before its statements"); process_import(n, n->pos.file, s); return; case N_MLABEL: { // macro label x = { ... }: place(x, ...) puts code into its braces; // they are not a scope, so what is declared there is seen after them Sym *l = mksym(S_MLABEL, n->name, n->pos); l->scope = s; l->target = n; declare(s, l); *push(&perm, &mlabels) = (MLabel){n, s}; check_stmts(n->a, s); mlabels.len--; return; } case N_STRUCT: case N_UNION: errorat(n->pos, "types are declared at file scope"); case N_DECLS: errorat(n->pos, "this group belongs at file scope"); default: errorat(n->pos, "this does not belong in a function body"); } } static void check_body(Node *b, Scope *s) { if (b->kind != N_BLOCK) { check_stmt(b, s); return; } Scope *bs = s->kind == SC_BLOCK && s->block == b ? s : mkscope(s, SC_BLOCK); bs->block = b; mark_block_imports(b); check_stmts(b, bs); } // Whether a block ends in an expression. static b32 ends_in_value(Node *b) { Node *last = b->list.len ? b->list.data[b->list.len - 1] : 0; if (!last) return 0; if (last->kind == N_IF) return last->c && ends_in_value(last->b) && ends_in_value(last->c); return last->kind == N_EXPR; } // A block whose value is used ends without one NORETURN static void missing_value(Pos at, Scope *s, Type *want) { if (s->parent && s->parent->kind == SC_FN && want) { // the body of a function help_text = fmtstr("end it with return and a %s, or with a %s as its last line", type_name(want), type_name(want)); errorat(at, "this function can reach its end without giving a %s", type_name(want)); } errorat(at, "this block needs a value; end it with an expression"); } // Code placed at the end of a block while its last statement was checked static void placed_after(Node *b, Node *last, Scope *s, b32 value) { for (iz k = position_in(b, last) + 1; k > 0 && k < b->list.len; k++) { if (value) errorat(b->list.data[k]->pos, "code placed at the end of this block would come after its value"); check_stmt(b->list.data[k], s); } } // A block whose last expression is its value. static Type * check_block_value(Node *b, Scope *s, Type *want, b32 needvalue) { s->block = b; s->isvalue = needvalue && want != t_void && s->kind != SC_FN; mark_block_imports(b); iz i = 0; while (i < b->list.len) { Node *st = b->list.data[i]; if (expand_stmt_at(b, &i, s)) continue; if (i + 1 == b->list.len) break; // the last one may give the value check_stmt(st, s); i = index_in(b, st) + 1; } iz n = b->list.len; if (i == n && n) { // a macro at the end produced nothing: every statement is checked Node *last = b->list.data[n - 1]; if (needvalue && want != t_void && !diverges(last)) missing_value(last->pos, s, want); return t_void; } if (!n) { if (needvalue && want != t_void) missing_value(b->pos, s, want); return t_void; } Node *last = b->list.data[n - 1]; // an inferred result (want 0) takes an ending if with else as the value b32 ending_if = !want && last->kind == N_IF && last->c && ends_in_value(last->b) && ends_in_value(last->c); if ((needvalue && want != t_void) || ending_if) { // a switch, if or block ending a value block gives its value Node *e = 0; if (last->kind == N_SWITCH) { e = mknode(N_SWITCHX, last->pos); last->flags |= F_DO; e->a = last; } else if (last->kind == N_IF && last->c) { e = mknode(N_IFX, last->pos); e->a = last->a; e->b = mknode(N_BLOCKX, last->b->pos); e->b->a = last->b; e->c = mknode(N_BLOCKX, last->c->pos); e->c->a = last->c; } else if (last->kind == N_BLOCK) { e = mknode(N_BLOCKX, last->pos); e->a = last; } if (e) { Node *x = mknode(N_EXPR, last->pos); x->a = e; b->list.data[n - 1] = last = x; } } Node *call = last->kind == N_EXPR && last->a->kind == N_CALL ? last->a->a : 0; if (call && call->kind == N_NAME && (equals(call->name, S("fail")) || equals(call->name, S("exit")))) { check_stmt(last, s); // ends the program instead of giving the value if (diverges(b)) return want ? want : t_void; } if (last->kind == N_EXPR) { check_value(&last->a, s, want == t_void ? 0 : want, S("the value")); if (want == t_void) unused_value(last); b->type = last->a->type; placed_after(b, last, s, needvalue && want != t_void); return b->type; } if (needvalue && want != t_void && want && last->kind == N_IF && !last->c && ends_in_value(last->b)) { // if n > 0 do 1 at the end: nothing when the condition fails help_text = fmtstr("add else and the %s for the other case", type_name(want)); errorat(last->pos, "this if gives a value only when its condition holds"); } check_stmt(last, s); placed_after(b, last, s, 0); if (needvalue && !diverges(last) && want != t_void) missing_value(last->pos, s, want); return want ? want : t_void; } // for x: T in xs { body } is label x = for each in xs { switch x in each { // case T: body; default: } }, the switch keeping no label of its own (break // x and continue x are for the loop). A pool gives references: each.*. static void filtered_loop(Node *n) { static STATE int count = 0; keep_source(n); Node *loop = mknode(N_FOR, n->pos); // a name no program declares (past every name___N it has), as the // labels of for_expansion loop->name = fmtstr("each___%lld", fresh_base + 2000000 + ++count); loop->a = n->a; Node *sw = mknode(N_SWITCH, n->pos); sw->name = n->name; sw->flags |= F_FILTER; Node *subject = mknode(N_NAME, n->pos); subject->name = loop->name; sw->a = subject; Node *cs = mknode(N_CASE, n->pos); Node *pat = mknode(N_TYPEX, n->c->pos); pat->a = n->c; addnode(&cs->list, pat); cs->a = n->b; Node *dflt = mknode(N_CASE, n->pos); dflt->a = mknode(N_BLOCK, n->pos); addnode(&sw->list, cs); addnode(&sw->list, dflt); Node *body = mknode(N_BLOCK, n->pos); addnode(&body->list, sw); loop->b = body; n->kind = N_LABELED; n->a = loop; n->b = n->c = 0; n->flags &= ~F_FILTER; } static void check_for(Node *n, Scope *s) { Scope *ls = mkscope(s, SC_LOOP); ls->loop = n->info ? (Node *)n->info : n; ls->holds = 1; Node *src = n->a; Type *bt = 0, *bt2 = 0; check_expr(src, s, 0); // a macro may turn the source into a range if (src->kind == N_RANGE) { bt = t_int; if (n->sval.len) errorat(n->pos, "a range gives one value per step"); } else { Type *t = deref_auto(&n->a); src = n->a; switch (t->kind) { case TY_LIST: case TY_ARRAY: bt = t->elem; if (n->sval.len) { bt2 = bt; bt = t_int; } break; case TY_MAP: bt = t->key; if (n->sval.len) bt2 = t->elem; break; case TY_POOL: if (n->sval.len) errorat(n->pos, "a pool gives one value per item: a reference to it"); bt = ref_of(t->elem); break; case TY_BOOL: errorat(n->pos, "for loops over a sequence; use while for a condition"); default: if (t->kind == TY_STRUCT || t->kind == TY_UNION) help_text = fmtstr("to loop over a %s, define macro fn for_expansion(v: Expr(%s), " "body: Code) -> Code", type_name(t), type_name(t)); errorat(src->pos, "for needs a list, string, map, pool or range, not %s", type_name(t)); } } Sym *b = declare_local(ls, n->name, n->pos, bt, 1); b->bound_by = 'f'; n->sym = b; if (n->sval.len) { Sym *b2 = declare_local(ls, n->sval, n->pos, bt2, 1); b2->bound_by = 'f'; n->sym2 = b2; // the second binding } loop_depth++; Scope *body = mkscope(ls, SC_BLOCK); check_body(n->b, body); loop_depth--; } // Switch enum { SW_UNION, SW_ANY, SW_VALUE }; typedef struct { struct { iz *data; iz len; iz cap; } variants; // SW_UNION: variant indices this case handles Type **types; // SW_ANY: the types this case handles iz ntypes; b32 isnil; b32 isdefault; Sym *binding; b32 payload; // the binding is the variant payload } CaseInfo; static Type * check_switch(Node *n, Scope *s, Type *want, b32 asvalue) { check_expr(n->a, s, 0); Type *t = n->a->type; if (t->kind == TY_REF && (n->flags & F_FILTER)) t = deref_auto(&n->a); // a pool item resolve_named(t); int kind = isunion(t) ? SW_UNION : t->kind == TY_ANY ? SW_ANY : SW_VALUE; if (kind == SW_VALUE && (n->flags & F_FILTER)) errorat(n->a->pos, "for x: T in xs keeps the elements of one variant, so they must be a " "union or Any, not %s", with_articles(fmtstr("a %s", type_name(t)))); if (kind == SW_VALUE && t->kind != TY_INT && !t->text && t->kind != TY_BOOL) errorat(n->a->pos, "switch needs a union, Any, integer, string or Bool, not %s", type_name(t)); if (n->flags & F_REF) { if (place_kind(n->a) != PLACE) errorat(n->a->pos, "switch & needs something that can change"); if (kind == SW_VALUE) errorat(n->pos, "switch & needs a union or an Any, not %s", with_articles(fmtstr("a %s", type_name(n->a->type)))); } n->ival = kind; // switch s in v names its braces s, as label s = switch v does Scope *sws = mkscope(s, SC_BLOCK); if (!(n->flags & F_FILTER)) sws->loop = n; // also where break c for label c = case ... goes sws->holds = 2; b32 *seen = new(&perm, t->fields.len + 1, b32); b32 seen_nil = 0, hasdefault = 0, seen_true = 0, seen_false = 0; Map *seen_values = 0; // value cases: their constant, so none comes twice Type **results = new(&perm, n->list.len + 1, Type *); for (iz ci = 0; ci < n->list.len; ci++) { Node *cs = n->list.data[ci]; CaseInfo *info = new(&perm, 1, CaseInfo); cs->info = info; Scope *cscope = mkscope(sws, SC_BLOCK); if (cs->name.len) cscope->loop = cs; // label c = case ...: names the case cscope->holds = 1; if (!cs->list.len) { if (hasdefault) errorat(cs->pos, "switch has two defaults"); hasdefault = 1; info->isdefault = 1; } else if (hasdefault) { help_text = S("move default: after the cases"); errorat(cs->pos, "default comes last in a switch"); } Type *bindtype = t; for (iz pi = 0; pi < cs->list.len; pi++) { Node *pat = cs->list.data[pi]; if (pat->kind == N_NIL) { if (kind == SW_UNION && t->nonil) errorat(pat->pos, "%s is no_nil, so it is never nil", type_name(t)); if (kind == SW_VALUE) errorat(pat->pos, "a %s is never nil", type_name(t)); if (seen_nil) errorat(pat->pos, "nil is handled twice"); seen_nil = 1; info->isnil = 1; continue; } if (kind == SW_UNION) { iz vi = -1; if (pat->kind == N_VARIANT && !(pat->flags & F_CALLED)) { vi = variant_index(t, pat->name); if (vi < 0) no_member(pat->pos, t, pat->name); } else if (pat->kind == N_TYPEX) { Type *pt = resolve_type(pat->a, s); if (pat->a->info) { VariantRef *vr = pat->a->info; if (vr->u != t) errorat(pat->pos, "this variant belongs to %s, not %s", type_name(vr->u), type_name(t)); vi = vr->index; } else { for (iz k = 0; k < t->fields.len; k++) { if (!t->fields.data[k].name.len && t->fields.data[k].type == pt) vi = k; } if (vi < 0) errorat(pat->pos, "%s has no variant of type %s", type_name(t), type_name(pt)); } } else { errorat(pat->pos, "a case of a union switch names a variant: case .name: or case Type:"); } if (seen[vi]) errorat(pat->pos, "%s is handled twice", variant_name(t, vi)); seen[vi] = 1; *push(&perm, &info->variants) = vi; } else if (kind == SW_ANY) { if (pat->kind != N_TYPEX) errorat(pat->pos, "a case of an Any switch names a type; write case Int:"); Type *pt = resolve_type(pat->a, s); Type **nt = new(&perm, info->ntypes + 1, Type *); if (info->ntypes) memcpy(nt, info->types, (size_t)info->ntypes * sizeof(Type *)); nt[info->ntypes++] = pt; info->types = nt; bindtype = pt; } else { check_value(&cs->list.data[pi], s, t, S("the case")); if (!is_constant(cs->list.data[pi], s)) errorat(pat->pos, "a case value must be a constant"); Node *cv = cs->list.data[pi]; i64 iv = 0; Str key = t == t_bool ? fmtstr("b%d", (int)!!cv->ival) : cv->kind == N_STR ? fmtstr("s%s", cv->sval) : const_int(cv, s, &iv) ? fmtstr("i%lld", (long long)iv) : (Str){0}; Node **first = key.len ? (Node **)upsert(&seen_values, key, 1) : 0; if (first && *first) { notes.len = 0; note((*first)->pos, S("handled here first")); errorat(pat->pos, "%s is handled twice", node_source(pat)); } if (first) *first = cv; if (t == t_bool) { if (cs->list.data[pi]->ival) seen_true = 1; else seen_false = 1; } } } if (kind == SW_UNION && info->variants.len == 1 && cs->list.len == 1) { Field *f = t->fields.data + info->variants.data[0]; if (f->type && f->type != t_void) { bindtype = f->type; info->payload = 1; } } if (kind == SW_ANY && info->ntypes != 1) bindtype = t; if (n->name.len && kind != SW_VALUE) { Sym *b = declare_local(cscope, n->name, cs->pos, bindtype, !(n->flags & F_REF)); b->bound_by = 's'; if (n->flags & F_REF) b->isvar = 1; // a payload is in a box of its own: a reference to it is checked b->boxroute = (n->flags & F_REF) && info->payload && kind == SW_UNION; info->binding = b; } if (asvalue) { results[ci] = check_block_value(cs->a, cscope, want, 1); } else { check_body(cs->a, cscope); } } if (!hasdefault) { if (kind == SW_UNION) { // every case missing, and the lines that add them Str *names = new(&perm, t->fields.len + 1, Str); Buf cases = {0}; iz missing = 0; for (iz v = 0; v <= t->fields.len; v++) { b32 isnil = v == t->fields.len; if (isnil ? t->nonil || seen_nil : seen[v]) continue; names[missing++] = isnil ? S("nil") : variant_name(t, v); b32 dot = !isnil && t->fields.data[v].name.len; // case .circle:, case Int: bputf(&cases, "\ncase %s%s:", dot ? S(".") : S(""), names[missing - 1]); } if (missing) { help_text = fmtstr("add %s, or default:%s", missing > 1 ? S("these cases") : S("this case"), bstr(&cases)); errorat(n->pos, "this switch does not handle %s", join_and(names, missing)); } n->flags |= F_COMPLETE; // complete without default } else if (kind == SW_ANY) { errorat(n->pos, "a switch on Any needs default:"); } else if (!(t == t_bool && seen_true && seen_false)) { errorat(n->pos, "this switch needs default:"); } else { n->flags |= F_COMPLETE; // true and false: complete } } if (!asvalue) return t_void; if (want) return want; // cases of two number types: an error, as in if values and lists for (iz i = 0, first = -1; i < n->list.len; i++) { Node *body = n->list.data[i]->a; if (diverges(body) || !body->list.len) continue; Node *last = body->list.data[body->list.len - 1]; if (last->kind != N_EXPR || !isnumeric(last->a->type)) continue; if (first < 0) { first = i; continue; } Node *fb = n->list.data[first]->a; Node *fl = fb->list.data[fb->list.len - 1]; Type *a = fl->a->type, *b = last->a->type; if (a == b) continue; if (last->a->kind == N_INT || (last->a->kind == N_UNARY && last->a->a->kind == N_INT)) { coerce(&last->a, a, S("this case")); // as in [2.5, 1]: the literal takes the type results[i] = a; continue; } if (fl->a->kind == N_INT) help_text = fmtstr("write %lld.0", (long long)fl->a->ival); errorat(last->a->pos, "the first case gives %s, so this one must too, but it gives %s", with_articles(fmtstr("a %s", type_name(a))), with_articles(fmtstr("a %s", type_name(b)))); } // the cases that give a value; one that returns or breaks gives none iz nr = 0; for (iz i = 0; i < n->list.len; i++) { if (!diverges(n->list.data[i]->a)) results[nr++] = results[i]; } if (!nr) return t_void; b32 same = 1; for (iz i = 0; i < nr; i++) same &= results[i] == results[0]; if (same) return results[0]; Type *u = anon_union(results, nr); for (iz i = 0; i < n->list.len; i++) { Node *body = n->list.data[i]->a; Node *last = body->list.data[body->list.len - 1]; if (last->kind == N_EXPR && !diverges(body)) coerce(&last->a, u, S("this case")); } return u; } // Functions static STATE struct { Fn **data; iz len; iz cap; } all_fns = {0}, closures = {0}; static STATE struct { Coro **data; iz len; iz cap; } coroutines = {0}; static void co_signature(Coro *co); static void co_analyze(Coro *co); static void fn_signature(Fn *f) { if (f->type || f->builtin) return; Fn *save_sig = signature_of; Pos save_at = signature_at; signature_of = f; signature_at = need_at; fn_signature_of(f); signature_of = save_sig; signature_at = save_at; } static void fn_signature_of(Fn *f) { if (f->co && f == f->co->resume) { co_signature(f->co); return; } if (f->checking) errorat(f->decl->pos, "%s uses its own result before it is known; write -> Type", f->name); Node *d = f->decl; Scope *s = f->scope; Fields params = {0}; for (iz i = 0; i < d->list.len; i++) { Node *pn = d->list.data[i]; Field fl = {0}; fl.name = pn->name; fl.pos = pn->pos; fl.isvar = (pn->flags & F_VAR) != 0; fl.israw = (pn->flags & F_RAW) != 0; fl.def = pn->b; if (fl.isvar && fl.def) errorat(pn->pos, "a var parameter cannot have a default"); if (pn->a) { fl.type = resolve_type(pn->a, s); if (pn->a->kind == N_TNAME && equals(pn->a->name, S("Expr")) && pn->a->list.len == 1) fl.gives = resolve_type(pn->a->list.data[0], s); if (pn->a->info && pn->a->kind == N_TNAME) { uptr *k = new(&perm, 2, uptr); *upsert(¶m_variants, pvariant_key(k, f, i), 1) = pn->a->info; // VariantRef } } if (fl.def) { if (!is_constant(fl.def, s)) errorat( fl.def->pos, "a default must be a constant: a literal, a named constant, or constant arithmetic"); check_value(&pn->b, s, fl.type, fl.name); fl.def = pn->b; if (!fl.type) fl.type = pn->b->type; } if (!fl.type) errorat(pn->pos, "%s needs a type", pn->name); for (iz j = 0; j < params.len; j++) { if (equals(params.data[j].name, fl.name)) errorat(pn->pos, "two parameters are named %s", fl.name); } *push(&perm, ¶ms) = fl; } Type *result = d->a ? resolve_type(d->a, s) : 0; if (!result) { if ((d->flags & F_DO) && !(d->flags & F_COROUTINE) && !c_function(f)) { f->infer = 1; // The result comes from the body: check it now. f->checking = 1; Type *tmp = mktype(TY_FN); tmp->fields = params; tmp->elem = 0; f->type = tmp; check_now(f, need_at.file >= 0 ? need_at : f->decl->pos, !f->ismacro); return; } result = t_void; } f->type = fn_type(params, result); } static void declare_params(Fn *f, Scope *fs) { Fields *ps = &f->type->fields; f->params.len = 0; for (iz i = 0; i < ps->len; i++) { Field *p = ps->data + i; Sym *sym = mksym(S_VAR, p->name, p->pos); sym->type = p->type; sym->owner = f; sym->isparam = 1; sym->isvar = p->isvar; sym->readonly = !p->isvar; sym->cname = newcname(S("p"), p->name); declare(fs, sym); *push(&perm, &f->params) = sym; } } static void check_fn(Fn *f) { if (f->co && f == f->co->resume) { // checked with its coroutine fn_signature(f); check_fn(f->co->fn); return; } if (f->builtin || f->checked) return; if (c_function(f)) { check_c_function(f); return; } if (!f->type) { fn_signature(f); if (f->checked) return; } if (f->checking && !f->infer) return; if (f->checking && f->infer && f->type->elem) return; Fn *savefn = curfn; Module *savemod = curmod; i32 savefile = curfile; b32 savemacro = in_macro_code; curfn = f; if (f->module >= 0) curmod = modules.data[f->module]; if (f->ismacro) in_macro_code = 1; f->checking = 1; Scope *fs = mkscope(f->scope, SC_FN); fs->fn = f; declare_params(f, fs); Node *body = f->decl->b; Scope *bs = mkscope(fs, SC_BLOCK); if (f->infer) { Type *r = check_block_value(body, bs, 0, 0); if (r == t_nil) errorat(body->pos, "nil needs a type here; write -> Type"); Fields ps = f->type->fields; f->type = fn_type(ps, r); } else if (f->type->elem == t_void) { check_block_value(body, bs, t_void, 0); } else { check_block_value(body, bs, f->type->elem, 1); } f->checking = 0; f->checked = 1; curfn = savefn; curmod = savemod; curfile = savefile; in_macro_code = savemacro; if (f->co && f == f->co->fn && !f->co->analyzed) co_analyze(f->co); } static Fn * new_fn(Node *d, Scope *s, Str name) { Fn *f = new(&perm, 1, Fn); f->name = name; f->decl = d; f->scope = s; f->module = curmod ? curmod->id : -1; f->ismacro = (d->flags & F_MACRO) != 0; f->isprivate = (d->flags & F_PRIVATE) != 0; f->cname = newcname(S("f"), name); f->op = d->op; f->isparenless = d->kind == N_FN && (d->flags & F_COMMAND) != 0; *push(&perm, &all_fns) = f; return f; } static void check_local_fn(Node *n, Scope *s) { Sym *sym = scope_get(s, n->name); if (!sym || sym->kind != S_FN) { sym = mksym(S_FN, n->name, n->pos); declare(s, sym); } Fn *f = new_fn(n, s, n->name); f->isnested = 1; f->isclosure = 1; f->parent = scope_fn(s); *push(&perm, &sym->fns) = f; *push(&perm, &closures) = f; fn_signature(f); check_fn(f); } static Type * check_fnlit(Node *n, Scope *s, Type *want) { if (n->info) return n->type; Fn *f = new_fn(n, s, S("closure")); f->isclosure = 1; f->escapes = 1; f->parent = scope_fn(s); n->info = f; *push(&perm, &closures) = f; // Parameter types may come from the expected function type. if (want && want->kind == TY_FN) { if (want->fields.len != n->list.len) errorat(n->pos, "this function needs %lld parameters", (long long)want->fields.len); for (iz i = 0; i < n->list.len; i++) { Node *pn = n->list.data[i]; if (!pn->a) { Field *wf = want->fields.data + i; Node *tn2 = mknode(N_TNAME, pn->pos); tn2->type = wf->type; pn->a = tn2; pn->info = wf->type; } if (want->fields.data[i].isvar) pn->flags |= F_VAR; } if (!n->a && !(n->flags & F_DO) && want->elem != t_void) { Node *rt = mknode(N_TNAME, n->pos); rt->type = want->elem; n->a = rt; } } fn_signature(f); check_fn(f); n->type = fn_value(f->type); return n->type; } // Coroutines (docs/coroutines.md) // resume(var c: Coroutine(NAME)) -> no_nil(union (yielded: Y, returned: R)): // known from the declaration, so overloads of resume choose without the // body. A union has nil, which resume never gives: no_nil keeps every switch // on what it gives to the two cases. static void co_signature(Coro *co) { Fn *f = co->fn, *r = co->resume; if (r->type) return; fn_signature(f); Module *save = curmod; curmod = modules.data[f->module]; Type *y = resolve_type(f->decl->d, f->scope); curmod = save; co->yields = y; Type *rt = f->type->elem; Type *u = mktype(TY_UNION); u->nonil = 1; Field fy = {0}, fr = {0}; fy.name = S("yielded"); fy.type = y; fy.pos = f->decl->d->pos; fr.name = S("returned"); fr.type = rt == t_void ? 0 : rt; fr.pos = f->decl->pos; *push(&perm, &u->fields) = fy; *push(&perm, &u->fields) = fr; u->resolved = 1; u = intern(u); compute_flags(u, 0); co->result = u; Sym *c = mksym(S_VAR, S("c"), f->decl->pos); c->type = co->type; c->owner = f; // the body works on it as on the coroutine's own parameter c->isparam = c->isvar = 1; c->cname = newcname(S("p"), S("c")); co->param = c; Fields ps = {new(&perm, 1, Field), 1, 1}; ps.data[0].name = S("c"); ps.data[0].type = co->type; ps.data[0].isvar = 1; ps.data[0].pos = f->decl->pos; r->params = (Syms){new(&perm, 1, Sym *), 1, 1}; r->params.data[0] = c; r->type = fn_type(ps, u); } // The coroutine that a yield in scope s yields from: the function it is in, // or one it is nested in, but not through a function value. static Coro * yield_target(Scope *s, Pos at) { Fn *lit = 0; for (Fn *f = scope_fn(s); f; f = f->parent) { if (f->co && f == f->co->fn) { if (lit) { notes.len = 0; note(lit->decl->pos, S("the function value")); errorat(at, "yield cannot be inside a function value: a call of it could come " "after the coroutine ended"); } fn_signature(f->co->resume); return f->co; } if (f->isclosure && !f->isnested && !lit) lit = f; if (!f->isclosure) break; } help_text = S("declare the function with coroutine(fn name(...) ..., Y), or nest it in one"); errorat(at, "yield only works in a coroutine, and in the functions nested in it"); } // Coroutine(name): the coroutine a name gives (also module.name) static Coro * coroutine_named(Node *a, Scope *s) { Sym *sym = 0; Str name = a->name; if (a->kind == N_NAME) { sym = find(s, a->name, a->pos, 1); } else if (a->kind == N_FIELD && a->a->kind == N_NAME) { Sym *alias = find(s, a->a->name, a->a->pos, 1); if (alias->kind != S_MODULE) errorat(a->a->pos, "%s is not an imported module", a->a->name); sym = scope_get(alias->mod->scope, a->name); if (!sym) no_module_member(a->pos, a->a->name, alias->mod->scope, a->name, S("")); if (sym->isprivate) errorat(a->pos, "%s is private to %s", a->name, alias->mod->name); } else { help_text = S("write Coroutine(name), name being declared with coroutine(fn name(...) ..., Y)"); errorat(a->pos, "Coroutine takes the name of a coroutine"); } Fn *f = sym->kind == S_FN && sym->fns.len == 1 ? sym->fns.data[0] : 0; if (!f || !f->co || f != f->co->fn) { help_text = fmtstr("declare it with coroutine(fn %s(...) ..., Y)", name); errorat(a->pos, "%s is not a coroutine", name); } sym->used = 1; return f->co; } // The fields of Coroutine(NAME) come from the body of NAME: used before its // place in the in-order pass, it is checked there, as a function whose result // is inferred is (design.md, Metaprogramming). static void co_need_body(Coro *co) { Fn *f = co->fn; if (co->analyzed) return; Pos at = need_at.file >= 0 ? need_at : f->decl->pos; if (f->checking) { notes.len = 0; note(f->decl->pos, S("the coroutine, still being checked")); errorat(at, "the fields of %s come from the body of %s, which is not checked yet here", type_name(co->type), f->name); } Module *save = curmod; curmod = modules.data[f->module]; check_now(f, at, !f->checked); curmod = save; } // What a coroutine keeps between resumes, worked out once its body is // checked. Scopes are numbered in source order, s1 being the body (the // parameters of a nested function are a scope of their own, before its // body). A yield, and a call of a nested function that yields, is a yield // point. A local is kept in the struct when a yield point comes after it in // a scope it belongs to; the others stay C locals. Nested functions that // yield are inlined at each call, so their locals are kept as the body's // are, and each call counts their yield points again. static b32 is_fresh(Node *n); static b32 quiet_value(Node *a); static b32 variable_place(Node *n); enum { CO_VALUE = 1, // inside a value, or not the whole of its statement CO_DEFER = 2, // inside a deferred statement CO_MAPLOOP = 4, // inside a loop over a map CO_REFSWITCH = 8, // inside a case of a switch & CO_DISPATCH = 16, // a case of a dispatching call }; typedef struct { Sym *sym; Fn *fn; // whose local it is i32 scope; // the scope it is declared in i32 seq; // when, in source order } CoLocal; typedef struct { Node *node; // the yield, or the call Fn *fn; // the function it is in Fn *callee; // a call of a nested function; 0 for a yield i32 seq; // when, in source order u32 where; // CO_ flags Node *why; // the construct a flag is about i32 *scopes; // the scopes of its function open there i32 nscopes; // Node **ctl; // the loops and switches of its function around it i32 nctl; // } CoSite; typedef struct { Coro *co; Map *nested; // decl node -> Fn: every function nested in the coroutine Fn *fn; // the function being walked i32 nscopes; // scopes numbered so far i32 seq; i32 stack[MAX_DEPTH + 8]; // the scopes open, those of fn from base on i32 sp, base; Node *ctl[MAX_DEPTH + 8]; // the loops and switches open, those of fn from cbase on i32 nc, cbase; u32 where; Node *why; struct { CoLocal *data; iz len; iz cap; } locals; struct { CoSite *data; iz len; iz cap; } sites; struct { Node **data; iz len; iz cap; } values; // nested functions used as values: their N_NAME Map *loop_scope; // N_FOR -> the number of its scope (body and bindings) } CoAn; static Fn * co_nested(CoAn *an, Fn *f) { return f && f->decl ? lookup(an->nested, ptrkey(f->decl)) : 0; } static i32 co_open(CoAn *an) { if (an->sp >= countof(an->stack)) errorat(an->co->fn->decl->pos, "this is nested too deeply"); an->stack[an->sp++] = ++an->nscopes; return an->nscopes; } static void co_local(CoAn *an, Sym *s) { if (!s) return; CoLocal *l = push(&perm, &an->locals); l->sym = s; l->fn = an->fn; l->scope = an->sp ? an->stack[an->sp - 1] : 0; l->seq = ++an->seq; } static void co_site(CoAn *an, Node *n, Fn *callee, u32 where) { CoSite *y = push(&perm, &an->sites); y->node = n; y->fn = an->fn; y->callee = callee; y->seq = ++an->seq; y->where = an->where | where; y->why = an->why; y->nscopes = an->sp - an->base; y->scopes = new(&perm, y->nscopes + 1, i32); memcpy(y->scopes, an->stack + an->base, (size_t)y->nscopes * sizeof(i32)); y->nctl = an->nc - an->cbase; y->ctl = new(&perm, y->nctl + 1, Node *); memcpy(y->ctl, an->ctl + an->cbase, (size_t)y->nctl * sizeof(Node *)); } static void co_walk(CoAn *an, Node *n, b32 whole); static void co_tail(CoAn *an, Node *e); // The statements of a block, in the scope open now static void co_stmts(CoAn *an, Node *b) { for (iz i = 0; b && i < b->list.len; i++) co_walk(an, b->list.data[i], 0); } static void co_block(CoAn *an, Node *b) { co_open(an); co_stmts(an, b); an->sp--; } // The value that ends a body is what it returns: an if, switch or block // there is a statement whose branches end in that value, so a yield may be // inside them, as inside a return. static void co_tail_stmts(CoAn *an, Node *b) { for (iz i = 0; b && i < b->list.len; i++) { Node *st = b->list.data[i]; if (i + 1 == b->list.len && st->kind == N_EXPR && st->a->type != t_void) co_tail(an, st->a); else co_walk(an, st, 0); } } // A switch statement; tail: it ends the body, so its cases end in the value // it returns. static void co_switch(CoAn *an, Node *n, b32 tail) { co_walk(an, n->a, 0); u32 save = an->where; Node *savewhy = an->why; if ((n->flags & F_REF) && !(an->where & CO_REFSWITCH)) an->why = n; if (n->flags & F_REF) an->where |= CO_REFSWITCH; if (an->nc < countof(an->ctl)) an->ctl[an->nc++] = n; for (iz i = 0; i < n->list.len; i++) { Node *cs = n->list.data[i]; CaseInfo *info = cs->info; co_open(an); if (info) co_local(an, info->binding); if (tail) co_tail_stmts(an, cs->a); else co_stmts(an, cs->a); an->sp--; } an->nc--; an->where = save; an->why = savewhy; } static void co_tail(CoAn *an, Node *e) { switch ((int)e->kind) { case N_IFX: co_walk(an, e->a, 0); for (int k = 0; k < 2; k++) { Node *x = k ? e->c : e->b; if (x->kind == N_BLOCKX) { co_open(an); co_tail_stmts(an, x->a); an->sp--; } else { co_tail(an, x); } } return; case N_BLOCKX: co_open(an); co_tail_stmts(an, e->a); an->sp--; return; case N_SWITCHX: co_switch(an, e->a, 1); return; default: co_walk(an, e, 1); return; // as return e } } // A function nested in the coroutine (fn or a function value): its scopes // are numbered in the same sequence; what is open around its declaration is // not around its body when it runs. static void co_function(CoAn *an, Fn *f) { if (!f || !f->decl || !f->decl->b) return; Fn *savefn = an->fn; i32 savebase = an->base, savecbase = an->cbase; u32 savewhere = an->where; Node *savewhy = an->why; an->fn = f; an->base = an->sp; an->cbase = an->nc; an->where = 0; // a yield point in a function value is found by its function an->why = 0; co_open(an); // the parameters for (iz i = 0; i < f->params.len; i++) co_local(an, f->params.data[i]); co_open(an); if (f->type && f->type->elem && f->type->elem != t_void) co_tail_stmts(an, f->decl->b); else co_stmts(an, f->decl->b); an->sp--; an->fn = savefn; an->sp = an->base; an->nc = an->cbase; an->base = savebase; an->cbase = savecbase; an->where = savewhere; an->why = savewhy; } static void co_flagged(CoAn *an, Node *n, u32 flag, Node *why, b32 isblock) { u32 save = an->where; Node *savewhy = an->why; if (!(an->where & flag)) an->why = why; an->where |= flag; if (isblock) co_block(an, n); else co_walk(an, n, 0); an->where = save; an->why = savewhy; } static void co_walk(CoAn *an, Node *n, b32 whole) { if (!n) return; switch ((int)n->kind) { case N_VAR: co_walk(an, n->b, 1); co_local(an, n->sym); return; case N_ASSIGN: // a = f(): the target is worked out after the call, so it may not // need temporaries (an index that is a call) co_walk(an, n->a, 0); co_walk(an, n->b, quiet_value(n->a)); return; case N_DISCARD: case N_EXPR: co_walk(an, n->a, 1); return; case N_RETURN: // return (if ...): as the value that ends a body if (n->a && (n->a->kind == N_IFX || n->a->kind == N_SWITCHX || n->a->kind == N_BLOCKX)) co_tail(an, n->a); else co_walk(an, n->a, 1); return; case N_IF: { co_walk(an, n->a, 0); co_block(an, n->b); Node *c = n->c, *only = c && c->list.len == 1 ? c->list.data[0] : 0; if (only && only->kind == N_IF && !(c->flags & F_DO)) co_walk(an, only, 0); // else if: the block around it is not written else if (c) co_block(an, c); return; } case N_WHILE: co_walk(an, n->a, 0); if (an->nc < countof(an->ctl)) an->ctl[an->nc++] = n; co_block(an, n->b); an->nc--; return; case N_FOR: { co_walk(an, n->a, 0); b32 map = n->a->type && n->a->type->kind == TY_MAP; u32 save = an->where; Node *savewhy = an->why; if (map && !(an->where & CO_MAPLOOP)) an->why = n; if (map) an->where |= CO_MAPLOOP; if (an->nc < countof(an->ctl)) an->ctl[an->nc++] = n; i32 k = co_open(an); // the bindings live in the braces they name *upsert(&an->loop_scope, ptrkey(n), 1) = (void *)(iz)k; co_local(an, n->sym); co_local(an, n->sym2); co_stmts(an, n->b); an->sp--; an->nc--; an->where = save; an->why = savewhy; return; } case N_BLOCK: co_block(an, n); return; case N_DEFER: co_flagged(an, n->a, CO_DEFER, n, 0); return; case N_LABELED: if (n->a && n->a->kind == N_BLOCK) co_block(an, n->a); else co_walk(an, n->a, 0); return; case N_SWITCH: co_switch(an, n, 0); return; case N_MLABEL: co_stmts(an, n->a); return; // not a scope case N_FN: co_function(an, lookup(an->nested, ptrkey(n))); return; case N_FNLIT: co_function(an, n->info); return; case N_IMPORT: case N_TEMPLATE: // code, not run here case N_TYPENAME: case N_TYPEX: case N_BREAK: case N_CONTINUE: return; case N_NAME: if (n->sym && n->sym->kind == S_FN) { for (iz i = 0; i < n->sym->fns.len; i++) { if (co_nested(an, n->sym->fns.data[i])) *push(&perm, &an->values) = n; } } return; case N_BLOCKX: co_flagged(an, n->a, CO_VALUE, n, 1); return; case N_IFX: case N_SWITCHX: { u32 save = an->where; Node *savewhy = an->why; if (!(an->where & CO_VALUE)) an->why = n; an->where |= CO_VALUE; co_walk(an, n->a, 0); // the condition, or the switch if (n->kind == N_IFX) co_walk(an, n->b, 0), co_walk(an, n->c, 0); an->where = save; an->why = savewhy; return; } case X_CONV: co_walk(an, n->a, whole); return; case X_CALLFN: { Call *c = n->info; if (c->kind == CALL_FN && c->builtin == B_YIELD) { co_walk(an, c->args[0], 1); // f(): runs before the yield co_site(an, n, 0, whole ? 0 : CO_VALUE); return; } for (iz i = 0; i < c->nargs; i++) co_walk(an, c->args[i], 0); if (c->kind == CALL_VALUE) co_walk(an, c->callee, 0); if (c->kind == CALL_FN && co_nested(an, c->fn)) co_site(an, n, c->fn, whole ? 0 : CO_VALUE); if (c->kind == CALL_DISPATCH) { for (iz v = 0; c->cases && v < c->dispatch_union->fields.len; v++) { if (co_nested(an, c->cases[v])) co_site(an, n, c->cases[v], CO_DISPATCH); } } return; } default: break; } // expressions: their parts, none of them the whole of a statement co_walk(an, n->a, 0); co_walk(an, n->b, 0); if (n->kind != N_FOR && n->kind != N_CAST) co_walk(an, n->c, 0); for (iz i = 0; i < n->list.len; i++) co_walk(an, n->list.data[i], 0); } // Recursion through a function that yields: inlining it would never end static void co_no_recursion(CoAn *an, Fn *f, Map **state) { void **st = upsert(state, ptrkey(f), 1); if (*st == (void *)2) return; *st = (void *)1; for (iz i = 0; i < an->sites.len; i++) { CoSite *y = an->sites.data + i; if (y->fn != f || !y->callee || !y->callee->yields) continue; void **cs = upsert(state, ptrkey(y->callee), 1); if (*cs == (void *)1) { notes.len = 0; note(y->callee->decl->pos, fmtstr("%s, which yields", y->callee->name)); help_text = S("loop instead of calling it again"); errorat(y->node->pos, "%s yields, so each call of it is inlined into the coroutine, and this " "call makes it recursive", y->callee->name); } co_no_recursion(an, y->callee, state); } *st = (void *)2; } // Yield points of f, each call of a nested function that yields with all of // its own static i64 co_points(CoAn *an, Fn *f, Map **memo) { void **m = upsert(memo, ptrkey(f), 1); if (*m) return (i64)(iz)*m - 1; i64 n = 0; for (iz i = 0; i < an->sites.len && n <= 1000000; i++) { CoSite *y = an->sites.data + i; if (y->fn != f) continue; if (!y->callee) n++; else if (y->callee->yields) n += co_points(an, y->callee, memo); } if (n > 1000000) n = 1000000; *m = (void *)(iz)(n + 1); return n; } static Str co_field_name(Fields *fs, Str want) { Str name = want; for (int k = 2;; k++) { b32 taken = 0; for (iz i = 0; i < fs->len; i++) taken |= equals(fs->data[i].name, name); if (!taken) return name; name = fmtstr("%s%d", want, k); } } // A constant a loop can work out again at each step, as plain C: the end of // 0..<3 static b32 co_constant(Node *n) { switch ((int)n->kind) { case N_INT: return 1; case N_UNARY: return n->op == T_MINUS && n->a->kind == N_INT; case N_NAME: return n->sym && n->sym->kind == S_VAR && n->sym->isconst && n->sym->isglobal; default: return 0; } } static void co_keep(Sym *s, Coro *co, Str scope, Type *holder) { s->co = co; s->co_path = fmtstr(".f_%s.f_%s", scope, cident(s->name)); s->co_scope = scope; s->co_holder = holder; } static void co_analyze(Coro *co) { Fn *f = co->fn; if (co->analyzed) return; Module *savemod = curmod; curmod = modules.data[f->module]; fn_signature(co->resume); CoAn an = {0}; an.co = co; // the functions nested in it, at any depth for (iz i = 0; i < all_fns.len; i++) { Fn *g = all_fns.data[i]; if (!g->decl || g == f) continue; Fn *p = g->parent; while (p && p != f) p = p->parent; if (p) *upsert(&an.nested, ptrkey(g->decl), 1) = g; } an.fn = f; co_open(&an); // s1, the body if (f->type->elem != t_void) co_tail_stmts(&an, f->decl->b); else co_stmts(&an, f->decl->b); an.sp--; // which nested functions yield: those with a yield, and those that call // one that does for (b32 changed = 1; changed;) { changed = 0; for (iz i = 0; i < an.sites.len; i++) { CoSite *y = an.sites.data + i; b32 real = !y->callee || y->callee->yields; if (real && y->fn != f && y->fn->isnested && !y->fn->yields) { y->fn->yields = 1; changed = 1; } } } for (iz i = 0; i < an.sites.len; i++) { Fn *g = an.sites.data[i].fn; for (iz k = 0; g->yields && k < g->params.len; k++) { if (!g->params.data[k]->isvar) continue; help_text = S("pass the value, and give back what changes by return"); errorat(g->params.data[k]->pos, "%s yields, so the coroutine keeps its parameters between resumes, and none " "can be var", g->name); } } Map *state = 0; co_no_recursion(&an, f, &state); for (iz i = 0; i < an.sites.len; i++) { Fn *g = an.sites.data[i].fn; if (g->yields) co_no_recursion(&an, g, &state); } Map *memo = 0; i64 points = co_points(&an, f, &memo); if (points >= 1000000) errorat(f->decl->pos, "%s has too many yield points once the nested functions that yield " "are inlined at each call", f->name); co->points = (i32)points; // where a yield point may not be for (iz i = 0; i < an.values.len; i++) { Node *v = an.values.data[i]; for (iz k = 0; k < v->sym->fns.len; k++) { Fn *g = v->sym->fns.data[k]; if (!g->yields) continue; help_text = fmtstr("call it: %s(...)", g->name); errorat(v->pos, "%s yields, so it can only be called, not used as a value", g->name); } } i32 *last = new(&perm, an.nscopes + 2, i32); // per scope: the last yield point in it for (iz i = 0; i < an.sites.len; i++) { CoSite *y = an.sites.data + i; if (y->callee && !y->callee->yields) continue; Str what = y->callee ? fmtstr("%s yields, so its call", y->callee->name) : S("yield"); if (y->fn != f && !y->fn->yields) { // a function value notes.len = 0; note(y->fn->decl->pos, S("the function value")); errorat(y->node->pos, "%s cannot be inside a function value: a call of it could come after the " "coroutine ended", what); } if (y->why) notes.len = 0; if (y->where & CO_DEFER) { if (y->why) note(y->why->pos, S("the deferred statement")); errorat(y->node->pos, "%s cannot be in a deferred statement", what); } if (y->where & CO_MAPLOOP) { if (y->why) note(y->why->pos, S("the loop over a map")); help_text = S("loop over a list of its keys, which a resume cannot disturb"); errorat(y->node->pos, "%s cannot be inside a loop over a map: the map could change between " "resumes", what); } if (y->where & CO_REFSWITCH) { if (y->why) note(y->why->pos, S("the switch &")); help_text = S("switch on the value, and assign what changes back after the switch"); errorat(y->node->pos, "%s cannot be inside a switch &: its binding is the place itself", what); } if (y->where & CO_DISPATCH) { notes.len = 0; errorat(y->node->pos, "%s yields, so it cannot be a case of a call that dispatches on a union", y->callee->name); } if (y->where & CO_VALUE) { if (y->why) note(y->why->pos, S("the value")); else notes.len = 0; if (!y->callee) { help_text = S("give it a line of its own: yield(value)"); errorat(y->node->pos, "yield is a statement: it cannot be part of a value"); } help_text = S("give it a line of its own, or make it the whole value of var, " "an assignment, return or yield"); errorat(y->node->pos, "%s cannot be part of a value", what); } notes.len = 0; for (i32 k = 0; k < y->nscopes; k++) { if (y->seq > last[y->scopes[k]]) last[y->scopes[k]] = y->seq; } for (i32 k = 0; k < y->nctl; k++) { Node *c = y->ctl[k]; Nodes *list = c->kind == N_SWITCH ? &co->yswitches : &co->yloops; b32 have = 0; for (iz j = 0; j < list->len; j++) have |= list->data[j] == c; if (!have) addnode(list, c); } } // the loops with a yield inside keep their own state too for (iz i = 0; i < co->yloops.len; i++) { Node *n = co->yloops.data[i]; if (n->kind != N_FOR) continue; CoLoop *cl = push(&perm, &co->fors); cl->loop = n; cl->scope = (i32)(iz)lookup(an.loop_scope, ptrkey(n)); Node *src = n->a; Type *t = src->type; if (src->kind == N_RANGE) { if (!co_constant(src->b)) cl->types[CL_END] = t_int; continue; } Node *root = root_of(src); b32 place = !is_fresh(src) && src->kind != N_STR && (root->kind == N_NAME || root->kind == N_DEREF); if (place && !variable_place(src)) { help_text = fmtstr("copy it into a variable first: var items = %s", node_source(src)); errorat(src->pos, "a loop with a yield inside goes over a variable, or a field of " "one: this place could move between resumes"); } if (!place) cl->types[CL_OVER] = t; if (!n->sym2 || t->kind == TY_POOL) cl->types[CL_INDEX] = t_int; if (t->kind != TY_ARRAY) cl->types[CL_COUNT] = t_int; } // Coroutine(NAME) (args, s1, s2, ..., returned, at): a scope gets a // field when it keeps something Type *t = co->type; Fields fields = {new(&perm, 8, Field), 0, 8}; Type *args = mktype(TY_STRUCT); for (iz i = 0; i < f->type->fields.len; i++) { Field fl = f->type->fields.data[i]; fl.def = 0; *push(&perm, &args->fields) = fl; } args->resolved = 1; args = intern(args); compute_flags(args, 0); Field fa = {0}; fa.name = S("args"); fa.type = args; fa.pos = f->decl->pos; *push(&perm, &fields) = fa; for (iz i = 0; i < f->params.len; i++) co_keep(f->params.data[i], co, S("args"), args); for (i32 k = 1; k <= an.nscopes; k++) { Fields kf = {0}; Syms kept = {0}; for (iz i = 0; i < an.locals.len; i++) { CoLocal *l = an.locals.data + i; if (l->scope != k || l->seq >= last[k]) continue; Field fl = {0}; fl.name = l->sym->name; fl.type = l->sym->type; fl.pos = l->sym->pos; *push(&perm, &kf) = fl; *push(&perm, &kept) = l->sym; } static Str hidden[4] = {SL("end___"), SL("index___"), SL("count___"), SL("over___")}; Str scope = fmtstr("s%d", k); for (iz j = 0; j < co->fors.len; j++) { CoLoop *cl = co->fors.data + j; for (int w = 0; cl->scope == k && w < 4; w++) { if (!cl->types[w]) continue; Field fl = {0}; fl.name = co_field_name(&kf, hidden[w]); fl.type = cl->types[w]; fl.pos = cl->loop->pos; *push(&perm, &kf) = fl; cl->paths[w] = fmtstr(".f_%s.f_%s", scope, cident(fl.name)); } } if (!kf.len) continue; Type *st = mktype(TY_STRUCT); st->fields = kf; st->resolved = 1; st = intern(st); compute_flags(st, 0); Field fs = {0}; fs.name = scope; fs.type = st; fs.pos = f->decl->pos; *push(&perm, &fields) = fs; for (iz i = 0; i < kept.len; i++) co_keep(kept.data[i], co, scope, st); } if (f->type->elem != t_void) { Field fr = {0}; fr.name = S("returned"); fr.type = f->type->elem; fr.pos = f->decl->pos; *push(&perm, &fields) = fr; } Field fat = {0}; fat.name = S("at"); fat.type = t_int; fat.pos = f->decl->pos; *push(&perm, &fields) = fat; t->fields = fields; t->resolved = 1; compute_flags(t, 0); if (contains_by_value(t, t, 0)) { notes.len = 0; for (iz i = 1; i < t->fields.len; i++) { // the local that holds one Fields *kf = &t->fields.data[i].type->fields; for (iz j = 0; t->fields.data[i].type->kind == TY_STRUCT && j < kf->len; j++) { Type *lt = kf->data[j].type; if (lt != t && !contains_by_value(lt, t, 0)) continue; note(kf->data[j].pos, fmtstr("%s, which it keeps", kf->data[j].name)); break; } } errorat(f->decl->pos, "%s contains itself, so it would be infinitely large", type_name(t)); } // what the struct keeps cannot be reached from where it does not live for (iz i = 0; i < an.locals.len; i++) { Sym *s = an.locals.data[i].sym; if (!s->co_path.len || !s->refpinned) continue; if (s->pin_site.file >= 0) { notes.len = 0; note(s->pos, S("declared here")); } help_text = S("keep a copy instead, or keep the value outside the coroutine"); errorat(s->pin_site.file >= 0 ? s->pin_site : s->pos, "%s is kept in the coroutine between resumes, so a reference to it cannot be " "stored", s->name); } for (iz i = 0; i < all_fns.len; i++) { Fn *g = all_fns.data[i]; if (!g->decl || g->yields || !lookup(an.nested, ptrkey(g->decl))) continue; // a function value or nested function that captures what the struct // keeps reaches it through c, as a nested function reaches a var // parameter b32 any = 0; iz k = 0; for (iz j = 0; j < g->captures.len; j++) { Sym *s = g->captures.data[j]; if (!s->co_path.len) { g->captures.data[k++] = s; continue; } if (g->escapes) { notes.len = 0; note(s->pos, S("declared here")); help_text = S("pass the function down only, or copy the value into a local " "declared after the last yield"); errorat(g->decl->pos, "this function value may outlive the resume, so it cannot use %s, which " "the coroutine keeps between resumes", s->name); } any = 1; } g->captures.len = k; if (!any) continue; b32 have = 0; for (iz j = 0; j < g->captures.len; j++) have |= g->captures.data[j] == co->param; if (!have) *push(&perm, &g->captures) = co->param; co->param->iscaptured = 1; } co->analyzed = 1; co->resume->checked = 1; curmod = savemod; } // Whether a function parameter of function type may outlive the call: it does // unless every use calls it. static b32 uses_escape(Node *n, Sym *p) { if (!n) return 0; if (n->kind == N_NAME && n->sym == p) return 1; if ((int)n->kind == X_CALLFN) { Call *c = n->info; for (iz i = 0; i < c->nargs; i++) { if (uses_escape(c->args[i], p)) return 1; } if (c->kind == CALL_VALUE && !(c->callee->kind == N_NAME && c->callee->sym == p)) return uses_escape(c->callee, p); return 0; } if (uses_escape(n->a, p) || uses_escape(n->b, p)) return 1; if (uses_escape(n->c, p)) return 1; for (iz i = 0; i < n->list.len; i++) { if (uses_escape(n->list.data[i], p)) return 1; } return 0; } static b32 param_escapes(Fn *f, iz i) { // a function whose body is not checked yet escapes it: checking it now // would check it out of source order if (!f->checked) return 1; if (i >= f->params.len) return 1; return uses_escape(f->decl->b, f->params.data[i]); } // std:code, the module of code values, in Star99. static Str std_code_lines[] = { SL("# std:code: code as ordinary data, for macros and for programs that work"), SL("# with code. Every construct has its own type; a site says where it was"), SL("# written."), SL(""), SL("struct Site (file: String, line, column: Int)"), SL(""), SL("struct Name (name: String, site: Site)"), SL("# A number, string, character, true, false or nil, as written"), SL("struct Literal (text: String, site: Site)"), SL("struct Call ("), SL(" callee: Expr,"), SL(" arguments: List(Expr),"), SL(" names: List(String),"), SL(" site: Site,"), SL(")"), SL("struct Field (base: Expr, name: String, site: Site)"), SL("struct Binary (op: String, left, right: Expr, site: Site)"), SL("struct Unary (op: String, operand: Expr, site: Site)"), SL("struct Index (base: Expr, indices: List(Expr), site: Site)"), SL("struct Deref (operand: Expr, site: Site)"), SL("struct Addr (operand: Expr, site: Site)"), SL("struct ListLiteral (items: List(Expr), site: Site)"), SL("struct Entry (key, value: Expr)"), SL("struct MapLiteral (entries: List(Entry), site: Site)"), SL("# .(a, b): a value of an anonymous struct"), SL("struct Anonymous (arguments: List(Expr), names: List(String), site: Site)"), SL("# .name or .name(...): a variant chosen by its name"), SL("struct Variant ("), SL(" name: String,"), SL(" arguments: List(Expr),"), SL(" names: List(String),"), SL(" called: Bool,"), SL(" site: Site,"), SL(")"), SL("# A type written where a value is expected"), SL("struct TypeValue (type: TypeExpr, site: Site)"), SL("struct BlockValue (body: List(Stmt), site: Site)"), SL("struct IfValue (condition, then, otherwise: Expr, site: Site)"), SL("struct SwitchValue (value: Switch, site: Site)"), SL("struct Closure ("), SL(" params: List(Param),"), SL(" result: TypeExpr,"), SL(" body: List(Stmt),"), SL(" site: Site,"), SL(" compile_time: Bool,"), SL(" do_body: Bool,"), SL(")"), SL("# code(...) or code { ... } inside code"), SL("struct Template (body: List(Stmt), statements: Bool, site: Site)"), SL("struct Splice (value: Expr, site: Site)"), SL("union Expr ("), SL(" Name,"), SL(" Literal,"), SL(" Call,"), SL(" Field,"), SL(" Binary,"), SL(" Unary,"), SL(" Index,"), SL(" Deref,"), SL(" Addr,"), SL(" ListLiteral,"), SL(" MapLiteral,"), SL(" Anonymous,"), SL(" Variant,"), SL(" TypeValue,"), SL(" BlockValue,"), SL(" IfValue,"), SL(" SwitchValue,"), SL(" Closure,"), SL(" Template,"), SL(" Splice,"), SL(")"), SL(""), SL("struct TypeName (name: String, arguments: List(TypeExpr), site: Site)"), SL("struct FunctionType (params: List(TypeExpr), result: TypeExpr, site: Site)"), SL("struct StructType (fields: List(Param), site: Site)"), SL("struct UnionType (variants: List(Param), site: Site, no_nil: Bool)"), SL("# A macro call where a type is expected"), SL("struct TypeCall (call: Call, site: Site)"), SL("# A value argument of a type, such as a length"), SL("struct Size (value: Expr, site: Site)"), SL("union TypeExpr (TypeName, FunctionType, StructType, UnionType, TypeCall, Size)"), SL(""), SL("struct Param ("), SL(" name: String,"), SL(" type: TypeExpr,"), SL(" value: Expr,"), SL(" is_var: Bool,"), SL(" site: Site,"), SL(" is_private: Bool,"), SL(")"), SL("struct Function ("), SL(" name: String,"), SL(" params: List(Param),"), SL(" result: TypeExpr,"), SL(" body: List(Stmt),"), SL(" site: Site,"), SL(" compile_time: Bool,"), SL(" is_private: Bool,"), SL(" parenless: Bool,"), SL(")"), SL("struct Struct ("), SL(" name: String,"), SL(" fields: List(Param),"), SL(" site: Site,"), SL(" compile_time: Bool,"), SL(" is_private: Bool,"), SL(")"), SL("struct Union ("), SL(" name: String,"), SL(" variants: List(Param),"), SL(" site: Site,"), SL(" compile_time: Bool,"), SL(" is_private: Bool,"), SL(" no_nil: Bool,"), SL(")"), SL("struct Global ("), SL(" name: String,"), SL(" type: TypeExpr,"), SL(" value: Expr,"), SL(" constant: Bool,"), SL(" site: Site,"), SL(" compile_time: Bool,"), SL(" is_private: Bool,"), SL(")"), SL("struct Import ("), SL(" path: String,"), SL(" alias: String,"), SL(" arguments: List(Expr),"), SL(" site: Site,"), SL(" compile_time: Bool,"), SL(")"), SL("# private { ... } or macro { ... }: declarations as a group; with neither,"), SL("# declarations written as one (var a, b: Int)"), SL("struct Group ("), SL(" body: List(Stmt),"), SL(" site: Site,"), SL(" compile_time: Bool,"), SL(" is_private: Bool,"), SL(")"), SL("struct MacroLabel (name: String, body: List(Stmt), site: Site)"), SL("struct Module (params: List(Param), site: Site)"), SL("struct Collection (name, path: String, site: Site)"), SL("union Decl ("), SL(" Function,"), SL(" Struct,"), SL(" Union,"), SL(" Global,"), SL(" Import,"), SL(" Group,"), SL(" MacroLabel,"), SL(" Module,"), SL(" Collection,"), SL(")"), SL(""), SL("# Program knowledge: questions about the program, answered by the compiler."), SL("# A Def is one declaration of the program; where a question takes a Def, a"), SL("# name of the program written there is that declaration (writes(scan))."), SL("union DefKind ("), SL(" function,"), SL(" macro_function,"), SL(" global,"), SL(" constant,"), SL(" struct_type,"), SL(" union_type,"), SL(")"), SL("struct Def (id: Int, name: String, kind: DefKind, site: Site)"), SL(""), SL("fn print(d: Def) do print(d.name)"), SL("fn print(s: Site) do print(s.file, \":\", s.line, \":\", s.column)"), SL(""), SL("fn def_handle(id: Int) -> Def do ask(Def, \"def\", id)"), SL(""), SL("# Every declaration that exists here, in source order"), SL("fn defs() -> List(Def) do ask(List(Def), \"defs\")"), SL(""), SL("# The declaration named name, for a name known only as a string"), SL("fn lookup(name: String) -> Def do ask(Def, \"lookup\", name)"), SL(""), SL("# The declaration of a function a macro was given (react(fn ...)): known by"), SL("# its site, so it can be asked about once it is declared"), SL("fn def_of("), SL(" f: Function,"), SL(") -> Def do Def(id: 0, name: f.name, kind: DefKind.function, site: f.site)"), SL(""), SL("fn overloads(d: Def) -> List(Def) do ask(List(Def), \"overloads\", d)"), SL(""), SL("# Its type: a function as fn (...) -> T, a global or a type as written"), SL("fn type(d: Def) -> TypeExpr do ask(TypeExpr, \"type\", d)"), SL(""), SL("# Its typed code, as written plus what macros made in it"), SL("fn decl_of(d: Def) -> Decl do ask(Decl, \"decl_of\", d)"), SL(""), SL("# The macro call that generated it, or nil for one written by hand"), SL("fn made_by(d: Def) -> union (Site) do ask(union (Site), \"made_by\", d)"), SL(""), SL("# The functions f calls directly (each case of a dispatch too)"), SL("fn calls(f: Def) -> List(Def) do ask(List(Def), \"calls\", f)"), SL(""), SL("# Where f calls through a function value (which function is not known)"), SL("fn calls_by_value(f: Def) -> List(Site) do ask(List(Site), \"calls_by_value\", f)"), SL(""), SL("# Everything f can run, through all its calls; a call through a function"), SL("# value can run any function whose value the program takes"), SL("fn reaches(f: Def) -> List(Def) do ask(List(Def), \"reaches\", f)"), SL(""), SL("# The functions that call f, or take it as a value"), SL("fn callers(f: Def) -> List(Def) do ask(List(Def), \"callers\", f)"), SL(""), SL("fn recursive(f: Def) -> Bool do ask(Bool, \"recursive\", f)"), SL(""), SL("# The globals f reads and writes, through everything it reaches (a superset:"), SL("# a call through a function value counts what any taken function does)"), SL("fn reads(f: Def) -> List(Def) do ask(List(Def), \"reads\", f)"), SL("fn writes(f: Def) -> List(Def) do ask(List(Def), \"writes\", f)"), SL(""), SL("# Where f (and what it reaches) reads or writes through a reference or a var"), SL("# parameter, a place no name says"), SL("fn reads_elsewhere("), SL(" f: Def,"), SL(") -> List(Site) do ask(List(Site), \"reads_elsewhere\", f)"), SL("fn writes_elsewhere("), SL(" f: Def,"), SL(") -> List(Site) do ask(List(Site), \"writes_elsewhere\", f)"), SL(""), SL("union EffectKind (output, input, files, run, environment, exit, fail)"), SL("struct Effect (kind: EffectKind, site: Site)"), SL(""), SL("fn print(e: Effect) do print(e.kind, \" at \", e.site)"), SL(""), SL("# What f can do besides computing, through everything it reaches"), SL("fn effects(f: Def) -> List(Effect) do ask(List(Effect), \"effects\", f)"), SL(""), SL("union LocalKind (variable, constant, parameter, var_parameter, binding)"), SL("union Lifetime (stack, counted_cell, permanent_cell)"), SL(""), SL("# A local of a function. lives: on the stack, or in a heap cell kept by"), SL("# kept_by (the closures that capture it and outlive the call, the stored"), SL("# reference to it); a counted cell goes when the last of them does"), SL("struct Local ("), SL(" name: String,"), SL(" type: TypeExpr,"), SL(" site: Site,"), SL(" kind: LocalKind,"), SL(" lives: Lifetime,"), SL(" kept_by: List(Site),"), SL(")"), SL(""), SL("fn print(l: Local) do print(l.name)"), SL(""), SL("fn locals(f: Def) -> List(Local) do ask(List(Local), \"locals\", f)"), SL(""), SL("# Build facts, known once the program is compiled (ask them in build(b))"), SL(""), SL("# The statements whose generated code may allocate, through everything f"), SL("# reaches (a superset); empty means f never allocates"), SL("fn allocates(f: Def) -> List(Site) do ask(List(Site), \"allocates\", f)"), SL(""), SL("union CheckKind ("), SL(" bounds,"), SL(" overflow,"), SL(" division,"), SL(" removed,"), SL(" missing_key,"), SL(" variant,"), SL(" nil_dispatch,"), SL(" conversion,"), SL(" assert,"), SL(" fail,"), SL(" nil_function,"), SL(")"), SL(""), SL("# A run-time check left in the generated code of f, and whether a loop runs it"), SL("struct Check (kind: CheckKind, site: Site, in_loop: Bool)"), SL(""), SL("fn print(c: Check) do print(c.kind, \" at \", c.site)"), SL(""), SL("fn checks(f: Def) -> List(Check) do ask(List(Check), \"checks\", f)"), SL(""), SL("# The bounds optimizing found for an Int local; none without optimizing"), SL("struct Range (local: Local, low, high: Int)"), SL(""), SL("fn print(r: Range) do print(r.local, \" from \", r.low, \" to \", r.high)"), SL(""), SL("fn ranges(f: Def) -> List(Range) do ask(List(Range), \"ranges\", f)"), SL(""), SL("# for index, binding in source; binding is empty for for xs { ... }, and"), SL("# variant is T for for x: T in xs"), SL("struct For ("), SL(" binding: String,"), SL(" source: Expr,"), SL(" body: List(Stmt),"), SL(" index: String,"), SL(" site: Site,"), SL(" loop_label: String,"), SL(" compile_time: Bool,"), SL(" do_body: Bool,"), SL(" variant: TypeExpr,"), SL(")"), SL("struct Return (value: Expr, site: Site)"), SL("struct Assign (op: String, target, value: Expr, site: Site)"), SL("# otherwise holds one If for else if"), SL("struct If ("), SL(" condition: Expr,"), SL(" then: List(Stmt),"), SL(" otherwise: List(Stmt),"), SL(" site: Site,"), SL(" compile_time: Bool,"), SL(" do_body: Bool,"), SL(" do_else: Bool,"), SL(")"), SL("struct While (condition: Expr, body: List(Stmt), site: Site, do_body: Bool)"), SL("struct Block (body: List(Stmt), site: Site)"), SL("struct Break (target: String, site: Site)"), SL("struct Continue (target: String, site: Site)"), SL("struct Defer (body: Stmt, site: Site)"), SL("struct Discard (value: Expr, site: Site)"), SL("struct Labeled (name: String, body: Stmt, site: Site)"), SL("struct Switch ("), SL(" value: Expr,"), SL(" binding: String,"), SL(" by_ref: Bool,"), SL(" cases: List(Case),"), SL(" site: Site,"), SL(" compile_time: Bool,"), SL(")"), SL("# no patterns: default"), SL("struct Case ("), SL(" patterns: List(Expr),"), SL(" body: List(Stmt),"), SL(" case_label: String,"), SL(" site: Site,"), SL(")"), SL("# Several statements as one, as a template of more than one gives"), SL("struct Statements (body: List(Stmt), site: Site)"), SL("union Stmt ("), SL(" For,"), SL(" Return,"), SL(" Assign,"), SL(" Expr,"), SL(" Decl,"), SL(" If,"), SL(" While,"), SL(" Block,"), SL(" Break,"), SL(" Continue,"), SL(" Defer,"), SL(" Discard,"), SL(" Labeled,"), SL(" Switch,"), SL(" Case,"), SL(" Statements,"), SL(")"), SL(""), SL("union Code (Stmt, TypeExpr)"), SL(""), SL("# Every code value prints as its Star99 source, the way star99 fmt writes"), SL("# it: no more parentheses than the grammar needs (and not (a == b) for a"), SL("# binary operand of not), four spaces of indentation. str(code) is the text"), SL("# of the code; println(code) shows it."), SL("var indentation = 0"), SL(""), SL("# A 6 marks a name a template of a module wrote: kept only in the text that"), SL("# goes back to the compiler, so the name resolves in that module."), SL("var keep_marks = false"), SL(""), SL("fn print_marked(name: String) {"), SL(" if len(name) > 0 and name[0] == '\\x06' and not keep_marks {"), SL(" var rest: String"), SL(" for i in 1.. String {"), SL(" keep_marks = true"), SL(" const t = str(c)"), SL(" keep_marks = false"), SL(" return t"), SL("}"), SL(""), SL("fn print(e: Name) do print_marked(e.name)"), SL("fn print(e: Literal) do print(e.text)"), SL(""), SL("fn print_indent() {"), SL(" for i in 0.. Int {"), SL(" switch op {"), SL(" case \"**\": return 8"), SL(" case \"*\", \"/\", \"%\": return 6"), SL(" case \"+\", \"-\": return 5"), SL(" case \"..<\", \"..=\", \"<..<\", \"<..=\": return 4"), SL(" case \"==\", \"!=\", \"<\", \"<=\", \">\", \">=\", \"in\", \"not in\": return 3"), SL(" case \"and\": return 1"), SL(" case \"or\": return 0"), SL(" default: return 9"), SL(" }"), SL("}"), SL(""), SL("fn binding(e: Expr) -> Int {"), SL(" switch e {"), SL(" case Binary: return precedence(e.op)"), SL(" case Unary: {"), SL(" if e.op == \"not\" do return 2"), SL(" if e.op == \"..<\" or e.op == \"..=\" do return 4"), SL(" return 7"), SL(" }"), SL(" case Addr: return 7"), SL(" case IfValue, SwitchValue, Closure: return -1"), SL(" case Literal: return (if len("), SL(" e.text, # -3 binds as a sign"), SL(" ) > 0 and e.text[0] == '-' do 7 else 9)"), SL(" default: return 9"), SL(" }"), SL("}"), SL(""), SL("# An operand, in parentheses when it binds looser than its place needs."), SL("fn print_operand(e: Expr, least: Int) {"), SL(" if binding(e) < least do print(\"(\", e, \")\") else print(e)"), SL("}"), SL(""), SL("fn print_arguments(arguments: List(Expr), names: List(String)) {"), SL(" for i, a in arguments {"), SL(" if i > 0 do print(\", \")"), SL(" if i < len(names) and len(names[i]) > 0 do print(names[i], \": \")"), SL(" print(a)"), SL(" }"), SL("}"), SL(""), SL("fn print(e: Call) {"), SL(" print_operand(e.callee, 9)"), SL(" print(\"(\")"), SL(" print_arguments(e.arguments, e.names)"), SL(" print(\")\")"), SL("}"), SL(""), SL("fn print(e: Field) {"), SL(" print_operand(e.base, 9)"), SL(" print(\".\", e.name)"), SL("}"), SL(""), SL("fn print(e: Binary) {"), SL(" const p = precedence(e.op)"), SL(" if p == 8 { # a ** b groups to the right and binds tighter than -a"), SL(" print_operand(e.left, 9)"), SL(" print(\" ** \")"), SL(" print_operand(e.right, 7)"), SL(" return"), SL(" }"), SL(" print_operand(e.left, if p == 3 or p == 4 do p + 1 else p)"), SL(" if p == 4 do print(e.op) else print(\" \", e.op, \" \")"), SL(" print_operand(e.right, p + 1)"), SL("}"), SL(""), SL("fn print(e: Unary) {"), SL(" if e.op == \"not\" {"), SL(" print(\"not \")"), SL(" switch e.operand {"), SL(" case Binary: print(\"(\", e.operand, \")\")"), SL(" default: print_operand(e.operand, 2)"), SL(" }"), SL(" } else {"), SL(" print(e.op)"), SL(" const is_range = e.op == \"..<\" or e.op == \"..=\""), SL(" print_operand(e.operand, if is_range do 5 else 7)"), SL(" }"), SL("}"), SL(""), SL("fn print(e: Index) {"), SL(" print_operand(e.base, 9)"), SL(" print(\"[\")"), SL(" print_arguments(e.indices, [])"), SL(" print(\"]\")"), SL("}"), SL(""), SL("fn print(e: Deref) {"), SL(" print_operand(e.operand, 9)"), SL(" print(\".*\")"), SL("}"), SL(""), SL("fn print(e: Addr) {"), SL(" print(\"&\")"), SL(" print_operand(e.operand, 7)"), SL("}"), SL(""), SL("fn print(e: ListLiteral) {"), SL(" print(\"[\")"), SL(" print_arguments(e.items, [])"), SL(" print(\"]\")"), SL("}"), SL(""), SL("fn print(e: MapLiteral) {"), SL(" if len(e.entries) == 0 {"), SL(" print(\"[:]\")"), SL(" return"), SL(" }"), SL(" print(\"[\")"), SL(" for i, x in e.entries {"), SL(" if i > 0 do print(\", \")"), SL(" print(x.key, \": \", x.value)"), SL(" }"), SL(" print(\"]\")"), SL("}"), SL(""), SL("fn print(e: Anonymous) {"), SL(" print(\".(\")"), SL(" print_arguments(e.arguments, e.names)"), SL(" print(\")\")"), SL("}"), SL(""), SL("fn print(e: Variant) {"), SL(" print(\".\", e.name)"), SL(" if not e.called do return"), SL(" print(\"(\")"), SL(" print_arguments(e.arguments, e.names)"), SL(" print(\")\")"), SL("}"), SL(""), SL("fn print(e: TypeValue) do print(e.type)"), SL("fn print(e: BlockValue) do print(e.body)"), SL(""), SL("fn print(e: IfValue) {"), SL(" print(\"if \", e.condition)"), SL(" switch e.then {"), SL(" case BlockValue: print(\" \")"), SL(" default: print(\" do \")"), SL(" }"), SL(" print_operand(e.then, 0) # an if inside needs parentheses"), SL(" print(\" else \", e.otherwise)"), SL("}"), SL(""), SL("fn print(e: SwitchValue) do print(e.value)"), SL(""), SL("fn print(e: Closure) {"), SL(" if e.compile_time do print(\"macro \")"), SL(" print(\"fn \", e.params)"), SL(" if e.result != nil do print(\" -> \", e.result)"), SL(" print(\" \")"), SL(" print_body(e.body, e.do_body)"), SL("}"), SL(""), SL("fn print(e: Template) {"), SL(" if e.statements {"), SL(" print(\"code \", e.body)"), SL(" } else {"), SL(" print(\"code(\")"), SL(" if len(e.body) > 0 do print(e.body[0])"), SL(" print(\")\")"), SL(" }"), SL("}"), SL(""), SL("fn print(e: Splice) {"), SL(" switch e.value {"), SL(" case Name: print(\"$\", e.value)"), SL(" default: print(\"$(\", e.value, \")\")"), SL(" }"), SL("}"), SL(""), SL("fn print(e: Expr) {"), SL(" switch e {"), SL(" case Name: print(e)"), SL(" case Literal: print(e)"), SL(" case Call: print(e)"), SL(" case Field: print(e)"), SL(" case Binary: print(e)"), SL(" case Unary: print(e)"), SL(" case Index: print(e)"), SL(" case Deref: print(e)"), SL(" case Addr: print(e)"), SL(" case ListLiteral: print(e)"), SL(" case MapLiteral: print(e)"), SL(" case Anonymous: print(e)"), SL(" case Variant: print(e)"), SL(" case TypeValue: print(e)"), SL(" case BlockValue: print(e)"), SL(" case IfValue: print(e)"), SL(" case SwitchValue: print(e)"), SL(" case Closure: print(e)"), SL(" case Template: print(e)"), SL(" case Splice: print(e)"), SL(" case nil: print(\"nil\")"), SL(" }"), SL("}"), SL(""), SL("fn print(xs: List(TypeExpr)) {"), SL(" for i, x in xs {"), SL(" if i > 0 do print(\", \")"), SL(" print(x)"), SL(" }"), SL("}"), SL(""), SL("fn print(t: TypeName) {"), SL(" print_marked(t.name)"), SL(" if len(t.arguments) == 0 do return"), SL(" print(\"(\", t.arguments, \")\")"), SL("}"), SL(""), SL("fn print(t: FunctionType) {"), SL(" print(\"fn (\", t.params, \")\")"), SL(" if t.result != nil do print(\" -> \", t.result)"), SL("}"), SL(""), SL("fn print(t: StructType) do print(\"struct \", t.fields)"), SL(""), SL("fn print(t: UnionType) {"), SL(" if t.no_nil do print(\"no_nil(\")"), SL(" print(\"union \", t.variants)"), SL(" if t.no_nil do print(\")\")"), SL("}"), SL(""), SL("fn print(t: TypeCall) do print(t.call)"), SL("fn print(t: Size) do print(t.value)"), SL(""), SL("fn print(t: TypeExpr) {"), SL(" switch t {"), SL(" case TypeName: print(t)"), SL(" case FunctionType: print(t)"), SL(" case StructType: print(t)"), SL(" case UnionType: print(t)"), SL(" case TypeCall: print(t)"), SL(" case Size: print(t)"), SL(" case nil:"), SL(" }"), SL("}"), SL(""), SL("fn print(p: Param) {"), SL(" if p.is_private do print(\"private \")"), SL(" if p.is_var do print(\"var \")"), SL(" if len(p.name) == 0 {"), SL(" print(p.type)"), SL(" return"), SL(" }"), SL(" print(p.name)"), SL(" if p.type != nil do print(\": \", p.type)"), SL(" if p.value != nil do print(\" = \", p.value)"), SL("}"), SL(""), SL("# Parameters of one type print as a group, a, b: Int, as fmt writes them."), SL("fn same_group(a, b: Param) -> Bool {"), SL(" return len("), SL(" a.name,"), SL(" ) > 0 and len("), SL(" b.name,"), SL(" ) > 0 and a.value == nil and b.value == nil and a.is_var == b.is_var and a.is_private == b.is_private and a.type != nil and str("), SL(" a.type,"), SL(" ) == str(b.type)"), SL("}"), SL(""), SL("fn print(ps: List(Param)) {"), SL(" print(\"(\")"), SL(" for i, p in ps {"), SL(" if i > 0 do print(\", \")"), SL(" const next_same = i + 1 < len(ps) and same_group(p, ps[i + 1])"), SL(" const after_same = i > 0 and same_group(ps[i - 1], p)"), SL(" if p.is_private and not after_same do print(\"private \")"), SL(" if p.is_var and not after_same do print(\"var \")"), SL(" if len(p.name) == 0 { # a variant written as its type: union (Tree)"), SL(" print(p.type)"), SL(" continue"), SL(" }"), SL(" print(p.name)"), SL(" if next_same do continue"), SL(" if p.type != nil do print(\": \", p.type)"), SL(" if p.value != nil do print(\" = \", p.value)"), SL(" }"), SL(" print(\")\")"), SL("}"), SL(""), SL("fn print(body: List(Stmt)) {"), SL(" if len(body) == 0 {"), SL(" print(\"{}\")"), SL(" return"), SL(" }"), SL(" print(\"{\\n\")"), SL(" indentation += 1"), SL(" for st in body {"), SL(" print_indent()"), SL(" print(st, \"\\n\")"), SL(" }"), SL(" indentation -= 1"), SL(" print_indent()"), SL(" print(\"}\")"), SL("}"), SL(""), SL("# A body written with do: one statement"), SL("fn print_body(body: List(Stmt), do_body: Bool) {"), SL(" if do_body and len(body) == 1 {"), SL(" print(\"do \", body[0])"), SL(" return"), SL(" }"), SL(" print(body)"), SL("}"), SL(""), SL("fn print(d: Function) {"), SL(" if d.is_private do print(\"private \")"), SL(" if d.parenless do print(\"parenless(\")"), SL(" if d.compile_time do print(\"macro \")"), SL(" print(\"fn \", d.name, d.params)"), SL(" if d.result != nil do print(\" -> \", d.result)"), SL(" print(\" \")"), SL(" # one expression is a do body"), SL(" var done = false"), SL(" if len(d.body) == 1 {"), SL(" switch d.body[0] {"), SL(" case Expr: {"), SL(" print(\"do \", d.body[0])"), SL(" done = true"), SL(" }"), SL(" default:"), SL(" }"), SL(" }"), SL(" if not done do print(d.body)"), SL(" if d.parenless do print(\")\")"), SL("}"), SL(""), SL("fn print_prefix(compile_time, is_private: Bool) {"), SL(" if is_private do print(\"private \")"), SL(" if compile_time do print(\"macro \")"), SL("}"), SL(""), SL("fn print(d: Struct) {"), SL(" print_prefix(d.compile_time, d.is_private)"), SL(" print(\"struct \", d.name, \" \", d.fields)"), SL("}"), SL(""), SL("fn print(d: Union) {"), SL(" print_prefix(d.compile_time, d.is_private)"), SL(" if d.no_nil do print(\"no_nil(\")"), SL(" print(\"union \", d.name, \" \", d.variants)"), SL(" if d.no_nil do print(\")\")"), SL("}"), SL(""), SL("fn print(d: Global) {"), SL(" print_prefix(d.compile_time, d.is_private)"), SL(" print(if d.constant do \"const \" else \"var \", d.name)"), SL(" if d.type != nil do print(\": \", d.type)"), SL(" if d.value != nil do print(\" = \", d.value)"), SL("}"), SL(""), SL("fn print(d: Import) {"), SL(" if d.compile_time do print(\"macro \")"), SL(" print(\"import \")"), SL(" print_quoted(d.path)"), SL(" if len(d.arguments) > 0 {"), SL(" print(\" (\")"), SL(" print_arguments(d.arguments, [])"), SL(" print(\")\")"), SL(" }"), SL(" if len(d.alias) > 0 do print(\" as \", d.alias)"), SL("}"), SL(""), SL("fn print(d: Group) {"), SL(" if d.compile_time or d.is_private {"), SL(" print_prefix(d.compile_time, d.is_private)"), SL(" print(d.body)"), SL(" return"), SL(" }"), SL(" print(Statements(body: d.body, site: d.site)) # var a, b: Int"), SL("}"), SL(""), SL("fn print(d: MacroLabel) do print(\"macro label \", d.name, \" = \", d.body)"), SL("fn print(d: Module) do print(\"module \", d.params)"), SL(""), SL("fn print(d: Collection) {"), SL(" print(\"collection \", d.name, \" = \")"), SL(" print_quoted(d.path)"), SL("}"), SL(""), SL("fn print(d: Decl) {"), SL(" switch d {"), SL(" case Function: print(d)"), SL(" case Struct: print(d)"), SL(" case Union: print(d)"), SL(" case Global: print(d)"), SL(" case Import: print(d)"), SL(" case Group: print(d)"), SL(" case MacroLabel: print(d)"), SL(" case Module: print(d)"), SL(" case Collection: print(d)"), SL(" case nil:"), SL(" }"), SL("}"), SL(""), SL("fn print(s: For) {"), SL(" if len(s.loop_label) > 0 do print(\"label \", s.loop_label, \" = \")"), SL(" if s.compile_time do print(\"macro \")"), SL(" print(\"for \")"), SL(" if s.variant != nil {"), SL(" print(s.binding, \": \", s.variant, \" in \")"), SL(" } else if len(s.binding) > 0 {"), SL(" if len(s.index) > 0 do print(s.index, \", \")"), SL(" print(s.binding, \" in \")"), SL(" }"), SL(" print(s.source, \" \")"), SL(" print_body(s.body, s.do_body)"), SL("}"), SL(""), SL("fn print(s: Return) {"), SL(" print(\"return\")"), SL(" if s.value != nil {"), SL(" print(\" \")"), SL(" print_operand(s.value, 0) # return (if ...) keeps its else"), SL(" }"), SL("}"), SL(""), SL("fn print(s: Assign) do print(s.target, \" \", s.op, \" \", s.value)"), SL(""), SL("fn is_if(s: Stmt) -> Bool {"), SL(" switch s {"), SL(" case If: return true"), SL(" default: return false"), SL(" }"), SL("}"), SL(""), SL("fn print(s: If) {"), SL(" if s.compile_time do print(\"macro \")"), SL(" print(\"if \", s.condition, \" \")"), SL(" print_body(s.then, s.do_body)"), SL(" if len(s.otherwise) == 0 do return"), SL(" print(\" else \")"), SL(" if len(s.otherwise) == 1 and (s.do_else or is_if(s.otherwise[0])) {"), SL(" print(s.otherwise[0])"), SL(" } else {"), SL(" print(s.otherwise)"), SL(" }"), SL("}"), SL(""), SL("fn print(s: While) {"), SL(" print(\"while \", s.condition, \" \")"), SL(" print_body(s.body, s.do_body)"), SL("}"), SL(""), SL("fn print(s: Block) do print(s.body)"), SL(""), SL("fn print(s: Break) {"), SL(" print(\"break\")"), SL(" if len(s.target) > 0 do print(\" \", s.target)"), SL("}"), SL(""), SL("fn print(s: Continue) {"), SL(" print(\"continue\")"), SL(" if len(s.target) > 0 do print(\" \", s.target)"), SL("}"), SL(""), SL("fn print(s: Defer) do print(\"defer \", s.body)"), SL("fn print(s: Discard) do print(\"_ = \", s.value)"), SL("fn print(s: Labeled) do print(\"label \", s.name, \" = \", s.body)"), SL(""), SL("fn print(s: Switch) {"), SL(" if s.compile_time do print(\"macro \")"), SL(" print(\"switch \")"), SL(" if s.by_ref do print(\"&\")"), SL(" if len(s.binding) > 0 do print(s.binding, \" in \")"), SL(" print(s.value, \" {\\n\")"), SL(" for c in s.cases {"), SL(" print_indent()"), SL(" print(c, \"\\n\")"), SL(" }"), SL(" print_indent()"), SL(" print(\"}\")"), SL("}"), SL(""), SL("# One simple statement stays on its case line"), SL("fn simple(s: Stmt) -> Bool {"), SL(" switch s {"), SL(" case If: return false"), SL(" case For: return false"), SL(" case While: return false"), SL(" case Switch: return false"), SL(" case Block: return false"), SL(" case Labeled: return false"), SL(" default: return true"), SL(" }"), SL("}"), SL(""), SL("fn is_block(s: Stmt) -> Bool {"), SL(" switch s {"), SL(" case Block: return true"), SL(" default: return false"), SL(" }"), SL("}"), SL(""), SL("fn print(c: Case) {"), SL(" if len(c.case_label) > 0 do print(\"label \", c.case_label, \" = \")"), SL(" if len(c.patterns) == 0 {"), SL(" print(\"default:\")"), SL(" } else {"), SL(" print(\"case \")"), SL(" print_arguments(c.patterns, [])"), SL(" print(\":\")"), SL(" }"), SL(" if len(c.body) == 1 and (simple(c.body[0]) or is_block(c.body[0])) {"), SL(" print(\" \", c.body[0]) # case X: { ... }"), SL(" return"), SL(" }"), SL(" indentation += 1"), SL(" for st in c.body {"), SL(" print(\"\\n\")"), SL(" print_indent()"), SL(" print(st)"), SL(" }"), SL(" indentation -= 1"), SL("}"), SL(""), SL("fn print(s: Statements) {"), SL(" for i, st in s.body {"), SL(" if i > 0 {"), SL(" print(\"\\n\")"), SL(" print_indent()"), SL(" }"), SL(" print(st)"), SL(" }"), SL("}"), SL(""), SL("fn print(s: Stmt) {"), SL(" switch s {"), SL(" case For: print(s)"), SL(" case Return: print(s)"), SL(" case Assign: print(s)"), SL(" case Expr: {"), SL(" # (if ...) stays an expression"), SL(" print_operand(s, 0)"), SL(" }"), SL(" case Decl: print(s)"), SL(" case If: print(s)"), SL(" case While: print(s)"), SL(" case Block: print(s)"), SL(" case Break: print(s)"), SL(" case Continue: print(s)"), SL(" case Defer: print(s)"), SL(" case Discard: print(s)"), SL(" case Labeled: print(s)"), SL(" case Switch: print(s)"), SL(" case Case: print(s)"), SL(" case Statements: print(s)"), SL(" case nil:"), SL(" }"), SL("}"), SL(""), SL("fn print(c: Code) {"), SL(" switch c {"), SL(" case Stmt: print(c)"), SL(" case TypeExpr: print(c)"), SL(" case nil:"), SL(" }"), SL("}"), SL(""), SL("fn site_of(e: Expr) -> Site {"), SL(" switch e {"), SL(" case Name: return e.site"), SL(" case Literal: return e.site"), SL(" case Call: return e.site"), SL(" case Field: return e.site"), SL(" case Binary: return e.site"), SL(" case Unary: return e.site"), SL(" case Index: return e.site"), SL(" case Deref: return e.site"), SL(" case Addr: return e.site"), SL(" case ListLiteral: return e.site"), SL(" case MapLiteral: return e.site"), SL(" case Anonymous: return e.site"), SL(" case Variant: return e.site"), SL(" case TypeValue: return e.site"), SL(" case BlockValue: return e.site"), SL(" case IfValue: return e.site"), SL(" case SwitchValue: return e.site"), SL(" case Closure: return e.site"), SL(" case Template: return e.site"), SL(" case Splice: return e.site"), SL(" case nil: return Site()"), SL(" }"), SL("}"), SL(""), SL("fn site_of(t: TypeExpr) -> Site {"), SL(" switch t {"), SL(" case TypeName: return t.site"), SL(" case FunctionType: return t.site"), SL(" case StructType: return t.site"), SL(" case UnionType: return t.site"), SL(" case TypeCall: return t.site"), SL(" case Size: return t.site"), SL(" case nil: return Site()"), SL(" }"), SL("}"), SL(""), SL("fn site_of(d: Decl) -> Site {"), SL(" switch d {"), SL(" case Function: return d.site"), SL(" case Struct: return d.site"), SL(" case Union: return d.site"), SL(" case Global: return d.site"), SL(" case Import: return d.site"), SL(" case Group: return d.site"), SL(" case MacroLabel: return d.site"), SL(" case Module: return d.site"), SL(" case Collection: return d.site"), SL(" case nil: return Site()"), SL(" }"), SL("}"), SL(""), SL("fn site_of(s: Stmt) -> Site {"), SL(" switch s {"), SL(" case For: return s.site"), SL(" case Return: return s.site"), SL(" case Assign: return s.site"), SL(" case Expr: return site_of(s)"), SL(" case Decl: return site_of(s)"), SL(" case If: return s.site"), SL(" case While: return s.site"), SL(" case Block: return s.site"), SL(" case Break: return s.site"), SL(" case Continue: return s.site"), SL(" case Defer: return s.site"), SL(" case Discard: return s.site"), SL(" case Labeled: return s.site"), SL(" case Switch: return s.site"), SL(" case Case: return s.site"), SL(" case Statements: return s.site"), SL(" case nil: return Site()"), SL(" }"), SL("}"), SL(""), SL("fn site_of(c: Code) -> Site {"), SL(" switch c {"), SL(" case Stmt: return site_of(c)"), SL(" case TypeExpr: return site_of(c)"), SL(" case nil: return Site()"), SL(" }"), SL("}"), SL(""), SL("# A name for generated code; no two calls in one program give the same."), SL("var fresh_count = -1"), SL(""), SL("fn fresh(base: String) -> String {"), SL(" # past every name__N the program already has"), SL(" if fresh_count < 0 do fresh_count = last_number("), SL(" str(\"0 \", getenv(\"STAR99_FRESH\")),"), SL(" )"), SL(" fresh_count += 1"), SL(" return str(base, \"__\", fresh_count)"), SL("}"), SL(""), SL("fn name(text: String) -> Expr do Name(name: text)"), SL(""), SL("# Code from source text, parsed by the compiler (while compiling)"), SL("fn parse_expr(text: String) -> Expr do ask(Expr, \"parse\", text, \"expr\")"), SL("fn parse_type(text: String) -> TypeExpr do ask(TypeExpr, \"parse\", text, \"type\")"), SL("fn parse_stmts("), SL(" text: String,"), SL(") -> List(Stmt) do ask(List(Stmt), \"parse\", text, \"stmts\")"), SL(""), SL("# literal(v): the code that builds v, from its text"), SL("fn literal_code(text: String, site: Site) -> Expr {"), SL(" if len("), SL(" text,"), SL(" ) > 0 and (text[0] == '\"' or text[0] == '\\'' or text[0] == '-' or text[0] >= '0' and text[0] <= '9' or text == \"true\" or text == \"false\" or text == \"nil\") {"), SL(" return Literal(text: text, site: site)"), SL(" }"), SL(" return parse_expr(text)"), SL("}"), SL(""), SL("# A $ splice in a template, by where it is and the kind of value spliced:"), SL("# one expression, type, statement or name, or the items of a list."), SL("fn splice_expr(e: Expr) -> Expr do e"), SL("fn splice_expr(t: TypeExpr) -> Expr do TypeValue(type: t, site: site_of(t))"), SL("fn splice_expr(d: Def) -> Expr do Name(name: d.name, site: d.site)"), SL("fn splice_expr(s: String) -> Expr do parse_expr(s)"), SL("fn splice_expr(n: Int) -> Expr do Literal(text: str(n))"), SL("fn splice_expr(f: Float) -> Expr {"), SL(" # infinities and nan as code that builds them, as literal() writes"), SL(" const zero = Literal(text: \"0.0\")"), SL(" if f != f do return Binary(op: \"/\", left: zero, right: zero)"), SL(" if f > 1.7976931348623157e308 do return Binary("), SL(" op: \"/\","), SL(" left: Literal(text: \"1.0\"),"), SL(" right: zero,"), SL(" )"), SL(" if f < -1.7976931348623157e308 do return Binary("), SL(" op: \"/\","), SL(" left: Literal(text: \"-1.0\"),"), SL(" right: zero,"), SL(" )"), SL(" return Literal(text: str(f))"), SL("}"), SL("fn splice_expr(b: Bool) -> Expr do Literal(text: str(b))"), SL(""), SL("fn splice_expr(c: Code) -> Expr {"), SL(" switch c {"), SL(" case Stmt: {"), SL(" switch c {"), SL(" case Expr: return c"), SL(" default:"), SL(" }"), SL(" }"), SL(" case TypeExpr: return TypeValue(type: c, site: site_of(c))"), SL(" case nil:"), SL(" }"), SL(" return parse_expr(str(c))"), SL("}"), SL(""), SL("fn splice_type(t: TypeExpr) -> TypeExpr do t"), SL("fn splice_type(s: String) -> TypeExpr do parse_type(s)"), SL("fn splice_type(d: Def) -> TypeExpr do TypeName(name: d.name, site: d.site)"), SL(""), SL("fn splice_type(c: Code) -> TypeExpr {"), SL(" switch c {"), SL(" case TypeExpr: return c"), SL(" default: return parse_type(str(c))"), SL(" }"), SL("}"), SL(""), SL("fn splice_name(s: String) -> String do s"), SL("fn splice_name(d: Def) -> String do d.name"), SL(""), SL("fn splice_name(e: Expr) -> String {"), SL(" switch e {"), SL(" case Name: return e.name"), SL(" default: return str(e)"), SL(" }"), SL("}"), SL(""), SL("fn splice_stmt(c: Code) -> Stmt {"), SL(" switch c {"), SL(" case Stmt: return c"), SL(" default: return Statements(body: parse_stmts(str(c)))"), SL(" }"), SL("}"), SL(""), SL("fn splice_stmt(s: String) -> Stmt do Statements(body: parse_stmts(s))"), SL(""), SL("fn splice_exprs(e: Expr) -> List(Expr) do [e]"), SL("fn splice_exprs(xs: List(Expr)) -> List(Expr) do xs"), SL("fn splice_exprs(s: String) -> List(Expr) do [parse_expr(s)]"), SL("fn splice_exprs(n: Int) -> List(Expr) do [splice_expr(n)]"), SL("fn splice_exprs(f: Float) -> List(Expr) do [splice_expr(f)]"), SL("fn splice_exprs(b: Bool) -> List(Expr) do [splice_expr(b)]"), SL("fn splice_exprs(c: Code) -> List(Expr) do [splice_expr(c)]"), SL("fn splice_exprs(d: Def) -> List(Expr) do [splice_expr(d)]"), SL(""), SL("fn splice_exprs(cs: List(Code)) -> List(Expr) {"), SL(" var out: List(Expr)"), SL(" for c in cs do add(&out, splice_expr(c))"), SL(" return out"), SL("}"), SL(""), SL("fn splice_stmts(c: Code) -> List(Stmt) do [splice_stmt(c)]"), SL("fn splice_stmts(s: String) -> List(Stmt) do parse_stmts(s)"), SL("fn splice_stmts(xs: List(Stmt)) -> List(Stmt) do xs"), SL(""), SL("fn splice_stmts(xs: List(Code)) -> List(Stmt) {"), SL(" var out: List(Stmt)"), SL(" for x in xs do add(&out, splice_stmt(x))"), SL(" return out"), SL("}"), SL(""), SL("fn splice_stmts(xs: List(Expr)) -> List(Stmt) {"), SL(" var out: List(Stmt)"), SL(" for x in xs do add(&out, x)"), SL(" return out"), SL("}"), SL(""), SL("fn splice_stmts(xs: List(Decl)) -> List(Stmt) {"), SL(" var out: List(Stmt)"), SL(" for x in xs do add(&out, x)"), SL(" return out"), SL("}"), SL(""), SL("fn splice_params(p: Param) -> List(Param) do [p]"), SL("fn splice_params(xs: List(Param)) -> List(Param) do xs"), SL("fn splice_params(s: String) -> List(Param) do [Param(name: s)]"), SL(""), SL("fn splice_types(t: TypeExpr) -> List(TypeExpr) do [t]"), SL("fn splice_types(xs: List(TypeExpr)) -> List(TypeExpr) do xs"), SL("fn splice_types(s: String) -> List(TypeExpr) do [parse_type(s)]"), SL("fn splice_types(c: Code) -> List(TypeExpr) do [splice_type(c)]"), SL(""), SL("fn splice_decl(d: Decl) -> Decl do d"), SL(""), SL("fn splice_decl(c: Code) -> Decl {"), SL(" switch c {"), SL(" case Stmt: {"), SL(" switch c {"), SL(" case Decl: return c"), SL(" default:"), SL(" }"), SL(" }"), SL(" default:"), SL(" }"), SL(" fail(str(\"this code is not a declaration: \", c))"), SL(" return nil"), SL("}"), SL(""), SL("# The items of an encoded list, without its length"), SL("fn items_text(s: String) -> String {"), SL(" var out: String"), SL(" var seen = false"), SL(" for c in s {"), SL(" if seen do add(&out, c) else if c == ';' do seen = true"), SL(" }"), SL(" return out"), SL("}"), SL(""), SL("# The encoded names of n arguments without names"), SL("fn blank_names(n: Int) -> String {"), SL(" var out: String"), SL(" for i in 0.. TypeExpr do ask(TypeExpr, \"type_at\", site_of(e))"), SL("fn def_of(n: Name) -> union (Def) do ask(union (Def), \"def_at\", n.site)"), SL("fn local_of(n: Name) -> union (Local) do ask(union (Local), \"local_at\", n.site)"), SL("fn calls_at(c: Call) -> List(Def) do ask(List(Def), \"calls_at\", c.site)"), SL(""), SL("# Every node of a piece of code, as walk gives them"), SL("union Node ("), SL(" Name,"), SL(" Literal,"), SL(" Call,"), SL(" Field,"), SL(" Binary,"), SL(" Unary,"), SL(" Index,"), SL(" Deref,"), SL(" Addr,"), SL(" ListLiteral,"), SL(" MapLiteral,"), SL(" Entry,"), SL(" Anonymous,"), SL(" Variant,"), SL(" TypeValue,"), SL(" BlockValue,"), SL(" IfValue,"), SL(" SwitchValue,"), SL(" Closure,"), SL(" Template,"), SL(" Splice,"), SL(" TypeName,"), SL(" FunctionType,"), SL(" StructType,"), SL(" UnionType,"), SL(" TypeCall,"), SL(" Size,"), SL(" Param,"), SL(" Function,"), SL(" Struct,"), SL(" Union,"), SL(" Global,"), SL(" Import,"), SL(" Group,"), SL(" MacroLabel,"), SL(" Module,"), SL(" Collection,"), SL(" For,"), SL(" Return,"), SL(" Assign,"), SL(" If,"), SL(" While,"), SL(" Block,"), SL(" Break,"), SL(" Continue,"), SL(" Defer,"), SL(" Discard,"), SL(" Labeled,"), SL(" Switch,"), SL(" Case,"), SL(" Statements,"), SL(")"), SL(""), SL("# walk(code): every node in it, each before the nodes inside it, in source"), SL("# order: for c: Call in walk(f.body) { ... }"), SL("fn walk(e: Expr) -> List(Node) {"), SL(" var out: List(Node)"), SL(" visit(&out, e)"), SL(" return out"), SL("}"), SL(""), SL("fn walk(t: TypeExpr) -> List(Node) {"), SL(" var out: List(Node)"), SL(" visit(&out, t)"), SL(" return out"), SL("}"), SL(""), SL("fn walk(s: Stmt) -> List(Node) {"), SL(" var out: List(Node)"), SL(" visit(&out, s)"), SL(" return out"), SL("}"), SL(""), SL("fn walk(c: Code) -> List(Node) {"), SL(" var out: List(Node)"), SL(" switch c {"), SL(" case Stmt: visit(&out, c)"), SL(" case TypeExpr: visit(&out, c)"), SL(" case nil:"), SL(" }"), SL(" return out"), SL("}"), SL(""), SL("fn walk(body: List(Stmt)) -> List(Node) {"), SL(" var out: List(Node)"), SL(" for s in body do visit(&out, s)"), SL(" return out"), SL("}"), SL(""), SL("fn visit(var out: List(Node), es: List(Expr)) {"), SL(" for e in es do visit(&out, e)"), SL("}"), SL(""), SL("fn visit(var out: List(Node), body: List(Stmt)) {"), SL(" for s in body do visit(&out, s)"), SL("}"), SL(""), SL("fn visit(var out: List(Node), ps: List(Param)) {"), SL(" for p in ps {"), SL(" add(&out, p)"), SL(" visit(&out, p.type)"), SL(" visit(&out, p.value)"), SL(" }"), SL("}"), SL(""), SL("fn visit(var out: List(Node), ts: List(TypeExpr)) {"), SL(" for t in ts do visit(&out, t)"), SL("}"), SL(""), SL("fn visit(var out: List(Node), e: Expr) {"), SL(" switch e {"), SL(" case Name: add(&out, e)"), SL(" case Literal: add(&out, e)"), SL(" case Call: {"), SL(" add(&out, e)"), SL(" visit(&out, e.callee)"), SL(" visit(&out, e.arguments)"), SL(" }"), SL(" case Field: {"), SL(" add(&out, e)"), SL(" visit(&out, e.base)"), SL(" }"), SL(" case Binary: {"), SL(" add(&out, e)"), SL(" visit(&out, e.left)"), SL(" visit(&out, e.right)"), SL(" }"), SL(" case Unary: {"), SL(" add(&out, e)"), SL(" visit(&out, e.operand)"), SL(" }"), SL(" case Index: {"), SL(" add(&out, e)"), SL(" visit(&out, e.base)"), SL(" visit(&out, e.indices)"), SL(" }"), SL(" case Deref: {"), SL(" add(&out, e)"), SL(" visit(&out, e.operand)"), SL(" }"), SL(" case Addr: {"), SL(" add(&out, e)"), SL(" visit(&out, e.operand)"), SL(" }"), SL(" case ListLiteral: {"), SL(" add(&out, e)"), SL(" visit(&out, e.items)"), SL(" }"), SL(" case MapLiteral: {"), SL(" add(&out, e)"), SL(" for x in e.entries {"), SL(" add(&out, x)"), SL(" visit(&out, x.key)"), SL(" visit(&out, x.value)"), SL(" }"), SL(" }"), SL(" case Anonymous: {"), SL(" add(&out, e)"), SL(" visit(&out, e.arguments)"), SL(" }"), SL(" case Variant: {"), SL(" add(&out, e)"), SL(" visit(&out, e.arguments)"), SL(" }"), SL(" case TypeValue: {"), SL(" add(&out, e)"), SL(" visit(&out, e.type)"), SL(" }"), SL(" case BlockValue: {"), SL(" add(&out, e)"), SL(" visit(&out, e.body)"), SL(" }"), SL(" case IfValue: {"), SL(" add(&out, e)"), SL(" visit(&out, e.condition)"), SL(" visit(&out, e.then)"), SL(" visit(&out, e.otherwise)"), SL(" }"), SL(" case SwitchValue: {"), SL(" add(&out, e)"), SL(" visit_switch(&out, e.value)"), SL(" }"), SL(" case Closure: {"), SL(" add(&out, e)"), SL(" visit(&out, e.params)"), SL(" visit(&out, e.result)"), SL(" visit(&out, e.body)"), SL(" }"), SL(" case Template: {"), SL(" add(&out, e)"), SL(" visit(&out, e.body)"), SL(" }"), SL(" case Splice: {"), SL(" add(&out, e)"), SL(" visit(&out, e.value)"), SL(" }"), SL(" case nil:"), SL(" }"), SL("}"), SL(""), SL("fn visit(var out: List(Node), t: TypeExpr) {"), SL(" switch t {"), SL(" case TypeName: {"), SL(" add(&out, t)"), SL(" visit(&out, t.arguments)"), SL(" }"), SL(" case FunctionType: {"), SL(" add(&out, t)"), SL(" visit(&out, t.params)"), SL(" visit(&out, t.result)"), SL(" }"), SL(" case StructType: {"), SL(" add(&out, t)"), SL(" visit(&out, t.fields)"), SL(" }"), SL(" case UnionType: {"), SL(" add(&out, t)"), SL(" visit(&out, t.variants)"), SL(" }"), SL(" case TypeCall: {"), SL(" add(&out, t)"), SL(" visit(&out, Expr(t.call))"), SL(" }"), SL(" case Size: {"), SL(" add(&out, t)"), SL(" visit(&out, t.value)"), SL(" }"), SL(" case nil:"), SL(" }"), SL("}"), SL(""), SL("fn visit_switch(var out: List(Node), s: Switch) {"), SL(" add(&out, s)"), SL(" visit(&out, s.value)"), SL(" for c in s.cases do visit_case(&out, c)"), SL("}"), SL(""), SL("fn visit_case(var out: List(Node), c: Case) {"), SL(" add(&out, c)"), SL(" visit(&out, c.patterns)"), SL(" visit(&out, c.body)"), SL("}"), SL(""), SL("fn visit(var out: List(Node), d: Decl) {"), SL(" switch d {"), SL(" case Function: {"), SL(" add(&out, d)"), SL(" visit(&out, d.params)"), SL(" visit(&out, d.result)"), SL(" visit(&out, d.body)"), SL(" }"), SL(" case Struct: {"), SL(" add(&out, d)"), SL(" visit(&out, d.fields)"), SL(" }"), SL(" case Union: {"), SL(" add(&out, d)"), SL(" visit(&out, d.variants)"), SL(" }"), SL(" case Global: {"), SL(" add(&out, d)"), SL(" visit(&out, d.type)"), SL(" visit(&out, d.value)"), SL(" }"), SL(" case Import: {"), SL(" add(&out, d)"), SL(" visit(&out, d.arguments)"), SL(" }"), SL(" case Group: {"), SL(" add(&out, d)"), SL(" visit(&out, d.body)"), SL(" }"), SL(" case MacroLabel: {"), SL(" add(&out, d)"), SL(" visit(&out, d.body)"), SL(" }"), SL(" case Module: {"), SL(" add(&out, d)"), SL(" visit(&out, d.params)"), SL(" }"), SL(" case Collection: add(&out, d)"), SL(" case nil:"), SL(" }"), SL("}"), SL(""), SL("fn visit(var out: List(Node), s: Stmt) {"), SL(" switch s {"), SL(" case For: {"), SL(" add(&out, s)"), SL(" visit(&out, s.variant)"), SL(" visit(&out, s.source)"), SL(" visit(&out, s.body)"), SL(" }"), SL(" case Return: {"), SL(" add(&out, s)"), SL(" visit(&out, s.value)"), SL(" }"), SL(" case Assign: {"), SL(" add(&out, s)"), SL(" visit(&out, s.target)"), SL(" visit(&out, s.value)"), SL(" }"), SL(" case Expr: visit(&out, s)"), SL(" case Decl: visit(&out, s)"), SL(" case If: {"), SL(" add(&out, s)"), SL(" visit(&out, s.condition)"), SL(" visit(&out, s.then)"), SL(" visit(&out, s.otherwise)"), SL(" }"), SL(" case While: {"), SL(" add(&out, s)"), SL(" visit(&out, s.condition)"), SL(" visit(&out, s.body)"), SL(" }"), SL(" case Block: {"), SL(" add(&out, s)"), SL(" visit(&out, s.body)"), SL(" }"), SL(" case Break: add(&out, s)"), SL(" case Continue: add(&out, s)"), SL(" case Defer: {"), SL(" add(&out, s)"), SL(" visit(&out, s.body)"), SL(" }"), SL(" case Discard: {"), SL(" add(&out, s)"), SL(" visit(&out, s.value)"), SL(" }"), SL(" case Labeled: {"), SL(" add(&out, s)"), SL(" visit(&out, s.body)"), SL(" }"), SL(" case Switch: visit_switch(&out, s)"), SL(" case Case: visit_case(&out, s)"), SL(" case Statements: {"), SL(" add(&out, s)"), SL(" visit(&out, s.body)"), SL(" }"), SL(" case nil:"), SL(" }"), SL("}"), }; // std:prelude, which every file sees without an import. static Str std_prelude_lines[] = { SL("# std:prelude: available in every file without an import."), SL("import \"std:code\""), SL(""), SL("# assert(condition) stops the program when the condition is false, naming"), SL("# the condition, where it is, and for a comparison the values of both sides."), SL("macro fn assert(c: Expr) -> Code do assert_code(c, \"\")"), SL(""), SL("macro fn assert("), SL(" c: Expr,"), SL(" message: String,"), SL(") -> Code do assert_code(c, str(\": \", message))"), SL(""), SL("# reversed(for x in xs { ... }) visits the list, string or range backward."), SL("# The length and bounds are taken once, as a forward loop takes them, and"), SL("# each step reads the list as it is then (an index error once it shrank)."), SL("macro fn reversed(f: For) -> Code {"), SL(" const x = name(f.binding)"), SL(" const loop = name(if len(f.loop_label) > 0 do f.loop_label else f.binding)"), SL(" const body = f.body"), SL(" const index = if len(f.index) > 0 do name(f.index) else name(fresh(\"index\"))"), SL(" var src = f.source"), SL(" switch short in src {"), SL(" case Unary: {"), SL(" if short.op == \"..<\" or short.op == \"..=\" {"), SL(" src = Binary("), SL(" op: short.op,"), SL(" left: literal(0),"), SL(" right: short.operand,"), SL(" site: short.site,"), SL(" )"), SL(" }"), SL(" }"), SL(" default:"), SL(" }"), SL(" switch src {"), SL(" case Binary: {"), SL(" const open_low = src.op == \"<..<\" or src.op == \"<..=\""), SL(" const open_high = src.op == \"..<\" or src.op == \"<..<\""), SL(" if src.op == \"..<\" or src.op == \"..=\" or open_low {"), SL(" const lo = src.left"), SL(" const hi = src.right"), SL(" const top = literal(if open_high do 1 else 0)"), SL(" const keep_low = literal(not open_low)"), SL(" const both_closed = literal(not open_low and not open_high)"), SL(" return code {"), SL(" const low = $lo"), SL(" const high = $hi"), SL(" if low < high or (low == high and $both_closed) {"), SL(" var step = high - $top"), SL(" var more = true"), SL(" label $loop = while more and (step > low or (step == low and $keep_low)) {"), SL(" const $x = step"), SL(" defer {"), SL(" if step > low do step -= 1 else more = false"), SL(" }"), SL(" $body"), SL(" }"), SL(" }"), SL(" }"), SL(" }"), SL(" }"), SL(" default:"), SL(" }"), SL(" const items = f.source"), SL(" # a place is read at each step, as a forward loop reads it: an element"), SL(" # removed meanwhile is an index error, not a stale copy"), SL(" if plain_place(items) do return code {"), SL(" const count = len($items)"), SL(" label $loop = for step in 0.. Bool {"), SL(" switch e {"), SL(" case Name: return true"), SL(" case Field: return plain_place(e.base)"), SL(" case Literal: return true"), SL(" case Deref: return plain_place(e.operand)"), SL(" case Index: {"), SL(" for i in e.indices do if not plain_place(i) do return false"), SL(" return plain_place(e.base)"), SL(" }"), SL(" default: return false"), SL(" }"), SL("}"), SL(""), SL("macro fn reversed(c: Code) -> Code {"), SL(" error(c, \"reversed requires a complete for loop\")"), SL(" return code {}"), SL("}"), SL(""), SL("# unreachable() marks code that must never run."), SL("macro fn unreachable() -> Code do code(fail(\"reached unreachable code\"))"), SL(""), SL("# In compile-time code, assert is an ordinary check reported at the call."), SL("macro fn assert(c: Bool) {"), SL(" if not c do error(here(), \"assertion failed\")"), SL("}"), SL(""), SL("macro fn assert(c: Bool, message: String) {"), SL(" if not c do error(here(), \"assertion failed: \", message)"), SL("}"), SL(""), SL("# A compared value in an assert message: text quoted, as code writes it."), SL("private fn assert_shown(s: String) -> String {"), SL(" var out = \"\\\"\""), SL(" for c in s {"), SL(" if c == '\"' or c == '\\\\' {"), SL(" add(&out, '\\\\', c)"), SL(" } else if c == '\\n' {"), SL(" add(&out, \"\\\\n\")"), SL(" } else {"), SL(" add(&out, c)"), SL(" }"), SL(" }"), SL(" add(&out, '\"')"), SL(" return out"), SL("}"), SL(""), SL("private fn assert_shown(a: Any) -> String do str(a)"), SL(""), SL("private macro fn assert_code(c: Expr, note: String) -> Code {"), SL(" const text = literal(str(\"assertion failed: \", c, note))"), SL(" switch c {"), SL(" case Binary: {"), SL(" const ops = [\"==\", \"!=\", \"<\", \"<=\", \">\", \">=\"]"), SL(" if not (c.op in ops) do return code {"), SL(" if not $c do fail($text)"), SL(" }"), SL(" # each side in a variable of its own, so the message shows its value;"), SL(" # a side that takes its type from the other (nil, [], .green, 1)"), SL(" # stays in the comparison, which then types it as written"), SL(" const l = c.left"), SL(" const r = c.right"), SL(" const lc = from_context(l)"), SL(" const rc = from_context(r)"), SL(" if lc and rc do return code {"), SL(" if not $c do fail($text)"), SL(" }"), SL(" const ln = name(fresh(\"lhs\"))"), SL(" const rn = name(fresh(\"rhs\"))"), SL(" const cmp = Binary("), SL(" op: c.op,"), SL(" left: if lc do l else ln,"), SL(" right: if rc do r else rn,"), SL(" )"), SL(" const ls = if lc do literal(str(l)) else code(assert_shown($ln))"), SL(" const rs = if rc do literal(str(r)) else code(assert_shown($rn))"), SL(" const op = literal(str(\" \", c.op, \" \"))"), SL(" const shown = code(str($text, \" (\", $ls, $op, $rs, \")\"))"), SL(" if lc do return code {"), SL(" const $rn = $r"), SL(" if not $cmp do fail($shown)"), SL(" }"), SL(" if rc do return code {"), SL(" const $ln = $l"), SL(" if not $cmp do fail($shown)"), SL(" }"), SL(" return code {"), SL(" const $ln = $l"), SL(" const $rn = $r"), SL(" if not $cmp do fail($shown)"), SL(" }"), SL(" }"), SL(" default:"), SL(" }"), SL(" return code {"), SL(" if not $c do fail($text)"), SL(" }"), SL("}"), SL(""), SL("# Code that takes its type from the other side of a comparison: nil, a list"), SL("# literal, a variant or anonymous struct, a number."), SL("private fn from_context(e: Expr) -> Bool {"), SL(" const t = str(e)"), SL(" if len(t) == 0 do return false"), SL(" const f = t[0]"), SL(" const number = f >= '0' and f <= '9' or f == '-' and len("), SL(" t,"), SL(" ) > 1 and t[1] >= '0' and t[1] <= '9'"), SL(" return t == \"nil\" or f == '[' or f == '.' or number"), SL("}"), }; // std:build, the prelude's building and fuzzing library: loaded, like // std:prelude, the first time a name of its own is looked up. static Str std_build_lines[] = { SL("# std:build: building and fuzzing, a part of the prelude loaded when used."), SL(""), SL("# What macro fn build(b: Build) knows: the C file in .star99/NAME/, every"), SL("# .star99 file of the program, the program's default name, and every string"), SL("# literal in it."), SL("struct Build ("), SL(" c_file: String,"), SL(" sources: List(String),"), SL(" name: String,"), SL(" strings: List(String),"), SL(")"), SL(""), SL("# How a program is compiled. build() can change any part and pass the"), SL("# result to command(). As in a build without build(), STAR99_CC replaces the"), SL("# compiler and STAR99_CFLAGS adds flags."), SL("struct Cc ("), SL(" compiler = \"cc\","), SL(" flags = \"-std=c99 -O2 -g -Wall -Wextra -Wpedantic\","), SL(" extra: String,"), SL(" libs = \"-lm\","), SL(")"), SL(""), SL("fn command(cc: Cc, b: Build) -> String {"), SL(" const env_cc = getenv(\"STAR99_CC\")"), SL(" const compiler = if len(env_cc) > 0 do env_cc else cc.compiler"), SL(" const extra = str(cc.extra, \" \", getenv(\"STAR99_CFLAGS\"))"), SL(" return str("), SL(" compiler,"), SL(" \" \","), SL(" cc.flags,"), SL(" \" \","), SL(" extra,"), SL(" \" \","), SL(" shell_quote(b.c_file),"), SL(" \" -o \","), SL(" shell_quote(b.name),"), SL(" \" \","), SL(" cc.libs,"), SL(" )"), SL("}"), SL(""), SL("# Fuzzing. fuzz(Fuzz(), b) in a build fuzzes the program with AFL++ and"), SL("# returns what it found; each step is a function of its own. A target is"), SL("# main, which runs once per input and reads it with read_file(\"-\"), or any"), SL("# function of the main module: its arguments are built from the input by"), SL("# builders the compiler derives for their types (arbitrary(d: Draw) -> T"), SL("# replaces one, valid(x: T) -> Bool filters them and checks results). A"), SL("# single List(U8) parameter gets the input bytes as they are. \"*\" fuzzes"), SL("# every function in turn, main included. Star99 checks, asserts and fail()"), SL("# are crashes."), SL("struct Fuzz ("), SL(" target = \"main\", # \"main\", a function name, or \"*\" for all of them"), SL(" seconds = 600, # for each target"), SL(" plateau = 300, # stop a target after this many seconds without new paths"), SL(" instances: Int, # fuzzers in parallel; 0 means half the free cores"), SL(" dir = \"fuzz\", # binaries, and per target: seeds, campaign and crashes"), SL(" tokens: List(String), # dictionary entries besides the program's own"), SL(" skip: List(String), # functions that \"*\" leaves out"), SL(" fail_on_crash: Bool, # a crash fails the build, for continuous integration"), SL(")"), SL(""), SL("# call: the failing call, written in Star99, as the target saw it."), SL("struct FuzzCrash (target, input, stack, call, reproduce: String)"), SL(""), SL("# errors: fuzzers that did not start or stopped early; a report with"), SL("# errors is not a clean result."), SL("struct FuzzReport ("), SL(" crashes: List(FuzzCrash),"), SL(" paths: Int,"), SL(" errors: List(String),"), SL(" targets: List(String),"), SL(")"), SL(""), SL("# Text as one word for the shell, in single quotes."), SL("fn shell_quote(text: String) -> String {"), SL(" var out = \"'\""), SL(" for c in text {"), SL(" if c == '\\'' do add(&out, \"'\\\\''\") else add(&out, c)"), SL(" }"), SL(" add(&out, \"'\")"), SL(" return out"), SL("}"), SL(""), SL("# Lines of text, without their newlines."), SL("fn lines(text: String) -> List(String) {"), SL(" var out: List(String)"), SL(" var line: String"), SL(" for c in text {"), SL(" if c == '\\n' {"), SL(" add(&out, line)"), SL(" line = \"\""), SL(" } else {"), SL(" add(&line, c)"), SL(" }"), SL(" }"), SL(" if len(line) > 0 do add(&out, line)"), SL(" return out"), SL("}"), SL(""), SL("# AFL++ dictionary syntax for one token."), SL("fn dict_entry(token: String) -> String {"), SL(" var out = \"\\\"\""), SL(" for c in token {"), SL(" if c < ' ' or c > '~' or c == '\"' or c == '\\\\' {"), SL(" const hex = \"0123456789abcdef\""), SL(" add(&out, \"\\\\x\", hex[Int(c) / 16], hex[Int(c) % 16])"), SL(" } else {"), SL(" add(&out, c)"), SL(" }"), SL(" }"), SL(" add(&out, \"\\\"\")"), SL(" return out"), SL("}"), SL(""), SL("# The number at the end of some output."), SL("fn last_number(text: String) -> Int {"), SL(" var n = 0"), SL(" for c in text {"), SL(" if c >= '0' and c <= '9' {"), SL(" n = n * 10 + Int(c) - 48"), SL(" } else if c != '\\n' {"), SL(" n = 0"), SL(" }"), SL(" }"), SL(" return n"), SL("}"), SL(""), SL("# The instrumented binaries (plain, asan, cmplog) with every target in"), SL("# them, and the dictionary."), SL("fn fuzz_build(f: Fuzz, b: Build) -> Bool {"), SL(" _ = run(str(\"mkdir -p \", shell_quote(f.dir)))"), SL(" const cc = if run("), SL(" \"command -v afl-clang-lto > /dev/null\","), SL(" ) == 0 do \"afl-clang-lto\" else \"afl-clang-fast\""), SL(" const flags = \"-std=c99 -g -O2 -w -DSTAR99_FUZZ \""), SL(" const binaries = ["), SL(" str("), SL(" cc,"), SL(" \" \","), SL(" flags,"), SL(" shell_quote(b.c_file),"), SL(" \" -o \","), SL(" shell_quote(f.dir),"), SL(" \"/plain -lm\","), SL(" ),"), SL(" str("), SL(" cc,"), SL(" \" \","), SL(" flags,"), SL(" \"-fsanitize=address,undefined -fno-sanitize-recover=all \","), SL(" shell_quote(b.c_file),"), SL(" \" -o \","), SL(" shell_quote(f.dir),"), SL(" \"/asan -lm\","), SL(" ),"), SL(" str("), SL(" \"AFL_LLVM_CMPLOG=1 \","), SL(" cc,"), SL(" \" \","), SL(" flags,"), SL(" shell_quote(b.c_file),"), SL(" \" -o \","), SL(" shell_quote(f.dir),"), SL(" \"/cmplog -lm\","), SL(" ),"), SL(" ]"), SL(" for command in binaries {"), SL(" if run(str(command, \" > /dev/null 2>&1\")) != 0 do return false"), SL(" }"), SL(" var dict: String"), SL(" for t in b.strings do add(&dict, dict_entry(t), \"\\n\")"), SL(" for t in f.tokens do add(&dict, dict_entry(t), \"\\n\")"), SL(" write_file(str(f.dir, \"/dict\"), dict)"), SL(" return true"), SL("}"), SL(""), SL("# The targets f names: one, or with \"*\" every function the program has as"), SL("# a target, less f.skip."), SL("fn fuzz_targets(f: Fuzz) -> List(String) {"), SL(" if f.target != \"*\" do return [f.target]"), SL(" var out: List(String)"), SL(" for t in lines("), SL(" shell(str(\"STAR99_TARGET=list \", shell_quote(f.dir), \"/plain\")),"), SL(" ) {"), SL(" if t not in f.skip do add(&out, t)"), SL(" }"), SL(" return out"), SL("}"), SL(""), SL("# Runs every seed of a target once. Seeds that crash are crashes already:"), SL("# they move to its out/seeds/crashes, where fuzz_crashes looks, since AFL++"), SL("# will not start on them. With no seed, a few small inputs and the words of"), SL("# the program are tried. Returns how many seeds are left."), SL("fn fuzz_seeds(f: Fuzz, b: Build, target: String) -> Int {"), SL(" const dir = str(f.dir, \"/\", target)"), SL(" _ = run("), SL(" str("), SL(" \"mkdir -p \","), SL(" shell_quote(dir),"), SL(" \"/seeds \","), SL(" shell_quote(dir),"), SL(" \"/out/seeds/crashes \","), SL(" shell_quote(f.dir),"), SL(" \"/crashes\","), SL(" ),"), SL(" )"), SL(" if len(shell(str(\"ls \", dir, \"/seeds\"))) == 0 {"), SL(" var seeds = [\"\", \"a\", \"0\", \"a=b\", \"\\x01\\x01\\x01\\x01\"]"), SL(" for t in b.strings do add(&seeds, t)"), SL(" for i, t in seeds do write_file(str(dir, \"/seeds/s\", i), t)"), SL(" }"), SL(" const script = str("), SL(" \"cd \","), SL(" dir,"), SL(" \" && n=0; for s in seeds/*; do [ -e \\\"$s\\\" ] || continue; \","), SL(" \"if STAR99_TARGET=\","), SL(" target,"), SL(" \" ASAN_OPTIONS=detect_leaks=0 timeout 5 ../asan < \\\"$s\\\" > /dev/null 2>&1; then n=$((n+1)); \","), SL(" \"else mv \\\"$s\\\" out/seeds/crashes/id:seed-$(basename \\\"$s\\\"); fi; done; echo $n\","), SL(" )"), SL(" return last_number(shell(script))"), SL("}"), SL(""), SL("# Fuzzes one target until its seconds are up or it stops finding paths: a"), SL("# main instance with CMPLOG, a sanitizer instance, and plain ones with"), SL("# varied schedules. Returns the number of paths found."), SL("fn fuzz_start(f: Fuzz, target: String) -> Int {"), SL(" const script = str("), SL(" \"cd \","), SL(" shell_quote(f.dir),"), SL(" \"/\","), SL(" target,"), SL(" \" && n=\","), SL(" f.instances,"), SL(" \"; free=$(( $(nproc) - $(pgrep -c -x afl-fuzz) )); \","), SL(" \"[ $n -gt 0 ] || n=$(( free / 2 )); [ $n -gt 0 ] || n=1; \","), SL(" \"export STAR99_TARGET=\","), SL(" target,"), SL(" \" AFL_SKIP_CPUFREQ=1 AFL_NO_UI=1 AFL_I_DONT_CARE_ABOUT_MISSING_CRASHES=1 AFL_NO_AFFINITY=1 \","), SL(" \"ASAN_OPTIONS=abort_on_error=1:symbolize=0:detect_leaks=0; \","), SL(" \"D=\\\"-i seeds -o out -x ../dict -t 2000 -m none -V \","), SL(" f.seconds,"), SL(" \"\\\"; \","), SL(" \"AFL_TMPDIR=$(mktemp -d) afl-fuzz -M main $D -c ../cmplog -l 2AT -- ../plain > main.log 2>&1 & \","), SL(" \"[ $n -gt 1 ] && AFL_TMPDIR=$(mktemp -d) afl-fuzz -S asan $D -- ../asan > asan.log 2>&1 & \","), SL(" \"i=2; for p in fast explore rare exploit coe; do [ $i -lt $n ] || break; \","), SL(" \"AFL_TMPDIR=$(mktemp -d) afl-fuzz -S s$i -p $p $D -- ../plain > s$i.log 2>&1 & i=$((i+1)); done; \","), SL(" \"start=$(date +%s); end=$((start + \","), SL(" f.seconds,"), SL(" \")); \","), SL(" \"while [ $(date +%s) -lt $end ]; do sleep 5; \","), SL(" \"last=$(cat out/*/fuzzer_stats 2>/dev/null | awk '/^last_find/ {if ($3 > m) m = $3} END {print m + 0}'); \","), SL(" \"[ $last -gt 0 ] || last=$start; [ $(( $(date +%s) - last )) -gt \","), SL(" f.plateau,"), SL(" \" ] && break; done; \","), SL(" \"kill $(jobs -p) 2>/dev/null; wait; \","), SL(" \"cat out/*/fuzzer_stats 2>/dev/null | awk '/^corpus_count/ {s += $3} END {print s + 0}'\","), SL(" )"), SL(" return last_number(shell(script))"), SL("}"), SL(""), SL("# Fuzzers of a target that failed to start or stopped early, with the reason"), SL("# AFL++ gave."), SL("fn fuzz_errors(f: Fuzz, target: String) -> List(String) {"), SL(" const script = str("), SL(" \"cd \","), SL(" shell_quote(f.dir),"), SL(" \"/\","), SL(" target,"), SL(" \" && for l in *.log; do [ -e \\\"$l\\\" ] || continue; n=${l%.log}; \","), SL(" \"if grep -q 'PROGRAM ABORT' \\\"$l\\\"; then echo \\\"\","), SL(" target,"), SL(" \" $n: $(grep -A1 'PROGRAM ABORT' \\\"$l\\\" | head -1 | \","), SL(" \"sed 's/\\\\x1b\\\\[[0-9;]*m//g; s/.*PROGRAM ABORT : *//')\\\"; \","), SL(" \"elif [ ! -e out/$n/fuzzer_stats ]; then echo \\\"\","), SL(" target,"), SL(" \" $n: did not start\\\"; fi; done\","), SL(" )"), SL(" return lines(shell(script))"), SL("}"), SL(""), SL("# The crashes of a target, one per distinct stack of Star99 functions"), SL("# (innermost first), minimized into DIR/crashes. Minimizing works on the"), SL("# choices, so a smaller input is a smaller value."), SL("fn fuzz_crashes(f: Fuzz, target: String) -> List(FuzzCrash) {"), SL(" const script = str("), SL(" \"cd \","), SL(" shell_quote(f.dir),"), SL(" \" && export STAR99_TARGET=\","), SL(" target,"), SL(" \" ASAN_OPTIONS=abort_on_error=1:detect_leaks=0:handle_abort=1; \","), SL(" \"for c in \","), SL(" target,"), SL(" \"/out/*/crashes/id:*; do [ -e \\\"$c\\\" ] || continue; \","), SL(" \"k=$(./asan < \\\"$c\\\" 2>&1 | grep -oE ' in f[0-9]+_[A-Za-z0-9_]+' | head -3 | sed 's/ in f[0-9]*_//' | tr '\\\\n' ' '); \","), SL(" \"h=\","), SL(" target,"), SL(" \"-$(echo \\\"$k\\\" | cksum | cut -d' ' -f1); [ -e crashes/$h ] && continue; \","), SL(" \"afl-tmin -i \\\"$c\\\" -o crashes/$h -- ./asan > /dev/null 2>&1 || cp \\\"$c\\\" crashes/$h; \","), SL(" \"echo \\\"$h $k\\\"; done\","), SL(" )"), SL(" var out: List(FuzzCrash)"), SL(" for line in lines(shell(script)) {"), SL(" var id: String"), SL(" var i = 0"), SL(" while i < len(line) and line[i] != ' ' {"), SL(" add(&id, line[i])"), SL(" i += 1"), SL(" }"), SL(" var stack: String"), SL(" for j in i + 1..&1\"))) {"), SL(" if len("), SL(" call,"), SL(" ) == 0 and \"call: \" in l do for k in 6.. Int {"), SL(" var failing = 0"), SL(" for input in lines(shell(str(\"ls \", shell_quote(f.dir), \"/crashes\"))) {"), SL(" var target: String"), SL(" var i = 0"), SL(" while i < len(input) and input[i] != '-' {"), SL(" add(&target, input[i])"), SL(" i += 1"), SL(" }"), SL(" const command = str("), SL(" \"STAR99_TARGET=\","), SL(" target,"), SL(" \" ASAN_OPTIONS=detect_leaks=0 \","), SL(" shell_quote(f.dir),"), SL(" \"/asan < \","), SL(" shell_quote(str(f.dir, \"/crashes/\", input)),"), SL(" )"), SL(" if run(str(command, \" > /dev/null 2>&1\")) != 0 do failing += 1"), SL(" }"), SL(" return failing"), SL("}"), SL(""), SL("# All of it: build, then for each target check the seeds, fuzz, triage."), SL("# Prints a summary at compile time. When no fuzzer could run, or when"), SL("# fail_on_crash and something crashed, the build fails."), SL("fn fuzz(f: Fuzz, b: Build) -> FuzzReport {"), SL(" if not fuzz_build("), SL(" f,"), SL(" b,"), SL(" ) do fail(\"fuzz: building the AFL++ binaries failed; is AFL++ installed?\")"), SL(" var report: FuzzReport"), SL(" report.targets = fuzz_targets(f)"), SL(" for target in report.targets {"), SL(" if fuzz_seeds(f, b, target) == 0 {"), SL(" add("), SL(" &report.errors,"), SL(" str("), SL(" target,"), SL(" \": every seed crashes it, so AFL++ cannot start; fix those first\","), SL(" ),"), SL(" )"), SL(" } else {"), SL(" report.paths += fuzz_start(f, target)"), SL(" for e in fuzz_errors(f, target) do add(&report.errors, e)"), SL(" }"), SL(" for c in fuzz_crashes(f, target) do add(&report.crashes, c)"), SL(" }"), SL(" println("), SL(" \"fuzz: \","), SL(" len(report.targets),"), SL(" \" targets, \","), SL(" report.paths,"), SL(" \" paths, \","), SL(" len(report.crashes),"), SL(" \" distinct crashes\","), SL(" )"), SL(" for c in report.crashes do println("), SL(" \" \","), SL(" c.target,"), SL(" \": \","), SL(" c.call,"), SL(" \" # \","), SL(" c.stack,"), SL(" \"\\n \","), SL(" c.reproduce,"), SL(" )"), SL(" for e in report.errors do println(\" fuzz error: \", e)"), SL(" if report.paths == 0 and len("), SL(" report.crashes,"), SL(" ) == 0 do fail(str(\"fuzz: no fuzzer ran (see \", f.dir, \"/*/*.log)\"))"), SL(" if f.fail_on_crash and len("), SL(" report.crashes,"), SL(" ) > 0 do fail(str(\"fuzz: \", len(report.crashes), \" crashes\"))"), SL(" return report"), SL("}"), }; // The runtime of generated programs, written into every C file. It is kept // as lines because C99 compilers may reject very long string literals. static Str runtime_lines[] = { SL("/* Star99 runtime: included at the top of every generated program. */"), SL("#define _DEFAULT_SOURCE /* madvise flags on Linux */"), SL("#include "), SL("#include "), SL("#include "), SL("#include "), SL("#include "), SL("#include "), SL("#include "), SL("#if defined(__unix__) || defined(__APPLE__)"), SL("#include "), SL("#define S99_HAVE_MMAP 1"), SL("#endif"), SL("/* Under ASan, freed blocks waiting for reuse are poisoned. */"), SL("#if defined(__SANITIZE_ADDRESS__)"), SL("#define S99_ASAN 1"), SL("#elif defined(__has_feature)"), SL("#if __has_feature(address_sanitizer)"), SL("#define S99_ASAN 1"), SL("#endif"), SL("#endif"), SL("#ifdef S99_ASAN"), SL("#include "), SL("#define S99_POISON(p, n) ASAN_POISON_MEMORY_REGION((p), (n))"), SL("#define S99_UNPOISON(p, n) ASAN_UNPOISON_MEMORY_REGION((p), (n))"), SL("#endif"), SL("#ifndef S99_POISON"), SL("#define S99_POISON(p, n) ((void)(p), (void)(n))"), SL("#define S99_UNPOISON(p, n) ((void)(p), (void)(n))"), SL("#endif"), SL("#if defined(__GNUC__) || defined(__clang__)"), SL("/* x == x is fine in Star99 source; C would warn about the copy of it */"), SL("#pragma GCC diagnostic ignored \"-Wtautological-compare\""), SL("#define S99_NORETURN __attribute__((noreturn))"), SL("#define S99_UNUSED __attribute__((unused))"), SL("#define S99_LIKELY(x) __builtin_expect(!!(x), 1)"), SL("#define S99_UNLIKELY(x) __builtin_expect(!!(x), 0)"), SL("#define S99_INLINE inline __attribute__((always_inline))"), SL("#else"), SL("#define S99_NORETURN"), SL("#define S99_UNUSED"), SL("#define S99_LIKELY(x) (x)"), SL("#define S99_UNLIKELY(x) (x)"), SL("#define S99_INLINE inline"), SL("#endif"), SL(""), SL("static S99_UNUSED int s99_argc = 0;"), SL("static S99_UNUSED char **s99_argv = 0;"), SL(""), SL("/* Every failed check ends here: bounds, removed items, missing keys,"), SL(" overflow, assert and fail. */"), SL("static S99_UNUSED FILE *s99_meta_fails = 0; /* in a metaprogram: failures go to the compiler */"), SL(""), SL("static S99_NORETURN S99_UNUSED void"), SL("s99_fail_n(const char *where, const void *msg, int64_t len)"), SL("{"), SL(" if (s99_meta_fails) {"), SL(" fprintf(s99_meta_fails, \"Ds%d:%ss%lld:%.*s\", (int)strlen(where), where, (long long)len, (int)len, (const char *)msg);"), SL(" fflush(s99_meta_fails);"), SL(" exit(1);"), SL(" }"), SL(" fflush(stdout);"), SL(" fprintf(stderr, \"%s: error: %.*s\\n\", where, (int)len, (const char *)msg);"), SL(" fflush(stderr);"), SL(" abort(); /* a signal, so sanitizers show the stack and fuzzers count it */"), SL("}"), SL(""), SL("static S99_NORETURN S99_UNUSED void"), SL("s99_fail(const char *where, const char *msg)"), SL("{"), SL(" s99_fail_n(where, msg, (int64_t)strlen(msg));"), SL("}"), SL(""), SL("static S99_NORETURN S99_UNUSED void"), SL("s99_fail_index(const char *where, int64_t i, int64_t len)"), SL("{"), SL(" char msg[96]; /* snprintf writes it; zeroed, every bounds check costs more */"), SL(" snprintf(msg, sizeof(msg), \"index %lld is out of bounds for length %lld\", (long long)i, (long long)len);"), SL(" s99_fail(where, msg);"), SL("}"), SL(""), SL("static S99_UNUSED void *"), SL("s99_alloc(size_t n)"), SL("{"), SL(" void *p = calloc(1, n ? n : 1);"), SL(" if (!p) s99_fail(\"star99\", \"out of memory\");"), SL(" return p;"), SL("}"), SL(""), SL("static S99_UNUSED void *"), SL("s99_realloc(void *p, size_t n)"), SL("{"), SL(" void *r = realloc(p, n ? n : 1);"), SL(" if (!r) s99_fail(\"star99\", \"out of memory\");"), SL(" return r;"), SL("}"), SL(""), SL("/* The bytes of n items of size bytes each: a size that does not fit is"), SL(" out of memory, never a wrapped number. */"), SL("static S99_UNUSED size_t"), SL("s99_nbytes(int64_t n, int64_t size)"), SL("{"), SL(" if (n < 0 || size < 0 || (size && (uint64_t)n > (uint64_t)PTRDIFF_MAX / (uint64_t)size))"), SL(" s99_fail(\"star99\", \"out of memory\");"), SL(" return (size_t)n * (size_t)size;"), SL("}"), SL(""), SL("/* A capacity of at least n, doubling from cap without overflowing. */"), SL("static S99_UNUSED int64_t"), SL("s99_grown(int64_t cap, int64_t n)"), SL("{"), SL(" if (cap < 1) cap = 1;"), SL(" while (cap < n) cap = cap > INT64_MAX / 2 ? n : cap * 2;"), SL(" return cap;"), SL("}"), SL(""), SL("/* Every reference target carries a generation from one counter for the whole"), SL(" program, so a reference to something removed never matches again. */"), SL("static S99_UNUSED uint64_t s99_gens = 1;"), SL(""), SL("/* Blocks that references can point into are never returned to malloc: a"), SL(" stale reference must still be able to read the generation. Freed blocks"), SL(" go to free lists by size class, poisoned except for the gen and the link. */"), SL("typedef struct s99_free {"), SL(" uint64_t gen;"), SL(" struct s99_free *next;"), SL("} s99_free;"), SL(""), SL("/* The generation starts every block a reference can reach (a box, a list"), SL(" header, a map entry, a free block). It is read and written as bytes:"), SL(" a block is freed as one type and checked as another, and under strict"), SL(" aliasing a C compiler may assume those accesses do not meet. */"), SL("static S99_UNUSED S99_INLINE uint64_t"), SL("s99_gen_get(const void *p)"), SL("{"), SL(" uint64_t g = 0;"), SL(" memcpy(&g, p, sizeof(g));"), SL(" return g;"), SL("}"), SL(""), SL("static S99_UNUSED S99_INLINE void"), SL("s99_gen_set(void *p, uint64_t g)"), SL("{"), SL(" memcpy(p, &g, sizeof(g));"), SL("}"), SL(""), SL("static S99_UNUSED s99_free *s99_freelists[128]; /* static, so zero; with = {0}, tcc would store the zeros in the executable */"), SL(""), SL("/* Size classes 16, 32, 48, 64, 96, 128, 192, 256, ...: a block wastes at"), SL(" most a third of itself. */"), SL("static S99_UNUSED int"), SL("s99_class(size_t n)"), SL("{"), SL(" if (n <= 16) return 0;"), SL(" if (n <= 32) return 1;"), SL(" int b = 0; /* 2^b < n <= 2^(b+1) */"), SL("#if defined(__GNUC__) || defined(__clang__)"), SL(" b = 63 - __builtin_clzll((unsigned long long)(n - 1));"), SL("#else"), SL(" while (((size_t)2 << b) < n) b++;"), SL("#endif"), SL(" return 2 * (b - 5) + 2 + (n > ((size_t)3 << (b - 1)));"), SL("}"), SL(""), SL("static S99_UNUSED size_t"), SL("s99_class_size(int c)"), SL("{"), SL(" if (c < 2) return (size_t)16 << c;"), SL(" int b = (c - 2) / 2 + 5;"), SL(" return c % 2 ? (size_t)2 << b : (size_t)3 << (b - 1);"), SL("}"), SL(""), SL("/* Areas. A loop body the compiler proves keeps nothing it allocates (no"), SL(" value of it reaches anything outside the body) allocates by bumping a"), SL(" pointer through one large reserved region, frees nothing one by one, and"), SL(" gives everything back at the end of each step by moving the pointer back."), SL(" Freeing a block there only clears its generation, so a reference to it"), SL(" still fails its check during the step. */"), SL("static S99_UNUSED char *s99_area_base = 0;"), SL("static S99_UNUSED char *s99_area_top = 0;"), SL("static S99_UNUSED char *s99_area_limit = 0;"), SL("#define S99_AREA_LEVELS 64"), SL("#define S99_AREA_CLASSES 12"), SL("static S99_UNUSED int s99_area_depth = 0; /* steps open */"), SL("/* per open step: blocks freed during it, reused within it */"), SL("static S99_UNUSED struct {"), SL(" s99_free *free[S99_AREA_CLASSES];"), SL(" bool used; /* whether free holds any: cleared when the step ends */"), SL("} s99_areas[S99_AREA_LEVELS + 1]; /* static, so zero; with = {0}, tcc would store the zeros in the executable */"), SL(""), SL("typedef struct {"), SL(" char *top;"), SL(" int opened;"), SL("} s99_area_mark;"), SL(""), SL("static S99_UNUSED void"), SL("s99_area_init(void)"), SL("{"), SL("#ifdef S99_HAVE_MMAP"), SL(" size_t n = (size_t)1 << 36; /* reserved, not used: pages come as touched */"), SL(" void *p = mmap(0, n, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0);"), SL(" if (p == MAP_FAILED) return;"), SL("#ifdef MADV_DONTDUMP"), SL(" madvise(p, n, MADV_DONTDUMP); /* a core file would hold the whole reservation */"), SL("#endif"), SL(" s99_area_base = s99_area_top = (char *)p;"), SL(" s99_area_limit = s99_area_base + n;"), SL("#endif"), SL("}"), SL(""), SL("#define S99_IN_AREA() (s99_area_depth > 0)"), SL(""), SL("static S99_UNUSED S99_INLINE void"), SL("s99_area_enter(s99_area_mark *m)"), SL("{"), SL(" if (!s99_area_base) s99_area_init();"), SL(" m->opened = s99_area_base && s99_area_depth < S99_AREA_LEVELS;"), SL(" m->top = s99_area_top;"), SL(" if (m->opened) s99_area_depth++; /* too deep: the step allocates where the one around it does */"), SL("}"), SL(""), SL("static S99_UNUSED S99_INLINE void"), SL("s99_area_leave(s99_area_mark *m)"), SL("{"), SL(" if (!m->opened) return;"), SL(" s99_area_top = m->top;"), SL(" int l = s99_area_depth--;"), SL(" if (s99_areas[l].used) {"), SL(" memset(s99_areas[l].free, 0, sizeof(s99_areas[l].free));"), SL(" s99_areas[l].used = 0;"), SL(" }"), SL("}"), SL(""), SL("static S99_UNUSED S99_INLINE bool"), SL("s99_in_area(const void *p)"), SL("{"), SL(" return (const char *)p >= s99_area_base && (const char *)p < s99_area_limit;"), SL("}"), SL(""), SL("static S99_UNUSED void *"), SL("s99_area_bump(size_t n)"), SL("{"), SL(" char *p = s99_area_top;"), SL(" n = (n + 15) & ~(size_t)15;"), SL(" if ((size_t)(s99_area_limit - p) < n) s99_fail(\"star99\", \"out of memory\");"), SL(" s99_area_top = p + n;"), SL(" return p;"), SL("}"), SL(""), SL("/* Value storage: element arrays, map tables. In an area it comes from the"), SL(" area, with its size in front so it can grow. */"), SL("static S99_UNUSED void *"), SL("s99_xalloc(size_t n)"), SL("{"), SL(" if (!S99_IN_AREA()) return s99_alloc(n);"), SL(" size_t *p = (size_t *)s99_area_bump(n + 16);"), SL(" p[0] = n;"), SL(" memset(p + 2, 0, n);"), SL(" return p + 2;"), SL("}"), SL(""), SL("static S99_UNUSED void *"), SL("s99_xrealloc(void *p, size_t n)"), SL("{"), SL(" if (p ? !s99_in_area(p) : !S99_IN_AREA()) return s99_realloc(p, n);"), SL(" size_t old = p ? ((size_t *)p)[-2] : 0;"), SL(" if (p && (char *)p + ((old + 15) & ~(size_t)15) == s99_area_top && n >= old) {"), SL(" /* the newest allocation grows in place */"), SL(" size_t more = ((n + 15) & ~(size_t)15) - ((old + 15) & ~(size_t)15);"), SL(" if ((size_t)(s99_area_limit - s99_area_top) < more) s99_fail(\"star99\", \"out of memory\");"), SL(" s99_area_top += more;"), SL(" ((size_t *)p)[-2] = n;"), SL(" return p;"), SL(" }"), SL(" size_t *q = (size_t *)s99_area_bump(n + 16);"), SL(" q[0] = n;"), SL(" if (p) memcpy(q + 2, p, old < n ? old : n);"), SL(" return q + 2;"), SL("}"), SL(""), SL("static S99_UNUSED void"), SL("s99_xfree(void *p)"), SL("{"), SL(" if (p && s99_in_area(p)) return;"), SL(" free(p);"), SL("}"), SL(""), SL("/* A block in the area: a freed one of its class, or a new one. */"), SL("static S99_UNUSED void *"), SL("s99_area_block(size_t n)"), SL("{"), SL(" int c = s99_class(n);"), SL(" if (c >= S99_AREA_CLASSES) return s99_area_bump(n);"), SL(" s99_free *f = s99_areas[s99_area_depth].free[c];"), SL(" if (!f) return s99_area_bump(s99_class_size(c));"), SL(" s99_areas[s99_area_depth].free[c] = f->next;"), SL(" return f;"), SL("}"), SL(""), SL("/* New blocks are cut from slabs: no malloc header each, and neighbors in"), SL(" memory. Under AddressSanitizer each block is its own allocation, so leak"), SL(" checking still sees every block. */"), SL("#ifndef S99_ASAN"), SL("static S99_UNUSED struct {"), SL(" char *next, *end;"), SL("} s99_slabs[128]; /* static, so zero; with = {0}, tcc would store the zeros in the executable */"), SL(""), SL("static S99_UNUSED void *"), SL("s99_slab_block(int c, size_t size)"), SL("{"), SL(" if ((size_t)(s99_slabs[c].end - s99_slabs[c].next) < size) {"), SL(" size_t n = size < 4096 ? (size_t)65536 : size * 16;"), SL(" s99_slabs[c].next = (char *)malloc(n);"), SL(" if (!s99_slabs[c].next) s99_fail(\"star99\", \"out of memory\");"), SL(" s99_slabs[c].end = s99_slabs[c].next + n;"), SL(" }"), SL(" void *p = s99_slabs[c].next;"), SL(" s99_slabs[c].next += size;"), SL(" return p;"), SL("}"), SL("#endif"), SL(""), SL("static S99_UNUSED void *"), SL("s99_block(size_t n)"), SL("{"), SL(" if (S99_IN_AREA()) {"), SL(" void *a = s99_area_block(n);"), SL(" memset(a, 0, n);"), SL(" return a;"), SL(" }"), SL(" int c = s99_class(n);"), SL(" size_t size = s99_class_size(c);"), SL(" s99_free *f = s99_freelists[c];"), SL(" void *p = 0;"), SL(" if (f) {"), SL(" S99_UNPOISON(f, size);"), SL(" s99_freelists[c] = f->next;"), SL(" memset(f, 0, size);"), SL(" p = f;"), SL(" } else {"), SL("#ifdef S99_ASAN"), SL(" p = s99_alloc(size);"), SL("#else"), SL(" p = s99_slab_block(c, size);"), SL(" memset(p, 0, size);"), SL("#endif"), SL(" }"), SL(" S99_POISON((char *)p + n, size - n); /* the unused tail is a red zone */"), SL(" return p;"), SL("}"), SL(""), SL("/* A block whose contents the caller writes in full: not zeroed. */"), SL("static S99_UNUSED void *"), SL("s99_block_raw(size_t n)"), SL("{"), SL(" if (S99_IN_AREA()) return s99_area_block(n);"), SL(" int c = s99_class(n);"), SL(" size_t size = s99_class_size(c);"), SL(" s99_free *f = s99_freelists[c];"), SL(" void *p = 0;"), SL(" if (f) {"), SL(" S99_UNPOISON(f, size);"), SL(" s99_freelists[c] = f->next;"), SL(" p = f;"), SL(" } else {"), SL("#ifdef S99_ASAN"), SL(" p = malloc(size);"), SL(" if (!p) s99_fail(\"star99\", \"out of memory\");"), SL("#else"), SL(" p = s99_slab_block(c, size);"), SL("#endif"), SL(" }"), SL(" S99_POISON((char *)p + n, size - n);"), SL(" return p;"), SL("}"), SL(""), SL("static S99_UNUSED void"), SL("s99_unblock(void *p, size_t n)"), SL("{"), SL(" if (s99_in_area(p)) {"), SL(" /* stale references fail; small blocks are reused within the step */"), SL(" s99_free *f = (s99_free *)p;"), SL(" int c = s99_class(n);"), SL(" s99_gen_set(p, 0);"), SL(" if (c < S99_AREA_CLASSES && S99_IN_AREA() && (char *)p >= s99_area_base) {"), SL(" /* reused within the step (a block of an outer step only when it ends) */"), SL(" f->next = s99_areas[s99_area_depth].free[c];"), SL(" s99_areas[s99_area_depth].free[c] = f;"), SL(" s99_areas[s99_area_depth].used = 1;"), SL(" }"), SL(" return;"), SL(" }"), SL(" int c = s99_class(n);"), SL(" size_t size = s99_class_size(c);"), SL(" s99_free *f = (s99_free *)p;"), SL(" S99_UNPOISON(f, size);"), SL(" s99_gen_set(p, 0);"), SL(" f->next = s99_freelists[c];"), SL(" s99_freelists[c] = f;"), SL(" S99_POISON((char *)p + sizeof(s99_free), size - sizeof(s99_free)); /* leak checkers follow next */"), SL("}"), SL(""), SL("/* Storage reached by stored references lives for the rest of the program."), SL(" Cells are listed so leak checkers see them as reachable. */"), SL("static S99_UNUSED void **s99_cells = 0;"), SL("static S99_UNUSED size_t s99_ncells = 0;"), SL("static S99_UNUSED size_t s99_capcells = 0;"), SL(""), SL("static S99_UNUSED void *"), SL("s99_cell(size_t n)"), SL("{"), SL(" void *p = s99_alloc(n);"), SL(" if (s99_ncells == s99_capcells) {"), SL(" s99_capcells = s99_capcells ? s99_capcells * 2 : 64;"), SL(" s99_cells = (void **)s99_realloc(s99_cells, s99_capcells * sizeof(void *));"), SL(" }"), SL(" s99_cells[s99_ncells++] = p;"), SL(" return p;"), SL("}"), SL(""), SL("/* Counted cells: a local a closure that outlives its scope captures, and"), SL(" such a closure's environment. The scope holds one count and every"), SL(" environment that reaches it one more; the last release drops the contents"), SL(" and frees the block. */"), SL("typedef struct {"), SL(" int32_t rc, size;"), SL(" void (*drop)(void *);"), SL("} s99_rc;"), SL(""), SL("static S99_UNUSED void *"), SL("s99_rc_new(size_t size, void (*drop)(void *))"), SL("{"), SL(" s99_rc *h = (s99_rc *)s99_block(sizeof(s99_rc) + size);"), SL(" h->rc = 1;"), SL(" h->size = (int32_t)size;"), SL(" h->drop = drop;"), SL(" return h + 1;"), SL("}"), SL(""), SL("static S99_UNUSED void *"), SL("s99_rc_retain(void *p)"), SL("{"), SL(" ((s99_rc *)p - 1)->rc++;"), SL(" return p;"), SL("}"), SL(""), SL("static S99_UNUSED void"), SL("s99_rc_release(void *p)"), SL("{"), SL(" s99_rc *h = (s99_rc *)p - 1;"), SL(" if (--h->rc) return;"), SL(" if (h->drop) h->drop(p);"), SL(" s99_unblock(h, sizeof(s99_rc) + (size_t)h->size);"), SL("}"), SL(""), SL("/* Output streams: a file, or a growing string for str(). */"), SL("typedef struct {"), SL(" FILE *f;"), SL(" uint8_t *buf;"), SL(" int64_t len, cap;"), SL(" uint8_t local[32]; /* a short str() needs no allocation */"), SL("} s99_stream;"), SL(""), SL("static S99_UNUSED s99_stream s99_stdout_stream = {0};"), SL("static S99_UNUSED s99_stream s99_stderr_stream = {0};"), SL("static S99_UNUSED s99_stream *s99_out = 0;"), SL("/* stdout and stderr as Star99 values of type File */"), SL("static S99_UNUSED s99_stream *s99_stdout_file = &s99_stdout_stream;"), SL("static S99_UNUSED s99_stream *s99_stderr_file = &s99_stderr_stream;"), SL(""), SL("static S99_UNUSED s99_stream *"), SL("s99_file(s99_stream *f)"), SL("{"), SL(" return f ? f : &s99_stdout_stream; /* a zero File is stdout */"), SL("}"), SL(""), SL("static S99_UNUSED void"), SL("s99_put(s99_stream *o, const void *data, int64_t len)"), SL("{"), SL(" if (o->f) {"), SL(" fwrite(data, 1, (size_t)len, o->f);"), SL(" return;"), SL(" }"), SL(" if (o->len + len > o->cap) {"), SL(" if (!o->buf && len <= (int64_t)sizeof(o->local)) {"), SL(" o->buf = o->local;"), SL(" o->cap = (int64_t)sizeof(o->local);"), SL(" } else {"), SL(" int64_t cap = s99_grown(o->cap > 64 ? o->cap : 64, o->len + len);"), SL(" if (o->buf == o->local) {"), SL(" uint8_t *b = (uint8_t *)s99_alloc((size_t)cap);"), SL(" memcpy(b, o->local, (size_t)o->len);"), SL(" o->buf = b;"), SL(" } else {"), SL(" o->buf = (uint8_t *)s99_realloc(o->buf, (size_t)cap);"), SL(" }"), SL(" o->cap = cap;"), SL(" }"), SL(" }"), SL(" if (len) memcpy(o->buf + o->len, data, (size_t)len);"), SL(" o->len += len;"), SL("}"), SL(""), SL("static S99_UNUSED void"), SL("s99_puts(s99_stream *o, const char *s)"), SL("{"), SL(" s99_put(o, s, (int64_t)strlen(s));"), SL("}"), SL(""), SL("/* The decimal digits of v, written backward to end at p; their count. */"), SL("static S99_UNUSED int"), SL("s99_digits(char *p, int64_t v)"), SL("{"), SL(" uint64_t u = v < 0 ? -(uint64_t)v : (uint64_t)v;"), SL(" char *q = p;"), SL(" do {"), SL(" *--q = (char)('0' + u % 10);"), SL(" u /= 10;"), SL(" } while (u);"), SL(" if (v < 0) *--q = '-';"), SL(" return (int)(p - q);"), SL("}"), SL(""), SL("static S99_UNUSED void"), SL("s99_put_int(s99_stream *o, int64_t v)"), SL("{"), SL(" char tmp[24]; /* s99_digits writes what is read; zeroed, every Int printed pays */"), SL(" int n = s99_digits(tmp + sizeof(tmp), v);"), SL(" s99_put(o, tmp + sizeof(tmp) - n, n);"), SL("}"), SL(""), SL("static S99_UNUSED void"), SL("s99_put_uint(s99_stream *o, uint64_t v)"), SL("{"), SL(" char tmp[32] = {0};"), SL(" int n = snprintf(tmp, sizeof(tmp), \"%llu\", (unsigned long long)v);"), SL(" s99_put(o, tmp, n);"), SL("}"), SL(""), SL("/* The shortest text that reads back as the same double. */"), SL("static S99_UNUSED void"), SL("s99_put_float(s99_stream *o, double v)"), SL("{"), SL(" char tmp[40] = {0};"), SL(" if (isnan(v)) {"), SL(" s99_puts(o, \"nan\");"), SL(" return;"), SL(" }"), SL(" if (isinf(v)) {"), SL(" s99_puts(o, v < 0 ? \"-inf\" : \"inf\");"), SL(" return;"), SL(" }"), SL(" int n = 0;"), SL(" double a = v < 0 ? -v : v;"), SL(" bool plain = a == 0 || (a >= 1e-5 && a < 1e16); /* 1500.0, not 1.5e+03 */"), SL(" for (int prec = plain ? 0 : 1; prec <= 17 + plain * 5; prec++) {"), SL(" n = snprintf(tmp, sizeof(tmp), plain ? \"%.*f\" : \"%.*g\", prec, v);"), SL(" if (strtod(tmp, 0) == v) break;"), SL(" }"), SL(" int hasdot = 0;"), SL(" for (int i = 0; i < n; i++) {"), SL(" if (tmp[i] == '.' || tmp[i] == 'e' || tmp[i] == 'n' || tmp[i] == 'i') hasdot = 1;"), SL(" }"), SL(" s99_put(o, tmp, n);"), SL(" if (!hasdot) s99_puts(o, \".0\");"), SL("}"), SL(""), SL("/* Checked integer arithmetic: overflow and division by zero stop the program. */"), SL("#if defined(__GNUC__) || defined(__clang__)"), SL("#define S99_OVF(op, a, b, r) __builtin_##op##_overflow(a, b, r)"), SL("#endif"), SL(""), SL("static S99_UNUSED int64_t"), SL("s99_add(int64_t a, int64_t b, const char *where)"), SL("{"), SL(" int64_t r = 0;"), SL("#ifdef S99_OVF"), SL(" if (S99_OVF(add, a, b, &r)) s99_fail(where, \"integer overflow\");"), SL("#else"), SL(" if ((b > 0 && a > INT64_MAX - b) || (b < 0 && a < INT64_MIN - b)) s99_fail(where, \"integer overflow\");"), SL(" r = a + b;"), SL("#endif"), SL(" return r;"), SL("}"), SL(""), SL("static S99_UNUSED int64_t"), SL("s99_sub(int64_t a, int64_t b, const char *where)"), SL("{"), SL(" int64_t r = 0;"), SL("#ifdef S99_OVF"), SL(" if (S99_OVF(sub, a, b, &r)) s99_fail(where, \"integer overflow\");"), SL("#else"), SL(" if ((b < 0 && a > INT64_MAX + b) || (b > 0 && a < INT64_MIN + b)) s99_fail(where, \"integer overflow\");"), SL(" r = a - b;"), SL("#endif"), SL(" return r;"), SL("}"), SL(""), SL("static S99_UNUSED int64_t"), SL("s99_mul(int64_t a, int64_t b, const char *where)"), SL("{"), SL(" int64_t r = 0;"), SL("#ifdef S99_OVF"), SL(" if (S99_OVF(mul, a, b, &r)) s99_fail(where, \"integer overflow\");"), SL("#else"), SL(" if (a && b && ((a == -1 && b == INT64_MIN) || (b == -1 && a == INT64_MIN) ||"), SL(" (a > 0 ? (b > 0 ? a > INT64_MAX / b : b < INT64_MIN / a)"), SL(" : (b > 0 ? a < INT64_MIN / b : a < INT64_MAX / b))))"), SL(" s99_fail(where, \"integer overflow\");"), SL(" r = a * b;"), SL("#endif"), SL(" return r;"), SL("}"), SL(""), SL("static S99_UNUSED int64_t"), SL("s99_div(int64_t a, int64_t b, const char *where)"), SL("{"), SL(" if (!b) s99_fail(where, \"division by zero\");"), SL(" if (a == INT64_MIN && b == -1) s99_fail(where, \"integer overflow\");"), SL(" return a / b;"), SL("}"), SL(""), SL("static S99_UNUSED int64_t"), SL("s99_mod(int64_t a, int64_t b, const char *where)"), SL("{"), SL(" if (!b) s99_fail(where, \"division by zero\");"), SL(" if (b == -1) return 0;"), SL(" return a % b;"), SL("}"), SL(""), SL("/* a ** b: by squaring; a negative exponent has no integer result */"), SL("static S99_UNUSED int64_t"), SL("s99_pow(int64_t a, int64_t b, const char *where)"), SL("{"), SL(" if (b < 0) s99_fail(where, \"a negative exponent has no integer result\");"), SL(" int64_t r = 1;"), SL(" while (b) {"), SL(" if (b & 1) r = s99_mul(r, a, where);"), SL(" b >>= 1;"), SL(" if (b) a = s99_mul(a, a, where);"), SL(" }"), SL(" return r;"), SL("}"), SL(""), SL("/* Narrow integers: compute in 64 bits, then check the range. */"), SL("static S99_UNUSED int64_t"), SL("s99_narrow(int64_t v, int64_t lo, int64_t hi, const char *where)"), SL("{"), SL(" if (v < lo || v > hi) s99_fail(where, \"integer overflow\");"), SL(" return v;"), SL("}"), SL(""), SL("static S99_UNUSED uint64_t"), SL("s99_uadd(uint64_t a, uint64_t b, uint64_t hi, const char *where)"), SL("{"), SL(" if (a > hi - b) s99_fail(where, \"integer overflow\");"), SL(" return a + b;"), SL("}"), SL(""), SL("static S99_UNUSED uint64_t"), SL("s99_usub(uint64_t a, uint64_t b, const char *where)"), SL("{"), SL(" if (b > a) s99_fail(where, \"integer overflow\");"), SL(" return a - b;"), SL("}"), SL(""), SL("static S99_UNUSED uint64_t"), SL("s99_umul(uint64_t a, uint64_t b, uint64_t hi, const char *where)"), SL("{"), SL(" if (a && b > hi / a) s99_fail(where, \"integer overflow\");"), SL(" return a * b;"), SL("}"), SL(""), SL("static S99_UNUSED uint64_t"), SL("s99_upow(uint64_t a, uint64_t b, uint64_t hi, const char *where)"), SL("{"), SL(" uint64_t r = 1;"), SL(" while (b) {"), SL(" if (b & 1) r = s99_umul(r, a, hi, where);"), SL(" b >>= 1;"), SL(" if (b) a = s99_umul(a, a, hi, where);"), SL(" }"), SL(" return r;"), SL("}"), SL(""), SL("static S99_UNUSED uint64_t"), SL("s99_udiv(uint64_t a, uint64_t b, const char *where)"), SL("{"), SL(" if (!b) s99_fail(where, \"division by zero\");"), SL(" return a / b;"), SL("}"), SL(""), SL("static S99_UNUSED uint64_t"), SL("s99_umod(uint64_t a, uint64_t b, const char *where)"), SL("{"), SL(" if (!b) s99_fail(where, \"division by zero\");"), SL(" return a % b;"), SL("}"), SL(""), SL("/* Casts that check the value fits: from a float, or between integers. */"), SL("static S99_NORETURN S99_UNUSED void"), SL("s99_misfit(const char *where, const char *v, int64_t lo, uint64_t hi)"), SL("{"), SL(" const char *name = hi == 127 ? \"I8\" : hi == 32767 ? \"I16\" : hi == 2147483647 ? \"I32\" : hi == 255 ? \"U8\""), SL(" : hi == 65535 ? \"U16\" : hi == 4294967295u ? \"U32\" : lo ? \"Int\" : hi == UINT64_MAX ? \"U64\" : \"Int\";"), SL(" char msg[160] = {0};"), SL(" snprintf(msg, sizeof(msg), \"%s does not fit %s, which holds %lld to %llu\", v, name, (long long)lo,"), SL(" (unsigned long long)hi);"), SL(" s99_fail(where, msg);"), SL("}"), SL(""), SL("static S99_NORETURN S99_UNUSED void"), SL("s99_misfit_float(const char *where, double v, int64_t lo, uint64_t hi)"), SL("{"), SL(" s99_stream o = {0};"), SL(" s99_put_float(&o, v); /* as Star99 prints it: nan, inf, 2.5 */"), SL(" s99_put(&o, \"\", 1);"), SL(" s99_misfit(where, (const char *)o.buf, lo, hi);"), SL("}"), SL(""), SL("static S99_UNUSED int64_t"), SL("s99_f2i(double v, int64_t lo, int64_t hi, const char *where)"), SL("{"), SL(" /* cut toward zero, then it must fit: -128.7 is -128, an I8 (NaN fails both) */"), SL(" bool ok = (lo == INT64_MIN ? v >= (double)lo : v > (double)lo - 1.0) && v < (double)hi + 1.0;"), SL(" if (!ok) s99_misfit_float(where, v, lo, (uint64_t)hi);"), SL(" return (int64_t)v;"), SL("}"), SL(""), SL("static S99_UNUSED uint64_t"), SL("s99_f2u(double v, uint64_t hi, const char *where)"), SL("{"), SL(" if (!(v > -1.0 && v < (double)hi + 1.0)) s99_misfit_float(where, v, 0, hi); /* -0.5 is 0 */"), SL(" return (uint64_t)v;"), SL("}"), SL(""), SL("static S99_UNUSED int64_t"), SL("s99_i2i(int64_t v, int64_t lo, int64_t hi, const char *where)"), SL("{"), SL(" char t[32] = {0};"), SL(" if (v < lo || v > hi) snprintf(t, sizeof(t), \"%lld\", (long long)v), s99_misfit(where, t, lo, (uint64_t)hi);"), SL(" return v;"), SL("}"), SL(""), SL("static S99_UNUSED uint64_t"), SL("s99_u2u(uint64_t v, int64_t lo, uint64_t hi, const char *where)"), SL("{"), SL(" char t[32] = {0};"), SL(" if (v > hi) snprintf(t, sizeof(t), \"%llu\", (unsigned long long)v), s99_misfit(where, t, lo, hi);"), SL(" return v;"), SL("}"), SL(""), SL("/* Lists: a header that stays put while its elements move. Variables own"), SL(" their header, so references to elements follow the variable. A few bytes"), SL(" of elements fit in the header itself, so a short string is one block. */"), SL("/* The length and capacity are long long, which no element type is (Int is"), SL(" int64_t, a long here): under strict aliasing, storing an element cannot"), SL(" change them, so the C compiler keeps them in registers in loops. */"), SL("typedef struct {"), SL(" uint64_t gen;"), SL(" long long len, cap;"), SL(" int64_t elemsize;"), SL(" uint8_t *data;"), SL(" uint8_t small[24];"), SL("} s99_list;"), SL(""), SL("static S99_UNUSED s99_list *"), SL("s99_list_new(int64_t elemsize)"), SL("{"), SL(" s99_list *l = (s99_list *)s99_block(sizeof(s99_list));"), SL(" l->gen = s99_gens++;"), SL(" l->elemsize = elemsize;"), SL(" return l;"), SL("}"), SL(""), SL("/* Exactly n bytes, so a sanitizer catches reads past them: fuzz input. */"), SL("static S99_UNUSED s99_list *"), SL("s99_bytes(const void *p, int64_t n)"), SL("{"), SL(" s99_list *l = s99_list_new(1);"), SL(" l->data = (uint8_t *)s99_xalloc((size_t)n);"), SL(" if (n) memcpy(l->data, p, (size_t)n);"), SL(" l->len = l->cap = n;"), SL(" return l;"), SL("}"), SL(""), SL("static S99_UNUSED void"), SL("s99_list_free(s99_list *l)"), SL("{"), SL(" if (!l) return;"), SL(" if (l->data != l->small) s99_xfree(l->data);"), SL(" s99_unblock(l, sizeof(s99_list));"), SL("}"), SL(""), SL("static S99_UNUSED int64_t"), SL("s99_list_len(const s99_list *l)"), SL("{"), SL(" return l ? l->len : 0;"), SL("}"), SL(""), SL("static S99_UNUSED void *"), SL("s99_list_at(const s99_list *l, int64_t i, const char *where)"), SL("{"), SL(" if (!l || i < 0 || i >= l->len) s99_fail_index(where, i, l ? l->len : 0);"), SL(" return l->data + i * l->elemsize;"), SL("}"), SL(""), SL("/* The elements, after checking that i is one: the caller indexes them with"), SL(" their own type, so the element size is a constant. */"), SL("static S99_UNUSED void *"), SL("s99_list_data(const s99_list *l, int64_t i, const char *where)"), SL("{"), SL(" if (S99_UNLIKELY(!l || (uint64_t)i >= (uint64_t)l->len)) s99_fail_index(where, i, l ? l->len : 0);"), SL(" return l->data;"), SL("}"), SL(""), SL("static S99_UNUSED void"), SL("s99_list_reserve(s99_list *l, int64_t n)"), SL("{"), SL(" if (n <= l->cap) return;"), SL(" int64_t small = l->elemsize ? (int64_t)sizeof(l->small) / l->elemsize : 0;"), SL(" if (!l->data && n <= small) {"), SL(" l->data = l->small;"), SL(" l->cap = small;"), SL(" return;"), SL(" }"), SL(" int64_t cap = s99_grown(l->cap ? l->cap : 8, n);"), SL(" size_t bytes = s99_nbytes(cap, l->elemsize);"), SL(" /* elements past len are never read: whoever extends len writes them */"), SL(" if (l->data == l->small) {"), SL(" uint8_t *d = (uint8_t *)s99_xalloc(bytes);"), SL(" memcpy(d, l->small, (size_t)(l->len * l->elemsize));"), SL(" l->data = d;"), SL(" } else {"), SL(" l->data = (uint8_t *)s99_xrealloc(l->data, bytes);"), SL(" }"), SL(" l->cap = cap;"), SL("}"), SL(""), SL("static S99_UNUSED s99_list *"), SL("s99_list_hdr(s99_list **pl, int64_t elemsize)"), SL("{"), SL(" if (!*pl) *pl = s99_list_new(elemsize);"), SL(" return *pl;"), SL("}"), SL(""), SL("/* The header of l with room for one more element: add(&xs, x) on a local"), SL(" writes xs = s99_list_room(xs, size) when full, so xs stays in a register. */"), SL("static S99_UNUSED s99_list *"), SL("s99_list_room(s99_list *l, int64_t elemsize)"), SL("{"), SL(" if (!l) l = s99_list_new(elemsize);"), SL(" s99_list_reserve(l, l->len + 1);"), SL(" return l;"), SL("}"), SL(""), SL("static S99_UNUSED void *"), SL("s99_list_push_slow(s99_list **pl, int64_t elemsize)"), SL("{"), SL(" s99_list *l = s99_list_hdr(pl, elemsize);"), SL(" s99_list_reserve(l, l->len + 1);"), SL(" return l->data + l->len++ * elemsize;"), SL("}"), SL(""), SL("/* Appends an element, which the caller writes, and returns it. */"), SL("static S99_UNUSED void *"), SL("s99_list_push(s99_list **pl, int64_t elemsize)"), SL("{"), SL(" s99_list *l = *pl;"), SL(" if (S99_LIKELY(l && l->len < l->cap)) return l->data + l->len++ * elemsize;"), SL(" return s99_list_push_slow(pl, elemsize);"), SL("}"), SL(""), SL("/* Strings are lists of bytes: everything a list does works on them. They"), SL(" differ only in how they print and in substring in. */"), SL("typedef s99_list *s99_str;"), SL(""), SL("static S99_UNUSED s99_str"), SL("s99_lit(const char *s, int64_t len)"), SL("{"), SL(" return s99_bytes(s, len);"), SL("}"), SL(""), SL("static S99_UNUSED int64_t"), SL("s99_str_len(s99_str s)"), SL("{"), SL(" return s ? s->len : 0;"), SL("}"), SL(""), SL("static S99_UNUSED const uint8_t *"), SL("s99_str_data(s99_str s)"), SL("{"), SL(" return s && s->len ? s->data : (const uint8_t *)\"\";"), SL("}"), SL(""), SL("/* Comparisons take the addresses of the values, like the generated helpers. */"), SL("static S99_UNUSED bool"), SL("s99_str_eq(const s99_str *a, const s99_str *b)"), SL("{"), SL(" int64_t n = s99_str_len(*a);"), SL(" return n == s99_str_len(*b) && (!n || !memcmp((*a)->data, (*b)->data, (size_t)n));"), SL("}"), SL(""), SL("static S99_UNUSED bool"), SL("s99_str_is(const s99_str *a, const char *lit, int64_t n)"), SL("{"), SL(" return s99_str_len(*a) == n && (!n || !memcmp((*a)->data, lit, (size_t)n));"), SL("}"), SL(""), SL("static S99_UNUSED int"), SL("s99_str_cmp(const s99_str *a, const s99_str *b)"), SL("{"), SL(" int64_t na = s99_str_len(*a), nb = s99_str_len(*b), n = na < nb ? na : nb;"), SL(" int c = n ? memcmp((*a)->data, (*b)->data, (size_t)n) : 0;"), SL(" if (c) return c;"), SL(" return na < nb ? -1 : na > nb;"), SL("}"), SL(""), SL("static S99_UNUSED bool"), SL("s99_str_contains(const s99_str *hay, const s99_str *needle)"), SL("{"), SL(" int64_t nh = s99_str_len(*hay), nn = s99_str_len(*needle);"), SL(" for (int64_t i = 0; i + nn <= nh; i++) {"), SL(" if (!nn || !memcmp((*hay)->data + i, (*needle)->data, (size_t)nn)) return true;"), SL(" }"), SL(" return false;"), SL("}"), SL(""), SL("/* 'a' in \"cat\": a byte of the string */"), SL("static S99_UNUSED bool"), SL("s99_str_has_byte(const s99_str *hay, uint8_t b)"), SL("{"), SL(" int64_t nh = s99_str_len(*hay);"), SL(" for (int64_t i = 0; i < nh; i++)"), SL(" if ((*hay)->data[i] == b) return true;"), SL(" return false;"), SL("}"), SL(""), SL("static S99_UNUSED char *"), SL("s99_cstr(s99_str s)"), SL("{"), SL(" int64_t n = s99_str_len(s);"), SL(" char *r = (char *)s99_alloc((size_t)n + 1);"), SL(" if (n) memcpy(r, s->data, (size_t)n);"), SL(" return r;"), SL("}"), SL(""), SL("static S99_UNUSED void"), SL("s99_put_str(s99_stream *o, const s99_str *s)"), SL("{"), SL(" s99_put(o, s99_str_data(*s), s99_str_len(*s));"), SL("}"), SL(""), SL("/* Appends bytes: add(&s, text) and str() both come here. */"), SL("static S99_UNUSED void"), SL("s99_str_append(s99_str *s, const void *data, int64_t n)"), SL("{"), SL(" s99_list *l = *s;"), SL(" if (S99_LIKELY(l && l->cap - l->len >= n)) {"), SL(" memcpy(l->data + l->len, data, (size_t)n);"), SL(" l->len += n;"), SL(" return;"), SL(" }"), SL(" if (!n) return;"), SL(" l = s99_list_hdr(s, 1);"), SL(" s99_list_reserve(l, l->len + n);"), SL(" memcpy(l->data + l->len, data, (size_t)n);"), SL(" l->len += n;"), SL("}"), SL(""), SL("/* add(&s, text): text may be s itself, so its length is taken first. */"), SL("static S99_UNUSED void"), SL("s99_str_append_str(s99_str *s, s99_str src)"), SL("{"), SL(" int64_t n = s99_str_len(src);"), SL(" if (!n) return;"), SL(" s99_list *l = s99_list_hdr(s, 1);"), SL(" bool self = src == l;"), SL(" s99_list_reserve(l, l->len + n);"), SL(" memcpy(l->data + l->len, self ? l->data : src->data, (size_t)n);"), SL(" l->len += n;"), SL("}"), SL(""), SL("static S99_UNUSED void"), SL("s99_str_append_int(s99_str *s, int64_t v)"), SL("{"), SL(" char tmp[24]; /* s99_digits writes what is read; zeroed, every Int printed pays */"), SL(" int n = s99_digits(tmp + sizeof(tmp), v);"), SL(" s99_str_append(s, tmp + sizeof(tmp) - n, n);"), SL("}"), SL(""), SL("static S99_UNUSED void"), SL("s99_put_quoted(s99_stream *o, const s99_str *sp)"), SL("{"), SL(" s99_str s = *sp;"), SL(" s99_put(o, \"\\\"\", 1);"), SL(" for (int64_t i = 0; i < s99_str_len(s); i++) {"), SL(" uint8_t c = s->data[i];"), SL(" if (c == '\"' || c == '\\\\') {"), SL(" s99_put(o, \"\\\\\", 1);"), SL(" s99_put(o, &c, 1);"), SL(" } else if (c == '\\n') {"), SL(" s99_puts(o, \"\\\\n\");"), SL(" } else if (c == '\\t') {"), SL(" s99_puts(o, \"\\\\t\");"), SL(" } else if (c < 32 || c == 127) {"), SL(" char tmp[8] = {0};"), SL(" snprintf(tmp, sizeof(tmp), \"\\\\x%02x\", c);"), SL(" s99_puts(o, tmp);"), SL(" } else {"), SL(" s99_put(o, &c, 1);"), SL(" }"), SL(" }"), SL(" s99_put(o, \"\\\"\", 1);"), SL("}"), SL(""), SL("static S99_UNUSED void"), SL("s99_stream_free(s99_stream *o)"), SL("{"), SL(" if (o->buf != o->local) free(o->buf);"), SL(" o->buf = 0;"), SL(" o->len = o->cap = 0;"), SL("}"), SL(""), SL("static S99_UNUSED s99_str"), SL("s99_stream_str(s99_stream *o)"), SL("{"), SL(" s99_str r = s99_list_new(1);"), SL(" if (o->buf == o->local || o->len <= (int64_t)sizeof(r->small) || o->len * 2 < o->cap ||"), SL(" S99_IN_AREA()) {"), SL(" /* short, mostly empty or in an area (which never frees a malloc"), SL(" buffer): copied, into the header when it fits */"), SL(" s99_list_reserve(r, o->len);"), SL(" if (o->len) memcpy(r->data, o->buf, (size_t)o->len);"), SL(" r->len = o->len;"), SL(" s99_stream_free(o);"), SL(" return r;"), SL(" }"), SL(" r->data = o->buf; /* the string takes the buffer */"), SL(" r->len = o->len;"), SL(" r->cap = o->cap;"), SL(" return r;"), SL("}"), SL(""), SL("static S99_UNUSED void *"), SL("s99_list_insert(s99_list **pl, int64_t elemsize, int64_t i, const char *where)"), SL("{"), SL(" s99_list *l = s99_list_hdr(pl, elemsize);"), SL(" if (i < 0 || i > l->len) s99_fail(where, \"insert position out of bounds\");"), SL(" s99_list_reserve(l, l->len + 1);"), SL(" memmove(l->data + (i + 1) * elemsize, l->data + i * elemsize, (size_t)((l->len - i) * elemsize));"), SL(" memset(l->data + i * elemsize, 0, (size_t)elemsize);"), SL(" l->len++;"), SL(" return l->data + i * elemsize;"), SL("}"), SL(""), SL("/* Sets the length to n; new elements are zero. The caller releases the"), SL(" elements past n first and fills in defaults after. */"), SL("static S99_UNUSED void"), SL("s99_list_resize(s99_list **pl, int64_t elemsize, int64_t n, const char *where)"), SL("{"), SL(" if (n < 0) s99_fail(where, \"resize to a negative length\");"), SL(" s99_list *l = s99_list_hdr(pl, elemsize);"), SL(" s99_list_reserve(l, n);"), SL(" if (n > l->len) memset(l->data + l->len * elemsize, 0, (size_t)((n - l->len) * elemsize));"), SL(" l->len = n;"), SL("}"), SL(""), SL("/* Removes element i; the caller has released it already. */"), SL("static S99_UNUSED void"), SL("s99_list_cut(s99_list *l, int64_t i)"), SL("{"), SL(" memmove(l->data + i * l->elemsize, l->data + (i + 1) * l->elemsize, (size_t)((l->len - i - 1) * l->elemsize));"), SL(" l->len--;"), SL(" memset(l->data + l->len * l->elemsize, 0, (size_t)l->elemsize);"), SL("}"), SL(""), SL("/* Boxes hold union payloads and Any values. */"), SL("/* The top byte of a box generation is the size class of its block, so a"), SL(" box is freed without knowing its payload type. */"), SL("typedef struct {"), SL(" uint64_t gen;"), SL("} s99_box;"), SL(""), SL("#define S99_BOXDATA(b) ((void *)((char *)(b) + sizeof(s99_box)))"), SL(""), SL("static S99_UNUSED s99_box *"), SL("s99_box_new(size_t size)"), SL("{"), SL(" s99_box *b = (s99_box *)s99_block(sizeof(s99_box) + size);"), SL(" b->gen = s99_gens++ | (uint64_t)s99_class(sizeof(s99_box) + size) << 56;"), SL(" return b;"), SL("}"), SL(""), SL("/* A box whose payload the caller stores right away. */"), SL("static S99_UNUSED s99_box *"), SL("s99_box_filled(size_t size)"), SL("{"), SL(" s99_box *b = (s99_box *)s99_block_raw(sizeof(s99_box) + size);"), SL(" b->gen = s99_gens++ | (uint64_t)s99_class(sizeof(s99_box) + size) << 56;"), SL(" return b;"), SL("}"), SL(""), SL("static S99_UNUSED void"), SL("s99_box_free(s99_box *b)"), SL("{"), SL(" if (!b) return;"), SL(" /* a new generation: references to the payload stop working now */"), SL(" uint64_t g = s99_gen_get(b);"), SL(" g = (g & (uint64_t)0xff << 56) | (s99_gens++ & (((uint64_t)1 << 56) - 1));"), SL(" s99_gen_set(b, g);"), SL(" s99_unblock(b, s99_class_size((int)(g >> 56)));"), SL("}"), SL(""), SL("/* References: a route to a place, checked whenever it is followed. */"), SL("enum { S99_NILREF, S99_CELL, S99_LIST, S99_POOL, S99_MAP, S99_BOX, S99_NEST };"), SL(""), SL("typedef struct {"), SL(" void *base;"), SL(" uint64_t gen;"), SL(" int64_t index;"), SL(" int32_t kind;"), SL(" int32_t off;"), SL("} s99_ref;"), SL(""), SL("typedef struct {"), SL(" uint64_t gen;"), SL(" int64_t len, cap, slotsize;"), SL(" uint8_t *slots;"), SL(" int64_t freehead;"), SL("} s99_pool;"), SL(""), SL("typedef struct {"), SL(" uint64_t version;"), SL(" int64_t nextfree;"), SL("} s99_slot;"), SL(""), SL("#define S99_SLOTDATA(p) ((void *)((char *)(p) + 16))"), SL(""), SL("typedef struct s99_mapent {"), SL(" uint64_t gen;"), SL(" uint32_t alive;"), SL(" uint32_t ord; /* its place in the order of insertion */"), SL("} s99_mapent;"), SL(""), SL("#define S99_ENTDATA(e) ((void *)((char *)(e) + sizeof(s99_mapent)))"), SL(""), SL("/* A reference into a list that is itself an element or a map value"), SL(" (&grid[1][1], &m[k][0]) is a route: the place holding the inner list,"), SL(" followed on each use, then a position in whatever list is there now."), SL(" Routes are not counted, so each lives for the rest of the program; there"), SL(" is one per distinct outer place and element size. */"), SL("typedef struct {"), SL(" s99_ref outer;"), SL(" int64_t elemsize;"), SL("} s99_route;"), SL(""), SL("static S99_UNUSED s99_route **s99_routes = 0;"), SL("static S99_UNUSED size_t s99_nroutes = 0;"), SL("static S99_UNUSED size_t s99_caproutes = 0;"), SL(""), SL("static S99_UNUSED size_t"), SL("s99_route_hash(const s99_ref *o, int64_t elemsize)"), SL("{"), SL(" uint64_t h = (uint64_t)(uintptr_t)o->base * 0x9e3779b97f4a7c15u;"), SL(" h ^= o->gen + 0x632be59bd9b4e019u + (h << 6) + (h >> 2);"), SL(" h ^= (uint64_t)o->index + ((uint64_t)o->kind << 32) + (uint64_t)(uint32_t)o->off + (h << 6) + (h >> 2);"), SL(" h ^= (uint64_t)elemsize + (h << 6) + (h >> 2);"), SL(" return (size_t)h;"), SL("}"), SL(""), SL("static S99_UNUSED s99_route *"), SL("s99_route_get(s99_ref outer, int64_t elemsize)"), SL("{"), SL(" if (2 * (s99_nroutes + 1) > s99_caproutes) { /* grow, keeping it half empty */"), SL(" size_t cap = s99_caproutes ? s99_caproutes * 2 : 64;"), SL(" s99_route **t = (s99_route **)calloc(cap, sizeof(s99_route *));"), SL(" if (!t) s99_fail(\"star99\", \"out of memory\");"), SL(" for (size_t i = 0; i < s99_caproutes; i++) {"), SL(" s99_route *r = s99_routes[i];"), SL(" if (!r) continue;"), SL(" size_t j = s99_route_hash(&r->outer, r->elemsize) & (cap - 1);"), SL(" while (t[j]) j = (j + 1) & (cap - 1);"), SL(" t[j] = r;"), SL(" }"), SL(" free(s99_routes);"), SL(" s99_routes = t;"), SL(" s99_caproutes = cap;"), SL(" }"), SL(" size_t j = s99_route_hash(&outer, elemsize) & (s99_caproutes - 1);"), SL(" for (s99_route *r = 0; (r = s99_routes[j]); j = (j + 1) & (s99_caproutes - 1)) {"), SL(" if (r->elemsize == elemsize && r->outer.base == outer.base && r->outer.gen == outer.gen &&"), SL(" r->outer.index == outer.index && r->outer.kind == outer.kind && r->outer.off == outer.off)"), SL(" return r;"), SL(" }"), SL(" s99_route *r = (s99_route *)malloc(sizeof(s99_route));"), SL(" if (!r) s99_fail(\"star99\", \"out of memory\");"), SL(" r->outer = outer;"), SL(" r->elemsize = elemsize;"), SL(" s99_routes[j] = r;"), SL(" s99_nroutes++;"), SL(" return r;"), SL("}"), SL(""), SL("static S99_UNUSED void *s99_deref_nest(s99_ref r, const char *where);"), SL("static S99_UNUSED void *s99_deref_map(s99_ref r, const char *where);"), SL(""), SL("/* Why a reference does not reach its target, out of the way of the paths"), SL(" that do. */"), SL("static S99_UNUSED S99_NORETURN void"), SL("s99_deref_fail(s99_ref r, const char *where)"), SL("{"), SL(" switch (r.kind) {"), SL(" case S99_LIST:"), SL(" if (s99_gen_get(r.base) != r.gen) s99_fail(where, \"the list this reference points into is gone\");"), SL(" s99_fail(where, \"this reference points past the end of its list\");"), SL(" case S99_POOL: s99_fail(where, \"this reference points to an item that was removed\");"), SL(" case S99_MAP: s99_fail(where, \"this reference points to a key that was removed\");"), SL(" case S99_BOX: s99_fail(where, \"this reference points into a value that changed variant or was replaced\");"), SL(" default: s99_fail(where, \"this reference is nil\");"), SL(" }"), SL("}"), SL(""), SL("/* Followed on every use: small and inline, so the C compiler sees the kind"), SL(" and shares the checks of repeated uses. */"), SL("static S99_UNUSED S99_INLINE void *"), SL("s99_deref(s99_ref r, const char *where)"), SL("{"), SL(" /* tests, not a switch: each is a predictable branch, and the C compiler"), SL(" can merge the tests of repeated uses */"), SL(" if (r.kind == S99_CELL) return (char *)r.base + r.off;"), SL(" if (r.kind == S99_POOL) {"), SL(" s99_pool *p = (s99_pool *)r.base;"), SL(" if (S99_LIKELY((uint64_t)r.index < (uint64_t)p->len)) {"), SL(" s99_slot *s = (s99_slot *)(p->slots + r.index * p->slotsize);"), SL(" if (S99_LIKELY(s->version == r.gen)) return (char *)S99_SLOTDATA(s) + r.off;"), SL(" }"), SL(" } else if (r.kind == S99_LIST) {"), SL(" s99_list *l = (s99_list *)r.base;"), SL(" if (S99_LIKELY(s99_gen_get(l) == r.gen && (uint64_t)r.index < (uint64_t)l->len))"), SL(" return l->data + r.index * l->elemsize + r.off;"), SL(" } else if (r.kind == S99_BOX) {"), SL(" s99_box *b = (s99_box *)r.base;"), SL(" if (S99_LIKELY(s99_gen_get(b) == r.gen)) return (char *)S99_BOXDATA(b) + r.off;"), SL(" } else if (r.kind == S99_MAP) {"), SL(" return s99_deref_map(r, where);"), SL(" } else if (r.kind == S99_NEST) {"), SL(" return s99_deref_nest(r, where);"), SL(" }"), SL(" s99_deref_fail(r, where);"), SL(" return 0; /* not reached; tcc does not know s99_deref_fail returns no more */"), SL("}"), SL(""), SL("/* Out of line: the outer place is followed with s99_deref again. */"), SL("static S99_UNUSED void *"), SL("s99_deref_nest(s99_ref r, const char *where)"), SL("{"), SL(" s99_route *rt = (s99_route *)r.base;"), SL(" s99_list *l = *(s99_list **)s99_deref(rt->outer, where);"), SL(" if (!l || (uint64_t)r.index >= (uint64_t)l->len)"), SL(" s99_fail(where, \"this reference points past the end of its list\");"), SL(" return l->data + r.index * rt->elemsize + r.off;"), SL("}"), SL(""), SL("/* &grid[i][j]: position j of the list at the place outer reaches. */"), SL("static S99_UNUSED s99_ref"), SL("s99_ref_nest(s99_ref outer, int64_t elemsize, int64_t i, const char *where)"), SL("{"), SL(" s99_list *l = *(s99_list **)s99_deref(outer, where);"), SL(" int64_t len = l ? l->len : 0;"), SL(" if (i < 0 || i >= len) s99_fail_index(where, i, len);"), SL(" s99_ref r = {s99_route_get(outer, elemsize), 0, i, S99_NEST, 0};"), SL(" return r;"), SL("}"), SL(""), SL("static S99_UNUSED s99_ref"), SL("s99_ref_cell(void *p)"), SL("{"), SL(" s99_ref r = {p, 0, 0, S99_CELL, 0};"), SL(" return r;"), SL("}"), SL(""), SL("static S99_UNUSED s99_ref"), SL("s99_ref_list(s99_list **pl, int64_t elemsize, int64_t i, const char *where)"), SL("{"), SL(" s99_list *l = s99_list_hdr(pl, elemsize);"), SL(" if (i < 0 || i >= l->len) s99_fail_index(where, i, l->len);"), SL(" s99_ref r = {l, l->gen, i, S99_LIST, 0};"), SL(" return r;"), SL("}"), SL(""), SL("static S99_UNUSED s99_ref"), SL("s99_ref_off(s99_ref r, int64_t off)"), SL("{"), SL(" r.off += (int32_t)off;"), SL(" return r;"), SL("}"), SL(""), SL("static S99_UNUSED bool"), SL("s99_ref_eq(const s99_ref *a, const s99_ref *b)"), SL("{"), SL(" if (a->kind != b->kind) return false;"), SL(" if (a->kind == S99_NILREF) return true;"), SL(" if (a->kind == S99_MAP) return a->base == b->base && a->gen == b->gen && a->off == b->off;"), SL(" return a->base == b->base && a->gen == b->gen && a->index == b->index && a->off == b->off;"), SL("}"), SL(""), SL("/* While a value holding references is copied, the copy of each list, box,"), SL(" pool and map entry is recorded, so references that lead inside the value"), SL(" can be moved over to the copy afterwards. */"), SL("typedef struct {"), SL(" void *old, *new_;"), SL(" uint64_t gen;"), SL("} s99_pair;"), SL(""), SL("static S99_UNUSED s99_pair *s99_pairs = 0;"), SL("static S99_UNUSED size_t s99_npairs = 0;"), SL("static S99_UNUSED size_t s99_cappairs = 0;"), SL("static S99_UNUSED int s99_remap_depth = 0;"), SL("static S99_UNUSED char *s99_rsrc = 0;"), SL("static S99_UNUSED char *s99_rdst = 0;"), SL("static S99_UNUSED size_t s99_rsize = 0;"), SL(""), SL("static S99_UNUSED void"), SL("s99_remap_begin(void *src, void *dst, size_t size)"), SL("{"), SL(" if (s99_remap_depth++) return;"), SL(" s99_rsrc = (char *)src;"), SL(" s99_rdst = (char *)dst;"), SL(" s99_rsize = size;"), SL(" s99_npairs = 0;"), SL("}"), SL(""), SL("static S99_UNUSED void"), SL("s99_remap_end(void)"), SL("{"), SL(" if (--s99_remap_depth) return;"), SL(" s99_npairs = 0;"), SL("}"), SL(""), SL("static S99_UNUSED void"), SL("s99_remap_add(void *old, void *n, uint64_t gen)"), SL("{"), SL(" if (!s99_remap_depth) return;"), SL(" if (s99_npairs == s99_cappairs) {"), SL(" s99_cappairs = s99_cappairs ? s99_cappairs * 2 : 64;"), SL(" s99_pairs = (s99_pair *)s99_realloc(s99_pairs, s99_cappairs * sizeof(*s99_pairs));"), SL(" }"), SL(" s99_pair p = {old, n, gen};"), SL(" s99_pairs[s99_npairs++] = p;"), SL("}"), SL(""), SL("static S99_UNUSED void"), SL("s99_ref_fix(s99_ref *r)"), SL("{"), SL(" if (r->kind == S99_NEST) { /* a route through a place inside the copy */"), SL(" s99_route *rt = (s99_route *)r->base;"), SL(" s99_ref o = rt->outer;"), SL(" s99_ref_fix(&o);"), SL(" if (o.base != rt->outer.base || o.gen != rt->outer.gen) r->base = s99_route_get(o, rt->elemsize);"), SL(" return;"), SL(" }"), SL(" if (r->kind == S99_CELL) {"), SL(" char *b = (char *)r->base;"), SL(" if (b >= s99_rsrc && b < s99_rsrc + s99_rsize) r->base = s99_rdst + (b - s99_rsrc);"), SL(" return;"), SL(" }"), SL(" for (size_t i = 0; i < s99_npairs; i++) {"), SL(" if (s99_pairs[i].old == r->base) {"), SL(" r->base = s99_pairs[i].new_;"), SL(" if (r->kind != S99_POOL && r->kind != S99_MAP) r->gen = s99_pairs[i].gen;"), SL(" return;"), SL(" }"), SL(" }"), SL("}"), SL(""), SL("/* Pools: items with versions. A slot's version is 0 while it is free. */"), SL("static S99_UNUSED s99_pool *"), SL("s99_pool_new(int64_t elemsize)"), SL("{"), SL(" s99_pool *p = (s99_pool *)s99_block(sizeof(s99_pool));"), SL(" p->gen = s99_gens++;"), SL(" p->slotsize = 16 + ((elemsize + 15) / 16) * 16;"), SL(" p->freehead = -1;"), SL(" return p;"), SL("}"), SL(""), SL("static S99_UNUSED s99_pool *"), SL("s99_pool_hdr(s99_pool **pp, int64_t elemsize)"), SL("{"), SL(" if (!*pp) *pp = s99_pool_new(elemsize);"), SL(" return *pp;"), SL("}"), SL(""), SL("static S99_UNUSED int64_t"), SL("s99_pool_count(const s99_pool *p)"), SL("{"), SL(" if (!p) return 0;"), SL(" int64_t n = 0;"), SL(" for (int64_t i = 0; i < p->len; i++) n += ((s99_slot *)(p->slots + i * p->slotsize))->version != 0;"), SL(" return n;"), SL("}"), SL(""), SL("static S99_UNUSED int64_t"), SL("s99_pool_grow(s99_pool *p)"), SL("{"), SL(" if (p->len == p->cap) {"), SL(" int64_t cap = p->cap ? s99_grown(p->cap, p->cap + 1) : 4;"), SL(" p->slots = (uint8_t *)s99_xrealloc(p->slots, s99_nbytes(cap, p->slotsize));"), SL(" memset(p->slots + p->cap * p->slotsize, 0, (size_t)((cap - p->cap) * p->slotsize));"), SL(" p->cap = cap;"), SL(" }"), SL(" return p->len++;"), SL("}"), SL(""), SL("/* A new item, zero until the caller stores it (with the element size a"), SL(" constant of the call, a store of the whole item replaces the zeroing). */"), SL("static S99_UNUSED S99_INLINE s99_ref"), SL("s99_pool_add(s99_pool **pp, int64_t elemsize, void **data)"), SL("{"), SL(" s99_pool *p = s99_pool_hdr(pp, elemsize);"), SL(" int64_t i = p->freehead;"), SL(" if (S99_LIKELY(i >= 0))"), SL(" p->freehead = ((s99_slot *)(p->slots + i * p->slotsize))->nextfree;"), SL(" else"), SL(" i = s99_pool_grow(p);"), SL(" s99_slot *s = (s99_slot *)(p->slots + i * p->slotsize);"), SL(" memset(S99_SLOTDATA(s), 0, (size_t)elemsize);"), SL(" s->version = s99_gens++;"), SL(" s->nextfree = -1;"), SL(" *data = S99_SLOTDATA(s);"), SL(" s99_ref r = {p, s->version, i, S99_POOL, 0};"), SL(" return r;"), SL("}"), SL(""), SL("/* Finds the slot an item reference names, or stops the program. */"), SL("static S99_UNUSED S99_INLINE void *"), SL("s99_pool_item(s99_pool *p, const s99_ref *r, const char *where)"), SL("{"), SL(" if (!p || r->kind != S99_POOL || r->base != p) s99_fail(where, \"this reference is not to an item of this pool\");"), SL(" return s99_deref(*r, where);"), SL("}"), SL(""), SL("static S99_UNUSED S99_INLINE void"), SL("s99_pool_free_slot(s99_pool *p, int64_t i)"), SL("{"), SL(" s99_slot *s = (s99_slot *)(p->slots + i * p->slotsize);"), SL(" s->version = 0;"), SL(" s->nextfree = p->freehead;"), SL(" p->freehead = i;"), SL("}"), SL(""), SL("static S99_UNUSED S99_INLINE bool"), SL("s99_pool_has(const s99_pool *p, const s99_ref *r)"), SL("{"), SL(" if (!p || r->kind != S99_POOL || r->base != p || r->index < 0 || r->index >= p->len) return false;"), SL(" return ((s99_slot *)(p->slots + r->index * p->slotsize))->version == r->gen;"), SL("}"), SL(""), SL("/* Maps: entries that never move (references name them), found through an"), SL(" open-addressing index of their hashes, plus the order of insertion. A"), SL(" removed key keeps its entry, dead, and a new insert of it revives it. */"), SL("typedef struct {"), SL(" uint64_t hash;"), SL(" s99_mapent *e;"), SL("} s99_mapslot;"), SL(""), SL("typedef struct {"), SL(" uint64_t gen;"), SL(" int64_t count;"), SL(" s99_mapslot *slots; /* a power of two of them, at most half full */"), SL(" int64_t nslots, used;"), SL(" s99_mapent **order;"), SL(" int64_t norder, cap;"), SL(" int64_t keysize, valsize, valoff;"), SL(" int keykind; /* 0 integer, 1 string, 2 bool */"), SL(" s99_mapent *last; /* the entry found last: k in m then m[k] looks once */"), SL(" uint64_t lasthash;"), SL("} s99_map;"), SL(""), SL("static S99_UNUSED uint64_t"), SL("s99_mix(uint64_t x)"), SL("{"), SL(" x ^= x >> 30;"), SL(" x *= 0xbf58476d1ce4e5b9u;"), SL(" x ^= x >> 27;"), SL(" x *= 0x94d049bb133111ebu;"), SL(" return x ^ (x >> 31);"), SL("}"), SL(""), SL("/* Bytes, eight at a time. */"), SL("static S99_UNUSED uint64_t"), SL("s99_hash_bytes(const uint8_t *p, int64_t n)"), SL("{"), SL(" uint64_t h = 0x9e3779b97f4a7c15u ^ (uint64_t)n;"), SL(" int64_t i = 0;"), SL(" for (; i + 8 <= n; i += 8) {"), SL(" uint64_t w = 0;"), SL(" memcpy(&w, p + i, 8);"), SL(" h = (h ^ w) * 0x100000001b3u;"), SL(" h ^= h >> 29;"), SL(" }"), SL(" uint64_t w = 0;"), SL(" if (i < n) memcpy(&w, p + i, (size_t)(n - i));"), SL(" return s99_mix(h ^ w);"), SL("}"), SL(""), SL("static S99_UNUSED uint64_t"), SL("s99_key_hash(const s99_map *m, const void *key)"), SL("{"), SL(" uint64_t h = 0;"), SL(" if (m->keykind == 1) {"), SL(" s99_str s = *(const s99_str *)key;"), SL(" h = s99_hash_bytes(s99_str_data(s), s99_str_len(s));"), SL(" } else {"), SL(" uint64_t w = 0;"), SL(" memcpy(&w, key, m->keysize <= 8 ? (size_t)m->keysize : 8);"), SL(" h = m->keysize <= 8 ? s99_mix(w) : s99_hash_bytes((const uint8_t *)key, m->keysize);"), SL(" }"), SL(" return h;"), SL("}"), SL(""), SL("/* For integer keys of up to eight bytes the hash is a bijection of the key,"), SL(" so equal hashes are equal keys and the entry need not be read. */"), SL("static S99_UNUSED bool"), SL("s99_hash_is_key(const s99_map *m)"), SL("{"), SL(" return m->keykind != 1 && m->keysize <= 8;"), SL("}"), SL(""), SL("static S99_UNUSED bool"), SL("s99_key_eq(const s99_map *m, const void *a, const void *b)"), SL("{"), SL(" if (m->keykind == 1) return s99_str_eq((const s99_str *)a, (const s99_str *)b);"), SL(" if (m->keysize == 8) return *(const int64_t *)a == *(const int64_t *)b;"), SL(" return !memcmp(a, b, (size_t)m->keysize);"), SL("}"), SL(""), SL("/* Looking up, with the key size and kind constants of the call site, so"), SL(" hashing and probing compile to a few instructions in the caller. */"), SL("static S99_UNUSED S99_INLINE uint64_t"), SL("s99_hash_key(const void *key, int64_t keysize, int keykind)"), SL("{"), SL(" if (keykind == 1) {"), SL(" s99_str s = *(const s99_str *)key;"), SL(" return s99_hash_bytes(s99_str_data(s), s99_str_len(s));"), SL(" }"), SL(" if (keysize > 8) return s99_hash_bytes((const uint8_t *)key, keysize);"), SL(" uint64_t w = 0;"), SL(" memcpy(&w, key, (size_t)keysize);"), SL(" return s99_mix(w);"), SL("}"), SL(""), SL("static S99_UNUSED S99_INLINE bool"), SL("s99_key_is(const void *a, const void *b, int64_t keysize, int keykind)"), SL("{"), SL(" if (keykind == 1) return s99_str_eq((const s99_str *)a, (const s99_str *)b);"), SL(" if (keysize <= 8) return true; /* the hashes were equal: so are the keys */"), SL(" return !memcmp(a, b, (size_t)keysize);"), SL("}"), SL(""), SL("/* The live entry for key, or null. */"), SL("static S99_UNUSED S99_INLINE s99_mapent *"), SL("s99_map_get(s99_map *m, const void *key, int64_t keysize, int keykind)"), SL("{"), SL(" if (!m || !m->nslots) return 0;"), SL(" uint64_t h = s99_hash_key(key, keysize, keykind);"), SL(" s99_mapent *e = m->last;"), SL(" if (e && m->lasthash == h && s99_key_is(S99_ENTDATA(e), key, keysize, keykind))"), SL(" return e->alive ? e : 0;"), SL(" uint64_t mask = (uint64_t)m->nslots - 1;"), SL(" for (uint64_t i = h & mask;; i = (i + 1) & mask) {"), SL(" e = m->slots[i].e;"), SL(" if (!e) return 0;"), SL(" if (m->slots[i].hash == h && s99_key_is(S99_ENTDATA(e), key, keysize, keykind)) {"), SL(" m->last = e;"), SL(" m->lasthash = h;"), SL(" return e->alive ? e : 0;"), SL(" }"), SL(" }"), SL("}"), SL(""), SL("static S99_UNUSED s99_map *"), SL("s99_map_new(int64_t keysize, int64_t valsize, int keykind)"), SL("{"), SL(" s99_map *m = (s99_map *)s99_block(sizeof(s99_map));"), SL(" m->gen = s99_gens++;"), SL(" m->keysize = keysize;"), SL(" m->valsize = valsize;"), SL(" m->valoff = ((keysize + 7) / 8) * 8;"), SL(" m->keykind = keykind;"), SL(" return m;"), SL("}"), SL(""), SL("static S99_UNUSED s99_map *"), SL("s99_map_hdr(s99_map **pm, int64_t keysize, int64_t valsize, int keykind)"), SL("{"), SL(" if (!*pm) *pm = s99_map_new(keysize, valsize, keykind);"), SL(" return *pm;"), SL("}"), SL(""), SL("static S99_UNUSED void"), SL("s99_map_grow(s99_map *m)"), SL("{"), SL(" int64_t n = m->nslots ? m->nslots * 2 : 16;"), SL(" s99_mapslot *s = (s99_mapslot *)s99_xalloc((size_t)n * sizeof(*s));"), SL(" for (int64_t i = 0; i < m->nslots; i++) {"), SL(" if (!m->slots[i].e) continue;"), SL(" int64_t j = (int64_t)(m->slots[i].hash & (uint64_t)(n - 1));"), SL(" while (s[j].e) j = (j + 1) & (n - 1);"), SL(" s[j] = m->slots[i];"), SL(" }"), SL(" s99_xfree(m->slots);"), SL(" m->slots = s;"), SL(" m->nslots = n;"), SL("}"), SL(""), SL("/* Where a key that was removed and comes back used to be. */"), SL("static S99_UNUSED s99_mapent s99_gone = {0};"), SL(""), SL("static S99_UNUSED void"), SL("s99_map_order_add(s99_map *m, s99_mapent *e)"), SL("{"), SL(" if (m->norder == m->cap) {"), SL(" if (m->cap >= UINT32_MAX / 2) s99_fail(\"star99\", \"this map has too many keys\");"), SL(" m->cap = m->cap ? m->cap * 2 : 8;"), SL(" m->order = (s99_mapent **)s99_xrealloc(m->order, (size_t)m->cap * sizeof(*m->order));"), SL(" }"), SL(" e->ord = (uint32_t)m->norder;"), SL(" m->order[m->norder++] = e;"), SL("}"), SL(""), SL("/* The maps being looped over, innermost last: their order stays put, so a"), SL(" loop that removes keys still visits every other key once. */"), SL("static S99_UNUSED struct {"), SL(" s99_map **data;"), SL(" int64_t len, cap;"), SL("} s99_map_loops = {0};"), SL(""), SL("static S99_UNUSED void"), SL("s99_map_loop_push(s99_map *m)"), SL("{"), SL(" if (s99_map_loops.len == s99_map_loops.cap) {"), SL(" s99_map_loops.cap = s99_map_loops.cap ? s99_map_loops.cap * 2 : 8;"), SL(" s99_map_loops.data = (s99_map **)s99_realloc(s99_map_loops.data,"), SL(" (size_t)s99_map_loops.cap * sizeof(s99_map *));"), SL(" }"), SL(" s99_map_loops.data[s99_map_loops.len++] = m;"), SL("}"), SL(""), SL("static S99_UNUSED void"), SL("s99_map_loop_pop(void)"), SL("{"), SL(" s99_map_loops.len--;"), SL("}"), SL(""), SL("/* Finds a live entry for key; with create, makes one (its key is copied by"), SL(" the caller when *created is set). */"), SL("static S99_UNUSED s99_mapent *"), SL("s99_map_find(s99_map *m, const void *key, bool create, bool *created)"), SL("{"), SL(" if (created) *created = false;"), SL(" if (!m) return 0;"), SL(" uint64_t h = s99_key_hash(m, key);"), SL(" s99_mapent *e = 0;"), SL(" bool exact = s99_hash_is_key(m);"), SL(" if (m->last && m->lasthash == h && (exact || s99_key_eq(m, S99_ENTDATA(m->last), key))) {"), SL(" e = m->last;"), SL(" } else if (m->nslots) {"), SL(" for (int64_t i = (int64_t)(h & (uint64_t)(m->nslots - 1));; i = (i + 1) & (m->nslots - 1)) {"), SL(" if (!m->slots[i].e) break;"), SL(" if (m->slots[i].hash == h && (exact || s99_key_eq(m, S99_ENTDATA(m->slots[i].e), key))) {"), SL(" e = m->slots[i].e;"), SL(" break;"), SL(" }"), SL(" }"), SL(" }"), SL(" if (e) {"), SL(" m->last = e;"), SL(" m->lasthash = h;"), SL(" if (e->alive) return e;"), SL(" if (!create) return 0;"), SL(" /* back again: it goes to the end of the order */"), SL(" if (e->ord < m->norder && m->order[e->ord] == e) m->order[e->ord] = &s99_gone;"), SL(" e->alive = 1;"), SL(" e->gen = s99_gens++;"), SL(" memset((char *)S99_ENTDATA(e) + m->valoff, 0, (size_t)m->valsize);"), SL(" m->count++;"), SL(" s99_map_order_add(m, e);"), SL(" if (created) *created = true;"), SL(" return e;"), SL(" }"), SL(" if (!create) return 0;"), SL(" if ((m->used + 1) * 2 > m->nslots) s99_map_grow(m);"), SL(" e = (s99_mapent *)s99_block((size_t)((int64_t)sizeof(s99_mapent) + m->valoff + m->valsize));"), SL(" e->gen = s99_gens++;"), SL(" e->alive = 1;"), SL(" int64_t i = (int64_t)(h & (uint64_t)(m->nslots - 1));"), SL(" while (m->slots[i].e) i = (i + 1) & (m->nslots - 1);"), SL(" m->slots[i].hash = h;"), SL(" m->slots[i].e = e;"), SL(" m->used++;"), SL(" m->count++;"), SL(" s99_map_order_add(m, e);"), SL(" m->last = e;"), SL(" m->lasthash = h;"), SL(" if (created) *created = true;"), SL(" return e;"), SL("}"), SL(""), SL("/* The live entry for key, made when missing: a key already there is found"), SL(" inline. */"), SL("static S99_UNUSED S99_INLINE s99_mapent *"), SL("s99_map_put(s99_map *m, const void *key, int64_t keysize, int keykind, bool *created)"), SL("{"), SL(" s99_mapent *e = s99_map_get(m, key, keysize, keykind);"), SL(" if (e) {"), SL(" *created = false;"), SL(" return e;"), SL(" }"), SL(" return s99_map_find(m, key, true, created);"), SL("}"), SL(""), SL("static S99_UNUSED void *"), SL("s99_map_key(s99_mapent *e)"), SL("{"), SL(" return S99_ENTDATA(e);"), SL("}"), SL(""), SL("static S99_UNUSED void *"), SL("s99_map_val(const s99_map *m, s99_mapent *e)"), SL("{"), SL(" return (char *)S99_ENTDATA(e) + m->valoff;"), SL("}"), SL(""), SL("static S99_UNUSED int64_t"), SL("s99_map_len(const s99_map *m)"), SL("{"), SL(" return m ? m->count : 0;"), SL("}"), SL(""), SL("/* Drops dead entries from the order list once enough of them pile up. */"), SL("static S99_UNUSED void"), SL("s99_map_compact(s99_map *m)"), SL("{"), SL(" if (!m || m->norder < 32 || m->count * 2 > m->norder) return;"), SL(" for (int64_t i = 0; i < s99_map_loops.len; i++) {"), SL(" if (s99_map_loops.data[i] == m) return;"), SL(" }"), SL(" int64_t k = 0;"), SL(" for (int64_t i = 0; i < m->norder; i++) {"), SL(" s99_mapent *e = m->order[i];"), SL(" if (!e->alive) continue;"), SL(" e->ord = (uint32_t)k;"), SL(" m->order[k++] = e;"), SL(" }"), SL(" m->norder = k;"), SL("}"), SL(""), SL("/* A map reference is an entry of the map at a place, as a pool reference is"), SL(" an item of the pool there: base is the map (its header follows the"), SL(" variable), gen the version of the entry, index its place in the order"), SL(" when made, where it is found at once. Versions are copied with the map,"), SL(" so a restored copy keeps its references; a removed key has none, and a"), SL(" key added again a new one. */"), SL("static S99_UNUSED s99_ref"), SL("s99_ref_map(s99_map *m, s99_mapent *e)"), SL("{"), SL(" s99_ref r = {m, s99_gen_get(e), (int64_t)e->ord, S99_MAP, 0};"), SL(" return r;"), SL("}"), SL(""), SL("static S99_UNUSED void *"), SL("s99_deref_map(s99_ref r, const char *where)"), SL("{"), SL(" s99_map *m = (s99_map *)r.base;"), SL(" if (S99_LIKELY(m && (uint64_t)r.index < (uint64_t)m->norder)) {"), SL(" s99_mapent *e = m->order[r.index];"), SL(" if (S99_LIKELY(e->alive && s99_gen_get(e) == r.gen)) return (char *)S99_ENTDATA(e) + m->valoff + r.off;"), SL(" }"), SL(" for (int64_t i = 0; m && i < m->norder; i++) { /* a copy or a compaction moved it */"), SL(" s99_mapent *e = m->order[i];"), SL(" if (e->alive && s99_gen_get(e) == r.gen) return (char *)S99_ENTDATA(e) + m->valoff + r.off;"), SL(" }"), SL(" s99_deref_fail(r, where);"), SL(" return 0;"), SL("}"), SL(""), SL("/* Releasing a map frees all its entries, dead ones too: references into a"), SL(" released map see gen 0. */"), SL("static S99_UNUSED void"), SL("s99_mapent_free_all(s99_map *m)"), SL("{"), SL(" for (int64_t i = 0; i < m->nslots; i++) {"), SL(" if (m->slots[i].e)"), SL(" s99_unblock(m->slots[i].e, (size_t)((int64_t)sizeof(s99_mapent) + m->valoff + m->valsize));"), SL(" }"), SL(" s99_xfree(m->slots);"), SL(" m->slots = 0;"), SL(" m->nslots = m->used = 0;"), SL(" m->last = 0;"), SL("}"), SL(""), SL("static S99_UNUSED void"), SL("s99_map_free(s99_map *m)"), SL("{"), SL(" if (!m) return;"), SL(" s99_mapent_free_all(m);"), SL(" s99_xfree(m->order);"), SL(" s99_unblock(m, sizeof(s99_map));"), SL("}"), SL(""), SL(""), SL("/* Unions share one layout: tag 0 is nil (unless no_nil), and the payload is"), SL(" boxed. A missing box reads as the zero payload. */"), SL("typedef struct {"), SL(" int32_t tag;"), SL(" s99_box *box;"), SL("} s99_union;"), SL(""), SL("static S99_UNUSED void *"), SL("s99_payload(const void *u, int32_t tag, const void *zero, const char *where)"), SL("{"), SL(" const s99_union *x = (const s99_union *)u;"), SL(" if (S99_UNLIKELY(x->tag != tag)) s99_fail(where, x->tag == 0 ? \"this is nil\" : \"this holds a different variant\");"), SL(" return x->box ? S99_BOXDATA(x->box) : (void *)zero;"), SL("}"), SL(""), SL("/* Compact unions: union (T), one variant and nil, are just the box"), SL(" pointer; nil is null and a value always has its box. */"), SL("static S99_UNUSED S99_INLINE void *"), SL("s99_cpayload(const void *u, const char *where)"), SL("{"), SL(" s99_box *b = *(s99_box *const *)u;"), SL(" if (S99_UNLIKELY(!b)) s99_fail(where, \"this is nil\");"), SL(" return S99_BOXDATA(b);"), SL("}"), SL(""), SL("static S99_UNUSED s99_box *"), SL("s99_cpayload_box(void *u, const char *where)"), SL("{"), SL(" s99_box *b = *(s99_box **)u;"), SL(" if (!b) s99_fail(where, \"this is nil\");"), SL(" return b;"), SL("}"), SL(""), SL("static S99_UNUSED s99_box *"), SL("s99_payload_box(void *u, int32_t tag, size_t size, const char *where)"), SL("{"), SL(" s99_union *x = (s99_union *)u;"), SL(" if (x->tag != tag) s99_fail(where, x->tag == 0 ? \"this is nil\" : \"this holds a different variant\");"), SL(" if (!x->box) x->box = s99_box_new(size);"), SL(" return x->box;"), SL("}"), SL(""), SL("static S99_UNUSED s99_ref"), SL("s99_ref_box(s99_box *b)"), SL("{"), SL(" s99_ref r = {b, b->gen, 0, S99_BOX, 0};"), SL(" return r;"), SL("}"), SL(""), SL("static S99_UNUSED int64_t"), SL("s99_chk(int64_t i, int64_t n, const char *where)"), SL("{"), SL(" if (i < 0 || i >= n) s99_fail_index(where, i, n);"), SL(" return i;"), SL("}"), SL(""), SL("/* Moves the contents of src into the header dst already owns, so references"), SL(" to dst keep following it; src is released. Elements of dst were dropped. */"), SL("static S99_UNUSED void"), SL("s99_list_assign(s99_list **dst, s99_list *src)"), SL("{"), SL(" if (!*dst) {"), SL(" *dst = src;"), SL(" return;"), SL(" }"), SL(" if ((*dst)->data != (*dst)->small) s99_xfree((*dst)->data);"), SL(" if (!src) {"), SL(" (*dst)->data = 0;"), SL(" (*dst)->len = (*dst)->cap = 0;"), SL(" return;"), SL(" }"), SL(" if (src->data == src->small) {"), SL(" memcpy((*dst)->small, src->small, sizeof(src->small));"), SL(" (*dst)->data = (*dst)->small;"), SL(" } else {"), SL(" (*dst)->data = src->data;"), SL(" }"), SL(" (*dst)->len = src->len;"), SL(" (*dst)->cap = src->cap;"), SL(" src->data = 0;"), SL(" s99_list_free(src);"), SL("}"), SL(""), SL("static S99_UNUSED void"), SL("s99_pool_assign(s99_pool **dst, s99_pool *src)"), SL("{"), SL(" if (!*dst) {"), SL(" *dst = src;"), SL(" return;"), SL(" }"), SL(" s99_xfree((*dst)->slots);"), SL(" if (!src) {"), SL(" (*dst)->slots = 0;"), SL(" (*dst)->len = (*dst)->cap = 0;"), SL(" (*dst)->freehead = -1;"), SL(" return;"), SL(" }"), SL(" (*dst)->slots = src->slots;"), SL(" (*dst)->len = src->len;"), SL(" (*dst)->cap = src->cap;"), SL(" (*dst)->freehead = src->freehead;"), SL(" src->slots = 0;"), SL(" s99_unblock(src, sizeof(s99_pool));"), SL("}"), SL(""), SL("static S99_UNUSED void"), SL("s99_map_assign(s99_map **dst, s99_map *src)"), SL("{"), SL(" if (!*dst) {"), SL(" *dst = src;"), SL(" return;"), SL(" }"), SL(" s99_mapent_free_all(*dst);"), SL(" s99_xfree((*dst)->order);"), SL(" uint64_t gen = (*dst)->gen;"), SL(" if (!src) {"), SL(" (*dst)->order = 0;"), SL(" (*dst)->count = (*dst)->norder = (*dst)->cap = 0;"), SL(" return;"), SL(" }"), SL(" **dst = *src;"), SL(" (*dst)->gen = gen;"), SL(" s99_unblock(src, sizeof(s99_map));"), SL("}"), SL(""), SL("/* Types as data: what printing, literal() and Any need to know about a"), SL(" type. kind: b Bool, i signed and u unsigned integer, f Float, s String,"), SL(" l List, a fixed List, m Map, p Pool, S struct, U union, c compact union,"), SL(" r Ref, F function, t Type, o File, A Any. A struct lists its fields at"), SL(" their offsets; a union its variants by tag (type 0 carries nothing). */"), SL("typedef struct s99_type s99_type;"), SL("typedef struct s99_draw s99_draw;"), SL("typedef struct {"), SL(" const char *name;"), SL(" const s99_type *type;"), SL(" int64_t offset;"), SL(" bool named; /* a union variant with a name of its own */"), SL(" bool deflt; /* a struct field with a default */"), SL("} s99_field;"), SL(""), SL("struct s99_type {"), SL(" const char *name; /* as written */"), SL(" int64_t size;"), SL(" char kind;"), SL(" bool nonil;"), SL(" bool refs; /* holds references, which a copy moves over to itself */"), SL(" int64_t count; /* a fixed length, or the number of fields */"), SL(" const s99_type *elem, *key;"), SL(" const s99_field *fields;"), SL(" void (*copy)(void *dst, const void *src); /* 0: plain bytes */"), SL(" void (*drop)(void *v); /* 0: nothing to release */"), SL(" void (*print)(s99_stream *o, const void *v); /* a print overload */"), SL(" int32_t id;"), SL(" void (*init)(void *v); /* the starting value, with defaults */"), SL(" void (*arb)(s99_draw *d, void *v); /* an arbitrary overload */"), SL(" bool (*valid)(const void *v); /* a valid overload */"), SL(" bool (*eq)(const void *a, const void *b); /* an == overload: every comparison of the type uses it */"), SL("};"), SL(""), SL("static S99_UNUSED const uint64_t s99_zeros[64] = {0};"), SL(""), SL("/* The payload of union value v, or the zero payload; and its tag. */"), SL("static S99_UNUSED const void *"), SL("s99_variant(const s99_type *t, const void *v, int32_t *tag)"), SL("{"), SL(" s99_box *box = t->kind == 'c' ? *(s99_box *const *)v : ((const s99_union *)v)->box;"), SL(" *tag = t->kind == 'c' ? box != 0 : ((const s99_union *)v)->tag;"), SL(" return box ? S99_BOXDATA(box) : (const void *)s99_zeros;"), SL("}"), SL(""), SL("static S99_UNUSED int64_t"), SL("s99_get_int(const s99_type *t, const void *v)"), SL("{"), SL(" switch (t->size) {"), SL(" case 1: return t->kind == 'i' ? *(const int8_t *)v : (int64_t) * (const uint8_t *)v;"), SL(" case 2: return t->kind == 'i' ? *(const int16_t *)v : (int64_t) * (const uint16_t *)v;"), SL(" case 4: return t->kind == 'i' ? *(const int32_t *)v : (int64_t) * (const uint32_t *)v;"), SL(" default: return *(const int64_t *)v;"), SL(" }"), SL("}"), SL(""), SL(""), SL("typedef struct {"), SL(" const s99_type *type;"), SL(" s99_box *box;"), SL("} s99_any;"), SL(""), SL("static S99_UNUSED void"), SL("s99_any_copy(s99_any *dst, const s99_any *src)"), SL("{"), SL(" dst->type = src->type;"), SL(" dst->box = 0;"), SL(" if (!src->type) return;"), SL(" dst->box = s99_box_new((size_t)src->type->size);"), SL(" if (src->type->copy) src->type->copy(S99_BOXDATA(dst->box), S99_BOXDATA(src->box));"), SL(" else memcpy(S99_BOXDATA(dst->box), S99_BOXDATA(src->box), (size_t)src->type->size);"), SL("}"), SL(""), SL("static S99_UNUSED void"), SL("s99_any_drop(s99_any *a)"), SL("{"), SL(" if (!a->type) return;"), SL(" if (a->type->drop) a->type->drop(S99_BOXDATA(a->box));"), SL(" s99_box_free(a->box);"), SL(" a->type = 0;"), SL(" a->box = 0;"), SL("}"), SL(""), SL(""), SL(""), SL("/* Function values: code plus the environment it captured. */"), SL("/* A function value. owns: env is a counted environment this value holds a"), SL(" count of (a closure that outlives its scope); otherwise env lives on the"), SL(" stack of the scope the closure cannot leave, or is null. */"), SL("typedef struct {"), SL(" void (*fn)(void);"), SL(" void *env;"), SL(" int64_t owns;"), SL("} s99_fn;"), SL(""), SL("static S99_UNUSED void"), SL("s99_set_int(const s99_type *t, void *v, int64_t n)"), SL("{"), SL(" switch (t->size) {"), SL(" case 1: *(uint8_t *)v = (uint8_t)n; break;"), SL(" case 2: *(uint16_t *)v = (uint16_t)n; break;"), SL(" case 4: *(uint32_t *)v = (uint32_t)n; break;"), SL(" default: *(int64_t *)v = n; break;"), SL(" }"), SL("}"), SL(""), SL("/* After a copy: references that led into the original lead into the copy. */"), SL("static S99_UNUSED void"), SL("s99_fix(const s99_type *t, void *v)"), SL("{"), SL(" if (!t->refs) return;"), SL(" switch (t->kind) {"), SL(" case 'r': s99_ref_fix((s99_ref *)v); return;"), SL(" case 'S':"), SL(" for (int64_t i = 0; i < t->count; i++) s99_fix(t->fields[i].type, (char *)v + t->fields[i].offset);"), SL(" return;"), SL(" case 'U':"), SL(" case 'c': {"), SL(" int32_t tag = 0;"), SL(" void *p = (void *)s99_variant(t, v, &tag);"), SL(" const s99_field *f = t->nonil || tag ? t->fields + (t->nonil ? tag : tag - 1) : 0;"), SL(" if (f && f->type && p != (void *)s99_zeros) s99_fix(f->type, p);"), SL(" return;"), SL(" }"), SL(" case 'a':"), SL(" for (int64_t i = 0; i < t->count; i++) s99_fix(t->elem, (char *)v + i * t->elem->size);"), SL(" return;"), SL(" case 'l': {"), SL(" s99_list *l = *(s99_list **)v;"), SL(" for (int64_t i = 0; i < s99_list_len(l); i++) s99_fix(t->elem, l->data + i * t->elem->size);"), SL(" return;"), SL(" }"), SL(" case 'p': {"), SL(" s99_pool *pl = *(s99_pool **)v;"), SL(" for (int64_t i = 0; pl && i < pl->len; i++) {"), SL(" s99_slot *sl = (s99_slot *)(pl->slots + i * pl->slotsize);"), SL(" if (sl->version) s99_fix(t->elem, S99_SLOTDATA(sl));"), SL(" }"), SL(" return;"), SL(" }"), SL(" case 'm': {"), SL(" s99_map *m = *(s99_map **)v;"), SL(" for (int64_t i = 0; m && i < m->norder; i++)"), SL(" if (m->order[i]->alive) s99_fix(t->elem, s99_map_val(m, m->order[i]));"), SL(" return;"), SL(" }"), SL(" default: return;"), SL(" }"), SL("}"), SL(""), SL("/* a == b for values of type t */"), SL("static S99_UNUSED bool"), SL("s99_equal(const s99_type *t, const void *a, const void *b)"), SL("{"), SL(" if (t->eq) return t->eq(a, b);"), SL(" switch (t->kind) {"), SL(" case 'b': return *(const bool *)a == *(const bool *)b;"), SL(" case 'i':"), SL(" case 'u': return s99_get_int(t, a) == s99_get_int(t, b);"), SL(" case 'f': return *(const double *)a == *(const double *)b;"), SL(" case 's': return s99_str_eq((const s99_str *)a, (const s99_str *)b);"), SL(" case 'r': return s99_ref_eq((const s99_ref *)a, (const s99_ref *)b);"), SL(" case 'p': { /* the live items, in slot order */"), SL(" const s99_pool *pa = *(s99_pool *const *)a, *pb = *(s99_pool *const *)b;"), SL(" int64_t i = 0, j = 0, na = pa ? pa->len : 0, nb = pb ? pb->len : 0;"), SL(" for (;;) {"), SL(" while (i < na && !((s99_slot *)(pa->slots + i * pa->slotsize))->version) i++;"), SL(" while (j < nb && !((s99_slot *)(pb->slots + j * pb->slotsize))->version) j++;"), SL(" if (i == na || j == nb) return i == na && j == nb;"), SL(" if (!s99_equal(t->elem, S99_SLOTDATA((s99_slot *)(pa->slots + i * pa->slotsize)),"), SL(" S99_SLOTDATA((s99_slot *)(pb->slots + j * pb->slotsize))))"), SL(" return false;"), SL(" i++, j++;"), SL(" }"), SL(" }"), SL(" case 't':"), SL(" case 'o': return *(void *const *)a == *(void *const *)b;"), SL(" case 'F': return ((const s99_fn *)a)->fn == ((const s99_fn *)b)->fn && ((const s99_fn *)a)->env == ((const s99_fn *)b)->env;"), SL(" case 'S':"), SL(" for (int64_t i = 0; i < t->count; i++) {"), SL(" const s99_field *f = t->fields + i;"), SL(" if (!s99_equal(f->type, (const char *)a + f->offset, (const char *)b + f->offset)) return false;"), SL(" }"), SL(" return true;"), SL(" case 'U':"), SL(" case 'c': {"), SL(" int32_t ta = 0, tb = 0;"), SL(" const void *pa = s99_variant(t, a, &ta), *pb = s99_variant(t, b, &tb);"), SL(" if (ta != tb) return false;"), SL(" const s99_field *f = t->nonil || ta ? t->fields + (t->nonil ? ta : ta - 1) : 0;"), SL(" return !f || !f->type || s99_equal(f->type, pa, pb);"), SL(" }"), SL(" case 'a':"), SL(" case 'l': {"), SL(" const s99_list *la = t->kind == 'l' ? *(s99_list *const *)a : 0, *lb = t->kind == 'l' ? *(s99_list *const *)b : 0;"), SL(" int64_t n = t->kind == 'a' ? t->count : s99_list_len(la);"), SL(" if (t->kind == 'l' && n != s99_list_len(lb)) return false;"), SL(" const char *da = t->kind == 'a' ? (const char *)a : la ? (const char *)la->data : 0;"), SL(" const char *db = t->kind == 'a' ? (const char *)b : lb ? (const char *)lb->data : 0;"), SL(" for (int64_t i = 0; i < n; i++)"), SL(" if (!s99_equal(t->elem, da + i * t->elem->size, db + i * t->elem->size)) return false;"), SL(" return true;"), SL(" }"), SL(" case 'm': {"), SL(" s99_map *ma = *(s99_map *const *)a, *mb = *(s99_map *const *)b;"), SL(" if (s99_map_len(ma) != s99_map_len(mb)) return false;"), SL(" for (int64_t i = 0; ma && i < ma->norder; i++) {"), SL(" s99_mapent *e = ma->order[i];"), SL(" if (!e->alive) continue;"), SL(" s99_mapent *f = s99_map_find(mb, s99_map_key(e), false, 0);"), SL(" if (!f || !s99_equal(t->elem, s99_map_val(ma, e), s99_map_val(mb, f))) return false;"), SL(" }"), SL(" return true;"), SL(" }"), SL(" case 'A': {"), SL(" const s99_any *x = (const s99_any *)a, *y = (const s99_any *)b;"), SL(" return x->type == y->type && (!x->type || s99_equal(x->type, S99_BOXDATA(x->box), S99_BOXDATA(y->box)));"), SL(" }"), SL(" default: return true;"), SL(" }"), SL("}"), SL(""), SL("enum { S99_PRINT, S99_INSIDE, S99_LITERAL, S99_SHOW }; /* SHOW: literal where it can be */"), SL(""), SL("/* print(v), print of a value inside another (strings quoted), or literal(v):"), SL(" the Star99 code that builds it, with its type names. */"), SL("static S99_UNUSED void"), SL("s99_show(s99_stream *o, const s99_type *t, const void *v, int mode)"), SL("{"), SL(" bool lit = mode >= S99_LITERAL, strict = mode == S99_LITERAL;"), SL(" int inner = lit ? mode : S99_INSIDE;"), SL(" if (t->print && !lit) {"), SL(" t->print(o, v);"), SL(" return;"), SL(" }"), SL(" switch (t->kind) {"), SL(" case 's':"), SL(" if (mode == S99_PRINT) s99_put_str(o, (const s99_str *)v);"), SL(" else s99_put_quoted(o, (const s99_str *)v);"), SL(" return;"), SL(" case 'b': s99_puts(o, *(const bool *)v ? \"true\" : \"false\"); return;"), SL(" case 'i':"), SL(" case 'u': {"), SL(" int64_t n = s99_get_int(t, v);"), SL(" bool plain = !lit || !strcmp(t->name, \"Int\");"), SL(" if (lit && !strcmp(t->name, \"U8\") && n >= 32 && n < 127) { /* 'a', '\\\\', '\\'' */"), SL(" char q[4] = {'\\'', '\\\\', (char)n, '\\''};"), SL(" bool esc = n == '\\'' || n == '\\\\';"), SL(" s99_put(o, esc ? q : (char[]){'\\'', (char)n, '\\''}, esc ? 4 : 3);"), SL(" return;"), SL(" }"), SL(" if (!plain) s99_puts(o, t->name), s99_puts(o, \"(\");"), SL(" if (t->kind == 'i') s99_put_int(o, n);"), SL(" else s99_put_uint(o, t->size == 8 ? *(const uint64_t *)v : (uint64_t)n);"), SL(" if (!plain) s99_puts(o, \")\");"), SL(" return;"), SL(" }"), SL(" case 'f': {"), SL(" double d = *(const double *)v;"), SL(" if (lit && d != d) s99_puts(o, \"(0.0 / 0.0)\"); /* code for the values without literals */"), SL(" else if (lit && (d > 1.7976931348623157e308 || d < -1.7976931348623157e308))"), SL(" s99_puts(o, d > 0 ? \"(1.0 / 0.0)\" : \"(-1.0 / 0.0)\");"), SL(" else s99_put_float(o, d);"), SL(" return;"), SL(" }"), SL(" case 'S':"), SL(" s99_puts(o, lit ? t->name : \"\");"), SL(" s99_puts(o, \"(\");"), SL(" for (int64_t i = 0; i < t->count; i++) {"), SL(" const s99_field *f = t->fields + i;"), SL(" if (i) s99_puts(o, \", \");"), SL(" s99_puts(o, f->name);"), SL(" s99_puts(o, \": \");"), SL(" s99_show(o, f->type, (const char *)v + f->offset, inner);"), SL(" }"), SL(" s99_puts(o, \")\");"), SL(" return;"), SL(" case 'U':"), SL(" case 'c': {"), SL(" int32_t tag = 0;"), SL(" const void *p = s99_variant(t, v, &tag);"), SL(" /* a union without a name of its own: what it holds, which converts"), SL(" where it is used (union (T).T(x) is not code) */"), SL(" bool named = lit && strncmp(t->name, \"union (\", 7) && strncmp(t->name, \"no_nil(\", 7);"), SL(" if (!t->nonil && !tag) {"), SL(" s99_puts(o, named ? t->name : \"\");"), SL(" s99_puts(o, named ? \"(nil)\" : \"nil\");"), SL(" return;"), SL(" }"), SL(" const s99_field *f = t->fields + (t->nonil ? tag : tag - 1);"), SL(" if (named) s99_puts(o, t->name);"), SL(" bool label = named || f->named || !f->type;"), SL(" if (label) s99_puts(o, \".\"), s99_puts(o, f->name);"), SL(" if (!f->type) return;"), SL(" if (label) s99_puts(o, \"(\");"), SL(" s99_show(o, f->type, p, inner);"), SL(" if (label) s99_puts(o, \")\");"), SL(" return;"), SL(" }"), SL(" case 'l':"), SL(" case 'a': {"), SL(" const s99_list *l = *(s99_list *const *)v;"), SL(" int64_t n = t->kind == 'a' ? t->count : s99_list_len(l);"), SL(" const char *at = t->kind == 'a' ? (const char *)v : l ? (const char *)l->data : 0;"), SL(" if (lit && !n) {"), SL(" s99_puts(o, t->name);"), SL(" s99_puts(o, \"()\");"), SL(" return;"), SL(" }"), SL(" s99_puts(o, \"[\");"), SL(" for (int64_t i = 0; i < n; i++) {"), SL(" if (i) s99_puts(o, \", \");"), SL(" s99_show(o, t->elem, at + i * t->elem->size, inner);"), SL(" }"), SL(" s99_puts(o, \"]\");"), SL(" return;"), SL(" }"), SL(" case 'm': {"), SL(" s99_map *m = *(s99_map *const *)v;"), SL(" if (lit && !s99_map_len(m)) {"), SL(" s99_puts(o, t->name);"), SL(" s99_puts(o, \"()\");"), SL(" return;"), SL(" }"), SL(" s99_puts(o, \"[\");"), SL(" int64_t n = 0;"), SL(" for (int64_t i = 0; m && i < m->norder; i++) {"), SL(" s99_mapent *e = m->order[i];"), SL(" if (!e->alive) continue;"), SL(" if (n++) s99_puts(o, \", \");"), SL(" s99_show(o, t->key, s99_map_key(e), inner);"), SL(" s99_puts(o, \": \");"), SL(" s99_show(o, t->elem, s99_map_val(m, e), inner);"), SL(" }"), SL(" s99_puts(o, n ? \"]\" : \":]\");"), SL(" return;"), SL(" }"), SL(" case 'p': {"), SL(" if (strict) break;"), SL(" const s99_pool *pl = *(s99_pool *const *)v;"), SL(" s99_puts(o, \"Pool(\");"), SL(" int64_t n = 0;"), SL(" for (int64_t i = 0; pl && i < pl->len; i++) {"), SL(" s99_slot *sl = (s99_slot *)(pl->slots + i * pl->slotsize);"), SL(" if (!sl->version) continue;"), SL(" if (n++) s99_puts(o, \", \");"), SL(" s99_show(o, t->elem, S99_SLOTDATA(sl), inner);"), SL(" }"), SL(" s99_puts(o, \")\");"), SL(" return;"), SL(" }"), SL(" case 'o':"), SL(" if (strict) break;"), SL(" s99_puts(o, *(s99_stream *const *)v == &s99_stderr_stream ? \"stderr\" : \"stdout\");"), SL(" return;"), SL(" case 't': /* a Type as code is its name */"), SL(" s99_puts(o, *(const s99_type *const *)v ? (*(const s99_type *const *)v)->name : \"nil\");"), SL(" return;"), SL(" case 'r':"), SL(" if (strict) break;"), SL(" s99_puts(o, ((const s99_ref *)v)->kind ? \"\" : \"nil\");"), SL(" return;"), SL(" case 'F':"), SL(" if (strict) break;"), SL(" s99_puts(o, ((const s99_fn *)v)->fn ? \"\" : \"nil\");"), SL(" return;"), SL(" case 'A': {"), SL(" const s99_any *a = (const s99_any *)v;"), SL(" if (!a->type) s99_puts(o, \"nil\");"), SL(" else s99_show(o, a->type, S99_BOXDATA(a->box), mode); /* \"x\" in a list stays quoted */"), SL(" return;"), SL(" }"), SL(" default: break;"), SL(" }"), SL(" if (strict) {"), SL(" char msg[256] = {0};"), SL(" snprintf(msg, sizeof(msg), \"a %s cannot be written as a literal\", t->name);"), SL(" s99_fail(\"star99\", msg);"), SL(" }"), SL("}"), SL(""), SL("static S99_UNUSED void"), SL("s99_check_fn(s99_fn f, const char *where)"), SL("{"), SL(" if (!f.fn) s99_fail(where, \"this function value is nil\");"), SL("}"), SL(""), SL("/* Fuzzing (-DSTAR99_FUZZ). A test input is a sequence of choices: every"), SL(" value a target needs is built from it by a builder the compiler derives"), SL(" for its type. Choices past the end are zero, and zero is always the"), SL(" simplest choice, so every input is valid, all zeros builds the zero"), SL(" values, and shorter or smaller inputs build smaller values. In a fuzzed"), SL(" main, read_file(\"-\") is the input. exit() and assume(false) end the"), SL(" input instead of the process. */"), SL("#ifdef STAR99_FUZZ"), SL("#include "), SL("static S99_UNUSED jmp_buf s99_fuzz_exit;"), SL("static S99_UNUSED s99_str s99_fuzz_input;"), SL("#endif"), SL(""), SL("struct s99_draw {"), SL(" const uint8_t *p;"), SL(" int64_t n, i;"), SL(" uint64_t swarm; /* union variants switched off for this input */"), SL(" int depth; /* nesting of values being built */"), SL("};"), SL(""), SL("/* The words and numbers of the program: strings and numbers it compares"), SL(" against. Builders mix them in. Filled in by the generated code, when"), SL(" fuzzing only. */"), SL("static S99_UNUSED const char *const *s99_words = 0;"), SL("static S99_UNUSED const int64_t *s99_word_lens = 0;"), SL("static S99_UNUSED int64_t s99_nwords = 0;"), SL("static S99_UNUSED const int64_t *s99_numbers = 0;"), SL("static S99_UNUSED int64_t s99_nnumbers = 0;"), SL(""), SL("static S99_UNUSED uint8_t"), SL("s99_draw_byte(s99_draw *d)"), SL("{"), SL(" return d->i < d->n ? d->p[d->i++] : 0;"), SL("}"), SL(""), SL("static S99_UNUSED uint64_t"), SL("s99_draw_bits(s99_draw *d, int bytes)"), SL("{"), SL(" uint64_t v = 0;"), SL(" for (int k = 0; k < bytes; k++) v |= (uint64_t)s99_draw_byte(d) << (8 * k);"), SL(" return v;"), SL("}"), SL(""), SL("/* A choice in [0, n). */"), SL("static S99_UNUSED int64_t"), SL("s99_draw_below(s99_draw *d, int64_t n)"), SL("{"), SL(" if (n <= 1) return 0;"), SL(" uint64_t v = n <= 256 ? s99_draw_byte(d) : n <= 65536 ? s99_draw_bits(d, 2) : s99_draw_bits(d, 8);"), SL(" return (int64_t)(v % (uint64_t)n);"), SL("}"), SL(""), SL("/* Whether a list goes on: zero stops it, and so does the end of input. */"), SL("static S99_UNUSED bool"), SL("s99_draw_more(s99_draw *d)"), SL("{"), SL(" return d->i < d->n && (s99_draw_byte(d) & 3) != 0;"), SL("}"), SL(""), SL("/* An integer in [lo, hi]: small ones first, then edges, the program's own"), SL(" numbers, and any value. */"), SL("static S99_UNUSED int64_t"), SL("s99_draw_int(s99_draw *d, int64_t lo, int64_t hi)"), SL("{"), SL(" uint8_t k = s99_draw_byte(d);"), SL(" int64_t v = 0;"), SL(" uint64_t span = (uint64_t)hi - (uint64_t)lo;"), SL(" switch (k % 8) {"), SL(" case 0: v = 0; break;"), SL(" case 1: v = (int64_t)(s99_draw_byte(d) % 17) - 8; break;"), SL(" case 2: {"), SL(" int64_t edges[] = {lo, hi, lo + 1, hi - 1, -1, 1, 255, 256, 65535, 65536, INT32_MAX, INT32_MIN};"), SL(" v = edges[s99_draw_below(d, (int64_t)(sizeof(edges) / sizeof(*edges)))];"), SL(" break;"), SL(" }"), SL(" case 3:"), SL(" if (s99_nnumbers) {"), SL(" v = s99_numbers[s99_draw_below(d, s99_nnumbers)] + (int64_t)(s99_draw_byte(d) % 3) - 1;"), SL(" break;"), SL(" }"), SL(" /* fallthrough */"), SL(" default: {"), SL(" uint64_t r = s99_draw_bits(d, 8);"), SL(" v = (int64_t)((uint64_t)lo + (span == UINT64_MAX ? r : r % (span + 1)));"), SL(" }"), SL(" }"), SL(" if (v < lo || v > hi) v = (int64_t)((uint64_t)lo + (uint64_t)v % (span == UINT64_MAX ? UINT64_MAX : span + 1));"), SL(" if (v < lo || v > hi) v = lo;"), SL(" return v;"), SL("}"), SL(""), SL("static S99_UNUSED uint64_t"), SL("s99_draw_uint(s99_draw *d, uint64_t hi)"), SL("{"), SL(" uint8_t k = s99_draw_byte(d);"), SL(" uint64_t v = 0;"), SL(" switch (k % 8) {"), SL(" case 0: v = 0; break;"), SL(" case 1: v = s99_draw_byte(d) % 9; break;"), SL(" case 2: {"), SL(" uint64_t edges[] = {hi, hi - 1, 1, 255, 256, 65535, 65536, 0x7fffffff, 0x80000000u};"), SL(" v = edges[s99_draw_below(d, (int64_t)(sizeof(edges) / sizeof(*edges)))];"), SL(" break;"), SL(" }"), SL(" case 3:"), SL(" if (s99_nnumbers) {"), SL(" v = (uint64_t)s99_numbers[s99_draw_below(d, s99_nnumbers)];"), SL(" break;"), SL(" }"), SL(" /* fallthrough */"), SL(" default: v = s99_draw_bits(d, 8);"), SL(" }"), SL(" return hi == UINT64_MAX ? v : v % (hi + 1);"), SL("}"), SL(""), SL("static S99_UNUSED double"), SL("s99_draw_float(s99_draw *d)"), SL("{"), SL(" uint8_t k = s99_draw_byte(d);"), SL(" switch (k % 8) {"), SL(" case 0: return 0.0;"), SL(" case 1: return (double)((int)(s99_draw_byte(d) % 21) - 10) / 2;"), SL(" case 2: {"), SL(" double edges[] = {-0.0, 1e-308, 1e308, 0.1, 1.0 / 3, 1e16, -1e16, INFINITY, -INFINITY, NAN};"), SL(" return edges[s99_draw_below(d, (int64_t)(sizeof(edges) / sizeof(*edges)))];"), SL(" }"), SL(" default: {"), SL(" uint64_t bits = s99_draw_bits(d, 8);"), SL(" double v = 0;"), SL(" memcpy(&v, &bits, sizeof(v));"), SL(" return v;"), SL(" }"), SL(" }"), SL("}"), SL(""), SL("/* Text: bytes, and now and then a word of the program. */"), SL("static S99_UNUSED void"), SL("s99_draw_text(s99_draw *d, s99_str *s)"), SL("{"), SL(" *s = 0;"), SL(" while (s99_draw_more(d) && s99_str_len(*s) < 4096) {"), SL(" uint8_t k = s99_draw_byte(d);"), SL(" if (k % 4 == 1 && s99_nwords) {"), SL(" int64_t w = s99_draw_below(d, s99_nwords);"), SL(" s99_str_append(s, s99_words[w], s99_word_lens[w]);"), SL(" } else {"), SL(" uint8_t c = k % 4 == 2 ? (uint8_t)(' ' + s99_draw_byte(d) % 95) : s99_draw_byte(d);"), SL(" s99_str_append(s, &c, 1);"), SL(" }"), SL(" }"), SL(" if (!*s) *s = s99_bytes(\"\", 0);"), SL("}"), SL(""), SL("/* Which variant of a union, or nil (-1): deep values and switched-off"), SL(" variants (swarm testing) fall back to the simplest. */"), SL("static S99_UNUSED int"), SL("s99_draw_variant(s99_draw *d, int typeid, int n, bool nilable)"), SL("{"), SL(" int v = (int)s99_draw_below(d, n + nilable) - nilable;"), SL(" if (d->depth > 24) return nilable ? -1 : 0;"), SL(" if (v >= 0 && (d->swarm >> ((typeid * 7 + v) % 64)) & 1) return nilable ? -1 : 0;"), SL(" return v;"), SL("}"), SL(""), SL("static S99_NORETURN S99_UNUSED void"), SL("s99_reject(void)"), SL("{"), SL("#ifdef STAR99_FUZZ"), SL(" longjmp(s99_fuzz_exit, 1);"), SL("#else"), SL(" exit(0);"), SL("#endif"), SL("}"), SL(""), SL("/* assume(c): an input that breaks c is not a test case. */"), SL("static S99_UNUSED void"), SL("s99_assume(bool c)"), SL("{"), SL(" if (!c) s99_reject();"), SL("}"), SL(""), SL("/* A value from choices, into v, which holds nothing yet: zero choices give"), SL(" the zero value, and every input gives some value. arbitrary(d) overloads"), SL(" replace this, and valid(x) keeps only the values that pass. */"), SL("static S99_UNUSED void"), SL("s99_arb(s99_draw *d, const s99_type *t, void *v)"), SL("{"), SL(" if (t->arb) {"), SL(" t->arb(d, v);"), SL(" } else {"), SL(" int bits = (int)t->size * 8;"), SL(" switch (t->kind) {"), SL(" case 'b': *(bool *)v = s99_draw_byte(d) & 1; break;"), SL(" case 'i': {"), SL(" int64_t hi = bits == 64 ? INT64_MAX : ((int64_t)1 << (bits - 1)) - 1;"), SL(" s99_set_int(t, v, s99_draw_int(d, -hi - 1, hi));"), SL(" break;"), SL(" }"), SL(" case 'u':"), SL(" if (bits == 64) *(uint64_t *)v = s99_draw_uint(d, UINT64_MAX);"), SL(" else s99_set_int(t, v, (int64_t)s99_draw_uint(d, ((uint64_t)1 << bits) - 1));"), SL(" break;"), SL(" case 'f': *(double *)v = s99_draw_float(d); break;"), SL(" case 's': s99_draw_text(d, (s99_str *)v); break;"), SL(" case 'l':"), SL(" *(s99_list **)v = 0;"), SL(" d->depth++;"), SL(" while (s99_draw_more(d) && s99_list_len(*(s99_list **)v) < 1024)"), SL(" s99_arb(d, t->elem, s99_list_push((s99_list **)v, t->elem->size));"), SL(" d->depth--;"), SL(" break;"), SL(" case 'a':"), SL(" for (int64_t i = 0; i < t->count; i++) s99_arb(d, t->elem, (char *)v + i * t->elem->size);"), SL(" break;"), SL(" case 'm': {"), SL(" s99_map **m = (s99_map **)v;"), SL(" int kind = t->key->kind == 's' ? 1 : t->key->kind == 'b' ? 2 : 0;"), SL(" *m = 0;"), SL(" d->depth++;"), SL(" while (s99_draw_more(d) && s99_map_len(*m) < 1024) {"), SL(" uint64_t k[4] = {0}; /* keys are Int, String or Bool: 8 bytes at most */"), SL(" s99_arb(d, t->key, k);"), SL(" bool created = 0;"), SL(" s99_mapent *e = s99_map_find(s99_map_hdr(m, t->key->size, t->elem->size, kind), k,"), SL(" true, &created);"), SL(" if (created) {"), SL(" memcpy(s99_map_key(e), k, (size_t)t->key->size);"), SL(" } else {"), SL(" if (t->key->drop) t->key->drop(k);"), SL(" if (t->elem->drop) t->elem->drop(s99_map_val(*m, e));"), SL(" }"), SL(" s99_arb(d, t->elem, s99_map_val(*m, e));"), SL(" }"), SL(" d->depth--;"), SL(" break;"), SL(" }"), SL(" case 'p': {"), SL(" s99_pool **pl = (s99_pool **)v;"), SL(" *pl = 0;"), SL(" d->depth++;"), SL(" while (s99_draw_more(d) && s99_pool_count(*pl) < 1024) {"), SL(" void *item = 0;"), SL(" s99_pool_add(pl, t->elem->size, &item);"), SL(" s99_arb(d, t->elem, item);"), SL(" }"), SL(" d->depth--;"), SL(" break;"), SL(" }"), SL(" case 'S':"), SL(" if (t->init) t->init(v);"), SL(" else memset(v, 0, (size_t)t->size);"), SL(" d->depth++;"), SL(" for (int64_t i = 0; i < t->count; i++) {"), SL(" const s99_field *f = t->fields + i;"), SL(" void *fv = (char *)v + f->offset;"), SL(" if (!f->deflt) {"), SL(" s99_arb(d, f->type, fv);"), SL(" } else if (s99_draw_more(d)) { /* a field with a default keeps it on a zero choice */"), SL(" if (f->type->drop) f->type->drop(fv);"), SL(" s99_arb(d, f->type, fv);"), SL(" }"), SL(" }"), SL(" d->depth--;"), SL(" break;"), SL(" case 'U':"), SL(" case 'c': {"), SL(" memset(v, 0, (size_t)t->size);"), SL(" d->depth++;"), SL(" int i = s99_draw_variant(d, t->id, (int)t->count, !t->nonil);"), SL(" if (i >= 0) {"), SL(" const s99_field *f = t->fields + i;"), SL(" s99_box *box = 0;"), SL(" if (f->type) {"), SL(" box = s99_box_new((size_t)f->type->size);"), SL(" s99_arb(d, f->type, S99_BOXDATA(box));"), SL(" }"), SL(" if (t->kind == 'c') {"), SL(" *(s99_box **)v = box;"), SL(" } else {"), SL(" ((s99_union *)v)->tag = t->nonil ? i : i + 1;"), SL(" ((s99_union *)v)->box = box;"), SL(" }"), SL(" }"), SL(" d->depth--;"), SL(" break;"), SL(" }"), SL(" default: memset(v, 0, (size_t)t->size); break; /* references, functions, files, Types, Any */"), SL(" }"), SL(" }"), SL(" if (t->valid) s99_assume(t->valid(v));"), SL("}"), SL(""), SL("static S99_NORETURN S99_UNUSED void"), SL("s99_exit(int code)"), SL("{"), SL(" fflush(stdout);"), SL("#ifdef STAR99_FUZZ"), SL(" (void)code;"), SL(" longjmp(s99_fuzz_exit, 1);"), SL("#else"), SL(" exit(code);"), SL("#endif"), SL("}"), SL(""), SL("/* Files and processes. The path \"-\" is standard input. */"), SL("static S99_UNUSED s99_str"), SL("s99_read_file(s99_str path, const char *where)"), SL("{"), SL(" bool in = s99_str_is(&path, \"-\", 1);"), SL("#ifdef STAR99_FUZZ"), SL(" if (in) return s99_bytes(s99_str_data(s99_fuzz_input), s99_str_len(s99_fuzz_input));"), SL("#endif"), SL(" char *p = s99_cstr(path);"), SL(" FILE *f = in ? stdin : fopen(p, \"rb\");"), SL(" if (!f) {"), SL(" char msg[512] = {0};"), SL(" snprintf(msg, sizeof(msg), \"cannot read %s\", p);"), SL(" free(p);"), SL(" s99_fail(where, msg);"), SL(" }"), SL(" free(p);"), SL(" uint8_t tmp[4096] = {0};"), SL(" size_t n = 0;"), SL(" s99_str r = 0;"), SL(" while ((n = fread(tmp, 1, sizeof(tmp), f)) > 0) s99_str_append(&r, tmp, (int64_t)n);"), SL(" if (!in) fclose(f);"), SL(" return r ? r : s99_bytes(\"\", 0);"), SL("}"), SL(""), SL("static S99_UNUSED void"), SL("s99_write_file(s99_str path, s99_str data, const char *where)"), SL("{"), SL(" char *p = s99_cstr(path);"), SL(" FILE *f = fopen(p, \"wb\");"), SL(" size_t n = (size_t)s99_str_len(data);"), SL(" if (!f || fwrite(s99_str_data(data), 1, n, f) != n || fclose(f)) {"), SL(" char msg[512] = {0};"), SL(" snprintf(msg, sizeof(msg), \"cannot write %s\", p);"), SL(" free(p);"), SL(" s99_fail(where, msg);"), SL(" }"), SL(" free(p);"), SL("}"), SL(""), SL("static S99_UNUSED bool"), SL("s99_file_exists(s99_str path)"), SL("{"), SL(" char *p = s99_cstr(path);"), SL(" FILE *f = fopen(p, \"rb\");"), SL(" free(p);"), SL(" if (f) fclose(f);"), SL(" return f != 0;"), SL("}"), SL(""), SL("static S99_UNUSED int64_t"), SL("s99_run(s99_str cmd)"), SL("{"), SL(" char *c = s99_cstr(cmd);"), SL(" fflush(stdout);"), SL(" int r = system(c);"), SL(" free(c);"), SL(" if (r == -1) return -1;"), SL("#if defined(__unix__) || defined(__APPLE__)"), SL(" if (r & 0x7f) return 128 + (r & 0x7f);"), SL(" return (r >> 8) & 0xff;"), SL("#else"), SL(" return r;"), SL("#endif"), SL("}"), SL(""), SL("/* What a shell command prints, its errors too, read through a pipe. */"), SL("static S99_UNUSED s99_str"), SL("s99_shell(s99_str cmd)"), SL("{"), SL(" char *c = s99_cstr(cmd);"), SL(" char *line = (char *)s99_alloc(strlen(c) + 8);"), SL(" sprintf(line, \"(%s) 2>&1\", c);"), SL(" free(c);"), SL(" fflush(stdout);"), SL(" s99_str r = 0;"), SL("#if defined(__unix__) || defined(__APPLE__)"), SL(" FILE *p = popen(line, \"r\");"), SL(" if (p) {"), SL(" uint8_t tmp[4096] = {0};"), SL(" size_t n = 0;"), SL(" while ((n = fread(tmp, 1, sizeof(tmp), p)) > 0) s99_str_append(&r, tmp, (int64_t)n);"), SL(" pclose(p);"), SL(" }"), SL("#endif"), SL(" free(line);"), SL(" return r ? r : s99_bytes(\"\", 0);"), SL("}"), SL(""), SL("static S99_UNUSED s99_str"), SL("s99_getenv(s99_str name)"), SL("{"), SL(" char *n = s99_cstr(name);"), SL(" const char *v = getenv(n);"), SL(" free(n);"), SL(" if (!v) return s99_lit(\"\", 0);"), SL(" return s99_bytes((const uint8_t *)v, (int64_t)strlen(v));"), SL("}"), SL(""), SL("static S99_UNUSED s99_list *"), SL("s99_arguments(void)"), SL("{"), SL(" s99_list *l = 0;"), SL(" for (int i = 1; i < s99_argc; i++) {"), SL(" s99_str *s = (s99_str *)s99_list_push(&l, sizeof(s99_str));"), SL(" *s = s99_bytes((const uint8_t *)s99_argv[i], (int64_t)strlen(s99_argv[i]));"), SL(" }"), SL(" return l;"), SL("}"), SL(""), SL("static S99_UNUSED void"), SL("s99_init(int argc, char **argv)"), SL("{"), SL(" s99_argc = argc;"), SL(" s99_argv = argv;"), SL(" s99_stdout_stream.f = stdout;"), SL(" s99_stderr_stream.f = stderr;"), SL(" s99_out = &s99_stdout_stream;"), SL("}"), SL(""), SL("/* Values crossing between the compiler and the metaprogram, as text:"), SL(" i; f; b0 b1 s: u; l; */"), SL("typedef struct {"), SL(" FILE *f; /* or, when 0, text in memory (templates) */"), SL(" const uint8_t *mem;"), SL(" int64_t len, pos;"), SL("} s99_reader;"), SL(""), SL("static S99_UNUSED void"), SL("s99_enc_int(s99_stream *o, int64_t v)"), SL("{"), SL(" char tmp[32] = {0};"), SL(" int n = snprintf(tmp, sizeof(tmp), \"i%lld;\", (long long)v);"), SL(" s99_put(o, tmp, n);"), SL("}"), SL(""), SL("static S99_UNUSED void"), SL("s99_enc_uint(s99_stream *o, uint64_t v)"), SL("{"), SL(" char tmp[32] = {0};"), SL(" int n = snprintf(tmp, sizeof(tmp), \"i%llu;\", (unsigned long long)v);"), SL(" s99_put(o, tmp, n);"), SL("}"), SL(""), SL("static S99_UNUSED void"), SL("s99_enc_float(s99_stream *o, double v)"), SL("{"), SL(" char tmp[48] = {0};"), SL(" int n = snprintf(tmp, sizeof(tmp), \"f%a;\", v);"), SL(" s99_put(o, tmp, n);"), SL("}"), SL(""), SL("static S99_UNUSED void"), SL("s99_enc_str(s99_stream *o, const s99_str *v)"), SL("{"), SL(" char tmp[32] = {0};"), SL(" int n = snprintf(tmp, sizeof(tmp), \"s%lld:\", (long long)s99_str_len(*v));"), SL(" s99_put(o, tmp, n);"), SL(" s99_put(o, s99_str_data(*v), s99_str_len(*v));"), SL("}"), SL(""), SL("static S99_NORETURN S99_UNUSED void"), SL("s99_dec_fail(void)"), SL("{"), SL(" s99_fail(\"star99\", \"the compiler sent a malformed value\");"), SL("}"), SL(""), SL("static S99_UNUSED int"), SL("s99_dec_char(s99_reader *r)"), SL("{"), SL(" int c = r->f ? getc(r->f) : r->pos < r->len ? r->mem[r->pos++] : EOF;"), SL(" if (c == EOF) {"), SL(" if (!r->f) s99_dec_fail();"), SL(" exit(0);"), SL(" }"), SL(" return c;"), SL("}"), SL(""), SL("static S99_UNUSED void"), SL("s99_dec_expect(s99_reader *r, int want)"), SL("{"), SL(" if (s99_dec_char(r) != want) s99_dec_fail();"), SL("}"), SL(""), SL("static S99_UNUSED int64_t"), SL("s99_dec_num(s99_reader *r)"), SL("{"), SL(" char tmp[64] = {0};"), SL(" int n = 0, c = 0;"), SL(" while ((c = s99_dec_char(r)) != ';' && c != ':') {"), SL(" if (n >= (int)sizeof(tmp) - 1) s99_dec_fail();"), SL(" tmp[n++] = (char)c;"), SL(" }"), SL(" tmp[n] = 0;"), SL(" return strtoll(tmp, 0, 10);"), SL("}"), SL(""), SL("static S99_UNUSED int64_t"), SL("s99_dec_int(s99_reader *r)"), SL("{"), SL(" s99_dec_expect(r, 'i');"), SL(" return s99_dec_num(r);"), SL("}"), SL(""), SL("static S99_UNUSED double"), SL("s99_dec_float(s99_reader *r)"), SL("{"), SL(" char tmp[64] = {0};"), SL(" int n = 0, c = 0;"), SL(" s99_dec_expect(r, 'f');"), SL(" while ((c = s99_dec_char(r)) != ';') {"), SL(" if (n >= (int)sizeof(tmp) - 1) s99_dec_fail();"), SL(" tmp[n++] = (char)c;"), SL(" }"), SL(" tmp[n] = 0;"), SL(" return strtod(tmp, 0);"), SL("}"), SL(""), SL("static S99_UNUSED bool"), SL("s99_dec_bool(s99_reader *r)"), SL("{"), SL(" s99_dec_expect(r, 'b');"), SL(" return s99_dec_char(r) == '1';"), SL("}"), SL(""), SL("static S99_UNUSED s99_str"), SL("s99_dec_str(s99_reader *r)"), SL("{"), SL(" s99_dec_expect(r, 's');"), SL(" int64_t n = s99_dec_num(r);"), SL(" uint8_t *buf = (uint8_t *)s99_alloc((size_t)n + 1);"), SL(" if (!r->f) {"), SL(" if (n > r->len - r->pos) s99_dec_fail();"), SL(" memcpy(buf, r->mem + r->pos, (size_t)n);"), SL(" r->pos += n;"), SL(" } else if (n && fread(buf, 1, (size_t)n, r->f) != (size_t)n) {"), SL(" exit(0);"), SL(" }"), SL(" s99_str s = s99_bytes(buf, n);"), SL(" free(buf);"), SL(" return s;"), SL("}"), SL(""), SL("/* The metaprogram reads calls from fd 3 and answers on fd 4; its standard"), SL(" output goes to the compiler's standard error. */"), SL("static S99_UNUSED s99_reader s99_requests = {0};"), SL("// inside known(...): questions whose answer is not final give zero values"), SL("static S99_UNUSED int s99_known_depth = 0;"), SL("static S99_UNUSED int s99_not_final = 0;"), SL("static S99_UNUSED s99_stream s99_replies = {0};"), SL(""), SL("static S99_UNUSED int64_t"), SL("s99_dec_tag(s99_reader *r, int kind)"), SL("{"), SL(" s99_dec_expect(r, kind);"), SL(" return s99_dec_num(r);"), SL("}"), SL(""), SL(""), SL("static S99_UNUSED S99_NORETURN void"), SL("s99_cannot_cross(const s99_type *t)"), SL("{"), SL(" char msg[256] = {0};"), SL(" snprintf(msg, sizeof(msg), \"a %s cannot move between compile time and run time\", t->name);"), SL(" s99_fail(\"star99\", msg);"), SL("}"), SL(""), SL("/* A value as text for the other side, by its descriptor. */"), SL("static S99_UNUSED void"), SL("s99_enc(s99_stream *o, const s99_type *t, const void *v)"), SL("{"), SL(" char tmp[32] = {0};"), SL(" switch (t->kind) {"), SL(" case 's': s99_enc_str(o, (const s99_str *)v); return;"), SL(" case 'b': s99_put(o, *(const bool *)v ? \"b1\" : \"b0\", 2); return;"), SL(" case 'i': s99_enc_int(o, s99_get_int(t, v)); return;"), SL(" case 'u': s99_enc_uint(o, t->size == 8 ? *(const uint64_t *)v : (uint64_t)s99_get_int(t, v)); return;"), SL(" case 'f': s99_enc_float(o, *(const double *)v); return;"), SL(" case 'S':"), SL(" for (int64_t i = 0; i < t->count; i++)"), SL(" s99_enc(o, t->fields[i].type, (const char *)v + t->fields[i].offset);"), SL(" return;"), SL(" case 'U':"), SL(" case 'c': {"), SL(" int32_t tag = 0;"), SL(" const void *p = s99_variant(t, v, &tag);"), SL(" s99_put(o, tmp, snprintf(tmp, sizeof(tmp), \"u%d;\", (int)tag));"), SL(" const s99_field *f = t->nonil || tag ? t->fields + (t->nonil ? tag : tag - 1) : 0;"), SL(" if (f && f->type) s99_enc(o, f->type, p);"), SL(" return;"), SL(" }"), SL(" case 'l': {"), SL(" const s99_list *l = *(s99_list *const *)v;"), SL(" s99_put(o, tmp, snprintf(tmp, sizeof(tmp), \"l%lld;\", (long long)s99_list_len(l)));"), SL(" for (int64_t i = 0; i < s99_list_len(l); i++) s99_enc(o, t->elem, l->data + i * t->elem->size);"), SL(" return;"), SL(" }"), SL(" case 'a':"), SL(" for (int64_t i = 0; i < t->count; i++) s99_enc(o, t->elem, (const char *)v + i * t->elem->size);"), SL(" return;"), SL(" default: s99_cannot_cross(t);"), SL(" }"), SL("}"), SL(""), SL("/* Reads a value written by s99_enc into v, which holds nothing yet. */"), SL("static S99_UNUSED void"), SL("s99_dec(s99_reader *r, const s99_type *t, void *v)"), SL("{"), SL(" switch (t->kind) {"), SL(" case 's': *(s99_str *)v = s99_dec_str(r); return;"), SL(" case 'b': *(bool *)v = s99_dec_bool(r); return;"), SL(" case 'i':"), SL(" case 'u': s99_set_int(t, v, s99_dec_int(r)); return;"), SL(" case 'f': *(double *)v = s99_dec_float(r); return;"), SL(" case 'S':"), SL(" for (int64_t i = 0; i < t->count; i++)"), SL(" s99_dec(r, t->fields[i].type, (char *)v + t->fields[i].offset);"), SL(" return;"), SL(" case 'U':"), SL(" case 'c': {"), SL(" int32_t tag = (int32_t)s99_dec_tag(r, 'u');"), SL(" s99_box *box = 0;"), SL(" const s99_field *f = t->nonil || tag ? t->fields + (t->nonil ? tag : tag - 1) : 0;"), SL(" if (f && f->type) {"), SL(" box = s99_box_new((size_t)f->type->size);"), SL(" s99_dec(r, f->type, S99_BOXDATA(box));"), SL(" }"), SL(" if (t->kind == 'c') {"), SL(" *(s99_box **)v = box;"), SL(" } else {"), SL(" ((s99_union *)v)->tag = tag;"), SL(" ((s99_union *)v)->box = box;"), SL(" }"), SL(" return;"), SL(" }"), SL(" case 'l': {"), SL(" s99_list **l = (s99_list **)v;"), SL(" *l = 0;"), SL(" int64_t n = s99_dec_tag(r, 'l');"), SL(" for (int64_t i = 0; i < n; i++) s99_dec(r, t->elem, s99_list_push(l, t->elem->size));"), SL(" return;"), SL(" }"), SL(" case 'a':"), SL(" for (int64_t i = 0; i < t->count; i++) s99_dec(r, t->elem, (char *)v + i * t->elem->size);"), SL(" return;"), SL(" default: s99_cannot_cross(t);"), SL(" }"), SL("}"), SL(""), SL("/* A value from its text, as a template builds code */"), SL("static S99_UNUSED void"), SL("s99_decoded(s99_str s, const s99_type *t, void *v)"), SL("{"), SL(" s99_reader r = {0, (const uint8_t *)s99_str_data(s), s99_str_len(s), 0};"), SL(" s99_dec(&r, t, v);"), SL("}"), }; // A program's C carries only the runtime functions and globals it reaches, // and only the headers those need. The runtime's pieces, from runtime_lines: // each function (with the comment just above it and a blank line after it) // and each global, and the names each declares, made once. The pieces a // piece names (outside comments and strings), and whether it needs // or , are read when it is first needed. Types and // macros always stay. Every runtime function and global is S99_UNUSED, so // one kept for a name that only looks like a use is never a warning. enum { RT_PIECES = 512, RT_NAMES = 1024, RT_SLOTS = 2048 }; static struct { b32 ready; int npieces, nnames; i32 piece_of[countof(runtime_lines)]; // the piece a line is part of, or -1 i32 proto_of[countof(runtime_lines)]; // the function a line declares, or -1 i32 first[RT_PIECES], last[RT_PIECES]; b32 read[RT_PIECES], math[RT_PIECES], mman[RT_PIECES]; u64 names[RT_PIECES][RT_PIECES / 64]; // the pieces each one names, once read struct { Str name; int piece; } name[RT_NAMES]; i32 slot[RT_SLOTS]; // a name by its hash, plus one iz math_line, mman_first, mman_last; // #include ; the block } rt = {0}; // A cheap hash of an s99_ name, from its length and a few of its bytes: // a big program's C names the runtime tens of thousands of times. static u64 rt_hash(Str s) { u8 *p = s.data; iz n = s.len; u64 h = (u64)n * 0x9e3779b97f4a7c15u; h ^= (u64)p[n - 1] << 8 | p[n > 5 ? 5 : n - 1]; h ^= (u64)p[n > 4 ? 4 : n - 1] << 16 | (u64)p[n - 2] << 24; return h ^ h >> 29; } // The piece a name declares, or -1. static int rt_find(Str name) { for (u64 i = rt_hash(name);; i++) { int k = rt.slot[i & (RT_SLOTS - 1)] - 1; if (k < 0) return -1; if (equals(rt.name[k].name, name)) return rt.name[k].piece; } } static void rt_declare(Str name, int piece) { assert(rt.nnames < RT_NAMES); int k = rt.nnames++; rt.name[k].name = name; rt.name[k].piece = piece; u64 h = rt_hash(name); while (rt.slot[h & (RT_SLOTS - 1)]) h++; rt.slot[h & (RT_SLOTS - 1)] = (i32)(k + 1); } // The s99_ names a global's line declares: those before any = or comment // (a type name is followed by another word or a *). static void rt_declare_global(Str l, int piece) { u8 *more = name_bytes(); iz stop = 0; while (stop < l.len && l.data[stop] != '=' && l.data[stop] != '/') stop++; for (iz c = 0;; c++) { iz k = indexof(substr(l, c, stop), S("s99_")); if (k < 0) break; c += k; if (c && more[l.data[c - 1]]) continue; iz e = c; while (e < l.len && more[l.data[e]]) e++; Str after = substr(l, e, l.len); if (startswith(after, S(",")) || startswith(after, S(";")) || startswith(after, S("[")) || startswith(after, S(" ="))) rt_declare(substr(l, c, e), piece); c = e - 1; } } // The name of the function a line "s99_name(...)" calls or declares. static Str rt_callee(Str l) { Str s = substr(l, indexof(l, S("s99_")), l.len); return substr(s, 0, indexof(s, S("("))); } static void rt_index(void) { if (rt.ready) return; rt.ready = 1; iz n = countof(runtime_lines); rt.math_line = rt.mman_first = rt.mman_last = -1; for (iz i = 0; i < n; i++) rt.piece_of[i] = rt.proto_of[i] = -1; for (iz i = 1; i < n; i++) { Str l = runtime_lines[i], prev = runtime_lines[i - 1]; u8 first_byte = l.len ? l.data[0] : 0; if (first_byte == '#') { if (equals(l, S("#include "))) rt.math_line = i; if (equals(l, S("#include "))) rt.mman_first = i - 1, rt.mman_last = i + 2; continue; } if (first_byte != 's') continue; // pieces and declarations start "s99_" or "static" iz first = i, last = i; assert(rt.npieces < RT_PIECES); if (startswith(l, S("s99_")) && indexof(l, S("(")) >= 0 && prev.len && prev.data[0] != ' ' && !endswith(l, S(";"))) { // a function: "s99_name(" at the start of a line after its result first = i - 1; while (last + 1 < n && !equals(runtime_lines[last], S("}"))) last++; if (first > 0 && endswith(runtime_lines[first - 1], S("*/"))) { iz k = first - 1; while (k > 0 && startswith(runtime_lines[k], S(" "))) k--; if (startswith(runtime_lines[k], S("/*"))) first = k; } if (last + 1 < n && !runtime_lines[last + 1].len) last++; rt_declare(rt_callee(l), rt.npieces); } else if (equals(l, S("static S99_UNUSED struct {"))) { // a global of a struct type of its own: to its "} name;" while (last + 1 < n && !startswith(runtime_lines[last], S("}"))) last++; rt_declare_global(runtime_lines[last], rt.npieces); } else if (startswith(l, S("static ")) && indexof(l, S("(")) < 0 && (endswith(l, S(";")) || endswith(l, S("*/")))) { rt_declare_global(l, rt.npieces); // a global, maybe with a comment } else { iz c = indexof(l, S("s99_")); if (startswith(l, S("static ")) && c >= 0 && indexof(substr(l, c, l.len), S("(")) >= 0 && endswith(l, S(");"))) rt.proto_of[i] = -2; // a declaration: its function is known below continue; } rt.first[rt.npieces] = (i32)first; rt.last[rt.npieces] = (i32)last; for (iz k = first; k <= last; k++) rt.piece_of[k] = (i32)rt.npieces; rt.npieces++; i = last; } for (iz i = 0; i < n; i++) { if (rt.proto_of[i] == -2) rt.proto_of[i] = (i32)rt_find(rt_callee(runtime_lines[i])); } } // What a piece names, outside comments and strings: other pieces, the math // functions of , and S99_HAVE_MMAP. static void rt_read(int p) { if (rt.read[p]) return; rt.read[p] = 1; static Str maths[] = {SL("isnan"), SL("isinf"), SL("isfinite"), SL("INFINITY"), SL("NAN"), SL("nan"), SL("HUGE_VAL"), SL("sqrt"), SL("floor"), SL("ceil"), SL("fabs"), SL("fmod"), SL("pow"), SL("round"), SL("trunc"), SL("frexp"), SL("ldexp"), SL("modf")}; u8 *more = name_bytes(); b32 comment = 0; for (iz i = rt.first[p]; i <= rt.last[p]; i++) { Str l = runtime_lines[i]; for (iz c = 0; c < l.len; c++) { u8 ch = l.data[c]; u8 next = c + 1 < l.len ? l.data[c + 1] : 0; if (comment) { if (ch == '*' && next == '/') comment = 0, c++; } else if (ch == '/' && next == '*') { comment = 1; c++; } else if (ch == '"' || ch == '\'') { while (c + 1 < l.len && l.data[c + 1] != ch) c += l.data[c + 1] == '\\' && c + 2 < l.len ? 2 : 1; if (c + 1 < l.len) c++; } else if (more[ch] && (!c || !more[l.data[c - 1]])) { iz e = c; while (e < l.len && more[l.data[e]]) e++; Str w = substr(l, c, e); int k = startswith(w, S("s99_")) ? rt_find(w) : -1; if (k >= 0) rt.names[p][k / 64] |= (u64)1 << (k % 64); if (equals(w, S("S99_HAVE_MMAP"))) rt.mman[p] = 1; for (iz m = 0; m < countof(maths) && !rt.math[p]; m++) rt.math[p] = maths[m].data[0] == ch && equals(w, maths[m]); c = e - 1; } } } } // Whether c has the word w at i: no name byte just before or after it. static b32 word_at(Str c, iz i, Str w) { u8 *more = name_bytes(); return startswith(substr(c, i, c.len), w) && (!i || !more[c.data[i - 1]]) && (i + w.len == c.len || !more[c.data[i + w.len]]); } // The C of a program, the runtime put in at the offset at: the pieces the // rest names, and those they name. static Str with_runtime(Str c, iz at) { rt_index(); b32 need[RT_PIECES] = {0}; int todo[RT_PIECES] = {0}, ntodo = 0; // needed, not yet followed u8 *more = name_bytes(); for (iz s = at;; s += 4) { iz k = indexof(substr(c, s, c.len), S("s99_")); if (k < 0) break; s += k; if (s && more[c.data[s - 1]]) continue; iz e = s + 4; while (e < c.len && more[c.data[e]]) e++; int p = rt_find(substr(c, s, e)); if (p >= 0 && !need[p]) need[p] = 1, todo[ntodo++] = p; } while (ntodo) { int p = todo[--ntodo]; rt_read(p); for (int w = 0; w < RT_PIECES / 64; w++) { for (u64 bits = rt.names[p][w]; bits; bits &= bits - 1) { int k = w * 64; for (u64 b = bits & -bits; b > 1; b >>= 1) k++; if (!need[k]) need[k] = 1, todo[ntodo++] = k; } } } b32 math = 0, mman = 0; for (int p = 0; p < rt.npieces; p++) { math |= need[p] && rt.math[p]; mman |= need[p] && rt.mman[p]; } for (iz d = at; !math; d++) { iz k = indexof(substr(c, d, c.len), S("double")); if (k < 0) break; d += k; math = word_at(c, d, S("double")); } b32 *keep = new(&perm, countof(runtime_lines), b32); iz size = c.len; for (iz i = 0; i < countof(runtime_lines); i++) { keep[i] = (rt.piece_of[i] < 0 || need[rt.piece_of[i]]) && (rt.proto_of[i] < 0 || need[rt.proto_of[i]]) && (i != rt.math_line || math) && (i < rt.mman_first || i > rt.mman_last || mman); if (keep[i]) size += runtime_lines[i].len + 1; } Buf out = {new(&perm, size, u8), 0, size}; // made once: a big program's C is large bput(&out, substr(c, 0, at)); for (iz i = 0; i < countof(runtime_lines); i++) { if (!keep[i]) continue; bput(&out, runtime_lines[i]); bputc(&out, '\n'); } bput(&out, substr(c, at, c.len)); return bstr(&out); } // The name a top-level declaration of the prelude declares, from its line, // or nothing for another line. static Str prelude_decl_name(Str s) { static Str kw[] = {SL("private "), SL("macro "), SL("fn "), SL("struct "), SL("union "), SL("var "), SL("const ")}; b32 decl = 0; for (b32 more = 1; more;) { more = 0; for (iz k = 0; k < countof(kw); k++) { if (startswith(s, kw[k])) s = substr(s, kw[k].len, s.len), more = decl = 1; } } iz n = 0; while (decl && n < s.len && name_bytes()[s.data[n]]) n++; return substr(s, 0, n); } // Which part of the prelude declares each of its names, read from the texts // (1 std:prelude, 2 std:build); and every name in the order one prelude // module declared them, so "did you mean" picks from them as it did. static STATE Map *prelude_parts = 0, *prelude_names_in_order = 0; static void index_prelude(void) { if (prelude_parts) return; Str *texts[2] = {std_prelude_lines, std_build_lines}; iz lens[2] = {countof(std_prelude_lines), countof(std_build_lines)}; for (iz p = 0; p < 2; p++) { for (iz i = 0; i < lens[p]; i++) { Str n = prelude_decl_name(texts[p][i]); if (!n.len) continue; void **slot = upsert(&prelude_parts, n, 1); if (!*slot) *slot = (void *)(p + 1); upsert(&prelude_names_in_order, n, 1); } } } static Map * prelude_names(void) { index_prelude(); return prelude_parts; } static Map * prelude_order(void) { index_prelude(); return prelude_names_in_order; } // Debug builds: these runtime lines come after the runtime. static Str debug_runtime_lines[] = { SL("/* Debug builds: a shadow stack of the Star99 calls, innermost first, which"), SL(" a failed check, fail() or a crash signal prints, one file:line:col per"), SL(" call. Each function keeps its frame on the C stack, pops it before it"), SL(" returns, and records the statement it runs (which also makes its frame"), SL(" the top again, after a call that left without popping). */"), SL("#if defined(__unix__) || defined(__APPLE__)"), SL("#include "), SL("#include "), SL("#define S99_DBG_SIGNALS 1"), SL("#endif"), SL("typedef struct s99_dbg_frame {"), SL(" const char *fn; /* the Star99 function, as declared */"), SL(" const char *at; /* the statement it runs */"), SL(" struct s99_dbg_frame *up;"), SL("} s99_dbg_frame;"), SL("static s99_dbg_frame *s99_dbg_top = 0;"), SL("#define S99_FRAME(name, where) s99_dbg_frame s99_df = {name, where, s99_dbg_top}; s99_dbg_top = &s99_df"), SL("#define S99_AT(where) (s99_df.at = (where), s99_dbg_top = &s99_df)"), SL("#define S99_POP (s99_dbg_top = s99_df.up)"), SL("/* Under the sanitizers a report stops the program with abort(), like a"), SL(" failed check, so the Star99 calls follow it (unless ASAN_OPTIONS or"), SL(" UBSAN_OPTIONS say otherwise). */"), SL("const char *__asan_default_options(void);"), SL("const char *__ubsan_default_options(void);"), SL("S99_UNUSED const char *"), SL("__asan_default_options(void)"), SL("{"), SL(" return \"abort_on_error=1\";"), SL("}"), SL("S99_UNUSED const char *"), SL("__ubsan_default_options(void)"), SL("{"), SL(" return \"print_stacktrace=1:halt_on_error=1:abort_on_error=1\";"), SL("}"), SL("#ifdef S99_DBG_SIGNALS"), SL("static volatile sig_atomic_t s99_dbg_reported = 0;"), SL("static struct sigaction s99_dbg_old[32]; /* static, so zero; with = {0}, tcc would store the zeros in the executable */"), SL(""), SL("/* Written out with write(): a crash may have left stdio or the heap broken. */"), SL("static void"), SL("s99_dbg_put(const char *s)"), SL("{"), SL(" size_t n = strlen(s);"), SL(" while (n) {"), SL(" ssize_t w = write(2, s, n);"), SL(" if (w <= 0) return;"), SL(" s += w;"), SL(" n -= (size_t)w;"), SL(" }"), SL("}"), SL(""), SL("/* The frame that called f. The walk stops at a loop and after very many"), SL(" frames: the chain is only as good as the pops (and the frames may be in"), SL(" a sanitizer's fake stack, in no order). */"), SL("static long s99_dbg_walked = 0;"), SL("static s99_dbg_frame *"), SL("s99_dbg_up(const s99_dbg_frame *f)"), SL("{"), SL(" return f->up != f && ++s99_dbg_walked < 50000000 ? f->up : 0;"), SL("}"), SL(""), SL("static int"), SL("s99_dbg_same(const s99_dbg_frame *a, const s99_dbg_frame *b)"), SL("{"), SL(" return !strcmp(a->fn, b->fn) && !strcmp(a->at, b->at);"), SL("}"), SL(""), SL("/* The calls, innermost first; a run of the same call (recursion) is one"), SL(" line, and of very many lines the first and the last are shown. */"), SL("static void"), SL("s99_dbg_stack(void)"), SL("{"), SL(" long lines = 0, line = 0;"), SL(" for (int pass = 0; pass < 2; pass++) {"), SL(" line = 0;"), SL(" s99_dbg_walked = 0;"), SL(" for (s99_dbg_frame *f = s99_dbg_top; f;) {"), SL(" s99_dbg_frame *g = s99_dbg_up(f);"), SL(" long times = 1;"), SL(" while (f != s99_dbg_top && g && s99_dbg_same(f, g)) times++, g = s99_dbg_up(g);"), SL(" if (pass == 1 && (lines <= 24 || line < 16 || line >= lines - 4)) {"), SL(" char n[48] = \"\";"), SL(" if (times > 1) snprintf(n, sizeof(n), \", %ld times\", times);"), SL(" s99_dbg_put(f->at);"), SL(" s99_dbg_put(f == s99_dbg_top ? \": note: in \" : \": note: called from \");"), SL(" s99_dbg_put(f->fn);"), SL(" s99_dbg_put(n);"), SL(" s99_dbg_put(\"\\n\");"), SL(" } else if (pass == 1 && line == 16) {"), SL(" char n[64] = {0};"), SL(" snprintf(n, sizeof(n), \"... %ld more lines of calls ...\\n\", lines - 20);"), SL(" s99_dbg_put(n);"), SL(" }"), SL(" line++;"), SL(" f = g;"), SL(" }"), SL(" lines = line;"), SL(" }"), SL("}"), SL(""), SL("static void"), SL("s99_dbg_signal(int sig, siginfo_t *si, void *uc)"), SL("{"), SL(" (void)uc;"), SL(" if (!s99_dbg_reported) {"), SL(" s99_dbg_reported = 1;"), SL(" if (sig != SIGABRT) { /* a check or fail() has said what went wrong */"), SL(" s99_dbg_put(s99_dbg_top ? s99_dbg_top->at : \"star99\");"), SL(" s99_dbg_put(\": error: the program crashed: \");"), SL(" s99_dbg_put(sig == SIGSEGV ? \"invalid memory access or stack overflow (SIGSEGV)\""), SL(" : sig == SIGBUS ? \"invalid memory access (SIGBUS)\""), SL(" : sig == SIGFPE ? \"arithmetic error (SIGFPE)\""), SL(" : sig == SIGILL ? \"illegal instruction (SIGILL)\""), SL(" : \"a signal\");"), SL(" s99_dbg_put(\"\\n\");"), SL(" }"), SL(" s99_dbg_stack();"), SL(" }"), SL(" /* then what there was before: the default, or a sanitizer's report */"), SL(" sigaction(sig, &s99_dbg_old[sig], 0);"), SL(" int sent = si->si_code <= 0 || si->si_code == SI_USER; /* Linux: <= 0 */"), SL("#ifdef SI_QUEUE"), SL(" sent |= si->si_code == SI_QUEUE;"), SL("#endif"), SL("#ifdef SI_LWP"), SL(" sent |= si->si_code == SI_LWP; /* FreeBSD's thr_kill */"), SL("#endif"), SL(" if (sig == SIGABRT || sent) raise(sig);"), SL(" /* a fault: the instruction runs again, and faults again */"), SL("}"), SL(""), SL("/* Run by main after s99_init (not as a constructor: tcc -b sets up its"), SL(" checks in one, and they must come first). */"), SL("static void"), SL("s99_dbg_start(void)"), SL("{"), SL(" static char stack[1 << 16]; /* a stack overflow is reported from here; static, so zero, and = {0} would put its 64 KiB in a tcc executable */"), SL(" static stack_t ss = {0}, old = {0};"), SL(" memset(&ss, 0, sizeof(ss));"), SL(" ss.ss_sp = stack;"), SL(" ss.ss_size = sizeof(stack);"), SL(" if (sigaltstack(0, &old) || (old.ss_flags & SS_DISABLE)) sigaltstack(&ss, 0);"), SL(" static const int sigs[] = {SIGABRT, SIGSEGV, SIGBUS, SIGFPE, SIGILL};"), SL(" static struct sigaction sa = {0};"), SL(" for (int i = 0; i < 5; i++) {"), SL(" memset(&sa, 0, sizeof(sa));"), SL(" sa.sa_sigaction = s99_dbg_signal;"), SL(" sa.sa_flags = SA_SIGINFO | SA_ONSTACK;"), SL(" sigemptyset(&sa.sa_mask);"), SL(" sigaction(sigs[i], &sa, &s99_dbg_old[sigs[i]]);"), SL(" }"), SL("}"), SL("#else"), SL("#define s99_dbg_start() ((void)0)"), SL("#endif"), }; // The debugger script of debug builds, Python for gdb and lldb: Star99 // values, names and stepping. A debug build holds it for gdb to load // (.debug_gdb_scripts), and star99 debug-script prints it. Kept as lines // like the runtime. static Str debug_script_lines[] = { SL("# Star99 debugging support for gdb and lldb: values shown as Star99 prints"), SL("# them, Star99 names in backtraces, and stepping that stays in Star99 code."), SL("#"), SL("# A debug build (star99 PROGRAM --debug) holds this script; gdb runs it by"), SL("# itself where auto-loading is allowed (add-auto-load-safe-path PROGRAM)."), SL("# `star99 debug-script` prints it, for loading it by hand:"), SL("# gdb: source star99.py"), SL("# lldb: command script import star99.py"), SL("# Both find what they need in the program: s99_debug_info, a table of what"), SL("# each C name stands for, and the runtime's type descriptors (info1, ...)."), SL(""), SL("import json"), SL("import os"), SL("import struct"), SL(""), SL("VERSION = 1"), SL(""), SL("# --- Star99 values from memory, for any debugger -------------------------"), SL(""), SL(""), SL("class Unreadable(Exception):"), SL(" pass"), SL(""), SL(""), SL("# The runtime's structs: each member's offset and size on a 64-bit target."), SL("# The debugger's view of the types replaces the offsets where it has them."), SL("LAYOUTS = {"), SL(" \"s99_type\": {\"name\": (0, \"p\"), \"size\": (8, \"q\"), \"kind\": (16, \"b\"), \"nonil\": (17, \"b\"),"), SL(" \"count\": (24, \"q\"), \"elem\": (32, \"p\"), \"key\": (40, \"p\"),"), SL(" \"fields\": (48, \"p\"), \"id\": (80, \"i\")},"), SL(" \"s99_field\": {\"name\": (0, \"p\"), \"type\": (8, \"p\"), \"offset\": (16, \"q\"),"), SL(" \"named\": (24, \"b\")},"), SL(" \"s99_list\": {\"gen\": (0, \"Q\"), \"len\": (8, \"q\"), \"cap\": (16, \"q\"), \"elemsize\": (24, \"q\"),"), SL(" \"data\": (32, \"p\")},"), SL(" \"s99_map\": {\"count\": (8, \"q\"), \"order\": (40, \"p\"), \"norder\": (48, \"q\"),"), SL(" \"valoff\": (80, \"q\")},"), SL(" \"s99_mapent\": {\"gen\": (0, \"Q\"), \"alive\": (8, \"I\")},"), SL(" \"s99_pool\": {\"len\": (8, \"q\"), \"slotsize\": (24, \"q\"), \"slots\": (32, \"p\")},"), SL(" \"s99_slot\": {\"version\": (0, \"Q\")},"), SL(" \"s99_box\": {\"gen\": (0, \"Q\")},"), SL(" \"s99_any\": {\"type\": (0, \"p\"), \"box\": (8, \"p\")},"), SL(" \"s99_ref\": {\"base\": (0, \"p\"), \"gen\": (8, \"Q\"), \"index\": (16, \"q\"), \"kind\": (24, \"i\"),"), SL(" \"off\": (28, \"i\")},"), SL(" \"s99_fn\": {\"fn\": (0, \"p\"), \"env\": (8, \"p\"), \"owns\": (16, \"q\")},"), SL(" \"s99_route\": {\"outer\": (0, \"r\"), \"elemsize\": (32, \"q\")},"), SL("}"), SL("SIZES = {\"s99_type\": 120, \"s99_field\": 32, \"s99_list\": 64, \"s99_mapent\": 16, \"s99_slot\": 16,"), SL(" \"s99_box\": 8, \"s99_any\": 16, \"s99_ref\": 32, \"s99_fn\": 24}"), SL(""), SL("REF_CELL, REF_LIST, REF_POOL, REF_MAP, REF_BOX, REF_NEST = 1, 2, 3, 4, 5, 6"), SL(""), SL(""), SL("class Desc:"), SL(" \"\"\"A runtime type descriptor (s99_type).\"\"\""), SL(""), SL(" def __init__(self, addr):"), SL(" self.addr = addr"), SL(" self.name = \"?\""), SL(" self.size = 0"), SL(" self.kind = \"?\""), SL(" self.nonil = False"), SL(" self.count = 0"), SL(" self.elem = None"), SL(" self.key = None"), SL(" self.fields = [] # (name, Desc or None, offset, named)"), SL(""), SL(""), SL("class Target:"), SL(" \"\"\"What the core needs of a debugger: memory, layouts and symbols.\"\"\""), SL(""), SL(" ptr = 8"), SL(""), SL(" def read(self, addr, n): # bytes, or raise Unreadable"), SL(" raise Unreadable()"), SL(""), SL(" def offset(self, struct_name, member): # or None"), SL(" return None"), SL(""), SL(" def sizeof(self, struct_name): # or None"), SL(" return None"), SL(""), SL(" def symbol_address(self, name): # a static variable, or None"), SL(" return None"), SL(""), SL(" def function_at(self, addr): # the C function there, or None"), SL(" return None"), SL(""), SL(" def members(self, struct_name): # {member: (offset, size)} of a C struct"), SL(" return {}"), SL(""), SL(" def forget(self): # addresses of symbols, which a new run may move"), SL(" pass"), SL(""), SL(" def run_id(self): # what changes when the program runs again (and moves)"), SL(" return None"), SL(""), SL(""), SL("def asan_options():"), SL(" \"\"\"ASAN_OPTIONS for a program under a debugger: leak checks off.\"\"\""), SL(" have = os.environ.get(\"ASAN_OPTIONS\", \"\")"), SL(" return (have + \":\" if have else \"\") + \"detect_leaks=0\""), SL(""), SL(""), SL("def utf8(b):"), SL(" return b.decode(\"utf-8\", \"backslashreplace\")"), SL(""), SL(""), SL("def quoted(b):"), SL(" \"\"\"Text as Star99 writes a string inside a value: \"a\\\\n\".\"\"\""), SL(" out = bytearray(b'\"')"), SL(" for c in b:"), SL(" if c == 34 or c == 92:"), SL(" out += b\"\\\\\" + bytes([c])"), SL(" elif c == 10:"), SL(" out += b\"\\\\n\""), SL(" elif c == 9:"), SL(" out += b\"\\\\t\""), SL(" elif c < 32 or c == 127:"), SL(" out += b\"\\\\x%02x\" % c"), SL(" else:"), SL(" out.append(c)"), SL(" out += b'\"'"), SL(" return utf8(bytes(out))"), SL(""), SL(""), SL("def float_text(v):"), SL(" \"\"\"A Float as Star99 prints it: the shortest text that reads back.\"\"\""), SL(" if v != v:"), SL(" return \"nan\""), SL(" if v in (float(\"inf\"), float(\"-inf\")):"), SL(" return \"inf\" if v > 0 else \"-inf\""), SL(" a = abs(v)"), SL(" plain = a == 0 or 1e-5 <= a < 1e16"), SL(" t = repr(v)"), SL(" for prec in range(0 if plain else 1, 18 + (5 if plain else 0)):"), SL(" t = (\"%.*f\" if plain else \"%.*g\") % (prec, v)"), SL(" if float(t) == v:"), SL(" break"), SL(" if not any(c in t for c in \".eni\"):"), SL(" t += \".0\""), SL(" return t"), SL(""), SL(""), SL("class Program:"), SL(" \"\"\"One Star99 debug build: its table and its descriptors.\"\"\""), SL(""), SL(" def __init__(self, target, info):"), SL(" self.t = target"), SL(" self.info = info"), SL(" self.types = {} # C typedef -> (Star99 name, descriptor symbol)"), SL(" for c, name, desc in info.get(\"types\", []):"), SL(" self.types[c] = (name, desc)"), SL(" self.fns = {f[\"c\"]: f for f in info.get(\"functions\", [])}"), SL(" # function values of named functions call a wrapper, w + the numbered name"), SL(" self.wrappers = {\"w\" + f[\"orig\"]: f[\"c\"] for f in info.get(\"functions\", [])"), SL(" if f.get(\"orig\")}"), SL(" self.globals = {g[0]: g for g in info.get(\"globals\", [])}"), SL(" self.descs = {}"), SL(" self.layout = {}"), SL(" self.limit = 200"), SL(" self.depth_limit = 20"), SL(" self.run = None"), SL(""), SL(" def fresh(self):"), SL(" \"\"\"Forgets what a run of the program before this one had where.\"\"\""), SL(" run = self.t.run_id()"), SL(" if run != self.run:"), SL(" self.run = run"), SL(" self.descs = {}"), SL(" self.t.forget()"), SL(""), SL(" # -- reading memory"), SL(""), SL(" def member(self, struct_name, name):"), SL(" key = (struct_name, name)"), SL(" if key not in self.layout:"), SL(" off, code = LAYOUTS[struct_name][name]"), SL(" got = self.t.offset(struct_name, name)"), SL(" self.layout[key] = (got if got is not None else off, code)"), SL(" return self.layout[key]"), SL(""), SL(" def size_of(self, struct_name):"), SL(" got = self.t.sizeof(struct_name)"), SL(" return got if got is not None else SIZES[struct_name]"), SL(""), SL(" def num(self, data, at, code):"), SL(" if code == \"p\":"), SL(" code = \"Q\" if self.t.ptr == 8 else \"I\""), SL(" size = struct.calcsize(\"<\" + code)"), SL(" if at + size > len(data):"), SL(" raise Unreadable()"), SL(" return struct.unpack_from(\"<\" + code, data, at)[0]"), SL(""), SL(" def get(self, struct_name, name, addr, data=None):"), SL(" \"\"\"A member of the runtime struct at addr (or in its bytes data).\"\"\""), SL(" off, code = self.member(struct_name, name)"), SL(" if data is not None:"), SL(" return self.num(data, off, code)"), SL(" size = struct.calcsize(\"<\" + (\"Q\" if code == \"p\" and self.t.ptr == 8 else"), SL(" \"I\" if code == \"p\" else code))"), SL(" return self.num(self.t.read(addr + off, size), 0, code)"), SL(""), SL(" def word(self, addr):"), SL(" return self.num(self.t.read(addr, self.t.ptr), 0, \"p\")"), SL(""), SL(" def cstring(self, addr, limit=256):"), SL(" if not addr:"), SL(" return \"\""), SL(" out = b\"\""), SL(" while len(out) < limit:"), SL(" chunk = self.t.read(addr + len(out), 32)"), SL(" z = chunk.find(b\"\\0\")"), SL(" if z >= 0:"), SL(" return utf8(out + chunk[:z])"), SL(" out += chunk"), SL(" return utf8(out)"), SL(""), SL(" def read(self, addr, n):"), SL(" if addr is None: # a missing payload reads as the zero value"), SL(" return bytes(n)"), SL(" if n == 0:"), SL(" return b\"\""), SL(" return self.t.read(addr, n)"), SL(""), SL(" # -- descriptors"), SL(""), SL(" def desc(self, addr):"), SL(" if not addr:"), SL(" return None"), SL(" if addr in self.descs:"), SL(" return self.descs[addr]"), SL(" d = Desc(addr)"), SL(" self.descs[addr] = d"), SL(" g = lambda n: self.get(\"s99_type\", n, addr)"), SL(" d.name = self.cstring(g(\"name\"))"), SL(" d.size = g(\"size\")"), SL(" d.kind = chr(g(\"kind\") & 0xFF)"), SL(" d.nonil = bool(g(\"nonil\"))"), SL(" d.count = g(\"count\")"), SL(" d.elem = self.desc(g(\"elem\"))"), SL(" d.key = self.desc(g(\"key\"))"), SL(" fields = g(\"fields\")"), SL(" if d.kind in \"SU c\" and fields:"), SL(" fs = self.size_of(\"s99_field\")"), SL(" n = d.count"), SL(" for i in range(n):"), SL(" f = fields + i * fs"), SL(" gf = lambda m: self.get(\"s99_field\", m, f)"), SL(" d.fields.append((self.cstring(gf(\"name\")), self.desc(gf(\"type\")),"), SL(" gf(\"offset\"), bool(gf(\"named\"))))"), SL(" return d"), SL(""), SL(" def desc_named(self, symbol):"), SL(" \"\"\"The descriptor a symbol like info338 is.\"\"\""), SL(" if not symbol:"), SL(" return None"), SL(" addr = self.t.symbol_address(symbol)"), SL(" return self.desc(addr) if addr else None"), SL(""), SL(" def ctype_of(self, desc):"), SL(" \"\"\"The C typedef of the type a descriptor describes, or None.\"\"\""), SL(" for c, (name, d) in self.types.items():"), SL(" if d == desc:"), SL(" return c"), SL(" return None"), SL(""), SL(" def desc_of_type(self, c_typedef):"), SL(" entry = self.types.get(c_typedef)"), SL(" return self.desc_named(entry[1]) if entry else None"), SL(""), SL(" # -- values, as Star99 prints them inside another (strings quoted)"), SL(""), SL(" def show(self, d, addr, data=None, depth=0):"), SL(" try:"), SL(" return self.show_(d, addr, data, depth)"), SL(" except Unreadable:"), SL(" return \"\" if addr is None else \"\" % addr"), SL(""), SL(" def value_bytes(self, d, addr, data):"), SL(" if data is not None:"), SL(" return data[:d.size]"), SL(" return self.read(addr, d.size)"), SL(""), SL(" def word_in(self, d, addr, data): # a value that is one pointer"), SL(" return self.num(self.value_bytes(d, addr, data), 0, \"p\")"), SL(""), SL(" def show_(self, d, addr, data, depth):"), SL(" if d is None:"), SL(" return \"?\""), SL(" if depth > self.depth_limit:"), SL(" return \"...\""), SL(" k = d.kind"), SL(" if k in \"iub\":"), SL(" b = self.value_bytes(d, addr, data)"), SL(" if k == \"b\":"), SL(" return \"true\" if b[0] else \"false\""), SL(" code = {1: \"b\", 2: \"h\", 4: \"i\", 8: \"q\"}[d.size]"), SL(" if k == \"u\":"), SL(" code = code.upper()"), SL(" return str(struct.unpack_from(\"<\" + code, b)[0])"), SL(" if k == \"f\":"), SL(" return float_text(struct.unpack_from(\"\" % d.name"), SL(""), SL(" def show_string(self, l, limit=None):"), SL(" if not l:"), SL(" return '\"\"'"), SL(" n = self.get(\"s99_list\", \"len\", l)"), SL(" at = self.get(\"s99_list\", \"data\", l)"), SL(" limit = limit or max(self.limit, 200)"), SL(" b = self.read(at, min(n, limit)) if n else b\"\""), SL(" return quoted(b) + (\"...\" if n > limit else \"\")"), SL(""), SL(" def show_items(self, open_, close, ed, at, n, depth, inline=None):"), SL(" if ed is None:"), SL(" return open_ + \"?\" + close"), SL(" shown = min(n, self.limit)"), SL(" size = ed.size"), SL(" block = inline"), SL(" if block is None:"), SL(" block = self.read(at, shown * size) if shown else b\"\""), SL(" parts = [self.show(ed, None, block[i * size:(i + 1) * size], depth + 1)"), SL(" for i in range(shown)]"), SL(" if n > shown:"), SL(" parts.append(\"...\")"), SL(" return open_ + \", \".join(parts) + close"), SL(""), SL(" def payload(self, box):"), SL(" return box + self.size_of(\"s99_box\") if box else None"), SL(""), SL(" def show_union(self, d, b, depth):"), SL(" if d.kind == \"c\": # compact: the box alone, null is nil"), SL(" box = self.num(b, 0, \"p\")"), SL(" tag = 1 if box else 0"), SL(" else:"), SL(" tag = struct.unpack_from(\"= len(d.fields):"), SL(" return \"<%s with tag %d>\" % (d.name, tag)"), SL(" name, fd, off, named = d.fields[i]"), SL(" label = named or fd is None"), SL(" text = \".\" + name if label else \"\""), SL(" if fd is None:"), SL(" return text"), SL(" inner = self.show(fd, self.payload(box), None, depth + 1)"), SL(" return text + \"(\" + inner + \")\" if label else inner"), SL(""), SL(" def show_map(self, d, m, depth):"), SL(" if not m:"), SL(" return \"[:]\""), SL(" norder = self.get(\"s99_map\", \"norder\", m)"), SL(" order = self.get(\"s99_map\", \"order\", m)"), SL(" valoff = self.get(\"s99_map\", \"valoff\", m)"), SL(" ent = self.size_of(\"s99_mapent\")"), SL(" parts = []"), SL(" ptrs = self.read(order, norder * self.t.ptr) if norder else b\"\""), SL(" for i in range(norder):"), SL(" if len(parts) >= self.limit:"), SL(" parts.append(\"...\")"), SL(" break"), SL(" e = self.num(ptrs, i * self.t.ptr, \"p\")"), SL(" if not e or not self.get(\"s99_mapent\", \"alive\", e):"), SL(" continue"), SL(" key = e + ent"), SL(" parts.append(\"%s: %s\" % (self.show(d.key, key, None, depth + 1),"), SL(" self.show(d.elem, key + valoff, None, depth + 1)))"), SL(" return \"[\" + \", \".join(parts) + \"]\" if parts else \"[:]\""), SL(""), SL(" def show_pool(self, d, p, depth):"), SL(" parts = []"), SL(" if p:"), SL(" n = self.get(\"s99_pool\", \"len\", p)"), SL(" slots = self.get(\"s99_pool\", \"slots\", p)"), SL(" size = self.get(\"s99_pool\", \"slotsize\", p)"), SL(" for i in range(n):"), SL(" if len(parts) >= self.limit:"), SL(" parts.append(\"...\")"), SL(" break"), SL(" s = slots + i * size"), SL(" if self.get(\"s99_slot\", \"version\", s):"), SL(" item = s + self.size_of(\"s99_slot\")"), SL(" parts.append(self.show(d.elem, item, None, depth + 1))"), SL(" return \"Pool(\" + \", \".join(parts) + \")\""), SL(""), SL(" def gen(self, addr):"), SL(" return self.num(self.read(addr, 8), 0, \"Q\")"), SL(""), SL(" def deref(self, b):"), SL(" \"\"\"Where a reference leads, as (address, what it is) or (None, why not).\"\"\""), SL(" g = lambda n: self.get(\"s99_ref\", n, 0, b)"), SL(" kind, base, gen, index, off = g(\"kind\"), g(\"base\"), g(\"gen\"), g(\"index\"), g(\"off\")"), SL(" if kind == REF_CELL:"), SL(" return base + off, \"\""), SL(" if kind == REF_LIST:"), SL(" if self.gen(base) != gen:"), SL(" return None, \"[%d] of a list that is gone\" % index"), SL(" if not 0 <= index < self.get(\"s99_list\", \"len\", base):"), SL(" return None, \"[%d] past the end\" % index"), SL(" return (self.get(\"s99_list\", \"data\", base) +"), SL(" index * self.get(\"s99_list\", \"elemsize\", base) + off, \"[%d]\" % index)"), SL(" if kind == REF_POOL:"), SL(" if 0 <= index < self.get(\"s99_pool\", \"len\", base):"), SL(" s = (self.get(\"s99_pool\", \"slots\", base) +"), SL(" index * self.get(\"s99_pool\", \"slotsize\", base))"), SL(" if self.get(\"s99_slot\", \"version\", s) == gen:"), SL(" return s + self.size_of(\"s99_slot\") + off, \"item %d\" % index"), SL(" return None, \"item %d, removed\" % index"), SL(" if kind == REF_BOX:"), SL(" if self.gen(base) != gen:"), SL(" return None, \"a payload that changed\""), SL(" return self.payload(base) + off, \"\""), SL(" if kind == REF_MAP:"), SL(" norder = self.get(\"s99_map\", \"norder\", base)"), SL(" order = self.get(\"s99_map\", \"order\", base)"), SL(" valoff = self.get(\"s99_map\", \"valoff\", base)"), SL(" for i in [index] + list(range(norder)):"), SL(" if not 0 <= i < norder:"), SL(" continue"), SL(" e = self.word(order + i * self.t.ptr)"), SL(" if self.get(\"s99_mapent\", \"alive\", e) and self.gen(e) == gen:"), SL(" return e + self.size_of(\"s99_mapent\") + valoff + off, \"key\""), SL(" return None, \"a key that was removed\""), SL(" if kind == REF_NEST:"), SL(" outer = self.read(base, self.size_of(\"s99_ref\"))"), SL(" at, why = self.deref(outer)"), SL(" if at is None:"), SL(" return None, why"), SL(" l = self.word(at)"), SL(" if not l or not 0 <= index < self.get(\"s99_list\", \"len\", l):"), SL(" return None, \"[%d] past the end\" % index"), SL(" es = self.get(\"s99_route\", \"elemsize\", base)"), SL(" return self.get(\"s99_list\", \"data\", l) + index * es + off, \"[%d]\" % index"), SL(" return None, \"nil\""), SL(""), SL(" def show_ref(self, b, target, depth):"), SL(" kind = self.get(\"s99_ref\", \"kind\", 0, b)"), SL(" if not kind:"), SL(" return \"nil\""), SL(" at, what = self.deref(b)"), SL(" if at is None:"), SL(" return \"\" % what"), SL(" where = (\" \" + what) if what and what != \"key\" else \"\""), SL(" if target is None:"), SL(" return \"\" % where"), SL(" if depth > 3:"), SL(" return \"\" % where"), SL(" return \"\" % (where, self.show(target, at, None, depth + 1))"), SL(""), SL(" def fn_title(self, c):"), SL(" f = self.fns.get(c)"), SL(" if not f:"), SL(" return c"), SL(" if f[\"name\"] == \"fn\":"), SL(" return \"fn in \" + f[\"in\"] if f[\"in\"] else \"fn\""), SL(" return f[\"name\"]"), SL(""), SL(" def show_fn(self, b, depth):"), SL(" fn = self.get(\"s99_fn\", \"fn\", 0, b)"), SL(" env = self.get(\"s99_fn\", \"env\", 0, b)"), SL(" if not fn:"), SL(" return \"nil\""), SL(" c = self.t.function_at(fn)"), SL(" c = self.wrappers.get(c, c)"), SL(" f = self.fns.get(c) if c else None"), SL(" if not f:"), SL(" return \"\""), SL(" caps = self.captures(f, env, depth) if env else []"), SL(" title = self.fn_title(c)"), SL(" return \"<%s%s>\" % (title if title.startswith(\"fn\") else \"fn \" + title,"), SL(" \"\".join(\", %s: %s\" % (n, v) for n, v in caps))"), SL(""), SL(" def captures(self, f, env, depth):"), SL(" \"\"\"The variables a closure holds, from its environment at env.\"\"\""), SL(" out = []"), SL(" members = self.t.members(f.get(\"env\", \"\")) if f.get(\"env\") else {}"), SL(" for cname, name, how, tname, desc, param in f.get(\"captures\", []):"), SL(" if cname not in members:"), SL(" continue"), SL(" try:"), SL(" cell = self.word(env + members[cname][0])"), SL(" out.append((name, self.show_var(how, desc, cell, None, depth + 1)))"), SL(" except Unreadable:"), SL(" out.append((name, \"\"))"), SL(" return out"), SL(""), SL(" def show_var(self, how, desc, addr, data, depth=0):"), SL(" \"\"\"A variable the table describes: how says what the C holds.\"\"\""), SL(" d = self.desc_named(desc)"), SL(" if d is None:"), SL(" return None"), SL(" if how == \"cell\" or how == \"ptr\": # addr (or data) is the pointer"), SL(" p = addr if data is None else self.num(data, 0, \"p\")"), SL(" return self.show(d, p, None, depth) if p else \"nil\""), SL(" if how == \"ref\":"), SL(" b = self.read(addr, self.size_of(\"s99_ref\")) if data is None else data"), SL(" at, what = self.deref(b)"), SL(" return self.show(d, at, None, depth) if at is not None else \"\" % what"), SL(" return self.show(d, addr, data, depth)"), SL(""), SL(" # -- Star99 expressions: names, .field, [index], [key], .*, len()"), SL(""), SL(" def place_of_var(self, how, desc, addr, data):"), SL(" \"\"\"Where a variable the table describes keeps its value.\"\"\""), SL(" d = self.desc_named(desc)"), SL(" if how in (\"cell\", \"ptr\"):"), SL(" return d, self.num(data, 0, \"p\") if data is not None else self.word(addr)"), SL(" if how == \"ref\":"), SL(" at, what = self.deref(data if data is not None else"), SL(" self.read(addr, self.size_of(\"s99_ref\")))"), SL(" if at is None:"), SL(" raise EvalError(\"the reference leads to \" + what)"), SL(" return d, at"), SL(" return d, addr"), SL(""), SL(" def place_of_capture(self, how, desc, member):"), SL(" \"\"\"Where a captured variable is: its environment member at member"), SL(" points to its cell, or to its route (a captured var).\"\"\""), SL(" cell = self.word(member)"), SL(" if how == \"ref\":"), SL(" return self.place_of_var(how, desc, None, self.read(cell, self.size_of(\"s99_ref\")))"), SL(" return self.place_of_var(how, desc, None, struct.pack(\"= len(self.s)"), SL(""), SL(" def rest(self):"), SL(" return self.s[self.i:]"), SL(""), SL(" def take(self, t):"), SL(" self.skip()"), SL(" if self.s.startswith(t, self.i):"), SL(" self.i += len(t)"), SL(" return True"), SL(" return False"), SL(""), SL(" def name(self):"), SL(" self.skip()"), SL(" j = self.i"), SL(" while j < len(self.s) and (self.s[j].isalnum() or self.s[j] == \"_\"):"), SL(" j += 1"), SL(" if j == self.i or self.s[self.i].isdigit():"), SL(" return None"), SL(" n = self.s[self.i:j]"), SL(" self.i = j"), SL(" return n"), SL(""), SL(" def literal(self):"), SL(" \"\"\"A number, \"string\", 'c', true or false; or None.\"\"\""), SL(" self.skip()"), SL(" s, i = self.s, self.i"), SL(" digit = lambda j: j < len(s) and s[j].isdigit()"), SL(" if digit(i) or (i < len(s) and s[i] == \"-\" and digit(i + 1)):"), SL(" j = i + 1"), SL(" while j < len(s) and s[j].isdigit():"), SL(" j += 1"), SL(" self.i = j"), SL(" return int(s[i:j])"), SL(" if i < len(s) and s[i] in \"\\\"'\":"), SL(" q, j, out = s[i], i + 1, bytearray()"), SL(" esc = {\"n\": 10, \"t\": 9, \"\\\\\": 92, '\"': 34, \"'\": 39, \"0\": 0}"), SL(" while j < len(s) and s[j] != q:"), SL(" if s[j] == \"\\\\\" and j + 1 < len(s):"), SL(" out.append(esc.get(s[j + 1], ord(s[j + 1])))"), SL(" j += 2"), SL(" else:"), SL(" out += s[j].encode()"), SL(" j += 1"), SL(" self.i = j + 1"), SL(" return (\"s\", bytes(out)) if q == '\"' else (out[0] if out else 0)"), SL(" for word, v in ((\"true\", True), (\"false\", False)):"), SL(" e = i + len(word)"), SL(" if s.startswith(word, i) and not (e < len(s) and (s[e].isalnum() or s[e] == \"_\")):"), SL(" self.i = e"), SL(" return v"), SL(" return None"), SL(""), SL(""), SL("# --- gdb ---------------------------------------------------------------"), SL(""), SL("try:"), SL(" import gdb"), SL("except ImportError:"), SL(" gdb = None"), SL(""), SL("if gdb is not None:"), SL(" import gdb.printing"), SL(" import gdb.types"), SL(" try:"), SL(" from gdb.FrameDecorator import FrameDecorator"), SL(" except ImportError:"), SL(" FrameDecorator = object"), SL(""), SL(" class GdbTarget(Target):"), SL(" def __init__(self, objfile):"), SL(" self.objfile = objfile"), SL(" self.ptr = gdb.lookup_type(\"void\").pointer().sizeof"), SL(" self.syms = {}"), SL(""), SL(" def read(self, addr, n):"), SL(" try:"), SL(" return bytes(gdb.selected_inferior().read_memory(addr, n))"), SL(" except (gdb.MemoryError, gdb.error, OverflowError, ValueError):"), SL(" raise Unreadable()"), SL(""), SL(" def run_id(self):"), SL(" inf = gdb.selected_inferior()"), SL(" return (inf.num, inf.pid)"), SL(""), SL(" def forget(self):"), SL(" self.syms = {}"), SL(""), SL(" def struct_type(self, name):"), SL(" try:"), SL(" return gdb.lookup_type(name).strip_typedefs()"), SL(" except gdb.error:"), SL(" return None"), SL(""), SL(" def offset(self, struct_name, member):"), SL(" t = self.struct_type(struct_name)"), SL(" if t is None:"), SL(" return None"), SL(" for f in t.fields():"), SL(" if f.name == member:"), SL(" return f.bitpos // 8"), SL(" return None"), SL(""), SL(" def sizeof(self, struct_name):"), SL(" t = self.struct_type(struct_name)"), SL(" return t.sizeof if t is not None else None"), SL(""), SL(" def symbol_address(self, name):"), SL(" if name in self.syms:"), SL(" return self.syms[name]"), SL(" addr = None"), SL(" try:"), SL(" sym = None"), SL(" if hasattr(self.objfile, \"lookup_static_symbol\"):"), SL(" sym = self.objfile.lookup_static_symbol(name)"), SL(" if sym is None:"), SL(" sym = gdb.lookup_static_symbol(name)"), SL(" if sym is not None:"), SL(" addr = int(sym.value().address)"), SL(" except gdb.error:"), SL(" addr = None"), SL(" self.syms[name] = addr"), SL(" return addr"), SL(""), SL(" def function_at(self, addr):"), SL(" try:"), SL(" block = gdb.block_for_pc(addr)"), SL(" while block is not None and block.function is None:"), SL(" block = block.superblock"), SL(" if block is not None:"), SL(" return block.function.name"), SL(" except (gdb.error, RuntimeError):"), SL(" pass"), SL(" return None"), SL(""), SL(" def members(self, struct_name):"), SL(" try:"), SL(" t = gdb.lookup_type(struct_name).strip_typedefs()"), SL(" except gdb.error:"), SL(" return {}"), SL(" return {f.name: (f.bitpos // 8, f.type.sizeof) for f in t.fields()}"), SL(""), SL(" programs = {} # objfile -> Program (or None: no table)"), SL(""), SL(" def gdb_program(objfile):"), SL(" if objfile is None or not objfile.is_valid():"), SL(" return None"), SL(" key = objfile"), SL(" if key in programs:"), SL(" prog = programs[key]"), SL(" if prog is not None:"), SL(" prog.fresh()"), SL(" return prog"), SL(" prog = None"), SL(" try:"), SL(" sym = None"), SL(" if hasattr(objfile, \"lookup_static_symbol\"):"), SL(" sym = objfile.lookup_static_symbol(\"s99_debug_info\")"), SL(" else: # gdb before 10: the one there is, if it is in this objfile"), SL(" sym = gdb.lookup_static_symbol(\"s99_debug_info\")"), SL(" if sym is not None and sym.symtab.objfile != objfile:"), SL(" sym = None"), SL(" if sym is not None:"), SL(" v = sym.value()"), SL(" data = bytes(gdb.selected_inferior().read_memory(v.address, v.type.sizeof))"), SL(" prog = Program(GdbTarget(objfile), json.loads(data.rstrip(b\"\\0\").decode()))"), SL(" except (gdb.error, gdb.MemoryError, ValueError):"), SL(" prog = None"), SL(" programs[key] = prog"), SL(" if prog is not None:"), SL(" prog.fresh()"), SL(" return prog"), SL(""), SL(" def gdb_settings(prog):"), SL(" try:"), SL(" n = gdb.parameter(\"print elements\")"), SL(" prog.limit = 1 << 30 if n in (None, 0) else int(n)"), SL(" depth = gdb.parameter(\"print max-depth\")"), SL(" prog.depth_limit = 1 << 30 if depth in (None, -1) else int(depth)"), SL(" except (gdb.error, RuntimeError):"), SL(" pass"), SL(""), SL(" def program_of(val_type):"), SL(" objfile = getattr(val_type, \"objfile\", None)"), SL(" prog = gdb_program(objfile) if objfile is not None else None"), SL(" if prog is None:"), SL(" for o in gdb.objfiles():"), SL(" p = gdb_program(o)"), SL(" if p is not None:"), SL(" return p"), SL(" return prog"), SL(""), SL(" def raw_bytes(val):"), SL(" try:"), SL(" return bytes(val.bytes)"), SL(" except (AttributeError, gdb.error):"), SL(" pass"), SL(" if val.address is not None:"), SL(" return bytes(gdb.selected_inferior().read_memory(int(val.address),"), SL(" val.type.sizeof))"), SL(" raise Unreadable()"), SL(""), SL(" class ValuePrinter:"), SL(" \"\"\"A Star99 value, as Star99 prints it.\"\"\""), SL(""), SL(" def __init__(self, prog, desc, val, pointer=False):"), SL(" self.prog = prog"), SL(" self.desc = desc"), SL(" self.val = val"), SL(" self.pointer = pointer"), SL(""), SL(" def to_string(self):"), SL(" prog = self.prog"), SL(" gdb_settings(prog)"), SL(" try:"), SL(" if self.pointer:"), SL(" p = int(self.val)"), SL(" if not p:"), SL(" return \"0x0\""), SL(" return \"&\" + prog.show(self.desc, p)"), SL(" return prog.show(self.desc, None, raw_bytes(self.val))"), SL(" except (Unreadable, gdb.error, gdb.MemoryError):"), SL(" return \"\""), SL(""), SL(" def star99_type_name(t):"), SL(" \"\"\"The C name of a Star99 type: its typedef, or (tcc keeps no typedef"), SL(" names) the tag of its struct.\"\"\""), SL(" if t.code == gdb.TYPE_CODE_TYPEDEF:"), SL(" return t.name"), SL(" if t.code in (gdb.TYPE_CODE_STRUCT, gdb.TYPE_CODE_UNION):"), SL(" return t.tag or t.name"), SL(" return None"), SL(""), SL(" def lookup_printer(val):"), SL(" t = val.type.unqualified()"), SL(" pointer = False"), SL(" if t.code == gdb.TYPE_CODE_PTR:"), SL(" t, pointer = t.target().unqualified(), True"), SL(" name = star99_type_name(t)"), SL(" if name is None:"), SL(" return None"), SL(" prog = program_of(t)"), SL(" if prog is None or name not in prog.types:"), SL(" return None"), SL(" d = prog.desc_of_type(name)"), SL(" if d is None:"), SL(" return None"), SL(" return ValuePrinter(prog, d, val, pointer)"), SL(""), SL(" class TypeRecognizer:"), SL(" def recognize(self, t):"), SL(" name = star99_type_name(t)"), SL(" if name is None:"), SL(" return None"), SL(" prog = program_of(t)"), SL(" if prog is None or name not in prog.types:"), SL(" return None"), SL(" return prog.types[name][0]"), SL(""), SL(" class Star99TypePrinter:"), SL(" def __init__(self):"), SL(" self.name = \"star99\""), SL(" self.enabled = True"), SL(""), SL(" def instantiate(self):"), SL(" return TypeRecognizer()"), SL(""), SL(" # -- frames, in Star99 terms"), SL(""), SL(" class Var:"), SL(" def __init__(self, name, text, value):"), SL(" self.name = name"), SL(" self.text = text"), SL(" self.val = value"), SL(""), SL(" def symbol(self):"), SL(" return self.name"), SL(""), SL(" def value(self):"), SL(" return self.val if self.val is not None else self.text"), SL(""), SL(" def frame_program(frame):"), SL(" try:"), SL(" fn = frame.function()"), SL(" except (gdb.error, RuntimeError):"), SL(" return None, None"), SL(" if fn is None or fn.symtab is None:"), SL(" return None, None"), SL(" prog = gdb_program(fn.symtab.objfile)"), SL(" if prog is None or fn.name not in prog.fns:"), SL(" return None, None"), SL(" return prog, prog.fns[fn.name]"), SL(""), SL(" def frame_vars(frame, prog, entry, params):"), SL(" \"\"\"The Star99 parameters or locals visible at frame, as (name, text).\"\"\""), SL(" out = []"), SL(" try:"), SL(" block = frame.block()"), SL(" except (gdb.error, RuntimeError):"), SL(" return out"), SL(" line = frame.find_sal().line"), SL(" wanted = {}"), SL(" for v in entry.get(\"vars\", []):"), SL(" if bool(v[5]) == params:"), SL(" wanted.setdefault(v[0], []).append(v)"), SL(" seen = set()"), SL(" while block is not None:"), SL(" for sym in block:"), SL(" if not sym.is_variable and not sym.is_argument:"), SL(" continue"), SL(" if bool(sym.is_argument) != params: # a local may share a parameter's name"), SL(" continue"), SL(" c = sym.name"), SL(" if c in seen or c not in wanted:"), SL(" continue"), SL(" if not params and sym.line > line and block.function is None:"), SL(" continue # declared further on: no value yet"), SL(" seen.add(c)"), SL(" out.append((c, sym))"), SL(" if block.function is not None:"), SL(" break"), SL(" block = block.superblock"), SL(" if params: # in their Star99 order (tcc writes them backward)"), SL(" order = [v[0] for v in entry.get(\"vars\", [])]"), SL(" out.sort(key=lambda cs: order.index(cs[0]) if cs[0] in order else len(order))"), SL(" result = []"), SL(" for c, sym in out:"), SL(" entries = wanted[c]"), SL(" value = None"), SL(" try:"), SL(" val = sym.value(frame)"), SL(" text = None"), SL(" ctype = val.type.unqualified()"), SL(" cname = star99_type_name(ctype)"), SL(" for v in entries:"), SL(" how, desc = v[2], v[4]"), SL(" if how in (\"cell\", \"ptr\", \"ref\"):"), SL(" data = raw_bytes(val)"), SL(" text = prog.show_var(how, desc, None if how == \"ref\" else int(val),"), SL(" data if how == \"ref\" else None)"), SL(" elif cname in prog.types:"), SL(" d = prog.desc_of_type(cname)"), SL(" text = prog.show(d, None, raw_bytes(val)) if d else None"), SL(" else: # the C type does not say (tcc): the table does"), SL(" d = prog.desc_named(desc)"), SL(" if d is not None and d.size == val.type.sizeof:"), SL(" text = prog.show(d, None, raw_bytes(val))"), SL(" if text is not None:"), SL(" name = v[1]"), SL(" value = star99_value(prog, val, how, desc)"), SL(" break"), SL(" else:"), SL(" name = entries[0][1]"), SL(" if text is None:"), SL(" text = str(val)"), SL(" except (gdb.error, gdb.MemoryError, Unreadable, ValueError):"), SL(" name, text = entries[0][1], \"\""), SL(" result.append((name, text, value))"), SL(" # a closure's captured variables, through its environment"), SL(" if not params and entry.get(\"captures\"):"), SL(" try:"), SL(" env = closure_env(frame)"), SL(" members = prog.t.members(entry[\"env\"])"), SL(" for cname, name, how, tname, desc, param in entry[\"captures\"]:"), SL(" if cname not in members:"), SL(" continue"), SL(" cell = prog.word(env + members[cname][0])"), SL(" text = prog.show_var(how, desc, cell, None)"), SL(" value = None"), SL(" ctype = prog.ctype_of(desc)"), SL(" if how == \"cell\" and ctype is not None:"), SL(" value = gdb.Value(cell).cast(gdb.lookup_type(ctype).pointer())"), SL(" value = value.dereference()"), SL(" result.append((name, text, value))"), SL(" except (gdb.error, ValueError, Unreadable):"), SL(" pass"), SL(" return result"), SL(""), SL(" def closure_env(frame):"), SL(" \"\"\"A closure's environment: the parameter envp, which (unlike env) is"), SL(" right from the first instruction.\"\"\""), SL(" try:"), SL(" return int(frame.read_var(\"envp\"))"), SL(" except (gdb.error, ValueError):"), SL(" return int(frame.read_var(\"env\"))"), SL(""), SL(" def star99_value(prog, val, how, desc):"), SL(" \"\"\"The gdb value a variable stands for: its own, the one its cell or"), SL(" pointer leads to, or the target of its route; or None.\"\"\""), SL(" try:"), SL(" if how in (\"cell\", \"ptr\"):"), SL(" return val.dereference()"), SL(" if how == \"ref\":"), SL(" cname = prog.ctype_of(desc)"), SL(" at, what = prog.deref(raw_bytes(val))"), SL(" if cname is None or at is None:"), SL(" return None"), SL(" return gdb.Value(at).cast(gdb.lookup_type(cname).pointer()).dereference()"), SL(" return val"), SL(" except (gdb.error, Unreadable, RuntimeError):"), SL(" return None"), SL(""), SL(" class Star99Frame(FrameDecorator):"), SL(" def __init__(self, base, prog, entry):"), SL(" super(Star99Frame, self).__init__(base)"), SL(" self.prog = prog"), SL(" self.entry = entry"), SL(""), SL(" def function(self):"), SL(" return self.prog.fn_title(self.entry[\"c\"])"), SL(""), SL(" def frame_args(self):"), SL(" gdb_settings(self.prog)"), SL(" return [Var(n, t, v) for n, t, v in frame_vars(self.inferior_frame(), self.prog,"), SL(" self.entry, True)]"), SL(""), SL(" def frame_locals(self):"), SL(" gdb_settings(self.prog)"), SL(" return [Var(n, t, v) for n, t, v in frame_vars(self.inferior_frame(), self.prog,"), SL(" self.entry, False)]"), SL(""), SL(" class Star99FrameFilter:"), SL(" def __init__(self):"), SL(" self.name = \"star99\""), SL(" self.priority = 100"), SL(" self.enabled = True"), SL(""), SL(" def filter(self, frames):"), SL(" for f in frames:"), SL(" prog, entry = frame_program(f.inferior_frame())"), SL(" yield Star99Frame(f, prog, entry) if prog else f"), SL(""), SL(" # -- commands"), SL(""), SL(" class Star99Command(gdb.Command):"), SL(" \"\"\"Star99 debugging: star99 locals, star99 print EXPR, star99 break NAME.\"\"\""), SL(""), SL(" def __init__(self):"), SL(" super(Star99Command, self).__init__(\"star99\", gdb.COMMAND_DATA,"), SL(" gdb.COMPLETE_NONE, True)"), SL(""), SL(" class LocalsCommand(gdb.Command):"), SL(" \"\"\"The Star99 parameters and locals of the selected frame, with their values.\"\"\""), SL(""), SL(" def __init__(self):"), SL(" super(LocalsCommand, self).__init__(\"star99 locals\", gdb.COMMAND_DATA)"), SL(""), SL(" def invoke(self, arg, from_tty):"), SL(" frame = gdb.selected_frame()"), SL(" prog, entry = frame_program(frame)"), SL(" if prog is None:"), SL(" raise gdb.GdbError(\"not in Star99 code\")"), SL(" gdb_settings(prog)"), SL(" for params in (True, False):"), SL(" for name, text, value in frame_vars(frame, prog, entry, params):"), SL(" gdb.write(\"%s = %s\\n\" % (name, text))"), SL(""), SL(" def frame_resolver(frame):"), SL(" \"\"\"resolve(name) for Star99 expressions in frame.\"\"\""), SL(" prog, entry = frame_program(frame)"), SL(""), SL(" def resolve(name):"), SL(" if prog is not None:"), SL(" wanted = {}"), SL(" for v in entry.get(\"vars\", []):"), SL(" if v[1] == name:"), SL(" wanted.setdefault(v[0], []).append(v)"), SL(" block = frame.block()"), SL(" while block is not None:"), SL(" for sym in block:"), SL(" if sym.name in wanted and (sym.is_variable or sym.is_argument):"), SL(" val = sym.value(frame)"), SL(" for v in wanted[sym.name]:"), SL(" how, desc = v[2], v[4]"), SL(" if how in (\"cell\", \"ptr\", \"ref\"):"), SL(" data = raw_bytes(val)"), SL(" return prog.place_of_var(how, desc, None, data)"), SL(" cname = star99_type_name(val.type.unqualified())"), SL(" d = (prog.desc_of_type(cname) if cname in prog.types"), SL(" else prog.desc_named(desc))"), SL(" if d is not None and d.size == val.type.sizeof:"), SL(" if val.address is None:"), SL(" raise EvalError(name + \" has no address\")"), SL(" return d, int(val.address)"), SL(" if block.function is not None:"), SL(" break"), SL(" block = block.superblock"), SL(" for cname, cn, how, tname, desc, param in entry.get(\"captures\", []):"), SL(" if cn == name:"), SL(" env = closure_env(frame)"), SL(" members = prog.t.members(entry[\"env\"])"), SL(" return prog.place_of_capture(how, desc, env + members[cname][0])"), SL(" for o in gdb.objfiles():"), SL(" p2 = gdb_program(o)"), SL(" if p2 is None:"), SL(" continue"), SL(" for cname, gname, how, tname, desc, param in p2.globals.values():"), SL(" if gname == name:"), SL(" addr = p2.t.symbol_address(cname)"), SL(" if addr:"), SL(" return p2.desc_named(desc), addr"), SL(" return None"), SL(""), SL(" return prog, resolve"), SL(""), SL(" class PrintCommand(gdb.Command):"), SL(" \"\"\"star99 print EXPR: a Star99 expression, as Star99 prints its value."), SL("EXPR is a variable, then .field, [index], [key] or .* as often as needed,"), SL("or len(EXPR): star99 print ages[\"bob\"], star99 print shapes[1].w.\"\"\""), SL(""), SL(" def __init__(self):"), SL(" super(PrintCommand, self).__init__(\"star99 print\", gdb.COMMAND_DATA)"), SL(""), SL(" def invoke(self, arg, from_tty):"), SL(" frame = gdb.selected_frame()"), SL(" prog, resolve = frame_resolver(frame)"), SL(" if prog is None:"), SL(" for o in gdb.objfiles():"), SL(" prog = gdb_program(o)"), SL(" if prog is not None:"), SL(" break"), SL(" if prog is None:"), SL(" raise gdb.GdbError(\"no Star99 program\")"), SL(" gdb_settings(prog)"), SL(" try:"), SL(" gdb.write(prog.evaluate(arg, resolve) + \"\\n\")"), SL(" except (EvalError, Unreadable) as e:"), SL(" raise gdb.GdbError(str(e) or \"cannot read that\")"), SL(""), SL(" class BreakCommand(gdb.Command):"), SL(" \"\"\"star99 break NAME: a breakpoint on every Star99 function named NAME.\"\"\""), SL(""), SL(" def __init__(self):"), SL(" super(BreakCommand, self).__init__(\"star99 break\", gdb.COMMAND_BREAKPOINTS)"), SL(""), SL(" def invoke(self, arg, from_tty):"), SL(" name = arg.strip()"), SL(" found = 0"), SL(" for o in gdb.objfiles():"), SL(" prog = gdb_program(o)"), SL(" if prog is None:"), SL(" continue"), SL(" for c, f in prog.fns.items():"), SL(" if f[\"name\"] == name or (name == \"main\" and c == \"main\"):"), SL(" gdb.Breakpoint(c)"), SL(" found += 1"), SL(" if not found:"), SL(" raise gdb.GdbError(\"no Star99 function %s\" % name)"), SL(""), SL(" class Star99PrettyPrinter(gdb.printing.PrettyPrinter):"), SL(" def __init__(self):"), SL(" super(Star99PrettyPrinter, self).__init__(\"star99\", [])"), SL(""), SL(" def __call__(self, val):"), SL(" return lookup_printer(val)"), SL(""), SL(" skipped_files = set()"), SL(""), SL(" def skip_runtime(objfile):"), SL(" \"\"\"step goes over all that is not the program's own Star99 code: the"), SL(" runtime, checks, copies, the allocator and the standard library all"), SL(" live in the generated C file, which step skips.\"\"\""), SL(" sym = None"), SL(" if hasattr(objfile, \"lookup_static_symbol\"):"), SL(" sym = objfile.lookup_static_symbol(\"s99_fail_n\")"), SL(" if sym is None or sym.symtab is None:"), SL(" return"), SL(" name = sym.symtab.filename"), SL(" if name in skipped_files:"), SL(" return"), SL(" skipped_files.add(name)"), SL(" try:"), SL(" gdb.execute(\"skip file %s\" % name, to_string=True)"), SL(" # tcc -b calls its bounds checks from the program's own code"), SL(" gdb.execute(\"skip file bcheck.c\", to_string=True)"), SL(" except gdb.error:"), SL(" pass"), SL(""), SL(" def register(objfile):"), SL(" if gdb_program(objfile) is None:"), SL(" return"), SL(" if any(getattr(p, \"name\", \"\") == \"star99\" for p in objfile.pretty_printers):"), SL(" return"), SL(" gdb.printing.register_pretty_printer(objfile, Star99PrettyPrinter())"), SL(" gdb.types.register_type_printer(objfile, Star99TypePrinter())"), SL(" if hasattr(objfile, \"frame_filters\"):"), SL(" objfile.frame_filters[\"star99\"] = Star99FrameFilter()"), SL(" skip_runtime(objfile)"), SL(""), SL(" def on_new_objfile(event):"), SL(" register(event.new_objfile)"), SL(""), SL(" def on_clear_objfiles(event):"), SL(" programs.clear()"), SL(""), SL(" if not hasattr(gdb, \"star99_loaded\"):"), SL(" gdb.star99_loaded = VERSION"), SL(" Star99Command()"), SL(" LocalsCommand()"), SL(" PrintCommand()"), SL(" BreakCommand()"), SL(" gdb.events.new_objfile.connect(on_new_objfile)"), SL(" gdb.events.clear_objfiles.connect(on_clear_objfiles)"), SL(" gdb.execute(\"set print frame-arguments all\", to_string=True)"), SL(" if \"detect_leaks\" not in os.environ.get(\"ASAN_OPTIONS\", \"\"):"), SL(" # LeakSanitizer cannot work under a debugger"), SL(" gdb.execute(\"set environment ASAN_OPTIONS=%s\" % asan_options(), to_string=True)"), SL(" for o in gdb.objfiles():"), SL(" register(o)"), SL(" elif gdb.current_objfile() is not None:"), SL(" register(gdb.current_objfile())"), SL(""), SL(""), SL("# --- lldb --------------------------------------------------------------"), SL(""), SL("try:"), SL(" import lldb"), SL("except ImportError:"), SL(" lldb = None"), SL(""), SL("# Runtime functions and the helpers made per type (copies, releases,"), SL("# descriptors): stepping goes over them."), SL("HELPERS = (r\"^(s99_|copy[0-9]+$|drop[0-9]+$|celldrop[0-9]+$|init[0-9]+$|\""), SL(" r\"info(copy|drop|print|init|eq|arb|valid)[0-9]+$|envdrop_|wf[0-9]+_|\""), SL(" r\"__bound_|__bc_)\") # the last: tcc -b's bounds checks"), SL(""), SL("if lldb is not None and gdb is None:"), SL(""), SL(" class LldbTarget(Target):"), SL(" def __init__(self, target):"), SL(" self.target = target"), SL(" self.ptr = target.GetAddressByteSize() or 8"), SL(" self.syms = {}"), SL(""), SL(" def read(self, addr, n):"), SL(" err = lldb.SBError()"), SL(" process = self.target.GetProcess()"), SL(" if process.IsValid() and process.GetState() != lldb.eStateInvalid:"), SL(" data = process.ReadMemory(addr, n, err)"), SL(" else:"), SL(" data = self.target.ReadMemory(self.target.ResolveLoadAddress(addr), n, err)"), SL(" if err.Fail() or data is None or len(data) < n:"), SL(" raise Unreadable()"), SL(" return bytes(data)"), SL(""), SL(" def run_id(self):"), SL(" return self.target.GetProcess().GetProcessID()"), SL(""), SL(" def forget(self):"), SL(" self.syms = {}"), SL(""), SL(" def struct_type(self, name):"), SL(" t = self.target.FindFirstType(name)"), SL(" return t.GetCanonicalType() if t.IsValid() else None"), SL(""), SL(" def offset(self, struct_name, member):"), SL(" t = self.struct_type(struct_name)"), SL(" if t is None:"), SL(" return None"), SL(" for i in range(t.GetNumberOfFields()):"), SL(" f = t.GetFieldAtIndex(i)"), SL(" if f.GetName() == member:"), SL(" return f.GetOffsetInBytes()"), SL(" return None"), SL(""), SL(" def sizeof(self, struct_name):"), SL(" t = self.struct_type(struct_name)"), SL(" return t.GetByteSize() if t is not None else None"), SL(""), SL(" def symbol_address(self, name):"), SL(" if name in self.syms:"), SL(" return self.syms[name]"), SL(" addr = None"), SL(" vals = self.target.FindGlobalVariables(name, 1)"), SL(" if vals.GetSize():"), SL(" v = vals.GetValueAtIndex(0)"), SL(" addr = v.GetLoadAddress()"), SL(" if addr == lldb.LLDB_INVALID_ADDRESS:"), SL(" addr = v.GetAddress().GetFileAddress()"), SL(" self.syms[name] = addr"), SL(" return addr"), SL(""), SL(" def function_at(self, addr):"), SL(" a = self.target.ResolveLoadAddress(addr)"), SL(" f = a.GetFunction()"), SL(" if f.IsValid():"), SL(" return f.GetName()"), SL(" s = a.GetSymbol()"), SL(" return s.GetName() if s.IsValid() else None"), SL(""), SL(" def members(self, struct_name):"), SL(" t = self.struct_type(struct_name.replace(\"struct \", \"\"))"), SL(" if t is None:"), SL(" return {}"), SL(" out = {}"), SL(" for i in range(t.GetNumberOfFields()):"), SL(" f = t.GetFieldAtIndex(i)"), SL(" out[f.GetName()] = (f.GetOffsetInBytes(), f.GetType().GetByteSize())"), SL(" return out"), SL(""), SL(" lldb_programs = {} # target -> Program (or None)"), SL(""), SL(" def lldb_program(target):"), SL(" if target is None or not target.IsValid():"), SL(" return None"), SL(" key = target.GetExecutable().fullpath"), SL(" if key in lldb_programs:"), SL(" prog = lldb_programs[key]"), SL(" if prog is not None:"), SL(" prog.fresh()"), SL(" return prog"), SL(" prog = None"), SL(" vals = target.FindGlobalVariables(\"s99_debug_info\", 1)"), SL(" if vals.GetSize():"), SL(" v = vals.GetValueAtIndex(0)"), SL(" err = lldb.SBError()"), SL(" data = v.GetData().ReadRawData(err, 0, v.GetByteSize())"), SL(" if not err.Fail() and data:"), SL(" try:"), SL(" prog = Program(LldbTarget(target), json.loads(bytes(data).rstrip(b\"\\0\")))"), SL(" except ValueError:"), SL(" prog = None"), SL(" lldb_programs[key] = prog"), SL(" if prog is not None:"), SL(" prog.fresh()"), SL(" return prog"), SL(""), SL(" def lldb_type_name(t):"), SL(" \"\"\"The C name of t: its typedef as written (GetUnqualifiedType() would"), SL(" resolve a typedef of a pointer), without qualifiers or struct.\"\"\""), SL(" name = t.GetName() or \"\""), SL(" for q in (\"const \", \"volatile \", \"struct \"):"), SL(" while name.startswith(q):"), SL(" name = name[len(q):]"), SL(" return name"), SL(""), SL(" def lldb_value_bytes(valobj):"), SL(" err = lldb.SBError()"), SL(" data = valobj.GetData().ReadRawData(err, 0, valobj.GetByteSize())"), SL(" if err.Fail() or data is None:"), SL(" raise Unreadable()"), SL(" return bytes(data)"), SL(""), SL(" def lldb_summary(valobj, internal_dict):"), SL(" \"\"\"Summary provider for every Star99 type: the value as Star99 prints it.\"\"\""), SL(" prog = lldb_program(valobj.GetTarget())"), SL(" if prog is None:"), SL(" return None"), SL(" t = valobj.GetType()"), SL(" name = lldb_type_name(t)"), SL(" pointer = False"), SL(" if name not in prog.types and t.IsPointerType(): # a cell: T *"), SL(" name = lldb_type_name(t.GetPointeeType())"), SL(" pointer = True"), SL(" d = prog.desc_of_type(name)"), SL(" if d is None:"), SL(" return \"\""), SL(" try:"), SL(" if pointer:"), SL(" p = valobj.GetValueAsUnsigned()"), SL(" return \"&\" + prog.show(d, p) if p else \"0x0\""), SL(" return prog.show(d, None, lldb_value_bytes(valobj))"), SL(" except Unreadable:"), SL(" return \"\""), SL(""), SL(" def lldb_frame_vars(frame, prog, entry, params):"), SL(" out = []"), SL(" line = frame.GetLineEntry().GetLine()"), SL(" wanted = {}"), SL(" for v in entry.get(\"vars\", []):"), SL(" if bool(v[5]) == params:"), SL(" wanted.setdefault(v[0], []).append(v)"), SL(" seen = set()"), SL(" for val in frame.GetVariables(params, not params, False, True):"), SL(" c = val.GetName()"), SL(" if c in seen or c not in wanted:"), SL(" continue"), SL(" decl = val.GetDeclaration()"), SL(" if not params and decl.IsValid() and decl.GetLine() > line:"), SL(" continue"), SL(" seen.add(c)"), SL(" text, name = None, wanted[c][0][1]"), SL(" try:"), SL(" data = lldb_value_bytes(val)"), SL(" cname = lldb_type_name(val.GetType())"), SL(" for v in wanted[c]:"), SL(" how, desc = v[2], v[4]"), SL(" if how in (\"cell\", \"ptr\"):"), SL(" text = prog.show_var(how, desc, prog.num(data, 0, \"p\"), None)"), SL(" elif how == \"ref\":"), SL(" text = prog.show_var(how, desc, None, data)"), SL(" else:"), SL(" d = prog.desc_of_type(cname) or prog.desc_named(desc)"), SL(" if d is not None and d.size == len(data):"), SL(" text = prog.show(d, None, data)"), SL(" if text is not None:"), SL(" name = v[1]"), SL(" break"), SL(" except Unreadable:"), SL(" text = \"\""), SL(" out.append((name, text if text is not None else str(val.GetValue())))"), SL(" if not params and entry.get(\"captures\"):"), SL(" env = frame.FindVariable(\"envp\")"), SL(" if not env.IsValid():"), SL(" env = frame.FindVariable(\"env\")"), SL(" if env.IsValid():"), SL(" out += prog.captures(entry, env.GetValueAsUnsigned(), 0)"), SL(" return out"), SL(""), SL(" def lldb_resolver(frame, prog, entry):"), SL(" def resolve(name):"), SL(" if entry is not None:"), SL(" for val in frame.GetVariables(True, True, False, True):"), SL(" for v in entry.get(\"vars\", []):"), SL(" if v[0] != val.GetName() or v[1] != name:"), SL(" continue"), SL(" how, desc = v[2], v[4]"), SL(" data = lldb_value_bytes(val)"), SL(" if how in (\"cell\", \"ptr\", \"ref\"):"), SL(" return prog.place_of_var(how, desc, None, data)"), SL(" d = prog.desc_of_type(lldb_type_name(val.GetType()))"), SL(" d = d or prog.desc_named(desc)"), SL(" if d is not None and d.size == len(data):"), SL(" return d, val.GetLoadAddress()"), SL(" for cname, cn, how, tname, desc, param in entry.get(\"captures\", []):"), SL(" if cn == name:"), SL(" env = frame.FindVariable(\"envp\").GetValueAsUnsigned()"), SL(" members = prog.t.members(entry[\"env\"])"), SL(" return prog.place_of_capture(how, desc, env + members[cname][0])"), SL(" for cname, gname, how, tname, desc, param in prog.globals.values():"), SL(" if gname == name:"), SL(" addr = prog.t.symbol_address(cname)"), SL(" if addr:"), SL(" return prog.desc_named(desc), addr"), SL(" return None"), SL(" return resolve"), SL(""), SL(" def lldb_star99(debugger, command, result, internal_dict):"), SL(" \"\"\"star99 locals | star99 print EXPR | star99 break NAME | star99 load\"\"\""), SL(" target = debugger.GetSelectedTarget()"), SL(" prog = lldb_program(target)"), SL(" if prog is None:"), SL(" result.SetError(\"no Star99 debug build is loaded\")"), SL(" return"), SL(" words = command.strip().split(None, 1)"), SL(" what = words[0] if words else \"\""), SL(" arg = words[1] if len(words) > 1 else \"\""), SL(" frame = target.GetProcess().GetSelectedThread().GetSelectedFrame()"), SL(" entry = prog.fns.get(frame.GetFunctionName()) if frame.IsValid() else None"), SL(" if what == \"locals\":"), SL(" if entry is None:"), SL(" result.SetError(\"not in Star99 code\")"), SL(" return"), SL(" for params in (True, False):"), SL(" for name, text in lldb_frame_vars(frame, prog, entry, params):"), SL(" result.AppendMessage(\"%s = %s\" % (name, text))"), SL(" elif what in (\"print\", \"p\"):"), SL(" try:"), SL(" result.AppendMessage(prog.evaluate(arg, lldb_resolver(frame, prog, entry)))"), SL(" except (EvalError, Unreadable) as e:"), SL(" result.SetError(str(e) or \"cannot read that\")"), SL(" elif what == \"break\":"), SL(" n = 0"), SL(" for c, f in prog.fns.items():"), SL(" if f[\"name\"] == arg or (arg == \"main\" and c == \"main\"):"), SL(" target.BreakpointCreateByName(c)"), SL(" n += 1"), SL(" if n:"), SL(" result.AppendMessage(\"%d breakpoints\" % n)"), SL(" else:"), SL(" result.SetError(\"no Star99 function \" + arg)"), SL(" elif what == \"load\":"), SL(" lldb_setup(debugger)"), SL(" else:"), SL(" result.SetError(\"star99 locals | star99 print EXPR | star99 break NAME | star99 load\")"), SL(""), SL(" def lldb_setup(debugger):"), SL(" \"\"\"Formatters and stepping rules for the Star99 program in the target.\"\"\""), SL(" target = debugger.GetSelectedTarget()"), SL(" lldb_programs.clear()"), SL(" prog = lldb_program(target)"), SL(" run = lambda c: debugger.HandleCommand(c)"), SL(" run(\"type category define star99\")"), SL(" if prog is not None:"), SL(" for c in prog.types:"), SL(" run(\"type summary add -v -w star99 -F %s.lldb_summary %s\" % (__name__, c))"), SL(" avoid = [HELPERS] + [\"^%s$\" % c for c, f in prog.fns.items() if f[\"library\"]]"), SL(" run(\"settings set target.process.thread.step-avoid-regexp %s\" % \"|\".join(avoid))"), SL(" run(\"type category enable star99\")"), SL(""), SL(" def __lldb_init_module(debugger, internal_dict):"), SL(" debugger.HandleCommand(\"command script add -o -f %s.lldb_star99 star99\" % __name__)"), SL(" if \"detect_leaks\" not in os.environ.get(\"ASAN_OPTIONS\", \"\"):"), SL(" # LeakSanitizer cannot work under a debugger"), SL(" debugger.HandleCommand(\"settings set target.env-vars ASAN_OPTIONS=%s\" %"), SL(" asan_options())"), SL(" lldb_setup(debugger)"), }; // Code generation static STATE Buf out_types = {0}, out_protos = {0}, out_helpers = {0}, out_globals = {0}, out_fns = {0}, out_fuzz = {0}; static STATE Map *arbitraries = 0; // type id -> fn arbitrary(d: Draw) -> T static STATE Map *valids = 0; // type id -> fn valid(x: T) -> Bool static Str int_limits(Type *t, Str which); static STATE int tmpcount = 0; static STATE Pos dbg_at = {-1, 0, 0, 0}; // debug builds: the .star99 line code stands for static STATE b32 dbg_framed = 0; // the function being generated has a shadow-stack frame enum { H_COPY = 1 << 0, H_DROP = 1 << 1, H_INIT = 1 << 4, H_INFO = 1 << 5, H_ZERO = 1 << 6, H_CELLDROP = 1 << 13, // drops the value in a counted cell }; static Str ctype(Type *t); static void define_type(Type *t); static b32 compact_union(Type *u); static b32 optimizing(void); static b32 loop_area(Node *n); static void enter_area(void); static b32 heavy(Type *t); static void use_fn(Fn *f); static STATE Fns pending_fns = {0}; static b32 place_allocates(Node *n); static Str c_string(Str s); static void enc_expr(Buf *b, Node *n); static void enc_decl(Buf *b, Node *n); static void need(Type *t, u32 helper); static Str tmpname(void) { return fmtstr("t%d", ++tmpcount); } static Str ctype(Type *t) { if (t->cname.len) return t->cname; switch (t->kind) { case TY_VOID: t->cname = S("void"); break; case TY_BOOL: t->cname = S("bool"); break; case TY_INT: t->cname = fmtstr("%sint%d_t", t->issigned ? S("") : S("u"), t->bits); break; case TY_FLOAT: t->cname = S("double"); break; case TY_LIST: t->cname = S("s99_list *"); break; case TY_MAP: t->cname = S("s99_map *"); break; case TY_POOL: t->cname = S("s99_pool *"); break; case TY_FILE: t->cname = S("s99_stream *"); break; case TY_DRAW: t->cname = S("s99_draw *"); break; case TY_TYPE: t->cname = S("const s99_type *"); break; case TY_REF: t->cname = S("s99_ref"); break; case TY_ANY: t->cname = S("s99_any"); break; case TY_FN: t->cname = S("s99_fn"); break; case TY_NIL: t->cname = S("s99_ref"); break; case TY_ARRAY: case TY_STRUCT: case TY_UNION: t->cname = fmtstr("T%d", t->id); bputf(&out_types, "typedef struct T%d T%d;", t->id, t->id); if (t->named) bputf(&out_types, " /* %s */", type_name(t)); bputc(&out_types, '\n'); break; default: t->cname = S("int"); break; } if (debugging() && t->kind != TY_VOID && t->kind != TY_DRAW && t->kind != TY_NIL && t->kind != TY_ARRAY && t->kind != TY_STRUCT && t->kind != TY_UNION && !equals(t->cname, S("int"))) { // a debugger tells the types apart by name: List(Int) from String bputf(&out_types, "typedef %s T%d; /* %s */\n", t->cname, t->id, type_name(t)); t->cname = fmtstr("T%d", t->id); } return t->cname; } static STATE int define_depth = 0; static STATE b32 defining_later = 0; static STATE struct { Type **data; iz len; iz cap; } define_later = {0}; static void define_type(Type *t) { if (t->kind != TY_ARRAY && t->kind != TY_STRUCT && t->kind != TY_UNION) { ctype(t); return; } if (t->state == 2) return; if (t->state == 3) return; // being defined: a dependency through a pointer ctype(t); t->state = 3; define_depth++; Buf b = {0}; if (t->kind == TY_ARRAY) { define_type(t->elem); bputf(&b, "struct T%d {\n %s a[%lld];\n};\n", t->id, ctype(t->elem), (long long)(t->count ? t->count : 1)); } else if (t->kind == TY_STRUCT) { for (iz i = 0; i < t->fields.len; i++) define_type(t->fields.data[i].type); bputf(&b, "struct T%d {\n", t->id); if (!t->fields.len) bput(&b, S(" char unused;\n")); for (iz i = 0; i < t->fields.len; i++) { Field *f = t->fields.data + i; bputf(&b, " %s f_%s;\n", ctype(f->type), cident(f->name)); } bput(&b, S("};\n")); } else { // tag 0 is nil, tag i + 1 is variant i; a no_nil union uses tag i for (iz i = 0; i < t->fields.len; i++) { if (t->fields.data[i].type) ctype(t->fields.data[i].type); } if (compact_union(t)) bputf(&b, "struct T%d {\n s99_box *box; /* compact: null is nil */\n};\n", t->id); else bputf(&b, "struct T%d {\n int32_t tag;\n s99_box *box;\n};\n", t->id); } bput(&out_types, bstr(&b)); t->state = 2; // the variants of a union live in boxes: they are defined once every // struct being defined now is, since one of them may hold a struct // that holds this union (Call holds Expr, which may hold a TypeCall, // which holds a Call) for (iz i = 0; t->kind == TY_UNION && i < t->fields.len; i++) if (t->fields.data[i].type) *push(&perm, &define_later) = t->fields.data[i].type; if (--define_depth || defining_later) return; defining_later = 1; for (iz i = 0; i < define_later.len; i++) define_type(define_later.data[i]); define_later.len = 0; defining_later = 0; } // union (T): one variant with a payload, and nil. It is stored as the box // pointer alone (null for nil), so an optional child costs one word. static b32 compact_union(Type *u) { return optimizing() && u->kind == TY_UNION && !u->nonil && u->fields.len == 1 && u->fields.data[0].type && u->fields.data[0].type != t_void; } // The tag of the union at pointer p. static Str utag(Type *u, Str p) { if (compact_union(u)) return fmtstr("(int)((%s)->box != 0)", p); return fmtstr("(%s)->tag", p); } static int union_tag(Type *u, iz variant) { return u->nonil ? (int)variant : (int)variant + 1; } static Str helper(Type *t, Str kind) { ctype(t); switch (t->kind) { case TY_ANY: return fmtstr("s99_any_%s", kind); default: return fmtstr("%s%d", kind, t->id); } } // C statements that copy a value into uninitialized storage, release it, and // so on. Plain values need nothing beyond assignment. static STATE b32 in_helper = 0; static Str copy_stmt(Type *t, Str dst, Str src) { if (t->managed) need(t, H_COPY); b32 remap = t->has_refs && !in_helper; // references into itself: copy, then move them over if (remap) need(t, H_INFO); Str copy = t->managed ? fmtstr("%s(&%s, &%s);", helper(t, S("copy")), dst, src) : fmtstr("%s = %s;", dst, src); if (!remap) return copy; return fmtstr( "s99_remap_begin(&%s, &%s, sizeof(%s)); %s s99_fix(&info%d, &%s); s99_remap_end();", src, dst, src, copy, t->id, dst); } static Str drop_stmt(Type *t, Str v) { if (!t->managed) return (Str){0}; need(t, H_DROP); return fmtstr("%s(&%s);", helper(t, S("drop")), v); } static Str gen_zero(Type *t); static Str user_print(Type *t); static void need_body(Type *t, u32 h); static void gen_default_into(Buf *b, Node *def, Str dst); // The types that have a descriptor (info%d) in this program static STATE struct { Type **data; iz len; iz cap; } described = {0}; // The == overload a program declares for t (both sides t), if any static Fn * user_equality(Type *t) { if (!t || (t->kind != TY_STRUCT && t->kind != TY_UNION)) return 0; for (iz i = 0; i < all_fns.len; i++) { Fn *f = all_fns.data[i]; if (f->builtin || !f->checked || f->ismacro || !equals(f->name, S("==")) || !f->type) continue; Fields *ps = &f->type->fields; if (ps->len == 2 && ps->data[0].type == t && ps->data[1].type == t && !ps->data[0].isvar && !ps->data[1].isvar) return f; } return 0; } // Generated helper functions, written once per type on first use. static void need(Type *t, u32 h) { if (t->helpers & h) return; t->helpers |= h; define_type(t); b32 save_helper = in_helper; in_helper = 1; Pos save_dbg = dbg_at; // a helper stands for no .star99 line b32 save_framed = dbg_framed; dbg_at = (Pos){-1, 0, 0, 0}; dbg_framed = 0; need_body(t, h); dbg_at = save_dbg; dbg_framed = save_framed; in_helper = save_helper; } static void need_body(Type *t, u32 h) { Str c = ctype(t); i32 id = t->id; Buf b = {0}; switch (h) { case H_COPY: { if (t->kind == TY_ANY) return; bputf(&out_protos, "static S99_UNUSED void copy%d(%s *dst, %s *src);\n", id, c, c); bputf(&b, "static S99_UNUSED void\ncopy%d(%s *dst, %s *src)\n{\n", id, c, c); if (!t->managed) bput(&b, S(" *dst = *src;\n")); // for Any, which copies any type switch (t->managed ? t->kind : TY_VOID) { case TY_FN: bput(&b, S(" *dst = *src;\n if (dst->owns) s99_rc_retain(dst->env);\n")); break; case TY_LIST: bput(&b, S(" *dst = 0;\n if (!*src) return;\n")); bputf(&b, " s99_list *d = s99_list_new(sizeof(%s));\n", ctype(t->elem)); bput(&b, S(" s99_list_reserve(d, (*src)->len);\n d->len = (*src)->len;\n")); if (t->elem->managed) { bputf(&b, " for (int64_t i = 0; i < d->len; i++) %s\n", copy_stmt(t->elem, fmtstr("((%s *)d->data)[i]", ctype(t->elem)), fmtstr("((%s *)(*src)->data)[i]", ctype(t->elem)))); } else { bput( &b, S(" if (d->len) memcpy(d->data, (*src)->data, (size_t)(d->len * d->elemsize));\n")); } bput(&b, S(" s99_remap_add(*src, d, d->gen);\n *dst = d;\n")); break; case TY_ARRAY: bputf(&b, " for (int64_t i = 0; i < %lld; i++) %s\n", (long long)t->count, copy_stmt(t->elem, S("dst->a[i]"), S("src->a[i]"))); break; case TY_STRUCT: for (iz i = 0; i < t->fields.len; i++) { Field *f = t->fields.data + i; Str fn = cident(f->name); bputf(&b, " %s\n", copy_stmt(f->type, fmtstr("dst->f_%s", fn), fmtstr("src->f_%s", fn))); } break; case TY_UNION: bput( &b, S(" dst->tag = src->tag;\n dst->box = 0;\n if (!src->box) return;\n switch (src->tag) {\n")); for (iz i = 0; i < t->fields.len; i++) { Type *pt = t->fields.data[i].type; if (!pt || pt == t_void) continue; Str pc = ctype(pt); bputf( &b, " case %d:\n dst->box = s99_box_filled(sizeof(%s));\n s99_remap_add(src->box, dst->box, dst->box->gen);\n", union_tag(t, i), pc); bputf(&b, " %s\n break;\n", copy_stmt(pt, fmtstr("*(%s *)S99_BOXDATA(dst->box)", pc), fmtstr("*(%s *)S99_BOXDATA(src->box)", pc))); } bput(&b, S(" default: break;\n }\n")); break; case TY_MAP: { Type *k = t->key, *v = t->elem; Str kc = ctype(k), vc = ctype(v); bput(&b, S(" *dst = 0;\n if (!*src) return;\n")); bputf(&b, " s99_map *d = s99_map_new(sizeof(%s), sizeof(%s), %d);\n", kc, vc, k->text ? 1 : k->kind == TY_BOOL ? 2 : 0); bput( &b, S(" for (int64_t i = 0; i < (*src)->norder; i++) {\n s99_mapent *e = (*src)->order[i];\n" " if (!e->alive) {\n s99_map_order_add(d, &s99_gone); /* the places stay */\n" " continue;\n }\n bool created;\n" " s99_mapent *ne = s99_map_find(d, s99_map_key(e), true, &created);\n" " s99_gen_set(ne, s99_gen_get(e)); /* versions are copied with the map */\n")); bputf(&b, " %s\n", copy_stmt(k, fmtstr("*(%s *)s99_map_key(ne)", kc), fmtstr("*(%s *)s99_map_key(e)", kc))); bputf(&b, " %s\n }\n", copy_stmt(v, fmtstr("*(%s *)s99_map_val(d, ne)", vc), fmtstr("*(%s *)s99_map_val(*src, e)", vc))); bput(&b, S(" s99_remap_add(*src, d, 0);\n *dst = d;\n")); break; } case TY_POOL: { Str ec = ctype(t->elem); bput(&b, S(" *dst = 0;\n if (!*src) return;\n")); bputf(&b, " s99_pool *d = s99_pool_new(sizeof(%s));\n", ec); bput(&b, S(" d->len = d->cap = (*src)->len;\n d->freehead = (*src)->freehead;\n" " d->slots = (uint8_t *)s99_alloc((size_t)(d->cap * d->slotsize));\n" " for (int64_t i = 0; i < d->len; i++) {\n" " s99_slot *s = (s99_slot *)((*src)->slots + i * d->slotsize);\n" " s99_slot *n = (s99_slot *)(d->slots + i * d->slotsize);\n" " n->version = s->version;\n n->nextfree = s->nextfree;\n" " if (!s->version) continue;\n")); bputf(&b, " %s\n }\n s99_remap_add(*src, d, 0);\n *dst = d;\n", copy_stmt(t->elem, fmtstr("*(%s *)S99_SLOTDATA(n)", ec), fmtstr("*(%s *)S99_SLOTDATA(s)", ec))); break; } default: break; } bput(&b, S("}\n")); break; } case H_DROP: { if (t->kind == TY_ANY) return; bputf(&out_protos, "static S99_UNUSED void drop%d(%s *v);\n", id, c); bputf(&b, "static S99_UNUSED void\ndrop%d(%s *v)\n{\n", id, c); if (!t->managed) bput(&b, S(" (void)v;\n")); switch (t->managed ? t->kind : TY_VOID) { case TY_FN: bput(&b, S(" if (v->owns) s99_rc_release(v->env);\n v->owns = 0;\n")); break; case TY_LIST: bput(&b, S(" if (!*v) return;\n")); if (t->elem->managed) bputf(&b, " for (int64_t i = 0; i < (*v)->len; i++) %s\n", drop_stmt(t->elem, fmtstr("((%s *)(*v)->data)[i]", ctype(t->elem)))); bput(&b, S(" s99_list_free(*v);\n *v = 0;\n")); break; case TY_ARRAY: bputf(&b, " for (int64_t i = 0; i < %lld; i++) %s\n", (long long)t->count, drop_stmt(t->elem, S("v->a[i]"))); break; case TY_STRUCT: for (iz i = 0; i < t->fields.len; i++) { Field *f = t->fields.data + i; if (f->type->managed) bputf(&b, " %s\n", drop_stmt(f->type, fmtstr("v->f_%s", cident(f->name)))); } break; case TY_UNION: bput(&b, S(" if (v->box) {\n switch (v->tag) {\n")); for (iz i = 0; i < t->fields.len; i++) { Type *pt = t->fields.data[i].type; if (!pt || !pt->managed) continue; bputf(&b, " case %d: %s break;\n", union_tag(t, i), drop_stmt(pt, fmtstr("*(%s *)S99_BOXDATA(v->box)", ctype(pt)))); } bput( &b, S(" default: break;\n }\n s99_box_free(v->box);\n }\n v->box = 0;\n")); break; case TY_MAP: { Type *k = t->key, *vt = t->elem; bput(&b, S(" if (!*v) return;\n")); if (k->managed || vt->managed) { bput( &b, S(" for (int64_t i = 0; i < (*v)->norder; i++) {\n s99_mapent *e = (*v)->order[i];\n" " if (!e->alive) continue;\n")); if (k->managed) bputf(&b, " %s\n", drop_stmt(k, fmtstr("*(%s *)s99_map_key(e)", ctype(k)))); if (vt->managed) bputf(&b, " %s\n", drop_stmt(vt, fmtstr("*(%s *)s99_map_val(*v, e)", ctype(vt)))); bput(&b, S(" }\n")); } bput(&b, S(" s99_map_free(*v);\n *v = 0;\n")); break; } case TY_POOL: bput(&b, S(" if (!*v) return;\n")); if (t->elem->managed) { bput(&b, S(" for (int64_t i = 0; i < (*v)->len; i++) {\n" " s99_slot *s = (s99_slot *)((*v)->slots + i * (*v)->slotsize);\n" " if (!s->version) continue;\n")); bputf(&b, " %s\n }\n", drop_stmt(t->elem, fmtstr("*(%s *)S99_SLOTDATA(s)", ctype(t->elem)))); } bput( &b, S(" s99_xfree((*v)->slots);\n s99_unblock(*v, sizeof(s99_pool));\n *v = 0;\n")); break; default: break; } bput(&b, S("}\n")); break; } case H_ZERO: // a zero value to read union payloads from before they are stored bputf(&out_protos, "static S99_UNUSED %s z%d;\n", c, id); for (iz i = 0; t->kind == TY_UNION && i < t->fields.len; i++) { if (t->fields.data[i].type) need(t->fields.data[i].type, H_ZERO); } return; case H_INFO: // a descriptor: declared now, defined by gen_descriptors at the end bputf(&out_protos, "static S99_UNUSED const s99_type info%d;\n", id); *push(&perm, &described) = t; if (t->elem) need(t->elem, H_INFO); if (t->key) need(t->key, H_INFO); for (iz i = 0; (t->kind == TY_STRUCT || t->kind == TY_UNION) && i < t->fields.len; i++) { Type *ft = t->fields.data[i].type; if (ft && ft != t_void) need(ft, H_INFO); } user_print(t); use_fn(lookup(arbitraries, fmtstr("%d", id))); use_fn(lookup(valids, fmtstr("%d", id))); if (t->kind == TY_STRUCT && t->has_defaults) need(t, H_INIT); return; case H_CELLDROP: need(t, H_DROP); bputf(&out_protos, "static void celldrop%d(void *v);\n", id); bputf(&b, "static void\ncelldrop%d(void *v)\n{\n drop%d((%s *)v);\n}\n", id, id, c); break; case H_INIT: { // the starting value of a type with defaults bputf(&out_protos, "static S99_UNUSED void init%d(%s *v);\n", id, c); bputf(&b, "static S99_UNUSED void\ninit%d(%s *v)\n{\n memset(v, 0, sizeof(*v));\n", id, c); if (t->kind == TY_STRUCT) { for (iz i = 0; i < t->fields.len; i++) { Field *f = t->fields.data + i; Str fname = fmtstr("v->f_%s", cident(f->name)); if (f->def) { gen_default_into(&b, f->def, fname); } else if (f->type->has_defaults) { need(f->type, H_INIT); bputf(&b, " init%d(&%s);\n", f->type->id, fname); } } } else if (t->kind == TY_ARRAY) { need(t->elem, H_INIT); bputf(&b, " for (int64_t i = 0; i < %lld; i++) init%d(&v->a[i]);\n", (long long)t->count, t->elem->id); } else if (t->kind == TY_UNION && t->nonil) { Type *pt = t->fields.data[0].type; if (pt && pt != t_void && pt->has_defaults) { need(pt, H_INIT); bputf( &b, " v->box = s99_box_new(sizeof(%s));\n init%d((%s *)S99_BOXDATA(v->box));\n", ctype(pt), pt->id, ctype(pt)); } } bput(&b, S("}\n")); break; } } if (compact_union(t)) { // the helpers of a compact union read its tag from the box pointer Str s = bstr(&b); s = replace_all(s, S(" dst->tag = src->tag;\n"), S("")); static Str ptrs[] = {SL("src"), SL("dst"), SL("v")}; for (iz i = 0; i < countof(ptrs); i++) s = replace_all(s, fmtstr("%s->tag", ptrs[i]), fmtstr("(int)(%s->box != 0)", ptrs[i])); bput(&out_helpers, s); return; } bput(&out_helpers, bstr(&b)); } // Expressions and statements typedef struct Frame Frame; typedef struct StmtCtx StmtCtx; struct StmtCtx { Strs drops; // C statements releasing this statement's temporaries // The context this one is open inside, and the frame open when it began: // a jump out of that frame releases these temporaries too. StmtCtx *parent; Frame *frame; }; struct Frame { Frame *parent; Syms locals; // managed locals to release, in declaration order Nodes defers; Strs exits; // C statements run when control leaves the frame Node *loop; // the loop or labeled block this frame belongs to i32 label; // the C label number of that loop or block b32 isfn; b32 isarea; // a loop step's area: everything inside is given back at its end b32 used_break, used_cont; }; static STATE Buf *body = 0; static STATE int indent = 0; static STATE StmtCtx *sctx = 0; static STATE Frame *frame = 0; static STATE Fn *genfn = 0; static STATE i32 label_counter = 0; static STATE Map *print_overloads = 0; // type id -> Fn static Str gen_expr(Node *n); static Str gen_lvalue(Node *n); static Str gen_ref(Node *n); static void gen_stmt(Node *n); static void gen_stmt_in(Node *n); static void gen_block_stmts(Node *b, b32 newframe); static Str gen_yield(Node *n, Call *c); static Str gen_inline_call(Node *n, Call *c); static b32 co_yields_in(Nodes *list, Node *n); static Pos loc_pos(Str w); // Build facts, read from the code generated for each function: the run-time // checks it has (allocates(f) and checks(f) in build), and the runtime // functions that may allocate, worked out once from the runtime itself. static STATE Pos gen_at = {-1, 0, 0, 0}; static STATE b32 building = 0; // build(b) runs: the program is generated // and the parts of its Build that with_main(b, name) gives again static STATE Str build_cpath = {0}, build_sources = {0}, build_strings = {0}; // Which runtime functions may allocate, or run a print or == overload of // the program: worked out when the generated code first calls each one, // from the s99_ functions its lines call and those they call. A function's // lines are the indented ones after its "s99_name(" line, up to the next // such line. Made from runtime_lines, so kept across fuzz cases. enum { AW_FNS = 1024, AW_CALLS = 16384, AW_SLOTS = 2048 }; static struct { b32 ready; int n, ncalls; i32 line[AW_FNS + 1]; // each function's "s99_name(" line; line[n] is the end Str name[AW_FNS]; i32 slot[AW_SLOTS]; b32 read[AW_FNS], mallocs[AW_FNS], hooks_here[AW_FNS], seed[AW_FNS]; i32 first[AW_FNS], count[AW_FNS], callee[AW_CALLS]; int allocates[AW_FNS], hooks[AW_FNS]; // 0 unknown, 1 yes, 2 no int kind[AW_FNS]; // check_kind plus one, 0 unknown; fail: by its text } aw = {0}; static int aw_find(Str name) { for (u64 i = rt_hash(name);; i++) { int f = aw.slot[i & (AW_SLOTS - 1)] - 1; if (f < 0 || equals(aw.name[f], name)) return f; } } static b32 aw_seed(Str fn) { static Str seeds[] = {SL("s99_alloc"), SL("s99_realloc"), SL("s99_block"), SL("s99_xalloc"), SL("s99_xrealloc"), SL("s99_area_bump"), SL("s99_cell"), SL("s99_rc_new"), SL("s99_box_new"), SL("malloc"), SL("calloc"), SL("realloc")}; for (iz i = 0; i < countof(seeds); i++) if (equals(fn, seeds[i])) return 1; return 0; } static void aw_index(void) { if (aw.ready) return; aw.ready = 1; for (iz i = 1; i < countof(runtime_lines) && aw.n < AW_FNS; i++) { Str l = runtime_lines[i]; if (!startswith(l, S("s99_")) || indexof(l, S("(")) < 0 || startswith(runtime_lines[i - 1], S(" "))) continue; int f = aw.n++; aw.line[f] = (i32)i; aw.name[f] = substr(l, 0, indexof(l, S("("))); u64 h = rt_hash(aw.name[f]); while (aw.slot[h & (AW_SLOTS - 1)]) h++; aw.slot[h & (AW_SLOTS - 1)] = f + 1; } aw.line[aw.n] = (i32)countof(runtime_lines); for (int f = 0; f < aw.n; f++) aw.seed[f] = aw_seed(aw.name[f]); } // The functions f's lines call, and whether they call malloc or a hook. static void aw_read(int f) { if (aw.read[f]) return; aw.read[f] = 1; aw.first[f] = aw.ncalls; u8 *more = name_bytes(); for (iz i = aw.line[f] + 1; i < aw.line[f + 1]; i++) { Str l = runtime_lines[i]; if (!startswith(l, S(" "))) continue; for (iz c = 0;; c++) { iz k = indexof(substr(l, c, l.len), S("s99_")); if (k < 0) break; c += k; if (c && more[l.data[c - 1]]) continue; iz e = c; while (e < l.len && more[l.data[e]]) e++; if (e == l.len || l.data[e] != '(') continue; int g = aw_find(substr(l, c, e)); if (g >= 0 && aw.ncalls < AW_CALLS) aw.callee[aw.ncalls++] = g; } if (indexof(l, S("t->eq(")) >= 0 || indexof(l, S("t->print(")) >= 0) aw.hooks_here[f] = 1; for (iz c = 0;; c++) { // malloc, calloc, realloc iz k = indexof(substr(l, c, l.len), S("alloc(")); if (k < 0) break; c += k; u8 one = c >= 1 ? l.data[c - 1] : 0, two = c >= 2 ? l.data[c - 2] : 0; if (one == 'm' || one == 'c' || two == 'r') aw.mallocs[f] = 1; } } aw.count[f] = aw.ncalls - aw.first[f]; } // Whether a function the generated code calls may allocate (hooks 0) or run // a print or == overload (hooks 1): the least answer that holds, over the // runtime functions it reaches, each answered then too. static b32 aw_may_fn(int f, b32 hooks) { if (!hooks && aw.seed[f]) return 1; int *known = hooks ? aw.hooks : aw.allocates; if (!known[f]) { static int reach[AW_FNS] = {0}; static b32 in[AW_FNS] = {0}; int n = 0; reach[n++] = f; in[f] = 1; for (int i = 0; i < n; i++) { aw_read(reach[i]); for (int k = 0; k < aw.count[reach[i]]; k++) { int g = aw.callee[aw.first[reach[i]] + k]; if (!in[g] && !known[g]) in[g] = 1, reach[n++] = g; } } for (int i = 0; i < n; i++) { // the base: answered alone int g = reach[i]; b32 yes = hooks ? aw.hooks_here[g] : aw.mallocs[g] || aw.seed[g]; if (yes) known[g] = 1; } for (b32 changed = 1; changed;) { changed = 0; for (int i = 0; i < n; i++) { int g = reach[i]; if (known[g] == 1) continue; for (int k = 0; k < aw.count[g]; k++) { if (known[aw.callee[aw.first[g] + k]] == 1) { known[g] = 1, changed = 1; break; } } } } for (int i = 0; i < n; i++) { if (!known[reach[i]]) known[reach[i]] = 2; in[reach[i]] = 0; } } return known[f] == 1; } // CheckKind: bounds, overflow, division, removed, missing_key, variant, // nil_dispatch, conversion, assert, fail, nil_function static int check_kind(Str fn, Str line_text) { static struct { Str name; int kind; } table[] = { {SL("s99_fail_index"), 1}, {SL("s99_list_at"), 1}, {SL("s99_list_insert"), 1}, {SL("s99_list_resize"), 1}, {SL("s99_add"), 2}, {SL("s99_sub"), 2}, {SL("s99_mul"), 2}, {SL("s99_pow"), 2}, {SL("s99_uadd"), 2}, {SL("s99_usub"), 2}, {SL("s99_umul"), 2}, {SL("s99_upow"), 2}, {SL("s99_narrow"), 2}, {SL("s99_div"), 3}, {SL("s99_mod"), 3}, {SL("s99_udiv"), 3}, {SL("s99_umod"), 3}, {SL("s99_deref"), 4}, {SL("s99_deref_fail"), 4}, {SL("s99_pool_item"), 4}, {SL("s99_ref_list"), 4}, {SL("s99_payload"), 6}, {SL("s99_cpayload"), 6}, {SL("s99_payload_box"), 6}, {SL("s99_cpayload_box"), 6}, {SL("s99_f2i"), 8}, {SL("s99_f2u"), 8}, {SL("s99_i2i"), 8}, {SL("s99_u2u"), 8}, {SL("s99_chk"), 1}, {SL("s99_list_data"), 1}, {SL("s99_check_fn"), 11}, }; for (iz i = 0; i < countof(table); i++) if (equals(fn, table[i].name)) return table[i].kind; if (equals(fn, S("s99_fail")) || equals(fn, S("s99_fail_n"))) { if (indexof(line_text, S("no key")) >= 0 || indexof(line_text, S("key not found")) >= 0) return 5; if (indexof(line_text, S("called on nil")) >= 0) return 7; if (indexof(line_text, S("unknown variant")) >= 0) return 6; if (indexof(line_text, S("assertion failed")) >= 0) return 9; return 10; } return 0; } static b32 in_loop_frame(void) { for (Frame *f = frame; f && !f->isfn; f = f->parent) if (f->loop && (f->loop->kind == N_FOR || f->loop->kind == N_WHILE || (f->loop->kind == N_LABELED && (f->loop->a->kind == N_FOR || f->loop->a->kind == N_WHILE)))) return 1; return 0; } // "file:line:col", as loc() writes a site static b32 looks_like_site(Str s) { iz i = s.len, digits = 0; while (i > 0 && isdigitc(s.data[i - 1])) i--, digits++; if (!digits || i == 0 || s.data[i - 1] != ':') return 0; i--; digits = 0; while (i > 0 && isdigitc(s.data[i - 1])) i--, digits++; return digits && i > 0 && s.data[i - 1] == ':'; } static void note_generated(Str text) { b32 allocates_here = 0; u8 *more = name_bytes(); aw_index(); for (iz c = 0; c < text.len; c++) { if (!more[text.data[c]]) continue; iz e = c; while (e < text.len && more[text.data[e]]) e++; if (e == text.len || text.data[e] != '(') { c = e - 1; // a word, not a call continue; } Str fn = substr(text, c, e); // a runtime function's answers are kept; another name can only be // malloc, calloc, realloc or a copy helper b32 runtime = fn.len > 4 && startswith(fn, S("s99_")); // every check is one int f = runtime ? aw_find(fn) : -1; if (f >= 0 ? aw_may_fn(f, 0) : aw_seed(fn)) allocates_here = 1; if (fn.len > 4 && startswith(fn, S("copy")) && isdigitc(fn.data[4])) allocates_here = 1; if (f >= 0 && aw_may_fn(f, 1)) genfn->gen_hooks = 1; if (f >= 0 && !aw.kind[f]) { b32 fail = equals(fn, S("s99_fail")) || equals(fn, S("s99_fail_n")); aw.kind[f] = fail ? -1 : check_kind(fn, text) + 1; } int kind = f >= 0 && aw.kind[f] > 0 ? aw.kind[f] - 1 : runtime ? check_kind(fn, text) : 0; if (kind) { // its site: the "file:line:col" among its own arguments Str site = {0}; int depth = 0; for (iz a = e; a < text.len; a++) { u8 ch = text.data[a]; if (ch == '"') { iz q = a + 1; while (q < text.len && text.data[q] != '"') q += text.data[q] == '\\' && q + 1 < text.len ? 2 : 1; Str lit = substr(text, a + 1, q); if (depth == 1 && looks_like_site(lit)) site = lit; a = q < text.len ? q : q - 1; continue; } if (ch == '(') depth++; if (ch == ')' && --depth == 0) break; } if (site.len) { Node *ch = mknode(N_NIL, loc_pos(copystr(&perm, site))); ch->ival = kind; ch->flags = in_loop_frame(); addnode(&genfn->gen_checks, ch); } } c = e - 1; } if (allocates_here && gen_at.file >= 0) { for (iz i = 0; i < genfn->gen_allocs.len; i++) if (genfn->gen_allocs.data[i]->pos.offset == gen_at.offset && genfn->gen_allocs.data[i]->pos.file == gen_at.file) return; addnode(&genfn->gen_allocs, mknode(N_NIL, gen_at)); } } // Debug builds: where the code being generated comes from. Each C line of // it gets a #line naming that .star99 line; code from no program source (the // runtime, helpers, std: modules) stays with the C file. static STATE Map *dbg_paths = 0; // file index -> its absolute path, quoted for C static b32 is_program_source(iz i); // The source position that code made at p stands for: a macro's code is // its call. static Pos source_pos(Pos p) { for (int hops = 0; hops < 16 && p.file >= 0 && p.file < files.len; hops++) { if (!files.data[p.file].generated) return p; p = files.data[p.file].origin; } return p.file >= 0 && p.file < files.len && !files.data[p.file].generated ? p : (Pos){-1, 0, 0, 0}; } static Str debug_path(i32 file) { Str key = fmtstr("%d", file); Str *got = lookup(dbg_paths, key); if (got) return *got; Str path = files.data[file].path; Str real = os_realpath(path); if (real.len) path = real; Str *q = new(&perm, 1, Str); *q = c_string(path); *upsert(&dbg_paths, key, 1) = q; return *q; } // "#line N \"path\"" for code made at p, or nothing static Str line_directive(Pos p) { p = source_pos(p); if (p.file < 0 || !is_program_source(p.file)) return (Str){0}; return fmtstr("#line %d %s", location(p).line, debug_path(p.file)); } // Where a statement starts (p points where its errors do). static Pos stmt_start(Pos p) { if (p.hi > p.lo) p.offset = p.lo; return p; } // The position of the closing brace of block b, or of its last statement. static Pos block_end(Node *b, Pos otherwise) { if (!b) return otherwise; if (b->kind == N_BLOCK && b->end > b->pos.offset) { Pos p = b->pos; p.offset = p.lo = p.hi = b->end; return p; } if (b->kind == N_BLOCK && b->list.len) return b->list.data[b->list.len - 1]->pos; return otherwise; } static void line(char *fmt, ...) { va_list ap; // set by va_start if (!debugging()) { // straight into the function's code, indented for (int i = 0; i < indent; i++) bput(body, S(" ")); iz start = body->len; va_start(ap, fmt); vbputf(body, fmt, ap); va_end(ap); if (genfn && !meta_mode) note_generated(substr(bstr(body), start, body->len)); bputc(body, '\n'); return; } Buf b = {0}; va_start(ap, fmt); vbputf(&b, fmt, ap); va_end(ap); Str text = bstr(&b); if (genfn && !meta_mode) note_generated(text); Str dir = line_directive(dbg_at); if (!dir.len) { for (int i = 0; i < indent; i++) bput(body, S(" ")); bput(body, text); bputc(body, '\n'); return; } // each physical line gets one, so all of them stand for this line for (iz l = 0; l < text.len;) { iz e = l; while (e < text.len && text.data[e] != '\n') e++; if (text.data[l] != '#') { bput(body, dir); bputc(body, '\n'); for (int i = 0; !l && i < indent; i++) bput(body, S(" ")); } bput(body, substr(text, l, e)); bputc(body, '\n'); l = e + 1; } } static void lines(Str s) { if (s.len) line("%s", s); } static Str loc(Pos p) { if (p.file < 0 || p.file >= files.len) return S("\"star99\""); if (files.data[p.file].generated) p = files.data[p.file].origin; // run-time errors name the call Location at = location(p); Str path = files.data[p.file].path; Buf b = {0}; bputc(&b, '"'); for (iz i = 0; i < path.len; i++) { if (path.data[i] == '"' || path.data[i] == '\\') bputc(&b, '\\'); bputc(&b, path.data[i]); } bputf(&b, ":%d:%d\"", at.line, at.col); return bstr(&b); } static Str c_string(Str s) { Buf b = {0}; bputc(&b, '"'); for (iz i = 0; i < s.len; i++) { u8 c = s.data[i]; if (c == '"' || c == '\\') { bputc(&b, '\\'); bputc(&b, c); } else if (c == '\n') { bput(&b, S("\\n")); } else if (c == '\t') { bput(&b, S("\\t")); } else if (c < 32 || c >= 127 || c == '?') { bputf(&b, "\\%03o", c); } else { bputc(&b, c); } } bputc(&b, '"'); if (b.len > 4000) { // C99 promises string literals of 4095 characters: longer text is // an array of byte values static STATE int nbig = 0; Buf a = {0}; bputf(&a, "static const unsigned char s99_big%d[] = {", ++nbig); for (iz i = 0; i < s.len; i++) bputf(&a, "%s%d", !i ? S("\n") : i % 32 ? S(",") : S(",\n"), s.data[i]); bput(&a, S(",0};\n")); bput(&out_globals, bstr(&a)); return fmtstr("((const char *)s99_big%d)", nbig); } return bstr(&b); } static Str temp(Type *t, Str value) { Str name = tmpname(); line("%s %s = %s;", ctype(t), name, value); return name; } // A temporary holding a fresh value, released at the end of the statement. static Str temp_drop(Type *t, Str value) { Str name = temp(t, value); if (t->managed && sctx) *push(&perm, &sctx->drops) = drop_stmt(t, name); return name; } static Str temp_decl(Type *t) { if (t == t_void) return (Str){0}; // nothing to hold: see set_result define_type(t); Str name = tmpname(); line("%s %s;", ctype(t), name); return name; } // r = v, or only v when the result is void. static void set_result(Str r, Str v) { if (r.len) line("%s = %s;", r, v); else line("(void)(%s);", v); } static Str gen_zero(Type *t) { switch (t->kind) { case TY_BOOL: return S("false"); case TY_INT: return fmtstr("(%s)0", ctype(t)); case TY_FLOAT: return S("0.0"); case TY_LIST: case TY_MAP: case TY_POOL: case TY_FILE: case TY_DRAW: case TY_TYPE: return S("0"); case TY_VOID: return S("0"); default: { Str ct = ctype(t); define_type(t); if (t->has_defaults) { need(t, H_INIT); Str name = tmpname(); line("%s %s;", ct, name); line("init%d(&%s);", t->id, name); return name; } return fmtstr("(%s){0}", ct); } } } // Whether an expression makes a new value that nobody else owns. static b32 is_fresh(Node *n) { switch ((int)n->kind) { case N_NAME: case N_FIELD: case N_INDEX: case N_DEREF: return 0; case N_STR: return !optimizing(); // a static literal is borrowed, never owned case N_FNLIT: { // only a closure that outlives its scope owns an environment Fn *f = n->info; return f && f->isclosure && f->escapes && f->captures.len; } default: return 1; // a call too } } static Str gen_move(Node *n); // The value of an expression, borrowed: fresh managed values are released at // the end of the statement. static Str gen_value(Node *n) { Str e = gen_expr(n); if (n->type->managed && is_fresh(n)) return temp_drop(n->type, e); return e; } static STATE Node *cur_block = 0; // the block being generated, and the statement static STATE iz cur_stmt = 0; // Last uses. A local whose value a statement takes (a constructor field, // add, an assignment) and that nothing reads afterwards is moved, not // copied: the value goes over and the local is left zero, so its release // at the end of the scope does nothing. Only for a local declared in the // block being generated, used once by the statement, not in a loop inside // it, and not mentioned by later statements or defers of the block. // How often n mentions v: a mention inside a loop, function literal or // labeled block counts twice, so 1 is a single use that runs once. static int mentions(Node *n, Sym *v, b32 inloop) { if (!n) return 0; if (n->kind == N_NAME && n->sym == v) return inloop ? 2 : 1; b32 loop = inloop || n->kind == N_FOR || n->kind == N_WHILE || n->kind == N_FNLIT || n->kind == N_LABELED; int c = 0; if ((int)n->kind == X_CALLFN) { // the checked arguments; the call node keeps the written ones too Call *call = n->info; for (iz i = 0; i < call->nargs; i++) c += mentions(call->args[i], v, loop); return c + mentions(call->callee, v, loop); } c += mentions(n->a, v, loop) + mentions(n->b, v, loop); if (n->kind != N_FOR) c += mentions(n->c, v, loop); for (iz i = 0; i < n->list.len; i++) c += mentions(n->list.data[i], v, loop); return c; } static b32 has_defer_of(Node *n, Sym *v); static Str var_expr(Sym *s, Pos p); static b32 last_use(Node *n) { if (!optimizing() || n->kind != N_NAME || !n->sym || n->sym->kind != S_VAR) return 0; Sym *v = n->sym; if (!v->type->managed || v->type->has_refs || v->pinned || v->iscaptured || v->isglobal || v->isparam || v->isvar || v->borrowed || v->owner != genfn || !v->decl) return 0; if (!cur_block || cur_stmt >= cur_block->list.len) return 0; b32 declared = 0; for (iz j = 0; j < cur_stmt; j++) declared |= cur_block->list.data[j] == v->decl; if (!declared) return 0; if (mentions(cur_block->list.data[cur_stmt], v, 0) != 1) return 0; for (iz j = cur_stmt + 1; j < cur_block->list.len; j++) { if (mentions(cur_block->list.data[j], v, 0)) return 0; } for (iz j = 0; j < cur_block->list.len; j++) { if (has_defer_of(cur_block->list.data[j], v)) return 0; } return 1; } static b32 has_defer_of(Node *n, Sym *v) { return n && n->kind == N_DEFER && mentions(n->a, v, 0); } // The value of an expression, owned by whoever receives it. static Str gen_move(Node *n) { if (last_use(n)) { // the local's value goes over; the local is left zero Str x = var_expr(n->sym, n->pos); Str t = temp(n->type, x); line("%s = %s;", x, gen_zero(n->type)); return t; } Str e = gen_expr(n); if (!n->type->managed || is_fresh(n)) return e; Str name = temp_decl(n->type); line("%s", copy_stmt(n->type, name, e)); return name; } // The address of a value, for helpers that take pointers. static Str gen_addr(Node *n) { switch ((int)n->kind) { case N_NAME: case N_FIELD: case N_INDEX: case N_DEREF: return fmtstr("&%s", gen_expr(n)); default: { Str v = gen_value(n); if (n->type->managed && is_fresh(n)) return fmtstr("&%s", v); return fmtstr("&%s", temp(n->type, v)); } } } // Writing the resume of this coroutine (gen_resume): the body runs on the // struct, and functions nested in it that yield are inlined. static STATE Coro *co_gen = 0; // ... and, when c is a route, the buffer its body goes to: statements there // that cannot move the struct reach it through one pointer (co_cache_stmt) static STATE Buf *co_route_body = 0; // A local of the function being written: its own, or, in a resume, one of a // nested function inlined there static b32 own_local(Sym *s) { return !genfn || !s->owner || s->owner == genfn || (co_gen && genfn == co_gen->fn && s->owner->yields); } // What a coroutine keeps is a field of its struct, reached through c: in its // resume directly, in the functions nested in it as they reach a var // parameter static Str co_place(Sym *s, Pos p) { return fmtstr("(%s)%s", var_expr(s->co->param, p), s->co_path); } static b32 debug_name_ok(Str name, b32 strict); static Str fn_signature_text(Fn *f, b32 result); static STATE Fn *dbg_root = 0; // a debug build: the function the C main is made of // The member of the environment of closure f that holds capture i: c0, c1, // ..., or in a debug build the captured variable's name where it is free. static Str cap_member(Fn *f, iz i) { Str name = cident(f->captures.data[i]->name); b32 numbered = name.len > 1 && name.data[0] == 'c'; for (iz k = 1; numbered && k < name.len; k++) numbered = isdigitc(name.data[k]); b32 free_name = debugging() && debug_name_ok(name, 1) && !numbered && !startswith(name, S("f_")); for (iz j = 0; free_name && j < f->captures.len; j++) free_name = j == i || !equals(cident(f->captures.data[j]->name), name); return free_name ? name : fmtstr("c%lld", (long long)i); } // Where s is among the captures of the function being generated. static iz capture_index(Sym *s, Pos p) { for (iz i = 0; i < genfn->captures.len; i++) { if (genfn->captures.data[i] == s) return i; } errorat(p, "star99 bug, please report it: %s is not captured", s->name); } static Str var_expr(Sym *s, Pos p) { if (s->co_path.len) return co_place(s, p); if (s->isglobal) return s->cname; // a var parameter is reached through a pointer or a route: what it // points to needs its whole type (in the metaprogram too, where nothing // else may have needed it) if (s->isvar) define_type(s->type); if (!own_local(s)) { // a captured variable, through the environment iz idx = capture_index(s, p); if (s->isvar) return fmtstr("(*(%s *)s99_deref(*env->%s, %s))", ctype(s->type), cap_member(genfn, idx), loc(p)); return fmtstr("(*env->%s)", cap_member(genfn, idx)); } // a var parameter as a plain pointer; a switch & binding is always a route if (s->isvar && s->isparam && s->owner && s->owner->var_ptr) return fmtstr("(*%s)", s->cname); if (s->isvar) return fmtstr("(*(%s *)s99_deref(%s, %s))", ctype(s->type), s->cname, loc(p)); if (s->pinned) return fmtstr("(*%s)", s->cname); return s->cname; } // The address of a variable, for building environments. static Str var_addr(Sym *s, Pos p) { if (s->co_path.len) return fmtstr("&%s", co_place(s, p)); if (!own_local(s) && !s->isglobal) return fmtstr("env->%s", cap_member(genfn, capture_index(s, p))); if (s->isvar && s->isparam && s->owner && s->owner->var_ptr) // a resume's c return fmtstr("&%s_ref", s->cname); if (s->isvar) return fmtstr("&%s", s->cname); if (s->pinned) return s->cname; return fmtstr("&%s", s->cname); } // A cell counted and freed with its last holder: a local pinned only // because closures that outlive its scope capture it. static b32 rc_cell(Sym *s) { return s->pinned && !s->refpinned && !s->isglobal && !s->isvar; } static Str cell_alloc(Sym *s) { Str ct = ctype(s->type); if (!rc_cell(s)) return fmtstr("s99_cell(sizeof(%s))", ct); if (!s->type->managed) return fmtstr("s99_rc_new(sizeof(%s), 0)", ct); need(s->type, H_CELLDROP); return fmtstr("s99_rc_new(sizeof(%s), celldrop%d)", ct, s->type->id); } static Str env_type(Fn *f) { return fmtstr("struct E_%s", f->cname); } static Str gen_closure_value(Fn *f, Pos p); // add(&xs, v). For a local list the header pointer is assigned, never // passed by address, so it stays in a register through a loop. static void gen_push(Node *addr, Str ca, Type *et, Str v) { Str ec = ctype(et); Node *x = addr->kind == N_ADDR ? addr->a : 0; if (optimizing() && x && x->kind == N_NAME && x->sym && x->sym->kind == S_VAR && !x->sym->pinned && !x->sym->isvar && !x->sym->isglobal && x->sym->owner == genfn) { Str l = var_expr(x->sym, x->pos); line("if (!(%s && %s->len < %s->cap)) %s = s99_list_room(%s, sizeof(%s));", l, l, l, l, l, ec); line("((%s *)%s->data)[%s->len++] = %s;", ec, l, l, v); return; } line("*(%s *)s99_list_push(%s, sizeof(%s)) = %s;", ec, ca, ec, v); } // How the runtime hashes the keys of a map type: 0 bytes, 1 string, 2 Bool. static int key_kind(Type *mt) { return mt->key->text ? 1 : mt->key->kind == TY_BOOL ? 2 : 0; } static Str int_literal(Type *t, i64 v) { if (t == t_int || !t || t->kind != TY_INT) { if (v == INT64_MIN) return S("INT64_MIN"); return fmtstr("INT64_C(%lld)", (long long)v); } if (!t->issigned) return fmtstr("((%s)UINT64_C(%llu))", ctype(t), (unsigned long long)v); return fmtstr("((%s)INT64_C(%lld))", ctype(t), (long long)v); } static Str float_literal(f64 v) { Str s = as_float(fmtstr("%.17g", v)); if (equals(s, S("inf"))) return S("HUGE_VAL"); if (equals(s, S("-inf"))) return S("(-HUGE_VAL)"); return s; } static Str int_limits(Type *t, Str which) { if (t->issigned) { if (equals(which, S("lo"))) return fmtstr("INT%d_MIN", t->bits); return fmtstr("INT%d_MAX", t->bits); } if (equals(which, S("lo"))) return S("0"); return fmtstr("UINT%d_MAX", t->bits); } // Value ranges. The checks of Int arithmetic stay unless the compiler can // show the result fits: from constants, x % k, small types, loop bounds, // len(), and the ranges of locals, worked out over every assignment they // get in their function (a local that keeps growing is not ranged). typedef struct { i64 lo, hi; } Range; static const Range full_range = {INT64_MIN, INT64_MAX}; static Range type_range(Type *t) { if (t->kind != TY_INT || t->bits == 64) return full_range; if (t->issigned) return (Range){-((i64)1 << (t->bits - 1)), ((i64)1 << (t->bits - 1)) - 1}; return (Range){0, ((i64)1 << t->bits) - 1}; } static b32 add_ok(i64 a, i64 b, i64 *r) { if ((b > 0 && a > INT64_MAX - b) || (b < 0 && a < INT64_MIN - b)) return 0; *r = a + b; return 1; } static b32 mul_ok(i64 a, i64 b, i64 *r) { if (a == 0 || b == 0) { *r = 0; return 1; } if (a > 0 ? (b > 0 ? a > INT64_MAX / b : b < INT64_MIN / a) : (b > 0 ? a < INT64_MIN / b : a < INT64_MAX / b)) return 0; *r = a * b; return 1; } // The operation of a compound assignment: += is +. static TokKind compound_op(TokKind op) { return op == T_PLUSEQ ? T_PLUS : op == T_MINUSEQ ? T_MINUS : op == T_STAREQ ? T_STAR : op == T_SLASHEQ ? T_SLASH : T_PERCENT; } // a op b over ranges; full when a bound would leave Int static Range range_op(TokKind op, Range a, Range b) { if (op == T_PERCENT) { if (b.lo > 0 || b.hi < 0) { if (b.lo == INT64_MIN) return full_range; // its size is past Int i64 k = b.lo > 0 ? b.hi : -b.lo; // the largest |divisor| Range r = {a.lo >= 0 ? 0 : -(k - 1), a.hi <= 0 ? 0 : k - 1}; return r; } return full_range; } if (op == T_SLASH) { if (b.lo > 0) { // dividing by a positive number moves toward zero Range r = {a.lo < 0 ? a.lo : a.lo / b.hi, a.hi > 0 ? a.hi : a.hi / b.hi}; if (a.lo >= 0) r.lo = a.lo / b.hi, r.hi = a.hi / b.lo; return r; } return full_range; } i64 c[4] = {0}; b32 ok = 1; switch (op) { case T_PLUS: ok = add_ok(a.lo, b.lo, &c[0]) && add_ok(a.hi, b.hi, &c[1]); c[2] = c[0], c[3] = c[1]; break; case T_MINUS: ok = b.hi != INT64_MIN && b.lo != INT64_MIN && add_ok(a.lo, -b.hi, &c[0]) && add_ok(a.hi, -b.lo, &c[1]); c[2] = c[0], c[3] = c[1]; break; case T_STAR: ok = mul_ok(a.lo, b.lo, &c[0]) && mul_ok(a.lo, b.hi, &c[1]) && mul_ok(a.hi, b.lo, &c[2]) && mul_ok(a.hi, b.hi, &c[3]); break; default: return full_range; } if (!ok) return full_range; i64 lo = c[0], hi = c[0]; for (int i = 1; i < 4; i++) { if (c[i] < lo) lo = c[i]; if (c[i] > hi) hi = c[i]; } return (Range){lo, hi}; } static b32 fits(TokKind op, Range a, Range b) { if (op == T_PERCENT || op == T_SLASH) { // no division by zero, and not INT64_MIN / -1 b32 nozero = b.lo > 0 || b.hi < 0; b32 nominus1 = b.lo > -1 || b.hi < -1 || a.lo > INT64_MIN; return nozero && nominus1; } Range r = range_op(op, a, b); return !(r.lo == INT64_MIN && r.hi == INT64_MAX) || ((a.lo == 0 && a.hi == 0) || (b.lo == 0 && b.hi == 0)); } static Range facts_on(Sym *s, Range r); // Names facts can be about: constants and parameters, and the Int locals of // this function that only it can change (facts about those die when a // statement changes them). static b32 fixed_int(Node *n) { if (n->kind != N_NAME || !n->sym || n->sym->kind != S_VAR || n->type != t_int || n->sym->isvar) return 0; Sym *s = n->sym; if (s->co_path.len) return 0; // kept by a coroutine: edited between resumes if (s->readonly || s->isconst) return 1; return !s->pinned && !s->iscaptured && !s->isglobal && s->owner == genfn; } static STATE u32 range_epoch = 1; // changes whenever a fact or a variable range does static Range range_of_(Node *n); static Range range_of(Node *n) { if (!optimizing() || !n->type || n->type->kind != TY_INT) return full_range; // worked out once while nothing it depends on changes (facts, ranges of // variables): asked again for every operator above, it would be // quadratic or worse in the length of an expression if (n->repoch && n->repoch == range_epoch) return (Range){n->rlo, n->rhi}; Range r = range_of_(n); n->repoch = range_epoch; n->rlo = r.lo, n->rhi = r.hi; return r; } static Range range_of_(Node *n) { switch ((int)n->kind) { case N_INT: return (Range){n->ival, n->ival}; case N_NAME: { Range r = n->sym && n->sym->kind == S_VAR && n->sym->ranged ? (Range){n->sym->rlo, n->sym->rhi} : type_range(n->type); return fixed_int(n) ? facts_on(n->sym, r) : r; } case N_UNARY: if (n->op == T_MINUS) { Range a = range_of(n->a); if (a.lo == INT64_MIN) return type_range(n->type); return (Range){-a.hi, -a.lo}; } return type_range(n->type); case N_BINARY: if (n->type == t_int && (n->op == T_PLUS || n->op == T_MINUS || n->op == T_STAR || n->op == T_SLASH || n->op == T_PERCENT)) return range_op(n->op, range_of(n->a), range_of(n->b)); return type_range(n->type); case N_CAST: { Range a = range_of(n->b); Range t = type_range(n->type); if (n->b->type->kind == TY_INT && n->b->type->bits < 64) a = type_range(n->b->type); return a.lo >= t.lo && a.hi <= t.hi ? a : t; } case X_CALLFN: { Call *c = n->info; if (c->kind == CALL_FN && c->builtin == B_LEN) return (Range){0, (i64)1 << 48}; // lengths fit in memory return type_range(n->type); } default: return type_range(n->type); } } static STATE b32 widening = 0; // ranges still growing jump to full // Facts from conditions: inside if x < 10 { ... } x is below 10, and after // if n < 2 do return n, n is 2 or more. Only for names that cannot change. typedef struct { Sym *sym; Range r; } Fact; static STATE Fact facts[64] = {0}; static STATE i32 nfacts = 0; static Range facts_on(Sym *s, Range r) { for (i32 i = 0; i < nfacts; i++) { if (facts[i].sym != s) continue; if (facts[i].r.lo > r.lo) r.lo = facts[i].r.lo; if (facts[i].r.hi < r.hi) r.hi = facts[i].r.hi; } if (r.lo > r.hi) r.hi = r.lo; // unreachable code: any range will do return r; } static void add_fact(Sym *s, Range r) { if (nfacts < (i32)countof(facts)) facts[nfacts++] = (Fact){s, r}; range_epoch++; } static b32 changes(Node *n, Sym *v); static b32 takes_addr(Node *n, Sym *v); // Facts about locals that n assigns or passes by address die (marked, so a // block restoring its count does not bring them back). static void kill_changed(Node *n, b32 addr_only) { for (i32 i = 0; i < nfacts; i++) { Sym *s = facts[i].sym; if (!s || s->readonly || s->isconst) continue; if (addr_only ? takes_addr(n, s) : changes(n, s)) { facts[i].sym = 0; range_epoch++; } } } // An index xs[i] that ran proves 0 <= i < len(xs). The fact waits until // the statement holding it has run in full: within it, C may evaluate other // parts first. static STATE Fact pend[64] = {0}; static STATE i32 npend = 0; static void add_pending(Sym *s, Range r) { if (npend < (i32)countof(pend)) pend[npend++] = (Fact){s, r}; } static void commit_pending(i32 from, Node *stmt) { for (i32 i = from; i < npend; i++) { if (stmt && changes(stmt, pend[i].sym)) continue; add_fact(pend[i].sym, pend[i].r); } npend = from; } // What holds when c is truth (or false): facts pushed for the names it // compares. static void learn(Node *c, b32 truth) { if (!optimizing()) return; if (c->kind == N_UNARY && c->op == T_NOT) { learn(c->a, !truth); return; } if (c->kind != N_BINARY) return; if ((c->op == T_AND && truth) || (c->op == T_OR && !truth)) { learn(c->a, truth); learn(c->b, truth); return; } TokKind op = c->op; if (op != T_LT && op != T_LE && op != T_GT && op != T_GE && op != T_EQEQ && op != T_NE) return; if (c->a->type != t_int || c->b->type != t_int) return; if (!truth) { op = op == T_LT ? T_GE : op == T_LE ? T_GT : op == T_GT ? T_LE : op == T_GE ? T_LT : op == T_EQEQ ? T_NE : T_EQEQ; } for (int side = 0; side < 2; side++) { Node *x = side ? c->b : c->a, *y = side ? c->a : c->b; if (!fixed_int(x)) continue; TokKind o = op; if (side) o = o == T_LT ? T_GT : o == T_LE ? T_GE : o == T_GT ? T_LT : o == T_GE ? T_LE : o; Range k = range_of(y), r = full_range; switch (o) { case T_LT: if (k.hi == INT64_MIN) continue; r.hi = k.hi - 1; break; case T_LE: r.hi = k.hi; break; case T_GT: if (k.lo == INT64_MAX) continue; r.lo = k.lo + 1; break; case T_GE: r.lo = k.lo; break; case T_EQEQ: r = k; break; default: continue; } add_fact(x->sym, r); } } static void join_range(Sym *s, Range r, b32 *changed) { range_epoch++; if (!s->ranged) { s->ranged = 1; s->rlo = r.lo, s->rhi = r.hi; *changed = 1; return; } if (r.lo < s->rlo) s->rlo = widening ? INT64_MIN : r.lo, *changed = 1; if (r.hi > s->rhi) s->rhi = widening ? INT64_MAX : r.hi, *changed = 1; } // The local a place (x, x.f, x[i]) is part of, or 0. static Sym * place_sym(Node *n) { while (n->kind == N_FIELD || n->kind == N_INDEX) n = n->a; return n->kind == N_NAME ? n->sym : 0; // through a Ref: unknown } // One pass over a function: every place an Int local gets a value. static void range_pass(Node *n, b32 *changed) { if (!n) return; switch ((int)n->kind) { case N_VAR: if (n->sym && n->sym->type == t_int && !n->sym->pinned && !n->sym->co_path.len) { join_range(n->sym, n->b ? range_of(n->b) : (Range){0, 0}, changed); } break; case N_FOR: if (n->sym && n->sym->co_path.len) { // kept by a coroutine: not ranged } else if (n->a->kind == N_RANGE && n->sym && n->sym->type == t_int) { Range lo = range_of(n->a->a), hi = range_of(n->a->b); i64 bottom = range_open_low(n->a->op) && lo.lo != INT64_MAX ? lo.lo + 1 : lo.lo; i64 top = range_open_high(n->a->op) ? (hi.hi == INT64_MIN ? INT64_MIN : hi.hi - 1) : hi.hi; join_range(n->sym, (Range){bottom, top < bottom ? bottom : top}, changed); } else if (n->sym2 && n->sym && n->sym->type == t_int) { // for i, x in xs: i is a position join_range(n->sym, (Range){0, (i64)1 << 48}, changed); } break; case N_ASSIGN: if (n->a->kind == N_NAME && n->a->sym && n->a->sym->ranged) { Sym *v = n->a->sym; Range r = range_of(n->b); if (n->op != T_EQ) r = range_op(compound_op(n->op), (Range){v->rlo, v->rhi}, r); join_range(v, r, changed); } break; case N_ADDR: { Sym *s = place_sym(n->a); if (s && s->ranged) join_range(s, full_range, changed); break; } } range_pass(n->a, changed); range_pass(n->b, changed); range_pass(n->c, changed); for (iz i = 0; i < n->list.len; i++) range_pass(n->list.data[i], changed); } // The ranges of the Int locals of a function: joined over all their values // until nothing changes; a bound still growing after a few rounds becomes // unbounded. static void compute_ranges(Node *body) { if (!optimizing() || !body) return; b32 changed = 1; // a few rounds to settle, then whatever still grows goes to full // range at once, which ends the iteration for (int round = 0; changed; round++) { widening = round >= 4; changed = 0; range_pass(body, &changed); } widening = 0; } // Affine arithmetic: + - * over constants and one other value x, as in // 3 * n + 1. Each operation fits exactly when x lies in an interval, so the // checks become compares of x, in the order the operations run, and the // arithmetic itself is plain C the compiler can reason about. typedef struct { i64 a, b; // the value is a * x + b b32 hasx; } Affine; static i64 floor_div(i64 n, i64 d) // d > 0 { return n / d - (n % d != 0 && n < 0); } // The x for which a * x + b fits in Int, a nonzero and small. static Range affine_fits(i64 a, i64 b) { Range r = full_range; b32 neg = a < 0; if (neg) a = -a; // a * y + b <= MAX: y <= floor((MAX - b) / a), split to stay in range i64 f = floor_div(INT64_MAX % a - b, a); i64 yhi = 0; if (!add_ok(INT64_MAX / a, f, &yhi)) yhi = INT64_MAX; // past the top: no bound // a * y + b >= MIN: y >= ceil((MIN - b) / a) f = floor_div(-(INT64_MIN % a) + b, a); // ceil(m / a) = -floor(-m / a) i64 ylo = 0; if (f == INT64_MIN || !add_ok(INT64_MIN / a, -f, &ylo)) ylo = INT64_MIN; if (!neg) return (Range){ylo, yhi}; r.lo = yhi == INT64_MIN ? INT64_MIN : -yhi; r.hi = ylo == INT64_MIN ? INT64_MAX : -ylo; return r; } static const i64 affine_small = (i64)1 << 31; static b32 affine_of(Node *n, Node **x, Affine *v) { if (n->type != t_int) return 0; Range k = range_of(n); if (k.lo == k.hi && (n->kind == N_INT || (n->kind == N_NAME && n->sym && n->sym->isconst))) { if (k.lo > affine_small || k.lo < -affine_small) return 0; *v = (Affine){0, k.lo, 0}; return 1; } if (n->kind == N_BINARY && (n->op == T_PLUS || n->op == T_MINUS || n->op == T_STAR)) { Affine l = {0}, r = {0}; if (!affine_of(n->a, x, &l) || !affine_of(n->b, x, &r)) return 0; if (l.hasx && r.hasx) return 0; Affine o = {0, 0, l.hasx || r.hasx}; switch (n->op) { case T_PLUS: o.a = l.a + r.a, o.b = l.b + r.b; break; case T_MINUS: o.a = l.a - r.a, o.b = l.b - r.b; break; default: if (l.hasx) o.a = l.a * r.b, o.b = l.b * r.b; else o.a = r.a * l.b, o.b = r.b * l.b; break; } if (!o.hasx) return 0; // all constants: the usual path folds and checks it // small coefficients keep all of this far from overflow if (o.a > affine_small || o.a < -affine_small || o.b > affine_small || o.b < -affine_small) return 0; *v = o; return 1; } if (*x) return 0; // a second value *x = n; *v = (Affine){1, 0, 1}; return 1; } // The checks of each operation in n, in evaluation order, then the plain // expression. static Str affine_emit(Node *n, Node *x, Str xv, Range xr) { if (n == x) return xv; if (n->kind != N_BINARY) return gen_value(n); Str a = affine_emit(n->a, x, xv, xr), b = affine_emit(n->b, x, xv, xr); Node *none = 0; Affine v = {0}; affine_of(n, &none, &v); if (v.hasx && v.a != 0) { Range ok = affine_fits(v.a, v.b); Str lo = xr.lo < ok.lo ? fmtstr("%s < %s", xv, int_literal(t_int, ok.lo)) : (Str){0}; Str hi = xr.hi > ok.hi ? fmtstr("%s > %s", xv, int_literal(t_int, ok.hi)) : (Str){0}; if (lo.len || hi.len) line("if (%s%s%s) s99_fail(%s, \"integer overflow\");", lo, lo.len && hi.len ? S(" || ") : S(""), hi, loc(n->pos)); } return fmtstr("(%s %s %s)", a, opname(n->op), b); // + - * } static Str gen_affine(Node *n) { if (!optimizing() || n->type != t_int || n->kind != N_BINARY) return (Str){0}; if (n->op != T_PLUS && n->op != T_MINUS && n->op != T_STAR) return (Str){0}; if (n->a->kind != N_BINARY && n->b->kind != N_BINARY) return (Str){0}; // one operation: the usual way Node *x = 0; Affine v = {0}; if (!affine_of(n, &x, &v) || !x) return (Str){0}; Str xv = temp(t_int, gen_value(x)); return affine_emit(n, x, xv, range_of(x)); } // a op b with nodes: plain C when the ranges show the result fits. static Str gen_arith(TokKind op, Type *t, Str a, Str b, Pos p); static Str gen_arith_nodes(TokKind op, Type *t, Node *na, Node *nb, Str a, Str b, Pos p) { if (op != T_STARSTAR && t == t_int && optimizing() && fits(op, range_of(na), range_of(nb))) { return fmtstr("(%s %s %s)", a, opname(op), b); // + - * / % } return gen_arith(op, t, a, b, p); } static Str gen_arith(TokKind op, Type *t, Str a, Str b, Pos p) { Str l = loc(p); if (t->kind == TY_FLOAT) { return fmtstr("(%s %s %s)", a, opname(op), b); // + - * / } static Str names[] = {[T_PLUS] = SL("add"), [T_MINUS] = SL("sub"), [T_STAR] = SL("mul"), [T_SLASH] = SL("div"), [T_PERCENT] = SL("mod"), [T_STARSTAR] = SL("pow")}; Str nm = names[op]; if (t->issigned) { Str r = fmtstr("s99_%s((int64_t)(%s), (int64_t)(%s), %s)", nm, a, b, l); if (t->bits == 64) return r; return fmtstr("((%s)s99_narrow(%s, %s, %s, %s))", ctype(t), r, int_limits(t, S("lo")), int_limits(t, S("hi")), l); } Str hi = int_limits(t, S("hi")); Str r = {0}; if (op == T_PLUS || op == T_STAR || op == T_STARSTAR) r = fmtstr("s99_u%s((uint64_t)(%s), (uint64_t)(%s), %s, %s)", nm, a, b, hi, l); else r = fmtstr("s99_u%s((uint64_t)(%s), (uint64_t)(%s), %s)", nm, a, b, l); return fmtstr("((%s)%s)", ctype(t), r); } // The left operand of a binary operation, evaluated before the right one // starts: C leaves the order of operands open, and checks that fail must // fail in the order written. static Str gen_left(Node *a, Node *b) { Str x = gen_value(a); b32 plain = b->kind == N_INT || b->kind == N_FLOAT || b->kind == N_BOOL || b->kind == N_NAME || b->kind == N_STR || b->kind == N_NIL || b->kind == N_TYPENAME; if (plain || a->kind == N_INT || a->kind == N_NAME) return x; return temp(a->type, x); } static Str gen_eq(Type *t, Node *a, Node *b) { switch (t->kind) { case TY_BOOL: case TY_INT: case TY_FLOAT: case TY_TYPE: { Str x = gen_left(a, b); return fmtstr("(%s == %s)", x, gen_expr(b)); } case TY_FILE: // a zero File is stdout return fmtstr("(s99_file(%s) == s99_file(%s))", gen_expr(a), gen_expr(b)); default: { Str pa = gen_addr(a), pb = gen_addr(b); need(t, H_INFO); return fmtstr("s99_equal(&info%d, %s, %s)", t->id, pa, pb); } } } static Str union_payload(Type *u, iz variant, Str uaddr, Pos p) { Type *pt = u->fields.data[variant].type; if (compact_union(u)) return fmtstr("(*(%s *)s99_cpayload(%s, %s))", ctype(pt), uaddr, loc(p)); need(pt, H_ZERO); return fmtstr("(*(%s *)s99_payload(%s, %d, &z%d, %s))", ctype(pt), uaddr, union_tag(u, variant), pt->id, loc(p)); } // The box of variant v of the union at uaddr, made when the union has none. static Str payload_box(Type *u, iz v, Str uaddr, Pos p) { if (compact_union(u)) return fmtstr("s99_cpayload_box(%s, %s)", uaddr, loc(p)); return fmtstr("s99_payload_box(%s, %d, sizeof(%s), %s)", uaddr, union_tag(u, v), ctype(u->fields.data[v].type), loc(p)); } static Str union_payload_w(Type *u, iz variant, Str uaddr, Pos p) { Str box = payload_box(u, variant, uaddr, p); return fmtstr("(*(%s *)S99_BOXDATA(%s))", ctype(u->fields.data[variant].type), box); } // A union value holding payload (owned) in variant vi. static Str union_value(Type *u, iz vi, Str payload) { Type *pt = u->fields.data[vi].type; Str t = temp_decl(u); if (!compact_union(u)) line("%s.tag = %d;", t, union_tag(u, vi)); line("%s.box = s99_box_filled(sizeof(%s));", t, ctype(pt)); line("*(%s *)S99_BOXDATA(%s.box) = %s;", ctype(pt), t, payload); return t; } // Builds a union value holding v (owned) in the variant at path. static Str wrap_union(Type *u, Conv *cv, iz depth, Str v) { iz vi = cv->path[depth]; Type *pt = u->fields.data[vi].type; return union_value(u, vi, depth + 1 < cv->npath ? wrap_union(pt, cv, depth + 1, v) : v); } static Str gen_call(Node *n); static Str gen_switch_value(Node *n); static Str gen_list_literal(Node *n) { Type *t = n->type; if (t->kind == TY_MAP) { need(t, H_DROP); Str m = temp(t, S("0")); for (iz i = 0; i < n->list.len; i++) { Node *pr = n->list.data[i]; Str k = gen_move(pr->a); Str v = gen_move(pr->b); line("{"); indent++; line("bool created;"); line("%s kk = %s;", ctype(t->key), k); line( "s99_mapent *e = s99_map_put(s99_map_hdr(&%s, sizeof(%s), sizeof(%s), %d), &kk, sizeof(%s), %d, &created);", m, ctype(t->key), ctype(t->elem), key_kind(t), ctype(t->key), key_kind(t)); // a repeated key keeps its first copy and takes the last value Str dk = drop_stmt(t->key, S("kk")); Str dv = drop_stmt(t->elem, fmtstr("*(%s *)s99_map_val(%s, e)", ctype(t->elem), m)); line("if (created) {"); line(" *(%s *)s99_map_key(e) = kk;", ctype(t->key)); line("} else {"); line(" %s %s", dk, dv); line("}"); line("*(%s *)s99_map_val(%s, e) = %s;", ctype(t->elem), m, v); indent--; line("}"); } return m; } if (t->kind == TY_ARRAY) { Str a = temp_decl(t); for (iz i = 0; i < n->list.len; i++) { Str v = gen_move(n->list.data[i]); line("%s.a[%lld] = %s;", a, (long long)i, v); } return a; } Str l = temp(t, S("0")); if (n->list.len) line("s99_list_reserve(s99_list_hdr(&%s, sizeof(%s)), %lld);", l, ctype(t->elem), (long long)n->list.len); for (iz i = 0; i < n->list.len; i++) { Str v = gen_move(n->list.data[i]); line("*(%s *)s99_list_push(&%s, sizeof(%s)) = %s;", ctype(t->elem), l, ctype(t->elem), v); } return l; } static Str gen_ctor(Type *t, Node **args, iz nargs) { Str v = temp_decl(t); if (t->has_defaults) { need(t, H_INIT); line("init%d(&%s);", t->id, v); } else { line("memset(&%s, 0, sizeof(%s));", v, v); } for (iz i = 0; i < nargs && i < t->fields.len; i++) { if (!args[i]) continue; Field *f = t->fields.data + i; Str fname = fmtstr("%s.f_%s", v, cident(f->name)); Str val = gen_move(args[i]); Str d = drop_stmt(f->type, fname); if (d.len && f->def) line("%s", d); line("%s = %s;", fname, val); } return v; } static Str gen_field(Node *n, b32 forwrite) { FieldAccess *fa = n->info; if (!fa) { // a module member return var_expr(n->sym, n->pos); } if (fa->kind == FA_STRUCT) { Str base = forwrite ? gen_lvalue(n->a) : gen_value(n->a); return fmtstr("%s.f_%s", base, cident(n->name)); } Type *u = fa->base; if (fa->kind == FA_UNION_ONE) { Type *pt = u->fields.data[0].type; Str uaddr = forwrite ? fmtstr("&%s", gen_lvalue(n->a)) : gen_addr(n->a); Str payload = forwrite ? union_payload_w(u, 0, uaddr, n->pos) : union_payload(u, 0, uaddr, n->pos); return fmtstr("%s.f_%s", payload, cident(pt->fields.data[fa->indices[0]].name)); } // a field every variant has Str uaddr = gen_addr(n->a); b32 same = !isunion(n->type) || n->type == u->fields.data[0].type->fields.data[fa->indices[0]].type; Str r = same ? fmtstr("p%d", ++tmpcount) : temp_decl(n->type); if (same) line("%s *%s = 0;", ctype(n->type), r); line("switch (%s) {", utag(u, uaddr)); for (iz v = 0; v < u->fields.len; v++) { Type *pt = u->fields.data[v].type; Field *pf = pt->fields.data + fa->indices[v]; Str pay = union_payload(u, v, uaddr, n->pos); if (same) { line("case %d: %s = &%s.f_%s; break;", union_tag(u, v), r, pay, cident(pf->name)); } else { line("case %d: {", union_tag(u, v)); indent++; Str copy = temp_decl(pf->type); line("%s", copy_stmt(pf->type, copy, fmtstr("%s.f_%s", pay, cident(pf->name)))); Conv cv = {0}; union_path(n->type, pf->type, &cv, 0); Str w = wrap_union(n->type, &cv, 0, copy); line("%s = %s;", r, w); line("break;"); indent--; line("}"); } } line("default: s99_fail(%s, \"this is nil\");", loc(n->pos)); line("}"); if (same) return fmtstr("(*%s)", r); if (sctx) *push(&perm, &sctx->drops) = drop_stmt(n->type, r); return r; } // A lookup with the key size and kind as constants, inline at the call. static Str map_get(Type *mt, Str map, Str keyaddr) { return fmtstr("s99_map_get(%s, %s, sizeof(%s), %d)", map, keyaddr, ctype(mt->key), key_kind(mt)); } static Str map_entry(Type *mt, Str maphandle_addr, Str keyaddr, b32 create, Pos p) { if (!create) { Str e = tmpname(); line("s99_mapent *%s = %s;", e, map_get(mt, fmtstr("*%s", maphandle_addr), keyaddr)); line("if (!%s) s99_fail(%s, \"key not found\");", e, loc(p)); return e; } Str e = tmpname(); Str created = tmpname(); line("bool %s;", created); line( "s99_mapent *%s = s99_map_put(s99_map_hdr(%s, sizeof(%s), sizeof(%s), %d), %s, sizeof(%s), %d, &%s);", e, maphandle_addr, ctype(mt->key), ctype(mt->elem), key_kind(mt), keyaddr, ctype(mt->key), key_kind(mt), created); line("if (%s) %s", created, copy_stmt(mt->key, fmtstr("*(%s *)s99_map_key(%s)", ctype(mt->key), e), fmtstr("*(%s)", keyaddr))); if (mt->elem->has_defaults) { need(mt->elem, H_INIT); line("if (%s) init%d((%s *)s99_map_val(*%s, %s));", created, mt->elem->id, ctype(mt->elem), maphandle_addr, e); } return e; } static Str gen_index(Node *n, b32 forwrite) { Type *bt = n->a->type; Node *ix = n->list.data[0]; switch (bt->kind) { case TY_LIST: { Str l = forwrite ? gen_lvalue(n->a) : gen_value(n->a); Str i = gen_value(ix); if (optimizing()) { // the element type indexes the data, so its size is a constant Str ti = temp(t_int, i); if (fixed_int(ix)) add_pending(ix->sym, (Range){0, ((i64)1 << 48) - 1}); return fmtstr("((%s *)s99_list_data(%s, %s, %s))[%s]", ctype(bt->elem), l, ti, loc(n->pos), ti); } return fmtstr("(*(%s *)s99_list_at(%s, %s, %s))", ctype(bt->elem), l, i, loc(n->pos)); } case TY_ARRAY: { Str a = forwrite ? gen_lvalue(n->a) : gen_value(n->a); Str i = gen_value(ix); return fmtstr("%s.a[s99_chk(%s, %lld, %s)]", a, i, (long long)bt->count, loc(n->pos)); } case TY_MAP: { Str m = forwrite ? fmtstr("&%s", gen_lvalue(n->a)) : fmtstr("&%s", temp(bt, gen_value(n->a))); Str k = gen_addr(ix); Str e = map_entry(bt, m, k, forwrite, n->pos); return fmtstr("(*(%s *)s99_map_val(*%s, %s))", ctype(bt->elem), m, e); } default: errorat(n->pos, "star99 bug, please report it: cannot index"); } } // An expression usable on the left of an assignment, or whose address is // taken. Writes create what they need: a map entry, a union payload box. static Str gen_lvalue(Node *n) { switch ((int)n->kind) { case N_NAME: return var_expr(n->sym, n->pos); case N_FIELD: return gen_field(n, 1); case N_INDEX: return gen_index(n, 1); case N_DEREF: { Str r = gen_value(n->a); return fmtstr("(*(%s *)s99_deref(%s, %s))", ctype(n->type), r, loc(n->pos)); } default: return gen_expr(n); } } // A reference to a place: a route that is checked each time it is followed. // A variable or a struct field of one: a list there keeps its header when // assigned, so a reference into it can name the header. static b32 variable_place(Node *n) { while (n->kind == N_FIELD) { FieldAccess *fa = n->info; if (!fa || fa->kind != FA_STRUCT) return 0; n = n->a; } return n->kind == N_NAME; } static Str gen_ref(Node *n) { switch ((int)n->kind) { case N_NAME: { Sym *s = n->sym; if (s->co_path.len) { // a field of c: through its route Node c = {0}; c.kind = N_NAME; c.pos = n->pos; c.sym = s->co->param; define_type(s->co->type); return fmtstr("s99_ref_off(%s, offsetof(%s, f_%s) + offsetof(%s, f_%s))", gen_ref(&c), ctype(s->co->type), s->co_scope, ctype(s->co_holder), cident(s->name)); } if (s->isvar) { if (!own_local(s)) return fmtstr("(*%s)", var_addr(s, n->pos)); if (s->isparam && s->owner && s->owner->var_ptr) return fmtstr("s99_ref_cell(%s)", s->cname); return s->cname; } return fmtstr("s99_ref_cell(%s)", var_addr(s, n->pos)); } case N_FIELD: { FieldAccess *fa = n->info; if (!fa) return fmtstr("s99_ref_cell(&%s)", var_expr(n->sym, n->pos)); if (fa->kind == FA_STRUCT) { Str base = gen_ref(n->a); return fmtstr("s99_ref_off(%s, offsetof(%s, f_%s))", base, ctype(fa->base), cident(n->name)); } if (fa->kind == FA_UNION_ONE) { Type *u = fa->base; Type *pt = u->fields.data[0].type; Str ul = gen_lvalue(n->a); Str box = payload_box(u, 0, fmtstr("&%s", ul), n->pos); return fmtstr("s99_ref_off(s99_ref_box(%s), offsetof(%s, f_%s))", box, ctype(pt), cident(pt->fields.data[fa->indices[0]].name)); } errorat(n->pos, "& needs a single place; this field differs by variant"); } case N_INDEX: { Type *bt = n->a->type; Node *ix = n->list.data[0]; if (bt->kind == TY_LIST && !variable_place(n->a)) { // a list inside a list or map: a route through its place Str outer = gen_ref(n->a); Str i = gen_value(ix); return fmtstr("s99_ref_nest(%s, sizeof(%s), %s, %s)", outer, ctype(bt->elem), i, loc(n->pos)); } if (bt->kind == TY_LIST) { Str l = gen_lvalue(n->a); Str i = gen_value(ix); return fmtstr("s99_ref_list(&%s, sizeof(%s), %s, %s)", l, ctype(bt->elem), i, loc(n->pos)); } if (bt->kind == TY_ARRAY) { Str base = gen_ref(n->a); Str i = gen_value(ix); return fmtstr("s99_ref_off(%s, (int64_t)sizeof(%s) * s99_chk(%s, %lld, %s))", base, ctype(bt->elem), i, (long long)bt->count, loc(n->pos)); } if (bt->kind == TY_MAP) { Str m = fmtstr("&%s", gen_lvalue(n->a)); Str k = gen_addr(ix); Str e = map_entry(bt, m, k, 1, n->pos); return fmtstr("s99_ref_map(*%s, %s)", m, e); } errorat(n->pos, "& cannot point into a string"); } case N_DEREF: return gen_value(n->a); default: errorat(n->pos, "star99 bug, please report it: no place to refer to"); } } static Str gen_conv(Node *n) { Conv *cv = n->info; Node *in = n->a; if (cv->kind == CV_UNION) { Str v = gen_move(in); return wrap_union(n->type, cv, 0, v); } // into Any if (in->type == t_nil) return S("(s99_any){0}"); Str v = gen_move(in); need(in->type, H_INFO); Str a = temp_decl(t_any); line("%s.type = &info%d;", a, in->type->id); line("%s.box = s99_box_filled(sizeof(%s));", a, ctype(in->type)); line("*(%s *)S99_BOXDATA(%s.box) = %s;", ctype(in->type), a, v); return a; } static Str gen_variant(Node *n) { Type *u = n->type; iz vi = n->ival; Field *f = u->fields.data + vi; if (compact_union(u) && !(n->flags & F_CALLED)) // the variant with a zero payload return fmtstr("((%s){s99_box_new(sizeof(%s))})", ctype(u), ctype(f->type)); if (!(n->flags & F_CALLED) || !f->type || f->type == t_void) return fmtstr("(%s){%d, 0}", ctype(u), union_tag(u, vi)); return union_value(u, vi, gen_move(n->list.data[0])); } // Code generated into a buffer of its own, one level in, for a branch that // runs conditionally: begin_branch gives what end_branch puts back. typedef struct { Buf *body; int indent; } Branch; static Branch begin_branch(void) { Branch outer = {body, indent}; body = new(&perm, 1, Buf); indent++; return outer; } // Back outside the branch: the code it made. static Buf * end_branch(Branch outer) { Buf *b = body; body = outer.body; indent = outer.indent; return b; } // c without parentheses around all of it: if ((a == b)) makes clang warn static Str unparen(Str c) { if (c.len < 2 || c.data[0] != '(' || c.data[c.len - 1] != ')') return c; iz depth = 0; for (iz i = 0; i < c.len - 1; i++) { if (c.data[i] == '"' || c.data[i] == '\'') return c; // keep it simple depth += c.data[i] == '(' ? 1 : c.data[i] == ')' ? -1 : 0; if (!depth) return c; // (a) == (b) } return substr(c, 1, c.len - 1); } // Opens a context for temporaries inside the current one. static void open_ctx(StmtCtx *ctx) { memset(ctx, 0, sizeof(*ctx)); ctx->parent = sctx; ctx->frame = frame; sctx = ctx; } // The releases of the temporaries of ctx, newest first. static void release_temps(StmtCtx *ctx) { for (iz i = ctx->drops.len; i-- > 0;) lines(ctx->drops.data[i]); } // Releases the temporaries of ctx and closes it. static void close_ctx(StmtCtx *ctx) { release_temps(ctx); sctx = ctx->parent; } static Str gen_ifx(Node *n) { Str c = unparen(gen_value(n->a)); Str r = temp_decl(n->type); line("if (%s) {", c); indent++; StmtCtx bctx = {0}; open_ctx(&bctx); i32 p0 = npend; Str a = gen_move(n->b); npend = p0; if (n->b->type != t_void) set_result(r, a); // a branch that returns gives nothing close_ctx(&bctx); indent--; line("} else {"); indent++; StmtCtx cctx = {0}; open_ctx(&cctx); Str b = gen_move(n->c); npend = p0; if (n->c->type != t_void) set_result(r, b); close_ctx(&cctx); indent--; line("}"); return r.len ? r : S("((void)0)"); } static void push_frame(Node *loop, i32 label); static void pop_frame(void); static Str gen_blockx(Node *n) { Node *b = n->a; Str r = temp_decl(n->type); line("{"); indent++; push_frame(0, 0); for (iz i = 0; i + 1 < b->list.len; i++) gen_stmt(b->list.data[i]); Node *last = b->list.data[b->list.len - 1]; if (last->kind == N_EXPR && last->a->type != t_void) { // not one that returns StmtCtx ctx = {0}; open_ctx(&ctx); Str v = gen_move(last->a); set_result(r, v); close_ctx(&ctx); } else { gen_stmt(last); } pop_frame(); indent--; line("}"); return r.len ? r : S("((void)0)"); } // A string literal as a list that lives in static storage: borrowed only, // so it is never released or changed, and whoever keeps it copies it. static STATE Map *literals = 0; static STATE int nliterals = 0; static Str static_literal(Str text) { void **slot = upsert(&literals, text, 1); if (!*slot) { Str *name = new(&perm, 1, Str); *name = fmtstr("s99_lit%d", nliterals++); bputf(&out_globals, "static s99_list %s_ = {0, %lld, %lld, 1, (uint8_t *)%s, {0}};\n", *name, (long long)text.len, (long long)text.len, c_string(text)); bputf(&out_globals, "static s99_list *%s S99_UNUSED = &%s_;\n", *name, *name); *slot = name; } return *(Str *)*slot; } static Str gen_expr(Node *n) { switch ((int)n->kind) { case N_INT: return int_literal(n->type, n->ival); case N_FLOAT: return float_literal(n->fval); case N_STR: if (!optimizing()) return fmtstr("s99_lit(%s, %lld)", c_string(n->sval), (long long)n->sval.len); return static_literal(n->sval); case N_BOOL: return n->ival ? S("true") : S("false"); case N_TYPEX: case N_TYPENAME: { // a Type value: the table entry of that type Type *t = n->info; need(t, H_INFO); return fmtstr("(&info%d)", t->id); } case N_NIL: return gen_zero(n->type == t_nil ? ref_of(t_void) : n->type); case N_NAME: { Sym *s = n->sym; if (s->kind == S_FN) return gen_closure_value(s->fns.data[0], n->pos); return var_expr(s, n->pos); } case N_FIELD: return gen_field(n, 0); case N_INDEX: return gen_index(n, 0); case N_DEREF: { Str r = gen_value(n->a); return fmtstr("(*(%s *)s99_deref(%s, %s))", ctype(n->type), r, loc(n->pos)); } case N_ADDR: return gen_ref(n->a); case N_UNARY: { if (n->op == T_NOT) return fmtstr("(!%s)", gen_value(n->a)); Str a = gen_value(n->a); if (n->op == T_PLUS) return a; if (n->type->kind == TY_FLOAT) return fmtstr("(-%s)", a); if (n->a->kind == N_INT) return int_literal(n->type, (i64)(0 - (u64)n->a->ival)); return gen_arith(T_MINUS, n->type, int_literal(n->type, 0), a, n->pos); } case N_BINARY: { TokKind op = n->op; Type *t = n->a->type; if (op == T_AND || op == T_OR) { Str a = gen_value(n->a); Branch outer = begin_branch(); StmtCtx ctx = {0}; open_ctx(&ctx); i32 p0 = npend; Str b = gen_value(n->b); npend = p0; // the right side may not run sctx = ctx.parent; // its releases go in the branch, below Buf *bb = end_branch(outer); if (!bb->len && !ctx.drops.len) return fmtstr("(%s %s %s)", a, op == T_AND ? S("&&") : S("||"), b); Str r = temp(t_bool, a); line("if (%s%s) {", op == T_AND ? S("") : S("!"), r); bput(body, bstr(bb)); indent++; line("%s = %s;", r, b); release_temps(&ctx); indent--; line("}"); return r; } if (op == T_PLUS || op == T_MINUS || op == T_STAR || op == T_SLASH || op == T_PERCENT || op == T_STARSTAR) { Str fused = gen_affine(n); if (fused.len) return fused; Str a = gen_left(n->a, n->b), b = gen_value(n->b); return gen_arith_nodes(op, n->type, n->a, n->b, a, b, n->pos); } if (op == T_EQEQ || op == T_NE) { Type *ct = t == t_nil ? n->b->type : t; Str e = gen_eq(ct, n->a, n->b); return op == T_NE ? fmtstr("(!%s)", e) : e; } Str o = opname(op); // < > <= >= if (t->text) { Str pa = gen_addr(n->a), pb = gen_addr(n->b); return fmtstr("(s99_str_cmp(%s, %s) %s 0)", pa, pb, o); } Str a = gen_left(n->a, n->b), b = gen_value(n->b); return fmtstr("(%s %s %s)", a, o, b); } case N_IN: { Node *cn = n->b; Str r = {0}; if (cn->kind == N_RANGE) { Str x = temp(t_int, gen_value(n->a)); Str lo = gen_value(cn->a), hi = gen_value(cn->b); r = fmtstr("(%s %s %s && %s %s %s)", x, range_open_low(cn->op) ? S(">") : S(">="), lo, x, range_open_high(cn->op) ? S("<") : S("<="), hi); } else { Type *ct = cn->type; switch (ct->text ? TY_STRING : ct->kind) { case TY_LIST: case TY_ARRAY: { Type *et = ct->elem; b32 plain = et->kind == TY_INT || et->kind == TY_FLOAT || et->kind == TY_BOOL; if (!plain) need(et, H_INFO); Str x = gen_addr(n->a); Str c = gen_value(cn); Str res = temp(t_bool, S("false")); Str ec = ctype(et); Str e = ct->kind == TY_LIST ? fmtstr("((%s *)%s->data)[i]", ec, c) : fmtstr("%s.a[i]", c); Str eq = plain ? fmtstr("%s == *%s", e, x) : fmtstr("s99_equal(&info%d, &%s, %s)", et->id, e, x); line("for (int64_t i = 0; i < %s && !%s; i++) %s = %s;", ct->kind == TY_LIST ? fmtstr("s99_list_len(%s)", c) : fmtstr("%lld", (long long)ct->count), res, res, eq); r = res; break; } case TY_MAP: { Str x = gen_addr(n->a); Str c = gen_value(cn); r = fmtstr("(%s != 0)", map_get(cn->type, c, x)); break; } case TY_POOL: { Str x = gen_addr(n->a); Str c = gen_value(cn); r = fmtstr("s99_pool_has(%s, %s)", c, x); break; } case TY_STRING: { if (n->a->type == t_u8) { Str x = gen_value(n->a); Str c = gen_addr(cn); r = fmtstr("s99_str_has_byte(%s, %s)", c, x); break; } Str x = gen_addr(n->a); Str c = gen_addr(cn); r = fmtstr("s99_str_contains(%s, %s)", c, x); break; } default: errorat(n->pos, "star99 bug, please report it: in on a %s", type_name(cn->type)); } } return (n->flags & F_NOT) ? fmtstr("(!%s)", r) : r; } case N_LIST: return gen_list_literal(n); case N_VARIANT: return gen_variant(n); case N_CAST: { Str v = gen_value(n->b); Type *to = n->type, *from = n->b->type; if (to->kind == TY_FLOAT) return fmtstr("((double)(%s))", v); if (from->kind == TY_FLOAT) { if (to->issigned) return fmtstr("((%s)s99_f2i(%s, %s, %s, %s))", ctype(to), v, int_limits(to, S("lo")), int_limits(to, S("hi")), loc(n->pos)); return fmtstr("((%s)s99_f2u(%s, %s, %s))", ctype(to), v, int_limits(to, S("hi")), loc(n->pos)); } if (from == to) return v; // integer to integer: the value must fit, unless every value does if (!from->issigned ? (to->issigned ? from->bits < to->bits : from->bits <= to->bits) : (to->issigned && from->bits <= to->bits)) return fmtstr("((%s)%s)", ctype(to), v); if (from->issigned) return fmtstr("((%s)s99_i2i((int64_t)%s, %s, %s, %s))", ctype(to), v, int_limits(to, S("lo")), int_limits(to, S("hi")), loc(n->pos)); return fmtstr("((%s)s99_u2u((uint64_t)%s, %s, %s, %s))", ctype(to), v, int_limits(to, S("lo")), int_limits(to, S("hi")), loc(n->pos)); } case N_BLOCKX: return gen_blockx(n); case N_IFX: return gen_ifx(n); case N_SWITCHX: return gen_switch_value(n); case N_FNLIT: return gen_closure_value((Fn *)n->info, n->pos); case X_CONV: if (n->a->type == t_nil) return gen_zero(n->type); return gen_conv(n); case X_CALLFN: return gen_call(n); default: errorat(n->pos, "star99 bug, please report it: cannot generate this expression (%d)", (int)n->kind); } } // The environment of a nested function or closure: pointers to the // variables it captured. Closures that may outlive their scope keep the // environment on the heap; the variables themselves are pinned. static Str gen_env(Fn *f, Pos p) { if (!f->captures.len) return S("0"); Str e = tmpname(); if (f->escapes) { // counted: each function value holding it has a count, and it holds // a count of each counted cell it reaches line("%s *%s = (%s *)s99_rc_new(sizeof(%s), envdrop_%s);", env_type(f), e, env_type(f), env_type(f), f->cname); for (iz i = 0; i < f->captures.len; i++) { Sym *s = f->captures.data[i]; Str a = var_addr(s, p); line(rc_cell(s) ? "%s->%s = s99_rc_retain(%s);" : "%s->%s = %s;", e, cap_member(f, i), a); } return e; } line("%s %s;", env_type(f), e); for (iz i = 0; i < f->captures.len; i++) line("%s.%s = %s;", e, cap_member(f, i), var_addr(f->captures.data[i], p)); return fmtstr("&%s", e); } static Str fn_wrapper(Fn *f) { // a top-level function used as a value takes an unused environment return fmtstr("w%s", f->cname); } static Str gen_closure_value(Fn *f, Pos p) { f->used = 1; use_fn(f); if (!f->isclosure) { f->escapes = 1; // marks that the wrapper is needed return fmtstr("((s99_fn){(void (*)(void))%s, 0, 0})", fn_wrapper(f)); } if (genfn == f) { // itself, from inside: a counted environment gets one more count if (f->escapes && f->captures.len) return fmtstr("((s99_fn){(void (*)(void))%s, s99_rc_retain(envp), 1})", f->cname); return fmtstr("((s99_fn){(void (*)(void))%s, envp, 0})", f->cname); } Str env = gen_env(f, p); return fmtstr("((s99_fn){(void (*)(void))%s, (void *)%s, %d})", f->cname, env, f->escapes && f->captures.len ? 1 : 0); } static Str fn_ptr_type(Type *ft) { Buf b = {0}; bputf(&b, "%s (*)(void *", ctype(ft->elem)); for (iz i = 0; i < ft->fields.len; i++) { bput(&b, S(", ")); Field *f = ft->fields.data + i; bput(&b, f->isvar ? S("s99_ref") : ctype(f->type)); } bputc(&b, ')'); return bstr(&b); } static Str gen_arg(Field *p, Node *a) { if (p->isvar) return gen_ref(a->a); return gen_value(a); } static void gen_print_bare(Node *a, Str pre); static Str *print_bares_first(Node **a, iz n); static b32 fn_writes(Fn *f); // A plain argument is a value, though it is passed without a copy. When the // call can change what it borrows, through a var argument rooted at the // same variable, or through a global or reference when the callee stores // outside itself, it gets a copy first. static b32 arg_may_change(Fn *f, Call *c, Node *a) { if (!a || !a->type || !a->type->managed) return 0; if (a->kind != N_NAME && a->kind != N_FIELD && a->kind != N_INDEX && a->kind != N_DEREF) return 0; // a new value, not a borrowed place Sym *rs = place_sym(a); // what a coroutine keeps is in its struct, which the callee could reach b32 kept = rs && rs->co_path.len; if (rs && rs->kind == S_VAR && rs->isconst && !rs->isglobal && !kept) return 0; // nothing changes it b32 local = rs && rs->kind == S_VAR && !rs->isglobal && !rs->isvar && !rs->iscaptured && rs->owner == genfn && !kept; Fields *ps = &f->type->fields; for (iz j = 0; j < ps->len && j < c->nargs; j++) { Node *va = c->args[j]; // &x for a var parameter or as a Ref if (va && va->kind == N_ADDR && (!local || place_sym(va->a) == rs)) return 1; } // a global, a var parameter, a reference, or a local some Ref reaches return (!local || rs->pinned) && fn_writes(f); } static Str call_args(Fn *f, Call *c, iz skip, Str skipval) { Buf b = {0}; b32 first = 1; if (f->isclosure) { Str env = {0}; if (genfn == f) { env = S("envp"); } else { env = gen_env(f, c->args && c->nargs && c->args[0] ? c->args[0]->pos : (Pos){-1, 0, 0, 0}); // a direct call holds the counted environment only while it runs if (f->escapes && f->captures.len && sctx) *push(&perm, &sctx->drops) = fmtstr("s99_rc_release(%s);", env); } bput(&b, env); first = 0; } Fields *ps = &f->type->fields; for (iz i = 0; i < ps->len; i++) { if (!first) bput(&b, S(", ")); first = 0; if (i == skip) { bput(&b, skipval); continue; } Node *a = c->args[i]; if (!a) a = ps->data[i].def; if (ps->data[i].isvar && f->var_ptr) { // a stable place: its address Node *t = a->a; if (t->kind == N_NAME && t->sym->isvar && t->sym->isparam && t->sym->owner && t->sym->owner->var_ptr) bput(&b, t->sym->cname); else bputf(&b, "&%s", gen_lvalue(t)); continue; } if (!ps->data[i].isvar && arg_may_change(f, c, a)) { // its own copy, released after the statement Str v = gen_expr(a), copy = temp_decl(a->type); line("%s", copy_stmt(a->type, copy, v)); if (sctx) *push(&perm, &sctx->drops) = drop_stmt(a->type, copy); bput(&b, copy); continue; } bput(&b, gen_arg(ps->data + i, a)); } return bstr(&b); } static Str gen_dispatch(Node *n, Call *c) { Type *u = c->dispatch_union; Node *subject = c->args[c->dispatch]; Str uaddr = gen_addr(subject); b32 isvoid = n->type == t_void; Str r = isvoid ? (Str){0} : temp_decl(n->type); line("switch (%s) {", utag(u, uaddr)); for (iz v = 0; v < u->fields.len; v++) { Fn *f = c->cases[v]; line("case %d: {", union_tag(u, v)); indent++; StmtCtx ctx = {0}; open_ctx(&ctx); Type *pt = u->fields.data[v].type; Str pay = {0}; if (f->type->fields.data[c->dispatch].isvar) { pay = fmtstr("s99_ref_box(%s)", payload_box(u, v, uaddr, n->pos)); } else { pay = pt && pt != t_void ? union_payload(u, v, uaddr, n->pos) : S("0"); } Call cc = *c; cc.fn = f; cc.args = c->case_args[v]; // converted for this case cc.nargs = f->type->fields.len; Str args = call_args(f, &cc, c->dispatch, pay); use_fn(f); Str call = fmtstr("%s(%s)", f->cname, args); if (isvoid) { line("%s;", call); } else if (f->type->elem == n->type) { line("%s = %s;", r, call); } else { Str one = temp(f->type->elem, call); Conv cv = {0}; union_path(n->type, f->type->elem, &cv, 0); Str w = wrap_union(n->type, &cv, 0, one); line("%s = %s;", r, w); } close_ctx(&ctx); line("break;"); indent--; line("}"); } line("default: s99_fail(%s, \"%s is called on nil, and it has no case for nil\");", loc(n->pos), c->cases[0]->name); line("}"); return isvoid ? S("((void)0)") : r; } static Str container_addr(Node *a) { // the first argument of add, insert and remove: &container return fmtstr("&%s", gen_lvalue(a->a)); } // The values printed into a new stream: its name static Str printed(Node **a, iz n) { Str st = tmpname(); Str *pre = print_bares_first(a, n); line("s99_stream %s = {0};", st); line("s99_stream *%s_out = s99_out;", st); line("s99_out = &%s;", st); for (iz i = 0; i < n; i++) gen_print_bare(a[i], pre[i]); line("s99_out = %s_out;", st); return st; } static Str gen_builtin(Node *n, Call *c) { Node **a = c->args; iz e = builtin_entry(c->builtin); if (e >= 0 && builtin_table[e].c.len) { // a fixed signature: its C, with the arguments evaluated in order Str v[4] = {0}; for (iz i = 0; i < c->nargs && i < 4; i++) v[i] = gen_value(a[i]); Str t = builtin_table[e].c; Buf b = {0}; for (iz i = 0; i < t.len; i++) { if (t.data[i] != '%') { bputc(&b, t.data[i]); } else if (t.data[++i] == '@') { bput(&b, loc(n->pos)); } else { bput(&b, v[t.data[i] - '0']); } } return bstr(&b); } switch (c->builtin) { case B_PRINT: case B_PRINTLN: { // print(stream, values...): a File first chooses where; a zero File // is stdout. Print overloads write to the stream being printed to. b32 to = c->nargs && a[0]->type == t_file; Str save = tmpname(); Str f = to ? temp(t_file, gen_value(a[0])) : (Str){0}; Str *pre = print_bares_first(a + to, c->nargs - to); if (to) { line("s99_stream *%s_out = s99_out;", save); line("s99_out = s99_file(%s);", f); } for (iz i = to; i < c->nargs; i++) gen_print_bare(a[i], pre[i - to]); if (c->builtin == B_PRINTLN) line("s99_put(s99_out, \"\\n\", 1);"); if (to) line("s99_out = %s_out;", save); return S("((void)0)"); } case B_STR: return fmtstr("s99_stream_str(&%s)", printed(a, c->nargs)); case B_LEN: { Type *t = a[0]->type; Str v = gen_value(a[0]); switch (t->kind) { case TY_LIST: return fmtstr("s99_list_len(%s)", v); case TY_ARRAY: return fmtstr("((void)%s, INT64_C(%lld))", v, (long long)t->count); case TY_MAP: return fmtstr("s99_map_len(%s)", v); case TY_POOL: return fmtstr("s99_pool_count(%s)", v); default: break; } break; } case B_ADD: { Type *t = a[0]->type->elem; if (t->text) { Str ca = container_addr(a[0]); for (iz i = 1; i < c->nargs; i++) { if (a[i]->type == t_u8) { gen_push(a[0], ca, t_u8, temp(t_u8, gen_value(a[i]))); continue; } if (optimizing() && a[i]->kind == N_STR) { line("s99_str_append(%s, %s, %lld);", ca, c_string(a[i]->sval), (long long)a[i]->sval.len); continue; } if (optimizing() && a[i]->type->text) { line("s99_str_append_str(%s, %s);", ca, gen_value(a[i])); continue; } if (optimizing() && a[i]->type == t_int) { line("s99_str_append_int(%s, %s);", ca, gen_value(a[i])); continue; } Str st = printed(a + i, 1); line("s99_str_append(%s, %s.buf, %s.len);", ca, st, st); line("s99_stream_free(&%s);", st); } return S("((void)0)"); } Str ec = ctype(t->elem); Str v = gen_move(a[1]); Str v2 = temp(t->elem, v); // a container outside the area step: grown, and given a copy, outside Str ca = container_addr(a[0]); if (t->kind == TY_LIST) { gen_push(a[0], ca, t->elem, v2); return S("((void)0)"); } Str d = tmpname(); line("void *%s;", d); Str r = temp(ref_of(t->elem), fmtstr("s99_pool_add(%s, sizeof(%s), &%s)", ca, ec, d)); line("*(%s *)%s = %s;", ec, d, v2); return r; } case B_RESIZE: { // shorter: the tail is released; longer: new elements start as any // new value does, zero with defaults Type *t = a[0]->type->elem; Type *et = t->elem; define_type(et); // sizeof needs the whole element type Str ec = ctype(et); Str len = temp(t_int, gen_value(a[1])); Str ca = container_addr(a[0]); Str old = tmpname(); if (et->managed || et->has_defaults) line("int64_t %s = s99_list_len(*%s);", old, ca); if (et->managed) { line("for (int64_t i = %s; i-- > %s;) %s", old, len, drop_stmt(et, fmtstr("((%s *)(*%s)->data)[i]", ec, ca))); } line("s99_list_resize(%s, sizeof(%s), %s, %s);", ca, ec, len, loc(n->pos)); if (et->has_defaults) { need(et, H_INIT); line("for (int64_t i = %s; i < %s; i++) init%d(&((%s *)(*%s)->data)[i]);", old, len, et->id, ec, ca); } return S("((void)0)"); } case B_INSERT: { Type *t = a[0]->type->elem; Str ec = ctype(t->elem); Str i = temp(t_int, gen_value(a[1])); Str v = temp(t->elem, gen_move(a[2])); Str ca = container_addr(a[0]); line("*(%s *)s99_list_insert(%s, sizeof(%s), %s, %s) = %s;", ec, ca, ec, i, loc(n->pos), v); return S("((void)0)"); } case B_REMOVE: { Type *t = a[0]->type->elem; Str ca = container_addr(a[0]); if (t->kind == TY_LIST) { Str ec = ctype(t->elem); Str i = temp(t_int, gen_value(a[1])); Str p = tmpname(); line("%s *%s = (%s *)s99_list_at(*%s, %s, %s);", ec, p, ec, ca, i, loc(n->pos)); lines(drop_stmt(t->elem, fmtstr("*%s", p))); line("(void)%s;", p); line("s99_list_cut(*%s, %s);", ca, i); } else if (t->kind == TY_POOL) { Str ec = ctype(t->elem); Str r = temp(ref_of(t->elem), gen_value(a[1])); Str p = tmpname(); line("%s *%s = (%s *)s99_pool_item(*%s, &%s, %s);", ec, p, ec, ca, r, loc(n->pos)); lines(drop_stmt(t->elem, fmtstr("*%s", p))); line("(void)%s;", p); line("s99_pool_free_slot(*%s, %s.index);", ca, r); } else { Str k = gen_addr(a[1]); Str e = tmpname(); line("s99_mapent *%s = %s;", e, map_get(t, fmtstr("*%s", ca), k)); line("if (%s) {", e); indent++; lines(drop_stmt(t->key, fmtstr("*(%s *)s99_map_key(%s)", ctype(t->key), e))); lines(drop_stmt(t->elem, fmtstr("*(%s *)s99_map_val(*%s, %s)", ctype(t->elem), ca, e))); line("%s->alive = 0;", e); line("%s->gen = 0;", e); line("(*%s)->count--;", ca); line("s99_map_compact(*%s);", ca); indent--; line("}"); } return S("((void)0)"); } case B_ABS: { Type *t = a[0]->type; Str v = temp(t, gen_value(a[0])); if (t->kind == TY_FLOAT) return fmtstr("fabs(%s)", v); if (!t->issigned) return v; return fmtstr("(%s < 0 ? %s : %s)", v, gen_arith(T_MINUS, t, int_literal(t, 0), v, n->pos), v); } case B_LITERAL: { Type *t = a[0]->type; need(t, H_INFO); Str v = gen_addr(a[0]); Str st = tmpname(); line("s99_stream %s = {0};", st); line("s99_show(&%s, &info%d, %s, S99_LITERAL);", st, t->id, v); Sym *op = code_sym(S("literal_code")); use_fn(op->fns.data[0]); return fmtstr("%s(s99_stream_str(&%s), s99_here)", op->fns.data[0]->cname, st); } case B_PLACE: { Str t = temp(t_string, gen_value(a[0])); Str x = temp(t_string, gen_value(a[1])); line("s99_put(&s99_replies, \"P\", 1);"); line("s99_enc_str(&s99_replies, &%s);", t); line("s99_enc_str(&s99_replies, &%s);", x); line("fflush(s99_replies.f);"); return S("((void)0)"); } case B_HERE: { Type *site = code_type(S("Site")); Str t = temp_decl(site); line("%s", copy_stmt(site, t, S("s99_here"))); return t; } case B_ERROR: case B_WARNING: { Type *site = code_type(S("Site")); need(site, H_INFO); Str at = gen_addr(a[0]); Str st = printed(a + 1, c->nargs - 1); line("s99_str %s_msg = s99_stream_str(&%s);", st, st); line("s99_put(&s99_replies, \"%s\", 1);", c->builtin == B_ERROR ? S("E") : S("W")); line("s99_enc(&s99_replies, &info%d, %s);", site->id, at); line("s99_enc_str(&s99_replies, &%s_msg);", st); line("fflush(s99_replies.f);"); if (c->builtin == B_ERROR) line("exit(1);"); return S("((void)0)"); } case B_DRAW: { Str d = temp(t_draw, gen_value(a[0])); Node *x = a[1]; if (x->kind == N_RANGE) { Str lo = temp(t_int, gen_value(x->a)), hi = temp(t_int, gen_value(x->b)); if (range_open_low(x->op)) { line("if (%s >= %s) s99_fail(%s, \"draw from an empty range\");", lo, hi, loc(n->pos)); line("%s++;", lo); // lo < hi guarantees lo is below Int maximum } line("if (%s %s %s) s99_fail(%s, \"draw from an empty range\");", lo, range_open_high(x->op) ? S(">=") : S(">"), hi, loc(n->pos)); return fmtstr("s99_draw_int(%s, %s, %s%s)", d, lo, hi, range_open_high(x->op) ? S(" - 1") : S("")); } if (x->kind == N_TYPENAME) { Type *t = x->info; need(t, H_INFO); Str v = temp_decl(t); line("s99_arb(%s, &info%d, &%s);", d, t->id, v); return v; } Type *lt = x->type; Str l = gen_addr(x); Str i = tmpname(); Str count = lt->kind == TY_LIST ? fmtstr("s99_list_len(*%s)", l) : fmtstr("%lld", (long long)lt->count); line("if (%s == 0) s99_fail(%s, \"draw from an empty list\");", count, loc(n->pos)); line("int64_t %s = s99_draw_below(%s, %s);", i, d, count); Str elem = lt->kind == TY_LIST ? fmtstr("((%s *)(*%s)->data)[%s]", ctype(lt->elem), l, i) : fmtstr("(%s)->a[%s]", l, i); Str v = temp_decl(lt->elem); line("%s", copy_stmt(lt->elem, v, elem)); return v; } case B_EXPANDED: (void)gen_value(a[0]); line("s99_put(&s99_replies, \"X\", 1);"); line("fflush(s99_replies.f);"); return S("s99_dec_str(&s99_requests)"); case B_ASK: { // the question, where it is asked, and its values; the compiler // answers with the encoded result Type *rt = n->type; need(rt, H_INFO); Str r = temp_decl(rt); if (!meta_mode) { line("s99_fail(%s, \"questions about the program work only while compiling\");", loc(n->pos)); return r; } Str at = loc(n->pos); Str vals[16] = {0}; iz nv = 0; for (iz i = 2; i < c->nargs && nv < 16; i++) vals[nv++] = gen_addr(a[i]); line("memset(&%s, 0, sizeof %s);", r, r); line("if (!s99_not_final) {"); // inside known(...) once one was not final: zero indent++; line("s99_put(&s99_replies, \"Q\", 1);"); line("s99_enc_str(&s99_replies, &(s99_str){s99_lit(%s, %lld)});", c_string(a[1]->sval), (long long)a[1]->sval.len); line("s99_enc_str(&s99_replies, &(s99_str){s99_lit(%s, %lld)});", at, (long long)(at.len - 2)); line("s99_put(&s99_replies, s99_known_depth ? \"1\" : \"0\", 1);"); // inside known(...) for (iz i = 0; i < nv; i++) { need(a[i + 2]->type, H_INFO); line("s99_enc(&s99_replies, &info%d, %s);", a[i + 2]->type->id, vals[i]); } line("fflush(s99_replies.f);"); need(t_bool, H_INFO); Str final = temp_decl(t_bool); line("s99_dec(&s99_requests, &info%d, &%s);", t_bool->id, final); line("if (!%s) s99_not_final = 1;", final); line("else s99_dec(&s99_requests, &info%d, &%s);", rt->id, r); indent--; line("}"); return r; } case B_ENCODE: { Type *t = a[0]->type; need(t, H_INFO); Str v = gen_addr(a[0]); Str st = tmpname(); line("s99_stream %s = {0};", st); line("s99_enc(&%s, &info%d, %s);", st, t->id, v); return fmtstr("s99_stream_str(&%s)", st); } case B_DECODE: { Type *rt = n->type; need(rt, H_INFO); Str x = temp(t_string, gen_value(a[1])); Str r = temp_decl(rt); line("s99_decoded(%s, &info%d, &%s);", x, rt->id, r); return r; } case B_KNOWN: { Str r = {0}; if (!meta_mode) { r = temp_decl(n->type); line("s99_fail(%s, \"questions about the program work only while compiling\");", loc(n->pos)); return r; } line("s99_known_depth++;"); Str v = gen_value(a[0]); // released at the end of the statement: give a copy line("s99_known_depth--;"); r = temp_decl(n->type); line("%s", copy_stmt(n->type, r, v)); Str drop = drop_stmt(n->type, r); line("if (s99_not_final) {"); // nil: not final yet indent++; line("s99_not_final = s99_known_depth > 0;"); if (drop.len) line("%s", drop); line("memset(&%s, 0, sizeof %s);", r, r); indent--; line("}"); return r; } case B_FORMAT: { // the compiler formats it: ask, then read the answer Str t = temp(t_string, gen_value(a[0])); Str at = loc(n->pos); // where the call is, for an error in the text line("s99_put(&s99_replies, \"F\", 1);"); line("s99_enc_str(&s99_replies, &(s99_str){s99_lit(%s, %lld)});", at, (long long)(at.len - 2)); line("s99_enc_str(&s99_replies, &%s);", t); line("fflush(s99_replies.f);"); return S("s99_dec_str(&s99_requests)"); } case B_FAIL: { Str m = temp(t_string, gen_value(a[0])); return fmtstr("s99_fail_n(%s, s99_str_data(%s), s99_str_len(%s))", loc(n->pos), m, m); } case B_YIELD: return gen_yield(n, c); default: break; } errorat(n->pos, "star99 bug, please report it: this built-in has no code yet"); } static Str gen_call(Node *n) { Call *c = n->info; switch (c->kind) { case CALL_FN: if (c->fn->builtin) return gen_builtin(n, c); if (c->fn->yields) return gen_inline_call(n, c); use_fn(c->fn); return fmtstr("%s(%s)", c->fn->cname, call_args(c->fn, c, -1, (Str){0})); case CALL_VALUE: { Type *ft = c->callee->type; Str f = temp(ft, gen_value(c->callee)); line("s99_check_fn(%s, %s);", f, loc(n->pos)); Buf b = {0}; bputf(&b, "((%s)%s.fn)(%s.env", fn_ptr_type(ft), f, f); for (iz i = 0; i < c->nargs; i++) { bput(&b, S(", ")); bput(&b, gen_arg(ft->fields.data + i, c->args[i])); } bputc(&b, ')'); return bstr(&b); } case CALL_CTOR: return gen_ctor(c->type, c->args, c->nargs); case CALL_ZERO: { Str z = gen_zero(c->type); if (c->type->kind == TY_STRUCT || c->type->kind == TY_UNION || c->type->kind == TY_ARRAY) return temp(c->type, z); return z; } case CALL_DISPATCH: return gen_dispatch(n, c); } return S("0"); } // Printing: user print overloads first, the built-in format otherwise. static Str user_print(Type *t) { Fn *f = lookup(print_overloads, fmtstr("%d", t->id)); use_fn(f); return f ? f->cname : (Str){0}; } // A value that printing it cannot print anything else: no call in it. static b32 quiet_value(Node *a) { switch ((int)a->kind) { case N_NAME: case N_INT: case N_FLOAT: case N_STR: case N_NIL: return 1; case N_FIELD: return quiet_value(a->a); case N_INDEX: for (iz i = 0; i < a->list.len; i++) if (!quiet_value(a->list.data[i])) return 0; return quiet_value(a->a); default: return 0; } } // Every value is worked out before any is printed: a call among them may // print too (str(f()) holds what f returns, not what it prints). static Str * print_bares_first(Node **a, iz n) { Str *pre = new(&perm, n + 1, Str); for (iz i = 0; i < n; i++) if (!quiet_value(a[i])) pre[i] = temp(a[i]->type, gen_value(a[i])); return pre; } static void gen_print_bare(Node *a, Str pre) { Type *t = a->type; Str up = user_print(t); #define VALUE() (pre.len ? pre : gen_value(a)) if (up.len) { Str v = VALUE(); line("%s(%s);", up, v); return; } if (optimizing() && a->kind == N_STR) { line("s99_put(s99_out, %s, %lld);", c_string(a->sval), (long long)a->sval.len); return; } switch (t->kind) { case TY_INT: if (t->issigned) line("s99_put_int(s99_out, (int64_t)%s);", VALUE()); else line("s99_put_uint(s99_out, (uint64_t)%s);", VALUE()); return; case TY_FLOAT: line("s99_put_float(s99_out, %s);", VALUE()); return; case TY_BOOL: line("s99_puts(s99_out, %s ? \"true\" : \"false\");", VALUE()); return; default: { need(t, H_INFO); Str p = pre.len ? fmtstr("&%s", pre) : gen_addr(a); line("s99_show(s99_out, &info%d, %s, S99_PRINT);", t->id, p); } } #undef VALUE } // Statements static void push_frame(Node *loop, i32 label) { Frame *f = new(&perm, 1, Frame); f->parent = frame; f->loop = loop; f->label = label; frame = f; } // Releases the locals of a frame and runs its defers, newest first. Each // defer is a statement of its own, with its own temporaries. static void exit_frame(Frame *f) { for (iz i = f->defers.len; i-- > 0;) gen_stmt(f->defers.data[i]); for (iz i = f->exits.len; i-- > 0;) lines(f->exits.data[i]); // Inside an area the end of the step gives the memory back: releasing // the locals one by one would only walk them. Nothing can tell: a // stored Ref pins its target in a cell of its own, outside the area. for (Frame *g = f; g && !g->isfn; g = g->parent) { if (g->isarea) return; } for (iz i = f->locals.len; i-- > 0;) { Sym *s = f->locals.data[i]; if (s->pinned) { // a counted cell: closures may still hold it; a cell a stored Ref // reaches stays for the rest of the program if (rc_cell(s)) line("s99_rc_release(%s);", s->cname); continue; } if (s->moved_out) continue; // returned as it is lines(drop_stmt(s->type, var_expr(s, s->pos))); } } static void pop_frame(void) { exit_frame(frame); frame = frame->parent; } // A jump out to the frame `to` (included): leaves every frame from the // innermost one out, each after the temporaries of the statements still // open in it, which the jump would otherwise skip with the code that // releases them after those statements (a switch on a call result keeps it // to the end of the switch). `to` itself is left only when exit_to is set. static void leave_frames(Frame *to, b32 exit_to) { StmtCtx *c = sctx; for (Frame *f = frame; f; f = f->parent) { for (; c && c->frame == f; c = c->parent) release_temps(c); if (f == to && !exit_to) return; exit_frame(f); if (f == to) return; } } // Whether a runtime function may run code of the program: an overload // (print, ==, valid, arbitrary), a starting value, or a function value. static b32 co_rt_runs_program(int f) { static STATE int known[AW_FNS] = {0}; // 0 unknown, 1 yes, 2 no static STATE b32 busy[AW_FNS] = {0}; if (known[f]) return known[f] == 1; if (busy[f]) return 0; // on the way: answered where it started busy[f] = 1; b32 yes = 0; for (iz i = aw.line[f] + 1; i < aw.line[f + 1] && !yes; i++) { Str l = runtime_lines[i]; if (!startswith(l, S(" "))) continue; static Str calls[] = {SL("->eq("), SL("->print("), SL("->init("), SL("->arb("), SL("->valid("), SL("fn)(")}; for (iz k = 0; k < countof(calls) && !yes; k++) yes = indexof(l, calls[k]) >= 0; } aw_read(f); for (int k = 0; k < aw.count[f] && !yes; k++) yes = co_rt_runs_program(aw.callee[aw.first[f] + k]); busy[f] = 0; known[f] = yes ? 1 : 2; return yes; } // Whether C code written for a statement of a resume cannot move the // coroutine's struct: the place its route names moves only when a list, // pool, map or box that holds it changes, which takes code that reaches // past the struct and resume's own locals: a function of the program or a // function value called, a runtime function that runs one, a global, or // another route. Anything else it calls is allowed by name. static b32 co_moves_nothing(Str text, Str cname) { aw_index(); u8 *t = text.data, *more = name_bytes(); iz n = text.len; for (iz i = 0; i < n; i++) { u8 ch = t[i]; if (ch == '"' || ch == '\'') { // a literal for (i++; i < n && t[i] != ch; i++) if (t[i] == '\\') i++; continue; } if (isdigitc(ch)) { while (i + 1 < n && more[t[i + 1]]) i++; continue; } if (!more[ch]) continue; iz a = i; while (i + 1 < n && more[t[i + 1]]) i++; Str w = substr(text, a, i + 1); iz j = i + 1; while (j < n && t[j] == ' ') j++; b32 call = j < n && t[j] == '('; iz b = a; while (b > 0 && t[b - 1] == ' ') b--; if ((b > 0 && t[b - 1] == '.') || (b > 1 && t[b - 1] == '>' && t[b - 2] == '-')) { if (equals(w, S("fn"))) return 0; // a function value, called continue; } if (!call) { if (w.len > 1 && w.data[0] == 'g' && isdigitc(w.data[1])) return 0; // a global continue; } static Str ok[] = {SL("if"), SL("while"), SL("for"), SL("switch"), SL("return"), SL("sizeof"), SL("offsetof"), SL("INT64_C"), SL("UINT64_C"), SL("memcpy"), SL("memset"), SL("memcmp"), SL("strlen"), SL("snprintf")}; b32 fine = 0; for (iz k = 0; k < countof(ok) && !fine; k++) fine = equals(w, ok[k]); if (fine) continue; if (w.len > 4 && startswith(w, S("S99_"))) continue; // a macro if (w.len > 4 && (startswith(w, S("drop")) || startswith(w, S("copy"))) && isdigitc(w.data[4])) continue; // a value's own release and copy if (w.len <= 4 || !startswith(w, S("s99_"))) return 0; // the program's int f = aw_find(w); if (f < 0 || co_rt_runs_program(f)) return 0; if (startswith(w, S("s99_deref"))) { // only c's route iz k = j + 1; while (k < n && t[k] == ' ') k++; if (k + cname.len >= n || !startswith(substr(text, k, n), cname) || more[t[k + cname.len]]) return 0; } } return 1; } // The C body[from..] of a statement of a resume whose c is a route: when it // cannot move the struct, it finds the struct once, at its start, and reaches // it through that pointer. The pointer is also found as resume starts, so a // resume that continues at a yield point inside the statement has it. static void co_cache_stmt(iz from) { Coro *co = co_gen; Str cname = co->param->cname; Str text = {body->data + from, body->len - from}; Str pat = fmtstr("(*(%s *)s99_deref(%s, ", ctype(co->type), cname); // inner statements may have found it already: once is enough Str again = fmtstr("%s_p = (%s *)s99_deref(%s, ", cname, ctype(co->type), cname); iz q = -1; // the site of the first use: where a place that is gone is reported for (iz i = 0; i < text.len && q < 0; i++) { if (i + pat.len <= text.len && !memcmp(text.data + i, pat.data, (size_t)pat.len)) q = i + pat.len; else if (i + again.len <= text.len && !memcmp(text.data + i, again.data, (size_t)again.len)) q = i + again.len; } if (q < 0 || text.data[q] != '"' || !co_moves_nothing(text, cname)) return; Buf nb = {0}; // after what a debug build starts the statement with: its #line and S99_AT iz head = 0; Str dir = {0}; // and the pointer is at the statement's line too for (;;) { iz k = head; while (k < text.len && text.data[k] == ' ') k++; Str rest = substr(text, k, text.len); if (!startswith(rest, S("#line ")) && !startswith(rest, S("S99_AT("))) break; iz b = k; while (k < text.len && text.data[k] != '\n') k++; if (startswith(rest, S("#line "))) dir = substr(text, b, k); head = k < text.len ? k + 1 : k; } bput(&nb, substr(text, 0, head)); if (dir.len) { bput(&nb, dir); bputc(&nb, '\n'); } iz ind = head; while (ind < text.len && text.data[ind] == ' ') ind++; for (iz k = head; k < ind; k++) bputc(&nb, ' '); iz e = q + 1; while (e < text.len && text.data[e] != '"') e += text.data[e] == '\\' ? 2 : 1; bputf(&nb, "%s_p = (%s *)s99_deref(%s, %s);\n", cname, ctype(co->type), cname, substr(text, q, e + 1)); for (iz i = head; i < text.len;) { if ((i == 0 || text.data[i - 1] == '\n')) { iz k = i; while (k < text.len && text.data[k] == ' ') k++; if (k + again.len <= text.len && !memcmp(text.data + k, again.data, (size_t)again.len)) { while (k < text.len && text.data[k] != '\n') k++; i = k + 1; continue; } } if (i + pat.len <= text.len && !memcmp(text.data + i, pat.data, (size_t)pat.len)) { iz k = i + pat.len; if (text.data[k] == '"') { k++; while (k < text.len && text.data[k] != '"') k += text.data[k] == '\\' ? 2 : 1; if (k + 2 < text.len && text.data[k + 1] == ')' && text.data[k + 2] == ')') { bputf(&nb, "(*%s_p)", cname); i = k + 3; continue; } } } bputc(&nb, text.data[i++]); } body->len = from; bput(body, bstr(&nb)); } static void gen_stmt(Node *n) { StmtCtx ctx = {0}; i32 p0 = npend; Pos saved_at = gen_at, saved_dbg = dbg_at; iz from = body->len; b32 cache = co_route_body && body == co_route_body; gen_at = n->pos; // allocates(f): the statement code is generated for dbg_at = n->pos; if (dbg_framed && genfn) line("S99_AT(%s);", loc(stmt_start(n->pos))); kill_changed(n, 1); open_ctx(&ctx); gen_stmt_in(n); close_ctx(&ctx); if (cache && body == co_route_body) co_cache_stmt(from); gen_at = saved_at; dbg_at = saved_dbg; kill_changed(n, 0); commit_pending(p0, n); } static void gen_block_stmts(Node *b, b32 newframe) { if (newframe) push_frame(0, 0); i32 known = nfacts; Node *saveb = cur_block; iz saves = cur_stmt; cur_block = b; for (iz i = 0; i < b->list.len; i++) { cur_stmt = i; gen_stmt(b->list.data[i]); } cur_block = saveb; cur_stmt = saves; nfacts = known; range_epoch++; if (newframe) { Pos saved_dbg = dbg_at; dbg_at = block_end(b, dbg_at); // what the block releases, at its brace pop_frame(); dbg_at = saved_dbg; } } // Borrowed constants. const k = xs[i] need not copy when nothing can change // xs while k lives: xs is a local of this function that no reference or // closure reaches, and the rest of the block neither assigns into it nor // takes its address (which add, remove and var arguments need). Then k // shares the storage and is not released. static Sym * place_root(Node *n) { for (;;) { if (n->kind == N_FIELD) { FieldAccess *fa = n->info; if (!fa || fa->kind != FA_STRUCT) return 0; n = n->a; } else if (n->kind == N_INDEX) { Type *t = n->a->type; if (t->kind != TY_LIST && t->kind != TY_ARRAY) return 0; n = n->a; } else { break; } } if (n->kind != N_NAME || !n->sym || n->sym->kind != S_VAR) return 0; return n->sym; } // n passes v by address somewhere (a callee may change it). static b32 takes_addr(Node *n, Sym *v) { if (!n) return 0; if (n->kind == N_ADDR && place_sym(n->a) == v) return 1; if (n->kind == N_FN || n->kind == N_FNLIT) return 0; if ((int)n->kind == X_CALLFN) { Call *c = n->info; for (iz i = 0; i < c->nargs; i++) { if (takes_addr(c->args[i], v)) return 1; } } if (takes_addr(n->a, v) || takes_addr(n->b, v)) return 1; if (n->kind != N_FOR && takes_addr(n->c, v)) return 1; for (iz i = 0; i < n->list.len; i++) { if (takes_addr(n->list.data[i], v)) return 1; } return 0; } static b32 changes(Node *n, Sym *v) { if (!n) return 0; switch ((int)n->kind) { case N_ASSIGN: if (place_sym(n->a) == v) return 1; break; case N_ADDR: if (place_sym(n->a) == v) return 1; break; case N_SWITCH: // switch &x: its cases may change x if ((n->flags & F_REF) && place_sym(n->a) == v) return 1; break; case N_RETURN: if (n->a && n->a->kind == N_NAME && n->a->sym == v) return 1; break; case N_FN: return 1; // a closure body: not worth following } if ((int)n->kind == X_CALLFN) { Call *c = n->info; for (iz i = 0; i < c->nargs; i++) { if (changes(c->args[i], v)) return 1; } } if (changes(n->a, v) || changes(n->b, v)) return 1; if (n->kind != N_FOR && changes(n->c, v)) return 1; for (iz i = 0; i < n->list.len; i++) { if (changes(n->list.data[i], v)) return 1; } return 0; } static b32 borrowable(Node *decl) { Sym *s = decl->sym; if (!optimizing() || !decl->b || !s->isconst || !s->type->managed || s->type->has_refs) return 0; if (s->pinned || s->iscaptured || s->isglobal || s->co_path.len) return 0; if (!cur_block || cur_stmt >= cur_block->list.len || cur_block->list.data[cur_stmt] != decl) return 0; Sym *v = place_root(decl->b); if (!v || v->isglobal || v->isparam || v->pinned || v->iscaptured || v->isvar || v->owner != genfn || v->co_path.len) return 0; for (iz i = cur_stmt + 1; i < cur_block->list.len; i++) { if (changes(cur_block->list.data[i], v)) return 0; } return 1; } static void gen_body(Node *b) { line("{"); indent++; gen_block_stmts(b, 1); indent--; line("}"); } // An index of an assignment target worked out before the value, as a // temporary the target then reads; the node is put back afterward (defer // bodies are generated more than once). typedef struct { Node **slot; Node *node; } PlaceSave; static int value_first(Node **slot, PlaceSave *saved, int ns); static int values_first(Node *n, PlaceSave *saved, int ns) { if (n->kind == N_FIELD) return values_first(n->a, saved, ns); if (n->kind == N_INDEX) { ns = values_first(n->a, saved, ns); for (iz i = 0; i < n->list.len; i++) ns = value_first(&n->list.data[i], saved, ns); return ns; } if (n->kind == N_DEREF) return value_first(&n->a, saved, ns); return ns; } static int value_first(Node **slot, PlaceSave *saved, int ns) { if (quiet_value(*slot) || ns >= 16) return ns; Node *e = *slot; Sym *v = mksym(S_VAR, S("index"), e->pos); v->type = e->type; v->owner = genfn; v->cname = temp(e->type, gen_value(e)); Node *nm = mknode(N_NAME, e->pos); nm->name = v->name; nm->sym = v; nm->type = e->type; saved[ns].slot = slot; saved[ns].node = e; *slot = nm; return ns + 1; } static void declare_var(Sym *s, Str init) { define_type(s->type); // a local holds the whole value if (s->co_path.len) { // kept in the struct: whatever an edit left there goes first Str place = var_expr(s, s->pos); lines(drop_stmt(s->type, place)); line("%s = %s;", place, init); if (s->type->managed && frame) *push(&perm, &frame->locals) = s; return; } Str ct = ctype(s->type); if (s->pinned) { line("%s *%s = (%s *)%s;", ct, s->cname, ct, cell_alloc(s)); line("*%s = %s;", s->cname, init); if (rc_cell(s) && frame) *push(&perm, &frame->locals) = s; return; } // Star99 allows unused locals; C would warn about them. A use the // checker saw may leave no C behind (type_of(k), an argument a macro // drops), and in an area a local whose release is skipped may be read // nowhere else. line("S99_UNUSED %s %s = %s;", ct, s->cname, init); if (s->type->managed && frame) *push(&perm, &frame->locals) = s; } static Str assign_value(Type *t, Str lv, Str v) { // Lists, maps and pools keep their header, so references follow the variable. switch (t->kind) { case TY_LIST: if (t->elem->managed) { need(t, H_DROP); line("if (%s) for (int64_t i = 0; i < %s->len; i++) %s", lv, lv, drop_stmt(t->elem, fmtstr("((%s *)%s->data)[i]", ctype(t->elem), lv))); } return fmtstr("s99_list_assign(&%s, %s);", lv, v); case TY_POOL: if (t->elem->managed) { line("if (%s) for (int64_t i = 0; i < %s->len; i++) {", lv, lv); indent++; line("s99_slot *s = (s99_slot *)(%s->slots + i * %s->slotsize);", lv, lv); line("if (s->version) %s", drop_stmt(t->elem, fmtstr("*(%s *)S99_SLOTDATA(s)", ctype(t->elem)))); indent--; line("}"); } return fmtstr("s99_pool_assign(&%s, %s);", lv, v); case TY_MAP: if (t->key->managed || t->elem->managed) { line("if (%s) for (int64_t i = 0; i < %s->norder; i++) {", lv, lv); indent++; line("s99_mapent *e = %s->order[i];", lv); line("if (!e->alive) continue;"); lines(drop_stmt(t->key, fmtstr("*(%s *)s99_map_key(e)", ctype(t->key)))); lines(drop_stmt(t->elem, fmtstr("*(%s *)s99_map_val(%s, e)", ctype(t->elem), lv))); indent--; line("}"); } return fmtstr("s99_map_assign(&%s, %s);", lv, v); case TY_STRUCT: if (t->managed) { Str src = temp(t, v); for (iz i = 0; i < t->fields.len; i++) { Field *f = t->fields.data + i; Str fn = cident(f->name); Str dst = fmtstr("%s.f_%s", lv, fn); Str sv = fmtstr("%s.f_%s", src, fn); if (f->type->managed) { Str st = assign_value(f->type, dst, sv); lines(st); } else { line("%s = %s;", dst, sv); } } return (Str){0}; } break; default: if (t->managed) { Str tv = temp(t, v); lines(drop_stmt(t, lv)); return fmtstr("%s = %s;", lv, tv); } } return fmtstr("%s = %s;", lv, v); } static void gen_switch_stmt(Node *n, Str result); static void gen_for(Node *n, i32 label); static Frame * loop_frame(Node *target) { for (Frame *f = frame; f; f = f->parent) { if (f->loop == target) return f; if (f->isfn) break; } return 0; } static void gen_jump(Node *n, b32 iscontinue) { Node *target = n->info; Frame *lf = loop_frame(target); if (!lf) errorat(n->pos, "star99 bug, please report it: no loop frame"); // release everything opened inside the loop body (and the loop's own // frame for a break) leave_frames(lf, !iscontinue); if (iscontinue) { lf->used_cont = 1; line("goto cont%d;", lf->label); } else { lf->used_break = 1; line("goto brk%d;", lf->label); } } // Optimizations can be switched off (STAR99_OPT=0) to check that the // program means the same without them. static b32 optimizing(void) { static int on = -1; if (on < 0) on = !equals(os_getenv(S("STAR99_OPT")), S("0")); return on; } // A local of this function that a return can hand over as it is: nothing // else can reach it once the function ends. static b32 movable_local(Node *a) { if (!optimizing() || a->kind != N_NAME || !a->sym || a->sym->kind != S_VAR) return 0; Sym *s = a->sym; // a switch or loop binding views its subject: returning it copies return !s->isglobal && !s->isparam && !s->pinned && !s->isvar && !s->borrowed && !s->bound_by && s->owner == genfn && s->type->managed; } static void gen_co_return(Node *n); static void gen_return(Node *n) { if (co_gen) { gen_co_return(n); return; } if (n->a && n->a->type == t_void) { // fail(...) ending a function with a result line("%s;", gen_expr(n->a)); Type *rt = genfn && genfn->type ? genfn->type->elem : t_void; if (rt != t_void) line("return %s; /* not reached */", gen_zero(rt)); return; } Str v = {0}; Sym *moved = 0; // a defer still to run could change x after return x: then x is copied b32 defers = 0; for (Frame *f = frame; f && !defers; f = f->parent) { defers = f->defers.len > 0; if (f->isfn) break; } if (n->a && movable_local(n->a) && !defers) { // return x: x itself goes to the caller, not a copy of it moved = n->a->sym; v = temp(n->a->type, var_expr(moved, n->pos)); moved->moved_out = 1; } else if (n->a) { Str m = gen_move(n->a); v = temp(n->a->type, m); } // release the temporaries of every open statement and every open // frame, then return Frame *fnf = frame; while (fnf && !fnf->isfn && fnf->parent) fnf = fnf->parent; leave_frames(fnf, 1); if (sctx) sctx->drops.len = 0; // the return's own: released if (moved) moved->moved_out = 0; if (dbg_framed) line("S99_POP;"); if (v.len) line("return %s;", v); else line(debugging() && genfn == dbg_root ? "S99_MAIN_RETURN;" : "return;"); } static void gen_stmt_in(Node *n) { switch ((int)n->kind) { case N_VAR: { Sym *s = n->sym; if (borrowable(n)) { define_type(s->type); line("S99_UNUSED %s %s = %s;", ctype(s->type), s->cname, gen_expr(n->b)); s->borrowed = 1; return; } if (n->b && s->type->has_refs && !is_fresh(n->b)) { // copied straight into place, so references into it land here Str src = gen_expr(n->b); declare_var(s, gen_zero(s->type)); line("%s", copy_stmt(s->type, var_expr(s, n->pos), src)); return; } Str init = {0}; if (n->b) init = gen_move(n->b); else init = gen_zero(s->type); declare_var(s, init); return; } case N_ASSIGN: { Type *t = n->a->type; // left to right: indexes of the target run before the value PlaceSave saved[16] = {0}; int nsaved = values_first(n->a, saved, 0); if (n->op == T_EQ && t->has_refs) { // references inside the value point into it: a copy moves them // over to the copy (moving parts one by one would not) b32 fresh = is_fresh(n->b); Str src = temp(t, gen_expr(n->b)); Str lv = gen_lvalue(n->a); lines(drop_stmt(t, lv)); line("%s", copy_stmt(t, lv, src)); if (fresh) lines(drop_stmt(t, src)); } else if (n->op == T_EQ) { Str v = gen_move(n->b); Str tv = temp(t, v); Str lv = gen_lvalue(n->a); lines(assign_value(t, lv, tv)); } else { Str v = temp(t, gen_value(n->b)); Str lv = gen_lvalue(n->a); Str p = tmpname(); line("%s *%s = &%s;", ctype(t), p, lv); TokKind op = compound_op(n->op); line("*%s = %s;", p, gen_arith_nodes(op, t, n->a, n->b, fmtstr("*%s", p), v, n->pos)); } while (nsaved-- > 0) *saved[nsaved].slot = saved[nsaved].node; // the tree as it was return; } case N_DISCARD: { Str v = gen_value(n->a); line("(void)%s;", v); return; } case N_EXPR: { Str v = gen_expr(n->a); if (n->a->type->managed && is_fresh(n->a)) { Str t = temp(n->a->type, v); lines(drop_stmt(n->a->type, t)); } else if (!equals(v, S("((void)0)"))) { line("%s;", v); } return; } case N_IF: { i32 p0 = npend; Str c = temp(t_bool, gen_value(n->a)); release_temps(sctx); sctx->drops.len = 0; commit_pending(p0, 0); // the condition ran before either branch line("if (%s) {", c); indent++; i32 known = nfacts; learn(n->a, 1); gen_block_stmts(n->b, 1); nfacts = known; range_epoch++; indent--; if (n->c) { line("} else {"); indent++; learn(n->a, 0); gen_block_stmts(n->c, 1); nfacts = known; range_epoch++; indent--; } line("}"); // past an if whose body never ends normally, its condition is false // (kept to the end of the enclosing block) if (!n->c && diverges(n->b)) learn(n->a, 0); return; } case N_WHILE: { i32 label = ++label_counter; Node *owner = n->info ? (Node *)n->info : n; // a loop runs again after its end: facts on what it changes die first kill_changed(n, 0); i32 known = nfacts, p0 = npend; line("for (;;) {"); indent++; StmtCtx cctx = {0}; open_ctx(&cctx); Str c = temp(t_bool, gen_value(n->a)); close_ctx(&cctx); line("if (!%s) break;", c); commit_pending(p0, 0); // the condition ran before the body push_frame(owner, label); Frame *wf = frame; b32 area = loop_area(n) && !(co_gen && co_yields_in(&co_gen->yloops, n)); if (area) enter_area(); gen_block_stmts(n->b, 1); if (area) pop_frame(); pop_frame(); nfacts = known; range_epoch++; indent--; if (wf->used_cont) line("cont%d:;", label); line("}"); if (wf->used_break) line("brk%d:;", label); return; } case N_FOR: gen_for(n, ++label_counter); return; case N_BLOCK: gen_body(n); return; case N_LABELED: { Node *inner = n->a; if (inner->kind == N_FOR || inner->kind == N_WHILE) { gen_stmt_in(inner); return; } i32 label = ++label_counter; line("{"); indent++; push_frame(n, label); Frame *lfr = frame; if (inner->kind == N_BLOCK) gen_block_stmts(inner, 0); else gen_stmt(inner); // label x = switch v { ... } pop_frame(); indent--; line("}"); if (lfr->used_break || lfr->used_cont) line("brk%d:;", label); return; } case N_RETURN: gen_return(n); return; case N_BREAK: gen_jump(n, 0); return; case N_CONTINUE: gen_jump(n, 1); return; case N_DEFER: *push(&perm, &frame->defers) = n->a; return; case N_MLABEL: // a compile-time block: its statements, in the enclosing C block for (iz i = 0; i < n->a->list.len; i++) gen_stmt(n->a->list.data[i]); return; case N_SWITCH: gen_switch_stmt(n, (Str){0}); return; case N_FN: return; // nested functions are generated on their own case N_IMPORT: return; default: errorat(n->pos, "star99 bug, please report it: cannot generate this statement"); } } // A loop binding for one step: its value, or a copy of the element (so // changing the container inside the loop never invalidates it). A binding a // closure keeps gets a cell of its own each step. static void loop_binding(Sym *s, Str v, b32 copy) { if (s->co_path.len) { // kept by a coroutine: a field Str place = var_expr(s, s->pos); if (copy) { lines(drop_stmt(s->type, place)); line("%s", copy_stmt(s->type, place, v)); } else { line("%s = %s;", place, v); } return; } Str ct = ctype(s->type); if (s->pinned) line("%s *%s = (%s *)%s;", ct, s->cname, ct, cell_alloc(s)); else line("S99_UNUSED %s %s;", ct, s->cname); Str place = var_expr(s, s->pos); if (copy) line("%s", copy_stmt(s->type, place, v)); else line("%s = %s;", place, v); } // Areas. A loop step that keeps nothing it allocates runs in an area: its // allocations bump a pointer and are all given back when the step ends, // with no frees one by one. The step keeps nothing when no value it makes // can reach anything outside it: it stores no managed value, function value // or Ref into a place declared outside the step (directly, through &, or // through a Ref), returns none, calls no function value, and calls only // functions that likewise store nothing into globals or through Refs. typedef struct { b32 fnmode; // checking a function: its own locals and parameters are inside Fn *fn; Syms inside; // loop mode: what the step declares } AreaCtx; // Writing to this place can allocate: a map element (a new key) or the // payload of a union (a new box). static b32 place_allocates(Node *n) { for (; n->kind == N_FIELD || n->kind == N_INDEX; n = n->a) { if (n->kind == N_INDEX && n->a->type && n->a->type->kind == TY_MAP) return 1; if (n->kind == N_FIELD) { FieldAccess *fa = n->info; if (fa && fa->kind != FA_STRUCT) return 1; } } return 0; } static b32 heavy(Type *t) { return t && (t->managed || t->has_refs || t->kind == TY_REF); } static b32 area_inside(AreaCtx *a, Sym *s) { if (!s || s->kind != S_VAR || s->isglobal) return 0; if (a->fnmode) return s->owner == a->fn; // var parameters: the caller checked the place for (iz i = 0; i < a->inside.len; i++) { if (a->inside.data[i] == s) return 1; } return 0; } static void area_decls(Node *n, Syms *out) { if (!n || n->kind == N_FNLIT || n->kind == N_FN) return; if (n->kind == N_VAR && n->sym) *push(&perm, out) = n->sym; if (n->kind == N_FOR) { if (n->sym) *push(&perm, out) = n->sym; if (n->sym2) *push(&perm, out) = n->sym2; } if (n->kind == N_CASE && n->info && ((CaseInfo *)n->info)->binding) *push(&perm, out) = ((CaseInfo *)n->info)->binding; if ((int)n->kind == X_CALLFN) { Call *c = n->info; for (iz i = 0; i < c->nargs; i++) area_decls(c->args[i], out); return; } area_decls(n->a, out); area_decls(n->b, out); if (n->kind != N_FOR) area_decls(n->c, out); for (iz i = 0; i < n->list.len; i++) area_decls(n->list.data[i], out); } static b32 contained_fn(Fn *f); static b32 area_ok(Node *n, AreaCtx *a) { if (!n) return 1; switch ((int)n->kind) { case N_FNLIT: { // its body runs only when called (calls of function values are // refused), but a counted environment holds counts of the cells it // captures, which only its release gives back Fn *f = n->info; return !(f && f->captures.len && f->escapes); } case N_FN: return 1; // runs only when called case N_ASSIGN: if (!area_inside(a, place_sym(n->a)) && (heavy(n->a->type) || heavy(n->b->type) || place_allocates(n->a))) return 0; break; case N_ADDR: // &m[k] may insert the key, &w.x may make the payload box if (!area_inside(a, place_sym(n->a)) && (heavy(n->a->type) || place_allocates(n->a))) return 0; break; case N_RETURN: if (!a->fnmode && n->a && heavy(n->a->type)) return 0; break; case N_SWITCH: if ((n->flags & F_REF) && !area_inside(a, place_sym(n->a))) return 0; break; } if ((int)n->kind == X_CALLFN) { Call *c = n->info; if (c->kind == CALL_VALUE) return 0; if (c->kind == CALL_FN && c->fn && !c->fn->builtin && !contained_fn(c->fn)) return 0; if (c->kind == CALL_DISPATCH) { for (iz v = 0; c->cases && v < c->dispatch_union->fields.len; v++) { if (c->cases[v] && !contained_fn(c->cases[v])) return 0; } } for (iz i = 0; i < c->nargs; i++) { if (!area_ok(c->args[i], a)) return 0; } return 1; } if (!area_ok(n->a, a) || !area_ok(n->b, a)) return 0; if (n->kind != N_FOR && !area_ok(n->c, a)) return 0; for (iz i = 0; i < n->list.len; i++) { if (!area_ok(n->list.data[i], a)) return 0; } return 1; } // Every function at once, as a least fixed point: a function is contained // until its body shows a store outside or a call of one that is not. An // answer worked out while another function was assumed contained could // otherwise stay wrong when that assumption fails. static STATE b32 areas_known = 0; static void compute_areas(void) { for (iz i = 0; i < all_fns.len; i++) { Fn *f = all_fns.data[i]; f->area = !f->builtin && f->checked && f->decl && f->decl->b && !f->isclosure ? 1 : 2; } areas_known = 1; for (b32 changed = 1; changed;) { changed = 0; for (iz i = 0; i < all_fns.len; i++) { Fn *f = all_fns.data[i]; if (f->area != 1) continue; AreaCtx a = {0}; a.fnmode = 1; a.fn = f; if (!area_ok(f->decl->b, &a)) f->area = 2, changed = 1; } } } static b32 contained_fn(Fn *f) { if (f->builtin) return 1; // a resume: the body of its coroutine; c is the caller's place if (f->co && f == f->co->resume) f = f->co->fn; if (!areas_known || !f->area) compute_areas(); // a function checked since: again return f->area == 1; } // Whether calling f can change anything outside its own locals: a global, // a var parameter, something reached through a Ref, a captured variable. // Same least fixed point as compute_areas. static STATE b32 writes_known = 0; static b32 local_place(Node *n, Fn *f) { Sym *v = place_sym(n); return v && v->kind == S_VAR && !v->isglobal && !v->isvar && v->owner == f; } static b32 writes_outside(Node *n, Fn *f) { if (!n || n->kind == N_FNLIT || n->kind == N_FN) return 0; // their own functions if ((n->kind == N_ASSIGN || n->kind == N_ADDR) && !local_place(n->a, f)) return 1; if (n->kind == N_SWITCH && (n->flags & F_REF) && !local_place(n->a, f)) return 1; if ((int)n->kind == X_CALLFN) { Call *c = n->info; if (c->kind == CALL_VALUE) return 1; // an unknown function if (c->kind == CALL_FN && c->fn && !c->fn->builtin && c->fn->writes != 1) return 1; if (c->kind == CALL_DISPATCH) { for (iz v = 0; c->cases && v < c->dispatch_union->fields.len; v++) if (c->cases[v] && !c->cases[v]->builtin && c->cases[v]->writes != 1) return 1; } for (iz i = 0; i < c->nargs; i++) if (writes_outside(c->args[i], f)) return 1; return 0; } if (writes_outside(n->a, f) || writes_outside(n->b, f)) return 1; if (writes_outside(n->c, f)) return 1; for (iz i = 0; i < n->list.len; i++) if (writes_outside(n->list.data[i], f)) return 1; return 0; } static b32 fn_writes(Fn *f) { if (f->builtin) return 0; // builtins change only what & gives them // a resume changes the coroutine, and what its body changes if (f->co && f == f->co->resume) return 1; if (!writes_known || !f->writes) { for (iz i = 0; i < all_fns.len; i++) { Fn *g = all_fns.data[i]; g->writes = !g->builtin && g->checked && g->decl && g->decl->b && !g->isclosure ? 1 : 2; } writes_known = 1; for (b32 changed = 1; changed;) { changed = 0; for (iz i = 0; i < all_fns.len; i++) { Fn *g = all_fns.data[i]; if (g->writes == 1 && writes_outside(g->decl->b, g)) g->writes = 2, changed = 1; } } } return f->writes != 1; } static b32 allocates(Node *n); // Whether calling f can allocate: it builds managed values or calls what // does (assumed while it is being worked out). static b32 fn_allocates(Fn *f) { if (f->builtin) return 1; if (f->co && f == f->co->resume) f = f->co->fn; if (f->alloc) return f->alloc != 2; f->alloc = 3; f->alloc = f->decl && f->decl->b && !allocates(f->decl->b) ? 2 : 1; return f->alloc == 1; } static b32 allocates(Node *n) { if (!n || n->kind == N_FNLIT || n->kind == N_FN) return 0; if (n->kind == N_LIST) return 1; if ((int)n->kind == X_CALLFN) { Call *c = n->info; if (n->type && n->type->managed) return 1; if (c->kind == CALL_FN && c->fn && !c->fn->builtin && fn_allocates(c->fn)) return 1; if (c->kind == CALL_FN && c->fn && c->fn->builtin && (c->builtin == B_ADD || c->builtin == B_INSERT || c->builtin == B_RESIZE || c->builtin == B_STR)) return 1; if (c->kind == CALL_VALUE || c->kind == CALL_DISPATCH) return 1; for (iz i = 0; i < c->nargs; i++) { if (allocates(c->args[i])) return 1; } return 0; } if (n->kind == N_VAR && n->sym && n->sym->type && n->sym->type->managed) return 1; if (allocates(n->a) || allocates(n->b) || (n->kind != N_FOR && allocates(n->c))) return 1; for (iz i = 0; i < n->list.len; i++) { if (allocates(n->list.data[i])) return 1; } return 0; } // Whether the body of loop n runs in an area each step. static b32 loop_area(Node *n) { if (!optimizing() || !n->b || !allocates(n->b)) return 0; AreaCtx a = {0}; area_decls(n->b, &a.inside); return area_ok(n->b, &a); } // Opens the area of a loop step: what the step allocates is given back when // the frame pushed here exits, however the step ends. static void enter_area(void) { Str m = fmtstr("area%d", ++tmpcount); line("s99_area_mark %s;", m); line("s99_area_enter(&%s);", m); push_frame(0, 0); frame->isarea = 1; *push(&perm, &frame->exits) = fmtstr("s99_area_leave(&%s);", m); } static CoLoop *co_loop(Node *n); static void gen_co_for(Node *n, i32 label, CoLoop *cl); // The body of a for loop whose head is written, and the end of the loop: // each step in an area of its own when area says so, and last_step after // the label continue goes to. static void for_body(Node *n, i32 label, b32 area, Str last_step) { Sym *b = n->sym, *b2 = n->sym2; push_frame(n->info ? (Node *)n->info : n, label); if ((b->type->managed && n->a->kind != N_RANGE) || rc_cell(b)) *push(&perm, &frame->locals) = b; if (b2 && (b2->type->managed || rc_cell(b2))) *push(&perm, &frame->locals) = b2; Frame *loopframe = frame; if (area) enter_area(); push_frame(0, 0); gen_block_stmts(n->b, 0); pop_frame(); if (area) pop_frame(); indent--; if (loopframe->used_cont) line("cont%d:;", label); indent++; exit_frame(loopframe); if (last_step.len) line("%s", last_step); // a..=b: no i++ past b, so b may be the Int maximum frame = loopframe->parent; indent--; line("}"); if (loopframe->used_break) line("brk%d:;", label); } static void gen_for(Node *n, i32 label) { CoLoop *cl = co_loop(n); if (cl) { gen_co_for(n, label, cl); return; } Node *src = n->a; Sym *b = n->sym; Sym *b2 = n->sym2; // a loop runs again after its end: facts on what it changes die first kill_changed(n, 0); i32 known = nfacts; Str owned_source = {0}; // the source is evaluated before the loop block, so its temporaries are // released after the whole loop Str lo = {0}, hi = {0}, c = {0}, last_step = {0}; if (src->kind == N_RANGE) { lo = temp(t_int, gen_value(src->a)); hi = temp(t_int, gen_value(src->b)); } else { Type *t = src->type; Node *root = root_of(src); // a place when it is a variable or a reference b32 isplace = !is_fresh(src) && src->kind != N_STR && (root->kind == N_NAME || root->kind == N_DEREF); if (isplace && (t->kind == TY_LIST || t->kind == TY_MAP || t->kind == TY_POOL)) { c = tmpname(); line("%s *%s = &%s;", ctype(t), c, gen_lvalue(src)); c = fmtstr("(*%s)", c); } else { Str v = gen_expr(src); c = temp(t, v); if (is_fresh(src) && t->managed) owned_source = c; } } line("{"); indent++; // a source the loop owns is released however the loop ends (a return // from inside it too) Frame *srcframe = 0; if (owned_source.len) { push_frame(0, 0); srcframe = frame; *push(&perm, &srcframe->exits) = drop_stmt(src->type, owned_source); } // a map keeps its order while loops visit it; however the loop ends, // leaving it lets the map compact again Frame *mapframe = 0; if (src->kind != N_RANGE && src->type->kind == TY_MAP) { line("s99_map_loop_push(%s);", c); push_frame(0, 0); mapframe = frame; *push(&perm, &mapframe->exits) = S("s99_map_loop_pop();"); } if (src->kind == N_RANGE) { Str i = tmpname(); if (range_open_low(src->op)) { // Increment only after proving i < hi, so even MAX<..=MAX is empty. line("for (int64_t %s = %s; %s < %s;) {", i, lo, i, hi); indent++; line("%s++;", i); if (range_open_high(src->op)) line("if (%s == %s) break;", i, hi); } else { line("for (int64_t %s = %s; %s %s %s; %s++) {", i, lo, i, range_open_high(src->op) ? S("<") : S("<="), hi, i); indent++; if (!range_open_high(src->op)) last_step = fmtstr("if (%s == %s) break;", i, hi); } loop_binding(b, i, 0); } else { Type *t = src->type; Str i = tmpname(); switch (t->kind) { case TY_LIST: case TY_ARRAY: { Str count = t->kind == TY_LIST ? fmtstr("s99_list_len(%s)", c) : fmtstr("%lld", (long long)t->count); // the length is taken once: adding inside the loop does not // extend it, and removing makes later steps stop with an error Str n = tmpname(); line("int64_t %s = %s;", n, count); line("for (int64_t %s = 0; %s < %s; %s++) {", i, i, n, i); indent++; // the body may have removed or cut elements: the step stops with an index error Sym *root = place_sym(src); b32 fixed = root && root->kind == S_VAR && !root->isglobal && (root->isconst || (root->isparam && !root->isvar)); // nothing can shrink it if (t->kind == TY_LIST && !fixed) line( "if (S99_UNLIKELY(%s >= s99_list_len(%s))) s99_fail_index(%s, %s, s99_list_len(%s));", i, c, loc(src->pos), i, c); Sym *val = b2 ? b2 : b; if (b2) loop_binding(b, i, 0); Type *et = val->type; Str elem = t->kind == TY_LIST ? fmtstr("((%s *)%s->data)[%s]", ctype(et), c, i) : t->kind == TY_ARRAY ? fmtstr("%s.a[%s]", c, i) : fmtstr("%s.data[%s]", c, i); loop_binding(val, elem, 1); break; } case TY_MAP: { // the entries there when the loop starts, as for a list Str n = tmpname(); line("int64_t %s = %s ? %s->norder : 0;", n, c, c); line("for (int64_t %s = 0; %s < %s; %s++) {", i, i, n, i); indent++; line("s99_mapent *e%s = %s->order[%s];", i, c, i); line("if (!e%s->alive) continue;", i); loop_binding(b, fmtstr("*(%s *)s99_map_key(e%s)", ctype(b->type), i), 1); if (b2) loop_binding(b2, fmtstr("*(%s *)s99_map_val(%s, e%s)", ctype(b2->type), c, i), 1); break; } case TY_POOL: { Str n = tmpname(); line("int64_t %s = %s ? %s->len : 0;", n, c, c); line("for (int64_t %s = 0; %s < %s; %s++) {", i, i, n, i); indent++; line("s99_slot *s%s = (s99_slot *)(%s->slots + %s * %s->slotsize);", i, c, i, c); line("if (!s%s->version) continue;", i); loop_binding(b, fmtstr("(s99_ref){%s, s%s->version, %s, S99_POOL, 0}", c, i, i), 0); break; } default: errorat(n->pos, "star99 bug, please report it: for over a %s", type_name(n->a->type)); } } for_body(n, label, loop_area(n), last_step); if (mapframe) { exit_frame(mapframe); frame = mapframe->parent; } nfacts = known; range_epoch++; if (srcframe) { exit_frame(srcframe); frame = srcframe->parent; } indent--; line("}"); } // A switch binding is a read-only value. It may share the storage of the // subject when nothing can change the subject in the case: a value of its // own (a call result), or a local or parameter of this function that the // case does not change; otherwise it is a copy, released with the case. static b32 binding_borrows(Node *sw, Node *body) { if (!optimizing()) return 0; Node *subj = sw->a; if (subj->kind != N_NAME && subj->kind != N_FIELD && subj->kind != N_INDEX && subj->kind != N_DEREF) return 1; Sym *v = place_root(subj); if (!v || v->isglobal || v->pinned || v->iscaptured || v->isvar || v->owner != genfn) return 0; if (v->co_path.len) return 0; // in a coroutine's struct return v->isparam || !changes(body, v); } static void bind_case_value(Sym *bind, Type *t, Str src, b32 borrow) { if (bind->co_path.len) { // kept by a coroutine: a copy in its field Str place = var_expr(bind, bind->pos); lines(drop_stmt(t, place)); line("%s", copy_stmt(t, place, src)); if (t->managed) *push(&perm, &frame->locals) = bind; return; } if (borrow || !t->managed) { line("%s %s = %s;", ctype(t), bind->cname, src); bind->borrowed = t->managed; // never moved out: the subject owns it return; } line("%s %s;", ctype(t), bind->cname); line("%s", copy_stmt(t, bind->cname, src)); *push(&perm, &frame->locals) = bind; } // switch, as a statement or a value (result is the C variable to assign). static void co_flush_drops(void); static void co_block_ends(Node *b); static STATE b32 switch_ends = 0; // the switch ends a body in a resume: its cases end in its value static void gen_switch_stmt(Node *n, Str result) { int kind = (int)n->ival; Type *t = n->a->type; b32 isref = (n->flags & F_REF) != 0; b32 ends = switch_ends; switch_ends = 0; // a case with a yield inside: a resume enters it in the middle, so the // subject is released as each case starts, and bindings are copies b32 cy = co_gen && co_yields_in(&co_gen->yswitches, n); Str subject = {0}, owned = {0}; if (isref) { subject = fmtstr("&%s", gen_lvalue(n->a)); } else if (cy && is_fresh(n->a) && t->managed) { owned = temp(t, gen_expr(n->a)); subject = fmtstr("&%s", owned); } else { subject = gen_addr(n->a); } Str sv = tmpname(); line("%s *%s = %s;", ctype(t), sv, subject); if (cy) co_flush_drops(); i32 label = ++label_counter; push_frame(n, label); // break s, for switch s in v Frame *swf = frame; b32 complete = (n->flags & F_COMPLETE) != 0; if (kind == SW_UNION) line("switch (%s) {", utag(t, sv)); else line("{"); for (iz ci = 0; ci < n->list.len; ci++) { Node *cs = n->list.data[ci]; CaseInfo *info = cs->info; if (kind == SW_UNION) { if (info->isdefault) line("default: {"); else { Buf b = {0}; for (iz k = 0; k < info->variants.len; k++) bputf(&b, "case %d: ", union_tag(t, info->variants.data[k])); if (info->isnil) bput(&b, S("case 0: ")); line("%s{", bstr(&b)); } } else if (kind == SW_ANY) { Buf b = {0}; if (info->isdefault) { bput(&b, ci ? S("else {") : S("if (1) {")); } else { bput(&b, ci ? S("else if (") : S("if (")); for (iz k = 0; k < info->ntypes; k++) { need(info->types[k], H_INFO); if (k) bput(&b, S(" || ")); bputf(&b, "%s->type == &info%d", sv, info->types[k]->id); } if (info->isnil) bputf(&b, "%s!%s->type", info->ntypes ? S(" || ") : S(""), sv); bput(&b, S(") {")); } line("%s", bstr(&b)); } else { Buf b = {0}; if (info->isdefault) { bput(&b, ci ? S("else {") : S("if (1) {")); } else { bput(&b, ci ? S("else if (") : S("if (")); for (iz k = 0; k < cs->list.len; k++) { if (k) bput(&b, S(" || ")); Node *pat = cs->list.data[k]; if (t == t_string && pat->kind == N_STR) { bputf(&b, "s99_str_is(%s, %s, %lld)", sv, c_string(pat->sval), (long long)pat->sval.len); continue; } Str pv = gen_value(pat); if (t == t_string) bputf(&b, "s99_str_eq(%s, &(%s))", sv, pv); else bputf(&b, "*%s == %s", sv, pv); } bput(&b, S(") {")); } line("%s", bstr(&b)); } indent++; push_frame(0, 0); Sym *bind = info->binding; if (bind) { if (info->payload && kind == SW_UNION) { iz v = info->variants.data[0]; Type *pt = t->fields.data[v].type; if (isref) { line("s99_ref %s = s99_ref_box(%s);", bind->cname, payload_box(t, v, sv, cs->pos)); } else { need(pt, H_ZERO); bind_case_value(bind, pt, fmtstr("*(%s *)(%s->box ? S99_BOXDATA(%s->box) : &z%d)", ctype(pt), sv, sv, pt->id), !cy && binding_borrows(n, cs->a)); } } else if (kind == SW_ANY && info->ntypes == 1 && isref) { // switch &a { case Int: a += 1 }: the value inside the Any line("s99_ref %s = s99_ref_box(%s->box);", bind->cname, sv); } else if (kind == SW_ANY && info->ntypes == 1) { Type *bt = info->types[0]; bind_case_value(bind, bt, fmtstr("*(%s *)S99_BOXDATA(%s->box)", ctype(bt), sv), !cy && binding_borrows(n, cs->a)); } else if (isref) { line("s99_ref %s = s99_ref_cell(%s);", bind->cname, sv); } else { bind_case_value(bind, t, fmtstr("*%s", sv), !cy && binding_borrows(n, cs->a)); } if (!bind->co_path.len) line("(void)%s;", bind->cname); } if (owned.len) lines(drop_stmt(t, owned)); // bound: the subject goes Node *b = cs->a; if (ends) { co_block_ends(b); } else if (result.len) { for (iz i = 0; i + 1 < b->list.len; i++) gen_stmt(b->list.data[i]); Node *last = b->list.len ? b->list.data[b->list.len - 1] : 0; if (last && last->kind == N_EXPR && last->a->type != t_void) { StmtCtx ctx = {0}; open_ctx(&ctx); Str v = gen_move(last->a); line("%s = %s;", result, v); close_ctx(&ctx); } else if (last) { // or a case that returns: no value gen_stmt(last); } } else { gen_block_stmts(b, 0); } pop_frame(); if (kind == SW_UNION) line("break;"); indent--; line("}"); } if (kind == SW_UNION && complete) line("default: s99_fail(%s, \"switch reached an unknown variant\");", loc(n->pos)); line("}"); pop_frame(); if (swf->used_break) line("brk%d:;", label); } static Str gen_switch_value(Node *n) { Str r = temp_decl(n->type); gen_switch_stmt(n->a, r); return r; } static void gen_default_into(Buf *b, Node *def, Str dst) { Buf *save = body; int si = indent; Buf tmp = {0}; body = &tmp; indent = 1; StmtCtx ctx = {0}; open_ctx(&ctx); Str v = gen_move(def); line("%s = %s;", dst, v); close_ctx(&ctx); body = save; indent = si; bput(b, bstr(&tmp)); } // Coroutines: the resume of a coroutine is its body as a state machine. It // starts with a switch on c.at that jumps to the yield point it stopped at; // what lives across a yield is in the struct (see co_analyze), so the jump // finds it there. A yield gives its value as .yielded and returns; a return // keeps the result in c.returned, sets c.at to -1 and gives .returned. // Nested functions that yield are inlined at each call. static STATE i32 co_point = 0; // yield points written so far static STATE Pos *co_yield_at = 0; // debug builds: the statement of each yield point typedef struct { Str result; // where its return puts the value i32 label; // its end Frame *frame; b32 used; } CoInline; static STATE CoInline co_inl[64] = {0}; static STATE int co_ninl = 0; static b32 co_yields_in(Nodes *list, Node *n) { for (iz i = 0; i < list->len; i++) if (list->data[i] == n) return 1; return 0; } static CoLoop * co_loop(Node *n) { for (iz i = 0; co_gen && i < co_gen->fors.len; i++) if (co_gen->fors.data[i].loop == n) return co_gen->fors.data + i; return 0; } // The temporaries of the statement, released now: a resume goes on after // this point, where they do not exist static void co_flush_drops(void) { if (!sctx) return; release_temps(sctx); sctx->drops.len = 0; } static Str co_base(Coro *co, Pos p) { return var_expr(co->param, p); } // return .returned, with a copy of the result the struct keeps static void co_give_returned(Coro *co, Pos p) { Type *u = co->result, *rt = co->fn->type->elem; define_type(u); if (rt == t_void) { if (dbg_framed) line("S99_POP;"); line("return (%s){1, 0};", ctype(u)); return; } Str r = temp_decl(u); Str rc = ctype(rt); line("%s.tag = 1;", r); line("%s.box = s99_box_filled(sizeof(%s));", r, rc); line("%s", copy_stmt(rt, fmtstr("*(%s *)S99_BOXDATA(%s.box)", rc, r), fmtstr("%s.f_returned", co_base(co, p)))); if (dbg_framed) line("S99_POP;"); line("return %s;", r); } // The coroutine returns value (0: nothing): its frames close, the struct // keeps the result, and it is finished static void co_finish(Node *value, Pos p) { Coro *co = co_gen; Type *rt = co->fn->type->elem; Str v = {0}; if (value) v = temp(rt, gen_move(value)); // the temporaries of every open statement and every open frame Frame *fnf = frame; while (fnf && !fnf->isfn && fnf->parent) fnf = fnf->parent; leave_frames(fnf, 1); if (sctx) sctx->drops.len = 0; // the return's own: released Str base = co_base(co, p); if (v.len) { Str place = fmtstr("%s.f_returned", base); lines(drop_stmt(rt, place)); line("%s = %s;", place, v); } line("%s.f_at = -1;", base); co_give_returned(co, p); } // return value from where it is: the coroutine, or a function inlined in it static void co_return_value(Node *value, Pos p) { if (!co_ninl) { co_finish(value, p); return; } CoInline *ic = &co_inl[co_ninl - 1]; if (value) { Str v = gen_move(value); if (ic->result.len) line("%s = %s;", ic->result, v); } // the temporaries of the statements open in the inlined function, and // its frames leave_frames(ic->frame, 1); if (sctx) sctx->drops.len = 0; // the return's own: released line("goto inl%d;", ic->label); ic->used = 1; } static void co_value_ends(Node *e); static void gen_co_return(Node *n) { if (n->a && n->a->type == t_void) { // fail(...) ending a function with a result line("%s;", gen_expr(n->a)); return; } if (n->a && (n->a->kind == N_IFX || n->a->kind == N_SWITCHX || n->a->kind == N_BLOCKX)) { co_value_ends(n->a); // return (if ...): each branch returns its value return; } co_return_value(n->a, n->pos); } // The value that ends a body is returned: an if, switch or block there runs // as a statement whose branches end in it (a yield may be inside them) static void co_block_ends(Node *b) { i32 known = nfacts; Node *saveb = cur_block; iz saves = cur_stmt; cur_block = b; for (iz i = 0; i < b->list.len; i++) { cur_stmt = i; Node *st = b->list.data[i]; if (i + 1 < b->list.len || st->kind != N_EXPR || st->a->type == t_void) { gen_stmt(st); continue; } StmtCtx ctx = {0}; Pos saved_at = gen_at, saved_dbg = dbg_at; gen_at = dbg_at = st->pos; if (dbg_framed) line("S99_AT(%s);", loc(stmt_start(st->pos))); open_ctx(&ctx); co_value_ends(st->a); close_ctx(&ctx); gen_at = saved_at; dbg_at = saved_dbg; } cur_block = saveb; cur_stmt = saves; nfacts = known; range_epoch++; } static void co_branch_ends(Node *x) { if (x->kind != N_BLOCKX) { co_value_ends(x); return; } push_frame(0, 0); co_block_ends(x->a); pop_frame(); } static void co_value_ends(Node *e) { switch ((int)e->kind) { case N_IFX: { i32 p0 = npend; Str c = temp(t_bool, gen_value(e->a)); co_flush_drops(); commit_pending(p0, 0); line("if (%s) {", c); indent++; i32 known = nfacts; learn(e->a, 1); co_branch_ends(e->b); nfacts = known; indent--; line("} else {"); indent++; learn(e->a, 0); co_branch_ends(e->c); nfacts = known; range_epoch++; indent--; line("}"); return; } case N_BLOCKX: line("{"); indent++; co_branch_ends(e); indent--; line("}"); return; case N_SWITCHX: switch_ends = 1; gen_switch_stmt(e->a, (Str){0}); return; default: co_return_value(e, e->pos); return; } } // yield(v): the value goes out as .yielded; the next resume comes back here static Str gen_yield(Node *n, Call *c) { Coro *co = co_gen; if (!co) errorat(n->pos, "star99 bug, please report it: a yield outside its coroutine"); Type *y = co->yields, *u = co->result; Str v = temp(y, gen_move(c->args[0])); co_flush_drops(); Str r = union_value(u, 0, v); // .yielded i32 k = ++co_point; line("%s.f_at = %d;", co_base(co, n->pos), k); // a debug build: the frame goes with the return, and the next resume // pushes it again and comes back to this statement if (dbg_framed) line("S99_POP;"); line("return %s;", r); line("yp%d:;", k); if (co_yield_at && k <= co->points) co_yield_at[k] = dbg_at; if (dbg_framed) line("S99_AT(%s);", loc(stmt_start(dbg_at))); range_epoch++; return S("((void)0)"); } // A call of a nested function that yields: its body, here. Its parameters // are kept in the struct like its locals, each with a value of its own. static Str gen_inline_call(Node *n, Call *c) { Fn *g = c->fn; if (co_ninl >= (int)countof(co_inl)) errorat(n->pos, "nested functions that yield call each other too deeply here"); Type *rt = g->type->elem; Fields *ps = &g->type->fields; for (iz i = 0; i < ps->len; i++) { Node *a = i < c->nargs && c->args[i] ? c->args[i] : ps->data[i].def; Sym *p = g->params.data[i]; Str place = var_expr(p, a->pos); if (p->type->has_refs && !is_fresh(a)) { // copied straight into place, so references into it land there Str src = gen_expr(a); lines(drop_stmt(p->type, place)); line("%s", copy_stmt(p->type, place, src)); continue; } Str v = temp(p->type, gen_move(a)); lines(drop_stmt(p->type, place)); line("%s = %s;", place, v); } co_flush_drops(); Str r = rt == t_void ? (Str){0} : temp_decl(rt); i32 label = ++label_counter; line("{"); indent++; push_frame(0, 0); frame->isfn = 1; // its returns end here; break and continue stay inside for (iz i = 0; i < g->params.len; i++) { Sym *p = g->params.data[i]; if (p->type->managed) *push(&perm, &frame->locals) = p; } CoInline *ic = &co_inl[co_ninl++]; *ic = (CoInline){r, label, frame, 0}; i32 known = nfacts; Node *bd = g->decl->b; if (rt == t_void) { gen_block_stmts(bd, 0); } else { co_block_ends(bd); Node *last = bd->list.len ? bd->list.data[bd->list.len - 1] : 0; if (!last || (last->kind != N_EXPR && !diverges(last))) line("s99_fail(%s, \"%s ended without returning a value\");", loc(g->decl->pos), g->name); } nfacts = known; range_epoch++; b32 used = ic->used; co_ninl--; pop_frame(); indent--; line("}"); if (used) line("inl%d:;", label); return r.len ? r : S("((void)0)"); } // A for loop with a yield inside: what gen_for keeps in C locals is in the // struct, and the binding of a range is its counter. The container is read // through its variable at each use. static void gen_co_for(Node *n, i32 label, CoLoop *cl) { Node *src = n->a; Sym *b = n->sym, *b2 = n->sym2; kill_changed(n, 0); i32 known = nfacts; Str base = co_base(co_gen, n->pos); Str hid[4] = {0}; for (int w = 0; w < 4; w++) hid[w] = cl->paths[w].len ? fmtstr("(%s)%s", base, cl->paths[w]) : (Str){0}; Str c = {0}, last_step = {0}; Frame *srcframe = 0; if (src->kind == N_RANGE) { Str lo = temp(t_int, gen_value(src->a)); Str hi = hid[CL_END]; if (hi.len) line("%s = %s;", hi, gen_value(src->b)); else hi = gen_value(src->b); // a literal co_flush_drops(); if (!b->co_path.len) errorat(n->pos, "star99 bug, please report it: a loop counter not kept"); Str i = var_expr(b, n->pos); line("{"); indent++; line("for (%s = %s; %s %s %s; %s++) {", i, lo, i, src->op == T_DOTLT ? S("<") : S("<="), hi, i); indent++; if (src->op == T_DOTEQ) last_step = fmtstr("if (%s == %s) break;", i, hi); } else { Type *t = src->type; if (hid[CL_OVER].len) { Str v = temp(t, gen_move(src)); lines(drop_stmt(t, hid[CL_OVER])); line("%s = %s;", hid[CL_OVER], v); c = hid[CL_OVER]; } else { c = gen_lvalue(src); // a variable or a field of one } co_flush_drops(); Str idx = hid[CL_INDEX].len ? hid[CL_INDEX] : var_expr(b, n->pos); Str count = t->kind == TY_ARRAY ? fmtstr("%lld", (long long)t->count) : hid[CL_COUNT]; if (t->kind == TY_LIST) line("%s = s99_list_len(%s);", count, c); if (t->kind == TY_POOL) line("%s = %s ? %s->len : 0;", count, c, c); line("{"); indent++; if (hid[CL_OVER].len && t->managed) { // the loop owns it: released however the loop ends push_frame(0, 0); srcframe = frame; *push(&perm, &srcframe->exits) = drop_stmt(t, hid[CL_OVER]); } line("for (%s = 0; %s < %s; %s++) {", idx, idx, count, idx); indent++; if (t->kind == TY_POOL) { Str sl = tmpname(); // the pool may have changed between resumes line("if (%s >= (%s ? %s->len : 0)) break;", idx, c, c); line("s99_slot *%s = (s99_slot *)(%s->slots + %s * %s->slotsize);", sl, c, idx, c); line("if (!%s->version) continue;", sl); loop_binding(b, fmtstr("(s99_ref){%s, %s->version, %s, S99_POOL, 0}", c, sl, idx), 0); } else { if (t->kind == TY_LIST) line("if (S99_UNLIKELY(%s >= s99_list_len(%s))) s99_fail_index(%s, %s, " "s99_list_len(%s));", idx, c, loc(src->pos), idx, c); Sym *val = b2 ? b2 : b; Str elem = t->kind == TY_LIST ? fmtstr("((%s *)%s->data)[%s]", ctype(val->type), c, idx) : fmtstr("%s.a[%s]", c, idx); loop_binding(val, elem, 1); } } for_body(n, label, 0, last_step); if (srcframe) { exit_frame(srcframe); frame = srcframe->parent; } nfacts = known; range_epoch++; indent--; line("}"); } static Str fn_params_c(Fn *f); // resume(c: var Coroutine(NAME)): the body of NAME, its locals reached as // the body's own (genfn is NAME), what it keeps in the struct through c static void gen_resume(Fn *rf) { Coro *co = rf->co; Fn *f = co->fn; nfacts = 0; range_epoch++; npend = 0; compute_ranges(f->decl->b); Type *rt = rf->type->elem; define_type(rt); define_type(co->type); Str sig = fmtstr("static S99_UNUSED %s %s(%s)", ctype(rt), rf->cname, fn_params_c(rf)); bputf(&out_protos, "%s;\n", sig); Buf fb = {0}, hb = {0}; Buf *save = body; Fn *savefn = genfn; Frame *saveframe = frame; Pos save_at = gen_at; f->var_ptr = rf->var_ptr; // c is the coroutine's own parameter in its body body = &fb; genfn = f; frame = 0; co_gen = co; co_route_body = rf->var_ptr ? 0 : &fb; co_point = 0; co_ninl = 0; Node *bd = f->decl->b; gen_at = bd->list.len ? bd->list.data[0]->pos : f->decl->pos; // a debug build: mapped as gen_fn maps a function, and a frame of the // shadow stack for each resume, popped where it returns (at a yield too) Pos save_dbg = dbg_at, end = block_end(bd, f->decl->pos); b32 save_framed = dbg_framed; dbg_framed = debugging(); dbg_at = gen_at; co_yield_at = dbg_framed ? new(&perm, co->points + 1, Pos) : 0; indent = 1; push_frame(0, 0); frame->isfn = 1; if (f->type->elem == t_void) { gen_block_stmts(bd, 0); dbg_at = end; co_finish(0, f->decl->pos); // the end of the body: finished } else { co_block_ends(bd); Node *last = bd->list.len ? bd->list.data[bd->list.len - 1] : 0; if (!last || (last->kind != N_EXPR && !diverges(last))) line("s99_fail(%s, \"%s ended without returning a value\");", loc(f->decl->pos), f->name); if (!last || last->kind != N_RETURN) // for tcc line("return %s; /* not reached */", gen_zero(rt)); } if (co_point != co->points) errorat(f->decl->pos, "star99 bug, please report it: %d yield points, %d written", co->points, co_point); // the start: where the last resume stopped. In a debug build the header // is at the declaration and the rest at the first statement, where a // debugger then stops on every resume; each jump is at its yield. body = &hb; indent = 0; Str dir = debugging() ? line_directive(f->decl->pos) : (Str){0}; if (dir.len) bputf(&hb, "%s\nstatic %s\n%s\n%s(%s)\n%s\n{\n", dir, ctype(rt), dir, rf->cname, fn_params_c(rf), dir); else bputf(&hb, "static %s\n%s(%s)\n{\n", ctype(rt), rf->cname, fn_params_c(rf)); indent = 1; dbg_at = gen_at; if (dbg_framed) line("S99_FRAME(%s, %s);", c_string(fn_signature_text(f, 0)), loc(stmt_start(gen_at))); if (rf->var_ptr && co->param->iscaptured) // what nested functions reach c by line("s99_ref %s_ref = s99_ref_cell(%s);", co->param->cname, co->param->cname); if (!rf->var_ptr) // the struct, for statements that cannot move it line("%s *%s_p = (%s *)s99_deref(%s, %s);", ctype(co->type), co->param->cname, ctype(co->type), co->param->cname, loc(f->decl->pos)); Str base = co_base(co, f->decl->pos); line("switch (%s.f_at) {", base); line("case 0: break;"); for (i32 k = 1; k <= co_point; k++) { if (co_yield_at) dbg_at = co_yield_at[k]; line("case %d: goto yp%d;", k, k); } dbg_at = f->decl->pos; line("case -1: {"); indent++; co_give_returned(co, f->decl->pos); indent--; line("}"); line("default: {"); indent++; Str msg = fmtstr("%s is at %%lld, which is none of its yield points", type_name(co->type)); line("char m[%lld];", (long long)msg.len + 32); line("snprintf(m, sizeof m, %s, (long long)%s.f_at);", c_string(msg), base); line("s99_fail(%s, m);", loc(f->decl->pos)); indent--; line("}"); line("}"); bput(&hb, bstr(&fb)); dir = debugging() ? line_directive(end) : (Str){0}; if (dir.len) bputf(&hb, "%s\n", dir); bput(&hb, S("}\n")); bput(&out_fns, bstr(&hb)); co_gen = 0; co_route_body = 0; co_yield_at = 0; dbg_at = save_dbg; dbg_framed = save_framed; body = save; gen_at = save_at; genfn = savefn; frame = saveframe; } // Functions static Str fn_params_c(Fn *f) { Buf b = {0}; b32 first = 1; if (f->isclosure) { bput(&b, S("void *envp")); first = 0; } Fields *ps = &f->type->fields; for (iz i = 0; i < ps->len; i++) { if (!first) bput(&b, S(", ")); first = 0; Field *p = ps->data + i; Sym *sym = i < f->params.len ? f->params.data[i] : 0; if (p->isvar && f->var_ptr) bputf(&b, "%s *", ctype(p->type)); else bput(&b, p->isvar ? S("s99_ref") : p->type == t_void ? S("int") : ctype(p->type)); bputc(&b, ' '); if (sym && sym->pinned && !p->isvar) bputf(&b, "%s_in", sym->cname); else if (sym) bput(&b, sym->cname); else bputf(&b, "a%lld", (long long)i); } if (first) bput(&b, S("void")); return bstr(&b); } static void gen_fn(Fn *f) { if (f->emitted || f->builtin) return; f->emitted = 1; if (f->co) { // a coroutine runs as its resume; it is never called itself if (f == f->co->resume) gen_resume(f); return; } if (f->yields) return; // inlined at each call if (f->c_code) { gen_c_function(f); return; } // nested in a coroutine still being checked (a metaprogram made meanwhile): // nothing can call it yet, and what yields there is not known for (Fn *p = f->parent; p; p = p->parent) if (p->co && p == p->co->fn && !p->co->analyzed) return; nfacts = 0; range_epoch++; npend = 0; compute_ranges(f->decl->b); Type *rt = f->type->elem; // the definition takes its parameters by value, so their types are needed whole define_type(rt); for (iz i = 0; i < f->type->fields.len; i++) { if (!f->type->fields.data[i].isvar) define_type(f->type->fields.data[i].type); } Str sig = fmtstr("static S99_UNUSED %s %s(%s)", ctype(rt), f->cname, fn_params_c(f)); b32 root = debugging() && f == dbg_root; // it may become the C main: see debug_main bputf(&out_protos, "%s%s;\n", root ? S("#s99 main-proto\n") : S(""), sig); if (f->isclosure) { Buf e = {0}; bputf(&e, "%s {\n", env_type(f)); for (iz i = 0; i < f->captures.len; i++) { Sym *s = f->captures.data[i]; bputf(&e, " %s *%s;\n", s->isvar ? S("s99_ref") : ctype(s->type), cap_member(f, i)); } if (!f->captures.len) bput(&e, S(" char unused;\n")); bput(&e, S("};\n")); bput(&out_types, bstr(&e)); if (f->escapes) { // the last count of the environment releases the cells it reaches bputf(&out_protos, "static S99_UNUSED void envdrop_%s(void *p);\n", f->cname); Buf d = {0}; bputf( &d, "static S99_UNUSED void\nenvdrop_%s(void *p)\n{\n %s *e = (%s *)p;\n (void)e;\n", f->cname, env_type(f), env_type(f)); for (iz i = 0; i < f->captures.len; i++) { if (rc_cell(f->captures.data[i])) bputf(&d, " s99_rc_release(e->%s);\n", cap_member(f, i)); } bput(&d, S("}\n")); bput(&out_fns, bstr(&d)); } } Buf fb = {0}; Buf *save = body; Fn *savefn = genfn; Frame *saveframe = frame; Pos save_at = gen_at; body = &fb; genfn = f; frame = 0; // allocates(f): code outside any statement (a do body, the parameters) // counts at the body gen_at = f->decl->b && f->decl->b->list.len ? f->decl->b->list.data[0]->pos : f->decl->pos; // the header and the brace at the declaration, the parameters at the // first statement: the prologue then has the declaration's line alone, // which debuggers skip when they step in or break at the function (gcc // has its stack protector at the brace: debug builds turn it off) Pos save_dbg = dbg_at; dbg_at = gen_at; Str dir = debugging() ? line_directive(f->decl->pos) : (Str){0}; indent = 0; if (dir.len) bputf(&fb, "%s%s\nstatic %s\n%s\n%s(%s)\n%s%s\n{\n", root ? S("#s99 main-header\n") : S(""), dir, ctype(rt), dir, f->cname, fn_params_c(f), root ? S("#s99 main-body\n") : S(""), dir); else bputf(&fb, "static %s\n%s(%s)\n{\n", ctype(rt), f->cname, fn_params_c(f)); indent = 1; if (root) line("S99_MAIN_START;"); b32 save_framed = dbg_framed; dbg_framed = debugging(); // the shadow stack of crash reports if (dbg_framed) line("S99_FRAME(%s, %s);", c_string(fn_signature_text(f, 0)), loc(stmt_start(gen_at))); if (f->isclosure) { if (f->captures.len) line("%s *env = (%s *)envp;", env_type(f), env_type(f)); else line("(void)envp;"); } push_frame(0, 0); frame->isfn = 1; for (iz i = 0; i < f->params.len; i++) { Sym *p = f->params.data[i]; line("(void)%s%s;", p->cname, p->pinned && !p->isvar ? S("_in") : S("")); if (p->pinned && !p->isvar) { // a stored reference or a closure outlives the call: own a copy Str ct = ctype(p->type); line("%s *%s = (%s *)%s;", ct, p->cname, ct, cell_alloc(p)); line("%s", copy_stmt(p->type, fmtstr("*%s", p->cname), fmtstr("%s_in", p->cname))); if (rc_cell(p)) *push(&perm, &frame->locals) = p; } } Node *bd = f->decl->b; Pos end = block_end(bd, f->decl->pos); // what the function releases, at its brace if (rt == t_void) { gen_block_stmts(bd, 0); dbg_at = end; pop_frame(); if (dbg_framed) line("S99_POP;"); if (root) line("S99_MAIN_END;"); } else { for (iz i = 0; i + 1 < bd->list.len; i++) gen_stmt(bd->list.data[i]); Node *last = bd->list.len ? bd->list.data[bd->list.len - 1] : 0; if (last && last->kind == N_EXPR && last->a->type != t_void) { // not fail(...) StmtCtx ctx = {0}; open_ctx(&ctx); dbg_at = last->pos; Str v = gen_move(last->a); Str r = temp(rt, v); close_ctx(&ctx); dbg_at = end; pop_frame(); if (dbg_framed) line("S99_POP;"); line("return %s;", r); } else { if (last) gen_stmt(last); dbg_at = end; frame = frame->parent; if (!last || !diverges(last)) line("s99_fail(%s, \"%s ended without returning a value\");", loc(f->decl->pos), f->name); if (!last || last->kind != N_RETURN) // for tcc line("return %s; /* not reached */", gen_zero(rt)); } } dir = debugging() ? line_directive(end) : (Str){0}; if (dir.len) bputf(&fb, "%s\n", dir); bput(&fb, S("}\n")); bput(&out_fns, bstr(&fb)); body = save; dbg_at = save_dbg; dbg_framed = save_framed; gen_at = save_at; genfn = savefn; frame = saveframe; } // Modules static Str dirname_of(Str path) { iz i = path.len; while (i > 0 && path.data[i - 1] != '/') i--; if (!i) return S("."); if (i == 1) return S("/"); return substr(path, 0, i - 1); } static Str basename_of(Str path) { iz i = path.len; while (i > 0 && path.data[i - 1] == '/') i--; path = substr(path, 0, i); iz j = path.len; while (j > 0 && path.data[j - 1] != '/') j--; return substr(path, j, path.len); } static Str join_path(Str a, Str b) { if (!a.len || equals(a, S("."))) return b; if (a.data[a.len - 1] == '/') return concat(a, b); return concat(concat(a, S("/")), b); } static b32 is_dir(Str p) { return os_stat(p, 0).isdir; } static STATE Map *collections = 0; static STATE b32 fuzz_targets_wanted = 0; // the main module has a build(): fuzz targets for its functions static STATE b32 expand_only = 0; // star99 --expand: print the expanded program static i32 add_file(Str path, Str text, i32 module) { SourceFile *f = push(&perm, &files); f->path = path; f->text = text; f->module = module; (void)push(&perm, &fileimports); // zero: no imports yet return (i32)(files.len - 1); } static void collect(Module *m, Node *d, Scope *fs, u32 flags); static void preload_imports(Node *n); static Module *load_module(Str dir, Pos from, Node *import, b32 single, Str single_file); static void cmpstr_sort(Str *a, iz n) { for (iz i = 1; i < n; i++) { Str k = a[i]; iz j = i; while (j > 0) { Str p = a[j - 1]; iz m = p.len < k.len ? p.len : k.len; int c = memcmp(p.data, k.data, (size_t)m); if (c < 0 || (c == 0 && p.len <= k.len)) break; a[j] = a[j - 1]; j--; } a[j] = k; } } // The .star99 files of dir, sorted; ok is 0 if dir cannot be read. static DirNames star99_files(Str dir) { DirNames all = os_listdir(dir), r = {0}; r.ok = all.ok; for (iz i = 0; i < all.names.len; i++) { Str name = all.names.data[i]; if (name.data[0] != '.' && endswith(name, S(".star99"))) *push(&perm, &r.names) = join_path(dir, name); } cmpstr_sort(r.names.data, r.names.len); return r; } static Module * find_module(Str key) { for (iz i = 0; i < modules.len; i++) { if (equals(modules.data[i]->key, key)) return modules.data[i]; } return 0; } static void process_import(Node *d, i32 file, Scope *into) { Str path = d->sval, dir = path; Pos p = d->pos; b32 isstd = startswith(path, S("std:")); if (!path.len) errorat(p, "an import needs the path of a module directory"); iz colon = -1; for (iz i = 0; i < path.len; i++) { if (path.data[i] == ':') colon = i; } if (isstd) { if (!equals(path, S("std:code"))) errorat(p, "there is no module %s; the standard collection has std:code", path); } else if (colon >= 0) { Str name = substr(path, 0, colon); Str *base = lookup(collections, name); if (!base) { help_text = fmtstr("declare it in the main module: collection %s = \"directory\"", name); errorat(p, "unknown collection %s", name); } dir = join_path(*base, substr(path, colon + 1, path.len)); } else { if (path.len && path.data[0] == '/') errorat(p, "imports are relative paths, so a program builds the same anywhere"); dir = join_path(dirname_of(files.data[file].path), path); } if (!isstd && !is_dir(dir)) { // a neighbouring directory with a close name Str parent = dirname_of(dir), base = basename_of(dir); near_start(base); Strs names = os_listdir(parent).names; for (iz i = 0; i < names.len; i++) { Str nm = names.data[i]; if (nm.data[0] != '.' && is_dir(join_path(parent, nm))) near_offer(nm); } near_help(); errorat(p, "no module directory %s", dir); } Module *m = load_module(dir, p, d, isstd, isstd ? path : (Str){0}); if (isstd) code_module = m; Module *from = modules.data[files.data[file].module]; if (from != m) *push(&perm, &from->deps) = m; FileImports *fi = fileimports.data + file; if (d->list.len && !d->name.len) errorat(p, "an import with arguments needs an alias: import \"...\" (...) as name"); if (into && !d->name.len) errorat(p, "an import inside a block needs an alias: import \"...\" as name"); if (d->name.len) { Sym *s = mksym(S_MODULE, d->name, p); s->mod = m; if (into) { declare(into, s); return; } void **slot = upsert(&fi->aliases, d->name, 1); if (*slot) errorat(p, "%s is already an import alias", d->name); *slot = s; } else { *push(&perm, &fi->open) = m; } } static void register_special(Fn *f); static Module * load_module(Str dir, Pos from, Node *import, b32 single, Str single_file) { b32 isstd = startswith(dir, S("std:")); b32 isstdin = single && equals(single_file, S("-")); Str rp = isstdin ? S("-") : isstd ? dir : os_realpath(single ? single_file : dir); if (!rp.len) errorat(from, "cannot open %s", dir); Str key = rp; if (import && import->list.len) { // module parameters make a separate instance per argument set Buf b = {0}; bput(&b, rp); for (iz i = 0; i < import->list.len; i++) { Node *a = import->list.data[i]; bputc(&b, '|'); if (a->kind == N_NAMED) { bput(&b, a->name); a = a->a; } // constant arithmetic, as in defaults: worked out here, so the // module sees a literal and the instance key the value i64 v = 0; Scope *fs = a->pos.file >= 0 && a->pos.file < fileimports.len ? fileimports.data[a->pos.file].scope : 0; if (a->kind == N_UNARY && a->op == T_MINUS && a->a->kind == N_FLOAT) { a->kind = N_FLOAT; a->fval = -a->a->fval; a->a = 0; } else if (a->kind != N_INT && fs && (no_const_overflow(a, fs), const_int(a, fs, &v))) { Node *lit = mknode(N_INT, a->pos); // a negative one is -n lit->ival = v < 0 ? (i64)(0 - (u64)v) : v; a->kind = v < 0 ? N_UNARY : N_INT; a->op = T_MINUS; a->ival = v; a->a = v < 0 ? lit : 0; a->b = 0; } if (a->kind == N_UNARY && a->op == T_MINUS && a->a->kind == N_INT) { bputi(&b, (i64)(0 - (u64)a->a->ival)); continue; } switch (a->kind) { case N_INT: bputi(&b, a->ival); break; case N_STR: bput(&b, a->sval); break; case N_BOOL: bput(&b, a->ival ? S("true") : S("false")); break; case N_FLOAT: bputf(&b, "%.17g", a->fval); break; default: errorat(a->pos, "module arguments are constants: literals, named constants or " "constant arithmetic"); } } key = bstr(&b); } Module *existing = find_module(key); if (existing) { if (!existing->loaded) errorat(from, "import cycle: this module imports itself through its imports"); return existing; } Module *m = new(&perm, 1, Module); m->isstd = isstd; m->dir = single ? dirname_of(single_file) : dir; m->name = basename_of(m->dir); m->key = key; m->id = (i32)modules.len; m->scope = mkscope(0, SC_MODULE); if (import) m->args = import->list; *push(&perm, &modules) = m; Strs paths = {0}; if (single) { *push(&perm, &paths) = single_file; } else { DirNames found = star99_files(dir); if (!found.ok) errorat(from, "cannot open %s", dir); if (!found.names.len) errorat(from, "%s has no .star99 files", dir); paths = found.names; } iz npaths = paths.len; Module *save = curmod; curmod = m; Nodes *perfile = new(&perm, npaths, Nodes); i32 *ids = new(&perm, npaths, i32); for (iz i = 0; i < npaths; i++) { Str text = {0}; if (isstd) { b32 prelude = equals(dir, S("std:prelude")), lib = equals(dir, S("std:build")); text = prelude ? join_lines(std_prelude_lines, countof(std_prelude_lines)) : lib ? join_lines(std_build_lines, countof(std_build_lines)) : join_lines(std_code_lines, countof(std_code_lines)); if (lib) text = c_build_text(text); #ifdef FUZZ } else if (equals(paths.data[i], S("-"))) { text = fuzz_case; #endif } else { Contents c = os_readfile(paths.data[i]); // "-" is the standard input if (!c.ok) errorat(from, "cannot read %s", paths.data[i]); text = c.data; } ids[i] = add_file(paths.data[i], text, m->id); *push(&perm, &m->fileids) = ids[i]; perfile[i] = parse_file(ids[i]); } // collections and module parameters first, then imports, then the rest for (iz i = 0; i < npaths; i++) { for (iz j = 0; j < perfile[i].len; j++) { Node *d = perfile[i].data[j]; if (d->kind == N_COLLECTION) { if (modules.len > 1 && m != modules.data[0]) errorat(d->pos, "collections are declared in the main module"); Str base = join_path(dirname_of(paths.data[i]), d->sval); Str *v = new(&perm, 1, Str); *v = base; void **slot = upsert(&collections, d->name, 1); if (*slot) errorat(d->pos, "collection %s is declared twice", d->name); *slot = v; } if (d->kind == N_MODULE) { if (m->params) errorat(d->pos, "a module declares its parameters once"); m->params = d; } } } Scope **filescopes = new(&perm, npaths, Scope *); for (iz i = 0; i < npaths; i++) { Scope *fs = mkscope(m->scope, SC_FILE); fs->file = ids[i]; filescopes[i] = fs; fileimports.data[ids[i]].scope = fs; } // module parameters are constants of the module if (m->params) { Node *pd = m->params; Nodes *args = &m->args; Str *missing = new(&perm, pd->list.len, Str); iz nmissing = 0; for (iz k = 0; k < pd->list.len; k++) { Node *pn = pd->list.data[k]; Node *value = 0; iz positional = 0; for (iz a = 0; a < args->len; a++) { Node *an = args->data[a]; if (an->kind == N_NAMED) { if (equals(an->name, pn->name)) value = an->a; } else if (positional++ == k) { value = an; } } if (!value) value = pn->b; if (!value) { missing[nmissing++] = pn->name; continue; } Node *v = mknode(N_VAR, pn->pos); v->name = pn->name; v->a = pn->a; v->b = value; v->flags = F_CONST; collect(m, v, filescopes[0], 0); } if (nmissing) errorat(import ? import->pos : pd->pos, "module %s needs the argument%s %s", m->name, nmissing > 1 ? S("s") : S(""), join_and(missing, nmissing)); } // arguments it does not take: all of them in one error iz npar = m->params ? m->params->list.len : 0, positional = 0, nunknown = 0, nbad = 0; Str *unknown = new(&perm, m->args.len + 1, Str); Pos at = import ? import->pos : (Pos){-1, 0, 0, 0}; for (iz a = 0; a < m->args.len; a++) { Node *an = m->args.data[a]; b32 known = an->kind != N_NAMED && positional++ < npar; for (iz k = 0; an->kind == N_NAMED && k < npar; k++) known |= equals(an->name, m->params->list.data[k]->name); if (known) continue; if (!nunknown && (an->kind == N_NAMED || !nbad)) at = an->pos; // the first one if (an->kind == N_NAMED) unknown[nunknown++] = an->name; nbad++; } iz extra = positional > npar ? positional - npar : 0; if (nunknown || extra) { Str *names = new(&perm, npar + 1, Str); for (iz k = 0; k < npar; k++) names[k] = m->params->list.data[k]->name; help_text = npar ? fmtstr("it takes %s", join_and(names, npar)) : S("it takes no arguments"); if (!nunknown) errorat(at, "module %s takes %lld argument%s, but this gives %lld", m->name, (long long)npar, npar == 1 ? S("") : S("s"), (long long)positional); errorat(at, "module %s has no parameter%s %s%s", m->name, nunknown > 1 ? S("s") : S(""), join_and(unknown, nunknown), extra ? S(", and it gets too many arguments by position") : S("")); } c_note_own(m, perfile, npaths); for (iz i = 0; i < npaths; i++) { for (iz j = 0; j < perfile[i].len; j++) { Node *d = perfile[i].data[j]; if (d->kind == N_IMPORT || d->kind == N_COLLECTION || d->kind == N_MODULE) { addnode(&m->header, d); continue; } addnode(&m->decls, d); collect(m, d, filescopes[i], 0); preload_imports(d); } } c_effect_kind(m); // imports after the declarations: an argument may name a constant for (iz i = 0; i < npaths; i++) { for (iz j = 0; j < perfile[i].len; j++) { Node *d = perfile[i].data[j]; if (d->kind == N_IMPORT) process_import(d, ids[i], 0); } } m->loaded = 1; curmod = save; // a module loaded while checking (std:code, the prelude) brings its // print overloads along for (iz i = 0; checking_started && i < all_fns.len; i++) { if (all_fns.data[i]->module == m->id) register_special(all_fns.data[i]); } return m; } static STATE struct { Sym **data; iz len; iz cap; } globals = {0}; static STATE Nodes file_macros = {0}; static STATE Scopes file_macro_scopes = {0}; static void synth_compile_time(Node *n, Scope *fs); // Modules imported inside blocks load with their importer, so every module // is known before checking begins. static void preload_imports(Node *n) { if (!n) return; if (n->kind == N_IMPORT) { Str path = n->sval; Str dir = {0}; iz colon = -1; for (iz i = 0; i < path.len; i++) { if (path.data[i] == ':') colon = i; } if (startswith(path, S("std:"))) return; if (colon >= 0) { Str *base = lookup(collections, substr(path, 0, colon)); if (!base) return; dir = join_path(*base, substr(path, colon + 1, path.len)); } else { dir = join_path(dirname_of(files.data[n->pos.file].path), path); } if (is_dir(dir)) load_module(dir, n->pos, n, 0, (Str){0}); return; } // compile-time code runs macro if, for and switch as ordinary ones static STATE int in_macro = 0; if ((n->kind == N_IF || n->kind == N_FOR || n->kind == N_SWITCH) && (n->flags & F_MACRO) && !in_macro && !n->info && n->pos.file < fileimports.len && fileimports.data[n->pos.file].scope) { synth_compile_time(n, fileimports.data[n->pos.file].scope); return; // its bodies are templates } b32 ct = (n->kind == N_FN || n->kind == N_DECLS) && (n->flags & F_MACRO); in_macro += ct; preload_imports(n->a); preload_imports(n->b); preload_imports(n->c); for (iz i = 0; i < n->list.len; i++) preload_imports(n->list.data[i]); in_macro -= ct; } // coroutine(fn NAME(...) ..., Y) at file scope: besides NAME, the type // Coroutine(NAME), whose fields wait for the body, and its resume overload static void declare_coroutine(Module *m, Fn *f, Scope *fs) { Node *d = f->decl; if (d->flags & F_MACRO) errorat(d->pos, "a coroutine runs at run time; it cannot be a macro"); if (equals(d->name, S("resume"))) errorat(d->pos, "a coroutine cannot be named resume: resume is what runs it"); for (iz i = 0; i < d->list.len; i++) { if (d->list.data[i]->flags & F_VAR) errorat(d->list.data[i]->pos, "a coroutine keeps its arguments between resumes, so none can be var"); } Coro *co = new(&perm, 1, Coro); co->fn = f; f->co = co; Type *t = mktype(TY_STRUCT); t->named = 1; t->name = fmtstr("Coroutine(%s)", d->name); t->module = m->id; t->isprivate = (d->flags & F_PRIVATE) != 0; t->co = co; co->type = t; // resume(var c: Coroutine(NAME)): its signature comes from the // coroutine's (co_signature), and its code from its body (gen_resume) Sym *rs = scope_get(m->scope, S("resume")); if (rs && rs->kind != S_FN) { notes.len = 0; note(d->pos, S("this coroutine makes a function resume")); errorat(rs->pos, "resume is already declared as something else"); } if (!rs) { rs = mksym(S_FN, S("resume"), d->pos); rs->module = m->id; declare(m->scope, rs); } Node *rd = mknode(N_FN, d->pos); rd->name = S("resume"); rd->flags = d->flags & F_PRIVATE; rd->b = mknode(N_BLOCK, d->pos); Fn *r = new_fn(rd, fs, S("resume")); r->module = m->id; r->co = co; co->resume = r; *push(&perm, &rs->fns) = r; *push(&perm, &coroutines) = co; } static void collect(Module *m, Node *d, Scope *fs, u32 flags) { flags |= d->flags & (F_MACRO | F_PRIVATE); switch ((int)d->kind) { case N_DECLS: for (iz i = 0; i < d->list.len; i++) collect(m, d->list.data[i], fs, flags); return; case N_FN: { Sym *sym = scope_get(m->scope, d->name); if (sym && sym->kind != S_FN) errorat(d->pos, "%s is already declared as something else", d->name); if (!sym) { sym = mksym(S_FN, d->name, d->pos); sym->module = m->id; sym->isprivate = (flags & F_PRIVATE) != 0; declare(m->scope, sym); } d->flags |= flags; Fn *f = new_fn(d, fs, d->name); f->module = m->id; for (iz i = 0; i < sym->fns.len; i++) { // Coroutine(NAME) names one function Fn *g = sym->fns.data[i]; if (!(d->flags & F_COROUTINE) && !(g->co && g == g->co->fn)) continue; notes.len = 0; note(g->decl->pos, S("the other one")); errorat(d->pos, "a coroutine cannot share its name with another function: %s", d->name); } *push(&perm, &sym->fns) = f; d->info = f; if (f->ismacro) *push(&perm, ¯o_fns) = f; if (d->flags & F_COROUTINE) declare_coroutine(m, f, fs); return; } case N_STRUCT: case N_UNION: { Type *t = mktype(d->kind == N_STRUCT ? TY_STRUCT : TY_UNION); t->named = 1; t->name = d->name; t->module = m->id; t->decl = d; t->nonil = (d->flags & F_NONIL) != 0; t->ismacro = (flags & F_MACRO) != 0; t->isprivate = (flags & F_PRIVATE) != 0; d->info = fs; Sym *sym = mksym(S_TYPE, d->name, d->pos); sym->type = t; sym->module = m->id; sym->isprivate = t->isprivate; sym->ismacro = t->ismacro; declare(m->scope, sym); return; } case N_VAR: { Sym *sym = mksym(S_VAR, d->name, d->pos); sym->isglobal = 1; sym->isconst = (d->flags & F_CONST) != 0; sym->ismacro = (flags & F_MACRO) != 0; sym->isprivate = (flags & F_PRIVATE) != 0; sym->decl = d; sym->module = m->id; sym->cname = newcname(S("g"), d->name); sym->scope = fs; d->sym = sym; declare(m->scope, sym); *push(&perm, &globals) = sym; return; } case N_EXPR: case N_IF: case N_FOR: case N_SWITCH: if (c_header(m, d)) return; // the C of a plugin // file-scope macro calls and compile-time control flow run where they // are, in source order (check_in_order) addnode(&file_macros, d); *push(&perm, &file_macro_scopes) = fs; if (d->kind != N_EXPR) synth_compile_time(d, fs); return; case N_MLABEL: { // a file-scope region: its declarations are the module's Sym *l = mksym(S_MLABEL, d->name, d->pos); l->scope = fs; l->target = d; declare(m->scope, l); for (iz i = 0; i < d->a->list.len; i++) collect(m, d->a->list.data[i], fs, flags); return; } default: errorat(d->pos, "this does not belong at file scope"); } } static void check_global(Sym *g) { if (g->type) return; Node *d = g->decl; if (g->resolving) errorat(d->pos, "%s is defined in terms of itself", g->name); g->resolving = 1; Scope *s = g->scope; Module *save = curmod; curmod = modules.data[g->module]; b32 savemacro = in_macro_code; in_macro_code |= g->ismacro; // a macro { } global: compile-time types are fine Type *t = d->a ? resolve_type(d->a, s) : 0; if (d->b) { if (!is_constant(d->b, s)) errorat(d->b->pos, "a global starts as a constant; compute other values in main"); check_value(&d->b, s, t, d->name); if (!t) t = d->b->type; if (t == t_nil) errorat(d->pos, "nil needs a type; write var %s: T = nil", d->name); } g->type = t; g->resolving = 0; curmod = save; in_macro_code = savemacro; } // print, arbitrary and valid overloads apply to their types wherever those // are printed or built, so they are found when their module loads. // Type-wide overloads (print, valid, arbitrary, operators) apply to every // use of their types. One a macro generates may not change code checked // before it: the first use of each kind is kept per type, and a generated // overload that would reach back is an error at the call that made it. static STATE Map *type_uses = 0; // "kind:type id" -> Pos * static void record_use(Str kind, Type *t, Pos at, int depth) { if (!t || depth > 16 || !in_order) return; Pos **slot = (Pos **)upsert(&type_uses, fmtstr("%s:%d", kind, t->id), 1); if (*slot) return; // the first use is the one named *slot = new(&perm, 1, Pos); **slot = at; // printing or building a value prints or builds its parts too if (t->kind == TY_STRUCT || t->kind == TY_UNION) { for (iz i = 0; i < t->fields.len; i++) record_use(kind, t->fields.data[i].type, at, depth + 1); } if (t->elem) record_use(kind, t->elem, at, depth + 1); if (t->kind == TY_MAP) record_use(kind, t->key, at, depth + 1); } // f was made by a macro: the uses it would change come before it static void no_backward_overload(Fn *f, Str kind, Type *t, Str what) { if (!f->decl || f->decl->pos.file < 0 || !files.data[f->decl->pos.file].generated || !t) return; Pos *first = lookup(type_uses, fmtstr("%s:%d", kind, t->id)); if (!first) return; notes.len = 0; Pos *boxed = equals(kind, S("print")) ? lookup(type_uses, fmtstr("any:%d", t->id)) : 0; b32 asany = boxed && boxed->file == first->file && boxed->offset == first->offset; note(*first, fmtstr("%s %s here, before it", type_name(t), asany ? S("becomes an Any") : what)); help_text = S("declare it before that use, or by hand"); errorat(files.data[f->decl->pos.file].origin, "this makes %s %s overload for %s, which would change code checked before it", oneof(f->name.data[0], S("aeiou")) ? S("an") : S("a"), f->name, type_name(t)); } static void register_special(Fn *f) { if (f->isnested || f->isclosure || f->builtin || f->special_done) return; b32 print = equals(f->name, S("print")), arb = equals(f->name, S("arbitrary")), val = equals(f->name, S("valid")); if (!print && !arb && !val) return; f->special_done = 1; Module *save = curmod; curmod = modules.data[f->module]; fn_signature(f); curmod = save; Fields *ps = &f->type->fields; if (ps->len != 1 || ps->data[0].isvar) return; if (print) no_backward_overload(f, S("print"), ps->data[0].type, S("is printed")); if (arb) no_backward_overload(f, S("fuzz"), f->type->elem, S("is built for fuzzing")); if (val) no_backward_overload(f, S("fuzz"), ps->data[0].type, S("is built for fuzzing")); if (print) { if (f->type->elem != t_void) errorat(f->decl->pos, "a print overload returns nothing"); Str key = fmtstr("%d", ps->data[0].type->id); if (lookup(print_overloads, key)) errorat(f->decl->pos, "%s already has a print overload", type_name(ps->data[0].type)); *upsert(&print_overloads, key, 1) = f; f->used = 1; } // fuzzing: arbitrary(d: Draw) -> T builds a T; valid(x: T) -> Bool says // which values are inputs at all if (arb && ps->data[0].type == t_draw && f->type->elem != t_void) { *upsert(&arbitraries, fmtstr("%d", f->type->elem->id), 1) = f; f->used = 1; } if (val && f->type->elem == t_bool) { *upsert(&valids, fmtstr("%d", ps->data[0].type->id), 1) = f; f->used = 1; } } // Two functions or macros of one name taking the same parameters: no call // could choose between them. static void same_overloads(Sym *s) { Fn *first = 0; for (iz i = 0; i < s->fns.len; i++) { Fn *a = s->fns.data[i]; if (a->builtin || a->isnested || a->isclosure || !a->decl) continue; if (!first) first = a; if (a->isparenless == first->isparenless) continue; notes.len = 0; note(first->decl->pos, first->isparenless ? S("this overload is parenless") : S("this overload is not parenless")); errorat(a->decl->pos, "some overloads of %s are parenless and some are not; wrap all of them in " "parenless(...), or none", s->name); } for (iz i = 0; i < s->fns.len; i++) { Fn *a = s->fns.data[i]; if (a->builtin || a->isnested || a->isclosure || !a->decl) continue; fn_signature(a); for (iz j = 0; j < i; j++) { Fn *b = s->fns.data[j]; if (b->builtin || b->isnested || b->isclosure || !b->decl || b->ismacro != a->ismacro) continue; fn_signature(b); Fields *pa = &a->type->fields, *pb = &b->type->fields; b32 same = pa->len == pb->len; for (iz k = 0; same && k < pa->len; k++) same = pa->data[k].type == pb->data[k].type && pa->data[k].isvar == pb->data[k].isvar && // Expr(Int) and Expr are one type: compare as written (!a->ismacro || !a->decl->list.data[k]->a || !b->decl->list.data[k]->a || equals(node_source(a->decl->list.data[k]->a), node_source(b->decl->list.data[k]->a))); if (!same) continue; notes.len = 0; note(b->decl->pos, S("the first one")); errorat(a->decl->pos, "%s %s is declared twice with the same parameters", a->ismacro ? S("macro") : S("function"), a->name); } } } // Closures that outlive their scope keep their captured variables alive. // Run before each generation: the metaprogram is generated in the middle of // checking. static void pin_escaping_captures(void) { for (iz i = 0; i < closures.len; i++) { Fn *f = closures.data[i]; if (!f->escapes) continue; for (iz j = 0; j < f->captures.len; j++) { Sym *s = f->captures.data[j]; if (s->isvar) errorat(f->decl->pos, "this function value may outlive the call, so it cannot use the var " "parameter %s; use a nested fn name(...) instead", s->name); s->pinned = 1; } } } static void check_module_in_order(Module *m); // Every named type of every module is resolved, and overloads that no call // could tell apart are refused. static void resolve_types_of(Module *m) { curmod = m; Map *stack[256] = {0}; int sp = 0; stack[sp++] = m->scope->names; while (sp) { Map *node = stack[--sp]; if (!node) continue; for (int k = 0; k < 4; k++) { if (node->child[k] && sp < 256) stack[sp++] = node->child[k]; } Sym *s = node->value; if (s && s->kind == S_TYPE) { b32 save = in_macro_code; in_macro_code |= s->ismacro; // a macro struct may hold other macro types resolve_named(s->type); in_macro_code = save; } if (s && s->kind == S_FN && s->fns.len > 1) same_overloads(s); } } static void resolve_module_types(void) { for (iz mi = 0; mi < modules.len; mi++) resolve_types_of(modules.data[mi]); } // One pass in source order (design.md, Metaprogramming): hand-written // declarations are known everywhere; macro calls run where they are, and // function bodies are checked where they are. static void check_program(void) { checking_started = 1; // before any macro runs: the metaprogram prints code with them too for (iz i = 0; i < all_fns.len; i++) register_special(all_fns.data[i]); for (iz mi = 0; mi < modules.len; mi++) c_effect_kind(modules.data[mi]); resolve_module_types(); in_order = 1; check_module_in_order(prelude_module); for (b32 more = 1; more;) { // modules loaded meanwhile (std:code) too more = 0; for (iz mi = 0; mi < modules.len; mi++) { if (modules.data[mi]->checked) continue; check_module_in_order(modules.data[mi]); more = 1; } } in_order = 0; resolve_module_types(); // the generated ones for (iz i = 0; i < globals.len; i++) check_global(globals.data[i]); for (iz i = 0; i < all_fns.len; i++) { Fn *f = all_fns.data[i]; if (f->isnested || f->isclosure) continue; curmod = modules.data[f->module]; register_special(f); } for (iz i = 0; i < all_fns.len; i++) { Fn *f = all_fns.data[i]; if (f->isnested || f->isclosure) continue; curmod = modules.data[f->module]; check_fn(f); } for (iz i = 0; i < coroutines.len; i++) co_need_body(coroutines.data[i]); pin_escaping_captures(); program_checked = 1; Sym *mainsym = scope_get(main_module->scope, S("main")); if (!mainsym || mainsym->kind != S_FN) errorat((Pos){main_module->fileids.data[0], 0, 0, 0}, "the program has no fn main()"); } // Ownership flags of compound types depend on their parts, which may be // resolved later than the compound itself. static void finalize_types(void) { for (int round = 0; round < 3; round++) { Map *stack[4096] = {0}; int sp = 0; if (interned) stack[sp++] = interned; while (sp) { Map *node = stack[--sp]; for (int k = 0; k < 4; k++) { if (node->child[k] && sp < 4096) stack[sp++] = node->child[k]; } if (node->value) compute_flags(node->value, 0); } for (iz mi = 0; mi < modules.len; mi++) { Map *st[4096] = {0}; int p = 0; if (modules.data[mi]->scope->names) st[p++] = modules.data[mi]->scope->names; while (p) { Map *node = st[--p]; for (int k = 0; k < 4; k++) { if (node->child[k] && p < 4096) st[p++] = node->child[k]; } Sym *sym = node->value; if (sym && sym->kind == S_TYPE) compute_flags(sym->type, 0); } } for (iz i = 0; i < coroutines.len; i++) compute_flags(coroutines.data[i]->type, 0); } } static void gen_wrapper(Fn *f) { Type *rt = f->type->elem; Buf w = {0}; bputf(&w, "static %s\n%s(void *envp", ctype(rt), fn_wrapper(f)); Fields *ps = &f->type->fields; for (iz i = 0; i < ps->len; i++) bputf(&w, ", %s a%lld", ps->data[i].isvar ? S("s99_ref") : ctype(ps->data[i].type), (long long)i); bputf(&w, ")\n{\n (void)envp;\n %s%s(", rt == t_void ? S("") : S("return "), f->cname); for (iz i = 0; i < ps->len; i++) bputf(&w, "%sa%lld", i ? S(", ") : S(""), (long long)i); bput(&w, S(");\n}\n")); bputf(&out_protos, "static S99_UNUSED %s %s(void *envp", ctype(rt), fn_wrapper(f)); for (iz i = 0; i < ps->len; i++) bputf(&out_protos, ", %s", ps->data[i].isvar ? S("s99_ref") : ctype(ps->data[i].type)); bput(&out_protos, S(");\n")); bput(&out_fns, bstr(&w)); } // The two programs built from one source: the run-time program, and the // metaprogram that runs macros. Each gets its own copy of plain functions, // types and globals; macro declarations exist only in the metaprogram. // Defines the descriptors: name, layout, fields or variants, the copy and // drop helpers of managed types, and the overload hooks. static STATE iz described_done = 0; // descriptors written so far static void gen_descriptors(void) { for (iz k = described_done; k < described.len; k = described_done) { described_done = k + 1; // each once, also when called again for late ones Type *t = described.data[k]; i32 id = t->id; Str c = ctype(t); b32 isvoid = t->kind == TY_VOID || t->kind == TY_NIL; Buf b = {0}; Str fields = S("0"), copy = S("0"), drop = S("0"), print = S("0"); if (t->kind == TY_STRUCT || t->kind == TY_UNION) { bputf(&b, "static const s99_field fields%d[] = {\n", id); for (iz i = 0; i < t->fields.len; i++) { Field *f = t->fields.data + i; b32 none = !f->type || f->type == t_void; if (t->kind == TY_STRUCT) bputf(&b, " {\"%s\", &info%d, offsetof(%s, f_%s), 0, %d},\n", f->name, f->type->id, c, cident(f->name), f->def || f->type->has_defaults); else bputf(&b, " {\"%s\", %s%s, 0, %d, 0},\n", variant_name(t, i), none ? S("0") : S("&info"), none ? S("") : fmtstr("%d", f->type->id), f->name.len > 0); } bput(&b, t->fields.len ? S("};\n") : S(" {0}};\n")); fields = fmtstr("fields%d", id); } if (t->managed) { need(t, H_COPY); need(t, H_DROP); bputf(&b, "static void\ninfocopy%d(void *d, const void *s)\n{\n %s\n}\n", id, copy_stmt(t, fmtstr("*(%s *)d", c), fmtstr("*(%s *)s", c))); bputf(&b, "static void\ninfodrop%d(void *v)\n{\n %s\n}\n", id, drop_stmt(t, fmtstr("*(%s *)v", c))); copy = fmtstr("infocopy%d", id); drop = fmtstr("infodrop%d", id); } Str up = user_print(t); if (up.len) { bputf(&b, "static void\ninfoprint%d(s99_stream *o, const void *v)\n{\n" " s99_stream *saved = s99_out;\n s99_out = o;\n %s(*(%s *)v);\n" " s99_out = saved;\n}\n", id, up, c); print = fmtstr("infoprint%d", id); } Str init = S("0"), arb = S("0"), valid = S("0"), eqf = S("0"); Fn *ue = user_equality(t); if (ue) { // == of a list, struct, map or union holding it uses it too use_fn(ue); define_type(t); bputf( &b, "static bool\ninfoeq%d(const void *a, const void *b)\n{\n return %s(*(%s *)a, *(%s *)b);\n}\n", id, ue->cname, c, c); eqf = fmtstr("infoeq%d", id); } if (t->kind == TY_STRUCT && t->has_defaults) { bputf(&b, "static void\ninfoinit%d(void *v)\n{\n init%d((%s *)v);\n}\n", id, id, c); init = fmtstr("infoinit%d", id); } Fn *ua = lookup(arbitraries, fmtstr("%d", id)), *uv = lookup(valids, fmtstr("%d", id)); if (ua) { bputf(&b, "static void\ninfoarb%d(s99_draw *d, void *v)\n{\n *(%s *)v = %s(d);\n}\n", id, c, ua->cname); arb = fmtstr("infoarb%d", id); } if (uv) { bputf(&b, "static bool\ninfovalid%d(const void *v)\n{\n return %s(*(%s *)v);\n}\n", id, uv->cname, c); valid = fmtstr("infovalid%d", id); } char kind = t->text ? 's' : t->kind == TY_BOOL ? 'b' : t->kind == TY_INT ? (t->issigned ? 'i' : 'u') : t->kind == TY_FLOAT ? 'f' : t->kind == TY_LIST ? 'l' : t->kind == TY_ARRAY ? 'a' : t->kind == TY_MAP ? 'm' : t->kind == TY_POOL ? 'p' : t->kind == TY_STRUCT ? 'S' : t->kind == TY_UNION ? (compact_union(t) ? 'c' : 'U') : t->kind == TY_REF ? 'r' : t->kind == TY_FN ? 'F' : t->kind == TY_TYPE ? 't' : t->kind == TY_FILE ? 'o' : t->kind == TY_ANY ? 'A' : '?'; Buf nm = {0}; Str name = type_name(t); for (iz i = 0; i < name.len; i++) { if (name.data[i] == '"' || name.data[i] == '\\') bputc(&nm, '\\'); bputc(&nm, name.data[i]); } iz count = t->kind == TY_ARRAY ? t->count : t->fields.len; bputf( &b, "static S99_UNUSED const s99_type info%d = {\"%s\", %s, '%c', %d, %d, %lld, %s, %s, %s, %s, %s, %s, " "%d, %s, %s, %s, %s};\n", id, bstr(&nm), isvoid ? S("0") : fmtstr("sizeof(%s)", c), kind, t->nonil != 0, t->has_refs || t->kind == TY_REF, (long long)count, t->elem ? fmtstr("&info%d", t->elem->id) : S("0"), t->key ? fmtstr("&info%d", t->key->id) : S("0"), fields, copy, drop, print, id, init, arb, valid, eqf); bput(&out_helpers, bstr(&b)); } } static void reset_codegen(void) { out_types.len = out_protos.len = out_helpers.len = out_globals.len = out_fns.len = out_fuzz.len = 0; literals = 0; // their definitions were in out_globals nliterals = 0; for (iz i = 0; i < all_types.len; i++) { Type *t = all_types.data[i]; t->helpers = 0; if (t->state > 0) t->state = 0; if (t->kind == TY_ARRAY || t->kind == TY_STRUCT || t->kind == TY_UNION || debug_build) t->cname = (Str){0}; // a debug build names every type in its own C } for (iz i = 0; i < all_fns.len; i++) all_fns.data[i]->emitted = all_fns.data[i]->queued = 0; // the analyses are worked out again: more functions are checked now for (iz i = 0; i < all_fns.len; i++) all_fns.data[i]->area = all_fns.data[i]->alloc = all_fns.data[i]->writes = 0; areas_known = writes_known = 0; described.len = 0; described_done = 0; pending_fns.len = 0; } static Type * code_type(Str name) { if (!code_module) return 0; Sym *s = scope_get(code_module->scope, name); return s && s->kind == S_TYPE ? s->type : 0; } // Whether fuzzing can build a value of this type for a target: no // functions, references, files or draws, which builders leave zero. static b32 fuzzable(Type *t, int depth) { if (!t || depth > 16) return 1; switch (t->kind) { case TY_FN: case TY_REF: case TY_FILE: case TY_DRAW: return 0; case TY_LIST: case TY_ARRAY: case TY_POOL: return fuzzable(t->elem, depth + 1); case TY_MAP: return fuzzable(t->key, depth + 1) && fuzzable(t->elem, depth + 1); case TY_STRUCT: case TY_UNION: for (iz i = 0; i < t->fields.len; i++) { if (!fuzzable(t->fields.data[i].type, depth + 1)) return 0; } return 1; default: return 1; } } // Fuzz targets: main, and in a program with a build(), every function of // the main module whose parameters fuzzing can build. Each gets a runner // that builds its arguments from the input, calls it, and checks valid() // on what it returns. static Buf gen_fuzz_targets(void) { Buf names = {0}, runs = {0}; bput(&names, S(" static const char *const s99_target_names[] = {\"main\"")); bput(&runs, S(" static void (*const s99_target_runs[])(s99_draw *, bool) = {0")); for (iz i = 0; fuzz_targets_wanted && i < all_fns.len; i++) { Fn *f = all_fns.data[i]; if (f->module != main_module->id || f->ismacro || f->isnested || f->isclosure || f->builtin || !f->decl || f->co) continue; if (equals(f->name, S("main")) || equals(f->name, S("arbitrary")) || equals(f->name, S("valid")) || equals(f->name, S("print")) || !f->checked) continue; Fields *ps = &f->type->fields; b32 ok = 1; for (iz j = 0; j < ps->len; j++) ok &= !ps->data[j].isvar && fuzzable(ps->data[j].type, 0); if (!ok) continue; f->used = 1; gen_fn(f); b32 raw = ps->len == 1 && ps->data[0].type == list_of(t_u8); // the input bytes as they are Buf r = {0}; bputf(&r, "static void\ns99_run_%s(s99_draw *d, bool show)\n{\n (void)show;\n", f->cname); Buf args = {0}; for (iz j = 0; j < ps->len; j++) { Type *pt = ps->data[j].type; define_type(pt); bputf(&r, " %s a%lld;\n", ctype(pt), (long long)j); if (raw) { bput(&r, S(" a0 = s99_bytes(d->p, d->n);\n")); } else { need(pt, H_INFO); bputf(&r, " s99_arb(d, &info%d, &a%lld);\n", pt->id, (long long)j); } bputf(&args, "%sa%lld", j ? S(", ") : S(""), (long long)j); } bputf(&r, " if (show) {\n s99_puts(&s99_stderr_stream, \"call: %s(\");\n", f->name); for (iz j = 0; j < ps->len; j++) { need(ps->data[j].type, H_INFO); if (j) bput(&r, S(" s99_puts(&s99_stderr_stream, \", \");\n")); bputf(&r, " s99_show(&s99_stderr_stream, &info%d, &a%lld, S99_SHOW);\n", ps->data[j].type->id, (long long)j); } bput(&r, S(" s99_puts(&s99_stderr_stream, \")\\n\");\n }\n")); Type *rt = f->type->elem; if (rt == t_void) { bputf(&r, " %s(%s);\n", f->cname, bstr(&args)); } else { define_type(rt); bputf(&r, " %s result = %s(%s);\n (void)result;\n", ctype(rt), f->cname, bstr(&args)); Fn *uv = lookup(valids, fmtstr("%d", rt->id)); if (uv) { use_fn(uv); bputf( &r, " if (!%s(result)) s99_fail(%s, \"%s returned %s that is not valid()\");\n", uv->cname, loc(f->decl->pos), f->name, with_articles(fmtstr("a %s", type_name(rt)))); } Str dr = drop_stmt(rt, S("result")); if (dr.len) bputf(&r, " %s\n", dr); } for (iz j = 0; j < ps->len; j++) { Str dr = drop_stmt(ps->data[j].type, fmtstr("a%lld", (long long)j)); if (dr.len) bputf(&r, " %s\n", dr); } bput(&r, S("}\n")); bput(&out_fuzz, bstr(&r)); bputf(&names, ", \"%s\"", f->name); bputf(&runs, ", s99_run_%s", f->cname); } bput(&names, S("};\n")); bput(&runs, S("};\n")); bput(&names, bstr(&runs)); // the words and compared numbers of the program, which builders mix in: // string literals, and integers next to a comparison or after case Buf words = {0}, lens = {0}, nums = {0}; Map *seen = 0; iz nw = 0, nn = 0; for (iz i = 0; i < files.len; i++) { if (!is_program_source(i)) continue; Toks toks = lex((i32)i); for (iz k = 0; k < toks.len; k++) { Tok *t = toks.data + k; if (t->kind == T_STRING && t->text.len && t->text.len < 256 && !c_text_at((i32)i, toks, k) && first_time(&seen, t->text)) { bputf(&words, "%s%s", nw ? S(", ") : S(""), c_string(t->text)); bputf(&lens, "%s%lld", nw ? S(", ") : S(""), (long long)t->text.len); nw++; } if (t->kind == T_INT && k > 0) { TokKind before = toks.data[k - 1].kind, after = k + 1 < toks.len ? toks.data[k + 1].kind : T_EOF; b32 cmp = (binprec(before) == 3 && before != T_IN) || before == T_CASE || (binprec(after) == 3 && after != T_IN); Str key = fmtstr("#%lld", (long long)t->ival); if (cmp && first_time(&seen, key)) { bputf(&nums, "%s%lld", nn ? S(", ") : S(""), (long long)t->ival); nn++; } } } } bputf(&names, " static const char *const s99_words_[] = {%s%s0};\n", bstr(&words), nw ? S(", ") : S("")); bputf(&names, " static const int64_t s99_word_lens_[] = {%s%s0};\n", bstr(&lens), nw ? S(", ") : S("")); bputf(&names, " static const int64_t s99_numbers_[] = {%s%s0};\n", bstr(&nums), nn ? S(", ") : S("")); bputf( &names, " s99_words = s99_words_;\n s99_word_lens = s99_word_lens_;\n s99_nwords = %lld;\n" " s99_numbers = s99_numbers_;\n s99_nnumbers = %lld;\n", (long long)nw, (long long)nn); return names; } // var parameters as plain pointers. A var argument is a route because the // place it names could move during the call (a list element, when the list // grows). A whole variable, or a struct field of one, cannot move while the // callee runs, so when every call of a function passes such places, its var // parameters are pointers. Functions used as values, closures, and cases of // a dispatch keep routes. static b32 stable_place(Node *a) { while (a->kind == N_FIELD) { FieldAccess *fa = a->info; if (fa && fa->kind != FA_STRUCT) return 0; a = a->a; } if (a->kind != N_NAME || !a->sym || a->sym->kind != S_VAR) return 0; Sym *s = a->sym; if (s->co_path.len) return s->co->resume->var_ptr; // inside c, which is a pointer or a route if (s->isvar) return s->isparam && s->owner && s->owner->var_ptr && s->owner == genfn; return !s->owner || s->owner == genfn; // a captured local goes through a route } static void var_ptr_pass(Node *n, b32 *changed) { if (!n) return; if ((int)n->kind == X_CALLFN) { Call *c = n->info; if (c->kind == CALL_FN && c->fn->var_ptr) { Fields *ps = &c->fn->type->fields; for (iz i = 0; i < ps->len && i < c->nargs; i++) { if (ps->data[i].isvar && c->args[i] && !stable_place(c->args[i]->a)) { c->fn->var_ptr = 0; *changed = 1; } } } if (c->kind == CALL_DISPATCH) { for (iz v = 0; c->cases && v < c->dispatch_union->fields.len; v++) { if (c->cases[v] && c->cases[v]->var_ptr) c->cases[v]->var_ptr = 0, *changed = 1; } } for (iz i = 0; i < c->nargs; i++) var_ptr_pass(c->args[i], changed); } var_ptr_pass(n->a, changed); var_ptr_pass(n->b, changed); if (n->kind != N_FOR) var_ptr_pass(n->c, changed); for (iz i = 0; i < n->list.len; i++) var_ptr_pass(n->list.data[i], changed); } static void decide_var_ptrs(void) { for (iz i = 0; i < all_fns.len; i++) { Fn *f = all_fns.data[i]; b32 any = 0; for (iz j = 0; f->type && j < f->type->fields.len; j++) any |= f->type->fields.data[j].isvar; f->var_ptr = optimizing() && any && f->checked && !f->builtin && !f->isclosure && !f->escapes; // a resume's c stays a pointer when nested functions use what it // keeps: their environment gets a route to it (co_param_ref) for (iz j = 0; f->var_ptr && j < f->params.len; j++) { if (f->params.data[j]->isvar && f->params.data[j]->iscaptured && !(f->co && f->co->resume == f)) f->var_ptr = 0; } } for (b32 changed = 1; changed;) { changed = 0; for (iz i = 0; i < all_fns.len; i++) { Fn *f = all_fns.data[i]; if (!f->checked || !f->decl) continue; genfn = f; var_ptr_pass(f->decl->b, &changed); } } genfn = 0; } // Functions are generated as code refers to them, from main outward, so a // program carries only what it can reach. static void use_fn(Fn *f) { if (f && meta_mode && !f->checked && !f->builtin && !f->emitted && f->type && !f->ismacro) { // a run-time function the metaprogram mentions (from one checked // before it) but compile-time code never calls: a stub f->emitted = 1; define_type(f->type->elem); for (iz i = 0; i < f->type->fields.len; i++) if (!f->type->fields.data[i].isvar) define_type(f->type->fields.data[i].type); Str sig = fmtstr("static S99_UNUSED %s %s(%s)", ctype(f->type->elem), f->cname, fn_params_c(f)); bputf(&out_protos, "%s;\n", sig); bputf(&out_fns, "%s\n{\n s99_fail(\"star99\", \"%s runs only at run time\");\n}\n", sig, f->name); return; } if (!f || f->builtin || f->emitted || f->queued || !f->checked) return; if (f->ismacro && !meta_mode) return; f->queued = 1; *push(&perm, &pending_fns) = f; } static void gen_pending_fns(void) { while (pending_fns.len) gen_fn(pending_fns.data[--pending_fns.len]); } // The function a run-time program starts at: main, or the one with_main(b, // name) asks for in build static STATE Fn *root_override = 0; static Fn * root_fn(void) { if (root_override) return root_override; return scope_get(main_module->scope, S("main"))->fns.data[0]; } // Debug builds: Star99 names. A pass over the finished C renames each // function, global, type, field, parameter and local to its Star99 name // where that cannot change what the C means: the name is no C keyword or // macro, and nothing it could be taken for is written there. Two locals of // one function share a name only where their scopes do not meet, or where // one hides the other in a deeper block without the other being used // there. Whatever cannot have its name keeps the numbered one. // Names the generated C may meet that a Star99 name must not take: C // keywords and the lowercase macros of the headers the runtime includes. static Str c_words[] = { SL("auto break case char const continue default do double else enum extern float for goto"), SL("if inline int long register restrict return short signed sizeof static struct switch"), SL("typedef union unsigned void volatile while asm typeof typeof_unqual bool true false"), SL("nullptr constexpr static_assert thread_local alignas alignof main errno assert stdin"), SL("stdout stderr offsetof setjmp longjmp sigsetjmp alloca va_arg va_start va_end va_copy"), SL("complex imaginary unix linux isnan isinf isfinite isnormal signbit fpclassify"), SL("isgreater isgreaterequal isless islessequal islessgreater isunordered math_errhandling"), SL("sigmask sa_handler sa_sigaction minor major makedev be16toh be32toh be64toh htobe16"), SL("htobe32 htobe64 htole16 htole32 htole64 le16toh le32toh le64toh L_ctermid L_tmpnam"), SL("P_tmpdir st_atime st_mtime st_ctime sigev_notify_attributes sigev_notify_function"), }; // Names a function, global or type of a debug build may not take: what the // headers the runtime includes declare at file scope (glibc, from clang's // syntax tree), their lowercase macros, the functions gcc knows as builtins, // and names of BSD, macOS and musl headers. static Str header_names[] = { SL("BUS_ADRALN BUS_ADRERR BUS_MCEERR_AO BUS_MCEERR_AR BUS_OBJERR CLD_CONTINUED CLD_DUMPED"), SL("CLD_EXITED CLD_KILLED CLD_STOPPED CLD_TRAPPED FE_DIVBYZERO FE_DOWNWARD FE_INEXACT"), SL("FE_INVALID FE_OVERFLOW FE_TONEAREST FE_TOWARDZERO FE_UNDERFLOW FE_UPWARD FILE"), SL("FPE_CONDTRAP FPE_FLTDIV FPE_FLTINV FPE_FLTOVF FPE_FLTRES FPE_FLTSUB FPE_FLTUND"), SL("FPE_FLTUNK FPE_INTDIV FPE_INTOVF FP_INFINITE FP_NAN FP_NORMAL FP_SUBNORMAL FP_ZERO"), SL("ILL_BADIADDR ILL_BADSTK ILL_COPROC ILL_ILLADR ILL_ILLOPC ILL_ILLOPN ILL_ILLTRP"), SL("ILL_PRVOPC ILL_PRVREG L_ctermid L_tmpnam POLL_ERR POLL_HUP POLL_IN POLL_MSG POLL_OUT"), SL("POLL_PRI PRId16 PRId32 PRId64 PRId8 PRIdFAST16 PRIdFAST32 PRIdFAST64 PRIdFAST8"), SL("PRIdLEAST16 PRIdLEAST32 PRIdLEAST64 PRIdLEAST8 PRIdMAX PRIdPTR PRIi16 PRIi32 PRIi64"), SL("PRIi8 PRIiFAST16 PRIiFAST32 PRIiFAST64 PRIiFAST8 PRIiLEAST16 PRIiLEAST32 PRIiLEAST64"), SL("PRIiLEAST8 PRIiMAX PRIiPTR PRIo16 PRIo32 PRIo64 PRIo8 PRIoFAST16 PRIoFAST32 PRIoFAST64"), SL("PRIoFAST8 PRIoLEAST16 PRIoLEAST32 PRIoLEAST64 PRIoLEAST8 PRIoMAX PRIoPTR PRIu16 PRIu32"), SL("PRIu64 PRIu8 PRIuFAST16 PRIuFAST32 PRIuFAST64 PRIuFAST8 PRIuLEAST16 PRIuLEAST32"), SL("PRIuLEAST64 PRIuLEAST8 PRIuMAX PRIuPTR PRIx16 PRIx32 PRIx64 PRIx8 PRIxFAST16"), SL("PRIxFAST32 PRIxFAST64 PRIxFAST8 PRIxLEAST16 PRIxLEAST32 PRIxLEAST64 PRIxLEAST8 PRIxMAX"), SL("PRIxPTR P_tmpdir SCNd16 SCNd32 SCNd64 SCNd8 SCNdFAST16 SCNdFAST32 SCNdFAST64 SCNdFAST8"), SL("SCNdLEAST16 SCNdLEAST32 SCNdLEAST64 SCNdLEAST8 SCNdMAX SCNdPTR SCNi16 SCNi32 SCNi64"), SL("SCNi8 SCNiFAST16 SCNiFAST32 SCNiFAST64 SCNiFAST8 SCNiLEAST16 SCNiLEAST32 SCNiLEAST64"), SL("SCNiLEAST8 SCNiMAX SCNiPTR SCNo16 SCNo32 SCNo64 SCNo8 SCNoFAST16 SCNoFAST32 SCNoFAST64"), SL("SCNoFAST8 SCNoLEAST16 SCNoLEAST32 SCNoLEAST64 SCNoLEAST8 SCNoMAX SCNoPTR SCNu16 SCNu32"), SL("SCNu64 SCNu8 SCNuFAST16 SCNuFAST32 SCNuFAST64 SCNuFAST8 SCNuLEAST16 SCNuLEAST32"), SL("SCNuLEAST64 SCNuLEAST8 SCNuMAX SCNuPTR SCNx16 SCNx32 SCNx64 SCNx8 SCNxFAST16"), SL("SCNxFAST32 SCNxFAST64 SCNxFAST8 SCNxLEAST16 SCNxLEAST32 SCNxLEAST64 SCNxLEAST8 SCNxMAX"), SL("SCNxPTR SEGV_ACCADI SEGV_ACCERR SEGV_ADIDERR SEGV_ADIPERR SEGV_BNDERR SEGV_CPERR"), SL("SEGV_MAPERR SEGV_MTEAERR SEGV_MTESERR SEGV_PKUERR SIGEV_NONE SIGEV_SIGNAL SIGEV_THREAD"), SL("SIGEV_THREAD_ID SI_ASYNCIO SI_ASYNCNL SI_DETHREAD SI_KERNEL SI_MESGQ SI_QUEUE SI_SIGIO"), SL("SI_TIMER SI_TKILL SI_USER SS_DISABLE SS_ONSTACK a64l abort abs access acct acos acosf"), SL("acosh acoshf acoshl acosl alarm alloca arc4random arc4random_buf arc4random_uniform"), SL("asctime asctime_r asin asinf asinh asinhf asinhl asinl asprintf atan atan2 atan2f"), SL("atan2l atanf atanh atanhf atanhl atanl atexit atof atoi atol atoll backtrace"), SL("backtrace_symbols backtrace_symbols_fd bcmp bcopy be16toh be32toh be64toh blkcnt_t"), SL("blksize_t bool brk bsearch btowc bzero cabs cabsf cabsl cacos cacosf cacosh cacoshf"), SL("cacoshl cacosl caddr_t calloc carg cargf cargl casin casinf casinh casinhf casinhl"), SL("casinl catan catanf catanh catanhf catanhl catanl cbrt cbrtf cbrtl ccos ccosf ccosh"), SL("ccoshf ccoshl ccosl ceil ceilf ceill cexp cexpf cexpl chdir chown chroot cimag cimagf"), SL("cimagl clearenv clearerr clearerr_unlocked clock clock_getcpuclockid clock_getres"), SL("clock_gettime clock_nanosleep clock_settime clock_t clockid_t clog clogf clogl close"), SL("closefrom complex confstr conj conjf conjl cookie_close_function_t"), SL("cookie_io_functions_t cookie_read_function_t cookie_seek_function_t"), SL("cookie_write_function_t copysign copysignf copysignl cos cosf cosh coshf coshl cosl"), SL("cpow cpowf cpowl cproj cprojf cprojl creal crealf creall crypt csin csinf csinh csinhf"), SL("csinhl csinl csqrt csqrtf csqrtl ctan ctanf ctanh ctanhf ctanhl ctanl ctermid ctime"), SL("ctime_r daddr_t daemon daylight dcgettext dev_t devname dgettext difftime div div_t"), SL("double_t dprintf drand48 drand48_data drand48_r drem dremf dreml dup dup2 duplocale"), SL("dysize ecvt ecvt_r encrypt endusershell environ erand48 erand48_r erf erfc erfcf erfcl"), SL("erff erfl errno execl execle execlp execv execve execvp exit exp exp10 exp2 exp2f"), SL("exp2l expf expl explicit_bzero expm1 expm1f expm1l fabs fabsf fabsl faccessat false"), SL("fchdir fchown fchownat fclose fcvt fcvt_r fd_mask fd_set fdatasync fdim fdimf fdiml"), SL("fdopen feclearexcept fegetenv fegetexceptflag fegetround feholdexcept fenv_t feof"), SL("feof_unlocked feraiseexcept ferror ferror_unlocked fesetenv fesetexceptflag fesetround"), SL("fetestexcept feupdateenv fexcept_t fexecve fflush fflush_unlocked ffs ffsl ffsll fgetc"), SL("fgetc_unlocked fgetln fgetpos fgets fgetwc fgetws fileno fileno_unlocked finite"), SL("finitef finitel float_t flockfile floor floorf floorl fma fmaf fmal fmax fmaxf fmaxl"), SL("fmemopen fmin fminf fminl fmod fmodf fmodl fopen fopencookie fork fpathconf fpclassify"), SL("fpos_t fpregset_t fprintf fpurge fputc fputc_unlocked fputs fputs_unlocked fputwc"), SL("fputws fread fread_unlocked free freelocale freopen frexp frexpf frexpl fsblkcnt_t"), SL("fscanf fseek fseeko fsetpos fsfilcnt_t fsid_t fsync ftell ftello ftruncate"), SL("ftrylockfile funlockfile funopen fwide fwprintf fwrite fwrite_unlocked fwscanf gamma"), SL("gammaf gammal gcvt getc getc_unlocked getchar getchar_unlocked getcwd getdelim"), SL("getdomainname getdtablesize getegid getentropy getenv geteuid getgid getgroups"), SL("gethostid gethostname getline getloadavg getlogin getlogin_r getmode getopt"), SL("getpagesize getpass getpgid getpgrp getpid getppid getprogname gets getsid getsubopt"), SL("gettext getuid getusershell getw getwc getwchar getwd gid_t gmtime gmtime_r greg_t"), SL("gregset_t gsignal heapsort htobe16 htobe32 htobe64 htole16 htole32 htole64 hypot"), SL("hypotf hypotl id_t ilogb ilogbf ilogbl imaxabs imaxdiv imaxdiv_t index initstate"), SL("initstate_r ino_t int16_t int32_t int64_t int8_t int_fast16_t int_fast32_t"), SL("int_fast64_t int_fast8_t int_least16_t int_least32_t int_least64_t int_least8_t"), SL("intmax_t intptr_t isalnum isalnum_l isalpha isalpha_l isascii isascii_l isatty isblank"), SL("isblank_l iscntrl iscntrl_l isdigit isdigit_l isfinite isgraph isgraph_l isgreater"), SL("isgreaterequal isinf isinff isinfl isless islessequal islessgreater islower islower_l"), SL("isnan isnanf isnanl isnormal isprint isprint_l ispunct ispunct_l issetugid isspace"), SL("isspace_l isunordered isupper isupper_l iswalnum iswalnum_l iswalpha iswalpha_l"), SL("iswblank iswblank_l iswcntrl iswcntrl_l iswctype iswctype_l iswdigit iswdigit_l"), SL("iswgraph iswgraph_l iswlower iswlower_l iswprint iswprint_l iswpunct iswpunct_l"), SL("iswspace iswspace_l iswupper iswupper_l iswxdigit iswxdigit_l isxdigit isxdigit_l"), SL("itimerspec j0 j0f j0l j1 j1f j1l jmp_buf jn jnf jnl jrand48 jrand48_r key_t kill"), SL("killpg l64a labs lchown lcong48 lcong48_r lconv ldexp ldexpf ldexpl ldiv ldiv_t"), SL("le16toh le32toh le64toh lgamma lgamma_r lgammaf lgammaf_r lgammal lgammal_r link"), SL("linkat llabs lldiv lldiv_t llrint llrintf llrintl llround llroundf llroundl locale_t"), SL("localeconv localtime localtime_r lockf loff_t log log10 log10f log10l log1p log1pf"), SL("log1pl log2 log2f log2l logb logbf logbl logf logl longjmp lrand48 lrand48_r lrint"), SL("lrintf lrintl lround lroundf lroundl lseek madvise malloc math_errhandling mblen"), SL("mbrlen mbrtowc mbsinit mbsnrtowcs mbsrtowcs mbstate_t mbstowcs mbtowc mcontext_t"), SL("memccpy memchr memcmp memcpy memmem memmove mempcpy memset memset_explicit mergesort"), SL("mincore mkdtemp mkstemp mkstemps mktemp mktime mlock mlockall mmap mode_t modf modff"), SL("modfl mprotect mrand48 mrand48_r msync munlock munlockall munmap nan nanf nanl"), SL("nanosleep nearbyint nearbyintf nearbyintl newlocale nextafter nextafterf nextafterl"), SL("nexttoward nexttowardf nexttowardl nice nlink_t nrand48 nrand48_r off_t offsetof"), SL("on_exit open_memstream open_wmemstream optarg opterr optind optopt pathconf pause"), SL("pclose perror pid_t pipe popen posix_madvise posix_memalign pow pow10 powf powl pread"), SL("printf profil pselect psiginfo psignal pthread_attr_t pthread_barrier_t"), SL("pthread_barrierattr_t pthread_cond_t pthread_condattr_t pthread_key_t pthread_kill"), SL("pthread_mutex_t pthread_mutexattr_t pthread_once_t pthread_rwlock_t"), SL("pthread_rwlockattr_t pthread_sigmask pthread_spinlock_t pthread_t ptrdiff_t putc"), SL("putc_unlocked putchar putchar_unlocked putenv puts putw putwc putwchar pwrite qecvt"), SL("qecvt_r qfcvt qfcvt_r qgcvt qsort qsort_r quad_t radixsort raise rand rand_r random"), SL("random_data random_r read readlink readlinkat realloc reallocarray reallocf realpath"), SL("register_t remainder remainderf remainderl remove remquo remquof remquol rename"), SL("renameat revoke rewind rindex rint rintf rintl rmdir round roundf roundl rpmatch"), SL("sa_handler sa_sigaction sbrk scalb scalbf scalbl scalbln scalblnf scalblnl scalbn"), SL("scalbnf scalbnl scanf seed48 seed48_r select setbuf setbuffer setdomainname setegid"), SL("setenv seteuid setgid sethostid sethostname setjmp setkey setlinebuf setlocale"), SL("setlogin setmode setpgid setpgrp setprogname setregid setreuid setsid setstate"), SL("setstate_r setuid setusershell setvbuf shm_open shm_unlink si_addr si_addr_lsb si_arch"), SL("si_band si_call_addr si_fd si_int si_lower si_overrun si_pid si_pkey si_ptr si_status"), SL("si_stime si_syscall si_timerid si_uid si_upper si_utime si_value sig_atomic_t sig_t"), SL("sigaction sigaddset sigaltstack sigblock sigcontext sigdelset sigemptyset"), SL("sigev_notify_attributes sigev_notify_function sigevent sigevent_t sigfillset"), SL("siggetmask siginfo_t siginterrupt sigismember sigjmp_buf siglongjmp sigmask signal"), SL("signbit signgam significand significandf significandl sigpending sigprocmask sigqueue"), SL("sigreturn sigset_t sigsetjmp sigsetmask sigstack sigsuspend sigtimedwait sigval"), SL("sigval_t sigwait sigwaitinfo sin sincos sinf sinh sinhf sinhl sinl size_t sleep"), SL("snprintf socklen_t sprintf sqrt sqrtf sqrtl srand srand48 srand48_r sranddev srandom"), SL("srandom_r srandomdev sscanf ssignal ssize_t stack_t stderr stderrp stdin stdinp stdout"), SL("stdoutp stpcpy stpncpy strcasecmp strcasecmp_l strcasestr strcat strchr strchrnul"), SL("strcmp strcoll strcoll_l strcpy strcspn strdup strerror strerror_l strerror_r strfmon"), SL("strftime strftime_l strlcat strlcpy strlen strncasecmp strncasecmp_l strncat strncmp"), SL("strncpy strndup strnlen strnstr strpbrk strrchr strsep strsignal strspn strstr strtod"), SL("strtof strtoimax strtok strtok_r strtol strtold strtoll strtonum strtoq strtoul"), SL("strtoull strtoumax strtouq strxfrm strxfrm_l suseconds_t swab swprintf swscanf symlink"), SL("symlinkat sync syscall sysconf system tan tanf tanh tanhf tanhl tanl tcgetpgrp"), SL("tcsetpgrp tempnam tgamma tgammaf tgammal time time_t timegm timelocal timer_create"), SL("timer_delete timer_getoverrun timer_gettime timer_settime timer_t timespec timeval"), SL("timezone tm tmpfile tmpnam tmpnam_r toascii toascii_l tolower tolower_l toupper"), SL("toupper_l towctrans towctrans_l towlower towlower_l towupper towupper_l true trunc"), SL("truncate truncf truncl ttyname ttyname_r ttyslot tzname tzset u_char u_int u_int16_t"), SL("u_int32_t u_int64_t u_int8_t u_long u_quad_t u_short ualarm ucontext_t uid_t uint"), SL("uint16_t uint32_t uint64_t uint8_t uint_fast16_t uint_fast32_t uint_fast64_t"), SL("uint_fast8_t uint_least16_t uint_least32_t uint_least64_t uint_least8_t uintmax_t"), SL("uintptr_t ulong ungetc ungetwc unlink unlinkat unsetenv useconds_t uselocale ushort"), SL("usleep va_list valloc vasprintf vdprintf vfork vfprintf vfscanf vfwprintf vfwscanf"), SL("vhangup vprintf vscanf vsnprintf vsprintf vsscanf vswprintf vswscanf vwprintf vwscanf"), SL("wchar_t wcpcpy wcpncpy wcrtomb wcscasecmp wcscasecmp_l wcscat wcschr wcscmp wcscoll"), SL("wcscoll_l wcscpy wcscspn wcsdup wcsftime wcslcat wcslcpy wcslen wcsncasecmp"), SL("wcsncasecmp_l wcsncat wcsncmp wcsncpy wcsnlen wcsnrtombs wcspbrk wcsrchr wcsrtombs"), SL("wcsspn wcsstr wcstod wcstof wcstoimax wcstok wcstol wcstold wcstoll wcstombs wcstoul"), SL("wcstoull wcstoumax wcsxfrm wcsxfrm_l wctob wctomb wctrans wctrans_l wctrans_t wctype"), SL("wctype_l wctype_t wint_t wmemchr wmemcmp wmemcpy wmemmove wmemset wprintf write wscanf"), SL("y0 y0f y0l y1 y1f y1l yn ynf ynl"), }; // Star99 type names written in capitals that are not macros anywhere static Str capital_types[] = {SL("U8"), SL("U16"), SL("U32"), SL("U64"), SL("I8"), SL("I16"), SL("I32"), SL("I64")}; // The space-separated words of lines, into a set. static void words_into(Map **set, Str *lines, iz n) { for (iz i = 0; i < n; i++) { Str l = lines[i]; for (iz a = 0; a < l.len;) { iz z = a; while (z < l.len && l.data[z] != ' ') z++; *upsert(set, substr(l, a, z), 1) = (void *)1; a = z + 1; } } } // Whether name can be a C name in a debug build: an identifier no keyword, // macro or name of the runtime has. strict also refuses names written in // capitals, which the macros of the system headers have. static b32 debug_name_ok(Str name, b32 strict) { if (!name.len || !isalphac(name.data[0]) || name.data[0] == '_' || name.len > 80) return 0; b32 upper = 1; for (iz i = 0; i < name.len; i++) { u8 c = name.data[i]; if (!isalphac(c) && !isdigitc(c)) return 0; if (c >= 'a' && c <= 'z') upper = 0; } if (upper && strict) return 0; if (upper) { b32 known = 0; for (iz i = 0; i < countof(capital_types); i++) known |= equals(name, capital_types[i]); if (!known) return 0; } if (startswith(name, S("s99_")) || startswith(name, S("S99_")) || startswith(name, S("si_"))) return 0; static STATE Map *words = 0; if (!words) words_into(&words, c_words, countof(c_words)); return !lookup(words, name); } // A Star99 type name as a C identifier: List(Int) is List_Int. static Str mangle(Str s) { Buf b = {0}; for (iz i = 0; i < s.len; i++) { u8 c = s.data[i]; if (isalphac(c) || isdigitc(c)) bputc(&b, c); else if (b.len && b.data[b.len - 1] != '_') bputc(&b, '_'); } while (b.len && b.data[b.len - 1] == '_') b.len--; return bstr(&b); } // The C name of an operator overload: fn +(a, b: V) is op_plus. static Str operator_name(Str op) { static Str words[][2] = { {SL("**"), SL("power")}, {SL("=="), SL("equals")}, {SL("!="), SL("unequal")}, {SL("<="), SL("less_equal")}, {SL(">="), SL("greater_equal")}, {SL("<<"), SL("shift_left")}, {SL(">>"), SL("shift_right")}, {SL("+"), SL("plus")}, {SL("-"), SL("minus")}, {SL("*"), SL("times")}, {SL("/"), SL("divide")}, {SL("%"), SL("mod")}, {SL("<"), SL("less")}, {SL(">"), SL("greater")}, {SL("&"), SL("and")}, {SL("|"), SL("or")}, {SL("^"), SL("xor")}, {SL("!"), SL("not")}, {SL("~"), SL("tilde")}, }; for (iz i = 0; i < countof(words); i++) if (equals(op, words[i][0])) return fmtstr("op_%s", words[i][1]); return fmtstr("op_%s", mangle(cident(op))); } typedef struct { i32 at, len; char kind; // 'i' an identifier, 'm' one after . or ->, one of { } ( ), ' ' the rest } CTok; typedef struct { CTok *data; iz len; iz cap; } CToks; // The names a #define line defines and uses (not its parameters). static void c_define_names(Str d, Map **macros) { iz i = 1; while (i < d.len && d.data[i] == ' ') i++; if (!startswith(substr(d, i, d.len), S("define"))) return; i += 6; Map *params = 0; b32 first = 1, inparams = 0; while (i < d.len) { u8 ch = d.data[i]; if (ch == '"' || ch == '\'') { iz e = i + 1; while (e < d.len && d.data[e] != ch) e += d.data[e] == '\\' ? 2 : 1; i = e + 1; } else if (isalphac(ch)) { iz e = i; while (e < d.len && (isalphac(d.data[e]) || isdigitc(d.data[e]))) e++; Str id = substr(d, i, e); if (inparams) *upsert(¶ms, id, 1) = (void *)1; else if (first || !lookup(params, id)) *upsert(macros, id, 1) = (void *)1; if (first && e < d.len && d.data[e] == '(') inparams = 1, e++; first = 0; i = e; } else if (isdigitc(ch)) { while (i < d.len && (isalphac(d.data[i]) || isdigitc(d.data[i]) || d.data[i] == '.')) i++; } else { if (ch == ')') inparams = 0; i++; } } } // The tokens of C text, without comments and preprocessor lines (whose // #define names go to macros). static CToks c_tokens(Str c, Map **macros) { CToks t = {0}; b32 bol = 1, member = 0; for (iz i = 0; i < c.len;) { u8 ch = c.data[i]; if (ch == '\n') { bol = 1; i++; continue; } if (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\f' || ch == '\v') { i++; continue; } if (bol && ch == '#') { iz s = i; while (i < c.len && c.data[i] != '\n') i += c.data[i] == '\\' && i + 1 < c.len ? 2 : 1; if (macros) c_define_names(substr(c, s, i), macros); continue; } bol = 0; if (ch == '/' && i + 1 < c.len && c.data[i + 1] == '*') { iz e = i + 2; while (e + 1 < c.len && !(c.data[e] == '*' && c.data[e + 1] == '/')) e++; i = e + 2; continue; } if (ch == '/' && i + 1 < c.len && c.data[i + 1] == '/') { while (i < c.len && c.data[i] != '\n') i++; continue; } CTok k = {(i32)i, 1, ' '}; if (ch == '"' || ch == '\'') { iz e = i + 1; while (e < c.len && c.data[e] != ch && c.data[e] != '\n') e += c.data[e] == '\\' ? 2 : 1; i = e + 1; } else if (isalphac(ch)) { iz e = i; while (e < c.len && (isalphac(c.data[e]) || isdigitc(c.data[e]))) e++; k.len = (i32)(e - i); k.kind = member ? 'm' : 'i'; i = e; } else if (isdigitc(ch)) { iz e = i + 1; while (e < c.len && (isalphac(c.data[e]) || isdigitc(c.data[e]) || c.data[e] == '.' || ((c.data[e] == '+' || c.data[e] == '-') && oneof(c.data[e - 1], S("eEpP"))))) e++; i = e; } else { if (ch == '{' || ch == '}' || ch == '(' || ch == ')') k.kind = (char)ch; i++; if (ch == '-' && i < c.len && c.data[i] == '>') i++; } member = ch == '.' || (ch == '-' && c.data[i - 1] == '>'); *push(&perm, &t) = k; } return t; } static Str ctok_text(Str c, CTok k) { return substr(c, k.at, k.at + k.len); } // Whether module m is a std: module (the prelude, std:build, std:code). static b32 library_module(i32 m) { return m >= 0 && m < modules.len && modules.data[m]->isstd; } // Debug builds: what the C names of the program stand for, for the // debugger scripts (debug_info_table writes it into the program). typedef struct { Fn *fn; Str cname; Syms vars; // its parameters and locals that the C has, in order } DebugFn; static STATE struct { DebugFn *data; iz len; iz cap; } dbg_fns = {0}; static STATE Map *dbg_renamed = 0; // old C name -> Str *new static Str dbg_final(Str name) { Str *r = lookup(dbg_renamed, name); return r ? *r : name; } typedef struct { iz start, end; // end is 0 when there is none } Span; // The line "#s99 NAME" from its start to past its newline, if there is one. static Span marker_line(Str c, Str m) { Span r = {0}; for (iz i = 0; i + m.len <= c.len; i++) { if ((i && c.data[i - 1] != '\n') || memcmp(c.data + i, m.data, (size_t)m.len)) continue; if (i + m.len < c.len && c.data[i + m.len] != '\n') continue; r.start = i; r.end = i + m.len < c.len ? i + m.len + 1 : i + m.len; break; } return r; } static Str replaced(Str c, iz start, iz end, Str with) { Buf b = {0}; bput(&b, substr(c, 0, start)); bput(&b, with); bput(&b, substr(c, end, c.len)); return bstr(&b); } static void rename_to(Str from, Str to) { Str *n = new(&perm, 1, Str); *n = to; *upsert(&dbg_renamed, from, 1) = n; } // The root function becomes the C main when nothing else names it: then // break main and start stop in the program's own main. The markers gen_fn // and generate write around it, and the macros S99_MAIN_START, S99_MAIN_END // and S99_MAIN_RETURN in its body, say how. static Str debug_main(Str c) { if (!dbg_root) return c; CToks t = c_tokens(c, 0); iz uses = 0; for (iz k = 0; k < t.len; k++) uses += t.data[k].kind == 'i' && equals(ctok_text(c, t.data[k]), dbg_root->cname); b32 merge = uses == 3; // its prototype, its definition and the call from main Span mm = marker_line(c, S("#s99 main-macros")); if (mm.end) c = replaced(c, mm.start, mm.end, merge ? S("#define S99_MAIN_START s99_debug_start(argc, argv)\n" "#define S99_MAIN_END goto s99_main_end; s99_main_end: " "s99_debug_finish(); return 0\n" "#define S99_MAIN_RETURN goto s99_main_end\n") : S("#define S99_MAIN_START (void)0\n#define S99_MAIN_END (void)0\n" "#define S99_MAIN_RETURN return\n")); Span mh = marker_line(c, S("#s99 main-header")), mb = marker_line(c, S("#s99 main-body")); if (mh.end && mb.end) { Str head = substr(c, mh.end, mb.start); Buf h = {0}; // static void and the name, each after its #line for (iz i = 0; i < head.len;) { iz j = i; while (j < head.len && head.data[j] != '\n') j++; Str l = substr(head, i, j); i = j + 1; if (merge && startswith(l, S("static "))) l = S("int"); else if (merge && startswith(l, dbg_root->cname)) l = S("main(int argc, char **argv)"); bput(&h, l); bputc(&h, '\n'); } c = replaced(c, mh.start, mb.end, bstr(&h)); } Span mp = marker_line(c, S("#s99 main-proto")); if (mp.end) { iz end = mp.end; // and the prototype on the line after it while (merge && end < c.len && c.data[end] != '\n') end++; c = replaced(c, mp.start, merge && end < c.len ? end + 1 : mp.end, S("")); } Span cb = marker_line(c, S("#s99 main-c-begin")), ce = marker_line(c, S("#s99 main-c-end")); if (cb.end && ce.end) { if (merge) { c = replaced(c, cb.start, ce.end, S("")); } else { c = replaced(c, ce.start, ce.end, S("")); c = replaced(c, cb.start, cb.end, S("")); } } if (merge) { *push(&perm, &dbg_fns) = (DebugFn){dbg_root, S("main"), {0}}; rename_to(dbg_root->cname, S("main")); } return c; } // The debugger script (debug_script_lines), as one text. static Str debug_script(void) { return join_lines(debug_script_lines, countof(debug_script_lines)); } static void json_str(Buf *b, Str s) { bputc(b, '"'); for (iz i = 0; i < s.len; i++) { u8 c = s.data[i]; if (c == '"' || c == '\\') bputc(b, '\\'), bputc(b, c); else if (c < 32) bputf(b, "\\u%04x", c); else bputc(b, c); } bputc(b, '"'); } // The Star99 signature of f as written, name(p: T, ...) -> R (result says // whether with -> R): for the debugger table and crash reports. A function // literal is fn (...), in a crash report with where it is written. static Str fn_signature_text(Fn *f, b32 result) { b32 lit = f->decl->kind == N_FNLIT; Buf sig = {0}; bput(&sig, lit ? S("fn ") : f->name); bputc(&sig, '('); for (iz k = 0; k < f->type->fields.len; k++) { Node *pn = k < f->decl->list.len ? f->decl->list.data[k] : 0; if (k) bput(&sig, S(", ")); if (f->type->fields.data[k].isvar) bput(&sig, S("var ")); bput(&sig, f->type->fields.data[k].name); bput(&sig, S(": ")); bput(&sig, pn && pn->kind == N_PARAM && pn->a ? node_source(pn->a) : type_name(f->type->fields.data[k].type)); } bputc(&sig, ')'); if (result && f->type->elem && f->type->elem != t_void) { bput(&sig, S(" -> ")); bput(&sig, type_name(f->type->elem)); } if (!result && lit && f->parent) bputf(&sig, " in %s", f->parent->name); return bstr(&sig); } // The descriptor of t, if the program has one static Str debug_descriptor(Type *t) { return t && (t->helpers & H_INFO) ? fmtstr("info%d", t->id) : S(""); } // A Star99 variable for the table: [C name, Star99 name, how the C holds // it, Star99 type, descriptor, 1 for a parameter]. How: "" the value, // "cell" a pointer to it, "ref" a route (s99_ref), "ptr" a pointer to the // caller's variable. static void json_var(Buf *b, Str cname, Sym *s, Str how) { bputc(b, '['); json_str(b, cname); bputc(b, ','); json_str(b, s->name); bputf(b, ",\"%s\",", how); json_str(b, type_name(s->type)); bputc(b, ','); json_str(b, debug_descriptor(s->type)); bputf(b, ",%d]", s->isparam ? 1 : 0); } // Debug builds: what the C names of the program stand for, for the gdb and // lldb scripts (star99 gdb-script): JSON, in the byte array s99_debug_info. static Str debug_info_table(void) { Buf j = {0}; bput(&j, S("{\"version\":1,\"functions\":[")); for (iz i = 0; i < dbg_fns.len; i++) { DebugFn *d = dbg_fns.data + i; Fn *f = d->fn; if (i) bputc(&j, ','); bput(&j, S("{\"c\":")); json_str(&j, d->cname); b32 lit = f->decl->kind == N_FNLIT; bput(&j, S(",\"name\":")); Fn *named = f->co ? f->co->fn : f; // a resume is its coroutine's body json_str(&j, lit ? S("fn") : named->name); bput(&j, S(",\"in\":")); json_str(&j, lit && f->parent ? f->parent->name : S("")); bput(&j, S(",\"orig\":")); json_str(&j, f->cname); bput(&j, S(",\"env\":")); json_str(&j, f->isclosure && f->captures.len ? env_type(f) : S("")); bput(&j, S(",\"sig\":")); json_str(&j, fn_signature_text(named, 1)); Pos p = source_pos(f->decl->pos); i32 line = p.file >= 0 ? location(p).line : 0; bput(&j, S(",\"file\":")); json_str(&j, p.file >= 0 ? files.data[p.file].path : S("")); bputf(&j, ",\"line\":%d,\"library\":%s,\"vars\":[", line, library_module(f->module) ? S("true") : S("false")); for (iz k = 0; k < d->vars.len; k++) { Sym *s = d->vars.data[k]; if (k) bputc(&j, ','); if (s->isparam && s->pinned && !s->isvar) { // the value it came with, which its cell copies only once the // body runs (and a parameter never changes) json_var(&j, fmtstr("%s_in", s->cname), s, S("")); continue; } Str how = s->isvar ? (s->isparam && f->var_ptr ? S("ptr") : S("ref")) : s->pinned ? S("cell") : S(""); json_var(&j, dbg_final(s->cname), s, how); } bput(&j, S("],\"captures\":[")); for (iz k = 0; f->isclosure && k < f->captures.len; k++) { Sym *s = f->captures.data[k]; if (k) bputc(&j, ','); json_var(&j, cap_member(f, k), s, s->isvar ? S("ref") : S("cell")); } bput(&j, S("]}")); } bput(&j, S("],\"types\":[")); b32 any = 0; for (iz i = 0; i < all_types.len; i++) { Type *t = all_types.data[i]; if (t->cname.len < 2 || t->cname.data[0] != 'T' || !isdigitc(t->cname.data[1])) continue; if (any) bputc(&j, ','); any = 1; bputc(&j, '['); json_str(&j, dbg_final(t->cname)); bputc(&j, ','); json_str(&j, type_name(t)); bputc(&j, ','); json_str(&j, debug_descriptor(t)); bputc(&j, ']'); } bput(&j, S("],\"globals\":[")); any = 0; for (iz i = 0; i < globals.len; i++) { Sym *g = globals.data[i]; if (!g->type || g->ismacro || !g->cname.len) continue; if (any) bputc(&j, ','); any = 1; json_var(&j, dbg_final(g->cname), g, S("")); } bput(&j, S("]}")); Str js = bstr(&j); Buf b = {0}; bput(&b, S("\n/* Debug builds: what the C names stand for, for the debugger scripts */\n")); bput(&b, S("#if defined(__GNUC__) || defined(__clang__)\n__attribute__((used))\n#endif\n")); bputf(&b, "static S99_UNUSED const unsigned char s99_debug_info[%lld] = {", (long long)js.len + 1); for (iz i = 0; i < js.len; i++) bputf(&b, "%s%d", !i ? S("\n") : i % 32 ? S(",") : S(",\n"), js.data[i]); bput(&b, S(",0};\n")); // the descriptors, which the debugger reads where nothing else does // (clang drops what nothing names, even unoptimized) bput(&b, S("#if defined(__GNUC__) || defined(__clang__)\n__attribute__((used))\n#endif\n" "static S99_UNUSED const s99_type *const s99_debug_types[] = {")); for (iz i = 0; i < all_types.len; i++) { Type *t = all_types.data[i]; if (t->helpers & H_INFO) bputf(&b, "&info%d, ", t->id); } bput(&b, S("0};\n")); // and the script, for gdb to run when it loads the program (an inline // script of .debug_gdb_scripts: 4, its name, a newline, the script, 0) Buf sc = {0}; bputc(&sc, 4); bput(&sc, S("gdb.inlined-script\n")); bput(&sc, debug_script()); bputc(&sc, 0); // in pieces of assembly, in order: C99 promises only short strings, and // a C array in that section would be compressed with the debug info bput(&b, S("\n/* The debugger script, which gdb runs where auto-loading is allowed */\n" "#if defined(__ELF__) || (defined(__TINYC__) && (defined(__linux__) || " "defined(__FreeBSD__)))\n")); for (iz i = 0; i < sc.len; i += 400) { bput(&b, S("__asm__(\".pushsection .debug_gdb_scripts\\n.byte ")); for (iz k = i; k < sc.len && k < i + 400; k++) bputf(&b, "%s%d", k == i ? S("") : S(","), sc.data[k]); bput(&b, S("\\n.popsection\\n\");\n")); } bput(&b, S("#endif\n")); return bstr(&b); } typedef struct { i32 first, last; // where it is declared, and the end of its scope i32 block; // the { of the block it is declared in Str name, cname; } DebugScope; // Whether block a is inside block b (and not b). static b32 block_in(i32 *match, i32 a, i32 b) { return a != b && b < a && match[a] <= match[b]; } // Whether name is written in tokens from..to. static b32 written_in(Str c, CToks *t, Str name, i32 from, i32 to) { for (i32 k = from; k <= to; k++) if (t->data[k].kind == 'i' && equals(ctok_text(c, t->data[k]), name)) return 1; return 0; } // What deciding a file-scope name needs: the names the file declares (at // the top level, outside parentheses, and enum constants), the bodies of // functions, and for each name being renamed the bodies that write it. typedef struct { i32 first, last; // tokens } BodyRange; typedef struct { Str c; CToks *t; Map *macros, *taken, *filescope; BodyRange *body; // each function body Map **names; // each body's identifiers, made on first use iz nbodies; Map *refs; // old name -> IdxList * of the bodies that write it } NameCx; typedef struct { i32 *data; iz len; iz cap; } IdxList; // Whether a function, global or type may be called want instead of old: // nothing at file scope has the name, and no body that names old has it // (a local there would hide it). static b32 file_name_free(NameCx *cx, Str want, Str old) { if (lookup(cx->macros, want) || lookup(cx->taken, want) || lookup(cx->filescope, want)) return 0; IdxList *in = lookup(cx->refs, old); for (iz i = 0; in && i < in->len; i++) { i32 b = in->data[i]; if (!cx->names[b]) { *upsert(&cx->names[b], S(" "), 1) = (void *)1; // made, though empty for (i32 k = cx->body[b].first; k <= cx->body[b].last; k++) if (cx->t->data[k].kind == 'i') *upsert(&cx->names[b], ctok_text(cx->c, cx->t->data[k]), 1) = (void *)1; } if (lookup(cx->names[b], want)) return 0; } return 1; } static Str debug_names(Str c) { c = debug_main(c); Map *macros = 0; CToks t = c_tokens(c, ¯os); i32 *match = new(&perm, t.len + 1, i32); i32 *stack = new(&perm, t.len + 1, i32); i32 sp = 0; Map *used = 0; // every identifier written outside comments and directives for (iz k = 0; k < t.len; k++) { match[k] = -1; char kd = t.data[k].kind; if (kd == 'i') *upsert(&used, ctok_text(c, t.data[k]), 1) = (void *)1; // not members if (kd == '{' || kd == '(') stack[sp++] = (i32)k; if ((kd == '}' || kd == ')') && sp) { i32 o = stack[--sp]; match[o] = (i32)k; match[k] = o; } } // the function bodies: { after ) at the top level Map *bodies = 0; // C name -> i32[2]: the ( of its parameters, the } of its body for (iz k = 0; k < t.len; k++) { if (t.data[k].kind != '{') continue; if (k > 0 && t.data[k - 1].kind == ')' && match[k - 1] > 0 && match[k] > k && t.data[match[k - 1] - 1].kind == 'i') { i32 *r = new(&perm, 2, i32); r[0] = match[k - 1]; r[1] = match[k]; *upsert(&bodies, ctok_text(c, t.data[match[k - 1] - 1]), 1) = r; } if (match[k] > k) k = match[k]; // past the body or the initializer } Map *taken = 0; // and what the system headers declare words_into(&taken, header_names, countof(header_names)); CHeaderWords cw = c_header_words(); // and the headers of C plugins words_into(&taken, cw.names.data, cw.names.len); words_into(&taken, cw.macros.data, cw.macros.len); words_into(¯os, cw.macros.data, cw.macros.len); NameCx cx = {c, &t, macros, taken, 0, 0, 0, 0, 0}; // what the file declares at the top level, enum constants too for (iz k = 0, depth = 0, parens = 0, inenum = 0; k < t.len; k++) { char kd = t.data[k].kind; depth += kd == '{' ? 1 : kd == '}' ? -1 : 0; parens += depth == 0 && kd == '(' ? 1 : depth == 0 && kd == ')' ? -1 : 0; if (kd == '{' && depth == 1 && inenum == 1) inenum = 2; if (kd == '}' && depth == 0) inenum = 0; if (kd != 'i') continue; Str id = ctok_text(c, t.data[k]); if (depth == 0 && parens == 0) { *upsert(&cx.filescope, id, 1) = (void *)1; if (equals(id, S("enum"))) inenum = 1; } else if (inenum == 2 && depth == 1) { *upsert(&cx.filescope, id, 1) = (void *)1; } } // every body, and which of them write each name about to be renamed Map *olds = 0; for (iz i = 0; i < all_types.len; i++) if (all_types.data[i]->cname.len) *upsert(&olds, all_types.data[i]->cname, 1) = (void *)1; for (iz i = 0; i < all_fns.len; i++) if (all_fns.data[i]->emitted) *upsert(&olds, all_fns.data[i]->cname, 1) = (void *)1; for (iz i = 0; i < globals.len; i++) if (globals.data[i]->cname.len) *upsert(&olds, globals.data[i]->cname, 1) = (void *)1; cx.body = new(&perm, t.len / 2 + 1, BodyRange); for (iz k = 0; k < t.len; k++) { if (t.data[k].kind != '{' || k == 0 || t.data[k - 1].kind != ')' || match[k] <= k) { if (t.data[k].kind == '{' && match[k] > k) k = match[k]; continue; } i32 b = (i32)cx.nbodies++; cx.body[b].first = match[k - 1]; cx.body[b].last = match[k]; for (i32 j = match[k - 1]; j <= match[k]; j++) { if (t.data[j].kind != 'i') continue; Str id = ctok_text(c, t.data[j]); if (!lookup(olds, id)) continue; IdxList **in = (IdxList **)upsert(&cx.refs, id, 1); if (!*in) *in = new(&perm, 1, IdxList); if (!(*in)->len || (*in)->data[(*in)->len - 1] != b) *push(&perm, *in) = b; } k = match[k]; } cx.names = new(&perm, cx.nbodies + 1, Map *); // file-scope names: types, functions, globals; the program's own first for (int pass = 0; pass < 6; pass++) { b32 library = pass % 2; int what = pass / 2; if (what == 0) { for (iz i = 0; i < all_types.len; i++) { Type *ty = all_types.data[i]; if (!startswith(ty->cname, S("T")) || !lookup(used, ty->cname)) continue; if ((ty->named && library_module(ty->module)) != library) continue; Str want = mangle(type_name(ty)); if (!debug_name_ok(want, 0) || !file_name_free(&cx, want, ty->cname)) want = fmtstr("%s_%d", want, ty->id); // fn (Int) -> Int, fn (Int, Int) if (!debug_name_ok(want, 0) || !file_name_free(&cx, want, ty->cname)) continue; *upsert(&cx.taken, want, 1) = (void *)1; rename_to(ty->cname, want); } } else if (what == 1) { for (iz i = 0; i < all_fns.len; i++) { Fn *f = all_fns.data[i]; if (!f->emitted || f->builtin || f->ismacro || library_module(f->module) != library) continue; if (!lookup(bodies, f->cname) || lookup(dbg_renamed, f->cname)) continue; *push(&perm, &dbg_fns) = (DebugFn){f, f->cname, {0}}; // overloads by their parameter types, closures by where they are iz same = 0; for (iz j = 0; j < all_fns.len; j++) { Fn *g = all_fns.data[j]; same += g->emitted && !g->builtin && equals(g->name, f->name) && g->module == f->module && lookup(bodies, g->cname); } // main where it is not the C main (a build() keeps that one, to fuzz) Str want = equals(f->name, S("main")) ? S("star99_main") : f->name.len && !isalphac(f->name.data[0]) ? operator_name(f->name) : cident(f->name); if (f->co) { // the resume of a coroutine runs its body: named after it want = cident(f->co->fn->name); } else if (f->decl->kind == N_FNLIT) { // a function literal: after the function it is written in Fn *p = f->parent; want = fmtstr("%s_fn", p ? dbg_final(p->cname) : S("closure")); } else if (same > 1) { // the parameter types as written: area(d: Shape.dot) is area_Shape_dot Buf b = {0}; bput(&b, want); for (iz k = 0; k < f->type->fields.len; k++) { Node *pn = k < f->decl->list.len ? f->decl->list.data[k] : 0; bputc(&b, '_'); bput(&b, mangle(pn && pn->kind == N_PARAM && pn->a ? node_source(pn->a) : type_name(f->type->fields.data[k].type))); } want = bstr(&b); } Str base = want; for (int n = 2; n < 100; n++) { if (debug_name_ok(want, 1) && file_name_free(&cx, want, f->cname)) break; want = fmtstr("%s_%d", base, n); } if (!debug_name_ok(want, 1) || !file_name_free(&cx, want, f->cname)) continue; *upsert(&cx.taken, want, 1) = (void *)1; rename_to(f->cname, want); dbg_fns.data[dbg_fns.len - 1].cname = want; } } else { for (iz i = 0; i < globals.len; i++) { Sym *g = globals.data[i]; if (!g->type || g->ismacro || library_module(g->module) != library) continue; if (!lookup(used, g->cname)) continue; Str want = cident(g->name); if (!debug_name_ok(want, 1) || !file_name_free(&cx, want, g->cname)) continue; *upsert(&cx.taken, want, 1) = (void *)1; rename_to(g->cname, want); } } } // struct fields: f_x is x, in every struct that has one Map *runtime = 0; for (iz i = 0; i < countof(runtime_lines); i++) { CToks rt = c_tokens(runtime_lines[i], 0); for (iz k = 0; k < rt.len; k++) if (rt.data[k].kind == 'i') *upsert(&runtime, ctok_text(runtime_lines[i], rt.data[k]), 1) = (void *)1; } for (iz i = 0; i < all_types.len; i++) { Type *ty = all_types.data[i]; if (ty->kind != TY_STRUCT || !lookup(used, ty->cname)) continue; for (iz j = 0; j < ty->fields.len; j++) { Str want = cident(ty->fields.data[j].name); Str have = fmtstr("f_%s", want); if (lookup(dbg_renamed, have) || !lookup(used, have) || lookup(runtime, have)) continue; if (!debug_name_ok(want, 1) || lookup(macros, want)) continue; rename_to(have, want); } } // the parameters and locals of each function, in its own text for (iz i = 0; i < dbg_fns.len; i++) { Fn *f = dbg_fns.data[i].fn; i32 *r = lookup(bodies, dbg_fns.data[i].cname); if (!r) r = lookup(bodies, f->cname); if (!r) continue; Map *names = 0, *first = 0; i32 *inner = new(&perm, r[1] - r[0] + 1, i32); // the innermost { around each token i32 isp = 0; for (i32 k = r[0]; k <= r[1]; k++) { CTok tk = t.data[k]; if (tk.kind == '{') stack[isp++] = k; inner[k - r[0]] = isp ? stack[isp - 1] : -1; if (tk.kind == '}' && isp) isp--; if (tk.kind != 'i') continue; // members have names of their own Str id = ctok_text(c, tk); *upsert(&names, dbg_final(id), 1) = (void *)1; if (!lookup(first, id)) *upsert(&first, id, 1) = (void *)(iz)(k + 1); } DebugScope *scopes = new(&perm, f->params.len + f->locals.len + 1, DebugScope); iz nscopes = 0; i32 top = match[r[0]] + 1; // the { of the body, where the parameters are for (iz j = 0; j < f->params.len + f->locals.len; j++) { Sym *s = j < f->params.len ? f->params.data[j] : f->locals.data[j - f->params.len]; iz at = (iz)lookup(first, s->cname); if (!at && s->isparam && s->pinned && !s->isvar) at = (iz)lookup(first, fmtstr("%s_in", s->cname)); if (at) *push(&perm, &dbg_fns.data[i].vars) = s; if (!at || lookup(dbg_renamed, s->cname)) continue; i32 k = (i32)at - 1; i32 o = inner[k - r[0]]; DebugScope sc = {k, o >= 0 && match[o] > o ? match[o] : r[1], o >= 0 ? o : top, cident(s->name), s->cname}; if (!debug_name_ok(sc.name, 1) || lookup(names, sc.name) || lookup(macros, sc.name)) continue; // the same name in scopes that meet: only one inside the other, a // deeper block, where the outer one is not written (switch s { // case Int: ... } binds s again) b32 clash = 0; for (iz q = 0; q < nscopes && !clash; q++) { DebugScope *o2 = scopes + q; if (!equals(o2->name, sc.name) || o2->last < sc.first || sc.last < o2->first) continue; if (block_in(match, sc.block, o2->block)) clash = written_in(c, &t, o2->cname, sc.first, sc.last); else if (block_in(match, o2->block, sc.block)) clash = written_in(c, &t, sc.cname, o2->first, o2->last); else clash = 1; } if (clash) continue; scopes[nscopes++] = sc; rename_to(s->cname, sc.name); } } // the text again, with the new names, and what they stand for Buf b = {0}; iz at = 0; for (iz k = 0; k < t.len; k++) { if (t.data[k].kind != 'i' && t.data[k].kind != 'm') continue; Str *n = lookup(dbg_renamed, ctok_text(c, t.data[k])); if (!n) continue; bput(&b, substr(c, at, t.data[k].at)); bput(&b, *n); at = t.data[k].at + t.data[k].len; } bput(&b, substr(c, at, c.len)); bput(&b, debug_info_table()); return bstr(&b); } // Whether a descriptor of t would hold an == overload declared in a block, // which descriptors cannot call (a normal build of a program that needs one // fails to compile the same way). static b32 eq_in_block(Type *t, Map **seen) { if (!t) return 0; Str key = fmtstr("%d", t->id); if (!first_time(seen, key)) return 0; Fn *ue = user_equality(t); if (ue && (ue->isclosure || ue->isnested)) return 1; if (eq_in_block(t->elem, seen) || eq_in_block(t->key, seen)) return 1; for (iz i = 0; (t->kind == TY_STRUCT || t->kind == TY_UNION) && i < t->fields.len; i++) if (eq_in_block(t->fields.data[i].type, seen)) return 1; return 0; } // A debug build describes every type its C names, so the debugger // scripts can print any value as Star99 does; not those that would make // it fail where a normal build compiles. static void describe_all(void) { for (b32 more = 1; more;) { more = 0; for (iz i = 0; i < all_types.len; i++) { Type *t = all_types.data[i]; if (t->cname.len < 2 || t->cname.data[0] != 'T' || !isdigitc(t->cname.data[1])) continue; Map *seen = 0; if ((t->helpers & H_INFO) || eq_in_block(t, &seen)) continue; need(t, H_INFO); more = 1; } } } static Str generate(void) { finalize_types(); reset_codegen(); decide_var_ptrs(); // a debug build of a program without build(): its main is the C main dbg_root = debugging() && !root_override && !fuzz_targets_wanted ? root_fn() : 0; if (dbg_root && !line_directive(dbg_root->decl->pos).len) dbg_root = 0; // no .star99 file dbg_fns.len = 0; dbg_renamed = 0; if (meta_mode) { // the metaprogram answers any macro: all of them, and what they use for (iz i = 0; i < all_fns.len; i++) { if (all_fns.data[i]->checked) use_fn(all_fns.data[i]); } } else { // a program is its root (main, or with_main's) and what it reaches use_fn(root_fn()); } gen_pending_fns(); Buf init = {0}; body = &init; indent = 1; genfn = 0; frame = 0; for (iz i = 0; i < globals.len; i++) { Sym *g = globals.data[i]; if (!g->type || (g->ismacro && !meta_mode)) continue; bputf(&out_globals, "static %s %s;\n", ctype(g->type), g->cname); StmtCtx ctx = {0}; open_ctx(&ctx); dbg_at = g->decl->pos; Str v = g->decl->b ? gen_move(g->decl->b) : gen_zero(g->type); line("%s = %s;", g->cname, v); close_ctx(&ctx); dbg_at = (Pos){-1, 0, 0, 0}; } Buf fuzz_table = {0}; if (!meta_mode) fuzz_table = gen_fuzz_targets(); gen_pending_fns(); for (iz i = 0; i < all_fns.len && !debugging(); i++) { // debug builds: after describe_all Fn *f = all_fns.data[i]; if (!f->isclosure && f->escapes && !f->builtin && f->emitted) gen_wrapper(f); } // globals are released when main returns, so leak checkers see no leaks Buf fini = {0}; body = &fini; for (iz i = 0; i < globals.len; i++) { Sym *g = globals.data[i]; if (!g->type || g->ismacro || !g->type->managed) continue; lines(drop_stmt(g->type, g->cname)); } body = &init; Buf dispatch = {0}; if (meta_mode) { // read a macro call, run it, send back its result; repeat Buf *save = body; body = &dispatch; indent = 2; Type *site = code_type(S("Site")); need(site, H_INFO); for (iz k = 0; k < macro_texts.len; k++) { Fn *f = macro_texts.data[k]; if (!f->checked) continue; line("case %lld: {", (long long)k); indent++; Fields *ps = &f->type->fields; Buf args = {0}; for (iz i = 0; i < ps->len; i++) { Type *pt = ps->data[i].type; need(pt, H_INFO); line("%s a%lld;", ctype(pt), (long long)i); line("s99_dec(&s99_requests, &info%d, &a%lld);", pt->id, (long long)i); bputf(&args, "%sa%lld", i ? S(", ") : S(""), (long long)i); } Type *rt = f->type->elem; if (rt == t_void) { line("%s(%s);", f->cname, bstr(&args)); line("s99_put(&s99_replies, \"Ru0;\", 4);"); } else { need(rt, H_INFO); line("%s r = %s(%s);", ctype(rt), f->cname, bstr(&args)); line("s99_put(&s99_replies, \"R\", 1);"); line("s99_enc(&s99_replies, &info%d, &r);", rt->id); } line("break;"); indent--; line("}"); } body = save; } if (debugging()) describe_all(); gen_descriptors(); // functions the last descriptors use (an == hook) while (pending_fns.len) { gen_pending_fns(); if (debugging()) describe_all(); gen_descriptors(); } // a debug build describes every type, which can bring in print and == // hooks, and the function values those use need their wrappers for (iz i = 0; i < all_fns.len && debugging(); i++) { Fn *f = all_fns.data[i]; if (!f->isclosure && f->escapes && !f->builtin && f->emitted) gen_wrapper(f); } Str cplugins = c_section(); // the C of plugins Buf out = {0}; bput(&out, S("/* Generated by star99 from ")); bput(&out, files.data[main_module->fileids.data[0]].path); bput(&out, meta_mode ? S(": the metaprogram. */\n#define _POSIX_C_SOURCE 200809L\n") : S(". */\n")); bput(&out, c_summary); iz runtime_at = out.len; // the runtime goes here once the rest says what it needs for (iz k = 0; debugging() && k < countof(debug_runtime_lines); k++) { bput(&out, debug_runtime_lines[k]); // after the runtime bputc(&out, '\n'); } bput(&out, cplugins); // after the runtime, which it may use bput(&out, S("\n/* Types */\n")); bput(&out, bstr(&out_types)); if (meta_mode) bputf(&out, "static %s s99_here;\n", ctype(code_type(S("Site")))); bput(&out, S("\n/* Declarations */\n")); if (dbg_root) bput(&out, S("#s99 main-macros\nstatic void s99_debug_start(int argc, char **argv);\n" "static void s99_debug_finish(void);\n")); bput(&out, bstr(&out_protos)); bput(&out, bstr(&out_globals)); bput(&out, S("\n/* Helpers */\n")); bput(&out, bstr(&out_helpers)); bput(&out, S("\n/* Functions */\n")); bput(&out, bstr(&out_fns)); if (out_fuzz.len && !dbg_root) { bput(&out, S("\n/* Fuzz targets */\n#ifdef STAR99_FUZZ\n")); bput(&out, bstr(&out_fuzz)); bput(&out, S("#endif\n")); } if (dbg_root) { // a debug build: no fuzzing, and main starts and finishes the runtime // around the root function, or debug_main moves its body in here bput(&out, S("\nstatic void\ns99_debug_start(int argc, char **argv)\n{\n" " s99_init(argc, argv);\n s99_dbg_start();\n")); bput(&out, bstr(&init)); bput(&out, S("}\n\nstatic void\ns99_debug_finish(void)\n{\n")); bput(&out, bstr(&fini)); bput(&out, S(" fflush(stdout);\n}\n#s99 main-c-begin\n\nint\nmain(int argc, char **argv)\n{\n" " s99_debug_start(argc, argv);\n")); bputf(&out, " %s();\n", dbg_root->cname); bput(&out, S(" s99_debug_finish();\n return 0;\n}\n#s99 main-c-end\n")); return debug_names(with_runtime(bstr(&out), runtime_at)); } if (!meta_mode) { bput( &out, S("\n#ifdef STAR99_FUZZ\n" "#include \n" "#ifdef __clang__ /* the AFL++ macros are not pedantic C */\n" "#pragma clang diagnostic ignored \"-Wpedantic\"\n" "#pragma clang diagnostic ignored \"-Wextra-semi\"\n" "#pragma clang diagnostic ignored \"-Wgnu-statement-expression-from-macro-expansion\"\n" "#endif\n" "#ifndef __AFL_FUZZ_TESTCASE_LEN\n" "/* without AFL++: one input from standard input */\n" "static unsigned char s99_fuzz_in[1 << 20];\n" "static int s99_fuzz_runs;\n" "#define __AFL_FUZZ_INIT() extern int s99_no_afl\n" "#define __AFL_INIT()\n" "#define __AFL_FUZZ_TESTCASE_BUF s99_fuzz_in\n" "#define __AFL_FUZZ_TESTCASE_LEN (int64_t)fread(s99_fuzz_in, 1, sizeof(s99_fuzz_in), stdin)\n" "#define __AFL_LOOP(n) (!s99_fuzz_runs++)\n" "#endif\n" "__AFL_FUZZ_INIT();\n" "#endif\n")); } bput(&out, S("\nint\nmain(int argc, char **argv)\n{\n s99_init(argc, argv);\n")); if (debugging()) bput(&out, S(" s99_dbg_start();\n")); bput(&out, bstr(&init)); if (meta_mode) { Type *site = code_type(S("Site")); bput( &out, S(" s99_requests.f = fdopen(3, \"rb\");\n s99_replies.f = s99_meta_fails = fdopen(4, \"wb\");\n" " if (!s99_requests.f || !s99_replies.f) return 1;\n for (;;) {\n" " int64_t k = s99_dec_tag(&s99_requests, 'c');\n")); bputf(&out, " s99_dec(&s99_requests, &info%d, &s99_here);\n switch (k) {\n", site->id); bput(&out, bstr(&dispatch)); bput( &out, S(" default: return 1;\n }\n fflush(stdout);\n fflush(s99_replies.f);\n }\n}\n")); } else { Fn *mainfn = root_fn(); // with -DSTAR99_FUZZ, main runs one target once per input: // STAR99_TARGET names it (main by default; "list" lists them), and // STAR99_SHOW prints each call as Star99. Globals start fresh. bput(&out, S("#ifdef STAR99_FUZZ\n")); bput(&out, bstr(&fuzz_table)); bput( &out, S(" const char *want = getenv(\"STAR99_TARGET\");\n" " if (!want || !*want) want = \"main\";\n" " volatile int target = -1;\n" " for (int k = 0; k < (int)(sizeof(s99_target_names) / sizeof(*s99_target_names)); k++) {\n" " if (!strcmp(want, \"list\")) printf(\"%s\\n\", s99_target_names[k]);\n" " else if (!strcmp(want, s99_target_names[k])) target = k;\n" " }\n" " if (!strcmp(want, \"list\")) return 0;\n" " if (target < 0) {\n" " fprintf(stderr, \"star99: no fuzz target %s; STAR99_TARGET=list lists them\\n\", want);\n" " return 2;\n }\n" " bool show = getenv(\"STAR99_SHOW\") != 0;\n" " __AFL_INIT();\n" " unsigned char *s99_fuzz_buf = __AFL_FUZZ_TESTCASE_BUF;\n" " while (__AFL_LOOP(1000)) {\n")); if (debugging()) // exit() leaves an input with longjmp, past the pops bput(&out, S(" s99_dbg_top = 0;\n")); bput( &out, S(" s99_fuzz_input = s99_bytes(s99_fuzz_buf, __AFL_FUZZ_TESTCASE_LEN);\n" " s99_draw d = {s99_str_data(s99_fuzz_input), s99_str_len(s99_fuzz_input), 0, 0, 0};\n" " if (s99_draw_byte(&d) % 4 == 3) d.swarm = s99_draw_bits(&d, 8); /* swarm testing */\n" " else d.i = 0;\n" " if (!setjmp(s99_fuzz_exit)) {\n")); bputf(&out, " if (target == 0) %s();\n", mainfn->cname); bput(&out, S(" else s99_target_runs[target](&d, show);\n }\n")); if (debugging()) bput(&out, S(" s99_dbg_top = 0;\n")); bput(&out, bstr(&fini)); bput(&out, bstr(&init)); bput(&out, S(" s99_list_free(s99_fuzz_input);\n }\n return 0;\n#endif\n")); // through a pointer the C compiler cannot follow: a function it sees // called once from main, and all it calls, it would compile for size bputf(&out, " static void (*volatile s99_program)(void) = %s;\n s99_program();\n", mainfn->cname); bput(&out, bstr(&fini)); bput(&out, S(" fflush(stdout);\n return 0;\n}\n")); } Str c = with_runtime(bstr(&out), runtime_at); return debugging() ? debug_names(c) : c; } // Macros static Str format_file(Str path, Str text); static Str expanded_text(void); static Str program_dir(void); static void drop_metaprograms(Str dir, Str keep); static void made(Str path); static Str debug_lines(Str c, Str cpath); static Str shell_quote(Str s); // The metaprogram runs as a child process for the whole compilation, so // macro calls share its state, in source order. Calls go to it on fd 3 and // answers come back on fd 4; its standard output is our standard error. static STATE i32 meta_pid = 0; static STATE i32 meta_req = 0, meta_rep = 0; // our ends of the two pipes static STATE struct { u8 buf[1 << 12]; iz len, at; } meta_in = {0}; // what was read from meta_rep, and how much of it was taken static STATE b32 meta_building = 0; static STATE iz meta_count = 0; // macro functions in the running metaprogram static STATE Strs meta_args = {0}; // what follows -- on the command line static STATE Pos macro_site = {0}; // the call being expanded, for notes static STATE Scope *macro_scope = 0; // the scope of that call static STATE Node *macro_call = 0; // and the call: its checked arguments answer type_at static b32 is_code_type(Type *t) { if (!code_module || !t || !t->named) return 0; return t->module == code_module->id; } static Sym * code_sym(Str name) { if (!code_module) return 0; return scope_get(code_module->scope, name); } static void need_code_module(Pos at) { if (code_module) return; Node imp = {0}; imp.kind = N_IMPORT; imp.sval = S("std:code"); imp.pos = at; code_module = load_module(S("std:code"), at, &imp, 1, S("std:code")); } static Node * name_node(Sym *sym, Pos p) { Node *n = mknode(N_NAME, p); n->name = sym->name; n->sym = sym; return n; } static Node * typed_node(Type *t, Pos p) { Node *n = mknode(N_TNAME, p); n->type = t; // a type node already resolved return n; } // A wrapper per macro that runs it and turns its result into source text, // which is what crosses back to the compiler. static Fn * macro_text_fn(Fn *m) { if (is_compile_hook(m)) return c_hook_text_fn(m); fn_signature(m); Type *rt = m->type->elem; if (rt != t_void && !is_code_type(rt)) errorat(m->decl->pos, "a macro returns code (Expr, Stmt, Decl, TypeExpr or Code), not %s", type_name(rt)); Pos p = m->decl->pos; Node *d = mknode(N_FN, p); d->name = fmtstr("%s__text", m->name); d->flags = F_MACRO; d->a = typed_node(t_string, p); Node *call = mknode(N_CALL, p); Sym *msym = mksym(S_FN, m->name, p); *push(&perm, &msym->fns) = m; call->a = name_node(msym, p); for (iz i = 0; i < m->decl->list.len; i++) { Node *pn = m->decl->list.data[i]; Node *np = mknode(N_PARAM, pn->pos); np->name = pn->name; np->a = typed_node(m->type->fields.data[i].type, pn->pos); addnode(&d->list, np); Node *arg = mknode(N_NAME, pn->pos); arg->name = pn->name; addnode(&call->list, arg); } if (rt == t_void) { Node *blk = mknode(N_BLOCK, p); Node *st = mknode(N_EXPR, p); st->a = call; addnode(&blk->list, st); Node *empty = mknode(N_STR, p); Node *st2 = mknode(N_EXPR, p); st2->a = empty; addnode(&blk->list, st2); d->b = blk; } else { Node *src = mknode(N_CALL, p); // the code, printed as Star99, with the marks of the names module // templates wrote src->a = name_node(code_sym(S("marked_text")), p); addnode(&src->list, call); Node *blk = mknode(N_BLOCK, p); Node *st = mknode(N_EXPR, p); st->a = src; addnode(&blk->list, st); d->b = blk; } Fn *f = new_fn(d, m->scope, d->name); f->module = m->module; f->ismacro = 1; return f; } static STATE Map *sent_sites = 0; // file:line:col of each Site sent to a macro static void put_str(Buf *b, Str s) { bputf(b, "s%lld:", (long long)s.len); bput(b, s); } static void put_site(Buf *b, Pos p) { if (p.file < 0 || p.file >= files.len) { bput(b, S("s0:i0;i0;")); return; } Location at = location(p); Str path = files.data[p.file].path; put_str(b, path); bputf(b, "i%d;i%d;", at.line, at.col); // a Site coming back from a macro underlines what this one spans Pos *keep = new(&perm, 1, Pos); *keep = p; *upsert(&sent_sites, fmtstr("%s:%d:%d", path, at.line, at.col), 1) = keep; } // The functions code calls, reached from compile-time code: marked so the // metaprogram checks them. Says whether anything new was marked. static b32 mark_compile_time_calls(Node *n) { if (!n) return 0; b32 marked = 0; if ((int)n->kind == X_CALLFN) { Call *c = n->info; if (c->kind == CALL_FN && c->fn && !c->fn->ctused) c->fn->ctused = marked = 1; for (iz v = 0; c->kind == CALL_DISPATCH && c->cases && v < c->dispatch_union->fields.len; v++) if (c->cases[v] && !c->cases[v]->ctused) c->cases[v]->ctused = marked = 1; for (iz i = 0; i < c->nargs; i++) marked |= mark_compile_time_calls(c->args[i]); return marked; } if (n->kind == N_FNLIT && n->info) { // a closure made there runs there too Fn *f = n->info; if (!f->ctused) f->ctused = marked = 1; } marked |= mark_compile_time_calls(n->a); marked |= mark_compile_time_calls(n->b); if (n->kind != N_FOR) marked |= mark_compile_time_calls(n->c); for (iz i = 0; i < n->list.len; i++) marked |= mark_compile_time_calls(n->list.data[i]); return marked; } static void start_metaprogram(Pos at) { prelude_part(1); // its macros are in every metaprogram, used or not if (meta_pid && meta_count == macro_fns.len) return; if (meta_pid) errorat(at, "a macro generated another macro; macros must be written in the source"); if (meta_building) errorat(at, "this macro is needed to build the macros themselves"); meta_building = 1; need_code_module(at); for (iz i = macro_texts.len; i < macro_fns.len; i++) { Fn *m = macro_fns.data[i]; check_fn(m); Fn *w = macro_text_fn(m); check_fn(w); *push(&perm, ¯o_texts) = w; } // and every function the macros call, until nothing new is reached for (b32 more = 1; more;) { more = 0; for (iz i = 0; i < all_fns.len; i++) { Fn *f = all_fns.data[i]; if (f->ctused && f->checking && !f->ismacro) errorat(f->decl->pos, "%s calls a macro whose compile-time code calls %s", f->name, f->name); // only what compile-time code calls: checking another function // here would check it out of source order b32 library = library_module(f->module); b32 needed = f->ctused || (f->used && (f->special_done || library)); // print hooks too if (needed && !f->checked && !f->checking && !f->builtin && !f->isnested && !f->isclosure) { check_fn(f); more = 1; } if (needed && f->checked && !f->ct_walked && f->decl) { f->ct_walked = 1; // and what it calls is compile-time code too more |= mark_compile_time_calls(f->decl->b); } } } pin_escaping_captures(); meta_mode = 1; Str c = generate(); meta_mode = 0; reset_codegen(); #ifdef FUZZ // the fuzz build checks and generates the metaprogram but never runs it (void)c; meta_pid = -1; meta_count = macro_fns.len; meta_building = 0; return; #endif // in the program's folder; one that changed replaces the older one Str dir = program_dir(); Str exe = join_path(dir, fmtstr("meta-%016llx", (unsigned long long)hash64(c, 99))); made(exe); made(concat(exe, S(".c"))); if (!os_runnable(exe)) { drop_metaprograms(dir, basename_of(exe)); Str cpath = concat(exe, S(".c")); if (!os_writefile(cpath, c)) fatal("cannot write %s", cpath); // quickly: tcc if there is one, else the C compiler without optimizing Str cc = os_getenv(S("STAR99_CC")); Str q = shell_quote(cpath), qe = shell_quote(exe); Str cmd = fmtstr("tcc %s -o %s -lm || %s -std=c99 -O0 -w %s -o %s -lm", q, qe, cc.len ? cc : S("cc"), q, qe); Str quiet = fmtstr("(%s) 2>/dev/null", cmd); if (os_system(quiet) && os_system(cmd)) // again, to show why fatal("building the metaprogram failed: %s", quiet); } // names made by fresh() start past every name__N in the sources long long most = 0; // place() strips only the suffixes past it for (iz f = 0; f < files.len; f++) { Toks tk = lex((i32)f); for (iz k = 0; k < tk.len; k++) { Str t = tk.data[k].text; if (tk.data[k].kind != T_NAME && tk.data[k].kind != T_TYPE) continue; iz d = t.len; while (d > 0 && t.data[d - 1] >= '0' && t.data[d - 1] <= '9') d--; if (d == t.len || d < 2 || t.data[d - 1] != '_' || t.data[d - 2] != '_' || t.len - d > 9) continue; long long v = parse_int(substr(t, d, t.len)); if (v > most) most = v; } } fresh_base = most; Strs args = {0}; *push(&perm, &args) = exe; for (iz i = 0; i < meta_args.len; i++) *push(&perm, &args) = meta_args.data[i]; Piped p = os_start_piped(args, S("STAR99_FRESH"), fmtstr("%lld", fresh_base)); if (p.pid <= 0) fatal("cannot start the metaprogram"); meta_req = p.req; meta_rep = p.rep; meta_in.len = meta_in.at = 0; meta_pid = p.pid; meta_count = macro_fns.len; meta_building = 0; } static void stop_metaprogram(void) { if (meta_pid <= 0) return; os_close(meta_req); os_close(meta_rep); os_wait(meta_pid); meta_pid = 0; } NORETURN static void meta_died(void) { notes.len = 0; errorat(macro_site, "this macro call failed while compiling"); } static int rd(void) { if (meta_in.at == meta_in.len) { meta_in.at = 0; meta_in.len = os_read(meta_rep, meta_in.buf, countof(meta_in.buf)); if (meta_in.len <= 0) { meta_in.len = 0; meta_died(); } } return meta_in.buf[meta_in.at++]; } static i64 rd_num(void) { u8 tmp[64] = {0}; iz n = 0; for (int c = rd(); c != ';' && c != ':'; c = rd()) { if (n < countof(tmp)) tmp[n++] = (u8)c; } return parse_int((Str){tmp, n}); } static Str rd_str(void) { if (rd() != 's') meta_died(); i64 n = rd_num(); Buf b = {0}; for (i64 i = 0; i < n; i++) bputc(&b, rd()); return copystr(&perm, bstr(&b)); } static Pos site_pos(Str file, i64 line_, i64 col) { Pos *sent = lookup(sent_sites, fmtstr("%s:%lld:%lld", file, (long long)line_, (long long)col)); if (sent) return *sent; for (iz f = 0; f < files.len; f++) { SourceFile *sf = files.data + f; if (!equals(sf->path, file)) continue; index_lines(sf); // where the line starts; past the last line, the end of the text iz off = line_ <= 1 ? 0 : line_ - 1 < sf->lines.len ? sf->lines.data[line_ - 1] : sf->text.len; // a column past the text (a Site a macro made up) is its end iz room = sf->text.len - off; iz c = col < 1 ? 0 : col - 1 < room ? col - 1 : room; return (Pos){(i32)f, (i32)(off + c), 0, 0}; } return macro_site; } // "file:line:col", as loc() writes it, back to a position (or where the // macro was called) static Pos loc_pos(Str w) { iz c2 = w.len; while (c2 > 0 && w.data[c2 - 1] != ':') c2--; iz c1 = c2 > 1 ? c2 - 1 : 0; while (c1 > 0 && w.data[c1 - 1] != ':') c1--; if (c1 <= 1) return macro_site; return site_pos(substr(w, 0, c1 - 1), parse_int(substr(w, c1, c2 - 1)), parse_int(substr(w, c2, w.len))); } static Pos rd_site(void) { Str file = rd_str(); if (rd() != 'i') meta_died(); i64 line_ = rd_num(); if (rd() != 'i') meta_died(); return site_pos(file, line_, rd_num()); } static void place_code(Str target, Str text, Pos site); static void enc_stmt(Buf *b, Node *n); static void enc_type(Buf *b, Node *n); static Node *parse_text(Str text, int position, Pos site, Str macro); // The tags of std:code values, in the variant order of its unions enum { EX_NAME = 1, EX_LITERAL, EX_CALL, EX_FIELD, EX_BINARY, EX_UNARY, EX_INDEX, EX_DEREF, EX_ADDR, EX_LIST, EX_MAP, EX_ANON, EX_VARIANT, EX_TYPE, EX_BLOCK, EX_IF, EX_SWITCH, EX_CLOSURE, EX_TEMPLATE, EX_SPLICE }; enum { TX_NAME = 1, TX_FN, TX_STRUCT, TX_UNION, TX_CALL, TX_SIZE }; enum { DX_FUNCTION = 1, DX_STRUCT, DX_UNION, DX_GLOBAL, DX_IMPORT, DX_GROUP, DX_MLABEL, DX_MODULE, DX_COLLECTION }; enum { SX_FOR = 1, SX_RETURN, SX_ASSIGN, SX_EXPR, SX_DECL, SX_IF, SX_WHILE, SX_BLOCK, SX_BREAK, SX_CONTINUE, SX_DEFER, SX_DISCARD, SX_LABELED, SX_SWITCH, SX_CASE, SX_STATEMENTS }; // Program knowledge: questions compile-time code asks (std:code), answered // from what the compiler knows, never by checking code a macro would run. // A Def is a declaration of the program, numbered when first named. static STATE Nodes def_table = {0}; static STATE Map *def_ids = 0; // decl node -> 1 + its place in def_table static iz def_id(Node *decl) { void **slot = upsert(&def_ids, ptrkey(decl), 1); if (!*slot) { addnode(&def_table, decl); *slot = (void *)def_table.len; } return (iz)*slot; } // def_handle(id): the std:code function that asks for a Def by number static Node * def_handle_node(Node *decl, Pos at) { Node *call = call_node(name_node(code_sym(S("def_handle")), at), at); Node *id = mknode(N_INT, at); id->ival = def_id(decl); addnode(&call->list, id); return call; } // DefKind: function, macro_function, global, constant, struct_type, union_type static int def_kind(Node *d) { switch ((int)d->kind) { case N_FN: return d->flags & F_MACRO ? 2 : 1; case N_VAR: return d->flags & F_CONST ? 4 : 3; case N_STRUCT: return 5; default: return 6; } } static void enc_def(Buf *b, Node *d) { bputf(b, "i%lld;", (long long)def_id(d)); put_str(b, d->name); bputf(b, "u%d;", def_kind(d)); put_site(b, d->pos); } // The declarations of the program's own modules that exist now, in order static void program_defs(Nodes *list, Nodes *out) { for (iz i = 0; i < list->len; i++) { Node *d = list->data[i]; if (d->kind == N_DECLS || d->kind == N_MLABEL) { program_defs(d->kind == N_DECLS ? &d->list : &d->a->list, out); continue; } if (d->kind == N_FN || ((d->kind == N_VAR || d->kind == N_STRUCT || d->kind == N_UNION) && !(d->flags & F_MACRO))) addnode(out, d); } } static Nodes all_program_defs(void) { Nodes out = {0}; for (iz mi = 0; mi < modules.len; mi++) { if (library_module((i32)mi)) continue; program_defs(&modules.data[mi]->decls, &out); } return out; } // A Def as encoded by the metaprogram: by its number, or (from code a // macro was given, def_of) by its site static Node * rd_def(Pos at) { if (rd() != 'i') meta_died(); i64 id = rd_num(); Str name = rd_str(); if (rd() != 'u') meta_died(); (void)rd_num(); Pos site = rd_site(); if (id > 0 && id <= def_table.len) return def_table.data[id - 1]; Nodes all = all_program_defs(); for (iz i = 0; i < all.len; i++) if (all.data[i]->pos.file == site.file && all.data[i]->pos.offset == site.offset) return all.data[i]; // the code a macro was given and returned: declared from the text it // returned, made by the call around that site for (iz i = 0; i < all.len; i++) { Node *d = all.data[i]; if (d->pos.file < 0 || !files.data[d->pos.file].generated || !equals(d->name, name)) continue; Pos o = files.data[d->pos.file].origin; i32 lo = o.hi > o.lo ? o.lo : o.offset, hi = o.hi > o.lo ? o.hi : o.offset; if (o.file == site.file && lo <= site.offset && site.offset <= hi) return d; } stop_metaprogram(); errorat(at, "this asks about a declaration that does not exist here"); } // A type as TypeExpr: a TypeName for named ones (with their arguments), a // FunctionType, StructType or UnionType for the others static void enc_type_value(Buf *b, Type *t, Pos site) { Str gen = t->kind == TY_LIST || t->kind == TY_ARRAY ? S("List") : t->kind == TY_MAP ? S("Map") : t->kind == TY_POOL ? S("Pool") : t->kind == TY_REF ? S("Ref") : (Str){0}; b32 named = gen.len || t->named || t->text || (t->kind != TY_FN && t->kind != TY_STRUCT && t->kind != TY_UNION); if (!named && t->kind == TY_FN) { bputf(b, "u%d;l%lld;", TX_FN, (long long)t->fields.len); for (iz i = 0; i < t->fields.len; i++) enc_type_value(b, t->fields.data[i].type, site); if (t->elem && t->elem != t_void) enc_type_value(b, t->elem, site); else bput(b, S("u0;")); put_site(b, site); return; } if (!named) { // struct (...) or union (...) bputf(b, "u%d;l%lld;", t->kind == TY_STRUCT ? TX_STRUCT : TX_UNION, (long long)t->fields.len); for (iz i = 0; i < t->fields.len; i++) { Field *f = &t->fields.data[i]; put_str(b, f->name); enc_type_value(b, f->type, site); bput(b, S("u0;b0")); put_site(b, site); bput(b, S("b0")); } put_site(b, site); if (t->kind == TY_UNION) bputf(b, "b%d", t->nonil != 0); return; } bputf(b, "u%d;", TX_NAME); put_str(b, gen.len ? gen : type_name(t)); if (t->kind == TY_MAP) { bput(b, S("l2;")); enc_type_value(b, t->key, site); enc_type_value(b, t->elem, site); } else if (t->kind == TY_ARRAY) { bput(b, S("l2;")); enc_type_value(b, t->elem, site); bputf(b, "u%d;u%d;", TX_SIZE, EX_LITERAL); put_str(b, fmtstr("%lld", (long long)t->count)); put_site(b, site); put_site(b, site); } else if (gen.len) { bput(b, S("l1;")); enc_type_value(b, t->elem, site); } else { bput(b, S("l0;")); } put_site(b, site); } static STATE b32 asking_softly = 0; // inside known(...): not final answers nil static STATE jmp_buf not_final = {0}; // The body of f, checked: now if it was not yet, as a detour would (the // same rules: no macro may run, none may be pending before it) static void need_checked(Fn *f, Pos at) { if (f->checked || f->builtin) return; if (asking_softly) longjmp(not_final, 1); // known(...) never checks code if (f->checking) { stop_metaprogram(); help_text = S("ask after it, or in build(b)"); errorat( at, "this needs the body of %s, which is around this macro call and not checked to its end", f->name); } if (!in_order) { check_fn(f); return; } b32 save_q = question_detour; question_detour = 1; early_check_allowed(f, at); Fn *save = detour_fn; Pos save_at = detour_at; detour_fn = f; detour_at = at; check_fn(f); detour_fn = save; detour_at = save_at; question_detour = save_q; } static void add_fn_once(Fns *out, Fn *f) { for (iz i = 0; i < out->len; i++) if (out->data[i] == f) return; *push(&perm, out) = f; } // Whether a value of type t may hold a value of type target (Any may hold // anything) static b32 may_hold(Type *t, Type *target) { // the types reachable inside t, each once (types may be recursive) Type *stack[256] = {0}, *seen[512] = {0}; int sp = 0, ns = 0; if (t) stack[sp++] = t; while (sp) { Type *u = stack[--sp]; if (u == target || u->kind == TY_ANY) return 1; b32 done = 0; for (int k = 0; k < ns && !done; k++) done = seen[k] == u; if (done) continue; if (ns == 512) return 1; // too many to follow: assume it may seen[ns++] = u; Type *parts[2] = {0, 0}; switch (u->kind) { case TY_STRUCT: case TY_UNION: for (iz i = 0; i < u->fields.len; i++) { if (sp == 256) return 1; if (u->fields.data[i].type) stack[sp++] = u->fields.data[i].type; } break; case TY_LIST: case TY_ARRAY: case TY_POOL: case TY_REF: parts[0] = u->elem; break; case TY_MAP: parts[0] = u->key; parts[1] = u->elem; break; default: break; } for (int k = 0; k < 2; k++) { if (!parts[k]) continue; if (sp == 256) return 1; stack[sp++] = parts[k]; } } return 0; } // The print and == overloads the runtime may run for a value of type t, // as calls: printing it, or comparing it (directly or inside lists, maps, // structs and unions) static void add_hooks(Fns *calls, Type *t, b32 printing) { if (!t) return; for (iz i = 0; i < all_fns.len; i++) { Fn *h = all_fns.data[i]; if (h->builtin || h->ismacro || !h->decl || !h->type || h->decl->kind != N_FN) continue; Fields *ps = &h->type->fields; b32 hook = printing ? equals(h->name, S("print")) && ps->len == 1 && h->type->elem == t_void : equals(h->name, S("==")) && ps->len == 2; if (hook && may_hold(t, ps->data[0].type)) add_fn_once(calls, h); } } // What a checked body calls: functions (dispatch cases, and the print and // == overloads the runtime runs for it), the sites of calls through // function values, and the functions whose values it takes (closures it // makes too). The body of a closure counts as part of the function that // makes it. static void body_calls(Node *n, Fns *calls, Nodes *by_value, Fns *taken) { if (!n) return; if ((int)n->kind == X_CALLFN) { Call *c = n->info; if (c->kind == CALL_FN && c->fn && !c->fn->builtin && c->fn->decl) add_fn_once(calls, c->fn); for (iz v = 0; c->kind == CALL_DISPATCH && c->cases && v < c->dispatch_union->fields.len; v++) if (c->cases[v] && c->cases[v]->decl) add_fn_once(calls, c->cases[v]); if (c->kind == CALL_VALUE) { Node *site = mknode(N_NIL, n->pos); addnode(by_value, site); body_calls(c->callee, calls, by_value, taken); } if (c->kind == CALL_FN && c->fn && c->fn->builtin) { b32 shows = c->builtin == B_PRINT || c->builtin == B_PRINTLN || c->builtin == B_STR || c->builtin == B_ADD || c->builtin == B_INSERT; for (iz i = 0; i < c->nargs; i++) { Type *t = c->args[i] ? c->args[i]->type : 0; if (t && t->kind == TY_REF) t = t->elem; if (shows) add_hooks(calls, t, 1); add_hooks(calls, t, 0); // lists, maps and in compare their items } } for (iz i = 0; i < c->nargs; i++) body_calls(c->args[i], calls, by_value, taken); return; } if ((n->kind == N_BINARY && (n->op == T_EQEQ || n->op == T_NE)) || n->kind == N_IN) { add_hooks(calls, n->a ? n->a->type : 0, 0); add_hooks(calls, n->b ? n->b->type : 0, 0); } if (n->kind == N_INDEX && n->a && n->a->type && n->a->type->kind == TY_MAP) add_hooks(calls, n->a->type->key, 0); if (n->kind == N_SWITCH && n->a) add_hooks(calls, n->a->type, 0); if (n->kind == N_NAME && n->sym && n->sym->kind == S_FN && n->type && n->type->kind == TY_FN) { // a function used as a value: whatever calls values may call it for (iz i = 0; i < n->sym->fns.len; i++) if (!n->sym->fns.data[i]->builtin) add_fn_once(taken, n->sym->fns.data[i]); } if (n->kind == N_FNLIT && n->info) { Fn *cl = n->info; add_fn_once(taken, cl); // a closure is a function value too if (cl->decl) body_calls(cl->decl->b, calls, by_value, taken); } body_calls(n->a, calls, by_value, taken); body_calls(n->b, calls, by_value, taken); if (n->kind != N_FOR) body_calls(n->c, calls, by_value, taken); for (iz i = 0; i < n->list.len; i++) body_calls(n->list.data[i], calls, by_value, taken); } static Fn * fn_of_def(Node *d) { return d && d->kind == N_FN && d->info ? (Fn *)d->info : 0; } // The function a question names, which must be one. static Fn * asked_fn(Node *d, Pos at) { Fn *f = fn_of_def(d); if (!f || f->ismacro) { stop_metaprogram(); errorat(at, "%s is not a function", d->name); } return f; } // Whether a declaration is one of the program's own (not std:, the prelude) static b32 program_decl(Node *d) { return d && d->pos.file >= 0 && !library_module(files.data[d->pos.file].module); } static void enc_fns(Buf *b, Fns *fs) { iz n = 0; for (iz i = 0; i < fs->len; i++) n += fs->data[i]->decl && fs->data[i]->decl->kind == N_FN && program_decl(fs->data[i]->decl); bputf(b, "l%lld;", (long long)n); for (iz i = 0; i < fs->len; i++) if (fs->data[i]->decl && fs->data[i]->decl->kind == N_FN && program_decl(fs->data[i]->decl)) enc_def(b, fs->data[i]->decl); } // A fact that can still grow until checking ends: an error naming the // moment, or nil inside known(...) NORETURN static void not_final_yet(Pos at, Pos why, Str what, Str reason) { if (asking_softly) longjmp(not_final, 1); stop_metaprogram(); notes.len = 0; if (why.file >= 0) { note(why, reason); } help_text = S("ask it in macro fn build(b: Build), when every function exists"); errorat(at, "%s can still grow while macros generate code", what); } // Everything f can run: its calls, theirs, and once anything calls through // a function value, every function whose value the program takes (which is // final only when checking has ended). *self says whether f reaches itself. static Fns fn_reaches(Fn *f, Pos at, Str what, b32 *self) { Fns out = {0}, taken = {0}; b32 values = 0, again = 0; add_fn_once(&out, f); for (iz i = 0; i < out.len; i++) { Fn *g = out.data[i]; need_checked(g, at); Fns calls = {0}; Nodes by_value = {0}; body_calls(g->decl->b, &calls, &by_value, &taken); for (iz k = 0; k < calls.len; k++) { again |= calls.data[k] == f; add_fn_once(&out, calls.data[k]); } if (by_value.len && !values) { if (!program_checked) not_final_yet(at, by_value.data[0]->pos, what, S("a call through a function value: it may run any function whose " "value the program takes")); values = 1; // every function value the program takes for (iz k = 0; k < all_fns.len; k++) { Fn *h = all_fns.data[k]; if (h->builtin || h->ismacro || h->isclosure || !h->decl || !h->checked) continue; Fns ignore = {0}; Nodes ignore2 = {0}; body_calls(h->decl->b, &ignore, &ignore2, &taken); } } if (values) for (iz k = 0; k < taken.len; k++) { again |= taken.data[k] == f; add_fn_once(&out, taken.data[k]); } } if (self) *self = again; return out; } // What a checked body reads and writes of the program's state, and its // effects. Globals are named; accesses through references and var // parameters (which place, the caller's) are kept as sites. typedef struct { Syms reads, writes; Nodes read_sites, write_sites; // N_NIL nodes holding a position struct { int *data; iz len; iz cap; } effect_kinds; // EffectKind tags Nodes effect_sites; } Access; static void add_c_effect(Access *a, Fn *f); static void add_sym_once(Syms *out, Sym *s) { for (iz i = 0; i < out->len; i++) if (out->data[i] == s) return; *push(&perm, out) = s; } static void add_site(Nodes *out, Pos p) { addnode(out, mknode(N_NIL, p)); } static b32 program_global(Sym *s) { return s && s->kind == S_VAR && s->isglobal && !s->ismacro && s->decl && !s->isconst; } // EffectKind: output, input, files, run, environment, exit, fail (and, in a // program with C, plugin) static void add_effect(Access *a, int kind, Pos p) { *push(&perm, &a->effect_kinds) = kind; add_site(&a->effect_sites, p); } static void body_access(Node *n, Access *a); // the place an assignment or & changes: a global, or somewhere else static void access_place(Node *t, Access *a, Fn *f) { Node *root = t; while (root->kind == N_FIELD || root->kind == N_INDEX) { for (iz i = 0; root->kind == N_INDEX && i < root->list.len; i++) body_access(root->list.data[i], a); root = root->a; } if (root->kind == N_NAME && root->sym) { Sym *s = root->sym; if (program_global(s)) add_sym_once(&a->writes, s); else if (s->kind == S_VAR && (s->isvar || (f && s->owner != f && !s->isglobal))) add_site(&a->write_sites, t->pos); // the caller's place, or a captured one return; } if (root->kind == N_DEREF) { body_access(root->a, a); add_site(&a->write_sites, t->pos); return; } body_access(root, a); } static STATE Fn *access_fn = 0; // whose body is walked static void body_access(Node *n, Access *a) { if (!n) return; switch ((int)n->kind) { case N_ASSIGN: access_place(n->a, a, access_fn); if (n->op != T_EQ) body_access(n->a, a); // += reads it too body_access(n->b, a); return; case N_ADDR: // & gives access to change it access_place(n->a, a, access_fn); body_access(n->a, a); return; case N_NAME: if (n->sym && program_global(n->sym)) add_sym_once(&a->reads, n->sym); else if (n->sym && n->sym->kind == S_VAR && n->sym->isvar) add_site(&a->read_sites, n->pos); return; case N_DEREF: add_site(&a->read_sites, n->pos); body_access(n->a, a); return; case N_SWITCH: if (n->flags & F_REF) access_place(n->a, a, access_fn); break; case N_FNLIT: if (n->info && ((Fn *)n->info)->decl) body_access(((Fn *)n->info)->decl->b, a); return; } if ((int)n->kind == X_CALLFN) { Call *c = n->info; if (c->kind == CALL_FN && c->fn && c->fn->builtin) { switch (c->builtin) { case B_PRINT: case B_PRINTLN: add_effect(a, 1, n->pos); break; case B_READ_FILE: add_effect(a, 2, n->pos), add_effect(a, 3, n->pos); break; case B_WRITE_FILE: case B_FILE_EXISTS: add_effect(a, 3, n->pos); break; case B_RUN: case B_SHELL: add_effect(a, 4, n->pos); break; case B_GETENV: case B_ARGUMENTS: add_effect(a, 5, n->pos); break; case B_EXIT: add_effect(a, 6, n->pos); break; case B_FAIL: add_effect(a, 7, n->pos); break; default: break; } } if (c->kind == CALL_VALUE) body_access(c->callee, a); for (iz i = 0; i < c->nargs; i++) body_access(c->args[i], a); return; } if ((int)n->kind == X_CONV) { body_access(n->a, a); return; } body_access(n->a, a); body_access(n->b, a); if (n->kind != N_FOR) body_access(n->c, a); for (iz i = 0; i < n->list.len; i++) body_access(n->list.data[i], a); } // Through everything f reaches static Access fn_access(Fn *f, Pos at, Str what) { Access a = {0}; Fns r = fn_reaches(f, at, what, 0); for (iz i = 0; i < r.len; i++) { access_fn = r.data[i]; body_access(r.data[i]->decl->b, &a); if (r.data[i]->c_code) add_c_effect(&a, r.data[i]); } access_fn = 0; return a; } static void enc_globals(Buf *b, Syms *gs) { bputf(b, "l%lld;", (long long)gs->len); for (iz i = 0; i < gs->len; i++) enc_def(b, gs->data[i]->decl); } static void enc_sites(Buf *b, Nodes *ns) { bputf(b, "l%lld;", (long long)ns->len); for (iz i = 0; i < ns->len; i++) put_site(b, ns->data[i]->pos); } // locals(f): its variables, parameters and bindings, with where each lives // and what keeps a cell (an escaping closure that captures it, a stored // reference to it) // A local the compiler made for its own code (templates, loops) static b32 internal_local(Str name) { static Str prefixes[] = {SL("template__"), SL("each__"), SL("macro__"), SL("it__")}; for (iz i = 0; i < countof(prefixes); i++) if (startswith(name, prefixes[i])) return 1; return 0; } static Str unrenamed(Str t); static void enc_local(Buf *b, Sym *s); static void enc_locals(Buf *b, Fn *f) { Syms all = {0}; for (iz i = 0; i < f->params.len; i++) *push(&perm, &all) = f->params.data[i]; for (iz i = 0; i < f->locals.len; i++) { Sym *s = f->locals.data[i]; b32 dup = 0; for (iz k = 0; k < all.len; k++) dup |= all.data[k] == s; if (!dup && s->kind == S_VAR && s->name.len && !internal_local(s->name)) *push(&perm, &all) = s; } bputf(b, "l%lld;", (long long)all.len); for (iz i = 0; i < all.len; i++) enc_local(b, all.data[i]); } static void enc_local(Buf *b, Sym *s) { put_str(b, unrenamed(s->name)); // as written in its template enc_type_value(b, s->type ? s->type : t_void, s->pos); put_site(b, s->pos); // LocalKind: variable, constant, parameter, var_parameter, binding int kind = s->isparam ? (s->isvar ? 4 : 3) : s->bound_by ? 5 : s->isconst ? 2 : 1; bputf(b, "u%d;", kind); Nodes kept = {0}; for (iz k = 0; k < closures.len; k++) { // an escaping closure that captures it Fn *c = closures.data[k]; if (!c->escapes) continue; for (iz j = 0; j < c->captures.len; j++) if (c->captures.data[j] == s && c->decl) add_site(&kept, c->decl->pos); } if (s->refpinned && s->pin_site.file >= 0 && (s->pin_site.offset || s->pin_site.file)) add_site(&kept, s->pin_site); b32 cell = s->pinned || kept.len; // Lifetime: stack, counted_cell, permanent_cell bputf(b, "u%d;", !cell ? 1 : s->refpinned ? 3 : 2); bputf(b, "l%lld;", (long long)kept.len); for (iz k = 0; k < kept.len; k++) put_site(b, kept.data[k]->pos); } // The checked copies of macro arguments, by the argument: type_at(e) and // the other questions about code a macro was given read them static STATE Map *checked_args = 0; // Questions about code a macro was given: the node written at a site, in // the arguments of the macro call (checked when the macro takes Expr(T)), // with the nodes around it static b32 node_path(Node *n, Pos site, b32 (*fits)(Node *), Nodes *path) { if (!n) return 0; addnode(path, n); if (n->pos.file == site.file && n->pos.offset == site.offset && fits(n)) return 1; if ((int)n->kind == X_CALLFN) { Call *c = n->info; // Follow the checked arguments, including inserted conversions, // rather than the original argument list kept for printing. if (node_path(c->callee ? c->callee : n->a, site, fits, path)) return 1; for (iz i = 0; i < c->nargs; i++) if (node_path(c->args[i], site, fits, path)) return 1; path->len--; return 0; } if (node_path(n->a, site, fits, path) || node_path(n->b, site, fits, path)) return 1; if (!(n->kind == N_FOR && n->c && n->sval.len) && node_path(n->c, site, fits, path)) return 1; for (iz i = 0; i < n->list.len; i++) if (node_path(n->list.data[i], site, fits, path)) return 1; path->len--; return 0; } static b32 typed_fits(Node *n) { return n->type && n->type != t_type && (int)n->kind != X_CONV; } static b32 name_fits(Node *n) { return n->kind == N_NAME; } static b32 call_fits(Node *n) { return (int)n->kind == X_CALLFN || n->kind == N_CALL; } // The node at site and the path to it (path->data[path->len - 1] is it); // *checked says whether it is from a checked copy static Node * given_node(Pos site, b32 (*fits)(Node *), Nodes *path, b32 *checked) { for (iz i = 0; macro_call && i < macro_call->list.len; i++) { Node *a = macro_call->list.data[i]; if (a->kind == N_NAMED) a = a->a; Node *copy = lookup(checked_args, ptrkey(a)); path->len = 0; if (node_path(copy ? copy : a, site, fits, path)) { if (checked) *checked = copy != 0; return path->data[path->len - 1]; } } return 0; } // Whether unchecked code declares name around the end of path: a // parameter, an earlier local, a loop or switch binding static b32 declared_around(Nodes *path, Str name) { for (iz i = 0; i + 1 < path->len; i++) { Node *a = path->data[i], *child = path->data[i + 1]; if (a->kind == N_FN || a->kind == N_FNLIT) { for (iz k = 0; k < a->list.len; k++) if (equals(a->list.data[k]->name, name)) return 1; } else if (a->kind == N_BLOCK) { for (iz k = 0; k < a->list.len && a->list.data[k] != child; k++) { Node *st = a->list.data[k]; if ((st->kind == N_VAR || st->kind == N_FN) && equals(st->name, name)) return 1; } } else if (a->kind == N_FOR && child == a->b) { if (equals(a->name, name) || equals(a->sval, name) || (equals(name, S("it")) && (a->flags & F_IMPLICIT))) return 1; } else if (a->kind == N_SWITCH && child != a->a) { if (equals(a->name, name)) return 1; } } return 0; } static void overloaded_name(Sym *sym, Pos at) { stop_metaprogram(); notes.len = 0; for (iz i = 0; i < sym->fns.len && i < 8; i++) { note(sym->fns.data[i]->decl->pos, S("an overload")); } help_text = fmtstr("name one with overload_of(%s, T, ...)", sym->name); errorat(at, "%s has %lld overloads", sym->name, (long long)sym->fns.len); } // The declaration a name at the end of path refers to: in checked code by // what the checker chose, in unchecked code by the scope of the macro call // unless the code around it declares the name. 0 for locals and built-ins. static Node * decl_of_name(Nodes *path, b32 checked, Pos at) { Node *n = path->data[path->len - 1]; Node *parent = path->len > 1 ? path->data[path->len - 2] : 0; if (checked && parent && (int)parent->kind == X_CALLFN) { Call *c = parent->info; b32 written_call = parent->src && (parent->src->kind == N_CALL || parent->src->kind == N_NAME); if (c->callee == n || (written_call && parent->a == n)) { if (c->kind == CALL_FN && c->fn) return c->fn->builtin ? 0 : c->fn->decl; // A dispatch names several declarations. calls_at gives them; // there is no single declaration for def_of to return. if (c->kind == CALL_DISPATCH) return 0; } } if (!checked && declared_around(path, n->name)) return 0; Sym *sym = n->sym; if (!sym && !checked && macro_scope) sym = find(macro_scope, n->name, n->pos, 0); if (!sym) return 0; if (sym->kind == S_FN) { if (sym->fns.len != 1) overloaded_name(sym, at); return sym->fns.data[0]->builtin ? 0 : sym->fns.data[0]->decl; } if ((sym->kind == S_VAR && sym->isglobal) || sym->kind == S_TYPE) return sym->decl; return 0; } // with_main(b, name): the program generated again from another root, in a // C file of its own; the functions keep the build facts of the main build static Str with_main_build(Str name, Pos at) { Sym *sym = scope_get(main_module->scope, name); if (!sym || sym->kind != S_FN || sym->fns.len != 1 || sym->fns.data[0]->ismacro) { stop_metaprogram(); errorat(at, sym && sym->kind == S_FN && sym->fns.len > 1 ? "%s has overloads; a main is one function" : "the program has no function %s to start at", name); } Fn *f = sym->fns.data[0]; if (f->decl->list.len || f->decl->a || (f->type && f->type->elem != t_void)) { stop_metaprogram(); notes.len = 0; note(f->decl->pos, S("its declaration")); errorat(at, "%s cannot start a program: a main takes no parameters and returns nothing", name); } struct Facts { Nodes checks, allocs; b32 hooks; } *saved = new(&perm, all_fns.len + 1, struct Facts); for (iz i = 0; i < all_fns.len; i++) { Fn *g = all_fns.data[i]; saved[i] = (struct Facts){g->gen_checks, g->gen_allocs, g->gen_hooks}; g->gen_checks = g->gen_allocs = (Nodes){0}; } root_override = f; Str c = generate(); root_override = 0; for (iz i = 0; i < all_fns.len; i++) { Fn *g = all_fns.data[i]; g->gen_checks = saved[i].checks; g->gen_allocs = saved[i].allocs; g->gen_hooks = saved[i].hooks; } Str cpath = fmtstr("%s-%s.c", substr(build_cpath, 0, build_cpath.len - 2), name); if (debug_build) c = debug_lines(c, cpath); if (!os_writefile(cpath, c)) fatal("cannot write %s", cpath); made(cpath); Buf b = {0}; put_str(&b, cpath); bput(&b, build_sources); put_str(&b, name); bput(&b, build_strings); return bstr(&b); } static Str answer(Str q, Pos at) { Buf b = {0}; // asked from a std:code question function: name the macro call instead if (at.file < 0 || (code_module && files.data[at.file].module == code_module->id)) at = macro_site; if (equals(q, S("def"))) { if (rd() != 'i') meta_died(); i64 id = rd_num(); if (id < 1 || id > def_table.len) meta_died(); enc_def(&b, def_table.data[id - 1]); } else if (equals(q, S("defs"))) { Nodes all = all_program_defs(); bputf(&b, "l%lld;", (long long)all.len); for (iz i = 0; i < all.len; i++) enc_def(&b, all.data[i]); } else if (equals(q, S("lookup"))) { Str name = rd_str(); Nodes all = all_program_defs(), found = {0}; for (iz i = 0; i < all.len; i++) if (equals(all.data[i]->name, name)) addnode(&found, all.data[i]); if (found.len != 1) { stop_metaprogram(); notes.len = 0; for (iz i = 0; i < found.len && i < 8; i++) { note(found.data[i]->pos, S("one of them")); } if (found.len) help_text = fmtstr("name one with overload_of(%s, T, ...)", name); if (!found.len) errorat(at, "the program declares nothing named %s here", name); errorat(at, "the program declares %lld things named %s", (long long)found.len, name); } enc_def(&b, found.data[0]); } else if (equals(q, S("type_at")) || equals(q, S("def_at")) || equals(q, S("local_at")) || equals(q, S("calls_at"))) { Pos site = rd_site(); Nodes path = {0}; b32 checked = 0; if (equals(q, S("type_at"))) { Node *n = given_node(site, typed_fits, &path, &checked); if (!n) { stop_metaprogram(); help_text = S("take it as Expr(T): such an argument is checked before the macro runs"); errorat(at, "this code was not checked, so it has no type yet"); } enc_type_value(&b, n->type, site); } else if (equals(q, S("calls_at"))) { // the functions a call may run: one, the cases of a dispatch, // none for a call through a function value or a built-in Node *n = given_node(site, call_fits, &path, &checked); Fns fs = {0}; if (n && (int)n->kind == X_CALLFN) { Call *c = n->info; if (c->kind == CALL_FN && c->fn && !c->fn->builtin) add_fn_once(&fs, c->fn); for (iz v = 0; c->kind == CALL_DISPATCH && c->cases && v < c->dispatch_union->fields.len; v++) if (c->cases[v] && c->cases[v]->decl) add_fn_once(&fs, c->cases[v]); } else if (n && n->a->kind == N_NAME) { addnode(&path, n->a); Node *d = decl_of_name(&path, 0, at); if (d && d->kind == N_FN) add_fn_once(&fs, d->info); } enc_fns(&b, &fs); } else { Node *n = given_node(site, name_fits, &path, &checked); if (equals(q, S("def_at"))) { Node *d = n ? decl_of_name(&path, checked, at) : 0; if (d) { bput(&b, S("u1;")); enc_def(&b, d); } else { bput(&b, S("u0;")); } } else if (n && n->sym && n->sym->kind == S_VAR && !n->sym->isglobal) { bput(&b, S("u1;")); enc_local(&b, n->sym); } else { bput(&b, S("u0;")); } } } else if (equals(q, S("with_main"))) { // the Build given (c_file, sources, name, strings, and in a program // with C c_flags and link), then the name (void)rd_str(); if (rd() != 'l') meta_died(); for (i64 k = rd_num(); k > 0; k--) (void)rd_str(); (void)rd_str(); if (rd() != 'l') meta_died(); for (i64 k = rd_num(); k > 0; k--) (void)rd_str(); Str c_flags = c_build_fields ? c_read_strs() : (Str){0}; Str link = c_build_fields ? c_read_strs() : (Str){0}; Str name = rd_str(); if (!building) { stop_metaprogram(); help_text = S("call it in macro fn build(b: Build)"); errorat(at, "with_main builds another program, so it works only in build"); } bput(&b, with_main_build(name, at)); if (c_build_fields) { // what the program's build gave bput(&b, c_flags); bput(&b, link); } } else if (equals(q, S("parse"))) { // code from text: parsed as an expression, a type or statements Str text = rd_str(), what = rd_str(); int position = equals(what, S("expr")) ? P_EXPR : equals(what, S("type")) ? P_TYPE : P_STMT; Node *r = parse_text(text, position, at, S("parse")); if (position == P_EXPR) { enc_expr(&b, r); } else if (position == P_TYPE) { enc_type(&b, r); } else { bputf(&b, "l%lld;", (long long)r->list.len); for (iz i = 0; i < r->list.len; i++) enc_stmt(&b, r->list.data[i]); } } else { Node *d = rd_def(at); if (equals(q, S("overloads"))) { Nodes all = all_program_defs(), same = {0}; for (iz i = 0; i < all.len; i++) if (all.data[i]->kind == d->kind && equals(all.data[i]->name, d->name)) addnode(&same, all.data[i]); bputf(&b, "l%lld;", (long long)same.len); for (iz i = 0; i < same.len; i++) enc_def(&b, same.data[i]); } else if (equals(q, S("type"))) { Type *t = 0; Pos save = need_at; need_at = at; if (d->kind == N_FN) { Fn *f = d->info; if (asking_softly && !f->checked && (!f->type || !f->type->elem)) longjmp(not_final, 1); b32 save_q = question_detour; question_detour = 1; fn_signature(f); // a result written by hand, or the body checked now if (!f->type->elem) need_checked(f, at); question_detour = save_q; t = f->type; } else if (d->kind == N_VAR) { check_global(d->sym); t = d->sym->type; } else { Sym *sym = scope_get(modules.data[files.data[d->pos.file].module]->scope, d->name); t = sym && sym->kind == S_TYPE ? sym->type : 0; } need_at = save; if (!t) meta_died(); enc_type_value(&b, t, d->pos); } else if (equals(q, S("decl_of"))) { enc_decl(&b, d); } else if (equals(q, S("allocates")) || equals(q, S("checks")) || equals(q, S("ranges"))) { Fn *f = asked_fn(d, at); if (!building) { if (asking_softly) longjmp(not_final, 1); stop_metaprogram(); help_text = S("ask it in macro fn build(b: Build), which runs after code generation"); errorat(at, "%s(%s) is known once the program is compiled", q, d->name); } if (equals(q, S("allocates"))) { // through everything it reaches, closures it makes, and the // print and == overloads the runtime may run for it Fns r = fn_reaches(f, at, S("allocates"), 0); b32 hooks = 0; for (iz i = 0; i < r.len; i++) { hooks |= r.data[i]->gen_hooks; for (iz k = 0; k < closures.len; k++) for (Fn *p = closures.data[k]->parent; p; p = p->parent) if (p == r.data[i]) add_fn_once(&r, closures.data[k]); if (i == r.len - 1 && hooks == 1) { hooks = 2; for (iz k = 0; k < all_fns.len; k++) { Fn *h = all_fns.data[k]; if (h->builtin || h->ismacro || !h->checked || !h->type || !h->decl) continue; b32 print = equals(h->name, S("print")) && h->type->fields.len == 1; b32 eq = equals(h->name, S("==")) && h->type->fields.len == 2; if (!print && !eq) continue; Fns hr = fn_reaches(h, at, S("allocates"), 0); for (iz j = 0; j < hr.len; j++) add_fn_once(&r, hr.data[j]); } } } Nodes sites = {0}; for (iz i = 0; i < r.len; i++) for (iz k = 0; k < r.data[i]->gen_allocs.len; k++) addnode(&sites, r.data[i]->gen_allocs.data[k]); enc_sites(&b, &sites); } else if (equals(q, S("checks"))) { Nodes cs = {0}, all = {0}; for (iz j = 0; j < f->gen_checks.len; j++) addnode(&all, f->gen_checks.data[j]); for (iz k = 0; k < closures.len; k++) // and the closures it makes for (Fn *p = closures.data[k]->parent; p; p = p->parent) if (p == f) for (iz j = 0; j < closures.data[k]->gen_checks.len; j++) addnode(&all, closures.data[k]->gen_checks.data[j]); for (iz i = 0; i < all.len; i++) { Node *c = all.data[i]; b32 dup = 0; for (iz k = 0; k < cs.len && !dup; k++) dup = cs.data[k]->ival == c->ival && cs.data[k]->pos.file == c->pos.file && cs.data[k]->pos.offset == c->pos.offset; if (!dup) addnode(&cs, c); } bputf(&b, "l%lld;", (long long)cs.len); for (iz i = 0; i < cs.len; i++) { bputf(&b, "u%d;", (int)cs.data[i]->ival); put_site(&b, cs.data[i]->pos); bput(&b, cs.data[i]->flags ? S("b1") : S("b0")); } } else { // ranges: what optimizing found, nothing without it Syms rs = {0}; for (iz i = 0; optimizing() && i < f->locals.len; i++) { Sym *v = f->locals.data[i]; if (v->kind != S_VAR || !v->type || v->type->kind != TY_INT || !v->name.len || indexof(v->name, S("__")) >= 0 || (v->rlo == INT64_MIN && v->rhi == INT64_MAX) || v->rlo > v->rhi) continue; b32 dup = 0; for (iz k = 0; k < rs.len; k++) dup |= rs.data[k] == v; if (!dup) *push(&perm, &rs) = v; } bputf(&b, "l%lld;", (long long)rs.len); for (iz i = 0; i < rs.len; i++) { enc_local(&b, rs.data[i]); bputf(&b, "i%lld;i%lld;", (long long)rs.data[i]->rlo, (long long)rs.data[i]->rhi); } } } else if (equals(q, S("locals"))) { Fn *f = asked_fn(d, at); need_checked(f, at); // where each lives is decided by its own body enc_locals(&b, f); } else if (equals(q, S("reads")) || equals(q, S("writes")) || equals(q, S("reads_elsewhere")) || equals(q, S("writes_elsewhere")) || equals(q, S("effects"))) { Fn *f = asked_fn(d, at); Access a = fn_access(f, at, fmtstr("%s(%s)", q, d->name)); if (equals(q, S("reads"))) enc_globals(&b, &a.reads); else if (equals(q, S("writes"))) enc_globals(&b, &a.writes); else if (equals(q, S("reads_elsewhere"))) enc_sites(&b, &a.read_sites); else if (equals(q, S("writes_elsewhere"))) enc_sites(&b, &a.write_sites); else { bputf(&b, "l%lld;", (long long)a.effect_kinds.len); for (iz i = 0; i < a.effect_kinds.len; i++) { bputf(&b, "u%d;", a.effect_kinds.data[i]); if (a.effect_kinds.data[i] == C_PLUGIN_EFFECT) put_str(&b, a.effect_sites.data[i]->sval); put_site(&b, a.effect_sites.data[i]->pos); } } } else if (equals(q, S("calls")) || equals(q, S("calls_by_value")) || equals(q, S("reaches")) || equals(q, S("callers")) || equals(q, S("recursive"))) { Str what = fmtstr("%s(%s)", q, d->name); Fn *f = asked_fn(d, at); if (equals(q, S("calls")) || equals(q, S("calls_by_value"))) { need_checked(f, at); Fns calls = {0}, taken = {0}; Nodes by_value = {0}; body_calls(f->decl->b, &calls, &by_value, &taken); if (equals(q, S("calls"))) { enc_fns(&b, &calls); } else { bputf(&b, "l%lld;", (long long)by_value.len); for (iz i = 0; i < by_value.len; i++) put_site(&b, by_value.data[i]->pos); } } else if (equals(q, S("reaches")) || equals(q, S("recursive"))) { b32 self = 0; Fns r = fn_reaches(f, at, what, &self); if (equals(q, S("recursive"))) { bput(&b, self ? S("b1") : S("b0")); } else { Fns rest = {0}; // f itself only when it reaches itself if (self) add_fn_once(&rest, f); for (iz i = 1; i < r.len; i++) add_fn_once(&rest, r.data[i]); enc_fns(&b, &rest); } } else { // callers: every function of the program is needed if (!program_checked) not_final_yet(at, (Pos){-1, 0, 0, 0}, what, S("later code may call it")); Nodes all = all_program_defs(); Fns callers = {0}; for (iz i = 0; i < all.len; i++) { Fn *g = fn_of_def(all.data[i]); if (!g || g->ismacro) continue; need_checked(g, at); Fns calls = {0}, taken = {0}; Nodes by_value = {0}; body_calls(g->decl->b, &calls, &by_value, &taken); for (iz k = 0; k < calls.len; k++) if (calls.data[k] == f) add_fn_once(&callers, g); for (iz k = 0; k < taken.len; k++) // takes its value: may call it if (taken.data[k] == f) add_fn_once(&callers, g); } enc_fns(&b, &callers); } } else if (equals(q, S("made_by"))) { if (d->pos.file >= 0 && files.data[d->pos.file].generated) { bput(&b, S("u1;")); put_site(&b, files.data[d->pos.file].origin); } else { bput(&b, S("u0;")); } } else { meta_died(); } } return bstr(&b); } // The answer after b1, or b0 inside known(...) when it is not final yet static Str final_answer(Str q, Pos at, b32 softly) { asking_softly = softly; if (softly) { if (setjmp(not_final)) { // C99 allows setjmp only as the whole condition asking_softly = 0; return S("b0"); } } Buf b = {0}; bput(&b, S("b1")); bput(&b, answer(q, at)); asking_softly = 0; return bstr(&b); } // Sends one macro call and returns its result as source text. static Str meta_call(iz index, Buf *args, Pos site) { start_metaprogram(site); #ifdef FUZZ (void)index; (void)args; return (Str){0}; // every macro produces nothing #endif Buf b = {0}; bputf(&b, "c%lld;", (long long)index); put_site(&b, site); bput(&b, bstr(args)); os_write(meta_req, bstr(&b)); Pos save = macro_site; macro_site = site; for (;;) { int k = rd(); if (k == 'R') { Str r = rd_str(); macro_site = save; return r; } if (k == 'E' || k == 'W' || k == 'D') { // D: a failed check in compile-time code, at file:line:col or in // the runtime ("star99") Pos at = k == 'D' ? loc_pos(rd_str()) : rd_site(); if (k == 'D') k = 'E'; Str msg = rd_str(); notes.len = 0; if (at.file != site.file || at.offset != site.offset) { note(site, S("in this macro call")); } if (k == 'E') { stop_metaprogram(); errorat(at, "%s", msg); } warnat(at, "%s", msg); continue; } if (k == 'P') { Str target = rd_str(); Str text = rd_str(); place_code(target, text, site); continue; } if (k == 'Q') { // a question about the program (std:code): its answer, encoded Str q = rd_str(); Pos at = loc_pos(rd_str()); int soft = rd(); if (soft != '0' && soft != '1') meta_died(); os_write(meta_req, final_answer(q, at, soft == '1')); continue; } if (k == 'X') { // expanded(b) in a build: the program with its macros expanded Buf out = {0}; put_str(&out, expanded_text()); os_write(meta_req, bstr(&out)); continue; } if (k == 'F') { // format(text) in compile-time code: star99 fmt on the text; an // error in the text is reported at the call format_call = loc_pos(rd_str()); Buf out = {0}; put_str(&out, format_file(S("format()"), rd_str())); format_call = (Pos){-1, 0, 0, 0}; os_write(meta_req, bstr(&out)); continue; } meta_died(); } } // Arguments, encoded as the std:code values the macro's parameters expect. // The tags follow the variant order of std:code's unions. static b32 is_type_node(Node *n) { return n->kind == N_TNAME || n->kind == N_TFN || n->kind == N_TSTRUCT || n->kind == N_TUNION || n->kind == N_TMACRO || n->kind == N_TYPEX || n->kind == N_TYPENAME; } static b32 is_decl_node(Node *n) { return n->kind == N_FN || n->kind == N_STRUCT || n->kind == N_UNION || n->kind == N_VAR || n->kind == N_DECLS; } static b32 is_stmt_node(Node *n) { switch (n->kind) { case N_VAR: case N_ASSIGN: case N_DISCARD: case N_EXPR: case N_IF: case N_FOR: case N_WHILE: case N_BLOCK: case N_RETURN: case N_BREAK: case N_CONTINUE: case N_DEFER: case N_LABELED: case N_SWITCH: case N_FN: return 1; default: return 0; } } // A template is encoded with markers that check_template turns into code // run by the macro: \1 kind index \1 for a splice, \2 name \3 for a name // the template declares (it gets the run's suffix), and \4 count;splices \4 // for the length of a list that holds spliced lists. static STATE b32 enc_template = 0; // A template of a module other than the main one marks the names it writes // (a 6 before each), so they resolve where the template is written. static STATE b32 template_own = 0; static Str own_marked(Str name) { if (!enc_template || !template_own || lookup(hyg_names, name)) return name; return concat(S("\x06"), name); } static STATE b32 enc_closure_params = 0; // encoding the parameters of a closure: its locals static STATE Nodes enc_splices = {0}; static STATE struct { char *data; iz len; iz cap; } enc_splice_kinds = {0}; static void enc_splice(Buf *b, Node *e, char kind) { if (e->kind == N_SPLICE) e = e->a; if (!enc_template) { // code a macro was given: $x inside a template there bputf(b, "u%d;", EX_SPLICE); enc_expr(b, e); put_site(b, e->pos); return; } bputc(b, 1); bputf(b, "%c%lld", kind, (long long)enc_splices.len); bputc(b, 1); addnode(&enc_splices, e); *push(&perm, &enc_splice_kinds) = kind; } // A name as a String; a name the template declares gets the run's suffix. static void put_name(Buf *b, Str name) { if (enc_template && lookup(hyg_names, name)) { bputc(b, 2); bput(b, name); bputc(b, 3); return; } put_str(b, name); } // A name that may be spliced: fn $f, var $x, label $l, .$v static void put_spliced_name(Buf *b, Node *splice, Str name) { if (splice && enc_template) { enc_splice(b, splice, 'n'); return; } if (splice) { // outside a template it stays as written put_str(b, node_source(splice)); return; } put_name(b, name); } // A list: its items, each by enc, and the spliced lists among them (kind // is the splice kind of an item that is a whole list). typedef void (*EncItem)(Buf *b, Node *n); static b32 item_splice(Node *n, char kind) { switch (kind) { case 'E': return n->kind == N_SPLICE; case 'S': return n->kind == N_EXPR && n->a && n->a->kind == N_SPLICE; case 'P': return n->kind == N_PARAM && n->c && !n->a && !n->b; case 'T': return n->kind == N_TNAME && equals(n->name, S("$")) && n->a; default: return 0; } } static void enc_list(Buf *b, Nodes *items, EncItem enc, char kind) { Buf body = {0}; iz fixed = 0; Buf spliced = {0}; for (iz i = 0; i < items->len; i++) { Node *n = items->data[i]; if (enc_template && item_splice(n, kind)) { Node *e = kind == 'S' ? n->a : kind == 'P' ? n->c : kind == 'T' ? n->a : n; if (spliced.len) bputc(&spliced, ','); bputf(&spliced, "%lld", (long long)enc_splices.len); n->ival = enc_splices.len; // for the names of spliced arguments enc_splice(&body, e, kind); continue; } fixed++; enc(&body, n); } if (spliced.len) { bputc(b, 4); bputf(b, "%lld;", (long long)fixed); bput(b, bstr(&spliced)); bputc(b, 4); } else { bputf(b, "l%lld;", (long long)fixed); } bput(b, bstr(&body)); } static void enc_expr_item(Buf *b, Node *n) { enc_expr(b, n->kind == N_NAMED ? n->a : n); } static void enc_stmt_item(Buf *b, Node *n) { enc_stmt(b, n); } static void enc_type_item(Buf *b, Node *n) { enc_type(b, n); } // The names of arguments (empty for one without), the same length as the // arguments: a spliced list gets that many empty names (\1B...\1) static void enc_names(Buf *b, Nodes *items) { Buf body = {0}, spliced = {0}; iz fixed = 0; for (iz i = 0; i < items->len; i++) { Node *a = items->data[i]; if (enc_template && item_splice(a, 'E')) { if (spliced.len) bputc(&spliced, ','); // the splice index the arguments list gave this item bputf(&spliced, "%lld", (long long)a->ival); bputc(&body, 1); bputf(&body, "B%lld", (long long)a->ival); bputc(&body, 1); continue; } fixed++; if (a->kind == N_NAMED && a->c) put_spliced_name(&body, a->c, a->name); else put_str(&body, a->kind == N_NAMED ? a->name : (Str){0}); } if (spliced.len) { bputc(b, 4); bputf(b, "%lld;", (long long)fixed); bput(b, bstr(&spliced)); bputc(b, 4); } else { bputf(b, "l%lld;", (long long)fixed); } bput(b, bstr(&body)); } // Arguments and their names (calls, variants, .(...)) static void enc_args(Buf *b, Nodes *items) { enc_list(b, items, enc_expr_item, 'E'); enc_names(b, items); } static void enc_body(Buf *b, Node *blk) { if (!blk) { bput(b, S("l0;")); return; } if (blk->kind != N_BLOCK) { // one statement Nodes one = {0}; addnode(&one, blk); enc_list(b, &one, enc_stmt_item, 'S'); return; } enc_list(b, &blk->list, enc_stmt_item, 'S'); } static void enc_params(Buf *b, Nodes *ps); static void enc_expr(Buf *b, Node *n) { if (!n) { bput(b, S("u0;")); return; } if (n->src) n = n->src; // as written switch ((int)n->kind) { case X_CONV: enc_expr(b, n->a); return; case N_SPLICE: enc_splice(b, n, 'e'); return; case N_NAME: bputf(b, "u%d;", EX_NAME); if (enc_template && template_own && !lookup(hyg_names, n->name)) put_str(b, own_marked(n->name)); else put_name(b, n->name); put_site(b, n->pos); return; case N_INT: case N_FLOAT: case N_STR: case N_BOOL: case N_NIL: bputf(b, "u%d;", EX_LITERAL); put_str(b, node_source(n)); put_site(b, n->pos); return; case X_CALLFN: case N_CALL: bputf(b, "u%d;", EX_CALL); enc_expr(b, n->a); enc_args(b, &n->list); put_site(b, n->pos); return; case N_CAST: { // Int(x): a call of the type bputf(b, "u%d;", EX_CALL); enc_expr(b, n->a); Nodes one = {0}; addnode(&one, n->b); enc_args(b, &one); put_site(b, n->pos); return; } case N_FIELD: bputf(b, "u%d;", EX_FIELD); enc_expr(b, n->a); put_spliced_name(b, n->b, n->name); put_site(b, n->pos); return; case N_BINARY: case N_IN: case N_RANGE: if (n->kind == N_RANGE && (n->flags & F_IMPLICIT)) { bputf(b, "u%d;", EX_UNARY); put_str(b, opname(n->op)); enc_expr(b, n->b); put_site(b, n->pos); return; } bputf(b, "u%d;", EX_BINARY); put_str(b, n->kind == N_IN && (n->flags & F_NOT) ? S("not in") : opname(n->op)); enc_expr(b, n->a); enc_expr(b, n->b); put_site(b, n->pos); return; case N_UNARY: bputf(b, "u%d;", EX_UNARY); put_str(b, opname(n->op)); enc_expr(b, n->a); put_site(b, n->pos); return; case N_INDEX: bputf(b, "u%d;", EX_INDEX); enc_expr(b, n->a); enc_list(b, &n->list, enc_expr_item, 'E'); put_site(b, n->pos); return; case N_DEREF: case N_ADDR: bputf(b, "u%d;", n->kind == N_DEREF ? EX_DEREF : EX_ADDR); enc_expr(b, n->a); put_site(b, n->pos); return; case N_LIST: { b32 map = (n->flags & F_MAP) != 0; for (iz i = 0; i < n->list.len; i++) map |= n->list.data[i]->kind == N_PAIR; if (!map) { bputf(b, "u%d;", EX_LIST); enc_list(b, &n->list, enc_expr_item, 'E'); put_site(b, n->pos); return; } bputf(b, "u%d;l%lld;", EX_MAP, (long long)n->list.len); for (iz i = 0; i < n->list.len; i++) { Node *e = n->list.data[i]; enc_expr(b, e->kind == N_PAIR ? e->a : e); enc_expr(b, e->kind == N_PAIR ? e->b : 0); } put_site(b, n->pos); return; } case N_ANON: bputf(b, "u%d;", EX_ANON); enc_args(b, &n->list); put_site(b, n->pos); return; case N_VARIANT: bputf(b, "u%d;", EX_VARIANT); put_spliced_name(b, n->a, n->name); enc_args(b, &n->list); bput(b, n->flags & F_CALLED ? S("b1") : S("b0")); put_site(b, n->pos); return; case N_TYPEX: case N_TYPENAME: case N_TNAME: case N_TFN: case N_TSTRUCT: case N_TUNION: case N_TMACRO: bputf(b, "u%d;", EX_TYPE); enc_type(b, n); put_site(b, n->pos); return; case N_BLOCKX: bputf(b, "u%d;", EX_BLOCK); enc_body(b, n->a); put_site(b, n->pos); return; case N_IFX: bputf(b, "u%d;", EX_IF); enc_expr(b, n->a); enc_expr(b, n->b); enc_expr(b, n->c); put_site(b, n->pos); return; case N_SWITCHX: bputf(b, "u%d;", EX_SWITCH); enc_stmt(b, n->a); put_site(b, n->pos); return; case N_FNLIT: { bputf(b, "u%d;", EX_CLOSURE); b32 save = enc_closure_params; enc_closure_params = 1; enc_params(b, &n->list); enc_closure_params = save; enc_type(b, n->a); enc_body(b, n->b); put_site(b, n->pos); bput(b, n->flags & F_MACRO ? S("b1") : S("b0")); bput(b, n->b && (n->b->flags & F_DO) && n->b->list.len == 1 ? S("b1") : S("b0")); return; } case N_TEMPLATE: { bputf(b, "u%d;", EX_TEMPLATE); if (n->flags & F_DO) { enc_list(b, &n->list, enc_stmt_item, 'S'); } else { Node *st = mknode(N_EXPR, n->pos); st->a = n->a; Nodes one = {0}; addnode(&one, st); enc_list(b, &one, enc_stmt_item, 'S'); } bput(b, n->flags & F_DO ? S("b1") : S("b0")); put_site(b, n->pos); return; } default: break; } errorat(n->pos, "this is not an expression a macro can take"); } static void enc_type(Buf *b, Node *n) { if (!n) { bput(b, S("u0;")); return; } if (n->src) n = n->src; if (n->kind == N_TYPEX) n = n->a; switch ((int)n->kind) { case N_NAME: // code(Int): a type written as a value bputf(b, "u%d;", TX_NAME); put_name(b, n->name); bput(b, S("l0;")); put_site(b, n->pos); return; case N_CALL: if (n->a->kind == N_NAME || n->a->kind == N_TYPENAME) { // List(Int) bputf(b, "u%d;", TX_NAME); put_str(b, n->a->name); enc_list(b, &n->list, enc_type_item, 'T'); put_site(b, n->pos); return; } break; case N_TYPENAME: bputf(b, "u%d;", TX_NAME); put_str(b, n->a ? concat(concat(n->a->name, S(".")), n->name) : own_marked(n->name)); bput(b, S("l0;")); put_site(b, n->pos); return; case N_TNAME: if (!n->name.len && n->type) { // a type the checker filled in bputf(b, "u%d;", TX_NAME); put_str(b, type_name(n->type)); bput(b, S("l0;")); put_site(b, n->pos); return; } if (equals(n->name, S("$")) && n->a) { enc_splice(b, n->a, 't'); return; } bputf(b, "u%d;", TX_NAME); if (n->a && (n->flags & F_CALLED)) put_str(b, concat(concat(node_source(n->a), S(".")), n->name)); else put_str(b, n->c ? concat(concat(n->c->name, S(".")), n->name) : own_marked(n->name)); enc_list(b, &n->list, enc_type_item, 'T'); put_site(b, n->pos); return; case N_TFN: bputf(b, "u%d;", TX_FN); enc_list(b, &n->list, enc_type_item, 'T'); enc_type(b, n->a); put_site(b, n->pos); return; case N_TSTRUCT: bputf(b, "u%d;", TX_STRUCT); enc_params(b, &n->list); put_site(b, n->pos); return; case N_TUNION: bputf(b, "u%d;", TX_UNION); enc_params(b, &n->list); put_site(b, n->pos); bput(b, n->flags & F_NONIL ? S("b1") : S("b0")); return; case N_TMACRO: { Node *call = n->b ? n->b : n->a; bputf(b, "u%d;", TX_CALL); Buf c = {0}; enc_expr(&c, call); Str tag = fmtstr("u%d;", EX_CALL); if (!startswith(bstr(&c), tag)) errorat(n->pos, "this type is not a macro call"); bput(b, substr(bstr(&c), tag.len, c.len)); put_site(b, n->pos); return; } default: break; } // a value argument of a type bputf(b, "u%d;", TX_SIZE); enc_expr(b, n); put_site(b, n->pos); } // Names of a declaration interface (parameters, fields, variants) are never // renamed for hygiene; parameters of a closure are its locals. static void enc_param(Buf *b, Node *p) { if (!p->c && !enc_closure_params) put_str(b, p->name); else put_spliced_name(b, p->c, p->name); enc_type(b, p->a); enc_expr(b, p->b); bput(b, p->flags & F_VAR ? S("b1") : S("b0")); put_site(b, p->pos); bput(b, p->flags & F_PRIVATE ? S("b1") : S("b0")); } static void enc_params(Buf *b, Nodes *ps) { enc_list(b, ps, enc_param, 'P'); } static void enc_decl(Buf *b, Node *n) { if (n->src) n = n->src; b32 ct = (n->flags & F_MACRO) != 0, private = (n->flags & F_PRIVATE) != 0; switch ((int)n->kind) { case N_FN: bputf(b, "u%d;", DX_FUNCTION); put_spliced_name(b, n->c, n->name); enc_params(b, &n->list); enc_type(b, n->a); enc_body(b, n->b); put_site(b, n->pos); bputf(b, "b%d", ct); bputf(b, "b%d", private); bputf(b, "b%d", (n->flags & F_COMMAND) != 0); return; case N_STRUCT: case N_UNION: bputf(b, "u%d;", n->kind == N_STRUCT ? DX_STRUCT : DX_UNION); put_spliced_name(b, n->c, n->name); enc_params(b, &n->list); put_site(b, n->pos); bputf(b, "b%d", ct); bputf(b, "b%d", private); if (n->kind == N_UNION) bputf(b, "b%d", (n->flags & F_NONIL) != 0); return; case N_VAR: bputf(b, "u%d;", DX_GLOBAL); put_spliced_name(b, n->c, n->name); enc_type(b, n->a); enc_expr(b, n->b); bputf(b, "b%d", (n->flags & F_CONST) != 0); put_site(b, n->pos); bputf(b, "b%d", ct); bputf(b, "b%d", private); return; case N_IMPORT: bputf(b, "u%d;", DX_IMPORT); put_str(b, n->sval); put_str(b, n->name); enc_list(b, &n->list, enc_expr_item, 'E'); put_site(b, n->pos); bputf(b, "b%d", ct); return; case N_DECLS: bputf(b, "u%d;", DX_GROUP); enc_list(b, &n->list, enc_stmt_item, 'S'); put_site(b, n->pos); bputf(b, "b%d", ct); bputf(b, "b%d", private); return; case N_MLABEL: bputf(b, "u%d;", DX_MLABEL); put_str(b, n->name); enc_list(b, n->a ? &n->a->list : &(Nodes){0}, enc_stmt_item, 'S'); put_site(b, n->pos); return; case N_MODULE: bputf(b, "u%d;", DX_MODULE); enc_params(b, &n->list); put_site(b, n->pos); return; case N_COLLECTION: bputf(b, "u%d;", DX_COLLECTION); put_str(b, n->name); put_str(b, n->sval); put_site(b, n->pos); return; default: break; } errorat(n->pos, "this is not a declaration a macro can take"); } static void enc_case(Buf *b, Node *cs) { enc_list(b, &cs->list, enc_expr_item, 'E'); enc_body(b, cs->a); put_name(b, cs->name); put_site(b, cs->pos); } static void enc_stmt(Buf *b, Node *n) { if (n->src) n = n->src; Str label = {0}; if (n->kind == N_LABELED && n->a->kind == N_FOR && !n->c) { label = n->name; // label outer for ... n = n->a; } switch ((int)n->kind) { case N_FOR: bputf(b, "u%d;", SX_FOR); if (n->flags & F_IMPLICIT) put_str(b, (Str){0}); else put_name(b, n->sval.len ? n->sval : n->name); // the element enc_expr(b, n->a); enc_body(b, n->b); if (n->sval.len) put_name(b, n->name); // the index else put_str(b, (Str){0}); put_site(b, n->pos); put_name(b, label); bputf(b, "b%d", (n->flags & F_MACRO) != 0); bputf(b, "b%d", n->b && (n->b->flags & F_DO) && n->b->list.len == 1); if (n->flags & F_FILTER) enc_type(b, n->c); else bput(b, S("u0;")); return; case N_RETURN: bputf(b, "u%d;", SX_RETURN); enc_expr(b, n->a); put_site(b, n->pos); return; case N_ASSIGN: bputf(b, "u%d;", SX_ASSIGN); put_str(b, opname(n->op)); enc_expr(b, n->a); enc_expr(b, n->b); put_site(b, n->pos); return; case N_EXPR: if (n->a && n->a->kind == N_SPLICE && enc_template) { enc_splice(b, n->a, 's'); return; } bputf(b, "u%d;", SX_EXPR); enc_expr(b, n->a); return; case N_FN: case N_VAR: case N_STRUCT: case N_UNION: case N_IMPORT: case N_MLABEL: case N_MODULE: case N_COLLECTION: bputf(b, "u%d;", SX_DECL); enc_decl(b, n); return; case N_DECLS: if (n->flags & F_GROUP) { // var a, b: Int: its declarations bputf(b, "u%d;", SX_STATEMENTS); enc_list(b, &n->list, enc_stmt_item, 'S'); put_site(b, n->pos); return; } bputf(b, "u%d;", SX_DECL); enc_decl(b, n); return; case N_IF: bputf(b, "u%d;", SX_IF); enc_expr(b, n->a); enc_body(b, n->b); enc_body(b, n->c); put_site(b, n->pos); bputf(b, "b%d", (n->flags & F_MACRO) != 0); bputf(b, "b%d", n->b && (n->b->flags & F_DO) && n->b->list.len == 1); bputf(b, "b%d", n->c && (n->c->flags & F_DO) && n->c->list.len == 1); return; case N_WHILE: bputf(b, "u%d;", SX_WHILE); enc_expr(b, n->a); enc_body(b, n->b); put_site(b, n->pos); bputf(b, "b%d", n->b && (n->b->flags & F_DO) && n->b->list.len == 1); return; case N_BLOCK: bputf(b, "u%d;", SX_BLOCK); enc_body(b, n); put_site(b, n->pos); return; case N_BREAK: case N_CONTINUE: bputf(b, "u%d;", n->kind == N_BREAK ? SX_BREAK : SX_CONTINUE); put_spliced_name(b, n->c, n->name); put_site(b, n->pos); return; case N_DEFER: bputf(b, "u%d;", SX_DEFER); enc_stmt(b, n->a); put_site(b, n->pos); return; case N_DISCARD: bputf(b, "u%d;", SX_DISCARD); enc_expr(b, n->a); put_site(b, n->pos); return; case N_LABELED: bputf(b, "u%d;", SX_LABELED); put_spliced_name(b, n->c, n->name); enc_stmt(b, n->a); put_site(b, n->pos); return; case N_SWITCH: bputf(b, "u%d;", SX_SWITCH); enc_expr(b, n->a); put_name(b, n->flags & F_IMPLICIT ? (Str){0} : n->name); bputf(b, "b%d", (n->flags & F_REF) != 0); bputf(b, "l%lld;", (long long)n->list.len); for (iz i = 0; i < n->list.len; i++) enc_case(b, n->list.data[i]); put_site(b, n->pos); bputf(b, "b%d", (n->flags & F_MACRO) != 0); return; case N_CASE: bputf(b, "u%d;", SX_CASE); enc_case(b, n); return; default: if (!is_stmt_node(n) && !is_decl_node(n)) { bputf(b, "u%d;", SX_EXPR); enc_expr(b, n); return; } break; } errorat(n->pos, "this is not a statement a macro can take"); } // Whether an argument fits a macro parameter, and its encoding. static b32 enc_arg(Buf *b, Type *t, Node *a, Scope *s) { if (a->kind == N_NAMED) a = a->a; if (is_code_type(t)) { Str tname = t->name; if (equals(tname, S("Expr"))) { if (is_decl_node(a) || (is_stmt_node(a) && a->kind != N_EXPR) || is_type_node(a)) { if (a->kind != N_TYPENAME && a->kind != N_TYPEX) return 0; } enc_expr(b, a->kind == N_EXPR ? a->a : a); return 1; } if (equals(tname, S("TypeExpr"))) { if (!is_type_node(a) && a->kind != N_CALL) return 0; enc_type(b, a); return 1; } if (equals(tname, S("Decl"))) { if (!is_decl_node(a)) return 0; enc_decl(b, a); return 1; } if (equals(tname, S("Stmt"))) { enc_stmt(b, a); return 1; } if (equals(tname, S("Code"))) { if (is_type_node(a)) { bput(b, S("u2;")); enc_type(b, a); } else { bput(b, S("u1;")); enc_stmt(b, a); } return 1; } // one variant of Expr, Decl or Stmt (Call, Function, For, ...): the // argument encoded as that union, if it is that variant static Str unions[] = {SL("Expr"), SL("Decl"), SL("Stmt"), SL("TypeExpr")}; for (iz k = 0; k < countof(unions); k++) { Type *u = code_type(unions[k]); resolve_named(u); iz vi = -1; for (iz i = 0; i < u->fields.len; i++) { if (u->fields.data[i].type == t) vi = i; } if (vi < 0) continue; if (k == 0 && (is_decl_node(a) || is_type_node(a))) return 0; if (k == 1 && !is_decl_node(a)) return 0; if (k == 3 && !is_type_node(a) && a->kind != N_CALL && a->kind != N_NAME) return 0; Buf tmp = {0}; if (k == 0) enc_expr(&tmp, a->kind == N_EXPR ? a->a : a); else if (k == 1) enc_decl(&tmp, a); else if (k == 2) enc_stmt(&tmp, a); else enc_type(&tmp, a); Str tag = fmtstr("u%lld;", (long long)union_tag(u, vi)); if (!startswith(bstr(&tmp), tag)) return 0; bput(b, substr(bstr(&tmp), tag.len, tmp.len)); return 1; } return 0; } // constants known while compiling switch (t->text ? TY_STRING : t->kind) { case TY_INT: { i64 v = 0; if (!const_int(a, s, &v)) return 0; bputf(b, "i%lld;", (long long)v); return 1; } case TY_BOOL: if (a->kind != N_BOOL) return 0; bput(b, a->ival ? S("b1") : S("b0")); return 1; case TY_FLOAT: if (a->kind != N_FLOAT && a->kind != N_INT) return 0; bputf(b, "f%a;", a->kind == N_FLOAT ? a->fval : (f64)a->ival); return 1; case TY_STRING: if (a->kind == N_STR) { put_str(b, a->sval); return 1; } if (a->kind == N_NAME) { Sym *sym = find(s, a->name, a->pos, 0); if (sym && sym->kind == S_VAR && sym->isconst && sym->isglobal && sym->decl->b && sym->decl->b->kind == N_STR) { put_str(b, sym->decl->b->sval); return 1; } } return 0; case TY_LIST: if (a->kind != N_LIST) return 0; bputf(b, "l%lld;", (long long)a->list.len); for (iz i = 0; i < a->list.len; i++) { if (!enc_arg(b, t->elem, a->list.data[i], s)) return 0; } return 1; default: return 0; } } // The macro a call names, if any. In compile-time code a macro is an // ordinary function call, so there is nothing to expand. static Sym * macro_sym(Node *call, Scope *s) { if (in_macro_code || call->kind != N_CALL) return 0; Node *c = call->a; Sym *sym = 0; if (c->kind == N_NAME) { sym = c->sym ? c->sym : find(s, c->name, c->pos, 0); } else if (c->kind == N_FIELD && c->a->kind == N_NAME) { Sym *m = find(s, c->a->name, c->a->pos, 0); if (m && m->kind == S_MODULE) sym = scope_get(m->mod->scope, c->name); } if (!sym || sym->kind != S_FN) return 0; for (iz i = 0; i < sym->fns.len; i++) { if (sym->fns.data[i]->ismacro) return sym; } return 0; } // Typed arguments are expanded first, inside out. static void expand_inner(Node *n, Scope *s) { if (!n) return; if (n->kind == N_TEMPLATE || n->kind == N_FNLIT) return; Node *r = n->kind == N_CALL && macro_sym(n, s) ? expand(n, s, P_EXPR) : 0; if (r) { *n = *r; expand_inner(n, s); return; } if (n->kind == N_BLOCK || n->kind == N_FN || n->kind == N_FOR || n->kind == N_IF || n->kind == N_SWITCH || n->kind == N_BLOCKX) return; // statements expand when they are checked expand_inner(n->a, s); expand_inner(n->b, s); expand_inner(n->c, s); for (iz i = 0; i < n->list.len; i++) expand_inner(n->list.data[i], s); } static STATE b32 text_builtin = 0; // the text parse_text gets comes from a prelude macro static STATE i32 text_module = 0; // and 1 + the module of the macro that made it, or 0 // A macro if, for or switch in n, outside code templates static Node * compile_time_flow(Node *n) { if (!n || n->kind == N_TEMPLATE) return 0; if ((n->kind == N_IF || n->kind == N_FOR || n->kind == N_SWITCH) && (n->flags & F_MACRO)) return n; Node *r = compile_time_flow(n->a); if (!r) r = compile_time_flow(n->b); if (!r && n->kind != N_FOR) r = compile_time_flow(n->c); for (iz i = 0; !r && i < n->list.len; i++) r = compile_time_flow(n->list.data[i]); return r; } // Parses text a macro produced, as code for the place of its call. static Node * parse_text(Str text, int position, Pos site, Str macro) { Str path = fmtstr("%s:%d (generated by %s)", files.data[site.file].path, location(site).line, macro); // names the template wrote (marked by a 6 before them) resolve in the // module of the macro first Map *own = 0; if (text.len && memchr(text.data, 6, (size_t)text.len)) { Buf t = {0}; for (iz i = 0; i < text.len; i++) { if (text.data[i] == 6) *upsert(&own, fmtstr("%lld", (long long)t.len), 1) = (void *)1; else bputc(&t, text.data[i]); } text = bstr(&t); } i32 file = add_file(path, text, files.data[site.file].module); files.data[file].own_names = own; fileimports.data[file] = fileimports.data[site.file]; files.data[file].generated = 1; files.data[file].builtin = text_builtin; files.data[file].macro_module = text_module; files.data[file].origin = files.data[site.file].generated ? files.data[site.file].origin : site; Parser p = {0}; p.toks = lex(file); p.file = file; p.code_import = files.data[site.file].code_import; Node *result = 0; notes.len = 0; note(site, fmtstr("in code generated by %s here", macro)); while (pk(&p) == T_NEWLINE) next(&p); switch (position) { case P_EXPR: if (pk(&p) == T_EOF) errorat(site, "%s gave no expression", macro); result = parse_expr(&p); break; case P_TYPE: result = parse_type(&p); break; case P_STMT: { result = mknode(N_BLOCK, site); for (;;) { while (pk(&p) == T_NEWLINE || pk(&p) == T_SEMI) next(&p); if (pk(&p) == T_EOF) break; // a block of statements from code { ... } joins the call's scope if (pk(&p) == T_LBRACE && result->list.len == 0) { Node *blk = parse_block(&p); for (iz i = 0; i < blk->list.len; i++) addnode(&result->list, blk->list.data[i]); continue; } addnode(&result->list, parse_stmt(&p)); end_stmt(&p); } break; } default: { result = mknode(N_DECLS, site); for (;;) { while (pk(&p) == T_NEWLINE || pk(&p) == T_SEMI) next(&p); if (pk(&p) == T_EOF) break; if (pk(&p) == T_LBRACE && result->list.len == 0) { Node *g = parse_group(&p, 0, 1); for (iz i = 0; i < g->list.len; i++) addnode(&result->list, g->list.data[i]); continue; } addnode(&result->list, parse_decl(&p, 1)); end_stmt(&p); } break; } } while (pk(&p) == T_NEWLINE || pk(&p) == T_SEMI) next(&p); if (pk(&p) != T_EOF) errorat(peek(&p)->pos, "%s generated more than fits here", macro); Node *flow = compile_time_flow(result); if (flow) { // it would need a new metaprogram, which would lose the macro vars help_text = S("decide it in the macro: an if there chooses which code to return"); errorat(flow->pos, "code made by %s cannot use macro %s", macro, flow->kind == N_IF ? S("if") : flow->kind == N_FOR ? S("for") : S("switch")); } notes.len = 0; return result; } static iz macro_index(Fn *f) { for (iz i = 0; i < macro_fns.len; i++) { if (macro_fns.data[i] == f) return i; } return -1; } // A copy of a tree, every node its own. static Node * clone_tree(Node *n) { if (!n) return 0; Node *c = mknode(n->kind, n->pos); *c = *n; c->a = clone_tree(n->a); c->b = clone_tree(n->b); c->c = clone_tree(n->c); c->list = (Nodes){.multiline = n->list.multiline}; for (iz i = 0; i < n->list.len; i++) addnode(&c->list, clone_tree(n->list.data[i])); return c; } // The type an argument expression would have at the call, checked on a // fresh copy so the argument itself stays code. static Type * arg_type(Node *a, Scope *s) { if (a->kind == N_NAMED) a = a->a; if (is_decl_node(a) || is_type_node(a) || (is_stmt_node(a) && a->kind != N_EXPR)) return 0; Node *copy = clone_tree(a->kind == N_EXPR ? a->a : a); Type *t = check_expr(copy, s, 0); *upsert(&checked_args, ptrkey(a), 1) = copy; return t; } // Why the arguments of call do not fit macro f, for its note static Str macro_why(Fn *f, Node *call, Scope *s) { Node **bound = try_bind(f, &call->list); if (!bound) return bind_why; Fields *ps = &f->type->fields; for (iz j = 0; j < ps->len; j++) { Field *p = ps->data + j; Node *a = bound[j] ? bound[j] : p->def; Buf none = {0}; if (p->gives) { Type *got = arg_type(a, s); if (got != p->gives) return fmtstr("%s needs an expression giving a %s, but this gives a %s", p->name, type_name(p->gives), got ? type_name(got) : S("nothing")); } else if (!enc_arg(&none, p->type, a, s)) { return is_code_type(p->type) ? fmtstr("%s needs code that is a %s", p->name, type_name(p->type)) : fmtstr("%s needs a %s known while compiling", p->name, type_name(p->type)); } } return S("this one fits"); } // Runs the macro a call names and returns the code it produced, parsed for // the place of the call: an expression, a block of statements, declarations // or a type. static Node * expand(Node *call, Scope *s, int position) { Scope *save_scope = macro_scope; macro_scope = s; Sym *sym = macro_sym(call, s); Fn *chosen = 0; Buf args = {0}; for (iz pass = 0; pass < 3 && !chosen; pass++) { // Expr(T) parameters whose T fits rank first, Raw ones last for (iz i = 0; i < sym->fns.len && !chosen; i++) { Fn *f = sym->fns.data[i]; if (!f->ismacro) continue; fn_signature(f); Fields *ps = &f->type->fields; b32 anyraw = 0, anygives = 0; for (iz j = 0; j < ps->len; j++) { anyraw |= ps->data[j].israw; anygives |= ps->data[j].gives != 0; } if (pass != (anyraw ? 2 : anygives ? 0 : 1)) continue; Node **bound = try_fit(f, &call->list); if (!bound) continue; args.len = 0; b32 ok = 1; for (iz j = 0; j < ps->len && ok; j++) { Node *a = bound[j] ? bound[j] : ps->data[j].def; if (!ps->data[j].israw) expand_inner(a, s); if (ps->data[j].gives) ok = arg_type(a, s) == ps->data[j].gives; ok = ok && enc_arg(&args, ps->data[j].type, a, s); } if (ok) chosen = f; } } if (!chosen) { // functions sharing the name take the calls no macro fits for (iz i = 0; i < sym->fns.len; i++) { if (!sym->fns.data[i]->ismacro) { macro_scope = save_scope; return 0; } } Str why[8] = {0}; // worked out first: that checks code, which may reset notes for (iz i = 0; i < sym->fns.len && i < 8; i++) { if (sym->fns.data[i]->decl) why[i] = with_articles(macro_why(sym->fns.data[i], call, s)); } notes.len = 0; for (iz i = 0; i < sym->fns.len && i < 8; i++) { if (!sym->fns.data[i]->decl) continue; note(sym->fns.data[i]->decl->pos, why[i]); } errorat(call->pos, "these arguments do not fit the macro %s", sym->name); } macro_runs_here(); Node *save_call = macro_call; macro_call = call; Str text = meta_call(macro_index(chosen), &args, call->pos); macro_call = save_call; macro_scope = save_scope; if (position == P_EXPR && !text.len) errorat(call->pos, "%s produces no value here", sym->name); text_builtin = chosen->module >= 0 && modules.data[chosen->module] == prelude_module; text_module = chosen->module + 1; Node *r = parse_text(text, position, call->pos, sym->name); text_builtin = 0; text_module = 0; return r; } // Code placed by place(target, code), found by the label it names. static STATE Map *placed = 0; // target and text -> already placed static STATE Map *place_counts = 0; // position -> how much was placed there // Different placed code may not declare a name placed there before. static void check_placed_names(Str where, Node *r, Str text, Pos site) { for (iz k = 0; k < r->list.len; k++) { Node *d = r->list.data[k]; if (!d->name.len || (d->kind != N_FN && d->kind != N_VAR && d->kind != N_STRUCT && d->kind != N_UNION)) continue; void **slot = upsert(&placed, fmtstr("%s/%s", where, d->name), 1); Str *prev = *slot; if (prev && !equals(*prev, text)) errorat(site, "place puts different code named %s where it was placed before", d->name); if (!prev) { prev = new(&perm, 1, Str); *prev = text; *slot = prev; } } } // place(target, code). The target is sent as "position:path": start, end, // before or after, and a dotted path of enclosing regions (a label, a // binding, an enclosing function, a macro label, or file), maybe ending in a // part (body, cases). The region must enclose the macro call. typedef struct { Str names[3]; // what names it: a label, loop bindings, a function name Node *owner; // the construct: N_LABELED, N_FOR, N_WHILE, N_SWITCH, N_CASE, N_FN, N_MLABEL Node *stmt; // the statement in its parent list (before / after) Node *list; // the node whose list takes code (a block, the switch, decls) Scope *scope; // where placed code is checked (0: file scope) b32 cases; // the list holds switch cases b32 decls; // the list holds declarations } Region; static Scope * scope_of_block(Scope *s, Node *blk) { for (Scope *sc = s; sc; sc = sc->parent) if (sc->block == blk) return sc; return 0; } // The regions that enclose the call, innermost first. static iz enclosing_regions(Scope *s, Region *out, iz max) { iz n = 0; for (Scope *sc = s; sc && n < max; sc = sc->parent) { for (iz k = mlabels.len; k-- > 0 && n < max;) { if (mlabels.data[k].scope != sc) continue; Node *ml = mlabels.data[k].region; out[n++] = (Region){{ml->name}, ml, ml, ml->a, sc, 0, ml->a->kind == N_DECLS}; } Node *lp = sc->loop; if (lp && sc->kind == SC_LOOP && lp->kind == N_LABELED) lp = lp->a; // label w = for ... if (lp && sc->kind == SC_LOOP && (lp->kind == N_FOR || lp->kind == N_WHILE)) { Node *wrap = lp->info && ((Node *)lp->info)->kind == N_LABELED ? lp->info : 0; Region r = {{wrap ? wrap->name : (Str){0}}, lp, wrap ? wrap : lp, lp->b, 0, 0, 0}; if (lp->kind == N_FOR) r.names[1] = lp->name, r.names[2] = lp->sval; r.scope = scope_of_block(s, lp->b); out[n++] = r; } else if (lp && lp->kind == N_LABELED && lp->a->kind == N_BLOCK) { out[n++] = (Region){{lp->name}, lp, lp, lp->a, scope_of_block(s, lp->a), 0, 0}; } else if (lp && lp->kind == N_LABELED && lp->a->kind == N_SWITCH) { out[n++] = (Region){{lp->name}, lp->a, lp, lp->a, sc, 1, 0}; } else if (lp && lp->kind == N_SWITCH) { out[n++] = (Region){{lp->name}, lp, lp, lp, sc, 1, 0}; } else if (lp && lp->kind == N_CASE) { out[n++] = (Region){{lp->name}, lp, lp, lp->a, scope_of_block(s, lp->a), 0, 0}; } // a named function, nested ones too (a closure has no name) if (sc->kind == SC_FN && sc->fn && sc->fn->decl && sc->fn->decl->kind == N_FN && n < max) { Fn *f = sc->fn; out[n++] = (Region){ {f->name}, f->decl, f->decl, f->decl->b, scope_of_block(s, f->decl->b), 0, 0}; } } for (iz k = mlabels.len; k-- > 0 && n < max;) { // file-scope macro labels if (mlabels.data[k].scope) continue; Node *ml = mlabels.data[k].region; out[n++] = (Region){{ml->name}, ml, ml, ml->a, 0, 0, 1}; } return n; } static b32 region_named(Region *r, Str name) { for (int k = 0; k < 3; k++) if (r->names[k].len && equals(r->names[k], name)) return 1; return 0; } // The parts of a region, as the fields of its std:code type: the ones // that hold code can take placed code static b32 part_name(Str s) { static Str parts[] = {SL("params"), SL("result"), SL("body"), SL("source"), SL("condition"), SL("subject"), SL("cases")}; for (iz i = 0; i < countof(parts); i++) if (equals(s, parts[i])) return 1; return 0; } // What a placed piece of code is named by: no hygiene renaming, however the // code was built (name___N past every name the program has loses ___N) static Str unrenamed(Str t) { Buf b = {0}; for (iz i = 0; i < t.len;) { u8 c = t.data[i]; if (c == '"' && i + 2 < t.len && t.data[i + 1] == '"' && t.data[i + 2] == '"') { iz j = i + 3; // a raw string as it is while (j + 2 < t.len && !(t.data[j] == '"' && t.data[j + 1] == '"' && t.data[j + 2] == '"')) j++; while (j + 3 < t.len && t.data[j + 3] == '"') j++; // its quotes before the close j = j + 3 < t.len ? j + 3 : t.len; bput(&b, substr(t, i, j)); i = j; continue; } if (c == '"' || c == '\'') { // literals as they are iz j = i + 1; while (j < t.len && t.data[j] != c) j += t.data[j] == '\\' ? 2 : 1; j = j < t.len ? j + 1 : t.len; bput(&b, substr(t, i, j)); i = j; continue; } if (c == '#') { iz j = i; while (j < t.len && t.data[j] != '\n') j++; bput(&b, substr(t, i, j)); i = j; continue; } if (!isalphac(c)) { // letters and _ bputc(&b, c); i++; continue; } iz j = i; while (j < t.len && (isalphac(t.data[j]) || isdigitc(t.data[j]))) j++; Str w = substr(t, i, j); iz d = w.len; while (d > 0 && isdigitc(w.data[d - 1])) d--; if (d < w.len && d >= 4 && w.data[d - 1] == '_' && w.data[d - 2] == '_' && w.data[d - 3] == '_' && w.len - d <= 9 && parse_int(substr(w, d, w.len)) > fresh_base) w = substr(w, 0, d - 3); bput(&b, w); i = j; } return bstr(&b); } // Code placed before statements already checked is checked as if written // there: the names those later statements declare are not seen yet, and a // name it declares may not hide one they already used. static void placed_before(Node *holder, iz at, Node *placed, Scope *into) { Sym *hidden[256] = {0}; iz nh = 0; for (iz k = at + placed->list.len; k < holder->list.len && nh < 256; k++) { Node *st = holder->list.data[k]; if (st->kind != N_VAR || !st->sym) continue; void **slot = upsert(&into->names, st->name, 0); if (slot && *slot == st->sym) { hidden[nh++] = st->sym; *slot = 0; } } for (iz k = 0; k < placed->list.len; k++) { Node *d = placed->list.data[k]; if (d->kind == N_VAR && into->parent) { Sym *outer = find(into->parent, d->name, d->pos, 0); if (outer && outer->kind == S_VAR) { notes.len = 0; if (outer->decl) { note(outer->decl->pos, S("the one that code checked already uses")); } errorat( d->pos, "placed code declares %s, which would hide another %s for code checked already", d->name, d->name); } } check_stmt(d, into); } for (iz i = 0; i < nh; i++) { void **slot = upsert(&into->names, hidden[i]->name, 1); if (*slot) { Sym *mine = *slot; notes.len = 0; note(hidden[i]->pos, S("declared again here, after it")); errorat(mine->pos, "placed code declares %s, which this block declares later", hidden[i]->name); } *slot = hidden[i]; } } static void place_code(Str target, Str text, Pos site) { Scope *s = macro_scope; if (!s) errorat(site, "place() works only while a macro call is expanded"); text = unrenamed(text); iz colon = 0; while (colon < target.len && target.data[colon] != ':') colon++; Str position = substr(target, 0, colon), path = substr(target, colon + 1, target.len); // the path: names, the last maybe a part Str parts[16] = {0}; int np = 0; for (iz i = 0, j = 0; j <= path.len && np < 16; j++) { if (j == path.len || path.data[j] == '.') { parts[np++] = substr(path, i, j); i = j + 1; } } Str part = {0}; if (np > 1 && part_name(parts[np - 1])) part = parts[--np]; b32 at_file = np == 1 && equals(parts[0], S("file")); Region chain[64] = {0}; iz nchain = enclosing_regions(s, chain, 64); Region *r = 0; Region file = {{S("file")}, 0, 0, 0, 0, 0, 1}; if (at_file) { file.list = 0; // the module's declarations r = &file; } else { // the last name, innermost first; the ones before it further out iz at = -1; for (iz k = 0; k < nchain && at < 0; k++) if (region_named(&chain[k], parts[np - 1])) at = k; if (at < 0) { Sym *l = find(s, parts[np - 1], site, 0); if (l && (l->kind == S_LABEL || l->kind == S_MLABEL || l->kind == S_FN)) errorat( site, "place puts code only into a region around the macro call, and %s is not around it", parts[np - 1]); help_text = S("place puts code only into a label, macro label or function around the " "call, or into file"); errorat(site, "nothing around this macro call is named %s", parts[np - 1]); } for (int q = np - 2, k = (int)at + 1; q >= 0; q--, k++) { while (k < nchain && !region_named(&chain[k], parts[q])) k++; if (k >= nchain) errorat(site, "%s is not inside %s around this macro call", parts[q + 1], parts[q]); } r = &chain[at]; } if (part.len) { b32 ok = equals(part, S("body")) && !r->cases && (r->owner->kind == N_FN || r->owner->kind == N_FOR || r->owner->kind == N_WHILE || r->owner->kind == N_CASE); ok |= equals(part, S("cases")) && r->cases; if (!ok) { help_text = S("code goes into a body, the cases of a switch, or a block"); errorat(site, "%s.%s holds no code that place can add to", parts[0], part); } } b32 before = equals(position, S("before")), after = equals(position, S("after")); b32 end = equals(position, S("end")); // the list and where in it Node *holder = 0; Scope *into = r->scope; Module *m = modules.data[files.data[site.file].module]; iz index = 0; if (before || after) { if (at_file || r->owner->kind == N_FN || r->owner->kind == N_MLABEL || r->owner->kind == N_CASE) errorat(site, "before() and after() take a label of a loop, block or switch"); Scope *ps = 0; for (Scope *sc = s; sc && !ps; sc = sc->parent) if (sc->block && position_in(sc->block, r->stmt) >= 0) ps = sc; if (!ps) errorat(site, "%s is not in a block that code can be placed into", parts[np - 1]); holder = ps->block; into = ps; index = index_in(holder, r->stmt) + (after ? 1 : 0); } else if (at_file) { holder = 0; } else { holder = r->list; if (!r->cases && !r->decls && !into) errorat(site, "code can be placed in %s only from inside it", parts[np - 1]); index = end ? holder->list.len : 0; if (r->cases && index > 0 && !holder->list.data[index - 1]->list.len) index--; // cases go before default, which comes last } if (r->cases && !end) errorat(site, "cases are placed at end(%s): the switch is being checked", parts[np - 1]); Nodes *list = holder ? &holder->list : &m->decls; b32 decls = at_file || (r->decls && !before && !after); // one position, one order: each placement after the earlier ones there Str where = fmtstr("%s/%s/%lld", ptrkey(list), position, (long long)(before || after ? (iz)r->stmt : 0)); if (!first_time(&placed, fmtstr("%s:%s", where, text))) return; // placed again: nothing void **cslot = upsert(&place_counts, where, 1); if (!*cslot) *cslot = new(&perm, 1, iz); iz *placed_here = *cslot; Node *r2 = r->cases ? parse_text(fmtstr("switch placed__ {\n%s\n}", text), P_STMT, site, S("place")) : parse_text(text, decls ? P_DECL : P_STMT, site, S("place")); if (r->cases) r2 = r2->list.data[0]; // the switch: its cases check_placed_names(where, r2, text, site); // start: after what was placed at the start; after(l): after what was // placed after l; before(l) and end: right there, so each comes later if (!end && !before) index += *placed_here; *placed_here += r2->list.len; for (iz k = 0; k < r2->list.len; k++) r2->list.data[k]->flags |= F_PLACED; Nodes out = {0}; for (iz k = 0; k < index && k < list->len; k++) addnode(&out, list->data[k]); for (iz k = 0; k < r2->list.len; k++) addnode(&out, r2->list.data[k]); for (iz k = index; k < list->len; k++) addnode(&out, list->data[k]); *list = out; if (decls) { Scope *fs = at_file ? fileimports.data[site.file].scope : r->scope; if (!fs) fs = fileimports.data[r->owner->pos.file].scope; for (iz k = 0; k < r2->list.len; k++) { collect(m, r2->list.data[k], fs, 0); preload_imports(r2->list.data[k]); } for (iz k = 0; k < r2->list.len; k++) generated_overloads(r2->list.data[k]); return; } // before the statement being checked: checked now; after it, the block // reaches it in turn if (!end && !after && !r->cases) placed_before(holder, index, r2, into); } // Templates: code(...) and code { ... } build code values. The source text // is fixed when the macro is compiled; splices and the names the template // declares are filled in each time it runs. // it and it_index: the loop variables a for_expansion gives the body, so // templates keep them as written static b32 loop_name(Str name) { return equals(name, S("it")) || equals(name, S("it_index")); } static void collect_declared(Node *n, Map **names, b32 top) { if (!n) return; switch ((int)n->kind) { case N_VAR: if (!n->c && !loop_name(n->name)) *upsert(names, n->name, 1) = n; break; case N_FN: // its name and parameters are its interface: never renamed break; case N_FNLIT: for (iz i = 0; i < n->list.len; i++) *upsert(names, n->list.data[i]->name, 1) = n; break; case N_FOR: if (!(n->flags & F_IMPLICIT) && !loop_name(n->name)) *upsert(names, n->name, 1) = n; if (n->sval.len && !loop_name(n->sval)) *upsert(names, n->sval, 1) = n; break; case N_SWITCH: if (n->name.len && !(n->flags & F_IMPLICIT)) *upsert(names, n->name, 1) = n; break; case N_LABELED: // labels a template declares are its own, like its variables case N_CASE: if (n->name.len && !n->c) *upsert(names, n->name, 1) = n; break; case N_SPLICE: return; default: break; } collect_declared(n->a, names, 0); collect_declared(n->b, names, 0); collect_declared(n->c, names, 0); for (iz i = 0; i < n->list.len; i++) collect_declared(n->list.data[i], names, top && n->kind == N_TEMPLATE); } static Node * call_node(Node *callee, Pos p) { Node *c = mknode(N_CALL, p); c->a = callee; return c; } static Node * str_node(Str s, Pos p) { Node *n = mknode(N_STR, p); n->sval = s; return n; } // Templates: the template is encoded as the std:code value it builds, with // markers where the macro supplies something when it runs (see enc_splice). // The macro then builds the encoded text and decodes it: // { const template__sN = splice_expr(x); code__decode(Expr, str(...)) } static Str splice_converter(char kind) { switch (kind) { case 'e': return S("splice_expr"); case 't': return S("splice_type"); case 'n': return S("splice_name"); case 's': return S("splice_stmt"); case 'd': return S("splice_decl"); case 'E': return S("splice_exprs"); case 'S': return S("splice_stmts"); case 'P': return S("splice_params"); default: return S("splice_types"); } } static Node * call1(Sym *f, Node *arg, Pos p) { Node *c = call_node(name_node(f, p), p); addnode(&c->list, arg); return c; } static Node * plain_name(Str name, Pos p) { Node *n = mknode(N_NAME, p); n->name = name; return n; } static Node * plus_node(Node *a, Node *b, Pos p) { Node *n = mknode(N_BINARY, p); n->op = T_PLUS; n->a = a; n->b = b; return n; } static Node * int_node(i64 v, Pos p) { Node *n = mknode(N_INT, p); n->ival = v; return n; } // The value of a template as text with markers, for the code type kind. static void enc_template_value(Buf *b, Node *n, Str kind) { Pos p = n->pos; if (n->flags & F_DO) { Nodes *l = &n->list; b32 one = l->len == 1 && !item_splice(l->data[0], 'S'); Node *only = l->len == 1 ? l->data[0] : 0; if (equals(kind, S("Stmt")) || equals(kind, S("Code"))) { if (equals(kind, S("Code"))) bput(b, S("u1;")); if (one) { enc_stmt(b, only); } else { bputf(b, "u%d;", SX_STATEMENTS); enc_list(b, l, enc_stmt_item, 'S'); put_site(b, p); } return; } if (equals(kind, S("Decl")) && only) { if (!one) { enc_splice(b, only->a, 'd'); return; } if (is_decl_node(only)) { enc_decl(b, only); return; } } if (equals(kind, S("Expr")) && one && only->kind == N_EXPR) { enc_expr(b, only->a); return; } errorat(p, "this template does not hold one %s", kind); } Node *x = n->a; if (equals(kind, S("Expr"))) { enc_expr(b, x); } else if (equals(kind, S("TypeExpr"))) { enc_type(b, x); } else if (equals(kind, S("Decl"))) { if (x->kind == N_SPLICE) enc_splice(b, x, 'd'); else if (is_decl_node(x)) enc_decl(b, x); else errorat(p, "this template does not hold a declaration"); } else { // Stmt or Code if (equals(kind, S("Code"))) { if (is_type_node(x)) { bput(b, S("u2;")); enc_type(b, x); return; } bput(b, S("u1;")); } if (x->kind == N_SPLICE) { enc_splice(b, x, 's'); } else { bputf(b, "u%d;", SX_EXPR); enc_expr(b, x); } } } // check_template takes the bytes 1, 2 and 4 for marks of its own; a string // written in the template may hold them too. NORETURN static void template_mark(Pos p) { errorat(p, "a string in this template holds the byte 1, 2 or 4; write \\x01, \\x02 or \\x04"); } static Type * check_template(Node *n, Scope *s, Type *want) { need_code_module(n->pos); Pos p = n->pos; Str kind = want && is_code_type(want) ? want->name : (n->flags & F_DO) ? S("Stmt") : S("Expr"); if (!equals(kind, S("Expr")) && !equals(kind, S("Stmt")) && !equals(kind, S("Decl")) && !equals(kind, S("TypeExpr")) && !equals(kind, S("Code"))) errorat(p, "a template makes an Expr, Stmt, Decl, TypeExpr or Code, not a %s", kind); Map *names = 0; if (!(n->flags & F_RAW)) collect_declared(n, &names, 1); hyg_names = names; b32 save_enc = enc_template, save_own = template_own; Nodes save_splices = enc_splices; enc_template = 1; template_own = !(n->flags & F_RAW) && n->pos.file >= 0 && n->pos.file < files.len && main_module && files.data[n->pos.file].module != main_module->id; enc_splices = (Nodes){0}; enc_splice_kinds.len = 0; Buf b = {0}; enc_template_value(&b, n, kind); enc_template = save_enc; template_own = save_own; hyg_names = 0; Nodes splices = enc_splices; char *kinds = new(&perm, splices.len + 1, char); if (splices.len) memcpy(kinds, enc_splice_kinds.data, (size_t)splices.len); enc_splices = save_splices; // each splice once, in a constant of its own static STATE int count = 0; Str *consts = new(&perm, splices.len + 1, Str); for (iz i = 0; i < splices.len; i++) consts[i] = fmtstr("template__s%d", ++count); Node *strcall = call_node(name_node(builtin_sym(S("str")), p), p); Str text = bstr(&b); b32 suffix = 0; iz start = 0; for (iz i = 0; i <= text.len; i++) { b32 end = i == text.len; if (!end && text.data[i] != 1 && text.data[i] != 2 && text.data[i] != 4) continue; if (i > start) addnode(&strcall->list, str_node(copystr(&perm, substr(text, start, i)), p)); if (end) break; u8 m = text.data[i]; iz j = i + 1; u8 close = m == 1 ? 1 : m == 2 ? 3 : 4; while (j < text.len && text.data[j] != close) j++; if (j == text.len) template_mark(p); Str inside = substr(text, i + 1, j); if (m == 1) { // a splice: its encoded value iz idx = inside.len ? parse_int(substr(inside, 1, inside.len)) : -1; if (idx < 0 || idx >= splices.len) template_mark(p); char k = (char)inside.data[0]; Node *ref = plain_name(consts[idx], p); Node *enc = call1(builtin_sym(S("code__encode")), ref, p); if (k == 'B') // as many empty names as spliced arguments addnode(&strcall->list, call1(code_sym(S("blank_names")), call1(builtin_sym(S("len")), plain_name(consts[idx], p), p), p)); else if (oneof(k, S("ESPT"))) addnode(&strcall->list, call1(code_sym(S("items_text")), enc, p)); else addnode(&strcall->list, enc); } else if (m == 2) { // a declared name, with this run's suffix suffix = 1; addnode(&strcall->list, str_node(S("s"), p)); addnode(&strcall->list, plus_node(int_node(inside.len, p), call1(builtin_sym(S("len")), plain_name(S("template__suffix"), p), p), p)); addnode(&strcall->list, str_node(S(":"), p)); addnode(&strcall->list, str_node(copystr(&perm, inside), p)); addnode(&strcall->list, plain_name(S("template__suffix"), p)); } else { // the length of a list with spliced lists in it iz semi = 0; while (semi < inside.len && inside.data[semi] != ';') semi++; if (semi == inside.len) template_mark(p); Node *sum = int_node(parse_int(substr(inside, 0, semi)), p); for (iz k = semi + 1; k < inside.len;) { iz e = k; while (e < inside.len && inside.data[e] != ',') e++; iz idx = parse_int(substr(inside, k, e)); if (idx < 0 || idx >= splices.len) template_mark(p); sum = plus_node(sum, call1(builtin_sym(S("len")), plain_name(consts[idx], p), p), p); k = e + 1; } addnode(&strcall->list, str_node(S("l"), p)); addnode(&strcall->list, sum); addnode(&strcall->list, str_node(S(";"), p)); } start = j + 1; i = j; } if (!strcall->list.len) addnode(&strcall->list, str_node(S(""), p)); Node *decode = call_node(name_node(builtin_sym(S("code__decode")), p), p); Node *tx = mknode(N_TYPEX, p); tx->a = typed_node(code_type(kind), p); addnode(&decode->list, tx); addnode(&decode->list, strcall); Node *blk = mknode(N_BLOCK, p); if (suffix) { // const template__suffix = fresh("_") Node *decl = mknode(N_VAR, p); decl->name = S("template__suffix"); decl->flags = F_CONST; decl->b = call1(code_sym(S("fresh")), str_node(S("_"), p), p); addnode(&blk->list, decl); } for (iz i = 0; i < splices.len; i++) { Node *decl = mknode(N_VAR, p); decl->name = consts[i]; decl->flags = F_CONST; decl->b = call1(code_sym(splice_converter(kinds[i])), splices.data[i], p); addnode(&blk->list, decl); } Node *result = decode; if (blk->list.len) { Node *st = mknode(N_EXPR, p); st->a = decode; addnode(&blk->list, st); result = mknode(N_BLOCKX, p); result->a = blk; } *n = *result; return check_expr(n, s, code_type(kind)); } // Compile-time control flow: macro if, macro switch and macro for become // macros made from their own code. The macro collects the code of the // chosen branches, or of every step, and returns it all; compile-time // control flow nested in a body runs within the same macro. static void ct_steps(Nodes *body, Node *out, Pos p); static Node * ct_block(Node *blk, Pos p) { Node *b = mknode(N_BLOCK, p); if (blk) ct_steps(&blk->list, b, p); return b; } // add(¯o__steps, code { run }) static void ct_add(Node *out, Nodes *run, Pos p) { if (!run->len) return; Node *t = mknode(N_TEMPLATE, p); t->flags |= F_DO | F_RAW; // the program's own code: its names stay as written t->list = *run; Node *addc = call_node(name_node(builtin_sym(S("add")), p), p); Node *ref = mknode(N_ADDR, p); ref->a = mknode(N_NAME, p); ref->a->name = S("macro__steps"); addnode(&addc->list, ref); addnode(&addc->list, t); Node *st = mknode(N_EXPR, p); st->a = addc; addnode(&out->list, st); *run = (Nodes){0}; } // The statements of body as macro code: plain statements become code that is // added; compile-time control flow runs, with its bodies done the same way. static void ct_steps(Nodes *body, Node *out, Pos p) { Nodes run = {0}; for (iz i = 0; i < body->len; i++) { Node *n = body->data[i]; if (!((n->kind == N_IF || n->kind == N_FOR || n->kind == N_SWITCH) && (n->flags & F_MACRO))) { addnode(&run, n); continue; } ct_add(out, &run, p); Node *c = mknode(N_NONE, p); *c = *n; c->flags &= ~(u32)F_MACRO; if (n->kind == N_SWITCH) { c->list = (Nodes){0}; for (iz k = 0; k < n->list.len; k++) { Node *cs = mknode(N_CASE, n->list.data[k]->pos); *cs = *n->list.data[k]; cs->a = ct_block(n->list.data[k]->a, p); addnode(&c->list, cs); } } else { c->b = ct_block(n->b, p); if (n->kind == N_IF && n->c) c->c = ct_block(n->c, p); } addnode(&out->list, c); } ct_add(out, &run, p); } static void synth_compile_time(Node *n, Scope *fs) { need_code_module(n->pos); Pos p = n->pos; Node *d = mknode(N_FN, p); d->name = fmtstr("macro_%s_%d", n->kind == N_IF ? S("if") : n->kind == N_FOR ? S("for") : S("switch"), (int)macro_fns.len); d->flags = F_MACRO; d->a = typed_node(code_type(S("Code")), p); // var macro__steps: List(Code); ...; code { $macro__steps } Node *bd = mknode(N_BLOCK, p); Node *v = mknode(N_VAR, p); v->name = S("macro__steps"); v->a = typed_node(list_of(code_type(S("Code"))), p); addnode(&bd->list, v); Nodes one = {0}; addnode(&one, n); ct_steps(&one, bd, p); Node *all = mknode(N_TEMPLATE, p); all->flags |= F_DO; Node *sp = mknode(N_SPLICE, p); sp->a = mknode(N_NAME, p); sp->a->name = v->name; Node *spst = mknode(N_EXPR, p); spst->a = sp; addnode(&all->list, spst); Node *last = mknode(N_EXPR, p); last->a = all; addnode(&bd->list, last); d->b = bd; Fn *f = new_fn(d, fs, d->name); f->ismacro = 1; f->module = files.data[fs->file].module; *push(&perm, ¯o_fns) = f; n->info = f; } // macro if, for and switch run as the macro made from them. static Node * run_compile_time(Fn *f, Pos at, int position) { macro_runs_here(); Buf none = {0}; Str text = meta_call(macro_index(f), &none, at); return parse_text(text, position, at, S("this compile-time code")); } // Statement-position macros: a macro call or compile-time control flow is // replaced by the statements it produces, in the same scope. // In the code of a for_expansion, it and it_index become the names the loop // gives (for node in t: node); the body keeps its own names. In the body, // break node leaves the whole loop. static STATE iz index_uses = 0; // it_index in the code of a for_expansion static void loop_names(Node *n, Str elem, Str index, Str mark) { if (!n) return; if (n->kind == N_LABELED && equals(n->name, mark)) return; // the body Str *names[] = {&n->name, &n->sval}; for (int k = 0; k < 2; k++) { if (n->kind != N_NAME && n->kind != N_VAR && n->kind != N_FOR && k == 0) continue; if (equals(*names[k], S("it"))) *names[k] = elem; else if (equals(*names[k], S("it_index")) && index.len) { *names[k] = index; index_uses++; } } loop_names(n->a, elem, index, mark); loop_names(n->b, elem, index, mark); if (n->kind != N_FOR) loop_names(n->c, elem, index, mark); for (iz i = 0; i < n->list.len; i++) loop_names(n->list.data[i], elem, index, mark); } // break node and continue node in the loop body static void loop_jumps(Node *n, Str elem, Str label) { if (!n || n->kind == N_FNLIT) return; if ((n->kind == N_BREAK || n->kind == N_CONTINUE) && equals(n->name, elem)) { if (n->kind == N_CONTINUE) errorat(n->pos, "write continue alone to go on with this loop"); n->name = label; } loop_jumps(n->a, elem, label); loop_jumps(n->b, elem, label); if (n->kind != N_FOR) loop_jumps(n->c, elem, label); for (iz i = 0; i < n->list.len; i++) loop_jumps(n->list.data[i], elem, label); } static iz count_label(Node *n, Str label) { if (!n) return 0; iz c = n->kind == N_LABELED && equals(n->name, label); c += count_label(n->a, label) + count_label(n->b, label) + count_label(n->c, label); for (iz i = 0; i < n->list.len; i++) c += count_label(n->list.data[i], label); return c; } // for x in v, for a type of v with a macro fn for_expansion(v: Expr(T), // body: Code): the code it returns, in a block labeled as the loop is (or // with a fresh label), so break and break x leave it static Node * for_expansion(Node *st, Scope *s) { Node *loop = st->kind == N_LABELED ? st->a : st; if (loop->kind != N_FOR || (loop->flags & F_MACRO) || loop->a->kind == N_RANGE) return 0; Sym *fe = find(s, S("for_expansion"), loop->pos, 0); if (!fe || fe->kind != S_FN) return 0; Type *t = arg_type(loop->a, s); Fn *fits = 0; for (iz i = 0; t && i < fe->fns.len; i++) { Fn *f = fe->fns.data[i]; fn_signature(f); if (f->ismacro && f->type->fields.len == 2 && f->type->fields.data[0].gives == t) fits = f; } if (!fits) return 0; if (t->kind == TY_LIST || t->kind == TY_ARRAY || t->kind == TY_MAP || t->kind == TY_POOL) errorat(fits->decl->pos, "loops over a %s are built in; a for_expansion cannot change them", type_name(t)); static STATE int count = 0; count++; // names no program declares (past every name___N it has) Str label = st->kind == N_LABELED ? st->name : fmtstr("for___%lld", fresh_base + 1000000 + count); Str mark = fmtstr("body___%lld", fresh_base + 1000000 + count); Str elem = loop->sval.len ? loop->sval : loop->name, index = loop->sval.len ? loop->name : S(""); loop_jumps(loop->b, elem, label); if (index.len) loop_jumps(loop->b, index, label); // both names label the loop Node *body = mknode(N_LABELED, loop->b->pos); body->name = mark; body->a = loop->b; Node *call = call_node(name_node(fe, loop->pos), loop->pos); addnode(&call->list, loop->a); addnode(&call->list, body); Node *r = expand(call, s, P_STMT); if (!r || count_label(r, mark) != 1) errorat(loop->pos, "the for_expansion of %s must splice $body exactly once", type_name(t)); index_uses = 0; loop_names(r, elem, index, mark); if (index.len && !index_uses) { help_text = fmtstr("write for %s in ...", elem); errorat(loop->pos, "the for_expansion of %s gives no index, so it takes one name", type_name(t)); } Node *blk = mknode(N_BLOCK, st->pos); blk->list = r->list; Node *out = mknode(N_LABELED, st->pos); out->name = label; out->flags |= F_EXPANDED; out->a = blk; return out; } static b32 expand_stmt_at(Node *blk, iz *at, Scope *s) { Node *st = blk->list.data[*at]; if (!in_macro_code) { Node *fe = for_expansion(st, s); if (fe) { blk->list.data[*at] = fe; return 1; } } Scope *save_scope = macro_scope; macro_scope = s; Node *r = 0; if (st->kind == N_EXPR && st->a->kind == N_CALL && macro_sym(st->a, s) && (r = expand(st->a, s, P_STMT))) { } else if ((st->kind == N_IF || st->kind == N_FOR || st->kind == N_SWITCH) && (st->flags & F_MACRO) && !in_macro_code) { r = run_compile_time(st->info, st->pos, P_STMT); } else { macro_scope = save_scope; return 0; } macro_scope = save_scope; if (r->list.len && r->list.data[0]->pos.file >= 0 && files.data[r->list.data[0]->pos.file].builtin) { // assert(...), unreachable(): --expand prints the call as written, // as the prelude macro is not the program's own code Node *call = mknode(N_EXPR, st->pos); *call = *st; r->list.data[0]->src = call; for (iz k = 1; k < r->list.len; k++) r->list.data[k]->flags |= F_HIDDEN; } iz i = *at = index_in(blk, st); // code placed meanwhile may have moved it replace_stmts(blk, i, 1, &r->list); return 1; } // Overloads a macro just made take effect now: print, valid and arbitrary // register, and none of them, nor an operator, may reach back. static void generated_overloads(Node *d) { if (d->kind == N_DECLS) { for (iz i = 0; i < d->list.len; i++) generated_overloads(d->list.data[i]); return; } if (d->kind != N_FN || !d->info || (d->flags & F_MACRO)) return; Fn *f = d->info; if (f->ismacro) return; if (f->module >= 0 && f->module < modules.len && !f->isnested && !f->isclosure) { Sym *same = scope_get(modules.data[f->module]->scope, f->name); Module *save = curmod; curmod = modules.data[f->module]; if (same && same->kind == S_FN && same->fns.len > 1) same_overloads(same); curmod = save; } register_special(f); static Str ops[] = {SL("+"), SL("-"), SL("*"), SL("/"), SL("%"), SL("**"), SL("=="), SL("<"), SL("in")}; for (iz k = 0; k < countof(ops); k++) { if (!equals(f->name, ops[k])) continue; Module *save = curmod; curmod = modules.data[f->module]; fn_signature(f); curmod = save; for (iz i = 0; i < f->type->fields.len; i++) no_backward_overload(f, fmtstr("op%s", ops[k]), f->type->fields.data[i].type, fmtstr("is used with %s", ops[k])); } } // One file-scope macro call or compile-time control flow: its declarations // take its place in list, from where the in-order pass goes on. static void run_file_macro(Module *m, Nodes *list, iz at) { Node *d = list->data[at]; Scope *fs = 0; for (iz i = 0; i < file_macros.len && !fs; i++) if (file_macros.data[i] == d) fs = file_macro_scopes.data[i]; Module *save = curmod; curmod = m; macro_scope = fs; Node *r = 0; if (d->kind == N_EXPR) { if (d->a->kind != N_CALL || !macro_sym(d->a, fs)) errorat(d->pos, "only declarations and macro calls may appear at file scope"); r = expand(d->a, fs, P_DECL); if (!r) errorat(d->pos, "only declarations and macro calls may appear at file scope"); } else { r = run_compile_time(d->info, d->pos, P_DECL); } Nodes out = {0}; for (iz k = 0; k < list->len; k++) { if (list->data[k] != d) { // by identity: code placed meanwhile moves it addnode(&out, list->data[k]); continue; } for (iz j = 0; j < r->list.len; j++) addnode(&out, r->list.data[j]); } *list = out; for (iz k = 0; k < r->list.len; k++) { collect(m, r->list.data[k], fs, 0); preload_imports(r->list.data[k]); } for (iz k = 0; k < r->list.len; k++) generated_overloads(r->list.data[k]); curmod = save; } static b32 is_file_macro(Node *d) { for (iz i = 0; i < file_macros.len; i++) if (file_macros.data[i] == d) return 1; return 0; } // The declarations of list in source order: a function body is checked // where it is, a macro call runs where it is (what it makes comes next). static STATE iz decls_handled = 0; static void check_decls_in_order(Module *m, Nodes *list) { for (iz i = 0; i < list->len;) { Node *d = list->data[i]; // code placed meanwhile may move what comes before: handled ones // are skipped, so nothing runs twice void **done = upsert(&decls_done, ptrkey(d), 1); if (*done) { i++; continue; } *done = d; decls_handled++; if (is_file_macro(d)) { run_file_macro(m, list, i); i = 0; // what it made takes its place: go on from there continue; } if (d->kind == N_DECLS) { check_decls_in_order(m, &d->list); } else if (d->kind == N_MLABEL) { *push(&perm, &mlabels) = (MLabel){d, 0}; check_decls_in_order(m, &d->a->list); mlabels.len--; } else if (d->kind == N_FN && d->info && !(d->flags & F_MACRO)) { Fn *f = d->info; if (!f->isnested && !f->isclosure && !f->ismacro) { curmod = modules.data[f->module]; register_special(f); at_place = f; check_fn(f); at_place = 0; if (fuzz_targets_wanted && m->ismain) { // a fuzz target: its types are built for (iz k = 0; k < f->type->fields.len; k++) record_use(S("fuzz"), f->type->fields.data[k].type, f->decl->pos, 0); record_use(S("fuzz"), f->type->elem, f->decl->pos, 0); } } } else if (d->kind == N_VAR && d->sym) { check_global(d->sym); } i++; } } // Imported modules first, then the module, each once. static void check_module_in_order(Module *m) { if (!m || m->checked) return; m->checked = 1; for (iz i = 0; i < m->deps.len; i++) check_module_in_order(m->deps.data[i]); Module *save = curmod; curmod = m; // again while code placed before the current declaration is new there for (iz before = -1; before != decls_handled;) { before = decls_handled; check_decls_in_order(m, &m->decls); } curmod = save; } static void end_with_return(Node *blk); // star99 --expand and expanded(b): the main module with every macro // expanded and placed code in place, printed as star99 fmt prints. static Str expanded_text(void) { printing_expanded = 1; Buf b = {0}; for (iz i = 0; i < main_module->header.len; i++) { if (main_module->header.data[i]->flags & F_MACRO) continue; print_node(&b, main_module->header.data[i], 0); bputc(&b, '\n'); } for (iz i = 0; i < main_module->decls.len; i++) { Node *d = main_module->decls.data[i]; if (d->flags & F_MACRO) continue; // compile-time code has done its work iz before = b.len; if (b.len) bputc(&b, '\n'); if (d->kind == N_FN && d->a && d->b && !(d->flags & F_DO)) end_with_return(d->b); iz start = b.len; Node *only[1] = {d}; Nodes one = {.data = only, .len = 1, .cap = 1}; print_fit(&b, &one, 0, 0); if (b.len == start) { // an empty macro label: no lines b.len = before; continue; } bputc(&b, '\n'); } printing_expanded = 0; return bstr(&b); } // C plugins // A plugin is a module with C in it, written with one built-in, c, in two // places. At file scope, c("""...""", effect: "...") is C its functions // share (includes, types, helpers, state), and what they do. As a whole // body, fn name(...) -> T do c("""...""") is a function written in C: its // parameters are C variables of their names, and its C returns its result. // Only numbers, Bool, String and List(U8) cross, so no pointer reaches // Star99 code: a plugin gives handles as structs with a private Int and // errors as unions, in Star99. A program carries a C function only when it // calls it, and the C of a module only when it carries one of its C // functions. What the C needs to compile and link comes from the module's // compile hook, macro fn compile(var b: Build), which runs only then. typedef struct { Nodes headers; // its c(...) at file scope, in file order Str effect; // what its C functions do, unless one says otherwise Pos effect_at; // where it says so b32 has_effect; } CModule; static STATE struct { CModule **data; iz len; iz cap; } c_modules = {0}; // by module id: 0 for a module without C // The modules whose C the program generate() made last carries, by id static STATE b32 *c_reached = 0; static STATE iz c_nreached = 0; // c_build_fields: std:build has c_flags and link, as it was loaded for a // program with C static CModule * c_module(i32 id, b32 make) { if (id < 0) return 0; while (make && c_modules.len <= id) *push(&perm, &c_modules) = 0; if (id >= c_modules.len) return 0; if (make && !c_modules.data[id]) c_modules.data[id] = new(&perm, 1, CModule); return c_modules.data[id]; } // c("...", ...): a call of the name c whose first argument is a string static b32 is_c_call(Node *call) { return call && call->kind == N_CALL && call->a && call->a->kind == N_NAME && equals(call->a->name, S("c")) && call->list.len >= 1 && call->list.data[0]->kind == N_STR; } // Whether the string token k of a file is the C of a c(...): not one of the // program's words, which fuzzing and Build.strings take. Only a module with // C has any; elsewhere c(...) is a call of the program's own c. static b32 c_text_at(i32 file, Toks t, iz k) { return c_module(files.data[file].module, 0) && k >= 2 && t.data[k].kind == T_STRING && t.data[k - 1].kind == T_LPAREN && t.data[k - 2].kind == T_NAME && equals(t.data[k - 2].text, S("c")); } // Modules that declare a c of their own (a function, macro or variable): // their c(...) is a call of it, never C static STATE struct { b32 *data; iz len; iz cap; } c_own = {0}; static b32 declares_c(Node *d) { Nodes *in = d->kind == N_DECLS ? &d->list : d->kind == N_MLABEL && d->a ? &d->a->list : 0; for (iz i = 0; in && i < in->len; i++) if (declares_c(in->data[i])) return 1; return (d->kind == N_FN || d->kind == N_VAR) && equals(d->name, S("c")); } // Before a module's declarations are collected: whether it has its own c, // wherever in it that is declared static void c_note_own(Module *m, Nodes *perfile, iz npaths) { for (iz i = 0; i < npaths; i++) { for (iz j = 0; j < perfile[i].len; j++) { if (!declares_c(perfile[i].data[j])) continue; while (c_own.len <= m->id) *push(&perm, &c_own) = 0; c_own.data[m->id] = 1; } } } static b32 c_owned(i32 id) { return id >= 0 && id < c_own.len && c_own.data[id]; } // EffectKind in a program with C gains plugin: String, after the kinds of // std:code (so their tags stay), for what plugins do. A program without C // has std:code as it is. Done once std:code's declarations are collected, // and when checking starts (std:code may come before the modules with C), // before its types are resolved. static void c_effect_kind(Module *m) { if (!m || !m->isstd || !equals(m->key, S("std:code"))) return; b32 any = 0; for (iz i = 0; i < c_modules.len; i++) any |= c_modules.data[i] != 0; Sym *s = scope_get(m->scope, S("EffectKind")); if (!any || !s || s->kind != S_TYPE || !s->type->decl) return; Node *d = s->type->decl; for (iz i = 0; i < d->list.len; i++) if (equals(d->list.data[i]->name, S("plugin"))) return; if (s->type->resolved || s->type->resolving) fatal("EffectKind was resolved before the plugins were seen; this is a bug in star99"); Node *v = mknode(N_PARAM, d->pos); v->name = S("plugin"); v->a = mknode(N_TNAME, d->pos); v->a->name = S("String"); v->flags |= F_HASTYPE; addnode(&d->list, v); assert(d->list.len == C_PLUGIN_EFFECT); } // c(...) anywhere but at file scope or as a whole body NORETURN static void c_misplaced(Node *call) { help_text = S("write fn name(...) -> T do c(\"\"\"...\"\"\"), or c(\"\"\"...\"\"\") at file scope"); errorat(call->pos, "c(...) is C: it is the whole body of a function, or C at file scope"); } // The effect a c(...) names (a string literal), or 0. Its C comes first, // then only effect: "name". static Node * c_effect_arg(Node *call) { Node *effect = 0; for (iz i = 1; i < call->list.len; i++) { Node *a = call->list.data[i]; help_text = S("write c(\"\"\"...\"\"\", effect: \"name\")"); if (a->kind != N_NAMED) errorat(a->pos, "c takes its C, then only effect: \"name\""); if (!equals(a->name, S("effect"))) { help_text = S( "it takes effect; flags come from a compile hook, macro fn compile(var b: Build)"); errorat(a->pos, "c has no parameter %s", a->name); } if (effect) errorat(a->pos, "effect is given twice"); if (a->a->kind != N_STR) errorat(a->a->pos, "effect is a string literal: c(...) is read while compiling"); // a name, as effects(f) reports it: .plugin("network") Str s = a->a->sval; b32 ok = 1; for (iz k = 0; k < s.len; k++) { u8 c = s.data[k]; ok &= (c >= 'a' && c <= 'z') || c == '_' || (k > 0 && c >= '0' && c <= '9'); } if (!ok) { help_text = S("write a name such as effect: \"network\", or effect: \"\" for none"); errorat(a->a->pos, "an effect is a lowercase name"); } effect = a->a; } help_text = (Str){0}; return effect; } // c("""...""", effect: "...") at file scope: C of the module. Says whether d // is one. static b32 c_header(Module *m, Node *d) { Node *call = d->kind == N_EXPR ? d->a : 0; if (c_owned(m->id)) return 0; // a call of its own c if (call && call->kind == N_CALL && call->a && call->a->kind == N_NAME && equals(call->a->name, S("c")) && !is_c_call(call)) { help_text = S("write c(\"\"\"#include <...>\"\"\")"); errorat(call->pos, "c takes its C first, as a string literal: it is read while compiling"); } if (!call || !is_c_call(call)) return 0; CModule *cm = c_module(m->id, 1); Node *e = c_effect_arg(call); if (e) { if (cm->has_effect && !equals(cm->effect, e->sval)) { notes.len = 0; note(cm->effect_at, S("the other one")); errorat(e->pos, "this module gives its C functions two effects; a function can say " "its own: do c(\"\"\"...\"\"\", effect: \"...\")"); } cm->effect = e->sval; cm->effect_at = e->pos; cm->has_effect = 1; } addnode(&cm->headers, d); return 1; } // Whether f is a function written in C: fn name(...) -> T do c("""..."""), // in a module with C. Its checked body becomes its C text, a string, so // what reads bodies finds nothing there; it still prints as written. static b32 c_function(Fn *f) { if (f->c_seen) return f->c_code != 0; f->c_seen = 1; Node *d = f->decl; if (!d || d->kind != N_FN || f->isnested || f->isclosure || f->module < 0) return 0; Node *b = d->b; if (!b || b->kind != N_BLOCK || !(b->flags & F_DO) || b->list.len != 1) return 0; Node *st = b->list.data[0]; if (st->kind != N_EXPR || !is_c_call(st->a)) return 0; if (c_owned(f->module) || find(f->scope, S("c"), st->a->pos, 0)) return 0; // its own c if (!c_module(f->module, 0)) { help_text = S("declare the C it uses at file scope: c(\"\"\"#include <...>\"\"\")"); errorat(st->a->pos, "a function written in C needs C in its module"); } if (d->flags & F_MACRO) errorat(d->pos, "a macro runs while compiling, in Star99; it cannot be written in C"); if (d->flags & F_COROUTINE) errorat(d->pos, "a coroutine cannot be written in C"); if (d->op) { help_text = S("call a C function from it"); errorat(d->pos, "an operator cannot be written in C"); } (void)c_effect_arg(st->a); Node *code = new(&perm, 1, Node); *code = *st->a; f->c_code = code; Node *text = mknode(N_STR, st->a->pos); text->sval = st->a->list.data[0]->sval; text->type = t_string; replace_node(st->a, text); return 1; } // What a function written in C does: its own effect, or its module's static Str c_effect(Fn *f) { Node *e = c_effect_arg(f->c_code); if (e) return e->sval; CModule *cm = c_module(f->module, 0); return cm ? cm->effect : (Str){0}; } // The types a C function takes and gives: numbers, Bool, String, List(U8) static b32 c_crosses(Type *t) { if (t->kind == TY_INT || t->kind == TY_FLOAT || t->kind == TY_BOOL) return 1; return t->kind == TY_LIST && t->elem && t->elem->kind == TY_INT && t->elem->bits == 8 && !t->elem->issigned; } // A parameter name the C can use as a variable: no C keyword or macro, // nothing of the runtime's static b32 c_name_ok(Str name) { if (startswith(name, S("s99_")) || startswith(name, S("S99_"))) return 0; for (iz i = 0; i < countof(c_words); i++) { Str w = c_words[i]; for (iz k = 0; k < w.len;) { iz e = k; while (e < w.len && w.data[e] != ' ') e++; if (equals(substr(w, k, e), name)) return 0; k = e + 1; } } return 1; } // A function written in C is checked by its signature alone. static void check_c_function(Fn *f) { fn_signature(f); Node *d = f->decl; Fields *ps = &f->type->fields; for (iz i = 0; i < ps->len; i++) { Field *p = ps->data + i; Node *pn = i < d->list.len ? d->list.data[i] : d; if (p->isvar) errorat(pn->pos, "a function written in C takes values, so %s cannot be var", p->name); if (!c_crosses(p->type)) { help_text = S("pass the parts it needs, or an Int that stands for a C object"); errorat(pn->pos, "a function written in C takes numbers, Bool, String and List(U8), " "not %s", type_name(p->type)); } if (!c_name_ok(p->name)) errorat(pn->pos, "%s means something in C, so it cannot name a parameter of a " "function written in C", p->name); } Type *rt = f->type->elem; if (rt != t_void && !c_crosses(rt)) { help_text = S("give the parts one by one, or an Int that stands for a C object"); errorat(d->a ? d->a->pos : d->pos, "a function written in C gives a number, Bool, String or List(U8), not %s", type_name(rt)); } f->checked = 1; } // The lines of C text written in a string at s, with #line directives in // debug builds (a raw string of many lines starts on the line after its """) static void c_text_lines(Buf *b, Node *s, Str text, Str indent) { Str dir = debugging() ? line_directive(s->pos) : (Str){0}; i32 line = 0; if (dir.len) { line = location(s->pos).line; Str file = files.data[s->pos.file].text; iz at = s->pos.offset; if (at < file.len && startswith(substr(file, at, file.len), S("\"\"\"\n"))) line++; } for (iz i = 0; i < text.len;) { iz e = i; while (e < text.len && text.data[e] != '\n') e++; if (dir.len) bputf(b, "#line %d %s\n", line++, debug_path(s->pos.file)); if (e > i) bput(b, indent); bput(b, substr(text, i, e)); bputc(b, '\n'); i = e + 1; } } static void gen_c_function(Fn *f) { Type *rt = f->type->elem; define_type(rt); Fields *ps = &f->type->fields; Buf params = {0}; for (iz i = 0; i < ps->len; i++) { define_type(ps->data[i].type); bputf(¶ms, "%s%s %s", i ? S(", ") : S(""), ctype(ps->data[i].type), ps->data[i].name); } if (!ps->len) bput(¶ms, S("void")); Str sig = fmtstr("%s(%s)", f->cname, bstr(¶ms)); bputf(&out_protos, "static S99_UNUSED %s %s;\n", ctype(rt), sig); Buf fb = {0}; Str dir = debugging() ? line_directive(f->decl->pos) : (Str){0}; if (dir.len) bputf(&fb, "%s\n", dir); bputf(&fb, "static %s\n", ctype(rt)); if (dir.len) bputf(&fb, "%s\n", dir); bputf(&fb, "%s\n", sig); if (dir.len) bputf(&fb, "%s\n", dir); bput(&fb, S("{\n")); for (iz i = 0; i < ps->len; i++) bputf(&fb, " (void)%s;\n", ps->data[i].name); if (meta_mode && !f->ctused) { // the metaprogram never runs it: none of its C bputf(&fb, " s99_fail(\"star99\", \"%s runs only at run time\");\n", f->name); if (rt != t_void) bputf(&fb, " return %s;\n", gen_zero(rt)); } else { c_text_lines(&fb, f->c_code->list.data[0], f->c_code->list.data[0]->sval, S(" ")); if (rt->managed) addnode(&f->gen_allocs, mknode(N_NIL, f->decl->pos)); // it makes one } bput(&fb, S("}\n")); bput(&out_fns, bstr(&fb)); } // A module as the C names it: its directory, or the file of a one-file program static Str c_module_name(i32 m) { Module *mod = modules.data[m]; if (!equals(mod->name, S(".")) || !mod->fileids.len) return mod->name; return basename_of(files.data[mod->fileids.data[0]].path); } // The C of the modules whose C functions the program carries, which goes // after the runtime; c_summary names them, for the top of the C. static Str c_section(void) { c_reached = new(&perm, modules.len + 1, b32); c_nreached = modules.len; for (iz i = 0; i < all_fns.len; i++) { Fn *f = all_fns.data[i]; if (f->c_code && f->emitted && (!meta_mode || f->ctused) && f->module >= 0) c_reached[f->module] = 1; } Buf b = {0}, names = {0}; for (iz m = 0; m < modules.len; m++) { CModule *cm = c_module((i32)m, 0); if (!c_reached[m] || !cm) continue; bputf(&names, "%s%s", names.len ? S(", ") : S(""), c_module_name((i32)m)); for (iz k = 0; k < cm->headers.len; k++) { Node *s = cm->headers.data[k]->a->list.data[0]; bputf(&b, "\n/* From %s */\n", files.data[s->pos.file].path); c_text_lines(&b, s, s->sval, S("")); } } c_summary = names.len ? fmtstr("/* C plugins: %s */\n", bstr(&names)) : (Str){0}; return bstr(&b); } // Debug builds give C functions, globals and types their Star99 names // (send, accept) where nothing else in the file has them; the headers a // plugin's C includes declare more than the runtime's, so what the C // compiler preprocesses the C of the program's plugins into is taken too: // its identifiers, and its macros. static CHeaderWords c_header_words(void) { CHeaderWords r = {0}; Buf src = {0}; for (iz m = 0; m < c_nreached && m < modules.len; m++) { CModule *cm = c_module((i32)m, 0); for (iz k = 0; c_reached[m] && cm && k < cm->headers.len; k++) { bput(&src, cm->headers.data[k]->a->list.data[0]->sval); bputc(&src, '\n'); } } if (!src.len) return r; Str cc = os_getenv(S("STAR99_CC")); if (!cc.len) cc = S("cc"); Str dir = program_dir(); Str in = join_path(dir, S("c-headers.c")), out = join_path(dir, S("c-headers.i")); Str defs = join_path(dir, S("c-headers.h")); if (!os_writefile(in, concat(S("#define _DEFAULT_SOURCE\n"), bstr(&src)))) return r; Str q = shell_quote(in); i32 failed = os_system(fmtstr("%s -E -P %s > %s 2>/dev/null && %s -E -dM %s > %s 2>/dev/null", cc, q, shell_quote(out), cc, q, shell_quote(defs))); Contents words = failed ? (Contents){0} : os_readfile(out); Contents ms = failed ? (Contents){0} : os_readfile(defs); os_remove(in); os_remove(out); os_remove(defs); if (!words.ok) words.data = bstr(&src); // what its own text names, at least u8 *more = name_bytes(); for (iz i = 0; i < words.data.len;) { if (!isalphac(words.data.data[i])) { i++; continue; } iz e = i; while (e < words.data.len && more[words.data.data[e]]) e++; *push(&perm, &r.names) = substr(words.data, i, e); i = e; } for (iz i = 0; ms.ok && i < ms.data.len; i++) { // #define NAME if (i && ms.data.data[i - 1] != '\n') continue; Str rest = substr(ms.data, i, ms.data.len); if (!startswith(rest, S("#define "))) continue; iz e = 8; while (e < rest.len && more[rest.data[e]]) e++; if (e > 8) *push(&perm, &r.macros) = substr(rest, 8, e); } return r; } // effects(f): a function written in C does what it says, by name: one of // the kinds of std:code (files, output, ...), or a plugin's own static void add_c_effect(Access *a, Fn *f) { Str e = c_effect(f); if (!e.len) return; static Str kinds[] = {SL("output"), SL("input"), SL("files"), SL("run"), SL("environment"), SL("exit"), SL("fail")}; int tag = C_PLUGIN_EFFECT; // its site an N_STR of its name for (iz k = 0; k < countof(kinds); k++) if (equals(e, kinds[k])) tag = (int)k + 1; *push(&perm, &a->effect_kinds) = tag; Node *n = mknode(N_STR, f->decl->pos); n->sval = e; addnode(&a->effect_sites, n); } // Compile hooks. macro fn compile(var b: Build) in a module with C adds what // that C needs to build: entries of b.c_flags (for compiling: -I, -D) and // b.link (for linking: -l, -L). It runs when the program carries C of the // module, after the C is generated and before the build: the program's own // build(b), or the C compiler. // The compile hook of module m, or 0 static Fn * c_compile_hook(i32 m) { if (!c_module(m, 0)) return 0; Sym *s = scope_get(modules.data[m]->scope, S("compile")); if (!s || s->kind != S_FN) return 0; Fn *f = 0; for (iz i = 0; i < s->fns.len; i++) if (s->fns.data[i]->ismacro) f = s->fns.data[i]; if (!f) return 0; fn_signature(f); Sym *bt = prelude_sym(S("Build")); Fields *ps = &f->type->fields; if (s->fns.len != 1 || ps->len != 1 || !ps->data[0].isvar || !bt || ps->data[0].type != bt->type || f->type->elem != t_void) { help_text = S("write macro fn compile(var b: Build) { ... }"); errorat(f->decl->pos, "a compile hook adds to the build what the C of its module needs"); } return f; } // Whether m, a macro, is a compile hook (its text function is made here) static b32 is_compile_hook(Fn *m) { return m->module >= 0 && equals(m->name, S("compile")) && c_compile_hook(m->module) == m; } // The function the metaprogram calls for a compile hook: the Build after // the hook, encoded. macro fn compile__text(b: Build) -> String { var // built__ = b; compile(&built__); code__encode(built__) } static Fn * c_hook_text_fn(Fn *m) { Pos p = m->decl->pos; Node *pn = m->decl->list.data[0]; Node *d = mknode(N_FN, p); d->name = fmtstr("%s__text", m->name); d->flags = F_MACRO; d->a = typed_node(t_string, p); Node *np = mknode(N_PARAM, pn->pos); np->name = pn->name; np->a = typed_node(m->type->fields.data[0].type, pn->pos); addnode(&d->list, np); Node *blk = mknode(N_BLOCK, p); Node *v = mknode(N_VAR, p); v->name = S("built__"); v->b = mknode(N_NAME, p); v->b->name = pn->name; addnode(&blk->list, v); Sym *msym = mksym(S_FN, m->name, p); *push(&perm, &msym->fns) = m; Node *call = call_node(name_node(msym, p), p); Node *addr = mknode(N_ADDR, p); addr->a = mknode(N_NAME, p); addr->a->name = v->name; addnode(&call->list, addr); Node *st = mknode(N_EXPR, p); st->a = call; addnode(&blk->list, st); Node *built = mknode(N_NAME, p); built->name = v->name; Node *st2 = mknode(N_EXPR, p); st2->a = call1(builtin_sym(S("code__encode")), built, p); addnode(&blk->list, st2); d->b = blk; Fn *f = new_fn(d, m->scope, d->name); f->module = m->module; f->ismacro = 1; return f; } typedef struct { i32 *data; iz len; iz cap; } ModuleIds; // The modules in import order, a module's imports before it: order with // those m brings that are not yet seen static ModuleIds c_module_order(Module *m, b32 *seen, ModuleIds order) { if (seen[m->id]) return order; seen[m->id] = 1; for (iz i = 0; i < m->deps.len; i++) order = c_module_order(m->deps.data[i], seen, order); *push(&perm, &order) = m->id; return order; } // A part of an encoded value, and where the next one starts typedef struct { Str value; iz next; } Taken; // The decimal digits of e at at static Taken c_take_count(Str e, iz at) { Taken r = {0}; iz i = at; while (i < e.len && isdigitc(e.data[i])) i++; r.value = substr(e, at, i); r.next = i; return r; } // One string of an encoded value at at: s5:hello static Taken c_take_str(Str e, iz at) { Taken r = {0}; r.next = at; if (at >= e.len || e.data[at] != 's') return r; Taken n = c_take_count(e, at + 1); iz len = parse_int(n.value), i = n.next; if (i >= e.len || e.data[i] != ':' || len > e.len - i - 1) return r; r.value = substr(e, i + 1, i + 1 + len); r.next = i + 1 + len; return r; } // A list of strings of an encoded value at at, encoded again with each // entry a run of words: line breaks and tabs become spaces, and an empty // entry goes (so a flag a hook gave from shell() is one line of a command) static Taken c_take_strs(Str e, iz at) { Taken r = {0}; r.value = S("l0;"); r.next = at; if (at >= e.len || e.data[at] != 'l') return r; Taken n = c_take_count(e, at + 1); iz count = parse_int(n.value), kept = 0; r.next = n.next + 1; Buf items = {0}; for (iz k = 0; k < count; k++) { Taken w = c_take_str(e, r.next); r.next = w.next; Buf t = {0}; for (iz j = 0; j < w.value.len; j++) { u8 c = w.value.data[j]; if (c == '\n' || c == '\r' || c == '\t') c = ' '; if (c != ' ' || (t.len && t.data[t.len - 1] != ' ')) bputc(&t, c); } while (t.len && t.data[t.len - 1] == ' ') t.len--; if (!t.len) continue; put_str(&items, bstr(&t)); kept++; } r.value = concat(fmtstr("l%lld;", (long long)kept), bstr(&items)); return r; } // The entries of an encoded list of strings, each after a space static Str c_words_of(Str list) { Buf b = {0}; if (!list.len || list.data[0] != 'l') return (Str){0}; Taken n = c_take_count(list, 1); iz at = n.next + 1; for (iz k = parse_int(n.value); k > 0; k--) { Taken w = c_take_str(list, at); bputf(&b, " %s", w.value); at = w.next; } return bstr(&b); } // An encoded list of strings, read from the metaprogram static Str c_read_strs(void) { Buf b = {0}; if (rd() != 'l') meta_died(); i64 n = rd_num(); bputf(&b, "l%lld;", (long long)n); for (i64 k = 0; k < n; k++) put_str(&b, rd_str()); return bstr(&b); } // Runs the compile hooks of the modules whose C the program carries (and, // in a program with build(), the C of every function with_main may start a // program at), in import order, each given the Build the one before made. // arg is the Build, encoded up to its strings; the hooks fill c_flags_used // and c_link_used, encoded lists of strings. static void run_compile_hooks(Str arg, b32 with_build) { c_flags_used = c_link_used = S("l0;"); b32 any = 0; for (iz i = 0; i < c_modules.len; i++) any |= c_modules.data[i] != 0; if (!any) return; // no C, no hooks b32 *want = new(&perm, modules.len + 1, b32); for (iz m = 0; m < c_nreached && m < modules.len; m++) want[m] = c_reached[m]; if (with_build) { Scope *ms = main_module->scope; for (iz i = 0; i < all_fns.len; i++) { Fn *f = all_fns.data[i]; if (f->module != main_module->id || f->ismacro || f->isnested || f->isclosure || !f->checked || !f->type || f->type->fields.len || f->type->elem != t_void || !scope_get(ms, f->name)) continue; Fns r = fn_reaches(f, f->decl->pos, S("with_main"), 0); for (iz k = 0; k < r.len; k++) if (r.data[k]->c_code && r.data[k]->module >= 0) want[r.data[k]->module] = 1; } } b32 *seen = new(&perm, modules.len + 1, b32); ModuleIds order = c_module_order(main_module, seen, (ModuleIds){0}); for (iz k = 0; k < order.len; k++) { Fn *hook = want[order.data[k]] ? c_compile_hook(order.data[k]) : 0; if (!hook) continue; if (!c_build_fields) fatal("std:build lacks c_flags and link; this is a bug in star99"); Buf b = {0}; bput(&b, arg); bput(&b, c_flags_used); bput(&b, c_link_used); Str enc = meta_call(macro_index(hook), &b, hook->decl->pos); // the Build it made: c_file, sources, name, strings, c_flags, link iz at = c_take_str(enc, 0).next; at = c_take_strs(enc, at).next; at = c_take_str(enc, at).next; at = c_take_strs(enc, at).next; Taken flags = c_take_strs(enc, at); c_flags_used = flags.value; c_link_used = c_take_strs(enc, flags.next).value; } } // std:build, with what plugins add, for a program that has C: Build gains // c_flags and link, which command() and the fuzzing library pass to the C // compiler. A program without C gets std:build as it is, so its // metaprogram (and so its own C, which numbers its temporaries after the // metaprogram's) does not change. static Str c_build_text(Str text) { b32 any = 0; for (iz i = 0; i < c_modules.len; i++) any |= c_modules.data[i] != 0; if (!any) return text; c_build_fields = 1; static Str edits[][2] = { {SL("# .star99 file of the program, the program's default name, and every string\n" "# literal in it.\n"), SL("# .star99 file of the program, the program's default name, and every string\n" "# literal in it; and what the C plugins it uses need to compile (c_flags)\n" "# and link (link), which their compile hooks give.\n")}, {SL(" strings: List(String),\n)\n"), SL(" strings: List(String),\n c_flags, link: List(String),\n)\n")}, {SL(" extra,\n \" \",\n"), SL(" extra,\n words(b.c_flags),\n \" \",\n")}, {SL(" cc.libs,\n )\n"), SL(" cc.libs,\n words(b.link),\n )\n")}, {SL(" const flags = \"-std=c99 -g -O2 -w -DSTAR99_FUZZ \"\n"), SL(" const flags = str(\"-std=c99 -g -O2 -w -DSTAR99_FUZZ\", words(b.c_flags), \" \")\n")}, {SL(" \"/plain -lm\",\n"), SL(" str(\"/plain -lm\", words(b.link)),\n")}, {SL(" \"/asan -lm\",\n"), SL(" str(\"/asan -lm\", words(b.link)),\n")}, {SL(" \"/cmplog -lm\",\n"), SL(" str(\"/cmplog -lm\", words(b.link)),\n")}, }; for (iz k = 0; k < countof(edits); k++) { iz at = indexof(text, edits[k][0]); assert(at >= 0); text = concat(concat(substr(text, 0, at), edits[k][1]), substr(text, at + edits[k][0].len, text.len)); } // flags as words of a command, each after a space return concat(text, S("\nprivate fn words(xs: List(String)) -> String {\n" " var out = \"\"\n" " for x in xs do add(&out, \" \", x)\n" " return out\n" "}\n")); } // star99 tree // star99 tree FILE prints FILE as star99 fmt writes it, then its syntax // tree, for tools that work on code by its parts (the projectional editor // edits a file by replacing, inserting and deleting the text of nodes): // // star99-tree 1 // text N // the formatted text: N bytes, then a line break // nodes M // PARENT KIND FIELD LO HI one line per node: node k is line k // // Node 0 is the File, its PARENT -1. KIND is the std:code type of a node // (Function, Param, Call, Name, Binary, If, ...), or File, Comment, or // Named (an argument given by name). FIELD says which part of its parent // it is, named as in std:code (body, params, arguments, condition, ...), // and name for the name of a declaration, a field or a binding, which are // Name nodes too. LO and HI are the byte offsets of its text, HI past its // end. A node's children are the nodes whose PARENT it is, in source // order. A file that does not parse prints its error, and the status is 1. typedef struct { i32 parent; Str kind, field; i32 lo, hi; } TreeNode; static STATE struct { TreeNode *data; iz len; iz cap; } tree = {0}; static STATE Toks tree_toks = {0}; // The token starting at or after offset at static iz tree_tok(i32 at) { iz lo = 0, hi = tree_toks.len; while (lo < hi) { iz mid = (lo + hi) / 2; if (tree_toks.data[mid].pos.lo < at) lo = mid + 1; else hi = mid; } return lo; } static i32 tree_add(i32 parent, Str kind, Str field, Pos p) { TreeNode t = {0}; t.parent = parent; t.kind = kind; t.field = field; t.lo = p.hi > p.lo ? p.lo : p.offset; t.hi = p.hi > p.lo ? p.hi : p.offset; if (t.hi <= t.lo) { // the token there iz k = tree_tok(t.lo); if (k < tree_toks.len && tree_toks.data[k].pos.lo == t.lo) t.hi = tree_toks.data[k].pos.hi; } *push(&perm, &tree) = t; return (i32)(tree.len - 1); } static b32 tree_open(TokKind k) { return k == T_LPAREN || k == T_LBRACKET || k == T_LBRACE; } static b32 tree_close(TokKind k) { return k == T_RPAREN || k == T_RBRACKET || k == T_RBRACE; } // Node k spans its own text and its children's. A statement reaches back // over the words before it on its line (private, fn, var, parenless(...) // and the like) and on to the end of its line; anything else takes the // closing brackets of those it opens. static void tree_span(i32 k, b32 stmt) { TreeNode *t = tree.data + k; for (iz j = k + 1; j < tree.len; j++) { TreeNode *c = tree.data + j; if (c->parent != k) continue; if (c->lo < t->lo) t->lo = c->lo; if (c->hi > t->hi) t->hi = c->hi; } iz first = tree_tok(t->lo); if (stmt) { while (first > 0) { Tok *b = tree_toks.data + first - 1; TokKind bk = b->kind; b32 head = bk == T_PRIVATE || bk == T_MACRO || bk == T_FN || bk == T_STRUCT || bk == T_UNION || bk == T_VAR || bk == T_CONST; if (bk == T_NAME && (equals(b->text, S("parenless")) || equals(b->text, S("coroutine")) || equals(b->text, S("no_nil")))) head = 1; if (bk == T_LPAREN && first > 1 && tree_toks.data[first - 2].kind == T_NAME && (equals(tree_toks.data[first - 2].text, S("parenless")) || equals(tree_toks.data[first - 2].text, S("coroutine")) || equals(tree_toks.data[first - 2].text, S("no_nil")))) head = 1; if (!head) break; first--; t->lo = b->pos.lo; } } int depth = 0; iz k2 = first; for (; k2 < tree_toks.len && tree_toks.data[k2].pos.lo < t->hi; k2++) { if (tree_open(tree_toks.data[k2].kind)) depth++; if (tree_close(tree_toks.data[k2].kind) && depth > 0) depth--; } for (; k2 < tree_toks.len; k2++) { Tok *tk = tree_toks.data + k2; if (depth > 0) { if (tree_open(tk->kind)) depth++; if (tree_close(tk->kind)) depth--; if (tk->pos.hi > tk->pos.lo) t->hi = tk->pos.hi; continue; } // an if's else is its own, even an empty one (else {}); a statement in // it ends before b32 own_else = tk->kind == T_ELSE && equals(t->kind, S("If")); if (!stmt || tk->kind == T_NEWLINE || tk->kind == T_SEMI || tk->kind == T_EOF || (tk->kind == T_ELSE && !own_else) || tk->kind == T_CASE || tk->kind == T_DEFAULT || tree_close(tk->kind)) break; if (tree_open(tk->kind)) depth++; if (tk->pos.hi > tk->pos.lo) t->hi = tk->pos.hi; } } // The token of a name in node k that no child of it holds: a Name node static void tree_name(i32 k, Str name, Str field) { if (!name.len || equals(name, S("$"))) return; TreeNode t = tree.data[k]; for (iz j = tree_tok(t.lo); j < tree_toks.len && tree_toks.data[j].pos.lo < t.hi; j++) { Tok *tk = tree_toks.data + j; // a string's text is what it holds: import "net" as net names net once if (tk->kind == T_STRING || !equals(tk->text, name)) continue; b32 inside = 0; for (iz c = k + 1; c < tree.len && !inside; c++) inside = tree.data[c].parent == k && tree.data[c].lo <= tk->pos.lo && tk->pos.lo < tree.data[c].hi; if (inside) continue; Pos p = tk->pos; tree_add(k, S("Name"), field, p); return; } } static i32 tree_walk(Node *n, i32 parent, Str field, b32 stmt); // The type and default of a parameter that shares them with the next (a, // b: Int) belong to the next static STATE b32 tree_shared = 0; // A list of statements (a block's, or one after do), or of other nodes static void tree_list(Nodes *l, i32 parent, Str field, b32 stmts) { for (iz i = 0; i < l->len; i++) { Node *n = l->data[i]; if (n->kind == N_VAR && (n->flags & F_GROUP)) continue; // var a, b: one statement tree_shared = n->kind == N_PARAM && i + 1 < l->len && l->data[i + 1]->kind == N_PARAM && l->data[i + 1]->a == n->a && l->data[i + 1]->b == n->b && (n->a || n->b); i32 k = tree_walk(n, parent, field, stmts); // the other names of var a, b: T, which come after it for (iz j = i + 1; n->kind == N_VAR && j < l->len; j++) { Node *m = l->data[j]; if (m->kind != N_VAR || !(m->flags & F_GROUP)) break; tree_name(k, m->name, S("name")); } } } // A body: the statements of a block, or the one after do static void tree_body(Node *b, i32 parent, Str field) { if (!b) return; if (b->kind == N_BLOCK) { tree_list(&b->list, parent, field, 1); return; } tree_walk(b, parent, field, 1); } static Str tree_kind(Node *n) { switch ((int)n->kind) { case N_INT: case N_FLOAT: case N_STR: case N_BOOL: case N_NIL: return S("Literal"); case N_NAME: return S("Name"); case N_TYPENAME: case N_TNAME: return S("TypeName"); case N_CALL: return S("Call"); case N_NAMED: return S("Named"); case N_FIELD: return S("Field"); case N_INDEX: return S("Index"); case N_DEREF: return S("Deref"); case N_ADDR: return S("Addr"); case N_UNARY: return S("Unary"); case N_BINARY: case N_IN: return S("Binary"); case N_RANGE: return n->flags & F_IMPLICIT ? S("Unary") : S("Binary"); case N_PAIR: return S("Entry"); case N_ANON: return S("Anonymous"); case N_VARIANT: return S("Variant"); case N_BLOCKX: return S("BlockValue"); case N_IFX: return S("IfValue"); case N_FNLIT: return S("Closure"); case N_TEMPLATE: return S("Template"); case N_SPLICE: return S("Splice"); case N_TYPEX: return S("TypeValue"); case N_TFN: return S("FunctionType"); case N_TSTRUCT: return S("StructType"); case N_TUNION: return S("UnionType"); case N_TMACRO: return S("TypeCall"); case N_VAR: return S("Global"); case N_ASSIGN: return S("Assign"); case N_DISCARD: return S("Discard"); case N_IF: return S("If"); case N_FOR: return S("For"); case N_WHILE: return S("While"); case N_BLOCK: return S("Block"); case N_RETURN: return S("Return"); case N_BREAK: return S("Break"); case N_CONTINUE: return S("Continue"); case N_DEFER: return S("Defer"); case N_LABELED: return S("Labeled"); case N_SWITCH: case N_SWITCHX: return S("Switch"); case N_CASE: return S("Case"); case N_MLABEL: return S("MacroLabel"); case N_FN: return S("Function"); case N_PARAM: return S("Param"); case N_STRUCT: return S("Struct"); case N_UNION: return S("Union"); case N_IMPORT: return S("Import"); case N_MODULE: return S("Module"); case N_COLLECTION: return S("Collection"); case N_DECLS: return S("Group"); default: return S("Node"); } } static i32 tree_walk(Node *n, i32 parent, Str field, b32 stmt) { if (!n) return -1; if (n->kind == N_EXPR) // an expression as a statement is that expression return tree_walk(n->a, parent, field, stmt); b32 shared = tree_shared; tree_shared = 0; if (n->kind == N_SWITCHX) n = n->a; Str kind = tree_kind(n); if (n->kind == N_LIST) { b32 map = (n->flags & F_MAP) != 0; for (iz i = 0; i < n->list.len; i++) map |= n->list.data[i]->kind == N_PAIR; kind = map ? S("MapLiteral") : S("ListLiteral"); } i32 k = tree_add(parent, kind, field, n->pos); switch ((int)n->kind) { case N_FN: tree_list(&n->list, k, S("params"), 0); tree_walk(n->a, k, S("result"), 0); tree_body(n->b, k, S("body")); break; case N_FNLIT: tree_list(&n->list, k, S("params"), 0); tree_walk(n->a, k, S("result"), 0); if (n->b && (n->b->flags & F_DO)) // inside an expression: not to the end of the line tree_list(&n->b->list, k, S("body"), 0); else tree_body(n->b, k, S("body")); break; case N_STRUCT: case N_TSTRUCT: tree_list(&n->list, k, S("fields"), 0); break; case N_UNION: case N_TUNION: tree_list(&n->list, k, S("variants"), 0); break; case N_MODULE: tree_list(&n->list, k, S("params"), 0); break; case N_PARAM: case N_VAR: if (shared) break; tree_walk(n->a, k, S("type"), 0); tree_walk(n->b, k, S("value"), 0); break; case N_IMPORT: tree_list(&n->list, k, S("arguments"), 0); break; case N_DECLS: tree_list(&n->list, k, S("body"), 1); break; case N_MLABEL: tree_body(n->a, k, S("body")); break; case N_ASSIGN: tree_walk(n->a, k, S("target"), 0); tree_walk(n->b, k, S("value"), 0); break; case N_DISCARD: case N_RETURN: tree_walk(n->a, k, S("value"), 0); break; case N_DEFER: tree_body(n->a, k, S("body")); break; case N_LABELED: tree_body(n->a, k, S("body")); break; case N_IF: tree_walk(n->a, k, S("condition"), 0); tree_body(n->b, k, S("then")); tree_body(n->c, k, S("otherwise")); break; case N_IFX: tree_walk(n->a, k, S("condition"), 0); tree_walk(n->b, k, S("then"), 0); tree_walk(n->c, k, S("otherwise"), 0); break; case N_FOR: if (n->flags & F_FILTER) tree_walk(n->c, k, S("variant"), 0); tree_walk(n->a, k, S("source"), 0); tree_body(n->b, k, S("body")); break; case N_WHILE: tree_walk(n->a, k, S("condition"), 0); tree_body(n->b, k, S("body")); break; case N_BLOCK: tree_list(&n->list, k, S("body"), 1); break; case N_BLOCKX: tree_body(n->a, k, S("body")); break; case N_SWITCH: tree_walk(n->a, k, S("value"), 0); tree_list(&n->list, k, S("cases"), 1); // each case to the end of its line break; case N_CASE: tree_list(&n->list, k, S("patterns"), 0); tree_body(n->a, k, S("body")); break; case N_CALL: tree_walk(n->a, k, S("callee"), 0); tree_list(&n->list, k, S("arguments"), 0); break; case N_NAMED: tree_walk(n->a, k, S("value"), 0); break; case N_FIELD: tree_walk(n->a, k, S("base"), 0); break; case N_INDEX: tree_walk(n->a, k, S("base"), 0); tree_list(&n->list, k, S("indices"), 0); break; case N_DEREF: case N_ADDR: case N_UNARY: tree_walk(n->a, k, S("operand"), 0); break; case N_BINARY: case N_IN: case N_RANGE: if (n->kind == N_RANGE && (n->flags & F_IMPLICIT)) { tree_walk(n->b, k, S("operand"), 0); } else { tree_walk(n->a, k, S("left"), 0); tree_walk(n->b, k, S("right"), 0); } break; case N_LIST: tree_list(&n->list, k, n->list.len && n->list.data[0]->kind == N_PAIR ? S("entries") : S("items"), 0); break; case N_PAIR: tree_walk(n->a, k, S("key"), 0); tree_walk(n->b, k, S("value"), 0); break; case N_ANON: case N_VARIANT: tree_list(&n->list, k, S("arguments"), 0); break; case N_TEMPLATE: if (n->flags & F_DO) tree_list(&n->list, k, S("body"), 1); else tree_walk(n->a, k, S("body"), 0); break; case N_SPLICE: tree_walk(n->a, k, S("value"), 0); break; case N_TYPEX: tree_walk(n->a, k, S("type"), 0); break; case N_TNAME: tree_list(&n->list, k, S("arguments"), 0); break; case N_TFN: tree_list(&n->list, k, S("params"), 0); tree_walk(n->a, k, S("result"), 0); break; case N_TMACRO: tree_walk(n->a, k, S("call"), 0); break; default: break; } tree_span(k, stmt); // a parameter's var or private, before the first name of its group (the // others of var a, b: T share them) if (n->kind == N_PARAM && (n->flags & (F_VAR | F_PRIVATE))) { for (iz first = tree_tok(tree.data[k].lo); first > 0; first--) { TokKind bk = tree_toks.data[first - 1].kind; if (bk != T_VAR && bk != T_PRIVATE) break; tree.data[k].lo = tree_toks.data[first - 1].pos.lo; } } // the names it declares or uses, as Name nodes switch ((int)n->kind) { case N_FN: case N_STRUCT: case N_UNION: case N_PARAM: case N_COLLECTION: case N_MLABEL: case N_LABELED: case N_FIELD: case N_VARIANT: case N_NAMED: tree_name(k, n->name, S("name")); break; case N_VAR: tree_name(k, n->name, S("name")); for (iz i = 0; i < n->list.len; i++) tree_name(k, n->list.data[i]->name, S("name")); break; case N_BREAK: case N_CONTINUE: tree_name(k, n->name, S("target")); break; case N_CASE: tree_name(k, n->name, S("case_label")); break; case N_IMPORT: tree_name(k, n->name, S("alias")); break; case N_SWITCH: if (!(n->flags & F_IMPLICIT)) tree_name(k, n->name, S("binding")); break; case N_FOR: if (!(n->flags & F_IMPLICIT)) { if (n->sval.len) tree_name(k, n->name, S("index")); tree_name(k, n->sval.len ? n->sval : n->name, S("binding")); } break; default: break; } return k; } // The innermost node whose text holds [lo, hi) static i32 tree_around(i32 lo, i32 hi) { i32 best = 0; for (iz j = 1; j < tree.len; j++) { TreeNode *t = tree.data + j; if (t->lo <= lo && hi <= t->hi && (t->hi - t->lo) < (tree.data[best].hi - tree.data[best].lo)) best = (i32)j; } return best; } UNUSED static int run_tree(Strs args) { if (args.len != 3) { os_write(2, S("usage: star99 tree FILE\n")); return 2; } Str path = args.data[2]; Contents in = os_readfile(path); // "-" is the standard input if (!in.ok) fatal("cannot read %s", path); Str out = format_file(path, in.data); i32 f = add_file(path, out, 0); Nodes decls = parse_file(f); tree_toks = lex(f); tree.len = 0; Pos all = {f, 0, 0, (i32)out.len}; tree_add(-1, S("File"), S(""), all); tree.data[0].lo = 0; tree.data[0].hi = (i32)out.len; tree_list(&decls, 0, S("decls"), 1); SourceFile *sf = files.data + f; for (iz i = 0; i < sf->comments.len; i++) { i32 lo = sf->comments.data[i], hi = lo; while (hi < out.len && out.data[hi] != '\n') hi++; Pos p = {f, lo, lo, hi}; tree_add(tree_around(lo, hi), S("Comment"), S("comment"), p); } Buf b = {0}; bputf(&b, "star99-tree 1\ntext %lld\n", (long long)out.len); bput(&b, out); bputf(&b, "\nnodes %lld\n", (long long)tree.len); for (iz i = 0; i < tree.len; i++) { TreeNode *n = tree.data + i; bputf(&b, "%d %s %s %d %d\n", n->parent, n->kind, n->field.len ? n->field : S("-"), n->lo, n->hi); } os_write(1, bstr(&b)); return 0; } // Driver // What star99 makes for a program goes in a folder of its own, NAME/, // inside .star99/ beside it: next to NAME.star99, or inside the directory // of a directory program. .star99/ tells git to ignore it. It holds only // what the latest compile of each program made: the program's C, its // metaprogram and its with_main C files. static STATE Str program_home = {0}, program_stem = {0}; static STATE Strs program_made = {0}; // the files this compile wrote into the program's folder static Str star99_dir(Str beside) { Str d = join_path(beside, S(".star99")); os_mkdir(d); Str ignore = join_path(d, S(".gitignore")); if (!os_exists(ignore)) os_writefile(ignore, S("*\n")); return d; } static Str program_dir(void) { Str d = join_path(star99_dir(program_home.len ? program_home : S(".")), program_stem.len ? program_stem : S("program")); os_mkdir(d); return d; } static void made(Str path) { *push(&perm, &program_made) = basename_of(path); } // Removes the program's other metaprograms, each an executable and its C. static void drop_metaprograms(Str dir, Str keep) { Strs names = os_listdir(dir).names; for (iz i = 0; i < names.len; i++) { Str n = names.data[i]; if (startswith(n, S("meta-")) && !startswith(n, keep)) os_remove(join_path(dir, n)); } } // A folder of .star99/ that holds a program's outputs, not a tool's work. static b32 program_folder(Str path, Str name) { if (!os_stat(path, 1).isdir) return 0; Strs names = os_listdir(path).names; for (iz i = 0; i < names.len; i++) { Str n = names.data[i]; if (startswith(n, S("meta-")) || equals(n, concat(name, S(".c"))) || equals(n, concat(name, S("-debug.c")))) return 1; } return 0; } // After a compile, .star99/ beside the program keeps only what exists: the // program's folder holds what this compile made (an older C file, with_main // file or metaprogram goes), and the folder of a program whose source is // gone (deleted, renamed) is removed. Nothing outside .star99/ is touched. static void keep_current_outputs(void) { Str top = join_path(program_home.len ? program_home : S("."), S(".star99")); Str self = basename_of(os_realpath(program_home.len ? program_home : S("."))); Strs names = os_listdir(top).names; for (iz i = 0; i < names.len; i++) { Str n = names.data[i], path = join_path(top, n); if (equals(n, program_stem)) { Strs files = os_listdir(path).names; for (iz j = 0; j < files.len; j++) { b32 keep = 0; for (iz k = 0; k < program_made.len && !keep; k++) keep = equals(files.data[j], program_made.data[k]); if (!keep) os_remove(join_path(path, files.data[j])); } os_remove(path); // only when empty } else if (n.data[0] != '.' && program_folder(path, n) && !equals(n, self) && !os_exists(join_path(program_home, concat(n, S(".star99"))))) { os_system(fmtstr("rm -rf %s", shell_quote(path))); } } } static Str shell_quote(Str s) { Buf b = {0}; bputc(&b, '\''); for (iz i = 0; i < s.len; i++) { if (s.data[i] == '\'') bput(&b, S("'\\''")); else bputc(&b, s.data[i]); } bputc(&b, '\''); return bstr(&b); } NORETURN UNUSED static void usage(void) { os_write(2, S("usage: star99 PROGRAM.star99 [--debug | --gdb | --lldb] [-o OUTPUT.c | --expand]\n" " [-- ARGUMENTS...]\n" " star99 DIRECTORY [--debug | --gdb | --lldb] [-o OUTPUT.c | --expand]\n" " [-- ARGUMENTS...]\n" " star99 fmt [--stdout] FILE...\n" " star99 tree FILE\n" " star99 test [NAME...] | gen SEED | dict | debug-script\n" "Builds ./PROGRAM from PROGRAM.star99. With -o, writes the C instead; with\n" "--expand, prints the program with its macros expanded. --debug builds it\n" "for a debugger; --gdb and --lldb also start one on it.\n")); os_exit(2); } // Whether a file is one of the program's own .star99 files. static b32 is_program_source(iz i) { SourceFile *f = files.data + i; return !f->generated && endswith(f->path, S(".star99")) && !startswith(f->path, S("std:")); } // Debug builds: the #line directives of the generated code made exact. A // line after one stands for that .star99 line, and every other line is set // back to its own place in the C file at cpath, so a debugger shows the // runtime there. Directives that change nothing are dropped. static Str debug_lines(Str c, Str cpath) { Str dir = os_realpath(dirname_of(cpath)); Str cq = c_string(dir.len ? join_path(dir, basename_of(cpath)) : cpath); Buf b = {0}; bputf(&b, "#line 2 %s\n", cq); long long real = 2, at = 2; // the line written next, and what the C compiler takes it for Str file = cq; Str want = {0}; long long want_line = 0; for (iz i = 0; i < c.len;) { iz e = i; while (e < c.len && c.data[e] != '\n') e++; Str l = substr(c, i, e); i = e + 1; if (startswith(l, S("#line "))) { iz k = 6; want_line = 0; while (k < l.len && isdigitc(l.data[k])) want_line = want_line * 10 + (l.data[k++] - '0'); while (k < l.len && l.data[k] == ' ') k++; want = substr(l, k, l.len); continue; } Str f = want.len ? want : cq; long long n = want.len ? want_line : real; if (!equals(f, file) || n != at) { if (!want.len) n = real + 1; // the directive takes a line itself if (equals(f, file)) bputf(&b, "#line %lld\n", n); else bputf(&b, "#line %lld %s\n", n, f); real++; file = f; at = n; } bput(&b, l); bputc(&b, '\n'); real++; at++; want = (Str){0}; } return bstr(&b); } // The file of a program: a path, or a name found in PATH; or nothing. static Str program_path(Str name) { if (indexof(name, S("/")) >= 0) return os_runnable(name) ? name : (Str){0}; Str path = os_getenv(S("PATH")); for (iz i = 0; i < path.len;) { iz e = i; while (e < path.len && path.data[e] != ':') e++; Str dir = substr(path, i, e); Str file = join_path(dir, name); if (dir.len && os_runnable(file)) return file; i = e + 1; } return (Str){0}; } // Debug builds: the C compiler flags. -O0 and debug info, with // AddressSanitizer and UndefinedBehaviorSanitizer where the compiler has // them working (gcc, clang), else tcc's bounds checker (-b) with DWARF (its // -g writes stabs, which gdb no longer reads). Found by compiling and // running a small program, once per compiler: the cache keeps the answer. // STAR99_DEBUG_CFLAGS replaces them. static STATE Str debug_cflags = {0}; static STATE Str debug_extra = {0}; // STAR99_CFLAGS before a debug build added its flags static STATE b32 debug_env = 0; // and that it did static Str find_debug_cflags(Str cc, Str dir) { Str env = os_getenv(S("STAR99_DEBUG_CFLAGS")); if (env.len) return env; // the compiler is its command and, where PATH finds it, its file Str user = os_getenv(S("STAR99_CFLAGS")); Str id = fmtstr("v6 %s %s", cc, user), file = program_path(cc); FileInfo st = file.len ? os_stat(file, 0) : (FileInfo){0}; if (st.exists) id = fmtstr("%s %lld %lld", id, (long long)st.size, (long long)st.mtime); Str base = join_path(dir, fmtstr("debug-cc-%016llx", (unsigned long long)hash64(id, 3))); Contents known = os_readfile(concat(base, S(".txt"))); if (known.ok && known.data.len) return known.data; // each with what makes the probe say it works, and the compiler must // say nothing: tcc takes options it does not know with a warning. The // stack is left uninstrumented (asan-stack=0): debuggers would show the // parameters at a function's breakpoint before ASan moved them in its // frame (the checks that matter for Star99 are of the heap). A // compiler's own name for it first: clang's, then gcc's. static Str tries[][2] = { {SL("-O0 -g -fno-omit-frame-pointer -fno-stack-protector -fsanitize=address,undefined " "-mllvm -asan-stack=0"), SL("-DWANT_ASAN")}, {SL("-O0 -g -fno-omit-frame-pointer -fno-stack-protector -fsanitize=address,undefined " "--param asan-stack=0"), SL("-DWANT_ASAN")}, {SL("-O0 -g -fno-omit-frame-pointer -fno-stack-protector -fsanitize=address,undefined"), SL("-DWANT_ASAN")}, {SL("-b -gdwarf"), SL("-DWANT_BCHECK")}, {SL("-O0 -gdwarf"), SL("")}, }; Str mine = fmtstr("%s-%d", base, os_pid()); // others may probe at once Str src = concat(mine, S(".c")), exe = concat(mine, S(".exe")); os_writefile(src, S("int\nmain(void)\n{\n#if defined(WANT_ASAN) && !defined(__SANITIZE_ADDRESS__)\n" "#if defined(__has_feature)\n#if !__has_feature(address_sanitizer)\n" " return 1;\n#endif\n#else\n return 1;\n#endif\n#endif\n" "#if defined(WANT_BCHECK) && !defined(__TCC_BCHECK__)\n return 1;\n#endif\n" " return 0;\n}\n")); Str flags = S("-O0 -g"); for (iz i = 0; i < countof(tries); i++) { // with the user's flags, as the build will be; a leak check cannot // run under a tracer (strace -f), which would make the probe fail Str cmd = fmtstr("test -z \"$(%s -std=c99 %s %s %s %s -o %s 2>&1)\" && " "ASAN_OPTIONS=detect_leaks=0 %s > /dev/null 2>&1", cc, tries[i][0], tries[i][1], user, shell_quote(src), shell_quote(exe), shell_quote(exe)); if (!os_system(cmd)) { flags = tries[i][0]; break; } } os_remove(src); os_remove(exe); if (os_writefile(concat(mine, S(".txt")), flags)) os_rename(concat(mine, S(".txt")), concat(base, S(".txt"))); return flags; } // star99 --gdb, --lldb: the debugger takes over, on the program just built, // with the debugger script loaded (STAR99_GDB and STAR99_LLDB name others). static int start_debugger(Str exe) { Str real = os_realpath(exe); Str path = real.len ? real : exe; b32 gdb = equals(debugger, S("gdb")); Str cmd = os_getenv(gdb ? S("STAR99_GDB") : S("STAR99_LLDB")); if (!cmd.len) cmd = debugger; Str args[5] = {0}; iz n = 0; args[n++] = cmd; if (gdb) { // the program holds the script; this lets gdb run it args[n++] = S("-q"); args[n++] = S("-iex"); args[n++] = fmtstr("add-auto-load-safe-path %s", path); } else { Str script = join_path(program_dir(), S("star99.py")); if (!os_writefile(script, debug_script())) fatal("cannot write %s", script); args[n++] = S("-o"); args[n++] = fmtstr("command script import %s", script); } args[n++] = path; os_exec((Strs){args, n, n}); os_write(2, fmtstr("star99: cannot start %s\n", cmd)); return 127; } // The Build of a program, encoded field by field: c_file, sources, name, // strings (what C plugins add follows) static Buf build_arg(Str cpath, Str out) { Buf arg = {0}; put_str(&arg, cpath); build_cpath = cpath; iz src_at = arg.len; iz nsrc = 0; for (iz i = 0; i < files.len; i++) nsrc += is_program_source(i); bputf(&arg, "l%lld;", (long long)nsrc); for (iz i = 0; i < files.len; i++) { if (is_program_source(i)) put_str(&arg, files.data[i].path); } build_sources = copystr(&perm, substr(bstr(&arg), src_at, arg.len)); put_str(&arg, out); iz lits_at = arg.len; // every string literal of the program, once: a fuzzing dictionary Map *seen = 0; Buf lits = {0}; iz nlits = 0; for (iz i = 0; i < files.len; i++) { if (!is_program_source(i)) continue; Toks toks = lex((i32)i); for (iz k = 0; k < toks.len; k++) { Str t = toks.data[k].text; if (toks.data[k].kind != T_STRING || !t.len || c_text_at((i32)i, toks, k) || !first_time(&seen, t)) continue; put_str(&lits, t); nlits++; } } bputf(&arg, "l%lld;", (long long)nlits); bput(&arg, bstr(&lits)); build_strings = copystr(&perm, substr(bstr(&arg), lits_at, arg.len)); return arg; } // Builds a program: checks it, writes its C to cout or to the cache, and // compiles that C into ./NAME. Returns the exit status. static int build(Str input, Str cout) { b32 single = !is_dir(input); if (!equals(input, S("-")) && !os_stat(input, 0).exists) fatal("cannot open %s", input); Pos nowhere = {-1, 0, 0, 0}; program_home = single ? dirname_of(input) : input; program_stem = basename_of(single && endswith(input, S(".star99")) ? substr(input, 0, input.len - 7) : single ? input : os_realpath(input)); main_module = load_module(single ? dirname_of(input) : input, nowhere, 0, single, input); main_module->ismain = 1; // main was collected before the module knew it was the main one Sym *ms = scope_get(main_module->scope, S("main")); if (ms && ms->kind == S_FN) { Node *d = ms->fns.data[0]->decl; if (d->list.len) errorat(d->pos, "main takes no parameters; use arguments() for the command line"); if (d->a) errorat(d->pos, "main returns nothing; use exit(code) for a status"); } Sym *bsym = scope_get(main_module->scope, S("build")); fuzz_targets_wanted = bsym && bsym->kind == S_FN && bsym->fns.data[0]->ismacro; check_program(); if (expand_only) { Str t = expanded_text(); stop_metaprogram(); os_write(1, t); keep_current_outputs(); return 0; } Str c = generate(); #ifdef FUZZ (void)cout; return c.len == 0; // generated, never written or compiled #endif if (cout.len) { stop_metaprogram(); if (debug_build) c = debug_lines(c, cout); if (!os_writefile(cout, c)) fatal("cannot write %s", cout); keep_current_outputs(); return 0; } Str stem = program_stem; Str cpath = join_path(program_dir(), fmtstr("%s%s.c", stem, debug_build ? S("-debug") : S(""))); if (debug_build) c = debug_lines(c, cpath); if (!os_writefile(cpath, c)) fatal("cannot write %s", cpath); made(cpath); // star99 prog from the parent of prog/: the program goes inside it Str out = is_dir(stem) ? join_path(stem, stem) : stem; Sym *bs = scope_get(main_module->scope, S("build")); Fn *bf = bs && bs->kind == S_FN && bs->fns.data[0]->ismacro ? bs->fns.data[0] : 0; // what the C plugins the program carries need: their compile hooks say Buf arg = build_arg(cpath, out); building = 1; run_compile_hooks(bstr(&arg), bf != 0); if (bf) { // macro fn build(b: Build) builds the program instead of cc fn_signature(bf); Fields *ps = &bf->type->fields; Sym *bt = prelude_sym(S("Build")); if (ps->len != 1 || !bt || ps->data[0].type != bt->type) errorat(bf->decl->pos, "build takes what is known about the build: macro fn build(b: Build)"); if (bf->type->elem != t_void) { help_text = S("write macro fn build(b: Build) { ... }"); errorat(bf->decl->pos, "build returns nothing: the program is already compiled"); } if (c_build_fields) { // and what the compile hooks of C plugins gave bput(&arg, c_flags_used); bput(&arg, c_link_used); } building = 1; meta_call(macro_index(bf), &arg, bf->decl->pos); stop_metaprogram(); keep_current_outputs(); return debugger.len ? start_debugger(out) : 0; } stop_metaprogram(); keep_current_outputs(); Str cc = os_getenv(S("STAR99_CC")); if (!cc.len) cc = S("cc"); Str extra = os_getenv(S("STAR99_CFLAGS")); if (debug_build && !debug_cflags.len) debug_cflags = find_debug_cflags(cc, star99_dir(program_home.len ? program_home : S("."))); if (debug_build && debug_env) extra = debug_extra; // STAR99_CFLAGS without the debug flags Str cmd = fmtstr("%s -std=c99 %s -Wall -Wextra -Wpedantic %s%s %s -o %s -lm%s", cc, debug_build ? debug_cflags : S("-O2 -g"), extra, c_words_of(c_flags_used), shell_quote(cpath), shell_quote(out), c_words_of(c_link_used)); if (os_system(cmd)) { os_write(2, fmtstr("star99: the C compiler failed (%s)\n", cmd)); return 1; } if (debugger.len) return start_debugger(out); return 0; } static void mkdirs(Str path) { for (iz i = 1; i <= path.len; i++) { if (i == path.len || path.data[i] == '/') os_mkdir(substr(path, 0, i)); } } // The first non-empty line of want that text lacks, or nothing. static Str missing_line(Str want, Str text) { iz start = 0; for (iz i = 0; i <= want.len; i++) { if (i < want.len && want.data[i] != '\n') continue; Str l = substr(want, start, i); start = i + 1; if (l.len && indexof(text, l) < 0) return l; } return (Str){0}; } // Whether anything named star99 appeared in a test's own ~/.cache. static b32 wrote_cache(Str work) { Strs names = os_listdir(join_path(work, S(".home/.cache"))).names; b32 found = 0; for (iz i = 0; i < names.len; i++) found |= startswith(names.data[i], S("star99")); return found; } // Whether a program can be started: a path, or a name found in PATH. static b32 have_program(Str name) { return program_path(name).len > 0; } // The debugger a .gdb or .lldb test runs (STAR99_GDB and STAR99_LLDB name // others). static Str debugger_name(b32 gdb) { Str dbg = os_getenv(gdb ? S("STAR99_GDB") : S("STAR99_LLDB")); return dbg.len ? dbg : gdb ? S("gdb") : S("lldb"); } // Why a .gdb or .lldb test cannot run here, or nothing: no such debugger, // or a first line "# not with tcc" when the C compiler is tcc, whose debug // info has no typedef names. static Str debugger_skip(Str test, b32 gdb) { Str cc = os_getenv(S("STAR99_CC")); if (startswith(test, S("# not with tcc\n")) && indexof(basename_of(cc), S("tcc")) >= 0) return S("not with tcc"); if (!have_program(debugger_name(gdb))) return fmtstr("no %s", gdb ? S("gdb") : S("lldb")); return (Str){0}; } // A .gdb or .lldb test: the debugger runs its commands (the lines before // ---) on the debug build in work, and its output must have the lines after // (those starting with ! must not be in it). Says what is wrong, or nothing. static Str debugger_test(Str work, Str name, Str test, b32 gdb) { Str dbg = debugger_name(gdb); iz cut = 0; while (cut < test.len && !(startswith(substr(test, cut, test.len), S("---\n")) && (cut == 0 || test.data[cut - 1] == '\n'))) cut++; Str cmds = substr(test, 0, cut), want = substr(test, cut + 4 < test.len ? cut + 4 : test.len, test.len); Buf b = {0}; if (!gdb) bput(&b, S("command script import star99.py\n")); bput(&b, cmds); os_writefile(join_path(work, S("cmds.txt")), bstr(&b)); if (!gdb) os_writefile(join_path(work, S("star99.py")), debug_script()); Str cmd = gdb ? fmtstr("cd %s && %s -q -batch -nx -iex 'set debuginfod enabled off' -iex " "'add-auto-load-safe-path ./%s' -x cmds.txt ./%s < /dev/null > " "debugger.txt 2>&1", shell_quote(work), dbg, name, name) : fmtstr("cd %s && %s -b -x -s cmds.txt ./%s < /dev/null > debugger.txt 2>&1", shell_quote(work), dbg, name); os_system(cmd); Str out = os_readfile(join_path(work, S("debugger.txt"))).data; Buf have = {0}; for (iz i = 0; i < want.len;) { iz e = i; while (e < want.len && want.data[e] != '\n') e++; Str l = substr(want, i, e); i = e + 1; if (l.len > 1 && l.data[0] == '!') { if (!missing_line(substr(l, 1, l.len), out).len) return fmtstr("the %s output has: %s\n%s", dbg, substr(l, 1, l.len), out); continue; } bput(&have, l); bputc(&have, '\n'); } Str miss = missing_line(bstr(&have), out); if (miss.len) return fmtstr("the %s output lacks: %s\n%s", dbg, miss, out); return (Str){0}; } // Whether a test runs a debug build: .gdb, .lldb and .trace tests do. static b32 debug_test(Str expect) { return startswith(expect, S("gdb:")) || startswith(expect, S("lldb:")) || startswith(expect, S("trace:")); } // star99 test [NAME...]: builds and runs every test case, each in its own // directory and its own process, several at a time. // // Tests live in tests/ next to the star99 executable: NAME.star99 is a // program; NAME.out its exact output, NAME.error lines in the compiler // output, NAME.fail lines in its run-time error, NAME.fmt what star99 fmt // makes of it, or NAME.warn lines of the warnings of a program that builds. A directory NAME holds // the other files it needs. NAME.gdb and NAME.lldb test a debug build in a // debugger (see debugger_test), and NAME.trace lines in the run-time error // of a debug build; a missing debugger skips the test. UNUSED static int run_tests(Strs args) { Str exe = args.data[0]; Str self = os_realpath(exe); // the compiler again, from inside each test directory Str dir = indexof(exe, S("/")) >= 0 ? join_path(dirname_of(exe), S("tests")) : S("tests"); Strs found = star99_files(dir).names; Str *paths = found.data; iz n = found.len; if (n <= 0) fatal("no tests in %s", dir); // a run's work is in tests-PID; the folders of runs that ended go Str top = star99_dir(dirname_of(dir)); Strs runs = os_listdir(top).names; for (iz i = 0; i < runs.len; i++) { Str r = runs.data[i]; if (!startswith(r, S("tests-")) || r.len == 6) continue; b32 digits = 1; for (iz k = 6; k < r.len; k++) digits &= isdigitc(r.data[k]); long long pid = parse_int(substr(r, 6, r.len)); if (digits && pid > 0 && pid <= INT32_MAX && !os_alive((i32)pid)) os_system(fmtstr("rm -rf %s", shell_quote(join_path(top, r)))); } Str root = join_path(top, fmtstr("tests-%d", os_pid())); Str *names = new(&perm, n, Str), *expects = new(&perm, n, Str), *sources = new(&perm, n, Str); b32 *selected = new(&perm, n, b32); i32 *pids = new(&perm, n, i32); i32 *status = new(&perm, n, i32); // as os_wait gives it static Str kinds[] = {SL("out"), SL("error"), SL("fail"), SL("fmt"), SL("warn"), SL("gdb"), SL("lldb"), SL("trace")}; for (iz i = 0; i < n; i++) { Str base = substr(paths[i], 0, paths[i].len - 7); // without .star99 names[i] = substr(base, dir.len + 1, base.len); for (iz k = 0; k < countof(kinds) && !expects[i].len; k++) { Contents e = os_readfile(fmtstr("%s.%s", base, kinds[k])); if (e.ok) expects[i] = fmtstr("%s:%s", kinds[k], e.data); } if (!expects[i].len) fatal("%s has no .out, .error, .fail, .fmt, .warn, .gdb, .lldb or .trace file", base); selected[i] = args.len <= 2; for (iz a = 2; a < args.len; a++) selected[i] |= indexof(names[i], args.data[a]) >= 0; } iz running = 0; for (iz i = 0; i < n; i++) { // debug builds: their flags found once, before the forks if (selected[i] && debug_test(expects[i]) && !debug_cflags.len) { Str cc = os_getenv(S("STAR99_CC")); debug_cflags = find_debug_cflags(cc.len ? cc : S("cc"), top); } } for (iz i = 0; i < n; i++) { if (!selected[i]) continue; Str work = join_path(root, names[i]); mkdirs(work); sources[i] = os_readfile(paths[i]).data; os_writefile(join_path(work, fmtstr("%s.star99", names[i])), sources[i]); Str extra = join_path(dir, names[i]); if (is_dir(extra)) os_system(fmtstr("cp -RL %s/. %s", shell_quote(extra), shell_quote(work))); i32 pid = os_fork(); if (pid < 0) fatal("cannot start a process for test %s", names[i]); if (!pid) { if (!os_chdir(work) || !os_redirect(2, S("build.txt"))) os_child_exit(99); // a home of its own, so anything star99 writes into ~/.cache shows Str here = os_getcwd(); if (!here.len) os_child_exit(99); Str home = join_path(here, S(".home")); os_mkdir(home); os_setenv(S("HOME"), home); os_setenv(S("XDG_CACHE_HOME"), join_path(home, S(".cache"))); if (self.len) os_setenv(S("STAR99"), self); // the compiler under test os_dup(1, 2); Str src = fmtstr("%s.star99", names[i]); if (startswith(expects[i], S("fmt:"))) { os_write(2, format_file(src, sources[i])); os_child_exit(0); } debug_build = debug_test(expects[i]); int r = build(src, (Str){0}); if (!r && self.len && startswith(expects[i], S("out:"))) { // the program as --expand prints it builds too (and must print the same) os_writefile(S("expanded.star99"), expanded_text()); if (os_system(fmtstr("%s expanded.star99 > expanded.txt 2>&1", self))) { os_write(2, S("error: the expanded program does not build:\n")); r = 1; } } if (!r) { // whatever the suite builds, star99 fmt brings to a fixed point Str once = format_file(src, sources[i]); if (!equals(format_file(src, once), once)) { os_write(2, S("error: star99 fmt changes its own output\n")); r = 1; } } os_child_exit(r); } pids[i] = pid; if (++running >= 8) { Ended done = os_wait(0); for (iz j = 0; j < n; j++) { if (pids[j] == done.pid) status[j] = done.code; } running--; } } for (iz i = 0; i < n; i++) { if (!selected[i] || !pids[i]) continue; Ended done = os_wait(pids[i]); if (done.pid == pids[i]) status[i] = done.code; } int passed = 0, failed = 0, skipped = 0; for (iz i = 0; i < n; i++) { if (!selected[i]) continue; Str work = join_path(root, names[i]); Str build_out = os_readfile(join_path(work, S("build.txt"))).data; b32 built = status[i] == 0; Str expect = expects[i]; Str problem = {0}; if (status[i] < 0) { problem = fmtstr("the compiler crashed (signal %d):\n%s", -status[i], build_out); } else if (startswith(expect, S("fmt:"))) { if (!equals(build_out, substr(expect, 4, expect.len))) problem = fmtstr("formatted as:\n%s", build_out); } else if (startswith(expect, S("warn:"))) { Str miss = missing_line(substr(expect, 5, expect.len), build_out); if (!built) problem = fmtstr("did not compile:\n%s", build_out); else if (miss.len) problem = fmtstr("the compiler output lacks: %s\n%s", miss, build_out); } else if (startswith(expect, S("error:"))) { Str miss = missing_line(substr(expect, 6, expect.len), build_out); if (built) problem = S("compiled, but should have failed"); else if (miss.len) problem = fmtstr("the compiler output lacks: %s\n%s", miss, build_out); } else if (!built) { problem = fmtstr("did not compile:\n%s", build_out); } else if (indexof(build_out, S("warning:")) >= 0 || indexof(build_out, S("error")) >= 0) { problem = fmtstr("compiling warned:\n%s", build_out); } else if (startswith(expect, S("gdb:")) || startswith(expect, S("lldb:"))) { b32 gdb = expect.data[0] == 'g'; Str test = substr(expect, gdb ? 4 : 5, expect.len), skip = debugger_skip(test, gdb); if (skip.len) { skipped++; os_write(1, fmtstr("SKIP %s: %s\n", names[i], skip)); continue; } problem = debugger_test(work, names[i], test, gdb); } else { int r = os_system(fmtstr("cd %s && ./%s < /dev/null > out.txt 2> err.txt", shell_quote(work), names[i])); Str out = os_readfile(join_path(work, S("out.txt"))).data; Str err = os_readfile(join_path(work, S("err.txt"))).data; if (startswith(expect, S("fail:")) || startswith(expect, S("trace:"))) { Str miss = missing_line(substr(expect, expect.data[0] == 'f' ? 5 : 6, expect.len), err); if (!r) problem = S("ran to the end, but should have stopped with an error"); else if (miss.len) problem = fmtstr("the error output lacks: %s\n%s", miss, err); } else if (r) { problem = fmtstr("exited with %d:\n%s", r, err); } else if (!equals(out, substr(expect, 4, expect.len))) { problem = fmtstr("printed:\n%s", out); } else if (self.len) { os_system(fmtstr("cd %s && ./expanded < /dev/null > out.txt 2> err.txt", shell_quote(work))); out = os_readfile(join_path(work, S("out.txt"))).data; if (!equals(out, substr(expect, 4, expect.len))) problem = fmtstr("expanded, it printed:\n%s", out); } } if (!problem.len && wrote_cache(work)) problem = S("star99 wrote into ~/.cache"); if (problem.len) { failed++; os_write(1, fmtstr("FAIL %s: %s\n", names[i], problem)); } else { passed++; } } if (failed) os_write(1, fmtstr("The failed tests' files are in %s\n", root)); if (skipped) os_write(1, fmtstr("%d passed, %d failed, %d skipped\n", passed, failed, skipped)); else os_write(1, fmtstr("%d passed, %d failed\n", passed, failed)); if (!failed) os_system(fmtstr("rm -rf %s", shell_quote(root))); return failed != 0; } // Random programs for differential testing: star99 gen SEED prints a // well-typed program. It must compile without a C warning and run without a // sanitizer report; its print/parse round trip must hold. Values stay small // so overflow is rare, and indexing is guarded. enum { G_INT, G_BOOL, G_STR, G_LIST, G_P, G_U, G_MAP, G_POOL, G_REF, G_FN, G_NTYPES }; static Str gen_tnames[] = {SL("Int"), SL("Bool"), SL("String"), SL("List(Int)"), SL("P"), SL("U"), SL("Map(String, Int)"), SL("Pool(Int)"), SL("Ref(Int)"), SL("fn (Int) -> Int")}; enum { G_PRINTABLE = G_POOL }; // the types before it print typedef struct { int type; int mut; int name; } GenVar; typedef struct { u64 rng; Buf out; GenVar vars[256]; int nvars; int names; int fns; // functions defined so far int fn_params[8][3]; // parameter types per function int fn_nparams[8]; int fn_result[8]; int indent; int loops; // nested loops, to keep run time small int walking[8]; // lists that enclosing loops walk: left unchanged int nwalking; } Gen; static int gen_rand(Gen *g, int n) { g->rng = g->rng * 0x3243f6a8885a308d + 1; return (int)((g->rng >> 33) % (u64)n); } static void gen_line(Gen *g) { bputc(&g->out, '\n'); for (int i = 0; i < g->indent; i++) bput(&g->out, S(" ")); } static int gen_find(Gen *g, int type, int mut) { int found[256] = {0}, n = 0; for (int i = 0; i < g->nvars; i++) { b32 walked = 0; for (int w = 0; w < g->nwalking; w++) walked |= mut && g->walking[w] == g->vars[i].name; if (g->vars[i].type == type && (!mut || g->vars[i].mut) && !walked) found[n++] = i; } return n ? found[gen_rand(g, n)] : -1; } static void rgen_expr(Gen *g, int type, int depth); static void gen_var_or(Gen *g, int type, int depth) { int v = gen_find(g, type, 0); if (v >= 0 && gen_rand(g, 3)) { bputf(&g->out, "v%d", g->vars[v].name); return; } rgen_expr(g, type, depth); } // Writes fmt with its parts generated: %I %B %S %L %P %U %M %O %R %F a // variable or new expression of that type (G_* order), the lower-case // letter a new expression only, %X any printable type, %d and %c the next // argument, %% a percent sign. static void gen_tpl(Gen *g, int depth, char *fmt, ...) { static Str letters = SL("IBSLPUMORF"); va_list ap; // set by va_start va_start(ap, fmt); for (char *f = fmt; *f; f++) { if (*f != '%') { bputc(&g->out, *f); continue; } u8 c = (u8) * ++f; iz up = -1, low = -1; // where c is in letters, and where its upper case is for (iz k = 0; k < letters.len; k++) { if (letters.data[k] == c) up = k; if (letters.data[k] + 32 == c) low = k; } if (c == 'd') bputf(&g->out, "%d", va_arg(ap, int)); else if (c == 'c') bputc(&g->out, va_arg(ap, int)); else if (c == 'X') gen_var_or(g, gen_rand(g, G_PRINTABLE), depth - 1); else if (up >= 0) gen_var_or(g, (int)up, depth - 1); else if (low >= 0) rgen_expr(g, (int)low, depth - 1); else bputc(&g->out, c); } va_end(ap); } static void rgen_expr(Gen *g, int type, int depth) { Buf *b = &g->out; int leaf = depth <= 0; int k = leaf ? 0 : gen_rand(g, 8); if (k == 1 && g->fns) { // a call to an earlier function with this result int cand[8] = {0}, n = 0; for (int f = 0; f < g->fns; f++) { if (g->fn_result[f] == type) cand[n++] = f; } if (n) { int f = cand[gen_rand(g, n)]; bputf(b, "f%d(", f); for (int i = 0; i < g->fn_nparams[f]; i++) { if (i) bput(b, S(", ")); gen_var_or(g, g->fn_params[f][i], depth - 1); } bputc(b, ')'); return; } } if (k == 2 && type != G_LIST) { bput(b, S("(if ")); rgen_expr(g, G_BOOL, depth - 1); bput(b, S(" do ")); gen_var_or(g, type, depth - 1); bput(b, S(" else ")); gen_var_or(g, type, depth - 1); bputc(b, ')'); return; } if (k == 3) { int v = gen_find(g, type, 0); if (v >= 0) { bputf(b, "v%d", g->vars[v].name); return; } } // variables of the kinds a case below reads, or -1 int vl = gen_find(g, G_LIST, 0), vu = gen_find(g, G_U, 0), vm = gen_find(g, G_MAP, 0); int vf = gen_find(g, G_FN, 0), vr = gen_find(g, G_REF, 0), vp = gen_find(g, G_POOL, 0); int vs = gen_find(g, G_STR, 0); #define VN(v) g->vars[v].name switch (type) { case G_INT: switch (leaf ? 0 : gen_rand(g, 17)) { case 1: gen_tpl(g, depth, gen_rand(g, 2) ? "(%I + %I)" : "(%I - %I)"); return; case 2: gen_tpl(g, depth, gen_rand(g, 2) ? "len(%L)" : "len(%S)"); return; case 3: gen_tpl(g, depth, "%P.x"); return; case 4: gen_tpl(g, depth, "(%I %% %d)", 1 + gen_rand(g, 9)); return; case 5: { if (vl < 0) break; int i = gen_rand(g, 4); bputf(b, "(if %d < len(v%d) do v%d[%d] else %d)", i, VN(vl), VN(vl), i, gen_rand(g, 10)); return; } case 6: if (vu < 0) break; bputf(b, "switch v%d {", VN(vu)); g->indent++; gen_line(g); bputf(b, "case Int: v%d", VN(vu)); gen_line(g); bputf(b, "case String: len(v%d)", VN(vu)); gen_line(g); bput(b, S("case nil: 0")); g->indent--; gen_line(g); bputc(b, '}'); return; case 7: gen_tpl(g, depth, "abs(%I)"); return; case 8: bputf(b, "(%d * %d)", gen_rand(g, 20), gen_rand(g, 20)); return; case 9: { if (vm < 0) break; int k2 = gen_rand(g, 4); bputf(b, "(if \"k%d\" in v%d do v%d[\"k%d\"] else %d)", k2, VN(vm), VN(vm), k2, gen_rand(g, 10)); return; } case 10: if (vf < 0) break; gen_tpl(g, depth, "v%d(%I)", VN(vf)); return; case 11: gen_tpl(g, depth, gen_rand(g, 2) ? "len(%M)" : "len(%O)"); return; case 12: if (vr < 0 || vp < 0) break; bputf(b, "(if v%d in v%d do v%d.* else %d)", VN(vr), VN(vp), VN(vr), gen_rand(g, 10)); return; // edges, so overflow and bounds checks fire and the optimizer has to // keep exactly those that can case 13: gen_tpl(g, depth, "(%I * %I)"); return; case 14: { static Str edge[] = {SL("9223372036854775807"), SL("(-9223372036854775807 - 1)"), SL("4611686018427387904"), SL("3074457345618258602"), SL("(-4611686018427387904)")}; int e = gen_rand(g, 5); bputf(b, "(%s - %d)", edge[e], e == 1 ? -gen_rand(g, 3) : gen_rand(g, 3)); return; } case 15: gen_tpl(g, depth, gen_rand(g, 2) ? "(%I / %d)" : "(%I %% %d)", gen_rand(g, 5) - 2); return; case 16: if (vl < 0) break; gen_tpl(g, depth, "v%d[%I]", VN(vl)); return; } if (!gen_rand(g, 6)) { // near the ends of Int, one leaf in six static Str edge[] = {SL("9223372036854775807"), SL("(-9223372036854775807 - 1)"), SL("4611686018427387904"), SL("3074457345618258602")}; bputf(b, "(%s - %d)", edge[gen_rand(g, 4)], gen_rand(g, 3)); return; } bputf(b, "%d", gen_rand(g, 100)); return; case G_BOOL: { static char *cmp[] = {"%I == %I", "%I != %I", "%I < %I", "%I <= %I", "%I > %I", "%I >= %I"}; switch (leaf ? 0 : gen_rand(g, 8)) { case 1: gen_tpl(g, depth, cmp[gen_rand(g, 6)]); return; case 2: gen_tpl(g, depth, "not (%b)"); return; case 3: gen_tpl(g, depth, gen_rand(g, 2) ? "(%b and %b)" : "(%b or %b)"); return; case 4: gen_tpl(g, depth, "%I in %L"); return; case 5: gen_tpl(g, depth, "%S == %S"); return; case 6: gen_tpl(g, depth, "%P == %P"); return; case 7: // a byte of a string against a character literal if (vs < 0) break; bputf(b, "(if len(v%d) > 0 do v%d[0] == '%c' else false)", VN(vs), VN(vs), 'a' + gen_rand(g, 26)); return; } bput(b, gen_rand(g, 2) ? S("true") : S("false")); return; } case G_STR: switch (leaf ? 0 : gen_rand(g, 4)) { case 1: gen_tpl(g, depth, "str(%X, \"-\", %I)"); return; case 2: gen_tpl(g, depth, "%P.s"); return; } bputf(b, "\"%c%d\"", 'a' + gen_rand(g, 26), gen_rand(g, 10)); return; case G_LIST: { int n = leaf ? gen_rand(g, 2) : gen_rand(g, 4); if (!n) { bput(b, S("List(Int)()")); return; } bputc(b, '['); for (int i = 0; i < n; i++) gen_tpl(g, depth, i ? ", %I" : "%I"); bputc(b, ']'); return; } case G_P: gen_tpl(g, depth, gen_rand(g, 2) ? "P(x: %I, s: %S)" : "P(x: %I)"); return; case G_U: gen_tpl(g, depth, (char *[]){"U(%I)", "U(%S)", "U(nil)"}[gen_rand(g, 3)]); return; case G_MAP: { int n = leaf ? 0 : gen_rand(g, 3); if (!n) { bput(b, S("Map(String, Int)()")); return; } bputc(b, '['); for (int i = 0; i < n; i++) gen_tpl(g, depth, i ? ", \"k%d\": %I" : "\"k%d\": %I", i * 2 + gen_rand(g, 2)); bputc(b, ']'); return; } case G_POOL: bput(b, S("Pool(Int)()")); return; case G_REF: { // a new item in some pool; reads check that it is still there int v = gen_find(g, G_POOL, 1); if (v < 0) bput(b, S("Ref(Int)()")); else gen_tpl(g, depth, "add(&v%d, %I)", VN(v)); return; } case G_FN: { // a closure over the variables in scope int name = g->names++; bputf(b, "fn (v%d: Int) do v%d + ", name, name); if (g->nvars < 256) g->vars[g->nvars++] = (GenVar){G_INT, 0, name}; gen_var_or(g, G_INT, depth - 1); g->nvars--; return; } } #undef VN } static void gen_block(Gen *g, int n, int depth); static void rgen_stmt(Gen *g, int depth) { Buf *b = &g->out; gen_line(g); switch (gen_rand(g, depth > 0 ? 15 : 5)) { case 0: case 1: { int t = gen_rand(g, G_NTYPES); int name = g->names++; int mut = gen_rand(g, 3) != 0; bputf(b, "%s v%d", mut ? S("var") : S("const"), name); b32 typed = gen_rand(g, 2); if (typed) bputf(b, ": %s", gen_tnames[t]); if (!mut || !typed || t == G_FN || gen_rand(g, 4)) { bput(b, S(" = ")); rgen_expr(g, t, 3); } if (g->nvars < 256) g->vars[g->nvars++] = (GenVar){t, mut, name}; return; } case 2: { bput(b, S("println(")); int n = 1 + gen_rand(g, 3); for (int i = 0; i < n; i++) { if (i) bput(b, S(", \" \", ")); gen_var_or(g, gen_rand(g, G_PRINTABLE), 2); } bputc(b, ')'); return; } case 3: { int t = gen_rand(g, G_NTYPES); int v = gen_find(g, t, 1); if (v < 0) { bput(b, S("println(\"no variable\")")); return; } int vn = g->vars[v].name; if (t == G_INT && gen_rand(g, 2)) { gen_tpl(g, 2, gen_rand(g, 2) ? "v%d += %i" : "v%d -= %i", vn); } else if (t == G_LIST && gen_rand(g, 2)) { gen_tpl(g, 3, "add(&v%d, %i)", vn); } else if (t == G_P && gen_rand(g, 2)) { gen_tpl(g, 3, "v%d.x = %i", vn); } else if (t == G_POOL) { int r = gen_find(g, G_REF, 0); if (r < 0) { bput(b, S("println(\"no ref\")")); return; } int rn = g->vars[r].name, pn = g->vars[v].name; bputf(b, "if v%d in v%d do remove(&v%d, v%d)", rn, pn, pn, rn); } else if (t == G_MAP && gen_rand(g, 2)) { gen_tpl(g, 3, "v%d[\"k%d\"] = %i", vn, gen_rand(g, 4)); } else { bputf(b, "v%d = ", g->vars[v].name); rgen_expr(g, t, 2); } return; } case 4: { int v = gen_find(g, G_LIST, 1); if (v < 0) { bput(b, S("println(\"no list\")")); return; } int n = g->vars[v].name; bputf(b, "if len(v%d) > 0 do remove(&v%d, 0)", n, n); return; } case 5: case 6: { gen_tpl(g, 3, "if %b {"); gen_block(g, 1 + gen_rand(g, 3), depth - 1); if (gen_rand(g, 2)) { bput(b, S(" else {")); gen_block(g, 1 + gen_rand(g, 3), depth - 1); } return; } case 7: { if (g->loops >= 2) { bput(b, S("println(\"deep\")")); return; } g->loops++; int name = g->names++; int save = g->nvars, pushed = 0; if (gen_rand(g, 2)) { bputf(b, "for v%d in 0..<%d {", name, gen_rand(g, 4)); } else { bputf(b, "for v%d in ", name); int v = gen_find(g, G_LIST, 0); if (v >= 0 && gen_rand(g, 2)) { bputf(b, "v%d", g->vars[v].name); g->walking[g->nwalking++] = g->vars[v].name; } else { rgen_expr(g, G_LIST, 1); v = -1; } bput(b, S(" {")); if (v < 0) g->walking[g->nwalking++] = -1; pushed = 1; } g->vars[g->nvars++] = (GenVar){G_INT, 0, name}; gen_block(g, 1 + gen_rand(g, 3), depth - 1); g->nwalking -= pushed; g->nvars = save; g->loops--; return; } case 8: { int v = gen_find(g, G_U, 0); if (v < 0) { bput(b, S("println(\"no union\")")); return; } int n = g->vars[v].name; bputf(b, "switch v%d {", n); gen_line(g); bputf(b, "case Int: println(\"int \", v%d)", n); gen_line(g); bputf(b, "case String: println(\"string \", v%d)", n); gen_line(g); bput(b, S("case nil: println(\"nil\")")); gen_line(g); bputc(b, '}'); return; } case 9: { if (g->loops >= 2) { bput(b, S("println(\"deep\")")); return; } int name = g->names++; bputf(b, "var v%d = 0", name); gen_line(g); bputf(b, "while v%d < %d {", name, gen_rand(g, 4)); g->loops++; g->indent++; gen_line(g); bputf(b, "v%d += 1", name); g->indent--; g->vars[g->nvars++] = (GenVar){G_INT, 0, name}; gen_block(g, 1 + gen_rand(g, 2), depth - 1); g->loops--; return; } case 10: { bput(b, S("{")); gen_block(g, 1 + gen_rand(g, 3), depth - 1); return; } case 11: gen_tpl(g, 3, "defer println(\"deferred \", %i)"); return; case 12: { // a reference into a list, used after the list may change int v = gen_find(g, G_LIST, 1); if (v < 0) break; int n = g->vars[v].name, r = g->names++; bputf(b, "if len(v%d) > 0 {", n); gen_line(g); bputf(b, " var v%d = &v%d[0]", r, n); gen_line(g); gen_tpl(g, 3, " v%d.* += %i", r); gen_line(g); bput(b, S("}")); return; } case 13: { int v = gen_find(g, G_MAP, 1); if (v < 0 || g->loops >= 2) break; int n = g->vars[v].name; bputf(b, "for k, x in v%d {", n); gen_line(g); bputf(b, " v%d[str(k, \"!\")] = x + 1", n); gen_line(g); bput(b, S("}")); return; } case 14: { int v = gen_find(g, G_STR, 1); if (v < 0) break; gen_tpl(g, 3, "add(&v%d, %i, \" \", %s)", g->vars[v].name); return; } } } // Statements, then the closing brace; the block's variables go out of scope. static void gen_block(Gen *g, int n, int depth) { int save = g->nvars; g->indent++; for (int i = 0; i < n; i++) rgen_stmt(g, depth); g->indent--; gen_line(g); bputc(&g->out, '}'); g->nvars = save; } UNUSED static Str gen_program(u64 seed) { Gen *g = new(&perm, 1, Gen); g->rng = seed * 0x9e3779b97f4a7c15 + 1; bput(&g->out, S("struct P (x: Int, s = \"p\")\nunion U (Int, String)\n")); int nf = gen_rand(g, 4); for (int f = 0; f < nf; f++) { g->nvars = 0; int np = gen_rand(g, 4); bputf(&g->out, "\nfn f%d(", f); for (int i = 0; i < np; i++) { int t = gen_rand(g, G_NTYPES); int name = g->names++; if (i) bput(&g->out, S(", ")); bputf(&g->out, "v%d: %s", name, gen_tnames[t]); g->vars[g->nvars++] = (GenVar){t, 0, name}; g->fn_params[f][i] = t; } g->fn_nparams[f] = np; int r = gen_rand(g, G_NTYPES); bputf(&g->out, ") -> %s {", gen_tnames[r]); g->indent = 1; for (int i = 0, n = gen_rand(g, 4); i < n; i++) rgen_stmt(g, 2); gen_line(g); bput(&g->out, S("return ")); rgen_expr(g, r, 3); g->indent = 0; bput(&g->out, S("\n}\n")); g->fn_result[f] = r; g->fns = f + 1; } g->nvars = 0; bput(&g->out, S("\nfn main() {")); g->indent = 1; for (int i = 0, n = 4 + gen_rand(g, 12); i < n; i++) rgen_stmt(g, 3); bput(&g->out, S("\n}\n")); return bstr(&g->out); } // The value that ends a function body becomes an explicit return, also at // the ends of the if and switch branches that end it. static void end_with_return(Node *blk) { if (!blk || blk->kind != N_BLOCK || !blk->list.len) return; Node **last = &blk->list.data[blk->list.len - 1]; Node *x = *last; if (x->kind == N_EXPR && !diverges(x)) { // fail(...) stays as it is Node *r = mknode(N_RETURN, x->pos); r->a = x->a; *last = r; } else if (x->kind == N_IF && x->c && !(x->flags & F_MACRO)) { end_with_return(x->b); end_with_return(x->c); // an else if is a block holding the if } else if (x->kind == N_SWITCH && !(x->flags & F_MACRO)) { for (iz i = 0; i < x->list.len; i++) end_with_return(x->list.data[i]->a); } } // star99 fmt writes the value ending a function body with a result type as // return value; do bodies and value blocks inside expressions stay. static void explicit_returns(Node *n) { if (!n) return; if ((n->kind == N_FN || n->kind == N_FNLIT) && n->a && n->b && n->b->kind == N_BLOCK && !(n->b->flags & F_DO) && !(n->flags & F_DO)) end_with_return(n->b); explicit_returns(n->a); explicit_returns(n->b); explicit_returns(n->c); for (iz i = 0; i < n->list.len; i++) explicit_returns(n->list.data[i]); } // Formatting is parse-only: do not load modules or run macros to discover // types. This small lexical environment proves just Int atoms and ordinary // calls. An opaque call may inspect syntax or introduce bindings, so a // function containing one is left alone by the range rewrite. enum { FI_UNKNOWN, FI_INT, FI_CALL }; typedef struct FmtBinding FmtBinding; struct FmtBinding { FmtBinding *next; Str name; int kind; }; static FmtBinding * fmt_binding(FmtBinding *env, Str name) { for (; env; env = env->next) if (equals(env->name, name)) return env; return 0; } static FmtBinding * fmt_bind(FmtBinding *env, Str name, int kind) { FmtBinding *b = new(&perm, 1, FmtBinding); *b = (FmtBinding){env, name, kind}; return b; } static b32 fmt_int_type(Node *n) { return n && n->kind == N_TNAME && equals(n->name, S("Int")) && !n->a && !n->c && !n->list.len; } static b32 fmt_int_literal(Node *n, i64 *value) { if (n->kind == N_INT && !(n->flags & F_CHAR) && n->ival >= 0) { *value = n->ival; return 1; } if (n->kind == N_UNARY && n->op == T_MINUS && n->a->kind == N_INT && !(n->a->flags & F_CHAR) && (u64)n->a->ival <= (UINT64_C(1) << 63)) { *value = (i64)(0 - (u64)n->a->ival); return 1; } return 0; } static b32 fmt_int_atom(Node *n, FmtBinding *env) { if (!n) return 0; i64 value = 0; if (fmt_int_literal(n, &value)) return 1; FmtBinding *b = n->kind == N_NAME ? fmt_binding(env, n->name) : 0; return b && b->kind == FI_INT; } static b32 fmt_same_atom(Node *a, Node *b) { if (a->kind == N_NAME && b->kind == N_NAME) return equals(a->name, b->name); i64 x = 0, y = 0; return fmt_int_literal(a, &x) && fmt_int_literal(b, &y) && x == y; } static b32 fmt_range_pair(Node *n, Node *lower, Node *upper, FmtBinding *env) { if (lower->kind != N_BINARY || upper->kind != N_BINARY) return 0; Node *lo = 0, *x = 0, *other = 0, *hi = 0; if (lower->op == T_LE || lower->op == T_LT) { lo = lower->a; x = lower->b; } else if (lower->op == T_GE || lower->op == T_GT) { lo = lower->b; x = lower->a; } else { return 0; } if (upper->op == T_LT || upper->op == T_LE) { other = upper->a; hi = upper->b; } else if (upper->op == T_GT || upper->op == T_GE) { other = upper->b; hi = upper->a; } else { return 0; } if (!fmt_int_atom(lo, env) || !fmt_int_atom(x, env) || !fmt_int_atom(hi, env) || !fmt_same_atom(x, other)) return 0; // Only reads and literals move or disappear. No call, checked access or // arithmetic may be evaluated earlier, twice instead of once, or eagerly // where the original and would have skipped it. Keep interior comments // attached to their original comparisons too. SourceFile *f = files.data + n->pos.file; iz comment_end = n->pos.hi; while (comment_end < f->text.len && f->text.data[comment_end] != '\n') comment_end++; for (iz i = 0; i < f->comments.len; i++) if (f->comments.data[i] >= n->pos.lo && f->comments.data[i] < comment_end) return 0; Node *range = mknode(N_RANGE, n->pos); range->op = upper->op == T_LT || upper->op == T_GT ? T_DOTLT : T_DOTEQ; b32 open_low = lower->op == T_LT || lower->op == T_GT; if (open_low) range->op = range->op == T_DOTLT ? T_LTDOTLT : T_LTDOTEQ; range->a = lo; range->b = hi; i64 value = 0; if (!open_low && fmt_int_literal(lo, &value) && !value) range->flags |= F_IMPLICIT; n->kind = N_IN; n->op = T_IN; n->a = x; n->b = range; return 1; } static int fmt_var_kind(Node *n, FmtBinding *env) { if (n->flags & F_MACRO) return FI_UNKNOWN; return (n->a ? fmt_int_type(n->a) : fmt_int_atom(n->b, env)) ? FI_INT : FI_UNKNOWN; } static int fmt_fn_kind(Node *n) { // These prelude macros join user overloads of the same name. return n->flags & F_MACRO || equals(n->name, S("assert")) || equals(n->name, S("reversed")) || equals(n->name, S("unreachable")) ? FI_UNKNOWN : FI_CALL; } static b32 fmt_ranges(Node *n, FmtBinding *env, b32 apply) { if (!n) return 1; if (n->flags & F_MACRO) return 0; switch (n->kind) { case N_TEMPLATE: return 1; // code values expose the syntax itself case N_SPLICE: case N_IMPORT: case N_MLABEL: return 0; case N_TNAME: case N_TFN: case N_TSTRUCT: case N_TUNION: apply = 0; break; case N_TMACRO: return 0; case N_CALL: { FmtBinding *callee = n->a->kind == N_NAME ? fmt_binding(env, n->a->name) : 0; if (!callee || callee->kind != FI_CALL) return 0; break; } case N_FN: case N_FNLIT: { FmtBinding *outer = env; // defaults resolve outside the parameter scope if (!fmt_ranges(n->a, outer, 0)) return 0; for (iz i = 0; i < n->list.len; i++) { Node *p = n->list.data[i]; if (!fmt_ranges(p->a, outer, 0) || !fmt_ranges(p->b, outer, 0)) return 0; int kind = p->a ? (fmt_int_type(p->a) ? FI_INT : FI_UNKNOWN) : (fmt_int_atom(p->b, outer) ? FI_INT : FI_UNKNOWN); env = fmt_bind(env, p->name, kind); } return fmt_ranges(n->b, env, apply); } case N_BLOCK: for (iz i = 0; i < n->list.len; i++) { Node *s = n->list.data[i]; if (!fmt_ranges(s, env, apply)) return 0; if (s->kind == N_VAR) env = fmt_bind(env, s->name, fmt_var_kind(s, env)); else if (s->kind == N_FN) env = fmt_bind(env, s->name, fmt_fn_kind(s)); } return 1; case N_VAR: return fmt_ranges(n->a, env, 0) && fmt_ranges(n->b, env, apply); case N_FOR: // A user-defined for_expansion receives the body as syntax. if (n->a->kind != N_RANGE) return 0; if (!fmt_ranges(n->a, env, apply) || !fmt_ranges(n->c, env, 0)) return 0; env = fmt_bind(env, n->name, n->a->kind == N_RANGE && !(n->flags & F_FILTER) ? FI_INT : FI_UNKNOWN); if (n->sval.len) env = fmt_bind(env, n->sval, FI_UNKNOWN); return fmt_ranges(n->b, env, apply); case N_SWITCH: if (!fmt_ranges(n->a, env, apply)) return 0; if (n->name.len) env = fmt_bind(env, n->name, FI_UNKNOWN); for (iz i = 0; i < n->list.len; i++) if (!fmt_ranges(n->list.data[i], env, apply)) return 0; return 1; default: break; } if (!fmt_ranges(n->a, env, apply) || !fmt_ranges(n->b, env, apply) || !fmt_ranges(n->c, env, apply)) return 0; for (iz i = 0; i < n->list.len; i++) if (!fmt_ranges(n->list.data[i], env, apply)) return 0; if (apply && n->kind == N_BINARY && n->op == T_AND) { if (!fmt_range_pair(n, n->a, n->b, env)) fmt_range_pair(n, n->b, n->a, env); } return 1; } static void fmt_range_idioms(Nodes *decls) { FmtBinding *env = 0; b32 imports = 0; for (iz i = 0; i < decls->len; i++) { Node *d = decls->data[i]; imports |= d->kind == N_IMPORT; // A top-level macro may change the declarations or their interfaces. if (d->kind == N_EXPR || d->kind == N_MLABEL || d->kind == N_DECLS || (d->kind != N_FN && (d->flags & F_MACRO)) || (d->kind == N_FN && (d->flags & F_MACRO) && equals(d->name, S("build")))) return; } if (!imports) { // Only these builtins accept ordinary values without syntax capture. static Str names[] = {SL("print"), SL("println"), SL("str"), SL("len")}; for (iz i = 0; i < countof(names); i++) env = fmt_bind(env, names[i], FI_CALL); } for (iz i = 0; i < decls->len; i++) { Node *d = decls->data[i]; if (d->kind == N_FN) { FmtBinding *old = fmt_binding(env, d->name); int kind = imports ? FI_UNKNOWN : fmt_fn_kind(d); if (old && old->kind == FI_UNKNOWN) kind = FI_UNKNOWN; env = fmt_bind(env, d->name, kind); } else if (d->kind == N_VAR) { env = fmt_bind(env, d->name, fmt_var_kind(d, env)); } } for (iz i = 0; i < decls->len; i++) { Node *d = decls->data[i]; if (d->kind != N_FN && d->kind != N_IMPORT && !fmt_ranges(d, env, 0)) return; } for (iz i = 0; i < decls->len; i++) { Node *d = decls->data[i]; if (d->kind == N_FN && fmt_ranges(d, env, 0)) fmt_ranges(d, env, 1); } } // The declarations as the printer shows them, one after another. static Str decls_text(Nodes *decls) { Buf b = {0}; for (iz i = 0; i < decls->len; i++) { bput(&b, node_source(decls->data[i])); bputc(&b, '\n'); } return bstr(&b); } // star99 fmt [--stdout] FILE...: rewrites each file in the printer's style, // keeping comments. The result must parse to the same program. static Str format_file(Str path, Str text) { i32 file = add_file(path, text, 0); Nodes decls = parse_file(file); for (iz i = 0; i < decls.len; i++) explicit_returns(decls.data[i]); fmt_range_idioms(&decls); Buf b = {0}; fmt_file = file + 1; fmt_next = 0; for (iz i = 0; i < decls.len;) { fmt_before(&b, decls.data[i]->pos, 0, i > 0); i += print_fit(&b, &decls, i, 0); bputc(&b, '\n'); } fmt_before(&b, (Pos){file, (i32)text.len, 0, 0}, 0, 1); fmt_file = 0; split_lists.len = 0; // the check below compares the programs, not their layout Str plain = decls_text(&decls); Str out = bstr(&b); // the formatted text, parsed again, prints the same program Nodes again = parse_file(add_file(path, out, 0)); Str check = decls_text(&again); #ifdef FUZZ if (os_getenv(S("STAR99_FUZZ_VERBOSE")).len && !equals(plain, check)) os_write(2, fmtstr("--- parsed\n%s--- formatted and parsed again\n%s", plain, check)); assert(equals(plain, check)); #endif if (!equals(plain, check)) fatal("formatting %s would change it; this is a bug in star99 fmt", path); return out; } UNUSED static int run_fmt(Strs args) { b32 to_stdout = 0; int nfiles = 0; for (iz i = 2; i < args.len; i++) { Str path = args.data[i]; if (equals(path, S("--stdout"))) { to_stdout = 1; continue; } nfiles++; Contents text = os_readfile(path); if (!text.ok) fatal("cannot read %s", path); Str out = format_file(path, text.data); if (to_stdout) os_write(1, out); else if (!equals(out, text.data) && !os_writefile(path, out)) fatal("cannot write %s", path); } if (!nfiles) fatal("usage: star99 fmt [--stdout] FILE..."); return 0; } // One entry of an AFL++ dictionary: w quoted, with its quotes (and, for // all, its backslashes) escaped. static void dict_entry(Buf *b, Str w, b32 all) { bputc(b, '"'); for (iz i = 0; i < w.len; i++) { if (w.data[i] == '"' || (all && w.data[i] == '\\')) bputc(b, '\\'); bputc(b, w.data[i]); } bput(b, S("\"\n")); } // star99 dict: an AFL++ dictionary from the lexer's own token table. UNUSED static int print_dict(void) { Buf b = {0}; for (iz i = T_STRING + 1; i < countof(tokname); i++) { // names and literals have no text Str t = tokname[i]; if (t.len > 2 && t.data[0] == '\'') t = substr(t, 1, t.len - 1); if (t.len) dict_entry(&b, t, 1); } for (iz i = 0; i < countof(builtin_table); i++) dict_entry(&b, concat(builtin_table[i].name, S("(")), 1); static Str extra[] = { SL("Int"), SL("Float"), SL("Bool"), SL("String"), SL("List("), SL("Map("), SL("Pool("), SL("Ref("), SL("Any"), SL("U8"), SL("I32"), SL("no_nil("), SL("code {"), SL("code("), SL("\"std:code\""), SL("Expr"), SL("Raw("), SL("&"), SL("'"), SL("'\\n'"), SL("\"\"\"")}; for (iz i = 0; i < countof(extra); i++) dict_entry(&b, extra[i], 0); os_write(1, bstr(&b)); return 0; } #ifdef FUZZ // The AFL++ entry point, in persistent mode: each test case is one program, // lexed, parsed, printed and parsed again, checked, and generated to C in // memory. Macros are checked and generated but not run, and nothing is // written. Between cases the arena chunks are freed and every STATE global // gets back the value it starts with. A compile error ends a case; a crash, // sanitizer report or failed assert is a finding. Without AFL++ it runs // one case from stdin. #ifndef __AFL_FUZZ_TESTCASE_LEN static u8 *fuzz_stdin = 0; static iz fuzz_stdin_len = 0; static int fuzz_once(void) { // STAR99_FUZZ_REPEAT=n runs the case n times, to check that the state // reset between cases is complete static int runs = 0; Str rep = os_getenv(S("STAR99_FUZZ_REPEAT")); if (runs++ >= (rep.len ? parse_int(rep) : 1)) return 0; if (fuzz_stdin) return 1; // outside the arena, which each case frees Str in = os_readfile(S("-")).data; fuzz_stdin = malloc((size_t)in.len + 1); if (in.len) memcpy(fuzz_stdin, in.data, (size_t)in.len); fuzz_stdin_len = in.len; return 1; } #define __AFL_FUZZ_INIT() #define __AFL_INIT() #define __AFL_FUZZ_TESTCASE_BUF fuzz_stdin #define __AFL_FUZZ_TESTCASE_LEN fuzz_stdin_len #define __AFL_LOOP(n) fuzz_once() #endif __AFL_FUZZ_INIT(); // the bounds of the STATE section, which the linker defines extern char __start_star99_state[], __stop_star99_state[]; int main(void) { // the STATE globals as the program starts (before any allocation), // put back after each case iz nstate = __stop_star99_state - __start_star99_state; char *state = malloc((size_t)nstate); if (!state) return 1; memcpy(state, __start_star99_state, (size_t)nstate); __AFL_INIT(); if (!os_getenv(S("STAR99_FUZZ_VERBOSE")).len && !os_redirect(2, S("/dev/null"))) return 1; while (__AFL_LOOP(10000)) { unsigned char *buf = __AFL_FUZZ_TESTCASE_BUF; iz len = (iz)__AFL_FUZZ_TESTCASE_LEN; // an exactly sized copy, so reading past the end is caught u8 *copy = malloc((size_t)(len ? len : 1)); if (len) memcpy(copy, buf, (size_t)len); fuzz_case = (Str){copy, len}; if (!setjmp(fuzz_done)) { init_types(); format_file(S("-"), fuzz_case); } if (!setjmp(fuzz_done)) build(S("-"), (Str){0}); free(copy); while (fuzz_chunks) { char *next = *(char **)fuzz_chunks; free(fuzz_chunks); fuzz_chunks = next; } memcpy(__start_star99_state, state, (size_t)nstate); } return 0; } #else int main(int argc, char **argv) { init_types(); Strs args = {0}; for (int i = 0; i < argc; i++) *push(&perm, &args) = cstr(argv[i]); Str cmd = args.len > 1 ? args.data[1] : (Str){0}; if (args.len >= 2 && equals(cmd, S("test"))) return run_tests(args); if (args.len == 2 && equals(cmd, S("dict"))) return print_dict(); if (args.len == 2 && equals(cmd, S("debug-script"))) { os_write(1, debug_script()); return 0; } if (args.len >= 2 && equals(cmd, S("fmt"))) return run_fmt(args); if (args.len >= 2 && equals(cmd, S("tree"))) return run_tree(args); if (args.len == 3 && equals(cmd, S("gen"))) { os_write(1, gen_program((u64)parse_int(args.data[2]))); return 0; } Str input = {0}, cout = {0}; for (iz i = 1; i < args.len; i++) { Str a = args.data[i]; if (equals(a, S("--"))) { iz n = args.len - i - 1; meta_args = (Strs){args.data + i + 1, n, n}; break; } if (equals(a, S("--expand"))) { expand_only = 1; } else if (equals(a, S("--debug"))) { debug_build = 1; } else if (equals(a, S("--gdb")) || equals(a, S("--lldb"))) { debug_build = 1; debugger = a.data[2] == 'g' ? S("gdb") : S("lldb"); } else if (equals(a, S("-o")) && i + 1 < args.len) { cout = args.data[++i]; } else if (a.len && a.data[0] == '-') { usage(); } else if (!input.len) { input = a; } else { usage(); } } if (!input.len || (debugger.len && (cout.len || expand_only))) usage(); // nothing to debug if (debug_build && !cout.len && !expand_only) { // build() and its command() compile with the debug flags too Str cc = os_getenv(S("STAR99_CC")); debug_cflags = find_debug_cflags(cc.len ? cc : S("cc"), star99_dir(is_dir(input) ? input : dirname_of(input))); debug_extra = os_getenv(S("STAR99_CFLAGS")); // a build() may compile with another compiler (Cc(compiler: ...)): // without the name each has for the uninstrumented stack static Str stack_flags[] = {SL(" -mllvm -asan-stack=0"), SL(" --param asan-stack=0")}; Buf shared = {0}; for (iz i = 0; i < debug_cflags.len;) { Str rest = substr(debug_cflags, i, debug_cflags.len); iz skip = 0; for (iz k = 0; k < countof(stack_flags) && !skip; k++) if (startswith(rest, stack_flags[k])) skip = stack_flags[k].len; if (skip) { i += skip; continue; } bputc(&shared, debug_cflags.data[i++]); } os_setenv(S("STAR99_CFLAGS"), fmtstr("%s %s", debug_extra, bstr(&shared))); debug_env = 1; } return build(input, cout); } #endif