-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathyices_api.c
12418 lines (9949 loc) · 306 KB
/
yices_api.c
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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is part of the Yices SMT Solver.
* Copyright (C) 2017 SRI International.
*
* Yices is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Yices 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Yices. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* YICES API
*/
/*
* This module implements the API defined in yices.h.
*
* It also implements the functions defined in yices_extensions.h, yices_api_lock_free.h and
* yices_iterators.h
*/
/*
* Visibility control: all extern functions declared here are in libyices's API
* Other extern functions should have visibility=hidden (cf. Makefile).
*
* On cygwin/mingw, we have two cases:
* - static build: NOYICES_DLL is defined.
* - dynamic build: NOYICES_DLL is not defined.
*
* We don't want the attribute __declspec(dllexport) when NOYICES_DLL is defined
* otherwise clang gives compilation warnings
*/
#if defined(CYGWIN) || defined(MINGW)
// Windows build
#if defined(NOYICES_DLL)
#define EXPORTED
#else
#define EXPORTED __declspec(dllexport)
#define __YICES_DLLSPEC__ EXPORTED
#endif
#else
// Not Windows
#define EXPORTED __attribute__((visibility("default")))
#endif
#include <assert.h>
#include <stddef.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include "api/context_config.h"
#include "api/search_parameters.h"
#include "api/smt_logic_codes.h"
#include "api/yices_error.h"
#include "api/yices_error_report.h"
#include "api/yices_api_lock_free.h"
#include "api/yices_extensions.h"
#include "api/yices_globals.h"
#include "api/yices_iterators.h"
#include "api/yval.h"
#include "context/context.h"
#include "exists_forall/ef_client.h"
#include "frontend/yices/yices_parser.h"
#include "io/model_printer.h"
#include "io/term_printer.h"
#include "io/type_printer.h"
#include "io/yices_pp.h"
#include "model/generalization.h"
#include "model/literal_collector.h"
#include "model/map_to_model.h"
#include "model/model_queries.h"
#include "model/models.h"
#include "model/val_to_term.h"
#include "solvers/cdcl/delegate.h"
#include "terms/bv64_constants.h"
#include "terms/bvarith64_buffer_terms.h"
#include "terms/bvarith_buffer_terms.h"
#include "terms/rba_buffer_terms.h"
#include "terms/term_explorer.h"
#include "terms/term_manager.h"
#include "terms/term_substitution.h"
#include "terms/term_utils.h"
#include "terms/types.h"
#include "utils/dl_lists.h"
#include "utils/int_array_sort.h"
#include "utils/refcount_strings.h"
#include "utils/sparse_arrays.h"
#include "utils/string_utils.h"
#ifdef HAVE_MCSAT
#include <poly/algebraic_number.h>
#else
// We need a definition for (lp_algebraic_number_t *)
typedef void lp_algebraic_number_t;
#endif
#include "mt/thread_macros.h"
#include "yices.h"
/****************************
* GLOBAL DATA STRUCTURES *
***************************/
// rational for building terms: protected by ownership of global lock.
static rational_t r0;
// buffer for building bitvector constants: protected by ownership of global lock.
static bvconstant_t bv0;
/*
* Initial sizes of the type and term tables.
*/
#define INIT_TYPE_SIZE 16
#define INIT_TERM_SIZE 64
/*
* Global table. The actual initialization is done in yices_init() and
* init_globals().
*/
yices_globals_t __yices_globals = {
};
/*
* SYNCHRONIZING ACCESS TO GLOBAL TABLE.
*/
/*
* Attempt to obtain sole access to yices' global data structures.
* In thread safe mode, calling this function will block all other
* yices API routines from accessing the global data structures.
*
* It is an error to call this more than once.
*/
int32_t yices_obtain_mutex(void){
#ifdef THREAD_SAFE
return get_yices_lock(&__yices_globals.lock);
#else
return 0;
#endif
}
/*
* Release the claim to sole access to yices' global data structures.
*
* The callee must have already obtained sole access via yices_obtain_mutex();
*/
int32_t yices_release_mutex(void){
#ifdef THREAD_SAFE
return release_yices_lock(&__yices_globals.lock);
#else
return 0;
#endif
}
/*
* Registry tables for root terms and types (for garbage collection).
* - the two tables are statically allocated but initialized only
* when needed.
* - we keep pointers to the tables:
* Initially, we set root_terms = NULL and root_types = NULL
* On the first call to register a term or type, we initialize the
* static tables and update root_terms/root_types to point to it
*
* - In the thread safe version they are protected by the __yices_globals.lock
*
*/
static sparse_array_t *root_terms;
static sparse_array_t *root_types;
static sparse_array_t the_root_terms;
static sparse_array_t the_root_types;
/************************************
* DYNAMICALLY ALLOCATED OBJECTS *
***********************************/
/*
* All objects that can be allocated via the API
* are stored in doubly-linked lists. This will help
* implement some form of garbage collection at some point.
* For now, this makes it possible to delete all
* global objects when yices_exit is called.
*/
/*
* Doubly-linked list of arithmetic buffers
*/
typedef struct {
dl_list_t header;
rba_buffer_t buffer;
} arith_buffer_elem_t;
static dl_list_t arith_buffer_list;
#ifdef THREAD_SAFE
static yices_lock_t arith_buffer_list_lock;
#endif
/*
* Doubly-linked list of bitvector arithmetic buffers
*/
typedef struct {
dl_list_t header;
bvarith_buffer_t buffer;
} bvarith_buffer_elem_t;
static dl_list_t bvarith_buffer_list;
#ifdef THREAD_SAFE
static yices_lock_t bvarith_buffer_list_lock;
#endif
/*
* Variant: 64bit buffers
*/
typedef struct {
dl_list_t header;
bvarith64_buffer_t buffer;
} bvarith64_buffer_elem_t;
static dl_list_t bvarith64_buffer_list;
#ifdef THREAD_SAFE
static yices_lock_t bvarith64_buffer_list_lock;
#endif
/*
* Doubly-linked list of bitvector buffers
*/
typedef struct {
dl_list_t header;
bvlogic_buffer_t buffer;
} bvlogic_buffer_elem_t;
static dl_list_t bvlogic_buffer_list;
#ifdef THREAD_SAFE
static yices_lock_t bvlogic_buffer_list_lock;
#endif
/*
* Doubly-linked list of contexts
*/
typedef struct {
dl_list_t header;
context_t context;
} context_elem_t;
static dl_list_t context_list;
#ifdef THREAD_SAFE
static yices_lock_t context_list_lock;
#endif
/*
* Models
*/
typedef struct {
dl_list_t header;
model_t model;
} model_elem_t;
static dl_list_t model_list;
#ifdef THREAD_SAFE
static yices_lock_t model_list_lock;
#endif
/*
* Context configurations
*/
typedef struct {
dl_list_t header;
ctx_config_t config;
} ctx_config_elem_t;
static dl_list_t config_list;
#ifdef THREAD_SAFE
static yices_lock_t config_list_lock;
#endif
/*
* Solver parameter descriptors
*/
typedef struct {
dl_list_t header;
param_t param;
} param_structure_elem_t;
static dl_list_t parameter_list;
#ifdef THREAD_SAFE
static yices_lock_t parameter_list_lock;
#endif
static inline void init_list_locks(void){
#ifdef THREAD_SAFE
create_yices_lock(&arith_buffer_list_lock);
create_yices_lock(&bvarith_buffer_list_lock);
create_yices_lock(&bvarith64_buffer_list_lock);
create_yices_lock(&bvlogic_buffer_list_lock);
create_yices_lock(&context_list_lock);
create_yices_lock(&model_list_lock);
create_yices_lock(&config_list_lock);
create_yices_lock(¶meter_list_lock);
#endif
}
static inline void delete_list_locks(void){
#ifdef THREAD_SAFE
destroy_yices_lock(&arith_buffer_list_lock);
destroy_yices_lock(&bvarith_buffer_list_lock);
destroy_yices_lock(&bvarith64_buffer_list_lock);
destroy_yices_lock(&bvlogic_buffer_list_lock);
destroy_yices_lock(&context_list_lock);
destroy_yices_lock(&model_list_lock);
destroy_yices_lock(&config_list_lock);
destroy_yices_lock(¶meter_list_lock);
#endif
}
/* the garbage collector must get all the locks */
static inline void get_list_locks(void){
#ifdef THREAD_SAFE
get_yices_lock(&arith_buffer_list_lock);
get_yices_lock(&bvarith_buffer_list_lock);
get_yices_lock(&bvarith64_buffer_list_lock);
get_yices_lock(&bvlogic_buffer_list_lock);
get_yices_lock(&context_list_lock);
get_yices_lock(&model_list_lock);
get_yices_lock(&config_list_lock);
get_yices_lock(¶meter_list_lock);
#endif
}
/* the garbage collector must also release all the locks */
static inline void release_list_locks(void){
#ifdef THREAD_SAFE
release_yices_lock(&arith_buffer_list_lock);
release_yices_lock(&bvarith_buffer_list_lock);
release_yices_lock(&bvarith64_buffer_list_lock);
release_yices_lock(&bvlogic_buffer_list_lock);
release_yices_lock(&context_list_lock);
release_yices_lock(&model_list_lock);
release_yices_lock(&config_list_lock);
release_yices_lock(¶meter_list_lock);
#endif
}
/**********************************
* ARITHMETIC-BUFFER ALLOCATION *
*********************************/
/*
* Get header of buffer b, assuming b is embedded into an arith_buffer_elem
*/
static inline dl_list_t *arith_buffer_header(rba_buffer_t *b) {
return (dl_list_t *)(((char *)b) - offsetof(arith_buffer_elem_t, buffer));
}
/*
* Get buffer of header l
*/
static inline rba_buffer_t *arith_buffer(dl_list_t *l) {
return (rba_buffer_t *)(((char *) l) + offsetof(arith_buffer_elem_t, buffer));
}
/*
* Allocate an arithmetic buffer and insert it into the list
*/
static inline rba_buffer_t *alloc_arith_buffer(void) {
arith_buffer_elem_t *new_elem;
new_elem = (arith_buffer_elem_t *) safe_malloc(sizeof(arith_buffer_elem_t));
MT_PROTECT_VOID(arith_buffer_list_lock, list_insert_next(&arith_buffer_list, &new_elem->header));
return &new_elem->buffer;
}
/*
* Remove b from the list and free b
*/
static inline void _o_free_arith_buffer(rba_buffer_t *b) {
dl_list_t *elem;
elem = arith_buffer_header(b);
list_remove(elem);
safe_free(elem);
}
static void free_arith_buffer(rba_buffer_t *b) { //BD could this be inline?
MT_PROTECT_VOID(arith_buffer_list_lock, _o_free_arith_buffer(b));
}
/*
* Clean up the arith buffer list: free all elements and empty the list
*/
static void _o_free_arith_buffer_list(void) {
dl_list_t *elem, *aux;
elem = arith_buffer_list.next;
while (elem != &arith_buffer_list) {
aux = elem->next;
delete_rba_buffer(arith_buffer(elem));
safe_free(elem);
elem = aux;
}
clear_list(&arith_buffer_list);
}
static void free_arith_buffer_list(void) {
MT_PROTECT_VOID(arith_buffer_list_lock, _o_free_arith_buffer_list());
}
/********************************************
* BITVECTOR ARITHMETIC BUFFER ALLOCATION *
*******************************************/
/*
* Get header of buffer b, assuming b is embedded into an bvarith_buffer_elem
*/
static inline dl_list_t *bvarith_buffer_header(bvarith_buffer_t *b) {
return (dl_list_t *)(((char *)b) - offsetof(bvarith_buffer_elem_t, buffer));
}
/*
* Get buffer of header l
*/
static inline bvarith_buffer_t *bvarith_buffer(dl_list_t *l) {
return (bvarith_buffer_t *)(((char *) l) + offsetof(bvarith_buffer_elem_t, buffer));
}
/*
* Allocate a bv-arithmetic buffer and insert it into the list
*/
static inline bvarith_buffer_t *_o_alloc_bvarith_buffer(void) {
bvarith_buffer_elem_t *new_elem;
new_elem = (bvarith_buffer_elem_t *) safe_malloc(sizeof(bvarith_buffer_elem_t));
list_insert_next(&bvarith_buffer_list, &new_elem->header);
return &new_elem->buffer;
}
static bvarith_buffer_t *alloc_bvarith_buffer(void) {
MT_PROTECT(bvarith_buffer_t *, bvarith_buffer_list_lock, _o_alloc_bvarith_buffer());
}
/*
* Remove b from the list and free b
*/
static inline void _o_free_bvarith_buffer(bvarith_buffer_t *b) {
dl_list_t *elem;
elem = bvarith_buffer_header(b);
list_remove(elem);
safe_free(elem);
}
static void free_bvarith_buffer(bvarith_buffer_t *b) {
MT_PROTECT_VOID(bvarith_buffer_list_lock, _o_free_bvarith_buffer(b));
}
/*
* Clean up the arith buffer list: free all elements and empty the list
*/
static void free_bvarith_buffer_list(void) {
dl_list_t *elem, *aux;
elem = bvarith_buffer_list.next;
while (elem != &bvarith_buffer_list) {
aux = elem->next;
delete_bvarith_buffer(bvarith_buffer(elem));
safe_free(elem);
elem = aux;
}
clear_list(&bvarith_buffer_list);
}
/*********************************
* BVARITH64 BUFFER ALLOCATION *
********************************/
/*
* Get header of buffer b, assuming b is embedded into an bvarith64_buffer_elem
*/
static inline dl_list_t *bvarith64_buffer_header(bvarith64_buffer_t *b) {
return (dl_list_t *)(((char *)b) - offsetof(bvarith64_buffer_elem_t, buffer));
}
/*
* Get buffer of header l
*/
static inline bvarith64_buffer_t *bvarith64_buffer(dl_list_t *l) {
return (bvarith64_buffer_t *)(((char *) l) + offsetof(bvarith64_buffer_elem_t, buffer));
}
/*
* Allocate a bv-arithmetic buffer and insert it into the list
*/
static inline bvarith64_buffer_t *_o_alloc_bvarith64_buffer(void) {
bvarith64_buffer_elem_t *new_elem;
new_elem = (bvarith64_buffer_elem_t *) safe_malloc(sizeof(bvarith64_buffer_elem_t));
list_insert_next(&bvarith64_buffer_list, &new_elem->header);
return &new_elem->buffer;
}
static bvarith64_buffer_t *alloc_bvarith64_buffer(void) {
MT_PROTECT(bvarith64_buffer_t *, bvarith64_buffer_list_lock, _o_alloc_bvarith64_buffer());
}
/*
* Remove b from the list and free b
*/
static inline void _o_free_bvarith64_buffer(bvarith64_buffer_t *b) {
dl_list_t *elem;
elem = bvarith64_buffer_header(b);
list_remove(elem);
safe_free(elem);
}
static void free_bvarith64_buffer(bvarith64_buffer_t *b) {
MT_PROTECT_VOID(bvarith64_buffer_list_lock, _o_free_bvarith64_buffer(b));
}
/*
* Clean up the buffer list: free all elements and empty the list
*/
static void free_bvarith64_buffer_list(void) {
dl_list_t *elem, *aux;
elem = bvarith64_buffer_list.next;
while (elem != &bvarith64_buffer_list) {
aux = elem->next;
delete_bvarith64_buffer(bvarith64_buffer(elem));
safe_free(elem);
elem = aux;
}
clear_list(&bvarith64_buffer_list);
}
/*****************************
* LOGIC BUFFER ALLOCATION *
****************************/
/*
* Get header of buffer b, assuming b is embedded into an bvlogic_buffer_elem
*/
static inline dl_list_t *bvlogic_buffer_header(bvlogic_buffer_t *b) {
return (dl_list_t *)(((char *)b) - offsetof(bvlogic_buffer_elem_t, buffer));
}
/*
* Get buffer of header l
*/
static inline bvlogic_buffer_t *bvlogic_buffer(dl_list_t *l) {
return (bvlogic_buffer_t *)(((char *) l) + offsetof(bvlogic_buffer_elem_t, buffer));
}
/*
* Allocate an arithmetic buffer and insert it into the list
*/
static inline bvlogic_buffer_t *_o_alloc_bvlogic_buffer(void) {
bvlogic_buffer_elem_t *new_elem;
new_elem = (bvlogic_buffer_elem_t *) safe_malloc(sizeof(bvlogic_buffer_elem_t));
list_insert_next(&bvlogic_buffer_list, &new_elem->header);
return &new_elem->buffer;
}
static bvlogic_buffer_t *alloc_bvlogic_buffer(void) {
MT_PROTECT(bvlogic_buffer_t *, bvlogic_buffer_list_lock, _o_alloc_bvlogic_buffer());
}
/*
* Remove b from the list and free b
*/
static inline void _o_free_bvlogic_buffer(bvlogic_buffer_t *b) {
dl_list_t *elem;
elem = bvlogic_buffer_header(b);
list_remove(elem);
safe_free(elem);
}
static void free_bvlogic_buffer(bvlogic_buffer_t *b) {
MT_PROTECT_VOID(bvlogic_buffer_list_lock, _o_free_bvlogic_buffer(b));
}
/*
* Clean up the arith buffer list: free all elements and empty the list
*/
static void free_bvlogic_buffer_list(void) {
dl_list_t *elem, *aux;
elem = bvlogic_buffer_list.next;
while (elem != &bvlogic_buffer_list) {
aux = elem->next;
delete_bvlogic_buffer(bvlogic_buffer(elem));
safe_free(elem);
elem = aux;
}
clear_list(&bvlogic_buffer_list);
}
/*************************
* CONTEXT ALLOCATION *
************************/
/*
* Get the header of a context c, assuming c is embedded in a context_elem
*/
static inline dl_list_t *header_of_context(context_t *c) {
return (dl_list_t *)(((char *) c) - offsetof(context_elem_t, context));
}
/*
* Get the context of header l
*/
static inline context_t *context_of_header(dl_list_t *l) {
return (context_t *) (((char *) l) + offsetof(context_elem_t, context));
}
/*
* Allocate a fresh context object and insert it in the context_list
* - WARNING: the context is not initialized
*/
static inline context_t *_o_alloc_context(void) {
context_elem_t *new_elem;
new_elem = (context_elem_t *) safe_malloc(sizeof(context_elem_t));
list_insert_next(&context_list, &new_elem->header);
return &new_elem->context;
}
static context_t *alloc_context(void) {
MT_PROTECT(context_t *, context_list_lock, _o_alloc_context());
}
/*
* Remove c from the list and free c
* - WARNING: make sure to call delete_context(c) before this
* function
*/
static inline void _o_free_context(context_t *c) {
dl_list_t *elem;
elem = header_of_context(c);
list_remove(elem);
safe_free(elem);
}
static void free_context(context_t *c) {
MT_PROTECT_VOID(context_list_lock, _o_free_context(c));
}
/*
* Cleanup the context list
*/
static void free_context_list(void) {
dl_list_t *elem, *aux;
elem = context_list.next;
while (elem != &context_list) {
aux = elem->next;
delete_context(context_of_header(elem));
safe_free(elem);
elem = aux;
}
clear_list(&context_list);
}
/***********************
* MODEL ALLOCATION *
**********************/
/*
* Get the header of a context m, assuming m is embedded in a model_elem
*/
static inline dl_list_t *header_of_model(model_t *m) {
return (dl_list_t *)(((char *) m) - offsetof(model_elem_t, model));
}
/*
* Get the model of header l
*/
static inline model_t *model_of_header(dl_list_t *l) {
return (model_t *) (((char *) l) + offsetof(model_elem_t, model));
}
/*
* Allocate a fresh model object and insert it in the model_list
* - WARNING: the model is not initialized
*/
static inline model_t *_o_alloc_model(void) {
model_elem_t *new_elem;
new_elem = (model_elem_t *) safe_malloc(sizeof(model_elem_t));
list_insert_next(&model_list, &new_elem->header);
return &new_elem->model;
}
static model_t *alloc_model(void) {
MT_PROTECT(model_t *, model_list_lock, _o_alloc_model());
}
/*
* Remove c from the list and free m
* - WARNING: make sure to call delete_model(c) before this
* function
*/
static inline void _o_free_model(model_t *m) {
dl_list_t *elem;
elem = header_of_model(m);
list_remove(elem);
safe_free(elem);
}
static inline void free_model(model_t *m) {
MT_PROTECT_VOID(model_list_lock, _o_free_model(m));
}
/*
* Cleanup the model list
*/
static void free_model_list(void) {
dl_list_t *elem, *aux;
elem = model_list.next;
while (elem != &model_list) {
aux = elem->next;
delete_model(model_of_header(elem));
safe_free(elem);
elem = aux;
}
clear_list(&model_list);
}
/********************************************
* CONFIG AND SEARCH PARAMETER STRUCTURES *
*******************************************/
/*
* Get the header
*/
static inline dl_list_t *header_of_config_structure(ctx_config_t *c) {
return (dl_list_t *) (((char *) c) - offsetof(ctx_config_elem_t, config));
}
static inline dl_list_t *header_of_param_structure(param_t *p) {
return (dl_list_t *) (((char *) p) - offsetof(param_structure_elem_t, param));
}
/*
* Config from a header l
*/
static inline ctx_config_t *config_of_header(dl_list_t *l) {
return (ctx_config_t *) (((char *) l) + offsetof(ctx_config_elem_t, config));
}
/*
* Allocate a structure and insert it into the generic
* WARNING: the record is not initialized
*/
static inline ctx_config_t *_o_alloc_config_structure(void) {
ctx_config_elem_t *new_elem;
new_elem = (ctx_config_elem_t *) safe_malloc(sizeof(ctx_config_elem_t));
list_insert_next(&config_list, &new_elem->header);
return &new_elem->config;
}
static ctx_config_t *alloc_config_structure(void) {
MT_PROTECT(ctx_config_t *, config_list_lock, _o_alloc_config_structure());
}
static inline param_t *_o_alloc_param_structure(void) {
param_structure_elem_t *new_elem;
new_elem = (param_structure_elem_t *) safe_malloc(sizeof(param_structure_elem_t));
list_insert_next(¶meter_list, &new_elem->header);
return &new_elem->param;
}
static param_t *alloc_param_structure(void) {
MT_PROTECT(param_t *, parameter_list_lock, _o_alloc_param_structure());
}
/*
* Remove a structure form the generic list
*/
static inline void _o_free_config_structure(ctx_config_t *c) {
dl_list_t *elem;
elem = header_of_config_structure(c);
list_remove(elem);
safe_free(elem);
}
static void free_config_structure(ctx_config_t *c) {
MT_PROTECT_VOID(config_list_lock, _o_free_config_structure(c));
}
static inline void _o_free_param_structure(param_t *p) {
dl_list_t *elem;
elem = header_of_param_structure(p);
list_remove(elem);
safe_free(elem);
}
static void free_param_structure(param_t *p) {
MT_PROTECT_VOID(parameter_list_lock, _o_free_param_structure(p));
}
/*
* Empty the list of configs
*/
static void free_config_list(void) {
dl_list_t *elem, *aux;
elem = config_list.next;
while (elem != &config_list) {
aux = elem->next;
delete_config(config_of_header(elem));
safe_free(elem);
elem = aux;
}
clear_list(&config_list);
}
/*
* Empty the list of parameter descriptors
*/
static void free_parameter_list(void) {
dl_list_t *elem, *aux;
elem = parameter_list.next;
while (elem != ¶meter_list) {
aux = elem->next;
safe_free(elem);
elem = aux;
}
clear_list(¶meter_list);
}
/***********************************
* PARSER AND RELATED STRUCTURES *
**********************************/
/*
* Return the internal parser
* - initialize it to read from the given string
* - s must be non-NULL, terminated by '\0'
*/
static parser_t *get_parser(const char *s) {
if (__yices_globals.parser == NULL) {
assert(__yices_globals.lexer == NULL && __yices_globals.tstack == NULL);
__yices_globals.tstack = (tstack_t *) safe_malloc(sizeof(tstack_t));
init_tstack(__yices_globals.tstack, NUM_BASE_OPCODES);
__yices_globals.lexer = (lexer_t *) safe_malloc(sizeof(lexer_t));
init_string_lexer(__yices_globals.lexer, s, "yices");
__yices_globals.parser = (parser_t *) safe_malloc(sizeof(parser_t));
init_parser(__yices_globals.parser, __yices_globals.lexer, __yices_globals.tstack);
} else {
// reset the input string
assert(__yices_globals.lexer != NULL && __yices_globals.tstack != NULL);
reset_string_lexer(__yices_globals.lexer, s);
}
return __yices_globals.parser;
}
/*
* Delete the internal parser, lexer, term stack
* (it they exist)
*/
static void delete_parsing_objects(void) {
if (__yices_globals.parser != NULL) {
assert(__yices_globals.lexer != NULL && __yices_globals.tstack != NULL);
delete_parser(__yices_globals.parser);
safe_free(__yices_globals.parser);
__yices_globals.parser = NULL;
close_lexer(__yices_globals.lexer);
safe_free(__yices_globals.lexer);
__yices_globals.lexer = NULL;
delete_tstack(__yices_globals.tstack);
safe_free(__yices_globals.tstack);
__yices_globals.tstack = NULL;
}
assert(__yices_globals.lexer == NULL && __yices_globals.tstack == NULL);
}
/************************
* VARIABLE COLLECTOR *
***********************/
/*
* Return the free variable collector
* - allocate and initialize it if necessary
*/
static fvar_collector_t *get_fvars(void) {
if (__yices_globals.fvars == NULL) {
__yices_globals.fvars = (fvar_collector_t *) safe_malloc(sizeof(fvar_collector_t));
init_fvar_collector(__yices_globals.fvars, __yices_globals.terms);
}
return __yices_globals.fvars;
}
/*
* Delete the free variable collector if it exists
*/
static void delete_fvars(void) {
if (__yices_globals.fvars != NULL) {
delete_fvar_collector(__yices_globals.fvars);
safe_free(__yices_globals.fvars);
__yices_globals.fvars = NULL;
}
}
/***************************************
* GLOBAL INITIALIZATION AND CLEANUP *
**************************************/
/*
* Initialize the table of global objects
*/