/* flan_rt — the milestone-2 host ABI. * * This is the whole of it: argv, stdout, exit, and four text conversions * (plan.org, Milestone-2 primitives). Keeping the list this short is what * makes the wasm32 target cheap, because a primitive is the only thing * implemented twice. * * Every function here takes and returns scalars or an out-pointer. Nothing * returns a struct by value: the emitted .ll would then have to agree with the * platform's struct-return ABI, which is exactly the kind of thing that works * on x86-64 and silently does not on wasm32. */ #include #include #include #include /* ── Conditions, spec-conditions.md ────────────────────────────────── */ /* A handler stack, and nothing more. signal walks it, calls every frame whose * type matches, and returns; a handler that returns normally leaves the * signalling function to carry on, and with an empty stack signal is a null * check. Nothing here transfers control — restart-case is what will, and it * needs a calling convention this does not. * * Frames are allocated by the caller, on its own stack: establishing a handler * is two stores and a push. The condition crosses as a pointer because a * condition is a struct and the handler runs while the signalling frame is * still alive, so there is nothing to copy. * * A type is a number rather than a pointer to anything, so that a module * compiled later against a running program agrees with it: see Check.type_id. */ typedef struct flan_handler { struct flan_handler *prev; uint32_t type_id; void (*fn)(void *condition, void *xfer); } flan_handler; static flan_handler *handlers; void flan_handler_push(flan_handler *h) { h->prev = handlers; handlers = h; } void flan_handler_pop(flan_handler *h) { /* By frame, not by count: restoring what this frame displaced is correct * even if something below it got the stack out of step. */ handlers = h->prev; } /* [xfer] is the signalling function's own end of the transfer channel * (spec-conditions.md §6), threaded through so that a handler invoking a * restart can write its target into it. That makes this C frame transparent to * a transfer, which it has to be: a handler is always reached through here, so * the rule that a transfer cannot cross a foreign frame would otherwise make * restart-case useless. * * A handler that transfers stops the walk. The remaining handlers are for a * signal that is still looking for someone; this one has been answered. */ void flan_signal(uint32_t type_id, void *condition, void *xfer) { for (flan_handler *h = handlers; h != NULL; h = h->prev) if (h->type_id == type_id) { h->fn(condition, xfer); if (*(void **)xfer != NULL) return; } } /* A restart stack, the same shape and for the same reasons. What a transfer * carries is the *address* of one of these frames, not a number: the frame is * allocated by the restart-case that offers it, on its own stack, so the * address is unique against every module a running program may later load and * against every re-entry of the same restart-case. §4's "innermost frame * offering the name" is then just the order of the walk. */ typedef struct flan_restart { struct flan_restart *prev; uint32_t name_id; } flan_restart; static flan_restart *restarts; void flan_restart_push(flan_restart *r) { r->prev = restarts; restarts = r; } void flan_restart_pop(flan_restart *r) { restarts = r->prev; } void *flan_find_restart(uint32_t name_id) { for (flan_restart *r = restarts; r != NULL; r = r->prev) if (r->name_id == name_id) return r; return NULL; } /* [T] and string are both ptr+len — see Emit.ll. */ typedef struct { const uint8_t *ptr; int64_t len; } flan_slice; static int rt_argc; static char **rt_argv; static flan_slice *rt_args; /* argv as [string], built once, never freed */ void flan_rt_init(int32_t argc, char **argv) { rt_argc = (int)argc; rt_argv = argv; /* Line buffered even when stdout is a file or a pipe, where the C default is * a 4K block. A Flan program can run for minutes with a REPL attached to it, * and output that only appears when it exits is output nobody can use. It is * also what makes a program's progress observable to a test that is driving * it. The cost is one write per line instead of per 4K. */ setvbuf(stdout, NULL, _IOLBF, 0); } void flan_argv(flan_slice *out) { if (rt_args == NULL && rt_argc > 0) { rt_args = (flan_slice *)malloc(sizeof(flan_slice) * (size_t)rt_argc); for (int i = 0; i < rt_argc; i++) { rt_args[i].ptr = (const uint8_t *)rt_argv[i]; rt_args[i].len = (int64_t)strlen(rt_argv[i]); } } out->ptr = (const uint8_t *)rt_args; out->len = (int64_t)rt_argc; } void flan_write_stdout(const uint8_t *p, int64_t n) { if (n > 0) fwrite(p, 1, (size_t)n, stdout); } void flan_exit(int32_t status) { fflush(stdout); exit((int)status); } /* The conversions are *text*: bytes->f64 parses "12.5", f64->bytes renders it. * calc-me's tokenizer needs the first, the prelude's printers the second. */ #define SCRATCH 64 static char scratch[SCRATCH]; /* rendered text lives here until the next call */ double flan_bytes_to_f64(const uint8_t *p, int64_t n) { char buf[512]; size_t k = (size_t)n < sizeof buf - 1 ? (size_t)n : sizeof buf - 1; memcpy(buf, p, k); buf[k] = '\0'; return strtod(buf, NULL); } int64_t flan_bytes_to_i64(const uint8_t *p, int64_t n) { char buf[64]; size_t k = (size_t)n < sizeof buf - 1 ? (size_t)n : sizeof buf - 1; memcpy(buf, p, k); buf[k] = '\0'; return (int64_t)strtoll(buf, NULL, 10); } /* %g so that 3.5 prints as "3.5" and not "3.500000" — calc-me's expected * output is a table of exact strings. */ void flan_f64_to_bytes(double x, flan_slice *out) { int n = snprintf(scratch, SCRATCH, "%g", x); out->ptr = (const uint8_t *)scratch; out->len = n < 0 ? 0 : (int64_t)n; } void flan_i64_to_bytes(int64_t x, flan_slice *out) { int n = snprintf(scratch, SCRATCH, "%lld", (long long)x); out->ptr = (const uint8_t *)scratch; out->len = n < 0 ? 0 : (int64_t)n; } /* Bounds failures. The emitted code branches here and then falls off the end * with `unreachable`, so these must not return — the same explicit shape as * every other non-local exit, which is what keeps wasm32 free of unwinding. * * The location is passed as ptr+len because that is what a Flan string already * is; nothing here allocates. Exit 134 is abort()'s status without abort()'s * signal, so the same assertion should hold once wasm32 builds. * * stdout is flushed *before* the message: stderr is unbuffered and a * redirected stdout is not, so without this the error appears above the output * that led to it. */ static _Noreturn void rt_die(void) { fflush(stdout); fflush(stderr); exit(134); } _Noreturn void flan_bounds_fail(const uint8_t *loc, int64_t loclen, int64_t idx, int64_t len) { fflush(stdout); fprintf(stderr, "%.*s: index %lld is out of bounds for length %lld\n", (int)loclen, (const char *)loc, (long long)idx, (long long)len); rt_die(); } /* §2's diverging variant: the same walk, but a handler that returns normally * has not answered it. Only a transfer gets past here — the caller's guard * sees the channel and forwards it — so with nothing transferring the program * stops. In a dev build this is where the break loop will go; until it exists, * stopping is all there is, and it says which condition it was. * * [flan_signal] is not reused with a flag because the two differ in what they * do when the walk ends, which is the whole of §1 against §2. */ void flan_error(uint32_t type_id, void *condition, void *xfer, const uint8_t *name, int64_t namelen) { flan_signal(type_id, condition, xfer); if (*(void **)xfer != NULL) return; fflush(stdout); fprintf(stderr, "unhandled %.*s\n", (int)namelen, (const char *)name); rt_die(); } /* Nothing on the restart stack offers the name. It is reported where the * invoke was, because that is the only place that knows what was asked for; * there is nowhere to resume, so there is nothing else to do. */ _Noreturn void flan_restart_fail(const uint8_t *loc, int64_t loclen, const uint8_t *name, int64_t namelen) { fflush(stdout); fprintf(stderr, "%.*s: no restart named %.*s is active\n", (int)loclen, (const char *)loc, (int)namelen, (const char *)name); rt_die(); } /* Something a defer called invoked a restart. A defer is the cleanup a * transfer runs on its way out (§5), so a transfer starting there would leave * this frame's defers half run with two targets and no way to choose. The * lexical case is refused by the checker; this is the one that reaches a * function through a call, where nothing static could see it. */ _Noreturn void flan_transfer_fail(const uint8_t *loc, int64_t loclen) { fflush(stdout); fprintf(stderr, "%.*s: a defer invoked a restart, which a defer may not do — it is " "the cleanup a transfer runs on its way out\n", (int)loclen, (const char *)loc); rt_die(); } _Noreturn void flan_slice_fail(const uint8_t *loc, int64_t loclen, int64_t lo, int64_t hi, int64_t len) { fflush(stdout); fprintf(stderr, "%.*s: slice [%lld %lld) is out of bounds for length %lld\n", (int)loclen, (const char *)loc, (long long)lo, (long long)hi, (long long)len); rt_die(); }