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
|
/********************* -*- C++ -*- */
/** expr_builder.h
** This file is part of the CVC4 prototype.
** Copyright (c) 2009 The Analysis of Computer Systems Group (ACSys)
** Courant Institute of Mathematical Sciences
** New York University
** See the file COPYING in the top-level source directory for licensing
** information.
**
**/
#ifndef __CVC4__NODE_BUILDER_H
#define __CVC4__NODE_BUILDER_H
#include <vector>
#include <cstdlib>
#include "expr/node_manager.h"
#include "expr/kind.h"
#include "util/Assert.h"
namespace CVC4 {
class AndExprBuilder;
class OrExprBuilder;
class PlusExprBuilder;
class MinusExprBuilder;
class MultExprBuilder;
class NodeBuilder {
NodeManager* d_em;
Kind d_kind;
// initially false, when you extract the Node this is set and you can't
// extract another
bool d_used;
static const unsigned nchild_thresh = 10;
unsigned d_nchildren;
union {
ExprValue* u_arr[nchild_thresh];
std::vector<Node>* u_vec;
} d_children;
void addChild(const Node& e) { addChild(e.d_ev); }
void addChild(ExprValue*);
NodeBuilder& collapse();
typedef ExprValue** ev_iterator;
typedef ExprValue const** const_ev_iterator;
ev_iterator ev_begin() {
return d_children.u_arr;
}
ev_iterator ev_end() {
return d_children.u_arr + d_nchildren;
}
NodeBuilder& reset(const ExprValue*);
public:
NodeBuilder();
NodeBuilder(Kind k);
NodeBuilder(const Node&);
NodeBuilder(const NodeBuilder&);
NodeBuilder(NodeManager*);
NodeBuilder(NodeManager*, Kind k);
NodeBuilder(NodeManager*, const Node&);
NodeBuilder(NodeManager*, const NodeBuilder&);
~NodeBuilder();
// Compound expression constructors
NodeBuilder& eqExpr(const Node& right);
NodeBuilder& notExpr();
NodeBuilder& andExpr(const Node& right);
NodeBuilder& orExpr(const Node& right);
NodeBuilder& iteExpr(const Node& thenpart, const Node& elsepart);
NodeBuilder& iffExpr(const Node& right);
NodeBuilder& impExpr(const Node& right);
NodeBuilder& xorExpr(const Node& right);
/* TODO design: these modify NodeBuilder */
NodeBuilder& operator!() { return notExpr(); }
NodeBuilder& operator&&(const Node& right) { return andExpr(right); }
NodeBuilder& operator||(const Node& right) { return orExpr(right); }
// "Stream" expression constructor syntax
NodeBuilder& operator<<(const Kind& op);
NodeBuilder& operator<<(const Node& child);
// For pushing sequences of children
NodeBuilder& append(const std::vector<Node>& children) { return append(children.begin(), children.end()); }
NodeBuilder& append(Node child) { return append(&child, (&child) + 1); }
template <class Iterator> NodeBuilder& append(const Iterator& begin, const Iterator& end);
operator Node();// not const
AndExprBuilder operator&&(Node);
OrExprBuilder operator||(Node);
PlusExprBuilder operator+ (Node);
PlusExprBuilder operator- (Node);
MultExprBuilder operator* (Node);
friend class AndExprBuilder;
friend class OrExprBuilder;
friend class PlusExprBuilder;
friend class MultExprBuilder;
};/* class NodeBuilder */
class AndExprBuilder {
NodeBuilder d_eb;
public:
AndExprBuilder(const NodeBuilder& eb) : d_eb(eb) {
if(d_eb.d_kind != AND) {
d_eb.collapse();
d_eb.d_kind = AND;
}
}
AndExprBuilder& operator&&(Node);
OrExprBuilder operator||(Node);
operator NodeBuilder() { return d_eb; }
};/* class AndExprBuilder */
class OrExprBuilder {
NodeBuilder d_eb;
public:
OrExprBuilder(const NodeBuilder& eb) : d_eb(eb) {
if(d_eb.d_kind != OR) {
d_eb.collapse();
d_eb.d_kind = OR;
}
}
AndExprBuilder operator&&(Node);
OrExprBuilder& operator||(Node);
operator NodeBuilder() { return d_eb; }
};/* class OrExprBuilder */
class PlusExprBuilder {
NodeBuilder d_eb;
public:
PlusExprBuilder(const NodeBuilder& eb) : d_eb(eb) {
if(d_eb.d_kind != PLUS) {
d_eb.collapse();
d_eb.d_kind = PLUS;
}
}
PlusExprBuilder& operator+(Node);
PlusExprBuilder& operator-(Node);
MultExprBuilder operator*(Node);
operator NodeBuilder() { return d_eb; }
};/* class PlusExprBuilder */
class MultExprBuilder {
NodeBuilder d_eb;
public:
MultExprBuilder(const NodeBuilder& eb) : d_eb(eb) {
if(d_eb.d_kind != MULT) {
d_eb.collapse();
d_eb.d_kind = MULT;
}
}
PlusExprBuilder operator+(Node);
PlusExprBuilder operator-(Node);
MultExprBuilder& operator*(Node);
operator NodeBuilder() { return d_eb; }
};/* class MultExprBuilder */
template <class Iterator>
inline NodeBuilder& NodeBuilder::append(const Iterator& begin, const Iterator& end) {
for(Iterator i = begin; i != end; ++i)
addChild(*i);
return *this;
}
// not const
inline NodeBuilder::operator Node() {
ExprValue *ev;
uint64_t hash;
Assert(d_kind != UNDEFINED_KIND, "Can't make an expression of an undefined kind!");
// variables are permitted to be duplicates (from POV of the expression manager)
if(d_kind == VARIABLE) {
ev = new ExprValue;
ev->d_kind = d_kind;
ev->d_id = ExprValue::next_id++;// FIXME multithreading
return Node(ev);
} else {
if(d_nchildren <= nchild_thresh) {
hash = ExprValue::computeHash<ev_iterator>(d_kind, ev_begin(), ev_end());
void *space = std::calloc(1, sizeof(ExprValue) + d_nchildren * sizeof(Node));
ev = new (space) ExprValue;
size_t nc = 0;
ev_iterator i = ev_begin();
ev_iterator i_end = ev_end();
for(; i != i_end; ++i) {
// The expressions in the allocated children are all 0, so we must
// construct it without using an assignment operator
ev->d_children[nc++].assignExprValue(*i);
}
} else {
hash = ExprValue::computeHash<std::vector<Node>::const_iterator>(d_kind, d_children.u_vec->begin(), d_children.u_vec->end());
void *space = std::calloc(1, sizeof(ExprValue) + d_nchildren * sizeof(Node));
ev = new (space) ExprValue;
size_t nc = 0;
for(std::vector<Node>::iterator i = d_children.u_vec->begin(); i != d_children.u_vec->end(); ++i)
ev->d_children[nc++] = Node(*i);
}
}
ev->d_nchildren = d_nchildren;
ev->d_kind = d_kind;
ev->d_id = ExprValue::next_id++;// FIXME multithreading
ev->d_rc = 0;
Node e(ev);
return d_em->lookup(hash, e);
}
inline AndExprBuilder NodeBuilder::operator&&(Node e) {
return AndExprBuilder(*this) && e;
}
inline OrExprBuilder NodeBuilder::operator||(Node e) {
return OrExprBuilder(*this) || e;
}
inline PlusExprBuilder NodeBuilder::operator+ (Node e) {
return PlusExprBuilder(*this) + e;
}
inline PlusExprBuilder NodeBuilder::operator- (Node e) {
return PlusExprBuilder(*this) - e;
}
inline MultExprBuilder NodeBuilder::operator* (Node e) {
return MultExprBuilder(*this) * e;
}
inline AndExprBuilder& AndExprBuilder::operator&&(Node e) {
d_eb.append(e);
return *this;
}
inline OrExprBuilder AndExprBuilder::operator||(Node e) {
return OrExprBuilder(d_eb.collapse()) || e;
}
inline AndExprBuilder OrExprBuilder::operator&&(Node e) {
return AndExprBuilder(d_eb.collapse()) && e;
}
inline OrExprBuilder& OrExprBuilder::operator||(Node e) {
d_eb.append(e);
return *this;
}
inline PlusExprBuilder& PlusExprBuilder::operator+(Node e) {
d_eb.append(e);
return *this;
}
inline PlusExprBuilder& PlusExprBuilder::operator-(Node e) {
d_eb.append(e.uMinusExpr());
return *this;
}
inline MultExprBuilder PlusExprBuilder::operator*(Node e) {
return MultExprBuilder(d_eb.collapse()) * e;
}
inline PlusExprBuilder MultExprBuilder::operator+(Node e) {
return MultExprBuilder(d_eb.collapse()) + e;
}
inline PlusExprBuilder MultExprBuilder::operator-(Node e) {
return MultExprBuilder(d_eb.collapse()) - e;
}
inline MultExprBuilder& MultExprBuilder::operator*(Node e) {
d_eb.append(e);
return *this;
}
}/* CVC4 namespace */
#endif /* __CVC4__NODE_BUILDER_H */
|