| @@ -0,0 +1,518 @@ |
| 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 2 |
| 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 | + |
| 98 | +/* Tags below TL_TAG_RUNTIME_MIN are user constructors (no raw area). */ |
| 99 | +#define TL_TAG_RUNTIME_MIN 0xFF00 |
| 100 | +#define TL_TAG_STRING 0xFF01 /* raw: int64 len; char bytes[len]; '\0' */ |
| 101 | +#define TL_TAG_BYTES 0xFF02 /* raw: int64 len; uint8 bytes[len] */ |
| 102 | +#define TL_TAG_FLOAT 0xFF03 /* raw: double */ |
| 103 | +#define TL_TAG_CLOSURE 0xFF04 /* fields: env; raw: tl_closure_fn code */ |
| 104 | +#define TL_TAG_REF 0xFF05 /* fields: [value]; no raw */ |
| 105 | +#define TL_TAG_REGION 0xFF06 /* raw: runtime-private list of refs */ |
| 106 | +#define TL_TAG_FRAME 0xFF07 /* raw: runtime-private `ctl` handler */ |
| 107 | +#define TL_TAG_RESOURCE 0xFF08 /* raw: void *ptr; finalizer */ |
| 108 | +#define TL_TAG_CAP 0xFF09 /* fields: clauses, marker; no raw */ |
| 109 | +#define TL_TAG_ARRAY 0xFF0A /* TL_SCAN_LARGE; raw: int64 spare; slots */ |
| 110 | +#define TL_TAG_BIGINT 0xFF0B /* raw: int64 ±nlimbs; uint64 limbs[n] */ |
| 111 | +#define TL_TAG_BUILDER 0xFF0C /* raw: int64 len; int64 cap; bytes[cap] */ |
| 112 | + |
| 113 | +static inline tl_block *tl_as_block(tl_value v) { return (tl_block *)v; } |
| 114 | +static inline tl_value tl_from_block(tl_block *b) { return (tl_value)b; } |
| 115 | + |
| 116 | +/* ------------------------------------------------------------------------ |
| 117 | + * Execution context |
| 118 | + * |
| 119 | + * One tl_ctx per actor, passed as the first argument of every generated |
| 120 | + * function. It is never stored in thread-local storage: an actor's code |
| 121 | + * must not observe which OS thread runs it. Fields after `rt` are private. |
| 122 | + * ---------------------------------------------------------------------- */ |
| 123 | +typedef enum tl_yield_kind { |
| 124 | + TL_YIELD_NONE = 0, |
| 125 | + TL_YIELD_FINAL = 1, /* `final ctl` op unwinding to handler `yield_marker` */ |
| 126 | + TL_YIELD_PANIC = 2, /* panic unwinding to the actor root */ |
| 127 | + TL_YIELD_KILL = 3, /* exit signal unwinding to the actor root */ |
| 128 | + TL_YIELD_CANCEL = 4, /* a never-resumed `ctl` handler body is cancelled */ |
| 129 | + /* Not unwinding: passes over a suspended `multi ctl` continuation |
| 130 | + * (ADR 0017). CLONE takes one more reference for each one the frames |
| 131 | + * hold (their unwinding code dups instead of dropping); DISCARD |
| 132 | + * releases them without running `defer` cleanups. */ |
| 133 | + TL_YIELD_CLONE = 5, |
| 134 | + TL_YIELD_DISCARD = 6 |
| 135 | +} tl_yield_kind; |
| 136 | + |
| 137 | +/* Generated functions share one signature (ctx + 6 values) so that every |
| 138 | + * tail call can be `musttail`; further arguments go through ctx->xargs, |
| 139 | + * which the callee reads on entry. Closure code receives the closure as |
| 140 | + * the first value. */ |
| 141 | +#define TL_XARGS 26 |
| 142 | + |
| 143 | +typedef struct tl_ctx { |
| 144 | + int32_t yielding; /* tl_yield_kind; checked after calls that may yield */ |
| 145 | + int32_t reductions; /* decremented on function entry; <= 0 -> tl_preempt */ |
| 146 | + uint64_t yield_marker; /* TL_YIELD_FINAL: marker of the target handler */ |
| 147 | + tl_value yield_value; /* owned payload: op argument or panic/exit reason */ |
| 148 | + void *evv; /* unused (null): handlers are values (ADR 0010) */ |
| 149 | + void *rt; /* runtime-private */ |
| 150 | + tl_value xargs[TL_XARGS]; /* call arguments after the sixth (Revision 1) */ |
| 151 | +} tl_ctx; |
| 152 | + |
| 153 | +_Static_assert(offsetof(tl_ctx, yielding) == 0, "tl_ctx layout"); |
| 154 | +_Static_assert(offsetof(tl_ctx, reductions) == 4, "tl_ctx layout"); |
| 155 | +_Static_assert(offsetof(tl_ctx, yield_marker) == 8, "tl_ctx layout"); |
| 156 | +_Static_assert(offsetof(tl_ctx, yield_value) == 16, "tl_ctx layout"); |
| 157 | +_Static_assert(offsetof(tl_ctx, xargs) == 40, "tl_ctx layout"); |
| 158 | +_Static_assert(sizeof(tl_ctx) == 40 + 8 * TL_XARGS, "tl_ctx layout"); |
| 159 | + |
| 160 | +typedef tl_value (*tl_fn)(tl_ctx *ctx, tl_value a0, tl_value a1, tl_value a2, |
| 161 | + tl_value a3, tl_value a4, tl_value a5); |
| 162 | + |
| 163 | +/* Closure entry point, stored type-erased. Callers cast it to the exact |
| 164 | + * arity, e.g. tl_value (*)(tl_ctx *, tl_value self, tl_value a), before |
| 165 | + * calling (never through a variadic type). All arguments, including |
| 166 | + * `self`, are owned by the callee. */ |
| 167 | +typedef void (*tl_closure_fn)(void); |
| 168 | + |
| 169 | +/* ------------------------------------------------------------------------ |
| 170 | + * Runtime entry points (implemented in Rust) |
| 171 | + * ---------------------------------------------------------------------- */ |
| 172 | + |
| 173 | +/* Memory */ |
| 174 | +tl_block *tl_alloc(tl_ctx *ctx, uint16_t tag, uint8_t scan_fsize, size_t raw_bytes); |
| 175 | +void tl_drop_slow(tl_ctx *ctx, tl_block *b); /* rc <= 1 or shared */ |
| 176 | +void tl_free(tl_ctx *ctx, tl_block *b); /* unique b; children already moved out */ |
| 177 | +void tl_dup_shared(tl_block *b); |
| 178 | +int64_t tl_stats_live_blocks(void); /* -1 when stats disabled */ |
| 179 | + |
| 180 | +/* Strings */ |
| 181 | +tl_value tl_string_from(tl_ctx *ctx, const char *bytes, size_t len); |
| 182 | +size_t tl_string_len(tl_value s); /* borrows s */ |
| 183 | +const char *tl_string_ptr(tl_value s); /* borrows s; NUL-terminated */ |
| 184 | +tl_value tl_println(tl_ctx *ctx, tl_value s); /* borrows s; may park (ADR 0022) */ |
| 185 | + |
| 186 | +/* Closures */ |
| 187 | +tl_value tl_closure_alloc(tl_ctx *ctx, tl_closure_fn code, uint8_t env_count); |
| 188 | + |
| 189 | +/* Effects and unwinding */ |
| 190 | +uint64_t tl_fresh_marker(tl_ctx *ctx); |
| 191 | +tl_value tl_yield_final(tl_ctx *ctx, uint64_t marker, tl_value payload); /* consumes payload */ |
| 192 | +bool tl_catch_final(tl_ctx *ctx, uint64_t marker, tl_value *payload_out); |
| 193 | +/* `defer` (ADR 0016): after a body returned `result` (normally or while |
| 194 | + * unwinding), run `cleanup` with the unwinding set aside. Consumes both; |
| 195 | + * returns `result`, or unit if unwinding continues. */ |
| 196 | +tl_value tl_defer(tl_ctx *ctx, tl_value result, tl_value cleanup); |
| 197 | +tl_value tl_panic_str(tl_ctx *ctx, const char *reason); |
| 198 | + |
| 199 | +/* Generated main calls this first: the runtime stops if it was built for |
| 200 | + * another TL_ABI_VERSION. */ |
| 201 | +void tl_abi_check(int64_t version); |
| 202 | + |
| 203 | +/* Int slow paths (runtime/louise_rt/src/int.rs): an overflow, or a big |
| 204 | + * integer argument. They borrow their arguments; results are canonical. */ |
| 205 | +tl_value tl_int_add_slow(tl_ctx *ctx, tl_value a, tl_value b); |
| 206 | +tl_value tl_int_sub_slow(tl_ctx *ctx, tl_value a, tl_value b); |
| 207 | +tl_value tl_int_mul_slow(tl_ctx *ctx, tl_value a, tl_value b); |
| 208 | +tl_value tl_int_div_slow(tl_ctx *ctx, tl_value a, tl_value b); /* panics on 0 */ |
| 209 | +tl_value tl_int_rem_slow(tl_ctx *ctx, tl_value a, tl_value b); /* panics on 0 */ |
| 210 | +tl_value tl_int_eq_slow(tl_ctx *ctx, tl_value a, tl_value b); |
| 211 | +tl_value tl_int_compare_slow(tl_ctx *ctx, tl_value a, tl_value b); |
| 212 | +/* For C bindings: an Int as int64_t (false if it does not fit), and an |
| 213 | + * int64_t as an Int (big outside 63 bits). */ |
| 214 | +bool tl_int_to_i64(tl_value v, int64_t *out); |
| 215 | +tl_value tl_int_from_i64(tl_ctx *ctx, int64_t n); |
| 216 | + |
| 217 | +/* Called by compiled programs (runtime/louise_rt/src/prims.rs). Like every |
| 218 | + * `@extern` function they borrow their arguments; results are owned. */ |
| 219 | +void tl_report_leaks(void); /* with LOUISE_LEAKS set */ |
| 220 | +tl_value tl_panic(tl_ctx *ctx, tl_value message); |
| 221 | +tl_value tl_float_from(tl_ctx *ctx, double x); |
| 222 | +double tl_float_get(tl_value v); |
| 223 | +tl_value tl_float_add(tl_ctx *ctx, tl_value a, tl_value b); |
| 224 | +tl_value tl_float_sub(tl_ctx *ctx, tl_value a, tl_value b); |
| 225 | +tl_value tl_float_mul(tl_ctx *ctx, tl_value a, tl_value b); |
| 226 | +tl_value tl_float_div(tl_ctx *ctx, tl_value a, tl_value b); |
| 227 | +tl_value tl_float_rem(tl_ctx *ctx, tl_value a, tl_value b); |
| 228 | +tl_value tl_float_eq(tl_ctx *ctx, tl_value a, tl_value b); |
| 229 | +tl_value tl_float_compare(tl_ctx *ctx, tl_value a, tl_value b); |
| 230 | +tl_value tl_float_show(tl_ctx *ctx, tl_value a); |
| 231 | +tl_value tl_hash_float(tl_ctx *ctx, tl_value a); |
| 232 | +tl_value tl_int_show(tl_ctx *ctx, tl_value a); |
| 233 | +tl_value tl_hash_int(tl_ctx *ctx, tl_value a); |
| 234 | +tl_value tl_hash_combine(tl_ctx *ctx, tl_value a, tl_value b); |
| 235 | +tl_value tl_string_eq(tl_ctx *ctx, tl_value a, tl_value b); |
| 236 | +tl_value tl_string_compare(tl_ctx *ctx, tl_value a, tl_value b); |
| 237 | +tl_value tl_string_concat(tl_ctx *ctx, tl_value a, tl_value b); |
| 238 | +tl_value tl_string_length(tl_ctx *ctx, tl_value a); |
| 239 | +tl_value tl_string_starts_with(tl_ctx *ctx, tl_value a, tl_value b); |
| 240 | +tl_value tl_string_ends_with(tl_ctx *ctx, tl_value a, tl_value b); |
| 241 | +tl_value tl_string_contains(tl_ctx *ctx, tl_value a, tl_value b); |
| 242 | +tl_value tl_string_byte_length(tl_ctx *ctx, tl_value a); |
| 243 | +tl_value tl_string_to_lower(tl_ctx *ctx, tl_value a); |
| 244 | +tl_value tl_string_trim(tl_ctx *ctx, tl_value a); |
| 245 | +tl_value tl_string_split(tl_ctx *ctx, tl_value a, tl_value sep); |
| 246 | +tl_value tl_string_parse_int(tl_ctx *ctx, tl_value a); |
| 247 | +tl_value tl_string_join(tl_ctx *ctx, tl_value parts, tl_value sep); |
| 248 | +/* `Bytes` and their builders (runtime/louise_rt/src/bytes.rs, builder.rs; ADR |
| 249 | + * 0012). The `append` functions and `build` consume their arguments (they |
| 250 | + * are `owned` externs): a unique builder is updated in place. */ |
| 251 | +tl_value tl_bytes_length(tl_ctx *ctx, tl_value b); |
| 252 | +tl_value tl_bytes_at(tl_ctx *ctx, tl_value b, tl_value i); |
| 253 | +tl_value tl_bytes_slice(tl_ctx *ctx, tl_value b, tl_value start, tl_value end); |
| 254 | +tl_value tl_bytes_concat(tl_ctx *ctx, tl_value a, tl_value b); |
| 255 | +tl_value tl_bytes_concat_all(tl_ctx *ctx, tl_value parts); |
| 256 | +tl_value tl_bytes_eq(tl_ctx *ctx, tl_value a, tl_value b); |
| 257 | +tl_value tl_bytes_compare(tl_ctx *ctx, tl_value a, tl_value b); |
| 258 | +tl_value tl_hash_bytes(tl_ctx *ctx, tl_value b); |
| 259 | +tl_value tl_bytes_debug(tl_ctx *ctx, tl_value b); |
| 260 | +tl_value tl_bytes_from_string(tl_ctx *ctx, tl_value s); |
| 261 | +tl_value tl_bytes_to_string(tl_ctx *ctx, tl_value b); |
| 262 | +tl_value tl_bytes_to_string_lossy(tl_ctx *ctx, tl_value b); |
| 263 | +tl_value tl_bytes_from_list(tl_ctx *ctx, tl_value items); /* panics on a non-byte */ |
| 264 | +tl_value tl_bytes_to_list(tl_ctx *ctx, tl_value b); |
| 265 | +tl_value tl_bytes_builder(tl_ctx *ctx, tl_value capacity); |
| 266 | +tl_value tl_bytes_append(tl_ctx *ctx, tl_value b, tl_value data); |
| 267 | +tl_value tl_bytes_append_string(tl_ctx *ctx, tl_value b, tl_value s); |
| 268 | +tl_value tl_bytes_append_byte(tl_ctx *ctx, tl_value b, tl_value byte); /* panics on a non-byte */ |
| 269 | +tl_value tl_bytes_build(tl_ctx *ctx, tl_value b); |
| 270 | +/* Arrays (runtime/louise_rt/src/array.rs; ADR 0014). `set` and `push` consume |
| 271 | + * their arguments (`owned`): a unique array is updated in place. */ |
| 272 | +tl_value tl_array_with_capacity(tl_ctx *ctx, tl_value n); |
| 273 | +tl_value tl_array_length(tl_ctx *ctx, tl_value a); |
| 274 | +tl_value tl_array_from_list(tl_ctx *ctx, tl_value list); |
| 275 | +tl_value tl_array_to_list(tl_ctx *ctx, tl_value a); |
| 276 | +tl_value tl_array_get(tl_ctx *ctx, tl_value a, tl_value i); |
| 277 | +tl_value tl_array_at(tl_ctx *ctx, tl_value a, tl_value i); /* panics out of range */ |
| 278 | +tl_value tl_array_set(tl_ctx *ctx, tl_value a, tl_value i, tl_value x); /* panics out of range */ |
| 279 | +tl_value tl_array_push(tl_ctx *ctx, tl_value a, tl_value x); |
| 280 | +tl_value tl_array_slice(tl_ctx *ctx, tl_value a, tl_value start, tl_value end); |
| 281 | +/* Sockets (runtime/louise_rt/src/net.rs; may park the actor) and HTTP framing |
| 282 | + * (http.rs). */ |
| 283 | +tl_value tl_net_listen(tl_ctx *ctx, tl_value host, tl_value port); |
| 284 | +tl_value tl_net_port(tl_ctx *ctx, tl_value listener); |
| 285 | +tl_value tl_net_accept(tl_ctx *ctx, tl_value listener); |
| 286 | +tl_value tl_net_connect(tl_ctx *ctx, tl_value host, tl_value port); |
| 287 | +tl_value tl_net_read(tl_ctx *ctx, tl_value conn, tl_value max); |
| 288 | +tl_value tl_net_write(tl_ctx *ctx, tl_value conn, tl_value data); /* Bytes */ |
| 289 | +tl_value tl_net_write_string(tl_ctx *ctx, tl_value conn, tl_value data); /* String */ |
| 290 | +tl_value tl_net_close(tl_ctx *ctx, tl_value conn); |
| 291 | +tl_value tl_http_parse_request(tl_ctx *ctx, tl_value buffer, tl_value at); |
| 292 | +tl_value tl_http_parse_chunk(tl_ctx *ctx, tl_value buffer, tl_value at); |
| 293 | +tl_value tl_url_decode(tl_ctx *ctx, tl_value s); |
| 294 | +tl_value tl_os_env(tl_ctx *ctx, tl_value name); |
| 295 | +tl_value tl_string_debug(tl_ctx *ctx, tl_value a); |
| 296 | +tl_value tl_hash_string(tl_ctx *ctx, tl_value a); |
| 297 | +tl_value tl_html_escape(tl_ctx *ctx, tl_value a); |
| 298 | +tl_value tl_print(tl_ctx *ctx, tl_value a); |
| 299 | +tl_value tl_eprintln(tl_ctx *ctx, tl_value a); |
| 300 | +/* Regions (ADR 0002, Revision 1; runtime/louise_rt/src/region.rs). */ |
| 301 | +tl_value tl_region_new(tl_ctx *ctx); |
| 302 | +tl_value tl_region_end(tl_ctx *ctx, tl_value region); |
| 303 | +/* Leave `region` (consumed): end it, unless copies of a `multi ctl` |
| 304 | + * continuation inside it have yet to leave (ADR 0017). */ |
| 305 | +void tl_region_exit(tl_ctx *ctx, tl_value region); |
| 306 | +tl_value tl_ref_new(tl_ctx *ctx, tl_value region, tl_value value); /* consumes value */ |
| 307 | +tl_value tl_ref_get(tl_ctx *ctx, tl_value ref); |
| 308 | +tl_value tl_ref_set(tl_ctx *ctx, tl_value ref, tl_value value); /* consumes value */ |
| 309 | + |
| 310 | +/* Handlers with `ctl` operations (runtime/louise_rt/src/handlers.rs): the body |
| 311 | + * runs on its own fiber; `cap` is the block of clauses then the marker. */ |
| 312 | +tl_value tl_handle_ctl(tl_ctx *ctx, tl_value body, tl_value cap, tl_value ret, uint64_t marker); |
| 313 | +tl_value tl_perform_ctl(tl_ctx *ctx, tl_value cap, tl_value payload); /* consumes payload */ |
| 314 | +/* The same for an effect with `multi ctl` operations (ADR 0017): bit i of |
| 315 | + * `multi_ops` is set if operation i is one; its `resume` may be called |
| 316 | + * several times. They are performed with tl_perform_ctl. */ |
| 317 | +tl_value tl_handle_multi(tl_ctx *ctx, tl_value body, tl_value cap, tl_value ret, uint64_t marker, |
| 318 | + uint64_t multi_ops); |
| 319 | + |
| 320 | +/* Actors of compiled programs (runtime/louise_rt/src/process.rs): the root |
| 321 | + * capability for `std/process.proc`, whose clauses are runtime closures. |
| 322 | + * `ops`: the number of operations the program was compiled for; the |
| 323 | + * runtime stops if it was built for another number. */ |
| 324 | +tl_value tl_root_cap(tl_ctx *ctx, int64_t ops); |
| 325 | +tl_value tl_pid_eq(tl_ctx *ctx, tl_value a, tl_value b); |
| 326 | +tl_value tl_coerce(tl_ctx *ctx, tl_value v); |
| 327 | + |
| 328 | +/* Native dependencies (ADR 0005; runtime/louise_rt/src/resource.rs, blocking.rs). |
| 329 | + * A C function bound with `@extern(c, "sym")` has this file's convention: |
| 330 | + * tl_value sym(tl_ctx *ctx, tl_value a, ...); |
| 331 | + * Arguments are borrowed (read them, don't drop them); the result is owned, |
| 332 | + * built with the functions below. */ |
| 333 | +typedef void (*tl_finalizer)(void *ptr); |
| 334 | +tl_value tl_resource_new(tl_ctx *ctx, void *ptr, tl_finalizer finalize, bool thread_safe); |
| 335 | +void *tl_resource_get(tl_value resource); |
| 336 | +tl_value tl_random_int(tl_ctx *ctx, tl_value below); |
| 337 | +tl_value tl_random_token(tl_ctx *ctx, tl_value bytes); |
| 338 | +/* `blocking` externs: generated code calls them through a thunk on the |
| 339 | + * blocking pool while the actor is parked. */ |
| 340 | +typedef tl_value (*tl_thunk)(tl_ctx *ctx, const tl_value *args); |
| 341 | +tl_value tl_call_blocking(tl_ctx *ctx, tl_thunk thunk, size_t argc, const tl_value *args); |
| 342 | + |
| 343 | +/* Callbacks from C (ADR 0015; runtime/louise_rt/src/callback.rs). An extern |
| 344 | + * taking a Louise function `f` passes C a trampoline and a tl_callback as |
| 345 | + * its `void *` environment, for the duration of the call only: |
| 346 | + * |
| 347 | + * tl_callback cb; |
| 348 | + * tl_callback_begin(ctx, &cb, f); |
| 349 | + * lib_sort(xs, n, trampoline, &cb); // calls trampoline(..., &cb) |
| 350 | + * return tl_callback_end(&cb, result); |
| 351 | + * |
| 352 | + * The trampoline converts its C arguments to values, calls |
| 353 | + * tl_callback_call, and converts (then drops) the result. The closure |
| 354 | + * runs in the calling actor, on its thread. If it panicked, |
| 355 | + * tl_callback_failed is true: later calls return TL_UNIT without running |
| 356 | + * it, and the panic continues once the extern returns. Not for |
| 357 | + * `blocking` externs. */ |
| 358 | +typedef struct tl_callback { |
| 359 | + tl_ctx *ctx; /* the actor that called into C */ |
| 360 | + tl_value fn; /* the closure, borrowed from the extern's argument */ |
| 361 | + uintptr_t thread; /* runtime-private */ |
| 362 | +} tl_callback; |
| 363 | +_Static_assert(sizeof(tl_callback) == 24, "tl_callback layout"); |
| 364 | + |
| 365 | +void tl_callback_begin(tl_ctx *ctx, tl_callback *cb, tl_value fn); |
| 366 | +/* Consumes args; returns the closure's result (owned), or TL_UNIT. */ |
| 367 | +tl_value tl_callback_call(const tl_callback *cb, size_t argc, const tl_value *args); |
| 368 | +/* Consumes result; returns it, or TL_UNIT after a panic. */ |
| 369 | +tl_value tl_callback_end(tl_callback *cb, tl_value result); |
| 370 | +static inline bool tl_callback_failed(const tl_callback *cb) { |
| 371 | + return cb->ctx->yielding != TL_YIELD_NONE; |
| 372 | +} |
| 373 | + |
| 374 | +/* Building prelude values from C (constructor order of std/prelude.lse). */ |
| 375 | +static inline tl_value tl_bool(bool b) { return b ? TL_TRUE : TL_FALSE; } |
| 376 | +static inline tl_value tl_wrap1(tl_ctx *ctx, uint16_t tag, tl_value v) { |
| 377 | + tl_block *b = tl_alloc(ctx, tag, 1, 0); |
| 378 | + b->fields[0] = v; |
| 379 | + return tl_from_block(b); |
| 380 | +} |
| 381 | +#define TL_NONE ((tl_value)1) /* None: constructor 0 */ |
| 382 | +static inline tl_value tl_some(tl_ctx *ctx, tl_value v) { return tl_wrap1(ctx, 1, v); } |
| 383 | +static inline tl_value tl_ok(tl_ctx *ctx, tl_value v) { return tl_wrap1(ctx, 0, v); } |
| 384 | +static inline tl_value tl_error(tl_ctx *ctx, tl_value e) { return tl_wrap1(ctx, 1, e); } |
| 385 | +static inline tl_value tl_string_cstr(tl_ctx *ctx, const char *s) { |
| 386 | + return tl_string_from(ctx, s, strlen(s)); |
| 387 | +} |
| 388 | + |
| 389 | +/* Scheduling and actors (runtime/louise_rt/src/sched.rs). Compiled programs |
| 390 | + * use std/process through tl_root_cap; tl_spawn and tl_kill are the M0 |
| 391 | + * spike's (runtime/spike/spike.c). */ |
| 392 | +void tl_preempt(tl_ctx *ctx); |
| 393 | +tl_value tl_spawn(tl_ctx *ctx, tl_value closure); /* consumes closure; returns pid */ |
| 394 | +void tl_kill(tl_ctx *ctx, tl_value pid); /* async exit signal */ |
| 395 | +int64_t tl_actor_count(void); |
| 396 | +int tl_main(tl_value (*entry)(tl_ctx *ctx)); /* runs entry as the root actor */ |
| 397 | +/* Like tl_main for a compiled program: if the root actor panics, print |
| 398 | + * "error: panic: <message>" (as `louise run` does) and return 101. */ |
| 399 | +int tl_main_program(tl_value (*entry)(tl_ctx *ctx)); |
| 400 | + |
| 401 | +/* ------------------------------------------------------------------------ |
| 402 | + * Inline fast paths |
| 403 | + * ---------------------------------------------------------------------- */ |
| 404 | +/* Immortal blocks (rc 0: literals, constant dictionaries) are left alone |
| 405 | + * inline, without a call. */ |
| 406 | +static inline tl_value tl_dup(tl_value v) { |
| 407 | + if (tl_is_ptr(v)) { |
| 408 | + tl_block *b = tl_as_block(v); |
| 409 | + int32_t rc = b->h.rc; |
| 410 | + if (tl_likely(rc > 0)) b->h.rc = rc + 1; |
| 411 | + else if (rc != 0) tl_dup_shared(b); |
| 412 | + } |
| 413 | + return v; |
| 414 | +} |
| 415 | + |
| 416 | +static inline void tl_drop(tl_ctx *ctx, tl_value v) { |
| 417 | + if (tl_is_ptr(v)) { |
| 418 | + tl_block *b = tl_as_block(v); |
| 419 | + int32_t rc = b->h.rc; |
| 420 | + if (tl_likely(rc > 1)) b->h.rc = rc - 1; |
| 421 | + else if (rc != 0) tl_drop_slow(ctx, b); |
| 422 | + } |
| 423 | +} |
| 424 | + |
| 425 | +static inline bool tl_is_unique(tl_value v) { |
| 426 | + return tl_is_ptr(v) && tl_as_block(v)->h.rc == 1; |
| 427 | +} |
| 428 | + |
| 429 | +static inline tl_closure_fn tl_closure_code(tl_value clo) { |
| 430 | + tl_block *b = tl_as_block(clo); |
| 431 | + tl_closure_fn code; /* memcpy: the raw area is not a tl_value (aliasing) */ |
| 432 | + memcpy(&code, &b->fields[b->h.scan_fsize], sizeof code); |
| 433 | + return code; |
| 434 | +} |
| 435 | + |
| 436 | +/* Function prologue and loop back-edge: count a reduction and maybe yield to |
| 437 | + * the scheduler. An exit signal is delivered while yielded, so on resume the |
| 438 | + * code jumps to `on_yield`, which drops the owned locals and returns |
| 439 | + * TL_UNIT. The yielding check is on the slow path only. */ |
| 440 | +#define TL_ENTER(ctx, on_yield) \ |
| 441 | + do { \ |
| 442 | + if (tl_unlikely(--(ctx)->reductions <= 0)) { \ |
| 443 | + tl_preempt(ctx); \ |
| 444 | + if (TL_YIELDING(ctx)) goto on_yield; \ |
| 445 | + } \ |
| 446 | + } while (0) |
| 447 | + |
| 448 | +/* After a call that may yield: true when the caller must drop its owned |
| 449 | + * locals and return immediately (see docs/runtime-abi.md, "Unwinding"). */ |
| 450 | +#define TL_YIELDING(ctx) tl_unlikely((ctx)->yielding != TL_YIELD_NONE) |
| 451 | + |
| 452 | +/* Release a reference the frame holds while it unwinds: a dup in a CLONE |
| 453 | + * pass (see tl_yield_kind). Out of line: unwinding paths are cold, and |
| 454 | + * keeping them small keeps the code around them fast. */ |
| 455 | +void tl_unwind_drop(tl_ctx *ctx, tl_value v); |
| 456 | +#define TL_UNWIND_DROP(ctx, v) tl_unwind_drop((ctx), (v)) |
| 457 | + |
| 458 | +/* Int arithmetic: two small ints without overflow stay inline; anything |
| 459 | + * else (an overflow, a big integer) takes the runtime's slow path, which |
| 460 | + * promotes to a big integer. Never panics. */ |
| 461 | +static inline tl_value tl_int_add(tl_ctx *ctx, tl_value a, tl_value b) { |
| 462 | + int64_t r; |
| 463 | + /* (2x+1) + (2y+1) - 1 == 2(x+y)+1; overflow of the 64-bit sum == 63-bit overflow */ |
| 464 | + if (tl_likely((a & b & 1) && !__builtin_add_overflow((int64_t)a, (int64_t)b - 1, &r))) |
| 465 | + return (tl_value)r; |
| 466 | + return tl_int_add_slow(ctx, a, b); |
| 467 | +} |
| 468 | + |
| 469 | +static inline tl_value tl_int_sub(tl_ctx *ctx, tl_value a, tl_value b) { |
| 470 | + int64_t r; |
| 471 | + if (tl_likely((a & b & 1) && !__builtin_sub_overflow((int64_t)a, (int64_t)b - 1, &r))) |
| 472 | + return (tl_value)r; |
| 473 | + return tl_int_sub_slow(ctx, a, b); |
| 474 | +} |
| 475 | + |
| 476 | +static inline tl_value tl_int_mul(tl_ctx *ctx, tl_value a, tl_value b) { |
| 477 | + int64_t r; |
| 478 | + /* x * (2y) == 2xy, which overflows 64 bits exactly when xy overflows 63; |
| 479 | + * 2xy + 1 then cannot overflow. */ |
| 480 | + if (tl_likely((a & b & 1) && !__builtin_mul_overflow(tl_int_get(a), (int64_t)b - 1, &r))) |
| 481 | + return (tl_value)r + 1; |
| 482 | + return tl_int_mul_slow(ctx, a, b); |
| 483 | +} |
| 484 | + |
| 485 | +/* Truncating division. Small operands cannot overflow 64 bits; only |
| 486 | + * -2^62 / -1 leaves the small range, so a divisor of -1 (and 0, which |
| 487 | + * panics) takes the slow path. */ |
| 488 | +static inline tl_value tl_int_div(tl_ctx *ctx, tl_value a, tl_value b) { |
| 489 | + if (tl_likely((a & b & 1) && b != tl_int(0) && b != tl_int(-1))) |
| 490 | + return tl_int(tl_int_get(a) / tl_int_get(b)); |
| 491 | + return tl_int_div_slow(ctx, a, b); |
| 492 | +} |
| 493 | + |
| 494 | +/* The remainder has the sign of `a`; it is always small. */ |
| 495 | +static inline tl_value tl_int_rem(tl_ctx *ctx, tl_value a, tl_value b) { |
| 496 | + if (tl_likely((a & b & 1) && b != tl_int(0))) |
| 497 | + return tl_int(tl_int_get(a) % tl_int_get(b)); |
| 498 | + return tl_int_rem_slow(ctx, a, b); |
| 499 | +} |
| 500 | + |
| 501 | +/* Canonical forms: a small int equals only the same word. */ |
| 502 | +static inline tl_value tl_int_eq(tl_ctx *ctx, tl_value a, tl_value b) { |
| 503 | + if (tl_likely(a == b || ((a | b) & 1))) return tl_bool(a == b); |
| 504 | + return tl_int_eq_slow(ctx, a, b); |
| 505 | +} |
| 506 | + |
| 507 | +/* `Order`: Lt, Eq, Gt are 0, 1, 2. Tagging keeps the order of small ints. */ |
| 508 | +static inline tl_value tl_int_compare(tl_ctx *ctx, tl_value a, tl_value b) { |
| 509 | + if (tl_likely(a & b & 1)) |
| 510 | + return tl_int(((int64_t)a > (int64_t)b) - ((int64_t)a < (int64_t)b) + 1); |
| 511 | + return tl_int_compare_slow(ctx, a, b); |
| 512 | +} |
| 513 | + |
| 514 | +#ifdef __cplusplus |
| 515 | +} |
| 516 | +#endif |
| 517 | + |
| 518 | +#endif /* LOUISE_RT_H */ |