Search code examples
chisel

What mechanism works to show component ID in chisel3 elaboration


Chisel throws an exception with an elaboration error message. The following is a result of my code as an example.

chisel3.core.Binding$ExpectedHardwareException: data to be connected 'chisel3.core.Bool@81' must be hardware, not a bare Chisel type. Perhaps you forgot to wrap it in Wire(_) or IO(_)?

This exception message is interesting because 81 behind chisel3.core.Bool@ looks like ID, not hashcode. Indeed, Data type extends HasId trait which has _id field, and _id field seems to generate a unique ID for each components.

I've thought Data type overrides toString to make string that has type@ID, but it does not override. That is why $node in below code should not be able to use ID.

throw Binding.ExpectedHardwareException(s"$prefix'$node' must be hardware, " +
            "not a bare Chisel type. Perhaps you forgot to wrap it in Wire(_) or IO(_)?")

Instead of toString, toNamed method exists in Data. However, this method seems to be called to generate a firrtl code, not to convert component into string.

Why can Data type show its ID?

If it is not ID, but exactly hashcode, this question is from my misunderstanding.


Solution

  • Scala classes come with a default toString method that is of the form className@hashCode.

    As you noted, the chisel3.core.Bool@81 sure looks like it's using the _id rather than the hashCode. That's because in the most recently published version of Chisel (3.1.6), the hashcode was the id! You can see this if you inspect the source files at the tag for that version: https://github.com/freechipsproject/chisel3/blob/dc4200f8b622e637ec170dc0728c7887a7dbc566/chiselFrontend/src/main/scala/chisel3/internal/Builder.scala#L81

    This is no longer the case on master which probably the source of any confusion! As Chick noted, we have just changed the .toString method to be more informative than the default; expect more informative representations in 3.2.0!