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 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
9 changes: 6 additions & 3 deletions backends/ebpf/ebpfObject.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 Down Expand Up @@ -105,8 +105,11 @@ void EBPFProgram::emitTypes(CodeBuilder* builder) {
for (auto d : *program->declarations) {
if (d->is<IR::Type>() && !d->is<IR::IContainer>() &&
!d->is<IR::Type_Extern>() && !d->is<IR::Type_Parser>() &&
!d->is<IR::Type_Control>() && !d->is<IR::Type_Typedef>()) {
!d->is<IR::Type_Control>() && !d->is<IR::Type_Typedef>() &&
!d->is<IR::Type_Error>()) {
auto type = EBPFTypeFactory::instance->create(d->to<IR::Type>());
if (type == nullptr)
continue;
type->emit(builder);
builder->newline();
}
Expand All @@ -118,7 +121,7 @@ class ErrorCodesVisitor : public Inspector {
CodeBuilder* builder;
public:
explicit ErrorCodesVisitor(CodeBuilder* builder) : builder(builder) {}
bool preorder(const IR::Declaration_Errors* errors) override {
bool preorder(const IR::Type_Error* errors) override {
for (auto m : *errors->getDeclarations()) {
builder->emitIndent();
builder->appendFormat("%s,\n", m->getName().name.c_str());
Expand Down
8 changes: 5 additions & 3 deletions backends/ebpf/ebpfType.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 Down Expand Up @@ -183,8 +183,10 @@ void EBPFStructType::emit(CodeBuilder* builder) {
if (type->is<IR::Type_Header>()) {
builder->emitIndent();
auto type = EBPFTypeFactory::instance->create(IR::Type_Boolean::get());
type->declare(builder, "ebpf_valid", false);
builder->endOfStatement(true);
if (type != nullptr) {
type->declare(builder, "ebpf_valid", false);
builder->endOfStatement(true);
}
}

builder->blockEnd(false);
Expand Down
38 changes: 13 additions & 25 deletions frontends/common/resolveReferences/resolveReferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ std::vector<const IR::IDeclaration*>*
ResolutionContext::resolve(IR::ID name, P4::ResolutionType type, bool previousOnly) const {
static std::vector<const IR::IDeclaration*> empty;

std::vector<const IR::INamespace*> toTry;
toTry = stack;
std::vector<const IR::INamespace*> toTry(stack);
toTry.insert(toTry.end(), globals.begin(), globals.end());

for (auto it = toTry.rbegin(); it != toTry.rend(); ++it) {
Expand Down Expand Up @@ -118,8 +117,7 @@ ResolutionContext::resolve(IR::ID name, P4::ResolutionType type, bool previousOn

void ResolutionContext::done() {
pop(rootNamespace);
if (!stack.empty())
BUG("ResolutionContext::stack not empty");
BUG_CHECK(stack.empty(), "ResolutionContext::stack not empty");
}

const IR::IDeclaration*
Expand Down Expand Up @@ -173,22 +171,19 @@ ResolveReferences::ResolveReferences(ReferenceMap* refMap,
}

void ResolveReferences::addToContext(const IR::INamespace* ns) {
LOG1("Adding to context " << ns);
if (context == nullptr)
BUG("No resolution context; did not start at P4Program?");
LOG1("Adding to context " << dbp(ns));
BUG_CHECK(context != nullptr, "No resolution context; did not start at P4Program?");
context->push(ns);
}

void ResolveReferences::addToGlobals(const IR::INamespace* ns) {
if (context == nullptr)
BUG("No resolution context; did not start at P4Program?");
BUG_CHECK(context != nullptr, "No resolution context; did not start at P4Program?");
context->addGlobal(ns);
}

void ResolveReferences::removeFromContext(const IR::INamespace* ns) {
LOG1("Removing from context " << ns);
if (context == nullptr)
BUG("No resolution context; did not start at P4Program?");
LOG1("Removing from context " << dbp(ns));
BUG_CHECK(context != nullptr, "No resolution context; did not start at P4Program?");
context->pop(ns);
}

Expand Down Expand Up @@ -221,8 +216,7 @@ void ResolveReferences::resolvePath(const IR::Path* path, bool isType) const {
ResolutionContext* ctx = resolvePathPrefix(path->prefix);
ResolutionType k = isType ? ResolutionType::Type : ResolutionType::Any;

if (resolveForward.empty())
BUG("Empty resolveForward");
BUG_CHECK(!resolveForward.empty(), "Empty resolveForward");
bool allowForward = resolveForward.back();

const IR::IDeclaration* decl = ctx->resolveUnique(path->name, k, !allowForward);
Expand Down Expand Up @@ -275,11 +269,9 @@ bool ResolveReferences::preorder(const IR::P4Program* program) {
if (refMap->checkMap(program))
return false;

if (!resolveForward.empty())
BUG("Expected empty resolvePath");
BUG_CHECK(resolveForward.empty(), "Expected empty resolvePath");
resolveForward.push_back(anyOrder);
if (rootNamespace != nullptr)
BUG("Root namespace already set");
BUG_CHECK(rootNamespace == nullptr, "Root namespace already set");
rootNamespace = program;
context = new ResolutionContext(rootNamespace);
return true;
Expand All @@ -289,8 +281,7 @@ void ResolveReferences::postorder(const IR::P4Program*) {
rootNamespace = nullptr;
context->done();
resolveForward.pop_back();
if (!resolveForward.empty())
BUG("Expected empty resolvePath");
BUG_CHECK(resolveForward.empty(), "Expected empty resolvePath");
context = nullptr;
LOG1("Reference map " << refMap);
}
Expand Down Expand Up @@ -416,11 +407,8 @@ void ResolveReferences::postorder(const IR::ParserState *s) {
checkShadowing(s);
}

bool ResolveReferences::preorder(const IR::Declaration_Errors *d) {
addToGlobals(d); return true; }

bool ResolveReferences::preorder(const IR::Declaration_MatchKind *d) {
addToGlobals(d); return true; }
bool ResolveReferences::preorder(const IR::Declaration_MatchKind *d)
{ addToGlobals(d); return true; }

bool ResolveReferences::preorder(const IR::Type_ArchBlock *t) {
resolveForward.push_back(anyOrder);
Expand Down
3 changes: 1 addition & 2 deletions frontends/common/resolveReferences/resolveReferences.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ enum class ResolutionType {
class ResolutionContext : public IHasDbPrint {
std::vector<const IR::INamespace*> stack;
const IR::INamespace* rootNamespace;
std::vector<const IR::INamespace*> globals; // errors and match_kind
std::vector<const IR::INamespace*> globals; // match_kind

public:
explicit ResolutionContext(const IR::INamespace* rootNamespace) :
Expand Down Expand Up @@ -119,7 +119,6 @@ class ResolveReferences : public Inspector {
#undef DECLARE

bool preorder(const IR::Declaration_MatchKind* d) override;
bool preorder(const IR::Declaration_Errors* d) override;
bool preorder(const IR::Declaration* d) override
{ refMap->usedName(d->getName().name); return true; }
bool preorder(const IR::Type_Declaration* d) override
Expand Down
3 changes: 2 additions & 1 deletion frontends/p4/def_use.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ class BaseLocation : public StorageLocation {
BaseLocation(const IR::Type* type, cstring name) :
StorageLocation(type, name)
{ BUG_CHECK(type->is<IR::Type_Base>() || type->is<IR::Type_Enum>()
|| type->is<IR::Type_Var>() || type->is<IR::Type_Tuple>(),
|| type->is<IR::Type_Var>() || type->is<IR::Type_Tuple>()
|| type-is<IR::Type_Error>() || type->is<IR::Type_Var>(),
"%1%: unexpected type", type); }
void addValidBits(LocationSet*) const override {}
void removeHeaders(LocationSet* result) const override;
Expand Down
39 changes: 31 additions & 8 deletions frontends/p4/p4-parse.ypp
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@ limitations under the License.
: Util::SourceInfo(YYRHSLOC(Rhs, 0).getEnd()))

// Program IR built here
static IR::IndexedVector<IR::Node> *declarations;
static IR::IndexedVector<IR::Node> *declarations = nullptr;
static IR::Type_Error* allErrors = nullptr;
static void yyerror(const char *fmt, ...);
static void checkShift(Util::SourceInfo f, Util::SourceInfo r);
static void addErrors(IR::Type_Error* decl);

static Util::ProgramStructure structure;

Expand Down Expand Up @@ -111,6 +113,7 @@ static int yylex();
IR::Type_Name *Type_Name;
IR::TypeParameters *TypeParameters;
IR::Type_Parser *Type_Parser;
IR::Type_Error *Type_Error;
}

%token LE GE SHL AND OR NE EQ
Expand Down Expand Up @@ -161,7 +164,8 @@ static int yylex();
derivedTypeDeclaration parserDeclaration
controlDeclaration enumDeclaration typedefDeclaration
packageTypeDeclaration typeDeclaration
%type<Node> errorDeclaration declaration externDeclaration matchKindDeclaration
%type<Type_Error> errorDeclaration
%type<Node> declaration externDeclaration matchKindDeclaration
%type<Type_Parser> parserTypeDeclaration
%type<Type_Control> controlTypeDeclaration
%type<decls> parserLocalElements controlLocalDeclarations
Expand Down Expand Up @@ -231,7 +235,7 @@ symbol_print(FILE* file, int type, YYSTYPE value)

input
: /* epsilon */
| input declaration { declarations->push_back($2->getNode()); }
| input declaration { if ($2) declarations->push_back($2->getNode()); }
| input ';' {} // empty declaration
;

Expand All @@ -243,7 +247,7 @@ declaration
| typeDeclaration { $$ = $1; }
| controlDeclaration { $$ = $1; }
| instantiation { $$ = $1; }
| errorDeclaration { $$ = $1; }
| errorDeclaration { addErrors($1); $$ = nullptr; }
| matchKindDeclaration { $$ = $1; }
;

Expand All @@ -256,7 +260,7 @@ nonTypeName
;

name
: nonTypeName
: nonTypeName { $$ = $1; }
| TYPE { $$ = new IR::ID(@1, $1); }
| NAMESPACE { $$ = new IR::ID(@1, $1); }
;
Expand Down Expand Up @@ -527,7 +531,7 @@ functionPrototype
methodPrototype
: functionPrototype ';' { $$ = $1; }
| ABSTRACT functionPrototype ';' { $$ = $2; $$->setAbstract(); }
| TYPE '(' parameterList ')' ';' // constructor
| TYPE '(' parameterList ')' ';' // constructor
{ auto par = new IR::ParameterList(@3, $3);
auto mt = new IR::Type_Method(@1, par);
$$ = new IR::Method(@1, IR::ID(@1, $1), mt); }
Expand Down Expand Up @@ -573,7 +577,7 @@ specializedType

baseType
: BOOL { $$ = IR::Type_Boolean::get(); }
| T_ERROR { $$ = IR::Type_Error::get(); }
| T_ERROR { $$ = new IR::Type_Name(@1, new IR::Path(IR::ID(@1, "error"))); }
| BIT { $$ = IR::Type::Bits::get(@1, 1); }
| BIT '<' INTEGER '>' { $$ = IR::Type::Bits::get(@3, $3->asInt(), false); }
| INT '<' INTEGER '>' { $$ = IR::Type::Bits::get(@3, $3->asInt(), true); }
Expand Down Expand Up @@ -653,7 +657,9 @@ enumDeclaration
;

errorDeclaration
: T_ERROR '{' identifierList '}' { $$ = new IR::Declaration_Errors(@1 + @4, $3); }
: T_ERROR '{' identifierList '}'
{ $$ = new IR::Type_Error(@1 + @4,
IR::ID(@1, "error"), $3); }
;

matchKindDeclaration
Expand Down Expand Up @@ -911,6 +917,12 @@ expression
| '+' expression %prec PREFIX { $$ = $2; }
| typeName '.' name { $$ = new IR::Member(
@1 + @3, new IR::TypeNameExpression(@1, $1), *$3); }
| T_ERROR '.' name { $$ = new IR::Member(
@1 + @3,
new IR::TypeNameExpression(
@1+@3, new IR::Type_Name(
@1, new IR::Path(IR::ID(@1, "error")))),
*$3); }
| expression '.' name { $$ = new IR::Member(@1 + @3, $1, *$3); }
| expression '*' expression { $$ = new IR::Mul(@1 + @3, $1, $3); }
| expression '/' expression { $$ = new IR::Div(@1 + @3, $1, $3); }
Expand Down Expand Up @@ -1002,3 +1014,14 @@ void checkShift(Util::SourceInfo l, Util::SourceInfo r)
f.getColumnNumber() != s.getColumnNumber() - 1)
::error("Syntax error at shift operator: %1%", l);
}

void addErrors(IR::Type_Error* errors) {
if (allErrors == nullptr) {
declarations->push_back(errors);
allErrors = errors;
return;
}
auto vec = new IR::IndexedVector<IR::Declaration_ID>(*allErrors->members);
vec->append(*errors->members);
allErrors->members = vec;
}
6 changes: 3 additions & 3 deletions frontends/p4/reservedWords.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ namespace P4 {

// Keep this in sync with the lexer
std::set<cstring> reservedWords = {
"abstract", // experimental
"abstract", // experimental
"action", "actions", "apply", "bool", "bit", "const", "control", "default",
"else", "enum", "error", "exit", "extern", "false", "header", "header_union", // experimental
"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
"return", "select", "set", "state", "struct", "switch", "table", "this", // experimental
"transition", "true", "tuple", "typedef", "varbit", "void", "_"
};

Expand Down
1 change: 1 addition & 0 deletions frontends/p4/sideEffects.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class SideEffects : public Inspector {
// An assignment statement
// e = e1;
// is treated as if it is a call to a function "set(out T v, in T v)"
// FIXME: does not handle select labels
class DoSimplifyExpressions : public Transform {
ReferenceMap* refMap;
TypeMap* typeMap;
Expand Down
Loading