$ show multikernel/kernelscript
KernelScript is a domain-specific language and compiler (OCaml) for developing eBPF-centric programs. The compiler translates a single `.ks` source file into multiple C outputs: idiomatic eBPF C code (for kernel execution via XDP, TC, kprobe, tracepoint, perf_event, struct_ops), userspace loader C code (managing maps, ring buffers, tail calls, and program lifetime), and optionally kernel module C code (for `@kfunc` functions). The compiler pipeline includes lexing, parsing, symbol table construction, type checking (including context-specific rules for eBPF program types), IR generation with control-flow and data-flow analysis, loop analysis (bound detection, bpf_loop optimization), tail-call detection and automatic progarry management, map assignment optimization, safety checking (null pointer, bounds, overflow), and multi-program optimization (map type changes, batching). It also supports BTF introspection for kernel type and kfunc extraction, a Python bridge, and a compile-time evaluator. The architecture is modular with pluggable context code generators per program type. The project includes an extensive test suite covering the full pipeline from parse to C output and specific features like perf_event attach, ring buffer dispatch, struct_ops, and multi-program scenario.
The LLM identified 3 files worth highlighting for reuse:
Provides a registry for context-specific C code generators (XDP, TC, kprobe, tracepoint, perf_event). Each context registers its own field mappings, include lists, and action constants. This design allows new eBPF program types to be added without modifying core codegen, making it a reusable pattern for extensible compiler backends. The file is self-contained and defines a clear interface.
Self-contained compile-time expression evaluator for KernelScript. Supports integer, string, char, bool, and array values with a hashtable-based memory. Handles builtins (bpf_trace_printk, map operations), let binding, if, match, and function calls (limited). Could be extracted and adapted for other compile-time evaluation tasks in DSL tools. Demonstrates a clean recursive evaluator pattern with error handling and environment management.
Analizes for loops to determine if they are bounded, unbounded, or simple, and selects an appropriate eBPF loop strategy (bpf_loop helper, unrolling, or simple check). The analysis is context-sensitive (uses constant propagation) and produces a status that can be reused in any backend that needs to enforce loop termination. It is reasonably isolated and the strategy enum and analysis logic could be applied to other verification or compilation projects.