-
Notifications
You must be signed in to change notification settings - Fork 453
/
Copy pathexpression.cpp
841 lines (776 loc) · 33.1 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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
/*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 "helpers.h"
#include "lib/algorithm.h"
namespace P4::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>() ||
expression->is<IR::AddSat>() || expression->is<IR::SubSat>())
// 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::Cmpl *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);
}
void ExpressionConverter::mapExpression(const IR::Expression *expression, Util::IJson *json) {
map.emplace(expression, json);
LOG3("Mapping " << dbp(expression) << " to " << json->toString());
}
Util::IJson *ExpressionConverter::get(const IR::Expression *expression) const {
auto result = ::P4::get(map, expression);
if (result == nullptr) {
LOG3("Looking up " << expression);
for (auto it : map) {
LOG3(" " << it.first << " " << it.second);
}
}
if (result == nullptr)
::P4::error(ErrorType::ERR_UNSUPPORTED, "%1%: could not convert expression 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);
mapExpression(expression, result);
}
void ExpressionConverter::postorder(const IR::MethodCallExpression *expression) {
auto instance = P4::MethodInstance::resolve(expression, refMap, typeMap);
if (auto em = instance->to<P4::ExternMethod>()) {
if (em->originalExternType->name == corelib.packetIn.name &&
em->method->name == corelib.packetIn.lookahead.name) {
if (expression->typeArguments->size() != 1)
::P4::error(ErrorType::ERR_INVALID, "Expected 1 type parameter for %1%",
em->method);
auto targ = expression->typeArguments->at(0);
auto typearg = typeMap->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"_cs);
v->append(0);
v->append(width);
mapExpression(expression, j);
return;
}
} else if (auto bim = instance->to<P4::BuiltInMethod>()) {
if (bim->name == IR::Type_Header::isValid) {
auto type = typeMap->getType(bim->appliedTo, true);
auto result = new Util::JsonObject();
auto l = get(bim->appliedTo);
if (!l) return;
if (type->is<IR::Type_HeaderUnion>()) {
result->emplace("type", "expression");
auto e = new Util::JsonObject();
result->emplace("value", e);
e->emplace("op", "valid_union");
e->emplace("left", Util::JsonValue::null);
e->emplace("right", l);
} else {
// Treat this as appliedTo.$valid$
result->emplace("type", "field");
auto e = mkArrayField(result, "value"_cs);
if (l->is<Util::JsonObject>())
e->append(l->to<Util::JsonObject>()->get("value"));
else
e->append(l);
e->append(V1ModelProperties::validField);
if (!simpleExpressionsOnly && !leftValue) {
// This is set when converting table keys;
// for table keys we don't need the casts.
auto cast = new Util::JsonObject();
auto value = new Util::JsonObject();
cast->emplace("type", "expression");
cast->emplace("value", value);
value->emplace("op", "d2b"); // data to Boolean cast
value->emplace("left", Util::JsonValue::null);
value->emplace("right", result);
result = cast;
}
}
mapExpression(expression, result);
return;
}
}
::P4::error(ErrorType::ERR_UNSUPPORTED_ON_TARGET, "%1%: not supported", 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);
if (!j) return;
mapExpression(expression, j);
}
void ExpressionConverter::postorder(const IR::Constant *expression) {
auto result = new Util::JsonObject();
result->emplace("type", "hexstr");
auto bitwidth = expression->type->width_bits();
cstring repr = stringRepr(expression->value, ROUNDUP(bitwidth, 8));
result->emplace("value", repr);
if (withConstantWidths) result->emplace("bitwidth", bitwidth);
mapExpression(expression, result);
}
void ExpressionConverter::postorder(const IR::ArrayIndex *expression) {
auto result = new Util::JsonObject();
cstring elementAccess;
LOG2("left: " << expression->left << " right: " << expression->right);
// This is can be either a header, which is part of the "headers" parameter
// or a temporary array.
if (auto mem = expression->left->to<IR::Member>()) {
// This is a header part of the parameters
auto parentType = typeMap->getType(mem->expr, true);
BUG_CHECK(parentType->is<IR::Type_StructLike>(), "%1%: expected a struct", parentType);
auto st = parentType->to<IR::Type_StructLike>();
auto field = st->getField(mem->member);
elementAccess = field->controlPlaneName();
} else if (auto path = expression->left->to<IR::PathExpression>()) {
// This is a temporary variable with type stack.
elementAccess = path->path->name;
}
if (!expression->right->is<IR::Constant>()) {
const IR::Expression *ex = expression->right;
auto fresult = ::P4::get(map, ex);
if (fresult == nullptr) {
LOG2("Looking up " << ex);
for (auto it : map) {
LOG3(" " << it.first << " " << it.second);
}
}
BUG_CHECK(fresult, "%1%: Runtime array index json generation failed", ex);
Util::JsonObject *fres = fresult->to<Util::JsonObject>();
result->emplace("type", "expression");
auto e = new Util::JsonObject();
e->emplace("op", "dereference_header_stack");
auto l = new Util::JsonObject();
l->emplace("type", "header_stack");
l->emplace("value", elementAccess);
e->emplace("left", l);
e->emplace("right", fres);
result->emplace("value", e);
} else {
result->emplace("type", "header");
int index = expression->right->to<IR::Constant>()->asInt();
elementAccess += "[" + Util::toString(index) + "]";
result->emplace("value", elementAccess);
}
mapExpression(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 = refMap->getDeclaration(pe->path, true);
auto param = decl->to<IR::Parameter>();
if (param == nullptr) return param;
if (structure->nonActionParameters.count(param) > 0) return param;
return nullptr;
}
void ExpressionConverter::postorder(const IR::Member *expression) {
auto result = new Util::JsonObject();
int index_pos = 0;
auto parentType = typeMap->getType(expression->expr, true);
cstring fieldName = expression->member.name;
auto type = typeMap->getType(expression, true);
if (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();
index_pos = st->getFieldIndex(fieldName);
}
}
// handle error
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);
auto errorValue = structure->errorCodesMap.at(decl);
// this generates error constant like hex value
auto reprValue = stringRepr(errorValue);
result->emplace("value", reprValue);
mapExpression(expression, result);
return;
}
auto param = enclosingParamReference(expression->expr);
if (param != nullptr) {
// convert architecture-dependent parameter
if (auto result = convertParam(param, fieldName)) {
mapExpression(expression, result);
return;
}
// convert normal parameters
if (auto st = type->to<IR::Type_Stack>()) {
auto et = typeMap->getTypeType(st->elementType, true);
if (et->is<IR::Type_HeaderUnion>())
result->emplace("type", "header_union_stack");
else
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);
if (!l) return;
cstring nestedField = fieldName;
if (auto lv = l->to<Util::JsonObject>()) {
lv->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_Error>() ||
type->is<IR::Type_Boolean>())) {
auto field = parentType->to<IR::Type_StructLike>()->getField(expression->member);
LOG3("looking up field " << field);
CHECK_NULL(field);
auto name = ::P4::get(structure->scalarMetadataFields, field);
BUG_CHECK((name != nullptr), "NULL name: %1%", field->name);
if (type->is<IR::Type_Bits>() || type->is<IR::Type_Error>() || leftValue ||
simpleExpressionsOnly) {
result->emplace("type", "field");
auto e = mkArrayField(result, "value"_cs);
e->append(scalarsName);
e->append(name);
} 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 a = mkArrayField(r, "value"_cs);
a->append(scalarsName);
a->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);
}
mapExpression(expression, result);
return;
}
bool done = false;
if (expression->expr->is<IR::Member>()) {
auto mem = expression->expr->to<IR::Member>();
auto memtype = typeMap->getType(mem->expr, true);
if (memtype->is<IR::Type_Stack>() && mem->member == IR::Type_Stack::next)
::P4::error(ErrorType::ERR_UNINITIALIZED, "%1% uninitialized: next field read", mem);
// array.last.field => type: "stack_field", value: [ array, field ]
if (memtype->is<IR::Type_Stack>() && mem->member == IR::Type_Stack::last) {
auto l = get(mem->expr);
if (!l) return;
result->emplace("type", "stack_field");
auto e = mkArrayField(result, "value"_cs);
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);
if (!l) return;
if (parentType->is<IR::Type_HeaderUnion>()) {
BUG_CHECK(l->is<Util::JsonObject>(), "Not a JsonObject");
auto lv = l->to<Util::JsonObject>()->get("value");
if (lv->is<Util::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 {
// lv must be a reference to a union stack field
auto a = lv->to<Util::JsonArray>()->clone();
CHECK_NULL(a);
result->emplace("type", "union_stack_field");
a->append(fieldName);
result->emplace("value"_cs, a);
}
} else if (parentType->is<IR::Type_Stack>() &&
expression->member == IR::Type_Stack::lastIndex) {
auto l = get(expression->expr);
if (!l) return;
result->emplace("type", "expression");
auto e = new Util::JsonObject();
result->emplace("value", e);
e->emplace("op", "last_stack_index");
e->emplace("left", Util::JsonValue::null);
e->emplace("right", l);
} else {
const char *fieldRef = parentType->is<IR::Type_Stack>() ? "stack_field" : "field";
Util::JsonArray *e = nullptr;
bool st = isArrayIndexRuntime(expression);
if (!st) {
result->emplace("type", fieldRef);
e = mkArrayField(result, "value"_cs);
}
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");
CHECK_NULL(e);
e->append(first);
cstring nestedField = second->to<Util::JsonValue>()->getString();
nestedField += "." + fieldName;
e->append(nestedField);
} else if (lv->is<Util::JsonValue>()) {
CHECK_NULL(e);
e->append(lv);
e->append(fieldName);
} else if (auto jo = l->to<Util::JsonObject>()) {
if (st) {
if (index_pos < 0) {
::P4::error(ErrorType::ERR_INVALID,
"BMV2: Struct has no field "
"for runtime index computation %1%",
st);
}
result->emplace("type", "expression");
auto e = new Util::JsonObject();
result->emplace("value", e);
e->emplace("op", "access_field");
e->emplace("left", jo);
e->emplace("right", index_pos);
}
} else {
BUG("%1%: Unexpected json", lv);
}
} else {
CHECK_NULL(e);
e->append(l);
e->append(fieldName);
}
if (!simpleExpressionsOnly && !leftValue && type->is<IR::Type_Boolean>()) {
auto cast = new Util::JsonObject();
auto value = new Util::JsonObject();
cast->emplace("type", "expression");
cast->emplace("value", value);
value->emplace("op", "d2b"); // data to Boolean cast
value->emplace("left", Util::JsonValue::null);
value->emplace("right", result);
result = cast;
}
}
}
mapExpression(expression, result);
}
Util::IJson *ExpressionConverter::fixLocal(Util::IJson *json) {
if (!json) return new Util::JsonValue(); // null
if (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();
mapExpression(expression, result);
if (simpleExpressionsOnly) {
::P4::error(ErrorType::ERR_UNSUPPORTED_ON_TARGET,
"%1%: expression too 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);
if (!l) return;
e->emplace("left"_cs, fixLocal(l));
auto r = get(expression->e2);
if (!r) return;
e->emplace("right"_cs, fixLocal(r));
auto c = get(expression->e0);
if (!c) return;
e->emplace("cond"_cs, fixLocal(c));
}
void ExpressionConverter::postorder(const IR::IntMod *expression) {
auto result = new Util::JsonObject();
mapExpression(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);
if (!l) return;
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();
mapExpression(expression, result);
if (simpleExpressionsOnly) {
::P4::error(ErrorType::ERR_UNSUPPORTED_ON_TARGET,
"%1%: expression too 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"_cs;
else if (op == "||")
op = "or"_cs;
e->emplace("op", op);
auto l = get(expression->left);
if (!l) return;
e->emplace("left", fixLocal(l));
auto r = get(expression->right);
if (!r) return;
e->emplace("right", fixLocal(r));
}
void ExpressionConverter::saturated_binary(const IR::Operation_Binary *expression) {
// This should never happen if we correctly typecheck the program
BUG_CHECK(expression->type->is<IR::Type_Bits>(), "saturated arithmetic requires bit types");
auto result = new Util::JsonObject();
mapExpression(expression, result);
result->emplace("type", "expression");
auto e = new Util::JsonObject();
result->emplace("value", e);
auto eType = expression->type->to<IR::Type_Bits>();
CHECK_NULL(eType);
auto opType = eType->isSigned ? "sat_cast" : "usat_cast";
e->emplace("op", opType);
// the left operand is the binary expression, but as a simple add/sub
auto eLeft = new Util::JsonObject();
eLeft->emplace("type", "expression");
auto e1 = new Util::JsonObject();
e1->emplace("op", expression->getStringOp() == "|+|" ? "+" : "-");
e1->emplace("left", fixLocal(get(expression->left)));
e1->emplace("right", fixLocal(get(expression->right)));
eLeft->emplace("value", e1);
e->emplace("left", eLeft);
// the right operand is the width of the type
auto r = new Util::JsonObject();
r->emplace("type", "hexstr");
cstring repr = stringRepr(eType->width_bits());
r->emplace("value", repr);
e->emplace("right", r);
}
void ExpressionConverter::postorder(const IR::ListExpression *expression) {
auto result = new Util::JsonArray();
mapExpression(expression, result);
if (simpleExpressionsOnly) {
::P4::error(ErrorType::ERR_UNSUPPORTED_ON_TARGET,
"%1%: expression too complex for this target", expression);
return;
}
for (auto e : expression->components) {
auto t = get(e);
if (!t) return;
result->append(t);
}
}
void ExpressionConverter::postorder(const IR::StructExpression *expression) {
// Handle like a ListExpression
auto result = new Util::JsonArray();
mapExpression(expression, result);
if (simpleExpressionsOnly) {
::P4::error(ErrorType::ERR_UNSUPPORTED_ON_TARGET,
"%1%: expression too complex for this target", expression);
return;
}
for (auto e : expression->components) {
auto t = get(e->expression);
if (!t) return;
result->append(t);
}
}
void ExpressionConverter::postorder(const IR::Operation_Unary *expression) {
auto result = new Util::JsonObject();
mapExpression(expression, result);
if (simpleExpressionsOnly) {
::P4::error(ErrorType::ERR_UNSUPPORTED_ON_TARGET,
"%1%: expression too 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"_cs;
e->emplace("op", op);
e->emplace("left", Util::JsonValue::null);
auto r = get(expression->expr);
if (!r) return;
e->emplace("right", fixLocal(r));
}
void ExpressionConverter::postorder(const IR::PathExpression *expression) {
// This is useful for action bodies mostly
auto decl = refMap->getDeclaration(expression->path, true);
if (auto param = decl->to<IR::Parameter>()) {
if (structure->nonActionParameters.find(param) != structure->nonActionParameters.end()) {
auto type = typeMap->getType(param, true);
if (type->is<IR::Type_StructLike>()) {
auto result = convertParam(param, cstring::empty);
if (result == nullptr) {
auto r = new Util::JsonObject();
r->emplace("type", "header");
r->emplace("value", param->name.name);
result = r;
}
mapExpression(expression, result);
} else {
mapExpression(expression, new Util::JsonValue(param->name.name));
}
return;
}
auto result = new Util::JsonObject();
result->emplace("type", "runtime_data");
unsigned paramIndex = ::P4::get(&structure->index, param);
result->emplace("value", paramIndex);
mapExpression(expression, result);
} else if (auto var = decl->to<IR::Declaration_Variable>()) {
LOG3("Variable to json " << var);
auto result = new Util::JsonObject();
auto type = typeMap->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 || simpleExpressionsOnly))) {
// no conversion d2b when writing (leftValue is true) to a boolean
result->emplace("type", "field");
auto e = mkArrayField(result, "value"_cs);
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"_cs);
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"_cs);
f->append(scalarsName);
f->append(var->name);
} else if (auto st = type->to<IR::Type_Stack>()) {
auto et = typeMap->getTypeType(st->elementType, true);
if (et->is<IR::Type_HeaderUnion>())
result->emplace("type", "header_union_stack");
else
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"_cs);
f->append(scalarsName);
f->append(var->name);
} else {
BUG("%1%: type not yet handled", type);
}
mapExpression(expression, result);
}
}
void ExpressionConverter::postorder(const IR::StringLiteral *expression) {
auto result = new Util::JsonObject();
result->emplace("type", "string");
result->emplace("value", expression->value);
mapExpression(expression, result);
}
void ExpressionConverter::postorder(const IR::TypeNameExpression *expression) { (void)expression; }
void ExpressionConverter::postorder(const IR::Slice *expression) {
auto result = new Util::JsonObject();
auto expr = expression->e0;
int h = expression->getH();
int l = expression->getL();
auto mask = Util::maskFromSlice(h, l);
result->emplace("type", "expression");
auto band = new Util::JsonObject();
result->emplace("value", band);
band->emplace("op", "&");
auto right = new Util::JsonObject();
auto bitwidth = expression->type->width_bits();
right->emplace("type", "hexstr");
right->emplace("value", stringRepr(mask, ROUNDUP(bitwidth, 8)));
auto le = get(expr);
if (!le) return;
band->emplace("left"_cs, le);
band->emplace("right"_cs, right);
mapExpression(expression, result);
}
void ExpressionConverter::postorder(const IR::Expression *expression) {
BUG("%1%: Unhandled case", expression);
}
bool ExpressionConverter::isArrayIndexRuntime(const IR::Expression *e) {
if (auto mem = e->to<IR::Member>()) {
if (auto ai = mem->expr->to<IR::ArrayIndex>()) {
auto right = ai->right;
if (!right->is<IR::Constant>()) {
return true;
}
}
}
return false;
}
/// 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(typeMap);
auto r = e->apply(af);
CHECK_NULL(r);
expr = r->to<IR::Expression>();
CHECK_NULL(expr);
}
expr->apply(*this);
auto result = ::P4::get(map, expr->to<IR::Expression>());
if (result == nullptr) {
::P4::error(ErrorType::ERR_UNSUPPORTED_ON_TARGET,
"%1%: Could not generate code for expression", e);
return new Util::JsonValue();
}
auto type = typeMap->getType(e, true);
if (convertBool && type->is<IR::Type_Boolean>()) {
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"_cs, "stack_field"_cs});
// 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) {
if (auto ro = result->to<Util::JsonObject>()) {
if (auto to = ro->get("type")) {
if (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(typeMap);
auto r = e->apply(af);
CHECK_NULL(r);
expr = r->to<IR::Expression>();
CHECK_NULL(expr);
expr->apply(*this);
auto result = ::P4::get(map, expr->to<IR::Expression>());
if (result == nullptr) BUG("%1%: Could not convert expression", e);
leftValue = false;
return result;
}
Util::IJson *ExpressionConverter::convertWithConstantWidths(const IR::Expression *e) {
withConstantWidths = true;
auto result = convert(e);
withConstantWidths = false;
return result;
}
} // namespace P4::BMV2