p4lang / p4c

P4_16 reference compiler
https://p4.org/
Apache License 2.0
679 stars 444 forks source link

Is there a pedantic mode for operators on arbitrary-precision integers #3730

Open apinski-cavium opened 1 year ago

apinski-cavium commented 1 year ago

From the spec:

Note: bitwise-operations (|,&,^,~) are not defined on expressions of type int.

But p4c allows to compile:

const  bit<48> tmp2 = (1 | 2) |+| 48w0;

Is there a pedantic mode to reject some extensions that p4c accept above the specifications like this case?

I noticed this while testing many of the testcases in the p4c testsuite on my front-end. testdata/p4_16_samples/gauntlet_various_ops-bmv2.p4 has this in there.

apinski-cavium commented 1 year ago

Or should |, ^ and & be valid really be made valid for arbitrary-precision integers. in similar way shifts and bit slices are handled?

mihaibudiu commented 1 year ago

Maybe I messed this up in the recent pr #3716

apinski-cavium commented 1 year ago

Maybe I messed this up in the recent pr #3716

No this has been accepted for a while I think.

2661 added the testcases.

rst0git commented 1 year ago

Note: bitwise-operations (|,&,^,~) are not defined on expressions of type int.

In general, bitwise operations such as |, & and ^ are supported for int in other programming languages. If I understand correctly, this restriction has been introduced with P4_14 and has not been updated since.

@mbudiu-vmw Is it worth discussing this at the P4 Language Design Working Group meetings?

apinski-cavium commented 1 year ago

Note: bitwise-operations (|,&,^,~) are not defined on expressions of type int.

In general, bitwise operations such as |, & and ^ are supported for int in other programming languages. If I understand correctly, this restriction has been introduced with P4_14 and has not been updated since.

Arbitrary-precision integers always seemed too limited in the P4_16 language and it seemed like the spec was always too tight in that area so I am ok with loosing it up if there is well defined behavior.

Even CST0 ++ CST1 could be defined as (CST0 << shift) | CST1 where shift is the min bitwidth that fits CST1 (which I think is the same as ceil(log2(CST1+1))), though I might have done my math wrong). And yes I think this should be raised to the P4 LDWG.

apinski-cavium commented 1 year ago

Note the only issue with ++ is how to handle negative #s for all of those operations. But if it is positive it is obvious what should be done really.

mihaibudiu commented 1 year ago

Filed https://github.com/p4lang/p4-spec/issues/1189 for us to discuss at some point.