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

Proposal: match bits #2016

Closed
vdisasm opened this issue Nov 20, 2018 · 10 comments
Closed

Proposal: match bits #2016

vdisasm opened this issue Nov 20, 2018 · 10 comments

Comments

@vdisasm
Copy link

vdisasm commented Nov 20, 2018

I'd like to discuss following idea

I'm creating disassemblers and it needs machine instruction decoding. i.e. matching many bit patterns and extracting bits.

Expression to test bits is

if ((value & mask_and) == mask_match)

For example, to test bits 0 and 3 are set

if ((value & 0b_1001) == 0xb_1001)

Using pattern matching could be

if (value is 0b_1??1)

where ? are ignored and can represent any digit
or

switch (value)
case 0b_1??1:

Some problems I can see:

  • It should work for binary and hex number, where it is easy to calculate mask for digits, unlike decimals.
  • It is incomplete without syntax to easy access bits, like
int bits = value[high..low];   or | int bits = value.bits[high..low]; 
value[high..low] = bits;          | value.bits[high..low] = bits;
  • It won't be widely used
@YairHalberstadt
Copy link
Contributor

This is a bit of a niche scenario to add a language feature for. It there a more generally useful proposal underlying this?

@vdisasm
Copy link
Author

vdisasm commented Nov 20, 2018

I know, it's very specific.
I doubt there would be mass interest in this, because I can imagine usage of this only when need to manipulate bits. Like low level structures decoding, processing input signals.

@YairHalberstadt
Copy link
Contributor

How much of that is currently done in C#?

@DavidArno
Copy link

@alrz,

I was going to suggest that this could be solved with your active patterns proposal:

public static bool BitPatternMatch(this int value. int mask) => (value & mask) == mask;

if (value is BitPatternMatch(0xb_1001))switch (value)
{
    case BitPatternMatch(0xb_1001) :

but you have closed that issue saying that it's no longer required with recursive patterns. How would you solve this with active patterns as I'm confused by your closing comment on that issue?

@alrz
Copy link
Contributor

alrz commented Nov 20, 2018

@DavidArno

In a recursive pattern like that, when the type lookup for BitPatternMatch fails, we'll look for a method or an extension method on the target value, e.g. value.BitPatternMatch(0xb_1001) (perhaps with some requirement like an attribute on the method or a name convention to prevent accidental binding).

Each argument can be a:

  • (1) constant pattern,
    • (1a) If the corresponding parameter is not out/ref we'll pass the constant value as the argument.
    • (1b) If the corresponding parameter is out we'll match it against the result.
    • (1c) otherwise, an error is produced.
  • or (2) some other pattern.
    • (2a) If the corresponding parameter is out we'll match it against the result.
    • (2b) otherwise, an error is produced.

This is mostly the same as how we handle Deconstruct, except for the case (1a) and the lookup.

@alrz
Copy link
Contributor

alrz commented Nov 20, 2018

saying that it's no longer required with recursive patterns

That's not what I was saying. This is still a separate feature, but the proposed parsing style is out of the question.

@DavidArno
Copy link

@alrz, ah that makes sense. Thanks.

@vdisasm
Copy link
Author

vdisasm commented Nov 20, 2018

(value & mask) == mask

should be

(value & mask1) == mask2

First mask tells what bits I want. 2nd mask is to test bits (either set or cleared, can be combined: 1??1, 0??0, 1??0, 0??1).

Can I do this with recursive patterns?

@DavidArno
Copy link

@vdisasm, recursive patterns, as planned for C# 8, will not support that. Some form of user-definable, extendable, or active pattern (as discussed in #1047, and all possibly the same thing, just with different names) would though. You'd simply write custom code to handle your pattern use-case.

@vdisasm
Copy link
Author

vdisasm commented Nov 20, 2018

Thanks I will check that

@vdisasm vdisasm closed this as completed Nov 20, 2018
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

4 participants