-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyadfa.h
More file actions
465 lines (411 loc) · 13.3 KB
/
Copy pathyadfa.h
File metadata and controls
465 lines (411 loc) · 13.3 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
#pragma once
#include <algorithm>
#include <cassert>
#include <fstream>
#include <iostream>
#include <iterator>
#include <map>
#include <memory>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include "tests.h"
#define ASMJIT_STATIC
#include <asmjit/asmjit.h>
enum builtin_type {
type_int8 = 0,
type_int16,
type_int32,
type_int64,
type_uint8,
type_uint16,
type_uint32,
type_uint64,
type_float
};
enum instruction_type {
op_var = 0,
op_mov,
op_push,
op_pop,
op_jmp,
op_if,
op_call,
op_add,
op_sub,
op_mul,
op_div,
op_ret,
op_new,
op_delete,
op_cmp_eq, // ==
op_cmp_neq, // !=
op_cmp_gt, // >
op_cmp_lt, // <
op_cmp_lte, // <=
op_cmp_gte, // >=
op_label,
op_function,
op_nop,
op_pop_args
};
class file_not_found_exception : public std::runtime_error {
public:
file_not_found_exception(const char* what) : std::runtime_error(what) {}
};
struct instruction {
instruction(instruction_type t) : type(t) {}
virtual std::ostream& dump(std::ostream& out) = 0;
virtual instruction* clone() = 0;
virtual bool is_arg_equal(const std::string& value) const = 0;
virtual ~instruction() = default;
instruction_type type;
protected:
std::ostream& dump_type(std::ostream& out) {
switch (type) {
case op_var:
out << std::string("var");
break;
case op_mov:
out << std::string("mov");
break;
case op_push:
out << std::string("push");
break;
case op_pop:
out << std::string("pop");
break;
case op_jmp:
out << std::string("jmp");
break;
case op_if:
out << std::string("if");
break;
case op_call:
out << std::string("call");
break;
case op_ret:
out << std::string("ret");
break;
case op_add:
out << std::string("add");
break;
case op_sub:
out << std::string("sub");
break;
case op_mul:
out << std::string("mul");
break;
case op_div:
out << std::string("div");
break;
case op_new:
out << std::string("new");
break;
case op_delete:
out << std::string("delete");
break;
case op_cmp_eq:
out << std::string("cmp_eq");
break;
case op_cmp_neq:
out << std::string("cmp_neq");
break;
case op_cmp_gt:
out << std::string("cmp_gt");
break;
case op_cmp_lt:
out << std::string("cmp_lt");
break;
case op_cmp_lte:
out << std::string("cmp_lte");
break;
case op_cmp_gte:
out << std::string("cmp_gte");
break;
case op_label:
out << std::string("label");
break;
case op_function:
out << std::string("function");
break;
case op_nop:
out << std::string("nop");
break;
case op_pop_args:
out << std::string("pop_args");
break;
default:
break;
}
return out;
}
};
using instruction_ptr = std::unique_ptr<instruction>;
using instruction_vec = std::vector<instruction_ptr>;
struct noarg_instruction : public instruction {
noarg_instruction(instruction_type t) : instruction(t) {}
std::ostream& dump(std::ostream& out) {
return dump_type(out) << '\n';
}
instruction* clone() {
return new noarg_instruction(*this);
}
bool is_arg_equal(const std::string& value) const {
return false;
}
};
struct unary_instruction : public instruction {
unary_instruction(instruction_type t, std::string arg) : instruction(t), arg_1(arg) {}
std::string arg_1;
std::ostream& dump(std::ostream& out) {
return dump_type(out) << ' ' << arg_1;
}
instruction* clone() {
return new unary_instruction(*this);
}
bool is_arg_equal(const std::string& value) const {
return arg_1 == value;
}
};
struct binary_instruction : public instruction {
binary_instruction(instruction_type t, std::string a_1, std::string a_2)
: instruction(t), arg_1(a_1), arg_2(a_2) {}
std::string arg_1;
std::string arg_2;
std::ostream& dump(std::ostream& out) {
return dump_type(out) << ' ' << arg_1 << ' ' << arg_2;
}
instruction* clone() {
return new binary_instruction(*this);
}
bool is_arg_equal(const std::string& value) const {
return arg_1 == value || arg_2 == value;
}
};
struct three_addr_instruction : public instruction {
three_addr_instruction(instruction_type t, std::string a_1, std::string a_2, std::string a_3)
: instruction(t), arg_1(a_1), arg_2(a_2), arg_3(a_3) {}
std::string arg_1;
std::string arg_2;
std::string arg_3;
std::ostream& dump(std::ostream& out) {
return dump_type(out) << ' ' << arg_1 << ' ' << arg_2 << ' ' << arg_3;
}
instruction* clone() {
return new three_addr_instruction(*this);
}
bool is_arg_equal(const std::string& value) const {
return arg_1 == value || arg_2 == value || arg_3 == value;
}
};
struct call_instruction : public instruction {
call_instruction(instruction_type t, const std::vector<std::string> &a)
: instruction(t), args(a) {}
std::vector<std::string> args;
std::ostream &dump(std::ostream &out) {
int arg_number = 0;
dump_type(out) << ' ';
for (const auto a : args) {
// arguments start from second
if (arg_number == 1) {
out << " (";
}
if (arg_number > 1) {
out << ' ';
}
out << a;
++arg_number;
}
// there args[0] is function name
if (args.size() == 1) {
out << '(';
}
out << ')';
return out;
}
instruction *clone() { return new call_instruction(*this); }
bool is_arg_equal(const std::string &value) const {
for (const auto &a : args) {
if (a == value)
return true;
}
return false;
}
};
struct pop_args_instruction : public instruction {
pop_args_instruction(
instruction_type t,
const std::vector<std::pair<std::string, std::string>> &a)
: instruction(t), args(a) {}
std::vector<std::pair<std::string, std::string>> args;
std::ostream &dump(std::ostream &out) {
int arg_number = 0;
dump_type(out) << ' ';
for (const auto a : args) {
// arguments start from second
if (arg_number == 0) {
out << " (";
}
if (arg_number > 0) {
out << ' , ';
}
out << a.first << " : " << a.second;
++arg_number;
}
// there args[0] is function name
if (args.size() == 0) {
out << '(';
}
out << ')';
return out;
}
instruction *clone() { return new pop_args_instruction(*this); }
bool is_arg_equal(const std::string &value) const {
for (const auto &a : args) {
if (a.first == value)
return true;
}
return false;
}
};
struct function_instruction : public instruction {
function_instruction(instruction_type t, std::vector<std::string> a,
instruction_vec i_vec)
: instruction(t), args(a), body(std::move(i_vec)) {}
function_instruction(const function_instruction &rhs)
: instruction(rhs.type) {
args = rhs.args;
for (const auto &i : rhs.body) {
std::unique_ptr<instruction> instr(i->clone());
body.push_back(std::move(instr));
}
}
std::vector<std::string> args;
instruction_vec body;
std::ostream& dump(std::ostream& out) {
int arg_number = 0;
dump_type(out) << ' ';
for (const auto a : args) {
// arguments start from second
if (arg_number == 1) {
out << " (";
}
if (arg_number > 1) {
out << ' ';
}
out << a;
++arg_number;
}
// there args[0] is function name
if (args.size() == 1) {
out << '(';
}
out << ')';
return out;
}
instruction *clone() { return new function_instruction(*this); }
bool is_arg_equal(const std::string& value) const {
for (const auto& a : args) {
if (a == value) return true;
}
return false;
}
};
struct label_table {
using internal_label_table = std::map<std::string, int>;
internal_label_table instance;
};
struct scanning_state {
scanning_state(const std::string& input) : current(input.begin()), end(input.end()) {}
std::string::const_iterator current;
std::string::const_iterator end;
bool eof() const {
return current == end;
}
size_t line_number = 1;
};
bool isbracket(char c);
bool isminus(char c);
bool is_identifier(char c);
bool iscolon(char c);
std::string getNextToken(scanning_state& state);
void parse_var(instruction_vec& i_vec, scanning_state& state);
void parse_mov(instruction_vec& i_vec, scanning_state& state);
void parse_push(instruction_vec& i_vec, scanning_state& state);
void parse_pop(instruction_vec& i_vec, scanning_state& state);
void parse_jmp(instruction_vec& i_vec, scanning_state& state);
void parse_if(instruction_vec& i_vec, scanning_state& state);
void parse_call(instruction_vec& i_vec, scanning_state& state);
void parse_ret(instruction_vec& i_vec, scanning_state& state);
void parse_add(instruction_vec& i_vec, scanning_state& state);
void parse_sub(instruction_vec& i_vec, scanning_state& state);
void parse_mul(instruction_vec &i_vec, scanning_state &state);
void parse_div(instruction_vec &i_vec, scanning_state &state);
void parse_new(instruction_vec& i_vec, scanning_state& state);
void parse_delete(instruction_vec& i_vec, scanning_state& state);
void parse_cmp_eq(instruction_vec& i_vec, scanning_state& state);
void parse_cmp_neq(instruction_vec& i_vec, scanning_state& state);
void parse_cmp_lt(instruction_vec& i_vec, scanning_state& state);
void parse_cmp_lte(instruction_vec& i_vec, scanning_state& state);
void parse_cmp_gt(instruction_vec& i_vec, scanning_state& state);
void parse_cmp_gte(instruction_vec& i_vec, scanning_state& state);
void parse_label(instruction_vec& i_vec, scanning_state& state, label_table& table);
void parse_function(instruction_vec& i_vec, scanning_state& state, label_table& table);
std::string read_file(const std::string file);
instruction_vec parse(const std::string& filename, label_table& table);
std::string parse_instruction(instruction_vec &program, scanning_state &state,
label_table &table);
struct in_out_sets {
std::vector<std::string> in_set;
std::vector<std::string> out_set;
};
using live_range = std::pair<size_t, size_t>;
using variable_interval_map = std::multimap<std::string, live_range>;
using control_flow_graph = std::multimap<int, int>;
using gen_set = std::map<int, std::vector<std::string>>; // aka use set
using kill_set = std::map<int, std::vector<std::string>>; // aka def set
using liveness_sets = std::map<int, in_out_sets>;
control_flow_graph build_cfg(const instruction_vec& i_vec, const label_table& table);
control_flow_graph build_backward_cfg(const control_flow_graph& cfg);
void build_use_def_sets(const instruction_vec& i_vec, gen_set& out_gen_set, kill_set& out_kill_set);
void dump_raw_use_def_set_impl(const std::map<int, std::vector<std::string>>& input_set,
std::ostream& out);
void dump_raw_gen_set(const gen_set& input_gen_set, std::ostream& out);
void dump_raw_kill_set(const kill_set& input_kill_set, std::ostream& out);
void dump_raw_cfg(const instruction_vec& i_vec, const control_flow_graph& cfg, std::ostream& out);
void dump_use_def_set_to_dot(const std::string& set_label,
const std::map<int, std::vector<std::string>>& input_set,
std::ostream& out);
void dump_kill_set_to_dot(const kill_set& input_kill_set, std::ostream& out);
void dump_gen_set_to_dot(const gen_set& input_gen_set, std::ostream& out);
void dump_liveness_sets_to_dot(const std::string& set_label,
const liveness_sets& liveness_input_set, std::ostream& out);
void dump_cfg_to_dot(const instruction_vec& i_vec, const control_flow_graph& cfg,
const gen_set& input_gen_set, const gen_set& input_kill_set,
const liveness_sets& liveness_sets_input, std::ostream& out);
void dump_raw_liveness(const liveness_sets& liveness_sets_input, std::ostream& out);
liveness_sets liveness_analysis(const instruction_vec& i_vec, const control_flow_graph& cfg);
variable_interval_map compute_variables_live_ranges(const liveness_sets& live_sets);
void dump_variable_intervals(const variable_interval_map& variables_intervals, std::ostream& out);
void generate_gnuplot_interval(const variable_interval_map& variables_intervals);
instruction_vec remove_dead_code(const instruction_vec& i_vec,
const variable_interval_map& variables_intervals);
instruction_vec optimize(const instruction_vec& i_vec,
const variable_interval_map& variables_intervals);
void dump_program(const instruction_vec& i_vec, std::ostream& out);
struct builtin_function {
void *function_pointer = nullptr;
std::vector<builtin_type> args;
};
using builtin_functions_map = std::map<std::string, builtin_function>;
// Code gen stuff
void gen_x64(const instruction_vec &i_vec, const asmjit::JitRuntime &rt,
asmjit::CodeHolder &code, const label_table <able,
const builtin_functions_map &builtin_functions);
int exec(const instruction_vec &i_vec, const label_table <able,
const builtin_functions_map &builtin_functions);
void dump_x86_64(const instruction_vec &i_vec, const label_table <able,
const builtin_functions_map &builtin_functions);