Repository navigation
Expand file tree
/
Copy pathlower.cpp
More file actions
373 lines (339 loc) · 14.6 KB
/
Copy pathlower.cpp
File metadata and controls
373 lines (339 loc) · 14.6 KB
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
/*
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 "lower.h"
#include "frontends/p4/coreLibrary.h"
#include "frontends/p4/methodInstance.h"
#include "frontends/p4/fromv1.0/v1model.h"
#include "lib/gmputil.h"
namespace PSDN {
// We make an effort to update the typeMap as we proceed
// since parent expression trees may need the information
// when processing in post-order.
const IR::Expression* LowerExpressions::shift(const IR::Operation_Binary* expression) const {
auto rhs = expression->right;
auto rhstype = typeMap->getType(rhs, true);
if (rhstype->is<IR::Type_InfInt>()) {
auto cst = rhs->to<IR::Constant>();
mpz_class maxShift = Util::shift_left(1, LowerExpressions::maxShiftWidth);
if (cst->value > maxShift)
::error(ErrorType::ERR_OVERLIMIT, "%1%: shift amount limited to %2% on this target",
expression, maxShift);
} else {
BUG_CHECK(rhstype->is<IR::Type_Bits>(), "%1%: expected a bit<> type", rhstype);
auto bs = rhstype->to<IR::Type_Bits>();
if (bs->size > LowerExpressions::maxShiftWidth)
::error(ErrorType::ERR_OVERLIMIT,
"%1%: shift amount limited to %2% bits on this target",
expression, LowerExpressions::maxShiftWidth);
}
auto ltype = typeMap->getType(getOriginal(), true);
typeMap->setType(expression, ltype);
return expression;
}
const IR::Node* LowerExpressions::postorder(IR::Neg* expression) {
auto type = typeMap->getType(getOriginal(), true);
auto zero = new IR::Constant(type, 0);
auto sub = new IR::Sub(expression->srcInfo, zero, expression->expr);
typeMap->setType(zero, type);
typeMap->setType(sub, type);
LOG3("Replaced " << expression << " with " << sub);
return sub;
}
const IR::Node* LowerExpressions::postorder(IR::Cast* expression) {
// handle bool <-> bit casts
auto destType = typeMap->getType(getOriginal(), true);
auto srcType = typeMap->getType(expression->expr, true);
if (destType->is<IR::Type_Boolean>() && srcType->is<IR::Type_Bits>()) {
auto zero = new IR::Constant(srcType, 0);
auto cmp = new IR::Neq(expression->srcInfo, expression->expr, zero);
typeMap->setType(cmp, destType);
LOG3("Replaced " << expression << " with " << cmp);
return cmp;
} else if (destType->is<IR::Type_Bits>() && srcType->is<IR::Type_Boolean>()) {
auto mux = new IR::Mux(expression->srcInfo, expression->expr,
new IR::Constant(destType, 1),
new IR::Constant(destType, 0));
typeMap->setType(mux, destType);
LOG3("Replaced " << expression << " with " << mux);
return mux;
}
// This may be a new expression
typeMap->setType(expression, destType);
return expression;
}
const IR::Node* LowerExpressions::postorder(IR::Expression* expression) {
// Just update the typeMap incrementally.
auto type = typeMap->getType(getOriginal(), true);
typeMap->setType(expression, type);
return expression;
}
const IR::Node* LowerExpressions::postorder(IR::Slice* expression) {
// This is in a RHS expression a[m:l] -> (cast)(a >> l)
int h = expression->getH();
int l = expression->getL();
auto e0type = typeMap->getType(expression->e0, true);
BUG_CHECK(e0type->is<IR::Type_Bits>(), "%1%: expected a bit<> type", e0type);
const IR::Expression* expr;
if (l != 0) {
expr = new IR::Shr(expression->e0->srcInfo, expression->e0, new IR::Constant(l));
typeMap->setType(expr, e0type);
} else {
expr = expression->e0;
}
// Narrowing cast.
auto type = IR::Type_Bits::get(h - l + 1, e0type->to<IR::Type_Bits>()->isSigned);
auto result = new IR::Cast(expression->srcInfo, type, expr);
typeMap->setType(result, type);
// Signedness conversion.
type = IR::Type_Bits::get(h - l + 1, false);
result = new IR::Cast(expression->srcInfo, type, result);
typeMap->setType(result, type);
LOG3("Replaced " << expression << " with " << result);
return result;
}
const IR::Node* LowerExpressions::postorder(IR::Concat* expression) {
// a ++ b -> ((cast)a << sizeof(b)) | ((cast)b & mask)
auto type = typeMap->getType(expression->right, true);
auto resulttype = typeMap->getType(getOriginal(), true);
BUG_CHECK(type->is<IR::Type_Bits>(), "%1%: expected a bitstring got a %2%",
expression->right, type);
BUG_CHECK(resulttype->is<IR::Type_Bits>(), "%1%: expected a bitstring got a %2%",
expression->right, type);
unsigned sizeofb = type->to<IR::Type_Bits>()->size;
unsigned sizeofresult = resulttype->to<IR::Type_Bits>()->size;
auto cast0 = new IR::Cast(expression->left->srcInfo, resulttype, expression->left);
auto cast1 = new IR::Cast(expression->right->srcInfo, resulttype, expression->right);
auto sh = new IR::Shl(cast0->srcInfo, cast0, new IR::Constant(sizeofb));
mpz_class m = Util::maskFromSlice(sizeofb, 0);
auto mask = new IR::Constant(expression->right->srcInfo,
IR::Type_Bits::get(sizeofresult), m, 16);
auto and0 = new IR::BAnd(expression->right->srcInfo, cast1, mask);
auto result = new IR::BOr(expression->srcInfo, sh, and0);
typeMap->setType(cast0, resulttype);
typeMap->setType(cast1, resulttype);
typeMap->setType(result, resulttype);
typeMap->setType(sh, resulttype);
typeMap->setType(and0, resulttype);
LOG3("Replaced " << expression << " with " << result);
return result;
}
/////////////////////////////////////////////////////////////
namespace {
/**
Detect whether a Select expression is too complicated for BMv2.
Also used to detect complex expressions that are arguments
to method calls.
*/
class ComplexExpression : public Inspector {
public:
bool isComplex = false;
ComplexExpression() { setName("ComplexExpression"); }
void postorder(const IR::ArrayIndex*) override {}
void postorder(const IR::TypeNameExpression*) override {}
void postorder(const IR::PathExpression*) override {}
void postorder(const IR::Member*) override {}
void postorder(const IR::Literal*) override {}
// all other expressions are complex
void postorder(const IR::Expression*) override { isComplex = true; }
};
} // namespace
const IR::PathExpression*
RemoveComplexExpressions::createTemporary(const IR::Expression* expression) {
auto type = typeMap->getType(expression, true);
auto name = refMap->newName("tmp");
auto decl = new IR::Declaration_Variable(IR::ID(name), type->getP4Type());
newDecls.push_back(decl);
typeMap->setType(decl, type);
auto assign = new IR::AssignmentStatement(
expression->srcInfo, new IR::PathExpression(name), expression);
assignments.push_back(assign);
return new IR::PathExpression(expression->srcInfo, new IR::Path(name));
}
const IR::Vector<IR::Argument>*
RemoveComplexExpressions::simplifyExpressions(const IR::Vector<IR::Argument>* args) {
bool changes = true;
auto result = new IR::Vector<IR::Argument>();
for (auto arg : *args) {
auto r = simplifyExpression(arg->expression, false);
if (r != arg->expression) {
changes = true;
result->push_back(new IR::Argument(arg->srcInfo, arg->name, r));
} else {
result->push_back(arg);
}
}
if (changes)
return result;
return args;
}
const IR::Expression*
RemoveComplexExpressions::simplifyExpression(const IR::Expression* expression, bool force) {
// Note that 'force' is not applied recursively
if (auto list = expression->to<IR::ListExpression>()) {
auto simpl = simplifyExpressions(&list->components);
if (simpl != &list->components)
return new IR::ListExpression(expression->srcInfo, *simpl);
return expression;
} else if (auto si = expression->to<IR::StructInitializerExpression>()) {
auto simpl = simplifyExpressions(&si->components);
if (simpl != &si->components)
return new IR::StructInitializerExpression(
si->srcInfo, si->typeName, si->typeName, *simpl);
return expression;
} else {
ComplexExpression ce;
(void)expression->apply(ce);
if (force || ce.isComplex) {
LOG3("Moved into temporary " << dbp(expression));
return createTemporary(expression);
}
return expression;
}
}
const IR::Vector<IR::Expression>*
RemoveComplexExpressions::simplifyExpressions(const IR::Vector<IR::Expression>* vec, bool force) {
// This is more complicated than I'd like. If an expression is
// a list expression, then we actually simplify the elements
// of the list. Otherwise we simplify the argument itself.
// This is mostly for functions that take FieldLists - these
// should still take a list as argument.
bool changes = true;
auto result = new IR::Vector<IR::Expression>();
for (auto e : *vec) {
auto r = simplifyExpression(e, force);
if (r != e)
changes = true;
result->push_back(r);
}
if (changes)
return result;
return vec;
}
const IR::IndexedVector<IR::NamedExpression>*
RemoveComplexExpressions::simplifyExpressions(
const IR::IndexedVector<IR::NamedExpression>* vec) {
auto result = new IR::IndexedVector<IR::NamedExpression>();
bool changes = false;
for (auto e : *vec) {
auto r = simplifyExpression(e->expression, true);
if (r != e->expression) {
changes = true;
result->push_back(new IR::NamedExpression(e->srcInfo, e->name, r));
} else {
result->push_back(e);
}
}
if (changes)
return result;
return vec;
}
const IR::Node*
RemoveComplexExpressions::postorder(IR::SelectExpression* expression) {
auto vec = simplifyExpressions(&expression->select->components);
if (vec != &expression->select->components)
expression->select = new IR::ListExpression(expression->select->srcInfo, *vec);
return expression;
}
const IR::Node*
RemoveComplexExpressions::preorder(IR::P4Control* control) {
if (policy != nullptr && !policy->convert(control)) {
prune();
return control;
}
newDecls.clear();
return control;
}
const IR::Node*
RemoveComplexExpressions::postorder(IR::MethodCallExpression* expression) {
if (expression->arguments->size() == 0)
return expression;
auto mi = P4::MethodInstance::resolve(expression, refMap, typeMap);
if (mi->isApply() || mi->is<P4::BuiltInMethod>())
return expression;
if (auto ef = mi->to<P4::ExternFunction>()) {
if (ef->method->name == P4V1::V1Model::instance.digest_receiver.name) {
// Special handling for digest; the semantics on bmv2 is to
// execute the digest at the very end of the pipeline, and to
// pass a reference to the fields, so fields can be modified
// and the latest value is part of the digest. We want the
// digest to appear as if it is executed instantly, so we copy
// the data to temporaries. This could be a problem for P4-14
// programs that depend on this semantics, but we hope that no
// one knew of this feature, since it was not very clearly
// documented.
if (expression->arguments->size() != 2) {
::error(ErrorType::ERR_EXPECTED, "2 arguments", expression);
return expression;
}
auto vec = new IR::Vector<IR::Argument>();
// Digest has two arguments, we have to save the second one.
// It should be a list expression.
vec->push_back(expression->arguments->at(0));
auto arg1 = expression->arguments->at(1)->expression;
if (auto list = arg1->to<IR::ListExpression>()) {
auto simplified = simplifyExpressions(&list->components, true);
arg1 = new IR::ListExpression(arg1->srcInfo, *simplified);
vec->push_back(new IR::Argument(arg1));
} else if (auto si = arg1->to<IR::StructInitializerExpression>()) {
auto list = simplifyExpressions(&si->components);
arg1 = new IR::StructInitializerExpression(
si->srcInfo, si->typeName, si->typeName, *list);
vec->push_back(new IR::Argument(arg1));
} else {
auto tmp = new IR::Argument(
expression->arguments->at(1)->srcInfo,
createTemporary(expression->arguments->at(1)->expression));
vec->push_back(tmp);
}
expression->arguments = vec;
return expression;
}
}
auto vec = simplifyExpressions(expression->arguments);
if (vec != expression->arguments)
expression->arguments = vec;
return expression;
}
const IR::Node*
RemoveComplexExpressions::simpleStatement(IR::Statement* statement) {
if (assignments.empty())
return statement;
auto block = new IR::BlockStatement(assignments);
block->push_back(statement);
assignments.clear();
return block;
}
const IR::Node*
RemoveComplexExpressions::postorder(IR::Statement* statement) {
return simpleStatement(statement);
}
const IR::Node*
RemoveComplexExpressions::postorder(IR::MethodCallStatement* statement) {
auto mi = P4::MethodInstance::resolve(statement, refMap, typeMap);
if (auto em = mi->to<P4::ExternMethod>()) {
if (em->originalExternType->name != P4::P4CoreLibrary::instance.packetIn.name ||
em->method->name != P4::P4CoreLibrary::instance.packetIn.lookahead.name)
return simpleStatement(statement);
auto type = em->actualMethodType->returnType;
auto name = refMap->newName("tmp");
LOG3("Adding variable for lookahead " << name);
auto decl = new IR::Declaration_Variable(IR::ID(name), type->getP4Type());
newDecls.push_back(decl);
typeMap->setType(decl, typeMap->getTypeType(type, true));
auto assign = new IR::AssignmentStatement(
statement->srcInfo, new IR::PathExpression(name), statement->methodCall);
return simpleStatement(assign);
}
return simpleStatement(statement);
}
} // namespace PSDN