Repository navigation
Expand file tree
/
Copy pathbackend.cpp
More file actions
136 lines (108 loc) · 4.14 KB
/
Copy pathbackend.cpp
File metadata and controls
136 lines (108 loc) · 4.14 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
/*
* Copyright 2019 Seungbin Song
*/
#include "backend.h"
#include "programStructure.h"
#include "lower.h"
#include "annotationParser.h"
#include "controlPolicy.h"
#include "expressionConverter.h"
#include "headerConverter.h"
#include "parserConverter.h"
#include "controlConverter.h"
#include "lib/null.h"
#include "lib/cstring.h"
#include "frontends/p4/simplify.h"
#include "frontends/p4/unusedDeclarations.h"
#include "frontends/p4/parseAnnotations.h"
namespace PSDN {
void Backend::convert(const IR::ToplevelBlock* _toplevel) {
CHECK_NULL(_toplevel);
// Get main & build program structure
auto main = _toplevel->getMain();
if (!main) { ::error("Cannot get main"); return; }
auto structure = new ProgramStructure();
auto structureBuilder = new ProgramStructureBuilder(structure);
main->apply(*structureBuilder);
if(::errorCount() > 0) { ::error("Cannot generate program structure"); return; }
// Apply Backend optimizations
auto evaluator = new P4::EvaluatorPass(refMap, typeMap);
auto program = _toplevel->getProgram();
PassManager simplify = {
new P4::SynthesizeActions(refMap, typeMap,
new PSDN::SkipControls(&structure->nonPipelineControls)),
new P4::MoveActionsToTables(refMap, typeMap),
new P4::TypeChecking(refMap, typeMap),
new P4::SimplifyControlFlow(refMap, typeMap),
new PSDN::LowerExpressions(typeMap),
new P4::ConstantFolding(refMap, typeMap, false),
new P4::TypeChecking(refMap, typeMap),
new PSDN::RemoveComplexExpressions(refMap, typeMap,
new PSDN::ProcessControls(&structure->pipelineControls)),
new P4::SimplifyControlFlow(refMap, typeMap),
new P4::RemoveAllUnusedDeclarations(refMap),
new P4::ParseAnnotationBodies(new AnnotationParser(), typeMap),
evaluator,
new VisitFunctor([this, evaluator, structure]() {
toplevel = evaluator->getToplevelBlock(); }),
};
auto hook = options.getDebugHook();
simplify.addDebugHook(hook);
program->apply(simplify);
if(::errorCount() > 0) { ::error("Cannot apply backend optimizations"); return; }
// Map IR node to resource blocks
toplevel->apply(*new PSDN::ResourceMapBuilder(&structure->resourceMap));
if(::errorCount() > 0) { ::error("Cannot generate resource map"); return; }
// Get main & build program structure
main = toplevel->getMain();
if (!main) { ::error("Cannot get main"); return; }
main->apply(*structureBuilder);
program = toplevel->getProgram();
program->apply(*structureBuilder);
if(::errorCount() > 0) { ::error("Cannot generate program structure"); return; }
// Generate conversion context
ctxt = new ConversionContext(refMap, typeMap, toplevel, structure);
// Open filestream
output.open(options.outputFile);
// Convert enums into constant
//
// enum X { a, b };
//
// const ENUM_X_a = 0;
// const ENUM_X_b = 1;
for (const auto &enums : *enumMap) {
std::map<cstring, cstring>* enumString = new std::map<cstring, cstring>;
auto enumName = enums.first->getName();
for (const auto &enumEntry : *enums.second) {
cstring str = "ENUM_" + enumName + "_" + enumEntry.first;
output << "const " << str << " = " << enumEntry.second << ";" << std::endl;
enumString->emplace(enumEntry.first, str.c_str());
}
ctxt->enumStringMap.emplace(enums.first, enumString);
}
output << std::endl;
// Convert error codes into constant
//
// error { a, b };
//
// const ERROR_a = 1;
// const ERROR_b = 2;
for (const auto &errorCode : structure->errorCodeMap) {
auto name = errorCode.first->toString();
cstring str = "ERROR_" + name;
output << "const " << str << " = " << errorCode.second << ";" << std::endl;
ctxt->errorStringMap.emplace(errorCode.first, str);
}
output << std::endl;
auto econv = new ExpressionConverter(refMap, typeMap, structure, ctxt);
auto hconv = new HeaderConverter(ctxt);
program->apply(*hconv);
output << hconv->emitTypeDef() << std::endl;
auto pconv = new ParserConverter(ctxt, hconv, econv);
program->apply(*pconv);
output << pconv->emitParser() << std::endl;
auto cconv = new ControlConverter(ctxt, hconv, econv);
program->apply(*cconv);
output.close();
}
}; //namespace PSDN