From ed5cc1f64837738f5d9ccbe93a1ec7085bb1c444 Mon Sep 17 00:00:00 2001 From: mbudiu-vmw Date: Wed, 10 May 2017 17:08:31 -0700 Subject: [PATCH] Front-end support for unions; partial fix for #561 --- testdata/p4_16_errors/issue561-1.p4 | 33 +++++++++++++ testdata/p4_16_errors_outputs/issue561-1.p4 | 23 +++++++++ .../p4_16_errors_outputs/issue561-1.p4-stderr | 18 +++++++ testdata/p4_16_samples/issue561.p4 | 48 +++++++++++++++++++ .../p4_16_samples_outputs/issue561-first.p4 | 34 +++++++++++++ .../issue561-frontend.p4 | 35 ++++++++++++++ .../p4_16_samples_outputs/issue561-midend.p4 | 42 ++++++++++++++++ testdata/p4_16_samples_outputs/issue561.p4 | 34 +++++++++++++ .../p4_16_samples_outputs/issue561.p4-stderr | 9 ++++ .../p4_16_samples_outputs/struct-first.p4 | 4 ++ .../p4_16_samples_outputs/struct-frontend.p4 | 3 ++ testdata/p4_16_samples_outputs/struct.p4 | 4 ++ 12 files changed, 287 insertions(+) create mode 100644 testdata/p4_16_errors/issue561-1.p4 create mode 100644 testdata/p4_16_errors_outputs/issue561-1.p4 create mode 100644 testdata/p4_16_errors_outputs/issue561-1.p4-stderr create mode 100644 testdata/p4_16_samples/issue561.p4 create mode 100644 testdata/p4_16_samples_outputs/issue561-first.p4 create mode 100644 testdata/p4_16_samples_outputs/issue561-frontend.p4 create mode 100644 testdata/p4_16_samples_outputs/issue561-midend.p4 create mode 100644 testdata/p4_16_samples_outputs/issue561.p4 create mode 100644 testdata/p4_16_samples_outputs/issue561.p4-stderr diff --git a/testdata/p4_16_errors/issue561-1.p4 b/testdata/p4_16_errors/issue561-1.p4 new file mode 100644 index 00000000000..44af3b837a4 --- /dev/null +++ b/testdata/p4_16_errors/issue561-1.p4 @@ -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; \ No newline at end of file diff --git a/testdata/p4_16_errors_outputs/issue561-1.p4 b/testdata/p4_16_errors_outputs/issue561-1.p4 new file mode 100644 index 00000000000..c89e86bc7bc --- /dev/null +++ b/testdata/p4_16_errors_outputs/issue561-1.p4 @@ -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; diff --git a/testdata/p4_16_errors_outputs/issue561-1.p4-stderr b/testdata/p4_16_errors_outputs/issue561-1.p4-stderr new file mode 100644 index 00000000000..98220cd5373 --- /dev/null +++ b/testdata/p4_16_errors_outputs/issue561-1.p4-stderr @@ -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 + ^^^^^^^^^^ diff --git a/testdata/p4_16_samples/issue561.p4 b/testdata/p4_16_samples/issue561.p4 new file mode 100644 index 00000000000..3600cda868b --- /dev/null +++ b/testdata/p4_16_samples/issue561.p4 @@ -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; \ No newline at end of file diff --git a/testdata/p4_16_samples_outputs/issue561-first.p4 b/testdata/p4_16_samples_outputs/issue561-first.p4 new file mode 100644 index 00000000000..0b367698c3e --- /dev/null +++ b/testdata/p4_16_samples_outputs/issue561-first.p4 @@ -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; diff --git a/testdata/p4_16_samples_outputs/issue561-frontend.p4 b/testdata/p4_16_samples_outputs/issue561-frontend.p4 new file mode 100644 index 00000000000..f3f1cb92e14 --- /dev/null +++ b/testdata/p4_16_samples_outputs/issue561-frontend.p4 @@ -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; diff --git a/testdata/p4_16_samples_outputs/issue561-midend.p4 b/testdata/p4_16_samples_outputs/issue561-midend.p4 new file mode 100644 index 00000000000..58e6444a17e --- /dev/null +++ b/testdata/p4_16_samples_outputs/issue561-midend.p4 @@ -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; diff --git a/testdata/p4_16_samples_outputs/issue561.p4 b/testdata/p4_16_samples_outputs/issue561.p4 new file mode 100644 index 00000000000..694b0cc066c --- /dev/null +++ b/testdata/p4_16_samples_outputs/issue561.p4 @@ -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; diff --git a/testdata/p4_16_samples_outputs/issue561.p4-stderr b/testdata/p4_16_samples_outputs/issue561.p4-stderr new file mode 100644 index 00000000000..d0431627644 --- /dev/null +++ b/testdata/p4_16_samples_outputs/issue561.p4-stderr @@ -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; + ^^^^^^^^^^ diff --git a/testdata/p4_16_samples_outputs/struct-first.p4 b/testdata/p4_16_samples_outputs/struct-first.p4 index a014d097f0e..ba7c33fe8ba 100644 --- a/testdata/p4_16_samples_outputs/struct-first.p4 +++ b/testdata/p4_16_samples_outputs/struct-first.p4 @@ -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 = { }; diff --git a/testdata/p4_16_samples_outputs/struct-frontend.p4 b/testdata/p4_16_samples_outputs/struct-frontend.p4 index 2626f6c84f7..6b92b64f37a 100644 --- a/testdata/p4_16_samples_outputs/struct-frontend.p4 +++ b/testdata/p4_16_samples_outputs/struct-frontend.p4 @@ -13,3 +13,6 @@ struct S { T s2; } +struct Empty { +} + diff --git a/testdata/p4_16_samples_outputs/struct.p4 b/testdata/p4_16_samples_outputs/struct.p4 index 68f47910ef7..104dadfb65e 100644 --- a/testdata/p4_16_samples_outputs/struct.p4 +++ b/testdata/p4_16_samples_outputs/struct.p4 @@ -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 = { };