Skip to content
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

Support for set<> type #123

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions frontends/common/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,7 @@ FILE* CompilerOptions::preprocess() {
void CompilerOptions::closeInput(FILE* inputStream) const {
if (close_input) {
int exitCode = pclose(inputStream);
if (WIFEXITED(exitCode) && WEXITSTATUS(exitCode) == 4)
::error("input file %s does not exist", file);
else if (exitCode != 0)
if (exitCode != 0)
::error("Preprocessor returned exit code %d; aborting compilation", exitCode);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why remove this change?

}
Expand Down
1 change: 1 addition & 0 deletions frontends/p4/p4-lex.l
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ std::string stringLiteral;
"package" { BEGIN(NORMAL); return PACKAGE; }
"return" { BEGIN(NORMAL); return RETURN; }
"select" { BEGIN(NORMAL); return SELECT; }
"set" { BEGIN(NORMAL); return SET; }
"state" { BEGIN(NORMAL); return STATE; }
"struct" { BEGIN(NORMAL); return STRUCT; }
"switch" { BEGIN(NORMAL); return SWITCH; }
Expand Down
9 changes: 7 additions & 2 deletions frontends/p4/p4-parse.ypp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ static int yylex();
%token ABSTRACT ACTION ACTIONS APPLY BOOL BIT CONST CONTROL DATA
DEFAULT DONTCARE ELSE ENUM T_ERROR // There is an ERROR macro elsewhere
EXIT EXTERN HEADER HEADER_UNION IF IN INOUT INT KEY MASK SELECT MATCH_KIND OUT
PACKAGE PARSER RANGE RETURN STATE STRUCT SWITCH TABLE TRANSITION
PACKAGE PARSER RANGE RETURN SET STATE STRUCT SWITCH TABLE TRANSITION
TUPLE TYPEDEF VARBIT VOID
%token<str> IDENTIFIER TYPE NAMESPACE STRING_LITERAL
%token<Constant> INTEGER
Expand All @@ -132,7 +132,7 @@ static int yylex();
argument simpleKeysetExpression transitionStatement switchLabel
%type<PathPrefix> pathPrefix relativePathPrefix nonEmptyRelativePathPrefix
%type<TypePtr> baseType typeOrVoid specializedType headerStackType typeRef
tupleType typeArg
tupleType typeArg setType
%type<Type_Name> typeName
%type<Parameter> parameter
%type<annos> optAnnotations
Expand Down Expand Up @@ -541,6 +541,7 @@ typeRef
| specializedType { $$ = $1; }
| headerStackType { $$ = $1; }
| tupleType { $$ = $1; }
| setType { $$ = $1; }
;

typeName
Expand All @@ -552,6 +553,10 @@ tupleType
: TUPLE '<' typeArgumentList '>' { $$ = new IR::Type_Tuple(@1+@4, $3); }
;

setType
: SET '<' typeRef '>' { $$ = new IR::Type_Set(@1+@4, $3); }
;

headerStackType
: typeName '[' expression ']' { $$ = new IR::Type_Stack(@1+@4, $1, $3); }
;
Expand Down
11 changes: 6 additions & 5 deletions frontends/p4/reservedWords.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2013-present Barefoot Networks, Inc.
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.
Expand All @@ -20,11 +20,12 @@ namespace P4 {

// Keep this in sync with the lexer
std::set<cstring> reservedWords = {
"abstract", // experimental
"action", "actions", "apply", "bool", "bit", "const", "control", "default",
"else", "enum", "error", "exit", "extern", "false", "header", "header_union", "if",
"in", "inout", "int", "key", "match_kind", "out", "parser", "package",
"return", "select", "state", "struct", "switch", "table", "transition",
"true", "typedef", "varbit", "void"
"else", "enum", "error", "exit", "extern", "false", "header", "header_union", // experimental
"if", "in", "inout", "int", "key", "match_kind", "out", "parser", "package",
"return", "select", "set", "state", "struct", "switch", "table", "this", // experimental
"transition", "true", "tuple", "typedef", "varbit", "void", "_"
};

} // namespace P4
19 changes: 19 additions & 0 deletions frontends/p4/typeChecking/typeChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,15 @@ const IR::Type* TypeInference::canonicalize(const IR::Type* type) {
type->is<IR::Type_ActionEnum>() ||
type->is<IR::Type_MatchKind>()) {
return type;
} else if (type->is<IR::Type_Set>()) {
auto set = type->to<IR::Type_Set>();
auto et = canonicalize(set->elementType);
if (et == nullptr)
return nullptr;
if (et == set->elementType)
return type;
const IR::Type* canon = new IR::Type_Set(type->srcInfo, et);
return canon;
} else if (type->is<IR::Type_Tuple>()) {
auto tuple = type->to<IR::Type_Tuple>();
auto fields = new IR::Vector<IR::Type>();
Expand Down Expand Up @@ -1016,6 +1025,16 @@ const IR::Node* TypeInference::postorder(IR::Type_Tuple* type) {
return type;
}

const IR::Node* TypeInference::postorder(IR::Type_Set* type) {
if (done()) return type;
auto canon = canonicalize(getOriginal<IR::Type_Set>());
if (canon != nullptr) {
setType(getOriginal(), canon);
setType(type, canon);
}
return type;
}

const IR::Node* TypeInference::postorder(IR::Type_Extern* type) {
if (done()) return type;
auto canon = canonicalize(getOriginal<IR::Type_Extern>())->to<IR::Type_Extern>();
Expand Down
1 change: 1 addition & 0 deletions frontends/p4/typeChecking/typeChecker.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ class TypeInference : public Transform {
const IR::Node* postorder(IR::Type_Specialized* type) override;
const IR::Node* postorder(IR::Type_SpecializedCanonical* type) override;
const IR::Node* postorder(IR::Type_Tuple* type) override;
const IR::Node* postorder(IR::Type_Set* type) override;
const IR::Node* postorder(IR::Type_ArchBlock* type) override;
const IR::Node* postorder(IR::Type_Package* type) override;
const IR::Node* postorder(IR::Type_ActionEnum* type) override;
Expand Down
5 changes: 5 additions & 0 deletions frontends/p4/typeMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ bool TypeMap::equivalent(const IR::Type* left, const IR::Type* right) {
}
return true;
}
if (left->is<IR::Type_Set>()) {
auto lt = left->to<IR::Type_Set>();
auto rt = right->to<IR::Type_Set>();
return equivalent(lt->elementType, rt->elementType);
}
if (left->is<IR::Type_Package>()) {
auto lp = left->to<IR::Type_Package>();
auto rp = right->to<IR::Type_Package>();
Expand Down
4 changes: 2 additions & 2 deletions testdata/p4_16_samples/methodArgCast.p4
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ limitations under the License.
*/
extern E {
E();
void set(in bit<32> arg);
void setValue(in bit<32> arg);
}

control c() {
E() e;
apply {
e.set(10);
e.setValue(10);
}
}

Expand Down
17 changes: 17 additions & 0 deletions testdata/p4_16_samples/set.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
Copyright 2016 VMware, 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.
*/

typedef set<bit<32>> IntSet;
4 changes: 2 additions & 2 deletions testdata/p4_16_samples/value-sets.p4
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ parser TopParser(packet_in b, out Parsed_packet p) {
}

state dispatch_value_sets {
bit<8> set = ethtype_kinds.index(p.ethernet.etherType);
transition select(set) {
bit<8> setIndex = ethtype_kinds.index(p.ethernet.etherType);
transition select(setIndex) {
ETH_KIND_TRILL: parse_trill;
ETH_KIND_TPID: parse_vlan_tag;
}
Expand Down
59 changes: 59 additions & 0 deletions testdata/p4_16_samples/value-sets1.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
Copyright 2016 VMware, 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 <core.p4>

header Ethernet_h {
bit<48> dstAddr;
bit<48> srcAddr;
bit<16> etherType;
}

struct Parsed_packet {
Ethernet_h ethernet;
}

extern ValueSet<T> {
ValueSet();
set<T> members();
}

parser TopParser(packet_in b, out Parsed_packet p) {
ValueSet<bit<16>>() trill;
ValueSet<bit<16>>() tpid;

state start {
b.extract(p.ethernet);
transition select(p.ethernet.etherType) {
16w0x0800: parse_ipv4;
16w0x0806: parse_arp;
16w0x86DD: parse_ipv6;
trill.members(): parse_trill;
tpid.members(): parse_vlan_tag;
}
}

state parse_ipv4 { transition accept; }
state parse_arp { transition accept; }
state parse_ipv6 { transition accept; }
state parse_trill { transition accept; }
state parse_vlan_tag { transition accept; }
}

parser proto<T>(packet_in p, out T h);
package top<T>(proto<T> _p);

top(TopParser()) main;
4 changes: 2 additions & 2 deletions testdata/p4_16_samples_outputs/methodArgCast-first.p4
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
extern E {
E();
void set(in bit<32> arg);
void setValue(in bit<32> arg);
}

control c() {
E() e;
apply {
e.set(32w10);
e.setValue(32w10);
}
}

Expand Down
4 changes: 2 additions & 2 deletions testdata/p4_16_samples_outputs/methodArgCast-frontend.p4
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
extern E {
E();
void set(in bit<32> arg);
void setValue(in bit<32> arg);
}

control c() {
@name("e") E() e_0;
apply {
e_0.set(32w10);
e_0.setValue(32w10);
}
}

Expand Down
4 changes: 2 additions & 2 deletions testdata/p4_16_samples_outputs/methodArgCast-midend.p4
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
extern E {
E();
void set(in bit<32> arg);
void setValue(in bit<32> arg);
}

control c() {
@name("e") E() e;
action act() {
e.set(32w10);
e.setValue(32w10);
}
table tbl_act() {
actions = {
Expand Down
4 changes: 2 additions & 2 deletions testdata/p4_16_samples_outputs/methodArgCast.p4
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
extern E {
E();
void set(in bit<32> arg);
void setValue(in bit<32> arg);
}

control c() {
E() e;
apply {
e.set(10);
e.setValue(10);
}
}

Expand Down
1 change: 1 addition & 0 deletions testdata/p4_16_samples_outputs/set-first.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
typedef bit<32> IntSet;
Empty file.
1 change: 1 addition & 0 deletions testdata/p4_16_samples_outputs/set.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
typedef bit<32> IntSet;
1 change: 1 addition & 0 deletions testdata/p4_16_samples_outputs/set.p4-stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
warning: Program does not contain a `main' module
4 changes: 2 additions & 2 deletions testdata/p4_16_samples_outputs/value-sets-first.p4
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ parser TopParser(packet_in b, out Parsed_packet p) {
}
}
state dispatch_value_sets {
bit<8> set = ethtype_kinds.index(p.ethernet.etherType);
transition select(set) {
bit<8> setIndex = ethtype_kinds.index(p.ethernet.etherType);
transition select(setIndex) {
8w1: parse_trill;
8w2: parse_vlan_tag;
}
Expand Down
6 changes: 3 additions & 3 deletions testdata/p4_16_samples_outputs/value-sets-frontend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ extern ValueSet {
}

parser TopParser(packet_in b, out Parsed_packet p) {
bit<8> set_0;
bit<8> setIndex_0;
bit<8> tmp;
@name("ethtype_kinds") ValueSet(32w5) ethtype_kinds_0;
state start {
Expand All @@ -30,8 +30,8 @@ parser TopParser(packet_in b, out Parsed_packet p) {
}
state dispatch_value_sets {
tmp = ethtype_kinds_0.index(p.ethernet.etherType);
set_0 = tmp;
transition select(set_0) {
setIndex_0 = tmp;
transition select(setIndex_0) {
8w1: parse_trill;
8w2: parse_vlan_tag;
}
Expand Down
6 changes: 3 additions & 3 deletions testdata/p4_16_samples_outputs/value-sets-midend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ extern ValueSet {
}

parser TopParser(packet_in b, out Parsed_packet p) {
bit<8> set;
bit<8> setIndex;
bit<8> tmp_0;
@name("ethtype_kinds") ValueSet(32w5) ethtype_kinds;
state start {
Expand All @@ -30,8 +30,8 @@ parser TopParser(packet_in b, out Parsed_packet p) {
}
state dispatch_value_sets {
tmp_0 = ethtype_kinds.index(p.ethernet.etherType);
set = tmp_0;
transition select(set) {
setIndex = tmp_0;
transition select(setIndex) {
8w1: parse_trill;
8w2: parse_vlan_tag;
}
Expand Down
4 changes: 2 additions & 2 deletions testdata/p4_16_samples_outputs/value-sets.p4
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ parser TopParser(packet_in b, out Parsed_packet p) {
}
}
state dispatch_value_sets {
bit<8> set = ethtype_kinds.index(p.ethernet.etherType);
transition select(set) {
bit<8> setIndex = ethtype_kinds.index(p.ethernet.etherType);
transition select(setIndex) {
1: parse_trill;
2: parse_vlan_tag;
}
Expand Down
Loading