-
Notifications
You must be signed in to change notification settings - Fork 0
/
Driver.cpp
128 lines (110 loc) · 3.97 KB
/
Driver.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
#include "CodeGen/AsmPrinter.hpp"
#include "CodeGen/FrameLowering.hpp"
#include "CodeGen/MEISel.hpp"
#include "CodeGen/SimpleRA.hpp"
#include "Parser/Parser.hpp"
#include "Pass/ConstantFolding.hpp"
#include "Pass/DeadCodeElimination.hpp"
#include "Pass/Dump.hpp"
#include "Pass/IRLegalize.hpp"
#include "Pass/InstCombine.hpp"
#include "Pass/MachineDCE.hpp"
#include "Pass/PassCollection.hpp"
#include "Pass/PeepHole.hpp"
#include "Pass/SimplifyCFG.hpp"
#include "Transformer/Transformer.hpp"
#include "Tree/Tree.hpp"
#include "Util/OptionParser.hpp"
#include "Util/RuntimeStackUtil.hpp"
#include "Util/TrivialValueVector.hpp"
#include <fmt/core.h>
#include <fstream>
#include <string_view>
using namespace std;
int main(int argc, char *argv[]) {
RuntimeStack::set_max_size(RuntimeStack::hard_max_size());
OptionParser optParser;
optParser.add(Option<bool>("--help", "-h").setDefault("false"),
Option<bool>("--version", "-v").setDefault("false"),
Option<bool>("--debug-opt-parser").setDefault("false"),
Option<bool>("--implicit-runtime").setDefault("true"),
Option<bool>("-S").setDefault("true"),
Option<bool>("-O2").setDefault("true"),
Option<std::string_view>("--output", "-o"),
Option<std::string_view>("filename"));
optParser.parse(argc, argv);
if (optParser["--debug-opt-parser"].as<bool>()) {
if (optParser.has("-h"))
fmt::print("-h: {}\n", optParser["-h"].as<bool>());
if (optParser.has("-v"))
fmt::print("-v: {}\n", optParser["-v"].as<bool>());
if (optParser.has("filename"))
fmt::print("p[0]: {}\n", optParser["filename"].as<std::string_view>());
if (optParser.has("-o"))
fmt::print("-o: {}\n", optParser["-o"].as<std::string_view>());
return 0;
}
if (optParser["-h"].as<bool>()) {
fmt::print("usage: syoc [filename] [(-o | --output) filename] [-h | "
"--help] [-v | --version]\n");
return 0;
}
if (optParser["-v"].as<bool>()) {
fmt::print("syoc version 0.1.0\n");
return 0;
}
string fileName;
string fileContent;
fileName = optParser["filename"].as<std::string_view>();
if (fileName.empty())
getline(ifstream("current.txt"), fileName, '\0');
ifstream inputStream(fileName);
assert(inputStream.good());
getline(inputStream, fileContent, '\0');
if (optParser["--implicit-runtime"].as<bool>()) {
fileContent = R"(
int getint(),getch(),getarray(int a[]);
float getfloat();
int getfarray(float a[]);
void putint(int a),putch(int a),putarray(int n,int a[]);
void putfloat(float a);
void putfarray(int n, float a[]);
void starttime();
void stoptime();
int __aeabi_idivmod(int a, int b);
int __aeabi_idiv(int a, int b);
void _sysy_starttime(int lineno);
void _sysy_stoptime(int lineno);
)" + fileContent;
}
SyOC::Parser parser(fileContent);
parser.tokenize();
auto *tree = parser.parse();
SyOC::Transformer transformer(tree);
transformer
.doTreeTransformation<SyOC::ConstantInitializerFold, SyOC::TypeCheck>();
// from tree gen ssa ir
transformer.doTree2SSATransformation<SyOC::Tree2SSA>();
// opt passes
transformer.doSSATransformation<
SyOC::SimplifyCFG, SyOC::SimpleAllocationElimination, SyOC::IRDump,
// SyOC::PromoteMem2Reg,
SyOC::InstCombine, SyOC::ConstantFolding, SyOC::SimplifyCFG,
SyOC::SimpleAllocationElimination, SyOC::DeadCodeElimination,
SyOC::IRLegalize, SyOC::FixTimeMeasurement, SyOC::IRDump, SyOC::CFGDump>();
// instruction selection
SyOC::ARMv7a::AsmPrinter out;
transformer.doSSA2MInstTransformation<SyOC::MEISel>();
out.print("mir.s", transformer.getMIR());
transformer.doMInstTransformation<
SyOC::ARMv7a::SimpleRA, SyOC::ARMv7a::PeepHole, SyOC::ARMv7a::MachineDCE,
SyOC::ARMv7a::FrameLowering>();
std::string asmFileName;
if (optParser.has("-o")) {
asmFileName = optParser["-o"].as<std::string_view>();
} else {
asmFileName = fileName + ".s";
}
out.print(asmFileName, transformer.getMIR());
return 0;
}