Skip to content

Commit f5ef531

Browse files
committed
Use zero_initializer instead of gen_zero
gen_zero/gen_one can now safely be removed.
1 parent 0f95418 commit f5ef531

File tree

13 files changed

+123
-225
lines changed

13 files changed

+123
-225
lines changed

src/cegis/instrument/cegis_library.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ Author: Daniel Kroening, [email protected]
77
88
\*******************************************************************/
99

10-
#include <util/expr_util.h>
11-
1210
#include <goto-programs/goto_convert_functions.h>
1311

1412
#include <ansi-c/c_types.h>
1513
#include <ansi-c/cprover_library.h>
1614

15+
#include <linking/zero_initializer.h>
16+
1717
#include <cegis/cegis-util/program_helper.h>
1818
#include <cegis/instrument/meta_variables.h>
1919
#include <cegis/instrument/instrument_var_ops.h>
@@ -78,7 +78,11 @@ goto_programt::targett init_array(const symbol_tablet &st, goto_programt &body,
7878
pos->source_location=default_cegis_source_location();
7979
const symbol_exprt array(st.lookup(name).symbol_expr());
8080
const array_typet &type=to_array_type(array.type());
81-
pos->code=code_assignt(array, array_of_exprt(gen_zero(type.subtype()), type));
81+
const namespacet ns(st);
82+
pos->code=
83+
code_assignt(
84+
array,
85+
zero_initializer(type, pos->source_location, ns));
8286
return pos;
8387
}
8488

src/cpp/cpp_typecheck_expr.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ Author: Daniel Kroening, [email protected]
1919
#include <ansi-c/c_qualifiers.h>
2020
#include <ansi-c/c_sizeof.h>
2121

22+
#include <linking/zero_initializer.h>
23+
2224
#include "cpp_type2name.h"
2325
#include "cpp_typecheck.h"
2426
#include "cpp_convert_type.h"
@@ -1035,15 +1037,12 @@ void cpp_typecheckt::typecheck_expr_explicit_typecast(exprt &expr)
10351037
{
10361038
// Default value, e.g., int()
10371039
typecheck_type(expr.type());
1038-
exprt new_expr=gen_zero(expr.type());
1039-
1040-
if(new_expr.is_nil())
1041-
{
1042-
error().source_location=expr.find_source_location();
1043-
error() << "no default value for `" << to_string(expr.type())
1044-
<< "'" << eom;
1045-
throw 0;
1046-
}
1040+
exprt new_expr=
1041+
::zero_initializer(
1042+
expr.type(),
1043+
expr.find_source_location(),
1044+
*this,
1045+
get_message_handler());
10471046

10481047
new_expr.add_source_location()=expr.source_location();
10491048
expr=new_expr;

src/goto-programs/builtin_functions.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,12 @@ void goto_convertt::do_java_new_array(
814814
t_p->source_location=location;
815815

816816
// zero-initialize the data
817-
exprt zero_element=gen_zero(data.type().subtype());
817+
exprt zero_element=
818+
zero_initializer(
819+
data.type().subtype(),
820+
location,
821+
ns,
822+
get_message_handler());
818823
codet array_set(ID_array_set);
819824
array_set.copy_to_operands(data, zero_element);
820825
goto_programt::targett t_d=dest.add_instruction(OTHER);
@@ -1646,7 +1651,13 @@ void goto_convertt::do_function_call_symbol(
16461651
{
16471652
goto_programt::targett t=dest.add_instruction(ASSIGN);
16481653
t->source_location=function.source_location();
1649-
t->code=code_assignt(dest_expr, gen_zero(dest_expr.type()));
1654+
exprt zero=
1655+
zero_initializer(
1656+
dest_expr.type(),
1657+
function.source_location(),
1658+
ns,
1659+
get_message_handler());
1660+
t->code=code_assignt(dest_expr, zero);
16501661
}
16511662
}
16521663
else if(identifier=="__sync_fetch_and_add" ||

src/goto-symex/symex_builtin_functions.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ Author: Daniel Kroening, [email protected]
2121

2222
#include <ansi-c/c_types.h>
2323

24+
#include <linking/zero_initializer.h>
25+
2426
#include "goto_symex.h"
2527
#include "goto_symex_state.h"
2628

@@ -235,7 +237,7 @@ void goto_symext::symex_gcc_builtin_va_arg_next(
235237
do_simplify(tmp);
236238
irep_idt id=get_symbol(tmp);
237239

238-
exprt rhs=gen_zero(lhs.type());
240+
exprt rhs=zero_initializer(lhs.type(), code.source_location(), ns);
239241

240242
if(id!=irep_idt())
241243
{

src/java_bytecode/java_bytecode_convert_class.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ Author: Daniel Kroening, [email protected]
1515
#include "java_types.h"
1616
#include "java_bytecode_convert_method.h"
1717

18+
#include <util/namespace.h>
1819
#include <util/std_expr.h>
19-
#include <util/expr_util.h>
20+
21+
#include <linking/zero_initializer.h>
2022

2123
class java_bytecode_convert_classt:public messaget
2224
{
@@ -215,7 +217,13 @@ void java_bytecode_convert_classt::convert(
215217
"."+id2string(f.name);
216218
new_symbol.mode=ID_java;
217219
new_symbol.is_type=false;
218-
new_symbol.value=gen_zero(field_type);
220+
const namespacet ns(symbol_table);
221+
new_symbol.value=
222+
zero_initializer(
223+
field_type,
224+
class_symbol.location,
225+
ns,
226+
get_message_handler());
219227

220228
// Do we have the static field symbol already?
221229
const auto s_it=symbol_table.symbols.find(new_symbol.name);

src/linking/zero_initializer.cpp

Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -74,25 +74,42 @@ exprt zero_initializert::zero_initializer_rec(
7474
{
7575
const irep_idt &type_id=type.id();
7676

77-
if(type_id==ID_bool)
77+
if(type_id==ID_unsignedbv ||
78+
type_id==ID_signedbv ||
79+
type_id==ID_pointer ||
80+
type_id==ID_c_enum ||
81+
type_id==ID_incomplete_c_enum ||
82+
type_id==ID_c_bit_field ||
83+
type_id==ID_bool ||
84+
type_id==ID_c_bool ||
85+
type_id==ID_floatbv ||
86+
type_id==ID_fixedbv)
7887
{
79-
exprt result=false_exprt();
88+
exprt result=from_integer(0, type);
8089
result.add_source_location()=source_location;
8190
return result;
8291
}
83-
else if(type_id==ID_unsignedbv ||
84-
type_id==ID_signedbv ||
85-
type_id==ID_floatbv ||
86-
type_id==ID_fixedbv ||
87-
type_id==ID_pointer ||
88-
type_id==ID_complex ||
89-
type_id==ID_c_enum ||
90-
type_id==ID_incomplete_c_enum ||
91-
type_id==ID_c_enum_tag ||
92-
type_id==ID_c_bit_field ||
93-
type_id==ID_c_bool)
92+
else if(type_id==ID_rational ||
93+
type_id==ID_real)
9494
{
95-
exprt result=gen_zero(type);
95+
constant_exprt result(ID_0, type);
96+
result.add_source_location()=source_location;
97+
return result;
98+
}
99+
else if(type_id==ID_verilog_signedbv ||
100+
type_id==ID_verilog_unsignedbv)
101+
{
102+
std::size_t width=to_bitvector_type(type).get_width();
103+
std::string value(width, '0');
104+
105+
constant_exprt result(value, type);
106+
result.add_source_location()=source_location;
107+
return result;
108+
}
109+
else if(type_id==ID_complex)
110+
{
111+
exprt sub_zero=zero_initializer_rec(type.subtype(), source_location);
112+
complex_exprt result(sub_zero, sub_zero, to_complex_type(type));
96113
result.add_source_location()=source_location;
97114
return result;
98115
}
@@ -266,6 +283,27 @@ exprt zero_initializert::zero_initializer_rec(
266283

267284
return result;
268285
}
286+
else if(type_id==ID_c_enum_tag)
287+
{
288+
return
289+
zero_initializer_rec(
290+
ns.follow_tag(to_c_enum_tag_type(type)),
291+
source_location);
292+
}
293+
else if(type_id==ID_struct_tag)
294+
{
295+
return
296+
zero_initializer_rec(
297+
ns.follow_tag(to_struct_tag_type(type)),
298+
source_location);
299+
}
300+
else if(type_id==ID_union_tag)
301+
{
302+
return
303+
zero_initializer_rec(
304+
ns.follow_tag(to_union_tag_type(type)),
305+
source_location);
306+
}
269307
else if(type_id==ID_string)
270308
{
271309
return constant_exprt(irep_idt(), type);

src/memory-models/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ SRC = mm_y.tab.cpp mm_lex.yy.cpp mmcc_main.cpp mm_parser.cpp \
22
mmcc_parse_options.cpp mm2cpp.cpp
33

44
OBJ += ../big-int/big-int$(LIBEXT) \
5+
../ansi-c/ansi-c$(LIBEXT) \
6+
../linking/linking$(LIBEXT) \
57
../util/util$(LIBEXT)
68

79
INCLUDES= -I ..

src/path-symex/path_symex.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ Author: Daniel Kroening, [email protected]
1111
#include <util/string2int.h>
1212
#include <util/byte_operators.h>
1313
#include <util/pointer_offset_size.h>
14-
#include <util/expr_util.h>
1514
#include <util/base_type.h>
1615
#include <util/prefix.h>
1716
#include <ansi-c/c_types.h>
1817

18+
#include <linking/zero_initializer.h>
19+
1920
#include <pointer-analysis/dereference.h>
2021

2122
#include "path_symex_class.h"
@@ -352,7 +353,11 @@ void path_symext::symex_va_arg_next(
352353

353354
// Get old symbol of va_arg and modify it to generate a new one.
354355
irep_idt id=get_old_va_symbol(state, tmp);
355-
exprt rhs=gen_zero(lhs.type());
356+
exprt rhs=
357+
zero_initializer(
358+
lhs.type(),
359+
code.source_location(),
360+
state.var_map.ns);
356361

357362
if(!id.empty())
358363
{

src/solvers/smt1/smt1_conv.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ Author: Daniel Kroening, [email protected]
2121

2222
#include <langapi/language_util.h>
2323

24+
#include <linking/zero_initializer.h>
25+
2426
#include <solvers/flattening/boolbv_width.h>
2527
#include <solvers/flattening/pointer_logic.h>
2628
#include <solvers/flattening/flatten_byte_operators.h>
@@ -1540,7 +1542,9 @@ void smt1_convt::convert_typecast(
15401542
out << "(not (= ";
15411543
convert_expr(src, true);
15421544
out << " ";
1543-
convert_expr(gen_zero(src_type), true);
1545+
convert_expr(
1546+
zero_initializer(src_type, expr.source_location(), ns),
1547+
true);
15441548
out << "))";
15451549
}
15461550
else
@@ -1566,7 +1570,9 @@ void smt1_convt::convert_typecast(
15661570
out << "(not (= ";
15671571
convert_expr(src, true);
15681572
out << " ";
1569-
convert_expr(gen_zero(src_type), true);
1573+
convert_expr(
1574+
zero_initializer(src_type, expr.source_location(), ns),
1575+
true);
15701576
out << ")) "; // not, =
15711577
out << " bv1[" << to_width << "]";
15721578
out << " bv0[" << to_width << "]";

src/solvers/smt2/smt2_conv.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ Author: Daniel Kroening, [email protected]
2222

2323
#include <langapi/language_util.h>
2424

25+
#include <linking/zero_initializer.h>
26+
2527
#include <solvers/flattening/boolbv_width.h>
2628
#include <solvers/flattening/flatten_byte_operators.h>
2729
#include <solvers/flattening/c_bit_field_replacement_type.h>
@@ -2062,7 +2064,8 @@ void smt2_convt::convert_typecast(const typecast_exprt &expr)
20622064
out << "(not (= ";
20632065
convert_expr(src);
20642066
out << " ";
2065-
convert_expr(gen_zero(src_type));
2067+
convert_expr(
2068+
zero_initializer(src_type, expr.source_location(), ns));
20662069
out << "))";
20672070
}
20682071
else if(src_type.id()==ID_floatbv)
@@ -2088,7 +2091,8 @@ void smt2_convt::convert_typecast(const typecast_exprt &expr)
20882091
out << "(not (= ";
20892092
convert_expr(src);
20902093
out << " ";
2091-
convert_expr(gen_zero(src_type));
2094+
convert_expr(
2095+
zero_initializer(src_type, expr.source_location(), ns));
20922096
out << ")) "; // not, =
20932097
out << " (_ bv1 " << to_width << ")";
20942098
out << " (_ bv0 " << to_width << ")";

0 commit comments

Comments
 (0)