18
18
#include " message.h"
19
19
#include " namespace.h"
20
20
#include " pointer_offset_size.h"
21
+ #include " std_code.h"
21
22
#include " std_expr.h"
22
23
24
+ template <bool nondet>
23
25
class expr_initializert : public messaget
24
26
{
25
27
public:
@@ -46,7 +48,8 @@ class expr_initializert : public messaget
46
48
const source_locationt &source_location);
47
49
};
48
50
49
- exprt expr_initializert::expr_initializer_rec (
51
+ template <bool nondet>
52
+ exprt expr_initializert<nondet>::expr_initializer_rec(
50
53
const typet &type,
51
54
const source_locationt &source_location)
52
55
{
@@ -63,38 +66,62 @@ exprt expr_initializert::expr_initializer_rec(
63
66
type_id==ID_floatbv ||
64
67
type_id==ID_fixedbv)
65
68
{
66
- exprt result=from_integer (0 , type);
69
+ exprt result;
70
+ if (nondet)
71
+ result = side_effect_expr_nondett (type);
72
+ else
73
+ result = from_integer (0 , type);
74
+
67
75
result.add_source_location ()=source_location;
68
76
return result;
69
77
}
70
78
else if (type_id==ID_rational ||
71
79
type_id==ID_real)
72
80
{
73
- constant_exprt result (ID_0, type);
81
+ exprt result;
82
+ if (nondet)
83
+ result = side_effect_expr_nondett (type);
84
+ else
85
+ result = constant_exprt (ID_0, type);
86
+
74
87
result.add_source_location ()=source_location;
75
88
return result;
76
89
}
77
90
else if (type_id==ID_verilog_signedbv ||
78
91
type_id==ID_verilog_unsignedbv)
79
92
{
80
- std::size_t width=to_bitvector_type (type).get_width ();
81
- std::string value (width, ' 0' );
93
+ exprt result;
94
+ if (nondet)
95
+ result = side_effect_expr_nondett (type);
96
+ else
97
+ {
98
+ const std::size_t width = to_bitvector_type (type).get_width ();
99
+ std::string value (width, ' 0' );
100
+
101
+ result = constant_exprt (value, type);
102
+ }
82
103
83
- constant_exprt result (value, type);
84
104
result.add_source_location ()=source_location;
85
105
return result;
86
106
}
87
107
else if (type_id==ID_complex)
88
108
{
89
- exprt sub_zero = expr_initializer_rec (type.subtype (), source_location);
90
- complex_exprt result (sub_zero, sub_zero, to_complex_type (type));
109
+ exprt result;
110
+ if (nondet)
111
+ result = side_effect_expr_nondett (type);
112
+ else
113
+ {
114
+ exprt sub_zero = expr_initializer_rec (type.subtype (), source_location);
115
+ result = complex_exprt (sub_zero, sub_zero, to_complex_type (type));
116
+ }
117
+
91
118
result.add_source_location ()=source_location;
92
119
return result;
93
120
}
94
121
else if (type_id==ID_code)
95
122
{
96
123
error ().source_location =source_location;
97
- error () << " cannot zero- initialize code-type" << eom;
124
+ error () << " cannot initialize code-type" << eom;
98
125
throw 0 ;
99
126
}
100
127
else if (type_id==ID_array)
@@ -120,12 +147,26 @@ exprt expr_initializert::expr_initializer_rec(
120
147
121
148
if (array_type.size ().id ()==ID_infinity)
122
149
{
150
+ if (nondet)
151
+ {
152
+ side_effect_expr_nondett result (type);
153
+ result.add_source_location () = source_location;
154
+ return result;
155
+ }
156
+
123
157
array_of_exprt value (tmpval, array_type);
124
158
value.add_source_location ()=source_location;
125
159
return value;
126
160
}
127
161
else if (to_integer (array_type.size (), array_size))
128
162
{
163
+ if (nondet)
164
+ {
165
+ side_effect_expr_nondett result (type);
166
+ result.add_source_location () = source_location;
167
+ return result;
168
+ }
169
+
129
170
error ().source_location =source_location;
130
171
error () << " failed to zero-initialize array of non-fixed size `"
131
172
<< format (array_type.size ()) << " '" << eom;
@@ -135,7 +176,7 @@ exprt expr_initializert::expr_initializer_rec(
135
176
if (array_size<0 )
136
177
{
137
178
error ().source_location =source_location;
138
- error () << " failed to zero- initialize array with negative size" << eom;
179
+ error () << " failed to initialize array with negative size" << eom;
139
180
throw 0 ;
140
181
}
141
182
@@ -155,6 +196,13 @@ exprt expr_initializert::expr_initializer_rec(
155
196
156
197
if (to_integer (vector_type.size (), vector_size))
157
198
{
199
+ if (nondet)
200
+ {
201
+ side_effect_expr_nondett result (type);
202
+ result.add_source_location () = source_location;
203
+ return result;
204
+ }
205
+
158
206
error ().source_location =source_location;
159
207
error () << " failed to zero-initialize vector of non-fixed size `"
160
208
<< format (vector_type.size ()) << " '" << eom;
@@ -164,7 +212,7 @@ exprt expr_initializert::expr_initializer_rec(
164
212
if (vector_size<0 )
165
213
{
166
214
error ().source_location =source_location;
167
- error () << " failed to zero- initialize vector with negative size" << eom;
215
+ error () << " failed to initialize vector with negative size" << eom;
168
216
throw 0 ;
169
217
}
170
218
@@ -283,12 +331,19 @@ exprt expr_initializert::expr_initializer_rec(
283
331
}
284
332
else if (type_id==ID_string)
285
333
{
286
- return constant_exprt (irep_idt (), type);
334
+ exprt result;
335
+ if (nondet)
336
+ result = side_effect_expr_nondett (type);
337
+ else
338
+ result = constant_exprt (irep_idt (), type);
339
+
340
+ result.add_source_location ()=source_location;
341
+ return result;
287
342
}
288
343
else
289
344
{
290
345
error ().source_location =source_location;
291
- error () << " failed to zero- initialize `" << format (type) << " '" << eom;
346
+ error () << " failed to initialize `" << format (type) << " '" << eom;
292
347
throw 0 ;
293
348
}
294
349
}
@@ -299,7 +354,17 @@ exprt zero_initializer(
299
354
const namespacet &ns,
300
355
message_handlert &message_handler)
301
356
{
302
- expr_initializert z_i (ns, message_handler);
357
+ expr_initializert<false > z_i (ns, message_handler);
358
+ return z_i (type, source_location);
359
+ }
360
+
361
+ exprt nondet_initializer (
362
+ const typet &type,
363
+ const source_locationt &source_location,
364
+ const namespacet &ns,
365
+ message_handlert &message_handler)
366
+ {
367
+ expr_initializert<true > z_i (ns, message_handler);
303
368
return z_i (type, source_location);
304
369
}
305
370
@@ -313,7 +378,26 @@ exprt zero_initializer(
313
378
314
379
try
315
380
{
316
- expr_initializert z_i (ns, mh);
381
+ expr_initializert<false > z_i (ns, mh);
382
+ return z_i (type, source_location);
383
+ }
384
+ catch (int )
385
+ {
386
+ throw oss.str ();
387
+ }
388
+ }
389
+
390
+ exprt nondet_initializer (
391
+ const typet &type,
392
+ const source_locationt &source_location,
393
+ const namespacet &ns)
394
+ {
395
+ std::ostringstream oss;
396
+ stream_message_handlert mh (oss);
397
+
398
+ try
399
+ {
400
+ expr_initializert<true > z_i (ns, mh);
317
401
return z_i (type, source_location);
318
402
}
319
403
catch (int )
0 commit comments