pervognsen / bitwise

Bitwise is an educational project where we create the software/hardware stack for a computer from scratch.
Other
5.14k stars 212 forks source link

Add enum typeinfos #73

Open uucidl opened 6 years ago

uucidl commented 6 years ago

Merge branch 'add-enum-typeinfos'

This introduces type information for enum types.

The ion program can now know that a type is an enum, and what is its base type and size as well as the values of available constants in the enum (see enum_items and num_enum_items)

This allows user programs to print the friendly enumeration constant associated with a value, or validate that a value is a legal one.

The name of the constant is the external C name, i.e. as mangled into the C symbols, similarly to how type names are also introspected. This guarantees unicity, however means that a ion program looking to generate new ion programs would have to reverse the manging somehow. The same issue is true for existing type names however.

Note that this creates a distinction between enum constants and plain constants: enum constants are known to the program and can be shared outside. This matches my experience of using enumerations in applications.

Initially I wanted to supply sorted arrays for an enumeration type by ame and value, to make lookups more efficient. However to do this at compile time would necessitate the ion compiler to evaluate the enum expression. Right now the value of the expression is only known to the ion program (after the C compiler has done its job)

So fast lookups (sub-O(n)) for large enumerations will have to be done by hand in the ion program using raw type infos for now.

uucidl commented 6 years ago

@pervognsen this PR is more of an example of how this could be done for followers of this repository. I'm also curious how you would export this information compared to this solution.

I can create this as an issue instead, maybe it fits the question format better. Let me know.