z3-src 415.4.3

Source distribution of the Z3 SMT solver, for use as a build dependency
Documentation
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
/*++
Copyright (c) 2006 Microsoft Corporation

Module Name:

    dependency.h

Abstract:

    <abstract>

Author:

    Leonardo de Moura (leonardo) 2008-12-10.

Revision History:

--*/
#pragma once

#include "util/vector.h"
#include "util/region.h"

template<typename C>
class dependency_manager {
public:
    typedef typename C::value         value;
    typedef typename C::value_manager value_manager;
    typedef typename C::allocator     allocator;

    class dependency { 
        unsigned  m_ref_count:30;
        unsigned  m_mark:1;
        unsigned  m_leaf:1;
        friend class dependency_manager;
        dependency(bool leaf):
            m_ref_count(0),
            m_mark(false),
            m_leaf(leaf) {
        }
        bool is_marked() const { return m_mark == 1; }
        void mark() { m_mark = true; }
        void unmark() { m_mark = false; }
    public:
        unsigned get_ref_count() const { return m_ref_count; }
        bool is_leaf() const { return m_leaf == 1; }
        value const& leaf_value() const { SASSERT(is_leaf()); return static_cast<leaf const*>(this)->m_value; }
    };

    static void linearize_todo(ptr_vector<dependency>& todo, vector<value, false>& vs) {
        unsigned qhead = 0;
        while (qhead < todo.size()) {
            dependency* d = todo[qhead];
            qhead++;
            if (d->is_leaf()) {
                vs.push_back(to_leaf(d)->m_value);
            }
            else {
                for (unsigned i = 0; i < 2; ++i) {
                    dependency* child = to_join(d)->m_children[i];
                    if (!child->is_marked()) {
                        todo.push_back(child);
                        child->mark();
                    }
                }
            }
        }
        for (auto* d : todo)
            d->unmark();
    }

    static void s_linearize(dependency* d, vector<value, false>& vs) {
        if (!d)
            return;
        ptr_vector<dependency> todo;
        todo.push_back(d);
        linearize_todo(todo, vs);
    }

private:
    struct join : public dependency {
        dependency *    m_children[2];
        join(dependency * d1, dependency * d2):
            dependency(false) {
            m_children[0] = d1;
            m_children[1] = d2;
        }
    };
    
    struct leaf : public dependency {
        value     m_value;
        leaf(value const & v):
            dependency(true),
            m_value(v) {
        }
    };

    static join * to_join(dependency * d) { SASSERT(!d->is_leaf()); return static_cast<join*>(d); }
    static leaf * to_leaf(dependency * d) { SASSERT(d->is_leaf()); return static_cast<leaf*>(d); }

    value_manager &         m_vmanager;
    allocator  &            m_allocator;
    mutable ptr_vector<dependency>  m_todo;

    void inc_ref(value const & v) {
        if (C::ref_count)
            m_vmanager.inc_ref(v);
    }

    void dec_ref(value const & v) {
        if (C::ref_count)
            m_vmanager.dec_ref(v);
    }

    void del(dependency * d) {
        SASSERT(d);
        SASSERT(m_todo.empty());
        m_todo.push_back(d);
        while (!m_todo.empty()) {
            d = m_todo.back();
            m_todo.pop_back();
            if (d->is_leaf()) {
                dec_ref(to_leaf(d)->m_value);
                to_leaf(d)->~leaf();
                m_allocator.deallocate(sizeof(leaf), to_leaf(d));
            }
            else {
                for (unsigned i = 0; i < 2; ++i) {
                    dependency * c = to_join(d)->m_children[i];
                    SASSERT(c->m_ref_count > 0);
                    c->m_ref_count--;
                    if (c->m_ref_count == 0)
                        m_todo.push_back(c);
                }
                to_join(d)->~join();
                m_allocator.deallocate(sizeof(join), to_join(d));
            }
        }
    }

    void unmark_todo() const {
        for (auto* d : m_todo) 
            d->unmark();
        m_todo.reset();
    }

public:
    
    dependency_manager(value_manager & m, allocator & a):
        m_vmanager(m),
        m_allocator(a) {
    }

    void inc_ref(dependency * d) {
        if (d)
            d->m_ref_count++;
    }
    
    void dec_ref(dependency * d) {
        if (d) {
            SASSERT(d->m_ref_count > 0);
            d->m_ref_count--;
            if (d->m_ref_count == 0)
                del(d);
        }
    }
        
    dependency * mk_empty() {
        return nullptr;
    }

    dependency * mk_leaf(value const & v) {
        void * mem = m_allocator.allocate(sizeof(leaf));
        inc_ref(v);
        return new (mem) leaf(v);
    }
    
    dependency * mk_join(dependency * d1, dependency * d2) {
        if (d1 == nullptr) {
            return d2;
        }
        else if (d2 == nullptr) {
            return d1; 
        }
        else if (d1 == d2) {
            return d1;
        }
        else {
            void * mem = m_allocator.allocate(sizeof(join));
            inc_ref(d1); inc_ref(d2);
            return new (mem) join(d1, d2);
        }
    }

    bool contains(dependency * d, value const & v) {
        if (d) {
            m_todo.reset();
            d->mark();
            m_todo.push_back(d);
            unsigned qhead = 0;
            while (qhead < m_todo.size()) {
                d = m_todo[qhead];
                qhead++;
                if (d->is_leaf()) {
                    if (to_leaf(d)->m_value == v) {
                        unmark_todo();
                        return true;
                    }
                }
                else {
                    for (unsigned i = 0; i < 2; ++i) {
                        dependency * child = to_join(d)->m_children[i];
                        if (!child->is_marked()) {
                            m_todo.push_back(child);
                            child->mark();
                        }
                    }
                }
            }
            unmark_todo();
        }
        return false;
    }



    void linearize(dependency * d, vector<value, false> & vs) const {
        if (!d) 
            return;
        SASSERT(m_todo.empty());
        d->mark();
        m_todo.push_back(d);
        linearize_todo(m_todo, vs);
        m_todo.reset();
    }

    void linearize(ptr_vector<dependency>& deps, vector<value, false> & vs) const {
        if (deps.empty())
            return;
        SASSERT(m_todo.empty());
        for (auto* d : deps) {
            if (d && !d->is_marked()) {
                d->mark();
                m_todo.push_back(d);
            }
        }
        linearize_todo(m_todo, vs);
        m_todo.reset();
    }
};

/**
   \brief Version of the dependency_manager where 
   memory management is scoped (i.e., reference counting is ignored),
   and push_scope/pop_scope are used instead. 

   Value must be a primitive type such as an integer or pointer.
*/
template<typename Value>
class scoped_dependency_manager {

    class config {
    public:
        static const bool ref_count        = true;

        typedef Value value;

        class value_manager {
        public:
            void inc_ref(value const & v) {
            }

            void dec_ref(value const & v) {
            }
        };

        class allocator {
            region   m_region;
        public:
            void * allocate(size_t sz) {
                return m_region.allocate(sz);
            }
            
            void deallocate(size_t sz, void * mem) {
            }
            
            void push_scope() { 
                m_region.push_scope(); 
            }
            
            void pop_scope(unsigned num) {
                m_region.pop_scope(num);
            }
            
            void reset() {
                m_region.reset();
            }
        };
    };

    typedef dependency_manager<config>       dep_manager;
public:
    typedef typename dep_manager::dependency dependency;
    typedef Value value;

private:
    typename config::value_manager m_vmanager;
    typename config::allocator     m_allocator;
    dep_manager                    m_dep_manager;

public:
    scoped_dependency_manager():
        m_dep_manager(m_vmanager, m_allocator) {
    }

    dependency * mk_empty() {
        return m_dep_manager.mk_empty();
    }
    
    dependency * mk_leaf(value const & v) {
        return m_dep_manager.mk_leaf(v); 
    }
    
    dependency * mk_join(dependency * d1, dependency * d2) {
        return m_dep_manager.mk_join(d1, d2);
    }

    bool contains(dependency * d, value const & v) {
        return m_dep_manager.contains(d, v); 
    }

    void linearize(dependency * d, vector<value, false> & vs) const {
        return m_dep_manager.linearize(d, vs);
    }    

    static vector<value, false> const& s_linearize(dependency* d, vector<value, false>& vs) {
        dep_manager::s_linearize(d, vs);
        return vs;
    }

    void linearize(ptr_vector<dependency>& d, vector<value, false> & vs) const {
        return m_dep_manager.linearize(d, vs);
    }    
    
    void reset() {
        m_allocator.reset();
    }
    
    void push_scope() {
        m_allocator.push_scope();
    }

    void pop_scope(unsigned num_scopes) {
        m_allocator.pop_scope(num_scopes);
    }
};

// Implement old dependency manager used by interval and groebner 
typedef scoped_dependency_manager<void*>             v_dependency_manager;
typedef scoped_dependency_manager<void*>::dependency v_dependency;
typedef scoped_dependency_manager<unsigned>             u_dependency_manager;
typedef scoped_dependency_manager<unsigned>::dependency u_dependency;

/**
   \brief Version of the scoped-depenendcy-manager where region scopes are handled externally.
*/
template<typename Value>
class stacked_dependency_manager {

    class config {
    public:
        static const bool ref_count = true;

        typedef Value value;

        class value_manager {
        public:
            void inc_ref(value const& v) {
            }

            void dec_ref(value const& v) {
            }
        };

        class allocator {
            region&   m_region;
        public:
            allocator(region& r) : m_region(r) {}

            void* allocate(size_t sz) { 
                return m_region.allocate(sz);
            }            

            void deallocate(size_t sz, void* mem) {
            }
        };
    };

    typedef dependency_manager<config>       dep_manager;
public:
    typedef typename dep_manager::dependency dependency;
    typedef Value value;

private:
    typename config::value_manager m_vmanager;
    typename config::allocator     m_allocator;
    dep_manager                    m_dep_manager;

public:
    stacked_dependency_manager(region& r) :
        m_allocator(r),
        m_dep_manager(m_vmanager, m_allocator) {
    }

    dependency* mk_empty() {
        return m_dep_manager.mk_empty();
    }

    dependency* mk_leaf(value const& v) {
        return m_dep_manager.mk_leaf(v);
    }

    dependency* mk_join(dependency* d1, dependency* d2) {
        return m_dep_manager.mk_join(d1, d2);
    }

    bool contains(dependency* d, value const& v) {
        return m_dep_manager.contains(d, v);
    }

    void linearize(dependency* d, vector<value, false>& vs) {
        return m_dep_manager.linearize(d, vs);
    }

    static vector<value, false> const& s_linearize(dependency* d, vector<value, false>& vs) {
        dep_manager::s_linearize(d, vs);
        return vs;
    }

    void linearize(ptr_vector<dependency>& d, vector<value, false>& vs) {
        return m_dep_manager.linearize(d, vs);
    }
};