-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend.cpp
194 lines (173 loc) · 6.99 KB
/
backend.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
/*
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 "action.h"
#include "backend.h"
#include "control.h"
#include "deparser.h"
#include "errorcode.h"
#include "expression.h"
#include "extern.h"
#include "frontends/common/constantFolding.h"
#include "frontends/p4/evaluator/evaluator.h"
#include "frontends/p4/methodInstance.h"
#include "frontends/p4/simplify.h"
#include "frontends/p4/typeChecking/typeChecker.h"
#include "frontends/p4/unusedDeclarations.h"
#include "midend/actionSynthesis.h"
#include "midend/removeLeftSlices.h"
#include "lower.h"
#include "header.h"
#include "parser.h"
#include "JsonObjects.h"
#include "extractArchInfo.h"
using namespace std;
namespace BMV2 {
/**
This class implements a policy suitable for the SynthesizeActions pass.
The policy is: do not synthesize actions for the controls whose names
are in the specified set.
For example, we expect that the code in the deparser will not use any
tables or actions.
*/
class SkipControls : public P4::ActionSynthesisPolicy {
// set of controls where actions are not synthesized
const std::set<cstring> *skip;
public:
explicit SkipControls(const std::set<cstring> *skip) : skip(skip) { CHECK_NULL(skip); }
bool convert(const IR::P4Control* control) const {
if (skip->find(control->name) != skip->end())
return false;
return true;
}
};
/**
This class implements a policy suitable for the RemoveComplexExpression pass.
The policy is: only remove complex expression for the controls whose names
are in the specified set.
For example, we expect that the code in ingress and egress will have complex
expression removed.
*/
class ProcessControls : public BMV2::RemoveComplexExpressionsPolicy {
const std::set<cstring> *process;
public:
explicit ProcessControls(const std::set<cstring> *process) : process(process) {
CHECK_NULL(process);
}
bool convert(const IR::P4Control* control) const {
if (process->find(control->name) != process->end())
return true;
return false;
}
};
void
Backend::process(const IR::ToplevelBlock* tlb, BMV2Options& options) {
// debug from here
CHECK_NULL(tlb);
setName("BackEnd");
auto evaluator = new P4::EvaluatorPass(refMap, typeMap);
if (options.arch != Target::UNKNOWN){
target = options.arch;
}
if (target == Target::SIMPLE) {
// omer
if (tlb == nullptr) {
::error("flextrum debug- tlb is null ptr");
}
simpleSwitch->setPipelineControls(tlb, &pipeline_controls, &pipeline_namemap);
simpleSwitch->setNonPipelineControls(tlb, &non_pipeline_controls);
simpleSwitch->setUpdateChecksumControls(tlb, &update_checksum_controls);
simpleSwitch->setDeparserControls(tlb, &deparser_controls);
} else if (target == Target::PORTABLE) {
P4C_UNIMPLEMENTED("PSA architecture is not yet implemented");
}
// These passes are logically bmv2-specific
addPasses({
new P4::SynthesizeActions(refMap, typeMap, new SkipControls(&non_pipeline_controls)),
new P4::MoveActionsToTables(refMap, typeMap),
new P4::TypeChecking(refMap, typeMap),
new P4::SimplifyControlFlow(refMap, typeMap),
new LowerExpressions(typeMap),
new P4::ConstantFolding(refMap, typeMap, false),
new P4::TypeChecking(refMap, typeMap),
new RemoveComplexExpressions(refMap, typeMap, new ProcessControls(&pipeline_controls)),
new FixupChecksum(&update_checksum_controls),
new P4::SimplifyControlFlow(refMap, typeMap),
new P4::RemoveAllUnusedDeclarations(refMap),
new DiscoverStructure(&structure),
new ErrorCodesVisitor(&errorCodesMap),
new ExtractArchInfo(typeMap),
evaluator,
new VisitFunctor([this, evaluator]() { toplevel = evaluator->getToplevelBlock(); }),
});
tlb->getProgram()->apply(*this);
}
/// BMV2 Backend that takes the top level block and converts it to a JsonObject
/// that can be interpreted by the BMv2 simulator.
void Backend::convert(BMV2Options& options) {
jsonTop.emplace("program", options.file);
jsonTop.emplace("__meta__", json->meta);
jsonTop.emplace("header_types", json->header_types);
jsonTop.emplace("headers", json->headers);
jsonTop.emplace("header_stacks", json->header_stacks);
jsonTop.emplace("header_union_types", json->header_union_types);
jsonTop.emplace("header_unions", json->header_unions);
jsonTop.emplace("header_union_stacks", json->header_union_stacks);
field_lists = mkArrayField(&jsonTop, "field_lists");
jsonTop.emplace("errors", json->errors);
jsonTop.emplace("enums", json->enums);
jsonTop.emplace("parsers", json->parsers);
jsonTop.emplace("deparsers", json->deparsers);
meter_arrays = mkArrayField(&jsonTop, "meter_arrays");
counters = mkArrayField(&jsonTop, "counter_arrays");
register_arrays = mkArrayField(&jsonTop, "register_arrays");
jsonTop.emplace("calculations", json->calculations);
learn_lists = mkArrayField(&jsonTop, "learn_lists");
BMV2::nextId("learn_lists");
jsonTop.emplace("actions", json->actions);
jsonTop.emplace("pipelines", json->pipelines);
jsonTop.emplace("checksums", json->checksums);
force_arith = mkArrayField(&jsonTop, "force_arith");
jsonTop.emplace("extern_instances", json->externs);
jsonTop.emplace("field_aliases", json->field_aliases);
json->add_program_info(options.file);
json->add_meta_info();
// convert all enums to json
for (const auto &pEnum : *enumMap) {
auto name = pEnum.first->getName();
for (const auto &pEntry : *pEnum.second) {
json->add_enum(name, pEntry.first, pEntry.second);
}
}
/// generate error types
for (const auto &p : errorCodesMap) {
auto name = p.first->toString();
auto type = p.second;
json->add_error(name, type);
}
cstring scalarsName = refMap->newName("scalars");
// This visitor is used in multiple passes to convert expression to json
conv = new ExpressionConverter(this, scalarsName);
// if (psa) tlb->apply(new ConvertExterns());
PassManager codegen_passes = {
new ConvertHeaders(this, scalarsName),
new ConvertExterns(this), // only run when target == PSA
new ConvertParser(this),
new ConvertActions(this),
new ConvertControl(this),
new ConvertDeparser(this),
};
codegen_passes.setName("CodeGen");
CHECK_NULL(toplevel);
toplevel->getMain()->apply(codegen_passes);
}
} // namespace BMV2