Design Rationale
Motivation
Section titled “Motivation”Every major embedded framework eventually invents its own file format:
| Framework | File format | Compile target |
|---|---|---|
| Arduino | .ino |
AVR / ARM bare-metal |
| PlatformIO | .ino / .cpp with platform env |
MCU bare-metal |
| Zephyr RTOS | .c + .overlay (DTS) |
RTOS firmware image |
| Linux kernel | .c + Kbuild Makefile |
Kernel module .ko |
| KDAL | .kdc + .kdh |
Linux kernel module .ko |
The Linux kernel has no domain-specific abstraction layer for writing device drivers that:
- Separates what a device looks like (register map, signals) from how the driver behaves (event handlers)
- Enforces safe register-access rules before the module is built
- Allows a single source file to target multiple backends (MMIO, platform_device, virtio, DT)
- Is auditable by a reviewer who does not know C
KDAL was designed to fill this gap. At 5eff1ff: register access is
checked by kdality lint (W003, W004) rather than by the compiler for an
imported header; the code generator emits the same platform_driver
module whatever the backend; and the generated module does not build yet
(open finding O7).
Language Philosophy
Section titled “Language Philosophy”Declarative at the top, imperative at the edges
Section titled “Declarative at the top, imperative at the edges”A .kdh file is purely declarative: it describes hardware registers, signal
lines, capabilities, and power states. It contains no executable logic.
A .kdc driver is mostly declarative (annotations and config) but contains
imperative event handlers where real work happens. This mirrors how hardware
engineers think: “the device has these registers; when this event occurs, do
this sequence.”
Intentionally not Turing-complete
Section titled “Intentionally not Turing-complete”KDAL rejects:
| Construct | Reason |
|---|---|
| Recursion | Call depth bounded at 1 (handler calls built-ins only) |
| Heap allocation | All state lives in a C struct allocated once by probe |
| Unbounded loops | Only for i in 0..N, meant for a compile-time constant N (the frozen compiler warns when it cannot tell) |
| Pointer arithmetic | Register access goes through typed paths |
| Inline assembly | No escape hatch to machine code |
The aim: generated code that can be audited, and whole classes of kernel bugs (memory corruption, double-free, stack overflow) that cannot be written in KDAL.
Written in C
Section titled “Written in C”The KDAL compiler (kdalc) is written in C. This is intentional:
- The compiler must run in any kernel build environment (which guarantees a C compiler)
- C is the language of the kernel - no bootstrap dependency
- Generated output is idiomatic C that a Linux kernel reviewer can read without knowing KDAL
File Format Decisions
Section titled “File Format Decisions”Why .kdh and .kdc (not .kdal)?
Section titled “Why .kdh and .kdc (not .kdal)?”The two-file split separates concerns:
.kdhis shareable - describes a hardware class, reused by many drivers.kdcis the implementation - one per driver, imports.kdhfiles- Mirrors the C
.h/.csplit with semantic difference explicit in the extension
Why no while loop?
Section titled “Why no while loop?”while loops can spin indefinitely in kernel context (IRQ or softirq),
causing system hangs. for i in 0..N guarantees termination at a glance.
The wait(signal, ms) built-in replaces while (!ready) {}; the generated
C calls the runtime’s kdal_wait_signal, one of the functions O7 found
missing.
Why is log a built-in?
Section titled “Why is log a built-in?”pr_debug and dev_dbg require a struct device * pointer. The compiler
injects the correct pointer from the driver context struct, preventing the
common bug of calling pr_err without a device context.
Why not Rust?
Section titled “Why not Rust?”Rust is a valid kernel language (Linux 6.1+). The decision to target C for KDAL 0.x is pragmatic:
- Rust in-kernel toolchains are still stabilising (bindgen, macros)
- The KDAL runtime is C - C codegen avoids cross-language FFI
- Rust-as-target is listed as future work
Comparison with Alternatives
Section titled “Comparison with Alternatives”| Feature | C + Kbuild | Arduino | Zephyr RTOS | KDAL |
|---|---|---|---|---|
| Target | Linux .ko |
Bare-metal | RTOS firmware image | Linux .ko (not yet built: O7) |
| Register access | Manual | Manual | Manual | ro/wo checked by kdality lint |
| Backend abstraction | Manual | None | DTS + Kconfig | backend declaration (not yet used by the generator) |
| Signal/IRQ model | Manual | attachInterrupt |
gpio_callback |
on signal handler |
| Power management | PM framework | None | pm_device |
on power handler |
| Turing-complete | Yes | Yes | Yes | No (by design) |
Compiler Architecture
Section titled “Compiler Architecture”.kdc → Lexer → Tokens → Parser → AST → Sema → Codegen → .c + Makefile.kbuildEach phase is a separate .c file. Phases communicate via the kdal_ast_t
struct hierarchy in compiler/include/ast.h. See the
Compiler page for full details.
Standard Library
Section titled “Standard Library”7 pre-built .kdh files in lang/stdlib/:
| File | Device class | Notable registers |
|---|---|---|
common.kdh |
KdalCommon (shared definitions) |
- |
uart.kdh |
UartDevice (PL011 layout) |
DR, FR, IBRD, FBRD, LCRH, CR, IMSC |
i2c.kdh |
I2cController (DesignWare) |
CON, TAR, DATA_CMD, SS/FS_SCL_HCNT/LCNT |
spi.kdh |
SpiController (PL022) |
CR0, CR1, DR, SR, CPSR |
gpio.kdh |
GpioBank (PL061 style) |
DATA, DIR, IS, IBE, IEV, IE |
gpu.kdh |
GpuDevice (abstract) |
CMD_SUBMIT, STATUS, RESET, FENCE_INSERT, FENCE_DONE |
npu.kdh |
NpuDevice (abstract) |
CTRL, STATUS, MODEL_ADDR_LO/HI, MODEL_SIZE |
Future Work
Section titled “Future Work”Planned before the freeze; with the language frozen since 2026-11-01 none of these is scheduled, and KDAL’s own version numbers (0.2.0 now) no longer follow this list:
asyncevent handlers (work-queue dispatch)- debugfs generated from register declarations
- KUnit test generation from
.kdhtype information (kdality test-gengenerates stubs from a.kdctoday) - a Rust code generator