-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.cpp
378 lines (347 loc) · 14.7 KB
/
parser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
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.
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 "parser.h"
#include "JsonObjects.h"
namespace BMV2 {
// TODO(hanw) refactor this function
Util::IJson* ParserConverter::convertParserStatement(const IR::StatOrDecl* stat) {
auto result = new Util::JsonObject();
auto params = mkArrayField(result, "parameters");
if (stat->is<IR::AssignmentStatement>()) {
auto assign = stat->to<IR::AssignmentStatement>();
auto type = typeMap->getType(assign->left, true);
cstring operation;
if (type->is<IR::Type_StructLike>())
operation = "assign_header";
else
operation = "set";
result->emplace("op", operation);
auto l = conv->convertLeftValue(assign->left);
bool convertBool = type->is<IR::Type_Boolean>();
auto r = conv->convert(assign->right, true, true, convertBool);
params->append(l);
params->append(r);
if (operation == "assign_header") {
// must wrap into another outer object
auto wrap = new Util::JsonObject();
wrap->emplace("op", "primitive");
auto params = mkParameters(wrap);
params->append(result);
result = wrap;
}
return result;
} else if (stat->is<IR::MethodCallStatement>()) {
auto mce = stat->to<IR::MethodCallStatement>()->methodCall;
auto minst = P4::MethodInstance::resolve(mce, refMap, typeMap);
if (minst->is<P4::ExternMethod>()) {
auto extmeth = minst->to<P4::ExternMethod>();
if (extmeth->method->name.name == corelib.packetIn.extract.name) {
int argCount = mce->arguments->size();
if (argCount == 1 || argCount == 2) {
cstring ename = argCount == 1 ? "extract" : "extract_VL";
result->emplace("op", ename);
auto arg = mce->arguments->at(0);
auto argtype = typeMap->getType(arg, true);
if (!argtype->is<IR::Type_Header>()) {
::error("%1%: extract only accepts arguments with header types, not %2%",
arg, argtype);
return result;
}
auto param = new Util::JsonObject();
params->append(param);
cstring type;
Util::IJson* j = nullptr;
if (arg->is<IR::Member>()) {
auto mem = arg->to<IR::Member>();
auto baseType = typeMap->getType(mem->expr, true);
if (baseType->is<IR::Type_Stack>()) {
if (mem->member == IR::Type_Stack::next) {
type = "stack";
j = conv->convert(mem->expr);
} else {
BUG("%1%: unsupported", mem);
}
}
}
if (j == nullptr) {
type = "regular";
j = conv->convert(arg);
}
auto value = j->to<Util::JsonObject>()->get("value");
param->emplace("type", type);
param->emplace("value", value);
if (argCount == 2) {
auto arg2 = mce->arguments->at(1);
auto jexpr = conv->convert(arg2, true, false);
auto rwrap = new Util::JsonObject();
// The spec says that this must always be wrapped in an expression
rwrap->emplace("type", "expression");
rwrap->emplace("value", jexpr);
params->append(rwrap);
}
return result;
}
}
} else if (minst->is<P4::ExternFunction>()) {
auto extfn = minst->to<P4::ExternFunction>();
if (extfn->method->name.name == IR::ParserState::verify) {
result->emplace("op", "verify");
BUG_CHECK(mce->arguments->size() == 2, "%1%: Expected 2 arguments", mce);
{
auto cond = mce->arguments->at(0);
// false means don't wrap in an outer expression object, which is not needed
// here
auto jexpr = conv->convert(cond, true, false);
params->append(jexpr);
}
{
auto error = mce->arguments->at(1);
// false means don't wrap in an outer expression object, which is not needed
// here
auto jexpr = conv->convert(error, true, false);
params->append(jexpr);
}
return result;
}
} else if (minst->is<P4::BuiltInMethod>()) {
/* example result:
{
"parameters" : [
{
"op" : "add_header",
"parameters" : [{"type" : "header", "value" : "ipv4"}]
}
],
"op" : "primitive"
} */
result->emplace("op", "primitive");
auto bi = minst->to<P4::BuiltInMethod>();
cstring primitive;
auto paramsValue = new Util::JsonObject();
params->append(paramsValue);
auto pp = mkArrayField(paramsValue, "parameters");
auto obj = conv->convert(bi->appliedTo);
pp->append(obj);
if (bi->name == IR::Type_Header::setValid) {
primitive = "add_header";
} else if (bi->name == IR::Type_Header::setInvalid) {
primitive = "remove_header";
} else if (bi->name == IR::Type_Stack::push_front ||
bi->name == IR::Type_Stack::pop_front) {
if (bi->name == IR::Type_Stack::push_front)
primitive = "push";
else
primitive = "pop";
BUG_CHECK(mce->arguments->size() == 1, "Expected 1 argument for %1%", mce);
auto arg = conv->convert(mce->arguments->at(0));
pp->append(arg);
} else {
BUG("%1%: Unexpected built-in method", bi->name);
}
paramsValue->emplace("op", primitive);
return result;
}
}
::error("%1%: not supported in parser on this target", stat);
return result;
}
// Operates on a select keyset
void ParserConverter::convertSimpleKey(const IR::Expression* keySet,
mpz_class& value, mpz_class& mask) const {
if (keySet->is<IR::Mask>()) {
auto mk = keySet->to<IR::Mask>();
if (!mk->left->is<IR::Constant>()) {
::error("%1% must evaluate to a compile-time constant", mk->left);
return;
}
if (!mk->right->is<IR::Constant>()) {
::error("%1% must evaluate to a compile-time constant", mk->right);
return;
}
value = mk->left->to<IR::Constant>()->value;
mask = mk->right->to<IR::Constant>()->value;
} else if (keySet->is<IR::Constant>()) {
value = keySet->to<IR::Constant>()->value;
mask = -1;
} else if (keySet->is<IR::BoolLiteral>()) {
value = keySet->to<IR::BoolLiteral>()->value ? 1 : 0;
mask = -1;
} else {
::error("%1% must evaluate to a compile-time constant", keySet);
value = 0;
mask = 0;
}
}
unsigned ParserConverter::combine(const IR::Expression* keySet,
const IR::ListExpression* select,
mpz_class& value, mpz_class& mask) const {
// From the BMv2 spec: For values and masks, make sure that you
// use the correct format. They need to be the concatenation (in
// the right order) of all byte padded fields (padded with 0
// bits). For example, if the transition key consists of a 12-bit
// field and a 2-bit field, each value will need to have 3 bytes
// (2 for the first field, 1 for the second one). If the
// transition value is 0xaba, 0x3, the value attribute will be set
// to 0x0aba03.
// Return width in bytes
value = 0;
mask = 0;
unsigned totalWidth = 0;
if (keySet->is<IR::DefaultExpression>()) {
return totalWidth;
} else if (keySet->is<IR::ListExpression>()) {
auto le = keySet->to<IR::ListExpression>();
BUG_CHECK(le->components.size() == select->components.size(),
"%1%: mismatched select", select);
unsigned index = 0;
bool noMask = true;
for (auto it = select->components.begin();
it != select->components.end(); ++it) {
auto e = *it;
auto keyElement = le->components.at(index);
auto type = typeMap->getType(e, true);
int width = type->width_bits();
BUG_CHECK(width > 0, "%1%: unknown width", e);
mpz_class key_value, mask_value;
convertSimpleKey(keyElement, key_value, mask_value);
unsigned w = 8 * ROUNDUP(width, 8);
totalWidth += ROUNDUP(width, 8);
value = Util::shift_left(value, w) + key_value;
if (mask_value != -1) {
mask = Util::shift_left(mask, w) + mask_value;
noMask = false;
}
LOG3("Shifting " << " into key " << key_value << " &&& " << mask_value <<
" result is " << value << " &&& " << mask);
index++;
}
if (noMask)
mask = -1;
return totalWidth;
} else {
BUG_CHECK(select->components.size() == 1, "%1%: mismatched select/label", select);
convertSimpleKey(keySet, value, mask);
auto type = typeMap->getType(select->components.at(0), true);
return type->width_bits() / 8;
}
}
Util::IJson* ParserConverter::stateName(IR::ID state) {
if (state.name == IR::ParserState::accept) {
return Util::JsonValue::null;
} else if (state.name == IR::ParserState::reject) {
::warning("Explicit transition to %1% not supported on this target", state);
return Util::JsonValue::null;
} else {
return new Util::JsonValue(state.name);
}
}
std::vector<Util::IJson*>
ParserConverter::convertSelectExpression(const IR::SelectExpression* expr) {
std::vector<Util::IJson*> result;
auto se = expr->to<IR::SelectExpression>();
for (auto sc : se->selectCases) {
auto trans = new Util::JsonObject();
mpz_class value, mask;
unsigned bytes = combine(sc->keyset, se->select, value, mask);
if (mask == 0) {
trans->emplace("value", "default");
trans->emplace("mask", Util::JsonValue::null);
trans->emplace("next_state", stateName(sc->state->path->name));
} else {
trans->emplace("value", stringRepr(value, bytes));
if (mask == -1)
trans->emplace("mask", Util::JsonValue::null);
else
trans->emplace("mask", stringRepr(mask, bytes));
trans->emplace("next_state", stateName(sc->state->path->name));
}
result.push_back(trans);
}
return result;
}
Util::IJson*
ParserConverter::convertSelectKey(const IR::SelectExpression* expr) {
auto se = expr->to<IR::SelectExpression>();
CHECK_NULL(se);
auto key = conv->convert(se->select, false);
return key;
}
Util::IJson*
ParserConverter::convertPathExpression(const IR::PathExpression* pe) {
auto trans = new Util::JsonObject();
trans->emplace("value", "default");
trans->emplace("mask", Util::JsonValue::null);
trans->emplace("next_state", stateName(pe->path->name));
return trans;
}
Util::IJson*
ParserConverter::createDefaultTransition() {
auto trans = new Util::JsonObject();
trans->emplace("value", "default");
trans->emplace("mask", Util::JsonValue::null);
trans->emplace("next_state", Util::JsonValue::null);
return trans;
}
bool ParserConverter::preorder(const IR::P4Parser* parser) {
// hanw hard-coded parser name assumed by BMv2
auto parser_id = json->add_parser("parser");
for (auto s : parser->parserLocals) {
if (s->is<IR::Declaration_Instance>()) {
::error("%1%: not supported on parsers on this target", s);
return false;
}
}
// convert parse state
for (auto state : parser->states) {
if (state->name == IR::ParserState::reject || state->name == IR::ParserState::accept)
continue;
auto state_id = json->add_parser_state(parser_id, state->controlPlaneName());
// convert statements
for (auto s : state->components) {
auto op = convertParserStatement(s);
json->add_parser_op(state_id, op);
}
// convert transitions
if (state->selectExpression != nullptr) {
if (state->selectExpression->is<IR::SelectExpression>()) {
auto expr = state->selectExpression->to<IR::SelectExpression>();
auto transitions = convertSelectExpression(expr);
for (auto transition : transitions) {
json->add_parser_transition(state_id, transition);
}
auto key = convertSelectKey(expr);
json->add_parser_transition_key(state_id, key);
} else if (state->selectExpression->is<IR::PathExpression>()) {
auto expr = state->selectExpression->to<IR::PathExpression>();
auto transition = convertPathExpression(expr);
json->add_parser_transition(state_id, transition);
} else {
BUG("%1%: unexpected selectExpression", state->selectExpression);
}
} else {
auto transition = createDefaultTransition();
json->add_parser_transition(state_id, transition);
}
}
return false;
}
/// visit ParserBlock only
bool ParserConverter::preorder(const IR::PackageBlock* block) {
for (auto it : block->constantValue) {
if (it.second->is<IR::ParserBlock>()) {
visit(it.second->getNode());
}
}
return false;
}
} // namespace BMV2