Open DigitalBrains1 opened 3 years ago
So the tricky bit is that the derived Enum
instance uses tagToEnum#
as the implementation for toEnum
. Before we had custom data representation we could simply translate tagToEnum#
as a bit-slice as we used a binary encoding and because GHC assumes 0-based index for the "tag" corresponding to a constructor. Bit-slicing can simply be an expression (as opposed to a statement) in our supported HDLs, so the internals of Clash are already committed to translating tagToEnum#
to an expression in HDL.
However, with custom bit-encodings, in the general case, tagToEnum#
must be translated to a statement expressing an n-ary choice. I guess the way forward is to have BlackBoxHaskell
primitive entry for tagToEnum#
that can decide between creating a declaration (statement) BlackBox for types with a custom bit-encoding, or creating an expression BlackBox for types without a custom bit-encoding.
The following code makes Clash crash:
(an alternative avoiding
Int
withUnsigned 2
andtoEnum . fromIntegral
also does not work)clash --vhdl
crashes with: