p4lang / p4c

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

bmv2 "array index must be a constant" error #2007

Closed hesingh closed 4 years ago

hesingh commented 5 years ago

I am using a header stack in v1model p4-16 code. The index into the header stack array is obtained from a header in the packet and index copied to metadata. Obviously, the header is parsed in the parser. p4c-bm2-ss fails because the index is not a compile-time constant. So, how is such code supposed to work? The p4test backend does not care and compiles the code fine.

./testdata/p4_16_samples/ipv6-switch-ml-bmv2.p4(111): [--Werror=invalid] error: meta._ml_idx4: Invalid all array indexes must be constant
             hdr.vector[0].e = pool[meta.ml_idx].val;
                                    ^^^^
hesingh commented 4 years ago

A correction to Andy's previous comment: bmv2 already supports dynamic indices for header stacks. This is done using the dereference_header_stack binary operand in the bmv2 JSON (see the doc), which takes as parameters the name of the header stack and an expression evaluating to a valid index for that header stack.

Now that I changed p4c bmv2 backend to support dynamic indices, the expression is a JsonObject. If you see this behavioral-model code,

https://github.com/p4lang/behavioral-model/blob/7192f7550b20859e9f5f1fb8759f2d07e912b6ae/src/bm_sim/P4Objects.cpp#L476

the code is incomplete to handle a expression if the expression type is objectValue. asString() fails on this line of code:

https://github.com/p4lang/behavioral-model/blob/7192f7550b20859e9f5f1fb8759f2d07e912b6ae/third_party/jsoncpp/src/jsoncpp.cpp#L3111

with type_ == 7 which is objectValue. So, even this json code is incomplete.

jafingerhut commented 4 years ago

If you believe you have a BMv2 JSON file that contains what you've been told is correct for variable array indices, and it is leading to some kind of misbehavior in simple_switch, I would recommend creating a separate issue for the behavioral-model repo, attach the BMv2 JSON file you think should work, and details about what is going wrong.

hesingh commented 4 years ago

I do think the JSON file from new p4c code with expression as a JsonObject is correct and the beharioral-model should support the expression being a JsonObject. I have filed this issue.

https://github.com/p4lang/behavioral-model/issues/835