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

Bitfield constants #1511

Merged
merged 4 commits into from
Sep 7, 2023
Merged

Bitfield constants #1511

merged 4 commits into from
Sep 7, 2023

Commits on Sep 7, 2023

  1. Change internal tuple representation of bitfield to store optional va…

    …lues.
    
    This will allow us to create bitfield constants where not at all
    elements are set. We also add an operator to test if an element is
    set (test forthcoming in a subsequent commit).
    rsmmr committed Sep 7, 2023
    Configuration menu
    Copy the full SHA
    fabc427 View commit details
    Browse the repository at this point in the history
  2. Add bitfield constants.

    It's now possible to initialize a bitfield value through an assignment
    from a struct constructor expression:
    
        type BF = bitfield(8) {
          a: 0..3;
          b: 4..7;
          c: 4..5;
        };
    
        global BF bf = [$a = 1, $c = 2];
    rsmmr committed Sep 7, 2023
    Configuration menu
    Copy the full SHA
    8a65ebd View commit details
    Browse the repository at this point in the history
  3. Extend bitfield type with per-item storage for constant values.

    This allows to associate an expression with each bit value. We don't
    use this from HILTI because there's isn't a good syntax to do so
    (and/or: it's not worth adding), but we'll use (and test) this from
    Spicy in a subsequent commit.
    rsmmr committed Sep 7, 2023
    Configuration menu
    Copy the full SHA
    d440dee View commit details
    Browse the repository at this point in the history
  4. Support bitfield constants in Spicy for parsing.

    One can now define bitfield "constants" for parsing by providing
    integer expressions with fields:
    
        type Foo = unit {
          x: bitfield(8) {
            a: 0..3 = 2;
            b: 4..7;
            c: 7 = 1;
          };
    
    This will first parse the bitfield as usual and then enforce that the
    two bit ranges that are coming with expressions (i.e., `a` and `c`)
    indeed containing the expected values. If they don't, that's a parse
    error.
    
    We also support using such bitfield constants for look-ahead parsing:
    
        type Foo = unit {
          x: uint8[];
          y: bitfield(8) {
            a: 0..3 = 4;
            b: 4..7;
          };
        };
    
    This will parse uint8s until a value is discovered that has its bits
    set as defined by the bitfield constant.
    
    (We use the term "constant" loosely here: only the bits with values
    are actually enforced to be constant, all others are parsed as usual.)
    
    Closes #1467.
    rsmmr committed Sep 7, 2023
    Configuration menu
    Copy the full SHA
    1c25f1b View commit details
    Browse the repository at this point in the history