Skip to content

Commit

Permalink
Clean up typeCheckTypes a bit; another test
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisDodd committed Feb 3, 2025
1 parent fb1888a commit 01ee268
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 12 deletions.
16 changes: 4 additions & 12 deletions frontends/p4/typeChecking/typeCheckTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<IR::Type_Stack>()) type = st->elementType;
if (type->is<IR::Type_Bits>() || type->is<IR::Type_Boolean>() || type->is<IR::Type_SerEnum>()) {
return true;
} else if (auto ht = type->to<IR::Type_Struct>()) {
Expand Down Expand Up @@ -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<IR::Type_Newtype>())
t = getTypeType(t->to<IR::Type_Newtype>()->type);
else if (auto *st = t->to<IR::Type_Stack>())
t = st->elementType;
else
break;
}
return t->is<IR::Type_Bits>() || t->is<IR::Type_Varbits>() ||
(t->is<IR::Type_Struct>() && onlyBitsOrBitStructs(t)) || t->is<IR::Type_SerEnum>() ||
t->is<IR::Type_Boolean>() || t->is<IR::Type_Var>() ||
t->is<IR::Type_SpecializedCanonical>();
while (t->is<IR::Type_Newtype>()) t = getTypeType(t->to<IR::Type_Newtype>()->type);
return onlyBitsOrBitStructs(t) || t->is<IR::Type_Varbits>() ||
t->is<IR::Type_Var>() || t->is<IR::Type_SpecializedCanonical>();
};
validateFields(canon, validator);
return type;
Expand Down
30 changes: 30 additions & 0 deletions testdata/p4_16_samples/array3.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
control proto<P>(inout P pkt);
package top<P>(proto<P> 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;
27 changes: 27 additions & 0 deletions testdata/p4_16_samples_outputs/array3-first.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
control proto<P>(inout P pkt);
package top<P>(proto<P> 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<headers>(c()) main;
27 changes: 27 additions & 0 deletions testdata/p4_16_samples_outputs/array3-frontend.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
control proto<P>(inout P pkt);
package top<P>(proto<P> 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<headers>(c()) main;
36 changes: 36 additions & 0 deletions testdata/p4_16_samples_outputs/array3-midend.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
control proto<P>(inout P pkt);
package top<P>(proto<P> 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<headers>(c()) main;
27 changes: 27 additions & 0 deletions testdata/p4_16_samples_outputs/array3.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
control proto<P>(inout P pkt);
package top<P>(proto<P> 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;
Empty file.

0 comments on commit 01ee268

Please sign in to comment.