-
Notifications
You must be signed in to change notification settings - Fork 445
/
ir.cpp
285 lines (248 loc) · 10.1 KB
/
ir.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
/*
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 "ir/ir.h"
#include <strings.h>
#include <functional>
#include <list>
#include <map>
#include <unordered_map>
#include <utility>
#include <vector>
#include "ir/declaration.h"
#include "ir/id.h"
#include "ir/indexed_vector.h"
#include "ir/node.h"
#include "ir/vector.h"
#include "lib/cstring.h"
#include "lib/enumerator.h"
#include "lib/error.h"
#include "lib/error_catalog.h"
#include "lib/exceptions.h"
#include "lib/log.h"
#include "lib/null.h"
#include "lib/ordered_map.h"
namespace IR {
const cstring ParserState::accept = "accept";
const cstring ParserState::reject = "reject";
const cstring ParserState::start = "start";
const cstring ParserState::verify = "verify";
const cstring TableProperties::actionsPropertyName = "actions";
const cstring TableProperties::keyPropertyName = "key";
const cstring TableProperties::defaultActionPropertyName = "default_action";
const cstring TableProperties::entriesPropertyName = "entries";
const cstring TableProperties::sizePropertyName = "size";
const cstring IApply::applyMethodName = "apply";
const cstring P4Program::main = "main";
const cstring Type_Error::error = "error";
long IR::Declaration::nextId = 0;
long IR::This::nextId = 0;
const Type_Method *P4Control::getConstructorMethodType() const {
return new Type_Method(getTypeParameters(), type, constructorParams, getName());
}
const Type_Method *P4Parser::getConstructorMethodType() const {
return new Type_Method(getTypeParameters(), type, constructorParams, getName());
}
const Type_Method *Type_Package::getConstructorMethodType() const {
return new Type_Method(getTypeParameters(), this, constructorParams, getName());
}
Util::Enumerator<const IR::IDeclaration *> *IGeneralNamespace::getDeclsByName(cstring name) const {
return getDeclarations()->where([name](const IDeclaration *d) {
CHECK_NULL(d);
return name == d->getName().name;
});
}
Util::Enumerator<const IDeclaration *> *INestedNamespace::getDeclarations() const {
Util::Enumerator<const IDeclaration *> *rv = nullptr;
for (const auto *nested : getNestedNamespaces()) {
if (nested == nullptr) continue;
rv = rv ? rv->concat(nested->getDeclarations()) : nested->getDeclarations();
}
return rv ? rv : new Util::EmptyEnumerator<const IDeclaration *>;
}
bool IFunctional::callMatches(const Vector<Argument> *arguments) const {
auto paramList = getParameters()->parameters;
std::map<cstring, const IR::Parameter *> paramNames;
for (auto param : paramList) paramNames.emplace(param->name.name, param);
size_t index = 0;
for (auto arg : *arguments) {
if (paramNames.size() == 0)
// Too many arguments
return false;
cstring argName = arg->name;
if (argName.isNullOrEmpty()) argName = paramList.at(index)->name.name;
auto it = paramNames.find(argName);
if (it == paramNames.end())
// Argument name does not match a parameter
return false;
else
paramNames.erase(it);
index++;
}
// Check if all remaining parameters have default values
// or are optional.
for (const auto &[_, param] : paramNames) {
if (!param->isOptional() && !param->defaultValue) return false;
}
return true;
}
void IGeneralNamespace::checkDuplicateDeclarations() const {
std::unordered_map<cstring, ID> seen;
for (auto decl : *getDeclarations()) {
IR::ID name = decl->getName();
auto f = seen.find(name.name);
if (f != seen.end()) {
::error(ErrorType::ERR_DUPLICATE,
"Duplicate declaration of %1%: previous declaration at %2%", name,
f->second.srcInfo);
}
seen.emplace(name.name, name);
}
}
void P4Parser::checkDuplicates() const {
for (auto decl : states) {
auto prev = parserLocals.getDeclaration(decl->getName().name);
if (prev != nullptr)
::error(ErrorType::ERR_DUPLICATE, "State %1% has same name as %2%", decl, prev);
}
}
bool Type_Stack::sizeKnown() const { return size->is<Constant>(); }
size_t Type_Stack::getSize() const {
if (!sizeKnown()) BUG("%1%: Size not yet known", size);
auto cst = size->to<IR::Constant>();
if (!cst->fitsInt()) {
::error(ErrorType::ERR_OVERLIMIT, "Index too large: %1%", cst);
return 0;
}
auto size = cst->asInt();
if (size < 0) {
::error(ErrorType::ERR_OVERLIMIT, "Illegal array size: %1%", cst);
return 0;
}
return static_cast<size_t>(size);
}
const Method *Type_Extern::lookupMethod(IR::ID name, const Vector<Argument> *arguments) const {
const Method *result = nullptr;
bool reported = false;
for (auto m : methods) {
if (m->name != name) continue;
if (m->callMatches(arguments)) {
if (result == nullptr) {
result = m;
} else {
::error(ErrorType::ERR_DUPLICATE, "Ambiguous method %1%", name);
if (!reported) {
::error(ErrorType::ERR_DUPLICATE, "Candidate is %1%", result);
reported = true;
}
::error(ErrorType::ERR_DUPLICATE, "Candidate is %1%", m);
return nullptr;
}
}
}
return result;
}
const Type_Method *Type_Parser::getApplyMethodType() const {
return new Type_Method(applyParams, getName());
}
const Type_Method *Type_Control::getApplyMethodType() const {
return new Type_Method(applyParams, getName());
}
const IR::Path *ActionListElement::getPath() const {
auto expr = expression;
if (expr->is<IR::MethodCallExpression>()) expr = expr->to<IR::MethodCallExpression>()->method;
if (expr->is<IR::PathExpression>()) return expr->to<IR::PathExpression>()->path;
BUG("%1%: unexpected expression", expression);
}
const Type_Method *P4Table::getApplyMethodType() const {
// Synthesize a new type for the return
auto actions = properties->getProperty(IR::TableProperties::actionsPropertyName);
if (actions == nullptr) {
::error(ErrorType::ERR_INVALID, "%1%: table does not contain a list of actions", this);
return nullptr;
}
if (!actions->value->is<IR::ActionList>())
BUG("Action property is not an IR::ActionList, but %1%", actions);
auto alv = actions->value->to<IR::ActionList>();
auto hit = new IR::StructField(IR::Type_Table::hit, IR::Type_Boolean::get());
auto miss = new IR::StructField(IR::Type_Table::miss, IR::Type_Boolean::get());
auto label = new IR::StructField(IR::Type_Table::action_run, new IR::Type_ActionEnum(alv));
auto rettype = new IR::Type_Struct(ID(name), {hit, miss, label});
auto applyMethod = new IR::Type_Method(rettype, new IR::ParameterList(), getName());
return applyMethod;
}
const Type_Method *Type_Table::getApplyMethodType() const { return table->getApplyMethodType(); }
void Block::setValue(const Node *node, const CompileTimeValue *value) {
CHECK_NULL(node);
auto it = constantValue.find(node);
if (it != constantValue.end())
BUG_CHECK(value->equiv(*constantValue[node]), "%1% already set in %2% to %3%, not %4%",
node, this, value, constantValue[node]);
else
constantValue[node] = value;
}
void InstantiatedBlock::instantiate(std::vector<const CompileTimeValue *> *args) {
CHECK_NULL(args);
auto it = args->begin();
for (auto p : *getConstructorParameters()->getEnumerator()) {
if (it == args->end()) {
BUG_CHECK(p->isOptional(), "Missing nonoptional arg %s", p);
continue;
}
LOG1("Set " << p << " to " << *it << " in " << id);
setValue(p, *it);
++it;
}
}
const IR::CompileTimeValue *InstantiatedBlock::getParameterValue(cstring paramName) const {
auto param = getConstructorParameters()->getDeclByName(paramName);
BUG_CHECK(param != nullptr, "No parameter named %1%", paramName);
BUG_CHECK(param->is<IR::Parameter>(), "No parameter named %1%", paramName);
return getValue(param->getNode());
}
const IR::CompileTimeValue *InstantiatedBlock::findParameterValue(cstring paramName) const {
auto *param = getConstructorParameters()->getDeclByName(paramName);
if (!param) return nullptr;
if (!param->is<IR::Parameter>()) return nullptr;
return getValue(param->getNode());
}
Util::Enumerator<const IDeclaration *> *P4Program::getDeclarations() const {
return objects.getEnumerator()->as<const IDeclaration *>()->where(
[](const IDeclaration *d) { return d != nullptr; });
}
const IR::PackageBlock *ToplevelBlock::getMain() const {
auto program = getProgram();
auto mainDecls = program->getDeclsByName(IR::P4Program::main)->toVector();
if (mainDecls.empty()) {
::warning(ErrorType::WARN_MISSING, "Program does not contain a `%s' module",
IR::P4Program::main);
return nullptr;
}
auto main = mainDecls[0];
if (mainDecls.size() > 1) {
::error(ErrorType::ERR_DUPLICATE, "Program has multiple `%s' instances: %1%, %2%",
IR::P4Program::main, main->getNode(), mainDecls[1]->getNode());
return nullptr;
}
if (!main->is<IR::Declaration_Instance>()) {
::error(ErrorType::ERR_INVALID, "%1%: must be a package declaration", main->getNode());
return nullptr;
}
auto block = getValue(main->getNode());
if (block == nullptr) return nullptr;
if (!block->is<IR::PackageBlock>()) {
::error(ErrorType::ERR_EXPECTED, "%1%: expected package declaration", block);
return nullptr;
}
return block->to<IR::PackageBlock>();
}
} // namespace IR