Device tree compiler and inspector in pure MoonBit: DTS to DTB, DTB back to DTS and overlays, byte-identical to dtc, with no dtc binary present.
| Capability | Status |
|---|---|
| 40-byte blob header, version 17 | done |
| Block tiling and alignment validation | done |
| Bounds-checked primitive reads | done |
| Struct block walk (nodes, properties) | done |
| Strings block lookup | done |
| Nesting limit | done |
| DTS lexer: identifiers, strings, numbers, comments, cpp markers | done |
| Expressions ( ... ): full C operator set and precedence | done |
| /include/ with cycle detection and relative paths | done |
| /bits/ 8, /bits/ 16, /bits/ 64 | done |
| /delete-node/, /delete-property/ | done |
| /omit-if-no-ref/ | done |
| Top-level &label { } patches, multiple root blocks | done |
| DTS parser: nodes, properties, typed values | done |
| Labels, &label and &{/path} references | done |
| /bits/, /memreserve/, /plugin/ | done |
| DTB writer | done |
| Reference resolution, phandle numbering | done |
| Overlay generation, __fixups__ | done |
| Overlay application, merge semantics | done |
| CLI: dump, decompile, compile, plugin compile, apply | done |
| Source emitter, byte-identical to dtc's dump | done |
| Browser demo (JavaScript backend) | done |
| DTS emitter (tree back to readable source) | done |
| C preprocessor and macros (#include, #define) | out of scope |
| /incbin/ | out of scope |
| Fixture | Size | Nodes | What it covers |
|---|---|---|---|
| minimal | 357 B | 3 | flat tree, strings and cells |
| nested | 1 086 B | 9 | CPU subtree, phandle, multi-line reg, empty ranges |
| overlay-base | 657 B | 5 | base tree built with -@: __symbols__, a phandle |
| overlay-labels | 403 B | 4 | three labels on one node, label order on the way back out |
| overlay-uart | 381 B | 4 | /plugin/ patches, __fixups__ |
| realview-pb1176 | 9 983 B | 69 | ARM board from the Linux tree: aliases, dma-ranges, 14 phandles |
| Part | Lines |
|---|---|
| Library | 5 625 |
| Tests (*_wbtest.mbt) | 2 099 |
| CLI and browser entry points | 385 |
| Operation | Time |
|---|---|
| Parse and render a 9 983-byte blob (69 nodes) | 0.24 ms |
| Throughput | ~4 300 parses/s |
| Compile the same source from the CLI, process start included | 20–60 ms |
moon testmoon run cmd/main -- internal/fixtures/nested.dts -o /tmp/out.dtb
dtc -q -I dts -O dtb -o /tmp/dtc.dtb internal/fixtures/nested.dts
cmp /tmp/out.dtb /tmp/dtc.dtbmoon run cmd/main -- /tmp/out.dtb -D > /tmp/round.dts
dtc -q -I dtb -O dts -o /tmp/dtc.dts /tmp/out.dtb
diff /tmp/round.dts /tmp/dtc.dts| File | Contents |
|---|---|
| dtbscope.mbt | Error type, cursor, hex formatting |
| reader.mbt | Bounds-checked reads of the primitive types |
| header.mbt | Blob header parse and validation |
| struct_block.mbt | Token walk, node tree, structural warnings |
| dts_token.mbt | Token kinds, spans, scanner state |
| dts_lex.mbt | Character rules and token scanning |
| dts_parse.mbt | Source grammar, value parsing, source tree |
| dts_emit.mbt | Tree back to source, byte-identical to dtc's dump |
| dump.mbt | Flat tree reading, shared by the CLI and the browser module |
| phandle.mbt | Label table, phandle numbering, reference rewrite |
| dtb_write.mbt | Blob encoder |
| cmd/main/ | Command line front end |
| overlay.mbt | Overlay generation, fixups, application |
| wasm/ | JavaScript entry points for the browser demo |
| web/ | Demo page, built into web/dist by tools/build-web.sh |
| tools/gen-fixtures.sh | Fixture regeneration |
| internal/fixtures/ | DTS sources and their compiled DTBs |
# Compile a plugin source to a .dtbo
moon run cmd/main -- board-overlay.dts --plugin -o board.dtbo
# Apply it to a base blob and write the merged result
moon run cmd/main -- board.dtbo -b board.dtb -o merged.dtb./tools/build-web.sh
python3 -m http.server -d web/dist 8000| Feature | Behaviour |
|---|---|
| ( expr ) | C operators and precedence, evaluated at parse time |
| /include/ "file" | spliced in, relative to the including file, cycles refused |
| /delete-node/ &label; | removes a node; top level only, which is dtc's rule |
| /delete-property/ name; | removes a property inside a patch body |
| /omit-if-no-ref/ | drops the node when nothing refers to it |
| &label { } after the root | patches the tree just read |
| several / { }; blocks | merged into one root, which is what an include produces |
pub(all) suberror DtbError {
UnexpectedEnd(offset~ : Int64, needed~ : Int64, available~ : Int64)
BadMagic(magic~ : UInt)
BadValue(field~ : String, value~ : Int64)
OutOfBounds(what~ : String, offset~ : Int64, size~ : Int64, limit~ : Int64)
BadName(offset~ : Int64)
Inconsistent(what~ : String, detail~ : String)
LimitExceeded(what~ : String, limit~ : Int64)
Unsupported(what~ : String)
}pub suberror ExprError {
DivisionByZero
BadShift(amount~ : Int64)
Overflow(what~ : String)
}pub suberror IncludeError {
Unreadable(path~ : String, detail~ : String)
Cycle(path~ : String, chain~ : Array[String])
Misplaced(line~ : Int)
TooDeep(limit~ : Int)
}impl Show for IncludeErrorimpl Show for LexerErrorpub(all) suberror OverlayError {
UnknownSymbol(label~ : String)
MissingTarget(path~ : String)
BadFixup(detail~ : String)
NoSymbols
BadFragment(name~ : String)
Resolve(inner~ : ResolveError)
}impl Show for OverlayErrorpub(all) suberror ParseError {
Unexpected(token~ : TokenKind, expected~ : String, span~ : Span)
Lexical(inner~ : LexerError)
BadValue(detail~ : String, span~ : Span)
LimitExceeded(what~ : String, limit~ : Int)
Unsupported(what~ : String, span~ : Span)
}impl Show for ParseErrorpub(all) suberror ResolveError {
UnknownLabel(label~ : String)
UnknownPath(path~ : String)
DuplicateLabel(label~ : String, first~ : String, second~ : String)
DuplicatePhandle(value~ : UInt, first~ : String, second~ : String)
BadPhandle(value~ : UInt, node~ : String)
}impl Show for ResolveErrorpub(all) enum BinOp {
Or
Xor
And
Eq
Ne
Lt
Le
Gt
Ge
Shl
Shr
Add
Sub
Mul
Div
Mod
}pub(all) struct Bounds {
bytes : Bytes
start : Int64
limit : Int64
}pub(all) enum Endian {
Little
Big
}pub(all) struct FixupLocation {
path : String
property : String
offset : Int
}pub(all) struct Header {
totalsize : Int64
off_dt_struct : Int64
off_dt_strings : Int64
off_mem_rsvmap : Int64
version : UInt
last_comp_version : UInt
boot_cpuid_phys : UInt
size_dt_strings : Int64
size_dt_struct : Int64
}pub(all) struct Lexer {
source : String
pos : Int
line : Int
column : Int
in_bytes : Bool
}pub(all) struct Property {
name : String
value : Bytes
offset : Int64
}pub(all) enum RelOp {
Less
Greater
LessEqual
GreaterEqual
EqualEqual
NotEqual
ShiftLeft
ShiftRight
}pub(all) struct SourceFile {
root : SourceNode
memreserves : Array[(UInt64, UInt64)]
is_plugin : Bool
fragments : Array[Fragment]
}pub(all) struct SourceNode {
name : String
labels : Array[String]
properties : Array[SourceProperty]
children : Array[SourceNode]
deletes : Array[String]
is_delete : Bool
omit_if_no_ref : Bool
}pub(all) struct Span {
line : Int
column : Int
}pub(all) enum StructTag {
BeginNode
EndNode
Prop
Nop
End
}pub(all) enum TokenKind {
Ident(String)
String(String)
Number(UInt64)
LBrace
RBrace
LAngle
RAngle
LBracket
RBracket
LParen
RParen
Comma
Semicolon
Equals
Slash
Amp
Colon
Minus
Plus
Star
Percent
Tilde
Bang
Caret
Pipe
Question
RelOp(RelOp)
HexByte(Byte)
Eof
}pub(all) enum Warning {
MissingEnd(offset~ : Int64)
TrailingData(offset~ : Int64, remaining~ : Int64)
}fn expand_includes(text : String, origin : String, read : (String) -> Result[String, String]) -> String raise IncludeErrorfn mask_to_width(value : Int64, width : UInt64) -> UInt64Install
Download zipDevice tree compiler and inspector in pure MoonBit: DTS to DTB, DTB back to DTS and overlays, byte-identical to dtc, with no dtc binary present.