-
Notifications
You must be signed in to change notification settings - Fork 0
/
p4c-fpp.cpp
88 lines (72 loc) · 2.41 KB
/
p4c-fpp.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
/*
Modifications copyright 2017 CESNET
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 "lib/log.h"
#include "lib/crash.h"
#include "lib/exceptions.h"
#include "lib/gc.h"
#include "lib/nullstream.h"
#include "midend.h"
#include "fppOptions.h"
#include "fppBackend.h"
#include "frontends/common/parseInput.h"
#include "frontends/p4/frontend.h"
void compile(FPPOptions& options) {
auto hook = options.getDebugHook();
bool isv1 = options.langVersion == CompilerOptions::FrontendVersion::P4_14;
if (isv1) {
::error("This compiler only handles P4-16");
return;
}
auto program = P4::parseP4File(options);
if (::errorCount() > 0)
return;
P4::FrontEnd frontend;
frontend.addDebugHook(hook);
program = frontend.run(options, program);
if (::errorCount() > 0)
return;
FPP::MidEnd midend;
midend.addDebugHook(hook);
auto toplevel = midend.run(options, program);
if (options.dumpJsonFile)
JSONGenerator(*openFile(options.dumpJsonFile, true)) << program << std::endl;
if (::errorCount() > 0)
return;
FPP::run_fpp_backend(options, toplevel, &midend.refMap, &midend.typeMap);
}
using FPPContext = P4CContextWithOptions<FPPOptions>;
int main(int argc, char *const argv[]) {
setup_gc_logging();
setup_signals();
AutoCompileContext autoEbpfContext(new FPPContext);
auto& options = FPPContext::get().options();
options.compilerVersion = "0.0.1";
if (options.process(argc, argv) != nullptr)
options.setInputFile();
if (::errorCount() > 0)
exit(1);
try {
compile(options);
} catch (const Util::P4CExceptionBase &bug) {
std::cerr << bug.what() << std::endl;
return 1;
}
if (Log::verbose())
std::cerr << "Done." << std::endl;
return ::errorCount() > 0;
}