google / emboss

Emboss is a tool for generating code that reads and writes binary data structures.
Apache License 2.0
70 stars 21 forks source link

Support cross-type integral equality #23

Open fsareshwala opened 2 years ago

fsareshwala commented 2 years ago

In the text below, we consider the following Emboss definition:

enum OpCodeGroup:
  # ...
  FOO = 0x08
  # ...

enum OpCodeCommand:
  # ...
  BAR = 0x0039
  # ...

bits OpCode(group: UInt:6, command: UInt:10):
  0 [+6] UInt ogf
  6 [+10] UInt ocf

We would like to pass constant values to the group and command parameters of the OpCode type. We could store these values in virtual fields within the struct we are composing using the let keyword. However, these values are better placed inside the OpCodeGroup and OpCodeCommand enumerations, respectively. Doing so allows the values to be defined once across the entire codebase and made accessible within both Emboss and C++ code. The following situations arise here:

  1. If we use the UInt:6 and UInt:10 types for parameters, the arguments must be hardcoded, and we no longer have a single definition of the constants across the codebase (they have to be defined elsewhere again)
  2. If we use the enumeration types for both parameters and fields, Emboss generates an EnumView which doesn’t allow writes, making ogf and ocf read only
  3. If we use the enumeration types for the parameters and UInt types for the fields, adding a check like [requires: ogf == group && ocf == command] won’t compile because the types are different

For obvious reasons, we don’t want to use option 1 to hardcode constants. Enumerations shouldn’t be writable so option 2 makes sense as it is. In order to support this use case, Emboss should support cross-type integral equality as implied in option 2.

Potential Implementation The .emb language syntax is updated to allow explicit int(Foo.Bar) conversions to allow for integral type coercion. We want to keep Emboss' strict type safety but want to be able to compare across only integral types.

EricRahm commented 3 days ago

I ran into a similar use case where I wanted to use an enum as an integral constant. A slightly reduced example:

enum FrameType:
  -- RFCOMM Frame Type
  [maximum_bits: 8]
 UIH  = 0xEF

bits Control:
  0 [+8] UInt control_byte
  0 [+4] UInt frame_type_1
  4 [+1] UInt p_f
  5 [+3] UInt frame_type_2

  # Note: emboss doesn't provide a way to cast an enum to an integral constant
  #       so we hardcode a value instead, also bit shift doesn't work but that's solvable
  #       with multiplication.
  # control_byte == (FrameType.UIH | (1 << 4))
  # or
  # let flow_control_enabled = FrameType.UIH + 16
  # control_byte == flow_control_enabled
  let flow_control = control_byte == 0xFF