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 zero_initializert :public messaget
24
26
{
25
27
public:
@@ -46,7 +48,8 @@ class zero_initializert:public messaget
46
48
const source_locationt &source_location);
47
49
};
48
50
49
- exprt zero_initializert::zero_initializer_rec (
51
+ template <bool nondet>
52
+ exprt zero_initializert<nondet>::zero_initializer_rec(
50
53
const typet &type,
51
54
const source_locationt &source_location)
52
55
{
@@ -63,31 +66,55 @@ exprt zero_initializert::zero_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=zero_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 = zero_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
}
@@ -119,12 +146,26 @@ exprt zero_initializert::zero_initializer_rec(
119
146
120
147
if (array_type.size ().id ()==ID_infinity)
121
148
{
149
+ if (nondet)
150
+ {
151
+ side_effect_expr_nondett result (type);
152
+ result.add_source_location () = source_location;
153
+ return result;
154
+ }
155
+
122
156
array_of_exprt value (tmpval, array_type);
123
157
value.add_source_location ()=source_location;
124
158
return value;
125
159
}
126
160
else if (to_integer (array_type.size (), array_size))
127
161
{
162
+ if (nondet)
163
+ {
164
+ side_effect_expr_nondett result (type);
165
+ result.add_source_location () = source_location;
166
+ return result;
167
+ }
168
+
128
169
error ().source_location =source_location;
129
170
error () << " failed to zero-initialize array of non-fixed size `"
130
171
<< format (array_type.size ()) << " '" << eom;
@@ -154,6 +195,13 @@ exprt zero_initializert::zero_initializer_rec(
154
195
155
196
if (to_integer (vector_type.size (), vector_size))
156
197
{
198
+ if (nondet)
199
+ {
200
+ side_effect_expr_nondett result (type);
201
+ result.add_source_location () = source_location;
202
+ return result;
203
+ }
204
+
157
205
error ().source_location =source_location;
158
206
error () << " failed to zero-initialize vector of non-fixed size `"
159
207
<< format (vector_type.size ()) << " '" << eom;
@@ -282,7 +330,14 @@ exprt zero_initializert::zero_initializer_rec(
282
330
}
283
331
else if (type_id==ID_string)
284
332
{
285
- return constant_exprt (irep_idt (), type);
333
+ exprt result;
334
+ if (nondet)
335
+ result = side_effect_expr_nondett (type);
336
+ else
337
+ result = constant_exprt (irep_idt (), type);
338
+
339
+ result.add_source_location ()=source_location;
340
+ return result;
286
341
}
287
342
else
288
343
{
@@ -298,7 +353,17 @@ exprt zero_initializer(
298
353
const namespacet &ns,
299
354
message_handlert &message_handler)
300
355
{
301
- zero_initializert z_i (ns, message_handler);
356
+ zero_initializert<false > z_i (ns, message_handler);
357
+ return z_i (type, source_location);
358
+ }
359
+
360
+ exprt nondet_initializer (
361
+ const typet &type,
362
+ const source_locationt &source_location,
363
+ const namespacet &ns,
364
+ message_handlert &message_handler)
365
+ {
366
+ zero_initializert<true > z_i (ns, message_handler);
302
367
return z_i (type, source_location);
303
368
}
304
369
@@ -312,7 +377,26 @@ exprt zero_initializer(
312
377
313
378
try
314
379
{
315
- zero_initializert z_i (ns, mh);
380
+ zero_initializert<false > z_i (ns, mh);
381
+ return z_i (type, source_location);
382
+ }
383
+ catch (int )
384
+ {
385
+ throw oss.str ();
386
+ }
387
+ }
388
+
389
+ exprt nondet_initializer (
390
+ const typet &type,
391
+ const source_locationt &source_location,
392
+ const namespacet &ns)
393
+ {
394
+ std::ostringstream oss;
395
+ stream_message_handlert mh (oss);
396
+
397
+ try
398
+ {
399
+ zero_initializert<true > z_i (ns, mh);
316
400
return z_i (type, source_location);
317
401
}
318
402
catch (int )
0 commit comments