Skip to content

Commit 0e1b194

Browse files
author
Daniel Kroening
authored
Merge pull request #523 from smowton/factor_fresh_symbol
Factor out fresh symbol generation
2 parents 0139558 + 002e94b commit 0e1b194

11 files changed

+187
-120
lines changed

src/goto-instrument/code_contracts.cpp

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Date: February 2016
99
\*******************************************************************/
1010

1111
#include <util/cprover_prefix.h>
12+
#include <util/fresh_symbol.h>
1213
#include <util/replace_symbol.h>
1314

1415
#include <goto-programs/remove_skip.h>
@@ -291,20 +292,13 @@ const symbolt &code_contractst::new_tmp_symbol(
291292
const typet &type,
292293
const source_locationt &source_location)
293294
{
294-
auxiliary_symbolt new_symbol;
295-
new_symbol.type=type;
296-
new_symbol.location=source_location;
297-
298-
symbolt *symbol_ptr;
299-
300-
do
301-
{
302-
new_symbol.base_name="tmp_cc$"+std::to_string(++temporary_counter);
303-
new_symbol.name=new_symbol.base_name;
304-
}
305-
while(symbol_table.move(new_symbol, symbol_ptr));
306-
307-
return *symbol_ptr;
295+
return get_fresh_aux_symbol(
296+
type,
297+
"",
298+
"tmp_cc",
299+
source_location,
300+
irep_idt(),
301+
symbol_table);
308302
}
309303

310304
/*******************************************************************\

src/goto-programs/goto_clean_expr.cpp

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

9+
#include <util/fresh_symbol.h>
910
#include <util/simplify_expr.h>
1011
#include <util/std_expr.h>
1112
#include <util/rename.h>
@@ -33,24 +34,21 @@ symbol_exprt goto_convertt::make_compound_literal(
3334
{
3435
const source_locationt source_location=expr.find_source_location();
3536

36-
auxiliary_symbolt new_symbol;
37-
symbolt *symbol_ptr;
38-
39-
do
40-
{
41-
new_symbol.base_name="literal$"+std::to_string(++temporary_counter);
42-
new_symbol.name=tmp_symbol_prefix+id2string(new_symbol.base_name);
43-
new_symbol.is_static_lifetime=source_location.get_function().empty();
44-
new_symbol.value=expr;
45-
new_symbol.type=expr.type();
46-
new_symbol.location=source_location;
47-
}
48-
while(symbol_table.move(new_symbol, symbol_ptr));
37+
symbolt &new_symbol=
38+
get_fresh_aux_symbol(
39+
expr.type(),
40+
tmp_symbol_prefix,
41+
"literal",
42+
source_location,
43+
irep_idt(),
44+
symbol_table);
45+
new_symbol.is_static_lifetime=source_location.get_function().empty();
46+
new_symbol.value=expr;
4947

5048
// The value might depend on a variable, thus
5149
// generate code for this.
5250

53-
symbol_exprt result=symbol_ptr->symbol_expr();
51+
symbol_exprt result=new_symbol.symbol_expr();
5452
result.add_source_location()=source_location;
5553

5654
// The lifetime of compound literals is really that of
@@ -62,7 +60,7 @@ symbol_exprt goto_convertt::make_compound_literal(
6260
convert(code_assign, dest);
6361

6462
// now create a 'dead' instruction
65-
if(!symbol_ptr->is_static_lifetime)
63+
if(!new_symbol.is_static_lifetime)
6664
{
6765
code_deadt code_dead(result);
6866
targets.destructor_stack.push_back(code_dead);

src/goto-programs/goto_convert.cpp

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Author: Daniel Kroening, [email protected]
1010

1111
#include <util/cprover_prefix.h>
1212
#include <util/expr_util.h>
13+
#include <util/fresh_symbol.h>
1314
#include <util/prefix.h>
1415
#include <util/std_expr.h>
1516
#include <util/symbol_table.h>
@@ -2754,24 +2755,21 @@ symbolt &goto_convertt::new_tmp_symbol(
27542755
goto_programt &dest,
27552756
const source_locationt &source_location)
27562757
{
2757-
auxiliary_symbolt new_symbol;
2758-
symbolt *symbol_ptr;
2759-
2760-
do
2761-
{
2762-
new_symbol.base_name="tmp_"+suffix+"$"+std::to_string(++temporary_counter);
2763-
new_symbol.name=tmp_symbol_prefix+id2string(new_symbol.base_name);
2764-
new_symbol.type=type;
2765-
new_symbol.location=source_location;
2766-
}
2767-
while(symbol_table.move(new_symbol, symbol_ptr));
2758+
symbolt &new_symbol=
2759+
get_fresh_aux_symbol(
2760+
type,
2761+
tmp_symbol_prefix,
2762+
"tmp_"+suffix,
2763+
source_location,
2764+
irep_idt(),
2765+
symbol_table);
27682766

27692767
code_declt decl;
2770-
decl.symbol()=symbol_ptr->symbol_expr();
2768+
decl.symbol()=new_symbol.symbol_expr();
27712769
decl.add_source_location()=source_location;
27722770
convert_decl(decl, dest);
27732771

2774-
return *symbol_ptr;
2772+
return new_symbol;
27752773
}
27762774

27772775
/*******************************************************************\

src/goto-programs/goto_convert_class.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class goto_convertt:public messaget
3232
symbol_table(_symbol_table),
3333
ns(_symbol_table),
3434
temporary_counter(0),
35-
tmp_symbol_prefix("goto_convertt::")
35+
tmp_symbol_prefix("goto_convertt")
3636
{
3737
}
3838

src/goto-programs/remove_function_pointers.cpp

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Author: Daniel Kroening, [email protected]
88

99
#include <cassert>
1010

11+
#include <util/fresh_symbol.h>
1112
#include <util/replace_expr.h>
1213
#include <util/source_location.h>
1314
#include <util/std_expr.h>
@@ -68,8 +69,6 @@ class remove_function_pointerst
6869
code_function_callt &function_call,
6970
goto_programt &dest);
7071

71-
symbolt &new_tmp_symbol();
72-
7372
void compute_address_taken_in_symbols(
7473
std::set<irep_idt> &address_taken)
7574
{
@@ -110,37 +109,6 @@ remove_function_pointerst::remove_function_pointerst(
110109

111110
/*******************************************************************\
112111
113-
Function: remove_function_pointerst::new_tmp_symbol
114-
115-
Inputs:
116-
117-
Outputs:
118-
119-
Purpose:
120-
121-
\*******************************************************************/
122-
123-
symbolt &remove_function_pointerst::new_tmp_symbol()
124-
{
125-
static int temporary_counter;
126-
127-
auxiliary_symbolt new_symbol;
128-
symbolt *symbol_ptr;
129-
130-
do
131-
{
132-
new_symbol.base_name=
133-
"tmp_return_val$"+std::to_string(++temporary_counter);
134-
new_symbol.name=
135-
"remove_function_pointers::"+id2string(new_symbol.base_name);
136-
}
137-
while(symbol_table.move(new_symbol, symbol_ptr));
138-
139-
return *symbol_ptr;
140-
}
141-
142-
/*******************************************************************\
143-
144112
Function: remove_function_pointerst::arg_is_type_compatible
145113
146114
Inputs:
@@ -311,9 +279,14 @@ void remove_function_pointerst::fix_return_type(
311279
code_type.return_type(), ns))
312280
return;
313281

314-
symbolt &tmp_symbol=new_tmp_symbol();
315-
tmp_symbol.type=code_type.return_type();
316-
tmp_symbol.location=function_call.source_location();
282+
symbolt &tmp_symbol=
283+
get_fresh_aux_symbol(
284+
code_type.return_type(),
285+
"remove_function_pointers",
286+
"tmp_return_val",
287+
function_call.source_location(),
288+
irep_idt(),
289+
symbol_table);
317290

318291
symbol_exprt tmp_symbol_expr;
319292
tmp_symbol_expr.type()=tmp_symbol.type;

src/goto-programs/remove_instanceof.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Author: Chris Smowton, [email protected]
99
#include "class_hierarchy.h"
1010
#include "class_identifier.h"
1111
#include "remove_instanceof.h"
12+
#include <util/fresh_symbol.h>
1213

1314
#include <sstream>
1415

@@ -20,8 +21,7 @@ class remove_instanceoft
2021
goto_functionst &_goto_functions):
2122
symbol_table(_symbol_table),
2223
ns(_symbol_table),
23-
goto_functions(_goto_functions),
24-
lowered_count(0)
24+
goto_functions(_goto_functions)
2525
{
2626
class_hierarchy(_symbol_table);
2727
}
@@ -34,7 +34,6 @@ class remove_instanceoft
3434
namespacet ns;
3535
class_hierarchyt class_hierarchy;
3636
goto_functionst &goto_functions;
37-
int lowered_count;
3837

3938
bool lower_instanceof(goto_programt &);
4039

@@ -128,15 +127,14 @@ void remove_instanceoft::lower_instanceof(
128127
symbol_typet jlo("java::java.lang.Object");
129128
exprt object_clsid=get_class_identifier_field(check_ptr, jlo, ns);
130129

131-
std::ostringstream symname;
132-
symname << "instanceof_tmp::instanceof_tmp" << (++lowered_count);
133-
auxiliary_symbolt newsym;
134-
newsym.name=symname.str();
135-
newsym.type=object_clsid.type();
136-
newsym.base_name=newsym.name;
137-
newsym.mode=ID_java;
138-
newsym.is_type=false;
139-
assert(!symbol_table.add(newsym));
130+
symbolt &newsym=
131+
get_fresh_aux_symbol(
132+
object_clsid.type(),
133+
"instanceof_tmp",
134+
"instanceof_tmp",
135+
source_locationt(),
136+
ID_java,
137+
symbol_table);
140138

141139
auto newinst=goto_program.insert_after(this_inst);
142140
newinst->make_assignment();

src/java_bytecode/java_object_factory.cpp

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Author: Daniel Kroening, [email protected]
1010
#include <sstream>
1111

1212
#include <util/arith_tools.h>
13+
#include <util/fresh_symbol.h>
1314
#include <util/std_types.h>
1415
#include <util/std_code.h>
1516
#include <util/std_expr.h>
@@ -107,8 +108,7 @@ exprt java_object_factoryt::allocate_object(
107108
bool cast_needed=allocate_type_resolved!=target_type;
108109
if(!create_dynamic_objects)
109110
{
110-
symbolt &aux_symbol=new_tmp_symbol(symbol_table);
111-
aux_symbol.type=allocate_type;
111+
symbolt &aux_symbol=new_tmp_symbol(symbol_table, loc, allocate_type);
112112
aux_symbol.is_static_lifetime=true;
113113

114114
exprt object=aux_symbol.symbol_expr();
@@ -136,8 +136,11 @@ exprt java_object_factoryt::allocate_object(
136136
// Create a symbol for the malloc expression so we can initialize
137137
// without having to do it potentially through a double-deref, which
138138
// breaks the to-SSA phase.
139-
symbolt &malloc_sym=new_tmp_symbol(symbol_table, "malloc_site");
140-
malloc_sym.type=pointer_typet(allocate_type);
139+
symbolt &malloc_sym=new_tmp_symbol(
140+
symbol_table,
141+
loc,
142+
pointer_typet(allocate_type),
143+
"malloc_site");
141144
code_assignt assign=code_assignt(malloc_sym.symbol_expr(), malloc_expr);
142145
code_assignt &malloc_assign=assign;
143146
malloc_assign.add_source_location()=loc;
@@ -211,8 +214,11 @@ void java_object_factoryt::gen_nondet_init(
211214
if(!assume_non_null)
212215
{
213216
auto returns_null_sym=
214-
new_tmp_symbol(symbol_table, "opaque_returns_null");
215-
returns_null_sym.type=c_bool_typet(1);
217+
new_tmp_symbol(
218+
symbol_table,
219+
loc,
220+
c_bool_typet(1),
221+
"opaque_returns_null");
216222
auto returns_null=returns_null_sym.symbol_expr();
217223
auto assign_returns_null=
218224
code_assignt(returns_null, get_nondet_bool(returns_null_sym.type));
@@ -365,8 +371,11 @@ void java_object_factoryt::gen_nondet_array_init(
365371
auto max_length_expr=from_integer(max_nondet_array_length, java_int_type());
366372

367373
typet allocate_type;
368-
symbolt &length_sym=new_tmp_symbol(symbol_table, "nondet_array_length");
369-
length_sym.type=java_int_type();
374+
symbolt &length_sym=new_tmp_symbol(
375+
symbol_table,
376+
loc,
377+
java_int_type(),
378+
"nondet_array_length");
370379
const auto &length_sym_expr=length_sym.symbol_expr();
371380

372381
// Initialize array with some undetermined length:
@@ -400,17 +409,23 @@ void java_object_factoryt::gen_nondet_array_init(
400409

401410
// Interpose a new symbol, as the goto-symex stage can't handle array indexing
402411
// via a cast.
403-
symbolt &array_init_symbol=new_tmp_symbol(symbol_table, "array_data_init");
404-
array_init_symbol.type=init_array_expr.type();
412+
symbolt &array_init_symbol=new_tmp_symbol(
413+
symbol_table,
414+
loc,
415+
init_array_expr.type(),
416+
"array_data_init");
405417
const auto &array_init_symexpr=array_init_symbol.symbol_expr();
406418
codet data_assign=code_assignt(array_init_symexpr, init_array_expr);
407419
data_assign.add_source_location()=loc;
408420
init_code.copy_to_operands(data_assign);
409421

410422
// Emit init loop for(array_init_iter=0; array_init_iter!=array.length;
411423
// ++array_init_iter) init(array[array_init_iter]);
412-
symbolt &counter=new_tmp_symbol(symbol_table, "array_init_iter");
413-
counter.type=length_sym_expr.type();
424+
symbolt &counter=new_tmp_symbol(
425+
symbol_table,
426+
loc,
427+
length_sym_expr.type(),
428+
"array_init_iter");
414429
exprt counter_expr=counter.symbol_expr();
415430

416431
exprt java_zero=from_integer(0, java_int_type());
@@ -512,22 +527,19 @@ Function: new_tmp_symbol
512527
513528
\*******************************************************************/
514529

515-
symbolt &new_tmp_symbol(symbol_tablet &symbol_table, const std::string &prefix)
530+
symbolt &new_tmp_symbol(
531+
symbol_tablet &symbol_table,
532+
const source_locationt &loc,
533+
const typet &type,
534+
const std::string &prefix)
516535
{
517-
static size_t temporary_counter=0; // TODO encapsulate as class variable
518-
519-
auxiliary_symbolt new_symbol;
520-
symbolt *symbol_ptr;
521-
522-
do
523-
{
524-
new_symbol.name="tmp_object_factory$"+std::to_string(++temporary_counter);
525-
new_symbol.base_name=new_symbol.name;
526-
new_symbol.mode=ID_java;
527-
}
528-
while(symbol_table.move(new_symbol, symbol_ptr));
529-
530-
return *symbol_ptr;
536+
return get_fresh_aux_symbol(
537+
type,
538+
"",
539+
prefix,
540+
loc,
541+
ID_java,
542+
symbol_table);
531543
}
532544

533545
/*******************************************************************\
@@ -572,8 +584,10 @@ exprt object_factory(
572584
{
573585
if(type.id()==ID_pointer)
574586
{
575-
symbolt &aux_symbol=new_tmp_symbol(symbol_table);
576-
aux_symbol.type=type;
587+
symbolt &aux_symbol=new_tmp_symbol(
588+
symbol_table,
589+
loc,
590+
type);
577591
aux_symbol.is_static_lifetime=true;
578592

579593
exprt object=aux_symbol.symbol_expr();

0 commit comments

Comments
 (0)