| @@ -7,6 +7,7 @@ |
| 7 | 7 | - Its Revision 1 made the extern convention `tl_value sym(tl_ctx *ctx, tl_value a, ...)`, with a few lines of C per library (the *shim*) converting values. Callbacks fit the same model: the shim gives C a *trampoline*, a C function of the library's callback type, which converts its arguments and calls the tilcayo function. |
| 8 | 8 | - Generated code runs on an actor's fiber. That fiber may suspend (preemption, `receive`, sockets, `blocking` externs), and may resume on another scheduler thread. |
| 9 | 9 | - Panics, exit signals and `final ctl` operations unwind by yield-bubbling ([ADR 0003](0003-effect-lowering.md) Revision 2): each frame checks `ctx->yielding` after a call and returns. C frames do no such check. |
| 10 | +- ADR 0005 deferred header-driven binding generation. Shims are mechanical for simple signatures, and hand-written ones are where conversion bugs hide. |
| 10 | 11 | |
| 11 | 12 | ## Decision |
| 12 | 13 | |
| @@ -80,6 +81,26 @@ These abort with a message. They catch shim bugs, not user errors: |
| 80 | 81 | - parking inside a callback; |
| 81 | 82 | - more than 31 callback arguments (five registers and the 26 `xargs` slots). |
| 82 | 83 | |
| 84 | +### Binding generation: `tilc bindgen` |
| 85 | +`tilc bindgen header.h --til src/lib.til --c csrc/lib_bindings.c` writes a module of `@extern` declarations and the shim they call, for the functions of the header whose types are in a declared subset (the `tilc_bindgen` crate's documentation has the table): |
| 86 | +- **Types:** |
| 87 | + - integers become `Int`, floating-point types `Float`, `bool` `Bool`; |
| 88 | + - `const char *` becomes `String`, and a `const char *` followed by an integer named `n`, `*len`, `*length` or `*size` becomes one `String` passed as pointer and length; |
| 89 | + - a pointer to an opaque struct (declared but not defined in the header) becomes `Resource<T>`, with `pub type T {}` declared; |
| 90 | + - a function pointer with one `void *` parameter, together with one `void *` parameter of the function, becomes a callback (above); |
| 91 | + - everything else (macros, enums, structs with fields or by value, writable `char *`, other pointers, arrays, variadic functions, `static inline` functions, variables) is skipped. Each skipped declaration is printed with its line and the reason, and listed at the top of the generated module. |
| 92 | +- **Conversions are checked.** An `Int` that does not fit the C integer type, a C result that does not fit `Int`, or a `NULL` string or resource result panics in the shim. That is safe because every extern may unwind (above). A trampoline whose argument or result does not fit panics the same way, through `ctx`. |
| 93 | +- **Ownership of resources is decided by naming.** |
| 94 | + - A pointer *result* becomes an owned `Resource` only from a function named like a constructor (`_new`, `_create`, `_open`, `_copy`, `_clone`); any other function returning a pointer is skipped, because it may return a pointer it still owns. |
| 95 | + - The finalizer is the one `void f(T *)` whose name ends in `_free`, `_destroy`, `_delete` or `_release`. It is not bound, since calling it would free the value twice. Without one, the type is reported: its resources are never freed. |
| 96 | + - Resources are created not thread-safe (pinned to their actor): bindgen cannot know better. |
| 97 | +- **Every binding performs `<ffi>`**, and none is `blocking`: bindgen cannot know that a C function is pure or slow. A library that needs either keeps a hand-written shim for those functions. |
| 98 | +- **Names:** a function keeps its C name when it is a valid tilcayo name (lowercase, not a keyword), and is skipped otherwise; `struct sk_vec` and `sk_vec_t` become `SkVec`. Shim functions are prefixed `tilcayo_`, so they collide neither with the library nor with the runtime (`tl_`). |
| 99 | +- **The parser is small and does not preprocess.** It removes comments and preprocessor lines, splits the header into top-level declarations and recognises a few shapes (`crates/tilc_bindgen/src/parse.rs`). A function declared through a macro (`API int f(void);`) is reported, not guessed. |
| 100 | +- **Generated files are checked in**, and a test regenerates them and compares, so the bindings, the header and the generator cannot drift apart silently. |
| 101 | + |
| 102 | +**The example (issue #48's exit criterion).** `examples/qsort` has two packages, as Rust's `-sys` crates do: `sortkit/` holds the C library (`vendor/`, written for the example and laid out like a vendored library) and its generated bindings as its `src/lib.til`; `qsort` wraps them in a tilcayo API and tests it, including a comparator that panics. `asan/` is a program with a deliberate off-by-one, whose AddressSanitizer report points at its `.til` lines (through the `#line` directives of issue #35). The split is needed today: a dependency's modules are loaded under its name (`qsort/sortkit`), but its own modules import each other without it (`sortkit`), so only a package's `lib.til` can be imported by the same name from inside and outside it. |
| 103 | + |
| 83 | 104 | ## Consequences |
| 84 | 105 | - **Binding a callback API** takes one function-typed parameter and a trampoline in the shim. The trampoline converts types, so it is per C signature; `tilc bindgen` generates it for simple signatures. |
| 85 | 106 | - **Cost:** one predictable branch after each call to an extern of a binding, and the drops of its unwinding path (code size). The runtime's own externs are unchanged. |
| @@ -90,6 +111,9 @@ These abort with a message. They catch shim bugs, not user errors: |
| 90 | 111 | - callbacks stored by C and called later (event loops, signal handlers). They would need a closure kept alive past the call and a message to the actor; |
| 91 | 112 | - callbacks called from C's own threads; |
| 92 | 113 | - the interpreter, which cannot call C at all (as for every native dependency): these tests are native. |
| 114 | +- **Generated bindings are a starting point for simple APIs.** The naming rules for ownership can be wrong for a given library (a `_copy` that returns a borrowed pointer); such functions need a hand-written shim. There is no way yet to override a rule from the command line. |
| 115 | +- **Generated C has `-Wextra` warnings** (`-Wtype-limits`, from the range checks on unsigned types); it compiles cleanly with `-Wall`. |
| 116 | +- **Native dependencies are compiled without `-g`**, so sanitizer stacks show the library's frames without lines; the `.til` frames have theirs. |
| 93 | 117 | |
| 94 | 118 | ## Alternatives considered |
| 95 | 119 | - **A trampoline generated by the compiler** from a C signature written in the attribute. This would put C types in the compiler next to one uniform convention; the shim (written or generated by `tilc bindgen`) keeps C types out. |
| @@ -97,3 +121,6 @@ These abort with a message. They catch shim bugs, not user errors: |
| 97 | 121 | - **Allowing any effect, and suspending inside callbacks.** C frames would move between threads with the fiber. This breaks C code using thread-locals or holding locks, and lets another actor re-enter the same library on the same thread. |
| 98 | 122 | - **Unwinding through C** with `longjmp` or forced unwinding. C frames would release nothing (locks, allocations), and Rust forbids unwinding across `extern "C"` frames. |
| 99 | 123 | - **Aborting the process** on a panic in a callback. It is simpler, but it turns one actor's error into a crash of all of them. |
| 124 | +- **libclang** (through `clang-sys`, or Rust's `bindgen` crate) parses any header exactly, macros included. It needs libclang installed where `tilc` is built or run, and ties `tilc` to its versions, for a subset that a small parser covers. It remains the way forward if headers outgrow that subset; the generator would keep its output. |
| 125 | +- **Running the preprocessor (`cc -E`) and a full C parser.** The expanded header includes every system header, and the declarations of interest are then hard to tell from those of libc. |
| 126 | +- **Bindings generated at build time**, not checked in. Builds would need the header and the generator's exact behaviour; checked-in files are reviewable, and the comparison test catches drift. |