
louise
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#1199 · main · 10 min 33 s
1Étoiles
0Forks
0Issues
| 1 | /* |
| 2 | * louise_rt.h -- Louise runtime ABI, version 2 (ABI v0 + Revision 1 calls, |
| 3 | * big integers). |
| 4 | * |
| 5 | * This header is the contract between C emitted by `louise` and the Rust |
| 6 | * runtime (`runtime/louise_rt`). Layouts here are mirrored in Rust with |
| 7 | * #[repr(C)] and checked on both sides. Rationale lives in |
| 8 | * docs/runtime-abi.md; change both together and bump TL_ABI_VERSION. |
| 9 | * |
| 10 | * Fast paths (tagging, dup/drop, yield/preemption checks) are static inline |
| 11 | * so generated code never pays a call for the common case. |
| 12 | * |
| 13 | * Two audiences. Generated code may use everything here. C bindings |
| 14 | * (`@extern` functions written by hand or by `louise bindgen`; ADR 0005 and |
| 15 | * 0015) use only: value tagging (tl_int, tl_int_get, tl_is_ptr, TL_UNIT, |
| 16 | * ...), tl_dup and tl_drop, strings (tl_string_from, tl_string_len, |
| 17 | * tl_string_ptr, tl_string_cstr), tl_int_to_i64 and tl_int_from_i64, |
| 18 | * floats (tl_float_from, tl_float_get), resources (tl_resource_new, |
| 19 | * tl_resource_get), the prelude builders (tl_bool, tl_some, tl_ok, |
| 20 | * tl_error), tl_panic_str and callbacks (tl_callback_*). Everything else |
| 21 | * is for generated code only. |
| 22 | */ |
| 23 | #ifndef LOUISE_RT_H |
| 24 | #define LOUISE_RT_H |
| 25 | |
| 26 | #include <stdbool.h> |
| 27 | #include <stddef.h> |
| 28 | #include <stdint.h> |
| 29 | #include <string.h> |
| 30 | |
| 31 | #ifdef __cplusplus |
| 32 | extern "C" { |
| 33 | #endif |
| 34 | |
| 35 | #define TL_ABI_VERSION 12 |
| 36 | |
| 37 | #define tl_likely(x) __builtin_expect(!!(x), 1) |
| 38 | #define tl_unlikely(x) __builtin_expect(!!(x), 0) |
| 39 | |
| 40 | /* ------------------------------------------------------------------------ |
| 41 | * Values |
| 42 | * |
| 43 | * Every Louise value is one machine word: |
| 44 | * low bit 1 -> 63-bit signed small int (also Bool, Unit, nullary ctors) |
| 45 | * low bit 0 -> pointer to an 8-byte aligned tl_block |
| 46 | * An Int outside 63 bits is a TL_TAG_BIGINT block; an Int that fits is |
| 47 | * always small (canonical form, ADR 0014). |
| 48 | * ---------------------------------------------------------------------- */ |
| 49 | typedef uintptr_t tl_value; |
| 50 | |
| 51 | _Static_assert(sizeof(tl_value) == 8, "Louise v0 targets 64-bit platforms"); |
| 52 | |
| 53 | #define TL_INT_MIN (-(INT64_C(1) << 62)) |
| 54 | #define TL_INT_MAX ((INT64_C(1) << 62) - 1) |
| 55 | |
| 56 | static inline bool tl_is_int(tl_value v) { return (v & 1) != 0; } |
| 57 | static inline bool tl_is_ptr(tl_value v) { return (v & 1) == 0; } |
| 58 | |
| 59 | /* Caller guarantees TL_INT_MIN <= n <= TL_INT_MAX. */ |
| 60 | static inline tl_value tl_int(int64_t n) { return ((uint64_t)n << 1) | 1; } |
| 61 | /* Relies on arithmetic right shift of signed values (GCC/Clang guarantee). */ |
| 62 | static inline int64_t tl_int_get(tl_value v) { return (int64_t)v >> 1; } |
| 63 | |
| 64 | #define TL_UNIT ((tl_value)1) /* tl_int(0) */ |
| 65 | #define TL_FALSE ((tl_value)1) /* tl_int(0) */ |
| 66 | #define TL_TRUE ((tl_value)3) /* tl_int(1) */ |
| 67 | |
| 68 | /* ------------------------------------------------------------------------ |
| 69 | * Heap blocks |
| 70 | * |
| 71 | * [ tl_header | scanned fields: tl_value[scan_fsize] | raw bytes ] |
| 72 | * |
| 73 | * Scanned fields are visited by drop and deep-copy. The raw area's size is |
| 74 | * a function of the tag (see docs/runtime-abi.md); user constructors have |
| 75 | * no raw area. |
| 76 | * ---------------------------------------------------------------------- */ |
| 77 | typedef struct tl_header { |
| 78 | int32_t rc; /* >0: local count (1 = unique); <0: shared, count = -rc */ |
| 79 | uint16_t tag; /* constructor tag or TL_TAG_* runtime kind */ |
| 80 | uint8_t scan_fsize; /* scanned field count; TL_SCAN_LARGE: see below */ |
| 81 | uint8_t flags; /* TL_FLAG_* */ |
| 82 | } tl_header; |
| 83 | |
| 84 | typedef struct tl_block { |
| 85 | tl_header h; |
| 86 | tl_value fields[]; |
| 87 | } tl_block; |
| 88 | |
| 89 | _Static_assert(sizeof(tl_header) == 8, "tl_header must be one word"); |
| 90 | _Static_assert(offsetof(tl_block, fields) == 8, "fields follow the header"); |
| 91 | |
| 92 | /* scan_fsize == TL_SCAN_LARGE: fields[0] is tl_int(n) and fields[1..=n] are scanned. */ |
| 93 | #define TL_SCAN_LARGE 0xFF |
| 94 | |
| 95 | /* Pinned blocks cannot be deep-copied into another actor (send fails). */ |
| 96 | #define TL_FLAG_PINNED 0x01 |
| 97 | /* A reuse token whose kept fields are dead (their bindings own the |
| 98 | * values): freeing it frees the cell only (ADR 0040 Revision 2, issue |
| 99 | * #193; runtime ABI 8). */ |
| 100 | #define TL_FLAG_TOKEN 0x02 |
| 101 | |
| 102 | /* Tags below TL_TAG_RUNTIME_MIN are user constructors (no raw area). */ |
| 103 | #define TL_TAG_RUNTIME_MIN 0xFF00 |
| 104 | #define TL_TAG_STRING 0xFF01 /* raw: int64 len; char bytes[len]; '\0' */ |
| 105 | #define TL_TAG_BYTES 0xFF02 /* raw: int64 len; uint8 bytes[len] */ |
| 106 | #define TL_TAG_FLOAT 0xFF03 /* raw: double */ |
| 107 | #define TL_TAG_CLOSURE 0xFF04 /* fields: env; raw: owned + borrowed code */ |
| 108 | #define TL_TAG_REF 0xFF05 /* fields: [value]; no raw */ |
| 109 | #define TL_TAG_REGION 0xFF06 /* raw: runtime-private list of refs */ |
| 110 | #define TL_TAG_FRAME 0xFF07 /* raw: runtime-private `ctl` handler */ |
| 111 | #define TL_TAG_RESOURCE 0xFF08 /* raw: void *ptr; finalizer */ |
| 112 | #define TL_TAG_CAP 0xFF09 /* fields: clauses, marker; no raw */ |
| 113 | #define TL_TAG_ARRAY 0xFF0A /* TL_SCAN_LARGE; raw: int64 spare; slots */ |
| 114 | #define TL_TAG_BIGINT 0xFF0B /* raw: int64 ±nlimbs; uint64 limbs[n] */ |
| 115 | #define TL_TAG_BUILDER 0xFF0C /* raw: int64 len; int64 cap; bytes[cap] */ |
| 116 | #define TL_TAG_U64 0xFF0D /* raw: uint64 (std/u64, ADR 0035) */ |
| 117 | #define TL_TAG_DEC 0xFF0E /* raw: 2×uint64 (an i128's limbs, lo * |
| 118 | * first), 10^18-scaled (std/dec) */ |
| 119 | |
| 120 | static inline tl_block *tl_as_block(tl_value v) { return (tl_block *)v; } |
| 121 | static inline tl_value tl_from_block(tl_block *b) { return (tl_value)b; } |
| 122 | |
| 123 | /* ------------------------------------------------------------------------ |
| 124 | * Execution context |
| 125 | * |
| 126 | * One tl_ctx per actor, passed as the first argument of every generated |
| 127 | * function. It is never stored in thread-local storage: an actor's code |
| 128 | * must not observe which OS thread runs it. Fields after `rt` are private. |
| 129 | * ---------------------------------------------------------------------- */ |
| 130 | typedef enum tl_yield_kind { |
| 131 | TL_YIELD_NONE = 0, |
| 132 | TL_YIELD_FINAL = 1, /* `final ctl` op unwinding to handler `yield_marker` */ |
| 133 | TL_YIELD_PANIC = 2, /* panic unwinding to the actor root */ |
| 134 | TL_YIELD_KILL = 3, /* exit signal unwinding to the actor root */ |
| 135 | TL_YIELD_CANCEL = 4, /* a never-resumed `ctl` handler body is cancelled */ |
| 136 | /* Not unwinding: passes over a suspended `multi ctl` continuation |
| 137 | * (ADR 0017). CLONE takes one more reference for each one the frames |
| 138 | * hold (their unwinding code dups instead of dropping); DISCARD |
| 139 | * releases them without running `defer` cleanups. */ |
| 140 | TL_YIELD_CLONE = 5, |
| 141 | TL_YIELD_DISCARD = 6 |
| 142 | } tl_yield_kind; |
| 143 | |
| 144 | /* Generated functions share one signature (ctx + 6 values) so that every |
| 145 | * tail call can be `musttail`; further arguments go through ctx->xargs, |
| 146 | * which the callee reads on entry. Closure code receives the closure as |
| 147 | * the first value. 123 slots: functions take up to 128 parameters, and a |
| 148 | * call through a closure one more value (ABI 12, issue #434). */ |
| 149 | #define TL_XARGS 123 |
| 150 | |
| 151 | typedef struct tl_ctx { |
| 152 | int32_t yielding; /* tl_yield_kind; checked after calls that may yield */ |
| 153 | int32_t reductions; /* decremented on function entry; <= 0 -> tl_preempt */ |
| 154 | uint64_t yield_marker; /* TL_YIELD_FINAL: marker of the target handler */ |
| 155 | tl_value yield_value; /* owned payload: op argument or panic/exit reason */ |
| 156 | void *evv; /* unused (null): handlers are values (ADR 0010) */ |
| 157 | void *rt; /* runtime-private */ |
| 158 | tl_value *xargs; /* TL_XARGS call arguments after the sixth: the * |
| 159 | * running thread's slots (ABI 3) */ |
| 160 | } tl_ctx; |
| 161 | |
| 162 | _Static_assert(offsetof(tl_ctx, yielding) == 0, "tl_ctx layout"); |
| 163 | _Static_assert(offsetof(tl_ctx, reductions) == 4, "tl_ctx layout"); |
| 164 | _Static_assert(offsetof(tl_ctx, yield_marker) == 8, "tl_ctx layout"); |
| 165 | _Static_assert(offsetof(tl_ctx, yield_value) == 16, "tl_ctx layout"); |
| 166 | _Static_assert(offsetof(tl_ctx, xargs) == 40, "tl_ctx layout"); |
| 167 | _Static_assert(sizeof(tl_ctx) == 48, "tl_ctx layout"); |
| 168 | |
| 169 | typedef tl_value (*tl_fn)(tl_ctx *ctx, tl_value a0, tl_value a1, tl_value a2, |
| 170 | tl_value a3, tl_value a4, tl_value a5); |
| 171 | |
| 172 | /* Closure entry points, stored type-erased. Callers cast one to the exact |
| 173 | * arity, e.g. tl_value (*)(tl_ctx *, tl_value self, tl_value a), before |
| 174 | * calling (never through a variadic type). A closure block carries TWO |
| 175 | * entries (ABI 10, ADR 0068): the owned one — all arguments, including |
| 176 | * `self`, owned by the callee — and the borrowed one, which neither drops |
| 177 | * nor consumes `self` or the arguments (the caller keeps its references; |
| 178 | * the result is owned as usual). */ |
| 179 | typedef void (*tl_closure_fn)(void); |
| 180 | |
| 181 | /* ------------------------------------------------------------------------ |
| 182 | * Runtime entry points (implemented in Rust) |
| 183 | * ---------------------------------------------------------------------- */ |
| 184 | |
| 185 | /* Memory */ |
| 186 | tl_block *tl_alloc(tl_ctx *ctx, uint16_t tag, uint8_t scan_fsize, size_t raw_bytes); |
| 187 | void tl_drop_slow(tl_ctx *ctx, tl_block *b); /* rc <= 1, shared, or stuck */ |
| 188 | void tl_free(tl_ctx *ctx, tl_block *b); /* unique b; children already moved out */ |
| 189 | void tl_dup_shared(tl_block *b); |
| 190 | int64_t tl_stats_live_blocks(void); /* -1 when stats disabled */ |
| 191 | int64_t tl_stats_allocations(void); /* -1 when stats disabled */ |
| 192 | |
| 193 | /* Strings */ |
| 194 | tl_value tl_string_from(tl_ctx *ctx, const char *bytes, size_t len); |
| 195 | size_t tl_string_len(tl_value s); /* borrows s */ |
| 196 | const char *tl_string_ptr(tl_value s); /* borrows s; NUL-terminated */ |
| 197 | tl_value tl_println(tl_ctx *ctx, tl_value s); /* borrows s; may park (ADR 0022) */ |
| 198 | |
| 199 | /* Closures */ |
| 200 | tl_value tl_closure_alloc(tl_ctx *ctx, tl_closure_fn code, tl_closure_fn borrowed, uint8_t env_count); |
| 201 | /* Borrowed entries of runtime-made closures (ADR 0068): dup `this` and |
| 202 | * the arguments for the owned entry, which consumes the duplicates. The |
| 203 | * suffix is the entry's arity (arguments after `this`). */ |
| 204 | tl_value tl_borrowed0(tl_ctx *ctx, tl_value this_, tl_value a, tl_value b, tl_value c, tl_value d, tl_value e); |
| 205 | tl_value tl_borrowed1(tl_ctx *ctx, tl_value this_, tl_value a, tl_value b, tl_value c, tl_value d, tl_value e); |
| 206 | tl_value tl_borrowed2(tl_ctx *ctx, tl_value this_, tl_value a, tl_value b, tl_value c, tl_value d, tl_value e); |
| 207 | |
| 208 | /* Effects and unwinding */ |
| 209 | uint64_t tl_fresh_marker(tl_ctx *ctx); |
| 210 | tl_value tl_yield_final(tl_ctx *ctx, uint64_t marker, tl_value payload); /* consumes payload */ |
| 211 | bool tl_catch_final(tl_ctx *ctx, uint64_t marker, tl_value *payload_out); |
| 212 | /* `defer` (ADR 0016): after a body returned `result` (normally or while |
| 213 | * unwinding), run `cleanup` with the unwinding set aside. Consumes both; |
| 214 | * returns `result`, or unit if unwinding continues. */ |
| 215 | tl_value tl_defer(tl_ctx *ctx, tl_value result, tl_value cleanup); |
| 216 | tl_value tl_panic_str(tl_ctx *ctx, const char *reason); |
| 217 | |
| 218 | /* Generated main calls this first: the runtime stops if it was built for |
| 219 | * another TL_ABI_VERSION. */ |
| 220 | void tl_abi_check(int64_t version); |
| 221 | |
| 222 | /* Int slow paths (runtime/louise_rt/src/int.rs): an overflow, or a big |
| 223 | * integer argument. They borrow their arguments; results are canonical. */ |
| 224 | tl_value tl_int_add_slow(tl_ctx *ctx, tl_value a, tl_value b); |
| 225 | tl_value tl_int_sub_slow(tl_ctx *ctx, tl_value a, tl_value b); |
| 226 | tl_value tl_int_mul_slow(tl_ctx *ctx, tl_value a, tl_value b); |
| 227 | tl_value tl_int_div_slow(tl_ctx *ctx, tl_value a, tl_value b); /* panics on 0 */ |
| 228 | tl_value tl_int_rem_slow(tl_ctx *ctx, tl_value a, tl_value b); /* panics on 0 */ |
| 229 | tl_value tl_int_eq_slow(tl_ctx *ctx, tl_value a, tl_value b); |
| 230 | tl_value tl_int_compare_slow(tl_ctx *ctx, tl_value a, tl_value b); |
| 231 | /* Bitwise slow paths (std/int, ADR 0035): a big argument, a result |
| 232 | * outside 63 bits, or a shift that panics. */ |
| 233 | tl_value tl_int_and_slow(tl_ctx *ctx, tl_value a, tl_value b); |
| 234 | tl_value tl_int_or_slow(tl_ctx *ctx, tl_value a, tl_value b); |
| 235 | tl_value tl_int_xor_slow(tl_ctx *ctx, tl_value a, tl_value b); |
| 236 | tl_value tl_int_not_slow(tl_ctx *ctx, tl_value a); |
| 237 | tl_value tl_int_shift_left_slow(tl_ctx *ctx, tl_value n, tl_value k); /* panics on k < 0 */ |
| 238 | tl_value tl_int_shift_right_slow(tl_ctx *ctx, tl_value n, tl_value k); /* panics on k < 0 */ |
| 239 | tl_value tl_int_bit_length(tl_ctx *ctx, tl_value n); |
| 240 | /* For C bindings: an Int as int64_t (false if it does not fit), and an |
| 241 | * int64_t as an Int (big outside 63 bits). */ |
| 242 | bool tl_int_to_i64(tl_value v, int64_t *out); |
| 243 | tl_value tl_int_from_i64(tl_ctx *ctx, int64_t n); |
| 244 | |
| 245 | /* Called by compiled programs (runtime/louise_rt/src/prims.rs). Like every |
| 246 | * `@extern` function they borrow their arguments; results are owned. */ |
| 247 | void tl_report_leaks(void); /* with LOUISE_LEAKS or LOUISE_STATS set */ |
| 248 | tl_value tl_panic(tl_ctx *ctx, tl_value message); |
| 249 | tl_value tl_float_from(tl_ctx *ctx, double x); |
| 250 | double tl_float_get(tl_value v); |
| 251 | tl_value tl_float_add(tl_ctx *ctx, tl_value a, tl_value b); |
| 252 | tl_value tl_float_sub(tl_ctx *ctx, tl_value a, tl_value b); |
| 253 | tl_value tl_float_mul(tl_ctx *ctx, tl_value a, tl_value b); |
| 254 | tl_value tl_float_div(tl_ctx *ctx, tl_value a, tl_value b); |
| 255 | tl_value tl_float_rem(tl_ctx *ctx, tl_value a, tl_value b); |
| 256 | tl_value tl_float_eq(tl_ctx *ctx, tl_value a, tl_value b); |
| 257 | tl_value tl_float_compare(tl_ctx *ctx, tl_value a, tl_value b); |
| 258 | tl_value tl_float_show(tl_ctx *ctx, tl_value a); |
| 259 | tl_value tl_hash_float(tl_ctx *ctx, tl_value a); |
| 260 | /* A Float's IEEE 754 bits and back (std/float, ADR 0035). */ |
| 261 | tl_value tl_float_to_bits(tl_ctx *ctx, tl_value a); |
| 262 | tl_value tl_float_from_bits(tl_ctx *ctx, tl_value n); |
| 263 | tl_value tl_float_to_bits32(tl_ctx *ctx, tl_value a); |
| 264 | tl_value tl_float_from_bits32(tl_ctx *ctx, tl_value n); |
| 265 | tl_value tl_float_from_int(tl_ctx *ctx, tl_value n); |
| 266 | /* U64 (std/u64, ADR 0035): unsigned 64-bit integers that wrap, boxed. */ |
| 267 | tl_value tl_u64_from_int(tl_ctx *ctx, tl_value n); |
| 268 | tl_value tl_u64_to_int(tl_ctx *ctx, tl_value x); |
| 269 | tl_value tl_u64_add(tl_ctx *ctx, tl_value a, tl_value b); |
| 270 | tl_value tl_u64_sub(tl_ctx *ctx, tl_value a, tl_value b); |
| 271 | tl_value tl_u64_mul(tl_ctx *ctx, tl_value a, tl_value b); |
| 272 | tl_value tl_u64_div(tl_ctx *ctx, tl_value a, tl_value b); /* panics on 0 */ |
| 273 | tl_value tl_u64_rem(tl_ctx *ctx, tl_value a, tl_value b); /* panics on 0 */ |
| 274 | tl_value tl_u64_and(tl_ctx *ctx, tl_value a, tl_value b); |
| 275 | tl_value tl_u64_or(tl_ctx *ctx, tl_value a, tl_value b); |
| 276 | tl_value tl_u64_xor(tl_ctx *ctx, tl_value a, tl_value b); |
| 277 | tl_value tl_u64_not(tl_ctx *ctx, tl_value a); |
| 278 | tl_value tl_u64_shift_left(tl_ctx *ctx, tl_value a, tl_value k); /* panics on k < 0 */ |
| 279 | tl_value tl_u64_shift_right(tl_ctx *ctx, tl_value a, tl_value k); /* panics on k < 0 */ |
| 280 | tl_value tl_u64_rotate_left(tl_ctx *ctx, tl_value a, tl_value k); |
| 281 | tl_value tl_u64_rotate_right(tl_ctx *ctx, tl_value a, tl_value k); |
| 282 | tl_value tl_u64_eq(tl_ctx *ctx, tl_value a, tl_value b); |
| 283 | tl_value tl_u64_compare(tl_ctx *ctx, tl_value a, tl_value b); |
| 284 | tl_value tl_u64_show(tl_ctx *ctx, tl_value a); |
| 285 | tl_value tl_hash_u64(tl_ctx *ctx, tl_value a); |
| 286 | /* Dec (std/dec, ADR 0070): a 128-bit fixed-point decimal, an int128 |
| 287 | * counting 10^-18 units, boxed. Inexact results round half-even; |
| 288 | * arithmetic panics on overflow. */ |
| 289 | tl_value tl_dec_from_int(tl_ctx *ctx, tl_value n); /* panics on overflow */ |
| 290 | tl_value tl_dec_to_int(tl_ctx *ctx, tl_value x); |
| 291 | tl_value tl_dec_from_float(tl_ctx *ctx, tl_value x); /* (Bool, Dec) */ |
| 292 | tl_value tl_dec_to_float(tl_ctx *ctx, tl_value x); |
| 293 | tl_value tl_dec_parse(tl_ctx *ctx, tl_value s); /* (Bool, Dec) */ |
| 294 | tl_value tl_dec_add(tl_ctx *ctx, tl_value a, tl_value b); /* panics on overflow */ |
| 295 | tl_value tl_dec_sub(tl_ctx *ctx, tl_value a, tl_value b); /* panics on overflow */ |
| 296 | tl_value tl_dec_mul(tl_ctx *ctx, tl_value a, tl_value b); /* panics on overflow */ |
| 297 | tl_value tl_dec_div(tl_ctx *ctx, tl_value a, tl_value b); /* panics on 0, overflow */ |
| 298 | tl_value tl_dec_rem(tl_ctx *ctx, tl_value a, tl_value b); /* panics on 0 */ |
| 299 | tl_value tl_dec_eq(tl_ctx *ctx, tl_value a, tl_value b); |
| 300 | tl_value tl_dec_compare(tl_ctx *ctx, tl_value a, tl_value b); |
| 301 | tl_value tl_dec_show(tl_ctx *ctx, tl_value a); |
| 302 | tl_value tl_dec_scaled(tl_ctx *ctx, tl_value a); |
| 303 | tl_value tl_dec_from_scaled(tl_ctx *ctx, tl_value n); /* (Bool, Dec) */ |
| 304 | tl_value tl_dec_round(tl_ctx *ctx, tl_value a, tl_value places); /* panics: places outside 0..=18 */ |
| 305 | tl_value tl_hash_dec(tl_ctx *ctx, tl_value a); |
| 306 | tl_value tl_int_show(tl_ctx *ctx, tl_value a); |
| 307 | tl_value tl_hash_int(tl_ctx *ctx, tl_value a); |
| 308 | tl_value tl_hash_combine(tl_ctx *ctx, tl_value a, tl_value b); |
| 309 | tl_value tl_string_eq(tl_ctx *ctx, tl_value a, tl_value b); |
| 310 | tl_value tl_string_eq_ignore_ascii_case(tl_ctx *ctx, tl_value a, tl_value b); |
| 311 | tl_value tl_string_compare(tl_ctx *ctx, tl_value a, tl_value b); |
| 312 | tl_value tl_string_concat(tl_ctx *ctx, tl_value a, tl_value b); |
| 313 | tl_value tl_string_length(tl_ctx *ctx, tl_value a); |
| 314 | tl_value tl_string_starts_with(tl_ctx *ctx, tl_value a, tl_value b); |
| 315 | tl_value tl_string_ends_with(tl_ctx *ctx, tl_value a, tl_value b); |
| 316 | tl_value tl_string_contains(tl_ctx *ctx, tl_value a, tl_value b); |
| 317 | /* Some(byte index of b's first occurrence in a), or None. */ |
| 318 | tl_value tl_string_find(tl_ctx *ctx, tl_value a, tl_value b); |
| 319 | tl_value tl_string_byte_length(tl_ctx *ctx, tl_value a); |
| 320 | tl_value tl_string_to_lower(tl_ctx *ctx, tl_value a); |
| 321 | tl_value tl_string_trim(tl_ctx *ctx, tl_value a); |
| 322 | tl_value tl_string_split(tl_ctx *ctx, tl_value a, tl_value sep); |
| 323 | tl_value tl_string_chars(tl_ctx *ctx, tl_value a); |
| 324 | tl_value tl_string_slice(tl_ctx *ctx, tl_value s, tl_value start, tl_value end); |
| 325 | tl_value tl_string_parse_int(tl_ctx *ctx, tl_value a); |
| 326 | tl_value tl_float_parse(tl_ctx *ctx, tl_value a); /* (Bool, Float) */ |
| 327 | tl_value tl_string_join(tl_ctx *ctx, tl_value parts, tl_value sep); |
| 328 | /* `Bytes` and their builders (runtime/louise_rt/src/bytes.rs, builder.rs; ADR |
| 329 | * 0012). The `append` functions and `build` consume their arguments (they |
| 330 | * are `owned` externs): a unique builder is updated in place. */ |
| 331 | tl_value tl_bytes_length(tl_ctx *ctx, tl_value b); |
| 332 | tl_value tl_bytes_at(tl_ctx *ctx, tl_value b, tl_value i); |
| 333 | /* Big-endian reads in place (cursor decoders): -1 when out of range. */ |
| 334 | tl_value tl_bytes_read_u16be(tl_ctx *ctx, tl_value b, tl_value off); |
| 335 | tl_value tl_bytes_read_u32be(tl_ctx *ctx, tl_value b, tl_value off); |
| 336 | tl_value tl_bytes_read_u64be(tl_ctx *ctx, tl_value b, tl_value off); |
| 337 | tl_value tl_bytes_slice(tl_ctx *ctx, tl_value b, tl_value start, tl_value end); |
| 338 | tl_value tl_bytes_concat(tl_ctx *ctx, tl_value a, tl_value b); |
| 339 | tl_value tl_bytes_concat_all(tl_ctx *ctx, tl_value parts); |
| 340 | tl_value tl_bytes_eq(tl_ctx *ctx, tl_value a, tl_value b); |
| 341 | tl_value tl_bytes_compare(tl_ctx *ctx, tl_value a, tl_value b); |
| 342 | tl_value tl_hash_bytes(tl_ctx *ctx, tl_value b); |
| 343 | tl_value tl_bytes_debug(tl_ctx *ctx, tl_value b); |
| 344 | tl_value tl_bytes_from_string(tl_ctx *ctx, tl_value s); |
| 345 | tl_value tl_bytes_to_string(tl_ctx *ctx, tl_value b); |
| 346 | tl_value tl_bytes_to_string_range(tl_ctx *ctx, tl_value b, tl_value off, tl_value len); |
| 347 | tl_value tl_bytes_to_string_lossy(tl_ctx *ctx, tl_value b); |
| 348 | tl_value tl_bytes_from_list(tl_ctx *ctx, tl_value items); /* panics on a non-byte */ |
| 349 | tl_value tl_bytes_to_list(tl_ctx *ctx, tl_value b); |
| 350 | tl_value tl_bytes_builder(tl_ctx *ctx, tl_value capacity); |
| 351 | tl_value tl_bytes_append(tl_ctx *ctx, tl_value b, tl_value data); |
| 352 | tl_value tl_bytes_append_string(tl_ctx *ctx, tl_value b, tl_value s); |
| 353 | /* append_escaped appends `s` HTML-escaped, straight into the buffer. */ |
| 354 | tl_value tl_bytes_append_escaped(tl_ctx *ctx, tl_value b, tl_value s); |
| 355 | tl_value tl_bytes_append_byte(tl_ctx *ctx, tl_value b, tl_value byte); /* panics on a non-byte */ |
| 356 | tl_value tl_bytes_build(tl_ctx *ctx, tl_value b); |
| 357 | tl_value tl_bytes_build_string(tl_ctx *ctx, tl_value b); /* panics on invalid UTF-8 */ |
| 358 | /* Arrays (runtime/louise_rt/src/array.rs; ADR 0014). `set` and `push` consume |
| 359 | * their arguments (`owned`): a unique array is updated in place. */ |
| 360 | tl_value tl_array_with_capacity(tl_ctx *ctx, tl_value n); |
| 361 | tl_value tl_array_from_list(tl_ctx *ctx, tl_value list); |
| 362 | tl_value tl_array_to_list(tl_ctx *ctx, tl_value a); |
| 363 | /* `length`, `get` and `at` are inline, below; `at`'s slow path panics. */ |
| 364 | tl_value tl_array_at_slow(tl_ctx *ctx, tl_value a, tl_value i); |
| 365 | tl_value tl_array_set(tl_ctx *ctx, tl_value a, tl_value i, tl_value x); /* panics out of range */ |
| 366 | tl_value tl_array_push(tl_ctx *ctx, tl_value a, tl_value x); |
| 367 | tl_value tl_array_slice(tl_ctx *ctx, tl_value a, tl_value start, tl_value end); |
| 368 | /* Sockets (runtime/louise_rt/src/net.rs; may park the actor) and HTTP framing |
| 369 | * (http.rs). */ |
| 370 | tl_value tl_net_resolve(tl_ctx *ctx, tl_value host); /* blocking */ |
| 371 | tl_value tl_net_listen(tl_ctx *ctx, tl_value host, tl_value port); |
| 372 | tl_value tl_net_port(tl_ctx *ctx, tl_value listener); |
| 373 | tl_value tl_net_accept(tl_ctx *ctx, tl_value listener); |
| 374 | tl_value tl_net_connect(tl_ctx *ctx, tl_value host, tl_value port); |
| 375 | tl_value tl_net_read(tl_ctx *ctx, tl_value conn, tl_value max); |
| 376 | tl_value tl_net_write(tl_ctx *ctx, tl_value conn, tl_value data); /* Bytes */ |
| 377 | tl_value tl_net_write_string(tl_ctx *ctx, tl_value conn, tl_value data); /* String */ |
| 378 | tl_value tl_net_peer(tl_ctx *ctx, tl_value conn); /* -> Result<(String, Int), String> */ |
| 379 | tl_value tl_net_close(tl_ctx *ctx, tl_value conn); |
| 380 | tl_value tl_net_close_listener(tl_ctx *ctx, tl_value listener); |
| 381 | /* TLS clients and servers (runtime/louise_rt/src/tls.rs, ADR 0038). Errors |
| 382 | * are `Error((code, message))`. The configurations are `blocking` (they |
| 383 | * read files); connect, accept, read and write may park the actor. */ |
| 384 | tl_value tl_tls_config(tl_ctx *ctx, tl_value default_roots, tl_value ca_pems, tl_value ca_files, |
| 385 | tl_value alpn, tl_value tls13_only, tl_value insecure, tl_value cert_file, |
| 386 | tl_value key_file); |
| 387 | tl_value tl_tls_server_config(tl_ctx *ctx, tl_value cert_file, tl_value key_file, |
| 388 | tl_value client_ca_pems, tl_value client_ca_files, tl_value alpn, |
| 389 | tl_value tls13_only); |
| 390 | tl_value tl_tls_connect(tl_ctx *ctx, tl_value conn, tl_value host, tl_value config); |
| 391 | tl_value tl_tls_accept(tl_ctx *ctx, tl_value conn, tl_value config); |
| 392 | tl_value tl_tls_read(tl_ctx *ctx, tl_value tls, tl_value max); |
| 393 | tl_value tl_tls_write(tl_ctx *ctx, tl_value tls, tl_value data); /* Bytes */ |
| 394 | tl_value tl_tls_write_string(tl_ctx *ctx, tl_value tls, tl_value data); /* String */ |
| 395 | tl_value tl_tls_close(tl_ctx *ctx, tl_value tls); |
| 396 | tl_value tl_tls_alpn(tl_ctx *ctx, tl_value tls); /* (Bool, String) */ |
| 397 | tl_value tl_tls_version(tl_ctx *ctx, tl_value tls); /* "TLS 1.3" ... */ |
| 398 | tl_value tl_tls_peer_names(tl_ctx *ctx, tl_value tls); /* List<String> */ |
| 399 | tl_value tl_http_parse_request(tl_ctx *ctx, tl_value buffer, tl_value at); |
| 400 | tl_value tl_http_parse_chunk(tl_ctx *ctx, tl_value buffer, tl_value at); |
| 401 | /* Whole-file kernels (runtime/louise_rt/src/fs.rs), retained with their |
| 402 | * original borrowed fixed arities. std/file.fs's root adapters call these via |
| 403 | * fixed-arity thunks on TL_BLOCKING_IO; no root argument is inserted here. |
| 404 | * Errors are `Error(message)` with the OS's message. Direct native rebinding |
| 405 | * remains an ffi escape, not a filesystem sandbox. */ |
| 406 | tl_value tl_file_read(tl_ctx *ctx, tl_value path); |
| 407 | tl_value tl_file_write(tl_ctx *ctx, tl_value path, tl_value content); /* Bytes */ |
| 408 | tl_value tl_file_exists(tl_ctx *ctx, tl_value path); |
| 409 | tl_value tl_file_size(tl_ctx *ctx, tl_value path); |
| 410 | tl_value tl_file_list_dir(tl_ctx *ctx, tl_value path); |
| 411 | /* Cryptographic hashes (runtime/louise_rt/src/crypto.rs, over ring). |
| 412 | * tl_crypto_pbkdf2_sha256 is `blocking` (the iteration count is the |
| 413 | * caller's). */ |
| 414 | tl_value tl_crypto_sha256(tl_ctx *ctx, tl_value data); |
| 415 | tl_value tl_crypto_hmac_sha256(tl_ctx *ctx, tl_value key, tl_value message); |
| 416 | tl_value tl_crypto_pbkdf2_sha256(tl_ctx *ctx, tl_value password, tl_value salt, tl_value iterations, |
| 417 | tl_value length); |
| 418 | /* Ed25519 (RFC 8032, ADR 0067): verify answers a Bool (False for wrong |
| 419 | * lengths); public_key and sign panic on a seed that is not 32 bytes. */ |
| 420 | tl_value tl_crypto_ed25519_verify(tl_ctx *ctx, tl_value public_key, tl_value message, |
| 421 | tl_value signature); |
| 422 | tl_value tl_crypto_ed25519_public_key(tl_ctx *ctx, tl_value seed); |
| 423 | tl_value tl_crypto_ed25519_sign(tl_ctx *ctx, tl_value seed, tl_value message); |
| 424 | /* ChaCha20-Poly1305 (RFC 8439) and HKDF-SHA-256 (RFC 5869), ADR 0071: |
| 425 | * seal answers the ciphertext then the tag; open answers |
| 426 | * Result<Bytes, String>; both panic on a key that is not 32 bytes or a |
| 427 | * nonce that is not 12. hkdf panics on a length outside 0..=8160. */ |
| 428 | tl_value tl_crypto_chacha20_poly1305_seal(tl_ctx *ctx, tl_value key, tl_value nonce, |
| 429 | tl_value plaintext, tl_value aad); |
| 430 | tl_value tl_crypto_chacha20_poly1305_open(tl_ctx *ctx, tl_value key, tl_value nonce, |
| 431 | tl_value sealed, tl_value aad); |
| 432 | tl_value tl_crypto_hkdf_sha256(tl_ctx *ctx, tl_value secret, tl_value salt, tl_value info, |
| 433 | tl_value length); |
| 434 | /* std/table (runtime/louise_rt/src/table.rs, ADR 0073): tables any actor |
| 435 | * reads without a message. get answers Option (retained); put consumes its |
| 436 | * arguments and panics on a value that cannot be shared. */ |
| 437 | tl_value tl_table_new(tl_ctx *ctx); |
| 438 | tl_value tl_table_get(tl_ctx *ctx, tl_value table, tl_value key); |
| 439 | tl_value tl_table_put(tl_ctx *ctx, tl_value table, tl_value key, tl_value value); |
| 440 | tl_value tl_table_remove(tl_ctx *ctx, tl_value table, tl_value key); |
| 441 | /* base64 (RFC 4648), for SCRAM (runtime/louise_rt/src/prims.rs). */ |
| 442 | tl_value tl_base64_encode(tl_ctx *ctx, tl_value a); /* Bytes -> String */ |
| 443 | tl_value tl_base64_decode(tl_ctx *ctx, tl_value a); /* String -> Result<Bytes, String> */ |
| 444 | tl_value tl_http_parse_head_end(tl_ctx *ctx, tl_value buffer, tl_value from); |
| 445 | tl_value tl_http_parse_query(tl_ctx *ctx, tl_value s); |
| 446 | tl_value tl_url_decode(tl_ctx *ctx, tl_value s); /* consumes `s` (owned) */ |
| 447 | tl_value tl_url_encode(tl_ctx *ctx, tl_value s); /* consumes `s` (owned) */ |
| 448 | tl_value tl_os_env(tl_ctx *ctx, tl_value name); |
| 449 | tl_value tl_os_args(tl_ctx *ctx); |
| 450 | /* Send the signals of `which` (a List<Signal>) to the running actor's subject `tag`. */ |
| 451 | tl_value tl_os_signals(tl_ctx *ctx, tl_value tag, tl_value which); |
| 452 | /* Exit the process with `code`, after flushing the console; never returns. */ |
| 453 | tl_value tl_os_exit(tl_ctx *ctx, tl_value code); |
| 454 | /* The blocking pools' ((size, running, queued), ...): cpu, then io. */ |
| 455 | tl_value tl_runtime_blocking_stats(tl_ctx *ctx); |
| 456 | /* Compiler-generated @remote thunk entry (ADR 0084): deny fs permanently to |
| 457 | * the current actor and all of its descendants. This is a restriction, not a |
| 458 | * capability grant; raw ffi still is not a native sandbox. */ |
| 459 | tl_value tl_dist_remote_enter(tl_ctx *ctx); |
| 460 | /* std/dist (ADR 0072, runtime/louise_rt/src/node.rs): start this node |
| 461 | (Result<port, why>; `advertise` an address or ""; `limits` is |
| 462 | (heartbeat_ms, node_down_after_ms, max_message_bytes, |
| 463 | outbound_queue_bytes, max_peers, discovery: 0 seeds, 1 mesh)); register |
| 464 | a name (a code); ask a node for one, or to spawn a `@remote` function, |
| 465 | linked, monitored (its Down on subject `down`) or neither, as `tie` |
| 466 | says (the answer arrives on the running actor's subject `tag`); give |
| 467 | up waiting for a monitored spawn (a Bool: false if answered |
| 468 | meanwhile); follow a node's connection (its NodeUp / NodeDown events |
| 469 | arrive on subject `tag`); the spawner's spawn of the body the |
| 470 | dispatcher gave, and its answer; the known nodes (a List<Peer>); stay |
| 471 | connected to a node, as to a seed (a code). */ |
| 472 | tl_value tl_dist_start(tl_ctx *ctx, tl_value node, tl_value cluster, tl_value listen, |
| 473 | tl_value advertise, tl_value seeds, tl_value cert_file, |
| 474 | tl_value key_file, tl_value ca_files, tl_value table, |
| 475 | tl_value schema_hash, tl_value limits, tl_value entries, |
| 476 | tl_value spawner); |
| 477 | tl_value tl_dist_register(tl_ctx *ctx, tl_value name, tl_value desc, tl_value subject); |
| 478 | tl_value tl_dist_whereis(tl_ctx *ctx, tl_value tag, tl_value node, tl_value name, |
| 479 | tl_value desc); |
| 480 | tl_value tl_dist_monitor_node(tl_ctx *ctx, tl_value tag, tl_value node); |
| 481 | tl_value tl_dist_spawn(tl_ctx *ctx, tl_value tag, tl_value node, tl_value key, |
| 482 | tl_value desc, tl_value args, tl_value tie, tl_value down); |
| 483 | tl_value tl_dist_spawn_abandon(tl_ctx *ctx, tl_value tag); |
| 484 | tl_value tl_dist_spawn_for(tl_ctx *ctx, tl_value asker, tl_value tag, tl_value tie, |
| 485 | tl_value down, tl_value result); /* consumes its arguments (owned) */ |
| 486 | tl_value tl_dist_peers(tl_ctx *ctx); |
| 487 | tl_value tl_dist_connect(tl_ctx *ctx, tl_value node, tl_value addr); |
| 488 | tl_value tl_string_debug(tl_ctx *ctx, tl_value a); |
| 489 | tl_value tl_hash_string(tl_ctx *ctx, tl_value a); |
| 490 | tl_value tl_html_escape(tl_ctx *ctx, tl_value a); /* consumes `a` (owned) */ |
| 491 | tl_value tl_print(tl_ctx *ctx, tl_value a); |
| 492 | tl_value tl_eprintln(tl_ctx *ctx, tl_value a); |
| 493 | /* Regions (ADR 0002, Revision 1; runtime/louise_rt/src/region.rs). */ |
| 494 | tl_value tl_region_new(tl_ctx *ctx); |
| 495 | tl_value tl_region_end(tl_ctx *ctx, tl_value region); |
| 496 | /* Leave `region` (consumed): end it, unless copies of a `multi ctl` |
| 497 | * continuation inside it have yet to leave (ADR 0017). */ |
| 498 | void tl_region_exit(tl_ctx *ctx, tl_value region); |
| 499 | tl_value tl_ref_new(tl_ctx *ctx, tl_value region, tl_value value); /* consumes value */ |
| 500 | tl_value tl_ref_get(tl_ctx *ctx, tl_value ref); |
| 501 | tl_value tl_ref_set(tl_ctx *ctx, tl_value ref, tl_value value); /* consumes value */ |
| 502 | |
| 503 | /* Handlers with `ctl` operations (runtime/louise_rt/src/handlers.rs): the body |
| 504 | * runs on its own fiber; `cap` is the block of clauses then the marker. */ |
| 505 | tl_value tl_handle_ctl(tl_ctx *ctx, tl_value body, tl_value cap, tl_value ret, uint64_t marker); |
| 506 | tl_value tl_perform_ctl(tl_ctx *ctx, tl_value cap, tl_value payload); /* consumes payload */ |
| 507 | /* The same for an effect with `multi ctl` operations (ADR 0017): bit i of |
| 508 | * `multi_ops` is set if operation i is one; its `resume` may be called |
| 509 | * several times. They are performed with tl_perform_ctl. */ |
| 510 | tl_value tl_handle_multi(tl_ctx *ctx, tl_value body, tl_value cap, tl_value ret, uint64_t marker, |
| 511 | uint64_t multi_ops); |
| 512 | |
| 513 | /* Root capabilities (ADRs 0036 and 0084): the runtime handles |
| 514 | * `std/process.proc` (process.rs), `std/time.clock`, `std/random.random` |
| 515 | * (clock.rs) and `std/file.fs` (fs.rs) around `main`, each test and every |
| 516 | * local actor, with a capability whose clauses are runtime closures. |
| 517 | * `kind`: one of TL_ROOT_*; `ops`: the number of operations the program was |
| 518 | * compiled for; the runtime stops if it was built for another number. Every |
| 519 | * capability is immortal (rc 0) and shared by all actors. TL_ROOT_FS is |
| 520 | * appended, and tl_root_cap/tl_main_program are unchanged, so the ABI stays |
| 521 | * version 12. */ |
| 522 | #define TL_ROOT_PROC 0 |
| 523 | #define TL_ROOT_CLOCK 1 |
| 524 | #define TL_ROOT_RANDOM 2 |
| 525 | #define TL_ROOT_FS 3 |
| 526 | tl_value tl_root_cap(tl_ctx *ctx, int64_t kind, int64_t ops); |
| 527 | tl_value tl_pid_eq(tl_ctx *ctx, tl_value a, tl_value b); |
| 528 | tl_value tl_coerce(tl_ctx *ctx, tl_value v); |
| 529 | |
| 530 | /* Native dependencies (ADR 0005; runtime/louise_rt/src/resource.rs, blocking.rs). |
| 531 | * A C function bound with `@extern(c, "sym")` has this file's convention: |
| 532 | * tl_value sym(tl_ctx *ctx, tl_value a, ...); |
| 533 | * Arguments are borrowed (read them, don't drop them); the result is owned, |
| 534 | * built with the functions below. */ |
| 535 | typedef void (*tl_finalizer)(void *ptr); |
| 536 | tl_value tl_resource_new(tl_ctx *ctx, void *ptr, tl_finalizer finalize, bool thread_safe); |
| 537 | void *tl_resource_get(tl_value resource); |
| 538 | /* `blocking` externs: generated code calls them through a thunk on their |
| 539 | * blocking pool (`TL_BLOCKING_*`, ADR 0005 Revision 2) while the actor is |
| 540 | * parked. */ |
| 541 | typedef tl_value (*tl_thunk)(tl_ctx *ctx, const tl_value *args); |
| 542 | #define TL_BLOCKING_CPU 0 |
| 543 | #define TL_BLOCKING_IO 1 |
| 544 | tl_value tl_call_blocking(tl_ctx *ctx, int pool, tl_thunk thunk, size_t argc, const tl_value *args); |
| 545 | |
| 546 | /* Callbacks from C (ADR 0015; runtime/louise_rt/src/callback.rs). An extern |
| 547 | * taking a Louise function `f` passes C a trampoline and a tl_callback as |
| 548 | * its `void *` environment, for the duration of the call only: |
| 549 | * |
| 550 | * tl_callback cb; |
| 551 | * tl_callback_begin(ctx, &cb, f); |
| 552 | * lib_sort(xs, n, trampoline, &cb); // calls trampoline(..., &cb) |
| 553 | * return tl_callback_end(&cb, result); |
| 554 | * |
| 555 | * The trampoline converts its C arguments to values, calls |
| 556 | * tl_callback_call, and converts (then drops) the result. The closure |
| 557 | * runs in the calling actor, on its thread. If it panicked, |
| 558 | * tl_callback_failed is true: later calls return TL_UNIT without running |
| 559 | * it, and the panic continues once the extern returns. Not for |
| 560 | * `blocking` externs. */ |
| 561 | typedef struct tl_callback { |
| 562 | tl_ctx *ctx; /* the actor that called into C */ |
| 563 | tl_value fn; /* the closure, borrowed from the extern's argument */ |
| 564 | uintptr_t thread; /* runtime-private */ |
| 565 | } tl_callback; |
| 566 | _Static_assert(sizeof(tl_callback) == 24, "tl_callback layout"); |
| 567 | |
| 568 | void tl_callback_begin(tl_ctx *ctx, tl_callback *cb, tl_value fn); |
| 569 | /* Consumes args; returns the closure's result (owned), or TL_UNIT. */ |
| 570 | tl_value tl_callback_call(const tl_callback *cb, size_t argc, const tl_value *args); |
| 571 | /* Consumes result; returns it, or TL_UNIT after a panic. */ |
| 572 | tl_value tl_callback_end(tl_callback *cb, tl_value result); |
| 573 | static inline bool tl_callback_failed(const tl_callback *cb) { |
| 574 | return cb->ctx->yielding != TL_YIELD_NONE; |
| 575 | } |
| 576 | |
| 577 | /* Building prelude values from C (constructor order of std/prelude.lse). */ |
| 578 | static inline tl_value tl_bool(bool b) { return b ? TL_TRUE : TL_FALSE; } |
| 579 | static inline tl_value tl_wrap1(tl_ctx *ctx, uint16_t tag, tl_value v) { |
| 580 | tl_block *b = tl_alloc(ctx, tag, 1, 0); |
| 581 | b->fields[0] = v; |
| 582 | return tl_from_block(b); |
| 583 | } |
| 584 | #define TL_NONE ((tl_value)1) /* None: constructor 0 */ |
| 585 | static inline tl_value tl_some(tl_ctx *ctx, tl_value v) { return tl_wrap1(ctx, 1, v); } |
| 586 | static inline tl_value tl_ok(tl_ctx *ctx, tl_value v) { return tl_wrap1(ctx, 0, v); } |
| 587 | static inline tl_value tl_error(tl_ctx *ctx, tl_value e) { return tl_wrap1(ctx, 1, e); } |
| 588 | static inline tl_value tl_string_cstr(tl_ctx *ctx, const char *s) { |
| 589 | return tl_string_from(ctx, s, strlen(s)); |
| 590 | } |
| 591 | |
| 592 | /* Scheduling and actors (runtime/louise_rt/src/sched.rs). Compiled programs |
| 593 | * use std/process through tl_root_cap; tl_spawn and tl_kill are the M0 |
| 594 | * spike's (runtime/spike/spike.c). */ |
| 595 | void tl_preempt(tl_ctx *ctx); |
| 596 | tl_value tl_spawn(tl_ctx *ctx, tl_value closure); /* consumes closure; returns pid */ |
| 597 | void tl_kill(tl_ctx *ctx, tl_value pid); /* async exit signal */ |
| 598 | int64_t tl_actor_count(void); |
| 599 | int tl_main(tl_value (*entry)(tl_ctx *ctx)); /* runs entry as the root actor */ |
| 600 | /* Like tl_main for a compiled program: if the root actor panics, print |
| 601 | * "error: panic: <message>" (as `louise run` does) and return 101. |
| 602 | * `actor_caps`: the kinds (TL_ROOT_*) of the `n_actor_caps` root |
| 603 | * capabilities every actor body takes, in order. */ |
| 604 | int tl_main_program(tl_value (*entry)(tl_ctx *ctx), const uint8_t *actor_caps, |
| 605 | int64_t n_actor_caps); |
| 606 | |
| 607 | /* Checked builds (debug ones: `-DTL_CHECKED`) verify two invariants at |
| 608 | * run time (issue #217; louise_rt/src/checked.rs). `main` calls |
| 609 | * tl_checked() first. |
| 610 | * - TL_NO_UNWIND wraps a call of an extern marked as never unwinding: a |
| 611 | * panic or a park in it aborts, naming it, instead of running on |
| 612 | * without a yield check. |
| 613 | * - TL_XARG copies argument slot `i` of `ctx->xargs`, which the runtime |
| 614 | * poisons at each slice: reading a poisoned slot aborts, since |
| 615 | * something suspended between the caller's stores and this copy. */ |
| 616 | #define TL_XARGS_POISON ((tl_value)2) |
| 617 | void tl_checked(void); |
| 618 | void tl_no_unwind_enter(const char *symbol); |
| 619 | void tl_no_unwind_exit(void); |
| 620 | void tl_xargs_poisoned(int64_t slot) __attribute__((noreturn)); |
| 621 | #ifdef TL_CHECKED |
| 622 | static inline tl_value tl_no_unwind_done(tl_value v) { |
| 623 | tl_no_unwind_exit(); |
| 624 | return v; |
| 625 | } |
| 626 | static inline tl_value tl_xarg(tl_ctx *ctx, int64_t i) { |
| 627 | tl_value v = ctx->xargs[i]; |
| 628 | if (v == TL_XARGS_POISON) tl_xargs_poisoned(i); |
| 629 | return v; |
| 630 | } |
| 631 | #define TL_NO_UNWIND(symbol, call) (tl_no_unwind_enter(symbol), tl_no_unwind_done(call)) |
| 632 | #define TL_XARG(ctx, i) tl_xarg((ctx), (i)) |
| 633 | #else |
| 634 | #define TL_NO_UNWIND(symbol, call) (call) |
| 635 | #define TL_XARG(ctx, i) ((ctx)->xargs[(i)]) |
| 636 | #endif |
| 637 | |
| 638 | /* ------------------------------------------------------------------------ |
| 639 | * Inline fast paths |
| 640 | * ---------------------------------------------------------------------- */ |
| 641 | /* Immortal blocks (rc 0: literals, constant dictionaries) are left alone |
| 642 | * inline, without a call. A local count stops at TL_RC_STUCK instead of |
| 643 | * overflowing (issue #217): the runtime leaves it there, and the block is |
| 644 | * never freed. One unsigned comparison tests both ends of each range. */ |
| 645 | #define TL_RC_STUCK INT32_MAX |
| 646 | |
| 647 | /* A block's count, read atomically: a shared block's count changes on |
| 648 | * other threads, so even the test that tells a local count from a shared |
| 649 | * one must not be a plain read (heap::rc, heap.rs). A relaxed load is a |
| 650 | * plain load on x86-64 and aarch64. Writes stay plain: only a local count |
| 651 | * is written that way, by its one owner. */ |
| 652 | static inline int32_t tl_rc(const tl_block *b) { |
| 653 | return __atomic_load_n(&b->h.rc, __ATOMIC_RELAXED); |
| 654 | } |
| 655 | |
| 656 | static inline tl_value tl_dup(tl_value v) { |
| 657 | if (tl_is_ptr(v)) { |
| 658 | tl_block *b = tl_as_block(v); |
| 659 | int32_t rc = tl_rc(b); |
| 660 | /* 1 <= rc < TL_RC_STUCK */ |
| 661 | if (tl_likely((uint32_t)rc - 1u < (uint32_t)TL_RC_STUCK - 1u)) b->h.rc = rc + 1; |
| 662 | else if (rc != 0) tl_dup_shared(b); |
| 663 | } |
| 664 | return v; |
| 665 | } |
| 666 | |
| 667 | static inline void tl_drop(tl_ctx *ctx, tl_value v) { |
| 668 | if (tl_is_ptr(v)) { |
| 669 | tl_block *b = tl_as_block(v); |
| 670 | int32_t rc = tl_rc(b); |
| 671 | /* 2 <= rc < TL_RC_STUCK */ |
| 672 | if (tl_likely((uint32_t)rc - 2u < (uint32_t)TL_RC_STUCK - 2u)) b->h.rc = rc - 1; |
| 673 | else if (rc != 0) tl_drop_slow(ctx, b); |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | static inline bool tl_is_unique(tl_value v) { |
| 678 | return tl_is_ptr(v) && tl_rc(tl_as_block(v)) == 1; |
| 679 | } |
| 680 | |
| 681 | static inline tl_closure_fn tl_closure_code(tl_value clo) { |
| 682 | tl_block *b = tl_as_block(clo); |
| 683 | tl_closure_fn code; /* memcpy: the raw area is not a tl_value (aliasing) */ |
| 684 | memcpy(&code, &b->fields[b->h.scan_fsize], sizeof code); |
| 685 | return code; |
| 686 | } |
| 687 | |
| 688 | /* The borrowed-contract entry (ABI 10, ADR 0068): the second code |
| 689 | * pointer of the raw area. */ |
| 690 | static inline tl_closure_fn tl_closure_code_borrowed(tl_value clo) { |
| 691 | tl_block *b = tl_as_block(clo); |
| 692 | tl_closure_fn code; |
| 693 | memcpy(&code, &b->fields[b->h.scan_fsize] + 1, sizeof code); |
| 694 | return code; |
| 695 | } |
| 696 | |
| 697 | /* Function prologue and loop back-edge: count a reduction and maybe yield to |
| 698 | * the scheduler. An exit signal is delivered while yielded, so on resume the |
| 699 | * code jumps to `on_yield`, which drops the owned locals and returns |
| 700 | * TL_UNIT. The yielding check is on the slow path only. */ |
| 701 | #define TL_ENTER(ctx, on_yield) \ |
| 702 | do { \ |
| 703 | if (tl_unlikely(--(ctx)->reductions <= 0)) { \ |
| 704 | tl_preempt(ctx); \ |
| 705 | if (TL_YIELDING(ctx)) goto on_yield; \ |
| 706 | } \ |
| 707 | } while (0) |
| 708 | |
| 709 | /* After a call that may yield: true when the caller must drop its owned |
| 710 | * locals and return immediately (see docs/runtime-abi.md, "Unwinding"). */ |
| 711 | #define TL_YIELDING(ctx) tl_unlikely((ctx)->yielding != TL_YIELD_NONE) |
| 712 | |
| 713 | /* Release a reference the frame holds while it unwinds: a dup in a CLONE |
| 714 | * pass (see tl_yield_kind). Out of line: unwinding paths are cold, and |
| 715 | * keeping them small keeps the code around them fast. */ |
| 716 | void tl_unwind_drop(tl_ctx *ctx, tl_value v); |
| 717 | #define TL_UNWIND_DROP(ctx, v) tl_unwind_drop((ctx), (v)) |
| 718 | |
| 719 | /* Int arithmetic: two small ints without overflow stay inline; anything |
| 720 | * else (an overflow, a big integer) takes the runtime's slow path, which |
| 721 | * promotes to a big integer. Never panics. */ |
| 722 | static inline tl_value tl_int_add(tl_ctx *ctx, tl_value a, tl_value b) { |
| 723 | int64_t r; |
| 724 | /* (2x+1) + (2y+1) - 1 == 2(x+y)+1; overflow of the 64-bit sum == 63-bit overflow */ |
| 725 | if (tl_likely((a & b & 1) && !__builtin_add_overflow((int64_t)a, (int64_t)b - 1, &r))) |
| 726 | return (tl_value)r; |
| 727 | return tl_int_add_slow(ctx, a, b); |
| 728 | } |
| 729 | |
| 730 | static inline tl_value tl_int_sub(tl_ctx *ctx, tl_value a, tl_value b) { |
| 731 | int64_t r; |
| 732 | if (tl_likely((a & b & 1) && !__builtin_sub_overflow((int64_t)a, (int64_t)b - 1, &r))) |
| 733 | return (tl_value)r; |
| 734 | return tl_int_sub_slow(ctx, a, b); |
| 735 | } |
| 736 | |
| 737 | static inline tl_value tl_int_mul(tl_ctx *ctx, tl_value a, tl_value b) { |
| 738 | int64_t r; |
| 739 | /* x * (2y) == 2xy, which overflows 64 bits exactly when xy overflows 63; |
| 740 | * 2xy + 1 then cannot overflow. */ |
| 741 | if (tl_likely((a & b & 1) && !__builtin_mul_overflow(tl_int_get(a), (int64_t)b - 1, &r))) |
| 742 | return (tl_value)r + 1; |
| 743 | return tl_int_mul_slow(ctx, a, b); |
| 744 | } |
| 745 | |
| 746 | /* Truncating division. Small operands cannot overflow 64 bits; only |
| 747 | * -2^62 / -1 leaves the small range, so a divisor of -1 (and 0, which |
| 748 | * panics) takes the slow path. */ |
| 749 | static inline tl_value tl_int_div(tl_ctx *ctx, tl_value a, tl_value b) { |
| 750 | if (tl_likely((a & b & 1) && b != tl_int(0) && b != tl_int(-1))) |
| 751 | return tl_int(tl_int_get(a) / tl_int_get(b)); |
| 752 | return tl_int_div_slow(ctx, a, b); |
| 753 | } |
| 754 | |
| 755 | /* The remainder has the sign of `a`; it is always small. */ |
| 756 | static inline tl_value tl_int_rem(tl_ctx *ctx, tl_value a, tl_value b) { |
| 757 | if (tl_likely((a & b & 1) && b != tl_int(0))) |
| 758 | return tl_int(tl_int_get(a) % tl_int_get(b)); |
| 759 | return tl_int_rem_slow(ctx, a, b); |
| 760 | } |
| 761 | |
| 762 | /* Canonical forms: a small int equals only the same word. */ |
| 763 | static inline tl_value tl_int_eq(tl_ctx *ctx, tl_value a, tl_value b) { |
| 764 | if (tl_likely(a == b || ((a | b) & 1))) return tl_bool(a == b); |
| 765 | return tl_int_eq_slow(ctx, a, b); |
| 766 | } |
| 767 | |
| 768 | /* `Order`: Lt, Eq, Gt are 0, 1, 2. Tagging keeps the order of small ints. */ |
| 769 | static inline tl_value tl_int_compare(tl_ctx *ctx, tl_value a, tl_value b) { |
| 770 | if (tl_likely(a & b & 1)) |
| 771 | return tl_int(((int64_t)a > (int64_t)b) - ((int64_t)a < (int64_t)b) + 1); |
| 772 | return tl_int_compare_slow(ctx, a, b); |
| 773 | } |
| 774 | |
| 775 | /* Bitwise operations (std/int, ADR 0035), two's complement with infinite |
| 776 | * sign extension. On small ints (`2x+1`), and/or keep the tag bit, xor |
| 777 | * restores it, and not flips every bit but it: none can overflow. A shift |
| 778 | * stays inline while its result is small; a big operand, a negative |
| 779 | * amount (a panic) or a result outside 63 bits takes the slow path. */ |
| 780 | static inline tl_value tl_int_and(tl_ctx *ctx, tl_value a, tl_value b) { |
| 781 | if (tl_likely(a & b & 1)) return a & b; |
| 782 | return tl_int_and_slow(ctx, a, b); |
| 783 | } |
| 784 | |
| 785 | static inline tl_value tl_int_or(tl_ctx *ctx, tl_value a, tl_value b) { |
| 786 | if (tl_likely(a & b & 1)) return a | b; |
| 787 | return tl_int_or_slow(ctx, a, b); |
| 788 | } |
| 789 | |
| 790 | static inline tl_value tl_int_xor(tl_ctx *ctx, tl_value a, tl_value b) { |
| 791 | if (tl_likely(a & b & 1)) return (a ^ b) | 1; |
| 792 | return tl_int_xor_slow(ctx, a, b); |
| 793 | } |
| 794 | |
| 795 | static inline tl_value tl_int_not(tl_ctx *ctx, tl_value a) { |
| 796 | if (tl_likely(a & 1)) return a ^ ~(tl_value)1; |
| 797 | return tl_int_not_slow(ctx, a); |
| 798 | } |
| 799 | |
| 800 | static inline tl_value tl_int_shift_left(tl_ctx *ctx, tl_value n, tl_value k) { |
| 801 | if (tl_likely(n & k & 1)) { |
| 802 | int64_t x = tl_int_get(n), s = tl_int_get(k); |
| 803 | if (s >= 0 && s < 62) { |
| 804 | int64_t r = (int64_t)((uint64_t)x << s); |
| 805 | if ((r >> s) == x && r >= TL_INT_MIN && r <= TL_INT_MAX) return tl_int(r); |
| 806 | } |
| 807 | } |
| 808 | return tl_int_shift_left_slow(ctx, n, k); |
| 809 | } |
| 810 | |
| 811 | static inline tl_value tl_int_shift_right(tl_ctx *ctx, tl_value n, tl_value k) { |
| 812 | if (tl_likely(n & k & 1)) { |
| 813 | int64_t x = tl_int_get(n), s = tl_int_get(k); |
| 814 | if (s >= 0) return tl_int(s < 63 ? x >> s : (x < 0 ? -1 : 0)); |
| 815 | } |
| 816 | return tl_int_shift_right_slow(ctx, n, k); |
| 817 | } |
| 818 | |
| 819 | /* Arrays (ADR 0014): `fields[0]` holds the length as an Int, the elements |
| 820 | * follow. Reading one is a bounds check and a load, inline; only `at` out |
| 821 | * of range calls the runtime, to panic (issue #128). An index that is a |
| 822 | * big integer is out of range. */ |
| 823 | static inline tl_value tl_array_length(tl_ctx *ctx, tl_value a) { |
| 824 | (void)ctx; |
| 825 | return tl_as_block(a)->fields[0]; |
| 826 | } |
| 827 | |
| 828 | static inline tl_value tl_array_get(tl_ctx *ctx, tl_value a, tl_value i) { |
| 829 | tl_value *f = tl_as_block(a)->fields; |
| 830 | if (tl_likely(tl_is_int(i)) && (uint64_t)tl_int_get(i) < (uint64_t)tl_int_get(f[0])) |
| 831 | return tl_some(ctx, tl_dup(f[1 + tl_int_get(i)])); |
| 832 | return TL_NONE; |
| 833 | } |
| 834 | |
| 835 | static inline tl_value tl_array_at(tl_ctx *ctx, tl_value a, tl_value i) { |
| 836 | tl_value *f = tl_as_block(a)->fields; |
| 837 | if (tl_likely(tl_is_int(i) && (uint64_t)tl_int_get(i) < (uint64_t)tl_int_get(f[0]))) |
| 838 | return tl_dup(f[1 + tl_int_get(i)]); |
| 839 | return tl_array_at_slow(ctx, a, i); |
| 840 | } |
| 841 | |
| 842 | /* Tail recursion modulo cons (ADR 0039; runtime/louise_rt/src/ctx.rs). |
| 843 | * A context is unit (empty) or a 3-field block: the root (owned), then |
| 844 | * tl_int(address of the last cell) and tl_int(the last cell's hole field). |
| 845 | * Each cell owns the next through its hole field; the last cell's hole is |
| 846 | * unit. Every cell of one function's contexts has the same hole field, so a |
| 847 | * shared context (a multi-shot resumption kept a copy) is copied along |
| 848 | * root -> last before it changes. Both functions consume their |
| 849 | * arguments. */ |
| 850 | tl_value tl_ctx_new(tl_ctx *ctx, tl_value cell, int64_t hole); |
| 851 | tl_value tl_ctx_extend_slow(tl_ctx *ctx, tl_value k, tl_value cell, int64_t hole); |
| 852 | tl_value tl_ctx_apply_slow(tl_ctx *ctx, tl_value k, tl_value value); |
| 853 | static inline tl_block *tl_ctx_last(tl_block *k) { |
| 854 | return (tl_block *)(uintptr_t)tl_int_get(k->fields[1]); |
| 855 | } |
| 856 | static inline tl_value tl_ctx_extend(tl_ctx *ctx, tl_value k, tl_value cell, int64_t hole) { |
| 857 | if (k == TL_UNIT) return tl_ctx_new(ctx, cell, hole); |
| 858 | tl_block *b = tl_as_block(k); |
| 859 | if (tl_likely(b->h.rc == 1)) { |
| 860 | tl_ctx_last(b)->fields[tl_int_get(b->fields[2])] = cell; |
| 861 | b->fields[1] = tl_int((int64_t)(uintptr_t)tl_as_block(cell)); |
| 862 | b->fields[2] = tl_int(hole); |
| 863 | return k; |
| 864 | } |
| 865 | return tl_ctx_extend_slow(ctx, k, cell, hole); |
| 866 | } |
| 867 | static inline tl_value tl_ctx_apply(tl_ctx *ctx, tl_value k, tl_value value) { |
| 868 | if (k == TL_UNIT) return value; |
| 869 | tl_block *b = tl_as_block(k); |
| 870 | if (tl_likely(b->h.rc == 1)) { |
| 871 | tl_ctx_last(b)->fields[tl_int_get(b->fields[2])] = value; |
| 872 | tl_value root = b->fields[0]; |
| 873 | b->fields[0] = TL_UNIT; |
| 874 | tl_drop(ctx, k); /* frees the context block alone */ |
| 875 | return root; |
| 876 | } |
| 877 | return tl_ctx_apply_slow(ctx, k, value); |
| 878 | } |
| 879 | |
| 880 | #ifdef __cplusplus |
| 881 | } |
| 882 | #endif |
| 883 | |
| 884 | #endif /* LOUISE_RT_H */ |