Skip to content

Commit 98b19b0

Browse files
author
Mihai Budiu
authored
support key=value annotations (#1300)
* Support for key=value annotations
1 parent a8a69b8 commit 98b19b0

File tree

9 files changed

+50
-4
lines changed

9 files changed

+50
-4
lines changed

frontends/p4/toP4/toP4.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -1014,6 +1014,19 @@ bool ToP4::preorder(const IR::Annotation * a) {
10141014
doneVec();
10151015
builder.append(")");
10161016
}
1017+
if (!a->kv.empty()) {
1018+
builder.append("(");
1019+
bool first = true;
1020+
for (auto kvp : a->kv) {
1021+
if (!first)
1022+
builder.append(", ");
1023+
first = false;
1024+
builder.append(kvp.first);
1025+
builder.append("=");
1026+
visit(kvp.second);
1027+
}
1028+
builder.append(")");
1029+
}
10171030
builder.spc();
10181031
return false;
10191032
}

frontends/parsers/p4/p4parser.ypp

+18
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ inline std::ostream& operator<<(std::ostream& out, const P4::OptionalConst& oc)
5959
return out;
6060
}
6161

62+
struct KV {
63+
cstring name;
64+
const IR::Expression* expression;
65+
};
66+
6267
// Bison uses the types you provide to %type to make constructors for the
6368
// variant type it uses under the hood, but its code generation is a little
6469
// naive and it always prepends 'const' to the type. This is problematic when
@@ -220,6 +225,8 @@ typedef const IR::Type ConstType;
220225
%type<IR::Entry*> entry
221226
%type<IR::Vector<IR::Entry>*> entriesList
222227
%type<IR::MethodCallExpression*> actionBinding
228+
%type<IR::NameMap<IR::Expression>*> kvList
229+
%type<KV*> kvPair
223230

224231
%left COMMA
225232
%nonassoc QUESTION
@@ -303,6 +310,17 @@ annotations
303310
annotation
304311
: "@" name { $$ = new IR::Annotation(@1, *$2, {}); }
305312
| "@" name "(" expressionList ")" { $$ = new IR::Annotation(@1, *$2, *$4); }
313+
| "@" name "(" kvList ")" { $$ = new IR::Annotation(@1, *$2, *$4); }
314+
;
315+
316+
kvList
317+
: kvPair { $$ = new IR::NameMap<IR::Expression>;
318+
$$->add($1->name, $1->expression); }
319+
| kvList "," kvPair { $$ = $1; $$->add($3->name, $3->expression); }
320+
;
321+
322+
kvPair
323+
: name "=" expression { $$ = new KV; $$->name = $1->name; $$->expression = $3; }
306324
;
307325

308326
parameterList

ir/base.def

+9
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,20 @@ class Path {
175175
/// Most P4 entities can be optionally annotated
176176
class Annotation {
177177
ID name;
178+
/// Annotations that are simple expressions
178179
inline Vector<Expression> expr;
180+
/// Annotations described as key-value pairs
181+
// TODO: this should be a map from ID not from cstring
182+
inline NameMap<Expression> kv;
179183
Annotation { if (!srcInfo) srcInfo = name.srcInfo; }
180184
Annotation(Util::SourceInfo si, ID n, const std::initializer_list<const IR::Expression *> &a)
181185
: Node(si), name(n), expr(a) {}
186+
Annotation(Util::SourceInfo si, ID n, const IR::Vector<IR::Expression> &a)
187+
: Node(si), name(n), expr(a) {}
188+
Annotation(Util::SourceInfo si, ID n, const NameMap<IR::Expression> &kv)
189+
: Node(si), name(n), kv(kv) {}
182190
Annotation(ID n, const std::initializer_list<const IR::Expression *> &a) : name(n), expr(a) {}
191+
Annotation(ID n, const IR::Vector<IR::Expression> &a) : name(n), expr(a) {}
183192
Annotation(ID n, intmax_t v) : name(n) {
184193
expr.push_back(new IR::Constant(v)); }
185194
/// Predefined annotations used by the compiler

midend/actionSynthesis.cpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ const IR::Node* DoMoveActionsToTables::postorder(IR::MethodCallStatement* statem
7272
cstring tblName = IR::ID(refMap->newName(cstring("tbl_") + ac->action->name.name), nullptr);
7373

7474
auto annos = new IR::Annotations();
75-
annos->add(new IR::Annotation(IR::Annotation::hiddenAnnotation,
76-
IR::Vector<IR::Expression>()));
75+
annos->add(new IR::Annotation(IR::Annotation::hiddenAnnotation, {}));
7776
auto tbl = new IR::P4Table(tblName, annos, props);
7877
tables.push_back(tbl);
7978

@@ -177,8 +176,7 @@ const IR::Statement* DoSynthesizeActions::createAction(const IR::Statement* toAd
177176
}
178177

179178
auto annos = new IR::Annotations();
180-
annos->add(new IR::Annotation(IR::Annotation::hiddenAnnotation,
181-
IR::Vector<IR::Expression>()));
179+
annos->add(new IR::Annotation(IR::Annotation::hiddenAnnotation, {}));
182180
auto action = new IR::P4Action(name, annos, new IR::ParameterList(), body);
183181
actions.push_back(action);
184182
auto actpath = new IR::PathExpression(name);

testdata/p4_16_samples/kvanno.p4

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@kvanno(
2+
name="x",
3+
value=0
4+
)
5+
const bit<32> x = 0;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@kvanno(name="x", value=0) const bit<32> x = 32w0;

testdata/p4_16_samples_outputs/kvanno-frontend.p4

Whitespace-only changes.
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@kvanno(name="x", value=0) const bit<32> x = 0;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
warning: Program does not contain a `main' module

0 commit comments

Comments
 (0)