-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.cpp
77 lines (58 loc) · 1.96 KB
/
main.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
#include <fstream>
#include <iostream>
#include "ir/ir.h"
#include "frontends/common/options.h"
#include "frontends/common/parseInput.h"
#include "frontends/p4/frontend.h"
#include "lib/nullstream.h"
#include "ftt_midend/ftt_midend.h"
class FTTOptions : public CompilerOptions {
public:
cstring outputFile = nullptr;
FTTOptions() {
registerOption("-o", "outfile",
[this](const char* arg) { this->outputFile = arg; return true; },
"Write output to outfile");
}
};
using FTTContext = P4CContextWithOptions<FTTOptions>;
int main(int argc, char *const argv[]) {
AutoCompileContext autoFTTContext(new FTTContext);
auto& options = FTTContext::get().options();
options.langVersion = CompilerOptions::FrontendVersion::P4_16;
options.compilerVersion = "0.0.1";
if (options.process(argc, argv) != nullptr)
options.setInputFile();
if (::errorCount() > 0)
return 1;
auto program = P4::parseP4File(options);
if (program == nullptr || ::errorCount() > 0) {
std::cerr << "Can't parse P4 file " << options.file << std::endl;
return 1;
}
try {
P4::FrontEnd fe;
program = fe.run(options, program);
} catch (const Util::P4CExceptionBase &bug) {
std::cerr << bug.what() << std::endl;
return 1;
}
if (program == nullptr || ::errorCount() > 0) {
std::cerr << "Can't process P4 file " << options.file << " by frontend"<< std::endl;
return 1;
}
FttMidEnd midEnd(options);
const IR::ToplevelBlock *top = nullptr;
try {
top = midEnd.process(program);
} catch (const Util::P4CExceptionBase &bug) {
std::cerr << bug.what() << std::endl;
return 1;
}
std::cout << top << std::endl;
std::cout << std::endl << std::endl;
std::cout << program << std::endl;
if (options.dumpJsonFile) {
JSONGenerator(*openFile(options.dumpJsonFile, true), true) << top << std::endl;
}
}