From 754755df3259b056ffb4993ec663e921e4d5f61f Mon Sep 17 00:00:00 2001 From: Mihai Budiu Date: Tue, 24 May 2022 17:04:47 -0700 Subject: [PATCH] Allow value sets as labels when select() is done on a constant Signed-off-by: Mihai Budiu --- frontends/common/constantFolding.cpp | 9 ++++----- testdata/p4_16_samples/issue3343.p4 | 9 +++++++++ testdata/p4_16_samples_outputs/issue3343-first.p4 | 10 ++++++++++ testdata/p4_16_samples_outputs/issue3343-frontend.p4 | 0 testdata/p4_16_samples_outputs/issue3343.p4 | 10 ++++++++++ testdata/p4_16_samples_outputs/issue3343.p4-stderr | 1 + 6 files changed, 34 insertions(+), 5 deletions(-) create mode 100644 testdata/p4_16_samples/issue3343.p4 create mode 100644 testdata/p4_16_samples_outputs/issue3343-first.p4 create mode 100644 testdata/p4_16_samples_outputs/issue3343-frontend.p4 create mode 100644 testdata/p4_16_samples_outputs/issue3343.p4 create mode 100644 testdata/p4_16_samples_outputs/issue3343.p4-stderr diff --git a/frontends/common/constantFolding.cpp b/frontends/common/constantFolding.cpp index 29ae9efdbc..e1003a74a7 100644 --- a/frontends/common/constantFolding.cpp +++ b/frontends/common/constantFolding.cpp @@ -971,7 +971,7 @@ DoConstantFolding::setContains(const IR::Expression* keySet, const IR::Expressio return Result::Yes; return Result::No; } - ::error(ErrorType::ERR_INVALID, "%1%: unexpected expression", keySet); + // Otherwise the keyset may be a ValueSet return Result::DontKnow; } @@ -985,10 +985,8 @@ const IR::Node* DoConstantFolding::postorder(IR::SelectExpression* expression) { bool someUnknown = false; bool changes = false; bool finished = false; - const IR::Expression* result = expression; - /* FIXME -- should erase/replace each element as needed, rather than creating a new Vector. - * Should really implement this in SelectCase pre/postorder and this postorder goes away */ + for (auto c : expression->selectCases) { if (finished) { if (warnings) @@ -1003,9 +1001,10 @@ const IR::Node* DoConstantFolding::postorder(IR::SelectExpression* expression) { someUnknown = true; cases.push_back(c); } else { - changes = true; finished = true; if (someUnknown) { + if (!c->keyset->is()) + changes = true; auto newc = new IR::SelectCase(c->srcInfo, new IR::DefaultExpression(), c->state); cases.push_back(newc); } else { diff --git a/testdata/p4_16_samples/issue3343.p4 b/testdata/p4_16_samples/issue3343.p4 new file mode 100644 index 0000000000..4be453567d --- /dev/null +++ b/testdata/p4_16_samples/issue3343.p4 @@ -0,0 +1,9 @@ +parser MyParser1(in bit<6> ttt) { + value_set>(4) myvs; + state start { + transition select(6w1) { + myvs: accept; + _: reject; + } + } +} diff --git a/testdata/p4_16_samples_outputs/issue3343-first.p4 b/testdata/p4_16_samples_outputs/issue3343-first.p4 new file mode 100644 index 0000000000..7fbf31027a --- /dev/null +++ b/testdata/p4_16_samples_outputs/issue3343-first.p4 @@ -0,0 +1,10 @@ +parser MyParser1(in bit<6> ttt) { + value_set>(4) myvs; + state start { + transition select(6w1) { + myvs: accept; + default: reject; + } + } +} + diff --git a/testdata/p4_16_samples_outputs/issue3343-frontend.p4 b/testdata/p4_16_samples_outputs/issue3343-frontend.p4 new file mode 100644 index 0000000000..e69de29bb2 diff --git a/testdata/p4_16_samples_outputs/issue3343.p4 b/testdata/p4_16_samples_outputs/issue3343.p4 new file mode 100644 index 0000000000..7fbf31027a --- /dev/null +++ b/testdata/p4_16_samples_outputs/issue3343.p4 @@ -0,0 +1,10 @@ +parser MyParser1(in bit<6> ttt) { + value_set>(4) myvs; + state start { + transition select(6w1) { + myvs: accept; + default: reject; + } + } +} + diff --git a/testdata/p4_16_samples_outputs/issue3343.p4-stderr b/testdata/p4_16_samples_outputs/issue3343.p4-stderr new file mode 100644 index 0000000000..7e57a518ff --- /dev/null +++ b/testdata/p4_16_samples_outputs/issue3343.p4-stderr @@ -0,0 +1 @@ +[--Wwarn=missing] warning: Program does not contain a `main' module