Skip to content

Commit

Permalink
Allow lookahead of structs with size of 0 (#4149)
Browse files Browse the repository at this point in the history
Signed-off-by: Mihai Budiu <[email protected]>
  • Loading branch information
mihaibudiu authored Sep 12, 2023
1 parent c9319af commit c2460b2
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 1 deletion.
2 changes: 1 addition & 1 deletion midend/expandLookahead.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void DoExpandLookahead::expand(
}
} else if (type->is<IR::Type_Bits>() || type->is<IR::Type_Boolean>()) {
unsigned size = type->width_bits();
BUG_CHECK(size > 0, "%1%: unexpected size %2%", type, size);
if (size == 0) return;
const IR::Expression *expression =
new IR::Slice(bitvector->clone(), *offset - 1, *offset - size);
auto tb = type->to<IR::Type_Bits>();
Expand Down
19 changes: 19 additions & 0 deletions testdata/p4_16_samples/issue4143.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <core.p4>

parser Parser(packet_in pkt);
package SimpleArch(Parser p);

struct Foo {
bit<0> f0;
}

parser MyParser(packet_in packet) {
state start {
transition select(packet.lookahead<Foo>().f0) {
0: accept;
default: reject;
}
}
}

SimpleArch(MyParser()) main;
18 changes: 18 additions & 0 deletions testdata/p4_16_samples_outputs/issue4143-first.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <core.p4>

parser Parser(packet_in pkt);
package SimpleArch(Parser p);
struct Foo {
bit<0> f0;
}

parser MyParser(packet_in packet) {
state start {
transition select((packet.lookahead<Foo>()).f0) {
0: accept;
default: reject;
}
}
}

SimpleArch(MyParser()) main;
22 changes: 22 additions & 0 deletions testdata/p4_16_samples_outputs/issue4143-frontend.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <core.p4>

parser Parser(packet_in pkt);
package SimpleArch(Parser p);
struct Foo {
bit<0> f0;
}

parser MyParser(packet_in packet) {
@name("MyParser.tmp") bit<0> tmp;
@name("MyParser.tmp_0") Foo tmp_0;
state start {
tmp_0 = packet.lookahead<Foo>();
tmp = tmp_0.f0;
transition select(tmp) {
0: accept;
default: reject;
}
}
}

SimpleArch(MyParser()) main;
20 changes: 20 additions & 0 deletions testdata/p4_16_samples_outputs/issue4143-midend.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <core.p4>

parser Parser(packet_in pkt);
package SimpleArch(Parser p);
struct Foo {
bit<0> f0;
}

parser MyParser(packet_in packet) {
@name("MyParser.tmp_0") Foo tmp_0;
state start {
packet.lookahead<bit<0>>();
transition select(tmp_0.f0) {
0: accept;
default: reject;
}
}
}

SimpleArch(MyParser()) main;
18 changes: 18 additions & 0 deletions testdata/p4_16_samples_outputs/issue4143.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <core.p4>

parser Parser(packet_in pkt);
package SimpleArch(Parser p);
struct Foo {
bit<0> f0;
}

parser MyParser(packet_in packet) {
state start {
transition select((packet.lookahead<Foo>()).f0) {
0: accept;
default: reject;
}
}
}

SimpleArch(MyParser()) main;
Empty file.

0 comments on commit c2460b2

Please sign in to comment.