SMTStabilizer API
Public API documentation for SMTStabilizer
Loading...
Searching...
No Matches
c_api.cpp
Go to the documentation of this file.
1// Copyright (c) 2026 XiangZhang
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19// SOFTWARE.
20
21#include <cstdlib>
22#include <cstring>
23#include <exception>
24#include <memory>
25#include <new>
26#include <stdexcept>
27#include <string>
28#include <utility>
29
30#include "api/stabilizer_api.h"
32
36
39 std::string last_error;
40
41 explicit stabilizer_handle(const stabilizer_options *options)
42 : stabilizer(options ? options->value : stabilizer::api::SMTStabilizerOptions()) {}
43};
44
45namespace {
46
47template <typename Fn>
48stabilizer_status capture(stabilizer_handle *handle, char **output, Fn &&fn) {
49 if (output != nullptr) {
50 *output = nullptr;
51 }
52 if (handle == nullptr || output == nullptr) {
53 if (handle != nullptr) {
54 handle->last_error = "invalid argument";
55 }
57 }
58
59 try {
60 std::string result = fn();
61 char *buffer = static_cast<char *>(std::malloc(result.size() + 1));
62 if (buffer == nullptr) {
63 throw std::bad_alloc();
64 }
65 std::memcpy(buffer, result.c_str(), result.size() + 1);
66 *output = buffer;
67 handle->last_error.clear();
69 }
70 catch (const std::invalid_argument &e) {
71 handle->last_error = e.what();
73 }
74 catch (const std::exception &e) {
75 handle->last_error = e.what();
77 }
78 catch (...) {
79 handle->last_error = "unknown error";
81 }
82}
83
84stabilizer::api::SMTStabilizerOptions current_options(const stabilizer_handle *handle) {
85 return handle->stabilizer.options();
86}
87
88void apply_options(stabilizer_handle *handle, const stabilizer::api::SMTStabilizerOptions &options) {
89 handle->stabilizer.set_options(options);
90}
91
92} // namespace
93
97
99 delete options;
100}
101
103 if (options != nullptr) {
104 options->value.set_rewrite(value);
105 }
106}
107
109 return options != nullptr ? options->value.get_rewrite() : false;
110}
111
113 if (options != nullptr) {
114 options->value.set_context_propagation(value);
115 }
116}
117
119 return options != nullptr ? options->value.get_context_propagation() : false;
120}
121
123 if (options != nullptr) {
124 options->value.set_subgraph_pruning(value);
125 }
126}
127
129 return options != nullptr ? options->value.get_subgraph_pruning() : false;
130}
131
133 return new stabilizer_handle(options);
134}
135
137 delete handle;
138}
139
140void stabilizer_set_rewrite(stabilizer_handle *handle, bool value) {
141 if (handle == nullptr) {
142 return;
143 }
144 auto options = current_options(handle);
145 options.set_rewrite(value);
146 apply_options(handle, options);
147}
148
150 return handle != nullptr ? handle->stabilizer.options().get_rewrite() : false;
151}
152
154 if (handle == nullptr) {
155 return;
156 }
157 auto options = current_options(handle);
158 options.set_context_propagation(value);
159 apply_options(handle, options);
160}
161
163 return handle != nullptr ? handle->stabilizer.options().get_context_propagation() : false;
164}
165
167 if (handle == nullptr) {
168 return;
169 }
170 auto options = current_options(handle);
171 options.set_subgraph_pruning(value);
172 apply_options(handle, options);
173}
174
176 return handle != nullptr ? handle->stabilizer.options().get_subgraph_pruning() : false;
177}
178
179stabilizer_status stabilizer_apply_file(stabilizer_handle *handle, const char *file_path, char **output) {
180 if (handle == nullptr || file_path == nullptr || output == nullptr) {
181 if (handle != nullptr) {
182 handle->last_error = "invalid argument";
183 }
184 if (output != nullptr) {
185 *output = nullptr;
186 }
188 }
189 return capture(handle, output, [handle, file_path]() { return handle->stabilizer.apply_file(file_path); });
190}
191
192stabilizer_status stabilizer_apply_text(stabilizer_handle *handle, const char *smt2_text, char **output) {
193 if (handle == nullptr || smt2_text == nullptr || output == nullptr) {
194 if (handle != nullptr) {
195 handle->last_error = "invalid argument";
196 }
197 if (output != nullptr) {
198 *output = nullptr;
199 }
201 }
202 return capture(handle, output, [handle, smt2_text]() { return handle->stabilizer.apply_text(smt2_text); });
203}
204
205const char *stabilizer_last_error(const stabilizer_handle *handle) {
206 return handle != nullptr ? handle->last_error.c_str() : "invalid handle";
207}
208
209void stabilizer_free_string(char *value) {
210 std::free(value);
211}
void stabilizer_options_set_context_propagation(stabilizer_options *options, bool value)
Enable or disable kernel context propagation.
Definition c_api.cpp:112
bool stabilizer_options_get_subgraph_pruning(const stabilizer_options *options)
Query kernel subgraph pruning/tie-breaking behavior.
Definition c_api.cpp:128
bool stabilizer_get_subgraph_pruning(const stabilizer_handle *handle)
Query subgraph pruning/tie-breaking behavior on a live stabilizer.
Definition c_api.cpp:175
const char * stabilizer_last_error(const stabilizer_handle *handle)
Return the last diagnostic message associated with a handle.
Definition c_api.cpp:205
stabilizer_handle * stabilizer_create(const stabilizer_options *options)
Create a stabilizer instance.
Definition c_api.cpp:132
bool stabilizer_options_get_context_propagation(const stabilizer_options *options)
Query kernel context propagation flag.
Definition c_api.cpp:118
bool stabilizer_get_rewrite(const stabilizer_handle *handle)
Query rewrite normalization on a live stabilizer.
Definition c_api.cpp:149
void stabilizer_set_subgraph_pruning(stabilizer_handle *handle, bool value)
Update subgraph pruning/tie-breaking behavior on a live stabilizer.
Definition c_api.cpp:166
void stabilizer_set_rewrite(stabilizer_handle *handle, bool value)
Update rewrite normalization on a live stabilizer.
Definition c_api.cpp:140
void stabilizer_options_set_rewrite(stabilizer_options *options, bool value)
Enable or disable parser rewrite normalization.
Definition c_api.cpp:102
void stabilizer_options_set_subgraph_pruning(stabilizer_options *options, bool value)
Enable or disable kernel subgraph pruning/tie-breaking behavior.
Definition c_api.cpp:122
void stabilizer_free_string(char *value)
Release strings returned through stabilizer_apply_*.
Definition c_api.cpp:209
void stabilizer_options_destroy(stabilizer_options *options)
Release an options object created by stabilizer_options_create.
Definition c_api.cpp:98
void stabilizer_set_context_propagation(stabilizer_handle *handle, bool value)
Update context propagation on a live stabilizer.
Definition c_api.cpp:153
stabilizer_status stabilizer_apply_text(stabilizer_handle *handle, const char *smt2_text, char **output)
Apply stabilization pipeline to SMT-LIB2 text.
Definition c_api.cpp:192
void stabilizer_destroy(stabilizer_handle *handle)
Destroy a stabilizer instance and associated internal state.
Definition c_api.cpp:136
bool stabilizer_get_context_propagation(const stabilizer_handle *handle)
Query context propagation on a live stabilizer.
Definition c_api.cpp:162
bool stabilizer_options_get_rewrite(const stabilizer_options *options)
Query parser rewrite normalization flag.
Definition c_api.cpp:108
stabilizer_options * stabilizer_options_create(void)
Allocate a new options object with default values.
Definition c_api.cpp:94
stabilizer_status stabilizer_apply_file(stabilizer_handle *handle, const char *file_path, char **output)
Apply stabilization pipeline to an SMT-LIB2 file.
Definition c_api.cpp:179
Public options for the SMTStabilizer API.
bool get_subgraph_pruning() const noexcept
void set_context_propagation(bool value) noexcept
void set_subgraph_pruning(bool value) noexcept
bool get_context_propagation() const noexcept
void set_rewrite(bool rewrite) noexcept
High-level facade for stabilizing SMT-LIB2 inputs.
std::string apply_file(const std::string &file_path) const
Apply the full pipeline to an SMT-LIB2 file.
std::string apply_text(const std::string &smt2_text) const
Apply the full pipeline to an SMT-LIB2 script provided as text.
const SMTStabilizerOptions & options() const noexcept
void set_options(const SMTStabilizerOptions &options) noexcept
stabilizer_status
Result status for C API calls.
@ STABILIZER_STATUS_OK
@ STABILIZER_STATUS_RUNTIME_ERROR
@ STABILIZER_STATUS_INVALID_ARGUMENT
stabilizer_handle(const stabilizer_options *options)
Definition c_api.cpp:41
stabilizer::api::SMTStabilizer stabilizer
Definition c_api.cpp:38
std::string last_error
Definition c_api.cpp:39
stabilizer::api::SMTStabilizerOptions value
Definition c_api.cpp:34