forked from SYNTCOMP/combine-aiger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombine.c
266 lines (246 loc) · 8.41 KB
/
combine.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
//
// Copyright (c) 2016, Leander Tentrup, Saarland University
//
// Licenced under ISC Licsnse, see ./LICENSE.txt form information
//
#include "combine.h"
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/**
* Note: Input names of monitor start with string 'AIGER_NEXT_'
*/
static bool names_do_match(const char *monitor_input, const char *impl_output)
{
const unsigned monitor_input_len = strlen(monitor_input);
const unsigned impl_output_len = strlen(impl_output);
const unsigned prefix_len = strlen("AIGER_NEXT_");
if (strncmp(monitor_input, "AIGER_NEXT_", prefix_len) != 0)
{
// special case if input is not contained in LTL formula
return strcmp(monitor_input, impl_output) == 0;
}
assert(monitor_input_len > prefix_len);
if (prefix_len + impl_output_len > monitor_input_len)
{
return false;
}
// do a case-insensitive comparision because of different translations
if (strcasecmp(monitor_input + prefix_len, impl_output) == 0)
{
return true;
}
return false;
}
static void check_signals_defined(const aiger *monitor, const aiger *implementation)
{
// outputs
for (unsigned i = 0; i < implementation->num_outputs; i++)
{
aiger_symbol output = implementation->outputs[i];
bool found = false;
for (unsigned j = 0; j < monitor->num_inputs; j++)
{
aiger_symbol input = monitor->inputs[j];
if (names_do_match(input.name, output.name))
{
found = true;
break;
}
}
if (!found)
{
fprintf(stderr, "error: output %s does not appear in monitor\n", output.name);
abort();
}
}
// inputs
for (unsigned i = 0; i < implementation->num_inputs; i++)
{
aiger_symbol impl_input = implementation->inputs[i];
bool found = false;
for (unsigned j = 0; j < monitor->num_inputs; j++)
{
aiger_symbol input = monitor->inputs[j];
if (names_do_match(input.name, impl_input.name))
{
found = true;
// use next literal to store corresponding literal in monitor
impl_input.next = input.lit;
implementation->inputs[i] = impl_input;
break;
}
}
if (!found)
{
fprintf(stderr, "error: input %s does not appear in monitor\n", impl_input.name);
abort();
}
}
}
static void add_inputs_from_monitor(aiger *combination, const aiger *monitor, const aiger *implementation)
{
for (unsigned i = 0; i < monitor->num_inputs; i++)
{
aiger_symbol input = monitor->inputs[i];
bool is_output = false;
for (unsigned j = 0; j < implementation->num_outputs; j++)
{
aiger_symbol impl_output = implementation->outputs[j];
if (names_do_match(input.name, impl_output.name))
{
is_output = true;
break;
}
}
if (is_output)
{
continue;
}
aiger_add_input(combination, input.lit, input.name);
}
}
static void add_latches_from_monitor(aiger *combination, const aiger *monitor, const aiger *implementation)
{
for (unsigned i = 0; i < monitor->num_latches; i++)
{
aiger_symbol latch = monitor->latches[i];
aiger_add_latch(combination, latch.lit, latch.next, latch.name);
// copy reset value if present
aiger_add_reset(combination, latch.lit, latch.reset);
}
}
static void add_constraints_from_monitor(aiger *combination, const aiger *monitor, const aiger *implementation)
{
// bad
for (unsigned i = 0; i < monitor->num_bad; i++)
{
aiger_symbol bad = monitor->bad[i];
aiger_add_bad(combination, bad.lit, bad.name);
}
// constraint
for (unsigned i = 0; i < monitor->num_constraints; i++)
{
aiger_symbol constraint = monitor->constraints[i];
aiger_add_constraint(combination, constraint.lit, constraint.name);
}
// justice
for (unsigned i = 0; i < monitor->num_justice; i++)
{
aiger_symbol justice = monitor->justice[i];
aiger_add_justice(combination, justice.size, justice.lits, justice.name);
}
// fairness
for (unsigned i = 0; i < monitor->num_fairness; i++)
{
aiger_symbol fairness = monitor->fairness[i];
aiger_add_fairness(combination, fairness.lit, fairness.name);
}
}
static void import_ands_from_monitor(aiger *combination, const aiger *monitor)
{
for (unsigned i = 0; i < monitor->num_ands; i++)
{
aiger_and and = monitor->ands[i];
aiger_add_and(combination, and.lhs, and.rhs0, and.rhs1);
}
}
static unsigned translate_literal(const aiger *implementation, unsigned literal, unsigned offset)
{
unsigned tag = aiger_lit2tag(implementation, literal);
if (tag == 0)
{
// constant
return literal;
}
else if (tag == 1)
{
// input
aiger_symbol *input = aiger_is_input(implementation, aiger_strip(literal));
assert(input != NULL);
assert(input->next != 0);
return literal % 2 == 1 ? aiger_not(input->next) : input->next;
}
else if (tag == 2)
{
// latch
return literal + offset * 2;
}
else
{
assert(tag == 3);
// and
return literal + offset * 2;
}
}
static void add_latches_from_implementation(aiger *combination, const aiger *implementation, const unsigned offset)
{
for (unsigned i = 0; i < implementation->num_latches; i++)
{
aiger_symbol latch = implementation->latches[i];
unsigned int lit = latch.lit;
// Reset value is only valid if reset = 0 || reset == 1 || reset == lit
// lit might get changed during the translation so if it was the same before, it should be the same afterwords
latch.lit = translate_literal(implementation, latch.lit, offset);
latch.next = translate_literal(implementation, latch.next, offset);
aiger_add_latch(combination, latch.lit, latch.next, latch.name);
// copy reset value if present (if before lit == reset, then the reset is also translated)
aiger_add_reset(combination, latch.lit, (lit == latch.reset) ? latch.lit : latch.reset);
}
}
static void import_ands_from_implementation(aiger *combination, const aiger *implementation, const unsigned offset)
{
for (unsigned i = 0; i < implementation->num_ands; i++)
{
aiger_and and = implementation->ands[i];
and.lhs = translate_literal(implementation, and.lhs, offset);
and.rhs0 = translate_literal(implementation, and.rhs0, offset);
and.rhs1 = translate_literal(implementation, and.rhs1, offset);
aiger_add_and(combination, and.lhs, and.rhs0, and.rhs1);
}
}
static void define_outputs(aiger *combination, const aiger *monitor, const aiger *implementation, const unsigned offset)
{
for (unsigned i = 0; i < monitor->num_inputs; i++)
{
aiger_symbol input = monitor->inputs[i];
bool is_output = false;
aiger_symbol output;
for (unsigned j = 0; j < implementation->num_outputs; j++)
{
output = implementation->outputs[j];
if (names_do_match(input.name, output.name))
{
is_output = true;
break;
}
}
if (!is_output)
{
continue;
}
unsigned function = translate_literal(implementation, output.lit, offset);
aiger_add_and(combination, input.lit, function, 1);
}
}
aiger *combine(const aiger *monitor, const aiger *implementation)
{
aiger *combination = aiger_init();
if (combination == NULL)
{
return NULL;
}
const unsigned offset = monitor->maxvar + 1;
check_signals_defined(monitor, implementation);
add_inputs_from_monitor(combination, monitor, implementation);
add_latches_from_monitor(combination, monitor, implementation);
assert(monitor->num_outputs == 0);
add_constraints_from_monitor(combination, monitor, implementation);
import_ands_from_monitor(combination, monitor);
add_latches_from_implementation(combination, implementation, offset);
import_ands_from_implementation(combination, implementation, offset);
define_outputs(combination, monitor, implementation, offset);
return combination;
}