-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcodegen_tool.cc
193 lines (154 loc) · 5.26 KB
/
codegen_tool.cc
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include "BE/Base/ir.h"
#include "BE/Base/serialize.h"
#include "BE/CodeGenX64/codegen.h"
#include "BE/CodeGenX64/legalize.h"
#include "BE/CodeGenX64/regs.h"
#include "BE/CpuA64/assembler.h"
#include "Util/breakpoint.h"
#include "Util/parse.h"
#include "Util/switch.h"
#include "Util/webserver.h"
#include <fstream>
#include <iomanip>
#include <iostream>
#include <thread>
namespace {
using namespace cwerg;
using namespace cwerg::base;
using namespace cwerg::code_gen_x64;
WebResponse DefaultHandler(const WebRequest& request) {
WebResponse out;
out.body << WebServer::kHtmlProlog;
out.body << "<h1>Debug Console</h1>\n";
RenderBreakPointHTML(&out.body);
out.body << "<h2>Code</h2>\n";
out.body << "<a href='/code'>Code</a>\n";
out.body << "<h2>BST Stats</h2>\n";
out.body << "<pre>\n";
DumpBstStates(out.body);
out.body << "</pre>\n";
out.body << "<h2>Stripes</h2>\n";
out.body << "<pre>\n";
cwerg::StripeGroup::DumpAllGroups(out.body);
out.body << "</pre>\n";
out.body << WebServer::kHtmlEpilog;
return out;
}
WebResponse CodeHandler(base::Unit unit, const WebRequest& request) {
WebResponse out;
out.body << WebServer::kHtmlProlog;
for (Mem mem : UnitMemIter(unit)) {
out.body << "<hr>\n";
out.body << "<pre>";
MemRenderToAsm(mem, &out.body);
out.body << "</pre>";
}
for (Fun fun : UnitFunIter(unit)) {
out.body << "<hr>\n";
out.body << "<pre>";
FunRenderToAsm(fun, &out.body, true);
out.body << "</pre>";
}
out.body << WebServer::kHtmlEpilog;
return out;
}
SwitchInt32 sw_multiplier("multiplier",
"adjust multiplies for item pool sizes",
4);
SwitchString sw_mode("mode", "mode indicating what to do", "optimize");
SwitchBool sw_show_stats("show_stats", "emit stats to cout");
SwitchBool sw_break_after_load("break_after_load", "break after load IR");
SwitchInt32 sw_webserver_port("webserver_port",
"launch webserver at given port",
-1);
void SleepForever() {
std::cerr << "execution asserted webserver still active\n";
std::this_thread::sleep_for(std::chrono::hours(1000));
}
BreakPoint bp_after_load("after_load");
BreakPoint bp_before_exit("before_exit");
} // namespace
int main(int argc, const char* argv[]) {
if (argc - 2 != cwerg::SwitchBase::ParseArgv(argc, argv, &std::cerr)) {
std::cerr << "need exactly two positional arguments\n";
return 1;
}
const bool DEBUG = false;
std::ostream* log = DEBUG ? &std::cout : nullptr;
// If the synchronization is turned off, the C++ standard streams are allowed
// to buffer their I/O independently from their stdio counterparts, which may
// be considerably faster in some cases.
std::ios_base::sync_with_stdio(DEBUG);
InitStripes(sw_multiplier.Value());
InitCodeGenX64();
std::ifstream finFile;
std::istream* fin = &std::cin;
if (argv[argc - 2] != std::string_view("-")) {
finFile.open(argv[argc - 2]);
fin = &finFile;
}
std::ofstream foutFile;
std::ostream* fout = &std::cout;
if (argv[argc - 1] != std::string_view("-")) {
foutFile.open(argv[argc - 1]);
fout = &foutFile;
}
std::vector<char> data = SlurpDataFromStream(fin);
Unit unit = UnitParseFromAsm("unit", {data.data(), data.size()}, {});
std::unique_ptr<cwerg::WebServer> webserver;
std::unique_ptr<std::thread> webserver_thread;
const bool launch_webserver = sw_webserver_port.Value() >= 0;
if (launch_webserver) {
using namespace std::placeholders;
std::cerr << "Launching webserver on port " << sw_webserver_port.Value()
<< "\n";
webserver = std::make_unique<cwerg::WebServer>();
webserver->handler.push_back(
WebHandler{"/code", "GET", std::bind(CodeHandler, unit, _1)});
webserver->handler.push_back(
WebHandler{"/resume/", "GET", ResumeBreakpointHandler});
webserver->handler.push_back(WebHandler{"/", "GET", DefaultHandler});
webserver_thread =
std::make_unique<std::thread>(&cwerg::WebServer::Start, webserver.get(),
sw_webserver_port.Value(), "");
SetAbortHandler(&SleepForever);
}
if (launch_webserver && sw_break_after_load.Value()) {
bp_after_load.Break();
}
if (sw_mode.Value() == "binary") {
LegalizeAll(unit, false, nullptr);
RegAllocGlobal(unit, false, nullptr);
RegAllocLocal(unit, false, nullptr);
x64::X64Unit cpuunit = EmitUnitAsBinary(unit);
auto exe = x64::MakeExe(&cpuunit, true);
std::vector<std::string_view> chunks = exe.Save();
for (const auto& c : chunks) {
fout->write((const char*)c.data(), c.size());
}
return 0;
}
LegalizeAll(unit, false, log);
if (sw_mode.Value() == "legalize") {
UnitRenderToAsm(unit, fout);
return 0;
}
RegAllocGlobal(unit, false, log);
if (sw_mode.Value() == "reg_alloc_global") {
UnitRenderToAsm(unit, fout);
return 0;
}
RegAllocLocal(unit, false, log);
if (sw_mode.Value() == "reg_alloc_local") {
UnitRenderToAsm(unit, fout);
return 0;
}
// normal is handled by this as well
EmitUnitAsText(unit, fout);
// If we spawned a webserver break before exiting so we can expect the code.
if (sw_webserver_port.Value() >= 0) {
bp_before_exit.Break();
webserver_thread->join();
}
return 0;
}