-
Notifications
You must be signed in to change notification settings - Fork 445
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
A few gtest cases for PSA #459
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,318 @@ | ||
/* | ||
Copyright 2013-present Barefoot Networks, 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. | ||
*/ | ||
|
||
#include "gtest/gtest.h" | ||
#include "ir/ir.h" | ||
#include "helper.h" | ||
#include "lib/log.h" | ||
|
||
#include "frontends/p4/typeMap.h" | ||
#include "frontends/common/resolveReferences/referenceMap.h" | ||
#include "frontends/common/resolveReferences/resolveReferences.h" | ||
|
||
#include "p4/createBuiltins.h" | ||
#include "p4/typeChecking/typeChecker.h" | ||
|
||
using namespace P4; | ||
|
||
TEST(arch, packet_out) { | ||
std::string program = R"( | ||
// simplified core.p4 | ||
extern packet_out { | ||
void emit<T> (in T hdr); | ||
} | ||
// simplified v1model.p4 | ||
control Deparser<H> (packet_out b, in H hdr); | ||
package PSA<H> (Deparser<H> p); | ||
// user program | ||
struct ParsedHeaders { | ||
bit<32> hdr; | ||
} | ||
control MyDeparser(packet_out b, in ParsedHeaders h){ | ||
apply { | ||
b.emit(h.hdr); | ||
} | ||
} | ||
PSA(MyDeparser()) main; | ||
)"; | ||
const IR::P4Program* pgm = parse_string(program); | ||
|
||
ReferenceMap refMap; | ||
TypeMap typeMap; | ||
PassManager passes ({ | ||
new TypeChecking(&refMap, &typeMap) | ||
}); | ||
|
||
pgm = pgm->apply(passes); | ||
ASSERT_NE(nullptr, pgm); | ||
} | ||
|
||
// Potential bug | ||
TEST(arch, duplicatedDeclarationBug) { | ||
std::string program = R"( | ||
// simplified core.p4 | ||
extern packet_out { | ||
void emit<T> (in T hdr); | ||
} | ||
// simplified v1model.p4 | ||
control Deparser<H> (packet_out b, in H hdr); | ||
package PSA<H> (Deparser<H> p); | ||
// user program | ||
struct ParsedHeaders { | ||
bit<32> hdr; | ||
} | ||
// BUG: duplicated name with 'Deparser' | ||
control Deparser(packet_out b, in ParsedHeaders h){ | ||
apply { | ||
b.emit(h.hdr); | ||
} | ||
} | ||
PSA(Deparser()) main; | ||
)"; | ||
const IR::P4Program* pgm = parse_string(program); | ||
|
||
ReferenceMap refMap; | ||
TypeMap typeMap; | ||
PassManager passes ({ | ||
new TypeChecking(&refMap, &typeMap) | ||
}); | ||
|
||
pgm = pgm->apply(passes); | ||
ASSERT_EQ(nullptr, pgm); | ||
} | ||
|
||
TEST(arch, instantiation) { | ||
std::string program = R"( | ||
// simplified core.p4 | ||
extern packet_in { | ||
void extract<T> (out T hdr); | ||
} | ||
extern packet_out { | ||
void emit<T> (in T hdr); | ||
} | ||
// simplified v1model.p4 | ||
parser Parser<M> (packet_in b, out M meta); | ||
control Ingress<H, M> (inout H hdr, inout M meta); | ||
control Deparser<H> (packet_out b, in H hdr); | ||
package PSA<H, M> (Parser<M> p, Ingress<H, M> ig, Deparser<H> dp); | ||
// user program | ||
struct ParsedHeaders { | ||
bit<32> hdr; | ||
} | ||
struct Metadata { | ||
bit<32> hdr; | ||
} | ||
parser MyParser(packet_in b, out Metadata m) { | ||
state start {} | ||
} | ||
control MyIngress(inout ParsedHeaders h, inout Metadata m) { | ||
apply { | ||
} | ||
} | ||
control MyDeparser(packet_out b, in ParsedHeaders h){ | ||
apply { | ||
b.emit(h.hdr); | ||
} | ||
} | ||
MyParser() p; | ||
MyIngress() ig; | ||
MyDeparser() dp; | ||
PSA(p, ig, dp) main; | ||
)"; | ||
const IR::P4Program* pgm = parse_string(program); | ||
|
||
ReferenceMap refMap; | ||
TypeMap typeMap; | ||
PassManager passes ({ | ||
new TypeChecking(&refMap, &typeMap) | ||
}); | ||
|
||
pgm = pgm->apply(passes); | ||
ASSERT_NE(nullptr, pgm); | ||
} | ||
|
||
TEST(arch, package_with_body) { | ||
std::string program = R"( | ||
// simplified core.p4 | ||
// simplified v1model.p4 | ||
control Ingress<H, M> (inout H hdr, inout M meta); | ||
package PSA<H, M> (Ingress<H, M> ig) {} | ||
// user program | ||
struct ParsedHeaders { | ||
bit<32> hdr; | ||
} | ||
struct Metadata { | ||
bit<32> hdr; | ||
} | ||
control MyIngress(inout ParsedHeaders h, inout Metadata m) (bit<32> x) { | ||
apply { | ||
} | ||
} | ||
MyIngress(2) ig; | ||
PSA(ig) main; | ||
)"; | ||
const IR::P4Program* pgm = parse_string(program); | ||
|
||
ReferenceMap refMap; | ||
TypeMap typeMap; | ||
PassManager passes ({ | ||
new TypeChecking(&refMap, &typeMap) | ||
}); | ||
pgm = pgm->apply(passes); | ||
ASSERT_EQ(nullptr, pgm); | ||
} | ||
|
||
TEST(arch, control_in_control) { | ||
std::string program = R"( | ||
// simplified core.p4 | ||
// simplified v1model.p4 | ||
control Ingress<H, M> (inout H hdr, inout M meta); | ||
control Egress<H, M> (inout M meta); | ||
package PSA<H, M> (Ingress<H, M> ig); | ||
// user program | ||
struct ParsedHeaders { | ||
bit<32> hdr; | ||
} | ||
struct Metadata { | ||
bit<32> hdr; | ||
} | ||
control MyIngress(inout ParsedHeaders h, inout Metadata m) { | ||
apply { | ||
} | ||
} | ||
control MyEgress(inout Metadata m) (MyIngress ig) { | ||
apply {} | ||
} | ||
MyIngress() ig; | ||
MyEgress(ig) eg; | ||
PSA(ig) main; | ||
)"; | ||
const IR::P4Program* pgm = parse_string(program); | ||
|
||
ReferenceMap refMap; | ||
TypeMap typeMap; | ||
PassManager passes ({ | ||
new TypeChecking(&refMap, &typeMap) | ||
}); | ||
pgm = pgm->apply(passes); | ||
ASSERT_NE(nullptr, pgm); | ||
} | ||
|
||
TEST(arch, psa_clone_as_param_to_package) { | ||
std::string program = R"( | ||
extern clone { | ||
clone(); | ||
void execute(in bit<32> sessions); | ||
} | ||
package PSA<H, M> (clone c); | ||
// user program | ||
struct ParsedHeaders { | ||
bit<32> hdr; | ||
} | ||
clone() c; | ||
PSA(c) main; | ||
)"; | ||
const IR::P4Program* pgm = parse_string(program); | ||
|
||
ReferenceMap refMap; | ||
TypeMap typeMap; | ||
PassManager passes ({ | ||
new TypeChecking(&refMap, &typeMap) | ||
}); | ||
pgm = pgm->apply(passes); | ||
ASSERT_NE(nullptr, pgm); | ||
} | ||
|
||
TEST(arch, psa_clone_as_param_to_control) { | ||
std::string program = R"( | ||
extern clone<T> { | ||
// constructor | ||
clone(); | ||
// methods | ||
void clone(in bit<32> sessions); | ||
void clone(in bit<32> sessions, in T data); | ||
} | ||
control ingress<H, M> (inout H hdr, inout M meta); | ||
package PSA<H, M> (ingress<H,M> ig); | ||
// user program | ||
struct ParsedHeaders { | ||
bit<32> hdr; | ||
} | ||
struct Metadata { | ||
bit<32> md; | ||
} | ||
control MyIngress (inout ParsedHeaders p, inout Metadata m) (clone<bit<32>> c) { | ||
apply{} | ||
} | ||
MyIngress(clone<bit<32>>()) ig; | ||
PSA(ig) main; | ||
)"; | ||
const IR::P4Program* pgm = parse_string(program); | ||
|
||
ReferenceMap refMap; | ||
TypeMap typeMap; | ||
PassManager passes ({ | ||
new TypeChecking(&refMap, &typeMap) | ||
}); | ||
pgm = pgm->apply(passes); | ||
ASSERT_NE(nullptr, pgm); | ||
|
||
dump(pgm); | ||
} | ||
|
||
TEST(arch, psa_clone_as_param_to_extern) { | ||
std::string program = R"( | ||
extern clone<T> { | ||
// constructor | ||
clone(); | ||
// methods | ||
void clone(in bit<32> sessions); | ||
void clone(in bit<32> sessions, in T data); | ||
} | ||
extern PRE<T> { | ||
PRE(); | ||
void clone(in bit<32> sessions); /* same as invoking c.clone(sessions); */ | ||
void clone(in bit<32> sessions, in T data); /* same as invoking c.clone(sessions, data) */ | ||
// ... | ||
// drop, multicast api here | ||
} | ||
control ingress<H, M> (inout H hdr, inout M meta); | ||
package PSA<H, M> (ingress<H,M> ig); | ||
// user program | ||
struct ParsedHeaders { | ||
bit<32> hdr; | ||
} | ||
struct Metadata { | ||
bit<32> md; | ||
} | ||
control MyIngress (inout ParsedHeaders p, inout Metadata m) (PRE<bit<32>> pre) { | ||
apply{} | ||
} | ||
PRE<bit<32>>() pre; | ||
MyIngress(pre) ig; | ||
PSA(ig) main; | ||
)"; | ||
const IR::P4Program* pgm = parse_string(program); | ||
|
||
ReferenceMap refMap; | ||
TypeMap typeMap; | ||
PassManager passes ({ | ||
new TypeChecking(&refMap, &typeMap) | ||
}); | ||
pgm = pgm->apply(passes); | ||
ASSERT_NE(nullptr, pgm); | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe that this is truly wasteful.
It is much simpler to just add a file containing the two declarations into testdata/p4_16_errors.
This will not only test that the compilation fails, but it will also check that the error message is the correct one. It also requires much less code: no c++, no "simplified" core.p4.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The point is to test the simplified core/psa stuff. The goal is ultimately to get a p4c-bmv2 that can generate code for either simple_switch or psa (depending on which header is included) and have it "just work"