Skip to content

Commit

Permalink
Testcase with side-effects in LHS of op-assign
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Dodd <[email protected]>
  • Loading branch information
ChrisDodd committed Jan 22, 2025
1 parent 3c23788 commit 5e82b7a
Show file tree
Hide file tree
Showing 11 changed files with 631 additions and 6 deletions.
7 changes: 5 additions & 2 deletions frontends/p4/typeChecking/typeCheckStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,11 @@ const IR::Node *TypeInferenceBase::postorder(const IR::AssignmentStatement *assi
}

auto newInit = assignment(assign, ltype, assign->right);
if (newInit != assign->right)
assign = new IR::AssignmentStatement(assign->srcInfo, assign->left, newInit);
if (newInit != assign->right) {
auto *clone = assign->clone();
clone->right = newInit;
assign = clone;
}
return assign;
}

Expand Down
87 changes: 87 additions & 0 deletions testdata/p4_16_samples/opassign2-bmv2.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#include <core.p4>
#include <v1model.p4>

header data_t {
bit<32> f1;
bit<32> f2;
bit<16> h1;
bit<16> h2;
bit<8> b1;
bit<8> b2;
bit<8> b3;
bit<8> b4;
}

header word_t {
bit<32> x;
}

struct metadata {}

struct headers {
data_t data;
word_t[8] rest;
}

parser p(packet_in b,
out headers hdr,
inout metadata meta,
inout standard_metadata_t stdmeta) {
state start {
b.extract(hdr.data);
b.extract(hdr.rest.next);
b.extract(hdr.rest.next);
b.extract(hdr.rest.next);
b.extract(hdr.rest.next);
b.extract(hdr.rest.next);
b.extract(hdr.rest.next);
b.extract(hdr.rest.next);
b.extract(hdr.rest.next);
transition accept;
}
}

bit<8> postincr(inout bit<8> x) {
bit<8> rv = x;
x += 1;
return rv;
}

control ingress(inout headers hdr,
inout metadata meta,
inout standard_metadata_t stdmeta) {
apply {
hdr.rest[postincr(hdr.data.b1)].x |= hdr.data.f1;
hdr.rest[postincr(hdr.data.b1)].x |= hdr.data.f2;
}
}

control egress(inout headers hdr,
inout metadata meta,
inout standard_metadata_t stdmeta) {
apply {}
}

control vc(inout headers hdr,
inout metadata meta) {
apply {}
}

control uc(inout headers hdr,
inout metadata meta) {
apply {}
}

control deparser(packet_out packet,
in headers hdr) {
apply {
packet.emit(hdr);
}
}

V1Switch<headers, metadata>(p(),
vc(),
ingress(),
egress(),
uc(),
deparser()) main;
2 changes: 2 additions & 0 deletions testdata/p4_16_samples/opassign2-bmv2.stf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
packet 0 00001234 00005678 DEAD DEAD 01 05 FF AA 10000000 20000000 30000000 40000000 50000000 60000000 70000000 80000000
expect 0 00001234 00005678 DEAD DEAD 03 05 FF AA 10000000 20001234 30005678 40000000 50000000 60000000 70000000 80000000
76 changes: 76 additions & 0 deletions testdata/p4_16_samples_outputs/opassign2-bmv2-first.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include <core.p4>
#define V1MODEL_VERSION 20180101
#include <v1model.p4>

header data_t {
bit<32> f1;
bit<32> f2;
bit<16> h1;
bit<16> h2;
bit<8> b1;
bit<8> b2;
bit<8> b3;
bit<8> b4;
}

header word_t {
bit<32> x;
}

struct metadata {
}

struct headers {
data_t data;
word_t[8] rest;
}

parser p(packet_in b, out headers hdr, inout metadata meta, inout standard_metadata_t stdmeta) {
state start {
b.extract<data_t>(hdr.data);
b.extract<word_t>(hdr.rest.next);
b.extract<word_t>(hdr.rest.next);
b.extract<word_t>(hdr.rest.next);
b.extract<word_t>(hdr.rest.next);
b.extract<word_t>(hdr.rest.next);
b.extract<word_t>(hdr.rest.next);
b.extract<word_t>(hdr.rest.next);
b.extract<word_t>(hdr.rest.next);
transition accept;
}
}

bit<8> postincr(inout bit<8> x) {
bit<8> rv = x;
x += 8w1;
return rv;
}
control ingress(inout headers hdr, inout metadata meta, inout standard_metadata_t stdmeta) {
apply {
hdr.rest[postincr(hdr.data.b1)].x |= hdr.data.f1;
hdr.rest[postincr(hdr.data.b1)].x |= hdr.data.f2;
}
}

control egress(inout headers hdr, inout metadata meta, inout standard_metadata_t stdmeta) {
apply {
}
}

control vc(inout headers hdr, inout metadata meta) {
apply {
}
}

control uc(inout headers hdr, inout metadata meta) {
apply {
}
}

control deparser(packet_out packet, in headers hdr) {
apply {
packet.emit<headers>(hdr);
}
}

V1Switch<headers, metadata>(p(), vc(), ingress(), egress(), uc(), deparser()) main;
95 changes: 95 additions & 0 deletions testdata/p4_16_samples_outputs/opassign2-bmv2-frontend.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#include <core.p4>
#define V1MODEL_VERSION 20180101
#include <v1model.p4>

header data_t {
bit<32> f1;
bit<32> f2;
bit<16> h1;
bit<16> h2;
bit<8> b1;
bit<8> b2;
bit<8> b3;
bit<8> b4;
}

header word_t {
bit<32> x;
}

struct metadata {
}

struct headers {
data_t data;
word_t[8] rest;
}

parser p(packet_in b, out headers hdr, inout metadata meta, inout standard_metadata_t stdmeta) {
state start {
b.extract<data_t>(hdr.data);
b.extract<word_t>(hdr.rest.next);
b.extract<word_t>(hdr.rest.next);
b.extract<word_t>(hdr.rest.next);
b.extract<word_t>(hdr.rest.next);
b.extract<word_t>(hdr.rest.next);
b.extract<word_t>(hdr.rest.next);
b.extract<word_t>(hdr.rest.next);
b.extract<word_t>(hdr.rest.next);
transition accept;
}
}

control ingress(inout headers hdr, inout metadata meta, inout standard_metadata_t stdmeta) {
@name("ingress.tmp") bit<8> tmp;
@name("ingress.tmp_0") bit<8> tmp_0;
@name("ingress.tmp_1") bit<8> tmp_1;
@name("ingress.tmp_2") bit<8> tmp_2;
@name("ingress.x_0") bit<8> x_2;
@name("ingress.retval") bit<8> retval;
@name("ingress.rv") bit<8> rv_0;
@name("ingress.x_1") bit<8> x_3;
@name("ingress.retval") bit<8> retval_1;
@name("ingress.rv") bit<8> rv_1;
apply {
x_2 = hdr.data.b1;
rv_0 = x_2;
x_2 = x_2 + 8w1;
retval = rv_0;
hdr.data.b1 = x_2;
tmp = retval;
tmp_0 = tmp;
hdr.rest[tmp_0].x = hdr.rest[tmp_0].x | hdr.data.f1;
x_3 = hdr.data.b1;
rv_1 = x_3;
x_3 = x_3 + 8w1;
retval_1 = rv_1;
hdr.data.b1 = x_3;
tmp_1 = retval_1;
tmp_2 = tmp_1;
hdr.rest[tmp_2].x = hdr.rest[tmp_2].x | hdr.data.f2;
}
}

control egress(inout headers hdr, inout metadata meta, inout standard_metadata_t stdmeta) {
apply {
}
}

control vc(inout headers hdr, inout metadata meta) {
apply {
}
}

control uc(inout headers hdr, inout metadata meta) {
apply {
}
}

control deparser(packet_out packet, in headers hdr) {
apply {
packet.emit<headers>(hdr);
}
}

V1Switch<headers, metadata>(p(), vc(), ingress(), egress(), uc(), deparser()) main;
Loading

0 comments on commit 5e82b7a

Please sign in to comment.