-
Notifications
You must be signed in to change notification settings - Fork 0
/
expression.cpp
591 lines (542 loc) · 22.9 KB
/
expression.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
/*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 "expression.h"
#include "backend.h"
namespace BMV2 {
class ArithmeticFixup;
const IR::Expression* ArithmeticFixup::fix(const IR::Expression* expr, const IR::Type_Bits* type) {
unsigned width = type->size;
if (!type->isSigned) {
auto mask = new IR::Constant(type, Util::mask(width), 16);
typeMap->setType(mask, type);
auto result = new IR::BAnd(expr->srcInfo, expr, mask);
typeMap->setType(result, type);
return result;
} else {
auto result = new IR::IntMod(expr->srcInfo, expr, width);
typeMap->setType(result, type);
return result;
}
return expr;
}
const IR::Node* ArithmeticFixup::updateType(const IR::Expression* expression) {
if (*expression != *getOriginal()) {
auto type = typeMap->getType(getOriginal(), true);
typeMap->setType(expression, type);
}
return expression;
}
const IR::Node* ArithmeticFixup::postorder(IR::Expression* expression) {
return updateType(expression);
}
const IR::Node* ArithmeticFixup::postorder(IR::Operation_Binary* expression) {
auto type = typeMap->getType(getOriginal(), true);
if (expression->is<IR::BAnd>() || expression->is<IR::BOr>() ||
expression->is<IR::BXor>())
// no need to clamp these
return updateType(expression);
if (type->is<IR::Type_Bits>())
return fix(expression, type->to<IR::Type_Bits>());
return updateType(expression);
}
const IR::Node* ArithmeticFixup::postorder(IR::Neg* expression) {
auto type = typeMap->getType(getOriginal(), true);
if (type->is<IR::Type_Bits>())
return fix(expression, type->to<IR::Type_Bits>());
return updateType(expression);
}
const IR::Node* ArithmeticFixup::postorder(IR::Cast* expression) {
auto type = typeMap->getType(getOriginal(), true);
if (type->is<IR::Type_Bits>())
return fix(expression, type->to<IR::Type_Bits>());
return updateType(expression);
}
Util::IJson* ExpressionConverter::get(const IR::Expression* expression) const {
auto result = ::get(map, expression);
if (result == nullptr) {
LOG3("Looking up " << expression);
for (auto it : map) {
LOG3(" " << it.first << " " << it.second);
}
}
BUG_CHECK(result, "%1%: could not convert to Json", expression);
return result;
}
void ExpressionConverter::postorder(const IR::BoolLiteral* expression) {
auto result = new Util::JsonObject();
result->emplace("type", "bool");
result->emplace("value", expression->value);
map.emplace(expression, result);
}
void ExpressionConverter::postorder(const IR::MethodCallExpression* expression) {
auto instance = P4::MethodInstance::resolve(expression,
backend->getRefMap(), backend->getTypeMap());
if (instance->is<P4::ExternMethod>()) {
auto em = instance->to<P4::ExternMethod>();
if (em->originalExternType->name == corelib.packetIn.name &&
em->method->name == corelib.packetIn.lookahead.name) {
BUG_CHECK(expression->typeArguments->size() == 1,
"Expected 1 type parameter for %1%", em->method);
auto targ = expression->typeArguments->at(0);
auto typearg = backend->getTypeMap()->getTypeType(targ, true);
int width = typearg->width_bits();
BUG_CHECK(width > 0, "%1%: unknown width", targ);
auto j = new Util::JsonObject();
j->emplace("type", "lookahead");
auto v = mkArrayField(j, "value");
v->append(0);
v->append(width);
map.emplace(expression, j);
return;
}
} else if (instance->is<P4::BuiltInMethod>()) {
auto bim = instance->to<P4::BuiltInMethod>();
if (bim->name == IR::Type_Header::isValid) {
// This should be applied to a header union; isValid() calls on
// headers should've been removed by SynthesizeValidField.
auto type = backend->getTypeMap()->getType(bim->appliedTo, true);
BUG_CHECK(type->is<IR::Type_HeaderUnion>(),
"Expected isValid() to be eliminated in %1%", expression);
auto result = new Util::JsonObject();
result->emplace("type", "expression");
auto e = new Util::JsonObject();
result->emplace("value", e);
e->emplace("op", "valid_union");
e->emplace("left", Util::JsonValue::null);
auto l = get(bim->appliedTo);
e->emplace("right", l);
map.emplace(expression, result);
return;
}
}
BUG("%1%: unhandled case", expression);
}
void ExpressionConverter::postorder(const IR::Cast* expression) {
// nothing to do for casts - the ArithmeticFixup pass should have handled them already
auto j = get(expression->expr);
map.emplace(expression, j);
}
void ExpressionConverter::postorder(const IR::Constant* expression) {
auto result = new Util::JsonObject();
result->emplace("type", "hexstr");
cstring repr = stringRepr(expression->value,
ROUNDUP(expression->type->width_bits(), 8));
result->emplace("value", repr);
map.emplace(expression, result);
}
void ExpressionConverter::postorder(const IR::ArrayIndex* expression) {
auto result = new Util::JsonObject();
result->emplace("type", "header");
cstring elementAccess;
// This is can be either a header, which is part of the "headers" parameter
// or a temporary array.
if (expression->left->is<IR::Member>()) {
// This is a header part of the parameters
auto mem = expression->left->to<IR::Member>();
elementAccess = mem->member.name;
} else if (expression->left->is<IR::PathExpression>()) {
// This is a temporary variable with type stack.
auto path = expression->left->to<IR::PathExpression>();
elementAccess = path->path->name;
}
if (!expression->right->is<IR::Constant>()) {
::error("%1%: all array indexes must be constant on this architecture",
expression->right);
} else {
int index = expression->right->to<IR::Constant>()->asInt();
elementAccess += "[" + Util::toString(index) + "]";
}
result->emplace("value", elementAccess);
map.emplace(expression, result);
}
/// Non-null if the expression refers to a parameter from the enclosing control
const IR::Parameter*
ExpressionConverter::enclosingParamReference(const IR::Expression* expression) {
CHECK_NULL(expression);
if (!expression->is<IR::PathExpression>())
return nullptr;
auto pe = expression->to<IR::PathExpression>();
auto decl = backend->getRefMap()->getDeclaration(pe->path, true);
auto param = decl->to<IR::Parameter>();
if (param == nullptr)
return param;
if (backend->getStructure().nonActionParameters.count(param) > 0)
return param;
return nullptr;
}
void ExpressionConverter::postorder(const IR::Member* expression) {
// TODO: deal with references that return bool
auto result = new Util::JsonObject();
auto parentType = backend->getTypeMap()->getType(expression->expr, true);
cstring fieldName = expression->member.name;
auto type = backend->getTypeMap()->getType(expression, true);
if (parentType->is<IR::Type_StructLike>()) {
auto st = parentType->to<IR::Type_StructLike>();
auto field = st->getField(expression->member);
if (field != nullptr)
// field could be a method call, i.e., isValid.
fieldName = field->controlPlaneName();
}
{
if (type->is<IR::Type_Error>() && expression->expr->is<IR::TypeNameExpression>()) {
// this deals with constants that have type 'error'
result->emplace("type", "hexstr");
auto decl = type->to<IR::Type_Error>()->getDeclByName(expression->member.name);
ErrorCodesMap errCodes = backend->getErrorCodesMap();
auto errorValue = errCodes.at(decl);
result->emplace("value", Util::toString(errorValue));
map.emplace(expression, result);
return;
}
}
auto param = enclosingParamReference(expression->expr);
if (param != nullptr) {
// hanw: need a function isStandardMetadata();
if (param->type->toString() == "standard_metadata_t") {
result->emplace("type", "field");
auto e = mkArrayField(result, "value");
e->append(BMV2::V1ModelProperties::jsonMetadataParameterName);
e->append(fieldName);
} else {
if (type->is<IR::Type_Stack>()) {
result->emplace("type", "header_stack");
result->emplace("value", fieldName);
} else if (type->is<IR::Type_HeaderUnion>()) {
result->emplace("type", "header_union");
result->emplace("value", fieldName);
} else if (parentType->is<IR::Type_HeaderUnion>()) {
auto l = get(expression->expr);
cstring nestedField = fieldName;
if (l->is<Util::JsonObject>()) {
auto lv = l->to<Util::JsonObject>()->get("value");
if (lv->is<Util::JsonValue>()) {
// header in union reference ["u", "f"] => "u.f"
cstring prefix = lv->to<Util::JsonValue>()->getString();
nestedField = prefix + "." + nestedField;
}
}
result->emplace("type", "header");
result->emplace("value", nestedField);
} else if (parentType->is<IR::Type_StructLike>() &&
(type->is<IR::Type_Bits>() || type->is<IR::Type_Boolean>() ||
type->is<IR::Type_Error>())) {
auto field = parentType->to<IR::Type_StructLike>()->getField(
expression->member);
CHECK_NULL(field);
LOG3("looking up field " << field);
auto name = ::get(backend->scalarMetadataFields, field);
CHECK_NULL(name);
result->emplace("type", "field");
auto e = mkArrayField(result, "value");
e->append(scalarsName);
e->append(name);
} else {
// This may be wrong, but the caller will handle it properly
// (e.g., this can be a method, such as packet.lookahead)
result->emplace("type", "header");
result->emplace("value", fieldName);
}
}
} else {
bool done = false;
if (expression->expr->is<IR::Member>()) {
// array.next.field => type: "stack_field", value: [ array, field ]
auto mem = expression->expr->to<IR::Member>();
auto memtype = backend->getTypeMap()->getType(mem->expr, true);
if (memtype->is<IR::Type_Stack>() && mem->member == IR::Type_Stack::last) {
auto l = get(mem->expr);
CHECK_NULL(l);
result->emplace("type", "stack_field");
auto e = mkArrayField(result, "value");
if (l->is<Util::JsonObject>())
e->append(l->to<Util::JsonObject>()->get("value"));
else
e->append(l);
e->append(fieldName);
done = true;
}
}
if (!done) {
auto l = get(expression->expr);
CHECK_NULL(l);
if (parentType->is<IR::Type_HeaderUnion>()) {
BUG_CHECK(l->is<Util::JsonObject>(), "Not a JsonObject");
auto lv = l->to<Util::JsonObject>()->get("value");
BUG_CHECK(lv->is<Util::JsonValue>(), "Not a JsonValue");
fieldName = lv->to<Util::JsonValue>()->getString() + "." + fieldName;
// Each header in a union is allocated a separate header instance.
// Refer to that instance directly.
result->emplace("type", "header");
result->emplace("value", fieldName);
} else {
result->emplace("type", "field");
auto e = mkArrayField(result, "value");
if (l->is<Util::JsonObject>()) {
auto lv = l->to<Util::JsonObject>()->get("value");
if (lv->is<Util::JsonArray>()) {
// TODO: is this case still necessary after eliminating nested structs?
// nested struct reference [ ["m", "f"], "x" ] => [ "m", "f.x" ]
auto array = lv->to<Util::JsonArray>();
BUG_CHECK(array->size() == 2, "expected 2 elements");
auto first = array->at(0);
auto second = array->at(1);
BUG_CHECK(second->is<Util::JsonValue>(), "expected a value");
e->append(first);
cstring nestedField = second->to<Util::JsonValue>()->getString();
nestedField += "." + fieldName;
e->append(nestedField);
} else if (lv->is<Util::JsonValue>()) {
e->append(lv);
e->append(fieldName);
} else {
BUG("%1%: Unexpected json", lv);
}
} else {
e->append(l);
e->append(fieldName);
}
}
}
}
map.emplace(expression, result);
}
Util::IJson* ExpressionConverter::fixLocal(Util::IJson* json) {
if (json->is<Util::JsonObject>()) {
auto jo = json->to<Util::JsonObject>();
auto to = jo->get("type");
if (to != nullptr && to->to<Util::JsonValue>() != nullptr &&
(*to->to<Util::JsonValue>()) == "runtime_data") {
auto result = new Util::JsonObject();
result->emplace("type", "local");
result->emplace("value", jo->get("value"));
return result;
}
}
return json;
}
void ExpressionConverter::postorder(const IR::Mux* expression) {
auto result = new Util::JsonObject();
map.emplace(expression, result);
if (simpleExpressionsOnly) {
::error("%1%: expression to complex for this target", expression);
return;
}
result->emplace("type", "expression");
auto e = new Util::JsonObject();
result->emplace("value", e);
e->emplace("op", "?");
auto l = get(expression->e1);
e->emplace("left", fixLocal(l));
auto r = get(expression->e2);
e->emplace("right", fixLocal(r));
auto c = get(expression->e0);
e->emplace("cond", fixLocal(c));
}
void ExpressionConverter::postorder(const IR::IntMod* expression) {
auto result = new Util::JsonObject();
map.emplace(expression, result);
result->emplace("type", "expression");
auto e = new Util::JsonObject();
result->emplace("value", e);
e->emplace("op", "two_comp_mod");
auto l = get(expression->expr);
e->emplace("left", fixLocal(l));
auto r = new Util::JsonObject();
r->emplace("type", "hexstr");
cstring repr = stringRepr(expression->width);
r->emplace("value", repr);
e->emplace("right", r);
}
void ExpressionConverter::postorder(const IR::Operation_Binary* expression) {
binary(expression);
}
void ExpressionConverter::binary(const IR::Operation_Binary* expression) {
auto result = new Util::JsonObject();
map.emplace(expression, result);
if (simpleExpressionsOnly) {
::error("%1%: expression to complex for this target", expression);
return;
}
result->emplace("type", "expression");
auto e = new Util::JsonObject();
result->emplace("value", e);
cstring op = expression->getStringOp();
if (op == "&&")
op = "and";
else if (op == "||")
op = "or";
e->emplace("op", op);
auto l = get(expression->left);
e->emplace("left", fixLocal(l));
auto r = get(expression->right);
e->emplace("right", fixLocal(r));
}
void ExpressionConverter::postorder(const IR::ListExpression* expression) {
auto result = new Util::JsonArray();
map.emplace(expression, result);
if (simpleExpressionsOnly) {
::error("%1%: expression to complex for this target", expression);
return;
}
for (auto e : expression->components) {
auto t = get(e);
result->append(t);
}
}
void ExpressionConverter::postorder(const IR::Operation_Unary* expression) {
auto result = new Util::JsonObject();
map.emplace(expression, result);
if (simpleExpressionsOnly) {
::error("%1%: expression to complex for this target", expression);
return;
}
result->emplace("type", "expression");
auto e = new Util::JsonObject();
result->emplace("value", e);
cstring op = expression->getStringOp();
if (op == "!")
op = "not";
e->emplace("op", op);
e->emplace("left", Util::JsonValue::null);
auto r = get(expression->expr);
e->emplace("right", fixLocal(r));
}
void ExpressionConverter::postorder(const IR::PathExpression* expression) {
// This is useful for action bodies mostly
auto decl = backend->getRefMap()->getDeclaration(expression->path, true);
if (auto param = decl->to<IR::Parameter>()) {
if (backend->getStructure().nonActionParameters.find(param) !=
backend->getStructure().nonActionParameters.end()) {
map.emplace(expression, new Util::JsonValue(param->name.name));
return;
}
auto result = new Util::JsonObject();
result->emplace("type", "runtime_data");
unsigned paramIndex = ::get(&backend->getStructure().index, param);
result->emplace("value", paramIndex);
map.emplace(expression, result);
} else if (auto var = decl->to<IR::Declaration_Variable>()) {
LOG3("Variable to json " << var);
auto result = new Util::JsonObject();
auto type = backend->getTypeMap()->getType(var, true);
if (type->is<IR::Type_StructLike>()) {
result->emplace("type", "header");
result->emplace("value", var->name);
} else if (type->is<IR::Type_Bits>() ||
(type->is<IR::Type_Boolean>() && leftValue)) {
// no conversion d2b when writing (leftValue is true) to a boolean
result->emplace("type", "field");
auto e = mkArrayField(result, "value");
e->append(scalarsName);
e->append(var->name);
} else if (type->is<IR::Type_Varbits>()) {
// varbits are synthesized in separate metadata instances
// with a single field each, where the field is named
// "field".
result->emplace("type", "field");
auto e = mkArrayField(result, "value");
e->append(var->name);
e->append("field");
} else if (type->is<IR::Type_Boolean>()) {
// Boolean variables are stored as ints, so we have to insert a conversion when
// reading such a variable
result->emplace("type", "expression");
auto e = new Util::JsonObject();
result->emplace("value", e);
e->emplace("op", "d2b"); // data to Boolean cast
e->emplace("left", Util::JsonValue::null);
auto r = new Util::JsonObject();
e->emplace("right", r);
r->emplace("type", "field");
auto f = mkArrayField(r, "value");
f->append(scalarsName);
f->append(var->name);
} else if (type->is<IR::Type_Stack>()) {
result->emplace("type", "header_stack");
result->emplace("value", var->name);
} else if (type->is<IR::Type_Error>()) {
result->emplace("type", "field");
auto f = mkArrayField(result, "value");
f->append(scalarsName);
f->append(var->name);
} else {
BUG("%1%: type not yet handled", type);
}
map.emplace(expression, result);
}
}
void ExpressionConverter::postorder(const IR::TypeNameExpression* expression) {
(void)expression;
}
void ExpressionConverter::postorder(const IR::Expression* expression) {
BUG("%1%: Unhandled case", expression);
}
// doFixup = true -> insert masking operations for proper arithmetic implementation
// see below for wrap
Util::IJson*
ExpressionConverter::convert(const IR::Expression* e, bool doFixup, bool wrap, bool convertBool) {
const IR::Expression *expr = e;
if (doFixup) {
ArithmeticFixup af(backend->getTypeMap());
auto r = e->apply(af);
CHECK_NULL(r);
expr = r->to<IR::Expression>();
CHECK_NULL(expr);
}
expr->apply(*this);
auto result = ::get(map, expr->to<IR::Expression>());
if (result == nullptr)
BUG("%1%: Could not convert expression", e);
if (convertBool) {
auto obj = new Util::JsonObject();
obj->emplace("type", "expression");
auto conv = new Util::JsonObject();
obj->emplace("value", conv);
conv->emplace("op", "b2d"); // boolean to data cast
conv->emplace("left", Util::JsonValue::null);
conv->emplace("right", result);
result = obj;
}
std::set<cstring> to_wrap({"expression", "stack_field"});
// This is weird, but that's how it is: expression and stack_field must be wrapped in
// another outer object. In a future version of the bmv2 JSON, this will not be needed
// anymore as expressions will be treated in a more uniform way.
if (wrap && result->is<Util::JsonObject>()) {
auto to = result->to<Util::JsonObject>()->get("type");
if (to != nullptr && to->to<Util::JsonValue>() != nullptr) {
auto jv = *to->to<Util::JsonValue>();
if (jv.isString() && to_wrap.find(jv.getString()) != to_wrap.end()) {
auto rwrap = new Util::JsonObject();
rwrap->emplace("type", "expression");
rwrap->emplace("value", result);
result = rwrap;
}
}
}
return result;
}
Util::IJson* ExpressionConverter::convertLeftValue(const IR::Expression* e) {
leftValue = true;
const IR::Expression *expr = e;
ArithmeticFixup af(backend->getTypeMap());
auto r = e->apply(af);
CHECK_NULL(r);
expr = r->to<IR::Expression>();
CHECK_NULL(expr);
expr->apply(*this);
auto result = ::get(map, expr->to<IR::Expression>());
if (result == nullptr)
BUG("%1%: Could not convert expression", e);
leftValue = false;
return result;
}
} // namespace BMV2