Skip to content

Commit

Permalink
Switch statement not correctly translated when default case is not pr…
Browse files Browse the repository at this point in the history
…esent
  • Loading branch information
github-sajan committed Dec 14, 2021
1 parent 37cd30e commit fd53229
Show file tree
Hide file tree
Showing 88 changed files with 2,000 additions and 561 deletions.
13 changes: 13 additions & 0 deletions midend/eliminateSwitch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,19 @@ const IR::Node* DoEliminateSwitch::postorder(IR::SwitchStatement* statement) {
properties.push_back(new IR::Property(src, "key", new IR::Key({tableKeyEl}), false));
IR::ID defaultAction = P4CoreLibrary::instance.noAction.Id();

// Add Default switch Expression if not present
bool isDefaultPresent = false;
for (auto sc : statement->cases) {
if (sc->label->is<IR::DefaultExpression>()) {
isDefaultPresent = true;
break;
}
}
if (!isDefaultPresent) {
statement->cases.push_back(
new IR::SwitchCase(new IR::DefaultExpression(IR::Type_Dontcare::get()),
new IR::BlockStatement));
}
// Create actions
IR::Vector<IR::Expression> pendingLabels; // switch labels with no statement
for (auto sc : statement->cases) {
Expand Down
5 changes: 3 additions & 2 deletions testdata/p4_14_samples_outputs/copy_to_cpu-first.p4
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ struct metadata {
}

struct headers {
@name(".cpu_header")
@name(".cpu_header")
cpu_header_t cpu_header;
@name(".ethernet")
@name(".ethernet")
ethernet_t ethernet;
}

Expand Down Expand Up @@ -110,3 +110,4 @@ control computeChecksum(inout headers hdr, inout metadata meta) {
}

V1Switch<headers, metadata>(ParserImpl(), verifyChecksum(), ingress(), egress(), computeChecksum(), DeparserImpl()) main;

5 changes: 3 additions & 2 deletions testdata/p4_14_samples_outputs/copy_to_cpu-frontend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ struct metadata {
}

struct headers {
@name(".cpu_header")
@name(".cpu_header")
cpu_header_t cpu_header;
@name(".ethernet")
@name(".ethernet")
ethernet_t ethernet;
}

Expand Down Expand Up @@ -118,3 +118,4 @@ control computeChecksum(inout headers hdr, inout metadata meta) {
}

V1Switch<headers, metadata>(ParserImpl(), verifyChecksum(), ingress(), egress(), computeChecksum(), DeparserImpl()) main;

5 changes: 3 additions & 2 deletions testdata/p4_14_samples_outputs/copy_to_cpu-midend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ struct metadata {
}

struct headers {
@name(".cpu_header")
@name(".cpu_header")
cpu_header_t cpu_header;
@name(".ethernet")
@name(".ethernet")
ethernet_t ethernet;
}

Expand Down Expand Up @@ -112,3 +112,4 @@ control computeChecksum(inout headers hdr, inout metadata meta) {
}

V1Switch<headers, metadata>(ParserImpl(), verifyChecksum(), ingress(), egress(), computeChecksum(), DeparserImpl()) main;

5 changes: 3 additions & 2 deletions testdata/p4_14_samples_outputs/copy_to_cpu.p4
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ struct metadata {
}

struct headers {
@name(".cpu_header")
@name(".cpu_header")
cpu_header_t cpu_header;
@name(".ethernet")
@name(".ethernet")
ethernet_t ethernet;
}

Expand Down Expand Up @@ -106,3 +106,4 @@ control computeChecksum(inout headers hdr, inout metadata meta) {
}

V1Switch(ParserImpl(), verifyChecksum(), ingress(), egress(), computeChecksum(), DeparserImpl()) main;

17 changes: 9 additions & 8 deletions testdata/p4_14_samples_outputs/p414-special-ops-2-bmv2-first.p4
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ struct intrinsic_metadata_t {
}

struct mymeta_t {
@field_list(FieldLists.clone_e2e_FL, FieldLists.recirculate_FL, FieldLists.resubmit_FL)
@field_list(FieldLists.clone_e2e_FL, FieldLists.recirculate_FL, FieldLists.resubmit_FL)
bit<8> resubmit_count;
@field_list(FieldLists.clone_e2e_FL, FieldLists.recirculate_FL, FieldLists.resubmit_FL)
@field_list(FieldLists.clone_e2e_FL, FieldLists.recirculate_FL, FieldLists.resubmit_FL)
bit<8> recirculate_count;
@field_list(FieldLists.clone_e2e_FL, FieldLists.recirculate_FL, FieldLists.resubmit_FL)
@field_list(FieldLists.clone_e2e_FL, FieldLists.recirculate_FL, FieldLists.resubmit_FL)
bit<8> clone_e2e_count;
@field_list(FieldLists.clone_e2e_FL, FieldLists.recirculate_FL, FieldLists.resubmit_FL)
@field_list(FieldLists.clone_e2e_FL, FieldLists.recirculate_FL, FieldLists.resubmit_FL)
bit<8> last_ing_instance_type;
@field_list(FieldLists.clone_e2e_FL, FieldLists.recirculate_FL, FieldLists.resubmit_FL)
@field_list(FieldLists.clone_e2e_FL, FieldLists.recirculate_FL, FieldLists.resubmit_FL)
bit<8> f1;
}

Expand All @@ -39,14 +39,14 @@ header ethernet_t {
}

struct metadata {
@name(".mymeta")
@name(".mymeta")
mymeta_t mymeta;
@name(".temporaries")
@name(".temporaries")
temporaries_t temporaries;
}

struct headers {
@name(".ethernet")
@name(".ethernet")
ethernet_t ethernet;
}

Expand Down Expand Up @@ -357,3 +357,4 @@ control computeChecksum(inout headers hdr, inout metadata meta) {
}

V1Switch<headers, metadata>(ParserImpl(), verifyChecksum(), ingress(), egress(), computeChecksum(), DeparserImpl()) main;

Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ struct intrinsic_metadata_t {
}

struct mymeta_t {
@field_list(FieldLists.clone_e2e_FL, FieldLists.recirculate_FL, FieldLists.resubmit_FL)
@field_list(FieldLists.clone_e2e_FL, FieldLists.recirculate_FL, FieldLists.resubmit_FL)
bit<8> resubmit_count;
@field_list(FieldLists.clone_e2e_FL, FieldLists.recirculate_FL, FieldLists.resubmit_FL)
@field_list(FieldLists.clone_e2e_FL, FieldLists.recirculate_FL, FieldLists.resubmit_FL)
bit<8> recirculate_count;
@field_list(FieldLists.clone_e2e_FL, FieldLists.recirculate_FL, FieldLists.resubmit_FL)
@field_list(FieldLists.clone_e2e_FL, FieldLists.recirculate_FL, FieldLists.resubmit_FL)
bit<8> clone_e2e_count;
@field_list(FieldLists.clone_e2e_FL, FieldLists.recirculate_FL, FieldLists.resubmit_FL)
@field_list(FieldLists.clone_e2e_FL, FieldLists.recirculate_FL, FieldLists.resubmit_FL)
bit<8> last_ing_instance_type;
@field_list(FieldLists.clone_e2e_FL, FieldLists.recirculate_FL, FieldLists.resubmit_FL)
@field_list(FieldLists.clone_e2e_FL, FieldLists.recirculate_FL, FieldLists.resubmit_FL)
bit<8> f1;
}

Expand All @@ -39,14 +39,14 @@ header ethernet_t {
}

struct metadata {
@name(".mymeta")
@name(".mymeta")
mymeta_t mymeta;
@name(".temporaries")
@name(".temporaries")
temporaries_t temporaries;
}

struct headers {
@name(".ethernet")
@name(".ethernet")
ethernet_t ethernet;
}

Expand Down Expand Up @@ -388,3 +388,4 @@ control computeChecksum(inout headers hdr, inout metadata meta) {
}

V1Switch<headers, metadata>(ParserImpl(), verifyChecksum(), ingress(), egress(), computeChecksum(), DeparserImpl()) main;

23 changes: 12 additions & 11 deletions testdata/p4_14_samples_outputs/p414-special-ops-2-bmv2-midend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ struct intrinsic_metadata_t {
}

struct mymeta_t {
@field_list(8w0, 8w1, 8w2)
@field_list(8w0, 8w1, 8w2)
bit<8> resubmit_count;
@field_list(8w0, 8w1, 8w2)
@field_list(8w0, 8w1, 8w2)
bit<8> recirculate_count;
@field_list(8w0, 8w1, 8w2)
@field_list(8w0, 8w1, 8w2)
bit<8> clone_e2e_count;
@field_list(8w0, 8w1, 8w2)
@field_list(8w0, 8w1, 8w2)
bit<8> last_ing_instance_type;
@field_list(8w0, 8w1, 8w2)
@field_list(8w0, 8w1, 8w2)
bit<8> f1;
}

Expand All @@ -33,21 +33,21 @@ header ethernet_t {
}

struct metadata {
@field_list(8w0, 8w1, 8w2)
@field_list(8w0, 8w1, 8w2)
bit<8> _mymeta_resubmit_count0;
@field_list(8w0, 8w1, 8w2)
@field_list(8w0, 8w1, 8w2)
bit<8> _mymeta_recirculate_count1;
@field_list(8w0, 8w1, 8w2)
@field_list(8w0, 8w1, 8w2)
bit<8> _mymeta_clone_e2e_count2;
@field_list(8w0, 8w1, 8w2)
@field_list(8w0, 8w1, 8w2)
bit<8> _mymeta_last_ing_instance_type3;
@field_list(8w0, 8w1, 8w2)
@field_list(8w0, 8w1, 8w2)
bit<8> _mymeta_f14;
bit<48> _temporaries_temp15;
}

struct headers {
@name(".ethernet")
@name(".ethernet")
ethernet_t ethernet;
}

Expand Down Expand Up @@ -389,3 +389,4 @@ control computeChecksum(inout headers hdr, inout metadata meta) {
}

V1Switch<headers, metadata>(ParserImpl(), verifyChecksum(), ingress(), egress(), computeChecksum(), DeparserImpl()) main;

17 changes: 9 additions & 8 deletions testdata/p4_14_samples_outputs/p414-special-ops-2-bmv2.p4
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ struct intrinsic_metadata_t {
}

struct mymeta_t {
@field_list(FieldLists.clone_e2e_FL, FieldLists.recirculate_FL, FieldLists.resubmit_FL)
@field_list(FieldLists.clone_e2e_FL, FieldLists.recirculate_FL, FieldLists.resubmit_FL)
bit<8> resubmit_count;
@field_list(FieldLists.clone_e2e_FL, FieldLists.recirculate_FL, FieldLists.resubmit_FL)
@field_list(FieldLists.clone_e2e_FL, FieldLists.recirculate_FL, FieldLists.resubmit_FL)
bit<8> recirculate_count;
@field_list(FieldLists.clone_e2e_FL, FieldLists.recirculate_FL, FieldLists.resubmit_FL)
@field_list(FieldLists.clone_e2e_FL, FieldLists.recirculate_FL, FieldLists.resubmit_FL)
bit<8> clone_e2e_count;
@field_list(FieldLists.clone_e2e_FL, FieldLists.recirculate_FL, FieldLists.resubmit_FL)
@field_list(FieldLists.clone_e2e_FL, FieldLists.recirculate_FL, FieldLists.resubmit_FL)
bit<8> last_ing_instance_type;
@field_list(FieldLists.clone_e2e_FL, FieldLists.recirculate_FL, FieldLists.resubmit_FL)
@field_list(FieldLists.clone_e2e_FL, FieldLists.recirculate_FL, FieldLists.resubmit_FL)
bit<8> f1;
}

Expand All @@ -39,14 +39,14 @@ header ethernet_t {
}

struct metadata {
@name(".mymeta")
@name(".mymeta")
mymeta_t mymeta;
@name(".temporaries")
@name(".temporaries")
temporaries_t temporaries;
}

struct headers {
@name(".ethernet")
@name(".ethernet")
ethernet_t ethernet;
}

Expand Down Expand Up @@ -361,3 +361,4 @@ control computeChecksum(inout headers hdr, inout metadata meta) {
}

V1Switch(ParserImpl(), verifyChecksum(), ingress(), egress(), computeChecksum(), DeparserImpl()) main;

15 changes: 8 additions & 7 deletions testdata/p4_14_samples_outputs/p414-special-ops-3-bmv2-first.p4
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ struct intrinsic_metadata_t {
}

struct mymeta_t {
@field_list(FieldLists.resubmit_FL)
@field_list(FieldLists.resubmit_FL)
bit<8> resubmit_count;
@field_list(FieldLists.recirculate_FL)
@field_list(FieldLists.recirculate_FL)
bit<8> recirculate_count;
@field_list(FieldLists.clone_e2e_FL)
@field_list(FieldLists.clone_e2e_FL)
bit<8> clone_e2e_count;
bit<8> last_ing_instance_type;
@field_list(FieldLists.clone_e2e_FL, FieldLists.recirculate_FL, FieldLists.resubmit_FL)
@field_list(FieldLists.clone_e2e_FL, FieldLists.recirculate_FL, FieldLists.resubmit_FL)
bit<8> f1;
}

Expand All @@ -38,14 +38,14 @@ header ethernet_t {
}

struct metadata {
@name(".mymeta")
@name(".mymeta")
mymeta_t mymeta;
@name(".temporaries")
@name(".temporaries")
temporaries_t temporaries;
}

struct headers {
@name(".ethernet")
@name(".ethernet")
ethernet_t ethernet;
}

Expand Down Expand Up @@ -381,3 +381,4 @@ control computeChecksum(inout headers hdr, inout metadata meta) {
}

V1Switch<headers, metadata>(ParserImpl(), verifyChecksum(), ingress(), egress(), computeChecksum(), DeparserImpl()) main;

Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ struct intrinsic_metadata_t {
}

struct mymeta_t {
@field_list(FieldLists.resubmit_FL)
@field_list(FieldLists.resubmit_FL)
bit<8> resubmit_count;
@field_list(FieldLists.recirculate_FL)
@field_list(FieldLists.recirculate_FL)
bit<8> recirculate_count;
@field_list(FieldLists.clone_e2e_FL)
@field_list(FieldLists.clone_e2e_FL)
bit<8> clone_e2e_count;
bit<8> last_ing_instance_type;
@field_list(FieldLists.clone_e2e_FL, FieldLists.recirculate_FL, FieldLists.resubmit_FL)
@field_list(FieldLists.clone_e2e_FL, FieldLists.recirculate_FL, FieldLists.resubmit_FL)
bit<8> f1;
}

Expand All @@ -38,14 +38,14 @@ header ethernet_t {
}

struct metadata {
@name(".mymeta")
@name(".mymeta")
mymeta_t mymeta;
@name(".temporaries")
@name(".temporaries")
temporaries_t temporaries;
}

struct headers {
@name(".ethernet")
@name(".ethernet")
ethernet_t ethernet;
}

Expand Down Expand Up @@ -412,3 +412,4 @@ control computeChecksum(inout headers hdr, inout metadata meta) {
}

V1Switch<headers, metadata>(ParserImpl(), verifyChecksum(), ingress(), egress(), computeChecksum(), DeparserImpl()) main;

Loading

0 comments on commit fd53229

Please sign in to comment.