Skip to content

Commit

Permalink
Front-end support for unions; partial fix for p4lang#561
Browse files Browse the repository at this point in the history
  • Loading branch information
mbudiu-vmw committed May 12, 2017
1 parent 4608027 commit ed5cc1f
Show file tree
Hide file tree
Showing 12 changed files with 287 additions and 0 deletions.
33 changes: 33 additions & 0 deletions testdata/p4_16_errors/issue561-1.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
Copyright 2017 VMware, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
header H1 { bit<32> f; }
header H2 { bit<32> g; }

header_union U {
H1 h1;
H2 h2;
}

control ct();
package top(ct _ct);

control c() {
apply {
U u = { { 10 }, { 20 } }; // illegal to initialize unions
u.setValid(); // no such method
}
}
top(c()) main;
23 changes: 23 additions & 0 deletions testdata/p4_16_errors_outputs/issue561-1.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
header H1 {
bit<32> f;
}

header H2 {
bit<32> g;
}

header_union U {
H1 h1;
H2 h2;
}

control ct();
package top(ct _ct);
control c() {
apply {
U u = { { 10 }, { 20 } };
u.setValid();
}
}

top(c()) main;
18 changes: 18 additions & 0 deletions testdata/p4_16_errors_outputs/issue561-1.p4-stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
../testdata/p4_16_errors/issue561-1.p4(29): error: u: Cannot unify Tuple(2) to header_union U
U u = { { 10 }, { 20 } }; // illegal to initialize unions
^^^^^^^^^^^^^^^^^^^^^^^^^
../testdata/p4_16_errors/issue561-1.p4(29)
U u = { { 10 }, { 20 } }; // illegal to initialize unions
^^^^^^^^^^^^^^^^^^
../testdata/p4_16_errors/issue561-1.p4(19)
header_union U {
^
../testdata/p4_16_errors/issue561-1.p4(19): error: Structure header_union U does not have a field setValid
header_union U {
^
../testdata/p4_16_errors/issue561-1.p4(30)
u.setValid(); // no such method
^^^^^^^^
../testdata/p4_16_errors/issue561-1.p4(30): error: Could not find type of u.setValid
u.setValid(); // no such method
^^^^^^^^^^
48 changes: 48 additions & 0 deletions testdata/p4_16_samples/issue561.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
Copyright 2017 VMware, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
header H1 { bit<32> f; }
header H2 { bit<32> g; }

header_union U {
H1 h1;
H2 h2;
}

control ct(out bit<32> b);
package top(ct _ct);

control c(out bit<32> x) {
apply {
U u;
U[2] u2;

bool b = u.isValid();
b = b || u.h1.isValid();

x = u.h1.f + u.h2.g;
u.h1.setValid();
u.h1.f = 0;
x = x + u.h1.f;

u.h2.g = 0;
x = x + u.h2.g;

u2[0].h1.setValid();
u2[0].h1.f = 2;
x = x + u2[1].h2.g + u2[0].h1.f;
}
}
top(c()) main;
34 changes: 34 additions & 0 deletions testdata/p4_16_samples_outputs/issue561-first.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
header H1 {
bit<32> f;
}

header H2 {
bit<32> g;
}

header_union U {
H1 h1;
H2 h2;
}

control ct(out bit<32> b);
package top(ct _ct);
control c(out bit<32> x) {
apply {
U u;
U[2] u2;
bool b = u.isValid();
b = b || u.h1.isValid();
x = u.h1.f + u.h2.g;
u.h1.setValid();
u.h1.f = 32w0;
x = x + u.h1.f;
u.h2.g = 32w0;
x = x + u.h2.g;
u2[0].h1.setValid();
u2[0].h1.f = 32w2;
x = x + u2[1].h2.g + u2[0].h1.f;
}
}

top(c()) main;
35 changes: 35 additions & 0 deletions testdata/p4_16_samples_outputs/issue561-frontend.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
header H1 {
bit<32> f;
}

header H2 {
bit<32> g;
}

header_union U {
H1 h1;
H2 h2;
}

control ct(out bit<32> b);
package top(ct _ct);
control c(out bit<32> x) {
U u_0;
U[2] u2_0;
bool b_0;
apply {
b_0 = u_0.isValid();
u_0.h1.isValid();
x = u_0.h1.f + u_0.h2.g;
u_0.h1.setValid();
u_0.h1.f = 32w0;
x = x + u_0.h1.f;
u_0.h2.g = 32w0;
x = x + u_0.h2.g;
u2_0[0].h1.setValid();
u2_0[0].h1.f = 32w2;
x = x + u2_0[1].h2.g + u2_0[0].h1.f;
}
}

top(c()) main;
42 changes: 42 additions & 0 deletions testdata/p4_16_samples_outputs/issue561-midend.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
header H1 {
bit<32> f;
}

header H2 {
bit<32> g;
}

header_union U {
H1 h1;
H2 h2;
}

control ct(out bit<32> b);
package top(ct _ct);
control c(out bit<32> x) {
U u;
U[2] u2;
@hidden action act() {
u.h1.isValid();
x = u.h1.f + u.h2.g;
u.h1.setValid();
u.h1.f = 32w0;
x = x + u.h1.f;
u.h2.g = 32w0;
x = x + u.h2.g;
u2[0].h1.setValid();
u2[0].h1.f = 32w2;
x = x + u2[1].h2.g + u2[0].h1.f;
}
@hidden table tbl_act {
actions = {
act();
}
const default_action = act();
}
apply {
tbl_act.apply();
}
}

top(c()) main;
34 changes: 34 additions & 0 deletions testdata/p4_16_samples_outputs/issue561.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
header H1 {
bit<32> f;
}

header H2 {
bit<32> g;
}

header_union U {
H1 h1;
H2 h2;
}

control ct(out bit<32> b);
package top(ct _ct);
control c(out bit<32> x) {
apply {
U u;
U[2] u2;
bool b = u.isValid();
b = b || u.h1.isValid();
x = u.h1.f + u.h2.g;
u.h1.setValid();
u.h1.f = 0;
x = x + u.h1.f;
u.h2.g = 0;
x = x + u.h2.g;
u2[0].h1.setValid();
u2[0].h1.f = 2;
x = x + u2[1].h2.g + u2[0].h1.f;
}
}

top(c()) main;
9 changes: 9 additions & 0 deletions testdata/p4_16_samples_outputs/issue561.p4-stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
../testdata/p4_16_samples/issue561.p4(35): warning: u.h1.f may be uninitialized
x = u.h1.f + u.h2.g;
^^^^^^
../testdata/p4_16_samples/issue561.p4(35): warning: u.h2.g may be uninitialized
x = u.h1.f + u.h2.g;
^^^^^^
../testdata/p4_16_samples/issue561.p4(45): warning: [].h2.g may be uninitialized
x = x + u2[1].h2.g + u2[0].h1.f;
^^^^^^^^^^
4 changes: 4 additions & 0 deletions testdata/p4_16_samples_outputs/struct-first.p4
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ struct S {
T s2;
}

struct Empty {
}

const T t = { 32s10, 32s20 };
const S s = { { 32s15, 32s25 }, { 32s10, 32s20 } };
const int<32> x = 32s10;
const int<32> y = 32s25;
const int<32> w = 32s10;
const T t1 = { 32s15, 32s25 };
const Empty e = { };
3 changes: 3 additions & 0 deletions testdata/p4_16_samples_outputs/struct-frontend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ struct S {
T s2;
}

struct Empty {
}

4 changes: 4 additions & 0 deletions testdata/p4_16_samples_outputs/struct.p4
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ struct S {
T s2;
}

struct Empty {
}

const T t = { 32s10, 32s20 };
const S s = { { 32s15, 32s25 }, t };
const int<32> x = t.t1;
const int<32> y = s.s1.t2;
const int<32> w = .t.t1;
const T t1 = s.s1;
const Empty e = { };

0 comments on commit ed5cc1f

Please sign in to comment.