Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-base

Source of release 0.41.0

0.41.0 / src / support / runtime.lucb

#==============================================================================================
#
#   runtime - The C runtime carried inside the compiler
#
#   DESCRIPTION:
#       The generated C includes `lucb_rt.h` and links `lucb_rt.c`; an `answer()` program
#       also links `start.c`. The three files live under `runtime/` and are copied here by
#       `tools/embed_runtime.py` as raw text, so a build needs nothing beside the binary
#       but the host C compiler. Do not edit this file; edit `runtime/` and rerun the tool.
#
#==============================================================================================

# mark: Runtime header (lucb_rt.h) ============================================================

## The text of `runtime/lucb_rt.h`.
pub let header: str = r"""//==============================================================================================
//
//   runtime/lucb_rt - Runtime interface for generated C
//
//   DESCRIPTION:
//       The types generated code uses (`lb_str`, `lb_span`, `lb_iface`, optionals and results
//       through `LB_OPT`/`LB_RES`), the inline checked arithmetic and bounds checks, and the
//       prototypes of everything in lucb_rt.c. Allocation, files, processes, threads, and
//       the sync primitives are Base source now (src/prelude.lucb); what remains here is
//       what generated code cannot spell itself.
//
//==============================================================================================

#pragma once

#include <math.h>
#include <stdarg.h>
#include <stdatomic.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

#if defined(__GNUC__)
#define LB_NORETURN __attribute__((noreturn))
#else
#define LB_NORETURN
#endif

/* A declaration bound to a symbol by name: `int f(void) LB_SYMBOL("f")` reaches C's `f`
   whatever the declaration is called, so a binding never clashes with a system prototype. */
#define LB_STR2(x) #x
#define LB_STR(x) LB_STR2(x)
#define LB_SYMBOL(name) __asm__(LB_STR(__USER_LABEL_PREFIX__) name)

/* The statement the thread is running, `file:line:column`, set by the code before each
   statement and restored when a function returns: what a trap names (§11.5). */
extern _Thread_local const char* lb_pos;
/* inline, so that a function the C compiler expands where it is called (`memory.read[T]`)
   leaves two stores of the position behind, not a call */
static inline void lb_restore_pos(const char** saved) {
    lb_pos = *saved;
}
/* The trap entry points: the generated program defines them, each calling `core`'s trap at
   `lb_pos`, so a trap writes the crash report and runs the crash handler (base.md §8.7). */
LB_NORETURN void lb_trap(const char* message);
LB_NORETURN void lb_trap_two(const char* message, const char* detail);
void lb_pause(void);

/* Checked arithmetic is inline so that `a + b` costs one instruction and one
   predicted branch after the host C compiler optimises (base.md §1, §7.2). */
static inline uint64_t mask_bits(int bits) {
    if (bits >= 64) {
        return ~(uint64_t)0;
    }
    if (bits <= 0) {
        return 0;
    }
    return ((uint64_t)1 << bits) - 1;
}
static inline int64_t smin(int bits) {
    if (bits <= 0) {
        return 0;
    }
    if (bits >= 64) {
        return INT64_MIN;
    }
    return -((int64_t)1 << (bits - 1));
}
static inline int64_t smax(int bits) {
    if (bits <= 0) {
        return 0;
    }
    if (bits >= 64) {
        return INT64_MAX;
    }
    return ((int64_t)1 << (bits - 1)) - 1;
}
static inline int64_t sext(int64_t a, int bits) {
    if (bits <= 0) {
        return 0;
    }
    if (bits >= 64) {
        return a;
    }
    uint64_t u = (uint64_t)a & mask_bits(bits);
    if (u & ((uint64_t)1 << (bits - 1))) {
        return (int64_t)(u | ~mask_bits(bits));
    }
    return (int64_t)u;
}
static inline uint64_t zext(uint64_t a, int bits) {
    return a & mask_bits(bits);
}

static inline int64_t lb_add_s(int64_t a, int64_t b, int bits) {
    a = sext(a, bits);
    b = sext(b, bits);
    int64_t r;
    if (__builtin_add_overflow(a, b, &r) || r < smin(bits) || r > smax(bits)) {
        lb_trap("integer overflow");
    }
    return r;
}

static inline uint64_t lb_add_u(uint64_t a, uint64_t b, int bits) {
    a = zext(a, bits);
    b = zext(b, bits);
    if (bits >= 64) {
        if (a > UINT64_MAX - b) {
            lb_trap("integer overflow");
        }
        return a + b;
    }
    uint64_t r = a + b;
    if (r > mask_bits(bits)) {
        lb_trap("integer overflow");
    }
    return r;
}

static inline int64_t lb_sub_s(int64_t a, int64_t b, int bits) {
    a = sext(a, bits);
    b = sext(b, bits);
    int64_t r;
    if (__builtin_sub_overflow(a, b, &r) || r < smin(bits) || r > smax(bits)) {
        lb_trap("integer overflow");
    }
    return r;
}

static inline uint64_t lb_sub_u(uint64_t a, uint64_t b, int bits) {
    a = zext(a, bits);
    b = zext(b, bits);
    if (a < b) {
        lb_trap("integer overflow");
    }
    return a - b;
}

static inline int64_t lb_mul_s(int64_t a, int64_t b, int bits) {
    a = sext(a, bits);
    b = sext(b, bits);
    int64_t r;
    if (__builtin_mul_overflow(a, b, &r) || r < smin(bits) || r > smax(bits)) {
        lb_trap("integer overflow");
    }
    return r;
}

static inline uint64_t lb_mul_u(uint64_t a, uint64_t b, int bits) {
    a = zext(a, bits);
    b = zext(b, bits);
    if (b != 0 && a > mask_bits(bits) / b) {
        lb_trap("integer overflow");
    }
    return zext(a * b, bits);
}





/* mode 0 = checked T(x), mode 1 = C cast (truncate / bit-reinterpret). */

// A `char` from an integer's bits: a scalar value or, when checked, a trap (§7.5).

/* The arithmetic, shifts and conversions the generated C calls at every operation, inline
   so that a release build compiles them into the expression that uses them. */
/* A divisor of zero, or the one quotient that overflows (the smallest value over -1), traps. */
static inline void lb_check_division(int64_t a, int64_t b, int bits) {
    if (b == 0) {
        lb_trap("division by zero");
    }
    if (a == smin(bits) && b == -1) {
        lb_trap("integer overflow");
    }
}

static inline int64_t lb_div_s(int64_t a, int64_t b, int bits) {
    a = sext(a, bits);
    b = sext(b, bits);
    lb_check_division(a, b, bits);
    return a / b;
}

static inline uint64_t lb_div_u(uint64_t a, uint64_t b, int bits) {
    a = zext(a, bits);
    b = zext(b, bits);
    if (b == 0) {
        lb_trap("division by zero");
    }
    return a / b;
}

static inline int64_t lb_mod_s(int64_t a, int64_t b, int bits) {
    a = sext(a, bits);
    b = sext(b, bits);
    lb_check_division(a, b, bits);
    return a % b;
}

static inline uint64_t lb_mod_u(uint64_t a, uint64_t b, int bits) {
    a = zext(a, bits);
    b = zext(b, bits);
    if (b == 0) {
        lb_trap("division by zero");
    }
    return a % b;
}

static inline int lb_qadd_s(int64_t a, int64_t b, int bits, int64_t* out) {
    a = sext(a, bits);
    b = sext(b, bits);
    int64_t r;
    if (__builtin_add_overflow(a, b, &r) || r < smin(bits) || r > smax(bits)) {
        return 0;
    }
    *out = r;
    return 1;
}

static inline int lb_qadd_u(uint64_t a, uint64_t b, int bits, uint64_t* out) {
    a = zext(a, bits);
    b = zext(b, bits);
    if (bits >= 64) {
        if (a > UINT64_MAX - b) {
            return 0;
        }
        *out = a + b;
        return 1;
    }
    uint64_t r = a + b;
    if (r > mask_bits(bits)) {
        return 0;
    }
    *out = r;
    return 1;
}

static inline int lb_qsub_s(int64_t a, int64_t b, int bits, int64_t* out) {
    a = sext(a, bits);
    b = sext(b, bits);
    int64_t r;
    if (__builtin_sub_overflow(a, b, &r) || r < smin(bits) || r > smax(bits)) {
        return 0;
    }
    *out = r;
    return 1;
}

static inline int lb_qsub_u(uint64_t a, uint64_t b, int bits, uint64_t* out) {
    a = zext(a, bits);
    b = zext(b, bits);
    if (a < b) {
        return 0;
    }
    *out = a - b;
    return 1;
}

static inline int lb_qmul_s(int64_t a, int64_t b, int bits, int64_t* out) {
    a = sext(a, bits);
    b = sext(b, bits);
    int64_t r;
    if (__builtin_mul_overflow(a, b, &r) || r < smin(bits) || r > smax(bits)) {
        return 0;
    }
    *out = r;
    return 1;
}

static inline int lb_qmul_u(uint64_t a, uint64_t b, int bits, uint64_t* out) {
    a = zext(a, bits);
    b = zext(b, bits);
    if (b != 0 && a > mask_bits(bits) / b) {
        return 0;
    }
    *out = zext(a * b, bits);
    return 1;
}

static inline int64_t lb_neg_s(int64_t a, int bits) {
    a = sext(a, bits);
    if (a == smin(bits)) {
        lb_trap("integer overflow");
    }
    return -a;
}

static inline int64_t lb_addw_s(int64_t a, int64_t b, int bits) {
    uint64_t r = (uint64_t)sext(a, bits) + (uint64_t)sext(b, bits);
    return sext((int64_t)(r & mask_bits(bits)), bits);
}

static inline uint64_t lb_addw_u(uint64_t a, uint64_t b, int bits) {
    return zext(a + b, bits);
}

static inline int64_t lb_subw_s(int64_t a, int64_t b, int bits) {
    uint64_t r = (uint64_t)sext(a, bits) - (uint64_t)sext(b, bits);
    return sext((int64_t)(r & mask_bits(bits)), bits);
}

static inline uint64_t lb_subw_u(uint64_t a, uint64_t b, int bits) {
    return zext(a - b, bits);
}

static inline int64_t lb_mulw_s(int64_t a, int64_t b, int bits) {
    uint64_t r = (uint64_t)sext(a, bits) * (uint64_t)sext(b, bits);
    return sext((int64_t)(r & mask_bits(bits)), bits);
}

static inline uint64_t lb_mulw_u(uint64_t a, uint64_t b, int bits) {
    return zext(a * b, bits);
}

static inline int64_t lb_negw_s(int64_t a, int bits) {
    uint64_t r = 0u - (uint64_t)sext(a, bits);
    return sext((int64_t)(r & mask_bits(bits)), bits);
}

static inline uint64_t lb_negw_u(uint64_t a, int bits) {
    return zext(0u - zext(a, bits), bits);
}

static inline int64_t lb_adds_s(int64_t a, int64_t b, int bits) {
    a = sext(a, bits);
    b = sext(b, bits);
    int64_t r;
    if (__builtin_add_overflow(a, b, &r) || r < smin(bits) || r > smax(bits)) {
        return (a < 0) ? smin(bits) : smax(bits);
    }
    return r;
}

static inline uint64_t lb_adds_u(uint64_t a, uint64_t b, int bits) {
    a = zext(a, bits);
    b = zext(b, bits);
    if (bits >= 64) {
        if (a > UINT64_MAX - b) {
            return UINT64_MAX;
        }
        return a + b;
    }
    uint64_t r = a + b;
    if (r > mask_bits(bits)) {
        return mask_bits(bits);
    }
    return r;
}

static inline int64_t lb_subs_s(int64_t a, int64_t b, int bits) {
    a = sext(a, bits);
    b = sext(b, bits);
    int64_t r;
    if (__builtin_sub_overflow(a, b, &r) || r < smin(bits) || r > smax(bits)) {
        return (a < 0) ? smin(bits) : smax(bits);
    }
    return r;
}

static inline uint64_t lb_subs_u(uint64_t a, uint64_t b, int bits) {
    a = zext(a, bits);
    b = zext(b, bits);
    if (a < b) {
        return 0;
    }
    return a - b;
}

static inline int64_t lb_muls_s(int64_t a, int64_t b, int bits) {
    a = sext(a, bits);
    b = sext(b, bits);
    int64_t r;
    if (__builtin_mul_overflow(a, b, &r) || r < smin(bits) || r > smax(bits)) {
        return ((a < 0) != (b < 0)) ? smin(bits) : smax(bits);
    }
    return r;
}

static inline uint64_t lb_muls_u(uint64_t a, uint64_t b, int bits) {
    a = zext(a, bits);
    b = zext(b, bits);
    if (b != 0 && a > mask_bits(bits) / b) {
        return mask_bits(bits);
    }
    return zext(a * b, bits);
}

static inline int64_t lb_shl_s(int64_t a, uint64_t n, int bits) {
    a = sext(a, bits);
    if (n >= (uint64_t)bits) {
        lb_trap("shift count out of range");
    }
    return sext((int64_t)(((uint64_t)a << n) & mask_bits(bits)), bits);
}

static inline uint64_t lb_shl_u(uint64_t a, uint64_t n, int bits) {
    a = zext(a, bits);
    if (n >= (uint64_t)bits) {
        lb_trap("shift count out of range");
    }
    return zext(a << n, bits);
}

static inline int64_t lb_shr_s(int64_t a, uint64_t n, int bits) {
    a = sext(a, bits);
    if (n >= (uint64_t)bits) {
        lb_trap("shift count out of range");
    }
    return a >> n;
}

static inline uint64_t lb_shr_u(uint64_t a, uint64_t n, int bits) {
    a = zext(a, bits);
    if (n >= (uint64_t)bits) {
        lb_trap("shift count out of range");
    }
    return a >> n;
}

static inline uint64_t lb_not_u(uint64_t a, int bits) {
    return zext(~a, bits);
}

static inline int fits_u(uint64_t v, int bits) {
    return v <= mask_bits(bits);
}

// The source of an integer conversion: `negative` when it is below zero, else the
// magnitude in `bits`; a 64-bit unsigned source above `INT64_MAX` is a magnitude too.
typedef struct {
    int negative;
    uint64_t bits;
} lb_source;

static inline lb_source source_of(uint64_t a, int from_bits, int from_signed) {
    lb_source s;
    if (from_signed) {
        int64_t v = sext((int64_t)a, from_bits);
        s.negative = v < 0;
        s.bits = (uint64_t)v;
    } else {
        s.negative = 0;
        s.bits = zext(a, from_bits);
    }
    return s;
}

// A checked conversion's result, as bits: out of the destination's range traps (§7.5).
static inline uint64_t checked_bits(lb_source s, int to_bits, int to_signed) {
    int bad;
    if (to_signed) {
        bad = s.negative ? (int64_t)s.bits < smin(to_bits) : s.bits > (uint64_t)smax(to_bits);
    } else {
        bad = s.negative || !fits_u(s.bits, to_bits);
    }
    if (bad) {
        lb_trap("integer conversion out of range");
    }
    return s.bits;
}

static inline int64_t lb_conv_s(int64_t a, int from_bits, int from_signed, int to_bits, int to_signed, int mode) {
    lb_source s = source_of((uint64_t)a, from_bits, from_signed);
    if (mode == 0) {
        return (int64_t)checked_bits(s, to_bits, to_signed);
    }
    uint64_t bits = s.bits & mask_bits(to_bits);
    if (to_signed) {
        return sext((int64_t)bits, to_bits);
    }
    return (int64_t)bits;
}

static inline uint64_t lb_conv_u(uint64_t a, int from_bits, int from_signed, int to_bits, int to_signed,
                   int mode) {
    lb_source s = source_of(a, from_bits, from_signed);
    if (mode == 0) {
        return checked_bits(s, to_bits, to_signed);
    }
    return zext(s.bits, to_bits);
}

static inline uint32_t lb_to_char(uint64_t a, int mode) {
    int bad = a > 0x10FFFF || (a >= 0xD800 && a <= 0xDFFF);
    if (bad && mode == 0) {
        lb_trap("integer conversion out of range");
    }
    return (uint32_t)a;
}

// `2^(bits-1)` and `2^bits` are exact doubles; the range checks compare against them
// rather than against the rounded `(double)smax`, which is `2^63` itself for 64 bits.
static inline int64_t lb_f_to_s(double a, int bits, int mode) {
    double hi = ldexp(1.0, bits - 1);
    double lo = -hi;
    if (mode == 0) {
        if (!isfinite(a) || a < lo || a >= hi) {
            lb_trap("integer conversion out of range");
        }
        return (int64_t)a;
    }
    if (isnan(a)) {
        return 0;
    }
    if (a <= lo) {
        return smin(bits);
    }
    if (a >= hi) {
        return smax(bits);
    }
    return (int64_t)a;
}

static inline uint64_t lb_f_to_u(double a, int bits, int mode) {
    double hi = ldexp(1.0, bits);
    if (mode == 0) {
        if (!isfinite(a) || a < 0 || a >= hi) {
            lb_trap("integer conversion out of range");
        }
        return (uint64_t)a;
    }
    if (isnan(a) || a < 0) {
        return 0;
    }
    if (a >= hi) {
        return mask_bits(bits);
    }
    return (uint64_t)a;
}

static inline double lb_to_f(int64_t a, int from_signed) {
    if (from_signed) {
        return (double)a;
    }
    return (double)(uint64_t)a;
}

static inline float lb_f64_to_f32(double a) {
    return (float)a;
}

typedef struct lb_str {
    const char* data;
    size_t length;
} lb_str;

/* Base diagnostic messages are byte spans, not NUL-terminated C strings. */
LB_NORETURN void lb_trap_text(lb_str message);
LB_NORETURN void lb_trap_detail(const char* prefix, lb_str detail);
/* A streamed trap message: a begin, its pieces streamed through `core`, then the end. */
void lb_trap_begin(void);
LB_NORETURN void lb_trap_end(void);

typedef struct lb_span {
    void* data;
    size_t length;
} lb_span;

/* A const span has the same representation as a span; constness is a Base
   fact the checker enforced, so one C struct serves both and no adapter
   is needed where a `T[]` meets a `const T[]`. */
typedef lb_span lb_cspan;

/* Ordering of text by bytes, then by length (base.md §5.5). */
int lb_str_compare(lb_str a, lb_str b);
void lb_print_i64(int64_t value);
void lb_print_u64(uint64_t value);
void lb_print_bool(bool value);
void lb_print_str(lb_str value);
static inline void lb_check_index(uint64_t i, uint64_t n) {
    if (i >= n) {
        lb_trap("index out of bounds");
    }
}
// The index `i` once checked against `n`: an index expression is evaluated once (§6.5).
/* A raw C cast borrows the address without reading past the byte view.
   C string consumers require caller-provided termination and sufficient lifetime. */
static inline char* lb_cstr_of(lb_str s) {
    return (char*)s.data;
}
static inline uint64_t lb_at(uint64_t i, uint64_t n) {
    lb_check_index(i, n);
    return i;
}
void lb_check_utf8(const char* s, size_t n);

typedef struct lb_iface {
    void* data;
    const void* vtable;
} lb_iface;

typedef struct lb_fmtbuf {
    char* data;
    size_t cap;
    size_t used;
    // what text past `cap` does: cut (LB_FMT_FIXED), move to the heap (LB_FMT_GROWS), or
    // grow on the heap it is on (LB_FMT_GROWN), as core.FormatBuffer
    size_t growth;
} lb_fmtbuf;

#define LB_FMT_FIXED 0
#define LB_FMT_GROWS 1
#define LB_FMT_GROWN 2

int lb_fmtbuf_put(lb_fmtbuf* b, const char* s, size_t n);
// a growing buffer over `cap` bytes at `data`, after freeing what it last took from the heap
void lb_fmtbuf_begin(lb_fmtbuf* b, char* data, size_t cap);
// free what a growing buffer took from the heap; the cleanup of a function's buffers
void lb_fmtbuf_release(lb_fmtbuf* b);
int lb_fmtbuf_i64(lb_fmtbuf* b, int64_t v);
int lb_fmtbuf_u64(lb_fmtbuf* b, uint64_t v);
int lb_fmtbuf_bool(lb_fmtbuf* b, bool v);
// The UTF-8 bytes of one scalar, and their count: how a `char` displays (§14.4).
size_t lucb_rt_utf8_encode(uint32_t cp, char out[4]);
int lb_fmtbuf_char(lb_fmtbuf* b, uint32_t cp);
lb_str lb_fmtbuf_finish(lb_fmtbuf* b);
// `print(f"...")` and `trap(f"...")`: the text gathers in `b` and goes out to `stream` when it
// passes half the buffer, and at its end (`lb_stream_flush`), as std's `core.stream_put`
// does natively, so a field that prints lands where it does in a native build. `stream` is
// a `FILE*`.
void lb_stream_put(lb_fmtbuf* b, const char* s, size_t n, void* stream);
void lb_stream_flush(lb_fmtbuf* b, void* stream);

int lb_utf8_ok(const char* s, size_t n);
// The scalar starting at byte `i` of the valid UTF-8 text `s`, and its width in `*width`.
uint32_t lb_utf8_scalar(const char* s, size_t n, size_t i, size_t* width);

uint64_t lb_hash_seed(void);
uint64_t lb_hash_mix(uint64_t h, uint64_t x);
uint64_t lb_hash_bytes(uint64_t h, const void* p, size_t n);

#define LB_FILES_MISSING 2
#define LB_INVALID_UTF8 3

// An `Error` (§11.3): its code; `at`, where it was raised, as its position text's distance
// from `lb_core_11origin_base` (`core.origin` reads it); and its message, whose length is 32
// bits, the word above it being `called`, where the error came out of a call into another
// package, kept as `at` is (`called_at`; 0 for none). Writing `message` whole clears
// `called`, since a length is below 2^32; `lb_message_of` reads the message back and
// `lb_message_set` replaces it with `called` kept.
typedef struct lb_error {
    int32_t code;
    int32_t at;
    union {
        lb_str message;
        struct {
            const char* data;
            uint32_t length;
            int32_t called;
        } m;
    };
} lb_error;

static inline lb_str lb_message_of(lb_error e) {
    return (lb_str){ e.m.data, e.m.length };
}

static inline void lb_message_set(lb_error* e, lb_str message) {
    e->m.data = message.data;
    e->m.length = (uint32_t)message.length;
}

#define LB_OPT(T, name)                                                                            \
    typedef struct name {                                                                          \
        T value;                                                                                   \
        bool present;                                                                              \
    } name

#define LB_RES(T, name)                                                                            \
    typedef struct name {                                                                          \
        T value;                                                                                   \
        lb_error error;                                                                            \
        bool failed;                                                                               \
    } name

typedef struct lb_r_unit {
    lb_error error;
    bool failed;
} lb_r_unit;

/* The startup shim's pieces, shared by the C and native backends: the argument
   vector as `str[]` (checked) or `c.str[]`, a failed `main`, and the test runner's
   report lines. */
lb_span lb_arguments(int argc, char** argv, bool as_text);
// A failed `main`: `error`, raised at `at` and come out of the call at `called`, on stderr;
// the exit status.
int lb_entry_failed(lb_error error, lb_str at, lb_str called);
// The `at` of an error raised at the running statement (`lb_pos`), which the generated code
// defines beside `core` (§11.3).
int32_t lb_here(void);

"""

# mark: Runtime source (lucb_rt.c) ============================================================

## The text of `runtime/lucb_rt.c`.
pub let source: str = r"""//==============================================================================================
//
//   runtime/lucb_rt - The C runtime linked into every Base program
//
//   DESCRIPTION:
//       The wrapping and saturating arithmetic families, conversions, formatted display of
//       scalars, UTF-8 validation, and hashing. Base has no runtime of its own (base.md
//       §1.3); the standard modules are Base source, and this is the helpers the generated C
//       calls by name. Traps are `core`'s: the generated program defines the `lb_trap`
//       entry points lucb_rt.h declares, each calling `core` at the position `lb_pos` holds.
//
//==============================================================================================

#include "lucb_rt.h"

#include <inttypes.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// make room in a growing buffer for `need` bytes and a NUL, at least doubling it
static bool lb_fmtbuf_grow(lb_fmtbuf* b, size_t need) {
    size_t size = b->cap * 2;
    if (size <= need) {
        size = need + 1;
    }
    char* data;
    if (b->growth == LB_FMT_GROWN) {
        data = realloc(b->data, size);
        if (data == NULL) {
            return false;
        }
    } else {
        data = malloc(size);
        if (data == NULL) {
            return false;
        }
        memcpy(data, b->data, b->used);
        b->growth = LB_FMT_GROWN;
    }
    b->data = data;
    b->cap = size;
    return true;
}

void lb_fmtbuf_release(lb_fmtbuf* b) {
    if (b->growth == LB_FMT_GROWN) {
        free(b->data);
        b->growth = LB_FMT_GROWS;
        b->cap = 0;
        b->used = 0;
    }
}

void lb_fmtbuf_begin(lb_fmtbuf* b, char* data, size_t cap) {
    lb_fmtbuf_release(b);
    b->data = data;
    b->cap = cap;
    b->used = 0;
    b->growth = LB_FMT_GROWS;
}

int lb_fmtbuf_put(lb_fmtbuf* b, const char* s, size_t n) {
    if (b == NULL) {
        return 1;
    }
    if (b->used > b->cap) {
        return 1;
    }
    if (n > b->cap - b->used && !(b->growth != LB_FMT_FIXED && lb_fmtbuf_grow(b, b->used + n))) {
        // what fits is kept; over capacity, `format` fails and `lb_fmtbuf_finish` marks the cut
        if (s != NULL && b->cap > b->used) {
            memcpy(b->data + b->used, s, b->cap - b->used);
        }
        b->used = b->cap + 1;
        return 1;
    }
    if (s != NULL && n > 0) {
        memcpy(b->data + b->used, s, n);
    }
    b->used += n;
    return 0;
}

int lb_fmtbuf_i64(lb_fmtbuf* b, int64_t v) {
    char tmp[32];
    int n = snprintf(tmp, sizeof(tmp), "%" PRId64, v);
    if (n < 0) {
        return 1;
    }
    return lb_fmtbuf_put(b, tmp, (size_t)n);
}

int lb_fmtbuf_u64(lb_fmtbuf* b, uint64_t v) {
    char tmp[32];
    int n = snprintf(tmp, sizeof(tmp), "%" PRIu64, v);
    if (n < 0) {
        return 1;
    }
    return lb_fmtbuf_put(b, tmp, (size_t)n);
}

int lb_fmtbuf_bool(lb_fmtbuf* b, bool v) {
    const char* s = v ? "true" : "false";
    return lb_fmtbuf_put(b, s, v ? 4 : 5);
}

size_t lucb_rt_utf8_encode(uint32_t cp, char out[4]) {
    size_t n = 0;
    if (cp < 0x80) {
        out[n++] = (char)cp;
    } else if (cp < 0x800) {
        out[n++] = (char)(0xC0 | (cp >> 6));
        out[n++] = (char)(0x80 | (cp & 0x3F));
    } else if (cp < 0x10000) {
        out[n++] = (char)(0xE0 | (cp >> 12));
        out[n++] = (char)(0x80 | ((cp >> 6) & 0x3F));
        out[n++] = (char)(0x80 | (cp & 0x3F));
    } else {
        out[n++] = (char)(0xF0 | (cp >> 18));
        out[n++] = (char)(0x80 | ((cp >> 12) & 0x3F));
        out[n++] = (char)(0x80 | ((cp >> 6) & 0x3F));
        out[n++] = (char)(0x80 | (cp & 0x3F));
    }
    return n;
}

int lb_fmtbuf_char(lb_fmtbuf* b, uint32_t cp) {
    char out[4];
    return lb_fmtbuf_put(b, out, lucb_rt_utf8_encode(cp, out));
}

lb_str lb_fmtbuf_finish(lb_fmtbuf* b) {
    lb_str s;
    s.data = "";
    s.length = 0;
    if (b == NULL) {
        return s;
    }
    if (b->used < b->cap) {
        b->data[b->used] = 0;
    }
    if (b->used > b->cap) {
        // cut at a character boundary and end in "...", so long text is never silently empty
        if (b->cap < 3) {
            return s;
        }
        size_t end = b->cap - 3;
        while (end > 0 && ((unsigned char)b->data[end] & 0xC0) == 0x80) {
            end--;
        }
        memcpy(b->data + end, "...", 3);
        s.data = b->data;
        s.length = end + 3;
        return s;
    }
    s.data = b->data;
    s.length = b->used;
    return s;
}

void lb_stream_put(lb_fmtbuf* b, const char* s, size_t n, void* stream) {
    if (b->used > b->cap || n > b->cap - b->used) {
        lb_stream_flush(b, stream);
    }
    if (n >= b->cap) {
        fwrite(s, 1, n, (FILE*)stream);
        return;
    }
    (void)lb_fmtbuf_put(b, s, n);
    if (b->used > b->cap / 2) {
        lb_stream_flush(b, stream);
    }
}

void lb_stream_flush(lb_fmtbuf* b, void* stream) {
    if (b->used > 0) {
        lb_str text = lb_fmtbuf_finish(b);
        fwrite(text.data, 1, text.length, (FILE*)stream);
    }
    b->used = 0;
}

lb_span lb_arguments(int argc, char** argv, bool as_text) {
    lb_span args;
    if (!as_text) {
        args.data = (void*)argv;
        args.length = (size_t)argc;
        return args;
    }
    lb_str* items = (lb_str*)malloc((size_t)(argc > 0 ? argc : 1) * sizeof(lb_str));
    if (items == NULL) {
        lb_trap("memory.exhausted");
    }
    for (int i = 0; i < argc; i++) {
        size_t n = 0;
        while (argv[i][n]) {
            n++;
        }
        lb_check_utf8(argv[i], n);
        items[i].data = argv[i];
        items[i].length = n;
    }
    args.data = items;
    args.length = (size_t)argc;
    return args;
}

int lb_entry_failed(lb_error error, lb_str at, lb_str called) {
    // as core.main_failed: where the error was raised, the message, the package's own number
    // of the code, then the call into another package it came out of when that differs
    lb_str message = lb_message_of(error);
    if (at.length > 0) {
        fprintf(stderr, "error: %.*s: ", (int)at.length, at.data);
    } else {
        fprintf(stderr, "error: ");
    }
    fprintf(stderr, "%.*s (code %u)\n", (int)message.length, message.data, (unsigned)error.code & 0xFFFFu);
    if (called.length > 0 && (called.length != at.length || memcmp(called.data, at.data, at.length) != 0)) {
        fprintf(stderr, "  called at %.*s\n", (int)called.length, called.data);
    }
    return 1;
}

int lb_str_compare(lb_str a, lb_str b) {
    size_t n = a.length < b.length ? a.length : b.length;
    int c = n == 0 ? 0 : memcmp(a.data, b.data, n);
    if (c != 0) {
        return c < 0 ? -1 : 1;
    }
    if (a.length < b.length) {
        return -1;
    }
    return a.length > b.length ? 1 : 0;
}

_Thread_local const char* lb_pos = "";

void lb_pause(void) {
#if defined(__aarch64__)
    __asm__ volatile("yield");
#elif defined(__x86_64__)
    __builtin_ia32_pause();
#else
    (void)0;
#endif
}

int64_t* lb_null_probe(void) {
    return NULL;
}

int lb_utf8_ok(const char* s, size_t n) {
    size_t i = 0;
    if (s == NULL) {
        return n == 0;
    }
    while (i < n) {
        unsigned char c = (unsigned char)s[i];
        size_t w = 1;
        uint32_t cp = 0;
        if (c < 0x80) {
            w = 1;
            cp = c;
        } else if ((c & 0xE0) == 0xC0) {
            w = 2;
            cp = c & 0x1Fu;
        } else if ((c & 0xF0) == 0xE0) {
            w = 3;
            cp = c & 0x0Fu;
        } else if ((c & 0xF8) == 0xF0) {
            w = 4;
            cp = c & 0x07u;
        } else {
            return 0;
        }
        if (i + w > n) {
            return 0;
        }
        for (size_t k = 1; k < w; k++) {
            unsigned char x = (unsigned char)s[i + k];
            if ((x & 0xC0) != 0x80) {
                return 0;
            }
            cp = (cp << 6) | (uint32_t)(x & 0x3Fu);
        }
        if (w == 2 && cp < 0x80) {
            return 0;
        }
        if (w == 3 && cp < 0x800) {
            return 0;
        }
        if (w == 4 && cp < 0x10000) {
            return 0;
        }
        if (cp >= 0xD800 && cp <= 0xDFFF) {
            return 0;
        }
        if (cp > 0x10FFFFu) {
            return 0;
        }
        i += w;
    }
    return 1;
}

uint32_t lb_utf8_scalar(const char* s, size_t n, size_t i, size_t* width) {
    const unsigned char* b = (const unsigned char*)s;
    unsigned char c = b[i];
    size_t w = 1;
    uint32_t cp = c;
    if (c >= 0xF0) {
        w = 4;
        cp = c & 0x07u;
    } else if (c >= 0xE0) {
        w = 3;
        cp = c & 0x0Fu;
    } else if (c >= 0xC0) {
        w = 2;
        cp = c & 0x1Fu;
    }
    if (i + w > n) {
        w = n - i;
    }
    for (size_t k = 1; k < w; k++) {
        cp = (cp << 6) | (b[i + k] & 0x3Fu);
    }
    *width = w;
    return cp;
}

void lb_check_utf8(const char* s, size_t n) {
    if (!lb_utf8_ok(s, n)) {
        lb_trap("invalid_utf8");
    }
}

static uint64_t lb_seed;
static int lb_seed_set;

uint64_t lb_hash_seed(void) {
    if (!lb_seed_set) {
        lb_seed = 0x9E3779B97F4A7C15ULL ^ (uint64_t)(uintptr_t)&lb_seed;
        lb_seed_set = 1;
    }
    return lb_seed;
}

uint64_t lb_hash_mix(uint64_t h, uint64_t x) {
    h ^= x;
    h *= 0x9E3779B97F4A7C15ULL;
    h ^= h >> 32;
    return h;
}

uint64_t lb_hash_bytes(uint64_t h, const void* p, size_t n) {
    const unsigned char* b = (const unsigned char*)p;
    for (size_t i = 0; i < n; i++) {
        h = lb_hash_mix(h, b[i]);
    }
    h = lb_hash_mix(h, (uint64_t)n);
    return h;
}

void lb_print_i64(int64_t value) {
    printf("%" PRId64 "\n", value);
}

void lb_print_u64(uint64_t value) {
    printf("%" PRIu64 "\n", value);
}

void lb_print_bool(bool value) {
    puts(value ? "true" : "false");
}

void lb_print_str(lb_str value) {
    printf("%.*s\n", (int)value.length, value.data != NULL ? value.data : "");
}

"""

# mark: Process entry (start.c) ===============================================================

## The text of `runtime/start.c`.
pub let start: str = r"""//==============================================================================================
//
//   runtime/start - Process entry for answer() programs
//
//   DESCRIPTION:
//       Calls `answer()` and prints the result; a program with `main` gets its shim from the
//       emitter instead. The startup symbols are spelled as `back/names.lucb` spells them.
//
//==============================================================================================

#include "lucb_rt.h"

#include <inttypes.h>
#include <stdio.h>

int64_t lb_answer(void);
void lb_memory_startup(void);
void lb_core_13process_start(void);

int main(void) {
    lb_core_13process_start();
    lb_memory_startup();
    int64_t value = lb_answer();
    printf("%" PRId64 "\n", value);
    return 0;
}
"""