13
13
#include < util/arith_tools.h>
14
14
#include < util/std_types.h>
15
15
#include < util/std_expr.h>
16
+ #include < util/std_code.h>
16
17
#include < util/pointer_offset_size.h>
17
18
18
19
#include < ansi-c/c_types.h>
19
20
#include < ansi-c/expr2c.h>
20
21
21
22
#include " zero_initializer.h"
22
23
24
+ template <bool nondet>
23
25
class zero_initializert :public messaget
24
26
{
25
27
public:
@@ -68,7 +70,8 @@ Function: zero_initializert::zero_initializer_rec
68
70
69
71
\*******************************************************************/
70
72
71
- exprt zero_initializert::zero_initializer_rec (
73
+ template <bool nondet>
74
+ exprt zero_initializert<nondet>::zero_initializer_rec(
72
75
const typet &type,
73
76
const source_locationt &source_location)
74
77
{
@@ -85,31 +88,55 @@ exprt zero_initializert::zero_initializer_rec(
85
88
type_id==ID_floatbv ||
86
89
type_id==ID_fixedbv)
87
90
{
88
- exprt result=from_integer (0 , type);
91
+ exprt result;
92
+ if (nondet)
93
+ result=side_effect_expr_nondett (type);
94
+ else
95
+ result=from_integer (0 , type);
96
+
89
97
result.add_source_location ()=source_location;
90
98
return result;
91
99
}
92
100
else if (type_id==ID_rational ||
93
101
type_id==ID_real)
94
102
{
95
- constant_exprt result (ID_0, type);
103
+ exprt result;
104
+ if (nondet)
105
+ result=side_effect_expr_nondett (type);
106
+ else
107
+ result=constant_exprt (ID_0, type);
108
+
96
109
result.add_source_location ()=source_location;
97
110
return result;
98
111
}
99
112
else if (type_id==ID_verilog_signedbv ||
100
113
type_id==ID_verilog_unsignedbv)
101
114
{
102
- std::size_t width=to_bitvector_type (type).get_width ();
103
- std::string value (width, ' 0' );
115
+ exprt result;
116
+ if (nondet)
117
+ result=side_effect_expr_nondett (type);
118
+ else
119
+ {
120
+ std::size_t width=to_bitvector_type (type).get_width ();
121
+ std::string value (width, ' 0' );
122
+
123
+ result=constant_exprt (value, type);
124
+ }
104
125
105
- constant_exprt result (value, type);
106
126
result.add_source_location ()=source_location;
107
127
return result;
108
128
}
109
129
else if (type_id==ID_complex)
110
130
{
111
- exprt sub_zero=zero_initializer_rec (type.subtype (), source_location);
112
- complex_exprt result (sub_zero, sub_zero, to_complex_type (type));
131
+ exprt result;
132
+ if (nondet)
133
+ result=side_effect_expr_nondett (type);
134
+ else
135
+ {
136
+ exprt sub_zero=zero_initializer_rec (type.subtype (), source_location);
137
+ result=complex_exprt (sub_zero, sub_zero, to_complex_type (type));
138
+ }
139
+
113
140
result.add_source_location ()=source_location;
114
141
return result;
115
142
}
@@ -148,6 +175,13 @@ exprt zero_initializert::zero_initializer_rec(
148
175
}
149
176
else if (to_integer (array_type.size (), array_size))
150
177
{
178
+ if (nondet)
179
+ {
180
+ exprt result=side_effect_expr_nondett (type);
181
+ result.add_source_location ()=source_location;
182
+ return result;
183
+ }
184
+
151
185
error ().source_location =source_location;
152
186
error () << " failed to zero-initialize array of non-fixed size `"
153
187
<< to_string (array_type.size ()) << " '" << eom;
@@ -178,6 +212,13 @@ exprt zero_initializert::zero_initializer_rec(
178
212
179
213
if (to_integer (vector_type.size (), vector_size))
180
214
{
215
+ if (nondet)
216
+ {
217
+ exprt result=side_effect_expr_nondett (type);
218
+ result.add_source_location ()=source_location;
219
+ return result;
220
+ }
221
+
181
222
error ().source_location =source_location;
182
223
error () << " failed to zero-initialize vector of non-fixed size `"
183
224
<< to_string (vector_type.size ()) << " '" << eom;
@@ -308,7 +349,14 @@ exprt zero_initializert::zero_initializer_rec(
308
349
}
309
350
else if (type_id==ID_string)
310
351
{
311
- return constant_exprt (irep_idt (), type);
352
+ exprt result;
353
+ if (nondet)
354
+ result=side_effect_expr_nondett (type);
355
+ else
356
+ result=constant_exprt (irep_idt (), type);
357
+
358
+ result.add_source_location ()=source_location;
359
+ return result;
312
360
}
313
361
else
314
362
{
@@ -337,7 +385,29 @@ exprt zero_initializer(
337
385
const namespacet &ns,
338
386
message_handlert &message_handler)
339
387
{
340
- zero_initializert z_i (ns, message_handler);
388
+ zero_initializert<false > z_i (ns, message_handler);
389
+ return z_i (type, source_location);
390
+ }
391
+
392
+ /* ******************************************************************\
393
+
394
+ Function: nondet_initializer
395
+
396
+ Inputs:
397
+
398
+ Outputs:
399
+
400
+ Purpose:
401
+
402
+ \*******************************************************************/
403
+
404
+ exprt nondet_initializer (
405
+ const typet &type,
406
+ const source_locationt &source_location,
407
+ const namespacet &ns,
408
+ message_handlert &message_handler)
409
+ {
410
+ zero_initializert<true > z_i (ns, message_handler);
341
411
return z_i (type, source_location);
342
412
}
343
413
@@ -363,7 +433,38 @@ exprt zero_initializer(
363
433
364
434
try
365
435
{
366
- zero_initializert z_i (ns, mh);
436
+ zero_initializert<false > z_i (ns, mh);
437
+ return z_i (type, source_location);
438
+ }
439
+ catch (int )
440
+ {
441
+ throw oss.str ();
442
+ }
443
+ }
444
+
445
+ /* ******************************************************************\
446
+
447
+ Function: nondet_initializer
448
+
449
+ Inputs:
450
+
451
+ Outputs:
452
+
453
+ Purpose:
454
+
455
+ \*******************************************************************/
456
+
457
+ exprt nondet_initializer (
458
+ const typet &type,
459
+ const source_locationt &source_location,
460
+ const namespacet &ns)
461
+ {
462
+ std::ostringstream oss;
463
+ stream_message_handlert mh (oss);
464
+
465
+ try
466
+ {
467
+ zero_initializert<true > z_i (ns, mh);
367
468
return z_i (type, source_location);
368
469
}
369
470
catch (int )
0 commit comments