-
Notifications
You must be signed in to change notification settings - Fork 0
/
expr_mgr.hh
498 lines (388 loc) · 13.8 KB
/
expr_mgr.hh
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
/**
* @file expr_mgr.hh
* @brief Expression management. ExprMgr class
*
* This module contains definitions and services that implement an
* optimized storage for expressions. Expressions are stored in a
* Directed Acyclic Graph (DAG) for data sharing.
*
* Copyright (C) 2012 Marco Pensallorto < marco AT pensallorto DOT gmail DOT com >
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
**/
#ifndef EXPR_MGR_H
#define EXPR_MGR_H
#include <expr.hh>
#include <pool.hh>
typedef class ExprMgr* ExprMgr_ptr;
class ExprMgr {
public:
// -- expr makers (mostly inlined) -----------------------------------------
/* LTL */
inline Expr_ptr make_F(Expr_ptr expr)
{ return make_expr(F, expr, NULL); }
inline Expr_ptr make_G(Expr_ptr expr)
{ return make_expr(G, expr, NULL); }
inline Expr_ptr make_X(Expr_ptr expr)
{ return make_expr(X, expr, NULL); }
inline Expr_ptr make_U(Expr_ptr lhs, Expr_ptr rhs)
{ return make_expr(U, lhs, rhs); }
inline Expr_ptr make_R(Expr_ptr lhs, Expr_ptr rhs)
{ return make_expr(R, lhs, rhs); }
/* CTL (A ops) */
inline Expr_ptr make_AF(Expr_ptr expr)
{ return make_expr(AF, expr, NULL); }
inline Expr_ptr make_AG(Expr_ptr expr)
{ return make_expr(AG, expr, NULL); }
inline Expr_ptr make_AX(Expr_ptr expr)
{ return make_expr(AX, expr, NULL); }
inline Expr_ptr make_AU(Expr_ptr lhs, Expr_ptr rhs)
{ return make_expr(AU, lhs, rhs); }
inline Expr_ptr make_AR(Expr_ptr lhs, Expr_ptr rhs)
{ return make_expr(AR, lhs, rhs); }
/* CTL (E ops) */
inline Expr_ptr make_EF(Expr_ptr expr)
{ return make_expr(EF, expr, NULL); }
inline Expr_ptr make_EG(Expr_ptr expr)
{ return make_expr(EG, expr, NULL); }
inline Expr_ptr make_EX(Expr_ptr expr)
{ return make_expr(EX, expr, NULL); }
inline Expr_ptr make_EU(Expr_ptr lhs, Expr_ptr rhs)
{ return make_expr(EU, lhs, rhs); }
inline Expr_ptr make_ER(Expr_ptr lhs, Expr_ptr rhs)
{ return make_expr(ER, lhs, rhs); }
/* primary expressions */
inline Expr_ptr make_next(Expr_ptr expr)
{ return make_expr(NEXT, expr, NULL); }
/* arithmetical operators */
inline Expr_ptr make_neg(Expr_ptr expr)
{ return make_expr(NEG, expr, NULL); }
inline Expr_ptr make_add(Expr_ptr a, Expr_ptr b)
{ return make_expr(PLUS, a, b); }
inline Expr_ptr make_sub(Expr_ptr a, Expr_ptr b)
{ return make_expr(SUB, a, b); }
inline Expr_ptr make_div(Expr_ptr a, Expr_ptr b)
{ return make_expr(DIV, a, b); }
inline Expr_ptr make_mul(Expr_ptr a, Expr_ptr b)
{ return make_expr(MUL, a, b); }
inline Expr_ptr make_mod(Expr_ptr a, Expr_ptr b)
{ return make_expr(MOD, a, b); }
/* logical/bitwise operators */
inline Expr_ptr make_not(Expr_ptr expr)
{ return make_expr(NOT, expr, NULL); }
inline Expr_ptr make_and(Expr_ptr a, Expr_ptr b)
{ return make_expr(AND, a, b); }
inline Expr_ptr make_or(Expr_ptr a, Expr_ptr b)
{ return make_expr(OR, a, b); }
inline Expr_ptr make_lshift(Expr_ptr a, Expr_ptr b)
{ return make_expr(LSHIFT, a, b); }
inline Expr_ptr make_rshift(Expr_ptr a, Expr_ptr b)
{ return make_expr(RSHIFT, a, b); }
inline Expr_ptr make_xor(Expr_ptr a, Expr_ptr b)
{ return make_expr(XOR, a, b); }
inline Expr_ptr make_xnor(Expr_ptr a, Expr_ptr b)
{ return make_expr(XNOR, a, b); }
inline Expr_ptr make_implies(Expr_ptr a, Expr_ptr b)
{ return make_expr(IMPLIES, a, b); }
inline Expr_ptr make_iff(Expr_ptr a, Expr_ptr b)
{ return make_expr(IFF, a, b); }
/* relational operators */
inline Expr_ptr make_eq(Expr_ptr a, Expr_ptr b)
{ return make_expr(EQ, a, b); }
inline Expr_ptr make_ne(Expr_ptr a, Expr_ptr b)
{ return make_expr(NE, a, b); }
inline Expr_ptr make_ge(Expr_ptr a, Expr_ptr b)
{ return make_expr(GE, a, b); }
inline Expr_ptr make_gt(Expr_ptr a, Expr_ptr b)
{ return make_expr(GT, a, b); }
inline Expr_ptr make_le(Expr_ptr a, Expr_ptr b)
{ return make_expr(LE, a, b); }
inline Expr_ptr make_lt(Expr_ptr a, Expr_ptr b)
{ return make_expr(LT, a, b); }
inline Expr_ptr make_cond(Expr_ptr a, Expr_ptr b)
{ return make_expr(COND, a, b); }
inline Expr_ptr make_ite(Expr_ptr a, Expr_ptr b)
{ return make_expr(ITE, a, b); }
inline Expr_ptr make_iconst(value_t value)
{
Expr tmp(ICONST, value); // we need a temp store
return __make_expr(&tmp);
}
inline Expr_ptr make_zero()
{
Expr tmp(ICONST, 0); // we need a temp store
return __make_expr(&tmp);
}
inline Expr_ptr make_one()
{
Expr tmp(ICONST, 1); // we need a temp store
return __make_expr(&tmp);
}
inline Expr_ptr make_hconst(value_t value)
{
Expr tmp(HCONST, value); // we need a temp store
return __make_expr(&tmp);
}
inline Expr_ptr make_oconst(value_t value)
{
Expr tmp(OCONST, value); // we need a temp store
return __make_expr(&tmp);
}
inline Expr_ptr make_dot(Expr_ptr a, Expr_ptr b)
{ return make_expr(DOT, a, b); }
inline Expr_ptr make_comma(Expr_ptr a, Expr_ptr b)
{ return make_expr(COMMA, a, b); }
inline Expr_ptr make_subscript(Expr_ptr a, Expr_ptr b)
{ return make_expr(SUBSCRIPT, a, b); }
inline Expr_ptr make_params(Expr_ptr a, Expr_ptr b)
{ return make_expr(PARAMS, a, b); }
/* type makers */
inline Expr_ptr make_boolean_type() const
{ return bool_expr; }
inline Expr_ptr make_int_const_type() const
{ return int_expr; }
inline Expr_ptr make_fxd_const_type() const
{ return fxd_expr; }
inline Expr_ptr make_range_type(Expr_ptr a, Expr_ptr b)
{
assert(is_int_numeric(a));
assert(is_int_numeric(b));
return make_expr(RANGE, a, b);
}
inline Expr_ptr make_abstract_unsigned_int_type()
{
return unsigned_int_expr;
}
inline Expr_ptr make_unsigned_int_type(unsigned digits)
{
return make_params(unsigned_int_expr,
make_iconst((value_t) digits));
}
inline Expr_ptr make_abstract_signed_int_type()
{
return signed_int_expr;
}
inline Expr_ptr make_signed_int_type(unsigned digits)
{
return make_params(signed_int_expr,
make_iconst((value_t) digits));
}
inline Expr_ptr make_abstract_unsigned_fxd_type()
{
return unsigned_fxd_expr;
}
inline Expr_ptr make_unsigned_fxd_type(unsigned int_digits,
unsigned fract_digits)
{
return make_params(unsigned_fxd_expr,
make_comma(make_iconst((value_t) int_digits),
make_iconst((value_t) fract_digits)));
}
inline Expr_ptr make_abstract_signed_fxd_type()
{
return signed_fxd_expr;
}
inline Expr_ptr make_signed_fxd_type(unsigned int_digits,
unsigned fract_digits)
{
return make_params(signed_fxd_expr,
make_comma(make_iconst((value_t) int_digits),
make_iconst((value_t) fract_digits)));
}
inline Expr_ptr make_abstract_enum_type()
{
return enum_expr;
}
Expr_ptr make_enum_type(ExprSet& literals);
inline Expr_ptr make_abstract_inst_type()
{
return inst_expr;
}
inline Expr_ptr make_abstract_array_type()
{
return array_expr;
}
/* builtin identifiers */
inline Expr_ptr make_main() const
{ return main_expr; }
inline Expr_ptr make_false()
{ return false_expr; }
inline Expr_ptr make_true()
{ return true_expr; }
// Here a bit of magic occurs, so it's better to keep a note:
// this method is used by the parser to build identifier
// nodes. The function is fed with a const char* coming from
// the Lexer, an Atom object (which in current implementation
// is in fact a std::string) is built on-the-fly and used to
// search the atom pool. The atom resulting from the search is
// always the one stored in the pool. The auto atom object,
// however gets destroyed as it gets out of scope, so no leak
// occurs.
inline Expr_ptr make_identifier(Atom atom)
{
AtomPoolHit ah = f_atom_pool.insert(atom);
const Atom& pooled_atom = (* ah.first);
#if 0
if (ah.second) {
DRIVEL << "Added new atom to pool: '"
<< pooled_atom << "'" << endl;
}
#endif
// no copy occurs here
return make_expr(pooled_atom);
}
inline Expr_ptr make_dec_const(Atom atom)
{ return make_iconst( strtoll(atom.c_str(), NULL, 10)); }
inline Expr_ptr make_hex_const(Atom atom)
{ return make_hconst( strtoll(atom.c_str(), NULL, 16)); }
inline Expr_ptr make_oct_const(Atom atom)
{ return make_oconst( strtoll(atom.c_str(), NULL, 8)); }
// -- is-a predicates -------------------------------------------------------
inline bool is_identifier(const Expr_ptr expr) const {
assert(expr);
return expr->f_symb == IDENT;
}
inline bool is_next(const Expr_ptr expr) const {
assert(expr);
return expr->f_symb == NEXT;
}
inline bool is_int_numeric(const Expr_ptr expr) const {
assert(expr);
return (expr->f_symb == ICONST)
|| (expr->f_symb == HCONST)
|| (expr->f_symb == OCONST) ;
}
inline bool is_fxd_numeric(const Expr_ptr expr) const {
assert(expr);
return (expr->f_symb == FCONST);
}
// expr inspectors, used by compiler as helpers to determine operands type
inline bool is_unary_logical(const Expr_ptr expr) const {
assert(expr);
ExprType symb = expr->f_symb;
return (NOT == symb);
}
inline bool is_ite(const Expr_ptr expr) const {
assert(expr);
ExprType symb = expr->f_symb;
return (ITE == symb);
}
inline bool is_cond(const Expr_ptr expr) const {
assert(expr);
ExprType symb = expr->f_symb;
return (COND == symb);
}
inline bool is_binary_logical(const Expr_ptr expr) const {
assert(expr);
ExprType symb = expr->f_symb;
return ((AND == symb) ||
(OR == symb) ||
(XOR == symb) ||
(XNOR == symb) ||
(IFF == symb) ||
(IMPLIES == symb));
}
inline bool is_unary_arithmetical(const Expr_ptr expr) const {
assert(expr);
ExprType symb = expr->f_symb;
return ( (NEG == symb) );
}
inline bool is_binary_arithmetical(const Expr_ptr expr) const {
assert(expr);
ExprType symb = expr->f_symb;
return ((PLUS == symb) ||
(SUB == symb) ||
(DIV == symb) ||
(MUL == symb) ||
(MOD == symb) ||
(RSHIFT == symb) ||
(LSHIFT == symb));
}
inline bool is_binary_relational(const Expr_ptr expr) const {
assert(expr);
ExprType symb = expr->f_symb;
return ((EQ == symb) ||
(NE == symb) ||
(LT == symb) ||
(LE == symb) ||
(GT == symb) ||
(GE == symb));
}
// singleton instance accessor
static inline ExprMgr& INSTANCE() {
if (! f_instance) {
f_instance = new ExprMgr();
}
return (*f_instance);
}
protected:
ExprMgr();
~ExprMgr();
private:
static ExprMgr_ptr f_instance;
/* mid level services, inlined for performance */
inline Expr_ptr make_expr(ExprType et, Expr_ptr a, Expr_ptr b)
{
Expr tmp(et, a, b); // we need a temp store
return __make_expr(&tmp);
}
inline Expr_ptr make_expr(const Atom& atom)
{
Expr tmp(atom); // we need a temp store
return __make_expr(&tmp);
}
// low-level, inlined for performance
inline Expr_ptr __make_expr(Expr_ptr expr) {
ExprPoolHit eh = f_expr_pool.insert(*expr);
Expr_ptr pooled_expr = const_cast<Expr_ptr> (& (*eh.first));
#if 0
if (eh.second) {
DRIVEL << "Added new expr to pool: '"
<< pooled_expr << "'" << endl;
}
#endif
return pooled_expr;
}
// utils
value_t pow2(unsigned exp);
/* -- data ------------------------------------------------------------- */
// // temporal exprs type
// Expr_ptr temporal_expr;
/* boolean exprs type and constants */
Expr_ptr bool_expr;
Expr_ptr false_expr;
Expr_ptr true_expr;
/* main module */
Expr_ptr main_expr;
/* reserved for abstract enum types */
Expr_ptr enum_expr;
/* reserved for abstract instance types */
Expr_ptr inst_expr;
/* reserved for abstract array types */
Expr_ptr array_expr;
/* integers */
Expr_ptr unsigned_int_expr;
Expr_ptr signed_int_expr;
Expr_ptr int_expr;
/* fixed-point reals */
Expr_ptr unsigned_fxd_expr;
Expr_ptr signed_fxd_expr;
Expr_ptr fxd_expr;
/* shared pools */
ExprPool f_expr_pool;
AtomPool f_atom_pool;
};
#endif