Skip to content

Commit c2460b2

Browse files
authored
Allow lookahead of structs with size of 0 (#4149)
Signed-off-by: Mihai Budiu <mbudiu@gmail.com>
1 parent c9319af commit c2460b2

File tree

7 files changed

+98
-1
lines changed

7 files changed

+98
-1
lines changed

midend/expandLookahead.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ void DoExpandLookahead::expand(
4343
}
4444
} else if (type->is<IR::Type_Bits>() || type->is<IR::Type_Boolean>()) {
4545
unsigned size = type->width_bits();
46-
BUG_CHECK(size > 0, "%1%: unexpected size %2%", type, size);
46+
if (size == 0) return;
4747
const IR::Expression *expression =
4848
new IR::Slice(bitvector->clone(), *offset - 1, *offset - size);
4949
auto tb = type->to<IR::Type_Bits>();

testdata/p4_16_samples/issue4143.p4

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <core.p4>
2+
3+
parser Parser(packet_in pkt);
4+
package SimpleArch(Parser p);
5+
6+
struct Foo {
7+
bit<0> f0;
8+
}
9+
10+
parser MyParser(packet_in packet) {
11+
state start {
12+
transition select(packet.lookahead<Foo>().f0) {
13+
0: accept;
14+
default: reject;
15+
}
16+
}
17+
}
18+
19+
SimpleArch(MyParser()) main;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <core.p4>
2+
3+
parser Parser(packet_in pkt);
4+
package SimpleArch(Parser p);
5+
struct Foo {
6+
bit<0> f0;
7+
}
8+
9+
parser MyParser(packet_in packet) {
10+
state start {
11+
transition select((packet.lookahead<Foo>()).f0) {
12+
0: accept;
13+
default: reject;
14+
}
15+
}
16+
}
17+
18+
SimpleArch(MyParser()) main;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <core.p4>
2+
3+
parser Parser(packet_in pkt);
4+
package SimpleArch(Parser p);
5+
struct Foo {
6+
bit<0> f0;
7+
}
8+
9+
parser MyParser(packet_in packet) {
10+
@name("MyParser.tmp") bit<0> tmp;
11+
@name("MyParser.tmp_0") Foo tmp_0;
12+
state start {
13+
tmp_0 = packet.lookahead<Foo>();
14+
tmp = tmp_0.f0;
15+
transition select(tmp) {
16+
0: accept;
17+
default: reject;
18+
}
19+
}
20+
}
21+
22+
SimpleArch(MyParser()) main;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <core.p4>
2+
3+
parser Parser(packet_in pkt);
4+
package SimpleArch(Parser p);
5+
struct Foo {
6+
bit<0> f0;
7+
}
8+
9+
parser MyParser(packet_in packet) {
10+
@name("MyParser.tmp_0") Foo tmp_0;
11+
state start {
12+
packet.lookahead<bit<0>>();
13+
transition select(tmp_0.f0) {
14+
0: accept;
15+
default: reject;
16+
}
17+
}
18+
}
19+
20+
SimpleArch(MyParser()) main;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <core.p4>
2+
3+
parser Parser(packet_in pkt);
4+
package SimpleArch(Parser p);
5+
struct Foo {
6+
bit<0> f0;
7+
}
8+
9+
parser MyParser(packet_in packet) {
10+
state start {
11+
transition select((packet.lookahead<Foo>()).f0) {
12+
0: accept;
13+
default: reject;
14+
}
15+
}
16+
}
17+
18+
SimpleArch(MyParser()) main;

testdata/p4_16_samples_outputs/issue4143.p4-stderr

Whitespace-only changes.

0 commit comments

Comments
 (0)