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

Conversation

rsmmr
Copy link
Member

@rsmmr rsmmr commented Aug 31, 2023

No description provided.

@rsmmr rsmmr force-pushed the topic/robin/gh-1467-bitfield-constants branch from a3bd5e9 to 3550048 Compare September 1, 2023 08:24
@rsmmr rsmmr marked this pull request as ready for review September 1, 2023 08:24
@rsmmr rsmmr force-pushed the topic/robin/gh-1467-bitfield-constants branch from 3550048 to 95ada1f Compare September 1, 2023 08:24
@rsmmr rsmmr requested a review from bbannier September 1, 2023 08:25
@rsmmr rsmmr force-pushed the topic/robin/gh-1467-bitfield-constants branch from 95ada1f to 22b31ed Compare September 1, 2023 11:08
bbannier
bbannier previously approved these changes Sep 1, 2023
hilti/toolchain/include/ast/operators/bitfield.h Outdated Show resolved Hide resolved
hilti/toolchain/include/ast/operators/bitfield.h Outdated Show resolved Hide resolved
doc/programming/language/types.rst Outdated Show resolved Hide resolved
doc/programming/parsing.rst Show resolved Hide resolved
spicy/toolchain/src/compiler/codegen/parsers/types.cc Outdated Show resolved Hide resolved
tests/spicy/types/bitfield/ctor-fail.spicy Show resolved Hide resolved
…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).
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];
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.
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 rsmmr force-pushed the topic/robin/gh-1467-bitfield-constants branch from 27fb006 to 1c25f1b Compare September 7, 2023 09:31
@rsmmr rsmmr merged commit f2129d7 into main Sep 7, 2023
22 checks passed
@rsmmr rsmmr deleted the topic/robin/gh-1467-bitfield-constants branch September 7, 2023 15:37
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

Successfully merging this pull request may close these issues.

2 participants