amaranth icon indicating copy to clipboard operation
amaranth copied to clipboard

Add an `amaranth.hdl._ast.Match` node, use it for `Value.matches`, and delegate from `Case()` to `Value.matches`

Open whitequark opened this issue 1 year ago • 0 comments

We currently have several warts in the very core of the language: the way .matches() works (by masking and comparing) which is different from the way with m.Case(): works (by emitting decision trees), and which duplicate most of their logic, which sometimes gets out of sync. Also, Case() emits Switch() statements, which are undocumented and extremely fragile.

Instead of this mess, we should emit a new (internal) Match AST node from Value.matches(), replace Switch with an (internal) decision tree statement Decision that will also absorb StatementList, and have Switch lower to a simple priority mux where if the condition is one or more Matches nodes, then the masking is absorbed into the RTLIL decision tree, and otherwise it's treated as a single bit pattern.

The Decision statement should conceptually contain a list of pairs (condition, statements), which are evaluated in order. The condition is an arbitrary expression that is reduced to a boolean. The first branch that evaluates to true is the one that becomes active.

whitequark avatar Jan 31 '24 19:01 whitequark