/
cfg.hpp
555 lines (454 loc) · 13.5 KB
/
cfg.hpp
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
#ifndef CPPPARSER_CFG_HPP
#define CPPPARSER_CFG_HPP
#include <boost/assert.hpp>
#include <boost/variant.hpp>
#include <boost/variant/recursive_variant.hpp>
#include <boost/none.hpp>
#include <boost/iterator/counting_iterator.hpp>
#include <boost/iterator/iterator_facade.hpp>
#include <boost/utility.hpp>
#include <boost/lexical_cast.hpp>
#include <algorithm>
#include <memory>
#include <vector>
#include <set>
#include <list>
#include <map>
#include <string>
struct range_tag
{
std::size_t fname;
int start_line, start_col;
int end_line, end_col;
friend bool operator==(range_tag const & lhs, range_tag const & rhs)
{
return lhs.fname == rhs.fname && lhs.start_line == rhs.start_line && lhs.start_col == rhs.start_col
&& lhs.end_line == rhs.end_line && lhs.end_col == rhs.end_col;
}
friend bool operator!=(range_tag const & lhs, range_tag const & rhs)
{
return !(lhs == rhs);
}
};
class filename_store
{
public:
std::size_t add(std::string const & fname)
{
BOOST_ASSERT(m_fname_map.size() == m_fnames.size());
std::pair<std::map<std::string, std::size_t>::iterator, bool> ins = m_fname_map.insert(std::make_pair(fname, m_fnames.size()));
if (ins.second)
m_fnames.push_back(fname);
return ins.first->second;
}
std::string operator[](std::size_t idx) const
{
return m_fnames[idx];
}
std::size_t size() const
{
return m_fnames.size();
}
private:
std::map<std::string, std::size_t> m_fname_map;
std::vector<std::string> m_fnames;
};
struct fullexpr_tag
{
std::size_t id;
friend bool operator==(fullexpr_tag const & lhs, fullexpr_tag const & rhs)
{
return lhs.id == rhs.id;
}
friend bool operator!=(fullexpr_tag const & lhs, fullexpr_tag const & rhs)
{
return !(lhs == rhs);
}
};
typedef boost::int64_t sir_int_t;
struct sir_array;
struct sir_dict;
typedef boost::variant<
boost::none_t,
sir_int_t,
double,
std::string,
boost::recursive_wrapper<sir_array>,
boost::recursive_wrapper<sir_dict>
> constant;
struct sir_array
{
std::vector<constant> values;
friend bool operator==(sir_array const & lhs, sir_array const & rhs)
{
return lhs.values == rhs.values;
}
friend bool operator!=(sir_array const & lhs, sir_array const & rhs)
{
return lhs.values != rhs.values;
}
friend std::ostream & operator<<(std::ostream & out, sir_array const & value)
{
out << "[";
if (!value.values.empty())
out << value.values[0];
for (std::size_t i = 1; i < value.values.size(); ++i)
out << ", " << value.values[i];
return out << "]";
}
};
struct sir_dict
{
std::map<std::string, constant> values;
friend bool operator==(sir_dict const & lhs, sir_dict const & rhs)
{
return lhs.values == rhs.values;
}
friend bool operator!=(sir_dict const & lhs, sir_dict const & rhs)
{
return lhs.values != rhs.values;
}
friend std::ostream & operator<<(std::ostream & out, sir_dict const & value)
{
out << "{";
std::map<std::string, constant>::const_iterator ci = value.values.begin();
if (ci != value.values.end())
out << "\"" << ci->first << "\": " << ci->second;
for (++ci; ci != value.values.end(); ++ci)
out << ", " << "\"" << ci->first << "\": " << ci->second;
return out << "}";
}
};
class cfg
{
private:
struct graph_vertex;
public:
typedef boost::variant<range_tag, fullexpr_tag> node_tag_type;
typedef std::size_t node_tag_ref;
typedef graph_vertex * vertex_descriptor;
enum node_type { nt_none, nt_exit, nt_value, nt_call, nt_phi,
nt_adjust, nt_dist, nt_deref, nt_assign, nt_member,
nt_add, nt_sub, nt_mul, nt_div, nt_rem, nt_shl, nt_shr, nt_and, nt_xor, nt_or,
nt_eq, nt_leq, nt_less,
nt_neg, nt_not,
};
enum op_type { ot_none, ot_func, ot_const, ot_node, ot_var, ot_varptr };
typedef boost::variant<boost::none_t, constant, vertex_descriptor> op_id;
template <typename T>
friend T get_const(op_id const & op)
{
return boost::get<T>(boost::get<constant>(op));
}
struct operand
{
op_type type;
op_id id;
operand(op_type type = ot_none, op_id id = boost::none)
: type(type), id(id)
{
}
friend bool operator==(operand const & lhs, operand const & rhs)
{
return lhs.type == rhs.type && lhs.id == rhs.id;
}
friend bool operator!=(operand const & lhs, operand const & rhs)
{
return !(lhs == rhs);
}
};
struct edge
{
std::size_t id;
constant cond;
edge(std::size_t id = 0, constant const & cond = boost::none)
: id(id), cond(cond)
{
}
friend bool operator==(edge const & lhs, edge const & rhs)
{
return lhs.id == rhs.id && lhs.cond == rhs.cond;
}
friend bool operator!=(edge const & lhs, edge const & rhs)
{
return !(lhs == rhs);
}
};
struct node
{
node_type type;
std::vector<operand> ops;
std::set<node_tag_ref> tags;
node(node_type type = nt_none)
: type(type)
{
}
};
private:
struct graph_edge
{
vertex_descriptor target;
edge prop;
graph_edge(vertex_descriptor target, edge prop = edge())
: target(target), prop(prop)
{
}
};
public:
struct edge_descriptor
{
vertex_descriptor source;
std::list<graph_edge>::iterator parity;
edge_descriptor(vertex_descriptor source, std::list<graph_edge>::iterator parity)
: source(source), parity(parity)
{
}
friend bool operator==(edge_descriptor const & lhs, edge_descriptor const & rhs)
{
return lhs.source == rhs.source && lhs.parity == rhs.parity;
}
friend bool operator!=(edge_descriptor const & lhs, edge_descriptor const & rhs)
{
return !(lhs == rhs);
}
friend bool operator<(edge_descriptor const & lhs, edge_descriptor const & rhs)
{
return lhs.source < rhs.source || (lhs.source == rhs.source && &*lhs.parity < &*rhs.parity);
}
};
cfg()
: m_entry(0)
{
}
cfg(cfg const & other)
: m_entry(0), m_params(other.m_params), m_locals(other.m_locals), m_tags(other.m_tags)
{
std::map<vertex_descriptor, vertex_descriptor> isom;
for (std::set<vertex_descriptor>::const_iterator it = other.m_vertices.begin(); it != other.m_vertices.end(); ++it)
{
isom[*it] = this->add_node(other[*it]);
}
if (other.entry() != 0)
m_entry = isom.find(other.entry())->second;
for (std::map<vertex_descriptor, vertex_descriptor>::const_iterator it = isom.begin(); it != isom.end(); ++it)
{
node & n = (*this)[it->second];
for (std::size_t i = 0; i != n.ops.size(); ++i)
{
if (n.ops[i].id.which() == 2)
n.ops[i].id = isom.find(boost::get<cfg::vertex_descriptor>(n.ops[i].id))->second;
}
std::pair<out_edge_iterator, out_edge_iterator> out_edge_range = out_edges(it->first, other);
for (; out_edge_range.first != out_edge_range.second; ++out_edge_range.first)
{
edge_descriptor e = add_edge(it->second, isom.find(target(*out_edge_range.first, other))->second, *this).first;
(*this)[e] = other[*out_edge_range.first];
}
}
}
~cfg()
{
for (std::set<vertex_descriptor>::iterator it = m_vertices.begin(); it != m_vertices.end(); ++it)
{
delete *it;
}
}
cfg & operator=(cfg other)
{
swap(*this, other);
return *this;
}
friend void swap(cfg & lhs, cfg & rhs)
{
using std::swap;
lhs.m_vertices.swap(rhs.m_vertices);
swap(lhs.m_entry, rhs.m_entry);
lhs.m_params.swap(rhs.m_params);
lhs.m_locals.swap(rhs.m_locals);
lhs.m_tags.swap(rhs.m_tags);
}
vertex_descriptor entry() const { return m_entry; }
void entry(vertex_descriptor v) { m_entry = v; }
vertex_descriptor add_node(node const & n)
{
std::auto_ptr<graph_vertex> res(new graph_vertex(n));
m_vertices.insert(res.get());
return res.release();
}
friend vertex_descriptor add_vertex(cfg & g)
{
std::auto_ptr<graph_vertex> res(new graph_vertex());
g.m_vertices.insert(res.get());
return res.release();
}
friend void clear_vertex(vertex_descriptor v, cfg & g)
{
for (std::list<graph_edge>::iterator it = v->succs.begin(); it != v->succs.end(); ++it)
it->target->preds.erase(edge_descriptor(v, it));
v->succs.clear();
for (std::set<edge_descriptor>::const_iterator it = v->preds.begin(); it != v->preds.end(); ++it)
it->source->succs.erase(it->parity);
v->preds.clear();
}
friend void remove_vertex(vertex_descriptor v, cfg & g)
{
BOOST_ASSERT(v->preds.empty());
BOOST_ASSERT(v->succs.empty());
if (g.m_entry == v)
g.m_entry = 0;
g.m_vertices.erase(v);
delete v;
}
friend std::pair<edge_descriptor, bool> add_edge(vertex_descriptor source, vertex_descriptor target, cfg & g)
{
source->succs.push_back(target);
edge_descriptor new_edge(source, boost::prior(source->succs.end()));
target->preds.insert(new_edge);
return std::make_pair(new_edge, true);
}
void redirect_vertex(cfg::vertex_descriptor source, cfg::vertex_descriptor target)
{
// TODO: basic exception guarantees
for (std::set<edge_descriptor>::const_iterator it = source->preds.begin(); it != source->preds.end(); ++it)
{
it->parity->target = target;
target->preds.insert(*it);
}
source->preds.clear();
if (m_entry == source)
m_entry = target;
}
friend vertex_descriptor source(edge_descriptor const & e, cfg const &)
{
return e.source;
}
friend vertex_descriptor target(edge_descriptor const & e, cfg const & g)
{
return e.parity->target;
}
static vertex_descriptor null_vertex()
{
return 0;
}
typedef std::set<vertex_descriptor>::const_iterator vertex_iterator;
friend std::pair<vertex_iterator, vertex_iterator> vertices(cfg const & g)
{
return std::pair<vertex_iterator, vertex_iterator>(g.m_vertices.begin(), g.m_vertices.end());
};
node & operator[](vertex_descriptor v) { return v->prop; }
node const & operator[](vertex_descriptor v) const { return v->prop; }
edge & operator[](edge_descriptor e) { return e.parity->prop; }
edge const & operator[](edge_descriptor e) const { return e.parity->prop; }
struct out_edge_iterator
: boost::iterator_facade<out_edge_iterator, edge_descriptor const, boost::bidirectional_traversal_tag>
{
out_edge_iterator(vertex_descriptor source, std::list<graph_edge>::iterator parity)
: m_current(source, parity)
{
}
edge_descriptor const & dereference() const { return m_current; };
void increment() { ++m_current.parity; }
void decrement() { --m_current.parity; }
bool equal(out_edge_iterator const & other) const { return m_current == other.m_current; }
private:
edge_descriptor m_current;
};
friend std::pair<out_edge_iterator, out_edge_iterator> out_edges(vertex_descriptor v, cfg const & g)
{
return std::make_pair(out_edge_iterator(v, v->succs.begin()), out_edge_iterator(v, v->succs.end()));
}
typedef std::set<edge_descriptor>::const_iterator in_edge_iterator;
friend std::pair<in_edge_iterator, in_edge_iterator> in_edges(vertex_descriptor v, cfg const & g)
{
return std::make_pair(v->preds.begin(), v->preds.end());
}
typedef std::size_t degree_size_type;
friend degree_size_type out_degree(vertex_descriptor v, cfg const &)
{
return v->succs.size();
}
friend degree_size_type in_degree(vertex_descriptor v, cfg const &)
{
return v->preds.size();
}
void add_param(std::string const & param)
{
m_params.push_back(param);
}
std::vector<std::string> const & params() const { return m_params; }
void add_local(std::string const & param)
{
m_locals.push_back(param);
}
std::vector<std::string> const & locals() const { return m_locals; }
node_tag_ref add_tag(node_tag_type const & tag)
{
std::vector<node_tag_type>::const_iterator it = std::find(m_tags.begin(), m_tags.end(), tag);
if (it != m_tags.end())
return it - m_tags.begin();
m_tags.push_back(tag);
return m_tags.size() - 1;
}
node_tag_type const & tag(node_tag_ref tagref) const
{
return m_tags[tagref];
}
private:
struct graph_vertex
{
std::list<graph_edge> succs;
std::set<edge_descriptor> preds;
node prop;
graph_vertex(node prop = node())
: prop(prop)
{
}
};
std::set<vertex_descriptor> m_vertices;
vertex_descriptor m_entry;
std::vector<std::string> m_params;
std::vector<std::string> m_locals;
std::vector<node_tag_type> m_tags;
};
class program
{
public:
typedef std::multimap<std::string, std::string> vfn_map_type;
typedef std::map<std::string, std::size_t> vfn_param_counts_type;
void add_cfg(std::string const & name, cfg const & c)
{
m_cfgs.insert(std::make_pair(name, c));
}
std::map<std::string, cfg> const & cfgs() const { return m_cfgs; }
filename_store const & fnames() const { return m_fnames; }
filename_store & fnames() { return m_fnames; }
void pretty_print(std::ostream & out) const;
void vfn_map(vfn_map_type const & v) { m_vfn_map = v; }
vfn_map_type const & vfn_map() const { return m_vfn_map; }
void vfn_param_counts(vfn_param_counts_type const & v) { m_vfn_param_counts = v; }
vfn_param_counts_type const & vfn_param_counts() const { return m_vfn_param_counts; }
std::string get_string_literal_symbol(std::vector<sir_int_t> const & lit, std::string const & static_prefix)
{
std::map<std::vector<sir_int_t>, std::string>::const_iterator ci = m_string_literal_symbols.find(lit);
if (ci == m_string_literal_symbols.end())
{
ci = m_string_literal_symbols.insert(std::make_pair(lit,
"_S" + boost::lexical_cast<std::string>(static_prefix.size()) + static_prefix +
"_Y" + boost::lexical_cast<std::string>(m_string_literal_symbols.size()))).first;
sir_array a;
for (std::size_t i = 0; i < lit.size(); ++i)
a.values.push_back(lit[i]);
m_globals[ci->second] = a;
}
return ci->second;
}
std::map<std::string, constant> const & globals() const { return m_globals; }
private:
std::map<std::string, cfg> m_cfgs;
filename_store m_fnames;
std::map<std::vector<sir_int_t>, std::string> m_string_literal_symbols;
std::map<std::string, constant> m_globals;
vfn_map_type m_vfn_map;
vfn_param_counts_type m_vfn_param_counts;
};
#endif