Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make illegal registers unrepresentable #58

Open
langston-barrett opened this issue Sep 25, 2024 · 0 comments
Open

Make illegal registers unrepresentable #58

langston-barrett opened this issue Sep 25, 2024 · 0 comments

Comments

@langston-barrett
Copy link
Contributor

Flexdis defines a number of register types that have more representations than they have valid values. Consider Reg64:

-- | One of the 16 64-bit general purpose registers.
newtype Reg64 = Reg64 { unReg64 :: Word8 }

As noted in the Haddock and enforced via assert, the only valid values of this type are Reg64 0, ..., Reg64 15. However, there are 241 additional, invalid values! To make matters worse, the constructor is exported, meaning that Flexdis cannot guarantee that clients don't construct invalid values.

The set of registers is small and fixed. This is the perfect use-case for an enumeration type! It would be nice to promote all of the patterns in that module to be the constructors of an enumeration. We could then exhaustively pattern-match on these types, and implement classes such as Enum and Bounded.

There are two downsides to this proposal:

  1. It's not backwards-compatible. Some client code could break.
  2. Functions like reg32_reg would be slightly more complex. Today they are defined simply like so:
reg32_reg :: Reg32 -> Reg64
reg32_reg (Reg32 r) = Reg64 r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant