-
Notifications
You must be signed in to change notification settings - Fork 457
/
Copy pathcomplexComparison.cpp
164 lines (152 loc) · 6.9 KB
/
complexComparison.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
/*
Copyright 2017 VMware, 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 "complexComparison.h"
namespace P4 {
const IR::Expression *RemoveComplexComparisons::explode(Util::SourceInfo srcInfo,
const IR::Type *leftType,
const IR::Expression *left,
const IR::Type *rightType,
const IR::Expression *right) {
// we allow several cases
// header == header
// header == list
// list == header
// list == list
// struct == struct
// array == array
// struct == list
// list == struct
auto rightTuple = rightType->to<IR::Type_BaseList>();
auto leftTuple = leftType->to<IR::Type_BaseList>();
if (leftTuple && !rightTuple)
// put the tuple on the right if it is the only one,
// so we handle fewer cases
return explode(srcInfo, rightType, right, leftType, left);
if (auto ht = leftType->to<IR::Type_Header>()) {
auto lmethod = new IR::Member(left, IR::Type_Header::isValid);
const IR::Expression *lvalid;
if (left->is<IR::StructExpression>()) {
// A header defined this way is always valid
lvalid = new IR::BoolLiteral(true);
} else {
lvalid = new IR::MethodCallExpression(srcInfo, lmethod);
}
auto linvalid = new IR::LNot(srcInfo, lvalid);
const IR::Expression *rvalid;
if (!rightTuple) {
if (right->is<IR::StructExpression>()) {
rvalid = new IR::BoolLiteral(true);
} else {
auto rmethod = new IR::Member(right, IR::Type_Header::isValid);
rvalid = new IR::MethodCallExpression(srcInfo, rmethod);
}
} else {
rvalid = new IR::BoolLiteral(true);
}
auto rinvalid = new IR::LNot(srcInfo, rvalid);
auto invalid = new IR::LAnd(srcInfo, linvalid, rinvalid);
auto valid = new IR::LAnd(srcInfo, lvalid, rvalid);
size_t index = 0;
for (auto f : ht->fields) {
auto ftype = f->type;
auto fleft = new IR::Member(srcInfo, left, f->name);
const IR::Expression *fright;
const IR::Type *rightType;
if (!rightTuple) {
if (auto si = right->to<IR::StructExpression>()) {
auto nf = si->components.getDeclaration<IR::NamedExpression>(f->name);
CHECK_NULL(nf);
fright = nf->expression;
} else {
fright = new IR::Member(srcInfo, right, f->name);
}
rightType = f->type;
} else {
fright = right->to<IR::ListExpression>()->components.at(index);
rightType = rightTuple->components.at(index);
}
auto rec = explode(srcInfo, ftype, fleft, rightType, fright);
valid = new IR::LAnd(srcInfo, valid, rec);
index++;
}
auto result = new IR::LOr(srcInfo, invalid, valid);
return result;
} else if (auto st = leftType->to<IR::Type_StructLike>()) {
// Works for structs and unions
const IR::Expression *result = new IR::BoolLiteral(true);
size_t index = 0;
for (auto f : st->fields) {
auto ftype = f->type;
auto fleft = new IR::Member(srcInfo, left, f->name);
const IR::Expression *fright;
const IR::Type *rightType;
if (!rightTuple) {
if (auto si = right->to<IR::StructExpression>()) {
auto nf = si->components.getDeclaration<IR::NamedExpression>(f->name);
CHECK_NULL(nf);
fright = nf->expression;
} else {
fright = new IR::Member(srcInfo, right, f->name);
}
rightType = f->type;
} else {
fright = right->to<IR::ListExpression>()->components.at(index);
rightType = rightTuple->components.at(index);
}
index++;
auto rec = explode(srcInfo, ftype, fleft, rightType, fright);
result = new IR::LAnd(srcInfo, result, rec);
}
return result;
} else if (auto at = leftType->to<IR::Type_Stack>()) {
auto size = at->getSize();
const IR::Expression *result = new IR::BoolLiteral(true);
BUG_CHECK(rightType->is<IR::Type_Stack>(), "%1%: comparing stack with %1%", left,
rightType);
for (unsigned i = 0; i < size; i++) {
auto index = new IR::Constant(i);
auto lelem = new IR::ArrayIndex(srcInfo, left, index);
auto relem = new IR::ArrayIndex(srcInfo, right, index);
auto rec = explode(srcInfo, at->elementType, lelem, at->elementType, relem);
result = new IR::LAnd(srcInfo, result, rec);
}
return result;
} else if (leftTuple) {
BUG_CHECK(rightTuple, "%1% vs %2%: unexpected comparison", left, right);
const IR::Expression *result = new IR::BoolLiteral(true);
auto leftList = left->to<IR::ListExpression>();
for (size_t index = 0; index < leftList->components.size(); index++) {
auto fleft = leftList->components.at(index);
auto leftType = leftTuple->components.at(index);
auto fright = right->to<IR::ListExpression>()->components.at(index);
auto rightType = rightTuple->components.at(index);
auto rec = explode(srcInfo, leftType, fleft, rightType, fright);
result = new IR::LAnd(srcInfo, result, rec);
}
return result;
} else {
return new IR::Equ(srcInfo, left, right);
}
}
const IR::Node *RemoveComplexComparisons::postorder(IR::Operation_Binary *expression) {
if (!expression->is<IR::Neq>() && !expression->is<IR::Equ>()) return expression;
auto ltype = typeMap->getType(expression->left, true);
auto rtype = typeMap->getType(expression->right, true);
if (!ltype->is<IR::Type_StructLike>() && !ltype->is<IR::Type_Stack>() &&
!ltype->is<IR::Type_BaseList>())
return expression;
auto result = explode(expression->srcInfo, ltype, expression->left, rtype, expression->right);
if (expression->is<IR::Neq>()) result = new IR::LNot(expression->srcInfo, result);
return result;
}
} // namespace P4