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

P4 spec issue 1261 add bounded loops #4120

Closed
Show file tree
Hide file tree
Changes from all commits
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
82 changes: 70 additions & 12 deletions frontends/parsers/p4/p4parser.ypp
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ inline std::ostream& operator<<(std::ostream& out, const P4::Token& t) {
%token<Token> RANGE ".."
%token<Token> TRUE FALSE THIS
%token<Token> ABSTRACT ACTION ACTIONS APPLY BOOL BIT CONST CONTROL DEFAULT
ELSE ENTRIES ENUM ERROR EXIT EXTERN HEADER HEADER_UNION IF IN INOUT
ELSE ENTRIES ENUM ERROR EXIT EXTERN FOR HEADER HEADER_UNION IF IN INOUT
INT KEY LIST SELECT MATCH_KIND TYPE OUT PACKAGE PARSER PRAGMA PRIORITY RETURN
STATE STRING STRUCT SWITCH TABLE TRANSITION TUPLE TYPEDEF VARBIT VALUESET VOID

Expand All @@ -247,7 +247,7 @@ inline std::ostream& operator<<(std::ostream& out, const P4::Token& t) {
stateExpression optInitializer initializer
simpleKeysetExpression transitionStatement switchLabel
p4rtControllerType reducedSimpleKeysetExpression entryPriority
nonBraceExpression
nonBraceExpression forCollectionExpr
%type<ConstType*> baseType typeOrVoid specializedType headerStackType
typeRef tupleType typeArg realTypeArg namedType p4listType
%type<IR::Type_Name*> typeName
Expand All @@ -270,19 +270,23 @@ inline std::ostream& operator<<(std::ostream& out, const P4::Token& t) {
%type<IR::Statement*> statement emptyStatement returnStatement
switchStatement exitStatement
assignmentOrMethodCallStatement conditionalStatement
assignmentOrMethodCallStatementWithoutSemicolon
directApplication
forStatement
%type<IR::BlockStatement*> blockStatement parserBlockStatement controlBody
objInitializer
%type<IR::StatOrDecl*> statementOrDeclaration parserStatement
%type<IR::IndexedVector<IR::StatOrDecl>*> objDeclarations statOrDeclList
parserStatements
declOrAssignmentOrMethodCallStatement
%type<IR::IndexedVector<IR::StatOrDecl>*> objDeclarations statOrDeclList parserStatements
forInitStatements forInitStatementsNonEmpty
forUpdateStatements forUpdateStatementsNonEmpty
%type<IR::SwitchCase*> switchCase
%type<IR::Vector<IR::SwitchCase>*> switchCases
%type<IR::Vector<IR::Type>*> typeArgumentList realTypeArgumentList
%type<IR::ParserState*> parserState
%type<IR::IndexedVector<IR::ParserState>*> parserStates
%type<IR::Declaration*> constantDeclaration actionDeclaration
variableDeclaration instantiation functionDeclaration
variableDeclaration variableDeclarationWithoutSemicolon instantiation functionDeclaration
objDeclaration tableDeclaration controlLocalDeclaration
parserLocalElement valueSetDeclaration
%type<IR::Type_Declaration*> headerTypeDeclaration structTypeDeclaration
Expand Down Expand Up @@ -1180,18 +1184,22 @@ typedefDeclaration
/*************************** STATEMENTS *************************/

assignmentOrMethodCallStatement
: assignmentOrMethodCallStatementWithoutSemicolon ";" { $$ = $1; }
;

assignmentOrMethodCallStatementWithoutSemicolon
// These rules are overly permissive, but they avoid some conflicts
: lvalue "(" argumentList ")" ";"
: lvalue "(" argumentList ")"
{ auto mc = new IR::MethodCallExpression(@1 + @4, $1,
new IR::Vector<IR::Type>(), $3);
$$ = new IR::MethodCallStatement(@1 + @4, mc); }

| lvalue l_angle typeArgumentList r_angle "(" argumentList ")" ";"
| lvalue l_angle typeArgumentList r_angle "(" argumentList ")"
{ auto mc = new IR::MethodCallExpression(@1 + @7,
$1, $3, $6);
$$ = new IR::MethodCallStatement(@1 + @7, mc); }

| lvalue "=" expression ";"
| lvalue "=" expression
{ $$ = new IR::AssignmentStatement(@2, $1, $3); }

;
Expand Down Expand Up @@ -1239,6 +1247,7 @@ statement
| returnStatement { $$ = $1; }
| exitStatement { $$ = $1; }
| switchStatement { $$ = $1; }
| forStatement { $$ = $1; }
;

blockStatement
Expand Down Expand Up @@ -1283,6 +1292,51 @@ statementOrDeclaration
| instantiation { $$ = $1; }
;

forStatement
: FOR "(" forInitStatements ";"
expression ";"
forUpdateStatements ")"
statement
{ $$ = new IR::ForStatement(@1+@8, *$3, $5, *$7, $9); }
| FOR "(" typeRef name IN forCollectionExpr ")"
statement
{ $$ = new IR::ForInStatement(@1+@6, new IR::Declaration_Variable(@3+@4, *$4, $3), $6, $8); }
;

forInitStatements
: %empty { $$ = new IR::IndexedVector<IR::StatOrDecl>(); }
| forInitStatementsNonEmpty { $$ = $1; }
;

forInitStatementsNonEmpty
: declOrAssignmentOrMethodCallStatement { $$ = new IR::IndexedVector<IR::StatOrDecl>($1); }
| forInitStatementsNonEmpty "," declOrAssignmentOrMethodCallStatement {
($$ = $1)->push_back($3); }
;

declOrAssignmentOrMethodCallStatement
: variableDeclarationWithoutSemicolon { $$ = $1; }
| assignmentOrMethodCallStatementWithoutSemicolon { $$ = $1; }
;

forUpdateStatements
: %empty { $$ = new IR::IndexedVector<IR::StatOrDecl>(); }
| forUpdateStatementsNonEmpty { $$ = $1; }
;

forUpdateStatementsNonEmpty
: assignmentOrMethodCallStatementWithoutSemicolon {
$$ = new IR::IndexedVector<IR::StatOrDecl>($1); }
| forUpdateStatementsNonEmpty "," assignmentOrMethodCallStatementWithoutSemicolon {
($$ = $1)->push_back($3); }
;

forCollectionExpr
: expression { $$ = $1; }
| expression ".." expression { $$ = new IR::Range(@1 + @3, $1, $3); }
| typeRef { $$ = new IR::ConstructorCallExpression(@1, $1, {}); }
;

/************************* TABLE *********************************/

tableDeclaration
Expand Down Expand Up @@ -1386,12 +1440,16 @@ actionDeclaration
/************************* VARIABLES *****************************/

variableDeclaration
: annotations typeRef name optInitializer ";"
: variableDeclarationWithoutSemicolon ";" { $$ = $1; }
;

variableDeclarationWithoutSemicolon
: annotations typeRef name optInitializer
{ auto ann = new IR::Annotations(@1, *$1);
$$ = new IR::Declaration_Variable(@1+@4, *$3, ann, $2, $4);
$$ = new IR::Declaration_Variable(@1, *$3, ann, $2, $4);
driver.structure->declareObject(*$3, $2->toString()); }
| typeRef name optInitializer ";"
{ $$ = new IR::Declaration_Variable(@1+@4, *$2, $1, $3);
| typeRef name optInitializer
{ $$ = new IR::Declaration_Variable(@1, *$2, $1, $3);
driver.structure->declareObject(*$2, $1->toString()); }
;

Expand Down
23 changes: 23 additions & 0 deletions ir/dbprint-stmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,26 @@ void IR::SwitchStatement::dbprint(std::ostream &out) const {
}
out << unindent << " }" << setprec(prec);
}

void IR::ForStatement::dbprint(std::ostream &out) const {
int prec = getprec(out);
out << Prec_Low << "for (";
bool first = true;
for (auto *sd : init) {
if (!first) out << ", ";
out << sd;
first = false; }
out << "; " << condition << "; ";
first = true;
for (auto *sd : updates) {
if (!first) out << ", ";
out << sd;
first = false; }
out << "{" << indent << Log::endl << body << " }" << unindent << setprec(prec);
}

void IR::ForInStatement::dbprint(std::ostream &out) const {
int prec = getprec(out);
out << Prec_Low << "for (" << decl << " in " << collection << ") {" << indent
<< Log::endl << body << " }" << unindent << setprec(prec);
}
22 changes: 22 additions & 0 deletions ir/ir.def
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,28 @@ class SwitchStatement : Statement {
split.run_visit(); }
}

class ForStatement : Statement {
inline IndexedVector<StatOrDecl> init;
Expression condition;
inline IndexedVector<StatOrDecl> updates;
Statement body;
visit_children {
v.visit(init, "init");
v.visit(condition, "condition");
// FIXME -- need visit closure over the body and updates + condition
v.visit(body, "body");
v.visit(updates, "updates"); }
}

// FIXME -- should we try to directly convert this to a ForStatement in the parser?
class ForInStatement : Statement {
Declaration_Variable decl;
Expression collection;
Statement body;
}

/////////////////////////////////////////////////////////////

class Function : Declaration, IFunctional, ISimpleNamespace, INestedNamespace {
Type_Method type;
BlockStatement body;
Expand Down