1 /******************************************
2 Copyright (c) 2016, @Storyyeller
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
21 ***********************************************/
22
23 #include "cryptominisat5/cryptominisat_c.h"
24 #include "constants.h"
25 #include "cryptominisat5/cryptominisat.h"
26
27
28 // C wrappers for SATSolver so that it can be used from other languages (e.g. Rust)
29 using namespace CMSat;
30
31 // Make sure the types we expose are C compatible and don't change unexpectedly
32 static_assert(sizeof(Lit) == sizeof(c_Lit), "Lit layout not c-compatible");
33 static_assert(alignof(Lit) == alignof(c_Lit), "Lit layout not c-compatible");
34 static_assert(sizeof(lbool) == sizeof(c_lbool), "lbool layout not c-compatible");
35 static_assert(alignof(lbool) == alignof(c_lbool), "lbool layout not c-compatible");
36
fromc(const c_Lit * x)37 const Lit* fromc(const c_Lit* x)
38 {
39 return reinterpret_cast<const Lit*>(x);
40 }
fromc(const c_lbool * x)41 const lbool* fromc(const c_lbool* x)
42 {
43 return reinterpret_cast<const lbool*>(x);
44 }
toc(const lbool * x)45 const c_lbool* toc(const lbool* x)
46 {
47 return reinterpret_cast<const c_lbool*>(x);
48 }
toc(const Lit * x)49 const c_Lit* toc(const Lit* x)
50 {
51 return reinterpret_cast<const c_Lit*>(x);
52 }
toc(lbool x)53 c_lbool toc(lbool x)
54 {
55 return {x.getValue()};
56 }
57
58 template<typename T>
wrap(const T * vals,size_t num_vals)59 std::vector<T> wrap(const T* vals, size_t num_vals)
60 {
61 return std::vector<T>(vals, vals + num_vals);
62 }
63
64 template<typename Dest, typename T>
65 Dest unwrap(const std::vector<T>& vec)
66 {
67 return Dest {toc(vec.data()), vec.size()};
68 }
69
70 #define NOEXCEPT_START noexcept { try {
71 #define NOEXCEPT_END } catch(...) { \
72 std::cerr << "ERROR: exception thrown past FFI boundary" << std::endl;\
73 std::exit(-1);\
74 } }
75
76 extern "C"
77 {
78
cmsat_new(void)79 DLL_PUBLIC SATSolver* cmsat_new(void) NOEXCEPT_START {
80 return new SATSolver();
81 } NOEXCEPT_END
82
cmsat_free(SATSolver * s)83 DLL_PUBLIC void cmsat_free(SATSolver* s) NOEXCEPT_START {
84 delete s;
85 } NOEXCEPT_END
86
cmsat_nvars(const SATSolver * self)87 DLL_PUBLIC unsigned cmsat_nvars(const SATSolver* self) NOEXCEPT_START {
88 return self->nVars();
89 } NOEXCEPT_END
90
cmsat_add_clause(SATSolver * self,const c_Lit * lits,size_t num_lits)91 DLL_PUBLIC bool cmsat_add_clause(SATSolver* self, const c_Lit* lits, size_t num_lits) NOEXCEPT_START {
92 return self->add_clause(wrap(fromc(lits), num_lits));
93 } NOEXCEPT_END
94
cmsat_add_xor_clause(SATSolver * self,const unsigned * vars,size_t num_vars,bool rhs)95 DLL_PUBLIC bool cmsat_add_xor_clause(SATSolver* self, const unsigned* vars, size_t num_vars, bool rhs) NOEXCEPT_START {
96 return self->add_xor_clause(wrap(vars, num_vars), rhs);
97 } NOEXCEPT_END
98
cmsat_new_vars(SATSolver * self,const size_t n)99 DLL_PUBLIC void cmsat_new_vars(SATSolver* self, const size_t n) NOEXCEPT_START {
100 self->new_vars(n);
101 } NOEXCEPT_END
102
cmsat_solve(SATSolver * self)103 DLL_PUBLIC c_lbool cmsat_solve(SATSolver* self) NOEXCEPT_START {
104 return toc(self->solve(nullptr));
105 } NOEXCEPT_END
106
cmsat_solve_with_assumptions(SATSolver * self,const c_Lit * assumptions,size_t num_assumptions)107 DLL_PUBLIC c_lbool cmsat_solve_with_assumptions(SATSolver* self, const c_Lit* assumptions, size_t num_assumptions) NOEXCEPT_START {
108 auto temp = wrap(fromc(assumptions), num_assumptions);
109 return toc(self->solve(&temp));
110 } NOEXCEPT_END
111
cmsat_get_model(const SATSolver * self)112 DLL_PUBLIC slice_lbool cmsat_get_model(const SATSolver* self) NOEXCEPT_START {
113 return unwrap<slice_lbool>(self->get_model());
114 } NOEXCEPT_END
115
cmsat_get_conflict(const SATSolver * self)116 DLL_PUBLIC slice_Lit cmsat_get_conflict(const SATSolver* self) NOEXCEPT_START {
117 return unwrap<slice_Lit>(self->get_conflict());
118 } NOEXCEPT_END
119
cmsat_print_stats(const SATSolver * self)120 DLL_PUBLIC void cmsat_print_stats(const SATSolver* self) NOEXCEPT_START {
121 self->print_stats();
122 } NOEXCEPT_END
123
124 //Setup
cmsat_set_num_threads(SATSolver * self,unsigned n)125 DLL_PUBLIC void cmsat_set_num_threads(SATSolver* self, unsigned n) NOEXCEPT_START {
126 self->set_num_threads(n);
127 } NOEXCEPT_END
128
cmsat_set_verbosity(SATSolver * self,unsigned n)129 DLL_PUBLIC void cmsat_set_verbosity(SATSolver* self, unsigned n) NOEXCEPT_START {
130 self->set_verbosity(n);
131 } NOEXCEPT_END
132
cmsat_set_default_polarity(SATSolver * self,int polarity)133 DLL_PUBLIC void cmsat_set_default_polarity(SATSolver* self, int polarity) NOEXCEPT_START {
134 self->set_default_polarity(polarity);
135 } NOEXCEPT_END
136
cmsat_set_no_simplify(SATSolver * self)137 DLL_PUBLIC void cmsat_set_no_simplify(SATSolver* self) NOEXCEPT_START {
138 self->set_no_simplify();
139 } NOEXCEPT_END
140
cmsat_set_no_simplify_at_startup(SATSolver * self)141 DLL_PUBLIC void cmsat_set_no_simplify_at_startup(SATSolver* self) NOEXCEPT_START {
142 self->set_no_simplify_at_startup();
143 } NOEXCEPT_END
144
cmsat_set_no_equivalent_lit_replacement(SATSolver * self)145 DLL_PUBLIC void cmsat_set_no_equivalent_lit_replacement(SATSolver* self) NOEXCEPT_START {
146 self->set_no_equivalent_lit_replacement();
147 } NOEXCEPT_END
148
cmsat_set_no_bva(SATSolver * self)149 DLL_PUBLIC void cmsat_set_no_bva(SATSolver* self) NOEXCEPT_START {
150 self->set_no_bva();
151 } NOEXCEPT_END
152
cmsat_set_no_bve(SATSolver * self)153 DLL_PUBLIC void cmsat_set_no_bve(SATSolver* self) NOEXCEPT_START {
154 self->set_no_bve();
155 } NOEXCEPT_END
156
cmsat_set_yes_comphandler(SATSolver * self)157 DLL_PUBLIC void cmsat_set_yes_comphandler(SATSolver* self) NOEXCEPT_START {
158 self->set_yes_comphandler();
159 } NOEXCEPT_END
160
cmsat_simplify(SATSolver * self,const c_Lit * assumptions,size_t num_assumptions)161 DLL_PUBLIC c_lbool cmsat_simplify(SATSolver* self, const c_Lit* assumptions, size_t num_assumptions) NOEXCEPT_START {
162 auto temp = wrap(fromc(assumptions), num_assumptions);
163 return toc(self->simplify(&temp));
164 } NOEXCEPT_END
165
cmsat_set_max_time(SATSolver * self,double max_time)166 DLL_PUBLIC void cmsat_set_max_time(SATSolver* self, double max_time) NOEXCEPT_START {
167 self->set_max_time(max_time);
168 } NOEXCEPT_END
169 }
170