From 08c5498e90aee83e1b91fa5f72a26f513c38dd13 Mon Sep 17 00:00:00 2001 From: Chris Dodd Date: Mon, 3 Feb 2025 04:25:18 +0000 Subject: [PATCH] Clean up typeCheckTypes a bit; another test --- frontends/p4/typeChecking/typeCheckTypes.cpp | 16 +++------ testdata/p4_16_samples/array3.p4 | 30 ++++++++++++++++ .../p4_16_samples_outputs/array3-first.p4 | 27 ++++++++++++++ .../p4_16_samples_outputs/array3-frontend.p4 | 27 ++++++++++++++ .../p4_16_samples_outputs/array3-midend.p4 | 36 +++++++++++++++++++ testdata/p4_16_samples_outputs/array3.p4 | 27 ++++++++++++++ .../p4_16_samples_outputs/array3.p4-stderr | 0 7 files changed, 151 insertions(+), 12 deletions(-) create mode 100644 testdata/p4_16_samples/array3.p4 create mode 100644 testdata/p4_16_samples_outputs/array3-first.p4 create mode 100644 testdata/p4_16_samples_outputs/array3-frontend.p4 create mode 100644 testdata/p4_16_samples_outputs/array3-midend.p4 create mode 100644 testdata/p4_16_samples_outputs/array3.p4 create mode 100644 testdata/p4_16_samples_outputs/array3.p4-stderr diff --git a/frontends/p4/typeChecking/typeCheckTypes.cpp b/frontends/p4/typeChecking/typeCheckTypes.cpp index 10f934ca114..c9b5eb12d28 100644 --- a/frontends/p4/typeChecking/typeCheckTypes.cpp +++ b/frontends/p4/typeChecking/typeCheckTypes.cpp @@ -47,6 +47,7 @@ bool hasVarbitsOrUnions(const TypeMap *typeMap, const IR::Type *type) { bool TypeInferenceBase::onlyBitsOrBitStructs(const IR::Type *type) const { // called for a canonical type + while (auto *st = type->to()) type = st->elementType; if (type->is() || type->is() || type->is()) { return true; } else if (auto ht = type->to()) { @@ -424,18 +425,9 @@ const IR::Node *TypeInferenceBase::postorder(const IR::StructField *field) { const IR::Node *TypeInferenceBase::postorder(const IR::Type_Header *type) { auto canon = setTypeType(type); auto validator = [this](const IR::Type *t) { - while (1) { - if (t->is()) - t = getTypeType(t->to()->type); - else if (auto *st = t->to()) - t = st->elementType; - else - break; - } - return t->is() || t->is() || - (t->is() && onlyBitsOrBitStructs(t)) || t->is() || - t->is() || t->is() || - t->is(); + while (t->is()) t = getTypeType(t->to()->type); + return onlyBitsOrBitStructs(t) || t->is() || + t->is() || t->is(); }; validateFields(canon, validator); return type; diff --git a/testdata/p4_16_samples/array3.p4 b/testdata/p4_16_samples/array3.p4 new file mode 100644 index 00000000000..37549afce71 --- /dev/null +++ b/testdata/p4_16_samples/array3.p4 @@ -0,0 +1,30 @@ +control proto

(inout P pkt); +package top

(proto

p); + +struct S { + bit<16> f1; + bit<8>[16] nested_arr; + bit<16> f2; +} + +header data_t { + S[16] arr; + bit<4> w; + bit<4> x; + bit<4> y; + bit<4> z; +} + +struct headers { + data_t data; +} + + +control c(inout headers hdr) { + apply { + hdr.data.arr[0].nested_arr[1] = + hdr.data.arr[2].nested_arr[3]; + } +} + +top(c()) main; diff --git a/testdata/p4_16_samples_outputs/array3-first.p4 b/testdata/p4_16_samples_outputs/array3-first.p4 new file mode 100644 index 00000000000..fe5f1f08e55 --- /dev/null +++ b/testdata/p4_16_samples_outputs/array3-first.p4 @@ -0,0 +1,27 @@ +control proto

(inout P pkt); +package top

(proto

p); +struct S { + bit<16> f1; + bit<8>[16] nested_arr; + bit<16> f2; +} + +header data_t { + S[16] arr; + bit<4> w; + bit<4> x; + bit<4> y; + bit<4> z; +} + +struct headers { + data_t data; +} + +control c(inout headers hdr) { + apply { + hdr.data.arr[0].nested_arr[1] = hdr.data.arr[2].nested_arr[3]; + } +} + +top(c()) main; diff --git a/testdata/p4_16_samples_outputs/array3-frontend.p4 b/testdata/p4_16_samples_outputs/array3-frontend.p4 new file mode 100644 index 00000000000..fe5f1f08e55 --- /dev/null +++ b/testdata/p4_16_samples_outputs/array3-frontend.p4 @@ -0,0 +1,27 @@ +control proto

(inout P pkt); +package top

(proto

p); +struct S { + bit<16> f1; + bit<8>[16] nested_arr; + bit<16> f2; +} + +header data_t { + S[16] arr; + bit<4> w; + bit<4> x; + bit<4> y; + bit<4> z; +} + +struct headers { + data_t data; +} + +control c(inout headers hdr) { + apply { + hdr.data.arr[0].nested_arr[1] = hdr.data.arr[2].nested_arr[3]; + } +} + +top(c()) main; diff --git a/testdata/p4_16_samples_outputs/array3-midend.p4 b/testdata/p4_16_samples_outputs/array3-midend.p4 new file mode 100644 index 00000000000..58b57571e72 --- /dev/null +++ b/testdata/p4_16_samples_outputs/array3-midend.p4 @@ -0,0 +1,36 @@ +control proto

(inout P pkt); +package top

(proto

p); +struct S { + bit<16> f1; + bit<8>[16] nested_arr; + bit<16> f2; +} + +header data_t { + S[16] arr; + bit<4> w; + bit<4> x; + bit<4> y; + bit<4> z; +} + +struct headers { + data_t data; +} + +control c(inout headers hdr) { + @hidden action array3l25() { + hdr.data.arr[0].nested_arr[1] = hdr.data.arr[2].nested_arr[3]; + } + @hidden table tbl_array3l25 { + actions = { + array3l25(); + } + const default_action = array3l25(); + } + apply { + tbl_array3l25.apply(); + } +} + +top(c()) main; diff --git a/testdata/p4_16_samples_outputs/array3.p4 b/testdata/p4_16_samples_outputs/array3.p4 new file mode 100644 index 00000000000..c8d032bb310 --- /dev/null +++ b/testdata/p4_16_samples_outputs/array3.p4 @@ -0,0 +1,27 @@ +control proto

(inout P pkt); +package top

(proto

p); +struct S { + bit<16> f1; + bit<8>[16] nested_arr; + bit<16> f2; +} + +header data_t { + S[16] arr; + bit<4> w; + bit<4> x; + bit<4> y; + bit<4> z; +} + +struct headers { + data_t data; +} + +control c(inout headers hdr) { + apply { + hdr.data.arr[0].nested_arr[1] = hdr.data.arr[2].nested_arr[3]; + } +} + +top(c()) main; diff --git a/testdata/p4_16_samples_outputs/array3.p4-stderr b/testdata/p4_16_samples_outputs/array3.p4-stderr new file mode 100644 index 00000000000..e69de29bb2d