Skip to content

Design Rationale

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).

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.”

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.

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

The two-file split separates concerns:

  • .kdh is shareable - describes a hardware class, reused by many drivers
  • .kdc is the implementation - one per driver, imports .kdh files
  • Mirrors the C .h / .c split with semantic difference explicit in the extension

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.

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.

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
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)
.kdc → Lexer → Tokens → Parser → AST → Sema → Codegen → .c + Makefile.kbuild

Each 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.

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

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:

  • async event handlers (work-queue dispatch)
  • debugfs generated from register declarations
  • KUnit test generation from .kdh type information (kdality test-gen generates stubs from a .kdc today)
  • a Rust code generator