-
Notifications
You must be signed in to change notification settings - Fork 3
/
cxx_stubs.cpp
1747 lines (1555 loc) · 46 KB
/
cxx_stubs.cpp
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
/**************************************************************************/
/* Bitwuzla: Satisfiability Modulo Theories (SMT) solver. */
/* */
/* Copyright (C) 2023 by the authors listed in the AUTHORS file at */
/* https://github.com/bitwuzla/bitwuzla/blob/main/AUTHORS */
/* */
/* This file is part of Bitwuzla under the MIT license. */
/* See COPYING for more information at */
/* https://github.com/bitwuzla/bitwuzla/blob/main/COPYING */
/**************************************************************************/
#include <string.h>
#include <caml/mlvalues.h>
#include <caml/alloc.h>
#include <caml/memory.h>
#include <caml/fail.h>
#include <caml/callback.h>
#include <caml/custom.h>
typedef value mlvalue;
#include <bitwuzla/cpp/bitwuzla.h>
static struct custom_operations exception_operations =
{
"dummy operations after exception",
custom_finalize_default,
custom_compare_default,
custom_hash_default,
custom_serialize_default,
custom_deserialize_default,
custom_compare_ext_default,
custom_fixed_length_default
};
extern "C" CAMLprim value
ocaml_bitwuzla_cxx_copyright ()
{
return caml_copy_string(bitwuzla::copyright());
}
extern "C" CAMLprim value
ocaml_bitwuzla_cxx_version ()
{
return caml_copy_string(bitwuzla::version());
}
static const value *vpp_open_vbox = NULL;
static const value *vpp_print_string = NULL;
static const value *vpp_print_char = NULL;
static const value *vpp_print_space = NULL;
static const value *vpp_close_box = NULL;
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_init_format (void)
{
vpp_open_vbox = caml_named_value("Format.pp_open_vbox");
vpp_print_string = caml_named_value("Format.pp_print_string");
vpp_print_char = caml_named_value("Format.pp_print_char");
vpp_print_space = caml_named_value("Format.pp_print_space");
vpp_close_box = caml_named_value("Format.pp_close_box");
return Val_unit;
}
class Format : public std::streambuf
{
mlvalue *vf;
public:
Format(mlvalue *_vf) : vf(_vf) {};
virtual std::streamsize xsputn(const char* s, std::streamsize n) override;
virtual int overflow(int c) override;
};
std::streamsize Format::xsputn(const char *s, std::streamsize n)
{
const char *p = s, *end = s + n;
while (p != end) {
const char *b = strchr(p, '\n');
if (b == NULL) {
mlvalue vs = caml_alloc_initialized_string(end - p, p);
caml_callback2(*vpp_print_string, *vf, vs);
break;
} else {
mlvalue vs = caml_alloc_initialized_string(b - p, p);
caml_callback2(*vpp_print_string, *vf, vs);
caml_callback2(*vpp_print_space, *vf, Val_unit);
p = b + 1;
}
}
return n;
}
int Format::overflow (int c)
{
char ch = traits_type::to_char_type(c);
if (ch == '\n') {
caml_callback2(*vpp_print_space, *vf, Val_unit);
} else {
caml_callback2(*vpp_print_char, *vf, Val_int(c));
}
return c;
}
#define BITWUZLA_TRY_CATCH_BEGIN \
try \
{
#define BITWUZLA_TRY_CATCH_END \
} \
catch (bitwuzla::Exception &e) { caml_invalid_argument(e.msg().c_str()); }
#define BITWUZLA_TRY_CATCH_END_CUSTOM_ALLOC(custom) \
} \
catch (bitwuzla::Exception &e) { \
if(custom != Val_unit) \
Custom_ops_val(custom) = &exception_operations; \
caml_invalid_argument(e.msg().c_str()); \
}
#define Manager_val(v) (*(bitwuzla::TermManager **)Data_custom_val(v))
static void manager_delete (value vt)
{
delete Manager_val(vt);
}
static struct custom_operations manager_operations =
{
"https://bitwuzla.github.io",
manager_delete,
custom_compare_default,
custom_hash_default,
custom_serialize_default,
custom_deserialize_default,
custom_compare_ext_default,
custom_fixed_length_default
};
extern "C" CAMLprim value
ocaml_bitwuzla_cxx_manager_new ()
{
bitwuzla::TermManager *t = new bitwuzla::TermManager();
value vt = caml_alloc_custom(&manager_operations,
sizeof(bitwuzla::TermManager *), 0, 1);
Manager_val(vt) = t;
return vt;
}
class Options : public bitwuzla::Options
{
public:
Options() : bitwuzla::Options() {}
~Options() {}
void * operator new (size_t size,
struct custom_operations * operations,
mlvalue *custom)
{
*custom = caml_alloc_custom(operations, size, 0, 1);
return Data_custom_val(*custom);
}
void operator delete (void *ptr) {}
};
#define Options_val(v) ((Options*)Data_custom_val(v))
static void options_delete (mlvalue vt)
{
delete Options_val(vt);
}
static struct custom_operations options_operations =
{
"https://bitwuzla.github.io",
&options_delete,
custom_compare_default,
custom_hash_default,
custom_serialize_default,
custom_deserialize_default,
custom_compare_ext_default,
custom_fixed_length_default
};
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_options_new ()
{
mlvalue custom = Val_unit;
BITWUZLA_TRY_CATCH_BEGIN;
new(&options_operations, &custom) Options();
return custom;
BITWUZLA_TRY_CATCH_END_CUSTOM_ALLOC(custom);
}
extern "C" CAMLprim mlvalue
native_bitwuzla_cxx_options_set_numeric (mlvalue vt, intnat k, intnat v)
{
BITWUZLA_TRY_CATCH_BEGIN;
Options_val(vt)->set((bitwuzla::Option)k, v);
BITWUZLA_TRY_CATCH_END;
return Val_unit;
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_options_set_numeric (mlvalue vt, mlvalue vk, mlvalue vv)
{
return native_bitwuzla_cxx_options_set_numeric(vt, Long_val(vk),
Long_val(vv));
}
extern "C" CAMLprim mlvalue
native_bitwuzla_cxx_options_set_mode (mlvalue vt, intnat k, mlvalue vv)
{
Options_val(vt)->set((bitwuzla::Option)k, String_val(vv));
return Val_unit;
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_options_set_mode (mlvalue vt, mlvalue vk, mlvalue vv)
{
return native_bitwuzla_cxx_options_set_mode(vt, Long_val(vk), vv);
}
extern "C" CAMLprim intnat
native_bitwuzla_cxx_options_get_numeric (mlvalue vt, intnat k)
{
return Options_val(vt)->get((bitwuzla::Option)k);
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_options_get_numeric (mlvalue vt, mlvalue vk)
{
return Val_long(native_bitwuzla_cxx_options_get_numeric(vt, Long_val(vk)));
}
extern "C" CAMLprim mlvalue
native_bitwuzla_cxx_options_get_mode (mlvalue vt, intnat k)
{
Options *t = Options_val(vt);
return caml_copy_string(t->get_mode((bitwuzla::Option)k).c_str());
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_options_get_mode (mlvalue vt, mlvalue vk)
{
return native_bitwuzla_cxx_options_get_mode(vt, Long_val(vk));
}
class Sort : public bitwuzla::Sort
{
public:
Sort(bitwuzla::Sort t) : bitwuzla::Sort(t) {}
~Sort() {}
void * operator new (size_t size,
struct custom_operations * operations,
mlvalue *custom)
{
*custom = caml_alloc_custom(operations, size, 0, 1);
return Data_custom_val(*custom);
}
void operator delete (void *ptr) {}
};
#define Sort_val(v) ((Sort*)Data_custom_val(v))
static void sort_delete (mlvalue vt)
{
delete Sort_val(vt);
}
extern "C" CAMLprim int native_bitwuzla_cxx_sort_compare (mlvalue v1, mlvalue v2)
{
uint64_t i1 = Sort_val(v1)->id(), i2 = Sort_val(v2)->id();
return (i1 >= i2) - (i1 <= i2);
}
extern "C" CAMLprim intnat native_bitwuzla_cxx_sort_hash (mlvalue vt)
{
return std::hash<bitwuzla::Sort>{}(*Sort_val(vt));
}
static struct custom_operations sort_operations =
{
"https://bitwuzla.github.io",
&sort_delete,
&native_bitwuzla_cxx_sort_compare,
&native_bitwuzla_cxx_sort_hash,
custom_serialize_default,
custom_deserialize_default,
custom_compare_ext_default,
custom_fixed_length_default
};
extern "C" CAMLprim uint64_t
native_bitwuzla_cxx_sort_id (mlvalue vt)
{
return Sort_val(vt)->id();
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_sort_id (mlvalue vt)
{
return caml_copy_int64(native_bitwuzla_cxx_sort_id(vt));
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_sort_compare (mlvalue v1, mlvalue v2)
{
return Val_long(native_bitwuzla_cxx_sort_compare(v1, v2));
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_sort_equal (mlvalue v1, mlvalue v2)
{
return Val_int(*Sort_val(v1) == *Sort_val(v2));
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_sort_hash (mlvalue vt)
{
return Val_long(native_bitwuzla_cxx_sort_hash(vt));
}
extern "C" CAMLprim intnat
native_bitwuzla_cxx_sort_bv_size (mlvalue vt)
{
BITWUZLA_TRY_CATCH_BEGIN;
return Sort_val(vt)->bv_size();
BITWUZLA_TRY_CATCH_END;
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_sort_bv_size (mlvalue vt)
{
return Val_long(native_bitwuzla_cxx_sort_bv_size(vt));
}
extern "C" CAMLprim intnat
native_bitwuzla_cxx_sort_fp_exp_size (mlvalue vt)
{
BITWUZLA_TRY_CATCH_BEGIN;
return Sort_val(vt)->fp_exp_size();
BITWUZLA_TRY_CATCH_END;
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_sort_fp_exp_size (mlvalue vt)
{
return Val_long(native_bitwuzla_cxx_sort_fp_exp_size(vt));
}
extern "C" CAMLprim intnat
native_bitwuzla_cxx_sort_fp_sig_size (mlvalue vt)
{
BITWUZLA_TRY_CATCH_BEGIN;
return Sort_val(vt)->fp_sig_size();
BITWUZLA_TRY_CATCH_END;
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_sort_fp_sig_size (mlvalue vt)
{
return Val_long(native_bitwuzla_cxx_sort_fp_sig_size(vt));
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_sort_array_index (mlvalue vt)
{
mlvalue custom = Val_unit;
BITWUZLA_TRY_CATCH_BEGIN;
new(&sort_operations, &custom) Sort(Sort_val(vt)->array_index());
return custom;
BITWUZLA_TRY_CATCH_END_CUSTOM_ALLOC(custom);
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_sort_array_element (mlvalue vt)
{
mlvalue custom = Val_unit;
BITWUZLA_TRY_CATCH_BEGIN;
new(&sort_operations, &custom) Sort(Sort_val(vt)->array_element());
return custom;
BITWUZLA_TRY_CATCH_END_CUSTOM_ALLOC(custom);
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_sort_fun_domain (mlvalue vt)
{
std::vector<bitwuzla::Sort> domain;
BITWUZLA_TRY_CATCH_BEGIN;
domain = Sort_val(vt)->fun_domain();
BITWUZLA_TRY_CATCH_END;
CAMLparam0();
CAMLlocal1(result);
size_t n = domain.size();
result = caml_alloc(n, 0);
for (size_t i = 0; i < n; i += 1) {
mlvalue custom = Val_unit;
BITWUZLA_TRY_CATCH_BEGIN;
new(&sort_operations, &custom) Sort(domain[i]);
BITWUZLA_TRY_CATCH_END_CUSTOM_ALLOC(custom);
caml_modify(&Field(result, i), custom);
}
CAMLreturn(result);
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_sort_fun_codomain (mlvalue vt)
{
mlvalue custom = Val_unit;
BITWUZLA_TRY_CATCH_BEGIN;
new(&sort_operations, &custom) Sort(Sort_val(vt)->fun_codomain());
return custom;
BITWUZLA_TRY_CATCH_END_CUSTOM_ALLOC(custom);
}
extern "C" CAMLprim intnat
native_bitwuzla_cxx_sort_fun_arity (mlvalue vt)
{
BITWUZLA_TRY_CATCH_BEGIN;
return Sort_val(vt)->fun_arity();
BITWUZLA_TRY_CATCH_END;
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_sort_fun_arity (mlvalue vt)
{
return Val_long(native_bitwuzla_cxx_sort_fun_arity(vt));
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_sort_uninterpreted_symbol (mlvalue vt)
{
std::optional<std::string> symbol;
BITWUZLA_TRY_CATCH_BEGIN;
symbol = Sort_val(vt)->uninterpreted_symbol();
BITWUZLA_TRY_CATCH_END;
if (symbol.has_value()) {
return caml_copy_string(symbol->c_str());
} else {
caml_raise_not_found();
__builtin_unreachable();
}
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_sort_is_array (mlvalue vt)
{
return Val_bool(Sort_val(vt)->is_array());
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_sort_is_bool (mlvalue vt)
{
return Val_bool(Sort_val(vt)->is_bool());
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_sort_is_bv (mlvalue vt)
{
return Val_bool(Sort_val(vt)->is_bv());
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_sort_is_fp (mlvalue vt)
{
return Val_bool(Sort_val(vt)->is_fp());
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_sort_is_fun (mlvalue vt)
{
return Val_bool(Sort_val(vt)->is_fun());
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_sort_is_rm (mlvalue vt)
{
return Val_bool(Sort_val(vt)->is_rm());
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_sort_is_uninterpreted (mlvalue vt)
{
return Val_bool(Sort_val(vt)->is_uninterpreted());
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_sort_to_string (mlvalue vt)
{
return caml_copy_string(Sort_val(vt)->str().c_str());
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_sort_pp (mlvalue vf, mlvalue vt)
{
CAMLparam1(vf);
Format format(&vf);
std::ostream formatter(&format);
caml_callback2(*vpp_open_vbox, vf, Val_int(0));
formatter << *Sort_val(vt);
caml_callback2(*vpp_close_box, vf, Val_unit);
CAMLreturn(Val_unit);
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_mk_array_sort (mlvalue vm, mlvalue vi, mlvalue ve)
{
CAMLparam2(vi, ve);
mlvalue custom = Val_unit;
BITWUZLA_TRY_CATCH_BEGIN;
new(&sort_operations, &custom)
Sort(Manager_val(vm)->mk_array_sort(*Sort_val(vi), *Sort_val(ve)));
BITWUZLA_TRY_CATCH_END_CUSTOM_ALLOC(custom);
CAMLreturn(custom);
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_mk_bool_sort (mlvalue vm)
{
mlvalue custom = Val_unit;
BITWUZLA_TRY_CATCH_BEGIN;
new(&sort_operations, &custom) Sort(Manager_val(vm)->mk_bool_sort());
BITWUZLA_TRY_CATCH_END_CUSTOM_ALLOC(custom);
return custom;
}
extern "C" CAMLprim mlvalue
native_bitwuzla_cxx_mk_bv_sort (mlvalue vm, intnat size)
{
mlvalue custom = Val_unit;
BITWUZLA_TRY_CATCH_BEGIN;
new(&sort_operations, &custom) Sort(Manager_val(vm)->mk_bv_sort(size));
BITWUZLA_TRY_CATCH_END_CUSTOM_ALLOC(custom);
return custom;
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_mk_bv_sort (mlvalue vm, mlvalue vsize)
{
return native_bitwuzla_cxx_mk_bv_sort(vm, Long_val(vsize));
}
extern "C" CAMLprim mlvalue
native_bitwuzla_cxx_mk_fp_sort (mlvalue vm, intnat exp_size, intnat sig_size)
{
mlvalue custom = Val_unit;
BITWUZLA_TRY_CATCH_BEGIN;
new(&sort_operations, &custom)
Sort(Manager_val(vm)->mk_fp_sort(exp_size, sig_size));
BITWUZLA_TRY_CATCH_END_CUSTOM_ALLOC(custom);
return custom;
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_mk_fp_sort (mlvalue vm, mlvalue vexp_size, mlvalue vsig_size)
{
return native_bitwuzla_cxx_mk_fp_sort(vm, Long_val(vexp_size),
Long_val(vsig_size));
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_mk_fun_sort (mlvalue vm, mlvalue vdomain, mlvalue vcodomain)
{
mlvalue custom = Val_unit;
BITWUZLA_TRY_CATCH_BEGIN;
CAMLparam1(vcodomain);
size_t arity = Wosize_val(vdomain);
std::vector<bitwuzla::Sort> domain;
domain.reserve(arity);
for (size_t i = 0; i < arity; i += 1)
domain.emplace_back(*Sort_val(Field(vdomain, i)));
new(&sort_operations, &custom)
Sort(Manager_val(vm)->mk_fun_sort(domain, *Sort_val(vcodomain)));
CAMLreturn(custom);
BITWUZLA_TRY_CATCH_END_CUSTOM_ALLOC(custom);
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_mk_rm_sort (mlvalue vm)
{
mlvalue custom = Val_unit;
BITWUZLA_TRY_CATCH_BEGIN;
new(&sort_operations, &custom) Sort(Manager_val(vm)->mk_rm_sort());
return custom;
BITWUZLA_TRY_CATCH_END_CUSTOM_ALLOC(custom);
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_mk_uninterpreted_sort (mlvalue vm, mlvalue vopt)
{
CAMLparam1(vopt);
mlvalue custom = Val_unit;
std::optional<const std::string> symbol;
if (Is_some(vopt))
symbol.emplace(std::string(String_val(Field(vopt, 0))));
BITWUZLA_TRY_CATCH_BEGIN;
new(&sort_operations, &custom)
Sort(Manager_val(vm)->mk_uninterpreted_sort(symbol));
CAMLreturn(custom);
BITWUZLA_TRY_CATCH_END_CUSTOM_ALLOC(custom);
}
#define value mlvalue
class Term : public bitwuzla::Term
{
public:
Term(bitwuzla::Term t) : bitwuzla::Term(t) {}
~Term() {}
void * operator new (size_t size,
struct custom_operations * operations,
mlvalue *custom)
{
*custom = caml_alloc_custom(operations, size, 0, 1);
return Data_custom_val(*custom);
}
void operator delete (void *ptr) {}
};
#undef value
#define Term_val(v) ((Term*)Data_custom_val(v))
static void term_delete (mlvalue vt)
{
delete Term_val(vt);
}
extern "C" CAMLprim int native_bitwuzla_cxx_term_compare (mlvalue v1, mlvalue v2)
{
uint64_t i1 = Term_val(v1)->id(), i2 = Term_val(v2)->id();
return (i1 >= i2) - (i1 <= i2);
}
extern "C" CAMLprim intnat native_bitwuzla_cxx_term_hash (mlvalue vt)
{
return std::hash<bitwuzla::Term>{}(*Term_val(vt));
}
static struct custom_operations term_operations =
{
"https://bitwuzla.github.io",
&term_delete,
&native_bitwuzla_cxx_term_compare,
&native_bitwuzla_cxx_term_hash,
custom_serialize_default,
custom_deserialize_default,
custom_compare_ext_default,
custom_fixed_length_default
};
extern "C" CAMLprim uint64_t
native_bitwuzla_cxx_term_id (mlvalue vt)
{
return Term_val(vt)->id();
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_term_id (mlvalue vt)
{
return caml_copy_int64(native_bitwuzla_cxx_term_id(vt));
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_term_compare (mlvalue v1, mlvalue v2)
{
return Val_long(native_bitwuzla_cxx_term_compare(v1, v2));
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_term_equal (mlvalue v1, mlvalue v2)
{
return Val_int(*Term_val(v1) == *Term_val(v2));
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_term_hash (mlvalue vt)
{
return Val_long(native_bitwuzla_cxx_term_hash(vt));
}
extern "C" CAMLprim intnat
native_bitwuzla_cxx_term_kind (mlvalue vt)
{
return (intnat)Term_val(vt)->kind();
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_term_kind (mlvalue vt)
{
return Val_long(native_bitwuzla_cxx_term_kind(vt));
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_term_sort (mlvalue vt)
{
mlvalue custom = Val_unit;
BITWUZLA_TRY_CATCH_BEGIN;
new(&sort_operations, &custom) Sort(Term_val(vt)->sort());
return custom;
BITWUZLA_TRY_CATCH_END_CUSTOM_ALLOC(custom);
}
extern "C" CAMLprim intnat
native_bitwuzla_cxx_term_num_children (mlvalue vt)
{
return Term_val(vt)->num_children();
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_term_num_children (mlvalue vt)
{
return Val_long(native_bitwuzla_cxx_term_num_children(vt));
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_term_children (mlvalue vt)
{
std::vector<bitwuzla::Term> children = Term_val(vt)->children();
CAMLparam0();
CAMLlocal1(result);
size_t n = children.size();
result = caml_alloc(n, 0);
for (size_t i = 0; i < n; i += 1) {
mlvalue custom = Val_unit;
BITWUZLA_TRY_CATCH_BEGIN;
new(&term_operations, &custom) Term(children[i]);
BITWUZLA_TRY_CATCH_END_CUSTOM_ALLOC(custom);
caml_modify(&Field(result, i), custom);
}
CAMLreturn(result);
}
extern "C" CAMLprim mlvalue
native_bitwuzla_cxx_term_get (mlvalue vt, intnat i)
{
CAMLparam1(vt);
mlvalue custom = Val_unit;
BITWUZLA_TRY_CATCH_BEGIN;
new(&term_operations, &custom) Term((*Term_val(vt))[i]);
CAMLreturn(custom);
BITWUZLA_TRY_CATCH_END_CUSTOM_ALLOC(custom);
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_term_get (mlvalue vt, mlvalue vi)
{
return native_bitwuzla_cxx_term_get(vt, Long_val(vi));
}
extern "C" CAMLprim intnat
native_bitwuzla_cxx_term_num_indices (mlvalue vt)
{
return Term_val(vt)->num_indices();
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_term_num_indices (mlvalue vt)
{
return Val_long(native_bitwuzla_cxx_term_num_indices(vt));
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_term_indices (mlvalue vt)
{
std::vector<uint64_t> indices;
BITWUZLA_TRY_CATCH_BEGIN;
indices = Term_val(vt)->indices();
BITWUZLA_TRY_CATCH_END;
CAMLparam0();
CAMLlocal1(result);
size_t n = indices.size();
result = caml_alloc(n, 0);
for (size_t i = 0; i < n; i += 1) {
Field(result, i) = Val_long(indices[i]);
}
CAMLreturn(result);
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_term_symbol (mlvalue vt)
{
std::optional<std::reference_wrapper<const std::string>> symbol =
Term_val(vt)->symbol();
if (symbol.has_value()) {
return caml_copy_string(symbol->get().c_str());
} else {
caml_raise_not_found();
__builtin_unreachable();
}
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_term_is_const (mlvalue vt)
{
return Val_bool(Term_val(vt)->is_const());
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_term_is_variable (mlvalue vt)
{
return Val_bool(Term_val(vt)->is_variable());
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_term_is_value (mlvalue vt)
{
return Val_bool(Term_val(vt)->is_value());
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_term_is_bv_value_zero (mlvalue vt)
{
return Val_bool(Term_val(vt)->is_bv_value_zero());
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_term_is_bv_value_one (mlvalue vt)
{
return Val_bool(Term_val(vt)->is_bv_value_one());
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_term_is_bv_value_ones (mlvalue vt)
{
return Val_bool(Term_val(vt)->is_bv_value_ones());
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_term_is_bv_value_min_signed (mlvalue vt)
{
return Val_bool(Term_val(vt)->is_bv_value_min_signed());
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_term_is_bv_value_max_signed (mlvalue vt)
{
return Val_bool(Term_val(vt)->is_bv_value_max_signed());
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_term_is_fp_value_pos_zero (mlvalue vt)
{
return Val_bool(Term_val(vt)->is_fp_value_pos_zero());
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_term_is_fp_value_neg_zero (mlvalue vt)
{
return Val_bool(Term_val(vt)->is_fp_value_neg_zero());
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_term_is_fp_value_pos_inf (mlvalue vt)
{
return Val_bool(Term_val(vt)->is_fp_value_pos_inf());
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_term_is_fp_value_neg_inf (mlvalue vt)
{
return Val_bool(Term_val(vt)->is_fp_value_neg_inf());
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_term_is_fp_value_nan (mlvalue vt)
{
return Val_bool(Term_val(vt)->is_fp_value_nan());
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_term_is_rm_value_rna (mlvalue vt)
{
return Val_bool(Term_val(vt)->is_rm_value_rna());
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_term_is_rm_value_rne (mlvalue vt)
{
return Val_bool(Term_val(vt)->is_rm_value_rne());
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_term_is_rm_value_rtn (mlvalue vt)
{
return Val_bool(Term_val(vt)->is_rm_value_rtn());
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_term_is_rm_value_rtp (mlvalue vt)
{
return Val_bool(Term_val(vt)->is_rm_value_rtp());
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_term_is_rm_value_rtz (mlvalue vt)
{
return Val_bool(Term_val(vt)->is_rm_value_rtz());
}
extern "C" CAMLprim mlvalue
native_bitwuzla_cxx_term_to_string (intnat base, mlvalue vt)
{
return caml_copy_string(Term_val(vt)->str(base).c_str());
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_term_to_string (mlvalue vbase, mlvalue vt)
{
return native_bitwuzla_cxx_term_to_string(Long_val(vbase), vt);
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_term_pp (mlvalue vf, mlvalue vt)
{
CAMLparam1(vf);
Format format(&vf);
std::ostream formatter(&format);
caml_callback2(*vpp_open_vbox, vf, Val_int(0));
formatter << *Term_val(vt);
caml_callback2(*vpp_close_box, vf, Val_unit);
CAMLreturn(Val_unit);
}
extern "C" CAMLprim mlvalue
native_bitwuzla_cxx_term_pp_smt2 (intnat base, mlvalue vf, mlvalue vt)
{
CAMLparam1(vf);
Format format(&vf);
std::ostream formatter(&format);
caml_callback2(*vpp_open_vbox, vf, Val_int(0));
formatter << bitwuzla::set_bv_format(base) << *Term_val(vt);
caml_callback2(*vpp_close_box, vf, Val_unit);
CAMLreturn(Val_unit);
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_term_pp_smt2 (mlvalue vbase, mlvalue vf, mlvalue vt)
{
return native_bitwuzla_cxx_term_pp_smt2(Long_val(vbase), vf, vt);
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_term_boolean_value (mlvalue vt)
{
BITWUZLA_TRY_CATCH_BEGIN;
return Val_bool(Term_val(vt)->value<bool>());
BITWUZLA_TRY_CATCH_END;
}
extern "C" CAMLprim intnat
native_bitwuzla_cxx_term_rounding_mode_value (mlvalue vt)
{
BITWUZLA_TRY_CATCH_BEGIN;
return (intnat)Term_val(vt)->value<bitwuzla::RoundingMode>();
BITWUZLA_TRY_CATCH_END;
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_term_rounding_mode_value (mlvalue vt)
{
return Val_long(native_bitwuzla_cxx_term_rounding_mode_value(vt));
}
extern "C" CAMLprim mlvalue
native_bitwuzla_cxx_term_string_value (mlvalue vt, intnat base)
{
BITWUZLA_TRY_CATCH_BEGIN;
return caml_copy_string(Term_val(vt)->value<std::string>(base).c_str());
BITWUZLA_TRY_CATCH_END;
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_term_string_value (mlvalue vt, mlvalue vbase)
{
return native_bitwuzla_cxx_term_string_value(vt, Long_val(vbase));
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_term_ieee_754_value (mlvalue vt)
{
std::tuple<std::string, std::string, std::string> tuple;
BITWUZLA_TRY_CATCH_BEGIN;
tuple =
Term_val(vt)->value<std::tuple<std::string, std::string, std::string>>();
BITWUZLA_TRY_CATCH_END;
CAMLparam0();
CAMLlocal1(result);
result = caml_alloc(3, 0);
caml_modify(&Field(result, 0), caml_copy_string(std::get<0>(tuple).c_str()));
caml_modify(&Field(result, 1), caml_copy_string(std::get<1>(tuple).c_str()));
caml_modify(&Field(result, 2), caml_copy_string(std::get<2>(tuple).c_str()));
CAMLreturn(result);
}
extern "C" CAMLprim mlvalue
ocaml_bitwuzla_cxx_term_zarith_value (mlvalue vt)
{
BITWUZLA_TRY_CATCH_BEGIN;
return Term_val(vt)->value<value>();
BITWUZLA_TRY_CATCH_END;
}
extern "C" CAMLprim mlvalue