-
Notifications
You must be signed in to change notification settings - Fork 0
/
bmv2.cpp
111 lines (96 loc) · 3.4 KB
/
bmv2.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
/*
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 <stdio.h>
#include <string>
#include <iostream>
#include "ir/ir.h"
#include "control-plane/p4RuntimeSerializer.h"
#include "frontends/common/parseInput.h"
#include "frontends/p4/frontend.h"
#include "lib/error.h"
#include "lib/exceptions.h"
#include "lib/gc.h"
#include "lib/log.h"
#include "lib/nullstream.h"
#include "backend.h"
#include "midend.h"
#include "options.h"
#include "JsonObjects.h"
int main(int argc, char *const argv[]) {
setup_gc_logging();
BMV2::BMV2Options options;
options.langVersion = BMV2::BMV2Options::FrontendVersion::P4_16;
options.compilerVersion = "0.0.5";
if (options.process(argc, argv) != nullptr)
options.setInputFile();
if (::errorCount() > 0)
return 1;
auto hook = options.getDebugHook();
// BMV2 is required for compatibility with the previous compiler.
options.preprocessor_options += " -D__TARGET_BMV2__";
auto program = P4::parseP4File(options);
if (program == nullptr || ::errorCount() > 0)
return 1;
try {
P4::FrontEnd frontend;
frontend.addDebugHook(hook);
program = frontend.run(options, program);
} catch (const Util::P4CExceptionBase &bug) {
std::cerr << bug.what() << std::endl;
return 1;
}
if (program == nullptr || ::errorCount() > 0)
return 1;
const IR::ToplevelBlock* toplevel = nullptr;
BMV2::MidEnd midEnd(options);
midEnd.addDebugHook(hook);
try {
toplevel = midEnd.process(program);
if (options.dumpJsonFile)
JSONGenerator(*openFile(options.dumpJsonFile, true)) << program << std::endl;
} catch (const Util::P4CExceptionBase &bug) {
std::cerr << bug.what() << std::endl;
return 1;
}
if (::errorCount() > 0)
return 1;
// backend depends on the modified refMap and typeMap from midEnd.
BMV2::Backend backend(options.isv1(), &midEnd.refMap,
&midEnd.typeMap, &midEnd.enumMap);
try {
backend.addDebugHook(hook);
backend.process(toplevel, options);
backend.convert(options);
} catch (const Util::P4CExceptionBase &bug) {
std::cerr << bug.what() << std::endl;
return 1;
}
if (::errorCount() > 0)
return 1;
if (!options.outputFile.isNullOrEmpty()) {
std::ostream* out = openFile(options.outputFile, false);
if (out != nullptr) {
backend.serialize(*out);
out->flush();
}
}
// Generate a PI control plane API for this program if requested.
if (!options.p4RuntimeFile.isNullOrEmpty()) {
std::ostream* out = openFile(options.p4RuntimeFile, false);
if (out != nullptr) {
serializeP4Runtime(out, program, toplevel, &midEnd.refMap,
&midEnd.typeMap, options.p4RuntimeFormat);
}
}
return ::errorCount() > 0;
}