Skip to content

Commit

Permalink
Make {#} a single token
Browse files Browse the repository at this point in the history
Signed-off-by: Mihai Budiu <[email protected]>
  • Loading branch information
Mihai Budiu committed Jan 10, 2023
1 parent 17917d7 commit f68731e
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 40 deletions.
13 changes: 8 additions & 5 deletions frontends/p4/toP4/toP4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -942,16 +942,19 @@ bool ToP4::preorder(const IR::StructExpression* e) {

bool ToP4::preorder(const IR::InvalidHeader* e) {
if (expressionPrecedence > DBPrint::Prec_Prefix) builder.append("(");
if (e->headerType != nullptr) {
builder.append("(");
visit(e->headerType);
builder.append(")");
}
builder.append("(");
visit(e->headerType);
builder.append(")");
builder.append("{#}");
if (expressionPrecedence > DBPrint::Prec_Prefix) builder.append(")");
return false;
}

bool ToP4::preorder(const IR::Invalid*) {
builder.append("{#}");
return false;
}

bool ToP4::preorder(const IR::MethodCallExpression* e) {
int prec = expressionPrecedence;
bool useParens = (prec > DBPrint::Prec_Postfix) ||
Expand Down
1 change: 1 addition & 0 deletions frontends/p4/toP4/toP4.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ class ToP4 : public Inspector {
bool preorder(const IR::P4ListExpression* e) override;
bool preorder(const IR::StructExpression* e) override;
bool preorder(const IR::InvalidHeader* e) override;
bool preorder(const IR::Invalid* e) override;
bool preorder(const IR::MethodCallExpression* e) override;
bool preorder(const IR::DefaultExpression* e) override;
bool preorder(const IR::This* e) override;
Expand Down
48 changes: 23 additions & 25 deletions frontends/p4/typeChecking/typeChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2081,23 +2081,23 @@ const IR::Node* TypeInference::postorder(IR::ListExpression* expression) {
return expression;
}

const IR::Node* TypeInference::postorder(IR::Invalid* expression) {
if (done()) return expression;
auto unk = IR::Type_Unknown::get();
setType(expression, unk);
setType(getOriginal(), unk);
setCompileTimeConstant(expression);
setCompileTimeConstant(getOriginal<IR::Expression>());
return expression;
}

const IR::Node* TypeInference::postorder(IR::InvalidHeader* expression) {
if (done()) return expression;
if (!expression->headerType) {
// This expression should be enclosed within a cast.
// Processing the cast will replace this expression with an
// InvalidHeader expression with a known type.
setType(expression, IR::Type_Unknown::get());
setType(getOriginal(), IR::Type_Unknown::get());
return expression;
}
auto type = getTypeType(expression->headerType);
if (!type->is<IR::Type_Header>()) {
typeError("%1%: invalid header expression has a non-header type `%2%`", expression, type);
return expression;
}
setType(getOriginal(), type);
BUG_CHECK(type->is<IR::Type_Header>(), "%1%: does not have a header type %2%", expression,
type);
setType(expression, type);
setType(getOriginal(), type);
setCompileTimeConstant(expression);
setCompileTimeConstant(getOriginal<IR::Expression>());
return expression;
Expand Down Expand Up @@ -2693,19 +2693,17 @@ const IR::Node* TypeInference::postorder(IR::Cast* expression) {
expression, st->fields.size(), le->components.size());
return expression;
}
} else if (auto ih = expression->expr->to<IR::InvalidHeader>()) {
if (!ih->headerType) {
auto type = castType->getP4Type();
if (!castType->is<IR::Type_Header>()) {
typeError("%1%: invalid header expression has a non-header type `%2%`",
expression, castType);
return expression;
}
setType(type, new IR::Type_Type(castType));
auto result = new IR::InvalidHeader(ih->srcInfo, type, type);
setType(result, castType);
return result;
} else if (auto ih = expression->expr->to<IR::Invalid>()) {
auto type = castType->getP4Type();
if (!castType->is<IR::Type_Header>()) {
typeError("%1%: invalid header expression has a non-header type `%2%`", expression,
castType);
return expression;
}
setType(type, new IR::Type_Type(castType));
auto result = new IR::InvalidHeader(ih->srcInfo, type, type);
setType(result, castType);
return result;
}
}
if (auto lt = concreteType->to<IR::Type_P4List>()) {
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 @@ -306,6 +306,7 @@ class TypeInference : public Transform {
const IR::Node* postorder(IR::TypeNameExpression* expression) override;
const IR::Node* postorder(IR::ListExpression* expression) override;
const IR::Node* postorder(IR::InvalidHeader* expression) override;
const IR::Node* postorder(IR::Invalid* expression) override;
const IR::Node* postorder(IR::P4ListExpression* expression) override;
const IR::Node* postorder(IR::StructExpression* expression) override;
const IR::Node* postorder(IR::MethodCallExpression* expression) override;
Expand Down
2 changes: 1 addition & 1 deletion frontends/parsers/p4/p4lexer.ll
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ using Parser = P4::P4Parser;
"++" { BEGIN(driver.saveState); driver.template_args = false; return makeToken(PP); }
"+" { BEGIN(driver.saveState); driver.template_args = false; return makeToken(PLUS); }
"#" { BEGIN(driver.saveState); driver.template_args = false; return makeToken(SHARP); }
"{#}" { BEGIN(driver.saveState); driver.template_args = false; return makeToken(INVALID); }
"|+|" { BEGIN(driver.saveState); driver.template_args = false; return makeToken(PLUS_SAT); }
"-" { BEGIN(driver.saveState); driver.template_args = false; return makeToken(MINUS); }
"|-|" { BEGIN(driver.saveState); driver.template_args = false; return makeToken(MINUS_SAT); }
Expand Down
5 changes: 2 additions & 3 deletions frontends/parsers/p4/p4parser.ypp
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ inline std::ostream& operator<<(std::ostream& out, const P4::Token& t) {
%token<Token> PLUS_SAT "|+|"
%token<Token> MINUS_SAT "|-|"
%token<Token> MUL "*"
%token<Token> SHARP "#"
%token<Token> INVALID "{#}"
%token<Token> DIV "/"
%token<Token> MOD "%"
%token<Token> BIT_OR "|"
Expand Down Expand Up @@ -1429,8 +1429,7 @@ expression
| expression "[" expression "]" { $$ = new IR::ArrayIndex(@1 + @4, $1, $3); }
| expression "[" expression ":" expression "]" { $$ = new IR::Slice(@1 + @6, $1, $3, $5); }
| "{" expressionList "}" { $$ = new IR::ListExpression(@1 + @3, *$2); }
| "{" SHARP "}" { $$ = new IR::InvalidHeader(
@1 + @3, IR::Type::Unknown::get(), nullptr); }
| INVALID { $$ = new IR::Invalid(@1, IR::Type::Unknown::get()); }
| "{" kvList "}" { $$ = new IR::StructExpression(
@1 + @3, IR::Type::Unknown::get(), (IR::Type_Name*)nullptr, *$2); }
| "(" expression ")" { $$ = $2; }
Expand Down
7 changes: 3 additions & 4 deletions ir/dbprint-expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,9 @@ void IR::ConstructorCallExpression::dbprint(std::ostream& out) const {
dbsetflags(out, flags);
}

void IR::InvalidHeader::dbprint(std::ostream& out) const {
out << "(" << type << "){#}"
<< ";";
}
void IR::InvalidHeader::dbprint(std::ostream& out) const { out << "(" << type << "){#}"; }

void IR::Invalid::dbprint(std::ostream& out) const { out << "{#}"; }

void IR::ListExpression::dbprint(std::ostream& out) const {
int prec = getprec(out);
Expand Down
7 changes: 5 additions & 2 deletions ir/expression.def
Original file line number Diff line number Diff line change
Expand Up @@ -453,10 +453,13 @@ class StructExpression : Expression {
size_t size() const { return components.size(); }
}

/// Can be an invalid header or header_union
class Invalid : Expression {
}

/// An expression that evaluates to an invalid header with the specified type.
class InvalidHeader : Expression {
/// May only be known after type checking; so it can be nullptr.
NullOK Type headerType;
Type headerType;
}

/// A ListExpression where all the components are compile-time values.
Expand Down

0 comments on commit f68731e

Please sign in to comment.